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 int inside_desktop_entry
= 0;
428 while (getline(&line
, &line_size
, fp
) >= 0) {
429 int len
= strlen(line
);
432 line
[len
- 1] = '\0';
433 if (line
[0] == '[') {
434 inside_desktop_entry
= (strcmp(line
, "[Desktop Entry]") == 0);
436 if (inside_desktop_entry
&& parse_dektop_line(line
, &key
, &value
)) {
437 if (!entry
->name
&& strcmp(key
, "Name") == 0) {
438 entry
->name
= strdup(value
);
439 } else if (!entry
->exec
&& strcmp(key
, "Exec") == 0) {
440 entry
->exec
= strdup(value
);
441 } else if (!entry
->icon
&& strcmp(key
, "Icon") == 0) {
442 entry
->icon
= strdup(value
);
448 // entry->name, entry->icon, entry->exec will never be empty strings (can be NULL though)
450 expand_exec(entry
, path
);
456 void free_desktop_entry(DesktopEntry
*entry
)
463 void test_launcher_read_desktop_file()
465 fprintf(stdout
, "\033[1;33m");
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");
472 //TODO Use UTF8 when parsing the file
473 IconTheme
*load_theme(char *name
)
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
488 file_name
= g_build_filename(g_get_home_dir(), ".icons", name
, "index.theme", NULL
);
489 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
491 file_name
= g_build_filename("/usr/share/icons", name
, "index.theme", NULL
);
492 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
494 file_name
= g_build_filename("/usr/share/pixmaps", name
, "index.theme", NULL
);
495 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
506 if ((f
= fopen(file_name
, "rt")) == NULL
) {
507 fprintf(stderr
, "Could not open theme '%s'\n", file_name
);
513 theme
= calloc(1, sizeof(IconTheme
));
514 theme
->name
= strdup(name
);
515 theme
->list_inherits
= NULL
;
516 theme
->list_directories
= NULL
;
518 IconThemeDir
*current_dir
= NULL
;
519 int inside_header
= 1;
520 while (getline(&line
, &line_size
, f
) >= 0) {
523 int line_len
= strlen(line
);
525 if (line
[line_len
- 1] == '\n') {
526 line
[line_len
- 1] = '\0';
535 if (parse_theme_line(line
, &key
, &value
)) {
536 if (strcmp(key
, "Inherits") == 0) {
537 // value is like oxygen,wood,default
539 token
= strtok(value
, ",\n");
540 while (token
!= NULL
)
542 theme
->list_inherits
= g_slist_append(theme
->list_inherits
, strdup(token
));
543 token
= strtok(NULL
, ",\n");
545 } else if (strcmp(key
, "Directories") == 0) {
546 // value is like 48x48/apps,48x48/mimetypes,32x32/apps,scalable/apps,scalable/mimetypes
548 token
= strtok(value
, ",\n");
549 while (token
!= NULL
)
551 IconThemeDir
*dir
= calloc(1, sizeof(IconThemeDir
));
552 dir
->name
= strdup(token
);
553 dir
->max_size
= dir
->min_size
= dir
->size
= -1;
554 dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
556 theme
->list_directories
= g_slist_append(theme
->list_directories
, dir
);
557 token
= strtok(NULL
, ",\n");
561 } else if (current_dir
!= NULL
) {
562 if (parse_theme_line(line
, &key
, &value
)) {
563 if (strcmp(key
, "Size") == 0) {
565 sscanf(value
, "%d", ¤t_dir
->size
);
566 if (current_dir
->max_size
== -1)
567 current_dir
->max_size
= current_dir
->size
;
568 if (current_dir
->min_size
== -1)
569 current_dir
->min_size
= current_dir
->size
;
570 } else if (strcmp(key
, "MaxSize") == 0) {
572 sscanf(value
, "%d", ¤t_dir
->max_size
);
573 } else if (strcmp(key
, "MinSize") == 0) {
575 sscanf(value
, "%d", ¤t_dir
->min_size
);
576 } else if (strcmp(key
, "Threshold") == 0) {
578 sscanf(value
, "%d", ¤t_dir
->threshold
);
579 } else if (strcmp(key
, "Type") == 0) {
580 // value is Fixed, Scalable or Threshold : default to scalable for unknown Type.
581 if (strcmp(value
, "Fixed") == 0) {
582 current_dir
->type
= ICON_DIR_TYPE_FIXED
;
583 } else if (strcmp(value
, "Threshold") == 0) {
584 current_dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
586 current_dir
->type
= ICON_DIR_TYPE_SCALABLE
;
588 } else if (strcmp(key
, "Context") == 0) {
589 // usual values: Actions, Applications, Devices, FileSystems, MimeTypes
590 current_dir
->context
= strdup(value
);
595 if (line
[0] == '[' && line
[line_len
- 1] == ']' && strcmp(line
, "[Icon Theme]") != 0) {
598 line
[line_len
- 1] = '\0';
599 char *dir_name
= line
+ 1;
600 GSList
* dir_item
= theme
->list_directories
;
601 while (dir_item
!= NULL
)
603 IconThemeDir
*dir
= dir_item
->data
;
604 if (strcmp(dir
->name
, dir_name
) == 0) {
608 dir_item
= g_slist_next(dir_item
);
619 void free_icon_theme(IconTheme
*theme
)
623 for (l_inherits
= theme
->list_inherits
; l_inherits
; l_inherits
= l_inherits
->next
) {
624 free(l_inherits
->data
);
627 for (l_dir
= theme
->list_directories
; l_dir
; l_dir
= l_dir
->next
) {
628 IconThemeDir
*dir
= (IconThemeDir
*)l_dir
->data
;
635 void test_launcher_read_theme_file()
637 fprintf(stdout
, "\033[1;33m");
638 IconTheme
*theme
= load_theme("oxygen");
640 printf("Could not load theme\n");
643 printf("Loaded theme: %s\n", theme
->name
);
644 GSList
* item
= theme
->list_inherits
;
647 printf("Inherits:%s\n", (char*)item
->data
);
648 item
= g_slist_next(item
);
650 item
= theme
->list_directories
;
653 IconThemeDir
*dir
= item
->data
;
654 printf("Dir:%s Size=%d MinSize=%d MaxSize=%d Threshold=%d Type=%s Context=%s\n",
655 dir
->name
, dir
->size
, dir
->min_size
, dir
->max_size
, dir
->threshold
,
656 dir
->type
== ICON_DIR_TYPE_FIXED
? "Fixed" :
657 dir
->type
== ICON_DIR_TYPE_SCALABLE
? "Scalable" :
658 dir
->type
== ICON_DIR_TYPE_THRESHOLD
? "Threshold" : "?????",
660 item
= g_slist_next(item
);
662 fprintf(stdout
, "\033[0m");
666 // Populates the list_icons list
667 void launcher_load_icons(Launcher
*launcher
)
669 // Load apps (.desktop style launcher items)
670 GSList
* app
= launcher
->list_apps
;
671 while (app
!= NULL
) {
673 launcher_read_desktop_file(app
->data
, &entry
);
675 LauncherIcon
*launcherIcon
= calloc(1, sizeof(LauncherIcon
));
676 launcherIcon
->area
.parent
= launcher
;
677 launcherIcon
->area
.panel
= launcher
->area
.panel
;
678 launcherIcon
->area
._draw_foreground
= draw_launcher_icon
;
679 launcherIcon
->area
.size_mode
= SIZE_BY_CONTENT
;
680 launcherIcon
->area
._resize
= NULL
;
681 launcherIcon
->area
.resize
= 0;
682 launcherIcon
->area
.redraw
= 1;
683 launcherIcon
->area
.bg
= &g_array_index(backgrounds
, Background
, 0);
684 launcherIcon
->area
.on_screen
= 1;
685 launcherIcon
->area
._on_change_layout
= launcher_icon_on_change_layout
;
686 launcherIcon
->area
._get_tooltip_text
= launcher_icon_get_tooltip_text
;
687 launcherIcon
->is_app_desktop
= 1;
688 launcherIcon
->cmd
= strdup(entry
.exec
);
689 launcherIcon
->icon_name
= entry
.icon
? strdup(entry
.icon
) : strdup(ICON_FALLBACK
);
690 launcherIcon
->icon_size
= 1;
691 launcherIcon
->icon_tooltip
= entry
.name
? strdup(entry
.name
) : strdup(entry
.exec
);
692 free_desktop_entry(&entry
);
693 launcher
->list_icons
= g_slist_append(launcher
->list_icons
, launcherIcon
);
694 add_area(&launcherIcon
->area
);
696 app
= g_slist_next(app
);
701 // Populates the list_themes list
702 void launcher_load_themes(Launcher
*launcher
)
704 // load the user theme, all the inherited themes recursively (DFS), and the hicolor theme
705 // avoid inheritance loops
706 if (!icon_theme_name
) {
707 fprintf(stderr
, "Missing launcher theme, default to 'hicolor'.\n");
708 icon_theme_name
= strdup("hicolor");
710 fprintf(stderr
, "Loading %s. Icon theme :", icon_theme_name
);
713 GSList
*queue
= g_slist_append(NULL
, strdup(icon_theme_name
));
714 GSList
*queued
= g_slist_append(NULL
, strdup(icon_theme_name
));
716 int hicolor_loaded
= 0;
717 while (queue
|| !hicolor_loaded
) {
719 GSList
* queued_item
= queued
;
720 while (queued_item
!= NULL
) {
721 if (strcmp(queued_item
->data
, "hicolor") == 0) {
725 queued_item
= g_slist_next(queued_item
);
729 queue
= g_slist_append(queue
, strdup("hicolor"));
730 queued
= g_slist_append(queued
, strdup("hicolor"));
733 char *name
= queue
->data
;
734 queue
= g_slist_remove(queue
, name
);
736 fprintf(stderr
, " '%s',", name
);
737 IconTheme
*theme
= load_theme(name
);
739 launcher
->list_themes
= g_slist_append(launcher
->list_themes
, theme
);
741 GSList
* item
= theme
->list_inherits
;
745 char *parent
= item
->data
;
747 GSList
* queued_item
= queued
;
748 while (queued_item
!= NULL
) {
749 if (strcmp(queued_item
->data
, parent
) == 0) {
753 queued_item
= g_slist_next(queued_item
);
756 queue
= g_slist_insert(queue
, strdup(parent
), pos
);
758 queued
= g_slist_append(queued
, strdup(parent
));
760 item
= g_slist_next(item
);
764 fprintf(stderr
, "\n");
768 for (l
= queue
; l
; l
= l
->next
)
771 for (l
= queued
; l
; l
= l
->next
)
773 g_slist_free(queued
);
776 int directory_matches_size(IconThemeDir
*dir
, int size
)
778 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
779 return dir
->size
== size
;
780 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
781 return dir
->min_size
<= size
&& size
<= dir
->max_size
;
782 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
783 return dir
->size
- dir
->threshold
<= size
&& size
<= dir
->size
+ dir
->threshold
;
787 int directory_size_distance(IconThemeDir
*dir
, int size
)
789 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
790 return abs(dir
->size
- size
);
791 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
792 if (size
< dir
->min_size
) {
793 return dir
->min_size
- size
;
794 } else if (size
> dir
->max_size
) {
795 return size
- dir
->max_size
;
799 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
800 if (size
< dir
->size
- dir
->threshold
) {
801 return dir
->min_size
- size
;
802 } else if (size
> dir
->size
+ dir
->threshold
) {
803 return size
- dir
->max_size
;
810 // Returns the full path to an icon file (or NULL) given the icon name
811 char *icon_path(Launcher
*launcher
, const char *icon_name
, int size
)
813 if (icon_name
== NULL
)
816 // If the icon_name is already a path and the file exists, return it
817 if (strstr(icon_name
, "/") == icon_name
) {
818 if (g_file_test(icon_name
, G_FILE_TEST_EXISTS
))
819 return strdup(icon_name
);
824 GSList
*basenames
= NULL
;
825 char *home_icons
= g_build_filename(g_get_home_dir(), ".icons", NULL
);
826 basenames
= g_slist_append(basenames
, home_icons
);
827 basenames
= g_slist_append(basenames
, "/usr/share/icons");
828 basenames
= g_slist_append(basenames
, "/usr/share/pixmaps");
830 GSList
*extensions
= NULL
;
831 extensions
= g_slist_append(extensions
, ".png");
832 extensions
= g_slist_append(extensions
, ".xpm");
833 // if the icon name already contains one of the extensions (e.g. vlc.png instead of vlc) add a special entry
835 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
836 char *extension
= (char*) ext
->data
;
837 if (strlen(icon_name
) > strlen(extension
) &&
838 strcmp(extension
, icon_name
+ strlen(icon_name
) - strlen(extension
)) == 0) {
839 extensions
= g_slist_append(extensions
, "");
845 // Stage 1: exact size match
846 // the theme must have a higher priority than having an exact size match, so we will just use
847 // the code that searches for the best size match (it will find the exact size match if one exists)
849 for (theme = launcher->list_themes; theme; theme = g_slist_next(theme)) {
851 for (dir = ((IconTheme*)theme->data)->list_directories; dir; dir = g_slist_next(dir)) {
852 if (directory_matches_size((IconThemeDir*)dir->data, size)) {
854 for (base = basenames; base; base = g_slist_next(base)) {
856 for (ext = extensions; ext; ext = g_slist_next(ext)) {
857 char *base_name = (char*) base->data;
858 char *theme_name = ((IconTheme*)theme->data)->name;
859 char *dir_name = ((IconThemeDir*)dir->data)->name;
860 char *extension = (char*) ext->data;
861 char *file_name = malloc(strlen(base_name) + strlen(theme_name) +
862 strlen(dir_name) + strlen(icon_name) + strlen(extension) + 100);
863 // filename = directory/$(themename)/subdirectory/iconname.extension
864 sprintf(file_name, "%s/%s/%s/%s%s", base_name, theme_name, dir_name, icon_name, extension);
865 //printf("found exact: %s\n", file_name);
866 //printf("checking %s\n", file_name);
867 if (g_file_test(file_name, G_FILE_TEST_EXISTS)) {
868 g_slist_free(basenames);
869 g_slist_free(extensions);
884 // Stage 2: best size match
885 // Contrary to the freedesktop spec, we are not choosing the closest icon in size, but the next larger icon
886 // otherwise the quality is usually crap (for size 22, if you can choose 16 or 32, you're better with 32)
887 // We do fallback to the closest size if we cannot find a larger or equal icon
889 // These 3 variables are used for keeping the closest size match
890 int minimal_size
= INT_MAX
;
891 char *best_file_name
= NULL
;
892 GSList
*best_file_theme
= NULL
;
894 // These 3 variables are used for keeping the next larger match
895 int next_larger_size
= -1;
896 char *next_larger
= NULL
;
897 GSList
*next_larger_theme
= NULL
;
899 for (theme
= launcher
->list_themes
; theme
; theme
= g_slist_next(theme
)) {
901 for (dir
= ((IconTheme
*)theme
->data
)->list_directories
; dir
; dir
= g_slist_next(dir
)) {
903 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
905 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
906 char *base_name
= (char*) base
->data
;
907 char *theme_name
= ((IconTheme
*)theme
->data
)->name
;
908 char *dir_name
= ((IconThemeDir
*)dir
->data
)->name
;
909 char *extension
= (char*) ext
->data
;
910 char *file_name
= malloc(strlen(base_name
) + strlen(theme_name
) +
911 strlen(dir_name
) + strlen(icon_name
) + strlen(extension
) + 100);
912 // filename = directory/$(themename)/subdirectory/iconname.extension
913 sprintf(file_name
, "%s/%s/%s/%s%s", base_name
, theme_name
, dir_name
, icon_name
, extension
);
914 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
915 //printf("found: %s\n", file_name);
917 if (directory_size_distance((IconThemeDir
*)dir
->data
, size
) < minimal_size
&& (!best_file_theme
? 1 : theme
== best_file_theme
)) {
918 if (best_file_name
) {
919 free(best_file_name
);
920 best_file_name
= NULL
;
922 best_file_name
= strdup(file_name
);
923 minimal_size
= directory_size_distance((IconThemeDir
*)dir
->data
, size
);
924 best_file_theme
= theme
;
925 //printf("best_file_name = %s; minimal_size = %d\n", best_file_name, minimal_size);
928 if (((IconThemeDir
*)dir
->data
)->size
>= size
&&
929 (next_larger_size
== -1 || ((IconThemeDir
*)dir
->data
)->size
< next_larger_size
) &&
930 (!next_larger_theme
? 1 : theme
== next_larger_theme
)) {
935 next_larger
= strdup(file_name
);
936 next_larger_size
= ((IconThemeDir
*)dir
->data
)->size
;
937 next_larger_theme
= theme
;
938 //printf("next_larger = %s; next_larger_size = %d\n", next_larger, next_larger_size);
947 g_slist_free(basenames
);
948 g_slist_free(extensions
);
949 free(best_file_name
);
953 if (best_file_name
) {
954 g_slist_free(basenames
);
955 g_slist_free(extensions
);
957 return best_file_name
;
960 // Stage 3: look in unthemed icons
963 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
965 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
966 char *base_name
= (char*) base
->data
;
967 char *extension
= (char*) ext
->data
;
968 char *file_name
= malloc(strlen(base_name
) + strlen(icon_name
) +
969 strlen(extension
) + 100);
970 // filename = directory/iconname.extension
971 sprintf(file_name
, "%s/%s%s", base_name
, icon_name
, extension
);
972 //printf("checking %s\n", file_name);
973 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
974 g_slist_free(basenames
);
975 g_slist_free(extensions
);
986 fprintf(stderr
, "Could not find icon %s\n", icon_name
);
988 g_slist_free(basenames
);
989 g_slist_free(extensions
);