smithay_client_toolkit/shell/xdg/window/
inner.rs1use std::{
2 convert::{TryFrom, TryInto},
3 num::NonZeroU32,
4 sync::Mutex,
5};
6
7use wayland_client::{Connection, QueueHandle};
8use wayland_protocols::{
9 xdg::decoration::zv1::client::{
10 zxdg_decoration_manager_v1,
11 zxdg_toplevel_decoration_v1::{self, Mode},
12 },
13 xdg::shell::client::{
14 xdg_surface,
15 xdg_toplevel::{self, State, WmCapabilities},
16 },
17};
18
19use crate::{
20 dispatch2::Dispatch2,
21 error::GlobalError,
22 globals::{GlobalData, ProvidesBoundGlobal},
23 shell::xdg::{XdgShell, XdgShellSurface},
24};
25
26use super::{
27 DecorationMode, Window, WindowConfigure, WindowData, WindowHandler, WindowManagerCapabilities,
28 WindowState,
29};
30
31impl Drop for WindowInner {
32 fn drop(&mut self) {
33 if let Some(toplevel_decoration) = self.toplevel_decoration.as_ref() {
35 toplevel_decoration.destroy();
36 }
37
38 self.xdg_toplevel.destroy();
40 }
43}
44
45#[derive(Debug)]
46pub struct WindowInner {
47 pub xdg_surface: XdgShellSurface,
48 pub xdg_toplevel: xdg_toplevel::XdgToplevel,
49 pub toplevel_decoration: Option<zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1>,
50 pub pending_configure: Mutex<WindowConfigure>,
51}
52
53impl ProvidesBoundGlobal<zxdg_decoration_manager_v1::ZxdgDecorationManagerV1, 1> for XdgShell {
54 fn bound_global(
55 &self,
56 ) -> Result<zxdg_decoration_manager_v1::ZxdgDecorationManagerV1, GlobalError> {
57 self.xdg_decoration_manager.get().cloned()
58 }
59}
60
61impl<D> Dispatch2<xdg_surface::XdgSurface, D> for WindowData
62where
63 D: WindowHandler,
64{
65 fn event(
66 &self,
67 data: &mut D,
68 xdg_surface: &xdg_surface::XdgSurface,
69 event: xdg_surface::Event,
70 conn: &Connection,
71 qh: &QueueHandle<D>,
72 ) {
73 if let Some(window) = Window::from_xdg_surface(xdg_surface) {
74 match event {
75 xdg_surface::Event::Configure { serial } => {
76 xdg_surface.ack_configure(serial);
78
79 let configure = { window.0.pending_configure.lock().unwrap().clone() };
80 WindowHandler::configure(data, conn, qh, &window, configure, serial);
81 }
82
83 _ => unreachable!(),
84 }
85 }
86 }
87}
88
89impl<D> Dispatch2<xdg_toplevel::XdgToplevel, D> for WindowData
90where
91 D: WindowHandler,
92{
93 fn event(
94 &self,
95 data: &mut D,
96 toplevel: &xdg_toplevel::XdgToplevel,
97 event: xdg_toplevel::Event,
98 conn: &Connection,
99 qh: &QueueHandle<D>,
100 ) {
101 if let Some(window) = Window::from_xdg_toplevel(toplevel) {
102 match event {
103 xdg_toplevel::Event::Configure { width, height, states } => {
104 let new_state = states
107 .chunks_exact(4)
108 .flat_map(TryInto::<[u8; 4]>::try_into)
109 .map(u32::from_ne_bytes)
110 .flat_map(State::try_from)
111 .fold(WindowState::empty(), |mut acc, state| {
112 match state {
113 State::Maximized => acc.set(WindowState::MAXIMIZED, true),
114 State::Fullscreen => acc.set(WindowState::FULLSCREEN, true),
115 State::Resizing => acc.set(WindowState::RESIZING, true),
116 State::Activated => acc.set(WindowState::ACTIVATED, true),
117 State::TiledLeft => acc.set(WindowState::TILED_LEFT, true),
118 State::TiledRight => acc.set(WindowState::TILED_RIGHT, true),
119 State::TiledTop => acc.set(WindowState::TILED_TOP, true),
120 State::TiledBottom => acc.set(WindowState::TILED_BOTTOM, true),
121 State::Suspended => acc.set(WindowState::SUSPENDED, true),
122 _ => (),
123 }
124 acc
125 });
126
127 let width = u32::try_from(width).ok().and_then(NonZeroU32::new);
130 let height = u32::try_from(height).ok().and_then(NonZeroU32::new);
131
132 let pending_configure = &mut window.0.pending_configure.lock().unwrap();
133 pending_configure.new_size = (width, height);
134 pending_configure.state = new_state;
135 }
136
137 xdg_toplevel::Event::Close => {
138 data.request_close(conn, qh, &window);
139 }
140
141 xdg_toplevel::Event::ConfigureBounds { width, height } => {
142 let pending_configure = &mut window.0.pending_configure.lock().unwrap();
143 if width == 0 && height == 0 {
144 pending_configure.suggested_bounds = None;
145 } else {
146 pending_configure.suggested_bounds = Some((width as u32, height as u32));
147 }
148 }
149 xdg_toplevel::Event::WmCapabilities { capabilities } => {
150 let pending_configure = &mut window.0.pending_configure.lock().unwrap();
151 pending_configure.capabilities = capabilities
152 .chunks_exact(4)
153 .flat_map(TryInto::<[u8; 4]>::try_into)
154 .map(u32::from_ne_bytes)
155 .flat_map(WmCapabilities::try_from)
156 .fold(WindowManagerCapabilities::empty(), |mut acc, capability| {
157 match capability {
158 WmCapabilities::WindowMenu => {
159 acc.set(WindowManagerCapabilities::WINDOW_MENU, true)
160 }
161 WmCapabilities::Maximize => {
162 acc.set(WindowManagerCapabilities::MAXIMIZE, true)
163 }
164 WmCapabilities::Fullscreen => {
165 acc.set(WindowManagerCapabilities::FULLSCREEN, true)
166 }
167 WmCapabilities::Minimize => {
168 acc.set(WindowManagerCapabilities::MINIMIZE, true)
169 }
170 _ => (),
171 }
172 acc
173 });
174 }
175 _ => unreachable!(),
176 }
177 }
178 }
179}
180
181impl<D> Dispatch2<zxdg_decoration_manager_v1::ZxdgDecorationManagerV1, D> for GlobalData
184where
185 D: WindowHandler,
186{
187 fn event(
188 &self,
189 _: &mut D,
190 _: &zxdg_decoration_manager_v1::ZxdgDecorationManagerV1,
191 _: zxdg_decoration_manager_v1::Event,
192 _: &Connection,
193 _: &QueueHandle<D>,
194 ) {
195 unreachable!("zxdg_decoration_manager_v1 has no events")
196 }
197}
198
199impl<D> Dispatch2<zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1, D> for WindowData
200where
201 D: WindowHandler,
202{
203 fn event(
204 &self,
205 _: &mut D,
206 decoration: &zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1,
207 event: zxdg_toplevel_decoration_v1::Event,
208 _: &Connection,
209 _: &QueueHandle<D>,
210 ) {
211 if let Some(window) = Window::from_toplevel_decoration(decoration) {
212 match event {
213 zxdg_toplevel_decoration_v1::Event::Configure { mode } => match mode {
214 wayland_client::WEnum::Value(mode) => {
215 let mode = match mode {
216 Mode::ClientSide => DecorationMode::Client,
217 Mode::ServerSide => DecorationMode::Server,
218
219 _ => unreachable!(),
220 };
221
222 window.0.pending_configure.lock().unwrap().decoration_mode = mode;
223 }
224
225 wayland_client::WEnum::Unknown(unknown) => {
226 log::error!(target: "sctk", "unknown decoration mode 0x{:x}", unknown);
227 }
228 },
229
230 _ => unreachable!(),
231 }
232 }
233 }
234}