Mercurial > tablet-emu
annotate src/main.rs @ 11:0193041f01d4
Split State into another module.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 01 Nov 2020 15:56:00 +0100 |
parents | 06d77bb94a50 |
children | d43c31aff57c |
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::*; |
9
d1972fc49a5b
Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
18 use glib::clone; |
0 | 19 use gtk::prelude::*; |
20 | |
21 use std::env::args; | |
22 use std::fs::{File, OpenOptions}; | |
23 use std::io::ErrorKind; | |
11
0193041f01d4
Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
10
diff
changeset
|
24 use std::sync::Arc; |
0 | 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 | |
11
0193041f01d4
Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
10
diff
changeset
|
31 mod state; |
0193041f01d4
Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
10
diff
changeset
|
32 use state::State; |
0193041f01d4
Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
10
diff
changeset
|
33 |
0 | 34 const WIDTH: i32 = 320; |
35 const HEIGHT: i32 = 180; | |
36 | |
37 const MAX_X: i32 = 69920; | |
38 const MAX_Y: i32 = 39980; | |
39 | |
40 fn create_uinput_device() -> std::io::Result<UInputHandle<File>> { | |
41 let file = OpenOptions::new().write(true).open("/dev/uinput")?; | |
42 let dev = UInputHandle::new(file); | |
43 | |
44 dev.set_evbit(EventKind::Synchronize)?; | |
45 dev.set_evbit(EventKind::Key)?; | |
46 dev.set_evbit(EventKind::Absolute)?; | |
47 dev.set_evbit(EventKind::Misc)?; | |
48 dev.set_keybit(Key::ButtonToolPen)?; | |
49 dev.set_keybit(Key::ButtonToolRubber)?; | |
50 dev.set_keybit(Key::ButtonToolBrush)?; | |
51 dev.set_keybit(Key::ButtonToolPencil)?; | |
52 dev.set_keybit(Key::ButtonToolAirbrush)?; | |
53 dev.set_keybit(Key::ButtonTouch)?; | |
54 dev.set_keybit(Key::ButtonStylus)?; | |
55 dev.set_keybit(Key::ButtonStylus2)?; | |
56 dev.set_keybit(Key::ButtonStylus3)?; | |
57 dev.set_mscbit(MiscKind::Serial)?; | |
58 dev.set_propbit(InputProperty::Direct)?; | |
59 | |
60 dev.set_absbit(AbsoluteAxis::X)?; | |
61 dev.set_absbit(AbsoluteAxis::Y)?; | |
62 dev.set_absbit(AbsoluteAxis::Z)?; | |
63 dev.set_absbit(AbsoluteAxis::Wheel)?; | |
64 dev.set_absbit(AbsoluteAxis::Pressure)?; | |
65 dev.set_absbit(AbsoluteAxis::Distance)?; | |
66 dev.set_absbit(AbsoluteAxis::TiltX)?; | |
67 dev.set_absbit(AbsoluteAxis::TiltY)?; | |
68 dev.set_absbit(AbsoluteAxis::Misc)?; | |
69 | |
70 let id = InputId { | |
71 bustype: 3, | |
72 vendor: 0x56a, | |
73 product: 0x350, | |
74 version: 0xb, | |
75 }; | |
76 | |
77 let x = AbsoluteInfoSetup { | |
78 axis: AbsoluteAxis::X, | |
79 info: AbsoluteInfo { | |
80 value: 0, | |
81 minimum: 0, | |
82 maximum: MAX_X, | |
83 fuzz: 0, | |
84 flat: 0, | |
85 resolution: 200, | |
86 }, | |
87 }; | |
88 let y = AbsoluteInfoSetup { | |
89 axis: AbsoluteAxis::Y, | |
90 info: AbsoluteInfo { | |
91 value: 0, | |
92 minimum: 0, | |
93 maximum: MAX_Y, | |
94 fuzz: 0, | |
95 flat: 0, | |
96 resolution: 200, | |
97 }, | |
98 }; | |
99 let z = AbsoluteInfoSetup { | |
100 axis: AbsoluteAxis::Z, | |
101 info: AbsoluteInfo { | |
102 value: 0, | |
103 minimum: -900, | |
104 maximum: 899, | |
105 fuzz: 0, | |
106 flat: 0, | |
107 resolution: 287, | |
108 }, | |
109 }; | |
110 let wheel = AbsoluteInfoSetup { | |
111 axis: AbsoluteAxis::Wheel, | |
112 info: AbsoluteInfo { | |
113 value: 0, | |
114 minimum: 0, | |
115 maximum: 2047, | |
116 fuzz: 0, | |
117 flat: 0, | |
118 resolution: 0, | |
119 }, | |
120 }; | |
121 let pressure = AbsoluteInfoSetup { | |
122 axis: AbsoluteAxis::Pressure, | |
123 info: AbsoluteInfo { | |
124 value: 0, | |
125 minimum: 0, | |
126 maximum: 8196, | |
127 fuzz: 0, | |
128 flat: 0, | |
129 resolution: 0, | |
130 }, | |
131 }; | |
132 let distance = AbsoluteInfoSetup { | |
133 axis: AbsoluteAxis::Distance, | |
134 info: AbsoluteInfo { | |
135 value: 0, | |
136 minimum: 0, | |
137 maximum: 63, | |
138 fuzz: 0, | |
139 flat: 0, | |
140 resolution: 0, | |
141 }, | |
142 }; | |
143 let tilt_x = AbsoluteInfoSetup { | |
144 axis: AbsoluteAxis::TiltX, | |
145 info: AbsoluteInfo { | |
146 value: 0, | |
147 minimum: -64, | |
148 maximum: 63, | |
149 fuzz: 0, | |
150 flat: 0, | |
151 resolution: 57, | |
152 }, | |
153 }; | |
154 let tilt_y = AbsoluteInfoSetup { | |
155 axis: AbsoluteAxis::TiltY, | |
156 info: AbsoluteInfo { | |
157 value: 0, | |
158 minimum: -64, | |
159 maximum: 63, | |
160 fuzz: 0, | |
161 flat: 0, | |
162 resolution: 57, | |
163 }, | |
164 }; | |
165 let misc = AbsoluteInfoSetup { | |
166 axis: AbsoluteAxis::Misc, | |
167 info: AbsoluteInfo { | |
168 value: 0, | |
169 minimum: 0, | |
170 maximum: 0, | |
171 fuzz: 0, | |
172 flat: 0, | |
173 resolution: 0, | |
174 }, | |
175 }; | |
176 | |
177 dev.create( | |
178 &id, | |
179 b"TabletEmu", | |
180 0, | |
181 &[x, y, z, wheel, pressure, distance, tilt_x, tilt_y, misc], | |
182 )?; | |
183 Ok(dev) | |
184 } | |
185 | |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
186 fn input_event_new(type_: EventKind, code: u16, value: i32) -> input_event { |
0 | 187 input_event { |
188 time: timeval { | |
189 tv_sec: 0, | |
190 tv_usec: 0, | |
191 }, | |
192 type_: type_ as u16, | |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
193 code, |
0 | 194 value, |
195 } | |
196 } | |
197 | |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
198 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
|
199 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
|
200 } |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
201 |
2
39f87ef69b2a
Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
202 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
|
203 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
|
204 } |
39f87ef69b2a
Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
205 |
1
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
206 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
|
207 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
|
208 } |
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 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
|
211 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
|
212 } |
6dbe2bbeef70
Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
213 |
9
d1972fc49a5b
Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
214 fn build_main_menu(application: >k::Application) { |
5
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
215 let quit = gio::SimpleAction::new("quit", None); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
216 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
|
217 application.add_action(&quit); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
218 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
|
219 |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
220 let about = gio::SimpleAction::new("about", None); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
221 application.add_action(&about); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
222 about.connect_activate(|_, _| { |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
223 let about = gtk::AboutDialog::new(); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
224 about.set_program_name("TabletEmu"); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
225 about.set_logo_icon_name(Some("input-tablet")); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
226 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
|
227 about.set_version(Some("0.1")); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
228 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
|
229 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
|
230 about.run(); |
9
d1972fc49a5b
Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
231 unsafe { |
d1972fc49a5b
Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
232 about.destroy(); |
d1972fc49a5b
Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
233 } |
5
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
234 }); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
235 |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
236 let menu = gio::Menu::new(); |
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 let file = gio::Menu::new(); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
239 file.append(Some("_Quit"), Some("app.quit")); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
240 menu.append_submenu(Some("_File"), &file); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
241 } |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
242 { |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
243 let help = gio::Menu::new(); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
244 help.append(Some("_About"), Some("app.about")); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
245 menu.append_submenu(Some("_Help"), &help); |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
246 } |
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
247 application.set_menubar(Some(&menu)); |
9
d1972fc49a5b
Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
248 } |
5
cdbf3a2c9af8
Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
249 |
9
d1972fc49a5b
Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
250 fn build_ui(application: >k::Application) { |
d1972fc49a5b
Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
251 build_main_menu(application); |
d1972fc49a5b
Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
252 |
d1972fc49a5b
Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
253 let state = match State::new() { |
d1972fc49a5b
Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
254 Ok(state) => state, |
0 | 255 Err(err) => { |
256 match err.kind() { | |
257 ErrorKind::NotFound => { | |
258 eprintln!("Couldn’t find /dev/uinput: {}", err); | |
259 eprintln!("Maybe you forgot to `modprobe uinput`?"); | |
260 } | |
261 ErrorKind::PermissionDenied => { | |
262 eprintln!("Couldn’t open /dev/uinput for writing: {}", err); | |
263 eprintln!("Maybe you aren’t allowed to create input devices?"); | |
264 } | |
265 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err), | |
266 } | |
9
d1972fc49a5b
Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
267 std::process::exit(1); |
0 | 268 } |
269 }; | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
270 |
0 | 271 let window = gtk::ApplicationWindow::new(application); |
272 window.set_title("tablet-emu"); | |
273 window.set_position(gtk::WindowPosition::Center); | |
274 | |
275 let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0); | |
276 let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0); | |
277 | |
4
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
278 macro_rules! impl_tool { |
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
279 ($tool:tt) => { |
8
51a6c86d3141
Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
280 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
|
281 let state_weak = Arc::downgrade(&state); |
4
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
282 tool.connect_clicked(move |b| { |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
283 let state = state_weak.upgrade().unwrap(); |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
284 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
|
285 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
|
286 "_Pen" => Key::ButtonToolPen, |
51a6c86d3141
Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
287 "_Rubber" => Key::ButtonToolRubber, |
51a6c86d3141
Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
288 "_Brush" => Key::ButtonToolBrush, |
51a6c86d3141
Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
289 "P_encil" => Key::ButtonToolPencil, |
51a6c86d3141
Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
290 "_Airbrush" => Key::ButtonToolAirbrush, |
2
39f87ef69b2a
Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
291 _ => unreachable!(), |
39f87ef69b2a
Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
292 }; |
10
06d77bb94a50
Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
293 state.select_tool(tool); |
0 | 294 }); |
4
2e074d185151
Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
295 tools_box.add(&tool); |
0 | 296 }; |
297 }; | |
8
51a6c86d3141
Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
298 impl_tool!("_Pen"); |
51a6c86d3141
Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
299 impl_tool!("_Rubber"); |
51a6c86d3141
Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
300 impl_tool!("_Brush"); |
51a6c86d3141
Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
301 impl_tool!("P_encil"); |
51a6c86d3141
Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
302 impl_tool!("_Airbrush"); |
0 | 303 |
304 let drawing_area = gtk::DrawingArea::new(); | |
305 drawing_area.set_size_request(WIDTH, HEIGHT); | |
306 drawing_area.set_hexpand(true); | |
307 drawing_area.set_events( | |
308 gdk::EventMask::BUTTON_PRESS_MASK | |
309 | gdk::EventMask::BUTTON_RELEASE_MASK | |
310 | gdk::EventMask::POINTER_MOTION_MASK, | |
311 ); | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
312 let state_weak = Arc::downgrade(&state); |
0 | 313 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
|
314 let state = state_weak.upgrade().unwrap(); |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
315 let mut state = state.lock().unwrap(); |
11
0193041f01d4
Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
10
diff
changeset
|
316 state.set_size(event.get_size()); |
0 | 317 true |
318 }); | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
319 let state_weak = Arc::downgrade(&state); |
0 | 320 drawing_area.connect_button_press_event(move |_, event| { |
321 if event.get_button() != 1 { | |
322 return Inhibit(false); | |
323 } | |
324 | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
325 let state = state_weak.upgrade().unwrap(); |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
326 let mut state = state.lock().unwrap(); |
0 | 327 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
|
328 state.press(x, y).unwrap(); |
0 | 329 Inhibit(false) |
330 }); | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
331 let state_weak = Arc::downgrade(&state); |
0 | 332 drawing_area.connect_button_release_event(move |_, event| { |
333 if event.get_button() != 1 { | |
334 return Inhibit(false); | |
335 } | |
336 | |
3
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
337 let state = state_weak.upgrade().unwrap(); |
72e63d6a3f8a
Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
338 let mut state = state.lock().unwrap(); |
0 | 339 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
|
340 state.release(x, y).unwrap(); |
0 | 341 Inhibit(false) |
342 }); | |
343 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
|
344 let mut state = state.lock().unwrap(); |
0 | 345 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
|
346 state.motion(x, y).unwrap(); |
0 | 347 Inhibit(false) |
348 }); | |
349 drawing_area.connect_draw(move |_, ctx| { | |
350 //println!("drawing {}", drawing_area); | |
351 ctx.set_source_rgb(1., 0., 0.); | |
352 ctx.set_operator(cairo::Operator::Screen); | |
353 ctx.paint(); | |
354 Inhibit(false) | |
355 }); | |
356 | |
357 hbox.add(&tools_box); | |
358 hbox.add(&drawing_area); | |
359 | |
360 window.add(&hbox); | |
361 | |
362 window.show_all(); | |
363 } | |
364 | |
365 fn main() { | |
366 let application = gtk::Application::new( | |
367 Some("fr.linkmauve.TabletEmu"), | |
368 gio::ApplicationFlags::empty(), | |
369 ) | |
370 .expect("Initialisation failed…"); | |
371 application.connect_activate(build_ui); | |
372 application.run(&args().collect::<Vec<_>>()); | |
373 } |