@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.
- package/README.md +34 -15
- package/binding.gyp +25 -5
- package/package.json +7 -4
- package/src/javascript/audio/audio-instance.js +9 -6
- package/src/javascript/cleanup.js +1 -1
- package/src/javascript/clipboard/index.js +2 -1
- package/src/javascript/controller/controller-instance.js +6 -0
- package/src/javascript/enums.js +1 -1
- package/src/javascript/events/index.js +302 -305
- package/src/javascript/index.js +1 -1
- package/src/javascript/joystick/joystick-instance.js +7 -0
- package/src/javascript/keyboard/index.js +3 -3
- package/src/javascript/keyboard/key-mapping.js +14 -6
- package/src/javascript/mouse/index.js +13 -12
- package/src/javascript/power/index.js +0 -2
- package/src/javascript/sensor/sensor-instance.js +1 -5
- package/src/javascript/video/window.js +17 -16
- package/src/native/audio.cpp +178 -0
- package/src/native/audio.h +21 -0
- package/src/native/clipboard.cpp +45 -0
- package/src/native/clipboard.h +13 -0
- package/src/native/cocoa-global.h +8 -0
- package/src/native/cocoa-global.mm +8 -0
- package/src/native/cocoa-window.h +9 -4
- package/src/native/cocoa-window.mm +6 -9
- package/src/native/controller.cpp +154 -0
- package/src/native/controller.h +17 -0
- package/src/native/enums.cpp +477 -0
- package/src/native/enums.h +67 -0
- package/src/native/events.cpp +339 -0
- package/src/native/events.h +15 -0
- package/src/native/global.cpp +187 -0
- package/src/native/global.h +15 -0
- package/src/native/joystick.cpp +388 -0
- package/src/native/joystick.h +22 -0
- package/src/native/keyboard.cpp +55 -0
- package/src/native/keyboard.h +18 -0
- package/src/native/module.cpp +98 -0
- package/src/native/mouse.cpp +186 -0
- package/src/native/mouse.h +21 -0
- package/src/native/power.cpp +18 -0
- package/src/native/power.h +12 -0
- package/src/native/sensor.cpp +117 -0
- package/src/native/sensor.h +15 -0
- package/src/native/video.cpp +90 -0
- package/src/native/video.h +12 -0
- package/src/native/window.cpp +755 -0
- package/src/native/window.h +29 -0
- package/src/types/index.d.ts +7 -4
- package/scripts/test.mjs +0 -12
- package/src/native/asprintf.c +0 -47
- package/src/native/asprintf.h +0 -37
- package/src/native/binding.cpp +0 -1680
- package/src/native/sdl-helpers.cpp +0 -2617
- package/src/native/sdl-helpers.h +0 -182
|
@@ -10,16 +10,16 @@ const keyboard = {
|
|
|
10
10
|
if (scancode < 0 || scancode >= 512) { throw Object.assign(new Error("invalid scancode"), { scancode }) }
|
|
11
11
|
|
|
12
12
|
const _key = Bindings.keyboard_getKey(scancode)
|
|
13
|
-
return mapping[_key] ?? _key?.length === 1 ? _key : null
|
|
13
|
+
return mapping[_key] ?? (_key?.length === 1 ? _key : null)
|
|
14
14
|
},
|
|
15
15
|
|
|
16
16
|
getScancode (key) {
|
|
17
17
|
if (typeof key !== 'string') { throw Object.assign(new Error("key must be a string"), { key }) }
|
|
18
18
|
|
|
19
|
-
const _key = reverseMapping[key] ?? key.length === 1 ? key : null
|
|
19
|
+
const _key = reverseMapping[key] ?? (key.length === 1 ? key : null)
|
|
20
20
|
if (_key === null) { throw Object.assign(new Error("invalid key"), { key }) }
|
|
21
21
|
|
|
22
|
-
return Bindings.keyboard_getScancode(_key)
|
|
22
|
+
return Bindings.keyboard_getScancode(_key) || null
|
|
23
23
|
},
|
|
24
24
|
|
|
25
25
|
getState () { return Bindings.keyboard_getState() },
|
|
@@ -206,12 +206,20 @@ const mapping = {
|
|
|
206
206
|
const reverseMapping = {}
|
|
207
207
|
|
|
208
208
|
for (const [ key, value ] of Object.entries(mapping)) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
209
|
+
maybeSkip: {
|
|
210
|
+
const existing = reverseMapping[value]
|
|
211
|
+
if (existing === undefined) { break maybeSkip }
|
|
212
|
+
|
|
213
|
+
const keyScancode = Bindings.keyboard_getScancode(key)
|
|
214
|
+
if (keyScancode === 0) { continue }
|
|
215
|
+
|
|
216
|
+
const existingScancode = Bindings.keyboard_getScancode(existing)
|
|
217
|
+
if (existingScancode === 0) { break maybeSkip }
|
|
218
|
+
|
|
219
|
+
if (existingScancode < keyScancode) { continue }
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
reverseMapping[value] = key
|
|
215
223
|
}
|
|
216
224
|
|
|
217
225
|
module.exports = {
|
|
@@ -11,13 +11,7 @@ const mouse = {
|
|
|
11
11
|
return Bindings.mouse_getButton(button)
|
|
12
12
|
},
|
|
13
13
|
|
|
14
|
-
get position () {
|
|
15
|
-
const position = Bindings.mouse_getPosition()
|
|
16
|
-
return {
|
|
17
|
-
get x () { return position.x },
|
|
18
|
-
get y () { return position.y },
|
|
19
|
-
}
|
|
20
|
-
},
|
|
14
|
+
get position () { return Bindings.mouse_getPosition() },
|
|
21
15
|
|
|
22
16
|
setPosition (x, y) {
|
|
23
17
|
if (!Number.isFinite(x)) { throw Object.assign(new Error("x must be a number"), { x }) }
|
|
@@ -27,16 +21,17 @@ const mouse = {
|
|
|
27
21
|
},
|
|
28
22
|
|
|
29
23
|
setCursor (cursor) {
|
|
30
|
-
const _cursor = Enums.cursor[cursor] ?? null
|
|
31
24
|
if (typeof cursor !== 'string') { throw Object.assign(new Error("cursor must be a string"), { cursor }) }
|
|
32
|
-
|
|
25
|
+
|
|
26
|
+
const _cursor = Enums.cursor[cursor]
|
|
27
|
+
if (_cursor === undefined) { throw Object.assign(new Error("invalid cursor"), { cursor }) }
|
|
33
28
|
|
|
34
29
|
Bindings.mouse_setCursor(_cursor)
|
|
35
30
|
},
|
|
36
31
|
|
|
37
|
-
|
|
38
|
-
const _format = Enums.pixelFormat[format] ?? null
|
|
32
|
+
resetCursor () { Bindings.mouse_resetCursor() },
|
|
39
33
|
|
|
34
|
+
setCursorImage (width, height, stride, format, buffer, x, y) {
|
|
40
35
|
if (!Number.isFinite(width)) { throw Object.assign(new Error("width must be a number"), { width }) }
|
|
41
36
|
if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
|
|
42
37
|
if (!Number.isFinite(height)) { throw Object.assign(new Error("height must be a number"), { height }) }
|
|
@@ -44,7 +39,6 @@ const mouse = {
|
|
|
44
39
|
if (!Number.isFinite(stride)) { throw Object.assign(new Error("stride must be a number"), { stride }) }
|
|
45
40
|
if (stride < width) { throw Object.assign(new Error("invalid stride"), { stride, width }) }
|
|
46
41
|
if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
|
|
47
|
-
if (_format === null) { throw Object.assign(new Error("invalid format"), { format }) }
|
|
48
42
|
if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
|
|
49
43
|
if (buffer.length < stride * height) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height }) }
|
|
50
44
|
if (!Number.isFinite(x)) { throw Object.assign(new Error("x must be a number"), { x }) }
|
|
@@ -52,18 +46,25 @@ const mouse = {
|
|
|
52
46
|
if (!Number.isFinite(y)) { throw Object.assign(new Error("y must be a number"), { y }) }
|
|
53
47
|
if (y < 0 || y >= height) { throw Object.assign(new Error("invalid y"), { y }) }
|
|
54
48
|
|
|
49
|
+
const _format = Enums.pixelFormat[format]
|
|
50
|
+
if (_format === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
|
|
51
|
+
|
|
55
52
|
Bindings.mouse_setCursorImage(width, height, stride, _format, buffer, x, y)
|
|
56
53
|
},
|
|
57
54
|
|
|
58
55
|
showCursor (show = true) {
|
|
59
56
|
if (typeof show !== 'boolean') { throw Object.assign(new Error("show must be a boolean"), { show }) }
|
|
57
|
+
|
|
60
58
|
Bindings.mouse_showCursor(show)
|
|
61
59
|
},
|
|
62
60
|
|
|
63
61
|
hideCursor () { mouse.showCursor(false) },
|
|
64
62
|
|
|
63
|
+
redrawCursor () { Bindings.mouse_redrawCursor() },
|
|
64
|
+
|
|
65
65
|
capture (capture = true) {
|
|
66
66
|
if (typeof capture !== 'boolean') { throw Object.assign(new Error("capture must be a boolean"), { capture }) }
|
|
67
|
+
|
|
67
68
|
Bindings.mouse_capture(capture)
|
|
68
69
|
},
|
|
69
70
|
|
|
@@ -4,8 +4,6 @@ const Enums = require('../enums')
|
|
|
4
4
|
const power = {
|
|
5
5
|
get info () {
|
|
6
6
|
const info = Bindings.power_getInfo()
|
|
7
|
-
if (info.seconds === -1) { info.seconds = null }
|
|
8
|
-
if (info.percent === -1) { info.percent = null }
|
|
9
7
|
info.state = Enums.powerStateNames[info.state]
|
|
10
8
|
return info
|
|
11
9
|
},
|
|
@@ -28,11 +28,7 @@ class SensorInstance extends EventsViaPoll {
|
|
|
28
28
|
|
|
29
29
|
get device () { return this._device }
|
|
30
30
|
|
|
31
|
-
get data () {
|
|
32
|
-
const data = Bindings.sensor_getData(this._device.id)
|
|
33
|
-
if (data.timestamp === 0) { data.timestamp = null }
|
|
34
|
-
return data
|
|
35
|
-
}
|
|
31
|
+
get data () { return Bindings.sensor_getData(this._device.id) }
|
|
36
32
|
|
|
37
33
|
get closed () { return this._closed }
|
|
38
34
|
close () {
|
|
@@ -95,10 +95,10 @@ class Window extends EventsViaPoll {
|
|
|
95
95
|
const result = Bindings.window_create(
|
|
96
96
|
title,
|
|
97
97
|
displayIndex,
|
|
98
|
-
width,
|
|
99
|
-
height,
|
|
100
98
|
x,
|
|
101
99
|
y,
|
|
100
|
+
width,
|
|
101
|
+
height,
|
|
102
102
|
visible,
|
|
103
103
|
fullscreen,
|
|
104
104
|
resizable,
|
|
@@ -127,7 +127,7 @@ class Window extends EventsViaPoll {
|
|
|
127
127
|
this._alwaysOnTop = result.alwaysOnTop
|
|
128
128
|
this._accelerated = result.accelerated
|
|
129
129
|
this._vsync = result.vsync
|
|
130
|
-
this._native = result.native
|
|
130
|
+
this._native = result.native
|
|
131
131
|
this._skipTaskbar = result.skipTaskbar
|
|
132
132
|
this._popupMenu = result.popupMenu
|
|
133
133
|
this._tooltip = result.tooltip
|
|
@@ -138,8 +138,6 @@ class Window extends EventsViaPoll {
|
|
|
138
138
|
this._opengl = opengl
|
|
139
139
|
this._webgpu = webgpu
|
|
140
140
|
|
|
141
|
-
this._focused = false
|
|
142
|
-
this._hovered = false
|
|
143
141
|
this._minimized = false
|
|
144
142
|
this._maximized = false
|
|
145
143
|
this._destroyed = false
|
|
@@ -266,7 +264,9 @@ class Window extends EventsViaPoll {
|
|
|
266
264
|
|
|
267
265
|
if (typeof accelerated !== 'boolean') { throw Object.assign(new Error("accelerated must be a boolean"), { accelerated }) }
|
|
268
266
|
|
|
269
|
-
|
|
267
|
+
const result = Bindings.window_setAcceleratedAndVsync(this._id, accelerated, this._vsync)
|
|
268
|
+
this._accelerated = result.accelerated
|
|
269
|
+
this._vsync = result.vsync
|
|
270
270
|
}
|
|
271
271
|
|
|
272
272
|
get vsync () { return this._vsync }
|
|
@@ -278,7 +278,9 @@ class Window extends EventsViaPoll {
|
|
|
278
278
|
|
|
279
279
|
if (typeof vsync !== 'boolean') { throw Object.assign(new Error("vsync must be a boolean"), { vsync }) }
|
|
280
280
|
|
|
281
|
-
|
|
281
|
+
const result = Bindings.window_setAcceleratedAndVsync(this._id, this._accelerated, vsync)
|
|
282
|
+
this._accelerated = result.accelerated
|
|
283
|
+
this._vsync = result.vsync
|
|
282
284
|
}
|
|
283
285
|
|
|
284
286
|
get opengl () { return this._opengl }
|
|
@@ -317,16 +319,15 @@ class Window extends EventsViaPoll {
|
|
|
317
319
|
this._fullscreen = false
|
|
318
320
|
}
|
|
319
321
|
|
|
320
|
-
get focused () { return
|
|
322
|
+
get focused () { return Globals.windows.focused === this }
|
|
321
323
|
focus () {
|
|
322
324
|
if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
|
|
323
325
|
|
|
324
326
|
Bindings.window_focus(this._id)
|
|
325
|
-
this._focused = true
|
|
326
327
|
Globals.windows.focused = this
|
|
327
328
|
}
|
|
328
329
|
|
|
329
|
-
get hovered () { return
|
|
330
|
+
get hovered () { return Globals.windows.hovered === this }
|
|
330
331
|
|
|
331
332
|
get skipTaskbar () { return this._skipTaskbar }
|
|
332
333
|
get popupMenu () { return this._popupMenu }
|
|
@@ -339,8 +340,6 @@ class Window extends EventsViaPoll {
|
|
|
339
340
|
if (this._opengl) { throw new Error("can't call render in opengl mode") }
|
|
340
341
|
if (this._webgpu) { throw new Error("can't call render in webgpu mode") }
|
|
341
342
|
|
|
342
|
-
const _format = Enums.pixelFormat[format] ?? null
|
|
343
|
-
|
|
344
343
|
if (!Number.isFinite(width)) { throw Object.assign(new Error("width must be a number"), { width }) }
|
|
345
344
|
if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
|
|
346
345
|
if (!Number.isFinite(height)) { throw Object.assign(new Error("height must be a number"), { height }) }
|
|
@@ -348,18 +347,18 @@ class Window extends EventsViaPoll {
|
|
|
348
347
|
if (!Number.isFinite(stride)) { throw Object.assign(new Error("stride must be a number"), { stride }) }
|
|
349
348
|
if (stride < width) { throw Object.assign(new Error("invalid stride"), { stride, width }) }
|
|
350
349
|
if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
|
|
351
|
-
if (_format === null) { throw Object.assign(new Error("invalid format"), { format }) }
|
|
352
350
|
if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
|
|
353
351
|
if (buffer.length < stride * height) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height }) }
|
|
354
352
|
|
|
353
|
+
const _format = Enums.pixelFormat[format]
|
|
354
|
+
if (_format === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
|
|
355
|
+
|
|
355
356
|
Bindings.window_render(this._id, width, height, stride, _format, buffer)
|
|
356
357
|
}
|
|
357
358
|
|
|
358
359
|
setIcon (width, height, stride, format, buffer) {
|
|
359
360
|
if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
|
|
360
361
|
|
|
361
|
-
const _format = Enums.pixelFormat[format] ?? null
|
|
362
|
-
|
|
363
362
|
if (!Number.isFinite(width)) { throw Object.assign(new Error("width must be a number"), { width }) }
|
|
364
363
|
if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
|
|
365
364
|
if (!Number.isFinite(height)) { throw Object.assign(new Error("height must be a number"), { height }) }
|
|
@@ -367,10 +366,12 @@ class Window extends EventsViaPoll {
|
|
|
367
366
|
if (!Number.isFinite(stride)) { throw Object.assign(new Error("stride must be a number"), { stride }) }
|
|
368
367
|
if (stride < width) { throw Object.assign(new Error("invalid stride"), { stride, width }) }
|
|
369
368
|
if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
|
|
370
|
-
if (_format === null) { throw Object.assign(new Error("invalid format"), { format }) }
|
|
371
369
|
if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
|
|
372
370
|
if (buffer.length < stride * height) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height }) }
|
|
373
371
|
|
|
372
|
+
const _format = Enums.pixelFormat[format]
|
|
373
|
+
if (_format === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
|
|
374
|
+
|
|
374
375
|
Bindings.window_setIcon(this._id, width, height, stride, _format, buffer)
|
|
375
376
|
}
|
|
376
377
|
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
#include "audio.h"
|
|
2
|
+
#include "global.h"
|
|
3
|
+
#include <SDL.h>
|
|
4
|
+
#include <string>
|
|
5
|
+
#include <sstream>
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Napi::Array
|
|
9
|
+
audio::_getDevices(Napi::Env &env, bool is_capture)
|
|
10
|
+
{
|
|
11
|
+
int num_devices = SDL_GetNumAudioDevices(is_capture ? 1 : 0);
|
|
12
|
+
if (num_devices == -1) {
|
|
13
|
+
const char *error = SDL_GetError();
|
|
14
|
+
if (error != global::no_error) {
|
|
15
|
+
std::ostringstream message;
|
|
16
|
+
message << "SDL_GetNumAudioDevices(" << is_capture << ") error: " << error;
|
|
17
|
+
SDL_ClearError();
|
|
18
|
+
throw Napi::Error::New(env, message.str());
|
|
19
|
+
}
|
|
20
|
+
return Napi::Array::New(env);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
Napi::Array devices = Napi::Array::New(env, num_devices);
|
|
24
|
+
|
|
25
|
+
for (int i = 0; i < num_devices; i++) {
|
|
26
|
+
const char *name = SDL_GetAudioDeviceName(i, is_capture);
|
|
27
|
+
if (name == nullptr) {
|
|
28
|
+
std::ostringstream message;
|
|
29
|
+
message << "SDL_GetAudioDeviceName(" << i << ", " << is_capture << ") error: " << SDL_GetError();
|
|
30
|
+
SDL_ClearError();
|
|
31
|
+
throw Napi::Error::New(env, message.str());
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
Napi::Object device = Napi::Object::New(env);
|
|
35
|
+
device.Set("isRecorder", Napi::Boolean::New(env, is_capture));
|
|
36
|
+
device.Set("name", Napi::String::New(env, name));
|
|
37
|
+
|
|
38
|
+
devices.Set(i, device);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return devices;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
Napi::Value
|
|
46
|
+
audio::getDevices(const Napi::CallbackInfo &info)
|
|
47
|
+
{
|
|
48
|
+
Napi::Env env = info.Env();
|
|
49
|
+
|
|
50
|
+
bool is_capture = info[0].As<Napi::Boolean>().Value();
|
|
51
|
+
|
|
52
|
+
return audio::_getDevices(env, is_capture);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
#include <iostream>
|
|
56
|
+
|
|
57
|
+
Napi::Value
|
|
58
|
+
audio::open (const Napi::CallbackInfo &info)
|
|
59
|
+
{
|
|
60
|
+
Napi::Env env = info.Env();
|
|
61
|
+
|
|
62
|
+
std::string name;
|
|
63
|
+
bool has_name = !info[0].IsNull();
|
|
64
|
+
if (has_name) { name = info[0].As<Napi::String>().Utf8Value(); }
|
|
65
|
+
bool is_capture = info[1].As<Napi::Boolean>().Value();
|
|
66
|
+
int freq = info[2].As<Napi::Number>().Int32Value();
|
|
67
|
+
int format = info[3].As<Napi::Number>().Int32Value();
|
|
68
|
+
int channels = info[4].As<Napi::Number>().Int32Value();
|
|
69
|
+
int samples = info[5].As<Napi::Number>().Int32Value();
|
|
70
|
+
|
|
71
|
+
SDL_AudioSpec desired;
|
|
72
|
+
SDL_memset(&desired, 0, sizeof(desired));
|
|
73
|
+
desired.freq = freq;
|
|
74
|
+
desired.format = format;
|
|
75
|
+
desired.channels = channels;
|
|
76
|
+
desired.samples = samples;
|
|
77
|
+
|
|
78
|
+
SDL_AudioDeviceID audio_id = SDL_OpenAudioDevice(has_name ? name.c_str() : nullptr, is_capture ? 1 : 0, &desired, nullptr, 0);
|
|
79
|
+
if (audio_id == 0) {
|
|
80
|
+
std::ostringstream message;
|
|
81
|
+
message << "SDL_OpenAudioDevice() error: " << SDL_GetError();
|
|
82
|
+
SDL_ClearError();
|
|
83
|
+
throw Napi::Error::New(env, message.str());
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return Napi::Number::New(env, audio_id);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
Napi::Value
|
|
90
|
+
audio::play (const Napi::CallbackInfo &info)
|
|
91
|
+
{
|
|
92
|
+
Napi::Env env = info.Env();
|
|
93
|
+
|
|
94
|
+
int audio_id = info[0].As<Napi::Number>().Int32Value();
|
|
95
|
+
bool should_play = info[1].As<Napi::Boolean>().Value();
|
|
96
|
+
|
|
97
|
+
SDL_PauseAudioDevice(audio_id, should_play ? 0 : 1);
|
|
98
|
+
|
|
99
|
+
return env.Undefined();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
Napi::Value
|
|
103
|
+
audio::getQueueSize (const Napi::CallbackInfo &info)
|
|
104
|
+
{
|
|
105
|
+
Napi::Env env = info.Env();
|
|
106
|
+
|
|
107
|
+
int audio_id = info[0].As<Napi::Number>().Int32Value();
|
|
108
|
+
|
|
109
|
+
int size = SDL_GetQueuedAudioSize(audio_id);
|
|
110
|
+
|
|
111
|
+
return Napi::Number::New(env, size);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
Napi::Value
|
|
115
|
+
audio::clearQueue (const Napi::CallbackInfo &info)
|
|
116
|
+
{
|
|
117
|
+
Napi::Env env = info.Env();
|
|
118
|
+
|
|
119
|
+
int audio_id = info[0].As<Napi::Number>().Int32Value();
|
|
120
|
+
|
|
121
|
+
SDL_ClearQueuedAudio(audio_id);
|
|
122
|
+
|
|
123
|
+
return env.Undefined();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
Napi::Value
|
|
127
|
+
audio::enqueue (const Napi::CallbackInfo &info)
|
|
128
|
+
{
|
|
129
|
+
Napi::Env env = info.Env();
|
|
130
|
+
|
|
131
|
+
int audio_id = info[0].As<Napi::Number>().Int32Value();
|
|
132
|
+
void *src = info[1].As<Napi::Buffer<char>>().Data();
|
|
133
|
+
int size = info[2].As<Napi::Number>().Int32Value();
|
|
134
|
+
|
|
135
|
+
if (SDL_QueueAudio(audio_id, src, size) < 0) {
|
|
136
|
+
std::ostringstream message;
|
|
137
|
+
message << "SDL_QueueAudio() error: " << SDL_GetError();
|
|
138
|
+
SDL_ClearError();
|
|
139
|
+
throw Napi::Error::New(env, message.str());
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return env.Undefined();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
Napi::Value
|
|
146
|
+
audio::dequeue (const Napi::CallbackInfo &info)
|
|
147
|
+
{
|
|
148
|
+
Napi::Env env = info.Env();
|
|
149
|
+
|
|
150
|
+
int audio_id = info[0].As<Napi::Number>().Int32Value();
|
|
151
|
+
void *dst = info[1].As<Napi::Buffer<char>>().Data();
|
|
152
|
+
int size = info[2].As<Napi::Number>().Int32Value();
|
|
153
|
+
|
|
154
|
+
int num = SDL_DequeueAudio(audio_id, dst, size);
|
|
155
|
+
const char *error = SDL_GetError();
|
|
156
|
+
if (error != global::no_error) {
|
|
157
|
+
std::ostringstream message;
|
|
158
|
+
message << "SDL_DequeueAudio(" << audio_id << ") error: " << error;
|
|
159
|
+
SDL_ClearError();
|
|
160
|
+
throw Napi::Error::New(env, message.str());
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return Napi::Number::New(env, num);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
Napi::Value
|
|
167
|
+
audio::close (const Napi::CallbackInfo &info)
|
|
168
|
+
{
|
|
169
|
+
Napi::Env env = info.Env();
|
|
170
|
+
|
|
171
|
+
int audio_id = info[0].As<Napi::Number>().Int32Value();
|
|
172
|
+
|
|
173
|
+
SDL_PauseAudioDevice(audio_id, 1);
|
|
174
|
+
SDL_ClearQueuedAudio(audio_id);
|
|
175
|
+
SDL_CloseAudioDevice(audio_id);
|
|
176
|
+
|
|
177
|
+
return env.Undefined();
|
|
178
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#ifndef _AUDIO_H_
|
|
2
|
+
#define _AUDIO_H_
|
|
3
|
+
|
|
4
|
+
#include <napi.h>
|
|
5
|
+
|
|
6
|
+
namespace audio {
|
|
7
|
+
|
|
8
|
+
Napi::Array _getDevices(Napi::Env &env, bool is_capture);
|
|
9
|
+
|
|
10
|
+
Napi::Value getDevices(const Napi::CallbackInfo &info);
|
|
11
|
+
Napi::Value open(const Napi::CallbackInfo &info);
|
|
12
|
+
Napi::Value play(const Napi::CallbackInfo &info);
|
|
13
|
+
Napi::Value getQueueSize(const Napi::CallbackInfo &info);
|
|
14
|
+
Napi::Value enqueue(const Napi::CallbackInfo &info);
|
|
15
|
+
Napi::Value dequeue(const Napi::CallbackInfo &info);
|
|
16
|
+
Napi::Value clearQueue(const Napi::CallbackInfo &info);
|
|
17
|
+
Napi::Value close(const Napi::CallbackInfo &info);
|
|
18
|
+
|
|
19
|
+
}; // namespace audio
|
|
20
|
+
|
|
21
|
+
#endif // _AUDIO_H_
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#include "clipboard.h"
|
|
2
|
+
#include <SDL.h>
|
|
3
|
+
#include <string>
|
|
4
|
+
#include <sstream>
|
|
5
|
+
|
|
6
|
+
Napi::Value
|
|
7
|
+
clipboard::getText (const Napi::CallbackInfo &info)
|
|
8
|
+
{
|
|
9
|
+
Napi::Env env = info.Env();
|
|
10
|
+
|
|
11
|
+
if (SDL_HasClipboardText() == SDL_FALSE) {
|
|
12
|
+
return Napi::String::New(env, "");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
char *text = SDL_GetClipboardText();
|
|
16
|
+
if (text[0] == '\0') {
|
|
17
|
+
SDL_free(text);
|
|
18
|
+
std::ostringstream message;
|
|
19
|
+
message << "SDL_GetClipboardText() error: " << SDL_GetError();
|
|
20
|
+
SDL_ClearError();
|
|
21
|
+
throw Napi::Error::New(env, message.str());
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
Napi::String result = Napi::String::New(env, text);
|
|
25
|
+
SDL_free(text);
|
|
26
|
+
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
Napi::Value
|
|
31
|
+
clipboard::setText (const Napi::CallbackInfo &info)
|
|
32
|
+
{
|
|
33
|
+
Napi::Env env = info.Env();
|
|
34
|
+
|
|
35
|
+
std::string text = info[0].As<Napi::String>().Utf8Value();
|
|
36
|
+
|
|
37
|
+
if (SDL_SetClipboardText(text.c_str()) < 0) {
|
|
38
|
+
std::ostringstream message;
|
|
39
|
+
message << "SDL_SetClipboardText() error: " << SDL_GetError();
|
|
40
|
+
SDL_ClearError();
|
|
41
|
+
throw Napi::Error::New(env, message.str());
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return env.Undefined();
|
|
45
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#ifndef _CLIPBOARD_H_
|
|
2
|
+
#define _CLIPBOARD_H_
|
|
3
|
+
|
|
4
|
+
#include <napi.h>
|
|
5
|
+
|
|
6
|
+
namespace clipboard {
|
|
7
|
+
|
|
8
|
+
Napi::Value getText(const Napi::CallbackInfo &info);
|
|
9
|
+
Napi::Value setText(const Napi::CallbackInfo &info);
|
|
10
|
+
|
|
11
|
+
}; // namespace clipboard
|
|
12
|
+
|
|
13
|
+
#endif // _CLIPBOARD_H_
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
+
#ifndef _COCOA_WINDOW_H_
|
|
2
|
+
#define _COCOA_WINDOW_H_
|
|
3
|
+
|
|
1
4
|
|
|
2
5
|
#ifndef __OBJC__
|
|
3
|
-
class CALayer;
|
|
6
|
+
class CALayer;
|
|
4
7
|
#endif
|
|
5
8
|
|
|
6
|
-
extern "C" CALayer * getCocoaGlView(NSWindow *);
|
|
7
9
|
|
|
8
|
-
extern "C" CALayer *
|
|
10
|
+
extern "C" CALayer *getCocoaGlView(NSWindow *);
|
|
11
|
+
|
|
12
|
+
extern "C" CALayer *getCocoaGpuView(NSWindow *);
|
|
13
|
+
|
|
9
14
|
|
|
10
|
-
|
|
15
|
+
#endif // _COCOA_WINDOW_H_
|
|
@@ -1,27 +1,24 @@
|
|
|
1
1
|
#include <Cocoa/Cocoa.h>
|
|
2
|
-
#
|
|
2
|
+
#include <QuartzCore/CAMetalLayer.h>
|
|
3
3
|
|
|
4
4
|
#include "cocoa-window.h"
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
|
|
7
|
+
extern "C" CALayer *getCocoaGlView(NSWindow *window) {
|
|
7
8
|
@autoreleasepool {
|
|
8
|
-
NSView *
|
|
9
|
+
NSView *view = [window contentView];
|
|
9
10
|
[view setWantsLayer:YES];
|
|
10
11
|
[[view layer] setContentsScale:[window backingScaleFactor]];
|
|
11
12
|
return [view layer];
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
extern "C" CALayer *
|
|
16
|
+
extern "C" CALayer *getCocoaGpuView(NSWindow *window) {
|
|
16
17
|
@autoreleasepool {
|
|
17
|
-
NSView *
|
|
18
|
+
NSView *view = [window contentView];
|
|
18
19
|
[view setWantsLayer:YES];
|
|
19
20
|
[view setLayer:[CAMetalLayer layer]];
|
|
20
21
|
[[view layer] setContentsScale:[window backingScaleFactor]];
|
|
21
22
|
return [view layer];
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
|
-
|
|
25
|
-
extern "C" void reenableInertialScrolling() {
|
|
26
|
-
[[NSUserDefaults standardUserDefaults] setBool: YES forKey: @"AppleMomentumScrollSupported"];
|
|
27
|
-
}
|