-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatlas.c
363 lines (320 loc) · 11.7 KB
/
atlas.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
#include <X11/Xatom.h>
#include <X11/Xft/Xft.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/extensions/Xinerama.h>
#include <X11/keysym.h>
#include <errno.h>
#include <locale.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "atlas.h"
#include "config.h"
#include "ipc.h"
#include "util.h"
/* variables */
void (*eventHandlers[LASTEvent])(XEvent *) = {
[ButtonPress] = handleMouseButtonPress,
[ClientMessage] = handleClientMessage,
[ConfigureRequest] = handleConfigureRequest,
[ConfigureNotify] = handleWindowConfigChange,
[DestroyNotify] = handleWindowDestroy,
[EnterNotify] = handleMouseEnter,
[FocusIn] = handleFocusIn,
[KeyPress] = handleKeypress,
[MappingNotify] = handleKeymappingChange,
[MapRequest] = handleWindowMappingRequest,
[MotionNotify] = handleMouseMotion,
[PropertyNotify] = handlePropertyChange,
[UnmapNotify] = handleWindowUnmap};
DrawContext *drawContext;
Atom wmAtoms[WM_ATOM_COUNT], netAtoms[NET_ATOM_COUNT];
Window root, wmCheckWindow;
CursorWrapper *cursor[CURSOR_COUNT];
int isWMRunning = 1;
Display *display;
Monitor *monitors, *selectedMonitor;
int screen, screenWidth, screenHeight;
unsigned int numLockMask = 0;
// Main Functions
static int (*defaultXErrorHandler)(Display *, XErrorEvent *);
static void checkForOtherWM(void);
static void cleanupWindowManager(void);
static void runWindowManager(void);
static void scan(void);
static void setupSignalHandlers(void);
static void initAtoms(void);
static void initCursors(void);
static void initWMCheck(void);
static void initWindowManager(void);
int handleXError(Display *dpy, XErrorEvent *ee);
int handleXErrorDummy(Display *dpy, XErrorEvent *ee);
int handleXErrorStart(Display *dpy, XErrorEvent *ee);
void checkForOtherWM(void) {
defaultXErrorHandler = XSetErrorHandler(handleXErrorStart);
/* this causes an error if some other window manager is running */
XSelectInput(display, DefaultRootWindow(display), SubstructureRedirectMask);
XSync(display, False);
XSetErrorHandler(handleXError);
XSync(display, False);
}
void cleanupWindowManager(void) {
Arg a = {.ui = ~0};
Layout foo = {"", NULL};
Monitor *m;
size_t i;
viewWorkspace(&a);
selectedMonitor->layouts[selectedMonitor->selectedLayout] = &foo;
for (m = monitors; m; m = m->next)
while (m->stack)
unmanage(m->stack, 0);
XUngrabKey(display, AnyKey, AnyModifier, root);
while (monitors)
cleanupMonitor(monitors);
for (i = 0; i < CURSOR_COUNT; i++)
drw_cur_free(drawContext, cursor[i]);
XDestroyWindow(display, wmCheckWindow);
drw_free(drawContext);
XSync(display, False);
XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
XDeleteProperty(display, root, netAtoms[NET_ACTIVE_WINDOW]);
}
void runWindowManager(void) {
XEvent ev;
XSync(display, False);
while (isWMRunning && !XNextEvent(display, &ev))
if (eventHandlers[ev.type])
eventHandlers[ev.type](&ev); /* call handler */
}
void scan(void) {
unsigned int i, num;
Window d1, d2, *wins = NULL;
XWindowAttributes wa;
if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
for (i = 0; i < num; i++) {
if (!XGetWindowAttributes(display, wins[i], &wa) ||
wa.override_redirect || XGetTransientForHint(display, wins[i], &d1))
continue;
if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
manage(wins[i], &wa);
}
for (i = 0; i < num; i++) { /* now the transients */
if (!XGetWindowAttributes(display, wins[i], &wa))
continue;
if (XGetTransientForHint(display, wins[i], &d1) &&
(wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
manage(wins[i], &wa);
}
if (wins)
XFree(wins);
}
}
void startupPrograms(void) {
if (!cfg.startup_progs || cfg.startup_prog_count == 0) {
return;
}
for (int i = 0; i < cfg.startup_prog_count; i++) {
StartupProgram *prog = &cfg.startup_progs[i];
if (!prog->command || !prog->args) {
LOG_ERROR("Invalid startup program at index %d", i);
continue;
}
pid_t pid = fork();
if (pid == -1) {
LOG_ERROR("Failed to fork for '%s': %s", prog->command, strerror(errno));
continue;
}
if (pid == 0) { // Child process
// Close X connection in child
if (display) {
close(ConnectionNumber(display));
}
// Create new session
if (setsid() == -1) {
LOG_ERROR("setsid failed for '%s': %s", prog->command, strerror(errno));
exit(EXIT_FAILURE);
}
// Execute the program
execvp(prog->command, prog->args);
// If we get here, execvp failed
LOG_ERROR("Failed to execute '%s': %s", prog->command, strerror(errno));
exit(EXIT_FAILURE);
}
// Parent process
LOG_INFO("Started program: %s (pid: %d)", prog->command, pid);
}
}
void setupSignalHandlers(void) {
struct sigaction sa;
/* do not transform children into zombies when they terminate */
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART;
sa.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &sa, NULL);
/* clean up any zombies (inherited from .xinitrc etc) immediately */
while (waitpid(-1, NULL, WNOHANG) > 0)
;
}
void initAtoms(void) {
wmAtoms[WM_PROTOCOLS] = XInternAtom(display, "WM_PROTOCOLS", False);
wmAtoms[WM_DELETE] = XInternAtom(display, "WM_DELETE_WINDOW", False);
wmAtoms[WM_STATE] = XInternAtom(display, "WM_STATE", False);
wmAtoms[WM_TAKE_FOCUS] = XInternAtom(display, "WM_TAKE_FOCUS", False);
netAtoms[NET_ACTIVE_WINDOW] =
XInternAtom(display, "_NET_ACTIVE_WINDOW", False);
netAtoms[NET_SUPPORTED] = XInternAtom(display, "_NET_SUPPORTED", False);
netAtoms[NET_WM_NAME] = XInternAtom(display, "_NET_WM_NAME", False);
netAtoms[NET_WM_STATE] = XInternAtom(display, "_NET_WM_STATE", False);
netAtoms[NET_WM_CHECK] =
XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
netAtoms[NET_WM_FULLSCREEN] =
XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", False);
netAtoms[NET_WM_WINDOW_TYPE] =
XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
netAtoms[NET_WM_WINDOW_TYPE_DIALOG] =
XInternAtom(display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
netAtoms[NET_CLIENT_LIST] = XInternAtom(display, "_NET_CLIENT_LIST", False);
netAtoms[NET_DESKTOP_VIEWPORT] =
XInternAtom(display, "_NET_DESKTOP_VIEWPORT", False);
netAtoms[NET_NUMBER_OF_DESKTOPS] =
XInternAtom(display, "_NET_NUMBER_OF_DESKTOPS", False);
netAtoms[NET_CURRENT_DESKTOP] =
XInternAtom(display, "_NET_CURRENT_DESKTOP", False);
netAtoms[NET_DESKTOP_NAMES] =
XInternAtom(display, "_NET_DESKTOP_NAMES", False);
}
void initCursors(void) {
cursor[CURSOR_NORMAL] = drw_cur_create(drawContext, XC_left_ptr);
cursor[CURSOR_RESIZE] = drw_cur_create(drawContext, XC_sizing);
cursor[CURSOR_MOVE] = drw_cur_create(drawContext, XC_fleur);
}
void initWMCheck(void) {
Atom utf8string;
Window check = XCreateSimpleWindow(display, root, 0, 0, 1, 1, 0, 0, 0);
utf8string = XInternAtom(display, "UTF8_STRING", False);
XChangeProperty(display, check, netAtoms[NET_WM_NAME], utf8string, 8,
PropModeReplace, (unsigned char *)"AtlasWM", 7);
XChangeProperty(display, check, netAtoms[NET_WM_CHECK], XA_WINDOW, 32,
PropModeReplace, (unsigned char *)&check, 1);
XChangeProperty(display, root, netAtoms[NET_WM_CHECK], XA_WINDOW, 32,
PropModeReplace, (unsigned char *)&check, 1);
}
void initWindowManager(void) {
XSetWindowAttributes wa;
// Load configuration
char config_path[256];
char *home = getenv("HOME");
if (home) {
snprintf(config_path, sizeof(config_path), "%s/.config/atlaswm/config.toml",
home);
if (load_config(config_path)) {
LOG_INFO("Configuration loaded successfully");
} else {
LOG_WARN("Failed to load config file, using defaults");
}
} else {
LOG_WARN("Could not get HOME directory, using default configuration");
}
setupSignalHandlers();
screen = DefaultScreen(display);
screenWidth = DisplayWidth(display, screen);
screenHeight = DisplayHeight(display, screen);
root = RootWindow(display, screen);
drawContext = drw_create(display, screen, root, screenWidth, screenHeight);
updateMonitorGeometry();
initAtoms();
initCursors();
initWMCheck();
setup_ipc(display);
XChangeProperty(display, root, netAtoms[NET_SUPPORTED], XA_ATOM, 32,
PropModeReplace, (unsigned char *)netAtoms, NET_ATOM_COUNT);
// Initialize monitor workspaces
setNumDesktops();
setCurrentDesktop();
setDesktopNames();
setViewport();
XDeleteProperty(display, root, netAtoms[NET_CLIENT_LIST]);
// Setup root window event mask
wa.cursor = cursor[CURSOR_NORMAL]->cursor;
wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask |
ButtonPressMask | PointerMotionMask | EnterWindowMask |
LeaveWindowMask | StructureNotifyMask | PropertyChangeMask;
XChangeWindowAttributes(display, root, CWEventMask | CWCursor, &wa);
XSelectInput(display, root, wa.event_mask);
// Register keyboard shortcuts
registerKeyboardShortcuts();
Monitor *m;
for (m = monitors; m; m = m->next) {
m->workspaceset[0] = m->workspaceset[1] = 1;
}
focus(NULL);
startupPrograms();
}
/* There's no way to check accesses to destroyed windows, thus those cases are
* ignored (especially on UnmapNotify's). Other types of errors call Xlibs
* default error handler, which may call exit. */
int handleXError(Display *dpy, XErrorEvent *ee) {
if (ee->error_code == BadWindow ||
(ee->request_code == X_SetInputFocus && ee->error_code == BadMatch) ||
(ee->request_code == X_PolyText8 && ee->error_code == BadDrawable) ||
(ee->request_code == X_PolyFillRectangle &&
ee->error_code == BadDrawable) ||
(ee->request_code == X_PolySegment && ee->error_code == BadDrawable) ||
(ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch) ||
(ee->request_code == X_GrabButton && ee->error_code == BadAccess) ||
(ee->request_code == X_GrabKey && ee->error_code == BadAccess) ||
(ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
return 0;
char error_text[1024];
XGetErrorText(dpy, ee->error_code, error_text, sizeof(error_text));
LOG_ERROR("X Error: request=%d error=%d (%s) resourceid=%lu serial=%lu",
ee->request_code, ee->error_code, error_text, ee->resourceid,
ee->serial);
return defaultXErrorHandler(dpy, ee);
}
int handleXErrorDummy(Display *dpy, XErrorEvent *ee) { return 0; }
int handleXErrorStart(Display *dpy, XErrorEvent *ee) {
LOG_FATAL("Another window manager is already running");
return -1;
}
int main(int argc, char *argv[]) {
if (argc == 2) {
if (!strcmp("-v", argv[1])) {
die("atlaswm-" VERSION);
} else if (strcmp(argv[1], "reload") == 0) {
Display *d = XOpenDisplay(NULL);
if (!d) {
LOG_ERROR("Cannot open display");
return 1;
}
int success = send_command(d, CMD_RELOAD);
XCloseDisplay(d);
return success ? 0 : 1;
} else {
die("Usage: atlaswm [-v|reload]");
}
} else if (argc != 1)
die("Usage: atlaswm [-v]");
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
LOG_FATAL("No locale support");
if (!(display = XOpenDisplay(NULL)))
LOG_FATAL("Failed to open display");
checkForOtherWM();
LOG_INFO("AtlasWM starting");
initWindowManager();
LOG_INFO("AtlasWM setup complete");
scan();
runWindowManager();
LOG_INFO("AtlasWM is exiting");
cleanupWindowManager();
XCloseDisplay(display);
return EXIT_SUCCESS;
}