Skip to main content

Crate smed_client

Crate smed_client 

Source
Expand description

Client for smed, the SpaceMouse Enterprise driver daemon.

smed speaks JSON lines over a Unix socket (docs/api.md in the repository is the specification). This crate holds the message types both sides use, the table of key names and a small blocking client:

use smed_client::{Client, ServerMessage};

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" });
    }
}

An application that puts its commands on the keys 1–12 and the LCD (API 2) sends its catalogue and environments, says when its window has the focus, and runs the commands smed reports:

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

let mut client = Client::connect("example")?;
client.set_icon_svg("line", "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'>\
    <path d='M4 20 20 4' stroke='currentColor' stroke-width='2'/></svg>")?;
client.set_commands(
    &App::new("Example"),
    &[Command::new("sketch.line", "Line").with_icon("line"), Command::new("edit.undo", "Undo")],
)?;
client.set_environments(&[Environment::new("sketch", "Sketch")
    .with_key("1", "sketch.line")
    .with_key("9", "edit.undo")])?;
client.set_environment("sketch")?;
client.focus(true)?;
while let Some(message) = client.next_message()? {
    match message {
        ServerMessage::Command { id, .. } => println!("run {id}"),
        ServerMessage::Error { message } => eprintln!("smed: {message}"),
        _ => {}
    }
}

An application that reads the socket in its own loop pushes what it reads into a Lines decoder instead, which hands out whole messages.

The enums and structs are #[non_exhaustive], so later API versions can add message types, values, and fields of App, Command, Environment and DeviceInfo without breaking your code: match messages with a _ arm and their fields with .., build the structs with their new functions, and send client messages through the Client methods rather than building ClientMessage values. A new field in a message variant is still a breaking change of this crate.

Outside this crate, a struct cannot be built field by field:

ⓘ
let command = smed_client::Command {
    id: "edit.undo".into(),
    label: "Undo".into(),
    icon: None,
};

Structs§

App
The application as shown in the LCD’s header (set_commands).
Client
A connection to smed, after the handshake.
Command
One command of the client’s catalogue (set_commands).
DeviceInfo
The device smed has, as reported in state and device.
Environment
One environment of the client (set_environments): a name and which command each key runs there, by key name ("1"–"12") and command id.
Lines
Splits what smed sends into messages, without doing any I/O: for an application that reads the socket in its own loop (a read timeout, other work between reads). Client reads through it too.

Enums§

ClientMessage
What a client sends.
LineError
What Lines::next_message found wrong.
Press
Whether the device reported a key as pressed briefly or held.
ServerMessage
What smed sends.
TextSize
Text size on the LCD.
Theme
Colour theme of the LCD.

Constants§

API_VERSION
The highest API version this crate speaks.
KEYS
The printed keys and their 3Dx IDs (docs/protocol.md, “Key numbering”).
LOCK_KEY_ID
The 3Dx ID of the rotation-lock key.
MAX_MESSAGE
The largest message smed accepts, in bytes (one line, icons included).
MAX_SERVER_LINE
The longest line Lines accepts from smed. smed’s longest, a screen with the LCD page as a PNG, stays well below it.
SOCKET_IN_RUNTIME_DIR
The socket’s path below $XDG_RUNTIME_DIR.

Functions§

default_socket_path
Where smed listens for the current user: $XDG_RUNTIME_DIR/smed/api.sock, or None when XDG_RUNTIME_DIR is not set.
key_id
The 3Dx ID of the key named name.
key_name
The name of the key with 3Dx ID id, or "id<N>" for an ID not on the device’s keys.
to_line
One message as a line, \n included.