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