view src/main.rs @ 10:06d77bb94a50

Move more state handling inside of State.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 01 Nov 2020 15:50:38 +0100
parents d1972fc49a5b
children 0193041f01d4
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 gio::prelude::*;
use glib::clone;
use gtk::prelude::*;

use std::env::args;
use std::fs::{File, OpenOptions};
use std::io::ErrorKind;
use std::sync::{Arc, Mutex};

use input_linux::{
    sys::input_event, sys::timeval, AbsoluteAxis, AbsoluteInfo, AbsoluteInfoSetup, EventKind,
    InputId, InputProperty, Key, MiscKind, SynchronizeKind, UInputHandle,
};

const WIDTH: i32 = 320;
const HEIGHT: i32 = 180;

const MAX_X: i32 = 69920;
const MAX_Y: i32 = 39980;

fn create_uinput_device() -> std::io::Result<UInputHandle<File>> {
    let file = OpenOptions::new().write(true).open("/dev/uinput")?;
    let dev = UInputHandle::new(file);

    dev.set_evbit(EventKind::Synchronize)?;
    dev.set_evbit(EventKind::Key)?;
    dev.set_evbit(EventKind::Absolute)?;
    dev.set_evbit(EventKind::Misc)?;
    dev.set_keybit(Key::ButtonToolPen)?;
    dev.set_keybit(Key::ButtonToolRubber)?;
    dev.set_keybit(Key::ButtonToolBrush)?;
    dev.set_keybit(Key::ButtonToolPencil)?;
    dev.set_keybit(Key::ButtonToolAirbrush)?;
    dev.set_keybit(Key::ButtonTouch)?;
    dev.set_keybit(Key::ButtonStylus)?;
    dev.set_keybit(Key::ButtonStylus2)?;
    dev.set_keybit(Key::ButtonStylus3)?;
    dev.set_mscbit(MiscKind::Serial)?;
    dev.set_propbit(InputProperty::Direct)?;

    dev.set_absbit(AbsoluteAxis::X)?;
    dev.set_absbit(AbsoluteAxis::Y)?;
    dev.set_absbit(AbsoluteAxis::Z)?;
    dev.set_absbit(AbsoluteAxis::Wheel)?;
    dev.set_absbit(AbsoluteAxis::Pressure)?;
    dev.set_absbit(AbsoluteAxis::Distance)?;
    dev.set_absbit(AbsoluteAxis::TiltX)?;
    dev.set_absbit(AbsoluteAxis::TiltY)?;
    dev.set_absbit(AbsoluteAxis::Misc)?;

    let id = InputId {
        bustype: 3,
        vendor: 0x56a,
        product: 0x350,
        version: 0xb,
    };

    let x = AbsoluteInfoSetup {
        axis: AbsoluteAxis::X,
        info: AbsoluteInfo {
            value: 0,
            minimum: 0,
            maximum: MAX_X,
            fuzz: 0,
            flat: 0,
            resolution: 200,
        },
    };
    let y = AbsoluteInfoSetup {
        axis: AbsoluteAxis::Y,
        info: AbsoluteInfo {
            value: 0,
            minimum: 0,
            maximum: MAX_Y,
            fuzz: 0,
            flat: 0,
            resolution: 200,
        },
    };
    let z = AbsoluteInfoSetup {
        axis: AbsoluteAxis::Z,
        info: AbsoluteInfo {
            value: 0,
            minimum: -900,
            maximum: 899,
            fuzz: 0,
            flat: 0,
            resolution: 287,
        },
    };
    let wheel = AbsoluteInfoSetup {
        axis: AbsoluteAxis::Wheel,
        info: AbsoluteInfo {
            value: 0,
            minimum: 0,
            maximum: 2047,
            fuzz: 0,
            flat: 0,
            resolution: 0,
        },
    };
    let pressure = AbsoluteInfoSetup {
        axis: AbsoluteAxis::Pressure,
        info: AbsoluteInfo {
            value: 0,
            minimum: 0,
            maximum: 8196,
            fuzz: 0,
            flat: 0,
            resolution: 0,
        },
    };
    let distance = AbsoluteInfoSetup {
        axis: AbsoluteAxis::Distance,
        info: AbsoluteInfo {
            value: 0,
            minimum: 0,
            maximum: 63,
            fuzz: 0,
            flat: 0,
            resolution: 0,
        },
    };
    let tilt_x = AbsoluteInfoSetup {
        axis: AbsoluteAxis::TiltX,
        info: AbsoluteInfo {
            value: 0,
            minimum: -64,
            maximum: 63,
            fuzz: 0,
            flat: 0,
            resolution: 57,
        },
    };
    let tilt_y = AbsoluteInfoSetup {
        axis: AbsoluteAxis::TiltY,
        info: AbsoluteInfo {
            value: 0,
            minimum: -64,
            maximum: 63,
            fuzz: 0,
            flat: 0,
            resolution: 57,
        },
    };
    let misc = AbsoluteInfoSetup {
        axis: AbsoluteAxis::Misc,
        info: AbsoluteInfo {
            value: 0,
            minimum: 0,
            maximum: 0,
            fuzz: 0,
            flat: 0,
            resolution: 0,
        },
    };

    dev.create(
        &id,
        b"TabletEmu",
        0,
        &[x, y, z, wheel, pressure, distance, tilt_x, tilt_y, misc],
    )?;
    Ok(dev)
}

fn input_event_new(type_: EventKind, code: u16, value: i32) -> input_event {
    input_event {
        time: timeval {
            tv_sec: 0,
            tv_usec: 0,
        },
        type_: type_ as u16,
        code,
        value,
    }
}

fn input_axis_new(code: AbsoluteAxis, value: i32) -> input_event {
    input_event_new(EventKind::Absolute, code as u16, value)
}

fn input_key_new(code: Key, value: i32) -> input_event {
    input_event_new(EventKind::Key, code as u16, value)
}

fn input_misc_new(code: MiscKind, value: i32) -> input_event {
    input_event_new(EventKind::Misc, code as u16, value)
}

fn input_synchronize_new(code: SynchronizeKind, value: i32) -> input_event {
    input_event_new(EventKind::Synchronize, code as u16, value)
}

struct State {
    dev: UInputHandle<File>,
    width: f64,
    height: f64,
    selected_tool: Key,
    pressed: bool,
}

impl State {
    fn new() -> std::io::Result<Arc<Mutex<State>>> {
        let dev = create_uinput_device()?;
        println!(
            "New device at {:?} ({:?})",
            dev.evdev_path()?,
            dev.sys_path()?
        );

        Ok(Arc::new(Mutex::new(State {
            dev,
            width: WIDTH as f64,
            height: HEIGHT as f64,
            selected_tool: Key::ButtonToolPen,
            pressed: false,
        })))
    }

    fn select_tool(&mut self, tool: Key) {
        self.selected_tool = tool;
    }

    fn press(&mut self, x: f64, y: f64) -> std::io::Result<()> {
        self.pressed = true;
        self.dev.write(&[
            input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
            input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
            input_axis_new(AbsoluteAxis::Z, 0),
            input_axis_new(AbsoluteAxis::Wheel, 0),
            input_axis_new(AbsoluteAxis::Pressure, 1024),
            input_axis_new(AbsoluteAxis::Distance, 0),
            input_axis_new(AbsoluteAxis::TiltX, 16),
            input_axis_new(AbsoluteAxis::TiltY, 0),
            input_misc_new(MiscKind::Serial, 0),
            input_key_new(self.selected_tool, 1),
            input_synchronize_new(SynchronizeKind::Report, 0),
        ])?;
        Ok(())
    }

    fn release(&mut self, x: f64, y: f64) -> std::io::Result<()> {
        self.pressed = false;
        self.dev.write(&[
            input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
            input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
            input_axis_new(AbsoluteAxis::Z, 0),
            input_axis_new(AbsoluteAxis::Wheel, 0),
            input_axis_new(AbsoluteAxis::Pressure, 0),
            input_axis_new(AbsoluteAxis::Distance, 16),
            input_axis_new(AbsoluteAxis::TiltX, 16),
            input_axis_new(AbsoluteAxis::TiltY, 0),
            input_misc_new(MiscKind::Serial, 0),
            input_key_new(self.selected_tool, 1),
            input_synchronize_new(SynchronizeKind::Report, 0),
        ])?;
        Ok(())
    }

    fn motion(&mut self, x: f64, y: f64) -> std::io::Result<()> {
        self.dev.write(&[
            input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
            input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
            input_axis_new(AbsoluteAxis::Z, 0),
            input_axis_new(AbsoluteAxis::Wheel, 0),
            input_axis_new(AbsoluteAxis::Pressure, if self.pressed { 2048 } else { 0 }),
            input_axis_new(AbsoluteAxis::Distance, if self.pressed { 0 } else { 32 }),
            input_axis_new(AbsoluteAxis::TiltX, 16),
            input_axis_new(AbsoluteAxis::TiltY, 0),
            input_misc_new(MiscKind::Serial, 0),
            input_key_new(self.selected_tool, 1),
            input_synchronize_new(SynchronizeKind::Report, 0),
        ])?;
        Ok(())
    }
}

fn build_main_menu(application: &gtk::Application) {
    let quit = gio::SimpleAction::new("quit", None);
    application.set_accels_for_action("app.quit", &["<Control>q"]);
    application.add_action(&quit);
    quit.connect_activate(clone!(@weak application => move |_, _| application.quit()));

    let about = gio::SimpleAction::new("about", None);
    application.add_action(&about);
    about.connect_activate(|_, _| {
        let about = gtk::AboutDialog::new();
        about.set_program_name("TabletEmu");
        about.set_logo_icon_name(Some("input-tablet"));
        about.set_website(Some("https://hg.linkmauve.fr/tablet-emu"));
        about.set_version(Some("0.1"));
        about.set_license_type(gtk::License::Agpl30);
        about.set_copyright(Some("© 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>"));
        about.run();
        unsafe {
            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);
    }
    application.set_menubar(Some(&menu));
}

fn build_ui(application: &gtk::Application) {
    build_main_menu(application);

    let state = match State::new() {
        Ok(state) => state,
        Err(err) => {
            match err.kind() {
                ErrorKind::NotFound => {
                    eprintln!("Couldn’t find /dev/uinput: {}", err);
                    eprintln!("Maybe you forgot to `modprobe uinput`?");
                }
                ErrorKind::PermissionDenied => {
                    eprintln!("Couldn’t open /dev/uinput for writing: {}", err);
                    eprintln!("Maybe you aren’t allowed to create input devices?");
                }
                _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err),
            }
            std::process::exit(1);
        }
    };

    let window = gtk::ApplicationWindow::new(application);
    window.set_title("tablet-emu");
    window.set_position(gtk::WindowPosition::Center);

    let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0);
    let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0);

    macro_rules! impl_tool {
        ($tool:tt) => {
            let tool = gtk::Button::with_mnemonic($tool);
            let state_weak = Arc::downgrade(&state);
            tool.connect_clicked(move |b| {
                let state = state_weak.upgrade().unwrap();
                let mut state = state.lock().unwrap();
                let tool = match b.get_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);
            });
            tools_box.add(&tool);
        };
    };
    impl_tool!("_Pen");
    impl_tool!("_Rubber");
    impl_tool!("_Brush");
    impl_tool!("P_encil");
    impl_tool!("_Airbrush");

    let drawing_area = gtk::DrawingArea::new();
    drawing_area.set_size_request(WIDTH, HEIGHT);
    drawing_area.set_hexpand(true);
    drawing_area.set_events(
        gdk::EventMask::BUTTON_PRESS_MASK
            | gdk::EventMask::BUTTON_RELEASE_MASK
            | gdk::EventMask::POINTER_MOTION_MASK,
    );
    let state_weak = Arc::downgrade(&state);
    drawing_area.connect_configure_event(move |_, event| {
        let state = state_weak.upgrade().unwrap();
        let mut state = state.lock().unwrap();
        match event.get_size() {
            (width, height) => {
                state.width = width as f64;
                state.height = height as f64;
            }
        }
        true
    });
    let state_weak = Arc::downgrade(&state);
    drawing_area.connect_button_press_event(move |_, event| {
        if event.get_button() != 1 {
            return Inhibit(false);
        }

        let state = state_weak.upgrade().unwrap();
        let mut state = state.lock().unwrap();
        let (x, y) = event.get_position();
        state.press(x, y).unwrap();
        Inhibit(false)
    });
    let state_weak = Arc::downgrade(&state);
    drawing_area.connect_button_release_event(move |_, event| {
        if event.get_button() != 1 {
            return Inhibit(false);
        }

        let state = state_weak.upgrade().unwrap();
        let mut state = state.lock().unwrap();
        let (x, y) = event.get_position();
        state.release(x, y).unwrap();
        Inhibit(false)
    });
    drawing_area.connect_motion_notify_event(move |_, event| {
        let mut state = state.lock().unwrap();
        let (x, y) = event.get_position();
        state.motion(x, y).unwrap();
        Inhibit(false)
    });
    drawing_area.connect_draw(move |_, ctx| {
        //println!("drawing {}", drawing_area);
        ctx.set_source_rgb(1., 0., 0.);
        ctx.set_operator(cairo::Operator::Screen);
        ctx.paint();
        Inhibit(false)
    });

    hbox.add(&tools_box);
    hbox.add(&drawing_area);

    window.add(&hbox);

    window.show_all();
}

fn main() {
    let application = gtk::Application::new(
        Some("fr.linkmauve.TabletEmu"),
        gio::ApplicationFlags::empty(),
    )
    .expect("Initialisation failed…");
    application.connect_activate(build_ui);
    application.run(&args().collect::<Vec<_>>());
}