1
0

obs-hotkey-name-map.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /******************************************************************************
  2. Copyright (C) 2014 by Ruwen Hahn <[email protected]>
  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,
  27. const char *key, int v)
  28. {
  29. if (!hmap || !key)
  30. return;
  31. obs_hotkey_name_map_item_t *t;
  32. HASH_FIND_STR(*hmap, key, t);
  33. if (t)
  34. return;
  35. t = bzalloc(sizeof(obs_hotkey_name_map_item_t));
  36. t->key = bstrdup(key);
  37. t->val = v;
  38. HASH_ADD_STR(*hmap, key, t);
  39. }
  40. static bool obs_hotkey_name_map_lookup(obs_hotkey_name_map_item_t *hmap,
  41. const char *key, int *v)
  42. {
  43. if (!hmap || !key)
  44. return false;
  45. obs_hotkey_name_map_item_t *elem;
  46. HASH_FIND_STR(hmap, key, elem);
  47. if (elem) {
  48. *v = elem->val;
  49. return true;
  50. }
  51. return false;
  52. }
  53. static const char *obs_key_names[] = {
  54. #define OBS_HOTKEY(x) #x,
  55. #include "obs-hotkeys.h"
  56. #undef OBS_HOTKEY
  57. };
  58. const char *obs_key_to_name(obs_key_t key)
  59. {
  60. if (key >= OBS_KEY_LAST_VALUE) {
  61. blog(LOG_ERROR,
  62. "obs-hotkey.c: queried unknown key "
  63. "with code %d",
  64. (int)key);
  65. return "";
  66. }
  67. return obs_key_names[key];
  68. }
  69. static obs_key_t obs_key_from_name_fallback(const char *name)
  70. {
  71. #define OBS_HOTKEY(x) \
  72. if (strcmp(#x, name) == 0) \
  73. return x;
  74. #include "obs-hotkeys.h"
  75. #undef OBS_HOTKEY
  76. return OBS_KEY_NONE;
  77. }
  78. static void init_name_map(void)
  79. {
  80. #define OBS_HOTKEY(x) obs_hotkey_name_map_insert(&obs->hotkeys.name_map, #x, x);
  81. #include "obs-hotkeys.h"
  82. #undef OBS_HOTKEY
  83. }
  84. obs_key_t obs_key_from_name(const char *name)
  85. {
  86. if (!obs)
  87. return obs_key_from_name_fallback(name);
  88. if (pthread_once(&obs->hotkeys.name_map_init_token, init_name_map))
  89. return obs_key_from_name_fallback(name);
  90. int v = 0;
  91. if (obs_hotkey_name_map_lookup(obs->hotkeys.name_map, name, &v))
  92. return v;
  93. return OBS_KEY_NONE;
  94. }
  95. void obs_hotkey_name_map_free(void)
  96. {
  97. if (!obs || !obs->hotkeys.name_map)
  98. return;
  99. obs_hotkey_name_map_item_t *root = obs->hotkeys.name_map;
  100. obs_hotkey_name_map_item_t *n, *tmp;
  101. HASH_ITER (hh, root, n, tmp) {
  102. HASH_DEL(root, n);
  103. bfree(n->key);
  104. bfree(n);
  105. }
  106. }