view src/main.rs @ 3:72e63d6a3f8a

Simplify state handling into a single struct.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 09 Oct 2020 15:03:55 +0200
parents 39f87ef69b2a
children 2e074d185151
line wrap: on
line source

use gio::prelude::*;
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,
}

fn build_ui(application: &gtk::Application) {
    let dev = match create_uinput_device() {
        Ok(dev) => dev,
        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),
            }
            return;
        }
    };
    println!(
        "New device at {:?} ({:?})",
        dev.evdev_path().unwrap(),
        dev.sys_path().unwrap()
    );

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

    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);

    let tool1 = gtk::Button::with_label("Pen");
    let tool2 = gtk::Button::with_label("Rubber");
    let tool3 = gtk::Button::with_label("Brush");
    let tool4 = gtk::Button::with_label("Pencil");
    let tool5 = gtk::Button::with_label("Airbrush");

    macro_rules! impl_tool_signal {
        ($tool:ident) => {
            let state_weak = Arc::downgrade(&state);
            $tool.connect_clicked(move |b| {
                let state = state_weak.upgrade().unwrap();
                let mut state = state.lock().unwrap();
                state.selected_tool = match b.get_label().unwrap().as_str() {
                    "Pen" => Key::ButtonToolPen,
                    "Rubber" => Key::ButtonToolRubber,
                    "Brush" => Key::ButtonToolBrush,
                    "Pencil" => Key::ButtonToolPencil,
                    "Airbrush" => Key::ButtonToolAirbrush,
                    _ => unreachable!(),
                };
            });
        };
    };
    impl_tool_signal!(tool1);
    impl_tool_signal!(tool2);
    impl_tool_signal!(tool3);
    impl_tool_signal!(tool4);
    impl_tool_signal!(tool5);

    tools_box.add(&tool1);
    tools_box.add(&tool2);
    tools_box.add(&tool3);
    tools_box.add(&tool4);
    tools_box.add(&tool5);

    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();
        state.pressed = true;
        let (x, y) = event.get_position();
        //println!("press tool {} at {}, {}", current_tool.lock().unwrap(), x, y);
        state.dev.write(&[
            input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / state.width) as i32),
            input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / state.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(state.selected_tool, 1),
            input_synchronize_new(SynchronizeKind::Report, 0),
        ])
        .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.pressed = false;
        //println!("release {}, {}", x, y);
        state.dev.write(&[
            input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / state.width) as i32),
            input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / state.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(state.selected_tool, 1),
            input_synchronize_new(SynchronizeKind::Report, 0),
        ])
        .unwrap();
        Inhibit(false)
    });
    drawing_area.connect_motion_notify_event(move |_, event| {
        let state = state.lock().unwrap();
        let (x, y) = event.get_position();
        //println!("motion {}, {}", x, y);
        state.dev.write(&[
            input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / state.width) as i32),
            input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / state.height) as i32),
            input_axis_new(AbsoluteAxis::Z, 0),
            input_axis_new(AbsoluteAxis::Wheel, 0),
            input_axis_new(AbsoluteAxis::Pressure, if state.pressed { 2048 } else { 0 }),
            input_axis_new(AbsoluteAxis::Distance, if state.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(state.selected_tool, 1),
            input_synchronize_new(SynchronizeKind::Report, 0),
        ])
        .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<_>>());
}