annotate src/gtk.rs @ 14:adab13145994

Add support for remote clients.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 02 Nov 2020 00:06:09 +0100
parents 97e543f50f62
children d103f7cca0bd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 // Tablet emulator, for people who don’t own one
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 // Copyright © 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 //
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 // This program is free software: you can redistribute it and/or modify
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 // it under the terms of the GNU Affero General Public License as published by
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 // the Free Software Foundation, either version 3 of the License, or
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 // (at your option) any later version.
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 //
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 // This program is distributed in the hope that it will be useful,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 // GNU Affero General Public License for more details.
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 //
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 // You should have received a copy of the GNU Affero General Public License
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 use gio::prelude::*;
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 use glib::clone;
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 use gtk::prelude::*;
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 use std::io::ErrorKind;
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
22 use std::sync::{Arc, Mutex};
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 use input_linux::Key;
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 use crate::{
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 state::State,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 DEFAULT_WIDTH, DEFAULT_HEIGHT,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 };
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 fn build_main_menu(application: &gtk::Application) {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 let quit = gio::SimpleAction::new("quit", None);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 application.set_accels_for_action("app.quit", &["<Control>q"]);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 application.add_action(&quit);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 quit.connect_activate(clone!(@weak application => move |_, _| application.quit()));
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 let about = gio::SimpleAction::new("about", None);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 application.add_action(&about);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 about.connect_activate(|_, _| {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 let about = gtk::AboutDialog::new();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 about.set_program_name("TabletEmu");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 about.set_logo_icon_name(Some("input-tablet"));
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 about.set_website(Some("https://hg.linkmauve.fr/tablet-emu"));
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 about.set_version(Some("0.1"));
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 about.set_license_type(gtk::License::Agpl30);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 about.set_copyright(Some("© 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>"));
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 about.run();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 unsafe {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 about.destroy();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 });
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 let menu = gio::Menu::new();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 let file = gio::Menu::new();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 file.append(Some("_Quit"), Some("app.quit"));
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 menu.append_submenu(Some("_File"), &file);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 let help = gio::Menu::new();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 help.append(Some("_About"), Some("app.about"));
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 menu.append_submenu(Some("_Help"), &help);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 application.set_menubar(Some(&menu));
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 fn build_ui(application: &gtk::Application) {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 build_main_menu(application);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 let state = match State::new() {
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
70 Ok(state) => Arc::new(Mutex::new(state)),
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 Err(err) => {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 match err.kind() {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 ErrorKind::NotFound => {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 eprintln!("Couldn’t find /dev/uinput: {}", err);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 eprintln!("Maybe you forgot to `modprobe uinput`?");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 ErrorKind::PermissionDenied => {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 eprintln!("Couldn’t open /dev/uinput for writing: {}", err);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 eprintln!("Maybe you aren’t allowed to create input devices?");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err),
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 std::process::exit(1);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 };
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 let window = gtk::ApplicationWindow::new(application);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 window.set_title("tablet-emu");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 window.set_position(gtk::WindowPosition::Center);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 macro_rules! impl_tool {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 ($tool:tt) => {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 let tool = gtk::Button::with_mnemonic($tool);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 let state_weak = Arc::downgrade(&state);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 tool.connect_clicked(move |b| {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 let state = state_weak.upgrade().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 let mut state = state.lock().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 let tool = match b.get_label().unwrap().as_str() {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 "_Pen" => Key::ButtonToolPen,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 "_Rubber" => Key::ButtonToolRubber,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 "_Brush" => Key::ButtonToolBrush,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 "P_encil" => Key::ButtonToolPencil,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 "_Airbrush" => Key::ButtonToolAirbrush,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 _ => unreachable!(),
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 };
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 state.select_tool(tool);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 });
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 tools_box.add(&tool);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 };
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 };
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 impl_tool!("_Pen");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115 impl_tool!("_Rubber");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 impl_tool!("_Brush");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 impl_tool!("P_encil");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 impl_tool!("_Airbrush");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 let drawing_area = gtk::DrawingArea::new();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 drawing_area.set_size_request(DEFAULT_WIDTH, DEFAULT_HEIGHT);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 drawing_area.set_hexpand(true);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 drawing_area.set_events(
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 gdk::EventMask::BUTTON_PRESS_MASK
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 | gdk::EventMask::BUTTON_RELEASE_MASK
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 | gdk::EventMask::POINTER_MOTION_MASK,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 );
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 let state_weak = Arc::downgrade(&state);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 drawing_area.connect_configure_event(move |_, event| {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 let state = state_weak.upgrade().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 let mut state = state.lock().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 state.set_size(event.get_size());
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 true
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 });
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 let state_weak = Arc::downgrade(&state);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 drawing_area.connect_button_press_event(move |_, event| {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 if event.get_button() != 1 {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138 return Inhibit(false);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 let state = state_weak.upgrade().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 let mut state = state.lock().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 let (x, y) = event.get_position();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 state.press(x, y).unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 Inhibit(false)
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 });
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 let state_weak = Arc::downgrade(&state);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 drawing_area.connect_button_release_event(move |_, event| {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 if event.get_button() != 1 {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 return Inhibit(false);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 let state = state_weak.upgrade().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 let mut state = state.lock().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 let (x, y) = event.get_position();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156 state.release(x, y).unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 Inhibit(false)
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 });
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 drawing_area.connect_motion_notify_event(move |_, event| {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 let mut state = state.lock().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 let (x, y) = event.get_position();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162 state.motion(x, y).unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 Inhibit(false)
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164 });
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165 drawing_area.connect_draw(move |_, ctx| {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166 //println!("drawing {}", drawing_area);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 ctx.set_source_rgb(1., 0., 0.);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 ctx.set_operator(cairo::Operator::Screen);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 ctx.paint();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170 Inhibit(false)
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 });
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173 hbox.add(&tools_box);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174 hbox.add(&drawing_area);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 window.add(&hbox);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178 window.show_all();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
180
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
181 pub fn main(args: &[String]) {
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182 let application = gtk::Application::new(
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
183 Some("fr.linkmauve.TabletEmu"),
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184 gio::ApplicationFlags::empty(),
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185 )
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
186 .expect("Initialisation failed…");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
187 application.connect_activate(build_ui);
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
188 application.run(args);
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189 }