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