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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
#define _XOPEN_SOURCE 700
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <stdint.h>
#include <limits.h>
#include "swaybar/tray/icon.h"
#include "swaybar/bar.h"
#include "swaybar/config.h"
#include "stringop.h"
#include "log.h"
/**
* REVIEW:
* This file repeats lots of "costly" operations that are the same for every
* icon. It's possible to create a dictionary or some other structure to cache
* these, though it may complicate things somewhat.
*
* Also parsing (index.theme) is currently pretty messy, so that could be made
* much better as well. Over all, things work, but are not optimal.
*/
/* Finds all themes that the given theme inherits */
static list_t *find_inherits(const char *theme_dir) {
const char inherits[] = "Inherits";
const char index_name[] = "/index.theme";
list_t *themes = create_list();
FILE *index = NULL;
char *path = malloc(strlen(theme_dir) + sizeof(index_name));
if (!path) {
goto fail;
}
if (!themes) {
goto fail;
}
strcpy(path, theme_dir);
strcat(path, index_name);
index = fopen(path, "r");
if (!index) {
goto fail;
}
char *buf = NULL;
size_t n = 0;
while (!feof(index) && getline(&buf, &n, index) != -1) {
if (n <= sizeof(inherits) + 1) {
continue;
}
if (strncmp(inherits, buf, sizeof(inherits) - 1) == 0) {
char *themestr = buf + sizeof(inherits);
themes = split_string(themestr, ",");
break;
}
}
free(buf);
fail:
free(path);
if (index) {
fclose(index);
}
return themes;
}
static bool isdir(const char *path) {
struct stat statbuf;
if (stat(path, &statbuf) != -1) {
if (S_ISDIR(statbuf.st_mode)) {
return true;
}
}
return false;
}
static bool isfile(const char *path) {
struct stat statbuf;
if (stat(path, &statbuf) != -1) {
if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode)) {
return true;
}
}
return false;
}
/**
* Returns the directory of a given theme if it exists.
* The returned pointer must be freed.
*/
static char *find_theme_dir(const char *theme) {
char *basedir;
char *icon_dir;
if (!theme) {
return NULL;
}
if (!(icon_dir = malloc(1024))) {
sway_log(L_ERROR, "Out of memory!");
goto fail;
}
if ((basedir = getenv("HOME"))) {
if (snprintf(icon_dir, 1024, "%s/.icons/%s", basedir, theme) >= 1024) {
sway_log(L_ERROR, "Path too long to render");
// XXX perhaps just goto trying in /usr/share? This
// shouldn't happen anyway, but might with a long global
goto fail;
}
if (isdir(icon_dir)) {
return icon_dir;
}
}
if ((basedir = getenv("XDG_DATA_DIRS"))) {
if (!(basedir = strdup(basedir))) {
sway_log_errno(L_ERROR, "Path too long to render");
goto fail;
}
char *token = strtok(basedir, ":");
while (token) {
// By peeking at the spec, there should be a slash at
// the end of the data dir.
if (snprintf(icon_dir, 1024, "%sicons/%s", token, theme) >= 1024) {
sway_log(L_ERROR, "Path too long to render");
// ditto
free(basedir);
goto fail;
}
if (isdir(icon_dir)) {
free(basedir);
return icon_dir;
}
token = strtok(NULL, ":");
}
free(basedir);
}
// Spec says use "/usr/share/pixmaps/", but I see everything in
// "/usr/share/icons/" look it both, I suppose.
if (snprintf(icon_dir, 1024, "/usr/share/pixmaps/%s", theme) >= 1024) {
sway_log(L_ERROR, "Path too long to render");
goto fail;
}
if (isdir(icon_dir)) {
return icon_dir;
}
if (snprintf(icon_dir, 1024, "/usr/share/icons/%s", theme) >= 1024) {
sway_log(L_ERROR, "Path too long to render");
goto fail;
}
if (isdir(icon_dir)) {
return icon_dir;
}
fail:
free(icon_dir);
sway_log(L_ERROR, "Could not find dir for theme: %s", theme);
return NULL;
}
/**
* Returns all theme dirs needed to be looked in for an icon.
* Does not check for duplicates
*/
static list_t *find_all_theme_dirs(const char *theme) {
list_t *dirs = create_list();
if (!dirs) {
return NULL;
}
char *dir = find_theme_dir(theme);
if (dir) {
list_add(dirs, dir);
list_t *inherits = find_inherits(dir);
list_cat(dirs, inherits);
list_free(inherits);
}
// 'default' usually inherits the default theme. I don't believe it has
// any icons, but look for them anyway
dir = find_theme_dir("default");
if (dir) {
list_add(dirs, dir);
list_t *inherits = find_inherits(dir);
list_cat(dirs, inherits);
list_free(inherits);
}
dir = find_theme_dir("hicolor");
if (dir) {
list_add(dirs, dir);
}
return dirs;
}
struct subdir {
int size;
char name[];
};
static int subdir_str_cmp(const void *_subdir, const void *_str) {
const struct subdir *subdir = _subdir;
const char *str = _str;
return strcmp(subdir->name, str);
}
/**
* Helper to find_subdirs. Acts similar to `split_string(subdirs, ",")` but
* generates a list of struct subdirs
*/
static list_t *split_subdirs(char *subdir_str) {
list_t *subdir_list = create_list();
char *copy = strdup(subdir_str);
if (!subdir_list || !copy) {
list_free(subdir_list);
free(copy);
return NULL;
}
char *token;
token = strtok(copy, ",");
while(token) {
int len = strlen(token) + 1;
struct subdir *subdir =
malloc(sizeof(struct subdir) + sizeof(char [len]));
if (!subdir) {
// Return what we have
return subdir_list;
}
subdir->size = 0;
strcpy(subdir->name, token);
list_add(subdir_list, subdir);
token = strtok(NULL, ",");
}
free(copy);
return subdir_list;
}
/**
* Returns a list of all subdirectories of a theme.
* Take note: the subdir names are all relative to `theme_dir` and must be
* combined with it to form a valid directory.
*
* Each member of the list is of type (struct subdir *) this struct contains
* the name of the subdir, along with size information. These must be freed
* bye the caller.
*
* This currently ignores min and max sizes of icons.
*/
static list_t* find_theme_subdirs(const char *theme_dir) {
const char index_name[] = "/index.theme";
list_t *dirs = NULL;
char *path = malloc(strlen(theme_dir) + sizeof(index_name));
FILE *index = NULL;
if (!path) {
sway_log(L_ERROR, "Failed to allocate memory");
goto fail;
}
strcpy(path, theme_dir);
strcat(path, index_name);
index = fopen(path, "r");
if (!index) {
sway_log(L_ERROR, "Could not open file: %s", path);
goto fail;
}
char *buf = NULL;
size_t n = 0;
const char directories[] = "Directories";
while (!feof(index) && getline(&buf, &n, index) != -1) {
if (n <= sizeof(directories) + 1) {
continue;
}
if (strncmp(directories, buf, sizeof(directories) - 1) == 0) {
char *dirstr = buf + sizeof(directories);
int len = strlen(dirstr);
if (dirstr[len-1] == '\n') {
dirstr[len-1] = '\0';
}
dirs = split_subdirs(dirstr);
break;
}
}
// Now, find the size of each dir
struct subdir *current_subdir = NULL;
const char size[] = "Size";
while (!feof(index) && getline(&buf, &n, index) != -1) {
if (buf[0] == '[') {
int len = strlen(buf);
if (buf[len-1] == '\n') {
len--;
}
// replace ']'
buf[len-1] = '\0';
int index;
if ((index = list_seq_find(dirs, subdir_str_cmp, buf+1)) != -1) {
current_subdir = (dirs->items[index]);
}
}
if (strncmp(size, buf, sizeof(size) - 1) == 0) {
if (current_subdir) {
current_subdir->size = atoi(buf + sizeof(size));
}
}
}
free(buf);
fail:
free(path);
if (index) {
fclose(index);
}
return dirs;
}
/* Returns true if full path and file exists */
static bool is_valid_path(const char *file) {
if (strstr(file, "/") == NULL || !isfile(file)) {
return false;
}
#ifdef WITH_GDK_PIXBUF
if (strstr(file, ".png") == NULL &&
strstr(file, ".xpm") == NULL &&
strstr(file, ".svg") == NULL) {
#else
if (strstr(file, ".png") == NULL) {
#endif
return false;
}
return true;
}
/* Returns the file of an icon given its name and size */
static char *find_icon_file(const char *name, int size) {
int namelen = strlen(name);
list_t *dirs = find_all_theme_dirs(swaybar.config->icon_theme);
if (!dirs) {
return NULL;
}
int min_size_diff = INT_MAX;
char *current_file = NULL;
for (int i = 0; i < dirs->length; ++i) {
char *dir = dirs->items[i];
list_t *subdirs = find_theme_subdirs(dir);
if (!subdirs) {
continue;
}
for (int i = 0; i < subdirs->length; ++i) {
struct subdir *subdir = subdirs->items[i];
// Only use an unsized if we don't already have a
// canidate this should probably change to allow svgs
if (!subdir->size && current_file) {
continue;
}
int size_diff = abs(size - subdir->size);
if (size_diff >= min_size_diff) {
continue;
}
char *path = malloc(strlen(subdir->name) + strlen(dir) + 2);
strcpy(path, dir);
path[strlen(dir)] = '/';
strcpy(path + strlen(dir) + 1, subdir->name);
DIR *icons = opendir(path);
if (!icons) {
free(path);
continue;
}
struct dirent *direntry;
while ((direntry = readdir(icons)) != NULL) {
int len = strlen(direntry->d_name);
if (len <= namelen + 2) { //must have some ext
continue;
}
if (strncmp(direntry->d_name, name, namelen) == 0) {
char *ext = direntry->d_name + namelen + 1;
#ifdef WITH_GDK_PIXBUF
if (strcmp(ext, "png") == 0 ||
strcmp(ext, "xpm") == 0 ||
strcmp(ext, "svg") == 0) {
#else
if (strcmp(ext, "png") == 0) {
#endif
free(current_file);
char *icon_path = malloc(strlen(path) + len + 2);
strcpy(icon_path, path);
icon_path[strlen(path)] = '/';
strcpy(icon_path + strlen(path) + 1, direntry->d_name);
current_file = icon_path;
min_size_diff = size_diff;
}
}
}
free(path);
closedir(icons);
}
free_flat_list(subdirs);
}
free_flat_list(dirs);
return current_file;
}
cairo_surface_t *find_icon(const char *name, int size) {
char *image_path;
if (is_valid_path(name)) {
image_path = strdup(name);
} else {
image_path = find_icon_file(name, size);
}
if (image_path == NULL) {
return NULL;
}
cairo_surface_t *image = NULL;
#ifdef WITH_GDK_PIXBUF
GError *err = NULL;
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(image_path, &err);
if (!pixbuf) {
sway_log(L_ERROR, "Failed to load icon image: %s", err->message);
}
image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf);
g_object_unref(pixbuf);
#else
// TODO make svg work? cairo supports it. maybe remove gdk alltogether
image = cairo_image_surface_create_from_png(image_path);
#endif //WITH_GDK_PIXBUF
if (!image) {
sway_log(L_ERROR, "Could not read icon image");
return NULL;
}
free(image_path);
return image;
}
|