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