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