forked from JLErvin/berry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wm.c
2516 lines (2177 loc) · 73.3 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) 2018 Joshua L Ervin. All rights reserved. */
/* Licensed under the MIT License. See the LICENSE file in the project root for full license information. */
#include "config.h"
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xinerama.h>
#include <X11/extensions/shape.h>
#include <X11/cursorfont.h>
#include <X11/Xft/Xft.h>
#include "globals.h"
#include "ipc.h"
#include "types.h"
#include "utils.h"
static struct client *f_client = NULL; /* focused client */
static struct client *c_list[WORKSPACE_NUMBER]; /* 'stack' of managed clients in drawing order */
static struct client *f_list[WORKSPACE_NUMBER]; /* ordered lists for clients to be focused */
static struct monitor *m_list = NULL; /* All saved monitors */
static struct config conf; /* gloabl config */
static int ws_m_list[WORKSPACE_NUMBER]; /* Mapping from workspaces to associated monitors */
static int curr_ws = 0;
static int m_count = 0;
static Cursor move_cursor, normal_cursor;
static Display *display = NULL;
static Atom net_atom[NetLast], wm_atom[WMLast], net_berry[BerryLast];
static Window root, check, nofocus;
static bool running = true;
static bool debug = false;
static int screen, display_width, display_height;
static int (*xerrorxlib)(Display *, XErrorEvent *);
static XftColor xft_focus_color, xft_unfocus_color;
static XftFont *font;
static char global_font[MAXLEN] = DEFAULT_FONT;
static XRenderColor r_color;
static GC gc;
static Atom utf8string;
/* All functions */
/* Client management functions */
static void client_cardinal_focus(struct client *c, int dir);
static void client_center(struct client *c);
static void client_center_in_rect(struct client *c, int x, int y, unsigned w, unsigned h);
static void client_close(struct client *c);
static void client_decorations_create(struct client *c);
static void client_decorations_destroy(struct client *c);
static void client_delete(struct client *c);
static void client_fullscreen(struct client *c, bool toggle, bool fullscreen, bool max);
static void client_hide(struct client *c);
static void client_manage_focus(struct client *c);
static void client_move_absolute(struct client *c, int x, int y);
static void client_move_relative(struct client *c, int x, int y);
static void client_move_to_front(struct client *c);
static void client_monocle(struct client *c);
static void client_place(struct client *c);
static void client_raise(struct client *c);
static void client_refresh(struct client *c);
static void client_resize_absolute(struct client *c, int w, int h);
static void client_resize_relative(struct client *c, int w, int h);
static void client_save(struct client *c, int ws);
static void client_send_to_ws(struct client *c, int ws);
static void client_set_color(struct client *c, unsigned long i_color, unsigned long b_color);
static void client_set_input(struct client *c);
static void client_set_title(struct client *c);
static void client_show(struct client *c);
static void client_snap_left(struct client *c);
static void client_snap_right(struct client *c);
static void client_toggle_decorations(struct client *c);
static void client_set_status(struct client *c);
/* EWMH functions */
static void ewmh_set_fullscreen(struct client *c, bool fullscreen);
static void ewmh_set_viewport(void);
static void ewmh_set_focus(struct client *c);
static void ewmh_set_desktop(struct client *c, int ws);
static void ewmh_set_frame_extents(struct client *c);
static void ewmh_set_client_list(void);
static void ewmh_set_desktop_names(void);
static void ewmh_set_active_desktop(int ws);
/* Event handlers */
static void handle_client_message(XEvent *e);
static void handle_configure_notify(XEvent *e);
static void handle_configure_request(XEvent *e);
static void handle_focus(XEvent *e);
static void handle_map_request(XEvent *e);
static void handle_unmap_notify(XEvent *e);
static void handle_button_press(XEvent *e);
static void handle_expose(XEvent *e);
static void handle_property_notify(XEvent *e);
static void handle_enter_notify(XEvent *e);
/* IPC client functions */
static void ipc_move_absolute(long *d);
static void ipc_move_relative(long *d);
static void ipc_monocle(long *d);
static void ipc_raise(long *d);
static void ipc_resize_absolute(long *d);
static void ipc_resize_relative(long *d);
static void ipc_toggle_decorations(long *d);
static void ipc_window_close(long *d);
static void ipc_window_center(long *d);
static void ipc_switch_ws(long *d);
static void ipc_send_to_ws(long *d);
static void ipc_fullscreen(long *d);
static void ipc_fullscreen_state(long *d);
static void ipc_snap_left(long *d);
static void ipc_snap_right(long *d);
static void ipc_cardinal_focus(long *d);
static void ipc_cycle_focus(long *d);
static void ipc_pointer_focus(long *d);
static void ipc_config(long *d);
static void ipc_save_monitor(long *d);
static void ipc_set_font(long *d);
static void ipc_edge_gap(long *d);
static void monitors_free(void);
static void monitors_setup(void);
static void close_wm(void);
static void draw_text(struct client *c, bool focused);
static struct client* get_client_from_window(Window w);
static void load_color(XftColor *dest_color, unsigned long raw_color);
static void load_config(char *conf_path);
static void manage_new_window(Window w, XWindowAttributes *wa);
static int manage_xsend_icccm(struct client *c, Atom atom);
static void grab_buttons(void);
static void ungrab_buttons(void);
static void refresh_config(void);
static void run(void);
static bool safe_to_focus(int ws);
static void setup(void);
static void switch_ws(int ws);
static void warp_pointer(struct client *c);
static void usage(void);
static void version(void);
static int xerror(Display *display, XErrorEvent *e);
static int get_actual_x(struct client *c);
static int get_actual_y(struct client *c);
static int get_actual_width(struct client *c);
static int get_actual_height(struct client *c);
static int get_dec_width(struct client *c);
static int get_dec_height(struct client *c);
static int left_width(struct client *c);
static int top_height(struct client *c);
typedef void (*x11_event_handler_t)(XEvent *e);
typedef void (*ipc_event_handler_t)(long *e);
/* Native X11 Event handler */
static const x11_event_handler_t event_handler [LASTEvent] = {
[MapRequest] = handle_map_request,
[UnmapNotify] = handle_unmap_notify,
[ConfigureNotify] = handle_configure_notify,
[ConfigureRequest] = handle_configure_request,
[ClientMessage] = handle_client_message,
[ButtonPress] = handle_button_press,
[PropertyNotify] = handle_property_notify,
[Expose] = handle_expose,
[FocusIn] = handle_focus,
[EnterNotify] = handle_enter_notify,
};
static const ipc_event_handler_t ipc_handler [IPCLast] = {
[IPCWindowMoveRelative] = ipc_move_relative,
[IPCWindowMoveAbsolute] = ipc_move_absolute,
[IPCWindowMonocle] = ipc_monocle,
[IPCWindowRaise] = ipc_raise,
[IPCWindowResizeRelative] = ipc_resize_relative,
[IPCWindowResizeAbsolute] = ipc_resize_absolute,
[IPCWindowToggleDecorations] = ipc_toggle_decorations,
[IPCWindowClose] = ipc_window_close,
[IPCWindowCenter] = ipc_window_center,
[IPCSwitchWorkspace] = ipc_switch_ws,
[IPCSendWorkspace] = ipc_send_to_ws,
[IPCFullscreen] = ipc_fullscreen,
[IPCFullscreenState] = ipc_fullscreen_state,
[IPCSnapLeft] = ipc_snap_left,
[IPCSnapRight] = ipc_snap_right,
[IPCCardinalFocus] = ipc_cardinal_focus,
[IPCCycleFocus] = ipc_cycle_focus,
[IPCPointerFocus] = ipc_pointer_focus,
[IPCSaveMonitor] = ipc_save_monitor,
[IPCSetFont] = ipc_set_font,
[IPCEdgeGap] = ipc_edge_gap,
[IPCConfig] = ipc_config
};
static unsigned
euclidean_distance(const struct client *a, const struct client *b)
{
int dx = a->geom.x - b->geom.x, dy = a->geom.y - b->geom.y;
return dx*dx + dy*dy;
}
/* Give focus to the given client in the given direction */
static void
client_cardinal_focus(struct client *c, int dir)
{
struct client *tmp, *focus_next;
int min;
tmp = c_list[curr_ws];
focus_next = NULL;
min = INT_MAX;
while (tmp != NULL) {
int dist = euclidean_distance(c, tmp);
switch (dir) {
case EAST:
LOGN("Focusing EAST");
if (tmp->geom.x > c->geom.x && dist < min) {
min = dist;
focus_next = tmp;
}
break;
case SOUTH:
LOGN("Focusing SOUTH");
if (tmp->geom.y > c->geom.y && dist < min) {
min = dist;
focus_next = tmp;
}
break;
case WEST:
LOGN("Focusing WEST");
if (tmp->geom.x < c->geom.x && dist < min) {
min = dist;
focus_next = tmp;
}
break;
case NORTH:
LOGN("Focusing NORTH");
if (tmp->geom.y < c->geom.y && dist < min) {
min = dist;
focus_next = tmp;
}
break;
}
tmp = tmp->next;
}
if (focus_next == NULL) {
LOGN("Cannot cardinal focus, no valid windows found");
return;
} else {
LOGP("Valid window found in direction %d, focusing", dir);
client_manage_focus(focus_next);
}
}
/* Move a client to the center of the screen, centered vertically and horizontally
* by the middle of the Client
*/
static void
client_center(struct client *c)
{
int mon;
LOGN("Centering Client");
mon = ws_m_list[c->ws];
client_center_in_rect(c, m_list[mon].x, m_list[mon].y, m_list[mon].width, m_list[mon].height);
}
static int
ceil10 (int n)
{
return (n + 9) - (n + 9) % 10;
}
static void
client_center_in_rect(struct client *c, int x, int y, unsigned w, unsigned h)
{
LOGP("Centering at x=%d, y=%d, w=%u, h=%u", x, y, w, h);
int new_x = ceil10(x + (conf.left_gap - conf.right_gap) / 2 + w / 2 - c->geom.width / 2);
int new_y = ceil10(y + (conf.top_gap - conf.bot_gap) / 2 + h / 2 - c->geom.height / 2);
LOGP("Sending to x=%d, y=%d", new_x, new_y);
client_move_absolute(c, new_x, new_y);
client_refresh(c); // in case we went over the top gap
}
/* Close connection to the current display */
static void
close_wm(void)
{
LOGN("Shutting down window manager");
for (int i = 0; i < WORKSPACE_NUMBER; i++) {
while (c_list[i] != NULL)
client_delete(c_list[i]);
}
XDeleteProperty(display, root, net_berry[BerryWindowStatus]);
XDeleteProperty(display, root, net_berry[BerryFontProperty]);
XDeleteProperty(display, root, net_atom[NetSupported]);
LOGN("Closing display...");
XCloseDisplay(display);
}
static void
draw_text(struct client *c, bool focused)
{
XftDraw *draw;
XftColor *xft_render_color;
XGlyphInfo extents;
int x, y, len;
if (!conf.draw_text) {
LOGN("drawing text disabled");
return;
}
if (!c->decorated) {
LOGN("Client not decorated, not drawing text");
return;
}
XftTextExtentsUtf8(display, font, (XftChar8 *)c->title, strlen(c->title), &extents);
y = (conf.t_height / 2) + ((extents.y) / 2);
x = !conf.t_center ? TITLE_X_OFFSET : (c->geom.width - extents.width) / 2;
for (len = strlen(c->title); len >= 0; len--) {
XftTextExtentsUtf8(display, font, (XftChar8 *)c->title, len, &extents);
if (extents.xOff < c->geom.width)
break;
}
LOGP("Text height is %u", extents.height);
if (extents.y > conf.t_height) {
LOGN("Text is taller than title bar height, not drawing text");
return;
}
LOGN("Drawing text on client");
LOGN("Drawing the following text");
LOGP(" %s", c->title);
XClearWindow(display, c->dec);
draw = XftDrawCreate(display, c->dec, DefaultVisual(display, screen), DefaultColormap(display, screen));
xft_render_color = focused ? &xft_focus_color : &xft_unfocus_color;
XftDrawStringUtf8(draw, xft_render_color, font, x, y, (XftChar8 *) c->title, strlen(c->title));
XftDrawDestroy(draw);
}
/* Communicate with the given Client, kindly telling it to close itself
* and terminate any associated processes using the WM_DELETE_WINDOW protocol
*/
static void
client_close(struct client *c)
{
XEvent ev;
ev.type = ClientMessage;
ev.xclient.window = c->window;
ev.xclient.message_type = wm_atom[WMProtocols];
ev.xclient.format = 32;
ev.xclient.data.l[0] = wm_atom[WMDeleteWindow];
ev.xclient.data.l[1] = CurrentTime;
XSendEvent(display, c->window, False, NoEventMask, &ev);
LOGN("Closing window...");
}
/* Create new "dummy" windows to be used as decorations for the given client */
static void
client_decorations_create(struct client *c)
{
LOGN("Decorating new client");
int w = c->geom.width + 2 * conf.i_width;
int h = c->geom.height + 2 * conf.i_width + conf.t_height;
int x = c->geom.x - conf.i_width - conf.b_width;
int y = c->geom.y - conf.i_width - conf.b_width - conf.t_height;
Window dec = XCreateSimpleWindow(display, root, x, y, w, h, conf.b_width,
conf.bu_color, conf.bf_color);
c->dec = dec;
c->decorated = true;
XSelectInput (display, c->dec, ExposureMask|EnterWindowMask);
XGrabButton(display, 1, AnyModifier, c->dec, True, ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync, None, None);
draw_text(c, true);
ewmh_set_frame_extents(c);
client_set_status(c);
}
/* Destroy any "dummy" windows associated with the given Client as decorations */
static void
client_decorations_destroy(struct client *c)
{
LOGN("Removing decorations");
c->decorated = false;
XUnmapWindow(display, c->dec);
XDestroyWindow(display, c->dec);
ewmh_set_frame_extents(c);
client_set_status(c);
}
/* Remove the given Client from the list of currently managed clients
* Does not free the given client from memory.
* */
static void
client_delete(struct client *c)
{
int ws;
ws = c->ws;
if (ws == -1) {
LOGN("Cannot delete client, not found");
return;
} else {
LOGP("Deleting client on workspace %d", ws);
}
/* Prevent BadDrawable error which sometimes occurs as a window is closed */
client_decorations_destroy(c);
/* Delete in the stack */
if (c_list[ws] == c) {
c_list[ws] = c_list[ws]->next;
} else {
struct client *tmp = c_list[ws];
while (tmp != NULL && tmp->next != c)
tmp = tmp->next;
if (tmp)
tmp->next = tmp->next->next;
}
/* Delete in the focus list */
/* I'll factor this out later */
/* Or actually it might not be so easy... */
if (f_list[ws] == c) {
f_list[ws] = f_list[ws]->f_next;
} else {
struct client *tmp = f_list[ws];
while (tmp != NULL && tmp->f_next != c)
tmp = tmp->f_next;
if (tmp)
tmp->f_next = tmp->f_next->f_next;
}
if (c_list[ws] == NULL)
f_client = NULL;
ewmh_set_client_list();
}
static void
monitors_free(void)
{
free(m_list);
m_list = NULL;
}
/* Set the given Client to be fullscreen. Moves the window to fill the dimensions
* of the given display.
* Updates the value of _NET_WM_STATE_FULLSCREEN to reflect fullscreen changes
*/
static void
client_fullscreen(struct client *c, bool toggle, bool fullscreen, bool max)
{
int mon;
bool to_fs;
mon = ws_m_list[c->ws];
UNUSED(max);
// save the old geometry values so that we can toggle between fulscreen mode
to_fs = toggle ? !c->fullscreen : fullscreen;
/* I'm going to keep this comment here for readability
* for the previous ternary statement
if (toggle)
to_fs = !c->fullscreen;
else
to_fs = fullscreen;
*/
// TODO: FACTOR THIS SHIT
if (to_fs) {
ewmh_set_fullscreen(c, true);
if (c->decorated && conf.fs_remove_dec) { //
client_decorations_destroy(c);
c->was_fs = true;
}
if (conf.fs_max) {
c->prev.x = c->geom.x;
c->prev.y = c->geom.y;
c->prev.width = c->geom.width;
c->prev.height = c->geom.height;
client_move_absolute(c, m_list[mon].x, m_list[mon].y);
client_resize_absolute(c, m_list[mon].width, m_list[mon].height);
}
c->fullscreen = true;
} else {
ewmh_set_fullscreen(c, false);
if (max) {
client_move_absolute(c, c->prev.x, c->prev.y);
client_resize_absolute(c, c->prev.width, c->prev.height);
}
if (!c->decorated && conf.fs_remove_dec && c->was_fs) { //
client_decorations_create(c);
XMapWindow(display, c->dec);
client_refresh(c);
client_raise(c);
client_manage_focus(c);
ewmh_set_frame_extents(c);
}
c->fullscreen = false;
c->was_fs = false;
client_refresh(c);
}
client_set_status(c);
}
/* Focus the next window in the list. Windows are sorted by the order in which they are
* created (mapped to the window manager)
*/
static void
focus_next(struct client *c)
{
if (c == NULL)
return;
int ws;
ws = c->ws;
if (f_list[ws] == c && f_list[ws]->f_next == NULL) {
client_manage_focus(f_list[ws]);
return;
}
struct client *tmp;
tmp = c->f_next == NULL ? f_list[ws] : c->f_next;
client_manage_focus(tmp);
}
static void
focus_best(struct client *c)
{
if (c == NULL)
return;
if (c_list[c->ws]->next == NULL)
return;
else
client_manage_focus(c_list[c->ws]->next);
}
/* Returns the struct client associated with the given struct Window */
static struct client*
get_client_from_window(Window w)
{
for (int i = 0; i < WORKSPACE_NUMBER; i++) {
for (struct client *tmp = c_list[i]; tmp != NULL; tmp = tmp->next) {
if (tmp->window == w)
return tmp;
else if (tmp->decorated && tmp->dec == w)
return tmp;
}
}
return NULL;
}
/* Redirect an XEvent from berry's client program, berryc */
static void
handle_client_message(XEvent *e)
{
XClientMessageEvent *cme = &e->xclient;
long cmd, *data;
if (cme->message_type == net_berry[BerryClientEvent]) {
LOGN("Recieved event from berryc");
if (cme->format != 32) {
LOGN("Wrong format, ignoring event");
return;
}
cmd = cme->data.l[0];
data = cme->data.l;
ipc_handler[cmd](data);
} else if (cme->message_type == net_atom[NetWMState]) {
struct client* c = get_client_from_window(cme->window);
if (c == NULL) {
LOGN("client not found...");
return;
}
if ((Atom)cme->data.l[1] == net_atom[NetWMStateFullscreen] ||
(Atom)cme->data.l[2] == net_atom[NetWMStateFullscreen]) {
LOGN("Recieved fullscreen request");
if (cme->data.l[0] == 0) { // remove fullscreen
/*ewmh_set_fullscreen(c, false);*/
client_fullscreen(c, false, false, true);
LOGN("type 0");
} else if (cme->data.l[0] == 1) { // set fullscreen
/*ewmh_set_fullscreen(c, true);*/
client_fullscreen(c, false, true, true);
LOGN("type 1");
} else if (cme->data.l[0] == 2) { // toggle fullscreen
/*ewmh_set_fullscreen(c, !c->fullscreen);*/
client_fullscreen(c, true, true, true);
LOGN("type 2");
}
}
} else if (cme->message_type == net_atom[NetActiveWindow]) {
struct client *c = get_client_from_window(cme->window);
if (c == NULL)
return;
client_manage_focus(c);
} else if (cme->message_type == net_atom[NetCurrentDesktop]) {
switch_ws(cme->data.l[0]);
} else if (cme->message_type == net_atom[NetWMMoveResize]) {
LOGN("Handling MOVERESIZE");
struct client *c = get_client_from_window(cme->window);
if (c == NULL)
return;
data = cme->data.l;
client_move_absolute(c, data[1], data[2]);
client_resize_absolute(c, data[3], data[4]);
}
}
static void
handle_button_press(XEvent *e)
{
/* Much credit to the authors of dwm for
* this function.
*/
XButtonPressedEvent *bev = &e->xbutton;
XEvent ev;
struct client *c;
int x, y, ocx, ocy, nx, ny, nw, nh, di, ocw, och;
unsigned int dui;
Window dummy;
Time current_time, last_motion;
XQueryPointer(display, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
LOGN("Handling button press event");
c = get_client_from_window(bev->window);
if (c == NULL)
return;
if (c != f_client) {
switch_ws(c->ws);
client_manage_focus(c);
}
ocx = c->geom.x;
ocy = c->geom.y;
ocw = c->geom.width;
och = c->geom.height;
last_motion = ev.xmotion.time;
if (XGrabPointer(display, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, None, move_cursor, CurrentTime) != GrabSuccess)
return;
do {
XMaskEvent(display, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
switch (ev.type) {
case ConfigureRequest:
case Expose:
case MapRequest:
event_handler[ev.type](&ev);
break;
case MotionNotify:
current_time = ev.xmotion.time;
Time diff_time = current_time - last_motion;
if (diff_time < (Time)conf.pointer_interval) {
continue;
}
last_motion = current_time;
if (ev.xbutton.state == (unsigned)(conf.move_mask|Button1Mask) || ev.xbutton.state == Button1Mask) {
nx = ocx + (ev.xmotion.x - x);
ny = ocy + (ev.xmotion.y - y);
if (conf.edge_lock)
client_move_relative(c, nx - c->geom.x, ny - c->geom.y);
else
client_move_absolute(c, nx, ny);
} else if (ev.xbutton.state == (unsigned)(conf.resize_mask|Button1Mask)) {
nw = ev.xmotion.x - x;
nh = ev.xmotion.y - y;
if (conf.edge_lock)
client_resize_relative(c, nw - c->geom.width + ocw, nh - c->geom.height + och);
else
client_resize_absolute(c, ocw + nw, och + nh);
}
XFlush(display);
break;
}
} while (ev.type != ButtonRelease);
XUngrabPointer(display, CurrentTime);
}
static void
handle_expose(XEvent *e)
{
XExposeEvent *ev = &e->xexpose;
struct client *c;
bool focused;
LOGN("Handling expose event");
c = get_client_from_window(ev->window);
if (c == NULL) {
LOGN("Expose event client not found, exiting");
return;
}
focused = c == f_client;
draw_text(c, focused);
}
static void
handle_focus(XEvent *e)
{
XFocusChangeEvent *ev = &e->xfocus;
UNUSED(ev);
return;
}
static void
handle_property_notify(XEvent *e)
{
XPropertyEvent *ev = &e->xproperty;
struct client *c;
LOGN("Handling property notify event");
c = get_client_from_window(ev->window);
if (c == NULL)
return;
if (ev->state == PropertyDelete)
return;
if (ev->atom == net_atom[NetWMName]) {
LOGN("Updating client title");
client_set_title(c);
draw_text(c, c == f_client);
}
}
static void
handle_configure_notify(XEvent *e)
{
XConfigureEvent *ev = &e->xconfigure;
if (ev->window == root) {
// handle display size changes by the root window
display_width = ev->width;
display_height = ev->height;
}
LOGN("Handling configure notify event");
monitors_free();
monitors_setup();
}
static void
handle_configure_request(XEvent *e)
{
struct client *c;
XConfigureRequestEvent *ev = &e->xconfigurerequest;
XWindowChanges wc;
LOGN("Handling configure request event");
wc.x = ev->x;
wc.y = ev->y;
wc.width = ev->width;
wc.height = ev->height;
wc.border_width = ev->border_width;
wc.sibling = ev->above;
wc.stack_mode = ev->detail;
XConfigureWindow(display, ev->window, ev->value_mask, &wc);
c = get_client_from_window(ev->window);
if (c != NULL) {
if (c->fullscreen)
return;
client_move_relative(c,
wc.x - get_actual_x(c) - 2 * left_width(c),
wc.y - get_actual_y(c) - 2 * top_height(c));
client_resize_relative(c,
wc.width - get_actual_width(c) + 2 * get_dec_width(c),
wc.height - get_actual_height(c) + 2 * get_dec_height(c));
client_refresh(c);
} else {
LOGN("Window for configure was not found");
}
}
static void
handle_map_request(XEvent *e)
{
static XWindowAttributes wa;
XMapRequestEvent *ev = &e->xmaprequest;
/*LOGN("Handling map request event");*/
if (!XGetWindowAttributes(display, ev->window, &wa))
return;
if (wa.override_redirect)
return;
manage_new_window(ev->window, &wa);
}
static void
handle_unmap_notify(XEvent *e)
{
XUnmapEvent *ev = &e->xunmap;
struct client *c;
c = get_client_from_window(ev->window);
if (c != NULL) {
LOGN("Client found while unmapping, focusing next client");
focus_best(c);
if (c->decorated)
XDestroyWindow(display, c->dec);
client_delete(c);
free(c);
client_raise(f_client);
} else {
/* Some applications *ahem* Spotify *ahem*, don't seem to place nicely with being deleted.
* They close slowing, causing focusing issues with unmap requests. Check to see if the current
* workspace is empty and, if so, focus the root client so that we can pick up new key presses..
*/
if (f_list[curr_ws] == NULL) {
LOGN("Client not found while deleting and ws is empty, focusing root window");
client_manage_focus(NULL);
} else {
LOGN("Client not found while deleting and ws is non-empty, doing nothing");
}
}
}
static void
handle_enter_notify(XEvent *e)
{
XEnterWindowEvent *ev = &e->xcrossing;
struct client *c;
if (!conf.follow_pointer)
return;
c = get_client_from_window(ev->window);
if (c != NULL && c != f_client) {
bool warp_pointer;
warp_pointer = conf.warp_pointer;
conf.warp_pointer = false;
client_manage_focus(c);
if (c->ws != curr_ws)
switch_ws(c->ws);
conf.warp_pointer = warp_pointer;
}
}
/* Hides the given Client by moving it outside of the visible display */
static void
client_hide(struct client *c)
{
if (!c->hidden) {
c->x_hide = c->geom.x;
LOGN("Hiding client");
client_move_absolute(c, display_width + conf.b_width, c->geom.y);
c->hidden = true;
}
}
static void
ipc_move_absolute(long *d)
{
int x, y;
if (f_client == NULL)
return;
x = d[1];
y = d[2];
client_move_absolute(f_client, x, y);
}
static void
ipc_move_relative(long *d)
{
int x, y;
if (f_client == NULL)
return;
x = d[1];
y = d[2];
client_move_relative(f_client, x, y);
}
static void
ipc_monocle(long *d)
{
UNUSED(d);
if (f_client == NULL)
return;
client_monocle(f_client);
}
static void
ipc_raise(long *d)
{
UNUSED(d);
if (f_client == NULL)
return;
client_raise(f_client);
}
static void
ipc_resize_absolute(long *d)
{
int w, h;
if (f_client == NULL)
return;
w = d[1];
h = d[2];
client_resize_absolute(f_client, w, h);
}
static void
ipc_resize_relative(long *d)
{
int w, h;
if (f_client == NULL)
return;
w = d[1];
h = d[2];
client_resize_relative(f_client, w, h);
}
static void
ipc_toggle_decorations(long *d)
{
UNUSED(d);
if (f_client == NULL)
return ;
client_toggle_decorations(f_client);
}
static void
ipc_window_close(long *d)
{
UNUSED(d);
if (f_client == NULL)
return;
client_close(f_client);
}
static void
ipc_window_center(long *d)
{
UNUSED(d);
if (f_client == NULL)
return;
client_center(f_client);
}
static void
ipc_switch_ws(long *d)
{
int ws = d[1];
LOGP("IPC says switch to workspace %d", ws);