| 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.bufPrintZ(&setup_buf, "package.path = '{s}/?.lua;' .. package.path", .{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(env: *std.process.Environ.Map) bool { |
| 75 | const home = env.get("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 | registerScrollModule(state); |
| 93 | registerTagModule(state); |
| 94 | registerMonitorModule(state); |
| 95 | registerRuleModule(state); |
| 96 | registerBarModule(state); |
| 97 | registerMiscFunctions(state); |
| 98 | |
| 99 | c.lua_setglobal(state, "oxwm"); |
| 100 | } |
| 101 | |
| 102 | fn registerSpawnFunctions(state: *c.lua_State) void { |
| 103 | c.lua_pushcfunction(state, luaSpawn); |
| 104 | c.lua_setfield(state, -2, "spawn"); |
| 105 | |
| 106 | c.lua_pushcfunction(state, luaSpawnTerminal); |
| 107 | c.lua_setfield(state, -2, "spawn_terminal"); |
| 108 | } |
| 109 | |
| 110 | fn registerKeyModule(state: *c.lua_State) void { |
| 111 | c.lua_createtable(state, 0, 2); |
| 112 | |
| 113 | c.lua_pushcfunction(state, luaKeyBind); |
| 114 | c.lua_setfield(state, -2, "bind"); |
| 115 | |
| 116 | c.lua_pushcfunction(state, luaKeyChord); |
| 117 | c.lua_setfield(state, -2, "chord"); |
| 118 | |
| 119 | c.lua_setfield(state, -2, "key"); |
| 120 | } |
| 121 | |
| 122 | fn registerGapsModule(state: *c.lua_State) void { |
| 123 | c.lua_createtable(state, 0, 6); |
| 124 | |
| 125 | c.lua_pushcfunction(state, luaGapsSetEnabled); |
| 126 | c.lua_setfield(state, -2, "set_enabled"); |
| 127 | |
| 128 | c.lua_pushcfunction(state, luaGapsEnable); |
| 129 | c.lua_setfield(state, -2, "enable"); |
| 130 | |
| 131 | c.lua_pushcfunction(state, luaGapsDisable); |
| 132 | c.lua_setfield(state, -2, "disable"); |
| 133 | |
| 134 | c.lua_pushcfunction(state, luaGapsSetInner); |
| 135 | c.lua_setfield(state, -2, "set_inner"); |
| 136 | |
| 137 | c.lua_pushcfunction(state, luaGapsSetOuter); |
| 138 | c.lua_setfield(state, -2, "set_outer"); |
| 139 | |
| 140 | c.lua_pushcfunction(state, luaGapsSetSmart); |
| 141 | c.lua_setfield(state, -2, "set_smart"); |
| 142 | |
| 143 | c.lua_setfield(state, -2, "gaps"); |
| 144 | } |
| 145 | |
| 146 | fn registerBorderModule(state: *c.lua_State) void { |
| 147 | c.lua_createtable(state, 0, 3); |
| 148 | |
| 149 | c.lua_pushcfunction(state, luaBorderSetWidth); |
| 150 | c.lua_setfield(state, -2, "set_width"); |
| 151 | |
| 152 | c.lua_pushcfunction(state, luaBorderSetFocusedColor); |
| 153 | c.lua_setfield(state, -2, "set_focused_color"); |
| 154 | |
| 155 | c.lua_pushcfunction(state, luaBorderSetUnfocusedColor); |
| 156 | c.lua_setfield(state, -2, "set_unfocused_color"); |
| 157 | |
| 158 | c.lua_setfield(state, -2, "border"); |
| 159 | } |
| 160 | |
| 161 | fn registerClientModule(state: *c.lua_State) void { |
| 162 | c.lua_createtable(state, 0, 5); |
| 163 | |
| 164 | c.lua_pushcfunction(state, luaClientKill); |
| 165 | c.lua_setfield(state, -2, "kill"); |
| 166 | |
| 167 | c.lua_pushcfunction(state, luaClientToggleFullscreen); |
| 168 | c.lua_setfield(state, -2, "toggle_fullscreen"); |
| 169 | |
| 170 | c.lua_pushcfunction(state, luaClientToggleFloating); |
| 171 | c.lua_setfield(state, -2, "toggle_floating"); |
| 172 | |
| 173 | c.lua_pushcfunction(state, luaClientFocusStack); |
| 174 | c.lua_setfield(state, -2, "focus_stack"); |
| 175 | |
| 176 | c.lua_pushcfunction(state, luaClientMoveStack); |
| 177 | c.lua_setfield(state, -2, "move_stack"); |
| 178 | |
| 179 | c.lua_setfield(state, -2, "client"); |
| 180 | } |
| 181 | |
| 182 | fn registerLayoutModule(state: *c.lua_State) void { |
| 183 | c.lua_createtable(state, 0, 4); |
| 184 | |
| 185 | c.lua_pushcfunction(state, luaLayoutCycle); |
| 186 | c.lua_setfield(state, -2, "cycle"); |
| 187 | |
| 188 | c.lua_pushcfunction(state, luaLayoutSet); |
| 189 | c.lua_setfield(state, -2, "set"); |
| 190 | |
| 191 | c.lua_pushcfunction(state, luaLayoutScrollLeft); |
| 192 | c.lua_setfield(state, -2, "scroll_left"); |
| 193 | |
| 194 | c.lua_pushcfunction(state, luaLayoutScrollRight); |
| 195 | c.lua_setfield(state, -2, "scroll_right"); |
| 196 | |
| 197 | c.lua_pushcfunction(state, luaLayoutCycleWidth); |
| 198 | c.lua_setfield(state, -2, "cycle_width"); |
| 199 | |
| 200 | c.lua_pushcfunction(state, luaLayoutSetWidth); |
| 201 | c.lua_setfield(state, -2, "set_width"); |
| 202 | |
| 203 | c.lua_setfield(state, -2, "layout"); |
| 204 | } |
| 205 | |
| 206 | fn registerScrollModule(state: *c.lua_State) void { |
| 207 | c.lua_createtable(state, 0, 6); |
| 208 | |
| 209 | c.lua_pushcfunction(state, luaScrollSetDefaultWidth); |
| 210 | c.lua_setfield(state, -2, "set_default_width"); |
| 211 | |
| 212 | c.lua_pushcfunction(state, luaScrollSetWidthPresets); |
| 213 | c.lua_setfield(state, -2, "set_width_presets"); |
| 214 | |
| 215 | c.lua_pushcfunction(state, luaScrollSetGestureEnabled); |
| 216 | c.lua_setfield(state, -2, "set_gesture_enabled"); |
| 217 | |
| 218 | c.lua_pushcfunction(state, luaScrollSetGestureFingers); |
| 219 | c.lua_setfield(state, -2, "set_gesture_fingers"); |
| 220 | |
| 221 | c.lua_pushcfunction(state, luaScrollSetGestureSpeed); |
| 222 | c.lua_setfield(state, -2, "set_gesture_speed"); |
| 223 | |
| 224 | c.lua_pushcfunction(state, luaScrollSetGestureNatural); |
| 225 | c.lua_setfield(state, -2, "set_gesture_natural"); |
| 226 | |
| 227 | c.lua_pushcfunction(state, luaScrollSetIndicator); |
| 228 | c.lua_setfield(state, -2, "set_indicator"); |
| 229 | |
| 230 | c.lua_pushcfunction(state, luaScrollSetIndicatorWidth); |
| 231 | c.lua_setfield(state, -2, "set_indicator_width"); |
| 232 | |
| 233 | c.lua_setfield(state, -2, "scroll"); |
| 234 | } |
| 235 | |
| 236 | fn registerTagModule(state: *c.lua_State) void { |
| 237 | c.lua_createtable(state, 0, 10); |
| 238 | |
| 239 | c.lua_pushcfunction(state, luaTagView); |
| 240 | c.lua_setfield(state, -2, "view"); |
| 241 | |
| 242 | c.lua_pushcfunction(state, luaTagViewNext); |
| 243 | c.lua_setfield(state, -2, "view_next"); |
| 244 | |
| 245 | c.lua_pushcfunction(state, luaTagViewPrevious); |
| 246 | c.lua_setfield(state, -2, "view_previous"); |
| 247 | |
| 248 | c.lua_pushcfunction(state, luaTagViewNextNonempty); |
| 249 | c.lua_setfield(state, -2, "view_next_nonempty"); |
| 250 | |
| 251 | c.lua_pushcfunction(state, luaTagViewPreviousNonempty); |
| 252 | c.lua_setfield(state, -2, "view_previous_nonempty"); |
| 253 | |
| 254 | c.lua_pushcfunction(state, luaTagToggleview); |
| 255 | c.lua_setfield(state, -2, "toggleview"); |
| 256 | |
| 257 | c.lua_pushcfunction(state, luaTagMoveTo); |
| 258 | c.lua_setfield(state, -2, "move_to"); |
| 259 | |
| 260 | c.lua_pushcfunction(state, luaTagToggletag); |
| 261 | c.lua_setfield(state, -2, "toggletag"); |
| 262 | |
| 263 | c.lua_pushcfunction(state, luaTagSetBackAndForth); |
| 264 | c.lua_setfield(state, -2, "set_back_and_forth"); |
| 265 | |
| 266 | c.lua_setfield(state, -2, "tag"); |
| 267 | } |
| 268 | |
| 269 | fn registerMonitorModule(state: *c.lua_State) void { |
| 270 | c.lua_createtable(state, 0, 4); |
| 271 | |
| 272 | c.lua_pushcfunction(state, luaMonitorFocus); |
| 273 | c.lua_setfield(state, -2, "focus"); |
| 274 | |
| 275 | c.lua_pushcfunction(state, luaMonitorTag); |
| 276 | c.lua_setfield(state, -2, "tag"); |
| 277 | |
| 278 | c.lua_pushcfunction(state, luaMonitorWarpCursor); |
| 279 | c.lua_setfield(state, -2, "warp_cursor"); |
| 280 | |
| 281 | c.lua_pushcfunction(state, luaMonitorWarpCursorOnSend); |
| 282 | c.lua_setfield(state, -2, "warp_cursor_on_send"); |
| 283 | |
| 284 | c.lua_setfield(state, -2, "monitor"); |
| 285 | } |
| 286 | |
| 287 | fn registerRuleModule(state: *c.lua_State) void { |
| 288 | c.lua_createtable(state, 0, 1); |
| 289 | |
| 290 | c.lua_pushcfunction(state, luaRuleAdd); |
| 291 | c.lua_setfield(state, -2, "add"); |
| 292 | |
| 293 | c.lua_setfield(state, -2, "rule"); |
| 294 | } |
| 295 | |
| 296 | fn registerBarModule(state: *c.lua_State) void { |
| 297 | c.lua_createtable(state, 0, 10); |
| 298 | |
| 299 | c.lua_pushcfunction(state, luaBarSetFont); |
| 300 | c.lua_setfield(state, -2, "set_font"); |
| 301 | |
| 302 | c.lua_pushcfunction(state, luaBarSetBlocks); |
| 303 | c.lua_setfield(state, -2, "set_blocks"); |
| 304 | |
| 305 | c.lua_pushcfunction(state, luaBarSetSchemeNormal); |
| 306 | c.lua_setfield(state, -2, "set_scheme_normal"); |
| 307 | |
| 308 | c.lua_pushcfunction(state, luaBarSetSchemeSelected); |
| 309 | c.lua_setfield(state, -2, "set_scheme_selected"); |
| 310 | |
| 311 | c.lua_pushcfunction(state, luaBarSetSchemeOccupied); |
| 312 | c.lua_setfield(state, -2, "set_scheme_occupied"); |
| 313 | |
| 314 | c.lua_pushcfunction(state, luaBarSetSchemeUrgent); |
| 315 | c.lua_setfield(state, -2, "set_scheme_urgent"); |
| 316 | |
| 317 | c.lua_pushcfunction(state, luaBarSetHideVacantTags); |
| 318 | c.lua_setfield(state, -2, "set_hide_vacant_tags"); |
| 319 | |
| 320 | c.lua_pushcfunction(state, luaBarSetPosition); |
| 321 | c.lua_setfield(state, -2, "set_position"); |
| 322 | |
| 323 | c.lua_createtable(state, 0, 6); |
| 324 | |
| 325 | c.lua_pushcfunction(state, luaBarBlockRam); |
| 326 | c.lua_setfield(state, -2, "ram"); |
| 327 | |
| 328 | c.lua_pushcfunction(state, luaBarBlockDatetime); |
| 329 | c.lua_setfield(state, -2, "datetime"); |
| 330 | |
| 331 | c.lua_pushcfunction(state, luaBarBlockShell); |
| 332 | c.lua_setfield(state, -2, "shell"); |
| 333 | |
| 334 | c.lua_pushcfunction(state, luaBarBlockStatic); |
| 335 | c.lua_setfield(state, -2, "static"); |
| 336 | |
| 337 | c.lua_pushcfunction(state, luaBarBlockBattery); |
| 338 | c.lua_setfield(state, -2, "battery"); |
| 339 | |
| 340 | c.lua_pushcfunction(state, luaBarBlockSystray); |
| 341 | c.lua_setfield(state, -2, "systray"); |
| 342 | |
| 343 | c.lua_setfield(state, -2, "block"); |
| 344 | |
| 345 | c.lua_setfield(state, -2, "bar"); |
| 346 | } |
| 347 | |
| 348 | fn registerMiscFunctions(state: *c.lua_State) void { |
| 349 | c.lua_pushcfunction(state, luaSetTerminal); |
| 350 | c.lua_setfield(state, -2, "set_terminal"); |
| 351 | |
| 352 | c.lua_pushcfunction(state, luaSetLayout); |
| 353 | c.lua_setfield(state, -2, "set_layout"); |
| 354 | |
| 355 | c.lua_pushcfunction(state, luaSetModkey); |
| 356 | c.lua_setfield(state, -2, "set_modkey"); |
| 357 | |
| 358 | c.lua_pushcfunction(state, luaSetTags); |
| 359 | c.lua_setfield(state, -2, "set_tags"); |
| 360 | |
| 361 | c.lua_pushcfunction(state, luaSetLayoutSymbol); |
| 362 | c.lua_setfield(state, -2, "set_layout_symbol"); |
| 363 | |
| 364 | c.lua_pushcfunction(state, luaSetAttachMethod); |
| 365 | c.lua_setfield(state, -2, "set_attach_method"); |
| 366 | |
| 367 | c.lua_pushcfunction(state, luaAutostart); |
| 368 | c.lua_setfield(state, -2, "autostart"); |
| 369 | |
| 370 | c.lua_pushcfunction(state, luaAutoTile); |
| 371 | c.lua_setfield(state, -2, "auto_tile"); |
| 372 | |
| 373 | c.lua_pushcfunction(state, luaTiledResizeMode); |
| 374 | c.lua_setfield(state, -2, "tiled_resize_mode"); |
| 375 | |
| 376 | c.lua_pushcfunction(state, luaQuit); |
| 377 | c.lua_setfield(state, -2, "quit"); |
| 378 | |
| 379 | c.lua_pushcfunction(state, luaRestart); |
| 380 | c.lua_setfield(state, -2, "restart"); |
| 381 | |
| 382 | c.lua_pushcfunction(state, luaToggleGaps); |
| 383 | c.lua_setfield(state, -2, "toggle_gaps"); |
| 384 | |
| 385 | c.lua_pushcfunction(state, luaToggleBar); |
| 386 | c.lua_setfield(state, -2, "toggle_bar"); |
| 387 | |
| 388 | c.lua_pushcfunction(state, luaShowKeybinds); |
| 389 | c.lua_setfield(state, -2, "show_keybinds"); |
| 390 | |
| 391 | c.lua_pushcfunction(state, luaSetMasterFactor); |
| 392 | c.lua_setfield(state, -2, "set_master_factor"); |
| 393 | |
| 394 | c.lua_pushcfunction(state, luaIncNumMaster); |
| 395 | c.lua_setfield(state, -2, "inc_num_master"); |
| 396 | |
| 397 | c.lua_pushcfunction(state, luaSetTagLayout); |
| 398 | c.lua_setfield(state, -2, "set_tag_layout"); |
| 399 | |
| 400 | c.lua_pushcfunction(state, luaSetFloatingPosition); |
| 401 | c.lua_setfield(state, -2, "set_floating_position"); |
| 402 | } |
| 403 | |
| 404 | fn createActionTable(state: *c.lua_State, action_name: [*:0]const u8) void { |
| 405 | c.lua_createtable(state, 0, 2); |
| 406 | _ = c.lua_pushstring(state, action_name); |
| 407 | c.lua_setfield(state, -2, "__action"); |
| 408 | } |
| 409 | |
| 410 | fn createActionTableWithInt(state: *c.lua_State, action_name: [*:0]const u8, arg: i32) void { |
| 411 | c.lua_createtable(state, 0, 2); |
| 412 | _ = c.lua_pushstring(state, action_name); |
| 413 | c.lua_setfield(state, -2, "__action"); |
| 414 | c.lua_pushinteger(state, arg); |
| 415 | c.lua_setfield(state, -2, "__arg"); |
| 416 | } |
| 417 | |
| 418 | fn createActionTableWithString(state: *c.lua_State, action_name: [*:0]const u8) void { |
| 419 | c.lua_createtable(state, 0, 2); |
| 420 | _ = c.lua_pushstring(state, action_name); |
| 421 | c.lua_setfield(state, -2, "__action"); |
| 422 | c.lua_pushvalue(state, 1); |
| 423 | c.lua_setfield(state, -2, "__arg"); |
| 424 | } |
| 425 | |
| 426 | fn luaSpawn(state: ?*c.lua_State) callconv(.c) c_int { |
| 427 | const s = state orelse return 0; |
| 428 | createActionTableWithString(s, "Spawn"); |
| 429 | return 1; |
| 430 | } |
| 431 | |
| 432 | fn luaSpawnTerminal(state: ?*c.lua_State) callconv(.c) c_int { |
| 433 | const s = state orelse return 0; |
| 434 | createActionTable(s, "SpawnTerminal"); |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | fn luaKeyBind(state: ?*c.lua_State) callconv(.c) c_int { |
| 439 | const s = state orelse return 0; |
| 440 | const cfg = config orelse return 0; |
| 441 | |
| 442 | const mod_mask = parseModifiers(s, 1); |
| 443 | const key_str = getStringArg(s, 2) orelse return 0; |
| 444 | const keysym = keynameToKeysym(key_str) orelse return 0; |
| 445 | |
| 446 | if (c.lua_type(s, 3) != c.LUA_TTABLE) return 0; |
| 447 | |
| 448 | _ = c.lua_getfield(s, 3, "__action"); |
| 449 | const action_str = getLuaString(s, -1) orelse { |
| 450 | c.lua_settop(s, -2); |
| 451 | return 0; |
| 452 | }; |
| 453 | c.lua_settop(s, -2); |
| 454 | |
| 455 | const action = parseAction(action_str) orelse return 0; |
| 456 | |
| 457 | var int_arg: i32 = 0; |
| 458 | var str_arg: ?[]const u8 = null; |
| 459 | |
| 460 | _ = c.lua_getfield(s, 3, "__arg"); |
| 461 | if (c.lua_type(s, -1) == c.LUA_TNUMBER) { |
| 462 | int_arg = @intCast(c.lua_tointegerx(s, -1, null)); |
| 463 | } else if (c.lua_type(s, -1) == c.LUA_TSTRING) { |
| 464 | str_arg = getLuaString(s, -1); |
| 465 | } else if (c.lua_type(s, -1) == c.LUA_TTABLE) { |
| 466 | str_arg = extractSpawnCommand(s, -1); |
| 467 | } |
| 468 | c.lua_settop(s, -2); |
| 469 | |
| 470 | var keybind: config_mod.Keybind = .{ |
| 471 | .action = action, |
| 472 | .int_arg = int_arg, |
| 473 | .str_arg = str_arg, |
| 474 | }; |
| 475 | keybind.keys[0] = .{ .mod_mask = mod_mask, .keysym = keysym }; |
| 476 | keybind.key_count = 1; |
| 477 | |
| 478 | cfg.addKeybind(keybind) catch return 0; |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | fn luaKeyChord(state: ?*c.lua_State) callconv(.c) c_int { |
| 484 | const s = state orelse return 0; |
| 485 | const cfg = config orelse return 0; |
| 486 | |
| 487 | if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0; |
| 488 | if (c.lua_type(s, 2) != c.LUA_TTABLE) return 0; |
| 489 | |
| 490 | var keybind: config_mod.Keybind = .{ |
| 491 | .action = .quit, |
| 492 | .int_arg = 0, |
| 493 | .str_arg = null, |
| 494 | }; |
| 495 | keybind.key_count = 0; |
| 496 | |
| 497 | const num_keys = c.lua_rawlen(s, 1); |
| 498 | if (num_keys == 0 or num_keys > 4) return 0; |
| 499 | |
| 500 | var i: usize = 1; |
| 501 | while (i <= num_keys) : (i += 1) { |
| 502 | _ = c.lua_rawgeti(s, 1, @intCast(i)); |
| 503 | if (c.lua_type(s, -1) != c.LUA_TTABLE) { |
| 504 | c.lua_settop(s, -2); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | _ = c.lua_rawgeti(s, -1, 1); |
| 509 | const mod_mask = parseModifiersAtTop(s); |
| 510 | c.lua_settop(s, -2); |
| 511 | |
| 512 | _ = c.lua_rawgeti(s, -1, 2); |
| 513 | const key_str = getLuaString(s, -1) orelse { |
| 514 | c.lua_settop(s, -3); |
| 515 | return 0; |
| 516 | }; |
| 517 | c.lua_settop(s, -2); |
| 518 | |
| 519 | const keysym = keynameToKeysym(key_str) orelse { |
| 520 | c.lua_settop(s, -2); |
| 521 | return 0; |
| 522 | }; |
| 523 | |
| 524 | keybind.keys[keybind.key_count] = .{ .mod_mask = mod_mask, .keysym = keysym }; |
| 525 | keybind.key_count += 1; |
| 526 | |
| 527 | c.lua_settop(s, -2); |
| 528 | } |
| 529 | |
| 530 | _ = c.lua_getfield(s, 2, "__action"); |
| 531 | const action_str = getLuaString(s, -1) orelse { |
| 532 | c.lua_settop(s, -2); |
| 533 | return 0; |
| 534 | }; |
| 535 | c.lua_settop(s, -2); |
| 536 | |
| 537 | keybind.action = parseAction(action_str) orelse return 0; |
| 538 | |
| 539 | _ = c.lua_getfield(s, 2, "__arg"); |
| 540 | if (c.lua_type(s, -1) == c.LUA_TNUMBER) { |
| 541 | keybind.int_arg = @intCast(c.lua_tointegerx(s, -1, null)); |
| 542 | } else if (c.lua_type(s, -1) == c.LUA_TSTRING) { |
| 543 | keybind.str_arg = getLuaString(s, -1); |
| 544 | } else if (c.lua_type(s, -1) == c.LUA_TTABLE) { |
| 545 | keybind.str_arg = extractSpawnCommand(s, -1); |
| 546 | } |
| 547 | |
| 548 | c.lua_settop(s, -2); |
| 549 | cfg.addKeybind(keybind) catch return 0; |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | fn luaGapsSetEnabled(state: ?*c.lua_State) callconv(.c) c_int { |
| 555 | const cfg = config orelse return 0; |
| 556 | const s = state orelse return 0; |
| 557 | cfg.gaps_enabled = c.lua_toboolean(s, 1) != 0; |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | fn luaGapsEnable(state: ?*c.lua_State) callconv(.c) c_int { |
| 562 | _ = state; |
| 563 | const cfg = config orelse return 0; |
| 564 | cfg.gaps_enabled = true; |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | fn luaGapsDisable(state: ?*c.lua_State) callconv(.c) c_int { |
| 569 | _ = state; |
| 570 | const cfg = config orelse return 0; |
| 571 | cfg.gaps_enabled = false; |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | fn luaGapsSetInner(state: ?*c.lua_State) callconv(.c) c_int { |
| 576 | const cfg = config orelse return 0; |
| 577 | const s = state orelse return 0; |
| 578 | cfg.gap_inner_h = @intCast(c.lua_tointegerx(s, 1, null)); |
| 579 | cfg.gap_inner_v = @intCast(c.lua_tointegerx(s, 2, null)); |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | fn luaGapsSetOuter(state: ?*c.lua_State) callconv(.c) c_int { |
| 584 | const cfg = config orelse return 0; |
| 585 | const s = state orelse return 0; |
| 586 | cfg.gap_outer_h = @intCast(c.lua_tointegerx(s, 1, null)); |
| 587 | cfg.gap_outer_v = @intCast(c.lua_tointegerx(s, 2, null)); |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | fn luaGapsSetSmart(state: ?*c.lua_State) callconv(.c) c_int { |
| 592 | const cfg = config orelse return 0; |
| 593 | const s = state orelse return 0; |
| 594 | cfg.smartgaps_enabled = c.lua_toboolean(s, 1) != 0; |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | fn luaBorderSetWidth(state: ?*c.lua_State) callconv(.c) c_int { |
| 599 | const cfg = config orelse return 0; |
| 600 | const s = state orelse return 0; |
| 601 | cfg.border_width = @intCast(c.lua_tointegerx(s, 1, null)); |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | fn luaBorderSetFocusedColor(state: ?*c.lua_State) callconv(.c) c_int { |
| 606 | const cfg = config orelse return 0; |
| 607 | const s = state orelse return 0; |
| 608 | cfg.border_focused = parseColor(s, 1); |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | fn luaBorderSetUnfocusedColor(state: ?*c.lua_State) callconv(.c) c_int { |
| 613 | const cfg = config orelse return 0; |
| 614 | const s = state orelse return 0; |
| 615 | cfg.border_unfocused = parseColor(s, 1); |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | fn luaClientKill(state: ?*c.lua_State) callconv(.c) c_int { |
| 620 | const s = state orelse return 0; |
| 621 | createActionTable(s, "KillClient"); |
| 622 | return 1; |
| 623 | } |
| 624 | |
| 625 | fn luaClientToggleFullscreen(state: ?*c.lua_State) callconv(.c) c_int { |
| 626 | const s = state orelse return 0; |
| 627 | createActionTable(s, "ToggleFullScreen"); |
| 628 | return 1; |
| 629 | } |
| 630 | |
| 631 | fn luaClientToggleFloating(state: ?*c.lua_State) callconv(.c) c_int { |
| 632 | const s = state orelse return 0; |
| 633 | createActionTable(s, "ToggleFloating"); |
| 634 | return 1; |
| 635 | } |
| 636 | |
| 637 | fn luaClientFocusStack(state: ?*c.lua_State) callconv(.c) c_int { |
| 638 | const s = state orelse return 0; |
| 639 | var isnum: c_int = 0; |
| 640 | const raw = c.lua_tointegerx(s, 1, &isnum); |
| 641 | const dir: i32 = if (isnum != 0) @intCast(raw) else 1; |
| 642 | createActionTableWithInt(s, "FocusStack", dir); |
| 643 | return 1; |
| 644 | } |
| 645 | |
| 646 | fn luaClientMoveStack(state: ?*c.lua_State) callconv(.c) c_int { |
| 647 | const s = state orelse return 0; |
| 648 | var isnum: c_int = 0; |
| 649 | const raw = c.lua_tointegerx(s, 1, &isnum); |
| 650 | const dir: i32 = if (isnum != 0) @intCast(raw) else 1; |
| 651 | createActionTableWithInt(s, "MoveStack", dir); |
| 652 | return 1; |
| 653 | } |
| 654 | |
| 655 | fn luaLayoutCycle(state: ?*c.lua_State) callconv(.c) c_int { |
| 656 | const s = state orelse return 0; |
| 657 | createActionTable(s, "CycleLayout"); |
| 658 | return 1; |
| 659 | } |
| 660 | |
| 661 | fn luaLayoutSet(state: ?*c.lua_State) callconv(.c) c_int { |
| 662 | const s = state orelse return 0; |
| 663 | createActionTableWithString(s, "ChangeLayout"); |
| 664 | return 1; |
| 665 | } |
| 666 | |
| 667 | fn luaLayoutScrollLeft(state: ?*c.lua_State) callconv(.c) c_int { |
| 668 | const s = state orelse return 0; |
| 669 | createActionTable(s, "ScrollLeft"); |
| 670 | return 1; |
| 671 | } |
| 672 | |
| 673 | fn luaLayoutScrollRight(state: ?*c.lua_State) callconv(.c) c_int { |
| 674 | const s = state orelse return 0; |
| 675 | createActionTable(s, "ScrollRight"); |
| 676 | return 1; |
| 677 | } |
| 678 | |
| 679 | fn luaLayoutCycleWidth(state: ?*c.lua_State) callconv(.c) c_int { |
| 680 | const s = state orelse return 0; |
| 681 | var isnum: c_int = 0; |
| 682 | const raw = c.lua_tointegerx(s, 1, &isnum); |
| 683 | const dir: i32 = if (isnum != 0) @intCast(raw) else 1; |
| 684 | createActionTableWithInt(s, "CycleWidth", dir); |
| 685 | return 1; |
| 686 | } |
| 687 | |
| 688 | fn luaLayoutSetWidth(state: ?*c.lua_State) callconv(.c) c_int { |
| 689 | const s = state orelse return 0; |
| 690 | var isnum: c_int = 0; |
| 691 | const value = c.lua_tonumberx(s, 1, &isnum); |
| 692 | const permille: i32 = if (isnum != 0 and value > 0) @intFromFloat(@round(value * 1000.0)) else 0; |
| 693 | createActionTableWithInt(s, "SetWidth", permille); |
| 694 | return 1; |
| 695 | } |
| 696 | |
| 697 | fn luaScrollSetDefaultWidth(state: ?*c.lua_State) callconv(.c) c_int { |
| 698 | const cfg = config orelse return 0; |
| 699 | const s = state orelse return 0; |
| 700 | var isnum: c_int = 0; |
| 701 | const value = c.lua_tonumberx(s, 1, &isnum); |
| 702 | if (isnum != 0 and value > 0) cfg.scroll_default_width = @floatCast(value); |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | fn luaScrollSetWidthPresets(state: ?*c.lua_State) callconv(.c) c_int { |
| 707 | const cfg = config orelse return 0; |
| 708 | const s = state orelse return 0; |
| 709 | if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0; |
| 710 | |
| 711 | const len = c.lua_rawlen(s, 1); |
| 712 | var count: u32 = 0; |
| 713 | var i: c_longlong = 1; |
| 714 | while (i <= len and count < cfg.scroll_width_presets.len) : (i += 1) { |
| 715 | _ = c.lua_rawgeti(s, 1, i); |
| 716 | var isnum: c_int = 0; |
| 717 | const value = c.lua_tonumberx(s, -1, &isnum); |
| 718 | c.lua_settop(s, -2); |
| 719 | if (isnum == 0 or value <= 0) continue; |
| 720 | cfg.scroll_width_presets[count] = @floatCast(value); |
| 721 | count += 1; |
| 722 | } |
| 723 | if (count > 0) cfg.scroll_width_preset_count = count; |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | fn luaScrollSetGestureEnabled(state: ?*c.lua_State) callconv(.c) c_int { |
| 728 | const cfg = config orelse return 0; |
| 729 | const s = state orelse return 0; |
| 730 | cfg.gesture_enabled = c.lua_toboolean(s, 1) != 0; |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | fn luaScrollSetGestureFingers(state: ?*c.lua_State) callconv(.c) c_int { |
| 735 | const cfg = config orelse return 0; |
| 736 | const s = state orelse return 0; |
| 737 | const value = c.lua_tointegerx(s, 1, null); |
| 738 | if (value >= 3 and value <= 5) cfg.gesture_fingers = @intCast(value); |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | fn luaScrollSetGestureSpeed(state: ?*c.lua_State) callconv(.c) c_int { |
| 743 | const cfg = config orelse return 0; |
| 744 | const s = state orelse return 0; |
| 745 | var isnum: c_int = 0; |
| 746 | const value = c.lua_tonumberx(s, 1, &isnum); |
| 747 | if (isnum != 0 and value > 0) cfg.gesture_speed = @floatCast(value); |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | fn luaScrollSetGestureNatural(state: ?*c.lua_State) callconv(.c) c_int { |
| 752 | const cfg = config orelse return 0; |
| 753 | const s = state orelse return 0; |
| 754 | cfg.gesture_natural = c.lua_toboolean(s, 1) != 0; |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | fn luaScrollSetIndicator(state: ?*c.lua_State) callconv(.c) c_int { |
| 759 | const cfg = config orelse return 0; |
| 760 | const s = state orelse return 0; |
| 761 | cfg.scroll_indicator = c.lua_toboolean(s, 1) != 0; |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | fn luaScrollSetIndicatorWidth(state: ?*c.lua_State) callconv(.c) c_int { |
| 766 | const cfg = config orelse return 0; |
| 767 | const s = state orelse return 0; |
| 768 | const value = c.lua_tointegerx(s, 1, null); |
| 769 | if (value >= 0) cfg.scroll_indicator_width = @intCast(value); |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | fn luaTagView(state: ?*c.lua_State) callconv(.c) c_int { |
| 774 | const s = state orelse return 0; |
| 775 | const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 776 | createActionTableWithInt(s, "ViewTag", idx); |
| 777 | return 1; |
| 778 | } |
| 779 | |
| 780 | fn luaTagViewNext(state: ?*c.lua_State) callconv(.c) c_int { |
| 781 | const s = state orelse return 0; |
| 782 | createActionTable(s, "ViewNextTag"); |
| 783 | return 1; |
| 784 | } |
| 785 | |
| 786 | fn luaTagViewPrevious(state: ?*c.lua_State) callconv(.c) c_int { |
| 787 | const s = state orelse return 0; |
| 788 | createActionTable(s, "ViewPreviousTag"); |
| 789 | return 1; |
| 790 | } |
| 791 | |
| 792 | fn luaTagViewNextNonempty(state: ?*c.lua_State) callconv(.c) c_int { |
| 793 | const s = state orelse return 0; |
| 794 | createActionTable(s, "ViewNextNonEmptyTag"); |
| 795 | return 1; |
| 796 | } |
| 797 | |
| 798 | fn luaTagViewPreviousNonempty(state: ?*c.lua_State) callconv(.c) c_int { |
| 799 | const s = state orelse return 0; |
| 800 | createActionTable(s, "ViewPreviousNonEmptyTag"); |
| 801 | return 1; |
| 802 | } |
| 803 | |
| 804 | fn luaTagToggleview(state: ?*c.lua_State) callconv(.c) c_int { |
| 805 | const s = state orelse return 0; |
| 806 | const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 807 | createActionTableWithInt(s, "ToggleView", idx); |
| 808 | return 1; |
| 809 | } |
| 810 | |
| 811 | fn luaTagMoveTo(state: ?*c.lua_State) callconv(.c) c_int { |
| 812 | const s = state orelse return 0; |
| 813 | const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 814 | createActionTableWithInt(s, "MoveToTag", idx); |
| 815 | return 1; |
| 816 | } |
| 817 | |
| 818 | fn luaTagToggletag(state: ?*c.lua_State) callconv(.c) c_int { |
| 819 | const s = state orelse return 0; |
| 820 | const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 821 | createActionTableWithInt(s, "ToggleTag", idx); |
| 822 | return 1; |
| 823 | } |
| 824 | |
| 825 | fn luaTagSetBackAndForth(state: ?*c.lua_State) callconv(.c) c_int { |
| 826 | const cfg = config orelse return 0; |
| 827 | const s = state orelse return 0; |
| 828 | cfg.tag_back_and_forth = c.lua_toboolean(s, 1) != 0; |
| 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | fn luaMonitorFocus(state: ?*c.lua_State) callconv(.c) c_int { |
| 833 | const s = state orelse return 0; |
| 834 | const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 835 | createActionTableWithInt(s, "FocusMonitor", dir); |
| 836 | return 1; |
| 837 | } |
| 838 | |
| 839 | fn luaMonitorTag(state: ?*c.lua_State) callconv(.c) c_int { |
| 840 | const s = state orelse return 0; |
| 841 | const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 842 | createActionTableWithInt(s, "TagMonitor", dir); |
| 843 | return 1; |
| 844 | } |
| 845 | |
| 846 | fn luaMonitorWarpCursor(state: ?*c.lua_State) callconv(.c) c_int { |
| 847 | const cfg = config orelse return 0; |
| 848 | const s = state orelse return 0; |
| 849 | cfg.warp_cursor_to_monitor = c.lua_toboolean(s, 1) != 0; |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | fn luaMonitorWarpCursorOnSend(state: ?*c.lua_State) callconv(.c) c_int { |
| 854 | const cfg = config orelse return 0; |
| 855 | const s = state orelse return 0; |
| 856 | cfg.warp_cursor_on_send = c.lua_toboolean(s, 1) != 0; |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | fn luaRuleAdd(state: ?*c.lua_State) callconv(.c) c_int { |
| 861 | const cfg = config orelse return 0; |
| 862 | const s = state orelse return 0; |
| 863 | |
| 864 | if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0; |
| 865 | |
| 866 | var rule = Rule{ |
| 867 | .class = null, |
| 868 | .instance = null, |
| 869 | .title = null, |
| 870 | .tags = 0, |
| 871 | .is_floating = false, |
| 872 | .is_fullscreen = false, |
| 873 | .monitor = -1, |
| 874 | .focus = false, |
| 875 | }; |
| 876 | |
| 877 | _ = c.lua_getfield(s, 1, "class"); |
| 878 | if (c.lua_type(s, -1) == c.LUA_TSTRING) { |
| 879 | rule.class = getLuaString(s, -1); |
| 880 | } |
| 881 | c.lua_settop(s, -2); |
| 882 | |
| 883 | _ = c.lua_getfield(s, 1, "instance"); |
| 884 | if (c.lua_type(s, -1) == c.LUA_TSTRING) { |
| 885 | rule.instance = getLuaString(s, -1); |
| 886 | } |
| 887 | c.lua_settop(s, -2); |
| 888 | |
| 889 | _ = c.lua_getfield(s, 1, "title"); |
| 890 | if (c.lua_type(s, -1) == c.LUA_TSTRING) { |
| 891 | rule.title = getLuaString(s, -1); |
| 892 | } |
| 893 | c.lua_settop(s, -2); |
| 894 | |
| 895 | _ = c.lua_getfield(s, 1, "tag"); |
| 896 | if (c.lua_type(s, -1) == c.LUA_TNUMBER) { |
| 897 | const tag_idx: i32 = @intCast(c.lua_tointegerx(s, -1, null)); |
| 898 | if (tag_idx > 0) { |
| 899 | rule.tags = @as(u32, 1) << @intCast(tag_idx - 1); |
| 900 | } |
| 901 | } |
| 902 | c.lua_settop(s, -2); |
| 903 | |
| 904 | _ = c.lua_getfield(s, 1, "floating"); |
| 905 | if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) { |
| 906 | rule.is_floating = c.lua_toboolean(s, -1) != 0; |
| 907 | } |
| 908 | c.lua_settop(s, -2); |
| 909 | |
| 910 | _ = c.lua_getfield(s, 1, "fullscreen"); |
| 911 | if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) { |
| 912 | rule.is_fullscreen = c.lua_toboolean(s, -1) != 0; |
| 913 | } |
| 914 | c.lua_settop(s, -2); |
| 915 | |
| 916 | _ = c.lua_getfield(s, 1, "monitor"); |
| 917 | if (c.lua_type(s, -1) == c.LUA_TNUMBER) { |
| 918 | rule.monitor = @intCast(c.lua_tointegerx(s, -1, null)); |
| 919 | } |
| 920 | c.lua_settop(s, -2); |
| 921 | |
| 922 | _ = c.lua_getfield(s, 1, "focus"); |
| 923 | if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) { |
| 924 | rule.focus = c.lua_toboolean(s, -1) != 0; |
| 925 | } |
| 926 | c.lua_settop(s, -2); |
| 927 | |
| 928 | _ = c.lua_getfield(s, 1, "width"); |
| 929 | if (c.lua_type(s, -1) == c.LUA_TNUMBER) { |
| 930 | const width = c.lua_tonumberx(s, -1, null); |
| 931 | if (width > 0) rule.width = @floatCast(width); |
| 932 | } |
| 933 | c.lua_settop(s, -2); |
| 934 | |
| 935 | cfg.addRule(rule) catch return 0; |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | fn luaBarSetFont(state: ?*c.lua_State) callconv(.c) c_int { |
| 940 | const cfg = config orelse return 0; |
| 941 | const s = state orelse return 0; |
| 942 | if (dupeLuaString(s, 1)) |font| { |
| 943 | cfg.font = font; |
| 944 | } |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | fn luaBarSetPosition(state: ?*c.lua_State) callconv(.c) c_int { |
| 949 | const cfg = config orelse return 0; |
| 950 | const s = state orelse return 0; |
| 951 | if (dupeLuaString(s, 1)) |position| { |
| 952 | cfg.bar_position = position; |
| 953 | } |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | fn luaBarSetBlocks(state: ?*c.lua_State) callconv(.c) c_int { |
| 958 | const cfg = config orelse return 0; |
| 959 | const s = state orelse return 0; |
| 960 | |
| 961 | if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0; |
| 962 | |
| 963 | const len = c.lua_rawlen(s, 1); |
| 964 | var i: usize = 1; |
| 965 | while (i <= len) : (i += 1) { |
| 966 | _ = c.lua_rawgeti(s, 1, @intCast(i)); |
| 967 | |
| 968 | if (c.lua_type(s, -1) != c.LUA_TTABLE) { |
| 969 | c.lua_settop(s, -2); |
| 970 | continue; |
| 971 | } |
| 972 | |
| 973 | if (parseBlockConfig(s, -1)) |block| { |
| 974 | cfg.addBlock(block) catch {}; |
| 975 | } |
| 976 | |
| 977 | c.lua_settop(s, -2); |
| 978 | } |
| 979 | |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | fn parseBlockConfig(state: *c.lua_State, idx: c_int) ?Block { |
| 984 | _ = c.lua_getfield(state, idx, "__block_type"); |
| 985 | const block_type_str = getLuaString(state, -1) orelse { |
| 986 | c.lua_settop(state, -2); |
| 987 | return null; |
| 988 | }; |
| 989 | c.lua_settop(state, -2); |
| 990 | |
| 991 | _ = c.lua_getfield(state, idx, "format"); |
| 992 | const format = dupeLuaString(state, -1) orelse ""; |
| 993 | c.lua_settop(state, -2); |
| 994 | |
| 995 | _ = c.lua_getfield(state, idx, "interval"); |
| 996 | const interval: u32 = @intCast(c.lua_tointegerx(state, -1, null)); |
| 997 | c.lua_settop(state, -2); |
| 998 | |
| 999 | _ = c.lua_getfield(state, idx, "color"); |
| 1000 | const color = parseColor(state, -1); |
| 1001 | c.lua_settop(state, -2); |
| 1002 | |
| 1003 | _ = c.lua_getfield(state, idx, "underline"); |
| 1004 | const underline = c.lua_toboolean(state, -1) != 0; |
| 1005 | c.lua_settop(state, -2); |
| 1006 | |
| 1007 | _ = c.lua_getfield(state, idx, "click"); |
| 1008 | const click = parseClickAction(state, -1); |
| 1009 | c.lua_settop(state, -2); |
| 1010 | |
| 1011 | var block = Block{ |
| 1012 | .block_type = .static, |
| 1013 | .format = format, |
| 1014 | .interval = interval, |
| 1015 | .color = color, |
| 1016 | .underline = underline, |
| 1017 | .click = click, |
| 1018 | }; |
| 1019 | |
| 1020 | if (std.mem.eql(u8, block_type_str, "Ram")) { |
| 1021 | block.block_type = .ram; |
| 1022 | } else if (std.mem.eql(u8, block_type_str, "DateTime")) { |
| 1023 | block.block_type = .datetime; |
| 1024 | _ = c.lua_getfield(state, idx, "__arg"); |
| 1025 | block.datetime_format = dupeLuaString(state, -1); |
| 1026 | c.lua_settop(state, -2); |
| 1027 | } else if (std.mem.eql(u8, block_type_str, "Shell")) { |
| 1028 | block.block_type = .shell; |
| 1029 | _ = c.lua_getfield(state, idx, "__arg"); |
| 1030 | block.command = dupeLuaString(state, -1); |
| 1031 | c.lua_settop(state, -2); |
| 1032 | } else if (std.mem.eql(u8, block_type_str, "Static")) { |
| 1033 | block.block_type = .static; |
| 1034 | _ = c.lua_getfield(state, idx, "__arg"); |
| 1035 | if (dupeLuaString(state, -1)) |text| { |
| 1036 | block.format = text; |
| 1037 | } |
| 1038 | c.lua_settop(state, -2); |
| 1039 | } else if (std.mem.eql(u8, block_type_str, "Battery")) { |
| 1040 | block.block_type = .battery; |
| 1041 | _ = c.lua_getfield(state, idx, "__arg"); |
| 1042 | if (c.lua_type(state, -1) == c.LUA_TTABLE) { |
| 1043 | _ = c.lua_getfield(state, -1, "charging"); |
| 1044 | block.format_charging = dupeLuaString(state, -1); |
| 1045 | c.lua_settop(state, -2); |
| 1046 | |
| 1047 | _ = c.lua_getfield(state, -1, "discharging"); |
| 1048 | block.format_discharging = dupeLuaString(state, -1); |
| 1049 | c.lua_settop(state, -2); |
| 1050 | |
| 1051 | _ = c.lua_getfield(state, -1, "full"); |
| 1052 | block.format_full = dupeLuaString(state, -1); |
| 1053 | c.lua_settop(state, -2); |
| 1054 | |
| 1055 | _ = c.lua_getfield(state, -1, "battery_name"); |
| 1056 | block.battery_name = dupeLuaString(state, -1); |
| 1057 | c.lua_settop(state, -2); |
| 1058 | } |
| 1059 | c.lua_settop(state, -2); |
| 1060 | } else if (std.mem.eql(u8, block_type_str, "Systray")) { |
| 1061 | block.block_type = .systray; |
| 1062 | } else { |
| 1063 | return null; |
| 1064 | } |
| 1065 | |
| 1066 | return block; |
| 1067 | } |
| 1068 | |
| 1069 | fn luaBarSetSchemeNormal(state: ?*c.lua_State) callconv(.c) c_int { |
| 1070 | const cfg = config orelse return 0; |
| 1071 | const s = state orelse return 0; |
| 1072 | cfg.scheme_normal = parseScheme(s); |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | fn luaBarSetSchemeSelected(state: ?*c.lua_State) callconv(.c) c_int { |
| 1077 | const cfg = config orelse return 0; |
| 1078 | const s = state orelse return 0; |
| 1079 | cfg.scheme_selected = parseScheme(s); |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
| 1083 | fn luaBarSetSchemeOccupied(state: ?*c.lua_State) callconv(.c) c_int { |
| 1084 | const cfg = config orelse return 0; |
| 1085 | const s = state orelse return 0; |
| 1086 | cfg.scheme_occupied = parseScheme(s); |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | fn luaBarSetSchemeUrgent(state: ?*c.lua_State) callconv(.c) c_int { |
| 1091 | const cfg = config orelse return 0; |
| 1092 | const s = state orelse return 0; |
| 1093 | cfg.scheme_urgent = parseScheme(s); |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | fn luaBarSetHideVacantTags(state: ?*c.lua_State) callconv(.c) c_int { |
| 1098 | const cfg = config orelse return 0; |
| 1099 | const s = state orelse return 0; |
| 1100 | cfg.hide_vacant_tags = c.lua_toboolean(s, 1) != 0; |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | fn luaBarBlockRam(state: ?*c.lua_State) callconv(.c) c_int { |
| 1105 | const s = state orelse return 0; |
| 1106 | createBlockTable(s, "Ram", null); |
| 1107 | return 1; |
| 1108 | } |
| 1109 | |
| 1110 | fn luaBarBlockDatetime(state: ?*c.lua_State) callconv(.c) c_int { |
| 1111 | const s = state orelse return 0; |
| 1112 | _ = c.lua_getfield(s, 1, "date_format"); |
| 1113 | const date_format = getLuaString(s, -1); |
| 1114 | c.lua_settop(s, -2); |
| 1115 | createBlockTable(s, "DateTime", date_format); |
| 1116 | return 1; |
| 1117 | } |
| 1118 | |
| 1119 | fn luaBarBlockShell(state: ?*c.lua_State) callconv(.c) c_int { |
| 1120 | const s = state orelse return 0; |
| 1121 | _ = c.lua_getfield(s, 1, "command"); |
| 1122 | const command = getLuaString(s, -1); |
| 1123 | c.lua_settop(s, -2); |
| 1124 | createBlockTable(s, "Shell", command); |
| 1125 | return 1; |
| 1126 | } |
| 1127 | |
| 1128 | fn luaBarBlockStatic(state: ?*c.lua_State) callconv(.c) c_int { |
| 1129 | const s = state orelse return 0; |
| 1130 | _ = c.lua_getfield(s, 1, "text"); |
| 1131 | const text = getLuaString(s, -1); |
| 1132 | c.lua_settop(s, -2); |
| 1133 | createBlockTable(s, "Static", text); |
| 1134 | return 1; |
| 1135 | } |
| 1136 | |
| 1137 | fn luaBarBlockSystray(state: ?*c.lua_State) callconv(.c) c_int { |
| 1138 | const s = state orelse return 0; |
| 1139 | createBlockTable(s, "Systray", null); |
| 1140 | return 1; |
| 1141 | } |
| 1142 | |
| 1143 | fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int { |
| 1144 | const s = state orelse return 0; |
| 1145 | |
| 1146 | c.lua_createtable(s, 0, 7); |
| 1147 | |
| 1148 | _ = c.lua_pushstring(s, "Battery"); |
| 1149 | c.lua_setfield(s, -2, "__block_type"); |
| 1150 | |
| 1151 | _ = c.lua_getfield(s, 1, "format"); |
| 1152 | c.lua_setfield(s, -2, "format"); |
| 1153 | |
| 1154 | _ = c.lua_getfield(s, 1, "interval"); |
| 1155 | c.lua_setfield(s, -2, "interval"); |
| 1156 | |
| 1157 | _ = c.lua_getfield(s, 1, "color"); |
| 1158 | c.lua_setfield(s, -2, "color"); |
| 1159 | |
| 1160 | _ = c.lua_getfield(s, 1, "underline"); |
| 1161 | c.lua_setfield(s, -2, "underline"); |
| 1162 | |
| 1163 | _ = c.lua_getfield(s, 1, "click"); |
| 1164 | c.lua_setfield(s, -2, "click"); |
| 1165 | |
| 1166 | c.lua_createtable(s, 0, 4); |
| 1167 | _ = c.lua_getfield(s, 1, "charging"); |
| 1168 | c.lua_setfield(s, -2, "charging"); |
| 1169 | _ = c.lua_getfield(s, 1, "discharging"); |
| 1170 | c.lua_setfield(s, -2, "discharging"); |
| 1171 | _ = c.lua_getfield(s, 1, "full"); |
| 1172 | c.lua_setfield(s, -2, "full"); |
| 1173 | _ = c.lua_getfield(s, 1, "battery_name"); |
| 1174 | c.lua_setfield(s, -2, "battery_name"); |
| 1175 | c.lua_setfield(s, -2, "__arg"); |
| 1176 | |
| 1177 | return 1; |
| 1178 | } |
| 1179 | |
| 1180 | fn createBlockTable(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]const u8) void { |
| 1181 | c.lua_createtable(state, 0, 7); |
| 1182 | |
| 1183 | _ = c.lua_pushstring(state, block_type); |
| 1184 | c.lua_setfield(state, -2, "__block_type"); |
| 1185 | |
| 1186 | _ = c.lua_getfield(state, 1, "format"); |
| 1187 | c.lua_setfield(state, -2, "format"); |
| 1188 | |
| 1189 | _ = c.lua_getfield(state, 1, "interval"); |
| 1190 | c.lua_setfield(state, -2, "interval"); |
| 1191 | |
| 1192 | _ = c.lua_getfield(state, 1, "color"); |
| 1193 | c.lua_setfield(state, -2, "color"); |
| 1194 | |
| 1195 | _ = c.lua_getfield(state, 1, "underline"); |
| 1196 | c.lua_setfield(state, -2, "underline"); |
| 1197 | |
| 1198 | _ = c.lua_getfield(state, 1, "click"); |
| 1199 | c.lua_setfield(state, -2, "click"); |
| 1200 | |
| 1201 | if (arg) |a| { |
| 1202 | var buf: [256]u8 = undefined; |
| 1203 | if (a.len < buf.len) { |
| 1204 | @memcpy(buf[0..a.len], a); |
| 1205 | buf[a.len] = 0; |
| 1206 | _ = c.lua_pushstring(state, &buf); |
| 1207 | c.lua_setfield(state, -2, "__arg"); |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | fn luaSetFloatingPosition(state: ?*c.lua_State) callconv(.c) c_int { |
| 1213 | const cfg = config orelse return 0; |
| 1214 | const s = state orelse return 0; |
| 1215 | if (getLuaString(s, 1)) |name| { |
| 1216 | if (config_mod.FloatingPosition.fromString(name)) |pos| { |
| 1217 | cfg.floating_position = pos; |
| 1218 | } |
| 1219 | } |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
| 1223 | fn luaSetTerminal(state: ?*c.lua_State) callconv(.c) c_int { |
| 1224 | const cfg = config orelse return 0; |
| 1225 | const s = state orelse return 0; |
| 1226 | if (dupeLuaString(s, 1)) |term| { |
| 1227 | cfg.terminal = term; |
| 1228 | } |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | fn luaSetLayout(state: ?*c.lua_State) callconv(.c) c_int { |
| 1233 | const cfg = config orelse return 0; |
| 1234 | const s = state orelse return 0; |
| 1235 | const name = getStringArg(s, 1) orelse return 0; |
| 1236 | if (config_mod.Layouts.fromString(name) == null) { |
| 1237 | std.debug.print("set_layout: unknown layout '{s}'\n", .{name}); |
| 1238 | return 0; |
| 1239 | } |
| 1240 | if (dupeLuaString(s, 1)) |layout| { |
| 1241 | cfg.layout = layout; |
| 1242 | } |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | fn luaSetAttachMethod(state: ?*c.lua_State) callconv(.c) c_int { |
| 1247 | const cfg = config orelse return 0; |
| 1248 | const s = state orelse return 0; |
| 1249 | const name = getStringArg(s, 1) orelse return 0; |
| 1250 | cfg.attach_method = config_mod.AttachMethods.fromString(name) orelse { |
| 1251 | std.debug.print("set_attach_method: unknown attach method '{s}'\n", .{name}); |
| 1252 | return 0; |
| 1253 | }; |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | fn luaSetModkey(state: ?*c.lua_State) callconv(.c) c_int { |
| 1258 | const cfg = config orelse return 0; |
| 1259 | const s = state orelse return 0; |
| 1260 | if (getStringArg(s, 1)) |modkey_str| { |
| 1261 | cfg.modkey = parseSingleModifier(modkey_str); |
| 1262 | cfg.addButton(.{ |
| 1263 | .click = .client_win, |
| 1264 | .mod_mask = cfg.modkey, |
| 1265 | .button = 1, |
| 1266 | .action = .move_mouse, |
| 1267 | }) catch {}; |
| 1268 | cfg.addButton(.{ |
| 1269 | .click = .client_win, |
| 1270 | .mod_mask = cfg.modkey, |
| 1271 | .button = 3, |
| 1272 | .action = .resize_mouse, |
| 1273 | }) catch {}; |
| 1274 | } |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
| 1278 | fn luaSetTags(state: ?*c.lua_State) callconv(.c) c_int { |
| 1279 | const cfg = config orelse return 0; |
| 1280 | const s = state orelse return 0; |
| 1281 | |
| 1282 | if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0; |
| 1283 | |
| 1284 | const len = c.lua_rawlen(s, 1); |
| 1285 | var i: usize = 0; |
| 1286 | while (i < len and i < 12) : (i += 1) { |
| 1287 | _ = c.lua_rawgeti(s, 1, @intCast(i + 1)); |
| 1288 | if (dupeLuaString(s, -1)) |tag_str| { |
| 1289 | cfg.tags[i] = tag_str; |
| 1290 | } |
| 1291 | c.lua_settop(s, -2); |
| 1292 | } |
| 1293 | |
| 1294 | if (i > 0) cfg.tag_count = @intCast(i); |
| 1295 | |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
| 1299 | fn luaAutostart(state: ?*c.lua_State) callconv(.c) c_int { |
| 1300 | const cfg = config orelse return 0; |
| 1301 | const s = state orelse return 0; |
| 1302 | if (dupeLuaString(s, 1)) |cmd| { |
| 1303 | cfg.addAutostart(cmd) catch return 0; |
| 1304 | } |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
| 1308 | fn luaAutoTile(state: ?*c.lua_State) callconv(.c) c_int { |
| 1309 | const cfg = config orelse return 0; |
| 1310 | const s = state orelse return 0; |
| 1311 | cfg.auto_tile = c.lua_toboolean(s, 1) != 0; |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | fn luaTiledResizeMode(state: ?*c.lua_State) callconv(.c) c_int { |
| 1316 | const cfg = config orelse return 0; |
| 1317 | const s = state orelse return 0; |
| 1318 | cfg.tiled_resize_mode = c.lua_toboolean(s, 1) != 0; |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
| 1322 | fn luaSetLayoutSymbol(state: ?*c.lua_State) callconv(.c) c_int { |
| 1323 | const cfg = config orelse return 0; |
| 1324 | const s = state orelse return 0; |
| 1325 | const name = getStringArg(s, 1) orelse return 0; |
| 1326 | const symbol = dupeLuaString(s, 2) orelse return 0; |
| 1327 | |
| 1328 | const layout = config_mod.Layouts.fromString(name) orelse return 0; |
| 1329 | switch (layout) { |
| 1330 | .tiling => cfg.layout_tile_symbol = symbol, |
| 1331 | .monocle => cfg.layout_monocle_symbol = symbol, |
| 1332 | .floating => cfg.layout_floating_symbol = symbol, |
| 1333 | .scrolling => cfg.layout_scrolling_symbol = symbol, |
| 1334 | .grid => cfg.layout_grid_symbol = symbol, |
| 1335 | .dwindle => cfg.layout_dwindle_symbol = symbol, |
| 1336 | } |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
| 1340 | fn luaSetTagLayout(state: ?*c.lua_State) callconv(.c) c_int { |
| 1341 | const cfg = config orelse return 0; |
| 1342 | const s = state orelse return 0; |
| 1343 | const tag_index = c.lua_tointegerx(s, 1, null); |
| 1344 | if (tag_index < 1 or tag_index > 12) return 0; |
| 1345 | const name = getStringArg(s, 2) orelse return 0; |
| 1346 | if (config_mod.Layouts.fromString(name) == null) { |
| 1347 | std.debug.print("set_tag_layout: unknown layout '{s}'\n", .{name}); |
| 1348 | return 0; |
| 1349 | } |
| 1350 | if (dupeLuaString(s, 2)) |layout_str| { |
| 1351 | cfg.tag_layouts[@intCast(tag_index - 1)] = layout_str; |
| 1352 | } |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | fn luaQuit(state: ?*c.lua_State) callconv(.c) c_int { |
| 1357 | const s = state orelse return 0; |
| 1358 | createActionTable(s, "Quit"); |
| 1359 | return 1; |
| 1360 | } |
| 1361 | |
| 1362 | fn luaRestart(state: ?*c.lua_State) callconv(.c) c_int { |
| 1363 | const s = state orelse return 0; |
| 1364 | createActionTable(s, "Restart"); |
| 1365 | return 1; |
| 1366 | } |
| 1367 | |
| 1368 | fn luaToggleGaps(state: ?*c.lua_State) callconv(.c) c_int { |
| 1369 | const s = state orelse return 0; |
| 1370 | createActionTable(s, "ToggleGaps"); |
| 1371 | return 1; |
| 1372 | } |
| 1373 | |
| 1374 | fn luaToggleBar(state: ?*c.lua_State) callconv(.c) c_int { |
| 1375 | const s = state orelse return 0; |
| 1376 | createActionTable(s, "ToggleBar"); |
| 1377 | return 1; |
| 1378 | } |
| 1379 | |
| 1380 | fn luaShowKeybinds(state: ?*c.lua_State) callconv(.c) c_int { |
| 1381 | const s = state orelse return 0; |
| 1382 | createActionTable(s, "ShowKeybinds"); |
| 1383 | return 1; |
| 1384 | } |
| 1385 | |
| 1386 | fn luaSetMasterFactor(state: ?*c.lua_State) callconv(.c) c_int { |
| 1387 | const s = state orelse return 0; |
| 1388 | const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 1389 | createActionTableWithInt(s, "ResizeMaster", delta); |
| 1390 | return 1; |
| 1391 | } |
| 1392 | |
| 1393 | fn luaIncNumMaster(state: ?*c.lua_State) callconv(.c) c_int { |
| 1394 | const s = state orelse return 0; |
| 1395 | const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| 1396 | if (delta > 0) { |
| 1397 | createActionTable(s, "IncMaster"); |
| 1398 | } else { |
| 1399 | createActionTable(s, "DecMaster"); |
| 1400 | } |
| 1401 | return 1; |
| 1402 | } |
| 1403 | |
| 1404 | fn getStringArg(state: *c.lua_State, idx: c_int) ?[]const u8 { |
| 1405 | if (c.lua_type(state, idx) != c.LUA_TSTRING) return null; |
| 1406 | return getLuaString(state, idx); |
| 1407 | } |
| 1408 | |
| 1409 | fn extractSpawnCommand(state: *c.lua_State, idx: c_int) ?[]const u8 { |
| 1410 | const len = c.lua_rawlen(state, idx); |
| 1411 | if (len == 0) return null; |
| 1412 | |
| 1413 | if (len >= 3) { |
| 1414 | _ = c.lua_rawgeti(state, idx, 1); |
| 1415 | const first = getLuaString(state, -1); |
| 1416 | c.lua_settop(state, -2); |
| 1417 | |
| 1418 | _ = c.lua_rawgeti(state, idx, 2); |
| 1419 | const second = getLuaString(state, -1); |
| 1420 | c.lua_settop(state, -2); |
| 1421 | |
| 1422 | if (first != null and second != null and |
| 1423 | std.mem.eql(u8, first.?, "sh") and std.mem.eql(u8, second.?, "-c")) |
| 1424 | { |
| 1425 | _ = c.lua_rawgeti(state, idx, 3); |
| 1426 | const cmd = getLuaString(state, -1); |
| 1427 | c.lua_settop(state, -2); |
| 1428 | return cmd; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | _ = c.lua_rawgeti(state, idx, 1); |
| 1433 | const first_elem = getLuaString(state, -1); |
| 1434 | c.lua_settop(state, -2); |
| 1435 | return first_elem; |
| 1436 | } |
| 1437 | |
| 1438 | fn getLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 { |
| 1439 | const cstr = c.lua_tolstring(state, idx, null); |
| 1440 | if (cstr == null) return null; |
| 1441 | return std.mem.span(cstr); |
| 1442 | } |
| 1443 | |
| 1444 | fn dupeLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 { |
| 1445 | const cfg = config orelse return null; |
| 1446 | const lua_str = getLuaString(state, idx) orelse return null; |
| 1447 | const arena_allocator = cfg.string_arena.allocator(); |
| 1448 | const duped = arena_allocator.dupe(u8, lua_str) catch return null; |
| 1449 | return duped; |
| 1450 | } |
| 1451 | |
| 1452 | fn parseClickAction(state: *c.lua_State, idx: c_int) ?config_mod.ClickAction { |
| 1453 | const lua_type = c.lua_type(state, idx); |
| 1454 | if (lua_type == c.LUA_TSTRING) { |
| 1455 | const cmd = dupeLuaString(state, idx) orelse return null; |
| 1456 | return .{ .command = cmd }; |
| 1457 | } else if (lua_type == c.LUA_TTABLE) { |
| 1458 | _ = c.lua_getfield(state, idx, "command"); |
| 1459 | const cmd = dupeLuaString(state, -1); |
| 1460 | c.lua_settop(state, -2); |
| 1461 | if (cmd == null) return null; |
| 1462 | |
| 1463 | _ = c.lua_getfield(state, idx, "floating"); |
| 1464 | const floating = c.lua_toboolean(state, -1) != 0; |
| 1465 | c.lua_settop(state, -2); |
| 1466 | |
| 1467 | _ = c.lua_getfield(state, idx, "bypass_rules"); |
| 1468 | const bypass_rules = c.lua_toboolean(state, -1) != 0; |
| 1469 | c.lua_settop(state, -2); |
| 1470 | |
| 1471 | return .{ .command = cmd.?, .floating = floating, .bypass_rules = bypass_rules }; |
| 1472 | } |
| 1473 | return null; |
| 1474 | } |
| 1475 | |
| 1476 | fn parseColor(state: *c.lua_State, idx: c_int) u32 { |
| 1477 | const lua_type = c.lua_type(state, idx); |
| 1478 | if (lua_type == c.LUA_TNUMBER) { |
| 1479 | return @intCast(c.lua_tointegerx(state, idx, null)); |
| 1480 | } |
| 1481 | if (lua_type == c.LUA_TSTRING) { |
| 1482 | const str = getLuaString(state, idx) orelse return 0; |
| 1483 | if (str.len > 0 and str[0] == '#') { |
| 1484 | return std.fmt.parseInt(u32, str[1..], 16) catch return 0; |
| 1485 | } |
| 1486 | if (str.len > 2 and str[0] == '0' and str[1] == 'x') { |
| 1487 | return std.fmt.parseInt(u32, str[2..], 16) catch return 0; |
| 1488 | } |
| 1489 | return std.fmt.parseInt(u32, str, 16) catch return 0; |
| 1490 | } |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | fn parseScheme(state: *c.lua_State) ColorScheme { |
| 1495 | return ColorScheme{ |
| 1496 | .foreground = parseColor(state, 1), |
| 1497 | .background = parseColor(state, 2), |
| 1498 | .border = parseColor(state, 3), |
| 1499 | }; |
| 1500 | } |
| 1501 | |
| 1502 | fn parseModifiers(state: *c.lua_State, idx: c_int) u32 { |
| 1503 | var mod_mask: u32 = 0; |
| 1504 | |
| 1505 | if (c.lua_type(state, idx) != c.LUA_TTABLE) return mod_mask; |
| 1506 | |
| 1507 | const len = c.lua_rawlen(state, idx); |
| 1508 | var i: usize = 1; |
| 1509 | while (i <= len) : (i += 1) { |
| 1510 | _ = c.lua_rawgeti(state, idx, @intCast(i)); |
| 1511 | if (getLuaString(state, -1)) |mod_str| { |
| 1512 | const parsed = parseSingleModifier(mod_str); |
| 1513 | mod_mask |= parsed; |
| 1514 | } |
| 1515 | c.lua_settop(state, -2); |
| 1516 | } |
| 1517 | |
| 1518 | return mod_mask; |
| 1519 | } |
| 1520 | |
| 1521 | fn parseModifiersAtTop(state: *c.lua_State) u32 { |
| 1522 | return parseModifiers(state, -1); |
| 1523 | } |
| 1524 | |
| 1525 | fn parseSingleModifier(name: []const u8) u32 { |
| 1526 | if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) { |
| 1527 | return (1 << 6); |
| 1528 | } else if (std.mem.eql(u8, name, "Mod1") or std.mem.eql(u8, name, "mod1") or std.mem.eql(u8, name, "alt")) { |
| 1529 | return (1 << 3); |
| 1530 | } else if (std.mem.eql(u8, name, "Shift") or std.mem.eql(u8, name, "shift")) { |
| 1531 | return (1 << 0); |
| 1532 | } else if (std.mem.eql(u8, name, "Control") or std.mem.eql(u8, name, "control") or std.mem.eql(u8, name, "ctrl")) { |
| 1533 | return (1 << 2); |
| 1534 | } |
| 1535 | return 0; |
| 1536 | } |
| 1537 | |
| 1538 | fn parseAction(name: []const u8) ?Action { |
| 1539 | const action_map = .{ |
| 1540 | .{ "Spawn", Action.spawn }, |
| 1541 | .{ "SpawnTerminal", Action.spawn_terminal }, |
| 1542 | .{ "KillClient", Action.kill_client }, |
| 1543 | .{ "Quit", Action.quit }, |
| 1544 | .{ "Restart", Action.restart }, |
| 1545 | .{ "ShowKeybinds", Action.show_keybinds }, |
| 1546 | .{ "FocusStack", Action.focus_next }, |
| 1547 | .{ "MoveStack", Action.move_next }, |
| 1548 | .{ "ResizeMaster", Action.resize_master }, |
| 1549 | .{ "IncMaster", Action.inc_master }, |
| 1550 | .{ "DecMaster", Action.dec_master }, |
| 1551 | .{ "ToggleFloating", Action.toggle_floating }, |
| 1552 | .{ "ToggleFullScreen", Action.toggle_fullscreen }, |
| 1553 | .{ "ToggleGaps", Action.toggle_gaps }, |
| 1554 | .{ "ToggleBar", Action.toggle_bar }, |
| 1555 | .{ "CycleLayout", Action.cycle_layout }, |
| 1556 | .{ "ChangeLayout", Action.set_layout }, |
| 1557 | .{ "ViewTag", Action.view_tag }, |
| 1558 | .{ "ViewNextTag", Action.view_next_tag }, |
| 1559 | .{ "ViewPreviousTag", Action.view_prev_tag }, |
| 1560 | .{ "ViewNextNonEmptyTag", Action.view_next_nonempty_tag }, |
| 1561 | .{ "ViewPreviousNonEmptyTag", Action.view_prev_nonempty_tag }, |
| 1562 | .{ "MoveToTag", Action.move_to_tag }, |
| 1563 | .{ "ToggleView", Action.toggle_view_tag }, |
| 1564 | .{ "ToggleTag", Action.toggle_tag }, |
| 1565 | .{ "FocusMonitor", Action.focus_monitor }, |
| 1566 | .{ "TagMonitor", Action.send_to_monitor }, |
| 1567 | .{ "ScrollLeft", Action.scroll_left }, |
| 1568 | .{ "ScrollRight", Action.scroll_right }, |
| 1569 | .{ "CycleWidth", Action.cycle_width }, |
| 1570 | .{ "SetWidth", Action.set_width }, |
| 1571 | }; |
| 1572 | |
| 1573 | inline for (action_map) |entry| { |
| 1574 | if (std.mem.eql(u8, name, entry[0])) { |
| 1575 | return entry[1]; |
| 1576 | } |
| 1577 | } |
| 1578 | return null; |
| 1579 | } |
| 1580 | |
| 1581 | fn keynameToKeysym(name: []const u8) ?u64 { |
| 1582 | const key_map = .{ |
| 1583 | .{ "Return", 0xff0d }, |
| 1584 | .{ "Enter", 0xff0d }, |
| 1585 | .{ "Tab", 0xff09 }, |
| 1586 | .{ "Escape", 0xff1b }, |
| 1587 | .{ "BackSpace", 0xff08 }, |
| 1588 | .{ "Delete", 0xffff }, |
| 1589 | .{ "space", 0x0020 }, |
| 1590 | .{ "Space", 0x0020 }, |
| 1591 | .{ "comma", 0x002c }, |
| 1592 | .{ "Comma", 0x002c }, |
| 1593 | .{ "period", 0x002e }, |
| 1594 | .{ "Period", 0x002e }, |
| 1595 | .{ "slash", 0x002f }, |
| 1596 | .{ "Slash", 0x002f }, |
| 1597 | .{ "minus", 0x002d }, |
| 1598 | .{ "Minus", 0x002d }, |
| 1599 | .{ "equal", 0x003d }, |
| 1600 | .{ "Equal", 0x003d }, |
| 1601 | .{ "bracketleft", 0x005b }, |
| 1602 | .{ "bracketright", 0x005d }, |
| 1603 | .{ "backslash", 0x005c }, |
| 1604 | .{ "colon", 0x003a }, |
| 1605 | .{ "semicolon", 0x003b }, |
| 1606 | .{ "apostrophe", 0x0027 }, |
| 1607 | .{ "quotedbl", 0x0022 }, |
| 1608 | .{ "ampersand", 0x0026 }, |
| 1609 | .{ "parenleft", 0x0028 }, |
| 1610 | .{ "parenright", 0x0029 }, |
| 1611 | .{ "underscore", 0x005f }, |
| 1612 | .{ "grave", 0x0060 }, |
| 1613 | .{ "agrave", 0x00e0 }, |
| 1614 | .{ "egrave", 0x00e8 }, |
| 1615 | .{ "ccedilla", 0x00e7 }, |
| 1616 | .{ "eacute", 0x00e9 }, |
| 1617 | .{ "Left", 0xff51 }, |
| 1618 | .{ "Up", 0xff52 }, |
| 1619 | .{ "Right", 0xff53 }, |
| 1620 | .{ "Down", 0xff54 }, |
| 1621 | .{ "F1", 0xffbe }, |
| 1622 | .{ "F2", 0xffbf }, |
| 1623 | .{ "F3", 0xffc0 }, |
| 1624 | .{ "F4", 0xffc1 }, |
| 1625 | .{ "F5", 0xffc2 }, |
| 1626 | .{ "F6", 0xffc3 }, |
| 1627 | .{ "F7", 0xffc4 }, |
| 1628 | .{ "F8", 0xffc5 }, |
| 1629 | .{ "F9", 0xffc6 }, |
| 1630 | .{ "F10", 0xffc7 }, |
| 1631 | .{ "F11", 0xffc8 }, |
| 1632 | .{ "F12", 0xffc9 }, |
| 1633 | .{ "Print", 0xff61 }, |
| 1634 | .{ "XF86AudioRaiseVolume", 0x1008ff13 }, |
| 1635 | .{ "XF86AudioLowerVolume", 0x1008ff11 }, |
| 1636 | .{ "XF86AudioMute", 0x1008ff12 }, |
| 1637 | .{ "XF86AudioPlay", 0x1008ff14 }, |
| 1638 | .{ "XF86AudioPause", 0x1008ff31 }, |
| 1639 | .{ "XF86AudioNext", 0x1008ff17 }, |
| 1640 | .{ "XF86AudioPrev", 0x1008ff16 }, |
| 1641 | .{ "XF86MonBrightnessUp", 0x1008ff02 }, |
| 1642 | .{ "XF86MonBrightnessDown", 0x1008ff03 }, |
| 1643 | }; |
| 1644 | |
| 1645 | inline for (key_map) |entry| { |
| 1646 | if (std.mem.eql(u8, name, entry[0])) { |
| 1647 | return entry[1]; |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | if (name.len == 1) { |
| 1652 | const char = name[0]; |
| 1653 | if (char >= 'a' and char <= 'z') { |
| 1654 | return char; |
| 1655 | } |
| 1656 | if (char >= 'A' and char <= 'Z') { |
| 1657 | return char + 32; |
| 1658 | } |
| 1659 | if (char >= '0' and char <= '9') { |
| 1660 | return char; |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | return null; |
| 1665 | } |