aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar
diff options
context:
space:
mode:
authorLibravatar Simon Ser <contact@emersion.fr>2023-02-28 16:43:05 +0100
committerLibravatar Simon Ser <contact@emersion.fr>2023-04-14 18:34:54 +0200
commit08c1946d71039e583696842c3558b337aede1cbf (patch)
tree3873db2edfb31146bd6cd17dae63f068aef34f05 /swaybar
parentcommon/gesture: use format_str() (diff)
downloadsway-08c1946d71039e583696842c3558b337aede1cbf.tar.gz
sway-08c1946d71039e583696842c3558b337aede1cbf.tar.zst
sway-08c1946d71039e583696842c3558b337aede1cbf.zip
Use format_str() throughout
Diffstat (limited to 'swaybar')
-rw-r--r--swaybar/tray/host.c10
-rw-r--r--swaybar/tray/icon.c26
-rw-r--r--swaybar/tray/watcher.c9
3 files changed, 12 insertions, 33 deletions
diff --git a/swaybar/tray/host.c b/swaybar/tray/host.c
index ddf2416d..eea2caa5 100644
--- a/swaybar/tray/host.c
+++ b/swaybar/tray/host.c
@@ -10,6 +10,7 @@
10#include "swaybar/tray/tray.h" 10#include "swaybar/tray/tray.h"
11#include "list.h" 11#include "list.h"
12#include "log.h" 12#include "log.h"
13#include "stringop.h"
13 14
14static const char *watcher_path = "/StatusNotifierWatcher"; 15static const char *watcher_path = "/StatusNotifierWatcher";
15 16
@@ -138,12 +139,10 @@ static int handle_new_watcher(sd_bus_message *msg,
138 139
139bool init_host(struct swaybar_host *host, char *protocol, 140bool init_host(struct swaybar_host *host, char *protocol,
140 struct swaybar_tray *tray) { 141 struct swaybar_tray *tray) {
141 size_t len = snprintf(NULL, 0, "org.%s.StatusNotifierWatcher", protocol) + 1; 142 host->watcher_interface = format_str("org.%s.StatusNotifierWatcher", protocol);
142 host->watcher_interface = malloc(len);
143 if (!host->watcher_interface) { 143 if (!host->watcher_interface) {
144 return false; 144 return false;
145 } 145 }
146 snprintf(host->watcher_interface, len, "org.%s.StatusNotifierWatcher", protocol);
147 146
148 sd_bus_slot *reg_slot = NULL, *unreg_slot = NULL, *watcher_slot = NULL; 147 sd_bus_slot *reg_slot = NULL, *unreg_slot = NULL, *watcher_slot = NULL;
149 int ret = sd_bus_match_signal(tray->bus, &reg_slot, host->watcher_interface, 148 int ret = sd_bus_match_signal(tray->bus, &reg_slot, host->watcher_interface,
@@ -173,13 +172,10 @@ bool init_host(struct swaybar_host *host, char *protocol,
173 } 172 }
174 173
175 pid_t pid = getpid(); 174 pid_t pid = getpid();
176 size_t service_len = snprintf(NULL, 0, "org.%s.StatusNotifierHost-%d", 175 host->service = format_str("org.%s.StatusNotifierHost-%d", protocol, pid);
177 protocol, pid) + 1;
178 host->service = malloc(service_len);
179 if (!host->service) { 176 if (!host->service) {
180 goto error; 177 goto error;
181 } 178 }
182 snprintf(host->service, service_len, "org.%s.StatusNotifierHost-%d", protocol, pid);
183 ret = sd_bus_request_name(tray->bus, host->service, 0); 179 ret = sd_bus_request_name(tray->bus, host->service, 0);
184 if (ret < 0) { 180 if (ret < 0) {
185 sway_log(SWAY_DEBUG, "Failed to acquire service name: %s", strerror(-ret)); 181 sway_log(SWAY_DEBUG, "Failed to acquire service name: %s", strerror(-ret));
diff --git a/swaybar/tray/icon.c b/swaybar/tray/icon.c
index c426c3d4..b513dca5 100644
--- a/swaybar/tray/icon.c
+++ b/swaybar/tray/icon.c
@@ -40,9 +40,7 @@ static list_t *get_basedirs(void) {
40 data_dirs = strdup(data_dirs); 40 data_dirs = strdup(data_dirs);
41 char *dir = strtok(data_dirs, ":"); 41 char *dir = strtok(data_dirs, ":");
42 do { 42 do {
43 size_t path_len = snprintf(NULL, 0, "%s/icons", dir) + 1; 43 char *path = format_str("%s/icons", dir);
44 char *path = malloc(path_len);
45 snprintf(path, path_len, "%s/icons", dir);
46 list_add(basedirs, path); 44 list_add(basedirs, path);
47 } while ((dir = strtok(NULL, ":"))); 45 } while ((dir = strtok(NULL, ":")));
48 free(data_dirs); 46 free(data_dirs);
@@ -206,13 +204,7 @@ static const char *entry_handler(char *group, char *key, char *value,
206 */ 204 */
207static struct icon_theme *read_theme_file(char *basedir, char *theme_name) { 205static struct icon_theme *read_theme_file(char *basedir, char *theme_name) {
208 // look for index.theme file 206 // look for index.theme file
209 size_t path_len = snprintf(NULL, 0, "%s/%s/index.theme", basedir, 207 char *path = format_str("%s/%s/index.theme", basedir, theme_name);
210 theme_name) + 1;
211 char *path = malloc(path_len);
212 if (!path) {
213 return NULL;
214 }
215 snprintf(path, path_len, "%s/%s/index.theme", basedir, theme_name);
216 FILE *theme_file = fopen(path, "r"); 208 FILE *theme_file = fopen(path, "r");
217 free(path); 209 free(path);
218 if (!theme_file) { 210 if (!theme_file) {
@@ -416,26 +408,20 @@ static char *find_icon_in_subdir(char *name, char *basedir, char *theme,
416#endif 408#endif
417 }; 409 };
418 410
419 size_t path_len = snprintf(NULL, 0, "%s/%s/%s/%s.EXT", basedir, theme,
420 subdir, name) + 1;
421 char *path = malloc(path_len);
422
423 for (size_t i = 0; i < sizeof(extensions) / sizeof(*extensions); ++i) { 411 for (size_t i = 0; i < sizeof(extensions) / sizeof(*extensions); ++i) {
424 snprintf(path, path_len, "%s/%s/%s/%s.%s", basedir, theme, subdir, 412 char *path = format_str("%s/%s/%s/%s.%s",
425 name, extensions[i]); 413 basedir, theme, subdir, name, extensions[i]);
426 if (access(path, R_OK) == 0) { 414 if (access(path, R_OK) == 0) {
427 return path; 415 return path;
428 } 416 }
417 free(path);
429 } 418 }
430 419
431 free(path);
432 return NULL; 420 return NULL;
433} 421}
434 422
435static bool theme_exists_in_basedir(char *theme, char *basedir) { 423static bool theme_exists_in_basedir(char *theme, char *basedir) {
436 size_t path_len = snprintf(NULL, 0, "%s/%s", basedir, theme) + 1; 424 char *path = format_str("%s/%s", basedir, theme);
437 char *path = malloc(path_len);
438 snprintf(path, path_len, "%s/%s", basedir, theme);
439 bool ret = dir_exists(path); 425 bool ret = dir_exists(path);
440 free(path); 426 free(path);
441 return ret; 427 return ret;
diff --git a/swaybar/tray/watcher.c b/swaybar/tray/watcher.c
index 16afc27c..551e1d12 100644
--- a/swaybar/tray/watcher.c
+++ b/swaybar/tray/watcher.c
@@ -6,6 +6,7 @@
6#include <string.h> 6#include <string.h>
7#include "list.h" 7#include "list.h"
8#include "log.h" 8#include "log.h"
9#include "stringop.h"
9#include "swaybar/tray/watcher.h" 10#include "swaybar/tray/watcher.h"
10 11
11static const char *obj_path = "/StatusNotifierWatcher"; 12static const char *obj_path = "/StatusNotifierWatcher";
@@ -76,9 +77,7 @@ static int register_sni(sd_bus_message *msg, void *data, sd_bus_error *error) {
76 service = service_or_path; 77 service = service_or_path;
77 path = "/StatusNotifierItem"; 78 path = "/StatusNotifierItem";
78 } 79 }
79 size_t id_len = snprintf(NULL, 0, "%s%s", service, path) + 1; 80 id = format_str("%s%s", service, path);
80 id = malloc(id_len);
81 snprintf(id, id_len, "%s%s", service, path);
82 } 81 }
83 82
84 if (list_seq_find(watcher->items, cmp_id, id) == -1) { 83 if (list_seq_find(watcher->items, cmp_id, id) == -1) {
@@ -159,9 +158,7 @@ struct swaybar_watcher *create_watcher(char *protocol, sd_bus *bus) {
159 return NULL; 158 return NULL;
160 } 159 }
161 160
162 size_t len = snprintf(NULL, 0, "org.%s.StatusNotifierWatcher", protocol) + 1; 161 watcher->interface = format_str("org.%s.StatusNotifierWatcher", protocol);
163 watcher->interface = malloc(len);
164 snprintf(watcher->interface, len, "org.%s.StatusNotifierWatcher", protocol);
165 162
166 sd_bus_slot *signal_slot = NULL, *vtable_slot = NULL; 163 sd_bus_slot *signal_slot = NULL, *vtable_slot = NULL;
167 int ret = sd_bus_add_object_vtable(bus, &vtable_slot, obj_path, 164 int ret = sd_bus_add_object_vtable(bus, &vtable_slot, obj_path,