@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 _AUDIO_H_
3
3
 
4
4
  #include <napi.h>
5
+ #include "packed.h"
5
6
  #include <SDL3/SDL.h>
6
7
  #include <map>
7
8
  #include <string>
8
9
 
9
10
  namespace audio {
10
11
 
11
- extern std::map<bool, std::string> device_types;
12
- extern std::map<SDL_AudioFormat, std::string> formats;
13
-
14
- void closeEnvStreams(Napi::Env env);
15
- void closeAllStreams();
16
-
17
- Napi::Value _getDevice(Napi::Env &env, SDL_AudioDeviceID id);
18
- Napi::Array _getDevices(Napi::Env &env, bool is_recording);
19
-
20
- Napi::Value getDevices(const Napi::CallbackInfo &info);
21
- Napi::Value open(const Napi::CallbackInfo &info);
22
- Napi::Value getDeviceFormat(const Napi::CallbackInfo &info);
23
- Napi::Value play(const Napi::CallbackInfo &info);
24
- Napi::Value getQueued(const Napi::CallbackInfo &info);
25
- Napi::Value getAvailable(const Napi::CallbackInfo &info);
26
- Napi::Value putData(const Napi::CallbackInfo &info);
27
- Napi::Value getData(const Napi::CallbackInfo &info);
28
- Napi::Value clear(const Napi::CallbackInfo &info);
29
- Napi::Value close(const Napi::CallbackInfo &info);
12
+ extern const std::map<bool, packed::Str> device_types;
13
+ extern const std::map<SDL_AudioFormat, packed::Str> formats;
14
+
15
+ void closeAllStreams ();
16
+
17
+ packed::Value _getDevice (SDL_AudioDeviceID id);
18
+ packed::Value _getFormat (SDL_AudioDeviceID id);
19
+ packed::Value _getDefaultDevice (bool is_recording);
20
+
21
+ Napi::Value getDrivers (const Napi::CallbackInfo &info);
22
+ Napi::Value getDevices (const Napi::CallbackInfo &info);
23
+ Napi::Value open (const Napi::CallbackInfo &info);
24
+ Napi::Value play (const Napi::CallbackInfo &info);
25
+ Napi::Value getQueued (const Napi::CallbackInfo &info);
26
+ Napi::Value flush (const Napi::CallbackInfo &info);
27
+ Napi::Value getAvailable (const Napi::CallbackInfo &info);
28
+ Napi::Value putData (const Napi::CallbackInfo &info);
29
+ Napi::Value getData (const Napi::CallbackInfo &info);
30
+ Napi::Value clear (const Napi::CallbackInfo &info);
31
+ Napi::Value close (const Napi::CallbackInfo &info);
32
+ Napi::Value setNotify (const Napi::CallbackInfo &info);
33
+ Napi::Value setGain (const Napi::CallbackInfo &info);
34
+ Napi::Value setFrequencyRatio (const Napi::CallbackInfo &info);
35
+ Napi::Value convert (const Napi::CallbackInfo &info);
30
36
 
31
37
  }; // namespace audio
32
38
 
@@ -1,47 +1,21 @@
1
1
  #include "clipboard.h"
2
+ #include "global.h"
2
3
  #include <SDL3/SDL.h>
3
4
  #include <string>
4
- #include <sstream>
5
5
 
6
6
  Napi::Value
7
7
  clipboard::getText (const Napi::CallbackInfo &info)
8
8
  {
9
9
  Napi::Env env = info.Env();
10
10
 
11
- bool has_text = SDL_HasClipboardText();
12
-
13
- // Clear any stale error, since an empty result is otherwise ambiguous
14
- SDL_ClearError();
15
-
16
- if (!has_text) {
17
- return Napi::String::New(env, "");
18
- }
19
-
20
- char *text = SDL_GetClipboardText();
21
- if (text[0] == '\0') {
22
- SDL_free(text);
23
-
24
- // The clipboard may have been legitimately emptied since the check above
25
- const char *error = SDL_GetError();
26
- if (error[0] == '\0') {
27
- return Napi::String::New(env, "");
28
- }
29
-
30
- std::ostringstream message;
31
- message << "SDL_GetClipboardText() error: " << error;
32
- SDL_ClearError();
33
- throw Napi::Error::New(env, message.str());
11
+ // SDL reports a failed read as an empty string and SDL_GetError() can't tell that apart from an empty clipboard, so only the out-of-memory NULL is treated as an error.
12
+ global::SdlPtr<char> text(SDL_GetClipboardText());
13
+ if (!text) {
14
+ throw global::sdlError(env, "SDL_GetClipboardText");
34
15
  }
35
16
 
36
- Napi::String result;
37
- try { result = Napi::String::New(env, text); }
38
- catch (...) {
39
- SDL_free(text);
40
- throw;
41
- }
42
- SDL_free(text);
43
-
44
- return result;
17
+ SDL_ClearError();
18
+ return Napi::String::New(env, text.get());
45
19
  }
46
20
 
47
21
  Napi::Value
@@ -52,10 +26,7 @@ clipboard::setText (const Napi::CallbackInfo &info)
52
26
  std::string text = info[0].As<Napi::String>().Utf8Value();
53
27
 
54
28
  if (!SDL_SetClipboardText(text.c_str())) {
55
- std::ostringstream message;
56
- message << "SDL_SetClipboardText() error: " << SDL_GetError();
57
- SDL_ClearError();
58
- throw Napi::Error::New(env, message.str());
29
+ throw global::sdlError(env, "SDL_SetClipboardText");
59
30
  }
60
31
 
61
32
  return env.Undefined();
@@ -5,8 +5,8 @@
5
5
 
6
6
  namespace clipboard {
7
7
 
8
- Napi::Value getText(const Napi::CallbackInfo &info);
9
- Napi::Value setText(const Napi::CallbackInfo &info);
8
+ Napi::Value getText (const Napi::CallbackInfo &info);
9
+ Napi::Value setText (const Napi::CallbackInfo &info);
10
10
 
11
11
  }; // namespace clipboard
12
12
 
@@ -2,7 +2,7 @@
2
2
  #include <SDL3/SDL.h>
3
3
 
4
4
  Napi::Value
5
- enums::get(const Napi::CallbackInfo &info)
5
+ enums::get (const Napi::CallbackInfo &info)
6
6
  {
7
7
  Napi::Env env = info.Env();
8
8
 
@@ -74,6 +74,7 @@ enums::get(const Napi::CallbackInfo &info)
74
74
  Napi::Object scale_mode = Napi::Object::New(env);
75
75
  scale_mode.Set("nearest", (int) SDL_SCALEMODE_NEAREST);
76
76
  scale_mode.Set("linear", (int) SDL_SCALEMODE_LINEAR);
77
+ // TODO: expose SDL_SCALEMODE_PIXELART as 'pixelArt' for window.render() only, since surface blits in convert() treat it as nearest. It needs SDL 3.4, so NODE_SDL_SYSTEM builds against 3.2 would need an SDL_VERSION_ATLEAST guard.
77
78
 
78
79
  Napi::Object audio_formats = Napi::Object::New(env);
79
80
  audio_formats.Set("s8", (int) SDL_AUDIO_S8);
@@ -248,6 +249,7 @@ enums::get(const Napi::CallbackInfo &info)
248
249
  scancodes.Set("CLEARAGAIN", (int) SDL_SCANCODE_CLEARAGAIN);
249
250
  scancodes.Set("CRSEL", (int) SDL_SCANCODE_CRSEL);
250
251
  scancodes.Set("EXSEL", (int) SDL_SCANCODE_EXSEL);
252
+ scancodes.Set("FRONT", (int) SDL_SCANCODE_FRONT);
251
253
  scancodes.Set("KP_00", (int) SDL_SCANCODE_KP_00);
252
254
  scancodes.Set("KP_000", (int) SDL_SCANCODE_KP_000);
253
255
  scancodes.Set("THOUSANDSSEPARATOR", (int) SDL_SCANCODE_THOUSANDSSEPARATOR);
@@ -366,6 +368,18 @@ enums::get(const Napi::CallbackInfo &info)
366
368
  cursors.Set("swResize", (int) SDL_SYSTEM_CURSOR_SW_RESIZE);
367
369
  cursors.Set("wResize", (int) SDL_SYSTEM_CURSOR_W_RESIZE);
368
370
 
371
+ Napi::Object hit_test_results = Napi::Object::New(env);
372
+ hit_test_results.Set("normal", (int) SDL_HITTEST_NORMAL);
373
+ hit_test_results.Set("draggable", (int) SDL_HITTEST_DRAGGABLE);
374
+ hit_test_results.Set("resizeTopLeft", (int) SDL_HITTEST_RESIZE_TOPLEFT);
375
+ hit_test_results.Set("resizeTop", (int) SDL_HITTEST_RESIZE_TOP);
376
+ hit_test_results.Set("resizeTopRight", (int) SDL_HITTEST_RESIZE_TOPRIGHT);
377
+ hit_test_results.Set("resizeRight", (int) SDL_HITTEST_RESIZE_RIGHT);
378
+ hit_test_results.Set("resizeBottomRight", (int) SDL_HITTEST_RESIZE_BOTTOMRIGHT);
379
+ hit_test_results.Set("resizeBottom", (int) SDL_HITTEST_RESIZE_BOTTOM);
380
+ hit_test_results.Set("resizeBottomLeft", (int) SDL_HITTEST_RESIZE_BOTTOMLEFT);
381
+ hit_test_results.Set("resizeLeft", (int) SDL_HITTEST_RESIZE_LEFT);
382
+
369
383
  Napi::Object all = Napi::Object::New(env);
370
384
  all.Set("pixelFormat", pixel_formats);
371
385
  all.Set("scaleMode", scale_mode);
@@ -373,6 +387,7 @@ enums::get(const Napi::CallbackInfo &info)
373
387
  all.Set("scancodes", scancodes);
374
388
  all.Set("mouseButtons", mouse_buttons);
375
389
  all.Set("cursors", cursors);
390
+ all.Set("hitTestResult", hit_test_results);
376
391
 
377
392
  return all;
378
393
  }
@@ -5,7 +5,7 @@
5
5
 
6
6
  namespace enums {
7
7
 
8
- Napi::Value get(const Napi::CallbackInfo &info);
8
+ Napi::Value get (const Napi::CallbackInfo &info);
9
9
 
10
10
  }; // namespace enums
11
11