mirror of
https://github.com/fairyglade/ly.git
synced 2026-08-06 21:21:58 +00:00
Remove shutdown_cmd & restart_cmd + sleep & hibernate
Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
@@ -64,8 +64,6 @@ gameoflife_fg: u32 = 0x0000FF00,
|
||||
gameoflife_entropy_interval: usize = 10,
|
||||
gameoflife_frame_delay: usize = 6,
|
||||
gameoflife_initial_density: f32 = 0.4,
|
||||
hibernate_cmd: ?[]const u8 = null,
|
||||
hibernate_key: []const u8 = "F4",
|
||||
hide_borders: bool = false,
|
||||
inactivity_cmd: ?[]const u8 = null,
|
||||
inactivity_delay: u16 = 0,
|
||||
@@ -81,7 +79,6 @@ margin_box_h: u8 = 2,
|
||||
margin_box_v: u8 = 1,
|
||||
numlock: bool = false,
|
||||
path: ?[]const u8 = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
||||
restart_cmd: []const u8 = "/sbin/shutdown -r now",
|
||||
restart_key: []const u8 = "F2",
|
||||
save: bool = true,
|
||||
service_name: [:0]const u8 = "ly",
|
||||
@@ -89,10 +86,7 @@ session_log: ?[]const u8 = "ly-session.log",
|
||||
setup_cmd: []const u8 = build_options.config_directory ++ "/ly/setup.sh",
|
||||
shell: bool = true,
|
||||
show_password_key: []const u8 = "F7",
|
||||
shutdown_cmd: []const u8 = "/sbin/shutdown -a now",
|
||||
shutdown_key: []const u8 = "F1",
|
||||
sleep_cmd: ?[]const u8 = null,
|
||||
sleep_key: []const u8 = "F3",
|
||||
start_cmd: ?[]const u8 = null,
|
||||
text_in_center: bool = false,
|
||||
type_username: bool = false,
|
||||
|
||||
@@ -14,6 +14,7 @@ const IniParser = ly_core.IniParser;
|
||||
const ini = ly_core.ini;
|
||||
|
||||
const Config = @import("Config.zig");
|
||||
const Lang = @import("Lang.zig");
|
||||
const OldSave = @import("OldSave.zig");
|
||||
const SavedUsers = @import("SavedUsers.zig");
|
||||
const custom = @import("custom.zig");
|
||||
@@ -45,6 +46,8 @@ const removed_properties = [_][]const u8{
|
||||
"wayland_cmd",
|
||||
"console_dev",
|
||||
"load",
|
||||
"shutdown_cmd",
|
||||
"restart_cmd",
|
||||
// Migrating these isn't worth the effort so just say we removed them
|
||||
"hide_key_hints",
|
||||
"hide_keyboard_locks",
|
||||
@@ -56,6 +59,10 @@ pub var auto_eight_colors: bool = true;
|
||||
|
||||
pub var maybe_animate: ?bool = null;
|
||||
pub var maybe_save_file: ?[]const u8 = null;
|
||||
pub var maybe_sleep_key: ?[]const u8 = null;
|
||||
pub var maybe_sleep_cmd: ?[]const u8 = null;
|
||||
pub var maybe_hibernate_key: ?[]const u8 = null;
|
||||
pub var maybe_hibernate_cmd: ?[]const u8 = null;
|
||||
|
||||
pub fn configFieldHandler(_: std.mem.Allocator, field: ini.IniField) ?ini.IniField {
|
||||
if (std.mem.eql(u8, field.key, "animate")) {
|
||||
@@ -129,7 +136,6 @@ pub fn configFieldHandler(_: std.mem.Allocator, field: ini.IniField) ?ini.IniFie
|
||||
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;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -168,6 +174,26 @@ pub fn configFieldHandler(_: std.mem.Allocator, field: ini.IniField) ?ini.IniFie
|
||||
return mapped_field;
|
||||
}
|
||||
|
||||
if (std.mem.eql(u8, field.key, "sleep_key")) {
|
||||
maybe_sleep_key = temporary_allocator.dupe(u8, field.value) catch return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (std.mem.eql(u8, field.key, "sleep_cmd")) {
|
||||
maybe_sleep_cmd = temporary_allocator.dupe(u8, field.value) catch return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (std.mem.eql(u8, field.key, "hibernate_key")) {
|
||||
maybe_hibernate_key = temporary_allocator.dupe(u8, field.value) catch return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (std.mem.eql(u8, field.key, "hibernate_cmd")) {
|
||||
maybe_hibernate_cmd = temporary_allocator.dupe(u8, field.value) catch return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO: Dearest Melpert,
|
||||
// I pray this message finds you well, as daylight dwindles and the witching hour
|
||||
// approaches, I find it more and more imperative as time continues that I place
|
||||
@@ -228,7 +254,7 @@ 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(config: *Config) void {
|
||||
pub fn lateConfigFieldHandler(config: *Config, lang: Lang) void {
|
||||
if (maybe_animate) |animate| {
|
||||
if (!animate) config.*.animation = .none;
|
||||
}
|
||||
@@ -257,6 +283,24 @@ pub fn lateConfigFieldHandler(config: *Config) void {
|
||||
if (!set_color_properties[7]) config.error_fg = Styling.BOLD | Color.ECOL_RED;
|
||||
if (!set_color_properties[8]) config.fg = Color.ECOL_WHITE;
|
||||
}
|
||||
|
||||
if (maybe_sleep_key) |key| {
|
||||
if (maybe_sleep_cmd) |cmd| {
|
||||
custom.binds.put(temporary_allocator, key, .{
|
||||
.name = temporary_allocator.dupe(u8, lang.sleep) catch "",
|
||||
.cmd = cmd,
|
||||
}) catch {};
|
||||
}
|
||||
}
|
||||
|
||||
if (maybe_hibernate_key) |key| {
|
||||
if (maybe_hibernate_cmd) |cmd| {
|
||||
custom.binds.put(temporary_allocator, key, .{
|
||||
.name = temporary_allocator.dupe(u8, lang.hibernate) catch "",
|
||||
.cmd = cmd,
|
||||
}) catch {};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tryMigrateIniSaveFile(allocator: std.mem.Allocator, io: std.Io, path: []const u8, saved_users: *SavedUsers, usernames: [][]const u8) !?IniParser(OldSave) {
|
||||
|
||||
130
src/main.zig
130
src/main.zig
@@ -88,8 +88,6 @@ const UiState = struct {
|
||||
labels_max_length: usize,
|
||||
shutdown_label: Label,
|
||||
restart_label: Label,
|
||||
sleep_label: Label,
|
||||
hibernate_label: Label,
|
||||
toggle_password_label: Label,
|
||||
brightness_down_label: Label,
|
||||
brightness_up_label: Label,
|
||||
@@ -141,9 +139,6 @@ var shutdown = false;
|
||||
var restart = false;
|
||||
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
var shutdown_cmd: []const u8 = undefined;
|
||||
var restart_cmd: []const u8 = undefined;
|
||||
var commands_allocated = false;
|
||||
var state: UiState = undefined;
|
||||
|
||||
state.io = init.io;
|
||||
@@ -152,21 +147,12 @@ pub fn main(init: std.process.Init) !void {
|
||||
var stderr_writer = std.Io.File.stderr().writer(state.io, &stderr_buffer);
|
||||
var stderr = &stderr_writer.interface;
|
||||
|
||||
// If we can't shutdown or restart due to an error, we print it to standard error. If that fails, just bail out
|
||||
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.replace(state.io, .{ .argv = &[_][]const u8{ "/bin/sh", "-c", shutdown_cmd } });
|
||||
std.log.err("couldn't shutdown: {s}", .{@errorName(shutdown_error)});
|
||||
interop.shutdownSystem() catch std.log.err("whoopsie doodle, couldn't shutdown system!", .{});
|
||||
} else if (restart) {
|
||||
const restart_error = std.process.replace(state.io, .{ .argv = &[_][]const u8{ "/bin/sh", "-c", restart_cmd } });
|
||||
std.log.err("couldn't restart: {s}", .{@errorName(restart_error)});
|
||||
} else {
|
||||
// The user has quit Ly using Ctrl+C
|
||||
if (commands_allocated) {
|
||||
// Necessary if we error out before allocating
|
||||
temporary_allocator.free(shutdown_cmd);
|
||||
temporary_allocator.free(restart_cmd);
|
||||
}
|
||||
interop.rebootSystem() catch std.log.err("whoopsie doodle, couldn't restart system!", .{});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,7 +279,7 @@ pub fn main(init: std.process.Init) !void {
|
||||
}
|
||||
|
||||
if (config_parser.maybe_load_error == null) {
|
||||
migrator.lateConfigFieldHandler(&state.config);
|
||||
migrator.lateConfigFieldHandler(&state.config, state.lang);
|
||||
}
|
||||
|
||||
var maybe_uid_range_error: ?anyerror = null;
|
||||
@@ -374,12 +360,6 @@ pub fn main(init: std.process.Init) !void {
|
||||
|
||||
try state.log_file.info(state.io, "tui", "using {s} vt", .{if (state.use_kmscon_vt) "kmscon" else "default"});
|
||||
|
||||
// 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, state.config.shutdown_cmd);
|
||||
restart_cmd = try temporary_allocator.dupe(u8, state.config.restart_cmd);
|
||||
commands_allocated = true;
|
||||
|
||||
if (state.config.start_cmd) |start_cmd| handle_start_cmd: {
|
||||
var process = std.process.spawn(state.io, .{
|
||||
.argv = &[_][]const u8{ "/bin/sh", "-c", start_cmd },
|
||||
@@ -469,26 +449,6 @@ pub fn main(init: std.process.Init) !void {
|
||||
);
|
||||
defer state.restart_label.deinit();
|
||||
|
||||
state.sleep_label = Label.init(
|
||||
"",
|
||||
null,
|
||||
state.buffer.fg,
|
||||
state.buffer.bg,
|
||||
null,
|
||||
null,
|
||||
);
|
||||
defer state.sleep_label.deinit();
|
||||
|
||||
state.hibernate_label = Label.init(
|
||||
"",
|
||||
null,
|
||||
state.buffer.fg,
|
||||
state.buffer.bg,
|
||||
null,
|
||||
null,
|
||||
);
|
||||
defer state.hibernate_label.deinit();
|
||||
|
||||
state.toggle_password_label = Label.init(
|
||||
"",
|
||||
null,
|
||||
@@ -535,20 +495,6 @@ pub fn main(init: std.process.Init) !void {
|
||||
"{s} {s}",
|
||||
.{ state.config.show_password_key, state.lang.toggle_password },
|
||||
);
|
||||
if (state.config.sleep_cmd != null) {
|
||||
try state.sleep_label.setTextAlloc(
|
||||
state.allocator,
|
||||
"{s} {s}",
|
||||
.{ state.config.sleep_key, state.lang.sleep },
|
||||
);
|
||||
}
|
||||
if (state.config.hibernate_cmd != null) {
|
||||
try state.hibernate_label.setTextAlloc(
|
||||
state.allocator,
|
||||
"{s} {s}",
|
||||
.{ state.config.hibernate_key, state.lang.hibernate },
|
||||
);
|
||||
}
|
||||
if (state.config.brightness_down_key) |key| {
|
||||
try state.brightness_down_label.setTextAlloc(
|
||||
state.allocator,
|
||||
@@ -1296,12 +1242,6 @@ pub fn main(init: std.process.Init) !void {
|
||||
if (!state.hide_key_hints) {
|
||||
try layer2.append(state.allocator, state.shutdown_label.widget());
|
||||
try layer2.append(state.allocator, state.restart_label.widget());
|
||||
if (state.config.sleep_cmd != null) {
|
||||
try layer2.append(state.allocator, state.sleep_label.widget());
|
||||
}
|
||||
if (state.config.hibernate_cmd != null) {
|
||||
try layer2.append(state.allocator, state.hibernate_label.widget());
|
||||
}
|
||||
try layer2.append(state.allocator, state.toggle_password_label.widget());
|
||||
if (state.config.brightness_down_key != null) {
|
||||
try layer2.append(state.allocator, state.brightness_down_label.widget());
|
||||
@@ -1372,8 +1312,6 @@ pub fn main(init: std.process.Init) !void {
|
||||
try state.buffer.registerGlobalKeybind(state.io, state.config.shutdown_key, &shutdownCmd, &state);
|
||||
try state.buffer.registerGlobalKeybind(state.io, state.config.restart_key, &restartCmd, &state);
|
||||
try state.buffer.registerGlobalKeybind(state.io, state.config.show_password_key, &togglePasswordMask, &state);
|
||||
if (state.config.sleep_cmd != null) try state.buffer.registerGlobalKeybind(state.io, state.config.sleep_key, &sleepCmd, &state);
|
||||
if (state.config.hibernate_cmd != null) try state.buffer.registerGlobalKeybind(state.io, state.config.hibernate_key, &hibernateCmd, &state);
|
||||
if (state.config.brightness_down_key) |key| try state.buffer.registerGlobalKeybind(state.io, key, &decreaseBrightnessCmd, &state);
|
||||
if (state.config.brightness_up_key) |key| try state.buffer.registerGlobalKeybind(state.io, key, &increaseBrightnessCmd, &state);
|
||||
|
||||
@@ -1787,62 +1725,6 @@ fn restartCmd(ptr: *anyopaque) !bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
fn sleepCmd(ptr: *anyopaque) !bool {
|
||||
var state: *UiState = @ptrCast(@alignCast(ptr));
|
||||
|
||||
if (state.config.sleep_cmd) |sleep_cmd| {
|
||||
var process = std.process.spawn(state.io, .{
|
||||
.argv = &[_][]const u8{ "/bin/sh", "-c", sleep_cmd },
|
||||
.stdout = .ignore,
|
||||
.stderr = .ignore,
|
||||
}) catch return false;
|
||||
|
||||
const process_result = process.wait(state.io) catch return false;
|
||||
if (process_result.exited != 0) {
|
||||
try state.info_line.addMessage(
|
||||
state.lang.err_sleep,
|
||||
state.config.error_bg,
|
||||
state.config.error_fg,
|
||||
);
|
||||
try state.log_file.err(
|
||||
state.io,
|
||||
"sys",
|
||||
"failed to execute sleep command: exit code {d}",
|
||||
.{process_result.exited},
|
||||
);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
fn hibernateCmd(ptr: *anyopaque) !bool {
|
||||
var state: *UiState = @ptrCast(@alignCast(ptr));
|
||||
|
||||
if (state.config.hibernate_cmd) |hibernate_cmd| {
|
||||
var process = std.process.spawn(state.io, .{
|
||||
.argv = &[_][]const u8{ "/bin/sh", "-c", hibernate_cmd },
|
||||
.stdout = .ignore,
|
||||
.stderr = .ignore,
|
||||
}) catch return false;
|
||||
|
||||
const process_result = process.wait(state.io) catch return false;
|
||||
if (process_result.exited != 0) {
|
||||
try state.info_line.addMessage(
|
||||
state.lang.err_hibernate,
|
||||
state.config.error_bg,
|
||||
state.config.error_fg,
|
||||
);
|
||||
try state.log_file.err(
|
||||
state.io,
|
||||
"sys",
|
||||
"failed to execute hibernate command: exit code {d}",
|
||||
.{process_result.exited},
|
||||
);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
fn decreaseBrightnessCmd(ptr: *anyopaque) !bool {
|
||||
var state: *UiState = @ptrCast(@alignCast(ptr));
|
||||
|
||||
@@ -2114,8 +1996,6 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu
|
||||
const labels = [_]?*Label{
|
||||
&state.shutdown_label,
|
||||
&state.restart_label,
|
||||
if (state.config.sleep_cmd != null) &state.sleep_label else null,
|
||||
if (state.config.hibernate_cmd != null) &state.hibernate_label else null,
|
||||
&state.toggle_password_label,
|
||||
if (state.config.brightness_down_key != null) &state.brightness_down_label else null,
|
||||
if (state.config.brightness_up_key != null) &state.brightness_up_label else null,
|
||||
@@ -2347,8 +2227,6 @@ fn positionWidgets(ptr: *anyopaque) !void {
|
||||
// Reset all potential corner widgets to offscreen
|
||||
state.shutdown_label.positionXY(offscreen);
|
||||
state.restart_label.positionXY(offscreen);
|
||||
state.sleep_label.positionXY(offscreen);
|
||||
state.hibernate_label.positionXY(offscreen);
|
||||
state.toggle_password_label.positionXY(offscreen);
|
||||
state.brightness_down_label.positionXY(offscreen);
|
||||
state.brightness_up_label.positionXY(offscreen);
|
||||
|
||||
Reference in New Issue
Block a user