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