From d1cf1ebd80381192a9486d43315354ade323a319 Mon Sep 17 00:00:00 2001 From: AnErrupTion Date: Sat, 1 Jul 2023 01:48:48 +0200 Subject: [PATCH] Rewrite INPUTS enum in Zig --- res/config.ini | 2 +- src/config.h | 6 ----- src/config.zig | 10 +++++++-- src/main.zig | 60 ++++++++++++++++++++++++++++---------------------- 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/res/config.ini b/res/config.ini index bc02462..2b2bcab 100644 --- a/res/config.ini +++ b/res/config.ini @@ -66,7 +66,7 @@ max_password_len = 255 # Input box active by default on startup -default_input = 2 +default_input = login # Load the saved desktop and username load = true diff --git a/src/config.h b/src/config.h index d932ac2..b5196e6 100644 --- a/src/config.h +++ b/src/config.h @@ -4,12 +4,6 @@ #include #include -enum INPUTS { - SESSION_SWITCH, - LOGIN_INPUT, - PASSWORD_INPUT, -}; - struct lang { char* capslock; diff --git a/src/config.zig b/src/config.zig index ef3940e..5ea6f0b 100644 --- a/src/config.zig +++ b/src/config.zig @@ -6,6 +6,12 @@ const interop = @import("interop.zig"); const INI_CONFIG_PATH: []const u8 = "/etc/ly/"; const INI_CONFIG_MAX_SIZE: usize = 16 * 1024; +pub const Inputs = enum { + session, + login, + password, +}; + pub const LyConfig = struct { ly: struct { animate: bool, @@ -17,7 +23,7 @@ pub const LyConfig = struct { blank_password: bool, clock: []const u8, console_dev: []const u8, - default_input: u8, + default_input: Inputs, fg: u8, hide_borders: bool, hide_f1_commands: bool, @@ -207,7 +213,7 @@ pub fn config_load(cfg_path: []const u8) !void { main.c_config.blank_password = ly_config.ly.blank_password; main.c_config.clock = config_clock.ptr; main.c_config.console_dev = config_console_dev.ptr; - main.c_config.default_input = ly_config.ly.default_input; + main.c_config.default_input = @intFromEnum(ly_config.ly.default_input); main.c_config.fg = ly_config.ly.fg; main.c_config.hide_borders = ly_config.ly.hide_borders; main.c_config.hide_f1_commands = ly_config.ly.hide_f1_commands; diff --git a/src/main.zig b/src/main.zig index 8074fed..1ab8d7b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -119,9 +119,9 @@ pub fn main() !void { // Place the cursor on the login field if there is no saved username // If there is, place the curser on the password field - var active_input: u8 = 0; - if (config.ly_config.ly.default_input == c.LOGIN_INPUT and username.text != username.end) { - active_input = c.PASSWORD_INPUT; + var active_input: config.Inputs = undefined; + if (config.ly_config.ly.default_input == .login and username.text != username.end) { + active_input = .password; } else { active_input = config.ly_config.ly.default_input; } @@ -135,16 +135,15 @@ pub fn main() !void { c.position_input(buffer, desktop, username, password); switch (active_input) { - c.SESSION_SWITCH => { + .session => { c.handle_desktop(desktop, event); }, - c.LOGIN_INPUT => { + .login => { c.handle_text(username, event); }, - c.PASSWORD_INPUT => { + .password => { c.handle_text(password, event); }, - else => unreachable, } if (config.ly_config.ly.animate) { @@ -171,16 +170,15 @@ pub fn main() !void { if (update) { if (auth_fails < MAX_AUTH_FAILS) { switch (active_input) { - c.SESSION_SWITCH => { + .session => { c.handle_desktop(desktop, event); }, - c.LOGIN_INPUT => { + .login => { c.handle_text(username, event); }, - c.PASSWORD_INPUT => { + .password => { c.handle_text(password, event); }, - else => unreachable, } c.tb_clear(); @@ -218,9 +216,9 @@ pub fn main() !void { _ = std.os.linux.gettimeofday(time, undefined); if (config.ly_config.ly.bigclock) { - timeout = @intCast(c_int, (60 - @mod(time.tv_sec, 60)) * 1000 - @divTrunc(time.tv_usec, 1000) + 1); + timeout = @intCast((60 - @mod(time.tv_sec, 60)) * 1000 - @divTrunc(time.tv_usec, 1000) + 1); } else if (config.ly_config.ly.clock.len > 0) { - timeout = @intCast(c_int, 1000 - @divTrunc(time.tv_usec, 1000) + 1); + timeout = @intCast(1000 - @divTrunc(time.tv_usec, 1000) + 1); } } @@ -248,12 +246,12 @@ pub fn main() !void { run = false; }, c.TB_KEY_CTRL_U => { - if (active_input > c.SESSION_SWITCH) { + if (active_input != .session) { switch (active_input) { - c.LOGIN_INPUT => { + .login => { c.input_text_clear(username); }, - c.PASSWORD_INPUT => { + .password => { c.input_text_clear(password); }, else => unreachable, @@ -263,23 +261,33 @@ pub fn main() !void { } }, c.TB_KEY_CTRL_K, c.TB_KEY_ARROW_UP => { - if (active_input > c.SESSION_SWITCH) { - active_input -= 1; + if (active_input != .session) { + active_input = switch (active_input) { + .login => .session, + .password => .login, + else => unreachable, + }; + update = true; } }, c.TB_KEY_CTRL_J, c.TB_KEY_ARROW_DOWN => { - if (active_input < c.PASSWORD_INPUT) { - active_input += 1; + if (active_input != .password) { + active_input = switch (active_input) { + .session => .login, + .login => .password, + else => unreachable, + }; + update = true; } }, c.TB_KEY_TAB => { - active_input += 1; - - if (active_input > c.PASSWORD_INPUT) { - active_input = c.SESSION_SWITCH; - } + active_input = switch (active_input) { + .session => .login, + .login => .password, + .password => .session, + }; update = true; }, @@ -291,7 +299,7 @@ pub fn main() !void { auth_fails += 1; // Move focus back to password input - active_input = c.PASSWORD_INPUT; + active_input = .password; if (c.dgn_output_code() != c.DGN_PAM) { buffer.info_line = c.dgn_output_log();