@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
@@ -1,140 +1,253 @@
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;
10
-
11
-
12
- static Napi::Value
13
- _packDisplay (Napi::Env &env, SDL_DisplayID display_id)
8
+ const std::map<SDL_DisplayOrientation, packed::Str> video::orientations {
9
+ // { SDL_ORIENTATION_UNKNOWN, nullptr },
10
+ { SDL_ORIENTATION_LANDSCAPE, "landscape" },
11
+ { SDL_ORIENTATION_LANDSCAPE_FLIPPED, "landscapeFlipped" },
12
+ { SDL_ORIENTATION_PORTRAIT, "portrait" },
13
+ { SDL_ORIENTATION_PORTRAIT_FLIPPED, "portraitFlipped" },
14
+ };
15
+
16
+ const std::map<SDL_PixelFormat, packed::Str> video::formats {
17
+ { SDL_PIXELFORMAT_RGB332, "rgb332" },
18
+ { SDL_PIXELFORMAT_XRGB4444, "xrgb4444" },
19
+ { SDL_PIXELFORMAT_XBGR4444, "xbgr4444" },
20
+ { SDL_PIXELFORMAT_XRGB1555, "xrgb1555" },
21
+ { SDL_PIXELFORMAT_XBGR1555, "xbgr1555" },
22
+ { SDL_PIXELFORMAT_ARGB4444, "argb4444" },
23
+ { SDL_PIXELFORMAT_RGBA4444, "rgba4444" },
24
+ { SDL_PIXELFORMAT_ABGR4444, "abgr4444" },
25
+ { SDL_PIXELFORMAT_BGRA4444, "bgra4444" },
26
+ { SDL_PIXELFORMAT_ARGB1555, "argb1555" },
27
+ { SDL_PIXELFORMAT_RGBA5551, "rgba5551" },
28
+ { SDL_PIXELFORMAT_ABGR1555, "abgr1555" },
29
+ { SDL_PIXELFORMAT_BGRA5551, "bgra5551" },
30
+ { SDL_PIXELFORMAT_RGB565, "rgb565" },
31
+ { SDL_PIXELFORMAT_BGR565, "bgr565" },
32
+ { SDL_PIXELFORMAT_RGB24, "rgb24" },
33
+ { SDL_PIXELFORMAT_BGR24, "bgr24" },
34
+ { SDL_PIXELFORMAT_XRGB8888, "xrgb8888" },
35
+ { SDL_PIXELFORMAT_RGBX8888, "rgbx8888" },
36
+ { SDL_PIXELFORMAT_XBGR8888, "xbgr8888" },
37
+ { SDL_PIXELFORMAT_BGRX8888, "bgrx8888" },
38
+ { SDL_PIXELFORMAT_ARGB8888, "argb8888" },
39
+ { SDL_PIXELFORMAT_RGBA8888, "rgba8888" },
40
+ { SDL_PIXELFORMAT_ABGR8888, "abgr8888" },
41
+ { SDL_PIXELFORMAT_BGRA8888, "bgra8888" },
42
+ { SDL_PIXELFORMAT_ARGB2101010, "argb2101010" },
43
+ { SDL_PIXELFORMAT_XRGB2101010, "xrgb2101010" },
44
+ { SDL_PIXELFORMAT_XBGR2101010, "xbgr2101010" },
45
+ { SDL_PIXELFORMAT_ABGR2101010, "abgr2101010" },
46
+ { SDL_PIXELFORMAT_RGB48, "rgb48" },
47
+ { SDL_PIXELFORMAT_BGR48, "bgr48" },
48
+ { SDL_PIXELFORMAT_RGBA64, "rgba64" },
49
+ { SDL_PIXELFORMAT_ARGB64, "argb64" },
50
+ { SDL_PIXELFORMAT_BGRA64, "bgra64" },
51
+ { SDL_PIXELFORMAT_ABGR64, "abgr64" },
52
+ { SDL_PIXELFORMAT_RGB48_FLOAT, "rgb48f" },
53
+ { SDL_PIXELFORMAT_BGR48_FLOAT, "bgr48f" },
54
+ { SDL_PIXELFORMAT_RGBA64_FLOAT, "rgba64f" },
55
+ { SDL_PIXELFORMAT_ARGB64_FLOAT, "argb64f" },
56
+ { SDL_PIXELFORMAT_BGRA64_FLOAT, "bgra64f" },
57
+ { SDL_PIXELFORMAT_ABGR64_FLOAT, "abgr64f" },
58
+ { SDL_PIXELFORMAT_RGB96_FLOAT, "rgb96f" },
59
+ { SDL_PIXELFORMAT_BGR96_FLOAT, "bgr96f" },
60
+ { SDL_PIXELFORMAT_RGBA128_FLOAT, "rgba128f" },
61
+ { SDL_PIXELFORMAT_ARGB128_FLOAT, "argb128f" },
62
+ { SDL_PIXELFORMAT_BGRA128_FLOAT, "bgra128f" },
63
+ { SDL_PIXELFORMAT_ABGR128_FLOAT, "abgr128f" },
64
+ { SDL_PIXELFORMAT_YV12, "yv12" },
65
+ { SDL_PIXELFORMAT_IYUV, "iyuv" },
66
+ { SDL_PIXELFORMAT_YUY2, "yuy2" },
67
+ { SDL_PIXELFORMAT_UYVY, "uyvy" },
68
+ { SDL_PIXELFORMAT_YVYU, "yvyu" },
69
+ { SDL_PIXELFORMAT_NV12, "nv12" },
70
+ { SDL_PIXELFORMAT_NV21, "nv21" },
71
+ { SDL_PIXELFORMAT_P010, "p010" },
72
+ };
73
+
74
+
75
+ packed::Value
76
+ video::_getDisplay (SDL_DisplayID display_id)
14
77
  {
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
78
  SDL_Rect rect;
32
- if(!SDL_GetDisplayBounds(display_id, &rect)) {
79
+
80
+ if (!SDL_GetDisplayBounds(display_id, &rect)) {
33
81
  SDL_ClearError();
34
- return env.Null();
82
+ return packed::Value();
35
83
  }
84
+ packed::Value geometry = packed::Value::object();
85
+ geometry.set("x", rect.x);
86
+ geometry.set("y", rect.y);
87
+ geometry.set("width", rect.w);
88
+ geometry.set("height", rect.h);
36
89
 
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);
90
+ const char *_name = SDL_GetDisplayName(display_id);
91
+ packed::Value name;
92
+ if (_name != nullptr) { name = _name; }
93
+ else { SDL_ClearError(); }
42
94
 
43
- if(!SDL_GetDisplayUsableBounds(display_id, &rect)) {
44
- SDL_ClearError();
45
- return env.Null();
95
+ packed::Value format;
96
+ packed::Value frequency;
97
+ const SDL_DisplayMode *mode = SDL_GetCurrentDisplayMode(display_id);
98
+ if (mode == nullptr) { SDL_ClearError(); }
99
+ else {
100
+ auto format_entry = video::formats.find(mode->format);
101
+ if (format_entry != video::formats.end()) { format = packed::Value::ref(format_entry->second); }
102
+ if (mode->refresh_rate != 0.0f) { frequency = mode->refresh_rate; }
46
103
  }
47
104
 
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);
105
+ // Can't fail: SDL only rejects unknown displays (falling back to the full
106
+ // bounds otherwise), and SDL_GetDisplayBounds just succeeded for this one.
107
+ SDL_GetDisplayUsableBounds(display_id, &rect);
108
+ packed::Value usable = packed::Value::object();
109
+ usable.set("x", rect.x);
110
+ usable.set("y", rect.y);
111
+ usable.set("width", rect.w);
112
+ usable.set("height", rect.h);
53
113
 
54
- Napi::Value scale;
114
+ packed::Value scale;
55
115
  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
- }
116
+ if (_scale == 0.0f) { SDL_ClearError(); }
117
+ else { scale = _scale; }
63
118
 
64
119
  SDL_DisplayOrientation _orientation = SDL_GetCurrentDisplayOrientation(display_id);
65
120
  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);
121
+ packed::Value orientation;
122
+ if (orientation_entry != video::orientations.end()) { orientation = packed::Value::ref(orientation_entry->second); }
123
+ else { SDL_ClearError(); }
124
+
125
+ packed::Value display = packed::Value::object();
126
+ display.set("id", display_id);
127
+ display.set("name", std::move(name));
128
+ display.set("format", std::move(format));
129
+ display.set("frequency", std::move(frequency));
130
+ display.set("geometry", std::move(geometry));
131
+ display.set("usable", std::move(usable));
132
+ display.set("scale", std::move(scale));
133
+ display.set("orientation", std::move(orientation));
93
134
 
94
135
  return display;
95
136
  }
96
137
 
97
- Napi::Value
98
- video::_getDisplay (Napi::Env &env, SDL_DisplayID display_id)
99
- {
100
- return _packDisplay(env, display_id);
101
- }
102
-
103
138
  Napi::Array
104
139
  video::_getDisplays (Napi::Env &env)
105
140
  {
106
141
  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());
142
+ global::SdlArray<SDL_DisplayID> display_ids(SDL_GetDisplays(&num_displays));
143
+ if (!display_ids) {
144
+ throw global::sdlError(env, "SDL_GetDisplays");
113
145
  }
114
146
 
115
147
  Napi::Array displays = Napi::Array::New(env);
116
148
 
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
- }
124
- }
125
- catch (...) {
126
- SDL_free(display_ids);
127
- throw;
149
+ int num_returned = 0;
150
+ for (int i = 0; i < num_displays; i++) {
151
+ packed::Value display = video::_getDisplay(display_ids[i]);
152
+ if (display.isNull()) { continue; }
153
+ displays.Set(num_returned++, display.toNapi(env));
128
154
  }
129
- SDL_free(display_ids);
130
155
 
131
156
  return displays;
132
157
  }
133
158
 
134
159
  Napi::Value
135
- video::getDisplays(const Napi::CallbackInfo &info)
160
+ video::getDrivers (const Napi::CallbackInfo &info)
161
+ {
162
+ Napi::Env env = info.Env();
163
+
164
+ Napi::Array all = Napi::Array::New(env);
165
+ int num_drivers = SDL_GetNumVideoDrivers();
166
+ for (int i = 0; i < num_drivers; i ++) {
167
+ all.Set(i, SDL_GetVideoDriver(i));
168
+ }
169
+
170
+ Napi::Object result = Napi::Object::New(env);
171
+ result.Set("all", all);
172
+ // Only NULL before the video subsystem is initialized, which the JS
173
+ // assertInitialized() guard rules out, so this can never be NULL here.
174
+ result.Set("current", Napi::String::New(env, SDL_GetCurrentVideoDriver()));
175
+
176
+ return result;
177
+ }
178
+
179
+ Napi::Value
180
+ video::getDisplays (const Napi::CallbackInfo &info)
136
181
  {
137
182
  Napi::Env env = info.Env();
138
183
 
139
184
  return video::_getDisplays(env);
140
185
  }
186
+
187
+ Napi::Value
188
+ video::convert (const Napi::CallbackInfo &info)
189
+ {
190
+ Napi::Env env = info.Env();
191
+
192
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
193
+
194
+ int src_w = info[0].As<Napi::Number>().Int32Value();
195
+ int src_h = info[1].As<Napi::Number>().Int32Value();
196
+ SDL_PixelFormat src_format = (SDL_PixelFormat) info[2].As<Napi::Number>().Int32Value();
197
+ void *src = info[3].As<Napi::Buffer<char>>().Data();
198
+ int src_stride = info[4].As<Napi::Number>().Int32Value();
199
+
200
+ int dst_w = info[5].As<Napi::Number>().Int32Value();
201
+ int dst_h = info[6].As<Napi::Number>().Int32Value();
202
+ SDL_PixelFormat dst_format = (SDL_PixelFormat) info[7].As<Napi::Number>().Int32Value();
203
+ void *dst = info[8].As<Napi::Buffer<char>>().Data();
204
+ int dst_stride = info[9].As<Napi::Number>().Int32Value();
205
+
206
+ SDL_ScaleMode scaling = (SDL_ScaleMode) info[10].As<Napi::Number>().Int32Value();
207
+
208
+ if (src_w == dst_w && src_h == dst_h) {
209
+ if (!SDL_ConvertPixels(src_w, src_h, src_format, src, src_stride, dst_format, dst, dst_stride)) {
210
+ throw global::sdlError(env, "SDL_ConvertPixels", src_w, src_h, src_format, dst_format);
211
+ }
212
+ return env.Undefined();
213
+ }
214
+
215
+ SDL_Surface *src_surface = SDL_CreateSurfaceFrom(src_w, src_h, src_format, src, src_stride);
216
+ if (src_surface == nullptr) {
217
+ throw global::sdlError(env, "SDL_CreateSurfaceFrom", src_w, src_h, src_format);
218
+ }
219
+ if (!SDL_SetSurfaceBlendMode(src_surface, SDL_BLENDMODE_NONE)) {
220
+ Napi::Error error = global::sdlError(env, "SDL_SetSurfaceBlendMode");
221
+ SDL_DestroySurface(src_surface);
222
+ throw error;
223
+ }
224
+
225
+ bool is_yuv = SDL_ISPIXELFORMAT_FOURCC(dst_format);
226
+ SDL_Surface *dst_surface = is_yuv
227
+ ? SDL_CreateSurface(dst_w, dst_h, SDL_PIXELFORMAT_ARGB8888)
228
+ : SDL_CreateSurfaceFrom(dst_w, dst_h, dst_format, dst, dst_stride);
229
+ if (dst_surface == nullptr) {
230
+ Napi::Error error = global::sdlError(env, is_yuv ? "SDL_CreateSurface" : "SDL_CreateSurfaceFrom", dst_w, dst_h, is_yuv ? SDL_PIXELFORMAT_ARGB8888 : dst_format);
231
+ SDL_DestroySurface(src_surface);
232
+ throw error;
233
+ }
234
+
235
+ if (!SDL_BlitSurfaceScaled(src_surface, nullptr, dst_surface, nullptr, scaling)) {
236
+ Napi::Error error = global::sdlError(env, "SDL_BlitSurfaceScaled", src_w, src_h, src_format, dst_w, dst_h, dst_format);
237
+ SDL_DestroySurface(src_surface);
238
+ SDL_DestroySurface(dst_surface);
239
+ throw error;
240
+ }
241
+ SDL_DestroySurface(src_surface);
242
+
243
+ if (is_yuv) {
244
+ if (!SDL_ConvertPixels(dst_w, dst_h, dst_surface->format, dst_surface->pixels, dst_surface->pitch, dst_format, dst, dst_stride)) {
245
+ Napi::Error error = global::sdlError(env, "SDL_ConvertPixels", dst_w, dst_h, SDL_PIXELFORMAT_ARGB8888, dst_format);
246
+ SDL_DestroySurface(dst_surface);
247
+ throw error;
248
+ }
249
+ }
250
+ SDL_DestroySurface(dst_surface);
251
+
252
+ return env.Undefined();
253
+ }
@@ -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 const std::map<SDL_DisplayOrientation, packed::Str> orientations;
12
+ extern const 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
 
16
- Napi::Value getDisplays(const Napi::CallbackInfo &info);
17
+ Napi::Value getDrivers (const Napi::CallbackInfo &info);
18
+ Napi::Value getDisplays (const Napi::CallbackInfo &info);
19
+ Napi::Value convert (const Napi::CallbackInfo &info);
17
20
 
18
21
  }; // namespace video
19
22