@kmamal/sdl 1.0.0-5 → 1.0.0-9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/CHANGELOG.md +12 -6
  2. package/README.md +2 -2
  3. package/binding.gyp +21 -4
  4. package/package.json +5 -12
  5. package/scripts/build.mjs +3 -1
  6. package/scripts/install.mjs +2 -0
  7. package/scripts/util/common.js +3 -2
  8. package/src/javascript/audio/audio-playback-stream.js +9 -10
  9. package/src/javascript/audio/audio-recording-stream.js +9 -8
  10. package/src/javascript/audio/audio-stream.js +101 -44
  11. package/src/javascript/audio/conversion.js +41 -0
  12. package/src/javascript/audio/devices.js +11 -0
  13. package/src/javascript/audio/events.js +65 -0
  14. package/src/javascript/audio/format-helpers.js +8 -4
  15. package/src/javascript/audio/index.js +25 -20
  16. package/src/javascript/audio/prevent-exit.js +13 -10
  17. package/src/javascript/audio/validate.js +9 -0
  18. package/src/javascript/cleanup.js +9 -6
  19. package/src/javascript/clipboard/index.js +8 -3
  20. package/src/javascript/device-instance.js +61 -0
  21. package/src/javascript/events/events-via-poll.js +32 -28
  22. package/src/javascript/events/index.js +85 -479
  23. package/src/javascript/events/try.js +25 -0
  24. package/src/javascript/gamepad/{device.js → devices.js} +4 -5
  25. package/src/javascript/gamepad/events.js +66 -0
  26. package/src/javascript/gamepad/gamepad-instance.js +12 -160
  27. package/src/javascript/gamepad/index.js +15 -16
  28. package/src/javascript/globals.js +10 -1
  29. package/src/javascript/index.js +2 -5
  30. package/src/javascript/joystick/devices.js +43 -0
  31. package/src/javascript/joystick/events.js +129 -0
  32. package/src/javascript/joystick/index.js +9 -18
  33. package/src/javascript/joystick/joystick-device-instance.js +176 -0
  34. package/src/javascript/joystick/joystick-instance.js +12 -158
  35. package/src/javascript/keyboard/index.js +20 -10
  36. package/src/javascript/keyboard/key-mapping.js +46 -27
  37. package/src/javascript/mouse/index.js +36 -29
  38. package/src/javascript/sensor/events.js +21 -0
  39. package/src/javascript/sensor/index.js +13 -2
  40. package/src/javascript/sensor/sensor-instance.js +7 -47
  41. package/src/javascript/subsystems.js +56 -0
  42. package/src/javascript/touch/index.js +9 -4
  43. package/src/javascript/validate.js +119 -0
  44. package/src/javascript/video/conversion.js +63 -0
  45. package/src/javascript/video/events.js +179 -0
  46. package/src/javascript/video/format-helpers.js +10 -8
  47. package/src/javascript/video/index.js +28 -14
  48. package/src/javascript/video/validate.js +45 -0
  49. package/src/javascript/video/window.js +196 -167
  50. package/src/native/audio.cpp +457 -153
  51. package/src/native/audio.h +25 -19
  52. package/src/native/clipboard.cpp +8 -37
  53. package/src/native/clipboard.h +2 -2
  54. package/src/native/enums.cpp +16 -1
  55. package/src/native/enums.h +1 -1
  56. package/src/native/events.cpp +351 -357
  57. package/src/native/events.h +70 -68
  58. package/src/native/gamepad.cpp +147 -103
  59. package/src/native/gamepad.h +10 -11
  60. package/src/native/global.cpp +183 -340
  61. package/src/native/global.h +51 -5
  62. package/src/native/joystick.cpp +204 -275
  63. package/src/native/joystick.h +21 -15
  64. package/src/native/keyboard.cpp +63 -25
  65. package/src/native/keyboard.h +5 -4
  66. package/src/native/module.cpp +23 -3
  67. package/src/native/mouse.cpp +28 -59
  68. package/src/native/mouse.h +9 -9
  69. package/src/native/packed.cpp +99 -0
  70. package/src/native/packed.h +86 -0
  71. package/src/native/power.cpp +13 -1
  72. package/src/native/power.h +3 -3
  73. package/src/native/sensor.cpp +78 -69
  74. package/src/native/sensor.h +7 -7
  75. package/src/native/testing.cpp +148 -0
  76. package/src/native/testing.h +17 -0
  77. package/src/native/threads.cpp +301 -0
  78. package/src/native/threads.h +36 -0
  79. package/src/native/touch.cpp +38 -44
  80. package/src/native/touch.h +3 -2
  81. package/src/native/video.cpp +211 -98
  82. package/src/native/video.h +7 -4
  83. package/src/native/window.cpp +201 -219
  84. package/src/native/window.h +24 -20
  85. package/src/types/index.d.ts +157 -70
  86. package/src/javascript/audio/device.js +0 -37
  87. package/src/javascript/joystick/device.js +0 -13
  88. package/src/javascript/joystick/shared.js +0 -90
  89. package/src/javascript/worker.js +0 -88
  90. package/src/types/worker.d.ts +0 -71
@@ -2,31 +2,37 @@
2
2
  #define _JOYSTICK_H_
3
3
 
4
4
  #include <napi.h>
5
+ #include "packed.h"
5
6
  #include <SDL3/SDL.h>
6
7
  #include <map>
7
- #include <string>
8
8
 
9
9
  namespace joystick {
10
10
 
11
- extern std::map<SDL_JoystickType, std::string> types;
12
- extern std::map<Uint8, std::string> hat_positions;
11
+ struct JoysticksLock {
12
+ JoysticksLock () { SDL_LockJoysticks(); }
13
+ ~JoysticksLock () { SDL_UnlockJoysticks(); }
14
+ };
15
+
16
+ extern const std::map<SDL_JoystickType, packed::Str> types;
17
+ extern const std::map<Uint8, packed::Str> hat_positions;
13
18
  extern SDL_GUID zero_guid;
14
19
 
15
20
  double mapAxis (SDL_Joystick *joystick, int axis);
16
21
  double mapAxisValue (int value);
17
- Napi::Value _getDevice (Napi::Env &env, SDL_JoystickID id);
22
+ packed::Value _getDevice (SDL_JoystickID id);
18
23
  Napi::Array _getDevices (Napi::Env &env);
19
- Napi::Value getPowerInfo(Napi::Env &env, SDL_Joystick *joystick);
20
- Napi::Value mapPowerInfo(Napi::Env &env, SDL_PowerState state, int percent);
21
-
22
- Napi::Value getDevices(const Napi::CallbackInfo &info);
23
- Napi::Value open(const Napi::CallbackInfo &info);
24
- Napi::Value getCapabilities(const Napi::CallbackInfo &info);
25
- Napi::Value setLed(const Napi::CallbackInfo &info);
26
- Napi::Value setPlayer(const Napi::CallbackInfo &info);
27
- Napi::Value rumble(const Napi::CallbackInfo &info);
28
- Napi::Value rumbleTriggers(const Napi::CallbackInfo &info);
29
- Napi::Value close(const Napi::CallbackInfo &info);
24
+ packed::Value getPowerInfo (SDL_Joystick *joystick);
25
+ const char *getState (SDL_Joystick *joystick, packed::Value &dst);
26
+ packed::Value mapPowerInfo (SDL_PowerState state, int percent);
27
+
28
+ Napi::Value getDevices (const Napi::CallbackInfo &info);
29
+ Napi::Value open (const Napi::CallbackInfo &info);
30
+ Napi::Value getCapabilities (const Napi::CallbackInfo &info);
31
+ Napi::Value setLed (const Napi::CallbackInfo &info);
32
+ Napi::Value setPlayer (const Napi::CallbackInfo &info);
33
+ Napi::Value rumble (const Napi::CallbackInfo &info);
34
+ Napi::Value rumbleTriggers (const Napi::CallbackInfo &info);
35
+ Napi::Value close (const Napi::CallbackInfo &info);
30
36
 
31
37
  }; // namespace joystick
32
38
 
@@ -1,47 +1,77 @@
1
1
  #include "keyboard.h"
2
2
  #include <SDL3/SDL.h>
3
3
  #include <string>
4
- #include <sstream>
5
4
 
6
5
  int keyboard::num_keys;
7
6
  const bool *keyboard::keys;
8
7
 
9
8
 
10
- Napi::Value
11
- keyboard::packKey(Napi::Env &env, SDL_Keycode keycode)
9
+ packed::Value
10
+ keyboard::packKey (SDL_Keycode keycode)
12
11
  {
13
- bool is_character = !(keycode & (SDLK_SCANCODE_MASK | SDLK_EXTENDED_MASK))
14
- && keycode >= 0x20
15
- && keycode != 0x7F;
16
-
17
- if (is_character) {
18
- char utf8[5];
19
- char *end = SDL_UCS4ToUTF8(keycode, utf8);
20
- *end = '\0';
21
- return Napi::String::New(env, utf8);
12
+ bool is_masked = (keycode & (SDLK_SCANCODE_MASK | SDLK_EXTENDED_MASK)) != 0;
13
+
14
+ if (!is_masked) {
15
+ switch (keycode) {
16
+ case SDLK_RETURN:
17
+ case SDLK_ESCAPE:
18
+ case SDLK_BACKSPACE:
19
+ case SDLK_TAB:
20
+ case SDLK_DELETE:
21
+ break;
22
+ default: {
23
+ bool is_control = keycode < 0x20 || (keycode >= 0x80 && keycode <= 0x9F);
24
+ if (is_control) { return packed::Value(); }
25
+ char utf8[5];
26
+ char *end = SDL_UCS4ToUTF8(keycode, utf8);
27
+ *end = '\0';
28
+ return packed::Value(utf8);
29
+ }
30
+ }
22
31
  }
23
32
 
24
33
  const char *name = SDL_GetKeyName(keycode);
25
34
  if (name[0] == '\0') {
26
35
  SDL_ClearError();
27
- return env.Null();
36
+ return packed::Value();
28
37
  }
29
- return Napi::String::New(env, name);
38
+
39
+ return packed::Value(name);
30
40
  }
31
41
 
32
42
  Napi::Value
33
- keyboard::getKey(const Napi::CallbackInfo &info)
43
+ keyboard::getKey (const Napi::CallbackInfo &info)
34
44
  {
35
45
  Napi::Env env = info.Env();
36
46
 
37
47
  int scancode = info[0].As<Napi::Number>().Int32Value();
38
48
 
39
49
  SDL_Keycode keycode = SDL_GetKeyFromScancode((SDL_Scancode) scancode, SDL_KMOD_NONE, true);
40
- return packKey(env, keycode);
50
+ return packKey(keycode).toNapi(env);
51
+ }
52
+
53
+ static bool
54
+ isKeypad (int scancode)
55
+ {
56
+ return (scancode >= SDL_SCANCODE_KP_DIVIDE && scancode <= SDL_SCANCODE_KP_PERIOD)
57
+ || scancode == SDL_SCANCODE_KP_EQUALS
58
+ || scancode == SDL_SCANCODE_KP_COMMA
59
+ || scancode == SDL_SCANCODE_KP_EQUALSAS400
60
+ || (scancode >= SDL_SCANCODE_KP_00 && scancode <= SDL_SCANCODE_KP_HEXADECIMAL);
61
+ }
62
+
63
+ static int
64
+ findScancode (SDL_Keycode keycode)
65
+ {
66
+ for (int i = 1; i < SDL_SCANCODE_COUNT; i++) {
67
+ if (isKeypad(i)) { continue; }
68
+ if (SDL_GetKeyFromScancode((SDL_Scancode) i, SDL_KMOD_NONE, true) == keycode) { return i; }
69
+ }
70
+ return 0;
41
71
  }
42
72
 
43
73
  Napi::Value
44
- keyboard::getScancode(const Napi::CallbackInfo &info)
74
+ keyboard::getScancode (const Napi::CallbackInfo &info)
45
75
  {
46
76
  Napi::Env env = info.Env();
47
77
 
@@ -53,25 +83,33 @@ keyboard::getScancode(const Napi::CallbackInfo &info)
53
83
  return env.Null();
54
84
  }
55
85
 
56
- int scancode = SDL_GetScancodeFromKey(keycode, nullptr);
86
+ const char *cursor = keyname.c_str();
87
+ size_t remaining = keyname.size();
88
+ Uint32 codepoint = SDL_StepUTF8(&cursor, &remaining);
89
+ SDL_Keycode exact = (codepoint != 0 && remaining == 0) ? (SDL_Keycode) codepoint : keycode;
90
+
91
+ int scancode = findScancode(exact);
92
+ if (scancode == 0 && exact != keycode) { scancode = findScancode(keycode); }
57
93
  if (scancode == 0) {
58
- SDL_ClearError();
59
- return env.Null();
94
+ scancode = SDL_GetScancodeFromKey(keycode, nullptr);
95
+ if (scancode == 0) {
96
+ SDL_ClearError();
97
+ return env.Null();
98
+ }
60
99
  }
61
100
 
62
101
  return Napi::Number::New(env, scancode);
63
102
  }
64
103
 
65
104
  Napi::Value
66
- keyboard::getState(const Napi::CallbackInfo &info)
105
+ keyboard::getState (const Napi::CallbackInfo &info)
67
106
  {
68
107
  Napi::Env env = info.Env();
69
108
 
70
- Napi::Array result = Napi::Array::New(env, keyboard::num_keys);
109
+ static_assert(sizeof(bool) == 1, "bool must be one byte");
71
110
 
72
- for (int i = 0; i < keyboard::num_keys; i++) {
73
- result.Set(i, keyboard::keys[i]);
74
- }
111
+ Napi::Uint8Array result = Napi::Uint8Array::New(env, keyboard::num_keys);
112
+ SDL_memcpy(result.Data(), keyboard::keys, keyboard::num_keys);
75
113
 
76
114
  return result;
77
115
  }
@@ -2,6 +2,7 @@
2
2
  #define _KEYBOARD_H_
3
3
 
4
4
  #include <napi.h>
5
+ #include "packed.h"
5
6
  #include <SDL3/SDL.h>
6
7
 
7
8
  namespace keyboard {
@@ -9,11 +10,11 @@ namespace keyboard {
9
10
  extern int num_keys;
10
11
  extern const bool *keys;
11
12
 
12
- Napi::Value packKey(Napi::Env &env, SDL_Keycode keycode);
13
+ packed::Value packKey (SDL_Keycode keycode);
13
14
 
14
- Napi::Value getKey(const Napi::CallbackInfo &info);
15
- Napi::Value getScancode(const Napi::CallbackInfo &info);
16
- Napi::Value getState(const Napi::CallbackInfo &info);
15
+ Napi::Value getKey (const Napi::CallbackInfo &info);
16
+ Napi::Value getScancode (const Napi::CallbackInfo &info);
17
+ Napi::Value getState (const Napi::CallbackInfo &info);
17
18
 
18
19
  }; // namespace keyboard
19
20
 
@@ -14,22 +14,28 @@
14
14
  #include "audio.h"
15
15
  #include "clipboard.h"
16
16
  #include "power.h"
17
+ #include "threads.h"
18
+ #include "testing.h"
17
19
 
18
20
 
19
21
  Napi::Object
20
22
  init (Napi::Env env, Napi::Object exports)
21
23
  {
22
- global::registerEnv(env);
23
-
24
24
  exports.Set("enums_get", Napi::Function::New<enums::get>(env));
25
25
 
26
26
  exports.Set("global_initialize", Napi::Function::New<global::initialize>(env));
27
+ exports.Set("global_initSubsystem", Napi::Function::New<global::initSubsystem>(env));
27
28
  exports.Set("global_cleanup", Napi::Function::New<global::cleanup>(env));
28
29
 
30
+ exports.Set("events_register", Napi::Function::New<threads::registerThread>(env));
31
+ exports.Set("events_setDemand", Napi::Function::New<threads::setDemand>(env));
29
32
  exports.Set("events_poll", Napi::Function::New<events::poll>(env));
33
+ exports.Set("events_broadcastChange", Napi::Function::New<events::broadcastChange>(env));
30
34
  exports.Set("events_isDispatchingFromWatch", Napi::Function::New<events::isDispatchingFromWatch>(env));
31
35
 
36
+ exports.Set("video_getDrivers", Napi::Function::New<video::getDrivers>(env));
32
37
  exports.Set("video_getDisplays", Napi::Function::New<video::getDisplays>(env));
38
+ exports.Set("video_convert", Napi::Function::New<video::convert>(env));
33
39
 
34
40
  exports.Set("window_create", Napi::Function::New<window::create>(env));
35
41
  exports.Set("window_setTitle", Napi::Function::New<window::setTitle>(env));
@@ -50,6 +56,8 @@ init (Napi::Env env, Napi::Object exports)
50
56
  exports.Set("window_render", Napi::Function::New<window::render>(env));
51
57
  exports.Set("window_setIcon", Napi::Function::New<window::setIcon>(env));
52
58
  exports.Set("window_flash", Napi::Function::New<window::flash>(env));
59
+ exports.Set("window_registerHitTestHandler", Napi::Function::New<window::registerHitTestHandler>(env));
60
+ exports.Set("window_setHitTest", Napi::Function::New<window::setHitTest>(env));
53
61
  exports.Set("window_destroy", Napi::Function::New<window::destroy>(env));
54
62
 
55
63
  exports.Set("keyboard_getKey", Napi::Function::New<keyboard::getKey>(env));
@@ -86,20 +94,32 @@ init (Napi::Env env, Napi::Object exports)
86
94
  exports.Set("sensor_getData", Napi::Function::New<sensor::getData>(env));
87
95
  exports.Set("sensor_close", Napi::Function::New<sensor::close>(env));
88
96
 
97
+ exports.Set("audio_getDrivers", Napi::Function::New<audio::getDrivers>(env));
89
98
  exports.Set("audio_getDevices", Napi::Function::New<audio::getDevices>(env));
90
99
  exports.Set("audio_open", Napi::Function::New<audio::open>(env));
91
- exports.Set("audio_getDeviceFormat", Napi::Function::New<audio::getDeviceFormat>(env));
92
100
  exports.Set("audio_play", Napi::Function::New<audio::play>(env));
93
101
  exports.Set("audio_getQueued", Napi::Function::New<audio::getQueued>(env));
102
+ exports.Set("audio_flush", Napi::Function::New<audio::flush>(env));
94
103
  exports.Set("audio_getAvailable", Napi::Function::New<audio::getAvailable>(env));
95
104
  exports.Set("audio_putData", Napi::Function::New<audio::putData>(env));
96
105
  exports.Set("audio_getData", Napi::Function::New<audio::getData>(env));
97
106
  exports.Set("audio_clear", Napi::Function::New<audio::clear>(env));
98
107
  exports.Set("audio_close", Napi::Function::New<audio::close>(env));
108
+ exports.Set("audio_setNotify", Napi::Function::New<audio::setNotify>(env));
109
+ exports.Set("audio_setGain", Napi::Function::New<audio::setGain>(env));
110
+ exports.Set("audio_setFrequencyRatio", Napi::Function::New<audio::setFrequencyRatio>(env));
111
+ exports.Set("audio_convert", Napi::Function::New<audio::convert>(env));
99
112
 
100
113
  exports.Set("clipboard_getText", Napi::Function::New<clipboard::getText>(env));
101
114
  exports.Set("clipboard_setText", Napi::Function::New<clipboard::setText>(env));
102
115
 
116
+ exports.Set("testing_attachVirtualJoystick", Napi::Function::New<testing::attachVirtualJoystick>(env));
117
+ exports.Set("testing_setVirtualJoystickAxis", Napi::Function::New<testing::setVirtualJoystickAxis>(env));
118
+ exports.Set("testing_setVirtualJoystickButton", Napi::Function::New<testing::setVirtualJoystickButton>(env));
119
+ exports.Set("testing_detachVirtualJoystick", Napi::Function::New<testing::detachVirtualJoystick>(env));
120
+ exports.Set("testing_pushAudioDeviceEvent", Napi::Function::New<testing::pushAudioDeviceEvent>(env));
121
+ exports.Set("testing_hitTest", Napi::Function::New<testing::hitTest>(env));
122
+
103
123
  exports.Set("power_getInfo", Napi::Function::New<power::getInfo>(env));
104
124
 
105
125
  return exports;
@@ -1,10 +1,24 @@
1
1
  #include "mouse.h"
2
+ #include "global.h"
2
3
  #include <SDL3/SDL.h>
3
4
  #include <string>
4
- #include <sstream>
5
5
 
6
6
  static SDL_Cursor *allocated_cursor = nullptr;
7
7
 
8
+ template <typename... Args>
9
+ static void
10
+ _adoptCursor (Napi::Env &env, SDL_Cursor *cursor, const Args &...args)
11
+ {
12
+ if (!SDL_SetCursor(cursor)) {
13
+ Napi::Error error = global::sdlError(env, "SDL_SetCursor", args...);
14
+ SDL_DestroyCursor(cursor);
15
+ throw error;
16
+ }
17
+
18
+ if (allocated_cursor != nullptr) { SDL_DestroyCursor(allocated_cursor); }
19
+ allocated_cursor = cursor;
20
+ }
21
+
8
22
 
9
23
  Napi::Value
10
24
  mouse::getButton (const Napi::CallbackInfo &info)
@@ -43,10 +57,7 @@ mouse::setPosition (const Napi::CallbackInfo &info)
43
57
  float y = info[1].As<Napi::Number>().FloatValue();
44
58
 
45
59
  if (!SDL_WarpMouseGlobal(x, y)) {
46
- std::ostringstream message;
47
- message << "SDL_WarpMouseGlobal(" << x << ", " << y << ") error: " << SDL_GetError();
48
- SDL_ClearError();
49
- throw Napi::Error::New(env, message.str());
60
+ throw global::sdlError(env, "SDL_WarpMouseGlobal", x, y);
50
61
  }
51
62
 
52
63
  return env.Undefined();
@@ -61,44 +72,26 @@ mouse::setCursor (const Napi::CallbackInfo &info)
61
72
 
62
73
  SDL_Cursor *cursor = SDL_CreateSystemCursor((SDL_SystemCursor) cursor_id);
63
74
  if (cursor == nullptr) {
64
- std::ostringstream message;
65
- message << "SDL_CreateSystemCursor(" << cursor_id << ") error: " << SDL_GetError();
66
- SDL_ClearError();
67
- throw Napi::Error::New(env, message.str());
68
- }
69
-
70
- if (!SDL_SetCursor(cursor)) {
71
- std::ostringstream message;
72
- message << "SDL_SetCursor(" << cursor_id << ") error: " << SDL_GetError();
73
- SDL_ClearError();
74
- SDL_DestroyCursor(cursor);
75
- throw Napi::Error::New(env, message.str());
75
+ throw global::sdlError(env, "SDL_CreateSystemCursor", cursor_id);
76
76
  }
77
77
 
78
- if (allocated_cursor != nullptr) { SDL_DestroyCursor(allocated_cursor); }
79
- allocated_cursor = cursor;
78
+ _adoptCursor(env, cursor, cursor_id);
80
79
 
81
80
  return env.Undefined();
82
81
  }
83
82
 
84
83
  Napi::Value
85
- mouse::resetCursor(const Napi::CallbackInfo &info)
84
+ mouse::resetCursor (const Napi::CallbackInfo &info)
86
85
  {
87
86
  Napi::Env env = info.Env();
88
87
 
89
88
  SDL_Cursor *cursor = SDL_GetDefaultCursor();
90
89
  if (cursor == nullptr) {
91
- std::ostringstream message;
92
- message << "SDL_GetDefaultCursor() error: " << SDL_GetError();
93
- SDL_ClearError();
94
- throw Napi::Error::New(env, message.str());
90
+ throw global::sdlError(env, "SDL_GetDefaultCursor");
95
91
  }
96
92
 
97
93
  if (!SDL_SetCursor(cursor)) {
98
- std::ostringstream message;
99
- message << "SDL_SetCursor() error: " << SDL_GetError();
100
- SDL_ClearError();
101
- throw Napi::Error::New(env, message.str());
94
+ throw global::sdlError(env, "SDL_SetCursor");
102
95
  }
103
96
 
104
97
  if (allocated_cursor != nullptr) {
@@ -124,34 +117,19 @@ mouse::setCursorImage (const Napi::CallbackInfo &info)
124
117
 
125
118
  SDL_Surface* surface = SDL_CreateSurfaceFrom(w, h, (SDL_PixelFormat) format, pixels, stride);
126
119
  if (surface == nullptr) {
127
- std::ostringstream message;
128
- message << "SDL_CreateSurfaceFrom(" << w << ", " << h << ", " << format << ") error: " << SDL_GetError();
129
- SDL_ClearError();
130
- throw Napi::Error::New(env, message.str());
120
+ throw global::sdlError(env, "SDL_CreateSurfaceFrom", w, h, format);
131
121
  }
132
122
 
133
123
  SDL_Cursor *cursor = SDL_CreateColorCursor(surface, x, y);
134
124
  if (cursor == nullptr) {
125
+ Napi::Error error = global::sdlError(env, "SDL_CreateColorCursor", x, y);
135
126
  SDL_DestroySurface(surface);
136
-
137
- std::ostringstream message;
138
- message << "SDL_CreateColorCursor(" << x << ", " << y << ") error: " << SDL_GetError();
139
- SDL_ClearError();
140
- throw Napi::Error::New(env, message.str());
127
+ throw error;
141
128
  }
142
129
 
143
130
  SDL_DestroySurface(surface);
144
131
 
145
- if (!SDL_SetCursor(cursor)) {
146
- std::ostringstream message;
147
- message << "SDL_SetCursor() error: " << SDL_GetError();
148
- SDL_ClearError();
149
- SDL_DestroyCursor(cursor);
150
- throw Napi::Error::New(env, message.str());
151
- }
152
-
153
- if (allocated_cursor != nullptr) { SDL_DestroyCursor(allocated_cursor); }
154
- allocated_cursor = cursor;
132
+ _adoptCursor(env, cursor);
155
133
 
156
134
  return env.Undefined();
157
135
  }
@@ -165,10 +143,7 @@ mouse::showCursor (const Napi::CallbackInfo &info)
165
143
 
166
144
  bool success = should_show ? SDL_ShowCursor() : SDL_HideCursor();
167
145
  if (!success) {
168
- std::ostringstream message;
169
- message << (should_show ? "SDL_ShowCursor(" : "SDL_HideCursor(") << ") error: " << SDL_GetError();
170
- SDL_ClearError();
171
- throw Napi::Error::New(env, message.str());
146
+ throw global::sdlError(env, should_show ? "SDL_ShowCursor" : "SDL_HideCursor");
172
147
  }
173
148
 
174
149
  return env.Undefined();
@@ -180,10 +155,7 @@ mouse::redrawCursor (const Napi::CallbackInfo &info)
180
155
  Napi::Env env = info.Env();
181
156
 
182
157
  if (!SDL_SetCursor(nullptr)) {
183
- std::ostringstream message;
184
- message << "SDL_SetCursor(nullptr) error: " << SDL_GetError();
185
- SDL_ClearError();
186
- throw Napi::Error::New(env, message.str());
158
+ throw global::sdlError(env, "SDL_SetCursor", "nullptr");
187
159
  }
188
160
 
189
161
  return env.Undefined();
@@ -197,10 +169,7 @@ mouse::capture (const Napi::CallbackInfo &info)
197
169
  bool should_capture = info[0].As<Napi::Boolean>().Value();
198
170
 
199
171
  if (!SDL_CaptureMouse(should_capture)) {
200
- std::ostringstream message;
201
- message << "SDL_CaptureMouse(" << should_capture << ") error: " << SDL_GetError();
202
- SDL_ClearError();
203
- throw Napi::Error::New(env, message.str());
172
+ throw global::sdlError(env, "SDL_CaptureMouse", should_capture);
204
173
  }
205
174
 
206
175
  return env.Undefined();
@@ -5,15 +5,15 @@
5
5
 
6
6
  namespace mouse {
7
7
 
8
- Napi::Value getButton(const Napi::CallbackInfo &info);
9
- Napi::Value getPosition(const Napi::CallbackInfo &info);
10
- Napi::Value setPosition(const Napi::CallbackInfo &info);
11
- Napi::Value setCursor(const Napi::CallbackInfo &info);
12
- Napi::Value resetCursor(const Napi::CallbackInfo &info);
13
- Napi::Value setCursorImage(const Napi::CallbackInfo &info);
14
- Napi::Value showCursor(const Napi::CallbackInfo &info);
15
- Napi::Value redrawCursor(const Napi::CallbackInfo &info);
16
- Napi::Value capture(const Napi::CallbackInfo &info);
8
+ Napi::Value getButton (const Napi::CallbackInfo &info);
9
+ Napi::Value getPosition (const Napi::CallbackInfo &info);
10
+ Napi::Value setPosition (const Napi::CallbackInfo &info);
11
+ Napi::Value setCursor (const Napi::CallbackInfo &info);
12
+ Napi::Value resetCursor (const Napi::CallbackInfo &info);
13
+ Napi::Value setCursorImage (const Napi::CallbackInfo &info);
14
+ Napi::Value showCursor (const Napi::CallbackInfo &info);
15
+ Napi::Value redrawCursor (const Napi::CallbackInfo &info);
16
+ Napi::Value capture (const Napi::CallbackInfo &info);
17
17
 
18
18
  }; // namespace mouse
19
19
 
@@ -0,0 +1,99 @@
1
+ #include "packed.h"
2
+ #include <cstring>
3
+
4
+
5
+ template <typename... Fs>
6
+ struct Overload : Fs... { using Fs::operator()...; };
7
+
8
+ template <typename... Fs>
9
+ Overload(Fs...) -> Overload<Fs...>;
10
+
11
+
12
+ packed::Value
13
+ packed::Value::ref (Str value)
14
+ {
15
+ Value result;
16
+ result.data.emplace<const char *>(value.c_str());
17
+ return result;
18
+ }
19
+
20
+ packed::Value
21
+ packed::Value::object ()
22
+ {
23
+ Value value;
24
+ value.data.emplace<Object>();
25
+ return value;
26
+ }
27
+
28
+ packed::Value
29
+ packed::Value::array ()
30
+ {
31
+ Value value;
32
+ value.data.emplace<Array>();
33
+ return value;
34
+ }
35
+
36
+ bool
37
+ packed::Value::has (const char *key) const
38
+ {
39
+ return get(key) != nullptr;
40
+ }
41
+
42
+ const packed::Value *
43
+ packed::Value::get (const char *key) const
44
+ {
45
+ for (const Member &member : std::get<Object>(data)) {
46
+ if (std::strcmp(member.key, key) == 0) { return &member.value; }
47
+ }
48
+ return nullptr;
49
+ }
50
+
51
+ packed::Value &
52
+ packed::Value::set (const char *key, Value value)
53
+ {
54
+ std::get<Object>(data).push_back({ key, std::move(value) });
55
+ return *this;
56
+ }
57
+
58
+ packed::Value &
59
+ packed::Value::set (Str key, Value value)
60
+ {
61
+ return set(key.c_str(), std::move(value));
62
+ }
63
+
64
+ packed::Value &
65
+ packed::Value::push (Value value)
66
+ {
67
+ std::get<Array>(data).push_back(std::move(value));
68
+ return *this;
69
+ }
70
+
71
+ Napi::Value
72
+ packed::Value::toNapi (Napi::Env env) const
73
+ {
74
+ return std::visit(Overload {
75
+ [&](std::monostate) -> Napi::Value { return env.Null(); },
76
+ [&](bool value) -> Napi::Value { return Napi::Boolean::New(env, value); },
77
+ [&](int32_t value) -> Napi::Value { return Napi::Number::New(env, value); },
78
+ [&](uint32_t value) -> Napi::Value { return Napi::Number::New(env, value); },
79
+ [&](double value) -> Napi::Value { return Napi::Number::New(env, value); },
80
+ [&](int64_t value) -> Napi::Value { return Napi::BigInt::New(env, value); },
81
+ [&](uint64_t value) -> Napi::Value { return Napi::BigInt::New(env, value); },
82
+ [&](const char *value) -> Napi::Value { return Napi::String::New(env, value); },
83
+ [&](const std::string &value) -> Napi::Value { return Napi::String::New(env, value); },
84
+ [&](const Object &members) -> Napi::Value {
85
+ Napi::Object object = Napi::Object::New(env);
86
+ for (const Member &member : members) {
87
+ object.Set(member.key, member.value.toNapi(env));
88
+ }
89
+ return object;
90
+ },
91
+ [&](const Array &items) -> Napi::Value {
92
+ Napi::Array array = Napi::Array::New(env, items.size());
93
+ for (size_t i = 0; i < items.size(); i++) {
94
+ array.Set((uint32_t) i, items[i].toNapi(env));
95
+ }
96
+ return array;
97
+ },
98
+ }, data);
99
+ }
@@ -0,0 +1,86 @@
1
+ #ifndef _PACKED_H_
2
+ #define _PACKED_H_
3
+
4
+ #include <napi.h>
5
+ #include <string>
6
+ #include <vector>
7
+ #include <variant>
8
+ #include <cstdint>
9
+
10
+ namespace packed {
11
+
12
+ struct Member;
13
+ struct Value;
14
+
15
+ using Object = std::vector<Member>;
16
+ using Array = std::vector<Value>;
17
+
18
+ struct Str {
19
+ template <size_t N>
20
+ constexpr Str (const char (&value)[N]) : string(value) {}
21
+
22
+ constexpr const char *c_str () const { return string; }
23
+ constexpr operator const char * () const { return string; }
24
+
25
+ private:
26
+ const char *string;
27
+ };
28
+
29
+ struct Value {
30
+ using Data = std::variant<
31
+ std::monostate,
32
+ bool,
33
+ int32_t,
34
+ uint32_t,
35
+ double,
36
+ int64_t,
37
+ uint64_t,
38
+ const char *,
39
+ std::string,
40
+ Object,
41
+ Array
42
+ >;
43
+
44
+ Data data;
45
+
46
+ Value () = default;
47
+ Value (bool value) : data(std::in_place_type<bool>, value) {}
48
+ Value (int32_t value) : data(std::in_place_type<int32_t>, value) {}
49
+ Value (uint32_t value) : data(std::in_place_type<uint32_t>, value) {}
50
+ Value (double value) : data(std::in_place_type<double>, value) {}
51
+ Value (float value) : data(std::in_place_type<double>, value) {}
52
+ Value (int64_t value) : data(std::in_place_type<int64_t>, value) {}
53
+ Value (uint64_t value) : data(std::in_place_type<uint64_t>, value) {}
54
+ Value (const char *value) : data(std::in_place_type<std::string>, value) {}
55
+ Value (const std::string &value) : data(std::in_place_type<std::string>, value) {}
56
+ Value (std::string &&value) : data(std::in_place_type<std::string>, std::move(value)) {}
57
+
58
+ static Value ref (Str value);
59
+ static Value ref (const std::string &value) = delete;
60
+ static Value ref (std::string &&value) = delete;
61
+ static Value object ();
62
+ static Value array ();
63
+
64
+ bool isNull () const { return std::holds_alternative<std::monostate>(data); }
65
+ bool has (const char *key) const;
66
+ const Value *get (const char *key) const;
67
+ const Object &members () const { return std::get<Object>(data); }
68
+ Value &set (const char *key, Value value);
69
+ Value &set (Str key, Value value);
70
+ Value &set (const std::string &key, Value value) = delete;
71
+ Value &set (std::string &&key, Value value) = delete;
72
+ Value &push (Value value);
73
+
74
+ Napi::Value toNapi (Napi::Env env) const;
75
+ };
76
+
77
+ struct Member {
78
+ // Non-owning on purpose: keys are always string literals or static strings, so
79
+ // skipping the copy keeps event packing cheap. Never pass a temporary here.
80
+ const char *key;
81
+ Value value;
82
+ };
83
+
84
+ }; // namespace packed
85
+
86
+ #endif // _PACKED_H_