@kmamal/sdl 0.5.0 → 0.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/package.json CHANGED
@@ -1,19 +1,26 @@
1
1
  {
2
- "version": "0.5.0",
2
+ "version": "0.6.0",
3
3
  "name": "@kmamal/sdl",
4
- "description": "SDL2 bindings for Node.js",
4
+ "description": "SDL bindings for Node.js",
5
5
  "keywords": [
6
6
  "sdl",
7
7
  "sdl2",
8
8
  "gui",
9
+ "desktop",
9
10
  "window",
10
11
  "screen",
11
12
  "video",
12
13
  "audio",
13
14
  "sound",
15
+ "speaker",
16
+ "mic",
14
17
  "input",
15
18
  "mouse",
16
19
  "keyboard",
20
+ "joystick",
21
+ "controller",
22
+ "gamepad",
23
+ "rumble",
17
24
  "clipboard",
18
25
  "game"
19
26
  ],
@@ -29,6 +36,7 @@
29
36
  "test": "cd scripts && node test.mjs",
30
37
  "install": "cd scripts && node install.mjs",
31
38
  "download-release": "cd scripts && node download-release.mjs",
39
+ "download-sdl": "cd scripts && node download-sdl.mjs",
32
40
  "build": "cd scripts && node build.mjs",
33
41
  "upload-release": "cd scripts && node upload-release.mjs",
34
42
  "release": "cd scripts && node release.mjs",
@@ -36,7 +44,11 @@
36
44
  "install-deps-mac": "./scripts/install-deps-mac.sh"
37
45
  },
38
46
  "dependencies": {
39
- "@kmamal/build-sdl": "~2.24.1-5",
40
47
  "tar": "^6.1.11"
48
+ },
49
+ "sdl": {
50
+ "owner": "kmamal",
51
+ "repo": "build-sdl",
52
+ "version": "2.24.1"
41
53
  }
42
54
  }
package/scripts/build.mjs CHANGED
@@ -11,19 +11,16 @@ await Promise.all([
11
11
  await Fs.promises.rm(dir, { recursive: true }).catch(() => {})
12
12
  }))
13
13
 
14
- console.log("load @kmamal/build-sdl")
15
- process.chdir(C.dir.root)
16
- const { default: sdlPath } = await import('@kmamal/build-sdl')
17
-
18
14
  console.log("build in", C.dir.build)
19
- process.env.SDL_INC = Path.join(sdlPath, 'include')
20
- process.env.SDL_LIB = Path.join(sdlPath, 'lib')
15
+ process.env.SDL_INC = Path.join(C.dir.sdl, 'include')
16
+ process.env.SDL_LIB = Path.join(C.dir.sdl, 'lib')
21
17
 
22
18
  let archFlag = ''
23
19
  if (process.env.CROSS_COMPILE_ARCH) {
24
20
  archFlag = `--arch ${process.env.CROSS_COMPILE_ARCH}`
25
21
  }
26
22
 
23
+ process.chdir(C.dir.root)
27
24
  execSync(`npx node-gyp rebuild ${archFlag} -j max --verbose`, { stdio: 'inherit' })
28
25
 
29
26
  console.log("install to", C.dir.dist)
package/scripts/clean.mjs CHANGED
@@ -4,6 +4,7 @@ import C from './util/common.js'
4
4
 
5
5
  const dirs = [
6
6
  Path.join(C.dir.root, 'node_modules'),
7
+ C.dir.sdl,
7
8
  C.dir.build,
8
9
  C.dir.dist,
9
10
  C.dir.publish,
@@ -0,0 +1,17 @@
1
+ import Fs from 'node:fs'
2
+ import { once } from 'node:events'
3
+ import C from './util/common.js'
4
+ import { fetch } from './util/fetch.js'
5
+ import Tar from 'tar'
6
+
7
+ const url = `https://github.com/${C.sdl.owner}/${C.sdl.repo}/releases/download/v${C.sdl.version}/${C.sdl.assetName}`
8
+
9
+ console.log("fetch", url)
10
+ const response = await fetch(url)
11
+
12
+ console.log("unpack to", C.dir.sdl)
13
+ await Fs.promises.rm(C.dir.sdl, { recursive: true }).catch(() => {})
14
+ await Fs.promises.mkdir(C.dir.sdl, { recursive: true })
15
+ const tar = Tar.extract({ gzip: true, C: C.dir.sdl })
16
+ response.stream().pipe(tar)
17
+ await once(tar, 'finish')
@@ -8,4 +8,5 @@ if (!process.env.NODE_SDL_FROM_SOURCE) {
8
8
  console.log("skip download and build from source")
9
9
  }
10
10
 
11
+ await import('./download-sdl.mjs')
11
12
  await import('./build.mjs')
@@ -3,6 +3,7 @@ const Path = require('node:path')
3
3
 
4
4
  const dir = {}
5
5
  dir.root = Path.resolve(__dirname, '../..')
6
+ dir.sdl = Path.join(dir.root, 'sdl')
6
7
  dir.build = Path.join(dir.root, 'build')
7
8
  dir.dist = Path.join(dir.root, 'dist')
8
9
  dir.publish = Path.join(dir.root, 'publish')
@@ -16,6 +17,9 @@ const { platform, arch } = process
16
17
  const targetArch = process.env.CROSS_COMPILE_ARCH || arch
17
18
  const assetName = `sdl.node-v${version}-${platform}-${targetArch}.tar.gz`
18
19
 
20
+ const sdl = pkg.sdl
21
+ sdl.assetName = `SDL-v${sdl.version}-${platform}-${targetArch}.tar.gz`
22
+
19
23
  module.exports = {
20
24
  dir,
21
25
  version,
@@ -25,4 +29,5 @@ module.exports = {
25
29
  arch,
26
30
  targetArch,
27
31
  assetName,
32
+ sdl,
28
33
  }
@@ -1,15 +1,16 @@
1
1
  const Bindings = require('../bindings')
2
- const { enums } = require('../enums')
2
+ const Enums = require('../enums')
3
3
  const Globals = require('../globals')
4
+ const { EventsViaPoll } = require('../events/events-via-poll')
4
5
  const { AudioFormatHelpers } = require('./format-helpers')
5
6
 
6
- Globals.audioDevices.playback = Bindings.audio_getDevices(false)
7
- Globals.audioDevices.recording = Bindings.audio_getDevices(true)
7
+ const validEvents = [ 'close' ]
8
8
 
9
- class AudioInstance {
9
+ class AudioInstance extends EventsViaPoll {
10
10
  constructor (device, options) {
11
- const { name: deviceName, recording } = device
12
- const name = options.name || deviceName
11
+ super(validEvents)
12
+
13
+ const { name, type } = device
13
14
 
14
15
  const {
15
16
  channels = 1,
@@ -18,8 +19,11 @@ class AudioInstance {
18
19
  buffered = 4096,
19
20
  } = options
20
21
 
21
- const _format = enums.audioFormat[format] ?? null
22
+ const _format = Enums.audioFormat[format] ?? null
22
23
 
24
+ if (name !== undefined && name !== null && typeof name !== 'string') { throw Object.assign(new Error("name must be a string"), { name }) }
25
+ if (typeof type !== 'string') { throw Object.assign(new Error("type must be a string"), { type }) }
26
+ if (type !== 'playback' && type !== 'recording') { throw Object.assign(new Error("type must be either 'playback' or 'recording'"), { type }) }
23
27
  if (!Number.isFinite(channels)) { throw Object.assign(new Error("channels must be a number"), { channels }) }
24
28
  if (![ 1, 2, 4, 6 ].includes(channels)) { throw Object.assign(new Error("invalid channels"), { channels }) }
25
29
  if (!Number.isFinite(frequency)) { throw Object.assign(new Error("frequency must be a number"), { frequency }) }
@@ -29,7 +33,7 @@ class AudioInstance {
29
33
  if (!Number.isFinite(buffered)) { throw Object.assign(new Error("buffered must be a number"), { buffered }) }
30
34
  if (buffered !== 2 ** (32 - Math.clz32(buffered) - 1)) { throw Object.assign(new Error("invalid buffered"), { buffered }) }
31
35
 
32
- this._id = Bindings.audio_openDevice(name, recording, frequency, _format, channels, buffered)
36
+ this._id = Bindings.audio_openDevice(name ?? '', type === 'recording', frequency, _format, channels, buffered)
33
37
 
34
38
  this._device = device
35
39
  this._name = name
@@ -49,7 +53,7 @@ class AudioInstance {
49
53
  this._reader = helper.reader
50
54
  this._writer = helper.writer
51
55
 
52
- Globals.audioInstances.all.set(this._id, this)
56
+ Globals.audioInstances.set(this._id, this)
53
57
  }
54
58
 
55
59
  get id () { return this._id }
@@ -105,9 +109,12 @@ class AudioInstance {
105
109
  close () {
106
110
  if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._id }) }
107
111
 
112
+ this.emit('close')
113
+ this.removeAllListeners()
108
114
  this._closed = true
115
+
109
116
  Bindings.audio_close(this._id)
110
- Globals.audioInstances.all.delete(this._id)
117
+ Globals.audioInstances.delete(this._id)
111
118
  }
112
119
  }
113
120
 
@@ -0,0 +1,18 @@
1
+
2
+ const make = (device) => {
3
+ const { isRecorder } = device
4
+ delete device.isRecorder
5
+ device.type = isRecorder ? 'recording' : 'playback'
6
+ }
7
+
8
+ const compare = (a, b) => {
9
+ const { name: aName } = a
10
+ const { name: bName } = b
11
+ if (aName === bName) { return 0 }
12
+ return a.name < b.name ? -1 : 1
13
+ }
14
+
15
+ module.exports = {
16
+ make,
17
+ compare,
18
+ }
@@ -17,7 +17,7 @@ const audio = new class extends EventsViaPoll {
17
17
  }
18
18
 
19
19
  openDevice (device, options = {}) {
20
- return device.recording
20
+ return device.type === "recording"
21
21
  ? new AudioRecordingInstance(device, options)
22
22
  : new AudioPlaybackInstance(device, options)
23
23
  }
@@ -4,7 +4,10 @@ const { AudioPlaybackInstance } = require('./audio-playback-instance')
4
4
  let timeout
5
5
 
6
6
  const resetTimeout = () => {
7
- if (timeout) { clearTimeout(timeout) }
7
+ if (timeout) {
8
+ clearTimeout(timeout)
9
+ timeout = null
10
+ }
8
11
  }
9
12
 
10
13
  process.on('beforeExit', (code) => {
@@ -12,7 +15,7 @@ process.on('beforeExit', (code) => {
12
15
 
13
16
  let duration = 0
14
17
 
15
- for (const instance of Globals.audioInstances.all.values()) {
18
+ for (const instance of Globals.audioInstances.values()) {
16
19
  if (!(instance instanceof AudioPlaybackInstance)) { continue }
17
20
  const { queued, playing, buffered } = instance
18
21
  if (!queued || !playing) { continue }
@@ -12,17 +12,17 @@ process.on('exit', (code) => {
12
12
  }
13
13
 
14
14
  // Close all audio instances
15
- for (const instance of Globals.audioInstances.all.values()) {
15
+ for (const instance of Globals.audioInstances.values()) {
16
16
  instance.close()
17
17
  }
18
18
 
19
19
  // Close all joysticks
20
- for (const joystick of Globals.joysticks.all.values()) {
20
+ for (const joystick of Globals.joystickInstances.all.values()) {
21
21
  joystick.close()
22
22
  }
23
23
 
24
24
  // Close all controllers
25
- for (const controller of Globals.controllers.all.values()) {
25
+ for (const controller of Globals.controllerInstances.all.values()) {
26
26
  controller.close()
27
27
  }
28
28
 
@@ -0,0 +1,115 @@
1
+ const Bindings = require('../bindings')
2
+ const Enums = require('../enums')
3
+ const Globals = require('../globals')
4
+ const { EventsViaPoll } = require('../events/events-via-poll')
5
+
6
+ const validEvents = [
7
+ 'axisMotion',
8
+ 'buttonDown',
9
+ 'buttonUp',
10
+ 'remap',
11
+ 'close',
12
+ ]
13
+
14
+ class ControllerInstance extends EventsViaPoll {
15
+ constructor (device) {
16
+ super(validEvents)
17
+
18
+ const result = Bindings.controller_open(device._index)
19
+
20
+ this._firmwareVersion = result.firmwareVersion
21
+ this._serialNumber = result.serialNumber
22
+ this._hasLed = result.hasLed
23
+ this._hasRumble = result.hasRumble
24
+ this._hasRumbleTriggers = result.hasRumbleTriggers
25
+ this._axes = result.axes
26
+ this._buttons = result.buttons
27
+
28
+ this._device = device
29
+
30
+ this._closed = false
31
+
32
+ Globals.controllerInstances.all.add(this)
33
+ let collection = Globals.controllerInstances.byId.get(this._device.id)
34
+ if (!collection) {
35
+ collection = new Set()
36
+ Globals.controllerInstances.byId.set(this._device.id, collection)
37
+ }
38
+ collection.add(this)
39
+ }
40
+
41
+ get device () { return this._device }
42
+ get firmwareVersion () { return this._firmwareVersion }
43
+ get serialNumber () { return this._serialNumber }
44
+
45
+ get axes () { return this._axes }
46
+ get buttons () { return this._buttons }
47
+
48
+ get power () {
49
+ const power = Bindings.joystick_getPower(this._device.id)
50
+ return Enums.powerLevelNames[power]
51
+ }
52
+
53
+ get hasLed () { return this._hasLed }
54
+ setLed (red, green, blue) {
55
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
56
+
57
+ if (!Number.isFinite(red)) { throw Object.assign(new Error("red must be a number"), { red }) }
58
+ if (red < 0 || red > 1) { throw Object.assign(new Error("red must be between 0 and 1"), { red }) }
59
+ if (!Number.isFinite(green)) { throw Object.assign(new Error("green must be a number"), { green }) }
60
+ if (green < 0 || green > 1) { throw Object.assign(new Error("green must be between 0 and 1"), { green }) }
61
+ if (!Number.isFinite(blue)) { throw Object.assign(new Error("blue must be a number"), { blue }) }
62
+ if (blue < 0 || blue > 1) { throw Object.assign(new Error("blue must be between 0 and 1"), { blue }) }
63
+
64
+ Bindings.joystick_rumble(this._device.id, red, green, blue)
65
+ }
66
+
67
+ get hasRumble () { return this._hasRumble }
68
+ rumble (lowFreqRumble = 1, highFreqRumble = 1, duration = 1e3) {
69
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
70
+
71
+ if (!Number.isFinite(lowFreqRumble)) { throw Object.assign(new Error("lowFreqRumble must be a number"), { lowFreqRumble }) }
72
+ if (lowFreqRumble < 0 || lowFreqRumble > 1) { throw Object.assign(new Error("lowFreqRumble must be between 0 and 1"), { lowFreqRumble }) }
73
+ if (!Number.isFinite(highFreqRumble)) { throw Object.assign(new Error("highFreqRumble must be a number"), { highFreqRumble }) }
74
+ if (highFreqRumble < 0 || highFreqRumble > 1) { throw Object.assign(new Error("highFreqRumble must be between 0 and 1"), { highFreqRumble }) }
75
+ if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
76
+
77
+ Bindings.joystick_rumble(this._device.id, lowFreqRumble, highFreqRumble, duration)
78
+ }
79
+
80
+ stopRumble () { this.rumble(0, 0) }
81
+
82
+ get hasRumbleTriggers () { return this._hasRumbleTriggers }
83
+ rumbleTriggers (leftRumble = 1, rightRumble = 1, duration = 1e3) {
84
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
85
+
86
+ if (!Number.isFinite(leftRumble)) { throw Object.assign(new Error("leftRumble must be a number"), { leftRumble }) }
87
+ if (leftRumble < 0 || leftRumble > 1) { throw Object.assign(new Error("leftRumble must be between 0 and 1"), { leftRumble }) }
88
+ if (!Number.isFinite(rightRumble)) { throw Object.assign(new Error("rightRumble must be a number"), { rightRumble }) }
89
+ if (rightRumble < 0 || rightRumble > 1) { throw Object.assign(new Error("rightRumble must be between 0 and 1"), { rightRumble }) }
90
+ if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
91
+
92
+ Bindings.joystick_rumbleTriggers(this._device.id, leftRumble, rightRumble, duration)
93
+ }
94
+
95
+ stopRumbleTriggers () { this.rumbleTriggers(0, 0) }
96
+
97
+ get closed () { return this._closed }
98
+ close () {
99
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
100
+
101
+ this.emit('close')
102
+ this.removeAllListeners()
103
+ this._closed = true
104
+
105
+ Globals.controllerInstances.all.delete(this)
106
+ const collection = Globals.controllerInstances.byId.get(this._device.id)
107
+ collection.delete(this)
108
+ if (collection.size === 0) {
109
+ Bindings.controller_close(this._device.id)
110
+ Globals.controllerInstances.byId.delete(this._device.id)
111
+ }
112
+ }
113
+ }
114
+
115
+ module.exports = { ControllerInstance }
@@ -0,0 +1,15 @@
1
+
2
+ const make = (device) => {
3
+ delete device.type
4
+ delete device.isController
5
+ }
6
+
7
+ const compare = (a, b) => a.id - b.id
8
+
9
+ const filter = (device) => device.isController
10
+
11
+ module.exports = {
12
+ make,
13
+ compare,
14
+ filter,
15
+ }
@@ -0,0 +1,25 @@
1
+ const Bindings = require('../bindings')
2
+ const Globals = require('../globals')
3
+ const { EventsViaPoll } = require('../events/events-via-poll')
4
+ const { ControllerInstance } = require('./controller-instance')
5
+
6
+ const validEvents = [ 'deviceAdd', 'deviceRemove', 'deviceRemap' ]
7
+
8
+ const controller = new class extends EventsViaPoll {
9
+ constructor () { super(validEvents) }
10
+
11
+ get devices () { return Globals.controllerDevices }
12
+
13
+ openDevice (device) { return new ControllerInstance(device) }
14
+
15
+ addMappings (mappings) {
16
+ if (!Array.isArray(mappings)) { throw Object.assign(new Error("mappings must be an array"), { mappings }) }
17
+ for (const mapping of mappings) {
18
+ if (typeof mapping !== 'string') { throw Object.assign(new Error("mapping must be a string"), { mappings }) }
19
+ }
20
+
21
+ Bindings.controller_addMappings(mappings)
22
+ }
23
+ }()
24
+
25
+ module.exports = { controller }
@@ -10,4 +10,4 @@ for (const [ key, obj ] of Object.entries(enums)) {
10
10
  enums[`${key}Names`] = names
11
11
  }
12
12
 
13
- module.exports = { enums }
13
+ module.exports = enums