]>
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 char *icon_theme_name
;
38 XSettingsClient
*xsettings_client
;
40 #define ICON_FALLBACK "application-x-executable"
42 char *icon_path(Launcher
*launcher
, const char *icon_name
, int size
);
43 void launcher_load_themes(Launcher
*launcher
);
44 void free_desktop_entry(DesktopEntry
*entry
);
45 int launcher_read_desktop_file(const char *path
, DesktopEntry
*entry
);
46 Imlib_Image
scale_icon(Imlib_Image original
, int icon_size
);
47 void free_icon(Imlib_Image icon
);
48 void free_icon_theme(IconTheme
*theme
);
51 void default_launcher()
54 launcher_max_icon_size
= 0;
56 xsettings_client
= NULL
;
62 if (launcher_enabled
) {
63 // if XSETTINGS manager running, tint2 read the icon_theme_name.
64 xsettings_client
= xsettings_client_new(server
.dsp
, server
.screen
, xsettings_notify_cb
, NULL
, NULL
);
69 void init_launcher_panel(void *p
)
71 Panel
*panel
=(Panel
*)p
;
72 Launcher
*launcher
= &panel
->launcher
;
74 launcher
->area
.parent
= p
;
75 launcher
->area
.panel
= p
;
76 launcher
->area
._draw_foreground
= 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;
81 if (launcher
->area
.bg
== 0)
82 launcher
->area
.bg
= &g_array_index(backgrounds
, Background
, 0);
85 if (launcher
->list_apps
== NULL
)
88 launcher
->area
.on_screen
= 1;
91 launcher_load_themes(launcher
);
92 launcher_load_icons(launcher
);
96 void cleanup_launcher()
100 if (xsettings_client
)
101 xsettings_client_destroy(xsettings_client
);
102 for (i
= 0 ; i
< nb_panel
; i
++) {
103 Panel
*panel
= &panel1
[i
];
104 Launcher
*launcher
= &panel
->launcher
;
105 cleanup_launcher_theme(launcher
);
108 for (l
= launcher
->list_apps
; l
; l
= l
->next
) {
111 g_slist_free(launcher
->list_apps
);
112 launcher
->list_apps
= NULL
;
114 g_free(icon_theme_name
);
115 launcher_enabled
= 0;
119 void cleanup_launcher_theme(Launcher
*launcher
)
121 free_area(&launcher
->area
);
123 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
124 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
126 free_icon(launcherIcon
->icon_scaled
);
127 free_icon(launcherIcon
->icon_original
);
128 free(launcherIcon
->icon_name
);
129 free(launcherIcon
->icon_path
);
130 free(launcherIcon
->cmd
);
134 g_slist_free(launcher
->list_icons
);
136 for (l
= launcher
->list_themes
; l
; l
= l
->next
) {
137 IconTheme
*theme
= (IconTheme
*) l
->data
;
138 free_icon_theme(theme
);
141 g_slist_free(launcher
->list_themes
);
142 launcher
->list_icons
= launcher
->list_themes
= NULL
;
146 int resize_launcher(void *obj
)
148 Launcher
*launcher
= obj
;
150 int count
, icon_size
;
151 int icons_per_column
=1, icons_per_row
=1, marging
=0;
153 if (panel_horizontal
)
154 icon_size
= launcher
->area
.height
;
156 icon_size
= launcher
->area
.width
;
157 icon_size
= icon_size
- (2 * launcher
->area
.bg
->border
.width
) - (2 * launcher
->area
.paddingy
);
158 if (launcher_max_icon_size
> 0 && icon_size
> launcher_max_icon_size
)
159 icon_size
= launcher_max_icon_size
;
161 // Resize icons if necessary
162 for (l
= launcher
->list_icons
; l
; l
= l
->next
) {
163 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
164 if (launcherIcon
->icon_size
!= icon_size
|| !launcherIcon
->icon_original
) {
165 launcherIcon
->icon_size
= icon_size
;
167 // Get the path for an icon file with the new size
168 char *new_icon_path
= icon_path(launcher
, launcherIcon
->icon_name
, launcherIcon
->icon_size
);
169 if (!new_icon_path
) {
171 free_icon(launcherIcon
->icon_original
);
172 launcherIcon
->icon_original
= NULL
;
173 free_icon(launcherIcon
->icon_scaled
);
174 launcherIcon
->icon_scaled
= NULL
;
175 new_icon_path
= icon_path(launcher
, ICON_FALLBACK
, launcherIcon
->icon_size
);
177 launcherIcon
->icon_original
= imlib_load_image(new_icon_path
);
178 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, new_icon_path
);
181 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, icon_size
);
184 if (launcherIcon
->icon_path
&& strcmp(new_icon_path
, launcherIcon
->icon_path
) == 0) {
185 // If it's the same file just rescale
186 free_icon(launcherIcon
->icon_scaled
);
187 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, icon_size
);
189 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, launcherIcon
->icon_path
);
191 // Free the old files
192 free_icon(launcherIcon
->icon_original
);
193 free_icon(launcherIcon
->icon_scaled
);
194 // Load the new file and scale
195 launcherIcon
->icon_original
= imlib_load_image(new_icon_path
);
196 launcherIcon
->icon_scaled
= scale_icon(launcherIcon
->icon_original
, launcherIcon
->icon_size
);
197 free(launcherIcon
->icon_path
);
198 launcherIcon
->icon_path
= new_icon_path
;
199 fprintf(stderr
, "launcher.c %d: Using icon %s\n", __LINE__
, launcherIcon
->icon_path
);
204 count
= g_slist_length(launcher
->list_icons
);
206 if (panel_horizontal
) {
207 if (!count
) launcher
->area
.width
= 0;
209 int height
= launcher
->area
.height
- 2*launcher
->area
.bg
->border
.width
- 2*launcher
->area
.paddingy
;
210 // here icons_per_column always higher than 0
211 icons_per_column
= (height
+launcher
->area
.paddingx
) / (icon_size
+launcher
->area
.paddingx
);
212 marging
= height
- (icons_per_column
-1)*(icon_size
+launcher
->area
.paddingx
) - icon_size
;
213 icons_per_row
= count
/ icons_per_column
+ (count%icons_per_column
!= 0);
214 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
);
218 if (!count
) launcher
->area
.height
= 0;
220 int width
= launcher
->area
.width
- 2*launcher
->area
.bg
->border
.width
- 2*launcher
->area
.paddingy
;
221 // here icons_per_row always higher than 0
222 icons_per_row
= (width
+launcher
->area
.paddingx
) / (icon_size
+launcher
->area
.paddingx
);
223 marging
= width
- (icons_per_row
-1)*(icon_size
+launcher
->area
.paddingx
) - icon_size
;
224 icons_per_column
= count
/ icons_per_row
+ (count%icons_per_row
!= 0);
225 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
);
230 int start
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingy
+ marging
/2;
231 if (panel_horizontal
) {
233 posx
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingxlr
;
237 posy
= launcher
->area
.bg
->border
.width
+ launcher
->area
.paddingxlr
;
240 for (i
=1, l
= launcher
->list_icons
; l
; i
++, l
= l
->next
) {
241 LauncherIcon
*launcherIcon
= (LauncherIcon
*)l
->data
;
243 launcherIcon
->y
= posy
;
244 launcherIcon
->x
= posx
;
245 //printf("launcher %d : %d,%d\n", i, posx, posy);
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
)
412 entry
->name
= entry
->icon
= entry
->exec
= NULL
;
414 if ((fp
= fopen(path
, "rt")) == NULL
) {
415 fprintf(stderr
, "Could not open file %s\n", path
);
419 while (getline(&line
, &line_size
, fp
) >= 0) {
420 int len
= strlen(line
);
423 line
[len
- 1] = '\0';
424 if (parse_dektop_line(line
, &key
, &value
)) {
425 if (strcmp(key
, "Name") == 0) {
426 entry
->name
= strdup(value
);
427 } else if (strcmp(key
, "Exec") == 0) {
428 entry
->exec
= strdup(value
);
429 } else if (strcmp(key
, "Icon") == 0) {
430 entry
->icon
= strdup(value
);
436 // entry->name, entry->icon, entry->exec will never be empty strings (can be NULL though)
438 expand_exec(entry
, path
);
444 void free_desktop_entry(DesktopEntry
*entry
)
451 void test_launcher_read_desktop_file()
453 fprintf(stdout
, "\033[1;33m");
455 launcher_read_desktop_file("/usr/share/applications/firefox.desktop", &entry
);
456 printf("Name:%s Icon:%s Exec:%s\n", entry
.name
, entry
.icon
, entry
.exec
);
457 fprintf(stdout
, "\033[0m");
460 //TODO Use UTF8 when parsing the file
461 IconTheme
*load_theme(char *name
)
463 // Look for name/index.theme in $HOME/.icons, /usr/share/icons, /usr/share/pixmaps (stop at the first found)
464 // Parse index.theme -> list of IconThemeDir with attributes
476 file_name
= g_build_filename(g_get_home_dir(), ".icons", name
, "index.theme", NULL
);
477 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
479 file_name
= g_build_filename("/usr/share/icons", name
, "index.theme", NULL
);
480 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
482 file_name
= g_build_filename("/usr/share/pixmaps", name
, "index.theme", NULL
);
483 if (!g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
494 if ((f
= fopen(file_name
, "rt")) == NULL
) {
495 fprintf(stderr
, "Could not open theme '%s'\n", file_name
);
501 theme
= calloc(1, sizeof(IconTheme
));
502 theme
->name
= strdup(name
);
503 theme
->list_inherits
= NULL
;
504 theme
->list_directories
= NULL
;
506 IconThemeDir
*current_dir
= NULL
;
507 int inside_header
= 1;
508 while (getline(&line
, &line_size
, f
) >= 0) {
511 int line_len
= strlen(line
);
513 if (line
[line_len
- 1] == '\n') {
514 line
[line_len
- 1] = '\0';
523 if (parse_theme_line(line
, &key
, &value
)) {
524 if (strcmp(key
, "Inherits") == 0) {
525 // value is like oxygen,wood,default
527 token
= strtok(value
, ",\n");
528 while (token
!= NULL
)
530 theme
->list_inherits
= g_slist_append(theme
->list_inherits
, strdup(token
));
531 token
= strtok(NULL
, ",\n");
533 } else if (strcmp(key
, "Directories") == 0) {
534 // value is like 48x48/apps,48x48/mimetypes,32x32/apps,scalable/apps,scalable/mimetypes
536 token
= strtok(value
, ",\n");
537 while (token
!= NULL
)
539 IconThemeDir
*dir
= calloc(1, sizeof(IconThemeDir
));
540 dir
->name
= strdup(token
);
541 dir
->max_size
= dir
->min_size
= dir
->size
= -1;
542 dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
544 theme
->list_directories
= g_slist_append(theme
->list_directories
, dir
);
545 token
= strtok(NULL
, ",\n");
549 } else if (current_dir
!= NULL
) {
550 if (parse_theme_line(line
, &key
, &value
)) {
551 if (strcmp(key
, "Size") == 0) {
553 sscanf(value
, "%d", ¤t_dir
->size
);
554 if (current_dir
->max_size
== -1)
555 current_dir
->max_size
= current_dir
->size
;
556 if (current_dir
->min_size
== -1)
557 current_dir
->min_size
= current_dir
->size
;
558 } else if (strcmp(key
, "MaxSize") == 0) {
560 sscanf(value
, "%d", ¤t_dir
->max_size
);
561 } else if (strcmp(key
, "MinSize") == 0) {
563 sscanf(value
, "%d", ¤t_dir
->min_size
);
564 } else if (strcmp(key
, "Threshold") == 0) {
566 sscanf(value
, "%d", ¤t_dir
->threshold
);
567 } else if (strcmp(key
, "Type") == 0) {
568 // value is Fixed, Scalable or Threshold : default to scalable for unknown Type.
569 if (strcmp(value
, "Fixed") == 0) {
570 current_dir
->type
= ICON_DIR_TYPE_FIXED
;
571 } else if (strcmp(value
, "Threshold") == 0) {
572 current_dir
->type
= ICON_DIR_TYPE_THRESHOLD
;
574 current_dir
->type
= ICON_DIR_TYPE_SCALABLE
;
576 } else if (strcmp(key
, "Context") == 0) {
577 // usual values: Actions, Applications, Devices, FileSystems, MimeTypes
578 current_dir
->context
= strdup(value
);
583 if (line
[0] == '[' && line
[line_len
- 1] == ']' && strcmp(line
, "[Icon Theme]") != 0) {
586 line
[line_len
- 1] = '\0';
587 char *dir_name
= line
+ 1;
588 GSList
* dir_item
= theme
->list_directories
;
589 while (dir_item
!= NULL
)
591 IconThemeDir
*dir
= dir_item
->data
;
592 if (strcmp(dir
->name
, dir_name
) == 0) {
596 dir_item
= g_slist_next(dir_item
);
607 void free_icon_theme(IconTheme
*theme
)
611 for (l_inherits
= theme
->list_inherits
; l_inherits
; l_inherits
= l_inherits
->next
) {
612 free(l_inherits
->data
);
615 for (l_dir
= theme
->list_directories
; l_dir
; l_dir
= l_dir
->next
) {
616 IconThemeDir
*dir
= (IconThemeDir
*)l_dir
->data
;
623 void test_launcher_read_theme_file()
625 fprintf(stdout
, "\033[1;33m");
626 IconTheme
*theme
= load_theme("oxygen");
628 printf("Could not load theme\n");
631 printf("Loaded theme: %s\n", theme
->name
);
632 GSList
* item
= theme
->list_inherits
;
635 printf("Inherits:%s\n", (char*)item
->data
);
636 item
= g_slist_next(item
);
638 item
= theme
->list_directories
;
641 IconThemeDir
*dir
= item
->data
;
642 printf("Dir:%s Size=%d MinSize=%d MaxSize=%d Threshold=%d Type=%s Context=%s\n",
643 dir
->name
, dir
->size
, dir
->min_size
, dir
->max_size
, dir
->threshold
,
644 dir
->type
== ICON_DIR_TYPE_FIXED
? "Fixed" :
645 dir
->type
== ICON_DIR_TYPE_SCALABLE
? "Scalable" :
646 dir
->type
== ICON_DIR_TYPE_THRESHOLD
? "Threshold" : "?????",
648 item
= g_slist_next(item
);
650 fprintf(stdout
, "\033[0m");
654 // Populates the list_icons list
655 void launcher_load_icons(Launcher
*launcher
)
657 // Load apps (.desktop style launcher items)
658 GSList
* app
= launcher
->list_apps
;
659 while (app
!= NULL
) {
661 launcher_read_desktop_file(app
->data
, &entry
);
663 LauncherIcon
*launcherIcon
= calloc(1, sizeof(LauncherIcon
));
664 launcherIcon
->is_app_desktop
= 1;
665 launcherIcon
->cmd
= strdup(entry
.exec
);
666 launcherIcon
->icon_name
= entry
.icon
? strdup(entry
.icon
) : strdup(ICON_FALLBACK
);
667 launcherIcon
->icon_size
= 1;
668 free_desktop_entry(&entry
);
669 launcher
->list_icons
= g_slist_append(launcher
->list_icons
, launcherIcon
);
671 app
= g_slist_next(app
);
676 // Populates the list_themes list
677 void launcher_load_themes(Launcher
*launcher
)
679 // load the user theme, all the inherited themes recursively (DFS), and the hicolor theme
680 // avoid inheritance loops
681 if (!icon_theme_name
) {
682 fprintf(stderr
, "Missing launcher theme, default to 'hicolor'.\n");
683 icon_theme_name
= "hicolor";
686 fprintf(stderr
, "Loading %s. Icon theme :", icon_theme_name
);
688 GSList
*queue
= g_slist_append(NULL
, strdup(icon_theme_name
));
689 GSList
*queued
= g_slist_append(NULL
, strdup(icon_theme_name
));
691 int hicolor_loaded
= 0;
692 while (queue
|| !hicolor_loaded
) {
694 GSList
* queued_item
= queued
;
695 while (queued_item
!= NULL
) {
696 if (strcmp(queued_item
->data
, "hicolor") == 0) {
700 queued_item
= g_slist_next(queued_item
);
704 queue
= g_slist_append(queue
, strdup("hicolor"));
705 queued
= g_slist_append(queued
, strdup("hicolor"));
708 char *name
= queue
->data
;
709 queue
= g_slist_remove(queue
, name
);
711 fprintf(stderr
, " '%s',", name
);
712 IconTheme
*theme
= load_theme(name
);
714 launcher
->list_themes
= g_slist_append(launcher
->list_themes
, theme
);
716 GSList
* item
= theme
->list_inherits
;
720 char *parent
= item
->data
;
722 GSList
* queued_item
= queued
;
723 while (queued_item
!= NULL
) {
724 if (strcmp(queued_item
->data
, parent
) == 0) {
728 queued_item
= g_slist_next(queued_item
);
731 queue
= g_slist_insert(queue
, strdup(parent
), pos
);
733 queued
= g_slist_append(queued
, strdup(parent
));
735 item
= g_slist_next(item
);
739 fprintf(stderr
, "\n");
743 for (l
= queue
; l
; l
= l
->next
)
746 for (l
= queued
; l
; l
= l
->next
)
748 g_slist_free(queued
);
751 int directory_matches_size(IconThemeDir
*dir
, int size
)
753 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
754 return dir
->size
== size
;
755 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
756 return dir
->min_size
<= size
&& size
<= dir
->max_size
;
757 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
758 return dir
->size
- dir
->threshold
<= size
&& size
<= dir
->size
+ dir
->threshold
;
762 int directory_size_distance(IconThemeDir
*dir
, int size
)
764 if (dir
->type
== ICON_DIR_TYPE_FIXED
) {
765 return abs(dir
->size
- size
);
766 } else if (dir
->type
== ICON_DIR_TYPE_SCALABLE
) {
767 if (size
< dir
->min_size
) {
768 return dir
->min_size
- size
;
769 } else if (size
> dir
->max_size
) {
770 return size
- dir
->max_size
;
774 } else /*if (dir->type == ICON_DIR_TYPE_THRESHOLD)*/ {
775 if (size
< dir
->size
- dir
->threshold
) {
776 return dir
->min_size
- size
;
777 } else if (size
> dir
->size
+ dir
->threshold
) {
778 return size
- dir
->max_size
;
785 // Returns the full path to an icon file (or NULL) given the icon name
786 char *icon_path(Launcher
*launcher
, const char *icon_name
, int size
)
788 if (icon_name
== NULL
)
791 // If the icon_name is already a path and the file exists, return it
792 if (strstr(icon_name
, "/") == icon_name
) {
793 if (g_file_test(icon_name
, G_FILE_TEST_EXISTS
))
794 return strdup(icon_name
);
799 GSList
*basenames
= NULL
;
800 char *home_icons
= g_build_filename(g_get_home_dir(), ".icons", NULL
);
801 basenames
= g_slist_append(basenames
, home_icons
);
802 basenames
= g_slist_append(basenames
, "/usr/share/icons");
803 basenames
= g_slist_append(basenames
, "/usr/share/pixmaps");
805 GSList
*extensions
= NULL
;
806 extensions
= g_slist_append(extensions
, ".png");
807 extensions
= g_slist_append(extensions
, ".xpm");
808 // if the icon name already contains one of the extensions (e.g. vlc.png instead of vlc) add a special entry
810 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
811 char *extension
= (char*) ext
->data
;
812 if (strlen(icon_name
) > strlen(extension
) &&
813 strcmp(extension
, icon_name
+ strlen(icon_name
) - strlen(extension
)) == 0) {
814 extensions
= g_slist_append(extensions
, "");
820 // Stage 1: exact size match
821 // the theme must have a higher priority than having an exact size match, so we will just use
822 // the code that searches for the best size match (it will find the exact size match if one exists)
824 for (theme = launcher->list_themes; theme; theme = g_slist_next(theme)) {
826 for (dir = ((IconTheme*)theme->data)->list_directories; dir; dir = g_slist_next(dir)) {
827 if (directory_matches_size((IconThemeDir*)dir->data, size)) {
829 for (base = basenames; base; base = g_slist_next(base)) {
831 for (ext = extensions; ext; ext = g_slist_next(ext)) {
832 char *base_name = (char*) base->data;
833 char *theme_name = ((IconTheme*)theme->data)->name;
834 char *dir_name = ((IconThemeDir*)dir->data)->name;
835 char *extension = (char*) ext->data;
836 char *file_name = malloc(strlen(base_name) + strlen(theme_name) +
837 strlen(dir_name) + strlen(icon_name) + strlen(extension) + 100);
838 // filename = directory/$(themename)/subdirectory/iconname.extension
839 sprintf(file_name, "%s/%s/%s/%s%s", base_name, theme_name, dir_name, icon_name, extension);
840 //printf("found exact: %s\n", file_name);
841 //printf("checking %s\n", file_name);
842 if (g_file_test(file_name, G_FILE_TEST_EXISTS)) {
843 g_slist_free(basenames);
844 g_slist_free(extensions);
859 // Stage 2: best size match
860 // Contrary to the freedesktop spec, we are not choosing the closest icon in size, but the next larger icon
861 // otherwise the quality is usually crap (for size 22, if you can choose 16 or 32, you're better with 32)
862 // We do fallback to the closest size if we cannot find a larger or equal icon
864 // These 3 variables are used for keeping the closest size match
865 int minimal_size
= INT_MAX
;
866 char *best_file_name
= NULL
;
867 GSList
*best_file_theme
= NULL
;
869 // These 3 variables are used for keeping the next larger match
870 int next_larger_size
= -1;
871 char *next_larger
= NULL
;
872 GSList
*next_larger_theme
= NULL
;
874 for (theme
= launcher
->list_themes
; theme
; theme
= g_slist_next(theme
)) {
876 for (dir
= ((IconTheme
*)theme
->data
)->list_directories
; dir
; dir
= g_slist_next(dir
)) {
878 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
880 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
881 char *base_name
= (char*) base
->data
;
882 char *theme_name
= ((IconTheme
*)theme
->data
)->name
;
883 char *dir_name
= ((IconThemeDir
*)dir
->data
)->name
;
884 char *extension
= (char*) ext
->data
;
885 char *file_name
= malloc(strlen(base_name
) + strlen(theme_name
) +
886 strlen(dir_name
) + strlen(icon_name
) + strlen(extension
) + 100);
887 // filename = directory/$(themename)/subdirectory/iconname.extension
888 sprintf(file_name
, "%s/%s/%s/%s%s", base_name
, theme_name
, dir_name
, icon_name
, extension
);
889 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
890 //printf("found: %s\n", file_name);
892 if (directory_size_distance((IconThemeDir
*)dir
->data
, size
) < minimal_size
&& (!best_file_theme
? 1 : theme
== best_file_theme
)) {
893 if (best_file_name
) {
894 free(best_file_name
);
895 best_file_name
= NULL
;
897 best_file_name
= strdup(file_name
);
898 minimal_size
= directory_size_distance((IconThemeDir
*)dir
->data
, size
);
899 best_file_theme
= theme
;
900 //printf("best_file_name = %s; minimal_size = %d\n", best_file_name, minimal_size);
903 if (((IconThemeDir
*)dir
->data
)->size
>= size
&&
904 (next_larger_size
== -1 || ((IconThemeDir
*)dir
->data
)->size
< next_larger_size
) &&
905 (!next_larger_theme
? 1 : theme
== next_larger_theme
)) {
910 next_larger
= strdup(file_name
);
911 next_larger_size
= ((IconThemeDir
*)dir
->data
)->size
;
912 next_larger_theme
= theme
;
913 //printf("next_larger = %s; next_larger_size = %d\n", next_larger, next_larger_size);
922 g_slist_free(basenames
);
923 g_slist_free(extensions
);
924 free(best_file_name
);
928 if (best_file_name
) {
929 g_slist_free(basenames
);
930 g_slist_free(extensions
);
932 return best_file_name
;
935 // Stage 3: look in unthemed icons
938 for (base
= basenames
; base
; base
= g_slist_next(base
)) {
940 for (ext
= extensions
; ext
; ext
= g_slist_next(ext
)) {
941 char *base_name
= (char*) base
->data
;
942 char *extension
= (char*) ext
->data
;
943 char *file_name
= malloc(strlen(base_name
) + strlen(icon_name
) +
944 strlen(extension
) + 100);
945 // filename = directory/iconname.extension
946 sprintf(file_name
, "%s/%s%s", base_name
, icon_name
, extension
);
947 //printf("checking %s\n", file_name);
948 if (g_file_test(file_name
, G_FILE_TEST_EXISTS
)) {
949 g_slist_free(basenames
);
950 g_slist_free(extensions
);
961 fprintf(stderr
, "Could not find icon %s\n", icon_name
);
963 g_slist_free(basenames
);
964 g_slist_free(extensions
);
This page took 0.090291 seconds and 5 git commands to generate.