obs-hotkey-name-map.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /******************************************************************************
  2. Copyright (C) 2014 by Ruwen Hahn <palana@stunned.de>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <string.h>
  15. #include <assert.h>
  16. #include <util/bmem.h>
  17. #include <util/c99defs.h>
  18. #include "obs-internal.h"
  19. struct obs_hotkey_name_map;
  20. typedef struct obs_hotkey_name_map_item obs_hotkey_name_map_item_t;
  21. struct obs_hotkey_name_map_item {
  22. char *key;
  23. int val;
  24. UT_hash_handle hh;
  25. };
  26. static void obs_hotkey_name_map_insert(obs_hotkey_name_map_item_t **hmap, const char *key, int v)
  27. {
  28. if (!hmap || !key)
  29. return;
  30. obs_hotkey_name_map_item_t *t;
  31. HASH_FIND_STR(*hmap, key, t);
  32. if (t)
  33. return;
  34. t = bzalloc(sizeof(obs_hotkey_name_map_item_t));
  35. t->key = bstrdup(key);
  36. t->val = v;
  37. HASH_ADD_STR(*hmap, key, t);
  38. }
  39. static bool obs_hotkey_name_map_lookup(obs_hotkey_name_map_item_t *hmap, const char *key, int *v)
  40. {
  41. if (!hmap || !key)
  42. return false;
  43. obs_hotkey_name_map_item_t *elem;
  44. HASH_FIND_STR(hmap, key, elem);
  45. if (elem) {
  46. *v = elem->val;
  47. return true;
  48. }
  49. return false;
  50. }
  51. static const char *obs_key_names[] = {
  52. #define OBS_HOTKEY(x) #x,
  53. #include "obs-hotkeys.h"
  54. #undef OBS_HOTKEY
  55. };
  56. const char *obs_key_to_name(obs_key_t key)
  57. {
  58. if (key >= OBS_KEY_LAST_VALUE) {
  59. blog(LOG_ERROR,
  60. "obs-hotkey.c: queried unknown key "
  61. "with code %d",
  62. (int)key);
  63. return "";
  64. }
  65. return obs_key_names[key];
  66. }
  67. static obs_key_t obs_key_from_name_fallback(const char *name)
  68. {
  69. #define OBS_HOTKEY(x) \
  70. if (strcmp(#x, name) == 0) \
  71. return x;
  72. #include "obs-hotkeys.h"
  73. #undef OBS_HOTKEY
  74. return OBS_KEY_NONE;
  75. }
  76. static void init_name_map(void)
  77. {
  78. #define OBS_HOTKEY(x) obs_hotkey_name_map_insert(&obs->hotkeys.name_map, #x, x);
  79. #include "obs-hotkeys.h"
  80. #undef OBS_HOTKEY
  81. }
  82. obs_key_t obs_key_from_name(const char *name)
  83. {
  84. if (!obs)
  85. return obs_key_from_name_fallback(name);
  86. if (pthread_once(&obs->hotkeys.name_map_init_token, init_name_map))
  87. return obs_key_from_name_fallback(name);
  88. int v = 0;
  89. if (obs_hotkey_name_map_lookup(obs->hotkeys.name_map, name, &v))
  90. return v;
  91. return OBS_KEY_NONE;
  92. }
  93. void obs_hotkey_name_map_free(void)
  94. {
  95. if (!obs || !obs->hotkeys.name_map)
  96. return;
  97. obs_hotkey_name_map_item_t *root = obs->hotkeys.name_map;
  98. obs_hotkey_name_map_item_t *n, *tmp;
  99. HASH_ITER (hh, root, n, tmp) {
  100. HASH_DEL(root, n);
  101. bfree(n->key);
  102. bfree(n);
  103. }
  104. }