@kmamal/sdl 1.0.0-5 → 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.
- package/CHANGELOG.md +12 -6
- package/binding.gyp +3 -0
- package/package.json +5 -12
- package/scripts/util/common.js +2 -2
- package/src/javascript/audio/audio-playback-stream.js +9 -10
- package/src/javascript/audio/audio-recording-stream.js +9 -8
- package/src/javascript/audio/audio-stream.js +100 -44
- package/src/javascript/audio/conversion.js +41 -0
- package/src/javascript/audio/devices.js +11 -0
- package/src/javascript/audio/events.js +65 -0
- package/src/javascript/audio/format-helpers.js +8 -4
- package/src/javascript/audio/index.js +25 -20
- package/src/javascript/audio/prevent-exit.js +12 -9
- package/src/javascript/audio/validate.js +9 -0
- package/src/javascript/cleanup.js +9 -6
- package/src/javascript/clipboard/index.js +8 -3
- package/src/javascript/device-instance.js +61 -0
- package/src/javascript/events/events-via-poll.js +33 -27
- package/src/javascript/events/index.js +75 -482
- package/src/javascript/events/try.js +22 -0
- package/src/javascript/gamepad/{device.js → devices.js} +4 -5
- package/src/javascript/gamepad/events.js +66 -0
- package/src/javascript/gamepad/gamepad-instance.js +12 -160
- package/src/javascript/gamepad/index.js +15 -16
- package/src/javascript/globals.js +10 -1
- package/src/javascript/index.js +2 -5
- package/src/javascript/joystick/devices.js +43 -0
- package/src/javascript/joystick/events.js +129 -0
- package/src/javascript/joystick/index.js +9 -18
- package/src/javascript/joystick/joystick-device-instance.js +175 -0
- package/src/javascript/joystick/joystick-instance.js +12 -158
- package/src/javascript/keyboard/index.js +20 -10
- package/src/javascript/keyboard/key-mapping.js +46 -27
- package/src/javascript/mouse/index.js +36 -29
- package/src/javascript/sensor/events.js +21 -0
- package/src/javascript/sensor/index.js +13 -2
- package/src/javascript/sensor/sensor-instance.js +7 -47
- package/src/javascript/subsystems.js +56 -0
- package/src/javascript/touch/index.js +9 -4
- package/src/javascript/validate.js +114 -0
- package/src/javascript/video/conversion.js +63 -0
- package/src/javascript/video/events.js +179 -0
- package/src/javascript/video/format-helpers.js +10 -8
- package/src/javascript/video/index.js +28 -14
- package/src/javascript/video/validate.js +45 -0
- package/src/javascript/video/window.js +144 -161
- package/src/native/audio.cpp +457 -151
- package/src/native/audio.h +12 -6
- package/src/native/clipboard.cpp +8 -37
- package/src/native/enums.cpp +2 -0
- package/src/native/events.cpp +416 -354
- package/src/native/events.h +67 -65
- package/src/native/gamepad.cpp +86 -103
- package/src/native/gamepad.h +7 -8
- package/src/native/global.cpp +190 -85
- package/src/native/global.h +48 -2
- package/src/native/joystick.cpp +180 -274
- package/src/native/joystick.h +12 -6
- package/src/native/keyboard.cpp +60 -22
- package/src/native/keyboard.h +2 -1
- package/src/native/module.cpp +20 -3
- package/src/native/mouse.cpp +27 -58
- package/src/native/packed.cpp +106 -0
- package/src/native/packed.h +91 -0
- package/src/native/power.cpp +7 -1
- package/src/native/power.h +2 -2
- package/src/native/sensor.cpp +61 -69
- package/src/native/sensor.h +3 -3
- package/src/native/testing.cpp +131 -0
- package/src/native/testing.h +16 -0
- package/src/native/threads.cpp +300 -0
- package/src/native/threads.h +36 -0
- package/src/native/touch.cpp +32 -43
- package/src/native/touch.h +2 -1
- package/src/native/video.cpp +145 -95
- package/src/native/video.h +6 -3
- package/src/native/window.cpp +155 -218
- package/src/native/window.h +21 -20
- package/src/types/index.d.ts +141 -69
- package/src/javascript/audio/device.js +0 -37
- package/src/javascript/joystick/device.js +0 -13
- package/src/javascript/joystick/shared.js +0 -90
- package/src/javascript/worker.js +0 -88
- package/src/types/worker.d.ts +0 -71
package/src/native/sensor.cpp
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
#include "sensor.h"
|
|
2
|
+
#include "global.h"
|
|
3
|
+
#include "threads.h"
|
|
2
4
|
#include <SDL3/SDL.h>
|
|
3
5
|
#include <map>
|
|
4
6
|
#include <string>
|
|
5
|
-
#include <sstream>
|
|
6
7
|
|
|
7
8
|
|
|
8
|
-
std::map<SDL_SensorType,
|
|
9
|
-
std::map<SDL_SensorType,
|
|
9
|
+
std::map<SDL_SensorType, packed::Str> sensor::types;
|
|
10
|
+
std::map<SDL_SensorType, packed::Str> sensor::sides;
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
Napi::Value
|
|
@@ -14,56 +15,49 @@ sensor::getDevices (const Napi::CallbackInfo &info)
|
|
|
14
15
|
{
|
|
15
16
|
Napi::Env env = info.Env();
|
|
16
17
|
|
|
18
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
19
|
+
global::assertReady(env);
|
|
20
|
+
|
|
17
21
|
int num_devices;
|
|
18
|
-
SDL_SensorID
|
|
19
|
-
if (sensor_ids
|
|
20
|
-
|
|
21
|
-
message << "SDL_GetSensors() error: " << SDL_GetError();
|
|
22
|
-
SDL_ClearError();
|
|
23
|
-
throw Napi::Error::New(env, message.str());
|
|
22
|
+
global::SdlArray<SDL_SensorID> sensor_ids(SDL_GetSensors(&num_devices));
|
|
23
|
+
if (!sensor_ids) {
|
|
24
|
+
throw global::sdlError(env, "SDL_GetSensors");
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
Napi::Array devices = Napi::Array::New(env, num_devices);
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
Napi::
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
Napi::
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
name = Napi::String::New(env, _name);
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
SDL_ClearError();
|
|
50
|
-
name = env.Null();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
Napi::Object device = Napi::Object::New(env);
|
|
54
|
-
device.Set("id", id);
|
|
55
|
-
device.Set("name", name);
|
|
56
|
-
device.Set("type", type);
|
|
57
|
-
device.Set("side", side);
|
|
58
|
-
|
|
59
|
-
devices.Set(i, device);
|
|
29
|
+
for (int i = 0; i < num_devices; i++) {
|
|
30
|
+
SDL_SensorID id = sensor_ids[i];
|
|
31
|
+
|
|
32
|
+
SDL_SensorType _type = SDL_GetSensorTypeForID(id);
|
|
33
|
+
if (_type == SDL_SENSOR_INVALID) { SDL_ClearError(); }
|
|
34
|
+
auto type_entry = sensor::types.find(_type);
|
|
35
|
+
Napi::Value type = type_entry != sensor::types.end()
|
|
36
|
+
? Napi::String::New(env, type_entry->second)
|
|
37
|
+
: env.Null();
|
|
38
|
+
auto side_entry = sensor::sides.find(_type);
|
|
39
|
+
Napi::Value side = side_entry != sensor::sides.end()
|
|
40
|
+
? Napi::String::New(env, side_entry->second)
|
|
41
|
+
: env.Null();
|
|
42
|
+
|
|
43
|
+
const char *_name = SDL_GetSensorNameForID(id);
|
|
44
|
+
Napi::Value name;
|
|
45
|
+
if (_name != nullptr) {
|
|
46
|
+
name = Napi::String::New(env, _name);
|
|
60
47
|
}
|
|
48
|
+
else {
|
|
49
|
+
SDL_ClearError();
|
|
50
|
+
name = env.Null();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
Napi::Object device = Napi::Object::New(env);
|
|
54
|
+
device.Set("id", id);
|
|
55
|
+
device.Set("name", name);
|
|
56
|
+
device.Set("type", type);
|
|
57
|
+
device.Set("side", side);
|
|
58
|
+
|
|
59
|
+
devices.Set(i, device);
|
|
61
60
|
}
|
|
62
|
-
catch (...) {
|
|
63
|
-
SDL_free(sensor_ids);
|
|
64
|
-
throw;
|
|
65
|
-
}
|
|
66
|
-
SDL_free(sensor_ids);
|
|
67
61
|
|
|
68
62
|
return devices;
|
|
69
63
|
}
|
|
@@ -73,15 +67,18 @@ sensor::open (const Napi::CallbackInfo &info)
|
|
|
73
67
|
{
|
|
74
68
|
Napi::Env env = info.Env();
|
|
75
69
|
|
|
76
|
-
|
|
70
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
71
|
+
global::assertReady(env);
|
|
72
|
+
|
|
73
|
+
SDL_SensorID id = info[0].As<Napi::Number>().Uint32Value();
|
|
77
74
|
|
|
78
75
|
SDL_Sensor *sensor = SDL_OpenSensor(id);
|
|
79
76
|
if (sensor == nullptr) {
|
|
80
|
-
|
|
81
|
-
message << "SDL_OpenSensor(" << id << ") error: " << SDL_GetError();
|
|
82
|
-
SDL_ClearError();
|
|
83
|
-
throw Napi::Error::New(env, message.str());
|
|
77
|
+
throw global::sdlError(env, "SDL_OpenSensor", id);
|
|
84
78
|
}
|
|
79
|
+
SDL_ClearError();
|
|
80
|
+
|
|
81
|
+
threads::trackOpen(env, threads::SENSOR, id);
|
|
85
82
|
|
|
86
83
|
return env.Undefined();
|
|
87
84
|
}
|
|
@@ -91,21 +88,16 @@ sensor::getData (const Napi::CallbackInfo &info)
|
|
|
91
88
|
{
|
|
92
89
|
Napi::Env env = info.Env();
|
|
93
90
|
|
|
94
|
-
|
|
91
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
92
|
+
global::assertReady(env);
|
|
95
93
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
message << "SDL_GetSensorFromID(" << sensor_id << ") error: invalid sensor id";
|
|
100
|
-
throw Napi::Error::New(env, message.str());
|
|
101
|
-
}
|
|
94
|
+
SDL_SensorID sensor_id = info[0].As<Napi::Number>().Uint32Value();
|
|
95
|
+
|
|
96
|
+
SDL_Sensor *sensor = global::fromIdOrThrow(env, SDL_GetSensorFromID(sensor_id), "SDL_GetSensorFromID", "sensor", sensor_id);
|
|
102
97
|
|
|
103
98
|
float data[3];
|
|
104
99
|
if (!SDL_GetSensorData(sensor, data, 3)) {
|
|
105
|
-
|
|
106
|
-
message << "SDL_GetSensorData(" << sensor_id << ") error: " << SDL_GetError();
|
|
107
|
-
SDL_ClearError();
|
|
108
|
-
throw Napi::Error::New(env, message.str());
|
|
100
|
+
throw global::sdlError(env, "SDL_GetSensorData", sensor_id);
|
|
109
101
|
}
|
|
110
102
|
|
|
111
103
|
Napi::Object result = Napi::Object::New(env);
|
|
@@ -121,14 +113,14 @@ sensor::close (const Napi::CallbackInfo &info)
|
|
|
121
113
|
{
|
|
122
114
|
Napi::Env env = info.Env();
|
|
123
115
|
|
|
124
|
-
|
|
116
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
117
|
+
global::assertReady(env);
|
|
125
118
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
119
|
+
SDL_SensorID sensor_id = info[0].As<Napi::Number>().Uint32Value();
|
|
120
|
+
|
|
121
|
+
threads::trackClose(env, threads::SENSOR, sensor_id);
|
|
122
|
+
|
|
123
|
+
SDL_Sensor *sensor = global::fromIdOrThrow(env, SDL_GetSensorFromID(sensor_id), "SDL_GetSensorFromID", "sensor", sensor_id);
|
|
132
124
|
|
|
133
125
|
SDL_CloseSensor(sensor);
|
|
134
126
|
|
package/src/native/sensor.h
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
#define _SENSOR_H_
|
|
3
3
|
|
|
4
4
|
#include <napi.h>
|
|
5
|
+
#include "packed.h"
|
|
5
6
|
#include <SDL3/SDL.h>
|
|
6
7
|
#include <map>
|
|
7
|
-
#include <string>
|
|
8
8
|
|
|
9
9
|
namespace sensor {
|
|
10
10
|
|
|
11
|
-
extern std::map<SDL_SensorType,
|
|
12
|
-
extern std::map<SDL_SensorType,
|
|
11
|
+
extern std::map<SDL_SensorType, packed::Str> types;
|
|
12
|
+
extern std::map<SDL_SensorType, packed::Str> sides;
|
|
13
13
|
|
|
14
14
|
Napi::Value getDevices(const Napi::CallbackInfo &info);
|
|
15
15
|
Napi::Value open(const Napi::CallbackInfo &info);
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
#include "testing.h"
|
|
2
|
+
#include "global.h"
|
|
3
|
+
#include <SDL3/SDL.h>
|
|
4
|
+
#include <cmath>
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
static SDL_Joystick *
|
|
8
|
+
_getVirtual (Napi::Env &env, SDL_JoystickID id)
|
|
9
|
+
{
|
|
10
|
+
SDL_Joystick *joystick = SDL_GetJoystickFromID(id);
|
|
11
|
+
if (joystick == nullptr || !SDL_IsJoystickVirtual(id)) {
|
|
12
|
+
SDL_ClearError();
|
|
13
|
+
throw Napi::Error::New(env, "invalid virtual joystick id");
|
|
14
|
+
}
|
|
15
|
+
return joystick;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
Napi::Value
|
|
19
|
+
testing::attachVirtualJoystick (const Napi::CallbackInfo &info)
|
|
20
|
+
{
|
|
21
|
+
Napi::Env env = info.Env();
|
|
22
|
+
|
|
23
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
24
|
+
global::assertReady(env);
|
|
25
|
+
|
|
26
|
+
bool is_gamepad = info[0].As<Napi::Boolean>().Value();
|
|
27
|
+
|
|
28
|
+
SDL_VirtualJoystickDesc desc;
|
|
29
|
+
SDL_INIT_INTERFACE(&desc);
|
|
30
|
+
desc.type = is_gamepad ? SDL_JOYSTICK_TYPE_GAMEPAD : SDL_JOYSTICK_TYPE_UNKNOWN;
|
|
31
|
+
desc.naxes = is_gamepad ? SDL_GAMEPAD_AXIS_COUNT : 2;
|
|
32
|
+
desc.nbuttons = is_gamepad ? SDL_GAMEPAD_BUTTON_COUNT : 2;
|
|
33
|
+
desc.name = is_gamepad ? "Virtual Gamepad" : "Virtual Joystick";
|
|
34
|
+
|
|
35
|
+
SDL_JoystickID id = SDL_AttachVirtualJoystick(&desc);
|
|
36
|
+
if (id == 0) { throw global::sdlError(env, "SDL_AttachVirtualJoystick"); }
|
|
37
|
+
|
|
38
|
+
SDL_Joystick *joystick = SDL_OpenJoystick(id);
|
|
39
|
+
if (joystick == nullptr) {
|
|
40
|
+
SDL_DetachVirtualJoystick(id);
|
|
41
|
+
throw global::sdlError(env, "SDL_OpenJoystick");
|
|
42
|
+
}
|
|
43
|
+
SDL_ClearError();
|
|
44
|
+
|
|
45
|
+
return Napi::Number::New(env, id);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
Napi::Value
|
|
49
|
+
testing::setVirtualJoystickAxis (const Napi::CallbackInfo &info)
|
|
50
|
+
{
|
|
51
|
+
Napi::Env env = info.Env();
|
|
52
|
+
|
|
53
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
54
|
+
global::assertReady(env);
|
|
55
|
+
|
|
56
|
+
SDL_JoystickID id = info[0].As<Napi::Number>().Uint32Value();
|
|
57
|
+
int axis = info[1].As<Napi::Number>().Int32Value();
|
|
58
|
+
double value = info[2].As<Napi::Number>().DoubleValue();
|
|
59
|
+
if (value != std::trunc(value) || value < SDL_JOYSTICK_AXIS_MIN || value > SDL_JOYSTICK_AXIS_MAX) {
|
|
60
|
+
throw Napi::Error::New(env, "value must be a signed 16-bit integer");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!SDL_SetJoystickVirtualAxis(_getVirtual(env, id), axis, (Sint16) value)) {
|
|
64
|
+
throw global::sdlError(env, "SDL_SetJoystickVirtualAxis");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return env.Undefined();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
Napi::Value
|
|
71
|
+
testing::setVirtualJoystickButton (const Napi::CallbackInfo &info)
|
|
72
|
+
{
|
|
73
|
+
Napi::Env env = info.Env();
|
|
74
|
+
|
|
75
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
76
|
+
global::assertReady(env);
|
|
77
|
+
|
|
78
|
+
SDL_JoystickID id = info[0].As<Napi::Number>().Uint32Value();
|
|
79
|
+
int button = info[1].As<Napi::Number>().Int32Value();
|
|
80
|
+
bool down = info[2].As<Napi::Boolean>().Value();
|
|
81
|
+
|
|
82
|
+
if (!SDL_SetJoystickVirtualButton(_getVirtual(env, id), button, down)) {
|
|
83
|
+
throw global::sdlError(env, "SDL_SetJoystickVirtualButton");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return env.Undefined();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
Napi::Value
|
|
90
|
+
testing::detachVirtualJoystick (const Napi::CallbackInfo &info)
|
|
91
|
+
{
|
|
92
|
+
Napi::Env env = info.Env();
|
|
93
|
+
|
|
94
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
95
|
+
global::assertReady(env);
|
|
96
|
+
|
|
97
|
+
SDL_JoystickID id = info[0].As<Napi::Number>().Uint32Value();
|
|
98
|
+
|
|
99
|
+
SDL_CloseJoystick(_getVirtual(env, id));
|
|
100
|
+
|
|
101
|
+
// Can't fail once _getVirtual() has confirmed the id is an attached virtual joystick, so closing first is safe.
|
|
102
|
+
if (!SDL_DetachVirtualJoystick(id)) { throw global::sdlError(env, "SDL_DetachVirtualJoystick"); }
|
|
103
|
+
|
|
104
|
+
return env.Undefined();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
Napi::Value
|
|
108
|
+
testing::pushAudioDeviceEvent (const Napi::CallbackInfo &info)
|
|
109
|
+
{
|
|
110
|
+
Napi::Env env = info.Env();
|
|
111
|
+
|
|
112
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
113
|
+
global::assertReady(env);
|
|
114
|
+
|
|
115
|
+
std::string type = info[0].As<Napi::String>().Utf8Value();
|
|
116
|
+
SDL_AudioDeviceID id = info[1].As<Napi::Number>().Uint32Value();
|
|
117
|
+
bool is_recording = info[2].As<Napi::Boolean>().Value();
|
|
118
|
+
|
|
119
|
+
SDL_Event event;
|
|
120
|
+
SDL_zero(event);
|
|
121
|
+
if (type == "add") { event.type = SDL_EVENT_AUDIO_DEVICE_ADDED; }
|
|
122
|
+
else if (type == "remove") { event.type = SDL_EVENT_AUDIO_DEVICE_REMOVED; }
|
|
123
|
+
else if (type == "format") { event.type = SDL_EVENT_AUDIO_DEVICE_FORMAT_CHANGED; }
|
|
124
|
+
else { throw Napi::Error::New(env, "invalid audio device event type"); }
|
|
125
|
+
event.adevice.which = id;
|
|
126
|
+
event.adevice.recording = is_recording;
|
|
127
|
+
|
|
128
|
+
if (!SDL_PushEvent(&event)) { throw global::sdlError(env, "SDL_PushEvent"); }
|
|
129
|
+
|
|
130
|
+
return env.Undefined();
|
|
131
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#ifndef _TESTING_H_
|
|
2
|
+
#define _TESTING_H_
|
|
3
|
+
|
|
4
|
+
#include <napi.h>
|
|
5
|
+
|
|
6
|
+
namespace testing {
|
|
7
|
+
|
|
8
|
+
Napi::Value attachVirtualJoystick(const Napi::CallbackInfo &info);
|
|
9
|
+
Napi::Value setVirtualJoystickAxis(const Napi::CallbackInfo &info);
|
|
10
|
+
Napi::Value setVirtualJoystickButton(const Napi::CallbackInfo &info);
|
|
11
|
+
Napi::Value detachVirtualJoystick(const Napi::CallbackInfo &info);
|
|
12
|
+
Napi::Value pushAudioDeviceEvent(const Napi::CallbackInfo &info);
|
|
13
|
+
|
|
14
|
+
}; // namespace testing
|
|
15
|
+
|
|
16
|
+
#endif // _TESTING_H_
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
#include "threads.h"
|
|
2
|
+
#include "global.h"
|
|
3
|
+
#include "events.h"
|
|
4
|
+
#include <map>
|
|
5
|
+
#include <set>
|
|
6
|
+
#include <deque>
|
|
7
|
+
#include <mutex>
|
|
8
|
+
#include <atomic>
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
struct Thread {
|
|
12
|
+
napi_threadsafe_function wake = nullptr;
|
|
13
|
+
std::deque<threads::Event> queue;
|
|
14
|
+
std::multiset<Uint32> opens[3];
|
|
15
|
+
bool is_main = false;
|
|
16
|
+
bool demand = false;
|
|
17
|
+
bool wake_pending = false;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
static std::mutex mutex;
|
|
21
|
+
static std::map<napi_env, Thread> all_threads;
|
|
22
|
+
static std::atomic<int> num_demanding { 0 };
|
|
23
|
+
static std::atomic<int> num_workers { 0 };
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
static void
|
|
27
|
+
_wake (Thread &thread)
|
|
28
|
+
{
|
|
29
|
+
if (thread.wake_pending || thread.wake == nullptr) { return; }
|
|
30
|
+
thread.wake_pending = true;
|
|
31
|
+
if (napi_call_threadsafe_function(thread.wake, nullptr, napi_tsfn_nonblocking) != napi_ok) {
|
|
32
|
+
thread.wake_pending = false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static bool
|
|
37
|
+
_wants (const Thread &thread, const threads::Event &event)
|
|
38
|
+
{
|
|
39
|
+
if (thread.is_main) { return true; }
|
|
40
|
+
|
|
41
|
+
const packed::Str *target = event.target;
|
|
42
|
+
if (target == nullptr) { return true; }
|
|
43
|
+
if (false
|
|
44
|
+
|| target == &events::targets::JOYSTICK_DEVICE
|
|
45
|
+
|| target == &events::targets::GAMEPAD_DEVICE
|
|
46
|
+
|| target == &events::targets::AUDIO_DEVICE
|
|
47
|
+
) { return true; }
|
|
48
|
+
if (target == &events::targets::JOYSTICK || target == &events::targets::GAMEPAD) {
|
|
49
|
+
return thread.opens[threads::JOYSTICK].count(event.target_id) > 0
|
|
50
|
+
|| thread.opens[threads::GAMEPAD].count(event.target_id) > 0;
|
|
51
|
+
}
|
|
52
|
+
if (target == &events::targets::SENSOR) {
|
|
53
|
+
return thread.opens[threads::SENSOR].count(event.target_id) > 0;
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static void
|
|
59
|
+
_closeOpens (Thread &thread)
|
|
60
|
+
{
|
|
61
|
+
for (Uint32 id : thread.opens[threads::JOYSTICK]) {
|
|
62
|
+
SDL_Joystick *joystick = SDL_GetJoystickFromID(id);
|
|
63
|
+
if (joystick != nullptr) { SDL_CloseJoystick(joystick); }
|
|
64
|
+
}
|
|
65
|
+
for (Uint32 id : thread.opens[threads::GAMEPAD]) {
|
|
66
|
+
SDL_Gamepad *gamepad = SDL_GetGamepadFromID(id);
|
|
67
|
+
if (gamepad != nullptr) { SDL_CloseGamepad(gamepad); }
|
|
68
|
+
}
|
|
69
|
+
for (Uint32 id : thread.opens[threads::SENSOR]) {
|
|
70
|
+
SDL_Sensor *sensor = SDL_GetSensorFromID(id);
|
|
71
|
+
if (sensor != nullptr) { SDL_CloseSensor(sensor); }
|
|
72
|
+
}
|
|
73
|
+
SDL_ClearError();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static void _envCleanup (void *arg);
|
|
77
|
+
|
|
78
|
+
static void
|
|
79
|
+
_leaveEnv (napi_env env, bool from_hook)
|
|
80
|
+
{
|
|
81
|
+
Thread thread;
|
|
82
|
+
{
|
|
83
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
84
|
+
|
|
85
|
+
{
|
|
86
|
+
std::lock_guard<std::mutex> registry_guard(mutex);
|
|
87
|
+
auto it = all_threads.find(env);
|
|
88
|
+
if (it == all_threads.end()) { return; }
|
|
89
|
+
thread = std::move(it->second);
|
|
90
|
+
all_threads.erase(it);
|
|
91
|
+
if (thread.demand) { num_demanding.fetch_sub(1, std::memory_order_acq_rel); }
|
|
92
|
+
if (!thread.is_main) { num_workers.fetch_sub(1, std::memory_order_acq_rel); }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (global::isReady()) { _closeOpens(thread); }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Not needed: the loop never runs again after this, and the abort release finalizes the function whether or not it is ref'd.
|
|
99
|
+
// if (thread.demand) { napi_unref_threadsafe_function(env, thread.wake); }
|
|
100
|
+
napi_release_threadsafe_function(thread.wake, napi_tsfn_abort);
|
|
101
|
+
if (!from_hook) { napi_remove_env_cleanup_hook(env, _envCleanup, (void *) env); }
|
|
102
|
+
|
|
103
|
+
if (thread.is_main) { global::teardown(); }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static void
|
|
107
|
+
_envCleanup (void *arg)
|
|
108
|
+
{
|
|
109
|
+
_leaveEnv((napi_env) arg, true);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
void
|
|
113
|
+
threads::leave (napi_env env)
|
|
114
|
+
{
|
|
115
|
+
_leaveEnv(env, false);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
void
|
|
119
|
+
threads::trackOpen (Napi::Env &env, Kind kind, Uint32 id)
|
|
120
|
+
{
|
|
121
|
+
std::lock_guard<std::mutex> guard(mutex);
|
|
122
|
+
|
|
123
|
+
auto it = all_threads.find(env);
|
|
124
|
+
if (it == all_threads.end()) { return; }
|
|
125
|
+
|
|
126
|
+
it->second.opens[kind].insert(id);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
void
|
|
130
|
+
threads::trackClose (Napi::Env &env, Kind kind, Uint32 id)
|
|
131
|
+
{
|
|
132
|
+
std::lock_guard<std::mutex> guard(mutex);
|
|
133
|
+
|
|
134
|
+
auto it = all_threads.find(env);
|
|
135
|
+
if (it == all_threads.end()) { return; }
|
|
136
|
+
|
|
137
|
+
auto &ids = it->second.opens[kind];
|
|
138
|
+
auto entry = ids.find(id);
|
|
139
|
+
if (entry != ids.end()) { ids.erase(entry); }
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
void
|
|
143
|
+
threads::distribute (const Event &event, napi_env except)
|
|
144
|
+
{
|
|
145
|
+
std::lock_guard<std::mutex> guard(mutex);
|
|
146
|
+
|
|
147
|
+
for (auto &entry : all_threads) {
|
|
148
|
+
if (entry.first == except) { continue; }
|
|
149
|
+
Thread &thread = entry.second;
|
|
150
|
+
if (!_wants(thread, event)) { continue; }
|
|
151
|
+
// Unbounded and uncoalesced on purpose: a blocked worker can't observe the queue anyway, and dropping or merging events would make poll() on a worker differ from the main thread.
|
|
152
|
+
thread.queue.push_back(event);
|
|
153
|
+
_wake(thread);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
void
|
|
158
|
+
threads::drain (Napi::Env &env, Napi::Function &callback)
|
|
159
|
+
{
|
|
160
|
+
std::deque<Event> pending;
|
|
161
|
+
{
|
|
162
|
+
std::lock_guard<std::mutex> guard(mutex);
|
|
163
|
+
auto it = all_threads.find(env);
|
|
164
|
+
if (it == all_threads.end()) { return; }
|
|
165
|
+
it->second.wake_pending = false;
|
|
166
|
+
pending.swap(it->second.queue);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
while (!pending.empty()) {
|
|
170
|
+
Napi::HandleScope scope(env);
|
|
171
|
+
Event event = std::move(pending.front());
|
|
172
|
+
pending.pop_front();
|
|
173
|
+
try {
|
|
174
|
+
callback.Call(env.Global(), { event.value->toNapi(env) });
|
|
175
|
+
}
|
|
176
|
+
catch (...) {
|
|
177
|
+
std::lock_guard<std::mutex> guard(mutex);
|
|
178
|
+
auto it = all_threads.find(env);
|
|
179
|
+
if (it != all_threads.end()) {
|
|
180
|
+
auto &queue = it->second.queue;
|
|
181
|
+
queue.insert(queue.begin(), pending.begin(), pending.end());
|
|
182
|
+
if (!queue.empty()) { _wake(it->second); }
|
|
183
|
+
}
|
|
184
|
+
throw;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
bool
|
|
190
|
+
threads::isPollingFast ()
|
|
191
|
+
{
|
|
192
|
+
return num_demanding.load(std::memory_order_acquire) > 0
|
|
193
|
+
|| num_workers.load(std::memory_order_acquire) > 0;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
bool
|
|
197
|
+
threads::hasWorkers ()
|
|
198
|
+
{
|
|
199
|
+
return num_workers.load(std::memory_order_acquire) > 0;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
void
|
|
203
|
+
threads::wakeMain ()
|
|
204
|
+
{
|
|
205
|
+
std::lock_guard<std::mutex> guard(mutex);
|
|
206
|
+
|
|
207
|
+
for (auto &entry : all_threads) {
|
|
208
|
+
if (entry.second.is_main) {
|
|
209
|
+
_wake(entry.second);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
void
|
|
216
|
+
threads::shutdown ()
|
|
217
|
+
{
|
|
218
|
+
std::lock_guard<std::mutex> guard(mutex);
|
|
219
|
+
|
|
220
|
+
for (auto &entry : all_threads) {
|
|
221
|
+
napi_release_threadsafe_function(entry.second.wake, napi_tsfn_abort);
|
|
222
|
+
}
|
|
223
|
+
all_threads.clear();
|
|
224
|
+
num_demanding.store(0, std::memory_order_release);
|
|
225
|
+
num_workers.store(0, std::memory_order_release);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
Napi::Value
|
|
229
|
+
threads::registerThread (const Napi::CallbackInfo &info)
|
|
230
|
+
{
|
|
231
|
+
Napi::Env env = info.Env();
|
|
232
|
+
|
|
233
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
234
|
+
|
|
235
|
+
Napi::Function poll = info[0].As<Napi::Function>();
|
|
236
|
+
bool is_main = info[1].As<Napi::Boolean>().Value();
|
|
237
|
+
|
|
238
|
+
napi_threadsafe_function wake;
|
|
239
|
+
napi_value resource_name;
|
|
240
|
+
NAPI_THROW_IF_FAILED(env, napi_create_string_utf8(env, "sdlPoll", NAPI_AUTO_LENGTH, &resource_name), Napi::Value());
|
|
241
|
+
NAPI_THROW_IF_FAILED(env, napi_create_threadsafe_function(env, poll, nullptr, resource_name, 0, 1, nullptr, nullptr, nullptr, nullptr, &wake), Napi::Value());
|
|
242
|
+
napi_status status = napi_unref_threadsafe_function(env, wake);
|
|
243
|
+
if (status != napi_ok) {
|
|
244
|
+
napi_release_threadsafe_function(wake, napi_tsfn_abort);
|
|
245
|
+
NAPI_THROW_IF_FAILED(env, status, Napi::Value());
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
{
|
|
249
|
+
std::lock_guard<std::mutex> registry_guard(mutex);
|
|
250
|
+
|
|
251
|
+
auto it = all_threads.find(env);
|
|
252
|
+
if (it != all_threads.end()) {
|
|
253
|
+
napi_release_threadsafe_function(wake, napi_tsfn_abort);
|
|
254
|
+
throw Napi::Error::New(env, "@kmamal/sdl is already loaded in this thread");
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Registered right after the wake function so that on worker termination it runs before Node finalizes it (cleanup hooks run last-registered-first).
|
|
258
|
+
status = napi_add_env_cleanup_hook(env, _envCleanup, (void *) (napi_env) env);
|
|
259
|
+
if (status != napi_ok) {
|
|
260
|
+
napi_release_threadsafe_function(wake, napi_tsfn_abort);
|
|
261
|
+
NAPI_THROW_IF_FAILED(env, status, Napi::Value());
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
Thread &thread = all_threads[env];
|
|
265
|
+
thread.wake = wake;
|
|
266
|
+
thread.is_main = is_main;
|
|
267
|
+
if (!is_main) { num_workers.fetch_add(1, std::memory_order_acq_rel); }
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (!is_main) { threads::wakeMain(); }
|
|
271
|
+
|
|
272
|
+
return env.Undefined();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
Napi::Value
|
|
276
|
+
threads::setDemand (const Napi::CallbackInfo &info)
|
|
277
|
+
{
|
|
278
|
+
Napi::Env env = info.Env();
|
|
279
|
+
|
|
280
|
+
bool demand = info[0].As<Napi::Boolean>().Value();
|
|
281
|
+
|
|
282
|
+
{
|
|
283
|
+
std::lock_guard<std::mutex> guard(mutex);
|
|
284
|
+
|
|
285
|
+
auto it = all_threads.find(env);
|
|
286
|
+
if (it == all_threads.end()) { return Napi::Boolean::New(env, false); }
|
|
287
|
+
|
|
288
|
+
Thread &thread = it->second;
|
|
289
|
+
if (thread.demand != demand) {
|
|
290
|
+
napi_status status = demand
|
|
291
|
+
? napi_ref_threadsafe_function(env, thread.wake)
|
|
292
|
+
: napi_unref_threadsafe_function(env, thread.wake);
|
|
293
|
+
NAPI_THROW_IF_FAILED(env, status, Napi::Value());
|
|
294
|
+
thread.demand = demand;
|
|
295
|
+
num_demanding.fetch_add(demand ? 1 : -1, std::memory_order_acq_rel);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return Napi::Boolean::New(env, threads::isPollingFast());
|
|
300
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#ifndef _THREADS_H_
|
|
2
|
+
#define _THREADS_H_
|
|
3
|
+
|
|
4
|
+
#include <napi.h>
|
|
5
|
+
#include <SDL3/SDL.h>
|
|
6
|
+
#include <memory>
|
|
7
|
+
#include <string>
|
|
8
|
+
#include "packed.h"
|
|
9
|
+
|
|
10
|
+
namespace threads {
|
|
11
|
+
|
|
12
|
+
enum Kind { JOYSTICK, GAMEPAD, SENSOR };
|
|
13
|
+
|
|
14
|
+
struct Event {
|
|
15
|
+
const packed::Str *target;
|
|
16
|
+
Uint32 target_id;
|
|
17
|
+
std::shared_ptr<const packed::Value> value;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
void trackOpen(Napi::Env &env, Kind kind, Uint32 id);
|
|
21
|
+
void trackClose(Napi::Env &env, Kind kind, Uint32 id);
|
|
22
|
+
|
|
23
|
+
void leave(napi_env env);
|
|
24
|
+
void distribute(const Event &event, napi_env except);
|
|
25
|
+
void drain(Napi::Env &env, Napi::Function &callback);
|
|
26
|
+
bool isPollingFast();
|
|
27
|
+
bool hasWorkers();
|
|
28
|
+
void wakeMain();
|
|
29
|
+
void shutdown();
|
|
30
|
+
|
|
31
|
+
Napi::Value registerThread(const Napi::CallbackInfo &info);
|
|
32
|
+
Napi::Value setDemand(const Napi::CallbackInfo &info);
|
|
33
|
+
|
|
34
|
+
}; // namespace threads
|
|
35
|
+
|
|
36
|
+
#endif // _THREADS_H_
|