tony / oxwm

Branch: 6592028
Switch branches/tags
master
tonybanters added tests, overlay, keychords. path to 100% compliance is en route. 2bbe427 2026-02-04
1281 lines · 37.5 KB Raw Blame History
1 const std = @import("std");
2 pub 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_set_layout_symbol);
304 c.lua_setfield(state, -2, "set_layout_symbol");
305
306 c.lua_pushcfunction(state, lua_autostart);
307 c.lua_setfield(state, -2, "autostart");
308
309 c.lua_pushcfunction(state, lua_auto_tile);
310 c.lua_setfield(state, -2, "auto_tile");
311
312 c.lua_pushcfunction(state, lua_quit);
313 c.lua_setfield(state, -2, "quit");
314
315 c.lua_pushcfunction(state, lua_restart);
316 c.lua_setfield(state, -2, "restart");
317
318 c.lua_pushcfunction(state, lua_toggle_gaps);
319 c.lua_setfield(state, -2, "toggle_gaps");
320
321 c.lua_pushcfunction(state, lua_show_keybinds);
322 c.lua_setfield(state, -2, "show_keybinds");
323
324 c.lua_pushcfunction(state, lua_set_master_factor);
325 c.lua_setfield(state, -2, "set_master_factor");
326
327 c.lua_pushcfunction(state, lua_inc_num_master);
328 c.lua_setfield(state, -2, "inc_num_master");
329 }
330
331 fn create_action_table(state: *c.lua_State, action_name: [*:0]const u8) void {
332 c.lua_createtable(state, 0, 2);
333 _ = c.lua_pushstring(state, action_name);
334 c.lua_setfield(state, -2, "__action");
335 }
336
337 fn create_action_table_with_int(state: *c.lua_State, action_name: [*:0]const u8, arg: i32) void {
338 c.lua_createtable(state, 0, 2);
339 _ = c.lua_pushstring(state, action_name);
340 c.lua_setfield(state, -2, "__action");
341 c.lua_pushinteger(state, arg);
342 c.lua_setfield(state, -2, "__arg");
343 }
344
345 fn create_action_table_with_string(state: *c.lua_State, action_name: [*:0]const u8) void {
346 c.lua_createtable(state, 0, 2);
347 _ = c.lua_pushstring(state, action_name);
348 c.lua_setfield(state, -2, "__action");
349 c.lua_pushvalue(state, 1);
350 c.lua_setfield(state, -2, "__arg");
351 }
352
353 fn lua_spawn(state: ?*c.lua_State) callconv(.c) c_int {
354 const s = state orelse return 0;
355 create_action_table_with_string(s, "Spawn");
356 return 1;
357 }
358
359 fn lua_spawn_terminal(state: ?*c.lua_State) callconv(.c) c_int {
360 const s = state orelse return 0;
361 create_action_table(s, "SpawnTerminal");
362 return 1;
363 }
364
365 fn lua_key_bind(state: ?*c.lua_State) callconv(.c) c_int {
366 const s = state orelse return 0;
367 const cfg = config orelse return 0;
368
369 const mod_mask = parse_modifiers(s, 1);
370 const key_str = get_string_arg(s, 2) orelse return 0;
371 const keysym = key_name_to_keysym(key_str) orelse return 0;
372
373 if (c.lua_type(s, 3) != c.LUA_TTABLE) return 0;
374
375 _ = c.lua_getfield(s, 3, "__action");
376 const action_str = get_lua_string(s, -1) orelse {
377 c.lua_settop(s, -2);
378 return 0;
379 };
380 c.lua_settop(s, -2);
381
382 const action = parse_action(action_str) orelse return 0;
383
384 var int_arg: i32 = 0;
385 var str_arg: ?[]const u8 = null;
386
387 _ = c.lua_getfield(s, 3, "__arg");
388 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
389 int_arg = @intCast(c.lua_tointegerx(s, -1, null));
390 } else if (c.lua_type(s, -1) == c.LUA_TSTRING) {
391 str_arg = get_lua_string(s, -1);
392 }
393 c.lua_settop(s, -2);
394
395 var keybind: config_mod.Keybind = .{
396 .action = action,
397 .int_arg = int_arg,
398 .str_arg = str_arg,
399 };
400 keybind.keys[0] = .{ .mod_mask = mod_mask, .keysym = keysym };
401 keybind.key_count = 1;
402
403 cfg.add_keybind(keybind) catch return 0;
404
405 return 0;
406 }
407
408 fn lua_key_chord(state: ?*c.lua_State) callconv(.c) c_int {
409 const s = state orelse return 0;
410 const cfg = config orelse return 0;
411
412 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
413 if (c.lua_type(s, 2) != c.LUA_TTABLE) return 0;
414
415 var keybind: config_mod.Keybind = .{
416 .action = .quit,
417 .int_arg = 0,
418 .str_arg = null,
419 };
420 keybind.key_count = 0;
421
422 const num_keys = c.lua_rawlen(s, 1);
423 if (num_keys == 0 or num_keys > 4) return 0;
424
425 var i: usize = 1;
426 while (i <= num_keys) : (i += 1) {
427 _ = c.lua_rawgeti(s, 1, @intCast(i));
428 if (c.lua_type(s, -1) != c.LUA_TTABLE) {
429 c.lua_settop(s, -2);
430 return 0;
431 }
432
433 _ = c.lua_rawgeti(s, -1, 1);
434 const mod_mask = parse_modifiers_at_top(s);
435 c.lua_settop(s, -2);
436
437 _ = c.lua_rawgeti(s, -1, 2);
438 const key_str = get_lua_string(s, -1) orelse {
439 c.lua_settop(s, -3);
440 return 0;
441 };
442 c.lua_settop(s, -2);
443
444 const keysym = key_name_to_keysym(key_str) orelse {
445 c.lua_settop(s, -2);
446 return 0;
447 };
448
449 keybind.keys[keybind.key_count] = .{ .mod_mask = mod_mask, .keysym = keysym };
450 keybind.key_count += 1;
451
452 c.lua_settop(s, -2);
453 }
454
455 _ = c.lua_getfield(s, 2, "__action");
456 const action_str = get_lua_string(s, -1) orelse {
457 c.lua_settop(s, -2);
458 return 0;
459 };
460 c.lua_settop(s, -2);
461
462 keybind.action = parse_action(action_str) orelse return 0;
463
464 _ = c.lua_getfield(s, 2, "__arg");
465 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
466 keybind.int_arg = @intCast(c.lua_tointegerx(s, -1, null));
467 } else if (c.lua_type(s, -1) == c.LUA_TSTRING) {
468 keybind.str_arg = get_lua_string(s, -1);
469 }
470 c.lua_settop(s, -2);
471
472 cfg.add_keybind(keybind) catch return 0;
473
474 return 0;
475 }
476
477 fn lua_gaps_set_enabled(state: ?*c.lua_State) callconv(.c) c_int {
478 const cfg = config orelse return 0;
479 const s = state orelse return 0;
480 cfg.gaps_enabled = c.lua_toboolean(s, 1) != 0;
481 return 0;
482 }
483
484 fn lua_gaps_enable(state: ?*c.lua_State) callconv(.c) c_int {
485 _ = state;
486 const cfg = config orelse return 0;
487 cfg.gaps_enabled = true;
488 return 0;
489 }
490
491 fn lua_gaps_disable(state: ?*c.lua_State) callconv(.c) c_int {
492 _ = state;
493 const cfg = config orelse return 0;
494 cfg.gaps_enabled = false;
495 return 0;
496 }
497
498 fn lua_gaps_set_inner(state: ?*c.lua_State) callconv(.c) c_int {
499 const cfg = config orelse return 0;
500 const s = state orelse return 0;
501 cfg.gap_inner_h = @intCast(c.lua_tointegerx(s, 1, null));
502 cfg.gap_inner_v = @intCast(c.lua_tointegerx(s, 2, null));
503 return 0;
504 }
505
506 fn lua_gaps_set_outer(state: ?*c.lua_State) callconv(.c) c_int {
507 const cfg = config orelse return 0;
508 const s = state orelse return 0;
509 cfg.gap_outer_h = @intCast(c.lua_tointegerx(s, 1, null));
510 cfg.gap_outer_v = @intCast(c.lua_tointegerx(s, 2, null));
511 return 0;
512 }
513
514 fn lua_gaps_set_smart(state: ?*c.lua_State) callconv(.c) c_int {
515 const cfg = config orelse return 0;
516 const s = state orelse return 0;
517 cfg.smartgaps_enabled = c.lua_toboolean(s, 1) != 0;
518 return 0;
519 }
520
521 fn lua_border_set_width(state: ?*c.lua_State) callconv(.c) c_int {
522 const cfg = config orelse return 0;
523 const s = state orelse return 0;
524 cfg.border_width = @intCast(c.lua_tointegerx(s, 1, null));
525 return 0;
526 }
527
528 fn lua_border_set_focused_color(state: ?*c.lua_State) callconv(.c) c_int {
529 const cfg = config orelse return 0;
530 const s = state orelse return 0;
531 cfg.border_focused = parse_color(s, 1);
532 return 0;
533 }
534
535 fn lua_border_set_unfocused_color(state: ?*c.lua_State) callconv(.c) c_int {
536 const cfg = config orelse return 0;
537 const s = state orelse return 0;
538 cfg.border_unfocused = parse_color(s, 1);
539 return 0;
540 }
541
542 fn lua_client_kill(state: ?*c.lua_State) callconv(.c) c_int {
543 const s = state orelse return 0;
544 create_action_table(s, "KillClient");
545 return 1;
546 }
547
548 fn lua_client_toggle_fullscreen(state: ?*c.lua_State) callconv(.c) c_int {
549 const s = state orelse return 0;
550 create_action_table(s, "ToggleFullScreen");
551 return 1;
552 }
553
554 fn lua_client_toggle_floating(state: ?*c.lua_State) callconv(.c) c_int {
555 const s = state orelse return 0;
556 create_action_table(s, "ToggleFloating");
557 return 1;
558 }
559
560 fn lua_client_focus_stack(state: ?*c.lua_State) callconv(.c) c_int {
561 const s = state orelse return 0;
562 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
563 create_action_table_with_int(s, "FocusStack", dir);
564 return 1;
565 }
566
567 fn lua_client_move_stack(state: ?*c.lua_State) callconv(.c) c_int {
568 const s = state orelse return 0;
569 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
570 create_action_table_with_int(s, "MoveStack", dir);
571 return 1;
572 }
573
574 fn lua_layout_cycle(state: ?*c.lua_State) callconv(.c) c_int {
575 const s = state orelse return 0;
576 create_action_table(s, "CycleLayout");
577 return 1;
578 }
579
580 fn lua_layout_set(state: ?*c.lua_State) callconv(.c) c_int {
581 const s = state orelse return 0;
582 create_action_table_with_string(s, "ChangeLayout");
583 return 1;
584 }
585
586 fn lua_layout_scroll_left(state: ?*c.lua_State) callconv(.c) c_int {
587 const s = state orelse return 0;
588 create_action_table(s, "ScrollLeft");
589 return 1;
590 }
591
592 fn lua_layout_scroll_right(state: ?*c.lua_State) callconv(.c) c_int {
593 const s = state orelse return 0;
594 create_action_table(s, "ScrollRight");
595 return 1;
596 }
597
598 fn lua_tag_view(state: ?*c.lua_State) callconv(.c) c_int {
599 const s = state orelse return 0;
600 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
601 create_action_table_with_int(s, "ViewTag", idx);
602 return 1;
603 }
604
605 fn lua_tag_view_next(state: ?*c.lua_State) callconv(.c) c_int {
606 const s = state orelse return 0;
607 create_action_table(s, "ViewNextTag");
608 return 1;
609 }
610
611 fn lua_tag_view_previous(state: ?*c.lua_State) callconv(.c) c_int {
612 const s = state orelse return 0;
613 create_action_table(s, "ViewPreviousTag");
614 return 1;
615 }
616
617 fn lua_tag_view_next_nonempty(state: ?*c.lua_State) callconv(.c) c_int {
618 const s = state orelse return 0;
619 create_action_table(s, "ViewNextNonEmptyTag");
620 return 1;
621 }
622
623 fn lua_tag_view_previous_nonempty(state: ?*c.lua_State) callconv(.c) c_int {
624 const s = state orelse return 0;
625 create_action_table(s, "ViewPreviousNonEmptyTag");
626 return 1;
627 }
628
629 fn lua_tag_toggleview(state: ?*c.lua_State) callconv(.c) c_int {
630 const s = state orelse return 0;
631 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
632 create_action_table_with_int(s, "ToggleView", idx);
633 return 1;
634 }
635
636 fn lua_tag_move_to(state: ?*c.lua_State) callconv(.c) c_int {
637 const s = state orelse return 0;
638 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
639 create_action_table_with_int(s, "MoveToTag", idx);
640 return 1;
641 }
642
643 fn lua_tag_toggletag(state: ?*c.lua_State) callconv(.c) c_int {
644 const s = state orelse return 0;
645 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
646 create_action_table_with_int(s, "ToggleTag", idx);
647 return 1;
648 }
649
650 fn lua_tag_set_back_and_forth(state: ?*c.lua_State) callconv(.c) c_int {
651 const cfg = config orelse return 0;
652 const s = state orelse return 0;
653 cfg.tag_back_and_forth = c.lua_toboolean(s, 1) != 0;
654 return 0;
655 }
656
657 fn lua_monitor_focus(state: ?*c.lua_State) callconv(.c) c_int {
658 const s = state orelse return 0;
659 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
660 create_action_table_with_int(s, "FocusMonitor", dir);
661 return 1;
662 }
663
664 fn lua_monitor_tag(state: ?*c.lua_State) callconv(.c) c_int {
665 const s = state orelse return 0;
666 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
667 create_action_table_with_int(s, "TagMonitor", dir);
668 return 1;
669 }
670
671 fn lua_rule_add(state: ?*c.lua_State) callconv(.c) c_int {
672 const cfg = config orelse return 0;
673 const s = state orelse return 0;
674
675 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
676
677 var rule = Rule{
678 .class = null,
679 .instance = null,
680 .title = null,
681 .tags = 0,
682 .is_floating = false,
683 .monitor = -1,
684 };
685
686 _ = c.lua_getfield(s, 1, "class");
687 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
688 rule.class = get_lua_string(s, -1);
689 }
690 c.lua_settop(s, -2);
691
692 _ = c.lua_getfield(s, 1, "instance");
693 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
694 rule.instance = get_lua_string(s, -1);
695 }
696 c.lua_settop(s, -2);
697
698 _ = c.lua_getfield(s, 1, "title");
699 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
700 rule.title = get_lua_string(s, -1);
701 }
702 c.lua_settop(s, -2);
703
704 _ = c.lua_getfield(s, 1, "tag");
705 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
706 const tag_idx: i32 = @intCast(c.lua_tointegerx(s, -1, null));
707 if (tag_idx > 0) {
708 rule.tags = @as(u32, 1) << @intCast(tag_idx - 1);
709 }
710 }
711 c.lua_settop(s, -2);
712
713 _ = c.lua_getfield(s, 1, "floating");
714 if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) {
715 rule.is_floating = c.lua_toboolean(s, -1) != 0;
716 }
717 c.lua_settop(s, -2);
718
719 _ = c.lua_getfield(s, 1, "monitor");
720 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
721 rule.monitor = @intCast(c.lua_tointegerx(s, -1, null));
722 }
723 c.lua_settop(s, -2);
724
725 cfg.add_rule(rule) catch return 0;
726 return 0;
727 }
728
729 fn lua_bar_set_font(state: ?*c.lua_State) callconv(.c) c_int {
730 const cfg = config orelse return 0;
731 const s = state orelse return 0;
732 if (get_string_arg(s, 1)) |font| {
733 cfg.font = font;
734 }
735 return 0;
736 }
737
738 fn lua_bar_set_blocks(state: ?*c.lua_State) callconv(.c) c_int {
739 const cfg = config orelse return 0;
740 const s = state orelse return 0;
741
742 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
743
744 const len = c.lua_rawlen(s, 1);
745 var i: usize = 1;
746 while (i <= len) : (i += 1) {
747 _ = c.lua_rawgeti(s, 1, @intCast(i));
748
749 if (c.lua_type(s, -1) != c.LUA_TTABLE) {
750 c.lua_settop(s, -2);
751 continue;
752 }
753
754 if (parse_block_config(s, -1)) |block| {
755 cfg.add_block(block) catch {};
756 }
757
758 c.lua_settop(s, -2);
759 }
760
761 return 0;
762 }
763
764 fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block {
765 _ = c.lua_getfield(state, idx, "__block_type");
766 const block_type_str = get_lua_string(state, -1) orelse {
767 c.lua_settop(state, -2);
768 return null;
769 };
770 c.lua_settop(state, -2);
771
772 _ = c.lua_getfield(state, idx, "format");
773 const format = get_lua_string(state, -1) orelse "";
774 c.lua_settop(state, -2);
775
776 _ = c.lua_getfield(state, idx, "interval");
777 const interval: u32 = @intCast(c.lua_tointegerx(state, -1, null));
778 c.lua_settop(state, -2);
779
780 _ = c.lua_getfield(state, idx, "color");
781 const color = parse_color(state, -1);
782 c.lua_settop(state, -2);
783
784 _ = c.lua_getfield(state, idx, "underline");
785 const underline = c.lua_toboolean(state, -1) != 0;
786 c.lua_settop(state, -2);
787
788 var block = Block{
789 .block_type = .static,
790 .format = format,
791 .interval = interval,
792 .color = color,
793 .underline = underline,
794 };
795
796 if (std.mem.eql(u8, block_type_str, "Ram")) {
797 block.block_type = .ram;
798 } else if (std.mem.eql(u8, block_type_str, "DateTime")) {
799 block.block_type = .datetime;
800 _ = c.lua_getfield(state, idx, "__arg");
801 block.datetime_format = get_lua_string(state, -1);
802 c.lua_settop(state, -2);
803 } else if (std.mem.eql(u8, block_type_str, "Shell")) {
804 block.block_type = .shell;
805 _ = c.lua_getfield(state, idx, "__arg");
806 block.command = get_lua_string(state, -1);
807 c.lua_settop(state, -2);
808 } else if (std.mem.eql(u8, block_type_str, "Static")) {
809 block.block_type = .static;
810 _ = c.lua_getfield(state, idx, "__arg");
811 if (get_lua_string(state, -1)) |text| {
812 block.format = text;
813 }
814 c.lua_settop(state, -2);
815 } else if (std.mem.eql(u8, block_type_str, "Battery")) {
816 block.block_type = .battery;
817 _ = c.lua_getfield(state, idx, "__arg");
818 if (c.lua_type(state, -1) == c.LUA_TTABLE) {
819 _ = c.lua_getfield(state, -1, "charging");
820 block.format_charging = get_lua_string(state, -1);
821 c.lua_settop(state, -2);
822
823 _ = c.lua_getfield(state, -1, "discharging");
824 block.format_discharging = get_lua_string(state, -1);
825 c.lua_settop(state, -2);
826
827 _ = c.lua_getfield(state, -1, "full");
828 block.format_full = get_lua_string(state, -1);
829 c.lua_settop(state, -2);
830
831 _ = c.lua_getfield(state, -1, "battery_name");
832 block.battery_name = get_lua_string(state, -1);
833 c.lua_settop(state, -2);
834 }
835 c.lua_settop(state, -2);
836 } else {
837 return null;
838 }
839
840 return block;
841 }
842
843 fn lua_bar_set_scheme_normal(state: ?*c.lua_State) callconv(.c) c_int {
844 const cfg = config orelse return 0;
845 const s = state orelse return 0;
846 cfg.scheme_normal = parse_scheme(s);
847 return 0;
848 }
849
850 fn lua_bar_set_scheme_selected(state: ?*c.lua_State) callconv(.c) c_int {
851 const cfg = config orelse return 0;
852 const s = state orelse return 0;
853 cfg.scheme_selected = parse_scheme(s);
854 return 0;
855 }
856
857 fn lua_bar_set_scheme_occupied(state: ?*c.lua_State) callconv(.c) c_int {
858 const cfg = config orelse return 0;
859 const s = state orelse return 0;
860 cfg.scheme_occupied = parse_scheme(s);
861 return 0;
862 }
863
864 fn lua_bar_set_scheme_urgent(state: ?*c.lua_State) callconv(.c) c_int {
865 const cfg = config orelse return 0;
866 const s = state orelse return 0;
867 cfg.scheme_urgent = parse_scheme(s);
868 return 0;
869 }
870
871 fn lua_bar_set_hide_vacant_tags(state: ?*c.lua_State) callconv(.c) c_int {
872 const cfg = config orelse return 0;
873 const s = state orelse return 0;
874 cfg.hide_vacant_tags = c.lua_toboolean(s, 1) != 0;
875 return 0;
876 }
877
878 fn lua_bar_block_ram(state: ?*c.lua_State) callconv(.c) c_int {
879 const s = state orelse return 0;
880 create_block_table(s, "Ram", null);
881 return 1;
882 }
883
884 fn lua_bar_block_datetime(state: ?*c.lua_State) callconv(.c) c_int {
885 const s = state orelse return 0;
886 _ = c.lua_getfield(s, 1, "date_format");
887 const date_format = get_lua_string(s, -1);
888 c.lua_settop(s, -2);
889 create_block_table(s, "DateTime", date_format);
890 return 1;
891 }
892
893 fn lua_bar_block_shell(state: ?*c.lua_State) callconv(.c) c_int {
894 const s = state orelse return 0;
895 _ = c.lua_getfield(s, 1, "command");
896 const command = get_lua_string(s, -1);
897 c.lua_settop(s, -2);
898 create_block_table(s, "Shell", command);
899 return 1;
900 }
901
902 fn lua_bar_block_static(state: ?*c.lua_State) callconv(.c) c_int {
903 const s = state orelse return 0;
904 _ = c.lua_getfield(s, 1, "text");
905 const text = get_lua_string(s, -1);
906 c.lua_settop(s, -2);
907 create_block_table(s, "Static", text);
908 return 1;
909 }
910
911 fn lua_bar_block_battery(state: ?*c.lua_State) callconv(.c) c_int {
912 const s = state orelse return 0;
913
914 c.lua_createtable(s, 0, 6);
915
916 _ = c.lua_pushstring(s, "Battery");
917 c.lua_setfield(s, -2, "__block_type");
918
919 _ = c.lua_getfield(s, 1, "format");
920 c.lua_setfield(s, -2, "format");
921
922 _ = c.lua_getfield(s, 1, "interval");
923 c.lua_setfield(s, -2, "interval");
924
925 _ = c.lua_getfield(s, 1, "color");
926 c.lua_setfield(s, -2, "color");
927
928 _ = c.lua_getfield(s, 1, "underline");
929 c.lua_setfield(s, -2, "underline");
930
931 c.lua_createtable(s, 0, 4);
932 _ = c.lua_getfield(s, 1, "charging");
933 c.lua_setfield(s, -2, "charging");
934 _ = c.lua_getfield(s, 1, "discharging");
935 c.lua_setfield(s, -2, "discharging");
936 _ = c.lua_getfield(s, 1, "full");
937 c.lua_setfield(s, -2, "full");
938 _ = c.lua_getfield(s, 1, "battery_name");
939 c.lua_setfield(s, -2, "battery_name");
940 c.lua_setfield(s, -2, "__arg");
941
942 return 1;
943 }
944
945 fn create_block_table(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]const u8) void {
946 c.lua_createtable(state, 0, 6);
947
948 _ = c.lua_pushstring(state, block_type);
949 c.lua_setfield(state, -2, "__block_type");
950
951 _ = c.lua_getfield(state, 1, "format");
952 c.lua_setfield(state, -2, "format");
953
954 _ = c.lua_getfield(state, 1, "interval");
955 c.lua_setfield(state, -2, "interval");
956
957 _ = c.lua_getfield(state, 1, "color");
958 c.lua_setfield(state, -2, "color");
959
960 _ = c.lua_getfield(state, 1, "underline");
961 c.lua_setfield(state, -2, "underline");
962
963 if (arg) |a| {
964 var buf: [256]u8 = undefined;
965 if (a.len < buf.len) {
966 @memcpy(buf[0..a.len], a);
967 buf[a.len] = 0;
968 _ = c.lua_pushstring(state, &buf);
969 c.lua_setfield(state, -2, "__arg");
970 }
971 }
972 }
973
974 fn lua_set_terminal(state: ?*c.lua_State) callconv(.c) c_int {
975 const cfg = config orelse return 0;
976 const s = state orelse return 0;
977 if (get_string_arg(s, 1)) |term| {
978 cfg.terminal = term;
979 }
980 return 0;
981 }
982
983 fn lua_set_modkey(state: ?*c.lua_State) callconv(.c) c_int {
984 const cfg = config orelse return 0;
985 const s = state orelse return 0;
986 if (get_string_arg(s, 1)) |modkey_str| {
987 cfg.modkey = parse_single_modifier(modkey_str);
988 }
989 return 0;
990 }
991
992 fn lua_set_tags(state: ?*c.lua_State) callconv(.c) c_int {
993 const cfg = config orelse return 0;
994 const s = state orelse return 0;
995
996 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
997
998 const len = c.lua_rawlen(s, 1);
999 var i: usize = 0;
1000 while (i < len and i < 9) : (i += 1) {
1001 _ = c.lua_rawgeti(s, 1, @intCast(i + 1));
1002 if (get_lua_string(s, -1)) |tag_str| {
1003 cfg.tags[i] = tag_str;
1004 }
1005 c.lua_settop(s, -2);
1006 }
1007
1008 return 0;
1009 }
1010
1011 fn lua_autostart(state: ?*c.lua_State) callconv(.c) c_int {
1012 const cfg = config orelse return 0;
1013 const s = state orelse return 0;
1014 if (get_string_arg(s, 1)) |cmd| {
1015 cfg.add_autostart(cmd) catch return 0;
1016 }
1017 return 0;
1018 }
1019
1020 fn lua_auto_tile(state: ?*c.lua_State) callconv(.c) c_int {
1021 const cfg = config orelse return 0;
1022 const s = state orelse return 0;
1023 cfg.auto_tile = c.lua_toboolean(s, 1) != 0;
1024 return 0;
1025 }
1026
1027 fn lua_set_layout_symbol(state: ?*c.lua_State) callconv(.c) c_int {
1028 const cfg = config orelse return 0;
1029 const s = state orelse return 0;
1030 const name = get_string_arg(s, 1) orelse return 0;
1031 const symbol = get_string_arg(s, 2) orelse return 0;
1032
1033 const layout_map = .{
1034 .{ "tiling", &cfg.layout_tile_symbol },
1035 .{ "tile", &cfg.layout_tile_symbol },
1036 .{ "normie", &cfg.layout_floating_symbol },
1037 .{ "floating", &cfg.layout_floating_symbol },
1038 .{ "float", &cfg.layout_floating_symbol },
1039 .{ "monocle", &cfg.layout_monocle_symbol },
1040 .{ "scrolling", &cfg.layout_scrolling_symbol },
1041 .{ "scroll", &cfg.layout_scrolling_symbol },
1042 };
1043
1044 inline for (layout_map) |entry| {
1045 if (std.mem.eql(u8, name, entry[0])) {
1046 entry[1].* = symbol;
1047 return 0;
1048 }
1049 }
1050 return 0;
1051 }
1052
1053 fn lua_quit(state: ?*c.lua_State) callconv(.c) c_int {
1054 const s = state orelse return 0;
1055 create_action_table(s, "Quit");
1056 return 1;
1057 }
1058
1059 fn lua_restart(state: ?*c.lua_State) callconv(.c) c_int {
1060 const s = state orelse return 0;
1061 create_action_table(s, "Restart");
1062 return 1;
1063 }
1064
1065 fn lua_toggle_gaps(state: ?*c.lua_State) callconv(.c) c_int {
1066 const s = state orelse return 0;
1067 create_action_table(s, "ToggleGaps");
1068 return 1;
1069 }
1070
1071 fn lua_show_keybinds(state: ?*c.lua_State) callconv(.c) c_int {
1072 const s = state orelse return 0;
1073 create_action_table(s, "ShowKeybinds");
1074 return 1;
1075 }
1076
1077 fn lua_set_master_factor(state: ?*c.lua_State) callconv(.c) c_int {
1078 const s = state orelse return 0;
1079 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1080 create_action_table_with_int(s, "ResizeMaster", delta);
1081 return 1;
1082 }
1083
1084 fn lua_inc_num_master(state: ?*c.lua_State) callconv(.c) c_int {
1085 const s = state orelse return 0;
1086 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1087 if (delta > 0) {
1088 create_action_table(s, "IncMaster");
1089 } else {
1090 create_action_table(s, "DecMaster");
1091 }
1092 return 1;
1093 }
1094
1095 fn get_string_arg(state: *c.lua_State, idx: c_int) ?[]const u8 {
1096 if (c.lua_type(state, idx) != c.LUA_TSTRING) return null;
1097 return get_lua_string(state, idx);
1098 }
1099
1100 fn get_lua_string(state: *c.lua_State, idx: c_int) ?[]const u8 {
1101 const cstr = c.lua_tolstring(state, idx, null);
1102 if (cstr == null) return null;
1103 return std.mem.span(cstr);
1104 }
1105
1106 fn parse_color(state: *c.lua_State, idx: c_int) u32 {
1107 const lua_type = c.lua_type(state, idx);
1108 if (lua_type == c.LUA_TNUMBER) {
1109 return @intCast(c.lua_tointegerx(state, idx, null));
1110 }
1111 if (lua_type == c.LUA_TSTRING) {
1112 const str = get_lua_string(state, idx) orelse return 0;
1113 if (str.len > 0 and str[0] == '#') {
1114 return std.fmt.parseInt(u32, str[1..], 16) catch return 0;
1115 }
1116 if (str.len > 2 and str[0] == '0' and str[1] == 'x') {
1117 return std.fmt.parseInt(u32, str[2..], 16) catch return 0;
1118 }
1119 return std.fmt.parseInt(u32, str, 16) catch return 0;
1120 }
1121 return 0;
1122 }
1123
1124 fn parse_scheme(state: *c.lua_State) Color_Scheme {
1125 return Color_Scheme{
1126 .fg = parse_color(state, 1),
1127 .bg = parse_color(state, 2),
1128 .border = parse_color(state, 3),
1129 };
1130 }
1131
1132 fn parse_modifiers(state: *c.lua_State, idx: c_int) u32 {
1133 var mod_mask: u32 = 0;
1134
1135 if (c.lua_type(state, idx) != c.LUA_TTABLE) return mod_mask;
1136
1137 const len = c.lua_rawlen(state, idx);
1138 var i: usize = 1;
1139 while (i <= len) : (i += 1) {
1140 _ = c.lua_rawgeti(state, idx, @intCast(i));
1141 if (get_lua_string(state, -1)) |mod_str| {
1142 mod_mask |= parse_single_modifier(mod_str);
1143 }
1144 c.lua_settop(state, -2);
1145 }
1146
1147 return mod_mask;
1148 }
1149
1150 fn parse_modifiers_at_top(state: *c.lua_State) u32 {
1151 return parse_modifiers(state, -1);
1152 }
1153
1154 fn parse_single_modifier(name: []const u8) u32 {
1155 if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) {
1156 return (1 << 6);
1157 } else if (std.mem.eql(u8, name, "Mod1") or std.mem.eql(u8, name, "mod1") or std.mem.eql(u8, name, "alt")) {
1158 return (1 << 3);
1159 } else if (std.mem.eql(u8, name, "Shift") or std.mem.eql(u8, name, "shift")) {
1160 return (1 << 0);
1161 } else if (std.mem.eql(u8, name, "Control") or std.mem.eql(u8, name, "control") or std.mem.eql(u8, name, "ctrl")) {
1162 return (1 << 2);
1163 }
1164 return 0;
1165 }
1166
1167 fn parse_action(name: []const u8) ?Action {
1168 const action_map = .{
1169 .{ "Spawn", Action.spawn },
1170 .{ "SpawnTerminal", Action.spawn_terminal },
1171 .{ "KillClient", Action.kill_client },
1172 .{ "Quit", Action.quit },
1173 .{ "Restart", Action.restart },
1174 .{ "ShowKeybinds", Action.show_keybinds },
1175 .{ "FocusStack", Action.focus_next },
1176 .{ "MoveStack", Action.move_next },
1177 .{ "ResizeMaster", Action.resize_master },
1178 .{ "IncMaster", Action.inc_master },
1179 .{ "DecMaster", Action.dec_master },
1180 .{ "ToggleFloating", Action.toggle_floating },
1181 .{ "ToggleFullScreen", Action.toggle_fullscreen },
1182 .{ "ToggleGaps", Action.toggle_gaps },
1183 .{ "CycleLayout", Action.cycle_layout },
1184 .{ "ChangeLayout", Action.set_layout },
1185 .{ "ViewTag", Action.view_tag },
1186 .{ "ViewNextTag", Action.view_next_tag },
1187 .{ "ViewPreviousTag", Action.view_prev_tag },
1188 .{ "ViewNextNonEmptyTag", Action.view_next_nonempty_tag },
1189 .{ "ViewPreviousNonEmptyTag", Action.view_prev_nonempty_tag },
1190 .{ "MoveToTag", Action.move_to_tag },
1191 .{ "ToggleView", Action.toggle_view_tag },
1192 .{ "ToggleTag", Action.toggle_tag },
1193 .{ "FocusMonitor", Action.focus_monitor },
1194 .{ "TagMonitor", Action.send_to_monitor },
1195 .{ "ScrollLeft", Action.scroll_left },
1196 .{ "ScrollRight", Action.scroll_right },
1197 };
1198
1199 inline for (action_map) |entry| {
1200 if (std.mem.eql(u8, name, entry[0])) {
1201 return entry[1];
1202 }
1203 }
1204 return null;
1205 }
1206
1207 fn key_name_to_keysym(name: []const u8) ?u64 {
1208 const key_map = .{
1209 .{ "Return", 0xff0d },
1210 .{ "Enter", 0xff0d },
1211 .{ "Tab", 0xff09 },
1212 .{ "Escape", 0xff1b },
1213 .{ "BackSpace", 0xff08 },
1214 .{ "Delete", 0xffff },
1215 .{ "space", 0x0020 },
1216 .{ "Space", 0x0020 },
1217 .{ "comma", 0x002c },
1218 .{ "Comma", 0x002c },
1219 .{ "period", 0x002e },
1220 .{ "Period", 0x002e },
1221 .{ "slash", 0x002f },
1222 .{ "Slash", 0x002f },
1223 .{ "minus", 0x002d },
1224 .{ "Minus", 0x002d },
1225 .{ "equal", 0x003d },
1226 .{ "Equal", 0x003d },
1227 .{ "bracketleft", 0x005b },
1228 .{ "bracketright", 0x005d },
1229 .{ "backslash", 0x005c },
1230 .{ "semicolon", 0x003b },
1231 .{ "apostrophe", 0x0027 },
1232 .{ "grave", 0x0060 },
1233 .{ "Left", 0xff51 },
1234 .{ "Up", 0xff52 },
1235 .{ "Right", 0xff53 },
1236 .{ "Down", 0xff54 },
1237 .{ "F1", 0xffbe },
1238 .{ "F2", 0xffbf },
1239 .{ "F3", 0xffc0 },
1240 .{ "F4", 0xffc1 },
1241 .{ "F5", 0xffc2 },
1242 .{ "F6", 0xffc3 },
1243 .{ "F7", 0xffc4 },
1244 .{ "F8", 0xffc5 },
1245 .{ "F9", 0xffc6 },
1246 .{ "F10", 0xffc7 },
1247 .{ "F11", 0xffc8 },
1248 .{ "F12", 0xffc9 },
1249 .{ "Print", 0xff61 },
1250 .{ "XF86AudioRaiseVolume", 0x1008ff13 },
1251 .{ "XF86AudioLowerVolume", 0x1008ff11 },
1252 .{ "XF86AudioMute", 0x1008ff12 },
1253 .{ "XF86AudioPlay", 0x1008ff14 },
1254 .{ "XF86AudioPause", 0x1008ff31 },
1255 .{ "XF86AudioNext", 0x1008ff17 },
1256 .{ "XF86AudioPrev", 0x1008ff16 },
1257 .{ "XF86MonBrightnessUp", 0x1008ff02 },
1258 .{ "XF86MonBrightnessDown", 0x1008ff03 },
1259 };
1260
1261 inline for (key_map) |entry| {
1262 if (std.mem.eql(u8, name, entry[0])) {
1263 return entry[1];
1264 }
1265 }
1266
1267 if (name.len == 1) {
1268 const char = name[0];
1269 if (char >= 'a' and char <= 'z') {
1270 return char;
1271 }
1272 if (char >= 'A' and char <= 'Z') {
1273 return char + 32;
1274 }
1275 if (char >= '0' and char <= '9') {
1276 return char;
1277 }
1278 }
1279
1280 return null;
1281 }