tony / oxwm

Branch: e95e4af
Switch branches/tags
master
tonybanters Zig port. e95e4af 2026-02-02
1172 lines · 34.3 KB Raw Blame History
1 const std = @import("std");
2 const config_mod = @import("config.zig");
3 const Config = config_mod.Config;
4 const Keybind = config_mod.Keybind;
5 const Action = config_mod.Action;
6 const Rule = config_mod.Rule;
7 const Block = config_mod.Block;
8 const Block_Type = config_mod.Block_Type;
9 const Mouse_Button = config_mod.Mouse_Button;
10 const Click_Target = config_mod.Click_Target;
11 const Mouse_Action = config_mod.Mouse_Action;
12 const Color_Scheme = config_mod.Color_Scheme;
13
14 const c = @cImport({
15 @cInclude("lua.h");
16 @cInclude("lauxlib.h");
17 @cInclude("lualib.h");
18 });
19
20 var L: ?*c.lua_State = null;
21 var config: ?*Config = null;
22
23 pub fn init(cfg: *Config) bool {
24 config = cfg;
25 L = c.luaL_newstate();
26 if (L == null) return false;
27 c.luaL_openlibs(L);
28 register_api();
29 return true;
30 }
31
32 pub fn deinit() void {
33 if (L) |state| {
34 c.lua_close(state);
35 }
36 L = null;
37 config = null;
38 }
39
40 pub fn load_file(path: []const u8) bool {
41 const state = L orelse return false;
42 var path_buf: [512]u8 = undefined;
43 if (path.len >= path_buf.len) return false;
44 @memcpy(path_buf[0..path.len], path);
45 path_buf[path.len] = 0;
46
47 if (c.luaL_loadfilex(state, &path_buf, null) != 0) {
48 const err = c.lua_tolstring(state, -1, null);
49 if (err != null) {
50 std.debug.print("lua load error: {s}\n", .{std.mem.span(err)});
51 }
52 c.lua_settop(state, -2);
53 return false;
54 }
55
56 if (c.lua_pcallk(state, 0, 0, 0, 0, null) != 0) {
57 const err = c.lua_tolstring(state, -1, null);
58 if (err != null) {
59 std.debug.print("lua runtime error: {s}\n", .{std.mem.span(err)});
60 }
61 c.lua_settop(state, -2);
62 return false;
63 }
64
65 return true;
66 }
67
68 pub fn load_config() bool {
69 const home = std.posix.getenv("HOME") orelse return false;
70 var path_buf: [512]u8 = undefined;
71 const path = std.fmt.bufPrint(&path_buf, "{s}/.config/oxwm/config.lua", .{home}) catch return false;
72 return load_file(path);
73 }
74
75 fn register_api() void {
76 const state = L orelse return;
77
78 c.lua_createtable(state, 0, 16);
79
80 register_spawn_functions(state);
81 register_key_module(state);
82 register_gaps_module(state);
83 register_border_module(state);
84 register_client_module(state);
85 register_layout_module(state);
86 register_tag_module(state);
87 register_monitor_module(state);
88 register_rule_module(state);
89 register_bar_module(state);
90 register_misc_functions(state);
91
92 c.lua_setglobal(state, "oxwm");
93 }
94
95 fn register_spawn_functions(state: *c.lua_State) void {
96 c.lua_pushcfunction(state, lua_spawn);
97 c.lua_setfield(state, -2, "spawn");
98
99 c.lua_pushcfunction(state, lua_spawn_terminal);
100 c.lua_setfield(state, -2, "spawn_terminal");
101 }
102
103 fn register_key_module(state: *c.lua_State) void {
104 c.lua_createtable(state, 0, 2);
105
106 c.lua_pushcfunction(state, lua_key_bind);
107 c.lua_setfield(state, -2, "bind");
108
109 c.lua_pushcfunction(state, lua_key_chord);
110 c.lua_setfield(state, -2, "chord");
111
112 c.lua_setfield(state, -2, "key");
113 }
114
115 fn register_gaps_module(state: *c.lua_State) void {
116 c.lua_createtable(state, 0, 6);
117
118 c.lua_pushcfunction(state, lua_gaps_set_enabled);
119 c.lua_setfield(state, -2, "set_enabled");
120
121 c.lua_pushcfunction(state, lua_gaps_enable);
122 c.lua_setfield(state, -2, "enable");
123
124 c.lua_pushcfunction(state, lua_gaps_disable);
125 c.lua_setfield(state, -2, "disable");
126
127 c.lua_pushcfunction(state, lua_gaps_set_inner);
128 c.lua_setfield(state, -2, "set_inner");
129
130 c.lua_pushcfunction(state, lua_gaps_set_outer);
131 c.lua_setfield(state, -2, "set_outer");
132
133 c.lua_pushcfunction(state, lua_gaps_set_smart);
134 c.lua_setfield(state, -2, "set_smart");
135
136 c.lua_setfield(state, -2, "gaps");
137 }
138
139 fn register_border_module(state: *c.lua_State) void {
140 c.lua_createtable(state, 0, 3);
141
142 c.lua_pushcfunction(state, lua_border_set_width);
143 c.lua_setfield(state, -2, "set_width");
144
145 c.lua_pushcfunction(state, lua_border_set_focused_color);
146 c.lua_setfield(state, -2, "set_focused_color");
147
148 c.lua_pushcfunction(state, lua_border_set_unfocused_color);
149 c.lua_setfield(state, -2, "set_unfocused_color");
150
151 c.lua_setfield(state, -2, "border");
152 }
153
154 fn register_client_module(state: *c.lua_State) void {
155 c.lua_createtable(state, 0, 5);
156
157 c.lua_pushcfunction(state, lua_client_kill);
158 c.lua_setfield(state, -2, "kill");
159
160 c.lua_pushcfunction(state, lua_client_toggle_fullscreen);
161 c.lua_setfield(state, -2, "toggle_fullscreen");
162
163 c.lua_pushcfunction(state, lua_client_toggle_floating);
164 c.lua_setfield(state, -2, "toggle_floating");
165
166 c.lua_pushcfunction(state, lua_client_focus_stack);
167 c.lua_setfield(state, -2, "focus_stack");
168
169 c.lua_pushcfunction(state, lua_client_move_stack);
170 c.lua_setfield(state, -2, "move_stack");
171
172 c.lua_setfield(state, -2, "client");
173 }
174
175 fn register_layout_module(state: *c.lua_State) void {
176 c.lua_createtable(state, 0, 4);
177
178 c.lua_pushcfunction(state, lua_layout_cycle);
179 c.lua_setfield(state, -2, "cycle");
180
181 c.lua_pushcfunction(state, lua_layout_set);
182 c.lua_setfield(state, -2, "set");
183
184 c.lua_pushcfunction(state, lua_layout_scroll_left);
185 c.lua_setfield(state, -2, "scroll_left");
186
187 c.lua_pushcfunction(state, lua_layout_scroll_right);
188 c.lua_setfield(state, -2, "scroll_right");
189
190 c.lua_setfield(state, -2, "layout");
191 }
192
193 fn register_tag_module(state: *c.lua_State) void {
194 c.lua_createtable(state, 0, 10);
195
196 c.lua_pushcfunction(state, lua_tag_view);
197 c.lua_setfield(state, -2, "view");
198
199 c.lua_pushcfunction(state, lua_tag_view_next);
200 c.lua_setfield(state, -2, "view_next");
201
202 c.lua_pushcfunction(state, lua_tag_view_previous);
203 c.lua_setfield(state, -2, "view_previous");
204
205 c.lua_pushcfunction(state, lua_tag_view_next_nonempty);
206 c.lua_setfield(state, -2, "view_next_nonempty");
207
208 c.lua_pushcfunction(state, lua_tag_view_previous_nonempty);
209 c.lua_setfield(state, -2, "view_previous_nonempty");
210
211 c.lua_pushcfunction(state, lua_tag_toggleview);
212 c.lua_setfield(state, -2, "toggleview");
213
214 c.lua_pushcfunction(state, lua_tag_move_to);
215 c.lua_setfield(state, -2, "move_to");
216
217 c.lua_pushcfunction(state, lua_tag_toggletag);
218 c.lua_setfield(state, -2, "toggletag");
219
220 c.lua_pushcfunction(state, lua_tag_set_back_and_forth);
221 c.lua_setfield(state, -2, "set_back_and_forth");
222
223 c.lua_setfield(state, -2, "tag");
224 }
225
226 fn register_monitor_module(state: *c.lua_State) void {
227 c.lua_createtable(state, 0, 2);
228
229 c.lua_pushcfunction(state, lua_monitor_focus);
230 c.lua_setfield(state, -2, "focus");
231
232 c.lua_pushcfunction(state, lua_monitor_tag);
233 c.lua_setfield(state, -2, "tag");
234
235 c.lua_setfield(state, -2, "monitor");
236 }
237
238 fn register_rule_module(state: *c.lua_State) void {
239 c.lua_createtable(state, 0, 1);
240
241 c.lua_pushcfunction(state, lua_rule_add);
242 c.lua_setfield(state, -2, "add");
243
244 c.lua_setfield(state, -2, "rule");
245 }
246
247 fn register_bar_module(state: *c.lua_State) void {
248 c.lua_createtable(state, 0, 10);
249
250 c.lua_pushcfunction(state, lua_bar_set_font);
251 c.lua_setfield(state, -2, "set_font");
252
253 c.lua_pushcfunction(state, lua_bar_set_blocks);
254 c.lua_setfield(state, -2, "set_blocks");
255
256 c.lua_pushcfunction(state, lua_bar_set_scheme_normal);
257 c.lua_setfield(state, -2, "set_scheme_normal");
258
259 c.lua_pushcfunction(state, lua_bar_set_scheme_selected);
260 c.lua_setfield(state, -2, "set_scheme_selected");
261
262 c.lua_pushcfunction(state, lua_bar_set_scheme_occupied);
263 c.lua_setfield(state, -2, "set_scheme_occupied");
264
265 c.lua_pushcfunction(state, lua_bar_set_scheme_urgent);
266 c.lua_setfield(state, -2, "set_scheme_urgent");
267
268 c.lua_pushcfunction(state, lua_bar_set_hide_vacant_tags);
269 c.lua_setfield(state, -2, "set_hide_vacant_tags");
270
271 c.lua_createtable(state, 0, 6);
272
273 c.lua_pushcfunction(state, lua_bar_block_ram);
274 c.lua_setfield(state, -2, "ram");
275
276 c.lua_pushcfunction(state, lua_bar_block_datetime);
277 c.lua_setfield(state, -2, "datetime");
278
279 c.lua_pushcfunction(state, lua_bar_block_shell);
280 c.lua_setfield(state, -2, "shell");
281
282 c.lua_pushcfunction(state, lua_bar_block_static);
283 c.lua_setfield(state, -2, "static");
284
285 c.lua_pushcfunction(state, lua_bar_block_battery);
286 c.lua_setfield(state, -2, "battery");
287
288 c.lua_setfield(state, -2, "block");
289
290 c.lua_setfield(state, -2, "bar");
291 }
292
293 fn register_misc_functions(state: *c.lua_State) void {
294 c.lua_pushcfunction(state, lua_set_terminal);
295 c.lua_setfield(state, -2, "set_terminal");
296
297 c.lua_pushcfunction(state, lua_set_modkey);
298 c.lua_setfield(state, -2, "set_modkey");
299
300 c.lua_pushcfunction(state, lua_set_tags);
301 c.lua_setfield(state, -2, "set_tags");
302
303 c.lua_pushcfunction(state, lua_autostart);
304 c.lua_setfield(state, -2, "autostart");
305
306 c.lua_pushcfunction(state, lua_auto_tile);
307 c.lua_setfield(state, -2, "auto_tile");
308
309 c.lua_pushcfunction(state, lua_quit);
310 c.lua_setfield(state, -2, "quit");
311
312 c.lua_pushcfunction(state, lua_restart);
313 c.lua_setfield(state, -2, "restart");
314
315 c.lua_pushcfunction(state, lua_toggle_gaps);
316 c.lua_setfield(state, -2, "toggle_gaps");
317
318 c.lua_pushcfunction(state, lua_set_master_factor);
319 c.lua_setfield(state, -2, "set_master_factor");
320
321 c.lua_pushcfunction(state, lua_inc_num_master);
322 c.lua_setfield(state, -2, "inc_num_master");
323 }
324
325 fn create_action_table(state: *c.lua_State, action_name: [*:0]const u8) void {
326 c.lua_createtable(state, 0, 2);
327 _ = c.lua_pushstring(state, action_name);
328 c.lua_setfield(state, -2, "__action");
329 }
330
331 fn create_action_table_with_int(state: *c.lua_State, action_name: [*:0]const u8, arg: i32) void {
332 c.lua_createtable(state, 0, 2);
333 _ = c.lua_pushstring(state, action_name);
334 c.lua_setfield(state, -2, "__action");
335 c.lua_pushinteger(state, arg);
336 c.lua_setfield(state, -2, "__arg");
337 }
338
339 fn create_action_table_with_string(state: *c.lua_State, action_name: [*:0]const u8) void {
340 c.lua_createtable(state, 0, 2);
341 _ = c.lua_pushstring(state, action_name);
342 c.lua_setfield(state, -2, "__action");
343 c.lua_pushvalue(state, 1);
344 c.lua_setfield(state, -2, "__arg");
345 }
346
347 fn lua_spawn(state: ?*c.lua_State) callconv(.c) c_int {
348 const s = state orelse return 0;
349 create_action_table_with_string(s, "Spawn");
350 return 1;
351 }
352
353 fn lua_spawn_terminal(state: ?*c.lua_State) callconv(.c) c_int {
354 const s = state orelse return 0;
355 create_action_table(s, "SpawnTerminal");
356 return 1;
357 }
358
359 fn lua_key_bind(state: ?*c.lua_State) callconv(.c) c_int {
360 const s = state orelse return 0;
361 const cfg = config orelse return 0;
362
363 const mod_mask = parse_modifiers(s, 1);
364 const key_str = get_string_arg(s, 2) orelse return 0;
365 const keysym = key_name_to_keysym(key_str) orelse return 0;
366
367 if (c.lua_type(s, 3) != c.LUA_TTABLE) return 0;
368
369 _ = c.lua_getfield(s, 3, "__action");
370 const action_str = get_lua_string(s, -1) orelse {
371 c.lua_settop(s, -2);
372 return 0;
373 };
374 c.lua_settop(s, -2);
375
376 const action = parse_action(action_str) orelse return 0;
377
378 var int_arg: i32 = 0;
379 var str_arg: ?[]const u8 = null;
380
381 _ = c.lua_getfield(s, 3, "__arg");
382 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
383 int_arg = @intCast(c.lua_tointegerx(s, -1, null));
384 } else if (c.lua_type(s, -1) == c.LUA_TSTRING) {
385 str_arg = get_lua_string(s, -1);
386 }
387 c.lua_settop(s, -2);
388
389 cfg.add_keybind(.{
390 .mod_mask = mod_mask,
391 .keysym = keysym,
392 .action = action,
393 .int_arg = int_arg,
394 .str_arg = str_arg,
395 }) catch return 0;
396
397 return 0;
398 }
399
400 fn lua_key_chord(state: ?*c.lua_State) callconv(.c) c_int {
401 _ = state;
402 return 0;
403 }
404
405 fn lua_gaps_set_enabled(state: ?*c.lua_State) callconv(.c) c_int {
406 const cfg = config orelse return 0;
407 const s = state orelse return 0;
408 cfg.gaps_enabled = c.lua_toboolean(s, 1) != 0;
409 return 0;
410 }
411
412 fn lua_gaps_enable(state: ?*c.lua_State) callconv(.c) c_int {
413 _ = state;
414 const cfg = config orelse return 0;
415 cfg.gaps_enabled = true;
416 return 0;
417 }
418
419 fn lua_gaps_disable(state: ?*c.lua_State) callconv(.c) c_int {
420 _ = state;
421 const cfg = config orelse return 0;
422 cfg.gaps_enabled = false;
423 return 0;
424 }
425
426 fn lua_gaps_set_inner(state: ?*c.lua_State) callconv(.c) c_int {
427 const cfg = config orelse return 0;
428 const s = state orelse return 0;
429 cfg.gap_inner_h = @intCast(c.lua_tointegerx(s, 1, null));
430 cfg.gap_inner_v = @intCast(c.lua_tointegerx(s, 2, null));
431 return 0;
432 }
433
434 fn lua_gaps_set_outer(state: ?*c.lua_State) callconv(.c) c_int {
435 const cfg = config orelse return 0;
436 const s = state orelse return 0;
437 cfg.gap_outer_h = @intCast(c.lua_tointegerx(s, 1, null));
438 cfg.gap_outer_v = @intCast(c.lua_tointegerx(s, 2, null));
439 return 0;
440 }
441
442 fn lua_gaps_set_smart(state: ?*c.lua_State) callconv(.c) c_int {
443 const cfg = config orelse return 0;
444 const s = state orelse return 0;
445 cfg.smartgaps_enabled = c.lua_toboolean(s, 1) != 0;
446 return 0;
447 }
448
449 fn lua_border_set_width(state: ?*c.lua_State) callconv(.c) c_int {
450 const cfg = config orelse return 0;
451 const s = state orelse return 0;
452 cfg.border_width = @intCast(c.lua_tointegerx(s, 1, null));
453 return 0;
454 }
455
456 fn lua_border_set_focused_color(state: ?*c.lua_State) callconv(.c) c_int {
457 const cfg = config orelse return 0;
458 const s = state orelse return 0;
459 cfg.border_focused = parse_color(s, 1);
460 return 0;
461 }
462
463 fn lua_border_set_unfocused_color(state: ?*c.lua_State) callconv(.c) c_int {
464 const cfg = config orelse return 0;
465 const s = state orelse return 0;
466 cfg.border_unfocused = parse_color(s, 1);
467 return 0;
468 }
469
470 fn lua_client_kill(state: ?*c.lua_State) callconv(.c) c_int {
471 const s = state orelse return 0;
472 create_action_table(s, "KillClient");
473 return 1;
474 }
475
476 fn lua_client_toggle_fullscreen(state: ?*c.lua_State) callconv(.c) c_int {
477 const s = state orelse return 0;
478 create_action_table(s, "ToggleFullScreen");
479 return 1;
480 }
481
482 fn lua_client_toggle_floating(state: ?*c.lua_State) callconv(.c) c_int {
483 const s = state orelse return 0;
484 create_action_table(s, "ToggleFloating");
485 return 1;
486 }
487
488 fn lua_client_focus_stack(state: ?*c.lua_State) callconv(.c) c_int {
489 const s = state orelse return 0;
490 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
491 create_action_table_with_int(s, "FocusStack", dir);
492 return 1;
493 }
494
495 fn lua_client_move_stack(state: ?*c.lua_State) callconv(.c) c_int {
496 const s = state orelse return 0;
497 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
498 create_action_table_with_int(s, "MoveStack", dir);
499 return 1;
500 }
501
502 fn lua_layout_cycle(state: ?*c.lua_State) callconv(.c) c_int {
503 const s = state orelse return 0;
504 create_action_table(s, "CycleLayout");
505 return 1;
506 }
507
508 fn lua_layout_set(state: ?*c.lua_State) callconv(.c) c_int {
509 const s = state orelse return 0;
510 create_action_table_with_string(s, "ChangeLayout");
511 return 1;
512 }
513
514 fn lua_layout_scroll_left(state: ?*c.lua_State) callconv(.c) c_int {
515 const s = state orelse return 0;
516 create_action_table(s, "ScrollLeft");
517 return 1;
518 }
519
520 fn lua_layout_scroll_right(state: ?*c.lua_State) callconv(.c) c_int {
521 const s = state orelse return 0;
522 create_action_table(s, "ScrollRight");
523 return 1;
524 }
525
526 fn lua_tag_view(state: ?*c.lua_State) callconv(.c) c_int {
527 const s = state orelse return 0;
528 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
529 create_action_table_with_int(s, "ViewTag", idx - 1);
530 return 1;
531 }
532
533 fn lua_tag_view_next(state: ?*c.lua_State) callconv(.c) c_int {
534 const s = state orelse return 0;
535 create_action_table(s, "ViewNextTag");
536 return 1;
537 }
538
539 fn lua_tag_view_previous(state: ?*c.lua_State) callconv(.c) c_int {
540 const s = state orelse return 0;
541 create_action_table(s, "ViewPreviousTag");
542 return 1;
543 }
544
545 fn lua_tag_view_next_nonempty(state: ?*c.lua_State) callconv(.c) c_int {
546 const s = state orelse return 0;
547 create_action_table(s, "ViewNextNonEmptyTag");
548 return 1;
549 }
550
551 fn lua_tag_view_previous_nonempty(state: ?*c.lua_State) callconv(.c) c_int {
552 const s = state orelse return 0;
553 create_action_table(s, "ViewPreviousNonEmptyTag");
554 return 1;
555 }
556
557 fn lua_tag_toggleview(state: ?*c.lua_State) callconv(.c) c_int {
558 const s = state orelse return 0;
559 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
560 create_action_table_with_int(s, "ToggleView", idx - 1);
561 return 1;
562 }
563
564 fn lua_tag_move_to(state: ?*c.lua_State) callconv(.c) c_int {
565 const s = state orelse return 0;
566 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
567 create_action_table_with_int(s, "MoveToTag", idx - 1);
568 return 1;
569 }
570
571 fn lua_tag_toggletag(state: ?*c.lua_State) callconv(.c) c_int {
572 const s = state orelse return 0;
573 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
574 create_action_table_with_int(s, "ToggleTag", idx - 1);
575 return 1;
576 }
577
578 fn lua_tag_set_back_and_forth(state: ?*c.lua_State) callconv(.c) c_int {
579 const cfg = config orelse return 0;
580 const s = state orelse return 0;
581 cfg.tag_back_and_forth = c.lua_toboolean(s, 1) != 0;
582 return 0;
583 }
584
585 fn lua_monitor_focus(state: ?*c.lua_State) callconv(.c) c_int {
586 const s = state orelse return 0;
587 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
588 create_action_table_with_int(s, "FocusMonitor", dir);
589 return 1;
590 }
591
592 fn lua_monitor_tag(state: ?*c.lua_State) callconv(.c) c_int {
593 const s = state orelse return 0;
594 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
595 create_action_table_with_int(s, "TagMonitor", dir);
596 return 1;
597 }
598
599 fn lua_rule_add(state: ?*c.lua_State) callconv(.c) c_int {
600 const cfg = config orelse return 0;
601 const s = state orelse return 0;
602
603 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
604
605 var rule = Rule{
606 .class = null,
607 .instance = null,
608 .title = null,
609 .tags = 0,
610 .is_floating = false,
611 .monitor = -1,
612 };
613
614 _ = c.lua_getfield(s, 1, "class");
615 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
616 rule.class = get_lua_string(s, -1);
617 }
618 c.lua_settop(s, -2);
619
620 _ = c.lua_getfield(s, 1, "instance");
621 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
622 rule.instance = get_lua_string(s, -1);
623 }
624 c.lua_settop(s, -2);
625
626 _ = c.lua_getfield(s, 1, "title");
627 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
628 rule.title = get_lua_string(s, -1);
629 }
630 c.lua_settop(s, -2);
631
632 _ = c.lua_getfield(s, 1, "tag");
633 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
634 const tag_idx: i32 = @intCast(c.lua_tointegerx(s, -1, null));
635 if (tag_idx > 0) {
636 rule.tags = @as(u32, 1) << @intCast(tag_idx - 1);
637 }
638 }
639 c.lua_settop(s, -2);
640
641 _ = c.lua_getfield(s, 1, "floating");
642 if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) {
643 rule.is_floating = c.lua_toboolean(s, -1) != 0;
644 }
645 c.lua_settop(s, -2);
646
647 _ = c.lua_getfield(s, 1, "monitor");
648 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
649 rule.monitor = @intCast(c.lua_tointegerx(s, -1, null));
650 }
651 c.lua_settop(s, -2);
652
653 cfg.add_rule(rule) catch return 0;
654 return 0;
655 }
656
657 fn lua_bar_set_font(state: ?*c.lua_State) callconv(.c) c_int {
658 const cfg = config orelse return 0;
659 const s = state orelse return 0;
660 if (get_string_arg(s, 1)) |font| {
661 cfg.font = font;
662 }
663 return 0;
664 }
665
666 fn lua_bar_set_blocks(state: ?*c.lua_State) callconv(.c) c_int {
667 const cfg = config orelse return 0;
668 const s = state orelse return 0;
669
670 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
671
672 const len = c.lua_rawlen(s, 1);
673 var i: usize = 1;
674 while (i <= len) : (i += 1) {
675 _ = c.lua_rawgeti(s, 1, @intCast(i));
676
677 if (c.lua_type(s, -1) != c.LUA_TTABLE) {
678 c.lua_settop(s, -2);
679 continue;
680 }
681
682 if (parse_block_config(s, -1)) |block| {
683 cfg.add_block(block) catch {};
684 }
685
686 c.lua_settop(s, -2);
687 }
688
689 return 0;
690 }
691
692 fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block {
693 _ = c.lua_getfield(state, idx, "__block_type");
694 const block_type_str = get_lua_string(state, -1) orelse {
695 c.lua_settop(state, -2);
696 return null;
697 };
698 c.lua_settop(state, -2);
699
700 _ = c.lua_getfield(state, idx, "format");
701 const format = get_lua_string(state, -1) orelse "";
702 c.lua_settop(state, -2);
703
704 _ = c.lua_getfield(state, idx, "interval");
705 const interval: u32 = @intCast(c.lua_tointegerx(state, -1, null));
706 c.lua_settop(state, -2);
707
708 _ = c.lua_getfield(state, idx, "color");
709 const color = parse_color(state, -1);
710 c.lua_settop(state, -2);
711
712 _ = c.lua_getfield(state, idx, "underline");
713 const underline = c.lua_toboolean(state, -1) != 0;
714 c.lua_settop(state, -2);
715
716 var block = Block{
717 .block_type = .static,
718 .format = format,
719 .interval = interval,
720 .color = color,
721 .underline = underline,
722 };
723
724 if (std.mem.eql(u8, block_type_str, "Ram")) {
725 block.block_type = .ram;
726 } else if (std.mem.eql(u8, block_type_str, "DateTime")) {
727 block.block_type = .datetime;
728 _ = c.lua_getfield(state, idx, "__arg");
729 block.datetime_format = get_lua_string(state, -1);
730 c.lua_settop(state, -2);
731 } else if (std.mem.eql(u8, block_type_str, "Shell")) {
732 block.block_type = .shell;
733 _ = c.lua_getfield(state, idx, "__arg");
734 block.command = get_lua_string(state, -1);
735 c.lua_settop(state, -2);
736 } else if (std.mem.eql(u8, block_type_str, "Static")) {
737 block.block_type = .static;
738 _ = c.lua_getfield(state, idx, "__arg");
739 if (get_lua_string(state, -1)) |text| {
740 block.format = text;
741 }
742 c.lua_settop(state, -2);
743 } else if (std.mem.eql(u8, block_type_str, "Battery")) {
744 block.block_type = .battery;
745 _ = c.lua_getfield(state, idx, "__arg");
746 if (c.lua_type(state, -1) == c.LUA_TTABLE) {
747 _ = c.lua_getfield(state, -1, "charging");
748 block.format_charging = get_lua_string(state, -1);
749 c.lua_settop(state, -2);
750
751 _ = c.lua_getfield(state, -1, "discharging");
752 block.format_discharging = get_lua_string(state, -1);
753 c.lua_settop(state, -2);
754
755 _ = c.lua_getfield(state, -1, "full");
756 block.format_full = get_lua_string(state, -1);
757 c.lua_settop(state, -2);
758
759 _ = c.lua_getfield(state, -1, "battery_name");
760 block.battery_name = get_lua_string(state, -1);
761 c.lua_settop(state, -2);
762 }
763 c.lua_settop(state, -2);
764 } else {
765 return null;
766 }
767
768 return block;
769 }
770
771 fn lua_bar_set_scheme_normal(state: ?*c.lua_State) callconv(.c) c_int {
772 const cfg = config orelse return 0;
773 const s = state orelse return 0;
774 cfg.scheme_normal = parse_scheme(s);
775 return 0;
776 }
777
778 fn lua_bar_set_scheme_selected(state: ?*c.lua_State) callconv(.c) c_int {
779 const cfg = config orelse return 0;
780 const s = state orelse return 0;
781 cfg.scheme_selected = parse_scheme(s);
782 return 0;
783 }
784
785 fn lua_bar_set_scheme_occupied(state: ?*c.lua_State) callconv(.c) c_int {
786 const cfg = config orelse return 0;
787 const s = state orelse return 0;
788 cfg.scheme_occupied = parse_scheme(s);
789 return 0;
790 }
791
792 fn lua_bar_set_scheme_urgent(state: ?*c.lua_State) callconv(.c) c_int {
793 const cfg = config orelse return 0;
794 const s = state orelse return 0;
795 cfg.scheme_urgent = parse_scheme(s);
796 return 0;
797 }
798
799 fn lua_bar_set_hide_vacant_tags(state: ?*c.lua_State) callconv(.c) c_int {
800 const cfg = config orelse return 0;
801 const s = state orelse return 0;
802 cfg.hide_vacant_tags = c.lua_toboolean(s, 1) != 0;
803 return 0;
804 }
805
806 fn lua_bar_block_ram(state: ?*c.lua_State) callconv(.c) c_int {
807 const s = state orelse return 0;
808 create_block_table(s, "Ram", null);
809 return 1;
810 }
811
812 fn lua_bar_block_datetime(state: ?*c.lua_State) callconv(.c) c_int {
813 const s = state orelse return 0;
814 _ = c.lua_getfield(s, 1, "date_format");
815 const date_format = get_lua_string(s, -1);
816 c.lua_settop(s, -2);
817 create_block_table(s, "DateTime", date_format);
818 return 1;
819 }
820
821 fn lua_bar_block_shell(state: ?*c.lua_State) callconv(.c) c_int {
822 const s = state orelse return 0;
823 _ = c.lua_getfield(s, 1, "command");
824 const command = get_lua_string(s, -1);
825 c.lua_settop(s, -2);
826 create_block_table(s, "Shell", command);
827 return 1;
828 }
829
830 fn lua_bar_block_static(state: ?*c.lua_State) callconv(.c) c_int {
831 const s = state orelse return 0;
832 _ = c.lua_getfield(s, 1, "text");
833 const text = get_lua_string(s, -1);
834 c.lua_settop(s, -2);
835 create_block_table(s, "Static", text);
836 return 1;
837 }
838
839 fn lua_bar_block_battery(state: ?*c.lua_State) callconv(.c) c_int {
840 const s = state orelse return 0;
841
842 c.lua_createtable(s, 0, 6);
843
844 _ = c.lua_pushstring(s, "Battery");
845 c.lua_setfield(s, -2, "__block_type");
846
847 _ = c.lua_getfield(s, 1, "format");
848 c.lua_setfield(s, -2, "format");
849
850 _ = c.lua_getfield(s, 1, "interval");
851 c.lua_setfield(s, -2, "interval");
852
853 _ = c.lua_getfield(s, 1, "color");
854 c.lua_setfield(s, -2, "color");
855
856 _ = c.lua_getfield(s, 1, "underline");
857 c.lua_setfield(s, -2, "underline");
858
859 c.lua_createtable(s, 0, 4);
860 _ = c.lua_getfield(s, 1, "charging");
861 c.lua_setfield(s, -2, "charging");
862 _ = c.lua_getfield(s, 1, "discharging");
863 c.lua_setfield(s, -2, "discharging");
864 _ = c.lua_getfield(s, 1, "full");
865 c.lua_setfield(s, -2, "full");
866 _ = c.lua_getfield(s, 1, "battery_name");
867 c.lua_setfield(s, -2, "battery_name");
868 c.lua_setfield(s, -2, "__arg");
869
870 return 1;
871 }
872
873 fn create_block_table(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]const u8) void {
874 c.lua_createtable(state, 0, 6);
875
876 _ = c.lua_pushstring(state, block_type);
877 c.lua_setfield(state, -2, "__block_type");
878
879 _ = c.lua_getfield(state, 1, "format");
880 c.lua_setfield(state, -2, "format");
881
882 _ = c.lua_getfield(state, 1, "interval");
883 c.lua_setfield(state, -2, "interval");
884
885 _ = c.lua_getfield(state, 1, "color");
886 c.lua_setfield(state, -2, "color");
887
888 _ = c.lua_getfield(state, 1, "underline");
889 c.lua_setfield(state, -2, "underline");
890
891 if (arg) |a| {
892 var buf: [256]u8 = undefined;
893 if (a.len < buf.len) {
894 @memcpy(buf[0..a.len], a);
895 buf[a.len] = 0;
896 _ = c.lua_pushstring(state, &buf);
897 c.lua_setfield(state, -2, "__arg");
898 }
899 }
900 }
901
902 fn lua_set_terminal(state: ?*c.lua_State) callconv(.c) c_int {
903 const cfg = config orelse return 0;
904 const s = state orelse return 0;
905 if (get_string_arg(s, 1)) |term| {
906 cfg.terminal = term;
907 }
908 return 0;
909 }
910
911 fn lua_set_modkey(state: ?*c.lua_State) callconv(.c) c_int {
912 const cfg = config orelse return 0;
913 const s = state orelse return 0;
914 if (get_string_arg(s, 1)) |modkey_str| {
915 cfg.modkey = parse_single_modifier(modkey_str);
916 }
917 return 0;
918 }
919
920 fn lua_set_tags(state: ?*c.lua_State) callconv(.c) c_int {
921 const cfg = config orelse return 0;
922 const s = state orelse return 0;
923
924 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
925
926 const len = c.lua_rawlen(s, 1);
927 var i: usize = 0;
928 while (i < len and i < 9) : (i += 1) {
929 _ = c.lua_rawgeti(s, 1, @intCast(i + 1));
930 if (get_lua_string(s, -1)) |tag_str| {
931 cfg.tags[i] = tag_str;
932 }
933 c.lua_settop(s, -2);
934 }
935
936 return 0;
937 }
938
939 fn lua_autostart(state: ?*c.lua_State) callconv(.c) c_int {
940 const cfg = config orelse return 0;
941 const s = state orelse return 0;
942 if (get_string_arg(s, 1)) |cmd| {
943 cfg.add_autostart(cmd) catch return 0;
944 }
945 return 0;
946 }
947
948 fn lua_auto_tile(state: ?*c.lua_State) callconv(.c) c_int {
949 const cfg = config orelse return 0;
950 const s = state orelse return 0;
951 cfg.auto_tile = c.lua_toboolean(s, 1) != 0;
952 return 0;
953 }
954
955 fn lua_quit(state: ?*c.lua_State) callconv(.c) c_int {
956 const s = state orelse return 0;
957 create_action_table(s, "Quit");
958 return 1;
959 }
960
961 fn lua_restart(state: ?*c.lua_State) callconv(.c) c_int {
962 const s = state orelse return 0;
963 create_action_table(s, "Restart");
964 return 1;
965 }
966
967 fn lua_toggle_gaps(state: ?*c.lua_State) callconv(.c) c_int {
968 const s = state orelse return 0;
969 create_action_table(s, "ToggleGaps");
970 return 1;
971 }
972
973 fn lua_set_master_factor(state: ?*c.lua_State) callconv(.c) c_int {
974 const s = state orelse return 0;
975 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
976 create_action_table_with_int(s, "ResizeMaster", delta);
977 return 1;
978 }
979
980 fn lua_inc_num_master(state: ?*c.lua_State) callconv(.c) c_int {
981 const s = state orelse return 0;
982 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
983 if (delta > 0) {
984 create_action_table(s, "IncMaster");
985 } else {
986 create_action_table(s, "DecMaster");
987 }
988 return 1;
989 }
990
991 fn get_string_arg(state: *c.lua_State, idx: c_int) ?[]const u8 {
992 if (c.lua_type(state, idx) != c.LUA_TSTRING) return null;
993 return get_lua_string(state, idx);
994 }
995
996 fn get_lua_string(state: *c.lua_State, idx: c_int) ?[]const u8 {
997 const cstr = c.lua_tolstring(state, idx, null);
998 if (cstr == null) return null;
999 return std.mem.span(cstr);
1000 }
1001
1002 fn parse_color(state: *c.lua_State, idx: c_int) u32 {
1003 const lua_type = c.lua_type(state, idx);
1004 if (lua_type == c.LUA_TNUMBER) {
1005 return @intCast(c.lua_tointegerx(state, idx, null));
1006 }
1007 if (lua_type == c.LUA_TSTRING) {
1008 const str = get_lua_string(state, idx) orelse return 0;
1009 if (str.len > 0 and str[0] == '#') {
1010 return std.fmt.parseInt(u32, str[1..], 16) catch return 0;
1011 }
1012 if (str.len > 2 and str[0] == '0' and str[1] == 'x') {
1013 return std.fmt.parseInt(u32, str[2..], 16) catch return 0;
1014 }
1015 return std.fmt.parseInt(u32, str, 16) catch return 0;
1016 }
1017 return 0;
1018 }
1019
1020 fn parse_scheme(state: *c.lua_State) Color_Scheme {
1021 return Color_Scheme{
1022 .fg = parse_color(state, 1),
1023 .bg = parse_color(state, 2),
1024 .border = parse_color(state, 3),
1025 };
1026 }
1027
1028 fn parse_modifiers(state: *c.lua_State, idx: c_int) u32 {
1029 var mod_mask: u32 = 0;
1030
1031 if (c.lua_type(state, idx) != c.LUA_TTABLE) return mod_mask;
1032
1033 const len = c.lua_rawlen(state, idx);
1034 var i: usize = 1;
1035 while (i <= len) : (i += 1) {
1036 _ = c.lua_rawgeti(state, idx, @intCast(i));
1037 if (get_lua_string(state, -1)) |mod_str| {
1038 mod_mask |= parse_single_modifier(mod_str);
1039 }
1040 c.lua_settop(state, -2);
1041 }
1042
1043 return mod_mask;
1044 }
1045
1046 fn parse_single_modifier(name: []const u8) u32 {
1047 if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) {
1048 return (1 << 6);
1049 } else if (std.mem.eql(u8, name, "Mod1") or std.mem.eql(u8, name, "mod1") or std.mem.eql(u8, name, "alt")) {
1050 return (1 << 3);
1051 } else if (std.mem.eql(u8, name, "Shift") or std.mem.eql(u8, name, "shift")) {
1052 return (1 << 0);
1053 } else if (std.mem.eql(u8, name, "Control") or std.mem.eql(u8, name, "control") or std.mem.eql(u8, name, "ctrl")) {
1054 return (1 << 2);
1055 }
1056 return 0;
1057 }
1058
1059 fn parse_action(name: []const u8) ?Action {
1060 const action_map = .{
1061 .{ "Spawn", Action.spawn },
1062 .{ "SpawnTerminal", Action.spawn_terminal },
1063 .{ "KillClient", Action.kill_client },
1064 .{ "Quit", Action.quit },
1065 .{ "Restart", Action.restart },
1066 .{ "FocusStack", Action.focus_next },
1067 .{ "MoveStack", Action.move_next },
1068 .{ "ResizeMaster", Action.resize_master },
1069 .{ "IncMaster", Action.inc_master },
1070 .{ "DecMaster", Action.dec_master },
1071 .{ "ToggleFloating", Action.toggle_floating },
1072 .{ "ToggleFullScreen", Action.toggle_fullscreen },
1073 .{ "ToggleGaps", Action.toggle_gaps },
1074 .{ "CycleLayout", Action.cycle_layout },
1075 .{ "ChangeLayout", Action.set_layout },
1076 .{ "ViewTag", Action.view_tag },
1077 .{ "ViewNextTag", Action.view_next_tag },
1078 .{ "ViewPreviousTag", Action.view_prev_tag },
1079 .{ "ViewNextNonEmptyTag", Action.view_next_nonempty_tag },
1080 .{ "ViewPreviousNonEmptyTag", Action.view_prev_nonempty_tag },
1081 .{ "MoveToTag", Action.move_to_tag },
1082 .{ "ToggleView", Action.toggle_view_tag },
1083 .{ "ToggleTag", Action.toggle_tag },
1084 .{ "FocusMonitor", Action.focus_monitor },
1085 .{ "TagMonitor", Action.send_to_monitor },
1086 .{ "ScrollLeft", Action.scroll_left },
1087 .{ "ScrollRight", Action.scroll_right },
1088 };
1089
1090 inline for (action_map) |entry| {
1091 if (std.mem.eql(u8, name, entry[0])) {
1092 return entry[1];
1093 }
1094 }
1095 return null;
1096 }
1097
1098 fn key_name_to_keysym(name: []const u8) ?u64 {
1099 const key_map = .{
1100 .{ "Return", 0xff0d },
1101 .{ "Enter", 0xff0d },
1102 .{ "Tab", 0xff09 },
1103 .{ "Escape", 0xff1b },
1104 .{ "BackSpace", 0xff08 },
1105 .{ "Delete", 0xffff },
1106 .{ "space", 0x0020 },
1107 .{ "Space", 0x0020 },
1108 .{ "comma", 0x002c },
1109 .{ "Comma", 0x002c },
1110 .{ "period", 0x002e },
1111 .{ "Period", 0x002e },
1112 .{ "slash", 0x002f },
1113 .{ "Slash", 0x002f },
1114 .{ "minus", 0x002d },
1115 .{ "Minus", 0x002d },
1116 .{ "equal", 0x003d },
1117 .{ "Equal", 0x003d },
1118 .{ "bracketleft", 0x005b },
1119 .{ "bracketright", 0x005d },
1120 .{ "backslash", 0x005c },
1121 .{ "semicolon", 0x003b },
1122 .{ "apostrophe", 0x0027 },
1123 .{ "grave", 0x0060 },
1124 .{ "Left", 0xff51 },
1125 .{ "Up", 0xff52 },
1126 .{ "Right", 0xff53 },
1127 .{ "Down", 0xff54 },
1128 .{ "F1", 0xffbe },
1129 .{ "F2", 0xffbf },
1130 .{ "F3", 0xffc0 },
1131 .{ "F4", 0xffc1 },
1132 .{ "F5", 0xffc2 },
1133 .{ "F6", 0xffc3 },
1134 .{ "F7", 0xffc4 },
1135 .{ "F8", 0xffc5 },
1136 .{ "F9", 0xffc6 },
1137 .{ "F10", 0xffc7 },
1138 .{ "F11", 0xffc8 },
1139 .{ "F12", 0xffc9 },
1140 .{ "Print", 0xff61 },
1141 .{ "XF86AudioRaiseVolume", 0x1008ff13 },
1142 .{ "XF86AudioLowerVolume", 0x1008ff11 },
1143 .{ "XF86AudioMute", 0x1008ff12 },
1144 .{ "XF86AudioPlay", 0x1008ff14 },
1145 .{ "XF86AudioPause", 0x1008ff31 },
1146 .{ "XF86AudioNext", 0x1008ff17 },
1147 .{ "XF86AudioPrev", 0x1008ff16 },
1148 .{ "XF86MonBrightnessUp", 0x1008ff02 },
1149 .{ "XF86MonBrightnessDown", 0x1008ff03 },
1150 };
1151
1152 inline for (key_map) |entry| {
1153 if (std.mem.eql(u8, name, entry[0])) {
1154 return entry[1];
1155 }
1156 }
1157
1158 if (name.len == 1) {
1159 const char = name[0];
1160 if (char >= 'a' and char <= 'z') {
1161 return char;
1162 }
1163 if (char >= 'A' and char <= 'Z') {
1164 return char + 32;
1165 }
1166 if (char >= '0' and char <= '9') {
1167 return char;
1168 }
1169 }
1170
1171 return null;
1172 }