@kmamal/sdl 1.0.0-4 → 1.0.0-8

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 (84) hide show
  1. package/CHANGELOG.md +12 -6
  2. package/binding.gyp +3 -0
  3. package/package.json +6 -13
  4. package/scripts/util/common.js +2 -2
  5. package/src/javascript/audio/audio-playback-stream.js +9 -10
  6. package/src/javascript/audio/audio-recording-stream.js +9 -8
  7. package/src/javascript/audio/audio-stream.js +100 -44
  8. package/src/javascript/audio/conversion.js +41 -0
  9. package/src/javascript/audio/devices.js +11 -0
  10. package/src/javascript/audio/events.js +65 -0
  11. package/src/javascript/audio/format-helpers.js +8 -4
  12. package/src/javascript/audio/index.js +25 -20
  13. package/src/javascript/audio/prevent-exit.js +12 -9
  14. package/src/javascript/audio/validate.js +9 -0
  15. package/src/javascript/cleanup.js +9 -6
  16. package/src/javascript/clipboard/index.js +8 -3
  17. package/src/javascript/device-instance.js +61 -0
  18. package/src/javascript/events/events-via-poll.js +33 -27
  19. package/src/javascript/events/index.js +76 -482
  20. package/src/javascript/events/try.js +22 -0
  21. package/src/javascript/gamepad/{device.js → devices.js} +4 -5
  22. package/src/javascript/gamepad/events.js +66 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +12 -160
  24. package/src/javascript/gamepad/index.js +15 -16
  25. package/src/javascript/globals.js +10 -1
  26. package/src/javascript/index.js +2 -5
  27. package/src/javascript/joystick/devices.js +43 -0
  28. package/src/javascript/joystick/events.js +129 -0
  29. package/src/javascript/joystick/index.js +9 -18
  30. package/src/javascript/joystick/joystick-device-instance.js +175 -0
  31. package/src/javascript/joystick/joystick-instance.js +12 -158
  32. package/src/javascript/keyboard/index.js +20 -10
  33. package/src/javascript/keyboard/key-mapping.js +46 -27
  34. package/src/javascript/mouse/index.js +36 -29
  35. package/src/javascript/sensor/events.js +21 -0
  36. package/src/javascript/sensor/index.js +13 -2
  37. package/src/javascript/sensor/sensor-instance.js +7 -47
  38. package/src/javascript/subsystems.js +56 -0
  39. package/src/javascript/touch/index.js +9 -4
  40. package/src/javascript/validate.js +114 -0
  41. package/src/javascript/video/conversion.js +63 -0
  42. package/src/javascript/video/events.js +179 -0
  43. package/src/javascript/video/format-helpers.js +10 -8
  44. package/src/javascript/video/index.js +28 -14
  45. package/src/javascript/video/validate.js +45 -0
  46. package/src/javascript/video/window.js +144 -161
  47. package/src/native/audio.cpp +457 -151
  48. package/src/native/audio.h +12 -6
  49. package/src/native/clipboard.cpp +8 -37
  50. package/src/native/enums.cpp +2 -0
  51. package/src/native/events.cpp +416 -354
  52. package/src/native/events.h +67 -65
  53. package/src/native/gamepad.cpp +86 -103
  54. package/src/native/gamepad.h +7 -8
  55. package/src/native/global.cpp +190 -85
  56. package/src/native/global.h +48 -2
  57. package/src/native/joystick.cpp +180 -274
  58. package/src/native/joystick.h +12 -6
  59. package/src/native/keyboard.cpp +60 -22
  60. package/src/native/keyboard.h +2 -1
  61. package/src/native/module.cpp +20 -3
  62. package/src/native/mouse.cpp +27 -58
  63. package/src/native/packed.cpp +106 -0
  64. package/src/native/packed.h +91 -0
  65. package/src/native/power.cpp +7 -1
  66. package/src/native/power.h +2 -2
  67. package/src/native/sensor.cpp +61 -69
  68. package/src/native/sensor.h +3 -3
  69. package/src/native/testing.cpp +131 -0
  70. package/src/native/testing.h +16 -0
  71. package/src/native/threads.cpp +300 -0
  72. package/src/native/threads.h +36 -0
  73. package/src/native/touch.cpp +32 -43
  74. package/src/native/touch.h +2 -1
  75. package/src/native/video.cpp +145 -95
  76. package/src/native/video.h +6 -3
  77. package/src/native/window.cpp +155 -218
  78. package/src/native/window.h +21 -20
  79. package/src/types/index.d.ts +141 -69
  80. package/src/javascript/audio/device.js +0 -37
  81. package/src/javascript/joystick/device.js +0 -13
  82. package/src/javascript/joystick/shared.js +0 -90
  83. package/src/javascript/worker.js +0 -88
  84. package/src/types/worker.d.ts +0 -71
@@ -2,9 +2,8 @@
2
2
  #include "global.h"
3
3
  #include <SDL3/SDL.h>
4
4
  #include <string>
5
- #include <sstream>
6
5
 
7
- std::map<SDL_TouchDeviceType, std::string> touch::device_types;
6
+ std::map<SDL_TouchDeviceType, packed::Str> touch::device_types;
8
7
 
9
8
  Napi::Value
10
9
  touch::getDevices(const Napi::CallbackInfo &info)
@@ -12,56 +11,46 @@ touch::getDevices(const Napi::CallbackInfo &info)
12
11
  Napi::Env env = info.Env();
13
12
 
14
13
  int num_devices;
15
- SDL_TouchID *touch_ids = SDL_GetTouchDevices(&num_devices);
16
- if (touch_ids == nullptr) {
17
- std::ostringstream message;
18
- message << "SDL_GetTouchDevices() error: " << SDL_GetError();
19
- SDL_ClearError();
20
- throw Napi::Error::New(env, message.str());
14
+ global::SdlArray<SDL_TouchID> touch_ids(SDL_GetTouchDevices(&num_devices));
15
+ if (!touch_ids) {
16
+ throw global::sdlError(env, "SDL_GetTouchDevices");
21
17
  }
22
18
 
23
19
  Napi::Array devices = Napi::Array::New(env);
24
20
 
25
- try {
26
- int num_returned = 0;
27
- for (int i = 0; i < num_devices; i++) {
28
- SDL_TouchID id = touch_ids[i];
29
- if (id == SDL_MOUSE_TOUCHID || id == SDL_PEN_TOUCHID) { continue; }
21
+ int num_returned = 0;
22
+ for (int i = 0; i < num_devices; i++) {
23
+ SDL_TouchID id = touch_ids[i];
24
+ if (id == SDL_MOUSE_TOUCHID || id == SDL_PEN_TOUCHID) { continue; }
30
25
 
31
- const char *_name = SDL_GetTouchDeviceName(id);
32
- Napi::Value name;
33
- if (_name != nullptr) {
34
- name = Napi::String::New(env, _name);
35
- }
36
- else {
37
- SDL_ClearError();
38
- name = env.Null();
39
- }
26
+ const char *_name = SDL_GetTouchDeviceName(id);
27
+ Napi::Value name;
28
+ if (_name != nullptr) {
29
+ name = Napi::String::New(env, _name);
30
+ }
31
+ else {
32
+ SDL_ClearError();
33
+ name = env.Null();
34
+ }
40
35
 
41
- SDL_TouchDeviceType _type = SDL_GetTouchDeviceType(id);
42
- auto type_entry = device_types.find(_type);
43
- Napi::Value type;
44
- if (type_entry != device_types.end()) {
45
- type = Napi::String::New(env, type_entry->second);
46
- }
47
- else {
48
- SDL_ClearError();
49
- type = env.Null();
50
- }
36
+ SDL_TouchDeviceType _type = SDL_GetTouchDeviceType(id);
37
+ auto type_entry = device_types.find(_type);
38
+ Napi::Value type;
39
+ if (type_entry != device_types.end()) {
40
+ type = Napi::String::New(env, type_entry->second);
41
+ }
42
+ else {
43
+ SDL_ClearError();
44
+ type = env.Null();
45
+ }
51
46
 
52
- Napi::Object device = Napi::Object::New(env);
53
- device.Set("id", Napi::BigInt::New(env, id));
54
- device.Set("name", name);
55
- device.Set("type", type);
47
+ Napi::Object device = Napi::Object::New(env);
48
+ device.Set("id", Napi::BigInt::New(env, id));
49
+ device.Set("name", name);
50
+ device.Set("type", type);
56
51
 
57
- devices.Set(num_returned++, device);
58
- }
59
- }
60
- catch (...) {
61
- SDL_free(touch_ids);
62
- throw;
52
+ devices.Set(num_returned++, device);
63
53
  }
64
- SDL_free(touch_ids);
65
54
 
66
55
  return devices;
67
56
  }
@@ -2,12 +2,13 @@
2
2
  #define _TOUCH_H_
3
3
 
4
4
  #include <napi.h>
5
+ #include "packed.h"
5
6
  #include <SDL3/SDL.h>
6
7
  #include <map>
7
8
 
8
9
  namespace touch {
9
10
 
10
- extern std::map<SDL_TouchDeviceType, std::string> device_types;
11
+ extern std::map<SDL_TouchDeviceType, packed::Str> device_types;
11
12
 
12
13
  Napi::Value getDevices(const Napi::CallbackInfo &info);
13
14
 
@@ -1,136 +1,118 @@
1
1
  #include "video.h"
2
+ #include "global.h"
2
3
  #include <SDL3/SDL.h>
3
4
  #include <string>
4
- #include <sstream>
5
5
  #include <map>
6
6
 
7
7
 
8
- std::map<SDL_DisplayOrientation, std::string> video::orientations;
9
- std::map<SDL_PixelFormat, std::string> video::formats;
8
+ std::map<SDL_DisplayOrientation, packed::Str> video::orientations;
9
+ std::map<SDL_PixelFormat, packed::Str> video::formats;
10
10
 
11
11
 
12
- static Napi::Value
13
- _packDisplay (Napi::Env &env, SDL_DisplayID display_id)
12
+ packed::Value
13
+ video::_getDisplay (SDL_DisplayID display_id)
14
14
  {
15
- const char *_name = SDL_GetDisplayName(display_id);
16
- Napi::Value name;
17
- if (_name != nullptr) {
18
- name = Napi::String::New(env, _name);
19
- }
20
- else {
21
- SDL_ClearError();
22
- name = env.Null();
23
- }
24
-
25
- const SDL_DisplayMode *mode = SDL_GetCurrentDisplayMode(display_id);
26
- if (mode == nullptr) {
27
- SDL_ClearError();
28
- return env.Null();
29
- }
30
-
31
15
  SDL_Rect rect;
32
- if(!SDL_GetDisplayBounds(display_id, &rect)) {
16
+
17
+ if (!SDL_GetDisplayBounds(display_id, &rect)) {
33
18
  SDL_ClearError();
34
- return env.Null();
19
+ return packed::Value();
35
20
  }
21
+ packed::Value geometry = packed::Value::object();
22
+ geometry.set("x", rect.x);
23
+ geometry.set("y", rect.y);
24
+ geometry.set("width", rect.w);
25
+ geometry.set("height", rect.h);
36
26
 
37
- Napi::Object geometry = Napi::Object::New(env);
38
- geometry.Set("x", rect.x);
39
- geometry.Set("y", rect.y);
40
- geometry.Set("width", rect.w);
41
- geometry.Set("height", rect.h);
27
+ const char *_name = SDL_GetDisplayName(display_id);
28
+ packed::Value name;
29
+ if (_name != nullptr) { name = _name; }
30
+ else { SDL_ClearError(); }
42
31
 
43
- if(!SDL_GetDisplayUsableBounds(display_id, &rect)) {
44
- SDL_ClearError();
45
- return env.Null();
32
+ packed::Value format;
33
+ packed::Value frequency;
34
+ const SDL_DisplayMode *mode = SDL_GetCurrentDisplayMode(display_id);
35
+ if (mode == nullptr) { SDL_ClearError(); }
36
+ else {
37
+ auto format_entry = video::formats.find(mode->format);
38
+ if (format_entry != video::formats.end()) { format = packed::Value::ref(format_entry->second); }
39
+ if (mode->refresh_rate != 0.0f) { frequency = mode->refresh_rate; }
46
40
  }
47
41
 
48
- Napi::Object usable = Napi::Object::New(env);
49
- usable.Set("x", rect.x);
50
- usable.Set("y", rect.y);
51
- usable.Set("width", rect.w);
52
- usable.Set("height", rect.h);
42
+ // Can't fail: SDL only rejects unknown displays (falling back to the full
43
+ // bounds otherwise), and SDL_GetDisplayBounds just succeeded for this one.
44
+ SDL_GetDisplayUsableBounds(display_id, &rect);
45
+ packed::Value usable = packed::Value::object();
46
+ usable.set("x", rect.x);
47
+ usable.set("y", rect.y);
48
+ usable.set("width", rect.w);
49
+ usable.set("height", rect.h);
53
50
 
54
- Napi::Value scale;
51
+ packed::Value scale;
55
52
  float _scale = SDL_GetDisplayContentScale(display_id);
56
- if (_scale == 0.0f) {
57
- SDL_ClearError();
58
- scale = env.Null();
59
- }
60
- else {
61
- scale = Napi::Number::New(env, _scale);
62
- }
53
+ if (_scale == 0.0f) { SDL_ClearError(); }
54
+ else { scale = _scale; }
63
55
 
64
56
  SDL_DisplayOrientation _orientation = SDL_GetCurrentDisplayOrientation(display_id);
65
57
  auto orientation_entry = video::orientations.find(_orientation);
66
- Napi::Value orientation;
67
- if (orientation_entry != video::orientations.end()) {
68
- orientation = Napi::String::New(env, orientation_entry->second);
69
- }
70
- else {
71
- SDL_ClearError();
72
- orientation = env.Null();
73
- }
74
-
75
- auto format_entry = video::formats.find(mode->format);
76
- Napi::Value format = format_entry != video::formats.end()
77
- ? Napi::String::New(env, format_entry->second)
78
- : env.Null();
79
-
80
- Napi::Value frequency = mode->refresh_rate != 0.0f
81
- ? Napi::Number::New(env, mode->refresh_rate)
82
- : env.Null();
83
-
84
- Napi::Object display = Napi::Object::New(env);
85
- display.Set("id", display_id);
86
- display.Set("name", name);
87
- display.Set("format", format);
88
- display.Set("frequency", frequency);
89
- display.Set("geometry", geometry);
90
- display.Set("usable", usable);
91
- display.Set("scale", scale);
92
- display.Set("orientation", orientation);
58
+ packed::Value orientation;
59
+ if (orientation_entry != video::orientations.end()) { orientation = packed::Value::ref(orientation_entry->second); }
60
+ else { SDL_ClearError(); }
61
+
62
+ packed::Value display = packed::Value::object();
63
+ display.set("id", display_id);
64
+ display.set("name", std::move(name));
65
+ display.set("format", std::move(format));
66
+ display.set("frequency", std::move(frequency));
67
+ display.set("geometry", std::move(geometry));
68
+ display.set("usable", std::move(usable));
69
+ display.set("scale", std::move(scale));
70
+ display.set("orientation", std::move(orientation));
93
71
 
94
72
  return display;
95
73
  }
96
74
 
97
- Napi::Value
98
- video::_getDisplay (Napi::Env &env, SDL_DisplayID display_id)
99
- {
100
- return _packDisplay(env, display_id);
101
- }
102
-
103
75
  Napi::Array
104
76
  video::_getDisplays (Napi::Env &env)
105
77
  {
106
78
  int num_displays;
107
- SDL_DisplayID *display_ids = SDL_GetDisplays(&num_displays);
108
- if (display_ids == nullptr) {
109
- std::ostringstream message;
110
- message << "SDL_GetDisplays() error: " << SDL_GetError();
111
- SDL_ClearError();
112
- throw Napi::Error::New(env, message.str());
79
+ global::SdlArray<SDL_DisplayID> display_ids(SDL_GetDisplays(&num_displays));
80
+ if (!display_ids) {
81
+ throw global::sdlError(env, "SDL_GetDisplays");
113
82
  }
114
83
 
115
84
  Napi::Array displays = Napi::Array::New(env);
116
85
 
117
- try {
118
- int num_returned = 0;
119
- for (int i = 0; i < num_displays; i++) {
120
- Napi::Value display = _packDisplay(env, display_ids[i]);
121
- if (display.IsNull()) { continue; }
122
- displays.Set(num_returned++, display);
123
- }
86
+ int num_returned = 0;
87
+ for (int i = 0; i < num_displays; i++) {
88
+ packed::Value display = video::_getDisplay(display_ids[i]);
89
+ if (display.isNull()) { continue; }
90
+ displays.Set(num_returned++, display.toNapi(env));
124
91
  }
125
- catch (...) {
126
- SDL_free(display_ids);
127
- throw;
128
- }
129
- SDL_free(display_ids);
130
92
 
131
93
  return displays;
132
94
  }
133
95
 
96
+ Napi::Value
97
+ video::getDrivers(const Napi::CallbackInfo &info)
98
+ {
99
+ Napi::Env env = info.Env();
100
+
101
+ Napi::Array all = Napi::Array::New(env);
102
+ int num_drivers = SDL_GetNumVideoDrivers();
103
+ for (int i = 0; i < num_drivers; i ++) {
104
+ all.Set(i, SDL_GetVideoDriver(i));
105
+ }
106
+
107
+ Napi::Object result = Napi::Object::New(env);
108
+ result.Set("all", all);
109
+ // Only NULL before the video subsystem is initialized, which the JS
110
+ // assertInitialized() guard rules out, so this can never be NULL here.
111
+ result.Set("current", Napi::String::New(env, SDL_GetCurrentVideoDriver()));
112
+
113
+ return result;
114
+ }
115
+
134
116
  Napi::Value
135
117
  video::getDisplays(const Napi::CallbackInfo &info)
136
118
  {
@@ -138,3 +120,71 @@ video::getDisplays(const Napi::CallbackInfo &info)
138
120
 
139
121
  return video::_getDisplays(env);
140
122
  }
123
+
124
+ Napi::Value
125
+ video::convert(const Napi::CallbackInfo &info)
126
+ {
127
+ Napi::Env env = info.Env();
128
+
129
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
130
+
131
+ int src_w = info[0].As<Napi::Number>().Int32Value();
132
+ int src_h = info[1].As<Napi::Number>().Int32Value();
133
+ SDL_PixelFormat src_format = (SDL_PixelFormat) info[2].As<Napi::Number>().Int32Value();
134
+ void *src = info[3].As<Napi::Buffer<char>>().Data();
135
+ int src_stride = info[4].As<Napi::Number>().Int32Value();
136
+
137
+ int dst_w = info[5].As<Napi::Number>().Int32Value();
138
+ int dst_h = info[6].As<Napi::Number>().Int32Value();
139
+ SDL_PixelFormat dst_format = (SDL_PixelFormat) info[7].As<Napi::Number>().Int32Value();
140
+ void *dst = info[8].As<Napi::Buffer<char>>().Data();
141
+ int dst_stride = info[9].As<Napi::Number>().Int32Value();
142
+
143
+ SDL_ScaleMode scaling = (SDL_ScaleMode) info[10].As<Napi::Number>().Int32Value();
144
+
145
+ if (src_w == dst_w && src_h == dst_h) {
146
+ if (!SDL_ConvertPixels(src_w, src_h, src_format, src, src_stride, dst_format, dst, dst_stride)) {
147
+ throw global::sdlError(env, "SDL_ConvertPixels", src_w, src_h, src_format, dst_format);
148
+ }
149
+ return env.Undefined();
150
+ }
151
+
152
+ SDL_Surface *src_surface = SDL_CreateSurfaceFrom(src_w, src_h, src_format, src, src_stride);
153
+ if (src_surface == nullptr) {
154
+ throw global::sdlError(env, "SDL_CreateSurfaceFrom", src_w, src_h, src_format);
155
+ }
156
+ if (!SDL_SetSurfaceBlendMode(src_surface, SDL_BLENDMODE_NONE)) {
157
+ Napi::Error error = global::sdlError(env, "SDL_SetSurfaceBlendMode");
158
+ SDL_DestroySurface(src_surface);
159
+ throw error;
160
+ }
161
+
162
+ bool is_yuv = SDL_ISPIXELFORMAT_FOURCC(dst_format);
163
+ SDL_Surface *dst_surface = is_yuv
164
+ ? SDL_CreateSurface(dst_w, dst_h, SDL_PIXELFORMAT_ARGB8888)
165
+ : SDL_CreateSurfaceFrom(dst_w, dst_h, dst_format, dst, dst_stride);
166
+ if (dst_surface == nullptr) {
167
+ Napi::Error error = global::sdlError(env, is_yuv ? "SDL_CreateSurface" : "SDL_CreateSurfaceFrom", dst_w, dst_h, is_yuv ? SDL_PIXELFORMAT_ARGB8888 : dst_format);
168
+ SDL_DestroySurface(src_surface);
169
+ throw error;
170
+ }
171
+
172
+ if (!SDL_BlitSurfaceScaled(src_surface, nullptr, dst_surface, nullptr, scaling)) {
173
+ Napi::Error error = global::sdlError(env, "SDL_BlitSurfaceScaled", src_w, src_h, src_format, dst_w, dst_h, dst_format);
174
+ SDL_DestroySurface(src_surface);
175
+ SDL_DestroySurface(dst_surface);
176
+ throw error;
177
+ }
178
+ SDL_DestroySurface(src_surface);
179
+
180
+ if (is_yuv) {
181
+ if (!SDL_ConvertPixels(dst_w, dst_h, dst_surface->format, dst_surface->pixels, dst_surface->pitch, dst_format, dst, dst_stride)) {
182
+ Napi::Error error = global::sdlError(env, "SDL_ConvertPixels", dst_w, dst_h, SDL_PIXELFORMAT_ARGB8888, dst_format);
183
+ SDL_DestroySurface(dst_surface);
184
+ throw error;
185
+ }
186
+ }
187
+ SDL_DestroySurface(dst_surface);
188
+
189
+ return env.Undefined();
190
+ }
@@ -2,18 +2,21 @@
2
2
  #define _VIDEO_H_
3
3
 
4
4
  #include <napi.h>
5
+ #include "packed.h"
5
6
  #include <SDL3/SDL.h>
6
7
  #include <map>
7
8
 
8
9
  namespace video {
9
10
 
10
- extern std::map<SDL_DisplayOrientation, std::string> orientations;
11
- extern std::map<SDL_PixelFormat, std::string> formats;
11
+ extern std::map<SDL_DisplayOrientation, packed::Str> orientations;
12
+ extern std::map<SDL_PixelFormat, packed::Str> formats;
12
13
 
13
- Napi::Value _getDisplay (Napi::Env &env, SDL_DisplayID display_id);
14
+ packed::Value _getDisplay (SDL_DisplayID display_id);
14
15
  Napi::Array _getDisplays (Napi::Env &env);
15
16
 
17
+ Napi::Value getDrivers(const Napi::CallbackInfo &info);
16
18
  Napi::Value getDisplays(const Napi::CallbackInfo &info);
19
+ Napi::Value convert(const Napi::CallbackInfo &info);
17
20
 
18
21
  }; // namespace video
19
22