annotate src/main.rs @ 12:d43c31aff57c

Split uinput helpers into another module.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 01 Nov 2020 16:01:00 +0100
parents 0193041f01d4
children 97e543f50f62
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::io::ErrorKind;
11
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10
diff changeset
23 use std::sync::Arc;
12
d43c31aff57c Split uinput helpers into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 11
diff changeset
24 use input_linux::Key;
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25
12
d43c31aff57c Split uinput helpers into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 11
diff changeset
26 mod uinput;
11
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10
diff changeset
27 mod state;
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10
diff changeset
28 use state::State;
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10
diff changeset
29
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 const WIDTH: i32 = 320;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 const HEIGHT: i32 = 180;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 const MAX_X: i32 = 69920;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 const MAX_Y: i32 = 39980;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35
9
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
36 fn build_main_menu(application: &gtk::Application) {
5
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
37 let quit = gio::SimpleAction::new("quit", None);
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
38 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
39 application.add_action(&quit);
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
40 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
41
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
42 let about = gio::SimpleAction::new("about", None);
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
43 application.add_action(&about);
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
44 about.connect_activate(|_, _| {
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
45 let about = gtk::AboutDialog::new();
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
46 about.set_program_name("TabletEmu");
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
47 about.set_logo_icon_name(Some("input-tablet"));
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
48 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
49 about.set_version(Some("0.1"));
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
50 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
51 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
52 about.run();
9
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
53 unsafe {
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
54 about.destroy();
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
55 }
5
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
56 });
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
57
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
58 let menu = gio::Menu::new();
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
59 {
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
60 let file = gio::Menu::new();
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
61 file.append(Some("_Quit"), Some("app.quit"));
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
62 menu.append_submenu(Some("_File"), &file);
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
63 }
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
64 {
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
65 let help = gio::Menu::new();
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
66 help.append(Some("_About"), Some("app.about"));
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
67 menu.append_submenu(Some("_Help"), &help);
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
68 }
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
69 application.set_menubar(Some(&menu));
9
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
70 }
5
cdbf3a2c9af8 Add a very simple menu.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
71
9
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
72 fn build_ui(application: &gtk::Application) {
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
73 build_main_menu(application);
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
74
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
75 let state = match State::new() {
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
76 Ok(state) => state,
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 Err(err) => {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 match err.kind() {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 ErrorKind::NotFound => {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 eprintln!("Couldn’t find /dev/uinput: {}", err);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 eprintln!("Maybe you forgot to `modprobe uinput`?");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 ErrorKind::PermissionDenied => {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 eprintln!("Couldn’t open /dev/uinput for writing: {}", err);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 eprintln!("Maybe you aren’t allowed to create input devices?");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err),
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 }
9
d1972fc49a5b Reorganise the code a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8
diff changeset
89 std::process::exit(1);
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 };
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
92
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 let window = gtk::ApplicationWindow::new(application);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 window.set_title("tablet-emu");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 window.set_position(gtk::WindowPosition::Center);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99
4
2e074d185151 Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
100 macro_rules! impl_tool {
2e074d185151 Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
101 ($tool:tt) => {
8
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
102 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
103 let state_weak = Arc::downgrade(&state);
4
2e074d185151 Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
104 tool.connect_clicked(move |b| {
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
105 let state = state_weak.upgrade().unwrap();
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
106 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
107 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
108 "_Pen" => Key::ButtonToolPen,
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
109 "_Rubber" => Key::ButtonToolRubber,
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
110 "_Brush" => Key::ButtonToolBrush,
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
111 "P_encil" => Key::ButtonToolPencil,
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
112 "_Airbrush" => Key::ButtonToolAirbrush,
2
39f87ef69b2a Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
113 _ => unreachable!(),
39f87ef69b2a Add support for selecting tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
114 };
10
06d77bb94a50 Move more state handling inside of State.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9
diff changeset
115 state.select_tool(tool);
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 });
4
2e074d185151 Simplify tool buttons.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
117 tools_box.add(&tool);
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 };
8
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
120 impl_tool!("_Pen");
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
121 impl_tool!("_Rubber");
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
122 impl_tool!("_Brush");
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
123 impl_tool!("P_encil");
51a6c86d3141 Add mnemonic to all current tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
124 impl_tool!("_Airbrush");
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 let drawing_area = gtk::DrawingArea::new();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 drawing_area.set_size_request(WIDTH, HEIGHT);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 drawing_area.set_hexpand(true);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 drawing_area.set_events(
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 gdk::EventMask::BUTTON_PRESS_MASK
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 | gdk::EventMask::BUTTON_RELEASE_MASK
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 | gdk::EventMask::POINTER_MOTION_MASK,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 );
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
134 let state_weak = Arc::downgrade(&state);
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 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
136 let state = state_weak.upgrade().unwrap();
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
137 let mut state = state.lock().unwrap();
11
0193041f01d4 Split State into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10
diff changeset
138 state.set_size(event.get_size());
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 true
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 });
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
141 let state_weak = Arc::downgrade(&state);
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 drawing_area.connect_button_press_event(move |_, event| {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 if event.get_button() != 1 {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 return Inhibit(false);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
147 let state = state_weak.upgrade().unwrap();
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
148 let mut state = state.lock().unwrap();
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 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
150 state.press(x, y).unwrap();
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151 Inhibit(false)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 });
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
153 let state_weak = Arc::downgrade(&state);
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 drawing_area.connect_button_release_event(move |_, event| {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 if event.get_button() != 1 {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156 return Inhibit(false);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158
3
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
159 let state = state_weak.upgrade().unwrap();
72e63d6a3f8a Simplify state handling into a single struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
160 let mut state = state.lock().unwrap();
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 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
162 state.release(x, y).unwrap();
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 Inhibit(false)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164 });
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165 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
166 let mut state = state.lock().unwrap();
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 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
168 state.motion(x, y).unwrap();
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 Inhibit(false)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170 });
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 drawing_area.connect_draw(move |_, ctx| {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 //println!("drawing {}", drawing_area);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173 ctx.set_source_rgb(1., 0., 0.);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174 ctx.set_operator(cairo::Operator::Screen);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 ctx.paint();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 Inhibit(false)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 });
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 hbox.add(&tools_box);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
180 hbox.add(&drawing_area);
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 window.add(&hbox);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
183
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184 window.show_all();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
186
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
187 fn main() {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
188 let application = gtk::Application::new(
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189 Some("fr.linkmauve.TabletEmu"),
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
190 gio::ApplicationFlags::empty(),
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
191 )
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
192 .expect("Initialisation failed…");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
193 application.connect_activate(build_ui);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
194 application.run(&args().collect::<Vec<_>>());
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195 }