aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar
diff options
context:
space:
mode:
authorLibravatar Ian Fan <ianfan0@gmail.com>2018-12-07 12:33:45 +0000
committerLibravatar Ian Fan <ianfan0@gmail.com>2018-12-31 20:40:18 +0000
commite6cb55e2f8176b0ea8a6dbf15c728c56d8b74056 (patch)
tree5de223965d04be7bf2835e0ca391426185494261 /swaybar
parentswaybar: implement icon themes and lookup for tray (diff)
downloadsway-e6cb55e2f8176b0ea8a6dbf15c728c56d8b74056.tar.gz
sway-e6cb55e2f8176b0ea8a6dbf15c728c56d8b74056.tar.zst
sway-e6cb55e2f8176b0ea8a6dbf15c728c56d8b74056.zip
swaybar: add StatusNotifierHost to tray
Diffstat (limited to 'swaybar')
-rw-r--r--swaybar/meson.build1
-rw-r--r--swaybar/tray/host.c177
-rw-r--r--swaybar/tray/tray.c13
3 files changed, 191 insertions, 0 deletions
diff --git a/swaybar/meson.build b/swaybar/meson.build
index b8ed2c23..85783a0f 100644
--- a/swaybar/meson.build
+++ b/swaybar/meson.build
@@ -1,4 +1,5 @@
1tray_files = get_option('enable-tray') ? [ 1tray_files = get_option('enable-tray') ? [
2 'tray/host.c',
2 'tray/icon.c', 3 'tray/icon.c',
3 'tray/tray.c', 4 'tray/tray.c',
4 'tray/watcher.c' 5 'tray/watcher.c'
diff --git a/swaybar/tray/host.c b/swaybar/tray/host.c
new file mode 100644
index 00000000..3cc90254
--- /dev/null
+++ b/swaybar/tray/host.c
@@ -0,0 +1,177 @@
1#define _POSIX_C_SOURCE 200809L
2#include <stdbool.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <unistd.h>
7#include "swaybar/tray/host.h"
8#include "swaybar/tray/tray.h"
9#include "list.h"
10#include "log.h"
11
12static const char *watcher_path = "/StatusNotifierWatcher";
13
14static int cmp_sni_id(const void *item, const void *cmp_to) {
15 const char *sni = item;
16 return strcmp(sni, cmp_to);
17}
18
19static void add_sni(struct swaybar_tray *tray, char *id) {
20 int idx = list_seq_find(tray->items, cmp_sni_id, id);
21 if (idx == -1) {
22 wlr_log(WLR_DEBUG, "Registering Status Notifier Item '%s'", id);
23 char *sni = strdup(id);
24 if (sni) {
25 list_add(tray->items, sni);
26 }
27 }
28}
29
30static int handle_sni_registered(sd_bus_message *msg, void *data,
31 sd_bus_error *error) {
32 char *id;
33 int ret = sd_bus_message_read(msg, "s", &id);
34 if (ret < 0) {
35 wlr_log(WLR_ERROR, "Failed to parse register SNI message: %s", strerror(-ret));
36 }
37
38 struct swaybar_tray *tray = data;
39 add_sni(tray, id);
40
41 return ret;
42}
43
44static int handle_sni_unregistered(sd_bus_message *msg, void *data,
45 sd_bus_error *error) {
46 char *id;
47 int ret = sd_bus_message_read(msg, "s", &id);
48 if (ret < 0) {
49 wlr_log(WLR_ERROR, "Failed to parse unregister SNI message: %s", strerror(-ret));
50 }
51
52 struct swaybar_tray *tray = data;
53 int idx = list_seq_find(tray->items, cmp_sni_id, id);
54 if (idx != -1) {
55 wlr_log(WLR_DEBUG, "Unregistering Status Notifier Item '%s'", id);
56 free(tray->items->items[idx]);
57 list_del(tray->items, idx);
58 }
59 return ret;
60}
61
62static int get_registered_snis_callback(sd_bus_message *msg, void *data,
63 sd_bus_error *error) {
64 if (sd_bus_message_is_method_error(msg, NULL)) {
65 sd_bus_error err = *sd_bus_message_get_error(msg);
66 wlr_log(WLR_ERROR, "Failed to get registered SNIs: %s", err.message);
67 return -sd_bus_error_get_errno(&err);
68 }
69
70 int ret = sd_bus_message_enter_container(msg, 'v', NULL);
71 if (ret < 0) {
72 wlr_log(WLR_ERROR, "Failed to read registered SNIs: %s", strerror(-ret));
73 return ret;
74 }
75
76 char **ids;
77 ret = sd_bus_message_read_strv(msg, &ids);
78 if (ret < 0) {
79 wlr_log(WLR_ERROR, "Failed to read registered SNIs: %s", strerror(-ret));
80 return ret;
81 }
82
83 if (ids) {
84 struct swaybar_tray *tray = data;
85 for (char **id = ids; *id; ++id) {
86 add_sni(tray, *id);
87 }
88 }
89
90 return ret;
91}
92
93static bool register_to_watcher(struct swaybar_host *host) {
94 // this is called asynchronously in case the watcher is owned by this process
95 int ret = sd_bus_call_method_async(host->tray->bus, NULL,
96 host->watcher_interface, watcher_path, host->watcher_interface,
97 "RegisterStatusNotifierHost", NULL, NULL, "s", host->service);
98 if (ret < 0) {
99 wlr_log(WLR_ERROR, "Failed to send register call: %s", strerror(-ret));
100 return false;
101 }
102
103 ret = sd_bus_call_method_async(host->tray->bus, NULL,
104 host->watcher_interface, watcher_path,
105 "org.freedesktop.DBus.Properties", "Get",
106 get_registered_snis_callback, host->tray, "ss",
107 host->watcher_interface, "RegisteredStatusNotifierItems");
108 if (ret < 0) {
109 wlr_log(WLR_ERROR, "Failed to get registered SNIs: %s", strerror(-ret));
110 }
111
112 return ret >= 0;
113}
114
115bool init_host(struct swaybar_host *host, char *protocol,
116 struct swaybar_tray *tray) {
117 size_t len = snprintf(NULL, 0, "org.%s.StatusNotifierWatcher", protocol) + 1;
118 host->watcher_interface = malloc(len);
119 if (!host->watcher_interface) {
120 return false;
121 }
122 snprintf(host->watcher_interface, len, "org.%s.StatusNotifierWatcher", protocol);
123
124 sd_bus_slot *reg_slot = NULL, *unreg_slot = NULL;
125 int ret = sd_bus_match_signal(tray->bus, &reg_slot, host->watcher_interface,
126 watcher_path, host->watcher_interface,
127 "StatusNotifierItemRegistered", handle_sni_registered, tray);
128 if (ret < 0) {
129 wlr_log(WLR_ERROR, "Failed to subscribe to registering events: %s",
130 strerror(-ret));
131 goto error;
132 }
133 ret = sd_bus_match_signal(tray->bus, &unreg_slot, host->watcher_interface,
134 watcher_path, host->watcher_interface,
135 "StatusNotifierItemUnregistered", handle_sni_unregistered, tray);
136 if (ret < 0) {
137 wlr_log(WLR_ERROR, "Failed to subscribe to unregistering events: %s",
138 strerror(-ret));
139 goto error;
140 }
141
142 pid_t pid = getpid();
143 size_t service_len = snprintf(NULL, 0, "org.%s.StatusNotifierHost-%d",
144 protocol, pid) + 1;
145 host->service = malloc(service_len);
146 if (!host->service) {
147 goto error;
148 }
149 snprintf(host->service, service_len, "org.%s.StatusNotifierHost-%d", protocol, pid);
150 ret = sd_bus_request_name(tray->bus, host->service, 0);
151 if (ret < 0) {
152 wlr_log(WLR_DEBUG, "Failed to acquire service name: %s", strerror(-ret));
153 goto error;
154 }
155
156 host->tray = tray;
157 if (!register_to_watcher(host)) {
158 goto error;
159 }
160
161 sd_bus_slot_set_floating(reg_slot, 1);
162 sd_bus_slot_set_floating(unreg_slot, 1);
163
164 wlr_log(WLR_DEBUG, "Registered %s", host->service);
165 return true;
166error:
167 sd_bus_slot_unref(reg_slot);
168 sd_bus_slot_unref(unreg_slot);
169 finish_host(host);
170 return false;
171}
172
173void finish_host(struct swaybar_host *host) {
174 sd_bus_release_name(host->tray->bus, host->service);
175 free(host->service);
176 free(host->watcher_interface);
177}
diff --git a/swaybar/tray/tray.c b/swaybar/tray/tray.c
index c1d3b50b..e760812c 100644
--- a/swaybar/tray/tray.c
+++ b/swaybar/tray/tray.c
@@ -4,8 +4,10 @@
4#include <string.h> 4#include <string.h>
5#include "swaybar/bar.h" 5#include "swaybar/bar.h"
6#include "swaybar/tray/icon.h" 6#include "swaybar/tray/icon.h"
7#include "swaybar/tray/host.h"
7#include "swaybar/tray/tray.h" 8#include "swaybar/tray/tray.h"
8#include "swaybar/tray/watcher.h" 9#include "swaybar/tray/watcher.h"
10#include "list.h"
9#include "log.h" 11#include "log.h"
10 12
11struct swaybar_tray *create_tray(struct swaybar *bar) { 13struct swaybar_tray *create_tray(struct swaybar *bar) {
@@ -29,6 +31,11 @@ struct swaybar_tray *create_tray(struct swaybar *bar) {
29 tray->watcher_xdg = create_watcher("freedesktop", tray->bus); 31 tray->watcher_xdg = create_watcher("freedesktop", tray->bus);
30 tray->watcher_kde = create_watcher("kde", tray->bus); 32 tray->watcher_kde = create_watcher("kde", tray->bus);
31 33
34 tray->items = create_list();
35
36 init_host(&tray->host_xdg, "freedesktop", tray);
37 init_host(&tray->host_kde, "kde", tray);
38
32 init_themes(&tray->themes, &tray->basedirs); 39 init_themes(&tray->themes, &tray->basedirs);
33 40
34 return tray; 41 return tray;
@@ -38,6 +45,12 @@ void destroy_tray(struct swaybar_tray *tray) {
38 if (!tray) { 45 if (!tray) {
39 return; 46 return;
40 } 47 }
48 finish_host(&tray->host_xdg);
49 finish_host(&tray->host_kde);
50 for (int i = 0; i < tray->items->length; ++i) {
51 free(tray->items->items[0]);
52 }
53 list_free(tray->items);
41 destroy_watcher(tray->watcher_xdg); 54 destroy_watcher(tray->watcher_xdg);
42 destroy_watcher(tray->watcher_kde); 55 destroy_watcher(tray->watcher_kde);
43 sd_bus_flush_close_unref(tray->bus); 56 sd_bus_flush_close_unref(tray->bus);