Mercurial > tablet-emu
annotate src/main.rs @ 1:6dbe2bbeef70
Improve input event types with helper functions.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 09 Oct 2020 00:18:05 +0200 |
parents | 816237b684ea |
children | 39f87ef69b2a |
rev | line source |
---|---|
0 | 1 use gio::prelude::*; |
2 use gtk::prelude::*; | |
3 | |
4 use std::env::args; | |
5 use std::fs::{File, OpenOptions}; | |
6 use std::io::ErrorKind; | |
7 use std::sync::{Arc, Mutex}; | |
8 | |
9 use input_linux::{ | |
10 sys::input_event, sys::timeval, AbsoluteAxis, AbsoluteInfo, AbsoluteInfoSetup, EventKind, | |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
11 InputId, InputProperty, Key, MiscKind, SynchronizeKind, UInputHandle, |
0 | 12 }; |
13 | |
14 const WIDTH: i32 = 320; | |
15 const HEIGHT: i32 = 180; | |
16 | |
17 const MAX_X: i32 = 69920; | |
18 const MAX_Y: i32 = 39980; | |
19 | |
20 fn create_uinput_device() -> std::io::Result<UInputHandle<File>> { | |
21 let file = OpenOptions::new().write(true).open("/dev/uinput")?; | |
22 let dev = UInputHandle::new(file); | |
23 | |
24 dev.set_evbit(EventKind::Synchronize)?; | |
25 dev.set_evbit(EventKind::Key)?; | |
26 dev.set_evbit(EventKind::Absolute)?; | |
27 dev.set_evbit(EventKind::Misc)?; | |
28 dev.set_keybit(Key::ButtonToolPen)?; | |
29 dev.set_keybit(Key::ButtonToolRubber)?; | |
30 dev.set_keybit(Key::ButtonToolBrush)?; | |
31 dev.set_keybit(Key::ButtonToolPencil)?; | |
32 dev.set_keybit(Key::ButtonToolAirbrush)?; | |
33 dev.set_keybit(Key::ButtonTouch)?; | |
34 dev.set_keybit(Key::ButtonStylus)?; | |
35 dev.set_keybit(Key::ButtonStylus2)?; | |
36 dev.set_keybit(Key::ButtonStylus3)?; | |
37 dev.set_mscbit(MiscKind::Serial)?; | |
38 dev.set_propbit(InputProperty::Direct)?; | |
39 | |
40 dev.set_absbit(AbsoluteAxis::X)?; | |
41 dev.set_absbit(AbsoluteAxis::Y)?; | |
42 dev.set_absbit(AbsoluteAxis::Z)?; | |
43 dev.set_absbit(AbsoluteAxis::Wheel)?; | |
44 dev.set_absbit(AbsoluteAxis::Pressure)?; | |
45 dev.set_absbit(AbsoluteAxis::Distance)?; | |
46 dev.set_absbit(AbsoluteAxis::TiltX)?; | |
47 dev.set_absbit(AbsoluteAxis::TiltY)?; | |
48 dev.set_absbit(AbsoluteAxis::Misc)?; | |
49 | |
50 let id = InputId { | |
51 bustype: 3, | |
52 vendor: 0x56a, | |
53 product: 0x350, | |
54 version: 0xb, | |
55 }; | |
56 | |
57 let x = AbsoluteInfoSetup { | |
58 axis: AbsoluteAxis::X, | |
59 info: AbsoluteInfo { | |
60 value: 0, | |
61 minimum: 0, | |
62 maximum: MAX_X, | |
63 fuzz: 0, | |
64 flat: 0, | |
65 resolution: 200, | |
66 }, | |
67 }; | |
68 let y = AbsoluteInfoSetup { | |
69 axis: AbsoluteAxis::Y, | |
70 info: AbsoluteInfo { | |
71 value: 0, | |
72 minimum: 0, | |
73 maximum: MAX_Y, | |
74 fuzz: 0, | |
75 flat: 0, | |
76 resolution: 200, | |
77 }, | |
78 }; | |
79 let z = AbsoluteInfoSetup { | |
80 axis: AbsoluteAxis::Z, | |
81 info: AbsoluteInfo { | |
82 value: 0, | |
83 minimum: -900, | |
84 maximum: 899, | |
85 fuzz: 0, | |
86 flat: 0, | |
87 resolution: 287, | |
88 }, | |
89 }; | |
90 let wheel = AbsoluteInfoSetup { | |
91 axis: AbsoluteAxis::Wheel, | |
92 info: AbsoluteInfo { | |
93 value: 0, | |
94 minimum: 0, | |
95 maximum: 2047, | |
96 fuzz: 0, | |
97 flat: 0, | |
98 resolution: 0, | |
99 }, | |
100 }; | |
101 let pressure = AbsoluteInfoSetup { | |
102 axis: AbsoluteAxis::Pressure, | |
103 info: AbsoluteInfo { | |
104 value: 0, | |
105 minimum: 0, | |
106 maximum: 8196, | |
107 fuzz: 0, | |
108 flat: 0, | |
109 resolution: 0, | |
110 }, | |
111 }; | |
112 let distance = AbsoluteInfoSetup { | |
113 axis: AbsoluteAxis::Distance, | |
114 info: AbsoluteInfo { | |
115 value: 0, | |
116 minimum: 0, | |
117 maximum: 63, | |
118 fuzz: 0, | |
119 flat: 0, | |
120 resolution: 0, | |
121 }, | |
122 }; | |
123 let tilt_x = AbsoluteInfoSetup { | |
124 axis: AbsoluteAxis::TiltX, | |
125 info: AbsoluteInfo { | |
126 value: 0, | |
127 minimum: -64, | |
128 maximum: 63, | |
129 fuzz: 0, | |
130 flat: 0, | |
131 resolution: 57, | |
132 }, | |
133 }; | |
134 let tilt_y = AbsoluteInfoSetup { | |
135 axis: AbsoluteAxis::TiltY, | |
136 info: AbsoluteInfo { | |
137 value: 0, | |
138 minimum: -64, | |
139 maximum: 63, | |
140 fuzz: 0, | |
141 flat: 0, | |
142 resolution: 57, | |
143 }, | |
144 }; | |
145 let misc = AbsoluteInfoSetup { | |
146 axis: AbsoluteAxis::Misc, | |
147 info: AbsoluteInfo { | |
148 value: 0, | |
149 minimum: 0, | |
150 maximum: 0, | |
151 fuzz: 0, | |
152 flat: 0, | |
153 resolution: 0, | |
154 }, | |
155 }; | |
156 | |
157 dev.create( | |
158 &id, | |
159 b"TabletEmu", | |
160 0, | |
161 &[x, y, z, wheel, pressure, distance, tilt_x, tilt_y, misc], | |
162 )?; | |
163 Ok(dev) | |
164 } | |
165 | |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
166 fn input_event_new(type_: EventKind, code: u16, value: i32) -> input_event { |
0 | 167 input_event { |
168 time: timeval { | |
169 tv_sec: 0, | |
170 tv_usec: 0, | |
171 }, | |
172 type_: type_ as u16, | |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
173 code, |
0 | 174 value, |
175 } | |
176 } | |
177 | |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
178 fn input_axis_new(code: AbsoluteAxis, value: i32) -> input_event { |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
179 input_event_new(EventKind::Absolute, code as u16, value) |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
180 } |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
181 |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
182 fn input_misc_new(code: MiscKind, value: i32) -> input_event { |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
183 input_event_new(EventKind::Absolute, code as u16, value) |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
184 } |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
185 |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
186 fn input_synchronize_new(code: SynchronizeKind, value: i32) -> input_event { |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
187 input_event_new(EventKind::Absolute, code as u16, value) |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
188 } |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
189 |
0 | 190 fn build_ui(application: >k::Application) { |
191 let dev = match create_uinput_device() { | |
192 Ok(dev) => Arc::new(dev), | |
193 Err(err) => { | |
194 match err.kind() { | |
195 ErrorKind::NotFound => { | |
196 eprintln!("Couldn’t find /dev/uinput: {}", err); | |
197 eprintln!("Maybe you forgot to `modprobe uinput`?"); | |
198 } | |
199 ErrorKind::PermissionDenied => { | |
200 eprintln!("Couldn’t open /dev/uinput for writing: {}", err); | |
201 eprintln!("Maybe you aren’t allowed to create input devices?"); | |
202 } | |
203 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err), | |
204 } | |
205 return; | |
206 } | |
207 }; | |
208 println!( | |
209 "New device at {:?} ({:?})", | |
210 dev.evdev_path().unwrap(), | |
211 dev.sys_path().unwrap() | |
212 ); | |
213 | |
214 let window = gtk::ApplicationWindow::new(application); | |
215 | |
216 window.set_title("tablet-emu"); | |
217 window.set_position(gtk::WindowPosition::Center); | |
218 | |
219 let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0); | |
220 let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0); | |
221 | |
222 let tool_name = Arc::new(Mutex::new(String::from("Pen"))); | |
223 let tool1 = gtk::Button::with_label("Pen"); | |
224 let tool2 = gtk::Button::with_label("Rubber"); | |
225 let tool3 = gtk::Button::with_label("Brush"); | |
226 let tool4 = gtk::Button::with_label("Pencil"); | |
227 let tool5 = gtk::Button::with_label("Airbrush"); | |
228 | |
229 macro_rules! impl_tool_signal { | |
230 ($tool:ident) => { | |
231 let tool_name_weak = Arc::downgrade(&tool_name); | |
232 $tool.connect_clicked(move |b| { | |
233 let tool_name = tool_name_weak.upgrade().unwrap(); | |
234 let mut tool_name = tool_name.lock().unwrap(); | |
235 *tool_name = b.get_label().unwrap().to_string(); | |
236 }); | |
237 }; | |
238 }; | |
239 impl_tool_signal!(tool1); | |
240 impl_tool_signal!(tool2); | |
241 impl_tool_signal!(tool3); | |
242 impl_tool_signal!(tool4); | |
243 impl_tool_signal!(tool5); | |
244 | |
245 tools_box.add(&tool1); | |
246 tools_box.add(&tool2); | |
247 tools_box.add(&tool3); | |
248 tools_box.add(&tool4); | |
249 tools_box.add(&tool5); | |
250 | |
251 let current_size = Arc::new(Mutex::new((WIDTH as f64, HEIGHT as f64))); | |
252 let pressed = Arc::new(Mutex::new(false)); | |
253 | |
254 let drawing_area = gtk::DrawingArea::new(); | |
255 drawing_area.set_size_request(WIDTH, HEIGHT); | |
256 drawing_area.set_hexpand(true); | |
257 drawing_area.set_events( | |
258 gdk::EventMask::BUTTON_PRESS_MASK | |
259 | gdk::EventMask::BUTTON_RELEASE_MASK | |
260 | gdk::EventMask::POINTER_MOTION_MASK, | |
261 ); | |
262 let current_size_weak = Arc::downgrade(¤t_size); | |
263 drawing_area.connect_configure_event(move |_, event| { | |
264 let current_size = current_size_weak.upgrade().unwrap(); | |
265 let mut current_size = current_size.lock().unwrap(); | |
266 *current_size = match event.get_size() { | |
267 (width, height) => (width as f64, height as f64), | |
268 }; | |
269 true | |
270 }); | |
271 let dev_weak = Arc::downgrade(&dev); | |
272 let current_size_weak = Arc::downgrade(¤t_size); | |
273 let pressed_weak = Arc::downgrade(&pressed); | |
274 drawing_area.connect_button_press_event(move |_, event| { | |
275 if event.get_button() != 1 { | |
276 return Inhibit(false); | |
277 } | |
278 | |
279 let dev = dev_weak.upgrade().unwrap(); | |
280 let pressed = pressed_weak.upgrade().unwrap(); | |
281 let mut pressed = pressed.lock().unwrap(); | |
282 *pressed = true; | |
283 let (x, y) = event.get_position(); | |
284 println!("press tool {} at {}, {}", tool_name.lock().unwrap(), x, y); | |
285 let current_size = current_size_weak.upgrade().unwrap(); | |
286 let current_size = current_size.lock().unwrap(); | |
287 let (width, height) = *current_size; | |
288 dev.write(&[ | |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
289 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
290 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / height) as i32), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
291 input_axis_new(AbsoluteAxis::Z, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
292 input_axis_new(AbsoluteAxis::Wheel, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
293 input_axis_new(AbsoluteAxis::Pressure, 1024), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
294 input_axis_new(AbsoluteAxis::Distance, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
295 input_axis_new(AbsoluteAxis::TiltX, 16), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
296 input_axis_new(AbsoluteAxis::TiltY, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
297 input_misc_new(MiscKind::Serial, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
298 input_synchronize_new(SynchronizeKind::Report, 0), |
0 | 299 ]) |
300 .unwrap(); | |
301 Inhibit(false) | |
302 }); | |
303 let dev_weak = Arc::downgrade(&dev); | |
304 let current_size_weak = Arc::downgrade(¤t_size); | |
305 let pressed_weak = Arc::downgrade(&pressed); | |
306 drawing_area.connect_button_release_event(move |_, event| { | |
307 if event.get_button() != 1 { | |
308 return Inhibit(false); | |
309 } | |
310 | |
311 let dev = dev_weak.upgrade().unwrap(); | |
312 let (x, y) = event.get_position(); | |
313 let pressed = pressed_weak.upgrade().unwrap(); | |
314 let mut pressed = pressed.lock().unwrap(); | |
315 *pressed = false; | |
316 //println!("release {}, {}", x, y); | |
317 let current_size = current_size_weak.upgrade().unwrap(); | |
318 let current_size = current_size.lock().unwrap(); | |
319 let (width, height) = *current_size; | |
320 dev.write(&[ | |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
321 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
322 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / height) as i32), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
323 input_axis_new(AbsoluteAxis::Z, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
324 input_axis_new(AbsoluteAxis::Wheel, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
325 input_axis_new(AbsoluteAxis::Pressure, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
326 input_axis_new(AbsoluteAxis::Distance, 16), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
327 input_axis_new(AbsoluteAxis::TiltX, 16), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
328 input_axis_new(AbsoluteAxis::TiltY, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
329 input_misc_new(MiscKind::Serial, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
330 input_synchronize_new(SynchronizeKind::Report, 0), |
0 | 331 ]) |
332 .unwrap(); | |
333 Inhibit(false) | |
334 }); | |
335 drawing_area.connect_motion_notify_event(move |_, event| { | |
336 let (x, y) = event.get_position(); | |
337 let pressed = pressed.lock().unwrap(); | |
338 //println!("motion {}, {}", x, y); | |
339 let current_size = current_size.lock().unwrap(); | |
340 let (width, height) = *current_size; | |
341 dev.write(&[ | |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
342 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
343 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / height) as i32), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
344 input_axis_new(AbsoluteAxis::Z, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
345 input_axis_new(AbsoluteAxis::Wheel, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
346 input_axis_new(AbsoluteAxis::Pressure, if *pressed { 2048 } else { 0 }), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
347 input_axis_new(AbsoluteAxis::Distance, if *pressed { 0 } else { 32 }), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
348 input_axis_new(AbsoluteAxis::TiltX, 16), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
349 input_axis_new(AbsoluteAxis::TiltY, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
350 input_misc_new(MiscKind::Serial, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
351 input_synchronize_new(SynchronizeKind::Report, 0), |
0 | 352 ]) |
353 .unwrap(); | |
354 Inhibit(false) | |
355 }); | |
356 drawing_area.connect_draw(move |_, ctx| { | |
357 //println!("drawing {}", drawing_area); | |
358 ctx.set_source_rgb(1., 0., 0.); | |
359 ctx.set_operator(cairo::Operator::Screen); | |
360 ctx.paint(); | |
361 Inhibit(false) | |
362 }); | |
363 | |
364 hbox.add(&tools_box); | |
365 hbox.add(&drawing_area); | |
366 | |
367 window.add(&hbox); | |
368 | |
369 window.show_all(); | |
370 } | |
371 | |
372 fn main() { | |
373 let application = gtk::Application::new( | |
374 Some("fr.linkmauve.TabletEmu"), | |
375 gio::ApplicationFlags::empty(), | |
376 ) | |
377 .expect("Initialisation failed…"); | |
378 application.connect_activate(build_ui); | |
379 application.run(&args().collect::<Vec<_>>()); | |
380 } |