diff --git a/.gitignore b/.gitignore index 60f36fa..de08f4f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea/ zig-cache/ zig-out/ -valgrind.log \ No newline at end of file +valgrind.log +.zig-cache diff --git a/build.zig b/build.zig index 65c06a2..17b7943 100644 --- a/build.zig +++ b/build.zig @@ -1,18 +1,34 @@ const std = @import("std"); +const builtin = @import("builtin"); -const ly_version = std.SemanticVersion{ .major = 1, .minor = 0, .patch = 0 }; +const min_zig_string = "0.12.0"; +const current_zig = builtin.zig_version; + +// Implementing zig version detection through compile time +comptime { + const min_zig = std.SemanticVersion.parse(min_zig_string) catch unreachable; + if (current_zig.order(min_zig) == .lt) { + @compileError(std.fmt.comptimePrint("Your Zig version v{} does not meet the minimum build requirement of v{}", .{ current_zig, min_zig })); + } +} + +const ly_version = std.SemanticVersion{ .major = 1, .minor = 0, .patch = 3 }; var dest_directory: []const u8 = undefined; var data_directory: []const u8 = undefined; var exe_name: []const u8 = undefined; +const ProgressNode = if (current_zig.minor == 12) *std.Progress.Node else std.Progress.Node; + pub fn build(b: *std.Build) !void { - dest_directory = b.option([]const u8, "dest_directory", "Specify a dest directory for installation") orelse ""; - data_directory = b.option([]const u8, "data_directory", "Specify a default data directory (default is /etc/ly)") orelse "/etc/ly"; - data_directory = try std.fs.path.join(b.allocator, &[_][]const u8{ dest_directory, data_directory }); + dest_directory = b.option([]const u8, "dest_directory", "Specify a destination directory for installation") orelse ""; + data_directory = b.option([]const u8, "data_directory", "Specify a default data directory (default is /etc/ly). This path gets embedded into the binary") orelse "/etc/ly"; exe_name = b.option([]const u8, "name", "Specify installed executable file name (default is ly)") orelse "ly"; + const bin_directory = try b.allocator.dupe(u8, data_directory); + data_directory = try std.fs.path.join(b.allocator, &[_][]const u8{ dest_directory, data_directory }); + const build_options = b.addOptions(); - build_options.addOption([]const u8, "data_directory", data_directory); + build_options.addOption([]const u8, "data_directory", bin_directory); const version_str = try getVersionStr(b, "ly", ly_version); @@ -23,7 +39,7 @@ pub fn build(b: *std.Build) !void { const exe = b.addExecutable(.{ .name = "ly", - .root_source_file = .{ .path = "src/main.zig" }, + .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); @@ -36,14 +52,14 @@ pub fn build(b: *std.Build) !void { const clap = b.dependency("clap", .{ .target = target, .optimize = optimize }); exe.root_module.addImport("clap", clap.module("clap")); - exe.addIncludePath(.{ .path = "include" }); + exe.addIncludePath(b.path("include")); exe.linkSystemLibrary("pam"); exe.linkSystemLibrary("xcb"); exe.linkLibC(); // HACK: Only fails with ReleaseSafe, so we'll override it. const translate_c = b.addTranslateC(.{ - .root_source_file = .{ .path = "include/termbox2.h" }, + .root_source_file = b.path("include/termbox2.h"), .target = target, .optimize = if (optimize == .ReleaseSafe) .ReleaseFast else optimize, }); @@ -92,7 +108,7 @@ pub fn build(b: *std.Build) !void { pub fn ExeInstaller(install_conf: bool) type { return struct { - pub fn make(step: *std.Build.Step, progress: *std.Progress.Node) !void { + pub fn make(step: *std.Build.Step, progress: ProgressNode) !void { _ = progress; try install_ly(step.owner.allocator, install_conf); } @@ -106,35 +122,38 @@ const InitSystem = enum { }; pub fn ServiceInstaller(comptime init_system: InitSystem) type { return struct { - pub fn make(step: *std.Build.Step, progress: *std.Progress.Node) !void { + pub fn make(step: *std.Build.Step, progress: ProgressNode) !void { _ = progress; const allocator = step.owner.allocator; switch (init_system) { .Openrc => { const service_path = try std.fs.path.join(allocator, &[_][]const u8{ dest_directory, "/etc/init.d" }); std.fs.cwd().makePath(service_path) catch {}; - var service_dir = std.fs.openDirAbsolute(service_path, .{}) catch unreachable; + var service_dir = std.fs.cwd().openDir(service_path, .{}) catch unreachable; defer service_dir.close(); - try std.fs.cwd().copyFile("res/ly-openrc", service_dir, exe_name, .{ .override_mode = 755 }); + try std.fs.cwd().copyFile("res/ly-openrc", service_dir, exe_name, .{ .override_mode = 0o755 }); }, .Runit => { const service_path = try std.fs.path.join(allocator, &[_][]const u8{ dest_directory, "/etc/sv/ly" }); std.fs.cwd().makePath(service_path) catch {}; - var service_dir = std.fs.openDirAbsolute(service_path, .{}) catch unreachable; + var service_dir = std.fs.cwd().openDir(service_path, .{}) catch unreachable; defer service_dir.close(); + const supervise_path = try std.fs.path.join(allocator, &[_][]const u8{ service_path, "supervise" }); + try std.fs.cwd().copyFile("res/ly-runit-service/conf", service_dir, "conf", .{}); - try std.fs.cwd().copyFile("res/ly-runit-service/finish", service_dir, "finish", .{}); - try std.fs.cwd().copyFile("res/ly-runit-service/run", service_dir, "run", .{}); + try std.fs.cwd().copyFile("res/ly-runit-service/finish", service_dir, "finish", .{ .override_mode = 0o755 }); + try std.fs.cwd().copyFile("res/ly-runit-service/run", service_dir, "run", .{ .override_mode = 0o755 }); + try std.fs.cwd().symLink("/run/runit/supervise.ly", supervise_path, .{}); }, .Systemd => { const service_path = try std.fs.path.join(allocator, &[_][]const u8{ dest_directory, "/usr/lib/systemd/system" }); std.fs.cwd().makePath(service_path) catch {}; - var service_dir = std.fs.openDirAbsolute(service_path, .{}) catch unreachable; + var service_dir = std.fs.cwd().openDir(service_path, .{}) catch unreachable; defer service_dir.close(); - try std.fs.cwd().copyFile("res/ly.service", service_dir, "ly.service", .{ .override_mode = 644 }); + try std.fs.cwd().copyFile("res/ly.service", service_dir, "ly.service", .{ .override_mode = 0o644 }); }, } } @@ -155,20 +174,20 @@ fn install_ly(allocator: std.mem.Allocator, install_config: bool) !void { { const exe_path = try std.fs.path.join(allocator, &[_][]const u8{ dest_directory, "/usr/bin" }); - if (!std.mem.eql(u8, dest_directory, "")) { - std.fs.cwd().makePath(exe_path) catch { + std.fs.cwd().makePath(exe_path) catch { + if (!std.mem.eql(u8, dest_directory, "")) { std.debug.print("warn: {s} already exists as a directory.\n", .{exe_path}); - }; - } + } + }; - var executable_dir = std.fs.openDirAbsolute(exe_path, .{}) catch unreachable; + var executable_dir = std.fs.cwd().openDir(exe_path, .{}) catch unreachable; defer executable_dir.close(); try current_dir.copyFile("zig-out/bin/ly", executable_dir, exe_name, .{}); } { - var config_dir = std.fs.openDirAbsolute(data_directory, .{}) catch unreachable; + var config_dir = std.fs.cwd().openDir(data_directory, .{}) catch unreachable; defer config_dir.close(); if (install_config) { @@ -179,7 +198,7 @@ fn install_ly(allocator: std.mem.Allocator, install_config: bool) !void { } { - var lang_dir = std.fs.openDirAbsolute(lang_path, .{}) catch unreachable; + var lang_dir = std.fs.cwd().openDir(lang_path, .{}) catch unreachable; defer lang_dir.close(); try current_dir.copyFile("res/lang/cat.ini", lang_dir, "cat.ini", .{}); @@ -202,42 +221,43 @@ fn install_ly(allocator: std.mem.Allocator, install_config: bool) !void { { const pam_path = try std.fs.path.join(allocator, &[_][]const u8{ dest_directory, "/etc/pam.d" }); - if (!std.mem.eql(u8, dest_directory, "")) { - std.fs.cwd().makePath(pam_path) catch { - std.debug.print("warn: {s} already exists as a directory.\n", .{pam_path}); - }; - } - var pam_dir = std.fs.openDirAbsolute(pam_path, .{}) catch unreachable; + std.fs.cwd().makePath(pam_path) catch { + if (!std.mem.eql(u8, dest_directory, "")) { + std.debug.print("warn: {s} already exists as a directory.\n", .{pam_path}); + } + }; + + var pam_dir = std.fs.cwd().openDir(pam_path, .{}) catch unreachable; defer pam_dir.close(); - try current_dir.copyFile("res/pam.d/ly", pam_dir, "ly", .{ .override_mode = 644 }); + try current_dir.copyFile("res/pam.d/ly", pam_dir, "ly", .{ .override_mode = 0o644 }); } } -pub fn uninstallall(step: *std.Build.Step, progress: *std.Progress.Node) !void { +pub fn uninstallall(step: *std.Build.Step, progress: ProgressNode) !void { _ = progress; - try std.fs.deleteTreeAbsolute(data_directory); + try std.fs.cwd().deleteTree(data_directory); const allocator = step.owner.allocator; const exe_path = try std.fs.path.join(allocator, &[_][]const u8{ dest_directory, "/usr/bin/", exe_name }); - try std.fs.deleteFileAbsolute(exe_path); + try std.fs.cwd().deleteFile(exe_path); const pam_path = try std.fs.path.join(allocator, &[_][]const u8{ dest_directory, "/etc/pam.d/ly" }); - try std.fs.deleteFileAbsolute(pam_path); + try std.fs.cwd().deleteFile(pam_path); const systemd_service_path = try std.fs.path.join(allocator, &[_][]const u8{ dest_directory, "/usr/lib/systemd/system/ly.service" }); - std.fs.deleteFileAbsolute(systemd_service_path) catch { + std.fs.cwd().deleteFile(systemd_service_path) catch { std.debug.print("warn: systemd service not found.\n", .{}); }; const openrc_service_path = try std.fs.path.join(allocator, &[_][]const u8{ dest_directory, "/etc/init.d/ly" }); - std.fs.deleteFileAbsolute(openrc_service_path) catch { + std.fs.cwd().deleteFile(openrc_service_path) catch { std.debug.print("warn: openrc service not found.\n", .{}); }; const runit_service_path = try std.fs.path.join(allocator, &[_][]const u8{ dest_directory, "/etc/sv/ly" }); - std.fs.deleteTreeAbsolute(runit_service_path) catch { + std.fs.cwd().deleteTree(runit_service_path) catch { std.debug.print("warn: runit service not found.\n", .{}); }; } diff --git a/build.zig.zon b/build.zig.zon index 85900c9..c4c9cbd 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -4,12 +4,12 @@ .minimum_zig_version = "0.12.0", .dependencies = .{ .clap = .{ - .url = "https://github.com/Hejsil/zig-clap/archive/8c98e6404b22aafc0184e999d8f068b81cc22fa1.tar.gz", - .hash = "122014e73fd712190e109950837b97f6143f02d7e2b6986e1db70b6f4aadb5ba6a0d", + .url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.9.1.tar.gz", + .hash = "122062d301a203d003547b414237229b09a7980095061697349f8bef41be9c30266b", }, .zigini = .{ - .url = "https://github.com/Kawaii-Ash/zigini/archive/ce1f322482099db058f5d9fdd05fbfa255d79723.tar.gz", - .hash = "1220e7a99793a0430e0a7c0b938cb3c98321035bc297e21cd0e2413cf740b4923b9f", + .url = "https://github.com/Kawaii-Ash/zigini/archive/0bba97a12582928e097f4074cc746c43351ba4c8.tar.gz", + .hash = "12209b971367b4066d40ecad4728e6fdffc4cc4f19356d424c2de57f5b69ac7a619a", }, }, .paths = .{""}, diff --git a/readme.md b/readme.md index 1402122..676d612 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,7 @@ Ly is a lightweight TUI (ncurses-like) display manager for Linux and BSD. ## Dependencies - Compile-time: - - zig 0.12.0 + - zig 0.12.0 or 0.13.0 - a C standard library - pam - xcb @@ -132,7 +132,7 @@ then you have to disable getty, so it doesn't respawn on top of ly # ln -s /etc/sv/ly /var/service/ ``` -By default, ly will run on tty2. To change the tty it must be set in `/etc/ly/config.ini` +By default, ly will run on tty2. To change the tty it must be set in `/etc/ly/config.ini` You should as well disable your existing display manager service if needed, e.g.: @@ -196,7 +196,7 @@ Take a look at your .xsession if X doesn't start, as it can interfere ## PSX DOOM fire animation To enable the famous PSX DOOM fire described by [Fabien Sanglard](http://fabiensanglard.net/doom_fire_psx/index.html), -just uncomment `animate = true` in `/etc/ly/config.ini`. You may also +just set `animation = doom` in `/etc/ly/config.ini`. You may also disable the main box borders with `hide_borders = true`. ## Additional Information diff --git a/res/config.ini b/res/config.ini index 68a39bb..e8fd099 100644 --- a/res/config.ini +++ b/res/config.ini @@ -30,12 +30,12 @@ vi_mode = false #define TB_CYAN 0x07 #define TB_WHITE 0x08 # -# Setting both to zero makes `bg` black and `fg` white. To set the actual color palette you are encouraged to use another tool -# such as [mkinitcpio-colors](https://github.com/evanpurkhiser/mkinitcpio-colors). Note that the color palette defined with -# `mkinitcpio-colors` takes 16 colors (0-15), only values 0-8 are valid for `ly` config and these values do not correspond -# exactly. For instance, in defining palettes with `mkinitcpio-colors` the order is black, dark red, dark green, brown, dark -# blue, dark purple, dark cyan, light gray, dark gray, bright red, bright green, yellow, bright blue, bright purple, bright -# cyan, and white, indexed in that order 0 through 15. For example, the color defined for white (indexed at 15 in the mkinitcpio +# Setting both to zero makes `bg` black and `fg` white. To set the actual color palette you are encouraged to use another tool +# such as [mkinitcpio-colors](https://github.com/evanpurkhiser/mkinitcpio-colors). Note that the color palette defined with +# `mkinitcpio-colors` takes 16 colors (0-15), only values 0-8 are valid for `ly` config and these values do not correspond +# exactly. For instance, in defining palettes with `mkinitcpio-colors` the order is black, dark red, dark green, brown, dark +# blue, dark purple, dark cyan, light gray, dark gray, bright red, bright green, yellow, bright blue, bright purple, bright +# cyan, and white, indexed in that order 0 through 15. For example, the color defined for white (indexed at 15 in the mkinitcpio # config) will be used by `ly` for `fg = 8`. # Background color id @@ -120,7 +120,7 @@ tty = 2 console_dev = /dev/console # Default path. If null, ly doesn't set a path. -path = /sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin +path = /sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin # Event timeout in milliseconds min_refresh_delta = 5 diff --git a/res/ly.service b/res/ly.service index 09c1559..2fd120a 100644 --- a/res/ly.service +++ b/res/ly.service @@ -7,7 +7,6 @@ Conflicts=getty@tty2.service [Service] Type=idle ExecStart=/usr/bin/ly -StandardError=journal StandardInput=tty TTYPath=/dev/tty2 TTYReset=yes diff --git a/res/wsetup.sh b/res/wsetup.sh index 68041a3..fd3a583 100755 --- a/res/wsetup.sh +++ b/res/wsetup.sh @@ -40,6 +40,7 @@ case $SHELL in ;; */fish) [ -f /etc/profile ] && . /etc/profile + [ -f $HOME/.profile ] && . $HOME/.profile xsess_tmp=`mktemp /tmp/xsess-env-XXXXXX` $SHELL --login -c "/bin/sh -c 'export -p' > $xsess_tmp" . $xsess_tmp diff --git a/res/xsetup.sh b/res/xsetup.sh index 487a078..e73d357 100755 --- a/res/xsetup.sh +++ b/res/xsetup.sh @@ -1,4 +1,4 @@ -#! /bin/sh +#!/bin/sh # Xsession - run as user # Copyright (C) 2016 Pier Luigi Fiorini @@ -40,6 +40,7 @@ case $SHELL in ;; */fish) [ -f /etc/profile ] && . /etc/profile + [ -f $HOME/.profile ] && . $HOME/.profile xsess_tmp=`mktemp /tmp/xsess-env-XXXXXX` $SHELL --login -c "/bin/sh -c 'export -p' > $xsess_tmp" . $xsess_tmp @@ -82,6 +83,10 @@ if [ -d "$xsessionddir" ]; then done fi +if [ -f "$USERXSESSION" ]; then + . "$USERXSESSION" +fi + if [ -d /etc/X11/Xresources ]; then for i in /etc/X11/Xresources/*; do [ -f $i ] && xrdb -merge $i @@ -92,10 +97,6 @@ fi [ -f $HOME/.Xresources ] && xrdb -merge $HOME/.Xresources [ -f $XDG_CONFIG_HOME/X11/Xresources ] && xrdb -merge $XDG_CONFIG_HOME/X11/Xresources -if [ -f "$USERXSESSION" ]; then - . "$USERXSESSION" -fi - if [ -z "$*" ]; then exec xmessage -center -buttons OK:0 -default OK "Sorry, $DESKTOP_SESSION is no valid session." else diff --git a/src/auth.zig b/src/auth.zig index dbbdfda..05ea61f 100644 --- a/src/auth.zig +++ b/src/auth.zig @@ -21,9 +21,12 @@ pub fn sessionSignalHandler(i: c_int) callconv(.C) void { } pub fn authenticate(config: Config, current_environment: Desktop.Environment, login: [:0]const u8, password: [:0]const u8) !void { - var tty_buffer: [2]u8 = undefined; + var tty_buffer: [3]u8 = undefined; const tty_str = try std.fmt.bufPrintZ(&tty_buffer, "{d}", .{config.tty}); + var pam_tty_buffer: [6]u8 = undefined; + const pam_tty_str = try std.fmt.bufPrintZ(&pam_tty_buffer, "tty{d}", .{config.tty}); + // Set the XDG environment variables setXdgSessionEnv(current_environment.display_server); try setXdgEnv(tty_str, current_environment.xdg_session_desktop, current_environment.xdg_desktop_names orelse ""); @@ -41,6 +44,10 @@ pub fn authenticate(config: Config, current_environment: Desktop.Environment, lo if (status != interop.pam.PAM_SUCCESS) return pamDiagnose(status); defer _ = interop.pam.pam_end(handle, status); + // Set PAM_TTY as the current TTY. This is required in case it isn't being set by another PAM module + status = interop.pam.pam_set_item(handle, interop.pam.PAM_TTY, pam_tty_str.ptr); + if (status != interop.pam.PAM_SUCCESS) return pamDiagnose(status); + // Do the PAM routine status = interop.pam.pam_authenticate(handle, 0); if (status != interop.pam.PAM_SUCCESS) return pamDiagnose(status); @@ -176,11 +183,11 @@ fn setXdgEnv(tty_str: [:0]u8, desktop_name: [:0]const u8, xdg_desktop_names: [:0 var uid_buffer: [10 + @sizeOf(u32) + 1]u8 = undefined; const uid_str = try std.fmt.bufPrintZ(&uid_buffer, "/run/user/{d}", .{uid}); - _ = interop.setenv("XDG_CURRENT_DESKTOP", xdg_desktop_names.ptr, 0); + if (!std.mem.eql(u8, xdg_desktop_names, "")) _ = interop.setenv("XDG_CURRENT_DESKTOP", xdg_desktop_names.ptr, 0); _ = interop.setenv("XDG_RUNTIME_DIR", uid_str.ptr, 0); _ = interop.setenv("XDG_SESSION_CLASS", "user", 0); _ = interop.setenv("XDG_SESSION_ID", "1", 0); - _ = interop.setenv("XDG_SESSION_DESKTOP", desktop_name.ptr, 0); + if (!std.mem.eql(u8, desktop_name, "")) _ = interop.setenv("XDG_SESSION_DESKTOP", desktop_name.ptr, 0); _ = interop.setenv("XDG_SEAT", "seat0", 0); _ = interop.setenv("XDG_VTNR", tty_str.ptr, 0); } @@ -327,7 +334,7 @@ fn createXauthFile(pwd: [:0]const u8) ![:0]const u8 { return xauthority; } -pub fn mcookie(cmd: [:0]const u8) ![32]u8 { +pub fn mcookie(shell: [*:0]const u8, cmd: [:0]const u8) ![32]u8 { const pipe = try std.posix.pipe(); defer std.posix.close(pipe[1]); @@ -341,8 +348,8 @@ pub fn mcookie(cmd: [:0]const u8) ![32]u8 { std.posix.dup2(pipe[1], std.posix.STDOUT_FILENO) catch std.process.exit(1); std.posix.close(pipe[1]); - const args = [_:null]?[*:0]u8{}; - std.posix.execveZ(cmd.ptr, &args, std.c.environ) catch {}; + const args = [_:null]?[*:0]const u8{ shell, "-c", cmd }; + std.posix.execveZ(shell, &args, std.c.environ) catch {}; std.process.exit(1); } @@ -364,7 +371,7 @@ fn xauth(display_name: [:0]u8, shell: [*:0]const u8, pw_dir: [*:0]const u8, xaut _ = interop.setenv("XAUTHORITY", xauthority, 1); _ = interop.setenv("DISPLAY", display_name, 1); - const mcookie_output = try mcookie(mcookie_cmd); + const mcookie_output = try mcookie(shell, mcookie_cmd); const pid = try std.posix.fork(); if (pid == 0) { diff --git a/src/config/Config.zig b/src/config/Config.zig index bddda19..ad119c4 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -30,7 +30,7 @@ max_password_len: u8 = 255, mcookie_cmd: [:0]const u8 = "/usr/bin/mcookie", min_refresh_delta: u16 = 5, numlock: bool = false, -path: ?[:0]const u8 = "/sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin", +path: ?[:0]const u8 = "/sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin", restart_cmd: []const u8 = "/sbin/shutdown -r now", restart_key: []const u8 = "F2", save: bool = true, diff --git a/src/config/migrator.zig b/src/config/migrator.zig index 0ae6001..377ee1a 100644 --- a/src/config/migrator.zig +++ b/src/config/migrator.zig @@ -1,7 +1,19 @@ +// The migrator ensures compatibility with <=0.6.0 configuration files + const std = @import("std"); const ini = @import("zigini"); const Save = @import("Save.zig"); +pub fn configFieldHandler(_: std.mem.Allocator, field: ini.IniField) ?ini.IniField { + var mapped_field = field; + + if (std.mem.eql(u8, field.key, "blank_password")) { + mapped_field.key = "clear_password"; + } + + return mapped_field; +} + pub fn tryMigrateSaveFile(user_buf: *[32]u8, path: []const u8) Save { var save = Save{}; diff --git a/src/main.zig b/src/main.zig index 6171361..022ff54 100644 --- a/src/main.zig +++ b/src/main.zig @@ -21,6 +21,7 @@ const utils = @import("tui/utils.zig"); const Ini = ini.Ini; const termbox = interop.termbox; +const temporary_allocator = std.heap.page_allocator; var session_pid: std.posix.pid_t = -1; pub fn signalHandler(i: c_int) callconv(.C) void { @@ -38,11 +39,32 @@ pub fn signalHandler(i: c_int) callconv(.C) void { } pub fn main() !void { + var shutdown = false; + var restart = false; + var shutdown_cmd: []const u8 = undefined; + var restart_cmd: []const u8 = undefined; + + const stderr = std.io.getStdErr().writer(); + + defer { + // If we can't shutdown or restart due to an error, we print it to standard error. If that fails, just bail out + if (shutdown) { + const shutdown_error = std.process.execv(temporary_allocator, &[_][]const u8{ "/bin/sh", "-c", shutdown_cmd }); + stderr.print("error: couldn't shutdown: {any}\n", .{shutdown_error}) catch std.process.exit(1); + } else if (restart) { + const restart_error = std.process.execv(temporary_allocator, &[_][]const u8{ "/bin/sh", "-c", restart_cmd }); + stderr.print("error: couldn't restart: {any}\n", .{restart_error}) catch std.process.exit(1); + } else { + // The user has quit Ly using Ctrl+C + temporary_allocator.free(shutdown_cmd); + temporary_allocator.free(restart_cmd); + } + } + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const allocator = gpa.allocator(); - const stderr = std.io.getStdErr().writer(); // Load arguments const params = comptime clap.parseParamsComptime( @@ -90,8 +112,7 @@ pub fn main() !void { if (save_path_alloc) allocator.free(save_path); } - // Compatibility with v0.6.0 - const mapped_config_fields = .{.{ "blank_password", "clear_password" }}; + const comment_characters = "#"; if (res.args.config) |s| { const trailing_slash = if (s[s.len - 1] != '/') "/" else ""; @@ -99,34 +120,41 @@ pub fn main() !void { const config_path = try std.fmt.allocPrint(allocator, "{s}{s}config.ini", .{ s, trailing_slash }); defer allocator.free(config_path); - config = config_ini.readFileToStructWithMap(config_path, mapped_config_fields) catch Config{}; + config = config_ini.readFileToStruct(config_path, comment_characters, migrator.configFieldHandler) catch Config{}; const lang_path = try std.fmt.allocPrint(allocator, "{s}{s}lang/{s}.ini", .{ s, trailing_slash, config.lang }); defer allocator.free(lang_path); - lang = lang_ini.readFileToStruct(lang_path) catch Lang{}; + lang = lang_ini.readFileToStruct(lang_path, comment_characters, null) catch Lang{}; if (config.load) { save_path = try std.fmt.allocPrint(allocator, "{s}{s}save.ini", .{ s, trailing_slash }); save_path_alloc = true; var user_buf: [32]u8 = undefined; - save = save_ini.readFileToStruct(save_path) catch migrator.tryMigrateSaveFile(&user_buf, config.save_file); + save = save_ini.readFileToStruct(save_path, comment_characters, null) catch migrator.tryMigrateSaveFile(&user_buf, config.save_file); } } else { - config = config_ini.readFileToStructWithMap(build_options.data_directory ++ "/config.ini", mapped_config_fields) catch Config{}; + const config_path = build_options.data_directory ++ "/config.ini"; + + config = config_ini.readFileToStruct(config_path, comment_characters, migrator.configFieldHandler) catch Config{}; const lang_path = try std.fmt.allocPrint(allocator, "{s}/lang/{s}.ini", .{ build_options.data_directory, config.lang }); defer allocator.free(lang_path); - lang = lang_ini.readFileToStruct(lang_path) catch Lang{}; + lang = lang_ini.readFileToStruct(lang_path, comment_characters, null) catch Lang{}; if (config.load) { var user_buf: [32]u8 = undefined; - save = save_ini.readFileToStruct(save_path) catch migrator.tryMigrateSaveFile(&user_buf, config.save_file); + save = save_ini.readFileToStruct(save_path, comment_characters, null) catch migrator.tryMigrateSaveFile(&user_buf, config.save_file); } } + // These strings only end up getting freed if the user quits Ly using Ctrl+C, which is fine since in the other cases + // we end up shutting down or restarting the system + shutdown_cmd = try temporary_allocator.dupe(u8, config.shutdown_cmd); + restart_cmd = try temporary_allocator.dupe(u8, config.restart_cmd); + interop.setNumlock(config.numlock) catch {}; if (config.initial_info_text) |text| { @@ -161,7 +189,14 @@ pub fn main() !void { // Initialize terminal buffer const labels_max_length = @max(lang.login.len, lang.password.len); - var buffer = TerminalBuffer.init(config, labels_max_length); + // Get a random seed for the PRNG (used by animations) + var seed: u64 = undefined; + try std.posix.getrandom(std.mem.asBytes(&seed)); + + var prng = std.Random.DefaultPrng.init(seed); + const random = prng.random(); + + var buffer = TerminalBuffer.init(config, labels_max_length, random); // Initialize components var desktop = try Desktop.init(allocator, &buffer, config.max_desktop_len, lang); @@ -250,8 +285,6 @@ pub fn main() !void { var run = true; var update = true; var resolution_changed = false; - var shutdown = false; - var restart = false; var auth_fails: u64 = 0; // Switch to selected TTY if possible @@ -426,8 +459,6 @@ pub fn main() !void { desktop.draw(); login.draw(); password.drawMasked(config.asterisk); - - update = animate; } else { std.time.sleep(std.time.ns_per_ms * 10); update = buffer.cascade(); @@ -460,6 +491,8 @@ pub fn main() !void { const event_error = if (timeout == -1) termbox.tb_poll_event(&event) else termbox.tb_peek_event(&event, timeout); + update = timeout != -1; + if (event_error < 0 or event.type != termbox.TB_EVENT_KEY) continue; switch (event.key) { @@ -479,7 +512,7 @@ pub fn main() !void { run = false; } else if (pressed_key == sleep_key) { if (config.sleep_cmd) |sleep_cmd| { - var sleep = std.ChildProcess.init(&[_][]const u8{ "/bin/sh", "-c", sleep_cmd }, allocator); + var sleep = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", sleep_cmd }, allocator); _ = sleep.spawnAndWait() catch .{}; } } @@ -534,7 +567,7 @@ pub fn main() !void { .user = login.text.items, .session_index = desktop.current, }; - ini.writeFromStruct(save_data, file.writer(), null) catch break :save_last_settings; + ini.writeFromStruct(save_data, file.writer(), null, true, .{}) catch break :save_last_settings; } var shared_err = try SharedError.init(); @@ -584,7 +617,7 @@ pub fn main() !void { update = true; - var restore_cursor = std.ChildProcess.init(&[_][]const u8{ "/bin/sh", "-c", config.term_restore_cursor_cmd }, allocator); + var restore_cursor = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", config.term_restore_cursor_cmd }, allocator); _ = restore_cursor.spawnAndWait() catch .{}; }, else => { @@ -628,12 +661,6 @@ pub fn main() !void { }, } } - - if (shutdown) { - return std.process.execv(allocator, &[_][]const u8{ "/bin/sh", "-c", config.shutdown_cmd }); - } else if (restart) { - return std.process.execv(allocator, &[_][]const u8{ "/bin/sh", "-c", config.restart_cmd }); - } } fn getAuthErrorMsg(err: anyerror, lang: Lang) []const u8 { diff --git a/src/tui/TerminalBuffer.zig b/src/tui/TerminalBuffer.zig index eec1a85..9692365 100644 --- a/src/tui/TerminalBuffer.zig +++ b/src/tui/TerminalBuffer.zig @@ -4,7 +4,7 @@ const interop = @import("../interop.zig"); const utils = @import("utils.zig"); const Config = @import("../config/Config.zig"); -const Random = std.rand.Random; +const Random = std.Random; const termbox = interop.termbox; @@ -35,11 +35,9 @@ box_height: u64, margin_box_v: u8, margin_box_h: u8, -pub fn init(config: Config, labels_max_length: u64) TerminalBuffer { - var prng = std.rand.Isaac64.init(@intCast(std.time.timestamp())); - +pub fn init(config: Config, labels_max_length: u64, random: Random) TerminalBuffer { return .{ - .random = prng.random(), + .random = random, .width = @intCast(termbox.tb_width()), .height = @intCast(termbox.tb_height()), .buffer = termbox.tb_cell_buffer(), diff --git a/src/tui/components/Desktop.zig b/src/tui/components/Desktop.zig index 98870d0..5bbaf60 100644 --- a/src/tui/components/Desktop.zig +++ b/src/tui/components/Desktop.zig @@ -150,7 +150,7 @@ pub fn crawl(self: *Desktop, path: []const u8, display_server: DisplayServer) !v const entry_path = try std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ path, item.name }); defer self.allocator.free(entry_path); var entry_ini = Ini(Entry).init(self.allocator); - _ = try entry_ini.readFileToStruct(entry_path); + _ = try entry_ini.readFileToStruct(entry_path, "#", null); errdefer entry_ini.deinit(); var xdg_session_desktop: []const u8 = undefined;