]>
Dogcows Code - chaz/tint2/blob - src/launcher/launcher.c
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
;
168 // Get the path for an icon file with the new size
169 char *new_icon_path
= icon_path(launcher
, launcherIcon
->icon_name
, launcherIcon
->icon_size
);
170 if (!new_icon_path
) {
172 free_icon(launcherIcon
->icon_original
);
173 launcherIcon
->icon_original
= NULL
;
174 free_icon(launcherIcon
->icon_scaled
);
175 launcherIcon
->icon_scaled
= NULL
;
176 new_icon_path
= icon_path(launcher
, ICON_FALLBACK
, launcherIcon
->icon_size
);
178 launcherIcon
->icon_original
= imlib_load_image(new_icon_path
);
179 fprintf(stderr
, "%s %d: Using icon %s\n", __FILE__
, __LINE__
, new_icon_path
);
182 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, icon_size
);
185 if (launcherIcon
->icon_path
&& strcmp(new_icon_path
, launcherIcon
->icon_path
) == 0) {
186 // If it's the same file just rescale
187 free_icon(launcherIcon
->icon_scaled
);
188 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, icon_size
);
190 fprintf(stderr
, "%s %d: Using icon %s\n", __FILE__
, __LINE__
, launcherIcon
->icon_path
);
192 // Free the old files
193 free_icon(launcherIcon
->icon_original
);
194 free_icon(launcherIcon
->icon_scaled
);
195 // Load the new file and scale
196 launcherIcon
->icon_original
= imlib_load_image(new_icon_path
);
197 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, launcherIcon
->icon_size
);
198 free(launcherIcon
->icon_path
);
199 launcherIcon
->icon_path
= new_icon_path
;
200 fprintf(stderr
, "%s %d: Using icon %s\n", __FILE__
, __LINE__
, launcherIcon
->icon_path
);
206 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
210 if (panel_horizontal
) {
211 if (!count
) launcher
->area
.width
= 0;
213 int height
= launcher
->area
.height
- 2*launcher
->area
.bg
->border
.width
- 2*launcher
->area
.paddingy
;
214 // here icons_per_column always higher than 0
215 icons_per_column
= (height
+launcher
->area
.paddingx
) / (icon_size
+launcher
->area
.paddingx
);
216 marging
= height
- (icons_per_column
-1)*(icon_size
+launcher
->area
.paddingx
) - icon_size
;
217 icons_per_row
= count
/ icons_per_column
+ (count%icons_per_column
!= 0);
218 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
);
222 if (!count
) launcher
->area
.height
= 0;
224 int width
= launcher
->area
.width
- 2*launcher
->area
.bg
->border
.width
- 2*launcher
->area
.paddingy
;
225 // here icons_per_row always higher than 0
226 icons_per_row
= (width
+launcher
->area
.paddingx
) / (icon_size
+launcher
->area
.paddingx
);
227 marging
= width
- (icons_per_row
-1)*(icon_size
+launcher
->area
.paddingx
) - icon_size
;
228 icons_per_column
= count
/ icons_per_row
+ (count%icons_per_row
!= 0);
229 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
);
234 int start
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingy
;// +marging/2;
235 if (panel_horizontal
) {
237 posx
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingxlr
;
241 posy
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingxlr
;
244 for (i
=1, l
= launcher
->list_icons
; l
; i
++, l
= l
->next
) {
245 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
247 launcherIcon
->y
= posy
;
248 launcherIcon
->x
= posx
;
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
);
270 void draw_launcher(void *obj
, cairo_t
*c
)
272 Launcher
*launcher
= obj
;
274 if (launcher
->list_icons
== 0) return;
276 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
277 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
278 int pos_x
= launcherIcon
->x
;
279 int pos_y
= launcherIcon
->y
;
280 Imlib_Image icon_scaled
= launcherIcon
->icon_scaled
;
282 imlib_context_set_image (icon_scaled
);
283 if (server
.real_transparency
) {
284 render_image(launcher
->area
.pix
, pos_x
, pos_y
, imlib_image_get_width(), imlib_image_get_height() );
287 imlib_context_set_drawable(launcher
->area
.pix
);
288 imlib_render_image_on_drawable (pos_x
, pos_y
);
293 Imlib_Image
scale_icon(Imlib_Image original
, int icon_size
)
295 Imlib_Image icon_scaled
;
297 imlib_context_set_image (original
);
298 icon_scaled
= imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), icon_size
, icon_size
);
300 icon_scaled
= imlib_create_image(icon_size
, icon_size
);
301 imlib_context_set_image (icon_scaled
);
302 imlib_context_set_color(255, 255, 255, 255);
303 imlib_image_fill_rectangle(0, 0, icon_size
, icon_size
);
308 void free_icon(Imlib_Image icon
)
311 imlib_context_set_image(icon
);
316 void launcher_action(LauncherIcon
*icon
)
318 char *cmd
= malloc(strlen(icon
->cmd
) + 10);
319 sprintf(cmd
, "(%s&)", icon
->cmd
);
324 /***************** Freedesktop app.desktop and icon theme handling *********************/
325 /* http://standards.freedesktop.org/desktop-entry-spec/ */
326 /* http://standards.freedesktop.org/icon-theme-spec/ */
328 // Splits line at first '=' and returns 1 if successful, and parts are not empty
329 // key and value point to the parts
330 int parse_dektop_line(char *line
, char **key
, char **value
)
335 for (p
= line
; *p
; p
++) {
345 if (found
&& (strlen(*key
) == 0 || strlen(*value
) == 0))
350 int parse_theme_line(char *line
, char **key
, char **value
)
352 return parse_dektop_line(line
, key
, value
);
355 void expand_exec(DesktopEntry
*entry
, const char *path
)
362 char *exec2
= malloc(strlen(entry
->exec
) + (entry
->name
? strlen(entry
->name
) : 1) + (entry
->icon
? strlen(entry
->icon
) : 1) + 100);
364 // p will never point to an escaped char
365 for (p
= entry
->exec
, q
= exec2
; *p
; p
++, q
++) {
369 // Copy the escaped char
370 if (*p
== '%') // For % we delete the backslash, i.e. write % over it
379 if (*p
== 'i' && entry
->icon
!= NULL
) {
380 sprintf(q
, "--icon '%s'", entry
->icon
);
381 q
+= strlen("--icon ''");
382 q
+= strlen(entry
->icon
);
383 q
--; // To balance the q++ in the for
384 } else if (*p
== 'c' && entry
->name
!= NULL
) {
385 sprintf(q
, "'%s'", entry
->name
);
387 q
+= strlen(entry
->name
);
388 q
--; // To balance the q++ in the for
389 } else if (*p
== 'c') {
390 sprintf(q
, "'%s'", path
);
393 q
--; // To balance the q++ in the for
395 // We don't care about other expansions
396 q
--; // Delete the last % from q
407 //TODO Use UTF8 when parsing the file
408 int launcher_read_desktop_file(const char *path
, DesktopEntry
*entry
)
414 entry
->name
= entry
->icon
= entry
->exec
= NULL
;
416 if ((fp
= fopen(path
, "rt")) == NULL
) {
417 fprintf(stderr
, "Could not open file %s\n", path
);
421 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
422 int len
= strlen(line
);
425 line
[len
- 1] = '\0';
426 if (parse_dektop_line(line
, &key
, &value
)) {
427 if (strcmp(key
, "Name") == 0) {
428 entry
->name
= strdup(value
);
429 } else if (strcmp(key
, "Exec") == 0) {
430 entry
->exec
= strdup(value
);
431 } else if (strcmp(key
, "Icon") == 0) {
432 entry
->icon
= strdup(value
);
438 // entry->name, entry->icon, entry->exec will never be empty strings (can be NULL though)
440 expand_exec(entry
, path
);
445 void free_desktop_entry(DesktopEntry
*entry
)
452 void test_launcher_read_desktop_file()
454 fprintf(stdout
, "\033[1;33m");
456 launcher_read_desktop_file("/usr/share/applications/firefox.desktop", &entry
);
457 printf("Name:%s Icon:%s Exec:%s\n", entry
.name
, entry
.icon
, entry
.exec
);
458 fprintf(stdout
, "\033[0m");
461 //TODO Use UTF8 when parsing the file
462 IconTheme
*load_theme(char *name
)
464 // Look for name/index.theme in $HOME/.icons, /usr/share/icons, /usr/share/pixmaps (stop at the first found)
465 // Parse index.theme -> list of IconThemeDir with attributes
476 fprintf(stderr
, "Loading icon theme %s\n", name
);
478 file_name
= malloc(100 + strlen(name
));
479 sprintf(file_name
, "~/.icons/%s/index.theme", name
);
480 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
481 sprintf(file_name
, "/usr/share/icons/%s/index.theme", name
);
482 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
483 sprintf(file_name
, "/usr/share/pixmaps/%s/index.theme", name
);
484 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
491 fprintf(stderr
, "Could not load theme %s\n", name
);
495 if ((f
= fopen(file_name
, "rt")) == NULL
)
500 theme
= calloc(1, sizeof(IconTheme
));
501 theme
->name
= strdup(name
);
502 theme
->list_inherits
= NULL
;
503 theme
->list_directories
= NULL
;
505 IconThemeDir
*current_dir
= NULL
;
506 int inside_header
= 1;
507 while (fgets(line
, sizeof(line
), f
) != NULL
) {
510 int line_len
= strlen(line
);
512 if (line
[line_len
- 1] == '\n') {
513 line
[line_len
- 1] = '\0';
522 if (parse_theme_line(line
, &key
, &value
)) {
523 if (strcmp(key
, "Inherits") == 0) {
524 // value is like oxygen,wood,default
526 token
= strtok(value
, ",\n");
527 while (token
!= NULL
)
529 theme
->list_inherits
= g_slist_append(theme
->list_inherits
, strdup(token
));
530 token
= strtok(NULL
, ",\n");
532 } else if (strcmp(key
, "Directories") == 0) {
533 // value is like 48x48/apps,48x48/mimetypes,32x32/apps,scalable/apps,scalable/mimetypes
535 token
= strtok(value
, ",\n");
536 while (token
!= NULL
)
538 IconThemeDir
*dir
= calloc(1, sizeof(IconThemeDir
));
539 dir
->name
= strdup(token
);
540 dir
->max_size
= dir
->min_size
= dir
->size
= -1;
541 dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
543 theme
->list_directories
= g_slist_append(theme
->list_directories
, dir
);
544 token
= strtok(NULL
, ",\n");
548 } else if (current_dir
!= NULL
) {
549 if (parse_theme_line(line
, &key
, &value
)) {
550 if (strcmp(key
, "Size") == 0) {
552 sscanf(value
, "%d", ¤t_dir
->size
);
553 if (current_dir
->max_size
== -1)
554 current_dir
->max_size
= current_dir
->size
;
555 if (current_dir
->min_size
== -1)
556 current_dir
->min_size
= current_dir
->size
;
557 } else if (strcmp(key
, "MaxSize") == 0) {
559 sscanf(value
, "%d", ¤t_dir
->max_size
);
560 } else if (strcmp(key
, "MinSize") == 0) {
562 sscanf(value
, "%d", ¤t_dir
->min_size
);
563 } else if (strcmp(key
, "Threshold") == 0) {
565 sscanf(value
, "%d", ¤t_dir
->threshold
);
566 } else if (strcmp(key
, "Type") == 0) {
567 // value is Fixed, Scalable or Threshold
568 if (strcmp(value
, "Fixed") == 0) {
569 current_dir
->type
= ICON_DIR_TYPE_FIXED
;
570 } else if (strcmp(value
, "Scalable") == 0) {
571 current_dir
->type
= ICON_DIR_TYPE_SCALABLE
;
572 } else if (strcmp(value
, "Threshold") == 0) {
573 current_dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
575 } else if (strcmp(key
, "Context") == 0) {
576 // usual values: Actions, Applications, Devices, FileSystems, MimeTypes
577 current_dir
->context
= strdup(value
);
582 if (line
[0] == '[' && line
[line_len
- 1] == ']' && strcmp(line
, "[Icon Theme]") != 0) {
585 line
[line_len
- 1] = '\0';
586 char *dir_name
= line
+ 1;
587 GSList
* dir_item
= theme
->list_directories
;
588 while (dir_item
!= NULL
)
590 IconThemeDir
*dir
= dir_item
->data
;
591 if (strcmp(dir
->name
, dir_name
) == 0) {
595 dir_item
= g_slist_next(dir_item
);
604 void free_icon_theme(IconTheme
*theme
)
608 for (l_inherits
= theme
->list_inherits
; l_inherits
; l_inherits
= l_inherits
->next
) {
609 free(l_inherits
->data
);
612 for (l_dir
= theme
->list_directories
; l_dir
; l_dir
= l_dir
->next
) {
613 IconThemeDir
*dir
= (IconThemeDir
*)l_dir
->data
;
620 void test_launcher_read_theme_file()
622 fprintf(stdout
, "\033[1;33m");
623 IconTheme
*theme
= load_theme("oxygen");
625 printf("Could not load theme\n");
628 printf("Loaded theme: %s\n", theme
->name
);
629 GSList
* item
= theme
->list_inherits
;
632 printf("Inherits:%s\n", (char*)item
->data
);
633 item
= g_slist_next(item
);
635 item
= theme
->list_directories
;
638 IconThemeDir
*dir
= item
->data
;
639 printf("Dir:%s Size=%d MinSize=%d MaxSize=%d Threshold=%d Type=%s Context=%s\n",
640 dir
->name
, dir
->size
, dir
->min_size
, dir
->max_size
, dir
->threshold
,
641 dir
->type
== ICON_DIR_TYPE_FIXED
? "Fixed" :
642 dir
->type
== ICON_DIR_TYPE_SCALABLE
? "Scalable" :
643 dir
->type
== ICON_DIR_TYPE_THRESHOLD
? "Threshold" : "?????",
645 item
= g_slist_next(item
);
647 fprintf(stdout
, "\033[0m");
650 // Populates the icon_themes list
651 void launcher_load_themes(Launcher
*launcher
)
653 // load the user theme, all the inherited themes recursively (DFS), and the hicolor theme
654 // avoid inheritance loops
656 GSList
*queue
= NULL
;
657 GSList
*queued
= NULL
;
659 GSList
* icon_theme_name_item
;
660 for (icon_theme_name_item
= launcher
->icon_theme_names
; icon_theme_name_item
; icon_theme_name_item
= g_slist_next(icon_theme_name_item
)) {
662 GSList
* queued_item
= queued
;
663 while (queued_item
!= NULL
) {
664 if (strcmp(queued_item
->data
, icon_theme_name_item
->data
) == 0) {
668 queued_item
= g_slist_next(queued_item
);
671 queue
= g_slist_append(queue
, strdup(icon_theme_name_item
->data
));
672 queued
= g_slist_append(queued
, strdup(icon_theme_name_item
->data
));
676 int hicolor_loaded
= 0;
677 while (queue
|| !hicolor_loaded
) {
679 GSList
* queued_item
= queued
;
680 while (queued_item
!= NULL
) {
681 if (strcmp(queued_item
->data
, "hicolor") == 0) {
685 queued_item
= g_slist_next(queued_item
);
689 queue
= g_slist_append(queue
, strdup("hicolor"));
690 queued
= g_slist_append(queued
, strdup("hicolor"));
693 char *name
= queue
->data
;
694 queue
= g_slist_remove(queue
, name
);
696 IconTheme
*theme
= load_theme(name
);
698 launcher
->icon_themes
= g_slist_append(launcher
->icon_themes
, theme
);
700 GSList
* item
= theme
->list_inherits
;
704 char *parent
= item
->data
;
706 GSList
* queued_item
= queued
;
707 while (queued_item
!= NULL
) {
708 if (strcmp(queued_item
->data
, parent
) == 0) {
712 queued_item
= g_slist_next(queued_item
);
715 queue
= g_slist_insert(queue
, strdup(parent
), pos
);
717 queued
= g_slist_append(queued
, strdup(parent
));
719 item
= g_slist_next(item
);
726 for (l
= queue
; l
; l
= l
->next
)
729 for (l
= queued
; l
; l
= l
->next
)
731 g_slist_free(queued
);
734 int directory_matches_size(IconThemeDir
*dir
, int size
)
736 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
737 return dir
->size
== size
;
738 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
739 return dir
->min_size
<= size
&& size
<= dir
->max_size
;
740 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
741 return dir
->size
- dir
->threshold
<= size
&& size
<= dir
->size
+ dir
->threshold
;
745 int directory_size_distance(IconThemeDir
*dir
, int size
)
747 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
748 return abs(dir
->size
- size
);
749 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
750 if (size
< dir
->min_size
) {
751 return dir
->min_size
- size
;
752 } else if (size
> dir
->max_size
) {
753 return size
- dir
->max_size
;
757 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
758 if (size
< dir
->size
- dir
->threshold
) {
759 return dir
->min_size
- size
;
760 } else if (size
> dir
->size
+ dir
->threshold
) {
761 return size
- dir
->max_size
;
768 // Returns the full path to an icon file (or NULL) given the icon name
769 char *icon_path(Launcher
*launcher
, const char *icon_name
, int size
)
771 if (icon_name
== NULL
)
774 // If the icon_name is already a path and the file exists, return it
775 if (strstr(icon_name
, "/") == icon_name
) {
776 if (g_file_test(icon_name
, G_FILE_TEST_EXISTS
))
777 return strdup(icon_name
);
782 GSList
*basenames
= NULL
;
783 basenames
= g_slist_append(basenames
, "~/.icons");
784 basenames
= g_slist_append(basenames
, "/usr/share/icons");
785 basenames
= g_slist_append(basenames
, "/usr/share/pixmaps");
787 GSList
*extensions
= NULL
;
788 extensions
= g_slist_append(extensions
, "png");
789 extensions
= g_slist_append(extensions
, "xpm");
791 // Stage 1: exact size match
793 for (theme
= launcher
->icon_themes
; theme
; theme
= g_slist_next(theme
)) {
795 for (dir
= ((IconTheme
*)theme
->data
)->list_directories
; dir
; dir
= g_slist_next(dir
)) {
796 if (directory_matches_size((IconThemeDir
*)dir
->data
, size
)) {
798 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
800 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
801 char *base_name
= (char*) base
->data
;
802 char *theme_name
= ((IconTheme
*)theme
->data
)->name
;
803 char *dir_name
= ((IconThemeDir
*)dir
->data
)->name
;
804 char *extension
= (char*) ext
->data
;
805 char *file_name
= malloc(strlen(base_name
) + strlen(theme_name
) +
806 strlen(dir_name
) + strlen(icon_name
) + strlen(extension
) + 100);
807 // filename = directory/$(themename)/subdirectory/iconname.extension
808 sprintf(file_name
, "%s/%s/%s/%s.%s", base_name
, theme_name
, dir_name
, icon_name
, extension
);
809 //printf("checking %s\n", file_name);
810 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
811 g_slist_free(basenames
);
812 g_slist_free(extensions
);
824 // Stage 2: best size match
825 // Contrary to the freedesktop spec, we are not choosing the closest icon in size, but the next larger icon
826 // otherwise the quality is usually crap (for size 22, if you can choose 16 or 32, you're better with 32)
827 // We do fallback to the closest size if we cannot find a larger or equal icon
828 int minimal_size
= INT_MAX
;
829 char *best_file_name
= NULL
;
830 int next_larger_size
= -1;
831 char *next_larger
= NULL
;
832 for (theme
= launcher
->icon_themes
; theme
; theme
= g_slist_next(theme
)) {
834 for (dir
= ((IconTheme
*)theme
->data
)->list_directories
; dir
; dir
= g_slist_next(dir
)) {
836 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
838 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
839 char *base_name
= (char*) base
->data
;
840 char *theme_name
= ((IconTheme
*)theme
->data
)->name
;
841 char *dir_name
= ((IconThemeDir
*)dir
->data
)->name
;
842 char *extension
= (char*) ext
->data
;
843 char *file_name
= malloc(strlen(base_name
) + strlen(theme_name
) +
844 strlen(dir_name
) + strlen(icon_name
) + strlen(extension
) + 100);
845 // filename = directory/$(themename)/subdirectory/iconname.extension
846 sprintf(file_name
, "%s/%s/%s/%s.%s", base_name
, theme_name
, dir_name
, icon_name
, extension
);
847 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
848 if (directory_size_distance((IconThemeDir
*)dir
->data
, size
) < minimal_size
) {
849 if (best_file_name
) {
850 free(best_file_name
);
851 best_file_name
= NULL
;
853 best_file_name
= strdup(file_name
);
854 minimal_size
= directory_size_distance((IconThemeDir
*)dir
->data
, size
);
856 if (((IconThemeDir
*)dir
->data
)->size
>= size
&& (next_larger_size
== -1 || ((IconThemeDir
*)dir
->data
)->size
< next_larger_size
)) {
861 next_larger
= strdup(file_name
);
862 next_larger_size
= ((IconThemeDir
*)dir
->data
)->size
;
871 g_slist_free(basenames
);
872 g_slist_free(extensions
);
873 free(best_file_name
);
876 if (best_file_name
) {
877 g_slist_free(basenames
);
878 g_slist_free(extensions
);
879 return best_file_name
;
882 // Stage 3: look in unthemed icons
885 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
887 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
888 char *base_name
= (char*) base
->data
;
889 char *extension
= (char*) ext
->data
;
890 char *file_name
= malloc(strlen(base_name
) + strlen(icon_name
) +
891 strlen(extension
) + 100);
892 // filename = directory/iconname.extension
893 sprintf(file_name
, "%s/%s.%s", base_name
, icon_name
, extension
);
894 //printf("checking %s\n", file_name);
895 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
896 g_slist_free(basenames
);
897 g_slist_free(extensions
);
907 fprintf(stderr
, "Could not find icon %s\n", icon_name
);
This page took 0.077089 seconds and 4 git commands to generate.