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