use std::{ cell::{RefCell, RefMut}, hash::Hash, os::fd::{AsRawFd, BorrowedFd}, path::PathBuf, rc::{Rc, Weak}, time::{Duration, Instant},}; use ashpd::WindowIdentifier;use calloop::{ EventLoop, LoopHandle, timer::{TimeoutAction, Timer},};use calloop_wayland_source::WaylandSource;use collections::HashMap;use filedescriptor::Pipe;use http_client::Url;use smallvec::SmallVec;use util::ResultExt as _;use wayland_backend::client::ObjectId;use wayland_backend::protocol::WEnum;use wayland_client::event_created_child;use wayland_client::globals::{GlobalList, GlobalListContents, registry_queue_init};use wayland_client::protocol::wl_callback::{self, WlCallback};use wayland_client::protocol::wl_data_device_manager::DndAction;use wayland_client::protocol::wl_data_offer::WlDataOffer;use wayland_client::protocol::wl_pointer::AxisSource;use wayland_client::protocol::{ wl_data_device, wl_data_device_manager, wl_data_offer, wl_data_source, wl_output, wl_region,};use wayland_client::{ Connection, Dispatch, Proxy, QueueHandle, delegate_noop, protocol::{ wl_buffer, wl_compositor, wl_keyboard, wl_pointer, wl_registry, wl_seat, wl_shm, wl_shm_pool, wl_surface, },};use wayland_protocols::wp::pointer_gestures::zv1::client::{ zwp_pointer_gesture_pinch_v1, zwp_pointer_gestures_v1,};use wayland_protocols::wp::primary_selection::zv1::client::zwp_primary_selection_offer_v1::{ self, ZwpPrimarySelectionOfferV1,};use wayland_protocols::wp::primary_selection::zv1::client::{ zwp_primary_selection_device_manager_v1, zwp_primary_selection_device_v1, zwp_primary_selection_source_v1,};use wayland_protocols::wp::text_input::zv3::client::zwp_text_input_v3::{ ContentHint, ContentPurpose,};use wayland_protocols::wp::text_input::zv3::client::{ zwp_text_input_manager_v3, zwp_text_input_v3,};use wayland_protocols::wp::viewporter::client::{wp_viewport, wp_viewporter};use wayland_protocols::xdg::activation::v1::client::{xdg_activation_token_v1, xdg_activation_v1};use wayland_protocols::xdg::decoration::zv1::client::{ zxdg_decoration_manager_v1, zxdg_toplevel_decoration_v1,};use wayland_protocols::xdg::shell::client::{xdg_surface, xdg_toplevel, xdg_wm_base};use wayland_protocols::{ wp::cursor_shape::v1::client::{wp_cursor_shape_device_v1, wp_cursor_shape_manager_v1}, xdg::dialog::v1::client::xdg_wm_dialog_v1::{self, XdgWmDialogV1},};use wayland_protocols::{ wp::fractional_scale::v1::client::{wp_fractional_scale_manager_v1, wp_fractional_scale_v1}, xdg::dialog::v1::client::xdg_dialog_v1::XdgDialogV1,};use wayland_protocols_plasma::blur::client::{org_kde_kwin_blur, org_kde_kwin_blur_manager};use wayland_protocols_wlr::layer_shell::v1::client::{zwlr_layer_shell_v1, zwlr_layer_surface_v1};use xkbcommon::xkb::ffi::XKB_KEYMAP_FORMAT_TEXT_V1;use xkbcommon::xkb::{self, KEYMAP_COMPILE_NO_FLAGS, Keycode}; use super::{ display::WaylandDisplay, window::{ImeInput, WaylandWindowStatePtr},}; use crate::linux::{ DOUBLE_CLICK_INTERVAL, LinuxClient, LinuxCommon, LinuxKeyboardLayout, SCROLL_LINES, capslock_from_xkb, cursor_style_to_icon_names, get_xkb_compose_state, is_within_click_distance, keystroke_from_xkb, keystroke_underlying_dead_key, modifiers_from_xkb, open_uri_internal, read_fd, reveal_path_internal, wayland::{ clipboard::{Clipboard, DataOffer, FILE_LIST_MIME_TYPE, TEXT_MIME_TYPES}, cursor::Cursor, serial::{SerialKind, SerialTracker}, to_shape, window::WaylandWindow, }, xdg_desktop_portal::{Event as XDPEvent, XDPEventSource},};use gpui::{ AnyWindowHandle, Bounds, Capslock, CursorStyle, DevicePixels, DisplayId, FileDropEvent, ForegroundExecutor, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers, ModifiersChangedEvent, MouseButton, MouseDownEvent, MouseExitEvent, MouseMoveEvent, MouseUpEvent, NavigationDirection, Pixels, PlatformDisplay, PlatformInput, PlatformKeyboardLayout, PlatformWindow, Point, ScrollDelta, ScrollWheelEvent, SharedString, Size, TaskTiming, TouchPhase, WindowParams, point, profiler, px, size,};use gpui_wgpu::{CompositorGpuHint, GpuContext};use wayland_protocols::wp::linux_dmabuf::zv1::client::{ zwp_linux_dmabuf_feedback_v1, zwp_linux_dmabuf_v1,}; /// Used to convert evdev scancode to xkb scancodeconst MIN_KEYCODE: u32 = 8; const UNKNOWN_KEYBOARD_LAYOUT_NAME: SharedString = SharedString::new_static("unknown"); #[derive(Clone)]pub struct Globals { pub qh: QueueHandle<WaylandClientStatePtr>, pub activation: Option<xdg_activation_v1::XdgActivationV1>, pub compositor: wl_compositor::WlCompositor, pub cursor_shape_manager: Option<wp_cursor_shape_manager_v1::WpCursorShapeManagerV1>, pub data_device_manager: Option<wl_data_device_manager::WlDataDeviceManager>, pub primary_selection_manager: Option<zwp_primary_selection_device_manager_v1::ZwpPrimarySelectionDeviceManagerV1>, pub wm_base: xdg_wm_base::XdgWmBase, pub shm: wl_shm::WlShm, pub seat: wl_seat::WlSeat, pub viewporter: Option<wp_viewporter::WpViewporter>, pub fractional_scale_manager: Option<wp_fractional_scale_manager_v1::WpFractionalScaleManagerV1>, pub decoration_manager: Option<zxdg_decoration_manager_v1::ZxdgDecorationManagerV1>, pub layer_shell: Option<zwlr_layer_shell_v1::ZwlrLayerShellV1>, pub blur_manager: Option<org_kde_kwin_blur_manager::OrgKdeKwinBlurManager>, pub text_input_manager: Option<zwp_text_input_manager_v3::ZwpTextInputManagerV3>, pub gesture_manager: Option<zwp_pointer_gestures_v1::ZwpPointerGesturesV1>, pub dialog: Option<xdg_wm_dialog_v1::XdgWmDialogV1>, pub executor: ForegroundExecutor,} impl Globals { fn new( globals: GlobalList, executor: ForegroundExecutor, qh: QueueHandle<WaylandClientStatePtr>, seat: wl_seat::WlSeat, ) -> Self { let dialog_v = XdgWmDialogV1::interface().version; Globals { activation: globals.bind(&qh, 1..=1, ()).ok(), compositor: globals .bind( &qh, wl_surface::REQ_SET_BUFFER_SCALE_SINCE ..=wl_surface::EVT_PREFERRED_BUFFER_SCALE_SINCE, (), ) .unwrap(), cursor_shape_manager: globals.bind(&qh, 1..=1, ()).ok(), data_device_manager: globals .bind( &qh, WL_DATA_DEVICE_MANAGER_VERSION..=WL_DATA_DEVICE_MANAGER_VERSION, (), ) .ok(), primary_selection_manager: globals.bind(&qh, 1..=1, ()).ok(), shm: globals.bind(&qh, 1..=1, ()).unwrap(), seat, wm_base: globals.bind(&qh, 1..=5, ()).unwrap(), viewporter: globals.bind(&qh, 1..=1, ()).ok(), fractional_scale_manager: globals.bind(&qh, 1..=1, ()).ok(), decoration_manager: globals.bind(&qh, 1..=1, ()).ok(), layer_shell: globals.bind(&qh, 1..=5, ()).ok(), blur_manager: globals.bind(&qh, 1..=1, ()).ok(), text_input_manager: globals.bind(&qh, 1..=1, ()).ok(), gesture_manager: globals.bind(&qh, 1..=3, ()).ok(), dialog: globals.bind(&qh, dialog_v..=dialog_v, ()).ok(), executor, qh, } }} #[derive(Default, Debug, Clone, PartialEq, Eq, Hash)]pub struct InProgressOutput { name: Option<String>, scale: Option<i32>, position: Option<Point<DevicePixels>>, size: Option<Size<DevicePixels>>,} impl InProgressOutput { fn complete(&self) -> Option<Output> { if let Some((position, size)) = self.position.zip(self.size) { let scale = self.scale.unwrap_or(1); Some(Output { name: self.name.clone(), scale, bounds: Bounds::new(position, size), }) } else { None } }} #[derive(Debug, Clone, Eq, PartialEq, Hash)]pub struct Output { pub name: Option<String>, pub scale: i32, pub bounds: Bounds<DevicePixels>,} pub(crate) struct WaylandClientState { serial_tracker: SerialTracker, globals: Globals, pub gpu_context: GpuContext, pub compositor_gpu: Option<CompositorGpuHint>, wl_seat: wl_seat::WlSeat, // TODO: Multi seat support wl_pointer: Option<wl_pointer::WlPointer>, pinch_gesture: Option<zwp_pointer_gesture_pinch_v1::ZwpPointerGesturePinchV1>, pinch_scale: f32, wl_keyboard: Option<wl_keyboard::WlKeyboard>, cursor_shape_device: Option<wp_cursor_shape_device_v1::WpCursorShapeDeviceV1>, data_device: Option<wl_data_device::WlDataDevice>, primary_selection: Option<zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1>, text_input: Option<zwp_text_input_v3::ZwpTextInputV3>, pre_edit_text: Option<String>, ime_pre_edit: Option<String>, composing: bool, // Surface to Window mapping windows: HashMap<ObjectId, WaylandWindowStatePtr>, // Output to scale mapping outputs: HashMap<ObjectId, Output>, in_progress_outputs: HashMap<ObjectId, InProgressOutput>, wl_outputs: HashMap<ObjectId, wl_output::WlOutput>, keyboard_layout: LinuxKeyboardLayout, keymap_state: Option<xkb::State>, compose_state: Option<xkb::compose::State>, drag: DragState, click: ClickState, repeat: KeyRepeat, pub modifiers: Modifiers, pub capslock: Capslock, axis_source: AxisSource, pub mouse_location: Option<Point<Pixels>>, continuous_scroll_delta: Option<Point<Pixels>>, discrete_scroll_delta: Option<Point<f32>>, vertical_modifier: f32, horizontal_modifier: f32, scroll_event_received: bool, enter_token: Option<()>, button_pressed: Option<MouseButton>, mouse_focused_window: Option<WaylandWindowStatePtr>, keyboard_focused_window: Option<WaylandWindowStatePtr>, loop_handle: LoopHandle<'static, WaylandClientStatePtr>, cursor_style: Option<CursorStyle>, clipboard: Clipboard, data_offers: Vec<DataOffer<WlDataOffer>>, primary_data_offer: Option<DataOffer<ZwpPrimarySelectionOfferV1>>, cursor: Cursor, pending_activation: Option<PendingActivation>, event_loop: Option<EventLoop<'static, WaylandClientStatePtr>>, pub common: LinuxCommon,} pub struct DragState { data_offer: Option<wl_data_offer::WlDataOffer>, window: Option<WaylandWindowStatePtr>, position: Point<Pixels>,} pub struct ClickState { last_mouse_button: Option<MouseButton>, last_click: Instant, last_location: Point<Pixels>, current_count: usize,} pub(crate) struct KeyRepeat { characters_per_second: u32, delay: Duration, current_id: u64, current_keycode: Option<xkb::Keycode>,} pub(crate) enum PendingActivation { /// URI to open in the web browser. Uri(String), /// Path to open in the file explorer. Path(PathBuf), /// A window from ourselves to raise. Window(ObjectId),} /// This struct is required to conform to Rust's orphan rules, so we can dispatch on the state but hand the/// window to GPUI.#[derive(Clone)]pub struct WaylandClientStatePtr(Weak<RefCell<WaylandClientState>>); impl WaylandClientStatePtr { pub fn get_client(&self) -> Rc<RefCell<WaylandClientState>> { self.0 .upgrade() .expect("The pointer should always be valid when dispatching in wayland") } pub fn get_serial(&self, kind: SerialKind) -> u32 { self.0.upgrade().unwrap().borrow().serial_tracker.get(kind) } pub fn set_pending_activation(&self, window: ObjectId) { self.0.upgrade().unwrap().borrow_mut().pending_activation = Some(PendingActivation::Window(window)); } pub fn enable_ime(&self) { let client = self.get_client(); let mut state = client.borrow_mut(); let Some(text_input) = state.text_input.take() else { return; }; text_input.enable(); text_input.set_content_type(ContentHint::None, ContentPurpose::Normal); if let Some(window) = state.keyboard_focused_window.clone() { drop(state); if let Some(area) = window.get_ime_area() { text_input.set_cursor_rectangle( f32::from(area.origin.x) as i32, f32::from(area.origin.y) as i32, f32::from(area.size.width) as i32, f32::from(area.size.height) as i32, ); } state = client.borrow_mut(); } text_input.commit(); state.text_input = Some(text_input); } pub fn disable_ime(&self) { let client = self.get_client(); let mut state = client.borrow_mut(); state.composing = false; if let Some(text_input) = &state.text_input { text_input.disable(); text_input.commit(); } } pub fn update_ime_position(&self, bounds: Bounds<Pixels>) { let client = self.get_client(); let state = client.borrow_mut(); if state.composing || state.text_input.is_none() || state.pre_edit_text.is_some() { return; } let text_input = state.text_input.as_ref().unwrap(); text_input.set_cursor_rectangle( bounds.origin.x.as_f32() as i32, bounds.origin.y.as_f32() as i32, bounds.size.width.as_f32() as i32, bounds.size.height.as_f32() as i32, ); text_input.commit(); } pub fn handle_keyboard_layout_change(&self) { let client = self.get_client(); let mut state = client.borrow_mut(); let changed = if let Some(keymap_state) = &state.keymap_state { let layout_idx = keymap_state.serialize_layout(xkbcommon::xkb::STATE_LAYOUT_EFFECTIVE); let keymap = keymap_state.get_keymap(); let layout_name = keymap.layout_get_name(layout_idx); let changed = layout_name != state.keyboard_layout.name(); if changed { state.keyboard_layout = LinuxKeyboardLayout::new(layout_name.to_string().into()); } changed } else { let changed = &UNKNOWN_KEYBOARD_LAYOUT_NAME != state.keyboard_layout.name(); if changed { state.keyboard_layout = LinuxKeyboardLayout::new(UNKNOWN_KEYBOARD_LAYOUT_NAME); } changed }; if changed && let Some(mut callback) = state.common.callbacks.keyboard_layout_change.take() { drop(state); callback(); state = client.borrow_mut(); state.common.callbacks.keyboard_layout_change = Some(callback); } } pub fn drop_window(&self, surface_id: &ObjectId) { let client = self.get_client(); let mut state = client.borrow_mut(); let closed_window = state.windows.remove(surface_id).unwrap(); if let Some(window) = state.mouse_focused_window.take() && !window.ptr_eq(&closed_window) { state.mouse_focused_window = Some(window); } if let Some(window) = state.keyboard_focused_window.take() && !window.ptr_eq(&closed_window) { state.keyboard_focused_window = Some(window); } }} #[derive(Clone)]pub struct WaylandClient(Rc<RefCell<WaylandClientState>>); impl Drop for WaylandClient { fn drop(&mut self) { let mut state = self.0.borrow_mut(); state.windows.clear(); if let Some(wl_pointer) = &state.wl_pointer { wl_pointer.release(); } if let Some(cursor_shape_device) = &state.cursor_shape_device { cursor_shape_device.destroy(); } if let Some(data_device) = &state.data_device { data_device.release(); } if let Some(text_input) = &state.text_input { text_input.destroy(); } }} const WL_DATA_DEVICE_MANAGER_VERSION: u32 = 3; fn wl_seat_version(version: u32) -> u32 { // We rely on the wl_pointer.frame event const WL_SEAT_MIN_VERSION: u32 = 5; const WL_SEAT_MAX_VERSION: u32 = 9; if version < WL_SEAT_MIN_VERSION { panic!( "wl_seat below required version: {} < {}", version, WL_SEAT_MIN_VERSION ); } version.clamp(WL_SEAT_MIN_VERSION, WL_SEAT_MAX_VERSION)} fn wl_output_version(version: u32) -> u32 { const WL_OUTPUT_MIN_VERSION: u32 = 2; const WL_OUTPUT_MAX_VERSION: u32 = 4; if version < WL_OUTPUT_MIN_VERSION { panic!( "wl_output below required version: {} < {}", version, WL_OUTPUT_MIN_VERSION ); } version.clamp(WL_OUTPUT_MIN_VERSION, WL_OUTPUT_MAX_VERSION)} impl WaylandClient { pub(crate) fn new() -> Self { let conn = Connection::connect_to_env().unwrap(); let (globals, event_queue) = registry_queue_init::<WaylandClientStatePtr>(&conn).unwrap(); let qh = event_queue.handle(); let mut seat: Option<wl_seat::WlSeat> = None; #[allow(clippy::mutable_key_type)] let mut in_progress_outputs = HashMap::default(); #[allow(clippy::mutable_key_type)] let mut wl_outputs: HashMap<ObjectId, wl_output::WlOutput> = HashMap::default(); globals.contents().with_list(|list| { for global in list { match &global.interface[..] { "wl_seat" => { seat = Some(globals.registry().bind::<wl_seat::WlSeat, _, _>( global.name, wl_seat_version(global.version), &qh, (), )); } "wl_output" => { let output = globals.registry().bind::<wl_output::WlOutput, _, _>( global.name, wl_output_version(global.version), &qh, (), ); in_progress_outputs.insert(output.id(), InProgressOutput::default()); wl_outputs.insert(output.id(), output); } _ => {} } } });Lines 1–500 of 2466. Load more
use std::{ cell::{RefCell, RefMut}, hash::Hash, os::fd::{AsRawFd, BorrowedFd}, path::PathBuf, rc::{Rc, Weak}, time::{Duration, Instant}, }; use ashpd::WindowIdentifier; use calloop::{ EventLoop, LoopHandle, timer::{TimeoutAction, Timer}, }; use calloop_wayland_source::WaylandSource; use collections::HashMap; use filedescriptor::Pipe; use http_client::Url; use smallvec::SmallVec; use util::ResultExt as _; use wayland_backend::client::ObjectId; use wayland_backend::protocol::WEnum; use wayland_client::event_created_child; use wayland_client::globals::{GlobalList, GlobalListContents, registry_queue_init}; use wayland_client::protocol::wl_callback::{self, WlCallback}; use wayland_client::protocol::wl_data_device_manager::DndAction; use wayland_client::protocol::wl_data_offer::WlDataOffer; use wayland_client::protocol::wl_pointer::AxisSource; use wayland_client::protocol::{ wl_data_device, wl_data_device_manager, wl_data_offer, wl_data_source, wl_output, wl_region, }; use wayland_client::{ Connection, Dispatch, Proxy, QueueHandle, delegate_noop, protocol::{ wl_buffer, wl_compositor, wl_keyboard, wl_pointer, wl_registry, wl_seat, wl_shm, wl_shm_pool, wl_surface, }, }; use wayland_protocols::wp::pointer_gestures::zv1::client::{ zwp_pointer_gesture_pinch_v1, zwp_pointer_gestures_v1, }; use wayland_protocols::wp::primary_selection::zv1::client::zwp_primary_selection_offer_v1::{ self, ZwpPrimarySelectionOfferV1, }; use wayland_protocols::wp::primary_selection::zv1::client::{ zwp_primary_selection_device_manager_v1, zwp_primary_selection_device_v1, zwp_primary_selection_source_v1, }; use wayland_protocols::wp::text_input::zv3::client::zwp_text_input_v3::{ ContentHint, ContentPurpose, }; use wayland_protocols::wp::text_input::zv3::client::{ zwp_text_input_manager_v3, zwp_text_input_v3, }; use wayland_protocols::wp::viewporter::client::{wp_viewport, wp_viewporter}; use wayland_protocols::xdg::activation::v1::client::{xdg_activation_token_v1, xdg_activation_v1}; use wayland_protocols::xdg::decoration::zv1::client::{ zxdg_decoration_manager_v1, zxdg_toplevel_decoration_v1, }; use wayland_protocols::xdg::shell::client::{xdg_surface, xdg_toplevel, xdg_wm_base}; use wayland_protocols::{ wp::cursor_shape::v1::client::{wp_cursor_shape_device_v1, wp_cursor_shape_manager_v1}, xdg::dialog::v1::client::xdg_wm_dialog_v1::{self, XdgWmDialogV1}, }; use wayland_protocols::{ wp::fractional_scale::v1::client::{wp_fractional_scale_manager_v1, wp_fractional_scale_v1}, xdg::dialog::v1::client::xdg_dialog_v1::XdgDialogV1, }; use wayland_protocols_plasma::blur::client::{org_kde_kwin_blur, org_kde_kwin_blur_manager}; use wayland_protocols_wlr::layer_shell::v1::client::{zwlr_layer_shell_v1, zwlr_layer_surface_v1}; use xkbcommon::xkb::ffi::XKB_KEYMAP_FORMAT_TEXT_V1; use xkbcommon::xkb::{self, KEYMAP_COMPILE_NO_FLAGS, Keycode}; use super::{ display::WaylandDisplay, window::{ImeInput, WaylandWindowStatePtr}, }; use crate::linux::{ DOUBLE_CLICK_INTERVAL, LinuxClient, LinuxCommon, LinuxKeyboardLayout, SCROLL_LINES, capslock_from_xkb, cursor_style_to_icon_names, get_xkb_compose_state, is_within_click_distance, keystroke_from_xkb, keystroke_underlying_dead_key, modifiers_from_xkb, open_uri_internal, read_fd, reveal_path_internal, wayland::{ clipboard::{Clipboard, DataOffer, FILE_LIST_MIME_TYPE, TEXT_MIME_TYPES}, cursor::Cursor, serial::{SerialKind, SerialTracker}, to_shape, window::WaylandWindow, }, xdg_desktop_portal::{Event as XDPEvent, XDPEventSource}, }; use gpui::{ AnyWindowHandle, Bounds, Capslock, CursorStyle, DevicePixels, DisplayId, FileDropEvent, ForegroundExecutor, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers, ModifiersChangedEvent, MouseButton, MouseDownEvent, MouseExitEvent, MouseMoveEvent, MouseUpEvent, NavigationDirection, Pixels, PlatformDisplay, PlatformInput, PlatformKeyboardLayout, PlatformWindow, Point, ScrollDelta, ScrollWheelEvent, SharedString, Size, TaskTiming, TouchPhase, WindowParams, point, profiler, px, size, }; use gpui_wgpu::{CompositorGpuHint, GpuContext}; use wayland_protocols::wp::linux_dmabuf::zv1::client::{ zwp_linux_dmabuf_feedback_v1, zwp_linux_dmabuf_v1, }; /// Used to convert evdev scancode to xkb scancode const MIN_KEYCODE: u32 = 8; const UNKNOWN_KEYBOARD_LAYOUT_NAME: SharedString = SharedString::new_static("unknown"); #[derive(Clone)] pub struct Globals { pub qh: QueueHandle<WaylandClientStatePtr>, pub activation: Option<xdg_activation_v1::XdgActivationV1>, pub compositor: wl_compositor::WlCompositor, pub cursor_shape_manager: Option<wp_cursor_shape_manager_v1::WpCursorShapeManagerV1>, pub data_device_manager: Option<wl_data_device_manager::WlDataDeviceManager>, pub primary_selection_manager: Option<zwp_primary_selection_device_manager_v1::ZwpPrimarySelectionDeviceManagerV1>, pub wm_base: xdg_wm_base::XdgWmBase, pub shm: wl_shm::WlShm, pub seat: wl_seat::WlSeat, pub viewporter: Option<wp_viewporter::WpViewporter>, pub fractional_scale_manager: Option<wp_fractional_scale_manager_v1::WpFractionalScaleManagerV1>, pub decoration_manager: Option<zxdg_decoration_manager_v1::ZxdgDecorationManagerV1>, pub layer_shell: Option<zwlr_layer_shell_v1::ZwlrLayerShellV1>, pub blur_manager: Option<org_kde_kwin_blur_manager::OrgKdeKwinBlurManager>, pub text_input_manager: Option<zwp_text_input_manager_v3::ZwpTextInputManagerV3>, pub gesture_manager: Option<zwp_pointer_gestures_v1::ZwpPointerGesturesV1>, pub dialog: Option<xdg_wm_dialog_v1::XdgWmDialogV1>, pub executor: ForegroundExecutor, } impl Globals { fn new( globals: GlobalList, executor: ForegroundExecutor, qh: QueueHandle<WaylandClientStatePtr>, seat: wl_seat::WlSeat, ) -> Self { let dialog_v = XdgWmDialogV1::interface().version; Globals { activation: globals.bind(&qh, 1..=1, ()).ok(), compositor: globals .bind( &qh, wl_surface::REQ_SET_BUFFER_SCALE_SINCE ..=wl_surface::EVT_PREFERRED_BUFFER_SCALE_SINCE, (), ) .unwrap(), cursor_shape_manager: globals.bind(&qh, 1..=1, ()).ok(), data_device_manager: globals .bind( &qh, WL_DATA_DEVICE_MANAGER_VERSION..=WL_DATA_DEVICE_MANAGER_VERSION, (), ) .ok(), primary_selection_manager: globals.bind(&qh, 1..=1, ()).ok(), shm: globals.bind(&qh, 1..=1, ()).unwrap(), seat, wm_base: globals.bind(&qh, 1..=5, ()).unwrap(), viewporter: globals.bind(&qh, 1..=1, ()).ok(), fractional_scale_manager: globals.bind(&qh, 1..=1, ()).ok(), decoration_manager: globals.bind(&qh, 1..=1, ()).ok(), layer_shell: globals.bind(&qh, 1..=5, ()).ok(), blur_manager: globals.bind(&qh, 1..=1, ()).ok(), text_input_manager: globals.bind(&qh, 1..=1, ()).ok(), gesture_manager: globals.bind(&qh, 1..=3, ()).ok(), dialog: globals.bind(&qh, dialog_v..=dialog_v, ()).ok(), executor, qh, } } } #[derive(Default, Debug, Clone, PartialEq, Eq, Hash)] pub struct InProgressOutput { name: Option<String>, scale: Option<i32>, position: Option<Point<DevicePixels>>, size: Option<Size<DevicePixels>>, } impl InProgressOutput { fn complete(&self) -> Option<Output> { if let Some((position, size)) = self.position.zip(self.size) { let scale = self.scale.unwrap_or(1); Some(Output { name: self.name.clone(), scale, bounds: Bounds::new(position, size), }) } else { None } } } #[derive(Debug, Clone, Eq, PartialEq, Hash)] pub struct Output { pub name: Option<String>, pub scale: i32, pub bounds: Bounds<DevicePixels>, } pub(crate) struct WaylandClientState { serial_tracker: SerialTracker, globals: Globals, pub gpu_context: GpuContext, pub compositor_gpu: Option<CompositorGpuHint>, wl_seat: wl_seat::WlSeat, // TODO: Multi seat support wl_pointer: Option<wl_pointer::WlPointer>, pinch_gesture: Option<zwp_pointer_gesture_pinch_v1::ZwpPointerGesturePinchV1>, pinch_scale: f32, wl_keyboard: Option<wl_keyboard::WlKeyboard>, cursor_shape_device: Option<wp_cursor_shape_device_v1::WpCursorShapeDeviceV1>, data_device: Option<wl_data_device::WlDataDevice>, primary_selection: Option<zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1>, text_input: Option<zwp_text_input_v3::ZwpTextInputV3>, pre_edit_text: Option<String>, ime_pre_edit: Option<String>, composing: bool, // Surface to Window mapping windows: HashMap<ObjectId, WaylandWindowStatePtr>, // Output to scale mapping outputs: HashMap<ObjectId, Output>, in_progress_outputs: HashMap<ObjectId, InProgressOutput>, wl_outputs: HashMap<ObjectId, wl_output::WlOutput>, keyboard_layout: LinuxKeyboardLayout, keymap_state: Option<xkb::State>, compose_state: Option<xkb::compose::State>, drag: DragState, click: ClickState, repeat: KeyRepeat, pub modifiers: Modifiers, pub capslock: Capslock, axis_source: AxisSource, pub mouse_location: Option<Point<Pixels>>, continuous_scroll_delta: Option<Point<Pixels>>, discrete_scroll_delta: Option<Point<f32>>, vertical_modifier: f32, horizontal_modifier: f32, scroll_event_received: bool, enter_token: Option<()>, button_pressed: Option<MouseButton>, mouse_focused_window: Option<WaylandWindowStatePtr>, keyboard_focused_window: Option<WaylandWindowStatePtr>, loop_handle: LoopHandle<'static, WaylandClientStatePtr>, cursor_style: Option<CursorStyle>, clipboard: Clipboard, data_offers: Vec<DataOffer<WlDataOffer>>, primary_data_offer: Option<DataOffer<ZwpPrimarySelectionOfferV1>>, cursor: Cursor, pending_activation: Option<PendingActivation>, event_loop: Option<EventLoop<'static, WaylandClientStatePtr>>, pub common: LinuxCommon, } pub struct DragState { data_offer: Option<wl_data_offer::WlDataOffer>, window: Option<WaylandWindowStatePtr>, position: Point<Pixels>, } pub struct ClickState { last_mouse_button: Option<MouseButton>, last_click: Instant, last_location: Point<Pixels>, current_count: usize, } pub(crate) struct KeyRepeat { characters_per_second: u32, delay: Duration, current_id: u64, current_keycode: Option<xkb::Keycode>, } pub(crate) enum PendingActivation { /// URI to open in the web browser. Uri(String), /// Path to open in the file explorer. Path(PathBuf), /// A window from ourselves to raise. Window(ObjectId), } /// This struct is required to conform to Rust's orphan rules, so we can dispatch on the state but hand the /// window to GPUI. #[derive(Clone)] pub struct WaylandClientStatePtr(Weak<RefCell<WaylandClientState>>); impl WaylandClientStatePtr { pub fn get_client(&self) -> Rc<RefCell<WaylandClientState>> { self.0 .upgrade() .expect("The pointer should always be valid when dispatching in wayland") } pub fn get_serial(&self, kind: SerialKind) -> u32 { self.0.upgrade().unwrap().borrow().serial_tracker.get(kind) } pub fn set_pending_activation(&self, window: ObjectId) { self.0.upgrade().unwrap().borrow_mut().pending_activation = Some(PendingActivation::Window(window)); } pub fn enable_ime(&self) { let client = self.get_client(); let mut state = client.borrow_mut(); let Some(text_input) = state.text_input.take() else { return; }; text_input.enable(); text_input.set_content_type(ContentHint::None, ContentPurpose::Normal); if let Some(window) = state.keyboard_focused_window.clone() { drop(state); if let Some(area) = window.get_ime_area() { text_input.set_cursor_rectangle( f32::from(area.origin.x) as i32, f32::from(area.origin.y) as i32, f32::from(area.size.width) as i32, f32::from(area.size.height) as i32, ); } state = client.borrow_mut(); } text_input.commit(); state.text_input = Some(text_input); } pub fn disable_ime(&self) { let client = self.get_client(); let mut state = client.borrow_mut(); state.composing = false; if let Some(text_input) = &state.text_input { text_input.disable(); text_input.commit(); } } pub fn update_ime_position(&self, bounds: Bounds<Pixels>) { let client = self.get_client(); let state = client.borrow_mut(); if state.composing || state.text_input.is_none() || state.pre_edit_text.is_some() { return; } let text_input = state.text_input.as_ref().unwrap(); text_input.set_cursor_rectangle( bounds.origin.x.as_f32() as i32, bounds.origin.y.as_f32() as i32, bounds.size.width.as_f32() as i32, bounds.size.height.as_f32() as i32, ); text_input.commit(); } pub fn handle_keyboard_layout_change(&self) { let client = self.get_client(); let mut state = client.borrow_mut(); let changed = if let Some(keymap_state) = &state.keymap_state { let layout_idx = keymap_state.serialize_layout(xkbcommon::xkb::STATE_LAYOUT_EFFECTIVE); let keymap = keymap_state.get_keymap(); let layout_name = keymap.layout_get_name(layout_idx); let changed = layout_name != state.keyboard_layout.name(); if changed { state.keyboard_layout = LinuxKeyboardLayout::new(layout_name.to_string().into()); } changed } else { let changed = &UNKNOWN_KEYBOARD_LAYOUT_NAME != state.keyboard_layout.name(); if changed { state.keyboard_layout = LinuxKeyboardLayout::new(UNKNOWN_KEYBOARD_LAYOUT_NAME); } changed }; if changed && let Some(mut callback) = state.common.callbacks.keyboard_layout_change.take() { drop(state); callback(); state = client.borrow_mut(); state.common.callbacks.keyboard_layout_change = Some(callback); } } pub fn drop_window(&self, surface_id: &ObjectId) { let client = self.get_client(); let mut state = client.borrow_mut(); let closed_window = state.windows.remove(surface_id).unwrap(); if let Some(window) = state.mouse_focused_window.take() && !window.ptr_eq(&closed_window) { state.mouse_focused_window = Some(window); } if let Some(window) = state.keyboard_focused_window.take() && !window.ptr_eq(&closed_window) { state.keyboard_focused_window = Some(window); } } } #[derive(Clone)] pub struct WaylandClient(Rc<RefCell<WaylandClientState>>); impl Drop for WaylandClient { fn drop(&mut self) { let mut state = self.0.borrow_mut(); state.windows.clear(); if let Some(wl_pointer) = &state.wl_pointer { wl_pointer.release(); } if let Some(cursor_shape_device) = &state.cursor_shape_device { cursor_shape_device.destroy(); } if let Some(data_device) = &state.data_device { data_device.release(); } if let Some(text_input) = &state.text_input { text_input.destroy(); } } } const WL_DATA_DEVICE_MANAGER_VERSION: u32 = 3; fn wl_seat_version(version: u32) -> u32 { // We rely on the wl_pointer.frame event const WL_SEAT_MIN_VERSION: u32 = 5; const WL_SEAT_MAX_VERSION: u32 = 9; if version < WL_SEAT_MIN_VERSION { panic!( "wl_seat below required version: {} < {}", version, WL_SEAT_MIN_VERSION ); } version.clamp(WL_SEAT_MIN_VERSION, WL_SEAT_MAX_VERSION) } fn wl_output_version(version: u32) -> u32 { const WL_OUTPUT_MIN_VERSION: u32 = 2; const WL_OUTPUT_MAX_VERSION: u32 = 4; if version < WL_OUTPUT_MIN_VERSION { panic!( "wl_output below required version: {} < {}", version, WL_OUTPUT_MIN_VERSION ); } version.clamp(WL_OUTPUT_MIN_VERSION, WL_OUTPUT_MAX_VERSION) } impl WaylandClient { pub(crate) fn new() -> Self { let conn = Connection::connect_to_env().unwrap(); let (globals, event_queue) = registry_queue_init::<WaylandClientStatePtr>(&conn).unwrap(); let qh = event_queue.handle(); let mut seat: Option<wl_seat::WlSeat> = None; #[allow(clippy::mutable_key_type)] let mut in_progress_outputs = HashMap::default(); #[allow(clippy::mutable_key_type)] let mut wl_outputs: HashMap<ObjectId, wl_output::WlOutput> = HashMap::default(); globals.contents().with_list(|list| { for global in list { match &global.interface[..] { "wl_seat" => { seat = Some(globals.registry().bind::<wl_seat::WlSeat, _, _>( global.name, wl_seat_version(global.version), &qh, (), )); } "wl_output" => { let output = globals.registry().bind::<wl_output::WlOutput, _, _>( global.name, wl_output_version(global.version), &qh, (), ); in_progress_outputs.insert(output.id(), InProgressOutput::default()); wl_outputs.insert(output.id(), output); } _ => {} } } });