-
Notifications
You must be signed in to change notification settings - Fork 34
/
frankenwm.c
3765 lines (3327 loc) · 118 KB
/
frankenwm.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
/* see license for copyright and license */
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include <stdarg.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <regex.h>
#include <sys/wait.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <xcb/xcb.h>
#include <xcb/xcb_atom.h>
#include <xcb/xcb_aux.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xcb_keysyms.h>
#include <xcb/xcb_ewmh.h>
/* compile with -DDEBUGGING for debugging output */
#ifdef DEBUGGING
# define DEBUG(x) fprintf(stderr, "%s\n", x);
# define DEBUGP(x, ...) fprintf(stderr, x, ##__VA_ARGS__);
#else
# define DEBUG(x);
# define DEBUGP(x, ...);
#endif
/* upstream compatility */
#define XCB_MOVE_RESIZE XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT
#define XCB_MOVE XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
#define XCB_RESIZE XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT
static char *WM_NAME = "FrankenWM";
static char *WM_ATOM_NAME[] = { "WM_PROTOCOLS", "WM_DELETE_WINDOW", "WM_STATE", "WM_TAKE_FOCUS" };
enum { WM_PROTOCOLS, WM_DELETE_WINDOW, WM_STATE, WM_TAKE_FOCUS, WM_COUNT };
enum { _NET_WM_STATE_REMOVE, _NET_WM_STATE_ADD, _NET_WM_STATE_TOGGLE };
#define LENGTH(x) (sizeof(x)/sizeof(*x))
#define CLEANMASK(mask) (mask & ~(numlockmask | XCB_MOD_MASK_LOCK))
#define BUTTONMASK XCB_EVENT_MASK_BUTTON_PRESS|XCB_EVENT_MASK_BUTTON_RELEASE
#define ISFMFTM(c) (c->isfullscreen || c->ismaximized || c->isfloating || c->istransient || c->isminimized || c->type != ewmh->_NET_WM_WINDOW_TYPE_NORMAL)
#define USAGE "usage: frankenwm [-h] [-v]"
/* future enhancements */
#define MONITORS 1
enum { RESIZE, MOVE };
enum { TILE, MONOCLE, BSTACK, GRID, FIBONACCI, DUALSTACK, EQUAL, MODES };
/* argument structure to be passed to function by config.h
* com - a command to run
* i - an integer to indicate different states
*/
typedef union {
const char **com;
const int i;
} Arg;
struct list {
struct node *head;
struct node *tail;
void *master; /* backpointer to the list's owner */
};
typedef struct list list;
struct node {
struct node *prev;
struct node *next;
struct list *parent;
};
typedef struct node node;
typedef struct {
int previous_x, previous_y;
int current_x, current_y;
} posxy_t;
/*
* aliens are unmanaged & selfmapped windows, ie dunst notifications.
* They are always on top of all other windows.
*/
struct alien {
node link;
xcb_window_t win;
xcb_atom_t type;
posxy_t position_info;
};
typedef struct alien alien;
/* a key struct represents a combination of
* mod - a modifier mask
* keysym - and the key pressed
* func - the function to be triggered because of the above combo
* arg - the argument to the function
*/
typedef struct {
unsigned int mod;
xcb_keysym_t keysym;
void (*func)(const Arg *);
const Arg arg;
} key;
/* a button struct represents a combination of
* mask - a modifier mask
* button - and the mouse button pressed
* func - the function to be triggered because of the above combo
* arg - the argument to the function
*/
typedef struct {
unsigned int mask, button;
void (*func)(const Arg *);
const Arg arg;
} Button;
/* a client is a wrapper to a window that additionally
* holds some properties for that window
*
* link - doubly linked list node
* isurgent - set when the window received an urgent hint
* istransient - set when the window is transient
* isfullscreen - set when the window is fullscreen (not maximized)
* ismaximized - set when the window is maximized (not fullscreen)
* isfloating - set when the window is floating
* win - the window this client is representing
* type - the _NET_WM_WINDOW_TYPE
* dim - the window dimensions when floating
* borderwidth - the border width if not using the global one
* setfocus - True: focus directly, else send wm_take_focus
*
* istransient is separate from isfloating as floating window can be reset
* to their tiling positions, while the transients will always be floating
*/
/*
* Developer reminder: do not forget to update create_client();
*/
typedef struct {
node link; /* must be first */
bool isurgent, istransient, isfloating, isfullscreen, ismaximized, isminimized;
xcb_window_t win;
xcb_atom_t type;
unsigned int dim[2];
posxy_t position_info;
int borderwidth;
bool setfocus;
} client;
/* properties of each desktop
* current - the currently highlighted window
* prevfocus - the client that previously had focus
* mode - the desktop's tiling layout mode
* growth - growth factor of the first stack window
* master_size - the size of the master window
* showpanel - the visibility status of the panel
*/
typedef struct {
float master_size;
int mode, growth, gaps;
bool showpanel, invert;
} displayinfo;
#define M_MASTER_SIZE (current_display->di.master_size)
#define M_MODE (current_display->di.mode)
#define M_GROWTH (current_display->di.growth)
#define M_GAPS (current_display->di.gaps)
#define M_SHOWPANEL (current_display->di.showpanel)
#define M_INVERT (current_display->di.invert)
/* properties of each display
* current - the currently highlighted window
*/
typedef struct {
node link; /* must be first */
list clients; /* must be second */
client *current, *prevfocus;
list miniq;
displayinfo di;
} display;
#define M_CURRENT (current_display->current)
#define M_PREVFOCUS (current_display->prevfocus)
typedef struct {
node link; /* must be first */
list displays; /* must be second */
unsigned int num;
int ww, wh;
int wy;
} monitor;
/* desktop */
typedef struct {
node link; /* must be first */
list monitors; /* must be second */
unsigned int num;
} desktop;
/* lifo for minimized clients */
typedef struct lifo {
node link; /* must be first */
client *c;
} lifo;
/* define behavior of certain applications
* configured in config.h
* class - the class of the window
* instance - the instance of the window
* desktop - what desktop it should be spawned at
* follow - whether to change desktop focus to the specified desktop
*/
typedef struct {
const char *class;
const char *instance;
const int desktop;
const bool follow, floating;
const int border_width;
} AppRule;
/* function prototypes sorted alphabetically */
static client *addwindow(xcb_window_t w, xcb_atom_t wtype);
static void adjust_borders(const Arg *arg);
static void adjust_gaps(const Arg *arg);
static void buttonpress(xcb_generic_event_t *e);
static void change_desktop(const Arg *arg);
static bool check_if_window_is_alien(xcb_window_t win, bool *isFloating, xcb_atom_t *wtype);
static bool check_wmproto(xcb_window_t win, xcb_atom_t proto);
static void centerfloating(client *c);
static void centerwindow();
static void cleanup(void);
static void cleanup_display(void);
static int client_borders(const client *c);
static void client_to_desktop(const Arg *arg);
static void clientmessage(xcb_generic_event_t *e);
static void configurerequest(xcb_generic_event_t *e);
static inline alien *create_alien(xcb_window_t win, xcb_atom_t atom);
static client *create_client(xcb_window_t win, xcb_atom_t wtype);
static bool deletewindow(xcb_window_t w);
static void desktopinfo(void);
static void destroynotify(xcb_generic_event_t *e);
static void dualstack(int hh, int cy);
static void enternotify(xcb_generic_event_t *e);
static void equal(int h, int y);
static void fibonacci(int h, int y);
static client *find_client(xcb_window_t w);
static desktop *find_desktop(unsigned int n);
/* static monitor *find_monitor(unsigned int n); future enhancement */
static void float_client(client *c);
static void float_x(const Arg *arg);
static void float_y(const Arg *arg);
static void focusmaster();
static void focusurgent();
static unsigned int getcolor(char *color);
static void grabbuttons(client *c);
static void grabkeys(void);
static void grid(int h, int y);
static void invertstack();
static void keypress(xcb_generic_event_t *e);
static void killclient();
static void last_desktop();
static void mapnotify(xcb_generic_event_t *e);
static void maprequest(xcb_generic_event_t *e);
static void maximize();
static void minimize_client(client *c);
static void minimize();
static void monocle(int h, int y);
static void move_down();
static void move_up();
static void mousemotion(const Arg *arg);
static void next_win();
static void prev_win();
static void propertynotify(xcb_generic_event_t *e);
static void quit(const Arg *arg);
static void removeclient(client *c);
static void resize_master(const Arg *arg);
static void resize_stack(const Arg *arg);
static void resize_x(const Arg *arg);
static void resize_y(const Arg *arg);
static void restore_client(client *c);
static void restore();
static bool desktop_populated(desktop *d);
static void rotate(const Arg *arg);
static void rotate_client(const Arg *arg);
static void rotate_filled(const Arg *arg);
static void rotate_mode(const Arg *arg);
static void run(void);
static void select_desktop(int i);
static bool sendevent(xcb_window_t win, xcb_atom_t proto);
static void setmaximize(client *c, bool fullscrn);
void setfullscreen(client *c, bool fullscrn);
static int setup(int default_screen);
static void setup_display(void);
static void setwindefattr(xcb_window_t w);
static void showhide();
static void sigchld();
static void spawn(const Arg *arg);
static void stack(int h, int y);
static void swap_master();
static void switch_mode(const Arg *arg);
static void tile(void);
static void tilemize();
static void togglepanel();
static void unfloat_client(client *c);
static void togglescratchpad();
static void update_current(client *c);
static void unmapnotify(xcb_generic_event_t *e);
static void xerror(xcb_generic_event_t *e);
static alien *wintoalien(list *l, xcb_window_t win);
static client *wintoclient(xcb_window_t w);
#include "config.h"
#ifdef EWMH_TASKBAR
typedef struct
{
uint16_t left, right, top, bottom;
// uint16_t left_start_y, left_end_y;
// uint16_t right_start_y, right_end_y;
// uint16_t top_start_x, top_end_x;
// uint16_t bottom_start_x, bottom_end_x;
} strut_t;
static void Setup_EWMH_Taskbar_Support(void);
static void Cleanup_EWMH_Taskbar_Support(void);
static inline void Update_EWMH_Taskbar_Properties(void);
static void Setup_Global_Strut(void);
static void Cleanup_Global_Strut(void);
static inline void Reset_Global_Strut(void);
static void Update_Global_Strut(void);
static strut_t gstrut;
#endif /* EWMH_TASKBAR */
/* variables */
static bool running = true, show = true, showscratchpad = false;
static int default_screen, previous_desktop, current_desktop_number, retval;
static int borders;
static unsigned int numlockmask, win_unfocus, win_focus, win_scratch;
static xcb_connection_t *dis;
static xcb_screen_t *screen;
static uint32_t checkwin;
static xcb_atom_t scrpd_atom;
static client *scrpd = NULL;
static list desktops;
static list aliens;
static desktop *current_desktop = NULL;
static monitor *current_monitor = NULL;
#define M_WW (current_monitor->ww)
#define M_WH (current_monitor->wh)
#define M_WY (current_monitor->wy)
static display *current_display = NULL;
#define M_HEAD ((client *)(current_display->clients.head))
#define M_TAIL ((client *)(current_display->clients.tail))
static xcb_ewmh_connection_t *ewmh;
static xcb_atom_t wmatoms[WM_COUNT];
static regex_t classruleregex[LENGTH(rules)];
static regex_t instanceruleregex[LENGTH(rules)];
static xcb_key_symbols_t *keysyms;
/* events array
* on receival of a new event, call the appropriate function to handle it
*/
static void (*events[XCB_NO_OPERATION])(xcb_generic_event_t *e);
/* layout array - given the current layout mode, tile the windows
* h (or hh) - avaible height that windows have to expand
* y (or cy) - offset from top to place the windows (reserved by the panel) */
static void (*layout[MODES])(int h, int y) = {
[TILE] = stack,
[BSTACK] = stack,
[GRID] = grid,
[MONOCLE] = monocle,
[FIBONACCI] = fibonacci,
[DUALSTACK] = dualstack,
[EQUAL] = equal,
};
/*
* lowlevel doubly linked list functions
*/
static node *rem_node(node *n)
{
list *l;
if (!n)
return NULL;
l = n->parent;
if (l) {
if (n == l->head) {
l->head = l->head->next;
if(l->head)
l->head->prev = NULL;
else
l->tail = NULL;
}
else if (n == l->tail) {
l->tail = l->tail->prev;
l->tail->next = NULL;
}
else {
n->prev->next = n->next;
n->next->prev = n->prev;
}
}
n->prev = n->next = NULL;
n->parent = NULL;
return n;
}
static void add_head(list *l, node *i)
{
node *o = l->head;
if (o == NULL) {
l->head = i;
l->tail = i;
}
else {
l->head = i;
i->prev = NULL;
i->next = o;
o->prev = i;
}
i->parent = l;
}
static void add_tail(list *l, node *i)
{
if(l->head == NULL)
add_head(l, i);
else {
node *o = l->tail;
l->tail = i;
o->next = i;
i->prev = o;
i->next = NULL;
}
i->parent = l;
}
static void insert_node_after(list *l, node *c, node *i)
{
node *n;
if (!c || !(n = c->next))
add_tail(l, i);
else {
c->next = i;
i->prev = c;
i->next = n;
n->prev = i;
}
i->parent = l;
}
static void insert_node_before(list *l, node *c, node *i)
{
node *p;
if (!c || !(p = c->prev))
add_head(l, i);
else {
p->next = i;
i->prev = p;
i->next = c;
c->prev = i;
}
i->parent = l;
}
/*
* glue functions for doubly linked stuff
*/
static inline bool check_head(list *l) { return (l && l->head) ? True : False; }
static inline node *get_head(list *l) { return (l) ? l->head : NULL; }
static inline node *rem_head(list *l) { return (l) ? rem_node(l->head) : NULL; }
static inline node *get_tail(list *l) { return (l) ? l->tail : NULL; }
static inline node *get_next(node *n) { return (n) ? n->next : NULL; }
static inline node *get_prev(node *n) { return (n) ? n->prev : NULL; }
static inline node *get_node_head(node *n) { return (n && n->parent) ? n->parent->head : NULL; }
#define M_GETNEXT(c) ((client *)get_next(&c->link))
#define M_GETPREV(c) ((client *)get_prev(&c->link))
/*
* Add an atom to a list of atoms the given property defines.
* This is useful, for example, for manipulating _NET_WM_STATE.
*
*/
void xcb_add_property(xcb_connection_t *con, xcb_window_t win, xcb_atom_t prop, xcb_atom_t atom)
{
xcb_change_property(con, XCB_PROP_MODE_APPEND, win, prop, XCB_ATOM_ATOM, 32, 1, (uint32_t[]){atom});
}
/*
* Remove an atom from a list of atoms the given property defines without
* removing any other potentially set atoms. This is useful, for example, for
* manipulating _NET_WM_STATE.
*
*/
void xcb_remove_property(xcb_connection_t *con, xcb_window_t win, xcb_atom_t prop, xcb_atom_t atom)
{
xcb_grab_server(con);
xcb_get_property_reply_t *reply =
xcb_get_property_reply(con,
xcb_get_property(con, False, win, prop, XCB_GET_PROPERTY_TYPE_ANY, 0, 4096), NULL);
if (reply == NULL || xcb_get_property_value_length(reply) == 0)
goto release_grab;
xcb_atom_t *atoms = xcb_get_property_value(reply);
if (atoms == NULL) {
goto release_grab;
}
{
int num = 0;
const int current_size = xcb_get_property_value_length(reply) / (reply->format / 8);
xcb_atom_t values[current_size];
for (int i = 0; i < current_size; i++) {
if (atoms[i] != atom)
values[num++] = atoms[i];
}
xcb_change_property(con, XCB_PROP_MODE_REPLACE, win, prop, XCB_ATOM_ATOM, 32, num, values);
}
release_grab:
if (reply)
free(reply);
xcb_ungrab_server(con);
}
static bool xcb_check_attribute(xcb_connection_t *con, xcb_window_t win, xcb_atom_t atom)
{
xcb_get_property_reply_t *prop_reply;
if ((prop_reply = xcb_get_property_reply(con, xcb_get_property(dis, 0, win, atom,
XCB_GET_PROPERTY_TYPE_ANY, 0, 0), NULL))) {
xcb_atom_t reply_type = prop_reply->type;
free(prop_reply);
if (reply_type != XCB_NONE)
return True;
}
return False;
}
/* get screen of display */
static xcb_screen_t *xcb_screen_of_display(xcb_connection_t *con, int screen)
{
xcb_screen_iterator_t iter;
iter = xcb_setup_roots_iterator(xcb_get_setup(con));
for (; iter.rem; --screen, xcb_screen_next(&iter))
if (screen == 0)
return iter.data;
return NULL;
}
/* wrapper to intern atom */
static inline xcb_atom_t xcb_internatom(xcb_connection_t *con, char *name, uint8_t only_if_exists)
{
xcb_atom_t atom;
xcb_intern_atom_cookie_t cookie;
xcb_intern_atom_reply_t *reply;
atom = 0;
cookie = xcb_intern_atom(con, only_if_exists, strlen(name), name);
reply = xcb_intern_atom_reply(con, cookie, NULL);
if (reply) {
atom = reply->atom;
free(reply);
}
/* TODO: Handle error */
return atom; // may be zero
}
/* wrapper to move and resize window */
static inline void xcb_move_resize(xcb_connection_t *con, xcb_window_t win, int x, int y, int w, int h, posxy_t *pi)
{
unsigned int pos[4] = { x, y, w, h };
if (pi) {
pi->previous_x = pi->current_x;
pi->previous_y = pi->current_y;
pi->current_x = x;
pi->current_y = y;
}
xcb_configure_window(con, win, XCB_MOVE_RESIZE, pos);
}
/* wrapper to move window */
static inline void xcb_move(xcb_connection_t *con, xcb_window_t win, int x, int y, posxy_t *pi)
{
unsigned int pos[2] = { x, y };
if (pi) {
pi->previous_x = pi->current_x;
pi->previous_y = pi->current_y;
pi->current_x = x;
pi->current_y = y;
}
xcb_configure_window(con, win, XCB_MOVE, pos);
}
/* wrapper to resize window */
static inline void xcb_resize(xcb_connection_t *con, xcb_window_t win, int w,
int h)
{
unsigned int pos[2] = { w, h };
xcb_configure_window(con, win, XCB_RESIZE, pos);
}
/* wrapper to raise window */
static inline void xcb_raise_window(xcb_connection_t *con, xcb_window_t win)
{
unsigned int arg[1] = { XCB_STACK_MODE_ABOVE };
xcb_configure_window(con, win, XCB_CONFIG_WINDOW_STACK_MODE, arg);
}
/* wrapper to lower window */
static inline void xcb_lower_window(xcb_connection_t *con, xcb_window_t win)
{
unsigned int arg[1] = { XCB_STACK_MODE_BELOW };
xcb_configure_window(con, win, XCB_CONFIG_WINDOW_STACK_MODE, arg);
}
/* wrapper to set xcb border width */
static inline void xcb_border_width(xcb_connection_t *con, xcb_window_t win,
int w)
{
unsigned int arg[1] = { w };
xcb_configure_window(con, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, arg);
}
/* wrapper to get xcb keysymbol from keycode */
static xcb_keysym_t xcb_get_keysym(xcb_keycode_t keycode)
{
xcb_keysym_t keysym;
keysym = xcb_key_symbols_get_keysym(keysyms, keycode, 0);
return keysym;
}
/* wrapper to get xcb keycodes from keysymbol (caller must free) */
static xcb_keycode_t *xcb_get_keycodes(xcb_keysym_t keysym)
{
xcb_keycode_t *keycode;
keycode = xcb_key_symbols_get_keycode(keysyms, keysym);
return keycode;
}
/* retieve RGB color from hex (think of html) */
static unsigned int xcb_get_colorpixel(char *hex)
{
char strgroups[3][3] = {{hex[1], hex[2], '\0'},
{hex[3], hex[4], '\0'},
{hex[5], hex[6], '\0'}};
unsigned int rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
(strtol(strgroups[1], NULL, 16)),
(strtol(strgroups[2], NULL, 16))};
return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
}
/* wrapper to get atoms using xcb */
static void xcb_get_atoms(char **names, xcb_atom_t *atoms, unsigned int count)
{
xcb_intern_atom_cookie_t cookies[count];
xcb_intern_atom_reply_t *reply;
for (unsigned int i = 0; i < count; i++)
cookies[i] = xcb_intern_atom(dis, 0, strlen(names[i]), names[i]);
for (unsigned int i = 0; i < count; i++) {
reply = xcb_intern_atom_reply(dis, cookies[i], NULL);
if (!reply)
errx(EXIT_FAILURE, "failed to register %s atom", names[i]);
DEBUGP("%s : %d\n", names[i], reply->atom);
atoms[i] = reply->atom; free(reply);
}
}
/* wrapper to window get attributes using xcb */
static void xcb_get_attributes(xcb_window_t *windows,
xcb_get_window_attributes_reply_t **reply,
unsigned int count)
{
xcb_get_window_attributes_cookie_t cookies[count];
for (unsigned int i = 0; i < count; i++)
cookies[i] = xcb_get_window_attributes(dis, windows[i]);
for (unsigned int i = 0; i < count; i++)
reply[i] = xcb_get_window_attributes_reply(dis, cookies[i], NULL);
/* TODO: Handle error */
}
/* wrapper to get window geometry */
static inline xcb_get_geometry_reply_t *get_geometry(xcb_window_t win)
{
xcb_get_geometry_reply_t *r;
r = xcb_get_geometry_reply(dis, xcb_get_geometry(dis, win), NULL);
if (!r)
errx(EXIT_FAILURE, "failed to get geometry for window %i", win);
return r;
}
/* check if other wm exists */
static int xcb_checkotherwm(void)
{
xcb_generic_error_t *error;
unsigned int values[1] = {XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT|
XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY|
XCB_EVENT_MASK_PROPERTY_CHANGE|
XCB_EVENT_MASK_BUTTON_PRESS};
error = xcb_request_check(dis, xcb_change_window_attributes_checked(dis,
screen->root, XCB_CW_EVENT_MASK, values));
if (error)
return 1;
return 0;
}
/*
static bool window_is_override_redirect(xcb_window_t win)
{
xcb_get_window_attributes_reply_t *attr[1];
xcb_window_t windows[] = {win};
bool override = True;
xcb_get_attributes(windows, attr, 1);
if (attr[0]) {
if (!attr[0]->override_redirect)
override = False;
free(attr[0]);
}
return override;
}
*/
/* find client in current_display by window id */
static client *find_client(xcb_window_t w)
{
client *c;
for (c = (client *)get_head(¤t_display->clients);
c && c->win != w; c = (client *)get_next(&c->link)) ;
return c;
}
/* find desktop by number */
static desktop *find_desktop(unsigned int n)
{
desktop *d;
for (d = (desktop *)get_head(&desktops);
d && d->num != n; d = (desktop *)get_next(&d->link)) ;
return d;
}
/* find monitor in current_desktop by number */
/*
static monitor *find_monitor(unsigned int n)
{
monitor *m;
for (m = (monitor *)get_head(¤t_desktop->monitors);
m && m->num != n; m = (monitor *)get_next(&m->link)) ;
return m;
}
*/
static void getparents(client *c, display **di, monitor **mo, desktop **de)
{
display *disp;
monitor *moni;
desktop *desk;
list *l;
/* backpointer mambo-jambo */
if (!c) return;
l = c->link.parent;
disp = l->master;
l = disp->link.parent;
moni = l->master;
l = moni->link.parent;
desk = l->master;
if (di)
*di = disp;
if (mo)
*mo = moni;
if (de)
*de = desk;
}
/* create a new client and add the new window
* window should notify of property change events
*/
client *addwindow(xcb_window_t win, xcb_atom_t wtype)
{
client *c = create_client(win, wtype);
/* c is valid, else we would not get here */
if (!check_head(¤t_display->clients)) {
add_head(¤t_display->clients, &c->link);
}
else {
if (!ATTACH_ASIDE)
add_head(¤t_display->clients, &c->link);
else
add_tail(¤t_display->clients, &c->link);
}
DEBUG("client added");
setwindefattr(win);
return c;
}
/* change the size of the window borders */
void adjust_borders(const Arg *arg)
{
if (arg->i > 0 || borders >= -arg->i)
borders += arg->i;
tile();
update_current(M_CURRENT);
}
/* change the size of the useless gaps on the fly and re-tile */
void adjust_gaps(const Arg *arg)
{
int gaps = M_GAPS;
if (arg->i > 0 || gaps >= -arg->i)
gaps += arg->i;
else
return;
if (GLOBALGAPS) {
desktop *desk;
for (desk = (desktop *)get_head(&desktops); desk; desk = (desktop *)get_next(&desk->link)) {
monitor *moni;
for (moni = (monitor *)get_head(&desk->monitors); moni; moni = (monitor *)get_next(&moni->link)) {
display *disp;
for (disp = (display *)get_head(&moni->displays); disp; disp = (display *)get_next(&disp->link)) {
disp->di.gaps = gaps;
}
}
}
}
else
M_GAPS = gaps;
tile();
}
/* on the press of a button check to see if there's a binded function to call */
void buttonpress(xcb_generic_event_t *e)
{
xcb_button_press_event_t *ev = (xcb_button_press_event_t *)e;
DEBUGP("xcb: button press: %d state: %d\n", ev->detail, ev->state);
client *c = wintoclient(ev->event);
if (!c) {
if(USE_SCRATCHPAD && showscratchpad && scrpd && ev->event == scrpd->win)
c = scrpd;
else
return;
}
if (CLICK_TO_FOCUS && M_CURRENT != c && ev->detail == XCB_BUTTON_INDEX_1)
update_current(c);
if (c != scrpd) {
for (unsigned int i = 0; i < LENGTH(buttons); i++)
if (buttons[i].func && buttons[i].button == ev->detail &&
CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) {
if (M_CURRENT != c)
update_current(c);
buttons[i].func(&(buttons[i].arg));
}
}
if (CLICK_TO_FOCUS) {
xcb_allow_events(dis, XCB_ALLOW_REPLAY_POINTER, ev->time);
xcb_flush(dis);
}
}
/* focus another desktop
*
* to avoid flickering
* first map the new windows
* first the current window and then all other
* then unmap the old windows
* first all others then the current */
void change_desktop(const Arg *arg)
{
if (arg->i == current_desktop_number || arg->i > DESKTOPS-1)
return;
previous_desktop = current_desktop_number;
select_desktop(arg->i);
if (show) {
if (M_CURRENT && M_CURRENT != scrpd)
xcb_move(dis, M_CURRENT->win, M_CURRENT->position_info.previous_x, M_CURRENT->position_info.previous_y, &M_CURRENT->position_info);
for (client *c = M_HEAD; c; c = M_GETNEXT(c)) {
if (c != M_CURRENT)
xcb_move(dis, c->win, c->position_info.previous_x, c->position_info.previous_y, &c->position_info);
}
}
select_desktop(previous_desktop);
for (client *c = M_HEAD; c; c = M_GETNEXT(c)) {
if (c != M_CURRENT)
xcb_move(dis, c->win, -2 * M_WW, 0, &c->position_info);
}
if (M_CURRENT && M_CURRENT != scrpd)
xcb_move(dis, M_CURRENT->win, -2 * M_WW, 0, &M_CURRENT->position_info);
select_desktop(arg->i);
update_current(M_CURRENT);
desktopinfo();
xcb_ewmh_set_current_desktop(ewmh, default_screen, arg->i);
}
static void print_window_type(xcb_window_t w, xcb_atom_t a)
{
char *s;
if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP) s = "_NET_WM_WINDOW_TYPE_DESKTOP";
else if (a == ewmh->_NET_WM_WINDOW_TYPE_DOCK) s = "_NET_WM_WINDOW_TYPE_DOCK";
else if (a == ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR) s = "_NET_WM_WINDOW_TYPE_TOOLBAR";
else if (a == ewmh->_NET_WM_WINDOW_TYPE_MENU) s = "_NET_WM_WINDOW_TYPE_MENU";
else if (a == ewmh->_NET_WM_WINDOW_TYPE_UTILITY) s = "_NET_WM_WINDOW_TYPE_UTILITY";
else if (a == ewmh->_NET_WM_WINDOW_TYPE_SPLASH) s = "_NET_WM_WINDOW_TYPE_SPLASH";
else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) s = "_NET_WM_WINDOW_TYPE_DIALOG";
else if (a == ewmh->_NET_WM_WINDOW_TYPE_DROPDOWN_MENU) s = "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU";
else if (a == ewmh->_NET_WM_WINDOW_TYPE_POPUP_MENU) s = "_NET_WM_WINDOW_TYPE_POPUP_MENU";
else if (a == ewmh->_NET_WM_WINDOW_TYPE_TOOLTIP) s = "_NET_WM_WINDOW_TYPE_TOOLTIP";
else if (a == ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION) s = "_NET_WM_WINDOW_TYPE_NOTIFICATION";
else if (a == ewmh->_NET_WM_WINDOW_TYPE_COMBO) s = "_NET_WM_WINDOW_TYPE_COMBO";
else if (a == ewmh->_NET_WM_WINDOW_TYPE_DND) s = "_NET_WM_WINDOW_TYPE_DND";
else if (a == ewmh->_NET_WM_WINDOW_TYPE_NORMAL) s = "_NET_WM_WINDOW_TYPE_NORMAL";
else s = "undefined window type";
if (w && s) {
DEBUGP("window %x has type %s\n", w, s);
}
}
/*
* returns:
* True if window is alien
* False if window will be client
*/
static bool check_if_window_is_alien(xcb_window_t win, bool *isFloating, xcb_atom_t *wtype)
{
xcb_get_window_attributes_reply_t *attr[1];
xcb_window_t windows[] = {win};
bool isAlien = False;
if (isFloating) *isFloating = False;
if (wtype) *wtype = ewmh->_NET_WM_WINDOW_TYPE_NORMAL;
xcb_get_attributes(windows, attr, 1);
if (!attr[0]) /* dead on arrival */
return True;
if (attr[0]->override_redirect) {
free(attr[0]);
return True;
}
else
free(attr[0]);
/*
* check if window type is not _NET_WM_WINDOW_TYPE_NORMAL.
* if yes, then we add it to alien list and map it.
*/
xcb_ewmh_get_atoms_reply_t type;
xcb_atom_t atype = 0;
if (xcb_ewmh_get_wm_window_type_reply(ewmh,
xcb_ewmh_get_wm_window_type(ewmh,
win), &type, NULL) == 1) {
if (wtype) *wtype = type.atoms[0];
for (unsigned int i = 0; i < type.atoms_len; i++) {
print_window_type(win, type.atoms[i]);
if (type.atoms[i] == ewmh->_NET_WM_WINDOW_TYPE_NORMAL) {
isAlien = False;
break;
}
if (type.atoms[i] == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
if (wtype) *wtype = type.atoms[i];
if (isFloating) *isFloating = True;
isAlien = False;
break;
}