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