icon_cache.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. static char * prefixes[] = {
  36. "png",
  37. "bmp",
  38. NULL
  39. };
  40. __attribute__((constructor))
  41. static void _init_caches(void) {
  42. icon_cache_16 = hashmap_create(10);
  43. { /* Generic fallback icon */
  44. sprite_t * app_icon = malloc(sizeof(sprite_t));
  45. load_sprite(app_icon, "/usr/share/icons/16/applications-generic.png");
  46. hashmap_set(icon_cache_16, "generic", app_icon);
  47. }
  48. icon_cache_48 = hashmap_create(10);
  49. { /* Generic fallback icon */
  50. sprite_t * app_icon = malloc(sizeof(sprite_t));
  51. load_sprite(app_icon, "/usr/share/icons/48/applications-generic.png");
  52. hashmap_set(icon_cache_48, "generic", app_icon);
  53. }
  54. }
  55. static sprite_t * icon_get_int(const char * name, hashmap_t * icon_cache, char ** icon_directories) {
  56. if (!strcmp(name,"")) {
  57. /* If a window doesn't have an icon set, return the generic icon */
  58. return hashmap_get(icon_cache, "generic");
  59. }
  60. /* Check the icon cache */
  61. sprite_t * icon = hashmap_get(icon_cache, (void*)name);
  62. if (!icon) {
  63. /* We don't have an icon cached for this identifier, try search */
  64. int i = 0;
  65. char path[100];
  66. while (icon_directories[i]) {
  67. /* Check each path... */
  68. char ** prefix = prefixes;
  69. while (*prefix) {
  70. sprintf(path, "%s/%s.%s", icon_directories[i], name, *prefix);
  71. if (access(path, R_OK) == 0) {
  72. /* And if we find one, cache it */
  73. icon = malloc(sizeof(sprite_t));
  74. load_sprite(icon, path);
  75. hashmap_set(icon_cache, (void*)name, icon);
  76. return icon;
  77. }
  78. prefix++;
  79. }
  80. i++;
  81. }
  82. /* If we've exhausted our search paths, just return the generic icon */
  83. icon = hashmap_get(icon_cache, "generic");
  84. hashmap_set(icon_cache, (void*)name, icon);
  85. }
  86. /* We have an icon, return it */
  87. return icon;
  88. }
  89. sprite_t * icon_get_16(const char * name) {
  90. return icon_get_int(name, icon_cache_16, icon_directories_16);
  91. }
  92. sprite_t * icon_get_48(const char * name) {
  93. return icon_get_int(name, icon_cache_48, icon_directories_48);
  94. }