darwin-modifiers.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <CoreGraphics/CoreGraphics.h>
  2. #include <dlfcn.h>
  3. #include <stdbool.h>
  4. #include <stddef.h>
  5. #include <string.h>
  6. #define NAPI_AUTO_LENGTH ((size_t)-1)
  7. typedef void* napi_env;
  8. typedef void* napi_value;
  9. typedef void* napi_callback_info;
  10. typedef napi_value (*napi_callback)(napi_env, napi_callback_info);
  11. typedef int (*napi_create_function_fn)(napi_env, const char*, size_t, napi_callback, void*, napi_value*);
  12. typedef int (*napi_set_named_property_fn)(napi_env, napi_value, const char*, napi_value);
  13. typedef int (*napi_get_boolean_fn)(napi_env, bool, napi_value*);
  14. typedef int (*napi_get_cb_info_fn)(napi_env, napi_callback_info, size_t*, napi_value*, napi_value*, void**);
  15. typedef int (*napi_get_value_string_utf8_fn)(napi_env, napi_value, char*, size_t, size_t*);
  16. static void* node_symbol(const char* name) {
  17. return dlsym(RTLD_DEFAULT, name);
  18. }
  19. static CGEventFlags modifier_mask_for_name(const char* name) {
  20. if (strcmp(name, "shift") == 0) return kCGEventFlagMaskShift;
  21. if (strcmp(name, "command") == 0) return kCGEventFlagMaskCommand;
  22. if (strcmp(name, "control") == 0) return kCGEventFlagMaskControl;
  23. if (strcmp(name, "option") == 0) return kCGEventFlagMaskAlternate;
  24. return 0;
  25. }
  26. static napi_value is_modifier_pressed(napi_env env, napi_callback_info info) {
  27. napi_get_cb_info_fn napi_get_cb_info = (napi_get_cb_info_fn)node_symbol("napi_get_cb_info");
  28. napi_get_value_string_utf8_fn napi_get_value_string_utf8 = (napi_get_value_string_utf8_fn)node_symbol("napi_get_value_string_utf8");
  29. napi_get_boolean_fn napi_get_boolean = (napi_get_boolean_fn)node_symbol("napi_get_boolean");
  30. bool pressed = false;
  31. if (napi_get_cb_info && napi_get_value_string_utf8) {
  32. size_t argc = 1;
  33. napi_value args[1] = {0};
  34. if (napi_get_cb_info(env, info, &argc, args, 0, 0) == 0 && argc >= 1 && args[0]) {
  35. char name[16] = {0};
  36. size_t copied = 0;
  37. if (napi_get_value_string_utf8(env, args[0], name, sizeof(name), &copied) == 0) {
  38. CGEventFlags mask = modifier_mask_for_name(name);
  39. if (mask != 0) {
  40. CGEventFlags flags = CGEventSourceFlagsState(kCGEventSourceStateCombinedSessionState);
  41. pressed = (flags & mask) != 0;
  42. }
  43. }
  44. }
  45. }
  46. napi_value result = 0;
  47. if (napi_get_boolean) napi_get_boolean(env, pressed, &result);
  48. return result;
  49. }
  50. __attribute__((visibility("default"))) napi_value napi_register_module_v1(napi_env env, napi_value exports) {
  51. napi_create_function_fn napi_create_function = (napi_create_function_fn)node_symbol("napi_create_function");
  52. napi_set_named_property_fn napi_set_named_property = (napi_set_named_property_fn)node_symbol("napi_set_named_property");
  53. napi_value fn = 0;
  54. if (napi_create_function &&
  55. napi_set_named_property &&
  56. napi_create_function(env, "isModifierPressed", NAPI_AUTO_LENGTH, is_modifier_pressed, 0, &fn) == 0) {
  57. napi_set_named_property(env, exports, "isModifierPressed", fn);
  58. }
  59. return exports;
  60. }