]>
Dogcows Code - chaz/tint2/blob - launcher.c
6e49ba7ec75c7a8a072e6d41ed4d231b23cd93bc
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
;
41 #define ICON_FALLBACK "exec"
43 char *icon_path(Launcher
*launcher
, const char *icon_name
, int size
);
44 void launcher_load_themes(Launcher
*launcher
);
45 void free_desktop_entry(DesktopEntry
*entry
);
46 int launcher_read_desktop_file(const char *path
, DesktopEntry
*entry
);
47 Imlib_Image
scale_icon(Imlib_Image original
, int icon_size
);
48 void free_icon(Imlib_Image icon
);
49 void free_icon_theme(IconTheme
*theme
);
52 void default_launcher()
55 launcher_max_icon_size
= 0;
58 xsettings_client
= NULL
;
59 printf("default_launcher\n");
65 if (launcher_enabled
) {
66 // if XSETTINGS manager running, tint2 read the icon_theme_name.
67 xsettings_client
= xsettings_client_new(server
.dsp
, server
.screen
, xsettings_notify_cb
, NULL
, NULL
);
72 void init_launcher_panel(void *p
)
74 Panel
*panel
=(Panel
*)p
;
75 Launcher
*launcher
= &panel
->launcher
;
77 launcher
->area
.parent
= p
;
78 launcher
->area
.panel
= p
;
79 launcher
->area
._draw_foreground
= draw_launcher
;
80 launcher
->area
.size_mode
= SIZE_BY_CONTENT
;
81 launcher
->area
._resize
= resize_launcher
;
82 launcher
->area
.resize
= 1;
83 launcher
->area
.redraw
= 1;
86 if (launcher
->list_apps
== NULL
)
89 launcher
->area
.on_screen
= 1;
92 launcher_load_themes(launcher
);
94 // Load apps (.desktop style launcher items)
95 GSList
* app
= launcher
->list_apps
;
98 launcher_read_desktop_file(app
->data
, &entry
);
100 LauncherIcon
*launcherIcon
= calloc(1, sizeof(LauncherIcon
));
101 launcherIcon
->is_app_desktop
= 1;
102 launcherIcon
->cmd
= strdup(entry
.exec
);
103 launcherIcon
->icon_name
= entry
.icon
? strdup(entry
.icon
) : strdup(ICON_FALLBACK
);
104 launcherIcon
->icon_size
= 1;
105 free_desktop_entry(&entry
);
106 launcher
->list_icons
= g_slist_append(launcher
->list_icons
, launcherIcon
);
108 app
= g_slist_next(app
);
113 void cleanup_launcher()
117 if (xsettings_client
)
118 xsettings_client_destroy(xsettings_client
);
119 for (i
= 0 ; i
< nb_panel
; i
++) {
120 Panel
*panel
= &panel1
[i
];
121 Launcher
*launcher
= &panel
->launcher
;
122 free_area(&launcher
->area
);
124 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
125 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
127 free_icon(launcherIcon
->icon_scaled
);
128 free_icon(launcherIcon
->icon_original
);
129 free(launcherIcon
->icon_name
);
130 free(launcherIcon
->icon_path
);
131 free(launcherIcon
->cmd
);
135 g_slist_free(launcher
->list_icons
);
137 for (l
= launcher
->list_apps
; l
; l
= l
->next
) {
140 g_slist_free(launcher
->list_apps
);
142 for (l
= launcher
->icon_themes
; l
; l
= l
->next
) {
143 IconTheme
*theme
= (IconTheme
*) l
->data
;
144 free_icon_theme(theme
);
147 g_slist_free(launcher
->icon_themes
);
149 launcher
->list_apps
= launcher
->list_icons
= launcher
->icon_themes
= NULL
;
151 g_free(icon_theme_name
);
152 launcher_enabled
= 0;
156 int resize_launcher(void *obj
)
158 Launcher
*launcher
= obj
;
160 int count
, icon_size
;
161 int icons_per_column
=1, icons_per_row
=1, marging
=0;
163 if (panel_horizontal
)
164 icon_size
= launcher
->area
.height
;
166 icon_size
= launcher
->area
.width
;
167 icon_size
= icon_size
- (2 * launcher
->area
.bg
->border
.width
) - (2 * launcher
->area
.paddingy
);
168 if (launcher_max_icon_size
> 0 && icon_size
> launcher_max_icon_size
)
169 icon_size
= launcher_max_icon_size
;
171 // Resize icons if necessary
172 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
173 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
174 if (launcherIcon
->icon_size
!= icon_size
|| !launcherIcon
->icon_original
) {
175 launcherIcon
->icon_size
= icon_size
;
177 // Get the path for an icon file with the new size
178 char *new_icon_path
= icon_path(launcher
, launcherIcon
->icon_name
, launcherIcon
->icon_size
);
179 if (!new_icon_path
) {
181 free_icon(launcherIcon
->icon_original
);
182 launcherIcon
->icon_original
= NULL
;
183 free_icon(launcherIcon
->icon_scaled
);
184 launcherIcon
->icon_scaled
= NULL
;
185 new_icon_path
= icon_path(launcher
, ICON_FALLBACK
, launcherIcon
->icon_size
);
187 launcherIcon
->icon_original
= imlib_load_image(new_icon_path
);
188 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, new_icon_path
);
191 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, icon_size
);
194 if (launcherIcon
->icon_path
&& strcmp(new_icon_path
, launcherIcon
->icon_path
) == 0) {
195 // If it's the same file just rescale
196 free_icon(launcherIcon
->icon_scaled
);
197 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, icon_size
);
199 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, launcherIcon
->icon_path
);
201 // Free the old files
202 free_icon(launcherIcon
->icon_original
);
203 free_icon(launcherIcon
->icon_scaled
);
204 // Load the new file and scale
205 launcherIcon
->icon_original
= imlib_load_image(new_icon_path
);
206 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, launcherIcon
->icon_size
);
207 free(launcherIcon
->icon_path
);
208 launcherIcon
->icon_path
= new_icon_path
;
209 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, launcherIcon
->icon_path
);
215 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
219 if (panel_horizontal
) {
220 if (!count
) launcher
->area
.width
= 0;
222 int height
= launcher
->area
.height
- 2*launcher
->area
.bg
->border
.width
- 2*launcher
->area
.paddingy
;
223 // here icons_per_column always higher than 0
224 icons_per_column
= (height
+launcher
->area
.paddingx
) / (icon_size
+launcher
->area
.paddingx
);
225 marging
= height
- (icons_per_column
-1)*(icon_size
+launcher
->area
.paddingx
) - icon_size
;
226 icons_per_row
= count
/ icons_per_column
+ (count%icons_per_column
!= 0);
227 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
);
231 if (!count
) launcher
->area
.height
= 0;
233 int width
= launcher
->area
.width
- 2*launcher
->area
.bg
->border
.width
- 2*launcher
->area
.paddingy
;
234 // here icons_per_row always higher than 0
235 icons_per_row
= (width
+launcher
->area
.paddingx
) / (icon_size
+launcher
->area
.paddingx
);
236 marging
= width
- (icons_per_row
-1)*(icon_size
+launcher
->area
.paddingx
) - icon_size
;
237 icons_per_column
= count
/ icons_per_row
+ (count%icons_per_row
!= 0);
238 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
);
243 int start
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingy
;// +marging/2;
244 if (panel_horizontal
) {
246 posx
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingxlr
;
250 posy
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingxlr
;
253 for (i
=1, l
= launcher
->list_icons
; l
; i
++, l
= l
->next
) {
254 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
256 launcherIcon
->y
= posy
;
257 launcherIcon
->x
= posx
;
258 if (panel_horizontal
) {
259 if (i
% icons_per_column
)
260 posy
+= icon_size
+ launcher
->area
.paddingx
;
263 posx
+= (icon_size
+ launcher
->area
.paddingx
);
267 if (i
% icons_per_row
)
268 posx
+= icon_size
+ launcher
->area
.paddingx
;
271 posy
+= (icon_size
+ launcher
->area
.paddingx
);
279 void draw_launcher(void *obj
, cairo_t
*c
)
281 Launcher
*launcher
= obj
;
283 if (launcher
->list_icons
== 0) return;
285 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
286 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
287 int pos_x
= launcherIcon
->x
;
288 int pos_y
= launcherIcon
->y
;
289 Imlib_Image icon_scaled
= launcherIcon
->icon_scaled
;
291 imlib_context_set_image (icon_scaled
);
292 if (server
.real_transparency
) {
293 render_image(launcher
->area
.pix
, pos_x
, pos_y
, imlib_image_get_width(), imlib_image_get_height() );
296 imlib_context_set_drawable(launcher
->area
.pix
);
297 imlib_render_image_on_drawable (pos_x
, pos_y
);
302 Imlib_Image
scale_icon(Imlib_Image original
, int icon_size
)
304 Imlib_Image icon_scaled
;
306 imlib_context_set_image (original
);
307 icon_scaled
= imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), icon_size
, icon_size
);
309 icon_scaled
= imlib_create_image(icon_size
, icon_size
);
310 imlib_context_set_image (icon_scaled
);
311 imlib_context_set_color(255, 255, 255, 255);
312 imlib_image_fill_rectangle(0, 0, icon_size
, icon_size
);
317 void free_icon(Imlib_Image icon
)
320 imlib_context_set_image(icon
);
325 void launcher_action(LauncherIcon
*icon
)
327 char *cmd
= malloc(strlen(icon
->cmd
) + 10);
328 sprintf(cmd
, "(%s&)", icon
->cmd
);
333 /***************** Freedesktop app.desktop and icon theme handling *********************/
334 /* http://standards.freedesktop.org/desktop-entry-spec/ */
335 /* http://standards.freedesktop.org/icon-theme-spec/ */
337 // Splits line at first '=' and returns 1 if successful, and parts are not empty
338 // key and value point to the parts
339 int parse_dektop_line(char *line
, char **key
, char **value
)
344 for (p
= line
; *p
; p
++) {
354 if (found
&& (strlen(*key
) == 0 || strlen(*value
) == 0))
359 int parse_theme_line(char *line
, char **key
, char **value
)
361 return parse_dektop_line(line
, key
, value
);
364 void expand_exec(DesktopEntry
*entry
, const char *path
)
371 char *exec2
= malloc(strlen(entry
->exec
) + (entry
->name
? strlen(entry
->name
) : 1) + (entry
->icon
? strlen(entry
->icon
) : 1) + 100);
373 // p will never point to an escaped char
374 for (p
= entry
->exec
, q
= exec2
; *p
; p
++, q
++) {
378 // Copy the escaped char
379 if (*p
== '%') // For % we delete the backslash, i.e. write % over it
388 if (*p
== 'i' && entry
->icon
!= NULL
) {
389 sprintf(q
, "--icon '%s'", entry
->icon
);
390 q
+= strlen("--icon ''");
391 q
+= strlen(entry
->icon
);
392 q
--; // To balance the q++ in the for
393 } else if (*p
== 'c' && entry
->name
!= NULL
) {
394 sprintf(q
, "'%s'", entry
->name
);
396 q
+= strlen(entry
->name
);
397 q
--; // To balance the q++ in the for
398 } else if (*p
== 'c') {
399 sprintf(q
, "'%s'", path
);
402 q
--; // To balance the q++ in the for
404 // We don't care about other expansions
405 q
--; // Delete the last % from q
416 //TODO Use UTF8 when parsing the file
417 int launcher_read_desktop_file(const char *path
, DesktopEntry
*entry
)
423 entry
->name
= entry
->icon
= entry
->exec
= NULL
;
425 if ((fp
= fopen(path
, "rt")) == NULL
) {
426 fprintf(stderr
, "Could not open file %s\n", path
);
430 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
431 int len
= strlen(line
);
434 line
[len
- 1] = '\0';
435 if (parse_dektop_line(line
, &key
, &value
)) {
436 if (strcmp(key
, "Name") == 0) {
437 entry
->name
= strdup(value
);
438 } else if (strcmp(key
, "Exec") == 0) {
439 entry
->exec
= strdup(value
);
440 } else if (strcmp(key
, "Icon") == 0) {
441 entry
->icon
= strdup(value
);
447 // entry->name, entry->icon, entry->exec will never be empty strings (can be NULL though)
449 expand_exec(entry
, path
);
454 void free_desktop_entry(DesktopEntry
*entry
)
461 void test_launcher_read_desktop_file()
463 fprintf(stdout
, "\033[1;33m");
465 launcher_read_desktop_file("/usr/share/applications/firefox.desktop", &entry
);
466 printf("Name:%s Icon:%s Exec:%s\n", entry
.name
, entry
.icon
, entry
.exec
);
467 fprintf(stdout
, "\033[0m");
470 //TODO Use UTF8 when parsing the file
471 IconTheme
*load_theme(char *name
)
473 // Look for name/index.theme in $HOME/.icons, /usr/share/icons, /usr/share/pixmaps (stop at the first found)
474 // Parse index.theme -> list of IconThemeDir with attributes
485 file_name
= g_build_filename(g_get_home_dir(), ".icons", name
, "index.theme", NULL
);
486 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
488 file_name
= g_build_filename("/usr/share/icons", name
, "index.theme", NULL
);
489 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
491 file_name
= g_build_filename("/usr/share/pixmaps", name
, "index.theme", NULL
);
492 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
503 if ((f
= fopen(file_name
, "rt")) == NULL
) {
504 fprintf(stderr
, "Could not open theme '%s'\n", file_name
);
510 theme
= calloc(1, sizeof(IconTheme
));
511 theme
->name
= strdup(name
);
512 theme
->list_inherits
= NULL
;
513 theme
->list_directories
= NULL
;
515 IconThemeDir
*current_dir
= NULL
;
516 int inside_header
= 1;
517 while (fgets(line
, sizeof(line
), f
) != NULL
) {
520 int line_len
= strlen(line
);
522 if (line
[line_len
- 1] == '\n') {
523 line
[line_len
- 1] = '\0';
532 if (parse_theme_line(line
, &key
, &value
)) {
533 if (strcmp(key
, "Inherits") == 0) {
534 // value is like oxygen,wood,default
536 token
= strtok(value
, ",\n");
537 while (token
!= NULL
)
539 theme
->list_inherits
= g_slist_append(theme
->list_inherits
, strdup(token
));
540 token
= strtok(NULL
, ",\n");
542 } else if (strcmp(key
, "Directories") == 0) {
543 // value is like 48x48/apps,48x48/mimetypes,32x32/apps,scalable/apps,scalable/mimetypes
545 token
= strtok(value
, ",\n");
546 while (token
!= NULL
)
548 IconThemeDir
*dir
= calloc(1, sizeof(IconThemeDir
));
549 dir
->name
= strdup(token
);
550 dir
->max_size
= dir
->min_size
= dir
->size
= -1;
551 dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
553 theme
->list_directories
= g_slist_append(theme
->list_directories
, dir
);
554 token
= strtok(NULL
, ",\n");
558 } else if (current_dir
!= NULL
) {
559 if (parse_theme_line(line
, &key
, &value
)) {
560 if (strcmp(key
, "Size") == 0) {
562 sscanf(value
, "%d", ¤t_dir
->size
);
563 if (current_dir
->max_size
== -1)
564 current_dir
->max_size
= current_dir
->size
;
565 if (current_dir
->min_size
== -1)
566 current_dir
->min_size
= current_dir
->size
;
567 } else if (strcmp(key
, "MaxSize") == 0) {
569 sscanf(value
, "%d", ¤t_dir
->max_size
);
570 } else if (strcmp(key
, "MinSize") == 0) {
572 sscanf(value
, "%d", ¤t_dir
->min_size
);
573 } else if (strcmp(key
, "Threshold") == 0) {
575 sscanf(value
, "%d", ¤t_dir
->threshold
);
576 } else if (strcmp(key
, "Type") == 0) {
577 // value is Fixed, Scalable or Threshold : default to scalable for unknown Type.
578 if (strcmp(value
, "Fixed") == 0) {
579 current_dir
->type
= ICON_DIR_TYPE_FIXED
;
580 } else if (strcmp(value
, "Threshold") == 0) {
581 current_dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
583 current_dir
->type
= ICON_DIR_TYPE_SCALABLE
;
585 } else if (strcmp(key
, "Context") == 0) {
586 // usual values: Actions, Applications, Devices, FileSystems, MimeTypes
587 current_dir
->context
= strdup(value
);
592 if (line
[0] == '[' && line
[line_len
- 1] == ']' && strcmp(line
, "[Icon Theme]") != 0) {
595 line
[line_len
- 1] = '\0';
596 char *dir_name
= line
+ 1;
597 GSList
* dir_item
= theme
->list_directories
;
598 while (dir_item
!= NULL
)
600 IconThemeDir
*dir
= dir_item
->data
;
601 if (strcmp(dir
->name
, dir_name
) == 0) {
605 dir_item
= g_slist_next(dir_item
);
614 void free_icon_theme(IconTheme
*theme
)
618 for (l_inherits
= theme
->list_inherits
; l_inherits
; l_inherits
= l_inherits
->next
) {
619 free(l_inherits
->data
);
622 for (l_dir
= theme
->list_directories
; l_dir
; l_dir
= l_dir
->next
) {
623 IconThemeDir
*dir
= (IconThemeDir
*)l_dir
->data
;
630 void test_launcher_read_theme_file()
632 fprintf(stdout
, "\033[1;33m");
633 IconTheme
*theme
= load_theme("oxygen");
635 printf("Could not load theme\n");
638 printf("Loaded theme: %s\n", theme
->name
);
639 GSList
* item
= theme
->list_inherits
;
642 printf("Inherits:%s\n", (char*)item
->data
);
643 item
= g_slist_next(item
);
645 item
= theme
->list_directories
;
648 IconThemeDir
*dir
= item
->data
;
649 printf("Dir:%s Size=%d MinSize=%d MaxSize=%d Threshold=%d Type=%s Context=%s\n",
650 dir
->name
, dir
->size
, dir
->min_size
, dir
->max_size
, dir
->threshold
,
651 dir
->type
== ICON_DIR_TYPE_FIXED
? "Fixed" :
652 dir
->type
== ICON_DIR_TYPE_SCALABLE
? "Scalable" :
653 dir
->type
== ICON_DIR_TYPE_THRESHOLD
? "Threshold" : "?????",
655 item
= g_slist_next(item
);
657 fprintf(stdout
, "\033[0m");
660 // Populates the icon_themes list
661 void launcher_load_themes(Launcher
*launcher
)
663 // load the user theme, all the inherited themes recursively (DFS), and the hicolor theme
664 // avoid inheritance loops
665 if (!icon_theme_name
) {
666 fprintf(stderr
, "Missing launcher theme, default to 'hicolor'.\n");
667 icon_theme_name
= "hicolor";
670 fprintf(stderr
, "Loading %s : ", icon_theme_name
);
672 GSList
*queue
= g_slist_append(NULL
, strdup(icon_theme_name
));
673 GSList
*queued
= g_slist_append(NULL
, strdup(icon_theme_name
));
675 int hicolor_loaded
= 0;
676 while (queue
|| !hicolor_loaded
) {
678 GSList
* queued_item
= queued
;
679 while (queued_item
!= NULL
) {
680 if (strcmp(queued_item
->data
, "hicolor") == 0) {
684 queued_item
= g_slist_next(queued_item
);
688 queue
= g_slist_append(queue
, strdup("hicolor"));
689 queued
= g_slist_append(queued
, strdup("hicolor"));
692 char *name
= queue
->data
;
693 queue
= g_slist_remove(queue
, name
);
695 fprintf(stderr
, "icon theme '%s', ", 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
);
723 fprintf(stderr
, "\n");
727 for (l
= queue
; l
; l
= l
->next
)
730 for (l
= queued
; l
; l
= l
->next
)
732 g_slist_free(queued
);
735 int directory_matches_size(IconThemeDir
*dir
, int size
)
737 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
738 return dir
->size
== size
;
739 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
740 return dir
->min_size
<= size
&& size
<= dir
->max_size
;
741 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
742 return dir
->size
- dir
->threshold
<= size
&& size
<= dir
->size
+ dir
->threshold
;
746 int directory_size_distance(IconThemeDir
*dir
, int size
)
748 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
749 return abs(dir
->size
- size
);
750 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
751 if (size
< dir
->min_size
) {
752 return dir
->min_size
- size
;
753 } else if (size
> dir
->max_size
) {
754 return size
- dir
->max_size
;
758 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
759 if (size
< dir
->size
- dir
->threshold
) {
760 return dir
->min_size
- size
;
761 } else if (size
> dir
->size
+ dir
->threshold
) {
762 return size
- dir
->max_size
;
769 // Returns the full path to an icon file (or NULL) given the icon name
770 char *icon_path(Launcher
*launcher
, const char *icon_name
, int size
)
772 if (icon_name
== NULL
)
775 // If the icon_name is already a path and the file exists, return it
776 if (strstr(icon_name
, "/") == icon_name
) {
777 if (g_file_test(icon_name
, G_FILE_TEST_EXISTS
))
778 return strdup(icon_name
);
783 GSList
*basenames
= NULL
;
784 char *file_name
= g_build_filename(g_get_home_dir(), ".icons", NULL
);
785 basenames
= g_slist_append(basenames
, file_name
);
786 basenames
= g_slist_append(basenames
, "/usr/share/icons");
787 basenames
= g_slist_append(basenames
, "/usr/share/pixmaps");
789 GSList
*extensions
= NULL
;
790 extensions
= g_slist_append(extensions
, "png");
791 extensions
= g_slist_append(extensions
, "xpm");
793 // Stage 1: exact size match
795 for (theme
= launcher
->icon_themes
; theme
; theme
= g_slist_next(theme
)) {
797 for (dir
= ((IconTheme
*)theme
->data
)->list_directories
; dir
; dir
= g_slist_next(dir
)) {
798 if (directory_matches_size((IconThemeDir
*)dir
->data
, size
)) {
800 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
802 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
803 char *base_name
= (char*) base
->data
;
804 char *theme_name
= ((IconTheme
*)theme
->data
)->name
;
805 char *dir_name
= ((IconThemeDir
*)dir
->data
)->name
;
806 char *extension
= (char*) ext
->data
;
807 char *file_name
= malloc(strlen(base_name
) + strlen(theme_name
) +
808 strlen(dir_name
) + strlen(icon_name
) + strlen(extension
) + 100);
809 // filename = directory/$(themename)/subdirectory/iconname.extension
810 sprintf(file_name
, "%s/%s/%s/%s.%s", base_name
, theme_name
, dir_name
, icon_name
, extension
);
811 //printf("checking %s\n", file_name);
812 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
813 g_slist_free(basenames
);
814 g_slist_free(extensions
);
827 // Stage 2: best size match
828 // Contrary to the freedesktop spec, we are not choosing the closest icon in size, but the next larger icon
829 // otherwise the quality is usually crap (for size 22, if you can choose 16 or 32, you're better with 32)
830 // We do fallback to the closest size if we cannot find a larger or equal icon
831 int minimal_size
= INT_MAX
;
832 char *best_file_name
= NULL
;
833 int next_larger_size
= -1;
834 char *next_larger
= NULL
;
835 for (theme
= launcher
->icon_themes
; theme
; theme
= g_slist_next(theme
)) {
837 for (dir
= ((IconTheme
*)theme
->data
)->list_directories
; dir
; dir
= g_slist_next(dir
)) {
839 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
841 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
842 char *base_name
= (char*) base
->data
;
843 char *theme_name
= ((IconTheme
*)theme
->data
)->name
;
844 char *dir_name
= ((IconThemeDir
*)dir
->data
)->name
;
845 char *extension
= (char*) ext
->data
;
846 char *file_name
= malloc(strlen(base_name
) + strlen(theme_name
) +
847 strlen(dir_name
) + strlen(icon_name
) + strlen(extension
) + 100);
848 // filename = directory/$(themename)/subdirectory/iconname.extension
849 sprintf(file_name
, "%s/%s/%s/%s.%s", base_name
, theme_name
, dir_name
, icon_name
, extension
);
850 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
851 if (directory_size_distance((IconThemeDir
*)dir
->data
, size
) < minimal_size
) {
852 if (best_file_name
) {
853 free(best_file_name
);
854 best_file_name
= NULL
;
856 best_file_name
= strdup(file_name
);
857 minimal_size
= directory_size_distance((IconThemeDir
*)dir
->data
, size
);
859 if (((IconThemeDir
*)dir
->data
)->size
>= size
&& (next_larger_size
== -1 || ((IconThemeDir
*)dir
->data
)->size
< next_larger_size
)) {
864 next_larger
= strdup(file_name
);
865 next_larger_size
= ((IconThemeDir
*)dir
->data
)->size
;
874 g_slist_free(basenames
);
875 g_slist_free(extensions
);
876 free(best_file_name
);
879 if (best_file_name
) {
880 g_slist_free(basenames
);
881 g_slist_free(extensions
);
882 return best_file_name
;
885 // Stage 3: look in unthemed icons
888 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
890 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
891 char *base_name
= (char*) base
->data
;
892 char *extension
= (char*) ext
->data
;
893 char *file_name
= malloc(strlen(base_name
) + strlen(icon_name
) +
894 strlen(extension
) + 100);
895 // filename = directory/iconname.extension
896 sprintf(file_name
, "%s/%s.%s", base_name
, icon_name
, extension
);
897 //printf("checking %s\n", file_name);
898 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
899 g_slist_free(basenames
);
900 g_slist_free(extensions
);
910 fprintf(stderr
, "Could not find icon %s\n", icon_name
);
This page took 0.077574 seconds and 3 git commands to generate.