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