-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsscrotwm.c
5159 lines (4451 loc) · 119 KB
/
sscrotwm.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) 2012 Jacob Courtneay <[email protected]>
* Copyright (c) 2009-2012 Marco Peereboom <[email protected]>
* Copyright (c) 2009-2011 Ryan McBride <[email protected]>
* Copyright (c) 2009 Darrin Chandler <[email protected]>
* Copyright (c) 2009 Pierre-Yves Ritschard <[email protected]>
* Copyright (c) 2010 Tuukka Kataja <[email protected]>
* Copyright (c) 2011 Jason L. Wright <[email protected]>
* Copyright (c) 2011-2012 Reginald Kennedy <[email protected]>
* Copyright (c) 2011-2012 Lawrence Teo <[email protected]>
* Copyright (c) 2011-2012 Tiago Cunha <[email protected]>
*
* 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.
*/
/*
* Much code and ideas taken from dwm under the following license:
* MIT/X Consortium License
*
* 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
* 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
* 2006-2007 Jukka Salmi <jukka at salmi dot ch>
* 2007 Premysl Hruby <dfenze at gmail dot com>
* 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
* 2007 Christof Musik <christof at sendfax dot de>
* 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
* 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
* 2008 Martin Hurton <martin dot hurton at gmail dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include <util.h>
#include <pwd.h>
#include <paths.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/queue.h>
#include <sys/param.h>
#include <sys/select.h>
#if defined(__linux__)
#include "tree.h"
#elif defined(__OpenBSD__)
#include <sys/tree.h>
#elif defined(__FreeBSD__)
#include <sys/tree.h>
#else
#include "tree.h"
#endif
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xrandr.h>
#ifdef __OSX__
#include <osx.h>
#endif
#include "version.h"
#ifdef SSCROTWM_BUILDSTR
static const char *buildstr = SSCROTWM_BUILDSTR;
#else
static const char *buildstr = SSCROTWM_VERSION;
#endif
#if RANDR_MAJOR < 1
# error XRandR versions less than 1.0 are not supported
#endif
#if RANDR_MAJOR >= 1
#if RANDR_MINOR >= 2
#define SWM_XRR_HAS_CRTC
#endif
#endif
/*#define SWM_DEBUG*/
#ifdef SWM_DEBUG
#define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
#define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
#define SWM_D_MISC 0x0001
#define SWM_D_EVENT 0x0002
#define SWM_D_WS 0x0004
#define SWM_D_FOCUS 0x0008
#define SWM_D_MOVE 0x0010
#define SWM_D_STACK 0x0020
#define SWM_D_MOUSE 0x0040
#define SWM_D_PROP 0x0080
#define SWM_D_CLASS 0x0100
#define SWM_D_KEY 0x0200
#define SWM_D_QUIRK 0x0400
#define SWM_D_SPAWN 0x0800
#define SWM_D_EVENTQ 0x1000
#define SWM_D_CONF 0x2000
u_int32_t swm_debug = 0
| SWM_D_MISC
| SWM_D_EVENT
| SWM_D_WS
| SWM_D_FOCUS
| SWM_D_MOVE
| SWM_D_STACK
| SWM_D_MOUSE
| SWM_D_PROP
| SWM_D_CLASS
| SWM_D_KEY
| SWM_D_QUIRK
| SWM_D_SPAWN
| SWM_D_EVENTQ
| SWM_D_CONF
;
#else
#define DPRINTF(x...)
#define DNPRINTF(n,x...)
#endif
#define LENGTH(x) (sizeof x / sizeof x[0])
#define MODKEY Mod1Mask
#define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
#define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
#define MOUSEMASK (BUTTONMASK|PointerMotionMask)
#define SWM_PROPLEN (16)
#define SWM_FUNCNAME_LEN (32)
#define SWM_KEYS_LEN (255)
#define SWM_QUIRK_LEN (64)
#define X(r) (r)->g.x
#define Y(r) (r)->g.y
#define WIDTH(r) (r)->g.w
#define HEIGHT(r) (r)->g.h
#define SH_MIN(w) (w)->sh_mask & PMinSize
#define SH_MIN_W(w) (w)->sh.min_width
#define SH_MIN_H(w) (w)->sh.min_height
#define SH_MAX(w) (w)->sh_mask & PMaxSize
#define SH_MAX_W(w) (w)->sh.max_width
#define SH_MAX_H(w) (w)->sh.max_height
#define SH_INC(w) (w)->sh_mask & PResizeInc
#define SH_INC_W(w) (w)->sh.width_inc
#define SH_INC_H(w) (w)->sh.height_inc
#define WINID(w) ((w) ? (w)->id : 0)
#define YESNO(x) ((x) ? "yes" : "no")
char **start_argv;
Atom astate;
Atom aprot;
Atom adelete;
Atom takefocus;
Atom a_wmname;
Atom a_netwmname;
Atom a_utf8_string;
Atom a_string;
volatile sig_atomic_t running = 1;
volatile sig_atomic_t restart_wm = 0;
int outputs = 0;
int (*xerrorxlib)(Display *, XErrorEvent *);
int other_wm;
int xrandr_support;
int xrandr_eventbase;
unsigned int numlockmask = 0;
Display *display;
/* configurable vars */
unsigned int mod_key = MODKEY;
double dialog_ratio = 0.6;
int disable_border = 0;
int border_width = 1;
struct passwd *pwd;
/* layout manager data */
struct swm_geometry {
int x;
int y;
int w;
int h;
};
struct swm_screen;
struct workspace;
/* virtual "screens" */
struct swm_region {
TAILQ_ENTRY(swm_region) entry;
struct swm_geometry g;
struct workspace *ws; /* current workspace on this region */
struct workspace *ws_prior; /* prior workspace on this region */
struct swm_screen *s; /* screen idx */
};
TAILQ_HEAD(swm_region_list, swm_region);
struct ws_win {
TAILQ_ENTRY(ws_win) entry;
Window id;
Window transient;
struct ws_win *child_trans; /* transient child window */
struct swm_geometry g; /* current geometry */
struct swm_geometry g_float; /* geometry when floating */
struct swm_geometry rg_float; /* region geom when floating */
int g_floatvalid; /* g_float geometry validity */
int floatmaxed; /* whether maxed by max_stack */
int floating;
int manual;
unsigned int ewmh_flags;
int can_delete;
int take_focus;
unsigned long quirks;
struct workspace *ws; /* always valid */
struct swm_screen *s; /* always valid, never changes */
XWindowAttributes wa;
XSizeHints sh;
long sh_mask;
XClassHint ch;
XWMHints *hints;
};
TAILQ_HEAD(ws_win_list, ws_win);
/* pid goo */
struct pid_e {
TAILQ_ENTRY(pid_e) entry;
long pid;
int ws;
};
TAILQ_HEAD(pid_list, pid_e);
struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
/* layout handlers */
void stack(void);
void vertical_config(struct workspace *, int);
void vertical_stack(struct workspace *, struct swm_geometry *);
void horizontal_config(struct workspace *, int);
void horizontal_stack(struct workspace *, struct swm_geometry *);
void max_stack(struct workspace *, struct swm_geometry *);
struct ws_win *find_window(Window);
void grabbuttons(struct ws_win *, int);
void new_region(struct swm_screen *, int, int, int, int);
void unmanage_window(struct ws_win *);
long getstate(Window);
int conf_load(char *);
struct layout {
void (*l_stack)(struct workspace *, struct swm_geometry *);
void (*l_config)(struct workspace *, int);
u_int32_t flags;
#define SWM_L_FOCUSPREV (1<<0)
#define SWM_L_MAPONFOCUS (1<<1)
} layouts[] = {
/* stack, configure */
{ vertical_stack, vertical_config, 0 },
{ horizontal_stack, horizontal_config, 0 },
{ max_stack, NULL, SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV },
{ NULL, NULL, 0 },
};
/* position of max_stack mode in the layouts array, index into layouts! */
#define SWM_V_STACK (0)
#define SWM_H_STACK (1)
#define SWM_MAX_STACK (2)
#define SWM_H_SLICE (32)
#define SWM_V_SLICE (32)
/* define work spaces */
struct workspace {
int idx; /* workspace index */
char *name; /* workspace name */
int always_raise; /* raise windows on focus */
struct layout *cur_layout; /* current layout handlers */
struct ws_win *focus; /* may be NULL */
struct ws_win *focus_prev; /* may be NULL */
struct swm_region *r; /* may be NULL */
struct swm_region *old_r; /* may be NULL */
struct ws_win_list winlist; /* list of windows in ws */
struct ws_win_list unmanagedlist; /* list of dead windows in ws */
/* stacker state */
struct {
int horizontal_msize;
int horizontal_mwin;
int horizontal_stacks;
int horizontal_flip;
int vertical_msize;
int vertical_mwin;
int vertical_stacks;
int vertical_flip;
} l_state;
};
enum { SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
/* physical screen mapping */
#define SWM_WS_MAX (10)
struct swm_screen {
int idx; /* screen index */
struct swm_region_list rl; /* list of regions on this screen */
struct swm_region_list orl; /* list of old regions */
Window root;
struct workspace ws[SWM_WS_MAX];
/* colors */
struct {
unsigned long color;
char *name;
} c[SWM_S_COLOR_MAX];
};
struct swm_screen *screens;
/* args to functions */
union arg {
int id;
#define SWM_ARG_ID_FOCUSNEXT (0)
#define SWM_ARG_ID_FOCUSPREV (1)
#define SWM_ARG_ID_FOCUSMAIN (2)
#define SWM_ARG_ID_FOCUSCUR (4)
#define SWM_ARG_ID_SWAPNEXT (10)
#define SWM_ARG_ID_SWAPPREV (11)
#define SWM_ARG_ID_SWAPMAIN (12)
#define SWM_ARG_ID_MOVELAST (13)
#define SWM_ARG_ID_MASTERSHRINK (20)
#define SWM_ARG_ID_MASTERGROW (21)
#define SWM_ARG_ID_MASTERADD (22)
#define SWM_ARG_ID_MASTERDEL (23)
#define SWM_ARG_ID_FLIPLAYOUT (24)
#define SWM_ARG_ID_STACKRESET (30)
#define SWM_ARG_ID_STACKINIT (31)
#define SWM_ARG_ID_CYCLEWS_UP (40)
#define SWM_ARG_ID_CYCLEWS_DOWN (41)
#define SWM_ARG_ID_CYCLESC_UP (42)
#define SWM_ARG_ID_CYCLESC_DOWN (43)
#define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
#define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
#define SWM_ARG_ID_STACKINC (50)
#define SWM_ARG_ID_STACKDEC (51)
#define SWM_ARG_ID_KILLWINDOW (80)
#define SWM_ARG_ID_DELETEWINDOW (81)
#define SWM_ARG_ID_WIDTHGROW (90)
#define SWM_ARG_ID_WIDTHSHRINK (91)
#define SWM_ARG_ID_HEIGHTGROW (92)
#define SWM_ARG_ID_HEIGHTSHRINK (93)
#define SWM_ARG_ID_MOVEUP (100)
#define SWM_ARG_ID_MOVEDOWN (101)
#define SWM_ARG_ID_MOVELEFT (102)
#define SWM_ARG_ID_MOVERIGHT (103)
char **argv;
};
void focus(struct swm_region *, union arg *);
void focus_magic(struct ws_win *);
/* quirks */
struct quirk {
TAILQ_ENTRY(quirk) entry;
char *class;
char *name;
unsigned long quirk;
#define SWM_Q_FLOAT (1<<0) /* float this window */
#define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
#define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
#define SWM_Q_FULLSCREEN (1<<3) /* remove border */
#define SWM_Q_FOCUSPREV (1<<4) /* focus on caller */
};
TAILQ_HEAD(quirk_list, quirk);
struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
/*
* Supported EWMH hints should be added to
* both the enum and the ewmh array
*/
enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
_NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
_NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
_NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
_NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
_NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
_NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
_NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
_NET_WM_ACTION_RESIZE, _NET_WM_ACTION_CLOSE,
SWM_EWMH_HINT_MAX };
struct ewmh_hint {
char *name;
Atom atom;
} ewmh[SWM_EWMH_HINT_MAX] = {
/* must be in same order as in the enum */
{"_NET_ACTIVE_WINDOW", None},
{"_NET_MOVERESIZE_WINDOW", None},
{"_NET_CLOSE_WINDOW", None},
{"_NET_WM_WINDOW_TYPE", None},
{"_NET_WM_WINDOW_TYPE_DOCK", None},
{"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
{"_NET_WM_WINDOW_TYPE_UTILITY", None},
{"_NET_WM_WINDOW_TYPE_SPLASH", None},
{"_NET_WM_WINDOW_TYPE_DIALOG", None},
{"_NET_WM_WINDOW_TYPE_NORMAL", None},
{"_NET_WM_STATE", None},
{"_NET_WM_STATE_SKIP_TASKBAR", None},
{"_NET_WM_STATE_SKIP_PAGER", None},
{"_NET_WM_STATE_ABOVE", None},
{"_SWM_WM_STATE_MANUAL", None},
{"_NET_WM_STATE_FULLSCREEN", None},
{"_NET_WM_ALLOWED_ACTIONS", None},
{"_NET_WM_ACTION_MOVE", None},
{"_NET_WM_ACTION_RESIZE", None},
{"_NET_WM_ACTION_CLOSE", None},
};
void store_float_geom(struct ws_win *, struct swm_region *);
int floating_toggle_win(struct ws_win *);
int
get_property(Window id, Atom atom, long count, Atom type, unsigned long *nitems,
unsigned long *nbytes, unsigned char **data)
{
int format, status;
unsigned long *nbytes_ret, *nitems_ret;
unsigned long nbytes_tmp, nitems_tmp;
Atom real;
nbytes_ret = nbytes != NULL ? nbytes : &nbytes_tmp;
nitems_ret = nitems != NULL ? nitems : &nitems_tmp;
status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
&real, &format, nitems_ret, nbytes_ret, data);
if (status != Success)
return False;
if (real != type)
return False;
return True;
}
void
setup_ewmh(void)
{
int i,j;
Atom sup_list;
sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
for (i = 0; i < LENGTH(ewmh); i++)
ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
for (i = 0; i < ScreenCount(display); i++) {
/* Support check window will be created by workaround(). */
/* Report supported atoms */
XDeleteProperty(display, screens[i].root, sup_list);
for (j = 0; j < LENGTH(ewmh); j++)
XChangeProperty(display, screens[i].root,
sup_list, XA_ATOM, 32,
PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
}
}
void
teardown_ewmh(void)
{
int i, success;
unsigned char *data = NULL;
unsigned long n;
Atom sup_check, sup_list;
Window id;
sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
for (i = 0; i < ScreenCount(display); i++) {
/* Get the support check window and destroy it */
success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
&n, NULL, &data);
if (success) {
id = data[0];
XDestroyWindow(display, id);
XDeleteProperty(display, screens[i].root, sup_check);
XDeleteProperty(display, screens[i].root, sup_list);
}
XFree(data);
}
}
void
ewmh_autoquirk(struct ws_win *win)
{
int success, i;
unsigned long *data = NULL, n;
Atom type;
success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
XA_ATOM, &n, NULL, (void *)&data);
if (!success) {
XFree(data);
return;
}
for (i = 0; i < n; i++) {
type = data[i];
if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
break;
if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
win->floating = 1;
win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
break;
}
if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
win->floating = 1;
win->quirks = SWM_Q_FLOAT;
break;
}
}
XFree(data);
}
#define SWM_EWMH_ACTION_COUNT_MAX (6)
#define EWMH_F_FULLSCREEN (1<<0)
#define EWMH_F_ABOVE (1<<1)
#define EWMH_F_SKIP_PAGER (1<<2)
#define EWMH_F_SKIP_TASKBAR (1<<3)
#define SWM_F_MANUAL (1<<4)
int
ewmh_set_win_fullscreen(struct ws_win *win, int fs)
{
struct swm_geometry rg;
if (!win->ws->r)
return 0;
if (!win->floating)
return 0;
DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%lx, "
"fullscreen %s\n", win->id, YESNO(fs));
rg = win->ws->r->g;
if (fs) {
store_float_geom(win, win->ws->r);
win->g = rg;
} else {
if (win->g_floatvalid) {
/* refloat at last floating relative position */
X(win) = win->g_float.x - win->rg_float.x + rg.x;
Y(win) = win->g_float.y - win->rg_float.y + rg.y;
WIDTH(win) = win->g_float.w;
HEIGHT(win) = win->g_float.h;
}
}
return 1;
}
void
ewmh_update_actions(struct ws_win *win)
{
Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
int n = 0;
if (win == NULL)
return;
actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
if (win->floating) {
actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
}
XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
}
#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
#define _NET_WM_STATE_ADD 1 /* add/set property */
#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
void
ewmh_update_win_state(struct ws_win *win, long state, long action)
{
unsigned int mask = 0;
unsigned int changed = 0;
unsigned int orig_flags;
if (win == NULL)
return;
if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
mask = EWMH_F_FULLSCREEN;
if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
mask = EWMH_F_ABOVE;
if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
mask = SWM_F_MANUAL;
if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
mask = EWMH_F_SKIP_PAGER;
if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
mask = EWMH_F_SKIP_TASKBAR;
orig_flags = win->ewmh_flags;
switch (action) {
case _NET_WM_STATE_REMOVE:
win->ewmh_flags &= ~mask;
break;
case _NET_WM_STATE_ADD:
win->ewmh_flags |= mask;
break;
case _NET_WM_STATE_TOGGLE:
win->ewmh_flags ^= mask;
break;
}
changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
if (changed)
if (!floating_toggle_win(win))
win->ewmh_flags = orig_flags; /* revert */
if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
if (changed)
win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
if (changed)
if (!ewmh_set_win_fullscreen(win,
win->ewmh_flags & EWMH_F_FULLSCREEN))
win->ewmh_flags = orig_flags; /* revert */
XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
if (win->ewmh_flags & EWMH_F_FULLSCREEN)
XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
XA_ATOM, 32, PropModeAppend,
(unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
XA_ATOM, 32, PropModeAppend,
(unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
XA_ATOM, 32, PropModeAppend,
(unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
if (win->ewmh_flags & EWMH_F_ABOVE)
XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
XA_ATOM, 32, PropModeAppend,
(unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
if (win->ewmh_flags & SWM_F_MANUAL)
XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
XA_ATOM, 32, PropModeAppend,
(unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
}
void
ewmh_get_win_state(struct ws_win *win)
{
int success, i;
unsigned long n;
Atom *states;
if (win == NULL)
return;
win->ewmh_flags = 0;
if (win->floating)
win->ewmh_flags |= EWMH_F_ABOVE;
if (win->manual)
win->ewmh_flags |= SWM_F_MANUAL;
success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
(~0L), XA_ATOM, &n, NULL, (void *)&states);
if (!success)
return;
for (i = 0; i < n; i++)
ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
XFree(states);
}
/* events */
#ifdef SWM_DEBUG
char *
geteventname(XEvent *e)
{
char *name = NULL;
switch (e->type) {
case KeyPress:
name = "KeyPress";
break;
case KeyRelease:
name = "KeyRelease";
break;
case ButtonPress:
name = "ButtonPress";
break;
case ButtonRelease:
name = "ButtonRelease";
break;
case MotionNotify:
name = "MotionNotify";
break;
case EnterNotify:
name = "EnterNotify";
break;
case LeaveNotify:
name = "LeaveNotify";
break;
case FocusIn:
name = "FocusIn";
break;
case FocusOut:
name = "FocusOut";
break;
case KeymapNotify:
name = "KeymapNotify";
break;
case Expose:
name = "Expose";
break;
case GraphicsExpose:
name = "GraphicsExpose";
break;
case NoExpose:
name = "NoExpose";
break;
case VisibilityNotify:
name = "VisibilityNotify";
break;
case CreateNotify:
name = "CreateNotify";
break;
case DestroyNotify:
name = "DestroyNotify";
break;
case UnmapNotify:
name = "UnmapNotify";
break;
case MapNotify:
name = "MapNotify";
break;
case MapRequest:
name = "MapRequest";
break;
case ReparentNotify:
name = "ReparentNotify";
break;
case ConfigureNotify:
name = "ConfigureNotify";
break;
case ConfigureRequest:
name = "ConfigureRequest";
break;
case GravityNotify:
name = "GravityNotify";
break;
case ResizeRequest:
name = "ResizeRequest";
break;
case CirculateNotify:
name = "CirculateNotify";
break;
case CirculateRequest:
name = "CirculateRequest";
break;
case PropertyNotify:
name = "PropertyNotify";
break;
case SelectionClear:
name = "SelectionClear";
break;
case SelectionRequest:
name = "SelectionRequest";
break;
case SelectionNotify:
name = "SelectionNotify";
break;
case ColormapNotify:
name = "ColormapNotify";
break;
case ClientMessage:
name = "ClientMessage";
break;
case MappingNotify:
name = "MappingNotify";
break;
default:
name = "Unknown";
}
return name;
}
char *
xrandr_geteventname(XEvent *e)
{
char *name = NULL;
switch(e->type - xrandr_eventbase) {
case RRScreenChangeNotify:
name = "RRScreenChangeNotify";
break;
default:
name = "Unknown";
}
return name;
}
void
dumpwins(struct swm_region *r, union arg *args)
{
struct ws_win *win;
unsigned int state;
XWindowAttributes wa;
if (r->ws == NULL) {
warnx("dumpwins: invalid workspace");
return;
}
warnx("=== managed window list ws %02d ===", r->ws->idx);
TAILQ_FOREACH(win, &r->ws->winlist, entry) {
state = getstate(win->id);
if (!XGetWindowAttributes(display, win->id, &wa))
warnx("window: 0x%lx, failed XGetWindowAttributes",
win->id);
warnx("window: 0x%lx, map_state: %d, state: %d, "
"transient: 0x%lx", win->id, wa.map_state, state,
win->transient);
}
warnx("===== unmanaged window list =====");
TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
state = getstate(win->id);
if (!XGetWindowAttributes(display, win->id, &wa))
warnx("window: 0x%lx, failed XGetWindowAttributes",
win->id);
warnx("window: 0x%lx, map_state: %d, state: %d, "
"transient: 0x%lx", win->id, wa.map_state, state,
win->transient);
}
warnx("=================================");
}
#else
void
dumpwins(struct swm_region *r, union arg *args)
{
}
#endif /* SWM_DEBUG */
void expose(XEvent *);
void keypress(XEvent *);
void buttonpress(XEvent *);
void configurerequest(XEvent *);
void configurenotify(XEvent *);
void destroynotify(XEvent *);
void mapnotify(XEvent *);
void mappingnotify(XEvent *);
void maprequest(XEvent *);
void propertynotify(XEvent *);
void unmapnotify(XEvent *);
void visibilitynotify(XEvent *);
void clientmessage(XEvent *);
void (*handler[LASTEvent])(XEvent *) = {
[Expose] = expose,
[KeyPress] = keypress,
[ButtonPress] = buttonpress,
[ConfigureRequest] = configurerequest,
[ConfigureNotify] = configurenotify,
[DestroyNotify] = destroynotify,
[MapNotify] = mapnotify,
[MappingNotify] = mappingnotify,
[MapRequest] = maprequest,
[PropertyNotify] = propertynotify,
[UnmapNotify] = unmapnotify,
[VisibilityNotify] = visibilitynotify,
[ClientMessage] = clientmessage,
};
void
sighdlr(int sig)
{
int saved_errno, status;
pid_t pid;
saved_errno = errno;
switch (sig) {
case SIGCHLD:
while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
if (pid == -1) {
if (errno == EINTR)
continue;
#ifdef SWM_DEBUG
if (errno != ECHILD)
warn("sighdlr: waitpid");
#endif /* SWM_DEBUG */
break;
}
#ifdef SWM_DEBUG
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0)
warnx("sighdlr: child exit status: %d",
WEXITSTATUS(status));
} else
warnx("sighdlr: child is terminated "
"abnormally");
#endif /* SWM_DEBUG */
}
break;
case SIGHUP:
restart_wm = 1;
break;
case SIGINT:
case SIGTERM:
case SIGQUIT:
running = 0;
break;
}
errno = saved_errno;
}
struct pid_e *
find_pid(long pid)
{
struct pid_e *p = NULL;
DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
if (pid == 0)
return (NULL);
TAILQ_FOREACH(p, &pidlist, entry) {
if (p->pid == pid)
return (p);
}
return (NULL);
}
unsigned long