| 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 ColorScheme = config_mod.ColorScheme; |
| 9 | |
| 10 | const c = @cImport({ |
| 11 | @cInclude("lua.h"); |
| 12 | @cInclude("lauxlib.h"); |
| 13 | @cInclude("lualib.h"); |
| 14 | }); |
| 15 | |
| 16 | var L: ?*c.lua_State = null; |
| 17 | var config: ?*Config = null; |
| 18 | |
| 19 | pub fn init(cfg: *Config) bool { |
| 20 | config = cfg; |
| 21 | L = c.luaL_newstate(); |
| 22 | if (L == null) return false; |
| 23 | c.luaL_openlibs(L); |
| 24 | registerApi(); |
| 25 | return true; |
| 26 | } |
| 27 | |
| 28 | pub fn deinit() void { |
| 29 | if (L) |state| { |
| 30 | c.lua_close(state); |
| 31 | } |
| 32 | L = null; |
| 33 | config = null; |
| 34 | } |
| 35 | |
| 36 | pub fn loadFile(path: []const u8) bool { |
| 37 | const state = L orelse return false; |
| 38 | var path_buf: [512]u8 = undefined; |
| 39 | if (path.len >= path_buf.len) return false; |
| 40 | @memcpy(path_buf[0..path.len], path); |
| 41 | path_buf[path.len] = 0; |
| 42 | |
| 43 | if (std.mem.lastIndexOfScalar(u8, path, '/')) |last_slash| { |
| 44 | const dir = path[0..last_slash]; |
| 45 | var setup_buf: [600]u8 = undefined; |
| 46 | const setup_code = std.fmt.bufPrint(&setup_buf, "package.path = '{s}/?.lua;' .. package.path\x00", .{dir}) catch return false; |
| 47 | if (c.luaL_loadstring(state, setup_code.ptr) != 0 or c.lua_pcallk(state, 0, 0, 0, 0, null) != 0) { |
| 48 | c.lua_settop(state, -2); |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (c.luaL_loadfilex(state, &path_buf, null) != 0) { |
| 54 | const err = c.lua_tolstring(state, -1, null); |
| 55 | if (err != null) { |
| 56 | std.debug.print("lua load error: {s}\n", .{std.mem.span(err)}); |
| 57 | } |
| 58 | c.lua_settop(state, -2); |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | if (c.lua_pcallk(state, 0, 0, 0, 0, null) != 0) { |
| 63 | const err = c.lua_tolstring(state, -1, null); |
| 64 | if (err != null) { |
| 65 | std.debug.print("lua runtime error: {s}\n", .{std.mem.span(err)}); |
| 66 | } |
| 67 | c.lua_settop(state, -2); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | pub fn loadConfig() bool { |
| 75 | const home = std.posix.getenv("HOME") orelse return false; |
| 76 | var path_buf: [512]u8 = undefined; |
| 77 | const path = std.fmt.bufPrint(&path_buf, "{s}/.config/oxwm/config.lua", .{home}) catch return false; |
| 78 | return loadFile(path); |
| 79 | } |
| 80 | |
| 81 | fn registerApi() void { |
| 82 | const state = L orelse return; |
| 83 | |
| 84 | c.lua_createtable(state, 0, 16); |
| 85 | |
| 86 | registerSpawnFunctions(state); |
| 87 | registerKeyModule(state); |
| 88 | registerGapsModule(state); |
| 89 | registerBorderModule(state); |
| 90 | registerClientModule(state); |
| 91 | registerLayoutModule(state); |
| 92 | registerTagModule(state); |
| 93 | registerMonitorModule(state); |
| 94 | registerRuleModule(state); |
| 95 | registerBarModule(state); |
| 96 | registerMiscFunctions(state); |
| 97 | |
| 98 | c.lua_setglobal(state, "oxwm"); |
| 99 | } |
| 100 | |
| 101 | fn registerSpawnFunctions(state: *c.lua_State) void { |
| 102 | c.lua_pushcfunction(state, luaSpawn); |
| 103 | c.lua_setfield(state, -2, "spawn"); |
| 104 | |
| 105 | c.lua_pushcfunction(state, luaSpawnTerminal); |
| 106 | c.lua_setfield(state, -2, "spawn_terminal"); |
| 107 | } |
| 108 | |
| 109 | fn registerKeyModule(state: *c.lua_State) void { |
| 110 | c.lua_createtable(state, 0, 2); |
| 111 | |
| 112 | c.lua_pushcfunction(state, luaKeyBind); |
| 113 | c.lua_setfield(state, -2, "bind"); |
| 114 | |
| 115 | c.lua_pushcfunction(state, luaKeyChord); |
| 116 | c.lua_setfield(state, -2, "chord"); |
| 117 | |
| 118 | c.lua_setfield(state, -2, "key"); |
| 119 | } |
| 120 | |
| 121 | fn registerGapsModule(state: *c.lua_State) void { |
| 122 | c.lua_createtable(state, 0, 6); |
| 123 | |
| 124 | c.lua_pushcfunction(state, luaGapsSetEnabled); |
| 125 | c.lua_setfield(state, -2, "set_enabled"); |
| 126 | |
| 127 | c.lua_pushcfunction(state, luaGapsEnable); |
| 128 | c.lua_setfield(state, -2, "enable"); |
| 129 | |
| 130 | c.lua_pushcfunction(state, luaGapsDisable); |
| 131 | c.lua_setfield(state, -2, "disable"); |
| 132 | |
| 133 | c.lua_pushcfunction(state, luaGapsSetInner); |
| 134 | c.lua_setfield(state, -2, "set_inner"); |
| 135 | |
| 136 | c.lua_pushcfunction(state, luaGapsSetOuter); |
| 137 | c.lua_setfield(state, -2, "set_outer"); |
| 138 | |
| 139 | c.lua_pushcfunction(state, luaGapsSetSmart); |
| 140 | c.lua_setfield(state, -2, "set_smart"); |
| 141 | |
| 142 | c.lua_setfield(state, -2, "gaps"); |
| 143 | } |
| 144 | |
| 145 | fn registerBorderModule(state: *c.lua_State) void { |
| 146 | c.lua_createtable(state, 0, 3); |
| 147 | |
| 148 | c.lua_pushcfunction(state, luaBorderSetWidth); |
| 149 | c.lua_setfield(state, -2, "set_width"); |
| 150 | |
| 151 | c.lua_pushcfunction(state, luaBorderSetFocusedColor); |
| 152 | c.lua_setfield(state, -2, "set_focused_color"); |
| 153 | |
| 154 | c.lua_pushcfunction(state, luaBorderSetUnfocusedColor); |
| 155 | c.lua_setfield(state, -2, "set_unfocused_color"); |
| 156 | |
| 157 | c.lua_setfield(state, -2, "border"); |
| 158 | } |
| 159 | |
| 160 | fn registerClientModule(state: *c.lua_State) void { |
| 161 | c.lua_createtable(state, 0, 5); |
| 162 | |
| 163 | c.lua_pushcfunction(state, luaClientKill); |
| 164 | c.lua_setfield(state, -2, "kill"); |
| 165 | |
| 166 | c.lua_pushcfunction(state, luaClientToggleFullscreen); |
| 167 | c.lua_setfield(state, -2, "toggle_fullscreen"); |
| 168 | |
| 169 | c.lua_pushcfunction(state, luaClientToggleFloating); |
| 170 | c.lua_setfield(state, -2, "toggle_floating"); |
| 171 | |
| 172 | c.lua_pushcfunction(state, luaClientFocusStack); |
| 173 | c.lua_setfield(state, -2, "focus_stack"); |
| 174 | |
| 175 | c.lua_pushcfunction(state, luaClientMoveStack); |
| 176 | c.lua_setfield(state, -2, "move_stack"); |
| 177 | |
| 178 | c.lua_setfield(state, -2, "client"); |
| 179 | } |
| 180 | |
| 181 | fn registerLayoutModule(state: *c.lua_State) void { |
| 182 | c.lua_createtable(state, 0, 4); |
| 183 | |
| 184 | c.lua_pushcfunction(state, luaLayoutCycle); |
| 185 | c.lua_setfield(state, -2, "cycle"); |
| 186 | |
| 187 | c.lua_pushcfunction(state, luaLayoutSet); |
| 188 | c.lua_setfield(state, -2, "set"); |
| 189 | |
| 190 | c.lua_pushcfunction(state, luaLayoutScrollLeft); |
| 191 | c.lua_setfield(state, -2, "scroll_left"); |
| 192 | |
| 193 | c.lua_pushcfunction(state, luaLayoutScrollRight); |
| 194 | c.lua_setfield(state, -2, "scroll_right"); |
| 195 | |
| 196 | c.lua_setfield(state, -2, "layout"); |
| 197 | } |
| 198 | |
| 199 | fn registerTagModule(state: *c.lua_State) void { |
| 200 | c.lua_createtable(state, 0, 10); |
| 201 | |
| 202 | c.lua_pushcfunction(state, luaTagView); |
| 203 | c.lua_setfield(state, -2, "view"); |
| 204 | |
| 205 | c.lua_pushcfunction(state, luaTagViewNext); |
| 206 | c.lua_setfield(state, -2, "view_next"); |
| 207 | |
| 208 | c.lua_pushcfunction(state, luaTagViewPrevious); |
| 209 | c.lua_setfield(state, -2, "view_previous"); |
| 210 | |
| 211 | c.lua_pushcfunction(state, luaTagViewNextNonempty); |
| 212 | c.lua_setfield(state, -2, "view_next_nonempty"); |
| 213 | |
| 214 | c.lua_pushcfunction(state, luaTagViewPreviousNonempty); |
| 215 | c.lua_setfield(state, -2, "view_previous_nonempty"); |
| 216 | |
| 217 | c.lua_pushcfunction(state, luaTagToggleview); |
| 218 | c.lua_setfield(state, -2, "toggleview"); |
| 219 | |
| 220 | c.lua_pushcfunction(state, luaTagMoveTo); |
| 221 | c.lua_setfield(state, -2, "move_to"); |
| 222 | |
| 223 | c.lua_pushcfunction(state, luaTagToggletag); |
| 224 | c.lua_setfield(state, -2, "toggletag"); |
| 225 | |
| 226 | c.lua_pushcfunction(state, luaTagSetBackAndForth); |
| 227 | c.lua_setfield(state, -2, "set_back_and_forth"); |
| 228 | |
| 229 | c.lua_setfield(state, -2, "tag"); |
| 230 | } |
| 231 | |
| 232 | fn registerMonitorModule(state: *c.lua_State) void { |
| 233 | c.lua_createtable(state, 0, 2); |
| 234 | |
| 235 | c.lua_pushcfunction(state, luaMonitorFocus); |
| 236 | c.lua_setfield(state, -2, "focus"); |
| 237 | |
| 238 | c.lua_pushcfunction(state, luaMonitorTag); |
| 239 | c.lua_setfield(state, -2, "tag"); |
| 240 | |
| 241 | c.lua_setfield(state, -2, "monitor"); |
| 242 | } |
| 243 | |
| 244 | fn registerRuleModule(state: *c.lua_State) void { |
| 245 | c.lua_createtable(state, 0, 1); |
| 246 | |
| 247 | c.lua_pushcfunction(state, luaRuleAdd); |
| 248 | c.lua_setfield(state, -2, "add"); |
| 249 | |
| 250 | c.lua_setfield(state, -2, "rule"); |
| 251 | } |
| 252 | |
| 253 | fn registerBarModule(state: *c.lua_State) void { |
| 254 | c.lua_createtable(state, 0, 10); |
| 255 | |
| 256 | c.lua_pushcfunction(state, luaBarSetFont); |
| 257 | c.lua_setfield(state, -2, "set_font"); |
| 258 | |
| 259 | c.lua_pushcfunction(state, luaBarSetBlocks); |
| 260 | c.lua_setfield(state, -2, "set_blocks"); |
| 261 | |
| 262 | c.lua_pushcfunction(state, luaBarSetSchemeNormal); |
| 263 | c.lua_setfield(state, -2, "set_scheme_normal"); |
| 264 | |
| 265 | c.lua_pushcfunction(state, luaBarSetSchemeSelected); |
| 266 | c.lua_setfield(state, -2, "set_scheme_selected"); |
| 267 | |
| 268 | c.lua_pushcfunction(state, luaBarSetSchemeOccupied); |
| 269 | c.lua_setfield(state, -2, "set_scheme_occupied"); |
| 270 | |
| 271 | c.lua_pushcfunction(state, luaBarSetSchemeUrgent); |
| 272 | c.lua_setfield(state, -2, "set_scheme_urgent"); |
| 273 | |
| 274 | c.lua_pushcfunction(state, luaBarSetHideVacantTags); |
| 275 | c.lua_setfield(state, -2, "set_hide_vacant_tags"); |
| 276 | |
| 277 | c.lua_createtable(state, 0, 6); |
| 278 | |
| 279 | c.lua_pushcfunction(state, luaBarBlockRam); |
| 280 | c.lua_setfield(state, -2, "ram"); |
| 281 | |
| 282 | c.lua_pushcfunction(state, luaBarBlockDatetime); |
| 283 | c.lua_setfield(state, -2, "datetime"); |
| 284 | |
| 285 | c.lua_pushcfunction(state, luaBarBlockShell); |
| 286 | c.lua_setfield(state, -2, "shell"); |
| 287 | |
| 288 | c.lua_pushcfunction(state, luaBarBlockStatic); |
| 289 | c.lua_setfield(state, -2, "static"); |
| 290 | |
| 291 | c.lua_pushcfunction(state, luaBarBlockBattery); |
| 292 | c.lua_setfield(state, -2, "battery"); |
| 293 | |
| 294 | c.lua_setfield(state, -2, "block"); |
| 295 | |
| 296 | c.lua_setfield(state, -2, "bar"); |
| 297 | } |
| 298 | |
| 299 | fn registerMiscFunctions(state: *c.lua_State) void { |
| 300 | c.lua_pushcfunction(state, luaSetTerminal); |
| 301 | c.lua_setfield(state, -2, "set_terminal"); |
| 302 | |
| 303 | c.lua_pushcfunction(state, luaSetModkey); |
| 304 | c.lua_setfield(state, -2, "set_modkey"); |
| 305 | |
| 306 | c.lua_pushcfunction(state, luaSetTags); |
| 307 | c.lua_setfield(state, -2, "set_tags"); |
| 308 | |
| 309 | c.lua_pushcfunction(state, luaSetLayoutSymbol); |
| 310 | c.lua_setfield(state, -2, "set_layout_symbol"); |
| 311 | |
| 312 | c.lua_pushcfunction(state, luaAutostart); |
| 313 | c.lua_setfield(state, -2, "autostart"); |
| 314 | |
| 315 | c.lua_pushcfunction(state, luaAutoTile); |
| 316 | c.lua_setfield(state, -2, "auto_tile"); |
| 317 | |
| 318 | c.lua_pushcfunction(state, luaQuit); |
| 319 | c.lua_setfield(state, -2, "quit"); |
| 320 | |
| 321 | c.lua_pushcfunction(state, luaRestart); |
| 322 | c.lua_setfield(state, -2, "restart"); |
| 323 | |
| 324 | c.lua_pushcfunction(state, luaToggleGaps); |
| 325 | c.lua_setfield(state, -2, "toggle_gaps"); |
| 326 | |
| 327 | c.lua_pushcfunction(state, luaShowKeybinds); |
| 328 | c.lua_setfield(state, -2, "show_keybinds"); |
| 329 | |
| 330 | c.lua_pushcfunction(state, luaSetMasterFactor); |
| 331 | c.lua_setfield(state, -2, "set_master_factor"); |
| 332 | |
| 333 | c.lua_pushcfunction(state, luaIncNumMaster); |
| 334 | c.lua_setfield(state, -2, "inc_num_master"); |
| 335 | } |
| 336 | |
| 337 | fn createActionTable(state: *c.lua_State, action_name: [*:0]const u8) void { |
| 338 | c.lua_createtable(state, 0, 2); |
| 339 | _ = c.lua_pushstring(state, action_name); |
| 340 | c.lua_setfield(state, -2, "__action"); |
| 341 | } |
| 342 | |
| 343 | fn createActionTableWithInt(state: *c.lua_State, action_name: [*:0]const u8, arg: i32) void { |
| 344 | c.lua_createtable(state, 0, 2); |
| 345 | _ = c.lua_pushstring(state, action_name); |
| 346 | c.lua_setfield(state, -2, "__action"); |
| 347 | c.lua_pushinteger(state, arg); |
| 348 | c.lua_setfield(state, -2, "__arg"); |
| 349 | } |
| 350 | |
| 351 | fn createActionTableWithString(state: *c.lua_State, action_name: [*:0]const u8) void { |
| 352 | c.lua_createtable(state, 0, 2); |
| 353 | _ = c.lua_pushstring(state, action_name); |
| 354 | c.lua_setfield(state, -2, "__action"); |
| 355 | c.lua_pushvalue(state, 1); |
| 356 | c.lua_setfield(state, -2, "__arg"); |
| 357 | } |
| 358 | |
| 359 | fn luaSpawn(state: ?*c.lua_State) callconv(.c) c_int { |
| 360 | const s = state orelse return 0; |
| 361 | createActionTableWithString(s, "Spawn"); |
| 362 | return 1; |
| 363 | } |
| 364 | |
| 365 | fn luaSpawnTerminal(state: ?*c.lua_State) callconv(.c) c_int { |
| 366 | const s = state orelse return 0; |
| 367 | createActionTable(s, "SpawnTerminal"); |
| 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | fn luaKeyBind(state: ?*c.lua_State) callconv(.c) c_int { |
| 372 | const s = state orelse return 0; |
| 373 | const cfg = config orelse return 0; |
| 374 | |
| 375 | const mod_mask = parseModifiers(s, 1); |
| 376 | const key_str = getStringArg(s, 2) orelse return 0; |
| 377 | const keysym = keyNameToKeysym(key_str) orelse return 0; |
| 378 | |
| 379 | if (c.lua_type(s, 3) != c.LUA_TTABLE) return 0; |
| 380 | |
| 381 | _ = c.lua_getfield(s, 3, "__action"); |
| 382 | const action_str = getLuaString(s, -1) orelse { |
| 383 | c.lua_settop(s, -2); |
| 384 | return 0; |
| 385 | }; |
| 386 | c.lua_settop(s, -2); |
| 387 | |
| 388 | const action = parseAction(action_str) orelse return 0; |
| 389 | |
| 390 | var int_arg: i32 = 0; |
| 391 | var str_arg: ?[]const u8 = null; |
| 392 | |
| 393 | _ = c.lua_getfield(s, 3, "__arg"); |
| 394 | if (c.lua_type(s, -1) == c.LUA_TNUMBER) { |
| 395 | int_arg = @intCast(c.lua_tointegerx(s, -1, null)); |
| 396 | } else if (c.lua_type(s, -1) == c.LUA_TSTRING) { |
| 397 | str_arg = getLuaString(s, -1); |
| 398 | } else if (c.lua_type(s, -1) == c.LUA_TTABLE) { |
| 399 | str_arg = extractSpawnCommand(s, -1); |
| 400 | } |
| 401 | c.lua_settop(s, -2); |
| 402 | |
| 403 | var keybind: config_mod.Keybind = .{ |
| 404 | .action = action, |
| 405 | .int_arg = int_arg, |
| 406 | .str_arg = str_arg, |
| 407 | }; |
| 408 | keybind.keys[0] = .{ .mod_mask = mod_mask, .keysym = keysym }; |
| 409 | keybind.key_count = 1; |
| 410 | |
| 411 | cfg.addKeybind(keybind) catch return 0; |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | fn luaKeyChord(state: ?*c.lua_State) callconv(.c) c_int { |
| 417 | const s = state orelse return 0; |
| 418 | const cfg = config orelse return 0; |
| 419 | |
| 420 | if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0; |
| 421 | if (c.lua_type(s, 2) != c.LUA_TTABLE) return 0; |
| 422 | |
| 423 | var keybind: config_mod.Keybind = .{ |
| 424 | .action = .quit, |
| 425 | .int_arg = 0, |
| 426 | .str_arg = null, |
| 427 | }; |
| 428 | keybind.key_count = 0; |
| 429 | |
| 430 | const num_keys = c.lua_rawlen(s, 1); |
| 431 | if (num_keys == 0 or num_keys > 4) return 0; |
| 432 | |
| 433 | var i: usize = 1; |
| 434 | while (i <= num_keys) : (i += 1) { |
| 435 | _ = c.lua_rawgeti(s, 1, @intCast(i)); |
| 436 | if (c.lua_type(s, -1) != c.LUA_TTABLE) { |
| 437 | c.lua_settop(s, -2); |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | _ = c.lua_rawgeti(s, -1, 1); |
| 442 | const mod_mask = parseModifiersAtTop(s); |
| 443 | c.lua_settop(s, -2); |
| 444 | |
| 445 | _ = c.lua_rawgeti(s, -1, 2); |
| 446 | const key_str = getLuaString(s, -1) orelse { |
| 447 | c.lua_settop(s, -3); |
| 448 | return 0; |
| 449 | }; |
| 450 | c.lua_settop(s, -2); |
| 451 | |
| 452 | const keysym = keyNameToKeysym(key_str) orelse { |
| 453 | c.lua_settop(s, -2); |
| 454 | return 0; |
| 455 | }; |
| 456 | |
| 457 | keybind.keys[keybind.key_count] = .{ .mod_mask = mod_mask, .keysym = keysym }; |
| 458 | keybind.key_count += 1; |
| 459 | |
| 460 | c.lua_settop(s, -2); |
| 461 | } |
| 462 | |
| 463 | _ = c.lua_getfield(s, 2, "__action"); |
| 464 | const action_str = getLuaString(s, -1) orelse { |
| 465 | c.lua_settop(s, -2); |
| 466 | return 0; |
| 467 | }; |
| 468 | c.lua_settop(s, -2); |
| 469 | |
| 470 | keybind.action = parseAction(action_str) orelse return 0; |
| 471 | |
| 472 | _ = c.lua_getfield(s, 2, "__arg"); |
| 473 | if (c.lua_type(s, -1) == c.LUA_TNUMBER) { |
| 474 | keybind.int_arg = @intCast(c.lua_tointegerx(s, -1, null)); |
| 475 | } else if (c.lua_type(s, -1) == c.LUA_TSTRING) { |
| 476 | keybind.str_arg = getLuaString(s, -1); |
| 477 | } else if (c.lua_type(s, -1) == c.LUA_TTABLE) { |
| 478 | keybind.str_arg = extractSpawnCommand(s, -1); |
| 479 | } |
| 480 | |
| 481 | c.lua_settop(s, -2); |
| 482 | cfg.addKeybind(keybind) catch return 0; |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | fn luaGapsSetEnabled(state: ?*c.lua_State) callconv(.c) c_int { |
| 488 | const cfg = config orelse return 0; |
| 489 | const s = state orelse return 0; |
| 490 | cfg.gaps_enabled = c.lua_toboolean(s, 1) != 0; |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | fn luaGapsEnable(state: ?*c.lua_State) callconv(.c) c_int { |
| 495 | _ = state; |
| 496 | const cfg = config orelse return 0; |
| 497 | cfg.gaps_enabled = true; |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | fn luaGapsDisable(state: ?*c.lua_State) callconv(.c) c_int { |
| 502 | _ = state; |
| 503 | const cfg = config orelse return 0; |
| 504 | cfg.gaps_enabled = false; |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | fn luaGapsSetInner(state: ?*c.lua_State) callconv(.c) c_int { |
| 509 | const cfg = config orelse return 0; |
| 510 | const s = state orelse return 0; |
| 511 | cfg.gap_inner_h = @intCast(c.lua_tointegerx(s, 1, null)); |
| 512 | cfg.gap_inner_v = @intCast(c.lua_tointegerx(s, 2, null)); |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | fn luaGapsSetOuter(state: ?*c.lua_State) callconv(.c) c_int { |
| 517 | const cfg = config orelse return 0; |
| 518 | const s = state orelse return 0; |
| 519 | cfg.gap_outer_h = @intCast(c.lua_tointegerx(s, 1, null)); |
| 520 | cfg.gap_outer_v = @intCast(c.lua_tointegerx(s, 2, null)); |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | fn luaGapsSetSmart(state: ?*c.lua_State) callconv(.c) c_int { |
| 525 | const cfg = config orelse return 0; |
| 526 | const s = state orelse return 0; |
| 527 | cfg.smartgaps_enabled = c.lua_toboolean(s, 1) != 0; |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | fn luaBorderSetWidth(state: ?*c.lua_State) callconv(.c) c_int { |
| 532 | const cfg = config orelse return 0; |
| 533 | const s = state orelse return 0; |
| 534 | cfg.border_width = @intCast(c.lua_tointegerx(s, 1, null)); |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | fn luaBorderSetFocusedColor(state: ?*c.lua_State) callconv(.c) c_int { |
| 539 | const cfg = config orelse return 0; |
| 540 | const s = state orelse return 0; |
| 541 | cfg.border_focused = parseColor(s, 1); |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | fn luaBorderSetUnfocusedColor(state: ?*c.lua_State) callconv(.c) c_int { |
| 546 | const cfg = config orelse return 0; |
| 547 | const s = state orelse return 0; |
| 548 | cfg.border_unfocused = parseColor(s, 1); |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | fn luaClientKill(state: ?*c.lua_State) callconv(.c) c_int { |
| 553 | const s = state orelse return 0; |
| 554 | createActionTable(s, "KillClient"); |
| 555 | return 1; |
| 556 | } |
| 557 | |
| 558 | fn luaClientToggleFullscreen(state: ?*c.lua_State) callconv(.c) c_int { |
| 559 | const s = state orelse return 0; |
| 560 | createActionTable(s, "ToggleFullScreen"); |
| 561 | return 1; |
| 562 | } |
| 563 | |
| 564 | fn luaClientToggleFloating(state: ?*c.lua_State) callconv(.c) c_int { |
| 565 | const s = state orelse return 0; |
| 566 | createActionTable(s, "ToggleFloating"); |
| 567 | return 1; |
| 568 | } |
| 569 | |
| 570 | fn luaClientFocusStack(state: ?*c.lua_State) callconv(.c) c_int { |
| 571 | const s = state orelse return 0; |
| 572 | const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 573 | createActionTableWithInt(s, "FocusStack", dir); |
| 574 | return 1; |
| 575 | } |
| 576 | |
| 577 | fn luaClientMoveStack(state: ?*c.lua_State) callconv(.c) c_int { |
| 578 | const s = state orelse return 0; |
| 579 | const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 580 | createActionTableWithInt(s, "MoveStack", dir); |
| 581 | return 1; |
| 582 | } |
| 583 | |
| 584 | fn luaLayoutCycle(state: ?*c.lua_State) callconv(.c) c_int { |
| 585 | const s = state orelse return 0; |
| 586 | createActionTable(s, "CycleLayout"); |
| 587 | return 1; |
| 588 | } |
| 589 | |
| 590 | fn luaLayoutSet(state: ?*c.lua_State) callconv(.c) c_int { |
| 591 | const s = state orelse return 0; |
| 592 | createActionTableWithString(s, "ChangeLayout"); |
| 593 | return 1; |
| 594 | } |
| 595 | |
| 596 | fn luaLayoutScrollLeft(state: ?*c.lua_State) callconv(.c) c_int { |
| 597 | const s = state orelse return 0; |
| 598 | createActionTable(s, "ScrollLeft"); |
| 599 | return 1; |
| 600 | } |
| 601 | |
| 602 | fn luaLayoutScrollRight(state: ?*c.lua_State) callconv(.c) c_int { |
| 603 | const s = state orelse return 0; |
| 604 | createActionTable(s, "ScrollRight"); |
| 605 | return 1; |
| 606 | } |
| 607 | |
| 608 | fn luaTagView(state: ?*c.lua_State) callconv(.c) c_int { |
| 609 | const s = state orelse return 0; |
| 610 | const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 611 | createActionTableWithInt(s, "ViewTag", idx); |
| 612 | return 1; |
| 613 | } |
| 614 | |
| 615 | fn luaTagViewNext(state: ?*c.lua_State) callconv(.c) c_int { |
| 616 | const s = state orelse return 0; |
| 617 | createActionTable(s, "ViewNextTag"); |
| 618 | return 1; |
| 619 | } |
| 620 | |
| 621 | fn luaTagViewPrevious(state: ?*c.lua_State) callconv(.c) c_int { |
| 622 | const s = state orelse return 0; |
| 623 | createActionTable(s, "ViewPreviousTag"); |
| 624 | return 1; |
| 625 | } |
| 626 | |
| 627 | fn luaTagViewNextNonempty(state: ?*c.lua_State) callconv(.c) c_int { |
| 628 | const s = state orelse return 0; |
| 629 | createActionTable(s, "ViewNextNonEmptyTag"); |
| 630 | return 1; |
| 631 | } |
| 632 | |
| 633 | fn luaTagViewPreviousNonempty(state: ?*c.lua_State) callconv(.c) c_int { |
| 634 | const s = state orelse return 0; |
| 635 | createActionTable(s, "ViewPreviousNonEmptyTag"); |
| 636 | return 1; |
| 637 | } |
| 638 | |
| 639 | fn luaTagToggleview(state: ?*c.lua_State) callconv(.c) c_int { |
| 640 | const s = state orelse return 0; |
| 641 | const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 642 | createActionTableWithInt(s, "ToggleView", idx); |
| 643 | return 1; |
| 644 | } |
| 645 | |
| 646 | fn luaTagMoveTo(state: ?*c.lua_State) callconv(.c) c_int { |
| 647 | const s = state orelse return 0; |
| 648 | const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 649 | createActionTableWithInt(s, "MoveToTag", idx); |
| 650 | return 1; |
| 651 | } |
| 652 | |
| 653 | fn luaTagToggletag(state: ?*c.lua_State) callconv(.c) c_int { |
| 654 | const s = state orelse return 0; |
| 655 | const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 656 | createActionTableWithInt(s, "ToggleTag", idx); |
| 657 | return 1; |
| 658 | } |
| 659 | |
| 660 | fn luaTagSetBackAndForth(state: ?*c.lua_State) callconv(.c) c_int { |
| 661 | const cfg = config orelse return 0; |
| 662 | const s = state orelse return 0; |
| 663 | cfg.tag_back_and_forth = c.lua_toboolean(s, 1) != 0; |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | fn luaMonitorFocus(state: ?*c.lua_State) callconv(.c) c_int { |
| 668 | const s = state orelse return 0; |
| 669 | const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 670 | createActionTableWithInt(s, "FocusMonitor", dir); |
| 671 | return 1; |
| 672 | } |
| 673 | |
| 674 | fn luaMonitorTag(state: ?*c.lua_State) callconv(.c) c_int { |
| 675 | const s = state orelse return 0; |
| 676 | const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 677 | createActionTableWithInt(s, "TagMonitor", dir); |
| 678 | return 1; |
| 679 | } |
| 680 | |
| 681 | fn luaRuleAdd(state: ?*c.lua_State) callconv(.c) c_int { |
| 682 | const cfg = config orelse return 0; |
| 683 | const s = state orelse return 0; |
| 684 | |
| 685 | if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0; |
| 686 | |
| 687 | var rule = Rule{ |
| 688 | .class = null, |
| 689 | .instance = null, |
| 690 | .title = null, |
| 691 | .tags = 0, |
| 692 | .is_floating = false, |
| 693 | .monitor = -1, |
| 694 | .focus = false, |
| 695 | }; |
| 696 | |
| 697 | _ = c.lua_getfield(s, 1, "class"); |
| 698 | if (c.lua_type(s, -1) == c.LUA_TSTRING) { |
| 699 | rule.class = getLuaString(s, -1); |
| 700 | } |
| 701 | c.lua_settop(s, -2); |
| 702 | |
| 703 | _ = c.lua_getfield(s, 1, "instance"); |
| 704 | if (c.lua_type(s, -1) == c.LUA_TSTRING) { |
| 705 | rule.instance = getLuaString(s, -1); |
| 706 | } |
| 707 | c.lua_settop(s, -2); |
| 708 | |
| 709 | _ = c.lua_getfield(s, 1, "title"); |
| 710 | if (c.lua_type(s, -1) == c.LUA_TSTRING) { |
| 711 | rule.title = getLuaString(s, -1); |
| 712 | } |
| 713 | c.lua_settop(s, -2); |
| 714 | |
| 715 | _ = c.lua_getfield(s, 1, "tag"); |
| 716 | if (c.lua_type(s, -1) == c.LUA_TNUMBER) { |
| 717 | const tag_idx: i32 = @intCast(c.lua_tointegerx(s, -1, null)); |
| 718 | if (tag_idx > 0) { |
| 719 | rule.tags = @as(u32, 1) << @intCast(tag_idx - 1); |
| 720 | } |
| 721 | } |
| 722 | c.lua_settop(s, -2); |
| 723 | |
| 724 | _ = c.lua_getfield(s, 1, "floating"); |
| 725 | if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) { |
| 726 | rule.is_floating = c.lua_toboolean(s, -1) != 0; |
| 727 | } |
| 728 | c.lua_settop(s, -2); |
| 729 | |
| 730 | _ = c.lua_getfield(s, 1, "monitor"); |
| 731 | if (c.lua_type(s, -1) == c.LUA_TNUMBER) { |
| 732 | rule.monitor = @intCast(c.lua_tointegerx(s, -1, null)); |
| 733 | } |
| 734 | c.lua_settop(s, -2); |
| 735 | |
| 736 | _ = c.lua_getfield(s, 1, "focus"); |
| 737 | if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) { |
| 738 | rule.focus = c.lua_toboolean(s, -1) != 0; |
| 739 | } |
| 740 | c.lua_settop(s, -2); |
| 741 | |
| 742 | cfg.addRule(rule) catch return 0; |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | fn luaBarSetFont(state: ?*c.lua_State) callconv(.c) c_int { |
| 747 | const cfg = config orelse return 0; |
| 748 | const s = state orelse return 0; |
| 749 | if (dupeLuaString(s, 1)) |font| { |
| 750 | cfg.font = font; |
| 751 | } |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | fn luaBarSetBlocks(state: ?*c.lua_State) callconv(.c) c_int { |
| 756 | const cfg = config orelse return 0; |
| 757 | const s = state orelse return 0; |
| 758 | |
| 759 | if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0; |
| 760 | |
| 761 | const len = c.lua_rawlen(s, 1); |
| 762 | var i: usize = 1; |
| 763 | while (i <= len) : (i += 1) { |
| 764 | _ = c.lua_rawgeti(s, 1, @intCast(i)); |
| 765 | |
| 766 | if (c.lua_type(s, -1) != c.LUA_TTABLE) { |
| 767 | c.lua_settop(s, -2); |
| 768 | continue; |
| 769 | } |
| 770 | |
| 771 | if (parseBlockConfig(s, -1)) |block| { |
| 772 | cfg.addBlock(block) catch {}; |
| 773 | } |
| 774 | |
| 775 | c.lua_settop(s, -2); |
| 776 | } |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | fn parseBlockConfig(state: *c.lua_State, idx: c_int) ?Block { |
| 782 | _ = c.lua_getfield(state, idx, "__block_type"); |
| 783 | const block_type_str = getLuaString(state, -1) orelse { |
| 784 | c.lua_settop(state, -2); |
| 785 | return null; |
| 786 | }; |
| 787 | c.lua_settop(state, -2); |
| 788 | |
| 789 | _ = c.lua_getfield(state, idx, "format"); |
| 790 | const format = dupeLuaString(state, -1) orelse ""; |
| 791 | c.lua_settop(state, -2); |
| 792 | |
| 793 | _ = c.lua_getfield(state, idx, "interval"); |
| 794 | const interval: u32 = @intCast(c.lua_tointegerx(state, -1, null)); |
| 795 | c.lua_settop(state, -2); |
| 796 | |
| 797 | _ = c.lua_getfield(state, idx, "color"); |
| 798 | const color = parseColor(state, -1); |
| 799 | c.lua_settop(state, -2); |
| 800 | |
| 801 | _ = c.lua_getfield(state, idx, "underline"); |
| 802 | const underline = c.lua_toboolean(state, -1) != 0; |
| 803 | c.lua_settop(state, -2); |
| 804 | |
| 805 | var block = Block{ |
| 806 | .block_type = .static, |
| 807 | .format = format, |
| 808 | .interval = interval, |
| 809 | .color = color, |
| 810 | .underline = underline, |
| 811 | }; |
| 812 | |
| 813 | if (std.mem.eql(u8, block_type_str, "Ram")) { |
| 814 | block.block_type = .ram; |
| 815 | } else if (std.mem.eql(u8, block_type_str, "DateTime")) { |
| 816 | block.block_type = .datetime; |
| 817 | _ = c.lua_getfield(state, idx, "__arg"); |
| 818 | block.datetime_format = dupeLuaString(state, -1); |
| 819 | c.lua_settop(state, -2); |
| 820 | } else if (std.mem.eql(u8, block_type_str, "Shell")) { |
| 821 | block.block_type = .shell; |
| 822 | _ = c.lua_getfield(state, idx, "__arg"); |
| 823 | block.command = dupeLuaString(state, -1); |
| 824 | c.lua_settop(state, -2); |
| 825 | } else if (std.mem.eql(u8, block_type_str, "Static")) { |
| 826 | block.block_type = .static; |
| 827 | _ = c.lua_getfield(state, idx, "__arg"); |
| 828 | if (dupeLuaString(state, -1)) |text| { |
| 829 | block.format = text; |
| 830 | } |
| 831 | c.lua_settop(state, -2); |
| 832 | } else if (std.mem.eql(u8, block_type_str, "Battery")) { |
| 833 | block.block_type = .battery; |
| 834 | _ = c.lua_getfield(state, idx, "__arg"); |
| 835 | if (c.lua_type(state, -1) == c.LUA_TTABLE) { |
| 836 | _ = c.lua_getfield(state, -1, "charging"); |
| 837 | block.format_charging = dupeLuaString(state, -1); |
| 838 | c.lua_settop(state, -2); |
| 839 | |
| 840 | _ = c.lua_getfield(state, -1, "discharging"); |
| 841 | block.format_discharging = dupeLuaString(state, -1); |
| 842 | c.lua_settop(state, -2); |
| 843 | |
| 844 | _ = c.lua_getfield(state, -1, "full"); |
| 845 | block.format_full = dupeLuaString(state, -1); |
| 846 | c.lua_settop(state, -2); |
| 847 | |
| 848 | _ = c.lua_getfield(state, -1, "battery_name"); |
| 849 | block.battery_name = dupeLuaString(state, -1); |
| 850 | c.lua_settop(state, -2); |
| 851 | } |
| 852 | c.lua_settop(state, -2); |
| 853 | } else { |
| 854 | return null; |
| 855 | } |
| 856 | |
| 857 | return block; |
| 858 | } |
| 859 | |
| 860 | fn luaBarSetSchemeNormal(state: ?*c.lua_State) callconv(.c) c_int { |
| 861 | const cfg = config orelse return 0; |
| 862 | const s = state orelse return 0; |
| 863 | cfg.scheme_normal = parseScheme(s); |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | fn luaBarSetSchemeSelected(state: ?*c.lua_State) callconv(.c) c_int { |
| 868 | const cfg = config orelse return 0; |
| 869 | const s = state orelse return 0; |
| 870 | cfg.scheme_selected = parseScheme(s); |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | fn luaBarSetSchemeOccupied(state: ?*c.lua_State) callconv(.c) c_int { |
| 875 | const cfg = config orelse return 0; |
| 876 | const s = state orelse return 0; |
| 877 | cfg.scheme_occupied = parseScheme(s); |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | fn luaBarSetSchemeUrgent(state: ?*c.lua_State) callconv(.c) c_int { |
| 882 | const cfg = config orelse return 0; |
| 883 | const s = state orelse return 0; |
| 884 | cfg.scheme_urgent = parseScheme(s); |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | fn luaBarSetHideVacantTags(state: ?*c.lua_State) callconv(.c) c_int { |
| 889 | const cfg = config orelse return 0; |
| 890 | const s = state orelse return 0; |
| 891 | cfg.hide_vacant_tags = c.lua_toboolean(s, 1) != 0; |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | fn luaBarBlockRam(state: ?*c.lua_State) callconv(.c) c_int { |
| 896 | const s = state orelse return 0; |
| 897 | createBlockTable(s, "Ram", null); |
| 898 | return 1; |
| 899 | } |
| 900 | |
| 901 | fn luaBarBlockDatetime(state: ?*c.lua_State) callconv(.c) c_int { |
| 902 | const s = state orelse return 0; |
| 903 | _ = c.lua_getfield(s, 1, "date_format"); |
| 904 | const date_format = getLuaString(s, -1); |
| 905 | c.lua_settop(s, -2); |
| 906 | createBlockTable(s, "DateTime", date_format); |
| 907 | return 1; |
| 908 | } |
| 909 | |
| 910 | fn luaBarBlockShell(state: ?*c.lua_State) callconv(.c) c_int { |
| 911 | const s = state orelse return 0; |
| 912 | _ = c.lua_getfield(s, 1, "command"); |
| 913 | const command = getLuaString(s, -1); |
| 914 | c.lua_settop(s, -2); |
| 915 | createBlockTable(s, "Shell", command); |
| 916 | return 1; |
| 917 | } |
| 918 | |
| 919 | fn luaBarBlockStatic(state: ?*c.lua_State) callconv(.c) c_int { |
| 920 | const s = state orelse return 0; |
| 921 | _ = c.lua_getfield(s, 1, "text"); |
| 922 | const text = getLuaString(s, -1); |
| 923 | c.lua_settop(s, -2); |
| 924 | createBlockTable(s, "Static", text); |
| 925 | return 1; |
| 926 | } |
| 927 | |
| 928 | fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int { |
| 929 | const s = state orelse return 0; |
| 930 | |
| 931 | c.lua_createtable(s, 0, 6); |
| 932 | |
| 933 | _ = c.lua_pushstring(s, "Battery"); |
| 934 | c.lua_setfield(s, -2, "__block_type"); |
| 935 | |
| 936 | _ = c.lua_getfield(s, 1, "format"); |
| 937 | c.lua_setfield(s, -2, "format"); |
| 938 | |
| 939 | _ = c.lua_getfield(s, 1, "interval"); |
| 940 | c.lua_setfield(s, -2, "interval"); |
| 941 | |
| 942 | _ = c.lua_getfield(s, 1, "color"); |
| 943 | c.lua_setfield(s, -2, "color"); |
| 944 | |
| 945 | _ = c.lua_getfield(s, 1, "underline"); |
| 946 | c.lua_setfield(s, -2, "underline"); |
| 947 | |
| 948 | c.lua_createtable(s, 0, 4); |
| 949 | _ = c.lua_getfield(s, 1, "charging"); |
| 950 | c.lua_setfield(s, -2, "charging"); |
| 951 | _ = c.lua_getfield(s, 1, "discharging"); |
| 952 | c.lua_setfield(s, -2, "discharging"); |
| 953 | _ = c.lua_getfield(s, 1, "full"); |
| 954 | c.lua_setfield(s, -2, "full"); |
| 955 | _ = c.lua_getfield(s, 1, "battery_name"); |
| 956 | c.lua_setfield(s, -2, "battery_name"); |
| 957 | c.lua_setfield(s, -2, "__arg"); |
| 958 | |
| 959 | return 1; |
| 960 | } |
| 961 | |
| 962 | fn createBlockTable(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]const u8) void { |
| 963 | c.lua_createtable(state, 0, 6); |
| 964 | |
| 965 | _ = c.lua_pushstring(state, block_type); |
| 966 | c.lua_setfield(state, -2, "__block_type"); |
| 967 | |
| 968 | _ = c.lua_getfield(state, 1, "format"); |
| 969 | c.lua_setfield(state, -2, "format"); |
| 970 | |
| 971 | _ = c.lua_getfield(state, 1, "interval"); |
| 972 | c.lua_setfield(state, -2, "interval"); |
| 973 | |
| 974 | _ = c.lua_getfield(state, 1, "color"); |
| 975 | c.lua_setfield(state, -2, "color"); |
| 976 | |
| 977 | _ = c.lua_getfield(state, 1, "underline"); |
| 978 | c.lua_setfield(state, -2, "underline"); |
| 979 | |
| 980 | if (arg) |a| { |
| 981 | var buf: [256]u8 = undefined; |
| 982 | if (a.len < buf.len) { |
| 983 | @memcpy(buf[0..a.len], a); |
| 984 | buf[a.len] = 0; |
| 985 | _ = c.lua_pushstring(state, &buf); |
| 986 | c.lua_setfield(state, -2, "__arg"); |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | fn luaSetTerminal(state: ?*c.lua_State) callconv(.c) c_int { |
| 992 | const cfg = config orelse return 0; |
| 993 | const s = state orelse return 0; |
| 994 | if (dupeLuaString(s, 1)) |term| { |
| 995 | cfg.terminal = term; |
| 996 | } |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | fn luaSetModkey(state: ?*c.lua_State) callconv(.c) c_int { |
| 1001 | const cfg = config orelse return 0; |
| 1002 | const s = state orelse return 0; |
| 1003 | if (getStringArg(s, 1)) |modkey_str| { |
| 1004 | cfg.modkey = parseSingleModifier(modkey_str); |
| 1005 | cfg.addButton(.{ |
| 1006 | .click = .client_win, |
| 1007 | .mod_mask = cfg.modkey, |
| 1008 | .button = 1, |
| 1009 | .action = .move_mouse, |
| 1010 | }) catch {}; |
| 1011 | cfg.addButton(.{ |
| 1012 | .click = .client_win, |
| 1013 | .mod_mask = cfg.modkey, |
| 1014 | .button = 3, |
| 1015 | .action = .resize_mouse, |
| 1016 | }) catch {}; |
| 1017 | } |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | fn luaSetTags(state: ?*c.lua_State) callconv(.c) c_int { |
| 1022 | const cfg = config orelse return 0; |
| 1023 | const s = state orelse return 0; |
| 1024 | |
| 1025 | if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0; |
| 1026 | |
| 1027 | const len = c.lua_rawlen(s, 1); |
| 1028 | var i: usize = 0; |
| 1029 | while (i < len and i < 9) : (i += 1) { |
| 1030 | _ = c.lua_rawgeti(s, 1, @intCast(i + 1)); |
| 1031 | if (dupeLuaString(s, -1)) |tag_str| { |
| 1032 | cfg.tags[i] = tag_str; |
| 1033 | } |
| 1034 | c.lua_settop(s, -2); |
| 1035 | } |
| 1036 | |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
| 1040 | fn luaAutostart(state: ?*c.lua_State) callconv(.c) c_int { |
| 1041 | const cfg = config orelse return 0; |
| 1042 | const s = state orelse return 0; |
| 1043 | if (dupeLuaString(s, 1)) |cmd| { |
| 1044 | cfg.addAutostart(cmd) catch return 0; |
| 1045 | } |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | fn luaAutoTile(state: ?*c.lua_State) callconv(.c) c_int { |
| 1050 | const cfg = config orelse return 0; |
| 1051 | const s = state orelse return 0; |
| 1052 | cfg.auto_tile = c.lua_toboolean(s, 1) != 0; |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
| 1056 | fn luaSetLayoutSymbol(state: ?*c.lua_State) callconv(.c) c_int { |
| 1057 | const cfg = config orelse return 0; |
| 1058 | const s = state orelse return 0; |
| 1059 | const name = getStringArg(s, 1) orelse return 0; |
| 1060 | const symbol = dupeLuaString(s, 2) orelse return 0; |
| 1061 | |
| 1062 | const layout_map = .{ |
| 1063 | .{ "tiling", &cfg.layout_tile_symbol }, |
| 1064 | .{ "tile", &cfg.layout_tile_symbol }, |
| 1065 | .{ "normie", &cfg.layout_floating_symbol }, |
| 1066 | .{ "floating", &cfg.layout_floating_symbol }, |
| 1067 | .{ "float", &cfg.layout_floating_symbol }, |
| 1068 | .{ "monocle", &cfg.layout_monocle_symbol }, |
| 1069 | .{ "scrolling", &cfg.layout_scrolling_symbol }, |
| 1070 | .{ "scroll", &cfg.layout_scrolling_symbol }, |
| 1071 | }; |
| 1072 | |
| 1073 | inline for (layout_map) |entry| { |
| 1074 | if (std.mem.eql(u8, name, entry[0])) { |
| 1075 | entry[1].* = symbol; |
| 1076 | return 0; |
| 1077 | } |
| 1078 | } |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | fn luaQuit(state: ?*c.lua_State) callconv(.c) c_int { |
| 1083 | const s = state orelse return 0; |
| 1084 | createActionTable(s, "Quit"); |
| 1085 | return 1; |
| 1086 | } |
| 1087 | |
| 1088 | fn luaRestart(state: ?*c.lua_State) callconv(.c) c_int { |
| 1089 | const s = state orelse return 0; |
| 1090 | createActionTable(s, "Restart"); |
| 1091 | return 1; |
| 1092 | } |
| 1093 | |
| 1094 | fn luaToggleGaps(state: ?*c.lua_State) callconv(.c) c_int { |
| 1095 | const s = state orelse return 0; |
| 1096 | createActionTable(s, "ToggleGaps"); |
| 1097 | return 1; |
| 1098 | } |
| 1099 | |
| 1100 | fn luaShowKeybinds(state: ?*c.lua_State) callconv(.c) c_int { |
| 1101 | const s = state orelse return 0; |
| 1102 | createActionTable(s, "ShowKeybinds"); |
| 1103 | return 1; |
| 1104 | } |
| 1105 | |
| 1106 | fn luaSetMasterFactor(state: ?*c.lua_State) callconv(.c) c_int { |
| 1107 | const s = state orelse return 0; |
| 1108 | const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 1109 | createActionTableWithInt(s, "ResizeMaster", delta); |
| 1110 | return 1; |
| 1111 | } |
| 1112 | |
| 1113 | fn luaIncNumMaster(state: ?*c.lua_State) callconv(.c) c_int { |
| 1114 | const s = state orelse return 0; |
| 1115 | const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 1116 | if (delta > 0) { |
| 1117 | createActionTable(s, "IncMaster"); |
| 1118 | } else { |
| 1119 | createActionTable(s, "DecMaster"); |
| 1120 | } |
| 1121 | return 1; |
| 1122 | } |
| 1123 | |
| 1124 | fn getStringArg(state: *c.lua_State, idx: c_int) ?[]const u8 { |
| 1125 | if (c.lua_type(state, idx) != c.LUA_TSTRING) return null; |
| 1126 | return getLuaString(state, idx); |
| 1127 | } |
| 1128 | |
| 1129 | fn extractSpawnCommand(state: *c.lua_State, idx: c_int) ?[]const u8 { |
| 1130 | const len = c.lua_rawlen(state, idx); |
| 1131 | if (len == 0) return null; |
| 1132 | |
| 1133 | if (len >= 3) { |
| 1134 | _ = c.lua_rawgeti(state, idx, 1); |
| 1135 | const first = getLuaString(state, -1); |
| 1136 | c.lua_settop(state, -2); |
| 1137 | |
| 1138 | _ = c.lua_rawgeti(state, idx, 2); |
| 1139 | const second = getLuaString(state, -1); |
| 1140 | c.lua_settop(state, -2); |
| 1141 | |
| 1142 | if (first != null and second != null and |
| 1143 | std.mem.eql(u8, first.?, "sh") and std.mem.eql(u8, second.?, "-c")) |
| 1144 | { |
| 1145 | _ = c.lua_rawgeti(state, idx, 3); |
| 1146 | const cmd = getLuaString(state, -1); |
| 1147 | c.lua_settop(state, -2); |
| 1148 | return cmd; |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | _ = c.lua_rawgeti(state, idx, 1); |
| 1153 | const first_elem = getLuaString(state, -1); |
| 1154 | c.lua_settop(state, -2); |
| 1155 | return first_elem; |
| 1156 | } |
| 1157 | |
| 1158 | fn getLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 { |
| 1159 | const cstr = c.lua_tolstring(state, idx, null); |
| 1160 | if (cstr == null) return null; |
| 1161 | return std.mem.span(cstr); |
| 1162 | } |
| 1163 | |
| 1164 | fn dupeLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 { |
| 1165 | const cfg = config orelse return null; |
| 1166 | const lua_str = getLuaString(state, idx) orelse return null; |
| 1167 | const arena_allocator = cfg.string_arena.allocator(); |
| 1168 | const duped = arena_allocator.dupe(u8, lua_str) catch return null; |
| 1169 | return duped; |
| 1170 | } |
| 1171 | |
| 1172 | fn parseColor(state: *c.lua_State, idx: c_int) u32 { |
| 1173 | const lua_type = c.lua_type(state, idx); |
| 1174 | if (lua_type == c.LUA_TNUMBER) { |
| 1175 | return @intCast(c.lua_tointegerx(state, idx, null)); |
| 1176 | } |
| 1177 | if (lua_type == c.LUA_TSTRING) { |
| 1178 | const str = getLuaString(state, idx) orelse return 0; |
| 1179 | if (str.len > 0 and str[0] == '#') { |
| 1180 | return std.fmt.parseInt(u32, str[1..], 16) catch return 0; |
| 1181 | } |
| 1182 | if (str.len > 2 and str[0] == '0' and str[1] == 'x') { |
| 1183 | return std.fmt.parseInt(u32, str[2..], 16) catch return 0; |
| 1184 | } |
| 1185 | return std.fmt.parseInt(u32, str, 16) catch return 0; |
| 1186 | } |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | fn parseScheme(state: *c.lua_State) ColorScheme { |
| 1191 | return ColorScheme{ |
| 1192 | .foreground = parseColor(state, 1), |
| 1193 | .background = parseColor(state, 2), |
| 1194 | .border = parseColor(state, 3), |
| 1195 | }; |
| 1196 | } |
| 1197 | |
| 1198 | fn parseModifiers(state: *c.lua_State, idx: c_int) u32 { |
| 1199 | var mod_mask: u32 = 0; |
| 1200 | |
| 1201 | if (c.lua_type(state, idx) != c.LUA_TTABLE) return mod_mask; |
| 1202 | |
| 1203 | const len = c.lua_rawlen(state, idx); |
| 1204 | var i: usize = 1; |
| 1205 | while (i <= len) : (i += 1) { |
| 1206 | _ = c.lua_rawgeti(state, idx, @intCast(i)); |
| 1207 | if (getLuaString(state, -1)) |mod_str| { |
| 1208 | const parsed = parseSingleModifier(mod_str); |
| 1209 | mod_mask |= parsed; |
| 1210 | } |
| 1211 | c.lua_settop(state, -2); |
| 1212 | } |
| 1213 | |
| 1214 | return mod_mask; |
| 1215 | } |
| 1216 | |
| 1217 | fn parseModifiersAtTop(state: *c.lua_State) u32 { |
| 1218 | return parseModifiers(state, -1); |
| 1219 | } |
| 1220 | |
| 1221 | fn parseSingleModifier(name: []const u8) u32 { |
| 1222 | if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) { |
| 1223 | return (1 << 6); |
| 1224 | } else if (std.mem.eql(u8, name, "Mod1") or std.mem.eql(u8, name, "mod1") or std.mem.eql(u8, name, "alt")) { |
| 1225 | return (1 << 3); |
| 1226 | } else if (std.mem.eql(u8, name, "Shift") or std.mem.eql(u8, name, "shift")) { |
| 1227 | return (1 << 0); |
| 1228 | } else if (std.mem.eql(u8, name, "Control") or std.mem.eql(u8, name, "control") or std.mem.eql(u8, name, "ctrl")) { |
| 1229 | return (1 << 2); |
| 1230 | } |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | fn parseAction(name: []const u8) ?Action { |
| 1235 | const action_map = .{ |
| 1236 | .{ "Spawn", Action.spawn }, |
| 1237 | .{ "SpawnTerminal", Action.spawn_terminal }, |
| 1238 | .{ "KillClient", Action.kill_client }, |
| 1239 | .{ "Quit", Action.quit }, |
| 1240 | .{ "Restart", Action.restart }, |
| 1241 | .{ "ShowKeybinds", Action.show_keybinds }, |
| 1242 | .{ "FocusStack", Action.focus_next }, |
| 1243 | .{ "MoveStack", Action.move_next }, |
| 1244 | .{ "ResizeMaster", Action.resize_master }, |
| 1245 | .{ "IncMaster", Action.inc_master }, |
| 1246 | .{ "DecMaster", Action.dec_master }, |
| 1247 | .{ "ToggleFloating", Action.toggle_floating }, |
| 1248 | .{ "ToggleFullScreen", Action.toggle_fullscreen }, |
| 1249 | .{ "ToggleGaps", Action.toggle_gaps }, |
| 1250 | .{ "CycleLayout", Action.cycle_layout }, |
| 1251 | .{ "ChangeLayout", Action.set_layout }, |
| 1252 | .{ "ViewTag", Action.view_tag }, |
| 1253 | .{ "ViewNextTag", Action.view_next_tag }, |
| 1254 | .{ "ViewPreviousTag", Action.view_prev_tag }, |
| 1255 | .{ "ViewNextNonEmptyTag", Action.view_next_nonempty_tag }, |
| 1256 | .{ "ViewPreviousNonEmptyTag", Action.view_prev_nonempty_tag }, |
| 1257 | .{ "MoveToTag", Action.move_to_tag }, |
| 1258 | .{ "ToggleView", Action.toggle_view_tag }, |
| 1259 | .{ "ToggleTag", Action.toggle_tag }, |
| 1260 | .{ "FocusMonitor", Action.focus_monitor }, |
| 1261 | .{ "TagMonitor", Action.send_to_monitor }, |
| 1262 | .{ "ScrollLeft", Action.scroll_left }, |
| 1263 | .{ "ScrollRight", Action.scroll_right }, |
| 1264 | }; |
| 1265 | |
| 1266 | inline for (action_map) |entry| { |
| 1267 | if (std.mem.eql(u8, name, entry[0])) { |
| 1268 | return entry[1]; |
| 1269 | } |
| 1270 | } |
| 1271 | return null; |
| 1272 | } |
| 1273 | |
| 1274 | fn keyNameToKeysym(name: []const u8) ?u64 { |
| 1275 | const key_map = .{ |
| 1276 | .{ "Return", 0xff0d }, |
| 1277 | .{ "Enter", 0xff0d }, |
| 1278 | .{ "Tab", 0xff09 }, |
| 1279 | .{ "Escape", 0xff1b }, |
| 1280 | .{ "BackSpace", 0xff08 }, |
| 1281 | .{ "Delete", 0xffff }, |
| 1282 | .{ "space", 0x0020 }, |
| 1283 | .{ "Space", 0x0020 }, |
| 1284 | .{ "comma", 0x002c }, |
| 1285 | .{ "Comma", 0x002c }, |
| 1286 | .{ "period", 0x002e }, |
| 1287 | .{ "Period", 0x002e }, |
| 1288 | .{ "slash", 0x002f }, |
| 1289 | .{ "Slash", 0x002f }, |
| 1290 | .{ "minus", 0x002d }, |
| 1291 | .{ "Minus", 0x002d }, |
| 1292 | .{ "equal", 0x003d }, |
| 1293 | .{ "Equal", 0x003d }, |
| 1294 | .{ "bracketleft", 0x005b }, |
| 1295 | .{ "bracketright", 0x005d }, |
| 1296 | .{ "backslash", 0x005c }, |
| 1297 | .{ "semicolon", 0x003b }, |
| 1298 | .{ "apostrophe", 0x0027 }, |
| 1299 | .{ "grave", 0x0060 }, |
| 1300 | .{ "Left", 0xff51 }, |
| 1301 | .{ "Up", 0xff52 }, |
| 1302 | .{ "Right", 0xff53 }, |
| 1303 | .{ "Down", 0xff54 }, |
| 1304 | .{ "F1", 0xffbe }, |
| 1305 | .{ "F2", 0xffbf }, |
| 1306 | .{ "F3", 0xffc0 }, |
| 1307 | .{ "F4", 0xffc1 }, |
| 1308 | .{ "F5", 0xffc2 }, |
| 1309 | .{ "F6", 0xffc3 }, |
| 1310 | .{ "F7", 0xffc4 }, |
| 1311 | .{ "F8", 0xffc5 }, |
| 1312 | .{ "F9", 0xffc6 }, |
| 1313 | .{ "F10", 0xffc7 }, |
| 1314 | .{ "F11", 0xffc8 }, |
| 1315 | .{ "F12", 0xffc9 }, |
| 1316 | .{ "Print", 0xff61 }, |
| 1317 | .{ "XF86AudioRaiseVolume", 0x1008ff13 }, |
| 1318 | .{ "XF86AudioLowerVolume", 0x1008ff11 }, |
| 1319 | .{ "XF86AudioMute", 0x1008ff12 }, |
| 1320 | .{ "XF86AudioPlay", 0x1008ff14 }, |
| 1321 | .{ "XF86AudioPause", 0x1008ff31 }, |
| 1322 | .{ "XF86AudioNext", 0x1008ff17 }, |
| 1323 | .{ "XF86AudioPrev", 0x1008ff16 }, |
| 1324 | .{ "XF86MonBrightnessUp", 0x1008ff02 }, |
| 1325 | .{ "XF86MonBrightnessDown", 0x1008ff03 }, |
| 1326 | }; |
| 1327 | |
| 1328 | inline for (key_map) |entry| { |
| 1329 | if (std.mem.eql(u8, name, entry[0])) { |
| 1330 | return entry[1]; |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | if (name.len == 1) { |
| 1335 | const char = name[0]; |
| 1336 | if (char >= 'a' and char <= 'z') { |
| 1337 | return char; |
| 1338 | } |
| 1339 | if (char >= 'A' and char <= 'Z') { |
| 1340 | return char + 32; |
| 1341 | } |
| 1342 | if (char >= '0' and char <= '9') { |
| 1343 | return char; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | return null; |
| 1348 | } |