summaryrefslogtreecommitdiff
path: root/src/sxwm.c
blob: a1aae45093d41e1d75048250bf0850c26b20f648 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
/*
 *	  See LICENSE for more info
 *	  
 *	  simple xorg window manager
 *	  sxwm is a user-friendly, easily configurable yet powerful
 *	  tiling window manager inspired by window managers such as
 *	  DWM and i3.
 *	  
 *	  The userconfig is designed to be as user-friendly as
 *	  possible, and I hope it is easy to configure even without
 *	  knowledge of C or programming, although most people who
 *	  will use this will probably be programmers :)
 *
 *	  (C) Abhinav Prasai 2025
*/

#include <X11/cursorfont.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/XKBlib.h>

#include "defs.h"

static void add_client(Window w);
static void change_workspace(uint ws);
static uint clean_mask(uint mask);
static void close_focused(void);
static void dec_gaps(void);
static void focus_next(void);
static void focus_prev(void);
static void grab_keys(void);
static void hdl_button(XEvent *xev);
static void hdl_button_release(XEvent *xev);
static void hdl_client_msg(XEvent *xev);
static void hdl_dummy(XEvent *xev);
static void hdl_destroy_ntf(XEvent *xev);
static void hdl_enter(XEvent *xev);
static void hdl_keypress(XEvent *xev);
static void hdl_map_req(XEvent *xev);
static void hdl_motion(XEvent *xev);
static void inc_gaps(void);
static void move_master_next(void);
static void move_master_prev(void);
static void move_to_workspace(uint ws);
static void other_wm(void);
static int other_wm_err(Display *dpy, XErrorEvent *ee);
static ulong parse_col(const char *hex);
static void quit(void);
static void resize_master_add(void);
static void resize_master_sub(void);
static void run(void);
static void setup(void);
static void setup_atoms(void);
static void spawn(const char **cmd);
static void tile(void);
static void toggle_floating(void);
static void toggle_floating_global(void);
static void toggle_fullscreen(void);
static void update_borders(void);
static void update_net_client_list(void);
static int xerr(Display *dpy, XErrorEvent *ee);
static void xev_case(XEvent *xev);
#include "config.h"

static Atom atom_wm_delete;
static Atom atom_wm_strut_partial;
static Atom atom_wm_window_type;
static Atom atom_net_supported;
static Atom atom_net_wm_state;
static Atom atom_net_wm_state_fullscreen;
static Atom atom_net_wm_window_type_dock;
static Atom atom_net_workarea;

static Cursor c_normal, c_move, c_resize;
static Client *workspaces[NUM_WORKSPACES] = { NULL };
static uint current_ws = 0;
static Client *drag_client = NULL;
static Client *focused = NULL;
static EventHandler evtable[LASTEvent];
static Display *dpy;
static Window root;
static Bool global_floating = False;

static ulong last_motion_time = 0;
static ulong border_foc_col;
static ulong border_ufoc_col;
static float master_frac = MASTER_WIDTH;
static uint gaps = GAPS;
static uint scr_width;
static uint scr_height;
static uint open_windows = 0;
static int drag_start_x, drag_start_y;
static int drag_orig_x, drag_orig_y, drag_orig_w, drag_orig_h;

static uint reserve_left = 0;
static uint reserve_right = 0;
static uint reserve_top = 0;
static uint reserve_bottom = 0;
INIT_WORKSPACE

static void
add_client(Window w)
{
	Client *c = malloc(sizeof(Client));
	if (!c) {
		fprintf(stderr, "sxwm: could not alloc memory for client\n");
		return;
	}
	c->win = w;
	c->next = workspaces[current_ws];
	workspaces[current_ws] = c;
	if (!focused)
		focused = c;
	++open_windows;

	XSelectInput(dpy, w,
			EnterWindowMask | LeaveWindowMask |
			FocusChangeMask | PropertyChangeMask |
			StructureNotifyMask);
	Atom protos[] = { atom_wm_delete };
	XSetWMProtocols(dpy, w, protos, 1);

	XWindowAttributes wa;
	XGetWindowAttributes(dpy, w, &wa);
	c->x = wa.x;
	c->y = wa.y;
	c->w = wa.width;
	c->h = wa.height;
	c->floating = False;
	c->fullscreen = False;

	if (global_floating) {
		XSetWindowBorder(dpy, c->win, border_foc_col);
		XSetWindowBorderWidth(dpy, c->win, BORDER_WIDTH);
	}
	XRaiseWindow(dpy, w);
}

static uint
clean_mask(uint mask)
{
	return mask & ~(LockMask | Mod2Mask | Mod3Mask);
}

static void
close_focused(void)
{
	if (!focused) return;

	Atom *protos;
	int n;
	if (XGetWMProtocols(dpy, focused->win, &protos, &n) && protos) {
		for (int i = 0; i < n ; ++i)
			if (protos[i] == atom_wm_delete) {
				XEvent ev = { .xclient = {
					.type = ClientMessage,
					.window = focused->win,
					.message_type = XInternAtom(dpy,"WM_PROTOCOLS",False),
					.format = 32
				}};
				ev.xclient.data.l[0] = atom_wm_delete;
				ev.xclient.data.l[1] = CurrentTime;
				XSendEvent(dpy, focused->win, False, NoEventMask, &ev);
				XFree(protos);
				return;
			}
		XFree(protos);
	}
	XKillClient(dpy, focused->win);
}

static void
dec_gaps(void)
{
	if (gaps > 0) {
		--gaps;
		tile();
		update_borders();
	}
}

static void
focus_next(void)
{
	if (!focused)
		return;

	focused = (focused->next ? focused->next : workspaces[current_ws]);
	XSetInputFocus(dpy, focused->win, RevertToPointerRoot, CurrentTime);
	XRaiseWindow(dpy, focused->win);
	update_borders();
}

static void
focus_prev(void)
{
	if (!focused)
		return;

	Client *p = workspaces[current_ws], *prev = NULL;
	while (p && p != focused) {
		prev = p;
		p = p->next;
	}

	if (!prev) {
		while (p->next) p = p->next;
		focused = p;
	} else {
		focused = prev;
	}

	XSetInputFocus(dpy, focused->win, RevertToPointerRoot, CurrentTime);
	XRaiseWindow(dpy, focused->win);
	update_borders();
}

static void
grab_keys(void)
{
	KeyCode keycode;
	uint modifiers[] = { 0, LockMask, Mod2Mask, LockMask|Mod2Mask };

	/* ungrab all keys */
	XUngrabKey(dpy, AnyKey, AnyModifier, root);

	for (uint i = 0; i < LENGTH(binds); ++i) {
		if ((keycode = XKeysymToKeycode(dpy, binds[i].keysym))) {
			for (uint j = 0; j < LENGTH(modifiers); ++j) {
				XGrabKey(dpy, keycode,
						binds[i].mods | modifiers[j],
						root, True, GrabModeAsync, GrabModeAsync);
			}
		}
	}
}

static void
hdl_button(XEvent *xev)
{
	XButtonEvent *e = &xev->xbutton;
	Window w = e->subwindow;
	if (!w) return;

	Client *head = workspaces[current_ws];
	for (Client *c = head; c; c = c->next) {
		if (c->win != w)
			continue;

		if (((e->state & MOD) && e->button == Button1 && !c->floating) ||
			((e->state & MOD) && e->button == Button3 && !c->floating)) {
			focused = c;
			toggle_floating();
		}

		if (!c->floating)
			return;

		Cursor cur = (e->button == Button1) ? c_move : c_resize;
		XGrabPointer(dpy, root, True,
				ButtonReleaseMask|PointerMotionMask,
				GrabModeAsync, GrabModeAsync,
				None, cur, CurrentTime);

		drag_client		= c;
		drag_start_x	= e->x_root;
		drag_start_y	= e->y_root;
		drag_orig_x		= c->x;
		drag_orig_y		= c->y;
		drag_orig_w		= c->w;
		drag_orig_h		= c->h;
		drag_mode		= (e->button == Button1 ? DRAG_MOVE : DRAG_RESIZE);
		focused			= c;
		XSetInputFocus(dpy, w, RevertToPointerRoot, CurrentTime);
		update_borders();
		XRaiseWindow(dpy, c->win);
		return;
	}
}

static void
hdl_button_release(XEvent *xev)
{
	(void)xev;
	XUngrabPointer(dpy, CurrentTime);
	drag_mode	= DRAG_NONE;
	drag_client = NULL;
}

static void
hdl_client_msg(XEvent *xev)
{
	if (xev->xclient.message_type == atom_net_wm_state) {
		long action = xev->xclient.data.l[0];
		Atom  target = xev->xclient.data.l[1];
		if (target == atom_net_wm_state_fullscreen) {
			if (action == 1 || action == 2)
				toggle_fullscreen();
			else if (action == 0 && focused && focused->fullscreen)
				toggle_fullscreen();
		}
		return;
	}
}

static void
hdl_dummy(XEvent *xev)
{
	(void) xev;
}

static void
hdl_destroy_ntf(XEvent *xev)
{
	Window w = xev->xdestroywindow.window;

	Client *prev = NULL, *c = workspaces[current_ws];
	while (c && c->win != w) {
		prev = c;
		c = c->next;
	}
	if (c) {
		if (focused == c) {
			if (c->next)
				focused = c->next;
			else if (prev)
				focused = prev;
			else
				focused = NULL;
		}
		if (!prev)
			workspaces[current_ws] = c->next;
		else
			prev->next = c->next;
		free(c);
		update_net_client_list();
		--open_windows;
	}

	tile();
	update_borders();

	if (focused) {
		XSetInputFocus(dpy, focused->win,
				RevertToPointerRoot, CurrentTime);
		XRaiseWindow(dpy, focused->win);
	}
}

static void
hdl_enter(XEvent *xev)
{
	Window w = xev->xcrossing.window;

	Client *head = workspaces[current_ws];
	for (Client *c = head; c; c = c->next) {
		if (c->win == w) {
			focused = c;
			XSetInputFocus(dpy, w, RevertToPointerRoot, CurrentTime);
			update_borders();
			break;
		}
	}
}

static void
hdl_keypress(XEvent *xev)
{
	KeySym keysym = XLookupKeysym(&xev->xkey, 0);
	uint mods = clean_mask(xev->xkey.state);

	for (uint i = 0; i < LENGTH(binds); ++i) {
		if (keysym == binds[i].keysym && mods == clean_mask(binds[i].mods)) {
			if (binds[i].is_func)
				binds[i].action.fn();
			else
				spawn(binds[i].action.cmd);
			return;
		}
	}
}

static void
hdl_map_req(XEvent *xev)
{
	XConfigureRequestEvent *cr = &xev->xconfigurerequest;

	Atom type;
	int format;
	ulong nitems, bytes_after;
	Atom *types = NULL;

	if (XGetWindowProperty(dpy, cr->window,
				atom_wm_window_type, 0, 1, False,
				XA_ATOM, &type, &format,
				&nitems, &bytes_after,
				(unsigned char**)&types) == Success && types)
	{
		if (nitems > 0 && types[0] == atom_net_wm_window_type_dock) {
			XFree(types);

			XMapWindow(dpy, cr->window);
			ulong *strut = NULL;
			if (XGetWindowProperty(dpy, cr->window,
						atom_wm_strut_partial, 0, 12, False,
						XA_CARDINAL, &type, &format,
						&nitems, &bytes_after,
						(unsigned char**)&strut) == Success && strut)
			{
				if (nitems >= 4) {
					reserve_left	= strut[0];
					reserve_right	= strut[1];
					reserve_top		= strut[2];
					reserve_bottom	= strut[3];
				}
				XFree(strut);
			}

			ulong workarea[4] = {
				reserve_left,
				reserve_top,
				scr_width  - reserve_left	- reserve_right,
				scr_height - reserve_top	- reserve_bottom
			};
			XChangeProperty(dpy, root,
					atom_net_workarea, XA_CARDINAL, 32,
					PropModeReplace, (unsigned char*)workarea, 4);
			return;
		}
		XFree(types);
	}

	if (open_windows == MAXCLIENTS)
		return;
	add_client(cr->window);

	{
		Window transient;
		if (XGetTransientForHint(dpy, cr->window, &transient)) {
			Client *c = workspaces[current_ws];
			c->floating = True;
		}
	}

	XMapWindow(dpy, cr->window);
	update_net_client_list();
	if (!global_floating)
		tile();
	update_borders();
}

static void
hdl_motion(XEvent *xev)
{
	if (drag_mode == DRAG_NONE || !drag_client)
		return;

	XMotionEvent *e = &xev->xmotion;
	if (e->time - last_motion_time <= (1000 / MOTION_THROTTLE))
		return;
	last_motion_time = e->time;

	int dx = e->x_root - drag_start_x;
	int dy = e->y_root - drag_start_y;
	int nx = drag_orig_x + dx;
	int ny = drag_orig_y + dy;

	if (drag_mode == DRAG_MOVE) {
		int outer_w = drag_client->w + 2 * BORDER_WIDTH;
		int outer_h = drag_client->h + 2 * BORDER_WIDTH;

		if (UDIST(nx, 0) <= SNAP_DISTANCE) {
			nx = 0;
		} else if (UDIST(nx + outer_w, scr_width) <= SNAP_DISTANCE) {
			nx = scr_width - outer_w;
		}

		/* snap y */
		if (UDIST(ny, 0) <= SNAP_DISTANCE) {
			ny = 0;
		} else if (UDIST(ny + outer_h, scr_height) <= SNAP_DISTANCE) {
			ny = scr_height - outer_h;
		}

		if (!drag_client->floating &&
				(UDIST(nx, drag_client->x) > SNAP_DISTANCE ||
				 UDIST(ny, drag_client->y) > SNAP_DISTANCE)) {
			toggle_floating();
		}

		XMoveWindow(dpy, drag_client->win, nx, ny);
		drag_client->x = nx;
		drag_client->y = ny;
	}


	else {
		/* resize clamp is 20px */
		int nw = drag_orig_w + dx;
		int nh = drag_orig_h + dy;
		drag_client->w = nw < 20 ? 20 : nw;
		drag_client->h = nh < 20 ? 20 : nh;
		XResizeWindow(dpy, drag_client->win,
				drag_client->w, drag_client->h);
	}
}

static void
inc_gaps(void)
{
	if (gaps < MAXGAPS) {
		++gaps;
		tile();
		update_borders();
	}
}

static void
move_master_next(void)
{
	if (!workspaces[current_ws] || !workspaces[current_ws]->next)
		return;
	Client *first = workspaces[current_ws];
	workspaces[current_ws] = first->next;
	first->next = NULL;
	Client *tail = workspaces[current_ws];
	while (tail->next)
		tail = tail->next;
	tail->next = first;
	tile();
	update_borders();
}

static void
move_master_prev(void)
{
	if (!workspaces[current_ws] || !workspaces[current_ws]->next)
		return;
	Client *prev = NULL, *cur = workspaces[current_ws];
	while (cur->next) {
		prev = cur;
		cur	 = cur->next;
	}
	prev->next	= NULL;
	cur->next	= workspaces[current_ws];
	workspaces[current_ws]		= cur;
	tile();
	update_borders();
}

static void
move_to_workspace(uint ws)
{
	if (!focused || ws >= NUM_WORKSPACES || ws == current_ws)
		return;

	if (focused->fullscreen) {
		focused->fullscreen = False;
		XMoveResizeWindow(dpy, focused->win, focused->orig_x, focused->orig_y, focused->orig_w, focused->orig_h);
		XSetWindowBorderWidth(dpy, focused->win, BORDER_WIDTH);
	}

	/* remove from current list */
	Client **pp = &workspaces[current_ws];
	while (*pp && *pp != focused) pp = &(*pp)->next;
	if (*pp) *pp = focused->next;

	/* push to target list */
	focused->next = workspaces[ws];
	workspaces[ws] = focused;

	/* unmap it here if switching away */
	XUnmapWindow(dpy, focused->win);

	/* tile current ws */
	tile();
	focused = workspaces[current_ws];
	if (focused)
		XSetInputFocus(dpy, focused->win, RevertToPointerRoot, CurrentTime);
}

static void
other_wm(void)
{
	XSetErrorHandler(other_wm_err);
	XChangeWindowAttributes(dpy, root, CWEventMask, 
			&(XSetWindowAttributes){.event_mask = SubstructureRedirectMask});
	XSync(dpy, False);
	XSetErrorHandler(xerr);
	XChangeWindowAttributes(dpy, root, CWEventMask, 
			&(XSetWindowAttributes){.event_mask = 0});
	XSync(dpy, False);
}

static int
other_wm_err(Display *dpy, XErrorEvent *ee)
{
	errx(0, "can't start because another window manager is already running");
	return 0;
	(void) dpy;
	(void) ee;
}

static ulong
parse_col(const char *hex)
{
	XColor col;
	Colormap cmap = DefaultColormap(dpy, DefaultScreen(dpy));

	if (!XParseColor(dpy, cmap, hex, &col)) {
		fprintf(stderr, "sxwm: cannot parse color %s\n", hex);
		return WhitePixel(dpy, DefaultScreen(dpy));
	}

	if (!XAllocColor(dpy, cmap, &col)) {
		fprintf(stderr, "sxwm: cannot allocate color %s\n", hex);
		return WhitePixel(dpy, DefaultScreen(dpy));
	}

	return col.pixel;
}

static void
quit(void)
{
	for (uint ws = 0; ws < NUM_WORKSPACES; ++ws) {
		for (Client *c = workspaces[ws]; c; c = c->next) {
			XUnmapWindow(dpy, c->win);
			XKillClient(dpy, c->win);
		}
	}
	XSync(dpy, False);
	XCloseDisplay(dpy);
	errx(0, "quitting...");
}

static void
resize_master_add(void)
{
	if (master_frac < MF_MAX - 0.001f)
		master_frac += ((float) RESIZE_MASTER_AMT / 100);
	tile();
	update_borders();
}

static void
resize_master_sub(void)
{
	if (master_frac > MF_MIN + 0.001f)
		master_frac -= ((float) RESIZE_MASTER_AMT / 100);
	tile();
	update_borders();
}

static void
run(void)
{
	XEvent xev;
	for (;;) {
		XNextEvent(dpy, &xev);
		xev_case(&xev);
	}
}

static void
setup(void)
{
	if ((dpy = XOpenDisplay(NULL)) == 0)
		errx(0, "can't open display. quitting...");
	root = XDefaultRootWindow(dpy);

	setup_atoms();
	other_wm();
	grab_keys();

	c_normal = XCreateFontCursor(dpy, XC_left_ptr);
	c_move	 = XCreateFontCursor(dpy, XC_fleur);
	c_resize = XCreateFontCursor(dpy, XC_bottom_right_corner);
	XDefineCursor(dpy, root, c_normal);

	scr_width = XDisplayWidth(dpy, DefaultScreen(dpy));
	scr_height = XDisplayHeight(dpy, DefaultScreen(dpy));
	XSelectInput(dpy, root, SubstructureRedirectMask | SubstructureNotifyMask | KeyPressMask);
	XGrabButton(dpy, Button1, MOD, root,
			True, ButtonPressMask|ButtonReleaseMask|PointerMotionMask,
			GrabModeAsync, GrabModeAsync, None, None);
	XGrabButton(dpy, Button3, MOD, root,
			True, ButtonPressMask|ButtonReleaseMask|PointerMotionMask,
			GrabModeAsync, GrabModeAsync, None, None);
	XSync(dpy, False);

	for (int i = 0; i < LASTEvent; ++i)
		evtable[i] = hdl_dummy;

	evtable[ButtonPress]	= hdl_button;
	evtable[ButtonRelease]	= hdl_button_release;
	evtable[ClientMessage]	= hdl_client_msg;
	evtable[DestroyNotify]	= hdl_destroy_ntf;
	evtable[EnterNotify] 	= hdl_enter;
	evtable[KeyPress]		= hdl_keypress;
	evtable[MapRequest]		= hdl_map_req;
	evtable[MotionNotify]	= hdl_motion;

	border_foc_col = parse_col(BORDER_FOC_COL);
	border_ufoc_col = parse_col(BORDER_UFOC_COL);
}

static void
setup_atoms(void)
{
	/* bar atoms */
	atom_net_supported				= XInternAtom(dpy, "_NET_SUPPORTED",			False);
	atom_wm_strut_partial			= XInternAtom(dpy, "_NET_WM_STRUT_PARTIAL",		False);
	atom_wm_window_type				= XInternAtom(dpy, "_NET_WM_WINDOW_TYPE",		False);
	atom_net_wm_window_type_dock	= XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DOCK",	False);
	atom_net_workarea				= XInternAtom(dpy, "_NET_WORKAREA",				False);

	Atom support_list[] = {
		XInternAtom(dpy, "_NET_WM_STRUT_PARTIAL",	False),
		XInternAtom(dpy, "_NET_WM_WINDOW_TYPE",		False),
		XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DOCK",False),
		XInternAtom(dpy, "_NET_WORKAREA",			False),
	};

	XChangeProperty(dpy, root,
			atom_net_supported,
			XA_ATOM, 32, PropModeReplace,
			(unsigned char*)support_list,
			sizeof(support_list)/sizeof(Atom));

	/* workspace atoms */
	Atom a_num = XInternAtom(dpy, 	"_NET_NUMBER_OF_DESKTOPS",	False);
	Atom a_names= XInternAtom(dpy, 	"_NET_DESKTOP_NAMES",		False);

	long  num = NUM_WORKSPACES;
	XChangeProperty(dpy, root, a_num, XA_CARDINAL, 32,
			PropModeReplace, (unsigned char*)&num, 1);

	const char names[] = WORKSPACE_NAMES;
	uint names_len = sizeof(names);

	XChangeProperty(dpy, root,
			a_names,
			XInternAtom(dpy, "UTF8_STRING", False),
			8,
			PropModeReplace,
			(unsigned char*)names,
			names_len);

	ulong initial = current_ws;
	XChangeProperty(dpy, root,
			XInternAtom(dpy, "_NET_CURRENT_DESKTOP", False),
			XA_CARDINAL, 32,
			PropModeReplace,
			(unsigned char*)&initial, 1);
	/* fullscreen atoms */
	atom_net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
	atom_net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);

	/* delete atoms */
	atom_wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
}

static void
spawn(const char **cmd)
{
	pid_t pid;

	if (!cmd || !cmd[0])
		return;

	pid = fork();
	if (pid < 0) {
		errx(1, "sxwm: fork failed");
	} else if (pid == 0) {
		setsid();
		execvp(cmd[0], (char *const *)cmd);
		errx(1, "sxwm: execvp '%s' failed", cmd[0]);
	}
}

static void
tile(void)
{
	uint total_windows = 0;
	Client *head = workspaces[current_ws];
	for (Client *c = head; c; c = c->next) {
		if (c->fullscreen)
			return;

		if (!c->floating)
			++total_windows;
	}
	if (total_windows == 0)
		return;

	uint stack_count = total_windows - 1;

	uint left	= reserve_left + gaps;
	uint top	= reserve_top + gaps;
	uint right	= reserve_right + gaps;
	uint bottom	= reserve_bottom + gaps;

	uint workarea_width  = scr_width  - left - right;
	uint workarea_height = scr_height - top  - bottom;

	uint column_gap	= (stack_count > 0 ? gaps : 0);
	uint master_width = (stack_count > 0)
		? workarea_width * master_frac
		: workarea_width;
	uint stack_width  = (stack_count > 0)
		? (workarea_width - master_width - column_gap)
		: 0;

	uint master_x = left;
	uint stack_x  = left + master_width + column_gap;

	uint total_row_gaps	= (stack_count > 1 ? gaps * (stack_count - 1) : 0);
	uint row_height	= (stack_count > 0)
		? (workarea_height - total_row_gaps) / stack_count
		: 0;
	uint used_height = row_height * (stack_count > 0 ? stack_count - 1 : 0)
		+ total_row_gaps;
	uint last_row_height = (stack_count > 0)
		? (workarea_height - used_height)
		: workarea_height;

	uint index = 0;
	for (Client *c = workspaces[current_ws]; c; c = c->next) {
		if (c->floating)
			continue;

		XWindowChanges changes = { .border_width = BORDER_WIDTH };

		if (index == 0) {
			changes.x		= master_x;
			changes.y		= top;
			changes.width	= master_width  - 2*BORDER_WIDTH;
			changes.height	= workarea_height - 2*BORDER_WIDTH;
		} else {
			uint row	= index - 1;
			uint y_pos	= top + row * (row_height + gaps);
			uint height	= (row < stack_count - 1)
				? row_height
				: last_row_height;

			changes.x		= stack_x;
			changes.y		= y_pos;
			changes.width	= stack_width - 2*BORDER_WIDTH;
			changes.height	= height - 2*BORDER_WIDTH;
		}

		XSetWindowBorder(dpy, c->win,
				(index == 0 ? border_foc_col : border_ufoc_col));
		XConfigureWindow(dpy, c->win,
				CWX|CWY|CWWidth|CWHeight|CWBorderWidth,
				&changes);
		++index;
	}
}

static void
toggle_floating(void)
{
	if (!focused) return;

	focused->floating = !focused->floating;
	if (focused->fullscreen) {
		focused->fullscreen = False;
		tile();
		XSetWindowBorderWidth(dpy, focused->win, BORDER_WIDTH);
	}

	if (focused->floating) {
		XWindowAttributes wa;
		XGetWindowAttributes(dpy, focused->win, &wa);
		focused->x = wa.x;
		focused->y = wa.y;
		focused->w = wa.width;
		focused->h = wa.height;

		XConfigureWindow(dpy, focused->win,
				CWX|CWY|CWWidth|CWHeight,
				&(XWindowChanges){
				.x = focused->x,
				.y = focused->y,
				.width	= focused->w,
				.height = focused->h
				});
	}

	tile();
	update_borders();

	/* floating windows are on top */
	if (focused->floating) {
		XRaiseWindow(dpy, focused->win);
		XSetInputFocus(dpy, focused->win,
				RevertToPointerRoot, CurrentTime);
	}
}

static void
toggle_floating_global(void)
{
	global_floating = !global_floating;
	Bool any_tiled = False;
	for (Client *c = workspaces[current_ws]; c; c = c->next) {
		if (!c->floating) {
			any_tiled = True;
			break;
		}
	}

	for (Client *c = workspaces[current_ws]; c; c = c->next) {
		c->floating = any_tiled;
		if (c->floating) {
			XWindowAttributes wa;
			XGetWindowAttributes(dpy, c->win, &wa);
			c->x = wa.x;
			c->y = wa.y;
			c->w = wa.width;
			c->h = wa.height;

			XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight,
					&(XWindowChanges){
					.x		= c->x,
					.y		= c->y,
					.width	= c->w,
					.height	= c->h
					}
					);
			XRaiseWindow(dpy, c->win);
		}
	}

	tile();
	update_borders();
}

static void
toggle_fullscreen(void)
{
	if (!focused)
		return;

	if (focused->floating)
		focused->floating = False;

	focused->fullscreen = !focused->fullscreen;

	if (focused->fullscreen) {
		XWindowAttributes wa;
		XGetWindowAttributes(dpy, focused->win, &wa);
		focused->orig_x = wa.x;
		focused->orig_y = wa.y;
		focused->orig_w = wa.width;
		focused->orig_h = wa.height;

		uint fs_x = 0;
		uint fs_y = 0;
		uint fs_w = scr_width;
		uint fs_h = scr_height;

		XSetWindowBorderWidth(dpy, focused->win, 0);
		XMoveResizeWindow(dpy, focused->win, fs_x, fs_y, fs_w, fs_h);
		XRaiseWindow(dpy, focused->win);
	} else {
		XMoveResizeWindow(dpy, focused->win, focused->orig_x, focused->orig_y, focused->orig_w, focused->orig_h);
		XSetWindowBorderWidth(dpy, focused->win, BORDER_WIDTH);
		tile();
		update_borders();
	}
}

static void
update_borders(void)
{
	for (Client *c = workspaces[current_ws]; c; c = c->next) {
		XSetWindowBorder(dpy, c->win,
				(c == focused ? border_foc_col : border_ufoc_col));
	}
}

static void
update_net_client_list(void)
{
	Window wins[MAXCLIENTS];
	int n = 0;
	for (int ws = 0; ws < NUM_WORKSPACES; ++ws) {
		for (Client *c = workspaces[ws]; c; c = c->next) {
			wins[n++] = c->win; /* has to be n++ or well get an off by one error i think */
		}
	}
	Atom prop = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
	XChangeProperty(dpy, root, prop,
			XA_WINDOW, 32, PropModeReplace,
			(unsigned char*)wins, n);
}

static void
change_workspace(uint ws)
{
	if (ws >= NUM_WORKSPACES || ws == current_ws)
		return;

	/* unmap old desktop */
	for (Client *c = workspaces[current_ws]; c; c=c->next)
		XUnmapWindow(dpy, c->win);

	current_ws = ws;

	/* map new desktop */
	for (Client *c = workspaces[current_ws]; c; c=c->next)
		XMapWindow(dpy, c->win);

	/* retile & refocus */
	tile();
	if (workspaces[current_ws]) {
		focused = workspaces[current_ws];
		XSetInputFocus(dpy, focused->win, RevertToPointerRoot, CurrentTime);
	}

	/* update atom */
	ulong cd = current_ws;
	XChangeProperty(dpy, root,
			XInternAtom(dpy, "_NET_CURRENT_DESKTOP", False),
			XA_CARDINAL, 32, PropModeReplace,
			(u_char*)&cd, 1);
}

static int
xerr(Display *dpy, XErrorEvent *ee)
{
	/* ignore noise and non fatal errors */
	static const struct {
		uint req, code;
	} ignore[] = {
		{ 0, BadWindow },
		{ X_GetGeometry, BadDrawable },
		{ X_SetInputFocus, BadMatch },
		{ X_ConfigureWindow, BadMatch },
	};

	for (size_t i = 0; i < sizeof(ignore)/sizeof(ignore[0]); ++i)
		if ((ignore[i].req == 0 || ignore[i].req  == ee->request_code) &&
				(ignore[i].code == ee->error_code))
			return 0;

	return 0;
	(void) dpy;
	(void) ee;
}

static void
xev_case(XEvent *xev)
{
	if (xev->type >= 0 && xev->type < LASTEvent)
		evtable[xev->type](xev);
	else
		printf("sxwm: invalid event type: %d\n", xev->type);
}

int
main(int ac, char **av)
{
	if (ac > 1) {
		if (strcmp(av[1], "-v") == 0 || strcmp(av[1], "--version") == 0)
			errx(0, "%s\n%s\n%s", SXWM_VERSION, SXWM_AUTHOR, SXWM_LICINFO);
		else
			errx(0, "usage:\n[-v || --version]: See the version of sxwm");
	}
	setup();
	run();
	return 0;
}