-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10ftwm.c
1102 lines (866 loc) · 29.1 KB
/
10ftwm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <xcb/xcb.h>
#include <xcb/xcb_ewmh.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <stdbool.h>
#include <linux/joystick.h>
#include <lirc/lirc_client.h>
#define OSD_FONT "12x24"
//#define OSD_FONT "-urw-urw palladio l-medium-r-normal--0-0-0-0-p-0-iso8859-16"
#define OSD_H_W 100
//Keypoard buttons
#define L_SHIFT 40
#define UP_ARROW 111
#define D_ARROW 116
#define R_ARROW 114
#define L_ARROW 113
//Gamepad buttons
#define GP_TOTAL_KEYS 10
#define GP_KEY_A 0
#define GP_KEY_B 1
#define GP_KEY_HOME 8
//Function modifiers
#define TOTAL_FUNCTIONS 4
#define SHOW_OSD 0
#define NEXT_WORKSPACE 1
#define PREVIOUS_WORKSPACE 2
#define REMOVE_WINDOW 3
/* --- List stuff --- */
typedef struct node
{
void* data;
struct node* next;
} linkedList;
typedef enum {
VOID,
INT,
STRING,
FLOAT
} LIST_TYPE;
linkedList* createList(void* data);
int sizeOfList(linkedList head);
void * getFromList(linkedList head, int index);
void appendToList(linkedList* head, void * data);
void removeFromList(linkedList* head, int index);
int indexOf( linkedList list, void * data, LIST_TYPE type );
void printList ( linkedList list, LIST_TYPE type );
/* --- End of List stuff --- */
void setupWindows();
int launch(char *program);
void updateCurrentWindow(int index);
void addWindow( xcb_window_t e );
void destroyWindow( const unsigned int index );
void toggleOSD();
int loop();
//Init
void processInput(int argc, char **argv);
int readFromFileAndConfigure(char* filename);
void parseKeyValueConfigPair(char* key, char* value);
char* strip( const char* str, const char* stripof);
//Is the WM all set up
//and running?
int looping = 0;
int joystick;
bool hasJoystick;
bool haslirc;
int gpKeyLastPressed[ GP_TOTAL_KEYS ];
int gpKeyMap[ TOTAL_FUNCTIONS ];
char* displayName;
//LIRC
struct lirc_config *lConfig;
//CONFIG
int screen_number;
//END_CONFIG
//Our primary screen
xcb_screen_t* screen;
//Our connection to the xServer
xcb_connection_t *connection;
//Our current window (always at front)
int currentWindowIndex;
//A list of windows
linkedList windowList;
//Our on-screen-display
xcb_window_t osd;
bool osdActive;
xcb_font_t osdFont;
//The graphics context used by the OSD
xcb_gcontext_t osdGC;
//The file to look for, for lIRC
char* lirc_fp = "/dev/lirc0";
char* lirc_config = NULL;
//The file descriptor for lirc
int lirc_fd = -1;
//The file to look for, for reading controller values
char* js_fp = "/dev/input/js0";
void processInput(int argc, char **argv)
{
int opt;
displayName = NULL;
screen_number = 0;
while ((opt = getopt(argc, argv, "hs:d:")) != -1)
{
switch (opt)
{
case 'd':
printf("Opening on display %s...\n", optarg);
displayName = optarg;
break;
case 's':
printf("Opening on screen number %i...\n", atoi(optarg));
screen_number = atoi(optarg);
break;
case 'h':
printf("\
\n10ftwm: A lightweight window manager designed to be used with media centers and other '10 foot' devices.\
\nUsage: 10ftwm -d [DISPLAY NAME] -s [SCREEN NUMBER]\
\nDISPAY NAME:\
\nThe display to open. Default is the contents of your $DISPLAY variable.\
\nSCREEN NUMBER:\
\nThe screen number to open. Default is 0.\n");
exit(0);
break;
}
}
}
int launch(char *program)
{
pid_t pid;
pid = fork();
if (-1 == pid)
{
perror("fork");
return -1;
}
else if (0 == pid)
{
char *argv[2];
/* In the child. */
/*
* Make this process a new process leader, otherwise the
* terminal will die when the wm dies. Also, this makes any
* SIGCHLD go to this process when we fork again.
*/
if (-1 == setsid())
{
perror("setsid");
exit(1);
}
argv[0] = program;
argv[1] = NULL;
if (-1 == execvp(program, argv))
{
perror("execve");
exit(1);
}
exit(0);
}
return 0;
}
int readFromFileAndConfigure(char* filename)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Couldn't open configuration file!\n");
return 0;
}
printf("Found configuration file '%s'!\n", filename);
//This is a pointer to the equals sign in the string
//(or the last space after the equals sign)
//basically: this is the last character before the value of
// option = value
char* equalSign;
//The value to set
char* value;
while ((read = getline(&line, &len, fp)) != -1)
{
//printf("Retrieved line of length %zu, and size %zu :\n", read, len);
equalSign = strchr(line, '=');
value = strtok(equalSign, "=");
value = strtok(value, " ");
while( value != NULL && isspace(value[0]) )
{
value = strtok(NULL, " ");
}
value = strip(value, "\n");
parseKeyValueConfigPair(line,value);
}
if (line)
free(line);
fclose(fp);
return 1;
}
void parseKeyValueConfigPair(char* key, char* value)
{
if( strncmp("screen", key, strlen("screen")) == 0 && !looping )
{
printf("[CONF] Opening on screen #%i\n", atoi(value));
screen_number = atoi(value);
}
else if( strncmp("display", key, strlen("display")) == 0 && !looping )
{
printf("[CONF] Opening on display: %s\n", value);
displayName = value;
}
else if( strncmp("OSD_button", key, strlen("OSD_button")) == 0 )
{
printf("[CONF] Gamepad OSD Toggle Button set to button #%i\n", atoi(value));
gpKeyMap[ SHOW_OSD ] = atoi(value);
}
else if( strncmp("OSD_next_ws", key, strlen("OSD_next_ws")) == 0 )
{
printf("[CONF] Gamepad OSD Next Workspace Button set to button #%i\n", atoi(value));
gpKeyMap[ NEXT_WORKSPACE ] = atoi(value);
}
else if( strncmp("OSD_previous_ws", key, strlen("OSD_previous_ws")) == 0 )
{
printf("[CONF] Gamepad OSD Previous Workspace Button set to button #%i\n", atoi(value));
gpKeyMap[ PREVIOUS_WORKSPACE ] = atoi(value);
}
else if( strncmp("OSD_remove_window", key, strlen("OSD_remove_window")) == 0 )
{
printf("[CONF] Gamepad OSD Remove Window Button set to button#%i\n", atoi(value));
gpKeyMap[ REMOVE_WINDOW ] = atoi(value);
}
else if( strncmp("js_file", key, strlen("js_file")) == 0 )
{
printf("[CONF] Joystick (controller) file to be read: %s\n", value);
js_fp = value;
}
else if( strncmp("lirc_config", key, strlen("lirc_config")) == 0 )
{
printf("[CONF] lirc (remote) configuration file to be used: %s\n", value);
lirc_fp = value;
}
else if( strncmp("exec", key, strlen("exec")) == 0 && looping )
{
printf("EXECUTING %s\n", value);
launch(value);
}
}
int main (int argc, char **argv)
{
uint32_t values[3];
uint32_t mask = 0;
osdActive = false;
for(int i = 0; i < GP_TOTAL_KEYS; i++)
gpKeyLastPressed[i] = 0;
for(int i = 0; i < TOTAL_FUNCTIONS; i++)
gpKeyMap[i] = 0;
gpKeyMap[SHOW_OSD] = GP_KEY_HOME;
gpKeyMap[NEXT_WORKSPACE] = GP_KEY_B;
gpKeyMap[PREVIOUS_WORKSPACE] = GP_KEY_A;
//Our main graphics context
//So... the background?
xcb_drawable_t mainGC;
xcb_void_cookie_t cookie;
xcb_generic_error_t *error;
processInput(argc, argv);
//readFromFileAndConfigure("10ftwmrc");
//Don't have a display name or number,
//so just go with what xcb gives us.
connection = xcb_connect(displayName, &screen_number);
if( xcb_connection_has_error(connection) )
{
printf("Error, could not connect to display!\n");
return 1;
}
screen = xcb_setup_roots_iterator( xcb_get_setup(connection)).data;
printf("Got screen. Width/height: %ix%i\n", screen->width_in_pixels, screen->height_in_pixels);
mainGC = screen->root;
//Get osd id
osd = xcb_generate_id( connection );
//Font id
osdFont = xcb_generate_id( connection );
//Open font
char* fontName = OSD_FONT;
cookie = xcb_open_font(connection, osdFont, strlen(fontName), fontName);
error = xcb_request_check (connection, cookie);
if (error)
{
printf ("ERROR: can't open font : %d\n", error->error_code);
}
else
printf("Loaded font '%s'.\n", fontName);
//OSD values
mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
values[0] = screen->white_pixel;
values[1] = XCB_EVENT_MASK_BUTTON_PRESS;
//Create OSD
xcb_create_window( connection, XCB_COPY_FROM_PARENT, osd, mainGC, 0,0 , OSD_H_W,OSD_H_W , 14, XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, mask, values);
osdGC = xcb_generate_id(connection);
mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
values[0] = screen->black_pixel;
values[1] = screen->white_pixel;
values[2] = osdFont;
xcb_create_gc (connection, osdGC, osd, mask, values);
//Grab keys/mouse buttons
xcb_grab_key(connection, 1, mainGC, XCB_MOD_MASK_SHIFT, R_ARROW,
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
xcb_grab_key(connection, 1, mainGC, XCB_MOD_MASK_SHIFT, L_ARROW,
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
xcb_grab_key(connection, 1, mainGC, XCB_MOD_MASK_SHIFT, UP_ARROW,
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
xcb_grab_key(connection, 1, mainGC, XCB_MOD_MASK_SHIFT, D_ARROW,
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
xcb_grab_button(connection, 0, mainGC, XCB_EVENT_MASK_BUTTON_PRESS |
XCB_EVENT_MASK_BUTTON_RELEASE, XCB_GRAB_MODE_ASYNC,
XCB_GRAB_MODE_ASYNC, mainGC, XCB_NONE, 1, XCB_MOD_MASK_1);
xcb_grab_button(connection, 0, mainGC, XCB_EVENT_MASK_BUTTON_PRESS |
XCB_EVENT_MASK_BUTTON_RELEASE, XCB_GRAB_MODE_ASYNC,
XCB_GRAB_MODE_ASYNC, mainGC, XCB_NONE, 3, XCB_MOD_MASK_1);
/* Subscribe to events. */
mask = XCB_CW_EVENT_MASK;
values[0] = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
| XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
| XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_KEY_PRESS;
cookie = xcb_change_window_attributes_checked(connection, mainGC, mask, values);
error = xcb_request_check(connection, cookie);
if(error != NULL)
{
printf("Are you, like, trying to run TWO window managers or something?\n");
exit(1);
}
xcb_flush(connection);
hasJoystick = false;
joystick = open (js_fp, O_RDONLY | O_NONBLOCK);
if(joystick >= 0)
{
printf("Found joystick!\n");
hasJoystick = true;
}
else
printf("Could not find joystick :(\n");
haslirc = false;
int fd = lirc_init("mceusb",1);
//Initialize LIRC
if(fd ==-1)
printf("Lirc not detected!\n");;
if(lirc_readconfig(lirc_config ? lirc_config : NULL ,&lConfig,NULL)==0)
{
printf("lirc config initialized!\n");
haslirc = true;
//lirc_fd = open(lirc_fp, O_RDONLY | O_NONBLOCK);
lirc_fd = fd;
}
else
printf("Failed to initialize lirc config!\n");
looping = 1;
char* home = getenv("HOME");
//Check if there is a configuration file in the same
//directory as the executable
if(!readFromFileAndConfigure("10ftwmrc") && home)
{
//...if no config is found, and the user's home
//folder is known, try to load the config from there
printf("Couldn't find config in same directory as executable. Searching home for config: %s\n", home);
char toAppend[] = "/.10ftwmrc";
const size_t pathLength = strlen(home) + strlen(toAppend);
char *const rcPath = malloc( sizeof(char) * (pathLength + 1) );
strcpy(rcPath, home);
strcpy(rcPath + strlen(home), toAppend);
readFromFileAndConfigure(rcPath);
free(rcPath);
}
//xcb_ewmh_set_desktop_geometry( xcb_ewmh_connection, 0, 1920, 1080 );
while(looping)
looping = loop();
return 0;
}
int loop()
{
//Variables for
//select()
int fd;
fd_set in;
//Get X's file descriptor, we already have
//the joystick's one
fd = xcb_get_file_descriptor(connection);
FD_ZERO(&in);
FD_SET(fd, &in);
if(haslirc)
FD_SET(lirc_fd, &in);
if(hasJoystick)
FD_SET(joystick, &in);
int max = fd;
if(lirc_fd > max) max = lirc_fd;
if(joystick > max) max = joystick;
select(max+1, &in, NULL, NULL, NULL);
// #justeventthings
xcb_generic_event_t *ev;
if( FD_ISSET(fd, &in) )
while( (ev = xcb_poll_for_event(connection)) )
{
switch (ev->response_type & ~0x80)
{
case XCB_BUTTON_PRESS:
{
printf("Dat button press\n");
return 0;
}
break;
case XCB_KEY_PRESS:
{
xcb_key_press_event_t* e;
e = (xcb_key_press_event_t*) ev;
printf("You pressed %i\n", e->detail);
if(e->detail == R_ARROW)
updateCurrentWindow(currentWindowIndex+1);
if(e->detail == L_ARROW)
updateCurrentWindow(currentWindowIndex-1);
if(e->detail == D_ARROW)
destroyWindow(currentWindowIndex);
if(e->detail == UP_ARROW)
toggleOSD();
}
break;
case XCB_DESTROY_NOTIFY:
{
xcb_destroy_notify_event_t *e;
e = (xcb_destroy_notify_event_t *) ev;
printf("Received window destroy notification for window %i.\n", e->window);
int index = indexOf( windowList, &(e->window), INT );
if(index == -1)
{
printf(" Could not find window %i in list.\n", e->window);
break;
}
removeFromList( &windowList, index);
int numWindows = sizeOfList(windowList);
printf("Removed window %i from list. There are now a total of %i windows.\n", index, sizeOfList(windowList) );
if(currentWindowIndex >= numWindows)
currentWindowIndex = numWindows-1;
else
if(currentWindowIndex < 0)
currentWindowIndex = 0;
}
break;
case XCB_MAP_REQUEST:
{
xcb_map_request_event_t *e;
e = (xcb_map_request_event_t *) ev;
printf("Received map request for window %i.\n", e->window);
addWindow(e->window);
}
break;
case XCB_MAP_NOTIFY:
{
xcb_map_notify_event_t *e;
e = (xcb_map_notify_event_t *) ev;
if( indexOf( windowList, &(e->window), INT ) == -1 && e->window != osd)
{
addWindow(e->window);
printf("Received map notify for window %i.\n", e->window);
}
}
case XCB_BUTTON_RELEASE:
{
xcb_ungrab_pointer(connection, XCB_CURRENT_TIME);
xcb_flush(connection);
}
break;
}
}
if(hasJoystick && FD_ISSET(joystick, &in) )
{
struct js_event event;
//Reading...
read(joystick, &event, sizeof(event));
//...books!
if(event.type == JS_EVENT_BUTTON)
{
//If button press
if(event.value == 1 && gpKeyLastPressed[ event.number ] == 0)
{
printf("You pressed button %i on the gamepad!\n", event.number);
//Only allow controller functions while the OSD is active
//...this does not apply to the Home key, which is responsible
//for toggling the OSD
if(osdActive || event.number == gpKeyMap[SHOW_OSD])
{
if(event.number == gpKeyMap[NEXT_WORKSPACE])
{
updateCurrentWindow(currentWindowIndex+1);
}
else if(event.number == gpKeyMap[PREVIOUS_WORKSPACE])
{
updateCurrentWindow(currentWindowIndex-1);
}
else if(event.number == gpKeyMap[SHOW_OSD])
{
toggleOSD();
}
else if(event.number == gpKeyMap[REMOVE_WINDOW])
destroyWindow( currentWindowIndex );
}
gpKeyLastPressed[ event.number ] = 1;
}
//If button release
else
if(event.value != 1 && gpKeyLastPressed[ event.number ] == 1)
{
printf("You released button %i on the gamepad!\n", event.number);
gpKeyLastPressed[ event.number ] = 0;
}
}
}
if(haslirc && FD_ISSET(lirc_fd, &in) )
{
char *code;
char *string;
lirc_nextcode(&code);
lirc_code2char(lConfig,code,&string);
if(string != NULL)
{
if( strncmp("TOGGLE_OSD", string, strlen("TOGGLE_OSD")) == 0 )
{
printf("lirc toggle osd command received!\n");
toggleOSD();
}
else if( strncmp("NEXT_WORKSPACE", string, strlen("NEXT_WORKSPACE")) == 0 )
{
printf("lirc next workspace command received!\n");
updateCurrentWindow(currentWindowIndex+1);
}
else if( strncmp("PREVIOUS_WORKSPACE", string, strlen("PREVIOUS_WORKSPACE")) == 0 )
{
printf("lirc previous workspace command received!\n");
updateCurrentWindow(currentWindowIndex-1);
}
else if( strncmp("REMOVE_WINDOW", string, strlen("REMOVE_WINDOW")) == 0 )
{
printf("lirc remove window command received!\n");
destroyWindow(currentWindowIndex);
}
else
printf("Unkown lirc command: %s\n", string);
}
}
return 1;
}
void updateCurrentWindow(int index)
{
//Sanity check to see if we have any windows
//to begin with
if(!sizeOfList(windowList))
return;
currentWindowIndex = index;
if(currentWindowIndex >= sizeOfList(windowList) )
currentWindowIndex = 0;
if(currentWindowIndex < 0)
currentWindowIndex = sizeOfList(windowList)-1;
printf("Bringing window %i to front\n", currentWindowIndex);
uint32_t values[] = { XCB_STACK_MODE_ABOVE };
xcb_configure_window (connection, *((int*)getFromList( windowList, currentWindowIndex )), XCB_CONFIG_WINDOW_STACK_MODE, values);
if( osdActive)
xcb_configure_window (connection, osd, XCB_CONFIG_WINDOW_STACK_MODE, values);
//This only works for numbers 0-9, should be changed in the future!
char curPos = (char)(((int)'0')+currentWindowIndex);
xcb_image_text_8(connection, sizeof(curPos), osd, osdGC, OSD_H_W/2, OSD_H_W/2, &curPos);
xcb_set_input_focus( connection, XCB_INPUT_FOCUS_PARENT, *((int*)getFromList( windowList, currentWindowIndex )), XCB_CURRENT_TIME );
xcb_flush(connection);
}
void toggleOSD()
{
if( !osdActive )
{
xcb_map_window(connection, osd);
osdActive = true;
uint32_t values[] = { XCB_STACK_MODE_ABOVE };
xcb_configure_window (connection, osd, XCB_CONFIG_WINDOW_STACK_MODE, values);
if(sizeOfList(windowList) > 0)
{
char curPos;
//This only works for numbers 0-9, should be changed in the future!
curPos = (char)(((int)'0')+currentWindowIndex);
xcb_image_text_8(connection, sizeof(curPos), osd, osdGC, OSD_H_W/2, OSD_H_W/2, &curPos);
}
else
{
char* naTxt = "N/A";
xcb_image_text_8(connection, sizeof(naTxt), osd, osdGC, OSD_H_W/2, OSD_H_W/2, naTxt);
}
printf("OSD enabled.\n");
}
else
{
xcb_unmap_window(connection, osd);
osdActive = false;
printf("OSD disabled.\n");
}
xcb_flush(connection);
}
void destroyWindow( const unsigned int index )
{
if( sizeOfList( windowList ) <= 0 )
{
printf("[Warning] Trying to remove a window when there are none!\n");
return;
}
xcb_window_t window = *((int*)getFromList( windowList, index )) ;
xcb_void_cookie_t cookie = xcb_destroy_window(connection, window);
xcb_generic_error_t* error = xcb_request_check( connection, cookie );
if(error != NULL)
{
printf("Something went wrong while removing a window!\n");
free( error );
}
removeFromList( &windowList, index);
int numWindows = sizeOfList(windowList);
printf("Removed window %i from list. There are now a total of %i windows.\n", index, sizeOfList(windowList) );
if(currentWindowIndex >= numWindows)
currentWindowIndex = numWindows-1;
else
if(currentWindowIndex < 0)
currentWindowIndex = 0;
updateCurrentWindow( currentWindowIndex );
}
void addWindow( xcb_window_t window )
{
uint32_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
uint32_t values[4];
xcb_void_cookie_t cookie;
xcb_generic_error_t *error;
values[0] = 0;
values[1] = 0;
values[2] = screen->width_in_pixels;
values[3] = screen->height_in_pixels;
xcb_configure_window( connection, window, mask, values);
cookie = xcb_map_window(connection, window);
//Man... why did I have to use generics for this?
//This is a pain... perhaps I should just switch back to storing ints?
//But, yes, here we're storing 'window' in the list.
void* toStore = malloc( sizeof(xcb_window_t) );
*((xcb_window_t*)toStore) = window;
appendToList(&windowList, toStore);
if( osdActive)
{
//If our osd is supposed to be active,
//draw it on top of the current window
uint32_t value[] = { XCB_STACK_MODE_ABOVE };
xcb_configure_window (connection, osd, XCB_CONFIG_WINDOW_STACK_MODE, value);
}
error = xcb_request_check(connection, cookie);
if(error != NULL)
printf("Something went wrong while adding a window!\n");
else
xcb_flush(connection);
printf("Added window %i into position %i. There are now a total of %i windows.\n", window, indexOf( windowList, &window, INT ), sizeOfList(windowList) );
updateCurrentWindow( indexOf( windowList, &window, INT ) );
}
//Strip from string str any chars contained in stripof
char* strip (const char *str, const char *stripof)
{
//The length of the string we're stripping
const int length = strlen(str);
//The length of the array holding the chars
//we're stripping
const int lengthSt = strlen(stripof);
int occurences = 0;
bool safe;
char *final;
int i, j;
for (i = 0; i < length; i++)
for(j = 0; j < lengthSt; j++)
if(str[i] == stripof[j])
occurences++;
final = malloc( sizeof(char) * (length-occurences) );
j = 0;
for (i = 0; i < length; i++)
{
safe = true;
for(int j = 0; j < lengthSt; j++)
if(str[i] == stripof[j])
safe=false;
if(safe)
{
final[j] = str[i];
j++;
}
}
//Required or else, for some odd reason,
//our incoming string ends up with garbage
//chars at the end. Really odd.
//(I've only seen this happen on Debian Wheezy)
final[i-1] = '\0';
return final;
}
/* --- LIST STUFF --- */
linkedList* createList(void* data)
{
linkedList* x = malloc( sizeof( linkedList ) );
x->data = data;
x->next = NULL;
return x;
}
int sizeOfList(linkedList head)
{
//if(isnan(head.data) || isinf(head.data) )
// return 0;
if( head.data == NULL ) return 0;
int size = 1;
linkedList* x = &head;
while(x->next != NULL)
{
size++;
x = x->next;
}
return size;
}
void* getFromList(linkedList head, int index)
{
if(index >= sizeOfList(head))
{
printf("ERROR: index out of range\n");
//This will always break something since we only ever
//use lists with unsigned ints.
return NULL;
}
linkedList* x = &head;
for(int i = 0; i < index; i++)
x = x->next;
return x->data;
}
void appendToList(linkedList* head, void* data)
{
//Sanity check to see if our list was
//not initialized with any data
//e.g. if we didn't use "createList"
if( head->data == NULL )
{
head->data = data;
return;
}
linkedList* x = malloc( sizeof(linkedList) );
x->data = data;
x->next = NULL;
if(head->next == NULL)
{
head->next = x;
}
else
{
linkedList* itr = head;
for(int i = 0; i < sizeOfList(*head) - 1; i++)
itr = itr->next;
itr->next = x;
}
}
void removeFromList(linkedList* head, int index)
{
int size = sizeOfList(*head);
if(size == 0)
return;
if( size == 1 )
{
head->data = NULL;
return;
}
//If we're trying to remove the first
//element of a list with more than one
//element.
if( index == 0 )
{
linkedList* toReplace = head->next;
head->data = toReplace->data;
head->next = toReplace->next;
toReplace->next = NULL;
free( toReplace );
return;
}
linkedList* x = head;
//Iterate until we get to the member
//before the one we're trying to remove
for( int i = 0; i < index - 1 ; i++)
x = x->next;
//The member we're actually removing.
linkedList* toDie = x->next;