comparison src/main.rs @ 9:d1972fc49a5b

Reorganise the code a bit.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 01 Nov 2020 15:23:57 +0100
parents 51a6c86d3141
children 06d77bb94a50
comparison
equal deleted inserted replaced
8:51a6c86d3141 9:d1972fc49a5b
13 // 13 //
14 // You should have received a copy of the GNU Affero General Public License 14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>. 15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17 use gio::prelude::*; 17 use gio::prelude::*;
18 use glib::clone;
18 use gtk::prelude::*; 19 use gtk::prelude::*;
19 use glib::clone;
20 20
21 use std::env::args; 21 use std::env::args;
22 use std::fs::{File, OpenOptions}; 22 use std::fs::{File, OpenOptions};
23 use std::io::ErrorKind; 23 use std::io::ErrorKind;
24 use std::sync::{Arc, Mutex}; 24 use std::sync::{Arc, Mutex};
214 height: f64, 214 height: f64,
215 selected_tool: Key, 215 selected_tool: Key,
216 pressed: bool, 216 pressed: bool,
217 } 217 }
218 218
219 fn build_ui(application: &gtk::Application) { 219 impl State {
220 fn new() -> std::io::Result<Arc<Mutex<State>>> {
221 let dev = create_uinput_device()?;
222 println!(
223 "New device at {:?} ({:?})",
224 dev.evdev_path()?,
225 dev.sys_path()?
226 );
227
228 Ok(Arc::new(Mutex::new(State {
229 dev,
230 width: WIDTH as f64,
231 height: HEIGHT as f64,
232 selected_tool: Key::ButtonToolPen,
233 pressed: false,
234 })))
235 }
236 }
237
238 fn build_main_menu(application: &gtk::Application) {
220 let quit = gio::SimpleAction::new("quit", None); 239 let quit = gio::SimpleAction::new("quit", None);
221 application.set_accels_for_action("app.quit", &["<Control>q"]); 240 application.set_accels_for_action("app.quit", &["<Control>q"]);
222 application.add_action(&quit); 241 application.add_action(&quit);
223 quit.connect_activate(clone!(@weak application => move |_, _| application.quit())); 242 quit.connect_activate(clone!(@weak application => move |_, _| application.quit()));
224 243
231 about.set_website(Some("https://hg.linkmauve.fr/tablet-emu")); 250 about.set_website(Some("https://hg.linkmauve.fr/tablet-emu"));
232 about.set_version(Some("0.1")); 251 about.set_version(Some("0.1"));
233 about.set_license_type(gtk::License::Agpl30); 252 about.set_license_type(gtk::License::Agpl30);
234 about.set_copyright(Some("© 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>")); 253 about.set_copyright(Some("© 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>"));
235 about.run(); 254 about.run();
236 unsafe { about.destroy(); } 255 unsafe {
256 about.destroy();
257 }
237 }); 258 });
238 259
239 let menu = gio::Menu::new(); 260 let menu = gio::Menu::new();
240 { 261 {
241 let file = gio::Menu::new(); 262 let file = gio::Menu::new();
246 let help = gio::Menu::new(); 267 let help = gio::Menu::new();
247 help.append(Some("_About"), Some("app.about")); 268 help.append(Some("_About"), Some("app.about"));
248 menu.append_submenu(Some("_Help"), &help); 269 menu.append_submenu(Some("_Help"), &help);
249 } 270 }
250 application.set_menubar(Some(&menu)); 271 application.set_menubar(Some(&menu));
251 272 }
252 let dev = match create_uinput_device() { 273
253 Ok(dev) => dev, 274 fn build_ui(application: &gtk::Application) {
275 build_main_menu(application);
276
277 let state = match State::new() {
278 Ok(state) => state,
254 Err(err) => { 279 Err(err) => {
255 match err.kind() { 280 match err.kind() {
256 ErrorKind::NotFound => { 281 ErrorKind::NotFound => {
257 eprintln!("Couldn’t find /dev/uinput: {}", err); 282 eprintln!("Couldn’t find /dev/uinput: {}", err);
258 eprintln!("Maybe you forgot to `modprobe uinput`?"); 283 eprintln!("Maybe you forgot to `modprobe uinput`?");
261 eprintln!("Couldn’t open /dev/uinput for writing: {}", err); 286 eprintln!("Couldn’t open /dev/uinput for writing: {}", err);
262 eprintln!("Maybe you aren’t allowed to create input devices?"); 287 eprintln!("Maybe you aren’t allowed to create input devices?");
263 } 288 }
264 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err), 289 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err),
265 } 290 }
266 return; 291 std::process::exit(1);
267 } 292 }
268 }; 293 };
269 println!(
270 "New device at {:?} ({:?})",
271 dev.evdev_path().unwrap(),
272 dev.sys_path().unwrap()
273 );
274
275 let state = Arc::new(Mutex::new(State {
276 dev,
277 width: WIDTH as f64,
278 height: HEIGHT as f64,
279 selected_tool: Key::ButtonToolPen,
280 pressed: false,
281 }));
282 294
283 let window = gtk::ApplicationWindow::new(application); 295 let window = gtk::ApplicationWindow::new(application);
284
285 window.set_title("tablet-emu"); 296 window.set_title("tablet-emu");
286 window.set_position(gtk::WindowPosition::Center); 297 window.set_position(gtk::WindowPosition::Center);
287 298
288 let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0); 299 let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0);
289 let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0); 300 let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0);