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