icon_manager.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* icon_manager.c - gfxmenu icon manager. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/types.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/err.h>
  23. #include <grub/gui_string_util.h>
  24. #include <grub/bitmap.h>
  25. #include <grub/bitmap_scale.h>
  26. #include <grub/menu.h>
  27. #include <grub/icon_manager.h>
  28. #include <grub/env.h>
  29. /* Currently hard coded to '.png' extension. */
  30. static const char icon_extension[] = ".png";
  31. typedef struct icon_entry
  32. {
  33. char *class_name;
  34. struct grub_video_bitmap *bitmap;
  35. struct icon_entry *next;
  36. } *icon_entry_t;
  37. struct grub_gfxmenu_icon_manager
  38. {
  39. char *theme_path;
  40. int icon_width;
  41. int icon_height;
  42. /* Icon cache: linked list w/ dummy head node. */
  43. struct icon_entry cache;
  44. };
  45. /* Create a new icon manager and return a point to it. */
  46. grub_gfxmenu_icon_manager_t
  47. grub_gfxmenu_icon_manager_new (void)
  48. {
  49. grub_gfxmenu_icon_manager_t mgr;
  50. mgr = grub_malloc (sizeof (*mgr));
  51. if (! mgr)
  52. return 0;
  53. mgr->theme_path = 0;
  54. mgr->icon_width = 0;
  55. mgr->icon_height = 0;
  56. /* Initialize the dummy head node. */
  57. mgr->cache.class_name = 0;
  58. mgr->cache.bitmap = 0;
  59. mgr->cache.next = 0;
  60. return mgr;
  61. }
  62. /* Destroy the icon manager MGR, freeing all resources used by it.
  63. Note: Any bitmaps returned by grub_gfxmenu_icon_manager_get_icon()
  64. are destroyed and must not be used by the caller after this function
  65. is called. */
  66. void
  67. grub_gfxmenu_icon_manager_destroy (grub_gfxmenu_icon_manager_t mgr)
  68. {
  69. grub_gfxmenu_icon_manager_clear_cache (mgr);
  70. grub_free (mgr->theme_path);
  71. grub_free (mgr);
  72. }
  73. /* Clear the icon cache. */
  74. void
  75. grub_gfxmenu_icon_manager_clear_cache (grub_gfxmenu_icon_manager_t mgr)
  76. {
  77. icon_entry_t cur;
  78. icon_entry_t next;
  79. for (cur = mgr->cache.next; cur; cur = next)
  80. {
  81. next = cur->next;
  82. grub_free (cur->class_name);
  83. grub_video_bitmap_destroy (cur->bitmap);
  84. grub_free (cur);
  85. }
  86. mgr->cache.next = 0;
  87. }
  88. /* Set the theme path. If the theme path is changed, the icon cache
  89. is cleared. */
  90. void
  91. grub_gfxmenu_icon_manager_set_theme_path (grub_gfxmenu_icon_manager_t mgr,
  92. const char *path)
  93. {
  94. /* Clear the cache if the theme path has changed. */
  95. if (((mgr->theme_path == 0) != (path == 0))
  96. || (grub_strcmp (mgr->theme_path, path) != 0))
  97. grub_gfxmenu_icon_manager_clear_cache (mgr);
  98. grub_free (mgr->theme_path);
  99. mgr->theme_path = path ? grub_strdup (path) : 0;
  100. }
  101. /* Set the icon size. When icons are requested from the icon manager,
  102. they are scaled to this size before being returned. If the size is
  103. changed, the icon cache is cleared. */
  104. void
  105. grub_gfxmenu_icon_manager_set_icon_size (grub_gfxmenu_icon_manager_t mgr,
  106. int width, int height)
  107. {
  108. /* If the width or height is changed, we must clear the cache, since the
  109. scaled bitmaps are stored in the cache. */
  110. if (width != mgr->icon_width || height != mgr->icon_height)
  111. grub_gfxmenu_icon_manager_clear_cache (mgr);
  112. mgr->icon_width = width;
  113. mgr->icon_height = height;
  114. }
  115. /* Try to load an icon for the specified CLASS_NAME in the directory DIR.
  116. Returns 0 if the icon could not be loaded, or returns a pointer to a new
  117. bitmap if it was successful. */
  118. static struct grub_video_bitmap *
  119. try_loading_icon (grub_gfxmenu_icon_manager_t mgr,
  120. const char *dir, const char *class_name)
  121. {
  122. char *path;
  123. int l;
  124. path = grub_malloc (grub_strlen (dir) + grub_strlen (class_name)
  125. + grub_strlen (icon_extension) + 3);
  126. if (! path)
  127. return 0;
  128. grub_strcpy (path, dir);
  129. l = grub_strlen (path);
  130. if (path[l-1] != '/')
  131. {
  132. path[l] = '/';
  133. path[l+1] = 0;
  134. }
  135. grub_strcat (path, class_name);
  136. grub_strcat (path, icon_extension);
  137. struct grub_video_bitmap *raw_bitmap;
  138. grub_video_bitmap_load (&raw_bitmap, path);
  139. grub_free (path);
  140. grub_errno = GRUB_ERR_NONE; /* Critical to clear the error!! */
  141. if (! raw_bitmap)
  142. return 0;
  143. struct grub_video_bitmap *scaled_bitmap;
  144. grub_video_bitmap_create_scaled (&scaled_bitmap,
  145. mgr->icon_width, mgr->icon_height,
  146. raw_bitmap,
  147. GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST, 0);
  148. grub_video_bitmap_destroy (raw_bitmap);
  149. if (! scaled_bitmap)
  150. {
  151. grub_error_push ();
  152. grub_error (grub_errno, "failed to scale icon");
  153. return 0;
  154. }
  155. return scaled_bitmap;
  156. }
  157. /* Get the icon for the specified class CLASS_NAME. If an icon for
  158. CLASS_NAME already exists in the cache, then a reference to the cached
  159. bitmap is returned. If it is not cached, then it is loaded and cached.
  160. If no icon could be could for CLASS_NAME, then 0 is returned. */
  161. static struct grub_video_bitmap *
  162. get_icon_by_class (grub_gfxmenu_icon_manager_t mgr, const char *class_name)
  163. {
  164. /* First check the icon cache. */
  165. icon_entry_t entry;
  166. for (entry = mgr->cache.next; entry; entry = entry->next)
  167. {
  168. if (grub_strcmp (entry->class_name, class_name) == 0)
  169. return entry->bitmap;
  170. }
  171. if (! mgr->theme_path)
  172. return 0;
  173. /* Otherwise, we search for an icon to load. */
  174. char *theme_dir = grub_get_dirname (mgr->theme_path);
  175. char *icons_dir;
  176. struct grub_video_bitmap *icon;
  177. icon = 0;
  178. /* First try the theme's own icons, from "grub/themes/NAME/icons/" */
  179. icons_dir = grub_resolve_relative_path (theme_dir, "icons/");
  180. if (icons_dir)
  181. {
  182. icon = try_loading_icon (mgr, icons_dir, class_name);
  183. grub_free (icons_dir);
  184. }
  185. grub_free (theme_dir);
  186. if (! icon)
  187. {
  188. const char *icondir;
  189. icondir = grub_env_get ("icondir");
  190. if (icondir)
  191. icon = try_loading_icon (mgr, icondir, class_name);
  192. }
  193. /* No icon was found. */
  194. /* This should probably be noted in the cache, so that a search is not
  195. performed each time an icon for CLASS_NAME is requested. */
  196. if (! icon)
  197. return 0;
  198. /* Insert a new cache entry for this icon. */
  199. entry = grub_malloc (sizeof (*entry));
  200. if (! entry)
  201. {
  202. grub_video_bitmap_destroy (icon);
  203. return 0;
  204. }
  205. entry->class_name = grub_strdup (class_name);
  206. entry->bitmap = icon;
  207. entry->next = mgr->cache.next;
  208. mgr->cache.next = entry; /* Link it into the cache. */
  209. return entry->bitmap;
  210. }
  211. /* Get the best available icon for ENTRY. Beginning with the first class
  212. listed in the menu entry and proceeding forward, an icon for each class
  213. is searched for. The first icon found is returned. The returned icon
  214. is scaled to the size specified by
  215. grub_gfxmenu_icon_manager_set_icon_size().
  216. Note: Bitmaps returned by this function are destroyed when the
  217. icon manager is destroyed.
  218. */
  219. struct grub_video_bitmap *
  220. grub_gfxmenu_icon_manager_get_icon (grub_gfxmenu_icon_manager_t mgr,
  221. grub_menu_entry_t entry)
  222. {
  223. struct grub_menu_entry_class *c;
  224. struct grub_video_bitmap *icon;
  225. /* Try each class in succession. */
  226. icon = 0;
  227. for (c = entry->classes->next; c && ! icon; c = c->next)
  228. icon = get_icon_by_class (mgr, c->name);
  229. return icon;
  230. }