changeset 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 0bce7fe96937
children ba09079686a0
files Cargo.toml src/bin/client.rs src/bin/server.rs src/bin/standalone.rs src/client.rs src/gtk.rs src/lib.rs src/main.rs src/protocol.rs src/server.rs
diffstat 6 files changed, 97 insertions(+), 110 deletions(-) [+]
line wrap: on
line diff
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,3 +24,15 @@ gtk = { package = "gtk4", version = "0.2
 
 [profile.release]
 lto = true
+
+[[bin]]
+name = "tablet-emu"
+path = "src/bin/standalone.rs"
+
+[[bin]]
+name = "tablet-emud"
+path = "src/bin/server.rs"
+
+[[bin]]
+name = "tablet-emu-remote"
+path = "src/bin/client.rs"
rename from src/client.rs
rename to src/bin/client.rs
--- a/src/client.rs
+++ b/src/bin/client.rs
@@ -14,15 +14,14 @@
 // You should have received a copy of the GNU Affero General Public License
 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
-use crate::{state::FakeState, DEFAULT_HEIGHT, DEFAULT_WIDTH};
 use gio::prelude::*;
 use glib::clone;
 use gtk::prelude::*;
 use input_linux::Key;
+use std::net::UdpSocket;
 use std::sync::{Arc, Mutex};
-use std::net::UdpSocket;
-use crate::Event;
-use crate::server::Buttons;
+use tablet_emu::protocol::{Buttons, Event};
+use tablet_emu::{state::FakeState, DEFAULT_HEIGHT, DEFAULT_WIDTH};
 
 fn build_main_menu(app: &gtk::Application) {
     let quit = gio::SimpleAction::new("quit", None);
@@ -204,7 +203,7 @@ fn build_ui(app: &gtk::Application) {
     window.show();
 }
 
-pub fn main(_args: &[String]) {
+pub fn main() {
     let app = gtk::Application::builder()
         .application_id("fr.linkmauve.TabletEmu")
         .build();
rename from src/server.rs
rename to src/bin/server.rs
--- a/src/server.rs
+++ b/src/bin/server.rs
@@ -14,58 +14,11 @@
 // You should have received a copy of the GNU Affero General Public License
 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
-use crate::state::State;
-use bitflags::bitflags;
 use input_linux::Key;
 use std::io::{self, ErrorKind};
 use std::net::UdpSocket;
-
-bitflags! {
-    /// This is the memory layout of the buttons on the 3DS.
-    #[derive(Default)]
-    pub struct Buttons: u32 {
-        const A = 0x00000001;
-        const B = 0x00000002;
-        const SELECT = 0x00000004;
-        const START = 0x00000008;
-        const RIGHT = 0x00000010;
-        const LEFT = 0x00000020;
-        const UP = 0x00000040;
-        const DOWN = 0x00000080;
-        const R = 0x00000100;
-        const L = 0x00000200;
-        const X = 0x00000400;
-        const Y = 0x00000800;
-        // Nothing
-        // Nothing
-        const ZL = 0x00004000;
-        const ZR = 0x00008000;
-        const RESIZE = 0x00010000; // Not an actual 3DS button!
-        // Nothing
-        // Nothing
-        // Nothing
-        const TOUCH = 0x00100000;
-        // Nothing
-        // Nothing
-        // Nothing
-        const C_RIGHT = 0x01000000;
-        const C_LEFT = 0x02000000;
-        const C_UP = 0x04000000;
-        const C_DOWN = 0x08000000;
-        const CIRCLE_RIGHT = 0x10000000;
-        const CIRCLE_LEFT = 0x20000000;
-        const CIRCLE_UP = 0x40000000;
-        const CIRCLE_DOWN = 0x80000000;
-    }
-}
-
-#[derive(Debug, Default)]
-pub struct Event {
-    pub buttons: Buttons,
-    pad: (i16, i16),
-    c_pad: (i16, i16),
-    pub touch: (u16, u16),
-}
+use tablet_emu::protocol::{Buttons, Event};
+use tablet_emu::state::State;
 
 pub fn run_server(address: &str) -> io::Result<()> {
     let mut state = match State::new() {
@@ -113,7 +66,10 @@ pub fn run_server(address: &str) -> io::
         } else if event.buttons.contains(Buttons::SELECT) {
             state.select_tool(Key::ButtonToolAirbrush);
         } else if event.buttons.contains(Buttons::RESIZE) {
-            println!("set_size({}, {})", event.touch.0 as i32, event.touch.1 as i32);
+            println!(
+                "set_size({}, {})",
+                event.touch.0 as i32, event.touch.1 as i32
+            );
             state.set_size(event.touch.0 as i32, event.touch.1 as i32);
         }
         let (x, y) = event.touch;
@@ -135,7 +91,8 @@ pub fn run_server(address: &str) -> io::
     }
 }
 
-pub fn main(args: &[String]) {
+pub fn main() {
+    let args: Vec<_> = std::env::args().collect();
     let address = if args.len() > 1 {
         args[1].clone()
     } else {
rename from src/gtk.rs
rename to src/bin/standalone.rs
--- a/src/gtk.rs
+++ b/src/bin/standalone.rs
@@ -14,13 +14,13 @@
 // You should have received a copy of the GNU Affero General Public License
 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
-use crate::{state::State, DEFAULT_HEIGHT, DEFAULT_WIDTH};
 use gio::prelude::*;
 use glib::clone;
 use gtk::prelude::*;
 use input_linux::Key;
 use std::io::ErrorKind;
 use std::sync::{Arc, Mutex};
+use tablet_emu::{state::State, DEFAULT_HEIGHT, DEFAULT_WIDTH};
 
 fn build_main_menu(app: &gtk::Application) {
     let quit = gio::SimpleAction::new("quit", None);
@@ -170,7 +170,7 @@ fn build_ui(app: &gtk::Application) {
     window.show();
 }
 
-pub fn main(_args: &[String]) {
+pub fn main() {
     let app = gtk::Application::builder()
         .application_id("fr.linkmauve.TabletEmu")
         .build();
rename from src/main.rs
rename to src/lib.rs
--- a/src/main.rs
+++ b/src/lib.rs
@@ -14,57 +14,12 @@
 // You should have received a copy of the GNU Affero General Public License
 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
-#[cfg(feature = "gui")]
-mod gtk;
-#[cfg(feature = "gui")]
-mod client;
-mod server;
-mod state;
-mod uinput;
-
-pub use server::Event;
-
-use std::env::args;
-
-const MAX_X: i32 = 69920;
-const MAX_Y: i32 = 39980;
-
-const DEFAULT_WIDTH: i32 = 320;
-const DEFAULT_HEIGHT: i32 = 180;
-
-#[derive(Debug)]
-enum Ui {
-    Gtk,
-    Server,
-    Client,
-}
+pub mod protocol;
+pub mod state;
+pub mod uinput;
 
-fn main() {
-    let mut args: Vec<_> = args().collect();
-    let ui = match if args.len() > 1 {
-        args.remove(1)
-    } else {
-        String::from("client")
-    }
-    .as_str()
-    {
-        "gui" => Ui::Gtk,
-        "server" => Ui::Server,
-        "client" => Ui::Client,
-        name => {
-            eprintln!("Wrong UI “{}”, expected gui or server.", name);
-            std::process::exit(2);
-        }
-    };
+pub const MAX_X: i32 = 69920;
+pub const MAX_Y: i32 = 39980;
 
-    match ui {
-        #[cfg(feature = "gui")]
-        Ui::Gtk => gtk::main(&args),
-
-        #[cfg(not(feature = "gui"))]
-        Ui::Gtk => panic!("tablet-emu has been compiled without GUI support."),
-
-        Ui::Server => server::main(&args),
-        Ui::Client => client::main(&args),
-    }
-}
+pub const DEFAULT_WIDTH: i32 = 320;
+pub const DEFAULT_HEIGHT: i32 = 180;
new file mode 100644
--- /dev/null
+++ b/src/protocol.rs
@@ -0,0 +1,64 @@
+// Tablet emulator, for people who don’t own one
+// Copyright © 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+use bitflags::bitflags;
+
+bitflags! {
+    /// This is the memory layout of the buttons on the 3DS.
+    #[derive(Default)]
+    pub struct Buttons: u32 {
+        const A = 0x00000001;
+        const B = 0x00000002;
+        const SELECT = 0x00000004;
+        const START = 0x00000008;
+        const RIGHT = 0x00000010;
+        const LEFT = 0x00000020;
+        const UP = 0x00000040;
+        const DOWN = 0x00000080;
+        const R = 0x00000100;
+        const L = 0x00000200;
+        const X = 0x00000400;
+        const Y = 0x00000800;
+        // Nothing
+        // Nothing
+        const ZL = 0x00004000;
+        const ZR = 0x00008000;
+        const RESIZE = 0x00010000; // Not an actual 3DS button!
+        // Nothing
+        // Nothing
+        // Nothing
+        const TOUCH = 0x00100000;
+        // Nothing
+        // Nothing
+        // Nothing
+        const C_RIGHT = 0x01000000;
+        const C_LEFT = 0x02000000;
+        const C_UP = 0x04000000;
+        const C_DOWN = 0x08000000;
+        const CIRCLE_RIGHT = 0x10000000;
+        const CIRCLE_LEFT = 0x20000000;
+        const CIRCLE_UP = 0x40000000;
+        const CIRCLE_DOWN = 0x80000000;
+    }
+}
+
+#[derive(Debug, Default)]
+pub struct Event {
+    pub buttons: Buttons,
+    pad: (i16, i16),
+    c_pad: (i16, i16),
+    pub touch: (u16, u16),
+}