comparison 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
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::net::UdpSocket;
22 use std::sync::{Arc, Mutex};
23 use tablet_emu::protocol::{Buttons, Event};
24 use tablet_emu::{state::FakeState, DEFAULT_HEIGHT, DEFAULT_WIDTH};
25
26 fn build_main_menu(app: &gtk::Application) {
27 let quit = gio::SimpleAction::new("quit", None);
28 app.set_accels_for_action("app.quit", &["<Control>q"]);
29 app.add_action(&quit);
30 quit.connect_activate(clone!(@weak app => move |_, _| app.quit()));
31
32 let about = gio::SimpleAction::new("about", None);
33 app.add_action(&about);
34 about.connect_activate(|_, _| {
35 let about = gtk::AboutDialog::builder()
36 .program_name("TabletEmu")
37 .logo_icon_name("input-tablet")
38 .website("https://hg.linkmauve.fr/tablet-emu")
39 .version("0.1")
40 .license_type(gtk::License::Agpl30)
41 .copyright("© 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>")
42 .build();
43 //about.run();
44 about.destroy();
45 });
46
47 let menu = gio::Menu::new();
48 {
49 let file = gio::Menu::new();
50 file.append(Some("_Quit"), Some("app.quit"));
51 menu.append_submenu(Some("_File"), &file);
52 }
53 {
54 let help = gio::Menu::new();
55 help.append(Some("_About"), Some("app.about"));
56 menu.append_submenu(Some("_Help"), &help);
57 }
58 app.set_menubar(Some(&menu));
59 }
60
61 fn build_ui(app: &gtk::Application) {
62 build_main_menu(app);
63
64 let address = "127.0.0.1:16150";
65 let socket = Arc::new(Mutex::new(UdpSocket::bind("127.0.0.1:4321").unwrap()));
66 println!("opened {:?}", socket);
67
68 let send = move |event: &Event| {
69 // TODO: Yolo-alignment.
70 let buf: &[u8; std::mem::size_of::<Event>()] = unsafe { std::mem::transmute(event) };
71 let socket = socket.lock().unwrap();
72 let amount = socket.send_to(buf, address).unwrap();
73 assert_eq!(amount, std::mem::size_of::<Event>());
74 };
75 let send2 = send.clone();
76 let send3 = send.clone();
77 let send4 = send.clone();
78
79 let state = Arc::new(Mutex::new(FakeState::new()));
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 let send = send.clone();
93 tool.connect_clicked(move |b| {
94 let state = state_weak.upgrade().unwrap();
95 let mut state = state.lock().unwrap();
96 let tool = match b.label().unwrap().as_str() {
97 "_Pen" => Key::ButtonToolPen,
98 "_Rubber" => Key::ButtonToolRubber,
99 "_Brush" => Key::ButtonToolBrush,
100 "P_encil" => Key::ButtonToolPencil,
101 "_Airbrush" => Key::ButtonToolAirbrush,
102 _ => unreachable!(),
103 };
104 state.select_tool(tool);
105
106 let mut event: Event = Default::default();
107 event.buttons = match tool {
108 Key::ButtonToolPen => Buttons::A,
109 Key::ButtonToolRubber => Buttons::B,
110 Key::ButtonToolBrush => Buttons::X,
111 Key::ButtonToolPencil => Buttons::Y,
112 Key::ButtonToolAirbrush => Buttons::SELECT,
113 _ => unreachable!(),
114 };
115 send(&event);
116 });
117 tools_box.append(&tool);
118 };
119 }
120 impl_tool!("_Pen");
121 impl_tool!("_Rubber");
122 impl_tool!("_Brush");
123 impl_tool!("P_encil");
124 impl_tool!("_Airbrush");
125
126 let drawing_area = gtk::DrawingArea::builder()
127 .content_width(DEFAULT_WIDTH)
128 .content_height(DEFAULT_HEIGHT)
129 .hexpand(true)
130 .build();
131 let gesture_click = gtk::GestureClick::new();
132 let event_controller = gtk::EventControllerMotion::new();
133 let state_weak = Arc::downgrade(&state);
134 drawing_area.connect_resize(move |_, width, height| {
135 let state = state_weak.upgrade().unwrap();
136 let mut state = state.lock().unwrap();
137 state.set_size(width, height);
138
139 let mut event: Event = Default::default();
140 event.buttons = Buttons::RESIZE;
141 event.touch = (width as u16, height as u16);
142 send(&event);
143 });
144 let state_weak = Arc::downgrade(&state);
145 gesture_click.connect_pressed(move |_, n_press, x, y| {
146 if n_press != 1 {
147 return;
148 }
149
150 let state = state_weak.upgrade().unwrap();
151 let mut state = state.lock().unwrap();
152 state.press(x, y).unwrap();
153
154 let mut event: Event = Default::default();
155 event.buttons = Buttons::TOUCH;
156 event.touch = (x as u16, y as u16);
157 send2(&event);
158 });
159 let state_weak = Arc::downgrade(&state);
160 gesture_click.connect_released(move |_, n_press, x, y| {
161 if n_press != 1 {
162 return;
163 }
164
165 let state = state_weak.upgrade().unwrap();
166 let mut state = state.lock().unwrap();
167 state.release(x, y).unwrap();
168
169 let mut event: Event = Default::default();
170 event.touch = (x as u16, y as u16);
171 send3(&event);
172 });
173 event_controller.connect_motion(move |_, x, y| {
174 let mut state = state.lock().unwrap();
175 state.motion(x, y).unwrap();
176
177 if state.is_pressed() {
178 let mut event: Event = Default::default();
179 event.buttons = Buttons::TOUCH;
180 event.touch = (x as u16, y as u16);
181 send4(&event);
182 }
183 });
184 drawing_area.add_controller(&gesture_click);
185 drawing_area.add_controller(&event_controller);
186 drawing_area.set_draw_func(move |_, ctx, _, _| {
187 ctx.set_source_rgb(1., 0., 0.);
188 ctx.set_operator(cairo::Operator::Screen);
189 ctx.paint().unwrap();
190 });
191
192 hbox.append(&tools_box);
193 hbox.append(&drawing_area);
194
195 let window = gtk::ApplicationWindow::builder()
196 .application(app)
197 .title("tablet-emu")
198 .default_width(800)
199 .default_height(480)
200 .child(&hbox)
201 .build();
202
203 window.show();
204 }
205
206 pub fn main() {
207 let app = gtk::Application::builder()
208 .application_id("fr.linkmauve.TabletEmu")
209 .build();
210 app.connect_activate(build_ui);
211 app.run();
212 }