Mercurial > tablet-emu
comparison src/bin/standalone.rs @ 18:3f7b7a3ad8fe
Build three binaries instead of using arguments.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 26 Aug 2021 22:15:55 +0200 |
parents | src/gtk.rs@478cf2a7d577 |
children |
comparison
equal
deleted
inserted
replaced
17:0bce7fe96937 | 18:3f7b7a3ad8fe |
---|---|
1 // Tablet emulator, for people who don’t own one | |
2 // Copyright © 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
3 // | |
4 // This program is free software: you can redistribute it and/or modify | |
5 // it under the terms of the GNU Affero General Public License as published by | |
6 // the Free Software Foundation, either version 3 of the License, or | |
7 // (at your option) any later version. | |
8 // | |
9 // This program is distributed in the hope that it will be useful, | |
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 // GNU Affero General Public License for more details. | |
13 // | |
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/>. | |
16 | |
17 use gio::prelude::*; | |
18 use glib::clone; | |
19 use gtk::prelude::*; | |
20 use input_linux::Key; | |
21 use std::io::ErrorKind; | |
22 use std::sync::{Arc, Mutex}; | |
23 use tablet_emu::{state::State, DEFAULT_HEIGHT, DEFAULT_WIDTH}; | |
24 | |
25 fn build_main_menu(app: >k::Application) { | |
26 let quit = gio::SimpleAction::new("quit", None); | |
27 app.set_accels_for_action("app.quit", &["<Control>q"]); | |
28 app.add_action(&quit); | |
29 quit.connect_activate(clone!(@weak app => move |_, _| app.quit())); | |
30 | |
31 let about = gio::SimpleAction::new("about", None); | |
32 app.add_action(&about); | |
33 about.connect_activate(|_, _| { | |
34 let about = gtk::AboutDialog::builder() | |
35 .program_name("TabletEmu") | |
36 .logo_icon_name("input-tablet") | |
37 .website("https://hg.linkmauve.fr/tablet-emu") | |
38 .version("0.1") | |
39 .license_type(gtk::License::Agpl30) | |
40 .copyright("© 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>") | |
41 .build(); | |
42 //about.run(); | |
43 about.destroy(); | |
44 }); | |
45 | |
46 let menu = gio::Menu::new(); | |
47 { | |
48 let file = gio::Menu::new(); | |
49 file.append(Some("_Quit"), Some("app.quit")); | |
50 menu.append_submenu(Some("_File"), &file); | |
51 } | |
52 { | |
53 let help = gio::Menu::new(); | |
54 help.append(Some("_About"), Some("app.about")); | |
55 menu.append_submenu(Some("_Help"), &help); | |
56 } | |
57 app.set_menubar(Some(&menu)); | |
58 } | |
59 | |
60 fn build_ui(app: >k::Application) { | |
61 build_main_menu(app); | |
62 | |
63 let state = match State::new() { | |
64 Ok(state) => Arc::new(Mutex::new(state)), | |
65 Err(err) => { | |
66 match err.kind() { | |
67 ErrorKind::NotFound => { | |
68 eprintln!("Couldn’t find /dev/uinput: {}", err); | |
69 eprintln!("Maybe you forgot to `modprobe uinput`?"); | |
70 } | |
71 ErrorKind::PermissionDenied => { | |
72 eprintln!("Couldn’t open /dev/uinput for writing: {}", err); | |
73 eprintln!("Maybe you aren’t allowed to create input devices?"); | |
74 } | |
75 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err), | |
76 } | |
77 std::process::exit(1); | |
78 } | |
79 }; | |
80 | |
81 let hbox = gtk::Box::builder() | |
82 .orientation(gtk::Orientation::Horizontal) | |
83 .build(); | |
84 let tools_box = gtk::Box::builder() | |
85 .orientation(gtk::Orientation::Vertical) | |
86 .build(); | |
87 | |
88 macro_rules! impl_tool { | |
89 ($tool:tt) => { | |
90 let tool = gtk::Button::with_mnemonic($tool); | |
91 let state_weak = Arc::downgrade(&state); | |
92 tool.connect_clicked(move |b| { | |
93 let state = state_weak.upgrade().unwrap(); | |
94 let mut state = state.lock().unwrap(); | |
95 let tool = match b.label().unwrap().as_str() { | |
96 "_Pen" => Key::ButtonToolPen, | |
97 "_Rubber" => Key::ButtonToolRubber, | |
98 "_Brush" => Key::ButtonToolBrush, | |
99 "P_encil" => Key::ButtonToolPencil, | |
100 "_Airbrush" => Key::ButtonToolAirbrush, | |
101 _ => unreachable!(), | |
102 }; | |
103 state.select_tool(tool); | |
104 }); | |
105 tools_box.append(&tool); | |
106 }; | |
107 } | |
108 impl_tool!("_Pen"); | |
109 impl_tool!("_Rubber"); | |
110 impl_tool!("_Brush"); | |
111 impl_tool!("P_encil"); | |
112 impl_tool!("_Airbrush"); | |
113 | |
114 let drawing_area = gtk::DrawingArea::builder() | |
115 .content_width(DEFAULT_WIDTH) | |
116 .content_height(DEFAULT_HEIGHT) | |
117 .hexpand(true) | |
118 .build(); | |
119 let gesture_click = gtk::GestureClick::new(); | |
120 let event_controller = gtk::EventControllerMotion::new(); | |
121 let state_weak = Arc::downgrade(&state); | |
122 drawing_area.connect_resize(move |_, width, height| { | |
123 let state = state_weak.upgrade().unwrap(); | |
124 let mut state = state.lock().unwrap(); | |
125 state.set_size(width, height); | |
126 }); | |
127 let state_weak = Arc::downgrade(&state); | |
128 gesture_click.connect_pressed(move |_, n_press, x, y| { | |
129 if n_press != 1 { | |
130 return; | |
131 } | |
132 | |
133 let state = state_weak.upgrade().unwrap(); | |
134 let mut state = state.lock().unwrap(); | |
135 state.press(x, y).unwrap(); | |
136 }); | |
137 let state_weak = Arc::downgrade(&state); | |
138 gesture_click.connect_released(move |_, n_press, x, y| { | |
139 if n_press != 1 { | |
140 return; | |
141 } | |
142 | |
143 let state = state_weak.upgrade().unwrap(); | |
144 let mut state = state.lock().unwrap(); | |
145 state.release(x, y).unwrap(); | |
146 }); | |
147 event_controller.connect_motion(move |_, x, y| { | |
148 let mut state = state.lock().unwrap(); | |
149 state.motion(x, y).unwrap(); | |
150 }); | |
151 drawing_area.add_controller(&gesture_click); | |
152 drawing_area.add_controller(&event_controller); | |
153 drawing_area.set_draw_func(move |_, ctx, _, _| { | |
154 ctx.set_source_rgb(1., 0., 0.); | |
155 ctx.set_operator(cairo::Operator::Screen); | |
156 ctx.paint().unwrap(); | |
157 }); | |
158 | |
159 hbox.append(&tools_box); | |
160 hbox.append(&drawing_area); | |
161 | |
162 let window = gtk::ApplicationWindow::builder() | |
163 .application(app) | |
164 .title("tablet-emu") | |
165 .default_width(800) | |
166 .default_height(480) | |
167 .child(&hbox) | |
168 .build(); | |
169 | |
170 window.show(); | |
171 } | |
172 | |
173 pub fn main() { | |
174 let app = gtk::Application::builder() | |
175 .application_id("fr.linkmauve.TabletEmu") | |
176 .build(); | |
177 app.connect_activate(build_ui); | |
178 app.run(); | |
179 } |