-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
513 lines (441 loc) · 14 KB
/
config.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
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
#include "config.h" // Includes "atlas.h"
#include "atlas.h"
#include "toml.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Global configuration instance
Config cfg = {
.outerGaps = 20,
.innerGaps = 10,
.borderWidth = 3,
.borderInactiveColor = "#222222",
.borderActiveColor = "#444444",
.snapDistance = 0,
.masterFactor = 0.5,
.lockFullscreen = 1,
.focusNewWindows = 1,
.keybindings = NULL,
.keybindingCapacity = 0,
.moveCursorWithFocus = 1,
.logLevel = "info",
};
static const struct {
const char *name;
ActionType action;
} action_map[] = {{"spawn", ACTION_SPAWN},
{"reload", ACTION_RELOAD},
{"cyclefocus", ACTION_CYCLEFOCUS},
{"killclient", ACTION_KILLCLIENT},
{"togglefloating", ACTION_TOGGLEFLOATING},
{"togglefullscreen", ACTION_TOGGLEFULLSCREEN},
{"focusmonitor", ACTION_FOCUSMONITOR},
{"movetomonitor", ACTION_MOVETOMONITOR},
{"viewworkspace", ACTION_VIEWWORKSPACE},
{"movetoworkspace", ACTION_MOVETOWORKSPACE},
{"duplicatetoworkspace", ACTION_DUPLICATETOWORKSPACE},
{"toggleworkspace", ACTION_TOGGLEWORKSPACE},
{"quit", ACTION_QUIT},
{NULL, ACTION_UNKNOWN}};
static const struct {
const char *name;
unsigned int mask;
} modifier_map[] = {{"Mod1", Mod1Mask},
{"Mod4", Mod4Mask},
{"Control", ControlMask},
{"Shift", ShiftMask},
{"Alt", Mod1Mask},
{"Super", Mod4Mask},
{NULL, 0}};
ActionType string_to_action(const char *action) {
for (int i = 0; action_map[i].name != NULL; i++) {
if (strcasecmp(action, action_map[i].name) == 0) {
return action_map[i].action;
}
}
return ACTION_UNKNOWN;
}
unsigned int parse_modifier(const char *mod) {
unsigned int mask = 0;
char *mod_copy = strdup(mod);
char *token = strtok(mod_copy, "+");
while (token) {
for (int i = 0; modifier_map[i].name != NULL; i++) {
if (strcasecmp(token, modifier_map[i].name) == 0) {
mask |= modifier_map[i].mask;
break;
}
}
token = strtok(NULL, "+");
}
free(mod_copy);
return mask;
}
KeySym parse_key(const char *key) {
// Convert the last token to a keysym
if (strlen(key) == 1) {
return XStringToKeysym(key);
}
return XStringToKeysym(key);
}
void parse_keybinding(const char *key_str, toml_table_t *binding_table) {
// Grow keybindings array if needed
if (cfg.keybindingCount >= cfg.keybindingCapacity) {
size_t new_capacity =
cfg.keybindingCapacity == 0 ? 16 : cfg.keybindingCapacity * 2;
Keybinding *new_keybindings =
realloc(cfg.keybindings, new_capacity * sizeof(Keybinding));
if (!new_keybindings) {
LOG_ERROR("Failed to allocate memory for keybindings");
return;
}
cfg.keybindings = new_keybindings;
cfg.keybindingCapacity = new_capacity;
}
// Parse the key combination
char *last_plus = strrchr(key_str, '+');
if (!last_plus) {
LOG_ERROR("Invalid key binding format: %s", key_str);
return;
}
// Split into modifier and key
size_t mod_len = last_plus - key_str;
char *modifier_str = strndup(key_str, mod_len);
const char *key = last_plus + 1;
LOG_INFO("Key: %s, Modifier: %s", key, modifier_str);
// Get the binding properties
toml_datum_t action = toml_string_in(binding_table, "action");
toml_datum_t value = toml_string_in(binding_table, "value");
toml_datum_t desc = toml_string_in(binding_table, "desc");
if (!action.ok) {
LOG_ERROR("Keybinding missing action: %s", key_str);
free(modifier_str);
return;
}
LOG_DEBUG("Action: %s", action.u.s);
// Create the keybinding
Keybinding *kb = &cfg.keybindings[cfg.keybindingCount];
kb->modifier = parse_modifier(modifier_str);
kb->keysym = parse_key(key);
kb->action = string_to_action(action.u.s);
// Allocate and copy value if present
if (value.ok) {
kb->value = strdup(value.u.s);
free(value.u.s);
} else {
kb->value = strdup(""); // Empty string instead of NULL
}
// Allocate and copy description if present
if (desc.ok) {
kb->description = strdup(desc.u.s);
free(desc.u.s);
} else {
kb->description = strdup(""); // Empty string instead of NULL
}
free(modifier_str);
free(action.u.s);
cfg.keybindingCount++;
LOG_DEBUG("Added keybinding: %s -> %s", key_str, kb->description);
}
void load_keybindings(toml_table_t *conf) {
toml_table_t *keybindings = toml_table_in(conf, "keybindings");
if (!keybindings) {
LOG_INFO("No keybindings configuration found");
return;
}
cfg.keybindingCount = 0;
// Get number of entries in the keybindings table
int keycount = toml_table_nkval(keybindings) + toml_table_ntab(keybindings);
for (int i = 0; i < keycount; i++) {
const char *key = toml_key_in(keybindings, i);
if (!key)
continue;
toml_table_t *binding = toml_table_in(keybindings, key);
if (binding) {
parse_keybinding(key, binding);
}
}
}
// Function to split command string into command and arguments
void parse_startup_program(const char *cmd_str, StartupProgram *prog) {
char *str = strdup(cmd_str);
char *token;
int capacity = 10; // Initial capacity for arguments array
prog->args = malloc(capacity * sizeof(char *));
prog->arg_count = 0;
// Get the command (first word)
token = strtok(str, " ");
if (token) {
prog->command = strdup(token);
prog->args[prog->arg_count++] = strdup(token);
// Parse remaining arguments
while ((token = strtok(NULL, " ")) != NULL) {
if (prog->arg_count >= capacity - 1) { // -1 for NULL terminator
capacity *= 2;
prog->args = realloc(prog->args, capacity * sizeof(char *));
}
prog->args[prog->arg_count++] = strdup(token);
}
}
// NULL terminate the arguments array
prog->args[prog->arg_count] = NULL;
free(str);
}
// Function to free startup program resources
void free_startup_program(StartupProgram *prog) {
if (!prog)
return;
free(prog->command);
if (prog->args) {
for (int i = 0; i < prog->arg_count; i++) {
free(prog->args[i]);
}
free(prog->args);
}
}
void free_startup_programs(Config *cfg) {
if (cfg->startup_progs) {
for (int i = 0; i < cfg->startup_prog_count; i++) {
free_startup_program(&cfg->startup_progs[i]);
}
free(cfg->startup_progs);
cfg->startup_progs = NULL;
cfg->startup_prog_count = 0;
}
}
// Add this to your load_config function after other TOML parsing
void load_startup_programs(toml_table_t *conf) {
// Free any existing startup programs
free_startup_programs(&cfg);
toml_array_t *startup = toml_array_in(conf, "startup_progs");
if (!startup) {
LOG_INFO("No startup programs configured");
return;
}
// Count the number of startup programs
int count = toml_array_nelem(startup);
if (count <= 0)
return;
// Allocate space for startup programs
cfg.startup_progs = calloc(count, sizeof(StartupProgram));
cfg.startup_prog_count = count;
// Parse each startup program
for (int i = 0; i < count; i++) {
toml_datum_t prog = toml_string_at(startup, i);
if (prog.ok) {
parse_startup_program(prog.u.s, &cfg.startup_progs[i]);
free(prog.u.s);
}
}
}
static void free_workspaces(void) {
if (cfg.workspaces) {
for (size_t i = 0; i < cfg.workspaceCount; i++) {
free(cfg.workspaces[i].name);
}
free(cfg.workspaces);
cfg.workspaces = NULL;
cfg.workspaceCount = 0;
}
}
static void load_workspaces(toml_table_t *conf) {
// Free existing workspaces if any
free_workspaces();
toml_array_t *workspaces = toml_array_in(conf, "workspaces");
if (!workspaces) {
// Set default workspaces if none specified
cfg.workspaceCount = 9;
cfg.workspaces = ecalloc(cfg.workspaceCount, sizeof(Workspace));
for (size_t i = 0; i < cfg.workspaceCount; i++) {
char num[2];
snprintf(num, sizeof(num), "%zu", i + 1);
cfg.workspaces[i].name = strdup(num);
}
return;
}
// Count array elements
cfg.workspaceCount = 0;
while (toml_raw_at(workspaces, cfg.workspaceCount)) {
cfg.workspaceCount++;
}
// Allocate workspace array
cfg.workspaces = ecalloc(cfg.workspaceCount, sizeof(Workspace));
// Load each workspace
for (size_t i = 0; i < cfg.workspaceCount; i++) {
toml_datum_t raw = toml_string_at(workspaces, i);
if (raw.ok) {
cfg.workspaces[i].name = strdup(raw.u.s);
free(raw.u.s);
} else {
LOG_ERROR("Failed to parse workspace %zu", i);
// Use default name as fallback
char num[2];
snprintf(num, sizeof(num), "%zu", i + 1);
cfg.workspaces[i].name = strdup(num);
}
}
}
// Add this to update_window_manager_state()
void update_keybindings(void) {
// Clear existing keybindings
XUngrabKey(display, AnyKey, AnyModifier, root);
// Register new keybindings
updateNumlockMask();
unsigned int modifiers[] = {0, LockMask, numLockMask, numLockMask | LockMask};
for (int i = 0; i < cfg.keybindingCount; i++) {
KeyCode code = XKeysymToKeycode(display, cfg.keybindings[i].keysym);
if (code) {
for (size_t j = 0; j < LENGTH(modifiers); j++) {
XGrabKey(display, code, cfg.keybindings[i].modifier | modifiers[j],
root, True, GrabModeAsync, GrabModeAsync);
}
}
}
}
void update_window_manager_state(void) {
Monitor *m;
Client *c;
// Update monitor properties
for (m = monitors; m; m = m->next) {
// Update all clients on this monitor
for (c = m->clients; c; c = c->next) {
if (!c->isFullscreen) { // Don't modify fullscreen windows
// Update border width
c->borderWidth = cfg.borderWidth;
Clr activeBorderColor;
drw_clr_create(drawContext, &activeBorderColor, cfg.borderActiveColor);
Clr inactiveBorderColor;
drw_clr_create(drawContext, &inactiveBorderColor,
cfg.borderInactiveColor);
XSetWindowBorder(display, c->win,
(c == selectedMonitor->active)
? activeBorderColor.pixel
: inactiveBorderColor.pixel);
// Apply border width change
XWindowChanges wc = {.x = c->x,
.y = c->y,
.width = c->w,
.height = c->h,
.border_width = c->borderWidth};
XConfigureWindow(display, c->win,
CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
}
}
// Update master factor and number of master windows
m->masterFactor = cfg.masterFactor;
}
setNumDesktops();
setCurrentDesktop();
setDesktopNames();
setViewport();
// Rearrange all monitors to apply gap changes and new layouts
arrange(NULL);
// Update keybindings
update_keybindings();
// Sync changes
XSync(display, False);
}
int load_config(const char *config_path) {
FILE *fp;
char errbuf[200];
// Open config file
fp = fopen(config_path, "r");
if (!fp) {
LOG_ERROR("Failed to open config file: %s", config_path);
return 0;
}
// Parse TOML
toml_table_t *conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
fclose(fp);
if (!conf) {
LOG_ERROR("Failed to parse config file: %s", errbuf);
return 0;
}
// Gaps
toml_table_t *gaps = toml_table_in(conf, "gaps");
if (gaps) {
toml_datum_t outer = toml_int_in(gaps, "outer");
if (outer.ok) {
cfg.outerGaps = outer.u.i;
}
toml_datum_t inner = toml_int_in(gaps, "inner");
if (inner.ok) {
cfg.innerGaps = inner.u.i;
}
}
// Read border configuration
toml_table_t *border = toml_table_in(conf, "border");
if (border) {
toml_datum_t width = toml_int_in(border, "width");
if (width.ok) {
cfg.borderWidth = width.u.i;
}
toml_datum_t active = toml_string_in(border, "active");
if (active.ok) {
safe_strcpy(cfg.borderActiveColor, active.u.s,
sizeof(cfg.borderActiveColor));
free(active.u.s);
}
toml_datum_t inactive = toml_string_in(border, "inactive");
if (inactive.ok) {
safe_strcpy(cfg.borderInactiveColor, inactive.u.s,
sizeof(cfg.borderInactiveColor));
free(inactive.u.s);
}
}
// Layout configuration
toml_table_t *layout = toml_table_in(conf, "layout");
if (layout) {
toml_datum_t master_factor = toml_double_in(layout, "master_factor");
if (master_factor.ok) {
cfg.masterFactor = master_factor.u.d;
}
}
// window configuration
toml_table_t *windows = toml_table_in(conf, "windows");
if (windows) {
toml_datum_t focus_new_windows = toml_bool_in(windows, "focus_new_windows");
if (focus_new_windows.ok) {
cfg.focusNewWindows = focus_new_windows.u.b;
}
toml_datum_t move_cursor_with_focus =
toml_bool_in(windows, "move_cursor_with_focus");
if (move_cursor_with_focus.ok) {
cfg.moveCursorWithFocus = move_cursor_with_focus.u.b;
}
}
toml_datum_t log_level = toml_string_in(conf, "log_level");
if (log_level.ok) {
// If log_level matches any avaliable log level, use it
if (strcmp(log_level.u.s, "debug") == 0 ||
strcmp(log_level.u.s, "info") == 0 ||
strcmp(log_level.u.s, "warning") == 0) {
cfg.logLevel = strdup(log_level.u.s);
free(log_level.u.s);
} else {
LOG_WARN("Invalid log level: %s", log_level.u.s);
}
}
load_keybindings(conf);
load_startup_programs(conf);
load_workspaces(conf);
toml_free(conf);
return 1;
}
void reload_config(void) {
char config_path[256];
char *home = getenv("HOME");
if (!home) {
LOG_ERROR("Could not get HOME directory");
return;
}
snprintf(config_path, sizeof(config_path), "%s/.config/atlaswm/config.toml",
home);
if (load_config(config_path)) {
LOG_INFO("Configuration reloaded successfully");
update_window_manager_state();
} else {
LOG_ERROR("Failed to reload configuration");
}
}