Change from window surface to zwp_input_popup surface

This commit is contained in:
2024-07-15 13:52:39 -05:00
parent 0abb4c2096
commit fc5ff2cf61
9 changed files with 2270 additions and 129 deletions

19
.direnv/bin/nix-direnv-reload Executable file
View File

@@ -0,0 +1,19 @@
#!/usr/bin/env bash
set -e
if [[ ! -d "/home/eesim/Projects/waywrite" ]]; then
echo "Cannot find source directory; Did you move it?"
echo "(Looking for "/home/eesim/Projects/waywrite")"
echo 'Cannot force reload with this script - use "direnv reload" manually and then try again'
exit 1
fi
# rebuild the cache forcefully
_nix_direnv_force_reload=1 direnv exec "/home/eesim/Projects/waywrite" true
# Update the mtime for .envrc.
# This will cause direnv to reload again - but without re-building.
touch "/home/eesim/Projects/waywrite/.envrc"
# Also update the timestamp of whatever profile_rc we have.
# This makes sure that we know we are up to date.
touch -r "/home/eesim/Projects/waywrite/.envrc" "/home/eesim/Projects/waywrite/.direnv"/*.rc

View File

@@ -0,0 +1 @@
/nix/store/3aamj0h5bv74fnn44584y5yp480076xk-nix-shell-env

File diff suppressed because it is too large Load Diff

1
.envrc Normal file
View File

@@ -0,0 +1 @@
use nix

14
Cargo.lock generated
View File

@@ -1157,6 +1157,19 @@ dependencies = [
"wayland-scanner", "wayland-scanner",
] ]
[[package]]
name = "wayland-protocols-misc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa5933740b200188c9b4c38601b8212e8c154d7de0d2cb171944e137a77de1e"
dependencies = [
"bitflags 2.5.0",
"wayland-backend",
"wayland-client",
"wayland-protocols",
"wayland-scanner",
]
[[package]] [[package]]
name = "wayland-protocols-wlr" name = "wayland-protocols-wlr"
version = "0.2.0" version = "0.2.0"
@@ -1205,6 +1218,7 @@ dependencies = [
"rten-tensor", "rten-tensor",
"smithay-client-toolkit", "smithay-client-toolkit",
"wayland-client", "wayland-client",
"wayland-protocols-misc",
] ]
[[package]] [[package]]

View File

@@ -15,3 +15,4 @@ smithay-client-toolkit = "0.18.1"
wayland-client = "0.31.2" wayland-client = "0.31.2"
itertools = "0.12.1" itertools = "0.12.1"
raqote = "0.8.4" raqote = "0.8.4"
wayland-protocols-misc = {version = "0.2.0", features = ["client"]}

4
rust-toolchain.toml Normal file
View File

@@ -0,0 +1,4 @@
[toolchain]
channel = "stable"
components = [ "rust-analyzer" ]
targets = ["x86_64-unknown-linux-gnu"]

41
shell.nix Normal file
View File

@@ -0,0 +1,41 @@
{ pkgs ? import <nixpkgs> {} }:
let
overrides = (builtins.fromTOML (builtins.readFile ./rust-toolchain.toml));
libPath = with pkgs; lib.makeLibraryPath [
];
in
pkgs.mkShell rec {
buildInputs = with pkgs; [
clang
llvmPackages.bintools
rustup
pkg-config
fontconfig
libxkbcommon
];
RUSTC_VERSION = overrides.toolchain.channel;
# https://github.com/rust-lang/rust-bindgen#environment-variables
LIBCLANG_PATH = pkgs.lib.makeLibraryPath [ pkgs.llvmPackages_latest.libclang.lib ];
shellHook = ''
export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin
export PATH=$PATH:''${RUSTUP_HOME:-~/.rustup}/toolchains/$RUSTC_VERSION-x86_64-unknown-linux-gnu/bin/
'';
# Add precompiled library to rustc search path
RUSTFLAGS = (builtins.map (a: ''-L ${a}/lib'') [
# add libraries here (e.g. pkgs.libvmi)
]);
LD_LIBRARY_PATH = libPath;
# Add glibc, clang, glib, and other headers to bindgen search path
BINDGEN_EXTRA_CLANG_ARGS =
# Includes normal include path
(builtins.map (a: ''-I"${a}/include"'') [
# add dev libraries here (e.g. pkgs.libvmi.dev)
pkgs.glibc.dev
])
# Includes with special directory paths
++ [
''-I"${pkgs.llvmPackages_latest.libclang.lib}/lib/clang/${pkgs.llvmPackages_latest.libclang.version}/include"''
''-I"${pkgs.glib.dev}/include/glib-2.0"''
''-I${pkgs.glib.out}/lib/glib-2.0/include/''
];
}

View File

@@ -3,7 +3,8 @@ use smithay_client_toolkit::{
activation::{ActivationState, ActivationHandler, RequestData}, activation::{ActivationState, ActivationHandler, RequestData},
compositor::{CompositorState, CompositorHandler}, compositor::{CompositorState, CompositorHandler},
output::{OutputHandler, OutputState}, output::{OutputHandler, OutputState},
registry::{RegistryState, ProvidesRegistryState}, globals::GlobalData,
registry::{self, RegistryState, ProvidesRegistryState, RegistryHandler, SimpleGlobal},
registry_handlers, registry_handlers,
seat::{ seat::{
pointer::{PointerEvent, PointerEventKind, PointerHandler}, pointer::{PointerEvent, PointerEventKind, PointerHandler},
@@ -21,13 +22,21 @@ use smithay_client_toolkit::{
}, },
delegate_registry, delegate_compositor, delegate_seat, delegate_output, delegate_registry, delegate_compositor, delegate_seat, delegate_output,
delegate_xdg_shell, delegate_shm, delegate_activation, delegate_xdg_window, delegate_xdg_shell, delegate_shm, delegate_activation, delegate_xdg_window,
delegate_pointer, delegate_pointer, delegate_simple,
}; };
use wayland_client::{ use wayland_client::{
Connection, QueueHandle, Connection, QueueHandle, Proxy, Dispatch,
globals::registry_queue_init, globals::{registry_queue_init, GlobalList},
protocol::{wl_surface, wl_output, wl_seat, wl_shm, wl_pointer}, protocol::{wl_surface::{self, WlSurface}, wl_output, wl_seat, wl_shm, wl_pointer, wl_keyboard},
};
use wayland_protocols_misc::zwp_input_method_v2::client::{
zwp_input_method_v2::{self, ZwpInputMethodV2},
zwp_input_popup_surface_v2::{self, ZwpInputPopupSurfaceV2},
zwp_input_method_manager_v2::{self, ZwpInputMethodManagerV2},
zwp_input_method_keyboard_grab_v2::{self, ZwpInputMethodKeyboardGrabV2},
}; };
use std::time::Duration; use std::time::Duration;
@@ -40,21 +49,28 @@ use crate::wayland::{
Position, Position,
}; };
use crate::process_point::print_words; use crate::process_point::print_words;
const WINDOW_HEIGHT: usize = 256; const WINDOW_HEIGHT: usize = 256;
const WINDOW_WIDTH: usize = 512; const WINDOW_WIDTH: usize = 512;
pub(crate) struct SimpleWindow { pub(crate) struct SimpleWindow {
registry_state: RegistryState, registry_state: RegistryState,
seat_state: SeatState, seat_state: SeatState,
output_state: OutputState, output_state: OutputState,
shm: Shm, compositor_state: CompositorState,
xdg_activation: Option<ActivationState>, xdg_activation: Option<ActivationState>,
input_manager: SimpleGlobal<ZwpInputMethodManagerV2, 1>,
input_method: Option<ZwpInputMethodV2>,
keyboard_grab: Option<ZwpInputMethodKeyboardGrabV2>,
popup_surface: Option<ZwpInputPopupSurfaceV2>,
shm: Shm,
buffer: Option<Buffer>, buffer: Option<Buffer>,
pool: SlotPool, pool: SlotPool,
window: Window, surface: Option<WlSurface>,
ui: Option<ui::Window>, ui: Option<ui::Window>,
width: usize, width: usize,
height: usize, height: usize,
@@ -69,6 +85,8 @@ pub(crate) struct SimpleWindow {
impl SimpleWindow { impl SimpleWindow {
pub(crate) fn run() -> anyhow::Result<()> { pub(crate) fn run() -> anyhow::Result<()> {
std::thread::sleep(std::time::Duration::from_secs(3));
let conn = Connection::connect_to_env()?; let conn = Connection::connect_to_env()?;
let (globals, event_queue) = registry_queue_init(&conn)?; let (globals, event_queue) = registry_queue_init(&conn)?;
@@ -78,44 +96,30 @@ impl SimpleWindow {
let loop_handle = event_loop.handle(); let loop_handle = event_loop.handle();
WaylandSource::new(conn.clone(), event_queue).insert(loop_handle)?; WaylandSource::new(conn.clone(), event_queue).insert(loop_handle)?;
let input_manager = SimpleGlobal::<ZwpInputMethodManagerV2, 1>::bind(&globals, &qh)
.expect("zwp_input_method_manager_v2 not available");
let compositor = CompositorState::bind(&globals, &qh)?; let compositor = CompositorState::bind(&globals, &qh)?;
let xdg_shell = XdgShell::bind(&globals, &qh)?;
let shm = Shm::bind(&globals, &qh)?; let shm = Shm::bind(&globals, &qh)?;
let xdg_activation = ActivationState::bind(&globals, &qh).ok();
let surface = compositor.create_surface(&qh);
let window = xdg_shell.create_window(surface, WindowDecorations::RequestServer, &qh);
window.set_title("A window");
window.set_app_id("simmer.simplewindow");
window.set_min_size(Some((WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32)));
window.set_max_size(Some((WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32)));
window.commit();
if let Some(activation) = xdg_activation.as_ref() {
activation.request_token(
&qh,
RequestData {
seat_and_serial: None,
surface: Some(window.wl_surface().clone()),
app_id: Some(String::from("simmer.waywrite")),
},
)
}
let pool = SlotPool::new((WINDOW_WIDTH as usize) * (WINDOW_HEIGHT as usize) * 4, &shm)?; let pool = SlotPool::new((WINDOW_WIDTH as usize) * (WINDOW_HEIGHT as usize) * 4, &shm)?;
let xdg_activation = ActivationState::bind(&globals, &qh).ok();
let mut simple_window = SimpleWindow { let mut simple_window = SimpleWindow {
registry_state: RegistryState::new(&globals), registry_state: RegistryState::new(&globals),
seat_state: SeatState::new(&globals, &qh), seat_state: SeatState::new(&globals, &qh),
output_state: OutputState::new(&globals, &qh), output_state: OutputState::new(&globals, &qh),
compositor_state: compositor,
xdg_activation, xdg_activation,
input_manager,
input_method: None,
keyboard_grab: None,
popup_surface: None,
shm, shm,
buffer: None, buffer: None,
pool, pool,
window, surface: None,
ui: None, ui: None,
width: WINDOW_WIDTH, width: WINDOW_WIDTH,
height: WINDOW_HEIGHT, height: WINDOW_HEIGHT,
@@ -131,6 +135,14 @@ impl SimpleWindow {
loop { loop {
event_loop.dispatch(Duration::from_millis(16), &mut simple_window)?; event_loop.dispatch(Duration::from_millis(16), &mut simple_window)?;
if simple_window.first_configure {
if let Some(_) = simple_window.surface {
simple_window.configure(&conn, &qh);
}
} else {
simple_window.draw(&conn, &qh);
}
if simple_window.exit { if simple_window.exit {
println!("exiting"); println!("exiting");
break; break;
@@ -142,6 +154,17 @@ impl SimpleWindow {
} }
} }
impl ActivationHandler for SimpleWindow {
type RequestData = RequestData;
fn new_token(&mut self, token: String, data: &Self::RequestData) {
self.xdg_activation
.as_ref()
.unwrap()
.activate::<SimpleWindow>(self.surface.as_ref().unwrap(), token);
}
}
impl CompositorHandler for SimpleWindow { impl CompositorHandler for SimpleWindow {
fn scale_factor_changed( fn scale_factor_changed(
&mut self, &mut self,
@@ -184,10 +207,21 @@ impl SeatHandler for SimpleWindow {
seat: wl_seat::WlSeat, seat: wl_seat::WlSeat,
capability: Capability, capability: Capability,
) { ) {
if capability == Capability::Pointer && self.pointer.is_none() { if capability == Capability::Pointer && self.pointer.is_none() && self.input_method.is_some() {
println!("Set pointer capability"); println!("Set pointer capability");
let pointer = self.seat_state.get_pointer(qh, &seat).expect("Failed to create pointer"); let pointer = self.seat_state.get_pointer(qh, &seat).expect("Failed to create pointer");
self.pointer = Some(pointer); self.pointer = Some(pointer);
} else if capability == Capability::Keyboard && self.input_method.is_none() {
let input_method = self.input_manager
.get()
.expect("failed to create input method")
.get_input_method(&seat, qh, ());
let surface = self.compositor_state.create_surface(&qh);
self.popup_surface = Some(input_method.get_input_popup_surface(&surface, &qh, ()));
// self.keyboard_grab = Some(input_method.grab_keyboard(&qh, ()));
self.surface = Some(surface);
self.input_method = Some(input_method);
} }
} }
fn remove_capability( fn remove_capability(
@@ -234,77 +268,6 @@ impl OutputHandler for SimpleWindow {
) {} ) {}
} }
impl WindowHandler for SimpleWindow {
fn request_close(
&mut self,
_: &Connection,
_: &QueueHandle<Self>,
_: &Window
) {
self.exit = true;
}
fn configure(
&mut self,
conn: &Connection,
qh: &QueueHandle<Self>,
_: &Window,
_: WindowConfigure,
_: u32,
) {
if self.first_configure {
self.first_configure = false;
self.draw(conn, qh);
}
if self.ui.is_none() {
let mut draw_area = DrawAreaBuilder::new();
draw_area.position(0.0, 50.0);
draw_area.width(self.width - 100);
draw_area.height(self.height - 50);
draw_area.path(&mut self.draw_path);
let draw_area = draw_area.finish();
let mut window = ui::Window::new(
self.width.try_into().unwrap(),
self.height.try_into().unwrap(),
draw_area,
);
let mut enter_button = ButtonBuilder::new();
enter_button.button_type(ButtonType::Enter);
enter_button.position(self.width - 100, 60);
enter_button.width(100);
enter_button.height(50);
let enter_button = enter_button.finish();
let mut clear_button = ButtonBuilder::new();
clear_button.button_type(ButtonType::Clear);
clear_button.position(self.width - 100, 120);
clear_button.width(100);
clear_button.height(50);
let clear_button = clear_button.finish();
let mut text_area = TextAreaBuilder::new();
text_area.position(0.0, 0.0);
text_area.width(self.width);
text_area.height(50);
let text_area = text_area.finish();
window.add_widget(enter_button);
window.add_widget(clear_button);
window.add_widget(text_area);
self.ui = Some(window);
}
}
}
impl ShmHandler for SimpleWindow { impl ShmHandler for SimpleWindow {
fn shm_state(&mut self) -> &mut Shm { fn shm_state(&mut self) -> &mut Shm {
@@ -312,17 +275,6 @@ impl ShmHandler for SimpleWindow {
} }
} }
impl ActivationHandler for SimpleWindow {
type RequestData = RequestData;
fn new_token(&mut self, token: String, _: &Self::RequestData) {
self.xdg_activation
.as_ref()
.unwrap()
.activate::<SimpleWindow>(self.window.wl_surface(), token);
}
}
impl PointerHandler for SimpleWindow { impl PointerHandler for SimpleWindow {
fn pointer_frame( fn pointer_frame(
&mut self, &mut self,
@@ -334,9 +286,6 @@ impl PointerHandler for SimpleWindow {
use PointerEventKind::*; use PointerEventKind::*;
for event in events { for event in events {
// Ignore events for other surfaces // Ignore events for other surfaces
if &event.surface != self.window.wl_surface() {
continue;
}
match event.kind { match event.kind {
Enter { .. } => { Enter { .. } => {
@@ -379,7 +328,10 @@ impl PointerHandler for SimpleWindow {
window.draw_area.path = DrawPath::default(); window.draw_area.path = DrawPath::default();
}, },
ButtonType::Enter if button.contains_point(pos) => { ButtonType::Enter if button.contains_point(pos) => {
print_words((&window.draw_area).into()).unwrap() if let Some(input_method) = &self.input_method {
input_method.commit_string(String::from("aaa"));
};
print_words((&window.draw_area).into()).unwrap();
}, },
_ => (), _ => (),
} }
@@ -433,37 +385,157 @@ impl SimpleWindow {
}; };
if let Some(window) = &mut self.ui {
window.render(canvas); if let Some(surface) = &self.surface {
if let Some(ui) = &mut self.ui {
ui.render(canvas);
} }
self.window.wl_surface().damage_buffer(0, 0, self.width as i32, self.height as i32);
self.window.wl_surface().frame(qh, self.window.wl_surface().clone());
buffer.attach_to(self.window.wl_surface()).expect("buffer attach");
self.window.commit();
surface.damage_buffer(0, 0, self.width as i32, self.height as i32);
surface.frame(qh, surface.clone());
buffer.attach_to(surface).expect("buffer attach");
surface.commit();
}
}
fn configure(
&mut self,
conn: &Connection,
qh: &QueueHandle<Self>,
) {
if self.first_configure {
self.first_configure = false;
self.draw(conn, qh);
}
if self.ui.is_none() {
let mut draw_area = DrawAreaBuilder::new();
draw_area.position(0.0, 50.0);
draw_area.width(self.width - 100);
draw_area.height(self.height - 50);
draw_area.path(&mut self.draw_path);
let draw_area = draw_area.finish();
let mut ui = ui::Window::new(
self.width.try_into().unwrap(),
self.height.try_into().unwrap(),
draw_area,
);
let mut enter_button = ButtonBuilder::new();
enter_button.button_type(ButtonType::Enter);
enter_button.position(self.width - 100, 60);
enter_button.width(100);
enter_button.height(50);
let enter_button = enter_button.finish();
let mut clear_button = ButtonBuilder::new();
clear_button.button_type(ButtonType::Clear);
clear_button.position(self.width - 100, 120);
clear_button.width(100);
clear_button.height(50);
let clear_button = clear_button.finish();
let mut text_area = TextAreaBuilder::new();
text_area.position(0.0, 0.0);
text_area.width(self.width);
text_area.height(50);
let text_area = text_area.finish();
ui.add_widget(enter_button);
ui.add_widget(clear_button);
ui.add_widget(text_area);
self.ui = Some(ui);
}
} }
} }
delegate_compositor!(SimpleWindow); delegate_compositor!(SimpleWindow);
delegate_output!(SimpleWindow); delegate_output!(SimpleWindow);
delegate_shm!(SimpleWindow); delegate_shm!(SimpleWindow);
delegate_activation!(SimpleWindow);
delegate_seat!(SimpleWindow); delegate_seat!(SimpleWindow);
delegate_xdg_shell!(SimpleWindow);
delegate_xdg_window!(SimpleWindow);
delegate_activation!(SimpleWindow);
delegate_pointer!(SimpleWindow); delegate_pointer!(SimpleWindow);
delegate_registry!(SimpleWindow); delegate_registry!(SimpleWindow);
delegate_simple!(SimpleWindow, ZwpInputMethodManagerV2, 1);
impl ProvidesRegistryState for SimpleWindow { impl ProvidesRegistryState for SimpleWindow {
fn registry(&mut self) -> &mut RegistryState { fn registry(&mut self) -> &mut RegistryState {
&mut self.registry_state &mut self.registry_state
} }
registry_handlers![OutputState, SeatState,]; registry_handlers![OutputState, SeatState];
}
impl Dispatch<ZwpInputMethodV2, ()> for SimpleWindow {
fn event(
_: &mut Self,
_: &ZwpInputMethodV2,
event: zwp_input_method_v2::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
match event {
zwp_input_method_v2::Event::Done => println!("input method done"),
zwp_input_method_v2::Event::Activate => println!("input method activated"),
zwp_input_method_v2::Event::Deactivate => println!("input method deactivated"),
zwp_input_method_v2::Event::ContentType { hint, purpose } => println!("content type, hint: {:#?}, purpose: {:#?}", hint, purpose),
zwp_input_method_v2::Event::Unavailable => println!("input method unavailable"),
zwp_input_method_v2::Event::SurroundingText { text, cursor, anchor } => println!("surrounding text, text: {:#?}, cursor: {:#?}, anchor: {:#?}", text, cursor, anchor),
zwp_input_method_v2::Event::TextChangeCause { cause } => println!("input change cause: {:#?}", cause),
_ => unreachable!("zwp_input_method_v2 has no other events"),
}
}
}
impl Dispatch<ZwpInputPopupSurfaceV2, ()> for SimpleWindow {
fn event(
_: &mut Self,
_: &ZwpInputPopupSurfaceV2,
event: zwp_input_popup_surface_v2::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
match event {
zwp_input_popup_surface_v2::Event::TextInputRectangle { x, y, width, height } => println!("New text input rectangle at {}, {}", x, y),
_ => unreachable!("zwp_input_popup_surface_v2::Event only contains TextInputRectangle"),
};
}
}
impl Dispatch<ZwpInputMethodKeyboardGrabV2, ()> for SimpleWindow {
fn event(
_: &mut Self,
_: &ZwpInputMethodKeyboardGrabV2,
event: zwp_input_method_keyboard_grab_v2::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
match event {
zwp_input_method_keyboard_grab_v2::Event::Key { serial, time, key, state } => {},
zwp_input_method_keyboard_grab_v2::Event::Keymap { format, fd, size } => {},
zwp_input_method_keyboard_grab_v2::Event::Modifiers { serial, mods_depressed, mods_latched, mods_locked, group } => {},
zwp_input_method_keyboard_grab_v2::Event::RepeatInfo { rate, delay } => {},
_ => unreachable!("zwp_input_method_keyboard_grab_v2::Event only has Key, Keymap, Modifiers, and RepeatInfo"),
};
}
} }