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). - Device
Info - The device smed has, as reported in
stateanddevice. - Environment
- One environment of the client (
set_environments): a name and which command each key runs there, by key name ("1"–"12") and commandid. - 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).
Clientreads through it too.
Enums§
- Client
Message - What a client sends.
- Line
Error - What
Lines::next_messagefound wrong. - Press
- Whether the device reported a key as pressed briefly or held.
- Server
Message - What smed sends.
- Text
Size - 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
Linesaccepts from smed. smed’s longest, ascreenwith 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, orNonewhenXDG_RUNTIME_DIRis 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,
\nincluded.