annotate src/state.rs @ 16:478cf2a7d577

Reformat with rustfmt.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 26 Aug 2021 19:45:30 +0200
parents d103f7cca0bd
children 0bce7fe96937
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
16
478cf2a7d577 Reformat with rustfmt.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
17 use crate::uinput::{
478cf2a7d577 Reformat with rustfmt.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
18 create_uinput_device, input_axis_new, input_key_new, input_misc_new, input_synchronize_new,
478cf2a7d577 Reformat with rustfmt.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
19 };
478cf2a7d577 Reformat with rustfmt.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
20 use crate::{DEFAULT_HEIGHT, DEFAULT_WIDTH, MAX_X, MAX_Y};
11
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 use input_linux::{AbsoluteAxis, Key, MiscKind, SynchronizeKind, UInputHandle};
16
478cf2a7d577 Reformat with rustfmt.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
22 use std::fs::File;
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 {
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
33 pub fn new() -> std::io::Result<State> {
11
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
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
41 Ok(State {
11
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,
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
47 })
11
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
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
50 pub fn set_size(&mut self, width: i32, height: i32) {
11
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 }