view src/client.rs @ 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
children
line wrap: on
line source

// 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: &gtk::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: &gtk::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();
}