annotate src/bin/server.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
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 last = Some((0., 0.));
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
47 state.set_size(320, 240);
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 loop {
19
ba09079686a0 Add support for different endianness between client and server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 18
diff changeset
49 let mut buf: [u8; 16] = Default::default();
ba09079686a0 Add support for different endianness between client and server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 18
diff changeset
50 let (amount, source) = socket.recv_from(&mut buf)?;
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 if amount != std::mem::size_of::<Event>() {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 eprintln!("Invalid data length: {}", amount);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 continue;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 }
19
ba09079686a0 Add support for different endianness between client and server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 18
diff changeset
55 let event: Event = bincode::deserialize(&buf).unwrap();
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 println!("{:?} from {:?}", event, source);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 if event.buttons.contains(Buttons::A) {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 state.select_tool(Key::ButtonToolPen);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 } else if event.buttons.contains(Buttons::B) {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 state.select_tool(Key::ButtonToolRubber);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 } else if event.buttons.contains(Buttons::X) {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 state.select_tool(Key::ButtonToolBrush);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 } else if event.buttons.contains(Buttons::Y) {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 state.select_tool(Key::ButtonToolPencil);
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 } else if event.buttons.contains(Buttons::SELECT) {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 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
67 } 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
68 println!(
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
69 "set_size({}, {})",
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
70 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
71 );
17
0bce7fe96937 Add a client, copy of the GTK interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 16
diff changeset
72 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
73 }
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 let (x, y) = event.touch;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 if event.buttons.contains(Buttons::TOUCH) {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 if let None = last {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 state.press(x as f64, y as f64)?;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 last = Some((x as f64, y as f64));
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 continue;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 }
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 } else {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 if let Some((x, y)) = last {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 state.release(x, y)?;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 last = None;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 continue;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 }
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 state.motion(x as f64, y as f64)?;
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 last = Some((x as f64, y as f64));
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 }
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
18
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
93 pub fn main() {
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 17
diff changeset
94 let args: Vec<_> = std::env::args().collect();
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 let address = if args.len() > 1 {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 args[1].clone()
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 } else {
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 String::from("0.0.0.0:16150")
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 };
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 run_server(&address).unwrap();
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 }