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