Finally made it wait for ^C
This commit is contained in:
parent
81f6ae9877
commit
f6136e505f
|
@ -17,6 +17,12 @@ pub fn build(b: *Builder) void {
|
||||||
"terminal-only version",
|
"terminal-only version",
|
||||||
) orelse false;
|
) orelse false;
|
||||||
exe.addBuildOption(bool, "terminal_version", terminal_version);
|
exe.addBuildOption(bool, "terminal_version", terminal_version);
|
||||||
|
const disable_terminal_mouse = b.option(
|
||||||
|
bool,
|
||||||
|
"disable_terminal_mouse",
|
||||||
|
"disables mouse input processing for terminal",
|
||||||
|
) orelse false;
|
||||||
|
exe.addBuildOption(bool, "disable_terminal_mouse", disable_terminal_mouse);
|
||||||
const debug_allocator = b.option(
|
const debug_allocator = b.option(
|
||||||
bool,
|
bool,
|
||||||
"debug_allocator",
|
"debug_allocator",
|
||||||
|
@ -35,7 +41,6 @@ pub fn build(b: *Builder) void {
|
||||||
}
|
}
|
||||||
exe.addBuildOption([]const u8, "weather_location", weather_location);
|
exe.addBuildOption([]const u8, "weather_location", weather_location);
|
||||||
|
|
||||||
|
|
||||||
//exe.strip = true;
|
//exe.strip = true;
|
||||||
exe.addPackage(.{
|
exe.addPackage(.{
|
||||||
.name = "interfaces",
|
.name = "interfaces",
|
||||||
|
|
|
@ -5,6 +5,19 @@ const MouseEvent = @import("../types/mouseevent.zig");
|
||||||
const os = std.os;
|
const os = std.os;
|
||||||
const terminal_version = @import("build_options").terminal_version;
|
const terminal_version = @import("build_options").terminal_version;
|
||||||
const debug_allocator = @import("build_options").debug_allocator;
|
const debug_allocator = @import("build_options").debug_allocator;
|
||||||
|
const disable_terminal_mouse = @import("build_options").disable_terminal_mouse;
|
||||||
|
|
||||||
|
fn readFromSignalFd(signal_fd: std.os.fd_t) !void {
|
||||||
|
var buf: [@sizeOf(os.linux.signalfd_siginfo)]u8 align(8) = undefined;
|
||||||
|
_ = try os.read(signal_fd, &buf);
|
||||||
|
return error.Shutdown;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sigemptyset(set: *std.os.sigset_t) void {
|
||||||
|
for (set) |*val| {
|
||||||
|
val.* = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub const Bar = struct {
|
pub const Bar = struct {
|
||||||
allocator: *std.mem.Allocator,
|
allocator: *std.mem.Allocator,
|
||||||
|
@ -21,19 +34,44 @@ pub const Bar = struct {
|
||||||
try self.infos.append(try self.dupe_info(w.initial_info()));
|
try self.infos.append(try self.dupe_info(w.initial_info()));
|
||||||
}
|
}
|
||||||
try self.print_infos(true);
|
try self.print_infos(true);
|
||||||
|
var mask: std.os.sigset_t = undefined;
|
||||||
|
|
||||||
|
sigemptyset(&mask);
|
||||||
|
os.linux.sigaddset(&mask, std.os.SIGTERM);
|
||||||
|
os.linux.sigaddset(&mask, std.os.SIGINT);
|
||||||
|
_ = os.linux.sigprocmask(std.os.SIG_BLOCK, &mask, null);
|
||||||
|
const signal_fd = try os.signalfd(-1, &mask, 0);
|
||||||
|
defer os.close(signal_fd);
|
||||||
|
std.debug.print("signalfd: {}\n", .{signal_fd});
|
||||||
|
|
||||||
for (self.widgets) |w| {
|
for (self.widgets) |w| {
|
||||||
var thread = try std.Thread.spawn(w, Widget.start);
|
var thread = try std.Thread.spawn(w, Widget.start);
|
||||||
}
|
}
|
||||||
var thread = try std.Thread.spawn(self, Bar.process);
|
var thread = try std.Thread.spawn(self, Bar.process);
|
||||||
// TODO: wait for kill signal to kill bar instead of waiting for thread.
|
// TODO: wait for kill signal to kill bar instead of waiting for thread.
|
||||||
thread.wait();
|
//thread.wait();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
readFromSignalFd(signal_fd) catch |err| {
|
||||||
|
if (err == error.Shutdown) break else std.debug.print("failed to read from signal fd: {}\n", .{err});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
std.debug.print("Shutting Down.\n", .{});
|
||||||
|
|
||||||
self.running = false;
|
self.running = false;
|
||||||
|
const lock = self.items_mutex.acquire();
|
||||||
|
defer lock.release();
|
||||||
// Wait for most widgets to stop.
|
// Wait for most widgets to stop.
|
||||||
std.time.sleep(1000 * std.time.ns_per_ms);
|
std.time.sleep(1000 * std.time.ns_per_ms);
|
||||||
|
|
||||||
for (self.infos.items) |info| {
|
for (self.infos.items) |info| {
|
||||||
try self.free_info(info);
|
try self.free_info(info);
|
||||||
}
|
}
|
||||||
self.infos.deinit();
|
self.infos.deinit();
|
||||||
|
std.debug.print("Shut Down.\n", .{});
|
||||||
|
if (terminal_version and !disable_terminal_mouse) {
|
||||||
|
try self.out_file.writer().writeAll("\u{001b}[?1000;1006;1015l");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fn print_i3bar_infos(self: *Bar) !void {
|
inline fn print_i3bar_infos(self: *Bar) !void {
|
||||||
|
@ -92,7 +130,7 @@ pub const Bar = struct {
|
||||||
// TODO: reset on bar end.
|
// TODO: reset on bar end.
|
||||||
try os.tcsetattr(0, .FLUSH, termios);
|
try os.tcsetattr(0, .FLUSH, termios);
|
||||||
|
|
||||||
while (true) {
|
while (self.running) {
|
||||||
var line_buffer: [128]u8 = undefined;
|
var line_buffer: [128]u8 = undefined;
|
||||||
// 0x1b is the ESC key which is used for sending and recieving events to xterm terminals.
|
// 0x1b is the ESC key which is used for sending and recieving events to xterm terminals.
|
||||||
const line_opt = try std.io.getStdIn().inStream().readUntilDelimiterOrEof(&line_buffer, 0x1b);
|
const line_opt = try std.io.getStdIn().inStream().readUntilDelimiterOrEof(&line_buffer, 0x1b);
|
||||||
|
@ -186,17 +224,17 @@ pub const Bar = struct {
|
||||||
// Right now this is what we do for the debug allocator for testing memory usage.
|
// Right now this is what we do for the debug allocator for testing memory usage.
|
||||||
// If it the best code? Heck no but until we can gracefully ^C the program
|
// If it the best code? Heck no but until we can gracefully ^C the program
|
||||||
// this is the best we can do.
|
// this is the best we can do.
|
||||||
if (debug_allocator) {
|
|
||||||
std.time.sleep(std.time.ns_per_ms * 2000 * 5);
|
|
||||||
if (true) return;
|
|
||||||
}
|
|
||||||
// TODO: log errors.
|
// TODO: log errors.
|
||||||
|
while (self.running) {
|
||||||
if (terminal_version) {
|
if (terminal_version) {
|
||||||
|
if (!disable_terminal_mouse) {
|
||||||
self.terminal_input_process() catch {};
|
self.terminal_input_process() catch {};
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
self.i3bar_input_process() catch {};
|
self.i3bar_input_process() catch {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
pub fn keep_running(self: *Bar) bool {
|
pub fn keep_running(self: *Bar) bool {
|
||||||
// TODO: maybe rename this function to something more descriptive?
|
// TODO: maybe rename this function to something more descriptive?
|
||||||
return self.running;
|
return self.running;
|
||||||
|
@ -226,6 +264,7 @@ pub const Bar = struct {
|
||||||
pub fn add(self: *Bar, info: Info) !void {
|
pub fn add(self: *Bar, info: Info) !void {
|
||||||
const lock = self.items_mutex.acquire();
|
const lock = self.items_mutex.acquire();
|
||||||
defer lock.release();
|
defer lock.release();
|
||||||
|
if (!self.running) return;
|
||||||
for (self.infos.items) |infoItem, index| {
|
for (self.infos.items) |infoItem, index| {
|
||||||
if (std.mem.eql(u8, infoItem.name, info.name)) {
|
if (std.mem.eql(u8, infoItem.name, info.name)) {
|
||||||
if (std.mem.eql(u8, infoItem.full_text, info.full_text)) {
|
if (std.mem.eql(u8, infoItem.full_text, info.full_text)) {
|
||||||
|
|
|
@ -16,7 +16,7 @@ fn formatCPUPercent(allocator: *std.mem.Allocator, percent: f64) ![]const u8 {
|
||||||
} else {
|
} else {
|
||||||
percentColour = "green";
|
percentColour = "green";
|
||||||
}
|
}
|
||||||
const percentString = try std.fmt.allocPrint(allocator, "{d:.2}" ++ comptimeColour("accentdark", "%"), .{percent});
|
const percentString = try std.fmt.allocPrint(allocator, "{d:0<2.2}" ++ comptimeColour("accentdark", "%"), .{percent});
|
||||||
|
|
||||||
return colour(allocator, percentColour, percentString);
|
return colour(allocator, percentColour, percentString);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue