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