annotate src/state.rs @ 15:d103f7cca0bd

Update to GTK 4.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 26 Aug 2021 19:34:40 +0200
parents adab13145994
children 478cf2a7d577
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
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 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
20 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
21 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
22
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 pub struct State {
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 dev: UInputHandle<File>,
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 width: f64,
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 height: f64,
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 selected_tool: Key,
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 pressed: bool,
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 }
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 impl State {
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
32 pub fn new() -> std::io::Result<State> {
11
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 let dev = create_uinput_device()?;
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 println!(
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 "New device at {:?} ({:?})",
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 dev.evdev_path()?,
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 dev.sys_path()?
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 );
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
40 Ok(State {
11
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 dev,
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 12
diff changeset
42 width: DEFAULT_WIDTH as f64,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 12
diff changeset
43 height: DEFAULT_HEIGHT as f64,
11
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 selected_tool: Key::ButtonToolPen,
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 pressed: false,
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
46 })
11
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
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
49 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
50 self.width = width as f64;
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 self.height = height as f64;
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 }
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 pub fn select_tool(&mut self, tool: Key) {
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 self.selected_tool = tool;
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 }
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 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
59 self.pressed = true;
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 self.dev.write(&[
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 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
62 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
63 input_axis_new(AbsoluteAxis::Z, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 input_axis_new(AbsoluteAxis::Wheel, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 input_axis_new(AbsoluteAxis::Pressure, 1024),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 input_axis_new(AbsoluteAxis::Distance, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 input_axis_new(AbsoluteAxis::TiltX, 16),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 input_axis_new(AbsoluteAxis::TiltY, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 input_misc_new(MiscKind::Serial, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 input_key_new(self.selected_tool, 1),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 input_synchronize_new(SynchronizeKind::Report, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 ])?;
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 Ok(())
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 }
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 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
77 self.pressed = false;
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 self.dev.write(&[
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 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
80 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
81 input_axis_new(AbsoluteAxis::Z, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 input_axis_new(AbsoluteAxis::Wheel, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 input_axis_new(AbsoluteAxis::Pressure, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 input_axis_new(AbsoluteAxis::Distance, 16),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 input_axis_new(AbsoluteAxis::TiltX, 16),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 input_axis_new(AbsoluteAxis::TiltY, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 input_misc_new(MiscKind::Serial, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 input_key_new(self.selected_tool, 1),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 input_synchronize_new(SynchronizeKind::Report, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 ])?;
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 Ok(())
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 }
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 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
95 self.dev.write(&[
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 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
97 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
98 input_axis_new(AbsoluteAxis::Z, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 input_axis_new(AbsoluteAxis::Wheel, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 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
101 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
102 input_axis_new(AbsoluteAxis::TiltX, 16),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 input_axis_new(AbsoluteAxis::TiltY, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 input_misc_new(MiscKind::Serial, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 input_key_new(self.selected_tool, 1),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 input_synchronize_new(SynchronizeKind::Report, 0),
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 ])?;
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 Ok(())
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 }
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 }