annotate src/bin/server.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/server.rs@0bce7fe96937
children ba09079686a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 // Tablet emulator, for people who don’t own one
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 // Copyright © 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 //
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 // This program is free software: you can redistribute it and/or modify
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 // it under the terms of the GNU Affero General Public License as published by
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 // the Free Software Foundation, either version 3 of the License, or
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 // (at your option) any later version.
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 //
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 // This program is distributed in the hope that it will be useful,
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 // GNU Affero General Public License for more details.
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 //
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 // You should have received a copy of the GNU Affero General Public License
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 use input_linux::Key;
16
478cf2a7d577 Reformat with rustfmt.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
18 use std::io::{self, ErrorKind};
478cf2a7d577 Reformat with rustfmt.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
19 use std::net::UdpSocket;
18
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
20 use tablet_emu::protocol::{Buttons, Event};
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
21 use tablet_emu::state::State;
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 pub fn run_server(address: &str) -> io::Result<()> {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 let mut state = match State::new() {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 Ok(state) => state,
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 Err(err) => {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 match err.kind() {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 ErrorKind::NotFound => {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 eprintln!("Couldn’t find /dev/uinput: {}", err);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 eprintln!("Maybe you forgot to `modprobe uinput`?");
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 }
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 ErrorKind::PermissionDenied => {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 eprintln!("Couldn’t open /dev/uinput for writing: {}", err);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 eprintln!("Maybe you aren’t allowed to create input devices?");
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 }
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err),
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 }
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 std::process::exit(1);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 }
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 };
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 let socket = UdpSocket::bind(address)?;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 println!("Listening on {:?}", socket);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 println!("Here is an example client: https://hg.linkmauve.fr/remote-gamepad");
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 let mut event: Event = Default::default();
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 let mut last = Some((0., 0.));
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
48 state.set_size(320, 240);
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 loop {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 // TODO: Yolo-alignment.
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 let buf: &mut [u8; 16] = unsafe { std::mem::transmute(&mut event) };
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 let (amount, source) = socket.recv_from(buf)?;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 if amount != std::mem::size_of::<Event>() {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 eprintln!("Invalid data length: {}", amount);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 continue;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 }
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 println!("{:?} from {:?}", event, source);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 if event.buttons.contains(Buttons::A) {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 state.select_tool(Key::ButtonToolPen);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 } else if event.buttons.contains(Buttons::B) {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 state.select_tool(Key::ButtonToolRubber);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 } else if event.buttons.contains(Buttons::X) {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 state.select_tool(Key::ButtonToolBrush);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 } else if event.buttons.contains(Buttons::Y) {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 state.select_tool(Key::ButtonToolPencil);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 } else if event.buttons.contains(Buttons::SELECT) {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 state.select_tool(Key::ButtonToolAirbrush);
17
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 16
diff changeset
68 } else if event.buttons.contains(Buttons::RESIZE) {
18
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
69 println!(
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
70 "set_size({}, {})",
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
71 event.touch.0 as i32, event.touch.1 as i32
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
72 );
17
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 16
diff changeset
73 state.set_size(event.touch.0 as i32, event.touch.1 as i32);
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 }
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 let (x, y) = event.touch;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 if event.buttons.contains(Buttons::TOUCH) {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 if let None = last {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 state.press(x as f64, y as f64)?;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 last = Some((x as f64, y as f64));
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 continue;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 }
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 } else {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 if let Some((x, y)) = last {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 state.release(x, y)?;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 last = None;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 continue;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 }
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 }
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 state.motion(x as f64, y as f64)?;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 last = Some((x as f64, y as f64));
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 }
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 }
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93
18
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
94 pub fn main() {
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
95 let args: Vec<_> = std::env::args().collect();
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 let address = if args.len() > 1 {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 args[1].clone()
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 } else {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 String::from("0.0.0.0:16150")
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 };
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 run_server(&address).unwrap();
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 }