watcher.c (7511B) - View raw
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227#include <stdbool.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "list.h" #include "log.h" #include "stringop.h" #include "swaybar/tray/watcher.h" static const char *obj_path = "/StatusNotifierWatcher"; static bool using_standard_protocol(struct swaybar_watcher *watcher) { return watcher->interface[strlen("org.")] == 'f'; // freedesktop } static int cmp_id(const void *item, const void *cmp_to) { return strcmp(item, cmp_to); } static int handle_lost_service(sd_bus_message *msg, void *data, sd_bus_error *error) { char *service, *old_owner, *new_owner; int ret = sd_bus_message_read(msg, "sss", &service, &old_owner, &new_owner); if (ret < 0) { sway_log(SWAY_ERROR, "Failed to parse owner change message: %s", strerror(-ret)); return ret; } if (!*new_owner) { struct swaybar_watcher *watcher = data; for (int idx = 0; idx < watcher->items->length; ++idx) { char *id = watcher->items->items[idx]; bool cmp_res = using_standard_protocol(watcher) ? cmp_id(id, service) == 0 : has_prefix(id, service); if (cmp_res) { sway_log(SWAY_DEBUG, "Unregistering Status Notifier Item '%s'", id); list_del(watcher->items, idx--); sd_bus_emit_signal(watcher->bus, obj_path, watcher->interface, "StatusNotifierItemUnregistered", "s", id); sd_bus_emit_properties_changed(watcher->bus, obj_path, watcher->interface, "RegisteredStatusNotifierItems", NULL); free(id); if (using_standard_protocol(watcher)) { break; } } } int idx = list_seq_find(watcher->hosts, cmp_id, service); if (idx != -1) { sway_log(SWAY_DEBUG, "Unregistering Status Notifier Host '%s'", service); free(watcher->hosts->items[idx]); list_del(watcher->hosts, idx); if (watcher->hosts->length == 0) { sd_bus_emit_properties_changed(watcher->bus, obj_path, watcher->interface, "IsStatusNotifierHostRegistered", NULL); } } } return 0; } static int register_sni(sd_bus_message *msg, void *data, sd_bus_error *error) { char *service_or_path, *id; int ret = sd_bus_message_read(msg, "s", &service_or_path); if (ret < 0) { sway_log(SWAY_ERROR, "Failed to parse register SNI message: %s", strerror(-ret)); return ret; } struct swaybar_watcher *watcher = data; if (using_standard_protocol(watcher)) { id = strdup(service_or_path); } else { const char *service, *path; if (service_or_path[0] == '/') { service = sd_bus_message_get_sender(msg); path = service_or_path; } else { service = service_or_path; path = "/StatusNotifierItem"; } id = format_str("%s%s", service, path); } if (list_seq_find(watcher->items, cmp_id, id) == -1) { sway_log(SWAY_DEBUG, "Registering Status Notifier Item '%s'", id); list_add(watcher->items, id); sd_bus_emit_properties_changed(watcher->bus, obj_path, watcher->interface, "RegisteredStatusNotifierItems", NULL); sd_bus_emit_signal(watcher->bus, obj_path, watcher->interface, "StatusNotifierItemRegistered", "s", id); } else { sway_log(SWAY_DEBUG, "Status Notifier Item '%s' already registered", id); free(id); } return sd_bus_reply_method_return(msg, ""); } static int register_host(sd_bus_message *msg, void *data, sd_bus_error *error) { char *service; int ret = sd_bus_message_read(msg, "s", &service); if (ret < 0) { sway_log(SWAY_ERROR, "Failed to parse register host message: %s", strerror(-ret)); return ret; } struct swaybar_watcher *watcher = data; if (list_seq_find(watcher->hosts, cmp_id, service) == -1) { sway_log(SWAY_DEBUG, "Registering Status Notifier Host '%s'", service); list_add(watcher->hosts, strdup(service)); if (watcher->hosts->length == 1) { sd_bus_emit_properties_changed(watcher->bus, obj_path, watcher->interface, "IsStatusNotifierHostRegistered", NULL); } sd_bus_emit_signal(watcher->bus, obj_path, watcher->interface, "StatusNotifierHostRegistered", ""); } else { sway_log(SWAY_DEBUG, "Status Notifier Host '%s' already registered", service); } return sd_bus_reply_method_return(msg, ""); } static int get_registered_snis(sd_bus *bus, const char *obj_path, const char *interface, const char *property, sd_bus_message *reply, void *data, sd_bus_error *error) { struct swaybar_watcher *watcher = data; list_add(watcher->items, NULL); // strv expects NULL-terminated string array int ret = sd_bus_message_append_strv(reply, (char **)watcher->items->items); list_del(watcher->items, watcher->items->length - 1); return ret; } static int is_host_registered(sd_bus *bus, const char *obj_path, const char *interface, const char *property, sd_bus_message *reply, void *data, sd_bus_error *error) { struct swaybar_watcher *watcher = data; int val = watcher->hosts->length > 0; // dbus expects int rather than bool return sd_bus_message_append_basic(reply, 'b', &val); } static const sd_bus_vtable watcher_vtable[] = { SD_BUS_VTABLE_START(0), SD_BUS_METHOD("RegisterStatusNotifierItem", "s", "", register_sni, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("RegisterStatusNotifierHost", "s", "", register_host, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_PROPERTY("RegisteredStatusNotifierItems", "as", get_registered_snis, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_PROPERTY("IsStatusNotifierHostRegistered", "b", is_host_registered, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_PROPERTY("ProtocolVersion", "i", NULL, offsetof(struct swaybar_watcher, version), SD_BUS_VTABLE_PROPERTY_CONST), SD_BUS_SIGNAL("StatusNotifierItemRegistered", "s", 0), SD_BUS_SIGNAL("StatusNotifierItemUnregistered", "s", 0), SD_BUS_SIGNAL("StatusNotifierHostRegistered", NULL, 0), SD_BUS_VTABLE_END }; struct swaybar_watcher *create_watcher(char *protocol, sd_bus *bus) { struct swaybar_watcher *watcher = calloc(1, sizeof(struct swaybar_watcher)); if (!watcher) { return NULL; } watcher->interface = format_str("org.%s.StatusNotifierWatcher", protocol); sd_bus_slot *signal_slot = NULL, *vtable_slot = NULL; int ret = sd_bus_add_object_vtable(bus, &vtable_slot, obj_path, watcher->interface, watcher_vtable, watcher); if (ret < 0) { sway_log(SWAY_ERROR, "Failed to add object vtable: %s", strerror(-ret)); goto error; } ret = sd_bus_match_signal(bus, &signal_slot, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "NameOwnerChanged", handle_lost_service, watcher); if (ret < 0) { sway_log(SWAY_ERROR, "Failed to subscribe to unregistering events: %s", strerror(-ret)); goto error; } ret = sd_bus_request_name(bus, watcher->interface, 0); if (ret < 0) { if (-ret == EEXIST) { sway_log(SWAY_DEBUG, "Failed to acquire service name '%s':" "another tray is already running", watcher->interface); } else { sway_log(SWAY_ERROR, "Failed to acquire service name '%s': %s", watcher->interface, strerror(-ret)); } goto error; } sd_bus_slot_set_floating(signal_slot, 0); sd_bus_slot_set_floating(vtable_slot, 0); watcher->bus = bus; watcher->hosts = create_list(); watcher->items = create_list(); watcher->version = 0; sway_log(SWAY_DEBUG, "Registered %s", watcher->interface); return watcher; error: sd_bus_slot_unref(signal_slot); sd_bus_slot_unref(vtable_slot); destroy_watcher(watcher); return NULL; } void destroy_watcher(struct swaybar_watcher *watcher) { if (!watcher) { return; } list_free_items_and_destroy(watcher->hosts); list_free_items_and_destroy(watcher->items); free(watcher->interface); free(watcher); }