diff options
author | Ian Fan <ianfan0@gmail.com> | 2018-12-17 14:08:16 +0000 |
---|---|---|
committer | Ian Fan <ianfan0@gmail.com> | 2018-12-31 20:40:18 +0000 |
commit | 371089f62c2894ff55478c2a4298505e3ed12f3f (patch) | |
tree | aa2029f0a2ecf39ee4016d2761b17b2b51dfd838 /swaybar | |
parent | swaybar: draw a sad face if SNI has no icon (diff) | |
download | sway-371089f62c2894ff55478c2a4298505e3ed12f3f.tar.gz sway-371089f62c2894ff55478c2a4298505e3ed12f3f.tar.zst sway-371089f62c2894ff55478c2a4298505e3ed12f3f.zip |
swaybar: handle new and lost StatusNotifierWatcher
Diffstat (limited to 'swaybar')
-rw-r--r-- | swaybar/tray/host.c | 32 | ||||
-rw-r--r-- | swaybar/tray/tray.c | 29 |
2 files changed, 60 insertions, 1 deletions
diff --git a/swaybar/tray/host.c b/swaybar/tray/host.c index c5756f17..30339fec 100644 --- a/swaybar/tray/host.c +++ b/swaybar/tray/host.c | |||
@@ -115,6 +115,25 @@ static bool register_to_watcher(struct swaybar_host *host) { | |||
115 | return ret >= 0; | 115 | return ret >= 0; |
116 | } | 116 | } |
117 | 117 | ||
118 | static int handle_new_watcher(sd_bus_message *msg, | ||
119 | void *data, sd_bus_error *error) { | ||
120 | char *service, *old_owner, *new_owner; | ||
121 | int ret = sd_bus_message_read(msg, "sss", &service, &old_owner, &new_owner); | ||
122 | if (ret < 0) { | ||
123 | wlr_log(WLR_ERROR, "Failed to parse owner change message: %s", strerror(-ret)); | ||
124 | return ret; | ||
125 | } | ||
126 | |||
127 | if (!*old_owner) { | ||
128 | struct swaybar_host *host = data; | ||
129 | if (strcmp(service, host->watcher_interface) == 0) { | ||
130 | register_to_watcher(host); | ||
131 | } | ||
132 | } | ||
133 | |||
134 | return 0; | ||
135 | } | ||
136 | |||
118 | bool init_host(struct swaybar_host *host, char *protocol, | 137 | bool init_host(struct swaybar_host *host, char *protocol, |
119 | struct swaybar_tray *tray) { | 138 | struct swaybar_tray *tray) { |
120 | size_t len = snprintf(NULL, 0, "org.%s.StatusNotifierWatcher", protocol) + 1; | 139 | size_t len = snprintf(NULL, 0, "org.%s.StatusNotifierWatcher", protocol) + 1; |
@@ -124,7 +143,7 @@ bool init_host(struct swaybar_host *host, char *protocol, | |||
124 | } | 143 | } |
125 | snprintf(host->watcher_interface, len, "org.%s.StatusNotifierWatcher", protocol); | 144 | snprintf(host->watcher_interface, len, "org.%s.StatusNotifierWatcher", protocol); |
126 | 145 | ||
127 | sd_bus_slot *reg_slot = NULL, *unreg_slot = NULL; | 146 | sd_bus_slot *reg_slot = NULL, *unreg_slot = NULL, *watcher_slot = NULL; |
128 | int ret = sd_bus_match_signal(tray->bus, ®_slot, host->watcher_interface, | 147 | int ret = sd_bus_match_signal(tray->bus, ®_slot, host->watcher_interface, |
129 | watcher_path, host->watcher_interface, | 148 | watcher_path, host->watcher_interface, |
130 | "StatusNotifierItemRegistered", handle_sni_registered, tray); | 149 | "StatusNotifierItemRegistered", handle_sni_registered, tray); |
@@ -142,6 +161,15 @@ bool init_host(struct swaybar_host *host, char *protocol, | |||
142 | goto error; | 161 | goto error; |
143 | } | 162 | } |
144 | 163 | ||
164 | ret = sd_bus_match_signal(tray->bus, &watcher_slot, "org.freedesktop.DBus", | ||
165 | "/org/freedesktop/DBus", "org.freedesktop.DBus", "NameOwnerChanged", | ||
166 | handle_new_watcher, host); | ||
167 | if (ret < 0) { | ||
168 | wlr_log(WLR_ERROR, "Failed to subscribe to unregistering events: %s", | ||
169 | strerror(-ret)); | ||
170 | goto error; | ||
171 | } | ||
172 | |||
145 | pid_t pid = getpid(); | 173 | pid_t pid = getpid(); |
146 | size_t service_len = snprintf(NULL, 0, "org.%s.StatusNotifierHost-%d", | 174 | size_t service_len = snprintf(NULL, 0, "org.%s.StatusNotifierHost-%d", |
147 | protocol, pid) + 1; | 175 | protocol, pid) + 1; |
@@ -163,12 +191,14 @@ bool init_host(struct swaybar_host *host, char *protocol, | |||
163 | 191 | ||
164 | sd_bus_slot_set_floating(reg_slot, 1); | 192 | sd_bus_slot_set_floating(reg_slot, 1); |
165 | sd_bus_slot_set_floating(unreg_slot, 1); | 193 | sd_bus_slot_set_floating(unreg_slot, 1); |
194 | sd_bus_slot_set_floating(watcher_slot, 1); | ||
166 | 195 | ||
167 | wlr_log(WLR_DEBUG, "Registered %s", host->service); | 196 | wlr_log(WLR_DEBUG, "Registered %s", host->service); |
168 | return true; | 197 | return true; |
169 | error: | 198 | error: |
170 | sd_bus_slot_unref(reg_slot); | 199 | sd_bus_slot_unref(reg_slot); |
171 | sd_bus_slot_unref(unreg_slot); | 200 | sd_bus_slot_unref(unreg_slot); |
201 | sd_bus_slot_unref(watcher_slot); | ||
172 | finish_host(host); | 202 | finish_host(host); |
173 | return false; | 203 | return false; |
174 | } | 204 | } |
diff --git a/swaybar/tray/tray.c b/swaybar/tray/tray.c index f186ed86..acc300af 100644 --- a/swaybar/tray/tray.c +++ b/swaybar/tray/tray.c | |||
@@ -12,6 +12,27 @@ | |||
12 | #include "list.h" | 12 | #include "list.h" |
13 | #include "log.h" | 13 | #include "log.h" |
14 | 14 | ||
15 | static int handle_lost_watcher(sd_bus_message *msg, | ||
16 | void *data, sd_bus_error *error) { | ||
17 | char *service, *old_owner, *new_owner; | ||
18 | int ret = sd_bus_message_read(msg, "sss", &service, &old_owner, &new_owner); | ||
19 | if (ret < 0) { | ||
20 | wlr_log(WLR_ERROR, "Failed to parse owner change message: %s", strerror(-ret)); | ||
21 | return ret; | ||
22 | } | ||
23 | |||
24 | if (!*new_owner) { | ||
25 | struct swaybar_tray *tray = data; | ||
26 | if (strcmp(service, "org.freedesktop.StatusNotifierWatcher") == 0) { | ||
27 | tray->watcher_xdg = create_watcher("freedesktop", tray->bus); | ||
28 | } else if (strcmp(service, "org.kde.StatusNotifierWatcher") == 0) { | ||
29 | tray->watcher_kde = create_watcher("kde", tray->bus); | ||
30 | } | ||
31 | } | ||
32 | |||
33 | return 0; | ||
34 | } | ||
35 | |||
15 | struct swaybar_tray *create_tray(struct swaybar *bar) { | 36 | struct swaybar_tray *create_tray(struct swaybar *bar) { |
16 | wlr_log(WLR_DEBUG, "Initializing tray"); | 37 | wlr_log(WLR_DEBUG, "Initializing tray"); |
17 | 38 | ||
@@ -33,6 +54,14 @@ struct swaybar_tray *create_tray(struct swaybar *bar) { | |||
33 | tray->watcher_xdg = create_watcher("freedesktop", tray->bus); | 54 | tray->watcher_xdg = create_watcher("freedesktop", tray->bus); |
34 | tray->watcher_kde = create_watcher("kde", tray->bus); | 55 | tray->watcher_kde = create_watcher("kde", tray->bus); |
35 | 56 | ||
57 | ret = sd_bus_match_signal(bus, NULL, "org.freedesktop.DBus", | ||
58 | "/org/freedesktop/DBus", "org.freedesktop.DBus", | ||
59 | "NameOwnerChanged", handle_lost_watcher, tray); | ||
60 | if (ret < 0) { | ||
61 | wlr_log(WLR_ERROR, "Failed to subscribe to unregistering events: %s", | ||
62 | strerror(-ret)); | ||
63 | } | ||
64 | |||
36 | tray->items = create_list(); | 65 | tray->items = create_list(); |
37 | 66 | ||
38 | init_host(&tray->host_xdg, "freedesktop", tray); | 67 | init_host(&tray->host_xdg, "freedesktop", tray); |