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