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