annotate src/main.rs @ 10:06d77bb94a50

Move more state handling inside of State.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 01 Nov 2020 15:50:38 +0100
parents d1972fc49a5b
children 0193041f01d4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
1 // Tablet emulator, for people who don’t own one
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
2 // Copyright © 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
3 //
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
4 // This program is free software: you can redistribute it and/or modify
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
5 // it under the terms of the GNU Affero General Public License as published by
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
6 // the Free Software Foundation, either version 3 of the License, or
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
7 // (at your option) any later version.
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
8 //
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
9 // This program is distributed in the hope that it will be useful,
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
12 // GNU Affero General Public License for more details.
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
13 //
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
14 // You should have received a copy of the GNU Affero General Public License
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
dd10331549c6 Add a license file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
16
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 use gio::prelude::*;
9
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
18 use glib::clone;
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 use gtk::prelude::*;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 use std::env::args;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 use std::fs::{File, OpenOptions};
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 use std::io::ErrorKind;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 use std::sync::{Arc, Mutex};
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 use input_linux::{
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 sys::input_event, sys::timeval, AbsoluteAxis, AbsoluteInfo, AbsoluteInfoSetup, EventKind,
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
28 InputId, InputProperty, Key, MiscKind, SynchronizeKind, UInputHandle,
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 const WIDTH: i32 = 320;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 const HEIGHT: i32 = 180;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 const MAX_X: i32 = 69920;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 const MAX_Y: i32 = 39980;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 fn create_uinput_device() -> std::io::Result<UInputHandle<File>> {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 let file = OpenOptions::new().write(true).open("/dev/uinput")?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 let dev = UInputHandle::new(file);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 dev.set_evbit(EventKind::Synchronize)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 dev.set_evbit(EventKind::Key)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 dev.set_evbit(EventKind::Absolute)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 dev.set_evbit(EventKind::Misc)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 dev.set_keybit(Key::ButtonToolPen)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 dev.set_keybit(Key::ButtonToolRubber)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 dev.set_keybit(Key::ButtonToolBrush)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 dev.set_keybit(Key::ButtonToolPencil)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 dev.set_keybit(Key::ButtonToolAirbrush)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 dev.set_keybit(Key::ButtonTouch)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 dev.set_keybit(Key::ButtonStylus)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 dev.set_keybit(Key::ButtonStylus2)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 dev.set_keybit(Key::ButtonStylus3)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 dev.set_mscbit(MiscKind::Serial)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 dev.set_propbit(InputProperty::Direct)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 dev.set_absbit(AbsoluteAxis::X)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 dev.set_absbit(AbsoluteAxis::Y)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 dev.set_absbit(AbsoluteAxis::Z)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 dev.set_absbit(AbsoluteAxis::Wheel)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 dev.set_absbit(AbsoluteAxis::Pressure)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 dev.set_absbit(AbsoluteAxis::Distance)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 dev.set_absbit(AbsoluteAxis::TiltX)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 dev.set_absbit(AbsoluteAxis::TiltY)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 dev.set_absbit(AbsoluteAxis::Misc)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 let id = InputId {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 bustype: 3,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 vendor: 0x56a,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 product: 0x350,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 version: 0xb,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 let x = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 axis: AbsoluteAxis::X,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 minimum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 maximum: MAX_X,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 resolution: 200,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 let y = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 axis: AbsoluteAxis::Y,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 minimum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 maximum: MAX_Y,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 resolution: 200,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 let z = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 axis: AbsoluteAxis::Z,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 minimum: -900,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 maximum: 899,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 resolution: 287,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 let wheel = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 axis: AbsoluteAxis::Wheel,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 minimum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 maximum: 2047,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115 resolution: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 let pressure = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 axis: AbsoluteAxis::Pressure,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 minimum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 maximum: 8196,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 resolution: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 let distance = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 axis: AbsoluteAxis::Distance,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 minimum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 maximum: 63,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 resolution: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 let tilt_x = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 axis: AbsoluteAxis::TiltX,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 minimum: -64,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 maximum: 63,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 resolution: 57,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151 let tilt_y = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 axis: AbsoluteAxis::TiltY,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 minimum: -64,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156 maximum: 63,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 resolution: 57,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162 let misc = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 axis: AbsoluteAxis::Misc,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166 minimum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 maximum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170 resolution: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174 dev.create(
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 &id,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 b"TabletEmu",
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178 &[x, y, z, wheel, pressure, distance, tilt_x, tilt_y, misc],
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 )?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
180 Ok(dev)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
181 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
183 fn input_event_new(type_: EventKind, code: u16, value: i32) -> input_event {
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184 input_event {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185 time: timeval {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
186 tv_sec: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
187 tv_usec: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
188 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189 type_: type_ as u16,
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
190 code,
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
191 value,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
192 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
193 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
194
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
195 fn input_axis_new(code: AbsoluteAxis, value: i32) -> input_event {
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
196 input_event_new(EventKind::Absolute, code as u16, value)
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
197 }
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
198
2
39f87ef69b2a Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
199 fn input_key_new(code: Key, value: i32) -> input_event {
39f87ef69b2a Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
200 input_event_new(EventKind::Key, code as u16, value)
39f87ef69b2a Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
201 }
39f87ef69b2a Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
202
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
203 fn input_misc_new(code: MiscKind, value: i32) -> input_event {
2
39f87ef69b2a Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
204 input_event_new(EventKind::Misc, code as u16, value)
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
205 }
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
206
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
207 fn input_synchronize_new(code: SynchronizeKind, value: i32) -> input_event {
2
39f87ef69b2a Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
208 input_event_new(EventKind::Synchronize, code as u16, value)
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
209 }
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
210
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
211 struct State {
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
212 dev: UInputHandle<File>,
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
213 width: f64,
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
214 height: f64,
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
215 selected_tool: Key,
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
216 pressed: bool,
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
217 }
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
218
9
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
219 impl State {
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
220 fn new() -> std::io::Result<Arc<Mutex<State>>> {
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
221 let dev = create_uinput_device()?;
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
222 println!(
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
223 "New device at {:?} ({:?})",
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
224 dev.evdev_path()?,
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
225 dev.sys_path()?
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
226 );
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
227
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
228 Ok(Arc::new(Mutex::new(State {
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
229 dev,
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
230 width: WIDTH as f64,
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
231 height: HEIGHT as f64,
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
232 selected_tool: Key::ButtonToolPen,
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
233 pressed: false,
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
234 })))
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
235 }
10
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
236
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
237 fn select_tool(&mut self, tool: Key) {
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
238 self.selected_tool = tool;
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
239 }
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
240
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
241 fn press(&mut self, x: f64, y: f64) -> std::io::Result<()> {
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
242 self.pressed = true;
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
243 self.dev.write(&[
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
244 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
245 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
246 input_axis_new(AbsoluteAxis::Z, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
247 input_axis_new(AbsoluteAxis::Wheel, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
248 input_axis_new(AbsoluteAxis::Pressure, 1024),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
249 input_axis_new(AbsoluteAxis::Distance, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
250 input_axis_new(AbsoluteAxis::TiltX, 16),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
251 input_axis_new(AbsoluteAxis::TiltY, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
252 input_misc_new(MiscKind::Serial, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
253 input_key_new(self.selected_tool, 1),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
254 input_synchronize_new(SynchronizeKind::Report, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
255 ])?;
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
256 Ok(())
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
257 }
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
258
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
259 fn release(&mut self, x: f64, y: f64) -> std::io::Result<()> {
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
260 self.pressed = false;
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
261 self.dev.write(&[
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
262 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
263 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
264 input_axis_new(AbsoluteAxis::Z, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
265 input_axis_new(AbsoluteAxis::Wheel, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
266 input_axis_new(AbsoluteAxis::Pressure, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
267 input_axis_new(AbsoluteAxis::Distance, 16),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
268 input_axis_new(AbsoluteAxis::TiltX, 16),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
269 input_axis_new(AbsoluteAxis::TiltY, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
270 input_misc_new(MiscKind::Serial, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
271 input_key_new(self.selected_tool, 1),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
272 input_synchronize_new(SynchronizeKind::Report, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
273 ])?;
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
274 Ok(())
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
275 }
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
276
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
277 fn motion(&mut self, x: f64, y: f64) -> std::io::Result<()> {
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
278 self.dev.write(&[
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
279 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
280 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
281 input_axis_new(AbsoluteAxis::Z, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
282 input_axis_new(AbsoluteAxis::Wheel, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
283 input_axis_new(AbsoluteAxis::Pressure, if self.pressed { 2048 } else { 0 }),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
284 input_axis_new(AbsoluteAxis::Distance, if self.pressed { 0 } else { 32 }),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
285 input_axis_new(AbsoluteAxis::TiltX, 16),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
286 input_axis_new(AbsoluteAxis::TiltY, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
287 input_misc_new(MiscKind::Serial, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
288 input_key_new(self.selected_tool, 1),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
289 input_synchronize_new(SynchronizeKind::Report, 0),
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
290 ])?;
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
291 Ok(())
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
292 }
9
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
293 }
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
294
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
295 fn build_main_menu(application: &gtk::Application) {
5
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
296 let quit = gio::SimpleAction::new("quit", None);
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
297 application.set_accels_for_action("app.quit", &["<Control>q"]);
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
298 application.add_action(&quit);
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
299 quit.connect_activate(clone!(@weak application => move |_, _| application.quit()));
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
300
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
301 let about = gio::SimpleAction::new("about", None);
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
302 application.add_action(&about);
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
303 about.connect_activate(|_, _| {
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
304 let about = gtk::AboutDialog::new();
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
305 about.set_program_name("TabletEmu");
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
306 about.set_logo_icon_name(Some("input-tablet"));
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
307 about.set_website(Some("https://hg.linkmauve.fr/tablet-emu"));
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
308 about.set_version(Some("0.1"));
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
309 about.set_license_type(gtk::License::Agpl30);
7
9c3e2d2efaa8 Add my copyright in the about dialog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 6
diff changeset
310 about.set_copyright(Some("© 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>"));
5
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
311 about.run();
9
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
312 unsafe {
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
313 about.destroy();
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
314 }
5
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
315 });
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
316
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
317 let menu = gio::Menu::new();
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
318 {
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
319 let file = gio::Menu::new();
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
320 file.append(Some("_Quit"), Some("app.quit"));
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
321 menu.append_submenu(Some("_File"), &file);
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
322 }
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
323 {
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
324 let help = gio::Menu::new();
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
325 help.append(Some("_About"), Some("app.about"));
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
326 menu.append_submenu(Some("_Help"), &help);
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
327 }
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
328 application.set_menubar(Some(&menu));
9
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
329 }
5
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
330
9
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
331 fn build_ui(application: &gtk::Application) {
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
332 build_main_menu(application);
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
333
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
334 let state = match State::new() {
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
335 Ok(state) => state,
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
336 Err(err) => {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
337 match err.kind() {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
338 ErrorKind::NotFound => {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
339 eprintln!("Couldn’t find /dev/uinput: {}", err);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
340 eprintln!("Maybe you forgot to `modprobe uinput`?");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
341 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
342 ErrorKind::PermissionDenied => {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
343 eprintln!("Couldn’t open /dev/uinput for writing: {}", err);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
344 eprintln!("Maybe you aren’t allowed to create input devices?");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
345 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
346 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err),
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
347 }
9
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
348 std::process::exit(1);
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
349 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
350 };
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
351
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
352 let window = gtk::ApplicationWindow::new(application);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
353 window.set_title("tablet-emu");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
354 window.set_position(gtk::WindowPosition::Center);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
355
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
356 let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
357 let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
358
4
2e074d185151 Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
359 macro_rules! impl_tool {
2e074d185151 Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
360 ($tool:tt) => {
8
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
361 let tool = gtk::Button::with_mnemonic($tool);
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
362 let state_weak = Arc::downgrade(&state);
4
2e074d185151 Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
363 tool.connect_clicked(move |b| {
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
364 let state = state_weak.upgrade().unwrap();
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
365 let mut state = state.lock().unwrap();
10
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
366 let tool = match b.get_label().unwrap().as_str() {
8
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
367 "_Pen" => Key::ButtonToolPen,
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
368 "_Rubber" => Key::ButtonToolRubber,
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
369 "_Brush" => Key::ButtonToolBrush,
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
370 "P_encil" => Key::ButtonToolPencil,
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
371 "_Airbrush" => Key::ButtonToolAirbrush,
2
39f87ef69b2a Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
372 _ => unreachable!(),
39f87ef69b2a Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
373 };
10
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
374 state.select_tool(tool);
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
375 });
4
2e074d185151 Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
376 tools_box.add(&tool);
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
377 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
378 };
8
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
379 impl_tool!("_Pen");
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
380 impl_tool!("_Rubber");
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
381 impl_tool!("_Brush");
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
382 impl_tool!("P_encil");
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
383 impl_tool!("_Airbrush");
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
384
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
385 let drawing_area = gtk::DrawingArea::new();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
386 drawing_area.set_size_request(WIDTH, HEIGHT);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
387 drawing_area.set_hexpand(true);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
388 drawing_area.set_events(
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
389 gdk::EventMask::BUTTON_PRESS_MASK
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
390 | gdk::EventMask::BUTTON_RELEASE_MASK
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
391 | gdk::EventMask::POINTER_MOTION_MASK,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
392 );
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
393 let state_weak = Arc::downgrade(&state);
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
394 drawing_area.connect_configure_event(move |_, event| {
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
395 let state = state_weak.upgrade().unwrap();
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
396 let mut state = state.lock().unwrap();
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
397 match event.get_size() {
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
398 (width, height) => {
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
399 state.width = width as f64;
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
400 state.height = height as f64;
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
401 }
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
402 }
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
403 true
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
404 });
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
405 let state_weak = Arc::downgrade(&state);
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
406 drawing_area.connect_button_press_event(move |_, event| {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
407 if event.get_button() != 1 {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
408 return Inhibit(false);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
409 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
410
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
411 let state = state_weak.upgrade().unwrap();
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
412 let mut state = state.lock().unwrap();
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
413 let (x, y) = event.get_position();
10
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
414 state.press(x, y).unwrap();
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
415 Inhibit(false)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
416 });
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
417 let state_weak = Arc::downgrade(&state);
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
418 drawing_area.connect_button_release_event(move |_, event| {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
419 if event.get_button() != 1 {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
420 return Inhibit(false);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
421 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
422
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
423 let state = state_weak.upgrade().unwrap();
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
424 let mut state = state.lock().unwrap();
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
425 let (x, y) = event.get_position();
10
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
426 state.release(x, y).unwrap();
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
427 Inhibit(false)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
428 });
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
429 drawing_area.connect_motion_notify_event(move |_, event| {
10
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
430 let mut state = state.lock().unwrap();
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
431 let (x, y) = event.get_position();
10
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
432 state.motion(x, y).unwrap();
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
433 Inhibit(false)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
434 });
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
435 drawing_area.connect_draw(move |_, ctx| {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
436 //println!("drawing {}", drawing_area);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
437 ctx.set_source_rgb(1., 0., 0.);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
438 ctx.set_operator(cairo::Operator::Screen);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
439 ctx.paint();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
440 Inhibit(false)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
441 });
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
442
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
443 hbox.add(&tools_box);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
444 hbox.add(&drawing_area);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
445
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
446 window.add(&hbox);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
447
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
448 window.show_all();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
449 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
450
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
451 fn main() {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
452 let application = gtk::Application::new(
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
453 Some("fr.linkmauve.TabletEmu"),
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
454 gio::ApplicationFlags::empty(),
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
455 )
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
456 .expect("Initialisation failed…");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
457 application.connect_activate(build_ui);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
458 application.run(&args().collect::<Vec<_>>());
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
459 }