comparison src/state.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
children d43c31aff57c
comparison
equal deleted inserted replaced
10:06d77bb94a50 11:0193041f01d4
1 // Tablet emulator, for people who don’t own one
2 // Copyright © 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 use std::fs::File;
18 use std::sync::{Arc, Mutex};
19
20 use crate::{create_uinput_device, WIDTH, HEIGHT, input_axis_new, input_misc_new, input_key_new, input_synchronize_new, MAX_X, MAX_Y};
21 use input_linux::{AbsoluteAxis, Key, MiscKind, SynchronizeKind, UInputHandle};
22
23 pub struct State {
24 dev: UInputHandle<File>,
25 width: f64,
26 height: f64,
27 selected_tool: Key,
28 pressed: bool,
29 }
30
31 impl State {
32 pub fn new() -> std::io::Result<Arc<Mutex<State>>> {
33 let dev = create_uinput_device()?;
34 println!(
35 "New device at {:?} ({:?})",
36 dev.evdev_path()?,
37 dev.sys_path()?
38 );
39
40 Ok(Arc::new(Mutex::new(State {
41 dev,
42 width: WIDTH as f64,
43 height: HEIGHT as f64,
44 selected_tool: Key::ButtonToolPen,
45 pressed: false,
46 })))
47 }
48
49 pub fn set_size(&mut self, (width, height): (u32, u32)) {
50 self.width = width as f64;
51 self.height = height as f64;
52 }
53
54 pub fn select_tool(&mut self, tool: Key) {
55 self.selected_tool = tool;
56 }
57
58 pub fn press(&mut self, x: f64, y: f64) -> std::io::Result<()> {
59 self.pressed = true;
60 self.dev.write(&[
61 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
62 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
63 input_axis_new(AbsoluteAxis::Z, 0),
64 input_axis_new(AbsoluteAxis::Wheel, 0),
65 input_axis_new(AbsoluteAxis::Pressure, 1024),
66 input_axis_new(AbsoluteAxis::Distance, 0),
67 input_axis_new(AbsoluteAxis::TiltX, 16),
68 input_axis_new(AbsoluteAxis::TiltY, 0),
69 input_misc_new(MiscKind::Serial, 0),
70 input_key_new(self.selected_tool, 1),
71 input_synchronize_new(SynchronizeKind::Report, 0),
72 ])?;
73 Ok(())
74 }
75
76 pub fn release(&mut self, x: f64, y: f64) -> std::io::Result<()> {
77 self.pressed = false;
78 self.dev.write(&[
79 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
80 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
81 input_axis_new(AbsoluteAxis::Z, 0),
82 input_axis_new(AbsoluteAxis::Wheel, 0),
83 input_axis_new(AbsoluteAxis::Pressure, 0),
84 input_axis_new(AbsoluteAxis::Distance, 16),
85 input_axis_new(AbsoluteAxis::TiltX, 16),
86 input_axis_new(AbsoluteAxis::TiltY, 0),
87 input_misc_new(MiscKind::Serial, 0),
88 input_key_new(self.selected_tool, 1),
89 input_synchronize_new(SynchronizeKind::Report, 0),
90 ])?;
91 Ok(())
92 }
93
94 pub fn motion(&mut self, x: f64, y: f64) -> std::io::Result<()> {
95 self.dev.write(&[
96 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
97 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
98 input_axis_new(AbsoluteAxis::Z, 0),
99 input_axis_new(AbsoluteAxis::Wheel, 0),
100 input_axis_new(AbsoluteAxis::Pressure, if self.pressed { 2048 } else { 0 }),
101 input_axis_new(AbsoluteAxis::Distance, if self.pressed { 0 } else { 32 }),
102 input_axis_new(AbsoluteAxis::TiltX, 16),
103 input_axis_new(AbsoluteAxis::TiltY, 0),
104 input_misc_new(MiscKind::Serial, 0),
105 input_key_new(self.selected_tool, 1),
106 input_synchronize_new(SynchronizeKind::Report, 0),
107 ])?;
108 Ok(())
109 }
110 }