]> Dogcows Code - chaz/tint2/blob - src/launcher/launcher.c
panel_items : fixed segfault (panel without clock, change number desktop)
[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 GSList *l;
161 int count, icon_size;
162 int icons_per_column=1, icons_per_row=1, marging=0;
163
164 if (panel_horizontal)
165 icon_size = launcher->area.height;
166 else
167 icon_size = launcher->area.width;
168 icon_size = icon_size - (2 * launcher->area.bg->border.width) - (2 * launcher->area.paddingy);
169 if (launcher_max_icon_size > 0 && icon_size > launcher_max_icon_size)
170 icon_size = launcher_max_icon_size;
171
172 // Resize icons if necessary
173 for (l = launcher->list_icons; l ; l = l->next) {
174 LauncherIcon *launcherIcon = (LauncherIcon *)l->data;
175 if (launcherIcon->icon_size != icon_size || !launcherIcon->icon_original) {
176 launcherIcon->icon_size = icon_size;
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, "%s %d: Using icon %s\n", __FILE__, __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, "%s %d: Using icon %s\n", __FILE__, __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, "%s %d: Using icon %s\n", __FILE__, __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 fprintf(stderr, "Loading icon theme %s\n", name);
486
487 file_name = malloc(100 + strlen(name));
488 sprintf(file_name, "~/.icons/%s/index.theme", name);
489 if (!g_file_test(file_name, G_FILE_TEST_EXISTS)) {
490 sprintf(file_name, "/usr/share/icons/%s/index.theme", name);
491 if (!g_file_test(file_name, G_FILE_TEST_EXISTS)) {
492 sprintf(file_name, "/usr/share/pixmaps/%s/index.theme", name);
493 if (!g_file_test(file_name, G_FILE_TEST_EXISTS)) {
494 free(file_name);
495 file_name = NULL;
496 }
497 }
498 }
499 if (!file_name) {
500 fprintf(stderr, "Could not load theme %s\n", name);
501 return NULL;
502 }
503
504 if ((f = fopen(file_name, "rt")) == NULL)
505 return NULL;
506
507 free(file_name);
508
509 theme = calloc(1, sizeof(IconTheme));
510 theme->name = strdup(name);
511 theme->list_inherits = NULL;
512 theme->list_directories = NULL;
513
514 IconThemeDir *current_dir = NULL;
515 int inside_header = 1;
516 while (fgets(line, sizeof(line), f) != NULL) {
517 char *key, *value;
518
519 int line_len = strlen(line);
520 if (line_len >= 1) {
521 if (line[line_len - 1] == '\n') {
522 line[line_len - 1] = '\0';
523 line_len--;
524 }
525 }
526
527 if (line_len == 0)
528 continue;
529
530 if (inside_header) {
531 if (parse_theme_line(line, &key, &value)) {
532 if (strcmp(key, "Inherits") == 0) {
533 // value is like oxygen,wood,default
534 char *token;
535 token = strtok(value, ",\n");
536 while (token != NULL)
537 {
538 theme->list_inherits = g_slist_append(theme->list_inherits, strdup(token));
539 token = strtok(NULL, ",\n");
540 }
541 } else if (strcmp(key, "Directories") == 0) {
542 // value is like 48x48/apps,48x48/mimetypes,32x32/apps,scalable/apps,scalable/mimetypes
543 char *token;
544 token = strtok(value, ",\n");
545 while (token != NULL)
546 {
547 IconThemeDir *dir = calloc(1, sizeof(IconThemeDir));
548 dir->name = strdup(token);
549 dir->max_size = dir->min_size = dir->size = -1;
550 dir->type = ICON_DIR_TYPE_THRESHOLD;
551 dir->threshold = 2;
552 theme->list_directories = g_slist_append(theme->list_directories, dir);
553 token = strtok(NULL, ",\n");
554 }
555 }
556 }
557 } else if (current_dir != NULL) {
558 if (parse_theme_line(line, &key, &value)) {
559 if (strcmp(key, "Size") == 0) {
560 // value is like 24
561 sscanf(value, "%d", &current_dir->size);
562 if (current_dir->max_size == -1)
563 current_dir->max_size = current_dir->size;
564 if (current_dir->min_size == -1)
565 current_dir->min_size = current_dir->size;
566 } else if (strcmp(key, "MaxSize") == 0) {
567 // value is like 24
568 sscanf(value, "%d", &current_dir->max_size);
569 } else if (strcmp(key, "MinSize") == 0) {
570 // value is like 24
571 sscanf(value, "%d", &current_dir->min_size);
572 } else if (strcmp(key, "Threshold") == 0) {
573 // value is like 2
574 sscanf(value, "%d", &current_dir->threshold);
575 } else if (strcmp(key, "Type") == 0) {
576 // value is Fixed, Scalable or Threshold
577 if (strcmp(value, "Fixed") == 0) {
578 current_dir->type = ICON_DIR_TYPE_FIXED;
579 } else if (strcmp(value, "Scalable") == 0) {
580 current_dir->type = ICON_DIR_TYPE_SCALABLE;
581 } else if (strcmp(value, "Threshold") == 0) {
582 current_dir->type = ICON_DIR_TYPE_THRESHOLD;
583 }
584 } else if (strcmp(key, "Context") == 0) {
585 // usual values: Actions, Applications, Devices, FileSystems, MimeTypes
586 current_dir->context = strdup(value);
587 }
588 }
589 }
590
591 if (line[0] == '[' && line[line_len - 1] == ']' && strcmp(line, "[Icon Theme]") != 0) {
592 inside_header = 0;
593 current_dir = NULL;
594 line[line_len - 1] = '\0';
595 char *dir_name = line + 1;
596 GSList* dir_item = theme->list_directories;
597 while (dir_item != NULL)
598 {
599 IconThemeDir *dir = dir_item->data;
600 if (strcmp(dir->name, dir_name) == 0) {
601 current_dir = dir;
602 break;
603 }
604 dir_item = g_slist_next(dir_item);
605 }
606 }
607 }
608 fclose(f);
609
610 return theme;
611 }
612
613 void free_icon_theme(IconTheme *theme)
614 {
615 free(theme->name);
616 GSList *l_inherits;
617 for (l_inherits = theme->list_inherits; l_inherits ; l_inherits = l_inherits->next) {
618 free(l_inherits->data);
619 }
620 GSList *l_dir;
621 for (l_dir = theme->list_directories; l_dir ; l_dir = l_dir->next) {
622 IconThemeDir *dir = (IconThemeDir *)l_dir->data;
623 free(dir->name);
624 free(dir->context);
625 free(l_dir->data);
626 }
627 }
628
629 void test_launcher_read_theme_file()
630 {
631 fprintf(stdout, "\033[1;33m");
632 IconTheme *theme = load_theme("oxygen");
633 if (!theme) {
634 printf("Could not load theme\n");
635 return;
636 }
637 printf("Loaded theme: %s\n", theme->name);
638 GSList* item = theme->list_inherits;
639 while (item != NULL)
640 {
641 printf("Inherits:%s\n", (char*)item->data);
642 item = g_slist_next(item);
643 }
644 item = theme->list_directories;
645 while (item != NULL)
646 {
647 IconThemeDir *dir = item->data;
648 printf("Dir:%s Size=%d MinSize=%d MaxSize=%d Threshold=%d Type=%s Context=%s\n",
649 dir->name, dir->size, dir->min_size, dir->max_size, dir->threshold,
650 dir->type == ICON_DIR_TYPE_FIXED ? "Fixed" :
651 dir->type == ICON_DIR_TYPE_SCALABLE ? "Scalable" :
652 dir->type == ICON_DIR_TYPE_THRESHOLD ? "Threshold" : "?????",
653 dir->context);
654 item = g_slist_next(item);
655 }
656 fprintf(stdout, "\033[0m");
657 }
658
659 // Populates the icon_themes list
660 void launcher_load_themes(Launcher *launcher)
661 {
662 // load the user theme, all the inherited themes recursively (DFS), and the hicolor theme
663 // avoid inheritance loops
664
665 GSList *queue = NULL;
666 GSList *queued = NULL;
667
668 GSList* icon_theme_name_item;
669 for (icon_theme_name_item = launcher->icon_theme_names; icon_theme_name_item; icon_theme_name_item = g_slist_next(icon_theme_name_item)) {
670 int duplicate = 0;
671 GSList* queued_item = queued;
672 while (queued_item != NULL) {
673 if (strcmp(queued_item->data, icon_theme_name_item->data) == 0) {
674 duplicate = 1;
675 break;
676 }
677 queued_item = g_slist_next(queued_item);
678 }
679 if (!duplicate) {
680 queue = g_slist_append(queue, strdup(icon_theme_name_item->data));
681 queued = g_slist_append(queued, strdup(icon_theme_name_item->data));
682 }
683 }
684
685 int hicolor_loaded = 0;
686 while (queue || !hicolor_loaded) {
687 if (!queue) {
688 GSList* queued_item = queued;
689 while (queued_item != NULL) {
690 if (strcmp(queued_item->data, "hicolor") == 0) {
691 hicolor_loaded = 1;
692 break;
693 }
694 queued_item = g_slist_next(queued_item);
695 }
696 if (hicolor_loaded)
697 break;
698 queue = g_slist_append(queue, strdup("hicolor"));
699 queued = g_slist_append(queued, strdup("hicolor"));
700 }
701
702 char *name = queue->data;
703 queue = g_slist_remove(queue, name);
704
705 IconTheme *theme = load_theme(name);
706 if (theme != NULL) {
707 launcher->icon_themes = g_slist_append(launcher->icon_themes, theme);
708
709 GSList* item = theme->list_inherits;
710 int pos = 0;
711 while (item != NULL)
712 {
713 char *parent = item->data;
714 int duplicate = 0;
715 GSList* queued_item = queued;
716 while (queued_item != NULL) {
717 if (strcmp(queued_item->data, parent) == 0) {
718 duplicate = 1;
719 break;
720 }
721 queued_item = g_slist_next(queued_item);
722 }
723 if (!duplicate) {
724 queue = g_slist_insert(queue, strdup(parent), pos);
725 pos++;
726 queued = g_slist_append(queued, strdup(parent));
727 }
728 item = g_slist_next(item);
729 }
730 }
731 }
732
733 // Free the queue
734 GSList *l;
735 for (l = queue; l ; l = l->next)
736 free(l->data);
737 g_slist_free(queue);
738 for (l = queued; l ; l = l->next)
739 free(l->data);
740 g_slist_free(queued);
741 }
742
743 int directory_matches_size(IconThemeDir *dir, int size)
744 {
745 if (dir->type == ICON_DIR_TYPE_FIXED) {
746 return dir->size == size;
747 } else if (dir->type == ICON_DIR_TYPE_SCALABLE) {
748 return dir->min_size <= size && size <= dir->max_size;
749 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
750 return dir->size - dir->threshold <= size && size <= dir->size + dir->threshold;
751 }
752 }
753
754 int directory_size_distance(IconThemeDir *dir, int size)
755 {
756 if (dir->type == ICON_DIR_TYPE_FIXED) {
757 return abs(dir->size - size);
758 } else if (dir->type == ICON_DIR_TYPE_SCALABLE) {
759 if (size < dir->min_size) {
760 return dir->min_size - size;
761 } else if (size > dir->max_size) {
762 return size - dir->max_size;
763 } else {
764 return 0;
765 }
766 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
767 if (size < dir->size - dir->threshold) {
768 return dir->min_size - size;
769 } else if (size > dir->size + dir->threshold) {
770 return size - dir->max_size;
771 } else {
772 return 0;
773 }
774 }
775 }
776
777 // Returns the full path to an icon file (or NULL) given the icon name
778 char *icon_path(Launcher *launcher, const char *icon_name, int size)
779 {
780 if (icon_name == NULL)
781 return NULL;
782
783 // If the icon_name is already a path and the file exists, return it
784 if (strstr(icon_name, "/") == icon_name) {
785 if (g_file_test(icon_name, G_FILE_TEST_EXISTS))
786 return strdup(icon_name);
787 else
788 return NULL;
789 }
790
791 GSList *basenames = NULL;
792 basenames = g_slist_append(basenames, "~/.icons");
793 basenames = g_slist_append(basenames, "/usr/share/icons");
794 basenames = g_slist_append(basenames, "/usr/share/pixmaps");
795
796 GSList *extensions = NULL;
797 extensions = g_slist_append(extensions, "png");
798 extensions = g_slist_append(extensions, "xpm");
799
800 // Stage 1: exact size match
801 GSList *theme;
802 for (theme = launcher->icon_themes; theme; theme = g_slist_next(theme)) {
803 GSList *dir;
804 for (dir = ((IconTheme*)theme->data)->list_directories; dir; dir = g_slist_next(dir)) {
805 if (directory_matches_size((IconThemeDir*)dir->data, size)) {
806 GSList *base;
807 for (base = basenames; base; base = g_slist_next(base)) {
808 GSList *ext;
809 for (ext = extensions; ext; ext = g_slist_next(ext)) {
810 char *base_name = (char*) base->data;
811 char *theme_name = ((IconTheme*)theme->data)->name;
812 char *dir_name = ((IconThemeDir*)dir->data)->name;
813 char *extension = (char*) ext->data;
814 char *file_name = malloc(strlen(base_name) + strlen(theme_name) +
815 strlen(dir_name) + strlen(icon_name) + strlen(extension) + 100);
816 // filename = directory/$(themename)/subdirectory/iconname.extension
817 sprintf(file_name, "%s/%s/%s/%s.%s", base_name, theme_name, dir_name, icon_name, extension);
818 //printf("checking %s\n", file_name);
819 if (g_file_test(file_name, G_FILE_TEST_EXISTS)) {
820 g_slist_free(basenames);
821 g_slist_free(extensions);
822 return file_name;
823 } else {
824 free(file_name);
825 file_name = NULL;
826 }
827 }
828 }
829 }
830 }
831 }
832
833 // Stage 2: best size match
834 // Contrary to the freedesktop spec, we are not choosing the closest icon in size, but the next larger icon
835 // otherwise the quality is usually crap (for size 22, if you can choose 16 or 32, you're better with 32)
836 // We do fallback to the closest size if we cannot find a larger or equal icon
837 int minimal_size = INT_MAX;
838 char *best_file_name = NULL;
839 int next_larger_size = -1;
840 char *next_larger = NULL;
841 for (theme = launcher->icon_themes; theme; theme = g_slist_next(theme)) {
842 GSList *dir;
843 for (dir = ((IconTheme*)theme->data)->list_directories; dir; dir = g_slist_next(dir)) {
844 GSList *base;
845 for (base = basenames; base; base = g_slist_next(base)) {
846 GSList *ext;
847 for (ext = extensions; ext; ext = g_slist_next(ext)) {
848 char *base_name = (char*) base->data;
849 char *theme_name = ((IconTheme*)theme->data)->name;
850 char *dir_name = ((IconThemeDir*)dir->data)->name;
851 char *extension = (char*) ext->data;
852 char *file_name = malloc(strlen(base_name) + strlen(theme_name) +
853 strlen(dir_name) + strlen(icon_name) + strlen(extension) + 100);
854 // filename = directory/$(themename)/subdirectory/iconname.extension
855 sprintf(file_name, "%s/%s/%s/%s.%s", base_name, theme_name, dir_name, icon_name, extension);
856 if (g_file_test(file_name, G_FILE_TEST_EXISTS)) {
857 if (directory_size_distance((IconThemeDir*)dir->data, size) < minimal_size) {
858 if (best_file_name) {
859 free(best_file_name);
860 best_file_name = NULL;
861 }
862 best_file_name = strdup(file_name);
863 minimal_size = directory_size_distance((IconThemeDir*)dir->data, size);
864 }
865 if (((IconThemeDir*)dir->data)->size >= size && (next_larger_size == -1 || ((IconThemeDir*)dir->data)->size < next_larger_size)) {
866 if (next_larger) {
867 free(next_larger);
868 next_larger = NULL;
869 }
870 next_larger = strdup(file_name);
871 next_larger_size = ((IconThemeDir*)dir->data)->size;
872 }
873 }
874 free(file_name);
875 }
876 }
877 }
878 }
879 if (next_larger) {
880 g_slist_free(basenames);
881 g_slist_free(extensions);
882 free(best_file_name);
883 return next_larger;
884 }
885 if (best_file_name) {
886 g_slist_free(basenames);
887 g_slist_free(extensions);
888 return best_file_name;
889 }
890
891 // Stage 3: look in unthemed icons
892 {
893 GSList *base;
894 for (base = basenames; base; base = g_slist_next(base)) {
895 GSList *ext;
896 for (ext = extensions; ext; ext = g_slist_next(ext)) {
897 char *base_name = (char*) base->data;
898 char *extension = (char*) ext->data;
899 char *file_name = malloc(strlen(base_name) + strlen(icon_name) +
900 strlen(extension) + 100);
901 // filename = directory/iconname.extension
902 sprintf(file_name, "%s/%s.%s", base_name, icon_name, extension);
903 //printf("checking %s\n", file_name);
904 if (g_file_test(file_name, G_FILE_TEST_EXISTS)) {
905 g_slist_free(basenames);
906 g_slist_free(extensions);
907 return file_name;
908 } else {
909 free(file_name);
910 file_name = NULL;
911 }
912 }
913 }
914 }
915
916 fprintf(stderr, "Could not find icon %s\n", icon_name);
917
918 return NULL;
919 }
This page took 0.0711270000000001 seconds and 4 git commands to generate.