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