101 lines
2.3 KiB
Zig
101 lines
2.3 KiB
Zig
const std = @import("std");
|
|
const cfg = @import("config.zig");
|
|
const ffi = @import("ffi.zig");
|
|
const c = ffi.c;
|
|
const gui = @import("gui.zig");
|
|
|
|
test {
|
|
_ = @import("config.zig");
|
|
_ = @import("ffi.zig");
|
|
_ = @import("gui.zig");
|
|
}
|
|
|
|
pub fn log(
|
|
comptime level: std.log.Level,
|
|
comptime scope: @TypeOf(.EnumLiteral),
|
|
comptime format: []const u8,
|
|
args: anytype,
|
|
) void {
|
|
const g_level = switch (level) {
|
|
.err => c.G_LOG_LEVEL_ERROR,
|
|
.warn => c.G_LOG_LEVEL_WARNING,
|
|
.info => c.G_LOG_LEVEL_INFO,
|
|
.debug => c.G_LOG_LEVEL_DEBUG,
|
|
};
|
|
|
|
var buf: [1024]u8 = undefined;
|
|
|
|
const s = std.fmt.bufPrintZ(
|
|
&buf,
|
|
format,
|
|
args,
|
|
) catch return;
|
|
|
|
var fields = [_]c.GLogField{
|
|
c.GLogField{
|
|
.key = "GLIB_DOMAIN",
|
|
.value = if (scope == .default) "gpower2" else "gpower2-" ++ @tagName(scope),
|
|
.length = -1,
|
|
},
|
|
c.GLogField{
|
|
.key = "MESSAGE",
|
|
.value = @ptrCast(*const anyopaque, s),
|
|
.length = -1,
|
|
},
|
|
};
|
|
|
|
c.g_log_structured_array(
|
|
g_level,
|
|
&fields,
|
|
fields.len,
|
|
);
|
|
}
|
|
|
|
// glib filters log messages
|
|
pub const log_level = .debug;
|
|
|
|
pub fn main() !u8 {
|
|
var conf = try cfg.Config.parse(std.heap.c_allocator);
|
|
defer conf.deinit();
|
|
cfg.global_config = &conf;
|
|
|
|
std.log.debug(
|
|
\\Using Configs:
|
|
\\
|
|
\\Shutdown Command: {s}
|
|
\\Reboot Command: {s}
|
|
\\Suspend Command: {s}
|
|
\\Hibernate Command: {s}
|
|
, .{
|
|
conf.shutdown_command,
|
|
conf.reboot_command,
|
|
conf.suspend_command,
|
|
conf.hibernate_command,
|
|
});
|
|
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
|
|
defer arena.deinit();
|
|
|
|
var state = gui.GuiState{
|
|
.alloc = std.heap.c_allocator,
|
|
.user_data_arena = arena.allocator(),
|
|
};
|
|
|
|
const app = c.gtk_application_new("de.mzte.gpower2", c.G_APPLICATION_FLAGS_NONE);
|
|
defer c.g_object_unref(app);
|
|
|
|
ffi.connectSignal(app, "activate", @ptrCast(c.GCallback, gui.activate), &state);
|
|
|
|
const status = c.g_application_run(
|
|
@ptrCast(*c.GApplication, app),
|
|
@intCast(i32, std.os.argv.len),
|
|
@ptrCast([*c][*c]u8, std.os.argv.ptr),
|
|
);
|
|
|
|
if (state.child) |*ch| {
|
|
_ = try ch.*.wait();
|
|
}
|
|
|
|
return @intCast(u8, status);
|
|
}
|