diff --git a/res/config.ini b/res/config.ini index 06f434b..64dbb3d 100644 --- a/res/config.ini +++ b/res/config.ini @@ -105,12 +105,6 @@ load = true # Save the current desktop and login as defaults save = true -# Deprecated - Will be removed in a future version -# New save files are now loaded from the same directory as the config -# Currently used to migrate old save files to the new version -# File in which to save and load the default desktop and login -save_file = $CONFIG_DIRECTORY/ly/save - # Remove power management command hints hide_key_hints = false diff --git a/src/config/Config.zig b/src/config/Config.zig index 0726d39..beb8e41 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -35,7 +35,6 @@ path: ?[:0]const u8 = "/sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/ restart_cmd: []const u8 = "/sbin/shutdown -r now", restart_key: []const u8 = "F2", save: bool = true, -save_file: []const u8 = build_options.config_directory ++ "/ly/save", service_name: [:0]const u8 = "ly", shutdown_cmd: []const u8 = "/sbin/shutdown -a now", shutdown_key: []const u8 = "F1", diff --git a/src/config/migrator.zig b/src/config/migrator.zig index 183dde9..93c470f 100644 --- a/src/config/migrator.zig +++ b/src/config/migrator.zig @@ -5,7 +5,10 @@ const ini = @import("zigini"); const Save = @import("Save.zig"); const enums = @import("../enums.zig"); -var maybe_animate: ?bool = null; +var temporary_allocator = std.heap.page_allocator; + +pub var maybe_animate: ?bool = null; +pub var maybe_save_file: ?[]const u8 = null; pub var mapped_config_fields = false; @@ -59,6 +62,14 @@ pub fn configFieldHandler(_: std.mem.Allocator, field: ini.IniField) ?ini.IniFie return mapped_field; } + if (std.mem.eql(u8, field.key, "save_file")) { + // The option doesn't exist anymore, but we save its value for migration later on + maybe_save_file = temporary_allocator.dupe(u8, field.value) catch return null; + + mapped_config_fields = true; + return null; + } + if (std.mem.eql(u8, field.key, "wayland_specifier") or std.mem.eql(u8, field.key, "max_desktop_len") or std.mem.eql(u8, field.key, "max_login_len") or @@ -78,34 +89,38 @@ pub fn configFieldHandler(_: std.mem.Allocator, field: ini.IniField) ?ini.IniFie // This is the stuff we only handle after reading the config. // For example, the "animate" field could come after "animation" pub fn lateConfigFieldHandler(animation: *enums.Animation) void { - if (maybe_animate == null) return; - - if (!maybe_animate.?) animation.* = .none; + if (maybe_animate) |animate| { + if (!animate) animation.* = .none; + } } -pub fn tryMigrateSaveFile(user_buf: *[32]u8, path: []const u8) Save { +pub fn tryMigrateSaveFile(user_buf: *[32]u8) Save { var save = Save{}; - var file = std.fs.openFileAbsolute(path, .{}) catch return save; - defer file.close(); + if (maybe_save_file) |path| { + defer temporary_allocator.free(path); - const reader = file.reader(); + var file = std.fs.openFileAbsolute(path, .{}) catch return save; + defer file.close(); - var user_fbs = std.io.fixedBufferStream(user_buf); - reader.streamUntilDelimiter(user_fbs.writer(), '\n', 32) catch return save; - const user = user_fbs.getWritten(); - if (user.len > 0) save.user = user; + const reader = file.reader(); - var session_buf: [20]u8 = undefined; - var session_fbs = std.io.fixedBufferStream(&session_buf); - reader.streamUntilDelimiter(session_fbs.writer(), '\n', 20) catch {}; + var user_fbs = std.io.fixedBufferStream(user_buf); + reader.streamUntilDelimiter(user_fbs.writer(), '\n', 32) catch return save; + const user = user_fbs.getWritten(); + if (user.len > 0) save.user = user; - const session_index_str = session_fbs.getWritten(); - var session_index: ?usize = null; - if (session_index_str.len > 0) { - session_index = std.fmt.parseUnsigned(usize, session_index_str, 10) catch return save; + var session_buf: [20]u8 = undefined; + var session_fbs = std.io.fixedBufferStream(&session_buf); + reader.streamUntilDelimiter(session_fbs.writer(), '\n', 20) catch {}; + + const session_index_str = session_fbs.getWritten(); + var session_index: ?usize = null; + if (session_index_str.len > 0) { + session_index = std.fmt.parseUnsigned(usize, session_index_str, 10) catch return save; + } + save.session_index = session_index; } - save.session_index = session_index; return save; } diff --git a/src/main.zig b/src/main.zig index f22b1b3..ec4a2f2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -142,20 +142,10 @@ pub fn main() !void { save_path_alloc = true; var user_buf: [32]u8 = undefined; - save = save_ini.readFileToStruct(save_path, comment_characters, null) catch migrator.tryMigrateSaveFile(&user_buf, config.save_file); + save = save_ini.readFileToStruct(save_path, comment_characters, null) catch migrator.tryMigrateSaveFile(&user_buf); } migrator.lateConfigFieldHandler(&config.animation); - - // if (migrator.mapped_config_fields) save_migrated_config: { - // var file = try std.fs.cwd().createFile(config_path, .{}); - // defer file.close(); - - // const writer = file.writer(); - // ini.writeFromStruct(config, writer, null, true, .{}) catch { - // break :save_migrated_config; - // }; - // } } else { const config_path = build_options.config_directory ++ "/ly/config.ini"; @@ -171,22 +161,22 @@ pub fn main() !void { if (config.load) { var user_buf: [32]u8 = undefined; - save = save_ini.readFileToStruct(save_path, comment_characters, null) catch migrator.tryMigrateSaveFile(&user_buf, config.save_file); + save = save_ini.readFileToStruct(save_path, comment_characters, null) catch migrator.tryMigrateSaveFile(&user_buf); } migrator.lateConfigFieldHandler(&config.animation); - - // if (migrator.mapped_config_fields) save_migrated_config: { - // var file = try std.fs.cwd().createFile(config_path, .{}); - // defer file.close(); - - // const writer = file.writer(); - // ini.writeFromStruct(config, writer, null, true, .{}) catch { - // break :save_migrated_config; - // }; - // } } + // if (migrator.mapped_config_fields) save_migrated_config: { + // var file = try std.fs.cwd().createFile(config_path, .{}); + // defer file.close(); + + // const writer = file.writer(); + // ini.writeFromStruct(config, writer, null, true, .{}) catch { + // break :save_migrated_config; + // }; + // } + // 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); @@ -669,7 +659,7 @@ pub fn main() !void { ini.writeFromStruct(save_data, file.writer(), null, true, .{}) catch break :save_last_settings; // Delete previous save file if it exists - std.fs.cwd().deleteFile(config.save_file) catch {}; + if (migrator.maybe_save_file) |path| std.fs.cwd().deleteFile(path) catch {}; } var shared_err = try SharedError.init();