Mercurial > tablet-emu
diff 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 |
line wrap: on
line diff
--- a/src/main.rs +++ b/src/main.rs @@ -21,13 +21,16 @@ use gtk::prelude::*; use std::env::args; use std::fs::{File, OpenOptions}; use std::io::ErrorKind; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use input_linux::{ sys::input_event, sys::timeval, AbsoluteAxis, AbsoluteInfo, AbsoluteInfoSetup, EventKind, InputId, InputProperty, Key, MiscKind, SynchronizeKind, UInputHandle, }; +mod state; +use state::State; + const WIDTH: i32 = 320; const HEIGHT: i32 = 180; @@ -208,90 +211,6 @@ fn input_synchronize_new(code: Synchroni input_event_new(EventKind::Synchronize, code as u16, value) } -struct State { - dev: UInputHandle<File>, - width: f64, - height: f64, - selected_tool: Key, - pressed: bool, -} - -impl State { - fn new() -> std::io::Result<Arc<Mutex<State>>> { - let dev = create_uinput_device()?; - println!( - "New device at {:?} ({:?})", - dev.evdev_path()?, - dev.sys_path()? - ); - - Ok(Arc::new(Mutex::new(State { - dev, - width: WIDTH as f64, - height: HEIGHT as f64, - selected_tool: Key::ButtonToolPen, - pressed: false, - }))) - } - - fn select_tool(&mut self, tool: Key) { - self.selected_tool = tool; - } - - fn press(&mut self, x: f64, y: f64) -> std::io::Result<()> { - self.pressed = true; - self.dev.write(&[ - input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32), - input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32), - input_axis_new(AbsoluteAxis::Z, 0), - input_axis_new(AbsoluteAxis::Wheel, 0), - input_axis_new(AbsoluteAxis::Pressure, 1024), - input_axis_new(AbsoluteAxis::Distance, 0), - input_axis_new(AbsoluteAxis::TiltX, 16), - input_axis_new(AbsoluteAxis::TiltY, 0), - input_misc_new(MiscKind::Serial, 0), - input_key_new(self.selected_tool, 1), - input_synchronize_new(SynchronizeKind::Report, 0), - ])?; - Ok(()) - } - - fn release(&mut self, x: f64, y: f64) -> std::io::Result<()> { - self.pressed = false; - self.dev.write(&[ - input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32), - input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32), - input_axis_new(AbsoluteAxis::Z, 0), - input_axis_new(AbsoluteAxis::Wheel, 0), - input_axis_new(AbsoluteAxis::Pressure, 0), - input_axis_new(AbsoluteAxis::Distance, 16), - input_axis_new(AbsoluteAxis::TiltX, 16), - input_axis_new(AbsoluteAxis::TiltY, 0), - input_misc_new(MiscKind::Serial, 0), - input_key_new(self.selected_tool, 1), - input_synchronize_new(SynchronizeKind::Report, 0), - ])?; - Ok(()) - } - - fn motion(&mut self, x: f64, y: f64) -> std::io::Result<()> { - self.dev.write(&[ - input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32), - input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32), - input_axis_new(AbsoluteAxis::Z, 0), - input_axis_new(AbsoluteAxis::Wheel, 0), - input_axis_new(AbsoluteAxis::Pressure, if self.pressed { 2048 } else { 0 }), - input_axis_new(AbsoluteAxis::Distance, if self.pressed { 0 } else { 32 }), - input_axis_new(AbsoluteAxis::TiltX, 16), - input_axis_new(AbsoluteAxis::TiltY, 0), - input_misc_new(MiscKind::Serial, 0), - input_key_new(self.selected_tool, 1), - input_synchronize_new(SynchronizeKind::Report, 0), - ])?; - Ok(()) - } -} - fn build_main_menu(application: >k::Application) { let quit = gio::SimpleAction::new("quit", None); application.set_accels_for_action("app.quit", &["<Control>q"]); @@ -394,12 +313,7 @@ fn build_ui(application: >k::Applicati drawing_area.connect_configure_event(move |_, event| { let state = state_weak.upgrade().unwrap(); let mut state = state.lock().unwrap(); - match event.get_size() { - (width, height) => { - state.width = width as f64; - state.height = height as f64; - } - } + state.set_size(event.get_size()); true }); let state_weak = Arc::downgrade(&state);