@kmamal/sdl 0.9.5 → 0.9.6-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 (55) hide show
  1. package/README.md +34 -15
  2. package/binding.gyp +25 -5
  3. package/package.json +7 -4
  4. package/src/javascript/audio/audio-instance.js +9 -6
  5. package/src/javascript/cleanup.js +1 -1
  6. package/src/javascript/clipboard/index.js +2 -1
  7. package/src/javascript/controller/controller-instance.js +6 -0
  8. package/src/javascript/enums.js +1 -1
  9. package/src/javascript/events/index.js +302 -305
  10. package/src/javascript/index.js +1 -1
  11. package/src/javascript/joystick/joystick-instance.js +7 -0
  12. package/src/javascript/keyboard/index.js +3 -3
  13. package/src/javascript/keyboard/key-mapping.js +14 -6
  14. package/src/javascript/mouse/index.js +13 -12
  15. package/src/javascript/power/index.js +0 -2
  16. package/src/javascript/sensor/sensor-instance.js +1 -5
  17. package/src/javascript/video/window.js +17 -16
  18. package/src/native/audio.cpp +178 -0
  19. package/src/native/audio.h +21 -0
  20. package/src/native/clipboard.cpp +45 -0
  21. package/src/native/clipboard.h +13 -0
  22. package/src/native/cocoa-global.h +8 -0
  23. package/src/native/cocoa-global.mm +8 -0
  24. package/src/native/cocoa-window.h +9 -4
  25. package/src/native/cocoa-window.mm +6 -9
  26. package/src/native/controller.cpp +154 -0
  27. package/src/native/controller.h +17 -0
  28. package/src/native/enums.cpp +477 -0
  29. package/src/native/enums.h +67 -0
  30. package/src/native/events.cpp +339 -0
  31. package/src/native/events.h +15 -0
  32. package/src/native/global.cpp +187 -0
  33. package/src/native/global.h +15 -0
  34. package/src/native/joystick.cpp +388 -0
  35. package/src/native/joystick.h +22 -0
  36. package/src/native/keyboard.cpp +55 -0
  37. package/src/native/keyboard.h +18 -0
  38. package/src/native/module.cpp +98 -0
  39. package/src/native/mouse.cpp +186 -0
  40. package/src/native/mouse.h +21 -0
  41. package/src/native/power.cpp +18 -0
  42. package/src/native/power.h +12 -0
  43. package/src/native/sensor.cpp +117 -0
  44. package/src/native/sensor.h +15 -0
  45. package/src/native/video.cpp +90 -0
  46. package/src/native/video.h +12 -0
  47. package/src/native/window.cpp +755 -0
  48. package/src/native/window.h +29 -0
  49. package/src/types/index.d.ts +7 -4
  50. package/scripts/test.mjs +0 -12
  51. package/src/native/asprintf.c +0 -47
  52. package/src/native/asprintf.h +0 -37
  53. package/src/native/binding.cpp +0 -1680
  54. package/src/native/sdl-helpers.cpp +0 -2617
  55. package/src/native/sdl-helpers.h +0 -182
@@ -0,0 +1,186 @@
1
+ #include "mouse.h"
2
+ #include <SDL.h>
3
+ #include <string>
4
+ #include <sstream>
5
+
6
+ static SDL_Cursor *allocated_cursor = nullptr;
7
+
8
+
9
+ Napi::Value
10
+ mouse::getButton (const Napi::CallbackInfo &info)
11
+ {
12
+ Napi::Env env = info.Env();
13
+
14
+ int button = info[0].As<Napi::Number>().Int32Value();
15
+
16
+ int buttons = SDL_GetMouseState(nullptr, nullptr);
17
+ bool state = buttons & SDL_BUTTON(button);
18
+
19
+ return Napi::Boolean::New(env, state);
20
+ }
21
+
22
+ Napi::Value
23
+ mouse::getPosition (const Napi::CallbackInfo &info)
24
+ {
25
+ Napi::Env env = info.Env();
26
+
27
+ int x, y;
28
+ SDL_GetGlobalMouseState(&x, &y);
29
+
30
+ Napi::Object result = Napi::Object::New(env);
31
+ result.Set("x", Napi::Number::New(env, x));
32
+ result.Set("y", Napi::Number::New(env, y));
33
+
34
+ return result;
35
+ }
36
+
37
+ Napi::Value
38
+ mouse::setPosition (const Napi::CallbackInfo &info)
39
+ {
40
+ Napi::Env env = info.Env();
41
+
42
+ int x = info[0].As<Napi::Number>().Int32Value();
43
+ int y = info[1].As<Napi::Number>().Int32Value();
44
+
45
+ if (SDL_WarpMouseGlobal(x, y) < 0) {
46
+ std::ostringstream message;
47
+ message << "SDL_WarpMouseGlobal(" << x << ", " << y << ") error: " << SDL_GetError();
48
+ SDL_ClearError();
49
+ throw Napi::Error::New(env, message.str());
50
+ }
51
+
52
+ return env.Undefined();
53
+ }
54
+
55
+ Napi::Value
56
+ mouse::setCursor (const Napi::CallbackInfo &info)
57
+ {
58
+ Napi::Env env = info.Env();
59
+
60
+ int cursor_id = info[0].As<Napi::Number>().Int32Value();
61
+
62
+ if (allocated_cursor != nullptr) {
63
+ SDL_FreeCursor(allocated_cursor);
64
+ allocated_cursor = nullptr;
65
+ }
66
+
67
+ SDL_Cursor *cursor = SDL_CreateSystemCursor((SDL_SystemCursor) cursor_id);
68
+ if (cursor == nullptr) {
69
+ std::ostringstream message;
70
+ message << "SDL_CreateSystemCursor(" << cursor_id << ") error: " << SDL_GetError();
71
+ SDL_ClearError();
72
+ throw Napi::Error::New(env, message.str());
73
+ }
74
+
75
+ SDL_SetCursor(cursor);
76
+
77
+ return env.Undefined();
78
+ }
79
+
80
+ Napi::Value
81
+ mouse::resetCursor(const Napi::CallbackInfo &info)
82
+ {
83
+ Napi::Env env = info.Env();
84
+
85
+ if (allocated_cursor != nullptr) {
86
+ SDL_FreeCursor(allocated_cursor);
87
+ allocated_cursor = nullptr;
88
+ }
89
+
90
+ SDL_Cursor *cursor = SDL_GetDefaultCursor();
91
+ if (cursor == nullptr) {
92
+ std::ostringstream message;
93
+ message << "SDL_GetDefaultCursor() error: " << SDL_GetError();
94
+ SDL_ClearError();
95
+ throw Napi::Error::New(env, message.str());
96
+ }
97
+
98
+ SDL_SetCursor(cursor);
99
+
100
+ return env.Undefined();
101
+ }
102
+
103
+ Napi::Value
104
+ mouse::setCursorImage (const Napi::CallbackInfo &info)
105
+ {
106
+ Napi::Env env = info.Env();
107
+
108
+ int w = info[0].As<Napi::Number>().Int32Value();
109
+ int h = info[1].As<Napi::Number>().Int32Value();
110
+ int stride = info[2].As<Napi::Number>().Int32Value();
111
+ unsigned int format = info[3].As<Napi::Number>().Int32Value();
112
+ void *pixels = info[4].As<Napi::Buffer<char>>().Data();
113
+ int x = info[5].As<Napi::Number>().Int32Value();
114
+ int y = info[6].As<Napi::Number>().Int32Value();
115
+
116
+ SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
117
+ if (surface == nullptr) {
118
+ std::ostringstream message;
119
+ message << "SDL_CreateRGBSurfaceWithFormatFrom(" << w << ", " << h << ", " << format << ") error: " << SDL_GetError();
120
+ SDL_ClearError();
121
+ throw Napi::Error::New(env, message.str());
122
+ }
123
+
124
+ if (allocated_cursor != nullptr) {
125
+ SDL_FreeCursor(allocated_cursor);
126
+ allocated_cursor = nullptr;
127
+ }
128
+
129
+ allocated_cursor = SDL_CreateColorCursor(surface, x, y);
130
+ if (allocated_cursor == nullptr) {
131
+ std::ostringstream message;
132
+ message << "SDL_CreateColorCursor(" << x << ", " << y << ") error: " << SDL_GetError();
133
+ SDL_ClearError();
134
+ throw Napi::Error::New(env, message.str());
135
+ }
136
+
137
+ SDL_FreeSurface(surface);
138
+
139
+ SDL_SetCursor(allocated_cursor);
140
+
141
+ return env.Undefined();
142
+ }
143
+
144
+ Napi::Value
145
+ mouse::showCursor (const Napi::CallbackInfo &info)
146
+ {
147
+ Napi::Env env = info.Env();
148
+
149
+ bool should_show = info[0].As<Napi::Boolean>().Value();
150
+
151
+ if (SDL_ShowCursor(should_show ? SDL_ENABLE : SDL_DISABLE) < 0) {
152
+ std::ostringstream message;
153
+ message << "SDL_ShowCursor(" << should_show << ") error: " << SDL_GetError();
154
+ SDL_ClearError();
155
+ throw Napi::Error::New(env, message.str());
156
+ }
157
+
158
+ return env.Undefined();
159
+ }
160
+
161
+ Napi::Value
162
+ mouse::redrawCursor (const Napi::CallbackInfo &info)
163
+ {
164
+ Napi::Env env = info.Env();
165
+
166
+ SDL_SetCursor(nullptr);
167
+
168
+ return env.Undefined();
169
+ }
170
+
171
+ Napi::Value
172
+ mouse::capture (const Napi::CallbackInfo &info)
173
+ {
174
+ Napi::Env env = info.Env();
175
+
176
+ bool should_capture = info[0].As<Napi::Boolean>().Value();
177
+
178
+ if (SDL_CaptureMouse(should_capture ? SDL_TRUE : SDL_FALSE) == -1) {
179
+ std::ostringstream message;
180
+ message << "SDL_CaptureMouse(" << should_capture << ") error: " << SDL_GetError();
181
+ SDL_ClearError();
182
+ throw Napi::Error::New(env, message.str());
183
+ }
184
+
185
+ return env.Undefined();
186
+ }
@@ -0,0 +1,21 @@
1
+ #ifndef _MOUSE_H_
2
+ #define _MOUSE_H_
3
+
4
+ #include <napi.h>
5
+
6
+ namespace mouse {
7
+
8
+ Napi::Value getButton(const Napi::CallbackInfo &info);
9
+ Napi::Value getPosition(const Napi::CallbackInfo &info);
10
+ Napi::Value setPosition(const Napi::CallbackInfo &info);
11
+ Napi::Value getCursor(const Napi::CallbackInfo &info);
12
+ Napi::Value setCursor(const Napi::CallbackInfo &info);
13
+ Napi::Value resetCursor(const Napi::CallbackInfo &info);
14
+ Napi::Value setCursorImage(const Napi::CallbackInfo &info);
15
+ Napi::Value showCursor(const Napi::CallbackInfo &info);
16
+ Napi::Value redrawCursor(const Napi::CallbackInfo &info);
17
+ Napi::Value capture(const Napi::CallbackInfo &info);
18
+
19
+ }; // namespace mouse
20
+
21
+ #endif // _MOUSE_H_
@@ -0,0 +1,18 @@
1
+ #include "power.h"
2
+ #include <SDL.h>
3
+
4
+ Napi::Value
5
+ power::getInfo (const Napi::CallbackInfo &info)
6
+ {
7
+ Napi::Env env = info.Env();
8
+
9
+ int seconds, percent;
10
+ SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
11
+
12
+ Napi::Object result = Napi::Object::New(env);
13
+ result.Set("state", Napi::Number::New(env, (int) state));
14
+ result.Set("seconds", seconds == -1 ? env.Null() : Napi::Number::New(env, seconds));
15
+ result.Set("percent", percent == -1 ? env.Null() : Napi::Number::New(env, percent));
16
+
17
+ return result;
18
+ }
@@ -0,0 +1,12 @@
1
+ #ifndef _POWER_H_
2
+ #define _POWER_H_
3
+
4
+ #include <napi.h>
5
+
6
+ namespace power {
7
+
8
+ Napi::Value getInfo(const Napi::CallbackInfo &info);
9
+
10
+ }; // namespace power
11
+
12
+ #endif // _POWER_H_
@@ -0,0 +1,117 @@
1
+ #include "sensor.h"
2
+ #include <SDL.h>
3
+ #include <string>
4
+ #include <sstream>
5
+
6
+ Napi::Value
7
+ sensor::getDevices (const Napi::CallbackInfo &info)
8
+ {
9
+ Napi::Env env = info.Env();
10
+
11
+ int num_devices = SDL_NumSensors();
12
+ if (num_devices < 0) {
13
+ std::ostringstream message;
14
+ message << "SDL_NumSensors() error: " << SDL_GetError();
15
+ SDL_ClearError();
16
+ throw Napi::Error::New(env, message.str());
17
+ }
18
+
19
+ Napi::Array devices = Napi::Array::New(env, num_devices);
20
+
21
+ for (int i = 0; i < num_devices; i++) {
22
+ int id = SDL_SensorGetDeviceInstanceID(i);
23
+ if (id == -1) {
24
+ std::ostringstream message;
25
+ message << "SDL_SensorGetDeviceInstanceID(" << i << ") error: " << SDL_GetError();
26
+ SDL_ClearError();
27
+ throw Napi::Error::New(env, message.str());
28
+ }
29
+
30
+ int type = SDL_SensorGetDeviceType(i);
31
+ // This function can only error if the index is invalid.
32
+
33
+ const char *name = SDL_SensorGetDeviceName(i);
34
+ // This function can only error if the index is invalid.
35
+
36
+ Napi::Object device = Napi::Object::New(env);
37
+ device.Set("_index", Napi::Number::New(env, i));
38
+ device.Set("id", Napi::Number::New(env, id));
39
+ device.Set("type", Napi::Number::New(env, type));
40
+ device.Set("name", Napi::String::New(env, name));
41
+
42
+ devices.Set(i, device);
43
+ }
44
+
45
+ return devices;
46
+ }
47
+
48
+ Napi::Value
49
+ sensor::open (const Napi::CallbackInfo &info)
50
+ {
51
+ Napi::Env env = info.Env();
52
+
53
+ int index = info[0].As<Napi::Number>().Int32Value();
54
+
55
+ SDL_Sensor *sensor = SDL_SensorOpen(index);
56
+ if (sensor == nullptr) {
57
+ std::ostringstream message;
58
+ message << "SDL_SensorOpen(" << index << ") error: " << SDL_GetError();
59
+ SDL_ClearError();
60
+ throw Napi::Error::New(env, message.str());
61
+ }
62
+
63
+ return env.Undefined();
64
+ }
65
+
66
+ Napi::Value
67
+ sensor::getData (const Napi::CallbackInfo &info)
68
+ {
69
+ Napi::Env env = info.Env();
70
+
71
+ int sensor_id = info[0].As<Napi::Number>().Int32Value();
72
+
73
+ SDL_Sensor *sensor = SDL_SensorFromInstanceID(sensor_id);
74
+ if (sensor == nullptr) {
75
+ std::ostringstream message;
76
+ message << "SDL_SensorFromInstanceID(" << sensor_id << ") error: " << SDL_GetError();
77
+ SDL_ClearError();
78
+ throw Napi::Error::New(env, message.str());
79
+ }
80
+
81
+ Uint64 timestamp;
82
+ float data[3];
83
+ if (SDL_SensorGetDataWithTimestamp(sensor, &timestamp, data, 3) == -1) {
84
+ std::ostringstream message;
85
+ message << "SDL_SensorGetDataWithTimestamp(" << sensor_id << ") error: " << SDL_GetError();
86
+ SDL_ClearError();
87
+ throw Napi::Error::New(env, message.str());
88
+ }
89
+
90
+ Napi::Object result = Napi::Object::New(env);
91
+ result.Set("timestamp", timestamp == 0 ? env.Null() : Napi::Number::New(env, timestamp));
92
+ result.Set("x", Napi::Number::New(env, data[0]));
93
+ result.Set("y", Napi::Number::New(env, data[1]));
94
+ result.Set("z", Napi::Number::New(env, data[2]));
95
+
96
+ return result;
97
+ }
98
+
99
+ Napi::Value
100
+ sensor::close (const Napi::CallbackInfo &info)
101
+ {
102
+ Napi::Env env = info.Env();
103
+
104
+ int sensor_id = info[0].As<Napi::Number>().Int32Value();
105
+
106
+ SDL_Sensor *sensor = SDL_SensorFromInstanceID(sensor_id);
107
+ if (sensor == nullptr) {
108
+ std::ostringstream message;
109
+ message << "SDL_SensorFromInstanceID(" << sensor_id << ") error: " << SDL_GetError();
110
+ SDL_ClearError();
111
+ throw Napi::Error::New(env, message.str());
112
+ }
113
+
114
+ SDL_SensorClose(sensor);
115
+
116
+ return env.Undefined();
117
+ }
@@ -0,0 +1,15 @@
1
+ #ifndef _SENSOR_H_
2
+ #define _SENSOR_H_
3
+
4
+ #include <napi.h>
5
+
6
+ namespace sensor {
7
+
8
+ Napi::Value getDevices(const Napi::CallbackInfo &info);
9
+ Napi::Value open(const Napi::CallbackInfo &info);
10
+ Napi::Value getData(const Napi::CallbackInfo &info);
11
+ Napi::Value close(const Napi::CallbackInfo &info);
12
+
13
+ }; // namespace sensor
14
+
15
+ #endif // _SENSOR_H_
@@ -0,0 +1,90 @@
1
+ #include "video.h"
2
+ #include <SDL.h>
3
+ #include <string>
4
+ #include <sstream>
5
+
6
+ Napi::Value
7
+ video::getDisplays(const Napi::CallbackInfo &info)
8
+ {
9
+ Napi::Env env = info.Env();
10
+
11
+ int num_displays = SDL_GetNumVideoDisplays();
12
+ if (num_displays < 0) {
13
+ std::ostringstream message;
14
+ message << "SDL_GetNumVideoDisplays() error: " << SDL_GetError();
15
+ SDL_ClearError();
16
+ throw Napi::Error::New(env, message.str());
17
+ }
18
+
19
+ Napi::Array displays = Napi::Array::New(env, num_displays);
20
+
21
+ for (int i = 0; i < num_displays; i++) {
22
+ const char *name = SDL_GetDisplayName(i);
23
+ if(name == nullptr) {
24
+ std::ostringstream message;
25
+ message << "SDL_GetDisplayName(" << i << ") error: " << SDL_GetError();
26
+ SDL_ClearError();
27
+ throw Napi::Error::New(env, message.str());
28
+ }
29
+
30
+ SDL_DisplayMode mode;
31
+ if (SDL_GetCurrentDisplayMode(i, &mode) < 0) {
32
+ std::ostringstream message;
33
+ message << "SDL_GetCurrentDisplayMode(" << i << ") error: " << SDL_GetError();
34
+ SDL_ClearError();
35
+ throw Napi::Error::New(env, message.str());
36
+ }
37
+
38
+ SDL_Rect rect;
39
+ if(SDL_GetDisplayBounds(i, &rect) < 0) {
40
+ std::ostringstream message;
41
+ message << "SDL_GetDisplayBounds(" << i << ") error: " << SDL_GetError();
42
+ SDL_ClearError();
43
+ throw Napi::Error::New(env, message.str());
44
+ }
45
+
46
+ Napi::Object geometry = Napi::Object::New(env);
47
+ geometry.Set("x", Napi::Number::New(env, rect.x));
48
+ geometry.Set("y", Napi::Number::New(env, rect.y));
49
+ geometry.Set("width", Napi::Number::New(env, rect.w));
50
+ geometry.Set("height", Napi::Number::New(env, rect.h));
51
+
52
+ if(SDL_GetDisplayUsableBounds(i, &rect) < 0) {
53
+ std::ostringstream message;
54
+ message << "SDL_GetDisplayUsableBounds(" << i << ") error: " << SDL_GetError();
55
+ SDL_ClearError();
56
+ throw Napi::Error::New(env, message.str());
57
+ }
58
+
59
+ Napi::Object usable = Napi::Object::New(env);
60
+ usable.Set("x", Napi::Number::New(env, rect.x));
61
+ usable.Set("y", Napi::Number::New(env, rect.y));
62
+ usable.Set("width", Napi::Number::New(env, rect.w));
63
+ usable.Set("height", Napi::Number::New(env, rect.h));
64
+
65
+ float ddpi, hdpi, vdpi;
66
+ if (SDL_GetDisplayDPI(i, &ddpi, &hdpi, &vdpi) < 0) {
67
+ std::ostringstream message;
68
+ message << "SDL_GetDisplayDPI(" << i << ") error: " << SDL_GetError();
69
+ SDL_ClearError();
70
+ throw Napi::Error::New(env, message.str());
71
+ }
72
+
73
+ Napi::Object dpi = Napi::Object::New(env);
74
+ dpi.Set("diagonal", Napi::Number::New(env, ddpi));
75
+ dpi.Set("horizontal", Napi::Number::New(env, hdpi));
76
+ dpi.Set("vertical", Napi::Number::New(env, vdpi));
77
+
78
+ Napi::Object display = Napi::Object::New(env);
79
+ display.Set("name", Napi::String::New(env, name));
80
+ display.Set("format", Napi::Number::New(env, mode.format));
81
+ display.Set("frequency", Napi::Number::New(env, mode.refresh_rate));
82
+ display.Set("geometry", geometry);
83
+ display.Set("usable", usable);
84
+ display.Set("dpi", dpi);
85
+
86
+ displays.Set(i, display);
87
+ }
88
+
89
+ return displays;
90
+ }
@@ -0,0 +1,12 @@
1
+ #ifndef _VIDEO_H_
2
+ #define _VIDEO_H_
3
+
4
+ #include <napi.h>
5
+
6
+ namespace video {
7
+
8
+ Napi::Value getDisplays(const Napi::CallbackInfo &info);
9
+
10
+ }; // namespace video
11
+
12
+ #endif // _VIDEO_H_