Skip to content

fix(linux): Port to gtk4-rs #1104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
518 changes: 289 additions & 229 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ rwh_05 = { package = "raw-window-handle", version = "0.5", features = [ "std" ],
rwh_06 = { package = "raw-window-handle", version = "0.6", features = [ "std" ], optional = true }
bitflags = "2"
crossbeam-channel = "0.5"
async-channel = "2.3"
url = "2"
dpi = "0.1"

Expand Down Expand Up @@ -146,9 +147,9 @@ dispatch = "0.2"
scopeguard = "1.2"

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
gtk = "0.18"
gdkx11-sys = "0.18"
gdkwayland-sys = "0.18.0"
gtk = { package = "gtk4", version = "0.9", features = ["v4_6"] }
gdk-x11 = { package = "gdk4-x11", version = "0.9" }
gdk-wayland = { package = "gdk4-wayland", version = "0.9", features = ["wayland_crate"]}
x11-dl = "2.21"
parking_lot = "0.12"
dlopen2 = "0.7.0"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ Gtk and its related libraries are used to build the support of Linux. Be sure to
#### Arch Linux / Manjaro:

```bash
sudo pacman -S gtk3
sudo pacman -S gtk4
```

#### Debian / Ubuntu:

```bash
sudo apt install libgtk-3-dev
sudo apt install libgtk-4-dev
```

### Acknowledgement
Expand Down
1 change: 1 addition & 0 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use tao::{

#[allow(clippy::single_match)]
fn main() {
env_logger::init();
let event_loop = EventLoop::new();

let mut window = Some(
Expand Down
20 changes: 11 additions & 9 deletions src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use crate::{
error::{ExternalError, OsError},
event_loop::{EventLoopBuilder, EventLoopWindowTarget},
monitor::MonitorHandle,
platform_impl::{x11::xdisplay::XError, Parent, Window as UnixWindow},
platform_impl::{
gtk_window::ApplicationWindow, x11::xdisplay::XError, Parent, Window as UnixWindow,
},
window::{Window, WindowBuilder},
};

Expand Down Expand Up @@ -67,11 +69,11 @@ pub trait WindowExtUnix {
/// and know what they're doing.
fn new_from_gtk_window<T: 'static>(
event_loop_window_target: &EventLoopWindowTarget<T>,
window: gtk::ApplicationWindow,
window: ApplicationWindow,
) -> Result<Window, OsError>;

/// Returns the `gtk::ApplicatonWindow` from gtk crate that is used by this window.
fn gtk_window(&self) -> &gtk::ApplicationWindow;
fn gtk_window(&self) -> &ApplicationWindow;

/// Returns the vertical `gtk::Box` that is added by default as the sole child of this window.
/// Returns `None` if the default vertical `gtk::Box` creation was disabled by [`WindowBuilderExtUnix::with_default_vbox`].
Expand All @@ -84,7 +86,7 @@ pub trait WindowExtUnix {
}

impl WindowExtUnix for Window {
fn gtk_window(&self) -> &gtk::ApplicationWindow {
fn gtk_window(&self) -> &ApplicationWindow {
&self.window.window
}

Expand All @@ -98,10 +100,10 @@ impl WindowExtUnix for Window {

fn new_from_gtk_window<T: 'static>(
event_loop_window_target: &EventLoopWindowTarget<T>,
window: gtk::ApplicationWindow,
window: ApplicationWindow,
) -> Result<Window, OsError> {
let window = UnixWindow::new_from_gtk_window(&event_loop_window_target.p, window)?;
Ok(Window { window: window })
Ok(Window { window })
}

fn set_badge_count(&self, count: Option<i64>, desktop_filename: Option<String>) {
Expand All @@ -114,7 +116,7 @@ pub trait WindowBuilderExtUnix {
fn with_skip_taskbar(self, skip: bool) -> WindowBuilder;
/// Set this window as a transient dialog for `parent`
/// <https://gtk-rs.org/gtk3-rs/stable/latest/docs/gdk/struct.Window.html#method.set_transient_for>
fn with_transient_for(self, parent: &impl gtk::glib::IsA<gtk::Window>) -> WindowBuilder;
fn with_transient_for(self, parent: &impl gtk::prelude::IsA<gtk::Window>) -> WindowBuilder;

/// Whether to enable or disable the internal draw for transparent window.
///
Expand Down Expand Up @@ -158,8 +160,8 @@ impl WindowBuilderExtUnix for WindowBuilder {
self
}

fn with_transient_for(mut self, parent: &impl gtk::glib::IsA<gtk::Window>) -> WindowBuilder {
use gtk::glib::Cast;
fn with_transient_for(mut self, parent: &impl gtk::prelude::IsA<gtk::Window>) -> WindowBuilder {
use gtk::prelude::Cast;
self.platform_specific.parent = Parent::ChildOf(parent.clone().upcast());
self
}
Expand Down
48 changes: 22 additions & 26 deletions src/platform_impl/linux/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ use std::{
ptr,
};

use gtk::glib;
use x11_dl::{xinput2, xlib};

use crate::event::{DeviceEvent, ElementState, RawKeyEvent};

use super::keycode_from_scancode;

/// Spawn Device event thread. Only works on x11 since wayland doesn't have such global events.
pub fn spawn(device_tx: glib::Sender<DeviceEvent>) {
std::thread::spawn(move || unsafe {
pub fn spawn(device_tx: async_channel::Sender<DeviceEvent>) {
std::thread::spawn(async move || unsafe {
let xlib = xlib::Xlib::open().unwrap();
let xinput2 = xinput2::XInput2::open().unwrap();
let display = (xlib.XOpenDisplay)(ptr::null());
Expand Down Expand Up @@ -45,35 +44,32 @@ pub fn spawn(device_tx: glib::Sender<DeviceEvent>) {
}

let event_type = event.get_type();
match event_type {
xlib::GenericEvent => {
let mut xev = event.generic_event_cookie;
if (xlib.XGetEventData)(display, &mut xev) == xlib::True {
match xev.evtype {
xinput2::XI_RawKeyPress | xinput2::XI_RawKeyRelease => {
let xev: &xinput2::XIRawEvent = &*(xev.data as *const _);
let physical_key = keycode_from_scancode(xev.detail as u32);
let state = match xev.evtype {
xinput2::XI_RawKeyPress => ElementState::Pressed,
xinput2::XI_RawKeyRelease => ElementState::Released,
_ => unreachable!(),
};
if event_type == xlib::GenericEvent {
let mut xev = event.generic_event_cookie;
if (xlib.XGetEventData)(display, &mut xev) == xlib::True {
match xev.evtype {
xinput2::XI_RawKeyPress | xinput2::XI_RawKeyRelease => {
let xev: &xinput2::XIRawEvent = &*(xev.data as *const _);
let physical_key = keycode_from_scancode(xev.detail as u32);
let state = match xev.evtype {
xinput2::XI_RawKeyPress => ElementState::Pressed,
xinput2::XI_RawKeyRelease => ElementState::Released,
_ => unreachable!(),
};

let event = RawKeyEvent {
physical_key,
state,
};
let event = RawKeyEvent {
physical_key,
state,
};

if let Err(e) = device_tx.send(DeviceEvent::Key(event)) {
log::info!("Failed to send device event {} since receiver is closed. Closing x11 thread along with it", e);
break;
}
if let Err(e) = device_tx.send_blocking(DeviceEvent::Key(event)) {
log::info!("Failed to send device event {} since receiver is closed. Closing x11 thread along with it", e);
break;
}
_ => {}
}
_ => {}
}
}
_ => {}
}
}
});
Expand Down
Loading