diff --git a/window/src/os/wayland/connection.rs b/window/src/os/wayland/connection.rs index 322c076fa..b60a7599e 100644 --- a/window/src/os/wayland/connection.rs +++ b/window/src/os/wayland/connection.rs @@ -1,21 +1,34 @@ // TODO: change this #![allow(dead_code, unused)] -use std::{borrow::BorrowMut, cell::RefCell, os::fd::AsRawFd, sync::atomic::AtomicUsize}; +use std::{ + borrow::BorrowMut, cell::RefCell, collections::HashMap, os::fd::AsRawFd, rc::Rc, + sync::atomic::AtomicUsize, +}; -use anyhow::{Context, bail}; +use anyhow::{bail, Context}; use mio::{unix::SourceFd, Events, Interest, Poll, Token}; use smithay_client_toolkit::{ - delegate_registry, + compositor::CompositorHandler, + delegate_compositor, delegate_registry, delegate_xdg_shell, delegate_xdg_window, + output::OutputHandler, registry::{ProvidesRegistryState, RegistryState}, - registry_handlers, delegate_compositor, compositor::CompositorHandler, output::OutputHandler, delegate_xdg_shell, delegate_xdg_window, shell::xdg::window::WindowHandler, + registry_handlers, + shell::xdg::window::WindowHandler, +}; +use wayland_client::{ + backend::{protocol::ProtocolError, WaylandError}, + globals::{registry_queue_init, GlobalList}, + Connection, EventQueue, }; -use wayland_client::{globals::{registry_queue_init, GlobalList}, Connection, EventQueue, backend::{protocol::ProtocolError, WaylandError}}; use crate::{spawn::SPAWN_QUEUE, ConnectionOps}; +use super::WaylandWindowInner; + pub struct WaylandConnection { pub(crate) should_terminate: RefCell, pub(crate) next_window_id: AtomicUsize, + pub(crate) windows: RefCell>>>, pub(crate) event_queue: RefCell>, pub(crate) globals: RefCell, @@ -23,7 +36,7 @@ pub struct WaylandConnection { pub(crate) wayland_state: RefCell, } -pub (crate) struct WaylandState { +pub(crate) struct WaylandState { registry_state: RegistryState, } @@ -39,6 +52,7 @@ impl WaylandConnection { let wayland_connection = WaylandConnection { should_terminate: RefCell::new(false), next_window_id: AtomicUsize::new(1), + windows: RefCell::new(HashMap::default()), event_queue: RefCell::new(event_queue), globals: RefCell::new(globals), @@ -120,7 +134,6 @@ impl WaylandConnection { } } } - } Ok(()) @@ -130,6 +143,10 @@ impl WaylandConnection { self.next_window_id .fetch_add(1, ::std::sync::atomic::Ordering::Relaxed) } + + pub(crate) fn window_by_id(&self, window_id: usize) -> Option>> { + self.windows.borrow().get(&window_id).map(Rc::clone) + } } impl ProvidesRegistryState for WaylandState { @@ -196,7 +213,12 @@ impl OutputHandler for WaylandState { } impl WindowHandler for WaylandState { - fn request_close(&mut self, conn: &Connection, qh: &wayland_client::QueueHandle, window: &smithay_client_toolkit::shell::xdg::window::Window) { + fn request_close( + &mut self, + conn: &Connection, + qh: &wayland_client::QueueHandle, + window: &smithay_client_toolkit::shell::xdg::window::Window, + ) { todo!() } diff --git a/window/src/os/wayland/window.rs b/window/src/os/wayland/window.rs index 3d767a3bd..cafbfe839 100644 --- a/window/src/os/wayland/window.rs +++ b/window/src/os/wayland/window.rs @@ -2,6 +2,7 @@ #![allow(dead_code, unused)] use std::any::Any; +use std::cell::RefCell; use std::rc::Rc; use std::sync::Arc; use std::sync::Mutex; @@ -12,23 +13,34 @@ use config::ConfigHandle; use promise::Future; use raw_window_handle::HasRawDisplayHandle; use raw_window_handle::HasRawWindowHandle; +use raw_window_handle::RawDisplayHandle; +use raw_window_handle::RawWindowHandle; +use raw_window_handle::WaylandWindowHandle; use smithay_client_toolkit::compositor::CompositorState; use smithay_client_toolkit::registry::ProvidesRegistryState; -use smithay_client_toolkit::shell::WaylandSurface; -use smithay_client_toolkit::shell::xdg::XdgShell; use smithay_client_toolkit::shell::xdg::window::DecorationMode; use smithay_client_toolkit::shell::xdg::window::WindowDecorations as Decorations; +use smithay_client_toolkit::shell::xdg::XdgShell; +use smithay_client_toolkit::shell::WaylandSurface; use wayland_client::globals::GlobalList; +use wayland_client::protocol::wl_surface::WlSurface; +use wayland_client::Proxy; +use wayland_egl::WlEglSurface; use wezterm_font::FontConfiguration; +use wezterm_input_types::KeyboardLedStatus; +use wezterm_input_types::Modifiers; +use wezterm_input_types::MouseButtons; use wezterm_input_types::WindowDecorations; use crate::wayland::WaylandConnection; use crate::Clipboard; +use crate::Connection; use crate::ConnectionOps; use crate::MouseCursor; use crate::RequestedWindowGeometry; use crate::Window; use crate::WindowEvent; +use crate::WindowEventSender; use crate::WindowOps; #[derive(Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)] @@ -72,11 +84,7 @@ impl WaylandWindow { let surface = compositor.create_surface(&qh); let xdg_shell = XdgShell::bind(&globals, &qh)?; - let window = xdg_shell.create_window( - surface, - Decorations::RequestServer, - &qh, - ); + let window = xdg_shell.create_window(surface.clone(), Decorations::RequestServer, &qh); window.set_app_id(class_name.to_string()); // TODO: investigate the resizable thing @@ -98,7 +106,7 @@ impl WaylandWindow { window.set_min_size(Some((32, 32))); - // + // // TODO: // let copy_and_paste = CopyAndPaste::create(); // let pending_mouse = PendingMouse::create(window_id, ©_and_paste); @@ -106,12 +114,18 @@ impl WaylandWindow { // conn.pointer.borrow().add_window(&surface, &pending_mouse); // TODO: WindowInner + let inner = Rc::new(RefCell::new(WaylandWindowInner { + events: WindowEventSender::new(event_handler), + surface, + })); let window_handle = Window::Wayland(WaylandWindow(window_id)); // TODO: assign window inner - // - + inner + .borrow_mut() + .events + .assign_window(window_handle.clone()); // window.set_decorate(if decorations == WindowDecorations::NONE { // Decorations::None @@ -204,14 +218,21 @@ impl WindowOps for WaylandWindow { } unsafe impl HasRawDisplayHandle for WaylandWindow { - fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle { + fn raw_display_handle(&self) -> RawDisplayHandle { todo!() } } unsafe impl HasRawWindowHandle for WaylandWindow { - fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle { - todo!() + fn raw_window_handle(&self) -> RawWindowHandle { + let conn = Connection::get().expect("raw_window_handle only callable on the main thread"); + let handle = conn + .wayland() + .window_by_id(self.0) + .expect("window handle invalid!?"); + + let inner = handle.borrow(); + inner.raw_window_handle() } } @@ -223,3 +244,57 @@ struct PendingEvent { dpi: Option, // window_state: Option, } + +pub struct WaylandWindowInner { + // window_id: usize, + pub(crate) events: WindowEventSender, + surface: WlSurface, + // surface_factor: f64, + // copy_and_paste: Arc>, + // window: Option>, + // dimensions: Dimensions, + // resize_increments: Option<(u16, u16)>, + // window_state: WindowState, + // last_mouse_coords: Point, + // mouse_buttons: MouseButtons, + // hscroll_remainder: f64, + // vscroll_remainder: f64, + // modifiers: Modifiers, + // leds: KeyboardLedStatus, + // key_repeat: Option<(u32, Arc>)>, + // pending_event: Arc>, + // pending_mouse: Arc>, + // pending_first_configure: Option>, + // frame_callback: Option>, + // invalidated: bool, + // font_config: Rc, + // text_cursor: Option, + // appearance: Appearance, + // config: ConfigHandle, + // // cache the title for comparison to avoid spamming + // // the compositor with updates that don't actually change it + // title: Option, + // // wegl_surface is listed before gl_state because it + // // must be dropped before gl_state otherwise the underlying + // // libraries will segfault on shutdown + // wegl_surface: Option, + // gl_state: Option>, +} + +unsafe impl HasRawDisplayHandle for WaylandWindowInner { + fn raw_display_handle(&self) -> RawDisplayHandle { + // let mut handle = WaylandDisplayHandle::empty(); + // let conn = WaylandConnection::get().unwrap().wayland(); + // handle.display = conn.display.borrow().c_ptr() as _; + // RawDisplayHandle::Wayland(handle) + todo!() + } +} + +unsafe impl HasRawWindowHandle for WaylandWindowInner { + fn raw_window_handle(&self) -> RawWindowHandle { + let mut handle = WaylandWindowHandle::empty(); + handle.surface = self.surface.id().as_ptr() as *mut _; + RawWindowHandle::Wayland(handle) + } +}