summaryrefslogtreecommitdiff
path: root/src/sxwm.c
blob: 491d6b7acf17b9f6d6892c76de7a28f68ad9ae91 (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
/*  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 <err.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

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

#include "defs.h"

typedef void (*EventHandler)(XEvent *);

static void hdl_dummy(XEvent *xev);
static void hdl_config_req(XEvent *xev);
static void hdl_destroy_ntf(XEvent *xev);
static void hdl_keypress(XEvent *xev);
static void hdl_map_req(XEvent *xev);
static void hdl_unmap_ntf(XEvent *xev);
static void other_wm(void);
static int other_wm_err(Display *dpy, XErrorEvent *ee);
static unsigned long parse_col(const char *hex);
static void quit(void);
static void run(void);
static void setup(void);
static void spawn(const char **cmd);
static int xerr(Display *dpy, XErrorEvent *ee);
static void xev_case(XEvent *xev);

static Client clients[MAXCLIENTS] = {0};
static EventHandler evtable[LASTEvent];
static Display 	*dpy;
static Window	root;

static unsigned long border_foc_col;
static unsigned long border_ufoc_col;


#include "usercfg.h"

static void
hdl_dummy(XEvent *xev)
{}

static void
hdl_config_req(XEvent *xev)
{
	XConfigureRequestEvent ev = xev->xconfigurerequest;
	XWindowChanges wc;
	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(dpy, ev.window, ev.value_mask, &wc);
	printf("sxwm: window configured: %ld\n", ev.window);
}


static void
hdl_destroy_ntf(XEvent *xev)
{
	XDestroyWindowEvent ev = xev->xdestroywindow;
	XDestroyWindow(dpy, ev.window);
	printf("sxwm: window destroyed: %ld\n", ev.window);
}

static void
hdl_keypress(XEvent *xev)
{
	KeySym keysym;
	XKeyEvent *ev = &xev->xkey;
	unsigned int modifiers;

	modifiers = ev->state;
	keysym = XkbKeycodeToKeysym(dpy, ev->keycode, 0, 0);

	int lenbindings = sizeof(binds) / sizeof(binds[0]);
	for (int i = 0; i < lenbindings; ++i) {
		if (keysym == binds[i].keysym && modifiers == 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)
{
	XMapRequestEvent ev = xev->xmaprequest;
	XSetWindowBorder(dpy, ev.window, border_foc_col);
	XSetWindowBorderWidth(dpy, ev.window, BORDER_WIDTH);
	XMapWindow(dpy, ev.window);

	XWindowAttributes wa;
	if (!XGetWindowAttributes(dpy, ev.window, &wa))
		return;

	printf("sxwm: window mapped: %ld\n", ev.window);
}

static void
hdl_unmap_ntf(XEvent *xev)
{
	XUnmapEvent ev = xev->xunmap;
	XUnmapWindow(dpy, ev.window);
	printf("sxwm: window unmapped: %ld\n", ev.window);
}

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

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

static unsigned long
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)
{
	errx(0, "sxwm: quitting...");
}

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

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

	root = XDefaultRootWindow(dpy);
	other_wm();
	XSelectInput(dpy, root,
		SubstructureRedirectMask | KeyPressMask | KeyReleaseMask
	);

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

	evtable[ConfigureRequest] = hdl_config_req;
	evtable[DestroyNotify] = hdl_destroy_ntf;
	evtable[KeyPress] = hdl_keypress;
	evtable[MapRequest] = hdl_map_req;
	evtable[UnmapNotify] = hdl_unmap_ntf;

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

static void
spawn(const char **cmd)
{
	if (!cmd) return;
	printf("sxwm: attempting to spawn: %s\n", cmd[0]);

	pid_t pid = fork();
	if (pid == 0) {
		if (dpy)
			close(ConnectionNumber(dpy));
		setsid();
		execvp(cmd[0], (char *const*)cmd);
		errx(1, "sxwm: execvp '%s' failed\n", cmd[0]);
	} else if (pid < 0) {
		fprintf(stderr, "sxwm: failed to fork process\n");
	}
}

static int
xerr(Display *dpy, XErrorEvent *ee)
{
	fprintf(stderr, "sxwm: fatal error\nrequest code:%d\nerror code:%d",
			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;
}