summaryrefslogtreecommitdiffstats
path: root/lan-mouse-gtk/src/lib.rs
blob: c2ec58111e7c9934a0cd1ba06ddbe9bc0673dc1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
mod client_object;
mod client_row;
mod window;

use std::{env, process, str};

use window::Window;

use lan_mouse_ipc::{FrontendEvent, FrontendRequest};

use adw::Application;
use gtk::{
    gdk::Display, glib::clone, prelude::*, subclass::prelude::ObjectSubclassIsExt, IconTheme,
};
use gtk::{gio, glib, prelude::ApplicationExt};

use self::client_object::ClientObject;

pub fn run() -> glib::ExitCode {
    log::debug!("running gtk frontend");
    #[cfg(windows)]
    let ret = std::thread::Builder::new()
        .stack_size(8 * 1024 * 1024) // https://gitlab.gnome.org/GNOME/gtk/-/commit/52dbb3f372b2c3ea339e879689c1de535ba2c2c3 -> caused crash on windows
        .name("gtk".into())
        .spawn(gtk_main)
        .unwrap()
        .join()
        .unwrap();
    #[cfg(not(windows))]
    let ret = gtk_main();

    if ret == glib::ExitCode::FAILURE {
        log::error!("frontend exited with failure");
    } else {
        log::info!("frontend exited successfully");
    }

    ret
}

fn gtk_main() -> glib::ExitCode {
    gio::resources_register_include!("lan-mouse.gresource").expect("Failed to register resources.");

    let app = Application::builder()
        .application_id("de.feschber.LanMouse")
        .build();

    app.connect_startup(|_| load_icons());
    app.connect_activate(build_ui);

    let args: Vec<&'static str> = vec![];
    app.run_with_args(&args)
}

fn load_icons() {
    let display = &Display::default().expect("Could not connect to a display.");
    let icon_theme = IconTheme::for_display(display);
    icon_theme.add_resource_path("/de/feschber/LanMouse/icons");
}

fn build_ui(app: &Application) {
    log::debug!("connecting to lan-mouse-socket");
    let (mut frontend_rx, frontend_tx) = match lan_mouse_ipc::connect() {
        Ok(conn) => conn,
        Err(e) => {
            log::error!("{e}");
            process::exit(1);
        }
    };
    log::debug!("connected to lan-mouse-socket");

    let (sender, receiver) = async_channel::bounded(10);

    gio::spawn_blocking(move || {
        while let Some(e) = frontend_rx.next_event() {
            match e {
                Ok(e) => sender.send_blocking(e).unwrap(),
                Err(e) => {
                    log::error!("{e}");
                    break;
                }
            }
        }
    });

    let window = Window::new(app, frontend_tx);

    glib::spawn_future_local(clone!(
        #[weak]
        window,
        async move {
            loop {
                let notify = receiver.recv().await.unwrap_or_else(|_| process::exit(1));
                match notify {
                    FrontendEvent::Changed(handle) => {
                        window.request(FrontendRequest::GetState(handle));
                    }
                    FrontendEvent::Created(handle, client, state) => {
                        window.new_client(handle, client, state);
                    }
                    FrontendEvent::Deleted(client) => {
                        window.delete_client(client);
                    }
                    FrontendEvent::State(handle, config, state) => {
                        window.update_client_config(handle, config);
                        window.update_client_state(handle, state);
                    }
                    FrontendEvent::NoSuchClient(_) => {}
                    FrontendEvent::Error(e) => {
                        window.show_toast(e.as_str());
                    }
                    FrontendEvent::Enumerate(clients) => {
                        for (handle, client, state) in clients {
                            if window.client_idx(handle).is_some() {
                                window.update_client_config(handle, client);
                                window.update_client_state(handle, state);
                            } else {
                                window.new_client(handle, client, state);
                            }
                        }
                    }
                    FrontendEvent::PortChanged(port, msg) => {
                        match msg {
                            None => window.show_toast(format!("port changed: {port}").as_str()),
                            Some(msg) => window.show_toast(msg.as_str()),
                        }
                        window.imp().set_port(port);
                    }
                    FrontendEvent::CaptureStatus(s) => {
                        window.set_capture(s.into());
                    }
                    FrontendEvent::EmulationStatus(s) => {
                        window.set_emulation(s.into());
                    }
                }
            }
        }
    ));

    window.present();
}