leejunyoung5819wayland-android-remote

공개

Ubuntu(Wayland/X11)에서 Android 기기 화면 미러링 및 원격 제어 (Rust)

220줄7.0 KB
1//! egui 키를 Android `KeyEvent` 값(`KEYCODE_*`, `META_*`)으로 바꾼다.
2//!
3//! 글자를 만드는 키는 TEXT_INPUT으로 보내므로 여기서 다루지 않는다.
4//! 예외로 Ctrl·Alt와 함께 누른 문자·숫자 키는 [`combo_keycode`]로 키 이벤트를 만든다.
5
6use egui::{Key, Modifiers};
7use wayland_android_remote::protocol::SystemAction;
8
9/// Android `KeyEvent.KEYCODE_SPACE`.
10pub const KEYCODE_SPACE: u16 = 62;
11
12/// `META_SHIFT_ON | META_SHIFT_LEFT_ON`.
13pub const META_SHIFT: u32 = 0x41;
14/// `META_ALT_ON | META_ALT_LEFT_ON`.
15pub const META_ALT: u32 = 0x12;
16/// `META_CTRL_ON | META_CTRL_LEFT_ON`.
17pub const META_CTRL: u32 = 0x3000;
18
19/// 글자를 만들지 않는 키의 Android 키코드. 스페이스는 글자를 만들 수 있어 따로 처리한다.
20pub fn special_keycode(key: Key) -> Option<u16> {
21 Some(match key {
22 Key::Enter => 66,
23 Key::Backspace => 67, // KEYCODE_DEL
24 Key::Delete => 112, // KEYCODE_FORWARD_DEL
25 Key::Tab => 61,
26 Key::Escape => 111,
27 Key::ArrowUp => 19,
28 Key::ArrowDown => 20,
29 Key::ArrowLeft => 21,
30 Key::ArrowRight => 22,
31 Key::Home => 122, // KEYCODE_MOVE_HOME
32 Key::End => 123, // KEYCODE_MOVE_END
33 Key::PageUp => 92,
34 Key::PageDown => 93,
35 Key::Insert => 124,
36 Key::F1 => 131,
37 Key::F2 => 132,
38 Key::F3 => 133,
39 Key::F4 => 134,
40 Key::F5 => 135,
41 Key::F6 => 136,
42 Key::F7 => 137,
43 Key::F8 => 138,
44 Key::F9 => 139,
45 Key::F10 => 140,
46 Key::F11 => 141,
47 Key::F12 => 142,
48 _ => return None,
49 })
50}
51
52/// 문자·숫자 키의 Android 키코드 (A=29..Z=54, 0=7..9=16).
53pub fn letter_digit_keycode(key: Key) -> Option<u16> {
54 const LETTERS: [Key; 26] = [
55 Key::A,
56 Key::B,
57 Key::C,
58 Key::D,
59 Key::E,
60 Key::F,
61 Key::G,
62 Key::H,
63 Key::I,
64 Key::J,
65 Key::K,
66 Key::L,
67 Key::M,
68 Key::N,
69 Key::O,
70 Key::P,
71 Key::Q,
72 Key::R,
73 Key::S,
74 Key::T,
75 Key::U,
76 Key::V,
77 Key::W,
78 Key::X,
79 Key::Y,
80 Key::Z,
81 ];
82 const DIGITS: [Key; 10] = [
83 Key::Num0,
84 Key::Num1,
85 Key::Num2,
86 Key::Num3,
87 Key::Num4,
88 Key::Num5,
89 Key::Num6,
90 Key::Num7,
91 Key::Num8,
92 Key::Num9,
93 ];
94 if let Some(i) = LETTERS.iter().position(|&k| k == key) {
95 return Some(29 + i as u16);
96 }
97 DIGITS.iter().position(|&k| k == key).map(|i| 7 + i as u16)
98}
99
100/// Ctrl·Alt 조합일 때만 문자·숫자 키를 키 이벤트로 보낸다. 그 밖에는 TEXT_INPUT이 맡는다.
101pub fn combo_keycode(key: Key, modifiers: Modifiers) -> Option<u16> {
102 if modifiers.ctrl || modifiers.alt {
103 letter_digit_keycode(key)
104 } else {
105 None
106 }
107}
108
109/// egui 보조키 상태를 Android `META_*` 비트로 바꾼다. 왼쪽/오른쪽 구분이 없어 왼쪽으로 둔다.
110pub fn meta_state(modifiers: Modifiers) -> u32 {
111 let mut meta = 0;
112 if modifiers.shift {
113 meta |= META_SHIFT;
114 }
115 if modifiers.alt {
116 meta |= META_ALT;
117 }
118 if modifiers.ctrl {
119 meta |= META_CTRL;
120 }
121 meta
122}
123
124/// Ctrl+C/V/X/A (다른 보조키 없이)와 전용 복사·붙여넣기·잘라내기 키를 시스템 동작으로 바꾼다.
125pub fn clipboard_action(key: Key, modifiers: Modifiers) -> Option<SystemAction> {
126 match key {
127 Key::Copy => return Some(SystemAction::Copy),
128 Key::Paste => return Some(SystemAction::Paste),
129 Key::Cut => return Some(SystemAction::Cut),
130 _ => {}
131 }
132 if !modifiers.ctrl || modifiers.alt || modifiers.shift {
133 return None;
134 }
135 match key {
136 Key::C => Some(SystemAction::Copy),
137 Key::V => Some(SystemAction::Paste),
138 Key::X => Some(SystemAction::Cut),
139 Key::A => Some(SystemAction::SelectAll),
140 _ => None,
141 }
142}
143
144#[cfg(test)]
145mod tests {
146 use super::*;
147
148 #[test]
149 fn special_keys_map_to_android_keycodes() {
150 assert_eq!(special_keycode(Key::Enter), Some(66));
151 assert_eq!(special_keycode(Key::Backspace), Some(67));
152 assert_eq!(special_keycode(Key::Delete), Some(112));
153 assert_eq!(special_keycode(Key::Tab), Some(61));
154 assert_eq!(special_keycode(Key::Escape), Some(111));
155 assert_eq!(special_keycode(Key::ArrowUp), Some(19));
156 assert_eq!(special_keycode(Key::ArrowDown), Some(20));
157 assert_eq!(special_keycode(Key::ArrowLeft), Some(21));
158 assert_eq!(special_keycode(Key::ArrowRight), Some(22));
159 assert_eq!(special_keycode(Key::Home), Some(122));
160 assert_eq!(special_keycode(Key::End), Some(123));
161 assert_eq!(special_keycode(Key::PageUp), Some(92));
162 assert_eq!(special_keycode(Key::PageDown), Some(93));
163 assert_eq!(special_keycode(Key::Insert), Some(124));
164 assert_eq!(special_keycode(Key::F1), Some(131));
165 assert_eq!(special_keycode(Key::F12), Some(142));
166 }
167
168 #[test]
169 fn text_keys_are_not_special() {
170 for key in [Key::A, Key::Num1, Key::Space, Key::Minus, Key::Comma] {
171 assert_eq!(special_keycode(key), None, "{key:?}");
172 }
173 }
174
175 #[test]
176 fn letters_and_digits() {
177 assert_eq!(letter_digit_keycode(Key::A), Some(29));
178 assert_eq!(letter_digit_keycode(Key::Z), Some(54));
179 assert_eq!(letter_digit_keycode(Key::Num0), Some(7));
180 assert_eq!(letter_digit_keycode(Key::Num9), Some(16));
181 assert_eq!(letter_digit_keycode(Key::Enter), None);
182 }
183
184 #[test]
185 fn combo_needs_ctrl_or_alt() {
186 assert_eq!(combo_keycode(Key::Z, Modifiers::NONE), None);
187 assert_eq!(combo_keycode(Key::Z, Modifiers::SHIFT), None);
188 assert_eq!(combo_keycode(Key::Z, Modifiers::CTRL), Some(54));
189 assert_eq!(combo_keycode(Key::Num1, Modifiers::ALT), Some(8));
190 }
191
192 #[test]
193 fn meta_bits() {
194 assert_eq!(meta_state(Modifiers::NONE), 0);
195 assert_eq!(meta_state(Modifiers::SHIFT), 0x41);
196 assert_eq!(meta_state(Modifiers::ALT), 0x12);
197 assert_eq!(meta_state(Modifiers::CTRL), 0x3000);
198 assert_eq!(meta_state(Modifiers::CTRL | Modifiers::SHIFT), 0x3041);
199 }
200
201 #[test]
202 fn clipboard_shortcuts() {
203 let ctrl = Modifiers::CTRL;
204 assert_eq!(clipboard_action(Key::C, ctrl), Some(SystemAction::Copy));
205 assert_eq!(clipboard_action(Key::V, ctrl), Some(SystemAction::Paste));
206 assert_eq!(clipboard_action(Key::X, ctrl), Some(SystemAction::Cut));
207 assert_eq!(
208 clipboard_action(Key::A, ctrl),
209 Some(SystemAction::SelectAll)
210 );
211 assert_eq!(clipboard_action(Key::Z, ctrl), None);
212 assert_eq!(clipboard_action(Key::C, Modifiers::NONE), None);
213 assert_eq!(clipboard_action(Key::C, ctrl | Modifiers::SHIFT), None);
214 assert_eq!(clipboard_action(Key::C, ctrl | Modifiers::ALT), None);
215 assert_eq!(
216 clipboard_action(Key::Copy, Modifiers::NONE),
217 Some(SystemAction::Copy)
218 );
219 }
220}