Rust · 2622 bytes Raw Blame History
1 //! HyprKVM Common - Shared types and protocols
2 //!
3 //! This crate contains types shared between the daemon, CLI, and GUI.
4
5 pub mod protocol;
6
7 use serde::{Deserialize, Serialize};
8
9 /// Direction relative to the current machine
10 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11 #[serde(rename_all = "lowercase")]
12 pub enum Direction {
13 Left,
14 Right,
15 Up,
16 Down,
17 }
18
19 impl Direction {
20 /// Get the opposite direction
21 pub fn opposite(&self) -> Self {
22 match self {
23 Direction::Left => Direction::Right,
24 Direction::Right => Direction::Left,
25 Direction::Up => Direction::Down,
26 Direction::Down => Direction::Up,
27 }
28 }
29 }
30
31 impl std::fmt::Display for Direction {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 match self {
34 Direction::Left => write!(f, "left"),
35 Direction::Right => write!(f, "right"),
36 Direction::Up => write!(f, "up"),
37 Direction::Down => write!(f, "down"),
38 }
39 }
40 }
41
42 impl std::str::FromStr for Direction {
43 type Err = DirectionParseError;
44
45 fn from_str(s: &str) -> Result<Self, Self::Err> {
46 match s.to_lowercase().as_str() {
47 "left" | "l" => Ok(Direction::Left),
48 "right" | "r" => Ok(Direction::Right),
49 "up" | "u" => Ok(Direction::Up),
50 "down" | "d" => Ok(Direction::Down),
51 _ => Err(DirectionParseError(s.to_string())),
52 }
53 }
54 }
55
56 #[derive(Debug, thiserror::Error)]
57 #[error("Invalid direction: {0}")]
58 pub struct DirectionParseError(String);
59
60 /// Modifier key state
61 #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
62 pub struct ModifierState {
63 pub shift: bool,
64 pub ctrl: bool,
65 pub alt: bool,
66 pub super_key: bool,
67 pub caps_lock: bool,
68 pub num_lock: bool,
69 }
70
71 /// Key state
72 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
73 #[serde(rename_all = "lowercase")]
74 pub enum KeyState {
75 Pressed,
76 Released,
77 }
78
79 /// Mouse button
80 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
81 #[serde(rename_all = "lowercase")]
82 pub enum MouseButton {
83 Left,
84 Right,
85 Middle,
86 Side,
87 Extra,
88 Forward,
89 Back,
90 }
91
92 /// Button state
93 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
94 #[serde(rename_all = "lowercase")]
95 pub enum ButtonState {
96 Pressed,
97 Released,
98 }
99
100 /// Scroll axis
101 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
102 #[serde(rename_all = "lowercase")]
103 pub enum ScrollAxis {
104 Vertical,
105 Horizontal,
106 }
107