# HG changeset patch # User Emmanuel Gil Peyrot # Date 1604242560 -3600 # Node ID 0193041f01d490fc9d6dbaa6581c059fe553ffda # Parent 06d77bb94a50e577c92b4fcde1b18c658a86e904 Split State into another module. diff --git a/src/main.rs b/src/main.rs --- 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, - width: f64, - height: f64, - selected_tool: Key, - pressed: bool, -} - -impl State { - fn new() -> std::io::Result>> { - 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", &["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); diff --git a/src/state.rs b/src/state.rs new file mode 100644 --- /dev/null +++ b/src/state.rs @@ -0,0 +1,110 @@ +// Tablet emulator, for people who don’t own one +// Copyright © 2020 Emmanuel Gil Peyrot +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use std::fs::File; +use std::sync::{Arc, Mutex}; + +use crate::{create_uinput_device, WIDTH, HEIGHT, input_axis_new, input_misc_new, input_key_new, input_synchronize_new, MAX_X, MAX_Y}; +use input_linux::{AbsoluteAxis, Key, MiscKind, SynchronizeKind, UInputHandle}; + +pub struct State { + dev: UInputHandle, + width: f64, + height: f64, + selected_tool: Key, + pressed: bool, +} + +impl State { + pub fn new() -> std::io::Result>> { + 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, + }))) + } + + pub fn set_size(&mut self, (width, height): (u32, u32)) { + self.width = width as f64; + self.height = height as f64; + } + + pub fn select_tool(&mut self, tool: Key) { + self.selected_tool = tool; + } + + pub 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(()) + } + + pub 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(()) + } + + pub 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(()) + } +}