Mercurial > tablet-emu
changeset 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 | 478cf2a7d577 |
children | 3f7b7a3ad8fe |
files | src/client.rs src/main.rs src/server.rs src/state.rs |
diffstat | 4 files changed, 274 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/src/client.rs @@ -0,0 +1,213 @@ +// 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 crate::{state::FakeState, DEFAULT_HEIGHT, DEFAULT_WIDTH}; +use gio::prelude::*; +use glib::clone; +use gtk::prelude::*; +use input_linux::Key; +use std::sync::{Arc, Mutex}; +use std::net::UdpSocket; +use crate::Event; +use crate::server::Buttons; + +fn build_main_menu(app: >k::Application) { + let quit = gio::SimpleAction::new("quit", None); + app.set_accels_for_action("app.quit", &["<Control>q"]); + app.add_action(&quit); + quit.connect_activate(clone!(@weak app => move |_, _| app.quit())); + + let about = gio::SimpleAction::new("about", None); + app.add_action(&about); + about.connect_activate(|_, _| { + let about = gtk::AboutDialog::builder() + .program_name("TabletEmu") + .logo_icon_name("input-tablet") + .website("https://hg.linkmauve.fr/tablet-emu") + .version("0.1") + .license_type(gtk::License::Agpl30) + .copyright("© 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>") + .build(); + //about.run(); + about.destroy(); + }); + + let menu = gio::Menu::new(); + { + let file = gio::Menu::new(); + file.append(Some("_Quit"), Some("app.quit")); + menu.append_submenu(Some("_File"), &file); + } + { + let help = gio::Menu::new(); + help.append(Some("_About"), Some("app.about")); + menu.append_submenu(Some("_Help"), &help); + } + app.set_menubar(Some(&menu)); +} + +fn build_ui(app: >k::Application) { + build_main_menu(app); + + let address = "127.0.0.1:16150"; + let socket = Arc::new(Mutex::new(UdpSocket::bind("127.0.0.1:4321").unwrap())); + println!("opened {:?}", socket); + + let send = move |event: &Event| { + // TODO: Yolo-alignment. + let buf: &[u8; std::mem::size_of::<Event>()] = unsafe { std::mem::transmute(event) }; + let socket = socket.lock().unwrap(); + let amount = socket.send_to(buf, address).unwrap(); + assert_eq!(amount, std::mem::size_of::<Event>()); + }; + let send2 = send.clone(); + let send3 = send.clone(); + let send4 = send.clone(); + + let state = Arc::new(Mutex::new(FakeState::new())); + + let hbox = gtk::Box::builder() + .orientation(gtk::Orientation::Horizontal) + .build(); + let tools_box = gtk::Box::builder() + .orientation(gtk::Orientation::Vertical) + .build(); + + macro_rules! impl_tool { + ($tool:tt) => { + let tool = gtk::Button::with_mnemonic($tool); + let state_weak = Arc::downgrade(&state); + let send = send.clone(); + tool.connect_clicked(move |b| { + let state = state_weak.upgrade().unwrap(); + let mut state = state.lock().unwrap(); + let tool = match b.label().unwrap().as_str() { + "_Pen" => Key::ButtonToolPen, + "_Rubber" => Key::ButtonToolRubber, + "_Brush" => Key::ButtonToolBrush, + "P_encil" => Key::ButtonToolPencil, + "_Airbrush" => Key::ButtonToolAirbrush, + _ => unreachable!(), + }; + state.select_tool(tool); + + let mut event: Event = Default::default(); + event.buttons = match tool { + Key::ButtonToolPen => Buttons::A, + Key::ButtonToolRubber => Buttons::B, + Key::ButtonToolBrush => Buttons::X, + Key::ButtonToolPencil => Buttons::Y, + Key::ButtonToolAirbrush => Buttons::SELECT, + _ => unreachable!(), + }; + send(&event); + }); + tools_box.append(&tool); + }; + } + impl_tool!("_Pen"); + impl_tool!("_Rubber"); + impl_tool!("_Brush"); + impl_tool!("P_encil"); + impl_tool!("_Airbrush"); + + let drawing_area = gtk::DrawingArea::builder() + .content_width(DEFAULT_WIDTH) + .content_height(DEFAULT_HEIGHT) + .hexpand(true) + .build(); + let gesture_click = gtk::GestureClick::new(); + let event_controller = gtk::EventControllerMotion::new(); + let state_weak = Arc::downgrade(&state); + drawing_area.connect_resize(move |_, width, height| { + let state = state_weak.upgrade().unwrap(); + let mut state = state.lock().unwrap(); + state.set_size(width, height); + + let mut event: Event = Default::default(); + event.buttons = Buttons::RESIZE; + event.touch = (width as u16, height as u16); + send(&event); + }); + let state_weak = Arc::downgrade(&state); + gesture_click.connect_pressed(move |_, n_press, x, y| { + if n_press != 1 { + return; + } + + let state = state_weak.upgrade().unwrap(); + let mut state = state.lock().unwrap(); + state.press(x, y).unwrap(); + + let mut event: Event = Default::default(); + event.buttons = Buttons::TOUCH; + event.touch = (x as u16, y as u16); + send2(&event); + }); + let state_weak = Arc::downgrade(&state); + gesture_click.connect_released(move |_, n_press, x, y| { + if n_press != 1 { + return; + } + + let state = state_weak.upgrade().unwrap(); + let mut state = state.lock().unwrap(); + state.release(x, y).unwrap(); + + let mut event: Event = Default::default(); + event.touch = (x as u16, y as u16); + send3(&event); + }); + event_controller.connect_motion(move |_, x, y| { + let mut state = state.lock().unwrap(); + state.motion(x, y).unwrap(); + + if state.is_pressed() { + let mut event: Event = Default::default(); + event.buttons = Buttons::TOUCH; + event.touch = (x as u16, y as u16); + send4(&event); + } + }); + drawing_area.add_controller(&gesture_click); + drawing_area.add_controller(&event_controller); + drawing_area.set_draw_func(move |_, ctx, _, _| { + ctx.set_source_rgb(1., 0., 0.); + ctx.set_operator(cairo::Operator::Screen); + ctx.paint().unwrap(); + }); + + hbox.append(&tools_box); + hbox.append(&drawing_area); + + let window = gtk::ApplicationWindow::builder() + .application(app) + .title("tablet-emu") + .default_width(800) + .default_height(480) + .child(&hbox) + .build(); + + window.show(); +} + +pub fn main(_args: &[String]) { + let app = gtk::Application::builder() + .application_id("fr.linkmauve.TabletEmu") + .build(); + app.connect_activate(build_ui); + app.run(); +}
--- a/src/main.rs +++ b/src/main.rs @@ -16,10 +16,14 @@ #[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; @@ -32,6 +36,7 @@ const DEFAULT_HEIGHT: i32 = 180; enum Ui { Gtk, Server, + Client, } fn main() { @@ -39,12 +44,13 @@ fn main() { let ui = match if args.len() > 1 { args.remove(1) } else { - String::from("gui") + 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); @@ -59,5 +65,6 @@ fn main() { Ui::Gtk => panic!("tablet-emu has been compiled without GUI support."), Ui::Server => server::main(&args), + Ui::Client => client::main(&args), } }
--- a/src/server.rs +++ b/src/server.rs @@ -23,7 +23,7 @@ use std::net::UdpSocket; bitflags! { /// This is the memory layout of the buttons on the 3DS. #[derive(Default)] - struct Buttons: u32 { + pub struct Buttons: u32 { const A = 0x00000001; const B = 0x00000002; const SELECT = 0x00000004; @@ -40,7 +40,7 @@ bitflags! { // Nothing const ZL = 0x00004000; const ZR = 0x00008000; - // Nothing + const RESIZE = 0x00010000; // Not an actual 3DS button! // Nothing // Nothing // Nothing @@ -60,11 +60,11 @@ bitflags! { } #[derive(Debug, Default)] -struct Event { - buttons: Buttons, +pub struct Event { + pub buttons: Buttons, pad: (i16, i16), c_pad: (i16, i16), - touch: (u16, u16), + pub touch: (u16, u16), } pub fn run_server(address: &str) -> io::Result<()> { @@ -112,6 +112,9 @@ pub fn run_server(address: &str) -> io:: state.select_tool(Key::ButtonToolPencil); } 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); + state.set_size(event.touch.0 as i32, event.touch.1 as i32); } let (x, y) = event.touch; if event.buttons.contains(Buttons::TOUCH) {
--- a/src/state.rs +++ b/src/state.rs @@ -109,3 +109,48 @@ impl State { Ok(()) } } + +pub struct FakeState { + width: f64, + height: f64, + selected_tool: Key, + pressed: bool, +} + +impl FakeState { + pub fn new() -> FakeState { + FakeState { + width: DEFAULT_WIDTH as f64, + height: DEFAULT_HEIGHT as f64, + selected_tool: Key::ButtonToolPen, + pressed: false, + } + } + + pub fn is_pressed(&self) -> bool { + self.pressed + } + + pub fn set_size(&mut self, width: i32, height: i32) { + self.width = width as f64; + self.height = height as f64; + } + + pub fn select_tool(&mut self, tool: Key) { + self.selected_tool = tool; + } + + pub fn press(&mut self, _x: f64, _y: f64) -> std::io::Result<()> { + self.pressed = true; + Ok(()) + } + + pub fn release(&mut self, _x: f64, _y: f64) -> std::io::Result<()> { + self.pressed = false; + Ok(()) + } + + pub fn motion(&mut self, _x: f64, _y: f64) -> std::io::Result<()> { + Ok(()) + } +}