Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

wifi indicator update

oxydizer3206eae0d210
diff --git a/src/Taskbar.zig b/src/Taskbar.zig
index 6413e83..1833eda 100644
--- a/src/Taskbar.zig
+++ b/src/Taskbar.zig
@@ -192,6 +192,9 @@ hover_target: Target = .none,
audio_generation_seen: u32 = 0,
audio_volume: f32 = 0,
audio_muted: bool = false,
+// NetworkManager reports no connection: the wifi icon is drawn in the danger
+// colour, like a muted speaker. Pushed by `network.Manager`, never polled.
+network_offline: bool = false,
clock_time: [16]u8 = [_]u8{0} ** 16,
clock_date: [128]u8 = [_]u8{0} ** 128,
@@ -390,6 +393,7 @@ pub fn create(server: *Server, wlr_output: *wlr.Output) !*Taskbar {
.capture_node = capture_node,
.battery_node = battery_node,
.battery_state = battery.read(server.io, gpa),
+ .network_offline = if (server.network) |network| network.offline() else false,
.clock_minute = @divFloor(c_time.time(null), 60),
.timer = undefined,
};
@@ -450,6 +454,16 @@ pub fn applyBattery(bar: *Taskbar, next: battery.State) void {
bar.wlr_output.scheduleFrame();
}
+/// A flip of `network.Manager.offline` recolours the wifi icon; nothing else
+/// about the tray changes, so only that icon repaints.
+pub fn applyNetwork(bar: *Taskbar, offline: bool) void {
+ if (offline == bar.network_offline) return;
+ bar.network_offline = offline;
+ bar.tray_dirty[@intFromEnum(TrayKind.wifi)] = true;
+ bar.markDirty(paint_tray);
+ bar.wlr_output.scheduleFrame();
+}
+
fn onTick(bar: *Taskbar) c_int {
// Seconds only repaint the clock. Keep expensive sysfs reads and the
// calendar on their minute cadence; plug events have their own uevent.
@@ -1611,7 +1625,11 @@ fn renderPart(bar: *Taskbar, now_ms: i64, part: Part, origin_x: i32, origin_y: i
}
if (part == .tray) {
const i = part.tray;
- const color = if (i == @intFromEnum(TrayKind.volume) and bar.audio_muted)
+ const alert = switch (@as(TrayKind, @enumFromInt(i))) {
+ .volume => bar.audio_muted,
+ .wifi => bar.network_offline,
+ };
+ const color = if (alert)
Color.fromRgba(ui_theme.taskbar(.danger))
else
Color.lerp(Color.tray_icon(), Color.tray_icon_hover(), hover);
diff --git a/src/network/manager.zig b/src/network/manager.zig
index ccd4ff6..da4df29 100644
--- a/src/network/manager.zig
+++ b/src/network/manager.zig
@@ -133,6 +133,8 @@ pub const Manager = struct {
wired_timer: ?*@import("wayland").server.wl.EventSource = null,
wired_message: []const u8 = "",
wired_generation: u64 = 0,
+ /// What the taskbars were last told by `publishOffline`.
+ offline_published: bool = false,
pub fn create(server: *Server) !*Manager {
const self = try gpa.create(Manager);
@@ -155,8 +157,23 @@ pub const Manager = struct {
self.scan_pending = true;
if (self.conn == null or self.conn.?.closed) self.connect() else self.fetch();
}
+ /// NetworkManager reports no connection at all (state ASLEEP or DISCONNECTED;
+ /// connecting, unknown and an absent service are not alarming), by wifi or
+ /// wire. The taskbar tints its network icon with it.
+ pub fn offline(self: *const Manager) bool {
+ return self.available and (self.state == 10 or self.state == 20);
+ }
+ /// Only a flip reaches the taskbars, so an idle session stays idle.
+ fn publishOffline(self: *Manager) void {
+ const now = self.offline();
+ if (now == self.offline_published) return;
+ self.offline_published = now;
+ var it = self.server.outputs.iterator(.forward);
+ while (it.next()) |output| if (output.taskbar) |bar| bar.applyNetwork(now);
+ }
fn refresh(self: *Manager) void {
if (self.closing) return;
+ self.publishOffline();
if (self.server.input.open_wifi) |popup| popup.changed();
if (self.server.input.open_control_center) |cc| if (cc.page == .network) cc.refresh();
}
@@ -241,6 +258,13 @@ pub const Manager = struct {
saved.loaded = false;
};
}
+ // The global state is one small signal: track it with no snapshot, so the
+ // taskbar stays right while no popup is open.
+ if (eq(u8, msg.headers.interface orelse "", nm) and eq(u8, msg.headers.member orelse "", "StateChanged") and eq(u8, msg.headers.signature, "u")) {
+ var body = msg.body;
+ self.state = try body.uint32();
+ self.publishOffline();
+ }
const settings_open = if (self.server.input.open_control_center) |cc| cc.page == .network else false;
if (self.server.input.open_wifi != null or settings_open or self.busy or self.wired_pending != null) self.fetch();
}
diff --git a/tests/wifi_popup.py b/tests/wifi_popup.py
index b57f8b6..0550e1e 100644
--- a/tests/wifi_popup.py
+++ b/tests/wifi_popup.py
@@ -29,11 +29,11 @@ ETH = ROOT + '/Devices/2'
IP4 = ROOT + '/IP4Config/2'
conn = Gio.bus_get_sync(Gio.BusType.SESSION)
v = GLib.Variant
-state = {'enabled': True, 'hardware': True, 'active': 1, 'fail': False, 'device': True, 'many': False, 'updated': False, 'last_scan': -1, 'hold_scan': True, 'ethernet': True, 'eth_state': 100, 'carrier': True, 'managed': True, 'eth_profile': True, 'eth_fail': False, 'hold_eth': False, 'connectivity': 4}
+state = {'global_state': 70, 'enabled': True, 'hardware': True, 'active': 1, 'fail': False, 'device': True, 'many': False, 'updated': False, 'last_scan': -1, 'hold_scan': True, 'ethernet': True, 'eth_state': 100, 'carrier': True, 'managed': True, 'eth_profile': True, 'eth_fail': False, 'hold_eth': False, 'connectivity': 4}
names = ['REDI_Home', 'Studio_5G', 'Cafe_WiFi', 'Linksys_7432', 'OfficeNet', 'MySpectrum']
def objects():
count = state.get('count', 35 if state['many'] else 6)
- result = {ROOT: {NM: {'WirelessEnabled': v('b', state['enabled']), 'WirelessHardwareEnabled': v('b', state['hardware']), 'State': v('u', 70), 'Connectivity': v('u', state['connectivity'])}}, SAVED: {NM+'.Settings.Connection': {}}}
+ result = {ROOT: {NM: {'WirelessEnabled': v('b', state['enabled']), 'WirelessHardwareEnabled': v('b', state['hardware']), 'State': v('u', state['global_state']), 'Connectivity': v('u', state['connectivity'])}}, SAVED: {NM+'.Settings.Connection': {}}}
if state['ethernet']:
result[ETH] = {NM+'.Device': {'Interface': v('s','enp4s0'), 'Driver': v('s','r8169'), 'Managed': v('b',state['managed']), 'State': v('u',state['eth_state']), 'Ip4Config': v('o',IP4), 'AvailableConnections': v('ao',[ROOT+'/Settings/2'] if state['eth_profile'] else [])}, NM+'.Device.Wired': {'Carrier': v('b',state['carrier']), 'Speed': v('u',2500), 'HwAddress': v('s','3C:EC:EF:12:34:56')}}
result[IP4] = {NM+'.IP4Config': {'AddressData': v('aa{sv}',[{'address':v('s','192.168.1.100'),'prefix':v('u',24)}]), 'Gateway': v('s','192.168.1.1'), 'NameserverData': v('aa{sv}',[{'address':v('s','1.1.1.1')},{'address':v('s','1.0.0.1')}])}}
@@ -123,6 +123,9 @@ def command(channel,cond):
req=json.loads(sys.stdin.readline())
state.update(req)
if req.get('complete_scan'): completed()
+ if 'global_state' in req:
+ conn.emit_signal(None, ROOT, NM, 'StateChanged', v('(u)', (req['global_state'],)))
+ conn.flush_sync()
if 'device' in req:
# Device hotplug is announced from ObjectManager's parent path.
if state['device']:
@@ -189,6 +192,20 @@ def run(scale):
target=Path('/tmp')/f'rediwm-wifi-{name}-{scale}-{os.environ.get("REDIWM_TEST_RENDERER", "gles2")}.png'
target.unlink(missing_ok=True)
ipc.screenshot(str(target))
+ def tray_red():
+ box,raster,target=boxes['network'],float(scale),tmp/'tray.png'
+ target.unlink(missing_ok=True)
+ ipc.screenshot(str(target))
+ with Image.open(target) as image:
+ icon=image.convert('RGB').crop((round(box['x']*raster),round(box['y']*raster),round((box['x']+box['width'])*raster),round((box['y']+box['height'])*raster)))
+ return any(red > green+60 and red > blue+60 for red,green,blue in icon.getdata())
+ # The tray icon follows NetworkManager's global state with no popup open:
+ # red only while nothing is connected, never while connecting.
+ assert not tray_red(), 'connected: the wifi icon is red'
+ command(global_state=20);assert tray_red(), 'disconnected: the wifi icon is not red'
+ command(global_state=40);assert not tray_red(), 'connecting: the wifi icon is red'
+ command(global_state=10);assert tray_red(), 'asleep: the wifi icon is not red'
+ command(global_state=70);assert not tray_red(), 'reconnected: the wifi icon is still red'
open_menu()
wait_for(lambda: ipc.hit_test(*at(100,100)).get('widget')=='row','network rows')
def sonar_image():