/
system.c
410 lines (339 loc) · 13.6 KB
/
system.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
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
//----------------------------------------------------------------------------------------------------------------------
// Module : system
// Copyright : (c) Julian Bouzas 2014
// License : BSD3-style (see LICENSE)
// Maintainer : Julian Bouzas - nnoell3[at]gmail.com
// Stability : stable
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
// PREPROCESSOR
//----------------------------------------------------------------------------------------------------------------------
// Includes
#include "system.h"
#include "config.h"
//----------------------------------------------------------------------------------------------------------------------
// PRIVATE VARIABLE DEFINITION
//----------------------------------------------------------------------------------------------------------------------
// Main variables
static Display *display_;
static int screen_;
static Window root_;
static NeuroRectangle screen_region_;
static NeuroRectangle hidden_region_;
static const int hidden_gaps_[ 4 ] = { 0 };
static Cursor cursors_[ NEURO_SYSTEM_CURSOR_END ];
static Atom wm_atoms_[ NEURO_SYSTEM_WMATOM_END ];
static Atom net_atoms_[ NEURO_SYSTEM_NETATOM_END ];
static NeuroColor colors_[ NEURO_SYSTEM_COLOR_END ];
// Recompile command
static char recompile_cmd_output_[ NEURO_NAME_SIZE_MAX ];
static char recompile_cmd_source_[ NEURO_NAME_SIZE_MAX ];
static const char *const recompile_cmd_[] = {
"/usr/bin/cc",
"-fpic",
"-O3",
"-o",
recompile_cmd_output_,
recompile_cmd_source_,
"-L/usr/lib/neuro",
"-l" PKG_NAME,
"-lX11",
"-lXrandr",
"-pthread",
NULL
};
//----------------------------------------------------------------------------------------------------------------------
// PRIVATE FUNCTION DEFINITION
//----------------------------------------------------------------------------------------------------------------------
// Starting error handler, used to check if another window manager is already running
static int xerror_handler_start(Display *d, XErrorEvent *ee) {
(void)d;
(void)ee;
NeuroSystemError("xerror_handler_start - Another window manager is already running");
return -1;
}
// Default error handler, which might call exit
static int xerror_handler(Display *d, XErrorEvent *ee) {
(void)d;
(void)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)
|| (ee->request_code == X_KillClient && ee->error_code == BadValue))
return 0;
printf("error: Request code=%d, error code=%d\n", ee->request_code, ee->error_code);
return -1;
}
static bool set_colors_cursors_atoms(void) {
if (!NeuroConfigGet()->normal_border_color || !NeuroConfigGet()->current_border_color ||
!NeuroConfigGet()->old_border_color || !NeuroConfigGet()->free_border_color ||
!NeuroConfigGet()->urgent_border_color)
return false;
// Colors
colors_[ NEURO_SYSTEM_COLOR_NORMAL ] = NeuroSystemGetColorFromHex(NeuroConfigGet()->normal_border_color);
colors_[ NEURO_SYSTEM_COLOR_CURRENT ] = NeuroSystemGetColorFromHex(NeuroConfigGet()->current_border_color);
colors_[ NEURO_SYSTEM_COLOR_OLD ] = NeuroSystemGetColorFromHex(NeuroConfigGet()->old_border_color);
colors_[ NEURO_SYSTEM_COLOR_FREE ] = NeuroSystemGetColorFromHex(NeuroConfigGet()->free_border_color);
colors_[ NEURO_SYSTEM_COLOR_URGENT ] = NeuroSystemGetColorFromHex(NeuroConfigGet()->urgent_border_color);
// Cursors
cursors_[ NEURO_SYSTEM_CURSOR_NORMAL ] = XCreateFontCursor(display_, XC_left_ptr);
cursors_[ NEURO_SYSTEM_CURSOR_RESIZE ] = XCreateFontCursor(display_, XC_bottom_right_corner);
cursors_[ NEURO_SYSTEM_CURSOR_MOVE ] = XCreateFontCursor(display_, XC_fleur);
// WM Atoms
wm_atoms_[ NEURO_SYSTEM_WMATOM_PROTOCOLS ] = XInternAtom(display_, "WM_PROTOCOLS", false);
wm_atoms_[ NEURO_SYSTEM_WMATOM_DELETEWINDOW ] = XInternAtom(display_, "WM_DELETE_WINDOW", false);
// Net Atoms
net_atoms_[ NEURO_SYSTEM_NETATOM_SUPPORTED ] = XInternAtom(display_, "_NET_SUPPORTED", false);
net_atoms_[ NEURO_SYSTEM_NETATOM_STATE ] = XInternAtom(display_, "_NET_WM_STATE", false);
net_atoms_[ NEURO_SYSTEM_NETATOM_NAME ] = XInternAtom(display_, "_NET_WM_NAME", false);
net_atoms_[ NEURO_SYSTEM_NETATOM_ACTIVE ] = XInternAtom(display_, "_NET_ACTIVE_WINDOW", false);
net_atoms_[ NEURO_SYSTEM_NETATOM_FULLSCREEN ] = XInternAtom(display_, "_NET_WM_STATE_FULLSCREEN", false);
net_atoms_[ NEURO_SYSTEM_NETATOM_STRUT ] = XInternAtom(display_, "_NET_WM_STRUT", false);
net_atoms_[ NEURO_SYSTEM_NETATOM_CLOSEWINDOW ] = XInternAtom(display_, "_NET_CLOSE_WINDOW", false);
// EWMH support per view
XChangeProperty(display_, root_, net_atoms_[ NEURO_SYSTEM_NETATOM_SUPPORTED ], XA_ATOM, 32, PropModeReplace,
(unsigned char *)net_atoms_, NEURO_SYSTEM_NETATOM_END);
return true;
}
// Note: This helper function will always exit
static void run_command(const char *const *cmd) {
assert(cmd);
assert(*cmd);
// Create a new command in order to avoid const correctness issues in execvp 2nd parameter
const NeuroIndex size = NeuroTypeArrayLength((const void *const *)cmd) + 1; // We need an extra slot for NULL
char **command = (char **)calloc(size, sizeof(void *));
if (!command)
NeuroSystemError("NeuroSystemSpawn - Could not calloc");
// Copy the data from cmd
command[ size - 1 ] = NULL;
NeuroIndex i = 0U;
for ( ; cmd[ i ]; ++i) {
const char *const src = cmd[ i ];
const NeuroIndex n = strlen(src);
command[ i ] = (char *)calloc(n, sizeof(char));
if (!command[ i ])
NeuroSystemError("NeuroSystemSpawn - Could not calloc");
strncpy(command[ i ], src, n);
}
// execute the command
execvp(command[ 0 ], command);
// If execvp failed, release command and terminate child process
for (i = 0U; command[ i ]; ++i) {
free(command[ i ]);
command[ i ] = NULL;
}
free(command);
command = NULL;
NeuroSystemError("NeuroSystemSpawn - Could not execvp");
}
//----------------------------------------------------------------------------------------------------------------------
// PUBLIC FUNCTION DEFINITION
//----------------------------------------------------------------------------------------------------------------------
// X functions
bool NeuroSystemInit(void) {
// WM global variables
display_ = XOpenDisplay(NULL);
if (!display_)
return false;
screen_ = DefaultScreen(display_);
root_ = RootWindow(display_, screen_);
// Get the regions
const int width = XDisplayWidth(display_, screen_);
const int height = XDisplayHeight(display_, screen_);
screen_region_ = (NeuroRectangle){ (NeuroPoint){ 0, 0 }, width, height };
hidden_region_ = (NeuroRectangle){ (NeuroPoint){ width, height }, 1920, 1080 };
// Set colors, cursors and atoms
if (!set_colors_cursors_atoms())
return false;
// Check if another window manager is already running
XSetErrorHandler(xerror_handler_start);
// Setup root window mask
XSetWindowAttributes wa;
wa.cursor = NeuroSystemGetCursor(NEURO_SYSTEM_CURSOR_NORMAL);
wa.event_mask = NEURO_SYSTEM_ROOT_MASK;
XChangeWindowAttributes(display_, root_, CWEventMask|CWCursor, &wa);
XSelectInput(display_, root_, wa.event_mask);
XSync(display_, false);
// Set custom X error handler
XSetErrorHandler(xerror_handler);
XSync(display_, false);
// Grab key bindings
NeuroSystemGrabKeys(root_, NeuroConfigGet()->key_list);
return true;
}
void NeuroSystemStop(void) {
XFreeCursor(display_, NeuroSystemGetCursor(NEURO_SYSTEM_CURSOR_NORMAL));
XFreeCursor(display_, NeuroSystemGetCursor(NEURO_SYSTEM_CURSOR_RESIZE));
XFreeCursor(display_, NeuroSystemGetCursor(NEURO_SYSTEM_CURSOR_MOVE));
XCloseDisplay(display_);
}
// Basic functions
Display *NeuroSystemGetDisplay(void) {
return display_;
}
Window NeuroSystemGetRoot(void) {
return root_;
}
int NeuroSystemGetScreen(void) {
return screen_;
}
const NeuroRectangle *NeuroSystemGetScreenRegion(void) {
return &screen_region_;
}
const NeuroRectangle *NeuroSystemGetHiddenRegion(void) {
return &hidden_region_;
}
const int *NeuroSystemGetHiddenGaps(void) {
return hidden_gaps_;
}
void NeuroSystemGetPointerWindowLocation(NeuroPoint *p, Window *w) {
Window root_win = 0UL, child_win = 0UL;
int px = 0, py = 0, xc = 0, yc = 0;
unsigned int state = 0;
if (!XQueryPointer(display_, root_, &root_win, w ? w : &child_win, p ? &p->x : &px, p ? &p->y : &py, &xc, &yc,
&state) ? p : NULL)
NeuroSystemError("NeuroSystemGetPointerWindowLocation - Could not query pointer");
}
Cursor NeuroSystemGetCursor(NeuroSystemCursor c) {
return cursors_[ c ];
}
Atom NeuroSystemGetWmAtom(NeuroSystemWmatom a) {
return wm_atoms_[ a ];
}
Atom NeuroSystemGetNetAtom(NeuroSystemNetatom a) {
return net_atoms_[ a ];
}
NeuroColor NeuroSystemGetColor(NeuroSystemColor c) {
return colors_[ c ];
}
NeuroColor NeuroSystemGetColorFromHex(const char* color) {
assert(color);
XColor c;
if (!XAllocNamedColor(display_, DefaultColormap(display_, screen_), color, &c, &c))
NeuroSystemError("NeuroSystemGetColorFromHex - Could not allocate color");
return (NeuroColor)c.pixel;
}
// System functions
const char * const *NeuroSystemGetRecompileCommand(const char **output, const char **source) {
snprintf((char *)recompile_cmd_output_, NEURO_NAME_SIZE_MAX, "%s/." PKG_NAME "/" PKG_MYNAME, getenv("HOME"));
snprintf((char *)recompile_cmd_source_, NEURO_NAME_SIZE_MAX, "%s/." PKG_NAME "/" PKG_NAME ".c", getenv("HOME"));
if (output)
*output = recompile_cmd_output_;
if (source)
*source = recompile_cmd_source_;
return recompile_cmd_;
}
void NeuroSystemChangeWmName(const char *name) {
assert(name);
const Atom netwmcheck = XInternAtom(display_, "_NET_SUPPORTING_WM_CHECK", false);
const Atom netwmname = XInternAtom(display_, "_NET_WM_NAME", false);
const Atom utf8_str = XInternAtom(display_, "UTF8_STRING", false);
XChangeProperty(display_, root_, netwmcheck, XA_WINDOW, 32, PropModeReplace, (unsigned char *)&root_, 1);
XChangeProperty(display_, root_, netwmname, utf8_str, 8, PropModeReplace, (const unsigned char *)name, strlen(name));
}
void NeuroSystemChangeProcName(const char *name) {
assert(name);
prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);
}
pid_t NeuroSystemGetWmPid(void) {
char pidstr[ NEURO_NAME_SIZE_MAX ];
FILE *const cmd = popen("/usr/bin/pidof -s " PKG_MYNAME, "r");
fgets(pidstr, NEURO_NAME_SIZE_MAX, cmd);
return (pid_t)strtoul(pidstr, NULL, 10);
}
bool NeuroSystemSpawn(const char *const *cmd, pid_t *p) {
if (!cmd && !*cmd)
return false;
// Create a new process for the new command
const pid_t pid = fork();
if (pid == -1)
return false;
// Run the command within the child process
if (pid == 0) {
if (display_)
close(ConnectionNumber(display_));
setsid();
run_command(cmd);
}
// Store the pid if needed
if (p)
*p = pid;
return true;
}
int NeuroSystemSpawnPipe(const char *const *cmd, pid_t *p) {
if (!cmd && !*cmd)
return -1;
// Create a fd array for the pipe
int filedes[ 2 ];
if (pipe(filedes))
return -1;
// Run command within the child process
const pid_t pid = fork();
if (pid < 0)
return -1;
// Run the command within the child process
if (pid == 0) {
if (display_)
close(ConnectionNumber(display_));
setsid();
// redirect the output of the command
dup2(filedes[ 0 ], STDIN_FILENO);
close(filedes[ 0 ]);
run_command(cmd);
}
// Store the pid if needed
if (p)
*p = pid;
return filedes[ 1 ];
}
void NeuroSystemError(const char *msg) {
perror(msg);
exit(EXIT_FAILURE);
}
// Binding functions
void NeuroSystemGrabKeys(Window w, const NeuroKey *const *key_list) {
if (!key_list)
return;
XUngrabKey(display_, AnyKey, AnyModifier, w);
for (NeuroIndex i = 0U; key_list[ i ]; ++i) {
const NeuroKey *const k = key_list[ i ];
const KeyCode code = XKeysymToKeycode(display_, k->key);
if (code)
XGrabKey(display_, code, k->mod, w, true, GrabModeAsync, GrabModeAsync);
}
}
void NeuroSystemUngrabKeys(Window w, const NeuroKey *const *key_list) {
if (!key_list)
return;
for (NeuroIndex i = 0U; key_list[ i ]; ++i) {
const NeuroKey *const k = key_list[ i ];
const KeyCode code = XKeysymToKeycode(display_, k->key);
if (code)
XUngrabKey(display_, code, k->mod, w);
}
}
void NeuroSystemGrabButtons(Window w, const NeuroButton *const *button_list) {
if (!button_list)
return;
XUngrabButton(display_, AnyButton, AnyModifier, w);
for (NeuroIndex i = 0U; button_list[ i ]; ++i) {
const NeuroButton *const b = button_list[ i ];
XGrabButton(display_, b->button, b->mod, w, false, ButtonPressMask|ButtonReleaseMask, GrabModeAsync, GrabModeSync,
None, None);
}
}
void NeuroSystemUngrabButtons(Window w, const NeuroButton *const *button_list) {
if (!button_list)
return;
for (NeuroIndex i = 0U; button_list[ i ]; ++i) {
const NeuroButton *b = button_list[ i ];
if (b->ungrab_on_focus)
XUngrabButton(display_, b->button, b->mod, w);
}
}