Mercurial > tablet-emu
annotate src/main.rs @ 6:dd10331549c6
Add a license file.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 01 Nov 2020 14:39:32 +0100 |
parents | cdbf3a2c9af8 |
children | 9c3e2d2efaa8 |
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 | 17 use gio::prelude::*; |
18 use gtk::prelude::*; | |
5
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
19 use glib::clone; |
0 | 20 |
21 use std::env::args; | |
22 use std::fs::{File, OpenOptions}; | |
23 use std::io::ErrorKind; | |
24 use std::sync::{Arc, Mutex}; | |
25 | |
26 use input_linux::{ | |
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 | 29 }; |
30 | |
31 const WIDTH: i32 = 320; | |
32 const HEIGHT: i32 = 180; | |
33 | |
34 const MAX_X: i32 = 69920; | |
35 const MAX_Y: i32 = 39980; | |
36 | |
37 fn create_uinput_device() -> std::io::Result<UInputHandle<File>> { | |
38 let file = OpenOptions::new().write(true).open("/dev/uinput")?; | |
39 let dev = UInputHandle::new(file); | |
40 | |
41 dev.set_evbit(EventKind::Synchronize)?; | |
42 dev.set_evbit(EventKind::Key)?; | |
43 dev.set_evbit(EventKind::Absolute)?; | |
44 dev.set_evbit(EventKind::Misc)?; | |
45 dev.set_keybit(Key::ButtonToolPen)?; | |
46 dev.set_keybit(Key::ButtonToolRubber)?; | |
47 dev.set_keybit(Key::ButtonToolBrush)?; | |
48 dev.set_keybit(Key::ButtonToolPencil)?; | |
49 dev.set_keybit(Key::ButtonToolAirbrush)?; | |
50 dev.set_keybit(Key::ButtonTouch)?; | |
51 dev.set_keybit(Key::ButtonStylus)?; | |
52 dev.set_keybit(Key::ButtonStylus2)?; | |
53 dev.set_keybit(Key::ButtonStylus3)?; | |
54 dev.set_mscbit(MiscKind::Serial)?; | |
55 dev.set_propbit(InputProperty::Direct)?; | |
56 | |
57 dev.set_absbit(AbsoluteAxis::X)?; | |
58 dev.set_absbit(AbsoluteAxis::Y)?; | |
59 dev.set_absbit(AbsoluteAxis::Z)?; | |
60 dev.set_absbit(AbsoluteAxis::Wheel)?; | |
61 dev.set_absbit(AbsoluteAxis::Pressure)?; | |
62 dev.set_absbit(AbsoluteAxis::Distance)?; | |
63 dev.set_absbit(AbsoluteAxis::TiltX)?; | |
64 dev.set_absbit(AbsoluteAxis::TiltY)?; | |
65 dev.set_absbit(AbsoluteAxis::Misc)?; | |
66 | |
67 let id = InputId { | |
68 bustype: 3, | |
69 vendor: 0x56a, | |
70 product: 0x350, | |
71 version: 0xb, | |
72 }; | |
73 | |
74 let x = AbsoluteInfoSetup { | |
75 axis: AbsoluteAxis::X, | |
76 info: AbsoluteInfo { | |
77 value: 0, | |
78 minimum: 0, | |
79 maximum: MAX_X, | |
80 fuzz: 0, | |
81 flat: 0, | |
82 resolution: 200, | |
83 }, | |
84 }; | |
85 let y = AbsoluteInfoSetup { | |
86 axis: AbsoluteAxis::Y, | |
87 info: AbsoluteInfo { | |
88 value: 0, | |
89 minimum: 0, | |
90 maximum: MAX_Y, | |
91 fuzz: 0, | |
92 flat: 0, | |
93 resolution: 200, | |
94 }, | |
95 }; | |
96 let z = AbsoluteInfoSetup { | |
97 axis: AbsoluteAxis::Z, | |
98 info: AbsoluteInfo { | |
99 value: 0, | |
100 minimum: -900, | |
101 maximum: 899, | |
102 fuzz: 0, | |
103 flat: 0, | |
104 resolution: 287, | |
105 }, | |
106 }; | |
107 let wheel = AbsoluteInfoSetup { | |
108 axis: AbsoluteAxis::Wheel, | |
109 info: AbsoluteInfo { | |
110 value: 0, | |
111 minimum: 0, | |
112 maximum: 2047, | |
113 fuzz: 0, | |
114 flat: 0, | |
115 resolution: 0, | |
116 }, | |
117 }; | |
118 let pressure = AbsoluteInfoSetup { | |
119 axis: AbsoluteAxis::Pressure, | |
120 info: AbsoluteInfo { | |
121 value: 0, | |
122 minimum: 0, | |
123 maximum: 8196, | |
124 fuzz: 0, | |
125 flat: 0, | |
126 resolution: 0, | |
127 }, | |
128 }; | |
129 let distance = AbsoluteInfoSetup { | |
130 axis: AbsoluteAxis::Distance, | |
131 info: AbsoluteInfo { | |
132 value: 0, | |
133 minimum: 0, | |
134 maximum: 63, | |
135 fuzz: 0, | |
136 flat: 0, | |
137 resolution: 0, | |
138 }, | |
139 }; | |
140 let tilt_x = AbsoluteInfoSetup { | |
141 axis: AbsoluteAxis::TiltX, | |
142 info: AbsoluteInfo { | |
143 value: 0, | |
144 minimum: -64, | |
145 maximum: 63, | |
146 fuzz: 0, | |
147 flat: 0, | |
148 resolution: 57, | |
149 }, | |
150 }; | |
151 let tilt_y = AbsoluteInfoSetup { | |
152 axis: AbsoluteAxis::TiltY, | |
153 info: AbsoluteInfo { | |
154 value: 0, | |
155 minimum: -64, | |
156 maximum: 63, | |
157 fuzz: 0, | |
158 flat: 0, | |
159 resolution: 57, | |
160 }, | |
161 }; | |
162 let misc = AbsoluteInfoSetup { | |
163 axis: AbsoluteAxis::Misc, | |
164 info: AbsoluteInfo { | |
165 value: 0, | |
166 minimum: 0, | |
167 maximum: 0, | |
168 fuzz: 0, | |
169 flat: 0, | |
170 resolution: 0, | |
171 }, | |
172 }; | |
173 | |
174 dev.create( | |
175 &id, | |
176 b"TabletEmu", | |
177 0, | |
178 &[x, y, z, wheel, pressure, distance, tilt_x, tilt_y, misc], | |
179 )?; | |
180 Ok(dev) | |
181 } | |
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 | 184 input_event { |
185 time: timeval { | |
186 tv_sec: 0, | |
187 tv_usec: 0, | |
188 }, | |
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 | 191 value, |
192 } | |
193 } | |
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 |
0 | 219 fn build_ui(application: >k::Application) { |
5
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
220 let quit = gio::SimpleAction::new("quit", None); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
221 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
|
222 application.add_action(&quit); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
223 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
|
224 |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
225 let about = gio::SimpleAction::new("about", None); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
226 application.add_action(&about); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
227 about.connect_activate(|_, _| { |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
228 let about = gtk::AboutDialog::new(); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
229 about.set_program_name("TabletEmu"); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
230 about.set_logo_icon_name(Some("input-tablet")); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
231 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
|
232 about.set_version(Some("0.1")); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
233 about.set_license_type(gtk::License::Agpl30); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
234 about.set_program_name("TabletEmu"); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
235 about.run(); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
236 unsafe { about.destroy(); } |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
237 }); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
238 |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
239 let menu = gio::Menu::new(); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
240 { |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
241 let file = gio::Menu::new(); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
242 file.append(Some("_Quit"), Some("app.quit")); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
243 menu.append_submenu(Some("_File"), &file); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
244 } |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
245 { |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
246 let help = gio::Menu::new(); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
247 help.append(Some("_About"), Some("app.about")); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
248 menu.append_submenu(Some("_Help"), &help); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
249 } |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
250 application.set_menubar(Some(&menu)); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
251 |
0 | 252 let dev = match create_uinput_device() { |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
253 Ok(dev) => dev, |
0 | 254 Err(err) => { |
255 match err.kind() { | |
256 ErrorKind::NotFound => { | |
257 eprintln!("Couldn’t find /dev/uinput: {}", err); | |
258 eprintln!("Maybe you forgot to `modprobe uinput`?"); | |
259 } | |
260 ErrorKind::PermissionDenied => { | |
261 eprintln!("Couldn’t open /dev/uinput for writing: {}", err); | |
262 eprintln!("Maybe you aren’t allowed to create input devices?"); | |
263 } | |
264 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err), | |
265 } | |
266 return; | |
267 } | |
268 }; | |
269 println!( | |
270 "New device at {:?} ({:?})", | |
271 dev.evdev_path().unwrap(), | |
272 dev.sys_path().unwrap() | |
273 ); | |
274 | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
275 let state = Arc::new(Mutex::new(State { |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
276 dev, |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
277 width: WIDTH as f64, |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
278 height: HEIGHT as f64, |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
279 selected_tool: Key::ButtonToolPen, |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
280 pressed: false, |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
281 })); |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
282 |
0 | 283 let window = gtk::ApplicationWindow::new(application); |
284 | |
285 window.set_title("tablet-emu"); | |
286 window.set_position(gtk::WindowPosition::Center); | |
287 | |
288 let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0); | |
289 let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0); | |
290 | |
4
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
291 macro_rules! impl_tool { |
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
292 ($tool:tt) => { |
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
293 let tool = gtk::Button::with_label($tool); |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
294 let state_weak = Arc::downgrade(&state); |
4
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
295 tool.connect_clicked(move |b| { |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
296 let state = state_weak.upgrade().unwrap(); |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
297 let mut state = state.lock().unwrap(); |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
298 state.selected_tool = match b.get_label().unwrap().as_str() { |
2
39f87ef69b2a
Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
299 "Pen" => Key::ButtonToolPen, |
39f87ef69b2a
Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
300 "Rubber" => Key::ButtonToolRubber, |
39f87ef69b2a
Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
301 "Brush" => Key::ButtonToolBrush, |
39f87ef69b2a
Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
302 "Pencil" => Key::ButtonToolPencil, |
39f87ef69b2a
Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
303 "Airbrush" => Key::ButtonToolAirbrush, |
39f87ef69b2a
Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
304 _ => unreachable!(), |
39f87ef69b2a
Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
305 }; |
0 | 306 }); |
4
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
307 tools_box.add(&tool); |
0 | 308 }; |
309 }; | |
4
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
310 impl_tool!("Pen"); |
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
311 impl_tool!("Rubber"); |
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
312 impl_tool!("Brush"); |
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
313 impl_tool!("Pencil"); |
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
314 impl_tool!("Airbrush"); |
0 | 315 |
316 let drawing_area = gtk::DrawingArea::new(); | |
317 drawing_area.set_size_request(WIDTH, HEIGHT); | |
318 drawing_area.set_hexpand(true); | |
319 drawing_area.set_events( | |
320 gdk::EventMask::BUTTON_PRESS_MASK | |
321 | gdk::EventMask::BUTTON_RELEASE_MASK | |
322 | gdk::EventMask::POINTER_MOTION_MASK, | |
323 ); | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
324 let state_weak = Arc::downgrade(&state); |
0 | 325 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
|
326 let state = state_weak.upgrade().unwrap(); |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
327 let mut state = state.lock().unwrap(); |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
328 match event.get_size() { |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
329 (width, height) => { |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
330 state.width = width as f64; |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
331 state.height = height as f64; |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
332 } |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
333 } |
0 | 334 true |
335 }); | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
336 let state_weak = Arc::downgrade(&state); |
0 | 337 drawing_area.connect_button_press_event(move |_, event| { |
338 if event.get_button() != 1 { | |
339 return Inhibit(false); | |
340 } | |
341 | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
342 let state = state_weak.upgrade().unwrap(); |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
343 let mut state = state.lock().unwrap(); |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
344 state.pressed = true; |
0 | 345 let (x, y) = event.get_position(); |
2
39f87ef69b2a
Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
346 //println!("press tool {} at {}, {}", current_tool.lock().unwrap(), x, y); |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
347 state.dev.write(&[ |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
348 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / state.width) as i32), |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
349 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / state.height) as i32), |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
350 input_axis_new(AbsoluteAxis::Z, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
351 input_axis_new(AbsoluteAxis::Wheel, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
352 input_axis_new(AbsoluteAxis::Pressure, 1024), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
353 input_axis_new(AbsoluteAxis::Distance, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
354 input_axis_new(AbsoluteAxis::TiltX, 16), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
355 input_axis_new(AbsoluteAxis::TiltY, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
356 input_misc_new(MiscKind::Serial, 0), |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
357 input_key_new(state.selected_tool, 1), |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
358 input_synchronize_new(SynchronizeKind::Report, 0), |
0 | 359 ]) |
360 .unwrap(); | |
361 Inhibit(false) | |
362 }); | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
363 let state_weak = Arc::downgrade(&state); |
0 | 364 drawing_area.connect_button_release_event(move |_, event| { |
365 if event.get_button() != 1 { | |
366 return Inhibit(false); | |
367 } | |
368 | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
369 let state = state_weak.upgrade().unwrap(); |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
370 let mut state = state.lock().unwrap(); |
0 | 371 let (x, y) = event.get_position(); |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
372 state.pressed = false; |
0 | 373 //println!("release {}, {}", x, y); |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
374 state.dev.write(&[ |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
375 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / state.width) as i32), |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
376 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / state.height) as i32), |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
377 input_axis_new(AbsoluteAxis::Z, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
378 input_axis_new(AbsoluteAxis::Wheel, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
379 input_axis_new(AbsoluteAxis::Pressure, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
380 input_axis_new(AbsoluteAxis::Distance, 16), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
381 input_axis_new(AbsoluteAxis::TiltX, 16), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
382 input_axis_new(AbsoluteAxis::TiltY, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
383 input_misc_new(MiscKind::Serial, 0), |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
384 input_key_new(state.selected_tool, 1), |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
385 input_synchronize_new(SynchronizeKind::Report, 0), |
0 | 386 ]) |
387 .unwrap(); | |
388 Inhibit(false) | |
389 }); | |
390 drawing_area.connect_motion_notify_event(move |_, event| { | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
391 let state = state.lock().unwrap(); |
0 | 392 let (x, y) = event.get_position(); |
393 //println!("motion {}, {}", x, y); | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
394 state.dev.write(&[ |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
395 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / state.width) as i32), |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
396 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / state.height) as i32), |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
397 input_axis_new(AbsoluteAxis::Z, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
398 input_axis_new(AbsoluteAxis::Wheel, 0), |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
399 input_axis_new(AbsoluteAxis::Pressure, if state.pressed { 2048 } else { 0 }), |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
400 input_axis_new(AbsoluteAxis::Distance, if state.pressed { 0 } else { 32 }), |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
401 input_axis_new(AbsoluteAxis::TiltX, 16), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
402 input_axis_new(AbsoluteAxis::TiltY, 0), |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
403 input_misc_new(MiscKind::Serial, 0), |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
404 input_key_new(state.selected_tool, 1), |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
405 input_synchronize_new(SynchronizeKind::Report, 0), |
0 | 406 ]) |
407 .unwrap(); | |
408 Inhibit(false) | |
409 }); | |
410 drawing_area.connect_draw(move |_, ctx| { | |
411 //println!("drawing {}", drawing_area); | |
412 ctx.set_source_rgb(1., 0., 0.); | |
413 ctx.set_operator(cairo::Operator::Screen); | |
414 ctx.paint(); | |
415 Inhibit(false) | |
416 }); | |
417 | |
418 hbox.add(&tools_box); | |
419 hbox.add(&drawing_area); | |
420 | |
421 window.add(&hbox); | |
422 | |
423 window.show_all(); | |
424 } | |
425 | |
426 fn main() { | |
427 let application = gtk::Application::new( | |
428 Some("fr.linkmauve.TabletEmu"), | |
429 gio::ApplicationFlags::empty(), | |
430 ) | |
431 .expect("Initialisation failed…"); | |
432 application.connect_activate(build_ui); | |
433 application.run(&args().collect::<Vec<_>>()); | |
434 } |