summaryrefslogtreecommitdiff
path: root/src/sxwm.c
blob: 2aaf2056d29b7b397a3281243adc023a83c02eec (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
/*  See LICENSE for more info
 *
 *	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/Xlib.h>
#include <X11/XKBlib.h>

#include "defs.h"

static void add_client(Window w);
static uint clean_mask(uint mask);
static void close_focused(void);
static void focus_next(void);
static void focus_prev(void);
static void grab_keys(void);
static void hdl_button(XEvent *);
static void hdl_button_release(XEvent *);
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 *);
static void other_wm(void);
static int other_wm_err(Display *dpy, XErrorEvent *ee);
static uint64_t parse_col(const char *hex);
static void quit(void);
static void run(void);
static void setup(void);
static void spawn(const char **cmd);
static void tile(void);
static void toggle_floating(void);
static void update_borders(void);
static int xerr(Display *dpy, XErrorEvent *ee);
static void xev_case(XEvent *xev);

static Cursor c_normal, c_move, c_resize;
static Client *clients = NULL;
static Client *drag_client = NULL;
static Client *focused = NULL;
static EventHandler evtable[LASTEvent];
static Display *dpy;
static Window root;

static uint64_t border_foc_col;
static uint64_t border_ufoc_col;
static uint scr_width;
static uint scr_height;
static int drag_start_x, drag_start_y;
static int drag_orig_x, drag_orig_y, drag_orig_w, drag_orig_h;

static uint open_windows = 0;
#include "usercfg.h"

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 = clients;
	clients = c;
	if (!focused)
		focused = c;
	++open_windows;

	XSelectInput(dpy, w,
			EnterWindowMask | LeaveWindowMask |
			FocusChangeMask | PropertyChangeMask |
			StructureNotifyMask);

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

	XRaiseWindow(dpy, w);
}

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

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

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

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

	focused = (focused->next ? focused->next : clients);
	XSetInputFocus(dpy, focused->win, RevertToPointerRoot, CurrentTime);
	XRaiseWindow(dpy, focused->win);
	update_borders();
}

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

	Client *p = clients, *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;

	// find the Client*
	for (Client *c = clients; c; c = c->next) {
		if (c->win == w && c->floating) {

			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();
			return;
		}
	}
}

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


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

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

	Client *prev = NULL, *c = clients;
	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)
			clients = c->next;
		else
			prev->next = c->next;
		free(c);
		--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;

	for (Client *c = clients; 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)
{
	if (open_windows == MAXCLIENTS)
		return;

	XConfigureRequestEvent *cr = &xev->xconfigurerequest;
	add_client(cr->window);
	XMapWindow(dpy, cr->window);

	tile();
	update_borders();
}

static void
hdl_motion(XEvent *xe)
{
	if (drag_mode == DRAG_NONE || !drag_client) return;
	XMotionEvent *e = &xe->xmotion;
	int dx = e->x_root - drag_start_x;
	int dy = e->y_root - drag_start_y;

	if (drag_mode == DRAG_MOVE) {
		drag_client->x = drag_orig_x + dx;
		drag_client->y = drag_orig_y + dy;
		XMoveWindow(dpy, drag_client->win,
				drag_client->x, drag_client->y);
	} else { // drag resize
		int nw = drag_orig_w + dx;
		int nh = drag_orig_h + dy;
		if (nw < 20) nw = 20;
		if (nh < 20) nh = 20;
		drag_client->w = nw;
		drag_client->h = nh;
		XResizeWindow(dpy, drag_client->win,
				drag_client->w, drag_client->h);
	}
}

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;
	if (dpy && ee) return 0;
}

static uint64_t
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 (Client *c = clients; c; c = c->next) {
		XUnmapWindow(dpy, c->win);
		XKillClient(dpy, c->win);
	}
	XSync(dpy, False);
	XCloseDisplay(dpy);
	errx(0, "quitting...");
}

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);

	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);

	other_wm();
	grab_keys();

	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);

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

	evtable[ButtonPress]	= hdl_button;
	evtable[ButtonRelease]	= hdl_button_release;
	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
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)
{
	if (!open_windows)
		return;

	int masterx = GAPS + BORDER_WIDTH,
		mastery = GAPS + BORDER_WIDTH,
		availableh = scr_height - (GAPS * 2),
		masterw, masterh, stackw = 0, stackwinh = 0,
		stack_count = open_windows - 1;

	if (open_windows == 1) {
		masterw = scr_width - (GAPS * 2 + BORDER_WIDTH * 2);
		masterh = availableh - (BORDER_WIDTH * 2);
	} else {
		int total_gapsw = GAPS * 4, total_bordersw = BORDER_WIDTH * 4;
		masterw = (scr_width - total_gapsw - total_bordersw) / 2;
		stackw = masterw;
		masterh = availableh - (BORDER_WIDTH * 2);

		int total_gapsh = (stack_count > 0 ? GAPS * (stack_count - 1) : 0),
			total_bordersh = BORDER_WIDTH * 2 * stack_count,
			total_stackh = availableh - total_gapsh - total_bordersh;
		stackwinh = (stack_count > 0 ? total_stackh / stack_count : 0);
	}

	int stackx = masterx + masterw + GAPS + (BORDER_WIDTH * 2),
		stacky = GAPS;
	Client *c = clients;
	uint i = 0;

	for (; c; c = c->next, ++i) {
		if (c->floating)
			continue;

		XWindowChanges changes = { .border_width = BORDER_WIDTH };
		if (i == 0) {
			changes.x = masterx;
			changes.y = mastery;
			changes.width = masterw;
			changes.height = masterh;
		} else {
			changes.x = stackx;
			changes.y = stacky + BORDER_WIDTH; // adjust for border
			changes.width = stackw;
			changes.height = stackwinh;
			if (i == open_windows - 1) {
				int used = stacky - GAPS + stackwinh + (BORDER_WIDTH * 2);
				changes.height += (availableh - used);
			}
			stacky += stackwinh + (BORDER_WIDTH * 2) + GAPS;
		}
		XSetWindowBorder(dpy, c->win,
				i == 0 ? border_foc_col : border_ufoc_col);

		XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &changes);
	}
}

static void
toggle_floating(void)
{
	if (!focused) return;
	focused->floating = !focused->floating;

	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
update_borders(void)
{
	for (Client *c = clients; c; c = c->next) {
		XSetWindowBorder(dpy, c->win,
				(c == focused ? border_foc_col : border_ufoc_col));
	}
	XSync(dpy, False);
}

static int
xerr(Display *dpy, XErrorEvent *ee)
{
	fprintf(stderr, "sxwm: fatal error\nrequest code:%d\nerror code:%d\n",
			ee->request_code, ee->error_code);
	return 0;
	if (dpy && ee) return 0;
}

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;
}