annotate src/bin/client.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/client.rs@0bce7fe96937
children ba09079686a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 // Tablet emulator, for people who don’t own one
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 // Copyright © 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 //
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 // This program is free software: you can redistribute it and/or modify
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 // it under the terms of the GNU Affero General Public License as published by
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 // the Free Software Foundation, either version 3 of the License, or
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 // (at your option) any later version.
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 //
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 // This program is distributed in the hope that it will be useful,
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 // GNU Affero General Public License for more details.
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 //
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 // You should have received a copy of the GNU Affero General Public License
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 use gio::prelude::*;
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 use glib::clone;
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 use gtk::prelude::*;
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 use input_linux::Key;
18
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
21 use std::net::UdpSocket;
17
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 use std::sync::{Arc, Mutex};
18
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
23 use tablet_emu::protocol::{Buttons, Event};
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
24 use tablet_emu::{state::FakeState, DEFAULT_HEIGHT, DEFAULT_WIDTH};
17
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 fn build_main_menu(app: &gtk::Application) {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 let quit = gio::SimpleAction::new("quit", None);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 app.set_accels_for_action("app.quit", &["<Control>q"]);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 app.add_action(&quit);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 quit.connect_activate(clone!(@weak app => move |_, _| app.quit()));
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 let about = gio::SimpleAction::new("about", None);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 app.add_action(&about);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 about.connect_activate(|_, _| {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 let about = gtk::AboutDialog::builder()
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 .program_name("TabletEmu")
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 .logo_icon_name("input-tablet")
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 .website("https://hg.linkmauve.fr/tablet-emu")
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 .version("0.1")
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 .license_type(gtk::License::Agpl30)
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 .copyright("© 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>")
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 .build();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 //about.run();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 about.destroy();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 });
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 let menu = gio::Menu::new();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 let file = gio::Menu::new();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 file.append(Some("_Quit"), Some("app.quit"));
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 menu.append_submenu(Some("_File"), &file);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 }
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 let help = gio::Menu::new();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 help.append(Some("_About"), Some("app.about"));
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 menu.append_submenu(Some("_Help"), &help);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 }
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 app.set_menubar(Some(&menu));
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 }
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 fn build_ui(app: &gtk::Application) {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 build_main_menu(app);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 let address = "127.0.0.1:16150";
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 let socket = Arc::new(Mutex::new(UdpSocket::bind("127.0.0.1:4321").unwrap()));
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 println!("opened {:?}", socket);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 let send = move |event: &Event| {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 // TODO: Yolo-alignment.
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 let buf: &[u8; std::mem::size_of::<Event>()] = unsafe { std::mem::transmute(event) };
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 let socket = socket.lock().unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 let amount = socket.send_to(buf, address).unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 assert_eq!(amount, std::mem::size_of::<Event>());
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 };
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 let send2 = send.clone();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 let send3 = send.clone();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 let send4 = send.clone();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 let state = Arc::new(Mutex::new(FakeState::new()));
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 let hbox = gtk::Box::builder()
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 .orientation(gtk::Orientation::Horizontal)
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 .build();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 let tools_box = gtk::Box::builder()
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 .orientation(gtk::Orientation::Vertical)
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 .build();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 macro_rules! impl_tool {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 ($tool:tt) => {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 let tool = gtk::Button::with_mnemonic($tool);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 let state_weak = Arc::downgrade(&state);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 let send = send.clone();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 tool.connect_clicked(move |b| {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 let state = state_weak.upgrade().unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 let mut state = state.lock().unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 let tool = match b.label().unwrap().as_str() {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 "_Pen" => Key::ButtonToolPen,
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 "_Rubber" => Key::ButtonToolRubber,
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 "_Brush" => Key::ButtonToolBrush,
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 "P_encil" => Key::ButtonToolPencil,
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 "_Airbrush" => Key::ButtonToolAirbrush,
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 _ => unreachable!(),
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 };
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 state.select_tool(tool);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 let mut event: Event = Default::default();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 event.buttons = match tool {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 Key::ButtonToolPen => Buttons::A,
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 Key::ButtonToolRubber => Buttons::B,
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 Key::ButtonToolBrush => Buttons::X,
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 Key::ButtonToolPencil => Buttons::Y,
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 Key::ButtonToolAirbrush => Buttons::SELECT,
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 _ => unreachable!(),
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 };
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115 send(&event);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 });
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 tools_box.append(&tool);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 };
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 }
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 impl_tool!("_Pen");
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 impl_tool!("_Rubber");
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 impl_tool!("_Brush");
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 impl_tool!("P_encil");
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 impl_tool!("_Airbrush");
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 let drawing_area = gtk::DrawingArea::builder()
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 .content_width(DEFAULT_WIDTH)
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 .content_height(DEFAULT_HEIGHT)
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 .hexpand(true)
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 .build();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 let gesture_click = gtk::GestureClick::new();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 let event_controller = gtk::EventControllerMotion::new();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 let state_weak = Arc::downgrade(&state);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 drawing_area.connect_resize(move |_, width, height| {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 let state = state_weak.upgrade().unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 let mut state = state.lock().unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 state.set_size(width, height);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 let mut event: Event = Default::default();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 event.buttons = Buttons::RESIZE;
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 event.touch = (width as u16, height as u16);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 send(&event);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 });
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 let state_weak = Arc::downgrade(&state);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 gesture_click.connect_pressed(move |_, n_press, x, y| {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 if n_press != 1 {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 return;
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 }
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 let state = state_weak.upgrade().unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151 let mut state = state.lock().unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 state.press(x, y).unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 let mut event: Event = Default::default();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 event.buttons = Buttons::TOUCH;
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156 event.touch = (x as u16, y as u16);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 send2(&event);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 });
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 let state_weak = Arc::downgrade(&state);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 gesture_click.connect_released(move |_, n_press, x, y| {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 if n_press != 1 {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162 return;
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 }
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165 let state = state_weak.upgrade().unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166 let mut state = state.lock().unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 state.release(x, y).unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 let mut event: Event = Default::default();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170 event.touch = (x as u16, y as u16);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 send3(&event);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 });
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173 event_controller.connect_motion(move |_, x, y| {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174 let mut state = state.lock().unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 state.motion(x, y).unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 if state.is_pressed() {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178 let mut event: Event = Default::default();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 event.buttons = Buttons::TOUCH;
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
180 event.touch = (x as u16, y as u16);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
181 send4(&event);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182 }
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
183 });
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184 drawing_area.add_controller(&gesture_click);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185 drawing_area.add_controller(&event_controller);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
186 drawing_area.set_draw_func(move |_, ctx, _, _| {
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
187 ctx.set_source_rgb(1., 0., 0.);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
188 ctx.set_operator(cairo::Operator::Screen);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189 ctx.paint().unwrap();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
190 });
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
191
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
192 hbox.append(&tools_box);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
193 hbox.append(&drawing_area);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
194
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195 let window = gtk::ApplicationWindow::builder()
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
196 .application(app)
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
197 .title("tablet-emu")
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
198 .default_width(800)
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
199 .default_height(480)
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
200 .child(&hbox)
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
201 .build();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
202
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
203 window.show();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
204 }
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
205
18
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
206 pub fn main() {
17
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
207 let app = gtk::Application::builder()
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
208 .application_id("fr.linkmauve.TabletEmu")
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
209 .build();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
210 app.connect_activate(build_ui);
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
211 app.run();
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
212 }