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