]> Dogcows Code - chaz/tint2/blob - src/launcher/launcher.c
Fixed theme lookup bug (was BFS instead of DFS)
[chaz/tint2] / src / launcher / launcher.c
1 /**************************************************************************
2 * Tint2 : launcher
3 *
4 * Copyright (C) 2010 (mrovi@interfete-web-club.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 **************************************************************************/
18
19 #include <string.h>
20 #include <stdio.h>
21 #include <cairo.h>
22 #include <cairo-xlib.h>
23 #include <pango/pangocairo.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <stdlib.h>
27
28 #include "window.h"
29 #include "server.h"
30 #include "area.h"
31 #include "panel.h"
32 #include "taskbar.h"
33 #include "launcher.h"
34
35 int launcher_enabled;
36 int launcher_max_icon_size;
37 GSList *icon_themes = 0;
38
39 #define ICON_FALLBACK "exec"
40
41 char *icon_path(Launcher *launcher, const char *icon_name, int size);
42 void launcher_load_themes(Launcher *launcher);
43 void free_desktop_entry(DesktopEntry *entry);
44 int launcher_read_desktop_file(const char *path, DesktopEntry *entry);
45 Imlib_Image scale_icon(Imlib_Image original, int icon_size);
46 void free_icon(Imlib_Image icon);
47 void free_icon_theme(IconTheme *theme);
48
49 void default_launcher()
50 {
51 launcher_enabled = 0;
52 launcher_max_icon_size = 0;
53 }
54
55
56 void init_launcher()
57 {
58 }
59
60
61 void init_launcher_panel(void *p)
62 {
63 Panel *panel =(Panel*)p;
64 Launcher *launcher = &panel->launcher;
65
66 launcher->area.parent = p;
67 launcher->area.panel = p;
68 launcher->area._draw_foreground = draw_launcher;
69 launcher->area.size_mode = SIZE_BY_CONTENT;
70 launcher->area._resize = resize_launcher;
71 launcher->area.resize = 1;
72 launcher->area.redraw = 1;
73 launcher->area.on_screen = 1;
74
75 if (panel_horizontal) {
76 // panel horizonal => fixed height and posy
77 launcher->area.posy = panel->area.bg->border.width + panel->area.paddingy;
78 launcher->area.height = panel->area.height - (2 * launcher->area.posy);
79 }
80 else {
81 // panel vertical => fixed width, height, posy and posx
82 launcher->area.posx = panel->area.bg->border.width + panel->area.paddingxlr;
83 launcher->area.width = panel->area.width - (2 * panel->area.bg->border.width) - (2 * panel->area.paddingy);
84 }
85
86 fprintf(stderr, "Loading themes...\n");
87 launcher_load_themes(launcher);
88 fprintf(stderr, "Done\n");
89
90 // Load apps (.desktop style launcher items)
91 GSList* app = launcher->list_apps;
92 while (app != NULL) {
93 DesktopEntry entry;
94 launcher_read_desktop_file(app->data, &entry);
95 if (entry.exec) {
96 LauncherIcon *launcherIcon = calloc(1, sizeof(LauncherIcon));
97 launcherIcon->is_app_desktop = 1;
98 launcherIcon->cmd = strdup(entry.exec);
99 launcherIcon->icon_name = entry.icon ? strdup(entry.icon) : strdup(ICON_FALLBACK);
100 launcherIcon->icon_size = 1;
101 free_desktop_entry(&entry);
102 launcher->list_icons = g_slist_append(launcher->list_icons, launcherIcon);
103 }
104 app = g_slist_next(app);
105 }
106 }
107
108
109 void cleanup_launcher()
110 {
111 int i;
112
113 for (i = 0 ; i < nb_panel ; i++) {
114 Panel *panel = &panel1[i];
115 Launcher *launcher = &panel->launcher;
116 free_area(&launcher->area);
117 GSList *l;
118 for (l = launcher->list_icons; l ; l = l->next) {
119 LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
120 if (launcherIcon) {
121 free_icon(launcherIcon->icon_scaled);
122 free_icon(launcherIcon->icon_original);
123 free(launcherIcon->icon_name);
124 free(launcherIcon->icon_path);
125 free(launcherIcon->cmd);
126 }
127 free(launcherIcon);
128 }
129 g_slist_free(launcher->list_icons);
130
131 for (l = launcher->list_apps; l ; l = l->next) {
132 free(l->data);
133 }
134 g_slist_free(launcher->list_apps);
135
136 for (l = launcher->icon_themes; l ; l = l->next) {
137 IconTheme *theme = (IconTheme*) l->data;
138 free_icon_theme(theme);
139 free(theme);
140 }
141 g_slist_free(launcher->icon_themes);
142
143 for (l = launcher->icon_theme_names; l ; l = l->next) {
144 free(l->data);
145 }
146 g_slist_free(launcher->icon_theme_names);
147 launcher->list_apps = launcher->list_icons = launcher->icon_theme_names = launcher->icon_themes = NULL;
148 }
149 launcher_enabled = 0;
150 }
151
152 void resize_launcher(void *obj)
153 {
154 Launcher *launcher = obj;
155 Panel *panel = launcher->area.panel;
156 GSList *l;
157 int count, icon_size;
158 int icons_per_column=1, icons_per_row=1, marging=0;
159
160 if (panel_horizontal)
161 icon_size = launcher->area.height;
162 else
163 icon_size = launcher->area.width;
164 icon_size = icon_size - (2 * launcher->area.bg->border.width) - (2 * launcher->area.paddingy);
165 if (launcher_max_icon_size > 0 && icon_size > launcher_max_icon_size)
166 icon_size = launcher_max_icon_size;
167
168 // Resize icons if necessary
169 for (l = launcher->list_icons; l ; l = l->next) {
170 LauncherIcon *launcherIcon = (LauncherIcon *)l->data;
171 if (launcherIcon->icon_size != icon_size || !launcherIcon->icon_original) {
172 launcherIcon->icon_size = icon_size;
173 // Get the path for an icon file with the new size
174 char *new_icon_path = icon_path(launcher, launcherIcon->icon_name, launcherIcon->icon_size);
175 if (!new_icon_path) {
176 // Draw a blank icon
177 free_icon(launcherIcon->icon_original);
178 launcherIcon->icon_original = NULL;
179 free_icon(launcherIcon->icon_scaled);
180 launcherIcon->icon_scaled = NULL;
181 new_icon_path = icon_path(launcher, ICON_FALLBACK, launcherIcon->icon_size);
182 if (new_icon_path) {
183 launcherIcon->icon_original = imlib_load_image(new_icon_path);
184 fprintf(stderr, "%s %d: Using icon %s\n", __FILE__, __LINE__, new_icon_path);
185 free(new_icon_path);
186 }
187 launcherIcon->icon_scaled = scale_icon(launcherIcon->icon_original, icon_size);
188 continue;
189 }
190 if (launcherIcon->icon_path && strcmp(new_icon_path, launcherIcon->icon_path) == 0) {
191 // If it's the same file just rescale
192 free_icon(launcherIcon->icon_scaled);
193 launcherIcon->icon_scaled = scale_icon(launcherIcon->icon_original, icon_size);
194 free(new_icon_path);
195 fprintf(stderr, "%s %d: Using icon %s\n", __FILE__, __LINE__, launcherIcon->icon_path);
196 } else {
197 // Free the old files
198 free_icon(launcherIcon->icon_original);
199 free_icon(launcherIcon->icon_scaled);
200 // Load the new file and scale
201 launcherIcon->icon_original = imlib_load_image(new_icon_path);
202 launcherIcon->icon_scaled = scale_icon(launcherIcon->icon_original, launcherIcon->icon_size);
203 free(launcherIcon->icon_path);
204 launcherIcon->icon_path = new_icon_path;
205 fprintf(stderr, "%s %d: Using icon %s\n", __FILE__, __LINE__, launcherIcon->icon_path);
206 }
207 }
208 }
209
210 count = 0;
211 for (l = launcher->list_icons; l ; l = l->next) {
212 count++;
213 }
214
215 if (panel_horizontal) {
216 if (!count) launcher->area.width = 0;
217 else {
218 int height = launcher->area.height - 2*launcher->area.bg->border.width - 2*launcher->area.paddingy;
219 // here icons_per_column always higher than 0
220 icons_per_column = (height+launcher->area.paddingx) / (icon_size+launcher->area.paddingx);
221 marging = height - (icons_per_column-1)*(icon_size+launcher->area.paddingx) - icon_size;
222 icons_per_row = count / icons_per_column + (count%icons_per_column != 0);
223 launcher->area.width = (2 * launcher->area.bg->border.width) + (2 * launcher->area.paddingxlr) + (icon_size * icons_per_row) + ((icons_per_row-1) * launcher->area.paddingx);
224 }
225
226 launcher->area.posx = panel->area.bg->border.width + panel->area.paddingxlr;
227 launcher->area.posy = panel->area.bg->border.width;
228 }
229 else {
230 if (!count) launcher->area.height = 0;
231 else {
232 int width = launcher->area.width - 2*launcher->area.bg->border.width - 2*launcher->area.paddingy;
233 // here icons_per_row always higher than 0
234 icons_per_row = (width+launcher->area.paddingx) / (icon_size+launcher->area.paddingx);
235 marging = width - (icons_per_row-1)*(icon_size+launcher->area.paddingx) - icon_size;
236 icons_per_column = count / icons_per_row+ (count%icons_per_row != 0);
237 launcher->area.height = (2 * launcher->area.bg->border.width) + (2 * launcher->area.paddingxlr) + (icon_size * icons_per_column) + ((icons_per_column-1) * launcher->area.paddingx);
238 }
239
240 launcher->area.posx = panel->area.bg->border.width;
241 launcher->area.posy = panel->area.height - panel->area.bg->border.width - panel->area.paddingxlr - launcher->area.height;
242 }
243
244 int i, posx, posy;
245 int start = launcher->area.bg->border.width + launcher->area.paddingy;// +marging/2;
246 if (panel_horizontal) {
247 posy = start;
248 posx = launcher->area.bg->border.width + launcher->area.paddingxlr;
249 }
250 else {
251 posx = start;
252 posy = launcher->area.bg->border.width + launcher->area.paddingxlr;
253 }
254
255 for (i=1, l = launcher->list_icons; l ; i++, l = l->next) {
256 LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
257
258 launcherIcon->y = posy;
259 launcherIcon->x = posx;
260 if (panel_horizontal) {
261 if (i % icons_per_column)
262 posy += icon_size + launcher->area.paddingx;
263 else {
264 posy = start;
265 posx += (icon_size + launcher->area.paddingx);
266 }
267 }
268 else {
269 if (i % icons_per_row)
270 posx += icon_size + launcher->area.paddingx;
271 else {
272 posx = start;
273 posy += (icon_size + launcher->area.paddingx);
274 }
275 }
276 }
277
278 // resize force the redraw
279 launcher->area.redraw = 1;
280 }
281
282
283 void draw_launcher(void *obj, cairo_t *c)
284 {
285 Launcher *launcher = obj;
286 GSList *l;
287 if (launcher->list_icons == 0) return;
288
289 for (l = launcher->list_icons; l ; l = l->next) {
290 LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
291 int pos_x = launcherIcon->x;
292 int pos_y = launcherIcon->y;
293 Imlib_Image icon_scaled = launcherIcon->icon_scaled;
294 // Render
295 imlib_context_set_image (icon_scaled);
296 if (server.real_transparency) {
297 render_image(launcher->area.pix, pos_x, pos_y, imlib_image_get_width(), imlib_image_get_height() );
298 }
299 else {
300 imlib_context_set_drawable(launcher->area.pix);
301 imlib_render_image_on_drawable (pos_x, pos_y);
302 }
303 }
304 }
305
306 Imlib_Image scale_icon(Imlib_Image original, int icon_size)
307 {
308 Imlib_Image icon_scaled;
309 if (original) {
310 imlib_context_set_image (original);
311 icon_scaled = imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), icon_size, icon_size);
312 } else {
313 icon_scaled = imlib_create_image(icon_size, icon_size);
314 imlib_context_set_image (icon_scaled);
315 imlib_context_set_color(255, 255, 255, 255);
316 imlib_image_fill_rectangle(0, 0, icon_size, icon_size);
317 }
318 return icon_scaled;
319 }
320
321 void free_icon(Imlib_Image icon)
322 {
323 if (icon) {
324 imlib_context_set_image(icon);
325 imlib_free_image();
326 }
327 }
328
329 void launcher_action(LauncherIcon *icon)
330 {
331 char *cmd = malloc(strlen(icon->cmd) + 10);
332 sprintf(cmd, "(%s&)", icon->cmd);
333 tint_exec(cmd);
334 free(cmd);
335 }
336
337 /***************** Freedesktop app.desktop and icon theme handling *********************/
338 /* http://standards.freedesktop.org/desktop-entry-spec/ */
339 /* http://standards.freedesktop.org/icon-theme-spec/ */
340
341 // Splits line at first '=' and returns 1 if successful, and parts are not empty
342 // key and value point to the parts
343 int parse_dektop_line(char *line, char **key, char **value)
344 {
345 char *p;
346 int found = 0;
347 *key = line;
348 for (p = line; *p; p++) {
349 if (*p == '=') {
350 *value = p + 1;
351 *p = 0;
352 found = 1;
353 break;
354 }
355 }
356 if (!found)
357 return 0;
358 if (found && (strlen(*key) == 0 || strlen(*value) == 0))
359 return 0;
360 return 1;
361 }
362
363 int parse_theme_line(char *line, char **key, char **value)
364 {
365 return parse_dektop_line(line, key, value);
366 }
367
368 void expand_exec(DesktopEntry *entry, const char *path)
369 {
370 // Expand % in exec
371 // %i -> --icon Icon
372 // %c -> Name
373 // %k -> path
374 if (entry->exec) {
375 char *exec2 = malloc(strlen(entry->exec) + strlen(entry->name) + strlen(entry->icon) + 100);
376 char *p, *q;
377 // p will never point to an escaped char
378 for (p = entry->exec, q = exec2; *p; p++, q++) {
379 *q = *p; // Copy
380 if (*p == '\\') {
381 p++, q++;
382 // Copy the escaped char
383 if (*p == '%') // For % we delete the backslash, i.e. write % over it
384 q--;
385 *q = *p;
386 if (!*p) break;
387 continue;
388 }
389 if (*p == '%') {
390 p++;
391 if (!*p) break;
392 if (*p == 'i' && entry->icon != NULL) {
393 sprintf(q, "--icon '%s'", entry->icon);
394 q += strlen("--icon ''");
395 q += strlen(entry->icon);
396 q--; // To balance the q++ in the for
397 } else if (*p == 'c' && entry->name != NULL) {
398 sprintf(q, "'%s'", entry->name);
399 q += strlen("''");
400 q += strlen(entry->name);
401 q--; // To balance the q++ in the for
402 } else if (*p == 'c') {
403 sprintf(q, "'%s'", path);
404 q += strlen("''");
405 q += strlen(path);
406 q--; // To balance the q++ in the for
407 } else {
408 // We don't care about other expansions
409 q--; // Delete the last % from q
410 }
411 continue;
412 }
413 }
414 *q = '\0';
415 free(entry->exec);
416 entry->exec = exec2;
417 }
418 }
419
420 //TODO Use UTF8 when parsing the file
421 int launcher_read_desktop_file(const char *path, DesktopEntry *entry)
422 {
423 FILE *fp;
424 char line[4096];
425 char *key, *value;
426
427 entry->name = entry->icon = entry->exec = NULL;
428
429 if ((fp = fopen(path, "rt")) == NULL) {
430 fprintf(stderr, "Could not open file %s\n", path);
431 return 0;
432 }
433
434 while (fgets(line, sizeof(line), fp) != NULL) {
435 int len = strlen(line);
436 if (len == 0)
437 continue;
438 line[len - 1] = '\0';
439 if (parse_dektop_line(line, &key, &value)) {
440 if (strcmp(key, "Name") == 0) {
441 entry->name = strdup(value);
442 } else if (strcmp(key, "Exec") == 0) {
443 entry->exec = strdup(value);
444 } else if (strcmp(key, "Icon") == 0) {
445 entry->icon = strdup(value);
446 }
447 }
448 }
449 fclose (fp);
450 // From this point:
451 // entry->name, entry->icon, entry->exec will never be empty strings (can be NULL though)
452
453 expand_exec(entry, path);
454
455 return 1;
456 }
457
458 void free_desktop_entry(DesktopEntry *entry)
459 {
460 free(entry->name);
461 free(entry->icon);
462 free(entry->exec);
463 }
464
465 void test_launcher_read_desktop_file()
466 {
467 fprintf(stdout, "\033[1;33m");
468 DesktopEntry entry;
469 launcher_read_desktop_file("/usr/share/applications/firefox.desktop", &entry);
470 printf("Name:%s Icon:%s Exec:%s\n", entry.name, entry.icon, entry.exec);
471 fprintf(stdout, "\033[0m");
472 }
473
474 //TODO Use UTF8 when parsing the file
475 IconTheme *load_theme(char *name)
476 {
477 // Look for name/index.theme in $HOME/.icons, /usr/share/icons, /usr/share/pixmaps (stop at the first found)
478 // Parse index.theme -> list of IconThemeDir with attributes
479 // Return IconTheme*
480
481 IconTheme *theme;
482 char *file_name;
483 FILE *f;
484 char line[2048];
485
486 if (name == NULL)
487 return NULL;
488
489 fprintf(stderr, "Loading icon theme %s\n", name);
490
491 file_name = malloc(100 + strlen(name));
492 sprintf(file_name, "~/.icons/%s/index.theme", name);
493 if (!g_file_test(file_name, G_FILE_TEST_EXISTS)) {
494 sprintf(file_name, "/usr/share/icons/%s/index.theme", name);
495 if (!g_file_test(file_name, G_FILE_TEST_EXISTS)) {
496 sprintf(file_name, "/usr/share/pixmaps/%s/index.theme", name);
497 if (!g_file_test(file_name, G_FILE_TEST_EXISTS)) {
498 free(file_name);
499 file_name = NULL;
500 }
501 }
502 }
503 if (!file_name) {
504 fprintf(stderr, "Could not load theme %s\n", name);
505 return NULL;
506 }
507
508 if ((f = fopen(file_name, "rt")) == NULL)
509 return NULL;
510
511 free(file_name);
512
513 theme = calloc(1, sizeof(IconTheme));
514 theme->name = strdup(name);
515 theme->list_inherits = NULL;
516 theme->list_directories = NULL;
517
518 IconThemeDir *current_dir = NULL;
519 int inside_header = 1;
520 while (fgets(line, sizeof(line), f) != NULL) {
521 char *key, *value;
522
523 int line_len = strlen(line);
524 if (line_len >= 1) {
525 if (line[line_len - 1] == '\n') {
526 line[line_len - 1] = '\0';
527 line_len--;
528 }
529 }
530
531 if (line_len == 0)
532 continue;
533
534 if (inside_header) {
535 if (parse_theme_line(line, &key, &value)) {
536 if (strcmp(key, "Inherits") == 0) {
537 // value is like oxygen,wood,default
538 char *token;
539 token = strtok(value, ",\n");
540 while (token != NULL)
541 {
542 theme->list_inherits = g_slist_append(theme->list_inherits, strdup(token));
543 token = strtok(NULL, ",\n");
544 }
545 } else if (strcmp(key, "Directories") == 0) {
546 // value is like 48x48/apps,48x48/mimetypes,32x32/apps,scalable/apps,scalable/mimetypes
547 char *token;
548 token = strtok(value, ",\n");
549 while (token != NULL)
550 {
551 IconThemeDir *dir = calloc(1, sizeof(IconThemeDir));
552 dir->name = strdup(token);
553 dir->max_size = dir->min_size = dir->size = -1;
554 dir->type = ICON_DIR_TYPE_THRESHOLD;
555 dir->threshold = 2;
556 theme->list_directories = g_slist_append(theme->list_directories, dir);
557 token = strtok(NULL, ",\n");
558 }
559 }
560 }
561 } else if (current_dir != NULL) {
562 if (parse_theme_line(line, &key, &value)) {
563 if (strcmp(key, "Size") == 0) {
564 // value is like 24
565 sscanf(value, "%d", &current_dir->size);
566 if (current_dir->max_size == -1)
567 current_dir->max_size = current_dir->size;
568 if (current_dir->min_size == -1)
569 current_dir->min_size = current_dir->size;
570 } else if (strcmp(key, "MaxSize") == 0) {
571 // value is like 24
572 sscanf(value, "%d", &current_dir->max_size);
573 } else if (strcmp(key, "MinSize") == 0) {
574 // value is like 24
575 sscanf(value, "%d", &current_dir->min_size);
576 } else if (strcmp(key, "Threshold") == 0) {
577 // value is like 2
578 sscanf(value, "%d", &current_dir->threshold);
579 } else if (strcmp(key, "Type") == 0) {
580 // value is Fixed, Scalable or Threshold
581 if (strcmp(value, "Fixed") == 0) {
582 current_dir->type = ICON_DIR_TYPE_FIXED;
583 } else if (strcmp(value, "Scalable") == 0) {
584 current_dir->type = ICON_DIR_TYPE_SCALABLE;
585 } else if (strcmp(value, "Threshold") == 0) {
586 current_dir->type = ICON_DIR_TYPE_THRESHOLD;
587 }
588 } else if (strcmp(key, "Context") == 0) {
589 // usual values: Actions, Applications, Devices, FileSystems, MimeTypes
590 current_dir->context = strdup(value);
591 }
592 }
593 }
594
595 if (line[0] == '[' && line[line_len - 1] == ']' && strcmp(line, "[Icon Theme]") != 0) {
596 inside_header = 0;
597 current_dir = NULL;
598 line[line_len - 1] = '\0';
599 char *dir_name = line + 1;
600 GSList* dir_item = theme->list_directories;
601 while (dir_item != NULL)
602 {
603 IconThemeDir *dir = dir_item->data;
604 if (strcmp(dir->name, dir_name) == 0) {
605 current_dir = dir;
606 break;
607 }
608 dir_item = g_slist_next(dir_item);
609 }
610 }
611 }
612 fclose(f);
613
614 return theme;
615 }
616
617 void free_icon_theme(IconTheme *theme)
618 {
619 free(theme->name);
620 GSList *l_inherits;
621 for (l_inherits = theme->list_inherits; l_inherits ; l_inherits = l_inherits->next) {
622 free(l_inherits->data);
623 }
624 GSList *l_dir;
625 for (l_dir = theme->list_directories; l_dir ; l_dir = l_dir->next) {
626 IconThemeDir *dir = (IconThemeDir *)l_dir->data;
627 free(dir->name);
628 free(dir->context);
629 free(l_dir->data);
630 }
631 }
632
633 void test_launcher_read_theme_file()
634 {
635 fprintf(stdout, "\033[1;33m");
636 IconTheme *theme = load_theme("oxygen");
637 if (!theme) {
638 printf("Could not load theme\n");
639 return;
640 }
641 printf("Loaded theme: %s\n", theme->name);
642 GSList* item = theme->list_inherits;
643 while (item != NULL)
644 {
645 printf("Inherits:%s\n", (char*)item->data);
646 item = g_slist_next(item);
647 }
648 item = theme->list_directories;
649 while (item != NULL)
650 {
651 IconThemeDir *dir = item->data;
652 printf("Dir:%s Size=%d MinSize=%d MaxSize=%d Threshold=%d Type=%s Context=%s\n",
653 dir->name, dir->size, dir->min_size, dir->max_size, dir->threshold,
654 dir->type == ICON_DIR_TYPE_FIXED ? "Fixed" :
655 dir->type == ICON_DIR_TYPE_SCALABLE ? "Scalable" :
656 dir->type == ICON_DIR_TYPE_THRESHOLD ? "Threshold" : "?????",
657 dir->context);
658 item = g_slist_next(item);
659 }
660 fprintf(stdout, "\033[0m");
661 }
662
663 // Populates the icon_themes list
664 void launcher_load_themes(Launcher *launcher)
665 {
666 // load the user theme, all the inherited themes recursively (DFS), and the hicolor theme
667 // avoid inheritance loops
668
669 GSList *queue = NULL;
670 GSList *queued = NULL;
671
672 GSList* icon_theme_name_item;
673 for (icon_theme_name_item = launcher->icon_theme_names; icon_theme_name_item; icon_theme_name_item = g_slist_next(icon_theme_name_item)) {
674 int duplicate = 0;
675 GSList* queued_item = queued;
676 while (queued_item != NULL) {
677 if (strcmp(queued_item->data, icon_theme_name_item->data) == 0) {
678 duplicate = 1;
679 break;
680 }
681 queued_item = g_slist_next(queued_item);
682 }
683 if (!duplicate) {
684 queue = g_slist_append(queue, strdup(icon_theme_name_item->data));
685 queued = g_slist_append(queued, strdup(icon_theme_name_item->data));
686 }
687 }
688
689 int hicolor_loaded = 0;
690 while (queue || !hicolor_loaded) {
691 if (!queue) {
692 GSList* queued_item = queued;
693 while (queued_item != NULL) {
694 if (strcmp(queued_item->data, "hicolor") == 0) {
695 hicolor_loaded = 1;
696 break;
697 }
698 queued_item = g_slist_next(queued_item);
699 }
700 if (hicolor_loaded)
701 break;
702 queue = g_slist_append(queue, strdup("hicolor"));
703 queued = g_slist_append(queued, strdup("hicolor"));
704 }
705
706 char *name = queue->data;
707 queue = g_slist_remove(queue, name);
708
709 IconTheme *theme = load_theme(name);
710 if (theme != NULL) {
711 launcher->icon_themes = g_slist_append(launcher->icon_themes, theme);
712
713 GSList* item = theme->list_inherits;
714 int pos = 0;
715 while (item != NULL)
716 {
717 char *parent = item->data;
718 int duplicate = 0;
719 GSList* queued_item = queued;
720 while (queued_item != NULL) {
721 if (strcmp(queued_item->data, parent) == 0) {
722 duplicate = 1;
723 break;
724 }
725 queued_item = g_slist_next(queued_item);
726 }
727 if (!duplicate) {
728 queue = g_slist_insert(queue, strdup(parent), pos);
729 pos++;
730 queued = g_slist_append(queued, strdup(parent));
731 }
732 item = g_slist_next(item);
733 }
734 }
735 }
736
737 // Free the queue
738 GSList *l;
739 for (l = queue; l ; l = l->next)
740 free(l->data);
741 g_slist_free(queue);
742 for (l = queued; l ; l = l->next)
743 free(l->data);
744 g_slist_free(queued);
745 }
746
747 int directory_matches_size(IconThemeDir *dir, int size)
748 {
749 if (dir->type == ICON_DIR_TYPE_FIXED) {
750 return dir->size == size;
751 } else if (dir->type == ICON_DIR_TYPE_SCALABLE) {
752 return dir->min_size <= size && size <= dir->max_size;
753 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
754 return dir->size - dir->threshold <= size && size <= dir->size + dir->threshold;
755 }
756 }
757
758 int directory_size_distance(IconThemeDir *dir, int size)
759 {
760 if (dir->type == ICON_DIR_TYPE_FIXED) {
761 return abs(dir->size - size);
762 } else if (dir->type == ICON_DIR_TYPE_SCALABLE) {
763 if (size < dir->min_size) {
764 return dir->min_size - size;
765 } else if (size > dir->max_size) {
766 return size - dir->max_size;
767 } else {
768 return 0;
769 }
770 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
771 if (size < dir->size - dir->threshold) {
772 return dir->min_size - size;
773 } else if (size > dir->size + dir->threshold) {
774 return size - dir->max_size;
775 } else {
776 return 0;
777 }
778 }
779 }
780
781 // Returns the full path to an icon file (or NULL) given the icon name
782 char *icon_path(Launcher *launcher, const char *icon_name, int size)
783 {
784 if (icon_name == NULL)
785 return NULL;
786
787 // If the icon_name is already a path and the file exists, return it
788 if (strstr(icon_name, "/") == icon_name) {
789 if (g_file_test(icon_name, G_FILE_TEST_EXISTS))
790 return strdup(icon_name);
791 else
792 return NULL;
793 }
794
795 GSList *basenames = NULL;
796 basenames = g_slist_append(basenames, "~/.icons");
797 basenames = g_slist_append(basenames, "/usr/share/icons");
798 basenames = g_slist_append(basenames, "/usr/share/pixmaps");
799
800 GSList *extensions = NULL;
801 extensions = g_slist_append(extensions, "png");
802 extensions = g_slist_append(extensions, "xpm");
803
804 // Stage 1: exact size match
805 GSList *theme;
806 for (theme = launcher->icon_themes; theme; theme = g_slist_next(theme)) {
807 GSList *dir;
808 for (dir = ((IconTheme*)theme->data)->list_directories; dir; dir = g_slist_next(dir)) {
809 if (directory_matches_size((IconThemeDir*)dir->data, size)) {
810 GSList *base;
811 for (base = basenames; base; base = g_slist_next(base)) {
812 GSList *ext;
813 for (ext = extensions; ext; ext = g_slist_next(ext)) {
814 char *base_name = (char*) base->data;
815 char *theme_name = ((IconTheme*)theme->data)->name;
816 char *dir_name = ((IconThemeDir*)dir->data)->name;
817 char *extension = (char*) ext->data;
818 char *file_name = malloc(strlen(base_name) + strlen(theme_name) +
819 strlen(dir_name) + strlen(icon_name) + strlen(extension) + 100);
820 // filename = directory/$(themename)/subdirectory/iconname.extension
821 sprintf(file_name, "%s/%s/%s/%s.%s", base_name, theme_name, dir_name, icon_name, extension);
822 //printf("checking %s\n", file_name);
823 if (g_file_test(file_name, G_FILE_TEST_EXISTS)) {
824 g_slist_free(basenames);
825 g_slist_free(extensions);
826 return file_name;
827 } else {
828 free(file_name);
829 file_name = NULL;
830 }
831 }
832 }
833 }
834 }
835 }
836
837 // Stage 2: best size match
838 // Contrary to the freedesktop spec, we are not choosing the closest icon in size, but the next larger icon
839 // otherwise the quality is usually crap (for size 22, if you can choose 16 or 32, you're better with 32)
840 // We do fallback to the closest size if we cannot find a larger or equal icon
841 int minimal_size = INT_MAX;
842 char *best_file_name = NULL;
843 int next_larger_size = -1;
844 char *next_larger = NULL;
845 for (theme = launcher->icon_themes; theme; theme = g_slist_next(theme)) {
846 GSList *dir;
847 for (dir = ((IconTheme*)theme->data)->list_directories; dir; dir = g_slist_next(dir)) {
848 GSList *base;
849 for (base = basenames; base; base = g_slist_next(base)) {
850 GSList *ext;
851 for (ext = extensions; ext; ext = g_slist_next(ext)) {
852 char *base_name = (char*) base->data;
853 char *theme_name = ((IconTheme*)theme->data)->name;
854 char *dir_name = ((IconThemeDir*)dir->data)->name;
855 char *extension = (char*) ext->data;
856 char *file_name = malloc(strlen(base_name) + strlen(theme_name) +
857 strlen(dir_name) + strlen(icon_name) + strlen(extension) + 100);
858 // filename = directory/$(themename)/subdirectory/iconname.extension
859 sprintf(file_name, "%s/%s/%s/%s.%s", base_name, theme_name, dir_name, icon_name, extension);
860 if (g_file_test(file_name, G_FILE_TEST_EXISTS)) {
861 if (directory_size_distance((IconThemeDir*)dir->data, size) < minimal_size) {
862 if (best_file_name) {
863 free(best_file_name);
864 best_file_name = NULL;
865 }
866 best_file_name = strdup(file_name);
867 minimal_size = directory_size_distance((IconThemeDir*)dir->data, size);
868 }
869 if (((IconThemeDir*)dir->data)->size >= size && (next_larger_size == -1 || ((IconThemeDir*)dir->data)->size < next_larger_size)) {
870 if (next_larger) {
871 free(next_larger);
872 next_larger = NULL;
873 }
874 next_larger = strdup(file_name);
875 next_larger_size = ((IconThemeDir*)dir->data)->size;
876 }
877 }
878 free(file_name);
879 }
880 }
881 }
882 }
883 if (next_larger) {
884 g_slist_free(basenames);
885 g_slist_free(extensions);
886 free(best_file_name);
887 return next_larger;
888 }
889 if (best_file_name) {
890 g_slist_free(basenames);
891 g_slist_free(extensions);
892 return best_file_name;
893 }
894
895 // Stage 3: look in unthemed icons
896 {
897 GSList *base;
898 for (base = basenames; base; base = g_slist_next(base)) {
899 GSList *ext;
900 for (ext = extensions; ext; ext = g_slist_next(ext)) {
901 char *base_name = (char*) base->data;
902 char *extension = (char*) ext->data;
903 char *file_name = malloc(strlen(base_name) + strlen(icon_name) +
904 strlen(extension) + 100);
905 // filename = directory/iconname.extension
906 sprintf(file_name, "%s/%s.%s", base_name, icon_name, extension);
907 //printf("checking %s\n", file_name);
908 if (g_file_test(file_name, G_FILE_TEST_EXISTS)) {
909 g_slist_free(basenames);
910 g_slist_free(extensions);
911 return file_name;
912 } else {
913 free(file_name);
914 file_name = NULL;
915 }
916 }
917 }
918 }
919
920 fprintf(stderr, "Could not find icon %s\n", icon_name);
921
922 return NULL;
923 }
This page took 0.080242 seconds and 5 git commands to generate.