added actual systray file
Browse files
+453 −0 src/bar/systray.zig new file mode 100644
| @@ -0,0 +1,453 @@ | ||
| 1 | +const std = @import("std"); | |
| 2 | +const xlib = @import("../x11/xlib.zig"); | |
| 3 | + | |
| 4 | +pub const SYSTEM_TRAY_REQUEST_DOCK: c_long = 0; | |
| 5 | +pub const SYSTEM_TRAY_BEGIN_MESSAGE: c_long = 1; | |
| 6 | +pub const SYSTEM_TRAY_CANCEL_MESSAGE: c_long = 2; | |
| 7 | + | |
| 8 | +pub const XEMBED_EMBEDDED_NOTIFY: c_long = 0; | |
| 9 | +pub const XEMBED_WINDOW_ACTIVATE: c_long = 1; | |
| 10 | +pub const XEMBED_WINDOW_DEACTIVATE: c_long = 2; | |
| 11 | +pub const XEMBED_REQUEST_FOCUS: c_long = 3; | |
| 12 | +pub const XEMBED_FOCUS_IN: c_long = 4; | |
| 13 | +pub const XEMBED_FOCUS_OUT: c_long = 5; | |
| 14 | +pub const XEMBED_FOCUS_NEXT: c_long = 6; | |
| 15 | +pub const XEMBED_FOCUS_PREV: c_long = 7; | |
| 16 | +pub const XEMBED_MAPPED: c_ulong = 1 << 0; | |
| 17 | + | |
| 18 | +pub const XEMBED_VERSION: c_long = 0; | |
| 19 | + | |
| 20 | +pub const Icon = struct { | |
| 21 | + window: xlib.Window, | |
| 22 | + width: i32, | |
| 23 | + height: i32, | |
| 24 | + mapped: bool, | |
| 25 | +}; | |
| 26 | + | |
| 27 | +pub const Systray = struct { | |
| 28 | + window: xlib.Window, | |
| 29 | + icons: std.ArrayList(Icon), | |
| 30 | + allocator: std.mem.Allocator, | |
| 31 | + display: *xlib.Display, | |
| 32 | + screen: c_int, | |
| 33 | + icon_size: i32, | |
| 34 | + bar_height: i32, | |
| 35 | + | |
| 36 | + net_system_tray_opcode: xlib.Atom, | |
| 37 | + net_system_tray_orientation: xlib.Atom, | |
| 38 | + net_system_tray_visual: xlib.Atom, | |
| 39 | + manager_atom: xlib.Atom, | |
| 40 | + xembed: xlib.Atom, | |
| 41 | + xembed_info: xlib.Atom, | |
| 42 | + | |
| 43 | + pub fn init( | |
| 44 | + allocator: std.mem.Allocator, | |
| 45 | + display: *xlib.Display, | |
| 46 | + screen: c_int, | |
| 47 | + bar_win: xlib.Window, | |
| 48 | + bar_height: i32, | |
| 49 | + background: u32, | |
| 50 | + ) ?*Systray { | |
| 51 | + const self = allocator.create(Systray) catch return null; | |
| 52 | + | |
| 53 | + const root = xlib.XRootWindow(display, screen); | |
| 54 | + | |
| 55 | + self.allocator = allocator; | |
| 56 | + self.display = display; | |
| 57 | + self.screen = screen; | |
| 58 | + self.bar_height = bar_height; | |
| 59 | + self.icon_size = bar_height - 4; | |
| 60 | + self.icons = .empty; | |
| 61 | + | |
| 62 | + self.net_system_tray_opcode = xlib.XInternAtom(display, "_NET_SYSTEM_TRAY_OPCODE", xlib.False); | |
| 63 | + self.net_system_tray_orientation = xlib.XInternAtom(display, "_NET_SYSTEM_TRAY_ORIENTATION", xlib.False); | |
| 64 | + self.net_system_tray_visual = xlib.XInternAtom(display, "_NET_SYSTEM_TRAY_VISUAL", xlib.False); | |
| 65 | + self.xembed = xlib.XInternAtom(display, "_XEMBED", xlib.False); | |
| 66 | + self.xembed_info = xlib.XInternAtom(display, "_XEMBED_INFO", xlib.False); | |
| 67 | + | |
| 68 | + var selection_name: [32]u8 = undefined; | |
| 69 | + const name_len = std.fmt.bufPrint(&selection_name, "_NET_SYSTEM_TRAY_S{d}", .{screen}) catch return null; | |
| 70 | + selection_name[name_len.len] = 0; | |
| 71 | + self.manager_atom = xlib.XInternAtom(display, @ptrCast(&selection_name), xlib.False); | |
| 72 | + | |
| 73 | + const current_owner = xlib.c.XGetSelectionOwner(display, self.manager_atom); | |
| 74 | + if (current_owner != xlib.None) { | |
| 75 | + std.debug.print("systray: another systray is already running\n", .{}); | |
| 76 | + allocator.destroy(self); | |
| 77 | + return null; | |
| 78 | + } | |
| 79 | + | |
| 80 | + self.window = xlib.c.XCreateSimpleWindow( | |
| 81 | + display, | |
| 82 | + bar_win, | |
| 83 | + 0, | |
| 84 | + 0, | |
| 85 | + 1, | |
| 86 | + @intCast(bar_height), | |
| 87 | + 0, | |
| 88 | + 0, | |
| 89 | + 0, | |
| 90 | + ); | |
| 91 | + | |
| 92 | + var attrs: xlib.c.XSetWindowAttributes = undefined; | |
| 93 | + attrs.event_mask = xlib.c.SubstructureNotifyMask | xlib.c.SubstructureRedirectMask | xlib.c.StructureNotifyMask | xlib.c.ExposureMask; | |
| 94 | + attrs.override_redirect = xlib.True; | |
| 95 | + attrs.background_pixel = background; | |
| 96 | + _ = xlib.c.XChangeWindowAttributes( | |
| 97 | + display, | |
| 98 | + self.window, | |
| 99 | + xlib.c.CWEventMask | xlib.c.CWOverrideRedirect | xlib.c.CWBackPixel, | |
| 100 | + &attrs, | |
| 101 | + ); | |
| 102 | + | |
| 103 | + _ = xlib.c.XSetSelectionOwner(display, self.manager_atom, self.window, xlib.CurrentTime); | |
| 104 | + if (xlib.c.XGetSelectionOwner(display, self.manager_atom) != self.window) { | |
| 105 | + std.debug.print("systray: failed to acquire selection ownership\n", .{}); | |
| 106 | + _ = xlib.c.XDestroyWindow(display, self.window); | |
| 107 | + allocator.destroy(self); | |
| 108 | + return null; | |
| 109 | + } | |
| 110 | + | |
| 111 | + const orientation: c_long = 0; | |
| 112 | + _ = xlib.XChangeProperty( | |
| 113 | + display, | |
| 114 | + self.window, | |
| 115 | + self.net_system_tray_orientation, | |
| 116 | + xlib.XA_CARDINAL, | |
| 117 | + 32, | |
| 118 | + xlib.PropModeReplace, | |
| 119 | + @ptrCast(&orientation), | |
| 120 | + 1, | |
| 121 | + ); | |
| 122 | + | |
| 123 | + self.sendManagerMessage(root); | |
| 124 | + | |
| 125 | + _ = xlib.XMapWindow(display, self.window); | |
| 126 | + _ = xlib.XSync(display, xlib.False); | |
| 127 | + | |
| 128 | + std.debug.print("systray: initialized on screen {d}\n", .{screen}); | |
| 129 | + return self; | |
| 130 | + } | |
| 131 | + | |
| 132 | + pub fn deinit(self: *Systray) void { | |
| 133 | + for (self.icons.items) |icon| { | |
| 134 | + _ = xlib.c.XUnmapWindow(self.display, icon.window); | |
| 135 | + _ = xlib.c.XReparentWindow(self.display, icon.window, xlib.XRootWindow(self.display, self.screen), 0, 0); | |
| 136 | + } | |
| 137 | + self.icons.deinit(self.allocator); | |
| 138 | + _ = xlib.c.XDestroyWindow(self.display, self.window); | |
| 139 | + self.allocator.destroy(self); | |
| 140 | + } | |
| 141 | + | |
| 142 | + fn sendManagerMessage(self: *Systray, root: xlib.Window) void { | |
| 143 | + var ev: xlib.c.XClientMessageEvent = undefined; | |
| 144 | + ev.type = xlib.ClientMessage; | |
| 145 | + ev.window = root; | |
| 146 | + ev.message_type = xlib.XInternAtom(self.display, "MANAGER", xlib.False); | |
| 147 | + ev.format = 32; | |
| 148 | + ev.data.l[0] = xlib.CurrentTime; | |
| 149 | + ev.data.l[1] = @intCast(self.manager_atom); | |
| 150 | + ev.data.l[2] = @intCast(self.window); | |
| 151 | + ev.data.l[3] = 0; | |
| 152 | + ev.data.l[4] = 0; | |
| 153 | + | |
| 154 | + _ = xlib.XSendEvent( | |
| 155 | + self.display, | |
| 156 | + root, | |
| 157 | + xlib.False, | |
| 158 | + xlib.c.StructureNotifyMask, | |
| 159 | + @ptrCast(&ev), | |
| 160 | + ); | |
| 161 | + } | |
| 162 | + | |
| 163 | + fn sendXembedMessage(self: *Systray, win: xlib.Window, msg: c_long, detail: c_long, data1: c_long, data2: c_long) void { | |
| 164 | + var ev: xlib.c.XClientMessageEvent = undefined; | |
| 165 | + ev.type = xlib.ClientMessage; | |
| 166 | + ev.window = win; | |
| 167 | + ev.message_type = self.xembed; | |
| 168 | + ev.format = 32; | |
| 169 | + ev.data.l[0] = xlib.CurrentTime; | |
| 170 | + ev.data.l[1] = msg; | |
| 171 | + ev.data.l[2] = detail; | |
| 172 | + ev.data.l[3] = data1; | |
| 173 | + ev.data.l[4] = data2; | |
| 174 | + | |
| 175 | + _ = xlib.XSendEvent( | |
| 176 | + self.display, | |
| 177 | + win, | |
| 178 | + xlib.False, | |
| 179 | + xlib.c.NoEventMask, | |
| 180 | + @ptrCast(&ev), | |
| 181 | + ); | |
| 182 | + } | |
| 183 | + | |
| 184 | + fn sendConfigureNotify(self: *Systray, win: xlib.Window, x: i32, y: i32, w: i32, h: i32) void { | |
| 185 | + var ev: xlib.c.XConfigureEvent = undefined; | |
| 186 | + ev.type = xlib.ConfigureNotify; | |
| 187 | + ev.event = win; | |
| 188 | + ev.window = win; | |
| 189 | + ev.x = x; | |
| 190 | + ev.y = y; | |
| 191 | + ev.width = w; | |
| 192 | + ev.height = h; | |
| 193 | + ev.border_width = 0; | |
| 194 | + ev.above = xlib.None; | |
| 195 | + ev.override_redirect = xlib.False; | |
| 196 | + | |
| 197 | + _ = xlib.XSendEvent( | |
| 198 | + self.display, | |
| 199 | + win, | |
| 200 | + xlib.False, | |
| 201 | + xlib.c.StructureNotifyMask, | |
| 202 | + @ptrCast(&ev), | |
| 203 | + ); | |
| 204 | + } | |
| 205 | + | |
| 206 | + fn getXembedInfo(self: *Systray, win: xlib.Window) ?struct { version: c_ulong, flags: c_ulong } { | |
| 207 | + var actual_type: xlib.Atom = 0; | |
| 208 | + var actual_format: c_int = 0; | |
| 209 | + var nitems: c_ulong = 0; | |
| 210 | + var bytes_after: c_ulong = 0; | |
| 211 | + var data: [*c]u8 = null; | |
| 212 | + | |
| 213 | + const result = xlib.XGetWindowProperty( | |
| 214 | + self.display, | |
| 215 | + win, | |
| 216 | + self.xembed_info, | |
| 217 | + 0, | |
| 218 | + 2, | |
| 219 | + xlib.False, | |
| 220 | + self.xembed_info, | |
| 221 | + &actual_type, | |
| 222 | + &actual_format, | |
| 223 | + &nitems, | |
| 224 | + &bytes_after, | |
| 225 | + &data, | |
| 226 | + ); | |
| 227 | + | |
| 228 | + if (result != 0 or actual_type != self.xembed_info or actual_format != 32 or nitems < 2) { | |
| 229 | + if (data != null) _ = xlib.XFree(data); | |
| 230 | + return null; | |
| 231 | + } | |
| 232 | + | |
| 233 | + const info_ptr: [*]c_ulong = @ptrCast(@alignCast(data)); | |
| 234 | + const version = info_ptr[0]; | |
| 235 | + const flags = info_ptr[1]; | |
| 236 | + _ = xlib.XFree(data); | |
| 237 | + | |
| 238 | + return .{ .version = version, .flags = flags }; | |
| 239 | + } | |
| 240 | + | |
| 241 | + pub fn handleClientMessage(self: *Systray, ev: *xlib.XClientMessageEvent) bool { | |
| 242 | + if (ev.message_type != self.net_system_tray_opcode) return false; | |
| 243 | + | |
| 244 | + const opcode = ev.data.l[1]; | |
| 245 | + if (opcode == SYSTEM_TRAY_REQUEST_DOCK) { | |
| 246 | + const icon_win: xlib.Window = @intCast(ev.data.l[2]); | |
| 247 | + self.dockIcon(icon_win); | |
| 248 | + return true; | |
| 249 | + } | |
| 250 | + | |
| 251 | + return false; | |
| 252 | + } | |
| 253 | + | |
| 254 | + fn updateIconGeometry(self: *Systray, icon: *Icon, w: i32, h: i32) void { | |
| 255 | + _ = w; | |
| 256 | + _ = h; | |
| 257 | + icon.width = self.icon_size; | |
| 258 | + icon.height = self.icon_size; | |
| 259 | + | |
| 260 | + _ = xlib.c.XResizeWindow( | |
| 261 | + self.display, | |
| 262 | + icon.window, | |
| 263 | + @intCast(icon.width), | |
| 264 | + @intCast(icon.height), | |
| 265 | + ); | |
| 266 | + } | |
| 267 | + | |
| 268 | + fn dockIcon(self: *Systray, icon_win: xlib.Window) void { | |
| 269 | + for (self.icons.items) |icon| { | |
| 270 | + if (icon.window == icon_win) return; | |
| 271 | + } | |
| 272 | + | |
| 273 | + var wa: xlib.XWindowAttributes = undefined; | |
| 274 | + if (xlib.XGetWindowAttributes(self.display, icon_win, &wa) == 0) { | |
| 275 | + std.debug.print("systray: failed to get window attributes for icon 0x{x}\n", .{icon_win}); | |
| 276 | + return; | |
| 277 | + } | |
| 278 | + | |
| 279 | + _ = xlib.c.XSetWindowBorderWidth(self.display, icon_win, 0); | |
| 280 | + | |
| 281 | + _ = xlib.c.XSelectInput( | |
| 282 | + self.display, | |
| 283 | + icon_win, | |
| 284 | + xlib.c.StructureNotifyMask | xlib.c.PropertyChangeMask | xlib.c.ResizeRedirectMask, | |
| 285 | + ); | |
| 286 | + | |
| 287 | + _ = xlib.c.XReparentWindow( | |
| 288 | + self.display, | |
| 289 | + icon_win, | |
| 290 | + self.window, | |
| 291 | + 0, | |
| 292 | + 0, | |
| 293 | + ); | |
| 294 | + | |
| 295 | + const info = self.getXembedInfo(icon_win); | |
| 296 | + const mapped = if (info) |i| (i.flags & XEMBED_MAPPED) != 0 else true; | |
| 297 | + | |
| 298 | + var icon = Icon{ | |
| 299 | + .window = icon_win, | |
| 300 | + .width = self.icon_size, | |
| 301 | + .height = self.icon_size, | |
| 302 | + .mapped = mapped, | |
| 303 | + }; | |
| 304 | + | |
| 305 | + self.updateIconGeometry(&icon, wa.width, wa.height); | |
| 306 | + | |
| 307 | + self.icons.append(self.allocator, icon) catch return; | |
| 308 | + | |
| 309 | + self.sendXembedMessage(icon_win, XEMBED_EMBEDDED_NOTIFY, 0, @intCast(self.window), XEMBED_VERSION); | |
| 310 | + self.sendXembedMessage(icon_win, XEMBED_WINDOW_ACTIVATE, 0, 0, 0); | |
| 311 | + self.sendXembedMessage(icon_win, XEMBED_FOCUS_IN, 0, 0, 0); | |
| 312 | + | |
| 313 | + if (mapped) { | |
| 314 | + _ = xlib.XMapWindow(self.display, icon_win); | |
| 315 | + } | |
| 316 | + | |
| 317 | + self.arrangeIcons(); | |
| 318 | + std.debug.print("systray: docked icon 0x{x} (size {}x{})\n", .{ icon_win, icon.width, icon.height }); | |
| 319 | + } | |
| 320 | + | |
| 321 | + pub fn handleConfigureRequest(self: *Systray, ev: *xlib.XConfigureRequestEvent) bool { | |
| 322 | + for (self.icons.items) |*icon| { | |
| 323 | + if (icon.window == ev.window) { | |
| 324 | + if ((ev.value_mask & xlib.c.CWWidth) != 0 or (ev.value_mask & xlib.c.CWHeight) != 0) { | |
| 325 | + self.updateIconGeometry(icon, ev.width, ev.height); | |
| 326 | + self.arrangeIcons(); | |
| 327 | + } | |
| 328 | + return true; | |
| 329 | + } | |
| 330 | + } | |
| 331 | + return false; | |
| 332 | + } | |
| 333 | + | |
| 334 | + pub fn handleResizeRequest(self: *Systray, win: xlib.Window, w: c_int, h: c_int) bool { | |
| 335 | + for (self.icons.items) |*icon| { | |
| 336 | + if (icon.window == win) { | |
| 337 | + self.updateIconGeometry(icon, w, h); | |
| 338 | + self.arrangeIcons(); | |
| 339 | + return true; | |
| 340 | + } | |
| 341 | + } | |
| 342 | + return false; | |
| 343 | + } | |
| 344 | + | |
| 345 | + pub fn handleDestroyNotify(self: *Systray, win: xlib.Window) bool { | |
| 346 | + return self.removeIcon(win); | |
| 347 | + } | |
| 348 | + | |
| 349 | + pub fn handleUnmapNotify(self: *Systray, win: xlib.Window) bool { | |
| 350 | + for (self.icons.items) |*icon| { | |
| 351 | + if (icon.window == win) { | |
| 352 | + icon.mapped = false; | |
| 353 | + self.arrangeIcons(); | |
| 354 | + return true; | |
| 355 | + } | |
| 356 | + } | |
| 357 | + return false; | |
| 358 | + } | |
| 359 | + | |
| 360 | + pub fn handleMapNotify(self: *Systray, win: xlib.Window) bool { | |
| 361 | + for (self.icons.items) |*icon| { | |
| 362 | + if (icon.window == win) { | |
| 363 | + icon.mapped = true; | |
| 364 | + self.arrangeIcons(); | |
| 365 | + return true; | |
| 366 | + } | |
| 367 | + } | |
| 368 | + return false; | |
| 369 | + } | |
| 370 | + | |
| 371 | + pub fn handlePropertyNotify(self: *Systray, ev: *xlib.XPropertyEvent) bool { | |
| 372 | + if (ev.atom != self.xembed_info) return false; | |
| 373 | + | |
| 374 | + for (self.icons.items) |*icon| { | |
| 375 | + if (icon.window == ev.window) { | |
| 376 | + const info = self.getXembedInfo(icon.window) orelse return true; | |
| 377 | + const should_map = (info.flags & XEMBED_MAPPED) != 0; | |
| 378 | + | |
| 379 | + if (should_map and !icon.mapped) { | |
| 380 | + icon.mapped = true; | |
| 381 | + _ = xlib.XMapWindow(self.display, icon.window); | |
| 382 | + self.arrangeIcons(); | |
| 383 | + } else if (!should_map and icon.mapped) { | |
| 384 | + icon.mapped = false; | |
| 385 | + _ = xlib.c.XUnmapWindow(self.display, icon.window); | |
| 386 | + self.arrangeIcons(); | |
| 387 | + } | |
| 388 | + return true; | |
| 389 | + } | |
| 390 | + } | |
| 391 | + return false; | |
| 392 | + } | |
| 393 | + | |
| 394 | + pub fn handleReparentNotify(self: *Systray, win: xlib.Window, parent: xlib.Window) bool { | |
| 395 | + if (parent != self.window) { | |
| 396 | + return self.removeIcon(win); | |
| 397 | + } | |
| 398 | + return false; | |
| 399 | + } | |
| 400 | + | |
| 401 | + fn removeIcon(self: *Systray, win: xlib.Window) bool { | |
| 402 | + for (self.icons.items, 0..) |icon, i| { | |
| 403 | + if (icon.window == win) { | |
| 404 | + _ = self.icons.orderedRemove(i); | |
| 405 | + self.arrangeIcons(); | |
| 406 | + std.debug.print("systray: removed icon 0x{x}\n", .{win}); | |
| 407 | + return true; | |
| 408 | + } | |
| 409 | + } | |
| 410 | + return false; | |
| 411 | + } | |
| 412 | + | |
| 413 | + fn arrangeIcons(self: *Systray) void { | |
| 414 | + const spacing: i32 = 4; | |
| 415 | + var x: i32 = 0; | |
| 416 | + const y: i32 = @divTrunc(self.bar_height - self.icon_size, 2); | |
| 417 | + | |
| 418 | + for (self.icons.items) |icon| { | |
| 419 | + if (icon.mapped) { | |
| 420 | + _ = xlib.XMoveWindow(self.display, icon.window, x, y); | |
| 421 | + self.sendConfigureNotify(icon.window, x, y, icon.width, icon.height); | |
| 422 | + x += icon.width + spacing; | |
| 423 | + } | |
| 424 | + } | |
| 425 | + | |
| 426 | + const total_width: u32 = if (x > 0) @intCast(x) else 1; | |
| 427 | + _ = xlib.c.XResizeWindow(self.display, self.window, total_width, @intCast(self.bar_height)); | |
| 428 | + _ = xlib.XSync(self.display, xlib.False); | |
| 429 | + } | |
| 430 | + | |
| 431 | + pub fn width(self: *const Systray) i32 { | |
| 432 | + const spacing: i32 = 4; | |
| 433 | + var w: i32 = 0; | |
| 434 | + for (self.icons.items) |icon| { | |
| 435 | + if (icon.mapped) { | |
| 436 | + w += icon.width + spacing; | |
| 437 | + } | |
| 438 | + } | |
| 439 | + return w; | |
| 440 | + } | |
| 441 | + | |
| 442 | + pub fn updatePosition(self: *Systray, x: i32, y: i32) void { | |
| 443 | + _ = xlib.XMoveWindow(self.display, self.window, x, y); | |
| 444 | + } | |
| 445 | + | |
| 446 | + pub fn isIconWindow(self: *const Systray, win: xlib.Window) bool { | |
| 447 | + if (win == self.window) return true; | |
| 448 | + for (self.icons.items) |icon| { | |
| 449 | + if (icon.window == win) return true; | |
| 450 | + } | |
| 451 | + return false; | |
| 452 | + } | |
| 453 | +}; |