@kmamal/sdl 0.11.8 → 0.11.10

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/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.11.8",
2
+ "version": "0.11.10",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -60,18 +60,20 @@
60
60
  "clean": "cd scripts && node clean.mjs"
61
61
  },
62
62
  "dependencies": {
63
- "node-addon-api": "^8.3.1",
64
- "node-gyp": "^11.1.0",
65
63
  "tar": "^7.4.3"
66
64
  },
67
- "sdl": {
68
- "owner": "kmamal",
69
- "repo": "build-sdl",
70
- "version": "2.32.8"
71
- },
72
65
  "devDependencies": {
73
66
  "@kmamal/evdev": "^0.0.2",
74
67
  "@kmamal/testing": "^0.0.31",
75
- "@types/node": "^22.13.5"
68
+ "@types/node": "^22.13.5",
69
+ "node-addon-api": "^8.3.1",
70
+ "node-gyp": "^11.1.0"
71
+ },
72
+ "config": {
73
+ "sdl": {
74
+ "owner": "kmamal",
75
+ "repo": "build-sdl",
76
+ "version": "2.32.8"
77
+ }
76
78
  }
77
79
  }
@@ -6,18 +6,18 @@ import * as Tar from 'tar'
6
6
 
7
7
  const url = `https://github.com/${C.sdl.owner}/${C.sdl.repo}/releases/download/v${C.sdl.version}/${C.sdl.assetName}`
8
8
 
9
- console.log("fetch", url)
10
9
  try {
10
+ console.log("fetch", url)
11
11
  const response = await fetch(url)
12
12
 
13
-
14
13
  console.log("unpack to", C.dir.sdl)
15
14
  await Fs.promises.rm(C.dir.sdl, { recursive: true }).catch(() => {})
16
15
  await Fs.promises.mkdir(C.dir.sdl, { recursive: true })
17
16
  const tar = Tar.extract({ gzip: true, C: C.dir.sdl })
18
17
  response.stream().pipe(tar)
19
18
  await once(tar, 'finish')
20
- } catch (error) {
19
+ }
20
+ catch (error) {
21
21
  console.log(error)
22
22
  throw error
23
23
  }
@@ -18,7 +18,7 @@ const { platform, arch } = process
18
18
  const targetArch = process.env.CROSS_COMPILE_ARCH || arch
19
19
  const assetName = `sdl.node-v${version}-${platform}-${targetArch}.tar.gz`
20
20
 
21
- const sdl = pkg.sdl
21
+ const sdl = pkg.config.sdl
22
22
  sdl.assetName = `SDL-v${sdl.version}-${platform}-${targetArch}.tar.gz`
23
23
 
24
24
  module.exports = {
@@ -10,7 +10,7 @@ const make = (device) => {
10
10
  return {
11
11
  ...rest,
12
12
  mapping: controllerMapping,
13
- name: controllerName,
13
+ name: controllerName ?? rest.name,
14
14
  type: controllerType,
15
15
  }
16
16
  }
@@ -2,7 +2,6 @@ const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
3
  const { EventsViaPoll } = require('../events/events-via-poll')
4
4
  const { ControllerInstance } = require('./controller-instance')
5
- const { reconcileJoystickAndControllerDevices } = require('../events/reconcile-joystick-and-controller-devices')
6
5
 
7
6
  const validEvents = [ 'deviceAdd', 'deviceRemove' ]
8
7
 
@@ -26,7 +25,8 @@ const controller = new class extends EventsViaPoll {
26
25
  Globals.events.poll()
27
26
 
28
27
  const devices = Bindings.joystick_getDevices()
29
- reconcileJoystickAndControllerDevices(devices)
28
+ require('../events/reconcile-joystick-and-controller-devices')
29
+ .reconcileJoystickAndControllerDevices(devices)
30
30
  }
31
31
  }()
32
32
 
@@ -1,11 +1,12 @@
1
1
  const Globals = require('../globals')
2
2
  const { reconcileDevices } = require('./reconcile')
3
3
 
4
+ const { audio: audioModule } = require('../audio')
4
5
  const { compare: compareAudioDevice } = require('../audio/device')
5
6
 
6
7
  const reconcileAudioDevices = (audioDevices, audioDeviceType) => {
7
8
  reconcileDevices(
8
- require('../audio').audio,
9
+ audioModule,
9
10
  Globals.audioDevices[audioDeviceType],
10
11
  audioDevices,
11
12
  compareAudioDevice,
@@ -1,11 +1,12 @@
1
1
  const Globals = require('../globals')
2
2
  const { reconcileDevices } = require('./reconcile')
3
3
 
4
+ const { video: videoModule } = require('../video')
4
5
  const { compare: compareDisplays } = require('../video/display')
5
6
 
6
7
  const reconcileDisplays = (displays) => {
7
8
  reconcileDevices(
8
- require('../video').video,
9
+ videoModule,
9
10
  Globals.displays,
10
11
  displays,
11
12
  compareDisplays,
@@ -1,10 +1,13 @@
1
1
  const Globals = require('../globals')
2
2
  const { reconcileDevices } = require('./reconcile')
3
3
 
4
+ const { joystick: joystickModule } = require('../joystick')
4
5
  const {
5
6
  make: makeJoystickDevice,
6
7
  compare: compareJoystickDevice,
7
8
  } = require('../joystick/device')
9
+
10
+ const { controller: controllerModule } = require('../controller')
8
11
  const {
9
12
  make: makeControllerDevice,
10
13
  compare: compareControllerDevice,
@@ -20,14 +23,14 @@ const reconcileJoystickAndControllerDevices = (devices) => {
20
23
  .map(makeControllerDevice)
21
24
 
22
25
  reconcileDevices(
23
- require('../joystick').joystick,
26
+ joystickModule,
24
27
  Globals.joystickDevices,
25
28
  joystickDevices,
26
29
  compareJoystickDevice,
27
30
  )
28
31
 
29
32
  reconcileDevices(
30
- require('../controller').controller,
33
+ controllerModule,
31
34
  Globals.controllerDevices,
32
35
  controllerDevices,
33
36
  compareControllerDevice,
@@ -135,7 +135,7 @@ controller::open (const Napi::CallbackInfo &info)
135
135
  : env.Null() ;
136
136
 
137
137
  const char *_serial_number = SDL_GameControllerGetSerial(controller);
138
- Napi::Value serial_number = _serial_number != 0
138
+ Napi::Value serial_number = _serial_number != nullptr
139
139
  ? Napi::String::New(env, _serial_number)
140
140
  : env.Null();
141
141
 
@@ -235,15 +235,11 @@ global::initialize(const Napi::CallbackInfo &info)
235
235
  power::states[SDL_POWERSTATE_CHARGED] = "charged";
236
236
 
237
237
 
238
+ Napi::Object initialized = Napi::Object::New(env);
239
+
238
240
  SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "0");
239
241
 
240
- if (SDL_Init(0
241
- | SDL_INIT_EVENTS
242
- | SDL_INIT_JOYSTICK
243
- | SDL_INIT_GAMECONTROLLER
244
- | SDL_INIT_HAPTIC
245
- | SDL_INIT_SENSOR
246
- ) < 0) {
242
+ if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
247
243
  std::ostringstream message;
248
244
  message << "SDL_Init() error: " << SDL_GetError();
249
245
  SDL_ClearError();
@@ -270,7 +266,7 @@ global::initialize(const Napi::CallbackInfo &info)
270
266
  versions.Set("compile", compile_version);
271
267
  versions.Set("runtime", runtime_version);
272
268
 
273
- const char *platform_name = SDL_GetPlatform();
269
+ const char *platform = SDL_GetPlatform();
274
270
 
275
271
  Napi::Array all_video_drivers = Napi::Array::New(env);
276
272
 
@@ -302,8 +298,11 @@ global::initialize(const Napi::CallbackInfo &info)
302
298
  throw Napi::Error::New(env, message.str());
303
299
  }
304
300
  current_video_driver = Napi::String::New(env, name);
305
- } else {
301
+ initialized.Set("video", true);
302
+ }
303
+ else {
306
304
  current_video_driver = env.Null();
305
+ initialized.Set("video", false);
307
306
  }
308
307
 
309
308
  Napi::Object video_drivers = Napi::Object::New(env);
@@ -341,8 +340,11 @@ global::initialize(const Napi::CallbackInfo &info)
341
340
  throw Napi::Error::New(env, message.str());
342
341
  }
343
342
  current_audio_driver = Napi::String::New(env, name);
344
- } else {
343
+ initialized.Set("audio", true);
344
+ }
345
+ else {
345
346
  current_audio_driver = env.Null();
347
+ initialized.Set("audio", false);
346
348
  }
347
349
 
348
350
  Napi::Object audio_drivers = Napi::Object::New(env);
@@ -353,6 +355,11 @@ global::initialize(const Napi::CallbackInfo &info)
353
355
  drivers.Set("video", video_drivers);
354
356
  drivers.Set("audio", audio_drivers);
355
357
 
358
+ initialized.Set("joystick", SDL_InitSubSystem(SDL_INIT_JOYSTICK) == 0);
359
+ initialized.Set("controller", SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0);
360
+ initialized.Set("haptic", SDL_InitSubSystem(SDL_INIT_HAPTIC) == 0);
361
+ initialized.Set("sensor", SDL_InitSubSystem(SDL_INIT_SENSOR) == 0);
362
+
356
363
  SDL_ClearError();
357
364
  global::no_error = SDL_GetError();
358
365
 
@@ -366,10 +373,13 @@ global::initialize(const Napi::CallbackInfo &info)
366
373
  reenableInertialScrolling();
367
374
  #endif
368
375
 
376
+ SDL_memset(&joystick::zero_guid, 0, sizeof(joystick::zero_guid));
377
+
369
378
  Napi::Object result = Napi::Object::New(env);
370
379
  result.Set("version", versions);
371
- result.Set("platform", platform_name);
380
+ result.Set("platform", platform);
372
381
  result.Set("drivers", drivers);
382
+ result.Set("initialized", initialized);
373
383
 
374
384
  return result;
375
385
  }
@@ -10,7 +10,7 @@
10
10
  std::map<Uint8, std::string> joystick::hat_positions;
11
11
  std::map<SDL_JoystickType, std::string> joystick::types;
12
12
  std::map<SDL_JoystickPowerLevel, std::string> joystick::power_levels;
13
-
13
+ SDL_JoystickGUID joystick::zero_guid;
14
14
 
15
15
  double
16
16
  joystick::mapAxis (SDL_Joystick *joystick, int axis) {
@@ -49,29 +49,27 @@ joystick::_getDevices (Napi::Env &env)
49
49
  throw Napi::Error::New(env, message.str());
50
50
  }
51
51
 
52
- const char *name = SDL_JoystickNameForIndex(i);
53
- if (name == nullptr) {
54
- std::ostringstream message;
55
- message << "SDL_JoystickNameForIndex(" << i << ") error: " << SDL_GetError();
56
- SDL_ClearError();
57
- throw Napi::Error::New(env, message.str());
58
- }
59
-
60
- const char *path = SDL_JoystickPathForIndex(i);
61
- if (path == nullptr) {
62
- std::ostringstream message;
63
- message << "SDL_JoystickPathForIndex(" << i << ") error: " << SDL_GetError();
64
- SDL_ClearError();
65
- throw Napi::Error::New(env, message.str());
66
- }
52
+ const char *_name = SDL_JoystickNameForIndex(i);
53
+ Napi::Value name = _name != nullptr
54
+ ? Napi::String::New(env, _name)
55
+ : env.Null();
67
56
 
68
- // This function can only error if the index is invalid.
69
- SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(i);
57
+ const char *_path = SDL_JoystickPathForIndex(i);
58
+ Napi::Value path = _path != nullptr
59
+ ? Napi::String::New(env, _path)
60
+ : env.Null();
70
61
 
71
- char guid_string[33];
72
- SDL_JoystickGetGUIDString(guid, guid_string, 33);
62
+ SDL_JoystickGUID _guid = SDL_JoystickGetDeviceGUID(i);
63
+ Napi::Value guid;
64
+ if (SDL_memcmp(&_guid, &zero_guid, sizeof(SDL_JoystickGUID)) != 0) {
65
+ char guid_string[33];
66
+ SDL_JoystickGetGUIDString(_guid, guid_string, 33);
67
+ guid = Napi::String::New(env, guid_string);
68
+ }
69
+ else {
70
+ guid = env.Null();
71
+ }
73
72
 
74
- // This function can only error if the index is invalid.
75
73
  SDL_JoystickType _type = SDL_JoystickGetDeviceType(i);
76
74
  Napi::Value type = _type != SDL_JOYSTICK_TYPE_UNKNOWN
77
75
  ? Napi::String::New(env, joystick::types[_type])
@@ -104,30 +102,23 @@ joystick::_getDevices (Napi::Env &env)
104
102
  Napi::Value controller_type;
105
103
 
106
104
  if (is_controller) {
107
- const char *_controller_mapping = SDL_GameControllerMappingForDeviceIndex(i);
108
- if (_controller_mapping == nullptr) {
109
- std::ostringstream message;
110
- message << "SDL_GameControllerMappingForDeviceIndex(" << i << ") error: " << SDL_GetError();
111
- SDL_ClearError();
112
- throw Napi::Error::New(env, message.str());
113
- }
114
- controller_mapping = Napi::String::New(env, _controller_mapping);
105
+ char *_controller_mapping = SDL_GameControllerMappingForDeviceIndex(i);
106
+ controller_mapping = _controller_mapping != nullptr
107
+ ? Napi::String::New(env, _controller_mapping)
108
+ : env.Null();
109
+ SDL_free(_controller_mapping);
115
110
 
116
111
  const char *_controller_name = SDL_GameControllerNameForIndex(i);
117
- if (_controller_name == nullptr) {
118
- std::ostringstream message;
119
- message << "SDL_GameControllerNameForIndex(" << i << ") error: " << SDL_GetError();
120
- SDL_ClearError();
121
- throw Napi::Error::New(env, message.str());
122
- }
123
- controller_name = Napi::String::New(env, _controller_name);
124
-
125
- // This function can only error if the index is invalid.
112
+ controller_name = _controller_name != nullptr
113
+ ? Napi::String::New(env, _controller_name)
114
+ : env.Null();
115
+
126
116
  SDL_GameControllerType _controller_type = SDL_GameControllerTypeForIndex(i);
127
117
  controller_type = _controller_type != SDL_CONTROLLER_TYPE_UNKNOWN
128
118
  ? Napi::String::New(env, controller::types[_controller_type])
129
119
  : env.Null();
130
- } else {
120
+ }
121
+ else {
131
122
  controller_mapping = env.Null();
132
123
  controller_name = env.Null();
133
124
  controller_type = env.Null();
@@ -138,8 +129,8 @@ joystick::_getDevices (Napi::Env &env)
138
129
  device.Set("id", id);
139
130
  device.Set("name", name);
140
131
  device.Set("path", path);
141
- device.Set("guid", guid_string);
142
132
  device.Set("type", type);
133
+ device.Set("guid", guid);
143
134
  device.Set("vendor", vendor);
144
135
  device.Set("product", product);
145
136
  device.Set("version", version);
@@ -11,6 +11,7 @@ namespace joystick {
11
11
  extern std::map<SDL_JoystickType, std::string> types;
12
12
  extern std::map<Uint8, std::string> hat_positions;
13
13
  extern std::map<SDL_JoystickPowerLevel, std::string> power_levels;
14
+ extern SDL_JoystickGUID zero_guid;
14
15
 
15
16
  double mapAxis (SDL_Joystick *joystick, int axis);
16
17
  double mapAxisValue (SDL_Joystick *joystick, int axis, int value);
@@ -33,7 +33,6 @@ sensor::getDevices (const Napi::CallbackInfo &info)
33
33
  throw Napi::Error::New(env, message.str());
34
34
  }
35
35
 
36
- // This function can only error if the index is invalid.
37
36
  SDL_SensorType _type = SDL_SensorGetDeviceType(i);
38
37
  Napi::Value type = _type != SDL_SENSOR_UNKNOWN
39
38
  ? Napi::String::New(env, sensor::types[_type])
@@ -25,13 +25,10 @@ video::getDisplays(const Napi::CallbackInfo &info)
25
25
  Napi::Array displays = Napi::Array::New(env, num_displays);
26
26
 
27
27
  for (int i = 0; i < num_displays; i++) {
28
- const char *name = SDL_GetDisplayName(i);
29
- if(name == nullptr) {
30
- std::ostringstream message;
31
- message << "SDL_GetDisplayName(" << i << ") error: " << SDL_GetError();
32
- SDL_ClearError();
33
- throw Napi::Error::New(env, message.str());
34
- }
28
+ const char *_name = SDL_GetDisplayName(i);
29
+ Napi::Value name = _name != nullptr
30
+ ? Napi::String::New(env, _name)
31
+ : env.Null();
35
32
 
36
33
  SDL_DisplayMode mode;
37
34
  if (SDL_GetCurrentDisplayMode(i, &mode) < 0) {
@@ -72,7 +69,8 @@ video::getDisplays(const Napi::CallbackInfo &info)
72
69
  float ddpi, hdpi, vdpi;
73
70
  if (SDL_GetDisplayDPI(i, &ddpi, &hdpi, &vdpi) < 0) {
74
71
  dpi = env.Null();
75
- } else {
72
+ }
73
+ else {
76
74
  Napi::Object dpi_obj = Napi::Object::New(env);
77
75
  dpi_obj.Set("diagonal", ddpi);
78
76
  dpi_obj.Set("horizontal", hdpi);
@@ -328,6 +328,14 @@ export namespace Sdl {
328
328
  readonly current: string
329
329
  }
330
330
  }
331
+ readonly initialized: {
332
+ video: boolean
333
+ audio: boolean
334
+ joystick: boolean
335
+ controller: boolean
336
+ haptic: boolean
337
+ sensor: boolean
338
+ }
331
339
  }
332
340
 
333
341
  export namespace Video {
@@ -382,7 +390,7 @@ export namespace Sdl {
382
390
  | 'best'
383
391
 
384
392
  export interface Display {
385
- readonly name: string
393
+ readonly name: string | null
386
394
  readonly format: Format
387
395
  readonly frequency: number
388
396
  readonly geometry: {
@@ -508,7 +516,7 @@ export namespace Sdl {
508
516
 
509
517
  setIcon (width: number, height: number, stride: number, format: Format, buffer: Buffer): void
510
518
 
511
- flash (untilFocused: boolean): void
519
+ flash (untilFocused?: boolean): void
512
520
  stopFlashing (): void
513
521
 
514
522
  readonly destroyed: boolean
@@ -902,10 +910,10 @@ export namespace Sdl {
902
910
 
903
911
  export interface Device {
904
912
  readonly id: number
905
- readonly name: string
906
- readonly path: string
907
- readonly guid: string
913
+ readonly name: string | null
914
+ readonly path: string | null
908
915
  readonly type: JoystickType | null
916
+ readonly guid: string | null
909
917
  readonly vendor: number | null
910
918
  readonly product: number | null
911
919
  readonly version: number | null
@@ -1012,14 +1020,14 @@ export namespace Sdl {
1012
1020
  export interface Device {
1013
1021
  readonly id: number
1014
1022
  readonly name: string
1015
- readonly path: string
1016
- readonly guid: string
1017
- readonly type: ControllerType | null;
1023
+ readonly path: string | null
1024
+ readonly type: ControllerType | null
1025
+ readonly guid: string | null
1018
1026
  readonly vendor: number | null
1019
1027
  readonly product: number | null
1020
1028
  readonly version: number | null
1021
1029
  readonly player: number | null
1022
- readonly mapping: string
1030
+ readonly mapping: string | null
1023
1031
  }
1024
1032
 
1025
1033
  export class ControllerInstance {