@kmamal/sdl 0.11.12 → 1.0.0-2

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 (86) hide show
  1. package/CHANGELOG.md +257 -1
  2. package/README.md +26 -2685
  3. package/binding.gyp +82 -65
  4. package/package.json +17 -11
  5. package/scripts/build-sdl.mjs +15 -7
  6. package/scripts/build.mjs +30 -15
  7. package/scripts/download-release.mjs +9 -6
  8. package/scripts/download-sdl.mjs +9 -6
  9. package/scripts/install.mjs +13 -5
  10. package/scripts/util/common.js +9 -1
  11. package/src/javascript/audio/audio-playback-stream.js +50 -0
  12. package/src/javascript/audio/audio-recording-stream.js +32 -0
  13. package/src/javascript/audio/{audio-instance.js → audio-stream.js} +35 -32
  14. package/src/javascript/audio/device.js +34 -6
  15. package/src/javascript/audio/format-helpers.js +26 -35
  16. package/src/javascript/audio/index.js +37 -26
  17. package/src/javascript/audio/prevent-exit.js +19 -9
  18. package/src/javascript/cleanup.js +19 -10
  19. package/src/javascript/enums.js +5 -0
  20. package/src/javascript/events/events-via-poll.js +25 -7
  21. package/src/javascript/events/index.js +274 -175
  22. package/src/javascript/gamepad/device.js +23 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +209 -0
  24. package/src/javascript/gamepad/index.js +51 -0
  25. package/src/javascript/globals.js +8 -5
  26. package/src/javascript/helpers.js +34 -7
  27. package/src/javascript/index.js +5 -3
  28. package/src/javascript/joystick/device.js +5 -10
  29. package/src/javascript/joystick/index.js +7 -7
  30. package/src/javascript/joystick/joystick-instance.js +79 -42
  31. package/src/javascript/joystick/shared.js +88 -0
  32. package/src/javascript/keyboard/index.js +6 -4
  33. package/src/javascript/keyboard/key-mapping.js +70 -28
  34. package/src/javascript/mouse/index.js +21 -10
  35. package/src/javascript/sensor/devices.js +17 -0
  36. package/src/javascript/sensor/index.js +2 -16
  37. package/src/javascript/sensor/sensor-instance.js +20 -6
  38. package/src/javascript/touch/devices.js +17 -0
  39. package/src/javascript/touch/index.js +3 -7
  40. package/src/javascript/video/format-helpers.js +99 -0
  41. package/src/javascript/video/index.js +37 -5
  42. package/src/javascript/video/window.js +280 -134
  43. package/src/native/audio.cpp +182 -58
  44. package/src/native/audio.h +10 -5
  45. package/src/native/clipboard.cpp +22 -5
  46. package/src/native/cocoa-window.h +1 -0
  47. package/src/native/cocoa-window.mm +1 -1
  48. package/src/native/enums.cpp +91 -58
  49. package/src/native/events.cpp +437 -284
  50. package/src/native/events.h +18 -12
  51. package/src/native/gamepad.cpp +185 -0
  52. package/src/native/gamepad.h +27 -0
  53. package/src/native/global.cpp +173 -167
  54. package/src/native/global.h +0 -2
  55. package/src/native/joystick.cpp +287 -225
  56. package/src/native/joystick.h +7 -6
  57. package/src/native/keyboard.cpp +28 -8
  58. package/src/native/keyboard.h +4 -2
  59. package/src/native/module.cpp +14 -8
  60. package/src/native/mouse.cpp +54 -34
  61. package/src/native/mouse.h +0 -1
  62. package/src/native/power.cpp +10 -4
  63. package/src/native/power.h +1 -1
  64. package/src/native/sensor.cpp +52 -52
  65. package/src/native/sensor.h +1 -1
  66. package/src/native/touch.cpp +45 -24
  67. package/src/native/touch.h +1 -1
  68. package/src/native/video.cpp +115 -75
  69. package/src/native/video.h +5 -2
  70. package/src/native/window.cpp +415 -305
  71. package/src/native/window.h +3 -1
  72. package/src/types/helpers.d.ts +44 -19
  73. package/src/types/index.d.ts +343 -255
  74. package/scripts/util/fetch.js +0 -85
  75. package/src/javascript/audio/audio-playback-instance.js +0 -23
  76. package/src/javascript/audio/audio-recording-instance.js +0 -17
  77. package/src/javascript/controller/controller-instance.js +0 -160
  78. package/src/javascript/controller/device.js +0 -26
  79. package/src/javascript/controller/index.js +0 -33
  80. package/src/javascript/events/reconcile-audio-devices.js +0 -16
  81. package/src/javascript/events/reconcile-displays.js +0 -17
  82. package/src/javascript/events/reconcile-joystick-and-controller-devices.js +0 -40
  83. package/src/javascript/events/reconcile.js +0 -61
  84. package/src/javascript/video/display.js +0 -4
  85. package/src/native/controller.cpp +0 -192
  86. package/src/native/controller.h +0 -26
@@ -2,7 +2,7 @@
2
2
  #define _TOUCH_H_
3
3
 
4
4
  #include <napi.h>
5
- #include <SDL.h>
5
+ #include <SDL3/SDL.h>
6
6
  #include <map>
7
7
 
8
8
  namespace touch {
@@ -1,100 +1,140 @@
1
1
  #include "video.h"
2
- #include <SDL.h>
2
+ #include <SDL3/SDL.h>
3
3
  #include <string>
4
4
  #include <sstream>
5
5
  #include <map>
6
6
 
7
7
 
8
8
  std::map<SDL_DisplayOrientation, std::string> video::orientations;
9
- std::map<SDL_PixelFormatEnum, std::string> video::formats;
9
+ std::map<SDL_PixelFormat, std::string> video::formats;
10
10
 
11
11
 
12
- Napi::Value
13
- video::getDisplays(const Napi::CallbackInfo &info)
12
+ static Napi::Value
13
+ _packDisplay (Napi::Env &env, SDL_DisplayID display_id)
14
14
  {
15
- Napi::Env env = info.Env();
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
+ }
16
24
 
17
- int num_displays = SDL_GetNumVideoDisplays();
18
- if (num_displays < 0) {
19
- std::ostringstream message;
20
- message << "SDL_GetNumVideoDisplays() error: " << SDL_GetError();
25
+ const SDL_DisplayMode *mode = SDL_GetCurrentDisplayMode(display_id);
26
+ if (mode == nullptr) {
21
27
  SDL_ClearError();
22
- throw Napi::Error::New(env, message.str());
28
+ return env.Null();
23
29
  }
24
30
 
25
- Napi::Array displays = Napi::Array::New(env, num_displays);
31
+ SDL_Rect rect;
32
+ if(!SDL_GetDisplayBounds(display_id, &rect)) {
33
+ SDL_ClearError();
34
+ return env.Null();
35
+ }
26
36
 
27
- for (int i = 0; i < num_displays; i++) {
28
- const char *_name = SDL_GetDisplayName(i);
29
- Napi::Value name = _name != nullptr
30
- ? Napi::String::New(env, _name)
31
- : env.Null();
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);
32
42
 
33
- SDL_DisplayMode mode;
34
- if (SDL_GetCurrentDisplayMode(i, &mode) < 0) {
35
- std::ostringstream message;
36
- message << "SDL_GetCurrentDisplayMode(" << i << ") error: " << SDL_GetError();
37
- SDL_ClearError();
38
- throw Napi::Error::New(env, message.str());
39
- }
43
+ if(!SDL_GetDisplayUsableBounds(display_id, &rect)) {
44
+ SDL_ClearError();
45
+ return env.Null();
46
+ }
40
47
 
41
- SDL_Rect rect;
42
- if(SDL_GetDisplayBounds(i, &rect) < 0) {
43
- std::ostringstream message;
44
- message << "SDL_GetDisplayBounds(" << i << ") error: " << SDL_GetError();
45
- SDL_ClearError();
46
- throw Napi::Error::New(env, message.str());
47
- }
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);
48
53
 
49
- Napi::Object geometry = Napi::Object::New(env);
50
- geometry.Set("x", rect.x);
51
- geometry.Set("y", rect.y);
52
- geometry.Set("width", rect.w);
53
- geometry.Set("height", rect.h);
54
-
55
- if(SDL_GetDisplayUsableBounds(i, &rect) < 0) {
56
- std::ostringstream message;
57
- message << "SDL_GetDisplayUsableBounds(" << i << ") error: " << SDL_GetError();
58
- SDL_ClearError();
59
- throw Napi::Error::New(env, message.str());
60
- }
54
+ Napi::Value scale;
55
+ 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
+ }
61
63
 
62
- Napi::Object usable = Napi::Object::New(env);
63
- usable.Set("x", rect.x);
64
- usable.Set("y", rect.y);
65
- usable.Set("width", rect.w);
66
- usable.Set("height", rect.h);
64
+ SDL_DisplayOrientation _orientation = SDL_GetCurrentDisplayOrientation(display_id);
65
+ 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
+ }
67
74
 
68
- Napi::Value dpi;
69
- float ddpi, hdpi, vdpi;
70
- if (SDL_GetDisplayDPI(i, &ddpi, &hdpi, &vdpi) < 0) {
71
- dpi = env.Null();
72
- }
73
- else {
74
- Napi::Object dpi_obj = Napi::Object::New(env);
75
- dpi_obj.Set("diagonal", ddpi);
76
- dpi_obj.Set("horizontal", hdpi);
77
- dpi_obj.Set("vertical", vdpi);
78
- dpi = dpi_obj;
79
- }
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);
93
+
94
+ return display;
95
+ }
96
+
97
+ Napi::Value
98
+ video::_getDisplay (Napi::Env &env, SDL_DisplayID display_id)
99
+ {
100
+ return _packDisplay(env, display_id);
101
+ }
102
+
103
+ Napi::Array
104
+ video::_getDisplays (Napi::Env &env)
105
+ {
106
+ 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());
113
+ }
114
+
115
+ Napi::Array displays = Napi::Array::New(env);
80
116
 
81
- SDL_DisplayOrientation _orientation = SDL_GetDisplayOrientation(i);
82
- Napi::Value orientation = _orientation != SDL_ORIENTATION_UNKNOWN
83
- ? Napi::String::New(env, video::orientations[_orientation])
84
- : env.Null();
85
-
86
- Napi::Object display = Napi::Object::New(env);
87
- display.Set("_index", i);
88
- display.Set("name", name);
89
- display.Set("format", video::formats[(SDL_PixelFormatEnum) mode.format]);
90
- display.Set("frequency", mode.refresh_rate);
91
- display.Set("geometry", geometry);
92
- display.Set("usable", usable);
93
- display.Set("dpi", dpi);
94
- display.Set("orientation", orientation);
95
-
96
- displays.Set(i, display);
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
+ }
97
124
  }
125
+ catch (...) {
126
+ SDL_free(display_ids);
127
+ throw;
128
+ }
129
+ SDL_free(display_ids);
98
130
 
99
131
  return displays;
100
132
  }
133
+
134
+ Napi::Value
135
+ video::getDisplays(const Napi::CallbackInfo &info)
136
+ {
137
+ Napi::Env env = info.Env();
138
+
139
+ return video::_getDisplays(env);
140
+ }
@@ -2,13 +2,16 @@
2
2
  #define _VIDEO_H_
3
3
 
4
4
  #include <napi.h>
5
- #include <SDL.h>
5
+ #include <SDL3/SDL.h>
6
6
  #include <map>
7
7
 
8
8
  namespace video {
9
9
 
10
10
  extern std::map<SDL_DisplayOrientation, std::string> orientations;
11
- extern std::map<SDL_PixelFormatEnum, std::string> formats;
11
+ extern std::map<SDL_PixelFormat, std::string> formats;
12
+
13
+ Napi::Value _getDisplay (Napi::Env &env, SDL_DisplayID display_id);
14
+ Napi::Array _getDisplays (Napi::Env &env);
12
15
 
13
16
  Napi::Value getDisplays(const Napi::CallbackInfo &info);
14
17