summaryrefslogtreecommitdiff
path: root/src/sxwm.c
blob: c8d50ba53eb04b6d6f0c4a115671cdfbb3c9f020 (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
/*  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_keypress(XEvent *xev);
static void other_wm(void);
static int other_wm_err(Display *dpy, XErrorEvent *ee);
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;

#include "usercfg.h"

static void
hdl_dummy(XEvent *xev){}

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
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 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[KeyPress] = hdl_keypress;
}

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