1 /**************************************************************************
4 * Copyright (C) 2010 (mrovi@interfete-web-club.com)
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.
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 **************************************************************************/
22 #include <cairo-xlib.h>
23 #include <pango/pangocairo.h>
36 int launcher_max_icon_size
;
37 char *icon_theme_name
;
38 XSettingsClient
*xsettings_client
;
40 #define ICON_FALLBACK "application-x-executable"
42 char *icon_path(Launcher
*launcher
, const char *icon_name
, int size
);
43 void launcher_load_themes(Launcher
*launcher
);
44 void free_desktop_entry(DesktopEntry
*entry
);
45 int launcher_read_desktop_file(const char *path
, DesktopEntry
*entry
);
46 Imlib_Image
scale_icon(Imlib_Image original
, int icon_size
);
47 void free_icon(Imlib_Image icon
);
48 void free_icon_theme(IconTheme
*theme
);
51 void default_launcher()
54 launcher_max_icon_size
= 0;
56 xsettings_client
= NULL
;
62 if (launcher_enabled
) {
63 // if XSETTINGS manager running, tint2 read the icon_theme_name.
64 xsettings_client
= xsettings_client_new(server
.dsp
, server
.screen
, xsettings_notify_cb
, NULL
, NULL
);
69 void init_launcher_panel(void *p
)
71 Panel
*panel
=(Panel
*)p
;
72 Launcher
*launcher
= &panel
->launcher
;
74 launcher
->area
.parent
= p
;
75 launcher
->area
.panel
= p
;
76 launcher
->area
._draw_foreground
= NULL
;
77 launcher
->area
.size_mode
= SIZE_BY_CONTENT
;
78 launcher
->area
._resize
= resize_launcher
;
79 launcher
->area
.resize
= 1;
80 launcher
->area
.redraw
= 1;
81 if (launcher
->area
.bg
== 0)
82 launcher
->area
.bg
= &g_array_index(backgrounds
, Background
, 0);
85 if (launcher
->list_apps
== NULL
)
88 launcher
->area
.on_screen
= 1;
91 launcher_load_themes(launcher
);
92 launcher_load_icons(launcher
);
96 void cleanup_launcher()
100 if (xsettings_client
)
101 xsettings_client_destroy(xsettings_client
);
102 for (i
= 0 ; i
< nb_panel
; i
++) {
103 Panel
*panel
= &panel1
[i
];
104 Launcher
*launcher
= &panel
->launcher
;
105 cleanup_launcher_theme(launcher
);
108 for (l
= launcher
->list_apps
; l
; l
= l
->next
) {
111 g_slist_free(launcher
->list_apps
);
112 launcher
->list_apps
= NULL
;
114 g_free(icon_theme_name
);
115 launcher_enabled
= 0;
119 void cleanup_launcher_theme(Launcher
*launcher
)
121 free_area(&launcher
->area
);
123 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
124 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
126 free_icon(launcherIcon
->icon_scaled
);
127 free_icon(launcherIcon
->icon_original
);
128 free(launcherIcon
->icon_name
);
129 free(launcherIcon
->icon_path
);
130 free(launcherIcon
->cmd
);
131 free(launcherIcon
->icon_tooltip
);
135 g_slist_free(launcher
->list_icons
);
137 for (l
= launcher
->list_themes
; l
; l
= l
->next
) {
138 IconTheme
*theme
= (IconTheme
*) l
->data
;
139 free_icon_theme(theme
);
142 g_slist_free(launcher
->list_themes
);
143 launcher
->list_icons
= launcher
->list_themes
= NULL
;
147 int resize_launcher(void *obj
)
149 Launcher
*launcher
= obj
;
151 int count
, icon_size
;
152 int icons_per_column
=1, icons_per_row
=1, marging
=0;
154 if (panel_horizontal
)
155 icon_size
= launcher
->area
.height
;
157 icon_size
= launcher
->area
.width
;
158 icon_size
= icon_size
- (2 * launcher
->area
.bg
->border
.width
) - (2 * launcher
->area
.paddingy
);
159 if (launcher_max_icon_size
> 0 && icon_size
> launcher_max_icon_size
)
160 icon_size
= launcher_max_icon_size
;
162 // Resize icons if necessary
163 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
164 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
165 if (launcherIcon
->icon_size
!= icon_size
|| !launcherIcon
->icon_original
) {
166 launcherIcon
->icon_size
= icon_size
;
167 launcherIcon
->area
.width
= launcherIcon
->icon_size
;
168 launcherIcon
->area
.height
= launcherIcon
->icon_size
;
170 // Get the path for an icon file with the new size
171 char *new_icon_path
= icon_path(launcher
, launcherIcon
->icon_name
, launcherIcon
->icon_size
);
172 if (!new_icon_path
) {
174 free_icon(launcherIcon
->icon_original
);
175 launcherIcon
->icon_original
= NULL
;
176 free_icon(launcherIcon
->icon_scaled
);
177 launcherIcon
->icon_scaled
= NULL
;
178 new_icon_path
= icon_path(launcher
, ICON_FALLBACK
, launcherIcon
->icon_size
);
180 launcherIcon
->icon_original
= imlib_load_image(new_icon_path
);
181 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, new_icon_path
);
184 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, icon_size
);
187 if (launcherIcon
->icon_path
&& strcmp(new_icon_path
, launcherIcon
->icon_path
) == 0) {
188 // If it's the same file just rescale
189 free_icon(launcherIcon
->icon_scaled
);
190 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, icon_size
);
192 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, launcherIcon
->icon_path
);
194 // Free the old files
195 free_icon(launcherIcon
->icon_original
);
196 free_icon(launcherIcon
->icon_scaled
);
197 // Load the new file and scale
198 launcherIcon
->icon_original
= imlib_load_image(new_icon_path
);
199 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, launcherIcon
->icon_size
);
200 free(launcherIcon
->icon_path
);
201 launcherIcon
->icon_path
= new_icon_path
;
202 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, launcherIcon
->icon_path
);
207 count
= g_slist_length(launcher
->list_icons
);
209 if (panel_horizontal
) {
210 if (!count
) launcher
->area
.width
= 0;
212 int height
= launcher
->area
.height
- 2*launcher
->area
.bg
->border
.width
- 2*launcher
->area
.paddingy
;
213 // here icons_per_column always higher than 0
214 icons_per_column
= (height
+launcher
->area
.paddingx
) / (icon_size
+launcher
->area
.paddingx
);
215 marging
= height
- (icons_per_column
-1)*(icon_size
+launcher
->area
.paddingx
) - icon_size
;
216 icons_per_row
= count
/ icons_per_column
+ (count%icons_per_column
!= 0);
217 launcher
->area
.width
= (2 * launcher
->area
.bg
->border
.width
) + (2 * launcher
->area
.paddingxlr
) + (icon_size
* icons_per_row
) + ((icons_per_row
-1) * launcher
->area
.paddingx
);
221 if (!count
) launcher
->area
.height
= 0;
223 int width
= launcher
->area
.width
- 2*launcher
->area
.bg
->border
.width
- 2*launcher
->area
.paddingy
;
224 // here icons_per_row always higher than 0
225 icons_per_row
= (width
+launcher
->area
.paddingx
) / (icon_size
+launcher
->area
.paddingx
);
226 marging
= width
- (icons_per_row
-1)*(icon_size
+launcher
->area
.paddingx
) - icon_size
;
227 icons_per_column
= count
/ icons_per_row
+ (count%icons_per_row
!= 0);
228 launcher
->area
.height
= (2 * launcher
->area
.bg
->border
.width
) + (2 * launcher
->area
.paddingxlr
) + (icon_size
* icons_per_column
) + ((icons_per_column
-1) * launcher
->area
.paddingx
);
233 int start
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingy
+ marging
/2;
234 if (panel_horizontal
) {
236 posx
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingxlr
;
240 posy
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingxlr
;
243 for (i
=1, l
= launcher
->list_icons
; l
; i
++, l
= l
->next
) {
244 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
246 launcherIcon
->y
= posy
;
247 launcherIcon
->x
= posx
;
248 //printf("launcher %d : %d,%d\n", i, posx, posy);
249 if (panel_horizontal
) {
250 if (i
% icons_per_column
)
251 posy
+= icon_size
+ launcher
->area
.paddingx
;
254 posx
+= (icon_size
+ launcher
->area
.paddingx
);
258 if (i
% icons_per_row
)
259 posx
+= icon_size
+ launcher
->area
.paddingx
;
262 posy
+= (icon_size
+ launcher
->area
.paddingx
);
269 // Here we override the default layout of the icons; normally Area layouts its children
270 // in a stack; we need to layout them in a kind of table
271 void launcher_icon_on_change_layout(void *obj
)
273 LauncherIcon
*launcherIcon
= (LauncherIcon
*)obj
;
274 launcherIcon
->area
.posy
= ((Area
*)launcherIcon
->area
.parent
)->posy
+ launcherIcon
->y
;
275 launcherIcon
->area
.posx
= ((Area
*)launcherIcon
->area
.parent
)->posx
+ launcherIcon
->x
;
278 const char* launcher_icon_get_tooltip_text(void *obj
)
280 LauncherIcon
*launcherIcon
= (LauncherIcon
*)obj
;
281 return launcherIcon
->icon_tooltip
;
284 void draw_launcher_icon(void *obj
, cairo_t
*c
)
286 LauncherIcon
*launcherIcon
= (LauncherIcon
*)obj
;
287 Imlib_Image icon_scaled
= launcherIcon
->icon_scaled
;
289 imlib_context_set_image (icon_scaled
);
290 if (server
.real_transparency
) {
291 render_image(launcherIcon
->area
.pix
, 0, 0, imlib_image_get_width(), imlib_image_get_height() );
293 imlib_context_set_drawable(launcherIcon
->area
.pix
);
294 imlib_render_image_on_drawable (0, 0);
298 Imlib_Image
scale_icon(Imlib_Image original
, int icon_size
)
300 Imlib_Image icon_scaled
;
302 imlib_context_set_image (original
);
303 icon_scaled
= imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), icon_size
, icon_size
);
305 icon_scaled
= imlib_create_image(icon_size
, icon_size
);
306 imlib_context_set_image (icon_scaled
);
307 imlib_context_set_color(255, 255, 255, 255);
308 imlib_image_fill_rectangle(0, 0, icon_size
, icon_size
);
313 void free_icon(Imlib_Image icon
)
316 imlib_context_set_image(icon
);
321 void launcher_action(LauncherIcon
*icon
)
323 char *cmd
= malloc(strlen(icon
->cmd
) + 10);
324 sprintf(cmd
, "(%s&)", icon
->cmd
);
329 /***************** Freedesktop app.desktop and icon theme handling *********************/
330 /* http://standards.freedesktop.org/desktop-entry-spec/ */
331 /* http://standards.freedesktop.org/icon-theme-spec/ */
333 // Splits line at first '=' and returns 1 if successful, and parts are not empty
334 // key and value point to the parts
335 int parse_dektop_line(char *line
, char **key
, char **value
)
340 for (p
= line
; *p
; p
++) {
350 if (found
&& (strlen(*key
) == 0 || strlen(*value
) == 0))
355 int parse_theme_line(char *line
, char **key
, char **value
)
357 return parse_dektop_line(line
, key
, value
);
360 void expand_exec(DesktopEntry
*entry
, const char *path
)
367 char *exec2
= malloc(strlen(entry
->exec
) + (entry
->name
? strlen(entry
->name
) : 1) + (entry
->icon
? strlen(entry
->icon
) : 1) + 100);
369 // p will never point to an escaped char
370 for (p
= entry
->exec
, q
= exec2
; *p
; p
++, q
++) {
374 // Copy the escaped char
375 if (*p
== '%') // For % we delete the backslash, i.e. write % over it
384 if (*p
== 'i' && entry
->icon
!= NULL
) {
385 sprintf(q
, "--icon '%s'", entry
->icon
);
386 q
+= strlen("--icon ''");
387 q
+= strlen(entry
->icon
);
388 q
--; // To balance the q++ in the for
389 } else if (*p
== 'c' && entry
->name
!= NULL
) {
390 sprintf(q
, "'%s'", entry
->name
);
392 q
+= strlen(entry
->name
);
393 q
--; // To balance the q++ in the for
394 } else if (*p
== 'c') {
395 sprintf(q
, "'%s'", path
);
398 q
--; // To balance the q++ in the for
400 // We don't care about other expansions
401 q
--; // Delete the last % from q
412 //TODO Use UTF8 when parsing the file
413 int launcher_read_desktop_file(const char *path
, DesktopEntry
*entry
)
420 entry
->name
= entry
->icon
= entry
->exec
= NULL
;
422 if ((fp
= fopen(path
, "rt")) == NULL
) {
423 fprintf(stderr
, "Could not open file %s\n", path
);
427 while (getline(&line
, &line_size
, fp
) >= 0) {
428 int len
= strlen(line
);
431 line
[len
- 1] = '\0';
432 if (parse_dektop_line(line
, &key
, &value
)) {
433 if (strcmp(key
, "Name") == 0) {
434 entry
->name
= strdup(value
);
435 } else if (strcmp(key
, "Exec") == 0) {
436 entry
->exec
= strdup(value
);
437 } else if (strcmp(key
, "Icon") == 0) {
438 entry
->icon
= strdup(value
);
444 // entry->name, entry->icon, entry->exec will never be empty strings (can be NULL though)
446 expand_exec(entry
, path
);
452 void free_desktop_entry(DesktopEntry
*entry
)
459 void test_launcher_read_desktop_file()
461 fprintf(stdout
, "\033[1;33m");
463 launcher_read_desktop_file("/usr/share/applications/firefox.desktop", &entry
);
464 printf("Name:%s Icon:%s Exec:%s\n", entry
.name
, entry
.icon
, entry
.exec
);
465 fprintf(stdout
, "\033[0m");
468 //TODO Use UTF8 when parsing the file
469 IconTheme
*load_theme(char *name
)
471 // Look for name/index.theme in $HOME/.icons, /usr/share/icons, /usr/share/pixmaps (stop at the first found)
472 // Parse index.theme -> list of IconThemeDir with attributes
484 file_name
= g_build_filename(g_get_home_dir(), ".icons", name
, "index.theme", NULL
);
485 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
487 file_name
= g_build_filename("/usr/share/icons", name
, "index.theme", NULL
);
488 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
490 file_name
= g_build_filename("/usr/share/pixmaps", name
, "index.theme", NULL
);
491 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
502 if ((f
= fopen(file_name
, "rt")) == NULL
) {
503 fprintf(stderr
, "Could not open theme '%s'\n", file_name
);
509 theme
= calloc(1, sizeof(IconTheme
));
510 theme
->name
= strdup(name
);
511 theme
->list_inherits
= NULL
;
512 theme
->list_directories
= NULL
;
514 IconThemeDir
*current_dir
= NULL
;
515 int inside_header
= 1;
516 while (getline(&line
, &line_size
, f
) >= 0) {
519 int line_len
= strlen(line
);
521 if (line
[line_len
- 1] == '\n') {
522 line
[line_len
- 1] = '\0';
531 if (parse_theme_line(line
, &key
, &value
)) {
532 if (strcmp(key
, "Inherits") == 0) {
533 // value is like oxygen,wood,default
535 token
= strtok(value
, ",\n");
536 while (token
!= NULL
)
538 theme
->list_inherits
= g_slist_append(theme
->list_inherits
, strdup(token
));
539 token
= strtok(NULL
, ",\n");
541 } else if (strcmp(key
, "Directories") == 0) {
542 // value is like 48x48/apps,48x48/mimetypes,32x32/apps,scalable/apps,scalable/mimetypes
544 token
= strtok(value
, ",\n");
545 while (token
!= NULL
)
547 IconThemeDir
*dir
= calloc(1, sizeof(IconThemeDir
));
548 dir
->name
= strdup(token
);
549 dir
->max_size
= dir
->min_size
= dir
->size
= -1;
550 dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
552 theme
->list_directories
= g_slist_append(theme
->list_directories
, dir
);
553 token
= strtok(NULL
, ",\n");
557 } else if (current_dir
!= NULL
) {
558 if (parse_theme_line(line
, &key
, &value
)) {
559 if (strcmp(key
, "Size") == 0) {
561 sscanf(value
, "%d", ¤t_dir
->size
);
562 if (current_dir
->max_size
== -1)
563 current_dir
->max_size
= current_dir
->size
;
564 if (current_dir
->min_size
== -1)
565 current_dir
->min_size
= current_dir
->size
;
566 } else if (strcmp(key
, "MaxSize") == 0) {
568 sscanf(value
, "%d", ¤t_dir
->max_size
);
569 } else if (strcmp(key
, "MinSize") == 0) {
571 sscanf(value
, "%d", ¤t_dir
->min_size
);
572 } else if (strcmp(key
, "Threshold") == 0) {
574 sscanf(value
, "%d", ¤t_dir
->threshold
);
575 } else if (strcmp(key
, "Type") == 0) {
576 // value is Fixed, Scalable or Threshold : default to scalable for unknown Type.
577 if (strcmp(value
, "Fixed") == 0) {
578 current_dir
->type
= ICON_DIR_TYPE_FIXED
;
579 } else if (strcmp(value
, "Threshold") == 0) {
580 current_dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
582 current_dir
->type
= ICON_DIR_TYPE_SCALABLE
;
584 } else if (strcmp(key
, "Context") == 0) {
585 // usual values: Actions, Applications, Devices, FileSystems, MimeTypes
586 current_dir
->context
= strdup(value
);
591 if (line
[0] == '[' && line
[line_len
- 1] == ']' && strcmp(line
, "[Icon Theme]") != 0) {
594 line
[line_len
- 1] = '\0';
595 char *dir_name
= line
+ 1;
596 GSList
* dir_item
= theme
->list_directories
;
597 while (dir_item
!= NULL
)
599 IconThemeDir
*dir
= dir_item
->data
;
600 if (strcmp(dir
->name
, dir_name
) == 0) {
604 dir_item
= g_slist_next(dir_item
);
615 void free_icon_theme(IconTheme
*theme
)
619 for (l_inherits
= theme
->list_inherits
; l_inherits
; l_inherits
= l_inherits
->next
) {
620 free(l_inherits
->data
);
623 for (l_dir
= theme
->list_directories
; l_dir
; l_dir
= l_dir
->next
) {
624 IconThemeDir
*dir
= (IconThemeDir
*)l_dir
->data
;
631 void test_launcher_read_theme_file()
633 fprintf(stdout
, "\033[1;33m");
634 IconTheme
*theme
= load_theme("oxygen");
636 printf("Could not load theme\n");
639 printf("Loaded theme: %s\n", theme
->name
);
640 GSList
* item
= theme
->list_inherits
;
643 printf("Inherits:%s\n", (char*)item
->data
);
644 item
= g_slist_next(item
);
646 item
= theme
->list_directories
;
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" : "?????",
656 item
= g_slist_next(item
);
658 fprintf(stdout
, "\033[0m");
662 // Populates the list_icons list
663 void launcher_load_icons(Launcher
*launcher
)
665 // Load apps (.desktop style launcher items)
666 GSList
* app
= launcher
->list_apps
;
667 while (app
!= NULL
) {
669 launcher_read_desktop_file(app
->data
, &entry
);
671 LauncherIcon
*launcherIcon
= calloc(1, sizeof(LauncherIcon
));
672 launcherIcon
->area
.parent
= launcher
;
673 launcherIcon
->area
.panel
= launcher
->area
.panel
;
674 launcherIcon
->area
._draw_foreground
= draw_launcher_icon
;
675 launcherIcon
->area
.size_mode
= SIZE_BY_CONTENT
;
676 launcherIcon
->area
._resize
= NULL
;
677 launcherIcon
->area
.resize
= 0;
678 launcherIcon
->area
.redraw
= 1;
679 launcherIcon
->area
.bg
= &g_array_index(backgrounds
, Background
, 0);
680 launcherIcon
->area
.on_screen
= 1;
681 launcherIcon
->area
._on_change_layout
= launcher_icon_on_change_layout
;
682 launcherIcon
->area
._get_tooltip_text
= launcher_icon_get_tooltip_text
;
683 launcherIcon
->is_app_desktop
= 1;
684 launcherIcon
->cmd
= strdup(entry
.exec
);
685 launcherIcon
->icon_name
= entry
.icon
? strdup(entry
.icon
) : strdup(ICON_FALLBACK
);
686 launcherIcon
->icon_size
= 1;
687 launcherIcon
->icon_tooltip
= entry
.name
? strdup(entry
.name
) : strdup(entry
.exec
);
688 free_desktop_entry(&entry
);
689 launcher
->list_icons
= g_slist_append(launcher
->list_icons
, launcherIcon
);
690 add_area(&launcherIcon
->area
);
692 app
= g_slist_next(app
);
697 // Populates the list_themes list
698 void launcher_load_themes(Launcher
*launcher
)
700 // load the user theme, all the inherited themes recursively (DFS), and the hicolor theme
701 // avoid inheritance loops
702 if (!icon_theme_name
) {
703 fprintf(stderr
, "Missing launcher theme, default to 'hicolor'.\n");
704 icon_theme_name
= "hicolor";
707 fprintf(stderr
, "Loading %s. Icon theme :", icon_theme_name
);
709 GSList
*queue
= g_slist_append(NULL
, strdup(icon_theme_name
));
710 GSList
*queued
= g_slist_append(NULL
, strdup(icon_theme_name
));
712 int hicolor_loaded
= 0;
713 while (queue
|| !hicolor_loaded
) {
715 GSList
* queued_item
= queued
;
716 while (queued_item
!= NULL
) {
717 if (strcmp(queued_item
->data
, "hicolor") == 0) {
721 queued_item
= g_slist_next(queued_item
);
725 queue
= g_slist_append(queue
, strdup("hicolor"));
726 queued
= g_slist_append(queued
, strdup("hicolor"));
729 char *name
= queue
->data
;
730 queue
= g_slist_remove(queue
, name
);
732 fprintf(stderr
, " '%s',", name
);
733 IconTheme
*theme
= load_theme(name
);
735 launcher
->list_themes
= g_slist_append(launcher
->list_themes
, theme
);
737 GSList
* item
= theme
->list_inherits
;
741 char *parent
= item
->data
;
743 GSList
* queued_item
= queued
;
744 while (queued_item
!= NULL
) {
745 if (strcmp(queued_item
->data
, parent
) == 0) {
749 queued_item
= g_slist_next(queued_item
);
752 queue
= g_slist_insert(queue
, strdup(parent
), pos
);
754 queued
= g_slist_append(queued
, strdup(parent
));
756 item
= g_slist_next(item
);
760 fprintf(stderr
, "\n");
764 for (l
= queue
; l
; l
= l
->next
)
767 for (l
= queued
; l
; l
= l
->next
)
769 g_slist_free(queued
);
772 int directory_matches_size(IconThemeDir
*dir
, int size
)
774 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
775 return dir
->size
== size
;
776 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
777 return dir
->min_size
<= size
&& size
<= dir
->max_size
;
778 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
779 return dir
->size
- dir
->threshold
<= size
&& size
<= dir
->size
+ dir
->threshold
;
783 int directory_size_distance(IconThemeDir
*dir
, int size
)
785 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
786 return abs(dir
->size
- size
);
787 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
788 if (size
< dir
->min_size
) {
789 return dir
->min_size
- size
;
790 } else if (size
> dir
->max_size
) {
791 return size
- dir
->max_size
;
795 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
796 if (size
< dir
->size
- dir
->threshold
) {
797 return dir
->min_size
- size
;
798 } else if (size
> dir
->size
+ dir
->threshold
) {
799 return size
- dir
->max_size
;
806 // Returns the full path to an icon file (or NULL) given the icon name
807 char *icon_path(Launcher
*launcher
, const char *icon_name
, int size
)
809 if (icon_name
== NULL
)
812 // If the icon_name is already a path and the file exists, return it
813 if (strstr(icon_name
, "/") == icon_name
) {
814 if (g_file_test(icon_name
, G_FILE_TEST_EXISTS
))
815 return strdup(icon_name
);
820 GSList
*basenames
= NULL
;
821 char *home_icons
= g_build_filename(g_get_home_dir(), ".icons", NULL
);
822 basenames
= g_slist_append(basenames
, home_icons
);
823 basenames
= g_slist_append(basenames
, "/usr/share/icons");
824 basenames
= g_slist_append(basenames
, "/usr/share/pixmaps");
826 GSList
*extensions
= NULL
;
827 extensions
= g_slist_append(extensions
, ".png");
828 extensions
= g_slist_append(extensions
, ".xpm");
829 // if the icon name already contains one of the extensions (e.g. vlc.png instead of vlc) add a special entry
831 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
832 char *extension
= (char*) ext
->data
;
833 if (strlen(icon_name
) > strlen(extension
) &&
834 strcmp(extension
, icon_name
+ strlen(icon_name
) - strlen(extension
)) == 0) {
835 extensions
= g_slist_append(extensions
, "");
841 // Stage 1: exact size match
842 // the theme must have a higher priority than having an exact size match, so we will just use
843 // the code that searches for the best size match (it will find the exact size match if one exists)
845 for (theme = launcher->list_themes; theme; theme = g_slist_next(theme)) {
847 for (dir = ((IconTheme*)theme->data)->list_directories; dir; dir = g_slist_next(dir)) {
848 if (directory_matches_size((IconThemeDir*)dir->data, size)) {
850 for (base = basenames; base; base = g_slist_next(base)) {
852 for (ext = extensions; ext; ext = g_slist_next(ext)) {
853 char *base_name = (char*) base->data;
854 char *theme_name = ((IconTheme*)theme->data)->name;
855 char *dir_name = ((IconThemeDir*)dir->data)->name;
856 char *extension = (char*) ext->data;
857 char *file_name = malloc(strlen(base_name) + strlen(theme_name) +
858 strlen(dir_name) + strlen(icon_name) + strlen(extension) + 100);
859 // filename = directory/$(themename)/subdirectory/iconname.extension
860 sprintf(file_name, "%s/%s/%s/%s%s", base_name, theme_name, dir_name, icon_name, extension);
861 //printf("found exact: %s\n", file_name);
862 //printf("checking %s\n", file_name);
863 if (g_file_test(file_name, G_FILE_TEST_EXISTS)) {
864 g_slist_free(basenames);
865 g_slist_free(extensions);
880 // Stage 2: best size match
881 // Contrary to the freedesktop spec, we are not choosing the closest icon in size, but the next larger icon
882 // otherwise the quality is usually crap (for size 22, if you can choose 16 or 32, you're better with 32)
883 // We do fallback to the closest size if we cannot find a larger or equal icon
885 // These 3 variables are used for keeping the closest size match
886 int minimal_size
= INT_MAX
;
887 char *best_file_name
= NULL
;
888 GSList
*best_file_theme
= NULL
;
890 // These 3 variables are used for keeping the next larger match
891 int next_larger_size
= -1;
892 char *next_larger
= NULL
;
893 GSList
*next_larger_theme
= NULL
;
895 for (theme
= launcher
->list_themes
; theme
; theme
= g_slist_next(theme
)) {
897 for (dir
= ((IconTheme
*)theme
->data
)->list_directories
; dir
; dir
= g_slist_next(dir
)) {
899 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
901 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
902 char *base_name
= (char*) base
->data
;
903 char *theme_name
= ((IconTheme
*)theme
->data
)->name
;
904 char *dir_name
= ((IconThemeDir
*)dir
->data
)->name
;
905 char *extension
= (char*) ext
->data
;
906 char *file_name
= malloc(strlen(base_name
) + strlen(theme_name
) +
907 strlen(dir_name
) + strlen(icon_name
) + strlen(extension
) + 100);
908 // filename = directory/$(themename)/subdirectory/iconname.extension
909 sprintf(file_name
, "%s/%s/%s/%s%s", base_name
, theme_name
, dir_name
, icon_name
, extension
);
910 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
911 //printf("found: %s\n", file_name);
913 if (directory_size_distance((IconThemeDir
*)dir
->data
, size
) < minimal_size
&& (!best_file_theme
? 1 : theme
== best_file_theme
)) {
914 if (best_file_name
) {
915 free(best_file_name
);
916 best_file_name
= NULL
;
918 best_file_name
= strdup(file_name
);
919 minimal_size
= directory_size_distance((IconThemeDir
*)dir
->data
, size
);
920 best_file_theme
= theme
;
921 //printf("best_file_name = %s; minimal_size = %d\n", best_file_name, minimal_size);
924 if (((IconThemeDir
*)dir
->data
)->size
>= size
&&
925 (next_larger_size
== -1 || ((IconThemeDir
*)dir
->data
)->size
< next_larger_size
) &&
926 (!next_larger_theme
? 1 : theme
== next_larger_theme
)) {
931 next_larger
= strdup(file_name
);
932 next_larger_size
= ((IconThemeDir
*)dir
->data
)->size
;
933 next_larger_theme
= theme
;
934 //printf("next_larger = %s; next_larger_size = %d\n", next_larger, next_larger_size);
943 g_slist_free(basenames
);
944 g_slist_free(extensions
);
945 free(best_file_name
);
949 if (best_file_name
) {
950 g_slist_free(basenames
);
951 g_slist_free(extensions
);
953 return best_file_name
;
956 // Stage 3: look in unthemed icons
959 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
961 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
962 char *base_name
= (char*) base
->data
;
963 char *extension
= (char*) ext
->data
;
964 char *file_name
= malloc(strlen(base_name
) + strlen(icon_name
) +
965 strlen(extension
) + 100);
966 // filename = directory/iconname.extension
967 sprintf(file_name
, "%s/%s%s", base_name
, icon_name
, extension
);
968 //printf("checking %s\n", file_name);
969 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
970 g_slist_free(basenames
);
971 g_slist_free(extensions
);
982 fprintf(stderr
, "Could not find icon %s\n", icon_name
);
984 g_slist_free(basenames
);
985 g_slist_free(extensions
);