@sdeck/plugin-audio 0.0.0-stage → 0.1.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/bin/sdeck-audio.exe +0 -0
- package/dist/helper.d.ts +95 -0
- package/dist/helper.d.ts.map +1 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1466 -0
- package/dist/index.js.map +22 -0
- package/native/Cargo.lock +230 -0
- package/native/Cargo.toml +29 -0
- package/native/src/main.rs +28 -0
- package/native/src/policy.rs +41 -0
- package/native/src/protocol.rs +132 -0
- package/native/src/windows_audio.rs +609 -0
- package/package.json +65 -4
- package/src/helper.ts +270 -0
- package/src/index.tsx +541 -0
- package/README.md +0 -3
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
|
2
|
+
# It is not intended for manual editing.
|
|
3
|
+
version = 4
|
|
4
|
+
|
|
5
|
+
[[package]]
|
|
6
|
+
name = "itoa"
|
|
7
|
+
version = "1.0.19"
|
|
8
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
9
|
+
checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4"
|
|
10
|
+
|
|
11
|
+
[[package]]
|
|
12
|
+
name = "memchr"
|
|
13
|
+
version = "2.8.3"
|
|
14
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
15
|
+
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
|
16
|
+
|
|
17
|
+
[[package]]
|
|
18
|
+
name = "proc-macro2"
|
|
19
|
+
version = "1.0.107"
|
|
20
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
21
|
+
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
|
22
|
+
dependencies = [
|
|
23
|
+
"unicode-ident",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[[package]]
|
|
27
|
+
name = "quote"
|
|
28
|
+
version = "1.0.47"
|
|
29
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
30
|
+
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
|
31
|
+
dependencies = [
|
|
32
|
+
"proc-macro2",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[[package]]
|
|
36
|
+
name = "sdeck-audio"
|
|
37
|
+
version = "0.1.0"
|
|
38
|
+
dependencies = [
|
|
39
|
+
"serde",
|
|
40
|
+
"serde_json",
|
|
41
|
+
"windows",
|
|
42
|
+
"windows-core",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[[package]]
|
|
46
|
+
name = "serde"
|
|
47
|
+
version = "1.0.229"
|
|
48
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
49
|
+
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
|
|
50
|
+
dependencies = [
|
|
51
|
+
"serde_core",
|
|
52
|
+
"serde_derive",
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
[[package]]
|
|
56
|
+
name = "serde_core"
|
|
57
|
+
version = "1.0.229"
|
|
58
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
59
|
+
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
|
|
60
|
+
dependencies = [
|
|
61
|
+
"serde_derive",
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
[[package]]
|
|
65
|
+
name = "serde_derive"
|
|
66
|
+
version = "1.0.229"
|
|
67
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
68
|
+
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
|
|
69
|
+
dependencies = [
|
|
70
|
+
"proc-macro2",
|
|
71
|
+
"quote",
|
|
72
|
+
"syn 3.0.10",
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
[[package]]
|
|
76
|
+
name = "serde_json"
|
|
77
|
+
version = "1.0.154"
|
|
78
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
79
|
+
checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6"
|
|
80
|
+
dependencies = [
|
|
81
|
+
"itoa",
|
|
82
|
+
"memchr",
|
|
83
|
+
"serde",
|
|
84
|
+
"serde_core",
|
|
85
|
+
"zmij",
|
|
86
|
+
]
|
|
87
|
+
|
|
88
|
+
[[package]]
|
|
89
|
+
name = "syn"
|
|
90
|
+
version = "2.0.119"
|
|
91
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
92
|
+
checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
|
|
93
|
+
dependencies = [
|
|
94
|
+
"proc-macro2",
|
|
95
|
+
"quote",
|
|
96
|
+
"unicode-ident",
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
[[package]]
|
|
100
|
+
name = "syn"
|
|
101
|
+
version = "3.0.10"
|
|
102
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
103
|
+
checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79"
|
|
104
|
+
dependencies = [
|
|
105
|
+
"proc-macro2",
|
|
106
|
+
"quote",
|
|
107
|
+
"unicode-ident",
|
|
108
|
+
]
|
|
109
|
+
|
|
110
|
+
[[package]]
|
|
111
|
+
name = "unicode-ident"
|
|
112
|
+
version = "1.0.27"
|
|
113
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
114
|
+
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
|
115
|
+
|
|
116
|
+
[[package]]
|
|
117
|
+
name = "windows"
|
|
118
|
+
version = "0.62.2"
|
|
119
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
120
|
+
checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580"
|
|
121
|
+
dependencies = [
|
|
122
|
+
"windows-collections",
|
|
123
|
+
"windows-core",
|
|
124
|
+
"windows-future",
|
|
125
|
+
"windows-numerics",
|
|
126
|
+
]
|
|
127
|
+
|
|
128
|
+
[[package]]
|
|
129
|
+
name = "windows-collections"
|
|
130
|
+
version = "0.3.2"
|
|
131
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
132
|
+
checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610"
|
|
133
|
+
dependencies = [
|
|
134
|
+
"windows-core",
|
|
135
|
+
]
|
|
136
|
+
|
|
137
|
+
[[package]]
|
|
138
|
+
name = "windows-core"
|
|
139
|
+
version = "0.62.2"
|
|
140
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
141
|
+
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
|
|
142
|
+
dependencies = [
|
|
143
|
+
"windows-implement",
|
|
144
|
+
"windows-interface",
|
|
145
|
+
"windows-link",
|
|
146
|
+
"windows-result",
|
|
147
|
+
"windows-strings",
|
|
148
|
+
]
|
|
149
|
+
|
|
150
|
+
[[package]]
|
|
151
|
+
name = "windows-future"
|
|
152
|
+
version = "0.3.2"
|
|
153
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
154
|
+
checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb"
|
|
155
|
+
dependencies = [
|
|
156
|
+
"windows-core",
|
|
157
|
+
"windows-link",
|
|
158
|
+
"windows-threading",
|
|
159
|
+
]
|
|
160
|
+
|
|
161
|
+
[[package]]
|
|
162
|
+
name = "windows-implement"
|
|
163
|
+
version = "0.60.2"
|
|
164
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
165
|
+
checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
|
|
166
|
+
dependencies = [
|
|
167
|
+
"proc-macro2",
|
|
168
|
+
"quote",
|
|
169
|
+
"syn 2.0.119",
|
|
170
|
+
]
|
|
171
|
+
|
|
172
|
+
[[package]]
|
|
173
|
+
name = "windows-interface"
|
|
174
|
+
version = "0.59.3"
|
|
175
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
176
|
+
checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
|
|
177
|
+
dependencies = [
|
|
178
|
+
"proc-macro2",
|
|
179
|
+
"quote",
|
|
180
|
+
"syn 2.0.119",
|
|
181
|
+
]
|
|
182
|
+
|
|
183
|
+
[[package]]
|
|
184
|
+
name = "windows-link"
|
|
185
|
+
version = "0.2.1"
|
|
186
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
187
|
+
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
|
188
|
+
|
|
189
|
+
[[package]]
|
|
190
|
+
name = "windows-numerics"
|
|
191
|
+
version = "0.3.1"
|
|
192
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
193
|
+
checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26"
|
|
194
|
+
dependencies = [
|
|
195
|
+
"windows-core",
|
|
196
|
+
"windows-link",
|
|
197
|
+
]
|
|
198
|
+
|
|
199
|
+
[[package]]
|
|
200
|
+
name = "windows-result"
|
|
201
|
+
version = "0.4.1"
|
|
202
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
203
|
+
checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
|
|
204
|
+
dependencies = [
|
|
205
|
+
"windows-link",
|
|
206
|
+
]
|
|
207
|
+
|
|
208
|
+
[[package]]
|
|
209
|
+
name = "windows-strings"
|
|
210
|
+
version = "0.5.1"
|
|
211
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
212
|
+
checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
|
|
213
|
+
dependencies = [
|
|
214
|
+
"windows-link",
|
|
215
|
+
]
|
|
216
|
+
|
|
217
|
+
[[package]]
|
|
218
|
+
name = "windows-threading"
|
|
219
|
+
version = "0.2.1"
|
|
220
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
221
|
+
checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37"
|
|
222
|
+
dependencies = [
|
|
223
|
+
"windows-link",
|
|
224
|
+
]
|
|
225
|
+
|
|
226
|
+
[[package]]
|
|
227
|
+
name = "zmij"
|
|
228
|
+
version = "1.0.23"
|
|
229
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
230
|
+
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "sdeck-audio"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
license = "MIT"
|
|
6
|
+
description = "sdeck's audio helper: Windows audio devices and per-app volume, JSON over stdio."
|
|
7
|
+
publish = false
|
|
8
|
+
|
|
9
|
+
[dependencies]
|
|
10
|
+
serde = { version = "1", features = ["derive"] }
|
|
11
|
+
serde_json = "1"
|
|
12
|
+
|
|
13
|
+
[target."cfg(windows)".dependencies]
|
|
14
|
+
windows = { version = "0.62", features = [
|
|
15
|
+
"Win32_Devices_FunctionDiscovery",
|
|
16
|
+
"Win32_Foundation",
|
|
17
|
+
"Win32_Media_Audio",
|
|
18
|
+
"Win32_Media_Audio_Endpoints",
|
|
19
|
+
"Win32_System_Com",
|
|
20
|
+
"Win32_System_Com_StructuredStorage",
|
|
21
|
+
"Win32_System_Threading",
|
|
22
|
+
"Win32_System_Variant",
|
|
23
|
+
"Win32_UI_Shell_PropertiesSystem",
|
|
24
|
+
] }
|
|
25
|
+
windows-core = "0.62"
|
|
26
|
+
|
|
27
|
+
[profile.release]
|
|
28
|
+
strip = true
|
|
29
|
+
lto = true
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//! sdeck's audio helper. It speaks JSON lines over stdio:
|
|
2
|
+
//!
|
|
3
|
+
//! - request: `{"id": 1, "method": "setVolume", "params": {...}}`
|
|
4
|
+
//! - answer: `{"id": 1, "result": ...}` or `{"id": 1, "error": "..."}`
|
|
5
|
+
//! - change: `{"event": "snapshot", "data": {...}}`, sent at the start and after every change
|
|
6
|
+
//!
|
|
7
|
+
//! It exits when its standard input closes, so it never outlives the process that started it.
|
|
8
|
+
|
|
9
|
+
#[cfg(windows)]
|
|
10
|
+
mod policy;
|
|
11
|
+
#[cfg(windows)]
|
|
12
|
+
mod windows_audio;
|
|
13
|
+
|
|
14
|
+
mod protocol;
|
|
15
|
+
|
|
16
|
+
#[cfg(windows)]
|
|
17
|
+
fn main() {
|
|
18
|
+
if let Err(error) = windows_audio::serve() {
|
|
19
|
+
eprintln!("sdeck-audio: {error}");
|
|
20
|
+
std::process::exit(1);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
#[cfg(not(windows))]
|
|
25
|
+
fn main() {
|
|
26
|
+
eprintln!("sdeck-audio: only Windows is supported");
|
|
27
|
+
std::process::exit(2);
|
|
28
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//! Changing the default audio device. Windows has no public API for it; this is the
|
|
2
|
+
//! `IPolicyConfig` COM interface the Sound control panel uses, unchanged since Windows 7.
|
|
3
|
+
|
|
4
|
+
#![allow(non_snake_case)]
|
|
5
|
+
|
|
6
|
+
use windows::core::{interface, IUnknown, IUnknown_Vtbl, Result, GUID, HRESULT, PCWSTR};
|
|
7
|
+
use windows::Win32::Media::Audio::{eCommunications, eConsole, eMultimedia, ERole};
|
|
8
|
+
use windows::Win32::System::Com::{CoCreateInstance, CLSCTX_ALL};
|
|
9
|
+
|
|
10
|
+
const POLICY_CONFIG_CLIENT: GUID = GUID::from_u128(0x870af99c_171d_4f9e_af0d_e63df40c2bc9);
|
|
11
|
+
|
|
12
|
+
#[interface("f8679f50-850a-41cf-9c72-430f290290c8")]
|
|
13
|
+
unsafe trait IPolicyConfig: IUnknown {
|
|
14
|
+
// The first ten methods are never called; they hold their places in the vtable.
|
|
15
|
+
unsafe fn GetMixFormat(&self) -> HRESULT;
|
|
16
|
+
unsafe fn GetDeviceFormat(&self) -> HRESULT;
|
|
17
|
+
unsafe fn ResetDeviceFormat(&self) -> HRESULT;
|
|
18
|
+
unsafe fn SetDeviceFormat(&self) -> HRESULT;
|
|
19
|
+
unsafe fn GetProcessingPeriod(&self) -> HRESULT;
|
|
20
|
+
unsafe fn SetProcessingPeriod(&self) -> HRESULT;
|
|
21
|
+
unsafe fn GetShareMode(&self) -> HRESULT;
|
|
22
|
+
unsafe fn SetShareMode(&self) -> HRESULT;
|
|
23
|
+
unsafe fn GetPropertyValue(&self) -> HRESULT;
|
|
24
|
+
unsafe fn SetPropertyValue(&self) -> HRESULT;
|
|
25
|
+
unsafe fn SetDefaultEndpoint(&self, device: PCWSTR, role: ERole) -> HRESULT;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// Makes `device` (an endpoint id) the default for every role.
|
|
29
|
+
pub fn set_default(device: &str) -> Result<()> {
|
|
30
|
+
let wide: Vec<u16> = device.encode_utf16().chain(Some(0)).collect();
|
|
31
|
+
// SAFETY: the interface matches the system's vtable, and `wide` outlives the calls.
|
|
32
|
+
unsafe {
|
|
33
|
+
let policy: IPolicyConfig = CoCreateInstance(&POLICY_CONFIG_CLIENT, None, CLSCTX_ALL)?;
|
|
34
|
+
for role in [eConsole, eMultimedia, eCommunications] {
|
|
35
|
+
policy
|
|
36
|
+
.SetDefaultEndpoint(PCWSTR(wide.as_ptr()), role)
|
|
37
|
+
.ok()?;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
Ok(())
|
|
41
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
//! The messages exchanged over stdio.
|
|
2
|
+
|
|
3
|
+
use serde::{Deserialize, Serialize};
|
|
4
|
+
|
|
5
|
+
#[derive(Deserialize)]
|
|
6
|
+
pub struct Request {
|
|
7
|
+
pub id: u64,
|
|
8
|
+
#[serde(flatten)]
|
|
9
|
+
pub command: Command,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
#[derive(Deserialize)]
|
|
13
|
+
#[serde(tag = "method", content = "params", rename_all = "camelCase")]
|
|
14
|
+
pub enum Command {
|
|
15
|
+
Snapshot,
|
|
16
|
+
/// Sets (`volume`) or changes (`delta`) a volume, in percent.
|
|
17
|
+
SetVolume {
|
|
18
|
+
target: Target,
|
|
19
|
+
volume: Option<f32>,
|
|
20
|
+
delta: Option<f32>,
|
|
21
|
+
},
|
|
22
|
+
/// Mutes or unmutes; without `muted`, toggles.
|
|
23
|
+
SetMute {
|
|
24
|
+
target: Target,
|
|
25
|
+
muted: Option<bool>,
|
|
26
|
+
},
|
|
27
|
+
/// Makes a device the default for its direction.
|
|
28
|
+
SetDefault {
|
|
29
|
+
device: String,
|
|
30
|
+
},
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#[derive(Deserialize)]
|
|
34
|
+
#[serde(tag = "kind", rename_all = "camelCase")]
|
|
35
|
+
pub enum Target {
|
|
36
|
+
/// A playback device (the default one without `device`).
|
|
37
|
+
Output { device: Option<String> },
|
|
38
|
+
/// A recording device (the default one without `device`).
|
|
39
|
+
Input { device: Option<String> },
|
|
40
|
+
/// Every audio session of an executable (`spotify.exe`).
|
|
41
|
+
App { app: String },
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#[derive(Serialize, Clone, Copy, PartialEq)]
|
|
45
|
+
pub struct Level {
|
|
46
|
+
/// 0–100.
|
|
47
|
+
pub volume: u8,
|
|
48
|
+
pub muted: bool,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// What a change did, so it can be reversed.
|
|
52
|
+
#[derive(Serialize)]
|
|
53
|
+
pub struct Change {
|
|
54
|
+
pub before: Level,
|
|
55
|
+
pub after: Level,
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
#[derive(Serialize, PartialEq)]
|
|
59
|
+
pub struct Device {
|
|
60
|
+
pub id: String,
|
|
61
|
+
pub name: String,
|
|
62
|
+
pub default: bool,
|
|
63
|
+
#[serde(flatten)]
|
|
64
|
+
pub level: Level,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
#[derive(Serialize, PartialEq)]
|
|
68
|
+
pub struct App {
|
|
69
|
+
/// Executable name, lowercased (`system` for system sounds).
|
|
70
|
+
pub name: String,
|
|
71
|
+
/// Playing right now.
|
|
72
|
+
pub active: bool,
|
|
73
|
+
#[serde(flatten)]
|
|
74
|
+
pub level: Level,
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
#[derive(Serialize, PartialEq)]
|
|
78
|
+
pub struct Snapshot {
|
|
79
|
+
pub outputs: Vec<Device>,
|
|
80
|
+
pub inputs: Vec<Device>,
|
|
81
|
+
pub apps: Vec<App>,
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// A scalar volume (0.0–1.0) as whole percent.
|
|
85
|
+
pub fn percent(scalar: f32) -> u8 {
|
|
86
|
+
(scalar * 100.0).round().clamp(0.0, 100.0) as u8
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// The scalar to set, from an absolute `volume` or a `delta` on `current` (both in percent).
|
|
90
|
+
pub fn wanted(current: f32, volume: Option<f32>, delta: Option<f32>) -> Option<f32> {
|
|
91
|
+
let target = match (volume, delta) {
|
|
92
|
+
(Some(v), _) => v,
|
|
93
|
+
(None, Some(d)) => (current * 100.0).round() + d,
|
|
94
|
+
(None, None) => return None,
|
|
95
|
+
};
|
|
96
|
+
target.is_finite().then(|| (target / 100.0).clamp(0.0, 1.0))
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
#[cfg(test)]
|
|
100
|
+
mod tests {
|
|
101
|
+
use super::*;
|
|
102
|
+
|
|
103
|
+
#[test]
|
|
104
|
+
fn volumes_are_clamped_percent() {
|
|
105
|
+
assert_eq!(percent(0.425), 43);
|
|
106
|
+
assert_eq!(percent(2.0), 100);
|
|
107
|
+
assert_eq!(wanted(0.5, Some(130.0), None), Some(1.0));
|
|
108
|
+
assert_eq!(wanted(0.5, None, Some(-5.0)), Some(0.45));
|
|
109
|
+
assert_eq!(wanted(0.02, None, Some(-5.0)), Some(0.0));
|
|
110
|
+
assert_eq!(wanted(0.5, None, None), None);
|
|
111
|
+
assert_eq!(wanted(0.5, Some(f32::NAN), None), None);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
#[test]
|
|
115
|
+
fn requests_parse() {
|
|
116
|
+
let request: Request = serde_json::from_str(
|
|
117
|
+
r#"{"id":3,"method":"setVolume","params":{"target":{"kind":"app","app":"a.exe"},"delta":5}}"#,
|
|
118
|
+
)
|
|
119
|
+
.unwrap();
|
|
120
|
+
assert_eq!(request.id, 3);
|
|
121
|
+
assert!(matches!(
|
|
122
|
+
request.command,
|
|
123
|
+
Command::SetVolume {
|
|
124
|
+
target: Target::App { .. },
|
|
125
|
+
volume: None,
|
|
126
|
+
delta: Some(_)
|
|
127
|
+
}
|
|
128
|
+
));
|
|
129
|
+
let bare: Request = serde_json::from_str(r#"{"id":1,"method":"snapshot"}"#).unwrap();
|
|
130
|
+
assert!(matches!(bare.command, Command::Snapshot));
|
|
131
|
+
}
|
|
132
|
+
}
|