zmk-studio-api 0.3.1

Rust + Python client for the ZMK Studio RPC API (Serial + BLE)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use std::io::{Read, Write};
use std::sync::Mutex;

use prost::Message;
use pyo3::exceptions::{PyRuntimeError, PyTypeError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::{PyAny, PyBytes, PyDict, PyModule};
use strum::IntoEnumIterator;

#[cfg(feature = "ble")]
use crate::transport::PlatformBleTransport;
#[cfg(feature = "serial")]
use crate::transport::serial::SerialTransport;
use crate::{Behavior, ClientError, HidUsage, Keycode, StudioClient};

trait ReadWriteSend: Read + Write + Send {}
impl<T: Read + Write + Send> ReadWriteSend for T {}

type DynClient = StudioClient<Box<dyn ReadWriteSend>>;

#[pyclass(name = "Behavior", from_py_object)]
#[derive(Clone)]
pub struct PyBehavior {
    inner: Behavior,
}

impl PyBehavior {
    fn new(inner: Behavior) -> Self {
        Self { inner }
    }
}

#[pymethods]
impl PyBehavior {
    #[getter]
    pub fn kind(&self) -> &'static str {
        match self.inner {
            Behavior::KeyPress(_) => "KeyPress",
            Behavior::KeyToggle(_) => "KeyToggle",
            Behavior::LayerTap { .. } => "LayerTap",
            Behavior::ModTap { .. } => "ModTap",
            Behavior::StickyKey(_) => "StickyKey",
            Behavior::StickyLayer { .. } => "StickyLayer",
            Behavior::MomentaryLayer { .. } => "MomentaryLayer",
            Behavior::ToggleLayer { .. } => "ToggleLayer",
            Behavior::ToLayer { .. } => "ToLayer",
            Behavior::Bluetooth { .. } => "Bluetooth",
            Behavior::ExternalPower { .. } => "ExternalPower",
            Behavior::OutputSelection { .. } => "OutputSelection",
            Behavior::Backlight { .. } => "Backlight",
            Behavior::Underglow { .. } => "Underglow",
            Behavior::MouseKeyPress { .. } => "MouseKeyPress",
            Behavior::MouseMove { .. } => "MouseMove",
            Behavior::MouseScroll { .. } => "MouseScroll",
            Behavior::CapsWord => "CapsWord",
            Behavior::KeyRepeat => "KeyRepeat",
            Behavior::Reset => "Reset",
            Behavior::Bootloader => "Bootloader",
            Behavior::SoftOff => "SoftOff",
            Behavior::StudioUnlock => "StudioUnlock",
            Behavior::GraveEscape => "GraveEscape",
            Behavior::Transparent => "Transparent",
            Behavior::None => "None",
            Behavior::Unknown { .. } => "Unknown",
        }
    }

    fn __repr__(&self) -> String {
        format!("Behavior({:?})", self.inner)
    }
}

#[pyclass(name = "StudioClient")]
pub struct PyStudioClient {
    inner: Mutex<DynClient>,
}

#[pymethods]
impl PyStudioClient {
    #[staticmethod]
    #[cfg(feature = "serial")]
    pub fn open_serial(path: &str) -> PyResult<Self> {
        let transport = SerialTransport::open(path).map_err(|err| {
            PyRuntimeError::new_err(format!("failed to open serial transport: {err}"))
        })?;
        Ok(Self {
            inner: Mutex::new(StudioClient::new(Box::new(transport))),
        })
    }

    #[staticmethod]
    #[cfg(not(feature = "serial"))]
    pub fn open_serial(_path: &str) -> PyResult<Self> {
        Err(PyRuntimeError::new_err(
            "serial support is disabled for this build",
        ))
    }

    #[staticmethod]
    #[cfg(feature = "ble")]
    pub fn open_ble(device_id: &str) -> PyResult<Self> {
        let transport = PlatformBleTransport::connect_device(device_id).map_err(|err| {
            PyRuntimeError::new_err(format!("failed to connect BLE transport: {err}"))
        })?;
        Ok(Self {
            inner: Mutex::new(StudioClient::new(Box::new(transport))),
        })
    }

    #[staticmethod]
    #[cfg(not(feature = "ble"))]
    pub fn open_ble(_device_id: &str) -> PyResult<Self> {
        Err(PyRuntimeError::new_err(
            "ble support is disabled for this build",
        ))
    }

    #[staticmethod]
    #[cfg(feature = "ble")]
    pub fn list_ble_devices() -> PyResult<Vec<(String, Option<String>)>> {
        let devices = StudioClient::<PlatformBleTransport>::list_ble_devices()
            .map_err(|err| PyRuntimeError::new_err(format!("failed to list BLE devices: {err}")))?;
        Ok(devices
            .into_iter()
            .map(|device| (device.device_id, device.local_name))
            .collect())
    }

    #[staticmethod]
    #[cfg(not(feature = "ble"))]
    pub fn list_ble_devices() -> PyResult<Vec<(String, Option<String>)>> {
        Err(PyRuntimeError::new_err(
            "ble support is disabled for this build",
        ))
    }

    pub fn get_lock_state(&self) -> PyResult<String> {
        let state = self.with_client(|client| client.get_lock_state())?;
        Ok(state.as_str_name().to_string())
    }

    pub fn reset_settings(&self) -> PyResult<bool> {
        self.with_client(|client| client.reset_settings())
    }

    pub fn list_all_behaviors(&self) -> PyResult<Vec<u32>> {
        self.with_client(|client| client.list_all_behaviors())
    }

    pub fn get_device_info_bytes<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
        let info = self.with_client(|client| client.get_device_info())?;
        Ok(PyBytes::new(py, &info.encode_to_vec()))
    }

    pub fn get_behavior_details_bytes<'py>(
        &self,
        py: Python<'py>,
        behavior_id: u32,
    ) -> PyResult<Bound<'py, PyBytes>> {
        let details = self.with_client(|client| client.get_behavior_details(behavior_id))?;
        Ok(PyBytes::new(py, &details.encode_to_vec()))
    }

    pub fn get_keymap_bytes<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
        let keymap = self.with_client(|client| client.get_keymap())?;
        Ok(PyBytes::new(py, &keymap.encode_to_vec()))
    }

    pub fn get_physical_layouts_bytes<'py>(
        &self,
        py: Python<'py>,
    ) -> PyResult<Bound<'py, PyBytes>> {
        let layouts = self.with_client(|client| client.get_physical_layouts())?;
        Ok(PyBytes::new(py, &layouts.encode_to_vec()))
    }

    pub fn get_key_at(&self, layer_id: u32, key_position: i32) -> PyResult<PyBehavior> {
        let behavior = self.with_client(|client| client.get_key_at(layer_id, key_position))?;
        Ok(PyBehavior::new(behavior))
    }

    pub fn set_key_at(
        &self,
        layer_id: u32,
        key_position: i32,
        behavior: PyBehavior,
    ) -> PyResult<()> {
        self.with_client(|client| client.set_key_at(layer_id, key_position, behavior.inner))
    }

    pub fn check_unsaved_changes(&self) -> PyResult<bool> {
        self.with_client(|client| client.check_unsaved_changes())
    }

    pub fn save_changes(&self) -> PyResult<()> {
        self.with_client(|client| client.save_changes())
    }

    pub fn discard_changes(&self) -> PyResult<bool> {
        self.with_client(|client| client.discard_changes())
    }
}

impl PyStudioClient {
    fn with_client<R>(
        &self,
        f: impl FnOnce(&mut DynClient) -> Result<R, ClientError>,
    ) -> PyResult<R> {
        let mut client = self
            .inner
            .lock()
            .map_err(|_| PyRuntimeError::new_err("client mutex is poisoned"))?;
        f(&mut client).map_err(|err| PyRuntimeError::new_err(err.to_string()))
    }
}

fn parse_hid_usage(value: &Bound<'_, PyAny>) -> PyResult<HidUsage> {
    if let Ok(encoded) = value.extract::<u32>() {
        return Ok(HidUsage::from_encoded(encoded));
    }

    if let Ok(name) = value.extract::<String>() {
        let keycode = Keycode::from_name(&name)
            .ok_or_else(|| PyValueError::new_err(format!("invalid keycode name: {name}")))?;
        return Ok(HidUsage::from_encoded(keycode.to_hid_usage()));
    }

    Err(PyTypeError::new_err(
        "key must be a Keycode/int or keycode name string",
    ))
}

#[pyfunction(name = "KeyPress")]
fn key_press(key: &Bound<'_, PyAny>) -> PyResult<PyBehavior> {
    Ok(PyBehavior::new(Behavior::KeyPress(parse_hid_usage(key)?)))
}

#[pyfunction(name = "KeyToggle")]
fn key_toggle(key: &Bound<'_, PyAny>) -> PyResult<PyBehavior> {
    Ok(PyBehavior::new(Behavior::KeyToggle(parse_hid_usage(key)?)))
}

#[pyfunction(name = "LayerTap")]
fn layer_tap(layer_id: u32, tap: &Bound<'_, PyAny>) -> PyResult<PyBehavior> {
    Ok(PyBehavior::new(Behavior::LayerTap {
        layer_id,
        tap: parse_hid_usage(tap)?,
    }))
}

#[pyfunction(name = "ModTap")]
fn mod_tap(hold: &Bound<'_, PyAny>, tap: &Bound<'_, PyAny>) -> PyResult<PyBehavior> {
    Ok(PyBehavior::new(Behavior::ModTap {
        hold: parse_hid_usage(hold)?,
        tap: parse_hid_usage(tap)?,
    }))
}

#[pyfunction(name = "StickyKey")]
fn sticky_key(key: &Bound<'_, PyAny>) -> PyResult<PyBehavior> {
    Ok(PyBehavior::new(Behavior::StickyKey(parse_hid_usage(key)?)))
}

#[pyfunction(name = "StickyLayer")]
fn sticky_layer(layer_id: u32) -> PyBehavior {
    PyBehavior::new(Behavior::StickyLayer { layer_id })
}

#[pyfunction(name = "MomentaryLayer")]
fn momentary_layer(layer_id: u32) -> PyBehavior {
    PyBehavior::new(Behavior::MomentaryLayer { layer_id })
}

#[pyfunction(name = "ToggleLayer")]
fn toggle_layer(layer_id: u32) -> PyBehavior {
    PyBehavior::new(Behavior::ToggleLayer { layer_id })
}

#[pyfunction(name = "ToLayer")]
fn to_layer(layer_id: u32) -> PyBehavior {
    PyBehavior::new(Behavior::ToLayer { layer_id })
}

#[pyfunction(name = "Bluetooth")]
fn bluetooth(command: u32, value: u32) -> PyBehavior {
    PyBehavior::new(Behavior::Bluetooth { command, value })
}

#[pyfunction(name = "ExternalPower")]
fn external_power(value: u32) -> PyBehavior {
    PyBehavior::new(Behavior::ExternalPower { value })
}

#[pyfunction(name = "OutputSelection")]
fn output_selection(value: u32) -> PyBehavior {
    PyBehavior::new(Behavior::OutputSelection { value })
}

#[pyfunction(name = "Backlight")]
fn backlight(command: u32, value: u32) -> PyBehavior {
    PyBehavior::new(Behavior::Backlight { command, value })
}

#[pyfunction(name = "Underglow")]
fn underglow(command: u32, value: u32) -> PyBehavior {
    PyBehavior::new(Behavior::Underglow { command, value })
}

#[pyfunction(name = "MouseKeyPress")]
fn mouse_key_press(value: u32) -> PyBehavior {
    PyBehavior::new(Behavior::MouseKeyPress { value })
}

#[pyfunction(name = "MouseMove")]
fn mouse_move(value: u32) -> PyBehavior {
    PyBehavior::new(Behavior::MouseMove { value })
}

#[pyfunction(name = "MouseScroll")]
fn mouse_scroll(value: u32) -> PyBehavior {
    PyBehavior::new(Behavior::MouseScroll { value })
}

#[pyfunction(name = "CapsWord")]
fn caps_word() -> PyBehavior {
    PyBehavior::new(Behavior::CapsWord)
}

#[pyfunction(name = "KeyRepeat")]
fn key_repeat() -> PyBehavior {
    PyBehavior::new(Behavior::KeyRepeat)
}

#[pyfunction(name = "Reset")]
fn reset() -> PyBehavior {
    PyBehavior::new(Behavior::Reset)
}

#[pyfunction(name = "Bootloader")]
fn bootloader() -> PyBehavior {
    PyBehavior::new(Behavior::Bootloader)
}

#[pyfunction(name = "SoftOff")]
fn soft_off() -> PyBehavior {
    PyBehavior::new(Behavior::SoftOff)
}

#[pyfunction(name = "StudioUnlock")]
fn studio_unlock() -> PyBehavior {
    PyBehavior::new(Behavior::StudioUnlock)
}

#[pyfunction(name = "GraveEscape")]
fn grave_escape() -> PyBehavior {
    PyBehavior::new(Behavior::GraveEscape)
}

#[pyfunction(name = "Transparent")]
fn transparent() -> PyBehavior {
    PyBehavior::new(Behavior::Transparent)
}

#[pyfunction(name = "NoBehavior")]
fn no_behavior() -> PyBehavior {
    PyBehavior::new(Behavior::None)
}

#[pyfunction(name = "Raw")]
fn raw(behavior_id: i32, param1: u32, param2: u32) -> PyBehavior {
    PyBehavior::new(Behavior::Unknown {
        behavior_id,
        param1,
        param2,
    })
}

#[pymodule]
fn zmk_studio_api(py: Python<'_>, module: &Bound<'_, PyModule>) -> PyResult<()> {
    module.add_class::<PyStudioClient>()?;
    module.add_class::<PyBehavior>()?;

    let enum_module = py.import("enum")?;
    let int_enum = enum_module.getattr("IntEnum")?;
    let members = PyDict::new(py);
    for keycode in Keycode::iter() {
        members.set_item(keycode.to_name(), keycode.to_hid_usage())?;
    }
    let keycode_enum = int_enum.call1(("Keycode", members))?;
    module.add("Keycode", keycode_enum)?;

    module.add_function(wrap_pyfunction!(key_press, module)?)?;
    module.add_function(wrap_pyfunction!(key_toggle, module)?)?;
    module.add_function(wrap_pyfunction!(layer_tap, module)?)?;
    module.add_function(wrap_pyfunction!(mod_tap, module)?)?;
    module.add_function(wrap_pyfunction!(sticky_key, module)?)?;
    module.add_function(wrap_pyfunction!(sticky_layer, module)?)?;
    module.add_function(wrap_pyfunction!(momentary_layer, module)?)?;
    module.add_function(wrap_pyfunction!(toggle_layer, module)?)?;
    module.add_function(wrap_pyfunction!(to_layer, module)?)?;
    module.add_function(wrap_pyfunction!(bluetooth, module)?)?;
    module.add_function(wrap_pyfunction!(external_power, module)?)?;
    module.add_function(wrap_pyfunction!(output_selection, module)?)?;
    module.add_function(wrap_pyfunction!(backlight, module)?)?;
    module.add_function(wrap_pyfunction!(underglow, module)?)?;
    module.add_function(wrap_pyfunction!(mouse_key_press, module)?)?;
    module.add_function(wrap_pyfunction!(mouse_move, module)?)?;
    module.add_function(wrap_pyfunction!(mouse_scroll, module)?)?;
    module.add_function(wrap_pyfunction!(caps_word, module)?)?;
    module.add_function(wrap_pyfunction!(key_repeat, module)?)?;
    module.add_function(wrap_pyfunction!(reset, module)?)?;
    module.add_function(wrap_pyfunction!(bootloader, module)?)?;
    module.add_function(wrap_pyfunction!(soft_off, module)?)?;
    module.add_function(wrap_pyfunction!(studio_unlock, module)?)?;
    module.add_function(wrap_pyfunction!(grave_escape, module)?)?;
    module.add_function(wrap_pyfunction!(transparent, module)?)?;
    module.add_function(wrap_pyfunction!(no_behavior, module)?)?;
    module.add_function(wrap_pyfunction!(raw, module)?)?;

    Ok(())
}