]>
Dogcows Code - chaz/tint2/blob - src/launcher/launcher.c
0cb9fc88157a800355f2efd73b54cdfbebf89747
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 "exec"
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
= draw_launcher
;
77 launcher
->area
.size_mode
= SIZE_BY_CONTENT
;
78 launcher
->area
._resize
= resize_launcher
;
79 launcher
->area
.resize
= 1;
80 launcher
->area
.redraw
= 1;
83 if (launcher
->list_apps
== NULL
)
86 launcher
->area
.on_screen
= 1;
89 launcher_load_themes(launcher
);
90 launcher_load_icons(launcher
);
94 void cleanup_launcher()
99 xsettings_client_destroy(xsettings_client
);
100 for (i
= 0 ; i
< nb_panel
; i
++) {
101 Panel
*panel
= &panel1
[i
];
102 Launcher
*launcher
= &panel
->launcher
;
103 cleanup_launcher_theme(launcher
);
106 for (l
= launcher
->list_apps
; l
; l
= l
->next
) {
109 g_slist_free(launcher
->list_apps
);
110 launcher
->list_apps
= NULL
;
112 g_free(icon_theme_name
);
113 launcher_enabled
= 0;
117 void cleanup_launcher_theme(Launcher
*launcher
)
119 free_area(&launcher
->area
);
121 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
122 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
124 free_icon(launcherIcon
->icon_scaled
);
125 free_icon(launcherIcon
->icon_original
);
126 free(launcherIcon
->icon_name
);
127 free(launcherIcon
->icon_path
);
128 free(launcherIcon
->cmd
);
132 g_slist_free(launcher
->list_icons
);
134 for (l
= launcher
->list_themes
; l
; l
= l
->next
) {
135 IconTheme
*theme
= (IconTheme
*) l
->data
;
136 free_icon_theme(theme
);
139 g_slist_free(launcher
->list_themes
);
140 launcher
->list_icons
= launcher
->list_themes
= NULL
;
144 int resize_launcher(void *obj
)
146 Launcher
*launcher
= obj
;
148 int count
, icon_size
;
149 int icons_per_column
=1, icons_per_row
=1, marging
=0;
151 if (panel_horizontal
)
152 icon_size
= launcher
->area
.height
;
154 icon_size
= launcher
->area
.width
;
155 icon_size
= icon_size
- (2 * launcher
->area
.bg
->border
.width
) - (2 * launcher
->area
.paddingy
);
156 if (launcher_max_icon_size
> 0 && icon_size
> launcher_max_icon_size
)
157 icon_size
= launcher_max_icon_size
;
159 // Resize icons if necessary
160 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
161 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
162 if (launcherIcon
->icon_size
!= icon_size
|| !launcherIcon
->icon_original
) {
163 launcherIcon
->icon_size
= icon_size
;
165 // Get the path for an icon file with the new size
166 char *new_icon_path
= icon_path(launcher
, launcherIcon
->icon_name
, launcherIcon
->icon_size
);
167 if (!new_icon_path
) {
169 free_icon(launcherIcon
->icon_original
);
170 launcherIcon
->icon_original
= NULL
;
171 free_icon(launcherIcon
->icon_scaled
);
172 launcherIcon
->icon_scaled
= NULL
;
173 new_icon_path
= icon_path(launcher
, ICON_FALLBACK
, launcherIcon
->icon_size
);
175 launcherIcon
->icon_original
= imlib_load_image(new_icon_path
);
176 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, new_icon_path
);
179 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, icon_size
);
182 if (launcherIcon
->icon_path
&& strcmp(new_icon_path
, launcherIcon
->icon_path
) == 0) {
183 // If it's the same file just rescale
184 free_icon(launcherIcon
->icon_scaled
);
185 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, icon_size
);
187 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, launcherIcon
->icon_path
);
189 // Free the old files
190 free_icon(launcherIcon
->icon_original
);
191 free_icon(launcherIcon
->icon_scaled
);
192 // Load the new file and scale
193 launcherIcon
->icon_original
= imlib_load_image(new_icon_path
);
194 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, launcherIcon
->icon_size
);
195 free(launcherIcon
->icon_path
);
196 launcherIcon
->icon_path
= new_icon_path
;
197 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, launcherIcon
->icon_path
);
203 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
207 if (panel_horizontal
) {
208 if (!count
) launcher
->area
.width
= 0;
210 int height
= launcher
->area
.height
- 2*launcher
->area
.bg
->border
.width
- 2*launcher
->area
.paddingy
;
211 // here icons_per_column always higher than 0
212 icons_per_column
= (height
+launcher
->area
.paddingx
) / (icon_size
+launcher
->area
.paddingx
);
213 marging
= height
- (icons_per_column
-1)*(icon_size
+launcher
->area
.paddingx
) - icon_size
;
214 icons_per_row
= count
/ icons_per_column
+ (count%icons_per_column
!= 0);
215 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
);
219 if (!count
) launcher
->area
.height
= 0;
221 int width
= launcher
->area
.width
- 2*launcher
->area
.bg
->border
.width
- 2*launcher
->area
.paddingy
;
222 // here icons_per_row always higher than 0
223 icons_per_row
= (width
+launcher
->area
.paddingx
) / (icon_size
+launcher
->area
.paddingx
);
224 marging
= width
- (icons_per_row
-1)*(icon_size
+launcher
->area
.paddingx
) - icon_size
;
225 icons_per_column
= count
/ icons_per_row
+ (count%icons_per_row
!= 0);
226 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
);
231 int start
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingy
;// +marging/2;
232 if (panel_horizontal
) {
234 posx
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingxlr
;
238 posy
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingxlr
;
241 for (i
=1, l
= launcher
->list_icons
; l
; i
++, l
= l
->next
) {
242 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
244 launcherIcon
->y
= posy
;
245 launcherIcon
->x
= posx
;
246 if (panel_horizontal
) {
247 if (i
% icons_per_column
)
248 posy
+= icon_size
+ launcher
->area
.paddingx
;
251 posx
+= (icon_size
+ launcher
->area
.paddingx
);
255 if (i
% icons_per_row
)
256 posx
+= icon_size
+ launcher
->area
.paddingx
;
259 posy
+= (icon_size
+ launcher
->area
.paddingx
);
267 void draw_launcher(void *obj
, cairo_t
*c
)
269 Launcher
*launcher
= obj
;
271 if (launcher
->list_icons
== 0) return;
273 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
274 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
275 int pos_x
= launcherIcon
->x
;
276 int pos_y
= launcherIcon
->y
;
277 Imlib_Image icon_scaled
= launcherIcon
->icon_scaled
;
279 imlib_context_set_image (icon_scaled
);
280 if (server
.real_transparency
) {
281 render_image(launcher
->area
.pix
, pos_x
, pos_y
, imlib_image_get_width(), imlib_image_get_height() );
284 imlib_context_set_drawable(launcher
->area
.pix
);
285 imlib_render_image_on_drawable (pos_x
, pos_y
);
290 Imlib_Image
scale_icon(Imlib_Image original
, int icon_size
)
292 Imlib_Image icon_scaled
;
294 imlib_context_set_image (original
);
295 icon_scaled
= imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), icon_size
, icon_size
);
297 icon_scaled
= imlib_create_image(icon_size
, icon_size
);
298 imlib_context_set_image (icon_scaled
);
299 imlib_context_set_color(255, 255, 255, 255);
300 imlib_image_fill_rectangle(0, 0, icon_size
, icon_size
);
305 void free_icon(Imlib_Image icon
)
308 imlib_context_set_image(icon
);
313 void launcher_action(LauncherIcon
*icon
)
315 char *cmd
= malloc(strlen(icon
->cmd
) + 10);
316 sprintf(cmd
, "(%s&)", icon
->cmd
);
321 /***************** Freedesktop app.desktop and icon theme handling *********************/
322 /* http://standards.freedesktop.org/desktop-entry-spec/ */
323 /* http://standards.freedesktop.org/icon-theme-spec/ */
325 // Splits line at first '=' and returns 1 if successful, and parts are not empty
326 // key and value point to the parts
327 int parse_dektop_line(char *line
, char **key
, char **value
)
332 for (p
= line
; *p
; p
++) {
342 if (found
&& (strlen(*key
) == 0 || strlen(*value
) == 0))
347 int parse_theme_line(char *line
, char **key
, char **value
)
349 return parse_dektop_line(line
, key
, value
);
352 void expand_exec(DesktopEntry
*entry
, const char *path
)
359 char *exec2
= malloc(strlen(entry
->exec
) + (entry
->name
? strlen(entry
->name
) : 1) + (entry
->icon
? strlen(entry
->icon
) : 1) + 100);
361 // p will never point to an escaped char
362 for (p
= entry
->exec
, q
= exec2
; *p
; p
++, q
++) {
366 // Copy the escaped char
367 if (*p
== '%') // For % we delete the backslash, i.e. write % over it
376 if (*p
== 'i' && entry
->icon
!= NULL
) {
377 sprintf(q
, "--icon '%s'", entry
->icon
);
378 q
+= strlen("--icon ''");
379 q
+= strlen(entry
->icon
);
380 q
--; // To balance the q++ in the for
381 } else if (*p
== 'c' && entry
->name
!= NULL
) {
382 sprintf(q
, "'%s'", entry
->name
);
384 q
+= strlen(entry
->name
);
385 q
--; // To balance the q++ in the for
386 } else if (*p
== 'c') {
387 sprintf(q
, "'%s'", path
);
390 q
--; // To balance the q++ in the for
392 // We don't care about other expansions
393 q
--; // Delete the last % from q
404 //TODO Use UTF8 when parsing the file
405 int launcher_read_desktop_file(const char *path
, DesktopEntry
*entry
)
411 entry
->name
= entry
->icon
= entry
->exec
= NULL
;
413 if ((fp
= fopen(path
, "rt")) == NULL
) {
414 fprintf(stderr
, "Could not open file %s\n", path
);
418 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
419 int len
= strlen(line
);
422 line
[len
- 1] = '\0';
423 if (parse_dektop_line(line
, &key
, &value
)) {
424 if (strcmp(key
, "Name") == 0) {
425 entry
->name
= strdup(value
);
426 } else if (strcmp(key
, "Exec") == 0) {
427 entry
->exec
= strdup(value
);
428 } else if (strcmp(key
, "Icon") == 0) {
429 entry
->icon
= strdup(value
);
435 // entry->name, entry->icon, entry->exec will never be empty strings (can be NULL though)
437 expand_exec(entry
, path
);
442 void free_desktop_entry(DesktopEntry
*entry
)
449 void test_launcher_read_desktop_file()
451 fprintf(stdout
, "\033[1;33m");
453 launcher_read_desktop_file("/usr/share/applications/firefox.desktop", &entry
);
454 printf("Name:%s Icon:%s Exec:%s\n", entry
.name
, entry
.icon
, entry
.exec
);
455 fprintf(stdout
, "\033[0m");
458 //TODO Use UTF8 when parsing the file
459 IconTheme
*load_theme(char *name
)
461 // Look for name/index.theme in $HOME/.icons, /usr/share/icons, /usr/share/pixmaps (stop at the first found)
462 // Parse index.theme -> list of IconThemeDir with attributes
473 file_name
= g_build_filename(g_get_home_dir(), ".icons", name
, "index.theme", NULL
);
474 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
476 file_name
= g_build_filename("/usr/share/icons", name
, "index.theme", NULL
);
477 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
479 file_name
= g_build_filename("/usr/share/pixmaps", name
, "index.theme", NULL
);
480 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
491 if ((f
= fopen(file_name
, "rt")) == NULL
) {
492 fprintf(stderr
, "Could not open theme '%s'\n", file_name
);
498 theme
= calloc(1, sizeof(IconTheme
));
499 theme
->name
= strdup(name
);
500 theme
->list_inherits
= NULL
;
501 theme
->list_directories
= NULL
;
503 IconThemeDir
*current_dir
= NULL
;
504 int inside_header
= 1;
505 while (fgets(line
, sizeof(line
), f
) != NULL
) {
508 int line_len
= strlen(line
);
510 if (line
[line_len
- 1] == '\n') {
511 line
[line_len
- 1] = '\0';
520 if (parse_theme_line(line
, &key
, &value
)) {
521 if (strcmp(key
, "Inherits") == 0) {
522 // value is like oxygen,wood,default
524 token
= strtok(value
, ",\n");
525 while (token
!= NULL
)
527 theme
->list_inherits
= g_slist_append(theme
->list_inherits
, strdup(token
));
528 token
= strtok(NULL
, ",\n");
530 } else if (strcmp(key
, "Directories") == 0) {
531 // value is like 48x48/apps,48x48/mimetypes,32x32/apps,scalable/apps,scalable/mimetypes
533 token
= strtok(value
, ",\n");
534 while (token
!= NULL
)
536 IconThemeDir
*dir
= calloc(1, sizeof(IconThemeDir
));
537 dir
->name
= strdup(token
);
538 dir
->max_size
= dir
->min_size
= dir
->size
= -1;
539 dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
541 theme
->list_directories
= g_slist_append(theme
->list_directories
, dir
);
542 token
= strtok(NULL
, ",\n");
546 } else if (current_dir
!= NULL
) {
547 if (parse_theme_line(line
, &key
, &value
)) {
548 if (strcmp(key
, "Size") == 0) {
550 sscanf(value
, "%d", ¤t_dir
->size
);
551 if (current_dir
->max_size
== -1)
552 current_dir
->max_size
= current_dir
->size
;
553 if (current_dir
->min_size
== -1)
554 current_dir
->min_size
= current_dir
->size
;
555 } else if (strcmp(key
, "MaxSize") == 0) {
557 sscanf(value
, "%d", ¤t_dir
->max_size
);
558 } else if (strcmp(key
, "MinSize") == 0) {
560 sscanf(value
, "%d", ¤t_dir
->min_size
);
561 } else if (strcmp(key
, "Threshold") == 0) {
563 sscanf(value
, "%d", ¤t_dir
->threshold
);
564 } else if (strcmp(key
, "Type") == 0) {
565 // value is Fixed, Scalable or Threshold : default to scalable for unknown Type.
566 if (strcmp(value
, "Fixed") == 0) {
567 current_dir
->type
= ICON_DIR_TYPE_FIXED
;
568 } else if (strcmp(value
, "Threshold") == 0) {
569 current_dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
571 current_dir
->type
= ICON_DIR_TYPE_SCALABLE
;
573 } else if (strcmp(key
, "Context") == 0) {
574 // usual values: Actions, Applications, Devices, FileSystems, MimeTypes
575 current_dir
->context
= strdup(value
);
580 if (line
[0] == '[' && line
[line_len
- 1] == ']' && strcmp(line
, "[Icon Theme]") != 0) {
583 line
[line_len
- 1] = '\0';
584 char *dir_name
= line
+ 1;
585 GSList
* dir_item
= theme
->list_directories
;
586 while (dir_item
!= NULL
)
588 IconThemeDir
*dir
= dir_item
->data
;
589 if (strcmp(dir
->name
, dir_name
) == 0) {
593 dir_item
= g_slist_next(dir_item
);
602 void free_icon_theme(IconTheme
*theme
)
606 for (l_inherits
= theme
->list_inherits
; l_inherits
; l_inherits
= l_inherits
->next
) {
607 free(l_inherits
->data
);
610 for (l_dir
= theme
->list_directories
; l_dir
; l_dir
= l_dir
->next
) {
611 IconThemeDir
*dir
= (IconThemeDir
*)l_dir
->data
;
618 void test_launcher_read_theme_file()
620 fprintf(stdout
, "\033[1;33m");
621 IconTheme
*theme
= load_theme("oxygen");
623 printf("Could not load theme\n");
626 printf("Loaded theme: %s\n", theme
->name
);
627 GSList
* item
= theme
->list_inherits
;
630 printf("Inherits:%s\n", (char*)item
->data
);
631 item
= g_slist_next(item
);
633 item
= theme
->list_directories
;
636 IconThemeDir
*dir
= item
->data
;
637 printf("Dir:%s Size=%d MinSize=%d MaxSize=%d Threshold=%d Type=%s Context=%s\n",
638 dir
->name
, dir
->size
, dir
->min_size
, dir
->max_size
, dir
->threshold
,
639 dir
->type
== ICON_DIR_TYPE_FIXED
? "Fixed" :
640 dir
->type
== ICON_DIR_TYPE_SCALABLE
? "Scalable" :
641 dir
->type
== ICON_DIR_TYPE_THRESHOLD
? "Threshold" : "?????",
643 item
= g_slist_next(item
);
645 fprintf(stdout
, "\033[0m");
649 // Populates the list_icons list
650 void launcher_load_icons(Launcher
*launcher
)
652 // Load apps (.desktop style launcher items)
653 GSList
* app
= launcher
->list_apps
;
654 while (app
!= NULL
) {
656 launcher_read_desktop_file(app
->data
, &entry
);
658 LauncherIcon
*launcherIcon
= calloc(1, sizeof(LauncherIcon
));
659 launcherIcon
->is_app_desktop
= 1;
660 launcherIcon
->cmd
= strdup(entry
.exec
);
661 launcherIcon
->icon_name
= entry
.icon
? strdup(entry
.icon
) : strdup(ICON_FALLBACK
);
662 launcherIcon
->icon_size
= 1;
663 free_desktop_entry(&entry
);
664 launcher
->list_icons
= g_slist_append(launcher
->list_icons
, launcherIcon
);
666 app
= g_slist_next(app
);
671 // Populates the list_themes list
672 void launcher_load_themes(Launcher
*launcher
)
674 // load the user theme, all the inherited themes recursively (DFS), and the hicolor theme
675 // avoid inheritance loops
676 if (!icon_theme_name
) {
677 fprintf(stderr
, "Missing launcher theme, default to 'hicolor'.\n");
678 icon_theme_name
= "hicolor";
681 fprintf(stderr
, "Loading %s. Icon theme :", icon_theme_name
);
683 GSList
*queue
= g_slist_append(NULL
, strdup(icon_theme_name
));
684 GSList
*queued
= g_slist_append(NULL
, strdup(icon_theme_name
));
686 int hicolor_loaded
= 0;
687 while (queue
|| !hicolor_loaded
) {
689 GSList
* queued_item
= queued
;
690 while (queued_item
!= NULL
) {
691 if (strcmp(queued_item
->data
, "hicolor") == 0) {
695 queued_item
= g_slist_next(queued_item
);
699 queue
= g_slist_append(queue
, strdup("hicolor"));
700 queued
= g_slist_append(queued
, strdup("hicolor"));
703 char *name
= queue
->data
;
704 queue
= g_slist_remove(queue
, name
);
706 fprintf(stderr
, " '%s',", name
);
707 IconTheme
*theme
= load_theme(name
);
709 launcher
->list_themes
= g_slist_append(launcher
->list_themes
, theme
);
711 GSList
* item
= theme
->list_inherits
;
715 char *parent
= item
->data
;
717 GSList
* queued_item
= queued
;
718 while (queued_item
!= NULL
) {
719 if (strcmp(queued_item
->data
, parent
) == 0) {
723 queued_item
= g_slist_next(queued_item
);
726 queue
= g_slist_insert(queue
, strdup(parent
), pos
);
728 queued
= g_slist_append(queued
, strdup(parent
));
730 item
= g_slist_next(item
);
734 fprintf(stderr
, "\n");
738 for (l
= queue
; l
; l
= l
->next
)
741 for (l
= queued
; l
; l
= l
->next
)
743 g_slist_free(queued
);
746 int directory_matches_size(IconThemeDir
*dir
, int size
)
748 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
749 return dir
->size
== size
;
750 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
751 return dir
->min_size
<= size
&& size
<= dir
->max_size
;
752 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
753 return dir
->size
- dir
->threshold
<= size
&& size
<= dir
->size
+ dir
->threshold
;
757 int directory_size_distance(IconThemeDir
*dir
, int size
)
759 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
760 return abs(dir
->size
- size
);
761 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
762 if (size
< dir
->min_size
) {
763 return dir
->min_size
- size
;
764 } else if (size
> dir
->max_size
) {
765 return size
- dir
->max_size
;
769 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
770 if (size
< dir
->size
- dir
->threshold
) {
771 return dir
->min_size
- size
;
772 } else if (size
> dir
->size
+ dir
->threshold
) {
773 return size
- dir
->max_size
;
780 // Returns the full path to an icon file (or NULL) given the icon name
781 char *icon_path(Launcher
*launcher
, const char *icon_name
, int size
)
783 if (icon_name
== NULL
)
786 // If the icon_name is already a path and the file exists, return it
787 if (strstr(icon_name
, "/") == icon_name
) {
788 if (g_file_test(icon_name
, G_FILE_TEST_EXISTS
))
789 return strdup(icon_name
);
794 GSList
*basenames
= NULL
;
795 char *file_name
= g_build_filename(g_get_home_dir(), ".icons", NULL
);
796 basenames
= g_slist_append(basenames
, file_name
);
797 basenames
= g_slist_append(basenames
, "/usr/share/icons");
798 basenames
= g_slist_append(basenames
, "/usr/share/pixmaps");
800 GSList
*extensions
= NULL
;
801 extensions
= g_slist_append(extensions
, "png");
802 extensions
= g_slist_append(extensions
, "xpm");
804 // Stage 1: exact size match
806 for (theme
= launcher
->list_themes
; theme
; theme
= g_slist_next(theme
)) {
808 for (dir
= ((IconTheme
*)theme
->data
)->list_directories
; dir
; dir
= g_slist_next(dir
)) {
809 if (directory_matches_size((IconThemeDir
*)dir
->data
, size
)) {
811 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
813 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
814 char *base_name
= (char*) base
->data
;
815 char *theme_name
= ((IconTheme
*)theme
->data
)->name
;
816 char *dir_name
= ((IconThemeDir
*)dir
->data
)->name
;
817 char *extension
= (char*) ext
->data
;
818 char *file_name
= malloc(strlen(base_name
) + strlen(theme_name
) +
819 strlen(dir_name
) + strlen(icon_name
) + strlen(extension
) + 100);
820 // filename = directory/$(themename)/subdirectory/iconname.extension
821 sprintf(file_name
, "%s/%s/%s/%s.%s", base_name
, theme_name
, dir_name
, icon_name
, extension
);
822 //printf("checking %s\n", file_name);
823 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
824 g_slist_free(basenames
);
825 g_slist_free(extensions
);
838 // Stage 2: best size match
839 // Contrary to the freedesktop spec, we are not choosing the closest icon in size, but the next larger icon
840 // otherwise the quality is usually crap (for size 22, if you can choose 16 or 32, you're better with 32)
841 // We do fallback to the closest size if we cannot find a larger or equal icon
842 int minimal_size
= INT_MAX
;
843 char *best_file_name
= NULL
;
844 int next_larger_size
= -1;
845 char *next_larger
= NULL
;
846 for (theme
= launcher
->list_themes
; theme
; theme
= g_slist_next(theme
)) {
848 for (dir
= ((IconTheme
*)theme
->data
)->list_directories
; dir
; dir
= g_slist_next(dir
)) {
850 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
852 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
853 char *base_name
= (char*) base
->data
;
854 char *theme_name
= ((IconTheme
*)theme
->data
)->name
;
855 char *dir_name
= ((IconThemeDir
*)dir
->data
)->name
;
856 char *extension
= (char*) ext
->data
;
857 char *file_name
= malloc(strlen(base_name
) + strlen(theme_name
) +
858 strlen(dir_name
) + strlen(icon_name
) + strlen(extension
) + 100);
859 // filename = directory/$(themename)/subdirectory/iconname.extension
860 sprintf(file_name
, "%s/%s/%s/%s.%s", base_name
, theme_name
, dir_name
, icon_name
, extension
);
861 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
862 if (directory_size_distance((IconThemeDir
*)dir
->data
, size
) < minimal_size
) {
863 if (best_file_name
) {
864 free(best_file_name
);
865 best_file_name
= NULL
;
867 best_file_name
= strdup(file_name
);
868 minimal_size
= directory_size_distance((IconThemeDir
*)dir
->data
, size
);
870 if (((IconThemeDir
*)dir
->data
)->size
>= size
&& (next_larger_size
== -1 || ((IconThemeDir
*)dir
->data
)->size
< next_larger_size
)) {
875 next_larger
= strdup(file_name
);
876 next_larger_size
= ((IconThemeDir
*)dir
->data
)->size
;
885 g_slist_free(basenames
);
886 g_slist_free(extensions
);
887 free(best_file_name
);
890 if (best_file_name
) {
891 g_slist_free(basenames
);
892 g_slist_free(extensions
);
893 return best_file_name
;
896 // Stage 3: look in unthemed icons
899 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
901 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
902 char *base_name
= (char*) base
->data
;
903 char *extension
= (char*) ext
->data
;
904 char *file_name
= malloc(strlen(base_name
) + strlen(icon_name
) +
905 strlen(extension
) + 100);
906 // filename = directory/iconname.extension
907 sprintf(file_name
, "%s/%s.%s", base_name
, icon_name
, extension
);
908 //printf("checking %s\n", file_name);
909 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
910 g_slist_free(basenames
);
911 g_slist_free(extensions
);
921 fprintf(stderr
, "Could not find icon %s\n", icon_name
);
This page took 0.079628 seconds and 4 git commands to generate.