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