-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwm.c
3778 lines (3156 loc) · 90.9 KB
/
wm.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
/* Copyright (c) 2016-2019 Tudor Ioan Roman. All rights reserved. */
/* Licensed under the ISC License. See the LICENSE file in the project root for full license information. */
#include <xcb/randr.h>
#include <xcb/xcb.h>
#include <xcb/xcb_ewmh.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xcb_keysyms.h>
#include <X11/keysym.h>
#include <assert.h>
#include <err.h>
#include <getopt.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tgmath.h>
#include <unistd.h>
#include <sys/wait.h>
#include "common.h"
#include "config.h"
#include "ipc.h"
#include "helpers.h"
#include "types.h"
#define EVENT_MASK(ev) (((ev) & ~0x80))
/* XCB event with the biggest value */
#define LAST_XCB_EVENT XCB_GET_MODIFIER_MAPPING
#define NULL_GROUP 0xffffffff
#define PI 3.14159265
/* atoms identifiers */
enum { WM_DELETE_WINDOW, WINDOWCHEF_ACTIVE_GROUPS, _IPC_ATOM_COMMAND, WINDOWCHEF_STATUS, NR_ATOMS };
/* button identifiers */
enum { BUTTON_LEFT, BUTTON_MIDDLE, BUTTON_RIGHT, NR_BUTTONS };
/* connection to the X server */
static xcb_connection_t *conn;
static xcb_ewmh_connection_t *ewmh;
static xcb_screen_t *scr;
static struct client *focused_win;
static struct conf conf;
/* number of the screen we're using */
static int scrno;
/* base for checking randr events */
static int randr_base;
static bool halt;
static int exit_code;
static bool *group_in_use = NULL;
static int last_group = 0;
/* keyboard modifiers (for mouse support) */
static uint16_t num_lock, caps_lock, scroll_lock;
static const xcb_button_index_t mouse_buttons[] = {
XCB_BUTTON_INDEX_1,
XCB_BUTTON_INDEX_2,
XCB_BUTTON_INDEX_3,
};
/* list of all windows. NULL is the empty list */
static struct list_item *win_list = NULL;
static struct list_item *mon_list = NULL;
static struct list_item *focus_list = NULL;
static char *atom_names[NR_ATOMS] = {
"WM_DELETE_WINDOW",
"WINDOWCHEF_ACTIVE_GROUPS",
ATOM_COMMAND,
"WINDOWCHEF_STATUS",
};
static xcb_atom_t ATOMS[NR_ATOMS];
/* function handlers for ipc commands */
static void (*ipc_handlers[NR_IPC_COMMANDS])(uint32_t *);
/* function handlers for events received from the X server */
static void (*events[LAST_XCB_EVENT + 1])(xcb_generic_event_t *);
static void cleanup(void);
static int setup(void);
static int setup_randr(void);
static void get_randr(void);
static void get_outputs(xcb_randr_output_t *, int len, xcb_timestamp_t);
static struct monitor * find_monitor(xcb_randr_output_t);
static struct monitor * find_monitor_by_coord(int16_t, int16_t);
static struct monitor * find_clones(xcb_randr_output_t, int16_t, int16_t);
static struct monitor * add_monitor(xcb_randr_output_t, char *, int16_t, int16_t, uint16_t, uint16_t);
static void free_monitor(struct monitor *);
static void get_monitor_size(struct client *, int16_t *, int16_t *, uint16_t *, uint16_t *);
static void arrange_by_monitor(struct monitor *);
static struct client * setup_window(xcb_window_t);
static void set_focused_no_raise(struct client *);
static void set_focused(struct client *);
static void set_focused_last_best();
static void raise_window(xcb_window_t);
static void close_window(struct client *);
static void delete_window(xcb_window_t);
static void teleport_window(xcb_window_t, int16_t, int16_t);
static void move_window(xcb_window_t , int16_t, int16_t);
static void resize_window_absolute(xcb_window_t, uint16_t, uint16_t);
static void resize_window(xcb_window_t, int16_t, int16_t);
static void fit_on_screen(struct client *);
static void maximize_window(struct client *, int16_t, int16_t, uint16_t, uint16_t);
static void hmaximize_window(struct client *, int16_t, uint16_t);
static void vmaximize_window(struct client *, int16_t, uint16_t);
static void monocle_window(struct client *, int16_t, int16_t, uint16_t, uint16_t);
static void reset_window(struct client *);
static bool is_special(struct client *);
static void cycle_window(struct client *);
static void rcycle_window(struct client *);
static void cycle_window_in_group(struct client *);
static void rcycle_window_in_group(struct client *);
static void cardinal_focus(uint32_t);
static float get_distance_between_windows(struct client *, struct client *);
static float get_angle_between_windows(struct client *, struct client *);
static struct win_position get_window_position(uint32_t, struct client *);
static bool is_overlapping(struct client *, struct client *);
static bool is_in_valid_direction(uint32_t, float, float);
static bool is_in_cardinal_direction(uint32_t , struct client *, struct client *);
static xcb_atom_t get_atom(char *);
static void update_desktop_viewport(void);
static bool get_pointer_location(xcb_window_t *, int16_t *, int16_t *);
static void center_pointer(struct client *);
static struct client * find_client(xcb_window_t *);
static bool get_geometry(xcb_window_t *, int16_t *, int16_t *, uint16_t *, uint16_t *, uint8_t *);
static void set_borders(struct client *client, uint32_t, uint32_t);
static bool is_mapped(xcb_window_t);
static void free_window(struct client *);
static void add_to_client_list(xcb_window_t);
static void update_client_list(void);
static void update_wm_desktop(struct client *);
static void update_current_desktop(struct client *);
static void update_window_status(struct client *);
static void group_add_window(struct client *, uint32_t);
static void group_remove_window(struct client *);
static void group_remove_all_windows(uint32_t);
static void group_activate(uint32_t);
static void group_deactivate(uint32_t);
static void group_toggle(uint32_t);
static void group_activate_specific(uint32_t);
static void update_group_list(void);
static void change_nr_of_groups(uint32_t);
static void refresh_borders(void);
static void update_ewmh_wm_state(struct client *);
static void handle_wm_state(struct client *, xcb_atom_t, unsigned int);
static void snap_window(struct client *, enum position);
static void grid_window(struct client *, uint16_t, uint16_t, uint16_t, uint16_t, uint16_t, uint16_t);
static void move_grid_window(struct client *, uint16_t, uint16_t);
static void resize_grid_window(struct client *, uint16_t, uint16_t);
static void register_event_handlers(void);
static void event_configure_request(xcb_generic_event_t *);
static void event_destroy_notify(xcb_generic_event_t *);
static void event_enter_notify(xcb_generic_event_t *);
static void event_map_request(xcb_generic_event_t *);
static void event_map_notify(xcb_generic_event_t *);
static void event_unmap_notify(xcb_generic_event_t *);
static void event_configure_notify(xcb_generic_event_t *);
static void event_circulate_request(xcb_generic_event_t *);
static void event_client_message(xcb_generic_event_t *);
static void event_focus_in(xcb_generic_event_t *);
static void event_focus_out(xcb_generic_event_t *);
static void event_button_press(xcb_generic_event_t *);
static void register_ipc_handlers(void);
static void ipc_window_move(uint32_t *);
static void ipc_window_move_absolute(uint32_t *);
static void ipc_window_resize(uint32_t *);
static void ipc_window_resize_absolute(uint32_t *);
static void ipc_window_maximize(uint32_t *);
static void ipc_window_unmaximize(uint32_t *);
static void ipc_window_hor_maximize(uint32_t *);
static void ipc_window_ver_maximize(uint32_t *);
static void ipc_window_monocle(uint32_t *);
static void ipc_window_close(uint32_t *);
static void ipc_window_put_in_grid(uint32_t *);
static void ipc_window_move_in_grid(uint32_t *);
static void ipc_window_resize_in_grid(uint32_t *);
static void ipc_window_snap(uint32_t *);
static void ipc_window_cycle(uint32_t *);
static void ipc_window_rev_cycle(uint32_t *);
static void ipc_window_cycle_in_group(uint32_t *);
static void ipc_window_rev_cycle_in_group(uint32_t *);
static void ipc_window_cardinal_focus(uint32_t *);
static void ipc_window_focus(uint32_t *);
static void ipc_window_focus_last(uint32_t *);
static void ipc_group_add_window(uint32_t *);
static void ipc_group_remove_window(uint32_t *);
static void ipc_group_remove_all_windows(uint32_t *);
static void ipc_group_activate(uint32_t *);
static void ipc_group_deactivate(uint32_t *);
static void ipc_group_toggle(uint32_t *);
static void ipc_group_activate_specific(uint32_t *);
static void ipc_wm_quit(uint32_t *);
static void ipc_wm_config(uint32_t *);
static void pointer_init(void);
static int16_t pointer_modfield_from_keysym(xcb_keysym_t);
static void window_grab_buttons(xcb_window_t);
static void window_grab_button(xcb_window_t, uint8_t, uint16_t);
static bool pointer_grab(enum pointer_action);
static enum resize_handle get_handle(struct client *, xcb_point_t, enum pointer_action);
static void track_pointer(struct client *, enum pointer_action, xcb_point_t);
static void grab_buttons(void);
static void ungrab_buttons(void);
static void usage(char *);
static void version(void);
static void load_defaults(void);
static void load_config(char *);
/*
* Gracefully disconnect.
*/
static void
cleanup(void)
{
xcb_set_input_focus(conn, XCB_NONE, XCB_INPUT_FOCUS_POINTER_ROOT,
XCB_CURRENT_TIME);
ungrab_buttons();
if (ewmh != NULL)
xcb_ewmh_connection_wipe(ewmh);
if (win_list != NULL)
list_delete_all_items(&win_list, true);
if (focus_list != NULL)
list_delete_all_items(&focus_list, true);
if (conn != NULL)
xcb_disconnect(conn);
}
/*
* Connect to the X server and initialize some things.
*/
static int
setup(void)
{
/* init xcb and grab events */
unsigned int values[1];
int mask;
conn = xcb_connect(NULL, &scrno);
if (xcb_connection_has_error(conn)) {
return -1;
}
/* get the first screen. hope it's the last one too */
scr = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
focused_win = NULL;
mask = XCB_CW_EVENT_MASK;
values[0] = XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
| XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
xcb_generic_error_t *e = xcb_request_check(conn,
xcb_change_window_attributes_checked(conn, scr->root,
mask, values));
if (e != NULL) {
free(e);
errx(EXIT_FAILURE, "Another window manager is already running.");
}
/* initialize ewmh variables */
ewmh = calloc(1, sizeof(xcb_ewmh_connection_t));
if (!ewmh)
warnx("couldn't set up ewmh connection");
xcb_intern_atom_cookie_t *cookie = xcb_ewmh_init_atoms(conn, ewmh);
xcb_ewmh_init_atoms_replies(ewmh, cookie, (void *)0);
xcb_ewmh_set_wm_pid(ewmh, scr->root, getpid());
xcb_ewmh_set_wm_name(ewmh, scr->root, strlen(__NAME__), __NAME__);
xcb_ewmh_set_current_desktop(ewmh, 0, 0);
xcb_ewmh_set_number_of_desktops(ewmh, 0, GROUPS);
update_desktop_viewport();
xcb_atom_t supported_atoms[] = {
ewmh->_NET_SUPPORTED , ewmh->_NET_WM_DESKTOP ,
ewmh->_NET_NUMBER_OF_DESKTOPS , ewmh->_NET_CURRENT_DESKTOP ,
ewmh->_NET_ACTIVE_WINDOW , ewmh->_NET_WM_STATE ,
ewmh->_NET_WM_STATE_FULLSCREEN , ewmh->_NET_WM_STATE_MAXIMIZED_VERT ,
ewmh->_NET_WM_STATE_MAXIMIZED_HORZ , ewmh->_NET_WM_NAME ,
ewmh->_NET_WM_ICON_NAME , ewmh->_NET_WM_WINDOW_TYPE ,
ewmh->_NET_WM_WINDOW_TYPE_DOCK , ewmh->_NET_WM_PID ,
ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR , ewmh->_NET_WM_WINDOW_TYPE_DESKTOP ,
ewmh->_NET_SUPPORTING_WM_CHECK , ewmh->_NET_DESKTOP_VIEWPORT ,
};
xcb_ewmh_set_supported(ewmh, scrno, sizeof(supported_atoms) / sizeof(xcb_atom_t), supported_atoms);
xcb_ewmh_set_supporting_wm_check(ewmh, scr->root, scr->root);
pointer_init();
/* send requests */
xcb_flush(conn);
/* get various atoms for icccm and ewmh */
for (int i = 0; i < NR_ATOMS; i++)
ATOMS[i] = get_atom(atom_names[i]);
randr_base = setup_randr();
group_in_use = malloc(conf.groups * sizeof(bool));
for (uint32_t i = 0; i < conf.groups; i++)
group_in_use[i] = false;
return 0;
}
/*
* Tells the server we want to use randr.
*/
static int
setup_randr(void)
{
int base;
const xcb_query_extension_reply_t *r = xcb_get_extension_data(conn, &xcb_randr_id);
if (!r->present)
return -1;
else
get_randr();
base = r->first_event;
xcb_randr_select_input(conn, scr->root,
XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
| XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
| XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE
| XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
return base;
}
/*
* Get information regarding randr.
*/
static void
get_randr(void)
{
int len;
xcb_randr_get_screen_resources_current_cookie_t c
= xcb_randr_get_screen_resources_current(conn, scr->root);
xcb_randr_get_screen_resources_current_reply_t *r
= xcb_randr_get_screen_resources_current_reply(conn, c, NULL);
if (r == NULL)
return;
xcb_timestamp_t timestamp = r->config_timestamp;
len = xcb_randr_get_screen_resources_current_outputs_length(r);
xcb_randr_output_t *outputs
= xcb_randr_get_screen_resources_current_outputs(r);
/* Request information for all outputs */
get_outputs(outputs, len, timestamp);
free(r);
}
/*
* Gets information about connected outputs.
*/
static void
get_outputs(xcb_randr_output_t *outputs, int len, xcb_timestamp_t timestamp)
{
int name_len;
char *name;
xcb_randr_get_crtc_info_cookie_t info_c;
xcb_randr_get_crtc_info_reply_t *crtc;
xcb_randr_get_output_info_reply_t *output;
struct monitor *mon, *clonemon;
struct list_item *item;
xcb_randr_get_output_info_cookie_t out_cookie[len];
for (int i = 0; i < len; i++)
out_cookie[i] = xcb_randr_get_output_info(conn, outputs[i],
timestamp);
for (int i = 0; i < len; i++) {
output = xcb_randr_get_output_info_reply(conn, out_cookie[i], NULL);
if (output == NULL)
continue;
name_len = xcb_randr_get_output_info_name_length(output);
if (16 < name_len)
name_len = 16;
/* +1 for the null character */
name = malloc(name_len + 1);
/* make sure the name is at most name_len + 1 length
* or we may run into problems. */
snprintf(name, name_len + 1, "%.*s", name_len,
xcb_randr_get_output_info_name(output));
if (output->crtc != XCB_NONE) {
info_c = xcb_randr_get_crtc_info(conn, output->crtc,
timestamp);
crtc = xcb_randr_get_crtc_info_reply(conn, info_c, NULL);
if (crtc == NULL)
return;
clonemon = find_clones(outputs[i], crtc->x, crtc->y);
if (clonemon != NULL)
continue;
mon = find_monitor(outputs[i]);
if (mon == NULL) {
add_monitor(outputs[i], name, crtc->x, crtc->y,
crtc->width, crtc->height);
} else {
mon->x = crtc->x;
mon->y = crtc->y;
mon->width = crtc->width;
mon->height = crtc->height;
arrange_by_monitor(mon);
}
free(crtc);
} else {
/* Check if the monitor was used before
* becoming disabled. */
mon = find_monitor(outputs[i]);
if (mon) {
struct client *client;
for (item = win_list; item != NULL; item = item->next) {
/* Move window from this monitor to
* either the next one or the first one. */
client = item->data;
if (client->monitor == mon) {
if (client->monitor->item->next)
/* If at end, take from the beginning */
if (mon_list == NULL)
client->monitor = NULL;
else
client->monitor = mon_list->data;
else
client->monitor = client->monitor->item->next->data;
fit_on_screen(client);
}
}
/* Monitor not active. Delete it. */
free_monitor(mon);
}
}
if (output != NULL)
free(output);
free(name);
}
}
/*
* Finds a monitor in the list.
*/
struct monitor *
find_monitor(xcb_randr_output_t mon)
{
struct list_item *item;
struct monitor *m;
item = mon_list;
while (item != NULL && (m = item->data)->monitor != mon)
item = item->next;
if (item == NULL)
return NULL;
else
return item->data;
}
/*
* Find a monitor in the list by its coordinates.
*/
static struct monitor *
find_monitor_by_coord(int16_t x, int16_t y)
{
struct list_item *item;
struct monitor *m, *ret;
m = ret = NULL;
item = mon_list;
while (item != NULL) {
m = item->data;
if (x >= m->x && x <= m->x + m->width
&& y >= m->y && y <= m->y + m->height)
ret = m;
item = item->next;
}
return ret;
}
/*
* Find cloned (mirrored) outputs.
*/
struct monitor *
find_clones(xcb_randr_output_t mon, int16_t x, int16_t y)
{
struct monitor *clonemon;
struct list_item *item;
item = mon_list;
while (item != NULL && ((clonemon = item->data)->monitor == mon
|| clonemon->x != x
|| clonemon->y != y)) {
item = item->next;
}
if (item == NULL)
return NULL;
else
return clonemon;
}
/*
* Add a monitor to the global monitor list.
*/
static struct monitor *
add_monitor(xcb_randr_output_t mon, char *name, int16_t x, int16_t y, uint16_t width, uint16_t height)
{
struct list_item *item;
struct monitor *monitor = malloc(sizeof(struct monitor));
if (monitor == NULL)
return NULL;
item = list_add_item(&mon_list);
if (item == NULL) {
free(monitor);
return NULL;
}
item->data = monitor;
monitor->item = item;
monitor->monitor = mon;
monitor->name = name;
monitor->x = x;
monitor->y = y;
monitor->width = width;
monitor->height = height;
return monitor;
}
/*
* Free a monitor from the global monitor list.
*/
static void
free_monitor(struct monitor *mon)
{
struct list_item *item = mon->item;
free(mon);
list_delete_item(&mon_list, item);
}
/*
* Get information about a certain monitor situated in a window: coordinates and size.
*/
static void
get_monitor_size(struct client *client, int16_t *mon_x, int16_t *mon_y, uint16_t *mon_width, uint16_t *mon_height)
{
if (client == NULL || client->monitor == NULL) {
if (mon_x != NULL && mon_y != NULL)
*mon_x = *mon_y = 0;
if (mon_width != NULL)
*mon_width = scr->width_in_pixels;
if (mon_height != NULL)
*mon_height = scr->height_in_pixels;
} else {
if (mon_x != NULL)
*mon_x = client->monitor->x;
if (mon_y != NULL)
*mon_y = client->monitor->y;
if (mon_width != NULL)
*mon_width = client->monitor->width;
if (mon_height != NULL)
*mon_height = client->monitor->height;
}
}
/*
* Arrange clients on a monitor.
*/
static void
arrange_by_monitor(struct monitor *mon)
{
struct client *client;
struct list_item *item;
for (item = win_list; item != NULL; item = item->next) {
client = item->data;
if (client->monitor == mon)
fit_on_screen(client);
}
}
/*
* Wait for events and handle them.
*/
static void
run(void)
{
xcb_generic_event_t *ev;
update_group_list();
halt = false;
exit_code = EXIT_SUCCESS;
while (!halt) {
xcb_flush(conn);
ev = xcb_wait_for_event(conn);
if (ev) {
DMSG("X Event %d\n", ev->response_type & ~0x80);
if (ev->response_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
get_randr();
DMSG("Screen layout changed\n");
}
if (events[EVENT_MASK(ev->response_type)] != NULL)
(events[EVENT_MASK(ev->response_type)])(ev);
free(ev);
}
}
}
/*
* Initialize a window for further work.
*/
static struct client *
setup_window(xcb_window_t win)
{
uint32_t values[2];
xcb_ewmh_get_atoms_reply_t win_type;
xcb_atom_t atom;
struct client *client;
struct list_item *item;
struct list_item *focus_item;
xcb_size_hints_t hints;
if (xcb_ewmh_get_wm_window_type_reply(ewmh,
xcb_ewmh_get_wm_window_type(ewmh, win),
&win_type, NULL) == 1) {
unsigned int i = 0;
/* if the window is a toolbar or a dock, map it and ignore it */
while (i < win_type.atoms_len &&
(atom = win_type.atoms[i]) != ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR
&& atom != ewmh->_NET_WM_WINDOW_TYPE_DOCK
&& atom != ewmh->_NET_WM_WINDOW_TYPE_DESKTOP)
i++;
if (i < win_type.atoms_len) {
xcb_ewmh_get_atoms_reply_wipe(&win_type);
xcb_map_window(conn, win);
return NULL;
}
}
/* subscribe to events */
values[0] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
xcb_change_window_attributes(conn, win, XCB_CW_EVENT_MASK, values);
/* in case of fire */
xcb_change_save_set(conn, XCB_SET_MODE_INSERT, win);
/* assign to the null group */
xcb_ewmh_set_wm_desktop(ewmh, win, NULL_GROUP);
item = list_add_item(&win_list);
if (item == NULL)
return NULL;
focus_item = list_add_item(&focus_list);
if (focus_item == NULL)
return NULL;
client = malloc(sizeof(struct client));
if (client == NULL)
return NULL;
/* initialize variables */
focus_item->data = client;
client->focus_item = focus_item;
item->data = client;
client->item = item;
client->window = win;
client->geom.x = client->geom.y = client->geom.width
= client->geom.height
= client->min_width = client->min_height = 0;
client->grid.gx = client->grid.gy = client->grid.px
= client->grid.py = client->grid.sx = client->grid.sy = 0;
client->width_inc = client->height_inc = 1;
client->maxed = client->hmaxed = client->vmaxed
= client->monocled = client->gridded = client->geom.set_by_user = false;
client->monitor = NULL;
client->mapped = false;
client->group = NULL_GROUP;
get_geometry(&client->window, &client->geom.x, &client->geom.y,
&client->geom.width, &client->geom.height, &client->depth);
xcb_icccm_get_wm_normal_hints_reply(conn,
xcb_icccm_get_wm_normal_hints_unchecked(conn, win),
&hints, NULL);
if (hints.flags & XCB_ICCCM_SIZE_HINT_US_POSITION)
client->geom.set_by_user = true;
if (hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {
client->min_width = hints.min_width;
client->min_height = hints.min_height;
}
if (hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC) {
client->width_inc = hints.width_inc;
client->height_inc = hints.height_inc;
}
update_window_status(client);
DMSG("new window was born 0x%08x\n", client->window);
return client;
}
/*
* Set focus state to active or inactive without raising the window.
*/
static void
set_focused_no_raise(struct client *client)
{
long data[] = {
XCB_ICCCM_WM_STATE_NORMAL,
XCB_NONE,
};
if (client == NULL)
return;
/* show window if hidden */
xcb_map_window(conn, client->window);
if (!client->maxed)
set_borders(client, conf.focus_color, conf.internal_focus_color);
/* focus the window */
xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
client->window, XCB_CURRENT_TIME);
/* set ewmh property */
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, scr->root,
ewmh->_NET_ACTIVE_WINDOW, XCB_ATOM_WINDOW, 32, 1, &client->window);
/* set window state */
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->window,
ewmh->_NET_WM_STATE, ewmh->_NET_WM_STATE, 32, 2, data);
/* set the focus state to inactive on the previously focused window */
if (client != focused_win) {
if (focused_win != NULL && !focused_win->maxed)
set_borders(focused_win, conf.unfocus_color, conf.internal_unfocus_color);
}
if (client->focus_item != NULL)
list_move_to_head(&focus_list, client->focus_item);
focused_win = client;
window_grab_buttons(focused_win->window);
}
/*
* Focus and raise.
*/
static void
set_focused(struct client *client)
{
set_focused_no_raise(client);
raise_window(client->window);
}
/*
* Focus last best focus (in a valid group, mapped, etc)
*/
static void
set_focused_last_best()
{
struct list_item *focused_item;
struct client *client;
focused_item = focus_list->next;
if (focused_item == NULL)
focused_item = focus_list;
while (focused_item != NULL) {
client = focused_item->data;
if (client != NULL && client->mapped) {
set_focused(client);
return;
}
focused_item = focused_item->next;
}
}
/*
* Put window at the top of the window stack.
*/
static void
raise_window(xcb_window_t win)
{
uint32_t values[1] = { XCB_STACK_MODE_ABOVE };
xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
}
/*
* Ask window to close gracefully. If the window doesn't respond, kill it.
*/
static void
close_window(struct client *client)
{
if (client == NULL)
return;
if (conf.last_window_focusing && client != NULL && client == focused_win)
set_focused_last_best();
if (focused_win == client)
focused_win = NULL;
xcb_window_t win = client->window;
xcb_get_property_cookie_t cookie =
xcb_icccm_get_wm_protocols_unchecked(conn,
win, ewmh->WM_PROTOCOLS);
xcb_icccm_get_wm_protocols_reply_t reply;
unsigned int i = 0;
bool got = false;
if (xcb_icccm_get_wm_protocols_reply(conn, cookie, &reply, NULL)) {
for (i = 0; i < reply.atoms_len; i++) {
got = (reply.atoms[i] = ATOMS[WM_DELETE_WINDOW]);
if (got)
break;
}
xcb_icccm_get_wm_protocols_reply_wipe(&reply);
}
if (got)
delete_window(win);
else
xcb_kill_client(conn, win);
}
/*
* Gracefully ask a window to close.
*/
static void
delete_window(xcb_window_t win)
{
xcb_client_message_event_t ev;
ev.response_type = XCB_CLIENT_MESSAGE;
ev.sequence = 0;
ev.format = 32;
ev.window = win;
ev.type = ewmh->WM_PROTOCOLS;
ev.data.data32[0] = ATOMS[WM_DELETE_WINDOW];
ev.data.data32[1] = XCB_CURRENT_TIME;
xcb_send_event(conn, 0, win, XCB_EVENT_MASK_NO_EVENT, (char *)&ev);
}
/*
* Teleports window absolutely to the given coordinates.
*/
static void
teleport_window(xcb_window_t win, int16_t x, int16_t y)
{
uint32_t values[2] = {x, y};
if (win == scr->root || win == 0)
return;
xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, values);
update_window_status(find_client(&win));
xcb_flush(conn);
}
/*
* Moves the window by a certain amount.
*/
static void
move_window(xcb_window_t win, int16_t x, int16_t y)
{
int16_t win_x, win_y;
uint16_t win_w, win_h;
if (!is_mapped(win) || win == scr->root)
return;
get_geometry(&win, &win_x, &win_y, &win_w, &win_h, NULL);
win_x += x;
win_y += y;
teleport_window(win, win_x, win_y);
}
/*
* Resizes window to the given size.
*/
static void
resize_window_absolute(xcb_window_t win, uint16_t w, uint16_t h)
{
uint32_t val[2];
uint32_t mask = XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT;
val[0] = w;
val[1] = h;
xcb_configure_window(conn, win, mask, val);
update_window_status(find_client(&win));
refresh_borders();
}
/*
* Resizes window by a certain amount.
*/
static void
resize_window(xcb_window_t win, int16_t w, int16_t h)
{
struct client *client;
int32_t aw, ah;
client = find_client(&win);
if (client == NULL)
return;
aw = client->geom.width;
ah = client->geom.height;
if (aw + w > 0)
aw += w;
if (ah + h > 0)
ah += h;
/* avoid weird stuff */
if (aw < 0)
aw = 0;
if (ah < 0)
ah = 0;
if (client->min_width != 0 && aw < client->min_width)
aw = client->min_width;
if (client->min_height != 0 && ah < client->min_height)
ah = client->min_height;
client->geom.width = aw - conf.resize_hints * (aw % client->width_inc);
client->geom.height = ah - conf.resize_hints * (ah % client->height_inc);
resize_window_absolute(win, client->geom.width, client->geom.height);
}
/*
* Fit window on screen if too big.
*/
static void
fit_on_screen(struct client *client)
{
int16_t mon_x, mon_y;
uint16_t mon_width, mon_height;