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