comparison src/main.rs @ 11:0193041f01d4

Split State into another module.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 01 Nov 2020 15:56:00 +0100
parents 06d77bb94a50
children d43c31aff57c
comparison
equal deleted inserted replaced
10:06d77bb94a50 11:0193041f01d4
19 use gtk::prelude::*; 19 use gtk::prelude::*;
20 20
21 use std::env::args; 21 use std::env::args;
22 use std::fs::{File, OpenOptions}; 22 use std::fs::{File, OpenOptions};
23 use std::io::ErrorKind; 23 use std::io::ErrorKind;
24 use std::sync::{Arc, Mutex}; 24 use std::sync::Arc;
25 25
26 use input_linux::{ 26 use input_linux::{
27 sys::input_event, sys::timeval, AbsoluteAxis, AbsoluteInfo, AbsoluteInfoSetup, EventKind, 27 sys::input_event, sys::timeval, AbsoluteAxis, AbsoluteInfo, AbsoluteInfoSetup, EventKind,
28 InputId, InputProperty, Key, MiscKind, SynchronizeKind, UInputHandle, 28 InputId, InputProperty, Key, MiscKind, SynchronizeKind, UInputHandle,
29 }; 29 };
30
31 mod state;
32 use state::State;
30 33
31 const WIDTH: i32 = 320; 34 const WIDTH: i32 = 320;
32 const HEIGHT: i32 = 180; 35 const HEIGHT: i32 = 180;
33 36
34 const MAX_X: i32 = 69920; 37 const MAX_X: i32 = 69920;
204 input_event_new(EventKind::Misc, code as u16, value) 207 input_event_new(EventKind::Misc, code as u16, value)
205 } 208 }
206 209
207 fn input_synchronize_new(code: SynchronizeKind, value: i32) -> input_event { 210 fn input_synchronize_new(code: SynchronizeKind, value: i32) -> input_event {
208 input_event_new(EventKind::Synchronize, code as u16, value) 211 input_event_new(EventKind::Synchronize, code as u16, value)
209 }
210
211 struct State {
212 dev: UInputHandle<File>,
213 width: f64,
214 height: f64,
215 selected_tool: Key,
216 pressed: bool,
217 }
218
219 impl State {
220 fn new() -> std::io::Result<Arc<Mutex<State>>> {
221 let dev = create_uinput_device()?;
222 println!(
223 "New device at {:?} ({:?})",
224 dev.evdev_path()?,
225 dev.sys_path()?
226 );
227
228 Ok(Arc::new(Mutex::new(State {
229 dev,
230 width: WIDTH as f64,
231 height: HEIGHT as f64,
232 selected_tool: Key::ButtonToolPen,
233 pressed: false,
234 })))
235 }
236
237 fn select_tool(&mut self, tool: Key) {
238 self.selected_tool = tool;
239 }
240
241 fn press(&mut self, x: f64, y: f64) -> std::io::Result<()> {
242 self.pressed = true;
243 self.dev.write(&[
244 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
245 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
246 input_axis_new(AbsoluteAxis::Z, 0),
247 input_axis_new(AbsoluteAxis::Wheel, 0),
248 input_axis_new(AbsoluteAxis::Pressure, 1024),
249 input_axis_new(AbsoluteAxis::Distance, 0),
250 input_axis_new(AbsoluteAxis::TiltX, 16),
251 input_axis_new(AbsoluteAxis::TiltY, 0),
252 input_misc_new(MiscKind::Serial, 0),
253 input_key_new(self.selected_tool, 1),
254 input_synchronize_new(SynchronizeKind::Report, 0),
255 ])?;
256 Ok(())
257 }
258
259 fn release(&mut self, x: f64, y: f64) -> std::io::Result<()> {
260 self.pressed = false;
261 self.dev.write(&[
262 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
263 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
264 input_axis_new(AbsoluteAxis::Z, 0),
265 input_axis_new(AbsoluteAxis::Wheel, 0),
266 input_axis_new(AbsoluteAxis::Pressure, 0),
267 input_axis_new(AbsoluteAxis::Distance, 16),
268 input_axis_new(AbsoluteAxis::TiltX, 16),
269 input_axis_new(AbsoluteAxis::TiltY, 0),
270 input_misc_new(MiscKind::Serial, 0),
271 input_key_new(self.selected_tool, 1),
272 input_synchronize_new(SynchronizeKind::Report, 0),
273 ])?;
274 Ok(())
275 }
276
277 fn motion(&mut self, x: f64, y: f64) -> std::io::Result<()> {
278 self.dev.write(&[
279 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
280 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
281 input_axis_new(AbsoluteAxis::Z, 0),
282 input_axis_new(AbsoluteAxis::Wheel, 0),
283 input_axis_new(AbsoluteAxis::Pressure, if self.pressed { 2048 } else { 0 }),
284 input_axis_new(AbsoluteAxis::Distance, if self.pressed { 0 } else { 32 }),
285 input_axis_new(AbsoluteAxis::TiltX, 16),
286 input_axis_new(AbsoluteAxis::TiltY, 0),
287 input_misc_new(MiscKind::Serial, 0),
288 input_key_new(self.selected_tool, 1),
289 input_synchronize_new(SynchronizeKind::Report, 0),
290 ])?;
291 Ok(())
292 }
293 } 212 }
294 213
295 fn build_main_menu(application: &gtk::Application) { 214 fn build_main_menu(application: &gtk::Application) {
296 let quit = gio::SimpleAction::new("quit", None); 215 let quit = gio::SimpleAction::new("quit", None);
297 application.set_accels_for_action("app.quit", &["<Control>q"]); 216 application.set_accels_for_action("app.quit", &["<Control>q"]);
392 ); 311 );
393 let state_weak = Arc::downgrade(&state); 312 let state_weak = Arc::downgrade(&state);
394 drawing_area.connect_configure_event(move |_, event| { 313 drawing_area.connect_configure_event(move |_, event| {
395 let state = state_weak.upgrade().unwrap(); 314 let state = state_weak.upgrade().unwrap();
396 let mut state = state.lock().unwrap(); 315 let mut state = state.lock().unwrap();
397 match event.get_size() { 316 state.set_size(event.get_size());
398 (width, height) => {
399 state.width = width as f64;
400 state.height = height as f64;
401 }
402 }
403 true 317 true
404 }); 318 });
405 let state_weak = Arc::downgrade(&state); 319 let state_weak = Arc::downgrade(&state);
406 drawing_area.connect_button_press_event(move |_, event| { 320 drawing_area.connect_button_press_event(move |_, event| {
407 if event.get_button() != 1 { 321 if event.get_button() != 1 {