gpui: Add Wayland support (#7664)

This PR adds Wayland support to gpui using
[wayland-rs](https://github.com/Smithay/wayland-rs). It is based on
[#7598](https://github.com/zed-industries/zed/pull/7598).

It detects Wayland support at runtime by checking the existence of the
`WAYLAND_DISPLAY` environment variable. If it does not exist or is
empty, the X11 backend will be used. To use the X11 backend in a Wayland
session (for development purposes), you just need to unset
WAYLAND_DISPLAY (`WAYLAND_DISPLAY= cargo run ...`).

At the moment it only creates the window and renders the initial content
provided by `BladeRenderer`, so it can run "Hello world" example.


![image](https://github.com/zed-industries/zed/assets/40907255/1655bc64-4d36-4178-9851-bfe42f03f716)

Todo:
- [x] Add basic Wayland support.
- [x] Add window resizing.
- [x] Add window closing.
- [x] Add window updating.
- [ ] Implement input handling, fractional scaling, and support other
Wayland protocols.
- [ ] Implement all unimplemented todo!(linux).
- [ ] Add window decorations or use custom decorations (like on MacOS).
- [ ] Address other missing functionality.

Release Notes:
- N/A

---------

Co-authored-by: gabydd <gabydinnerdavid@gmail.com>
Co-authored-by: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
Roman 2024-02-15 01:50:11 +03:00 committed by GitHub
parent 6e6ae0ef21
commit f62baeda64
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 874 additions and 62 deletions

View file

@ -1,5 +1,6 @@
#![allow(unused)]
use std::env;
use std::{
path::{Path, PathBuf},
rc::Rc,
@ -8,21 +9,21 @@ use std::{
};
use async_task::Runnable;
use flume::{Receiver, Sender};
use futures::channel::oneshot;
use parking_lot::Mutex;
use time::UtcOffset;
use collections::{HashMap, HashSet};
use wayland_client::Connection;
use crate::platform::linux::client::Client;
use crate::platform::linux::client_dispatcher::ClientDispatcher;
use crate::platform::linux::wayland::{WaylandClient, WaylandClientDispatcher};
use crate::platform::{X11Client, X11ClientDispatcher, XcbAtoms};
use crate::{
Action, AnyWindowHandle, BackgroundExecutor, Bounds, ClipboardItem, CursorStyle, DisplayId,
Action, AnyWindowHandle, BackgroundExecutor, ClipboardItem, CursorStyle, DisplayId,
ForegroundExecutor, Keymap, LinuxDispatcher, LinuxTextSystem, Menu, PathPromptOptions,
Platform, PlatformDisplay, PlatformInput, PlatformTextSystem, PlatformWindow, Point, Result,
SemanticVersion, Size, Task, WindowAppearance, WindowOptions, X11Display, X11Window,
X11WindowState,
Platform, PlatformDisplay, PlatformInput, PlatformTextSystem, PlatformWindow, Result,
SemanticVersion, Task, WindowOptions,
};
#[derive(Default)]
@ -64,35 +65,78 @@ impl Default for LinuxPlatform {
impl LinuxPlatform {
pub(crate) fn new() -> Self {
let (xcb_connection, x_root_index) =
xcb::Connection::connect_with_extensions(None, &[xcb::Extension::Present], &[])
.unwrap();
let atoms = XcbAtoms::intern_all(&xcb_connection).unwrap();
let wayland_display = env::var_os("WAYLAND_DISPLAY");
let use_wayland = wayland_display.is_some() && !wayland_display.unwrap().is_empty();
let xcb_connection = Arc::new(xcb_connection);
let (main_sender, main_receiver) = flume::unbounded::<Runnable>();
let client_dispatcher: Arc<dyn ClientDispatcher + Send + Sync> =
Arc::new(X11ClientDispatcher::new(&xcb_connection, x_root_index));
let dispatcher = LinuxDispatcher::new(main_sender, &client_dispatcher);
let dispatcher = Arc::new(dispatcher);
let text_system = Arc::new(LinuxTextSystem::new());
let callbacks = Mutex::new(Callbacks::default());
let state = Mutex::new(LinuxPlatformState {
quit_requested: false,
});
let inner = LinuxPlatformInner {
if use_wayland {
Self::new_wayland(main_sender, main_receiver, text_system, callbacks, state)
} else {
Self::new_x11(main_sender, main_receiver, text_system, callbacks, state)
}
}
fn new_wayland(
main_sender: Sender<Runnable>,
main_receiver: Receiver<Runnable>,
text_system: Arc<LinuxTextSystem>,
callbacks: Mutex<Callbacks>,
state: Mutex<LinuxPlatformState>,
) -> Self {
let conn = Arc::new(Connection::connect_to_env().unwrap());
let client_dispatcher: Arc<dyn ClientDispatcher + Send + Sync> =
Arc::new(WaylandClientDispatcher::new(&conn));
let dispatcher = Arc::new(LinuxDispatcher::new(main_sender, &client_dispatcher));
let inner = Arc::new(LinuxPlatformInner {
background_executor: BackgroundExecutor::new(dispatcher.clone()),
foreground_executor: ForegroundExecutor::new(dispatcher.clone()),
main_receiver,
text_system: Arc::new(LinuxTextSystem::new()),
callbacks: Mutex::new(Callbacks::default()),
state: Mutex::new(LinuxPlatformState {
quit_requested: false,
}),
};
let inner = Arc::new(inner);
let x11client = X11Client::new(Arc::clone(&inner), xcb_connection, x_root_index, atoms);
let x11client = Arc::new(x11client);
text_system,
callbacks,
state,
});
let client = Arc::new(WaylandClient::new(Arc::clone(&inner), Arc::clone(&conn)));
Self {
client: x11client,
client,
inner: Arc::clone(&inner),
}
}
fn new_x11(
main_sender: Sender<Runnable>,
main_receiver: Receiver<Runnable>,
text_system: Arc<LinuxTextSystem>,
callbacks: Mutex<Callbacks>,
state: Mutex<LinuxPlatformState>,
) -> Self {
let (xcb_connection, x_root_index) = xcb::Connection::connect(None).unwrap();
let atoms = XcbAtoms::intern_all(&xcb_connection).unwrap();
let xcb_connection = Arc::new(xcb_connection);
let client_dispatcher: Arc<dyn ClientDispatcher + Send + Sync> =
Arc::new(X11ClientDispatcher::new(&xcb_connection, x_root_index));
let dispatcher = Arc::new(LinuxDispatcher::new(main_sender, &client_dispatcher));
let inner = Arc::new(LinuxPlatformInner {
background_executor: BackgroundExecutor::new(dispatcher.clone()),
foreground_executor: ForegroundExecutor::new(dispatcher.clone()),
main_receiver,
text_system,
callbacks,
state,
});
let client = Arc::new(X11Client::new(
Arc::clone(&inner),
xcb_connection,
x_root_index,
atoms,
));
Self {
client,
inner: Arc::clone(&inner),
}
}