From 11735290b848a30c4b47e26fd2e95f23da623d5e Mon Sep 17 00:00:00 2001 From: AnErrupTion Date: Thu, 5 Feb 2026 19:50:48 +0100 Subject: [PATCH] Fix active TTY detection for KMSCON Signed-off-by: AnErrupTion --- ly-core/src/interop.zig | 21 +++++++++++++++++---- src/main.zig | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/ly-core/src/interop.zig b/ly-core/src/interop.zig index 1ba8613..ef542f7 100644 --- a/ly-core/src/interop.zig +++ b/ly-core/src/interop.zig @@ -96,8 +96,21 @@ fn PlatformStruct() type { // 4. Finally, compare the major and minor device numbers with the // extracted values. If they correspond, parse [dir] to get the // TTY ID - pub fn getActiveTtyImpl(allocator: std.mem.Allocator) !u8 { + pub fn getActiveTtyImpl(allocator: std.mem.Allocator, use_kmscon_vt: bool) !u8 { var file_buffer: [256]u8 = undefined; + + if (use_kmscon_vt) { + var file = try std.fs.openFileAbsolute("/sys/class/tty/tty0/active", .{}); + defer file.close(); + + var reader = file.reader(&file_buffer); + var buffer: [16]u8 = undefined; + const read = try readBuffer(&reader.interface, &buffer); + + const tty = buffer[0..(read - 1)]; + return std.fmt.parseInt(u8, tty["tty".len..], 10); + } + var tty_major: u16 = undefined; var tty_minor: u16 = undefined; @@ -242,7 +255,7 @@ fn PlatformStruct() type { if (result != 0) return error.SetUserUidFailed; } - pub fn getActiveTtyImpl(_: std.mem.Allocator) !u8 { + pub fn getActiveTtyImpl(_: std.mem.Allocator, _: bool) !u8 { return error.FeatureUnimplemented; } @@ -285,8 +298,8 @@ pub fn getTimeOfDay() !TimeOfDay { }; } -pub fn getActiveTty(allocator: std.mem.Allocator) !u8 { - return platform_struct.getActiveTtyImpl(allocator); +pub fn getActiveTty(allocator: std.mem.Allocator, use_kmscon_vt: bool) !u8 { + return platform_struct.getActiveTtyImpl(allocator, use_kmscon_vt); } pub fn switchTty(tty: u8) !void { diff --git a/src/main.zig b/src/main.zig index f2e2fcd..cc305c4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -568,14 +568,14 @@ pub fn main() !void { var inactivity_cmd_ran = false; // Switch to selected TTY - const active_tty = interop.getActiveTty(allocator) catch |err| no_tty_found: { + const active_tty = interop.getActiveTty(allocator, use_kmscon_vt) catch |err| no_tty_found: { try info_line.addMessage(lang.err_get_active_tty, config.error_bg, config.error_fg); try log_file.err("sys", "failed to get active tty: {s}", .{@errorName(err)}); break :no_tty_found build_options.fallback_tty; }; interop.switchTty(active_tty) catch |err| { try info_line.addMessage(lang.err_switch_tty, config.error_bg, config.error_fg); - try log_file.err("sys", "failed to switch tty: {s}", .{@errorName(err)}); + try log_file.err("sys", "failed to switch to tty {d}: {s}", .{ active_tty, @errorName(err) }); }; if (config.initial_info_text) |text| {