@kmamal/sdl 0.9.6-0 → 0.10.0

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 (48) hide show
  1. package/README.md +79 -90
  2. package/package.json +4 -3
  3. package/scripts/util/common.js +2 -0
  4. package/src/javascript/audio/audio-instance.js +1 -1
  5. package/src/javascript/audio/index.js +1 -0
  6. package/src/javascript/clipboard/index.js +6 -1
  7. package/src/javascript/controller/controller-instance.js +12 -9
  8. package/src/javascript/controller/index.js +5 -2
  9. package/src/javascript/enums.js +0 -8
  10. package/src/javascript/events/events-via-poll.js +3 -3
  11. package/src/javascript/events/index.js +57 -55
  12. package/src/javascript/globals.js +1 -1
  13. package/src/javascript/joystick/device.js +0 -3
  14. package/src/javascript/joystick/index.js +5 -2
  15. package/src/javascript/joystick/joystick-instance.js +23 -13
  16. package/src/javascript/keyboard/index.js +7 -3
  17. package/src/javascript/keyboard/key-mapping.js +2 -2
  18. package/src/javascript/mouse/index.js +2 -2
  19. package/src/javascript/power/index.js +1 -6
  20. package/src/javascript/sensor/sensor-instance.js +1 -1
  21. package/src/javascript/video/index.js +5 -6
  22. package/src/javascript/video/window.js +1 -1
  23. package/src/native/audio.cpp +10 -2
  24. package/src/native/controller.cpp +9 -1
  25. package/src/native/controller.h +5 -0
  26. package/src/native/enums.cpp +3 -141
  27. package/src/native/enums.h +0 -55
  28. package/src/native/events.cpp +115 -63
  29. package/src/native/events.h +55 -0
  30. package/src/native/global.cpp +184 -13
  31. package/src/native/joystick.cpp +20 -10
  32. package/src/native/joystick.h +7 -0
  33. package/src/native/keyboard.cpp +5 -3
  34. package/src/native/power.cpp +6 -1
  35. package/src/native/power.h +5 -0
  36. package/src/native/sensor.cpp +9 -2
  37. package/src/native/sensor.h +6 -0
  38. package/src/native/video.cpp +6 -1
  39. package/src/native/video.h +4 -0
  40. package/src/native/window.cpp +24 -97
  41. package/src/native/window.h +1 -0
  42. package/src/types/index.d.ts +84 -45
  43. package/scripts/install-deps-mac.sh +0 -2
  44. package/scripts/install-deps-raspbian.sh +0 -5
  45. package/scripts/install-deps-ubuntu.sh +0 -5
  46. package/scripts/release-rpi-qemu.sh +0 -116
  47. package/scripts/release.mjs +0 -13
  48. package/scripts/upload-release.mjs +0 -86
@@ -1,41 +1,212 @@
1
1
  #include "global.h"
2
2
  #include "events.h"
3
3
  #include "keyboard.h"
4
+ #include "video.h"
5
+ #include "joystick.h"
6
+ #include "controller.h"
7
+ #include "sensor.h"
8
+ #include "power.h"
4
9
  #include <SDL.h>
5
10
  #include <string>
6
11
  #include <sstream>
12
+ #include <vector>
7
13
 
8
14
  #if defined(__MACOSX__)
9
15
  #include "cocoa-global.h"
10
16
  #endif
11
17
 
12
18
 
13
- SDL_threadID mainThreadId;
19
+ static SDL_threadID mainThreadId;
14
20
 
15
21
  int filterEvents(void*, SDL_Event *event) {
16
- if (SDL_ThreadID() != mainThreadId) { return 1; }
17
-
18
- if (event->type != SDL_WINDOWEVENT) { return 1; }
19
-
20
- int window_event_type = event->window.event;
21
22
  if (true
22
- && window_event_type != SDL_WINDOWEVENT_MOVED
23
- && window_event_type != SDL_WINDOWEVENT_RESIZED
24
- ) { return 1; }
25
-
26
- events::dispatchEvent(*event);
27
-
28
- return 0;
23
+ && SDL_ThreadID() == mainThreadId
24
+ && event->type == SDL_WINDOWEVENT
25
+ && (false
26
+ || event->window.event == SDL_WINDOWEVENT_MOVED
27
+ || event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED
28
+ )
29
+ ) { events::dispatchEvent(*event); }
30
+
31
+ return 1;
29
32
  }
30
33
 
31
34
 
32
35
  const char *global::no_error = nullptr;
33
36
 
37
+
34
38
  Napi::Value
35
39
  global::initialize(const Napi::CallbackInfo &info)
36
40
  {
37
41
  Napi::Env env = info.Env();
38
42
 
43
+
44
+ events::families::DISPLAY = "display";
45
+ events::families::WINDOW = "window";
46
+ events::families::DROP = "drop";
47
+ events::families::KEYBOARD = "keyboard";
48
+ events::families::TEXT = "text";
49
+ events::families::MOUSE = "mouse";
50
+ events::families::JOYSTICK_DEVICE = "joystickDevice";
51
+ events::families::JOYSTICK = "joystick";
52
+ events::families::CONTROLLER = "controller";
53
+ events::families::SENSOR = "sensor";
54
+ events::families::AUDIO_DEVICE = "audioDevice";
55
+ events::families::CLIPBOARD = "clipboard";
56
+
57
+ events::types::DISPLAY_ADD = "displayAdd";
58
+ events::types::DISPLAY_REMOVE = "displayRemove";
59
+ events::types::DISPLAY_ORIENT = "displayOrient";
60
+ events::types::SHOW = "show";
61
+ events::types::HIDE = "hide";
62
+ events::types::EXPOSE = "expose";
63
+ events::types::MOVE = "move";
64
+ events::types::RESIZE = "resize";
65
+ events::types::MINIMIZE = "minimize";
66
+ events::types::MAXIMIZE = "maximize";
67
+ events::types::RESTORE = "restore";
68
+ events::types::FOCUS = "focus";
69
+ events::types::BLUR = "blur";
70
+ events::types::HOVER = "hover";
71
+ events::types::LEAVE = "leave";
72
+ events::types::KEY_DOWN = "keyDown";
73
+ events::types::KEY_UP = "keyUp";
74
+ events::types::TEXT_INPUT = "textInput";
75
+ events::types::MOUSE_MOVE = "mouseMove";
76
+ events::types::MOUSE_BUTTON_DOWN = "mouseButtonDown";
77
+ events::types::MOUSE_BUTTON_UP = "mouseButtonUp";
78
+ events::types::MOUSE_WHEEL = "mouseWheel";
79
+ events::types::DROP_BEGIN = "dropBegin";
80
+ events::types::DROP_COMPLETE = "dropComplete";
81
+ events::types::DROP_FILE = "dropFile";
82
+ events::types::DROP_TEXT = "dropText";
83
+ events::types::CLOSE = "close";
84
+ events::types::DEVICE_ADD = "deviceAdd";
85
+ events::types::DEVICE_REMOVE = "deviceRemove";
86
+ events::types::AXIS_MOTION = "axisMotion";
87
+ events::types::BUTTON_DOWN = "buttonDown";
88
+ events::types::BUTTON_UP = "buttonUp";
89
+ events::types::BALL_MOTION = "ballMotion";
90
+ events::types::HAT_MOTION = "hatMotion";
91
+ events::types::REMAP = "remap";
92
+ events::types::UPDATE = "update";
93
+
94
+ video::formats[SDL_PIXELFORMAT_RGB332] = "rgb332";
95
+ video::formats[SDL_PIXELFORMAT_RGB444] = "rgb444";
96
+ video::formats[SDL_PIXELFORMAT_RGB555] = "rgb555";
97
+ video::formats[SDL_PIXELFORMAT_BGR555] = "bgr555";
98
+ video::formats[SDL_PIXELFORMAT_ARGB4444] = "argb4444";
99
+ video::formats[SDL_PIXELFORMAT_RGBA4444] = "rgba4444";
100
+ video::formats[SDL_PIXELFORMAT_ABGR4444] = "abgr4444";
101
+ video::formats[SDL_PIXELFORMAT_BGRA4444] = "bgra4444";
102
+ video::formats[SDL_PIXELFORMAT_ARGB1555] = "argb1555";
103
+ video::formats[SDL_PIXELFORMAT_RGBA5551] = "rgba5551";
104
+ video::formats[SDL_PIXELFORMAT_ABGR1555] = "abgr1555";
105
+ video::formats[SDL_PIXELFORMAT_BGRA5551] = "bgra5551";
106
+ video::formats[SDL_PIXELFORMAT_RGB565] = "rgb565";
107
+ video::formats[SDL_PIXELFORMAT_BGR565] = "bgr565";
108
+ video::formats[SDL_PIXELFORMAT_RGB24] = "rgb24";
109
+ video::formats[SDL_PIXELFORMAT_BGR24] = "bgr24";
110
+ video::formats[SDL_PIXELFORMAT_RGB888] = "rgb888";
111
+ video::formats[SDL_PIXELFORMAT_RGBX8888] = "rgbx8888";
112
+ video::formats[SDL_PIXELFORMAT_BGR888] = "bgr888";
113
+ video::formats[SDL_PIXELFORMAT_BGRX8888] = "bgrx8888";
114
+ video::formats[SDL_PIXELFORMAT_ARGB8888] = "argb8888";
115
+ video::formats[SDL_PIXELFORMAT_RGBA8888] = "rgba8888";
116
+ video::formats[SDL_PIXELFORMAT_ABGR8888] = "abgr8888";
117
+ video::formats[SDL_PIXELFORMAT_BGRA8888] = "bgra8888";
118
+ video::formats[SDL_PIXELFORMAT_ARGB2101010] = "argb2101010";
119
+ video::formats[SDL_PIXELFORMAT_RGBA32] = "rgba32";
120
+ video::formats[SDL_PIXELFORMAT_ARGB32] = "argb32";
121
+ video::formats[SDL_PIXELFORMAT_BGRA32] = "bgra32";
122
+ video::formats[SDL_PIXELFORMAT_ABGR32] = "abgr32";
123
+ video::formats[SDL_PIXELFORMAT_YV12] = "yv12";
124
+ video::formats[SDL_PIXELFORMAT_IYUV] = "iyuv";
125
+ video::formats[SDL_PIXELFORMAT_YUY2] = "yuy2";
126
+ video::formats[SDL_PIXELFORMAT_UYVY] = "uyvy";
127
+ video::formats[SDL_PIXELFORMAT_YVYU] = "yvyu";
128
+ video::formats[SDL_PIXELFORMAT_NV12] = "nv12";
129
+ video::formats[SDL_PIXELFORMAT_NV21] = "nv21";
130
+
131
+ // joystick::power_levels[SDL_JOYSTICK_POWER_UNKNOWN] = nullptr;
132
+ joystick::power_levels[SDL_JOYSTICK_POWER_EMPTY] = "empty";
133
+ joystick::power_levels[SDL_JOYSTICK_POWER_LOW] = "low";
134
+ joystick::power_levels[SDL_JOYSTICK_POWER_MEDIUM] = "medium";
135
+ joystick::power_levels[SDL_JOYSTICK_POWER_FULL] = "full";
136
+ joystick::power_levels[SDL_JOYSTICK_POWER_WIRED] = "wired";
137
+ joystick::power_levels[SDL_JOYSTICK_POWER_MAX] = "max";
138
+
139
+ // joystick::joystick_types[SDL_JOYSTICK_TYPE_UNKNOWN] = nullptr; // Only possible on error
140
+ joystick::joystick_types[SDL_JOYSTICK_TYPE_GAMECONTROLLER] = "gamecontroller";
141
+ joystick::joystick_types[SDL_JOYSTICK_TYPE_WHEEL] = "wheel";
142
+ joystick::joystick_types[SDL_JOYSTICK_TYPE_ARCADE_STICK] = "arcadestick";
143
+ joystick::joystick_types[SDL_JOYSTICK_TYPE_FLIGHT_STICK] = "flightstick";
144
+ joystick::joystick_types[SDL_JOYSTICK_TYPE_DANCE_PAD] = "dancepad";
145
+ joystick::joystick_types[SDL_JOYSTICK_TYPE_GUITAR] = "guitar";
146
+ joystick::joystick_types[SDL_JOYSTICK_TYPE_DRUM_KIT] = "drumkit";
147
+ joystick::joystick_types[SDL_JOYSTICK_TYPE_ARCADE_PAD] = "arcadepad";
148
+ joystick::joystick_types[SDL_JOYSTICK_TYPE_THROTTLE] = "throttle";
149
+
150
+ joystick::hat_positions[SDL_HAT_CENTERED] = "centered";
151
+ joystick::hat_positions[SDL_HAT_UP] = "up";
152
+ joystick::hat_positions[SDL_HAT_RIGHT] = "right";
153
+ joystick::hat_positions[SDL_HAT_DOWN] = "down";
154
+ joystick::hat_positions[SDL_HAT_LEFT] = "left";
155
+ joystick::hat_positions[SDL_HAT_RIGHTUP] = "rightup";
156
+ joystick::hat_positions[SDL_HAT_RIGHTDOWN] = "rightdown";
157
+ joystick::hat_positions[SDL_HAT_LEFTUP] = "leftup";
158
+ joystick::hat_positions[SDL_HAT_LEFTDOWN] = "leftdown";
159
+
160
+ controller::axes[SDL_CONTROLLER_AXIS_LEFTX] = "leftStickX";
161
+ controller::axes[SDL_CONTROLLER_AXIS_LEFTY] = "leftStickY";
162
+ controller::axes[SDL_CONTROLLER_AXIS_RIGHTX] = "rightStickX";
163
+ controller::axes[SDL_CONTROLLER_AXIS_RIGHTY] = "rightStickY";
164
+ controller::axes[SDL_CONTROLLER_AXIS_TRIGGERLEFT] = "leftTrigger";
165
+ controller::axes[SDL_CONTROLLER_AXIS_TRIGGERRIGHT] = "rightTrigger";
166
+
167
+ controller::buttons[SDL_CONTROLLER_BUTTON_DPAD_LEFT] = "dpadLeft";
168
+ controller::buttons[SDL_CONTROLLER_BUTTON_DPAD_RIGHT] = "dpadRight";
169
+ controller::buttons[SDL_CONTROLLER_BUTTON_DPAD_UP] = "dpadUp";
170
+ controller::buttons[SDL_CONTROLLER_BUTTON_DPAD_DOWN] = "dpadDown";
171
+ controller::buttons[SDL_CONTROLLER_BUTTON_A] = "a";
172
+ controller::buttons[SDL_CONTROLLER_BUTTON_B] = "b";
173
+ controller::buttons[SDL_CONTROLLER_BUTTON_X] = "x";
174
+ controller::buttons[SDL_CONTROLLER_BUTTON_Y] = "y";
175
+ controller::buttons[SDL_CONTROLLER_BUTTON_GUIDE] = "guide";
176
+ controller::buttons[SDL_CONTROLLER_BUTTON_BACK] = "back";
177
+ controller::buttons[SDL_CONTROLLER_BUTTON_START] = "start";
178
+ controller::buttons[SDL_CONTROLLER_BUTTON_LEFTSTICK] = "leftStick";
179
+ controller::buttons[SDL_CONTROLLER_BUTTON_RIGHTSTICK] = "rightStick";
180
+ controller::buttons[SDL_CONTROLLER_BUTTON_LEFTSHOULDER] = "leftShoulder";
181
+ controller::buttons[SDL_CONTROLLER_BUTTON_RIGHTSHOULDER] = "rightShoulder";
182
+ controller::buttons[SDL_CONTROLLER_BUTTON_PADDLE1] = "paddle1";
183
+ controller::buttons[SDL_CONTROLLER_BUTTON_PADDLE2] = "paddle2";
184
+ controller::buttons[SDL_CONTROLLER_BUTTON_PADDLE3] = "paddle3";
185
+ controller::buttons[SDL_CONTROLLER_BUTTON_PADDLE4] = "paddle4";
186
+
187
+ // sensor::types[SDL_SENSOR_UNKNOWN] = nullptr;
188
+ sensor::types[SDL_SENSOR_ACCEL] = "accelerometer";
189
+ sensor::types[SDL_SENSOR_GYRO] = "gyroscope";
190
+ sensor::types[SDL_SENSOR_ACCEL_L] = "accelerometer";
191
+ sensor::types[SDL_SENSOR_GYRO_L] = "gyroscope";
192
+ sensor::types[SDL_SENSOR_ACCEL_R] = "accelerometer";
193
+ sensor::types[SDL_SENSOR_GYRO_R] = "gyroscope";
194
+
195
+ // sensor::sides[SDL_SENSOR_UNKNOWN] = nullptr;
196
+ // sensor::sides[SDL_SENSOR_ACCEL] = nullptr;
197
+ // sensor::sides[SDL_SENSOR_GYRO] = nullptr;
198
+ sensor::sides[SDL_SENSOR_ACCEL_L] = "left";
199
+ sensor::sides[SDL_SENSOR_GYRO_L] = "left";
200
+ sensor::sides[SDL_SENSOR_ACCEL_R] = "right";
201
+ sensor::sides[SDL_SENSOR_GYRO_R] = "right";
202
+
203
+ // power::states[SDL_POWERSTATE_UNKNOWN] = nullptr;
204
+ power::states[SDL_POWERSTATE_NO_BATTERY] = "noBattery";
205
+ power::states[SDL_POWERSTATE_ON_BATTERY] = "battery";
206
+ power::states[SDL_POWERSTATE_CHARGING] = "charging";
207
+ power::states[SDL_POWERSTATE_CHARGED] = "charged";
208
+
209
+
39
210
  SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "0");
40
211
 
41
212
  if (SDL_Init(0
@@ -3,6 +3,12 @@
3
3
  #include <SDL.h>
4
4
  #include <string>
5
5
  #include <sstream>
6
+ #include <map>
7
+
8
+
9
+ std::map<Uint8, std::string> joystick::hat_positions;
10
+ std::map<SDL_JoystickType, std::string> joystick::joystick_types;
11
+ std::map<SDL_JoystickPowerLevel, std::string> joystick::power_levels;
6
12
 
7
13
 
8
14
  double
@@ -33,7 +39,7 @@ joystick::_getDevices (Napi::Env &env)
33
39
  throw Napi::Error::New(env, message.str());
34
40
  }
35
41
 
36
- int type = SDL_JoystickGetDeviceType(i);
42
+ SDL_JoystickType type = SDL_JoystickGetDeviceType(i);
37
43
  // This function can only error if the index is invalid.
38
44
 
39
45
  const char *name = SDL_JoystickNameForIndex(i);
@@ -89,7 +95,7 @@ joystick::_getDevices (Napi::Env &env)
89
95
  Napi::Object device = Napi::Object::New(env);
90
96
  device.Set("_index", Napi::Number::New(env, i));
91
97
  device.Set("id", Napi::Number::New(env, id));
92
- device.Set("type", Napi::Number::New(env, type));
98
+ device.Set("type", joystick::joystick_types[type]);
93
99
  device.Set("name", Napi::String::New(env, name));
94
100
  device.Set("path", Napi::String::New(env, path));
95
101
  device.Set("guid", Napi::String::New(env, guid_string));
@@ -142,7 +148,7 @@ joystick::open (const Napi::CallbackInfo &info)
142
148
  Napi::Value firmware_version = firmware == 0 ? env.Null() : Napi::Number::New(env, firmware);
143
149
 
144
150
  const char *serial = SDL_JoystickGetSerial(joystick);
145
- Napi::Value serial_number = serial == 0 ? env.Null() : Napi::String::New(env, serial);
151
+ Napi::Value serial_number = serial == nullptr ? env.Null() : Napi::String::New(env, serial);
146
152
 
147
153
  SDL_bool has_led = SDL_JoystickHasLED(joystick);
148
154
  SDL_bool has_rumble = SDL_JoystickHasRumble(joystick);
@@ -158,12 +164,16 @@ joystick::open (const Napi::CallbackInfo &info)
158
164
 
159
165
  Napi::Array axes = Napi::Array::New(env, num_axes);
160
166
 
167
+ const char *error = SDL_GetError();
168
+ if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
169
+ SDL_ClearError();
170
+
161
171
  for (int i = 0; i < num_axes; i++) {
162
172
  double value = joystick::mapAxis(SDL_JoystickGetAxis(joystick, i));
163
- const char *error = SDL_GetError();
173
+ error = SDL_GetError();
164
174
  if (error != global::no_error) {
165
175
  std::ostringstream message;
166
- message << "SDL_JoystickGetAxis(" << index << ", " << i << ") error: " << SDL_GetError();
176
+ message << "SDL_JoystickGetAxis(" << index << ", " << i << ") error: " << error;
167
177
  SDL_ClearError();
168
178
  throw Napi::Error::New(env, message.str());
169
179
  }
@@ -222,8 +232,8 @@ joystick::open (const Napi::CallbackInfo &info)
222
232
  Napi::Array hats = Napi::Array::New(env, num_hats);
223
233
 
224
234
  for (int i = 0; i < num_hats; i++) {
225
- int position = SDL_JoystickGetHat(joystick, i);
226
- hats.Set(i, Napi::Number::New(env, position));
235
+ int hat_position = SDL_JoystickGetHat(joystick, i);
236
+ hats.Set(i, joystick::hat_positions[hat_position]);
227
237
  }
228
238
 
229
239
  Napi::Object result = Napi::Object::New(env);
@@ -256,10 +266,10 @@ joystick::getPower (const Napi::CallbackInfo &info)
256
266
  throw Napi::Error::New(env, message.str());
257
267
  }
258
268
 
259
- SDL_JoystickPowerLevel power = SDL_JoystickCurrentPowerLevel(joystick);
260
- if (power == SDL_JOYSTICK_POWER_UNKNOWN) { return env.Null(); }
269
+ SDL_JoystickPowerLevel power_level = SDL_JoystickCurrentPowerLevel(joystick);
270
+ if (power_level == SDL_JOYSTICK_POWER_UNKNOWN) { return env.Null(); }
261
271
 
262
- return Napi::Number::New(env, power);
272
+ return Napi::String::New(env, joystick::power_levels[power_level]);
263
273
  }
264
274
 
265
275
  Napi::Value
@@ -2,9 +2,16 @@
2
2
  #define _JOYSTICK_H_
3
3
 
4
4
  #include <napi.h>
5
+ #include <SDL.h>
6
+ #include <map>
7
+ #include <string>
5
8
 
6
9
  namespace joystick {
7
10
 
11
+ extern std::map<SDL_JoystickType, std::string> joystick_types;
12
+ extern std::map<Uint8, std::string> hat_positions;
13
+ extern std::map<SDL_JoystickPowerLevel, std::string> power_levels;
14
+
8
15
  double mapAxis (int value);
9
16
  Napi::Array _getDevices (Napi::Env &env);
10
17
 
@@ -29,13 +29,15 @@ keyboard::getScancode(const Napi::CallbackInfo &info)
29
29
 
30
30
  SDL_Keycode keycode = SDL_GetKeyFromName(keyname.c_str());
31
31
  if (keycode == SDLK_UNKNOWN) {
32
- std::ostringstream message;
33
- message << "SDL_GetKeyFromName(" << keyname << ") error: " << SDL_GetError();
34
32
  SDL_ClearError();
35
- throw Napi::Error::New(env, message.str());
33
+ return env.Null();
36
34
  }
37
35
 
38
36
  int scancode = SDL_GetScancodeFromKey(keycode);
37
+ if (scancode == 0) {
38
+ SDL_ClearError();
39
+ return env.Null();
40
+ }
39
41
 
40
42
  return Napi::Number::New(env, scancode);
41
43
  }
@@ -1,5 +1,10 @@
1
1
  #include "power.h"
2
2
  #include <SDL.h>
3
+ #include <map>
4
+
5
+
6
+ std::map<SDL_PowerState, std::string> power::states;
7
+
3
8
 
4
9
  Napi::Value
5
10
  power::getInfo (const Napi::CallbackInfo &info)
@@ -10,7 +15,7 @@ power::getInfo (const Napi::CallbackInfo &info)
10
15
  SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
11
16
 
12
17
  Napi::Object result = Napi::Object::New(env);
13
- result.Set("state", Napi::Number::New(env, (int) state));
18
+ result.Set("state", state == SDL_POWERSTATE_UNKNOWN ? env.Null() : Napi::String::New(env, power::states[state]));
14
19
  result.Set("seconds", seconds == -1 ? env.Null() : Napi::Number::New(env, seconds));
15
20
  result.Set("percent", percent == -1 ? env.Null() : Napi::Number::New(env, percent));
16
21
 
@@ -2,9 +2,14 @@
2
2
  #define _POWER_H_
3
3
 
4
4
  #include <napi.h>
5
+ #include <SDL.h>
6
+ #include <map>
7
+ #include <string>
5
8
 
6
9
  namespace power {
7
10
 
11
+ extern std::map<SDL_PowerState, std::string> states;
12
+
8
13
  Napi::Value getInfo(const Napi::CallbackInfo &info);
9
14
 
10
15
  }; // namespace power
@@ -1,8 +1,14 @@
1
1
  #include "sensor.h"
2
2
  #include <SDL.h>
3
+ #include <map>
3
4
  #include <string>
4
5
  #include <sstream>
5
6
 
7
+
8
+ std::map<SDL_SensorType, std::string> sensor::types;
9
+ std::map<SDL_SensorType, std::string> sensor::sides;
10
+
11
+
6
12
  Napi::Value
7
13
  sensor::getDevices (const Napi::CallbackInfo &info)
8
14
  {
@@ -27,7 +33,7 @@ sensor::getDevices (const Napi::CallbackInfo &info)
27
33
  throw Napi::Error::New(env, message.str());
28
34
  }
29
35
 
30
- int type = SDL_SensorGetDeviceType(i);
36
+ SDL_SensorType type = SDL_SensorGetDeviceType(i);
31
37
  // This function can only error if the index is invalid.
32
38
 
33
39
  const char *name = SDL_SensorGetDeviceName(i);
@@ -36,8 +42,9 @@ sensor::getDevices (const Napi::CallbackInfo &info)
36
42
  Napi::Object device = Napi::Object::New(env);
37
43
  device.Set("_index", Napi::Number::New(env, i));
38
44
  device.Set("id", Napi::Number::New(env, id));
39
- device.Set("type", Napi::Number::New(env, type));
40
45
  device.Set("name", Napi::String::New(env, name));
46
+ device.Set("type", type == SDL_SENSOR_UNKNOWN ? env.Null() : Napi::String::New(env, sensor::types[type]));
47
+ device.Set("side", type == SDL_SENSOR_UNKNOWN || type == SDL_SENSOR_ACCEL || type == SDL_SENSOR_GYRO ? env.Null() : Napi::String::New(env, sensor::sides[type]));
41
48
 
42
49
  devices.Set(i, device);
43
50
  }
@@ -2,9 +2,15 @@
2
2
  #define _SENSOR_H_
3
3
 
4
4
  #include <napi.h>
5
+ #include <SDL.h>
6
+ #include <map>
7
+ #include <string>
5
8
 
6
9
  namespace sensor {
7
10
 
11
+ extern std::map<SDL_SensorType, std::string> types;
12
+ extern std::map<SDL_SensorType, std::string> sides;
13
+
8
14
  Napi::Value getDevices(const Napi::CallbackInfo &info);
9
15
  Napi::Value open(const Napi::CallbackInfo &info);
10
16
  Napi::Value getData(const Napi::CallbackInfo &info);
@@ -2,6 +2,11 @@
2
2
  #include <SDL.h>
3
3
  #include <string>
4
4
  #include <sstream>
5
+ #include <map>
6
+
7
+
8
+ std::map<SDL_PixelFormatEnum, std::string> video::formats;
9
+
5
10
 
6
11
  Napi::Value
7
12
  video::getDisplays(const Napi::CallbackInfo &info)
@@ -77,7 +82,7 @@ video::getDisplays(const Napi::CallbackInfo &info)
77
82
 
78
83
  Napi::Object display = Napi::Object::New(env);
79
84
  display.Set("name", Napi::String::New(env, name));
80
- display.Set("format", Napi::Number::New(env, mode.format));
85
+ display.Set("format", video::formats[(SDL_PixelFormatEnum) mode.format]);
81
86
  display.Set("frequency", Napi::Number::New(env, mode.refresh_rate));
82
87
  display.Set("geometry", geometry);
83
88
  display.Set("usable", usable);
@@ -2,9 +2,13 @@
2
2
  #define _VIDEO_H_
3
3
 
4
4
  #include <napi.h>
5
+ #include <SDL.h>
6
+ #include <map>
5
7
 
6
8
  namespace video {
7
9
 
10
+ extern std::map<SDL_PixelFormatEnum, std::string> formats;
11
+
8
12
  Napi::Value getDisplays(const Napi::CallbackInfo &info);
9
13
 
10
14
  }; // namespace video
@@ -29,46 +29,6 @@
29
29
  #endif
30
30
 
31
31
 
32
- void
33
- updateTexture(
34
- Napi::Env &env,
35
- SDL_Window *window,
36
- SDL_Renderer *renderer,
37
- int pixel_width, int pixel_height,
38
- SDL_Texture **texture_dst
39
- ) {
40
- int window_id = SDL_GetWindowID(window);
41
- // Not likely to fail.
42
-
43
- int format = SDL_GetWindowPixelFormat(window);
44
- if (format == SDL_PIXELFORMAT_UNKNOWN) {
45
- std::ostringstream message;
46
- message << "SDL_GetWindowPixelFormat(" << window_id << ") error: " << SDL_GetError();
47
- SDL_ClearError();
48
- throw Napi::Error::New(env, message.str());
49
- }
50
-
51
- SDL_Texture *texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, pixel_width, pixel_height);
52
- if (texture == nullptr) {
53
- std::ostringstream message;
54
- message << "SDL_CreateTexture(" << window_id << ") error: " << SDL_GetError();
55
- SDL_ClearError();
56
- throw Napi::Error::New(env, message.str());
57
- }
58
-
59
- if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE) < 0) {
60
- std::ostringstream message;
61
- message << "SDL_SetTextureBlendMode(" << window_id << ") error: " << SDL_GetError();
62
- SDL_ClearError();
63
- throw Napi::Error::New(env, message.str());
64
- }
65
-
66
- SDL_Texture *old_texture = (SDL_Texture *) SDL_SetWindowData(window, "texture", texture);
67
- if (old_texture != nullptr) { SDL_DestroyTexture(old_texture); }
68
-
69
- if (texture_dst != nullptr) { *texture_dst = texture; }
70
- }
71
-
72
32
  void
73
33
  updateRenderer(
74
34
  Napi::Env &env,
@@ -82,14 +42,24 @@ updateRenderer(
82
42
  SDL_Renderer *old_renderer = SDL_GetRenderer(window);
83
43
  if (old_renderer != nullptr) {
84
44
  SDL_DestroyRenderer(old_renderer);
85
- SDL_SetWindowData(window, "texture", nullptr);
86
45
  }
87
46
 
88
- int renderer_flags = 0
89
- | (*is_accelerated ? SDL_RENDERER_ACCELERATED : SDL_RENDERER_SOFTWARE)
90
- | (*is_accelerated && *is_vsync ? SDL_RENDERER_PRESENTVSYNC : 0);
47
+ SDL_Renderer *renderer;
48
+ for (int i = 0; i < 4; i++) {
49
+ int renderer_flags = 0
50
+ | (*is_accelerated ? SDL_RENDERER_ACCELERATED : SDL_RENDERER_SOFTWARE)
51
+ | (*is_vsync ? SDL_RENDERER_PRESENTVSYNC : 0);
52
+
53
+ renderer = SDL_CreateRenderer(window, -1, renderer_flags);
54
+ if (renderer != nullptr) {
55
+ SDL_ClearError();
56
+ break;
57
+ }
58
+
59
+ *is_vsync = !*is_vsync;
60
+ if (i % 2) { *is_accelerated = !*is_accelerated; }
61
+ }
91
62
 
92
- SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, renderer_flags);
93
63
  if (renderer == nullptr) {
94
64
  std::ostringstream message;
95
65
  message << "SDL_CreateRenderer(" << window_id << ", " << is_accelerated << ", " << is_vsync << ") error: " << SDL_GetError();
@@ -107,11 +77,6 @@ updateRenderer(
107
77
 
108
78
  *is_accelerated = info.flags & SDL_RENDERER_ACCELERATED;
109
79
  *is_vsync = info.flags & SDL_RENDERER_PRESENTVSYNC;
110
-
111
- int pixel_width, pixel_height;
112
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
113
-
114
- updateTexture(env, window, renderer, pixel_width, pixel_height, nullptr);
115
80
  }
116
81
 
117
82
 
@@ -581,73 +546,38 @@ window::render (const Napi::CallbackInfo &info)
581
546
  throw Napi::Error::New(env, message.str());
582
547
  }
583
548
 
584
- SDL_Texture *texture = (SDL_Texture *) SDL_GetWindowData(window, "texture");
585
- if (texture == nullptr) {
586
- std::ostringstream message;
587
- message << "SDL_GetWindowData(" << window_id << ") error: " << SDL_GetError();
588
- SDL_ClearError();
589
- throw Napi::Error::New(env, message.str());
590
- }
591
-
592
- unsigned int texture_format;
593
- int texture_width, texture_height;
594
- if (SDL_QueryTexture(texture, &texture_format, nullptr, &texture_width, &texture_height) < 0) {
595
- std::ostringstream message;
596
- message << "SDL_QueryTexture(" << window_id << ") error: " << SDL_GetError();
597
- SDL_ClearError();
598
- throw Napi::Error::New(env, message.str());
599
- }
600
-
601
- int pixel_width, pixel_height;
602
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
603
-
604
- if (pixel_width != texture_width || pixel_height != texture_height) {
605
- updateTexture(env, window, renderer, pixel_width, pixel_height, &texture);
606
- texture_width = pixel_width;
607
- texture_height = pixel_height;
608
- }
609
-
610
- SDL_Surface *src_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
611
- if (src_surface == nullptr) {
549
+ SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
550
+ if (surface == nullptr) {
612
551
  std::ostringstream message;
613
552
  message << "SDL_CreateRGBSurfaceWithFormatFrom(" << window_id << ", " << w << ", " << h << ", " << stride << ", " << format << ") error: " << SDL_GetError();
614
553
  SDL_ClearError();
615
554
  throw Napi::Error::New(env, message.str());
616
555
  }
617
556
 
618
- if (SDL_SetSurfaceBlendMode(src_surface, SDL_BLENDMODE_NONE) < 0) {
557
+ if (SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE) < 0) {
619
558
  std::ostringstream message;
620
559
  message << "SDL_SetSurfaceBlendMode(" << window_id << ") error: " << SDL_GetError();
621
560
  SDL_ClearError();
622
561
  throw Napi::Error::New(env, message.str());
623
562
  }
624
563
 
625
- void *texture_pixels;
626
- int texture_stride;
627
- if (SDL_LockTexture(texture, nullptr, &texture_pixels, &texture_stride) < 0) {
564
+ SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
565
+ if (texture == nullptr) {
628
566
  std::ostringstream message;
629
- message << "SDL_LockTexture(" << window_id << ", " << texture_width << ", " << texture_height << ", " << texture_stride << ", " << texture_format << ") error: " << SDL_GetError();
567
+ message << "SDL_CreateTextureFromSurface(" << window_id << ") error: " << SDL_GetError();
630
568
  SDL_ClearError();
631
569
  throw Napi::Error::New(env, message.str());
632
570
  }
633
571
 
634
- SDL_Surface *dst_surface = SDL_CreateRGBSurfaceWithFormatFrom(texture_pixels, texture_width, texture_height, SDL_BITSPERPIXEL(texture_format), texture_stride, texture_format);
635
- if (dst_surface == nullptr) {
636
- std::ostringstream message;
637
- message << "SDL_CreateRGBSurfaceWithFormatFrom(" << window_id << ", " << texture_width << ", " << texture_height << ", " << texture_stride << ", " << texture_format << ") error: " << SDL_GetError();
638
- SDL_ClearError();
639
- throw Napi::Error::New(env, message.str());
640
- }
572
+ SDL_FreeSurface(surface);
641
573
 
642
- if (SDL_BlitScaled(src_surface, NULL, dst_surface, NULL) < 0) {
574
+ if (SDL_RenderClear(renderer) < 0) {
643
575
  std::ostringstream message;
644
- message << "SDL_BlitSurface(" << window_id << ") error: " << SDL_GetError();
576
+ message << "SDL_RenderClear(" << window_id << ") error: " << SDL_GetError();
645
577
  SDL_ClearError();
646
578
  throw Napi::Error::New(env, message.str());
647
579
  }
648
580
 
649
- SDL_UnlockTexture(texture);
650
-
651
581
  if (SDL_RenderCopy(renderer, texture, nullptr, nullptr) < 0) {
652
582
  std::ostringstream message;
653
583
  message << "SDL_RenderCopy(" << window_id << ") error: " << SDL_GetError();
@@ -657,9 +587,6 @@ window::render (const Napi::CallbackInfo &info)
657
587
 
658
588
  SDL_RenderPresent(renderer);
659
589
 
660
- SDL_FreeSurface(src_surface);
661
- SDL_FreeSurface(dst_surface);
662
-
663
590
  return env.Undefined();
664
591
  }
665
592
 
@@ -2,6 +2,7 @@
2
2
  #define _WINDOW_H_
3
3
 
4
4
  #include <napi.h>
5
+ #include <SDL.h>
5
6
 
6
7
  namespace window {
7
8