| 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 - 8; |
| 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 | allocator.destroy(self); |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | self.window = xlib.c.XCreateSimpleWindow( |
| 80 | display, |
| 81 | bar_win, |
| 82 | 0, |
| 83 | 0, |
| 84 | 1, |
| 85 | @intCast(bar_height - 2), |
| 86 | 0, |
| 87 | 0, |
| 88 | 0, |
| 89 | ); |
| 90 | |
| 91 | var attrs: xlib.c.XSetWindowAttributes = undefined; |
| 92 | attrs.event_mask = xlib.c.SubstructureNotifyMask | xlib.c.SubstructureRedirectMask | xlib.c.StructureNotifyMask | xlib.c.ExposureMask; |
| 93 | attrs.override_redirect = xlib.True; |
| 94 | attrs.background_pixel = background; |
| 95 | _ = xlib.c.XChangeWindowAttributes( |
| 96 | display, |
| 97 | self.window, |
| 98 | xlib.c.CWEventMask | xlib.c.CWOverrideRedirect | xlib.c.CWBackPixel, |
| 99 | &attrs, |
| 100 | ); |
| 101 | |
| 102 | _ = xlib.c.XSetSelectionOwner(display, self.manager_atom, self.window, xlib.CurrentTime); |
| 103 | if (xlib.c.XGetSelectionOwner(display, self.manager_atom) != self.window) { |
| 104 | _ = xlib.c.XDestroyWindow(display, self.window); |
| 105 | allocator.destroy(self); |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | const orientation: c_long = 0; |
| 110 | _ = xlib.XChangeProperty( |
| 111 | display, |
| 112 | self.window, |
| 113 | self.net_system_tray_orientation, |
| 114 | xlib.XA_CARDINAL, |
| 115 | 32, |
| 116 | xlib.PropModeReplace, |
| 117 | @ptrCast(&orientation), |
| 118 | 1, |
| 119 | ); |
| 120 | |
| 121 | self.sendManagerMessage(root); |
| 122 | |
| 123 | _ = xlib.XMapWindow(display, self.window); |
| 124 | _ = xlib.XSync(display, xlib.False); |
| 125 | |
| 126 | return self; |
| 127 | } |
| 128 | |
| 129 | pub fn deinit(self: *Systray) void { |
| 130 | for (self.icons.items) |icon| { |
| 131 | _ = xlib.c.XUnmapWindow(self.display, icon.window); |
| 132 | _ = xlib.c.XReparentWindow(self.display, icon.window, xlib.XRootWindow(self.display, self.screen), 0, 0); |
| 133 | } |
| 134 | self.icons.deinit(self.allocator); |
| 135 | _ = xlib.c.XDestroyWindow(self.display, self.window); |
| 136 | self.allocator.destroy(self); |
| 137 | } |
| 138 | |
| 139 | fn sendManagerMessage(self: *Systray, root: xlib.Window) void { |
| 140 | var ev: xlib.c.XClientMessageEvent = undefined; |
| 141 | ev.type = xlib.ClientMessage; |
| 142 | ev.window = root; |
| 143 | ev.message_type = xlib.XInternAtom(self.display, "MANAGER", xlib.False); |
| 144 | ev.format = 32; |
| 145 | ev.data.l[0] = xlib.CurrentTime; |
| 146 | ev.data.l[1] = @intCast(self.manager_atom); |
| 147 | ev.data.l[2] = @intCast(self.window); |
| 148 | ev.data.l[3] = 0; |
| 149 | ev.data.l[4] = 0; |
| 150 | |
| 151 | _ = xlib.XSendEvent( |
| 152 | self.display, |
| 153 | root, |
| 154 | xlib.False, |
| 155 | xlib.c.StructureNotifyMask, |
| 156 | @ptrCast(&ev), |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | fn sendXembedMessage(self: *Systray, win: xlib.Window, msg: c_long, detail: c_long, data1: c_long, data2: c_long) void { |
| 161 | var ev: xlib.c.XClientMessageEvent = undefined; |
| 162 | ev.type = xlib.ClientMessage; |
| 163 | ev.window = win; |
| 164 | ev.message_type = self.xembed; |
| 165 | ev.format = 32; |
| 166 | ev.data.l[0] = xlib.CurrentTime; |
| 167 | ev.data.l[1] = msg; |
| 168 | ev.data.l[2] = detail; |
| 169 | ev.data.l[3] = data1; |
| 170 | ev.data.l[4] = data2; |
| 171 | |
| 172 | _ = xlib.XSendEvent( |
| 173 | self.display, |
| 174 | win, |
| 175 | xlib.False, |
| 176 | xlib.c.NoEventMask, |
| 177 | @ptrCast(&ev), |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | fn sendConfigureNotify(self: *Systray, win: xlib.Window, x: i32, y: i32, w: i32, h: i32) void { |
| 182 | var ev: xlib.c.XConfigureEvent = undefined; |
| 183 | ev.type = xlib.ConfigureNotify; |
| 184 | ev.event = win; |
| 185 | ev.window = win; |
| 186 | ev.x = x; |
| 187 | ev.y = y; |
| 188 | ev.width = w; |
| 189 | ev.height = h; |
| 190 | ev.border_width = 0; |
| 191 | ev.above = xlib.None; |
| 192 | ev.override_redirect = xlib.False; |
| 193 | |
| 194 | _ = xlib.XSendEvent( |
| 195 | self.display, |
| 196 | win, |
| 197 | xlib.False, |
| 198 | xlib.c.StructureNotifyMask, |
| 199 | @ptrCast(&ev), |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | fn getXembedInfo(self: *Systray, win: xlib.Window) ?struct { version: c_ulong, flags: c_ulong } { |
| 204 | var actual_type: xlib.Atom = 0; |
| 205 | var actual_format: c_int = 0; |
| 206 | var nitems: c_ulong = 0; |
| 207 | var bytes_after: c_ulong = 0; |
| 208 | var data: [*c]u8 = null; |
| 209 | |
| 210 | const result = xlib.XGetWindowProperty( |
| 211 | self.display, |
| 212 | win, |
| 213 | self.xembed_info, |
| 214 | 0, |
| 215 | 2, |
| 216 | xlib.False, |
| 217 | self.xembed_info, |
| 218 | &actual_type, |
| 219 | &actual_format, |
| 220 | &nitems, |
| 221 | &bytes_after, |
| 222 | &data, |
| 223 | ); |
| 224 | |
| 225 | if (result != 0 or actual_type != self.xembed_info or actual_format != 32 or nitems < 2) { |
| 226 | if (data != null) _ = xlib.XFree(data); |
| 227 | return null; |
| 228 | } |
| 229 | |
| 230 | const info_ptr: [*]c_ulong = @ptrCast(@alignCast(data)); |
| 231 | const version = info_ptr[0]; |
| 232 | const flags = info_ptr[1]; |
| 233 | _ = xlib.XFree(data); |
| 234 | |
| 235 | return .{ .version = version, .flags = flags }; |
| 236 | } |
| 237 | |
| 238 | pub fn handleClientMessage(self: *Systray, ev: *xlib.XClientMessageEvent) bool { |
| 239 | if (ev.message_type != self.net_system_tray_opcode) return false; |
| 240 | |
| 241 | const opcode = ev.data.l[1]; |
| 242 | if (opcode == SYSTEM_TRAY_REQUEST_DOCK) { |
| 243 | const icon_win: xlib.Window = @intCast(ev.data.l[2]); |
| 244 | self.dockIcon(icon_win); |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | fn updateIconGeometry(self: *Systray, icon: *Icon, w: i32, h: i32) void { |
| 252 | _ = w; |
| 253 | _ = h; |
| 254 | icon.width = self.icon_size; |
| 255 | icon.height = self.icon_size; |
| 256 | |
| 257 | _ = xlib.c.XResizeWindow( |
| 258 | self.display, |
| 259 | icon.window, |
| 260 | @intCast(icon.width), |
| 261 | @intCast(icon.height), |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | fn dockIcon(self: *Systray, icon_win: xlib.Window) void { |
| 266 | for (self.icons.items) |icon| { |
| 267 | if (icon.window == icon_win) return; |
| 268 | } |
| 269 | |
| 270 | var wa: xlib.XWindowAttributes = undefined; |
| 271 | if (xlib.XGetWindowAttributes(self.display, icon_win, &wa) == 0) { |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | _ = xlib.c.XSetWindowBorderWidth(self.display, icon_win, 0); |
| 276 | |
| 277 | _ = xlib.c.XSelectInput( |
| 278 | self.display, |
| 279 | icon_win, |
| 280 | xlib.c.StructureNotifyMask | xlib.c.PropertyChangeMask | xlib.c.ResizeRedirectMask, |
| 281 | ); |
| 282 | |
| 283 | _ = xlib.c.XReparentWindow( |
| 284 | self.display, |
| 285 | icon_win, |
| 286 | self.window, |
| 287 | 0, |
| 288 | 0, |
| 289 | ); |
| 290 | |
| 291 | const info = self.getXembedInfo(icon_win); |
| 292 | const mapped = if (info) |i| (i.flags & XEMBED_MAPPED) != 0 else true; |
| 293 | |
| 294 | var icon = Icon{ |
| 295 | .window = icon_win, |
| 296 | .width = self.icon_size, |
| 297 | .height = self.icon_size, |
| 298 | .mapped = mapped, |
| 299 | }; |
| 300 | |
| 301 | self.updateIconGeometry(&icon, wa.width, wa.height); |
| 302 | |
| 303 | self.icons.append(self.allocator, icon) catch return; |
| 304 | |
| 305 | self.sendXembedMessage(icon_win, XEMBED_EMBEDDED_NOTIFY, 0, @intCast(self.window), XEMBED_VERSION); |
| 306 | self.sendXembedMessage(icon_win, XEMBED_WINDOW_ACTIVATE, 0, 0, 0); |
| 307 | self.sendXembedMessage(icon_win, XEMBED_FOCUS_IN, 0, 0, 0); |
| 308 | |
| 309 | if (mapped) { |
| 310 | _ = xlib.XMapWindow(self.display, icon_win); |
| 311 | } |
| 312 | |
| 313 | self.arrangeIcons(); |
| 314 | } |
| 315 | |
| 316 | pub fn handleConfigureRequest(self: *Systray, ev: *xlib.XConfigureRequestEvent) bool { |
| 317 | for (self.icons.items) |*icon| { |
| 318 | if (icon.window == ev.window) { |
| 319 | if ((ev.value_mask & xlib.c.CWWidth) != 0 or (ev.value_mask & xlib.c.CWHeight) != 0) { |
| 320 | self.updateIconGeometry(icon, ev.width, ev.height); |
| 321 | self.arrangeIcons(); |
| 322 | } |
| 323 | return true; |
| 324 | } |
| 325 | } |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | pub fn handleResizeRequest(self: *Systray, win: xlib.Window, w: c_int, h: c_int) bool { |
| 330 | for (self.icons.items) |*icon| { |
| 331 | if (icon.window == win) { |
| 332 | self.updateIconGeometry(icon, w, h); |
| 333 | self.arrangeIcons(); |
| 334 | return true; |
| 335 | } |
| 336 | } |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | pub fn handleDestroyNotify(self: *Systray, win: xlib.Window) bool { |
| 341 | return self.removeIcon(win); |
| 342 | } |
| 343 | |
| 344 | pub fn handleUnmapNotify(self: *Systray, win: xlib.Window) bool { |
| 345 | for (self.icons.items) |*icon| { |
| 346 | if (icon.window == win) { |
| 347 | icon.mapped = false; |
| 348 | self.arrangeIcons(); |
| 349 | return true; |
| 350 | } |
| 351 | } |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | pub fn handleMapNotify(self: *Systray, win: xlib.Window) bool { |
| 356 | for (self.icons.items) |*icon| { |
| 357 | if (icon.window == win) { |
| 358 | icon.mapped = true; |
| 359 | self.arrangeIcons(); |
| 360 | return true; |
| 361 | } |
| 362 | } |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | pub fn handlePropertyNotify(self: *Systray, ev: *xlib.XPropertyEvent) bool { |
| 367 | if (ev.atom != self.xembed_info) return false; |
| 368 | |
| 369 | for (self.icons.items) |*icon| { |
| 370 | if (icon.window == ev.window) { |
| 371 | const info = self.getXembedInfo(icon.window) orelse return true; |
| 372 | const should_map = (info.flags & XEMBED_MAPPED) != 0; |
| 373 | |
| 374 | if (should_map and !icon.mapped) { |
| 375 | icon.mapped = true; |
| 376 | _ = xlib.XMapWindow(self.display, icon.window); |
| 377 | self.arrangeIcons(); |
| 378 | } else if (!should_map and icon.mapped) { |
| 379 | icon.mapped = false; |
| 380 | _ = xlib.c.XUnmapWindow(self.display, icon.window); |
| 381 | self.arrangeIcons(); |
| 382 | } |
| 383 | return true; |
| 384 | } |
| 385 | } |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | pub fn handleReparentNotify(self: *Systray, win: xlib.Window, parent: xlib.Window) bool { |
| 390 | if (parent != self.window) { |
| 391 | return self.removeIcon(win); |
| 392 | } |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | fn removeIcon(self: *Systray, win: xlib.Window) bool { |
| 397 | for (self.icons.items, 0..) |icon, i| { |
| 398 | if (icon.window == win) { |
| 399 | _ = self.icons.orderedRemove(i); |
| 400 | self.arrangeIcons(); |
| 401 | return true; |
| 402 | } |
| 403 | } |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | fn arrangeIcons(self: *Systray) void { |
| 408 | const spacing: i32 = 4; |
| 409 | var x: i32 = 0; |
| 410 | const y: i32 = @divTrunc(self.bar_height - self.icon_size, 2); |
| 411 | |
| 412 | for (self.icons.items) |icon| { |
| 413 | if (icon.mapped) { |
| 414 | _ = xlib.XMoveWindow(self.display, icon.window, x, y); |
| 415 | self.sendConfigureNotify(icon.window, x, y, icon.width, icon.height); |
| 416 | x += icon.width + spacing; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | const total_width: u32 = if (x > 0) @intCast(x) else 1; |
| 421 | _ = xlib.c.XResizeWindow(self.display, self.window, total_width, @intCast(self.bar_height - 2)); |
| 422 | _ = xlib.XSync(self.display, xlib.False); |
| 423 | } |
| 424 | |
| 425 | pub fn width(self: *const Systray) i32 { |
| 426 | const spacing: i32 = 4; |
| 427 | var w: i32 = 0; |
| 428 | for (self.icons.items) |icon| { |
| 429 | if (icon.mapped) { |
| 430 | w += icon.width + spacing; |
| 431 | } |
| 432 | } |
| 433 | return w; |
| 434 | } |
| 435 | |
| 436 | pub fn updatePosition(self: *Systray, x: i32, y: i32) void { |
| 437 | _ = xlib.XMoveWindow(self.display, self.window, x, y); |
| 438 | } |
| 439 | |
| 440 | pub fn isIconWindow(self: *const Systray, win: xlib.Window) bool { |
| 441 | if (win == self.window) return true; |
| 442 | for (self.icons.items) |icon| { |
| 443 | if (icon.window == win) return true; |
| 444 | } |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | pub fn handleButtonPress(self: *Systray, ev: *xlib.XButtonEvent) bool { |
| 449 | if (ev.window != self.window) { |
| 450 | for (self.icons.items) |icon| { |
| 451 | if (icon.window == ev.window) { |
| 452 | self.sendButtonEvent(icon.window, ev, true); |
| 453 | return true; |
| 454 | } |
| 455 | } |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | const spacing: i32 = 4; |
| 460 | var x: i32 = 0; |
| 461 | for (self.icons.items) |icon| { |
| 462 | if (icon.mapped) { |
| 463 | if (ev.x >= x and ev.x < x + icon.width) { |
| 464 | self.sendButtonEvent(icon.window, ev, true); |
| 465 | return true; |
| 466 | } |
| 467 | x += icon.width + spacing; |
| 468 | } |
| 469 | } |
| 470 | return false; |
| 471 | } |
| 472 | |
| 473 | pub fn handleButtonRelease(self: *Systray, ev: *xlib.XButtonEvent) bool { |
| 474 | if (ev.window != self.window) { |
| 475 | for (self.icons.items) |icon| { |
| 476 | if (icon.window == ev.window) { |
| 477 | self.sendButtonEvent(icon.window, ev, false); |
| 478 | return true; |
| 479 | } |
| 480 | } |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | const spacing: i32 = 4; |
| 485 | var x: i32 = 0; |
| 486 | for (self.icons.items) |icon| { |
| 487 | if (icon.mapped) { |
| 488 | if (ev.x >= x and ev.x < x + icon.width) { |
| 489 | self.sendButtonEvent(icon.window, ev, false); |
| 490 | return true; |
| 491 | } |
| 492 | x += icon.width + spacing; |
| 493 | } |
| 494 | } |
| 495 | return false; |
| 496 | } |
| 497 | |
| 498 | fn sendButtonEvent(self: *Systray, win: xlib.Window, ev: *xlib.XButtonEvent, is_press: bool) void { |
| 499 | var event: xlib.c.XButtonEvent = undefined; |
| 500 | event.type = if (is_press) xlib.ButtonPress else xlib.ButtonRelease; |
| 501 | event.window = win; |
| 502 | event.root = xlib.XRootWindow(self.display, self.screen); |
| 503 | event.subwindow = xlib.None; |
| 504 | event.time = ev.time; |
| 505 | event.x = ev.x; |
| 506 | event.y = ev.y; |
| 507 | event.x_root = ev.x_root; |
| 508 | event.y_root = ev.y_root; |
| 509 | event.state = ev.state; |
| 510 | event.button = ev.button; |
| 511 | event.same_screen = xlib.True; |
| 512 | |
| 513 | _ = xlib.XSendEvent( |
| 514 | self.display, |
| 515 | win, |
| 516 | xlib.False, |
| 517 | xlib.c.ButtonPressMask | xlib.c.ButtonReleaseMask, |
| 518 | @ptrCast(&event), |
| 519 | ); |
| 520 | _ = xlib.XSync(self.display, xlib.False); |
| 521 | } |
| 522 | }; |