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