pinnacle/src/focus.rs

464 lines
13 KiB
Rust
Raw Normal View History

2023-08-01 11:06:35 -05:00
// SPDX-License-Identifier: GPL-3.0-or-later
2023-06-25 17:18:50 -05:00
use smithay::{
desktop::{LayerSurface, PopupKind},
input::{
keyboard::KeyboardTarget,
pointer::{MotionEvent, PointerTarget},
2024-02-29 16:28:25 -06:00
touch::{self, TouchTarget},
Seat,
},
output::Output,
reexports::wayland_server::{protocol::wl_surface::WlSurface, Resource},
2024-02-29 16:28:25 -06:00
utils::{IsAlive, Serial, SERIAL_COUNTER},
wayland::seat::WaylandFocus,
};
2023-07-24 18:59:05 -05:00
2023-09-09 22:47:59 -05:00
use crate::{
state::{State, WithState},
window::WindowElement,
};
2023-06-17 21:02:58 -05:00
2023-09-09 22:47:59 -05:00
impl State {
2023-12-14 15:23:37 -06:00
/// Get the currently focused window on `output`
/// that isn't an override redirect window, if any.
pub fn focused_window(&self, output: &Output) -> Option<WindowElement> {
// TODO: see if the below is necessary
// output.with_state(|state| state.focus_stack.stack.retain(|win| win.alive()));
2023-09-09 22:47:59 -05:00
let windows = output.with_state(|state| {
state
.focus_stack
.stack
2023-09-09 22:47:59 -05:00
.iter()
.rev()
.filter(|win| {
let win_tags = win.with_state(|state| state.tags.clone());
let output_tags = state.focused_tags().cloned().collect::<Vec<_>>();
win_tags
.iter()
.any(|win_tag| output_tags.iter().any(|op_tag| win_tag == op_tag))
})
.cloned()
.collect::<Vec<_>>()
2023-09-09 22:47:59 -05:00
});
windows
.into_iter()
.find(|win| !win.is_x11_override_redirect())
2023-09-09 22:47:59 -05:00
}
2023-09-12 09:38:34 -05:00
/// Update the keyboard focus.
2023-09-12 09:38:34 -05:00
pub fn update_focus(&mut self, output: &Output) {
2023-09-15 02:50:42 -05:00
let current_focus = self.focused_window(output);
2023-09-12 09:38:34 -05:00
if let Some(win) = &current_focus {
assert!(!win.is_x11_override_redirect());
if let Some(toplevel) = win.toplevel() {
toplevel.send_configure();
2023-09-15 02:50:42 -05:00
}
2023-09-12 09:38:34 -05:00
}
self.seat.get_keyboard().expect("no keyboard").set_focus(
self,
current_focus.map(|win| win.into()),
SERIAL_COUNTER.next_serial(),
);
}
pub fn fixup_focus(&mut self) {
for win in self.z_index_stack.stack.iter() {
self.space.raise_element(win, false);
}
2023-09-12 09:38:34 -05:00
}
2023-09-09 22:47:59 -05:00
}
/// A vector of windows, with the last one being the one in focus and the first
/// being the one at the bottom of the focus stack.
#[derive(Debug)]
pub struct FocusStack<T> {
pub stack: Vec<T>,
focused: bool,
}
2023-06-17 21:02:58 -05:00
impl<T> Default for FocusStack<T> {
fn default() -> Self {
Self {
stack: Default::default(),
focused: Default::default(),
}
2023-06-17 21:02:58 -05:00
}
}
2023-08-11 18:48:51 -05:00
impl<T: PartialEq> FocusStack<T> {
/// Set `focus` to be focused.
2023-08-11 18:48:51 -05:00
///
/// If it's already in the stack, it will be removed then pushed.
/// If it isn't, it will just be pushed.
pub fn set_focus(&mut self, focus: T) {
self.stack.retain(|foc| foc != &focus);
self.stack.push(focus);
self.focused = true;
}
pub fn unset_focus(&mut self) {
self.focused = false;
}
pub fn current_focus(&self) -> Option<&T> {
self.focused.then(|| self.stack.last())?
2023-08-11 18:48:51 -05:00
}
2023-06-17 21:02:58 -05:00
}
2023-12-16 21:20:29 -06:00
/// Different focusable objects.
#[derive(Debug, Clone, PartialEq)]
pub enum FocusTarget {
Window(WindowElement),
Popup(PopupKind),
2023-08-04 18:48:10 -05:00
LayerSurface(LayerSurface),
}
impl IsAlive for FocusTarget {
fn alive(&self) -> bool {
match self {
FocusTarget::Window(window) => window.alive(),
FocusTarget::Popup(popup) => popup.alive(),
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => surf.alive(),
}
}
}
2023-07-28 15:16:25 -05:00
impl TryFrom<FocusTarget> for WlSurface {
type Error = ();
fn try_from(value: FocusTarget) -> Result<Self, Self::Error> {
value.wl_surface().ok_or(())
}
}
2023-08-28 22:53:24 -05:00
impl PointerTarget<State> for FocusTarget {
2023-10-12 17:10:23 -05:00
fn frame(&self, seat: &Seat<State>, data: &mut State) {
match self {
2024-02-29 16:28:25 -06:00
FocusTarget::Window(window) => PointerTarget::frame(window, seat, data),
FocusTarget::Popup(popup) => PointerTarget::frame(popup.wl_surface(), seat, data),
FocusTarget::LayerSurface(surf) => PointerTarget::frame(surf.wl_surface(), seat, data),
2023-10-12 17:10:23 -05:00
}
}
2023-08-28 22:53:24 -05:00
fn enter(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
match self {
FocusTarget::Window(window) => PointerTarget::enter(window, seat, data, event),
FocusTarget::Popup(popup) => {
PointerTarget::enter(popup.wl_surface(), seat, data, event);
}
2024-02-29 16:28:25 -06:00
FocusTarget::LayerSurface(surf) => {
PointerTarget::enter(surf.wl_surface(), seat, data, event)
}
}
}
2023-08-28 22:53:24 -05:00
fn motion(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
match self {
FocusTarget::Window(window) => PointerTarget::motion(window, seat, data, event),
FocusTarget::Popup(popup) => {
PointerTarget::motion(popup.wl_surface(), seat, data, event);
}
2024-02-29 16:28:25 -06:00
FocusTarget::LayerSurface(surf) => {
PointerTarget::motion(surf.wl_surface(), seat, data, event)
}
}
}
fn relative_motion(
&self,
2023-08-28 22:53:24 -05:00
seat: &Seat<State>,
data: &mut State,
event: &smithay::input::pointer::RelativeMotionEvent,
) {
match self {
FocusTarget::Window(window) => {
PointerTarget::relative_motion(window, seat, data, event);
}
FocusTarget::Popup(popup) => {
PointerTarget::relative_motion(popup.wl_surface(), seat, data, event);
}
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => {
2024-02-29 16:28:25 -06:00
PointerTarget::relative_motion(surf.wl_surface(), seat, data, event);
2023-08-04 18:48:10 -05:00
}
}
}
fn button(
&self,
2023-08-28 22:53:24 -05:00
seat: &Seat<State>,
data: &mut State,
event: &smithay::input::pointer::ButtonEvent,
) {
match self {
FocusTarget::Window(window) => PointerTarget::button(window, seat, data, event),
FocusTarget::Popup(popup) => {
PointerTarget::button(popup.wl_surface(), seat, data, event);
}
2024-02-29 16:28:25 -06:00
FocusTarget::LayerSurface(surf) => {
PointerTarget::button(surf.wl_surface(), seat, data, event)
}
}
}
fn axis(
&self,
2023-08-28 22:53:24 -05:00
seat: &Seat<State>,
data: &mut State,
frame: smithay::input::pointer::AxisFrame,
) {
match self {
FocusTarget::Window(window) => PointerTarget::axis(window, seat, data, frame),
FocusTarget::Popup(popup) => PointerTarget::axis(popup.wl_surface(), seat, data, frame),
2024-02-29 16:28:25 -06:00
FocusTarget::LayerSurface(surf) => {
PointerTarget::axis(surf.wl_surface(), seat, data, frame)
}
}
}
2024-02-29 16:28:25 -06:00
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial, time: u32) {
match self {
FocusTarget::Window(window) => PointerTarget::leave(window, seat, data, serial, time),
FocusTarget::Popup(popup) => {
PointerTarget::leave(popup.wl_surface(), seat, data, serial, time);
}
2024-02-29 16:28:25 -06:00
FocusTarget::LayerSurface(surf) => {
PointerTarget::leave(surf.wl_surface(), seat, data, serial, time)
}
}
}
2023-09-08 20:46:01 -05:00
fn gesture_swipe_begin(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GestureSwipeBeginEvent,
) {
todo!()
}
fn gesture_swipe_update(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GestureSwipeUpdateEvent,
) {
todo!()
}
fn gesture_swipe_end(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GestureSwipeEndEvent,
) {
todo!()
}
fn gesture_pinch_begin(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GesturePinchBeginEvent,
) {
todo!()
}
fn gesture_pinch_update(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GesturePinchUpdateEvent,
) {
todo!()
}
fn gesture_pinch_end(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GesturePinchEndEvent,
) {
todo!()
}
fn gesture_hold_begin(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GestureHoldBeginEvent,
) {
todo!()
}
fn gesture_hold_end(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GestureHoldEndEvent,
) {
todo!()
}
}
2023-08-28 22:53:24 -05:00
impl KeyboardTarget<State> for FocusTarget {
fn enter(
&self,
2023-08-28 22:53:24 -05:00
seat: &Seat<State>,
data: &mut State,
keys: Vec<smithay::input::keyboard::KeysymHandle<'_>>,
2024-02-29 16:28:25 -06:00
serial: Serial,
) {
match self {
FocusTarget::Window(window) => KeyboardTarget::enter(window, seat, data, keys, serial),
FocusTarget::Popup(popup) => {
KeyboardTarget::enter(popup.wl_surface(), seat, data, keys, serial);
}
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => {
2024-02-29 16:28:25 -06:00
KeyboardTarget::enter(surf.wl_surface(), seat, data, keys, serial);
2023-08-04 18:48:10 -05:00
}
}
}
2024-02-29 16:28:25 -06:00
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial) {
match self {
FocusTarget::Window(window) => KeyboardTarget::leave(window, seat, data, serial),
FocusTarget::Popup(popup) => {
KeyboardTarget::leave(popup.wl_surface(), seat, data, serial);
}
2024-02-29 16:28:25 -06:00
FocusTarget::LayerSurface(surf) => {
KeyboardTarget::leave(surf.wl_surface(), seat, data, serial)
}
}
}
fn key(
&self,
2023-08-28 22:53:24 -05:00
seat: &Seat<State>,
data: &mut State,
key: smithay::input::keyboard::KeysymHandle<'_>,
state: smithay::backend::input::KeyState,
2024-02-29 16:28:25 -06:00
serial: Serial,
time: u32,
) {
match self {
FocusTarget::Window(window) => {
KeyboardTarget::key(window, seat, data, key, state, serial, time);
}
FocusTarget::Popup(popup) => {
KeyboardTarget::key(popup.wl_surface(), seat, data, key, state, serial, time);
}
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => {
2024-02-29 16:28:25 -06:00
KeyboardTarget::key(surf.wl_surface(), seat, data, key, state, serial, time);
2023-08-04 18:48:10 -05:00
}
}
}
fn modifiers(
&self,
2023-08-28 22:53:24 -05:00
seat: &Seat<State>,
data: &mut State,
modifiers: smithay::input::keyboard::ModifiersState,
2024-02-29 16:28:25 -06:00
serial: Serial,
) {
match self {
FocusTarget::Window(window) => {
KeyboardTarget::modifiers(window, seat, data, modifiers, serial);
}
FocusTarget::Popup(popup) => {
KeyboardTarget::modifiers(popup.wl_surface(), seat, data, modifiers, serial);
}
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => {
2024-02-29 16:28:25 -06:00
KeyboardTarget::modifiers(surf.wl_surface(), seat, data, modifiers, serial);
2023-08-04 18:48:10 -05:00
}
}
}
}
2024-02-29 16:28:25 -06:00
impl TouchTarget<State> for FocusTarget {
fn down(&self, seat: &Seat<State>, data: &mut State, event: &touch::DownEvent, seq: Serial) {
todo!()
}
fn up(&self, seat: &Seat<State>, data: &mut State, event: &touch::UpEvent, seq: Serial) {
todo!()
}
fn motion(
&self,
seat: &Seat<State>,
data: &mut State,
event: &touch::MotionEvent,
seq: Serial,
) {
todo!()
}
fn frame(&self, seat: &Seat<State>, data: &mut State, seq: Serial) {
todo!()
}
fn cancel(&self, seat: &Seat<State>, data: &mut State, seq: Serial) {
todo!()
}
fn shape(&self, seat: &Seat<State>, data: &mut State, event: &touch::ShapeEvent, seq: Serial) {
todo!()
}
fn orientation(
&self,
seat: &Seat<State>,
data: &mut State,
event: &touch::OrientationEvent,
seq: Serial,
) {
todo!()
}
}
impl WaylandFocus for FocusTarget {
fn wl_surface(&self) -> Option<WlSurface> {
match self {
FocusTarget::Window(window) => window.wl_surface(),
FocusTarget::Popup(popup) => Some(popup.wl_surface().clone()),
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => Some(surf.wl_surface().clone()),
}
}
fn same_client_as(
&self,
object_id: &smithay::reexports::wayland_server::backend::ObjectId,
) -> bool {
match self {
FocusTarget::Window(window) => window.same_client_as(object_id),
FocusTarget::Popup(popup) => popup.wl_surface().id().same_client_as(object_id),
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => surf.wl_surface().id().same_client_as(object_id),
}
}
}
impl From<WindowElement> for FocusTarget {
fn from(value: WindowElement) -> Self {
FocusTarget::Window(value)
}
}
impl From<PopupKind> for FocusTarget {
fn from(value: PopupKind) -> Self {
FocusTarget::Popup(value)
}
}
2023-08-04 18:48:10 -05:00
impl From<LayerSurface> for FocusTarget {
fn from(value: LayerSurface) -> Self {
FocusTarget::LayerSurface(value)
}
}