spice-client 0.2.0

A pure Rust SPICE client library with native and WebAssembly support
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Inputs channel implementation for keyboard and mouse events

use crate::channels::{Channel, ChannelConnection, InputEvent, KeyCode, MouseButton};
use crate::error::{Result, SpiceError};
use crate::protocol::*;
use tracing::{debug, error, info, warn};

/// Mouse operation mode
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MouseMode {
    Server,
    Client,
}

/// Inputs channel for sending keyboard and mouse events
pub struct InputsChannel {
    pub(crate) connection: ChannelConnection,
    mouse_mode: MouseMode,
    modifiers: KeyModifiers,
}

#[derive(Debug, Clone, Copy, Default)]
pub struct KeyModifiers {
    pub shift: bool,
    pub ctrl: bool,
    pub alt: bool,
    pub meta: bool,
}

impl InputsChannel {
    pub async fn new(host: &str, port: u16, channel_id: u8) -> Result<Self> {
        Self::new_with_connection_id(host, port, channel_id, None).await
    }

    pub async fn new_with_connection_id(
        host: &str,
        port: u16,
        channel_id: u8,
        connection_id: Option<u32>,
    ) -> Result<Self> {
        let mut connection =
            ChannelConnection::new(host, port, ChannelType::Inputs, channel_id).await?;
        if let Some(conn_id) = connection_id {
            connection.set_connection_id(conn_id);
        }
        connection.handshake().await?;

        Ok(Self {
            connection,
            mouse_mode: MouseMode::Server,
            modifiers: KeyModifiers::default(),
        })
    }

    #[cfg(target_arch = "wasm32")]
    pub async fn new_websocket(websocket_url: &str, channel_id: u8) -> Result<Self> {
        Self::new_websocket_with_auth(websocket_url, channel_id, None).await
    }

    #[cfg(target_arch = "wasm32")]
    pub async fn new_websocket_with_auth(
        websocket_url: &str,
        channel_id: u8,
        auth_token: Option<String>,
    ) -> Result<Self> {
        let mut connection = ChannelConnection::new_websocket_with_auth(
            websocket_url,
            ChannelType::Inputs,
            channel_id,
            auth_token,
        )
        .await?;
        connection.handshake().await?;

        Ok(Self {
            connection,
            mouse_mode: MouseMode::Server,
            modifiers: KeyModifiers::default(),
        })
    }

    #[cfg(target_arch = "wasm32")]
    pub async fn new_websocket_with_auth_and_session(
        websocket_url: &str,
        channel_id: u8,
        auth_token: Option<String>,
        password: Option<String>,
        connection_id: Option<u32>,
    ) -> Result<Self> {
        let mut connection = ChannelConnection::new_websocket_with_auth(
            websocket_url,
            ChannelType::Inputs,
            channel_id,
            auth_token,
        )
        .await?;
        if let Some(pwd) = password {
            connection.set_password(pwd);
        }
        if let Some(conn_id) = connection_id {
            connection.set_connection_id(conn_id);
        }
        connection.handshake().await?;

        Ok(Self {
            connection,
            mouse_mode: MouseMode::Server,
            modifiers: KeyModifiers::default(),
        })
    }

    pub async fn initialize(&mut self) -> Result<()> {
        info!("Inputs channel {} initialized", self.connection.channel_id);
        Ok(())
    }

    pub fn get_mouse_mode(&self) -> MouseMode {
        self.mouse_mode
    }

    pub fn get_modifiers(&self) -> KeyModifiers {
        self.modifiers
    }

    /// Sends an input event to the server
    pub async fn send_event(&mut self, event: InputEvent) -> Result<()> {
        match event {
            InputEvent::KeyDown(key) => {
                let scancode = key_to_scancode(key);
                self.update_modifiers(&key, true);
                self.send_key_down(scancode).await?
            }
            InputEvent::KeyUp(key) => {
                let scancode = key_to_scancode(key);
                self.update_modifiers(&key, false);
                self.send_key_up(scancode).await?
            }
            InputEvent::MouseMove { x, y } => self.send_mouse_motion(x, y).await?,
            InputEvent::MouseButton { button, pressed } => {
                self.send_mouse_button(button, pressed).await?
            }
        }
        Ok(())
    }

    /// Sends a key down event with scancode
    pub async fn send_key_down(&mut self, scancode: u32) -> Result<()> {
        let mut data = Vec::new();
        data.extend_from_slice(&scancode.to_le_bytes());

        self.connection
            .send_message(SPICE_MSG_INPUTS_KEY_DOWN, &data)
            .await?;
        debug!("Sent key down: scancode {}", scancode);
        Ok(())
    }

    /// Sends a key up event with scancode
    pub async fn send_key_up(&mut self, scancode: u32) -> Result<()> {
        let mut data = Vec::new();
        data.extend_from_slice(&scancode.to_le_bytes());

        self.connection
            .send_message(SPICE_MSG_INPUTS_KEY_UP, &data)
            .await?;
        debug!("Sent key up: scancode {}", scancode);
        Ok(())
    }

    /// Sends a mouse motion event
    pub async fn send_mouse_motion(&mut self, x: i32, y: i32) -> Result<()> {
        let mut data = Vec::new();
        data.extend_from_slice(&x.to_le_bytes());
        data.extend_from_slice(&y.to_le_bytes());
        data.extend_from_slice(&0u32.to_le_bytes()); // button state

        self.connection
            .send_message(SPICE_MSG_INPUTS_MOUSE_MOTION, &data)
            .await?;
        debug!("Sent mouse motion: ({}, {})", x, y);
        Ok(())
    }

    /// Sends a mouse button event
    pub async fn send_mouse_button(&mut self, button: MouseButton, pressed: bool) -> Result<()> {
        let button_mask = match button {
            MouseButton::Left => SPICE_MOUSE_BUTTON_LEFT,
            MouseButton::Middle => SPICE_MOUSE_BUTTON_MIDDLE,
            MouseButton::Right => SPICE_MOUSE_BUTTON_RIGHT,
            MouseButton::WheelUp => SPICE_MOUSE_BUTTON_WHEEL_UP,
            MouseButton::WheelDown => SPICE_MOUSE_BUTTON_WHEEL_DOWN,
        };

        let msg_type = if pressed {
            SPICE_MSG_INPUTS_MOUSE_PRESS
        } else {
            SPICE_MSG_INPUTS_MOUSE_RELEASE
        };

        let mut data = Vec::new();
        data.extend_from_slice(&button_mask.to_le_bytes());

        self.connection.send_message(msg_type, &data).await?;
        debug!(
            "Sent mouse button: {:?} {}",
            button,
            if pressed { "pressed" } else { "released" }
        );
        Ok(())
    }

    /// Updates modifier keys state
    fn update_modifiers(&mut self, key: &KeyCode, pressed: bool) {
        match key {
            KeyCode::Other(scancode) => {
                match *scancode {
                    0x2A | 0x36 => self.modifiers.shift = pressed, // Left/Right Shift
                    0x1D | 0x9D => self.modifiers.ctrl = pressed,  // Left/Right Ctrl
                    0x38 | 0xB8 => self.modifiers.alt = pressed,   // Left/Right Alt
                    0x5B | 0x5C => self.modifiers.meta = pressed,  // Left/Right Meta
                    _ => {}
                }
            }
            _ => {}
        }
    }

    pub async fn run(&mut self) -> Result<()> {
        loop {
            let (header, data) = self.connection.read_message().await?;
            self.handle_message(&header, &data).await?;
        }
    }

    async fn handle_init_message(&mut self, data: &[u8]) -> Result<()> {
        if data.len() >= 2 {
            let modifiers = u16::from_le_bytes([data[0], data[1]]);
            info!("Inputs init - modifiers: 0x{:04X}", modifiers);

            // Update modifier state based on init message
            self.modifiers.shift = (modifiers & SPICE_KEYBOARD_MODIFIER_SHIFT) != 0;
            self.modifiers.ctrl = (modifiers & SPICE_KEYBOARD_MODIFIER_CTRL) != 0;
            self.modifiers.alt = (modifiers & SPICE_KEYBOARD_MODIFIER_ALT) != 0;
        }
        Ok(())
    }

    async fn handle_modifiers_message(&mut self, data: &[u8]) -> Result<()> {
        if data.len() >= 2 {
            let modifiers = u16::from_le_bytes([data[0], data[1]]);
            debug!("Modifiers update: 0x{:04X}", modifiers);

            self.modifiers.shift = (modifiers & SPICE_KEYBOARD_MODIFIER_SHIFT) != 0;
            self.modifiers.ctrl = (modifiers & SPICE_KEYBOARD_MODIFIER_CTRL) != 0;
            self.modifiers.alt = (modifiers & SPICE_KEYBOARD_MODIFIER_ALT) != 0;
        }
        Ok(())
    }
}

impl Channel for InputsChannel {
    async fn handle_message(&mut self, header: &SpiceDataHeader, data: &[u8]) -> Result<()> {
        match header.msg_type {
            SPICE_MSG_INPUTS_INIT => {
                debug!("Received inputs init");
                self.handle_init_message(data).await?;
            }
            SPICE_MSG_INPUTS_KEY_MODIFIERS => {
                debug!("Received key modifiers");
                self.handle_modifiers_message(data).await?;
            }
            x if x == SPICE_MSG_SET_ACK => {
                debug!("Received SET_ACK message in inputs channel");
                if data.len() >= 4 {
                    let generation = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);
                    debug!("Inputs channel: SET_ACK generation: {}", generation);
                    // Send ACK_SYNC response
                    let ack_data = generation.to_le_bytes();
                    self.connection
                        .send_message(SPICE_MSGC_ACK_SYNC, &ack_data)
                        .await?;
                }
            }
            x if x == SPICE_MSG_PING => {
                debug!("Received PING message in inputs channel");
                if data.len() >= 4 {
                    let id = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);
                    let time = if data.len() >= 12 {
                        u64::from_le_bytes([
                            data[4], data[5], data[6], data[7], data[8], data[9], data[10],
                            data[11],
                        ])
                    } else {
                        0
                    };
                    // Send PONG response
                    let mut pong_data = Vec::with_capacity(12);
                    pong_data.extend_from_slice(&id.to_le_bytes());
                    pong_data.extend_from_slice(&time.to_le_bytes());
                    self.connection
                        .send_message(SPICE_MSGC_PONG, &pong_data)
                        .await?;
                }
            }
            x if x == SPICE_MSG_NOTIFY => {
                if data.len() >= 12 {
                    let severity = u32::from_le_bytes([data[4], data[5], data[6], data[7]]);
                    let msg_len =
                        u32::from_le_bytes([data[8], data[9], data[10], data[11]]) as usize;
                    if data.len() >= 12 + msg_len {
                        let message = String::from_utf8_lossy(&data[12..12 + msg_len]);
                        match severity {
                            0 => info!("Inputs server info: {}", message),
                            1 => warn!("Inputs server warning: {}", message),
                            2 => error!("Inputs server error: {}", message),
                            _ => debug!("Inputs server notification: {}", message),
                        }
                    }
                }
            }
            x if x == SPICE_MSG_DISCONNECTING => {
                info!("Inputs channel: Server is disconnecting");
                return Err(SpiceError::ConnectionClosed);
            }
            x if x == SPICE_MSG_MIGRATE => {
                warn!("Inputs channel: Migration not implemented");
            }
            x if x == SPICE_MSG_MIGRATE_DATA => {
                warn!("Inputs channel: Migration data not implemented");
            }
            x if x == SPICE_MSG_WAIT_FOR_CHANNELS => {
                warn!("Inputs channel: Wait for channels not implemented");
            }
            _ => {
                warn!("Unknown inputs message type: {}", header.msg_type);
            }
        }

        Ok(())
    }

    fn channel_type(&self) -> ChannelType {
        ChannelType::Inputs
    }
}

// Inputs channel message types
pub const SPICE_MSG_INPUTS_INIT: u16 = 101;
pub const SPICE_MSG_INPUTS_KEY_MODIFIERS: u16 = 102;

// Client to server messages
pub const SPICE_MSG_INPUTS_KEY_DOWN: u16 = 103;
pub const SPICE_MSG_INPUTS_KEY_UP: u16 = 104;
pub const SPICE_MSG_INPUTS_MOUSE_MOTION: u16 = 105;
pub const SPICE_MSG_INPUTS_MOUSE_POSITION: u16 = 106;
pub const SPICE_MSG_INPUTS_MOUSE_PRESS: u16 = 107;
pub const SPICE_MSG_INPUTS_MOUSE_RELEASE: u16 = 108;

// Mouse button masks
pub const SPICE_MOUSE_BUTTON_LEFT: u32 = 1 << 0;
pub const SPICE_MOUSE_BUTTON_MIDDLE: u32 = 1 << 1;
pub const SPICE_MOUSE_BUTTON_RIGHT: u32 = 1 << 2;
pub const SPICE_MOUSE_BUTTON_WHEEL_UP: u32 = 1 << 3; // Button 4
pub const SPICE_MOUSE_BUTTON_WHEEL_DOWN: u32 = 1 << 4; // Button 5

// Keyboard modifier masks
pub const SPICE_KEYBOARD_MODIFIER_SHIFT: u16 = 1 << 0;
pub const SPICE_KEYBOARD_MODIFIER_CTRL: u16 = 1 << 1;
pub const SPICE_KEYBOARD_MODIFIER_ALT: u16 = 1 << 2;

/// Converts a KeyCode to a PC scancode
fn key_to_scancode(key: KeyCode) -> u32 {
    match key {
        KeyCode::Escape => 0x01,
        KeyCode::Enter => 0x1C,
        KeyCode::Space => 0x39,
        KeyCode::Tab => 0x0F,
        KeyCode::Backspace => 0x0E,
        KeyCode::Function(n) => match n {
            1 => 0x3B,
            2 => 0x3C,
            3 => 0x3D,
            4 => 0x3E,
            5 => 0x3F,
            6 => 0x40,
            7 => 0x41,
            8 => 0x42,
            9 => 0x43,
            10 => 0x44,
            11 => 0x57,
            12 => 0x58,
            _ => 0x00,
        },
        KeyCode::ArrowUp => 0x48,
        KeyCode::ArrowDown => 0x50,
        KeyCode::ArrowLeft => 0x4B,
        KeyCode::ArrowRight => 0x4D,
        KeyCode::Char(c) => char_to_scancode(c),
        KeyCode::Other(scancode) => scancode,
    }
}

/// Converts a character to a PC scancode
fn char_to_scancode(c: char) -> u32 {
    match c.to_ascii_uppercase() {
        'A' => 0x1E,
        'B' => 0x30,
        'C' => 0x2E,
        'D' => 0x20,
        'E' => 0x12,
        'F' => 0x21,
        'G' => 0x22,
        'H' => 0x23,
        'I' => 0x17,
        'J' => 0x24,
        'K' => 0x25,
        'L' => 0x26,
        'M' => 0x32,
        'N' => 0x31,
        'O' => 0x18,
        'P' => 0x19,
        'Q' => 0x10,
        'R' => 0x13,
        'S' => 0x1F,
        'T' => 0x14,
        'U' => 0x16,
        'V' => 0x2F,
        'W' => 0x11,
        'X' => 0x2D,
        'Y' => 0x15,
        'Z' => 0x2C,
        '1' => 0x02,
        '2' => 0x03,
        '3' => 0x04,
        '4' => 0x05,
        '5' => 0x06,
        '6' => 0x07,
        '7' => 0x08,
        '8' => 0x09,
        '9' => 0x0A,
        '0' => 0x0B,
        '-' => 0x0C,
        '=' => 0x0D,
        '[' => 0x1A,
        ']' => 0x1B,
        ';' => 0x27,
        '\'' => 0x28,
        ',' => 0x33,
        '.' => 0x34,
        '/' => 0x35,
        '\\' => 0x2B,
        '`' => 0x29,
        _ => 0x00, // Unknown
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_key_to_scancode() {
        assert_eq!(key_to_scancode(KeyCode::Escape), 0x01);
        assert_eq!(key_to_scancode(KeyCode::Enter), 0x1C);
        assert_eq!(key_to_scancode(KeyCode::Space), 0x39);
        assert_eq!(key_to_scancode(KeyCode::Char('A')), 0x1E);
        assert_eq!(key_to_scancode(KeyCode::Char('a')), 0x1E);
        assert_eq!(key_to_scancode(KeyCode::Other(0x42)), 0x42);
    }

    #[test]
    fn test_modifiers() {
        let mut modifiers = KeyModifiers::default();
        assert!(!modifiers.shift);
        assert!(!modifiers.ctrl);
        assert!(!modifiers.alt);
        assert!(!modifiers.meta);

        modifiers.shift = true;
        modifiers.ctrl = true;
        assert!(modifiers.shift);
        assert!(modifiers.ctrl);
    }
}