]>
Dogcows Code - chaz/tint2/blob - src/launcher/launcher.c
e0266d155405cefab3d8a57f8b824c451abf7025
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 GSList
*icon_themes
= 0;
39 #define ICON_FALLBACK "exec"
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
);
49 void default_launcher()
52 launcher_max_icon_size
= 0;
61 void init_launcher_panel(void *p
)
63 Panel
*panel
=(Panel
*)p
;
64 Launcher
*launcher
= &panel
->launcher
;
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;
75 if (launcher
->list_apps
== NULL
)
78 launcher
->area
.on_screen
= 1;
81 fprintf(stderr
, "Loading themes...\n");
82 launcher_load_themes(launcher
);
83 fprintf(stderr
, "Done\n");
85 // Load apps (.desktop style launcher items)
86 GSList
* app
= launcher
->list_apps
;
89 launcher_read_desktop_file(app
->data
, &entry
);
91 LauncherIcon
*launcherIcon
= calloc(1, sizeof(LauncherIcon
));
92 launcherIcon
->is_app_desktop
= 1;
93 launcherIcon
->cmd
= strdup(entry
.exec
);
94 launcherIcon
->icon_name
= entry
.icon
? strdup(entry
.icon
) : strdup(ICON_FALLBACK
);
95 launcherIcon
->icon_size
= 1;
96 free_desktop_entry(&entry
);
97 launcher
->list_icons
= g_slist_append(launcher
->list_icons
, launcherIcon
);
99 app
= g_slist_next(app
);
104 void cleanup_launcher()
108 for (i
= 0 ; i
< nb_panel
; i
++) {
109 Panel
*panel
= &panel1
[i
];
110 Launcher
*launcher
= &panel
->launcher
;
111 free_area(&launcher
->area
);
113 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
114 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
116 free_icon(launcherIcon
->icon_scaled
);
117 free_icon(launcherIcon
->icon_original
);
118 free(launcherIcon
->icon_name
);
119 free(launcherIcon
->icon_path
);
120 free(launcherIcon
->cmd
);
124 g_slist_free(launcher
->list_icons
);
126 for (l
= launcher
->list_apps
; l
; l
= l
->next
) {
129 g_slist_free(launcher
->list_apps
);
131 for (l
= launcher
->icon_themes
; l
; l
= l
->next
) {
132 IconTheme
*theme
= (IconTheme
*) l
->data
;
133 free_icon_theme(theme
);
136 g_slist_free(launcher
->icon_themes
);
138 for (l
= launcher
->icon_theme_names
; l
; l
= l
->next
) {
141 g_slist_free(launcher
->icon_theme_names
);
142 launcher
->list_apps
= launcher
->list_icons
= launcher
->icon_theme_names
= launcher
->icon_themes
= NULL
;
144 launcher_enabled
= 0;
148 int resize_launcher(void *obj
)
150 Launcher
*launcher
= obj
;
152 int count
, icon_size
;
153 int icons_per_column
=1, icons_per_row
=1, marging
=0;
155 if (panel_horizontal
)
156 icon_size
= launcher
->area
.height
;
158 icon_size
= launcher
->area
.width
;
159 icon_size
= icon_size
- (2 * launcher
->area
.bg
->border
.width
) - (2 * launcher
->area
.paddingy
);
160 if (launcher_max_icon_size
> 0 && icon_size
> launcher_max_icon_size
)
161 icon_size
= launcher_max_icon_size
;
163 // Resize icons if necessary
164 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
165 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
166 if (launcherIcon
->icon_size
!= icon_size
|| !launcherIcon
->icon_original
) {
167 launcherIcon
->icon_size
= icon_size
;
169 // Get the path for an icon file with the new size
170 char *new_icon_path
= icon_path(launcher
, launcherIcon
->icon_name
, launcherIcon
->icon_size
);
171 if (!new_icon_path
) {
173 free_icon(launcherIcon
->icon_original
);
174 launcherIcon
->icon_original
= NULL
;
175 free_icon(launcherIcon
->icon_scaled
);
176 launcherIcon
->icon_scaled
= NULL
;
177 new_icon_path
= icon_path(launcher
, ICON_FALLBACK
, launcherIcon
->icon_size
);
179 launcherIcon
->icon_original
= imlib_load_image(new_icon_path
);
180 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, new_icon_path
);
183 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, icon_size
);
186 if (launcherIcon
->icon_path
&& strcmp(new_icon_path
, launcherIcon
->icon_path
) == 0) {
187 // If it's the same file just rescale
188 free_icon(launcherIcon
->icon_scaled
);
189 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, icon_size
);
191 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, launcherIcon
->icon_path
);
193 // Free the old files
194 free_icon(launcherIcon
->icon_original
);
195 free_icon(launcherIcon
->icon_scaled
);
196 // Load the new file and scale
197 launcherIcon
->icon_original
= imlib_load_image(new_icon_path
);
198 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, launcherIcon
->icon_size
);
199 free(launcherIcon
->icon_path
);
200 launcherIcon
->icon_path
= new_icon_path
;
201 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, launcherIcon
->icon_path
);
207 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
211 if (panel_horizontal
) {
212 if (!count
) launcher
->area
.width
= 0;
214 int height
= launcher
->area
.height
- 2*launcher
->area
.bg
->border
.width
- 2*launcher
->area
.paddingy
;
215 // here icons_per_column always higher than 0
216 icons_per_column
= (height
+launcher
->area
.paddingx
) / (icon_size
+launcher
->area
.paddingx
);
217 marging
= height
- (icons_per_column
-1)*(icon_size
+launcher
->area
.paddingx
) - icon_size
;
218 icons_per_row
= count
/ icons_per_column
+ (count%icons_per_column
!= 0);
219 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
);
223 if (!count
) launcher
->area
.height
= 0;
225 int width
= launcher
->area
.width
- 2*launcher
->area
.bg
->border
.width
- 2*launcher
->area
.paddingy
;
226 // here icons_per_row always higher than 0
227 icons_per_row
= (width
+launcher
->area
.paddingx
) / (icon_size
+launcher
->area
.paddingx
);
228 marging
= width
- (icons_per_row
-1)*(icon_size
+launcher
->area
.paddingx
) - icon_size
;
229 icons_per_column
= count
/ icons_per_row
+ (count%icons_per_row
!= 0);
230 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 int start
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingy
;// +marging/2;
236 if (panel_horizontal
) {
238 posx
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingxlr
;
242 posy
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingxlr
;
245 for (i
=1, l
= launcher
->list_icons
; l
; i
++, l
= l
->next
) {
246 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
248 launcherIcon
->y
= posy
;
249 launcherIcon
->x
= posx
;
250 if (panel_horizontal
) {
251 if (i
% icons_per_column
)
252 posy
+= icon_size
+ launcher
->area
.paddingx
;
255 posx
+= (icon_size
+ launcher
->area
.paddingx
);
259 if (i
% icons_per_row
)
260 posx
+= icon_size
+ launcher
->area
.paddingx
;
263 posy
+= (icon_size
+ launcher
->area
.paddingx
);
271 void draw_launcher(void *obj
, cairo_t
*c
)
273 Launcher
*launcher
= obj
;
275 if (launcher
->list_icons
== 0) return;
277 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
278 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
279 int pos_x
= launcherIcon
->x
;
280 int pos_y
= launcherIcon
->y
;
281 Imlib_Image icon_scaled
= launcherIcon
->icon_scaled
;
283 imlib_context_set_image (icon_scaled
);
284 if (server
.real_transparency
) {
285 render_image(launcher
->area
.pix
, pos_x
, pos_y
, imlib_image_get_width(), imlib_image_get_height() );
288 imlib_context_set_drawable(launcher
->area
.pix
);
289 imlib_render_image_on_drawable (pos_x
, pos_y
);
294 Imlib_Image
scale_icon(Imlib_Image original
, int icon_size
)
296 Imlib_Image icon_scaled
;
298 imlib_context_set_image (original
);
299 icon_scaled
= imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), icon_size
, icon_size
);
301 icon_scaled
= imlib_create_image(icon_size
, icon_size
);
302 imlib_context_set_image (icon_scaled
);
303 imlib_context_set_color(255, 255, 255, 255);
304 imlib_image_fill_rectangle(0, 0, icon_size
, icon_size
);
309 void free_icon(Imlib_Image icon
)
312 imlib_context_set_image(icon
);
317 void launcher_action(LauncherIcon
*icon
)
319 char *cmd
= malloc(strlen(icon
->cmd
) + 10);
320 sprintf(cmd
, "(%s&)", icon
->cmd
);
325 /***************** Freedesktop app.desktop and icon theme handling *********************/
326 /* http://standards.freedesktop.org/desktop-entry-spec/ */
327 /* http://standards.freedesktop.org/icon-theme-spec/ */
329 // Splits line at first '=' and returns 1 if successful, and parts are not empty
330 // key and value point to the parts
331 int parse_dektop_line(char *line
, char **key
, char **value
)
336 for (p
= line
; *p
; p
++) {
346 if (found
&& (strlen(*key
) == 0 || strlen(*value
) == 0))
351 int parse_theme_line(char *line
, char **key
, char **value
)
353 return parse_dektop_line(line
, key
, value
);
356 void expand_exec(DesktopEntry
*entry
, const char *path
)
363 char *exec2
= malloc(strlen(entry
->exec
) + (entry
->name
? strlen(entry
->name
) : 1) + (entry
->icon
? strlen(entry
->icon
) : 1) + 100);
365 // p will never point to an escaped char
366 for (p
= entry
->exec
, q
= exec2
; *p
; p
++, q
++) {
370 // Copy the escaped char
371 if (*p
== '%') // For % we delete the backslash, i.e. write % over it
380 if (*p
== 'i' && entry
->icon
!= NULL
) {
381 sprintf(q
, "--icon '%s'", entry
->icon
);
382 q
+= strlen("--icon ''");
383 q
+= strlen(entry
->icon
);
384 q
--; // To balance the q++ in the for
385 } else if (*p
== 'c' && entry
->name
!= NULL
) {
386 sprintf(q
, "'%s'", entry
->name
);
388 q
+= strlen(entry
->name
);
389 q
--; // To balance the q++ in the for
390 } else if (*p
== 'c') {
391 sprintf(q
, "'%s'", path
);
394 q
--; // To balance the q++ in the for
396 // We don't care about other expansions
397 q
--; // Delete the last % from q
408 //TODO Use UTF8 when parsing the file
409 int launcher_read_desktop_file(const char *path
, DesktopEntry
*entry
)
415 entry
->name
= entry
->icon
= entry
->exec
= NULL
;
417 if ((fp
= fopen(path
, "rt")) == NULL
) {
418 fprintf(stderr
, "Could not open file %s\n", path
);
422 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
423 int len
= strlen(line
);
426 line
[len
- 1] = '\0';
427 if (parse_dektop_line(line
, &key
, &value
)) {
428 if (strcmp(key
, "Name") == 0) {
429 entry
->name
= strdup(value
);
430 } else if (strcmp(key
, "Exec") == 0) {
431 entry
->exec
= strdup(value
);
432 } else if (strcmp(key
, "Icon") == 0) {
433 entry
->icon
= strdup(value
);
439 // entry->name, entry->icon, entry->exec will never be empty strings (can be NULL though)
441 expand_exec(entry
, path
);
446 void free_desktop_entry(DesktopEntry
*entry
)
453 void test_launcher_read_desktop_file()
455 fprintf(stdout
, "\033[1;33m");
457 launcher_read_desktop_file("/usr/share/applications/firefox.desktop", &entry
);
458 printf("Name:%s Icon:%s Exec:%s\n", entry
.name
, entry
.icon
, entry
.exec
);
459 fprintf(stdout
, "\033[0m");
462 //TODO Use UTF8 when parsing the file
463 IconTheme
*load_theme(char *name
)
465 // Look for name/index.theme in $HOME/.icons, /usr/share/icons, /usr/share/pixmaps (stop at the first found)
466 // Parse index.theme -> list of IconThemeDir with attributes
477 file_name
= g_build_filename(g_get_home_dir(), ".icons", name
, "index.theme", NULL
);
478 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
480 file_name
= g_build_filename("/usr/share/icons", name
, "index.theme", NULL
);
481 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
483 file_name
= g_build_filename("/usr/share/pixmaps", name
, "index.theme", NULL
);
484 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
492 fprintf(stderr
, "Could not found theme '%s'\n", name
);
496 if ((f
= fopen(file_name
, "rt")) == NULL
) {
497 fprintf(stderr
, "Could not open theme '%s'\n", file_name
);
501 fprintf(stderr
, "Loading icon theme '%s'\n", file_name
);
505 theme
= calloc(1, sizeof(IconTheme
));
506 theme
->name
= strdup(name
);
507 theme
->list_inherits
= NULL
;
508 theme
->list_directories
= NULL
;
510 IconThemeDir
*current_dir
= NULL
;
511 int inside_header
= 1;
512 while (fgets(line
, sizeof(line
), f
) != NULL
) {
515 int line_len
= strlen(line
);
517 if (line
[line_len
- 1] == '\n') {
518 line
[line_len
- 1] = '\0';
527 if (parse_theme_line(line
, &key
, &value
)) {
528 if (strcmp(key
, "Inherits") == 0) {
529 // value is like oxygen,wood,default
531 token
= strtok(value
, ",\n");
532 while (token
!= NULL
)
534 theme
->list_inherits
= g_slist_append(theme
->list_inherits
, strdup(token
));
535 token
= strtok(NULL
, ",\n");
537 } else if (strcmp(key
, "Directories") == 0) {
538 // value is like 48x48/apps,48x48/mimetypes,32x32/apps,scalable/apps,scalable/mimetypes
540 token
= strtok(value
, ",\n");
541 while (token
!= NULL
)
543 IconThemeDir
*dir
= calloc(1, sizeof(IconThemeDir
));
544 dir
->name
= strdup(token
);
545 dir
->max_size
= dir
->min_size
= dir
->size
= -1;
546 dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
548 theme
->list_directories
= g_slist_append(theme
->list_directories
, dir
);
549 token
= strtok(NULL
, ",\n");
553 } else if (current_dir
!= NULL
) {
554 if (parse_theme_line(line
, &key
, &value
)) {
555 if (strcmp(key
, "Size") == 0) {
557 sscanf(value
, "%d", ¤t_dir
->size
);
558 if (current_dir
->max_size
== -1)
559 current_dir
->max_size
= current_dir
->size
;
560 if (current_dir
->min_size
== -1)
561 current_dir
->min_size
= current_dir
->size
;
562 } else if (strcmp(key
, "MaxSize") == 0) {
564 sscanf(value
, "%d", ¤t_dir
->max_size
);
565 } else if (strcmp(key
, "MinSize") == 0) {
567 sscanf(value
, "%d", ¤t_dir
->min_size
);
568 } else if (strcmp(key
, "Threshold") == 0) {
570 sscanf(value
, "%d", ¤t_dir
->threshold
);
571 } else if (strcmp(key
, "Type") == 0) {
572 // value is Fixed, Scalable or Threshold : default to scalable for unknown Type.
573 if (strcmp(value
, "Fixed") == 0) {
574 current_dir
->type
= ICON_DIR_TYPE_FIXED
;
575 } else if (strcmp(value
, "Threshold") == 0) {
576 current_dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
578 current_dir
->type
= ICON_DIR_TYPE_SCALABLE
;
580 } else if (strcmp(key
, "Context") == 0) {
581 // usual values: Actions, Applications, Devices, FileSystems, MimeTypes
582 current_dir
->context
= strdup(value
);
587 if (line
[0] == '[' && line
[line_len
- 1] == ']' && strcmp(line
, "[Icon Theme]") != 0) {
590 line
[line_len
- 1] = '\0';
591 char *dir_name
= line
+ 1;
592 GSList
* dir_item
= theme
->list_directories
;
593 while (dir_item
!= NULL
)
595 IconThemeDir
*dir
= dir_item
->data
;
596 if (strcmp(dir
->name
, dir_name
) == 0) {
600 dir_item
= g_slist_next(dir_item
);
609 void free_icon_theme(IconTheme
*theme
)
613 for (l_inherits
= theme
->list_inherits
; l_inherits
; l_inherits
= l_inherits
->next
) {
614 free(l_inherits
->data
);
617 for (l_dir
= theme
->list_directories
; l_dir
; l_dir
= l_dir
->next
) {
618 IconThemeDir
*dir
= (IconThemeDir
*)l_dir
->data
;
625 void test_launcher_read_theme_file()
627 fprintf(stdout
, "\033[1;33m");
628 IconTheme
*theme
= load_theme("oxygen");
630 printf("Could not load theme\n");
633 printf("Loaded theme: %s\n", theme
->name
);
634 GSList
* item
= theme
->list_inherits
;
637 printf("Inherits:%s\n", (char*)item
->data
);
638 item
= g_slist_next(item
);
640 item
= theme
->list_directories
;
643 IconThemeDir
*dir
= item
->data
;
644 printf("Dir:%s Size=%d MinSize=%d MaxSize=%d Threshold=%d Type=%s Context=%s\n",
645 dir
->name
, dir
->size
, dir
->min_size
, dir
->max_size
, dir
->threshold
,
646 dir
->type
== ICON_DIR_TYPE_FIXED
? "Fixed" :
647 dir
->type
== ICON_DIR_TYPE_SCALABLE
? "Scalable" :
648 dir
->type
== ICON_DIR_TYPE_THRESHOLD
? "Threshold" : "?????",
650 item
= g_slist_next(item
);
652 fprintf(stdout
, "\033[0m");
655 // Populates the icon_themes list
656 void launcher_load_themes(Launcher
*launcher
)
658 // load the user theme, all the inherited themes recursively (DFS), and the hicolor theme
659 // avoid inheritance loops
661 GSList
*queue
= NULL
;
662 GSList
*queued
= NULL
;
664 GSList
* icon_theme_name_item
;
665 for (icon_theme_name_item
= launcher
->icon_theme_names
; icon_theme_name_item
; icon_theme_name_item
= g_slist_next(icon_theme_name_item
)) {
667 GSList
* queued_item
= queued
;
668 while (queued_item
!= NULL
) {
669 if (strcmp(queued_item
->data
, icon_theme_name_item
->data
) == 0) {
673 queued_item
= g_slist_next(queued_item
);
676 queue
= g_slist_append(queue
, strdup(icon_theme_name_item
->data
));
677 queued
= g_slist_append(queued
, strdup(icon_theme_name_item
->data
));
681 int hicolor_loaded
= 0;
682 while (queue
|| !hicolor_loaded
) {
684 GSList
* queued_item
= queued
;
685 while (queued_item
!= NULL
) {
686 if (strcmp(queued_item
->data
, "hicolor") == 0) {
690 queued_item
= g_slist_next(queued_item
);
694 queue
= g_slist_append(queue
, strdup("hicolor"));
695 queued
= g_slist_append(queued
, strdup("hicolor"));
698 char *name
= queue
->data
;
699 queue
= g_slist_remove(queue
, name
);
701 IconTheme
*theme
= load_theme(name
);
703 launcher
->icon_themes
= g_slist_append(launcher
->icon_themes
, theme
);
705 GSList
* item
= theme
->list_inherits
;
709 char *parent
= item
->data
;
711 GSList
* queued_item
= queued
;
712 while (queued_item
!= NULL
) {
713 if (strcmp(queued_item
->data
, parent
) == 0) {
717 queued_item
= g_slist_next(queued_item
);
720 queue
= g_slist_insert(queue
, strdup(parent
), pos
);
722 queued
= g_slist_append(queued
, strdup(parent
));
724 item
= g_slist_next(item
);
731 for (l
= queue
; l
; l
= l
->next
)
734 for (l
= queued
; l
; l
= l
->next
)
736 g_slist_free(queued
);
739 int directory_matches_size(IconThemeDir
*dir
, int size
)
741 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
742 return dir
->size
== size
;
743 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
744 return dir
->min_size
<= size
&& size
<= dir
->max_size
;
745 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
746 return dir
->size
- dir
->threshold
<= size
&& size
<= dir
->size
+ dir
->threshold
;
750 int directory_size_distance(IconThemeDir
*dir
, int size
)
752 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
753 return abs(dir
->size
- size
);
754 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
755 if (size
< dir
->min_size
) {
756 return dir
->min_size
- size
;
757 } else if (size
> dir
->max_size
) {
758 return size
- dir
->max_size
;
762 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
763 if (size
< dir
->size
- dir
->threshold
) {
764 return dir
->min_size
- size
;
765 } else if (size
> dir
->size
+ dir
->threshold
) {
766 return size
- dir
->max_size
;
773 // Returns the full path to an icon file (or NULL) given the icon name
774 char *icon_path(Launcher
*launcher
, const char *icon_name
, int size
)
776 if (icon_name
== NULL
)
779 // If the icon_name is already a path and the file exists, return it
780 if (strstr(icon_name
, "/") == icon_name
) {
781 if (g_file_test(icon_name
, G_FILE_TEST_EXISTS
))
782 return strdup(icon_name
);
787 GSList
*basenames
= NULL
;
788 char *file_name
= g_build_filename(g_get_home_dir(), ".icons", NULL
);
789 basenames
= g_slist_append(basenames
, file_name
);
790 basenames
= g_slist_append(basenames
, "/usr/share/icons");
791 basenames
= g_slist_append(basenames
, "/usr/share/pixmaps");
793 GSList
*extensions
= NULL
;
794 extensions
= g_slist_append(extensions
, "png");
795 extensions
= g_slist_append(extensions
, "xpm");
797 // Stage 1: exact size match
799 for (theme
= launcher
->icon_themes
; theme
; theme
= g_slist_next(theme
)) {
801 for (dir
= ((IconTheme
*)theme
->data
)->list_directories
; dir
; dir
= g_slist_next(dir
)) {
802 if (directory_matches_size((IconThemeDir
*)dir
->data
, size
)) {
804 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
806 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
807 char *base_name
= (char*) base
->data
;
808 char *theme_name
= ((IconTheme
*)theme
->data
)->name
;
809 char *dir_name
= ((IconThemeDir
*)dir
->data
)->name
;
810 char *extension
= (char*) ext
->data
;
811 char *file_name
= malloc(strlen(base_name
) + strlen(theme_name
) +
812 strlen(dir_name
) + strlen(icon_name
) + strlen(extension
) + 100);
813 // filename = directory/$(themename)/subdirectory/iconname.extension
814 sprintf(file_name
, "%s/%s/%s/%s.%s", base_name
, theme_name
, dir_name
, icon_name
, extension
);
815 //printf("checking %s\n", file_name);
816 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
817 g_slist_free(basenames
);
818 g_slist_free(extensions
);
831 // Stage 2: best size match
832 // Contrary to the freedesktop spec, we are not choosing the closest icon in size, but the next larger icon
833 // otherwise the quality is usually crap (for size 22, if you can choose 16 or 32, you're better with 32)
834 // We do fallback to the closest size if we cannot find a larger or equal icon
835 int minimal_size
= INT_MAX
;
836 char *best_file_name
= NULL
;
837 int next_larger_size
= -1;
838 char *next_larger
= NULL
;
839 for (theme
= launcher
->icon_themes
; theme
; theme
= g_slist_next(theme
)) {
841 for (dir
= ((IconTheme
*)theme
->data
)->list_directories
; dir
; dir
= g_slist_next(dir
)) {
843 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
845 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
846 char *base_name
= (char*) base
->data
;
847 char *theme_name
= ((IconTheme
*)theme
->data
)->name
;
848 char *dir_name
= ((IconThemeDir
*)dir
->data
)->name
;
849 char *extension
= (char*) ext
->data
;
850 char *file_name
= malloc(strlen(base_name
) + strlen(theme_name
) +
851 strlen(dir_name
) + strlen(icon_name
) + strlen(extension
) + 100);
852 // filename = directory/$(themename)/subdirectory/iconname.extension
853 sprintf(file_name
, "%s/%s/%s/%s.%s", base_name
, theme_name
, dir_name
, icon_name
, extension
);
854 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
855 if (directory_size_distance((IconThemeDir
*)dir
->data
, size
) < minimal_size
) {
856 if (best_file_name
) {
857 free(best_file_name
);
858 best_file_name
= NULL
;
860 best_file_name
= strdup(file_name
);
861 minimal_size
= directory_size_distance((IconThemeDir
*)dir
->data
, size
);
863 if (((IconThemeDir
*)dir
->data
)->size
>= size
&& (next_larger_size
== -1 || ((IconThemeDir
*)dir
->data
)->size
< next_larger_size
)) {
868 next_larger
= strdup(file_name
);
869 next_larger_size
= ((IconThemeDir
*)dir
->data
)->size
;
878 g_slist_free(basenames
);
879 g_slist_free(extensions
);
880 free(best_file_name
);
883 if (best_file_name
) {
884 g_slist_free(basenames
);
885 g_slist_free(extensions
);
886 return best_file_name
;
889 // Stage 3: look in unthemed icons
892 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
894 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
895 char *base_name
= (char*) base
->data
;
896 char *extension
= (char*) ext
->data
;
897 char *file_name
= malloc(strlen(base_name
) + strlen(icon_name
) +
898 strlen(extension
) + 100);
899 // filename = directory/iconname.extension
900 sprintf(file_name
, "%s/%s.%s", base_name
, icon_name
, extension
);
901 //printf("checking %s\n", file_name);
902 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
903 g_slist_free(basenames
);
904 g_slist_free(extensions
);
914 fprintf(stderr
, "Could not find icon %s\n", icon_name
);
This page took 0.081577 seconds and 3 git commands to generate.