view src/bin/standalone.rs @ 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 src/gtk.rs@478cf2a7d577
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 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);
    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 state = match State::new() {
        Ok(state) => Arc::new(Mutex::new(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 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);
            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);
            });
            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 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 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();
    });
    event_controller.connect_motion(move |_, x, y| {
        let mut state = state.lock().unwrap();
        state.motion(x, y).unwrap();
    });
    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() {
    let app = gtk::Application::builder()
        .application_id("fr.linkmauve.TabletEmu")
        .build();
    app.connect_activate(build_ui);
    app.run();
}