From f4bade27c48ddd3626439eb7248bdd40e74fe6f7 Mon Sep 17 00:00:00 2001 From: namedkitten Date: Sun, 12 Jul 2020 21:16:40 +0100 Subject: [PATCH] Delete info(); --- src/bar/bar.zig | 17 +++++++++++++---- src/main.zig | 2 +- src/types/mouseevent.zig | 6 +++++- src/types/widget.zig | 4 ---- src/widgets/battery/battery.zig | 3 --- src/widgets/memory/memory.zig | 3 --- src/widgets/text/text.zig | 3 --- src/widgets/time/time.zig | 4 ---- src/widgets/weather/weather.zig | 3 --- 9 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/bar/bar.zig b/src/bar/bar.zig index 8b5403f..615b661 100644 --- a/src/bar/bar.zig +++ b/src/bar/bar.zig @@ -1,6 +1,7 @@ const std = @import("std"); const Widget = @import("../types/widget.zig").Widget; const Info = @import("../types/info.zig").Info; +const MouseEvent = @import("../types/mouseevent.zig").MouseEvent; const terminal_version = @import("build_options").terminal_version; @@ -9,7 +10,7 @@ pub const Bar = struct { widgets: []const *Widget, running: bool, infos: std.ArrayList(Info), - mutex: std.Mutex, + items_mutex: std.Mutex, out_file: std.fs.File, pub fn start(self: *Bar) !void { self.running = true; @@ -35,7 +36,7 @@ pub const Bar = struct { fn print_infos(self: *Bar, should_lock: bool) !void { if (should_lock) { - const lock = self.mutex.acquire(); + const lock = self.items_mutex.acquire(); defer lock.release(); } if (!terminal_version) try self.out_file.writer().writeAll("["); @@ -63,12 +64,20 @@ pub const Bar = struct { fn process(self: *Bar) !void { var line_buffer: [512]u8 = undefined; while (self.running) { + var arena = std.heap.ArenaAllocator.init(self.allocator); + defer arena.deinit(); + var allocator = &arena.allocator; const line_opt = try std.io.getStdIn().inStream().readUntilDelimiterOrEof(&line_buffer, '\n'); if (line_opt) |l| { var line = l; if (std.mem.eql(u8, line, "[")) continue; if (line[0] == ',') line = line[1..line.len]; std.debug.print("{}\n", .{line}); + + const parseOptions = std.json.ParseOptions{ .allocator = allocator }; + const data = try std.json.parse(MouseEvent, &std.json.TokenStream.init(line), parseOptions); + std.debug.print("{}\n", .{data}); + std.json.parseFree(MouseEvent, data, parseOptions); } //std.time.sleep(5000 * std.time.ns_per_ms); @@ -97,7 +106,7 @@ pub const Bar = struct { } pub fn add(self: *Bar, info: Info) !void { - const lock = self.mutex.acquire(); + const lock = self.items_mutex.acquire(); defer lock.release(); //std.debug.warn("info: {}\n", .{info.name}); for (self.infos.items) |infoItem, index| { @@ -121,7 +130,7 @@ pub fn InitBar(allocator: *std.mem.Allocator) Bar { .widgets = undefined, .running = false, .infos = std.ArrayList(Info).init(allocator), - .mutex = std.Mutex.init(), + .items_mutex = std.Mutex.init(), .out_file = std.io.getStdOut(), }; } diff --git a/src/main.zig b/src/main.zig index 51acccc..5d9a201 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,7 +28,7 @@ pub fn main() !void { &Widget.init(&textWidget.New("owo", "potato")), // 4KiB &Widget.init(&textWidget.New("uwu", "tomato")), // 4KiB &Widget.init(&memoryWidget.New(allocator, &br)), // 8.08KiB - &Widget.init(&weatherWidget.New(allocator, &br, "London")), // 16KiB + &Widget.init(&weatherWidget.New(allocator, &br, "Stockholm")), // 16KiB &Widget.init(&batteryWidget.New(allocator, &br)), // 12.11KiB &Widget.init(&timeWidget.New(allocator, &br)), // 32.46KiB }; diff --git a/src/types/mouseevent.zig b/src/types/mouseevent.zig index 4a4f259..618ada3 100644 --- a/src/types/mouseevent.zig +++ b/src/types/mouseevent.zig @@ -1,11 +1,15 @@ pub const MouseEvent = struct { name: []const u8, - button: enum(u3) { + button: enum(u4) { LeftClick = 1, MiddleClick = 2, RightClick = 3, ScrollUp = 4, ScrollDown = 5, + WheelLeft = 6, + WheelRight = 7, + Backwards = 8, + Forwards = 9, }, event: u16, x: u16, diff --git a/src/types/widget.zig b/src/types/widget.zig index 33720df..0346483 100644 --- a/src/types/widget.zig +++ b/src/types/widget.zig @@ -7,7 +7,6 @@ pub const Widget = struct { const IFace = Interface(struct { name: fn (*SelfType) []const u8, initial_info: fn (*SelfType) Info, - info: fn (*SelfType) Info, start: fn (*SelfType) anyerror!void, }, interface.Storage.NonOwning); iface: IFace, @@ -17,9 +16,6 @@ pub const Widget = struct { pub fn name(self: *Widget) []const u8 { return self.iface.call("name", .{}); } - pub fn info(self: *Widget) Info { - return self.iface.call("info", .{}); - } pub fn initial_info(self: *Widget) Info { return self.iface.call("initial_info", .{}); } diff --git a/src/widgets/battery/battery.zig b/src/widgets/battery/battery.zig index bd673ab..57da053 100644 --- a/src/widgets/battery/battery.zig +++ b/src/widgets/battery/battery.zig @@ -43,9 +43,6 @@ pub const BatteryWidget = struct { .markup = "pango", }; } - pub fn info(self: *BatteryWidget) Info { - return self.initial_info(); - } pub fn get_power_paths(self: *BatteryWidget, provided_allocator: *std.mem.Allocator) anyerror!PowerPaths { var arena = std.heap.ArenaAllocator.init(provided_allocator); defer arena.deinit(); diff --git a/src/widgets/memory/memory.zig b/src/widgets/memory/memory.zig index 99a4b80..0bdad69 100644 --- a/src/widgets/memory/memory.zig +++ b/src/widgets/memory/memory.zig @@ -90,9 +90,6 @@ pub const MemoryWidget = struct { .markup = "pango", }; } - pub fn info(self: *MemoryWidget) Info { - return self.initial_info(); - } fn update_bar(self: *MemoryWidget) !void { var arena = std.heap.ArenaAllocator.init(self.allocator); diff --git a/src/widgets/text/text.zig b/src/widgets/text/text.zig index 93b57fb..2a1ab06 100644 --- a/src/widgets/text/text.zig +++ b/src/widgets/text/text.zig @@ -15,9 +15,6 @@ pub const TextWidget = struct { .markup = "pango", }; } - pub fn info(self: *TextWidget) Info { - return self.initial_info(); - } pub fn start(self: *TextWidget) anyerror!void {} }; diff --git a/src/widgets/time/time.zig b/src/widgets/time/time.zig index 9290979..552125e 100644 --- a/src/widgets/time/time.zig +++ b/src/widgets/time/time.zig @@ -19,10 +19,6 @@ pub const TimeWidget = struct { }; } - pub fn info(self: *TimeWidget) Info { - return self.initial_info(); - } - pub fn start(self: *TimeWidget) anyerror!void { while (self.bar.keep_running()) { var arena = std.heap.ArenaAllocator.init(self.allocator); diff --git a/src/widgets/weather/weather.zig b/src/widgets/weather/weather.zig index 2520e43..509b9be 100644 --- a/src/widgets/weather/weather.zig +++ b/src/widgets/weather/weather.zig @@ -30,9 +30,6 @@ pub const WeatherWidget = struct { .markup = "pango", }; } - pub fn info(self: *WeatherWidget) Info { - return self.initial_info(); - } fn get_weather_info(self: *WeatherWidget, allocator: *std.mem.Allocator) !WeatherData { // this will allocate some memory but it will be freed by the time it is returned.