icon_cache.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * icon_cache - caches icons
  7. *
  8. * Used be a few different applications.
  9. * Probably needs scaling?
  10. */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include <toaru/graphics.h>
  16. #include <toaru/hashmap.h>
  17. static hashmap_t * icon_cache_16;
  18. static hashmap_t * icon_cache_48;
  19. static char * icon_directories_16[] = {
  20. "/usr/share/icons/16",
  21. "/usr/share/icons/24",
  22. "/usr/share/icons/48",
  23. "/usr/share/icons",
  24. "/usr/share/icons/external",
  25. NULL
  26. };
  27. static char * icon_directories_48[] = {
  28. "/usr/share/icons/48",
  29. "/usr/share/icons/24",
  30. "/usr/share/icons/16",
  31. "/usr/share/icons",
  32. "/usr/share/icons/external",
  33. NULL
  34. };
  35. __attribute__((constructor))
  36. static void _init_caches(void) {
  37. icon_cache_16 = hashmap_create(10);
  38. { /* Generic fallback icon */
  39. sprite_t * app_icon = malloc(sizeof(sprite_t));
  40. load_sprite(app_icon, "/usr/share/icons/16/applications-generic.bmp");
  41. app_icon->alpha = ALPHA_EMBEDDED;
  42. hashmap_set(icon_cache_16, "generic", app_icon);
  43. }
  44. icon_cache_48 = hashmap_create(10);
  45. { /* Generic fallback icon */
  46. sprite_t * app_icon = malloc(sizeof(sprite_t));
  47. load_sprite(app_icon, "/usr/share/icons/48/applications-generic.bmp");
  48. app_icon->alpha = ALPHA_EMBEDDED;
  49. hashmap_set(icon_cache_48, "generic", app_icon);
  50. }
  51. }
  52. static sprite_t * icon_get_int(const char * name, hashmap_t * icon_cache, char ** icon_directories) {
  53. if (!strcmp(name,"")) {
  54. /* If a window doesn't have an icon set, return the generic icon */
  55. return hashmap_get(icon_cache, "generic");
  56. }
  57. /* Check the icon cache */
  58. sprite_t * icon = hashmap_get(icon_cache, (void*)name);
  59. if (!icon) {
  60. /* We don't have an icon cached for this identifier, try search */
  61. int i = 0;
  62. char path[100];
  63. while (icon_directories[i]) {
  64. /* Check each path... */
  65. sprintf(path, "%s/%s.bmp", icon_directories[i], name);
  66. if (access(path, R_OK) == 0) {
  67. /* And if we find one, cache it */
  68. icon = malloc(sizeof(sprite_t));
  69. load_sprite(icon, path);
  70. icon->alpha = ALPHA_EMBEDDED;
  71. hashmap_set(icon_cache, (void*)name, icon);
  72. return icon;
  73. }
  74. i++;
  75. }
  76. /* If we've exhausted our search paths, just return the generic icon */
  77. icon = hashmap_get(icon_cache, "generic");
  78. hashmap_set(icon_cache, (void*)name, icon);
  79. }
  80. /* We have an icon, return it */
  81. return icon;
  82. }
  83. sprite_t * icon_get_16(const char * name) {
  84. return icon_get_int(name, icon_cache_16, icon_directories_16);
  85. }
  86. sprite_t * icon_get_48(const char * name) {
  87. return icon_get_int(name, icon_cache_48, icon_directories_48);
  88. }