Mercurial > tablet-emu
annotate src/bin/client.rs @ 20:f81d56c5da76 default tip
Use features to condition building binaries.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 26 Aug 2021 23:49:52 +0200 |
parents | ba09079686a0 |
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 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: >k::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: >k::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| { |
19
ba09079686a0
Add support for different endianness between client and server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
69 let buf = bincode::serialize(event).unwrap(); |
17
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 let socket = socket.lock().unwrap(); |
19
ba09079686a0
Add support for different endianness between client and server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
71 let amount = socket.send_to(&buf, address).unwrap(); |
17
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 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
|
73 }; |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 let send2 = send.clone(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 let send3 = send.clone(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 let send4 = send.clone(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
78 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
|
79 |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
80 let hbox = gtk::Box::builder() |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
81 .orientation(gtk::Orientation::Horizontal) |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
82 .build(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
83 let tools_box = gtk::Box::builder() |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
84 .orientation(gtk::Orientation::Vertical) |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
85 .build(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
86 |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
87 macro_rules! impl_tool { |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
88 ($tool:tt) => { |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
89 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
|
90 let state_weak = Arc::downgrade(&state); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
91 let send = send.clone(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
92 tool.connect_clicked(move |b| { |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
93 let state = state_weak.upgrade().unwrap(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
94 let mut state = state.lock().unwrap(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
95 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
|
96 "_Pen" => Key::ButtonToolPen, |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
97 "_Rubber" => Key::ButtonToolRubber, |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
98 "_Brush" => Key::ButtonToolBrush, |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
99 "P_encil" => Key::ButtonToolPencil, |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
100 "_Airbrush" => Key::ButtonToolAirbrush, |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
101 _ => unreachable!(), |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
102 }; |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
103 state.select_tool(tool); |
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 let mut event: Event = Default::default(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
106 event.buttons = match tool { |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
107 Key::ButtonToolPen => Buttons::A, |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
108 Key::ButtonToolRubber => Buttons::B, |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
109 Key::ButtonToolBrush => Buttons::X, |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
110 Key::ButtonToolPencil => Buttons::Y, |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
111 Key::ButtonToolAirbrush => Buttons::SELECT, |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
112 _ => unreachable!(), |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
113 }; |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
114 send(&event); |
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 tools_box.append(&tool); |
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 } |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
119 impl_tool!("_Pen"); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
120 impl_tool!("_Rubber"); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
121 impl_tool!("_Brush"); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
122 impl_tool!("P_encil"); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
123 impl_tool!("_Airbrush"); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
124 |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
125 let drawing_area = gtk::DrawingArea::builder() |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
126 .content_width(DEFAULT_WIDTH) |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
127 .content_height(DEFAULT_HEIGHT) |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
128 .hexpand(true) |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
129 .build(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
130 let gesture_click = gtk::GestureClick::new(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
131 let event_controller = gtk::EventControllerMotion::new(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
132 let state_weak = Arc::downgrade(&state); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
133 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
|
134 let state = state_weak.upgrade().unwrap(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
135 let mut state = state.lock().unwrap(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
136 state.set_size(width, height); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
137 |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
138 let mut event: Event = Default::default(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
139 event.buttons = Buttons::RESIZE; |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
140 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
|
141 send(&event); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
142 }); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
143 let state_weak = Arc::downgrade(&state); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
144 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
|
145 if n_press != 1 { |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
146 return; |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
147 } |
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 let state = state_weak.upgrade().unwrap(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
150 let mut state = state.lock().unwrap(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
151 state.press(x, y).unwrap(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
152 |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
153 let mut event: Event = Default::default(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
154 event.buttons = Buttons::TOUCH; |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
155 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
|
156 send2(&event); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
157 }); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
158 let state_weak = Arc::downgrade(&state); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
159 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
|
160 if n_press != 1 { |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
161 return; |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
162 } |
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 let state = state_weak.upgrade().unwrap(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
165 let mut state = state.lock().unwrap(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
166 state.release(x, y).unwrap(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
167 |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
168 let mut event: Event = Default::default(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
169 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
|
170 send3(&event); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
171 }); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
172 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
|
173 let mut state = state.lock().unwrap(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
174 state.motion(x, y).unwrap(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
175 |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
176 if state.is_pressed() { |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
177 let mut event: Event = Default::default(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
178 event.buttons = Buttons::TOUCH; |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
179 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
|
180 send4(&event); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
181 } |
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 drawing_area.add_controller(&gesture_click); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
184 drawing_area.add_controller(&event_controller); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
185 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
|
186 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
|
187 ctx.set_operator(cairo::Operator::Screen); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
188 ctx.paint().unwrap(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
189 }); |
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 hbox.append(&tools_box); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
192 hbox.append(&drawing_area); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
193 |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
194 let window = gtk::ApplicationWindow::builder() |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
195 .application(app) |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
196 .title("tablet-emu") |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
197 .default_width(800) |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
198 .default_height(480) |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
199 .child(&hbox) |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
200 .build(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
201 |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
202 window.show(); |
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 |
18
3f7b7a3ad8fe
Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
17
diff
changeset
|
205 pub fn main() { |
17
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
206 let app = gtk::Application::builder() |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
207 .application_id("fr.linkmauve.TabletEmu") |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
208 .build(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
209 app.connect_activate(build_ui); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
210 app.run(); |
0bce7fe96937
Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
211 } |