smed-client 0.2.0

Client for smed, the SpaceMouse Enterprise driver daemon (JSON-lines API)
Documentation

smed-client

Client for smed, a Linux driver daemon for the 3Dconnexion SpaceMouse Enterprise.

smed owns the device and reports it to any number of clients over a Unix socket at $XDG_RUNTIME_DIR/smed/api.sock, in JSON lines. It reports the six raw axes, every key by its 3Dconnexion button ID with short or long press, and the rotation lock, which clients can also set, and draws the LCD for the application that has the focus. This crate holds the message types both sides use, the table of key names and a small blocking client. The protocol is specified in docs/api.md.

use smed_client::{Client, ServerMessage};

fn main() -> std::io::Result<()> {
    let mut client = Client::connect("example")?;
    while let Some(message) = client.next_message()? {
        if let ServerMessage::Key { key, press, down, .. } = message {
            println!("{key} {press:?} {}", if down { "down" } else { "up" });
        }
    }
    Ok(())
}

With API 2, an application puts its commands on keys 1–12 and the device's LCD: it sends its command catalogue (labels, SVG or PNG icons) and environments, says when its window has the focus, and smed reports a command when a key with a command goes down:

use smed_client::{App, Client, Command, Environment, ServerMessage};

fn main() -> std::io::Result<()> {
    let mut client = Client::connect("example")?;
    client.set_commands(&App::new("Example"), &[Command::new("edit.undo", "Undo")])?;
    client.set_environments(&[Environment::new("main", "Main").with_key("1", "edit.undo")])?;
    client.set_environment("main")?;
    client.focus(true)?;
    while let Some(message) = client.next_message()? {
        if let ServerMessage::Command { id, .. } = message {
            println!("run {id}");
        }
    }
    Ok(())
}

The message enums and structs are #[non_exhaustive]: later API versions can add message types, values, and fields of the structs (App, Command, Environment, DeviceInfo) without breaking your code. Match messages with a _ arm and .., build the structs with new, and send through the Client methods. A new field in a message variant still needs a new minor version of this crate.

An application that reads the socket in its own loop (with a read timeout, or other work between reads) pushes what it reads into a Lines decoder, which hands out whole messages; Client reads through it too.

Not affiliated with or endorsed by 3Dconnexion.

Licence

MIT or Apache-2.0, at your option.