-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmwm.c
4412 lines (3674 loc) · 110 KB
/
wmwm.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
/* wmwm, mcwm fork */
/*
* mcwm, a small window manager for the X Window System using the X
* protocol C Binding libraries.
*
* For 'user' configurable stuff, see config.h.
*
* MC, mc at the domain hack.org
* http://hack.org/mc/
*
* Copyright (c) 2010, 2011, 2012 Michael Cardell Widerkrantz, mc at
* the domain hack.org.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "wmwm.h"
#include <assert.h>
#include <errno.h> // for EINTR, errno
#include <getopt.h> // for optarg, getopt
#include <poll.h> // for pollfd, poll, POLLIN
#include <signal.h> // for signal, SIG_DFL, SIG_ERR, SIGCHLD, SIGINT
#include <stdbool.h> // for false, bool, true
#include <stdint.h> // for uint32_t, uint16_t, uint8_t, int16_t
#include <stdio.h> // for NULL, fprintf, stderr, perror, printf
#include <stdlib.h> // for free, exit, calloc, atoi, strtoul
#include <string.h> // for strlen, memset, strcpy, strncpy
#include <unistd.h> // for execvp, fork, setsid, pid_t
#include <X11/keysymdef.h> // for XK_VoidSymbol
#include <xcb/xcb.h> // for xcb_generic_event_t, xcb_generic_error_t
#include <xcb/randr.h> // for xcb_randr_get_crtc_info_reply_t, xcb_ra...
#include <xcb/shape.h> // for xcb_shape_notify_event_t, xcb_shape_com...
#include <xcb/xcb_event.h> // for xcb_event_get_error_label, xcb_event_ge...
#include <xcb/xcb_ewmh.h> // for xcb_ewmh_connection_t, xcb_ewmh_set_act...
#include <xcb/xcb_icccm.h> // for xcb_size_hints_t, ::XCB_ICCCM_SIZE_HINT...
#include <xcb/xcb_keysyms.h> // for xcb_key_symbols_free, xcb_key_symbols_t
#include <xcb/xinput.h> // for xcb_input_device_motion_notify_event_t
#include <xcb/xproto.h> // for xcb_rectangle_t, xcb_screen_t, xcb_atom_t
/* list functions */
#include "list.h" // for list_t, list_add, list_to_head, list_erase...
/* container functions */
#include "window_tree.h"
/* Check here for user configurable parts: */
#include "config.h"
#define PERROR(Args...) \
do { fprintf(stderr, "ERROR wmwm: "); fprintf(stderr, ##Args); } while(0)
#ifdef DEBUGMSG
#define PDEBUG(Args...) \
do { fprintf(stderr, "wmwm: "); fprintf(stderr, ##Args); } while(0)
#define D(x) x
#else
#define PDEBUG(Args...)
#define D(x)
#endif
#define destroy(x) do { free(x); x = NULL; } while (0)
/* Internal Constants. */
typedef enum {
mode_nothing, /* We're currently doing nothing special */
mode_move, /* We're currently moving a window with the mouse. */
mode_resize, /* We're currently resizing a window with the mouse. */
mode_tab /* We're currently tabbing around the window list,
looking for a new window to focus on. */
} wm_mode_t;
typedef enum {
step_up = 1 << 0,
step_down = 1 << 1,
step_left = 1 << 2,
step_right = 1 << 3
} step_direction_t;
/*
typedef enum {
sh_us_position = XCB_ICCCM_SIZE_HINT_US_POSITION,
sh_us_size = XCB_ICCCM_SIZE_HINT_US_SIZE,
sh_position = XCB_ICCCM_SIZE_HINT_P_POSITION,
sh_size = XCB_ICCCM_SIZE_HINT_P_SIZE,
sh_min_size = XCB_ICCCM_SIZE_HINT_P_MIN_SIZE,
sh_max_size = XCB_ICCCM_SIZE_HINT_P_MAX_SIZE,
sh_resize_inc = XCB_ICCCM_SIZE_HINT_P_RESIZE_INC,
sh_aspect = XCB_ICCCM_SIZE_HINT_P_ASPECT,
sh_base_size = XCB_ICCCM_SIZE_HINT_BASE_SIZE,
sh_win_gravity = XCB_ICCCM_SIZE_HINT_P_WIN_GRAVITY
} size_hint_t;
*/
/* This means we didn't get any window hint at all. */
#define WORKSPACE_NONE 0xfffffffe
/* Default Client Events
*
* Only listen for property_notify on the client,
* the other events come in via substructure-requests/notifications via
* frame window
* */
#define DEFAULT_WINDOW_EVENTS XCB_EVENT_MASK_PROPERTY_CHANGE
/* Frame events
*
* Enter events
* Substructure notify (unmap/destroy notify etc.)
* Substructure Redirect (configure requests etc.)
*/
#define DEFAULT_FRAME_EVENTS (XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_FOCUS_CHANGE)
/* Frame events for hidden frames
*
* only listen for destroy notifcations and the like
* (have to substructure redirect because it's mine mwhahaaa)
*/
#define HIDDEN_FRAME_EVENTS (XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY)
/* What we listen to on the root window */
#define DEFAULT_ROOT_WINDOW_EVENTS (XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_ENTER_WINDOW)
/* Globals */
int sigcode; /* Signal code. Non-zero if we've been
* interrupted by a signal. */
xcb_connection_t *conn; /* Connection to X server. */
xcb_screen_t *screen; /* Our current screen. */
int screen_number;
xcb_timestamp_t current_time; /* latest timestamp XXX */
xcb_ewmh_connection_t *ewmh; /* EWMH Connection */
xcb_atom_t ewmh__NET_WM_STATE_FOCUSED; /* atom not in extension */
int randrbase; /* Beginning of RANDR extension events. */
int shapebase; /* Beginning of SHAPE extension events. */
uint32_t curws = 0; /* Current workspace. */
int16_t mode_x = 0;
int16_t mode_y = 0;
list_t *monlist = NULL; /* List of all physical monitor outputs. */
wm_mode_t MCWM_mode = mode_nothing; /* Internal mode, such as move or resize */
tiling_t tiling_mode = DEFAULT_TILING_MODE; /* global tiling mode */
bool floating_mode = false; /* global floating mode, overrides tiling_mode */
/*
* Workspace list: Every workspace has a list of all visible
* windows.
*/
wtree_t *wslist[WORKSPACES];
/* Shortcut key type and initialization. */
struct keys {
xcb_keysym_t keysym;
xcb_keycode_t keycode;
} keys[KEY_MAX] = {
{ USERKEY_MOVE_LEFT, 0},
{ USERKEY_MOVE_DOWN, 0},
{ USERKEY_MOVE_UP, 0},
{ USERKEY_MOVE_RIGHT, 0},
{ USERKEY_TILING, 0},
{ USERKEY_FLOATING, 0},
{ USERKEY_RAISE, 0},
{ USERKEY_SWAP, 0},
{ USERKEY_TERMINAL, 0},
{ USERKEY_MENU, 0},
{ USERKEY_MAX, 0},
{ USERKEY_CHANGE, 0},
{ USERKEY_WS1, 0},
{ USERKEY_WS2, 0},
{ USERKEY_WS3, 0},
{ USERKEY_WS4, 0},
{ USERKEY_WS5, 0},
{ USERKEY_WS6, 0},
{ USERKEY_WS7, 0},
{ USERKEY_WS8, 0},
{ USERKEY_WS9, 0},
{ USERKEY_WS10, 0},
{ USERKEY_TOPLEFT, 0},
{ USERKEY_TOPRIGHT, 0},
{ USERKEY_BOTLEFT, 0},
{ USERKEY_BOTRIGHT, 0},
{ USERKEY_DELETE, 0},
{ USERKEY_PREVSCREEN, 0},
{ USERKEY_NEXTSCREEN, 0},
{ USERKEY_ICONIFY, 0},
};
/* All keycodes generating our MODKEY mask. */
struct modkeycodes {
xcb_keycode_t *keycodes;
unsigned len;
} modkeys = { NULL, 0};
/* Global configuration. */
struct conf {
int borderwidth; /* Do we draw borders? If so, how large? */
int gapwidth; /* Do we have gaps? If so, how large? */
char *terminal; /* Path to terminal to start. */
char *menu; /* Path to menu to start. */
uint32_t focuscol; /* Focused border color. */
uint32_t unfocuscol; /* Unfocused border color. */
bool allowicons; /* Allow windows to be unmapped. */
} conf;
/* elemental atoms not in ewmh */
// JUST USE XCB_WM_NAME_ etc pp?
struct icccm {
xcb_atom_t wm_delete_window; /* WM_DELETE_WINDOW event to close windows. */
xcb_atom_t wm_change_state;
xcb_atom_t wm_state;
xcb_atom_t wm_protocols; /* WM_PROTOCOLS. */
xcb_atom_t wm_take_focus;
} icccm;
static xcb_atom_t ewmh_allowed_actions[2] = { XCB_ATOM_NONE, XCB_ATOM_NONE };
/* Functions declarations. */
/* print out X error to stderr */
static void print_x_error(xcb_generic_error_t *e);
/* event handlers */
static void handle_error_event(xcb_generic_event_t*);
static void handle_map_request(xcb_generic_event_t*);
static void handle_button_press(xcb_generic_event_t*);
static void handle_motion_notify(xcb_generic_event_t*);
static void handle_button_release(xcb_generic_event_t*);
static void handle_key_press(xcb_generic_event_t*);
static void handle_key_release(xcb_generic_event_t*);
static void handle_enter_notify(xcb_generic_event_t*);
static void handle_focus_in(xcb_generic_event_t*);
static void handle_configure_notify(xcb_generic_event_t*);
static void handle_configure_request(xcb_generic_event_t*);
static void handle_client_message(xcb_generic_event_t*);
static void handle_circulate_request(xcb_generic_event_t*);
static void handle_mapping_notify(xcb_generic_event_t*);
static void handle_unmap_notify(xcb_generic_event_t*);
static void handle_destroy_notify(xcb_generic_event_t*);
static void handle_property_notify(xcb_generic_event_t*);
static void handle_colormap_notify(xcb_generic_event_t*);
// RESPONSE_TYPE_MASK is uint_8t (and is only 0x1f, so little waste)
static void (*handler[XCB_EVENT_RESPONSE_TYPE_MASK]) (xcb_generic_event_t*) = {
[0] = handle_error_event,
[XCB_MAP_REQUEST] = handle_map_request,
[XCB_BUTTON_PRESS] = handle_button_press,
[XCB_MOTION_NOTIFY] = handle_motion_notify,
[XCB_BUTTON_RELEASE] = handle_button_release,
[XCB_KEY_PRESS] = handle_key_press,
[XCB_KEY_RELEASE] = handle_key_release,
[XCB_ENTER_NOTIFY] = handle_enter_notify,
[XCB_FOCUS_IN] = handle_focus_in,
[XCB_CONFIGURE_NOTIFY] = handle_configure_notify,
[XCB_CONFIGURE_REQUEST] = handle_configure_request,
[XCB_CLIENT_MESSAGE] = handle_client_message,
[XCB_CIRCULATE_REQUEST] = handle_circulate_request,
[XCB_MAPPING_NOTIFY] = handle_mapping_notify,
[XCB_UNMAP_NOTIFY] = handle_unmap_notify,
[XCB_DESTROY_NOTIFY] = handle_destroy_notify,
[XCB_PROPERTY_NOTIFY] = handle_property_notify,
[XCB_COLORMAP_NOTIFY] = handle_colormap_notify
};
static uint32_t getcolor(const char *colstr);
static xcb_atom_t get_atom(char *atom_name);
#if DEBUGMSG
static char* get_atomname(xcb_atom_t atom);
#endif
static bool ewmh_is_fullscreen(client_t*);
static uint32_t ewmh_get_workspace(xcb_drawable_t win);
static void ewmh_update_client_list();
static void ewmh_frame_extents(xcb_window_t win, int width);
static void ewmh_update_state(client_t* client);
static void resize_step(client_t *client, step_direction_t direction);
static void mouse_move(client_t *client, int rel_x, int rel_y);
static void mouse_resize(client_t *client, int rel_x, int rel_y);
static void move_step(client_t *client, step_direction_t direction);
static void set_to_workspace(client_t *client, uint32_t ws);
static void move_to_workspace(client_t *client, uint32_t ws);
static void remove_from_workspace(client_t *client);
static void change_workspace(uint32_t ws);
static void update_shape(client_t *client);
static void adjust_stacking(client_t *client);
static void raise_client(client_t *client);
static void lower_client(client_t *client);
static void raise_or_lower_client(client_t *client);
static void set_focus(client_t *client);
static void unset_focus();
static void focus_next();
static void focus_under_cursor();
static void toggle_fullscreen(client_t *client);
static void toggle_vertical(client_t *client);
static void unmax(client_t *client);
static void attach_frame(client_t *client);
static void delete_win(client_t*);
static void hide(client_t *client);
static void erase_client(client_t *client);
static void show(client_t *client);
static void send_client_message(xcb_window_t window, xcb_atom_t atom);
static void send_configuration(client_t *client);
static void set_borders(xcb_drawable_t win, int width);
static void update_bordercolor(client_t* client);
static void arrbymon(monitor_t *monitor);
static void arrangewindows();
static int start(char *program);
static void new_win(xcb_window_t win);
static client_t *create_client(xcb_window_t win);
static key_enum_t key_from_keycode(xcb_keycode_t keycode);
static struct modkeycodes get_modkeys(xcb_mod_mask_t modmask);
static xcb_keycode_t keysym_to_keycode(xcb_keysym_t keysym,
xcb_key_symbols_t * keysyms);
static bool setup_keys();
static bool setup_screen();
static bool setup_ewmh();
static int setup_randr();
static void get_randr();
static void get_outputs(xcb_randr_output_t * outputs, int len,
xcb_timestamp_t timestamp);
static monitor_t *find_monitor(xcb_randr_output_t id);
static monitor_t *find_clones(xcb_randr_output_t id, int16_t x, int16_t y);
static monitor_t *find_monitor_at(int16_t x, int16_t y);
static void del_monitor(monitor_t *mon);
static monitor_t *add_monitor(xcb_randr_output_t id, char *name,
uint32_t x, uint32_t y, uint16_t width,
uint16_t height);
static void apply_gravity(client_t *client, xcb_rectangle_t* geometry);
static int update_geometry(client_t *client, const xcb_rectangle_t *geometry);
static client_t *find_client(xcb_drawable_t win);
static client_t *find_clientp(xcb_drawable_t win);
static bool get_pointer(xcb_drawable_t win, int16_t * x, int16_t * y);
static bool get_geometry(xcb_drawable_t win, xcb_rectangle_t *geometry);
static void set_hidden_events(client_t *client);
static void set_default_events(client_t *client);
static void warp_focuswin(step_direction_t direction);
static void prev_screen();
static void next_screen();
static void configure_win(xcb_window_t win, uint16_t old_mask, winconf_t wc);
static void events();
static void print_help();
static void signal_catch(int sig);
static void get_monitor_geometry(monitor_t* monitor, xcb_rectangle_t* sp);
static void cleanup(int code);
/**********************************************************************/
/* new functions for tiling-branch */
static client_t *focuswin(uint32_t ws);
static void set_focuswin(uint32_t ws, client_t* client);
/* setup workspace array */
static void setup_workspaces();
/* update clues in nodes and update screen */
/* Function bodies. */
/* new functions for tiling-branch */
static void toggle_tiling(client_t *client);
static void toggle_floating(client_t *client);
/* update window sizes below wtree_t */
static void update_clues(wtree_t *node, xcb_rectangle_t rect);
// XXX tiling tmp hack
static xcb_rectangle_t screen_rect()
{
xcb_rectangle_t geo;
geo.x = 0;
geo.y = 0;
geo.width = screen->width_in_pixels;
geo.height = screen->height_in_pixels;
return geo;
}
void toggle_floating(client_t *client)
{
if (client == NULL)
return;
// XXX save original geometry?
// restore geometry if floating-window got unfullscreened
if (wtree_toggle_floating(client->wsitem) && ! client->fullscreen)
update_geometry(client, &client->geometry_last);
update_clues(wslist[client->ws], screen_rect());
wtree_print_tree(wslist[client->ws]);
adjust_stacking(client);
}
// toggle tiling mode of parent container
void toggle_tiling(client_t *client)
{
assert(client != NULL);
switch (wtree_parent_tiling(client->wsitem)) {
case TILING_VERTICAL:
wtree_set_parent_tiling(client->wsitem, TILING_HORIZONTAL);
break;
case TILING_HORIZONTAL:
wtree_set_parent_tiling(client->wsitem, TILING_VERTICAL);
break;
}
// XXX tiling, store geometry in tiling nodes
update_clues(wslist[client->ws], screen_rect());
wtree_print_tree(wslist[client->ws]);
adjust_stacking(client);
}
void setup_workspaces()
{
for (uint32_t i = 0; i < WORKSPACES; i++)
wslist[i] = wtree_new_workspace(screen_rect());
}
/* XXX Don't like that */
client_t *focuswin(uint32_t ws)
{
assert(ws < WORKSPACES);
return wtree_focuswin(wslist[ws]);
}
void show_node(char* str, wtree_t *node)
{
if (node == NULL)
fprintf(stderr, "(%s) node: %p\n", str, NULL);
else {
fprintf(stderr, "(%s) node: %p\n - parent: %p\n - child: %p\n - prev: %p\n - next: %p\n",
str, (void*)node, (void*)node->parent, (void*)node->child,
(void*)node->prev, (void*)node->next);
}
}
void set_focuswin(uint32_t ws, client_t* client)
{
assert(ws < WORKSPACES);
wtree_set_focuswin(wslist[ws], client);
}
/**********************************************************************/
// XXX this is just a little precaution and encapsulation
static void set_mode(wm_mode_t modus) { MCWM_mode = modus; }
static wm_mode_t get_mode() { return MCWM_mode; }
static bool is_mode(wm_mode_t modus) { return (get_mode() == modus); }
static xcb_timestamp_t get_timestamp() { return current_time; }
static void set_timestamp(xcb_timestamp_t t) { current_time = t; }
static void update_timestamp(xcb_timestamp_t t) { if (t != XCB_TIME_CURRENT_TIME) current_time = t; }
/* check if pointer is over client */
/* XXX check for workspace and monitor */
bool pointer_over_client(client_t* client)
{
int16_t x,y;
const xcb_rectangle_t *geo = &client->geometry;
if (! get_pointer(screen->root, &x, &y))
return false;
return (x >= geo->x &&
y >= geo->y &&
x <= geo->width + geo->x &&
y <= geo->height + geo->y);
}
/*
* Update client's window's atoms
*/
void ewmh_update_state(client_t* client)
{
xcb_atom_t atoms[5];
uint32_t i = 0;
if (! client)
return;
if (client->fullscreen)
atoms[i++] = ewmh->_NET_WM_STATE_FULLSCREEN;
if (client->vertmaxed)
atoms[i++] = ewmh->_NET_WM_STATE_MAXIMIZED_VERT;
if (client->hidden)
atoms[i++] = ewmh->_NET_WM_STATE_HIDDEN;
if (client == focuswin(curws))
atoms[i++] = ewmh__NET_WM_STATE_FOCUSED;
if (i > 0) {
xcb_ewmh_set_wm_state(ewmh, client->id, i, atoms);
client->ewmh_state_set = true;
} else if (client->ewmh_state_set) {
/* remove atom if there's no state and an old atom */
xcb_delete_property(conn, client->id, ewmh->_NET_WM_STATE);
client->ewmh_state_set = false;
}
}
/* XXX: I don't know what that does at all */
/*
* Find out what keycode modmask is bound to. Returns a struct. If the
* len in the struct is 0 something went wrong.
*/
struct modkeycodes get_modkeys(xcb_mod_mask_t modmask)
{
xcb_get_modifier_mapping_cookie_t cookie;
xcb_get_modifier_mapping_reply_t *reply;
xcb_keycode_t *modmap;
struct modkeycodes keycodes = {
NULL,
0
};
const xcb_mod_mask_t masks[8] = { XCB_MOD_MASK_SHIFT,
XCB_MOD_MASK_LOCK,
XCB_MOD_MASK_CONTROL,
XCB_MOD_MASK_1,
XCB_MOD_MASK_2,
XCB_MOD_MASK_3,
XCB_MOD_MASK_4,
XCB_MOD_MASK_5
};
cookie = xcb_get_modifier_mapping_unchecked(conn);
reply = xcb_get_modifier_mapping_reply(conn, cookie, NULL);
if (! reply) {
return keycodes;
}
keycodes.keycodes = calloc(reply->keycodes_per_modifier,
sizeof(xcb_keycode_t));
if (! keycodes.keycodes) {
PERROR("Out of memory.\n");
destroy(reply);
return keycodes;
}
modmap = xcb_get_modifier_mapping_keycodes(reply);
/*
* The modmap now contains keycodes.
*
* The number of keycodes in the list is 8 *
* keycodes_per_modifier. The keycodes are divided into eight
* sets, with each set containing keycodes_per_modifier elements.
*
* Each set corresponds to a modifier in masks[] in the order
* specified above.
*
* The keycodes_per_modifier value is chosen arbitrarily by the
* server. Zeroes are used to fill in unused elements within each
* set.
*/
for (unsigned mask = 0; mask < (sizeof(masks) / sizeof(int)); mask++) {
if (masks[mask] == modmask) {
for (uint8_t i = 0; i < reply->keycodes_per_modifier; i++) {
if (modmap[mask * reply->keycodes_per_modifier + i]) {
keycodes.keycodes[i]
= modmap[mask * reply->keycodes_per_modifier + i];
keycodes.len++;
}
}
PDEBUG("Got %u keycodes.\n", keycodes.len);
}
} /* for mask */
destroy(reply);
return keycodes;
}
/*
* Set keyboard focus to follow mouse pointer. Then exit.
*
* We don't need to bother mapping all windows we know about. They
* should all be in the X server's Save Set and should be mapped
* automagically.
*/
void cleanup(int code)
{
xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
XCB_INPUT_FOCUS_POINTER_ROOT, get_timestamp());
if (ewmh) {
xcb_ewmh_connection_wipe(ewmh);
}
xcb_disconnect(conn);
exit(code);
}
/*
* Rearrange windows to fit new screen size.
*/
void arrangewindows()
{
/*
* Go through all windows. If they don't fit on the new screen,
* move them around and resize them as necessary.
*/
/* TODO tiling */
}
/*
* set _NET_CLIENT_LIST
*
* in order of first mapping
* (btw. we don't have stacking information of all windows
* so we don't set _NET_CLIENT_LIST_STACKING
*/
void ewmh_update_client_list()
{
/* TODO tiling */
#if 0
list_t *item;
xcb_window_t *window_list;
uint32_t windows = 0;
/* leave if no windows */
if (! winlist) {
xcb_ewmh_set_client_list(ewmh, screen_number, 0, NULL);
return;
}
/* count windows */
for (item = winlist; item; item = item->next, ++windows);
/* create window array */
window_list = calloc(windows, sizeof(xcb_window_t));
if (! window_list) {
xcb_ewmh_set_client_list(ewmh, screen_number, 0, NULL);
return;
}
/* fill window array in reverse order */
uint32_t id = windows;
for (item = winlist; item; item = item->next) {
const client_t* client = item->data;
window_list[--id] = client->id;
}
xcb_ewmh_set_client_list(ewmh, screen_number, windows, window_list);
destroy(window_list);
#endif
}
/*
* Check if window has _NET_WM_STATE_FULLSCREEN atom
*/
bool ewmh_is_fullscreen(client_t* client)
{
xcb_ewmh_get_atoms_reply_t atoms;
if (0 == xcb_ewmh_get_wm_state_reply(ewmh,
xcb_ewmh_get_wm_state_unchecked(ewmh, client->id),
&atoms, NULL)) {
return false;
}
bool ret = false;
for (uint32_t i = 0; i < atoms.atoms_len; i++) {
if (atoms.atoms[i] == ewmh->_NET_WM_STATE_FULLSCREEN) {
ret = true;
break;
}
}
xcb_ewmh_get_atoms_reply_wipe(&atoms);
return ret;
}
/*
* Get EWWM hint so we might know what workspace window win should be
* visible on.
*
* Returns either workspace, WORKSPACE_NONE if we didn't find any hints.
*/
uint32_t ewmh_get_workspace(xcb_drawable_t win)
{
xcb_get_property_cookie_t cookie;
uint32_t ws;
cookie = xcb_ewmh_get_wm_desktop_unchecked(ewmh, win);
if (! xcb_ewmh_get_wm_desktop_reply(ewmh, cookie, &ws, NULL)) {
return WORKSPACE_NONE;
}
return ws;
}
// XXX update_clues does not know about fullscreen, so tries to change windows which shouldn't
// anyhow, that situation needs to change
void update_clues(wtree_t *node, xcb_rectangle_t rect)
{
if (node == NULL)
return;
// root node
if (wtree_is_workspace_type(node)) {
update_clues(node->child, rect);
return;
}
if (wtree_is_tiling_type(node) && wtree_tiles(node) > 0) {
// tiling node with actual tiles
xcb_rectangle_t tmp = rect;
int tiles = wtree_tiles(node);
// fix width of the tiling container if there's more than one child
if (tiles > 1) {
if (wtree_tiling(node) == TILING_VERTICAL)
tmp.width /= tiles;
if (wtree_tiling(node) == TILING_HORIZONTAL)
tmp.height /= tiles;
}
// jump to the clients
update_clues(node->child, tmp);
} else if (wtree_is_client_type(node) && ! wtree_is_floating(node)) {
int gaps = conf.borderwidth + conf.gapwidth;
xcb_rectangle_t tmp = rect;
tmp.x += gaps;
tmp.y += gaps;
assert(tmp.width > gaps * 2); assert(tmp.height > gaps * 2); // XXX
tmp.width -= gaps * 2;
tmp.height -= gaps * 2;
update_geometry(wtree_client(node), &tmp);
} else {
update_clues(node->next, rect);
return;
}
// only either non-floating nodes or tiling-nodes with non-floating nodes get here
if (wtree_is_tiling_type(node->parent)) {
if (wtree_parent_tiling(node) == TILING_HORIZONTAL)
rect.y += rect.height;
else if (wtree_parent_tiling(node) == TILING_VERTICAL)
rect.x += rect.width;
}
update_clues(node->next, rect);
}
void move_to_workspace(client_t *client, uint32_t ws)
{
if (curws == ws || client == NULL)
return;
hide(client);
set_to_workspace(client, ws);
}
void remove_from_workspace(client_t *client)
{
assert(client != NULL);
if (client->ws == WORKSPACE_NONE)
return;
/* Is it currently on its workspace */
if (focuswin(client->ws) == client)
set_focuswin(client->ws, NULL);
/* Remove old position and update old tree */
wtree_remove(client->wsitem);
update_clues(wslist[client->ws], screen_rect());
client->ws = WORKSPACE_NONE;
}
/*
* set client to one or no workspace
*/
// XXX: fix for fullsreeen
void set_to_workspace(client_t *client, uint32_t ws)
{
assert(client != NULL);
assert(ws < WORKSPACES);
PDEBUG("set workspace for 0x%x to %u\n", client->id, ws);
if (client->ws == ws)
return;
remove_from_workspace(client);
client->ws = ws;
/* new workspace to be added to */
/* Is there a focused window we can add to ? */
wtree_t *node = client->wsitem;
client_t *focus = focuswin(ws);
/* Check for active window */
if (focus == NULL) {
/* Attach client to root */
if (wslist[ws]->child == NULL) {
/* No tiling-node on root, attach tiling-node with node. */
wtree_append_tile_child(wslist[ws], node, tiling_mode);
} else {
tree_t *parent = wslist[ws]->child;
if (wtree_tiling(parent) == tiling_mode) {
/* Tiling-node on root with the same tiling, append. */
wtree_append_child(parent, node);
} else {
assert(parent->child != NULL);
/* Tiling-node on root with different tiling,
- on singleton: change tiling-mode and add to
- on siblings : add as tile-sibling
*/
if (parent->child->next == NULL && parent->child->prev == NULL) {
wtree_set_tiling(parent, tiling_mode);
wtree_add_sibling(parent->child, node);
} else {
wtree_append_tile_child(parent, node, tiling_mode);
}
}
}
} else {
/* Attach to active window */
if (wtree_parent_tiling(focus->wsitem) != tiling_mode) {
/* Tiling-node of focuswin with different tiling,
- on singleton: change tiling-mode and add to
- on siblings : replace focuswin with tiling-node
and add as sibling
*/
if (focus->wsitem->next == NULL && focus->wsitem->prev == NULL)
wtree_set_parent_tiling(focus->wsitem, tiling_mode);
else
wtree_inter_tile(focus->wsitem, tiling_mode);
}
wtree_add_sibling(focus->wsitem, node);
}
/* Set _NET_WM_DESKTOP accordingly or leave it */
xcb_ewmh_set_wm_desktop(ewmh, client->id, ws);
// fixup geometries in tree
if (! (wtree_is_floating(node) || client->fullscreen))
update_clues(wslist[ws], screen_rect());
wtree_print_tree(wslist[ws]);
xcb_flush(conn);
}
/* Change current workspace to ws. */
void change_workspace(uint32_t ws)
{
if (ws == curws) {
return;
}
PDEBUG("Changing from workspace #%u to #%u\n", curws, ws);
/*
* We lose our focus if the window we focus isn't fixed. An
* EnterNotify event will set focus later.
*/
unset_focus();
/* Apply hidden window event mask, this ensures no invalid enter events */
wtree_traverse_clients(wslist[curws], &set_hidden_events);
/* Go through list of current ws. Unmap everything that isn't fixed. */
wtree_traverse_clients(wslist[curws], &hide);
/* Set the new current workspace */
xcb_ewmh_set_current_desktop(ewmh, screen_number, ws);
curws = ws;
/* Go through list of new ws and map everything */
wtree_traverse_clients(wslist[curws], &show);
/* Re-enable enter events */
wtree_traverse_clients(wslist[curws], &set_default_events);
/* Map the windows now */
xcb_flush(conn);
/* Set focus on the window under the mouse */
focus_under_cursor();
}
/*
* Get the pixel values of a named color colstr.
*
* Returns pixel values.
*/
uint32_t getcolor(const char *colstr)
{
xcb_alloc_named_color_reply_t *col_reply;
xcb_generic_error_t *error;
xcb_alloc_named_color_cookie_t colcookie;
uint32_t color;
if (! colstr)
return 0;
if (strlen(colstr) > 1 && *colstr == '#') {
return (uint32_t)strtoul((colstr + 1), NULL, 16);
}
colcookie = xcb_alloc_named_color(conn, screen->default_colormap, strlen(colstr),
colstr);
col_reply = xcb_alloc_named_color_reply(conn, colcookie, &error);
if (col_reply == NULL) {
PERROR("Couldn't get pixel value for color %s. Exiting.\n", colstr);
print_x_error(error);
destroy(error);
cleanup(1);
}
color = col_reply->pixel;
destroy(col_reply);
return color;
}
/* Check new geometrys legality, apply hints and update window */
int update_geometry(client_t *client,
const xcb_rectangle_t *geometry)
{
xcb_rectangle_t monitor;
xcb_rectangle_t geo;
if (geometry)
geo = *geometry;
else
geo = client->geometry;
const xcb_size_hints_t *hints = &client->hints;
const int border = client->fullscreen ? 0 : conf.borderwidth;
get_monitor_geometry(client->monitor, &monitor);
/* XXX: check if geometry or monitor geometry changed (or hints, maybe set a geo changed flag) */
/* Fullscreen, skip the checks */
if (client->fullscreen) {
geo = monitor;
goto out;
}
// or fullscreen ? XXX: some hints?
if (! wtree_is_floating(client->wsitem))
goto out;
/* Is geometry proposed, or do we check current */
if (! geometry)
geo = client->geometry;
else
geo = *geometry;