]> Dogcows Code - chaz/tint2/blob - src/launcher/launcher.c
a813fea2982998f65a781a42973bd069af39d053
[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 int inside_desktop_entry = 0;
428 while (getline(&line, &line_size, fp) >= 0) {
429 int len = strlen(line);
430 if (len == 0)
431 continue;
432 line[len - 1] = '\0';
433 if (line[0] == '[') {
434 inside_desktop_entry = (strcmp(line, "[Desktop Entry]") == 0);
435 }
436 if (inside_desktop_entry && parse_dektop_line(line, &key, &value)) {
437 if (!entry->name && strcmp(key, "Name") == 0) {
438 entry->name = strdup(value);
439 } else if (!entry->exec && strcmp(key, "Exec") == 0) {
440 entry->exec = strdup(value);
441 } else if (!entry->icon && strcmp(key, "Icon") == 0) {
442 entry->icon = strdup(value);
443 }
444 }
445 }
446 fclose (fp);
447 // From this point:
448 // entry->name, entry->icon, entry->exec will never be empty strings (can be NULL though)
449
450 expand_exec(entry, path);
451
452 free(line);
453 return 1;
454 }
455
456 void free_desktop_entry(DesktopEntry *entry)
457 {
458 free(entry->name);
459 free(entry->icon);
460 free(entry->exec);
461 }
462
463 void test_launcher_read_desktop_file()
464 {
465 fprintf(stdout, "\033[1;33m");
466 DesktopEntry entry;
467 launcher_read_desktop_file("/usr/share/applications/firefox.desktop", &entry);
468 printf("Name:%s Icon:%s Exec:%s\n", entry.name, entry.icon, entry.exec);
469 fprintf(stdout, "\033[0m");
470 }
471
472 //TODO Use UTF8 when parsing the file
473 IconTheme *load_theme(char *name)
474 {
475 // Look for name/index.theme in $HOME/.icons, /usr/share/icons, /usr/share/pixmaps (stop at the first found)
476 // Parse index.theme -> list of IconThemeDir with attributes
477 // Return IconTheme*
478
479 IconTheme *theme;
480 char *file_name;
481 FILE *f;
482 char *line = NULL;
483 size_t line_size;
484
485 if (name == NULL)
486 return NULL;
487
488 file_name = g_build_filename(g_get_home_dir(), ".icons", name, "index.theme", NULL);
489 if (!g_file_test(file_name, G_FILE_TEST_EXISTS)) {
490 g_free (file_name);
491 file_name = g_build_filename("/usr/share/icons", name, "index.theme", NULL);
492 if (!g_file_test(file_name, G_FILE_TEST_EXISTS)) {
493 g_free (file_name);
494 file_name = g_build_filename("/usr/share/pixmaps", name, "index.theme", NULL);
495 if (!g_file_test(file_name, G_FILE_TEST_EXISTS)) {
496 g_free (file_name);
497 file_name = NULL;
498 }
499 }
500 }
501
502 if (!file_name) {
503 return NULL;
504 }
505
506 if ((f = fopen(file_name, "rt")) == NULL) {
507 fprintf(stderr, "Could not open theme '%s'\n", file_name);
508 return NULL;
509 }
510
511 g_free (file_name);
512
513 theme = calloc(1, sizeof(IconTheme));
514 theme->name = strdup(name);
515 theme->list_inherits = NULL;
516 theme->list_directories = NULL;
517
518 IconThemeDir *current_dir = NULL;
519 int inside_header = 1;
520 while (getline(&line, &line_size, f) >= 0) {
521 char *key, *value;
522
523 int line_len = strlen(line);
524 if (line_len >= 1) {
525 if (line[line_len - 1] == '\n') {
526 line[line_len - 1] = '\0';
527 line_len--;
528 }
529 }
530
531 if (line_len == 0)
532 continue;
533
534 if (inside_header) {
535 if (parse_theme_line(line, &key, &value)) {
536 if (strcmp(key, "Inherits") == 0) {
537 // value is like oxygen,wood,default
538 char *token;
539 token = strtok(value, ",\n");
540 while (token != NULL)
541 {
542 theme->list_inherits = g_slist_append(theme->list_inherits, strdup(token));
543 token = strtok(NULL, ",\n");
544 }
545 } else if (strcmp(key, "Directories") == 0) {
546 // value is like 48x48/apps,48x48/mimetypes,32x32/apps,scalable/apps,scalable/mimetypes
547 char *token;
548 token = strtok(value, ",\n");
549 while (token != NULL)
550 {
551 IconThemeDir *dir = calloc(1, sizeof(IconThemeDir));
552 dir->name = strdup(token);
553 dir->max_size = dir->min_size = dir->size = -1;
554 dir->type = ICON_DIR_TYPE_THRESHOLD;
555 dir->threshold = 2;
556 theme->list_directories = g_slist_append(theme->list_directories, dir);
557 token = strtok(NULL, ",\n");
558 }
559 }
560 }
561 } else if (current_dir != NULL) {
562 if (parse_theme_line(line, &key, &value)) {
563 if (strcmp(key, "Size") == 0) {
564 // value is like 24
565 sscanf(value, "%d", &current_dir->size);
566 if (current_dir->max_size == -1)
567 current_dir->max_size = current_dir->size;
568 if (current_dir->min_size == -1)
569 current_dir->min_size = current_dir->size;
570 } else if (strcmp(key, "MaxSize") == 0) {
571 // value is like 24
572 sscanf(value, "%d", &current_dir->max_size);
573 } else if (strcmp(key, "MinSize") == 0) {
574 // value is like 24
575 sscanf(value, "%d", &current_dir->min_size);
576 } else if (strcmp(key, "Threshold") == 0) {
577 // value is like 2
578 sscanf(value, "%d", &current_dir->threshold);
579 } else if (strcmp(key, "Type") == 0) {
580 // value is Fixed, Scalable or Threshold : default to scalable for unknown Type.
581 if (strcmp(value, "Fixed") == 0) {
582 current_dir->type = ICON_DIR_TYPE_FIXED;
583 } else if (strcmp(value, "Threshold") == 0) {
584 current_dir->type = ICON_DIR_TYPE_THRESHOLD;
585 } else {
586 current_dir->type = ICON_DIR_TYPE_SCALABLE;
587 }
588 } else if (strcmp(key, "Context") == 0) {
589 // usual values: Actions, Applications, Devices, FileSystems, MimeTypes
590 current_dir->context = strdup(value);
591 }
592 }
593 }
594
595 if (line[0] == '[' && line[line_len - 1] == ']' && strcmp(line, "[Icon Theme]") != 0) {
596 inside_header = 0;
597 current_dir = NULL;
598 line[line_len - 1] = '\0';
599 char *dir_name = line + 1;
600 GSList* dir_item = theme->list_directories;
601 while (dir_item != NULL)
602 {
603 IconThemeDir *dir = dir_item->data;
604 if (strcmp(dir->name, dir_name) == 0) {
605 current_dir = dir;
606 break;
607 }
608 dir_item = g_slist_next(dir_item);
609 }
610 }
611 }
612 fclose(f);
613
614 free(line);
615
616 return theme;
617 }
618
619 void free_icon_theme(IconTheme *theme)
620 {
621 free(theme->name);
622 GSList *l_inherits;
623 for (l_inherits = theme->list_inherits; l_inherits ; l_inherits = l_inherits->next) {
624 free(l_inherits->data);
625 }
626 GSList *l_dir;
627 for (l_dir = theme->list_directories; l_dir ; l_dir = l_dir->next) {
628 IconThemeDir *dir = (IconThemeDir *)l_dir->data;
629 free(dir->name);
630 free(dir->context);
631 free(l_dir->data);
632 }
633 }
634
635 void test_launcher_read_theme_file()
636 {
637 fprintf(stdout, "\033[1;33m");
638 IconTheme *theme = load_theme("oxygen");
639 if (!theme) {
640 printf("Could not load theme\n");
641 return;
642 }
643 printf("Loaded theme: %s\n", theme->name);
644 GSList* item = theme->list_inherits;
645 while (item != NULL)
646 {
647 printf("Inherits:%s\n", (char*)item->data);
648 item = g_slist_next(item);
649 }
650 item = theme->list_directories;
651 while (item != NULL)
652 {
653 IconThemeDir *dir = item->data;
654 printf("Dir:%s Size=%d MinSize=%d MaxSize=%d Threshold=%d Type=%s Context=%s\n",
655 dir->name, dir->size, dir->min_size, dir->max_size, dir->threshold,
656 dir->type == ICON_DIR_TYPE_FIXED ? "Fixed" :
657 dir->type == ICON_DIR_TYPE_SCALABLE ? "Scalable" :
658 dir->type == ICON_DIR_TYPE_THRESHOLD ? "Threshold" : "?????",
659 dir->context);
660 item = g_slist_next(item);
661 }
662 fprintf(stdout, "\033[0m");
663 }
664
665
666 // Populates the list_icons list
667 void launcher_load_icons(Launcher *launcher)
668 {
669 // Load apps (.desktop style launcher items)
670 GSList* app = launcher->list_apps;
671 while (app != NULL) {
672 DesktopEntry entry;
673 launcher_read_desktop_file(app->data, &entry);
674 if (entry.exec) {
675 LauncherIcon *launcherIcon = calloc(1, sizeof(LauncherIcon));
676 launcherIcon->area.parent = launcher;
677 launcherIcon->area.panel = launcher->area.panel;
678 launcherIcon->area._draw_foreground = draw_launcher_icon;
679 launcherIcon->area.size_mode = SIZE_BY_CONTENT;
680 launcherIcon->area._resize = NULL;
681 launcherIcon->area.resize = 0;
682 launcherIcon->area.redraw = 1;
683 launcherIcon->area.bg = &g_array_index(backgrounds, Background, 0);
684 launcherIcon->area.on_screen = 1;
685 launcherIcon->area._on_change_layout = launcher_icon_on_change_layout;
686 launcherIcon->area._get_tooltip_text = launcher_icon_get_tooltip_text;
687 launcherIcon->is_app_desktop = 1;
688 launcherIcon->cmd = strdup(entry.exec);
689 launcherIcon->icon_name = entry.icon ? strdup(entry.icon) : strdup(ICON_FALLBACK);
690 launcherIcon->icon_size = 1;
691 launcherIcon->icon_tooltip = entry.name ? strdup(entry.name) : strdup(entry.exec);
692 free_desktop_entry(&entry);
693 launcher->list_icons = g_slist_append(launcher->list_icons, launcherIcon);
694 add_area(&launcherIcon->area);
695 }
696 app = g_slist_next(app);
697 }
698 }
699
700
701 // Populates the list_themes list
702 void launcher_load_themes(Launcher *launcher)
703 {
704 // load the user theme, all the inherited themes recursively (DFS), and the hicolor theme
705 // avoid inheritance loops
706 if (!icon_theme_name) {
707 fprintf(stderr, "Missing launcher theme, default to 'hicolor'.\n");
708 icon_theme_name = strdup("hicolor");
709 } else {
710 fprintf(stderr, "Loading %s. Icon theme :", icon_theme_name);
711 }
712
713 GSList *queue = g_slist_append(NULL, strdup(icon_theme_name));
714 GSList *queued = g_slist_append(NULL, strdup(icon_theme_name));
715
716 int hicolor_loaded = 0;
717 while (queue || !hicolor_loaded) {
718 if (!queue) {
719 GSList* queued_item = queued;
720 while (queued_item != NULL) {
721 if (strcmp(queued_item->data, "hicolor") == 0) {
722 hicolor_loaded = 1;
723 break;
724 }
725 queued_item = g_slist_next(queued_item);
726 }
727 if (hicolor_loaded)
728 break;
729 queue = g_slist_append(queue, strdup("hicolor"));
730 queued = g_slist_append(queued, strdup("hicolor"));
731 }
732
733 char *name = queue->data;
734 queue = g_slist_remove(queue, name);
735
736 fprintf(stderr, " '%s',", name);
737 IconTheme *theme = load_theme(name);
738 if (theme != NULL) {
739 launcher->list_themes = g_slist_append(launcher->list_themes, theme);
740
741 GSList* item = theme->list_inherits;
742 int pos = 0;
743 while (item != NULL)
744 {
745 char *parent = item->data;
746 int duplicate = 0;
747 GSList* queued_item = queued;
748 while (queued_item != NULL) {
749 if (strcmp(queued_item->data, parent) == 0) {
750 duplicate = 1;
751 break;
752 }
753 queued_item = g_slist_next(queued_item);
754 }
755 if (!duplicate) {
756 queue = g_slist_insert(queue, strdup(parent), pos);
757 pos++;
758 queued = g_slist_append(queued, strdup(parent));
759 }
760 item = g_slist_next(item);
761 }
762 }
763 }
764 fprintf(stderr, "\n");
765
766 // Free the queue
767 GSList *l;
768 for (l = queue; l ; l = l->next)
769 free(l->data);
770 g_slist_free(queue);
771 for (l = queued; l ; l = l->next)
772 free(l->data);
773 g_slist_free(queued);
774 }
775
776 int directory_matches_size(IconThemeDir *dir, int size)
777 {
778 if (dir->type == ICON_DIR_TYPE_FIXED) {
779 return dir->size == size;
780 } else if (dir->type == ICON_DIR_TYPE_SCALABLE) {
781 return dir->min_size <= size && size <= dir->max_size;
782 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
783 return dir->size - dir->threshold <= size && size <= dir->size + dir->threshold;
784 }
785 }
786
787 int directory_size_distance(IconThemeDir *dir, int size)
788 {
789 if (dir->type == ICON_DIR_TYPE_FIXED) {
790 return abs(dir->size - size);
791 } else if (dir->type == ICON_DIR_TYPE_SCALABLE) {
792 if (size < dir->min_size) {
793 return dir->min_size - size;
794 } else if (size > dir->max_size) {
795 return size - dir->max_size;
796 } else {
797 return 0;
798 }
799 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
800 if (size < dir->size - dir->threshold) {
801 return dir->min_size - size;
802 } else if (size > dir->size + dir->threshold) {
803 return size - dir->max_size;
804 } else {
805 return 0;
806 }
807 }
808 }
809
810 // Returns the full path to an icon file (or NULL) given the icon name
811 char *icon_path(Launcher *launcher, const char *icon_name, int size)
812 {
813 if (icon_name == NULL)
814 return NULL;
815
816 // If the icon_name is already a path and the file exists, return it
817 if (strstr(icon_name, "/") == icon_name) {
818 if (g_file_test(icon_name, G_FILE_TEST_EXISTS))
819 return strdup(icon_name);
820 else
821 return NULL;
822 }
823
824 GSList *basenames = NULL;
825 char *home_icons = g_build_filename(g_get_home_dir(), ".icons", NULL);
826 basenames = g_slist_append(basenames, home_icons);
827 basenames = g_slist_append(basenames, "/usr/share/icons");
828 basenames = g_slist_append(basenames, "/usr/share/pixmaps");
829
830 GSList *extensions = NULL;
831 extensions = g_slist_append(extensions, ".png");
832 extensions = g_slist_append(extensions, ".xpm");
833 // if the icon name already contains one of the extensions (e.g. vlc.png instead of vlc) add a special entry
834 GSList *ext;
835 for (ext = extensions; ext; ext = g_slist_next(ext)) {
836 char *extension = (char*) ext->data;
837 if (strlen(icon_name) > strlen(extension) &&
838 strcmp(extension, icon_name + strlen(icon_name) - strlen(extension)) == 0) {
839 extensions = g_slist_append(extensions, "");
840 break;
841 }
842 }
843
844 GSList *theme;
845 // Stage 1: exact size match
846 // the theme must have a higher priority than having an exact size match, so we will just use
847 // the code that searches for the best size match (it will find the exact size match if one exists)
848 /*
849 for (theme = launcher->list_themes; theme; theme = g_slist_next(theme)) {
850 GSList *dir;
851 for (dir = ((IconTheme*)theme->data)->list_directories; dir; dir = g_slist_next(dir)) {
852 if (directory_matches_size((IconThemeDir*)dir->data, size)) {
853 GSList *base;
854 for (base = basenames; base; base = g_slist_next(base)) {
855 GSList *ext;
856 for (ext = extensions; ext; ext = g_slist_next(ext)) {
857 char *base_name = (char*) base->data;
858 char *theme_name = ((IconTheme*)theme->data)->name;
859 char *dir_name = ((IconThemeDir*)dir->data)->name;
860 char *extension = (char*) ext->data;
861 char *file_name = malloc(strlen(base_name) + strlen(theme_name) +
862 strlen(dir_name) + strlen(icon_name) + strlen(extension) + 100);
863 // filename = directory/$(themename)/subdirectory/iconname.extension
864 sprintf(file_name, "%s/%s/%s/%s%s", base_name, theme_name, dir_name, icon_name, extension);
865 //printf("found exact: %s\n", file_name);
866 //printf("checking %s\n", file_name);
867 if (g_file_test(file_name, G_FILE_TEST_EXISTS)) {
868 g_slist_free(basenames);
869 g_slist_free(extensions);
870 g_free(home_icons);
871 return file_name;
872 } else {
873 free(file_name);
874 file_name = NULL;
875 }
876 }
877 }
878 }
879 }
880 }
881 g_free (file_name);
882 */
883
884 // Stage 2: best size match
885 // Contrary to the freedesktop spec, we are not choosing the closest icon in size, but the next larger icon
886 // otherwise the quality is usually crap (for size 22, if you can choose 16 or 32, you're better with 32)
887 // We do fallback to the closest size if we cannot find a larger or equal icon
888
889 // These 3 variables are used for keeping the closest size match
890 int minimal_size = INT_MAX;
891 char *best_file_name = NULL;
892 GSList *best_file_theme = NULL;
893
894 // These 3 variables are used for keeping the next larger match
895 int next_larger_size = -1;
896 char *next_larger = NULL;
897 GSList *next_larger_theme = NULL;
898
899 for (theme = launcher->list_themes; theme; theme = g_slist_next(theme)) {
900 GSList *dir;
901 for (dir = ((IconTheme*)theme->data)->list_directories; dir; dir = g_slist_next(dir)) {
902 GSList *base;
903 for (base = basenames; base; base = g_slist_next(base)) {
904 GSList *ext;
905 for (ext = extensions; ext; ext = g_slist_next(ext)) {
906 char *base_name = (char*) base->data;
907 char *theme_name = ((IconTheme*)theme->data)->name;
908 char *dir_name = ((IconThemeDir*)dir->data)->name;
909 char *extension = (char*) ext->data;
910 char *file_name = malloc(strlen(base_name) + strlen(theme_name) +
911 strlen(dir_name) + strlen(icon_name) + strlen(extension) + 100);
912 // filename = directory/$(themename)/subdirectory/iconname.extension
913 sprintf(file_name, "%s/%s/%s/%s%s", base_name, theme_name, dir_name, icon_name, extension);
914 if (g_file_test(file_name, G_FILE_TEST_EXISTS)) {
915 //printf("found: %s\n", file_name);
916 // Closest match
917 if (directory_size_distance((IconThemeDir*)dir->data, size) < minimal_size && (!best_file_theme ? 1 : theme == best_file_theme)) {
918 if (best_file_name) {
919 free(best_file_name);
920 best_file_name = NULL;
921 }
922 best_file_name = strdup(file_name);
923 minimal_size = directory_size_distance((IconThemeDir*)dir->data, size);
924 best_file_theme = theme;
925 //printf("best_file_name = %s; minimal_size = %d\n", best_file_name, minimal_size);
926 }
927 // Next larger match
928 if (((IconThemeDir*)dir->data)->size >= size &&
929 (next_larger_size == -1 || ((IconThemeDir*)dir->data)->size < next_larger_size) &&
930 (!next_larger_theme ? 1 : theme == next_larger_theme)) {
931 if (next_larger) {
932 free(next_larger);
933 next_larger = NULL;
934 }
935 next_larger = strdup(file_name);
936 next_larger_size = ((IconThemeDir*)dir->data)->size;
937 next_larger_theme = theme;
938 //printf("next_larger = %s; next_larger_size = %d\n", next_larger, next_larger_size);
939 }
940 }
941 free(file_name);
942 }
943 }
944 }
945 }
946 if (next_larger) {
947 g_slist_free(basenames);
948 g_slist_free(extensions);
949 free(best_file_name);
950 g_free(home_icons);
951 return next_larger;
952 }
953 if (best_file_name) {
954 g_slist_free(basenames);
955 g_slist_free(extensions);
956 g_free(home_icons);
957 return best_file_name;
958 }
959
960 // Stage 3: look in unthemed icons
961 {
962 GSList *base;
963 for (base = basenames; base; base = g_slist_next(base)) {
964 GSList *ext;
965 for (ext = extensions; ext; ext = g_slist_next(ext)) {
966 char *base_name = (char*) base->data;
967 char *extension = (char*) ext->data;
968 char *file_name = malloc(strlen(base_name) + strlen(icon_name) +
969 strlen(extension) + 100);
970 // filename = directory/iconname.extension
971 sprintf(file_name, "%s/%s%s", base_name, icon_name, extension);
972 //printf("checking %s\n", file_name);
973 if (g_file_test(file_name, G_FILE_TEST_EXISTS)) {
974 g_slist_free(basenames);
975 g_slist_free(extensions);
976 g_free(home_icons);
977 return file_name;
978 } else {
979 free(file_name);
980 file_name = NULL;
981 }
982 }
983 }
984 }
985
986 fprintf(stderr, "Could not find icon %s\n", icon_name);
987
988 g_slist_free(basenames);
989 g_slist_free(extensions);
990 g_free(home_icons);
991 return NULL;
992 }
993
This page took 0.085558 seconds and 3 git commands to generate.