annotate src/state.rs @ 13:97e543f50f62

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