|
| 1 | +//go:build linux || freebsd || openbsd || netbsd || dragonfly || solaris || illumos || aix |
| 2 | + |
| 3 | +// Package x11tray provides system tray functionality for Unix platforms. |
| 4 | +// It handles StatusNotifierItem integration via DBus and manages the snixembed proxy |
| 5 | +// for compatibility with legacy X11 system trays. |
| 6 | +package x11tray |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "errors" |
| 11 | + "fmt" |
| 12 | + "log/slog" |
| 13 | + "os" |
| 14 | + "os/exec" |
| 15 | + "strings" |
| 16 | + "time" |
| 17 | + |
| 18 | + "github.com/godbus/dbus/v5" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + statusNotifierWatcher = "org.kde.StatusNotifierWatcher" |
| 23 | + statusNotifierWatcherPath = "/StatusNotifierWatcher" |
| 24 | +) |
| 25 | + |
| 26 | +// HealthCheck verifies that a system tray implementation is available via D-Bus. |
| 27 | +// It checks for the KDE StatusNotifierWatcher service which is required for |
| 28 | +// system tray icons on modern Linux desktops. |
| 29 | +// |
| 30 | +// Returns nil if a tray is available, or an error describing the issue. |
| 31 | +func HealthCheck() error { |
| 32 | + conn, err := dbus.ConnectSessionBus() |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("failed to connect to D-Bus session bus: %w", err) |
| 35 | + } |
| 36 | + defer func() { |
| 37 | + if err := conn.Close(); err != nil { |
| 38 | + slog.Debug("[X11TRAY] Failed to close DBus connection", "error", err) |
| 39 | + } |
| 40 | + }() |
| 41 | + |
| 42 | + // Check if the StatusNotifierWatcher service exists |
| 43 | + var names []string |
| 44 | + err = conn.BusObject().Call("org.freedesktop.DBus.ListNames", 0).Store(&names) |
| 45 | + if err != nil { |
| 46 | + return fmt.Errorf("failed to query D-Bus services: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + for _, name := range names { |
| 50 | + if name == statusNotifierWatcher { |
| 51 | + slog.Debug("[X11TRAY] StatusNotifierWatcher found", "service", statusNotifierWatcher) |
| 52 | + return nil |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return fmt.Errorf("no system tray found: %s service not available", statusNotifierWatcher) |
| 57 | +} |
| 58 | + |
| 59 | +// ProxyProcess represents a running snixembed background process. |
| 60 | +type ProxyProcess struct { |
| 61 | + cmd *exec.Cmd |
| 62 | + cancel context.CancelFunc |
| 63 | +} |
| 64 | + |
| 65 | +// Stop terminates the proxy process gracefully. |
| 66 | +func (p *ProxyProcess) Stop() error { |
| 67 | + if p.cancel != nil { |
| 68 | + p.cancel() |
| 69 | + } |
| 70 | + if p.cmd != nil && p.cmd.Process != nil { |
| 71 | + return p.cmd.Process.Kill() |
| 72 | + } |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +// TryProxy attempts to start snixembed as a system tray proxy service. |
| 77 | +// snixembed bridges legacy X11 system trays to modern StatusNotifier-based trays. |
| 78 | +// |
| 79 | +// If successful, returns a ProxyProcess that should be stopped when the application exits. |
| 80 | +// Returns an error if snixembed is not found or fails to start successfully. |
| 81 | +func TryProxy(ctx context.Context) (*ProxyProcess, error) { |
| 82 | + // Check if snixembed is available |
| 83 | + snixembedPath, err := exec.LookPath("snixembed") |
| 84 | + if err != nil { |
| 85 | + return nil, errors.New( |
| 86 | + "snixembed not found in PATH: install it with your package manager " + |
| 87 | + "(e.g., 'apt install snixembed' or 'yay -S snixembed')") |
| 88 | + } |
| 89 | + |
| 90 | + slog.Info("[X11TRAY] Starting snixembed proxy", "path", snixembedPath) |
| 91 | + |
| 92 | + // Create a cancellable context for the proxy process |
| 93 | + proxyCtx, cancel := context.WithCancel(ctx) |
| 94 | + |
| 95 | + // Start snixembed in the background |
| 96 | + cmd := exec.CommandContext(proxyCtx, snixembedPath) |
| 97 | + |
| 98 | + // Capture output for debugging |
| 99 | + if err := cmd.Start(); err != nil { |
| 100 | + cancel() |
| 101 | + return nil, fmt.Errorf("failed to start snixembed: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + proxy := &ProxyProcess{ |
| 105 | + cmd: cmd, |
| 106 | + cancel: cancel, |
| 107 | + } |
| 108 | + |
| 109 | + // Give snixembed time to register with D-Bus |
| 110 | + // This is necessary because the service takes a moment to become available |
| 111 | + time.Sleep(500 * time.Millisecond) |
| 112 | + |
| 113 | + // Verify that the proxy worked by checking again |
| 114 | + if err := HealthCheck(); err != nil { |
| 115 | + // snixembed started but didn't fix the problem |
| 116 | + if stopErr := proxy.Stop(); stopErr != nil { |
| 117 | + slog.Debug("[X11TRAY] Failed to stop proxy after failed health check", "error", stopErr) |
| 118 | + } |
| 119 | + return nil, fmt.Errorf("snixembed started but system tray still unavailable: %w", err) |
| 120 | + } |
| 121 | + |
| 122 | + slog.Info("[X11TRAY] snixembed proxy started successfully") |
| 123 | + return proxy, nil |
| 124 | +} |
| 125 | + |
| 126 | +// EnsureTray checks for system tray availability and attempts to start a proxy if needed. |
| 127 | +// This is a convenience function that combines HealthCheck and TryProxy. |
| 128 | +// |
| 129 | +// Returns a ProxyProcess if one was started (caller must Stop() it on exit), or nil if |
| 130 | +// the native tray was available. Returns an error if no tray solution could be found. |
| 131 | +func EnsureTray(ctx context.Context) (*ProxyProcess, error) { |
| 132 | + // First, check if we already have a working tray |
| 133 | + if err := HealthCheck(); err == nil { |
| 134 | + slog.Debug("[X11TRAY] Native system tray available") |
| 135 | + // No proxy needed (nil) and no error (nil) - native tray is working |
| 136 | + return nil, nil //nolint:nilnil // nil proxy is valid when native tray exists |
| 137 | + } |
| 138 | + |
| 139 | + slog.Warn("[X11TRAY] No native system tray found, attempting to start proxy") |
| 140 | + |
| 141 | + // Try to start the proxy |
| 142 | + proxy, err := TryProxy(ctx) |
| 143 | + if err != nil { |
| 144 | + return nil, fmt.Errorf("system tray unavailable and proxy failed: %w", err) |
| 145 | + } |
| 146 | + |
| 147 | + return proxy, nil |
| 148 | +} |
| 149 | + |
| 150 | +// ShowContextMenu triggers the context menu via DBus on Unix platforms. |
| 151 | +// On Linux/FreeBSD with StatusNotifierItem, the menu parameter in click handlers is nil, |
| 152 | +// so we need to manually call the ContextMenu method via DBus to show the menu. |
| 153 | +func ShowContextMenu() { |
| 154 | + conn, err := dbus.ConnectSessionBus() |
| 155 | + if err != nil { |
| 156 | + slog.Warn("[X11TRAY] Failed to connect to session bus", "error", err) |
| 157 | + return |
| 158 | + } |
| 159 | + defer func() { |
| 160 | + if err := conn.Close(); err != nil { |
| 161 | + slog.Debug("[X11TRAY] Failed to close DBus connection", "error", err) |
| 162 | + } |
| 163 | + }() |
| 164 | + |
| 165 | + // Find our StatusNotifierItem service |
| 166 | + var names []string |
| 167 | + err = conn.BusObject().Call("org.freedesktop.DBus.ListNames", 0).Store(&names) |
| 168 | + if err != nil { |
| 169 | + slog.Warn("[X11TRAY] Failed to list DBus names", "error", err) |
| 170 | + return |
| 171 | + } |
| 172 | + |
| 173 | + // Find the StatusNotifierItem service for this process |
| 174 | + var serviceName string |
| 175 | + pid := os.Getpid() |
| 176 | + expectedPrefix := fmt.Sprintf("org.kde.StatusNotifierItem-%d-", pid) |
| 177 | + for _, name := range names { |
| 178 | + if strings.HasPrefix(name, expectedPrefix) { |
| 179 | + serviceName = name |
| 180 | + break |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if serviceName == "" { |
| 185 | + slog.Warn("[X11TRAY] StatusNotifierItem service not found", "pid", pid, "expectedPrefix", expectedPrefix) |
| 186 | + return |
| 187 | + } |
| 188 | + |
| 189 | + slog.Info("[X11TRAY] Attempting to trigger context menu", "service", serviceName) |
| 190 | + |
| 191 | + // Try different methods to trigger the menu display |
| 192 | + obj := conn.Object(serviceName, "/StatusNotifierItem") |
| 193 | + |
| 194 | + // First try: Call ContextMenu method (standard StatusNotifierItem) |
| 195 | + call := obj.Call("org.kde.StatusNotifierItem.ContextMenu", 0, int32(0), int32(0)) |
| 196 | + if call.Err != nil { |
| 197 | + slog.Info("[X11TRAY] ContextMenu method failed, trying SecondaryActivate", "error", call.Err) |
| 198 | + |
| 199 | + // Second try: Call SecondaryActivate (right-click equivalent) |
| 200 | + call = obj.Call("org.kde.StatusNotifierItem.SecondaryActivate", 0, int32(0), int32(0)) |
| 201 | + if call.Err != nil { |
| 202 | + slog.Warn("[X11TRAY] Both ContextMenu and SecondaryActivate failed", "contextMenuErr", call.Err) |
| 203 | + slog.Info("[X11TRAY] Note: Menu should still work with right-click via snixembed") |
| 204 | + return |
| 205 | + } |
| 206 | + slog.Info("[X11TRAY] Successfully triggered SecondaryActivate") |
| 207 | + return |
| 208 | + } |
| 209 | + |
| 210 | + slog.Info("[X11TRAY] Successfully triggered ContextMenu") |
| 211 | +} |
0 commit comments