1 | // This file contains generated code. Do not edit directly. |
2 | // To regenerate this, run 'make'. |
3 | |
4 | //! Bindings to the core X11 protocol. |
5 | //! |
6 | //! For more documentation on the X11 protocol, see the |
7 | //! [protocol reference manual](https://www.x.org/releases/X11R7.6/doc/xproto/x11protocol.html). |
8 | //! This is especially recommended for looking up the exact semantics of |
9 | //! specific errors, events, or requests. |
10 | |
11 | #![allow (clippy::too_many_arguments)] |
12 | // The code generator is simpler if it can always use conversions |
13 | #![allow (clippy::useless_conversion)] |
14 | |
15 | #[allow (unused_imports)] |
16 | use alloc::borrow::Cow; |
17 | #[allow (unused_imports)] |
18 | use core::convert::TryInto; |
19 | use alloc::vec; |
20 | use alloc::vec::Vec; |
21 | use core::convert::TryFrom; |
22 | use crate::errors::ParseError; |
23 | #[allow (unused_imports)] |
24 | use crate::x11_utils::TryIntoUSize; |
25 | use crate::{BufWithFds, PiecewiseBuf}; |
26 | #[allow (unused_imports)] |
27 | use crate::utils::{RawFdContainer, pretty_print_bitmask, pretty_print_enum}; |
28 | #[allow (unused_imports)] |
29 | use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd}; |
30 | |
31 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
32 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
33 | pub struct Char2b { |
34 | pub byte1: u8, |
35 | pub byte2: u8, |
36 | } |
37 | impl TryParse for Char2b { |
38 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
39 | let (byte1: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
40 | let (byte2: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
41 | let result: Char2b = Char2b { byte1, byte2 }; |
42 | Ok((result, remaining)) |
43 | } |
44 | } |
45 | impl Serialize for Char2b { |
46 | type Bytes = [u8; 2]; |
47 | fn serialize(&self) -> [u8; 2] { |
48 | let byte1_bytes: [u8; 1] = self.byte1.serialize(); |
49 | let byte2_bytes: [u8; 1] = self.byte2.serialize(); |
50 | [ |
51 | byte1_bytes[0], |
52 | byte2_bytes[0], |
53 | ] |
54 | } |
55 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
56 | bytes.reserve(additional:2); |
57 | self.byte1.serialize_into(bytes); |
58 | self.byte2.serialize_into(bytes); |
59 | } |
60 | } |
61 | |
62 | pub type Window = u32; |
63 | |
64 | pub type Pixmap = u32; |
65 | |
66 | pub type Cursor = u32; |
67 | |
68 | pub type Font = u32; |
69 | |
70 | pub type Gcontext = u32; |
71 | |
72 | pub type Colormap = u32; |
73 | |
74 | pub type Atom = u32; |
75 | |
76 | pub type Drawable = u32; |
77 | |
78 | pub type Fontable = u32; |
79 | |
80 | pub type Bool32 = u32; |
81 | |
82 | pub type Visualid = u32; |
83 | |
84 | pub type Timestamp = u32; |
85 | |
86 | pub type Keysym = u32; |
87 | |
88 | pub type Keycode = u8; |
89 | |
90 | pub type Keycode32 = u32; |
91 | |
92 | pub type Button = u8; |
93 | |
94 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
95 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
96 | pub struct Point { |
97 | pub x: i16, |
98 | pub y: i16, |
99 | } |
100 | impl TryParse for Point { |
101 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
102 | let (x: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
103 | let (y: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
104 | let result: Point = Point { x, y }; |
105 | Ok((result, remaining)) |
106 | } |
107 | } |
108 | impl Serialize for Point { |
109 | type Bytes = [u8; 4]; |
110 | fn serialize(&self) -> [u8; 4] { |
111 | let x_bytes: [u8; 2] = self.x.serialize(); |
112 | let y_bytes: [u8; 2] = self.y.serialize(); |
113 | [ |
114 | x_bytes[0], |
115 | x_bytes[1], |
116 | y_bytes[0], |
117 | y_bytes[1], |
118 | ] |
119 | } |
120 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
121 | bytes.reserve(additional:4); |
122 | self.x.serialize_into(bytes); |
123 | self.y.serialize_into(bytes); |
124 | } |
125 | } |
126 | |
127 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
128 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
129 | pub struct Rectangle { |
130 | pub x: i16, |
131 | pub y: i16, |
132 | pub width: u16, |
133 | pub height: u16, |
134 | } |
135 | impl TryParse for Rectangle { |
136 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
137 | let (x: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
138 | let (y: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
139 | let (width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
140 | let (height: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
141 | let result: Rectangle = Rectangle { x, y, width, height }; |
142 | Ok((result, remaining)) |
143 | } |
144 | } |
145 | impl Serialize for Rectangle { |
146 | type Bytes = [u8; 8]; |
147 | fn serialize(&self) -> [u8; 8] { |
148 | let x_bytes = self.x.serialize(); |
149 | let y_bytes = self.y.serialize(); |
150 | let width_bytes = self.width.serialize(); |
151 | let height_bytes = self.height.serialize(); |
152 | [ |
153 | x_bytes[0], |
154 | x_bytes[1], |
155 | y_bytes[0], |
156 | y_bytes[1], |
157 | width_bytes[0], |
158 | width_bytes[1], |
159 | height_bytes[0], |
160 | height_bytes[1], |
161 | ] |
162 | } |
163 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
164 | bytes.reserve(8); |
165 | self.x.serialize_into(bytes); |
166 | self.y.serialize_into(bytes); |
167 | self.width.serialize_into(bytes); |
168 | self.height.serialize_into(bytes); |
169 | } |
170 | } |
171 | |
172 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
173 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
174 | pub struct Arc { |
175 | pub x: i16, |
176 | pub y: i16, |
177 | pub width: u16, |
178 | pub height: u16, |
179 | pub angle1: i16, |
180 | pub angle2: i16, |
181 | } |
182 | impl TryParse for Arc { |
183 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
184 | let (x: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
185 | let (y: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
186 | let (width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
187 | let (height: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
188 | let (angle1: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
189 | let (angle2: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
190 | let result: Arc = Arc { x, y, width, height, angle1, angle2 }; |
191 | Ok((result, remaining)) |
192 | } |
193 | } |
194 | impl Serialize for Arc { |
195 | type Bytes = [u8; 12]; |
196 | fn serialize(&self) -> [u8; 12] { |
197 | let x_bytes = self.x.serialize(); |
198 | let y_bytes = self.y.serialize(); |
199 | let width_bytes = self.width.serialize(); |
200 | let height_bytes = self.height.serialize(); |
201 | let angle1_bytes = self.angle1.serialize(); |
202 | let angle2_bytes = self.angle2.serialize(); |
203 | [ |
204 | x_bytes[0], |
205 | x_bytes[1], |
206 | y_bytes[0], |
207 | y_bytes[1], |
208 | width_bytes[0], |
209 | width_bytes[1], |
210 | height_bytes[0], |
211 | height_bytes[1], |
212 | angle1_bytes[0], |
213 | angle1_bytes[1], |
214 | angle2_bytes[0], |
215 | angle2_bytes[1], |
216 | ] |
217 | } |
218 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
219 | bytes.reserve(12); |
220 | self.x.serialize_into(bytes); |
221 | self.y.serialize_into(bytes); |
222 | self.width.serialize_into(bytes); |
223 | self.height.serialize_into(bytes); |
224 | self.angle1.serialize_into(bytes); |
225 | self.angle2.serialize_into(bytes); |
226 | } |
227 | } |
228 | |
229 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
230 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
231 | pub struct Format { |
232 | pub depth: u8, |
233 | pub bits_per_pixel: u8, |
234 | pub scanline_pad: u8, |
235 | } |
236 | impl TryParse for Format { |
237 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
238 | let (depth: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
239 | let (bits_per_pixel: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
240 | let (scanline_pad: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
241 | let remaining: &[u8] = remaining.get(5..).ok_or(err:ParseError::InsufficientData)?; |
242 | let result: Format = Format { depth, bits_per_pixel, scanline_pad }; |
243 | Ok((result, remaining)) |
244 | } |
245 | } |
246 | impl Serialize for Format { |
247 | type Bytes = [u8; 8]; |
248 | fn serialize(&self) -> [u8; 8] { |
249 | let depth_bytes = self.depth.serialize(); |
250 | let bits_per_pixel_bytes = self.bits_per_pixel.serialize(); |
251 | let scanline_pad_bytes = self.scanline_pad.serialize(); |
252 | [ |
253 | depth_bytes[0], |
254 | bits_per_pixel_bytes[0], |
255 | scanline_pad_bytes[0], |
256 | 0, |
257 | 0, |
258 | 0, |
259 | 0, |
260 | 0, |
261 | ] |
262 | } |
263 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
264 | bytes.reserve(8); |
265 | self.depth.serialize_into(bytes); |
266 | self.bits_per_pixel.serialize_into(bytes); |
267 | self.scanline_pad.serialize_into(bytes); |
268 | bytes.extend_from_slice(&[0; 5]); |
269 | } |
270 | } |
271 | |
272 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
273 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
274 | pub struct VisualClass(u8); |
275 | impl VisualClass { |
276 | pub const STATIC_GRAY: Self = Self(0); |
277 | pub const GRAY_SCALE: Self = Self(1); |
278 | pub const STATIC_COLOR: Self = Self(2); |
279 | pub const PSEUDO_COLOR: Self = Self(3); |
280 | pub const TRUE_COLOR: Self = Self(4); |
281 | pub const DIRECT_COLOR: Self = Self(5); |
282 | } |
283 | impl From<VisualClass> for u8 { |
284 | #[inline ] |
285 | fn from(input: VisualClass) -> Self { |
286 | input.0 |
287 | } |
288 | } |
289 | impl From<VisualClass> for Option<u8> { |
290 | #[inline ] |
291 | fn from(input: VisualClass) -> Self { |
292 | Some(input.0) |
293 | } |
294 | } |
295 | impl From<VisualClass> for u16 { |
296 | #[inline ] |
297 | fn from(input: VisualClass) -> Self { |
298 | u16::from(input.0) |
299 | } |
300 | } |
301 | impl From<VisualClass> for Option<u16> { |
302 | #[inline ] |
303 | fn from(input: VisualClass) -> Self { |
304 | Some(u16::from(input.0)) |
305 | } |
306 | } |
307 | impl From<VisualClass> for u32 { |
308 | #[inline ] |
309 | fn from(input: VisualClass) -> Self { |
310 | u32::from(input.0) |
311 | } |
312 | } |
313 | impl From<VisualClass> for Option<u32> { |
314 | #[inline ] |
315 | fn from(input: VisualClass) -> Self { |
316 | Some(u32::from(input.0)) |
317 | } |
318 | } |
319 | impl From<u8> for VisualClass { |
320 | #[inline ] |
321 | fn from(value: u8) -> Self { |
322 | Self(value) |
323 | } |
324 | } |
325 | impl core::fmt::Debug for VisualClass { |
326 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
327 | let variants: [(u32, &str, &str); 6] = [ |
328 | (Self::STATIC_GRAY.0.into(), "STATIC_GRAY" , "StaticGray" ), |
329 | (Self::GRAY_SCALE.0.into(), "GRAY_SCALE" , "GrayScale" ), |
330 | (Self::STATIC_COLOR.0.into(), "STATIC_COLOR" , "StaticColor" ), |
331 | (Self::PSEUDO_COLOR.0.into(), "PSEUDO_COLOR" , "PseudoColor" ), |
332 | (Self::TRUE_COLOR.0.into(), "TRUE_COLOR" , "TrueColor" ), |
333 | (Self::DIRECT_COLOR.0.into(), "DIRECT_COLOR" , "DirectColor" ), |
334 | ]; |
335 | pretty_print_enum(fmt, self.0.into(), &variants) |
336 | } |
337 | } |
338 | |
339 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
340 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
341 | pub struct Visualtype { |
342 | pub visual_id: Visualid, |
343 | pub class: VisualClass, |
344 | pub bits_per_rgb_value: u8, |
345 | pub colormap_entries: u16, |
346 | pub red_mask: u32, |
347 | pub green_mask: u32, |
348 | pub blue_mask: u32, |
349 | } |
350 | impl TryParse for Visualtype { |
351 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
352 | let (visual_id: u32, remaining: &[u8]) = Visualid::try_parse(remaining)?; |
353 | let (class: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
354 | let (bits_per_rgb_value: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
355 | let (colormap_entries: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
356 | let (red_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
357 | let (green_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
358 | let (blue_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
359 | let remaining: &[u8] = remaining.get(4..).ok_or(err:ParseError::InsufficientData)?; |
360 | let class: VisualClass = class.into(); |
361 | let result: Visualtype = Visualtype { visual_id, class, bits_per_rgb_value, colormap_entries, red_mask, green_mask, blue_mask }; |
362 | Ok((result, remaining)) |
363 | } |
364 | } |
365 | impl Serialize for Visualtype { |
366 | type Bytes = [u8; 24]; |
367 | fn serialize(&self) -> [u8; 24] { |
368 | let visual_id_bytes = self.visual_id.serialize(); |
369 | let class_bytes = u8::from(self.class).serialize(); |
370 | let bits_per_rgb_value_bytes = self.bits_per_rgb_value.serialize(); |
371 | let colormap_entries_bytes = self.colormap_entries.serialize(); |
372 | let red_mask_bytes = self.red_mask.serialize(); |
373 | let green_mask_bytes = self.green_mask.serialize(); |
374 | let blue_mask_bytes = self.blue_mask.serialize(); |
375 | [ |
376 | visual_id_bytes[0], |
377 | visual_id_bytes[1], |
378 | visual_id_bytes[2], |
379 | visual_id_bytes[3], |
380 | class_bytes[0], |
381 | bits_per_rgb_value_bytes[0], |
382 | colormap_entries_bytes[0], |
383 | colormap_entries_bytes[1], |
384 | red_mask_bytes[0], |
385 | red_mask_bytes[1], |
386 | red_mask_bytes[2], |
387 | red_mask_bytes[3], |
388 | green_mask_bytes[0], |
389 | green_mask_bytes[1], |
390 | green_mask_bytes[2], |
391 | green_mask_bytes[3], |
392 | blue_mask_bytes[0], |
393 | blue_mask_bytes[1], |
394 | blue_mask_bytes[2], |
395 | blue_mask_bytes[3], |
396 | 0, |
397 | 0, |
398 | 0, |
399 | 0, |
400 | ] |
401 | } |
402 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
403 | bytes.reserve(24); |
404 | self.visual_id.serialize_into(bytes); |
405 | u8::from(self.class).serialize_into(bytes); |
406 | self.bits_per_rgb_value.serialize_into(bytes); |
407 | self.colormap_entries.serialize_into(bytes); |
408 | self.red_mask.serialize_into(bytes); |
409 | self.green_mask.serialize_into(bytes); |
410 | self.blue_mask.serialize_into(bytes); |
411 | bytes.extend_from_slice(&[0; 4]); |
412 | } |
413 | } |
414 | |
415 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
416 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
417 | pub struct Depth { |
418 | pub depth: u8, |
419 | pub visuals: Vec<Visualtype>, |
420 | } |
421 | impl TryParse for Depth { |
422 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
423 | let (depth: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
424 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
425 | let (visuals_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
426 | let remaining: &[u8] = remaining.get(4..).ok_or(err:ParseError::InsufficientData)?; |
427 | let (visuals: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<Visualtype>(data:remaining, list_length:visuals_len.try_to_usize()?)?; |
428 | let result: Depth = Depth { depth, visuals }; |
429 | Ok((result, remaining)) |
430 | } |
431 | } |
432 | impl Serialize for Depth { |
433 | type Bytes = Vec<u8>; |
434 | fn serialize(&self) -> Vec<u8> { |
435 | let mut result: Vec = Vec::new(); |
436 | self.serialize_into(&mut result); |
437 | result |
438 | } |
439 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
440 | bytes.reserve(additional:8); |
441 | self.depth.serialize_into(bytes); |
442 | bytes.extend_from_slice(&[0; 1]); |
443 | let visuals_len: u16 = u16::try_from(self.visuals.len()).expect(msg:"`visuals` has too many elements" ); |
444 | visuals_len.serialize_into(bytes); |
445 | bytes.extend_from_slice(&[0; 4]); |
446 | self.visuals.serialize_into(bytes); |
447 | } |
448 | } |
449 | impl Depth { |
450 | /// Get the value of the `visuals_len` field. |
451 | /// |
452 | /// The `visuals_len` field is used as the length field of the `visuals` field. |
453 | /// This function computes the field's value again based on the length of the list. |
454 | /// |
455 | /// # Panics |
456 | /// |
457 | /// Panics if the value cannot be represented in the target type. This |
458 | /// cannot happen with values of the struct received from the X11 server. |
459 | pub fn visuals_len(&self) -> u16 { |
460 | self.visuals.len() |
461 | .try_into().unwrap() |
462 | } |
463 | } |
464 | |
465 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
466 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
467 | pub struct EventMask(u32); |
468 | impl EventMask { |
469 | pub const NO_EVENT: Self = Self(0); |
470 | pub const KEY_PRESS: Self = Self(1 << 0); |
471 | pub const KEY_RELEASE: Self = Self(1 << 1); |
472 | pub const BUTTON_PRESS: Self = Self(1 << 2); |
473 | pub const BUTTON_RELEASE: Self = Self(1 << 3); |
474 | pub const ENTER_WINDOW: Self = Self(1 << 4); |
475 | pub const LEAVE_WINDOW: Self = Self(1 << 5); |
476 | pub const POINTER_MOTION: Self = Self(1 << 6); |
477 | pub const POINTER_MOTION_HINT: Self = Self(1 << 7); |
478 | pub const BUTTON1_MOTION: Self = Self(1 << 8); |
479 | pub const BUTTON2_MOTION: Self = Self(1 << 9); |
480 | pub const BUTTON3_MOTION: Self = Self(1 << 10); |
481 | pub const BUTTON4_MOTION: Self = Self(1 << 11); |
482 | pub const BUTTON5_MOTION: Self = Self(1 << 12); |
483 | pub const BUTTON_MOTION: Self = Self(1 << 13); |
484 | pub const KEYMAP_STATE: Self = Self(1 << 14); |
485 | pub const EXPOSURE: Self = Self(1 << 15); |
486 | pub const VISIBILITY_CHANGE: Self = Self(1 << 16); |
487 | pub const STRUCTURE_NOTIFY: Self = Self(1 << 17); |
488 | pub const RESIZE_REDIRECT: Self = Self(1 << 18); |
489 | pub const SUBSTRUCTURE_NOTIFY: Self = Self(1 << 19); |
490 | pub const SUBSTRUCTURE_REDIRECT: Self = Self(1 << 20); |
491 | pub const FOCUS_CHANGE: Self = Self(1 << 21); |
492 | pub const PROPERTY_CHANGE: Self = Self(1 << 22); |
493 | pub const COLOR_MAP_CHANGE: Self = Self(1 << 23); |
494 | pub const OWNER_GRAB_BUTTON: Self = Self(1 << 24); |
495 | } |
496 | impl From<EventMask> for u32 { |
497 | #[inline ] |
498 | fn from(input: EventMask) -> Self { |
499 | input.0 |
500 | } |
501 | } |
502 | impl From<EventMask> for Option<u32> { |
503 | #[inline ] |
504 | fn from(input: EventMask) -> Self { |
505 | Some(input.0) |
506 | } |
507 | } |
508 | impl From<u8> for EventMask { |
509 | #[inline ] |
510 | fn from(value: u8) -> Self { |
511 | Self(value.into()) |
512 | } |
513 | } |
514 | impl From<u16> for EventMask { |
515 | #[inline ] |
516 | fn from(value: u16) -> Self { |
517 | Self(value.into()) |
518 | } |
519 | } |
520 | impl From<u32> for EventMask { |
521 | #[inline ] |
522 | fn from(value: u32) -> Self { |
523 | Self(value) |
524 | } |
525 | } |
526 | impl core::fmt::Debug for EventMask { |
527 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
528 | let variants = [ |
529 | (Self::NO_EVENT.0, "NO_EVENT" , "NoEvent" ), |
530 | (Self::KEY_PRESS.0, "KEY_PRESS" , "KeyPress" ), |
531 | (Self::KEY_RELEASE.0, "KEY_RELEASE" , "KeyRelease" ), |
532 | (Self::BUTTON_PRESS.0, "BUTTON_PRESS" , "ButtonPress" ), |
533 | (Self::BUTTON_RELEASE.0, "BUTTON_RELEASE" , "ButtonRelease" ), |
534 | (Self::ENTER_WINDOW.0, "ENTER_WINDOW" , "EnterWindow" ), |
535 | (Self::LEAVE_WINDOW.0, "LEAVE_WINDOW" , "LeaveWindow" ), |
536 | (Self::POINTER_MOTION.0, "POINTER_MOTION" , "PointerMotion" ), |
537 | (Self::POINTER_MOTION_HINT.0, "POINTER_MOTION_HINT" , "PointerMotionHint" ), |
538 | (Self::BUTTON1_MOTION.0, "BUTTON1_MOTION" , "Button1Motion" ), |
539 | (Self::BUTTON2_MOTION.0, "BUTTON2_MOTION" , "Button2Motion" ), |
540 | (Self::BUTTON3_MOTION.0, "BUTTON3_MOTION" , "Button3Motion" ), |
541 | (Self::BUTTON4_MOTION.0, "BUTTON4_MOTION" , "Button4Motion" ), |
542 | (Self::BUTTON5_MOTION.0, "BUTTON5_MOTION" , "Button5Motion" ), |
543 | (Self::BUTTON_MOTION.0, "BUTTON_MOTION" , "ButtonMotion" ), |
544 | (Self::KEYMAP_STATE.0, "KEYMAP_STATE" , "KeymapState" ), |
545 | (Self::EXPOSURE.0, "EXPOSURE" , "Exposure" ), |
546 | (Self::VISIBILITY_CHANGE.0, "VISIBILITY_CHANGE" , "VisibilityChange" ), |
547 | (Self::STRUCTURE_NOTIFY.0, "STRUCTURE_NOTIFY" , "StructureNotify" ), |
548 | (Self::RESIZE_REDIRECT.0, "RESIZE_REDIRECT" , "ResizeRedirect" ), |
549 | (Self::SUBSTRUCTURE_NOTIFY.0, "SUBSTRUCTURE_NOTIFY" , "SubstructureNotify" ), |
550 | (Self::SUBSTRUCTURE_REDIRECT.0, "SUBSTRUCTURE_REDIRECT" , "SubstructureRedirect" ), |
551 | (Self::FOCUS_CHANGE.0, "FOCUS_CHANGE" , "FocusChange" ), |
552 | (Self::PROPERTY_CHANGE.0, "PROPERTY_CHANGE" , "PropertyChange" ), |
553 | (Self::COLOR_MAP_CHANGE.0, "COLOR_MAP_CHANGE" , "ColorMapChange" ), |
554 | (Self::OWNER_GRAB_BUTTON.0, "OWNER_GRAB_BUTTON" , "OwnerGrabButton" ), |
555 | ]; |
556 | pretty_print_bitmask(fmt, self.0, &variants) |
557 | } |
558 | } |
559 | bitmask_binop!(EventMask, u32); |
560 | |
561 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
562 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
563 | pub struct BackingStore(u32); |
564 | impl BackingStore { |
565 | pub const NOT_USEFUL: Self = Self(0); |
566 | pub const WHEN_MAPPED: Self = Self(1); |
567 | pub const ALWAYS: Self = Self(2); |
568 | } |
569 | impl From<BackingStore> for u32 { |
570 | #[inline ] |
571 | fn from(input: BackingStore) -> Self { |
572 | input.0 |
573 | } |
574 | } |
575 | impl From<BackingStore> for Option<u32> { |
576 | #[inline ] |
577 | fn from(input: BackingStore) -> Self { |
578 | Some(input.0) |
579 | } |
580 | } |
581 | impl From<u8> for BackingStore { |
582 | #[inline ] |
583 | fn from(value: u8) -> Self { |
584 | Self(value.into()) |
585 | } |
586 | } |
587 | impl From<u16> for BackingStore { |
588 | #[inline ] |
589 | fn from(value: u16) -> Self { |
590 | Self(value.into()) |
591 | } |
592 | } |
593 | impl From<u32> for BackingStore { |
594 | #[inline ] |
595 | fn from(value: u32) -> Self { |
596 | Self(value) |
597 | } |
598 | } |
599 | impl core::fmt::Debug for BackingStore { |
600 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
601 | let variants: [(u32, &str, &str); 3] = [ |
602 | (Self::NOT_USEFUL.0, "NOT_USEFUL" , "NotUseful" ), |
603 | (Self::WHEN_MAPPED.0, "WHEN_MAPPED" , "WhenMapped" ), |
604 | (Self::ALWAYS.0, "ALWAYS" , "Always" ), |
605 | ]; |
606 | pretty_print_enum(fmt, self.0, &variants) |
607 | } |
608 | } |
609 | |
610 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
611 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
612 | pub struct Screen { |
613 | pub root: Window, |
614 | pub default_colormap: Colormap, |
615 | pub white_pixel: u32, |
616 | pub black_pixel: u32, |
617 | pub current_input_masks: EventMask, |
618 | pub width_in_pixels: u16, |
619 | pub height_in_pixels: u16, |
620 | pub width_in_millimeters: u16, |
621 | pub height_in_millimeters: u16, |
622 | pub min_installed_maps: u16, |
623 | pub max_installed_maps: u16, |
624 | pub root_visual: Visualid, |
625 | pub backing_stores: BackingStore, |
626 | pub save_unders: bool, |
627 | pub root_depth: u8, |
628 | pub allowed_depths: Vec<Depth>, |
629 | } |
630 | impl TryParse for Screen { |
631 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
632 | let (root, remaining) = Window::try_parse(remaining)?; |
633 | let (default_colormap, remaining) = Colormap::try_parse(remaining)?; |
634 | let (white_pixel, remaining) = u32::try_parse(remaining)?; |
635 | let (black_pixel, remaining) = u32::try_parse(remaining)?; |
636 | let (current_input_masks, remaining) = u32::try_parse(remaining)?; |
637 | let (width_in_pixels, remaining) = u16::try_parse(remaining)?; |
638 | let (height_in_pixels, remaining) = u16::try_parse(remaining)?; |
639 | let (width_in_millimeters, remaining) = u16::try_parse(remaining)?; |
640 | let (height_in_millimeters, remaining) = u16::try_parse(remaining)?; |
641 | let (min_installed_maps, remaining) = u16::try_parse(remaining)?; |
642 | let (max_installed_maps, remaining) = u16::try_parse(remaining)?; |
643 | let (root_visual, remaining) = Visualid::try_parse(remaining)?; |
644 | let (backing_stores, remaining) = u8::try_parse(remaining)?; |
645 | let (save_unders, remaining) = bool::try_parse(remaining)?; |
646 | let (root_depth, remaining) = u8::try_parse(remaining)?; |
647 | let (allowed_depths_len, remaining) = u8::try_parse(remaining)?; |
648 | let (allowed_depths, remaining) = crate::x11_utils::parse_list::<Depth>(remaining, allowed_depths_len.try_to_usize()?)?; |
649 | let current_input_masks = current_input_masks.into(); |
650 | let backing_stores = backing_stores.into(); |
651 | let result = Screen { root, default_colormap, white_pixel, black_pixel, current_input_masks, width_in_pixels, height_in_pixels, width_in_millimeters, height_in_millimeters, min_installed_maps, max_installed_maps, root_visual, backing_stores, save_unders, root_depth, allowed_depths }; |
652 | Ok((result, remaining)) |
653 | } |
654 | } |
655 | impl Serialize for Screen { |
656 | type Bytes = Vec<u8>; |
657 | fn serialize(&self) -> Vec<u8> { |
658 | let mut result = Vec::new(); |
659 | self.serialize_into(&mut result); |
660 | result |
661 | } |
662 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
663 | bytes.reserve(40); |
664 | self.root.serialize_into(bytes); |
665 | self.default_colormap.serialize_into(bytes); |
666 | self.white_pixel.serialize_into(bytes); |
667 | self.black_pixel.serialize_into(bytes); |
668 | u32::from(self.current_input_masks).serialize_into(bytes); |
669 | self.width_in_pixels.serialize_into(bytes); |
670 | self.height_in_pixels.serialize_into(bytes); |
671 | self.width_in_millimeters.serialize_into(bytes); |
672 | self.height_in_millimeters.serialize_into(bytes); |
673 | self.min_installed_maps.serialize_into(bytes); |
674 | self.max_installed_maps.serialize_into(bytes); |
675 | self.root_visual.serialize_into(bytes); |
676 | (u32::from(self.backing_stores) as u8).serialize_into(bytes); |
677 | self.save_unders.serialize_into(bytes); |
678 | self.root_depth.serialize_into(bytes); |
679 | let allowed_depths_len = u8::try_from(self.allowed_depths.len()).expect("`allowed_depths` has too many elements" ); |
680 | allowed_depths_len.serialize_into(bytes); |
681 | self.allowed_depths.serialize_into(bytes); |
682 | } |
683 | } |
684 | impl Screen { |
685 | /// Get the value of the `allowed_depths_len` field. |
686 | /// |
687 | /// The `allowed_depths_len` field is used as the length field of the `allowed_depths` field. |
688 | /// This function computes the field's value again based on the length of the list. |
689 | /// |
690 | /// # Panics |
691 | /// |
692 | /// Panics if the value cannot be represented in the target type. This |
693 | /// cannot happen with values of the struct received from the X11 server. |
694 | pub fn allowed_depths_len(&self) -> u8 { |
695 | self.allowed_depths.len() |
696 | .try_into().unwrap() |
697 | } |
698 | } |
699 | |
700 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
701 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
702 | pub struct SetupRequest { |
703 | pub byte_order: u8, |
704 | pub protocol_major_version: u16, |
705 | pub protocol_minor_version: u16, |
706 | pub authorization_protocol_name: Vec<u8>, |
707 | pub authorization_protocol_data: Vec<u8>, |
708 | } |
709 | impl TryParse for SetupRequest { |
710 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
711 | let value = remaining; |
712 | let (byte_order, remaining) = u8::try_parse(remaining)?; |
713 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
714 | let (protocol_major_version, remaining) = u16::try_parse(remaining)?; |
715 | let (protocol_minor_version, remaining) = u16::try_parse(remaining)?; |
716 | let (authorization_protocol_name_len, remaining) = u16::try_parse(remaining)?; |
717 | let (authorization_protocol_data_len, remaining) = u16::try_parse(remaining)?; |
718 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
719 | let (authorization_protocol_name, remaining) = crate::x11_utils::parse_u8_list(remaining, authorization_protocol_name_len.try_to_usize()?)?; |
720 | let authorization_protocol_name = authorization_protocol_name.to_vec(); |
721 | // Align offset to multiple of 4 |
722 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
723 | let misalignment = (4 - (offset % 4)) % 4; |
724 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
725 | let (authorization_protocol_data, remaining) = crate::x11_utils::parse_u8_list(remaining, authorization_protocol_data_len.try_to_usize()?)?; |
726 | let authorization_protocol_data = authorization_protocol_data.to_vec(); |
727 | // Align offset to multiple of 4 |
728 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
729 | let misalignment = (4 - (offset % 4)) % 4; |
730 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
731 | let result = SetupRequest { byte_order, protocol_major_version, protocol_minor_version, authorization_protocol_name, authorization_protocol_data }; |
732 | Ok((result, remaining)) |
733 | } |
734 | } |
735 | impl Serialize for SetupRequest { |
736 | type Bytes = Vec<u8>; |
737 | fn serialize(&self) -> Vec<u8> { |
738 | let mut result: Vec = Vec::new(); |
739 | self.serialize_into(&mut result); |
740 | result |
741 | } |
742 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
743 | bytes.reserve(additional:12); |
744 | self.byte_order.serialize_into(bytes); |
745 | bytes.extend_from_slice(&[0; 1]); |
746 | self.protocol_major_version.serialize_into(bytes); |
747 | self.protocol_minor_version.serialize_into(bytes); |
748 | let authorization_protocol_name_len: u16 = u16::try_from(self.authorization_protocol_name.len()).expect(msg:"`authorization_protocol_name` has too many elements" ); |
749 | authorization_protocol_name_len.serialize_into(bytes); |
750 | let authorization_protocol_data_len: u16 = u16::try_from(self.authorization_protocol_data.len()).expect(msg:"`authorization_protocol_data` has too many elements" ); |
751 | authorization_protocol_data_len.serialize_into(bytes); |
752 | bytes.extend_from_slice(&[0; 2]); |
753 | bytes.extend_from_slice(&self.authorization_protocol_name); |
754 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
755 | bytes.extend_from_slice(&self.authorization_protocol_data); |
756 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
757 | } |
758 | } |
759 | impl SetupRequest { |
760 | /// Get the value of the `authorization_protocol_name_len` field. |
761 | /// |
762 | /// The `authorization_protocol_name_len` field is used as the length field of the `authorization_protocol_name` field. |
763 | /// This function computes the field's value again based on the length of the list. |
764 | /// |
765 | /// # Panics |
766 | /// |
767 | /// Panics if the value cannot be represented in the target type. This |
768 | /// cannot happen with values of the struct received from the X11 server. |
769 | pub fn authorization_protocol_name_len(&self) -> u16 { |
770 | self.authorization_protocol_name.len() |
771 | .try_into().unwrap() |
772 | } |
773 | /// Get the value of the `authorization_protocol_data_len` field. |
774 | /// |
775 | /// The `authorization_protocol_data_len` field is used as the length field of the `authorization_protocol_data` field. |
776 | /// This function computes the field's value again based on the length of the list. |
777 | /// |
778 | /// # Panics |
779 | /// |
780 | /// Panics if the value cannot be represented in the target type. This |
781 | /// cannot happen with values of the struct received from the X11 server. |
782 | pub fn authorization_protocol_data_len(&self) -> u16 { |
783 | self.authorization_protocol_data.len() |
784 | .try_into().unwrap() |
785 | } |
786 | } |
787 | |
788 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
789 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
790 | pub struct SetupFailed { |
791 | pub status: u8, |
792 | pub protocol_major_version: u16, |
793 | pub protocol_minor_version: u16, |
794 | pub length: u16, |
795 | pub reason: Vec<u8>, |
796 | } |
797 | impl TryParse for SetupFailed { |
798 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
799 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
800 | let (reason_len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
801 | let (protocol_major_version: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
802 | let (protocol_minor_version: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
803 | let (length: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
804 | let (reason: &[u8], remaining: &[u8]) = crate::x11_utils::parse_u8_list(data:remaining, list_length:reason_len.try_to_usize()?)?; |
805 | let reason: Vec = reason.to_vec(); |
806 | let result: SetupFailed = SetupFailed { status, protocol_major_version, protocol_minor_version, length, reason }; |
807 | Ok((result, remaining)) |
808 | } |
809 | } |
810 | impl Serialize for SetupFailed { |
811 | type Bytes = Vec<u8>; |
812 | fn serialize(&self) -> Vec<u8> { |
813 | let mut result: Vec = Vec::new(); |
814 | self.serialize_into(&mut result); |
815 | result |
816 | } |
817 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
818 | bytes.reserve(additional:8); |
819 | self.status.serialize_into(bytes); |
820 | let reason_len: u8 = u8::try_from(self.reason.len()).expect(msg:"`reason` has too many elements" ); |
821 | reason_len.serialize_into(bytes); |
822 | self.protocol_major_version.serialize_into(bytes); |
823 | self.protocol_minor_version.serialize_into(bytes); |
824 | self.length.serialize_into(bytes); |
825 | bytes.extend_from_slice(&self.reason); |
826 | } |
827 | } |
828 | impl SetupFailed { |
829 | /// Get the value of the `reason_len` field. |
830 | /// |
831 | /// The `reason_len` field is used as the length field of the `reason` field. |
832 | /// This function computes the field's value again based on the length of the list. |
833 | /// |
834 | /// # Panics |
835 | /// |
836 | /// Panics if the value cannot be represented in the target type. This |
837 | /// cannot happen with values of the struct received from the X11 server. |
838 | pub fn reason_len(&self) -> u8 { |
839 | self.reason.len() |
840 | .try_into().unwrap() |
841 | } |
842 | } |
843 | |
844 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
845 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
846 | pub struct SetupAuthenticate { |
847 | pub status: u8, |
848 | pub reason: Vec<u8>, |
849 | } |
850 | impl TryParse for SetupAuthenticate { |
851 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
852 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
853 | let remaining: &[u8] = remaining.get(5..).ok_or(err:ParseError::InsufficientData)?; |
854 | let (length: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
855 | let (reason: &[u8], remaining: &[u8]) = crate::x11_utils::parse_u8_list(data:remaining, list_length:u32::from(length).checked_mul(4u32).ok_or(err:ParseError::InvalidExpression)?.try_to_usize()?)?; |
856 | let reason: Vec = reason.to_vec(); |
857 | let result: SetupAuthenticate = SetupAuthenticate { status, reason }; |
858 | Ok((result, remaining)) |
859 | } |
860 | } |
861 | impl Serialize for SetupAuthenticate { |
862 | type Bytes = Vec<u8>; |
863 | fn serialize(&self) -> Vec<u8> { |
864 | let mut result: Vec = Vec::new(); |
865 | self.serialize_into(&mut result); |
866 | result |
867 | } |
868 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
869 | bytes.reserve(additional:8); |
870 | self.status.serialize_into(bytes); |
871 | bytes.extend_from_slice(&[0; 5]); |
872 | assert_eq!(self.reason.len() % 4, 0, "`reason` has an incorrect length, must be a multiple of 4" ); |
873 | let length: u16 = u16::try_from(self.reason.len() / 4).expect(msg:"`reason` has too many elements" ); |
874 | length.serialize_into(bytes); |
875 | bytes.extend_from_slice(&self.reason); |
876 | } |
877 | } |
878 | impl SetupAuthenticate { |
879 | /// Get the value of the `length` field. |
880 | /// |
881 | /// The `length` field is used as the length field of the `reason` field. |
882 | /// This function computes the field's value again based on the length of the list. |
883 | /// |
884 | /// # Panics |
885 | /// |
886 | /// Panics if the value cannot be represented in the target type. This |
887 | /// cannot happen with values of the struct received from the X11 server. |
888 | pub fn length(&self) -> u16 { |
889 | self.reason.len() |
890 | .checked_div(4).unwrap() |
891 | .try_into().unwrap() |
892 | } |
893 | } |
894 | |
895 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
896 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
897 | pub struct ImageOrder(u8); |
898 | impl ImageOrder { |
899 | pub const LSB_FIRST: Self = Self(0); |
900 | pub const MSB_FIRST: Self = Self(1); |
901 | } |
902 | impl From<ImageOrder> for u8 { |
903 | #[inline ] |
904 | fn from(input: ImageOrder) -> Self { |
905 | input.0 |
906 | } |
907 | } |
908 | impl From<ImageOrder> for Option<u8> { |
909 | #[inline ] |
910 | fn from(input: ImageOrder) -> Self { |
911 | Some(input.0) |
912 | } |
913 | } |
914 | impl From<ImageOrder> for u16 { |
915 | #[inline ] |
916 | fn from(input: ImageOrder) -> Self { |
917 | u16::from(input.0) |
918 | } |
919 | } |
920 | impl From<ImageOrder> for Option<u16> { |
921 | #[inline ] |
922 | fn from(input: ImageOrder) -> Self { |
923 | Some(u16::from(input.0)) |
924 | } |
925 | } |
926 | impl From<ImageOrder> for u32 { |
927 | #[inline ] |
928 | fn from(input: ImageOrder) -> Self { |
929 | u32::from(input.0) |
930 | } |
931 | } |
932 | impl From<ImageOrder> for Option<u32> { |
933 | #[inline ] |
934 | fn from(input: ImageOrder) -> Self { |
935 | Some(u32::from(input.0)) |
936 | } |
937 | } |
938 | impl From<u8> for ImageOrder { |
939 | #[inline ] |
940 | fn from(value: u8) -> Self { |
941 | Self(value) |
942 | } |
943 | } |
944 | impl core::fmt::Debug for ImageOrder { |
945 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
946 | let variants: [(u32, &str, &str); 2] = [ |
947 | (Self::LSB_FIRST.0.into(), "LSB_FIRST" , "LSBFirst" ), |
948 | (Self::MSB_FIRST.0.into(), "MSB_FIRST" , "MSBFirst" ), |
949 | ]; |
950 | pretty_print_enum(fmt, self.0.into(), &variants) |
951 | } |
952 | } |
953 | |
954 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
955 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
956 | pub struct Setup { |
957 | pub status: u8, |
958 | pub protocol_major_version: u16, |
959 | pub protocol_minor_version: u16, |
960 | pub length: u16, |
961 | pub release_number: u32, |
962 | pub resource_id_base: u32, |
963 | pub resource_id_mask: u32, |
964 | pub motion_buffer_size: u32, |
965 | pub maximum_request_length: u16, |
966 | pub image_byte_order: ImageOrder, |
967 | pub bitmap_format_bit_order: ImageOrder, |
968 | pub bitmap_format_scanline_unit: u8, |
969 | pub bitmap_format_scanline_pad: u8, |
970 | pub min_keycode: Keycode, |
971 | pub max_keycode: Keycode, |
972 | pub vendor: Vec<u8>, |
973 | pub pixmap_formats: Vec<Format>, |
974 | pub roots: Vec<Screen>, |
975 | } |
976 | impl TryParse for Setup { |
977 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
978 | let value = remaining; |
979 | let (status, remaining) = u8::try_parse(remaining)?; |
980 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
981 | let (protocol_major_version, remaining) = u16::try_parse(remaining)?; |
982 | let (protocol_minor_version, remaining) = u16::try_parse(remaining)?; |
983 | let (length, remaining) = u16::try_parse(remaining)?; |
984 | let (release_number, remaining) = u32::try_parse(remaining)?; |
985 | let (resource_id_base, remaining) = u32::try_parse(remaining)?; |
986 | let (resource_id_mask, remaining) = u32::try_parse(remaining)?; |
987 | let (motion_buffer_size, remaining) = u32::try_parse(remaining)?; |
988 | let (vendor_len, remaining) = u16::try_parse(remaining)?; |
989 | let (maximum_request_length, remaining) = u16::try_parse(remaining)?; |
990 | let (roots_len, remaining) = u8::try_parse(remaining)?; |
991 | let (pixmap_formats_len, remaining) = u8::try_parse(remaining)?; |
992 | let (image_byte_order, remaining) = u8::try_parse(remaining)?; |
993 | let (bitmap_format_bit_order, remaining) = u8::try_parse(remaining)?; |
994 | let (bitmap_format_scanline_unit, remaining) = u8::try_parse(remaining)?; |
995 | let (bitmap_format_scanline_pad, remaining) = u8::try_parse(remaining)?; |
996 | let (min_keycode, remaining) = Keycode::try_parse(remaining)?; |
997 | let (max_keycode, remaining) = Keycode::try_parse(remaining)?; |
998 | let remaining = remaining.get(4..).ok_or(ParseError::InsufficientData)?; |
999 | let (vendor, remaining) = crate::x11_utils::parse_u8_list(remaining, vendor_len.try_to_usize()?)?; |
1000 | let vendor = vendor.to_vec(); |
1001 | // Align offset to multiple of 4 |
1002 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
1003 | let misalignment = (4 - (offset % 4)) % 4; |
1004 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
1005 | let (pixmap_formats, remaining) = crate::x11_utils::parse_list::<Format>(remaining, pixmap_formats_len.try_to_usize()?)?; |
1006 | let (roots, remaining) = crate::x11_utils::parse_list::<Screen>(remaining, roots_len.try_to_usize()?)?; |
1007 | let image_byte_order = image_byte_order.into(); |
1008 | let bitmap_format_bit_order = bitmap_format_bit_order.into(); |
1009 | let result = Setup { status, protocol_major_version, protocol_minor_version, length, release_number, resource_id_base, resource_id_mask, motion_buffer_size, maximum_request_length, image_byte_order, bitmap_format_bit_order, bitmap_format_scanline_unit, bitmap_format_scanline_pad, min_keycode, max_keycode, vendor, pixmap_formats, roots }; |
1010 | Ok((result, remaining)) |
1011 | } |
1012 | } |
1013 | impl Serialize for Setup { |
1014 | type Bytes = Vec<u8>; |
1015 | fn serialize(&self) -> Vec<u8> { |
1016 | let mut result = Vec::new(); |
1017 | self.serialize_into(&mut result); |
1018 | result |
1019 | } |
1020 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
1021 | bytes.reserve(40); |
1022 | self.status.serialize_into(bytes); |
1023 | bytes.extend_from_slice(&[0; 1]); |
1024 | self.protocol_major_version.serialize_into(bytes); |
1025 | self.protocol_minor_version.serialize_into(bytes); |
1026 | self.length.serialize_into(bytes); |
1027 | self.release_number.serialize_into(bytes); |
1028 | self.resource_id_base.serialize_into(bytes); |
1029 | self.resource_id_mask.serialize_into(bytes); |
1030 | self.motion_buffer_size.serialize_into(bytes); |
1031 | let vendor_len = u16::try_from(self.vendor.len()).expect("`vendor` has too many elements" ); |
1032 | vendor_len.serialize_into(bytes); |
1033 | self.maximum_request_length.serialize_into(bytes); |
1034 | let roots_len = u8::try_from(self.roots.len()).expect("`roots` has too many elements" ); |
1035 | roots_len.serialize_into(bytes); |
1036 | let pixmap_formats_len = u8::try_from(self.pixmap_formats.len()).expect("`pixmap_formats` has too many elements" ); |
1037 | pixmap_formats_len.serialize_into(bytes); |
1038 | u8::from(self.image_byte_order).serialize_into(bytes); |
1039 | u8::from(self.bitmap_format_bit_order).serialize_into(bytes); |
1040 | self.bitmap_format_scanline_unit.serialize_into(bytes); |
1041 | self.bitmap_format_scanline_pad.serialize_into(bytes); |
1042 | self.min_keycode.serialize_into(bytes); |
1043 | self.max_keycode.serialize_into(bytes); |
1044 | bytes.extend_from_slice(&[0; 4]); |
1045 | bytes.extend_from_slice(&self.vendor); |
1046 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
1047 | self.pixmap_formats.serialize_into(bytes); |
1048 | self.roots.serialize_into(bytes); |
1049 | } |
1050 | } |
1051 | impl Setup { |
1052 | /// Get the value of the `vendor_len` field. |
1053 | /// |
1054 | /// The `vendor_len` field is used as the length field of the `vendor` field. |
1055 | /// This function computes the field's value again based on the length of the list. |
1056 | /// |
1057 | /// # Panics |
1058 | /// |
1059 | /// Panics if the value cannot be represented in the target type. This |
1060 | /// cannot happen with values of the struct received from the X11 server. |
1061 | pub fn vendor_len(&self) -> u16 { |
1062 | self.vendor.len() |
1063 | .try_into().unwrap() |
1064 | } |
1065 | /// Get the value of the `roots_len` field. |
1066 | /// |
1067 | /// The `roots_len` field is used as the length field of the `roots` field. |
1068 | /// This function computes the field's value again based on the length of the list. |
1069 | /// |
1070 | /// # Panics |
1071 | /// |
1072 | /// Panics if the value cannot be represented in the target type. This |
1073 | /// cannot happen with values of the struct received from the X11 server. |
1074 | pub fn roots_len(&self) -> u8 { |
1075 | self.roots.len() |
1076 | .try_into().unwrap() |
1077 | } |
1078 | /// Get the value of the `pixmap_formats_len` field. |
1079 | /// |
1080 | /// The `pixmap_formats_len` field is used as the length field of the `pixmap_formats` field. |
1081 | /// This function computes the field's value again based on the length of the list. |
1082 | /// |
1083 | /// # Panics |
1084 | /// |
1085 | /// Panics if the value cannot be represented in the target type. This |
1086 | /// cannot happen with values of the struct received from the X11 server. |
1087 | pub fn pixmap_formats_len(&self) -> u8 { |
1088 | self.pixmap_formats.len() |
1089 | .try_into().unwrap() |
1090 | } |
1091 | } |
1092 | |
1093 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
1094 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
1095 | pub struct ModMask(u16); |
1096 | impl ModMask { |
1097 | pub const SHIFT: Self = Self(1 << 0); |
1098 | pub const LOCK: Self = Self(1 << 1); |
1099 | pub const CONTROL: Self = Self(1 << 2); |
1100 | pub const M1: Self = Self(1 << 3); |
1101 | pub const M2: Self = Self(1 << 4); |
1102 | pub const M3: Self = Self(1 << 5); |
1103 | pub const M4: Self = Self(1 << 6); |
1104 | pub const M5: Self = Self(1 << 7); |
1105 | pub const ANY: Self = Self(1 << 15); |
1106 | } |
1107 | impl From<ModMask> for u16 { |
1108 | #[inline ] |
1109 | fn from(input: ModMask) -> Self { |
1110 | input.0 |
1111 | } |
1112 | } |
1113 | impl From<ModMask> for Option<u16> { |
1114 | #[inline ] |
1115 | fn from(input: ModMask) -> Self { |
1116 | Some(input.0) |
1117 | } |
1118 | } |
1119 | impl From<ModMask> for u32 { |
1120 | #[inline ] |
1121 | fn from(input: ModMask) -> Self { |
1122 | u32::from(input.0) |
1123 | } |
1124 | } |
1125 | impl From<ModMask> for Option<u32> { |
1126 | #[inline ] |
1127 | fn from(input: ModMask) -> Self { |
1128 | Some(u32::from(input.0)) |
1129 | } |
1130 | } |
1131 | impl From<u8> for ModMask { |
1132 | #[inline ] |
1133 | fn from(value: u8) -> Self { |
1134 | Self(value.into()) |
1135 | } |
1136 | } |
1137 | impl From<u16> for ModMask { |
1138 | #[inline ] |
1139 | fn from(value: u16) -> Self { |
1140 | Self(value) |
1141 | } |
1142 | } |
1143 | impl core::fmt::Debug for ModMask { |
1144 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
1145 | let variants: [(u32, &str, &str); 9] = [ |
1146 | (Self::SHIFT.0.into(), "SHIFT" , "Shift" ), |
1147 | (Self::LOCK.0.into(), "LOCK" , "Lock" ), |
1148 | (Self::CONTROL.0.into(), "CONTROL" , "Control" ), |
1149 | (Self::M1.0.into(), "M1" , "M1" ), |
1150 | (Self::M2.0.into(), "M2" , "M2" ), |
1151 | (Self::M3.0.into(), "M3" , "M3" ), |
1152 | (Self::M4.0.into(), "M4" , "M4" ), |
1153 | (Self::M5.0.into(), "M5" , "M5" ), |
1154 | (Self::ANY.0.into(), "ANY" , "Any" ), |
1155 | ]; |
1156 | pretty_print_bitmask(fmt, self.0.into(), &variants) |
1157 | } |
1158 | } |
1159 | bitmask_binop!(ModMask, u16); |
1160 | |
1161 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
1162 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
1163 | pub struct KeyButMask(u16); |
1164 | impl KeyButMask { |
1165 | pub const SHIFT: Self = Self(1 << 0); |
1166 | pub const LOCK: Self = Self(1 << 1); |
1167 | pub const CONTROL: Self = Self(1 << 2); |
1168 | pub const MOD1: Self = Self(1 << 3); |
1169 | pub const MOD2: Self = Self(1 << 4); |
1170 | pub const MOD3: Self = Self(1 << 5); |
1171 | pub const MOD4: Self = Self(1 << 6); |
1172 | pub const MOD5: Self = Self(1 << 7); |
1173 | pub const BUTTON1: Self = Self(1 << 8); |
1174 | pub const BUTTON2: Self = Self(1 << 9); |
1175 | pub const BUTTON3: Self = Self(1 << 10); |
1176 | pub const BUTTON4: Self = Self(1 << 11); |
1177 | pub const BUTTON5: Self = Self(1 << 12); |
1178 | } |
1179 | impl From<KeyButMask> for u16 { |
1180 | #[inline ] |
1181 | fn from(input: KeyButMask) -> Self { |
1182 | input.0 |
1183 | } |
1184 | } |
1185 | impl From<KeyButMask> for Option<u16> { |
1186 | #[inline ] |
1187 | fn from(input: KeyButMask) -> Self { |
1188 | Some(input.0) |
1189 | } |
1190 | } |
1191 | impl From<KeyButMask> for u32 { |
1192 | #[inline ] |
1193 | fn from(input: KeyButMask) -> Self { |
1194 | u32::from(input.0) |
1195 | } |
1196 | } |
1197 | impl From<KeyButMask> for Option<u32> { |
1198 | #[inline ] |
1199 | fn from(input: KeyButMask) -> Self { |
1200 | Some(u32::from(input.0)) |
1201 | } |
1202 | } |
1203 | impl From<u8> for KeyButMask { |
1204 | #[inline ] |
1205 | fn from(value: u8) -> Self { |
1206 | Self(value.into()) |
1207 | } |
1208 | } |
1209 | impl From<u16> for KeyButMask { |
1210 | #[inline ] |
1211 | fn from(value: u16) -> Self { |
1212 | Self(value) |
1213 | } |
1214 | } |
1215 | impl core::fmt::Debug for KeyButMask { |
1216 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
1217 | let variants: [(u32, &str, &str); 13] = [ |
1218 | (Self::SHIFT.0.into(), "SHIFT" , "Shift" ), |
1219 | (Self::LOCK.0.into(), "LOCK" , "Lock" ), |
1220 | (Self::CONTROL.0.into(), "CONTROL" , "Control" ), |
1221 | (Self::MOD1.0.into(), "MOD1" , "Mod1" ), |
1222 | (Self::MOD2.0.into(), "MOD2" , "Mod2" ), |
1223 | (Self::MOD3.0.into(), "MOD3" , "Mod3" ), |
1224 | (Self::MOD4.0.into(), "MOD4" , "Mod4" ), |
1225 | (Self::MOD5.0.into(), "MOD5" , "Mod5" ), |
1226 | (Self::BUTTON1.0.into(), "BUTTON1" , "Button1" ), |
1227 | (Self::BUTTON2.0.into(), "BUTTON2" , "Button2" ), |
1228 | (Self::BUTTON3.0.into(), "BUTTON3" , "Button3" ), |
1229 | (Self::BUTTON4.0.into(), "BUTTON4" , "Button4" ), |
1230 | (Self::BUTTON5.0.into(), "BUTTON5" , "Button5" ), |
1231 | ]; |
1232 | pretty_print_bitmask(fmt, self.0.into(), &variants) |
1233 | } |
1234 | } |
1235 | bitmask_binop!(KeyButMask, u16); |
1236 | |
1237 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
1238 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
1239 | pub struct WindowEnum(u8); |
1240 | impl WindowEnum { |
1241 | pub const NONE: Self = Self(0); |
1242 | } |
1243 | impl From<WindowEnum> for u8 { |
1244 | #[inline ] |
1245 | fn from(input: WindowEnum) -> Self { |
1246 | input.0 |
1247 | } |
1248 | } |
1249 | impl From<WindowEnum> for Option<u8> { |
1250 | #[inline ] |
1251 | fn from(input: WindowEnum) -> Self { |
1252 | Some(input.0) |
1253 | } |
1254 | } |
1255 | impl From<WindowEnum> for u16 { |
1256 | #[inline ] |
1257 | fn from(input: WindowEnum) -> Self { |
1258 | u16::from(input.0) |
1259 | } |
1260 | } |
1261 | impl From<WindowEnum> for Option<u16> { |
1262 | #[inline ] |
1263 | fn from(input: WindowEnum) -> Self { |
1264 | Some(u16::from(input.0)) |
1265 | } |
1266 | } |
1267 | impl From<WindowEnum> for u32 { |
1268 | #[inline ] |
1269 | fn from(input: WindowEnum) -> Self { |
1270 | u32::from(input.0) |
1271 | } |
1272 | } |
1273 | impl From<WindowEnum> for Option<u32> { |
1274 | #[inline ] |
1275 | fn from(input: WindowEnum) -> Self { |
1276 | Some(u32::from(input.0)) |
1277 | } |
1278 | } |
1279 | impl From<u8> for WindowEnum { |
1280 | #[inline ] |
1281 | fn from(value: u8) -> Self { |
1282 | Self(value) |
1283 | } |
1284 | } |
1285 | impl core::fmt::Debug for WindowEnum { |
1286 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
1287 | let variants: [(u32, &str, &str); 1] = [ |
1288 | (Self::NONE.0.into(), "NONE" , "None" ), |
1289 | ]; |
1290 | pretty_print_enum(fmt, self.0.into(), &variants) |
1291 | } |
1292 | } |
1293 | |
1294 | /// Opcode for the KeyPress event |
1295 | pub const KEY_PRESS_EVENT: u8 = 2; |
1296 | /// a key was pressed/released. |
1297 | /// |
1298 | /// # Fields |
1299 | /// |
1300 | /// * `detail` - The keycode (a number representing a physical key on the keyboard) of the key |
1301 | /// which was pressed. |
1302 | /// * `time` - Time when the event was generated (in milliseconds). |
1303 | /// * `root` - The root window of `child`. |
1304 | /// * `same_screen` - Whether the `event` window is on the same screen as the `root` window. |
1305 | /// * `event_x` - If `same_screen` is true, this is the X coordinate relative to the `event` |
1306 | /// window's origin. Otherwise, `event_x` will be set to zero. |
1307 | /// * `event_y` - If `same_screen` is true, this is the Y coordinate relative to the `event` |
1308 | /// window's origin. Otherwise, `event_y` will be set to zero. |
1309 | /// * `root_x` - The X coordinate of the pointer relative to the `root` window at the time of |
1310 | /// the event. |
1311 | /// * `root_y` - The Y coordinate of the pointer relative to the `root` window at the time of |
1312 | /// the event. |
1313 | /// * `state` - The logical state of the pointer buttons and modifier keys just prior to the |
1314 | /// event. |
1315 | /// |
1316 | /// # See |
1317 | /// |
1318 | /// * `GrabKey`: request |
1319 | /// * `GrabKeyboard`: request |
1320 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
1321 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
1322 | pub struct KeyPressEvent { |
1323 | pub response_type: u8, |
1324 | pub detail: Keycode, |
1325 | pub sequence: u16, |
1326 | pub time: Timestamp, |
1327 | pub root: Window, |
1328 | pub event: Window, |
1329 | pub child: Window, |
1330 | pub root_x: i16, |
1331 | pub root_y: i16, |
1332 | pub event_x: i16, |
1333 | pub event_y: i16, |
1334 | pub state: KeyButMask, |
1335 | pub same_screen: bool, |
1336 | } |
1337 | impl TryParse for KeyPressEvent { |
1338 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
1339 | let remaining = initial_value; |
1340 | let (response_type, remaining) = u8::try_parse(remaining)?; |
1341 | let (detail, remaining) = Keycode::try_parse(remaining)?; |
1342 | let (sequence, remaining) = u16::try_parse(remaining)?; |
1343 | let (time, remaining) = Timestamp::try_parse(remaining)?; |
1344 | let (root, remaining) = Window::try_parse(remaining)?; |
1345 | let (event, remaining) = Window::try_parse(remaining)?; |
1346 | let (child, remaining) = Window::try_parse(remaining)?; |
1347 | let (root_x, remaining) = i16::try_parse(remaining)?; |
1348 | let (root_y, remaining) = i16::try_parse(remaining)?; |
1349 | let (event_x, remaining) = i16::try_parse(remaining)?; |
1350 | let (event_y, remaining) = i16::try_parse(remaining)?; |
1351 | let (state, remaining) = u16::try_parse(remaining)?; |
1352 | let (same_screen, remaining) = bool::try_parse(remaining)?; |
1353 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
1354 | let state = state.into(); |
1355 | let result = KeyPressEvent { response_type, detail, sequence, time, root, event, child, root_x, root_y, event_x, event_y, state, same_screen }; |
1356 | let _ = remaining; |
1357 | let remaining = initial_value.get(32..) |
1358 | .ok_or(ParseError::InsufficientData)?; |
1359 | Ok((result, remaining)) |
1360 | } |
1361 | } |
1362 | impl Serialize for KeyPressEvent { |
1363 | type Bytes = [u8; 32]; |
1364 | fn serialize(&self) -> [u8; 32] { |
1365 | let response_type_bytes = self.response_type.serialize(); |
1366 | let detail_bytes = self.detail.serialize(); |
1367 | let sequence_bytes = self.sequence.serialize(); |
1368 | let time_bytes = self.time.serialize(); |
1369 | let root_bytes = self.root.serialize(); |
1370 | let event_bytes = self.event.serialize(); |
1371 | let child_bytes = self.child.serialize(); |
1372 | let root_x_bytes = self.root_x.serialize(); |
1373 | let root_y_bytes = self.root_y.serialize(); |
1374 | let event_x_bytes = self.event_x.serialize(); |
1375 | let event_y_bytes = self.event_y.serialize(); |
1376 | let state_bytes = u16::from(self.state).serialize(); |
1377 | let same_screen_bytes = self.same_screen.serialize(); |
1378 | [ |
1379 | response_type_bytes[0], |
1380 | detail_bytes[0], |
1381 | sequence_bytes[0], |
1382 | sequence_bytes[1], |
1383 | time_bytes[0], |
1384 | time_bytes[1], |
1385 | time_bytes[2], |
1386 | time_bytes[3], |
1387 | root_bytes[0], |
1388 | root_bytes[1], |
1389 | root_bytes[2], |
1390 | root_bytes[3], |
1391 | event_bytes[0], |
1392 | event_bytes[1], |
1393 | event_bytes[2], |
1394 | event_bytes[3], |
1395 | child_bytes[0], |
1396 | child_bytes[1], |
1397 | child_bytes[2], |
1398 | child_bytes[3], |
1399 | root_x_bytes[0], |
1400 | root_x_bytes[1], |
1401 | root_y_bytes[0], |
1402 | root_y_bytes[1], |
1403 | event_x_bytes[0], |
1404 | event_x_bytes[1], |
1405 | event_y_bytes[0], |
1406 | event_y_bytes[1], |
1407 | state_bytes[0], |
1408 | state_bytes[1], |
1409 | same_screen_bytes[0], |
1410 | 0, |
1411 | ] |
1412 | } |
1413 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
1414 | bytes.reserve(32); |
1415 | self.response_type.serialize_into(bytes); |
1416 | self.detail.serialize_into(bytes); |
1417 | self.sequence.serialize_into(bytes); |
1418 | self.time.serialize_into(bytes); |
1419 | self.root.serialize_into(bytes); |
1420 | self.event.serialize_into(bytes); |
1421 | self.child.serialize_into(bytes); |
1422 | self.root_x.serialize_into(bytes); |
1423 | self.root_y.serialize_into(bytes); |
1424 | self.event_x.serialize_into(bytes); |
1425 | self.event_y.serialize_into(bytes); |
1426 | u16::from(self.state).serialize_into(bytes); |
1427 | self.same_screen.serialize_into(bytes); |
1428 | bytes.extend_from_slice(&[0; 1]); |
1429 | } |
1430 | } |
1431 | impl From<&KeyPressEvent> for [u8; 32] { |
1432 | fn from(input: &KeyPressEvent) -> Self { |
1433 | let response_type_bytes = input.response_type.serialize(); |
1434 | let detail_bytes = input.detail.serialize(); |
1435 | let sequence_bytes = input.sequence.serialize(); |
1436 | let time_bytes = input.time.serialize(); |
1437 | let root_bytes = input.root.serialize(); |
1438 | let event_bytes = input.event.serialize(); |
1439 | let child_bytes = input.child.serialize(); |
1440 | let root_x_bytes = input.root_x.serialize(); |
1441 | let root_y_bytes = input.root_y.serialize(); |
1442 | let event_x_bytes = input.event_x.serialize(); |
1443 | let event_y_bytes = input.event_y.serialize(); |
1444 | let state_bytes = u16::from(input.state).serialize(); |
1445 | let same_screen_bytes = input.same_screen.serialize(); |
1446 | [ |
1447 | response_type_bytes[0], |
1448 | detail_bytes[0], |
1449 | sequence_bytes[0], |
1450 | sequence_bytes[1], |
1451 | time_bytes[0], |
1452 | time_bytes[1], |
1453 | time_bytes[2], |
1454 | time_bytes[3], |
1455 | root_bytes[0], |
1456 | root_bytes[1], |
1457 | root_bytes[2], |
1458 | root_bytes[3], |
1459 | event_bytes[0], |
1460 | event_bytes[1], |
1461 | event_bytes[2], |
1462 | event_bytes[3], |
1463 | child_bytes[0], |
1464 | child_bytes[1], |
1465 | child_bytes[2], |
1466 | child_bytes[3], |
1467 | root_x_bytes[0], |
1468 | root_x_bytes[1], |
1469 | root_y_bytes[0], |
1470 | root_y_bytes[1], |
1471 | event_x_bytes[0], |
1472 | event_x_bytes[1], |
1473 | event_y_bytes[0], |
1474 | event_y_bytes[1], |
1475 | state_bytes[0], |
1476 | state_bytes[1], |
1477 | same_screen_bytes[0], |
1478 | 0, |
1479 | ] |
1480 | } |
1481 | } |
1482 | impl From<KeyPressEvent> for [u8; 32] { |
1483 | fn from(input: KeyPressEvent) -> Self { |
1484 | Self::from(&input) |
1485 | } |
1486 | } |
1487 | |
1488 | /// Opcode for the KeyRelease event |
1489 | pub const KEY_RELEASE_EVENT: u8 = 3; |
1490 | pub type KeyReleaseEvent = KeyPressEvent; |
1491 | |
1492 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
1493 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
1494 | pub struct ButtonMask(u16); |
1495 | impl ButtonMask { |
1496 | pub const M1: Self = Self(1 << 8); |
1497 | pub const M2: Self = Self(1 << 9); |
1498 | pub const M3: Self = Self(1 << 10); |
1499 | pub const M4: Self = Self(1 << 11); |
1500 | pub const M5: Self = Self(1 << 12); |
1501 | pub const ANY: Self = Self(1 << 15); |
1502 | } |
1503 | impl From<ButtonMask> for u16 { |
1504 | #[inline ] |
1505 | fn from(input: ButtonMask) -> Self { |
1506 | input.0 |
1507 | } |
1508 | } |
1509 | impl From<ButtonMask> for Option<u16> { |
1510 | #[inline ] |
1511 | fn from(input: ButtonMask) -> Self { |
1512 | Some(input.0) |
1513 | } |
1514 | } |
1515 | impl From<ButtonMask> for u32 { |
1516 | #[inline ] |
1517 | fn from(input: ButtonMask) -> Self { |
1518 | u32::from(input.0) |
1519 | } |
1520 | } |
1521 | impl From<ButtonMask> for Option<u32> { |
1522 | #[inline ] |
1523 | fn from(input: ButtonMask) -> Self { |
1524 | Some(u32::from(input.0)) |
1525 | } |
1526 | } |
1527 | impl From<u8> for ButtonMask { |
1528 | #[inline ] |
1529 | fn from(value: u8) -> Self { |
1530 | Self(value.into()) |
1531 | } |
1532 | } |
1533 | impl From<u16> for ButtonMask { |
1534 | #[inline ] |
1535 | fn from(value: u16) -> Self { |
1536 | Self(value) |
1537 | } |
1538 | } |
1539 | impl core::fmt::Debug for ButtonMask { |
1540 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
1541 | let variants: [(u32, &str, &str); 6] = [ |
1542 | (Self::M1.0.into(), "M1" , "M1" ), |
1543 | (Self::M2.0.into(), "M2" , "M2" ), |
1544 | (Self::M3.0.into(), "M3" , "M3" ), |
1545 | (Self::M4.0.into(), "M4" , "M4" ), |
1546 | (Self::M5.0.into(), "M5" , "M5" ), |
1547 | (Self::ANY.0.into(), "ANY" , "Any" ), |
1548 | ]; |
1549 | pretty_print_bitmask(fmt, self.0.into(), &variants) |
1550 | } |
1551 | } |
1552 | bitmask_binop!(ButtonMask, u16); |
1553 | |
1554 | /// Opcode for the ButtonPress event |
1555 | pub const BUTTON_PRESS_EVENT: u8 = 4; |
1556 | /// a mouse button was pressed/released. |
1557 | /// |
1558 | /// # Fields |
1559 | /// |
1560 | /// * `detail` - The keycode (a number representing a physical key on the keyboard) of the key |
1561 | /// which was pressed. |
1562 | /// * `time` - Time when the event was generated (in milliseconds). |
1563 | /// * `root` - The root window of `child`. |
1564 | /// * `same_screen` - Whether the `event` window is on the same screen as the `root` window. |
1565 | /// * `event_x` - If `same_screen` is true, this is the X coordinate relative to the `event` |
1566 | /// window's origin. Otherwise, `event_x` will be set to zero. |
1567 | /// * `event_y` - If `same_screen` is true, this is the Y coordinate relative to the `event` |
1568 | /// window's origin. Otherwise, `event_y` will be set to zero. |
1569 | /// * `root_x` - The X coordinate of the pointer relative to the `root` window at the time of |
1570 | /// the event. |
1571 | /// * `root_y` - The Y coordinate of the pointer relative to the `root` window at the time of |
1572 | /// the event. |
1573 | /// * `state` - The logical state of the pointer buttons and modifier keys just prior to the |
1574 | /// event. |
1575 | /// |
1576 | /// # See |
1577 | /// |
1578 | /// * `GrabButton`: request |
1579 | /// * `GrabPointer`: request |
1580 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
1581 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
1582 | pub struct ButtonPressEvent { |
1583 | pub response_type: u8, |
1584 | pub detail: Button, |
1585 | pub sequence: u16, |
1586 | pub time: Timestamp, |
1587 | pub root: Window, |
1588 | pub event: Window, |
1589 | pub child: Window, |
1590 | pub root_x: i16, |
1591 | pub root_y: i16, |
1592 | pub event_x: i16, |
1593 | pub event_y: i16, |
1594 | pub state: KeyButMask, |
1595 | pub same_screen: bool, |
1596 | } |
1597 | impl TryParse for ButtonPressEvent { |
1598 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
1599 | let remaining = initial_value; |
1600 | let (response_type, remaining) = u8::try_parse(remaining)?; |
1601 | let (detail, remaining) = Button::try_parse(remaining)?; |
1602 | let (sequence, remaining) = u16::try_parse(remaining)?; |
1603 | let (time, remaining) = Timestamp::try_parse(remaining)?; |
1604 | let (root, remaining) = Window::try_parse(remaining)?; |
1605 | let (event, remaining) = Window::try_parse(remaining)?; |
1606 | let (child, remaining) = Window::try_parse(remaining)?; |
1607 | let (root_x, remaining) = i16::try_parse(remaining)?; |
1608 | let (root_y, remaining) = i16::try_parse(remaining)?; |
1609 | let (event_x, remaining) = i16::try_parse(remaining)?; |
1610 | let (event_y, remaining) = i16::try_parse(remaining)?; |
1611 | let (state, remaining) = u16::try_parse(remaining)?; |
1612 | let (same_screen, remaining) = bool::try_parse(remaining)?; |
1613 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
1614 | let state = state.into(); |
1615 | let result = ButtonPressEvent { response_type, detail, sequence, time, root, event, child, root_x, root_y, event_x, event_y, state, same_screen }; |
1616 | let _ = remaining; |
1617 | let remaining = initial_value.get(32..) |
1618 | .ok_or(ParseError::InsufficientData)?; |
1619 | Ok((result, remaining)) |
1620 | } |
1621 | } |
1622 | impl Serialize for ButtonPressEvent { |
1623 | type Bytes = [u8; 32]; |
1624 | fn serialize(&self) -> [u8; 32] { |
1625 | let response_type_bytes = self.response_type.serialize(); |
1626 | let detail_bytes = self.detail.serialize(); |
1627 | let sequence_bytes = self.sequence.serialize(); |
1628 | let time_bytes = self.time.serialize(); |
1629 | let root_bytes = self.root.serialize(); |
1630 | let event_bytes = self.event.serialize(); |
1631 | let child_bytes = self.child.serialize(); |
1632 | let root_x_bytes = self.root_x.serialize(); |
1633 | let root_y_bytes = self.root_y.serialize(); |
1634 | let event_x_bytes = self.event_x.serialize(); |
1635 | let event_y_bytes = self.event_y.serialize(); |
1636 | let state_bytes = u16::from(self.state).serialize(); |
1637 | let same_screen_bytes = self.same_screen.serialize(); |
1638 | [ |
1639 | response_type_bytes[0], |
1640 | detail_bytes[0], |
1641 | sequence_bytes[0], |
1642 | sequence_bytes[1], |
1643 | time_bytes[0], |
1644 | time_bytes[1], |
1645 | time_bytes[2], |
1646 | time_bytes[3], |
1647 | root_bytes[0], |
1648 | root_bytes[1], |
1649 | root_bytes[2], |
1650 | root_bytes[3], |
1651 | event_bytes[0], |
1652 | event_bytes[1], |
1653 | event_bytes[2], |
1654 | event_bytes[3], |
1655 | child_bytes[0], |
1656 | child_bytes[1], |
1657 | child_bytes[2], |
1658 | child_bytes[3], |
1659 | root_x_bytes[0], |
1660 | root_x_bytes[1], |
1661 | root_y_bytes[0], |
1662 | root_y_bytes[1], |
1663 | event_x_bytes[0], |
1664 | event_x_bytes[1], |
1665 | event_y_bytes[0], |
1666 | event_y_bytes[1], |
1667 | state_bytes[0], |
1668 | state_bytes[1], |
1669 | same_screen_bytes[0], |
1670 | 0, |
1671 | ] |
1672 | } |
1673 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
1674 | bytes.reserve(32); |
1675 | self.response_type.serialize_into(bytes); |
1676 | self.detail.serialize_into(bytes); |
1677 | self.sequence.serialize_into(bytes); |
1678 | self.time.serialize_into(bytes); |
1679 | self.root.serialize_into(bytes); |
1680 | self.event.serialize_into(bytes); |
1681 | self.child.serialize_into(bytes); |
1682 | self.root_x.serialize_into(bytes); |
1683 | self.root_y.serialize_into(bytes); |
1684 | self.event_x.serialize_into(bytes); |
1685 | self.event_y.serialize_into(bytes); |
1686 | u16::from(self.state).serialize_into(bytes); |
1687 | self.same_screen.serialize_into(bytes); |
1688 | bytes.extend_from_slice(&[0; 1]); |
1689 | } |
1690 | } |
1691 | impl From<&ButtonPressEvent> for [u8; 32] { |
1692 | fn from(input: &ButtonPressEvent) -> Self { |
1693 | let response_type_bytes = input.response_type.serialize(); |
1694 | let detail_bytes = input.detail.serialize(); |
1695 | let sequence_bytes = input.sequence.serialize(); |
1696 | let time_bytes = input.time.serialize(); |
1697 | let root_bytes = input.root.serialize(); |
1698 | let event_bytes = input.event.serialize(); |
1699 | let child_bytes = input.child.serialize(); |
1700 | let root_x_bytes = input.root_x.serialize(); |
1701 | let root_y_bytes = input.root_y.serialize(); |
1702 | let event_x_bytes = input.event_x.serialize(); |
1703 | let event_y_bytes = input.event_y.serialize(); |
1704 | let state_bytes = u16::from(input.state).serialize(); |
1705 | let same_screen_bytes = input.same_screen.serialize(); |
1706 | [ |
1707 | response_type_bytes[0], |
1708 | detail_bytes[0], |
1709 | sequence_bytes[0], |
1710 | sequence_bytes[1], |
1711 | time_bytes[0], |
1712 | time_bytes[1], |
1713 | time_bytes[2], |
1714 | time_bytes[3], |
1715 | root_bytes[0], |
1716 | root_bytes[1], |
1717 | root_bytes[2], |
1718 | root_bytes[3], |
1719 | event_bytes[0], |
1720 | event_bytes[1], |
1721 | event_bytes[2], |
1722 | event_bytes[3], |
1723 | child_bytes[0], |
1724 | child_bytes[1], |
1725 | child_bytes[2], |
1726 | child_bytes[3], |
1727 | root_x_bytes[0], |
1728 | root_x_bytes[1], |
1729 | root_y_bytes[0], |
1730 | root_y_bytes[1], |
1731 | event_x_bytes[0], |
1732 | event_x_bytes[1], |
1733 | event_y_bytes[0], |
1734 | event_y_bytes[1], |
1735 | state_bytes[0], |
1736 | state_bytes[1], |
1737 | same_screen_bytes[0], |
1738 | 0, |
1739 | ] |
1740 | } |
1741 | } |
1742 | impl From<ButtonPressEvent> for [u8; 32] { |
1743 | fn from(input: ButtonPressEvent) -> Self { |
1744 | Self::from(&input) |
1745 | } |
1746 | } |
1747 | |
1748 | /// Opcode for the ButtonRelease event |
1749 | pub const BUTTON_RELEASE_EVENT: u8 = 5; |
1750 | pub type ButtonReleaseEvent = ButtonPressEvent; |
1751 | |
1752 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
1753 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
1754 | pub struct Motion(u8); |
1755 | impl Motion { |
1756 | pub const NORMAL: Self = Self(0); |
1757 | pub const HINT: Self = Self(1); |
1758 | } |
1759 | impl From<Motion> for u8 { |
1760 | #[inline ] |
1761 | fn from(input: Motion) -> Self { |
1762 | input.0 |
1763 | } |
1764 | } |
1765 | impl From<Motion> for Option<u8> { |
1766 | #[inline ] |
1767 | fn from(input: Motion) -> Self { |
1768 | Some(input.0) |
1769 | } |
1770 | } |
1771 | impl From<Motion> for u16 { |
1772 | #[inline ] |
1773 | fn from(input: Motion) -> Self { |
1774 | u16::from(input.0) |
1775 | } |
1776 | } |
1777 | impl From<Motion> for Option<u16> { |
1778 | #[inline ] |
1779 | fn from(input: Motion) -> Self { |
1780 | Some(u16::from(input.0)) |
1781 | } |
1782 | } |
1783 | impl From<Motion> for u32 { |
1784 | #[inline ] |
1785 | fn from(input: Motion) -> Self { |
1786 | u32::from(input.0) |
1787 | } |
1788 | } |
1789 | impl From<Motion> for Option<u32> { |
1790 | #[inline ] |
1791 | fn from(input: Motion) -> Self { |
1792 | Some(u32::from(input.0)) |
1793 | } |
1794 | } |
1795 | impl From<u8> for Motion { |
1796 | #[inline ] |
1797 | fn from(value: u8) -> Self { |
1798 | Self(value) |
1799 | } |
1800 | } |
1801 | impl core::fmt::Debug for Motion { |
1802 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
1803 | let variants: [(u32, &str, &str); 2] = [ |
1804 | (Self::NORMAL.0.into(), "NORMAL" , "Normal" ), |
1805 | (Self::HINT.0.into(), "HINT" , "Hint" ), |
1806 | ]; |
1807 | pretty_print_enum(fmt, self.0.into(), &variants) |
1808 | } |
1809 | } |
1810 | |
1811 | /// Opcode for the MotionNotify event |
1812 | pub const MOTION_NOTIFY_EVENT: u8 = 6; |
1813 | /// a key was pressed. |
1814 | /// |
1815 | /// # Fields |
1816 | /// |
1817 | /// * `detail` - The keycode (a number representing a physical key on the keyboard) of the key |
1818 | /// which was pressed. |
1819 | /// * `time` - Time when the event was generated (in milliseconds). |
1820 | /// * `root` - The root window of `child`. |
1821 | /// * `same_screen` - Whether the `event` window is on the same screen as the `root` window. |
1822 | /// * `event_x` - If `same_screen` is true, this is the X coordinate relative to the `event` |
1823 | /// window's origin. Otherwise, `event_x` will be set to zero. |
1824 | /// * `event_y` - If `same_screen` is true, this is the Y coordinate relative to the `event` |
1825 | /// window's origin. Otherwise, `event_y` will be set to zero. |
1826 | /// * `root_x` - The X coordinate of the pointer relative to the `root` window at the time of |
1827 | /// the event. |
1828 | /// * `root_y` - The Y coordinate of the pointer relative to the `root` window at the time of |
1829 | /// the event. |
1830 | /// * `state` - The logical state of the pointer buttons and modifier keys just prior to the |
1831 | /// event. |
1832 | /// |
1833 | /// # See |
1834 | /// |
1835 | /// * `GrabKey`: request |
1836 | /// * `GrabKeyboard`: request |
1837 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
1838 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
1839 | pub struct MotionNotifyEvent { |
1840 | pub response_type: u8, |
1841 | pub detail: Motion, |
1842 | pub sequence: u16, |
1843 | pub time: Timestamp, |
1844 | pub root: Window, |
1845 | pub event: Window, |
1846 | pub child: Window, |
1847 | pub root_x: i16, |
1848 | pub root_y: i16, |
1849 | pub event_x: i16, |
1850 | pub event_y: i16, |
1851 | pub state: KeyButMask, |
1852 | pub same_screen: bool, |
1853 | } |
1854 | impl TryParse for MotionNotifyEvent { |
1855 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
1856 | let remaining = initial_value; |
1857 | let (response_type, remaining) = u8::try_parse(remaining)?; |
1858 | let (detail, remaining) = u8::try_parse(remaining)?; |
1859 | let (sequence, remaining) = u16::try_parse(remaining)?; |
1860 | let (time, remaining) = Timestamp::try_parse(remaining)?; |
1861 | let (root, remaining) = Window::try_parse(remaining)?; |
1862 | let (event, remaining) = Window::try_parse(remaining)?; |
1863 | let (child, remaining) = Window::try_parse(remaining)?; |
1864 | let (root_x, remaining) = i16::try_parse(remaining)?; |
1865 | let (root_y, remaining) = i16::try_parse(remaining)?; |
1866 | let (event_x, remaining) = i16::try_parse(remaining)?; |
1867 | let (event_y, remaining) = i16::try_parse(remaining)?; |
1868 | let (state, remaining) = u16::try_parse(remaining)?; |
1869 | let (same_screen, remaining) = bool::try_parse(remaining)?; |
1870 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
1871 | let detail = detail.into(); |
1872 | let state = state.into(); |
1873 | let result = MotionNotifyEvent { response_type, detail, sequence, time, root, event, child, root_x, root_y, event_x, event_y, state, same_screen }; |
1874 | let _ = remaining; |
1875 | let remaining = initial_value.get(32..) |
1876 | .ok_or(ParseError::InsufficientData)?; |
1877 | Ok((result, remaining)) |
1878 | } |
1879 | } |
1880 | impl Serialize for MotionNotifyEvent { |
1881 | type Bytes = [u8; 32]; |
1882 | fn serialize(&self) -> [u8; 32] { |
1883 | let response_type_bytes = self.response_type.serialize(); |
1884 | let detail_bytes = u8::from(self.detail).serialize(); |
1885 | let sequence_bytes = self.sequence.serialize(); |
1886 | let time_bytes = self.time.serialize(); |
1887 | let root_bytes = self.root.serialize(); |
1888 | let event_bytes = self.event.serialize(); |
1889 | let child_bytes = self.child.serialize(); |
1890 | let root_x_bytes = self.root_x.serialize(); |
1891 | let root_y_bytes = self.root_y.serialize(); |
1892 | let event_x_bytes = self.event_x.serialize(); |
1893 | let event_y_bytes = self.event_y.serialize(); |
1894 | let state_bytes = u16::from(self.state).serialize(); |
1895 | let same_screen_bytes = self.same_screen.serialize(); |
1896 | [ |
1897 | response_type_bytes[0], |
1898 | detail_bytes[0], |
1899 | sequence_bytes[0], |
1900 | sequence_bytes[1], |
1901 | time_bytes[0], |
1902 | time_bytes[1], |
1903 | time_bytes[2], |
1904 | time_bytes[3], |
1905 | root_bytes[0], |
1906 | root_bytes[1], |
1907 | root_bytes[2], |
1908 | root_bytes[3], |
1909 | event_bytes[0], |
1910 | event_bytes[1], |
1911 | event_bytes[2], |
1912 | event_bytes[3], |
1913 | child_bytes[0], |
1914 | child_bytes[1], |
1915 | child_bytes[2], |
1916 | child_bytes[3], |
1917 | root_x_bytes[0], |
1918 | root_x_bytes[1], |
1919 | root_y_bytes[0], |
1920 | root_y_bytes[1], |
1921 | event_x_bytes[0], |
1922 | event_x_bytes[1], |
1923 | event_y_bytes[0], |
1924 | event_y_bytes[1], |
1925 | state_bytes[0], |
1926 | state_bytes[1], |
1927 | same_screen_bytes[0], |
1928 | 0, |
1929 | ] |
1930 | } |
1931 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
1932 | bytes.reserve(32); |
1933 | self.response_type.serialize_into(bytes); |
1934 | u8::from(self.detail).serialize_into(bytes); |
1935 | self.sequence.serialize_into(bytes); |
1936 | self.time.serialize_into(bytes); |
1937 | self.root.serialize_into(bytes); |
1938 | self.event.serialize_into(bytes); |
1939 | self.child.serialize_into(bytes); |
1940 | self.root_x.serialize_into(bytes); |
1941 | self.root_y.serialize_into(bytes); |
1942 | self.event_x.serialize_into(bytes); |
1943 | self.event_y.serialize_into(bytes); |
1944 | u16::from(self.state).serialize_into(bytes); |
1945 | self.same_screen.serialize_into(bytes); |
1946 | bytes.extend_from_slice(&[0; 1]); |
1947 | } |
1948 | } |
1949 | impl From<&MotionNotifyEvent> for [u8; 32] { |
1950 | fn from(input: &MotionNotifyEvent) -> Self { |
1951 | let response_type_bytes = input.response_type.serialize(); |
1952 | let detail_bytes = u8::from(input.detail).serialize(); |
1953 | let sequence_bytes = input.sequence.serialize(); |
1954 | let time_bytes = input.time.serialize(); |
1955 | let root_bytes = input.root.serialize(); |
1956 | let event_bytes = input.event.serialize(); |
1957 | let child_bytes = input.child.serialize(); |
1958 | let root_x_bytes = input.root_x.serialize(); |
1959 | let root_y_bytes = input.root_y.serialize(); |
1960 | let event_x_bytes = input.event_x.serialize(); |
1961 | let event_y_bytes = input.event_y.serialize(); |
1962 | let state_bytes = u16::from(input.state).serialize(); |
1963 | let same_screen_bytes = input.same_screen.serialize(); |
1964 | [ |
1965 | response_type_bytes[0], |
1966 | detail_bytes[0], |
1967 | sequence_bytes[0], |
1968 | sequence_bytes[1], |
1969 | time_bytes[0], |
1970 | time_bytes[1], |
1971 | time_bytes[2], |
1972 | time_bytes[3], |
1973 | root_bytes[0], |
1974 | root_bytes[1], |
1975 | root_bytes[2], |
1976 | root_bytes[3], |
1977 | event_bytes[0], |
1978 | event_bytes[1], |
1979 | event_bytes[2], |
1980 | event_bytes[3], |
1981 | child_bytes[0], |
1982 | child_bytes[1], |
1983 | child_bytes[2], |
1984 | child_bytes[3], |
1985 | root_x_bytes[0], |
1986 | root_x_bytes[1], |
1987 | root_y_bytes[0], |
1988 | root_y_bytes[1], |
1989 | event_x_bytes[0], |
1990 | event_x_bytes[1], |
1991 | event_y_bytes[0], |
1992 | event_y_bytes[1], |
1993 | state_bytes[0], |
1994 | state_bytes[1], |
1995 | same_screen_bytes[0], |
1996 | 0, |
1997 | ] |
1998 | } |
1999 | } |
2000 | impl From<MotionNotifyEvent> for [u8; 32] { |
2001 | fn from(input: MotionNotifyEvent) -> Self { |
2002 | Self::from(&input) |
2003 | } |
2004 | } |
2005 | |
2006 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
2007 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
2008 | pub struct NotifyDetail(u8); |
2009 | impl NotifyDetail { |
2010 | pub const ANCESTOR: Self = Self(0); |
2011 | pub const VIRTUAL: Self = Self(1); |
2012 | pub const INFERIOR: Self = Self(2); |
2013 | pub const NONLINEAR: Self = Self(3); |
2014 | pub const NONLINEAR_VIRTUAL: Self = Self(4); |
2015 | pub const POINTER: Self = Self(5); |
2016 | pub const POINTER_ROOT: Self = Self(6); |
2017 | pub const NONE: Self = Self(7); |
2018 | } |
2019 | impl From<NotifyDetail> for u8 { |
2020 | #[inline ] |
2021 | fn from(input: NotifyDetail) -> Self { |
2022 | input.0 |
2023 | } |
2024 | } |
2025 | impl From<NotifyDetail> for Option<u8> { |
2026 | #[inline ] |
2027 | fn from(input: NotifyDetail) -> Self { |
2028 | Some(input.0) |
2029 | } |
2030 | } |
2031 | impl From<NotifyDetail> for u16 { |
2032 | #[inline ] |
2033 | fn from(input: NotifyDetail) -> Self { |
2034 | u16::from(input.0) |
2035 | } |
2036 | } |
2037 | impl From<NotifyDetail> for Option<u16> { |
2038 | #[inline ] |
2039 | fn from(input: NotifyDetail) -> Self { |
2040 | Some(u16::from(input.0)) |
2041 | } |
2042 | } |
2043 | impl From<NotifyDetail> for u32 { |
2044 | #[inline ] |
2045 | fn from(input: NotifyDetail) -> Self { |
2046 | u32::from(input.0) |
2047 | } |
2048 | } |
2049 | impl From<NotifyDetail> for Option<u32> { |
2050 | #[inline ] |
2051 | fn from(input: NotifyDetail) -> Self { |
2052 | Some(u32::from(input.0)) |
2053 | } |
2054 | } |
2055 | impl From<u8> for NotifyDetail { |
2056 | #[inline ] |
2057 | fn from(value: u8) -> Self { |
2058 | Self(value) |
2059 | } |
2060 | } |
2061 | impl core::fmt::Debug for NotifyDetail { |
2062 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
2063 | let variants: [(u32, &str, &str); 8] = [ |
2064 | (Self::ANCESTOR.0.into(), "ANCESTOR" , "Ancestor" ), |
2065 | (Self::VIRTUAL.0.into(), "VIRTUAL" , "Virtual" ), |
2066 | (Self::INFERIOR.0.into(), "INFERIOR" , "Inferior" ), |
2067 | (Self::NONLINEAR.0.into(), "NONLINEAR" , "Nonlinear" ), |
2068 | (Self::NONLINEAR_VIRTUAL.0.into(), "NONLINEAR_VIRTUAL" , "NonlinearVirtual" ), |
2069 | (Self::POINTER.0.into(), "POINTER" , "Pointer" ), |
2070 | (Self::POINTER_ROOT.0.into(), "POINTER_ROOT" , "PointerRoot" ), |
2071 | (Self::NONE.0.into(), "NONE" , "None" ), |
2072 | ]; |
2073 | pretty_print_enum(fmt, self.0.into(), &variants) |
2074 | } |
2075 | } |
2076 | |
2077 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
2078 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
2079 | pub struct NotifyMode(u8); |
2080 | impl NotifyMode { |
2081 | pub const NORMAL: Self = Self(0); |
2082 | pub const GRAB: Self = Self(1); |
2083 | pub const UNGRAB: Self = Self(2); |
2084 | pub const WHILE_GRABBED: Self = Self(3); |
2085 | } |
2086 | impl From<NotifyMode> for u8 { |
2087 | #[inline ] |
2088 | fn from(input: NotifyMode) -> Self { |
2089 | input.0 |
2090 | } |
2091 | } |
2092 | impl From<NotifyMode> for Option<u8> { |
2093 | #[inline ] |
2094 | fn from(input: NotifyMode) -> Self { |
2095 | Some(input.0) |
2096 | } |
2097 | } |
2098 | impl From<NotifyMode> for u16 { |
2099 | #[inline ] |
2100 | fn from(input: NotifyMode) -> Self { |
2101 | u16::from(input.0) |
2102 | } |
2103 | } |
2104 | impl From<NotifyMode> for Option<u16> { |
2105 | #[inline ] |
2106 | fn from(input: NotifyMode) -> Self { |
2107 | Some(u16::from(input.0)) |
2108 | } |
2109 | } |
2110 | impl From<NotifyMode> for u32 { |
2111 | #[inline ] |
2112 | fn from(input: NotifyMode) -> Self { |
2113 | u32::from(input.0) |
2114 | } |
2115 | } |
2116 | impl From<NotifyMode> for Option<u32> { |
2117 | #[inline ] |
2118 | fn from(input: NotifyMode) -> Self { |
2119 | Some(u32::from(input.0)) |
2120 | } |
2121 | } |
2122 | impl From<u8> for NotifyMode { |
2123 | #[inline ] |
2124 | fn from(value: u8) -> Self { |
2125 | Self(value) |
2126 | } |
2127 | } |
2128 | impl core::fmt::Debug for NotifyMode { |
2129 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
2130 | let variants: [(u32, &str, &str); 4] = [ |
2131 | (Self::NORMAL.0.into(), "NORMAL" , "Normal" ), |
2132 | (Self::GRAB.0.into(), "GRAB" , "Grab" ), |
2133 | (Self::UNGRAB.0.into(), "UNGRAB" , "Ungrab" ), |
2134 | (Self::WHILE_GRABBED.0.into(), "WHILE_GRABBED" , "WhileGrabbed" ), |
2135 | ]; |
2136 | pretty_print_enum(fmt, self.0.into(), &variants) |
2137 | } |
2138 | } |
2139 | |
2140 | /// Opcode for the EnterNotify event |
2141 | pub const ENTER_NOTIFY_EVENT: u8 = 7; |
2142 | /// the pointer is in a different window. |
2143 | /// |
2144 | /// # Fields |
2145 | /// |
2146 | /// * `event` - The window on which the event was generated. |
2147 | /// * `child` - If the `event` window has subwindows and the final pointer position is in one |
2148 | /// of them, then `child` is set to that subwindow, `XCB_WINDOW_NONE` otherwise. |
2149 | /// * `root` - The root window for the final cursor position. |
2150 | /// * `root_x` - The pointer X coordinate relative to `root`'s origin at the time of the event. |
2151 | /// * `root_y` - The pointer Y coordinate relative to `root`'s origin at the time of the event. |
2152 | /// * `event_x` - If `event` is on the same screen as `root`, this is the pointer X coordinate |
2153 | /// relative to the event window's origin. |
2154 | /// * `event_y` - If `event` is on the same screen as `root`, this is the pointer Y coordinate |
2155 | /// relative to the event window's origin. |
2156 | /// * `mode` - |
2157 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
2158 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
2159 | pub struct EnterNotifyEvent { |
2160 | pub response_type: u8, |
2161 | pub detail: NotifyDetail, |
2162 | pub sequence: u16, |
2163 | pub time: Timestamp, |
2164 | pub root: Window, |
2165 | pub event: Window, |
2166 | pub child: Window, |
2167 | pub root_x: i16, |
2168 | pub root_y: i16, |
2169 | pub event_x: i16, |
2170 | pub event_y: i16, |
2171 | pub state: KeyButMask, |
2172 | pub mode: NotifyMode, |
2173 | pub same_screen_focus: u8, |
2174 | } |
2175 | impl TryParse for EnterNotifyEvent { |
2176 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
2177 | let remaining = initial_value; |
2178 | let (response_type, remaining) = u8::try_parse(remaining)?; |
2179 | let (detail, remaining) = u8::try_parse(remaining)?; |
2180 | let (sequence, remaining) = u16::try_parse(remaining)?; |
2181 | let (time, remaining) = Timestamp::try_parse(remaining)?; |
2182 | let (root, remaining) = Window::try_parse(remaining)?; |
2183 | let (event, remaining) = Window::try_parse(remaining)?; |
2184 | let (child, remaining) = Window::try_parse(remaining)?; |
2185 | let (root_x, remaining) = i16::try_parse(remaining)?; |
2186 | let (root_y, remaining) = i16::try_parse(remaining)?; |
2187 | let (event_x, remaining) = i16::try_parse(remaining)?; |
2188 | let (event_y, remaining) = i16::try_parse(remaining)?; |
2189 | let (state, remaining) = u16::try_parse(remaining)?; |
2190 | let (mode, remaining) = u8::try_parse(remaining)?; |
2191 | let (same_screen_focus, remaining) = u8::try_parse(remaining)?; |
2192 | let detail = detail.into(); |
2193 | let state = state.into(); |
2194 | let mode = mode.into(); |
2195 | let result = EnterNotifyEvent { response_type, detail, sequence, time, root, event, child, root_x, root_y, event_x, event_y, state, mode, same_screen_focus }; |
2196 | let _ = remaining; |
2197 | let remaining = initial_value.get(32..) |
2198 | .ok_or(ParseError::InsufficientData)?; |
2199 | Ok((result, remaining)) |
2200 | } |
2201 | } |
2202 | impl Serialize for EnterNotifyEvent { |
2203 | type Bytes = [u8; 32]; |
2204 | fn serialize(&self) -> [u8; 32] { |
2205 | let response_type_bytes = self.response_type.serialize(); |
2206 | let detail_bytes = u8::from(self.detail).serialize(); |
2207 | let sequence_bytes = self.sequence.serialize(); |
2208 | let time_bytes = self.time.serialize(); |
2209 | let root_bytes = self.root.serialize(); |
2210 | let event_bytes = self.event.serialize(); |
2211 | let child_bytes = self.child.serialize(); |
2212 | let root_x_bytes = self.root_x.serialize(); |
2213 | let root_y_bytes = self.root_y.serialize(); |
2214 | let event_x_bytes = self.event_x.serialize(); |
2215 | let event_y_bytes = self.event_y.serialize(); |
2216 | let state_bytes = u16::from(self.state).serialize(); |
2217 | let mode_bytes = u8::from(self.mode).serialize(); |
2218 | let same_screen_focus_bytes = self.same_screen_focus.serialize(); |
2219 | [ |
2220 | response_type_bytes[0], |
2221 | detail_bytes[0], |
2222 | sequence_bytes[0], |
2223 | sequence_bytes[1], |
2224 | time_bytes[0], |
2225 | time_bytes[1], |
2226 | time_bytes[2], |
2227 | time_bytes[3], |
2228 | root_bytes[0], |
2229 | root_bytes[1], |
2230 | root_bytes[2], |
2231 | root_bytes[3], |
2232 | event_bytes[0], |
2233 | event_bytes[1], |
2234 | event_bytes[2], |
2235 | event_bytes[3], |
2236 | child_bytes[0], |
2237 | child_bytes[1], |
2238 | child_bytes[2], |
2239 | child_bytes[3], |
2240 | root_x_bytes[0], |
2241 | root_x_bytes[1], |
2242 | root_y_bytes[0], |
2243 | root_y_bytes[1], |
2244 | event_x_bytes[0], |
2245 | event_x_bytes[1], |
2246 | event_y_bytes[0], |
2247 | event_y_bytes[1], |
2248 | state_bytes[0], |
2249 | state_bytes[1], |
2250 | mode_bytes[0], |
2251 | same_screen_focus_bytes[0], |
2252 | ] |
2253 | } |
2254 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
2255 | bytes.reserve(32); |
2256 | self.response_type.serialize_into(bytes); |
2257 | u8::from(self.detail).serialize_into(bytes); |
2258 | self.sequence.serialize_into(bytes); |
2259 | self.time.serialize_into(bytes); |
2260 | self.root.serialize_into(bytes); |
2261 | self.event.serialize_into(bytes); |
2262 | self.child.serialize_into(bytes); |
2263 | self.root_x.serialize_into(bytes); |
2264 | self.root_y.serialize_into(bytes); |
2265 | self.event_x.serialize_into(bytes); |
2266 | self.event_y.serialize_into(bytes); |
2267 | u16::from(self.state).serialize_into(bytes); |
2268 | u8::from(self.mode).serialize_into(bytes); |
2269 | self.same_screen_focus.serialize_into(bytes); |
2270 | } |
2271 | } |
2272 | impl From<&EnterNotifyEvent> for [u8; 32] { |
2273 | fn from(input: &EnterNotifyEvent) -> Self { |
2274 | let response_type_bytes = input.response_type.serialize(); |
2275 | let detail_bytes = u8::from(input.detail).serialize(); |
2276 | let sequence_bytes = input.sequence.serialize(); |
2277 | let time_bytes = input.time.serialize(); |
2278 | let root_bytes = input.root.serialize(); |
2279 | let event_bytes = input.event.serialize(); |
2280 | let child_bytes = input.child.serialize(); |
2281 | let root_x_bytes = input.root_x.serialize(); |
2282 | let root_y_bytes = input.root_y.serialize(); |
2283 | let event_x_bytes = input.event_x.serialize(); |
2284 | let event_y_bytes = input.event_y.serialize(); |
2285 | let state_bytes = u16::from(input.state).serialize(); |
2286 | let mode_bytes = u8::from(input.mode).serialize(); |
2287 | let same_screen_focus_bytes = input.same_screen_focus.serialize(); |
2288 | [ |
2289 | response_type_bytes[0], |
2290 | detail_bytes[0], |
2291 | sequence_bytes[0], |
2292 | sequence_bytes[1], |
2293 | time_bytes[0], |
2294 | time_bytes[1], |
2295 | time_bytes[2], |
2296 | time_bytes[3], |
2297 | root_bytes[0], |
2298 | root_bytes[1], |
2299 | root_bytes[2], |
2300 | root_bytes[3], |
2301 | event_bytes[0], |
2302 | event_bytes[1], |
2303 | event_bytes[2], |
2304 | event_bytes[3], |
2305 | child_bytes[0], |
2306 | child_bytes[1], |
2307 | child_bytes[2], |
2308 | child_bytes[3], |
2309 | root_x_bytes[0], |
2310 | root_x_bytes[1], |
2311 | root_y_bytes[0], |
2312 | root_y_bytes[1], |
2313 | event_x_bytes[0], |
2314 | event_x_bytes[1], |
2315 | event_y_bytes[0], |
2316 | event_y_bytes[1], |
2317 | state_bytes[0], |
2318 | state_bytes[1], |
2319 | mode_bytes[0], |
2320 | same_screen_focus_bytes[0], |
2321 | ] |
2322 | } |
2323 | } |
2324 | impl From<EnterNotifyEvent> for [u8; 32] { |
2325 | fn from(input: EnterNotifyEvent) -> Self { |
2326 | Self::from(&input) |
2327 | } |
2328 | } |
2329 | |
2330 | /// Opcode for the LeaveNotify event |
2331 | pub const LEAVE_NOTIFY_EVENT: u8 = 8; |
2332 | pub type LeaveNotifyEvent = EnterNotifyEvent; |
2333 | |
2334 | /// Opcode for the FocusIn event |
2335 | pub const FOCUS_IN_EVENT: u8 = 9; |
2336 | /// NOT YET DOCUMENTED. |
2337 | /// |
2338 | /// # Fields |
2339 | /// |
2340 | /// * `event` - The window on which the focus event was generated. This is the window used by |
2341 | /// the X server to report the event. |
2342 | /// * `detail` - |
2343 | /// * `mode` - |
2344 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
2345 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
2346 | pub struct FocusInEvent { |
2347 | pub response_type: u8, |
2348 | pub detail: NotifyDetail, |
2349 | pub sequence: u16, |
2350 | pub event: Window, |
2351 | pub mode: NotifyMode, |
2352 | } |
2353 | impl TryParse for FocusInEvent { |
2354 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
2355 | let remaining: &[u8] = initial_value; |
2356 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
2357 | let (detail: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
2358 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2359 | let (event: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
2360 | let (mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
2361 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
2362 | let detail: NotifyDetail = detail.into(); |
2363 | let mode: NotifyMode = mode.into(); |
2364 | let result: FocusInEvent = FocusInEvent { response_type, detail, sequence, event, mode }; |
2365 | let _ = remaining; |
2366 | let remaining: &[u8] = initial_value.get(32..) |
2367 | .ok_or(err:ParseError::InsufficientData)?; |
2368 | Ok((result, remaining)) |
2369 | } |
2370 | } |
2371 | impl Serialize for FocusInEvent { |
2372 | type Bytes = [u8; 12]; |
2373 | fn serialize(&self) -> [u8; 12] { |
2374 | let response_type_bytes = self.response_type.serialize(); |
2375 | let detail_bytes = u8::from(self.detail).serialize(); |
2376 | let sequence_bytes = self.sequence.serialize(); |
2377 | let event_bytes = self.event.serialize(); |
2378 | let mode_bytes = u8::from(self.mode).serialize(); |
2379 | [ |
2380 | response_type_bytes[0], |
2381 | detail_bytes[0], |
2382 | sequence_bytes[0], |
2383 | sequence_bytes[1], |
2384 | event_bytes[0], |
2385 | event_bytes[1], |
2386 | event_bytes[2], |
2387 | event_bytes[3], |
2388 | mode_bytes[0], |
2389 | 0, |
2390 | 0, |
2391 | 0, |
2392 | ] |
2393 | } |
2394 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
2395 | bytes.reserve(12); |
2396 | self.response_type.serialize_into(bytes); |
2397 | u8::from(self.detail).serialize_into(bytes); |
2398 | self.sequence.serialize_into(bytes); |
2399 | self.event.serialize_into(bytes); |
2400 | u8::from(self.mode).serialize_into(bytes); |
2401 | bytes.extend_from_slice(&[0; 3]); |
2402 | } |
2403 | } |
2404 | impl From<&FocusInEvent> for [u8; 32] { |
2405 | fn from(input: &FocusInEvent) -> Self { |
2406 | let response_type_bytes = input.response_type.serialize(); |
2407 | let detail_bytes = u8::from(input.detail).serialize(); |
2408 | let sequence_bytes = input.sequence.serialize(); |
2409 | let event_bytes = input.event.serialize(); |
2410 | let mode_bytes = u8::from(input.mode).serialize(); |
2411 | [ |
2412 | response_type_bytes[0], |
2413 | detail_bytes[0], |
2414 | sequence_bytes[0], |
2415 | sequence_bytes[1], |
2416 | event_bytes[0], |
2417 | event_bytes[1], |
2418 | event_bytes[2], |
2419 | event_bytes[3], |
2420 | mode_bytes[0], |
2421 | 0, |
2422 | 0, |
2423 | 0, |
2424 | // trailing padding |
2425 | 0, |
2426 | 0, |
2427 | 0, |
2428 | 0, |
2429 | 0, |
2430 | 0, |
2431 | 0, |
2432 | 0, |
2433 | 0, |
2434 | 0, |
2435 | 0, |
2436 | 0, |
2437 | 0, |
2438 | 0, |
2439 | 0, |
2440 | 0, |
2441 | 0, |
2442 | 0, |
2443 | 0, |
2444 | 0, |
2445 | ] |
2446 | } |
2447 | } |
2448 | impl From<FocusInEvent> for [u8; 32] { |
2449 | fn from(input: FocusInEvent) -> Self { |
2450 | Self::from(&input) |
2451 | } |
2452 | } |
2453 | |
2454 | /// Opcode for the FocusOut event |
2455 | pub const FOCUS_OUT_EVENT: u8 = 10; |
2456 | pub type FocusOutEvent = FocusInEvent; |
2457 | |
2458 | /// Opcode for the KeymapNotify event |
2459 | pub const KEYMAP_NOTIFY_EVENT: u8 = 11; |
2460 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
2461 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
2462 | pub struct KeymapNotifyEvent { |
2463 | pub response_type: u8, |
2464 | pub keys: [u8; 31], |
2465 | } |
2466 | impl TryParse for KeymapNotifyEvent { |
2467 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
2468 | let remaining: &[u8] = initial_value; |
2469 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
2470 | let (keys: [u8; 31], remaining: &[u8]) = crate::x11_utils::parse_u8_array::<31>(data:remaining)?; |
2471 | let result: KeymapNotifyEvent = KeymapNotifyEvent { response_type, keys }; |
2472 | let _ = remaining; |
2473 | let remaining: &[u8] = initial_value.get(32..) |
2474 | .ok_or(err:ParseError::InsufficientData)?; |
2475 | Ok((result, remaining)) |
2476 | } |
2477 | } |
2478 | impl Serialize for KeymapNotifyEvent { |
2479 | type Bytes = [u8; 32]; |
2480 | fn serialize(&self) -> [u8; 32] { |
2481 | let response_type_bytes = self.response_type.serialize(); |
2482 | [ |
2483 | response_type_bytes[0], |
2484 | self.keys[0], |
2485 | self.keys[1], |
2486 | self.keys[2], |
2487 | self.keys[3], |
2488 | self.keys[4], |
2489 | self.keys[5], |
2490 | self.keys[6], |
2491 | self.keys[7], |
2492 | self.keys[8], |
2493 | self.keys[9], |
2494 | self.keys[10], |
2495 | self.keys[11], |
2496 | self.keys[12], |
2497 | self.keys[13], |
2498 | self.keys[14], |
2499 | self.keys[15], |
2500 | self.keys[16], |
2501 | self.keys[17], |
2502 | self.keys[18], |
2503 | self.keys[19], |
2504 | self.keys[20], |
2505 | self.keys[21], |
2506 | self.keys[22], |
2507 | self.keys[23], |
2508 | self.keys[24], |
2509 | self.keys[25], |
2510 | self.keys[26], |
2511 | self.keys[27], |
2512 | self.keys[28], |
2513 | self.keys[29], |
2514 | self.keys[30], |
2515 | ] |
2516 | } |
2517 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
2518 | bytes.reserve(32); |
2519 | self.response_type.serialize_into(bytes); |
2520 | bytes.extend_from_slice(&self.keys); |
2521 | } |
2522 | } |
2523 | impl From<&KeymapNotifyEvent> for [u8; 32] { |
2524 | fn from(input: &KeymapNotifyEvent) -> Self { |
2525 | let response_type_bytes = input.response_type.serialize(); |
2526 | [ |
2527 | response_type_bytes[0], |
2528 | input.keys[0], |
2529 | input.keys[1], |
2530 | input.keys[2], |
2531 | input.keys[3], |
2532 | input.keys[4], |
2533 | input.keys[5], |
2534 | input.keys[6], |
2535 | input.keys[7], |
2536 | input.keys[8], |
2537 | input.keys[9], |
2538 | input.keys[10], |
2539 | input.keys[11], |
2540 | input.keys[12], |
2541 | input.keys[13], |
2542 | input.keys[14], |
2543 | input.keys[15], |
2544 | input.keys[16], |
2545 | input.keys[17], |
2546 | input.keys[18], |
2547 | input.keys[19], |
2548 | input.keys[20], |
2549 | input.keys[21], |
2550 | input.keys[22], |
2551 | input.keys[23], |
2552 | input.keys[24], |
2553 | input.keys[25], |
2554 | input.keys[26], |
2555 | input.keys[27], |
2556 | input.keys[28], |
2557 | input.keys[29], |
2558 | input.keys[30], |
2559 | ] |
2560 | } |
2561 | } |
2562 | impl From<KeymapNotifyEvent> for [u8; 32] { |
2563 | fn from(input: KeymapNotifyEvent) -> Self { |
2564 | Self::from(&input) |
2565 | } |
2566 | } |
2567 | |
2568 | /// Opcode for the Expose event |
2569 | pub const EXPOSE_EVENT: u8 = 12; |
2570 | /// NOT YET DOCUMENTED. |
2571 | /// |
2572 | /// # Fields |
2573 | /// |
2574 | /// * `window` - The exposed (damaged) window. |
2575 | /// * `x` - The X coordinate of the left-upper corner of the exposed rectangle, relative to |
2576 | /// the `window`'s origin. |
2577 | /// * `y` - The Y coordinate of the left-upper corner of the exposed rectangle, relative to |
2578 | /// the `window`'s origin. |
2579 | /// * `width` - The width of the exposed rectangle. |
2580 | /// * `height` - The height of the exposed rectangle. |
2581 | /// * `count` - The amount of `Expose` events following this one. Simple applications that do |
2582 | /// not want to optimize redisplay by distinguishing between subareas of its window |
2583 | /// can just ignore all Expose events with nonzero counts and perform full |
2584 | /// redisplays on events with zero counts. |
2585 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
2586 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
2587 | pub struct ExposeEvent { |
2588 | pub response_type: u8, |
2589 | pub sequence: u16, |
2590 | pub window: Window, |
2591 | pub x: u16, |
2592 | pub y: u16, |
2593 | pub width: u16, |
2594 | pub height: u16, |
2595 | pub count: u16, |
2596 | } |
2597 | impl TryParse for ExposeEvent { |
2598 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
2599 | let remaining: &[u8] = initial_value; |
2600 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
2601 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
2602 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2603 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
2604 | let (x: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2605 | let (y: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2606 | let (width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2607 | let (height: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2608 | let (count: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2609 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
2610 | let result: ExposeEvent = ExposeEvent { response_type, sequence, window, x, y, width, height, count }; |
2611 | let _ = remaining; |
2612 | let remaining: &[u8] = initial_value.get(32..) |
2613 | .ok_or(err:ParseError::InsufficientData)?; |
2614 | Ok((result, remaining)) |
2615 | } |
2616 | } |
2617 | impl Serialize for ExposeEvent { |
2618 | type Bytes = [u8; 20]; |
2619 | fn serialize(&self) -> [u8; 20] { |
2620 | let response_type_bytes = self.response_type.serialize(); |
2621 | let sequence_bytes = self.sequence.serialize(); |
2622 | let window_bytes = self.window.serialize(); |
2623 | let x_bytes = self.x.serialize(); |
2624 | let y_bytes = self.y.serialize(); |
2625 | let width_bytes = self.width.serialize(); |
2626 | let height_bytes = self.height.serialize(); |
2627 | let count_bytes = self.count.serialize(); |
2628 | [ |
2629 | response_type_bytes[0], |
2630 | 0, |
2631 | sequence_bytes[0], |
2632 | sequence_bytes[1], |
2633 | window_bytes[0], |
2634 | window_bytes[1], |
2635 | window_bytes[2], |
2636 | window_bytes[3], |
2637 | x_bytes[0], |
2638 | x_bytes[1], |
2639 | y_bytes[0], |
2640 | y_bytes[1], |
2641 | width_bytes[0], |
2642 | width_bytes[1], |
2643 | height_bytes[0], |
2644 | height_bytes[1], |
2645 | count_bytes[0], |
2646 | count_bytes[1], |
2647 | 0, |
2648 | 0, |
2649 | ] |
2650 | } |
2651 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
2652 | bytes.reserve(20); |
2653 | self.response_type.serialize_into(bytes); |
2654 | bytes.extend_from_slice(&[0; 1]); |
2655 | self.sequence.serialize_into(bytes); |
2656 | self.window.serialize_into(bytes); |
2657 | self.x.serialize_into(bytes); |
2658 | self.y.serialize_into(bytes); |
2659 | self.width.serialize_into(bytes); |
2660 | self.height.serialize_into(bytes); |
2661 | self.count.serialize_into(bytes); |
2662 | bytes.extend_from_slice(&[0; 2]); |
2663 | } |
2664 | } |
2665 | impl From<&ExposeEvent> for [u8; 32] { |
2666 | fn from(input: &ExposeEvent) -> Self { |
2667 | let response_type_bytes = input.response_type.serialize(); |
2668 | let sequence_bytes = input.sequence.serialize(); |
2669 | let window_bytes = input.window.serialize(); |
2670 | let x_bytes = input.x.serialize(); |
2671 | let y_bytes = input.y.serialize(); |
2672 | let width_bytes = input.width.serialize(); |
2673 | let height_bytes = input.height.serialize(); |
2674 | let count_bytes = input.count.serialize(); |
2675 | [ |
2676 | response_type_bytes[0], |
2677 | 0, |
2678 | sequence_bytes[0], |
2679 | sequence_bytes[1], |
2680 | window_bytes[0], |
2681 | window_bytes[1], |
2682 | window_bytes[2], |
2683 | window_bytes[3], |
2684 | x_bytes[0], |
2685 | x_bytes[1], |
2686 | y_bytes[0], |
2687 | y_bytes[1], |
2688 | width_bytes[0], |
2689 | width_bytes[1], |
2690 | height_bytes[0], |
2691 | height_bytes[1], |
2692 | count_bytes[0], |
2693 | count_bytes[1], |
2694 | 0, |
2695 | 0, |
2696 | // trailing padding |
2697 | 0, |
2698 | 0, |
2699 | 0, |
2700 | 0, |
2701 | 0, |
2702 | 0, |
2703 | 0, |
2704 | 0, |
2705 | 0, |
2706 | 0, |
2707 | 0, |
2708 | 0, |
2709 | ] |
2710 | } |
2711 | } |
2712 | impl From<ExposeEvent> for [u8; 32] { |
2713 | fn from(input: ExposeEvent) -> Self { |
2714 | Self::from(&input) |
2715 | } |
2716 | } |
2717 | |
2718 | /// Opcode for the GraphicsExposure event |
2719 | pub const GRAPHICS_EXPOSURE_EVENT: u8 = 13; |
2720 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
2721 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
2722 | pub struct GraphicsExposureEvent { |
2723 | pub response_type: u8, |
2724 | pub sequence: u16, |
2725 | pub drawable: Drawable, |
2726 | pub x: u16, |
2727 | pub y: u16, |
2728 | pub width: u16, |
2729 | pub height: u16, |
2730 | pub minor_opcode: u16, |
2731 | pub count: u16, |
2732 | pub major_opcode: u8, |
2733 | } |
2734 | impl TryParse for GraphicsExposureEvent { |
2735 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
2736 | let remaining: &[u8] = initial_value; |
2737 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
2738 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
2739 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2740 | let (drawable: u32, remaining: &[u8]) = Drawable::try_parse(remaining)?; |
2741 | let (x: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2742 | let (y: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2743 | let (width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2744 | let (height: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2745 | let (minor_opcode: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2746 | let (count: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2747 | let (major_opcode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
2748 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
2749 | let result: GraphicsExposureEvent = GraphicsExposureEvent { response_type, sequence, drawable, x, y, width, height, minor_opcode, count, major_opcode }; |
2750 | let _ = remaining; |
2751 | let remaining: &[u8] = initial_value.get(32..) |
2752 | .ok_or(err:ParseError::InsufficientData)?; |
2753 | Ok((result, remaining)) |
2754 | } |
2755 | } |
2756 | impl Serialize for GraphicsExposureEvent { |
2757 | type Bytes = [u8; 24]; |
2758 | fn serialize(&self) -> [u8; 24] { |
2759 | let response_type_bytes = self.response_type.serialize(); |
2760 | let sequence_bytes = self.sequence.serialize(); |
2761 | let drawable_bytes = self.drawable.serialize(); |
2762 | let x_bytes = self.x.serialize(); |
2763 | let y_bytes = self.y.serialize(); |
2764 | let width_bytes = self.width.serialize(); |
2765 | let height_bytes = self.height.serialize(); |
2766 | let minor_opcode_bytes = self.minor_opcode.serialize(); |
2767 | let count_bytes = self.count.serialize(); |
2768 | let major_opcode_bytes = self.major_opcode.serialize(); |
2769 | [ |
2770 | response_type_bytes[0], |
2771 | 0, |
2772 | sequence_bytes[0], |
2773 | sequence_bytes[1], |
2774 | drawable_bytes[0], |
2775 | drawable_bytes[1], |
2776 | drawable_bytes[2], |
2777 | drawable_bytes[3], |
2778 | x_bytes[0], |
2779 | x_bytes[1], |
2780 | y_bytes[0], |
2781 | y_bytes[1], |
2782 | width_bytes[0], |
2783 | width_bytes[1], |
2784 | height_bytes[0], |
2785 | height_bytes[1], |
2786 | minor_opcode_bytes[0], |
2787 | minor_opcode_bytes[1], |
2788 | count_bytes[0], |
2789 | count_bytes[1], |
2790 | major_opcode_bytes[0], |
2791 | 0, |
2792 | 0, |
2793 | 0, |
2794 | ] |
2795 | } |
2796 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
2797 | bytes.reserve(24); |
2798 | self.response_type.serialize_into(bytes); |
2799 | bytes.extend_from_slice(&[0; 1]); |
2800 | self.sequence.serialize_into(bytes); |
2801 | self.drawable.serialize_into(bytes); |
2802 | self.x.serialize_into(bytes); |
2803 | self.y.serialize_into(bytes); |
2804 | self.width.serialize_into(bytes); |
2805 | self.height.serialize_into(bytes); |
2806 | self.minor_opcode.serialize_into(bytes); |
2807 | self.count.serialize_into(bytes); |
2808 | self.major_opcode.serialize_into(bytes); |
2809 | bytes.extend_from_slice(&[0; 3]); |
2810 | } |
2811 | } |
2812 | impl From<&GraphicsExposureEvent> for [u8; 32] { |
2813 | fn from(input: &GraphicsExposureEvent) -> Self { |
2814 | let response_type_bytes = input.response_type.serialize(); |
2815 | let sequence_bytes = input.sequence.serialize(); |
2816 | let drawable_bytes = input.drawable.serialize(); |
2817 | let x_bytes = input.x.serialize(); |
2818 | let y_bytes = input.y.serialize(); |
2819 | let width_bytes = input.width.serialize(); |
2820 | let height_bytes = input.height.serialize(); |
2821 | let minor_opcode_bytes = input.minor_opcode.serialize(); |
2822 | let count_bytes = input.count.serialize(); |
2823 | let major_opcode_bytes = input.major_opcode.serialize(); |
2824 | [ |
2825 | response_type_bytes[0], |
2826 | 0, |
2827 | sequence_bytes[0], |
2828 | sequence_bytes[1], |
2829 | drawable_bytes[0], |
2830 | drawable_bytes[1], |
2831 | drawable_bytes[2], |
2832 | drawable_bytes[3], |
2833 | x_bytes[0], |
2834 | x_bytes[1], |
2835 | y_bytes[0], |
2836 | y_bytes[1], |
2837 | width_bytes[0], |
2838 | width_bytes[1], |
2839 | height_bytes[0], |
2840 | height_bytes[1], |
2841 | minor_opcode_bytes[0], |
2842 | minor_opcode_bytes[1], |
2843 | count_bytes[0], |
2844 | count_bytes[1], |
2845 | major_opcode_bytes[0], |
2846 | 0, |
2847 | 0, |
2848 | 0, |
2849 | // trailing padding |
2850 | 0, |
2851 | 0, |
2852 | 0, |
2853 | 0, |
2854 | 0, |
2855 | 0, |
2856 | 0, |
2857 | 0, |
2858 | ] |
2859 | } |
2860 | } |
2861 | impl From<GraphicsExposureEvent> for [u8; 32] { |
2862 | fn from(input: GraphicsExposureEvent) -> Self { |
2863 | Self::from(&input) |
2864 | } |
2865 | } |
2866 | |
2867 | /// Opcode for the NoExposure event |
2868 | pub const NO_EXPOSURE_EVENT: u8 = 14; |
2869 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
2870 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
2871 | pub struct NoExposureEvent { |
2872 | pub response_type: u8, |
2873 | pub sequence: u16, |
2874 | pub drawable: Drawable, |
2875 | pub minor_opcode: u16, |
2876 | pub major_opcode: u8, |
2877 | } |
2878 | impl TryParse for NoExposureEvent { |
2879 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
2880 | let remaining: &[u8] = initial_value; |
2881 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
2882 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
2883 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2884 | let (drawable: u32, remaining: &[u8]) = Drawable::try_parse(remaining)?; |
2885 | let (minor_opcode: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
2886 | let (major_opcode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
2887 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
2888 | let result: NoExposureEvent = NoExposureEvent { response_type, sequence, drawable, minor_opcode, major_opcode }; |
2889 | let _ = remaining; |
2890 | let remaining: &[u8] = initial_value.get(32..) |
2891 | .ok_or(err:ParseError::InsufficientData)?; |
2892 | Ok((result, remaining)) |
2893 | } |
2894 | } |
2895 | impl Serialize for NoExposureEvent { |
2896 | type Bytes = [u8; 12]; |
2897 | fn serialize(&self) -> [u8; 12] { |
2898 | let response_type_bytes = self.response_type.serialize(); |
2899 | let sequence_bytes = self.sequence.serialize(); |
2900 | let drawable_bytes = self.drawable.serialize(); |
2901 | let minor_opcode_bytes = self.minor_opcode.serialize(); |
2902 | let major_opcode_bytes = self.major_opcode.serialize(); |
2903 | [ |
2904 | response_type_bytes[0], |
2905 | 0, |
2906 | sequence_bytes[0], |
2907 | sequence_bytes[1], |
2908 | drawable_bytes[0], |
2909 | drawable_bytes[1], |
2910 | drawable_bytes[2], |
2911 | drawable_bytes[3], |
2912 | minor_opcode_bytes[0], |
2913 | minor_opcode_bytes[1], |
2914 | major_opcode_bytes[0], |
2915 | 0, |
2916 | ] |
2917 | } |
2918 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
2919 | bytes.reserve(12); |
2920 | self.response_type.serialize_into(bytes); |
2921 | bytes.extend_from_slice(&[0; 1]); |
2922 | self.sequence.serialize_into(bytes); |
2923 | self.drawable.serialize_into(bytes); |
2924 | self.minor_opcode.serialize_into(bytes); |
2925 | self.major_opcode.serialize_into(bytes); |
2926 | bytes.extend_from_slice(&[0; 1]); |
2927 | } |
2928 | } |
2929 | impl From<&NoExposureEvent> for [u8; 32] { |
2930 | fn from(input: &NoExposureEvent) -> Self { |
2931 | let response_type_bytes = input.response_type.serialize(); |
2932 | let sequence_bytes = input.sequence.serialize(); |
2933 | let drawable_bytes = input.drawable.serialize(); |
2934 | let minor_opcode_bytes = input.minor_opcode.serialize(); |
2935 | let major_opcode_bytes = input.major_opcode.serialize(); |
2936 | [ |
2937 | response_type_bytes[0], |
2938 | 0, |
2939 | sequence_bytes[0], |
2940 | sequence_bytes[1], |
2941 | drawable_bytes[0], |
2942 | drawable_bytes[1], |
2943 | drawable_bytes[2], |
2944 | drawable_bytes[3], |
2945 | minor_opcode_bytes[0], |
2946 | minor_opcode_bytes[1], |
2947 | major_opcode_bytes[0], |
2948 | 0, |
2949 | // trailing padding |
2950 | 0, |
2951 | 0, |
2952 | 0, |
2953 | 0, |
2954 | 0, |
2955 | 0, |
2956 | 0, |
2957 | 0, |
2958 | 0, |
2959 | 0, |
2960 | 0, |
2961 | 0, |
2962 | 0, |
2963 | 0, |
2964 | 0, |
2965 | 0, |
2966 | 0, |
2967 | 0, |
2968 | 0, |
2969 | 0, |
2970 | ] |
2971 | } |
2972 | } |
2973 | impl From<NoExposureEvent> for [u8; 32] { |
2974 | fn from(input: NoExposureEvent) -> Self { |
2975 | Self::from(&input) |
2976 | } |
2977 | } |
2978 | |
2979 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
2980 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
2981 | pub struct Visibility(u8); |
2982 | impl Visibility { |
2983 | pub const UNOBSCURED: Self = Self(0); |
2984 | pub const PARTIALLY_OBSCURED: Self = Self(1); |
2985 | pub const FULLY_OBSCURED: Self = Self(2); |
2986 | } |
2987 | impl From<Visibility> for u8 { |
2988 | #[inline ] |
2989 | fn from(input: Visibility) -> Self { |
2990 | input.0 |
2991 | } |
2992 | } |
2993 | impl From<Visibility> for Option<u8> { |
2994 | #[inline ] |
2995 | fn from(input: Visibility) -> Self { |
2996 | Some(input.0) |
2997 | } |
2998 | } |
2999 | impl From<Visibility> for u16 { |
3000 | #[inline ] |
3001 | fn from(input: Visibility) -> Self { |
3002 | u16::from(input.0) |
3003 | } |
3004 | } |
3005 | impl From<Visibility> for Option<u16> { |
3006 | #[inline ] |
3007 | fn from(input: Visibility) -> Self { |
3008 | Some(u16::from(input.0)) |
3009 | } |
3010 | } |
3011 | impl From<Visibility> for u32 { |
3012 | #[inline ] |
3013 | fn from(input: Visibility) -> Self { |
3014 | u32::from(input.0) |
3015 | } |
3016 | } |
3017 | impl From<Visibility> for Option<u32> { |
3018 | #[inline ] |
3019 | fn from(input: Visibility) -> Self { |
3020 | Some(u32::from(input.0)) |
3021 | } |
3022 | } |
3023 | impl From<u8> for Visibility { |
3024 | #[inline ] |
3025 | fn from(value: u8) -> Self { |
3026 | Self(value) |
3027 | } |
3028 | } |
3029 | impl core::fmt::Debug for Visibility { |
3030 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
3031 | let variants: [(u32, &str, &str); 3] = [ |
3032 | (Self::UNOBSCURED.0.into(), "UNOBSCURED" , "Unobscured" ), |
3033 | (Self::PARTIALLY_OBSCURED.0.into(), "PARTIALLY_OBSCURED" , "PartiallyObscured" ), |
3034 | (Self::FULLY_OBSCURED.0.into(), "FULLY_OBSCURED" , "FullyObscured" ), |
3035 | ]; |
3036 | pretty_print_enum(fmt, self.0.into(), &variants) |
3037 | } |
3038 | } |
3039 | |
3040 | /// Opcode for the VisibilityNotify event |
3041 | pub const VISIBILITY_NOTIFY_EVENT: u8 = 15; |
3042 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
3043 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
3044 | pub struct VisibilityNotifyEvent { |
3045 | pub response_type: u8, |
3046 | pub sequence: u16, |
3047 | pub window: Window, |
3048 | pub state: Visibility, |
3049 | } |
3050 | impl TryParse for VisibilityNotifyEvent { |
3051 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
3052 | let remaining: &[u8] = initial_value; |
3053 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
3054 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
3055 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3056 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3057 | let (state: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
3058 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
3059 | let state: Visibility = state.into(); |
3060 | let result: VisibilityNotifyEvent = VisibilityNotifyEvent { response_type, sequence, window, state }; |
3061 | let _ = remaining; |
3062 | let remaining: &[u8] = initial_value.get(32..) |
3063 | .ok_or(err:ParseError::InsufficientData)?; |
3064 | Ok((result, remaining)) |
3065 | } |
3066 | } |
3067 | impl Serialize for VisibilityNotifyEvent { |
3068 | type Bytes = [u8; 12]; |
3069 | fn serialize(&self) -> [u8; 12] { |
3070 | let response_type_bytes = self.response_type.serialize(); |
3071 | let sequence_bytes = self.sequence.serialize(); |
3072 | let window_bytes = self.window.serialize(); |
3073 | let state_bytes = u8::from(self.state).serialize(); |
3074 | [ |
3075 | response_type_bytes[0], |
3076 | 0, |
3077 | sequence_bytes[0], |
3078 | sequence_bytes[1], |
3079 | window_bytes[0], |
3080 | window_bytes[1], |
3081 | window_bytes[2], |
3082 | window_bytes[3], |
3083 | state_bytes[0], |
3084 | 0, |
3085 | 0, |
3086 | 0, |
3087 | ] |
3088 | } |
3089 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
3090 | bytes.reserve(12); |
3091 | self.response_type.serialize_into(bytes); |
3092 | bytes.extend_from_slice(&[0; 1]); |
3093 | self.sequence.serialize_into(bytes); |
3094 | self.window.serialize_into(bytes); |
3095 | u8::from(self.state).serialize_into(bytes); |
3096 | bytes.extend_from_slice(&[0; 3]); |
3097 | } |
3098 | } |
3099 | impl From<&VisibilityNotifyEvent> for [u8; 32] { |
3100 | fn from(input: &VisibilityNotifyEvent) -> Self { |
3101 | let response_type_bytes = input.response_type.serialize(); |
3102 | let sequence_bytes = input.sequence.serialize(); |
3103 | let window_bytes = input.window.serialize(); |
3104 | let state_bytes = u8::from(input.state).serialize(); |
3105 | [ |
3106 | response_type_bytes[0], |
3107 | 0, |
3108 | sequence_bytes[0], |
3109 | sequence_bytes[1], |
3110 | window_bytes[0], |
3111 | window_bytes[1], |
3112 | window_bytes[2], |
3113 | window_bytes[3], |
3114 | state_bytes[0], |
3115 | 0, |
3116 | 0, |
3117 | 0, |
3118 | // trailing padding |
3119 | 0, |
3120 | 0, |
3121 | 0, |
3122 | 0, |
3123 | 0, |
3124 | 0, |
3125 | 0, |
3126 | 0, |
3127 | 0, |
3128 | 0, |
3129 | 0, |
3130 | 0, |
3131 | 0, |
3132 | 0, |
3133 | 0, |
3134 | 0, |
3135 | 0, |
3136 | 0, |
3137 | 0, |
3138 | 0, |
3139 | ] |
3140 | } |
3141 | } |
3142 | impl From<VisibilityNotifyEvent> for [u8; 32] { |
3143 | fn from(input: VisibilityNotifyEvent) -> Self { |
3144 | Self::from(&input) |
3145 | } |
3146 | } |
3147 | |
3148 | /// Opcode for the CreateNotify event |
3149 | pub const CREATE_NOTIFY_EVENT: u8 = 16; |
3150 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
3151 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
3152 | pub struct CreateNotifyEvent { |
3153 | pub response_type: u8, |
3154 | pub sequence: u16, |
3155 | pub parent: Window, |
3156 | pub window: Window, |
3157 | pub x: i16, |
3158 | pub y: i16, |
3159 | pub width: u16, |
3160 | pub height: u16, |
3161 | pub border_width: u16, |
3162 | pub override_redirect: bool, |
3163 | } |
3164 | impl TryParse for CreateNotifyEvent { |
3165 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
3166 | let remaining: &[u8] = initial_value; |
3167 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
3168 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
3169 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3170 | let (parent: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3171 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3172 | let (x: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
3173 | let (y: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
3174 | let (width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3175 | let (height: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3176 | let (border_width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3177 | let (override_redirect: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
3178 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
3179 | let result: CreateNotifyEvent = CreateNotifyEvent { response_type, sequence, parent, window, x, y, width, height, border_width, override_redirect }; |
3180 | let _ = remaining; |
3181 | let remaining: &[u8] = initial_value.get(32..) |
3182 | .ok_or(err:ParseError::InsufficientData)?; |
3183 | Ok((result, remaining)) |
3184 | } |
3185 | } |
3186 | impl Serialize for CreateNotifyEvent { |
3187 | type Bytes = [u8; 24]; |
3188 | fn serialize(&self) -> [u8; 24] { |
3189 | let response_type_bytes = self.response_type.serialize(); |
3190 | let sequence_bytes = self.sequence.serialize(); |
3191 | let parent_bytes = self.parent.serialize(); |
3192 | let window_bytes = self.window.serialize(); |
3193 | let x_bytes = self.x.serialize(); |
3194 | let y_bytes = self.y.serialize(); |
3195 | let width_bytes = self.width.serialize(); |
3196 | let height_bytes = self.height.serialize(); |
3197 | let border_width_bytes = self.border_width.serialize(); |
3198 | let override_redirect_bytes = self.override_redirect.serialize(); |
3199 | [ |
3200 | response_type_bytes[0], |
3201 | 0, |
3202 | sequence_bytes[0], |
3203 | sequence_bytes[1], |
3204 | parent_bytes[0], |
3205 | parent_bytes[1], |
3206 | parent_bytes[2], |
3207 | parent_bytes[3], |
3208 | window_bytes[0], |
3209 | window_bytes[1], |
3210 | window_bytes[2], |
3211 | window_bytes[3], |
3212 | x_bytes[0], |
3213 | x_bytes[1], |
3214 | y_bytes[0], |
3215 | y_bytes[1], |
3216 | width_bytes[0], |
3217 | width_bytes[1], |
3218 | height_bytes[0], |
3219 | height_bytes[1], |
3220 | border_width_bytes[0], |
3221 | border_width_bytes[1], |
3222 | override_redirect_bytes[0], |
3223 | 0, |
3224 | ] |
3225 | } |
3226 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
3227 | bytes.reserve(24); |
3228 | self.response_type.serialize_into(bytes); |
3229 | bytes.extend_from_slice(&[0; 1]); |
3230 | self.sequence.serialize_into(bytes); |
3231 | self.parent.serialize_into(bytes); |
3232 | self.window.serialize_into(bytes); |
3233 | self.x.serialize_into(bytes); |
3234 | self.y.serialize_into(bytes); |
3235 | self.width.serialize_into(bytes); |
3236 | self.height.serialize_into(bytes); |
3237 | self.border_width.serialize_into(bytes); |
3238 | self.override_redirect.serialize_into(bytes); |
3239 | bytes.extend_from_slice(&[0; 1]); |
3240 | } |
3241 | } |
3242 | impl From<&CreateNotifyEvent> for [u8; 32] { |
3243 | fn from(input: &CreateNotifyEvent) -> Self { |
3244 | let response_type_bytes = input.response_type.serialize(); |
3245 | let sequence_bytes = input.sequence.serialize(); |
3246 | let parent_bytes = input.parent.serialize(); |
3247 | let window_bytes = input.window.serialize(); |
3248 | let x_bytes = input.x.serialize(); |
3249 | let y_bytes = input.y.serialize(); |
3250 | let width_bytes = input.width.serialize(); |
3251 | let height_bytes = input.height.serialize(); |
3252 | let border_width_bytes = input.border_width.serialize(); |
3253 | let override_redirect_bytes = input.override_redirect.serialize(); |
3254 | [ |
3255 | response_type_bytes[0], |
3256 | 0, |
3257 | sequence_bytes[0], |
3258 | sequence_bytes[1], |
3259 | parent_bytes[0], |
3260 | parent_bytes[1], |
3261 | parent_bytes[2], |
3262 | parent_bytes[3], |
3263 | window_bytes[0], |
3264 | window_bytes[1], |
3265 | window_bytes[2], |
3266 | window_bytes[3], |
3267 | x_bytes[0], |
3268 | x_bytes[1], |
3269 | y_bytes[0], |
3270 | y_bytes[1], |
3271 | width_bytes[0], |
3272 | width_bytes[1], |
3273 | height_bytes[0], |
3274 | height_bytes[1], |
3275 | border_width_bytes[0], |
3276 | border_width_bytes[1], |
3277 | override_redirect_bytes[0], |
3278 | 0, |
3279 | // trailing padding |
3280 | 0, |
3281 | 0, |
3282 | 0, |
3283 | 0, |
3284 | 0, |
3285 | 0, |
3286 | 0, |
3287 | 0, |
3288 | ] |
3289 | } |
3290 | } |
3291 | impl From<CreateNotifyEvent> for [u8; 32] { |
3292 | fn from(input: CreateNotifyEvent) -> Self { |
3293 | Self::from(&input) |
3294 | } |
3295 | } |
3296 | |
3297 | /// Opcode for the DestroyNotify event |
3298 | pub const DESTROY_NOTIFY_EVENT: u8 = 17; |
3299 | /// a window is destroyed. |
3300 | /// |
3301 | /// # Fields |
3302 | /// |
3303 | /// * `event` - The reconfigured window or its parent, depending on whether `StructureNotify` |
3304 | /// or `SubstructureNotify` was selected. |
3305 | /// * `window` - The window that is destroyed. |
3306 | /// |
3307 | /// # See |
3308 | /// |
3309 | /// * `DestroyWindow`: request |
3310 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
3311 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
3312 | pub struct DestroyNotifyEvent { |
3313 | pub response_type: u8, |
3314 | pub sequence: u16, |
3315 | pub event: Window, |
3316 | pub window: Window, |
3317 | } |
3318 | impl TryParse for DestroyNotifyEvent { |
3319 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
3320 | let remaining: &[u8] = initial_value; |
3321 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
3322 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
3323 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3324 | let (event: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3325 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3326 | let result: DestroyNotifyEvent = DestroyNotifyEvent { response_type, sequence, event, window }; |
3327 | let _ = remaining; |
3328 | let remaining: &[u8] = initial_value.get(32..) |
3329 | .ok_or(err:ParseError::InsufficientData)?; |
3330 | Ok((result, remaining)) |
3331 | } |
3332 | } |
3333 | impl Serialize for DestroyNotifyEvent { |
3334 | type Bytes = [u8; 12]; |
3335 | fn serialize(&self) -> [u8; 12] { |
3336 | let response_type_bytes = self.response_type.serialize(); |
3337 | let sequence_bytes = self.sequence.serialize(); |
3338 | let event_bytes = self.event.serialize(); |
3339 | let window_bytes = self.window.serialize(); |
3340 | [ |
3341 | response_type_bytes[0], |
3342 | 0, |
3343 | sequence_bytes[0], |
3344 | sequence_bytes[1], |
3345 | event_bytes[0], |
3346 | event_bytes[1], |
3347 | event_bytes[2], |
3348 | event_bytes[3], |
3349 | window_bytes[0], |
3350 | window_bytes[1], |
3351 | window_bytes[2], |
3352 | window_bytes[3], |
3353 | ] |
3354 | } |
3355 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
3356 | bytes.reserve(12); |
3357 | self.response_type.serialize_into(bytes); |
3358 | bytes.extend_from_slice(&[0; 1]); |
3359 | self.sequence.serialize_into(bytes); |
3360 | self.event.serialize_into(bytes); |
3361 | self.window.serialize_into(bytes); |
3362 | } |
3363 | } |
3364 | impl From<&DestroyNotifyEvent> for [u8; 32] { |
3365 | fn from(input: &DestroyNotifyEvent) -> Self { |
3366 | let response_type_bytes = input.response_type.serialize(); |
3367 | let sequence_bytes = input.sequence.serialize(); |
3368 | let event_bytes = input.event.serialize(); |
3369 | let window_bytes = input.window.serialize(); |
3370 | [ |
3371 | response_type_bytes[0], |
3372 | 0, |
3373 | sequence_bytes[0], |
3374 | sequence_bytes[1], |
3375 | event_bytes[0], |
3376 | event_bytes[1], |
3377 | event_bytes[2], |
3378 | event_bytes[3], |
3379 | window_bytes[0], |
3380 | window_bytes[1], |
3381 | window_bytes[2], |
3382 | window_bytes[3], |
3383 | // trailing padding |
3384 | 0, |
3385 | 0, |
3386 | 0, |
3387 | 0, |
3388 | 0, |
3389 | 0, |
3390 | 0, |
3391 | 0, |
3392 | 0, |
3393 | 0, |
3394 | 0, |
3395 | 0, |
3396 | 0, |
3397 | 0, |
3398 | 0, |
3399 | 0, |
3400 | 0, |
3401 | 0, |
3402 | 0, |
3403 | 0, |
3404 | ] |
3405 | } |
3406 | } |
3407 | impl From<DestroyNotifyEvent> for [u8; 32] { |
3408 | fn from(input: DestroyNotifyEvent) -> Self { |
3409 | Self::from(&input) |
3410 | } |
3411 | } |
3412 | |
3413 | /// Opcode for the UnmapNotify event |
3414 | pub const UNMAP_NOTIFY_EVENT: u8 = 18; |
3415 | /// a window is unmapped. |
3416 | /// |
3417 | /// # Fields |
3418 | /// |
3419 | /// * `event` - The reconfigured window or its parent, depending on whether `StructureNotify` |
3420 | /// or `SubstructureNotify` was selected. |
3421 | /// * `window` - The window that was unmapped. |
3422 | /// * `from_configure` - Set to 1 if the event was generated as a result of a resizing of the window's |
3423 | /// parent when `window` had a win_gravity of `UnmapGravity`. |
3424 | /// |
3425 | /// # See |
3426 | /// |
3427 | /// * `UnmapWindow`: request |
3428 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
3429 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
3430 | pub struct UnmapNotifyEvent { |
3431 | pub response_type: u8, |
3432 | pub sequence: u16, |
3433 | pub event: Window, |
3434 | pub window: Window, |
3435 | pub from_configure: bool, |
3436 | } |
3437 | impl TryParse for UnmapNotifyEvent { |
3438 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
3439 | let remaining: &[u8] = initial_value; |
3440 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
3441 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
3442 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3443 | let (event: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3444 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3445 | let (from_configure: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
3446 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
3447 | let result: UnmapNotifyEvent = UnmapNotifyEvent { response_type, sequence, event, window, from_configure }; |
3448 | let _ = remaining; |
3449 | let remaining: &[u8] = initial_value.get(32..) |
3450 | .ok_or(err:ParseError::InsufficientData)?; |
3451 | Ok((result, remaining)) |
3452 | } |
3453 | } |
3454 | impl Serialize for UnmapNotifyEvent { |
3455 | type Bytes = [u8; 16]; |
3456 | fn serialize(&self) -> [u8; 16] { |
3457 | let response_type_bytes = self.response_type.serialize(); |
3458 | let sequence_bytes = self.sequence.serialize(); |
3459 | let event_bytes = self.event.serialize(); |
3460 | let window_bytes = self.window.serialize(); |
3461 | let from_configure_bytes = self.from_configure.serialize(); |
3462 | [ |
3463 | response_type_bytes[0], |
3464 | 0, |
3465 | sequence_bytes[0], |
3466 | sequence_bytes[1], |
3467 | event_bytes[0], |
3468 | event_bytes[1], |
3469 | event_bytes[2], |
3470 | event_bytes[3], |
3471 | window_bytes[0], |
3472 | window_bytes[1], |
3473 | window_bytes[2], |
3474 | window_bytes[3], |
3475 | from_configure_bytes[0], |
3476 | 0, |
3477 | 0, |
3478 | 0, |
3479 | ] |
3480 | } |
3481 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
3482 | bytes.reserve(16); |
3483 | self.response_type.serialize_into(bytes); |
3484 | bytes.extend_from_slice(&[0; 1]); |
3485 | self.sequence.serialize_into(bytes); |
3486 | self.event.serialize_into(bytes); |
3487 | self.window.serialize_into(bytes); |
3488 | self.from_configure.serialize_into(bytes); |
3489 | bytes.extend_from_slice(&[0; 3]); |
3490 | } |
3491 | } |
3492 | impl From<&UnmapNotifyEvent> for [u8; 32] { |
3493 | fn from(input: &UnmapNotifyEvent) -> Self { |
3494 | let response_type_bytes = input.response_type.serialize(); |
3495 | let sequence_bytes = input.sequence.serialize(); |
3496 | let event_bytes = input.event.serialize(); |
3497 | let window_bytes = input.window.serialize(); |
3498 | let from_configure_bytes = input.from_configure.serialize(); |
3499 | [ |
3500 | response_type_bytes[0], |
3501 | 0, |
3502 | sequence_bytes[0], |
3503 | sequence_bytes[1], |
3504 | event_bytes[0], |
3505 | event_bytes[1], |
3506 | event_bytes[2], |
3507 | event_bytes[3], |
3508 | window_bytes[0], |
3509 | window_bytes[1], |
3510 | window_bytes[2], |
3511 | window_bytes[3], |
3512 | from_configure_bytes[0], |
3513 | 0, |
3514 | 0, |
3515 | 0, |
3516 | // trailing padding |
3517 | 0, |
3518 | 0, |
3519 | 0, |
3520 | 0, |
3521 | 0, |
3522 | 0, |
3523 | 0, |
3524 | 0, |
3525 | 0, |
3526 | 0, |
3527 | 0, |
3528 | 0, |
3529 | 0, |
3530 | 0, |
3531 | 0, |
3532 | 0, |
3533 | ] |
3534 | } |
3535 | } |
3536 | impl From<UnmapNotifyEvent> for [u8; 32] { |
3537 | fn from(input: UnmapNotifyEvent) -> Self { |
3538 | Self::from(&input) |
3539 | } |
3540 | } |
3541 | |
3542 | /// Opcode for the MapNotify event |
3543 | pub const MAP_NOTIFY_EVENT: u8 = 19; |
3544 | /// a window was mapped. |
3545 | /// |
3546 | /// # Fields |
3547 | /// |
3548 | /// * `event` - The window which was mapped or its parent, depending on whether |
3549 | /// `StructureNotify` or `SubstructureNotify` was selected. |
3550 | /// * `window` - The window that was mapped. |
3551 | /// * `override_redirect` - Window managers should ignore this window if `override_redirect` is 1. |
3552 | /// |
3553 | /// # See |
3554 | /// |
3555 | /// * `MapWindow`: request |
3556 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
3557 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
3558 | pub struct MapNotifyEvent { |
3559 | pub response_type: u8, |
3560 | pub sequence: u16, |
3561 | pub event: Window, |
3562 | pub window: Window, |
3563 | pub override_redirect: bool, |
3564 | } |
3565 | impl TryParse for MapNotifyEvent { |
3566 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
3567 | let remaining: &[u8] = initial_value; |
3568 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
3569 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
3570 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3571 | let (event: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3572 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3573 | let (override_redirect: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
3574 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
3575 | let result: MapNotifyEvent = MapNotifyEvent { response_type, sequence, event, window, override_redirect }; |
3576 | let _ = remaining; |
3577 | let remaining: &[u8] = initial_value.get(32..) |
3578 | .ok_or(err:ParseError::InsufficientData)?; |
3579 | Ok((result, remaining)) |
3580 | } |
3581 | } |
3582 | impl Serialize for MapNotifyEvent { |
3583 | type Bytes = [u8; 16]; |
3584 | fn serialize(&self) -> [u8; 16] { |
3585 | let response_type_bytes = self.response_type.serialize(); |
3586 | let sequence_bytes = self.sequence.serialize(); |
3587 | let event_bytes = self.event.serialize(); |
3588 | let window_bytes = self.window.serialize(); |
3589 | let override_redirect_bytes = self.override_redirect.serialize(); |
3590 | [ |
3591 | response_type_bytes[0], |
3592 | 0, |
3593 | sequence_bytes[0], |
3594 | sequence_bytes[1], |
3595 | event_bytes[0], |
3596 | event_bytes[1], |
3597 | event_bytes[2], |
3598 | event_bytes[3], |
3599 | window_bytes[0], |
3600 | window_bytes[1], |
3601 | window_bytes[2], |
3602 | window_bytes[3], |
3603 | override_redirect_bytes[0], |
3604 | 0, |
3605 | 0, |
3606 | 0, |
3607 | ] |
3608 | } |
3609 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
3610 | bytes.reserve(16); |
3611 | self.response_type.serialize_into(bytes); |
3612 | bytes.extend_from_slice(&[0; 1]); |
3613 | self.sequence.serialize_into(bytes); |
3614 | self.event.serialize_into(bytes); |
3615 | self.window.serialize_into(bytes); |
3616 | self.override_redirect.serialize_into(bytes); |
3617 | bytes.extend_from_slice(&[0; 3]); |
3618 | } |
3619 | } |
3620 | impl From<&MapNotifyEvent> for [u8; 32] { |
3621 | fn from(input: &MapNotifyEvent) -> Self { |
3622 | let response_type_bytes = input.response_type.serialize(); |
3623 | let sequence_bytes = input.sequence.serialize(); |
3624 | let event_bytes = input.event.serialize(); |
3625 | let window_bytes = input.window.serialize(); |
3626 | let override_redirect_bytes = input.override_redirect.serialize(); |
3627 | [ |
3628 | response_type_bytes[0], |
3629 | 0, |
3630 | sequence_bytes[0], |
3631 | sequence_bytes[1], |
3632 | event_bytes[0], |
3633 | event_bytes[1], |
3634 | event_bytes[2], |
3635 | event_bytes[3], |
3636 | window_bytes[0], |
3637 | window_bytes[1], |
3638 | window_bytes[2], |
3639 | window_bytes[3], |
3640 | override_redirect_bytes[0], |
3641 | 0, |
3642 | 0, |
3643 | 0, |
3644 | // trailing padding |
3645 | 0, |
3646 | 0, |
3647 | 0, |
3648 | 0, |
3649 | 0, |
3650 | 0, |
3651 | 0, |
3652 | 0, |
3653 | 0, |
3654 | 0, |
3655 | 0, |
3656 | 0, |
3657 | 0, |
3658 | 0, |
3659 | 0, |
3660 | 0, |
3661 | ] |
3662 | } |
3663 | } |
3664 | impl From<MapNotifyEvent> for [u8; 32] { |
3665 | fn from(input: MapNotifyEvent) -> Self { |
3666 | Self::from(&input) |
3667 | } |
3668 | } |
3669 | |
3670 | /// Opcode for the MapRequest event |
3671 | pub const MAP_REQUEST_EVENT: u8 = 20; |
3672 | /// window wants to be mapped. |
3673 | /// |
3674 | /// # Fields |
3675 | /// |
3676 | /// * `parent` - The parent of `window`. |
3677 | /// * `window` - The window to be mapped. |
3678 | /// |
3679 | /// # See |
3680 | /// |
3681 | /// * `MapWindow`: request |
3682 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
3683 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
3684 | pub struct MapRequestEvent { |
3685 | pub response_type: u8, |
3686 | pub sequence: u16, |
3687 | pub parent: Window, |
3688 | pub window: Window, |
3689 | } |
3690 | impl TryParse for MapRequestEvent { |
3691 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
3692 | let remaining: &[u8] = initial_value; |
3693 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
3694 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
3695 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3696 | let (parent: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3697 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3698 | let result: MapRequestEvent = MapRequestEvent { response_type, sequence, parent, window }; |
3699 | let _ = remaining; |
3700 | let remaining: &[u8] = initial_value.get(32..) |
3701 | .ok_or(err:ParseError::InsufficientData)?; |
3702 | Ok((result, remaining)) |
3703 | } |
3704 | } |
3705 | impl Serialize for MapRequestEvent { |
3706 | type Bytes = [u8; 12]; |
3707 | fn serialize(&self) -> [u8; 12] { |
3708 | let response_type_bytes = self.response_type.serialize(); |
3709 | let sequence_bytes = self.sequence.serialize(); |
3710 | let parent_bytes = self.parent.serialize(); |
3711 | let window_bytes = self.window.serialize(); |
3712 | [ |
3713 | response_type_bytes[0], |
3714 | 0, |
3715 | sequence_bytes[0], |
3716 | sequence_bytes[1], |
3717 | parent_bytes[0], |
3718 | parent_bytes[1], |
3719 | parent_bytes[2], |
3720 | parent_bytes[3], |
3721 | window_bytes[0], |
3722 | window_bytes[1], |
3723 | window_bytes[2], |
3724 | window_bytes[3], |
3725 | ] |
3726 | } |
3727 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
3728 | bytes.reserve(12); |
3729 | self.response_type.serialize_into(bytes); |
3730 | bytes.extend_from_slice(&[0; 1]); |
3731 | self.sequence.serialize_into(bytes); |
3732 | self.parent.serialize_into(bytes); |
3733 | self.window.serialize_into(bytes); |
3734 | } |
3735 | } |
3736 | impl From<&MapRequestEvent> for [u8; 32] { |
3737 | fn from(input: &MapRequestEvent) -> Self { |
3738 | let response_type_bytes = input.response_type.serialize(); |
3739 | let sequence_bytes = input.sequence.serialize(); |
3740 | let parent_bytes = input.parent.serialize(); |
3741 | let window_bytes = input.window.serialize(); |
3742 | [ |
3743 | response_type_bytes[0], |
3744 | 0, |
3745 | sequence_bytes[0], |
3746 | sequence_bytes[1], |
3747 | parent_bytes[0], |
3748 | parent_bytes[1], |
3749 | parent_bytes[2], |
3750 | parent_bytes[3], |
3751 | window_bytes[0], |
3752 | window_bytes[1], |
3753 | window_bytes[2], |
3754 | window_bytes[3], |
3755 | // trailing padding |
3756 | 0, |
3757 | 0, |
3758 | 0, |
3759 | 0, |
3760 | 0, |
3761 | 0, |
3762 | 0, |
3763 | 0, |
3764 | 0, |
3765 | 0, |
3766 | 0, |
3767 | 0, |
3768 | 0, |
3769 | 0, |
3770 | 0, |
3771 | 0, |
3772 | 0, |
3773 | 0, |
3774 | 0, |
3775 | 0, |
3776 | ] |
3777 | } |
3778 | } |
3779 | impl From<MapRequestEvent> for [u8; 32] { |
3780 | fn from(input: MapRequestEvent) -> Self { |
3781 | Self::from(&input) |
3782 | } |
3783 | } |
3784 | |
3785 | /// Opcode for the ReparentNotify event |
3786 | pub const REPARENT_NOTIFY_EVENT: u8 = 21; |
3787 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
3788 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
3789 | pub struct ReparentNotifyEvent { |
3790 | pub response_type: u8, |
3791 | pub sequence: u16, |
3792 | pub event: Window, |
3793 | pub window: Window, |
3794 | pub parent: Window, |
3795 | pub x: i16, |
3796 | pub y: i16, |
3797 | pub override_redirect: bool, |
3798 | } |
3799 | impl TryParse for ReparentNotifyEvent { |
3800 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
3801 | let remaining: &[u8] = initial_value; |
3802 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
3803 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
3804 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3805 | let (event: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3806 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3807 | let (parent: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3808 | let (x: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
3809 | let (y: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
3810 | let (override_redirect: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
3811 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
3812 | let result: ReparentNotifyEvent = ReparentNotifyEvent { response_type, sequence, event, window, parent, x, y, override_redirect }; |
3813 | let _ = remaining; |
3814 | let remaining: &[u8] = initial_value.get(32..) |
3815 | .ok_or(err:ParseError::InsufficientData)?; |
3816 | Ok((result, remaining)) |
3817 | } |
3818 | } |
3819 | impl Serialize for ReparentNotifyEvent { |
3820 | type Bytes = [u8; 24]; |
3821 | fn serialize(&self) -> [u8; 24] { |
3822 | let response_type_bytes = self.response_type.serialize(); |
3823 | let sequence_bytes = self.sequence.serialize(); |
3824 | let event_bytes = self.event.serialize(); |
3825 | let window_bytes = self.window.serialize(); |
3826 | let parent_bytes = self.parent.serialize(); |
3827 | let x_bytes = self.x.serialize(); |
3828 | let y_bytes = self.y.serialize(); |
3829 | let override_redirect_bytes = self.override_redirect.serialize(); |
3830 | [ |
3831 | response_type_bytes[0], |
3832 | 0, |
3833 | sequence_bytes[0], |
3834 | sequence_bytes[1], |
3835 | event_bytes[0], |
3836 | event_bytes[1], |
3837 | event_bytes[2], |
3838 | event_bytes[3], |
3839 | window_bytes[0], |
3840 | window_bytes[1], |
3841 | window_bytes[2], |
3842 | window_bytes[3], |
3843 | parent_bytes[0], |
3844 | parent_bytes[1], |
3845 | parent_bytes[2], |
3846 | parent_bytes[3], |
3847 | x_bytes[0], |
3848 | x_bytes[1], |
3849 | y_bytes[0], |
3850 | y_bytes[1], |
3851 | override_redirect_bytes[0], |
3852 | 0, |
3853 | 0, |
3854 | 0, |
3855 | ] |
3856 | } |
3857 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
3858 | bytes.reserve(24); |
3859 | self.response_type.serialize_into(bytes); |
3860 | bytes.extend_from_slice(&[0; 1]); |
3861 | self.sequence.serialize_into(bytes); |
3862 | self.event.serialize_into(bytes); |
3863 | self.window.serialize_into(bytes); |
3864 | self.parent.serialize_into(bytes); |
3865 | self.x.serialize_into(bytes); |
3866 | self.y.serialize_into(bytes); |
3867 | self.override_redirect.serialize_into(bytes); |
3868 | bytes.extend_from_slice(&[0; 3]); |
3869 | } |
3870 | } |
3871 | impl From<&ReparentNotifyEvent> for [u8; 32] { |
3872 | fn from(input: &ReparentNotifyEvent) -> Self { |
3873 | let response_type_bytes = input.response_type.serialize(); |
3874 | let sequence_bytes = input.sequence.serialize(); |
3875 | let event_bytes = input.event.serialize(); |
3876 | let window_bytes = input.window.serialize(); |
3877 | let parent_bytes = input.parent.serialize(); |
3878 | let x_bytes = input.x.serialize(); |
3879 | let y_bytes = input.y.serialize(); |
3880 | let override_redirect_bytes = input.override_redirect.serialize(); |
3881 | [ |
3882 | response_type_bytes[0], |
3883 | 0, |
3884 | sequence_bytes[0], |
3885 | sequence_bytes[1], |
3886 | event_bytes[0], |
3887 | event_bytes[1], |
3888 | event_bytes[2], |
3889 | event_bytes[3], |
3890 | window_bytes[0], |
3891 | window_bytes[1], |
3892 | window_bytes[2], |
3893 | window_bytes[3], |
3894 | parent_bytes[0], |
3895 | parent_bytes[1], |
3896 | parent_bytes[2], |
3897 | parent_bytes[3], |
3898 | x_bytes[0], |
3899 | x_bytes[1], |
3900 | y_bytes[0], |
3901 | y_bytes[1], |
3902 | override_redirect_bytes[0], |
3903 | 0, |
3904 | 0, |
3905 | 0, |
3906 | // trailing padding |
3907 | 0, |
3908 | 0, |
3909 | 0, |
3910 | 0, |
3911 | 0, |
3912 | 0, |
3913 | 0, |
3914 | 0, |
3915 | ] |
3916 | } |
3917 | } |
3918 | impl From<ReparentNotifyEvent> for [u8; 32] { |
3919 | fn from(input: ReparentNotifyEvent) -> Self { |
3920 | Self::from(&input) |
3921 | } |
3922 | } |
3923 | |
3924 | /// Opcode for the ConfigureNotify event |
3925 | pub const CONFIGURE_NOTIFY_EVENT: u8 = 22; |
3926 | /// NOT YET DOCUMENTED. |
3927 | /// |
3928 | /// # Fields |
3929 | /// |
3930 | /// * `event` - The reconfigured window or its parent, depending on whether `StructureNotify` |
3931 | /// or `SubstructureNotify` was selected. |
3932 | /// * `window` - The window whose size, position, border, and/or stacking order was changed. |
3933 | /// * `above_sibling` - If `XCB_NONE`, the `window` is on the bottom of the stack with respect to |
3934 | /// sibling windows. However, if set to a sibling window, the `window` is placed on |
3935 | /// top of this sibling window. |
3936 | /// * `x` - The X coordinate of the upper-left outside corner of `window`, relative to the |
3937 | /// parent window's origin. |
3938 | /// * `y` - The Y coordinate of the upper-left outside corner of `window`, relative to the |
3939 | /// parent window's origin. |
3940 | /// * `width` - The inside width of `window`, not including the border. |
3941 | /// * `height` - The inside height of `window`, not including the border. |
3942 | /// * `border_width` - The border width of `window`. |
3943 | /// * `override_redirect` - Window managers should ignore this window if `override_redirect` is 1. |
3944 | /// |
3945 | /// # See |
3946 | /// |
3947 | /// * `FreeColormap`: request |
3948 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
3949 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
3950 | pub struct ConfigureNotifyEvent { |
3951 | pub response_type: u8, |
3952 | pub sequence: u16, |
3953 | pub event: Window, |
3954 | pub window: Window, |
3955 | pub above_sibling: Window, |
3956 | pub x: i16, |
3957 | pub y: i16, |
3958 | pub width: u16, |
3959 | pub height: u16, |
3960 | pub border_width: u16, |
3961 | pub override_redirect: bool, |
3962 | } |
3963 | impl TryParse for ConfigureNotifyEvent { |
3964 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
3965 | let remaining: &[u8] = initial_value; |
3966 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
3967 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
3968 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3969 | let (event: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3970 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3971 | let (above_sibling: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
3972 | let (x: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
3973 | let (y: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
3974 | let (width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3975 | let (height: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3976 | let (border_width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
3977 | let (override_redirect: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
3978 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
3979 | let result: ConfigureNotifyEvent = ConfigureNotifyEvent { response_type, sequence, event, window, above_sibling, x, y, width, height, border_width, override_redirect }; |
3980 | let _ = remaining; |
3981 | let remaining: &[u8] = initial_value.get(32..) |
3982 | .ok_or(err:ParseError::InsufficientData)?; |
3983 | Ok((result, remaining)) |
3984 | } |
3985 | } |
3986 | impl Serialize for ConfigureNotifyEvent { |
3987 | type Bytes = [u8; 28]; |
3988 | fn serialize(&self) -> [u8; 28] { |
3989 | let response_type_bytes = self.response_type.serialize(); |
3990 | let sequence_bytes = self.sequence.serialize(); |
3991 | let event_bytes = self.event.serialize(); |
3992 | let window_bytes = self.window.serialize(); |
3993 | let above_sibling_bytes = self.above_sibling.serialize(); |
3994 | let x_bytes = self.x.serialize(); |
3995 | let y_bytes = self.y.serialize(); |
3996 | let width_bytes = self.width.serialize(); |
3997 | let height_bytes = self.height.serialize(); |
3998 | let border_width_bytes = self.border_width.serialize(); |
3999 | let override_redirect_bytes = self.override_redirect.serialize(); |
4000 | [ |
4001 | response_type_bytes[0], |
4002 | 0, |
4003 | sequence_bytes[0], |
4004 | sequence_bytes[1], |
4005 | event_bytes[0], |
4006 | event_bytes[1], |
4007 | event_bytes[2], |
4008 | event_bytes[3], |
4009 | window_bytes[0], |
4010 | window_bytes[1], |
4011 | window_bytes[2], |
4012 | window_bytes[3], |
4013 | above_sibling_bytes[0], |
4014 | above_sibling_bytes[1], |
4015 | above_sibling_bytes[2], |
4016 | above_sibling_bytes[3], |
4017 | x_bytes[0], |
4018 | x_bytes[1], |
4019 | y_bytes[0], |
4020 | y_bytes[1], |
4021 | width_bytes[0], |
4022 | width_bytes[1], |
4023 | height_bytes[0], |
4024 | height_bytes[1], |
4025 | border_width_bytes[0], |
4026 | border_width_bytes[1], |
4027 | override_redirect_bytes[0], |
4028 | 0, |
4029 | ] |
4030 | } |
4031 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
4032 | bytes.reserve(28); |
4033 | self.response_type.serialize_into(bytes); |
4034 | bytes.extend_from_slice(&[0; 1]); |
4035 | self.sequence.serialize_into(bytes); |
4036 | self.event.serialize_into(bytes); |
4037 | self.window.serialize_into(bytes); |
4038 | self.above_sibling.serialize_into(bytes); |
4039 | self.x.serialize_into(bytes); |
4040 | self.y.serialize_into(bytes); |
4041 | self.width.serialize_into(bytes); |
4042 | self.height.serialize_into(bytes); |
4043 | self.border_width.serialize_into(bytes); |
4044 | self.override_redirect.serialize_into(bytes); |
4045 | bytes.extend_from_slice(&[0; 1]); |
4046 | } |
4047 | } |
4048 | impl From<&ConfigureNotifyEvent> for [u8; 32] { |
4049 | fn from(input: &ConfigureNotifyEvent) -> Self { |
4050 | let response_type_bytes = input.response_type.serialize(); |
4051 | let sequence_bytes = input.sequence.serialize(); |
4052 | let event_bytes = input.event.serialize(); |
4053 | let window_bytes = input.window.serialize(); |
4054 | let above_sibling_bytes = input.above_sibling.serialize(); |
4055 | let x_bytes = input.x.serialize(); |
4056 | let y_bytes = input.y.serialize(); |
4057 | let width_bytes = input.width.serialize(); |
4058 | let height_bytes = input.height.serialize(); |
4059 | let border_width_bytes = input.border_width.serialize(); |
4060 | let override_redirect_bytes = input.override_redirect.serialize(); |
4061 | [ |
4062 | response_type_bytes[0], |
4063 | 0, |
4064 | sequence_bytes[0], |
4065 | sequence_bytes[1], |
4066 | event_bytes[0], |
4067 | event_bytes[1], |
4068 | event_bytes[2], |
4069 | event_bytes[3], |
4070 | window_bytes[0], |
4071 | window_bytes[1], |
4072 | window_bytes[2], |
4073 | window_bytes[3], |
4074 | above_sibling_bytes[0], |
4075 | above_sibling_bytes[1], |
4076 | above_sibling_bytes[2], |
4077 | above_sibling_bytes[3], |
4078 | x_bytes[0], |
4079 | x_bytes[1], |
4080 | y_bytes[0], |
4081 | y_bytes[1], |
4082 | width_bytes[0], |
4083 | width_bytes[1], |
4084 | height_bytes[0], |
4085 | height_bytes[1], |
4086 | border_width_bytes[0], |
4087 | border_width_bytes[1], |
4088 | override_redirect_bytes[0], |
4089 | 0, |
4090 | // trailing padding |
4091 | 0, |
4092 | 0, |
4093 | 0, |
4094 | 0, |
4095 | ] |
4096 | } |
4097 | } |
4098 | impl From<ConfigureNotifyEvent> for [u8; 32] { |
4099 | fn from(input: ConfigureNotifyEvent) -> Self { |
4100 | Self::from(&input) |
4101 | } |
4102 | } |
4103 | |
4104 | /// Opcode for the ConfigureRequest event |
4105 | pub const CONFIGURE_REQUEST_EVENT: u8 = 23; |
4106 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
4107 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
4108 | pub struct ConfigureRequestEvent { |
4109 | pub response_type: u8, |
4110 | pub stack_mode: StackMode, |
4111 | pub sequence: u16, |
4112 | pub parent: Window, |
4113 | pub window: Window, |
4114 | pub sibling: Window, |
4115 | pub x: i16, |
4116 | pub y: i16, |
4117 | pub width: u16, |
4118 | pub height: u16, |
4119 | pub border_width: u16, |
4120 | pub value_mask: ConfigWindow, |
4121 | } |
4122 | impl TryParse for ConfigureRequestEvent { |
4123 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
4124 | let remaining: &[u8] = initial_value; |
4125 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
4126 | let (stack_mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
4127 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
4128 | let (parent: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
4129 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
4130 | let (sibling: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
4131 | let (x: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
4132 | let (y: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
4133 | let (width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
4134 | let (height: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
4135 | let (border_width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
4136 | let (value_mask: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
4137 | let stack_mode: StackMode = stack_mode.into(); |
4138 | let value_mask: ConfigWindow = value_mask.into(); |
4139 | let result: ConfigureRequestEvent = ConfigureRequestEvent { response_type, stack_mode, sequence, parent, window, sibling, x, y, width, height, border_width, value_mask }; |
4140 | let _ = remaining; |
4141 | let remaining: &[u8] = initial_value.get(32..) |
4142 | .ok_or(err:ParseError::InsufficientData)?; |
4143 | Ok((result, remaining)) |
4144 | } |
4145 | } |
4146 | impl Serialize for ConfigureRequestEvent { |
4147 | type Bytes = [u8; 28]; |
4148 | fn serialize(&self) -> [u8; 28] { |
4149 | let response_type_bytes = self.response_type.serialize(); |
4150 | let stack_mode_bytes = (u32::from(self.stack_mode) as u8).serialize(); |
4151 | let sequence_bytes = self.sequence.serialize(); |
4152 | let parent_bytes = self.parent.serialize(); |
4153 | let window_bytes = self.window.serialize(); |
4154 | let sibling_bytes = self.sibling.serialize(); |
4155 | let x_bytes = self.x.serialize(); |
4156 | let y_bytes = self.y.serialize(); |
4157 | let width_bytes = self.width.serialize(); |
4158 | let height_bytes = self.height.serialize(); |
4159 | let border_width_bytes = self.border_width.serialize(); |
4160 | let value_mask_bytes = u16::from(self.value_mask).serialize(); |
4161 | [ |
4162 | response_type_bytes[0], |
4163 | stack_mode_bytes[0], |
4164 | sequence_bytes[0], |
4165 | sequence_bytes[1], |
4166 | parent_bytes[0], |
4167 | parent_bytes[1], |
4168 | parent_bytes[2], |
4169 | parent_bytes[3], |
4170 | window_bytes[0], |
4171 | window_bytes[1], |
4172 | window_bytes[2], |
4173 | window_bytes[3], |
4174 | sibling_bytes[0], |
4175 | sibling_bytes[1], |
4176 | sibling_bytes[2], |
4177 | sibling_bytes[3], |
4178 | x_bytes[0], |
4179 | x_bytes[1], |
4180 | y_bytes[0], |
4181 | y_bytes[1], |
4182 | width_bytes[0], |
4183 | width_bytes[1], |
4184 | height_bytes[0], |
4185 | height_bytes[1], |
4186 | border_width_bytes[0], |
4187 | border_width_bytes[1], |
4188 | value_mask_bytes[0], |
4189 | value_mask_bytes[1], |
4190 | ] |
4191 | } |
4192 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
4193 | bytes.reserve(28); |
4194 | self.response_type.serialize_into(bytes); |
4195 | (u32::from(self.stack_mode) as u8).serialize_into(bytes); |
4196 | self.sequence.serialize_into(bytes); |
4197 | self.parent.serialize_into(bytes); |
4198 | self.window.serialize_into(bytes); |
4199 | self.sibling.serialize_into(bytes); |
4200 | self.x.serialize_into(bytes); |
4201 | self.y.serialize_into(bytes); |
4202 | self.width.serialize_into(bytes); |
4203 | self.height.serialize_into(bytes); |
4204 | self.border_width.serialize_into(bytes); |
4205 | u16::from(self.value_mask).serialize_into(bytes); |
4206 | } |
4207 | } |
4208 | impl From<&ConfigureRequestEvent> for [u8; 32] { |
4209 | fn from(input: &ConfigureRequestEvent) -> Self { |
4210 | let response_type_bytes = input.response_type.serialize(); |
4211 | let stack_mode_bytes = (u32::from(input.stack_mode) as u8).serialize(); |
4212 | let sequence_bytes = input.sequence.serialize(); |
4213 | let parent_bytes = input.parent.serialize(); |
4214 | let window_bytes = input.window.serialize(); |
4215 | let sibling_bytes = input.sibling.serialize(); |
4216 | let x_bytes = input.x.serialize(); |
4217 | let y_bytes = input.y.serialize(); |
4218 | let width_bytes = input.width.serialize(); |
4219 | let height_bytes = input.height.serialize(); |
4220 | let border_width_bytes = input.border_width.serialize(); |
4221 | let value_mask_bytes = u16::from(input.value_mask).serialize(); |
4222 | [ |
4223 | response_type_bytes[0], |
4224 | stack_mode_bytes[0], |
4225 | sequence_bytes[0], |
4226 | sequence_bytes[1], |
4227 | parent_bytes[0], |
4228 | parent_bytes[1], |
4229 | parent_bytes[2], |
4230 | parent_bytes[3], |
4231 | window_bytes[0], |
4232 | window_bytes[1], |
4233 | window_bytes[2], |
4234 | window_bytes[3], |
4235 | sibling_bytes[0], |
4236 | sibling_bytes[1], |
4237 | sibling_bytes[2], |
4238 | sibling_bytes[3], |
4239 | x_bytes[0], |
4240 | x_bytes[1], |
4241 | y_bytes[0], |
4242 | y_bytes[1], |
4243 | width_bytes[0], |
4244 | width_bytes[1], |
4245 | height_bytes[0], |
4246 | height_bytes[1], |
4247 | border_width_bytes[0], |
4248 | border_width_bytes[1], |
4249 | value_mask_bytes[0], |
4250 | value_mask_bytes[1], |
4251 | // trailing padding |
4252 | 0, |
4253 | 0, |
4254 | 0, |
4255 | 0, |
4256 | ] |
4257 | } |
4258 | } |
4259 | impl From<ConfigureRequestEvent> for [u8; 32] { |
4260 | fn from(input: ConfigureRequestEvent) -> Self { |
4261 | Self::from(&input) |
4262 | } |
4263 | } |
4264 | |
4265 | /// Opcode for the GravityNotify event |
4266 | pub const GRAVITY_NOTIFY_EVENT: u8 = 24; |
4267 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
4268 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
4269 | pub struct GravityNotifyEvent { |
4270 | pub response_type: u8, |
4271 | pub sequence: u16, |
4272 | pub event: Window, |
4273 | pub window: Window, |
4274 | pub x: i16, |
4275 | pub y: i16, |
4276 | } |
4277 | impl TryParse for GravityNotifyEvent { |
4278 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
4279 | let remaining: &[u8] = initial_value; |
4280 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
4281 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
4282 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
4283 | let (event: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
4284 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
4285 | let (x: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
4286 | let (y: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
4287 | let result: GravityNotifyEvent = GravityNotifyEvent { response_type, sequence, event, window, x, y }; |
4288 | let _ = remaining; |
4289 | let remaining: &[u8] = initial_value.get(32..) |
4290 | .ok_or(err:ParseError::InsufficientData)?; |
4291 | Ok((result, remaining)) |
4292 | } |
4293 | } |
4294 | impl Serialize for GravityNotifyEvent { |
4295 | type Bytes = [u8; 16]; |
4296 | fn serialize(&self) -> [u8; 16] { |
4297 | let response_type_bytes = self.response_type.serialize(); |
4298 | let sequence_bytes = self.sequence.serialize(); |
4299 | let event_bytes = self.event.serialize(); |
4300 | let window_bytes = self.window.serialize(); |
4301 | let x_bytes = self.x.serialize(); |
4302 | let y_bytes = self.y.serialize(); |
4303 | [ |
4304 | response_type_bytes[0], |
4305 | 0, |
4306 | sequence_bytes[0], |
4307 | sequence_bytes[1], |
4308 | event_bytes[0], |
4309 | event_bytes[1], |
4310 | event_bytes[2], |
4311 | event_bytes[3], |
4312 | window_bytes[0], |
4313 | window_bytes[1], |
4314 | window_bytes[2], |
4315 | window_bytes[3], |
4316 | x_bytes[0], |
4317 | x_bytes[1], |
4318 | y_bytes[0], |
4319 | y_bytes[1], |
4320 | ] |
4321 | } |
4322 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
4323 | bytes.reserve(16); |
4324 | self.response_type.serialize_into(bytes); |
4325 | bytes.extend_from_slice(&[0; 1]); |
4326 | self.sequence.serialize_into(bytes); |
4327 | self.event.serialize_into(bytes); |
4328 | self.window.serialize_into(bytes); |
4329 | self.x.serialize_into(bytes); |
4330 | self.y.serialize_into(bytes); |
4331 | } |
4332 | } |
4333 | impl From<&GravityNotifyEvent> for [u8; 32] { |
4334 | fn from(input: &GravityNotifyEvent) -> Self { |
4335 | let response_type_bytes = input.response_type.serialize(); |
4336 | let sequence_bytes = input.sequence.serialize(); |
4337 | let event_bytes = input.event.serialize(); |
4338 | let window_bytes = input.window.serialize(); |
4339 | let x_bytes = input.x.serialize(); |
4340 | let y_bytes = input.y.serialize(); |
4341 | [ |
4342 | response_type_bytes[0], |
4343 | 0, |
4344 | sequence_bytes[0], |
4345 | sequence_bytes[1], |
4346 | event_bytes[0], |
4347 | event_bytes[1], |
4348 | event_bytes[2], |
4349 | event_bytes[3], |
4350 | window_bytes[0], |
4351 | window_bytes[1], |
4352 | window_bytes[2], |
4353 | window_bytes[3], |
4354 | x_bytes[0], |
4355 | x_bytes[1], |
4356 | y_bytes[0], |
4357 | y_bytes[1], |
4358 | // trailing padding |
4359 | 0, |
4360 | 0, |
4361 | 0, |
4362 | 0, |
4363 | 0, |
4364 | 0, |
4365 | 0, |
4366 | 0, |
4367 | 0, |
4368 | 0, |
4369 | 0, |
4370 | 0, |
4371 | 0, |
4372 | 0, |
4373 | 0, |
4374 | 0, |
4375 | ] |
4376 | } |
4377 | } |
4378 | impl From<GravityNotifyEvent> for [u8; 32] { |
4379 | fn from(input: GravityNotifyEvent) -> Self { |
4380 | Self::from(&input) |
4381 | } |
4382 | } |
4383 | |
4384 | /// Opcode for the ResizeRequest event |
4385 | pub const RESIZE_REQUEST_EVENT: u8 = 25; |
4386 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
4387 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
4388 | pub struct ResizeRequestEvent { |
4389 | pub response_type: u8, |
4390 | pub sequence: u16, |
4391 | pub window: Window, |
4392 | pub width: u16, |
4393 | pub height: u16, |
4394 | } |
4395 | impl TryParse for ResizeRequestEvent { |
4396 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
4397 | let remaining: &[u8] = initial_value; |
4398 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
4399 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
4400 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
4401 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
4402 | let (width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
4403 | let (height: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
4404 | let result: ResizeRequestEvent = ResizeRequestEvent { response_type, sequence, window, width, height }; |
4405 | let _ = remaining; |
4406 | let remaining: &[u8] = initial_value.get(32..) |
4407 | .ok_or(err:ParseError::InsufficientData)?; |
4408 | Ok((result, remaining)) |
4409 | } |
4410 | } |
4411 | impl Serialize for ResizeRequestEvent { |
4412 | type Bytes = [u8; 12]; |
4413 | fn serialize(&self) -> [u8; 12] { |
4414 | let response_type_bytes = self.response_type.serialize(); |
4415 | let sequence_bytes = self.sequence.serialize(); |
4416 | let window_bytes = self.window.serialize(); |
4417 | let width_bytes = self.width.serialize(); |
4418 | let height_bytes = self.height.serialize(); |
4419 | [ |
4420 | response_type_bytes[0], |
4421 | 0, |
4422 | sequence_bytes[0], |
4423 | sequence_bytes[1], |
4424 | window_bytes[0], |
4425 | window_bytes[1], |
4426 | window_bytes[2], |
4427 | window_bytes[3], |
4428 | width_bytes[0], |
4429 | width_bytes[1], |
4430 | height_bytes[0], |
4431 | height_bytes[1], |
4432 | ] |
4433 | } |
4434 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
4435 | bytes.reserve(12); |
4436 | self.response_type.serialize_into(bytes); |
4437 | bytes.extend_from_slice(&[0; 1]); |
4438 | self.sequence.serialize_into(bytes); |
4439 | self.window.serialize_into(bytes); |
4440 | self.width.serialize_into(bytes); |
4441 | self.height.serialize_into(bytes); |
4442 | } |
4443 | } |
4444 | impl From<&ResizeRequestEvent> for [u8; 32] { |
4445 | fn from(input: &ResizeRequestEvent) -> Self { |
4446 | let response_type_bytes = input.response_type.serialize(); |
4447 | let sequence_bytes = input.sequence.serialize(); |
4448 | let window_bytes = input.window.serialize(); |
4449 | let width_bytes = input.width.serialize(); |
4450 | let height_bytes = input.height.serialize(); |
4451 | [ |
4452 | response_type_bytes[0], |
4453 | 0, |
4454 | sequence_bytes[0], |
4455 | sequence_bytes[1], |
4456 | window_bytes[0], |
4457 | window_bytes[1], |
4458 | window_bytes[2], |
4459 | window_bytes[3], |
4460 | width_bytes[0], |
4461 | width_bytes[1], |
4462 | height_bytes[0], |
4463 | height_bytes[1], |
4464 | // trailing padding |
4465 | 0, |
4466 | 0, |
4467 | 0, |
4468 | 0, |
4469 | 0, |
4470 | 0, |
4471 | 0, |
4472 | 0, |
4473 | 0, |
4474 | 0, |
4475 | 0, |
4476 | 0, |
4477 | 0, |
4478 | 0, |
4479 | 0, |
4480 | 0, |
4481 | 0, |
4482 | 0, |
4483 | 0, |
4484 | 0, |
4485 | ] |
4486 | } |
4487 | } |
4488 | impl From<ResizeRequestEvent> for [u8; 32] { |
4489 | fn from(input: ResizeRequestEvent) -> Self { |
4490 | Self::from(&input) |
4491 | } |
4492 | } |
4493 | |
4494 | /// # Fields |
4495 | /// |
4496 | /// * `OnTop` - The window is now on top of all siblings. |
4497 | /// * `OnBottom` - The window is now below all siblings. |
4498 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
4499 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
4500 | pub struct Place(u8); |
4501 | impl Place { |
4502 | pub const ON_TOP: Self = Self(0); |
4503 | pub const ON_BOTTOM: Self = Self(1); |
4504 | } |
4505 | impl From<Place> for u8 { |
4506 | #[inline ] |
4507 | fn from(input: Place) -> Self { |
4508 | input.0 |
4509 | } |
4510 | } |
4511 | impl From<Place> for Option<u8> { |
4512 | #[inline ] |
4513 | fn from(input: Place) -> Self { |
4514 | Some(input.0) |
4515 | } |
4516 | } |
4517 | impl From<Place> for u16 { |
4518 | #[inline ] |
4519 | fn from(input: Place) -> Self { |
4520 | u16::from(input.0) |
4521 | } |
4522 | } |
4523 | impl From<Place> for Option<u16> { |
4524 | #[inline ] |
4525 | fn from(input: Place) -> Self { |
4526 | Some(u16::from(input.0)) |
4527 | } |
4528 | } |
4529 | impl From<Place> for u32 { |
4530 | #[inline ] |
4531 | fn from(input: Place) -> Self { |
4532 | u32::from(input.0) |
4533 | } |
4534 | } |
4535 | impl From<Place> for Option<u32> { |
4536 | #[inline ] |
4537 | fn from(input: Place) -> Self { |
4538 | Some(u32::from(input.0)) |
4539 | } |
4540 | } |
4541 | impl From<u8> for Place { |
4542 | #[inline ] |
4543 | fn from(value: u8) -> Self { |
4544 | Self(value) |
4545 | } |
4546 | } |
4547 | impl core::fmt::Debug for Place { |
4548 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
4549 | let variants: [(u32, &str, &str); 2] = [ |
4550 | (Self::ON_TOP.0.into(), "ON_TOP" , "OnTop" ), |
4551 | (Self::ON_BOTTOM.0.into(), "ON_BOTTOM" , "OnBottom" ), |
4552 | ]; |
4553 | pretty_print_enum(fmt, self.0.into(), &variants) |
4554 | } |
4555 | } |
4556 | |
4557 | /// Opcode for the CirculateNotify event |
4558 | pub const CIRCULATE_NOTIFY_EVENT: u8 = 26; |
4559 | /// NOT YET DOCUMENTED. |
4560 | /// |
4561 | /// # Fields |
4562 | /// |
4563 | /// * `event` - Either the restacked window or its parent, depending on whether |
4564 | /// `StructureNotify` or `SubstructureNotify` was selected. |
4565 | /// * `window` - The restacked window. |
4566 | /// * `place` - |
4567 | /// |
4568 | /// # See |
4569 | /// |
4570 | /// * `CirculateWindow`: request |
4571 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
4572 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
4573 | pub struct CirculateNotifyEvent { |
4574 | pub response_type: u8, |
4575 | pub sequence: u16, |
4576 | pub event: Window, |
4577 | pub window: Window, |
4578 | pub place: Place, |
4579 | } |
4580 | impl TryParse for CirculateNotifyEvent { |
4581 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
4582 | let remaining: &[u8] = initial_value; |
4583 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
4584 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
4585 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
4586 | let (event: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
4587 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
4588 | let remaining: &[u8] = remaining.get(4..).ok_or(err:ParseError::InsufficientData)?; |
4589 | let (place: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
4590 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
4591 | let place: Place = place.into(); |
4592 | let result: CirculateNotifyEvent = CirculateNotifyEvent { response_type, sequence, event, window, place }; |
4593 | let _ = remaining; |
4594 | let remaining: &[u8] = initial_value.get(32..) |
4595 | .ok_or(err:ParseError::InsufficientData)?; |
4596 | Ok((result, remaining)) |
4597 | } |
4598 | } |
4599 | impl Serialize for CirculateNotifyEvent { |
4600 | type Bytes = [u8; 20]; |
4601 | fn serialize(&self) -> [u8; 20] { |
4602 | let response_type_bytes = self.response_type.serialize(); |
4603 | let sequence_bytes = self.sequence.serialize(); |
4604 | let event_bytes = self.event.serialize(); |
4605 | let window_bytes = self.window.serialize(); |
4606 | let place_bytes = u8::from(self.place).serialize(); |
4607 | [ |
4608 | response_type_bytes[0], |
4609 | 0, |
4610 | sequence_bytes[0], |
4611 | sequence_bytes[1], |
4612 | event_bytes[0], |
4613 | event_bytes[1], |
4614 | event_bytes[2], |
4615 | event_bytes[3], |
4616 | window_bytes[0], |
4617 | window_bytes[1], |
4618 | window_bytes[2], |
4619 | window_bytes[3], |
4620 | 0, |
4621 | 0, |
4622 | 0, |
4623 | 0, |
4624 | place_bytes[0], |
4625 | 0, |
4626 | 0, |
4627 | 0, |
4628 | ] |
4629 | } |
4630 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
4631 | bytes.reserve(20); |
4632 | self.response_type.serialize_into(bytes); |
4633 | bytes.extend_from_slice(&[0; 1]); |
4634 | self.sequence.serialize_into(bytes); |
4635 | self.event.serialize_into(bytes); |
4636 | self.window.serialize_into(bytes); |
4637 | bytes.extend_from_slice(&[0; 4]); |
4638 | u8::from(self.place).serialize_into(bytes); |
4639 | bytes.extend_from_slice(&[0; 3]); |
4640 | } |
4641 | } |
4642 | impl From<&CirculateNotifyEvent> for [u8; 32] { |
4643 | fn from(input: &CirculateNotifyEvent) -> Self { |
4644 | let response_type_bytes = input.response_type.serialize(); |
4645 | let sequence_bytes = input.sequence.serialize(); |
4646 | let event_bytes = input.event.serialize(); |
4647 | let window_bytes = input.window.serialize(); |
4648 | let place_bytes = u8::from(input.place).serialize(); |
4649 | [ |
4650 | response_type_bytes[0], |
4651 | 0, |
4652 | sequence_bytes[0], |
4653 | sequence_bytes[1], |
4654 | event_bytes[0], |
4655 | event_bytes[1], |
4656 | event_bytes[2], |
4657 | event_bytes[3], |
4658 | window_bytes[0], |
4659 | window_bytes[1], |
4660 | window_bytes[2], |
4661 | window_bytes[3], |
4662 | 0, |
4663 | 0, |
4664 | 0, |
4665 | 0, |
4666 | place_bytes[0], |
4667 | 0, |
4668 | 0, |
4669 | 0, |
4670 | // trailing padding |
4671 | 0, |
4672 | 0, |
4673 | 0, |
4674 | 0, |
4675 | 0, |
4676 | 0, |
4677 | 0, |
4678 | 0, |
4679 | 0, |
4680 | 0, |
4681 | 0, |
4682 | 0, |
4683 | ] |
4684 | } |
4685 | } |
4686 | impl From<CirculateNotifyEvent> for [u8; 32] { |
4687 | fn from(input: CirculateNotifyEvent) -> Self { |
4688 | Self::from(&input) |
4689 | } |
4690 | } |
4691 | |
4692 | /// Opcode for the CirculateRequest event |
4693 | pub const CIRCULATE_REQUEST_EVENT: u8 = 27; |
4694 | pub type CirculateRequestEvent = CirculateNotifyEvent; |
4695 | |
4696 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
4697 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
4698 | pub struct Property(u8); |
4699 | impl Property { |
4700 | pub const NEW_VALUE: Self = Self(0); |
4701 | pub const DELETE: Self = Self(1); |
4702 | } |
4703 | impl From<Property> for u8 { |
4704 | #[inline ] |
4705 | fn from(input: Property) -> Self { |
4706 | input.0 |
4707 | } |
4708 | } |
4709 | impl From<Property> for Option<u8> { |
4710 | #[inline ] |
4711 | fn from(input: Property) -> Self { |
4712 | Some(input.0) |
4713 | } |
4714 | } |
4715 | impl From<Property> for u16 { |
4716 | #[inline ] |
4717 | fn from(input: Property) -> Self { |
4718 | u16::from(input.0) |
4719 | } |
4720 | } |
4721 | impl From<Property> for Option<u16> { |
4722 | #[inline ] |
4723 | fn from(input: Property) -> Self { |
4724 | Some(u16::from(input.0)) |
4725 | } |
4726 | } |
4727 | impl From<Property> for u32 { |
4728 | #[inline ] |
4729 | fn from(input: Property) -> Self { |
4730 | u32::from(input.0) |
4731 | } |
4732 | } |
4733 | impl From<Property> for Option<u32> { |
4734 | #[inline ] |
4735 | fn from(input: Property) -> Self { |
4736 | Some(u32::from(input.0)) |
4737 | } |
4738 | } |
4739 | impl From<u8> for Property { |
4740 | #[inline ] |
4741 | fn from(value: u8) -> Self { |
4742 | Self(value) |
4743 | } |
4744 | } |
4745 | impl core::fmt::Debug for Property { |
4746 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
4747 | let variants: [(u32, &str, &str); 2] = [ |
4748 | (Self::NEW_VALUE.0.into(), "NEW_VALUE" , "NewValue" ), |
4749 | (Self::DELETE.0.into(), "DELETE" , "Delete" ), |
4750 | ]; |
4751 | pretty_print_enum(fmt, self.0.into(), &variants) |
4752 | } |
4753 | } |
4754 | |
4755 | /// Opcode for the PropertyNotify event |
4756 | pub const PROPERTY_NOTIFY_EVENT: u8 = 28; |
4757 | /// a window property changed. |
4758 | /// |
4759 | /// # Fields |
4760 | /// |
4761 | /// * `window` - The window whose associated property was changed. |
4762 | /// * `atom` - The property's atom, to indicate which property was changed. |
4763 | /// * `time` - A timestamp of the server time when the property was changed. |
4764 | /// * `state` - |
4765 | /// |
4766 | /// # See |
4767 | /// |
4768 | /// * `ChangeProperty`: request |
4769 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
4770 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
4771 | pub struct PropertyNotifyEvent { |
4772 | pub response_type: u8, |
4773 | pub sequence: u16, |
4774 | pub window: Window, |
4775 | pub atom: Atom, |
4776 | pub time: Timestamp, |
4777 | pub state: Property, |
4778 | } |
4779 | impl TryParse for PropertyNotifyEvent { |
4780 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
4781 | let remaining: &[u8] = initial_value; |
4782 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
4783 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
4784 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
4785 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
4786 | let (atom: u32, remaining: &[u8]) = Atom::try_parse(remaining)?; |
4787 | let (time: u32, remaining: &[u8]) = Timestamp::try_parse(remaining)?; |
4788 | let (state: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
4789 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
4790 | let state: Property = state.into(); |
4791 | let result: PropertyNotifyEvent = PropertyNotifyEvent { response_type, sequence, window, atom, time, state }; |
4792 | let _ = remaining; |
4793 | let remaining: &[u8] = initial_value.get(32..) |
4794 | .ok_or(err:ParseError::InsufficientData)?; |
4795 | Ok((result, remaining)) |
4796 | } |
4797 | } |
4798 | impl Serialize for PropertyNotifyEvent { |
4799 | type Bytes = [u8; 20]; |
4800 | fn serialize(&self) -> [u8; 20] { |
4801 | let response_type_bytes = self.response_type.serialize(); |
4802 | let sequence_bytes = self.sequence.serialize(); |
4803 | let window_bytes = self.window.serialize(); |
4804 | let atom_bytes = self.atom.serialize(); |
4805 | let time_bytes = self.time.serialize(); |
4806 | let state_bytes = u8::from(self.state).serialize(); |
4807 | [ |
4808 | response_type_bytes[0], |
4809 | 0, |
4810 | sequence_bytes[0], |
4811 | sequence_bytes[1], |
4812 | window_bytes[0], |
4813 | window_bytes[1], |
4814 | window_bytes[2], |
4815 | window_bytes[3], |
4816 | atom_bytes[0], |
4817 | atom_bytes[1], |
4818 | atom_bytes[2], |
4819 | atom_bytes[3], |
4820 | time_bytes[0], |
4821 | time_bytes[1], |
4822 | time_bytes[2], |
4823 | time_bytes[3], |
4824 | state_bytes[0], |
4825 | 0, |
4826 | 0, |
4827 | 0, |
4828 | ] |
4829 | } |
4830 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
4831 | bytes.reserve(20); |
4832 | self.response_type.serialize_into(bytes); |
4833 | bytes.extend_from_slice(&[0; 1]); |
4834 | self.sequence.serialize_into(bytes); |
4835 | self.window.serialize_into(bytes); |
4836 | self.atom.serialize_into(bytes); |
4837 | self.time.serialize_into(bytes); |
4838 | u8::from(self.state).serialize_into(bytes); |
4839 | bytes.extend_from_slice(&[0; 3]); |
4840 | } |
4841 | } |
4842 | impl From<&PropertyNotifyEvent> for [u8; 32] { |
4843 | fn from(input: &PropertyNotifyEvent) -> Self { |
4844 | let response_type_bytes = input.response_type.serialize(); |
4845 | let sequence_bytes = input.sequence.serialize(); |
4846 | let window_bytes = input.window.serialize(); |
4847 | let atom_bytes = input.atom.serialize(); |
4848 | let time_bytes = input.time.serialize(); |
4849 | let state_bytes = u8::from(input.state).serialize(); |
4850 | [ |
4851 | response_type_bytes[0], |
4852 | 0, |
4853 | sequence_bytes[0], |
4854 | sequence_bytes[1], |
4855 | window_bytes[0], |
4856 | window_bytes[1], |
4857 | window_bytes[2], |
4858 | window_bytes[3], |
4859 | atom_bytes[0], |
4860 | atom_bytes[1], |
4861 | atom_bytes[2], |
4862 | atom_bytes[3], |
4863 | time_bytes[0], |
4864 | time_bytes[1], |
4865 | time_bytes[2], |
4866 | time_bytes[3], |
4867 | state_bytes[0], |
4868 | 0, |
4869 | 0, |
4870 | 0, |
4871 | // trailing padding |
4872 | 0, |
4873 | 0, |
4874 | 0, |
4875 | 0, |
4876 | 0, |
4877 | 0, |
4878 | 0, |
4879 | 0, |
4880 | 0, |
4881 | 0, |
4882 | 0, |
4883 | 0, |
4884 | ] |
4885 | } |
4886 | } |
4887 | impl From<PropertyNotifyEvent> for [u8; 32] { |
4888 | fn from(input: PropertyNotifyEvent) -> Self { |
4889 | Self::from(&input) |
4890 | } |
4891 | } |
4892 | |
4893 | /// Opcode for the SelectionClear event |
4894 | pub const SELECTION_CLEAR_EVENT: u8 = 29; |
4895 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
4896 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
4897 | pub struct SelectionClearEvent { |
4898 | pub response_type: u8, |
4899 | pub sequence: u16, |
4900 | pub time: Timestamp, |
4901 | pub owner: Window, |
4902 | pub selection: Atom, |
4903 | } |
4904 | impl TryParse for SelectionClearEvent { |
4905 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
4906 | let remaining: &[u8] = initial_value; |
4907 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
4908 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
4909 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
4910 | let (time: u32, remaining: &[u8]) = Timestamp::try_parse(remaining)?; |
4911 | let (owner: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
4912 | let (selection: u32, remaining: &[u8]) = Atom::try_parse(remaining)?; |
4913 | let result: SelectionClearEvent = SelectionClearEvent { response_type, sequence, time, owner, selection }; |
4914 | let _ = remaining; |
4915 | let remaining: &[u8] = initial_value.get(32..) |
4916 | .ok_or(err:ParseError::InsufficientData)?; |
4917 | Ok((result, remaining)) |
4918 | } |
4919 | } |
4920 | impl Serialize for SelectionClearEvent { |
4921 | type Bytes = [u8; 16]; |
4922 | fn serialize(&self) -> [u8; 16] { |
4923 | let response_type_bytes = self.response_type.serialize(); |
4924 | let sequence_bytes = self.sequence.serialize(); |
4925 | let time_bytes = self.time.serialize(); |
4926 | let owner_bytes = self.owner.serialize(); |
4927 | let selection_bytes = self.selection.serialize(); |
4928 | [ |
4929 | response_type_bytes[0], |
4930 | 0, |
4931 | sequence_bytes[0], |
4932 | sequence_bytes[1], |
4933 | time_bytes[0], |
4934 | time_bytes[1], |
4935 | time_bytes[2], |
4936 | time_bytes[3], |
4937 | owner_bytes[0], |
4938 | owner_bytes[1], |
4939 | owner_bytes[2], |
4940 | owner_bytes[3], |
4941 | selection_bytes[0], |
4942 | selection_bytes[1], |
4943 | selection_bytes[2], |
4944 | selection_bytes[3], |
4945 | ] |
4946 | } |
4947 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
4948 | bytes.reserve(16); |
4949 | self.response_type.serialize_into(bytes); |
4950 | bytes.extend_from_slice(&[0; 1]); |
4951 | self.sequence.serialize_into(bytes); |
4952 | self.time.serialize_into(bytes); |
4953 | self.owner.serialize_into(bytes); |
4954 | self.selection.serialize_into(bytes); |
4955 | } |
4956 | } |
4957 | impl From<&SelectionClearEvent> for [u8; 32] { |
4958 | fn from(input: &SelectionClearEvent) -> Self { |
4959 | let response_type_bytes = input.response_type.serialize(); |
4960 | let sequence_bytes = input.sequence.serialize(); |
4961 | let time_bytes = input.time.serialize(); |
4962 | let owner_bytes = input.owner.serialize(); |
4963 | let selection_bytes = input.selection.serialize(); |
4964 | [ |
4965 | response_type_bytes[0], |
4966 | 0, |
4967 | sequence_bytes[0], |
4968 | sequence_bytes[1], |
4969 | time_bytes[0], |
4970 | time_bytes[1], |
4971 | time_bytes[2], |
4972 | time_bytes[3], |
4973 | owner_bytes[0], |
4974 | owner_bytes[1], |
4975 | owner_bytes[2], |
4976 | owner_bytes[3], |
4977 | selection_bytes[0], |
4978 | selection_bytes[1], |
4979 | selection_bytes[2], |
4980 | selection_bytes[3], |
4981 | // trailing padding |
4982 | 0, |
4983 | 0, |
4984 | 0, |
4985 | 0, |
4986 | 0, |
4987 | 0, |
4988 | 0, |
4989 | 0, |
4990 | 0, |
4991 | 0, |
4992 | 0, |
4993 | 0, |
4994 | 0, |
4995 | 0, |
4996 | 0, |
4997 | 0, |
4998 | ] |
4999 | } |
5000 | } |
5001 | impl From<SelectionClearEvent> for [u8; 32] { |
5002 | fn from(input: SelectionClearEvent) -> Self { |
5003 | Self::from(&input) |
5004 | } |
5005 | } |
5006 | |
5007 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
5008 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
5009 | pub struct Time(u8); |
5010 | impl Time { |
5011 | pub const CURRENT_TIME: Self = Self(0); |
5012 | } |
5013 | impl From<Time> for u8 { |
5014 | #[inline ] |
5015 | fn from(input: Time) -> Self { |
5016 | input.0 |
5017 | } |
5018 | } |
5019 | impl From<Time> for Option<u8> { |
5020 | #[inline ] |
5021 | fn from(input: Time) -> Self { |
5022 | Some(input.0) |
5023 | } |
5024 | } |
5025 | impl From<Time> for u16 { |
5026 | #[inline ] |
5027 | fn from(input: Time) -> Self { |
5028 | u16::from(input.0) |
5029 | } |
5030 | } |
5031 | impl From<Time> for Option<u16> { |
5032 | #[inline ] |
5033 | fn from(input: Time) -> Self { |
5034 | Some(u16::from(input.0)) |
5035 | } |
5036 | } |
5037 | impl From<Time> for u32 { |
5038 | #[inline ] |
5039 | fn from(input: Time) -> Self { |
5040 | u32::from(input.0) |
5041 | } |
5042 | } |
5043 | impl From<Time> for Option<u32> { |
5044 | #[inline ] |
5045 | fn from(input: Time) -> Self { |
5046 | Some(u32::from(input.0)) |
5047 | } |
5048 | } |
5049 | impl From<u8> for Time { |
5050 | #[inline ] |
5051 | fn from(value: u8) -> Self { |
5052 | Self(value) |
5053 | } |
5054 | } |
5055 | impl core::fmt::Debug for Time { |
5056 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
5057 | let variants: [(u32, &str, &str); 1] = [ |
5058 | (Self::CURRENT_TIME.0.into(), "CURRENT_TIME" , "CurrentTime" ), |
5059 | ]; |
5060 | pretty_print_enum(fmt, self.0.into(), &variants) |
5061 | } |
5062 | } |
5063 | |
5064 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
5065 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
5066 | pub struct AtomEnum(u8); |
5067 | impl AtomEnum { |
5068 | pub const NONE: Self = Self(0); |
5069 | pub const ANY: Self = Self(0); |
5070 | pub const PRIMARY: Self = Self(1); |
5071 | pub const SECONDARY: Self = Self(2); |
5072 | pub const ARC: Self = Self(3); |
5073 | pub const ATOM: Self = Self(4); |
5074 | pub const BITMAP: Self = Self(5); |
5075 | pub const CARDINAL: Self = Self(6); |
5076 | pub const COLORMAP: Self = Self(7); |
5077 | pub const CURSOR: Self = Self(8); |
5078 | pub const CUT_BUFFE_R0: Self = Self(9); |
5079 | pub const CUT_BUFFE_R1: Self = Self(10); |
5080 | pub const CUT_BUFFE_R2: Self = Self(11); |
5081 | pub const CUT_BUFFE_R3: Self = Self(12); |
5082 | pub const CUT_BUFFE_R4: Self = Self(13); |
5083 | pub const CUT_BUFFE_R5: Self = Self(14); |
5084 | pub const CUT_BUFFE_R6: Self = Self(15); |
5085 | pub const CUT_BUFFE_R7: Self = Self(16); |
5086 | pub const DRAWABLE: Self = Self(17); |
5087 | pub const FONT: Self = Self(18); |
5088 | pub const INTEGER: Self = Self(19); |
5089 | pub const PIXMAP: Self = Self(20); |
5090 | pub const POINT: Self = Self(21); |
5091 | pub const RECTANGLE: Self = Self(22); |
5092 | pub const RESOURCE_MANAGER: Self = Self(23); |
5093 | pub const RGB_COLOR_MAP: Self = Self(24); |
5094 | pub const RGB_BEST_MAP: Self = Self(25); |
5095 | pub const RGB_BLUE_MAP: Self = Self(26); |
5096 | pub const RGB_DEFAULT_MAP: Self = Self(27); |
5097 | pub const RGB_GRAY_MAP: Self = Self(28); |
5098 | pub const RGB_GREEN_MAP: Self = Self(29); |
5099 | pub const RGB_RED_MAP: Self = Self(30); |
5100 | pub const STRING: Self = Self(31); |
5101 | pub const VISUALID: Self = Self(32); |
5102 | pub const WINDOW: Self = Self(33); |
5103 | pub const WM_COMMAND: Self = Self(34); |
5104 | pub const WM_HINTS: Self = Self(35); |
5105 | pub const WM_CLIENT_MACHINE: Self = Self(36); |
5106 | pub const WM_ICON_NAME: Self = Self(37); |
5107 | pub const WM_ICON_SIZE: Self = Self(38); |
5108 | pub const WM_NAME: Self = Self(39); |
5109 | pub const WM_NORMAL_HINTS: Self = Self(40); |
5110 | pub const WM_SIZE_HINTS: Self = Self(41); |
5111 | pub const WM_ZOOM_HINTS: Self = Self(42); |
5112 | pub const MIN_SPACE: Self = Self(43); |
5113 | pub const NORM_SPACE: Self = Self(44); |
5114 | pub const MAX_SPACE: Self = Self(45); |
5115 | pub const END_SPACE: Self = Self(46); |
5116 | pub const SUPERSCRIPT_X: Self = Self(47); |
5117 | pub const SUPERSCRIPT_Y: Self = Self(48); |
5118 | pub const SUBSCRIPT_X: Self = Self(49); |
5119 | pub const SUBSCRIPT_Y: Self = Self(50); |
5120 | pub const UNDERLINE_POSITION: Self = Self(51); |
5121 | pub const UNDERLINE_THICKNESS: Self = Self(52); |
5122 | pub const STRIKEOUT_ASCENT: Self = Self(53); |
5123 | pub const STRIKEOUT_DESCENT: Self = Self(54); |
5124 | pub const ITALIC_ANGLE: Self = Self(55); |
5125 | pub const X_HEIGHT: Self = Self(56); |
5126 | pub const QUAD_WIDTH: Self = Self(57); |
5127 | pub const WEIGHT: Self = Self(58); |
5128 | pub const POINT_SIZE: Self = Self(59); |
5129 | pub const RESOLUTION: Self = Self(60); |
5130 | pub const COPYRIGHT: Self = Self(61); |
5131 | pub const NOTICE: Self = Self(62); |
5132 | pub const FONT_NAME: Self = Self(63); |
5133 | pub const FAMILY_NAME: Self = Self(64); |
5134 | pub const FULL_NAME: Self = Self(65); |
5135 | pub const CAP_HEIGHT: Self = Self(66); |
5136 | pub const WM_CLASS: Self = Self(67); |
5137 | pub const WM_TRANSIENT_FOR: Self = Self(68); |
5138 | } |
5139 | impl From<AtomEnum> for u8 { |
5140 | #[inline ] |
5141 | fn from(input: AtomEnum) -> Self { |
5142 | input.0 |
5143 | } |
5144 | } |
5145 | impl From<AtomEnum> for Option<u8> { |
5146 | #[inline ] |
5147 | fn from(input: AtomEnum) -> Self { |
5148 | Some(input.0) |
5149 | } |
5150 | } |
5151 | impl From<AtomEnum> for u16 { |
5152 | #[inline ] |
5153 | fn from(input: AtomEnum) -> Self { |
5154 | u16::from(input.0) |
5155 | } |
5156 | } |
5157 | impl From<AtomEnum> for Option<u16> { |
5158 | #[inline ] |
5159 | fn from(input: AtomEnum) -> Self { |
5160 | Some(u16::from(input.0)) |
5161 | } |
5162 | } |
5163 | impl From<AtomEnum> for u32 { |
5164 | #[inline ] |
5165 | fn from(input: AtomEnum) -> Self { |
5166 | u32::from(input.0) |
5167 | } |
5168 | } |
5169 | impl From<AtomEnum> for Option<u32> { |
5170 | #[inline ] |
5171 | fn from(input: AtomEnum) -> Self { |
5172 | Some(u32::from(input.0)) |
5173 | } |
5174 | } |
5175 | impl From<u8> for AtomEnum { |
5176 | #[inline ] |
5177 | fn from(value: u8) -> Self { |
5178 | Self(value) |
5179 | } |
5180 | } |
5181 | impl core::fmt::Debug for AtomEnum { |
5182 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
5183 | let variants = [ |
5184 | (Self::NONE.0.into(), "NONE" , "None" ), |
5185 | (Self::ANY.0.into(), "ANY" , "Any" ), |
5186 | (Self::PRIMARY.0.into(), "PRIMARY" , "PRIMARY" ), |
5187 | (Self::SECONDARY.0.into(), "SECONDARY" , "SECONDARY" ), |
5188 | (Self::ARC.0.into(), "ARC" , "ARC" ), |
5189 | (Self::ATOM.0.into(), "ATOM" , "ATOM" ), |
5190 | (Self::BITMAP.0.into(), "BITMAP" , "BITMAP" ), |
5191 | (Self::CARDINAL.0.into(), "CARDINAL" , "CARDINAL" ), |
5192 | (Self::COLORMAP.0.into(), "COLORMAP" , "COLORMAP" ), |
5193 | (Self::CURSOR.0.into(), "CURSOR" , "CURSOR" ), |
5194 | (Self::CUT_BUFFE_R0.0.into(), "CUT_BUFFE_R0" , "CUT_BUFFER0" ), |
5195 | (Self::CUT_BUFFE_R1.0.into(), "CUT_BUFFE_R1" , "CUT_BUFFER1" ), |
5196 | (Self::CUT_BUFFE_R2.0.into(), "CUT_BUFFE_R2" , "CUT_BUFFER2" ), |
5197 | (Self::CUT_BUFFE_R3.0.into(), "CUT_BUFFE_R3" , "CUT_BUFFER3" ), |
5198 | (Self::CUT_BUFFE_R4.0.into(), "CUT_BUFFE_R4" , "CUT_BUFFER4" ), |
5199 | (Self::CUT_BUFFE_R5.0.into(), "CUT_BUFFE_R5" , "CUT_BUFFER5" ), |
5200 | (Self::CUT_BUFFE_R6.0.into(), "CUT_BUFFE_R6" , "CUT_BUFFER6" ), |
5201 | (Self::CUT_BUFFE_R7.0.into(), "CUT_BUFFE_R7" , "CUT_BUFFER7" ), |
5202 | (Self::DRAWABLE.0.into(), "DRAWABLE" , "DRAWABLE" ), |
5203 | (Self::FONT.0.into(), "FONT" , "FONT" ), |
5204 | (Self::INTEGER.0.into(), "INTEGER" , "INTEGER" ), |
5205 | (Self::PIXMAP.0.into(), "PIXMAP" , "PIXMAP" ), |
5206 | (Self::POINT.0.into(), "POINT" , "POINT" ), |
5207 | (Self::RECTANGLE.0.into(), "RECTANGLE" , "RECTANGLE" ), |
5208 | (Self::RESOURCE_MANAGER.0.into(), "RESOURCE_MANAGER" , "RESOURCE_MANAGER" ), |
5209 | (Self::RGB_COLOR_MAP.0.into(), "RGB_COLOR_MAP" , "RGB_COLOR_MAP" ), |
5210 | (Self::RGB_BEST_MAP.0.into(), "RGB_BEST_MAP" , "RGB_BEST_MAP" ), |
5211 | (Self::RGB_BLUE_MAP.0.into(), "RGB_BLUE_MAP" , "RGB_BLUE_MAP" ), |
5212 | (Self::RGB_DEFAULT_MAP.0.into(), "RGB_DEFAULT_MAP" , "RGB_DEFAULT_MAP" ), |
5213 | (Self::RGB_GRAY_MAP.0.into(), "RGB_GRAY_MAP" , "RGB_GRAY_MAP" ), |
5214 | (Self::RGB_GREEN_MAP.0.into(), "RGB_GREEN_MAP" , "RGB_GREEN_MAP" ), |
5215 | (Self::RGB_RED_MAP.0.into(), "RGB_RED_MAP" , "RGB_RED_MAP" ), |
5216 | (Self::STRING.0.into(), "STRING" , "STRING" ), |
5217 | (Self::VISUALID.0.into(), "VISUALID" , "VISUALID" ), |
5218 | (Self::WINDOW.0.into(), "WINDOW" , "WINDOW" ), |
5219 | (Self::WM_COMMAND.0.into(), "WM_COMMAND" , "WM_COMMAND" ), |
5220 | (Self::WM_HINTS.0.into(), "WM_HINTS" , "WM_HINTS" ), |
5221 | (Self::WM_CLIENT_MACHINE.0.into(), "WM_CLIENT_MACHINE" , "WM_CLIENT_MACHINE" ), |
5222 | (Self::WM_ICON_NAME.0.into(), "WM_ICON_NAME" , "WM_ICON_NAME" ), |
5223 | (Self::WM_ICON_SIZE.0.into(), "WM_ICON_SIZE" , "WM_ICON_SIZE" ), |
5224 | (Self::WM_NAME.0.into(), "WM_NAME" , "WM_NAME" ), |
5225 | (Self::WM_NORMAL_HINTS.0.into(), "WM_NORMAL_HINTS" , "WM_NORMAL_HINTS" ), |
5226 | (Self::WM_SIZE_HINTS.0.into(), "WM_SIZE_HINTS" , "WM_SIZE_HINTS" ), |
5227 | (Self::WM_ZOOM_HINTS.0.into(), "WM_ZOOM_HINTS" , "WM_ZOOM_HINTS" ), |
5228 | (Self::MIN_SPACE.0.into(), "MIN_SPACE" , "MIN_SPACE" ), |
5229 | (Self::NORM_SPACE.0.into(), "NORM_SPACE" , "NORM_SPACE" ), |
5230 | (Self::MAX_SPACE.0.into(), "MAX_SPACE" , "MAX_SPACE" ), |
5231 | (Self::END_SPACE.0.into(), "END_SPACE" , "END_SPACE" ), |
5232 | (Self::SUPERSCRIPT_X.0.into(), "SUPERSCRIPT_X" , "SUPERSCRIPT_X" ), |
5233 | (Self::SUPERSCRIPT_Y.0.into(), "SUPERSCRIPT_Y" , "SUPERSCRIPT_Y" ), |
5234 | (Self::SUBSCRIPT_X.0.into(), "SUBSCRIPT_X" , "SUBSCRIPT_X" ), |
5235 | (Self::SUBSCRIPT_Y.0.into(), "SUBSCRIPT_Y" , "SUBSCRIPT_Y" ), |
5236 | (Self::UNDERLINE_POSITION.0.into(), "UNDERLINE_POSITION" , "UNDERLINE_POSITION" ), |
5237 | (Self::UNDERLINE_THICKNESS.0.into(), "UNDERLINE_THICKNESS" , "UNDERLINE_THICKNESS" ), |
5238 | (Self::STRIKEOUT_ASCENT.0.into(), "STRIKEOUT_ASCENT" , "STRIKEOUT_ASCENT" ), |
5239 | (Self::STRIKEOUT_DESCENT.0.into(), "STRIKEOUT_DESCENT" , "STRIKEOUT_DESCENT" ), |
5240 | (Self::ITALIC_ANGLE.0.into(), "ITALIC_ANGLE" , "ITALIC_ANGLE" ), |
5241 | (Self::X_HEIGHT.0.into(), "X_HEIGHT" , "X_HEIGHT" ), |
5242 | (Self::QUAD_WIDTH.0.into(), "QUAD_WIDTH" , "QUAD_WIDTH" ), |
5243 | (Self::WEIGHT.0.into(), "WEIGHT" , "WEIGHT" ), |
5244 | (Self::POINT_SIZE.0.into(), "POINT_SIZE" , "POINT_SIZE" ), |
5245 | (Self::RESOLUTION.0.into(), "RESOLUTION" , "RESOLUTION" ), |
5246 | (Self::COPYRIGHT.0.into(), "COPYRIGHT" , "COPYRIGHT" ), |
5247 | (Self::NOTICE.0.into(), "NOTICE" , "NOTICE" ), |
5248 | (Self::FONT_NAME.0.into(), "FONT_NAME" , "FONT_NAME" ), |
5249 | (Self::FAMILY_NAME.0.into(), "FAMILY_NAME" , "FAMILY_NAME" ), |
5250 | (Self::FULL_NAME.0.into(), "FULL_NAME" , "FULL_NAME" ), |
5251 | (Self::CAP_HEIGHT.0.into(), "CAP_HEIGHT" , "CAP_HEIGHT" ), |
5252 | (Self::WM_CLASS.0.into(), "WM_CLASS" , "WM_CLASS" ), |
5253 | (Self::WM_TRANSIENT_FOR.0.into(), "WM_TRANSIENT_FOR" , "WM_TRANSIENT_FOR" ), |
5254 | ]; |
5255 | pretty_print_enum(fmt, self.0.into(), &variants) |
5256 | } |
5257 | } |
5258 | |
5259 | /// Opcode for the SelectionRequest event |
5260 | pub const SELECTION_REQUEST_EVENT: u8 = 30; |
5261 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
5262 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
5263 | pub struct SelectionRequestEvent { |
5264 | pub response_type: u8, |
5265 | pub sequence: u16, |
5266 | pub time: Timestamp, |
5267 | pub owner: Window, |
5268 | pub requestor: Window, |
5269 | pub selection: Atom, |
5270 | pub target: Atom, |
5271 | pub property: Atom, |
5272 | } |
5273 | impl TryParse for SelectionRequestEvent { |
5274 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
5275 | let remaining: &[u8] = initial_value; |
5276 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
5277 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
5278 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
5279 | let (time: u32, remaining: &[u8]) = Timestamp::try_parse(remaining)?; |
5280 | let (owner: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
5281 | let (requestor: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
5282 | let (selection: u32, remaining: &[u8]) = Atom::try_parse(remaining)?; |
5283 | let (target: u32, remaining: &[u8]) = Atom::try_parse(remaining)?; |
5284 | let (property: u32, remaining: &[u8]) = Atom::try_parse(remaining)?; |
5285 | let result: SelectionRequestEvent = SelectionRequestEvent { response_type, sequence, time, owner, requestor, selection, target, property }; |
5286 | let _ = remaining; |
5287 | let remaining: &[u8] = initial_value.get(32..) |
5288 | .ok_or(err:ParseError::InsufficientData)?; |
5289 | Ok((result, remaining)) |
5290 | } |
5291 | } |
5292 | impl Serialize for SelectionRequestEvent { |
5293 | type Bytes = [u8; 28]; |
5294 | fn serialize(&self) -> [u8; 28] { |
5295 | let response_type_bytes = self.response_type.serialize(); |
5296 | let sequence_bytes = self.sequence.serialize(); |
5297 | let time_bytes = self.time.serialize(); |
5298 | let owner_bytes = self.owner.serialize(); |
5299 | let requestor_bytes = self.requestor.serialize(); |
5300 | let selection_bytes = self.selection.serialize(); |
5301 | let target_bytes = self.target.serialize(); |
5302 | let property_bytes = self.property.serialize(); |
5303 | [ |
5304 | response_type_bytes[0], |
5305 | 0, |
5306 | sequence_bytes[0], |
5307 | sequence_bytes[1], |
5308 | time_bytes[0], |
5309 | time_bytes[1], |
5310 | time_bytes[2], |
5311 | time_bytes[3], |
5312 | owner_bytes[0], |
5313 | owner_bytes[1], |
5314 | owner_bytes[2], |
5315 | owner_bytes[3], |
5316 | requestor_bytes[0], |
5317 | requestor_bytes[1], |
5318 | requestor_bytes[2], |
5319 | requestor_bytes[3], |
5320 | selection_bytes[0], |
5321 | selection_bytes[1], |
5322 | selection_bytes[2], |
5323 | selection_bytes[3], |
5324 | target_bytes[0], |
5325 | target_bytes[1], |
5326 | target_bytes[2], |
5327 | target_bytes[3], |
5328 | property_bytes[0], |
5329 | property_bytes[1], |
5330 | property_bytes[2], |
5331 | property_bytes[3], |
5332 | ] |
5333 | } |
5334 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
5335 | bytes.reserve(28); |
5336 | self.response_type.serialize_into(bytes); |
5337 | bytes.extend_from_slice(&[0; 1]); |
5338 | self.sequence.serialize_into(bytes); |
5339 | self.time.serialize_into(bytes); |
5340 | self.owner.serialize_into(bytes); |
5341 | self.requestor.serialize_into(bytes); |
5342 | self.selection.serialize_into(bytes); |
5343 | self.target.serialize_into(bytes); |
5344 | self.property.serialize_into(bytes); |
5345 | } |
5346 | } |
5347 | impl From<&SelectionRequestEvent> for [u8; 32] { |
5348 | fn from(input: &SelectionRequestEvent) -> Self { |
5349 | let response_type_bytes = input.response_type.serialize(); |
5350 | let sequence_bytes = input.sequence.serialize(); |
5351 | let time_bytes = input.time.serialize(); |
5352 | let owner_bytes = input.owner.serialize(); |
5353 | let requestor_bytes = input.requestor.serialize(); |
5354 | let selection_bytes = input.selection.serialize(); |
5355 | let target_bytes = input.target.serialize(); |
5356 | let property_bytes = input.property.serialize(); |
5357 | [ |
5358 | response_type_bytes[0], |
5359 | 0, |
5360 | sequence_bytes[0], |
5361 | sequence_bytes[1], |
5362 | time_bytes[0], |
5363 | time_bytes[1], |
5364 | time_bytes[2], |
5365 | time_bytes[3], |
5366 | owner_bytes[0], |
5367 | owner_bytes[1], |
5368 | owner_bytes[2], |
5369 | owner_bytes[3], |
5370 | requestor_bytes[0], |
5371 | requestor_bytes[1], |
5372 | requestor_bytes[2], |
5373 | requestor_bytes[3], |
5374 | selection_bytes[0], |
5375 | selection_bytes[1], |
5376 | selection_bytes[2], |
5377 | selection_bytes[3], |
5378 | target_bytes[0], |
5379 | target_bytes[1], |
5380 | target_bytes[2], |
5381 | target_bytes[3], |
5382 | property_bytes[0], |
5383 | property_bytes[1], |
5384 | property_bytes[2], |
5385 | property_bytes[3], |
5386 | // trailing padding |
5387 | 0, |
5388 | 0, |
5389 | 0, |
5390 | 0, |
5391 | ] |
5392 | } |
5393 | } |
5394 | impl From<SelectionRequestEvent> for [u8; 32] { |
5395 | fn from(input: SelectionRequestEvent) -> Self { |
5396 | Self::from(&input) |
5397 | } |
5398 | } |
5399 | |
5400 | /// Opcode for the SelectionNotify event |
5401 | pub const SELECTION_NOTIFY_EVENT: u8 = 31; |
5402 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
5403 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
5404 | pub struct SelectionNotifyEvent { |
5405 | pub response_type: u8, |
5406 | pub sequence: u16, |
5407 | pub time: Timestamp, |
5408 | pub requestor: Window, |
5409 | pub selection: Atom, |
5410 | pub target: Atom, |
5411 | pub property: Atom, |
5412 | } |
5413 | impl TryParse for SelectionNotifyEvent { |
5414 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
5415 | let remaining: &[u8] = initial_value; |
5416 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
5417 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
5418 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
5419 | let (time: u32, remaining: &[u8]) = Timestamp::try_parse(remaining)?; |
5420 | let (requestor: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
5421 | let (selection: u32, remaining: &[u8]) = Atom::try_parse(remaining)?; |
5422 | let (target: u32, remaining: &[u8]) = Atom::try_parse(remaining)?; |
5423 | let (property: u32, remaining: &[u8]) = Atom::try_parse(remaining)?; |
5424 | let result: SelectionNotifyEvent = SelectionNotifyEvent { response_type, sequence, time, requestor, selection, target, property }; |
5425 | let _ = remaining; |
5426 | let remaining: &[u8] = initial_value.get(32..) |
5427 | .ok_or(err:ParseError::InsufficientData)?; |
5428 | Ok((result, remaining)) |
5429 | } |
5430 | } |
5431 | impl Serialize for SelectionNotifyEvent { |
5432 | type Bytes = [u8; 24]; |
5433 | fn serialize(&self) -> [u8; 24] { |
5434 | let response_type_bytes = self.response_type.serialize(); |
5435 | let sequence_bytes = self.sequence.serialize(); |
5436 | let time_bytes = self.time.serialize(); |
5437 | let requestor_bytes = self.requestor.serialize(); |
5438 | let selection_bytes = self.selection.serialize(); |
5439 | let target_bytes = self.target.serialize(); |
5440 | let property_bytes = self.property.serialize(); |
5441 | [ |
5442 | response_type_bytes[0], |
5443 | 0, |
5444 | sequence_bytes[0], |
5445 | sequence_bytes[1], |
5446 | time_bytes[0], |
5447 | time_bytes[1], |
5448 | time_bytes[2], |
5449 | time_bytes[3], |
5450 | requestor_bytes[0], |
5451 | requestor_bytes[1], |
5452 | requestor_bytes[2], |
5453 | requestor_bytes[3], |
5454 | selection_bytes[0], |
5455 | selection_bytes[1], |
5456 | selection_bytes[2], |
5457 | selection_bytes[3], |
5458 | target_bytes[0], |
5459 | target_bytes[1], |
5460 | target_bytes[2], |
5461 | target_bytes[3], |
5462 | property_bytes[0], |
5463 | property_bytes[1], |
5464 | property_bytes[2], |
5465 | property_bytes[3], |
5466 | ] |
5467 | } |
5468 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
5469 | bytes.reserve(24); |
5470 | self.response_type.serialize_into(bytes); |
5471 | bytes.extend_from_slice(&[0; 1]); |
5472 | self.sequence.serialize_into(bytes); |
5473 | self.time.serialize_into(bytes); |
5474 | self.requestor.serialize_into(bytes); |
5475 | self.selection.serialize_into(bytes); |
5476 | self.target.serialize_into(bytes); |
5477 | self.property.serialize_into(bytes); |
5478 | } |
5479 | } |
5480 | impl From<&SelectionNotifyEvent> for [u8; 32] { |
5481 | fn from(input: &SelectionNotifyEvent) -> Self { |
5482 | let response_type_bytes = input.response_type.serialize(); |
5483 | let sequence_bytes = input.sequence.serialize(); |
5484 | let time_bytes = input.time.serialize(); |
5485 | let requestor_bytes = input.requestor.serialize(); |
5486 | let selection_bytes = input.selection.serialize(); |
5487 | let target_bytes = input.target.serialize(); |
5488 | let property_bytes = input.property.serialize(); |
5489 | [ |
5490 | response_type_bytes[0], |
5491 | 0, |
5492 | sequence_bytes[0], |
5493 | sequence_bytes[1], |
5494 | time_bytes[0], |
5495 | time_bytes[1], |
5496 | time_bytes[2], |
5497 | time_bytes[3], |
5498 | requestor_bytes[0], |
5499 | requestor_bytes[1], |
5500 | requestor_bytes[2], |
5501 | requestor_bytes[3], |
5502 | selection_bytes[0], |
5503 | selection_bytes[1], |
5504 | selection_bytes[2], |
5505 | selection_bytes[3], |
5506 | target_bytes[0], |
5507 | target_bytes[1], |
5508 | target_bytes[2], |
5509 | target_bytes[3], |
5510 | property_bytes[0], |
5511 | property_bytes[1], |
5512 | property_bytes[2], |
5513 | property_bytes[3], |
5514 | // trailing padding |
5515 | 0, |
5516 | 0, |
5517 | 0, |
5518 | 0, |
5519 | 0, |
5520 | 0, |
5521 | 0, |
5522 | 0, |
5523 | ] |
5524 | } |
5525 | } |
5526 | impl From<SelectionNotifyEvent> for [u8; 32] { |
5527 | fn from(input: SelectionNotifyEvent) -> Self { |
5528 | Self::from(&input) |
5529 | } |
5530 | } |
5531 | |
5532 | /// # Fields |
5533 | /// |
5534 | /// * `Uninstalled` - The colormap was uninstalled. |
5535 | /// * `Installed` - The colormap was installed. |
5536 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
5537 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
5538 | pub struct ColormapState(u8); |
5539 | impl ColormapState { |
5540 | pub const UNINSTALLED: Self = Self(0); |
5541 | pub const INSTALLED: Self = Self(1); |
5542 | } |
5543 | impl From<ColormapState> for u8 { |
5544 | #[inline ] |
5545 | fn from(input: ColormapState) -> Self { |
5546 | input.0 |
5547 | } |
5548 | } |
5549 | impl From<ColormapState> for Option<u8> { |
5550 | #[inline ] |
5551 | fn from(input: ColormapState) -> Self { |
5552 | Some(input.0) |
5553 | } |
5554 | } |
5555 | impl From<ColormapState> for u16 { |
5556 | #[inline ] |
5557 | fn from(input: ColormapState) -> Self { |
5558 | u16::from(input.0) |
5559 | } |
5560 | } |
5561 | impl From<ColormapState> for Option<u16> { |
5562 | #[inline ] |
5563 | fn from(input: ColormapState) -> Self { |
5564 | Some(u16::from(input.0)) |
5565 | } |
5566 | } |
5567 | impl From<ColormapState> for u32 { |
5568 | #[inline ] |
5569 | fn from(input: ColormapState) -> Self { |
5570 | u32::from(input.0) |
5571 | } |
5572 | } |
5573 | impl From<ColormapState> for Option<u32> { |
5574 | #[inline ] |
5575 | fn from(input: ColormapState) -> Self { |
5576 | Some(u32::from(input.0)) |
5577 | } |
5578 | } |
5579 | impl From<u8> for ColormapState { |
5580 | #[inline ] |
5581 | fn from(value: u8) -> Self { |
5582 | Self(value) |
5583 | } |
5584 | } |
5585 | impl core::fmt::Debug for ColormapState { |
5586 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
5587 | let variants: [(u32, &str, &str); 2] = [ |
5588 | (Self::UNINSTALLED.0.into(), "UNINSTALLED" , "Uninstalled" ), |
5589 | (Self::INSTALLED.0.into(), "INSTALLED" , "Installed" ), |
5590 | ]; |
5591 | pretty_print_enum(fmt, self.0.into(), &variants) |
5592 | } |
5593 | } |
5594 | |
5595 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
5596 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
5597 | pub struct ColormapEnum(u8); |
5598 | impl ColormapEnum { |
5599 | pub const NONE: Self = Self(0); |
5600 | } |
5601 | impl From<ColormapEnum> for u8 { |
5602 | #[inline ] |
5603 | fn from(input: ColormapEnum) -> Self { |
5604 | input.0 |
5605 | } |
5606 | } |
5607 | impl From<ColormapEnum> for Option<u8> { |
5608 | #[inline ] |
5609 | fn from(input: ColormapEnum) -> Self { |
5610 | Some(input.0) |
5611 | } |
5612 | } |
5613 | impl From<ColormapEnum> for u16 { |
5614 | #[inline ] |
5615 | fn from(input: ColormapEnum) -> Self { |
5616 | u16::from(input.0) |
5617 | } |
5618 | } |
5619 | impl From<ColormapEnum> for Option<u16> { |
5620 | #[inline ] |
5621 | fn from(input: ColormapEnum) -> Self { |
5622 | Some(u16::from(input.0)) |
5623 | } |
5624 | } |
5625 | impl From<ColormapEnum> for u32 { |
5626 | #[inline ] |
5627 | fn from(input: ColormapEnum) -> Self { |
5628 | u32::from(input.0) |
5629 | } |
5630 | } |
5631 | impl From<ColormapEnum> for Option<u32> { |
5632 | #[inline ] |
5633 | fn from(input: ColormapEnum) -> Self { |
5634 | Some(u32::from(input.0)) |
5635 | } |
5636 | } |
5637 | impl From<u8> for ColormapEnum { |
5638 | #[inline ] |
5639 | fn from(value: u8) -> Self { |
5640 | Self(value) |
5641 | } |
5642 | } |
5643 | impl core::fmt::Debug for ColormapEnum { |
5644 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
5645 | let variants: [(u32, &str, &str); 1] = [ |
5646 | (Self::NONE.0.into(), "NONE" , "None" ), |
5647 | ]; |
5648 | pretty_print_enum(fmt, self.0.into(), &variants) |
5649 | } |
5650 | } |
5651 | |
5652 | /// Opcode for the ColormapNotify event |
5653 | pub const COLORMAP_NOTIFY_EVENT: u8 = 32; |
5654 | /// the colormap for some window changed. |
5655 | /// |
5656 | /// # Fields |
5657 | /// |
5658 | /// * `window` - The window whose associated colormap is changed, installed or uninstalled. |
5659 | /// * `colormap` - The colormap which is changed, installed or uninstalled. This is `XCB_NONE` |
5660 | /// when the colormap is changed by a call to `FreeColormap`. |
5661 | /// * `_new` - Indicates whether the colormap was changed (1) or installed/uninstalled (0). |
5662 | /// * `state` - |
5663 | /// |
5664 | /// # See |
5665 | /// |
5666 | /// * `FreeColormap`: request |
5667 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
5668 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
5669 | pub struct ColormapNotifyEvent { |
5670 | pub response_type: u8, |
5671 | pub sequence: u16, |
5672 | pub window: Window, |
5673 | pub colormap: Colormap, |
5674 | pub new: bool, |
5675 | pub state: ColormapState, |
5676 | } |
5677 | impl TryParse for ColormapNotifyEvent { |
5678 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
5679 | let remaining: &[u8] = initial_value; |
5680 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
5681 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
5682 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
5683 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
5684 | let (colormap: u32, remaining: &[u8]) = Colormap::try_parse(remaining)?; |
5685 | let (new: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
5686 | let (state: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
5687 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
5688 | let state: ColormapState = state.into(); |
5689 | let result: ColormapNotifyEvent = ColormapNotifyEvent { response_type, sequence, window, colormap, new, state }; |
5690 | let _ = remaining; |
5691 | let remaining: &[u8] = initial_value.get(32..) |
5692 | .ok_or(err:ParseError::InsufficientData)?; |
5693 | Ok((result, remaining)) |
5694 | } |
5695 | } |
5696 | impl Serialize for ColormapNotifyEvent { |
5697 | type Bytes = [u8; 16]; |
5698 | fn serialize(&self) -> [u8; 16] { |
5699 | let response_type_bytes = self.response_type.serialize(); |
5700 | let sequence_bytes = self.sequence.serialize(); |
5701 | let window_bytes = self.window.serialize(); |
5702 | let colormap_bytes = self.colormap.serialize(); |
5703 | let new_bytes = self.new.serialize(); |
5704 | let state_bytes = u8::from(self.state).serialize(); |
5705 | [ |
5706 | response_type_bytes[0], |
5707 | 0, |
5708 | sequence_bytes[0], |
5709 | sequence_bytes[1], |
5710 | window_bytes[0], |
5711 | window_bytes[1], |
5712 | window_bytes[2], |
5713 | window_bytes[3], |
5714 | colormap_bytes[0], |
5715 | colormap_bytes[1], |
5716 | colormap_bytes[2], |
5717 | colormap_bytes[3], |
5718 | new_bytes[0], |
5719 | state_bytes[0], |
5720 | 0, |
5721 | 0, |
5722 | ] |
5723 | } |
5724 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
5725 | bytes.reserve(16); |
5726 | self.response_type.serialize_into(bytes); |
5727 | bytes.extend_from_slice(&[0; 1]); |
5728 | self.sequence.serialize_into(bytes); |
5729 | self.window.serialize_into(bytes); |
5730 | self.colormap.serialize_into(bytes); |
5731 | self.new.serialize_into(bytes); |
5732 | u8::from(self.state).serialize_into(bytes); |
5733 | bytes.extend_from_slice(&[0; 2]); |
5734 | } |
5735 | } |
5736 | impl From<&ColormapNotifyEvent> for [u8; 32] { |
5737 | fn from(input: &ColormapNotifyEvent) -> Self { |
5738 | let response_type_bytes = input.response_type.serialize(); |
5739 | let sequence_bytes = input.sequence.serialize(); |
5740 | let window_bytes = input.window.serialize(); |
5741 | let colormap_bytes = input.colormap.serialize(); |
5742 | let new_bytes = input.new.serialize(); |
5743 | let state_bytes = u8::from(input.state).serialize(); |
5744 | [ |
5745 | response_type_bytes[0], |
5746 | 0, |
5747 | sequence_bytes[0], |
5748 | sequence_bytes[1], |
5749 | window_bytes[0], |
5750 | window_bytes[1], |
5751 | window_bytes[2], |
5752 | window_bytes[3], |
5753 | colormap_bytes[0], |
5754 | colormap_bytes[1], |
5755 | colormap_bytes[2], |
5756 | colormap_bytes[3], |
5757 | new_bytes[0], |
5758 | state_bytes[0], |
5759 | 0, |
5760 | 0, |
5761 | // trailing padding |
5762 | 0, |
5763 | 0, |
5764 | 0, |
5765 | 0, |
5766 | 0, |
5767 | 0, |
5768 | 0, |
5769 | 0, |
5770 | 0, |
5771 | 0, |
5772 | 0, |
5773 | 0, |
5774 | 0, |
5775 | 0, |
5776 | 0, |
5777 | 0, |
5778 | ] |
5779 | } |
5780 | } |
5781 | impl From<ColormapNotifyEvent> for [u8; 32] { |
5782 | fn from(input: ColormapNotifyEvent) -> Self { |
5783 | Self::from(&input) |
5784 | } |
5785 | } |
5786 | |
5787 | #[derive (Debug, Copy, Clone)] |
5788 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
5789 | pub struct ClientMessageData([u8; 20]); |
5790 | impl ClientMessageData { |
5791 | pub fn as_data8(&self) -> [u8; 20] { |
5792 | fn do_the_parse(remaining: &[u8]) -> Result<[u8; 20], ParseError> { |
5793 | let (data8, remaining) = crate::x11_utils::parse_u8_array::<20>(remaining)?; |
5794 | let _ = remaining; |
5795 | Ok(data8) |
5796 | } |
5797 | do_the_parse(&self.0).unwrap() |
5798 | } |
5799 | pub fn as_data16(&self) -> [u16; 10] { |
5800 | fn do_the_parse(remaining: &[u8]) -> Result<[u16; 10], ParseError> { |
5801 | let (data16_0, remaining) = u16::try_parse(remaining)?; |
5802 | let (data16_1, remaining) = u16::try_parse(remaining)?; |
5803 | let (data16_2, remaining) = u16::try_parse(remaining)?; |
5804 | let (data16_3, remaining) = u16::try_parse(remaining)?; |
5805 | let (data16_4, remaining) = u16::try_parse(remaining)?; |
5806 | let (data16_5, remaining) = u16::try_parse(remaining)?; |
5807 | let (data16_6, remaining) = u16::try_parse(remaining)?; |
5808 | let (data16_7, remaining) = u16::try_parse(remaining)?; |
5809 | let (data16_8, remaining) = u16::try_parse(remaining)?; |
5810 | let (data16_9, remaining) = u16::try_parse(remaining)?; |
5811 | let data16 = [ |
5812 | data16_0, |
5813 | data16_1, |
5814 | data16_2, |
5815 | data16_3, |
5816 | data16_4, |
5817 | data16_5, |
5818 | data16_6, |
5819 | data16_7, |
5820 | data16_8, |
5821 | data16_9, |
5822 | ]; |
5823 | let _ = remaining; |
5824 | Ok(data16) |
5825 | } |
5826 | do_the_parse(&self.0).unwrap() |
5827 | } |
5828 | pub fn as_data32(&self) -> [u32; 5] { |
5829 | fn do_the_parse(remaining: &[u8]) -> Result<[u32; 5], ParseError> { |
5830 | let (data32_0, remaining) = u32::try_parse(remaining)?; |
5831 | let (data32_1, remaining) = u32::try_parse(remaining)?; |
5832 | let (data32_2, remaining) = u32::try_parse(remaining)?; |
5833 | let (data32_3, remaining) = u32::try_parse(remaining)?; |
5834 | let (data32_4, remaining) = u32::try_parse(remaining)?; |
5835 | let data32 = [ |
5836 | data32_0, |
5837 | data32_1, |
5838 | data32_2, |
5839 | data32_3, |
5840 | data32_4, |
5841 | ]; |
5842 | let _ = remaining; |
5843 | Ok(data32) |
5844 | } |
5845 | do_the_parse(&self.0).unwrap() |
5846 | } |
5847 | } |
5848 | impl Serialize for ClientMessageData { |
5849 | type Bytes = [u8; 20]; |
5850 | fn serialize(&self) -> [u8; 20] { |
5851 | self.0 |
5852 | } |
5853 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
5854 | bytes.extend_from_slice(&self.0); |
5855 | } |
5856 | } |
5857 | impl TryParse for ClientMessageData { |
5858 | fn try_parse(value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
5859 | let inner: [u8; 20] = valueResult<[u8; 20], TryFromSliceError>.get(..20) |
5860 | .ok_or(err:ParseError::InsufficientData)? |
5861 | .try_into() |
5862 | .unwrap(); |
5863 | let result: ClientMessageData = ClientMessageData(inner); |
5864 | Ok((result, &value[20..])) |
5865 | } |
5866 | } |
5867 | impl From<[u8; 20]> for ClientMessageData { |
5868 | fn from(data8: [u8; 20]) -> Self { |
5869 | Self(data8) |
5870 | } |
5871 | } |
5872 | impl From<[u16; 10]> for ClientMessageData { |
5873 | fn from(data16: [u16; 10]) -> Self { |
5874 | let data16_0_bytes = data16[0].serialize(); |
5875 | let data16_1_bytes = data16[1].serialize(); |
5876 | let data16_2_bytes = data16[2].serialize(); |
5877 | let data16_3_bytes = data16[3].serialize(); |
5878 | let data16_4_bytes = data16[4].serialize(); |
5879 | let data16_5_bytes = data16[5].serialize(); |
5880 | let data16_6_bytes = data16[6].serialize(); |
5881 | let data16_7_bytes = data16[7].serialize(); |
5882 | let data16_8_bytes = data16[8].serialize(); |
5883 | let data16_9_bytes = data16[9].serialize(); |
5884 | let value = [ |
5885 | data16_0_bytes[0], |
5886 | data16_0_bytes[1], |
5887 | data16_1_bytes[0], |
5888 | data16_1_bytes[1], |
5889 | data16_2_bytes[0], |
5890 | data16_2_bytes[1], |
5891 | data16_3_bytes[0], |
5892 | data16_3_bytes[1], |
5893 | data16_4_bytes[0], |
5894 | data16_4_bytes[1], |
5895 | data16_5_bytes[0], |
5896 | data16_5_bytes[1], |
5897 | data16_6_bytes[0], |
5898 | data16_6_bytes[1], |
5899 | data16_7_bytes[0], |
5900 | data16_7_bytes[1], |
5901 | data16_8_bytes[0], |
5902 | data16_8_bytes[1], |
5903 | data16_9_bytes[0], |
5904 | data16_9_bytes[1], |
5905 | ]; |
5906 | Self(value) |
5907 | } |
5908 | } |
5909 | impl From<[u32; 5]> for ClientMessageData { |
5910 | fn from(data32: [u32; 5]) -> Self { |
5911 | let data32_0_bytes = data32[0].serialize(); |
5912 | let data32_1_bytes = data32[1].serialize(); |
5913 | let data32_2_bytes = data32[2].serialize(); |
5914 | let data32_3_bytes = data32[3].serialize(); |
5915 | let data32_4_bytes = data32[4].serialize(); |
5916 | let value = [ |
5917 | data32_0_bytes[0], |
5918 | data32_0_bytes[1], |
5919 | data32_0_bytes[2], |
5920 | data32_0_bytes[3], |
5921 | data32_1_bytes[0], |
5922 | data32_1_bytes[1], |
5923 | data32_1_bytes[2], |
5924 | data32_1_bytes[3], |
5925 | data32_2_bytes[0], |
5926 | data32_2_bytes[1], |
5927 | data32_2_bytes[2], |
5928 | data32_2_bytes[3], |
5929 | data32_3_bytes[0], |
5930 | data32_3_bytes[1], |
5931 | data32_3_bytes[2], |
5932 | data32_3_bytes[3], |
5933 | data32_4_bytes[0], |
5934 | data32_4_bytes[1], |
5935 | data32_4_bytes[2], |
5936 | data32_4_bytes[3], |
5937 | ]; |
5938 | Self(value) |
5939 | } |
5940 | } |
5941 | |
5942 | /// Opcode for the ClientMessage event |
5943 | pub const CLIENT_MESSAGE_EVENT: u8 = 33; |
5944 | /// NOT YET DOCUMENTED. |
5945 | /// |
5946 | /// This event represents a ClientMessage, sent by another X11 client. An example |
5947 | /// is a client sending the `_NET_WM_STATE` ClientMessage to the root window |
5948 | /// to indicate the fullscreen window state, effectively requesting that the window |
5949 | /// manager puts it into fullscreen mode. |
5950 | /// |
5951 | /// # Fields |
5952 | /// |
5953 | /// * `format` - Specifies how to interpret `data`. Can be either 8, 16 or 32. |
5954 | /// * `type` - An atom which indicates how the data should be interpreted by the receiving |
5955 | /// client. |
5956 | /// * `data` - The data itself (20 bytes max). |
5957 | /// |
5958 | /// # See |
5959 | /// |
5960 | /// * `SendEvent`: request |
5961 | #[derive (Debug, Clone, Copy)] |
5962 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
5963 | pub struct ClientMessageEvent { |
5964 | pub response_type: u8, |
5965 | pub format: u8, |
5966 | pub sequence: u16, |
5967 | pub window: Window, |
5968 | pub type_: Atom, |
5969 | pub data: ClientMessageData, |
5970 | } |
5971 | impl TryParse for ClientMessageEvent { |
5972 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
5973 | let remaining: &[u8] = initial_value; |
5974 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
5975 | let (format: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
5976 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
5977 | let (window: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
5978 | let (type_: u32, remaining: &[u8]) = Atom::try_parse(remaining)?; |
5979 | let (data: ClientMessageData, remaining: &[u8]) = ClientMessageData::try_parse(remaining)?; |
5980 | let result: ClientMessageEvent = ClientMessageEvent { response_type, format, sequence, window, type_, data }; |
5981 | let _ = remaining; |
5982 | let remaining: &[u8] = initial_value.get(32..) |
5983 | .ok_or(err:ParseError::InsufficientData)?; |
5984 | Ok((result, remaining)) |
5985 | } |
5986 | } |
5987 | impl Serialize for ClientMessageEvent { |
5988 | type Bytes = [u8; 32]; |
5989 | fn serialize(&self) -> [u8; 32] { |
5990 | let response_type_bytes = self.response_type.serialize(); |
5991 | let format_bytes = self.format.serialize(); |
5992 | let sequence_bytes = self.sequence.serialize(); |
5993 | let window_bytes = self.window.serialize(); |
5994 | let type_bytes = self.type_.serialize(); |
5995 | let data_bytes = self.data.serialize(); |
5996 | [ |
5997 | response_type_bytes[0], |
5998 | format_bytes[0], |
5999 | sequence_bytes[0], |
6000 | sequence_bytes[1], |
6001 | window_bytes[0], |
6002 | window_bytes[1], |
6003 | window_bytes[2], |
6004 | window_bytes[3], |
6005 | type_bytes[0], |
6006 | type_bytes[1], |
6007 | type_bytes[2], |
6008 | type_bytes[3], |
6009 | data_bytes[0], |
6010 | data_bytes[1], |
6011 | data_bytes[2], |
6012 | data_bytes[3], |
6013 | data_bytes[4], |
6014 | data_bytes[5], |
6015 | data_bytes[6], |
6016 | data_bytes[7], |
6017 | data_bytes[8], |
6018 | data_bytes[9], |
6019 | data_bytes[10], |
6020 | data_bytes[11], |
6021 | data_bytes[12], |
6022 | data_bytes[13], |
6023 | data_bytes[14], |
6024 | data_bytes[15], |
6025 | data_bytes[16], |
6026 | data_bytes[17], |
6027 | data_bytes[18], |
6028 | data_bytes[19], |
6029 | ] |
6030 | } |
6031 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
6032 | bytes.reserve(32); |
6033 | self.response_type.serialize_into(bytes); |
6034 | self.format.serialize_into(bytes); |
6035 | self.sequence.serialize_into(bytes); |
6036 | self.window.serialize_into(bytes); |
6037 | self.type_.serialize_into(bytes); |
6038 | self.data.serialize_into(bytes); |
6039 | } |
6040 | } |
6041 | impl From<&ClientMessageEvent> for [u8; 32] { |
6042 | fn from(input: &ClientMessageEvent) -> Self { |
6043 | let response_type_bytes = input.response_type.serialize(); |
6044 | let format_bytes = input.format.serialize(); |
6045 | let sequence_bytes = input.sequence.serialize(); |
6046 | let window_bytes = input.window.serialize(); |
6047 | let type_bytes = input.type_.serialize(); |
6048 | let data_bytes = input.data.serialize(); |
6049 | [ |
6050 | response_type_bytes[0], |
6051 | format_bytes[0], |
6052 | sequence_bytes[0], |
6053 | sequence_bytes[1], |
6054 | window_bytes[0], |
6055 | window_bytes[1], |
6056 | window_bytes[2], |
6057 | window_bytes[3], |
6058 | type_bytes[0], |
6059 | type_bytes[1], |
6060 | type_bytes[2], |
6061 | type_bytes[3], |
6062 | data_bytes[0], |
6063 | data_bytes[1], |
6064 | data_bytes[2], |
6065 | data_bytes[3], |
6066 | data_bytes[4], |
6067 | data_bytes[5], |
6068 | data_bytes[6], |
6069 | data_bytes[7], |
6070 | data_bytes[8], |
6071 | data_bytes[9], |
6072 | data_bytes[10], |
6073 | data_bytes[11], |
6074 | data_bytes[12], |
6075 | data_bytes[13], |
6076 | data_bytes[14], |
6077 | data_bytes[15], |
6078 | data_bytes[16], |
6079 | data_bytes[17], |
6080 | data_bytes[18], |
6081 | data_bytes[19], |
6082 | ] |
6083 | } |
6084 | } |
6085 | impl From<ClientMessageEvent> for [u8; 32] { |
6086 | fn from(input: ClientMessageEvent) -> Self { |
6087 | Self::from(&input) |
6088 | } |
6089 | } |
6090 | impl ClientMessageEvent { |
6091 | /// Create a new `ClientMessageEvent`. |
6092 | /// |
6093 | /// This function simplifies the creation of a `ClientMessageEvent` by applying |
6094 | /// some useful defaults: |
6095 | /// - `response_type = CLIENT_MESSAGE_EVENT` |
6096 | /// - `sequence = 0` |
6097 | /// |
6098 | /// The other fields are set from the parameters given to this function. |
6099 | pub fn new(format: u8, window: Window, type_: impl Into<Atom>, data: impl Into<ClientMessageData>) -> Self { |
6100 | Self { |
6101 | response_type: CLIENT_MESSAGE_EVENT, |
6102 | format, |
6103 | sequence: 0, |
6104 | window, |
6105 | type_: type_.into(), |
6106 | data: data.into(), |
6107 | } |
6108 | } |
6109 | } |
6110 | |
6111 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
6112 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
6113 | pub struct Mapping(u8); |
6114 | impl Mapping { |
6115 | pub const MODIFIER: Self = Self(0); |
6116 | pub const KEYBOARD: Self = Self(1); |
6117 | pub const POINTER: Self = Self(2); |
6118 | } |
6119 | impl From<Mapping> for u8 { |
6120 | #[inline ] |
6121 | fn from(input: Mapping) -> Self { |
6122 | input.0 |
6123 | } |
6124 | } |
6125 | impl From<Mapping> for Option<u8> { |
6126 | #[inline ] |
6127 | fn from(input: Mapping) -> Self { |
6128 | Some(input.0) |
6129 | } |
6130 | } |
6131 | impl From<Mapping> for u16 { |
6132 | #[inline ] |
6133 | fn from(input: Mapping) -> Self { |
6134 | u16::from(input.0) |
6135 | } |
6136 | } |
6137 | impl From<Mapping> for Option<u16> { |
6138 | #[inline ] |
6139 | fn from(input: Mapping) -> Self { |
6140 | Some(u16::from(input.0)) |
6141 | } |
6142 | } |
6143 | impl From<Mapping> for u32 { |
6144 | #[inline ] |
6145 | fn from(input: Mapping) -> Self { |
6146 | u32::from(input.0) |
6147 | } |
6148 | } |
6149 | impl From<Mapping> for Option<u32> { |
6150 | #[inline ] |
6151 | fn from(input: Mapping) -> Self { |
6152 | Some(u32::from(input.0)) |
6153 | } |
6154 | } |
6155 | impl From<u8> for Mapping { |
6156 | #[inline ] |
6157 | fn from(value: u8) -> Self { |
6158 | Self(value) |
6159 | } |
6160 | } |
6161 | impl core::fmt::Debug for Mapping { |
6162 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
6163 | let variants: [(u32, &str, &str); 3] = [ |
6164 | (Self::MODIFIER.0.into(), "MODIFIER" , "Modifier" ), |
6165 | (Self::KEYBOARD.0.into(), "KEYBOARD" , "Keyboard" ), |
6166 | (Self::POINTER.0.into(), "POINTER" , "Pointer" ), |
6167 | ]; |
6168 | pretty_print_enum(fmt, self.0.into(), &variants) |
6169 | } |
6170 | } |
6171 | |
6172 | /// Opcode for the MappingNotify event |
6173 | pub const MAPPING_NOTIFY_EVENT: u8 = 34; |
6174 | /// keyboard mapping changed. |
6175 | /// |
6176 | /// # Fields |
6177 | /// |
6178 | /// * `request` - |
6179 | /// * `first_keycode` - The first number in the range of the altered mapping. |
6180 | /// * `count` - The number of keycodes altered. |
6181 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
6182 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
6183 | pub struct MappingNotifyEvent { |
6184 | pub response_type: u8, |
6185 | pub sequence: u16, |
6186 | pub request: Mapping, |
6187 | pub first_keycode: Keycode, |
6188 | pub count: u8, |
6189 | } |
6190 | impl TryParse for MappingNotifyEvent { |
6191 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
6192 | let remaining: &[u8] = initial_value; |
6193 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
6194 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
6195 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
6196 | let (request: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
6197 | let (first_keycode: u8, remaining: &[u8]) = Keycode::try_parse(remaining)?; |
6198 | let (count: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
6199 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
6200 | let request: Mapping = request.into(); |
6201 | let result: MappingNotifyEvent = MappingNotifyEvent { response_type, sequence, request, first_keycode, count }; |
6202 | let _ = remaining; |
6203 | let remaining: &[u8] = initial_value.get(32..) |
6204 | .ok_or(err:ParseError::InsufficientData)?; |
6205 | Ok((result, remaining)) |
6206 | } |
6207 | } |
6208 | impl Serialize for MappingNotifyEvent { |
6209 | type Bytes = [u8; 8]; |
6210 | fn serialize(&self) -> [u8; 8] { |
6211 | let response_type_bytes = self.response_type.serialize(); |
6212 | let sequence_bytes = self.sequence.serialize(); |
6213 | let request_bytes = u8::from(self.request).serialize(); |
6214 | let first_keycode_bytes = self.first_keycode.serialize(); |
6215 | let count_bytes = self.count.serialize(); |
6216 | [ |
6217 | response_type_bytes[0], |
6218 | 0, |
6219 | sequence_bytes[0], |
6220 | sequence_bytes[1], |
6221 | request_bytes[0], |
6222 | first_keycode_bytes[0], |
6223 | count_bytes[0], |
6224 | 0, |
6225 | ] |
6226 | } |
6227 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
6228 | bytes.reserve(8); |
6229 | self.response_type.serialize_into(bytes); |
6230 | bytes.extend_from_slice(&[0; 1]); |
6231 | self.sequence.serialize_into(bytes); |
6232 | u8::from(self.request).serialize_into(bytes); |
6233 | self.first_keycode.serialize_into(bytes); |
6234 | self.count.serialize_into(bytes); |
6235 | bytes.extend_from_slice(&[0; 1]); |
6236 | } |
6237 | } |
6238 | impl From<&MappingNotifyEvent> for [u8; 32] { |
6239 | fn from(input: &MappingNotifyEvent) -> Self { |
6240 | let response_type_bytes = input.response_type.serialize(); |
6241 | let sequence_bytes = input.sequence.serialize(); |
6242 | let request_bytes = u8::from(input.request).serialize(); |
6243 | let first_keycode_bytes = input.first_keycode.serialize(); |
6244 | let count_bytes = input.count.serialize(); |
6245 | [ |
6246 | response_type_bytes[0], |
6247 | 0, |
6248 | sequence_bytes[0], |
6249 | sequence_bytes[1], |
6250 | request_bytes[0], |
6251 | first_keycode_bytes[0], |
6252 | count_bytes[0], |
6253 | 0, |
6254 | // trailing padding |
6255 | 0, |
6256 | 0, |
6257 | 0, |
6258 | 0, |
6259 | 0, |
6260 | 0, |
6261 | 0, |
6262 | 0, |
6263 | 0, |
6264 | 0, |
6265 | 0, |
6266 | 0, |
6267 | 0, |
6268 | 0, |
6269 | 0, |
6270 | 0, |
6271 | 0, |
6272 | 0, |
6273 | 0, |
6274 | 0, |
6275 | 0, |
6276 | 0, |
6277 | 0, |
6278 | 0, |
6279 | ] |
6280 | } |
6281 | } |
6282 | impl From<MappingNotifyEvent> for [u8; 32] { |
6283 | fn from(input: MappingNotifyEvent) -> Self { |
6284 | Self::from(&input) |
6285 | } |
6286 | } |
6287 | |
6288 | /// Opcode for the GeGeneric event |
6289 | pub const GE_GENERIC_EVENT: u8 = 35; |
6290 | /// generic event (with length). |
6291 | /// |
6292 | /// # Fields |
6293 | /// |
6294 | /// * `extension` - The major opcode of the extension creating this event |
6295 | /// * `length` - The amount (in 4-byte units) of data beyond 32 bytes |
6296 | /// * `evtype` - The extension-specific event type |
6297 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
6298 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
6299 | pub struct GeGenericEvent { |
6300 | pub response_type: u8, |
6301 | pub extension: u8, |
6302 | pub sequence: u16, |
6303 | pub length: u32, |
6304 | pub event_type: u16, |
6305 | } |
6306 | impl TryParse for GeGenericEvent { |
6307 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
6308 | let remaining: &[u8] = initial_value; |
6309 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
6310 | let (extension: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
6311 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
6312 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
6313 | let (event_type: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
6314 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
6315 | let result: GeGenericEvent = GeGenericEvent { response_type, extension, sequence, length, event_type }; |
6316 | let _ = remaining; |
6317 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
6318 | .ok_or(err:ParseError::InsufficientData)?; |
6319 | Ok((result, remaining)) |
6320 | } |
6321 | } |
6322 | impl Serialize for GeGenericEvent { |
6323 | type Bytes = [u8; 32]; |
6324 | fn serialize(&self) -> [u8; 32] { |
6325 | let response_type_bytes = self.response_type.serialize(); |
6326 | let extension_bytes = self.extension.serialize(); |
6327 | let sequence_bytes = self.sequence.serialize(); |
6328 | let length_bytes = self.length.serialize(); |
6329 | let event_type_bytes = self.event_type.serialize(); |
6330 | [ |
6331 | response_type_bytes[0], |
6332 | extension_bytes[0], |
6333 | sequence_bytes[0], |
6334 | sequence_bytes[1], |
6335 | length_bytes[0], |
6336 | length_bytes[1], |
6337 | length_bytes[2], |
6338 | length_bytes[3], |
6339 | event_type_bytes[0], |
6340 | event_type_bytes[1], |
6341 | 0, |
6342 | 0, |
6343 | 0, |
6344 | 0, |
6345 | 0, |
6346 | 0, |
6347 | 0, |
6348 | 0, |
6349 | 0, |
6350 | 0, |
6351 | 0, |
6352 | 0, |
6353 | 0, |
6354 | 0, |
6355 | 0, |
6356 | 0, |
6357 | 0, |
6358 | 0, |
6359 | 0, |
6360 | 0, |
6361 | 0, |
6362 | 0, |
6363 | ] |
6364 | } |
6365 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
6366 | bytes.reserve(32); |
6367 | self.response_type.serialize_into(bytes); |
6368 | self.extension.serialize_into(bytes); |
6369 | self.sequence.serialize_into(bytes); |
6370 | self.length.serialize_into(bytes); |
6371 | self.event_type.serialize_into(bytes); |
6372 | bytes.extend_from_slice(&[0; 22]); |
6373 | } |
6374 | } |
6375 | |
6376 | /// Opcode for the Request error |
6377 | pub const REQUEST_ERROR: u8 = 1; |
6378 | |
6379 | /// Opcode for the Value error |
6380 | pub const VALUE_ERROR: u8 = 2; |
6381 | |
6382 | /// Opcode for the Window error |
6383 | pub const WINDOW_ERROR: u8 = 3; |
6384 | |
6385 | /// Opcode for the Pixmap error |
6386 | pub const PIXMAP_ERROR: u8 = 4; |
6387 | |
6388 | /// Opcode for the Atom error |
6389 | pub const ATOM_ERROR: u8 = 5; |
6390 | |
6391 | /// Opcode for the Cursor error |
6392 | pub const CURSOR_ERROR: u8 = 6; |
6393 | |
6394 | /// Opcode for the Font error |
6395 | pub const FONT_ERROR: u8 = 7; |
6396 | |
6397 | /// Opcode for the Match error |
6398 | pub const MATCH_ERROR: u8 = 8; |
6399 | |
6400 | /// Opcode for the Drawable error |
6401 | pub const DRAWABLE_ERROR: u8 = 9; |
6402 | |
6403 | /// Opcode for the Access error |
6404 | pub const ACCESS_ERROR: u8 = 10; |
6405 | |
6406 | /// Opcode for the Alloc error |
6407 | pub const ALLOC_ERROR: u8 = 11; |
6408 | |
6409 | /// Opcode for the Colormap error |
6410 | pub const COLORMAP_ERROR: u8 = 12; |
6411 | |
6412 | /// Opcode for the GContext error |
6413 | pub const G_CONTEXT_ERROR: u8 = 13; |
6414 | |
6415 | /// Opcode for the IDChoice error |
6416 | pub const ID_CHOICE_ERROR: u8 = 14; |
6417 | |
6418 | /// Opcode for the Name error |
6419 | pub const NAME_ERROR: u8 = 15; |
6420 | |
6421 | /// Opcode for the Length error |
6422 | pub const LENGTH_ERROR: u8 = 16; |
6423 | |
6424 | /// Opcode for the Implementation error |
6425 | pub const IMPLEMENTATION_ERROR: u8 = 17; |
6426 | |
6427 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
6428 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
6429 | pub struct WindowClass(u16); |
6430 | impl WindowClass { |
6431 | pub const COPY_FROM_PARENT: Self = Self(0); |
6432 | pub const INPUT_OUTPUT: Self = Self(1); |
6433 | pub const INPUT_ONLY: Self = Self(2); |
6434 | } |
6435 | impl From<WindowClass> for u16 { |
6436 | #[inline ] |
6437 | fn from(input: WindowClass) -> Self { |
6438 | input.0 |
6439 | } |
6440 | } |
6441 | impl From<WindowClass> for Option<u16> { |
6442 | #[inline ] |
6443 | fn from(input: WindowClass) -> Self { |
6444 | Some(input.0) |
6445 | } |
6446 | } |
6447 | impl From<WindowClass> for u32 { |
6448 | #[inline ] |
6449 | fn from(input: WindowClass) -> Self { |
6450 | u32::from(input.0) |
6451 | } |
6452 | } |
6453 | impl From<WindowClass> for Option<u32> { |
6454 | #[inline ] |
6455 | fn from(input: WindowClass) -> Self { |
6456 | Some(u32::from(input.0)) |
6457 | } |
6458 | } |
6459 | impl From<u8> for WindowClass { |
6460 | #[inline ] |
6461 | fn from(value: u8) -> Self { |
6462 | Self(value.into()) |
6463 | } |
6464 | } |
6465 | impl From<u16> for WindowClass { |
6466 | #[inline ] |
6467 | fn from(value: u16) -> Self { |
6468 | Self(value) |
6469 | } |
6470 | } |
6471 | impl core::fmt::Debug for WindowClass { |
6472 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
6473 | let variants: [(u32, &str, &str); 3] = [ |
6474 | (Self::COPY_FROM_PARENT.0.into(), "COPY_FROM_PARENT" , "CopyFromParent" ), |
6475 | (Self::INPUT_OUTPUT.0.into(), "INPUT_OUTPUT" , "InputOutput" ), |
6476 | (Self::INPUT_ONLY.0.into(), "INPUT_ONLY" , "InputOnly" ), |
6477 | ]; |
6478 | pretty_print_enum(fmt, self.0.into(), &variants) |
6479 | } |
6480 | } |
6481 | |
6482 | /// # Fields |
6483 | /// |
6484 | /// * `BackPixmap` - Overrides the default background-pixmap. The background pixmap and window must |
6485 | /// have the same root and same depth. Any size pixmap can be used, although some |
6486 | /// sizes may be faster than others. |
6487 | /// |
6488 | /// If `XCB_BACK_PIXMAP_NONE` is specified, the window has no defined background. |
6489 | /// The server may fill the contents with the previous screen contents or with |
6490 | /// contents of its own choosing. |
6491 | /// |
6492 | /// If `XCB_BACK_PIXMAP_PARENT_RELATIVE` is specified, the parent's background is |
6493 | /// used, but the window must have the same depth as the parent (or a Match error |
6494 | /// results). The parent's background is tracked, and the current version is |
6495 | /// used each time the window background is required. |
6496 | /// * `BackPixel` - Overrides `BackPixmap`. A pixmap of undefined size filled with the specified |
6497 | /// background pixel is used for the background. Range-checking is not performed, |
6498 | /// the background pixel is truncated to the appropriate number of bits. |
6499 | /// * `BorderPixmap` - Overrides the default border-pixmap. The border pixmap and window must have the |
6500 | /// same root and the same depth. Any size pixmap can be used, although some sizes |
6501 | /// may be faster than others. |
6502 | /// |
6503 | /// The special value `XCB_COPY_FROM_PARENT` means the parent's border pixmap is |
6504 | /// copied (subsequent changes to the parent's border attribute do not affect the |
6505 | /// child), but the window must have the same depth as the parent. |
6506 | /// * `BorderPixel` - Overrides `BorderPixmap`. A pixmap of undefined size filled with the specified |
6507 | /// border pixel is used for the border. Range checking is not performed on the |
6508 | /// border-pixel value, it is truncated to the appropriate number of bits. |
6509 | /// * `BitGravity` - Defines which region of the window should be retained if the window is resized. |
6510 | /// * `WinGravity` - Defines how the window should be repositioned if the parent is resized (see |
6511 | /// `ConfigureWindow`). |
6512 | /// * `BackingStore` - A backing-store of `WhenMapped` advises the server that maintaining contents of |
6513 | /// obscured regions when the window is mapped would be beneficial. A backing-store |
6514 | /// of `Always` advises the server that maintaining contents even when the window |
6515 | /// is unmapped would be beneficial. In this case, the server may generate an |
6516 | /// exposure event when the window is created. A value of `NotUseful` advises the |
6517 | /// server that maintaining contents is unnecessary, although a server may still |
6518 | /// choose to maintain contents while the window is mapped. Note that if the server |
6519 | /// maintains contents, then the server should maintain complete contents not just |
6520 | /// the region within the parent boundaries, even if the window is larger than its |
6521 | /// parent. While the server maintains contents, exposure events will not normally |
6522 | /// be generated, but the server may stop maintaining contents at any time. |
6523 | /// * `BackingPlanes` - The backing-planes indicates (with bits set to 1) which bit planes of the |
6524 | /// window hold dynamic data that must be preserved in backing-stores and during |
6525 | /// save-unders. |
6526 | /// * `BackingPixel` - The backing-pixel specifies what value to use in planes not covered by |
6527 | /// backing-planes. The server is free to save only the specified bit planes in the |
6528 | /// backing-store or save-under and regenerate the remaining planes with the |
6529 | /// specified pixel value. Any bits beyond the specified depth of the window in |
6530 | /// these values are simply ignored. |
6531 | /// * `OverrideRedirect` - The override-redirect specifies whether map and configure requests on this |
6532 | /// window should override a SubstructureRedirect on the parent, typically to |
6533 | /// inform a window manager not to tamper with the window. |
6534 | /// * `SaveUnder` - If 1, the server is advised that when this window is mapped, saving the |
6535 | /// contents of windows it obscures would be beneficial. |
6536 | /// * `EventMask` - The event-mask defines which events the client is interested in for this window |
6537 | /// (or for some event types, inferiors of the window). |
6538 | /// * `DontPropagate` - The do-not-propagate-mask defines which events should not be propagated to |
6539 | /// ancestor windows when no client has the event type selected in this window. |
6540 | /// * `Colormap` - The colormap specifies the colormap that best reflects the true colors of the window. Servers |
6541 | /// capable of supporting multiple hardware colormaps may use this information, and window man- |
6542 | /// agers may use it for InstallColormap requests. The colormap must have the same visual type |
6543 | /// and root as the window (or a Match error results). If CopyFromParent is specified, the parent's |
6544 | /// colormap is copied (subsequent changes to the parent's colormap attribute do not affect the child). |
6545 | /// However, the window must have the same visual type as the parent (or a Match error results), |
6546 | /// and the parent must not have a colormap of None (or a Match error results). For an explanation |
6547 | /// of None, see FreeColormap request. The colormap is copied by sharing the colormap object |
6548 | /// between the child and the parent, not by making a complete copy of the colormap contents. |
6549 | /// * `Cursor` - If a cursor is specified, it will be used whenever the pointer is in the window. If None is speci- |
6550 | /// fied, the parent's cursor will be used when the pointer is in the window, and any change in the |
6551 | /// parent's cursor will cause an immediate change in the displayed cursor. |
6552 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
6553 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
6554 | pub struct CW(u32); |
6555 | impl CW { |
6556 | pub const BACK_PIXMAP: Self = Self(1 << 0); |
6557 | pub const BACK_PIXEL: Self = Self(1 << 1); |
6558 | pub const BORDER_PIXMAP: Self = Self(1 << 2); |
6559 | pub const BORDER_PIXEL: Self = Self(1 << 3); |
6560 | pub const BIT_GRAVITY: Self = Self(1 << 4); |
6561 | pub const WIN_GRAVITY: Self = Self(1 << 5); |
6562 | pub const BACKING_STORE: Self = Self(1 << 6); |
6563 | pub const BACKING_PLANES: Self = Self(1 << 7); |
6564 | pub const BACKING_PIXEL: Self = Self(1 << 8); |
6565 | pub const OVERRIDE_REDIRECT: Self = Self(1 << 9); |
6566 | pub const SAVE_UNDER: Self = Self(1 << 10); |
6567 | pub const EVENT_MASK: Self = Self(1 << 11); |
6568 | pub const DONT_PROPAGATE: Self = Self(1 << 12); |
6569 | pub const COLORMAP: Self = Self(1 << 13); |
6570 | pub const CURSOR: Self = Self(1 << 14); |
6571 | } |
6572 | impl From<CW> for u32 { |
6573 | #[inline ] |
6574 | fn from(input: CW) -> Self { |
6575 | input.0 |
6576 | } |
6577 | } |
6578 | impl From<CW> for Option<u32> { |
6579 | #[inline ] |
6580 | fn from(input: CW) -> Self { |
6581 | Some(input.0) |
6582 | } |
6583 | } |
6584 | impl From<u8> for CW { |
6585 | #[inline ] |
6586 | fn from(value: u8) -> Self { |
6587 | Self(value.into()) |
6588 | } |
6589 | } |
6590 | impl From<u16> for CW { |
6591 | #[inline ] |
6592 | fn from(value: u16) -> Self { |
6593 | Self(value.into()) |
6594 | } |
6595 | } |
6596 | impl From<u32> for CW { |
6597 | #[inline ] |
6598 | fn from(value: u32) -> Self { |
6599 | Self(value) |
6600 | } |
6601 | } |
6602 | impl core::fmt::Debug for CW { |
6603 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
6604 | let variants: [(u32, &str, &str); 15] = [ |
6605 | (Self::BACK_PIXMAP.0, "BACK_PIXMAP" , "BackPixmap" ), |
6606 | (Self::BACK_PIXEL.0, "BACK_PIXEL" , "BackPixel" ), |
6607 | (Self::BORDER_PIXMAP.0, "BORDER_PIXMAP" , "BorderPixmap" ), |
6608 | (Self::BORDER_PIXEL.0, "BORDER_PIXEL" , "BorderPixel" ), |
6609 | (Self::BIT_GRAVITY.0, "BIT_GRAVITY" , "BitGravity" ), |
6610 | (Self::WIN_GRAVITY.0, "WIN_GRAVITY" , "WinGravity" ), |
6611 | (Self::BACKING_STORE.0, "BACKING_STORE" , "BackingStore" ), |
6612 | (Self::BACKING_PLANES.0, "BACKING_PLANES" , "BackingPlanes" ), |
6613 | (Self::BACKING_PIXEL.0, "BACKING_PIXEL" , "BackingPixel" ), |
6614 | (Self::OVERRIDE_REDIRECT.0, "OVERRIDE_REDIRECT" , "OverrideRedirect" ), |
6615 | (Self::SAVE_UNDER.0, "SAVE_UNDER" , "SaveUnder" ), |
6616 | (Self::EVENT_MASK.0, "EVENT_MASK" , "EventMask" ), |
6617 | (Self::DONT_PROPAGATE.0, "DONT_PROPAGATE" , "DontPropagate" ), |
6618 | (Self::COLORMAP.0, "COLORMAP" , "Colormap" ), |
6619 | (Self::CURSOR.0, "CURSOR" , "Cursor" ), |
6620 | ]; |
6621 | pretty_print_bitmask(fmt, self.0, &variants) |
6622 | } |
6623 | } |
6624 | bitmask_binop!(CW, u32); |
6625 | |
6626 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
6627 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
6628 | pub struct BackPixmap(bool); |
6629 | impl BackPixmap { |
6630 | pub const NONE: Self = Self(false); |
6631 | pub const PARENT_RELATIVE: Self = Self(true); |
6632 | } |
6633 | impl From<BackPixmap> for bool { |
6634 | #[inline ] |
6635 | fn from(input: BackPixmap) -> Self { |
6636 | input.0 |
6637 | } |
6638 | } |
6639 | impl From<BackPixmap> for Option<bool> { |
6640 | #[inline ] |
6641 | fn from(input: BackPixmap) -> Self { |
6642 | Some(input.0) |
6643 | } |
6644 | } |
6645 | impl From<BackPixmap> for u8 { |
6646 | #[inline ] |
6647 | fn from(input: BackPixmap) -> Self { |
6648 | u8::from(input.0) |
6649 | } |
6650 | } |
6651 | impl From<BackPixmap> for Option<u8> { |
6652 | #[inline ] |
6653 | fn from(input: BackPixmap) -> Self { |
6654 | Some(u8::from(input.0)) |
6655 | } |
6656 | } |
6657 | impl From<BackPixmap> for u16 { |
6658 | #[inline ] |
6659 | fn from(input: BackPixmap) -> Self { |
6660 | u16::from(input.0) |
6661 | } |
6662 | } |
6663 | impl From<BackPixmap> for Option<u16> { |
6664 | #[inline ] |
6665 | fn from(input: BackPixmap) -> Self { |
6666 | Some(u16::from(input.0)) |
6667 | } |
6668 | } |
6669 | impl From<BackPixmap> for u32 { |
6670 | #[inline ] |
6671 | fn from(input: BackPixmap) -> Self { |
6672 | u32::from(input.0) |
6673 | } |
6674 | } |
6675 | impl From<BackPixmap> for Option<u32> { |
6676 | #[inline ] |
6677 | fn from(input: BackPixmap) -> Self { |
6678 | Some(u32::from(input.0)) |
6679 | } |
6680 | } |
6681 | impl From<bool> for BackPixmap { |
6682 | #[inline ] |
6683 | fn from(value: bool) -> Self { |
6684 | Self(value) |
6685 | } |
6686 | } |
6687 | impl core::fmt::Debug for BackPixmap { |
6688 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
6689 | let variants: [(u32, &str, &str); 2] = [ |
6690 | (Self::NONE.0.into(), "NONE" , "None" ), |
6691 | (Self::PARENT_RELATIVE.0.into(), "PARENT_RELATIVE" , "ParentRelative" ), |
6692 | ]; |
6693 | pretty_print_enum(fmt, self.0.into(), &variants) |
6694 | } |
6695 | } |
6696 | |
6697 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
6698 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
6699 | pub struct Gravity(u32); |
6700 | impl Gravity { |
6701 | pub const BIT_FORGET: Self = Self(0); |
6702 | pub const WIN_UNMAP: Self = Self(0); |
6703 | pub const NORTH_WEST: Self = Self(1); |
6704 | pub const NORTH: Self = Self(2); |
6705 | pub const NORTH_EAST: Self = Self(3); |
6706 | pub const WEST: Self = Self(4); |
6707 | pub const CENTER: Self = Self(5); |
6708 | pub const EAST: Self = Self(6); |
6709 | pub const SOUTH_WEST: Self = Self(7); |
6710 | pub const SOUTH: Self = Self(8); |
6711 | pub const SOUTH_EAST: Self = Self(9); |
6712 | pub const STATIC: Self = Self(10); |
6713 | } |
6714 | impl From<Gravity> for u32 { |
6715 | #[inline ] |
6716 | fn from(input: Gravity) -> Self { |
6717 | input.0 |
6718 | } |
6719 | } |
6720 | impl From<Gravity> for Option<u32> { |
6721 | #[inline ] |
6722 | fn from(input: Gravity) -> Self { |
6723 | Some(input.0) |
6724 | } |
6725 | } |
6726 | impl From<u8> for Gravity { |
6727 | #[inline ] |
6728 | fn from(value: u8) -> Self { |
6729 | Self(value.into()) |
6730 | } |
6731 | } |
6732 | impl From<u16> for Gravity { |
6733 | #[inline ] |
6734 | fn from(value: u16) -> Self { |
6735 | Self(value.into()) |
6736 | } |
6737 | } |
6738 | impl From<u32> for Gravity { |
6739 | #[inline ] |
6740 | fn from(value: u32) -> Self { |
6741 | Self(value) |
6742 | } |
6743 | } |
6744 | impl core::fmt::Debug for Gravity { |
6745 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
6746 | let variants: [(u32, &str, &str); 12] = [ |
6747 | (Self::BIT_FORGET.0, "BIT_FORGET" , "BitForget" ), |
6748 | (Self::WIN_UNMAP.0, "WIN_UNMAP" , "WinUnmap" ), |
6749 | (Self::NORTH_WEST.0, "NORTH_WEST" , "NorthWest" ), |
6750 | (Self::NORTH.0, "NORTH" , "North" ), |
6751 | (Self::NORTH_EAST.0, "NORTH_EAST" , "NorthEast" ), |
6752 | (Self::WEST.0, "WEST" , "West" ), |
6753 | (Self::CENTER.0, "CENTER" , "Center" ), |
6754 | (Self::EAST.0, "EAST" , "East" ), |
6755 | (Self::SOUTH_WEST.0, "SOUTH_WEST" , "SouthWest" ), |
6756 | (Self::SOUTH.0, "SOUTH" , "South" ), |
6757 | (Self::SOUTH_EAST.0, "SOUTH_EAST" , "SouthEast" ), |
6758 | (Self::STATIC.0, "STATIC" , "Static" ), |
6759 | ]; |
6760 | pretty_print_enum(fmt, self.0, &variants) |
6761 | } |
6762 | } |
6763 | |
6764 | /// Auxiliary and optional information for the `create_window` function |
6765 | #[derive (Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] |
6766 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
6767 | pub struct CreateWindowAux { |
6768 | pub background_pixmap: Option<Pixmap>, |
6769 | pub background_pixel: Option<u32>, |
6770 | pub border_pixmap: Option<Pixmap>, |
6771 | pub border_pixel: Option<u32>, |
6772 | pub bit_gravity: Option<Gravity>, |
6773 | pub win_gravity: Option<Gravity>, |
6774 | pub backing_store: Option<BackingStore>, |
6775 | pub backing_planes: Option<u32>, |
6776 | pub backing_pixel: Option<u32>, |
6777 | pub override_redirect: Option<Bool32>, |
6778 | pub save_under: Option<Bool32>, |
6779 | pub event_mask: Option<EventMask>, |
6780 | pub do_not_propogate_mask: Option<EventMask>, |
6781 | pub colormap: Option<Colormap>, |
6782 | pub cursor: Option<Cursor>, |
6783 | } |
6784 | impl CreateWindowAux { |
6785 | fn try_parse(value: &[u8], value_mask: u32) -> Result<(Self, &[u8]), ParseError> { |
6786 | let switch_expr = u32::from(value_mask); |
6787 | let mut outer_remaining = value; |
6788 | let background_pixmap = if switch_expr & u32::from(CW::BACK_PIXMAP) != 0 { |
6789 | let remaining = outer_remaining; |
6790 | let (background_pixmap, remaining) = Pixmap::try_parse(remaining)?; |
6791 | outer_remaining = remaining; |
6792 | Some(background_pixmap) |
6793 | } else { |
6794 | None |
6795 | }; |
6796 | let background_pixel = if switch_expr & u32::from(CW::BACK_PIXEL) != 0 { |
6797 | let remaining = outer_remaining; |
6798 | let (background_pixel, remaining) = u32::try_parse(remaining)?; |
6799 | outer_remaining = remaining; |
6800 | Some(background_pixel) |
6801 | } else { |
6802 | None |
6803 | }; |
6804 | let border_pixmap = if switch_expr & u32::from(CW::BORDER_PIXMAP) != 0 { |
6805 | let remaining = outer_remaining; |
6806 | let (border_pixmap, remaining) = Pixmap::try_parse(remaining)?; |
6807 | outer_remaining = remaining; |
6808 | Some(border_pixmap) |
6809 | } else { |
6810 | None |
6811 | }; |
6812 | let border_pixel = if switch_expr & u32::from(CW::BORDER_PIXEL) != 0 { |
6813 | let remaining = outer_remaining; |
6814 | let (border_pixel, remaining) = u32::try_parse(remaining)?; |
6815 | outer_remaining = remaining; |
6816 | Some(border_pixel) |
6817 | } else { |
6818 | None |
6819 | }; |
6820 | let bit_gravity = if switch_expr & u32::from(CW::BIT_GRAVITY) != 0 { |
6821 | let remaining = outer_remaining; |
6822 | let (bit_gravity, remaining) = u32::try_parse(remaining)?; |
6823 | let bit_gravity = bit_gravity.into(); |
6824 | outer_remaining = remaining; |
6825 | Some(bit_gravity) |
6826 | } else { |
6827 | None |
6828 | }; |
6829 | let win_gravity = if switch_expr & u32::from(CW::WIN_GRAVITY) != 0 { |
6830 | let remaining = outer_remaining; |
6831 | let (win_gravity, remaining) = u32::try_parse(remaining)?; |
6832 | let win_gravity = win_gravity.into(); |
6833 | outer_remaining = remaining; |
6834 | Some(win_gravity) |
6835 | } else { |
6836 | None |
6837 | }; |
6838 | let backing_store = if switch_expr & u32::from(CW::BACKING_STORE) != 0 { |
6839 | let remaining = outer_remaining; |
6840 | let (backing_store, remaining) = u32::try_parse(remaining)?; |
6841 | let backing_store = backing_store.into(); |
6842 | outer_remaining = remaining; |
6843 | Some(backing_store) |
6844 | } else { |
6845 | None |
6846 | }; |
6847 | let backing_planes = if switch_expr & u32::from(CW::BACKING_PLANES) != 0 { |
6848 | let remaining = outer_remaining; |
6849 | let (backing_planes, remaining) = u32::try_parse(remaining)?; |
6850 | outer_remaining = remaining; |
6851 | Some(backing_planes) |
6852 | } else { |
6853 | None |
6854 | }; |
6855 | let backing_pixel = if switch_expr & u32::from(CW::BACKING_PIXEL) != 0 { |
6856 | let remaining = outer_remaining; |
6857 | let (backing_pixel, remaining) = u32::try_parse(remaining)?; |
6858 | outer_remaining = remaining; |
6859 | Some(backing_pixel) |
6860 | } else { |
6861 | None |
6862 | }; |
6863 | let override_redirect = if switch_expr & u32::from(CW::OVERRIDE_REDIRECT) != 0 { |
6864 | let remaining = outer_remaining; |
6865 | let (override_redirect, remaining) = Bool32::try_parse(remaining)?; |
6866 | outer_remaining = remaining; |
6867 | Some(override_redirect) |
6868 | } else { |
6869 | None |
6870 | }; |
6871 | let save_under = if switch_expr & u32::from(CW::SAVE_UNDER) != 0 { |
6872 | let remaining = outer_remaining; |
6873 | let (save_under, remaining) = Bool32::try_parse(remaining)?; |
6874 | outer_remaining = remaining; |
6875 | Some(save_under) |
6876 | } else { |
6877 | None |
6878 | }; |
6879 | let event_mask = if switch_expr & u32::from(CW::EVENT_MASK) != 0 { |
6880 | let remaining = outer_remaining; |
6881 | let (event_mask, remaining) = u32::try_parse(remaining)?; |
6882 | let event_mask = event_mask.into(); |
6883 | outer_remaining = remaining; |
6884 | Some(event_mask) |
6885 | } else { |
6886 | None |
6887 | }; |
6888 | let do_not_propogate_mask = if switch_expr & u32::from(CW::DONT_PROPAGATE) != 0 { |
6889 | let remaining = outer_remaining; |
6890 | let (do_not_propogate_mask, remaining) = u32::try_parse(remaining)?; |
6891 | let do_not_propogate_mask = do_not_propogate_mask.into(); |
6892 | outer_remaining = remaining; |
6893 | Some(do_not_propogate_mask) |
6894 | } else { |
6895 | None |
6896 | }; |
6897 | let colormap = if switch_expr & u32::from(CW::COLORMAP) != 0 { |
6898 | let remaining = outer_remaining; |
6899 | let (colormap, remaining) = Colormap::try_parse(remaining)?; |
6900 | outer_remaining = remaining; |
6901 | Some(colormap) |
6902 | } else { |
6903 | None |
6904 | }; |
6905 | let cursor = if switch_expr & u32::from(CW::CURSOR) != 0 { |
6906 | let remaining = outer_remaining; |
6907 | let (cursor, remaining) = Cursor::try_parse(remaining)?; |
6908 | outer_remaining = remaining; |
6909 | Some(cursor) |
6910 | } else { |
6911 | None |
6912 | }; |
6913 | let result = CreateWindowAux { background_pixmap, background_pixel, border_pixmap, border_pixel, bit_gravity, win_gravity, backing_store, backing_planes, backing_pixel, override_redirect, save_under, event_mask, do_not_propogate_mask, colormap, cursor }; |
6914 | Ok((result, outer_remaining)) |
6915 | } |
6916 | } |
6917 | impl CreateWindowAux { |
6918 | #[allow (dead_code)] |
6919 | fn serialize(&self, value_mask: u32) -> Vec<u8> { |
6920 | let mut result = Vec::new(); |
6921 | self.serialize_into(&mut result, u32::from(value_mask)); |
6922 | result |
6923 | } |
6924 | fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) { |
6925 | assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant" ); |
6926 | if let Some(background_pixmap) = self.background_pixmap { |
6927 | background_pixmap.serialize_into(bytes); |
6928 | } |
6929 | if let Some(background_pixel) = self.background_pixel { |
6930 | background_pixel.serialize_into(bytes); |
6931 | } |
6932 | if let Some(border_pixmap) = self.border_pixmap { |
6933 | border_pixmap.serialize_into(bytes); |
6934 | } |
6935 | if let Some(border_pixel) = self.border_pixel { |
6936 | border_pixel.serialize_into(bytes); |
6937 | } |
6938 | if let Some(bit_gravity) = self.bit_gravity { |
6939 | u32::from(bit_gravity).serialize_into(bytes); |
6940 | } |
6941 | if let Some(win_gravity) = self.win_gravity { |
6942 | u32::from(win_gravity).serialize_into(bytes); |
6943 | } |
6944 | if let Some(backing_store) = self.backing_store { |
6945 | u32::from(backing_store).serialize_into(bytes); |
6946 | } |
6947 | if let Some(backing_planes) = self.backing_planes { |
6948 | backing_planes.serialize_into(bytes); |
6949 | } |
6950 | if let Some(backing_pixel) = self.backing_pixel { |
6951 | backing_pixel.serialize_into(bytes); |
6952 | } |
6953 | if let Some(override_redirect) = self.override_redirect { |
6954 | override_redirect.serialize_into(bytes); |
6955 | } |
6956 | if let Some(save_under) = self.save_under { |
6957 | save_under.serialize_into(bytes); |
6958 | } |
6959 | if let Some(event_mask) = self.event_mask { |
6960 | u32::from(event_mask).serialize_into(bytes); |
6961 | } |
6962 | if let Some(do_not_propogate_mask) = self.do_not_propogate_mask { |
6963 | u32::from(do_not_propogate_mask).serialize_into(bytes); |
6964 | } |
6965 | if let Some(colormap) = self.colormap { |
6966 | colormap.serialize_into(bytes); |
6967 | } |
6968 | if let Some(cursor) = self.cursor { |
6969 | cursor.serialize_into(bytes); |
6970 | } |
6971 | } |
6972 | } |
6973 | impl CreateWindowAux { |
6974 | fn switch_expr(&self) -> u32 { |
6975 | let mut expr_value = 0; |
6976 | if self.background_pixmap.is_some() { |
6977 | expr_value |= u32::from(CW::BACK_PIXMAP); |
6978 | } |
6979 | if self.background_pixel.is_some() { |
6980 | expr_value |= u32::from(CW::BACK_PIXEL); |
6981 | } |
6982 | if self.border_pixmap.is_some() { |
6983 | expr_value |= u32::from(CW::BORDER_PIXMAP); |
6984 | } |
6985 | if self.border_pixel.is_some() { |
6986 | expr_value |= u32::from(CW::BORDER_PIXEL); |
6987 | } |
6988 | if self.bit_gravity.is_some() { |
6989 | expr_value |= u32::from(CW::BIT_GRAVITY); |
6990 | } |
6991 | if self.win_gravity.is_some() { |
6992 | expr_value |= u32::from(CW::WIN_GRAVITY); |
6993 | } |
6994 | if self.backing_store.is_some() { |
6995 | expr_value |= u32::from(CW::BACKING_STORE); |
6996 | } |
6997 | if self.backing_planes.is_some() { |
6998 | expr_value |= u32::from(CW::BACKING_PLANES); |
6999 | } |
7000 | if self.backing_pixel.is_some() { |
7001 | expr_value |= u32::from(CW::BACKING_PIXEL); |
7002 | } |
7003 | if self.override_redirect.is_some() { |
7004 | expr_value |= u32::from(CW::OVERRIDE_REDIRECT); |
7005 | } |
7006 | if self.save_under.is_some() { |
7007 | expr_value |= u32::from(CW::SAVE_UNDER); |
7008 | } |
7009 | if self.event_mask.is_some() { |
7010 | expr_value |= u32::from(CW::EVENT_MASK); |
7011 | } |
7012 | if self.do_not_propogate_mask.is_some() { |
7013 | expr_value |= u32::from(CW::DONT_PROPAGATE); |
7014 | } |
7015 | if self.colormap.is_some() { |
7016 | expr_value |= u32::from(CW::COLORMAP); |
7017 | } |
7018 | if self.cursor.is_some() { |
7019 | expr_value |= u32::from(CW::CURSOR); |
7020 | } |
7021 | expr_value |
7022 | } |
7023 | } |
7024 | impl CreateWindowAux { |
7025 | /// Create a new instance with all fields unset / not present. |
7026 | pub fn new() -> Self { |
7027 | Default::default() |
7028 | } |
7029 | /// Set the `background_pixmap` field of this structure. |
7030 | #[must_use ] |
7031 | pub fn background_pixmap<I>(mut self, value: I) -> Self where I: Into<Option<Pixmap>> { |
7032 | self.background_pixmap = value.into(); |
7033 | self |
7034 | } |
7035 | /// Set the `background_pixel` field of this structure. |
7036 | #[must_use ] |
7037 | pub fn background_pixel<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
7038 | self.background_pixel = value.into(); |
7039 | self |
7040 | } |
7041 | /// Set the `border_pixmap` field of this structure. |
7042 | #[must_use ] |
7043 | pub fn border_pixmap<I>(mut self, value: I) -> Self where I: Into<Option<Pixmap>> { |
7044 | self.border_pixmap = value.into(); |
7045 | self |
7046 | } |
7047 | /// Set the `border_pixel` field of this structure. |
7048 | #[must_use ] |
7049 | pub fn border_pixel<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
7050 | self.border_pixel = value.into(); |
7051 | self |
7052 | } |
7053 | /// Set the `bit_gravity` field of this structure. |
7054 | #[must_use ] |
7055 | pub fn bit_gravity<I>(mut self, value: I) -> Self where I: Into<Option<Gravity>> { |
7056 | self.bit_gravity = value.into(); |
7057 | self |
7058 | } |
7059 | /// Set the `win_gravity` field of this structure. |
7060 | #[must_use ] |
7061 | pub fn win_gravity<I>(mut self, value: I) -> Self where I: Into<Option<Gravity>> { |
7062 | self.win_gravity = value.into(); |
7063 | self |
7064 | } |
7065 | /// Set the `backing_store` field of this structure. |
7066 | #[must_use ] |
7067 | pub fn backing_store<I>(mut self, value: I) -> Self where I: Into<Option<BackingStore>> { |
7068 | self.backing_store = value.into(); |
7069 | self |
7070 | } |
7071 | /// Set the `backing_planes` field of this structure. |
7072 | #[must_use ] |
7073 | pub fn backing_planes<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
7074 | self.backing_planes = value.into(); |
7075 | self |
7076 | } |
7077 | /// Set the `backing_pixel` field of this structure. |
7078 | #[must_use ] |
7079 | pub fn backing_pixel<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
7080 | self.backing_pixel = value.into(); |
7081 | self |
7082 | } |
7083 | /// Set the `override_redirect` field of this structure. |
7084 | #[must_use ] |
7085 | pub fn override_redirect<I>(mut self, value: I) -> Self where I: Into<Option<Bool32>> { |
7086 | self.override_redirect = value.into(); |
7087 | self |
7088 | } |
7089 | /// Set the `save_under` field of this structure. |
7090 | #[must_use ] |
7091 | pub fn save_under<I>(mut self, value: I) -> Self where I: Into<Option<Bool32>> { |
7092 | self.save_under = value.into(); |
7093 | self |
7094 | } |
7095 | /// Set the `event_mask` field of this structure. |
7096 | #[must_use ] |
7097 | pub fn event_mask<I>(mut self, value: I) -> Self where I: Into<Option<EventMask>> { |
7098 | self.event_mask = value.into(); |
7099 | self |
7100 | } |
7101 | /// Set the `do_not_propogate_mask` field of this structure. |
7102 | #[must_use ] |
7103 | pub fn do_not_propogate_mask<I>(mut self, value: I) -> Self where I: Into<Option<EventMask>> { |
7104 | self.do_not_propogate_mask = value.into(); |
7105 | self |
7106 | } |
7107 | /// Set the `colormap` field of this structure. |
7108 | #[must_use ] |
7109 | pub fn colormap<I>(mut self, value: I) -> Self where I: Into<Option<Colormap>> { |
7110 | self.colormap = value.into(); |
7111 | self |
7112 | } |
7113 | /// Set the `cursor` field of this structure. |
7114 | #[must_use ] |
7115 | pub fn cursor<I>(mut self, value: I) -> Self where I: Into<Option<Cursor>> { |
7116 | self.cursor = value.into(); |
7117 | self |
7118 | } |
7119 | } |
7120 | |
7121 | /// Opcode for the CreateWindow request |
7122 | pub const CREATE_WINDOW_REQUEST: u8 = 1; |
7123 | /// Creates a window. |
7124 | /// |
7125 | /// Creates an unmapped window as child of the specified `parent` window. A |
7126 | /// CreateNotify event will be generated. The new window is placed on top in the |
7127 | /// stacking order with respect to siblings. |
7128 | /// |
7129 | /// The coordinate system has the X axis horizontal and the Y axis vertical with |
7130 | /// the origin [0, 0] at the upper-left corner. Coordinates are integral, in terms |
7131 | /// of pixels, and coincide with pixel centers. Each window and pixmap has its own |
7132 | /// coordinate system. For a window, the origin is inside the border at the inside, |
7133 | /// upper-left corner. |
7134 | /// |
7135 | /// The created window is not yet displayed (mapped), call `xcb_map_window` to |
7136 | /// display it. |
7137 | /// |
7138 | /// The created window will initially use the same cursor as its parent. |
7139 | /// |
7140 | /// # Fields |
7141 | /// |
7142 | /// * `wid` - The ID with which you will refer to the new window, created by |
7143 | /// `xcb_generate_id`. |
7144 | /// * `depth` - Specifies the new window's depth (TODO: what unit?). |
7145 | /// |
7146 | /// The special value `XCB_COPY_FROM_PARENT` means the depth is taken from the |
7147 | /// `parent` window. |
7148 | /// * `visual` - Specifies the id for the new window's visual. |
7149 | /// |
7150 | /// The special value `XCB_COPY_FROM_PARENT` means the visual is taken from the |
7151 | /// `parent` window. |
7152 | /// * `class` - |
7153 | /// * `parent` - The parent window of the new window. |
7154 | /// * `border_width` - TODO: |
7155 | /// |
7156 | /// Must be zero if the `class` is `InputOnly` or a `xcb_match_error_t` occurs. |
7157 | /// * `x` - The X coordinate of the new window. |
7158 | /// * `y` - The Y coordinate of the new window. |
7159 | /// * `width` - The width of the new window. |
7160 | /// * `height` - The height of the new window. |
7161 | /// |
7162 | /// # Errors |
7163 | /// |
7164 | /// * `Colormap` - TODO: reasons? |
7165 | /// * `Match` - TODO: reasons? |
7166 | /// * `Cursor` - TODO: reasons? |
7167 | /// * `Pixmap` - TODO: reasons? |
7168 | /// * `Value` - TODO: reasons? |
7169 | /// * `Window` - TODO: reasons? |
7170 | /// * `Alloc` - The X server could not allocate the requested resources (no memory?). |
7171 | /// |
7172 | /// # See |
7173 | /// |
7174 | /// * `xcb_generate_id`: function |
7175 | /// * `MapWindow`: request |
7176 | /// * `CreateNotify`: event |
7177 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
7178 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
7179 | pub struct CreateWindowRequest<'input> { |
7180 | pub depth: u8, |
7181 | pub wid: Window, |
7182 | pub parent: Window, |
7183 | pub x: i16, |
7184 | pub y: i16, |
7185 | pub width: u16, |
7186 | pub height: u16, |
7187 | pub border_width: u16, |
7188 | pub class: WindowClass, |
7189 | pub visual: Visualid, |
7190 | pub value_list: Cow<'input, CreateWindowAux>, |
7191 | } |
7192 | impl<'input> CreateWindowRequest<'input> { |
7193 | /// Serialize this request into bytes for the provided connection |
7194 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
7195 | let length_so_far = 0; |
7196 | let depth_bytes = self.depth.serialize(); |
7197 | let wid_bytes = self.wid.serialize(); |
7198 | let parent_bytes = self.parent.serialize(); |
7199 | let x_bytes = self.x.serialize(); |
7200 | let y_bytes = self.y.serialize(); |
7201 | let width_bytes = self.width.serialize(); |
7202 | let height_bytes = self.height.serialize(); |
7203 | let border_width_bytes = self.border_width.serialize(); |
7204 | let class_bytes = u16::from(self.class).serialize(); |
7205 | let visual_bytes = self.visual.serialize(); |
7206 | let value_mask: u32 = self.value_list.switch_expr(); |
7207 | let value_mask_bytes = value_mask.serialize(); |
7208 | let mut request0 = vec![ |
7209 | CREATE_WINDOW_REQUEST, |
7210 | depth_bytes[0], |
7211 | 0, |
7212 | 0, |
7213 | wid_bytes[0], |
7214 | wid_bytes[1], |
7215 | wid_bytes[2], |
7216 | wid_bytes[3], |
7217 | parent_bytes[0], |
7218 | parent_bytes[1], |
7219 | parent_bytes[2], |
7220 | parent_bytes[3], |
7221 | x_bytes[0], |
7222 | x_bytes[1], |
7223 | y_bytes[0], |
7224 | y_bytes[1], |
7225 | width_bytes[0], |
7226 | width_bytes[1], |
7227 | height_bytes[0], |
7228 | height_bytes[1], |
7229 | border_width_bytes[0], |
7230 | border_width_bytes[1], |
7231 | class_bytes[0], |
7232 | class_bytes[1], |
7233 | visual_bytes[0], |
7234 | visual_bytes[1], |
7235 | visual_bytes[2], |
7236 | visual_bytes[3], |
7237 | value_mask_bytes[0], |
7238 | value_mask_bytes[1], |
7239 | value_mask_bytes[2], |
7240 | value_mask_bytes[3], |
7241 | ]; |
7242 | let length_so_far = length_so_far + request0.len(); |
7243 | let value_list_bytes = self.value_list.serialize(u32::from(value_mask)); |
7244 | let length_so_far = length_so_far + value_list_bytes.len(); |
7245 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
7246 | let length_so_far = length_so_far + padding0.len(); |
7247 | assert_eq!(length_so_far % 4, 0); |
7248 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
7249 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
7250 | (vec![request0.into(), value_list_bytes.into(), padding0.into()], vec![]) |
7251 | } |
7252 | /// Parse this request given its header, its body, and any fds that go along with it |
7253 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
7254 | if header.major_opcode != CREATE_WINDOW_REQUEST { |
7255 | return Err(ParseError::InvalidValue); |
7256 | } |
7257 | let remaining = &[header.minor_opcode]; |
7258 | let (depth, remaining) = u8::try_parse(remaining)?; |
7259 | let _ = remaining; |
7260 | let (wid, remaining) = Window::try_parse(value)?; |
7261 | let (parent, remaining) = Window::try_parse(remaining)?; |
7262 | let (x, remaining) = i16::try_parse(remaining)?; |
7263 | let (y, remaining) = i16::try_parse(remaining)?; |
7264 | let (width, remaining) = u16::try_parse(remaining)?; |
7265 | let (height, remaining) = u16::try_parse(remaining)?; |
7266 | let (border_width, remaining) = u16::try_parse(remaining)?; |
7267 | let (class, remaining) = u16::try_parse(remaining)?; |
7268 | let class = class.into(); |
7269 | let (visual, remaining) = Visualid::try_parse(remaining)?; |
7270 | let (value_mask, remaining) = u32::try_parse(remaining)?; |
7271 | let (value_list, remaining) = CreateWindowAux::try_parse(remaining, u32::from(value_mask))?; |
7272 | let _ = remaining; |
7273 | Ok(CreateWindowRequest { |
7274 | depth, |
7275 | wid, |
7276 | parent, |
7277 | x, |
7278 | y, |
7279 | width, |
7280 | height, |
7281 | border_width, |
7282 | class, |
7283 | visual, |
7284 | value_list: Cow::Owned(value_list), |
7285 | }) |
7286 | } |
7287 | /// Clone all borrowed data in this CreateWindowRequest. |
7288 | pub fn into_owned(self) -> CreateWindowRequest<'static> { |
7289 | CreateWindowRequest { |
7290 | depth: self.depth, |
7291 | wid: self.wid, |
7292 | parent: self.parent, |
7293 | x: self.x, |
7294 | y: self.y, |
7295 | width: self.width, |
7296 | height: self.height, |
7297 | border_width: self.border_width, |
7298 | class: self.class, |
7299 | visual: self.visual, |
7300 | value_list: Cow::Owned(self.value_list.into_owned()), |
7301 | } |
7302 | } |
7303 | } |
7304 | impl<'input> Request for CreateWindowRequest<'input> { |
7305 | const EXTENSION_NAME: Option<&'static str> = None; |
7306 | |
7307 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
7308 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
7309 | // Flatten the buffers into a single vector |
7310 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
7311 | (buf, fds) |
7312 | } |
7313 | } |
7314 | impl<'input> crate::x11_utils::VoidRequest for CreateWindowRequest<'input> { |
7315 | } |
7316 | |
7317 | /// Auxiliary and optional information for the `change_window_attributes` function |
7318 | #[derive (Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] |
7319 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
7320 | pub struct ChangeWindowAttributesAux { |
7321 | pub background_pixmap: Option<Pixmap>, |
7322 | pub background_pixel: Option<u32>, |
7323 | pub border_pixmap: Option<Pixmap>, |
7324 | pub border_pixel: Option<u32>, |
7325 | pub bit_gravity: Option<Gravity>, |
7326 | pub win_gravity: Option<Gravity>, |
7327 | pub backing_store: Option<BackingStore>, |
7328 | pub backing_planes: Option<u32>, |
7329 | pub backing_pixel: Option<u32>, |
7330 | pub override_redirect: Option<Bool32>, |
7331 | pub save_under: Option<Bool32>, |
7332 | pub event_mask: Option<EventMask>, |
7333 | pub do_not_propogate_mask: Option<EventMask>, |
7334 | pub colormap: Option<Colormap>, |
7335 | pub cursor: Option<Cursor>, |
7336 | } |
7337 | impl ChangeWindowAttributesAux { |
7338 | fn try_parse(value: &[u8], value_mask: u32) -> Result<(Self, &[u8]), ParseError> { |
7339 | let switch_expr = u32::from(value_mask); |
7340 | let mut outer_remaining = value; |
7341 | let background_pixmap = if switch_expr & u32::from(CW::BACK_PIXMAP) != 0 { |
7342 | let remaining = outer_remaining; |
7343 | let (background_pixmap, remaining) = Pixmap::try_parse(remaining)?; |
7344 | outer_remaining = remaining; |
7345 | Some(background_pixmap) |
7346 | } else { |
7347 | None |
7348 | }; |
7349 | let background_pixel = if switch_expr & u32::from(CW::BACK_PIXEL) != 0 { |
7350 | let remaining = outer_remaining; |
7351 | let (background_pixel, remaining) = u32::try_parse(remaining)?; |
7352 | outer_remaining = remaining; |
7353 | Some(background_pixel) |
7354 | } else { |
7355 | None |
7356 | }; |
7357 | let border_pixmap = if switch_expr & u32::from(CW::BORDER_PIXMAP) != 0 { |
7358 | let remaining = outer_remaining; |
7359 | let (border_pixmap, remaining) = Pixmap::try_parse(remaining)?; |
7360 | outer_remaining = remaining; |
7361 | Some(border_pixmap) |
7362 | } else { |
7363 | None |
7364 | }; |
7365 | let border_pixel = if switch_expr & u32::from(CW::BORDER_PIXEL) != 0 { |
7366 | let remaining = outer_remaining; |
7367 | let (border_pixel, remaining) = u32::try_parse(remaining)?; |
7368 | outer_remaining = remaining; |
7369 | Some(border_pixel) |
7370 | } else { |
7371 | None |
7372 | }; |
7373 | let bit_gravity = if switch_expr & u32::from(CW::BIT_GRAVITY) != 0 { |
7374 | let remaining = outer_remaining; |
7375 | let (bit_gravity, remaining) = u32::try_parse(remaining)?; |
7376 | let bit_gravity = bit_gravity.into(); |
7377 | outer_remaining = remaining; |
7378 | Some(bit_gravity) |
7379 | } else { |
7380 | None |
7381 | }; |
7382 | let win_gravity = if switch_expr & u32::from(CW::WIN_GRAVITY) != 0 { |
7383 | let remaining = outer_remaining; |
7384 | let (win_gravity, remaining) = u32::try_parse(remaining)?; |
7385 | let win_gravity = win_gravity.into(); |
7386 | outer_remaining = remaining; |
7387 | Some(win_gravity) |
7388 | } else { |
7389 | None |
7390 | }; |
7391 | let backing_store = if switch_expr & u32::from(CW::BACKING_STORE) != 0 { |
7392 | let remaining = outer_remaining; |
7393 | let (backing_store, remaining) = u32::try_parse(remaining)?; |
7394 | let backing_store = backing_store.into(); |
7395 | outer_remaining = remaining; |
7396 | Some(backing_store) |
7397 | } else { |
7398 | None |
7399 | }; |
7400 | let backing_planes = if switch_expr & u32::from(CW::BACKING_PLANES) != 0 { |
7401 | let remaining = outer_remaining; |
7402 | let (backing_planes, remaining) = u32::try_parse(remaining)?; |
7403 | outer_remaining = remaining; |
7404 | Some(backing_planes) |
7405 | } else { |
7406 | None |
7407 | }; |
7408 | let backing_pixel = if switch_expr & u32::from(CW::BACKING_PIXEL) != 0 { |
7409 | let remaining = outer_remaining; |
7410 | let (backing_pixel, remaining) = u32::try_parse(remaining)?; |
7411 | outer_remaining = remaining; |
7412 | Some(backing_pixel) |
7413 | } else { |
7414 | None |
7415 | }; |
7416 | let override_redirect = if switch_expr & u32::from(CW::OVERRIDE_REDIRECT) != 0 { |
7417 | let remaining = outer_remaining; |
7418 | let (override_redirect, remaining) = Bool32::try_parse(remaining)?; |
7419 | outer_remaining = remaining; |
7420 | Some(override_redirect) |
7421 | } else { |
7422 | None |
7423 | }; |
7424 | let save_under = if switch_expr & u32::from(CW::SAVE_UNDER) != 0 { |
7425 | let remaining = outer_remaining; |
7426 | let (save_under, remaining) = Bool32::try_parse(remaining)?; |
7427 | outer_remaining = remaining; |
7428 | Some(save_under) |
7429 | } else { |
7430 | None |
7431 | }; |
7432 | let event_mask = if switch_expr & u32::from(CW::EVENT_MASK) != 0 { |
7433 | let remaining = outer_remaining; |
7434 | let (event_mask, remaining) = u32::try_parse(remaining)?; |
7435 | let event_mask = event_mask.into(); |
7436 | outer_remaining = remaining; |
7437 | Some(event_mask) |
7438 | } else { |
7439 | None |
7440 | }; |
7441 | let do_not_propogate_mask = if switch_expr & u32::from(CW::DONT_PROPAGATE) != 0 { |
7442 | let remaining = outer_remaining; |
7443 | let (do_not_propogate_mask, remaining) = u32::try_parse(remaining)?; |
7444 | let do_not_propogate_mask = do_not_propogate_mask.into(); |
7445 | outer_remaining = remaining; |
7446 | Some(do_not_propogate_mask) |
7447 | } else { |
7448 | None |
7449 | }; |
7450 | let colormap = if switch_expr & u32::from(CW::COLORMAP) != 0 { |
7451 | let remaining = outer_remaining; |
7452 | let (colormap, remaining) = Colormap::try_parse(remaining)?; |
7453 | outer_remaining = remaining; |
7454 | Some(colormap) |
7455 | } else { |
7456 | None |
7457 | }; |
7458 | let cursor = if switch_expr & u32::from(CW::CURSOR) != 0 { |
7459 | let remaining = outer_remaining; |
7460 | let (cursor, remaining) = Cursor::try_parse(remaining)?; |
7461 | outer_remaining = remaining; |
7462 | Some(cursor) |
7463 | } else { |
7464 | None |
7465 | }; |
7466 | let result = ChangeWindowAttributesAux { background_pixmap, background_pixel, border_pixmap, border_pixel, bit_gravity, win_gravity, backing_store, backing_planes, backing_pixel, override_redirect, save_under, event_mask, do_not_propogate_mask, colormap, cursor }; |
7467 | Ok((result, outer_remaining)) |
7468 | } |
7469 | } |
7470 | impl ChangeWindowAttributesAux { |
7471 | #[allow (dead_code)] |
7472 | fn serialize(&self, value_mask: u32) -> Vec<u8> { |
7473 | let mut result = Vec::new(); |
7474 | self.serialize_into(&mut result, u32::from(value_mask)); |
7475 | result |
7476 | } |
7477 | fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) { |
7478 | assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant" ); |
7479 | if let Some(background_pixmap) = self.background_pixmap { |
7480 | background_pixmap.serialize_into(bytes); |
7481 | } |
7482 | if let Some(background_pixel) = self.background_pixel { |
7483 | background_pixel.serialize_into(bytes); |
7484 | } |
7485 | if let Some(border_pixmap) = self.border_pixmap { |
7486 | border_pixmap.serialize_into(bytes); |
7487 | } |
7488 | if let Some(border_pixel) = self.border_pixel { |
7489 | border_pixel.serialize_into(bytes); |
7490 | } |
7491 | if let Some(bit_gravity) = self.bit_gravity { |
7492 | u32::from(bit_gravity).serialize_into(bytes); |
7493 | } |
7494 | if let Some(win_gravity) = self.win_gravity { |
7495 | u32::from(win_gravity).serialize_into(bytes); |
7496 | } |
7497 | if let Some(backing_store) = self.backing_store { |
7498 | u32::from(backing_store).serialize_into(bytes); |
7499 | } |
7500 | if let Some(backing_planes) = self.backing_planes { |
7501 | backing_planes.serialize_into(bytes); |
7502 | } |
7503 | if let Some(backing_pixel) = self.backing_pixel { |
7504 | backing_pixel.serialize_into(bytes); |
7505 | } |
7506 | if let Some(override_redirect) = self.override_redirect { |
7507 | override_redirect.serialize_into(bytes); |
7508 | } |
7509 | if let Some(save_under) = self.save_under { |
7510 | save_under.serialize_into(bytes); |
7511 | } |
7512 | if let Some(event_mask) = self.event_mask { |
7513 | u32::from(event_mask).serialize_into(bytes); |
7514 | } |
7515 | if let Some(do_not_propogate_mask) = self.do_not_propogate_mask { |
7516 | u32::from(do_not_propogate_mask).serialize_into(bytes); |
7517 | } |
7518 | if let Some(colormap) = self.colormap { |
7519 | colormap.serialize_into(bytes); |
7520 | } |
7521 | if let Some(cursor) = self.cursor { |
7522 | cursor.serialize_into(bytes); |
7523 | } |
7524 | } |
7525 | } |
7526 | impl ChangeWindowAttributesAux { |
7527 | fn switch_expr(&self) -> u32 { |
7528 | let mut expr_value = 0; |
7529 | if self.background_pixmap.is_some() { |
7530 | expr_value |= u32::from(CW::BACK_PIXMAP); |
7531 | } |
7532 | if self.background_pixel.is_some() { |
7533 | expr_value |= u32::from(CW::BACK_PIXEL); |
7534 | } |
7535 | if self.border_pixmap.is_some() { |
7536 | expr_value |= u32::from(CW::BORDER_PIXMAP); |
7537 | } |
7538 | if self.border_pixel.is_some() { |
7539 | expr_value |= u32::from(CW::BORDER_PIXEL); |
7540 | } |
7541 | if self.bit_gravity.is_some() { |
7542 | expr_value |= u32::from(CW::BIT_GRAVITY); |
7543 | } |
7544 | if self.win_gravity.is_some() { |
7545 | expr_value |= u32::from(CW::WIN_GRAVITY); |
7546 | } |
7547 | if self.backing_store.is_some() { |
7548 | expr_value |= u32::from(CW::BACKING_STORE); |
7549 | } |
7550 | if self.backing_planes.is_some() { |
7551 | expr_value |= u32::from(CW::BACKING_PLANES); |
7552 | } |
7553 | if self.backing_pixel.is_some() { |
7554 | expr_value |= u32::from(CW::BACKING_PIXEL); |
7555 | } |
7556 | if self.override_redirect.is_some() { |
7557 | expr_value |= u32::from(CW::OVERRIDE_REDIRECT); |
7558 | } |
7559 | if self.save_under.is_some() { |
7560 | expr_value |= u32::from(CW::SAVE_UNDER); |
7561 | } |
7562 | if self.event_mask.is_some() { |
7563 | expr_value |= u32::from(CW::EVENT_MASK); |
7564 | } |
7565 | if self.do_not_propogate_mask.is_some() { |
7566 | expr_value |= u32::from(CW::DONT_PROPAGATE); |
7567 | } |
7568 | if self.colormap.is_some() { |
7569 | expr_value |= u32::from(CW::COLORMAP); |
7570 | } |
7571 | if self.cursor.is_some() { |
7572 | expr_value |= u32::from(CW::CURSOR); |
7573 | } |
7574 | expr_value |
7575 | } |
7576 | } |
7577 | impl ChangeWindowAttributesAux { |
7578 | /// Create a new instance with all fields unset / not present. |
7579 | pub fn new() -> Self { |
7580 | Default::default() |
7581 | } |
7582 | /// Set the `background_pixmap` field of this structure. |
7583 | #[must_use ] |
7584 | pub fn background_pixmap<I>(mut self, value: I) -> Self where I: Into<Option<Pixmap>> { |
7585 | self.background_pixmap = value.into(); |
7586 | self |
7587 | } |
7588 | /// Set the `background_pixel` field of this structure. |
7589 | #[must_use ] |
7590 | pub fn background_pixel<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
7591 | self.background_pixel = value.into(); |
7592 | self |
7593 | } |
7594 | /// Set the `border_pixmap` field of this structure. |
7595 | #[must_use ] |
7596 | pub fn border_pixmap<I>(mut self, value: I) -> Self where I: Into<Option<Pixmap>> { |
7597 | self.border_pixmap = value.into(); |
7598 | self |
7599 | } |
7600 | /// Set the `border_pixel` field of this structure. |
7601 | #[must_use ] |
7602 | pub fn border_pixel<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
7603 | self.border_pixel = value.into(); |
7604 | self |
7605 | } |
7606 | /// Set the `bit_gravity` field of this structure. |
7607 | #[must_use ] |
7608 | pub fn bit_gravity<I>(mut self, value: I) -> Self where I: Into<Option<Gravity>> { |
7609 | self.bit_gravity = value.into(); |
7610 | self |
7611 | } |
7612 | /// Set the `win_gravity` field of this structure. |
7613 | #[must_use ] |
7614 | pub fn win_gravity<I>(mut self, value: I) -> Self where I: Into<Option<Gravity>> { |
7615 | self.win_gravity = value.into(); |
7616 | self |
7617 | } |
7618 | /// Set the `backing_store` field of this structure. |
7619 | #[must_use ] |
7620 | pub fn backing_store<I>(mut self, value: I) -> Self where I: Into<Option<BackingStore>> { |
7621 | self.backing_store = value.into(); |
7622 | self |
7623 | } |
7624 | /// Set the `backing_planes` field of this structure. |
7625 | #[must_use ] |
7626 | pub fn backing_planes<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
7627 | self.backing_planes = value.into(); |
7628 | self |
7629 | } |
7630 | /// Set the `backing_pixel` field of this structure. |
7631 | #[must_use ] |
7632 | pub fn backing_pixel<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
7633 | self.backing_pixel = value.into(); |
7634 | self |
7635 | } |
7636 | /// Set the `override_redirect` field of this structure. |
7637 | #[must_use ] |
7638 | pub fn override_redirect<I>(mut self, value: I) -> Self where I: Into<Option<Bool32>> { |
7639 | self.override_redirect = value.into(); |
7640 | self |
7641 | } |
7642 | /// Set the `save_under` field of this structure. |
7643 | #[must_use ] |
7644 | pub fn save_under<I>(mut self, value: I) -> Self where I: Into<Option<Bool32>> { |
7645 | self.save_under = value.into(); |
7646 | self |
7647 | } |
7648 | /// Set the `event_mask` field of this structure. |
7649 | #[must_use ] |
7650 | pub fn event_mask<I>(mut self, value: I) -> Self where I: Into<Option<EventMask>> { |
7651 | self.event_mask = value.into(); |
7652 | self |
7653 | } |
7654 | /// Set the `do_not_propogate_mask` field of this structure. |
7655 | #[must_use ] |
7656 | pub fn do_not_propogate_mask<I>(mut self, value: I) -> Self where I: Into<Option<EventMask>> { |
7657 | self.do_not_propogate_mask = value.into(); |
7658 | self |
7659 | } |
7660 | /// Set the `colormap` field of this structure. |
7661 | #[must_use ] |
7662 | pub fn colormap<I>(mut self, value: I) -> Self where I: Into<Option<Colormap>> { |
7663 | self.colormap = value.into(); |
7664 | self |
7665 | } |
7666 | /// Set the `cursor` field of this structure. |
7667 | #[must_use ] |
7668 | pub fn cursor<I>(mut self, value: I) -> Self where I: Into<Option<Cursor>> { |
7669 | self.cursor = value.into(); |
7670 | self |
7671 | } |
7672 | } |
7673 | |
7674 | /// Opcode for the ChangeWindowAttributes request |
7675 | pub const CHANGE_WINDOW_ATTRIBUTES_REQUEST: u8 = 2; |
7676 | /// change window attributes. |
7677 | /// |
7678 | /// Changes the attributes specified by `value_mask` for the specified `window`. |
7679 | /// |
7680 | /// # Fields |
7681 | /// |
7682 | /// * `window` - The window to change. |
7683 | /// * `value_list` - Values for each of the attributes specified in the bitmask `value_mask`. The |
7684 | /// order has to correspond to the order of possible `value_mask` bits. See the |
7685 | /// example. |
7686 | /// |
7687 | /// # Errors |
7688 | /// |
7689 | /// * `Access` - TODO: reasons? |
7690 | /// * `Colormap` - TODO: reasons? |
7691 | /// * `Cursor` - TODO: reasons? |
7692 | /// * `Match` - TODO: reasons? |
7693 | /// * `Pixmap` - TODO: reasons? |
7694 | /// * `Value` - TODO: reasons? |
7695 | /// * `Window` - The specified `window` does not exist. |
7696 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
7697 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
7698 | pub struct ChangeWindowAttributesRequest<'input> { |
7699 | pub window: Window, |
7700 | pub value_list: Cow<'input, ChangeWindowAttributesAux>, |
7701 | } |
7702 | impl<'input> ChangeWindowAttributesRequest<'input> { |
7703 | /// Serialize this request into bytes for the provided connection |
7704 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
7705 | let length_so_far = 0; |
7706 | let window_bytes = self.window.serialize(); |
7707 | let value_mask: u32 = self.value_list.switch_expr(); |
7708 | let value_mask_bytes = value_mask.serialize(); |
7709 | let mut request0 = vec![ |
7710 | CHANGE_WINDOW_ATTRIBUTES_REQUEST, |
7711 | 0, |
7712 | 0, |
7713 | 0, |
7714 | window_bytes[0], |
7715 | window_bytes[1], |
7716 | window_bytes[2], |
7717 | window_bytes[3], |
7718 | value_mask_bytes[0], |
7719 | value_mask_bytes[1], |
7720 | value_mask_bytes[2], |
7721 | value_mask_bytes[3], |
7722 | ]; |
7723 | let length_so_far = length_so_far + request0.len(); |
7724 | let value_list_bytes = self.value_list.serialize(u32::from(value_mask)); |
7725 | let length_so_far = length_so_far + value_list_bytes.len(); |
7726 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
7727 | let length_so_far = length_so_far + padding0.len(); |
7728 | assert_eq!(length_so_far % 4, 0); |
7729 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
7730 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
7731 | (vec![request0.into(), value_list_bytes.into(), padding0.into()], vec![]) |
7732 | } |
7733 | /// Parse this request given its header, its body, and any fds that go along with it |
7734 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
7735 | if header.major_opcode != CHANGE_WINDOW_ATTRIBUTES_REQUEST { |
7736 | return Err(ParseError::InvalidValue); |
7737 | } |
7738 | let remaining = &[header.minor_opcode]; |
7739 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
7740 | let _ = remaining; |
7741 | let (window, remaining) = Window::try_parse(value)?; |
7742 | let (value_mask, remaining) = u32::try_parse(remaining)?; |
7743 | let (value_list, remaining) = ChangeWindowAttributesAux::try_parse(remaining, u32::from(value_mask))?; |
7744 | let _ = remaining; |
7745 | Ok(ChangeWindowAttributesRequest { |
7746 | window, |
7747 | value_list: Cow::Owned(value_list), |
7748 | }) |
7749 | } |
7750 | /// Clone all borrowed data in this ChangeWindowAttributesRequest. |
7751 | pub fn into_owned(self) -> ChangeWindowAttributesRequest<'static> { |
7752 | ChangeWindowAttributesRequest { |
7753 | window: self.window, |
7754 | value_list: Cow::Owned(self.value_list.into_owned()), |
7755 | } |
7756 | } |
7757 | } |
7758 | impl<'input> Request for ChangeWindowAttributesRequest<'input> { |
7759 | const EXTENSION_NAME: Option<&'static str> = None; |
7760 | |
7761 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
7762 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
7763 | // Flatten the buffers into a single vector |
7764 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
7765 | (buf, fds) |
7766 | } |
7767 | } |
7768 | impl<'input> crate::x11_utils::VoidRequest for ChangeWindowAttributesRequest<'input> { |
7769 | } |
7770 | |
7771 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
7772 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
7773 | pub struct MapState(u8); |
7774 | impl MapState { |
7775 | pub const UNMAPPED: Self = Self(0); |
7776 | pub const UNVIEWABLE: Self = Self(1); |
7777 | pub const VIEWABLE: Self = Self(2); |
7778 | } |
7779 | impl From<MapState> for u8 { |
7780 | #[inline ] |
7781 | fn from(input: MapState) -> Self { |
7782 | input.0 |
7783 | } |
7784 | } |
7785 | impl From<MapState> for Option<u8> { |
7786 | #[inline ] |
7787 | fn from(input: MapState) -> Self { |
7788 | Some(input.0) |
7789 | } |
7790 | } |
7791 | impl From<MapState> for u16 { |
7792 | #[inline ] |
7793 | fn from(input: MapState) -> Self { |
7794 | u16::from(input.0) |
7795 | } |
7796 | } |
7797 | impl From<MapState> for Option<u16> { |
7798 | #[inline ] |
7799 | fn from(input: MapState) -> Self { |
7800 | Some(u16::from(input.0)) |
7801 | } |
7802 | } |
7803 | impl From<MapState> for u32 { |
7804 | #[inline ] |
7805 | fn from(input: MapState) -> Self { |
7806 | u32::from(input.0) |
7807 | } |
7808 | } |
7809 | impl From<MapState> for Option<u32> { |
7810 | #[inline ] |
7811 | fn from(input: MapState) -> Self { |
7812 | Some(u32::from(input.0)) |
7813 | } |
7814 | } |
7815 | impl From<u8> for MapState { |
7816 | #[inline ] |
7817 | fn from(value: u8) -> Self { |
7818 | Self(value) |
7819 | } |
7820 | } |
7821 | impl core::fmt::Debug for MapState { |
7822 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
7823 | let variants: [(u32, &str, &str); 3] = [ |
7824 | (Self::UNMAPPED.0.into(), "UNMAPPED" , "Unmapped" ), |
7825 | (Self::UNVIEWABLE.0.into(), "UNVIEWABLE" , "Unviewable" ), |
7826 | (Self::VIEWABLE.0.into(), "VIEWABLE" , "Viewable" ), |
7827 | ]; |
7828 | pretty_print_enum(fmt, self.0.into(), &variants) |
7829 | } |
7830 | } |
7831 | |
7832 | /// Opcode for the GetWindowAttributes request |
7833 | pub const GET_WINDOW_ATTRIBUTES_REQUEST: u8 = 3; |
7834 | /// Gets window attributes. |
7835 | /// |
7836 | /// Gets the current attributes for the specified `window`. |
7837 | /// |
7838 | /// # Fields |
7839 | /// |
7840 | /// * `window` - The window to get the attributes from. |
7841 | /// |
7842 | /// # Errors |
7843 | /// |
7844 | /// * `Window` - The specified `window` does not exist. |
7845 | /// * `Drawable` - TODO: reasons? |
7846 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
7847 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
7848 | pub struct GetWindowAttributesRequest { |
7849 | pub window: Window, |
7850 | } |
7851 | impl GetWindowAttributesRequest { |
7852 | /// Serialize this request into bytes for the provided connection |
7853 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
7854 | let length_so_far = 0; |
7855 | let window_bytes = self.window.serialize(); |
7856 | let mut request0 = vec![ |
7857 | GET_WINDOW_ATTRIBUTES_REQUEST, |
7858 | 0, |
7859 | 0, |
7860 | 0, |
7861 | window_bytes[0], |
7862 | window_bytes[1], |
7863 | window_bytes[2], |
7864 | window_bytes[3], |
7865 | ]; |
7866 | let length_so_far = length_so_far + request0.len(); |
7867 | assert_eq!(length_so_far % 4, 0); |
7868 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
7869 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
7870 | (vec![request0.into()], vec![]) |
7871 | } |
7872 | /// Parse this request given its header, its body, and any fds that go along with it |
7873 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
7874 | if header.major_opcode != GET_WINDOW_ATTRIBUTES_REQUEST { |
7875 | return Err(ParseError::InvalidValue); |
7876 | } |
7877 | let remaining = &[header.minor_opcode]; |
7878 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
7879 | let _ = remaining; |
7880 | let (window, remaining) = Window::try_parse(value)?; |
7881 | let _ = remaining; |
7882 | Ok(GetWindowAttributesRequest { |
7883 | window, |
7884 | }) |
7885 | } |
7886 | } |
7887 | impl Request for GetWindowAttributesRequest { |
7888 | const EXTENSION_NAME: Option<&'static str> = None; |
7889 | |
7890 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
7891 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
7892 | // Flatten the buffers into a single vector |
7893 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
7894 | (buf, fds) |
7895 | } |
7896 | } |
7897 | impl crate::x11_utils::ReplyRequest for GetWindowAttributesRequest { |
7898 | type Reply = GetWindowAttributesReply; |
7899 | } |
7900 | |
7901 | /// # Fields |
7902 | /// |
7903 | /// * `override_redirect` - Window managers should ignore this window if `override_redirect` is 1. |
7904 | /// * `visual` - The associated visual structure of `window`. |
7905 | /// * `backing_planes` - Planes to be preserved if possible. |
7906 | /// * `backing_pixel` - Value to be used when restoring planes. |
7907 | /// * `save_under` - Boolean, should bits under be saved? |
7908 | /// * `colormap` - Color map to be associated with window. |
7909 | /// * `all_event_masks` - Set of events all people have interest in. |
7910 | /// * `your_event_mask` - My event mask. |
7911 | /// * `do_not_propagate_mask` - Set of events that should not propagate. |
7912 | /// * `backing_store` - |
7913 | /// * `class` - |
7914 | /// * `bit_gravity` - |
7915 | /// * `win_gravity` - |
7916 | /// * `map_state` - |
7917 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
7918 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
7919 | pub struct GetWindowAttributesReply { |
7920 | pub backing_store: BackingStore, |
7921 | pub sequence: u16, |
7922 | pub length: u32, |
7923 | pub visual: Visualid, |
7924 | pub class: WindowClass, |
7925 | pub bit_gravity: Gravity, |
7926 | pub win_gravity: Gravity, |
7927 | pub backing_planes: u32, |
7928 | pub backing_pixel: u32, |
7929 | pub save_under: bool, |
7930 | pub map_is_installed: bool, |
7931 | pub map_state: MapState, |
7932 | pub override_redirect: bool, |
7933 | pub colormap: Colormap, |
7934 | pub all_event_masks: EventMask, |
7935 | pub your_event_mask: EventMask, |
7936 | pub do_not_propagate_mask: EventMask, |
7937 | } |
7938 | impl TryParse for GetWindowAttributesReply { |
7939 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
7940 | let remaining = initial_value; |
7941 | let (response_type, remaining) = u8::try_parse(remaining)?; |
7942 | let (backing_store, remaining) = u8::try_parse(remaining)?; |
7943 | let (sequence, remaining) = u16::try_parse(remaining)?; |
7944 | let (length, remaining) = u32::try_parse(remaining)?; |
7945 | let (visual, remaining) = Visualid::try_parse(remaining)?; |
7946 | let (class, remaining) = u16::try_parse(remaining)?; |
7947 | let (bit_gravity, remaining) = u8::try_parse(remaining)?; |
7948 | let (win_gravity, remaining) = u8::try_parse(remaining)?; |
7949 | let (backing_planes, remaining) = u32::try_parse(remaining)?; |
7950 | let (backing_pixel, remaining) = u32::try_parse(remaining)?; |
7951 | let (save_under, remaining) = bool::try_parse(remaining)?; |
7952 | let (map_is_installed, remaining) = bool::try_parse(remaining)?; |
7953 | let (map_state, remaining) = u8::try_parse(remaining)?; |
7954 | let (override_redirect, remaining) = bool::try_parse(remaining)?; |
7955 | let (colormap, remaining) = Colormap::try_parse(remaining)?; |
7956 | let (all_event_masks, remaining) = u32::try_parse(remaining)?; |
7957 | let (your_event_mask, remaining) = u32::try_parse(remaining)?; |
7958 | let (do_not_propagate_mask, remaining) = u16::try_parse(remaining)?; |
7959 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
7960 | if response_type != 1 { |
7961 | return Err(ParseError::InvalidValue); |
7962 | } |
7963 | let backing_store = backing_store.into(); |
7964 | let class = class.into(); |
7965 | let bit_gravity = bit_gravity.into(); |
7966 | let win_gravity = win_gravity.into(); |
7967 | let map_state = map_state.into(); |
7968 | let all_event_masks = all_event_masks.into(); |
7969 | let your_event_mask = your_event_mask.into(); |
7970 | let do_not_propagate_mask = do_not_propagate_mask.into(); |
7971 | let result = GetWindowAttributesReply { backing_store, sequence, length, visual, class, bit_gravity, win_gravity, backing_planes, backing_pixel, save_under, map_is_installed, map_state, override_redirect, colormap, all_event_masks, your_event_mask, do_not_propagate_mask }; |
7972 | let _ = remaining; |
7973 | let remaining = initial_value.get(32 + length as usize * 4..) |
7974 | .ok_or(ParseError::InsufficientData)?; |
7975 | Ok((result, remaining)) |
7976 | } |
7977 | } |
7978 | impl Serialize for GetWindowAttributesReply { |
7979 | type Bytes = [u8; 44]; |
7980 | fn serialize(&self) -> [u8; 44] { |
7981 | let response_type_bytes = &[1]; |
7982 | let backing_store_bytes = (u32::from(self.backing_store) as u8).serialize(); |
7983 | let sequence_bytes = self.sequence.serialize(); |
7984 | let length_bytes = self.length.serialize(); |
7985 | let visual_bytes = self.visual.serialize(); |
7986 | let class_bytes = u16::from(self.class).serialize(); |
7987 | let bit_gravity_bytes = (u32::from(self.bit_gravity) as u8).serialize(); |
7988 | let win_gravity_bytes = (u32::from(self.win_gravity) as u8).serialize(); |
7989 | let backing_planes_bytes = self.backing_planes.serialize(); |
7990 | let backing_pixel_bytes = self.backing_pixel.serialize(); |
7991 | let save_under_bytes = self.save_under.serialize(); |
7992 | let map_is_installed_bytes = self.map_is_installed.serialize(); |
7993 | let map_state_bytes = u8::from(self.map_state).serialize(); |
7994 | let override_redirect_bytes = self.override_redirect.serialize(); |
7995 | let colormap_bytes = self.colormap.serialize(); |
7996 | let all_event_masks_bytes = u32::from(self.all_event_masks).serialize(); |
7997 | let your_event_mask_bytes = u32::from(self.your_event_mask).serialize(); |
7998 | let do_not_propagate_mask_bytes = (u32::from(self.do_not_propagate_mask) as u16).serialize(); |
7999 | [ |
8000 | response_type_bytes[0], |
8001 | backing_store_bytes[0], |
8002 | sequence_bytes[0], |
8003 | sequence_bytes[1], |
8004 | length_bytes[0], |
8005 | length_bytes[1], |
8006 | length_bytes[2], |
8007 | length_bytes[3], |
8008 | visual_bytes[0], |
8009 | visual_bytes[1], |
8010 | visual_bytes[2], |
8011 | visual_bytes[3], |
8012 | class_bytes[0], |
8013 | class_bytes[1], |
8014 | bit_gravity_bytes[0], |
8015 | win_gravity_bytes[0], |
8016 | backing_planes_bytes[0], |
8017 | backing_planes_bytes[1], |
8018 | backing_planes_bytes[2], |
8019 | backing_planes_bytes[3], |
8020 | backing_pixel_bytes[0], |
8021 | backing_pixel_bytes[1], |
8022 | backing_pixel_bytes[2], |
8023 | backing_pixel_bytes[3], |
8024 | save_under_bytes[0], |
8025 | map_is_installed_bytes[0], |
8026 | map_state_bytes[0], |
8027 | override_redirect_bytes[0], |
8028 | colormap_bytes[0], |
8029 | colormap_bytes[1], |
8030 | colormap_bytes[2], |
8031 | colormap_bytes[3], |
8032 | all_event_masks_bytes[0], |
8033 | all_event_masks_bytes[1], |
8034 | all_event_masks_bytes[2], |
8035 | all_event_masks_bytes[3], |
8036 | your_event_mask_bytes[0], |
8037 | your_event_mask_bytes[1], |
8038 | your_event_mask_bytes[2], |
8039 | your_event_mask_bytes[3], |
8040 | do_not_propagate_mask_bytes[0], |
8041 | do_not_propagate_mask_bytes[1], |
8042 | 0, |
8043 | 0, |
8044 | ] |
8045 | } |
8046 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
8047 | bytes.reserve(44); |
8048 | let response_type_bytes = &[1]; |
8049 | bytes.push(response_type_bytes[0]); |
8050 | (u32::from(self.backing_store) as u8).serialize_into(bytes); |
8051 | self.sequence.serialize_into(bytes); |
8052 | self.length.serialize_into(bytes); |
8053 | self.visual.serialize_into(bytes); |
8054 | u16::from(self.class).serialize_into(bytes); |
8055 | (u32::from(self.bit_gravity) as u8).serialize_into(bytes); |
8056 | (u32::from(self.win_gravity) as u8).serialize_into(bytes); |
8057 | self.backing_planes.serialize_into(bytes); |
8058 | self.backing_pixel.serialize_into(bytes); |
8059 | self.save_under.serialize_into(bytes); |
8060 | self.map_is_installed.serialize_into(bytes); |
8061 | u8::from(self.map_state).serialize_into(bytes); |
8062 | self.override_redirect.serialize_into(bytes); |
8063 | self.colormap.serialize_into(bytes); |
8064 | u32::from(self.all_event_masks).serialize_into(bytes); |
8065 | u32::from(self.your_event_mask).serialize_into(bytes); |
8066 | (u32::from(self.do_not_propagate_mask) as u16).serialize_into(bytes); |
8067 | bytes.extend_from_slice(&[0; 2]); |
8068 | } |
8069 | } |
8070 | |
8071 | /// Opcode for the DestroyWindow request |
8072 | pub const DESTROY_WINDOW_REQUEST: u8 = 4; |
8073 | /// Destroys a window. |
8074 | /// |
8075 | /// Destroys the specified window and all of its subwindows. A DestroyNotify event |
8076 | /// is generated for each destroyed window (a DestroyNotify event is first generated |
8077 | /// for any given window's inferiors). If the window was mapped, it will be |
8078 | /// automatically unmapped before destroying. |
8079 | /// |
8080 | /// Calling DestroyWindow on the root window will do nothing. |
8081 | /// |
8082 | /// # Fields |
8083 | /// |
8084 | /// * `window` - The window to destroy. |
8085 | /// |
8086 | /// # Errors |
8087 | /// |
8088 | /// * `Window` - The specified window does not exist. |
8089 | /// |
8090 | /// # See |
8091 | /// |
8092 | /// * `DestroyNotify`: event |
8093 | /// * `MapWindow`: request |
8094 | /// * `UnmapWindow`: request |
8095 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
8096 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
8097 | pub struct DestroyWindowRequest { |
8098 | pub window: Window, |
8099 | } |
8100 | impl DestroyWindowRequest { |
8101 | /// Serialize this request into bytes for the provided connection |
8102 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
8103 | let length_so_far = 0; |
8104 | let window_bytes = self.window.serialize(); |
8105 | let mut request0 = vec![ |
8106 | DESTROY_WINDOW_REQUEST, |
8107 | 0, |
8108 | 0, |
8109 | 0, |
8110 | window_bytes[0], |
8111 | window_bytes[1], |
8112 | window_bytes[2], |
8113 | window_bytes[3], |
8114 | ]; |
8115 | let length_so_far = length_so_far + request0.len(); |
8116 | assert_eq!(length_so_far % 4, 0); |
8117 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
8118 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
8119 | (vec![request0.into()], vec![]) |
8120 | } |
8121 | /// Parse this request given its header, its body, and any fds that go along with it |
8122 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
8123 | if header.major_opcode != DESTROY_WINDOW_REQUEST { |
8124 | return Err(ParseError::InvalidValue); |
8125 | } |
8126 | let remaining = &[header.minor_opcode]; |
8127 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
8128 | let _ = remaining; |
8129 | let (window, remaining) = Window::try_parse(value)?; |
8130 | let _ = remaining; |
8131 | Ok(DestroyWindowRequest { |
8132 | window, |
8133 | }) |
8134 | } |
8135 | } |
8136 | impl Request for DestroyWindowRequest { |
8137 | const EXTENSION_NAME: Option<&'static str> = None; |
8138 | |
8139 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
8140 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
8141 | // Flatten the buffers into a single vector |
8142 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
8143 | (buf, fds) |
8144 | } |
8145 | } |
8146 | impl crate::x11_utils::VoidRequest for DestroyWindowRequest { |
8147 | } |
8148 | |
8149 | /// Opcode for the DestroySubwindows request |
8150 | pub const DESTROY_SUBWINDOWS_REQUEST: u8 = 5; |
8151 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
8152 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
8153 | pub struct DestroySubwindowsRequest { |
8154 | pub window: Window, |
8155 | } |
8156 | impl DestroySubwindowsRequest { |
8157 | /// Serialize this request into bytes for the provided connection |
8158 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
8159 | let length_so_far = 0; |
8160 | let window_bytes = self.window.serialize(); |
8161 | let mut request0 = vec![ |
8162 | DESTROY_SUBWINDOWS_REQUEST, |
8163 | 0, |
8164 | 0, |
8165 | 0, |
8166 | window_bytes[0], |
8167 | window_bytes[1], |
8168 | window_bytes[2], |
8169 | window_bytes[3], |
8170 | ]; |
8171 | let length_so_far = length_so_far + request0.len(); |
8172 | assert_eq!(length_so_far % 4, 0); |
8173 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
8174 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
8175 | (vec![request0.into()], vec![]) |
8176 | } |
8177 | /// Parse this request given its header, its body, and any fds that go along with it |
8178 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
8179 | if header.major_opcode != DESTROY_SUBWINDOWS_REQUEST { |
8180 | return Err(ParseError::InvalidValue); |
8181 | } |
8182 | let remaining = &[header.minor_opcode]; |
8183 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
8184 | let _ = remaining; |
8185 | let (window, remaining) = Window::try_parse(value)?; |
8186 | let _ = remaining; |
8187 | Ok(DestroySubwindowsRequest { |
8188 | window, |
8189 | }) |
8190 | } |
8191 | } |
8192 | impl Request for DestroySubwindowsRequest { |
8193 | const EXTENSION_NAME: Option<&'static str> = None; |
8194 | |
8195 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
8196 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
8197 | // Flatten the buffers into a single vector |
8198 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
8199 | (buf, fds) |
8200 | } |
8201 | } |
8202 | impl crate::x11_utils::VoidRequest for DestroySubwindowsRequest { |
8203 | } |
8204 | |
8205 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
8206 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
8207 | pub struct SetMode(u8); |
8208 | impl SetMode { |
8209 | pub const INSERT: Self = Self(0); |
8210 | pub const DELETE: Self = Self(1); |
8211 | } |
8212 | impl From<SetMode> for u8 { |
8213 | #[inline ] |
8214 | fn from(input: SetMode) -> Self { |
8215 | input.0 |
8216 | } |
8217 | } |
8218 | impl From<SetMode> for Option<u8> { |
8219 | #[inline ] |
8220 | fn from(input: SetMode) -> Self { |
8221 | Some(input.0) |
8222 | } |
8223 | } |
8224 | impl From<SetMode> for u16 { |
8225 | #[inline ] |
8226 | fn from(input: SetMode) -> Self { |
8227 | u16::from(input.0) |
8228 | } |
8229 | } |
8230 | impl From<SetMode> for Option<u16> { |
8231 | #[inline ] |
8232 | fn from(input: SetMode) -> Self { |
8233 | Some(u16::from(input.0)) |
8234 | } |
8235 | } |
8236 | impl From<SetMode> for u32 { |
8237 | #[inline ] |
8238 | fn from(input: SetMode) -> Self { |
8239 | u32::from(input.0) |
8240 | } |
8241 | } |
8242 | impl From<SetMode> for Option<u32> { |
8243 | #[inline ] |
8244 | fn from(input: SetMode) -> Self { |
8245 | Some(u32::from(input.0)) |
8246 | } |
8247 | } |
8248 | impl From<u8> for SetMode { |
8249 | #[inline ] |
8250 | fn from(value: u8) -> Self { |
8251 | Self(value) |
8252 | } |
8253 | } |
8254 | impl core::fmt::Debug for SetMode { |
8255 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
8256 | let variants: [(u32, &str, &str); 2] = [ |
8257 | (Self::INSERT.0.into(), "INSERT" , "Insert" ), |
8258 | (Self::DELETE.0.into(), "DELETE" , "Delete" ), |
8259 | ]; |
8260 | pretty_print_enum(fmt, self.0.into(), &variants) |
8261 | } |
8262 | } |
8263 | |
8264 | /// Opcode for the ChangeSaveSet request |
8265 | pub const CHANGE_SAVE_SET_REQUEST: u8 = 6; |
8266 | /// Changes a client's save set. |
8267 | /// |
8268 | /// TODO: explain what the save set is for. |
8269 | /// |
8270 | /// This function either adds or removes the specified window to the client's (your |
8271 | /// application's) save set. |
8272 | /// |
8273 | /// # Fields |
8274 | /// |
8275 | /// * `mode` - Insert to add the specified window to the save set or Delete to delete it from the save set. |
8276 | /// * `window` - The window to add or delete to/from your save set. |
8277 | /// |
8278 | /// # Errors |
8279 | /// |
8280 | /// * `Match` - You created the specified window. This does not make sense, you can only add |
8281 | /// windows created by other clients to your save set. |
8282 | /// * `Value` - You specified an invalid mode. |
8283 | /// * `Window` - The specified window does not exist. |
8284 | /// |
8285 | /// # See |
8286 | /// |
8287 | /// * `ReparentWindow`: request |
8288 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
8289 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
8290 | pub struct ChangeSaveSetRequest { |
8291 | pub mode: SetMode, |
8292 | pub window: Window, |
8293 | } |
8294 | impl ChangeSaveSetRequest { |
8295 | /// Serialize this request into bytes for the provided connection |
8296 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
8297 | let length_so_far = 0; |
8298 | let mode_bytes = u8::from(self.mode).serialize(); |
8299 | let window_bytes = self.window.serialize(); |
8300 | let mut request0 = vec![ |
8301 | CHANGE_SAVE_SET_REQUEST, |
8302 | mode_bytes[0], |
8303 | 0, |
8304 | 0, |
8305 | window_bytes[0], |
8306 | window_bytes[1], |
8307 | window_bytes[2], |
8308 | window_bytes[3], |
8309 | ]; |
8310 | let length_so_far = length_so_far + request0.len(); |
8311 | assert_eq!(length_so_far % 4, 0); |
8312 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
8313 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
8314 | (vec![request0.into()], vec![]) |
8315 | } |
8316 | /// Parse this request given its header, its body, and any fds that go along with it |
8317 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
8318 | if header.major_opcode != CHANGE_SAVE_SET_REQUEST { |
8319 | return Err(ParseError::InvalidValue); |
8320 | } |
8321 | let remaining = &[header.minor_opcode]; |
8322 | let (mode, remaining) = u8::try_parse(remaining)?; |
8323 | let mode = mode.into(); |
8324 | let _ = remaining; |
8325 | let (window, remaining) = Window::try_parse(value)?; |
8326 | let _ = remaining; |
8327 | Ok(ChangeSaveSetRequest { |
8328 | mode, |
8329 | window, |
8330 | }) |
8331 | } |
8332 | } |
8333 | impl Request for ChangeSaveSetRequest { |
8334 | const EXTENSION_NAME: Option<&'static str> = None; |
8335 | |
8336 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
8337 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
8338 | // Flatten the buffers into a single vector |
8339 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
8340 | (buf, fds) |
8341 | } |
8342 | } |
8343 | impl crate::x11_utils::VoidRequest for ChangeSaveSetRequest { |
8344 | } |
8345 | |
8346 | /// Opcode for the ReparentWindow request |
8347 | pub const REPARENT_WINDOW_REQUEST: u8 = 7; |
8348 | /// Reparents a window. |
8349 | /// |
8350 | /// Makes the specified window a child of the specified parent window. If the |
8351 | /// window is mapped, it will automatically be unmapped before reparenting and |
8352 | /// re-mapped after reparenting. The window is placed in the stacking order on top |
8353 | /// with respect to sibling windows. |
8354 | /// |
8355 | /// After reparenting, a ReparentNotify event is generated. |
8356 | /// |
8357 | /// # Fields |
8358 | /// |
8359 | /// * `window` - The window to reparent. |
8360 | /// * `parent` - The new parent of the window. |
8361 | /// * `x` - The X position of the window within its new parent. |
8362 | /// * `y` - The Y position of the window within its new parent. |
8363 | /// |
8364 | /// # Errors |
8365 | /// |
8366 | /// * `Match` - The new parent window is not on the same screen as the old parent window. |
8367 | /// |
8368 | /// The new parent window is the specified window or an inferior of the specified window. |
8369 | /// |
8370 | /// The new parent is InputOnly and the window is not. |
8371 | /// |
8372 | /// The specified window has a ParentRelative background and the new parent window is not the same depth as the specified window. |
8373 | /// * `Window` - The specified window does not exist. |
8374 | /// |
8375 | /// # See |
8376 | /// |
8377 | /// * `ReparentNotify`: event |
8378 | /// * `MapWindow`: request |
8379 | /// * `UnmapWindow`: request |
8380 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
8381 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
8382 | pub struct ReparentWindowRequest { |
8383 | pub window: Window, |
8384 | pub parent: Window, |
8385 | pub x: i16, |
8386 | pub y: i16, |
8387 | } |
8388 | impl ReparentWindowRequest { |
8389 | /// Serialize this request into bytes for the provided connection |
8390 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
8391 | let length_so_far = 0; |
8392 | let window_bytes = self.window.serialize(); |
8393 | let parent_bytes = self.parent.serialize(); |
8394 | let x_bytes = self.x.serialize(); |
8395 | let y_bytes = self.y.serialize(); |
8396 | let mut request0 = vec![ |
8397 | REPARENT_WINDOW_REQUEST, |
8398 | 0, |
8399 | 0, |
8400 | 0, |
8401 | window_bytes[0], |
8402 | window_bytes[1], |
8403 | window_bytes[2], |
8404 | window_bytes[3], |
8405 | parent_bytes[0], |
8406 | parent_bytes[1], |
8407 | parent_bytes[2], |
8408 | parent_bytes[3], |
8409 | x_bytes[0], |
8410 | x_bytes[1], |
8411 | y_bytes[0], |
8412 | y_bytes[1], |
8413 | ]; |
8414 | let length_so_far = length_so_far + request0.len(); |
8415 | assert_eq!(length_so_far % 4, 0); |
8416 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
8417 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
8418 | (vec![request0.into()], vec![]) |
8419 | } |
8420 | /// Parse this request given its header, its body, and any fds that go along with it |
8421 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
8422 | if header.major_opcode != REPARENT_WINDOW_REQUEST { |
8423 | return Err(ParseError::InvalidValue); |
8424 | } |
8425 | let remaining = &[header.minor_opcode]; |
8426 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
8427 | let _ = remaining; |
8428 | let (window, remaining) = Window::try_parse(value)?; |
8429 | let (parent, remaining) = Window::try_parse(remaining)?; |
8430 | let (x, remaining) = i16::try_parse(remaining)?; |
8431 | let (y, remaining) = i16::try_parse(remaining)?; |
8432 | let _ = remaining; |
8433 | Ok(ReparentWindowRequest { |
8434 | window, |
8435 | parent, |
8436 | x, |
8437 | y, |
8438 | }) |
8439 | } |
8440 | } |
8441 | impl Request for ReparentWindowRequest { |
8442 | const EXTENSION_NAME: Option<&'static str> = None; |
8443 | |
8444 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
8445 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
8446 | // Flatten the buffers into a single vector |
8447 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
8448 | (buf, fds) |
8449 | } |
8450 | } |
8451 | impl crate::x11_utils::VoidRequest for ReparentWindowRequest { |
8452 | } |
8453 | |
8454 | /// Opcode for the MapWindow request |
8455 | pub const MAP_WINDOW_REQUEST: u8 = 8; |
8456 | /// Makes a window visible. |
8457 | /// |
8458 | /// Maps the specified window. This means making the window visible (as long as its |
8459 | /// parent is visible). |
8460 | /// |
8461 | /// This MapWindow request will be translated to a MapRequest request if a window |
8462 | /// manager is running. The window manager then decides to either map the window or |
8463 | /// not. Set the override-redirect window attribute to true if you want to bypass |
8464 | /// this mechanism. |
8465 | /// |
8466 | /// If the window manager decides to map the window (or if no window manager is |
8467 | /// running), a MapNotify event is generated. |
8468 | /// |
8469 | /// If the window becomes viewable and no earlier contents for it are remembered, |
8470 | /// the X server tiles the window with its background. If the window's background |
8471 | /// is undefined, the existing screen contents are not altered, and the X server |
8472 | /// generates zero or more Expose events. |
8473 | /// |
8474 | /// If the window type is InputOutput, an Expose event will be generated when the |
8475 | /// window becomes visible. The normal response to an Expose event should be to |
8476 | /// repaint the window. |
8477 | /// |
8478 | /// # Fields |
8479 | /// |
8480 | /// * `window` - The window to make visible. |
8481 | /// |
8482 | /// # Errors |
8483 | /// |
8484 | /// * `Match` - The specified window does not exist. |
8485 | /// |
8486 | /// # See |
8487 | /// |
8488 | /// * `MapNotify`: event |
8489 | /// * `Expose`: event |
8490 | /// * `UnmapWindow`: request |
8491 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
8492 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
8493 | pub struct MapWindowRequest { |
8494 | pub window: Window, |
8495 | } |
8496 | impl MapWindowRequest { |
8497 | /// Serialize this request into bytes for the provided connection |
8498 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
8499 | let length_so_far = 0; |
8500 | let window_bytes = self.window.serialize(); |
8501 | let mut request0 = vec![ |
8502 | MAP_WINDOW_REQUEST, |
8503 | 0, |
8504 | 0, |
8505 | 0, |
8506 | window_bytes[0], |
8507 | window_bytes[1], |
8508 | window_bytes[2], |
8509 | window_bytes[3], |
8510 | ]; |
8511 | let length_so_far = length_so_far + request0.len(); |
8512 | assert_eq!(length_so_far % 4, 0); |
8513 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
8514 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
8515 | (vec![request0.into()], vec![]) |
8516 | } |
8517 | /// Parse this request given its header, its body, and any fds that go along with it |
8518 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
8519 | if header.major_opcode != MAP_WINDOW_REQUEST { |
8520 | return Err(ParseError::InvalidValue); |
8521 | } |
8522 | let remaining = &[header.minor_opcode]; |
8523 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
8524 | let _ = remaining; |
8525 | let (window, remaining) = Window::try_parse(value)?; |
8526 | let _ = remaining; |
8527 | Ok(MapWindowRequest { |
8528 | window, |
8529 | }) |
8530 | } |
8531 | } |
8532 | impl Request for MapWindowRequest { |
8533 | const EXTENSION_NAME: Option<&'static str> = None; |
8534 | |
8535 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
8536 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
8537 | // Flatten the buffers into a single vector |
8538 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
8539 | (buf, fds) |
8540 | } |
8541 | } |
8542 | impl crate::x11_utils::VoidRequest for MapWindowRequest { |
8543 | } |
8544 | |
8545 | /// Opcode for the MapSubwindows request |
8546 | pub const MAP_SUBWINDOWS_REQUEST: u8 = 9; |
8547 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
8548 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
8549 | pub struct MapSubwindowsRequest { |
8550 | pub window: Window, |
8551 | } |
8552 | impl MapSubwindowsRequest { |
8553 | /// Serialize this request into bytes for the provided connection |
8554 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
8555 | let length_so_far = 0; |
8556 | let window_bytes = self.window.serialize(); |
8557 | let mut request0 = vec![ |
8558 | MAP_SUBWINDOWS_REQUEST, |
8559 | 0, |
8560 | 0, |
8561 | 0, |
8562 | window_bytes[0], |
8563 | window_bytes[1], |
8564 | window_bytes[2], |
8565 | window_bytes[3], |
8566 | ]; |
8567 | let length_so_far = length_so_far + request0.len(); |
8568 | assert_eq!(length_so_far % 4, 0); |
8569 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
8570 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
8571 | (vec![request0.into()], vec![]) |
8572 | } |
8573 | /// Parse this request given its header, its body, and any fds that go along with it |
8574 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
8575 | if header.major_opcode != MAP_SUBWINDOWS_REQUEST { |
8576 | return Err(ParseError::InvalidValue); |
8577 | } |
8578 | let remaining = &[header.minor_opcode]; |
8579 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
8580 | let _ = remaining; |
8581 | let (window, remaining) = Window::try_parse(value)?; |
8582 | let _ = remaining; |
8583 | Ok(MapSubwindowsRequest { |
8584 | window, |
8585 | }) |
8586 | } |
8587 | } |
8588 | impl Request for MapSubwindowsRequest { |
8589 | const EXTENSION_NAME: Option<&'static str> = None; |
8590 | |
8591 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
8592 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
8593 | // Flatten the buffers into a single vector |
8594 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
8595 | (buf, fds) |
8596 | } |
8597 | } |
8598 | impl crate::x11_utils::VoidRequest for MapSubwindowsRequest { |
8599 | } |
8600 | |
8601 | /// Opcode for the UnmapWindow request |
8602 | pub const UNMAP_WINDOW_REQUEST: u8 = 10; |
8603 | /// Makes a window invisible. |
8604 | /// |
8605 | /// Unmaps the specified window. This means making the window invisible (and all |
8606 | /// its child windows). |
8607 | /// |
8608 | /// Unmapping a window leads to the `UnmapNotify` event being generated. Also, |
8609 | /// `Expose` events are generated for formerly obscured windows. |
8610 | /// |
8611 | /// # Fields |
8612 | /// |
8613 | /// * `window` - The window to make invisible. |
8614 | /// |
8615 | /// # Errors |
8616 | /// |
8617 | /// * `Window` - The specified window does not exist. |
8618 | /// |
8619 | /// # See |
8620 | /// |
8621 | /// * `UnmapNotify`: event |
8622 | /// * `Expose`: event |
8623 | /// * `MapWindow`: request |
8624 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
8625 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
8626 | pub struct UnmapWindowRequest { |
8627 | pub window: Window, |
8628 | } |
8629 | impl UnmapWindowRequest { |
8630 | /// Serialize this request into bytes for the provided connection |
8631 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
8632 | let length_so_far = 0; |
8633 | let window_bytes = self.window.serialize(); |
8634 | let mut request0 = vec![ |
8635 | UNMAP_WINDOW_REQUEST, |
8636 | 0, |
8637 | 0, |
8638 | 0, |
8639 | window_bytes[0], |
8640 | window_bytes[1], |
8641 | window_bytes[2], |
8642 | window_bytes[3], |
8643 | ]; |
8644 | let length_so_far = length_so_far + request0.len(); |
8645 | assert_eq!(length_so_far % 4, 0); |
8646 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
8647 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
8648 | (vec![request0.into()], vec![]) |
8649 | } |
8650 | /// Parse this request given its header, its body, and any fds that go along with it |
8651 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
8652 | if header.major_opcode != UNMAP_WINDOW_REQUEST { |
8653 | return Err(ParseError::InvalidValue); |
8654 | } |
8655 | let remaining = &[header.minor_opcode]; |
8656 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
8657 | let _ = remaining; |
8658 | let (window, remaining) = Window::try_parse(value)?; |
8659 | let _ = remaining; |
8660 | Ok(UnmapWindowRequest { |
8661 | window, |
8662 | }) |
8663 | } |
8664 | } |
8665 | impl Request for UnmapWindowRequest { |
8666 | const EXTENSION_NAME: Option<&'static str> = None; |
8667 | |
8668 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
8669 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
8670 | // Flatten the buffers into a single vector |
8671 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
8672 | (buf, fds) |
8673 | } |
8674 | } |
8675 | impl crate::x11_utils::VoidRequest for UnmapWindowRequest { |
8676 | } |
8677 | |
8678 | /// Opcode for the UnmapSubwindows request |
8679 | pub const UNMAP_SUBWINDOWS_REQUEST: u8 = 11; |
8680 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
8681 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
8682 | pub struct UnmapSubwindowsRequest { |
8683 | pub window: Window, |
8684 | } |
8685 | impl UnmapSubwindowsRequest { |
8686 | /// Serialize this request into bytes for the provided connection |
8687 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
8688 | let length_so_far = 0; |
8689 | let window_bytes = self.window.serialize(); |
8690 | let mut request0 = vec![ |
8691 | UNMAP_SUBWINDOWS_REQUEST, |
8692 | 0, |
8693 | 0, |
8694 | 0, |
8695 | window_bytes[0], |
8696 | window_bytes[1], |
8697 | window_bytes[2], |
8698 | window_bytes[3], |
8699 | ]; |
8700 | let length_so_far = length_so_far + request0.len(); |
8701 | assert_eq!(length_so_far % 4, 0); |
8702 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
8703 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
8704 | (vec![request0.into()], vec![]) |
8705 | } |
8706 | /// Parse this request given its header, its body, and any fds that go along with it |
8707 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
8708 | if header.major_opcode != UNMAP_SUBWINDOWS_REQUEST { |
8709 | return Err(ParseError::InvalidValue); |
8710 | } |
8711 | let remaining = &[header.minor_opcode]; |
8712 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
8713 | let _ = remaining; |
8714 | let (window, remaining) = Window::try_parse(value)?; |
8715 | let _ = remaining; |
8716 | Ok(UnmapSubwindowsRequest { |
8717 | window, |
8718 | }) |
8719 | } |
8720 | } |
8721 | impl Request for UnmapSubwindowsRequest { |
8722 | const EXTENSION_NAME: Option<&'static str> = None; |
8723 | |
8724 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
8725 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
8726 | // Flatten the buffers into a single vector |
8727 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
8728 | (buf, fds) |
8729 | } |
8730 | } |
8731 | impl crate::x11_utils::VoidRequest for UnmapSubwindowsRequest { |
8732 | } |
8733 | |
8734 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
8735 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
8736 | pub struct ConfigWindow(u16); |
8737 | impl ConfigWindow { |
8738 | pub const X: Self = Self(1 << 0); |
8739 | pub const Y: Self = Self(1 << 1); |
8740 | pub const WIDTH: Self = Self(1 << 2); |
8741 | pub const HEIGHT: Self = Self(1 << 3); |
8742 | pub const BORDER_WIDTH: Self = Self(1 << 4); |
8743 | pub const SIBLING: Self = Self(1 << 5); |
8744 | pub const STACK_MODE: Self = Self(1 << 6); |
8745 | } |
8746 | impl From<ConfigWindow> for u16 { |
8747 | #[inline ] |
8748 | fn from(input: ConfigWindow) -> Self { |
8749 | input.0 |
8750 | } |
8751 | } |
8752 | impl From<ConfigWindow> for Option<u16> { |
8753 | #[inline ] |
8754 | fn from(input: ConfigWindow) -> Self { |
8755 | Some(input.0) |
8756 | } |
8757 | } |
8758 | impl From<ConfigWindow> for u32 { |
8759 | #[inline ] |
8760 | fn from(input: ConfigWindow) -> Self { |
8761 | u32::from(input.0) |
8762 | } |
8763 | } |
8764 | impl From<ConfigWindow> for Option<u32> { |
8765 | #[inline ] |
8766 | fn from(input: ConfigWindow) -> Self { |
8767 | Some(u32::from(input.0)) |
8768 | } |
8769 | } |
8770 | impl From<u8> for ConfigWindow { |
8771 | #[inline ] |
8772 | fn from(value: u8) -> Self { |
8773 | Self(value.into()) |
8774 | } |
8775 | } |
8776 | impl From<u16> for ConfigWindow { |
8777 | #[inline ] |
8778 | fn from(value: u16) -> Self { |
8779 | Self(value) |
8780 | } |
8781 | } |
8782 | impl core::fmt::Debug for ConfigWindow { |
8783 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
8784 | let variants: [(u32, &str, &str); 7] = [ |
8785 | (Self::X.0.into(), "X" , "X" ), |
8786 | (Self::Y.0.into(), "Y" , "Y" ), |
8787 | (Self::WIDTH.0.into(), "WIDTH" , "Width" ), |
8788 | (Self::HEIGHT.0.into(), "HEIGHT" , "Height" ), |
8789 | (Self::BORDER_WIDTH.0.into(), "BORDER_WIDTH" , "BorderWidth" ), |
8790 | (Self::SIBLING.0.into(), "SIBLING" , "Sibling" ), |
8791 | (Self::STACK_MODE.0.into(), "STACK_MODE" , "StackMode" ), |
8792 | ]; |
8793 | pretty_print_bitmask(fmt, self.0.into(), &variants) |
8794 | } |
8795 | } |
8796 | bitmask_binop!(ConfigWindow, u16); |
8797 | |
8798 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
8799 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
8800 | pub struct StackMode(u32); |
8801 | impl StackMode { |
8802 | pub const ABOVE: Self = Self(0); |
8803 | pub const BELOW: Self = Self(1); |
8804 | pub const TOP_IF: Self = Self(2); |
8805 | pub const BOTTOM_IF: Self = Self(3); |
8806 | pub const OPPOSITE: Self = Self(4); |
8807 | } |
8808 | impl From<StackMode> for u32 { |
8809 | #[inline ] |
8810 | fn from(input: StackMode) -> Self { |
8811 | input.0 |
8812 | } |
8813 | } |
8814 | impl From<StackMode> for Option<u32> { |
8815 | #[inline ] |
8816 | fn from(input: StackMode) -> Self { |
8817 | Some(input.0) |
8818 | } |
8819 | } |
8820 | impl From<u8> for StackMode { |
8821 | #[inline ] |
8822 | fn from(value: u8) -> Self { |
8823 | Self(value.into()) |
8824 | } |
8825 | } |
8826 | impl From<u16> for StackMode { |
8827 | #[inline ] |
8828 | fn from(value: u16) -> Self { |
8829 | Self(value.into()) |
8830 | } |
8831 | } |
8832 | impl From<u32> for StackMode { |
8833 | #[inline ] |
8834 | fn from(value: u32) -> Self { |
8835 | Self(value) |
8836 | } |
8837 | } |
8838 | impl core::fmt::Debug for StackMode { |
8839 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
8840 | let variants: [(u32, &str, &str); 5] = [ |
8841 | (Self::ABOVE.0, "ABOVE" , "Above" ), |
8842 | (Self::BELOW.0, "BELOW" , "Below" ), |
8843 | (Self::TOP_IF.0, "TOP_IF" , "TopIf" ), |
8844 | (Self::BOTTOM_IF.0, "BOTTOM_IF" , "BottomIf" ), |
8845 | (Self::OPPOSITE.0, "OPPOSITE" , "Opposite" ), |
8846 | ]; |
8847 | pretty_print_enum(fmt, self.0, &variants) |
8848 | } |
8849 | } |
8850 | |
8851 | /// Auxiliary and optional information for the `configure_window` function |
8852 | #[derive (Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] |
8853 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
8854 | pub struct ConfigureWindowAux { |
8855 | pub x: Option<i32>, |
8856 | pub y: Option<i32>, |
8857 | pub width: Option<u32>, |
8858 | pub height: Option<u32>, |
8859 | pub border_width: Option<u32>, |
8860 | pub sibling: Option<Window>, |
8861 | pub stack_mode: Option<StackMode>, |
8862 | } |
8863 | impl ConfigureWindowAux { |
8864 | fn try_parse(value: &[u8], value_mask: u16) -> Result<(Self, &[u8]), ParseError> { |
8865 | let switch_expr = u16::from(value_mask); |
8866 | let mut outer_remaining = value; |
8867 | let x = if switch_expr & u16::from(ConfigWindow::X) != 0 { |
8868 | let remaining = outer_remaining; |
8869 | let (x, remaining) = i32::try_parse(remaining)?; |
8870 | outer_remaining = remaining; |
8871 | Some(x) |
8872 | } else { |
8873 | None |
8874 | }; |
8875 | let y = if switch_expr & u16::from(ConfigWindow::Y) != 0 { |
8876 | let remaining = outer_remaining; |
8877 | let (y, remaining) = i32::try_parse(remaining)?; |
8878 | outer_remaining = remaining; |
8879 | Some(y) |
8880 | } else { |
8881 | None |
8882 | }; |
8883 | let width = if switch_expr & u16::from(ConfigWindow::WIDTH) != 0 { |
8884 | let remaining = outer_remaining; |
8885 | let (width, remaining) = u32::try_parse(remaining)?; |
8886 | outer_remaining = remaining; |
8887 | Some(width) |
8888 | } else { |
8889 | None |
8890 | }; |
8891 | let height = if switch_expr & u16::from(ConfigWindow::HEIGHT) != 0 { |
8892 | let remaining = outer_remaining; |
8893 | let (height, remaining) = u32::try_parse(remaining)?; |
8894 | outer_remaining = remaining; |
8895 | Some(height) |
8896 | } else { |
8897 | None |
8898 | }; |
8899 | let border_width = if switch_expr & u16::from(ConfigWindow::BORDER_WIDTH) != 0 { |
8900 | let remaining = outer_remaining; |
8901 | let (border_width, remaining) = u32::try_parse(remaining)?; |
8902 | outer_remaining = remaining; |
8903 | Some(border_width) |
8904 | } else { |
8905 | None |
8906 | }; |
8907 | let sibling = if switch_expr & u16::from(ConfigWindow::SIBLING) != 0 { |
8908 | let remaining = outer_remaining; |
8909 | let (sibling, remaining) = Window::try_parse(remaining)?; |
8910 | outer_remaining = remaining; |
8911 | Some(sibling) |
8912 | } else { |
8913 | None |
8914 | }; |
8915 | let stack_mode = if switch_expr & u16::from(ConfigWindow::STACK_MODE) != 0 { |
8916 | let remaining = outer_remaining; |
8917 | let (stack_mode, remaining) = u32::try_parse(remaining)?; |
8918 | let stack_mode = stack_mode.into(); |
8919 | outer_remaining = remaining; |
8920 | Some(stack_mode) |
8921 | } else { |
8922 | None |
8923 | }; |
8924 | let result = ConfigureWindowAux { x, y, width, height, border_width, sibling, stack_mode }; |
8925 | Ok((result, outer_remaining)) |
8926 | } |
8927 | } |
8928 | impl ConfigureWindowAux { |
8929 | #[allow (dead_code)] |
8930 | fn serialize(&self, value_mask: u16) -> Vec<u8> { |
8931 | let mut result = Vec::new(); |
8932 | self.serialize_into(&mut result, u16::from(value_mask)); |
8933 | result |
8934 | } |
8935 | fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u16) { |
8936 | assert_eq!(self.switch_expr(), u16::from(value_mask), "switch `value_list` has an inconsistent discriminant" ); |
8937 | if let Some(x) = self.x { |
8938 | x.serialize_into(bytes); |
8939 | } |
8940 | if let Some(y) = self.y { |
8941 | y.serialize_into(bytes); |
8942 | } |
8943 | if let Some(width) = self.width { |
8944 | width.serialize_into(bytes); |
8945 | } |
8946 | if let Some(height) = self.height { |
8947 | height.serialize_into(bytes); |
8948 | } |
8949 | if let Some(border_width) = self.border_width { |
8950 | border_width.serialize_into(bytes); |
8951 | } |
8952 | if let Some(sibling) = self.sibling { |
8953 | sibling.serialize_into(bytes); |
8954 | } |
8955 | if let Some(stack_mode) = self.stack_mode { |
8956 | u32::from(stack_mode).serialize_into(bytes); |
8957 | } |
8958 | } |
8959 | } |
8960 | impl ConfigureWindowAux { |
8961 | fn switch_expr(&self) -> u16 { |
8962 | let mut expr_value = 0; |
8963 | if self.x.is_some() { |
8964 | expr_value |= u16::from(ConfigWindow::X); |
8965 | } |
8966 | if self.y.is_some() { |
8967 | expr_value |= u16::from(ConfigWindow::Y); |
8968 | } |
8969 | if self.width.is_some() { |
8970 | expr_value |= u16::from(ConfigWindow::WIDTH); |
8971 | } |
8972 | if self.height.is_some() { |
8973 | expr_value |= u16::from(ConfigWindow::HEIGHT); |
8974 | } |
8975 | if self.border_width.is_some() { |
8976 | expr_value |= u16::from(ConfigWindow::BORDER_WIDTH); |
8977 | } |
8978 | if self.sibling.is_some() { |
8979 | expr_value |= u16::from(ConfigWindow::SIBLING); |
8980 | } |
8981 | if self.stack_mode.is_some() { |
8982 | expr_value |= u16::from(ConfigWindow::STACK_MODE); |
8983 | } |
8984 | expr_value |
8985 | } |
8986 | } |
8987 | impl ConfigureWindowAux { |
8988 | /// Create a new instance with all fields unset / not present. |
8989 | pub fn new() -> Self { |
8990 | Default::default() |
8991 | } |
8992 | /// Set the `x` field of this structure. |
8993 | #[must_use ] |
8994 | pub fn x<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
8995 | self.x = value.into(); |
8996 | self |
8997 | } |
8998 | /// Set the `y` field of this structure. |
8999 | #[must_use ] |
9000 | pub fn y<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
9001 | self.y = value.into(); |
9002 | self |
9003 | } |
9004 | /// Set the `width` field of this structure. |
9005 | #[must_use ] |
9006 | pub fn width<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
9007 | self.width = value.into(); |
9008 | self |
9009 | } |
9010 | /// Set the `height` field of this structure. |
9011 | #[must_use ] |
9012 | pub fn height<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
9013 | self.height = value.into(); |
9014 | self |
9015 | } |
9016 | /// Set the `border_width` field of this structure. |
9017 | #[must_use ] |
9018 | pub fn border_width<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
9019 | self.border_width = value.into(); |
9020 | self |
9021 | } |
9022 | /// Set the `sibling` field of this structure. |
9023 | #[must_use ] |
9024 | pub fn sibling<I>(mut self, value: I) -> Self where I: Into<Option<Window>> { |
9025 | self.sibling = value.into(); |
9026 | self |
9027 | } |
9028 | /// Set the `stack_mode` field of this structure. |
9029 | #[must_use ] |
9030 | pub fn stack_mode<I>(mut self, value: I) -> Self where I: Into<Option<StackMode>> { |
9031 | self.stack_mode = value.into(); |
9032 | self |
9033 | } |
9034 | } |
9035 | impl ConfigureWindowAux { |
9036 | /// Construct from a [`ConfigureRequestEvent`]. |
9037 | /// |
9038 | /// This function construct a new `ConfigureWindowAux` instance by accepting all requested |
9039 | /// changes from a `ConfigureRequestEvent`. This function is useful for window managers that want |
9040 | /// to handle `ConfigureRequestEvent`s. |
9041 | pub fn from_configure_request(event: &ConfigureRequestEvent) -> Self { |
9042 | let mut result = Self::new(); |
9043 | let value_mask = u16::from(event.value_mask); |
9044 | if value_mask & u16::from(ConfigWindow::X) != 0 { |
9045 | result = result.x(i32::from(event.x)); |
9046 | } |
9047 | if value_mask & u16::from(ConfigWindow::Y) != 0 { |
9048 | result = result.y(i32::from(event.y)); |
9049 | } |
9050 | if value_mask & u16::from(ConfigWindow::WIDTH) != 0 { |
9051 | result = result.width(u32::from(event.width)); |
9052 | } |
9053 | if value_mask & u16::from(ConfigWindow::HEIGHT) != 0 { |
9054 | result = result.height(u32::from(event.height)); |
9055 | } |
9056 | if value_mask & u16::from(ConfigWindow::BORDER_WIDTH) != 0 { |
9057 | result = result.border_width(u32::from(event.border_width)); |
9058 | } |
9059 | if value_mask & u16::from(ConfigWindow::SIBLING) != 0 { |
9060 | result = result.sibling(event.sibling); |
9061 | } |
9062 | if value_mask & u16::from(ConfigWindow::STACK_MODE) != 0 { |
9063 | result = result.stack_mode(event.stack_mode); |
9064 | } |
9065 | result |
9066 | } |
9067 | } |
9068 | |
9069 | /// Opcode for the ConfigureWindow request |
9070 | pub const CONFIGURE_WINDOW_REQUEST: u8 = 12; |
9071 | /// Configures window attributes. |
9072 | /// |
9073 | /// Configures a window's size, position, border width and stacking order. |
9074 | /// |
9075 | /// # Fields |
9076 | /// |
9077 | /// * `window` - The window to configure. |
9078 | /// * `value_list` - New values, corresponding to the attributes in value_mask. The order has to |
9079 | /// correspond to the order of possible `value_mask` bits. See the example. |
9080 | /// |
9081 | /// # Errors |
9082 | /// |
9083 | /// * `Match` - You specified a Sibling without also specifying StackMode or the window is not |
9084 | /// actually a Sibling. |
9085 | /// * `Window` - The specified window does not exist. TODO: any other reason? |
9086 | /// * `Value` - TODO: reasons? |
9087 | /// |
9088 | /// # See |
9089 | /// |
9090 | /// * `MapNotify`: event |
9091 | /// * `Expose`: event |
9092 | /// |
9093 | /// # Example |
9094 | /// |
9095 | /// ```text |
9096 | /// /* |
9097 | /// * Configures the given window to the left upper corner |
9098 | /// * with a size of 1024x768 pixels. |
9099 | /// * |
9100 | /// */ |
9101 | /// void my_example(xcb_connection_t *c, xcb_window_t window) { |
9102 | /// uint16_t mask = 0; |
9103 | /// |
9104 | /// mask |= XCB_CONFIG_WINDOW_X; |
9105 | /// mask |= XCB_CONFIG_WINDOW_Y; |
9106 | /// mask |= XCB_CONFIG_WINDOW_WIDTH; |
9107 | /// mask |= XCB_CONFIG_WINDOW_HEIGHT; |
9108 | /// |
9109 | /// const uint32_t values[] = { |
9110 | /// 0, /* x */ |
9111 | /// 0, /* y */ |
9112 | /// 1024, /* width */ |
9113 | /// 768 /* height */ |
9114 | /// }; |
9115 | /// |
9116 | /// xcb_configure_window(c, window, mask, values); |
9117 | /// xcb_flush(c); |
9118 | /// } |
9119 | /// ``` |
9120 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
9121 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
9122 | pub struct ConfigureWindowRequest<'input> { |
9123 | pub window: Window, |
9124 | pub value_list: Cow<'input, ConfigureWindowAux>, |
9125 | } |
9126 | impl<'input> ConfigureWindowRequest<'input> { |
9127 | /// Serialize this request into bytes for the provided connection |
9128 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
9129 | let length_so_far = 0; |
9130 | let window_bytes = self.window.serialize(); |
9131 | let value_mask: u16 = self.value_list.switch_expr(); |
9132 | let value_mask_bytes = value_mask.serialize(); |
9133 | let mut request0 = vec![ |
9134 | CONFIGURE_WINDOW_REQUEST, |
9135 | 0, |
9136 | 0, |
9137 | 0, |
9138 | window_bytes[0], |
9139 | window_bytes[1], |
9140 | window_bytes[2], |
9141 | window_bytes[3], |
9142 | value_mask_bytes[0], |
9143 | value_mask_bytes[1], |
9144 | 0, |
9145 | 0, |
9146 | ]; |
9147 | let length_so_far = length_so_far + request0.len(); |
9148 | let value_list_bytes = self.value_list.serialize(u16::from(value_mask)); |
9149 | let length_so_far = length_so_far + value_list_bytes.len(); |
9150 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
9151 | let length_so_far = length_so_far + padding0.len(); |
9152 | assert_eq!(length_so_far % 4, 0); |
9153 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
9154 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
9155 | (vec![request0.into(), value_list_bytes.into(), padding0.into()], vec![]) |
9156 | } |
9157 | /// Parse this request given its header, its body, and any fds that go along with it |
9158 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
9159 | if header.major_opcode != CONFIGURE_WINDOW_REQUEST { |
9160 | return Err(ParseError::InvalidValue); |
9161 | } |
9162 | let remaining = &[header.minor_opcode]; |
9163 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
9164 | let _ = remaining; |
9165 | let (window, remaining) = Window::try_parse(value)?; |
9166 | let (value_mask, remaining) = u16::try_parse(remaining)?; |
9167 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
9168 | let (value_list, remaining) = ConfigureWindowAux::try_parse(remaining, u16::from(value_mask))?; |
9169 | let _ = remaining; |
9170 | Ok(ConfigureWindowRequest { |
9171 | window, |
9172 | value_list: Cow::Owned(value_list), |
9173 | }) |
9174 | } |
9175 | /// Clone all borrowed data in this ConfigureWindowRequest. |
9176 | pub fn into_owned(self) -> ConfigureWindowRequest<'static> { |
9177 | ConfigureWindowRequest { |
9178 | window: self.window, |
9179 | value_list: Cow::Owned(self.value_list.into_owned()), |
9180 | } |
9181 | } |
9182 | } |
9183 | impl<'input> Request for ConfigureWindowRequest<'input> { |
9184 | const EXTENSION_NAME: Option<&'static str> = None; |
9185 | |
9186 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
9187 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
9188 | // Flatten the buffers into a single vector |
9189 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
9190 | (buf, fds) |
9191 | } |
9192 | } |
9193 | impl<'input> crate::x11_utils::VoidRequest for ConfigureWindowRequest<'input> { |
9194 | } |
9195 | |
9196 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
9197 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
9198 | pub struct Circulate(u8); |
9199 | impl Circulate { |
9200 | pub const RAISE_LOWEST: Self = Self(0); |
9201 | pub const LOWER_HIGHEST: Self = Self(1); |
9202 | } |
9203 | impl From<Circulate> for u8 { |
9204 | #[inline ] |
9205 | fn from(input: Circulate) -> Self { |
9206 | input.0 |
9207 | } |
9208 | } |
9209 | impl From<Circulate> for Option<u8> { |
9210 | #[inline ] |
9211 | fn from(input: Circulate) -> Self { |
9212 | Some(input.0) |
9213 | } |
9214 | } |
9215 | impl From<Circulate> for u16 { |
9216 | #[inline ] |
9217 | fn from(input: Circulate) -> Self { |
9218 | u16::from(input.0) |
9219 | } |
9220 | } |
9221 | impl From<Circulate> for Option<u16> { |
9222 | #[inline ] |
9223 | fn from(input: Circulate) -> Self { |
9224 | Some(u16::from(input.0)) |
9225 | } |
9226 | } |
9227 | impl From<Circulate> for u32 { |
9228 | #[inline ] |
9229 | fn from(input: Circulate) -> Self { |
9230 | u32::from(input.0) |
9231 | } |
9232 | } |
9233 | impl From<Circulate> for Option<u32> { |
9234 | #[inline ] |
9235 | fn from(input: Circulate) -> Self { |
9236 | Some(u32::from(input.0)) |
9237 | } |
9238 | } |
9239 | impl From<u8> for Circulate { |
9240 | #[inline ] |
9241 | fn from(value: u8) -> Self { |
9242 | Self(value) |
9243 | } |
9244 | } |
9245 | impl core::fmt::Debug for Circulate { |
9246 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
9247 | let variants: [(u32, &str, &str); 2] = [ |
9248 | (Self::RAISE_LOWEST.0.into(), "RAISE_LOWEST" , "RaiseLowest" ), |
9249 | (Self::LOWER_HIGHEST.0.into(), "LOWER_HIGHEST" , "LowerHighest" ), |
9250 | ]; |
9251 | pretty_print_enum(fmt, self.0.into(), &variants) |
9252 | } |
9253 | } |
9254 | |
9255 | /// Opcode for the CirculateWindow request |
9256 | pub const CIRCULATE_WINDOW_REQUEST: u8 = 13; |
9257 | /// Change window stacking order. |
9258 | /// |
9259 | /// If `direction` is `XCB_CIRCULATE_RAISE_LOWEST`, the lowest mapped child (if |
9260 | /// any) will be raised to the top of the stack. |
9261 | /// |
9262 | /// If `direction` is `XCB_CIRCULATE_LOWER_HIGHEST`, the highest mapped child will |
9263 | /// be lowered to the bottom of the stack. |
9264 | /// |
9265 | /// # Fields |
9266 | /// |
9267 | /// * `direction` - |
9268 | /// * `window` - The window to raise/lower (depending on `direction`). |
9269 | /// |
9270 | /// # Errors |
9271 | /// |
9272 | /// * `Window` - The specified `window` does not exist. |
9273 | /// * `Value` - The specified `direction` is invalid. |
9274 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
9275 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
9276 | pub struct CirculateWindowRequest { |
9277 | pub direction: Circulate, |
9278 | pub window: Window, |
9279 | } |
9280 | impl CirculateWindowRequest { |
9281 | /// Serialize this request into bytes for the provided connection |
9282 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
9283 | let length_so_far = 0; |
9284 | let direction_bytes = u8::from(self.direction).serialize(); |
9285 | let window_bytes = self.window.serialize(); |
9286 | let mut request0 = vec![ |
9287 | CIRCULATE_WINDOW_REQUEST, |
9288 | direction_bytes[0], |
9289 | 0, |
9290 | 0, |
9291 | window_bytes[0], |
9292 | window_bytes[1], |
9293 | window_bytes[2], |
9294 | window_bytes[3], |
9295 | ]; |
9296 | let length_so_far = length_so_far + request0.len(); |
9297 | assert_eq!(length_so_far % 4, 0); |
9298 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
9299 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
9300 | (vec![request0.into()], vec![]) |
9301 | } |
9302 | /// Parse this request given its header, its body, and any fds that go along with it |
9303 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
9304 | if header.major_opcode != CIRCULATE_WINDOW_REQUEST { |
9305 | return Err(ParseError::InvalidValue); |
9306 | } |
9307 | let remaining = &[header.minor_opcode]; |
9308 | let (direction, remaining) = u8::try_parse(remaining)?; |
9309 | let direction = direction.into(); |
9310 | let _ = remaining; |
9311 | let (window, remaining) = Window::try_parse(value)?; |
9312 | let _ = remaining; |
9313 | Ok(CirculateWindowRequest { |
9314 | direction, |
9315 | window, |
9316 | }) |
9317 | } |
9318 | } |
9319 | impl Request for CirculateWindowRequest { |
9320 | const EXTENSION_NAME: Option<&'static str> = None; |
9321 | |
9322 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
9323 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
9324 | // Flatten the buffers into a single vector |
9325 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
9326 | (buf, fds) |
9327 | } |
9328 | } |
9329 | impl crate::x11_utils::VoidRequest for CirculateWindowRequest { |
9330 | } |
9331 | |
9332 | /// Opcode for the GetGeometry request |
9333 | pub const GET_GEOMETRY_REQUEST: u8 = 14; |
9334 | /// Get current window geometry. |
9335 | /// |
9336 | /// Gets the current geometry of the specified drawable (either `Window` or `Pixmap`). |
9337 | /// |
9338 | /// # Fields |
9339 | /// |
9340 | /// * `drawable` - The drawable (`Window` or `Pixmap`) of which the geometry will be received. |
9341 | /// |
9342 | /// # Errors |
9343 | /// |
9344 | /// * `Drawable` - TODO: reasons? |
9345 | /// * `Window` - TODO: reasons? |
9346 | /// |
9347 | /// # See |
9348 | /// |
9349 | /// * `xwininfo`: program |
9350 | /// |
9351 | /// # Example |
9352 | /// |
9353 | /// ```text |
9354 | /// /* |
9355 | /// * Displays the x and y position of the given window. |
9356 | /// * |
9357 | /// */ |
9358 | /// void my_example(xcb_connection_t *c, xcb_window_t window) { |
9359 | /// xcb_get_geometry_cookie_t cookie; |
9360 | /// xcb_get_geometry_reply_t *reply; |
9361 | /// |
9362 | /// cookie = xcb_get_geometry(c, window); |
9363 | /// /* ... do other work here if possible ... */ |
9364 | /// if ((reply = xcb_get_geometry_reply(c, cookie, NULL))) { |
9365 | /// printf("This window is at %d, %d\\n", reply->x, reply->y); |
9366 | /// } |
9367 | /// free(reply); |
9368 | /// } |
9369 | /// ``` |
9370 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
9371 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
9372 | pub struct GetGeometryRequest { |
9373 | pub drawable: Drawable, |
9374 | } |
9375 | impl GetGeometryRequest { |
9376 | /// Serialize this request into bytes for the provided connection |
9377 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
9378 | let length_so_far = 0; |
9379 | let drawable_bytes = self.drawable.serialize(); |
9380 | let mut request0 = vec![ |
9381 | GET_GEOMETRY_REQUEST, |
9382 | 0, |
9383 | 0, |
9384 | 0, |
9385 | drawable_bytes[0], |
9386 | drawable_bytes[1], |
9387 | drawable_bytes[2], |
9388 | drawable_bytes[3], |
9389 | ]; |
9390 | let length_so_far = length_so_far + request0.len(); |
9391 | assert_eq!(length_so_far % 4, 0); |
9392 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
9393 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
9394 | (vec![request0.into()], vec![]) |
9395 | } |
9396 | /// Parse this request given its header, its body, and any fds that go along with it |
9397 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
9398 | if header.major_opcode != GET_GEOMETRY_REQUEST { |
9399 | return Err(ParseError::InvalidValue); |
9400 | } |
9401 | let remaining = &[header.minor_opcode]; |
9402 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
9403 | let _ = remaining; |
9404 | let (drawable, remaining) = Drawable::try_parse(value)?; |
9405 | let _ = remaining; |
9406 | Ok(GetGeometryRequest { |
9407 | drawable, |
9408 | }) |
9409 | } |
9410 | } |
9411 | impl Request for GetGeometryRequest { |
9412 | const EXTENSION_NAME: Option<&'static str> = None; |
9413 | |
9414 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
9415 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
9416 | // Flatten the buffers into a single vector |
9417 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
9418 | (buf, fds) |
9419 | } |
9420 | } |
9421 | impl crate::x11_utils::ReplyRequest for GetGeometryRequest { |
9422 | type Reply = GetGeometryReply; |
9423 | } |
9424 | |
9425 | /// # Fields |
9426 | /// |
9427 | /// * `root` - Root window of the screen containing `drawable`. |
9428 | /// * `x` - The X coordinate of `drawable`. If `drawable` is a window, the coordinate |
9429 | /// specifies the upper-left outer corner relative to its parent's origin. If |
9430 | /// `drawable` is a pixmap, the X coordinate is always 0. |
9431 | /// * `y` - The Y coordinate of `drawable`. If `drawable` is a window, the coordinate |
9432 | /// specifies the upper-left outer corner relative to its parent's origin. If |
9433 | /// `drawable` is a pixmap, the Y coordinate is always 0. |
9434 | /// * `width` - The width of `drawable`. |
9435 | /// * `height` - The height of `drawable`. |
9436 | /// * `border_width` - The border width (in pixels). |
9437 | /// * `depth` - The depth of the drawable (bits per pixel for the object). |
9438 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
9439 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
9440 | pub struct GetGeometryReply { |
9441 | pub depth: u8, |
9442 | pub sequence: u16, |
9443 | pub length: u32, |
9444 | pub root: Window, |
9445 | pub x: i16, |
9446 | pub y: i16, |
9447 | pub width: u16, |
9448 | pub height: u16, |
9449 | pub border_width: u16, |
9450 | } |
9451 | impl TryParse for GetGeometryReply { |
9452 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
9453 | let remaining: &[u8] = initial_value; |
9454 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
9455 | let (depth: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
9456 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
9457 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
9458 | let (root: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
9459 | let (x: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
9460 | let (y: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
9461 | let (width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
9462 | let (height: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
9463 | let (border_width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
9464 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
9465 | if response_type != 1 { |
9466 | return Err(ParseError::InvalidValue); |
9467 | } |
9468 | let result: GetGeometryReply = GetGeometryReply { depth, sequence, length, root, x, y, width, height, border_width }; |
9469 | let _ = remaining; |
9470 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
9471 | .ok_or(err:ParseError::InsufficientData)?; |
9472 | Ok((result, remaining)) |
9473 | } |
9474 | } |
9475 | impl Serialize for GetGeometryReply { |
9476 | type Bytes = [u8; 24]; |
9477 | fn serialize(&self) -> [u8; 24] { |
9478 | let response_type_bytes = &[1]; |
9479 | let depth_bytes = self.depth.serialize(); |
9480 | let sequence_bytes = self.sequence.serialize(); |
9481 | let length_bytes = self.length.serialize(); |
9482 | let root_bytes = self.root.serialize(); |
9483 | let x_bytes = self.x.serialize(); |
9484 | let y_bytes = self.y.serialize(); |
9485 | let width_bytes = self.width.serialize(); |
9486 | let height_bytes = self.height.serialize(); |
9487 | let border_width_bytes = self.border_width.serialize(); |
9488 | [ |
9489 | response_type_bytes[0], |
9490 | depth_bytes[0], |
9491 | sequence_bytes[0], |
9492 | sequence_bytes[1], |
9493 | length_bytes[0], |
9494 | length_bytes[1], |
9495 | length_bytes[2], |
9496 | length_bytes[3], |
9497 | root_bytes[0], |
9498 | root_bytes[1], |
9499 | root_bytes[2], |
9500 | root_bytes[3], |
9501 | x_bytes[0], |
9502 | x_bytes[1], |
9503 | y_bytes[0], |
9504 | y_bytes[1], |
9505 | width_bytes[0], |
9506 | width_bytes[1], |
9507 | height_bytes[0], |
9508 | height_bytes[1], |
9509 | border_width_bytes[0], |
9510 | border_width_bytes[1], |
9511 | 0, |
9512 | 0, |
9513 | ] |
9514 | } |
9515 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
9516 | bytes.reserve(24); |
9517 | let response_type_bytes = &[1]; |
9518 | bytes.push(response_type_bytes[0]); |
9519 | self.depth.serialize_into(bytes); |
9520 | self.sequence.serialize_into(bytes); |
9521 | self.length.serialize_into(bytes); |
9522 | self.root.serialize_into(bytes); |
9523 | self.x.serialize_into(bytes); |
9524 | self.y.serialize_into(bytes); |
9525 | self.width.serialize_into(bytes); |
9526 | self.height.serialize_into(bytes); |
9527 | self.border_width.serialize_into(bytes); |
9528 | bytes.extend_from_slice(&[0; 2]); |
9529 | } |
9530 | } |
9531 | |
9532 | /// Opcode for the QueryTree request |
9533 | pub const QUERY_TREE_REQUEST: u8 = 15; |
9534 | /// query the window tree. |
9535 | /// |
9536 | /// Gets the root window ID, parent window ID and list of children windows for the |
9537 | /// specified `window`. The children are listed in bottom-to-top stacking order. |
9538 | /// |
9539 | /// # Fields |
9540 | /// |
9541 | /// * `window` - The `window` to query. |
9542 | /// |
9543 | /// # See |
9544 | /// |
9545 | /// * `xwininfo`: program |
9546 | /// |
9547 | /// # Example |
9548 | /// |
9549 | /// ```text |
9550 | /// /* |
9551 | /// * Displays the root, parent and children of the specified window. |
9552 | /// * |
9553 | /// */ |
9554 | /// void my_example(xcb_connection_t *conn, xcb_window_t window) { |
9555 | /// xcb_query_tree_cookie_t cookie; |
9556 | /// xcb_query_tree_reply_t *reply; |
9557 | /// |
9558 | /// cookie = xcb_query_tree(conn, window); |
9559 | /// if ((reply = xcb_query_tree_reply(conn, cookie, NULL))) { |
9560 | /// printf("root = 0x%08x\\n", reply->root); |
9561 | /// printf("parent = 0x%08x\\n", reply->parent); |
9562 | /// |
9563 | /// xcb_window_t *children = xcb_query_tree_children(reply); |
9564 | /// for (int i = 0; i < xcb_query_tree_children_length(reply); i++) |
9565 | /// printf("child window = 0x%08x\\n", children[i]); |
9566 | /// |
9567 | /// free(reply); |
9568 | /// } |
9569 | /// } |
9570 | /// ``` |
9571 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
9572 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
9573 | pub struct QueryTreeRequest { |
9574 | pub window: Window, |
9575 | } |
9576 | impl QueryTreeRequest { |
9577 | /// Serialize this request into bytes for the provided connection |
9578 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
9579 | let length_so_far = 0; |
9580 | let window_bytes = self.window.serialize(); |
9581 | let mut request0 = vec![ |
9582 | QUERY_TREE_REQUEST, |
9583 | 0, |
9584 | 0, |
9585 | 0, |
9586 | window_bytes[0], |
9587 | window_bytes[1], |
9588 | window_bytes[2], |
9589 | window_bytes[3], |
9590 | ]; |
9591 | let length_so_far = length_so_far + request0.len(); |
9592 | assert_eq!(length_so_far % 4, 0); |
9593 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
9594 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
9595 | (vec![request0.into()], vec![]) |
9596 | } |
9597 | /// Parse this request given its header, its body, and any fds that go along with it |
9598 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
9599 | if header.major_opcode != QUERY_TREE_REQUEST { |
9600 | return Err(ParseError::InvalidValue); |
9601 | } |
9602 | let remaining = &[header.minor_opcode]; |
9603 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
9604 | let _ = remaining; |
9605 | let (window, remaining) = Window::try_parse(value)?; |
9606 | let _ = remaining; |
9607 | Ok(QueryTreeRequest { |
9608 | window, |
9609 | }) |
9610 | } |
9611 | } |
9612 | impl Request for QueryTreeRequest { |
9613 | const EXTENSION_NAME: Option<&'static str> = None; |
9614 | |
9615 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
9616 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
9617 | // Flatten the buffers into a single vector |
9618 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
9619 | (buf, fds) |
9620 | } |
9621 | } |
9622 | impl crate::x11_utils::ReplyRequest for QueryTreeRequest { |
9623 | type Reply = QueryTreeReply; |
9624 | } |
9625 | |
9626 | /// # Fields |
9627 | /// |
9628 | /// * `root` - The root window of `window`. |
9629 | /// * `parent` - The parent window of `window`. |
9630 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
9631 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
9632 | pub struct QueryTreeReply { |
9633 | pub sequence: u16, |
9634 | pub length: u32, |
9635 | pub root: Window, |
9636 | pub parent: Window, |
9637 | pub children: Vec<Window>, |
9638 | } |
9639 | impl TryParse for QueryTreeReply { |
9640 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
9641 | let remaining: &[u8] = initial_value; |
9642 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
9643 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
9644 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
9645 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
9646 | let (root: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
9647 | let (parent: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
9648 | let (children_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
9649 | let remaining: &[u8] = remaining.get(14..).ok_or(err:ParseError::InsufficientData)?; |
9650 | let (children: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<Window>(data:remaining, list_length:children_len.try_to_usize()?)?; |
9651 | if response_type != 1 { |
9652 | return Err(ParseError::InvalidValue); |
9653 | } |
9654 | let result: QueryTreeReply = QueryTreeReply { sequence, length, root, parent, children }; |
9655 | let _ = remaining; |
9656 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
9657 | .ok_or(err:ParseError::InsufficientData)?; |
9658 | Ok((result, remaining)) |
9659 | } |
9660 | } |
9661 | impl Serialize for QueryTreeReply { |
9662 | type Bytes = Vec<u8>; |
9663 | fn serialize(&self) -> Vec<u8> { |
9664 | let mut result: Vec = Vec::new(); |
9665 | self.serialize_into(&mut result); |
9666 | result |
9667 | } |
9668 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
9669 | bytes.reserve(additional:32); |
9670 | let response_type_bytes: &[u8; 1] = &[1]; |
9671 | bytes.push(response_type_bytes[0]); |
9672 | bytes.extend_from_slice(&[0; 1]); |
9673 | self.sequence.serialize_into(bytes); |
9674 | self.length.serialize_into(bytes); |
9675 | self.root.serialize_into(bytes); |
9676 | self.parent.serialize_into(bytes); |
9677 | let children_len: u16 = u16::try_from(self.children.len()).expect(msg:"`children` has too many elements" ); |
9678 | children_len.serialize_into(bytes); |
9679 | bytes.extend_from_slice(&[0; 14]); |
9680 | self.children.serialize_into(bytes); |
9681 | } |
9682 | } |
9683 | impl QueryTreeReply { |
9684 | /// Get the value of the `children_len` field. |
9685 | /// |
9686 | /// The `children_len` field is used as the length field of the `children` field. |
9687 | /// This function computes the field's value again based on the length of the list. |
9688 | /// |
9689 | /// # Panics |
9690 | /// |
9691 | /// Panics if the value cannot be represented in the target type. This |
9692 | /// cannot happen with values of the struct received from the X11 server. |
9693 | pub fn children_len(&self) -> u16 { |
9694 | self.children.len() |
9695 | .try_into().unwrap() |
9696 | } |
9697 | } |
9698 | |
9699 | /// Opcode for the InternAtom request |
9700 | pub const INTERN_ATOM_REQUEST: u8 = 16; |
9701 | /// Get atom identifier by name. |
9702 | /// |
9703 | /// Retrieves the identifier (xcb_atom_t TODO) for the atom with the specified |
9704 | /// name. Atoms are used in protocols like EWMH, for example to store window titles |
9705 | /// (`_NET_WM_NAME` atom) as property of a window. |
9706 | /// |
9707 | /// If `only_if_exists` is 0, the atom will be created if it does not already exist. |
9708 | /// If `only_if_exists` is 1, `XCB_ATOM_NONE` will be returned if the atom does |
9709 | /// not yet exist. |
9710 | /// |
9711 | /// # Fields |
9712 | /// |
9713 | /// * `name` - The name of the atom. |
9714 | /// * `only_if_exists` - Return a valid atom id only if the atom already exists. |
9715 | /// |
9716 | /// # Errors |
9717 | /// |
9718 | /// * `Alloc` - TODO: reasons? |
9719 | /// * `Value` - A value other than 0 or 1 was specified for `only_if_exists`. |
9720 | /// |
9721 | /// # See |
9722 | /// |
9723 | /// * `xlsatoms`: program |
9724 | /// * `GetAtomName`: request |
9725 | /// |
9726 | /// # Example |
9727 | /// |
9728 | /// ```text |
9729 | /// /* |
9730 | /// * Resolves the _NET_WM_NAME atom. |
9731 | /// * |
9732 | /// */ |
9733 | /// void my_example(xcb_connection_t *c) { |
9734 | /// xcb_intern_atom_cookie_t cookie; |
9735 | /// xcb_intern_atom_reply_t *reply; |
9736 | /// |
9737 | /// cookie = xcb_intern_atom(c, 0, strlen("_NET_WM_NAME"), "_NET_WM_NAME"); |
9738 | /// /* ... do other work here if possible ... */ |
9739 | /// if ((reply = xcb_intern_atom_reply(c, cookie, NULL))) { |
9740 | /// printf("The _NET_WM_NAME atom has ID %u\n", reply->atom); |
9741 | /// free(reply); |
9742 | /// } |
9743 | /// } |
9744 | /// ``` |
9745 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
9746 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
9747 | pub struct InternAtomRequest<'input> { |
9748 | pub only_if_exists: bool, |
9749 | pub name: Cow<'input, [u8]>, |
9750 | } |
9751 | impl<'input> InternAtomRequest<'input> { |
9752 | /// Serialize this request into bytes for the provided connection |
9753 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
9754 | let length_so_far = 0; |
9755 | let only_if_exists_bytes = self.only_if_exists.serialize(); |
9756 | let name_len = u16::try_from(self.name.len()).expect("`name` has too many elements" ); |
9757 | let name_len_bytes = name_len.serialize(); |
9758 | let mut request0 = vec![ |
9759 | INTERN_ATOM_REQUEST, |
9760 | only_if_exists_bytes[0], |
9761 | 0, |
9762 | 0, |
9763 | name_len_bytes[0], |
9764 | name_len_bytes[1], |
9765 | 0, |
9766 | 0, |
9767 | ]; |
9768 | let length_so_far = length_so_far + request0.len(); |
9769 | let length_so_far = length_so_far + self.name.len(); |
9770 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
9771 | let length_so_far = length_so_far + padding0.len(); |
9772 | assert_eq!(length_so_far % 4, 0); |
9773 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
9774 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
9775 | (vec![request0.into(), self.name, padding0.into()], vec![]) |
9776 | } |
9777 | /// Parse this request given its header, its body, and any fds that go along with it |
9778 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
9779 | if header.major_opcode != INTERN_ATOM_REQUEST { |
9780 | return Err(ParseError::InvalidValue); |
9781 | } |
9782 | let remaining = &[header.minor_opcode]; |
9783 | let (only_if_exists, remaining) = bool::try_parse(remaining)?; |
9784 | let _ = remaining; |
9785 | let (name_len, remaining) = u16::try_parse(value)?; |
9786 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
9787 | let (name, remaining) = crate::x11_utils::parse_u8_list(remaining, name_len.try_to_usize()?)?; |
9788 | let _ = remaining; |
9789 | Ok(InternAtomRequest { |
9790 | only_if_exists, |
9791 | name: Cow::Borrowed(name), |
9792 | }) |
9793 | } |
9794 | /// Clone all borrowed data in this InternAtomRequest. |
9795 | pub fn into_owned(self) -> InternAtomRequest<'static> { |
9796 | InternAtomRequest { |
9797 | only_if_exists: self.only_if_exists, |
9798 | name: Cow::Owned(self.name.into_owned()), |
9799 | } |
9800 | } |
9801 | } |
9802 | impl<'input> Request for InternAtomRequest<'input> { |
9803 | const EXTENSION_NAME: Option<&'static str> = None; |
9804 | |
9805 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
9806 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
9807 | // Flatten the buffers into a single vector |
9808 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
9809 | (buf, fds) |
9810 | } |
9811 | } |
9812 | impl<'input> crate::x11_utils::ReplyRequest for InternAtomRequest<'input> { |
9813 | type Reply = InternAtomReply; |
9814 | } |
9815 | |
9816 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
9817 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
9818 | pub struct InternAtomReply { |
9819 | pub sequence: u16, |
9820 | pub length: u32, |
9821 | pub atom: Atom, |
9822 | } |
9823 | impl TryParse for InternAtomReply { |
9824 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
9825 | let remaining: &[u8] = initial_value; |
9826 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
9827 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
9828 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
9829 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
9830 | let (atom: u32, remaining: &[u8]) = Atom::try_parse(remaining)?; |
9831 | if response_type != 1 { |
9832 | return Err(ParseError::InvalidValue); |
9833 | } |
9834 | let result: InternAtomReply = InternAtomReply { sequence, length, atom }; |
9835 | let _ = remaining; |
9836 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
9837 | .ok_or(err:ParseError::InsufficientData)?; |
9838 | Ok((result, remaining)) |
9839 | } |
9840 | } |
9841 | impl Serialize for InternAtomReply { |
9842 | type Bytes = [u8; 12]; |
9843 | fn serialize(&self) -> [u8; 12] { |
9844 | let response_type_bytes = &[1]; |
9845 | let sequence_bytes = self.sequence.serialize(); |
9846 | let length_bytes = self.length.serialize(); |
9847 | let atom_bytes = self.atom.serialize(); |
9848 | [ |
9849 | response_type_bytes[0], |
9850 | 0, |
9851 | sequence_bytes[0], |
9852 | sequence_bytes[1], |
9853 | length_bytes[0], |
9854 | length_bytes[1], |
9855 | length_bytes[2], |
9856 | length_bytes[3], |
9857 | atom_bytes[0], |
9858 | atom_bytes[1], |
9859 | atom_bytes[2], |
9860 | atom_bytes[3], |
9861 | ] |
9862 | } |
9863 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
9864 | bytes.reserve(12); |
9865 | let response_type_bytes = &[1]; |
9866 | bytes.push(response_type_bytes[0]); |
9867 | bytes.extend_from_slice(&[0; 1]); |
9868 | self.sequence.serialize_into(bytes); |
9869 | self.length.serialize_into(bytes); |
9870 | self.atom.serialize_into(bytes); |
9871 | } |
9872 | } |
9873 | |
9874 | /// Opcode for the GetAtomName request |
9875 | pub const GET_ATOM_NAME_REQUEST: u8 = 17; |
9876 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
9877 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
9878 | pub struct GetAtomNameRequest { |
9879 | pub atom: Atom, |
9880 | } |
9881 | impl GetAtomNameRequest { |
9882 | /// Serialize this request into bytes for the provided connection |
9883 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
9884 | let length_so_far = 0; |
9885 | let atom_bytes = self.atom.serialize(); |
9886 | let mut request0 = vec![ |
9887 | GET_ATOM_NAME_REQUEST, |
9888 | 0, |
9889 | 0, |
9890 | 0, |
9891 | atom_bytes[0], |
9892 | atom_bytes[1], |
9893 | atom_bytes[2], |
9894 | atom_bytes[3], |
9895 | ]; |
9896 | let length_so_far = length_so_far + request0.len(); |
9897 | assert_eq!(length_so_far % 4, 0); |
9898 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
9899 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
9900 | (vec![request0.into()], vec![]) |
9901 | } |
9902 | /// Parse this request given its header, its body, and any fds that go along with it |
9903 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
9904 | if header.major_opcode != GET_ATOM_NAME_REQUEST { |
9905 | return Err(ParseError::InvalidValue); |
9906 | } |
9907 | let remaining = &[header.minor_opcode]; |
9908 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
9909 | let _ = remaining; |
9910 | let (atom, remaining) = Atom::try_parse(value)?; |
9911 | let _ = remaining; |
9912 | Ok(GetAtomNameRequest { |
9913 | atom, |
9914 | }) |
9915 | } |
9916 | } |
9917 | impl Request for GetAtomNameRequest { |
9918 | const EXTENSION_NAME: Option<&'static str> = None; |
9919 | |
9920 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
9921 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
9922 | // Flatten the buffers into a single vector |
9923 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
9924 | (buf, fds) |
9925 | } |
9926 | } |
9927 | impl crate::x11_utils::ReplyRequest for GetAtomNameRequest { |
9928 | type Reply = GetAtomNameReply; |
9929 | } |
9930 | |
9931 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
9932 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
9933 | pub struct GetAtomNameReply { |
9934 | pub sequence: u16, |
9935 | pub length: u32, |
9936 | pub name: Vec<u8>, |
9937 | } |
9938 | impl TryParse for GetAtomNameReply { |
9939 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
9940 | let remaining: &[u8] = initial_value; |
9941 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
9942 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
9943 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
9944 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
9945 | let (name_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
9946 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
9947 | let (name: &[u8], remaining: &[u8]) = crate::x11_utils::parse_u8_list(data:remaining, list_length:name_len.try_to_usize()?)?; |
9948 | let name: Vec = name.to_vec(); |
9949 | if response_type != 1 { |
9950 | return Err(ParseError::InvalidValue); |
9951 | } |
9952 | let result: GetAtomNameReply = GetAtomNameReply { sequence, length, name }; |
9953 | let _ = remaining; |
9954 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
9955 | .ok_or(err:ParseError::InsufficientData)?; |
9956 | Ok((result, remaining)) |
9957 | } |
9958 | } |
9959 | impl Serialize for GetAtomNameReply { |
9960 | type Bytes = Vec<u8>; |
9961 | fn serialize(&self) -> Vec<u8> { |
9962 | let mut result: Vec = Vec::new(); |
9963 | self.serialize_into(&mut result); |
9964 | result |
9965 | } |
9966 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
9967 | bytes.reserve(additional:32); |
9968 | let response_type_bytes: &[u8; 1] = &[1]; |
9969 | bytes.push(response_type_bytes[0]); |
9970 | bytes.extend_from_slice(&[0; 1]); |
9971 | self.sequence.serialize_into(bytes); |
9972 | self.length.serialize_into(bytes); |
9973 | let name_len: u16 = u16::try_from(self.name.len()).expect(msg:"`name` has too many elements" ); |
9974 | name_len.serialize_into(bytes); |
9975 | bytes.extend_from_slice(&[0; 22]); |
9976 | bytes.extend_from_slice(&self.name); |
9977 | } |
9978 | } |
9979 | impl GetAtomNameReply { |
9980 | /// Get the value of the `name_len` field. |
9981 | /// |
9982 | /// The `name_len` field is used as the length field of the `name` field. |
9983 | /// This function computes the field's value again based on the length of the list. |
9984 | /// |
9985 | /// # Panics |
9986 | /// |
9987 | /// Panics if the value cannot be represented in the target type. This |
9988 | /// cannot happen with values of the struct received from the X11 server. |
9989 | pub fn name_len(&self) -> u16 { |
9990 | self.name.len() |
9991 | .try_into().unwrap() |
9992 | } |
9993 | } |
9994 | |
9995 | /// # Fields |
9996 | /// |
9997 | /// * `Replace` - Discard the previous property value and store the new data. |
9998 | /// * `Prepend` - Insert the new data before the beginning of existing data. The `format` must |
9999 | /// match existing property value. If the property is undefined, it is treated as |
10000 | /// defined with the correct type and format with zero-length data. |
10001 | /// * `Append` - Insert the new data after the beginning of existing data. The `format` must |
10002 | /// match existing property value. If the property is undefined, it is treated as |
10003 | /// defined with the correct type and format with zero-length data. |
10004 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
10005 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
10006 | pub struct PropMode(u8); |
10007 | impl PropMode { |
10008 | pub const REPLACE: Self = Self(0); |
10009 | pub const PREPEND: Self = Self(1); |
10010 | pub const APPEND: Self = Self(2); |
10011 | } |
10012 | impl From<PropMode> for u8 { |
10013 | #[inline ] |
10014 | fn from(input: PropMode) -> Self { |
10015 | input.0 |
10016 | } |
10017 | } |
10018 | impl From<PropMode> for Option<u8> { |
10019 | #[inline ] |
10020 | fn from(input: PropMode) -> Self { |
10021 | Some(input.0) |
10022 | } |
10023 | } |
10024 | impl From<PropMode> for u16 { |
10025 | #[inline ] |
10026 | fn from(input: PropMode) -> Self { |
10027 | u16::from(input.0) |
10028 | } |
10029 | } |
10030 | impl From<PropMode> for Option<u16> { |
10031 | #[inline ] |
10032 | fn from(input: PropMode) -> Self { |
10033 | Some(u16::from(input.0)) |
10034 | } |
10035 | } |
10036 | impl From<PropMode> for u32 { |
10037 | #[inline ] |
10038 | fn from(input: PropMode) -> Self { |
10039 | u32::from(input.0) |
10040 | } |
10041 | } |
10042 | impl From<PropMode> for Option<u32> { |
10043 | #[inline ] |
10044 | fn from(input: PropMode) -> Self { |
10045 | Some(u32::from(input.0)) |
10046 | } |
10047 | } |
10048 | impl From<u8> for PropMode { |
10049 | #[inline ] |
10050 | fn from(value: u8) -> Self { |
10051 | Self(value) |
10052 | } |
10053 | } |
10054 | impl core::fmt::Debug for PropMode { |
10055 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
10056 | let variants: [(u32, &str, &str); 3] = [ |
10057 | (Self::REPLACE.0.into(), "REPLACE" , "Replace" ), |
10058 | (Self::PREPEND.0.into(), "PREPEND" , "Prepend" ), |
10059 | (Self::APPEND.0.into(), "APPEND" , "Append" ), |
10060 | ]; |
10061 | pretty_print_enum(fmt, self.0.into(), &variants) |
10062 | } |
10063 | } |
10064 | |
10065 | /// Opcode for the ChangeProperty request |
10066 | pub const CHANGE_PROPERTY_REQUEST: u8 = 18; |
10067 | /// Changes a window property. |
10068 | /// |
10069 | /// Sets or updates a property on the specified `window`. Properties are for |
10070 | /// example the window title (`WM_NAME`) or its minimum size (`WM_NORMAL_HINTS`). |
10071 | /// Protocols such as EWMH also use properties - for example EWMH defines the |
10072 | /// window title, encoded as UTF-8 string, in the `_NET_WM_NAME` property. |
10073 | /// |
10074 | /// # Fields |
10075 | /// |
10076 | /// * `window` - The window whose property you want to change. |
10077 | /// * `mode` - |
10078 | /// * `property` - The property you want to change (an atom). |
10079 | /// * `type` - The type of the property you want to change (an atom). |
10080 | /// * `format` - Specifies whether the data should be viewed as a list of 8-bit, 16-bit or |
10081 | /// 32-bit quantities. Possible values are 8, 16 and 32. This information allows |
10082 | /// the X server to correctly perform byte-swap operations as necessary. |
10083 | /// * `data_len` - Specifies the number of elements (see `format`). |
10084 | /// * `data` - The property data. |
10085 | /// |
10086 | /// # Errors |
10087 | /// |
10088 | /// * `Match` - TODO: reasons? |
10089 | /// * `Value` - TODO: reasons? |
10090 | /// * `Window` - The specified `window` does not exist. |
10091 | /// * `Atom` - `property` or `type` do not refer to a valid atom. |
10092 | /// * `Alloc` - The X server could not store the property (no memory?). |
10093 | /// |
10094 | /// # See |
10095 | /// |
10096 | /// * `InternAtom`: request |
10097 | /// * `xprop`: program |
10098 | /// |
10099 | /// # Example |
10100 | /// |
10101 | /// ```text |
10102 | /// /* |
10103 | /// * Sets the WM_NAME property of the window to "XCB Example". |
10104 | /// * |
10105 | /// */ |
10106 | /// void my_example(xcb_connection_t *conn, xcb_window_t window) { |
10107 | /// xcb_change_property(conn, |
10108 | /// XCB_PROP_MODE_REPLACE, |
10109 | /// window, |
10110 | /// XCB_ATOM_WM_NAME, |
10111 | /// XCB_ATOM_STRING, |
10112 | /// 8, |
10113 | /// strlen("XCB Example"), |
10114 | /// "XCB Example"); |
10115 | /// xcb_flush(conn); |
10116 | /// } |
10117 | /// ``` |
10118 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
10119 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
10120 | pub struct ChangePropertyRequest<'input> { |
10121 | pub mode: PropMode, |
10122 | pub window: Window, |
10123 | pub property: Atom, |
10124 | pub type_: Atom, |
10125 | pub format: u8, |
10126 | pub data_len: u32, |
10127 | pub data: Cow<'input, [u8]>, |
10128 | } |
10129 | impl<'input> ChangePropertyRequest<'input> { |
10130 | /// Serialize this request into bytes for the provided connection |
10131 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
10132 | let length_so_far = 0; |
10133 | let mode_bytes = u8::from(self.mode).serialize(); |
10134 | let window_bytes = self.window.serialize(); |
10135 | let property_bytes = self.property.serialize(); |
10136 | let type_bytes = self.type_.serialize(); |
10137 | let format_bytes = self.format.serialize(); |
10138 | let data_len_bytes = self.data_len.serialize(); |
10139 | let mut request0 = vec![ |
10140 | CHANGE_PROPERTY_REQUEST, |
10141 | mode_bytes[0], |
10142 | 0, |
10143 | 0, |
10144 | window_bytes[0], |
10145 | window_bytes[1], |
10146 | window_bytes[2], |
10147 | window_bytes[3], |
10148 | property_bytes[0], |
10149 | property_bytes[1], |
10150 | property_bytes[2], |
10151 | property_bytes[3], |
10152 | type_bytes[0], |
10153 | type_bytes[1], |
10154 | type_bytes[2], |
10155 | type_bytes[3], |
10156 | format_bytes[0], |
10157 | 0, |
10158 | 0, |
10159 | 0, |
10160 | data_len_bytes[0], |
10161 | data_len_bytes[1], |
10162 | data_len_bytes[2], |
10163 | data_len_bytes[3], |
10164 | ]; |
10165 | let length_so_far = length_so_far + request0.len(); |
10166 | assert_eq!(self.data.len(), usize::try_from(u32::from(self.data_len).checked_mul(u32::from(self.format)).unwrap().checked_div(8u32).unwrap()).unwrap(), "`data` has an incorrect length" ); |
10167 | let length_so_far = length_so_far + self.data.len(); |
10168 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
10169 | let length_so_far = length_so_far + padding0.len(); |
10170 | assert_eq!(length_so_far % 4, 0); |
10171 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
10172 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
10173 | (vec![request0.into(), self.data, padding0.into()], vec![]) |
10174 | } |
10175 | /// Parse this request given its header, its body, and any fds that go along with it |
10176 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
10177 | if header.major_opcode != CHANGE_PROPERTY_REQUEST { |
10178 | return Err(ParseError::InvalidValue); |
10179 | } |
10180 | let remaining = &[header.minor_opcode]; |
10181 | let (mode, remaining) = u8::try_parse(remaining)?; |
10182 | let mode = mode.into(); |
10183 | let _ = remaining; |
10184 | let (window, remaining) = Window::try_parse(value)?; |
10185 | let (property, remaining) = Atom::try_parse(remaining)?; |
10186 | let (type_, remaining) = Atom::try_parse(remaining)?; |
10187 | let (format, remaining) = u8::try_parse(remaining)?; |
10188 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
10189 | let (data_len, remaining) = u32::try_parse(remaining)?; |
10190 | let (data, remaining) = crate::x11_utils::parse_u8_list(remaining, u32::from(data_len).checked_mul(u32::from(format)).ok_or(ParseError::InvalidExpression)?.checked_div(8u32).ok_or(ParseError::InvalidExpression)?.try_to_usize()?)?; |
10191 | let _ = remaining; |
10192 | Ok(ChangePropertyRequest { |
10193 | mode, |
10194 | window, |
10195 | property, |
10196 | type_, |
10197 | format, |
10198 | data_len, |
10199 | data: Cow::Borrowed(data), |
10200 | }) |
10201 | } |
10202 | /// Clone all borrowed data in this ChangePropertyRequest. |
10203 | pub fn into_owned(self) -> ChangePropertyRequest<'static> { |
10204 | ChangePropertyRequest { |
10205 | mode: self.mode, |
10206 | window: self.window, |
10207 | property: self.property, |
10208 | type_: self.type_, |
10209 | format: self.format, |
10210 | data_len: self.data_len, |
10211 | data: Cow::Owned(self.data.into_owned()), |
10212 | } |
10213 | } |
10214 | } |
10215 | impl<'input> Request for ChangePropertyRequest<'input> { |
10216 | const EXTENSION_NAME: Option<&'static str> = None; |
10217 | |
10218 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
10219 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
10220 | // Flatten the buffers into a single vector |
10221 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
10222 | (buf, fds) |
10223 | } |
10224 | } |
10225 | impl<'input> crate::x11_utils::VoidRequest for ChangePropertyRequest<'input> { |
10226 | } |
10227 | |
10228 | /// Opcode for the DeleteProperty request |
10229 | pub const DELETE_PROPERTY_REQUEST: u8 = 19; |
10230 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
10231 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
10232 | pub struct DeletePropertyRequest { |
10233 | pub window: Window, |
10234 | pub property: Atom, |
10235 | } |
10236 | impl DeletePropertyRequest { |
10237 | /// Serialize this request into bytes for the provided connection |
10238 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
10239 | let length_so_far = 0; |
10240 | let window_bytes = self.window.serialize(); |
10241 | let property_bytes = self.property.serialize(); |
10242 | let mut request0 = vec![ |
10243 | DELETE_PROPERTY_REQUEST, |
10244 | 0, |
10245 | 0, |
10246 | 0, |
10247 | window_bytes[0], |
10248 | window_bytes[1], |
10249 | window_bytes[2], |
10250 | window_bytes[3], |
10251 | property_bytes[0], |
10252 | property_bytes[1], |
10253 | property_bytes[2], |
10254 | property_bytes[3], |
10255 | ]; |
10256 | let length_so_far = length_so_far + request0.len(); |
10257 | assert_eq!(length_so_far % 4, 0); |
10258 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
10259 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
10260 | (vec![request0.into()], vec![]) |
10261 | } |
10262 | /// Parse this request given its header, its body, and any fds that go along with it |
10263 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
10264 | if header.major_opcode != DELETE_PROPERTY_REQUEST { |
10265 | return Err(ParseError::InvalidValue); |
10266 | } |
10267 | let remaining = &[header.minor_opcode]; |
10268 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
10269 | let _ = remaining; |
10270 | let (window, remaining) = Window::try_parse(value)?; |
10271 | let (property, remaining) = Atom::try_parse(remaining)?; |
10272 | let _ = remaining; |
10273 | Ok(DeletePropertyRequest { |
10274 | window, |
10275 | property, |
10276 | }) |
10277 | } |
10278 | } |
10279 | impl Request for DeletePropertyRequest { |
10280 | const EXTENSION_NAME: Option<&'static str> = None; |
10281 | |
10282 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
10283 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
10284 | // Flatten the buffers into a single vector |
10285 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
10286 | (buf, fds) |
10287 | } |
10288 | } |
10289 | impl crate::x11_utils::VoidRequest for DeletePropertyRequest { |
10290 | } |
10291 | |
10292 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
10293 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
10294 | pub struct GetPropertyType(u8); |
10295 | impl GetPropertyType { |
10296 | pub const ANY: Self = Self(0); |
10297 | } |
10298 | impl From<GetPropertyType> for u8 { |
10299 | #[inline ] |
10300 | fn from(input: GetPropertyType) -> Self { |
10301 | input.0 |
10302 | } |
10303 | } |
10304 | impl From<GetPropertyType> for Option<u8> { |
10305 | #[inline ] |
10306 | fn from(input: GetPropertyType) -> Self { |
10307 | Some(input.0) |
10308 | } |
10309 | } |
10310 | impl From<GetPropertyType> for u16 { |
10311 | #[inline ] |
10312 | fn from(input: GetPropertyType) -> Self { |
10313 | u16::from(input.0) |
10314 | } |
10315 | } |
10316 | impl From<GetPropertyType> for Option<u16> { |
10317 | #[inline ] |
10318 | fn from(input: GetPropertyType) -> Self { |
10319 | Some(u16::from(input.0)) |
10320 | } |
10321 | } |
10322 | impl From<GetPropertyType> for u32 { |
10323 | #[inline ] |
10324 | fn from(input: GetPropertyType) -> Self { |
10325 | u32::from(input.0) |
10326 | } |
10327 | } |
10328 | impl From<GetPropertyType> for Option<u32> { |
10329 | #[inline ] |
10330 | fn from(input: GetPropertyType) -> Self { |
10331 | Some(u32::from(input.0)) |
10332 | } |
10333 | } |
10334 | impl From<u8> for GetPropertyType { |
10335 | #[inline ] |
10336 | fn from(value: u8) -> Self { |
10337 | Self(value) |
10338 | } |
10339 | } |
10340 | impl core::fmt::Debug for GetPropertyType { |
10341 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
10342 | let variants: [(u32, &str, &str); 1] = [ |
10343 | (Self::ANY.0.into(), "ANY" , "Any" ), |
10344 | ]; |
10345 | pretty_print_enum(fmt, self.0.into(), &variants) |
10346 | } |
10347 | } |
10348 | |
10349 | /// Opcode for the GetProperty request |
10350 | pub const GET_PROPERTY_REQUEST: u8 = 20; |
10351 | /// Gets a window property. |
10352 | /// |
10353 | /// Gets the specified `property` from the specified `window`. Properties are for |
10354 | /// example the window title (`WM_NAME`) or its minimum size (`WM_NORMAL_HINTS`). |
10355 | /// Protocols such as EWMH also use properties - for example EWMH defines the |
10356 | /// window title, encoded as UTF-8 string, in the `_NET_WM_NAME` property. |
10357 | /// |
10358 | /// TODO: talk about `type` |
10359 | /// |
10360 | /// TODO: talk about `delete` |
10361 | /// |
10362 | /// TODO: talk about the offset/length thing. what's a valid use case? |
10363 | /// |
10364 | /// # Fields |
10365 | /// |
10366 | /// * `window` - The window whose property you want to get. |
10367 | /// * `delete` - Whether the property should actually be deleted. For deleting a property, the |
10368 | /// specified `type` has to match the actual property type. |
10369 | /// * `property` - The property you want to get (an atom). |
10370 | /// * `type` - The type of the property you want to get (an atom). |
10371 | /// * `long_offset` - Specifies the offset (in 32-bit multiples) in the specified property where the |
10372 | /// data is to be retrieved. |
10373 | /// * `long_length` - Specifies how many 32-bit multiples of data should be retrieved (e.g. if you |
10374 | /// set `long_length` to 4, you will receive 16 bytes of data). |
10375 | /// |
10376 | /// # Errors |
10377 | /// |
10378 | /// * `Window` - The specified `window` does not exist. |
10379 | /// * `Atom` - `property` or `type` do not refer to a valid atom. |
10380 | /// * `Value` - The specified `long_offset` is beyond the actual property length (e.g. the |
10381 | /// property has a length of 3 bytes and you are setting `long_offset` to 1, |
10382 | /// resulting in a byte offset of 4). |
10383 | /// |
10384 | /// # See |
10385 | /// |
10386 | /// * `InternAtom`: request |
10387 | /// * `xprop`: program |
10388 | /// |
10389 | /// # Example |
10390 | /// |
10391 | /// ```text |
10392 | /// /* |
10393 | /// * Prints the WM_NAME property of the window. |
10394 | /// * |
10395 | /// */ |
10396 | /// void my_example(xcb_connection_t *c, xcb_window_t window) { |
10397 | /// xcb_get_property_cookie_t cookie; |
10398 | /// xcb_get_property_reply_t *reply; |
10399 | /// |
10400 | /// /* These atoms are predefined in the X11 protocol. */ |
10401 | /// xcb_atom_t property = XCB_ATOM_WM_NAME; |
10402 | /// xcb_atom_t type = XCB_ATOM_STRING; |
10403 | /// |
10404 | /// // TODO: a reasonable long_length for WM_NAME? |
10405 | /// cookie = xcb_get_property(c, 0, window, property, type, 0, 0); |
10406 | /// if ((reply = xcb_get_property_reply(c, cookie, NULL))) { |
10407 | /// int len = xcb_get_property_value_length(reply); |
10408 | /// if (len == 0) { |
10409 | /// printf("TODO\\n"); |
10410 | /// free(reply); |
10411 | /// return; |
10412 | /// } |
10413 | /// printf("WM_NAME is %.*s\\n", len, |
10414 | /// (char*)xcb_get_property_value(reply)); |
10415 | /// } |
10416 | /// free(reply); |
10417 | /// } |
10418 | /// ``` |
10419 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
10420 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
10421 | pub struct GetPropertyRequest { |
10422 | pub delete: bool, |
10423 | pub window: Window, |
10424 | pub property: Atom, |
10425 | pub type_: Atom, |
10426 | pub long_offset: u32, |
10427 | pub long_length: u32, |
10428 | } |
10429 | impl GetPropertyRequest { |
10430 | /// Serialize this request into bytes for the provided connection |
10431 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
10432 | let length_so_far = 0; |
10433 | let delete_bytes = self.delete.serialize(); |
10434 | let window_bytes = self.window.serialize(); |
10435 | let property_bytes = self.property.serialize(); |
10436 | let type_bytes = self.type_.serialize(); |
10437 | let long_offset_bytes = self.long_offset.serialize(); |
10438 | let long_length_bytes = self.long_length.serialize(); |
10439 | let mut request0 = vec![ |
10440 | GET_PROPERTY_REQUEST, |
10441 | delete_bytes[0], |
10442 | 0, |
10443 | 0, |
10444 | window_bytes[0], |
10445 | window_bytes[1], |
10446 | window_bytes[2], |
10447 | window_bytes[3], |
10448 | property_bytes[0], |
10449 | property_bytes[1], |
10450 | property_bytes[2], |
10451 | property_bytes[3], |
10452 | type_bytes[0], |
10453 | type_bytes[1], |
10454 | type_bytes[2], |
10455 | type_bytes[3], |
10456 | long_offset_bytes[0], |
10457 | long_offset_bytes[1], |
10458 | long_offset_bytes[2], |
10459 | long_offset_bytes[3], |
10460 | long_length_bytes[0], |
10461 | long_length_bytes[1], |
10462 | long_length_bytes[2], |
10463 | long_length_bytes[3], |
10464 | ]; |
10465 | let length_so_far = length_so_far + request0.len(); |
10466 | assert_eq!(length_so_far % 4, 0); |
10467 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
10468 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
10469 | (vec![request0.into()], vec![]) |
10470 | } |
10471 | /// Parse this request given its header, its body, and any fds that go along with it |
10472 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
10473 | if header.major_opcode != GET_PROPERTY_REQUEST { |
10474 | return Err(ParseError::InvalidValue); |
10475 | } |
10476 | let remaining = &[header.minor_opcode]; |
10477 | let (delete, remaining) = bool::try_parse(remaining)?; |
10478 | let _ = remaining; |
10479 | let (window, remaining) = Window::try_parse(value)?; |
10480 | let (property, remaining) = Atom::try_parse(remaining)?; |
10481 | let (type_, remaining) = Atom::try_parse(remaining)?; |
10482 | let (long_offset, remaining) = u32::try_parse(remaining)?; |
10483 | let (long_length, remaining) = u32::try_parse(remaining)?; |
10484 | let _ = remaining; |
10485 | Ok(GetPropertyRequest { |
10486 | delete, |
10487 | window, |
10488 | property, |
10489 | type_, |
10490 | long_offset, |
10491 | long_length, |
10492 | }) |
10493 | } |
10494 | } |
10495 | impl Request for GetPropertyRequest { |
10496 | const EXTENSION_NAME: Option<&'static str> = None; |
10497 | |
10498 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
10499 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
10500 | // Flatten the buffers into a single vector |
10501 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
10502 | (buf, fds) |
10503 | } |
10504 | } |
10505 | impl crate::x11_utils::ReplyRequest for GetPropertyRequest { |
10506 | type Reply = GetPropertyReply; |
10507 | } |
10508 | impl GetPropertyReply { |
10509 | /// Iterate over the contained value if its format is 8. |
10510 | /// |
10511 | /// This function checks if the `format` member of the reply |
10512 | /// is 8. If it it is not, `None` is returned. Otherwise |
10513 | /// and iterator is returned that interprets the value in |
10514 | /// this reply as type `u8`. |
10515 | /// |
10516 | /// # Examples |
10517 | /// |
10518 | /// Successfully iterate over the value: |
10519 | /// ``` |
10520 | /// // First, we have to 'invent' a GetPropertyReply. |
10521 | /// let reply = x11rb_protocol::protocol::xproto::GetPropertyReply { |
10522 | /// format: 8, |
10523 | /// sequence: 0, |
10524 | /// length: 0, // This value is incorrect |
10525 | /// type_: 0, // This value is incorrect |
10526 | /// bytes_after: 0, |
10527 | /// value_len: 4, |
10528 | /// value: vec![1, 2, 3, 4], |
10529 | /// }; |
10530 | /// |
10531 | /// // This is the actual example: Iterate over the value. |
10532 | /// let mut iter = reply.value8().unwrap(); |
10533 | /// assert_eq!(iter.next(), Some(1)); |
10534 | /// assert_eq!(iter.next(), Some(2)); |
10535 | /// assert_eq!(iter.next(), Some(3)); |
10536 | /// assert_eq!(iter.next(), Some(4)); |
10537 | /// assert_eq!(iter.next(), None); |
10538 | /// ``` |
10539 | /// |
10540 | /// An iterator is only returned when the `format` is correct. |
10541 | /// The following example shows this. |
10542 | /// ``` |
10543 | /// // First, we have to 'invent' a GetPropertyReply. |
10544 | /// let reply = x11rb_protocol::protocol::xproto::GetPropertyReply { |
10545 | /// format: 42, // Not allowed in X11, but used for the example |
10546 | /// sequence: 0, |
10547 | /// length: 0, // This value is incorrect |
10548 | /// type_: 0, // This value is incorrect |
10549 | /// bytes_after: 0, |
10550 | /// value_len: 4, |
10551 | /// value: vec![1, 2, 3, 4], |
10552 | /// }; |
10553 | /// assert!(reply.value8().is_none()); |
10554 | /// ``` |
10555 | pub fn value8(&self) -> Option<impl Iterator<Item=u8> + '_> { |
10556 | if self.format == 8 { |
10557 | Some(crate::wrapper::PropertyIterator::new(&self.value)) |
10558 | } else { |
10559 | None |
10560 | } |
10561 | } |
10562 | /// Iterate over the contained value if its format is 16. |
10563 | /// |
10564 | /// This function checks if the `format` member of the reply |
10565 | /// is 16. If it it is not, `None` is returned. Otherwise |
10566 | /// and iterator is returned that interprets the value in |
10567 | /// this reply as type `u16`. |
10568 | /// |
10569 | /// # Examples |
10570 | /// |
10571 | /// Successfully iterate over the value: |
10572 | /// ``` |
10573 | /// // First, we have to 'invent' a GetPropertyReply. |
10574 | /// let reply = x11rb_protocol::protocol::xproto::GetPropertyReply { |
10575 | /// format: 16, |
10576 | /// sequence: 0, |
10577 | /// length: 0, // This value is incorrect |
10578 | /// type_: 0, // This value is incorrect |
10579 | /// bytes_after: 0, |
10580 | /// value_len: 4, |
10581 | /// value: vec![1, 1, 2, 2], |
10582 | /// }; |
10583 | /// |
10584 | /// // This is the actual example: Iterate over the value. |
10585 | /// let mut iter = reply.value16().unwrap(); |
10586 | /// assert_eq!(iter.next(), Some(257)); |
10587 | /// assert_eq!(iter.next(), Some(514)); |
10588 | /// assert_eq!(iter.next(), None); |
10589 | /// ``` |
10590 | /// |
10591 | /// An iterator is only returned when the `format` is correct. |
10592 | /// The following example shows this. |
10593 | /// ``` |
10594 | /// // First, we have to 'invent' a GetPropertyReply. |
10595 | /// let reply = x11rb_protocol::protocol::xproto::GetPropertyReply { |
10596 | /// format: 42, // Not allowed in X11, but used for the example |
10597 | /// sequence: 0, |
10598 | /// length: 0, // This value is incorrect |
10599 | /// type_: 0, // This value is incorrect |
10600 | /// bytes_after: 0, |
10601 | /// value_len: 4, |
10602 | /// value: vec![1, 2, 3, 4], |
10603 | /// }; |
10604 | /// assert!(reply.value16().is_none()); |
10605 | /// ``` |
10606 | pub fn value16(&self) -> Option<impl Iterator<Item=u16> + '_> { |
10607 | if self.format == 16 { |
10608 | Some(crate::wrapper::PropertyIterator::new(&self.value)) |
10609 | } else { |
10610 | None |
10611 | } |
10612 | } |
10613 | /// Iterate over the contained value if its format is 32. |
10614 | /// |
10615 | /// This function checks if the `format` member of the reply |
10616 | /// is 32. If it it is not, `None` is returned. Otherwise |
10617 | /// and iterator is returned that interprets the value in |
10618 | /// this reply as type `u32`. |
10619 | /// |
10620 | /// # Examples |
10621 | /// |
10622 | /// Successfully iterate over the value: |
10623 | /// ``` |
10624 | /// // First, we have to 'invent' a GetPropertyReply. |
10625 | /// let reply = x11rb_protocol::protocol::xproto::GetPropertyReply { |
10626 | /// format: 32, |
10627 | /// sequence: 0, |
10628 | /// length: 0, // This value is incorrect |
10629 | /// type_: 0, // This value is incorrect |
10630 | /// bytes_after: 0, |
10631 | /// value_len: 4, |
10632 | /// value: vec![1, 2, 2, 1], |
10633 | /// }; |
10634 | /// |
10635 | /// // This is the actual example: Iterate over the value. |
10636 | /// let mut iter = reply.value32().unwrap(); |
10637 | /// assert_eq!(iter.next(), Some(16908801)); |
10638 | /// assert_eq!(iter.next(), None); |
10639 | /// ``` |
10640 | /// |
10641 | /// An iterator is only returned when the `format` is correct. |
10642 | /// The following example shows this. |
10643 | /// ``` |
10644 | /// // First, we have to 'invent' a GetPropertyReply. |
10645 | /// let reply = x11rb_protocol::protocol::xproto::GetPropertyReply { |
10646 | /// format: 42, // Not allowed in X11, but used for the example |
10647 | /// sequence: 0, |
10648 | /// length: 0, // This value is incorrect |
10649 | /// type_: 0, // This value is incorrect |
10650 | /// bytes_after: 0, |
10651 | /// value_len: 4, |
10652 | /// value: vec![1, 2, 3, 4], |
10653 | /// }; |
10654 | /// assert!(reply.value32().is_none()); |
10655 | /// ``` |
10656 | pub fn value32(&self) -> Option<impl Iterator<Item=u32> + '_> { |
10657 | if self.format == 32 { |
10658 | Some(crate::wrapper::PropertyIterator::new(&self.value)) |
10659 | } else { |
10660 | None |
10661 | } |
10662 | } |
10663 | } |
10664 | |
10665 | |
10666 | /// # Fields |
10667 | /// |
10668 | /// * `format` - Specifies whether the data should be viewed as a list of 8-bit, 16-bit, or |
10669 | /// 32-bit quantities. Possible values are 8, 16, and 32. This information allows |
10670 | /// the X server to correctly perform byte-swap operations as necessary. |
10671 | /// * `type` - The actual type of the property (an atom). |
10672 | /// * `bytes_after` - The number of bytes remaining to be read in the property if a partial read was |
10673 | /// performed. |
10674 | /// * `value_len` - The length of value. You should use the corresponding accessor instead of this |
10675 | /// field. |
10676 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
10677 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
10678 | pub struct GetPropertyReply { |
10679 | pub format: u8, |
10680 | pub sequence: u16, |
10681 | pub length: u32, |
10682 | pub type_: Atom, |
10683 | pub bytes_after: u32, |
10684 | pub value_len: u32, |
10685 | pub value: Vec<u8>, |
10686 | } |
10687 | impl TryParse for GetPropertyReply { |
10688 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
10689 | let remaining: &[u8] = initial_value; |
10690 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
10691 | let (format: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
10692 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
10693 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
10694 | let (type_: u32, remaining: &[u8]) = Atom::try_parse(remaining)?; |
10695 | let (bytes_after: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
10696 | let (value_len: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
10697 | let remaining: &[u8] = remaining.get(12..).ok_or(err:ParseError::InsufficientData)?; |
10698 | let (value: &[u8], remaining: &[u8]) = crate::x11_utils::parse_u8_list(data:remaining, list_length:u32::from(value_len).checked_mul(u32::from(format).checked_div(8u32).ok_or(ParseError::InvalidExpression)?).ok_or(err:ParseError::InvalidExpression)?.try_to_usize()?)?; |
10699 | let value: Vec = value.to_vec(); |
10700 | if response_type != 1 { |
10701 | return Err(ParseError::InvalidValue); |
10702 | } |
10703 | let result: GetPropertyReply = GetPropertyReply { format, sequence, length, type_, bytes_after, value_len, value }; |
10704 | let _ = remaining; |
10705 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
10706 | .ok_or(err:ParseError::InsufficientData)?; |
10707 | Ok((result, remaining)) |
10708 | } |
10709 | } |
10710 | impl Serialize for GetPropertyReply { |
10711 | type Bytes = Vec<u8>; |
10712 | fn serialize(&self) -> Vec<u8> { |
10713 | let mut result: Vec = Vec::new(); |
10714 | self.serialize_into(&mut result); |
10715 | result |
10716 | } |
10717 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
10718 | bytes.reserve(additional:32); |
10719 | let response_type_bytes: &[u8; 1] = &[1]; |
10720 | bytes.push(response_type_bytes[0]); |
10721 | self.format.serialize_into(bytes); |
10722 | self.sequence.serialize_into(bytes); |
10723 | self.length.serialize_into(bytes); |
10724 | self.type_.serialize_into(bytes); |
10725 | self.bytes_after.serialize_into(bytes); |
10726 | self.value_len.serialize_into(bytes); |
10727 | bytes.extend_from_slice(&[0; 12]); |
10728 | assert_eq!(self.value.len(), usize::try_from(u32::from(self.value_len).checked_mul(u32::from(self.format).checked_div(8u32).unwrap()).unwrap()).unwrap(), "`value` has an incorrect length" ); |
10729 | bytes.extend_from_slice(&self.value); |
10730 | } |
10731 | } |
10732 | |
10733 | /// Opcode for the ListProperties request |
10734 | pub const LIST_PROPERTIES_REQUEST: u8 = 21; |
10735 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
10736 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
10737 | pub struct ListPropertiesRequest { |
10738 | pub window: Window, |
10739 | } |
10740 | impl ListPropertiesRequest { |
10741 | /// Serialize this request into bytes for the provided connection |
10742 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
10743 | let length_so_far = 0; |
10744 | let window_bytes = self.window.serialize(); |
10745 | let mut request0 = vec![ |
10746 | LIST_PROPERTIES_REQUEST, |
10747 | 0, |
10748 | 0, |
10749 | 0, |
10750 | window_bytes[0], |
10751 | window_bytes[1], |
10752 | window_bytes[2], |
10753 | window_bytes[3], |
10754 | ]; |
10755 | let length_so_far = length_so_far + request0.len(); |
10756 | assert_eq!(length_so_far % 4, 0); |
10757 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
10758 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
10759 | (vec![request0.into()], vec![]) |
10760 | } |
10761 | /// Parse this request given its header, its body, and any fds that go along with it |
10762 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
10763 | if header.major_opcode != LIST_PROPERTIES_REQUEST { |
10764 | return Err(ParseError::InvalidValue); |
10765 | } |
10766 | let remaining = &[header.minor_opcode]; |
10767 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
10768 | let _ = remaining; |
10769 | let (window, remaining) = Window::try_parse(value)?; |
10770 | let _ = remaining; |
10771 | Ok(ListPropertiesRequest { |
10772 | window, |
10773 | }) |
10774 | } |
10775 | } |
10776 | impl Request for ListPropertiesRequest { |
10777 | const EXTENSION_NAME: Option<&'static str> = None; |
10778 | |
10779 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
10780 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
10781 | // Flatten the buffers into a single vector |
10782 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
10783 | (buf, fds) |
10784 | } |
10785 | } |
10786 | impl crate::x11_utils::ReplyRequest for ListPropertiesRequest { |
10787 | type Reply = ListPropertiesReply; |
10788 | } |
10789 | |
10790 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
10791 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
10792 | pub struct ListPropertiesReply { |
10793 | pub sequence: u16, |
10794 | pub length: u32, |
10795 | pub atoms: Vec<Atom>, |
10796 | } |
10797 | impl TryParse for ListPropertiesReply { |
10798 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
10799 | let remaining: &[u8] = initial_value; |
10800 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
10801 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
10802 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
10803 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
10804 | let (atoms_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
10805 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
10806 | let (atoms: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<Atom>(data:remaining, list_length:atoms_len.try_to_usize()?)?; |
10807 | if response_type != 1 { |
10808 | return Err(ParseError::InvalidValue); |
10809 | } |
10810 | let result: ListPropertiesReply = ListPropertiesReply { sequence, length, atoms }; |
10811 | let _ = remaining; |
10812 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
10813 | .ok_or(err:ParseError::InsufficientData)?; |
10814 | Ok((result, remaining)) |
10815 | } |
10816 | } |
10817 | impl Serialize for ListPropertiesReply { |
10818 | type Bytes = Vec<u8>; |
10819 | fn serialize(&self) -> Vec<u8> { |
10820 | let mut result: Vec = Vec::new(); |
10821 | self.serialize_into(&mut result); |
10822 | result |
10823 | } |
10824 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
10825 | bytes.reserve(additional:32); |
10826 | let response_type_bytes: &[u8; 1] = &[1]; |
10827 | bytes.push(response_type_bytes[0]); |
10828 | bytes.extend_from_slice(&[0; 1]); |
10829 | self.sequence.serialize_into(bytes); |
10830 | self.length.serialize_into(bytes); |
10831 | let atoms_len: u16 = u16::try_from(self.atoms.len()).expect(msg:"`atoms` has too many elements" ); |
10832 | atoms_len.serialize_into(bytes); |
10833 | bytes.extend_from_slice(&[0; 22]); |
10834 | self.atoms.serialize_into(bytes); |
10835 | } |
10836 | } |
10837 | impl ListPropertiesReply { |
10838 | /// Get the value of the `atoms_len` field. |
10839 | /// |
10840 | /// The `atoms_len` field is used as the length field of the `atoms` field. |
10841 | /// This function computes the field's value again based on the length of the list. |
10842 | /// |
10843 | /// # Panics |
10844 | /// |
10845 | /// Panics if the value cannot be represented in the target type. This |
10846 | /// cannot happen with values of the struct received from the X11 server. |
10847 | pub fn atoms_len(&self) -> u16 { |
10848 | self.atoms.len() |
10849 | .try_into().unwrap() |
10850 | } |
10851 | } |
10852 | |
10853 | /// Opcode for the SetSelectionOwner request |
10854 | pub const SET_SELECTION_OWNER_REQUEST: u8 = 22; |
10855 | /// Sets the owner of a selection. |
10856 | /// |
10857 | /// Makes `window` the owner of the selection `selection` and updates the |
10858 | /// last-change time of the specified selection. |
10859 | /// |
10860 | /// TODO: briefly explain what a selection is. |
10861 | /// |
10862 | /// # Fields |
10863 | /// |
10864 | /// * `selection` - The selection. |
10865 | /// * `owner` - The new owner of the selection. |
10866 | /// |
10867 | /// The special value `XCB_NONE` means that the selection will have no owner. |
10868 | /// * `time` - Timestamp to avoid race conditions when running X over the network. |
10869 | /// |
10870 | /// The selection will not be changed if `time` is earlier than the current |
10871 | /// last-change time of the `selection` or is later than the current X server time. |
10872 | /// Otherwise, the last-change time is set to the specified time. |
10873 | /// |
10874 | /// The special value `XCB_CURRENT_TIME` will be replaced with the current server |
10875 | /// time. |
10876 | /// |
10877 | /// # Errors |
10878 | /// |
10879 | /// * `Atom` - `selection` does not refer to a valid atom. |
10880 | /// |
10881 | /// # See |
10882 | /// |
10883 | /// * `SetSelectionOwner`: request |
10884 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
10885 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
10886 | pub struct SetSelectionOwnerRequest { |
10887 | pub owner: Window, |
10888 | pub selection: Atom, |
10889 | pub time: Timestamp, |
10890 | } |
10891 | impl SetSelectionOwnerRequest { |
10892 | /// Serialize this request into bytes for the provided connection |
10893 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
10894 | let length_so_far = 0; |
10895 | let owner_bytes = self.owner.serialize(); |
10896 | let selection_bytes = self.selection.serialize(); |
10897 | let time_bytes = self.time.serialize(); |
10898 | let mut request0 = vec![ |
10899 | SET_SELECTION_OWNER_REQUEST, |
10900 | 0, |
10901 | 0, |
10902 | 0, |
10903 | owner_bytes[0], |
10904 | owner_bytes[1], |
10905 | owner_bytes[2], |
10906 | owner_bytes[3], |
10907 | selection_bytes[0], |
10908 | selection_bytes[1], |
10909 | selection_bytes[2], |
10910 | selection_bytes[3], |
10911 | time_bytes[0], |
10912 | time_bytes[1], |
10913 | time_bytes[2], |
10914 | time_bytes[3], |
10915 | ]; |
10916 | let length_so_far = length_so_far + request0.len(); |
10917 | assert_eq!(length_so_far % 4, 0); |
10918 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
10919 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
10920 | (vec![request0.into()], vec![]) |
10921 | } |
10922 | /// Parse this request given its header, its body, and any fds that go along with it |
10923 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
10924 | if header.major_opcode != SET_SELECTION_OWNER_REQUEST { |
10925 | return Err(ParseError::InvalidValue); |
10926 | } |
10927 | let remaining = &[header.minor_opcode]; |
10928 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
10929 | let _ = remaining; |
10930 | let (owner, remaining) = Window::try_parse(value)?; |
10931 | let (selection, remaining) = Atom::try_parse(remaining)?; |
10932 | let (time, remaining) = Timestamp::try_parse(remaining)?; |
10933 | let _ = remaining; |
10934 | Ok(SetSelectionOwnerRequest { |
10935 | owner, |
10936 | selection, |
10937 | time, |
10938 | }) |
10939 | } |
10940 | } |
10941 | impl Request for SetSelectionOwnerRequest { |
10942 | const EXTENSION_NAME: Option<&'static str> = None; |
10943 | |
10944 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
10945 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
10946 | // Flatten the buffers into a single vector |
10947 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
10948 | (buf, fds) |
10949 | } |
10950 | } |
10951 | impl crate::x11_utils::VoidRequest for SetSelectionOwnerRequest { |
10952 | } |
10953 | |
10954 | /// Opcode for the GetSelectionOwner request |
10955 | pub const GET_SELECTION_OWNER_REQUEST: u8 = 23; |
10956 | /// Gets the owner of a selection. |
10957 | /// |
10958 | /// Gets the owner of the specified selection. |
10959 | /// |
10960 | /// TODO: briefly explain what a selection is. |
10961 | /// |
10962 | /// # Fields |
10963 | /// |
10964 | /// * `selection` - The selection. |
10965 | /// |
10966 | /// # Errors |
10967 | /// |
10968 | /// * `Atom` - `selection` does not refer to a valid atom. |
10969 | /// |
10970 | /// # See |
10971 | /// |
10972 | /// * `SetSelectionOwner`: request |
10973 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
10974 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
10975 | pub struct GetSelectionOwnerRequest { |
10976 | pub selection: Atom, |
10977 | } |
10978 | impl GetSelectionOwnerRequest { |
10979 | /// Serialize this request into bytes for the provided connection |
10980 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
10981 | let length_so_far = 0; |
10982 | let selection_bytes = self.selection.serialize(); |
10983 | let mut request0 = vec![ |
10984 | GET_SELECTION_OWNER_REQUEST, |
10985 | 0, |
10986 | 0, |
10987 | 0, |
10988 | selection_bytes[0], |
10989 | selection_bytes[1], |
10990 | selection_bytes[2], |
10991 | selection_bytes[3], |
10992 | ]; |
10993 | let length_so_far = length_so_far + request0.len(); |
10994 | assert_eq!(length_so_far % 4, 0); |
10995 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
10996 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
10997 | (vec![request0.into()], vec![]) |
10998 | } |
10999 | /// Parse this request given its header, its body, and any fds that go along with it |
11000 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
11001 | if header.major_opcode != GET_SELECTION_OWNER_REQUEST { |
11002 | return Err(ParseError::InvalidValue); |
11003 | } |
11004 | let remaining = &[header.minor_opcode]; |
11005 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
11006 | let _ = remaining; |
11007 | let (selection, remaining) = Atom::try_parse(value)?; |
11008 | let _ = remaining; |
11009 | Ok(GetSelectionOwnerRequest { |
11010 | selection, |
11011 | }) |
11012 | } |
11013 | } |
11014 | impl Request for GetSelectionOwnerRequest { |
11015 | const EXTENSION_NAME: Option<&'static str> = None; |
11016 | |
11017 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
11018 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
11019 | // Flatten the buffers into a single vector |
11020 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
11021 | (buf, fds) |
11022 | } |
11023 | } |
11024 | impl crate::x11_utils::ReplyRequest for GetSelectionOwnerRequest { |
11025 | type Reply = GetSelectionOwnerReply; |
11026 | } |
11027 | |
11028 | /// # Fields |
11029 | /// |
11030 | /// * `owner` - The current selection owner window. |
11031 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
11032 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
11033 | pub struct GetSelectionOwnerReply { |
11034 | pub sequence: u16, |
11035 | pub length: u32, |
11036 | pub owner: Window, |
11037 | } |
11038 | impl TryParse for GetSelectionOwnerReply { |
11039 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
11040 | let remaining: &[u8] = initial_value; |
11041 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
11042 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
11043 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
11044 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
11045 | let (owner: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
11046 | if response_type != 1 { |
11047 | return Err(ParseError::InvalidValue); |
11048 | } |
11049 | let result: GetSelectionOwnerReply = GetSelectionOwnerReply { sequence, length, owner }; |
11050 | let _ = remaining; |
11051 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
11052 | .ok_or(err:ParseError::InsufficientData)?; |
11053 | Ok((result, remaining)) |
11054 | } |
11055 | } |
11056 | impl Serialize for GetSelectionOwnerReply { |
11057 | type Bytes = [u8; 12]; |
11058 | fn serialize(&self) -> [u8; 12] { |
11059 | let response_type_bytes = &[1]; |
11060 | let sequence_bytes = self.sequence.serialize(); |
11061 | let length_bytes = self.length.serialize(); |
11062 | let owner_bytes = self.owner.serialize(); |
11063 | [ |
11064 | response_type_bytes[0], |
11065 | 0, |
11066 | sequence_bytes[0], |
11067 | sequence_bytes[1], |
11068 | length_bytes[0], |
11069 | length_bytes[1], |
11070 | length_bytes[2], |
11071 | length_bytes[3], |
11072 | owner_bytes[0], |
11073 | owner_bytes[1], |
11074 | owner_bytes[2], |
11075 | owner_bytes[3], |
11076 | ] |
11077 | } |
11078 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
11079 | bytes.reserve(12); |
11080 | let response_type_bytes = &[1]; |
11081 | bytes.push(response_type_bytes[0]); |
11082 | bytes.extend_from_slice(&[0; 1]); |
11083 | self.sequence.serialize_into(bytes); |
11084 | self.length.serialize_into(bytes); |
11085 | self.owner.serialize_into(bytes); |
11086 | } |
11087 | } |
11088 | |
11089 | /// Opcode for the ConvertSelection request |
11090 | pub const CONVERT_SELECTION_REQUEST: u8 = 24; |
11091 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
11092 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
11093 | pub struct ConvertSelectionRequest { |
11094 | pub requestor: Window, |
11095 | pub selection: Atom, |
11096 | pub target: Atom, |
11097 | pub property: Atom, |
11098 | pub time: Timestamp, |
11099 | } |
11100 | impl ConvertSelectionRequest { |
11101 | /// Serialize this request into bytes for the provided connection |
11102 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
11103 | let length_so_far = 0; |
11104 | let requestor_bytes = self.requestor.serialize(); |
11105 | let selection_bytes = self.selection.serialize(); |
11106 | let target_bytes = self.target.serialize(); |
11107 | let property_bytes = self.property.serialize(); |
11108 | let time_bytes = self.time.serialize(); |
11109 | let mut request0 = vec![ |
11110 | CONVERT_SELECTION_REQUEST, |
11111 | 0, |
11112 | 0, |
11113 | 0, |
11114 | requestor_bytes[0], |
11115 | requestor_bytes[1], |
11116 | requestor_bytes[2], |
11117 | requestor_bytes[3], |
11118 | selection_bytes[0], |
11119 | selection_bytes[1], |
11120 | selection_bytes[2], |
11121 | selection_bytes[3], |
11122 | target_bytes[0], |
11123 | target_bytes[1], |
11124 | target_bytes[2], |
11125 | target_bytes[3], |
11126 | property_bytes[0], |
11127 | property_bytes[1], |
11128 | property_bytes[2], |
11129 | property_bytes[3], |
11130 | time_bytes[0], |
11131 | time_bytes[1], |
11132 | time_bytes[2], |
11133 | time_bytes[3], |
11134 | ]; |
11135 | let length_so_far = length_so_far + request0.len(); |
11136 | assert_eq!(length_so_far % 4, 0); |
11137 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
11138 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
11139 | (vec![request0.into()], vec![]) |
11140 | } |
11141 | /// Parse this request given its header, its body, and any fds that go along with it |
11142 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
11143 | if header.major_opcode != CONVERT_SELECTION_REQUEST { |
11144 | return Err(ParseError::InvalidValue); |
11145 | } |
11146 | let remaining = &[header.minor_opcode]; |
11147 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
11148 | let _ = remaining; |
11149 | let (requestor, remaining) = Window::try_parse(value)?; |
11150 | let (selection, remaining) = Atom::try_parse(remaining)?; |
11151 | let (target, remaining) = Atom::try_parse(remaining)?; |
11152 | let (property, remaining) = Atom::try_parse(remaining)?; |
11153 | let (time, remaining) = Timestamp::try_parse(remaining)?; |
11154 | let _ = remaining; |
11155 | Ok(ConvertSelectionRequest { |
11156 | requestor, |
11157 | selection, |
11158 | target, |
11159 | property, |
11160 | time, |
11161 | }) |
11162 | } |
11163 | } |
11164 | impl Request for ConvertSelectionRequest { |
11165 | const EXTENSION_NAME: Option<&'static str> = None; |
11166 | |
11167 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
11168 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
11169 | // Flatten the buffers into a single vector |
11170 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
11171 | (buf, fds) |
11172 | } |
11173 | } |
11174 | impl crate::x11_utils::VoidRequest for ConvertSelectionRequest { |
11175 | } |
11176 | |
11177 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
11178 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
11179 | pub struct SendEventDest(bool); |
11180 | impl SendEventDest { |
11181 | pub const POINTER_WINDOW: Self = Self(false); |
11182 | pub const ITEM_FOCUS: Self = Self(true); |
11183 | } |
11184 | impl From<SendEventDest> for bool { |
11185 | #[inline ] |
11186 | fn from(input: SendEventDest) -> Self { |
11187 | input.0 |
11188 | } |
11189 | } |
11190 | impl From<SendEventDest> for Option<bool> { |
11191 | #[inline ] |
11192 | fn from(input: SendEventDest) -> Self { |
11193 | Some(input.0) |
11194 | } |
11195 | } |
11196 | impl From<SendEventDest> for u8 { |
11197 | #[inline ] |
11198 | fn from(input: SendEventDest) -> Self { |
11199 | u8::from(input.0) |
11200 | } |
11201 | } |
11202 | impl From<SendEventDest> for Option<u8> { |
11203 | #[inline ] |
11204 | fn from(input: SendEventDest) -> Self { |
11205 | Some(u8::from(input.0)) |
11206 | } |
11207 | } |
11208 | impl From<SendEventDest> for u16 { |
11209 | #[inline ] |
11210 | fn from(input: SendEventDest) -> Self { |
11211 | u16::from(input.0) |
11212 | } |
11213 | } |
11214 | impl From<SendEventDest> for Option<u16> { |
11215 | #[inline ] |
11216 | fn from(input: SendEventDest) -> Self { |
11217 | Some(u16::from(input.0)) |
11218 | } |
11219 | } |
11220 | impl From<SendEventDest> for u32 { |
11221 | #[inline ] |
11222 | fn from(input: SendEventDest) -> Self { |
11223 | u32::from(input.0) |
11224 | } |
11225 | } |
11226 | impl From<SendEventDest> for Option<u32> { |
11227 | #[inline ] |
11228 | fn from(input: SendEventDest) -> Self { |
11229 | Some(u32::from(input.0)) |
11230 | } |
11231 | } |
11232 | impl From<bool> for SendEventDest { |
11233 | #[inline ] |
11234 | fn from(value: bool) -> Self { |
11235 | Self(value) |
11236 | } |
11237 | } |
11238 | impl core::fmt::Debug for SendEventDest { |
11239 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
11240 | let variants: [(u32, &str, &str); 2] = [ |
11241 | (Self::POINTER_WINDOW.0.into(), "POINTER_WINDOW" , "PointerWindow" ), |
11242 | (Self::ITEM_FOCUS.0.into(), "ITEM_FOCUS" , "ItemFocus" ), |
11243 | ]; |
11244 | pretty_print_enum(fmt, self.0.into(), &variants) |
11245 | } |
11246 | } |
11247 | |
11248 | /// Opcode for the SendEvent request |
11249 | pub const SEND_EVENT_REQUEST: u8 = 25; |
11250 | /// send an event. |
11251 | /// |
11252 | /// Identifies the `destination` window, determines which clients should receive |
11253 | /// the specified event and ignores any active grabs. |
11254 | /// |
11255 | /// The `event` must be one of the core events or an event defined by an extension, |
11256 | /// so that the X server can correctly byte-swap the contents as necessary. The |
11257 | /// contents of `event` are otherwise unaltered and unchecked except for the |
11258 | /// `send_event` field which is forced to 'true'. |
11259 | /// |
11260 | /// # Fields |
11261 | /// |
11262 | /// * `destination` - The window to send this event to. Every client which selects any event within |
11263 | /// `event_mask` on `destination` will get the event. |
11264 | /// |
11265 | /// The special value `XCB_SEND_EVENT_DEST_POINTER_WINDOW` refers to the window |
11266 | /// that contains the mouse pointer. |
11267 | /// |
11268 | /// The special value `XCB_SEND_EVENT_DEST_ITEM_FOCUS` refers to the window which |
11269 | /// has the keyboard focus. |
11270 | /// * `event_mask` - Event_mask for determining which clients should receive the specified event. |
11271 | /// See `destination` and `propagate`. |
11272 | /// * `propagate` - If `propagate` is true and no clients have selected any event on `destination`, |
11273 | /// the destination is replaced with the closest ancestor of `destination` for |
11274 | /// which some client has selected a type in `event_mask` and for which no |
11275 | /// intervening window has that type in its do-not-propagate-mask. If no such |
11276 | /// window exists or if the window is an ancestor of the focus window and |
11277 | /// `InputFocus` was originally specified as the destination, the event is not sent |
11278 | /// to any clients. Otherwise, the event is reported to every client selecting on |
11279 | /// the final destination any of the types specified in `event_mask`. |
11280 | /// * `event` - The event to send to the specified `destination`. |
11281 | /// |
11282 | /// # Errors |
11283 | /// |
11284 | /// * `Window` - The specified `destination` window does not exist. |
11285 | /// * `Value` - The given `event` is neither a core event nor an event defined by an extension. |
11286 | /// |
11287 | /// # See |
11288 | /// |
11289 | /// * `ConfigureNotify`: event |
11290 | /// |
11291 | /// # Example |
11292 | /// |
11293 | /// ```text |
11294 | /// /* |
11295 | /// * Tell the given window that it was configured to a size of 800x600 pixels. |
11296 | /// * |
11297 | /// */ |
11298 | /// void my_example(xcb_connection_t *conn, xcb_window_t window) { |
11299 | /// /* Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes. |
11300 | /// * In order to properly initialize these bytes, we allocate 32 bytes even |
11301 | /// * though we only need less for an xcb_configure_notify_event_t */ |
11302 | /// xcb_configure_notify_event_t *event = calloc(32, 1); |
11303 | /// |
11304 | /// event->event = window; |
11305 | /// event->window = window; |
11306 | /// event->response_type = XCB_CONFIGURE_NOTIFY; |
11307 | /// |
11308 | /// event->x = 0; |
11309 | /// event->y = 0; |
11310 | /// event->width = 800; |
11311 | /// event->height = 600; |
11312 | /// |
11313 | /// event->border_width = 0; |
11314 | /// event->above_sibling = XCB_NONE; |
11315 | /// event->override_redirect = false; |
11316 | /// |
11317 | /// xcb_send_event(conn, false, window, XCB_EVENT_MASK_STRUCTURE_NOTIFY, |
11318 | /// (char*)event); |
11319 | /// xcb_flush(conn); |
11320 | /// free(event); |
11321 | /// } |
11322 | /// ``` |
11323 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
11324 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
11325 | pub struct SendEventRequest<'input> { |
11326 | pub propagate: bool, |
11327 | pub destination: Window, |
11328 | pub event_mask: EventMask, |
11329 | pub event: Cow<'input, [u8; 32]>, |
11330 | } |
11331 | impl<'input> SendEventRequest<'input> { |
11332 | /// Serialize this request into bytes for the provided connection |
11333 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
11334 | let length_so_far = 0; |
11335 | let propagate_bytes = self.propagate.serialize(); |
11336 | let destination_bytes = self.destination.serialize(); |
11337 | let event_mask_bytes = u32::from(self.event_mask).serialize(); |
11338 | let mut request0 = vec![ |
11339 | SEND_EVENT_REQUEST, |
11340 | propagate_bytes[0], |
11341 | 0, |
11342 | 0, |
11343 | destination_bytes[0], |
11344 | destination_bytes[1], |
11345 | destination_bytes[2], |
11346 | destination_bytes[3], |
11347 | event_mask_bytes[0], |
11348 | event_mask_bytes[1], |
11349 | event_mask_bytes[2], |
11350 | event_mask_bytes[3], |
11351 | ]; |
11352 | let length_so_far = length_so_far + request0.len(); |
11353 | let length_so_far = length_so_far + self.event.len(); |
11354 | assert_eq!(length_so_far % 4, 0); |
11355 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
11356 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
11357 | (vec![request0.into(), Cow::Owned(self.event.to_vec())], vec![]) |
11358 | } |
11359 | /// Parse this request given its header, its body, and any fds that go along with it |
11360 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
11361 | if header.major_opcode != SEND_EVENT_REQUEST { |
11362 | return Err(ParseError::InvalidValue); |
11363 | } |
11364 | let remaining = &[header.minor_opcode]; |
11365 | let (propagate, remaining) = bool::try_parse(remaining)?; |
11366 | let _ = remaining; |
11367 | let (destination, remaining) = Window::try_parse(value)?; |
11368 | let (event_mask, remaining) = u32::try_parse(remaining)?; |
11369 | let event_mask = event_mask.into(); |
11370 | let (event, remaining) = crate::x11_utils::parse_u8_array_ref::<32>(remaining)?; |
11371 | let _ = remaining; |
11372 | Ok(SendEventRequest { |
11373 | propagate, |
11374 | destination, |
11375 | event_mask, |
11376 | event: Cow::Borrowed(event), |
11377 | }) |
11378 | } |
11379 | /// Clone all borrowed data in this SendEventRequest. |
11380 | pub fn into_owned(self) -> SendEventRequest<'static> { |
11381 | SendEventRequest { |
11382 | propagate: self.propagate, |
11383 | destination: self.destination, |
11384 | event_mask: self.event_mask, |
11385 | event: Cow::Owned(self.event.into_owned()), |
11386 | } |
11387 | } |
11388 | } |
11389 | impl<'input> Request for SendEventRequest<'input> { |
11390 | const EXTENSION_NAME: Option<&'static str> = None; |
11391 | |
11392 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
11393 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
11394 | // Flatten the buffers into a single vector |
11395 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
11396 | (buf, fds) |
11397 | } |
11398 | } |
11399 | impl<'input> crate::x11_utils::VoidRequest for SendEventRequest<'input> { |
11400 | } |
11401 | |
11402 | /// # Fields |
11403 | /// |
11404 | /// * `Sync` - The state of the keyboard appears to freeze: No further keyboard events are |
11405 | /// generated by the server until the grabbing client issues a releasing |
11406 | /// `AllowEvents` request or until the keyboard grab is released. |
11407 | /// * `Async` - Keyboard event processing continues normally. |
11408 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
11409 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
11410 | pub struct GrabMode(u8); |
11411 | impl GrabMode { |
11412 | pub const SYNC: Self = Self(0); |
11413 | pub const ASYNC: Self = Self(1); |
11414 | } |
11415 | impl From<GrabMode> for u8 { |
11416 | #[inline ] |
11417 | fn from(input: GrabMode) -> Self { |
11418 | input.0 |
11419 | } |
11420 | } |
11421 | impl From<GrabMode> for Option<u8> { |
11422 | #[inline ] |
11423 | fn from(input: GrabMode) -> Self { |
11424 | Some(input.0) |
11425 | } |
11426 | } |
11427 | impl From<GrabMode> for u16 { |
11428 | #[inline ] |
11429 | fn from(input: GrabMode) -> Self { |
11430 | u16::from(input.0) |
11431 | } |
11432 | } |
11433 | impl From<GrabMode> for Option<u16> { |
11434 | #[inline ] |
11435 | fn from(input: GrabMode) -> Self { |
11436 | Some(u16::from(input.0)) |
11437 | } |
11438 | } |
11439 | impl From<GrabMode> for u32 { |
11440 | #[inline ] |
11441 | fn from(input: GrabMode) -> Self { |
11442 | u32::from(input.0) |
11443 | } |
11444 | } |
11445 | impl From<GrabMode> for Option<u32> { |
11446 | #[inline ] |
11447 | fn from(input: GrabMode) -> Self { |
11448 | Some(u32::from(input.0)) |
11449 | } |
11450 | } |
11451 | impl From<u8> for GrabMode { |
11452 | #[inline ] |
11453 | fn from(value: u8) -> Self { |
11454 | Self(value) |
11455 | } |
11456 | } |
11457 | impl core::fmt::Debug for GrabMode { |
11458 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
11459 | let variants: [(u32, &str, &str); 2] = [ |
11460 | (Self::SYNC.0.into(), "SYNC" , "Sync" ), |
11461 | (Self::ASYNC.0.into(), "ASYNC" , "Async" ), |
11462 | ]; |
11463 | pretty_print_enum(fmt, self.0.into(), &variants) |
11464 | } |
11465 | } |
11466 | |
11467 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
11468 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
11469 | pub struct GrabStatus(u8); |
11470 | impl GrabStatus { |
11471 | pub const SUCCESS: Self = Self(0); |
11472 | pub const ALREADY_GRABBED: Self = Self(1); |
11473 | pub const INVALID_TIME: Self = Self(2); |
11474 | pub const NOT_VIEWABLE: Self = Self(3); |
11475 | pub const FROZEN: Self = Self(4); |
11476 | } |
11477 | impl From<GrabStatus> for u8 { |
11478 | #[inline ] |
11479 | fn from(input: GrabStatus) -> Self { |
11480 | input.0 |
11481 | } |
11482 | } |
11483 | impl From<GrabStatus> for Option<u8> { |
11484 | #[inline ] |
11485 | fn from(input: GrabStatus) -> Self { |
11486 | Some(input.0) |
11487 | } |
11488 | } |
11489 | impl From<GrabStatus> for u16 { |
11490 | #[inline ] |
11491 | fn from(input: GrabStatus) -> Self { |
11492 | u16::from(input.0) |
11493 | } |
11494 | } |
11495 | impl From<GrabStatus> for Option<u16> { |
11496 | #[inline ] |
11497 | fn from(input: GrabStatus) -> Self { |
11498 | Some(u16::from(input.0)) |
11499 | } |
11500 | } |
11501 | impl From<GrabStatus> for u32 { |
11502 | #[inline ] |
11503 | fn from(input: GrabStatus) -> Self { |
11504 | u32::from(input.0) |
11505 | } |
11506 | } |
11507 | impl From<GrabStatus> for Option<u32> { |
11508 | #[inline ] |
11509 | fn from(input: GrabStatus) -> Self { |
11510 | Some(u32::from(input.0)) |
11511 | } |
11512 | } |
11513 | impl From<u8> for GrabStatus { |
11514 | #[inline ] |
11515 | fn from(value: u8) -> Self { |
11516 | Self(value) |
11517 | } |
11518 | } |
11519 | impl core::fmt::Debug for GrabStatus { |
11520 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
11521 | let variants: [(u32, &str, &str); 5] = [ |
11522 | (Self::SUCCESS.0.into(), "SUCCESS" , "Success" ), |
11523 | (Self::ALREADY_GRABBED.0.into(), "ALREADY_GRABBED" , "AlreadyGrabbed" ), |
11524 | (Self::INVALID_TIME.0.into(), "INVALID_TIME" , "InvalidTime" ), |
11525 | (Self::NOT_VIEWABLE.0.into(), "NOT_VIEWABLE" , "NotViewable" ), |
11526 | (Self::FROZEN.0.into(), "FROZEN" , "Frozen" ), |
11527 | ]; |
11528 | pretty_print_enum(fmt, self.0.into(), &variants) |
11529 | } |
11530 | } |
11531 | |
11532 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
11533 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
11534 | pub struct CursorEnum(u8); |
11535 | impl CursorEnum { |
11536 | pub const NONE: Self = Self(0); |
11537 | } |
11538 | impl From<CursorEnum> for u8 { |
11539 | #[inline ] |
11540 | fn from(input: CursorEnum) -> Self { |
11541 | input.0 |
11542 | } |
11543 | } |
11544 | impl From<CursorEnum> for Option<u8> { |
11545 | #[inline ] |
11546 | fn from(input: CursorEnum) -> Self { |
11547 | Some(input.0) |
11548 | } |
11549 | } |
11550 | impl From<CursorEnum> for u16 { |
11551 | #[inline ] |
11552 | fn from(input: CursorEnum) -> Self { |
11553 | u16::from(input.0) |
11554 | } |
11555 | } |
11556 | impl From<CursorEnum> for Option<u16> { |
11557 | #[inline ] |
11558 | fn from(input: CursorEnum) -> Self { |
11559 | Some(u16::from(input.0)) |
11560 | } |
11561 | } |
11562 | impl From<CursorEnum> for u32 { |
11563 | #[inline ] |
11564 | fn from(input: CursorEnum) -> Self { |
11565 | u32::from(input.0) |
11566 | } |
11567 | } |
11568 | impl From<CursorEnum> for Option<u32> { |
11569 | #[inline ] |
11570 | fn from(input: CursorEnum) -> Self { |
11571 | Some(u32::from(input.0)) |
11572 | } |
11573 | } |
11574 | impl From<u8> for CursorEnum { |
11575 | #[inline ] |
11576 | fn from(value: u8) -> Self { |
11577 | Self(value) |
11578 | } |
11579 | } |
11580 | impl core::fmt::Debug for CursorEnum { |
11581 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
11582 | let variants: [(u32, &str, &str); 1] = [ |
11583 | (Self::NONE.0.into(), "NONE" , "None" ), |
11584 | ]; |
11585 | pretty_print_enum(fmt, self.0.into(), &variants) |
11586 | } |
11587 | } |
11588 | |
11589 | /// Opcode for the GrabPointer request |
11590 | pub const GRAB_POINTER_REQUEST: u8 = 26; |
11591 | /// Grab the pointer. |
11592 | /// |
11593 | /// Actively grabs control of the pointer. Further pointer events are reported only to the grabbing client. Overrides any active pointer grab by this client. |
11594 | /// |
11595 | /// # Fields |
11596 | /// |
11597 | /// * `event_mask` - Specifies which pointer events are reported to the client. |
11598 | /// |
11599 | /// TODO: which values? |
11600 | /// * `confine_to` - Specifies the window to confine the pointer in (the user will not be able to |
11601 | /// move the pointer out of that window). |
11602 | /// |
11603 | /// The special value `XCB_NONE` means don't confine the pointer. |
11604 | /// * `cursor` - Specifies the cursor that should be displayed or `XCB_NONE` to not change the |
11605 | /// cursor. |
11606 | /// * `owner_events` - If 1, the `grab_window` will still get the pointer events. If 0, events are not |
11607 | /// reported to the `grab_window`. |
11608 | /// * `grab_window` - Specifies the window on which the pointer should be grabbed. |
11609 | /// * `time` - The time argument allows you to avoid certain circumstances that come up if |
11610 | /// applications take a long time to respond or if there are long network delays. |
11611 | /// Consider a situation where you have two applications, both of which normally |
11612 | /// grab the pointer when clicked on. If both applications specify the timestamp |
11613 | /// from the event, the second application may wake up faster and successfully grab |
11614 | /// the pointer before the first application. The first application then will get |
11615 | /// an indication that the other application grabbed the pointer before its request |
11616 | /// was processed. |
11617 | /// |
11618 | /// The special value `XCB_CURRENT_TIME` will be replaced with the current server |
11619 | /// time. |
11620 | /// * `pointer_mode` - |
11621 | /// * `keyboard_mode` - |
11622 | /// |
11623 | /// # Errors |
11624 | /// |
11625 | /// * `Value` - TODO: reasons? |
11626 | /// * `Window` - The specified `window` does not exist. |
11627 | /// |
11628 | /// # See |
11629 | /// |
11630 | /// * `GrabKeyboard`: request |
11631 | /// |
11632 | /// # Example |
11633 | /// |
11634 | /// ```text |
11635 | /// /* |
11636 | /// * Grabs the pointer actively |
11637 | /// * |
11638 | /// */ |
11639 | /// void my_example(xcb_connection_t *conn, xcb_screen_t *screen, xcb_cursor_t cursor) { |
11640 | /// xcb_grab_pointer_cookie_t cookie; |
11641 | /// xcb_grab_pointer_reply_t *reply; |
11642 | /// |
11643 | /// cookie = xcb_grab_pointer( |
11644 | /// conn, |
11645 | /// false, /* get all pointer events specified by the following mask */ |
11646 | /// screen->root, /* grab the root window */ |
11647 | /// XCB_NONE, /* which events to let through */ |
11648 | /// XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */ |
11649 | /// XCB_GRAB_MODE_ASYNC, /* keyboard mode */ |
11650 | /// XCB_NONE, /* confine_to = in which window should the cursor stay */ |
11651 | /// cursor, /* we change the cursor to whatever the user wanted */ |
11652 | /// XCB_CURRENT_TIME |
11653 | /// ); |
11654 | /// |
11655 | /// if ((reply = xcb_grab_pointer_reply(conn, cookie, NULL))) { |
11656 | /// if (reply->status == XCB_GRAB_STATUS_SUCCESS) |
11657 | /// printf("successfully grabbed the pointer\\n"); |
11658 | /// free(reply); |
11659 | /// } |
11660 | /// } |
11661 | /// ``` |
11662 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
11663 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
11664 | pub struct GrabPointerRequest { |
11665 | pub owner_events: bool, |
11666 | pub grab_window: Window, |
11667 | pub event_mask: EventMask, |
11668 | pub pointer_mode: GrabMode, |
11669 | pub keyboard_mode: GrabMode, |
11670 | pub confine_to: Window, |
11671 | pub cursor: Cursor, |
11672 | pub time: Timestamp, |
11673 | } |
11674 | impl GrabPointerRequest { |
11675 | /// Serialize this request into bytes for the provided connection |
11676 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
11677 | let length_so_far = 0; |
11678 | let owner_events_bytes = self.owner_events.serialize(); |
11679 | let grab_window_bytes = self.grab_window.serialize(); |
11680 | let event_mask_bytes = (u32::from(self.event_mask) as u16).serialize(); |
11681 | let pointer_mode_bytes = u8::from(self.pointer_mode).serialize(); |
11682 | let keyboard_mode_bytes = u8::from(self.keyboard_mode).serialize(); |
11683 | let confine_to_bytes = self.confine_to.serialize(); |
11684 | let cursor_bytes = self.cursor.serialize(); |
11685 | let time_bytes = self.time.serialize(); |
11686 | let mut request0 = vec![ |
11687 | GRAB_POINTER_REQUEST, |
11688 | owner_events_bytes[0], |
11689 | 0, |
11690 | 0, |
11691 | grab_window_bytes[0], |
11692 | grab_window_bytes[1], |
11693 | grab_window_bytes[2], |
11694 | grab_window_bytes[3], |
11695 | event_mask_bytes[0], |
11696 | event_mask_bytes[1], |
11697 | pointer_mode_bytes[0], |
11698 | keyboard_mode_bytes[0], |
11699 | confine_to_bytes[0], |
11700 | confine_to_bytes[1], |
11701 | confine_to_bytes[2], |
11702 | confine_to_bytes[3], |
11703 | cursor_bytes[0], |
11704 | cursor_bytes[1], |
11705 | cursor_bytes[2], |
11706 | cursor_bytes[3], |
11707 | time_bytes[0], |
11708 | time_bytes[1], |
11709 | time_bytes[2], |
11710 | time_bytes[3], |
11711 | ]; |
11712 | let length_so_far = length_so_far + request0.len(); |
11713 | assert_eq!(length_so_far % 4, 0); |
11714 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
11715 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
11716 | (vec![request0.into()], vec![]) |
11717 | } |
11718 | /// Parse this request given its header, its body, and any fds that go along with it |
11719 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
11720 | if header.major_opcode != GRAB_POINTER_REQUEST { |
11721 | return Err(ParseError::InvalidValue); |
11722 | } |
11723 | let remaining = &[header.minor_opcode]; |
11724 | let (owner_events, remaining) = bool::try_parse(remaining)?; |
11725 | let _ = remaining; |
11726 | let (grab_window, remaining) = Window::try_parse(value)?; |
11727 | let (event_mask, remaining) = u16::try_parse(remaining)?; |
11728 | let event_mask = event_mask.into(); |
11729 | let (pointer_mode, remaining) = u8::try_parse(remaining)?; |
11730 | let pointer_mode = pointer_mode.into(); |
11731 | let (keyboard_mode, remaining) = u8::try_parse(remaining)?; |
11732 | let keyboard_mode = keyboard_mode.into(); |
11733 | let (confine_to, remaining) = Window::try_parse(remaining)?; |
11734 | let (cursor, remaining) = Cursor::try_parse(remaining)?; |
11735 | let (time, remaining) = Timestamp::try_parse(remaining)?; |
11736 | let _ = remaining; |
11737 | Ok(GrabPointerRequest { |
11738 | owner_events, |
11739 | grab_window, |
11740 | event_mask, |
11741 | pointer_mode, |
11742 | keyboard_mode, |
11743 | confine_to, |
11744 | cursor, |
11745 | time, |
11746 | }) |
11747 | } |
11748 | } |
11749 | impl Request for GrabPointerRequest { |
11750 | const EXTENSION_NAME: Option<&'static str> = None; |
11751 | |
11752 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
11753 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
11754 | // Flatten the buffers into a single vector |
11755 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
11756 | (buf, fds) |
11757 | } |
11758 | } |
11759 | impl crate::x11_utils::ReplyRequest for GrabPointerRequest { |
11760 | type Reply = GrabPointerReply; |
11761 | } |
11762 | |
11763 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
11764 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
11765 | pub struct GrabPointerReply { |
11766 | pub status: GrabStatus, |
11767 | pub sequence: u16, |
11768 | pub length: u32, |
11769 | } |
11770 | impl TryParse for GrabPointerReply { |
11771 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
11772 | let remaining: &[u8] = initial_value; |
11773 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
11774 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
11775 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
11776 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
11777 | if response_type != 1 { |
11778 | return Err(ParseError::InvalidValue); |
11779 | } |
11780 | let status: GrabStatus = status.into(); |
11781 | let result: GrabPointerReply = GrabPointerReply { status, sequence, length }; |
11782 | let _ = remaining; |
11783 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
11784 | .ok_or(err:ParseError::InsufficientData)?; |
11785 | Ok((result, remaining)) |
11786 | } |
11787 | } |
11788 | impl Serialize for GrabPointerReply { |
11789 | type Bytes = [u8; 8]; |
11790 | fn serialize(&self) -> [u8; 8] { |
11791 | let response_type_bytes = &[1]; |
11792 | let status_bytes = u8::from(self.status).serialize(); |
11793 | let sequence_bytes = self.sequence.serialize(); |
11794 | let length_bytes = self.length.serialize(); |
11795 | [ |
11796 | response_type_bytes[0], |
11797 | status_bytes[0], |
11798 | sequence_bytes[0], |
11799 | sequence_bytes[1], |
11800 | length_bytes[0], |
11801 | length_bytes[1], |
11802 | length_bytes[2], |
11803 | length_bytes[3], |
11804 | ] |
11805 | } |
11806 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
11807 | bytes.reserve(8); |
11808 | let response_type_bytes = &[1]; |
11809 | bytes.push(response_type_bytes[0]); |
11810 | u8::from(self.status).serialize_into(bytes); |
11811 | self.sequence.serialize_into(bytes); |
11812 | self.length.serialize_into(bytes); |
11813 | } |
11814 | } |
11815 | |
11816 | /// Opcode for the UngrabPointer request |
11817 | pub const UNGRAB_POINTER_REQUEST: u8 = 27; |
11818 | /// release the pointer. |
11819 | /// |
11820 | /// Releases the pointer and any queued events if you actively grabbed the pointer |
11821 | /// before using `xcb_grab_pointer`, `xcb_grab_button` or within a normal button |
11822 | /// press. |
11823 | /// |
11824 | /// EnterNotify and LeaveNotify events are generated. |
11825 | /// |
11826 | /// # Fields |
11827 | /// |
11828 | /// * `time` - Timestamp to avoid race conditions when running X over the network. |
11829 | /// |
11830 | /// The pointer will not be released if `time` is earlier than the |
11831 | /// last-pointer-grab time or later than the current X server time. |
11832 | /// * `name_len` - Length (in bytes) of `name`. |
11833 | /// * `name` - A pattern describing an X core font. |
11834 | /// |
11835 | /// # See |
11836 | /// |
11837 | /// * `GrabPointer`: request |
11838 | /// * `GrabButton`: request |
11839 | /// * `EnterNotify`: event |
11840 | /// * `LeaveNotify`: event |
11841 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
11842 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
11843 | pub struct UngrabPointerRequest { |
11844 | pub time: Timestamp, |
11845 | } |
11846 | impl UngrabPointerRequest { |
11847 | /// Serialize this request into bytes for the provided connection |
11848 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
11849 | let length_so_far = 0; |
11850 | let time_bytes = self.time.serialize(); |
11851 | let mut request0 = vec![ |
11852 | UNGRAB_POINTER_REQUEST, |
11853 | 0, |
11854 | 0, |
11855 | 0, |
11856 | time_bytes[0], |
11857 | time_bytes[1], |
11858 | time_bytes[2], |
11859 | time_bytes[3], |
11860 | ]; |
11861 | let length_so_far = length_so_far + request0.len(); |
11862 | assert_eq!(length_so_far % 4, 0); |
11863 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
11864 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
11865 | (vec![request0.into()], vec![]) |
11866 | } |
11867 | /// Parse this request given its header, its body, and any fds that go along with it |
11868 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
11869 | if header.major_opcode != UNGRAB_POINTER_REQUEST { |
11870 | return Err(ParseError::InvalidValue); |
11871 | } |
11872 | let remaining = &[header.minor_opcode]; |
11873 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
11874 | let _ = remaining; |
11875 | let (time, remaining) = Timestamp::try_parse(value)?; |
11876 | let _ = remaining; |
11877 | Ok(UngrabPointerRequest { |
11878 | time, |
11879 | }) |
11880 | } |
11881 | } |
11882 | impl Request for UngrabPointerRequest { |
11883 | const EXTENSION_NAME: Option<&'static str> = None; |
11884 | |
11885 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
11886 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
11887 | // Flatten the buffers into a single vector |
11888 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
11889 | (buf, fds) |
11890 | } |
11891 | } |
11892 | impl crate::x11_utils::VoidRequest for UngrabPointerRequest { |
11893 | } |
11894 | |
11895 | /// # Fields |
11896 | /// |
11897 | /// * `Any` - Any of the following (or none): |
11898 | /// * `1` - The left mouse button. |
11899 | /// * `2` - The right mouse button. |
11900 | /// * `3` - The middle mouse button. |
11901 | /// * `4` - Scroll wheel. TODO: direction? |
11902 | /// * `5` - Scroll wheel. TODO: direction? |
11903 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
11904 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
11905 | pub struct ButtonIndex(u8); |
11906 | impl ButtonIndex { |
11907 | pub const ANY: Self = Self(0); |
11908 | pub const M1: Self = Self(1); |
11909 | pub const M2: Self = Self(2); |
11910 | pub const M3: Self = Self(3); |
11911 | pub const M4: Self = Self(4); |
11912 | pub const M5: Self = Self(5); |
11913 | } |
11914 | impl From<ButtonIndex> for u8 { |
11915 | #[inline ] |
11916 | fn from(input: ButtonIndex) -> Self { |
11917 | input.0 |
11918 | } |
11919 | } |
11920 | impl From<ButtonIndex> for Option<u8> { |
11921 | #[inline ] |
11922 | fn from(input: ButtonIndex) -> Self { |
11923 | Some(input.0) |
11924 | } |
11925 | } |
11926 | impl From<ButtonIndex> for u16 { |
11927 | #[inline ] |
11928 | fn from(input: ButtonIndex) -> Self { |
11929 | u16::from(input.0) |
11930 | } |
11931 | } |
11932 | impl From<ButtonIndex> for Option<u16> { |
11933 | #[inline ] |
11934 | fn from(input: ButtonIndex) -> Self { |
11935 | Some(u16::from(input.0)) |
11936 | } |
11937 | } |
11938 | impl From<ButtonIndex> for u32 { |
11939 | #[inline ] |
11940 | fn from(input: ButtonIndex) -> Self { |
11941 | u32::from(input.0) |
11942 | } |
11943 | } |
11944 | impl From<ButtonIndex> for Option<u32> { |
11945 | #[inline ] |
11946 | fn from(input: ButtonIndex) -> Self { |
11947 | Some(u32::from(input.0)) |
11948 | } |
11949 | } |
11950 | impl From<u8> for ButtonIndex { |
11951 | #[inline ] |
11952 | fn from(value: u8) -> Self { |
11953 | Self(value) |
11954 | } |
11955 | } |
11956 | impl core::fmt::Debug for ButtonIndex { |
11957 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
11958 | let variants: [(u32, &str, &str); 6] = [ |
11959 | (Self::ANY.0.into(), "ANY" , "Any" ), |
11960 | (Self::M1.0.into(), "M1" , "M1" ), |
11961 | (Self::M2.0.into(), "M2" , "M2" ), |
11962 | (Self::M3.0.into(), "M3" , "M3" ), |
11963 | (Self::M4.0.into(), "M4" , "M4" ), |
11964 | (Self::M5.0.into(), "M5" , "M5" ), |
11965 | ]; |
11966 | pretty_print_enum(fmt, self.0.into(), &variants) |
11967 | } |
11968 | } |
11969 | |
11970 | /// Opcode for the GrabButton request |
11971 | pub const GRAB_BUTTON_REQUEST: u8 = 28; |
11972 | /// Grab pointer button(s). |
11973 | /// |
11974 | /// This request establishes a passive grab. The pointer is actively grabbed as |
11975 | /// described in GrabPointer, the last-pointer-grab time is set to the time at |
11976 | /// which the button was pressed (as transmitted in the ButtonPress event), and the |
11977 | /// ButtonPress event is reported if all of the following conditions are true: |
11978 | /// |
11979 | /// The pointer is not grabbed and the specified button is logically pressed when |
11980 | /// the specified modifier keys are logically down, and no other buttons or |
11981 | /// modifier keys are logically down. |
11982 | /// |
11983 | /// The grab-window contains the pointer. |
11984 | /// |
11985 | /// The confine-to window (if any) is viewable. |
11986 | /// |
11987 | /// A passive grab on the same button/key combination does not exist on any |
11988 | /// ancestor of grab-window. |
11989 | /// |
11990 | /// The interpretation of the remaining arguments is the same as for GrabPointer. |
11991 | /// The active grab is terminated automatically when the logical state of the |
11992 | /// pointer has all buttons released, independent of the logical state of modifier |
11993 | /// keys. Note that the logical state of a device (as seen by means of the |
11994 | /// protocol) may lag the physical state if device event processing is frozen. This |
11995 | /// request overrides all previous passive grabs by the same client on the same |
11996 | /// button/key combinations on the same window. A modifier of AnyModifier is |
11997 | /// equivalent to issuing the request for all possible modifier combinations |
11998 | /// (including the combination of no modifiers). It is not required that all |
11999 | /// specified modifiers have currently assigned keycodes. A button of AnyButton is |
12000 | /// equivalent to issuing the request for all possible buttons. Otherwise, it is |
12001 | /// not required that the button specified currently be assigned to a physical |
12002 | /// button. |
12003 | /// |
12004 | /// An Access error is generated if some other client has already issued a |
12005 | /// GrabButton request with the same button/key combination on the same window. |
12006 | /// When using AnyModifier or AnyButton, the request fails completely (no grabs are |
12007 | /// established), and an Access error is generated if there is a conflicting grab |
12008 | /// for any combination. The request has no effect on an active grab. |
12009 | /// |
12010 | /// # Fields |
12011 | /// |
12012 | /// * `owner_events` - If 1, the `grab_window` will still get the pointer events. If 0, events are not |
12013 | /// reported to the `grab_window`. |
12014 | /// * `grab_window` - Specifies the window on which the pointer should be grabbed. |
12015 | /// * `event_mask` - Specifies which pointer events are reported to the client. |
12016 | /// |
12017 | /// TODO: which values? |
12018 | /// * `confine_to` - Specifies the window to confine the pointer in (the user will not be able to |
12019 | /// move the pointer out of that window). |
12020 | /// |
12021 | /// The special value `XCB_NONE` means don't confine the pointer. |
12022 | /// * `cursor` - Specifies the cursor that should be displayed or `XCB_NONE` to not change the |
12023 | /// cursor. |
12024 | /// * `modifiers` - The modifiers to grab. |
12025 | /// |
12026 | /// Using the special value `XCB_MOD_MASK_ANY` means grab the pointer with all |
12027 | /// possible modifier combinations. |
12028 | /// * `pointer_mode` - |
12029 | /// * `keyboard_mode` - |
12030 | /// * `button` - |
12031 | /// |
12032 | /// # Errors |
12033 | /// |
12034 | /// * `Access` - Another client has already issued a GrabButton with the same button/key |
12035 | /// combination on the same window. |
12036 | /// * `Value` - TODO: reasons? |
12037 | /// * `Cursor` - The specified `cursor` does not exist. |
12038 | /// * `Window` - The specified `window` does not exist. |
12039 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
12040 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
12041 | pub struct GrabButtonRequest { |
12042 | pub owner_events: bool, |
12043 | pub grab_window: Window, |
12044 | pub event_mask: EventMask, |
12045 | pub pointer_mode: GrabMode, |
12046 | pub keyboard_mode: GrabMode, |
12047 | pub confine_to: Window, |
12048 | pub cursor: Cursor, |
12049 | pub button: ButtonIndex, |
12050 | pub modifiers: ModMask, |
12051 | } |
12052 | impl GrabButtonRequest { |
12053 | /// Serialize this request into bytes for the provided connection |
12054 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
12055 | let length_so_far = 0; |
12056 | let owner_events_bytes = self.owner_events.serialize(); |
12057 | let grab_window_bytes = self.grab_window.serialize(); |
12058 | let event_mask_bytes = (u32::from(self.event_mask) as u16).serialize(); |
12059 | let pointer_mode_bytes = u8::from(self.pointer_mode).serialize(); |
12060 | let keyboard_mode_bytes = u8::from(self.keyboard_mode).serialize(); |
12061 | let confine_to_bytes = self.confine_to.serialize(); |
12062 | let cursor_bytes = self.cursor.serialize(); |
12063 | let button_bytes = u8::from(self.button).serialize(); |
12064 | let modifiers_bytes = u16::from(self.modifiers).serialize(); |
12065 | let mut request0 = vec![ |
12066 | GRAB_BUTTON_REQUEST, |
12067 | owner_events_bytes[0], |
12068 | 0, |
12069 | 0, |
12070 | grab_window_bytes[0], |
12071 | grab_window_bytes[1], |
12072 | grab_window_bytes[2], |
12073 | grab_window_bytes[3], |
12074 | event_mask_bytes[0], |
12075 | event_mask_bytes[1], |
12076 | pointer_mode_bytes[0], |
12077 | keyboard_mode_bytes[0], |
12078 | confine_to_bytes[0], |
12079 | confine_to_bytes[1], |
12080 | confine_to_bytes[2], |
12081 | confine_to_bytes[3], |
12082 | cursor_bytes[0], |
12083 | cursor_bytes[1], |
12084 | cursor_bytes[2], |
12085 | cursor_bytes[3], |
12086 | button_bytes[0], |
12087 | 0, |
12088 | modifiers_bytes[0], |
12089 | modifiers_bytes[1], |
12090 | ]; |
12091 | let length_so_far = length_so_far + request0.len(); |
12092 | assert_eq!(length_so_far % 4, 0); |
12093 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
12094 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
12095 | (vec![request0.into()], vec![]) |
12096 | } |
12097 | /// Parse this request given its header, its body, and any fds that go along with it |
12098 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
12099 | if header.major_opcode != GRAB_BUTTON_REQUEST { |
12100 | return Err(ParseError::InvalidValue); |
12101 | } |
12102 | let remaining = &[header.minor_opcode]; |
12103 | let (owner_events, remaining) = bool::try_parse(remaining)?; |
12104 | let _ = remaining; |
12105 | let (grab_window, remaining) = Window::try_parse(value)?; |
12106 | let (event_mask, remaining) = u16::try_parse(remaining)?; |
12107 | let event_mask = event_mask.into(); |
12108 | let (pointer_mode, remaining) = u8::try_parse(remaining)?; |
12109 | let pointer_mode = pointer_mode.into(); |
12110 | let (keyboard_mode, remaining) = u8::try_parse(remaining)?; |
12111 | let keyboard_mode = keyboard_mode.into(); |
12112 | let (confine_to, remaining) = Window::try_parse(remaining)?; |
12113 | let (cursor, remaining) = Cursor::try_parse(remaining)?; |
12114 | let (button, remaining) = u8::try_parse(remaining)?; |
12115 | let button = button.into(); |
12116 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
12117 | let (modifiers, remaining) = u16::try_parse(remaining)?; |
12118 | let modifiers = modifiers.into(); |
12119 | let _ = remaining; |
12120 | Ok(GrabButtonRequest { |
12121 | owner_events, |
12122 | grab_window, |
12123 | event_mask, |
12124 | pointer_mode, |
12125 | keyboard_mode, |
12126 | confine_to, |
12127 | cursor, |
12128 | button, |
12129 | modifiers, |
12130 | }) |
12131 | } |
12132 | } |
12133 | impl Request for GrabButtonRequest { |
12134 | const EXTENSION_NAME: Option<&'static str> = None; |
12135 | |
12136 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
12137 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
12138 | // Flatten the buffers into a single vector |
12139 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
12140 | (buf, fds) |
12141 | } |
12142 | } |
12143 | impl crate::x11_utils::VoidRequest for GrabButtonRequest { |
12144 | } |
12145 | |
12146 | /// Opcode for the UngrabButton request |
12147 | pub const UNGRAB_BUTTON_REQUEST: u8 = 29; |
12148 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
12149 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
12150 | pub struct UngrabButtonRequest { |
12151 | pub button: ButtonIndex, |
12152 | pub grab_window: Window, |
12153 | pub modifiers: ModMask, |
12154 | } |
12155 | impl UngrabButtonRequest { |
12156 | /// Serialize this request into bytes for the provided connection |
12157 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
12158 | let length_so_far = 0; |
12159 | let button_bytes = u8::from(self.button).serialize(); |
12160 | let grab_window_bytes = self.grab_window.serialize(); |
12161 | let modifiers_bytes = u16::from(self.modifiers).serialize(); |
12162 | let mut request0 = vec![ |
12163 | UNGRAB_BUTTON_REQUEST, |
12164 | button_bytes[0], |
12165 | 0, |
12166 | 0, |
12167 | grab_window_bytes[0], |
12168 | grab_window_bytes[1], |
12169 | grab_window_bytes[2], |
12170 | grab_window_bytes[3], |
12171 | modifiers_bytes[0], |
12172 | modifiers_bytes[1], |
12173 | 0, |
12174 | 0, |
12175 | ]; |
12176 | let length_so_far = length_so_far + request0.len(); |
12177 | assert_eq!(length_so_far % 4, 0); |
12178 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
12179 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
12180 | (vec![request0.into()], vec![]) |
12181 | } |
12182 | /// Parse this request given its header, its body, and any fds that go along with it |
12183 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
12184 | if header.major_opcode != UNGRAB_BUTTON_REQUEST { |
12185 | return Err(ParseError::InvalidValue); |
12186 | } |
12187 | let remaining = &[header.minor_opcode]; |
12188 | let (button, remaining) = u8::try_parse(remaining)?; |
12189 | let button = button.into(); |
12190 | let _ = remaining; |
12191 | let (grab_window, remaining) = Window::try_parse(value)?; |
12192 | let (modifiers, remaining) = u16::try_parse(remaining)?; |
12193 | let modifiers = modifiers.into(); |
12194 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
12195 | let _ = remaining; |
12196 | Ok(UngrabButtonRequest { |
12197 | button, |
12198 | grab_window, |
12199 | modifiers, |
12200 | }) |
12201 | } |
12202 | } |
12203 | impl Request for UngrabButtonRequest { |
12204 | const EXTENSION_NAME: Option<&'static str> = None; |
12205 | |
12206 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
12207 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
12208 | // Flatten the buffers into a single vector |
12209 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
12210 | (buf, fds) |
12211 | } |
12212 | } |
12213 | impl crate::x11_utils::VoidRequest for UngrabButtonRequest { |
12214 | } |
12215 | |
12216 | /// Opcode for the ChangeActivePointerGrab request |
12217 | pub const CHANGE_ACTIVE_POINTER_GRAB_REQUEST: u8 = 30; |
12218 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
12219 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
12220 | pub struct ChangeActivePointerGrabRequest { |
12221 | pub cursor: Cursor, |
12222 | pub time: Timestamp, |
12223 | pub event_mask: EventMask, |
12224 | } |
12225 | impl ChangeActivePointerGrabRequest { |
12226 | /// Serialize this request into bytes for the provided connection |
12227 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
12228 | let length_so_far = 0; |
12229 | let cursor_bytes = self.cursor.serialize(); |
12230 | let time_bytes = self.time.serialize(); |
12231 | let event_mask_bytes = (u32::from(self.event_mask) as u16).serialize(); |
12232 | let mut request0 = vec![ |
12233 | CHANGE_ACTIVE_POINTER_GRAB_REQUEST, |
12234 | 0, |
12235 | 0, |
12236 | 0, |
12237 | cursor_bytes[0], |
12238 | cursor_bytes[1], |
12239 | cursor_bytes[2], |
12240 | cursor_bytes[3], |
12241 | time_bytes[0], |
12242 | time_bytes[1], |
12243 | time_bytes[2], |
12244 | time_bytes[3], |
12245 | event_mask_bytes[0], |
12246 | event_mask_bytes[1], |
12247 | 0, |
12248 | 0, |
12249 | ]; |
12250 | let length_so_far = length_so_far + request0.len(); |
12251 | assert_eq!(length_so_far % 4, 0); |
12252 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
12253 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
12254 | (vec![request0.into()], vec![]) |
12255 | } |
12256 | /// Parse this request given its header, its body, and any fds that go along with it |
12257 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
12258 | if header.major_opcode != CHANGE_ACTIVE_POINTER_GRAB_REQUEST { |
12259 | return Err(ParseError::InvalidValue); |
12260 | } |
12261 | let remaining = &[header.minor_opcode]; |
12262 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
12263 | let _ = remaining; |
12264 | let (cursor, remaining) = Cursor::try_parse(value)?; |
12265 | let (time, remaining) = Timestamp::try_parse(remaining)?; |
12266 | let (event_mask, remaining) = u16::try_parse(remaining)?; |
12267 | let event_mask = event_mask.into(); |
12268 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
12269 | let _ = remaining; |
12270 | Ok(ChangeActivePointerGrabRequest { |
12271 | cursor, |
12272 | time, |
12273 | event_mask, |
12274 | }) |
12275 | } |
12276 | } |
12277 | impl Request for ChangeActivePointerGrabRequest { |
12278 | const EXTENSION_NAME: Option<&'static str> = None; |
12279 | |
12280 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
12281 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
12282 | // Flatten the buffers into a single vector |
12283 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
12284 | (buf, fds) |
12285 | } |
12286 | } |
12287 | impl crate::x11_utils::VoidRequest for ChangeActivePointerGrabRequest { |
12288 | } |
12289 | |
12290 | /// Opcode for the GrabKeyboard request |
12291 | pub const GRAB_KEYBOARD_REQUEST: u8 = 31; |
12292 | /// Grab the keyboard. |
12293 | /// |
12294 | /// Actively grabs control of the keyboard and generates FocusIn and FocusOut |
12295 | /// events. Further key events are reported only to the grabbing client. |
12296 | /// |
12297 | /// Any active keyboard grab by this client is overridden. If the keyboard is |
12298 | /// actively grabbed by some other client, `AlreadyGrabbed` is returned. If |
12299 | /// `grab_window` is not viewable, `GrabNotViewable` is returned. If the keyboard |
12300 | /// is frozen by an active grab of another client, `GrabFrozen` is returned. If the |
12301 | /// specified `time` is earlier than the last-keyboard-grab time or later than the |
12302 | /// current X server time, `GrabInvalidTime` is returned. Otherwise, the |
12303 | /// last-keyboard-grab time is set to the specified time. |
12304 | /// |
12305 | /// # Fields |
12306 | /// |
12307 | /// * `owner_events` - If 1, the `grab_window` will still get the pointer events. If 0, events are not |
12308 | /// reported to the `grab_window`. |
12309 | /// * `grab_window` - Specifies the window on which the pointer should be grabbed. |
12310 | /// * `time` - Timestamp to avoid race conditions when running X over the network. |
12311 | /// |
12312 | /// The special value `XCB_CURRENT_TIME` will be replaced with the current server |
12313 | /// time. |
12314 | /// * `pointer_mode` - |
12315 | /// * `keyboard_mode` - |
12316 | /// |
12317 | /// # Errors |
12318 | /// |
12319 | /// * `Value` - TODO: reasons? |
12320 | /// * `Window` - The specified `window` does not exist. |
12321 | /// |
12322 | /// # See |
12323 | /// |
12324 | /// * `GrabPointer`: request |
12325 | /// |
12326 | /// # Example |
12327 | /// |
12328 | /// ```text |
12329 | /// /* |
12330 | /// * Grabs the keyboard actively |
12331 | /// * |
12332 | /// */ |
12333 | /// void my_example(xcb_connection_t *conn, xcb_screen_t *screen) { |
12334 | /// xcb_grab_keyboard_cookie_t cookie; |
12335 | /// xcb_grab_keyboard_reply_t *reply; |
12336 | /// |
12337 | /// cookie = xcb_grab_keyboard( |
12338 | /// conn, |
12339 | /// true, /* report events */ |
12340 | /// screen->root, /* grab the root window */ |
12341 | /// XCB_CURRENT_TIME, |
12342 | /// XCB_GRAB_MODE_ASYNC, /* process events as normal, do not require sync */ |
12343 | /// XCB_GRAB_MODE_ASYNC |
12344 | /// ); |
12345 | /// |
12346 | /// if ((reply = xcb_grab_keyboard_reply(conn, cookie, NULL))) { |
12347 | /// if (reply->status == XCB_GRAB_STATUS_SUCCESS) |
12348 | /// printf("successfully grabbed the keyboard\\n"); |
12349 | /// |
12350 | /// free(reply); |
12351 | /// } |
12352 | /// } |
12353 | /// ``` |
12354 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
12355 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
12356 | pub struct GrabKeyboardRequest { |
12357 | pub owner_events: bool, |
12358 | pub grab_window: Window, |
12359 | pub time: Timestamp, |
12360 | pub pointer_mode: GrabMode, |
12361 | pub keyboard_mode: GrabMode, |
12362 | } |
12363 | impl GrabKeyboardRequest { |
12364 | /// Serialize this request into bytes for the provided connection |
12365 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
12366 | let length_so_far = 0; |
12367 | let owner_events_bytes = self.owner_events.serialize(); |
12368 | let grab_window_bytes = self.grab_window.serialize(); |
12369 | let time_bytes = self.time.serialize(); |
12370 | let pointer_mode_bytes = u8::from(self.pointer_mode).serialize(); |
12371 | let keyboard_mode_bytes = u8::from(self.keyboard_mode).serialize(); |
12372 | let mut request0 = vec![ |
12373 | GRAB_KEYBOARD_REQUEST, |
12374 | owner_events_bytes[0], |
12375 | 0, |
12376 | 0, |
12377 | grab_window_bytes[0], |
12378 | grab_window_bytes[1], |
12379 | grab_window_bytes[2], |
12380 | grab_window_bytes[3], |
12381 | time_bytes[0], |
12382 | time_bytes[1], |
12383 | time_bytes[2], |
12384 | time_bytes[3], |
12385 | pointer_mode_bytes[0], |
12386 | keyboard_mode_bytes[0], |
12387 | 0, |
12388 | 0, |
12389 | ]; |
12390 | let length_so_far = length_so_far + request0.len(); |
12391 | assert_eq!(length_so_far % 4, 0); |
12392 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
12393 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
12394 | (vec![request0.into()], vec![]) |
12395 | } |
12396 | /// Parse this request given its header, its body, and any fds that go along with it |
12397 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
12398 | if header.major_opcode != GRAB_KEYBOARD_REQUEST { |
12399 | return Err(ParseError::InvalidValue); |
12400 | } |
12401 | let remaining = &[header.minor_opcode]; |
12402 | let (owner_events, remaining) = bool::try_parse(remaining)?; |
12403 | let _ = remaining; |
12404 | let (grab_window, remaining) = Window::try_parse(value)?; |
12405 | let (time, remaining) = Timestamp::try_parse(remaining)?; |
12406 | let (pointer_mode, remaining) = u8::try_parse(remaining)?; |
12407 | let pointer_mode = pointer_mode.into(); |
12408 | let (keyboard_mode, remaining) = u8::try_parse(remaining)?; |
12409 | let keyboard_mode = keyboard_mode.into(); |
12410 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
12411 | let _ = remaining; |
12412 | Ok(GrabKeyboardRequest { |
12413 | owner_events, |
12414 | grab_window, |
12415 | time, |
12416 | pointer_mode, |
12417 | keyboard_mode, |
12418 | }) |
12419 | } |
12420 | } |
12421 | impl Request for GrabKeyboardRequest { |
12422 | const EXTENSION_NAME: Option<&'static str> = None; |
12423 | |
12424 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
12425 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
12426 | // Flatten the buffers into a single vector |
12427 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
12428 | (buf, fds) |
12429 | } |
12430 | } |
12431 | impl crate::x11_utils::ReplyRequest for GrabKeyboardRequest { |
12432 | type Reply = GrabKeyboardReply; |
12433 | } |
12434 | |
12435 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
12436 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
12437 | pub struct GrabKeyboardReply { |
12438 | pub status: GrabStatus, |
12439 | pub sequence: u16, |
12440 | pub length: u32, |
12441 | } |
12442 | impl TryParse for GrabKeyboardReply { |
12443 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
12444 | let remaining: &[u8] = initial_value; |
12445 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
12446 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
12447 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
12448 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
12449 | if response_type != 1 { |
12450 | return Err(ParseError::InvalidValue); |
12451 | } |
12452 | let status: GrabStatus = status.into(); |
12453 | let result: GrabKeyboardReply = GrabKeyboardReply { status, sequence, length }; |
12454 | let _ = remaining; |
12455 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
12456 | .ok_or(err:ParseError::InsufficientData)?; |
12457 | Ok((result, remaining)) |
12458 | } |
12459 | } |
12460 | impl Serialize for GrabKeyboardReply { |
12461 | type Bytes = [u8; 8]; |
12462 | fn serialize(&self) -> [u8; 8] { |
12463 | let response_type_bytes = &[1]; |
12464 | let status_bytes = u8::from(self.status).serialize(); |
12465 | let sequence_bytes = self.sequence.serialize(); |
12466 | let length_bytes = self.length.serialize(); |
12467 | [ |
12468 | response_type_bytes[0], |
12469 | status_bytes[0], |
12470 | sequence_bytes[0], |
12471 | sequence_bytes[1], |
12472 | length_bytes[0], |
12473 | length_bytes[1], |
12474 | length_bytes[2], |
12475 | length_bytes[3], |
12476 | ] |
12477 | } |
12478 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
12479 | bytes.reserve(8); |
12480 | let response_type_bytes = &[1]; |
12481 | bytes.push(response_type_bytes[0]); |
12482 | u8::from(self.status).serialize_into(bytes); |
12483 | self.sequence.serialize_into(bytes); |
12484 | self.length.serialize_into(bytes); |
12485 | } |
12486 | } |
12487 | |
12488 | /// Opcode for the UngrabKeyboard request |
12489 | pub const UNGRAB_KEYBOARD_REQUEST: u8 = 32; |
12490 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
12491 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
12492 | pub struct UngrabKeyboardRequest { |
12493 | pub time: Timestamp, |
12494 | } |
12495 | impl UngrabKeyboardRequest { |
12496 | /// Serialize this request into bytes for the provided connection |
12497 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
12498 | let length_so_far = 0; |
12499 | let time_bytes = self.time.serialize(); |
12500 | let mut request0 = vec![ |
12501 | UNGRAB_KEYBOARD_REQUEST, |
12502 | 0, |
12503 | 0, |
12504 | 0, |
12505 | time_bytes[0], |
12506 | time_bytes[1], |
12507 | time_bytes[2], |
12508 | time_bytes[3], |
12509 | ]; |
12510 | let length_so_far = length_so_far + request0.len(); |
12511 | assert_eq!(length_so_far % 4, 0); |
12512 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
12513 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
12514 | (vec![request0.into()], vec![]) |
12515 | } |
12516 | /// Parse this request given its header, its body, and any fds that go along with it |
12517 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
12518 | if header.major_opcode != UNGRAB_KEYBOARD_REQUEST { |
12519 | return Err(ParseError::InvalidValue); |
12520 | } |
12521 | let remaining = &[header.minor_opcode]; |
12522 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
12523 | let _ = remaining; |
12524 | let (time, remaining) = Timestamp::try_parse(value)?; |
12525 | let _ = remaining; |
12526 | Ok(UngrabKeyboardRequest { |
12527 | time, |
12528 | }) |
12529 | } |
12530 | } |
12531 | impl Request for UngrabKeyboardRequest { |
12532 | const EXTENSION_NAME: Option<&'static str> = None; |
12533 | |
12534 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
12535 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
12536 | // Flatten the buffers into a single vector |
12537 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
12538 | (buf, fds) |
12539 | } |
12540 | } |
12541 | impl crate::x11_utils::VoidRequest for UngrabKeyboardRequest { |
12542 | } |
12543 | |
12544 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
12545 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
12546 | pub struct Grab(u8); |
12547 | impl Grab { |
12548 | pub const ANY: Self = Self(0); |
12549 | } |
12550 | impl From<Grab> for u8 { |
12551 | #[inline ] |
12552 | fn from(input: Grab) -> Self { |
12553 | input.0 |
12554 | } |
12555 | } |
12556 | impl From<Grab> for Option<u8> { |
12557 | #[inline ] |
12558 | fn from(input: Grab) -> Self { |
12559 | Some(input.0) |
12560 | } |
12561 | } |
12562 | impl From<Grab> for u16 { |
12563 | #[inline ] |
12564 | fn from(input: Grab) -> Self { |
12565 | u16::from(input.0) |
12566 | } |
12567 | } |
12568 | impl From<Grab> for Option<u16> { |
12569 | #[inline ] |
12570 | fn from(input: Grab) -> Self { |
12571 | Some(u16::from(input.0)) |
12572 | } |
12573 | } |
12574 | impl From<Grab> for u32 { |
12575 | #[inline ] |
12576 | fn from(input: Grab) -> Self { |
12577 | u32::from(input.0) |
12578 | } |
12579 | } |
12580 | impl From<Grab> for Option<u32> { |
12581 | #[inline ] |
12582 | fn from(input: Grab) -> Self { |
12583 | Some(u32::from(input.0)) |
12584 | } |
12585 | } |
12586 | impl From<u8> for Grab { |
12587 | #[inline ] |
12588 | fn from(value: u8) -> Self { |
12589 | Self(value) |
12590 | } |
12591 | } |
12592 | impl core::fmt::Debug for Grab { |
12593 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
12594 | let variants: [(u32, &str, &str); 1] = [ |
12595 | (Self::ANY.0.into(), "ANY" , "Any" ), |
12596 | ]; |
12597 | pretty_print_enum(fmt, self.0.into(), &variants) |
12598 | } |
12599 | } |
12600 | |
12601 | /// Opcode for the GrabKey request |
12602 | pub const GRAB_KEY_REQUEST: u8 = 33; |
12603 | /// Grab keyboard key(s). |
12604 | /// |
12605 | /// Establishes a passive grab on the keyboard. In the future, the keyboard is |
12606 | /// actively grabbed (as for `GrabKeyboard`), the last-keyboard-grab time is set to |
12607 | /// the time at which the key was pressed (as transmitted in the KeyPress event), |
12608 | /// and the KeyPress event is reported if all of the following conditions are true: |
12609 | /// |
12610 | /// The keyboard is not grabbed and the specified key (which can itself be a |
12611 | /// modifier key) is logically pressed when the specified modifier keys are |
12612 | /// logically down, and no other modifier keys are logically down. |
12613 | /// |
12614 | /// Either the grab_window is an ancestor of (or is) the focus window, or the |
12615 | /// grab_window is a descendant of the focus window and contains the pointer. |
12616 | /// |
12617 | /// A passive grab on the same key combination does not exist on any ancestor of |
12618 | /// grab_window. |
12619 | /// |
12620 | /// The interpretation of the remaining arguments is as for XGrabKeyboard. The active grab is terminated |
12621 | /// automatically when the logical state of the keyboard has the specified key released (independent of the |
12622 | /// logical state of the modifier keys), at which point a KeyRelease event is reported to the grabbing window. |
12623 | /// |
12624 | /// Note that the logical state of a device (as seen by client applications) may lag the physical state if |
12625 | /// device event processing is frozen. |
12626 | /// |
12627 | /// A modifiers argument of AnyModifier is equivalent to issuing the request for all possible modifier combinations (including the combination of no modifiers). It is not required that all modifiers specified |
12628 | /// have currently assigned KeyCodes. A keycode argument of AnyKey is equivalent to issuing the request for |
12629 | /// all possible KeyCodes. Otherwise, the specified keycode must be in the range specified by min_keycode |
12630 | /// and max_keycode in the connection setup, or a BadValue error results. |
12631 | /// |
12632 | /// If some other client has issued a XGrabKey with the same key combination on the same window, a BadAccess |
12633 | /// error results. When using AnyModifier or AnyKey, the request fails completely, and a BadAccess error |
12634 | /// results (no grabs are established) if there is a conflicting grab for any combination. |
12635 | /// |
12636 | /// # Fields |
12637 | /// |
12638 | /// * `owner_events` - If 1, the `grab_window` will still get the key events. If 0, events are not |
12639 | /// reported to the `grab_window`. |
12640 | /// * `grab_window` - Specifies the window on which the key should be grabbed. |
12641 | /// * `key` - The keycode of the key to grab. |
12642 | /// |
12643 | /// The special value `XCB_GRAB_ANY` means grab any key. |
12644 | /// * `modifiers` - The modifiers to grab. |
12645 | /// |
12646 | /// Using the special value `XCB_MOD_MASK_ANY` means grab the key with all |
12647 | /// possible modifier combinations. |
12648 | /// * `pointer_mode` - |
12649 | /// * `keyboard_mode` - |
12650 | /// |
12651 | /// # Errors |
12652 | /// |
12653 | /// * `Access` - Another client has already issued a GrabKey with the same button/key |
12654 | /// combination on the same window. |
12655 | /// * `Value` - The key is not `XCB_GRAB_ANY` and not in the range specified by `min_keycode` |
12656 | /// and `max_keycode` in the connection setup. |
12657 | /// * `Window` - The specified `window` does not exist. |
12658 | /// |
12659 | /// # See |
12660 | /// |
12661 | /// * `GrabKeyboard`: request |
12662 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
12663 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
12664 | pub struct GrabKeyRequest { |
12665 | pub owner_events: bool, |
12666 | pub grab_window: Window, |
12667 | pub modifiers: ModMask, |
12668 | pub key: Keycode, |
12669 | pub pointer_mode: GrabMode, |
12670 | pub keyboard_mode: GrabMode, |
12671 | } |
12672 | impl GrabKeyRequest { |
12673 | /// Serialize this request into bytes for the provided connection |
12674 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
12675 | let length_so_far = 0; |
12676 | let owner_events_bytes = self.owner_events.serialize(); |
12677 | let grab_window_bytes = self.grab_window.serialize(); |
12678 | let modifiers_bytes = u16::from(self.modifiers).serialize(); |
12679 | let key_bytes = self.key.serialize(); |
12680 | let pointer_mode_bytes = u8::from(self.pointer_mode).serialize(); |
12681 | let keyboard_mode_bytes = u8::from(self.keyboard_mode).serialize(); |
12682 | let mut request0 = vec![ |
12683 | GRAB_KEY_REQUEST, |
12684 | owner_events_bytes[0], |
12685 | 0, |
12686 | 0, |
12687 | grab_window_bytes[0], |
12688 | grab_window_bytes[1], |
12689 | grab_window_bytes[2], |
12690 | grab_window_bytes[3], |
12691 | modifiers_bytes[0], |
12692 | modifiers_bytes[1], |
12693 | key_bytes[0], |
12694 | pointer_mode_bytes[0], |
12695 | keyboard_mode_bytes[0], |
12696 | 0, |
12697 | 0, |
12698 | 0, |
12699 | ]; |
12700 | let length_so_far = length_so_far + request0.len(); |
12701 | assert_eq!(length_so_far % 4, 0); |
12702 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
12703 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
12704 | (vec![request0.into()], vec![]) |
12705 | } |
12706 | /// Parse this request given its header, its body, and any fds that go along with it |
12707 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
12708 | if header.major_opcode != GRAB_KEY_REQUEST { |
12709 | return Err(ParseError::InvalidValue); |
12710 | } |
12711 | let remaining = &[header.minor_opcode]; |
12712 | let (owner_events, remaining) = bool::try_parse(remaining)?; |
12713 | let _ = remaining; |
12714 | let (grab_window, remaining) = Window::try_parse(value)?; |
12715 | let (modifiers, remaining) = u16::try_parse(remaining)?; |
12716 | let modifiers = modifiers.into(); |
12717 | let (key, remaining) = Keycode::try_parse(remaining)?; |
12718 | let (pointer_mode, remaining) = u8::try_parse(remaining)?; |
12719 | let pointer_mode = pointer_mode.into(); |
12720 | let (keyboard_mode, remaining) = u8::try_parse(remaining)?; |
12721 | let keyboard_mode = keyboard_mode.into(); |
12722 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
12723 | let _ = remaining; |
12724 | Ok(GrabKeyRequest { |
12725 | owner_events, |
12726 | grab_window, |
12727 | modifiers, |
12728 | key, |
12729 | pointer_mode, |
12730 | keyboard_mode, |
12731 | }) |
12732 | } |
12733 | } |
12734 | impl Request for GrabKeyRequest { |
12735 | const EXTENSION_NAME: Option<&'static str> = None; |
12736 | |
12737 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
12738 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
12739 | // Flatten the buffers into a single vector |
12740 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
12741 | (buf, fds) |
12742 | } |
12743 | } |
12744 | impl crate::x11_utils::VoidRequest for GrabKeyRequest { |
12745 | } |
12746 | |
12747 | /// Opcode for the UngrabKey request |
12748 | pub const UNGRAB_KEY_REQUEST: u8 = 34; |
12749 | /// release a key combination. |
12750 | /// |
12751 | /// Releases the key combination on `grab_window` if you grabbed it using |
12752 | /// `xcb_grab_key` before. |
12753 | /// |
12754 | /// # Fields |
12755 | /// |
12756 | /// * `key` - The keycode of the specified key combination. |
12757 | /// |
12758 | /// Using the special value `XCB_GRAB_ANY` means releasing all possible key codes. |
12759 | /// * `grab_window` - The window on which the grabbed key combination will be released. |
12760 | /// * `modifiers` - The modifiers of the specified key combination. |
12761 | /// |
12762 | /// Using the special value `XCB_MOD_MASK_ANY` means releasing the key combination |
12763 | /// with every possible modifier combination. |
12764 | /// |
12765 | /// # Errors |
12766 | /// |
12767 | /// * `Window` - The specified `grab_window` does not exist. |
12768 | /// * `Value` - TODO: reasons? |
12769 | /// |
12770 | /// # See |
12771 | /// |
12772 | /// * `GrabKey`: request |
12773 | /// * `xev`: program |
12774 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
12775 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
12776 | pub struct UngrabKeyRequest { |
12777 | pub key: Keycode, |
12778 | pub grab_window: Window, |
12779 | pub modifiers: ModMask, |
12780 | } |
12781 | impl UngrabKeyRequest { |
12782 | /// Serialize this request into bytes for the provided connection |
12783 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
12784 | let length_so_far = 0; |
12785 | let key_bytes = self.key.serialize(); |
12786 | let grab_window_bytes = self.grab_window.serialize(); |
12787 | let modifiers_bytes = u16::from(self.modifiers).serialize(); |
12788 | let mut request0 = vec![ |
12789 | UNGRAB_KEY_REQUEST, |
12790 | key_bytes[0], |
12791 | 0, |
12792 | 0, |
12793 | grab_window_bytes[0], |
12794 | grab_window_bytes[1], |
12795 | grab_window_bytes[2], |
12796 | grab_window_bytes[3], |
12797 | modifiers_bytes[0], |
12798 | modifiers_bytes[1], |
12799 | 0, |
12800 | 0, |
12801 | ]; |
12802 | let length_so_far = length_so_far + request0.len(); |
12803 | assert_eq!(length_so_far % 4, 0); |
12804 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
12805 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
12806 | (vec![request0.into()], vec![]) |
12807 | } |
12808 | /// Parse this request given its header, its body, and any fds that go along with it |
12809 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
12810 | if header.major_opcode != UNGRAB_KEY_REQUEST { |
12811 | return Err(ParseError::InvalidValue); |
12812 | } |
12813 | let remaining = &[header.minor_opcode]; |
12814 | let (key, remaining) = Keycode::try_parse(remaining)?; |
12815 | let _ = remaining; |
12816 | let (grab_window, remaining) = Window::try_parse(value)?; |
12817 | let (modifiers, remaining) = u16::try_parse(remaining)?; |
12818 | let modifiers = modifiers.into(); |
12819 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
12820 | let _ = remaining; |
12821 | Ok(UngrabKeyRequest { |
12822 | key, |
12823 | grab_window, |
12824 | modifiers, |
12825 | }) |
12826 | } |
12827 | } |
12828 | impl Request for UngrabKeyRequest { |
12829 | const EXTENSION_NAME: Option<&'static str> = None; |
12830 | |
12831 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
12832 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
12833 | // Flatten the buffers into a single vector |
12834 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
12835 | (buf, fds) |
12836 | } |
12837 | } |
12838 | impl crate::x11_utils::VoidRequest for UngrabKeyRequest { |
12839 | } |
12840 | |
12841 | /// # Fields |
12842 | /// |
12843 | /// * `AsyncPointer` - For AsyncPointer, if the pointer is frozen by the client, pointer event |
12844 | /// processing continues normally. If the pointer is frozen twice by the client on |
12845 | /// behalf of two separate grabs, AsyncPointer thaws for both. AsyncPointer has no |
12846 | /// effect if the pointer is not frozen by the client, but the pointer need not be |
12847 | /// grabbed by the client. |
12848 | /// |
12849 | /// TODO: rewrite this in more understandable terms. |
12850 | /// * `SyncPointer` - For SyncPointer, if the pointer is frozen and actively grabbed by the client, |
12851 | /// pointer event processing continues normally until the next ButtonPress or |
12852 | /// ButtonRelease event is reported to the client, at which time the pointer again |
12853 | /// appears to freeze. However, if the reported event causes the pointer grab to be |
12854 | /// released, then the pointer does not freeze. SyncPointer has no effect if the |
12855 | /// pointer is not frozen by the client or if the pointer is not grabbed by the |
12856 | /// client. |
12857 | /// * `ReplayPointer` - For ReplayPointer, if the pointer is actively grabbed by the client and is |
12858 | /// frozen as the result of an event having been sent to the client (either from |
12859 | /// the activation of a GrabButton or from a previous AllowEvents with mode |
12860 | /// SyncPointer but not from a GrabPointer), then the pointer grab is released and |
12861 | /// that event is completely reprocessed, this time ignoring any passive grabs at |
12862 | /// or above (towards the root) the grab-window of the grab just released. The |
12863 | /// request has no effect if the pointer is not grabbed by the client or if the |
12864 | /// pointer is not frozen as the result of an event. |
12865 | /// * `AsyncKeyboard` - For AsyncKeyboard, if the keyboard is frozen by the client, keyboard event |
12866 | /// processing continues normally. If the keyboard is frozen twice by the client on |
12867 | /// behalf of two separate grabs, AsyncKeyboard thaws for both. AsyncKeyboard has |
12868 | /// no effect if the keyboard is not frozen by the client, but the keyboard need |
12869 | /// not be grabbed by the client. |
12870 | /// * `SyncKeyboard` - For SyncKeyboard, if the keyboard is frozen and actively grabbed by the client, |
12871 | /// keyboard event processing continues normally until the next KeyPress or |
12872 | /// KeyRelease event is reported to the client, at which time the keyboard again |
12873 | /// appears to freeze. However, if the reported event causes the keyboard grab to |
12874 | /// be released, then the keyboard does not freeze. SyncKeyboard has no effect if |
12875 | /// the keyboard is not frozen by the client or if the keyboard is not grabbed by |
12876 | /// the client. |
12877 | /// * `ReplayKeyboard` - For ReplayKeyboard, if the keyboard is actively grabbed by the client and is |
12878 | /// frozen as the result of an event having been sent to the client (either from |
12879 | /// the activation of a GrabKey or from a previous AllowEvents with mode |
12880 | /// SyncKeyboard but not from a GrabKeyboard), then the keyboard grab is released |
12881 | /// and that event is completely reprocessed, this time ignoring any passive grabs |
12882 | /// at or above (towards the root) the grab-window of the grab just released. The |
12883 | /// request has no effect if the keyboard is not grabbed by the client or if the |
12884 | /// keyboard is not frozen as the result of an event. |
12885 | /// * `SyncBoth` - For SyncBoth, if both pointer and keyboard are frozen by the client, event |
12886 | /// processing (for both devices) continues normally until the next ButtonPress, |
12887 | /// ButtonRelease, KeyPress, or KeyRelease event is reported to the client for a |
12888 | /// grabbed device (button event for the pointer, key event for the keyboard), at |
12889 | /// which time the devices again appear to freeze. However, if the reported event |
12890 | /// causes the grab to be released, then the devices do not freeze (but if the |
12891 | /// other device is still grabbed, then a subsequent event for it will still cause |
12892 | /// both devices to freeze). SyncBoth has no effect unless both pointer and |
12893 | /// keyboard are frozen by the client. If the pointer or keyboard is frozen twice |
12894 | /// by the client on behalf of two separate grabs, SyncBoth thaws for both (but a |
12895 | /// subsequent freeze for SyncBoth will only freeze each device once). |
12896 | /// * `AsyncBoth` - For AsyncBoth, if the pointer and the keyboard are frozen by the client, event |
12897 | /// processing for both devices continues normally. If a device is frozen twice by |
12898 | /// the client on behalf of two separate grabs, AsyncBoth thaws for both. AsyncBoth |
12899 | /// has no effect unless both pointer and keyboard are frozen by the client. |
12900 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
12901 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
12902 | pub struct Allow(u8); |
12903 | impl Allow { |
12904 | pub const ASYNC_POINTER: Self = Self(0); |
12905 | pub const SYNC_POINTER: Self = Self(1); |
12906 | pub const REPLAY_POINTER: Self = Self(2); |
12907 | pub const ASYNC_KEYBOARD: Self = Self(3); |
12908 | pub const SYNC_KEYBOARD: Self = Self(4); |
12909 | pub const REPLAY_KEYBOARD: Self = Self(5); |
12910 | pub const ASYNC_BOTH: Self = Self(6); |
12911 | pub const SYNC_BOTH: Self = Self(7); |
12912 | } |
12913 | impl From<Allow> for u8 { |
12914 | #[inline ] |
12915 | fn from(input: Allow) -> Self { |
12916 | input.0 |
12917 | } |
12918 | } |
12919 | impl From<Allow> for Option<u8> { |
12920 | #[inline ] |
12921 | fn from(input: Allow) -> Self { |
12922 | Some(input.0) |
12923 | } |
12924 | } |
12925 | impl From<Allow> for u16 { |
12926 | #[inline ] |
12927 | fn from(input: Allow) -> Self { |
12928 | u16::from(input.0) |
12929 | } |
12930 | } |
12931 | impl From<Allow> for Option<u16> { |
12932 | #[inline ] |
12933 | fn from(input: Allow) -> Self { |
12934 | Some(u16::from(input.0)) |
12935 | } |
12936 | } |
12937 | impl From<Allow> for u32 { |
12938 | #[inline ] |
12939 | fn from(input: Allow) -> Self { |
12940 | u32::from(input.0) |
12941 | } |
12942 | } |
12943 | impl From<Allow> for Option<u32> { |
12944 | #[inline ] |
12945 | fn from(input: Allow) -> Self { |
12946 | Some(u32::from(input.0)) |
12947 | } |
12948 | } |
12949 | impl From<u8> for Allow { |
12950 | #[inline ] |
12951 | fn from(value: u8) -> Self { |
12952 | Self(value) |
12953 | } |
12954 | } |
12955 | impl core::fmt::Debug for Allow { |
12956 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
12957 | let variants: [(u32, &str, &str); 8] = [ |
12958 | (Self::ASYNC_POINTER.0.into(), "ASYNC_POINTER" , "AsyncPointer" ), |
12959 | (Self::SYNC_POINTER.0.into(), "SYNC_POINTER" , "SyncPointer" ), |
12960 | (Self::REPLAY_POINTER.0.into(), "REPLAY_POINTER" , "ReplayPointer" ), |
12961 | (Self::ASYNC_KEYBOARD.0.into(), "ASYNC_KEYBOARD" , "AsyncKeyboard" ), |
12962 | (Self::SYNC_KEYBOARD.0.into(), "SYNC_KEYBOARD" , "SyncKeyboard" ), |
12963 | (Self::REPLAY_KEYBOARD.0.into(), "REPLAY_KEYBOARD" , "ReplayKeyboard" ), |
12964 | (Self::ASYNC_BOTH.0.into(), "ASYNC_BOTH" , "AsyncBoth" ), |
12965 | (Self::SYNC_BOTH.0.into(), "SYNC_BOTH" , "SyncBoth" ), |
12966 | ]; |
12967 | pretty_print_enum(fmt, self.0.into(), &variants) |
12968 | } |
12969 | } |
12970 | |
12971 | /// Opcode for the AllowEvents request |
12972 | pub const ALLOW_EVENTS_REQUEST: u8 = 35; |
12973 | /// release queued events. |
12974 | /// |
12975 | /// Releases queued events if the client has caused a device (pointer/keyboard) to |
12976 | /// freeze due to grabbing it actively. This request has no effect if `time` is |
12977 | /// earlier than the last-grab time of the most recent active grab for this client |
12978 | /// or if `time` is later than the current X server time. |
12979 | /// |
12980 | /// # Fields |
12981 | /// |
12982 | /// * `mode` - |
12983 | /// * `time` - Timestamp to avoid race conditions when running X over the network. |
12984 | /// |
12985 | /// The special value `XCB_CURRENT_TIME` will be replaced with the current server |
12986 | /// time. |
12987 | /// |
12988 | /// # Errors |
12989 | /// |
12990 | /// * `Value` - You specified an invalid `mode`. |
12991 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
12992 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
12993 | pub struct AllowEventsRequest { |
12994 | pub mode: Allow, |
12995 | pub time: Timestamp, |
12996 | } |
12997 | impl AllowEventsRequest { |
12998 | /// Serialize this request into bytes for the provided connection |
12999 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
13000 | let length_so_far = 0; |
13001 | let mode_bytes = u8::from(self.mode).serialize(); |
13002 | let time_bytes = self.time.serialize(); |
13003 | let mut request0 = vec![ |
13004 | ALLOW_EVENTS_REQUEST, |
13005 | mode_bytes[0], |
13006 | 0, |
13007 | 0, |
13008 | time_bytes[0], |
13009 | time_bytes[1], |
13010 | time_bytes[2], |
13011 | time_bytes[3], |
13012 | ]; |
13013 | let length_so_far = length_so_far + request0.len(); |
13014 | assert_eq!(length_so_far % 4, 0); |
13015 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
13016 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
13017 | (vec![request0.into()], vec![]) |
13018 | } |
13019 | /// Parse this request given its header, its body, and any fds that go along with it |
13020 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
13021 | if header.major_opcode != ALLOW_EVENTS_REQUEST { |
13022 | return Err(ParseError::InvalidValue); |
13023 | } |
13024 | let remaining = &[header.minor_opcode]; |
13025 | let (mode, remaining) = u8::try_parse(remaining)?; |
13026 | let mode = mode.into(); |
13027 | let _ = remaining; |
13028 | let (time, remaining) = Timestamp::try_parse(value)?; |
13029 | let _ = remaining; |
13030 | Ok(AllowEventsRequest { |
13031 | mode, |
13032 | time, |
13033 | }) |
13034 | } |
13035 | } |
13036 | impl Request for AllowEventsRequest { |
13037 | const EXTENSION_NAME: Option<&'static str> = None; |
13038 | |
13039 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
13040 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
13041 | // Flatten the buffers into a single vector |
13042 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
13043 | (buf, fds) |
13044 | } |
13045 | } |
13046 | impl crate::x11_utils::VoidRequest for AllowEventsRequest { |
13047 | } |
13048 | |
13049 | /// Opcode for the GrabServer request |
13050 | pub const GRAB_SERVER_REQUEST: u8 = 36; |
13051 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
13052 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
13053 | pub struct GrabServerRequest; |
13054 | impl GrabServerRequest { |
13055 | /// Serialize this request into bytes for the provided connection |
13056 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
13057 | let length_so_far = 0; |
13058 | let mut request0 = vec![ |
13059 | GRAB_SERVER_REQUEST, |
13060 | 0, |
13061 | 0, |
13062 | 0, |
13063 | ]; |
13064 | let length_so_far = length_so_far + request0.len(); |
13065 | assert_eq!(length_so_far % 4, 0); |
13066 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
13067 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
13068 | (vec![request0.into()], vec![]) |
13069 | } |
13070 | /// Parse this request given its header, its body, and any fds that go along with it |
13071 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
13072 | if header.major_opcode != GRAB_SERVER_REQUEST { |
13073 | return Err(ParseError::InvalidValue); |
13074 | } |
13075 | let remaining = &[header.minor_opcode]; |
13076 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
13077 | let _ = remaining; |
13078 | let _ = value; |
13079 | Ok(GrabServerRequest |
13080 | ) |
13081 | } |
13082 | } |
13083 | impl Request for GrabServerRequest { |
13084 | const EXTENSION_NAME: Option<&'static str> = None; |
13085 | |
13086 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
13087 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
13088 | // Flatten the buffers into a single vector |
13089 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
13090 | (buf, fds) |
13091 | } |
13092 | } |
13093 | impl crate::x11_utils::VoidRequest for GrabServerRequest { |
13094 | } |
13095 | |
13096 | /// Opcode for the UngrabServer request |
13097 | pub const UNGRAB_SERVER_REQUEST: u8 = 37; |
13098 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
13099 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
13100 | pub struct UngrabServerRequest; |
13101 | impl UngrabServerRequest { |
13102 | /// Serialize this request into bytes for the provided connection |
13103 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
13104 | let length_so_far = 0; |
13105 | let mut request0 = vec![ |
13106 | UNGRAB_SERVER_REQUEST, |
13107 | 0, |
13108 | 0, |
13109 | 0, |
13110 | ]; |
13111 | let length_so_far = length_so_far + request0.len(); |
13112 | assert_eq!(length_so_far % 4, 0); |
13113 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
13114 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
13115 | (vec![request0.into()], vec![]) |
13116 | } |
13117 | /// Parse this request given its header, its body, and any fds that go along with it |
13118 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
13119 | if header.major_opcode != UNGRAB_SERVER_REQUEST { |
13120 | return Err(ParseError::InvalidValue); |
13121 | } |
13122 | let remaining = &[header.minor_opcode]; |
13123 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
13124 | let _ = remaining; |
13125 | let _ = value; |
13126 | Ok(UngrabServerRequest |
13127 | ) |
13128 | } |
13129 | } |
13130 | impl Request for UngrabServerRequest { |
13131 | const EXTENSION_NAME: Option<&'static str> = None; |
13132 | |
13133 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
13134 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
13135 | // Flatten the buffers into a single vector |
13136 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
13137 | (buf, fds) |
13138 | } |
13139 | } |
13140 | impl crate::x11_utils::VoidRequest for UngrabServerRequest { |
13141 | } |
13142 | |
13143 | /// Opcode for the QueryPointer request |
13144 | pub const QUERY_POINTER_REQUEST: u8 = 38; |
13145 | /// get pointer coordinates. |
13146 | /// |
13147 | /// Gets the root window the pointer is logically on and the pointer coordinates |
13148 | /// relative to the root window's origin. |
13149 | /// |
13150 | /// # Fields |
13151 | /// |
13152 | /// * `window` - A window to check if the pointer is on the same screen as `window` (see the |
13153 | /// `same_screen` field in the reply). |
13154 | /// |
13155 | /// # Errors |
13156 | /// |
13157 | /// * `Window` - The specified `window` does not exist. |
13158 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
13159 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
13160 | pub struct QueryPointerRequest { |
13161 | pub window: Window, |
13162 | } |
13163 | impl QueryPointerRequest { |
13164 | /// Serialize this request into bytes for the provided connection |
13165 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
13166 | let length_so_far = 0; |
13167 | let window_bytes = self.window.serialize(); |
13168 | let mut request0 = vec![ |
13169 | QUERY_POINTER_REQUEST, |
13170 | 0, |
13171 | 0, |
13172 | 0, |
13173 | window_bytes[0], |
13174 | window_bytes[1], |
13175 | window_bytes[2], |
13176 | window_bytes[3], |
13177 | ]; |
13178 | let length_so_far = length_so_far + request0.len(); |
13179 | assert_eq!(length_so_far % 4, 0); |
13180 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
13181 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
13182 | (vec![request0.into()], vec![]) |
13183 | } |
13184 | /// Parse this request given its header, its body, and any fds that go along with it |
13185 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
13186 | if header.major_opcode != QUERY_POINTER_REQUEST { |
13187 | return Err(ParseError::InvalidValue); |
13188 | } |
13189 | let remaining = &[header.minor_opcode]; |
13190 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
13191 | let _ = remaining; |
13192 | let (window, remaining) = Window::try_parse(value)?; |
13193 | let _ = remaining; |
13194 | Ok(QueryPointerRequest { |
13195 | window, |
13196 | }) |
13197 | } |
13198 | } |
13199 | impl Request for QueryPointerRequest { |
13200 | const EXTENSION_NAME: Option<&'static str> = None; |
13201 | |
13202 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
13203 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
13204 | // Flatten the buffers into a single vector |
13205 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
13206 | (buf, fds) |
13207 | } |
13208 | } |
13209 | impl crate::x11_utils::ReplyRequest for QueryPointerRequest { |
13210 | type Reply = QueryPointerReply; |
13211 | } |
13212 | |
13213 | /// # Fields |
13214 | /// |
13215 | /// * `same_screen` - If `same_screen` is False, then the pointer is not on the same screen as the |
13216 | /// argument window, `child` is None, and `win_x` and `win_y` are zero. If |
13217 | /// `same_screen` is True, then `win_x` and `win_y` are the pointer coordinates |
13218 | /// relative to the argument window's origin, and child is the child containing the |
13219 | /// pointer, if any. |
13220 | /// * `root` - The root window the pointer is logically on. |
13221 | /// * `child` - The child window containing the pointer, if any, if `same_screen` is true. If |
13222 | /// `same_screen` is false, `XCB_NONE` is returned. |
13223 | /// * `root_x` - The pointer X position, relative to `root`. |
13224 | /// * `root_y` - The pointer Y position, relative to `root`. |
13225 | /// * `win_x` - The pointer X coordinate, relative to `child`, if `same_screen` is true. Zero |
13226 | /// otherwise. |
13227 | /// * `win_y` - The pointer Y coordinate, relative to `child`, if `same_screen` is true. Zero |
13228 | /// otherwise. |
13229 | /// * `mask` - The current logical state of the modifier keys and the buttons. Note that the |
13230 | /// logical state of a device (as seen by means of the protocol) may lag the |
13231 | /// physical state if device event processing is frozen. |
13232 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
13233 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
13234 | pub struct QueryPointerReply { |
13235 | pub same_screen: bool, |
13236 | pub sequence: u16, |
13237 | pub length: u32, |
13238 | pub root: Window, |
13239 | pub child: Window, |
13240 | pub root_x: i16, |
13241 | pub root_y: i16, |
13242 | pub win_x: i16, |
13243 | pub win_y: i16, |
13244 | pub mask: KeyButMask, |
13245 | } |
13246 | impl TryParse for QueryPointerReply { |
13247 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
13248 | let remaining = initial_value; |
13249 | let (response_type, remaining) = u8::try_parse(remaining)?; |
13250 | let (same_screen, remaining) = bool::try_parse(remaining)?; |
13251 | let (sequence, remaining) = u16::try_parse(remaining)?; |
13252 | let (length, remaining) = u32::try_parse(remaining)?; |
13253 | let (root, remaining) = Window::try_parse(remaining)?; |
13254 | let (child, remaining) = Window::try_parse(remaining)?; |
13255 | let (root_x, remaining) = i16::try_parse(remaining)?; |
13256 | let (root_y, remaining) = i16::try_parse(remaining)?; |
13257 | let (win_x, remaining) = i16::try_parse(remaining)?; |
13258 | let (win_y, remaining) = i16::try_parse(remaining)?; |
13259 | let (mask, remaining) = u16::try_parse(remaining)?; |
13260 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
13261 | if response_type != 1 { |
13262 | return Err(ParseError::InvalidValue); |
13263 | } |
13264 | let mask = mask.into(); |
13265 | let result = QueryPointerReply { same_screen, sequence, length, root, child, root_x, root_y, win_x, win_y, mask }; |
13266 | let _ = remaining; |
13267 | let remaining = initial_value.get(32 + length as usize * 4..) |
13268 | .ok_or(ParseError::InsufficientData)?; |
13269 | Ok((result, remaining)) |
13270 | } |
13271 | } |
13272 | impl Serialize for QueryPointerReply { |
13273 | type Bytes = [u8; 28]; |
13274 | fn serialize(&self) -> [u8; 28] { |
13275 | let response_type_bytes = &[1]; |
13276 | let same_screen_bytes = self.same_screen.serialize(); |
13277 | let sequence_bytes = self.sequence.serialize(); |
13278 | let length_bytes = self.length.serialize(); |
13279 | let root_bytes = self.root.serialize(); |
13280 | let child_bytes = self.child.serialize(); |
13281 | let root_x_bytes = self.root_x.serialize(); |
13282 | let root_y_bytes = self.root_y.serialize(); |
13283 | let win_x_bytes = self.win_x.serialize(); |
13284 | let win_y_bytes = self.win_y.serialize(); |
13285 | let mask_bytes = u16::from(self.mask).serialize(); |
13286 | [ |
13287 | response_type_bytes[0], |
13288 | same_screen_bytes[0], |
13289 | sequence_bytes[0], |
13290 | sequence_bytes[1], |
13291 | length_bytes[0], |
13292 | length_bytes[1], |
13293 | length_bytes[2], |
13294 | length_bytes[3], |
13295 | root_bytes[0], |
13296 | root_bytes[1], |
13297 | root_bytes[2], |
13298 | root_bytes[3], |
13299 | child_bytes[0], |
13300 | child_bytes[1], |
13301 | child_bytes[2], |
13302 | child_bytes[3], |
13303 | root_x_bytes[0], |
13304 | root_x_bytes[1], |
13305 | root_y_bytes[0], |
13306 | root_y_bytes[1], |
13307 | win_x_bytes[0], |
13308 | win_x_bytes[1], |
13309 | win_y_bytes[0], |
13310 | win_y_bytes[1], |
13311 | mask_bytes[0], |
13312 | mask_bytes[1], |
13313 | 0, |
13314 | 0, |
13315 | ] |
13316 | } |
13317 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
13318 | bytes.reserve(28); |
13319 | let response_type_bytes = &[1]; |
13320 | bytes.push(response_type_bytes[0]); |
13321 | self.same_screen.serialize_into(bytes); |
13322 | self.sequence.serialize_into(bytes); |
13323 | self.length.serialize_into(bytes); |
13324 | self.root.serialize_into(bytes); |
13325 | self.child.serialize_into(bytes); |
13326 | self.root_x.serialize_into(bytes); |
13327 | self.root_y.serialize_into(bytes); |
13328 | self.win_x.serialize_into(bytes); |
13329 | self.win_y.serialize_into(bytes); |
13330 | u16::from(self.mask).serialize_into(bytes); |
13331 | bytes.extend_from_slice(&[0; 2]); |
13332 | } |
13333 | } |
13334 | |
13335 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
13336 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
13337 | pub struct Timecoord { |
13338 | pub time: Timestamp, |
13339 | pub x: i16, |
13340 | pub y: i16, |
13341 | } |
13342 | impl TryParse for Timecoord { |
13343 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
13344 | let (time: u32, remaining: &[u8]) = Timestamp::try_parse(remaining)?; |
13345 | let (x: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
13346 | let (y: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
13347 | let result: Timecoord = Timecoord { time, x, y }; |
13348 | Ok((result, remaining)) |
13349 | } |
13350 | } |
13351 | impl Serialize for Timecoord { |
13352 | type Bytes = [u8; 8]; |
13353 | fn serialize(&self) -> [u8; 8] { |
13354 | let time_bytes: [u8; 4] = self.time.serialize(); |
13355 | let x_bytes: [u8; 2] = self.x.serialize(); |
13356 | let y_bytes: [u8; 2] = self.y.serialize(); |
13357 | [ |
13358 | time_bytes[0], |
13359 | time_bytes[1], |
13360 | time_bytes[2], |
13361 | time_bytes[3], |
13362 | x_bytes[0], |
13363 | x_bytes[1], |
13364 | y_bytes[0], |
13365 | y_bytes[1], |
13366 | ] |
13367 | } |
13368 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
13369 | bytes.reserve(additional:8); |
13370 | self.time.serialize_into(bytes); |
13371 | self.x.serialize_into(bytes); |
13372 | self.y.serialize_into(bytes); |
13373 | } |
13374 | } |
13375 | |
13376 | /// Opcode for the GetMotionEvents request |
13377 | pub const GET_MOTION_EVENTS_REQUEST: u8 = 39; |
13378 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
13379 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
13380 | pub struct GetMotionEventsRequest { |
13381 | pub window: Window, |
13382 | pub start: Timestamp, |
13383 | pub stop: Timestamp, |
13384 | } |
13385 | impl GetMotionEventsRequest { |
13386 | /// Serialize this request into bytes for the provided connection |
13387 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
13388 | let length_so_far = 0; |
13389 | let window_bytes = self.window.serialize(); |
13390 | let start_bytes = self.start.serialize(); |
13391 | let stop_bytes = self.stop.serialize(); |
13392 | let mut request0 = vec![ |
13393 | GET_MOTION_EVENTS_REQUEST, |
13394 | 0, |
13395 | 0, |
13396 | 0, |
13397 | window_bytes[0], |
13398 | window_bytes[1], |
13399 | window_bytes[2], |
13400 | window_bytes[3], |
13401 | start_bytes[0], |
13402 | start_bytes[1], |
13403 | start_bytes[2], |
13404 | start_bytes[3], |
13405 | stop_bytes[0], |
13406 | stop_bytes[1], |
13407 | stop_bytes[2], |
13408 | stop_bytes[3], |
13409 | ]; |
13410 | let length_so_far = length_so_far + request0.len(); |
13411 | assert_eq!(length_so_far % 4, 0); |
13412 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
13413 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
13414 | (vec![request0.into()], vec![]) |
13415 | } |
13416 | /// Parse this request given its header, its body, and any fds that go along with it |
13417 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
13418 | if header.major_opcode != GET_MOTION_EVENTS_REQUEST { |
13419 | return Err(ParseError::InvalidValue); |
13420 | } |
13421 | let remaining = &[header.minor_opcode]; |
13422 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
13423 | let _ = remaining; |
13424 | let (window, remaining) = Window::try_parse(value)?; |
13425 | let (start, remaining) = Timestamp::try_parse(remaining)?; |
13426 | let (stop, remaining) = Timestamp::try_parse(remaining)?; |
13427 | let _ = remaining; |
13428 | Ok(GetMotionEventsRequest { |
13429 | window, |
13430 | start, |
13431 | stop, |
13432 | }) |
13433 | } |
13434 | } |
13435 | impl Request for GetMotionEventsRequest { |
13436 | const EXTENSION_NAME: Option<&'static str> = None; |
13437 | |
13438 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
13439 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
13440 | // Flatten the buffers into a single vector |
13441 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
13442 | (buf, fds) |
13443 | } |
13444 | } |
13445 | impl crate::x11_utils::ReplyRequest for GetMotionEventsRequest { |
13446 | type Reply = GetMotionEventsReply; |
13447 | } |
13448 | |
13449 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
13450 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
13451 | pub struct GetMotionEventsReply { |
13452 | pub sequence: u16, |
13453 | pub length: u32, |
13454 | pub events: Vec<Timecoord>, |
13455 | } |
13456 | impl TryParse for GetMotionEventsReply { |
13457 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
13458 | let remaining: &[u8] = initial_value; |
13459 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
13460 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
13461 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
13462 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
13463 | let (events_len: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
13464 | let remaining: &[u8] = remaining.get(20..).ok_or(err:ParseError::InsufficientData)?; |
13465 | let (events: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<Timecoord>(data:remaining, list_length:events_len.try_to_usize()?)?; |
13466 | if response_type != 1 { |
13467 | return Err(ParseError::InvalidValue); |
13468 | } |
13469 | let result: GetMotionEventsReply = GetMotionEventsReply { sequence, length, events }; |
13470 | let _ = remaining; |
13471 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
13472 | .ok_or(err:ParseError::InsufficientData)?; |
13473 | Ok((result, remaining)) |
13474 | } |
13475 | } |
13476 | impl Serialize for GetMotionEventsReply { |
13477 | type Bytes = Vec<u8>; |
13478 | fn serialize(&self) -> Vec<u8> { |
13479 | let mut result: Vec = Vec::new(); |
13480 | self.serialize_into(&mut result); |
13481 | result |
13482 | } |
13483 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
13484 | bytes.reserve(additional:32); |
13485 | let response_type_bytes: &[u8; 1] = &[1]; |
13486 | bytes.push(response_type_bytes[0]); |
13487 | bytes.extend_from_slice(&[0; 1]); |
13488 | self.sequence.serialize_into(bytes); |
13489 | self.length.serialize_into(bytes); |
13490 | let events_len: u32 = u32::try_from(self.events.len()).expect(msg:"`events` has too many elements" ); |
13491 | events_len.serialize_into(bytes); |
13492 | bytes.extend_from_slice(&[0; 20]); |
13493 | self.events.serialize_into(bytes); |
13494 | } |
13495 | } |
13496 | impl GetMotionEventsReply { |
13497 | /// Get the value of the `events_len` field. |
13498 | /// |
13499 | /// The `events_len` field is used as the length field of the `events` field. |
13500 | /// This function computes the field's value again based on the length of the list. |
13501 | /// |
13502 | /// # Panics |
13503 | /// |
13504 | /// Panics if the value cannot be represented in the target type. This |
13505 | /// cannot happen with values of the struct received from the X11 server. |
13506 | pub fn events_len(&self) -> u32 { |
13507 | self.events.len() |
13508 | .try_into().unwrap() |
13509 | } |
13510 | } |
13511 | |
13512 | /// Opcode for the TranslateCoordinates request |
13513 | pub const TRANSLATE_COORDINATES_REQUEST: u8 = 40; |
13514 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
13515 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
13516 | pub struct TranslateCoordinatesRequest { |
13517 | pub src_window: Window, |
13518 | pub dst_window: Window, |
13519 | pub src_x: i16, |
13520 | pub src_y: i16, |
13521 | } |
13522 | impl TranslateCoordinatesRequest { |
13523 | /// Serialize this request into bytes for the provided connection |
13524 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
13525 | let length_so_far = 0; |
13526 | let src_window_bytes = self.src_window.serialize(); |
13527 | let dst_window_bytes = self.dst_window.serialize(); |
13528 | let src_x_bytes = self.src_x.serialize(); |
13529 | let src_y_bytes = self.src_y.serialize(); |
13530 | let mut request0 = vec![ |
13531 | TRANSLATE_COORDINATES_REQUEST, |
13532 | 0, |
13533 | 0, |
13534 | 0, |
13535 | src_window_bytes[0], |
13536 | src_window_bytes[1], |
13537 | src_window_bytes[2], |
13538 | src_window_bytes[3], |
13539 | dst_window_bytes[0], |
13540 | dst_window_bytes[1], |
13541 | dst_window_bytes[2], |
13542 | dst_window_bytes[3], |
13543 | src_x_bytes[0], |
13544 | src_x_bytes[1], |
13545 | src_y_bytes[0], |
13546 | src_y_bytes[1], |
13547 | ]; |
13548 | let length_so_far = length_so_far + request0.len(); |
13549 | assert_eq!(length_so_far % 4, 0); |
13550 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
13551 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
13552 | (vec![request0.into()], vec![]) |
13553 | } |
13554 | /// Parse this request given its header, its body, and any fds that go along with it |
13555 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
13556 | if header.major_opcode != TRANSLATE_COORDINATES_REQUEST { |
13557 | return Err(ParseError::InvalidValue); |
13558 | } |
13559 | let remaining = &[header.minor_opcode]; |
13560 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
13561 | let _ = remaining; |
13562 | let (src_window, remaining) = Window::try_parse(value)?; |
13563 | let (dst_window, remaining) = Window::try_parse(remaining)?; |
13564 | let (src_x, remaining) = i16::try_parse(remaining)?; |
13565 | let (src_y, remaining) = i16::try_parse(remaining)?; |
13566 | let _ = remaining; |
13567 | Ok(TranslateCoordinatesRequest { |
13568 | src_window, |
13569 | dst_window, |
13570 | src_x, |
13571 | src_y, |
13572 | }) |
13573 | } |
13574 | } |
13575 | impl Request for TranslateCoordinatesRequest { |
13576 | const EXTENSION_NAME: Option<&'static str> = None; |
13577 | |
13578 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
13579 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
13580 | // Flatten the buffers into a single vector |
13581 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
13582 | (buf, fds) |
13583 | } |
13584 | } |
13585 | impl crate::x11_utils::ReplyRequest for TranslateCoordinatesRequest { |
13586 | type Reply = TranslateCoordinatesReply; |
13587 | } |
13588 | |
13589 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
13590 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
13591 | pub struct TranslateCoordinatesReply { |
13592 | pub same_screen: bool, |
13593 | pub sequence: u16, |
13594 | pub length: u32, |
13595 | pub child: Window, |
13596 | pub dst_x: i16, |
13597 | pub dst_y: i16, |
13598 | } |
13599 | impl TryParse for TranslateCoordinatesReply { |
13600 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
13601 | let remaining: &[u8] = initial_value; |
13602 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
13603 | let (same_screen: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
13604 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
13605 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
13606 | let (child: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
13607 | let (dst_x: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
13608 | let (dst_y: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
13609 | if response_type != 1 { |
13610 | return Err(ParseError::InvalidValue); |
13611 | } |
13612 | let result: TranslateCoordinatesReply = TranslateCoordinatesReply { same_screen, sequence, length, child, dst_x, dst_y }; |
13613 | let _ = remaining; |
13614 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
13615 | .ok_or(err:ParseError::InsufficientData)?; |
13616 | Ok((result, remaining)) |
13617 | } |
13618 | } |
13619 | impl Serialize for TranslateCoordinatesReply { |
13620 | type Bytes = [u8; 16]; |
13621 | fn serialize(&self) -> [u8; 16] { |
13622 | let response_type_bytes = &[1]; |
13623 | let same_screen_bytes = self.same_screen.serialize(); |
13624 | let sequence_bytes = self.sequence.serialize(); |
13625 | let length_bytes = self.length.serialize(); |
13626 | let child_bytes = self.child.serialize(); |
13627 | let dst_x_bytes = self.dst_x.serialize(); |
13628 | let dst_y_bytes = self.dst_y.serialize(); |
13629 | [ |
13630 | response_type_bytes[0], |
13631 | same_screen_bytes[0], |
13632 | sequence_bytes[0], |
13633 | sequence_bytes[1], |
13634 | length_bytes[0], |
13635 | length_bytes[1], |
13636 | length_bytes[2], |
13637 | length_bytes[3], |
13638 | child_bytes[0], |
13639 | child_bytes[1], |
13640 | child_bytes[2], |
13641 | child_bytes[3], |
13642 | dst_x_bytes[0], |
13643 | dst_x_bytes[1], |
13644 | dst_y_bytes[0], |
13645 | dst_y_bytes[1], |
13646 | ] |
13647 | } |
13648 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
13649 | bytes.reserve(16); |
13650 | let response_type_bytes = &[1]; |
13651 | bytes.push(response_type_bytes[0]); |
13652 | self.same_screen.serialize_into(bytes); |
13653 | self.sequence.serialize_into(bytes); |
13654 | self.length.serialize_into(bytes); |
13655 | self.child.serialize_into(bytes); |
13656 | self.dst_x.serialize_into(bytes); |
13657 | self.dst_y.serialize_into(bytes); |
13658 | } |
13659 | } |
13660 | |
13661 | /// Opcode for the WarpPointer request |
13662 | pub const WARP_POINTER_REQUEST: u8 = 41; |
13663 | /// move mouse pointer. |
13664 | /// |
13665 | /// Moves the mouse pointer to the specified position. |
13666 | /// |
13667 | /// If `src_window` is not `XCB_NONE` (TODO), the move will only take place if the |
13668 | /// pointer is inside `src_window` and within the rectangle specified by (`src_x`, |
13669 | /// `src_y`, `src_width`, `src_height`). The rectangle coordinates are relative to |
13670 | /// `src_window`. |
13671 | /// |
13672 | /// If `dst_window` is not `XCB_NONE` (TODO), the pointer will be moved to the |
13673 | /// offsets (`dst_x`, `dst_y`) relative to `dst_window`. If `dst_window` is |
13674 | /// `XCB_NONE` (TODO), the pointer will be moved by the offsets (`dst_x`, `dst_y`) |
13675 | /// relative to the current position of the pointer. |
13676 | /// |
13677 | /// # Fields |
13678 | /// |
13679 | /// * `src_window` - If `src_window` is not `XCB_NONE` (TODO), the move will only take place if the |
13680 | /// pointer is inside `src_window` and within the rectangle specified by (`src_x`, |
13681 | /// `src_y`, `src_width`, `src_height`). The rectangle coordinates are relative to |
13682 | /// `src_window`. |
13683 | /// * `dst_window` - If `dst_window` is not `XCB_NONE` (TODO), the pointer will be moved to the |
13684 | /// offsets (`dst_x`, `dst_y`) relative to `dst_window`. If `dst_window` is |
13685 | /// `XCB_NONE` (TODO), the pointer will be moved by the offsets (`dst_x`, `dst_y`) |
13686 | /// relative to the current position of the pointer. |
13687 | /// |
13688 | /// # Errors |
13689 | /// |
13690 | /// * `Window` - TODO: reasons? |
13691 | /// |
13692 | /// # See |
13693 | /// |
13694 | /// * `SetInputFocus`: request |
13695 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
13696 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
13697 | pub struct WarpPointerRequest { |
13698 | pub src_window: Window, |
13699 | pub dst_window: Window, |
13700 | pub src_x: i16, |
13701 | pub src_y: i16, |
13702 | pub src_width: u16, |
13703 | pub src_height: u16, |
13704 | pub dst_x: i16, |
13705 | pub dst_y: i16, |
13706 | } |
13707 | impl WarpPointerRequest { |
13708 | /// Serialize this request into bytes for the provided connection |
13709 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
13710 | let length_so_far = 0; |
13711 | let src_window_bytes = self.src_window.serialize(); |
13712 | let dst_window_bytes = self.dst_window.serialize(); |
13713 | let src_x_bytes = self.src_x.serialize(); |
13714 | let src_y_bytes = self.src_y.serialize(); |
13715 | let src_width_bytes = self.src_width.serialize(); |
13716 | let src_height_bytes = self.src_height.serialize(); |
13717 | let dst_x_bytes = self.dst_x.serialize(); |
13718 | let dst_y_bytes = self.dst_y.serialize(); |
13719 | let mut request0 = vec![ |
13720 | WARP_POINTER_REQUEST, |
13721 | 0, |
13722 | 0, |
13723 | 0, |
13724 | src_window_bytes[0], |
13725 | src_window_bytes[1], |
13726 | src_window_bytes[2], |
13727 | src_window_bytes[3], |
13728 | dst_window_bytes[0], |
13729 | dst_window_bytes[1], |
13730 | dst_window_bytes[2], |
13731 | dst_window_bytes[3], |
13732 | src_x_bytes[0], |
13733 | src_x_bytes[1], |
13734 | src_y_bytes[0], |
13735 | src_y_bytes[1], |
13736 | src_width_bytes[0], |
13737 | src_width_bytes[1], |
13738 | src_height_bytes[0], |
13739 | src_height_bytes[1], |
13740 | dst_x_bytes[0], |
13741 | dst_x_bytes[1], |
13742 | dst_y_bytes[0], |
13743 | dst_y_bytes[1], |
13744 | ]; |
13745 | let length_so_far = length_so_far + request0.len(); |
13746 | assert_eq!(length_so_far % 4, 0); |
13747 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
13748 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
13749 | (vec![request0.into()], vec![]) |
13750 | } |
13751 | /// Parse this request given its header, its body, and any fds that go along with it |
13752 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
13753 | if header.major_opcode != WARP_POINTER_REQUEST { |
13754 | return Err(ParseError::InvalidValue); |
13755 | } |
13756 | let remaining = &[header.minor_opcode]; |
13757 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
13758 | let _ = remaining; |
13759 | let (src_window, remaining) = Window::try_parse(value)?; |
13760 | let (dst_window, remaining) = Window::try_parse(remaining)?; |
13761 | let (src_x, remaining) = i16::try_parse(remaining)?; |
13762 | let (src_y, remaining) = i16::try_parse(remaining)?; |
13763 | let (src_width, remaining) = u16::try_parse(remaining)?; |
13764 | let (src_height, remaining) = u16::try_parse(remaining)?; |
13765 | let (dst_x, remaining) = i16::try_parse(remaining)?; |
13766 | let (dst_y, remaining) = i16::try_parse(remaining)?; |
13767 | let _ = remaining; |
13768 | Ok(WarpPointerRequest { |
13769 | src_window, |
13770 | dst_window, |
13771 | src_x, |
13772 | src_y, |
13773 | src_width, |
13774 | src_height, |
13775 | dst_x, |
13776 | dst_y, |
13777 | }) |
13778 | } |
13779 | } |
13780 | impl Request for WarpPointerRequest { |
13781 | const EXTENSION_NAME: Option<&'static str> = None; |
13782 | |
13783 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
13784 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
13785 | // Flatten the buffers into a single vector |
13786 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
13787 | (buf, fds) |
13788 | } |
13789 | } |
13790 | impl crate::x11_utils::VoidRequest for WarpPointerRequest { |
13791 | } |
13792 | |
13793 | /// # Fields |
13794 | /// |
13795 | /// * `None` - The focus reverts to `XCB_NONE`, so no window will have the input focus. |
13796 | /// * `PointerRoot` - The focus reverts to `XCB_POINTER_ROOT` respectively. When the focus reverts, |
13797 | /// FocusIn and FocusOut events are generated, but the last-focus-change time is |
13798 | /// not changed. |
13799 | /// * `Parent` - The focus reverts to the parent (or closest viewable ancestor) and the new |
13800 | /// revert_to value is `XCB_INPUT_FOCUS_NONE`. |
13801 | /// * `FollowKeyboard` - NOT YET DOCUMENTED. Only relevant for the xinput extension. |
13802 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
13803 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
13804 | pub struct InputFocus(u8); |
13805 | impl InputFocus { |
13806 | pub const NONE: Self = Self(0); |
13807 | pub const POINTER_ROOT: Self = Self(1); |
13808 | pub const PARENT: Self = Self(2); |
13809 | pub const FOLLOW_KEYBOARD: Self = Self(3); |
13810 | } |
13811 | impl From<InputFocus> for u8 { |
13812 | #[inline ] |
13813 | fn from(input: InputFocus) -> Self { |
13814 | input.0 |
13815 | } |
13816 | } |
13817 | impl From<InputFocus> for Option<u8> { |
13818 | #[inline ] |
13819 | fn from(input: InputFocus) -> Self { |
13820 | Some(input.0) |
13821 | } |
13822 | } |
13823 | impl From<InputFocus> for u16 { |
13824 | #[inline ] |
13825 | fn from(input: InputFocus) -> Self { |
13826 | u16::from(input.0) |
13827 | } |
13828 | } |
13829 | impl From<InputFocus> for Option<u16> { |
13830 | #[inline ] |
13831 | fn from(input: InputFocus) -> Self { |
13832 | Some(u16::from(input.0)) |
13833 | } |
13834 | } |
13835 | impl From<InputFocus> for u32 { |
13836 | #[inline ] |
13837 | fn from(input: InputFocus) -> Self { |
13838 | u32::from(input.0) |
13839 | } |
13840 | } |
13841 | impl From<InputFocus> for Option<u32> { |
13842 | #[inline ] |
13843 | fn from(input: InputFocus) -> Self { |
13844 | Some(u32::from(input.0)) |
13845 | } |
13846 | } |
13847 | impl From<u8> for InputFocus { |
13848 | #[inline ] |
13849 | fn from(value: u8) -> Self { |
13850 | Self(value) |
13851 | } |
13852 | } |
13853 | impl core::fmt::Debug for InputFocus { |
13854 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
13855 | let variants: [(u32, &str, &str); 4] = [ |
13856 | (Self::NONE.0.into(), "NONE" , "None" ), |
13857 | (Self::POINTER_ROOT.0.into(), "POINTER_ROOT" , "PointerRoot" ), |
13858 | (Self::PARENT.0.into(), "PARENT" , "Parent" ), |
13859 | (Self::FOLLOW_KEYBOARD.0.into(), "FOLLOW_KEYBOARD" , "FollowKeyboard" ), |
13860 | ]; |
13861 | pretty_print_enum(fmt, self.0.into(), &variants) |
13862 | } |
13863 | } |
13864 | |
13865 | /// Opcode for the SetInputFocus request |
13866 | pub const SET_INPUT_FOCUS_REQUEST: u8 = 42; |
13867 | /// Sets input focus. |
13868 | /// |
13869 | /// Changes the input focus and the last-focus-change time. If the specified `time` |
13870 | /// is earlier than the current last-focus-change time, the request is ignored (to |
13871 | /// avoid race conditions when running X over the network). |
13872 | /// |
13873 | /// A FocusIn and FocusOut event is generated when focus is changed. |
13874 | /// |
13875 | /// # Fields |
13876 | /// |
13877 | /// * `focus` - The window to focus. All keyboard events will be reported to this window. The |
13878 | /// window must be viewable (TODO), or a `xcb_match_error_t` occurs (TODO). |
13879 | /// |
13880 | /// If `focus` is `XCB_NONE` (TODO), all keyboard events are |
13881 | /// discarded until a new focus window is set. |
13882 | /// |
13883 | /// If `focus` is `XCB_POINTER_ROOT` (TODO), focus is on the root window of the |
13884 | /// screen on which the pointer is on currently. |
13885 | /// * `time` - Timestamp to avoid race conditions when running X over the network. |
13886 | /// |
13887 | /// The special value `XCB_CURRENT_TIME` will be replaced with the current server |
13888 | /// time. |
13889 | /// * `revert_to` - Specifies what happens when the `focus` window becomes unviewable (if `focus` |
13890 | /// is neither `XCB_NONE` nor `XCB_POINTER_ROOT`). |
13891 | /// |
13892 | /// # Errors |
13893 | /// |
13894 | /// * `Window` - The specified `focus` window does not exist. |
13895 | /// * `Match` - The specified `focus` window is not viewable. |
13896 | /// * `Value` - TODO: Reasons? |
13897 | /// |
13898 | /// # See |
13899 | /// |
13900 | /// * `FocusIn`: event |
13901 | /// * `FocusOut`: event |
13902 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
13903 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
13904 | pub struct SetInputFocusRequest { |
13905 | pub revert_to: InputFocus, |
13906 | pub focus: Window, |
13907 | pub time: Timestamp, |
13908 | } |
13909 | impl SetInputFocusRequest { |
13910 | /// Serialize this request into bytes for the provided connection |
13911 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
13912 | let length_so_far = 0; |
13913 | let revert_to_bytes = u8::from(self.revert_to).serialize(); |
13914 | let focus_bytes = self.focus.serialize(); |
13915 | let time_bytes = self.time.serialize(); |
13916 | let mut request0 = vec![ |
13917 | SET_INPUT_FOCUS_REQUEST, |
13918 | revert_to_bytes[0], |
13919 | 0, |
13920 | 0, |
13921 | focus_bytes[0], |
13922 | focus_bytes[1], |
13923 | focus_bytes[2], |
13924 | focus_bytes[3], |
13925 | time_bytes[0], |
13926 | time_bytes[1], |
13927 | time_bytes[2], |
13928 | time_bytes[3], |
13929 | ]; |
13930 | let length_so_far = length_so_far + request0.len(); |
13931 | assert_eq!(length_so_far % 4, 0); |
13932 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
13933 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
13934 | (vec![request0.into()], vec![]) |
13935 | } |
13936 | /// Parse this request given its header, its body, and any fds that go along with it |
13937 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
13938 | if header.major_opcode != SET_INPUT_FOCUS_REQUEST { |
13939 | return Err(ParseError::InvalidValue); |
13940 | } |
13941 | let remaining = &[header.minor_opcode]; |
13942 | let (revert_to, remaining) = u8::try_parse(remaining)?; |
13943 | let revert_to = revert_to.into(); |
13944 | let _ = remaining; |
13945 | let (focus, remaining) = Window::try_parse(value)?; |
13946 | let (time, remaining) = Timestamp::try_parse(remaining)?; |
13947 | let _ = remaining; |
13948 | Ok(SetInputFocusRequest { |
13949 | revert_to, |
13950 | focus, |
13951 | time, |
13952 | }) |
13953 | } |
13954 | } |
13955 | impl Request for SetInputFocusRequest { |
13956 | const EXTENSION_NAME: Option<&'static str> = None; |
13957 | |
13958 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
13959 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
13960 | // Flatten the buffers into a single vector |
13961 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
13962 | (buf, fds) |
13963 | } |
13964 | } |
13965 | impl crate::x11_utils::VoidRequest for SetInputFocusRequest { |
13966 | } |
13967 | |
13968 | /// Opcode for the GetInputFocus request |
13969 | pub const GET_INPUT_FOCUS_REQUEST: u8 = 43; |
13970 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
13971 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
13972 | pub struct GetInputFocusRequest; |
13973 | impl GetInputFocusRequest { |
13974 | /// Serialize this request into bytes for the provided connection |
13975 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
13976 | let length_so_far = 0; |
13977 | let mut request0 = vec![ |
13978 | GET_INPUT_FOCUS_REQUEST, |
13979 | 0, |
13980 | 0, |
13981 | 0, |
13982 | ]; |
13983 | let length_so_far = length_so_far + request0.len(); |
13984 | assert_eq!(length_so_far % 4, 0); |
13985 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
13986 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
13987 | (vec![request0.into()], vec![]) |
13988 | } |
13989 | /// Parse this request given its header, its body, and any fds that go along with it |
13990 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
13991 | if header.major_opcode != GET_INPUT_FOCUS_REQUEST { |
13992 | return Err(ParseError::InvalidValue); |
13993 | } |
13994 | let remaining = &[header.minor_opcode]; |
13995 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
13996 | let _ = remaining; |
13997 | let _ = value; |
13998 | Ok(GetInputFocusRequest |
13999 | ) |
14000 | } |
14001 | } |
14002 | impl Request for GetInputFocusRequest { |
14003 | const EXTENSION_NAME: Option<&'static str> = None; |
14004 | |
14005 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
14006 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
14007 | // Flatten the buffers into a single vector |
14008 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
14009 | (buf, fds) |
14010 | } |
14011 | } |
14012 | impl crate::x11_utils::ReplyRequest for GetInputFocusRequest { |
14013 | type Reply = GetInputFocusReply; |
14014 | } |
14015 | |
14016 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14017 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14018 | pub struct GetInputFocusReply { |
14019 | pub revert_to: InputFocus, |
14020 | pub sequence: u16, |
14021 | pub length: u32, |
14022 | pub focus: Window, |
14023 | } |
14024 | impl TryParse for GetInputFocusReply { |
14025 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
14026 | let remaining: &[u8] = initial_value; |
14027 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
14028 | let (revert_to: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
14029 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
14030 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
14031 | let (focus: u32, remaining: &[u8]) = Window::try_parse(remaining)?; |
14032 | if response_type != 1 { |
14033 | return Err(ParseError::InvalidValue); |
14034 | } |
14035 | let revert_to: InputFocus = revert_to.into(); |
14036 | let result: GetInputFocusReply = GetInputFocusReply { revert_to, sequence, length, focus }; |
14037 | let _ = remaining; |
14038 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
14039 | .ok_or(err:ParseError::InsufficientData)?; |
14040 | Ok((result, remaining)) |
14041 | } |
14042 | } |
14043 | impl Serialize for GetInputFocusReply { |
14044 | type Bytes = [u8; 12]; |
14045 | fn serialize(&self) -> [u8; 12] { |
14046 | let response_type_bytes = &[1]; |
14047 | let revert_to_bytes = u8::from(self.revert_to).serialize(); |
14048 | let sequence_bytes = self.sequence.serialize(); |
14049 | let length_bytes = self.length.serialize(); |
14050 | let focus_bytes = self.focus.serialize(); |
14051 | [ |
14052 | response_type_bytes[0], |
14053 | revert_to_bytes[0], |
14054 | sequence_bytes[0], |
14055 | sequence_bytes[1], |
14056 | length_bytes[0], |
14057 | length_bytes[1], |
14058 | length_bytes[2], |
14059 | length_bytes[3], |
14060 | focus_bytes[0], |
14061 | focus_bytes[1], |
14062 | focus_bytes[2], |
14063 | focus_bytes[3], |
14064 | ] |
14065 | } |
14066 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
14067 | bytes.reserve(12); |
14068 | let response_type_bytes = &[1]; |
14069 | bytes.push(response_type_bytes[0]); |
14070 | u8::from(self.revert_to).serialize_into(bytes); |
14071 | self.sequence.serialize_into(bytes); |
14072 | self.length.serialize_into(bytes); |
14073 | self.focus.serialize_into(bytes); |
14074 | } |
14075 | } |
14076 | |
14077 | /// Opcode for the QueryKeymap request |
14078 | pub const QUERY_KEYMAP_REQUEST: u8 = 44; |
14079 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14080 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14081 | pub struct QueryKeymapRequest; |
14082 | impl QueryKeymapRequest { |
14083 | /// Serialize this request into bytes for the provided connection |
14084 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
14085 | let length_so_far = 0; |
14086 | let mut request0 = vec![ |
14087 | QUERY_KEYMAP_REQUEST, |
14088 | 0, |
14089 | 0, |
14090 | 0, |
14091 | ]; |
14092 | let length_so_far = length_so_far + request0.len(); |
14093 | assert_eq!(length_so_far % 4, 0); |
14094 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
14095 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
14096 | (vec![request0.into()], vec![]) |
14097 | } |
14098 | /// Parse this request given its header, its body, and any fds that go along with it |
14099 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
14100 | if header.major_opcode != QUERY_KEYMAP_REQUEST { |
14101 | return Err(ParseError::InvalidValue); |
14102 | } |
14103 | let remaining = &[header.minor_opcode]; |
14104 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
14105 | let _ = remaining; |
14106 | let _ = value; |
14107 | Ok(QueryKeymapRequest |
14108 | ) |
14109 | } |
14110 | } |
14111 | impl Request for QueryKeymapRequest { |
14112 | const EXTENSION_NAME: Option<&'static str> = None; |
14113 | |
14114 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
14115 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
14116 | // Flatten the buffers into a single vector |
14117 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
14118 | (buf, fds) |
14119 | } |
14120 | } |
14121 | impl crate::x11_utils::ReplyRequest for QueryKeymapRequest { |
14122 | type Reply = QueryKeymapReply; |
14123 | } |
14124 | |
14125 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14126 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14127 | pub struct QueryKeymapReply { |
14128 | pub sequence: u16, |
14129 | pub length: u32, |
14130 | pub keys: [u8; 32], |
14131 | } |
14132 | impl TryParse for QueryKeymapReply { |
14133 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
14134 | let remaining: &[u8] = initial_value; |
14135 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
14136 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
14137 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
14138 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
14139 | let (keys: [u8; 32], remaining: &[u8]) = crate::x11_utils::parse_u8_array::<32>(data:remaining)?; |
14140 | if response_type != 1 { |
14141 | return Err(ParseError::InvalidValue); |
14142 | } |
14143 | let result: QueryKeymapReply = QueryKeymapReply { sequence, length, keys }; |
14144 | let _ = remaining; |
14145 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
14146 | .ok_or(err:ParseError::InsufficientData)?; |
14147 | Ok((result, remaining)) |
14148 | } |
14149 | } |
14150 | impl Serialize for QueryKeymapReply { |
14151 | type Bytes = [u8; 40]; |
14152 | fn serialize(&self) -> [u8; 40] { |
14153 | let response_type_bytes = &[1]; |
14154 | let sequence_bytes = self.sequence.serialize(); |
14155 | let length_bytes = self.length.serialize(); |
14156 | [ |
14157 | response_type_bytes[0], |
14158 | 0, |
14159 | sequence_bytes[0], |
14160 | sequence_bytes[1], |
14161 | length_bytes[0], |
14162 | length_bytes[1], |
14163 | length_bytes[2], |
14164 | length_bytes[3], |
14165 | self.keys[0], |
14166 | self.keys[1], |
14167 | self.keys[2], |
14168 | self.keys[3], |
14169 | self.keys[4], |
14170 | self.keys[5], |
14171 | self.keys[6], |
14172 | self.keys[7], |
14173 | self.keys[8], |
14174 | self.keys[9], |
14175 | self.keys[10], |
14176 | self.keys[11], |
14177 | self.keys[12], |
14178 | self.keys[13], |
14179 | self.keys[14], |
14180 | self.keys[15], |
14181 | self.keys[16], |
14182 | self.keys[17], |
14183 | self.keys[18], |
14184 | self.keys[19], |
14185 | self.keys[20], |
14186 | self.keys[21], |
14187 | self.keys[22], |
14188 | self.keys[23], |
14189 | self.keys[24], |
14190 | self.keys[25], |
14191 | self.keys[26], |
14192 | self.keys[27], |
14193 | self.keys[28], |
14194 | self.keys[29], |
14195 | self.keys[30], |
14196 | self.keys[31], |
14197 | ] |
14198 | } |
14199 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
14200 | bytes.reserve(40); |
14201 | let response_type_bytes = &[1]; |
14202 | bytes.push(response_type_bytes[0]); |
14203 | bytes.extend_from_slice(&[0; 1]); |
14204 | self.sequence.serialize_into(bytes); |
14205 | self.length.serialize_into(bytes); |
14206 | bytes.extend_from_slice(&self.keys); |
14207 | } |
14208 | } |
14209 | |
14210 | /// Opcode for the OpenFont request |
14211 | pub const OPEN_FONT_REQUEST: u8 = 45; |
14212 | /// opens a font. |
14213 | /// |
14214 | /// Opens any X core font matching the given `name` (for example "-misc-fixed-*"). |
14215 | /// |
14216 | /// Note that X core fonts are deprecated (but still supported) in favor of |
14217 | /// client-side rendering using Xft. |
14218 | /// |
14219 | /// # Fields |
14220 | /// |
14221 | /// * `fid` - The ID with which you will refer to the font, created by `xcb_generate_id`. |
14222 | /// * `name` - A pattern describing an X core font. |
14223 | /// |
14224 | /// # Errors |
14225 | /// |
14226 | /// * `Name` - No font matches the given `name`. |
14227 | /// |
14228 | /// # See |
14229 | /// |
14230 | /// * `xcb_generate_id`: function |
14231 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14232 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14233 | pub struct OpenFontRequest<'input> { |
14234 | pub fid: Font, |
14235 | pub name: Cow<'input, [u8]>, |
14236 | } |
14237 | impl<'input> OpenFontRequest<'input> { |
14238 | /// Serialize this request into bytes for the provided connection |
14239 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
14240 | let length_so_far = 0; |
14241 | let fid_bytes = self.fid.serialize(); |
14242 | let name_len = u16::try_from(self.name.len()).expect("`name` has too many elements" ); |
14243 | let name_len_bytes = name_len.serialize(); |
14244 | let mut request0 = vec![ |
14245 | OPEN_FONT_REQUEST, |
14246 | 0, |
14247 | 0, |
14248 | 0, |
14249 | fid_bytes[0], |
14250 | fid_bytes[1], |
14251 | fid_bytes[2], |
14252 | fid_bytes[3], |
14253 | name_len_bytes[0], |
14254 | name_len_bytes[1], |
14255 | 0, |
14256 | 0, |
14257 | ]; |
14258 | let length_so_far = length_so_far + request0.len(); |
14259 | let length_so_far = length_so_far + self.name.len(); |
14260 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
14261 | let length_so_far = length_so_far + padding0.len(); |
14262 | assert_eq!(length_so_far % 4, 0); |
14263 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
14264 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
14265 | (vec![request0.into(), self.name, padding0.into()], vec![]) |
14266 | } |
14267 | /// Parse this request given its header, its body, and any fds that go along with it |
14268 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
14269 | if header.major_opcode != OPEN_FONT_REQUEST { |
14270 | return Err(ParseError::InvalidValue); |
14271 | } |
14272 | let remaining = &[header.minor_opcode]; |
14273 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
14274 | let _ = remaining; |
14275 | let (fid, remaining) = Font::try_parse(value)?; |
14276 | let (name_len, remaining) = u16::try_parse(remaining)?; |
14277 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
14278 | let (name, remaining) = crate::x11_utils::parse_u8_list(remaining, name_len.try_to_usize()?)?; |
14279 | let _ = remaining; |
14280 | Ok(OpenFontRequest { |
14281 | fid, |
14282 | name: Cow::Borrowed(name), |
14283 | }) |
14284 | } |
14285 | /// Clone all borrowed data in this OpenFontRequest. |
14286 | pub fn into_owned(self) -> OpenFontRequest<'static> { |
14287 | OpenFontRequest { |
14288 | fid: self.fid, |
14289 | name: Cow::Owned(self.name.into_owned()), |
14290 | } |
14291 | } |
14292 | } |
14293 | impl<'input> Request for OpenFontRequest<'input> { |
14294 | const EXTENSION_NAME: Option<&'static str> = None; |
14295 | |
14296 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
14297 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
14298 | // Flatten the buffers into a single vector |
14299 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
14300 | (buf, fds) |
14301 | } |
14302 | } |
14303 | impl<'input> crate::x11_utils::VoidRequest for OpenFontRequest<'input> { |
14304 | } |
14305 | |
14306 | /// Opcode for the CloseFont request |
14307 | pub const CLOSE_FONT_REQUEST: u8 = 46; |
14308 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14309 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14310 | pub struct CloseFontRequest { |
14311 | pub font: Font, |
14312 | } |
14313 | impl CloseFontRequest { |
14314 | /// Serialize this request into bytes for the provided connection |
14315 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
14316 | let length_so_far = 0; |
14317 | let font_bytes = self.font.serialize(); |
14318 | let mut request0 = vec![ |
14319 | CLOSE_FONT_REQUEST, |
14320 | 0, |
14321 | 0, |
14322 | 0, |
14323 | font_bytes[0], |
14324 | font_bytes[1], |
14325 | font_bytes[2], |
14326 | font_bytes[3], |
14327 | ]; |
14328 | let length_so_far = length_so_far + request0.len(); |
14329 | assert_eq!(length_so_far % 4, 0); |
14330 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
14331 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
14332 | (vec![request0.into()], vec![]) |
14333 | } |
14334 | /// Parse this request given its header, its body, and any fds that go along with it |
14335 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
14336 | if header.major_opcode != CLOSE_FONT_REQUEST { |
14337 | return Err(ParseError::InvalidValue); |
14338 | } |
14339 | let remaining = &[header.minor_opcode]; |
14340 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
14341 | let _ = remaining; |
14342 | let (font, remaining) = Font::try_parse(value)?; |
14343 | let _ = remaining; |
14344 | Ok(CloseFontRequest { |
14345 | font, |
14346 | }) |
14347 | } |
14348 | } |
14349 | impl Request for CloseFontRequest { |
14350 | const EXTENSION_NAME: Option<&'static str> = None; |
14351 | |
14352 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
14353 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
14354 | // Flatten the buffers into a single vector |
14355 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
14356 | (buf, fds) |
14357 | } |
14358 | } |
14359 | impl crate::x11_utils::VoidRequest for CloseFontRequest { |
14360 | } |
14361 | |
14362 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14363 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14364 | pub struct FontDraw(u8); |
14365 | impl FontDraw { |
14366 | pub const LEFT_TO_RIGHT: Self = Self(0); |
14367 | pub const RIGHT_TO_LEFT: Self = Self(1); |
14368 | } |
14369 | impl From<FontDraw> for u8 { |
14370 | #[inline ] |
14371 | fn from(input: FontDraw) -> Self { |
14372 | input.0 |
14373 | } |
14374 | } |
14375 | impl From<FontDraw> for Option<u8> { |
14376 | #[inline ] |
14377 | fn from(input: FontDraw) -> Self { |
14378 | Some(input.0) |
14379 | } |
14380 | } |
14381 | impl From<FontDraw> for u16 { |
14382 | #[inline ] |
14383 | fn from(input: FontDraw) -> Self { |
14384 | u16::from(input.0) |
14385 | } |
14386 | } |
14387 | impl From<FontDraw> for Option<u16> { |
14388 | #[inline ] |
14389 | fn from(input: FontDraw) -> Self { |
14390 | Some(u16::from(input.0)) |
14391 | } |
14392 | } |
14393 | impl From<FontDraw> for u32 { |
14394 | #[inline ] |
14395 | fn from(input: FontDraw) -> Self { |
14396 | u32::from(input.0) |
14397 | } |
14398 | } |
14399 | impl From<FontDraw> for Option<u32> { |
14400 | #[inline ] |
14401 | fn from(input: FontDraw) -> Self { |
14402 | Some(u32::from(input.0)) |
14403 | } |
14404 | } |
14405 | impl From<u8> for FontDraw { |
14406 | #[inline ] |
14407 | fn from(value: u8) -> Self { |
14408 | Self(value) |
14409 | } |
14410 | } |
14411 | impl core::fmt::Debug for FontDraw { |
14412 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
14413 | let variants: [(u32, &str, &str); 2] = [ |
14414 | (Self::LEFT_TO_RIGHT.0.into(), "LEFT_TO_RIGHT" , "LeftToRight" ), |
14415 | (Self::RIGHT_TO_LEFT.0.into(), "RIGHT_TO_LEFT" , "RightToLeft" ), |
14416 | ]; |
14417 | pretty_print_enum(fmt, self.0.into(), &variants) |
14418 | } |
14419 | } |
14420 | |
14421 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14422 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14423 | pub struct Fontprop { |
14424 | pub name: Atom, |
14425 | pub value: u32, |
14426 | } |
14427 | impl TryParse for Fontprop { |
14428 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
14429 | let (name: u32, remaining: &[u8]) = Atom::try_parse(remaining)?; |
14430 | let (value: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
14431 | let result: Fontprop = Fontprop { name, value }; |
14432 | Ok((result, remaining)) |
14433 | } |
14434 | } |
14435 | impl Serialize for Fontprop { |
14436 | type Bytes = [u8; 8]; |
14437 | fn serialize(&self) -> [u8; 8] { |
14438 | let name_bytes: [u8; 4] = self.name.serialize(); |
14439 | let value_bytes: [u8; 4] = self.value.serialize(); |
14440 | [ |
14441 | name_bytes[0], |
14442 | name_bytes[1], |
14443 | name_bytes[2], |
14444 | name_bytes[3], |
14445 | value_bytes[0], |
14446 | value_bytes[1], |
14447 | value_bytes[2], |
14448 | value_bytes[3], |
14449 | ] |
14450 | } |
14451 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
14452 | bytes.reserve(additional:8); |
14453 | self.name.serialize_into(bytes); |
14454 | self.value.serialize_into(bytes); |
14455 | } |
14456 | } |
14457 | |
14458 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14459 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14460 | pub struct Charinfo { |
14461 | pub left_side_bearing: i16, |
14462 | pub right_side_bearing: i16, |
14463 | pub character_width: i16, |
14464 | pub ascent: i16, |
14465 | pub descent: i16, |
14466 | pub attributes: u16, |
14467 | } |
14468 | impl TryParse for Charinfo { |
14469 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
14470 | let (left_side_bearing: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
14471 | let (right_side_bearing: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
14472 | let (character_width: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
14473 | let (ascent: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
14474 | let (descent: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
14475 | let (attributes: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
14476 | let result: Charinfo = Charinfo { left_side_bearing, right_side_bearing, character_width, ascent, descent, attributes }; |
14477 | Ok((result, remaining)) |
14478 | } |
14479 | } |
14480 | impl Serialize for Charinfo { |
14481 | type Bytes = [u8; 12]; |
14482 | fn serialize(&self) -> [u8; 12] { |
14483 | let left_side_bearing_bytes = self.left_side_bearing.serialize(); |
14484 | let right_side_bearing_bytes = self.right_side_bearing.serialize(); |
14485 | let character_width_bytes = self.character_width.serialize(); |
14486 | let ascent_bytes = self.ascent.serialize(); |
14487 | let descent_bytes = self.descent.serialize(); |
14488 | let attributes_bytes = self.attributes.serialize(); |
14489 | [ |
14490 | left_side_bearing_bytes[0], |
14491 | left_side_bearing_bytes[1], |
14492 | right_side_bearing_bytes[0], |
14493 | right_side_bearing_bytes[1], |
14494 | character_width_bytes[0], |
14495 | character_width_bytes[1], |
14496 | ascent_bytes[0], |
14497 | ascent_bytes[1], |
14498 | descent_bytes[0], |
14499 | descent_bytes[1], |
14500 | attributes_bytes[0], |
14501 | attributes_bytes[1], |
14502 | ] |
14503 | } |
14504 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
14505 | bytes.reserve(12); |
14506 | self.left_side_bearing.serialize_into(bytes); |
14507 | self.right_side_bearing.serialize_into(bytes); |
14508 | self.character_width.serialize_into(bytes); |
14509 | self.ascent.serialize_into(bytes); |
14510 | self.descent.serialize_into(bytes); |
14511 | self.attributes.serialize_into(bytes); |
14512 | } |
14513 | } |
14514 | |
14515 | /// Opcode for the QueryFont request |
14516 | pub const QUERY_FONT_REQUEST: u8 = 47; |
14517 | /// query font metrics. |
14518 | /// |
14519 | /// Queries information associated with the font. |
14520 | /// |
14521 | /// # Fields |
14522 | /// |
14523 | /// * `font` - The fontable (Font or Graphics Context) to query. |
14524 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14525 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14526 | pub struct QueryFontRequest { |
14527 | pub font: Fontable, |
14528 | } |
14529 | impl QueryFontRequest { |
14530 | /// Serialize this request into bytes for the provided connection |
14531 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
14532 | let length_so_far = 0; |
14533 | let font_bytes = self.font.serialize(); |
14534 | let mut request0 = vec![ |
14535 | QUERY_FONT_REQUEST, |
14536 | 0, |
14537 | 0, |
14538 | 0, |
14539 | font_bytes[0], |
14540 | font_bytes[1], |
14541 | font_bytes[2], |
14542 | font_bytes[3], |
14543 | ]; |
14544 | let length_so_far = length_so_far + request0.len(); |
14545 | assert_eq!(length_so_far % 4, 0); |
14546 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
14547 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
14548 | (vec![request0.into()], vec![]) |
14549 | } |
14550 | /// Parse this request given its header, its body, and any fds that go along with it |
14551 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
14552 | if header.major_opcode != QUERY_FONT_REQUEST { |
14553 | return Err(ParseError::InvalidValue); |
14554 | } |
14555 | let remaining = &[header.minor_opcode]; |
14556 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
14557 | let _ = remaining; |
14558 | let (font, remaining) = Fontable::try_parse(value)?; |
14559 | let _ = remaining; |
14560 | Ok(QueryFontRequest { |
14561 | font, |
14562 | }) |
14563 | } |
14564 | } |
14565 | impl Request for QueryFontRequest { |
14566 | const EXTENSION_NAME: Option<&'static str> = None; |
14567 | |
14568 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
14569 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
14570 | // Flatten the buffers into a single vector |
14571 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
14572 | (buf, fds) |
14573 | } |
14574 | } |
14575 | impl crate::x11_utils::ReplyRequest for QueryFontRequest { |
14576 | type Reply = QueryFontReply; |
14577 | } |
14578 | |
14579 | /// # Fields |
14580 | /// |
14581 | /// * `min_bounds` - minimum bounds over all existing char |
14582 | /// * `max_bounds` - maximum bounds over all existing char |
14583 | /// * `min_char_or_byte2` - first character |
14584 | /// * `max_char_or_byte2` - last character |
14585 | /// * `default_char` - char to print for undefined character |
14586 | /// * `all_chars_exist` - flag if all characters have nonzero size |
14587 | /// * `font_ascent` - baseline to top edge of raster |
14588 | /// * `font_descent` - baseline to bottom edge of raster |
14589 | /// * `draw_direction` - |
14590 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14591 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14592 | pub struct QueryFontReply { |
14593 | pub sequence: u16, |
14594 | pub length: u32, |
14595 | pub min_bounds: Charinfo, |
14596 | pub max_bounds: Charinfo, |
14597 | pub min_char_or_byte2: u16, |
14598 | pub max_char_or_byte2: u16, |
14599 | pub default_char: u16, |
14600 | pub draw_direction: FontDraw, |
14601 | pub min_byte1: u8, |
14602 | pub max_byte1: u8, |
14603 | pub all_chars_exist: bool, |
14604 | pub font_ascent: i16, |
14605 | pub font_descent: i16, |
14606 | pub properties: Vec<Fontprop>, |
14607 | pub char_infos: Vec<Charinfo>, |
14608 | } |
14609 | impl TryParse for QueryFontReply { |
14610 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
14611 | let remaining = initial_value; |
14612 | let (response_type, remaining) = u8::try_parse(remaining)?; |
14613 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
14614 | let (sequence, remaining) = u16::try_parse(remaining)?; |
14615 | let (length, remaining) = u32::try_parse(remaining)?; |
14616 | let (min_bounds, remaining) = Charinfo::try_parse(remaining)?; |
14617 | let remaining = remaining.get(4..).ok_or(ParseError::InsufficientData)?; |
14618 | let (max_bounds, remaining) = Charinfo::try_parse(remaining)?; |
14619 | let remaining = remaining.get(4..).ok_or(ParseError::InsufficientData)?; |
14620 | let (min_char_or_byte2, remaining) = u16::try_parse(remaining)?; |
14621 | let (max_char_or_byte2, remaining) = u16::try_parse(remaining)?; |
14622 | let (default_char, remaining) = u16::try_parse(remaining)?; |
14623 | let (properties_len, remaining) = u16::try_parse(remaining)?; |
14624 | let (draw_direction, remaining) = u8::try_parse(remaining)?; |
14625 | let (min_byte1, remaining) = u8::try_parse(remaining)?; |
14626 | let (max_byte1, remaining) = u8::try_parse(remaining)?; |
14627 | let (all_chars_exist, remaining) = bool::try_parse(remaining)?; |
14628 | let (font_ascent, remaining) = i16::try_parse(remaining)?; |
14629 | let (font_descent, remaining) = i16::try_parse(remaining)?; |
14630 | let (char_infos_len, remaining) = u32::try_parse(remaining)?; |
14631 | let (properties, remaining) = crate::x11_utils::parse_list::<Fontprop>(remaining, properties_len.try_to_usize()?)?; |
14632 | let (char_infos, remaining) = crate::x11_utils::parse_list::<Charinfo>(remaining, char_infos_len.try_to_usize()?)?; |
14633 | if response_type != 1 { |
14634 | return Err(ParseError::InvalidValue); |
14635 | } |
14636 | let draw_direction = draw_direction.into(); |
14637 | let result = QueryFontReply { sequence, length, min_bounds, max_bounds, min_char_or_byte2, max_char_or_byte2, default_char, draw_direction, min_byte1, max_byte1, all_chars_exist, font_ascent, font_descent, properties, char_infos }; |
14638 | let _ = remaining; |
14639 | let remaining = initial_value.get(32 + length as usize * 4..) |
14640 | .ok_or(ParseError::InsufficientData)?; |
14641 | Ok((result, remaining)) |
14642 | } |
14643 | } |
14644 | impl Serialize for QueryFontReply { |
14645 | type Bytes = Vec<u8>; |
14646 | fn serialize(&self) -> Vec<u8> { |
14647 | let mut result = Vec::new(); |
14648 | self.serialize_into(&mut result); |
14649 | result |
14650 | } |
14651 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
14652 | bytes.reserve(60); |
14653 | let response_type_bytes = &[1]; |
14654 | bytes.push(response_type_bytes[0]); |
14655 | bytes.extend_from_slice(&[0; 1]); |
14656 | self.sequence.serialize_into(bytes); |
14657 | self.length.serialize_into(bytes); |
14658 | self.min_bounds.serialize_into(bytes); |
14659 | bytes.extend_from_slice(&[0; 4]); |
14660 | self.max_bounds.serialize_into(bytes); |
14661 | bytes.extend_from_slice(&[0; 4]); |
14662 | self.min_char_or_byte2.serialize_into(bytes); |
14663 | self.max_char_or_byte2.serialize_into(bytes); |
14664 | self.default_char.serialize_into(bytes); |
14665 | let properties_len = u16::try_from(self.properties.len()).expect("`properties` has too many elements" ); |
14666 | properties_len.serialize_into(bytes); |
14667 | u8::from(self.draw_direction).serialize_into(bytes); |
14668 | self.min_byte1.serialize_into(bytes); |
14669 | self.max_byte1.serialize_into(bytes); |
14670 | self.all_chars_exist.serialize_into(bytes); |
14671 | self.font_ascent.serialize_into(bytes); |
14672 | self.font_descent.serialize_into(bytes); |
14673 | let char_infos_len = u32::try_from(self.char_infos.len()).expect("`char_infos` has too many elements" ); |
14674 | char_infos_len.serialize_into(bytes); |
14675 | self.properties.serialize_into(bytes); |
14676 | self.char_infos.serialize_into(bytes); |
14677 | } |
14678 | } |
14679 | impl QueryFontReply { |
14680 | /// Get the value of the `properties_len` field. |
14681 | /// |
14682 | /// The `properties_len` field is used as the length field of the `properties` field. |
14683 | /// This function computes the field's value again based on the length of the list. |
14684 | /// |
14685 | /// # Panics |
14686 | /// |
14687 | /// Panics if the value cannot be represented in the target type. This |
14688 | /// cannot happen with values of the struct received from the X11 server. |
14689 | pub fn properties_len(&self) -> u16 { |
14690 | self.properties.len() |
14691 | .try_into().unwrap() |
14692 | } |
14693 | /// Get the value of the `char_infos_len` field. |
14694 | /// |
14695 | /// The `char_infos_len` field is used as the length field of the `char_infos` field. |
14696 | /// This function computes the field's value again based on the length of the list. |
14697 | /// |
14698 | /// # Panics |
14699 | /// |
14700 | /// Panics if the value cannot be represented in the target type. This |
14701 | /// cannot happen with values of the struct received from the X11 server. |
14702 | pub fn char_infos_len(&self) -> u32 { |
14703 | self.char_infos.len() |
14704 | .try_into().unwrap() |
14705 | } |
14706 | } |
14707 | |
14708 | /// Opcode for the QueryTextExtents request |
14709 | pub const QUERY_TEXT_EXTENTS_REQUEST: u8 = 48; |
14710 | /// get text extents. |
14711 | /// |
14712 | /// Query text extents from the X11 server. This request returns the bounding box |
14713 | /// of the specified 16-bit character string in the specified `font` or the font |
14714 | /// contained in the specified graphics context. |
14715 | /// |
14716 | /// `font_ascent` is set to the maximum of the ascent metrics of all characters in |
14717 | /// the string. `font_descent` is set to the maximum of the descent metrics. |
14718 | /// `overall_width` is set to the sum of the character-width metrics of all |
14719 | /// characters in the string. For each character in the string, let W be the sum of |
14720 | /// the character-width metrics of all characters preceding it in the string. Let L |
14721 | /// be the left-side-bearing metric of the character plus W. Let R be the |
14722 | /// right-side-bearing metric of the character plus W. The lbearing member is set |
14723 | /// to the minimum L of all characters in the string. The rbearing member is set to |
14724 | /// the maximum R. |
14725 | /// |
14726 | /// For fonts defined with linear indexing rather than 2-byte matrix indexing, each |
14727 | /// `xcb_char2b_t` structure is interpreted as a 16-bit number with byte1 as the |
14728 | /// most significant byte. If the font has no defined default character, undefined |
14729 | /// characters in the string are taken to have all zero metrics. |
14730 | /// |
14731 | /// Characters with all zero metrics are ignored. If the font has no defined |
14732 | /// default_char, the undefined characters in the string are also ignored. |
14733 | /// |
14734 | /// # Fields |
14735 | /// |
14736 | /// * `font` - The `font` to calculate text extents in. You can also pass a graphics context. |
14737 | /// * `string_len` - The number of characters in `string`. |
14738 | /// * `string` - The text to get text extents for. |
14739 | /// |
14740 | /// # Errors |
14741 | /// |
14742 | /// * `GContext` - The specified graphics context does not exist. |
14743 | /// * `Font` - The specified `font` does not exist. |
14744 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14745 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14746 | pub struct QueryTextExtentsRequest<'input> { |
14747 | pub font: Fontable, |
14748 | pub string: Cow<'input, [Char2b]>, |
14749 | } |
14750 | impl<'input> QueryTextExtentsRequest<'input> { |
14751 | /// Serialize this request into bytes for the provided connection |
14752 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
14753 | let string_len = u32::try_from(self.string.len()).unwrap(); |
14754 | let length_so_far = 0; |
14755 | let odd_length = (u32::from(string_len) & 1u32) != 0; |
14756 | let odd_length_bytes = odd_length.serialize(); |
14757 | let font_bytes = self.font.serialize(); |
14758 | let mut request0 = vec![ |
14759 | QUERY_TEXT_EXTENTS_REQUEST, |
14760 | odd_length_bytes[0], |
14761 | 0, |
14762 | 0, |
14763 | font_bytes[0], |
14764 | font_bytes[1], |
14765 | font_bytes[2], |
14766 | font_bytes[3], |
14767 | ]; |
14768 | let length_so_far = length_so_far + request0.len(); |
14769 | let string_bytes = self.string.serialize(); |
14770 | let length_so_far = length_so_far + string_bytes.len(); |
14771 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
14772 | let length_so_far = length_so_far + padding0.len(); |
14773 | assert_eq!(length_so_far % 4, 0); |
14774 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
14775 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
14776 | (vec![request0.into(), string_bytes.into(), padding0.into()], vec![]) |
14777 | } |
14778 | /// Parse this request given its header, its body, and any fds that go along with it |
14779 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
14780 | if header.major_opcode != QUERY_TEXT_EXTENTS_REQUEST { |
14781 | return Err(ParseError::InvalidValue); |
14782 | } |
14783 | let remaining = &[header.minor_opcode]; |
14784 | let (odd_length, remaining) = bool::try_parse(remaining)?; |
14785 | let _ = remaining; |
14786 | let (font, remaining) = Fontable::try_parse(value)?; |
14787 | let mut remaining = remaining; |
14788 | // Length is 'everything left in the input' |
14789 | let mut string = Vec::new(); |
14790 | while !remaining.is_empty() { |
14791 | let (v, new_remaining) = Char2b::try_parse(remaining)?; |
14792 | remaining = new_remaining; |
14793 | string.push(v); |
14794 | } |
14795 | let _ = remaining; |
14796 | if odd_length { |
14797 | if string.is_empty() { |
14798 | return Err(ParseError::InvalidValue); |
14799 | } |
14800 | string.truncate(string.len() - 1); |
14801 | } |
14802 | Ok(QueryTextExtentsRequest { |
14803 | font, |
14804 | string: Cow::Owned(string), |
14805 | }) |
14806 | } |
14807 | /// Clone all borrowed data in this QueryTextExtentsRequest. |
14808 | pub fn into_owned(self) -> QueryTextExtentsRequest<'static> { |
14809 | QueryTextExtentsRequest { |
14810 | font: self.font, |
14811 | string: Cow::Owned(self.string.into_owned()), |
14812 | } |
14813 | } |
14814 | } |
14815 | impl<'input> Request for QueryTextExtentsRequest<'input> { |
14816 | const EXTENSION_NAME: Option<&'static str> = None; |
14817 | |
14818 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
14819 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
14820 | // Flatten the buffers into a single vector |
14821 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
14822 | (buf, fds) |
14823 | } |
14824 | } |
14825 | impl<'input> crate::x11_utils::ReplyRequest for QueryTextExtentsRequest<'input> { |
14826 | type Reply = QueryTextExtentsReply; |
14827 | } |
14828 | |
14829 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14830 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14831 | pub struct QueryTextExtentsReply { |
14832 | pub draw_direction: FontDraw, |
14833 | pub sequence: u16, |
14834 | pub length: u32, |
14835 | pub font_ascent: i16, |
14836 | pub font_descent: i16, |
14837 | pub overall_ascent: i16, |
14838 | pub overall_descent: i16, |
14839 | pub overall_width: i32, |
14840 | pub overall_left: i32, |
14841 | pub overall_right: i32, |
14842 | } |
14843 | impl TryParse for QueryTextExtentsReply { |
14844 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
14845 | let remaining = initial_value; |
14846 | let (response_type, remaining) = u8::try_parse(remaining)?; |
14847 | let (draw_direction, remaining) = u8::try_parse(remaining)?; |
14848 | let (sequence, remaining) = u16::try_parse(remaining)?; |
14849 | let (length, remaining) = u32::try_parse(remaining)?; |
14850 | let (font_ascent, remaining) = i16::try_parse(remaining)?; |
14851 | let (font_descent, remaining) = i16::try_parse(remaining)?; |
14852 | let (overall_ascent, remaining) = i16::try_parse(remaining)?; |
14853 | let (overall_descent, remaining) = i16::try_parse(remaining)?; |
14854 | let (overall_width, remaining) = i32::try_parse(remaining)?; |
14855 | let (overall_left, remaining) = i32::try_parse(remaining)?; |
14856 | let (overall_right, remaining) = i32::try_parse(remaining)?; |
14857 | if response_type != 1 { |
14858 | return Err(ParseError::InvalidValue); |
14859 | } |
14860 | let draw_direction = draw_direction.into(); |
14861 | let result = QueryTextExtentsReply { draw_direction, sequence, length, font_ascent, font_descent, overall_ascent, overall_descent, overall_width, overall_left, overall_right }; |
14862 | let _ = remaining; |
14863 | let remaining = initial_value.get(32 + length as usize * 4..) |
14864 | .ok_or(ParseError::InsufficientData)?; |
14865 | Ok((result, remaining)) |
14866 | } |
14867 | } |
14868 | impl Serialize for QueryTextExtentsReply { |
14869 | type Bytes = [u8; 28]; |
14870 | fn serialize(&self) -> [u8; 28] { |
14871 | let response_type_bytes = &[1]; |
14872 | let draw_direction_bytes = u8::from(self.draw_direction).serialize(); |
14873 | let sequence_bytes = self.sequence.serialize(); |
14874 | let length_bytes = self.length.serialize(); |
14875 | let font_ascent_bytes = self.font_ascent.serialize(); |
14876 | let font_descent_bytes = self.font_descent.serialize(); |
14877 | let overall_ascent_bytes = self.overall_ascent.serialize(); |
14878 | let overall_descent_bytes = self.overall_descent.serialize(); |
14879 | let overall_width_bytes = self.overall_width.serialize(); |
14880 | let overall_left_bytes = self.overall_left.serialize(); |
14881 | let overall_right_bytes = self.overall_right.serialize(); |
14882 | [ |
14883 | response_type_bytes[0], |
14884 | draw_direction_bytes[0], |
14885 | sequence_bytes[0], |
14886 | sequence_bytes[1], |
14887 | length_bytes[0], |
14888 | length_bytes[1], |
14889 | length_bytes[2], |
14890 | length_bytes[3], |
14891 | font_ascent_bytes[0], |
14892 | font_ascent_bytes[1], |
14893 | font_descent_bytes[0], |
14894 | font_descent_bytes[1], |
14895 | overall_ascent_bytes[0], |
14896 | overall_ascent_bytes[1], |
14897 | overall_descent_bytes[0], |
14898 | overall_descent_bytes[1], |
14899 | overall_width_bytes[0], |
14900 | overall_width_bytes[1], |
14901 | overall_width_bytes[2], |
14902 | overall_width_bytes[3], |
14903 | overall_left_bytes[0], |
14904 | overall_left_bytes[1], |
14905 | overall_left_bytes[2], |
14906 | overall_left_bytes[3], |
14907 | overall_right_bytes[0], |
14908 | overall_right_bytes[1], |
14909 | overall_right_bytes[2], |
14910 | overall_right_bytes[3], |
14911 | ] |
14912 | } |
14913 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
14914 | bytes.reserve(28); |
14915 | let response_type_bytes = &[1]; |
14916 | bytes.push(response_type_bytes[0]); |
14917 | u8::from(self.draw_direction).serialize_into(bytes); |
14918 | self.sequence.serialize_into(bytes); |
14919 | self.length.serialize_into(bytes); |
14920 | self.font_ascent.serialize_into(bytes); |
14921 | self.font_descent.serialize_into(bytes); |
14922 | self.overall_ascent.serialize_into(bytes); |
14923 | self.overall_descent.serialize_into(bytes); |
14924 | self.overall_width.serialize_into(bytes); |
14925 | self.overall_left.serialize_into(bytes); |
14926 | self.overall_right.serialize_into(bytes); |
14927 | } |
14928 | } |
14929 | |
14930 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14931 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14932 | pub struct Str { |
14933 | pub name: Vec<u8>, |
14934 | } |
14935 | impl TryParse for Str { |
14936 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
14937 | let (name_len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
14938 | let (name: &[u8], remaining: &[u8]) = crate::x11_utils::parse_u8_list(data:remaining, list_length:name_len.try_to_usize()?)?; |
14939 | let name: Vec = name.to_vec(); |
14940 | let result: Str = Str { name }; |
14941 | Ok((result, remaining)) |
14942 | } |
14943 | } |
14944 | impl Serialize for Str { |
14945 | type Bytes = Vec<u8>; |
14946 | fn serialize(&self) -> Vec<u8> { |
14947 | let mut result: Vec = Vec::new(); |
14948 | self.serialize_into(&mut result); |
14949 | result |
14950 | } |
14951 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
14952 | let name_len: u8 = u8::try_from(self.name.len()).expect(msg:"`name` has too many elements" ); |
14953 | name_len.serialize_into(bytes); |
14954 | bytes.extend_from_slice(&self.name); |
14955 | } |
14956 | } |
14957 | impl Str { |
14958 | /// Get the value of the `name_len` field. |
14959 | /// |
14960 | /// The `name_len` field is used as the length field of the `name` field. |
14961 | /// This function computes the field's value again based on the length of the list. |
14962 | /// |
14963 | /// # Panics |
14964 | /// |
14965 | /// Panics if the value cannot be represented in the target type. This |
14966 | /// cannot happen with values of the struct received from the X11 server. |
14967 | pub fn name_len(&self) -> u8 { |
14968 | self.name.len() |
14969 | .try_into().unwrap() |
14970 | } |
14971 | } |
14972 | |
14973 | /// Opcode for the ListFonts request |
14974 | pub const LIST_FONTS_REQUEST: u8 = 49; |
14975 | /// get matching font names. |
14976 | /// |
14977 | /// Gets a list of available font names which match the given `pattern`. |
14978 | /// |
14979 | /// # Fields |
14980 | /// |
14981 | /// * `pattern` - A font pattern, for example "-misc-fixed-*". |
14982 | /// |
14983 | /// The asterisk (*) is a wildcard for any number of characters. The question mark |
14984 | /// (?) is a wildcard for a single character. Use of uppercase or lowercase does |
14985 | /// not matter. |
14986 | /// * `max_names` - The maximum number of fonts to be returned. |
14987 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
14988 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
14989 | pub struct ListFontsRequest<'input> { |
14990 | pub max_names: u16, |
14991 | pub pattern: Cow<'input, [u8]>, |
14992 | } |
14993 | impl<'input> ListFontsRequest<'input> { |
14994 | /// Serialize this request into bytes for the provided connection |
14995 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
14996 | let length_so_far = 0; |
14997 | let max_names_bytes = self.max_names.serialize(); |
14998 | let pattern_len = u16::try_from(self.pattern.len()).expect("`pattern` has too many elements" ); |
14999 | let pattern_len_bytes = pattern_len.serialize(); |
15000 | let mut request0 = vec![ |
15001 | LIST_FONTS_REQUEST, |
15002 | 0, |
15003 | 0, |
15004 | 0, |
15005 | max_names_bytes[0], |
15006 | max_names_bytes[1], |
15007 | pattern_len_bytes[0], |
15008 | pattern_len_bytes[1], |
15009 | ]; |
15010 | let length_so_far = length_so_far + request0.len(); |
15011 | let length_so_far = length_so_far + self.pattern.len(); |
15012 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
15013 | let length_so_far = length_so_far + padding0.len(); |
15014 | assert_eq!(length_so_far % 4, 0); |
15015 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
15016 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
15017 | (vec![request0.into(), self.pattern, padding0.into()], vec![]) |
15018 | } |
15019 | /// Parse this request given its header, its body, and any fds that go along with it |
15020 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
15021 | if header.major_opcode != LIST_FONTS_REQUEST { |
15022 | return Err(ParseError::InvalidValue); |
15023 | } |
15024 | let remaining = &[header.minor_opcode]; |
15025 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
15026 | let _ = remaining; |
15027 | let (max_names, remaining) = u16::try_parse(value)?; |
15028 | let (pattern_len, remaining) = u16::try_parse(remaining)?; |
15029 | let (pattern, remaining) = crate::x11_utils::parse_u8_list(remaining, pattern_len.try_to_usize()?)?; |
15030 | let _ = remaining; |
15031 | Ok(ListFontsRequest { |
15032 | max_names, |
15033 | pattern: Cow::Borrowed(pattern), |
15034 | }) |
15035 | } |
15036 | /// Clone all borrowed data in this ListFontsRequest. |
15037 | pub fn into_owned(self) -> ListFontsRequest<'static> { |
15038 | ListFontsRequest { |
15039 | max_names: self.max_names, |
15040 | pattern: Cow::Owned(self.pattern.into_owned()), |
15041 | } |
15042 | } |
15043 | } |
15044 | impl<'input> Request for ListFontsRequest<'input> { |
15045 | const EXTENSION_NAME: Option<&'static str> = None; |
15046 | |
15047 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
15048 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
15049 | // Flatten the buffers into a single vector |
15050 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
15051 | (buf, fds) |
15052 | } |
15053 | } |
15054 | impl<'input> crate::x11_utils::ReplyRequest for ListFontsRequest<'input> { |
15055 | type Reply = ListFontsReply; |
15056 | } |
15057 | |
15058 | /// # Fields |
15059 | /// |
15060 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
15061 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
15062 | pub struct ListFontsReply { |
15063 | pub sequence: u16, |
15064 | pub length: u32, |
15065 | pub names: Vec<Str>, |
15066 | } |
15067 | impl TryParse for ListFontsReply { |
15068 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
15069 | let remaining: &[u8] = initial_value; |
15070 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
15071 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
15072 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
15073 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
15074 | let (names_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
15075 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
15076 | let (names: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<Str>(data:remaining, list_length:names_len.try_to_usize()?)?; |
15077 | if response_type != 1 { |
15078 | return Err(ParseError::InvalidValue); |
15079 | } |
15080 | let result: ListFontsReply = ListFontsReply { sequence, length, names }; |
15081 | let _ = remaining; |
15082 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
15083 | .ok_or(err:ParseError::InsufficientData)?; |
15084 | Ok((result, remaining)) |
15085 | } |
15086 | } |
15087 | impl Serialize for ListFontsReply { |
15088 | type Bytes = Vec<u8>; |
15089 | fn serialize(&self) -> Vec<u8> { |
15090 | let mut result: Vec = Vec::new(); |
15091 | self.serialize_into(&mut result); |
15092 | result |
15093 | } |
15094 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
15095 | bytes.reserve(additional:32); |
15096 | let response_type_bytes: &[u8; 1] = &[1]; |
15097 | bytes.push(response_type_bytes[0]); |
15098 | bytes.extend_from_slice(&[0; 1]); |
15099 | self.sequence.serialize_into(bytes); |
15100 | self.length.serialize_into(bytes); |
15101 | let names_len: u16 = u16::try_from(self.names.len()).expect(msg:"`names` has too many elements" ); |
15102 | names_len.serialize_into(bytes); |
15103 | bytes.extend_from_slice(&[0; 22]); |
15104 | self.names.serialize_into(bytes); |
15105 | } |
15106 | } |
15107 | impl ListFontsReply { |
15108 | /// Get the value of the `names_len` field. |
15109 | /// |
15110 | /// The `names_len` field is used as the length field of the `names` field. |
15111 | /// This function computes the field's value again based on the length of the list. |
15112 | /// |
15113 | /// # Panics |
15114 | /// |
15115 | /// Panics if the value cannot be represented in the target type. This |
15116 | /// cannot happen with values of the struct received from the X11 server. |
15117 | pub fn names_len(&self) -> u16 { |
15118 | self.names.len() |
15119 | .try_into().unwrap() |
15120 | } |
15121 | } |
15122 | |
15123 | /// Opcode for the ListFontsWithInfo request |
15124 | pub const LIST_FONTS_WITH_INFO_REQUEST: u8 = 50; |
15125 | /// get matching font names and information. |
15126 | /// |
15127 | /// Gets a list of available font names which match the given `pattern`. |
15128 | /// |
15129 | /// # Fields |
15130 | /// |
15131 | /// * `pattern` - A font pattern, for example "-misc-fixed-*". |
15132 | /// |
15133 | /// The asterisk (*) is a wildcard for any number of characters. The question mark |
15134 | /// (?) is a wildcard for a single character. Use of uppercase or lowercase does |
15135 | /// not matter. |
15136 | /// * `max_names` - The maximum number of fonts to be returned. |
15137 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
15138 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
15139 | pub struct ListFontsWithInfoRequest<'input> { |
15140 | pub max_names: u16, |
15141 | pub pattern: Cow<'input, [u8]>, |
15142 | } |
15143 | impl<'input> ListFontsWithInfoRequest<'input> { |
15144 | /// Serialize this request into bytes for the provided connection |
15145 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
15146 | let length_so_far = 0; |
15147 | let max_names_bytes = self.max_names.serialize(); |
15148 | let pattern_len = u16::try_from(self.pattern.len()).expect("`pattern` has too many elements" ); |
15149 | let pattern_len_bytes = pattern_len.serialize(); |
15150 | let mut request0 = vec![ |
15151 | LIST_FONTS_WITH_INFO_REQUEST, |
15152 | 0, |
15153 | 0, |
15154 | 0, |
15155 | max_names_bytes[0], |
15156 | max_names_bytes[1], |
15157 | pattern_len_bytes[0], |
15158 | pattern_len_bytes[1], |
15159 | ]; |
15160 | let length_so_far = length_so_far + request0.len(); |
15161 | let length_so_far = length_so_far + self.pattern.len(); |
15162 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
15163 | let length_so_far = length_so_far + padding0.len(); |
15164 | assert_eq!(length_so_far % 4, 0); |
15165 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
15166 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
15167 | (vec![request0.into(), self.pattern, padding0.into()], vec![]) |
15168 | } |
15169 | /// Parse this request given its header, its body, and any fds that go along with it |
15170 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
15171 | if header.major_opcode != LIST_FONTS_WITH_INFO_REQUEST { |
15172 | return Err(ParseError::InvalidValue); |
15173 | } |
15174 | let remaining = &[header.minor_opcode]; |
15175 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
15176 | let _ = remaining; |
15177 | let (max_names, remaining) = u16::try_parse(value)?; |
15178 | let (pattern_len, remaining) = u16::try_parse(remaining)?; |
15179 | let (pattern, remaining) = crate::x11_utils::parse_u8_list(remaining, pattern_len.try_to_usize()?)?; |
15180 | let _ = remaining; |
15181 | Ok(ListFontsWithInfoRequest { |
15182 | max_names, |
15183 | pattern: Cow::Borrowed(pattern), |
15184 | }) |
15185 | } |
15186 | /// Clone all borrowed data in this ListFontsWithInfoRequest. |
15187 | pub fn into_owned(self) -> ListFontsWithInfoRequest<'static> { |
15188 | ListFontsWithInfoRequest { |
15189 | max_names: self.max_names, |
15190 | pattern: Cow::Owned(self.pattern.into_owned()), |
15191 | } |
15192 | } |
15193 | } |
15194 | impl<'input> Request for ListFontsWithInfoRequest<'input> { |
15195 | const EXTENSION_NAME: Option<&'static str> = None; |
15196 | |
15197 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
15198 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
15199 | // Flatten the buffers into a single vector |
15200 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
15201 | (buf, fds) |
15202 | } |
15203 | } |
15204 | impl<'input> crate::x11_utils::ReplyRequest for ListFontsWithInfoRequest<'input> { |
15205 | type Reply = ListFontsWithInfoReply; |
15206 | } |
15207 | |
15208 | /// # Fields |
15209 | /// |
15210 | /// * `min_bounds` - minimum bounds over all existing char |
15211 | /// * `max_bounds` - maximum bounds over all existing char |
15212 | /// * `min_char_or_byte2` - first character |
15213 | /// * `max_char_or_byte2` - last character |
15214 | /// * `default_char` - char to print for undefined character |
15215 | /// * `all_chars_exist` - flag if all characters have nonzero size |
15216 | /// * `font_ascent` - baseline to top edge of raster |
15217 | /// * `font_descent` - baseline to bottom edge of raster |
15218 | /// * `replies_hint` - An indication of how many more fonts will be returned. This is only a hint and |
15219 | /// may be larger or smaller than the number of fonts actually returned. A zero |
15220 | /// value does not guarantee that no more fonts will be returned. |
15221 | /// * `draw_direction` - |
15222 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
15223 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
15224 | pub struct ListFontsWithInfoReply { |
15225 | pub sequence: u16, |
15226 | pub length: u32, |
15227 | pub min_bounds: Charinfo, |
15228 | pub max_bounds: Charinfo, |
15229 | pub min_char_or_byte2: u16, |
15230 | pub max_char_or_byte2: u16, |
15231 | pub default_char: u16, |
15232 | pub draw_direction: FontDraw, |
15233 | pub min_byte1: u8, |
15234 | pub max_byte1: u8, |
15235 | pub all_chars_exist: bool, |
15236 | pub font_ascent: i16, |
15237 | pub font_descent: i16, |
15238 | pub replies_hint: u32, |
15239 | pub properties: Vec<Fontprop>, |
15240 | pub name: Vec<u8>, |
15241 | } |
15242 | impl TryParse for ListFontsWithInfoReply { |
15243 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
15244 | let remaining = initial_value; |
15245 | let (response_type, remaining) = u8::try_parse(remaining)?; |
15246 | let (name_len, remaining) = u8::try_parse(remaining)?; |
15247 | let (sequence, remaining) = u16::try_parse(remaining)?; |
15248 | let (length, remaining) = u32::try_parse(remaining)?; |
15249 | let (min_bounds, remaining) = Charinfo::try_parse(remaining)?; |
15250 | let remaining = remaining.get(4..).ok_or(ParseError::InsufficientData)?; |
15251 | let (max_bounds, remaining) = Charinfo::try_parse(remaining)?; |
15252 | let remaining = remaining.get(4..).ok_or(ParseError::InsufficientData)?; |
15253 | let (min_char_or_byte2, remaining) = u16::try_parse(remaining)?; |
15254 | let (max_char_or_byte2, remaining) = u16::try_parse(remaining)?; |
15255 | let (default_char, remaining) = u16::try_parse(remaining)?; |
15256 | let (properties_len, remaining) = u16::try_parse(remaining)?; |
15257 | let (draw_direction, remaining) = u8::try_parse(remaining)?; |
15258 | let (min_byte1, remaining) = u8::try_parse(remaining)?; |
15259 | let (max_byte1, remaining) = u8::try_parse(remaining)?; |
15260 | let (all_chars_exist, remaining) = bool::try_parse(remaining)?; |
15261 | let (font_ascent, remaining) = i16::try_parse(remaining)?; |
15262 | let (font_descent, remaining) = i16::try_parse(remaining)?; |
15263 | let (replies_hint, remaining) = u32::try_parse(remaining)?; |
15264 | let (properties, remaining) = crate::x11_utils::parse_list::<Fontprop>(remaining, properties_len.try_to_usize()?)?; |
15265 | let (name, remaining) = crate::x11_utils::parse_u8_list(remaining, name_len.try_to_usize()?)?; |
15266 | let name = name.to_vec(); |
15267 | if response_type != 1 { |
15268 | return Err(ParseError::InvalidValue); |
15269 | } |
15270 | let draw_direction = draw_direction.into(); |
15271 | let result = ListFontsWithInfoReply { sequence, length, min_bounds, max_bounds, min_char_or_byte2, max_char_or_byte2, default_char, draw_direction, min_byte1, max_byte1, all_chars_exist, font_ascent, font_descent, replies_hint, properties, name }; |
15272 | let _ = remaining; |
15273 | let remaining = initial_value.get(32 + length as usize * 4..) |
15274 | .ok_or(ParseError::InsufficientData)?; |
15275 | Ok((result, remaining)) |
15276 | } |
15277 | } |
15278 | impl Serialize for ListFontsWithInfoReply { |
15279 | type Bytes = Vec<u8>; |
15280 | fn serialize(&self) -> Vec<u8> { |
15281 | let mut result = Vec::new(); |
15282 | self.serialize_into(&mut result); |
15283 | result |
15284 | } |
15285 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
15286 | bytes.reserve(60); |
15287 | let response_type_bytes = &[1]; |
15288 | bytes.push(response_type_bytes[0]); |
15289 | let name_len = u8::try_from(self.name.len()).expect("`name` has too many elements" ); |
15290 | name_len.serialize_into(bytes); |
15291 | self.sequence.serialize_into(bytes); |
15292 | self.length.serialize_into(bytes); |
15293 | self.min_bounds.serialize_into(bytes); |
15294 | bytes.extend_from_slice(&[0; 4]); |
15295 | self.max_bounds.serialize_into(bytes); |
15296 | bytes.extend_from_slice(&[0; 4]); |
15297 | self.min_char_or_byte2.serialize_into(bytes); |
15298 | self.max_char_or_byte2.serialize_into(bytes); |
15299 | self.default_char.serialize_into(bytes); |
15300 | let properties_len = u16::try_from(self.properties.len()).expect("`properties` has too many elements" ); |
15301 | properties_len.serialize_into(bytes); |
15302 | u8::from(self.draw_direction).serialize_into(bytes); |
15303 | self.min_byte1.serialize_into(bytes); |
15304 | self.max_byte1.serialize_into(bytes); |
15305 | self.all_chars_exist.serialize_into(bytes); |
15306 | self.font_ascent.serialize_into(bytes); |
15307 | self.font_descent.serialize_into(bytes); |
15308 | self.replies_hint.serialize_into(bytes); |
15309 | self.properties.serialize_into(bytes); |
15310 | bytes.extend_from_slice(&self.name); |
15311 | } |
15312 | } |
15313 | impl ListFontsWithInfoReply { |
15314 | /// Get the value of the `name_len` field. |
15315 | /// |
15316 | /// The `name_len` field is used as the length field of the `name` field. |
15317 | /// This function computes the field's value again based on the length of the list. |
15318 | /// |
15319 | /// # Panics |
15320 | /// |
15321 | /// Panics if the value cannot be represented in the target type. This |
15322 | /// cannot happen with values of the struct received from the X11 server. |
15323 | pub fn name_len(&self) -> u8 { |
15324 | self.name.len() |
15325 | .try_into().unwrap() |
15326 | } |
15327 | /// Get the value of the `properties_len` field. |
15328 | /// |
15329 | /// The `properties_len` field is used as the length field of the `properties` field. |
15330 | /// This function computes the field's value again based on the length of the list. |
15331 | /// |
15332 | /// # Panics |
15333 | /// |
15334 | /// Panics if the value cannot be represented in the target type. This |
15335 | /// cannot happen with values of the struct received from the X11 server. |
15336 | pub fn properties_len(&self) -> u16 { |
15337 | self.properties.len() |
15338 | .try_into().unwrap() |
15339 | } |
15340 | } |
15341 | |
15342 | /// Opcode for the SetFontPath request |
15343 | pub const SET_FONT_PATH_REQUEST: u8 = 51; |
15344 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
15345 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
15346 | pub struct SetFontPathRequest<'input> { |
15347 | pub font: Cow<'input, [Str]>, |
15348 | } |
15349 | impl<'input> SetFontPathRequest<'input> { |
15350 | /// Serialize this request into bytes for the provided connection |
15351 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
15352 | let length_so_far = 0; |
15353 | let font_qty = u16::try_from(self.font.len()).expect("`font` has too many elements" ); |
15354 | let font_qty_bytes = font_qty.serialize(); |
15355 | let mut request0 = vec![ |
15356 | SET_FONT_PATH_REQUEST, |
15357 | 0, |
15358 | 0, |
15359 | 0, |
15360 | font_qty_bytes[0], |
15361 | font_qty_bytes[1], |
15362 | 0, |
15363 | 0, |
15364 | ]; |
15365 | let length_so_far = length_so_far + request0.len(); |
15366 | let font_bytes = self.font.serialize(); |
15367 | let length_so_far = length_so_far + font_bytes.len(); |
15368 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
15369 | let length_so_far = length_so_far + padding0.len(); |
15370 | assert_eq!(length_so_far % 4, 0); |
15371 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
15372 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
15373 | (vec![request0.into(), font_bytes.into(), padding0.into()], vec![]) |
15374 | } |
15375 | /// Parse this request given its header, its body, and any fds that go along with it |
15376 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
15377 | if header.major_opcode != SET_FONT_PATH_REQUEST { |
15378 | return Err(ParseError::InvalidValue); |
15379 | } |
15380 | let remaining = &[header.minor_opcode]; |
15381 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
15382 | let _ = remaining; |
15383 | let (font_qty, remaining) = u16::try_parse(value)?; |
15384 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
15385 | let (font, remaining) = crate::x11_utils::parse_list::<Str>(remaining, font_qty.try_to_usize()?)?; |
15386 | let _ = remaining; |
15387 | Ok(SetFontPathRequest { |
15388 | font: Cow::Owned(font), |
15389 | }) |
15390 | } |
15391 | /// Clone all borrowed data in this SetFontPathRequest. |
15392 | pub fn into_owned(self) -> SetFontPathRequest<'static> { |
15393 | SetFontPathRequest { |
15394 | font: Cow::Owned(self.font.into_owned()), |
15395 | } |
15396 | } |
15397 | } |
15398 | impl<'input> Request for SetFontPathRequest<'input> { |
15399 | const EXTENSION_NAME: Option<&'static str> = None; |
15400 | |
15401 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
15402 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
15403 | // Flatten the buffers into a single vector |
15404 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
15405 | (buf, fds) |
15406 | } |
15407 | } |
15408 | impl<'input> crate::x11_utils::VoidRequest for SetFontPathRequest<'input> { |
15409 | } |
15410 | |
15411 | /// Opcode for the GetFontPath request |
15412 | pub const GET_FONT_PATH_REQUEST: u8 = 52; |
15413 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
15414 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
15415 | pub struct GetFontPathRequest; |
15416 | impl GetFontPathRequest { |
15417 | /// Serialize this request into bytes for the provided connection |
15418 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
15419 | let length_so_far = 0; |
15420 | let mut request0 = vec![ |
15421 | GET_FONT_PATH_REQUEST, |
15422 | 0, |
15423 | 0, |
15424 | 0, |
15425 | ]; |
15426 | let length_so_far = length_so_far + request0.len(); |
15427 | assert_eq!(length_so_far % 4, 0); |
15428 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
15429 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
15430 | (vec![request0.into()], vec![]) |
15431 | } |
15432 | /// Parse this request given its header, its body, and any fds that go along with it |
15433 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
15434 | if header.major_opcode != GET_FONT_PATH_REQUEST { |
15435 | return Err(ParseError::InvalidValue); |
15436 | } |
15437 | let remaining = &[header.minor_opcode]; |
15438 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
15439 | let _ = remaining; |
15440 | let _ = value; |
15441 | Ok(GetFontPathRequest |
15442 | ) |
15443 | } |
15444 | } |
15445 | impl Request for GetFontPathRequest { |
15446 | const EXTENSION_NAME: Option<&'static str> = None; |
15447 | |
15448 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
15449 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
15450 | // Flatten the buffers into a single vector |
15451 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
15452 | (buf, fds) |
15453 | } |
15454 | } |
15455 | impl crate::x11_utils::ReplyRequest for GetFontPathRequest { |
15456 | type Reply = GetFontPathReply; |
15457 | } |
15458 | |
15459 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
15460 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
15461 | pub struct GetFontPathReply { |
15462 | pub sequence: u16, |
15463 | pub length: u32, |
15464 | pub path: Vec<Str>, |
15465 | } |
15466 | impl TryParse for GetFontPathReply { |
15467 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
15468 | let remaining: &[u8] = initial_value; |
15469 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
15470 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
15471 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
15472 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
15473 | let (path_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
15474 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
15475 | let (path: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<Str>(data:remaining, list_length:path_len.try_to_usize()?)?; |
15476 | if response_type != 1 { |
15477 | return Err(ParseError::InvalidValue); |
15478 | } |
15479 | let result: GetFontPathReply = GetFontPathReply { sequence, length, path }; |
15480 | let _ = remaining; |
15481 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
15482 | .ok_or(err:ParseError::InsufficientData)?; |
15483 | Ok((result, remaining)) |
15484 | } |
15485 | } |
15486 | impl Serialize for GetFontPathReply { |
15487 | type Bytes = Vec<u8>; |
15488 | fn serialize(&self) -> Vec<u8> { |
15489 | let mut result: Vec = Vec::new(); |
15490 | self.serialize_into(&mut result); |
15491 | result |
15492 | } |
15493 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
15494 | bytes.reserve(additional:32); |
15495 | let response_type_bytes: &[u8; 1] = &[1]; |
15496 | bytes.push(response_type_bytes[0]); |
15497 | bytes.extend_from_slice(&[0; 1]); |
15498 | self.sequence.serialize_into(bytes); |
15499 | self.length.serialize_into(bytes); |
15500 | let path_len: u16 = u16::try_from(self.path.len()).expect(msg:"`path` has too many elements" ); |
15501 | path_len.serialize_into(bytes); |
15502 | bytes.extend_from_slice(&[0; 22]); |
15503 | self.path.serialize_into(bytes); |
15504 | } |
15505 | } |
15506 | impl GetFontPathReply { |
15507 | /// Get the value of the `path_len` field. |
15508 | /// |
15509 | /// The `path_len` field is used as the length field of the `path` field. |
15510 | /// This function computes the field's value again based on the length of the list. |
15511 | /// |
15512 | /// # Panics |
15513 | /// |
15514 | /// Panics if the value cannot be represented in the target type. This |
15515 | /// cannot happen with values of the struct received from the X11 server. |
15516 | pub fn path_len(&self) -> u16 { |
15517 | self.path.len() |
15518 | .try_into().unwrap() |
15519 | } |
15520 | } |
15521 | |
15522 | /// Opcode for the CreatePixmap request |
15523 | pub const CREATE_PIXMAP_REQUEST: u8 = 53; |
15524 | /// Creates a pixmap. |
15525 | /// |
15526 | /// Creates a pixmap. The pixmap can only be used on the same screen as `drawable` |
15527 | /// is on and only with drawables of the same `depth`. |
15528 | /// |
15529 | /// # Fields |
15530 | /// |
15531 | /// * `depth` - TODO |
15532 | /// * `pid` - The ID with which you will refer to the new pixmap, created by |
15533 | /// `xcb_generate_id`. |
15534 | /// * `drawable` - Drawable to get the screen from. |
15535 | /// * `width` - The width of the new pixmap. |
15536 | /// * `height` - The height of the new pixmap. |
15537 | /// |
15538 | /// # Errors |
15539 | /// |
15540 | /// * `Value` - TODO: reasons? |
15541 | /// * `Drawable` - The specified `drawable` (Window or Pixmap) does not exist. |
15542 | /// * `Alloc` - The X server could not allocate the requested resources (no memory?). |
15543 | /// |
15544 | /// # See |
15545 | /// |
15546 | /// * `xcb_generate_id`: function |
15547 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
15548 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
15549 | pub struct CreatePixmapRequest { |
15550 | pub depth: u8, |
15551 | pub pid: Pixmap, |
15552 | pub drawable: Drawable, |
15553 | pub width: u16, |
15554 | pub height: u16, |
15555 | } |
15556 | impl CreatePixmapRequest { |
15557 | /// Serialize this request into bytes for the provided connection |
15558 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
15559 | let length_so_far = 0; |
15560 | let depth_bytes = self.depth.serialize(); |
15561 | let pid_bytes = self.pid.serialize(); |
15562 | let drawable_bytes = self.drawable.serialize(); |
15563 | let width_bytes = self.width.serialize(); |
15564 | let height_bytes = self.height.serialize(); |
15565 | let mut request0 = vec![ |
15566 | CREATE_PIXMAP_REQUEST, |
15567 | depth_bytes[0], |
15568 | 0, |
15569 | 0, |
15570 | pid_bytes[0], |
15571 | pid_bytes[1], |
15572 | pid_bytes[2], |
15573 | pid_bytes[3], |
15574 | drawable_bytes[0], |
15575 | drawable_bytes[1], |
15576 | drawable_bytes[2], |
15577 | drawable_bytes[3], |
15578 | width_bytes[0], |
15579 | width_bytes[1], |
15580 | height_bytes[0], |
15581 | height_bytes[1], |
15582 | ]; |
15583 | let length_so_far = length_so_far + request0.len(); |
15584 | assert_eq!(length_so_far % 4, 0); |
15585 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
15586 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
15587 | (vec![request0.into()], vec![]) |
15588 | } |
15589 | /// Parse this request given its header, its body, and any fds that go along with it |
15590 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
15591 | if header.major_opcode != CREATE_PIXMAP_REQUEST { |
15592 | return Err(ParseError::InvalidValue); |
15593 | } |
15594 | let remaining = &[header.minor_opcode]; |
15595 | let (depth, remaining) = u8::try_parse(remaining)?; |
15596 | let _ = remaining; |
15597 | let (pid, remaining) = Pixmap::try_parse(value)?; |
15598 | let (drawable, remaining) = Drawable::try_parse(remaining)?; |
15599 | let (width, remaining) = u16::try_parse(remaining)?; |
15600 | let (height, remaining) = u16::try_parse(remaining)?; |
15601 | let _ = remaining; |
15602 | Ok(CreatePixmapRequest { |
15603 | depth, |
15604 | pid, |
15605 | drawable, |
15606 | width, |
15607 | height, |
15608 | }) |
15609 | } |
15610 | } |
15611 | impl Request for CreatePixmapRequest { |
15612 | const EXTENSION_NAME: Option<&'static str> = None; |
15613 | |
15614 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
15615 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
15616 | // Flatten the buffers into a single vector |
15617 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
15618 | (buf, fds) |
15619 | } |
15620 | } |
15621 | impl crate::x11_utils::VoidRequest for CreatePixmapRequest { |
15622 | } |
15623 | |
15624 | /// Opcode for the FreePixmap request |
15625 | pub const FREE_PIXMAP_REQUEST: u8 = 54; |
15626 | /// Destroys a pixmap. |
15627 | /// |
15628 | /// Deletes the association between the pixmap ID and the pixmap. The pixmap |
15629 | /// storage will be freed when there are no more references to it. |
15630 | /// |
15631 | /// # Fields |
15632 | /// |
15633 | /// * `pixmap` - The pixmap to destroy. |
15634 | /// |
15635 | /// # Errors |
15636 | /// |
15637 | /// * `Pixmap` - The specified pixmap does not exist. |
15638 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
15639 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
15640 | pub struct FreePixmapRequest { |
15641 | pub pixmap: Pixmap, |
15642 | } |
15643 | impl FreePixmapRequest { |
15644 | /// Serialize this request into bytes for the provided connection |
15645 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
15646 | let length_so_far = 0; |
15647 | let pixmap_bytes = self.pixmap.serialize(); |
15648 | let mut request0 = vec![ |
15649 | FREE_PIXMAP_REQUEST, |
15650 | 0, |
15651 | 0, |
15652 | 0, |
15653 | pixmap_bytes[0], |
15654 | pixmap_bytes[1], |
15655 | pixmap_bytes[2], |
15656 | pixmap_bytes[3], |
15657 | ]; |
15658 | let length_so_far = length_so_far + request0.len(); |
15659 | assert_eq!(length_so_far % 4, 0); |
15660 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
15661 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
15662 | (vec![request0.into()], vec![]) |
15663 | } |
15664 | /// Parse this request given its header, its body, and any fds that go along with it |
15665 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
15666 | if header.major_opcode != FREE_PIXMAP_REQUEST { |
15667 | return Err(ParseError::InvalidValue); |
15668 | } |
15669 | let remaining = &[header.minor_opcode]; |
15670 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
15671 | let _ = remaining; |
15672 | let (pixmap, remaining) = Pixmap::try_parse(value)?; |
15673 | let _ = remaining; |
15674 | Ok(FreePixmapRequest { |
15675 | pixmap, |
15676 | }) |
15677 | } |
15678 | } |
15679 | impl Request for FreePixmapRequest { |
15680 | const EXTENSION_NAME: Option<&'static str> = None; |
15681 | |
15682 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
15683 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
15684 | // Flatten the buffers into a single vector |
15685 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
15686 | (buf, fds) |
15687 | } |
15688 | } |
15689 | impl crate::x11_utils::VoidRequest for FreePixmapRequest { |
15690 | } |
15691 | |
15692 | /// # Fields |
15693 | /// |
15694 | /// * `Function` - TODO: Refer to GX |
15695 | /// * `PlaneMask` - In graphics operations, given a source and destination pixel, the result is |
15696 | /// computed bitwise on corresponding bits of the pixels; that is, a Boolean |
15697 | /// operation is performed in each bit plane. The plane-mask restricts the |
15698 | /// operation to a subset of planes, so the result is: |
15699 | /// |
15700 | /// ((src FUNC dst) AND plane-mask) OR (dst AND (NOT plane-mask)) |
15701 | /// * `Foreground` - Foreground colorpixel. |
15702 | /// * `Background` - Background colorpixel. |
15703 | /// * `LineWidth` - The line-width is measured in pixels and can be greater than or equal to one, a wide line, or the |
15704 | /// special value zero, a thin line. |
15705 | /// * `LineStyle` - The line-style defines which sections of a line are drawn: |
15706 | /// Solid The full path of the line is drawn. |
15707 | /// DoubleDash The full path of the line is drawn, but the even dashes are filled differently |
15708 | /// than the odd dashes (see fill-style), with Butt cap-style used where even and |
15709 | /// odd dashes meet. |
15710 | /// OnOffDash Only the even dashes are drawn, and cap-style applies to all internal ends of |
15711 | /// the individual dashes (except NotLast is treated as Butt). |
15712 | /// * `CapStyle` - The cap-style defines how the endpoints of a path are drawn: |
15713 | /// NotLast The result is equivalent to Butt, except that for a line-width of zero the final |
15714 | /// endpoint is not drawn. |
15715 | /// Butt The result is square at the endpoint (perpendicular to the slope of the line) |
15716 | /// with no projection beyond. |
15717 | /// Round The result is a circular arc with its diameter equal to the line-width, centered |
15718 | /// on the endpoint; it is equivalent to Butt for line-width zero. |
15719 | /// Projecting The result is square at the end, but the path continues beyond the endpoint for |
15720 | /// a distance equal to half the line-width; it is equivalent to Butt for line-width |
15721 | /// zero. |
15722 | /// * `JoinStyle` - The join-style defines how corners are drawn for wide lines: |
15723 | /// Miter The outer edges of the two lines extend to meet at an angle. However, if the |
15724 | /// angle is less than 11 degrees, a Bevel join-style is used instead. |
15725 | /// Round The result is a circular arc with a diameter equal to the line-width, centered |
15726 | /// on the joinpoint. |
15727 | /// Bevel The result is Butt endpoint styles, and then the triangular notch is filled. |
15728 | /// * `FillStyle` - The fill-style defines the contents of the source for line, text, and fill requests. For all text and fill |
15729 | /// requests (for example, PolyText8, PolyText16, PolyFillRectangle, FillPoly, and PolyFillArc) |
15730 | /// as well as for line requests with line-style Solid, (for example, PolyLine, PolySegment, |
15731 | /// PolyRectangle, PolyArc) and for the even dashes for line requests with line-style OnOffDash |
15732 | /// or DoubleDash: |
15733 | /// Solid Foreground |
15734 | /// Tiled Tile |
15735 | /// OpaqueStippled A tile with the same width and height as stipple but with background |
15736 | /// everywhere stipple has a zero and with foreground everywhere stipple |
15737 | /// has a one |
15738 | /// Stippled Foreground masked by stipple |
15739 | /// For the odd dashes for line requests with line-style DoubleDash: |
15740 | /// Solid Background |
15741 | /// Tiled Same as for even dashes |
15742 | /// OpaqueStippled Same as for even dashes |
15743 | /// Stippled Background masked by stipple |
15744 | /// * `FillRule` - |
15745 | /// * `Tile` - The tile/stipple represents an infinite two-dimensional plane with the tile/stipple replicated in all |
15746 | /// dimensions. When that plane is superimposed on the drawable for use in a graphics operation, |
15747 | /// the upper-left corner of some instance of the tile/stipple is at the coordinates within the drawable |
15748 | /// specified by the tile/stipple origin. The tile/stipple and clip origins are interpreted relative to the |
15749 | /// origin of whatever destination drawable is specified in a graphics request. |
15750 | /// The tile pixmap must have the same root and depth as the gcontext (or a Match error results). |
15751 | /// The stipple pixmap must have depth one and must have the same root as the gcontext (or a |
15752 | /// Match error results). For fill-style Stippled (but not fill-style |
15753 | /// OpaqueStippled), the stipple pattern is tiled in a single plane and acts as an |
15754 | /// additional clip mask to be ANDed with the clip-mask. |
15755 | /// Any size pixmap can be used for tiling or stippling, although some sizes may be faster to use than |
15756 | /// others. |
15757 | /// * `Stipple` - The tile/stipple represents an infinite two-dimensional plane with the tile/stipple replicated in all |
15758 | /// dimensions. When that plane is superimposed on the drawable for use in a graphics operation, |
15759 | /// the upper-left corner of some instance of the tile/stipple is at the coordinates within the drawable |
15760 | /// specified by the tile/stipple origin. The tile/stipple and clip origins are interpreted relative to the |
15761 | /// origin of whatever destination drawable is specified in a graphics request. |
15762 | /// The tile pixmap must have the same root and depth as the gcontext (or a Match error results). |
15763 | /// The stipple pixmap must have depth one and must have the same root as the gcontext (or a |
15764 | /// Match error results). For fill-style Stippled (but not fill-style |
15765 | /// OpaqueStippled), the stipple pattern is tiled in a single plane and acts as an |
15766 | /// additional clip mask to be ANDed with the clip-mask. |
15767 | /// Any size pixmap can be used for tiling or stippling, although some sizes may be faster to use than |
15768 | /// others. |
15769 | /// * `TileStippleOriginX` - TODO |
15770 | /// * `TileStippleOriginY` - TODO |
15771 | /// * `Font` - Which font to use for the `ImageText8` and `ImageText16` requests. |
15772 | /// * `SubwindowMode` - For ClipByChildren, both source and destination windows are additionally |
15773 | /// clipped by all viewable InputOutput children. For IncludeInferiors, neither |
15774 | /// source nor destination window is |
15775 | /// clipped by inferiors. This will result in including subwindow contents in the source and drawing |
15776 | /// through subwindow boundaries of the destination. The use of IncludeInferiors with a source or |
15777 | /// destination window of one depth with mapped inferiors of differing depth is not illegal, but the |
15778 | /// semantics is undefined by the core protocol. |
15779 | /// * `GraphicsExposures` - Whether ExposureEvents should be generated (1) or not (0). |
15780 | /// |
15781 | /// The default is 1. |
15782 | /// * `ClipOriginX` - TODO |
15783 | /// * `ClipOriginY` - TODO |
15784 | /// * `ClipMask` - The clip-mask restricts writes to the destination drawable. Only pixels where the clip-mask has |
15785 | /// bits set to 1 are drawn. Pixels are not drawn outside the area covered by the clip-mask or where |
15786 | /// the clip-mask has bits set to 0. The clip-mask affects all graphics requests, but it does not clip |
15787 | /// sources. The clip-mask origin is interpreted relative to the origin of whatever destination drawable is specified in a graphics request. If a pixmap is specified as the clip-mask, it must have |
15788 | /// depth 1 and have the same root as the gcontext (or a Match error results). If clip-mask is None, |
15789 | /// then pixels are always drawn, regardless of the clip origin. The clip-mask can also be set with the |
15790 | /// SetClipRectangles request. |
15791 | /// * `DashOffset` - TODO |
15792 | /// * `DashList` - TODO |
15793 | /// * `ArcMode` - TODO |
15794 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
15795 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
15796 | pub struct GC(u32); |
15797 | impl GC { |
15798 | pub const FUNCTION: Self = Self(1 << 0); |
15799 | pub const PLANE_MASK: Self = Self(1 << 1); |
15800 | pub const FOREGROUND: Self = Self(1 << 2); |
15801 | pub const BACKGROUND: Self = Self(1 << 3); |
15802 | pub const LINE_WIDTH: Self = Self(1 << 4); |
15803 | pub const LINE_STYLE: Self = Self(1 << 5); |
15804 | pub const CAP_STYLE: Self = Self(1 << 6); |
15805 | pub const JOIN_STYLE: Self = Self(1 << 7); |
15806 | pub const FILL_STYLE: Self = Self(1 << 8); |
15807 | pub const FILL_RULE: Self = Self(1 << 9); |
15808 | pub const TILE: Self = Self(1 << 10); |
15809 | pub const STIPPLE: Self = Self(1 << 11); |
15810 | pub const TILE_STIPPLE_ORIGIN_X: Self = Self(1 << 12); |
15811 | pub const TILE_STIPPLE_ORIGIN_Y: Self = Self(1 << 13); |
15812 | pub const FONT: Self = Self(1 << 14); |
15813 | pub const SUBWINDOW_MODE: Self = Self(1 << 15); |
15814 | pub const GRAPHICS_EXPOSURES: Self = Self(1 << 16); |
15815 | pub const CLIP_ORIGIN_X: Self = Self(1 << 17); |
15816 | pub const CLIP_ORIGIN_Y: Self = Self(1 << 18); |
15817 | pub const CLIP_MASK: Self = Self(1 << 19); |
15818 | pub const DASH_OFFSET: Self = Self(1 << 20); |
15819 | pub const DASH_LIST: Self = Self(1 << 21); |
15820 | pub const ARC_MODE: Self = Self(1 << 22); |
15821 | } |
15822 | impl From<GC> for u32 { |
15823 | #[inline ] |
15824 | fn from(input: GC) -> Self { |
15825 | input.0 |
15826 | } |
15827 | } |
15828 | impl From<GC> for Option<u32> { |
15829 | #[inline ] |
15830 | fn from(input: GC) -> Self { |
15831 | Some(input.0) |
15832 | } |
15833 | } |
15834 | impl From<u8> for GC { |
15835 | #[inline ] |
15836 | fn from(value: u8) -> Self { |
15837 | Self(value.into()) |
15838 | } |
15839 | } |
15840 | impl From<u16> for GC { |
15841 | #[inline ] |
15842 | fn from(value: u16) -> Self { |
15843 | Self(value.into()) |
15844 | } |
15845 | } |
15846 | impl From<u32> for GC { |
15847 | #[inline ] |
15848 | fn from(value: u32) -> Self { |
15849 | Self(value) |
15850 | } |
15851 | } |
15852 | impl core::fmt::Debug for GC { |
15853 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
15854 | let variants = [ |
15855 | (Self::FUNCTION.0, "FUNCTION" , "Function" ), |
15856 | (Self::PLANE_MASK.0, "PLANE_MASK" , "PlaneMask" ), |
15857 | (Self::FOREGROUND.0, "FOREGROUND" , "Foreground" ), |
15858 | (Self::BACKGROUND.0, "BACKGROUND" , "Background" ), |
15859 | (Self::LINE_WIDTH.0, "LINE_WIDTH" , "LineWidth" ), |
15860 | (Self::LINE_STYLE.0, "LINE_STYLE" , "LineStyle" ), |
15861 | (Self::CAP_STYLE.0, "CAP_STYLE" , "CapStyle" ), |
15862 | (Self::JOIN_STYLE.0, "JOIN_STYLE" , "JoinStyle" ), |
15863 | (Self::FILL_STYLE.0, "FILL_STYLE" , "FillStyle" ), |
15864 | (Self::FILL_RULE.0, "FILL_RULE" , "FillRule" ), |
15865 | (Self::TILE.0, "TILE" , "Tile" ), |
15866 | (Self::STIPPLE.0, "STIPPLE" , "Stipple" ), |
15867 | (Self::TILE_STIPPLE_ORIGIN_X.0, "TILE_STIPPLE_ORIGIN_X" , "TileStippleOriginX" ), |
15868 | (Self::TILE_STIPPLE_ORIGIN_Y.0, "TILE_STIPPLE_ORIGIN_Y" , "TileStippleOriginY" ), |
15869 | (Self::FONT.0, "FONT" , "Font" ), |
15870 | (Self::SUBWINDOW_MODE.0, "SUBWINDOW_MODE" , "SubwindowMode" ), |
15871 | (Self::GRAPHICS_EXPOSURES.0, "GRAPHICS_EXPOSURES" , "GraphicsExposures" ), |
15872 | (Self::CLIP_ORIGIN_X.0, "CLIP_ORIGIN_X" , "ClipOriginX" ), |
15873 | (Self::CLIP_ORIGIN_Y.0, "CLIP_ORIGIN_Y" , "ClipOriginY" ), |
15874 | (Self::CLIP_MASK.0, "CLIP_MASK" , "ClipMask" ), |
15875 | (Self::DASH_OFFSET.0, "DASH_OFFSET" , "DashOffset" ), |
15876 | (Self::DASH_LIST.0, "DASH_LIST" , "DashList" ), |
15877 | (Self::ARC_MODE.0, "ARC_MODE" , "ArcMode" ), |
15878 | ]; |
15879 | pretty_print_bitmask(fmt, self.0, &variants) |
15880 | } |
15881 | } |
15882 | bitmask_binop!(GC, u32); |
15883 | |
15884 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
15885 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
15886 | pub struct GX(u32); |
15887 | impl GX { |
15888 | pub const CLEAR: Self = Self(0); |
15889 | pub const AND: Self = Self(1); |
15890 | pub const AND_REVERSE: Self = Self(2); |
15891 | pub const COPY: Self = Self(3); |
15892 | pub const AND_INVERTED: Self = Self(4); |
15893 | pub const NOOP: Self = Self(5); |
15894 | pub const XOR: Self = Self(6); |
15895 | pub const OR: Self = Self(7); |
15896 | pub const NOR: Self = Self(8); |
15897 | pub const EQUIV: Self = Self(9); |
15898 | pub const INVERT: Self = Self(10); |
15899 | pub const OR_REVERSE: Self = Self(11); |
15900 | pub const COPY_INVERTED: Self = Self(12); |
15901 | pub const OR_INVERTED: Self = Self(13); |
15902 | pub const NAND: Self = Self(14); |
15903 | pub const SET: Self = Self(15); |
15904 | } |
15905 | impl From<GX> for u32 { |
15906 | #[inline ] |
15907 | fn from(input: GX) -> Self { |
15908 | input.0 |
15909 | } |
15910 | } |
15911 | impl From<GX> for Option<u32> { |
15912 | #[inline ] |
15913 | fn from(input: GX) -> Self { |
15914 | Some(input.0) |
15915 | } |
15916 | } |
15917 | impl From<u8> for GX { |
15918 | #[inline ] |
15919 | fn from(value: u8) -> Self { |
15920 | Self(value.into()) |
15921 | } |
15922 | } |
15923 | impl From<u16> for GX { |
15924 | #[inline ] |
15925 | fn from(value: u16) -> Self { |
15926 | Self(value.into()) |
15927 | } |
15928 | } |
15929 | impl From<u32> for GX { |
15930 | #[inline ] |
15931 | fn from(value: u32) -> Self { |
15932 | Self(value) |
15933 | } |
15934 | } |
15935 | impl core::fmt::Debug for GX { |
15936 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
15937 | let variants: [(u32, &str, &str); 16] = [ |
15938 | (Self::CLEAR.0, "CLEAR" , "Clear" ), |
15939 | (Self::AND.0, "AND" , "And" ), |
15940 | (Self::AND_REVERSE.0, "AND_REVERSE" , "AndReverse" ), |
15941 | (Self::COPY.0, "COPY" , "Copy" ), |
15942 | (Self::AND_INVERTED.0, "AND_INVERTED" , "AndInverted" ), |
15943 | (Self::NOOP.0, "NOOP" , "Noop" ), |
15944 | (Self::XOR.0, "XOR" , "Xor" ), |
15945 | (Self::OR.0, "OR" , "Or" ), |
15946 | (Self::NOR.0, "NOR" , "Nor" ), |
15947 | (Self::EQUIV.0, "EQUIV" , "Equiv" ), |
15948 | (Self::INVERT.0, "INVERT" , "Invert" ), |
15949 | (Self::OR_REVERSE.0, "OR_REVERSE" , "OrReverse" ), |
15950 | (Self::COPY_INVERTED.0, "COPY_INVERTED" , "CopyInverted" ), |
15951 | (Self::OR_INVERTED.0, "OR_INVERTED" , "OrInverted" ), |
15952 | (Self::NAND.0, "NAND" , "Nand" ), |
15953 | (Self::SET.0, "SET" , "Set" ), |
15954 | ]; |
15955 | pretty_print_enum(fmt, self.0, &variants) |
15956 | } |
15957 | } |
15958 | |
15959 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
15960 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
15961 | pub struct LineStyle(u32); |
15962 | impl LineStyle { |
15963 | pub const SOLID: Self = Self(0); |
15964 | pub const ON_OFF_DASH: Self = Self(1); |
15965 | pub const DOUBLE_DASH: Self = Self(2); |
15966 | } |
15967 | impl From<LineStyle> for u32 { |
15968 | #[inline ] |
15969 | fn from(input: LineStyle) -> Self { |
15970 | input.0 |
15971 | } |
15972 | } |
15973 | impl From<LineStyle> for Option<u32> { |
15974 | #[inline ] |
15975 | fn from(input: LineStyle) -> Self { |
15976 | Some(input.0) |
15977 | } |
15978 | } |
15979 | impl From<u8> for LineStyle { |
15980 | #[inline ] |
15981 | fn from(value: u8) -> Self { |
15982 | Self(value.into()) |
15983 | } |
15984 | } |
15985 | impl From<u16> for LineStyle { |
15986 | #[inline ] |
15987 | fn from(value: u16) -> Self { |
15988 | Self(value.into()) |
15989 | } |
15990 | } |
15991 | impl From<u32> for LineStyle { |
15992 | #[inline ] |
15993 | fn from(value: u32) -> Self { |
15994 | Self(value) |
15995 | } |
15996 | } |
15997 | impl core::fmt::Debug for LineStyle { |
15998 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
15999 | let variants: [(u32, &str, &str); 3] = [ |
16000 | (Self::SOLID.0, "SOLID" , "Solid" ), |
16001 | (Self::ON_OFF_DASH.0, "ON_OFF_DASH" , "OnOffDash" ), |
16002 | (Self::DOUBLE_DASH.0, "DOUBLE_DASH" , "DoubleDash" ), |
16003 | ]; |
16004 | pretty_print_enum(fmt, self.0, &variants) |
16005 | } |
16006 | } |
16007 | |
16008 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
16009 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
16010 | pub struct CapStyle(u32); |
16011 | impl CapStyle { |
16012 | pub const NOT_LAST: Self = Self(0); |
16013 | pub const BUTT: Self = Self(1); |
16014 | pub const ROUND: Self = Self(2); |
16015 | pub const PROJECTING: Self = Self(3); |
16016 | } |
16017 | impl From<CapStyle> for u32 { |
16018 | #[inline ] |
16019 | fn from(input: CapStyle) -> Self { |
16020 | input.0 |
16021 | } |
16022 | } |
16023 | impl From<CapStyle> for Option<u32> { |
16024 | #[inline ] |
16025 | fn from(input: CapStyle) -> Self { |
16026 | Some(input.0) |
16027 | } |
16028 | } |
16029 | impl From<u8> for CapStyle { |
16030 | #[inline ] |
16031 | fn from(value: u8) -> Self { |
16032 | Self(value.into()) |
16033 | } |
16034 | } |
16035 | impl From<u16> for CapStyle { |
16036 | #[inline ] |
16037 | fn from(value: u16) -> Self { |
16038 | Self(value.into()) |
16039 | } |
16040 | } |
16041 | impl From<u32> for CapStyle { |
16042 | #[inline ] |
16043 | fn from(value: u32) -> Self { |
16044 | Self(value) |
16045 | } |
16046 | } |
16047 | impl core::fmt::Debug for CapStyle { |
16048 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
16049 | let variants: [(u32, &str, &str); 4] = [ |
16050 | (Self::NOT_LAST.0, "NOT_LAST" , "NotLast" ), |
16051 | (Self::BUTT.0, "BUTT" , "Butt" ), |
16052 | (Self::ROUND.0, "ROUND" , "Round" ), |
16053 | (Self::PROJECTING.0, "PROJECTING" , "Projecting" ), |
16054 | ]; |
16055 | pretty_print_enum(fmt, self.0, &variants) |
16056 | } |
16057 | } |
16058 | |
16059 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
16060 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
16061 | pub struct JoinStyle(u32); |
16062 | impl JoinStyle { |
16063 | pub const MITER: Self = Self(0); |
16064 | pub const ROUND: Self = Self(1); |
16065 | pub const BEVEL: Self = Self(2); |
16066 | } |
16067 | impl From<JoinStyle> for u32 { |
16068 | #[inline ] |
16069 | fn from(input: JoinStyle) -> Self { |
16070 | input.0 |
16071 | } |
16072 | } |
16073 | impl From<JoinStyle> for Option<u32> { |
16074 | #[inline ] |
16075 | fn from(input: JoinStyle) -> Self { |
16076 | Some(input.0) |
16077 | } |
16078 | } |
16079 | impl From<u8> for JoinStyle { |
16080 | #[inline ] |
16081 | fn from(value: u8) -> Self { |
16082 | Self(value.into()) |
16083 | } |
16084 | } |
16085 | impl From<u16> for JoinStyle { |
16086 | #[inline ] |
16087 | fn from(value: u16) -> Self { |
16088 | Self(value.into()) |
16089 | } |
16090 | } |
16091 | impl From<u32> for JoinStyle { |
16092 | #[inline ] |
16093 | fn from(value: u32) -> Self { |
16094 | Self(value) |
16095 | } |
16096 | } |
16097 | impl core::fmt::Debug for JoinStyle { |
16098 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
16099 | let variants: [(u32, &str, &str); 3] = [ |
16100 | (Self::MITER.0, "MITER" , "Miter" ), |
16101 | (Self::ROUND.0, "ROUND" , "Round" ), |
16102 | (Self::BEVEL.0, "BEVEL" , "Bevel" ), |
16103 | ]; |
16104 | pretty_print_enum(fmt, self.0, &variants) |
16105 | } |
16106 | } |
16107 | |
16108 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
16109 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
16110 | pub struct FillStyle(u32); |
16111 | impl FillStyle { |
16112 | pub const SOLID: Self = Self(0); |
16113 | pub const TILED: Self = Self(1); |
16114 | pub const STIPPLED: Self = Self(2); |
16115 | pub const OPAQUE_STIPPLED: Self = Self(3); |
16116 | } |
16117 | impl From<FillStyle> for u32 { |
16118 | #[inline ] |
16119 | fn from(input: FillStyle) -> Self { |
16120 | input.0 |
16121 | } |
16122 | } |
16123 | impl From<FillStyle> for Option<u32> { |
16124 | #[inline ] |
16125 | fn from(input: FillStyle) -> Self { |
16126 | Some(input.0) |
16127 | } |
16128 | } |
16129 | impl From<u8> for FillStyle { |
16130 | #[inline ] |
16131 | fn from(value: u8) -> Self { |
16132 | Self(value.into()) |
16133 | } |
16134 | } |
16135 | impl From<u16> for FillStyle { |
16136 | #[inline ] |
16137 | fn from(value: u16) -> Self { |
16138 | Self(value.into()) |
16139 | } |
16140 | } |
16141 | impl From<u32> for FillStyle { |
16142 | #[inline ] |
16143 | fn from(value: u32) -> Self { |
16144 | Self(value) |
16145 | } |
16146 | } |
16147 | impl core::fmt::Debug for FillStyle { |
16148 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
16149 | let variants: [(u32, &str, &str); 4] = [ |
16150 | (Self::SOLID.0, "SOLID" , "Solid" ), |
16151 | (Self::TILED.0, "TILED" , "Tiled" ), |
16152 | (Self::STIPPLED.0, "STIPPLED" , "Stippled" ), |
16153 | (Self::OPAQUE_STIPPLED.0, "OPAQUE_STIPPLED" , "OpaqueStippled" ), |
16154 | ]; |
16155 | pretty_print_enum(fmt, self.0, &variants) |
16156 | } |
16157 | } |
16158 | |
16159 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
16160 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
16161 | pub struct FillRule(u32); |
16162 | impl FillRule { |
16163 | pub const EVEN_ODD: Self = Self(0); |
16164 | pub const WINDING: Self = Self(1); |
16165 | } |
16166 | impl From<FillRule> for u32 { |
16167 | #[inline ] |
16168 | fn from(input: FillRule) -> Self { |
16169 | input.0 |
16170 | } |
16171 | } |
16172 | impl From<FillRule> for Option<u32> { |
16173 | #[inline ] |
16174 | fn from(input: FillRule) -> Self { |
16175 | Some(input.0) |
16176 | } |
16177 | } |
16178 | impl From<u8> for FillRule { |
16179 | #[inline ] |
16180 | fn from(value: u8) -> Self { |
16181 | Self(value.into()) |
16182 | } |
16183 | } |
16184 | impl From<u16> for FillRule { |
16185 | #[inline ] |
16186 | fn from(value: u16) -> Self { |
16187 | Self(value.into()) |
16188 | } |
16189 | } |
16190 | impl From<u32> for FillRule { |
16191 | #[inline ] |
16192 | fn from(value: u32) -> Self { |
16193 | Self(value) |
16194 | } |
16195 | } |
16196 | impl core::fmt::Debug for FillRule { |
16197 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
16198 | let variants: [(u32, &str, &str); 2] = [ |
16199 | (Self::EVEN_ODD.0, "EVEN_ODD" , "EvenOdd" ), |
16200 | (Self::WINDING.0, "WINDING" , "Winding" ), |
16201 | ]; |
16202 | pretty_print_enum(fmt, self.0, &variants) |
16203 | } |
16204 | } |
16205 | |
16206 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
16207 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
16208 | pub struct SubwindowMode(u32); |
16209 | impl SubwindowMode { |
16210 | pub const CLIP_BY_CHILDREN: Self = Self(0); |
16211 | pub const INCLUDE_INFERIORS: Self = Self(1); |
16212 | } |
16213 | impl From<SubwindowMode> for u32 { |
16214 | #[inline ] |
16215 | fn from(input: SubwindowMode) -> Self { |
16216 | input.0 |
16217 | } |
16218 | } |
16219 | impl From<SubwindowMode> for Option<u32> { |
16220 | #[inline ] |
16221 | fn from(input: SubwindowMode) -> Self { |
16222 | Some(input.0) |
16223 | } |
16224 | } |
16225 | impl From<u8> for SubwindowMode { |
16226 | #[inline ] |
16227 | fn from(value: u8) -> Self { |
16228 | Self(value.into()) |
16229 | } |
16230 | } |
16231 | impl From<u16> for SubwindowMode { |
16232 | #[inline ] |
16233 | fn from(value: u16) -> Self { |
16234 | Self(value.into()) |
16235 | } |
16236 | } |
16237 | impl From<u32> for SubwindowMode { |
16238 | #[inline ] |
16239 | fn from(value: u32) -> Self { |
16240 | Self(value) |
16241 | } |
16242 | } |
16243 | impl core::fmt::Debug for SubwindowMode { |
16244 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
16245 | let variants: [(u32, &str, &str); 2] = [ |
16246 | (Self::CLIP_BY_CHILDREN.0, "CLIP_BY_CHILDREN" , "ClipByChildren" ), |
16247 | (Self::INCLUDE_INFERIORS.0, "INCLUDE_INFERIORS" , "IncludeInferiors" ), |
16248 | ]; |
16249 | pretty_print_enum(fmt, self.0, &variants) |
16250 | } |
16251 | } |
16252 | |
16253 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
16254 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
16255 | pub struct ArcMode(u32); |
16256 | impl ArcMode { |
16257 | pub const CHORD: Self = Self(0); |
16258 | pub const PIE_SLICE: Self = Self(1); |
16259 | } |
16260 | impl From<ArcMode> for u32 { |
16261 | #[inline ] |
16262 | fn from(input: ArcMode) -> Self { |
16263 | input.0 |
16264 | } |
16265 | } |
16266 | impl From<ArcMode> for Option<u32> { |
16267 | #[inline ] |
16268 | fn from(input: ArcMode) -> Self { |
16269 | Some(input.0) |
16270 | } |
16271 | } |
16272 | impl From<u8> for ArcMode { |
16273 | #[inline ] |
16274 | fn from(value: u8) -> Self { |
16275 | Self(value.into()) |
16276 | } |
16277 | } |
16278 | impl From<u16> for ArcMode { |
16279 | #[inline ] |
16280 | fn from(value: u16) -> Self { |
16281 | Self(value.into()) |
16282 | } |
16283 | } |
16284 | impl From<u32> for ArcMode { |
16285 | #[inline ] |
16286 | fn from(value: u32) -> Self { |
16287 | Self(value) |
16288 | } |
16289 | } |
16290 | impl core::fmt::Debug for ArcMode { |
16291 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
16292 | let variants: [(u32, &str, &str); 2] = [ |
16293 | (Self::CHORD.0, "CHORD" , "Chord" ), |
16294 | (Self::PIE_SLICE.0, "PIE_SLICE" , "PieSlice" ), |
16295 | ]; |
16296 | pretty_print_enum(fmt, self.0, &variants) |
16297 | } |
16298 | } |
16299 | |
16300 | /// Auxiliary and optional information for the `create_gc` function |
16301 | #[derive (Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] |
16302 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
16303 | pub struct CreateGCAux { |
16304 | pub function: Option<GX>, |
16305 | pub plane_mask: Option<u32>, |
16306 | pub foreground: Option<u32>, |
16307 | pub background: Option<u32>, |
16308 | pub line_width: Option<u32>, |
16309 | pub line_style: Option<LineStyle>, |
16310 | pub cap_style: Option<CapStyle>, |
16311 | pub join_style: Option<JoinStyle>, |
16312 | pub fill_style: Option<FillStyle>, |
16313 | pub fill_rule: Option<FillRule>, |
16314 | pub tile: Option<Pixmap>, |
16315 | pub stipple: Option<Pixmap>, |
16316 | pub tile_stipple_x_origin: Option<i32>, |
16317 | pub tile_stipple_y_origin: Option<i32>, |
16318 | pub font: Option<Font>, |
16319 | pub subwindow_mode: Option<SubwindowMode>, |
16320 | pub graphics_exposures: Option<Bool32>, |
16321 | pub clip_x_origin: Option<i32>, |
16322 | pub clip_y_origin: Option<i32>, |
16323 | pub clip_mask: Option<Pixmap>, |
16324 | pub dash_offset: Option<u32>, |
16325 | pub dashes: Option<u32>, |
16326 | pub arc_mode: Option<ArcMode>, |
16327 | } |
16328 | impl CreateGCAux { |
16329 | fn try_parse(value: &[u8], value_mask: u32) -> Result<(Self, &[u8]), ParseError> { |
16330 | let switch_expr = u32::from(value_mask); |
16331 | let mut outer_remaining = value; |
16332 | let function = if switch_expr & u32::from(GC::FUNCTION) != 0 { |
16333 | let remaining = outer_remaining; |
16334 | let (function, remaining) = u32::try_parse(remaining)?; |
16335 | let function = function.into(); |
16336 | outer_remaining = remaining; |
16337 | Some(function) |
16338 | } else { |
16339 | None |
16340 | }; |
16341 | let plane_mask = if switch_expr & u32::from(GC::PLANE_MASK) != 0 { |
16342 | let remaining = outer_remaining; |
16343 | let (plane_mask, remaining) = u32::try_parse(remaining)?; |
16344 | outer_remaining = remaining; |
16345 | Some(plane_mask) |
16346 | } else { |
16347 | None |
16348 | }; |
16349 | let foreground = if switch_expr & u32::from(GC::FOREGROUND) != 0 { |
16350 | let remaining = outer_remaining; |
16351 | let (foreground, remaining) = u32::try_parse(remaining)?; |
16352 | outer_remaining = remaining; |
16353 | Some(foreground) |
16354 | } else { |
16355 | None |
16356 | }; |
16357 | let background = if switch_expr & u32::from(GC::BACKGROUND) != 0 { |
16358 | let remaining = outer_remaining; |
16359 | let (background, remaining) = u32::try_parse(remaining)?; |
16360 | outer_remaining = remaining; |
16361 | Some(background) |
16362 | } else { |
16363 | None |
16364 | }; |
16365 | let line_width = if switch_expr & u32::from(GC::LINE_WIDTH) != 0 { |
16366 | let remaining = outer_remaining; |
16367 | let (line_width, remaining) = u32::try_parse(remaining)?; |
16368 | outer_remaining = remaining; |
16369 | Some(line_width) |
16370 | } else { |
16371 | None |
16372 | }; |
16373 | let line_style = if switch_expr & u32::from(GC::LINE_STYLE) != 0 { |
16374 | let remaining = outer_remaining; |
16375 | let (line_style, remaining) = u32::try_parse(remaining)?; |
16376 | let line_style = line_style.into(); |
16377 | outer_remaining = remaining; |
16378 | Some(line_style) |
16379 | } else { |
16380 | None |
16381 | }; |
16382 | let cap_style = if switch_expr & u32::from(GC::CAP_STYLE) != 0 { |
16383 | let remaining = outer_remaining; |
16384 | let (cap_style, remaining) = u32::try_parse(remaining)?; |
16385 | let cap_style = cap_style.into(); |
16386 | outer_remaining = remaining; |
16387 | Some(cap_style) |
16388 | } else { |
16389 | None |
16390 | }; |
16391 | let join_style = if switch_expr & u32::from(GC::JOIN_STYLE) != 0 { |
16392 | let remaining = outer_remaining; |
16393 | let (join_style, remaining) = u32::try_parse(remaining)?; |
16394 | let join_style = join_style.into(); |
16395 | outer_remaining = remaining; |
16396 | Some(join_style) |
16397 | } else { |
16398 | None |
16399 | }; |
16400 | let fill_style = if switch_expr & u32::from(GC::FILL_STYLE) != 0 { |
16401 | let remaining = outer_remaining; |
16402 | let (fill_style, remaining) = u32::try_parse(remaining)?; |
16403 | let fill_style = fill_style.into(); |
16404 | outer_remaining = remaining; |
16405 | Some(fill_style) |
16406 | } else { |
16407 | None |
16408 | }; |
16409 | let fill_rule = if switch_expr & u32::from(GC::FILL_RULE) != 0 { |
16410 | let remaining = outer_remaining; |
16411 | let (fill_rule, remaining) = u32::try_parse(remaining)?; |
16412 | let fill_rule = fill_rule.into(); |
16413 | outer_remaining = remaining; |
16414 | Some(fill_rule) |
16415 | } else { |
16416 | None |
16417 | }; |
16418 | let tile = if switch_expr & u32::from(GC::TILE) != 0 { |
16419 | let remaining = outer_remaining; |
16420 | let (tile, remaining) = Pixmap::try_parse(remaining)?; |
16421 | outer_remaining = remaining; |
16422 | Some(tile) |
16423 | } else { |
16424 | None |
16425 | }; |
16426 | let stipple = if switch_expr & u32::from(GC::STIPPLE) != 0 { |
16427 | let remaining = outer_remaining; |
16428 | let (stipple, remaining) = Pixmap::try_parse(remaining)?; |
16429 | outer_remaining = remaining; |
16430 | Some(stipple) |
16431 | } else { |
16432 | None |
16433 | }; |
16434 | let tile_stipple_x_origin = if switch_expr & u32::from(GC::TILE_STIPPLE_ORIGIN_X) != 0 { |
16435 | let remaining = outer_remaining; |
16436 | let (tile_stipple_x_origin, remaining) = i32::try_parse(remaining)?; |
16437 | outer_remaining = remaining; |
16438 | Some(tile_stipple_x_origin) |
16439 | } else { |
16440 | None |
16441 | }; |
16442 | let tile_stipple_y_origin = if switch_expr & u32::from(GC::TILE_STIPPLE_ORIGIN_Y) != 0 { |
16443 | let remaining = outer_remaining; |
16444 | let (tile_stipple_y_origin, remaining) = i32::try_parse(remaining)?; |
16445 | outer_remaining = remaining; |
16446 | Some(tile_stipple_y_origin) |
16447 | } else { |
16448 | None |
16449 | }; |
16450 | let font = if switch_expr & u32::from(GC::FONT) != 0 { |
16451 | let remaining = outer_remaining; |
16452 | let (font, remaining) = Font::try_parse(remaining)?; |
16453 | outer_remaining = remaining; |
16454 | Some(font) |
16455 | } else { |
16456 | None |
16457 | }; |
16458 | let subwindow_mode = if switch_expr & u32::from(GC::SUBWINDOW_MODE) != 0 { |
16459 | let remaining = outer_remaining; |
16460 | let (subwindow_mode, remaining) = u32::try_parse(remaining)?; |
16461 | let subwindow_mode = subwindow_mode.into(); |
16462 | outer_remaining = remaining; |
16463 | Some(subwindow_mode) |
16464 | } else { |
16465 | None |
16466 | }; |
16467 | let graphics_exposures = if switch_expr & u32::from(GC::GRAPHICS_EXPOSURES) != 0 { |
16468 | let remaining = outer_remaining; |
16469 | let (graphics_exposures, remaining) = Bool32::try_parse(remaining)?; |
16470 | outer_remaining = remaining; |
16471 | Some(graphics_exposures) |
16472 | } else { |
16473 | None |
16474 | }; |
16475 | let clip_x_origin = if switch_expr & u32::from(GC::CLIP_ORIGIN_X) != 0 { |
16476 | let remaining = outer_remaining; |
16477 | let (clip_x_origin, remaining) = i32::try_parse(remaining)?; |
16478 | outer_remaining = remaining; |
16479 | Some(clip_x_origin) |
16480 | } else { |
16481 | None |
16482 | }; |
16483 | let clip_y_origin = if switch_expr & u32::from(GC::CLIP_ORIGIN_Y) != 0 { |
16484 | let remaining = outer_remaining; |
16485 | let (clip_y_origin, remaining) = i32::try_parse(remaining)?; |
16486 | outer_remaining = remaining; |
16487 | Some(clip_y_origin) |
16488 | } else { |
16489 | None |
16490 | }; |
16491 | let clip_mask = if switch_expr & u32::from(GC::CLIP_MASK) != 0 { |
16492 | let remaining = outer_remaining; |
16493 | let (clip_mask, remaining) = Pixmap::try_parse(remaining)?; |
16494 | outer_remaining = remaining; |
16495 | Some(clip_mask) |
16496 | } else { |
16497 | None |
16498 | }; |
16499 | let dash_offset = if switch_expr & u32::from(GC::DASH_OFFSET) != 0 { |
16500 | let remaining = outer_remaining; |
16501 | let (dash_offset, remaining) = u32::try_parse(remaining)?; |
16502 | outer_remaining = remaining; |
16503 | Some(dash_offset) |
16504 | } else { |
16505 | None |
16506 | }; |
16507 | let dashes = if switch_expr & u32::from(GC::DASH_LIST) != 0 { |
16508 | let remaining = outer_remaining; |
16509 | let (dashes, remaining) = u32::try_parse(remaining)?; |
16510 | outer_remaining = remaining; |
16511 | Some(dashes) |
16512 | } else { |
16513 | None |
16514 | }; |
16515 | let arc_mode = if switch_expr & u32::from(GC::ARC_MODE) != 0 { |
16516 | let remaining = outer_remaining; |
16517 | let (arc_mode, remaining) = u32::try_parse(remaining)?; |
16518 | let arc_mode = arc_mode.into(); |
16519 | outer_remaining = remaining; |
16520 | Some(arc_mode) |
16521 | } else { |
16522 | None |
16523 | }; |
16524 | let result = CreateGCAux { function, plane_mask, foreground, background, line_width, line_style, cap_style, join_style, fill_style, fill_rule, tile, stipple, tile_stipple_x_origin, tile_stipple_y_origin, font, subwindow_mode, graphics_exposures, clip_x_origin, clip_y_origin, clip_mask, dash_offset, dashes, arc_mode }; |
16525 | Ok((result, outer_remaining)) |
16526 | } |
16527 | } |
16528 | impl CreateGCAux { |
16529 | #[allow (dead_code)] |
16530 | fn serialize(&self, value_mask: u32) -> Vec<u8> { |
16531 | let mut result = Vec::new(); |
16532 | self.serialize_into(&mut result, u32::from(value_mask)); |
16533 | result |
16534 | } |
16535 | fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) { |
16536 | assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant" ); |
16537 | if let Some(function) = self.function { |
16538 | u32::from(function).serialize_into(bytes); |
16539 | } |
16540 | if let Some(plane_mask) = self.plane_mask { |
16541 | plane_mask.serialize_into(bytes); |
16542 | } |
16543 | if let Some(foreground) = self.foreground { |
16544 | foreground.serialize_into(bytes); |
16545 | } |
16546 | if let Some(background) = self.background { |
16547 | background.serialize_into(bytes); |
16548 | } |
16549 | if let Some(line_width) = self.line_width { |
16550 | line_width.serialize_into(bytes); |
16551 | } |
16552 | if let Some(line_style) = self.line_style { |
16553 | u32::from(line_style).serialize_into(bytes); |
16554 | } |
16555 | if let Some(cap_style) = self.cap_style { |
16556 | u32::from(cap_style).serialize_into(bytes); |
16557 | } |
16558 | if let Some(join_style) = self.join_style { |
16559 | u32::from(join_style).serialize_into(bytes); |
16560 | } |
16561 | if let Some(fill_style) = self.fill_style { |
16562 | u32::from(fill_style).serialize_into(bytes); |
16563 | } |
16564 | if let Some(fill_rule) = self.fill_rule { |
16565 | u32::from(fill_rule).serialize_into(bytes); |
16566 | } |
16567 | if let Some(tile) = self.tile { |
16568 | tile.serialize_into(bytes); |
16569 | } |
16570 | if let Some(stipple) = self.stipple { |
16571 | stipple.serialize_into(bytes); |
16572 | } |
16573 | if let Some(tile_stipple_x_origin) = self.tile_stipple_x_origin { |
16574 | tile_stipple_x_origin.serialize_into(bytes); |
16575 | } |
16576 | if let Some(tile_stipple_y_origin) = self.tile_stipple_y_origin { |
16577 | tile_stipple_y_origin.serialize_into(bytes); |
16578 | } |
16579 | if let Some(font) = self.font { |
16580 | font.serialize_into(bytes); |
16581 | } |
16582 | if let Some(subwindow_mode) = self.subwindow_mode { |
16583 | u32::from(subwindow_mode).serialize_into(bytes); |
16584 | } |
16585 | if let Some(graphics_exposures) = self.graphics_exposures { |
16586 | graphics_exposures.serialize_into(bytes); |
16587 | } |
16588 | if let Some(clip_x_origin) = self.clip_x_origin { |
16589 | clip_x_origin.serialize_into(bytes); |
16590 | } |
16591 | if let Some(clip_y_origin) = self.clip_y_origin { |
16592 | clip_y_origin.serialize_into(bytes); |
16593 | } |
16594 | if let Some(clip_mask) = self.clip_mask { |
16595 | clip_mask.serialize_into(bytes); |
16596 | } |
16597 | if let Some(dash_offset) = self.dash_offset { |
16598 | dash_offset.serialize_into(bytes); |
16599 | } |
16600 | if let Some(dashes) = self.dashes { |
16601 | dashes.serialize_into(bytes); |
16602 | } |
16603 | if let Some(arc_mode) = self.arc_mode { |
16604 | u32::from(arc_mode).serialize_into(bytes); |
16605 | } |
16606 | } |
16607 | } |
16608 | impl CreateGCAux { |
16609 | fn switch_expr(&self) -> u32 { |
16610 | let mut expr_value = 0; |
16611 | if self.function.is_some() { |
16612 | expr_value |= u32::from(GC::FUNCTION); |
16613 | } |
16614 | if self.plane_mask.is_some() { |
16615 | expr_value |= u32::from(GC::PLANE_MASK); |
16616 | } |
16617 | if self.foreground.is_some() { |
16618 | expr_value |= u32::from(GC::FOREGROUND); |
16619 | } |
16620 | if self.background.is_some() { |
16621 | expr_value |= u32::from(GC::BACKGROUND); |
16622 | } |
16623 | if self.line_width.is_some() { |
16624 | expr_value |= u32::from(GC::LINE_WIDTH); |
16625 | } |
16626 | if self.line_style.is_some() { |
16627 | expr_value |= u32::from(GC::LINE_STYLE); |
16628 | } |
16629 | if self.cap_style.is_some() { |
16630 | expr_value |= u32::from(GC::CAP_STYLE); |
16631 | } |
16632 | if self.join_style.is_some() { |
16633 | expr_value |= u32::from(GC::JOIN_STYLE); |
16634 | } |
16635 | if self.fill_style.is_some() { |
16636 | expr_value |= u32::from(GC::FILL_STYLE); |
16637 | } |
16638 | if self.fill_rule.is_some() { |
16639 | expr_value |= u32::from(GC::FILL_RULE); |
16640 | } |
16641 | if self.tile.is_some() { |
16642 | expr_value |= u32::from(GC::TILE); |
16643 | } |
16644 | if self.stipple.is_some() { |
16645 | expr_value |= u32::from(GC::STIPPLE); |
16646 | } |
16647 | if self.tile_stipple_x_origin.is_some() { |
16648 | expr_value |= u32::from(GC::TILE_STIPPLE_ORIGIN_X); |
16649 | } |
16650 | if self.tile_stipple_y_origin.is_some() { |
16651 | expr_value |= u32::from(GC::TILE_STIPPLE_ORIGIN_Y); |
16652 | } |
16653 | if self.font.is_some() { |
16654 | expr_value |= u32::from(GC::FONT); |
16655 | } |
16656 | if self.subwindow_mode.is_some() { |
16657 | expr_value |= u32::from(GC::SUBWINDOW_MODE); |
16658 | } |
16659 | if self.graphics_exposures.is_some() { |
16660 | expr_value |= u32::from(GC::GRAPHICS_EXPOSURES); |
16661 | } |
16662 | if self.clip_x_origin.is_some() { |
16663 | expr_value |= u32::from(GC::CLIP_ORIGIN_X); |
16664 | } |
16665 | if self.clip_y_origin.is_some() { |
16666 | expr_value |= u32::from(GC::CLIP_ORIGIN_Y); |
16667 | } |
16668 | if self.clip_mask.is_some() { |
16669 | expr_value |= u32::from(GC::CLIP_MASK); |
16670 | } |
16671 | if self.dash_offset.is_some() { |
16672 | expr_value |= u32::from(GC::DASH_OFFSET); |
16673 | } |
16674 | if self.dashes.is_some() { |
16675 | expr_value |= u32::from(GC::DASH_LIST); |
16676 | } |
16677 | if self.arc_mode.is_some() { |
16678 | expr_value |= u32::from(GC::ARC_MODE); |
16679 | } |
16680 | expr_value |
16681 | } |
16682 | } |
16683 | impl CreateGCAux { |
16684 | /// Create a new instance with all fields unset / not present. |
16685 | pub fn new() -> Self { |
16686 | Default::default() |
16687 | } |
16688 | /// Set the `function` field of this structure. |
16689 | #[must_use ] |
16690 | pub fn function<I>(mut self, value: I) -> Self where I: Into<Option<GX>> { |
16691 | self.function = value.into(); |
16692 | self |
16693 | } |
16694 | /// Set the `plane_mask` field of this structure. |
16695 | #[must_use ] |
16696 | pub fn plane_mask<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
16697 | self.plane_mask = value.into(); |
16698 | self |
16699 | } |
16700 | /// Set the `foreground` field of this structure. |
16701 | #[must_use ] |
16702 | pub fn foreground<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
16703 | self.foreground = value.into(); |
16704 | self |
16705 | } |
16706 | /// Set the `background` field of this structure. |
16707 | #[must_use ] |
16708 | pub fn background<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
16709 | self.background = value.into(); |
16710 | self |
16711 | } |
16712 | /// Set the `line_width` field of this structure. |
16713 | #[must_use ] |
16714 | pub fn line_width<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
16715 | self.line_width = value.into(); |
16716 | self |
16717 | } |
16718 | /// Set the `line_style` field of this structure. |
16719 | #[must_use ] |
16720 | pub fn line_style<I>(mut self, value: I) -> Self where I: Into<Option<LineStyle>> { |
16721 | self.line_style = value.into(); |
16722 | self |
16723 | } |
16724 | /// Set the `cap_style` field of this structure. |
16725 | #[must_use ] |
16726 | pub fn cap_style<I>(mut self, value: I) -> Self where I: Into<Option<CapStyle>> { |
16727 | self.cap_style = value.into(); |
16728 | self |
16729 | } |
16730 | /// Set the `join_style` field of this structure. |
16731 | #[must_use ] |
16732 | pub fn join_style<I>(mut self, value: I) -> Self where I: Into<Option<JoinStyle>> { |
16733 | self.join_style = value.into(); |
16734 | self |
16735 | } |
16736 | /// Set the `fill_style` field of this structure. |
16737 | #[must_use ] |
16738 | pub fn fill_style<I>(mut self, value: I) -> Self where I: Into<Option<FillStyle>> { |
16739 | self.fill_style = value.into(); |
16740 | self |
16741 | } |
16742 | /// Set the `fill_rule` field of this structure. |
16743 | #[must_use ] |
16744 | pub fn fill_rule<I>(mut self, value: I) -> Self where I: Into<Option<FillRule>> { |
16745 | self.fill_rule = value.into(); |
16746 | self |
16747 | } |
16748 | /// Set the `tile` field of this structure. |
16749 | #[must_use ] |
16750 | pub fn tile<I>(mut self, value: I) -> Self where I: Into<Option<Pixmap>> { |
16751 | self.tile = value.into(); |
16752 | self |
16753 | } |
16754 | /// Set the `stipple` field of this structure. |
16755 | #[must_use ] |
16756 | pub fn stipple<I>(mut self, value: I) -> Self where I: Into<Option<Pixmap>> { |
16757 | self.stipple = value.into(); |
16758 | self |
16759 | } |
16760 | /// Set the `tile_stipple_x_origin` field of this structure. |
16761 | #[must_use ] |
16762 | pub fn tile_stipple_x_origin<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
16763 | self.tile_stipple_x_origin = value.into(); |
16764 | self |
16765 | } |
16766 | /// Set the `tile_stipple_y_origin` field of this structure. |
16767 | #[must_use ] |
16768 | pub fn tile_stipple_y_origin<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
16769 | self.tile_stipple_y_origin = value.into(); |
16770 | self |
16771 | } |
16772 | /// Set the `font` field of this structure. |
16773 | #[must_use ] |
16774 | pub fn font<I>(mut self, value: I) -> Self where I: Into<Option<Font>> { |
16775 | self.font = value.into(); |
16776 | self |
16777 | } |
16778 | /// Set the `subwindow_mode` field of this structure. |
16779 | #[must_use ] |
16780 | pub fn subwindow_mode<I>(mut self, value: I) -> Self where I: Into<Option<SubwindowMode>> { |
16781 | self.subwindow_mode = value.into(); |
16782 | self |
16783 | } |
16784 | /// Set the `graphics_exposures` field of this structure. |
16785 | #[must_use ] |
16786 | pub fn graphics_exposures<I>(mut self, value: I) -> Self where I: Into<Option<Bool32>> { |
16787 | self.graphics_exposures = value.into(); |
16788 | self |
16789 | } |
16790 | /// Set the `clip_x_origin` field of this structure. |
16791 | #[must_use ] |
16792 | pub fn clip_x_origin<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
16793 | self.clip_x_origin = value.into(); |
16794 | self |
16795 | } |
16796 | /// Set the `clip_y_origin` field of this structure. |
16797 | #[must_use ] |
16798 | pub fn clip_y_origin<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
16799 | self.clip_y_origin = value.into(); |
16800 | self |
16801 | } |
16802 | /// Set the `clip_mask` field of this structure. |
16803 | #[must_use ] |
16804 | pub fn clip_mask<I>(mut self, value: I) -> Self where I: Into<Option<Pixmap>> { |
16805 | self.clip_mask = value.into(); |
16806 | self |
16807 | } |
16808 | /// Set the `dash_offset` field of this structure. |
16809 | #[must_use ] |
16810 | pub fn dash_offset<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
16811 | self.dash_offset = value.into(); |
16812 | self |
16813 | } |
16814 | /// Set the `dashes` field of this structure. |
16815 | #[must_use ] |
16816 | pub fn dashes<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
16817 | self.dashes = value.into(); |
16818 | self |
16819 | } |
16820 | /// Set the `arc_mode` field of this structure. |
16821 | #[must_use ] |
16822 | pub fn arc_mode<I>(mut self, value: I) -> Self where I: Into<Option<ArcMode>> { |
16823 | self.arc_mode = value.into(); |
16824 | self |
16825 | } |
16826 | } |
16827 | |
16828 | /// Opcode for the CreateGC request |
16829 | pub const CREATE_GC_REQUEST: u8 = 55; |
16830 | /// Creates a graphics context. |
16831 | /// |
16832 | /// Creates a graphics context. The graphics context can be used with any drawable |
16833 | /// that has the same root and depth as the specified drawable. |
16834 | /// |
16835 | /// # Fields |
16836 | /// |
16837 | /// * `cid` - The ID with which you will refer to the graphics context, created by |
16838 | /// `xcb_generate_id`. |
16839 | /// * `drawable` - Drawable to get the root/depth from. |
16840 | /// |
16841 | /// # Errors |
16842 | /// |
16843 | /// * `Drawable` - The specified `drawable` (Window or Pixmap) does not exist. |
16844 | /// * `Match` - TODO: reasons? |
16845 | /// * `Font` - TODO: reasons? |
16846 | /// * `Pixmap` - TODO: reasons? |
16847 | /// * `Value` - TODO: reasons? |
16848 | /// * `Alloc` - The X server could not allocate the requested resources (no memory?). |
16849 | /// |
16850 | /// # See |
16851 | /// |
16852 | /// * `xcb_generate_id`: function |
16853 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
16854 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
16855 | pub struct CreateGCRequest<'input> { |
16856 | pub cid: Gcontext, |
16857 | pub drawable: Drawable, |
16858 | pub value_list: Cow<'input, CreateGCAux>, |
16859 | } |
16860 | impl<'input> CreateGCRequest<'input> { |
16861 | /// Serialize this request into bytes for the provided connection |
16862 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
16863 | let length_so_far = 0; |
16864 | let cid_bytes = self.cid.serialize(); |
16865 | let drawable_bytes = self.drawable.serialize(); |
16866 | let value_mask: u32 = self.value_list.switch_expr(); |
16867 | let value_mask_bytes = value_mask.serialize(); |
16868 | let mut request0 = vec![ |
16869 | CREATE_GC_REQUEST, |
16870 | 0, |
16871 | 0, |
16872 | 0, |
16873 | cid_bytes[0], |
16874 | cid_bytes[1], |
16875 | cid_bytes[2], |
16876 | cid_bytes[3], |
16877 | drawable_bytes[0], |
16878 | drawable_bytes[1], |
16879 | drawable_bytes[2], |
16880 | drawable_bytes[3], |
16881 | value_mask_bytes[0], |
16882 | value_mask_bytes[1], |
16883 | value_mask_bytes[2], |
16884 | value_mask_bytes[3], |
16885 | ]; |
16886 | let length_so_far = length_so_far + request0.len(); |
16887 | let value_list_bytes = self.value_list.serialize(u32::from(value_mask)); |
16888 | let length_so_far = length_so_far + value_list_bytes.len(); |
16889 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
16890 | let length_so_far = length_so_far + padding0.len(); |
16891 | assert_eq!(length_so_far % 4, 0); |
16892 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
16893 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
16894 | (vec![request0.into(), value_list_bytes.into(), padding0.into()], vec![]) |
16895 | } |
16896 | /// Parse this request given its header, its body, and any fds that go along with it |
16897 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
16898 | if header.major_opcode != CREATE_GC_REQUEST { |
16899 | return Err(ParseError::InvalidValue); |
16900 | } |
16901 | let remaining = &[header.minor_opcode]; |
16902 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
16903 | let _ = remaining; |
16904 | let (cid, remaining) = Gcontext::try_parse(value)?; |
16905 | let (drawable, remaining) = Drawable::try_parse(remaining)?; |
16906 | let (value_mask, remaining) = u32::try_parse(remaining)?; |
16907 | let (value_list, remaining) = CreateGCAux::try_parse(remaining, u32::from(value_mask))?; |
16908 | let _ = remaining; |
16909 | Ok(CreateGCRequest { |
16910 | cid, |
16911 | drawable, |
16912 | value_list: Cow::Owned(value_list), |
16913 | }) |
16914 | } |
16915 | /// Clone all borrowed data in this CreateGCRequest. |
16916 | pub fn into_owned(self) -> CreateGCRequest<'static> { |
16917 | CreateGCRequest { |
16918 | cid: self.cid, |
16919 | drawable: self.drawable, |
16920 | value_list: Cow::Owned(self.value_list.into_owned()), |
16921 | } |
16922 | } |
16923 | } |
16924 | impl<'input> Request for CreateGCRequest<'input> { |
16925 | const EXTENSION_NAME: Option<&'static str> = None; |
16926 | |
16927 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
16928 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
16929 | // Flatten the buffers into a single vector |
16930 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
16931 | (buf, fds) |
16932 | } |
16933 | } |
16934 | impl<'input> crate::x11_utils::VoidRequest for CreateGCRequest<'input> { |
16935 | } |
16936 | |
16937 | /// Auxiliary and optional information for the `change_gc` function |
16938 | #[derive (Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] |
16939 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
16940 | pub struct ChangeGCAux { |
16941 | pub function: Option<GX>, |
16942 | pub plane_mask: Option<u32>, |
16943 | pub foreground: Option<u32>, |
16944 | pub background: Option<u32>, |
16945 | pub line_width: Option<u32>, |
16946 | pub line_style: Option<LineStyle>, |
16947 | pub cap_style: Option<CapStyle>, |
16948 | pub join_style: Option<JoinStyle>, |
16949 | pub fill_style: Option<FillStyle>, |
16950 | pub fill_rule: Option<FillRule>, |
16951 | pub tile: Option<Pixmap>, |
16952 | pub stipple: Option<Pixmap>, |
16953 | pub tile_stipple_x_origin: Option<i32>, |
16954 | pub tile_stipple_y_origin: Option<i32>, |
16955 | pub font: Option<Font>, |
16956 | pub subwindow_mode: Option<SubwindowMode>, |
16957 | pub graphics_exposures: Option<Bool32>, |
16958 | pub clip_x_origin: Option<i32>, |
16959 | pub clip_y_origin: Option<i32>, |
16960 | pub clip_mask: Option<Pixmap>, |
16961 | pub dash_offset: Option<u32>, |
16962 | pub dashes: Option<u32>, |
16963 | pub arc_mode: Option<ArcMode>, |
16964 | } |
16965 | impl ChangeGCAux { |
16966 | fn try_parse(value: &[u8], value_mask: u32) -> Result<(Self, &[u8]), ParseError> { |
16967 | let switch_expr = u32::from(value_mask); |
16968 | let mut outer_remaining = value; |
16969 | let function = if switch_expr & u32::from(GC::FUNCTION) != 0 { |
16970 | let remaining = outer_remaining; |
16971 | let (function, remaining) = u32::try_parse(remaining)?; |
16972 | let function = function.into(); |
16973 | outer_remaining = remaining; |
16974 | Some(function) |
16975 | } else { |
16976 | None |
16977 | }; |
16978 | let plane_mask = if switch_expr & u32::from(GC::PLANE_MASK) != 0 { |
16979 | let remaining = outer_remaining; |
16980 | let (plane_mask, remaining) = u32::try_parse(remaining)?; |
16981 | outer_remaining = remaining; |
16982 | Some(plane_mask) |
16983 | } else { |
16984 | None |
16985 | }; |
16986 | let foreground = if switch_expr & u32::from(GC::FOREGROUND) != 0 { |
16987 | let remaining = outer_remaining; |
16988 | let (foreground, remaining) = u32::try_parse(remaining)?; |
16989 | outer_remaining = remaining; |
16990 | Some(foreground) |
16991 | } else { |
16992 | None |
16993 | }; |
16994 | let background = if switch_expr & u32::from(GC::BACKGROUND) != 0 { |
16995 | let remaining = outer_remaining; |
16996 | let (background, remaining) = u32::try_parse(remaining)?; |
16997 | outer_remaining = remaining; |
16998 | Some(background) |
16999 | } else { |
17000 | None |
17001 | }; |
17002 | let line_width = if switch_expr & u32::from(GC::LINE_WIDTH) != 0 { |
17003 | let remaining = outer_remaining; |
17004 | let (line_width, remaining) = u32::try_parse(remaining)?; |
17005 | outer_remaining = remaining; |
17006 | Some(line_width) |
17007 | } else { |
17008 | None |
17009 | }; |
17010 | let line_style = if switch_expr & u32::from(GC::LINE_STYLE) != 0 { |
17011 | let remaining = outer_remaining; |
17012 | let (line_style, remaining) = u32::try_parse(remaining)?; |
17013 | let line_style = line_style.into(); |
17014 | outer_remaining = remaining; |
17015 | Some(line_style) |
17016 | } else { |
17017 | None |
17018 | }; |
17019 | let cap_style = if switch_expr & u32::from(GC::CAP_STYLE) != 0 { |
17020 | let remaining = outer_remaining; |
17021 | let (cap_style, remaining) = u32::try_parse(remaining)?; |
17022 | let cap_style = cap_style.into(); |
17023 | outer_remaining = remaining; |
17024 | Some(cap_style) |
17025 | } else { |
17026 | None |
17027 | }; |
17028 | let join_style = if switch_expr & u32::from(GC::JOIN_STYLE) != 0 { |
17029 | let remaining = outer_remaining; |
17030 | let (join_style, remaining) = u32::try_parse(remaining)?; |
17031 | let join_style = join_style.into(); |
17032 | outer_remaining = remaining; |
17033 | Some(join_style) |
17034 | } else { |
17035 | None |
17036 | }; |
17037 | let fill_style = if switch_expr & u32::from(GC::FILL_STYLE) != 0 { |
17038 | let remaining = outer_remaining; |
17039 | let (fill_style, remaining) = u32::try_parse(remaining)?; |
17040 | let fill_style = fill_style.into(); |
17041 | outer_remaining = remaining; |
17042 | Some(fill_style) |
17043 | } else { |
17044 | None |
17045 | }; |
17046 | let fill_rule = if switch_expr & u32::from(GC::FILL_RULE) != 0 { |
17047 | let remaining = outer_remaining; |
17048 | let (fill_rule, remaining) = u32::try_parse(remaining)?; |
17049 | let fill_rule = fill_rule.into(); |
17050 | outer_remaining = remaining; |
17051 | Some(fill_rule) |
17052 | } else { |
17053 | None |
17054 | }; |
17055 | let tile = if switch_expr & u32::from(GC::TILE) != 0 { |
17056 | let remaining = outer_remaining; |
17057 | let (tile, remaining) = Pixmap::try_parse(remaining)?; |
17058 | outer_remaining = remaining; |
17059 | Some(tile) |
17060 | } else { |
17061 | None |
17062 | }; |
17063 | let stipple = if switch_expr & u32::from(GC::STIPPLE) != 0 { |
17064 | let remaining = outer_remaining; |
17065 | let (stipple, remaining) = Pixmap::try_parse(remaining)?; |
17066 | outer_remaining = remaining; |
17067 | Some(stipple) |
17068 | } else { |
17069 | None |
17070 | }; |
17071 | let tile_stipple_x_origin = if switch_expr & u32::from(GC::TILE_STIPPLE_ORIGIN_X) != 0 { |
17072 | let remaining = outer_remaining; |
17073 | let (tile_stipple_x_origin, remaining) = i32::try_parse(remaining)?; |
17074 | outer_remaining = remaining; |
17075 | Some(tile_stipple_x_origin) |
17076 | } else { |
17077 | None |
17078 | }; |
17079 | let tile_stipple_y_origin = if switch_expr & u32::from(GC::TILE_STIPPLE_ORIGIN_Y) != 0 { |
17080 | let remaining = outer_remaining; |
17081 | let (tile_stipple_y_origin, remaining) = i32::try_parse(remaining)?; |
17082 | outer_remaining = remaining; |
17083 | Some(tile_stipple_y_origin) |
17084 | } else { |
17085 | None |
17086 | }; |
17087 | let font = if switch_expr & u32::from(GC::FONT) != 0 { |
17088 | let remaining = outer_remaining; |
17089 | let (font, remaining) = Font::try_parse(remaining)?; |
17090 | outer_remaining = remaining; |
17091 | Some(font) |
17092 | } else { |
17093 | None |
17094 | }; |
17095 | let subwindow_mode = if switch_expr & u32::from(GC::SUBWINDOW_MODE) != 0 { |
17096 | let remaining = outer_remaining; |
17097 | let (subwindow_mode, remaining) = u32::try_parse(remaining)?; |
17098 | let subwindow_mode = subwindow_mode.into(); |
17099 | outer_remaining = remaining; |
17100 | Some(subwindow_mode) |
17101 | } else { |
17102 | None |
17103 | }; |
17104 | let graphics_exposures = if switch_expr & u32::from(GC::GRAPHICS_EXPOSURES) != 0 { |
17105 | let remaining = outer_remaining; |
17106 | let (graphics_exposures, remaining) = Bool32::try_parse(remaining)?; |
17107 | outer_remaining = remaining; |
17108 | Some(graphics_exposures) |
17109 | } else { |
17110 | None |
17111 | }; |
17112 | let clip_x_origin = if switch_expr & u32::from(GC::CLIP_ORIGIN_X) != 0 { |
17113 | let remaining = outer_remaining; |
17114 | let (clip_x_origin, remaining) = i32::try_parse(remaining)?; |
17115 | outer_remaining = remaining; |
17116 | Some(clip_x_origin) |
17117 | } else { |
17118 | None |
17119 | }; |
17120 | let clip_y_origin = if switch_expr & u32::from(GC::CLIP_ORIGIN_Y) != 0 { |
17121 | let remaining = outer_remaining; |
17122 | let (clip_y_origin, remaining) = i32::try_parse(remaining)?; |
17123 | outer_remaining = remaining; |
17124 | Some(clip_y_origin) |
17125 | } else { |
17126 | None |
17127 | }; |
17128 | let clip_mask = if switch_expr & u32::from(GC::CLIP_MASK) != 0 { |
17129 | let remaining = outer_remaining; |
17130 | let (clip_mask, remaining) = Pixmap::try_parse(remaining)?; |
17131 | outer_remaining = remaining; |
17132 | Some(clip_mask) |
17133 | } else { |
17134 | None |
17135 | }; |
17136 | let dash_offset = if switch_expr & u32::from(GC::DASH_OFFSET) != 0 { |
17137 | let remaining = outer_remaining; |
17138 | let (dash_offset, remaining) = u32::try_parse(remaining)?; |
17139 | outer_remaining = remaining; |
17140 | Some(dash_offset) |
17141 | } else { |
17142 | None |
17143 | }; |
17144 | let dashes = if switch_expr & u32::from(GC::DASH_LIST) != 0 { |
17145 | let remaining = outer_remaining; |
17146 | let (dashes, remaining) = u32::try_parse(remaining)?; |
17147 | outer_remaining = remaining; |
17148 | Some(dashes) |
17149 | } else { |
17150 | None |
17151 | }; |
17152 | let arc_mode = if switch_expr & u32::from(GC::ARC_MODE) != 0 { |
17153 | let remaining = outer_remaining; |
17154 | let (arc_mode, remaining) = u32::try_parse(remaining)?; |
17155 | let arc_mode = arc_mode.into(); |
17156 | outer_remaining = remaining; |
17157 | Some(arc_mode) |
17158 | } else { |
17159 | None |
17160 | }; |
17161 | let result = ChangeGCAux { function, plane_mask, foreground, background, line_width, line_style, cap_style, join_style, fill_style, fill_rule, tile, stipple, tile_stipple_x_origin, tile_stipple_y_origin, font, subwindow_mode, graphics_exposures, clip_x_origin, clip_y_origin, clip_mask, dash_offset, dashes, arc_mode }; |
17162 | Ok((result, outer_remaining)) |
17163 | } |
17164 | } |
17165 | impl ChangeGCAux { |
17166 | #[allow (dead_code)] |
17167 | fn serialize(&self, value_mask: u32) -> Vec<u8> { |
17168 | let mut result = Vec::new(); |
17169 | self.serialize_into(&mut result, u32::from(value_mask)); |
17170 | result |
17171 | } |
17172 | fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) { |
17173 | assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant" ); |
17174 | if let Some(function) = self.function { |
17175 | u32::from(function).serialize_into(bytes); |
17176 | } |
17177 | if let Some(plane_mask) = self.plane_mask { |
17178 | plane_mask.serialize_into(bytes); |
17179 | } |
17180 | if let Some(foreground) = self.foreground { |
17181 | foreground.serialize_into(bytes); |
17182 | } |
17183 | if let Some(background) = self.background { |
17184 | background.serialize_into(bytes); |
17185 | } |
17186 | if let Some(line_width) = self.line_width { |
17187 | line_width.serialize_into(bytes); |
17188 | } |
17189 | if let Some(line_style) = self.line_style { |
17190 | u32::from(line_style).serialize_into(bytes); |
17191 | } |
17192 | if let Some(cap_style) = self.cap_style { |
17193 | u32::from(cap_style).serialize_into(bytes); |
17194 | } |
17195 | if let Some(join_style) = self.join_style { |
17196 | u32::from(join_style).serialize_into(bytes); |
17197 | } |
17198 | if let Some(fill_style) = self.fill_style { |
17199 | u32::from(fill_style).serialize_into(bytes); |
17200 | } |
17201 | if let Some(fill_rule) = self.fill_rule { |
17202 | u32::from(fill_rule).serialize_into(bytes); |
17203 | } |
17204 | if let Some(tile) = self.tile { |
17205 | tile.serialize_into(bytes); |
17206 | } |
17207 | if let Some(stipple) = self.stipple { |
17208 | stipple.serialize_into(bytes); |
17209 | } |
17210 | if let Some(tile_stipple_x_origin) = self.tile_stipple_x_origin { |
17211 | tile_stipple_x_origin.serialize_into(bytes); |
17212 | } |
17213 | if let Some(tile_stipple_y_origin) = self.tile_stipple_y_origin { |
17214 | tile_stipple_y_origin.serialize_into(bytes); |
17215 | } |
17216 | if let Some(font) = self.font { |
17217 | font.serialize_into(bytes); |
17218 | } |
17219 | if let Some(subwindow_mode) = self.subwindow_mode { |
17220 | u32::from(subwindow_mode).serialize_into(bytes); |
17221 | } |
17222 | if let Some(graphics_exposures) = self.graphics_exposures { |
17223 | graphics_exposures.serialize_into(bytes); |
17224 | } |
17225 | if let Some(clip_x_origin) = self.clip_x_origin { |
17226 | clip_x_origin.serialize_into(bytes); |
17227 | } |
17228 | if let Some(clip_y_origin) = self.clip_y_origin { |
17229 | clip_y_origin.serialize_into(bytes); |
17230 | } |
17231 | if let Some(clip_mask) = self.clip_mask { |
17232 | clip_mask.serialize_into(bytes); |
17233 | } |
17234 | if let Some(dash_offset) = self.dash_offset { |
17235 | dash_offset.serialize_into(bytes); |
17236 | } |
17237 | if let Some(dashes) = self.dashes { |
17238 | dashes.serialize_into(bytes); |
17239 | } |
17240 | if let Some(arc_mode) = self.arc_mode { |
17241 | u32::from(arc_mode).serialize_into(bytes); |
17242 | } |
17243 | } |
17244 | } |
17245 | impl ChangeGCAux { |
17246 | fn switch_expr(&self) -> u32 { |
17247 | let mut expr_value = 0; |
17248 | if self.function.is_some() { |
17249 | expr_value |= u32::from(GC::FUNCTION); |
17250 | } |
17251 | if self.plane_mask.is_some() { |
17252 | expr_value |= u32::from(GC::PLANE_MASK); |
17253 | } |
17254 | if self.foreground.is_some() { |
17255 | expr_value |= u32::from(GC::FOREGROUND); |
17256 | } |
17257 | if self.background.is_some() { |
17258 | expr_value |= u32::from(GC::BACKGROUND); |
17259 | } |
17260 | if self.line_width.is_some() { |
17261 | expr_value |= u32::from(GC::LINE_WIDTH); |
17262 | } |
17263 | if self.line_style.is_some() { |
17264 | expr_value |= u32::from(GC::LINE_STYLE); |
17265 | } |
17266 | if self.cap_style.is_some() { |
17267 | expr_value |= u32::from(GC::CAP_STYLE); |
17268 | } |
17269 | if self.join_style.is_some() { |
17270 | expr_value |= u32::from(GC::JOIN_STYLE); |
17271 | } |
17272 | if self.fill_style.is_some() { |
17273 | expr_value |= u32::from(GC::FILL_STYLE); |
17274 | } |
17275 | if self.fill_rule.is_some() { |
17276 | expr_value |= u32::from(GC::FILL_RULE); |
17277 | } |
17278 | if self.tile.is_some() { |
17279 | expr_value |= u32::from(GC::TILE); |
17280 | } |
17281 | if self.stipple.is_some() { |
17282 | expr_value |= u32::from(GC::STIPPLE); |
17283 | } |
17284 | if self.tile_stipple_x_origin.is_some() { |
17285 | expr_value |= u32::from(GC::TILE_STIPPLE_ORIGIN_X); |
17286 | } |
17287 | if self.tile_stipple_y_origin.is_some() { |
17288 | expr_value |= u32::from(GC::TILE_STIPPLE_ORIGIN_Y); |
17289 | } |
17290 | if self.font.is_some() { |
17291 | expr_value |= u32::from(GC::FONT); |
17292 | } |
17293 | if self.subwindow_mode.is_some() { |
17294 | expr_value |= u32::from(GC::SUBWINDOW_MODE); |
17295 | } |
17296 | if self.graphics_exposures.is_some() { |
17297 | expr_value |= u32::from(GC::GRAPHICS_EXPOSURES); |
17298 | } |
17299 | if self.clip_x_origin.is_some() { |
17300 | expr_value |= u32::from(GC::CLIP_ORIGIN_X); |
17301 | } |
17302 | if self.clip_y_origin.is_some() { |
17303 | expr_value |= u32::from(GC::CLIP_ORIGIN_Y); |
17304 | } |
17305 | if self.clip_mask.is_some() { |
17306 | expr_value |= u32::from(GC::CLIP_MASK); |
17307 | } |
17308 | if self.dash_offset.is_some() { |
17309 | expr_value |= u32::from(GC::DASH_OFFSET); |
17310 | } |
17311 | if self.dashes.is_some() { |
17312 | expr_value |= u32::from(GC::DASH_LIST); |
17313 | } |
17314 | if self.arc_mode.is_some() { |
17315 | expr_value |= u32::from(GC::ARC_MODE); |
17316 | } |
17317 | expr_value |
17318 | } |
17319 | } |
17320 | impl ChangeGCAux { |
17321 | /// Create a new instance with all fields unset / not present. |
17322 | pub fn new() -> Self { |
17323 | Default::default() |
17324 | } |
17325 | /// Set the `function` field of this structure. |
17326 | #[must_use ] |
17327 | pub fn function<I>(mut self, value: I) -> Self where I: Into<Option<GX>> { |
17328 | self.function = value.into(); |
17329 | self |
17330 | } |
17331 | /// Set the `plane_mask` field of this structure. |
17332 | #[must_use ] |
17333 | pub fn plane_mask<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
17334 | self.plane_mask = value.into(); |
17335 | self |
17336 | } |
17337 | /// Set the `foreground` field of this structure. |
17338 | #[must_use ] |
17339 | pub fn foreground<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
17340 | self.foreground = value.into(); |
17341 | self |
17342 | } |
17343 | /// Set the `background` field of this structure. |
17344 | #[must_use ] |
17345 | pub fn background<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
17346 | self.background = value.into(); |
17347 | self |
17348 | } |
17349 | /// Set the `line_width` field of this structure. |
17350 | #[must_use ] |
17351 | pub fn line_width<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
17352 | self.line_width = value.into(); |
17353 | self |
17354 | } |
17355 | /// Set the `line_style` field of this structure. |
17356 | #[must_use ] |
17357 | pub fn line_style<I>(mut self, value: I) -> Self where I: Into<Option<LineStyle>> { |
17358 | self.line_style = value.into(); |
17359 | self |
17360 | } |
17361 | /// Set the `cap_style` field of this structure. |
17362 | #[must_use ] |
17363 | pub fn cap_style<I>(mut self, value: I) -> Self where I: Into<Option<CapStyle>> { |
17364 | self.cap_style = value.into(); |
17365 | self |
17366 | } |
17367 | /// Set the `join_style` field of this structure. |
17368 | #[must_use ] |
17369 | pub fn join_style<I>(mut self, value: I) -> Self where I: Into<Option<JoinStyle>> { |
17370 | self.join_style = value.into(); |
17371 | self |
17372 | } |
17373 | /// Set the `fill_style` field of this structure. |
17374 | #[must_use ] |
17375 | pub fn fill_style<I>(mut self, value: I) -> Self where I: Into<Option<FillStyle>> { |
17376 | self.fill_style = value.into(); |
17377 | self |
17378 | } |
17379 | /// Set the `fill_rule` field of this structure. |
17380 | #[must_use ] |
17381 | pub fn fill_rule<I>(mut self, value: I) -> Self where I: Into<Option<FillRule>> { |
17382 | self.fill_rule = value.into(); |
17383 | self |
17384 | } |
17385 | /// Set the `tile` field of this structure. |
17386 | #[must_use ] |
17387 | pub fn tile<I>(mut self, value: I) -> Self where I: Into<Option<Pixmap>> { |
17388 | self.tile = value.into(); |
17389 | self |
17390 | } |
17391 | /// Set the `stipple` field of this structure. |
17392 | #[must_use ] |
17393 | pub fn stipple<I>(mut self, value: I) -> Self where I: Into<Option<Pixmap>> { |
17394 | self.stipple = value.into(); |
17395 | self |
17396 | } |
17397 | /// Set the `tile_stipple_x_origin` field of this structure. |
17398 | #[must_use ] |
17399 | pub fn tile_stipple_x_origin<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
17400 | self.tile_stipple_x_origin = value.into(); |
17401 | self |
17402 | } |
17403 | /// Set the `tile_stipple_y_origin` field of this structure. |
17404 | #[must_use ] |
17405 | pub fn tile_stipple_y_origin<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
17406 | self.tile_stipple_y_origin = value.into(); |
17407 | self |
17408 | } |
17409 | /// Set the `font` field of this structure. |
17410 | #[must_use ] |
17411 | pub fn font<I>(mut self, value: I) -> Self where I: Into<Option<Font>> { |
17412 | self.font = value.into(); |
17413 | self |
17414 | } |
17415 | /// Set the `subwindow_mode` field of this structure. |
17416 | #[must_use ] |
17417 | pub fn subwindow_mode<I>(mut self, value: I) -> Self where I: Into<Option<SubwindowMode>> { |
17418 | self.subwindow_mode = value.into(); |
17419 | self |
17420 | } |
17421 | /// Set the `graphics_exposures` field of this structure. |
17422 | #[must_use ] |
17423 | pub fn graphics_exposures<I>(mut self, value: I) -> Self where I: Into<Option<Bool32>> { |
17424 | self.graphics_exposures = value.into(); |
17425 | self |
17426 | } |
17427 | /// Set the `clip_x_origin` field of this structure. |
17428 | #[must_use ] |
17429 | pub fn clip_x_origin<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
17430 | self.clip_x_origin = value.into(); |
17431 | self |
17432 | } |
17433 | /// Set the `clip_y_origin` field of this structure. |
17434 | #[must_use ] |
17435 | pub fn clip_y_origin<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
17436 | self.clip_y_origin = value.into(); |
17437 | self |
17438 | } |
17439 | /// Set the `clip_mask` field of this structure. |
17440 | #[must_use ] |
17441 | pub fn clip_mask<I>(mut self, value: I) -> Self where I: Into<Option<Pixmap>> { |
17442 | self.clip_mask = value.into(); |
17443 | self |
17444 | } |
17445 | /// Set the `dash_offset` field of this structure. |
17446 | #[must_use ] |
17447 | pub fn dash_offset<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
17448 | self.dash_offset = value.into(); |
17449 | self |
17450 | } |
17451 | /// Set the `dashes` field of this structure. |
17452 | #[must_use ] |
17453 | pub fn dashes<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
17454 | self.dashes = value.into(); |
17455 | self |
17456 | } |
17457 | /// Set the `arc_mode` field of this structure. |
17458 | #[must_use ] |
17459 | pub fn arc_mode<I>(mut self, value: I) -> Self where I: Into<Option<ArcMode>> { |
17460 | self.arc_mode = value.into(); |
17461 | self |
17462 | } |
17463 | } |
17464 | |
17465 | /// Opcode for the ChangeGC request |
17466 | pub const CHANGE_GC_REQUEST: u8 = 56; |
17467 | /// change graphics context components. |
17468 | /// |
17469 | /// Changes the components specified by `value_mask` for the specified graphics context. |
17470 | /// |
17471 | /// # Fields |
17472 | /// |
17473 | /// * `gc` - The graphics context to change. |
17474 | /// * `value_list` - Values for each of the components specified in the bitmask `value_mask`. The |
17475 | /// order has to correspond to the order of possible `value_mask` bits. See the |
17476 | /// example. |
17477 | /// |
17478 | /// # Errors |
17479 | /// |
17480 | /// * `Font` - TODO: reasons? |
17481 | /// * `GContext` - TODO: reasons? |
17482 | /// * `Match` - TODO: reasons? |
17483 | /// * `Pixmap` - TODO: reasons? |
17484 | /// * `Value` - TODO: reasons? |
17485 | /// * `Alloc` - The X server could not allocate the requested resources (no memory?). |
17486 | /// |
17487 | /// # Example |
17488 | /// |
17489 | /// ```text |
17490 | /// /* |
17491 | /// * Changes the foreground color component of the specified graphics context. |
17492 | /// * |
17493 | /// */ |
17494 | /// void my_example(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t fg, uint32_t bg) { |
17495 | /// /* C99 allows us to use a compact way of changing a single component: */ |
17496 | /// xcb_change_gc(conn, gc, XCB_GC_FOREGROUND, (uint32_t[]){ fg }); |
17497 | /// |
17498 | /// /* The more explicit way. Beware that the order of values is important! */ |
17499 | /// uint32_t mask = 0; |
17500 | /// mask |= XCB_GC_FOREGROUND; |
17501 | /// mask |= XCB_GC_BACKGROUND; |
17502 | /// |
17503 | /// uint32_t values[] = { |
17504 | /// fg, |
17505 | /// bg |
17506 | /// }; |
17507 | /// xcb_change_gc(conn, gc, mask, values); |
17508 | /// xcb_flush(conn); |
17509 | /// } |
17510 | /// ``` |
17511 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
17512 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
17513 | pub struct ChangeGCRequest<'input> { |
17514 | pub gc: Gcontext, |
17515 | pub value_list: Cow<'input, ChangeGCAux>, |
17516 | } |
17517 | impl<'input> ChangeGCRequest<'input> { |
17518 | /// Serialize this request into bytes for the provided connection |
17519 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
17520 | let length_so_far = 0; |
17521 | let gc_bytes = self.gc.serialize(); |
17522 | let value_mask: u32 = self.value_list.switch_expr(); |
17523 | let value_mask_bytes = value_mask.serialize(); |
17524 | let mut request0 = vec![ |
17525 | CHANGE_GC_REQUEST, |
17526 | 0, |
17527 | 0, |
17528 | 0, |
17529 | gc_bytes[0], |
17530 | gc_bytes[1], |
17531 | gc_bytes[2], |
17532 | gc_bytes[3], |
17533 | value_mask_bytes[0], |
17534 | value_mask_bytes[1], |
17535 | value_mask_bytes[2], |
17536 | value_mask_bytes[3], |
17537 | ]; |
17538 | let length_so_far = length_so_far + request0.len(); |
17539 | let value_list_bytes = self.value_list.serialize(u32::from(value_mask)); |
17540 | let length_so_far = length_so_far + value_list_bytes.len(); |
17541 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
17542 | let length_so_far = length_so_far + padding0.len(); |
17543 | assert_eq!(length_so_far % 4, 0); |
17544 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
17545 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
17546 | (vec![request0.into(), value_list_bytes.into(), padding0.into()], vec![]) |
17547 | } |
17548 | /// Parse this request given its header, its body, and any fds that go along with it |
17549 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
17550 | if header.major_opcode != CHANGE_GC_REQUEST { |
17551 | return Err(ParseError::InvalidValue); |
17552 | } |
17553 | let remaining = &[header.minor_opcode]; |
17554 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
17555 | let _ = remaining; |
17556 | let (gc, remaining) = Gcontext::try_parse(value)?; |
17557 | let (value_mask, remaining) = u32::try_parse(remaining)?; |
17558 | let (value_list, remaining) = ChangeGCAux::try_parse(remaining, u32::from(value_mask))?; |
17559 | let _ = remaining; |
17560 | Ok(ChangeGCRequest { |
17561 | gc, |
17562 | value_list: Cow::Owned(value_list), |
17563 | }) |
17564 | } |
17565 | /// Clone all borrowed data in this ChangeGCRequest. |
17566 | pub fn into_owned(self) -> ChangeGCRequest<'static> { |
17567 | ChangeGCRequest { |
17568 | gc: self.gc, |
17569 | value_list: Cow::Owned(self.value_list.into_owned()), |
17570 | } |
17571 | } |
17572 | } |
17573 | impl<'input> Request for ChangeGCRequest<'input> { |
17574 | const EXTENSION_NAME: Option<&'static str> = None; |
17575 | |
17576 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
17577 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
17578 | // Flatten the buffers into a single vector |
17579 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
17580 | (buf, fds) |
17581 | } |
17582 | } |
17583 | impl<'input> crate::x11_utils::VoidRequest for ChangeGCRequest<'input> { |
17584 | } |
17585 | |
17586 | /// Opcode for the CopyGC request |
17587 | pub const COPY_GC_REQUEST: u8 = 57; |
17588 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
17589 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
17590 | pub struct CopyGCRequest { |
17591 | pub src_gc: Gcontext, |
17592 | pub dst_gc: Gcontext, |
17593 | pub value_mask: GC, |
17594 | } |
17595 | impl CopyGCRequest { |
17596 | /// Serialize this request into bytes for the provided connection |
17597 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
17598 | let length_so_far = 0; |
17599 | let src_gc_bytes = self.src_gc.serialize(); |
17600 | let dst_gc_bytes = self.dst_gc.serialize(); |
17601 | let value_mask_bytes = u32::from(self.value_mask).serialize(); |
17602 | let mut request0 = vec![ |
17603 | COPY_GC_REQUEST, |
17604 | 0, |
17605 | 0, |
17606 | 0, |
17607 | src_gc_bytes[0], |
17608 | src_gc_bytes[1], |
17609 | src_gc_bytes[2], |
17610 | src_gc_bytes[3], |
17611 | dst_gc_bytes[0], |
17612 | dst_gc_bytes[1], |
17613 | dst_gc_bytes[2], |
17614 | dst_gc_bytes[3], |
17615 | value_mask_bytes[0], |
17616 | value_mask_bytes[1], |
17617 | value_mask_bytes[2], |
17618 | value_mask_bytes[3], |
17619 | ]; |
17620 | let length_so_far = length_so_far + request0.len(); |
17621 | assert_eq!(length_so_far % 4, 0); |
17622 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
17623 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
17624 | (vec![request0.into()], vec![]) |
17625 | } |
17626 | /// Parse this request given its header, its body, and any fds that go along with it |
17627 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
17628 | if header.major_opcode != COPY_GC_REQUEST { |
17629 | return Err(ParseError::InvalidValue); |
17630 | } |
17631 | let remaining = &[header.minor_opcode]; |
17632 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
17633 | let _ = remaining; |
17634 | let (src_gc, remaining) = Gcontext::try_parse(value)?; |
17635 | let (dst_gc, remaining) = Gcontext::try_parse(remaining)?; |
17636 | let (value_mask, remaining) = u32::try_parse(remaining)?; |
17637 | let value_mask = value_mask.into(); |
17638 | let _ = remaining; |
17639 | Ok(CopyGCRequest { |
17640 | src_gc, |
17641 | dst_gc, |
17642 | value_mask, |
17643 | }) |
17644 | } |
17645 | } |
17646 | impl Request for CopyGCRequest { |
17647 | const EXTENSION_NAME: Option<&'static str> = None; |
17648 | |
17649 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
17650 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
17651 | // Flatten the buffers into a single vector |
17652 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
17653 | (buf, fds) |
17654 | } |
17655 | } |
17656 | impl crate::x11_utils::VoidRequest for CopyGCRequest { |
17657 | } |
17658 | |
17659 | /// Opcode for the SetDashes request |
17660 | pub const SET_DASHES_REQUEST: u8 = 58; |
17661 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
17662 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
17663 | pub struct SetDashesRequest<'input> { |
17664 | pub gc: Gcontext, |
17665 | pub dash_offset: u16, |
17666 | pub dashes: Cow<'input, [u8]>, |
17667 | } |
17668 | impl<'input> SetDashesRequest<'input> { |
17669 | /// Serialize this request into bytes for the provided connection |
17670 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
17671 | let length_so_far = 0; |
17672 | let gc_bytes = self.gc.serialize(); |
17673 | let dash_offset_bytes = self.dash_offset.serialize(); |
17674 | let dashes_len = u16::try_from(self.dashes.len()).expect("`dashes` has too many elements" ); |
17675 | let dashes_len_bytes = dashes_len.serialize(); |
17676 | let mut request0 = vec![ |
17677 | SET_DASHES_REQUEST, |
17678 | 0, |
17679 | 0, |
17680 | 0, |
17681 | gc_bytes[0], |
17682 | gc_bytes[1], |
17683 | gc_bytes[2], |
17684 | gc_bytes[3], |
17685 | dash_offset_bytes[0], |
17686 | dash_offset_bytes[1], |
17687 | dashes_len_bytes[0], |
17688 | dashes_len_bytes[1], |
17689 | ]; |
17690 | let length_so_far = length_so_far + request0.len(); |
17691 | let length_so_far = length_so_far + self.dashes.len(); |
17692 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
17693 | let length_so_far = length_so_far + padding0.len(); |
17694 | assert_eq!(length_so_far % 4, 0); |
17695 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
17696 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
17697 | (vec![request0.into(), self.dashes, padding0.into()], vec![]) |
17698 | } |
17699 | /// Parse this request given its header, its body, and any fds that go along with it |
17700 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
17701 | if header.major_opcode != SET_DASHES_REQUEST { |
17702 | return Err(ParseError::InvalidValue); |
17703 | } |
17704 | let remaining = &[header.minor_opcode]; |
17705 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
17706 | let _ = remaining; |
17707 | let (gc, remaining) = Gcontext::try_parse(value)?; |
17708 | let (dash_offset, remaining) = u16::try_parse(remaining)?; |
17709 | let (dashes_len, remaining) = u16::try_parse(remaining)?; |
17710 | let (dashes, remaining) = crate::x11_utils::parse_u8_list(remaining, dashes_len.try_to_usize()?)?; |
17711 | let _ = remaining; |
17712 | Ok(SetDashesRequest { |
17713 | gc, |
17714 | dash_offset, |
17715 | dashes: Cow::Borrowed(dashes), |
17716 | }) |
17717 | } |
17718 | /// Clone all borrowed data in this SetDashesRequest. |
17719 | pub fn into_owned(self) -> SetDashesRequest<'static> { |
17720 | SetDashesRequest { |
17721 | gc: self.gc, |
17722 | dash_offset: self.dash_offset, |
17723 | dashes: Cow::Owned(self.dashes.into_owned()), |
17724 | } |
17725 | } |
17726 | } |
17727 | impl<'input> Request for SetDashesRequest<'input> { |
17728 | const EXTENSION_NAME: Option<&'static str> = None; |
17729 | |
17730 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
17731 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
17732 | // Flatten the buffers into a single vector |
17733 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
17734 | (buf, fds) |
17735 | } |
17736 | } |
17737 | impl<'input> crate::x11_utils::VoidRequest for SetDashesRequest<'input> { |
17738 | } |
17739 | |
17740 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
17741 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
17742 | pub struct ClipOrdering(u8); |
17743 | impl ClipOrdering { |
17744 | pub const UNSORTED: Self = Self(0); |
17745 | pub const Y_SORTED: Self = Self(1); |
17746 | pub const YX_SORTED: Self = Self(2); |
17747 | pub const YX_BANDED: Self = Self(3); |
17748 | } |
17749 | impl From<ClipOrdering> for u8 { |
17750 | #[inline ] |
17751 | fn from(input: ClipOrdering) -> Self { |
17752 | input.0 |
17753 | } |
17754 | } |
17755 | impl From<ClipOrdering> for Option<u8> { |
17756 | #[inline ] |
17757 | fn from(input: ClipOrdering) -> Self { |
17758 | Some(input.0) |
17759 | } |
17760 | } |
17761 | impl From<ClipOrdering> for u16 { |
17762 | #[inline ] |
17763 | fn from(input: ClipOrdering) -> Self { |
17764 | u16::from(input.0) |
17765 | } |
17766 | } |
17767 | impl From<ClipOrdering> for Option<u16> { |
17768 | #[inline ] |
17769 | fn from(input: ClipOrdering) -> Self { |
17770 | Some(u16::from(input.0)) |
17771 | } |
17772 | } |
17773 | impl From<ClipOrdering> for u32 { |
17774 | #[inline ] |
17775 | fn from(input: ClipOrdering) -> Self { |
17776 | u32::from(input.0) |
17777 | } |
17778 | } |
17779 | impl From<ClipOrdering> for Option<u32> { |
17780 | #[inline ] |
17781 | fn from(input: ClipOrdering) -> Self { |
17782 | Some(u32::from(input.0)) |
17783 | } |
17784 | } |
17785 | impl From<u8> for ClipOrdering { |
17786 | #[inline ] |
17787 | fn from(value: u8) -> Self { |
17788 | Self(value) |
17789 | } |
17790 | } |
17791 | impl core::fmt::Debug for ClipOrdering { |
17792 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
17793 | let variants: [(u32, &str, &str); 4] = [ |
17794 | (Self::UNSORTED.0.into(), "UNSORTED" , "Unsorted" ), |
17795 | (Self::Y_SORTED.0.into(), "Y_SORTED" , "YSorted" ), |
17796 | (Self::YX_SORTED.0.into(), "YX_SORTED" , "YXSorted" ), |
17797 | (Self::YX_BANDED.0.into(), "YX_BANDED" , "YXBanded" ), |
17798 | ]; |
17799 | pretty_print_enum(fmt, self.0.into(), &variants) |
17800 | } |
17801 | } |
17802 | |
17803 | /// Opcode for the SetClipRectangles request |
17804 | pub const SET_CLIP_RECTANGLES_REQUEST: u8 = 59; |
17805 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
17806 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
17807 | pub struct SetClipRectanglesRequest<'input> { |
17808 | pub ordering: ClipOrdering, |
17809 | pub gc: Gcontext, |
17810 | pub clip_x_origin: i16, |
17811 | pub clip_y_origin: i16, |
17812 | pub rectangles: Cow<'input, [Rectangle]>, |
17813 | } |
17814 | impl<'input> SetClipRectanglesRequest<'input> { |
17815 | /// Serialize this request into bytes for the provided connection |
17816 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
17817 | let length_so_far = 0; |
17818 | let ordering_bytes = u8::from(self.ordering).serialize(); |
17819 | let gc_bytes = self.gc.serialize(); |
17820 | let clip_x_origin_bytes = self.clip_x_origin.serialize(); |
17821 | let clip_y_origin_bytes = self.clip_y_origin.serialize(); |
17822 | let mut request0 = vec![ |
17823 | SET_CLIP_RECTANGLES_REQUEST, |
17824 | ordering_bytes[0], |
17825 | 0, |
17826 | 0, |
17827 | gc_bytes[0], |
17828 | gc_bytes[1], |
17829 | gc_bytes[2], |
17830 | gc_bytes[3], |
17831 | clip_x_origin_bytes[0], |
17832 | clip_x_origin_bytes[1], |
17833 | clip_y_origin_bytes[0], |
17834 | clip_y_origin_bytes[1], |
17835 | ]; |
17836 | let length_so_far = length_so_far + request0.len(); |
17837 | let rectangles_bytes = self.rectangles.serialize(); |
17838 | let length_so_far = length_so_far + rectangles_bytes.len(); |
17839 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
17840 | let length_so_far = length_so_far + padding0.len(); |
17841 | assert_eq!(length_so_far % 4, 0); |
17842 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
17843 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
17844 | (vec![request0.into(), rectangles_bytes.into(), padding0.into()], vec![]) |
17845 | } |
17846 | /// Parse this request given its header, its body, and any fds that go along with it |
17847 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
17848 | if header.major_opcode != SET_CLIP_RECTANGLES_REQUEST { |
17849 | return Err(ParseError::InvalidValue); |
17850 | } |
17851 | let remaining = &[header.minor_opcode]; |
17852 | let (ordering, remaining) = u8::try_parse(remaining)?; |
17853 | let ordering = ordering.into(); |
17854 | let _ = remaining; |
17855 | let (gc, remaining) = Gcontext::try_parse(value)?; |
17856 | let (clip_x_origin, remaining) = i16::try_parse(remaining)?; |
17857 | let (clip_y_origin, remaining) = i16::try_parse(remaining)?; |
17858 | let mut remaining = remaining; |
17859 | // Length is 'everything left in the input' |
17860 | let mut rectangles = Vec::new(); |
17861 | while !remaining.is_empty() { |
17862 | let (v, new_remaining) = Rectangle::try_parse(remaining)?; |
17863 | remaining = new_remaining; |
17864 | rectangles.push(v); |
17865 | } |
17866 | let _ = remaining; |
17867 | Ok(SetClipRectanglesRequest { |
17868 | ordering, |
17869 | gc, |
17870 | clip_x_origin, |
17871 | clip_y_origin, |
17872 | rectangles: Cow::Owned(rectangles), |
17873 | }) |
17874 | } |
17875 | /// Clone all borrowed data in this SetClipRectanglesRequest. |
17876 | pub fn into_owned(self) -> SetClipRectanglesRequest<'static> { |
17877 | SetClipRectanglesRequest { |
17878 | ordering: self.ordering, |
17879 | gc: self.gc, |
17880 | clip_x_origin: self.clip_x_origin, |
17881 | clip_y_origin: self.clip_y_origin, |
17882 | rectangles: Cow::Owned(self.rectangles.into_owned()), |
17883 | } |
17884 | } |
17885 | } |
17886 | impl<'input> Request for SetClipRectanglesRequest<'input> { |
17887 | const EXTENSION_NAME: Option<&'static str> = None; |
17888 | |
17889 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
17890 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
17891 | // Flatten the buffers into a single vector |
17892 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
17893 | (buf, fds) |
17894 | } |
17895 | } |
17896 | impl<'input> crate::x11_utils::VoidRequest for SetClipRectanglesRequest<'input> { |
17897 | } |
17898 | |
17899 | /// Opcode for the FreeGC request |
17900 | pub const FREE_GC_REQUEST: u8 = 60; |
17901 | /// Destroys a graphics context. |
17902 | /// |
17903 | /// Destroys the specified `gc` and all associated storage. |
17904 | /// |
17905 | /// # Fields |
17906 | /// |
17907 | /// * `gc` - The graphics context to destroy. |
17908 | /// |
17909 | /// # Errors |
17910 | /// |
17911 | /// * `GContext` - The specified graphics context does not exist. |
17912 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
17913 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
17914 | pub struct FreeGCRequest { |
17915 | pub gc: Gcontext, |
17916 | } |
17917 | impl FreeGCRequest { |
17918 | /// Serialize this request into bytes for the provided connection |
17919 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
17920 | let length_so_far = 0; |
17921 | let gc_bytes = self.gc.serialize(); |
17922 | let mut request0 = vec![ |
17923 | FREE_GC_REQUEST, |
17924 | 0, |
17925 | 0, |
17926 | 0, |
17927 | gc_bytes[0], |
17928 | gc_bytes[1], |
17929 | gc_bytes[2], |
17930 | gc_bytes[3], |
17931 | ]; |
17932 | let length_so_far = length_so_far + request0.len(); |
17933 | assert_eq!(length_so_far % 4, 0); |
17934 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
17935 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
17936 | (vec![request0.into()], vec![]) |
17937 | } |
17938 | /// Parse this request given its header, its body, and any fds that go along with it |
17939 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
17940 | if header.major_opcode != FREE_GC_REQUEST { |
17941 | return Err(ParseError::InvalidValue); |
17942 | } |
17943 | let remaining = &[header.minor_opcode]; |
17944 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
17945 | let _ = remaining; |
17946 | let (gc, remaining) = Gcontext::try_parse(value)?; |
17947 | let _ = remaining; |
17948 | Ok(FreeGCRequest { |
17949 | gc, |
17950 | }) |
17951 | } |
17952 | } |
17953 | impl Request for FreeGCRequest { |
17954 | const EXTENSION_NAME: Option<&'static str> = None; |
17955 | |
17956 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
17957 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
17958 | // Flatten the buffers into a single vector |
17959 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
17960 | (buf, fds) |
17961 | } |
17962 | } |
17963 | impl crate::x11_utils::VoidRequest for FreeGCRequest { |
17964 | } |
17965 | |
17966 | /// Opcode for the ClearArea request |
17967 | pub const CLEAR_AREA_REQUEST: u8 = 61; |
17968 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
17969 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
17970 | pub struct ClearAreaRequest { |
17971 | pub exposures: bool, |
17972 | pub window: Window, |
17973 | pub x: i16, |
17974 | pub y: i16, |
17975 | pub width: u16, |
17976 | pub height: u16, |
17977 | } |
17978 | impl ClearAreaRequest { |
17979 | /// Serialize this request into bytes for the provided connection |
17980 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
17981 | let length_so_far = 0; |
17982 | let exposures_bytes = self.exposures.serialize(); |
17983 | let window_bytes = self.window.serialize(); |
17984 | let x_bytes = self.x.serialize(); |
17985 | let y_bytes = self.y.serialize(); |
17986 | let width_bytes = self.width.serialize(); |
17987 | let height_bytes = self.height.serialize(); |
17988 | let mut request0 = vec![ |
17989 | CLEAR_AREA_REQUEST, |
17990 | exposures_bytes[0], |
17991 | 0, |
17992 | 0, |
17993 | window_bytes[0], |
17994 | window_bytes[1], |
17995 | window_bytes[2], |
17996 | window_bytes[3], |
17997 | x_bytes[0], |
17998 | x_bytes[1], |
17999 | y_bytes[0], |
18000 | y_bytes[1], |
18001 | width_bytes[0], |
18002 | width_bytes[1], |
18003 | height_bytes[0], |
18004 | height_bytes[1], |
18005 | ]; |
18006 | let length_so_far = length_so_far + request0.len(); |
18007 | assert_eq!(length_so_far % 4, 0); |
18008 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
18009 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
18010 | (vec![request0.into()], vec![]) |
18011 | } |
18012 | /// Parse this request given its header, its body, and any fds that go along with it |
18013 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
18014 | if header.major_opcode != CLEAR_AREA_REQUEST { |
18015 | return Err(ParseError::InvalidValue); |
18016 | } |
18017 | let remaining = &[header.minor_opcode]; |
18018 | let (exposures, remaining) = bool::try_parse(remaining)?; |
18019 | let _ = remaining; |
18020 | let (window, remaining) = Window::try_parse(value)?; |
18021 | let (x, remaining) = i16::try_parse(remaining)?; |
18022 | let (y, remaining) = i16::try_parse(remaining)?; |
18023 | let (width, remaining) = u16::try_parse(remaining)?; |
18024 | let (height, remaining) = u16::try_parse(remaining)?; |
18025 | let _ = remaining; |
18026 | Ok(ClearAreaRequest { |
18027 | exposures, |
18028 | window, |
18029 | x, |
18030 | y, |
18031 | width, |
18032 | height, |
18033 | }) |
18034 | } |
18035 | } |
18036 | impl Request for ClearAreaRequest { |
18037 | const EXTENSION_NAME: Option<&'static str> = None; |
18038 | |
18039 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
18040 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
18041 | // Flatten the buffers into a single vector |
18042 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
18043 | (buf, fds) |
18044 | } |
18045 | } |
18046 | impl crate::x11_utils::VoidRequest for ClearAreaRequest { |
18047 | } |
18048 | |
18049 | /// Opcode for the CopyArea request |
18050 | pub const COPY_AREA_REQUEST: u8 = 62; |
18051 | /// copy areas. |
18052 | /// |
18053 | /// Copies the specified rectangle from `src_drawable` to `dst_drawable`. |
18054 | /// |
18055 | /// # Fields |
18056 | /// |
18057 | /// * `dst_drawable` - The destination drawable (Window or Pixmap). |
18058 | /// * `src_drawable` - The source drawable (Window or Pixmap). |
18059 | /// * `gc` - The graphics context to use. |
18060 | /// * `src_x` - The source X coordinate. |
18061 | /// * `src_y` - The source Y coordinate. |
18062 | /// * `dst_x` - The destination X coordinate. |
18063 | /// * `dst_y` - The destination Y coordinate. |
18064 | /// * `width` - The width of the area to copy (in pixels). |
18065 | /// * `height` - The height of the area to copy (in pixels). |
18066 | /// |
18067 | /// # Errors |
18068 | /// |
18069 | /// * `Drawable` - The specified `drawable` (Window or Pixmap) does not exist. |
18070 | /// * `GContext` - The specified graphics context does not exist. |
18071 | /// * `Match` - `src_drawable` has a different root or depth than `dst_drawable`. |
18072 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
18073 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
18074 | pub struct CopyAreaRequest { |
18075 | pub src_drawable: Drawable, |
18076 | pub dst_drawable: Drawable, |
18077 | pub gc: Gcontext, |
18078 | pub src_x: i16, |
18079 | pub src_y: i16, |
18080 | pub dst_x: i16, |
18081 | pub dst_y: i16, |
18082 | pub width: u16, |
18083 | pub height: u16, |
18084 | } |
18085 | impl CopyAreaRequest { |
18086 | /// Serialize this request into bytes for the provided connection |
18087 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
18088 | let length_so_far = 0; |
18089 | let src_drawable_bytes = self.src_drawable.serialize(); |
18090 | let dst_drawable_bytes = self.dst_drawable.serialize(); |
18091 | let gc_bytes = self.gc.serialize(); |
18092 | let src_x_bytes = self.src_x.serialize(); |
18093 | let src_y_bytes = self.src_y.serialize(); |
18094 | let dst_x_bytes = self.dst_x.serialize(); |
18095 | let dst_y_bytes = self.dst_y.serialize(); |
18096 | let width_bytes = self.width.serialize(); |
18097 | let height_bytes = self.height.serialize(); |
18098 | let mut request0 = vec![ |
18099 | COPY_AREA_REQUEST, |
18100 | 0, |
18101 | 0, |
18102 | 0, |
18103 | src_drawable_bytes[0], |
18104 | src_drawable_bytes[1], |
18105 | src_drawable_bytes[2], |
18106 | src_drawable_bytes[3], |
18107 | dst_drawable_bytes[0], |
18108 | dst_drawable_bytes[1], |
18109 | dst_drawable_bytes[2], |
18110 | dst_drawable_bytes[3], |
18111 | gc_bytes[0], |
18112 | gc_bytes[1], |
18113 | gc_bytes[2], |
18114 | gc_bytes[3], |
18115 | src_x_bytes[0], |
18116 | src_x_bytes[1], |
18117 | src_y_bytes[0], |
18118 | src_y_bytes[1], |
18119 | dst_x_bytes[0], |
18120 | dst_x_bytes[1], |
18121 | dst_y_bytes[0], |
18122 | dst_y_bytes[1], |
18123 | width_bytes[0], |
18124 | width_bytes[1], |
18125 | height_bytes[0], |
18126 | height_bytes[1], |
18127 | ]; |
18128 | let length_so_far = length_so_far + request0.len(); |
18129 | assert_eq!(length_so_far % 4, 0); |
18130 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
18131 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
18132 | (vec![request0.into()], vec![]) |
18133 | } |
18134 | /// Parse this request given its header, its body, and any fds that go along with it |
18135 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
18136 | if header.major_opcode != COPY_AREA_REQUEST { |
18137 | return Err(ParseError::InvalidValue); |
18138 | } |
18139 | let remaining = &[header.minor_opcode]; |
18140 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
18141 | let _ = remaining; |
18142 | let (src_drawable, remaining) = Drawable::try_parse(value)?; |
18143 | let (dst_drawable, remaining) = Drawable::try_parse(remaining)?; |
18144 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
18145 | let (src_x, remaining) = i16::try_parse(remaining)?; |
18146 | let (src_y, remaining) = i16::try_parse(remaining)?; |
18147 | let (dst_x, remaining) = i16::try_parse(remaining)?; |
18148 | let (dst_y, remaining) = i16::try_parse(remaining)?; |
18149 | let (width, remaining) = u16::try_parse(remaining)?; |
18150 | let (height, remaining) = u16::try_parse(remaining)?; |
18151 | let _ = remaining; |
18152 | Ok(CopyAreaRequest { |
18153 | src_drawable, |
18154 | dst_drawable, |
18155 | gc, |
18156 | src_x, |
18157 | src_y, |
18158 | dst_x, |
18159 | dst_y, |
18160 | width, |
18161 | height, |
18162 | }) |
18163 | } |
18164 | } |
18165 | impl Request for CopyAreaRequest { |
18166 | const EXTENSION_NAME: Option<&'static str> = None; |
18167 | |
18168 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
18169 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
18170 | // Flatten the buffers into a single vector |
18171 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
18172 | (buf, fds) |
18173 | } |
18174 | } |
18175 | impl crate::x11_utils::VoidRequest for CopyAreaRequest { |
18176 | } |
18177 | |
18178 | /// Opcode for the CopyPlane request |
18179 | pub const COPY_PLANE_REQUEST: u8 = 63; |
18180 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
18181 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
18182 | pub struct CopyPlaneRequest { |
18183 | pub src_drawable: Drawable, |
18184 | pub dst_drawable: Drawable, |
18185 | pub gc: Gcontext, |
18186 | pub src_x: i16, |
18187 | pub src_y: i16, |
18188 | pub dst_x: i16, |
18189 | pub dst_y: i16, |
18190 | pub width: u16, |
18191 | pub height: u16, |
18192 | pub bit_plane: u32, |
18193 | } |
18194 | impl CopyPlaneRequest { |
18195 | /// Serialize this request into bytes for the provided connection |
18196 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
18197 | let length_so_far = 0; |
18198 | let src_drawable_bytes = self.src_drawable.serialize(); |
18199 | let dst_drawable_bytes = self.dst_drawable.serialize(); |
18200 | let gc_bytes = self.gc.serialize(); |
18201 | let src_x_bytes = self.src_x.serialize(); |
18202 | let src_y_bytes = self.src_y.serialize(); |
18203 | let dst_x_bytes = self.dst_x.serialize(); |
18204 | let dst_y_bytes = self.dst_y.serialize(); |
18205 | let width_bytes = self.width.serialize(); |
18206 | let height_bytes = self.height.serialize(); |
18207 | let bit_plane_bytes = self.bit_plane.serialize(); |
18208 | let mut request0 = vec![ |
18209 | COPY_PLANE_REQUEST, |
18210 | 0, |
18211 | 0, |
18212 | 0, |
18213 | src_drawable_bytes[0], |
18214 | src_drawable_bytes[1], |
18215 | src_drawable_bytes[2], |
18216 | src_drawable_bytes[3], |
18217 | dst_drawable_bytes[0], |
18218 | dst_drawable_bytes[1], |
18219 | dst_drawable_bytes[2], |
18220 | dst_drawable_bytes[3], |
18221 | gc_bytes[0], |
18222 | gc_bytes[1], |
18223 | gc_bytes[2], |
18224 | gc_bytes[3], |
18225 | src_x_bytes[0], |
18226 | src_x_bytes[1], |
18227 | src_y_bytes[0], |
18228 | src_y_bytes[1], |
18229 | dst_x_bytes[0], |
18230 | dst_x_bytes[1], |
18231 | dst_y_bytes[0], |
18232 | dst_y_bytes[1], |
18233 | width_bytes[0], |
18234 | width_bytes[1], |
18235 | height_bytes[0], |
18236 | height_bytes[1], |
18237 | bit_plane_bytes[0], |
18238 | bit_plane_bytes[1], |
18239 | bit_plane_bytes[2], |
18240 | bit_plane_bytes[3], |
18241 | ]; |
18242 | let length_so_far = length_so_far + request0.len(); |
18243 | assert_eq!(length_so_far % 4, 0); |
18244 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
18245 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
18246 | (vec![request0.into()], vec![]) |
18247 | } |
18248 | /// Parse this request given its header, its body, and any fds that go along with it |
18249 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
18250 | if header.major_opcode != COPY_PLANE_REQUEST { |
18251 | return Err(ParseError::InvalidValue); |
18252 | } |
18253 | let remaining = &[header.minor_opcode]; |
18254 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
18255 | let _ = remaining; |
18256 | let (src_drawable, remaining) = Drawable::try_parse(value)?; |
18257 | let (dst_drawable, remaining) = Drawable::try_parse(remaining)?; |
18258 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
18259 | let (src_x, remaining) = i16::try_parse(remaining)?; |
18260 | let (src_y, remaining) = i16::try_parse(remaining)?; |
18261 | let (dst_x, remaining) = i16::try_parse(remaining)?; |
18262 | let (dst_y, remaining) = i16::try_parse(remaining)?; |
18263 | let (width, remaining) = u16::try_parse(remaining)?; |
18264 | let (height, remaining) = u16::try_parse(remaining)?; |
18265 | let (bit_plane, remaining) = u32::try_parse(remaining)?; |
18266 | let _ = remaining; |
18267 | Ok(CopyPlaneRequest { |
18268 | src_drawable, |
18269 | dst_drawable, |
18270 | gc, |
18271 | src_x, |
18272 | src_y, |
18273 | dst_x, |
18274 | dst_y, |
18275 | width, |
18276 | height, |
18277 | bit_plane, |
18278 | }) |
18279 | } |
18280 | } |
18281 | impl Request for CopyPlaneRequest { |
18282 | const EXTENSION_NAME: Option<&'static str> = None; |
18283 | |
18284 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
18285 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
18286 | // Flatten the buffers into a single vector |
18287 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
18288 | (buf, fds) |
18289 | } |
18290 | } |
18291 | impl crate::x11_utils::VoidRequest for CopyPlaneRequest { |
18292 | } |
18293 | |
18294 | /// # Fields |
18295 | /// |
18296 | /// * `Origin` - Treats all coordinates as relative to the origin. |
18297 | /// * `Previous` - Treats all coordinates after the first as relative to the previous coordinate. |
18298 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
18299 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
18300 | pub struct CoordMode(u8); |
18301 | impl CoordMode { |
18302 | pub const ORIGIN: Self = Self(0); |
18303 | pub const PREVIOUS: Self = Self(1); |
18304 | } |
18305 | impl From<CoordMode> for u8 { |
18306 | #[inline ] |
18307 | fn from(input: CoordMode) -> Self { |
18308 | input.0 |
18309 | } |
18310 | } |
18311 | impl From<CoordMode> for Option<u8> { |
18312 | #[inline ] |
18313 | fn from(input: CoordMode) -> Self { |
18314 | Some(input.0) |
18315 | } |
18316 | } |
18317 | impl From<CoordMode> for u16 { |
18318 | #[inline ] |
18319 | fn from(input: CoordMode) -> Self { |
18320 | u16::from(input.0) |
18321 | } |
18322 | } |
18323 | impl From<CoordMode> for Option<u16> { |
18324 | #[inline ] |
18325 | fn from(input: CoordMode) -> Self { |
18326 | Some(u16::from(input.0)) |
18327 | } |
18328 | } |
18329 | impl From<CoordMode> for u32 { |
18330 | #[inline ] |
18331 | fn from(input: CoordMode) -> Self { |
18332 | u32::from(input.0) |
18333 | } |
18334 | } |
18335 | impl From<CoordMode> for Option<u32> { |
18336 | #[inline ] |
18337 | fn from(input: CoordMode) -> Self { |
18338 | Some(u32::from(input.0)) |
18339 | } |
18340 | } |
18341 | impl From<u8> for CoordMode { |
18342 | #[inline ] |
18343 | fn from(value: u8) -> Self { |
18344 | Self(value) |
18345 | } |
18346 | } |
18347 | impl core::fmt::Debug for CoordMode { |
18348 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
18349 | let variants: [(u32, &str, &str); 2] = [ |
18350 | (Self::ORIGIN.0.into(), "ORIGIN" , "Origin" ), |
18351 | (Self::PREVIOUS.0.into(), "PREVIOUS" , "Previous" ), |
18352 | ]; |
18353 | pretty_print_enum(fmt, self.0.into(), &variants) |
18354 | } |
18355 | } |
18356 | |
18357 | /// Opcode for the PolyPoint request |
18358 | pub const POLY_POINT_REQUEST: u8 = 64; |
18359 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
18360 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
18361 | pub struct PolyPointRequest<'input> { |
18362 | pub coordinate_mode: CoordMode, |
18363 | pub drawable: Drawable, |
18364 | pub gc: Gcontext, |
18365 | pub points: Cow<'input, [Point]>, |
18366 | } |
18367 | impl<'input> PolyPointRequest<'input> { |
18368 | /// Serialize this request into bytes for the provided connection |
18369 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
18370 | let length_so_far = 0; |
18371 | let coordinate_mode_bytes = u8::from(self.coordinate_mode).serialize(); |
18372 | let drawable_bytes = self.drawable.serialize(); |
18373 | let gc_bytes = self.gc.serialize(); |
18374 | let mut request0 = vec![ |
18375 | POLY_POINT_REQUEST, |
18376 | coordinate_mode_bytes[0], |
18377 | 0, |
18378 | 0, |
18379 | drawable_bytes[0], |
18380 | drawable_bytes[1], |
18381 | drawable_bytes[2], |
18382 | drawable_bytes[3], |
18383 | gc_bytes[0], |
18384 | gc_bytes[1], |
18385 | gc_bytes[2], |
18386 | gc_bytes[3], |
18387 | ]; |
18388 | let length_so_far = length_so_far + request0.len(); |
18389 | let points_bytes = self.points.serialize(); |
18390 | let length_so_far = length_so_far + points_bytes.len(); |
18391 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
18392 | let length_so_far = length_so_far + padding0.len(); |
18393 | assert_eq!(length_so_far % 4, 0); |
18394 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
18395 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
18396 | (vec![request0.into(), points_bytes.into(), padding0.into()], vec![]) |
18397 | } |
18398 | /// Parse this request given its header, its body, and any fds that go along with it |
18399 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
18400 | if header.major_opcode != POLY_POINT_REQUEST { |
18401 | return Err(ParseError::InvalidValue); |
18402 | } |
18403 | let remaining = &[header.minor_opcode]; |
18404 | let (coordinate_mode, remaining) = u8::try_parse(remaining)?; |
18405 | let coordinate_mode = coordinate_mode.into(); |
18406 | let _ = remaining; |
18407 | let (drawable, remaining) = Drawable::try_parse(value)?; |
18408 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
18409 | let mut remaining = remaining; |
18410 | // Length is 'everything left in the input' |
18411 | let mut points = Vec::new(); |
18412 | while !remaining.is_empty() { |
18413 | let (v, new_remaining) = Point::try_parse(remaining)?; |
18414 | remaining = new_remaining; |
18415 | points.push(v); |
18416 | } |
18417 | let _ = remaining; |
18418 | Ok(PolyPointRequest { |
18419 | coordinate_mode, |
18420 | drawable, |
18421 | gc, |
18422 | points: Cow::Owned(points), |
18423 | }) |
18424 | } |
18425 | /// Clone all borrowed data in this PolyPointRequest. |
18426 | pub fn into_owned(self) -> PolyPointRequest<'static> { |
18427 | PolyPointRequest { |
18428 | coordinate_mode: self.coordinate_mode, |
18429 | drawable: self.drawable, |
18430 | gc: self.gc, |
18431 | points: Cow::Owned(self.points.into_owned()), |
18432 | } |
18433 | } |
18434 | } |
18435 | impl<'input> Request for PolyPointRequest<'input> { |
18436 | const EXTENSION_NAME: Option<&'static str> = None; |
18437 | |
18438 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
18439 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
18440 | // Flatten the buffers into a single vector |
18441 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
18442 | (buf, fds) |
18443 | } |
18444 | } |
18445 | impl<'input> crate::x11_utils::VoidRequest for PolyPointRequest<'input> { |
18446 | } |
18447 | |
18448 | /// Opcode for the PolyLine request |
18449 | pub const POLY_LINE_REQUEST: u8 = 65; |
18450 | /// draw lines. |
18451 | /// |
18452 | /// Draws `points_len`-1 lines between each pair of points (point[i], point[i+1]) |
18453 | /// in the `points` array. The lines are drawn in the order listed in the array. |
18454 | /// They join correctly at all intermediate points, and if the first and last |
18455 | /// points coincide, the first and last lines also join correctly. For any given |
18456 | /// line, a pixel is not drawn more than once. If thin (zero line-width) lines |
18457 | /// intersect, the intersecting pixels are drawn multiple times. If wide lines |
18458 | /// intersect, the intersecting pixels are drawn only once, as though the entire |
18459 | /// request were a single, filled shape. |
18460 | /// |
18461 | /// # Fields |
18462 | /// |
18463 | /// * `drawable` - The drawable to draw the line(s) on. |
18464 | /// * `gc` - The graphics context to use. |
18465 | /// * `points_len` - The number of `xcb_point_t` structures in `points`. |
18466 | /// * `points` - An array of points. |
18467 | /// * `coordinate_mode` - |
18468 | /// |
18469 | /// # Errors |
18470 | /// |
18471 | /// * `Drawable` - TODO: reasons? |
18472 | /// * `GContext` - TODO: reasons? |
18473 | /// * `Match` - TODO: reasons? |
18474 | /// * `Value` - TODO: reasons? |
18475 | /// |
18476 | /// # Example |
18477 | /// |
18478 | /// ```text |
18479 | /// /* |
18480 | /// * Draw a straight line. |
18481 | /// * |
18482 | /// */ |
18483 | /// void my_example(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc) { |
18484 | /// xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, drawable, gc, 2, |
18485 | /// (xcb_point_t[]) { {10, 10}, {100, 10} }); |
18486 | /// xcb_flush(conn); |
18487 | /// } |
18488 | /// ``` |
18489 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
18490 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
18491 | pub struct PolyLineRequest<'input> { |
18492 | pub coordinate_mode: CoordMode, |
18493 | pub drawable: Drawable, |
18494 | pub gc: Gcontext, |
18495 | pub points: Cow<'input, [Point]>, |
18496 | } |
18497 | impl<'input> PolyLineRequest<'input> { |
18498 | /// Serialize this request into bytes for the provided connection |
18499 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
18500 | let length_so_far = 0; |
18501 | let coordinate_mode_bytes = u8::from(self.coordinate_mode).serialize(); |
18502 | let drawable_bytes = self.drawable.serialize(); |
18503 | let gc_bytes = self.gc.serialize(); |
18504 | let mut request0 = vec![ |
18505 | POLY_LINE_REQUEST, |
18506 | coordinate_mode_bytes[0], |
18507 | 0, |
18508 | 0, |
18509 | drawable_bytes[0], |
18510 | drawable_bytes[1], |
18511 | drawable_bytes[2], |
18512 | drawable_bytes[3], |
18513 | gc_bytes[0], |
18514 | gc_bytes[1], |
18515 | gc_bytes[2], |
18516 | gc_bytes[3], |
18517 | ]; |
18518 | let length_so_far = length_so_far + request0.len(); |
18519 | let points_bytes = self.points.serialize(); |
18520 | let length_so_far = length_so_far + points_bytes.len(); |
18521 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
18522 | let length_so_far = length_so_far + padding0.len(); |
18523 | assert_eq!(length_so_far % 4, 0); |
18524 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
18525 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
18526 | (vec![request0.into(), points_bytes.into(), padding0.into()], vec![]) |
18527 | } |
18528 | /// Parse this request given its header, its body, and any fds that go along with it |
18529 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
18530 | if header.major_opcode != POLY_LINE_REQUEST { |
18531 | return Err(ParseError::InvalidValue); |
18532 | } |
18533 | let remaining = &[header.minor_opcode]; |
18534 | let (coordinate_mode, remaining) = u8::try_parse(remaining)?; |
18535 | let coordinate_mode = coordinate_mode.into(); |
18536 | let _ = remaining; |
18537 | let (drawable, remaining) = Drawable::try_parse(value)?; |
18538 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
18539 | let mut remaining = remaining; |
18540 | // Length is 'everything left in the input' |
18541 | let mut points = Vec::new(); |
18542 | while !remaining.is_empty() { |
18543 | let (v, new_remaining) = Point::try_parse(remaining)?; |
18544 | remaining = new_remaining; |
18545 | points.push(v); |
18546 | } |
18547 | let _ = remaining; |
18548 | Ok(PolyLineRequest { |
18549 | coordinate_mode, |
18550 | drawable, |
18551 | gc, |
18552 | points: Cow::Owned(points), |
18553 | }) |
18554 | } |
18555 | /// Clone all borrowed data in this PolyLineRequest. |
18556 | pub fn into_owned(self) -> PolyLineRequest<'static> { |
18557 | PolyLineRequest { |
18558 | coordinate_mode: self.coordinate_mode, |
18559 | drawable: self.drawable, |
18560 | gc: self.gc, |
18561 | points: Cow::Owned(self.points.into_owned()), |
18562 | } |
18563 | } |
18564 | } |
18565 | impl<'input> Request for PolyLineRequest<'input> { |
18566 | const EXTENSION_NAME: Option<&'static str> = None; |
18567 | |
18568 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
18569 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
18570 | // Flatten the buffers into a single vector |
18571 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
18572 | (buf, fds) |
18573 | } |
18574 | } |
18575 | impl<'input> crate::x11_utils::VoidRequest for PolyLineRequest<'input> { |
18576 | } |
18577 | |
18578 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
18579 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
18580 | pub struct Segment { |
18581 | pub x1: i16, |
18582 | pub y1: i16, |
18583 | pub x2: i16, |
18584 | pub y2: i16, |
18585 | } |
18586 | impl TryParse for Segment { |
18587 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
18588 | let (x1: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
18589 | let (y1: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
18590 | let (x2: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
18591 | let (y2: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
18592 | let result: Segment = Segment { x1, y1, x2, y2 }; |
18593 | Ok((result, remaining)) |
18594 | } |
18595 | } |
18596 | impl Serialize for Segment { |
18597 | type Bytes = [u8; 8]; |
18598 | fn serialize(&self) -> [u8; 8] { |
18599 | let x1_bytes = self.x1.serialize(); |
18600 | let y1_bytes = self.y1.serialize(); |
18601 | let x2_bytes = self.x2.serialize(); |
18602 | let y2_bytes = self.y2.serialize(); |
18603 | [ |
18604 | x1_bytes[0], |
18605 | x1_bytes[1], |
18606 | y1_bytes[0], |
18607 | y1_bytes[1], |
18608 | x2_bytes[0], |
18609 | x2_bytes[1], |
18610 | y2_bytes[0], |
18611 | y2_bytes[1], |
18612 | ] |
18613 | } |
18614 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
18615 | bytes.reserve(8); |
18616 | self.x1.serialize_into(bytes); |
18617 | self.y1.serialize_into(bytes); |
18618 | self.x2.serialize_into(bytes); |
18619 | self.y2.serialize_into(bytes); |
18620 | } |
18621 | } |
18622 | |
18623 | /// Opcode for the PolySegment request |
18624 | pub const POLY_SEGMENT_REQUEST: u8 = 66; |
18625 | /// draw lines. |
18626 | /// |
18627 | /// Draws multiple, unconnected lines. For each segment, a line is drawn between |
18628 | /// (x1, y1) and (x2, y2). The lines are drawn in the order listed in the array of |
18629 | /// `xcb_segment_t` structures and does not perform joining at coincident |
18630 | /// endpoints. For any given line, a pixel is not drawn more than once. If lines |
18631 | /// intersect, the intersecting pixels are drawn multiple times. |
18632 | /// |
18633 | /// TODO: include the xcb_segment_t data structure |
18634 | /// |
18635 | /// TODO: an example |
18636 | /// |
18637 | /// # Fields |
18638 | /// |
18639 | /// * `drawable` - A drawable (Window or Pixmap) to draw on. |
18640 | /// * `gc` - The graphics context to use. |
18641 | /// |
18642 | /// TODO: document which attributes of a gc are used |
18643 | /// * `segments_len` - The number of `xcb_segment_t` structures in `segments`. |
18644 | /// * `segments` - An array of `xcb_segment_t` structures. |
18645 | /// |
18646 | /// # Errors |
18647 | /// |
18648 | /// * `Drawable` - The specified `drawable` does not exist. |
18649 | /// * `GContext` - The specified `gc` does not exist. |
18650 | /// * `Match` - TODO: reasons? |
18651 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
18652 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
18653 | pub struct PolySegmentRequest<'input> { |
18654 | pub drawable: Drawable, |
18655 | pub gc: Gcontext, |
18656 | pub segments: Cow<'input, [Segment]>, |
18657 | } |
18658 | impl<'input> PolySegmentRequest<'input> { |
18659 | /// Serialize this request into bytes for the provided connection |
18660 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
18661 | let length_so_far = 0; |
18662 | let drawable_bytes = self.drawable.serialize(); |
18663 | let gc_bytes = self.gc.serialize(); |
18664 | let mut request0 = vec![ |
18665 | POLY_SEGMENT_REQUEST, |
18666 | 0, |
18667 | 0, |
18668 | 0, |
18669 | drawable_bytes[0], |
18670 | drawable_bytes[1], |
18671 | drawable_bytes[2], |
18672 | drawable_bytes[3], |
18673 | gc_bytes[0], |
18674 | gc_bytes[1], |
18675 | gc_bytes[2], |
18676 | gc_bytes[3], |
18677 | ]; |
18678 | let length_so_far = length_so_far + request0.len(); |
18679 | let segments_bytes = self.segments.serialize(); |
18680 | let length_so_far = length_so_far + segments_bytes.len(); |
18681 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
18682 | let length_so_far = length_so_far + padding0.len(); |
18683 | assert_eq!(length_so_far % 4, 0); |
18684 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
18685 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
18686 | (vec![request0.into(), segments_bytes.into(), padding0.into()], vec![]) |
18687 | } |
18688 | /// Parse this request given its header, its body, and any fds that go along with it |
18689 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
18690 | if header.major_opcode != POLY_SEGMENT_REQUEST { |
18691 | return Err(ParseError::InvalidValue); |
18692 | } |
18693 | let remaining = &[header.minor_opcode]; |
18694 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
18695 | let _ = remaining; |
18696 | let (drawable, remaining) = Drawable::try_parse(value)?; |
18697 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
18698 | let mut remaining = remaining; |
18699 | // Length is 'everything left in the input' |
18700 | let mut segments = Vec::new(); |
18701 | while !remaining.is_empty() { |
18702 | let (v, new_remaining) = Segment::try_parse(remaining)?; |
18703 | remaining = new_remaining; |
18704 | segments.push(v); |
18705 | } |
18706 | let _ = remaining; |
18707 | Ok(PolySegmentRequest { |
18708 | drawable, |
18709 | gc, |
18710 | segments: Cow::Owned(segments), |
18711 | }) |
18712 | } |
18713 | /// Clone all borrowed data in this PolySegmentRequest. |
18714 | pub fn into_owned(self) -> PolySegmentRequest<'static> { |
18715 | PolySegmentRequest { |
18716 | drawable: self.drawable, |
18717 | gc: self.gc, |
18718 | segments: Cow::Owned(self.segments.into_owned()), |
18719 | } |
18720 | } |
18721 | } |
18722 | impl<'input> Request for PolySegmentRequest<'input> { |
18723 | const EXTENSION_NAME: Option<&'static str> = None; |
18724 | |
18725 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
18726 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
18727 | // Flatten the buffers into a single vector |
18728 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
18729 | (buf, fds) |
18730 | } |
18731 | } |
18732 | impl<'input> crate::x11_utils::VoidRequest for PolySegmentRequest<'input> { |
18733 | } |
18734 | |
18735 | /// Opcode for the PolyRectangle request |
18736 | pub const POLY_RECTANGLE_REQUEST: u8 = 67; |
18737 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
18738 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
18739 | pub struct PolyRectangleRequest<'input> { |
18740 | pub drawable: Drawable, |
18741 | pub gc: Gcontext, |
18742 | pub rectangles: Cow<'input, [Rectangle]>, |
18743 | } |
18744 | impl<'input> PolyRectangleRequest<'input> { |
18745 | /// Serialize this request into bytes for the provided connection |
18746 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
18747 | let length_so_far = 0; |
18748 | let drawable_bytes = self.drawable.serialize(); |
18749 | let gc_bytes = self.gc.serialize(); |
18750 | let mut request0 = vec![ |
18751 | POLY_RECTANGLE_REQUEST, |
18752 | 0, |
18753 | 0, |
18754 | 0, |
18755 | drawable_bytes[0], |
18756 | drawable_bytes[1], |
18757 | drawable_bytes[2], |
18758 | drawable_bytes[3], |
18759 | gc_bytes[0], |
18760 | gc_bytes[1], |
18761 | gc_bytes[2], |
18762 | gc_bytes[3], |
18763 | ]; |
18764 | let length_so_far = length_so_far + request0.len(); |
18765 | let rectangles_bytes = self.rectangles.serialize(); |
18766 | let length_so_far = length_so_far + rectangles_bytes.len(); |
18767 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
18768 | let length_so_far = length_so_far + padding0.len(); |
18769 | assert_eq!(length_so_far % 4, 0); |
18770 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
18771 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
18772 | (vec![request0.into(), rectangles_bytes.into(), padding0.into()], vec![]) |
18773 | } |
18774 | /// Parse this request given its header, its body, and any fds that go along with it |
18775 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
18776 | if header.major_opcode != POLY_RECTANGLE_REQUEST { |
18777 | return Err(ParseError::InvalidValue); |
18778 | } |
18779 | let remaining = &[header.minor_opcode]; |
18780 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
18781 | let _ = remaining; |
18782 | let (drawable, remaining) = Drawable::try_parse(value)?; |
18783 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
18784 | let mut remaining = remaining; |
18785 | // Length is 'everything left in the input' |
18786 | let mut rectangles = Vec::new(); |
18787 | while !remaining.is_empty() { |
18788 | let (v, new_remaining) = Rectangle::try_parse(remaining)?; |
18789 | remaining = new_remaining; |
18790 | rectangles.push(v); |
18791 | } |
18792 | let _ = remaining; |
18793 | Ok(PolyRectangleRequest { |
18794 | drawable, |
18795 | gc, |
18796 | rectangles: Cow::Owned(rectangles), |
18797 | }) |
18798 | } |
18799 | /// Clone all borrowed data in this PolyRectangleRequest. |
18800 | pub fn into_owned(self) -> PolyRectangleRequest<'static> { |
18801 | PolyRectangleRequest { |
18802 | drawable: self.drawable, |
18803 | gc: self.gc, |
18804 | rectangles: Cow::Owned(self.rectangles.into_owned()), |
18805 | } |
18806 | } |
18807 | } |
18808 | impl<'input> Request for PolyRectangleRequest<'input> { |
18809 | const EXTENSION_NAME: Option<&'static str> = None; |
18810 | |
18811 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
18812 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
18813 | // Flatten the buffers into a single vector |
18814 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
18815 | (buf, fds) |
18816 | } |
18817 | } |
18818 | impl<'input> crate::x11_utils::VoidRequest for PolyRectangleRequest<'input> { |
18819 | } |
18820 | |
18821 | /// Opcode for the PolyArc request |
18822 | pub const POLY_ARC_REQUEST: u8 = 68; |
18823 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
18824 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
18825 | pub struct PolyArcRequest<'input> { |
18826 | pub drawable: Drawable, |
18827 | pub gc: Gcontext, |
18828 | pub arcs: Cow<'input, [Arc]>, |
18829 | } |
18830 | impl<'input> PolyArcRequest<'input> { |
18831 | /// Serialize this request into bytes for the provided connection |
18832 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
18833 | let length_so_far = 0; |
18834 | let drawable_bytes = self.drawable.serialize(); |
18835 | let gc_bytes = self.gc.serialize(); |
18836 | let mut request0 = vec![ |
18837 | POLY_ARC_REQUEST, |
18838 | 0, |
18839 | 0, |
18840 | 0, |
18841 | drawable_bytes[0], |
18842 | drawable_bytes[1], |
18843 | drawable_bytes[2], |
18844 | drawable_bytes[3], |
18845 | gc_bytes[0], |
18846 | gc_bytes[1], |
18847 | gc_bytes[2], |
18848 | gc_bytes[3], |
18849 | ]; |
18850 | let length_so_far = length_so_far + request0.len(); |
18851 | let arcs_bytes = self.arcs.serialize(); |
18852 | let length_so_far = length_so_far + arcs_bytes.len(); |
18853 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
18854 | let length_so_far = length_so_far + padding0.len(); |
18855 | assert_eq!(length_so_far % 4, 0); |
18856 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
18857 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
18858 | (vec![request0.into(), arcs_bytes.into(), padding0.into()], vec![]) |
18859 | } |
18860 | /// Parse this request given its header, its body, and any fds that go along with it |
18861 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
18862 | if header.major_opcode != POLY_ARC_REQUEST { |
18863 | return Err(ParseError::InvalidValue); |
18864 | } |
18865 | let remaining = &[header.minor_opcode]; |
18866 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
18867 | let _ = remaining; |
18868 | let (drawable, remaining) = Drawable::try_parse(value)?; |
18869 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
18870 | let mut remaining = remaining; |
18871 | // Length is 'everything left in the input' |
18872 | let mut arcs = Vec::new(); |
18873 | while !remaining.is_empty() { |
18874 | let (v, new_remaining) = Arc::try_parse(remaining)?; |
18875 | remaining = new_remaining; |
18876 | arcs.push(v); |
18877 | } |
18878 | let _ = remaining; |
18879 | Ok(PolyArcRequest { |
18880 | drawable, |
18881 | gc, |
18882 | arcs: Cow::Owned(arcs), |
18883 | }) |
18884 | } |
18885 | /// Clone all borrowed data in this PolyArcRequest. |
18886 | pub fn into_owned(self) -> PolyArcRequest<'static> { |
18887 | PolyArcRequest { |
18888 | drawable: self.drawable, |
18889 | gc: self.gc, |
18890 | arcs: Cow::Owned(self.arcs.into_owned()), |
18891 | } |
18892 | } |
18893 | } |
18894 | impl<'input> Request for PolyArcRequest<'input> { |
18895 | const EXTENSION_NAME: Option<&'static str> = None; |
18896 | |
18897 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
18898 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
18899 | // Flatten the buffers into a single vector |
18900 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
18901 | (buf, fds) |
18902 | } |
18903 | } |
18904 | impl<'input> crate::x11_utils::VoidRequest for PolyArcRequest<'input> { |
18905 | } |
18906 | |
18907 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
18908 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
18909 | pub struct PolyShape(u8); |
18910 | impl PolyShape { |
18911 | pub const COMPLEX: Self = Self(0); |
18912 | pub const NONCONVEX: Self = Self(1); |
18913 | pub const CONVEX: Self = Self(2); |
18914 | } |
18915 | impl From<PolyShape> for u8 { |
18916 | #[inline ] |
18917 | fn from(input: PolyShape) -> Self { |
18918 | input.0 |
18919 | } |
18920 | } |
18921 | impl From<PolyShape> for Option<u8> { |
18922 | #[inline ] |
18923 | fn from(input: PolyShape) -> Self { |
18924 | Some(input.0) |
18925 | } |
18926 | } |
18927 | impl From<PolyShape> for u16 { |
18928 | #[inline ] |
18929 | fn from(input: PolyShape) -> Self { |
18930 | u16::from(input.0) |
18931 | } |
18932 | } |
18933 | impl From<PolyShape> for Option<u16> { |
18934 | #[inline ] |
18935 | fn from(input: PolyShape) -> Self { |
18936 | Some(u16::from(input.0)) |
18937 | } |
18938 | } |
18939 | impl From<PolyShape> for u32 { |
18940 | #[inline ] |
18941 | fn from(input: PolyShape) -> Self { |
18942 | u32::from(input.0) |
18943 | } |
18944 | } |
18945 | impl From<PolyShape> for Option<u32> { |
18946 | #[inline ] |
18947 | fn from(input: PolyShape) -> Self { |
18948 | Some(u32::from(input.0)) |
18949 | } |
18950 | } |
18951 | impl From<u8> for PolyShape { |
18952 | #[inline ] |
18953 | fn from(value: u8) -> Self { |
18954 | Self(value) |
18955 | } |
18956 | } |
18957 | impl core::fmt::Debug for PolyShape { |
18958 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
18959 | let variants: [(u32, &str, &str); 3] = [ |
18960 | (Self::COMPLEX.0.into(), "COMPLEX" , "Complex" ), |
18961 | (Self::NONCONVEX.0.into(), "NONCONVEX" , "Nonconvex" ), |
18962 | (Self::CONVEX.0.into(), "CONVEX" , "Convex" ), |
18963 | ]; |
18964 | pretty_print_enum(fmt, self.0.into(), &variants) |
18965 | } |
18966 | } |
18967 | |
18968 | /// Opcode for the FillPoly request |
18969 | pub const FILL_POLY_REQUEST: u8 = 69; |
18970 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
18971 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
18972 | pub struct FillPolyRequest<'input> { |
18973 | pub drawable: Drawable, |
18974 | pub gc: Gcontext, |
18975 | pub shape: PolyShape, |
18976 | pub coordinate_mode: CoordMode, |
18977 | pub points: Cow<'input, [Point]>, |
18978 | } |
18979 | impl<'input> FillPolyRequest<'input> { |
18980 | /// Serialize this request into bytes for the provided connection |
18981 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
18982 | let length_so_far = 0; |
18983 | let drawable_bytes = self.drawable.serialize(); |
18984 | let gc_bytes = self.gc.serialize(); |
18985 | let shape_bytes = u8::from(self.shape).serialize(); |
18986 | let coordinate_mode_bytes = u8::from(self.coordinate_mode).serialize(); |
18987 | let mut request0 = vec![ |
18988 | FILL_POLY_REQUEST, |
18989 | 0, |
18990 | 0, |
18991 | 0, |
18992 | drawable_bytes[0], |
18993 | drawable_bytes[1], |
18994 | drawable_bytes[2], |
18995 | drawable_bytes[3], |
18996 | gc_bytes[0], |
18997 | gc_bytes[1], |
18998 | gc_bytes[2], |
18999 | gc_bytes[3], |
19000 | shape_bytes[0], |
19001 | coordinate_mode_bytes[0], |
19002 | 0, |
19003 | 0, |
19004 | ]; |
19005 | let length_so_far = length_so_far + request0.len(); |
19006 | let points_bytes = self.points.serialize(); |
19007 | let length_so_far = length_so_far + points_bytes.len(); |
19008 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
19009 | let length_so_far = length_so_far + padding0.len(); |
19010 | assert_eq!(length_so_far % 4, 0); |
19011 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
19012 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
19013 | (vec![request0.into(), points_bytes.into(), padding0.into()], vec![]) |
19014 | } |
19015 | /// Parse this request given its header, its body, and any fds that go along with it |
19016 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
19017 | if header.major_opcode != FILL_POLY_REQUEST { |
19018 | return Err(ParseError::InvalidValue); |
19019 | } |
19020 | let remaining = &[header.minor_opcode]; |
19021 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
19022 | let _ = remaining; |
19023 | let (drawable, remaining) = Drawable::try_parse(value)?; |
19024 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
19025 | let (shape, remaining) = u8::try_parse(remaining)?; |
19026 | let shape = shape.into(); |
19027 | let (coordinate_mode, remaining) = u8::try_parse(remaining)?; |
19028 | let coordinate_mode = coordinate_mode.into(); |
19029 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
19030 | let mut remaining = remaining; |
19031 | // Length is 'everything left in the input' |
19032 | let mut points = Vec::new(); |
19033 | while !remaining.is_empty() { |
19034 | let (v, new_remaining) = Point::try_parse(remaining)?; |
19035 | remaining = new_remaining; |
19036 | points.push(v); |
19037 | } |
19038 | let _ = remaining; |
19039 | Ok(FillPolyRequest { |
19040 | drawable, |
19041 | gc, |
19042 | shape, |
19043 | coordinate_mode, |
19044 | points: Cow::Owned(points), |
19045 | }) |
19046 | } |
19047 | /// Clone all borrowed data in this FillPolyRequest. |
19048 | pub fn into_owned(self) -> FillPolyRequest<'static> { |
19049 | FillPolyRequest { |
19050 | drawable: self.drawable, |
19051 | gc: self.gc, |
19052 | shape: self.shape, |
19053 | coordinate_mode: self.coordinate_mode, |
19054 | points: Cow::Owned(self.points.into_owned()), |
19055 | } |
19056 | } |
19057 | } |
19058 | impl<'input> Request for FillPolyRequest<'input> { |
19059 | const EXTENSION_NAME: Option<&'static str> = None; |
19060 | |
19061 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
19062 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
19063 | // Flatten the buffers into a single vector |
19064 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
19065 | (buf, fds) |
19066 | } |
19067 | } |
19068 | impl<'input> crate::x11_utils::VoidRequest for FillPolyRequest<'input> { |
19069 | } |
19070 | |
19071 | /// Opcode for the PolyFillRectangle request |
19072 | pub const POLY_FILL_RECTANGLE_REQUEST: u8 = 70; |
19073 | /// Fills rectangles. |
19074 | /// |
19075 | /// Fills the specified rectangle(s) in the order listed in the array. For any |
19076 | /// given rectangle, each pixel is not drawn more than once. If rectangles |
19077 | /// intersect, the intersecting pixels are drawn multiple times. |
19078 | /// |
19079 | /// # Fields |
19080 | /// |
19081 | /// * `drawable` - The drawable (Window or Pixmap) to draw on. |
19082 | /// * `gc` - The graphics context to use. |
19083 | /// |
19084 | /// The following graphics context components are used: function, plane-mask, |
19085 | /// fill-style, subwindow-mode, clip-x-origin, clip-y-origin, and clip-mask. |
19086 | /// |
19087 | /// The following graphics context mode-dependent components are used: |
19088 | /// foreground, background, tile, stipple, tile-stipple-x-origin, and |
19089 | /// tile-stipple-y-origin. |
19090 | /// * `rectangles_len` - The number of `xcb_rectangle_t` structures in `rectangles`. |
19091 | /// * `rectangles` - The rectangles to fill. |
19092 | /// |
19093 | /// # Errors |
19094 | /// |
19095 | /// * `Drawable` - The specified `drawable` (Window or Pixmap) does not exist. |
19096 | /// * `GContext` - The specified graphics context does not exist. |
19097 | /// * `Match` - TODO: reasons? |
19098 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
19099 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
19100 | pub struct PolyFillRectangleRequest<'input> { |
19101 | pub drawable: Drawable, |
19102 | pub gc: Gcontext, |
19103 | pub rectangles: Cow<'input, [Rectangle]>, |
19104 | } |
19105 | impl<'input> PolyFillRectangleRequest<'input> { |
19106 | /// Serialize this request into bytes for the provided connection |
19107 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
19108 | let length_so_far = 0; |
19109 | let drawable_bytes = self.drawable.serialize(); |
19110 | let gc_bytes = self.gc.serialize(); |
19111 | let mut request0 = vec![ |
19112 | POLY_FILL_RECTANGLE_REQUEST, |
19113 | 0, |
19114 | 0, |
19115 | 0, |
19116 | drawable_bytes[0], |
19117 | drawable_bytes[1], |
19118 | drawable_bytes[2], |
19119 | drawable_bytes[3], |
19120 | gc_bytes[0], |
19121 | gc_bytes[1], |
19122 | gc_bytes[2], |
19123 | gc_bytes[3], |
19124 | ]; |
19125 | let length_so_far = length_so_far + request0.len(); |
19126 | let rectangles_bytes = self.rectangles.serialize(); |
19127 | let length_so_far = length_so_far + rectangles_bytes.len(); |
19128 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
19129 | let length_so_far = length_so_far + padding0.len(); |
19130 | assert_eq!(length_so_far % 4, 0); |
19131 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
19132 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
19133 | (vec![request0.into(), rectangles_bytes.into(), padding0.into()], vec![]) |
19134 | } |
19135 | /// Parse this request given its header, its body, and any fds that go along with it |
19136 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
19137 | if header.major_opcode != POLY_FILL_RECTANGLE_REQUEST { |
19138 | return Err(ParseError::InvalidValue); |
19139 | } |
19140 | let remaining = &[header.minor_opcode]; |
19141 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
19142 | let _ = remaining; |
19143 | let (drawable, remaining) = Drawable::try_parse(value)?; |
19144 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
19145 | let mut remaining = remaining; |
19146 | // Length is 'everything left in the input' |
19147 | let mut rectangles = Vec::new(); |
19148 | while !remaining.is_empty() { |
19149 | let (v, new_remaining) = Rectangle::try_parse(remaining)?; |
19150 | remaining = new_remaining; |
19151 | rectangles.push(v); |
19152 | } |
19153 | let _ = remaining; |
19154 | Ok(PolyFillRectangleRequest { |
19155 | drawable, |
19156 | gc, |
19157 | rectangles: Cow::Owned(rectangles), |
19158 | }) |
19159 | } |
19160 | /// Clone all borrowed data in this PolyFillRectangleRequest. |
19161 | pub fn into_owned(self) -> PolyFillRectangleRequest<'static> { |
19162 | PolyFillRectangleRequest { |
19163 | drawable: self.drawable, |
19164 | gc: self.gc, |
19165 | rectangles: Cow::Owned(self.rectangles.into_owned()), |
19166 | } |
19167 | } |
19168 | } |
19169 | impl<'input> Request for PolyFillRectangleRequest<'input> { |
19170 | const EXTENSION_NAME: Option<&'static str> = None; |
19171 | |
19172 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
19173 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
19174 | // Flatten the buffers into a single vector |
19175 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
19176 | (buf, fds) |
19177 | } |
19178 | } |
19179 | impl<'input> crate::x11_utils::VoidRequest for PolyFillRectangleRequest<'input> { |
19180 | } |
19181 | |
19182 | /// Opcode for the PolyFillArc request |
19183 | pub const POLY_FILL_ARC_REQUEST: u8 = 71; |
19184 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
19185 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
19186 | pub struct PolyFillArcRequest<'input> { |
19187 | pub drawable: Drawable, |
19188 | pub gc: Gcontext, |
19189 | pub arcs: Cow<'input, [Arc]>, |
19190 | } |
19191 | impl<'input> PolyFillArcRequest<'input> { |
19192 | /// Serialize this request into bytes for the provided connection |
19193 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
19194 | let length_so_far = 0; |
19195 | let drawable_bytes = self.drawable.serialize(); |
19196 | let gc_bytes = self.gc.serialize(); |
19197 | let mut request0 = vec![ |
19198 | POLY_FILL_ARC_REQUEST, |
19199 | 0, |
19200 | 0, |
19201 | 0, |
19202 | drawable_bytes[0], |
19203 | drawable_bytes[1], |
19204 | drawable_bytes[2], |
19205 | drawable_bytes[3], |
19206 | gc_bytes[0], |
19207 | gc_bytes[1], |
19208 | gc_bytes[2], |
19209 | gc_bytes[3], |
19210 | ]; |
19211 | let length_so_far = length_so_far + request0.len(); |
19212 | let arcs_bytes = self.arcs.serialize(); |
19213 | let length_so_far = length_so_far + arcs_bytes.len(); |
19214 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
19215 | let length_so_far = length_so_far + padding0.len(); |
19216 | assert_eq!(length_so_far % 4, 0); |
19217 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
19218 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
19219 | (vec![request0.into(), arcs_bytes.into(), padding0.into()], vec![]) |
19220 | } |
19221 | /// Parse this request given its header, its body, and any fds that go along with it |
19222 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
19223 | if header.major_opcode != POLY_FILL_ARC_REQUEST { |
19224 | return Err(ParseError::InvalidValue); |
19225 | } |
19226 | let remaining = &[header.minor_opcode]; |
19227 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
19228 | let _ = remaining; |
19229 | let (drawable, remaining) = Drawable::try_parse(value)?; |
19230 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
19231 | let mut remaining = remaining; |
19232 | // Length is 'everything left in the input' |
19233 | let mut arcs = Vec::new(); |
19234 | while !remaining.is_empty() { |
19235 | let (v, new_remaining) = Arc::try_parse(remaining)?; |
19236 | remaining = new_remaining; |
19237 | arcs.push(v); |
19238 | } |
19239 | let _ = remaining; |
19240 | Ok(PolyFillArcRequest { |
19241 | drawable, |
19242 | gc, |
19243 | arcs: Cow::Owned(arcs), |
19244 | }) |
19245 | } |
19246 | /// Clone all borrowed data in this PolyFillArcRequest. |
19247 | pub fn into_owned(self) -> PolyFillArcRequest<'static> { |
19248 | PolyFillArcRequest { |
19249 | drawable: self.drawable, |
19250 | gc: self.gc, |
19251 | arcs: Cow::Owned(self.arcs.into_owned()), |
19252 | } |
19253 | } |
19254 | } |
19255 | impl<'input> Request for PolyFillArcRequest<'input> { |
19256 | const EXTENSION_NAME: Option<&'static str> = None; |
19257 | |
19258 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
19259 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
19260 | // Flatten the buffers into a single vector |
19261 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
19262 | (buf, fds) |
19263 | } |
19264 | } |
19265 | impl<'input> crate::x11_utils::VoidRequest for PolyFillArcRequest<'input> { |
19266 | } |
19267 | |
19268 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
19269 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
19270 | pub struct ImageFormat(u8); |
19271 | impl ImageFormat { |
19272 | pub const XY_BITMAP: Self = Self(0); |
19273 | pub const XY_PIXMAP: Self = Self(1); |
19274 | pub const Z_PIXMAP: Self = Self(2); |
19275 | } |
19276 | impl From<ImageFormat> for u8 { |
19277 | #[inline ] |
19278 | fn from(input: ImageFormat) -> Self { |
19279 | input.0 |
19280 | } |
19281 | } |
19282 | impl From<ImageFormat> for Option<u8> { |
19283 | #[inline ] |
19284 | fn from(input: ImageFormat) -> Self { |
19285 | Some(input.0) |
19286 | } |
19287 | } |
19288 | impl From<ImageFormat> for u16 { |
19289 | #[inline ] |
19290 | fn from(input: ImageFormat) -> Self { |
19291 | u16::from(input.0) |
19292 | } |
19293 | } |
19294 | impl From<ImageFormat> for Option<u16> { |
19295 | #[inline ] |
19296 | fn from(input: ImageFormat) -> Self { |
19297 | Some(u16::from(input.0)) |
19298 | } |
19299 | } |
19300 | impl From<ImageFormat> for u32 { |
19301 | #[inline ] |
19302 | fn from(input: ImageFormat) -> Self { |
19303 | u32::from(input.0) |
19304 | } |
19305 | } |
19306 | impl From<ImageFormat> for Option<u32> { |
19307 | #[inline ] |
19308 | fn from(input: ImageFormat) -> Self { |
19309 | Some(u32::from(input.0)) |
19310 | } |
19311 | } |
19312 | impl From<u8> for ImageFormat { |
19313 | #[inline ] |
19314 | fn from(value: u8) -> Self { |
19315 | Self(value) |
19316 | } |
19317 | } |
19318 | impl core::fmt::Debug for ImageFormat { |
19319 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
19320 | let variants: [(u32, &str, &str); 3] = [ |
19321 | (Self::XY_BITMAP.0.into(), "XY_BITMAP" , "XYBitmap" ), |
19322 | (Self::XY_PIXMAP.0.into(), "XY_PIXMAP" , "XYPixmap" ), |
19323 | (Self::Z_PIXMAP.0.into(), "Z_PIXMAP" , "ZPixmap" ), |
19324 | ]; |
19325 | pretty_print_enum(fmt, self.0.into(), &variants) |
19326 | } |
19327 | } |
19328 | |
19329 | /// Opcode for the PutImage request |
19330 | pub const PUT_IMAGE_REQUEST: u8 = 72; |
19331 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
19332 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
19333 | pub struct PutImageRequest<'input> { |
19334 | pub format: ImageFormat, |
19335 | pub drawable: Drawable, |
19336 | pub gc: Gcontext, |
19337 | pub width: u16, |
19338 | pub height: u16, |
19339 | pub dst_x: i16, |
19340 | pub dst_y: i16, |
19341 | pub left_pad: u8, |
19342 | pub depth: u8, |
19343 | pub data: Cow<'input, [u8]>, |
19344 | } |
19345 | impl<'input> PutImageRequest<'input> { |
19346 | /// Serialize this request into bytes for the provided connection |
19347 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
19348 | let length_so_far = 0; |
19349 | let format_bytes = u8::from(self.format).serialize(); |
19350 | let drawable_bytes = self.drawable.serialize(); |
19351 | let gc_bytes = self.gc.serialize(); |
19352 | let width_bytes = self.width.serialize(); |
19353 | let height_bytes = self.height.serialize(); |
19354 | let dst_x_bytes = self.dst_x.serialize(); |
19355 | let dst_y_bytes = self.dst_y.serialize(); |
19356 | let left_pad_bytes = self.left_pad.serialize(); |
19357 | let depth_bytes = self.depth.serialize(); |
19358 | let mut request0 = vec![ |
19359 | PUT_IMAGE_REQUEST, |
19360 | format_bytes[0], |
19361 | 0, |
19362 | 0, |
19363 | drawable_bytes[0], |
19364 | drawable_bytes[1], |
19365 | drawable_bytes[2], |
19366 | drawable_bytes[3], |
19367 | gc_bytes[0], |
19368 | gc_bytes[1], |
19369 | gc_bytes[2], |
19370 | gc_bytes[3], |
19371 | width_bytes[0], |
19372 | width_bytes[1], |
19373 | height_bytes[0], |
19374 | height_bytes[1], |
19375 | dst_x_bytes[0], |
19376 | dst_x_bytes[1], |
19377 | dst_y_bytes[0], |
19378 | dst_y_bytes[1], |
19379 | left_pad_bytes[0], |
19380 | depth_bytes[0], |
19381 | 0, |
19382 | 0, |
19383 | ]; |
19384 | let length_so_far = length_so_far + request0.len(); |
19385 | let length_so_far = length_so_far + self.data.len(); |
19386 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
19387 | let length_so_far = length_so_far + padding0.len(); |
19388 | assert_eq!(length_so_far % 4, 0); |
19389 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
19390 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
19391 | (vec![request0.into(), self.data, padding0.into()], vec![]) |
19392 | } |
19393 | /// Parse this request given its header, its body, and any fds that go along with it |
19394 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
19395 | if header.major_opcode != PUT_IMAGE_REQUEST { |
19396 | return Err(ParseError::InvalidValue); |
19397 | } |
19398 | let remaining = &[header.minor_opcode]; |
19399 | let (format, remaining) = u8::try_parse(remaining)?; |
19400 | let format = format.into(); |
19401 | let _ = remaining; |
19402 | let (drawable, remaining) = Drawable::try_parse(value)?; |
19403 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
19404 | let (width, remaining) = u16::try_parse(remaining)?; |
19405 | let (height, remaining) = u16::try_parse(remaining)?; |
19406 | let (dst_x, remaining) = i16::try_parse(remaining)?; |
19407 | let (dst_y, remaining) = i16::try_parse(remaining)?; |
19408 | let (left_pad, remaining) = u8::try_parse(remaining)?; |
19409 | let (depth, remaining) = u8::try_parse(remaining)?; |
19410 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
19411 | let (data, remaining) = remaining.split_at(remaining.len()); |
19412 | let _ = remaining; |
19413 | Ok(PutImageRequest { |
19414 | format, |
19415 | drawable, |
19416 | gc, |
19417 | width, |
19418 | height, |
19419 | dst_x, |
19420 | dst_y, |
19421 | left_pad, |
19422 | depth, |
19423 | data: Cow::Borrowed(data), |
19424 | }) |
19425 | } |
19426 | /// Clone all borrowed data in this PutImageRequest. |
19427 | pub fn into_owned(self) -> PutImageRequest<'static> { |
19428 | PutImageRequest { |
19429 | format: self.format, |
19430 | drawable: self.drawable, |
19431 | gc: self.gc, |
19432 | width: self.width, |
19433 | height: self.height, |
19434 | dst_x: self.dst_x, |
19435 | dst_y: self.dst_y, |
19436 | left_pad: self.left_pad, |
19437 | depth: self.depth, |
19438 | data: Cow::Owned(self.data.into_owned()), |
19439 | } |
19440 | } |
19441 | } |
19442 | impl<'input> Request for PutImageRequest<'input> { |
19443 | const EXTENSION_NAME: Option<&'static str> = None; |
19444 | |
19445 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
19446 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
19447 | // Flatten the buffers into a single vector |
19448 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
19449 | (buf, fds) |
19450 | } |
19451 | } |
19452 | impl<'input> crate::x11_utils::VoidRequest for PutImageRequest<'input> { |
19453 | } |
19454 | |
19455 | /// Opcode for the GetImage request |
19456 | pub const GET_IMAGE_REQUEST: u8 = 73; |
19457 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
19458 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
19459 | pub struct GetImageRequest { |
19460 | pub format: ImageFormat, |
19461 | pub drawable: Drawable, |
19462 | pub x: i16, |
19463 | pub y: i16, |
19464 | pub width: u16, |
19465 | pub height: u16, |
19466 | pub plane_mask: u32, |
19467 | } |
19468 | impl GetImageRequest { |
19469 | /// Serialize this request into bytes for the provided connection |
19470 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
19471 | let length_so_far = 0; |
19472 | let format_bytes = u8::from(self.format).serialize(); |
19473 | let drawable_bytes = self.drawable.serialize(); |
19474 | let x_bytes = self.x.serialize(); |
19475 | let y_bytes = self.y.serialize(); |
19476 | let width_bytes = self.width.serialize(); |
19477 | let height_bytes = self.height.serialize(); |
19478 | let plane_mask_bytes = self.plane_mask.serialize(); |
19479 | let mut request0 = vec![ |
19480 | GET_IMAGE_REQUEST, |
19481 | format_bytes[0], |
19482 | 0, |
19483 | 0, |
19484 | drawable_bytes[0], |
19485 | drawable_bytes[1], |
19486 | drawable_bytes[2], |
19487 | drawable_bytes[3], |
19488 | x_bytes[0], |
19489 | x_bytes[1], |
19490 | y_bytes[0], |
19491 | y_bytes[1], |
19492 | width_bytes[0], |
19493 | width_bytes[1], |
19494 | height_bytes[0], |
19495 | height_bytes[1], |
19496 | plane_mask_bytes[0], |
19497 | plane_mask_bytes[1], |
19498 | plane_mask_bytes[2], |
19499 | plane_mask_bytes[3], |
19500 | ]; |
19501 | let length_so_far = length_so_far + request0.len(); |
19502 | assert_eq!(length_so_far % 4, 0); |
19503 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
19504 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
19505 | (vec![request0.into()], vec![]) |
19506 | } |
19507 | /// Parse this request given its header, its body, and any fds that go along with it |
19508 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
19509 | if header.major_opcode != GET_IMAGE_REQUEST { |
19510 | return Err(ParseError::InvalidValue); |
19511 | } |
19512 | let remaining = &[header.minor_opcode]; |
19513 | let (format, remaining) = u8::try_parse(remaining)?; |
19514 | let format = format.into(); |
19515 | let _ = remaining; |
19516 | let (drawable, remaining) = Drawable::try_parse(value)?; |
19517 | let (x, remaining) = i16::try_parse(remaining)?; |
19518 | let (y, remaining) = i16::try_parse(remaining)?; |
19519 | let (width, remaining) = u16::try_parse(remaining)?; |
19520 | let (height, remaining) = u16::try_parse(remaining)?; |
19521 | let (plane_mask, remaining) = u32::try_parse(remaining)?; |
19522 | let _ = remaining; |
19523 | Ok(GetImageRequest { |
19524 | format, |
19525 | drawable, |
19526 | x, |
19527 | y, |
19528 | width, |
19529 | height, |
19530 | plane_mask, |
19531 | }) |
19532 | } |
19533 | } |
19534 | impl Request for GetImageRequest { |
19535 | const EXTENSION_NAME: Option<&'static str> = None; |
19536 | |
19537 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
19538 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
19539 | // Flatten the buffers into a single vector |
19540 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
19541 | (buf, fds) |
19542 | } |
19543 | } |
19544 | impl crate::x11_utils::ReplyRequest for GetImageRequest { |
19545 | type Reply = GetImageReply; |
19546 | } |
19547 | |
19548 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
19549 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
19550 | pub struct GetImageReply { |
19551 | pub depth: u8, |
19552 | pub sequence: u16, |
19553 | pub visual: Visualid, |
19554 | pub data: Vec<u8>, |
19555 | } |
19556 | impl TryParse for GetImageReply { |
19557 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
19558 | let remaining: &[u8] = initial_value; |
19559 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
19560 | let (depth: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
19561 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
19562 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
19563 | let (visual: u32, remaining: &[u8]) = Visualid::try_parse(remaining)?; |
19564 | let remaining: &[u8] = remaining.get(20..).ok_or(err:ParseError::InsufficientData)?; |
19565 | let (data: &[u8], remaining: &[u8]) = crate::x11_utils::parse_u8_list(data:remaining, list_length:u32::from(length).checked_mul(4u32).ok_or(err:ParseError::InvalidExpression)?.try_to_usize()?)?; |
19566 | let data: Vec = data.to_vec(); |
19567 | if response_type != 1 { |
19568 | return Err(ParseError::InvalidValue); |
19569 | } |
19570 | let result: GetImageReply = GetImageReply { depth, sequence, visual, data }; |
19571 | let _ = remaining; |
19572 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
19573 | .ok_or(err:ParseError::InsufficientData)?; |
19574 | Ok((result, remaining)) |
19575 | } |
19576 | } |
19577 | impl Serialize for GetImageReply { |
19578 | type Bytes = Vec<u8>; |
19579 | fn serialize(&self) -> Vec<u8> { |
19580 | let mut result: Vec = Vec::new(); |
19581 | self.serialize_into(&mut result); |
19582 | result |
19583 | } |
19584 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
19585 | bytes.reserve(additional:32); |
19586 | let response_type_bytes: &[u8; 1] = &[1]; |
19587 | bytes.push(response_type_bytes[0]); |
19588 | self.depth.serialize_into(bytes); |
19589 | self.sequence.serialize_into(bytes); |
19590 | assert_eq!(self.data.len() % 4, 0, "`data` has an incorrect length, must be a multiple of 4" ); |
19591 | let length: u32 = u32::try_from(self.data.len() / 4).expect(msg:"`data` has too many elements" ); |
19592 | length.serialize_into(bytes); |
19593 | self.visual.serialize_into(bytes); |
19594 | bytes.extend_from_slice(&[0; 20]); |
19595 | bytes.extend_from_slice(&self.data); |
19596 | } |
19597 | } |
19598 | impl GetImageReply { |
19599 | /// Get the value of the `length` field. |
19600 | /// |
19601 | /// The `length` field is used as the length field of the `data` field. |
19602 | /// This function computes the field's value again based on the length of the list. |
19603 | /// |
19604 | /// # Panics |
19605 | /// |
19606 | /// Panics if the value cannot be represented in the target type. This |
19607 | /// cannot happen with values of the struct received from the X11 server. |
19608 | pub fn length(&self) -> u32 { |
19609 | self.data.len() |
19610 | .checked_div(4).unwrap() |
19611 | .try_into().unwrap() |
19612 | } |
19613 | } |
19614 | |
19615 | /// Opcode for the PolyText8 request |
19616 | pub const POLY_TEXT8_REQUEST: u8 = 74; |
19617 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
19618 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
19619 | pub struct PolyText8Request<'input> { |
19620 | pub drawable: Drawable, |
19621 | pub gc: Gcontext, |
19622 | pub x: i16, |
19623 | pub y: i16, |
19624 | pub items: Cow<'input, [u8]>, |
19625 | } |
19626 | impl<'input> PolyText8Request<'input> { |
19627 | /// Serialize this request into bytes for the provided connection |
19628 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
19629 | let length_so_far = 0; |
19630 | let drawable_bytes = self.drawable.serialize(); |
19631 | let gc_bytes = self.gc.serialize(); |
19632 | let x_bytes = self.x.serialize(); |
19633 | let y_bytes = self.y.serialize(); |
19634 | let mut request0 = vec![ |
19635 | POLY_TEXT8_REQUEST, |
19636 | 0, |
19637 | 0, |
19638 | 0, |
19639 | drawable_bytes[0], |
19640 | drawable_bytes[1], |
19641 | drawable_bytes[2], |
19642 | drawable_bytes[3], |
19643 | gc_bytes[0], |
19644 | gc_bytes[1], |
19645 | gc_bytes[2], |
19646 | gc_bytes[3], |
19647 | x_bytes[0], |
19648 | x_bytes[1], |
19649 | y_bytes[0], |
19650 | y_bytes[1], |
19651 | ]; |
19652 | let length_so_far = length_so_far + request0.len(); |
19653 | let length_so_far = length_so_far + self.items.len(); |
19654 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
19655 | let length_so_far = length_so_far + padding0.len(); |
19656 | assert_eq!(length_so_far % 4, 0); |
19657 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
19658 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
19659 | (vec![request0.into(), self.items, padding0.into()], vec![]) |
19660 | } |
19661 | /// Parse this request given its header, its body, and any fds that go along with it |
19662 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
19663 | if header.major_opcode != POLY_TEXT8_REQUEST { |
19664 | return Err(ParseError::InvalidValue); |
19665 | } |
19666 | let remaining = &[header.minor_opcode]; |
19667 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
19668 | let _ = remaining; |
19669 | let (drawable, remaining) = Drawable::try_parse(value)?; |
19670 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
19671 | let (x, remaining) = i16::try_parse(remaining)?; |
19672 | let (y, remaining) = i16::try_parse(remaining)?; |
19673 | let (items, remaining) = remaining.split_at(remaining.len()); |
19674 | let _ = remaining; |
19675 | Ok(PolyText8Request { |
19676 | drawable, |
19677 | gc, |
19678 | x, |
19679 | y, |
19680 | items: Cow::Borrowed(items), |
19681 | }) |
19682 | } |
19683 | /// Clone all borrowed data in this PolyText8Request. |
19684 | pub fn into_owned(self) -> PolyText8Request<'static> { |
19685 | PolyText8Request { |
19686 | drawable: self.drawable, |
19687 | gc: self.gc, |
19688 | x: self.x, |
19689 | y: self.y, |
19690 | items: Cow::Owned(self.items.into_owned()), |
19691 | } |
19692 | } |
19693 | } |
19694 | impl<'input> Request for PolyText8Request<'input> { |
19695 | const EXTENSION_NAME: Option<&'static str> = None; |
19696 | |
19697 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
19698 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
19699 | // Flatten the buffers into a single vector |
19700 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
19701 | (buf, fds) |
19702 | } |
19703 | } |
19704 | impl<'input> crate::x11_utils::VoidRequest for PolyText8Request<'input> { |
19705 | } |
19706 | |
19707 | /// Opcode for the PolyText16 request |
19708 | pub const POLY_TEXT16_REQUEST: u8 = 75; |
19709 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
19710 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
19711 | pub struct PolyText16Request<'input> { |
19712 | pub drawable: Drawable, |
19713 | pub gc: Gcontext, |
19714 | pub x: i16, |
19715 | pub y: i16, |
19716 | pub items: Cow<'input, [u8]>, |
19717 | } |
19718 | impl<'input> PolyText16Request<'input> { |
19719 | /// Serialize this request into bytes for the provided connection |
19720 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
19721 | let length_so_far = 0; |
19722 | let drawable_bytes = self.drawable.serialize(); |
19723 | let gc_bytes = self.gc.serialize(); |
19724 | let x_bytes = self.x.serialize(); |
19725 | let y_bytes = self.y.serialize(); |
19726 | let mut request0 = vec![ |
19727 | POLY_TEXT16_REQUEST, |
19728 | 0, |
19729 | 0, |
19730 | 0, |
19731 | drawable_bytes[0], |
19732 | drawable_bytes[1], |
19733 | drawable_bytes[2], |
19734 | drawable_bytes[3], |
19735 | gc_bytes[0], |
19736 | gc_bytes[1], |
19737 | gc_bytes[2], |
19738 | gc_bytes[3], |
19739 | x_bytes[0], |
19740 | x_bytes[1], |
19741 | y_bytes[0], |
19742 | y_bytes[1], |
19743 | ]; |
19744 | let length_so_far = length_so_far + request0.len(); |
19745 | let length_so_far = length_so_far + self.items.len(); |
19746 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
19747 | let length_so_far = length_so_far + padding0.len(); |
19748 | assert_eq!(length_so_far % 4, 0); |
19749 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
19750 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
19751 | (vec![request0.into(), self.items, padding0.into()], vec![]) |
19752 | } |
19753 | /// Parse this request given its header, its body, and any fds that go along with it |
19754 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
19755 | if header.major_opcode != POLY_TEXT16_REQUEST { |
19756 | return Err(ParseError::InvalidValue); |
19757 | } |
19758 | let remaining = &[header.minor_opcode]; |
19759 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
19760 | let _ = remaining; |
19761 | let (drawable, remaining) = Drawable::try_parse(value)?; |
19762 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
19763 | let (x, remaining) = i16::try_parse(remaining)?; |
19764 | let (y, remaining) = i16::try_parse(remaining)?; |
19765 | let (items, remaining) = remaining.split_at(remaining.len()); |
19766 | let _ = remaining; |
19767 | Ok(PolyText16Request { |
19768 | drawable, |
19769 | gc, |
19770 | x, |
19771 | y, |
19772 | items: Cow::Borrowed(items), |
19773 | }) |
19774 | } |
19775 | /// Clone all borrowed data in this PolyText16Request. |
19776 | pub fn into_owned(self) -> PolyText16Request<'static> { |
19777 | PolyText16Request { |
19778 | drawable: self.drawable, |
19779 | gc: self.gc, |
19780 | x: self.x, |
19781 | y: self.y, |
19782 | items: Cow::Owned(self.items.into_owned()), |
19783 | } |
19784 | } |
19785 | } |
19786 | impl<'input> Request for PolyText16Request<'input> { |
19787 | const EXTENSION_NAME: Option<&'static str> = None; |
19788 | |
19789 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
19790 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
19791 | // Flatten the buffers into a single vector |
19792 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
19793 | (buf, fds) |
19794 | } |
19795 | } |
19796 | impl<'input> crate::x11_utils::VoidRequest for PolyText16Request<'input> { |
19797 | } |
19798 | |
19799 | /// Opcode for the ImageText8 request |
19800 | pub const IMAGE_TEXT8_REQUEST: u8 = 76; |
19801 | /// Draws text. |
19802 | /// |
19803 | /// Fills the destination rectangle with the background pixel from `gc`, then |
19804 | /// paints the text with the foreground pixel from `gc`. The upper-left corner of |
19805 | /// the filled rectangle is at [x, y - font-ascent]. The width is overall-width, |
19806 | /// the height is font-ascent + font-descent. The overall-width, font-ascent and |
19807 | /// font-descent are as returned by `xcb_query_text_extents` (TODO). |
19808 | /// |
19809 | /// Note that using X core fonts is deprecated (but still supported) in favor of |
19810 | /// client-side rendering using Xft. |
19811 | /// |
19812 | /// # Fields |
19813 | /// |
19814 | /// * `drawable` - The drawable (Window or Pixmap) to draw text on. |
19815 | /// * `string` - The string to draw. Only the first 255 characters are relevant due to the data |
19816 | /// type of `string_len`. |
19817 | /// * `x` - The x coordinate of the first character, relative to the origin of `drawable`. |
19818 | /// * `y` - The y coordinate of the first character, relative to the origin of `drawable`. |
19819 | /// * `gc` - The graphics context to use. |
19820 | /// |
19821 | /// The following graphics context components are used: plane-mask, foreground, |
19822 | /// background, font, subwindow-mode, clip-x-origin, clip-y-origin, and clip-mask. |
19823 | /// |
19824 | /// # Errors |
19825 | /// |
19826 | /// * `Drawable` - The specified `drawable` (Window or Pixmap) does not exist. |
19827 | /// * `GContext` - The specified graphics context does not exist. |
19828 | /// * `Match` - TODO: reasons? |
19829 | /// |
19830 | /// # See |
19831 | /// |
19832 | /// * `ImageText16`: request |
19833 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
19834 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
19835 | pub struct ImageText8Request<'input> { |
19836 | pub drawable: Drawable, |
19837 | pub gc: Gcontext, |
19838 | pub x: i16, |
19839 | pub y: i16, |
19840 | pub string: Cow<'input, [u8]>, |
19841 | } |
19842 | impl<'input> ImageText8Request<'input> { |
19843 | /// Serialize this request into bytes for the provided connection |
19844 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
19845 | let length_so_far = 0; |
19846 | let string_len = u8::try_from(self.string.len()).expect("`string` has too many elements" ); |
19847 | let string_len_bytes = string_len.serialize(); |
19848 | let drawable_bytes = self.drawable.serialize(); |
19849 | let gc_bytes = self.gc.serialize(); |
19850 | let x_bytes = self.x.serialize(); |
19851 | let y_bytes = self.y.serialize(); |
19852 | let mut request0 = vec![ |
19853 | IMAGE_TEXT8_REQUEST, |
19854 | string_len_bytes[0], |
19855 | 0, |
19856 | 0, |
19857 | drawable_bytes[0], |
19858 | drawable_bytes[1], |
19859 | drawable_bytes[2], |
19860 | drawable_bytes[3], |
19861 | gc_bytes[0], |
19862 | gc_bytes[1], |
19863 | gc_bytes[2], |
19864 | gc_bytes[3], |
19865 | x_bytes[0], |
19866 | x_bytes[1], |
19867 | y_bytes[0], |
19868 | y_bytes[1], |
19869 | ]; |
19870 | let length_so_far = length_so_far + request0.len(); |
19871 | let length_so_far = length_so_far + self.string.len(); |
19872 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
19873 | let length_so_far = length_so_far + padding0.len(); |
19874 | assert_eq!(length_so_far % 4, 0); |
19875 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
19876 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
19877 | (vec![request0.into(), self.string, padding0.into()], vec![]) |
19878 | } |
19879 | /// Parse this request given its header, its body, and any fds that go along with it |
19880 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
19881 | if header.major_opcode != IMAGE_TEXT8_REQUEST { |
19882 | return Err(ParseError::InvalidValue); |
19883 | } |
19884 | let remaining = &[header.minor_opcode]; |
19885 | let (string_len, remaining) = u8::try_parse(remaining)?; |
19886 | let _ = remaining; |
19887 | let (drawable, remaining) = Drawable::try_parse(value)?; |
19888 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
19889 | let (x, remaining) = i16::try_parse(remaining)?; |
19890 | let (y, remaining) = i16::try_parse(remaining)?; |
19891 | let (string, remaining) = crate::x11_utils::parse_u8_list(remaining, string_len.try_to_usize()?)?; |
19892 | let _ = remaining; |
19893 | Ok(ImageText8Request { |
19894 | drawable, |
19895 | gc, |
19896 | x, |
19897 | y, |
19898 | string: Cow::Borrowed(string), |
19899 | }) |
19900 | } |
19901 | /// Clone all borrowed data in this ImageText8Request. |
19902 | pub fn into_owned(self) -> ImageText8Request<'static> { |
19903 | ImageText8Request { |
19904 | drawable: self.drawable, |
19905 | gc: self.gc, |
19906 | x: self.x, |
19907 | y: self.y, |
19908 | string: Cow::Owned(self.string.into_owned()), |
19909 | } |
19910 | } |
19911 | } |
19912 | impl<'input> Request for ImageText8Request<'input> { |
19913 | const EXTENSION_NAME: Option<&'static str> = None; |
19914 | |
19915 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
19916 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
19917 | // Flatten the buffers into a single vector |
19918 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
19919 | (buf, fds) |
19920 | } |
19921 | } |
19922 | impl<'input> crate::x11_utils::VoidRequest for ImageText8Request<'input> { |
19923 | } |
19924 | |
19925 | /// Opcode for the ImageText16 request |
19926 | pub const IMAGE_TEXT16_REQUEST: u8 = 77; |
19927 | /// Draws text. |
19928 | /// |
19929 | /// Fills the destination rectangle with the background pixel from `gc`, then |
19930 | /// paints the text with the foreground pixel from `gc`. The upper-left corner of |
19931 | /// the filled rectangle is at [x, y - font-ascent]. The width is overall-width, |
19932 | /// the height is font-ascent + font-descent. The overall-width, font-ascent and |
19933 | /// font-descent are as returned by `xcb_query_text_extents` (TODO). |
19934 | /// |
19935 | /// Note that using X core fonts is deprecated (but still supported) in favor of |
19936 | /// client-side rendering using Xft. |
19937 | /// |
19938 | /// # Fields |
19939 | /// |
19940 | /// * `drawable` - The drawable (Window or Pixmap) to draw text on. |
19941 | /// * `string` - The string to draw. Only the first 255 characters are relevant due to the data |
19942 | /// type of `string_len`. Every character uses 2 bytes (hence the 16 in this |
19943 | /// request's name). |
19944 | /// * `x` - The x coordinate of the first character, relative to the origin of `drawable`. |
19945 | /// * `y` - The y coordinate of the first character, relative to the origin of `drawable`. |
19946 | /// * `gc` - The graphics context to use. |
19947 | /// |
19948 | /// The following graphics context components are used: plane-mask, foreground, |
19949 | /// background, font, subwindow-mode, clip-x-origin, clip-y-origin, and clip-mask. |
19950 | /// |
19951 | /// # Errors |
19952 | /// |
19953 | /// * `Drawable` - The specified `drawable` (Window or Pixmap) does not exist. |
19954 | /// * `GContext` - The specified graphics context does not exist. |
19955 | /// * `Match` - TODO: reasons? |
19956 | /// |
19957 | /// # See |
19958 | /// |
19959 | /// * `ImageText8`: request |
19960 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
19961 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
19962 | pub struct ImageText16Request<'input> { |
19963 | pub drawable: Drawable, |
19964 | pub gc: Gcontext, |
19965 | pub x: i16, |
19966 | pub y: i16, |
19967 | pub string: Cow<'input, [Char2b]>, |
19968 | } |
19969 | impl<'input> ImageText16Request<'input> { |
19970 | /// Serialize this request into bytes for the provided connection |
19971 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
19972 | let length_so_far = 0; |
19973 | let string_len = u8::try_from(self.string.len()).expect("`string` has too many elements" ); |
19974 | let string_len_bytes = string_len.serialize(); |
19975 | let drawable_bytes = self.drawable.serialize(); |
19976 | let gc_bytes = self.gc.serialize(); |
19977 | let x_bytes = self.x.serialize(); |
19978 | let y_bytes = self.y.serialize(); |
19979 | let mut request0 = vec![ |
19980 | IMAGE_TEXT16_REQUEST, |
19981 | string_len_bytes[0], |
19982 | 0, |
19983 | 0, |
19984 | drawable_bytes[0], |
19985 | drawable_bytes[1], |
19986 | drawable_bytes[2], |
19987 | drawable_bytes[3], |
19988 | gc_bytes[0], |
19989 | gc_bytes[1], |
19990 | gc_bytes[2], |
19991 | gc_bytes[3], |
19992 | x_bytes[0], |
19993 | x_bytes[1], |
19994 | y_bytes[0], |
19995 | y_bytes[1], |
19996 | ]; |
19997 | let length_so_far = length_so_far + request0.len(); |
19998 | let string_bytes = self.string.serialize(); |
19999 | let length_so_far = length_so_far + string_bytes.len(); |
20000 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
20001 | let length_so_far = length_so_far + padding0.len(); |
20002 | assert_eq!(length_so_far % 4, 0); |
20003 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
20004 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
20005 | (vec![request0.into(), string_bytes.into(), padding0.into()], vec![]) |
20006 | } |
20007 | /// Parse this request given its header, its body, and any fds that go along with it |
20008 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
20009 | if header.major_opcode != IMAGE_TEXT16_REQUEST { |
20010 | return Err(ParseError::InvalidValue); |
20011 | } |
20012 | let remaining = &[header.minor_opcode]; |
20013 | let (string_len, remaining) = u8::try_parse(remaining)?; |
20014 | let _ = remaining; |
20015 | let (drawable, remaining) = Drawable::try_parse(value)?; |
20016 | let (gc, remaining) = Gcontext::try_parse(remaining)?; |
20017 | let (x, remaining) = i16::try_parse(remaining)?; |
20018 | let (y, remaining) = i16::try_parse(remaining)?; |
20019 | let (string, remaining) = crate::x11_utils::parse_list::<Char2b>(remaining, string_len.try_to_usize()?)?; |
20020 | let _ = remaining; |
20021 | Ok(ImageText16Request { |
20022 | drawable, |
20023 | gc, |
20024 | x, |
20025 | y, |
20026 | string: Cow::Owned(string), |
20027 | }) |
20028 | } |
20029 | /// Clone all borrowed data in this ImageText16Request. |
20030 | pub fn into_owned(self) -> ImageText16Request<'static> { |
20031 | ImageText16Request { |
20032 | drawable: self.drawable, |
20033 | gc: self.gc, |
20034 | x: self.x, |
20035 | y: self.y, |
20036 | string: Cow::Owned(self.string.into_owned()), |
20037 | } |
20038 | } |
20039 | } |
20040 | impl<'input> Request for ImageText16Request<'input> { |
20041 | const EXTENSION_NAME: Option<&'static str> = None; |
20042 | |
20043 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
20044 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
20045 | // Flatten the buffers into a single vector |
20046 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
20047 | (buf, fds) |
20048 | } |
20049 | } |
20050 | impl<'input> crate::x11_utils::VoidRequest for ImageText16Request<'input> { |
20051 | } |
20052 | |
20053 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20054 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20055 | pub struct ColormapAlloc(u8); |
20056 | impl ColormapAlloc { |
20057 | pub const NONE: Self = Self(0); |
20058 | pub const ALL: Self = Self(1); |
20059 | } |
20060 | impl From<ColormapAlloc> for u8 { |
20061 | #[inline ] |
20062 | fn from(input: ColormapAlloc) -> Self { |
20063 | input.0 |
20064 | } |
20065 | } |
20066 | impl From<ColormapAlloc> for Option<u8> { |
20067 | #[inline ] |
20068 | fn from(input: ColormapAlloc) -> Self { |
20069 | Some(input.0) |
20070 | } |
20071 | } |
20072 | impl From<ColormapAlloc> for u16 { |
20073 | #[inline ] |
20074 | fn from(input: ColormapAlloc) -> Self { |
20075 | u16::from(input.0) |
20076 | } |
20077 | } |
20078 | impl From<ColormapAlloc> for Option<u16> { |
20079 | #[inline ] |
20080 | fn from(input: ColormapAlloc) -> Self { |
20081 | Some(u16::from(input.0)) |
20082 | } |
20083 | } |
20084 | impl From<ColormapAlloc> for u32 { |
20085 | #[inline ] |
20086 | fn from(input: ColormapAlloc) -> Self { |
20087 | u32::from(input.0) |
20088 | } |
20089 | } |
20090 | impl From<ColormapAlloc> for Option<u32> { |
20091 | #[inline ] |
20092 | fn from(input: ColormapAlloc) -> Self { |
20093 | Some(u32::from(input.0)) |
20094 | } |
20095 | } |
20096 | impl From<u8> for ColormapAlloc { |
20097 | #[inline ] |
20098 | fn from(value: u8) -> Self { |
20099 | Self(value) |
20100 | } |
20101 | } |
20102 | impl core::fmt::Debug for ColormapAlloc { |
20103 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
20104 | let variants: [(u32, &str, &str); 2] = [ |
20105 | (Self::NONE.0.into(), "NONE" , "None" ), |
20106 | (Self::ALL.0.into(), "ALL" , "All" ), |
20107 | ]; |
20108 | pretty_print_enum(fmt, self.0.into(), &variants) |
20109 | } |
20110 | } |
20111 | |
20112 | /// Opcode for the CreateColormap request |
20113 | pub const CREATE_COLORMAP_REQUEST: u8 = 78; |
20114 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20115 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20116 | pub struct CreateColormapRequest { |
20117 | pub alloc: ColormapAlloc, |
20118 | pub mid: Colormap, |
20119 | pub window: Window, |
20120 | pub visual: Visualid, |
20121 | } |
20122 | impl CreateColormapRequest { |
20123 | /// Serialize this request into bytes for the provided connection |
20124 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
20125 | let length_so_far = 0; |
20126 | let alloc_bytes = u8::from(self.alloc).serialize(); |
20127 | let mid_bytes = self.mid.serialize(); |
20128 | let window_bytes = self.window.serialize(); |
20129 | let visual_bytes = self.visual.serialize(); |
20130 | let mut request0 = vec![ |
20131 | CREATE_COLORMAP_REQUEST, |
20132 | alloc_bytes[0], |
20133 | 0, |
20134 | 0, |
20135 | mid_bytes[0], |
20136 | mid_bytes[1], |
20137 | mid_bytes[2], |
20138 | mid_bytes[3], |
20139 | window_bytes[0], |
20140 | window_bytes[1], |
20141 | window_bytes[2], |
20142 | window_bytes[3], |
20143 | visual_bytes[0], |
20144 | visual_bytes[1], |
20145 | visual_bytes[2], |
20146 | visual_bytes[3], |
20147 | ]; |
20148 | let length_so_far = length_so_far + request0.len(); |
20149 | assert_eq!(length_so_far % 4, 0); |
20150 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
20151 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
20152 | (vec![request0.into()], vec![]) |
20153 | } |
20154 | /// Parse this request given its header, its body, and any fds that go along with it |
20155 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
20156 | if header.major_opcode != CREATE_COLORMAP_REQUEST { |
20157 | return Err(ParseError::InvalidValue); |
20158 | } |
20159 | let remaining = &[header.minor_opcode]; |
20160 | let (alloc, remaining) = u8::try_parse(remaining)?; |
20161 | let alloc = alloc.into(); |
20162 | let _ = remaining; |
20163 | let (mid, remaining) = Colormap::try_parse(value)?; |
20164 | let (window, remaining) = Window::try_parse(remaining)?; |
20165 | let (visual, remaining) = Visualid::try_parse(remaining)?; |
20166 | let _ = remaining; |
20167 | Ok(CreateColormapRequest { |
20168 | alloc, |
20169 | mid, |
20170 | window, |
20171 | visual, |
20172 | }) |
20173 | } |
20174 | } |
20175 | impl Request for CreateColormapRequest { |
20176 | const EXTENSION_NAME: Option<&'static str> = None; |
20177 | |
20178 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
20179 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
20180 | // Flatten the buffers into a single vector |
20181 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
20182 | (buf, fds) |
20183 | } |
20184 | } |
20185 | impl crate::x11_utils::VoidRequest for CreateColormapRequest { |
20186 | } |
20187 | |
20188 | /// Opcode for the FreeColormap request |
20189 | pub const FREE_COLORMAP_REQUEST: u8 = 79; |
20190 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20191 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20192 | pub struct FreeColormapRequest { |
20193 | pub cmap: Colormap, |
20194 | } |
20195 | impl FreeColormapRequest { |
20196 | /// Serialize this request into bytes for the provided connection |
20197 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
20198 | let length_so_far = 0; |
20199 | let cmap_bytes = self.cmap.serialize(); |
20200 | let mut request0 = vec![ |
20201 | FREE_COLORMAP_REQUEST, |
20202 | 0, |
20203 | 0, |
20204 | 0, |
20205 | cmap_bytes[0], |
20206 | cmap_bytes[1], |
20207 | cmap_bytes[2], |
20208 | cmap_bytes[3], |
20209 | ]; |
20210 | let length_so_far = length_so_far + request0.len(); |
20211 | assert_eq!(length_so_far % 4, 0); |
20212 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
20213 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
20214 | (vec![request0.into()], vec![]) |
20215 | } |
20216 | /// Parse this request given its header, its body, and any fds that go along with it |
20217 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
20218 | if header.major_opcode != FREE_COLORMAP_REQUEST { |
20219 | return Err(ParseError::InvalidValue); |
20220 | } |
20221 | let remaining = &[header.minor_opcode]; |
20222 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
20223 | let _ = remaining; |
20224 | let (cmap, remaining) = Colormap::try_parse(value)?; |
20225 | let _ = remaining; |
20226 | Ok(FreeColormapRequest { |
20227 | cmap, |
20228 | }) |
20229 | } |
20230 | } |
20231 | impl Request for FreeColormapRequest { |
20232 | const EXTENSION_NAME: Option<&'static str> = None; |
20233 | |
20234 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
20235 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
20236 | // Flatten the buffers into a single vector |
20237 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
20238 | (buf, fds) |
20239 | } |
20240 | } |
20241 | impl crate::x11_utils::VoidRequest for FreeColormapRequest { |
20242 | } |
20243 | |
20244 | /// Opcode for the CopyColormapAndFree request |
20245 | pub const COPY_COLORMAP_AND_FREE_REQUEST: u8 = 80; |
20246 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20247 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20248 | pub struct CopyColormapAndFreeRequest { |
20249 | pub mid: Colormap, |
20250 | pub src_cmap: Colormap, |
20251 | } |
20252 | impl CopyColormapAndFreeRequest { |
20253 | /// Serialize this request into bytes for the provided connection |
20254 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
20255 | let length_so_far = 0; |
20256 | let mid_bytes = self.mid.serialize(); |
20257 | let src_cmap_bytes = self.src_cmap.serialize(); |
20258 | let mut request0 = vec![ |
20259 | COPY_COLORMAP_AND_FREE_REQUEST, |
20260 | 0, |
20261 | 0, |
20262 | 0, |
20263 | mid_bytes[0], |
20264 | mid_bytes[1], |
20265 | mid_bytes[2], |
20266 | mid_bytes[3], |
20267 | src_cmap_bytes[0], |
20268 | src_cmap_bytes[1], |
20269 | src_cmap_bytes[2], |
20270 | src_cmap_bytes[3], |
20271 | ]; |
20272 | let length_so_far = length_so_far + request0.len(); |
20273 | assert_eq!(length_so_far % 4, 0); |
20274 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
20275 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
20276 | (vec![request0.into()], vec![]) |
20277 | } |
20278 | /// Parse this request given its header, its body, and any fds that go along with it |
20279 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
20280 | if header.major_opcode != COPY_COLORMAP_AND_FREE_REQUEST { |
20281 | return Err(ParseError::InvalidValue); |
20282 | } |
20283 | let remaining = &[header.minor_opcode]; |
20284 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
20285 | let _ = remaining; |
20286 | let (mid, remaining) = Colormap::try_parse(value)?; |
20287 | let (src_cmap, remaining) = Colormap::try_parse(remaining)?; |
20288 | let _ = remaining; |
20289 | Ok(CopyColormapAndFreeRequest { |
20290 | mid, |
20291 | src_cmap, |
20292 | }) |
20293 | } |
20294 | } |
20295 | impl Request for CopyColormapAndFreeRequest { |
20296 | const EXTENSION_NAME: Option<&'static str> = None; |
20297 | |
20298 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
20299 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
20300 | // Flatten the buffers into a single vector |
20301 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
20302 | (buf, fds) |
20303 | } |
20304 | } |
20305 | impl crate::x11_utils::VoidRequest for CopyColormapAndFreeRequest { |
20306 | } |
20307 | |
20308 | /// Opcode for the InstallColormap request |
20309 | pub const INSTALL_COLORMAP_REQUEST: u8 = 81; |
20310 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20311 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20312 | pub struct InstallColormapRequest { |
20313 | pub cmap: Colormap, |
20314 | } |
20315 | impl InstallColormapRequest { |
20316 | /// Serialize this request into bytes for the provided connection |
20317 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
20318 | let length_so_far = 0; |
20319 | let cmap_bytes = self.cmap.serialize(); |
20320 | let mut request0 = vec![ |
20321 | INSTALL_COLORMAP_REQUEST, |
20322 | 0, |
20323 | 0, |
20324 | 0, |
20325 | cmap_bytes[0], |
20326 | cmap_bytes[1], |
20327 | cmap_bytes[2], |
20328 | cmap_bytes[3], |
20329 | ]; |
20330 | let length_so_far = length_so_far + request0.len(); |
20331 | assert_eq!(length_so_far % 4, 0); |
20332 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
20333 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
20334 | (vec![request0.into()], vec![]) |
20335 | } |
20336 | /// Parse this request given its header, its body, and any fds that go along with it |
20337 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
20338 | if header.major_opcode != INSTALL_COLORMAP_REQUEST { |
20339 | return Err(ParseError::InvalidValue); |
20340 | } |
20341 | let remaining = &[header.minor_opcode]; |
20342 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
20343 | let _ = remaining; |
20344 | let (cmap, remaining) = Colormap::try_parse(value)?; |
20345 | let _ = remaining; |
20346 | Ok(InstallColormapRequest { |
20347 | cmap, |
20348 | }) |
20349 | } |
20350 | } |
20351 | impl Request for InstallColormapRequest { |
20352 | const EXTENSION_NAME: Option<&'static str> = None; |
20353 | |
20354 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
20355 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
20356 | // Flatten the buffers into a single vector |
20357 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
20358 | (buf, fds) |
20359 | } |
20360 | } |
20361 | impl crate::x11_utils::VoidRequest for InstallColormapRequest { |
20362 | } |
20363 | |
20364 | /// Opcode for the UninstallColormap request |
20365 | pub const UNINSTALL_COLORMAP_REQUEST: u8 = 82; |
20366 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20367 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20368 | pub struct UninstallColormapRequest { |
20369 | pub cmap: Colormap, |
20370 | } |
20371 | impl UninstallColormapRequest { |
20372 | /// Serialize this request into bytes for the provided connection |
20373 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
20374 | let length_so_far = 0; |
20375 | let cmap_bytes = self.cmap.serialize(); |
20376 | let mut request0 = vec![ |
20377 | UNINSTALL_COLORMAP_REQUEST, |
20378 | 0, |
20379 | 0, |
20380 | 0, |
20381 | cmap_bytes[0], |
20382 | cmap_bytes[1], |
20383 | cmap_bytes[2], |
20384 | cmap_bytes[3], |
20385 | ]; |
20386 | let length_so_far = length_so_far + request0.len(); |
20387 | assert_eq!(length_so_far % 4, 0); |
20388 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
20389 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
20390 | (vec![request0.into()], vec![]) |
20391 | } |
20392 | /// Parse this request given its header, its body, and any fds that go along with it |
20393 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
20394 | if header.major_opcode != UNINSTALL_COLORMAP_REQUEST { |
20395 | return Err(ParseError::InvalidValue); |
20396 | } |
20397 | let remaining = &[header.minor_opcode]; |
20398 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
20399 | let _ = remaining; |
20400 | let (cmap, remaining) = Colormap::try_parse(value)?; |
20401 | let _ = remaining; |
20402 | Ok(UninstallColormapRequest { |
20403 | cmap, |
20404 | }) |
20405 | } |
20406 | } |
20407 | impl Request for UninstallColormapRequest { |
20408 | const EXTENSION_NAME: Option<&'static str> = None; |
20409 | |
20410 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
20411 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
20412 | // Flatten the buffers into a single vector |
20413 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
20414 | (buf, fds) |
20415 | } |
20416 | } |
20417 | impl crate::x11_utils::VoidRequest for UninstallColormapRequest { |
20418 | } |
20419 | |
20420 | /// Opcode for the ListInstalledColormaps request |
20421 | pub const LIST_INSTALLED_COLORMAPS_REQUEST: u8 = 83; |
20422 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20423 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20424 | pub struct ListInstalledColormapsRequest { |
20425 | pub window: Window, |
20426 | } |
20427 | impl ListInstalledColormapsRequest { |
20428 | /// Serialize this request into bytes for the provided connection |
20429 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
20430 | let length_so_far = 0; |
20431 | let window_bytes = self.window.serialize(); |
20432 | let mut request0 = vec![ |
20433 | LIST_INSTALLED_COLORMAPS_REQUEST, |
20434 | 0, |
20435 | 0, |
20436 | 0, |
20437 | window_bytes[0], |
20438 | window_bytes[1], |
20439 | window_bytes[2], |
20440 | window_bytes[3], |
20441 | ]; |
20442 | let length_so_far = length_so_far + request0.len(); |
20443 | assert_eq!(length_so_far % 4, 0); |
20444 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
20445 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
20446 | (vec![request0.into()], vec![]) |
20447 | } |
20448 | /// Parse this request given its header, its body, and any fds that go along with it |
20449 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
20450 | if header.major_opcode != LIST_INSTALLED_COLORMAPS_REQUEST { |
20451 | return Err(ParseError::InvalidValue); |
20452 | } |
20453 | let remaining = &[header.minor_opcode]; |
20454 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
20455 | let _ = remaining; |
20456 | let (window, remaining) = Window::try_parse(value)?; |
20457 | let _ = remaining; |
20458 | Ok(ListInstalledColormapsRequest { |
20459 | window, |
20460 | }) |
20461 | } |
20462 | } |
20463 | impl Request for ListInstalledColormapsRequest { |
20464 | const EXTENSION_NAME: Option<&'static str> = None; |
20465 | |
20466 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
20467 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
20468 | // Flatten the buffers into a single vector |
20469 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
20470 | (buf, fds) |
20471 | } |
20472 | } |
20473 | impl crate::x11_utils::ReplyRequest for ListInstalledColormapsRequest { |
20474 | type Reply = ListInstalledColormapsReply; |
20475 | } |
20476 | |
20477 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20478 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20479 | pub struct ListInstalledColormapsReply { |
20480 | pub sequence: u16, |
20481 | pub length: u32, |
20482 | pub cmaps: Vec<Colormap>, |
20483 | } |
20484 | impl TryParse for ListInstalledColormapsReply { |
20485 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
20486 | let remaining: &[u8] = initial_value; |
20487 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
20488 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
20489 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20490 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
20491 | let (cmaps_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20492 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
20493 | let (cmaps: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<Colormap>(data:remaining, list_length:cmaps_len.try_to_usize()?)?; |
20494 | if response_type != 1 { |
20495 | return Err(ParseError::InvalidValue); |
20496 | } |
20497 | let result: ListInstalledColormapsReply = ListInstalledColormapsReply { sequence, length, cmaps }; |
20498 | let _ = remaining; |
20499 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
20500 | .ok_or(err:ParseError::InsufficientData)?; |
20501 | Ok((result, remaining)) |
20502 | } |
20503 | } |
20504 | impl Serialize for ListInstalledColormapsReply { |
20505 | type Bytes = Vec<u8>; |
20506 | fn serialize(&self) -> Vec<u8> { |
20507 | let mut result: Vec = Vec::new(); |
20508 | self.serialize_into(&mut result); |
20509 | result |
20510 | } |
20511 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
20512 | bytes.reserve(additional:32); |
20513 | let response_type_bytes: &[u8; 1] = &[1]; |
20514 | bytes.push(response_type_bytes[0]); |
20515 | bytes.extend_from_slice(&[0; 1]); |
20516 | self.sequence.serialize_into(bytes); |
20517 | self.length.serialize_into(bytes); |
20518 | let cmaps_len: u16 = u16::try_from(self.cmaps.len()).expect(msg:"`cmaps` has too many elements" ); |
20519 | cmaps_len.serialize_into(bytes); |
20520 | bytes.extend_from_slice(&[0; 22]); |
20521 | self.cmaps.serialize_into(bytes); |
20522 | } |
20523 | } |
20524 | impl ListInstalledColormapsReply { |
20525 | /// Get the value of the `cmaps_len` field. |
20526 | /// |
20527 | /// The `cmaps_len` field is used as the length field of the `cmaps` field. |
20528 | /// This function computes the field's value again based on the length of the list. |
20529 | /// |
20530 | /// # Panics |
20531 | /// |
20532 | /// Panics if the value cannot be represented in the target type. This |
20533 | /// cannot happen with values of the struct received from the X11 server. |
20534 | pub fn cmaps_len(&self) -> u16 { |
20535 | self.cmaps.len() |
20536 | .try_into().unwrap() |
20537 | } |
20538 | } |
20539 | |
20540 | /// Opcode for the AllocColor request |
20541 | pub const ALLOC_COLOR_REQUEST: u8 = 84; |
20542 | /// Allocate a color. |
20543 | /// |
20544 | /// Allocates a read-only colormap entry corresponding to the closest RGB value |
20545 | /// supported by the hardware. If you are using TrueColor, you can take a shortcut |
20546 | /// and directly calculate the color pixel value to avoid the round trip. But, for |
20547 | /// example, on 16-bit color setups (VNC), you can easily get the closest supported |
20548 | /// RGB value to the RGB value you are specifying. |
20549 | /// |
20550 | /// # Fields |
20551 | /// |
20552 | /// * `cmap` - TODO |
20553 | /// * `red` - The red value of your color. |
20554 | /// * `green` - The green value of your color. |
20555 | /// * `blue` - The blue value of your color. |
20556 | /// |
20557 | /// # Errors |
20558 | /// |
20559 | /// * `Colormap` - The specified colormap `cmap` does not exist. |
20560 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20561 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20562 | pub struct AllocColorRequest { |
20563 | pub cmap: Colormap, |
20564 | pub red: u16, |
20565 | pub green: u16, |
20566 | pub blue: u16, |
20567 | } |
20568 | impl AllocColorRequest { |
20569 | /// Serialize this request into bytes for the provided connection |
20570 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
20571 | let length_so_far = 0; |
20572 | let cmap_bytes = self.cmap.serialize(); |
20573 | let red_bytes = self.red.serialize(); |
20574 | let green_bytes = self.green.serialize(); |
20575 | let blue_bytes = self.blue.serialize(); |
20576 | let mut request0 = vec![ |
20577 | ALLOC_COLOR_REQUEST, |
20578 | 0, |
20579 | 0, |
20580 | 0, |
20581 | cmap_bytes[0], |
20582 | cmap_bytes[1], |
20583 | cmap_bytes[2], |
20584 | cmap_bytes[3], |
20585 | red_bytes[0], |
20586 | red_bytes[1], |
20587 | green_bytes[0], |
20588 | green_bytes[1], |
20589 | blue_bytes[0], |
20590 | blue_bytes[1], |
20591 | 0, |
20592 | 0, |
20593 | ]; |
20594 | let length_so_far = length_so_far + request0.len(); |
20595 | assert_eq!(length_so_far % 4, 0); |
20596 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
20597 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
20598 | (vec![request0.into()], vec![]) |
20599 | } |
20600 | /// Parse this request given its header, its body, and any fds that go along with it |
20601 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
20602 | if header.major_opcode != ALLOC_COLOR_REQUEST { |
20603 | return Err(ParseError::InvalidValue); |
20604 | } |
20605 | let remaining = &[header.minor_opcode]; |
20606 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
20607 | let _ = remaining; |
20608 | let (cmap, remaining) = Colormap::try_parse(value)?; |
20609 | let (red, remaining) = u16::try_parse(remaining)?; |
20610 | let (green, remaining) = u16::try_parse(remaining)?; |
20611 | let (blue, remaining) = u16::try_parse(remaining)?; |
20612 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
20613 | let _ = remaining; |
20614 | Ok(AllocColorRequest { |
20615 | cmap, |
20616 | red, |
20617 | green, |
20618 | blue, |
20619 | }) |
20620 | } |
20621 | } |
20622 | impl Request for AllocColorRequest { |
20623 | const EXTENSION_NAME: Option<&'static str> = None; |
20624 | |
20625 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
20626 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
20627 | // Flatten the buffers into a single vector |
20628 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
20629 | (buf, fds) |
20630 | } |
20631 | } |
20632 | impl crate::x11_utils::ReplyRequest for AllocColorRequest { |
20633 | type Reply = AllocColorReply; |
20634 | } |
20635 | |
20636 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20637 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20638 | pub struct AllocColorReply { |
20639 | pub sequence: u16, |
20640 | pub length: u32, |
20641 | pub red: u16, |
20642 | pub green: u16, |
20643 | pub blue: u16, |
20644 | pub pixel: u32, |
20645 | } |
20646 | impl TryParse for AllocColorReply { |
20647 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
20648 | let remaining: &[u8] = initial_value; |
20649 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
20650 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
20651 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20652 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
20653 | let (red: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20654 | let (green: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20655 | let (blue: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20656 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
20657 | let (pixel: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
20658 | if response_type != 1 { |
20659 | return Err(ParseError::InvalidValue); |
20660 | } |
20661 | let result: AllocColorReply = AllocColorReply { sequence, length, red, green, blue, pixel }; |
20662 | let _ = remaining; |
20663 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
20664 | .ok_or(err:ParseError::InsufficientData)?; |
20665 | Ok((result, remaining)) |
20666 | } |
20667 | } |
20668 | impl Serialize for AllocColorReply { |
20669 | type Bytes = [u8; 20]; |
20670 | fn serialize(&self) -> [u8; 20] { |
20671 | let response_type_bytes = &[1]; |
20672 | let sequence_bytes = self.sequence.serialize(); |
20673 | let length_bytes = self.length.serialize(); |
20674 | let red_bytes = self.red.serialize(); |
20675 | let green_bytes = self.green.serialize(); |
20676 | let blue_bytes = self.blue.serialize(); |
20677 | let pixel_bytes = self.pixel.serialize(); |
20678 | [ |
20679 | response_type_bytes[0], |
20680 | 0, |
20681 | sequence_bytes[0], |
20682 | sequence_bytes[1], |
20683 | length_bytes[0], |
20684 | length_bytes[1], |
20685 | length_bytes[2], |
20686 | length_bytes[3], |
20687 | red_bytes[0], |
20688 | red_bytes[1], |
20689 | green_bytes[0], |
20690 | green_bytes[1], |
20691 | blue_bytes[0], |
20692 | blue_bytes[1], |
20693 | 0, |
20694 | 0, |
20695 | pixel_bytes[0], |
20696 | pixel_bytes[1], |
20697 | pixel_bytes[2], |
20698 | pixel_bytes[3], |
20699 | ] |
20700 | } |
20701 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
20702 | bytes.reserve(20); |
20703 | let response_type_bytes = &[1]; |
20704 | bytes.push(response_type_bytes[0]); |
20705 | bytes.extend_from_slice(&[0; 1]); |
20706 | self.sequence.serialize_into(bytes); |
20707 | self.length.serialize_into(bytes); |
20708 | self.red.serialize_into(bytes); |
20709 | self.green.serialize_into(bytes); |
20710 | self.blue.serialize_into(bytes); |
20711 | bytes.extend_from_slice(&[0; 2]); |
20712 | self.pixel.serialize_into(bytes); |
20713 | } |
20714 | } |
20715 | |
20716 | /// Opcode for the AllocNamedColor request |
20717 | pub const ALLOC_NAMED_COLOR_REQUEST: u8 = 85; |
20718 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20719 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20720 | pub struct AllocNamedColorRequest<'input> { |
20721 | pub cmap: Colormap, |
20722 | pub name: Cow<'input, [u8]>, |
20723 | } |
20724 | impl<'input> AllocNamedColorRequest<'input> { |
20725 | /// Serialize this request into bytes for the provided connection |
20726 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
20727 | let length_so_far = 0; |
20728 | let cmap_bytes = self.cmap.serialize(); |
20729 | let name_len = u16::try_from(self.name.len()).expect("`name` has too many elements" ); |
20730 | let name_len_bytes = name_len.serialize(); |
20731 | let mut request0 = vec![ |
20732 | ALLOC_NAMED_COLOR_REQUEST, |
20733 | 0, |
20734 | 0, |
20735 | 0, |
20736 | cmap_bytes[0], |
20737 | cmap_bytes[1], |
20738 | cmap_bytes[2], |
20739 | cmap_bytes[3], |
20740 | name_len_bytes[0], |
20741 | name_len_bytes[1], |
20742 | 0, |
20743 | 0, |
20744 | ]; |
20745 | let length_so_far = length_so_far + request0.len(); |
20746 | let length_so_far = length_so_far + self.name.len(); |
20747 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
20748 | let length_so_far = length_so_far + padding0.len(); |
20749 | assert_eq!(length_so_far % 4, 0); |
20750 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
20751 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
20752 | (vec![request0.into(), self.name, padding0.into()], vec![]) |
20753 | } |
20754 | /// Parse this request given its header, its body, and any fds that go along with it |
20755 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
20756 | if header.major_opcode != ALLOC_NAMED_COLOR_REQUEST { |
20757 | return Err(ParseError::InvalidValue); |
20758 | } |
20759 | let remaining = &[header.minor_opcode]; |
20760 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
20761 | let _ = remaining; |
20762 | let (cmap, remaining) = Colormap::try_parse(value)?; |
20763 | let (name_len, remaining) = u16::try_parse(remaining)?; |
20764 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
20765 | let (name, remaining) = crate::x11_utils::parse_u8_list(remaining, name_len.try_to_usize()?)?; |
20766 | let _ = remaining; |
20767 | Ok(AllocNamedColorRequest { |
20768 | cmap, |
20769 | name: Cow::Borrowed(name), |
20770 | }) |
20771 | } |
20772 | /// Clone all borrowed data in this AllocNamedColorRequest. |
20773 | pub fn into_owned(self) -> AllocNamedColorRequest<'static> { |
20774 | AllocNamedColorRequest { |
20775 | cmap: self.cmap, |
20776 | name: Cow::Owned(self.name.into_owned()), |
20777 | } |
20778 | } |
20779 | } |
20780 | impl<'input> Request for AllocNamedColorRequest<'input> { |
20781 | const EXTENSION_NAME: Option<&'static str> = None; |
20782 | |
20783 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
20784 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
20785 | // Flatten the buffers into a single vector |
20786 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
20787 | (buf, fds) |
20788 | } |
20789 | } |
20790 | impl<'input> crate::x11_utils::ReplyRequest for AllocNamedColorRequest<'input> { |
20791 | type Reply = AllocNamedColorReply; |
20792 | } |
20793 | |
20794 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20795 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20796 | pub struct AllocNamedColorReply { |
20797 | pub sequence: u16, |
20798 | pub length: u32, |
20799 | pub pixel: u32, |
20800 | pub exact_red: u16, |
20801 | pub exact_green: u16, |
20802 | pub exact_blue: u16, |
20803 | pub visual_red: u16, |
20804 | pub visual_green: u16, |
20805 | pub visual_blue: u16, |
20806 | } |
20807 | impl TryParse for AllocNamedColorReply { |
20808 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
20809 | let remaining: &[u8] = initial_value; |
20810 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
20811 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
20812 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20813 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
20814 | let (pixel: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
20815 | let (exact_red: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20816 | let (exact_green: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20817 | let (exact_blue: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20818 | let (visual_red: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20819 | let (visual_green: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20820 | let (visual_blue: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20821 | if response_type != 1 { |
20822 | return Err(ParseError::InvalidValue); |
20823 | } |
20824 | let result: AllocNamedColorReply = AllocNamedColorReply { sequence, length, pixel, exact_red, exact_green, exact_blue, visual_red, visual_green, visual_blue }; |
20825 | let _ = remaining; |
20826 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
20827 | .ok_or(err:ParseError::InsufficientData)?; |
20828 | Ok((result, remaining)) |
20829 | } |
20830 | } |
20831 | impl Serialize for AllocNamedColorReply { |
20832 | type Bytes = [u8; 24]; |
20833 | fn serialize(&self) -> [u8; 24] { |
20834 | let response_type_bytes = &[1]; |
20835 | let sequence_bytes = self.sequence.serialize(); |
20836 | let length_bytes = self.length.serialize(); |
20837 | let pixel_bytes = self.pixel.serialize(); |
20838 | let exact_red_bytes = self.exact_red.serialize(); |
20839 | let exact_green_bytes = self.exact_green.serialize(); |
20840 | let exact_blue_bytes = self.exact_blue.serialize(); |
20841 | let visual_red_bytes = self.visual_red.serialize(); |
20842 | let visual_green_bytes = self.visual_green.serialize(); |
20843 | let visual_blue_bytes = self.visual_blue.serialize(); |
20844 | [ |
20845 | response_type_bytes[0], |
20846 | 0, |
20847 | sequence_bytes[0], |
20848 | sequence_bytes[1], |
20849 | length_bytes[0], |
20850 | length_bytes[1], |
20851 | length_bytes[2], |
20852 | length_bytes[3], |
20853 | pixel_bytes[0], |
20854 | pixel_bytes[1], |
20855 | pixel_bytes[2], |
20856 | pixel_bytes[3], |
20857 | exact_red_bytes[0], |
20858 | exact_red_bytes[1], |
20859 | exact_green_bytes[0], |
20860 | exact_green_bytes[1], |
20861 | exact_blue_bytes[0], |
20862 | exact_blue_bytes[1], |
20863 | visual_red_bytes[0], |
20864 | visual_red_bytes[1], |
20865 | visual_green_bytes[0], |
20866 | visual_green_bytes[1], |
20867 | visual_blue_bytes[0], |
20868 | visual_blue_bytes[1], |
20869 | ] |
20870 | } |
20871 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
20872 | bytes.reserve(24); |
20873 | let response_type_bytes = &[1]; |
20874 | bytes.push(response_type_bytes[0]); |
20875 | bytes.extend_from_slice(&[0; 1]); |
20876 | self.sequence.serialize_into(bytes); |
20877 | self.length.serialize_into(bytes); |
20878 | self.pixel.serialize_into(bytes); |
20879 | self.exact_red.serialize_into(bytes); |
20880 | self.exact_green.serialize_into(bytes); |
20881 | self.exact_blue.serialize_into(bytes); |
20882 | self.visual_red.serialize_into(bytes); |
20883 | self.visual_green.serialize_into(bytes); |
20884 | self.visual_blue.serialize_into(bytes); |
20885 | } |
20886 | } |
20887 | |
20888 | /// Opcode for the AllocColorCells request |
20889 | pub const ALLOC_COLOR_CELLS_REQUEST: u8 = 86; |
20890 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20891 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20892 | pub struct AllocColorCellsRequest { |
20893 | pub contiguous: bool, |
20894 | pub cmap: Colormap, |
20895 | pub colors: u16, |
20896 | pub planes: u16, |
20897 | } |
20898 | impl AllocColorCellsRequest { |
20899 | /// Serialize this request into bytes for the provided connection |
20900 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
20901 | let length_so_far = 0; |
20902 | let contiguous_bytes = self.contiguous.serialize(); |
20903 | let cmap_bytes = self.cmap.serialize(); |
20904 | let colors_bytes = self.colors.serialize(); |
20905 | let planes_bytes = self.planes.serialize(); |
20906 | let mut request0 = vec![ |
20907 | ALLOC_COLOR_CELLS_REQUEST, |
20908 | contiguous_bytes[0], |
20909 | 0, |
20910 | 0, |
20911 | cmap_bytes[0], |
20912 | cmap_bytes[1], |
20913 | cmap_bytes[2], |
20914 | cmap_bytes[3], |
20915 | colors_bytes[0], |
20916 | colors_bytes[1], |
20917 | planes_bytes[0], |
20918 | planes_bytes[1], |
20919 | ]; |
20920 | let length_so_far = length_so_far + request0.len(); |
20921 | assert_eq!(length_so_far % 4, 0); |
20922 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
20923 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
20924 | (vec![request0.into()], vec![]) |
20925 | } |
20926 | /// Parse this request given its header, its body, and any fds that go along with it |
20927 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
20928 | if header.major_opcode != ALLOC_COLOR_CELLS_REQUEST { |
20929 | return Err(ParseError::InvalidValue); |
20930 | } |
20931 | let remaining = &[header.minor_opcode]; |
20932 | let (contiguous, remaining) = bool::try_parse(remaining)?; |
20933 | let _ = remaining; |
20934 | let (cmap, remaining) = Colormap::try_parse(value)?; |
20935 | let (colors, remaining) = u16::try_parse(remaining)?; |
20936 | let (planes, remaining) = u16::try_parse(remaining)?; |
20937 | let _ = remaining; |
20938 | Ok(AllocColorCellsRequest { |
20939 | contiguous, |
20940 | cmap, |
20941 | colors, |
20942 | planes, |
20943 | }) |
20944 | } |
20945 | } |
20946 | impl Request for AllocColorCellsRequest { |
20947 | const EXTENSION_NAME: Option<&'static str> = None; |
20948 | |
20949 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
20950 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
20951 | // Flatten the buffers into a single vector |
20952 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
20953 | (buf, fds) |
20954 | } |
20955 | } |
20956 | impl crate::x11_utils::ReplyRequest for AllocColorCellsRequest { |
20957 | type Reply = AllocColorCellsReply; |
20958 | } |
20959 | |
20960 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
20961 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
20962 | pub struct AllocColorCellsReply { |
20963 | pub sequence: u16, |
20964 | pub length: u32, |
20965 | pub pixels: Vec<u32>, |
20966 | pub masks: Vec<u32>, |
20967 | } |
20968 | impl TryParse for AllocColorCellsReply { |
20969 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
20970 | let remaining: &[u8] = initial_value; |
20971 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
20972 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
20973 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20974 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
20975 | let (pixels_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20976 | let (masks_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
20977 | let remaining: &[u8] = remaining.get(20..).ok_or(err:ParseError::InsufficientData)?; |
20978 | let (pixels: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:pixels_len.try_to_usize()?)?; |
20979 | let (masks: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:masks_len.try_to_usize()?)?; |
20980 | if response_type != 1 { |
20981 | return Err(ParseError::InvalidValue); |
20982 | } |
20983 | let result: AllocColorCellsReply = AllocColorCellsReply { sequence, length, pixels, masks }; |
20984 | let _ = remaining; |
20985 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
20986 | .ok_or(err:ParseError::InsufficientData)?; |
20987 | Ok((result, remaining)) |
20988 | } |
20989 | } |
20990 | impl Serialize for AllocColorCellsReply { |
20991 | type Bytes = Vec<u8>; |
20992 | fn serialize(&self) -> Vec<u8> { |
20993 | let mut result: Vec = Vec::new(); |
20994 | self.serialize_into(&mut result); |
20995 | result |
20996 | } |
20997 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
20998 | bytes.reserve(additional:32); |
20999 | let response_type_bytes: &[u8; 1] = &[1]; |
21000 | bytes.push(response_type_bytes[0]); |
21001 | bytes.extend_from_slice(&[0; 1]); |
21002 | self.sequence.serialize_into(bytes); |
21003 | self.length.serialize_into(bytes); |
21004 | let pixels_len: u16 = u16::try_from(self.pixels.len()).expect(msg:"`pixels` has too many elements" ); |
21005 | pixels_len.serialize_into(bytes); |
21006 | let masks_len: u16 = u16::try_from(self.masks.len()).expect(msg:"`masks` has too many elements" ); |
21007 | masks_len.serialize_into(bytes); |
21008 | bytes.extend_from_slice(&[0; 20]); |
21009 | self.pixels.serialize_into(bytes); |
21010 | self.masks.serialize_into(bytes); |
21011 | } |
21012 | } |
21013 | impl AllocColorCellsReply { |
21014 | /// Get the value of the `pixels_len` field. |
21015 | /// |
21016 | /// The `pixels_len` field is used as the length field of the `pixels` field. |
21017 | /// This function computes the field's value again based on the length of the list. |
21018 | /// |
21019 | /// # Panics |
21020 | /// |
21021 | /// Panics if the value cannot be represented in the target type. This |
21022 | /// cannot happen with values of the struct received from the X11 server. |
21023 | pub fn pixels_len(&self) -> u16 { |
21024 | self.pixels.len() |
21025 | .try_into().unwrap() |
21026 | } |
21027 | /// Get the value of the `masks_len` field. |
21028 | /// |
21029 | /// The `masks_len` field is used as the length field of the `masks` field. |
21030 | /// This function computes the field's value again based on the length of the list. |
21031 | /// |
21032 | /// # Panics |
21033 | /// |
21034 | /// Panics if the value cannot be represented in the target type. This |
21035 | /// cannot happen with values of the struct received from the X11 server. |
21036 | pub fn masks_len(&self) -> u16 { |
21037 | self.masks.len() |
21038 | .try_into().unwrap() |
21039 | } |
21040 | } |
21041 | |
21042 | /// Opcode for the AllocColorPlanes request |
21043 | pub const ALLOC_COLOR_PLANES_REQUEST: u8 = 87; |
21044 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21045 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21046 | pub struct AllocColorPlanesRequest { |
21047 | pub contiguous: bool, |
21048 | pub cmap: Colormap, |
21049 | pub colors: u16, |
21050 | pub reds: u16, |
21051 | pub greens: u16, |
21052 | pub blues: u16, |
21053 | } |
21054 | impl AllocColorPlanesRequest { |
21055 | /// Serialize this request into bytes for the provided connection |
21056 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
21057 | let length_so_far = 0; |
21058 | let contiguous_bytes = self.contiguous.serialize(); |
21059 | let cmap_bytes = self.cmap.serialize(); |
21060 | let colors_bytes = self.colors.serialize(); |
21061 | let reds_bytes = self.reds.serialize(); |
21062 | let greens_bytes = self.greens.serialize(); |
21063 | let blues_bytes = self.blues.serialize(); |
21064 | let mut request0 = vec![ |
21065 | ALLOC_COLOR_PLANES_REQUEST, |
21066 | contiguous_bytes[0], |
21067 | 0, |
21068 | 0, |
21069 | cmap_bytes[0], |
21070 | cmap_bytes[1], |
21071 | cmap_bytes[2], |
21072 | cmap_bytes[3], |
21073 | colors_bytes[0], |
21074 | colors_bytes[1], |
21075 | reds_bytes[0], |
21076 | reds_bytes[1], |
21077 | greens_bytes[0], |
21078 | greens_bytes[1], |
21079 | blues_bytes[0], |
21080 | blues_bytes[1], |
21081 | ]; |
21082 | let length_so_far = length_so_far + request0.len(); |
21083 | assert_eq!(length_so_far % 4, 0); |
21084 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
21085 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
21086 | (vec![request0.into()], vec![]) |
21087 | } |
21088 | /// Parse this request given its header, its body, and any fds that go along with it |
21089 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
21090 | if header.major_opcode != ALLOC_COLOR_PLANES_REQUEST { |
21091 | return Err(ParseError::InvalidValue); |
21092 | } |
21093 | let remaining = &[header.minor_opcode]; |
21094 | let (contiguous, remaining) = bool::try_parse(remaining)?; |
21095 | let _ = remaining; |
21096 | let (cmap, remaining) = Colormap::try_parse(value)?; |
21097 | let (colors, remaining) = u16::try_parse(remaining)?; |
21098 | let (reds, remaining) = u16::try_parse(remaining)?; |
21099 | let (greens, remaining) = u16::try_parse(remaining)?; |
21100 | let (blues, remaining) = u16::try_parse(remaining)?; |
21101 | let _ = remaining; |
21102 | Ok(AllocColorPlanesRequest { |
21103 | contiguous, |
21104 | cmap, |
21105 | colors, |
21106 | reds, |
21107 | greens, |
21108 | blues, |
21109 | }) |
21110 | } |
21111 | } |
21112 | impl Request for AllocColorPlanesRequest { |
21113 | const EXTENSION_NAME: Option<&'static str> = None; |
21114 | |
21115 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
21116 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
21117 | // Flatten the buffers into a single vector |
21118 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
21119 | (buf, fds) |
21120 | } |
21121 | } |
21122 | impl crate::x11_utils::ReplyRequest for AllocColorPlanesRequest { |
21123 | type Reply = AllocColorPlanesReply; |
21124 | } |
21125 | |
21126 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21127 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21128 | pub struct AllocColorPlanesReply { |
21129 | pub sequence: u16, |
21130 | pub length: u32, |
21131 | pub red_mask: u32, |
21132 | pub green_mask: u32, |
21133 | pub blue_mask: u32, |
21134 | pub pixels: Vec<u32>, |
21135 | } |
21136 | impl TryParse for AllocColorPlanesReply { |
21137 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
21138 | let remaining: &[u8] = initial_value; |
21139 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
21140 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
21141 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21142 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
21143 | let (pixels_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21144 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
21145 | let (red_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
21146 | let (green_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
21147 | let (blue_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
21148 | let remaining: &[u8] = remaining.get(8..).ok_or(err:ParseError::InsufficientData)?; |
21149 | let (pixels: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:pixels_len.try_to_usize()?)?; |
21150 | if response_type != 1 { |
21151 | return Err(ParseError::InvalidValue); |
21152 | } |
21153 | let result: AllocColorPlanesReply = AllocColorPlanesReply { sequence, length, red_mask, green_mask, blue_mask, pixels }; |
21154 | let _ = remaining; |
21155 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
21156 | .ok_or(err:ParseError::InsufficientData)?; |
21157 | Ok((result, remaining)) |
21158 | } |
21159 | } |
21160 | impl Serialize for AllocColorPlanesReply { |
21161 | type Bytes = Vec<u8>; |
21162 | fn serialize(&self) -> Vec<u8> { |
21163 | let mut result: Vec = Vec::new(); |
21164 | self.serialize_into(&mut result); |
21165 | result |
21166 | } |
21167 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
21168 | bytes.reserve(additional:32); |
21169 | let response_type_bytes: &[u8; 1] = &[1]; |
21170 | bytes.push(response_type_bytes[0]); |
21171 | bytes.extend_from_slice(&[0; 1]); |
21172 | self.sequence.serialize_into(bytes); |
21173 | self.length.serialize_into(bytes); |
21174 | let pixels_len: u16 = u16::try_from(self.pixels.len()).expect(msg:"`pixels` has too many elements" ); |
21175 | pixels_len.serialize_into(bytes); |
21176 | bytes.extend_from_slice(&[0; 2]); |
21177 | self.red_mask.serialize_into(bytes); |
21178 | self.green_mask.serialize_into(bytes); |
21179 | self.blue_mask.serialize_into(bytes); |
21180 | bytes.extend_from_slice(&[0; 8]); |
21181 | self.pixels.serialize_into(bytes); |
21182 | } |
21183 | } |
21184 | impl AllocColorPlanesReply { |
21185 | /// Get the value of the `pixels_len` field. |
21186 | /// |
21187 | /// The `pixels_len` field is used as the length field of the `pixels` field. |
21188 | /// This function computes the field's value again based on the length of the list. |
21189 | /// |
21190 | /// # Panics |
21191 | /// |
21192 | /// Panics if the value cannot be represented in the target type. This |
21193 | /// cannot happen with values of the struct received from the X11 server. |
21194 | pub fn pixels_len(&self) -> u16 { |
21195 | self.pixels.len() |
21196 | .try_into().unwrap() |
21197 | } |
21198 | } |
21199 | |
21200 | /// Opcode for the FreeColors request |
21201 | pub const FREE_COLORS_REQUEST: u8 = 88; |
21202 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21203 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21204 | pub struct FreeColorsRequest<'input> { |
21205 | pub cmap: Colormap, |
21206 | pub plane_mask: u32, |
21207 | pub pixels: Cow<'input, [u32]>, |
21208 | } |
21209 | impl<'input> FreeColorsRequest<'input> { |
21210 | /// Serialize this request into bytes for the provided connection |
21211 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
21212 | let length_so_far = 0; |
21213 | let cmap_bytes = self.cmap.serialize(); |
21214 | let plane_mask_bytes = self.plane_mask.serialize(); |
21215 | let mut request0 = vec![ |
21216 | FREE_COLORS_REQUEST, |
21217 | 0, |
21218 | 0, |
21219 | 0, |
21220 | cmap_bytes[0], |
21221 | cmap_bytes[1], |
21222 | cmap_bytes[2], |
21223 | cmap_bytes[3], |
21224 | plane_mask_bytes[0], |
21225 | plane_mask_bytes[1], |
21226 | plane_mask_bytes[2], |
21227 | plane_mask_bytes[3], |
21228 | ]; |
21229 | let length_so_far = length_so_far + request0.len(); |
21230 | let pixels_bytes = self.pixels.serialize(); |
21231 | let length_so_far = length_so_far + pixels_bytes.len(); |
21232 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
21233 | let length_so_far = length_so_far + padding0.len(); |
21234 | assert_eq!(length_so_far % 4, 0); |
21235 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
21236 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
21237 | (vec![request0.into(), pixels_bytes.into(), padding0.into()], vec![]) |
21238 | } |
21239 | /// Parse this request given its header, its body, and any fds that go along with it |
21240 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
21241 | if header.major_opcode != FREE_COLORS_REQUEST { |
21242 | return Err(ParseError::InvalidValue); |
21243 | } |
21244 | let remaining = &[header.minor_opcode]; |
21245 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
21246 | let _ = remaining; |
21247 | let (cmap, remaining) = Colormap::try_parse(value)?; |
21248 | let (plane_mask, remaining) = u32::try_parse(remaining)?; |
21249 | let mut remaining = remaining; |
21250 | // Length is 'everything left in the input' |
21251 | let mut pixels = Vec::new(); |
21252 | while !remaining.is_empty() { |
21253 | let (v, new_remaining) = u32::try_parse(remaining)?; |
21254 | remaining = new_remaining; |
21255 | pixels.push(v); |
21256 | } |
21257 | let _ = remaining; |
21258 | Ok(FreeColorsRequest { |
21259 | cmap, |
21260 | plane_mask, |
21261 | pixels: Cow::Owned(pixels), |
21262 | }) |
21263 | } |
21264 | /// Clone all borrowed data in this FreeColorsRequest. |
21265 | pub fn into_owned(self) -> FreeColorsRequest<'static> { |
21266 | FreeColorsRequest { |
21267 | cmap: self.cmap, |
21268 | plane_mask: self.plane_mask, |
21269 | pixels: Cow::Owned(self.pixels.into_owned()), |
21270 | } |
21271 | } |
21272 | } |
21273 | impl<'input> Request for FreeColorsRequest<'input> { |
21274 | const EXTENSION_NAME: Option<&'static str> = None; |
21275 | |
21276 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
21277 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
21278 | // Flatten the buffers into a single vector |
21279 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
21280 | (buf, fds) |
21281 | } |
21282 | } |
21283 | impl<'input> crate::x11_utils::VoidRequest for FreeColorsRequest<'input> { |
21284 | } |
21285 | |
21286 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21287 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21288 | pub struct ColorFlag(u8); |
21289 | impl ColorFlag { |
21290 | pub const RED: Self = Self(1 << 0); |
21291 | pub const GREEN: Self = Self(1 << 1); |
21292 | pub const BLUE: Self = Self(1 << 2); |
21293 | } |
21294 | impl From<ColorFlag> for u8 { |
21295 | #[inline ] |
21296 | fn from(input: ColorFlag) -> Self { |
21297 | input.0 |
21298 | } |
21299 | } |
21300 | impl From<ColorFlag> for Option<u8> { |
21301 | #[inline ] |
21302 | fn from(input: ColorFlag) -> Self { |
21303 | Some(input.0) |
21304 | } |
21305 | } |
21306 | impl From<ColorFlag> for u16 { |
21307 | #[inline ] |
21308 | fn from(input: ColorFlag) -> Self { |
21309 | u16::from(input.0) |
21310 | } |
21311 | } |
21312 | impl From<ColorFlag> for Option<u16> { |
21313 | #[inline ] |
21314 | fn from(input: ColorFlag) -> Self { |
21315 | Some(u16::from(input.0)) |
21316 | } |
21317 | } |
21318 | impl From<ColorFlag> for u32 { |
21319 | #[inline ] |
21320 | fn from(input: ColorFlag) -> Self { |
21321 | u32::from(input.0) |
21322 | } |
21323 | } |
21324 | impl From<ColorFlag> for Option<u32> { |
21325 | #[inline ] |
21326 | fn from(input: ColorFlag) -> Self { |
21327 | Some(u32::from(input.0)) |
21328 | } |
21329 | } |
21330 | impl From<u8> for ColorFlag { |
21331 | #[inline ] |
21332 | fn from(value: u8) -> Self { |
21333 | Self(value) |
21334 | } |
21335 | } |
21336 | impl core::fmt::Debug for ColorFlag { |
21337 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
21338 | let variants: [(u32, &str, &str); 3] = [ |
21339 | (Self::RED.0.into(), "RED" , "Red" ), |
21340 | (Self::GREEN.0.into(), "GREEN" , "Green" ), |
21341 | (Self::BLUE.0.into(), "BLUE" , "Blue" ), |
21342 | ]; |
21343 | pretty_print_bitmask(fmt, self.0.into(), &variants) |
21344 | } |
21345 | } |
21346 | bitmask_binop!(ColorFlag, u8); |
21347 | |
21348 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21349 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21350 | pub struct Coloritem { |
21351 | pub pixel: u32, |
21352 | pub red: u16, |
21353 | pub green: u16, |
21354 | pub blue: u16, |
21355 | pub flags: ColorFlag, |
21356 | } |
21357 | impl TryParse for Coloritem { |
21358 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
21359 | let (pixel: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
21360 | let (red: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21361 | let (green: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21362 | let (blue: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21363 | let (flags: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
21364 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
21365 | let flags: ColorFlag = flags.into(); |
21366 | let result: Coloritem = Coloritem { pixel, red, green, blue, flags }; |
21367 | Ok((result, remaining)) |
21368 | } |
21369 | } |
21370 | impl Serialize for Coloritem { |
21371 | type Bytes = [u8; 12]; |
21372 | fn serialize(&self) -> [u8; 12] { |
21373 | let pixel_bytes = self.pixel.serialize(); |
21374 | let red_bytes = self.red.serialize(); |
21375 | let green_bytes = self.green.serialize(); |
21376 | let blue_bytes = self.blue.serialize(); |
21377 | let flags_bytes = u8::from(self.flags).serialize(); |
21378 | [ |
21379 | pixel_bytes[0], |
21380 | pixel_bytes[1], |
21381 | pixel_bytes[2], |
21382 | pixel_bytes[3], |
21383 | red_bytes[0], |
21384 | red_bytes[1], |
21385 | green_bytes[0], |
21386 | green_bytes[1], |
21387 | blue_bytes[0], |
21388 | blue_bytes[1], |
21389 | flags_bytes[0], |
21390 | 0, |
21391 | ] |
21392 | } |
21393 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
21394 | bytes.reserve(12); |
21395 | self.pixel.serialize_into(bytes); |
21396 | self.red.serialize_into(bytes); |
21397 | self.green.serialize_into(bytes); |
21398 | self.blue.serialize_into(bytes); |
21399 | u8::from(self.flags).serialize_into(bytes); |
21400 | bytes.extend_from_slice(&[0; 1]); |
21401 | } |
21402 | } |
21403 | |
21404 | /// Opcode for the StoreColors request |
21405 | pub const STORE_COLORS_REQUEST: u8 = 89; |
21406 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21407 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21408 | pub struct StoreColorsRequest<'input> { |
21409 | pub cmap: Colormap, |
21410 | pub items: Cow<'input, [Coloritem]>, |
21411 | } |
21412 | impl<'input> StoreColorsRequest<'input> { |
21413 | /// Serialize this request into bytes for the provided connection |
21414 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
21415 | let length_so_far = 0; |
21416 | let cmap_bytes = self.cmap.serialize(); |
21417 | let mut request0 = vec![ |
21418 | STORE_COLORS_REQUEST, |
21419 | 0, |
21420 | 0, |
21421 | 0, |
21422 | cmap_bytes[0], |
21423 | cmap_bytes[1], |
21424 | cmap_bytes[2], |
21425 | cmap_bytes[3], |
21426 | ]; |
21427 | let length_so_far = length_so_far + request0.len(); |
21428 | let items_bytes = self.items.serialize(); |
21429 | let length_so_far = length_so_far + items_bytes.len(); |
21430 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
21431 | let length_so_far = length_so_far + padding0.len(); |
21432 | assert_eq!(length_so_far % 4, 0); |
21433 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
21434 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
21435 | (vec![request0.into(), items_bytes.into(), padding0.into()], vec![]) |
21436 | } |
21437 | /// Parse this request given its header, its body, and any fds that go along with it |
21438 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
21439 | if header.major_opcode != STORE_COLORS_REQUEST { |
21440 | return Err(ParseError::InvalidValue); |
21441 | } |
21442 | let remaining = &[header.minor_opcode]; |
21443 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
21444 | let _ = remaining; |
21445 | let (cmap, remaining) = Colormap::try_parse(value)?; |
21446 | let mut remaining = remaining; |
21447 | // Length is 'everything left in the input' |
21448 | let mut items = Vec::new(); |
21449 | while !remaining.is_empty() { |
21450 | let (v, new_remaining) = Coloritem::try_parse(remaining)?; |
21451 | remaining = new_remaining; |
21452 | items.push(v); |
21453 | } |
21454 | let _ = remaining; |
21455 | Ok(StoreColorsRequest { |
21456 | cmap, |
21457 | items: Cow::Owned(items), |
21458 | }) |
21459 | } |
21460 | /// Clone all borrowed data in this StoreColorsRequest. |
21461 | pub fn into_owned(self) -> StoreColorsRequest<'static> { |
21462 | StoreColorsRequest { |
21463 | cmap: self.cmap, |
21464 | items: Cow::Owned(self.items.into_owned()), |
21465 | } |
21466 | } |
21467 | } |
21468 | impl<'input> Request for StoreColorsRequest<'input> { |
21469 | const EXTENSION_NAME: Option<&'static str> = None; |
21470 | |
21471 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
21472 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
21473 | // Flatten the buffers into a single vector |
21474 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
21475 | (buf, fds) |
21476 | } |
21477 | } |
21478 | impl<'input> crate::x11_utils::VoidRequest for StoreColorsRequest<'input> { |
21479 | } |
21480 | |
21481 | /// Opcode for the StoreNamedColor request |
21482 | pub const STORE_NAMED_COLOR_REQUEST: u8 = 90; |
21483 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21484 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21485 | pub struct StoreNamedColorRequest<'input> { |
21486 | pub flags: ColorFlag, |
21487 | pub cmap: Colormap, |
21488 | pub pixel: u32, |
21489 | pub name: Cow<'input, [u8]>, |
21490 | } |
21491 | impl<'input> StoreNamedColorRequest<'input> { |
21492 | /// Serialize this request into bytes for the provided connection |
21493 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
21494 | let length_so_far = 0; |
21495 | let flags_bytes = u8::from(self.flags).serialize(); |
21496 | let cmap_bytes = self.cmap.serialize(); |
21497 | let pixel_bytes = self.pixel.serialize(); |
21498 | let name_len = u16::try_from(self.name.len()).expect("`name` has too many elements" ); |
21499 | let name_len_bytes = name_len.serialize(); |
21500 | let mut request0 = vec![ |
21501 | STORE_NAMED_COLOR_REQUEST, |
21502 | flags_bytes[0], |
21503 | 0, |
21504 | 0, |
21505 | cmap_bytes[0], |
21506 | cmap_bytes[1], |
21507 | cmap_bytes[2], |
21508 | cmap_bytes[3], |
21509 | pixel_bytes[0], |
21510 | pixel_bytes[1], |
21511 | pixel_bytes[2], |
21512 | pixel_bytes[3], |
21513 | name_len_bytes[0], |
21514 | name_len_bytes[1], |
21515 | 0, |
21516 | 0, |
21517 | ]; |
21518 | let length_so_far = length_so_far + request0.len(); |
21519 | let length_so_far = length_so_far + self.name.len(); |
21520 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
21521 | let length_so_far = length_so_far + padding0.len(); |
21522 | assert_eq!(length_so_far % 4, 0); |
21523 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
21524 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
21525 | (vec![request0.into(), self.name, padding0.into()], vec![]) |
21526 | } |
21527 | /// Parse this request given its header, its body, and any fds that go along with it |
21528 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
21529 | if header.major_opcode != STORE_NAMED_COLOR_REQUEST { |
21530 | return Err(ParseError::InvalidValue); |
21531 | } |
21532 | let remaining = &[header.minor_opcode]; |
21533 | let (flags, remaining) = u8::try_parse(remaining)?; |
21534 | let flags = flags.into(); |
21535 | let _ = remaining; |
21536 | let (cmap, remaining) = Colormap::try_parse(value)?; |
21537 | let (pixel, remaining) = u32::try_parse(remaining)?; |
21538 | let (name_len, remaining) = u16::try_parse(remaining)?; |
21539 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
21540 | let (name, remaining) = crate::x11_utils::parse_u8_list(remaining, name_len.try_to_usize()?)?; |
21541 | let _ = remaining; |
21542 | Ok(StoreNamedColorRequest { |
21543 | flags, |
21544 | cmap, |
21545 | pixel, |
21546 | name: Cow::Borrowed(name), |
21547 | }) |
21548 | } |
21549 | /// Clone all borrowed data in this StoreNamedColorRequest. |
21550 | pub fn into_owned(self) -> StoreNamedColorRequest<'static> { |
21551 | StoreNamedColorRequest { |
21552 | flags: self.flags, |
21553 | cmap: self.cmap, |
21554 | pixel: self.pixel, |
21555 | name: Cow::Owned(self.name.into_owned()), |
21556 | } |
21557 | } |
21558 | } |
21559 | impl<'input> Request for StoreNamedColorRequest<'input> { |
21560 | const EXTENSION_NAME: Option<&'static str> = None; |
21561 | |
21562 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
21563 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
21564 | // Flatten the buffers into a single vector |
21565 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
21566 | (buf, fds) |
21567 | } |
21568 | } |
21569 | impl<'input> crate::x11_utils::VoidRequest for StoreNamedColorRequest<'input> { |
21570 | } |
21571 | |
21572 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21573 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21574 | pub struct Rgb { |
21575 | pub red: u16, |
21576 | pub green: u16, |
21577 | pub blue: u16, |
21578 | } |
21579 | impl TryParse for Rgb { |
21580 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
21581 | let (red: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21582 | let (green: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21583 | let (blue: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21584 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
21585 | let result: Rgb = Rgb { red, green, blue }; |
21586 | Ok((result, remaining)) |
21587 | } |
21588 | } |
21589 | impl Serialize for Rgb { |
21590 | type Bytes = [u8; 8]; |
21591 | fn serialize(&self) -> [u8; 8] { |
21592 | let red_bytes = self.red.serialize(); |
21593 | let green_bytes = self.green.serialize(); |
21594 | let blue_bytes = self.blue.serialize(); |
21595 | [ |
21596 | red_bytes[0], |
21597 | red_bytes[1], |
21598 | green_bytes[0], |
21599 | green_bytes[1], |
21600 | blue_bytes[0], |
21601 | blue_bytes[1], |
21602 | 0, |
21603 | 0, |
21604 | ] |
21605 | } |
21606 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
21607 | bytes.reserve(8); |
21608 | self.red.serialize_into(bytes); |
21609 | self.green.serialize_into(bytes); |
21610 | self.blue.serialize_into(bytes); |
21611 | bytes.extend_from_slice(&[0; 2]); |
21612 | } |
21613 | } |
21614 | |
21615 | /// Opcode for the QueryColors request |
21616 | pub const QUERY_COLORS_REQUEST: u8 = 91; |
21617 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21618 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21619 | pub struct QueryColorsRequest<'input> { |
21620 | pub cmap: Colormap, |
21621 | pub pixels: Cow<'input, [u32]>, |
21622 | } |
21623 | impl<'input> QueryColorsRequest<'input> { |
21624 | /// Serialize this request into bytes for the provided connection |
21625 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
21626 | let length_so_far = 0; |
21627 | let cmap_bytes = self.cmap.serialize(); |
21628 | let mut request0 = vec![ |
21629 | QUERY_COLORS_REQUEST, |
21630 | 0, |
21631 | 0, |
21632 | 0, |
21633 | cmap_bytes[0], |
21634 | cmap_bytes[1], |
21635 | cmap_bytes[2], |
21636 | cmap_bytes[3], |
21637 | ]; |
21638 | let length_so_far = length_so_far + request0.len(); |
21639 | let pixels_bytes = self.pixels.serialize(); |
21640 | let length_so_far = length_so_far + pixels_bytes.len(); |
21641 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
21642 | let length_so_far = length_so_far + padding0.len(); |
21643 | assert_eq!(length_so_far % 4, 0); |
21644 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
21645 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
21646 | (vec![request0.into(), pixels_bytes.into(), padding0.into()], vec![]) |
21647 | } |
21648 | /// Parse this request given its header, its body, and any fds that go along with it |
21649 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
21650 | if header.major_opcode != QUERY_COLORS_REQUEST { |
21651 | return Err(ParseError::InvalidValue); |
21652 | } |
21653 | let remaining = &[header.minor_opcode]; |
21654 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
21655 | let _ = remaining; |
21656 | let (cmap, remaining) = Colormap::try_parse(value)?; |
21657 | let mut remaining = remaining; |
21658 | // Length is 'everything left in the input' |
21659 | let mut pixels = Vec::new(); |
21660 | while !remaining.is_empty() { |
21661 | let (v, new_remaining) = u32::try_parse(remaining)?; |
21662 | remaining = new_remaining; |
21663 | pixels.push(v); |
21664 | } |
21665 | let _ = remaining; |
21666 | Ok(QueryColorsRequest { |
21667 | cmap, |
21668 | pixels: Cow::Owned(pixels), |
21669 | }) |
21670 | } |
21671 | /// Clone all borrowed data in this QueryColorsRequest. |
21672 | pub fn into_owned(self) -> QueryColorsRequest<'static> { |
21673 | QueryColorsRequest { |
21674 | cmap: self.cmap, |
21675 | pixels: Cow::Owned(self.pixels.into_owned()), |
21676 | } |
21677 | } |
21678 | } |
21679 | impl<'input> Request for QueryColorsRequest<'input> { |
21680 | const EXTENSION_NAME: Option<&'static str> = None; |
21681 | |
21682 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
21683 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
21684 | // Flatten the buffers into a single vector |
21685 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
21686 | (buf, fds) |
21687 | } |
21688 | } |
21689 | impl<'input> crate::x11_utils::ReplyRequest for QueryColorsRequest<'input> { |
21690 | type Reply = QueryColorsReply; |
21691 | } |
21692 | |
21693 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21694 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21695 | pub struct QueryColorsReply { |
21696 | pub sequence: u16, |
21697 | pub length: u32, |
21698 | pub colors: Vec<Rgb>, |
21699 | } |
21700 | impl TryParse for QueryColorsReply { |
21701 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
21702 | let remaining: &[u8] = initial_value; |
21703 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
21704 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
21705 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21706 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
21707 | let (colors_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21708 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
21709 | let (colors: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<Rgb>(data:remaining, list_length:colors_len.try_to_usize()?)?; |
21710 | if response_type != 1 { |
21711 | return Err(ParseError::InvalidValue); |
21712 | } |
21713 | let result: QueryColorsReply = QueryColorsReply { sequence, length, colors }; |
21714 | let _ = remaining; |
21715 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
21716 | .ok_or(err:ParseError::InsufficientData)?; |
21717 | Ok((result, remaining)) |
21718 | } |
21719 | } |
21720 | impl Serialize for QueryColorsReply { |
21721 | type Bytes = Vec<u8>; |
21722 | fn serialize(&self) -> Vec<u8> { |
21723 | let mut result: Vec = Vec::new(); |
21724 | self.serialize_into(&mut result); |
21725 | result |
21726 | } |
21727 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
21728 | bytes.reserve(additional:32); |
21729 | let response_type_bytes: &[u8; 1] = &[1]; |
21730 | bytes.push(response_type_bytes[0]); |
21731 | bytes.extend_from_slice(&[0; 1]); |
21732 | self.sequence.serialize_into(bytes); |
21733 | self.length.serialize_into(bytes); |
21734 | let colors_len: u16 = u16::try_from(self.colors.len()).expect(msg:"`colors` has too many elements" ); |
21735 | colors_len.serialize_into(bytes); |
21736 | bytes.extend_from_slice(&[0; 22]); |
21737 | self.colors.serialize_into(bytes); |
21738 | } |
21739 | } |
21740 | impl QueryColorsReply { |
21741 | /// Get the value of the `colors_len` field. |
21742 | /// |
21743 | /// The `colors_len` field is used as the length field of the `colors` field. |
21744 | /// This function computes the field's value again based on the length of the list. |
21745 | /// |
21746 | /// # Panics |
21747 | /// |
21748 | /// Panics if the value cannot be represented in the target type. This |
21749 | /// cannot happen with values of the struct received from the X11 server. |
21750 | pub fn colors_len(&self) -> u16 { |
21751 | self.colors.len() |
21752 | .try_into().unwrap() |
21753 | } |
21754 | } |
21755 | |
21756 | /// Opcode for the LookupColor request |
21757 | pub const LOOKUP_COLOR_REQUEST: u8 = 92; |
21758 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21759 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21760 | pub struct LookupColorRequest<'input> { |
21761 | pub cmap: Colormap, |
21762 | pub name: Cow<'input, [u8]>, |
21763 | } |
21764 | impl<'input> LookupColorRequest<'input> { |
21765 | /// Serialize this request into bytes for the provided connection |
21766 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
21767 | let length_so_far = 0; |
21768 | let cmap_bytes = self.cmap.serialize(); |
21769 | let name_len = u16::try_from(self.name.len()).expect("`name` has too many elements" ); |
21770 | let name_len_bytes = name_len.serialize(); |
21771 | let mut request0 = vec![ |
21772 | LOOKUP_COLOR_REQUEST, |
21773 | 0, |
21774 | 0, |
21775 | 0, |
21776 | cmap_bytes[0], |
21777 | cmap_bytes[1], |
21778 | cmap_bytes[2], |
21779 | cmap_bytes[3], |
21780 | name_len_bytes[0], |
21781 | name_len_bytes[1], |
21782 | 0, |
21783 | 0, |
21784 | ]; |
21785 | let length_so_far = length_so_far + request0.len(); |
21786 | let length_so_far = length_so_far + self.name.len(); |
21787 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
21788 | let length_so_far = length_so_far + padding0.len(); |
21789 | assert_eq!(length_so_far % 4, 0); |
21790 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
21791 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
21792 | (vec![request0.into(), self.name, padding0.into()], vec![]) |
21793 | } |
21794 | /// Parse this request given its header, its body, and any fds that go along with it |
21795 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
21796 | if header.major_opcode != LOOKUP_COLOR_REQUEST { |
21797 | return Err(ParseError::InvalidValue); |
21798 | } |
21799 | let remaining = &[header.minor_opcode]; |
21800 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
21801 | let _ = remaining; |
21802 | let (cmap, remaining) = Colormap::try_parse(value)?; |
21803 | let (name_len, remaining) = u16::try_parse(remaining)?; |
21804 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
21805 | let (name, remaining) = crate::x11_utils::parse_u8_list(remaining, name_len.try_to_usize()?)?; |
21806 | let _ = remaining; |
21807 | Ok(LookupColorRequest { |
21808 | cmap, |
21809 | name: Cow::Borrowed(name), |
21810 | }) |
21811 | } |
21812 | /// Clone all borrowed data in this LookupColorRequest. |
21813 | pub fn into_owned(self) -> LookupColorRequest<'static> { |
21814 | LookupColorRequest { |
21815 | cmap: self.cmap, |
21816 | name: Cow::Owned(self.name.into_owned()), |
21817 | } |
21818 | } |
21819 | } |
21820 | impl<'input> Request for LookupColorRequest<'input> { |
21821 | const EXTENSION_NAME: Option<&'static str> = None; |
21822 | |
21823 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
21824 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
21825 | // Flatten the buffers into a single vector |
21826 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
21827 | (buf, fds) |
21828 | } |
21829 | } |
21830 | impl<'input> crate::x11_utils::ReplyRequest for LookupColorRequest<'input> { |
21831 | type Reply = LookupColorReply; |
21832 | } |
21833 | |
21834 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21835 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21836 | pub struct LookupColorReply { |
21837 | pub sequence: u16, |
21838 | pub length: u32, |
21839 | pub exact_red: u16, |
21840 | pub exact_green: u16, |
21841 | pub exact_blue: u16, |
21842 | pub visual_red: u16, |
21843 | pub visual_green: u16, |
21844 | pub visual_blue: u16, |
21845 | } |
21846 | impl TryParse for LookupColorReply { |
21847 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
21848 | let remaining: &[u8] = initial_value; |
21849 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
21850 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
21851 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21852 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
21853 | let (exact_red: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21854 | let (exact_green: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21855 | let (exact_blue: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21856 | let (visual_red: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21857 | let (visual_green: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21858 | let (visual_blue: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
21859 | if response_type != 1 { |
21860 | return Err(ParseError::InvalidValue); |
21861 | } |
21862 | let result: LookupColorReply = LookupColorReply { sequence, length, exact_red, exact_green, exact_blue, visual_red, visual_green, visual_blue }; |
21863 | let _ = remaining; |
21864 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
21865 | .ok_or(err:ParseError::InsufficientData)?; |
21866 | Ok((result, remaining)) |
21867 | } |
21868 | } |
21869 | impl Serialize for LookupColorReply { |
21870 | type Bytes = [u8; 20]; |
21871 | fn serialize(&self) -> [u8; 20] { |
21872 | let response_type_bytes = &[1]; |
21873 | let sequence_bytes = self.sequence.serialize(); |
21874 | let length_bytes = self.length.serialize(); |
21875 | let exact_red_bytes = self.exact_red.serialize(); |
21876 | let exact_green_bytes = self.exact_green.serialize(); |
21877 | let exact_blue_bytes = self.exact_blue.serialize(); |
21878 | let visual_red_bytes = self.visual_red.serialize(); |
21879 | let visual_green_bytes = self.visual_green.serialize(); |
21880 | let visual_blue_bytes = self.visual_blue.serialize(); |
21881 | [ |
21882 | response_type_bytes[0], |
21883 | 0, |
21884 | sequence_bytes[0], |
21885 | sequence_bytes[1], |
21886 | length_bytes[0], |
21887 | length_bytes[1], |
21888 | length_bytes[2], |
21889 | length_bytes[3], |
21890 | exact_red_bytes[0], |
21891 | exact_red_bytes[1], |
21892 | exact_green_bytes[0], |
21893 | exact_green_bytes[1], |
21894 | exact_blue_bytes[0], |
21895 | exact_blue_bytes[1], |
21896 | visual_red_bytes[0], |
21897 | visual_red_bytes[1], |
21898 | visual_green_bytes[0], |
21899 | visual_green_bytes[1], |
21900 | visual_blue_bytes[0], |
21901 | visual_blue_bytes[1], |
21902 | ] |
21903 | } |
21904 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
21905 | bytes.reserve(20); |
21906 | let response_type_bytes = &[1]; |
21907 | bytes.push(response_type_bytes[0]); |
21908 | bytes.extend_from_slice(&[0; 1]); |
21909 | self.sequence.serialize_into(bytes); |
21910 | self.length.serialize_into(bytes); |
21911 | self.exact_red.serialize_into(bytes); |
21912 | self.exact_green.serialize_into(bytes); |
21913 | self.exact_blue.serialize_into(bytes); |
21914 | self.visual_red.serialize_into(bytes); |
21915 | self.visual_green.serialize_into(bytes); |
21916 | self.visual_blue.serialize_into(bytes); |
21917 | } |
21918 | } |
21919 | |
21920 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21921 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21922 | pub struct PixmapEnum(u8); |
21923 | impl PixmapEnum { |
21924 | pub const NONE: Self = Self(0); |
21925 | } |
21926 | impl From<PixmapEnum> for u8 { |
21927 | #[inline ] |
21928 | fn from(input: PixmapEnum) -> Self { |
21929 | input.0 |
21930 | } |
21931 | } |
21932 | impl From<PixmapEnum> for Option<u8> { |
21933 | #[inline ] |
21934 | fn from(input: PixmapEnum) -> Self { |
21935 | Some(input.0) |
21936 | } |
21937 | } |
21938 | impl From<PixmapEnum> for u16 { |
21939 | #[inline ] |
21940 | fn from(input: PixmapEnum) -> Self { |
21941 | u16::from(input.0) |
21942 | } |
21943 | } |
21944 | impl From<PixmapEnum> for Option<u16> { |
21945 | #[inline ] |
21946 | fn from(input: PixmapEnum) -> Self { |
21947 | Some(u16::from(input.0)) |
21948 | } |
21949 | } |
21950 | impl From<PixmapEnum> for u32 { |
21951 | #[inline ] |
21952 | fn from(input: PixmapEnum) -> Self { |
21953 | u32::from(input.0) |
21954 | } |
21955 | } |
21956 | impl From<PixmapEnum> for Option<u32> { |
21957 | #[inline ] |
21958 | fn from(input: PixmapEnum) -> Self { |
21959 | Some(u32::from(input.0)) |
21960 | } |
21961 | } |
21962 | impl From<u8> for PixmapEnum { |
21963 | #[inline ] |
21964 | fn from(value: u8) -> Self { |
21965 | Self(value) |
21966 | } |
21967 | } |
21968 | impl core::fmt::Debug for PixmapEnum { |
21969 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
21970 | let variants: [(u32, &str, &str); 1] = [ |
21971 | (Self::NONE.0.into(), "NONE" , "None" ), |
21972 | ]; |
21973 | pretty_print_enum(fmt, self.0.into(), &variants) |
21974 | } |
21975 | } |
21976 | |
21977 | /// Opcode for the CreateCursor request |
21978 | pub const CREATE_CURSOR_REQUEST: u8 = 93; |
21979 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21980 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
21981 | pub struct CreateCursorRequest { |
21982 | pub cid: Cursor, |
21983 | pub source: Pixmap, |
21984 | pub mask: Pixmap, |
21985 | pub fore_red: u16, |
21986 | pub fore_green: u16, |
21987 | pub fore_blue: u16, |
21988 | pub back_red: u16, |
21989 | pub back_green: u16, |
21990 | pub back_blue: u16, |
21991 | pub x: u16, |
21992 | pub y: u16, |
21993 | } |
21994 | impl CreateCursorRequest { |
21995 | /// Serialize this request into bytes for the provided connection |
21996 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
21997 | let length_so_far = 0; |
21998 | let cid_bytes = self.cid.serialize(); |
21999 | let source_bytes = self.source.serialize(); |
22000 | let mask_bytes = self.mask.serialize(); |
22001 | let fore_red_bytes = self.fore_red.serialize(); |
22002 | let fore_green_bytes = self.fore_green.serialize(); |
22003 | let fore_blue_bytes = self.fore_blue.serialize(); |
22004 | let back_red_bytes = self.back_red.serialize(); |
22005 | let back_green_bytes = self.back_green.serialize(); |
22006 | let back_blue_bytes = self.back_blue.serialize(); |
22007 | let x_bytes = self.x.serialize(); |
22008 | let y_bytes = self.y.serialize(); |
22009 | let mut request0 = vec![ |
22010 | CREATE_CURSOR_REQUEST, |
22011 | 0, |
22012 | 0, |
22013 | 0, |
22014 | cid_bytes[0], |
22015 | cid_bytes[1], |
22016 | cid_bytes[2], |
22017 | cid_bytes[3], |
22018 | source_bytes[0], |
22019 | source_bytes[1], |
22020 | source_bytes[2], |
22021 | source_bytes[3], |
22022 | mask_bytes[0], |
22023 | mask_bytes[1], |
22024 | mask_bytes[2], |
22025 | mask_bytes[3], |
22026 | fore_red_bytes[0], |
22027 | fore_red_bytes[1], |
22028 | fore_green_bytes[0], |
22029 | fore_green_bytes[1], |
22030 | fore_blue_bytes[0], |
22031 | fore_blue_bytes[1], |
22032 | back_red_bytes[0], |
22033 | back_red_bytes[1], |
22034 | back_green_bytes[0], |
22035 | back_green_bytes[1], |
22036 | back_blue_bytes[0], |
22037 | back_blue_bytes[1], |
22038 | x_bytes[0], |
22039 | x_bytes[1], |
22040 | y_bytes[0], |
22041 | y_bytes[1], |
22042 | ]; |
22043 | let length_so_far = length_so_far + request0.len(); |
22044 | assert_eq!(length_so_far % 4, 0); |
22045 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
22046 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
22047 | (vec![request0.into()], vec![]) |
22048 | } |
22049 | /// Parse this request given its header, its body, and any fds that go along with it |
22050 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
22051 | if header.major_opcode != CREATE_CURSOR_REQUEST { |
22052 | return Err(ParseError::InvalidValue); |
22053 | } |
22054 | let remaining = &[header.minor_opcode]; |
22055 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
22056 | let _ = remaining; |
22057 | let (cid, remaining) = Cursor::try_parse(value)?; |
22058 | let (source, remaining) = Pixmap::try_parse(remaining)?; |
22059 | let (mask, remaining) = Pixmap::try_parse(remaining)?; |
22060 | let (fore_red, remaining) = u16::try_parse(remaining)?; |
22061 | let (fore_green, remaining) = u16::try_parse(remaining)?; |
22062 | let (fore_blue, remaining) = u16::try_parse(remaining)?; |
22063 | let (back_red, remaining) = u16::try_parse(remaining)?; |
22064 | let (back_green, remaining) = u16::try_parse(remaining)?; |
22065 | let (back_blue, remaining) = u16::try_parse(remaining)?; |
22066 | let (x, remaining) = u16::try_parse(remaining)?; |
22067 | let (y, remaining) = u16::try_parse(remaining)?; |
22068 | let _ = remaining; |
22069 | Ok(CreateCursorRequest { |
22070 | cid, |
22071 | source, |
22072 | mask, |
22073 | fore_red, |
22074 | fore_green, |
22075 | fore_blue, |
22076 | back_red, |
22077 | back_green, |
22078 | back_blue, |
22079 | x, |
22080 | y, |
22081 | }) |
22082 | } |
22083 | } |
22084 | impl Request for CreateCursorRequest { |
22085 | const EXTENSION_NAME: Option<&'static str> = None; |
22086 | |
22087 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
22088 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
22089 | // Flatten the buffers into a single vector |
22090 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
22091 | (buf, fds) |
22092 | } |
22093 | } |
22094 | impl crate::x11_utils::VoidRequest for CreateCursorRequest { |
22095 | } |
22096 | |
22097 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
22098 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
22099 | pub struct FontEnum(u8); |
22100 | impl FontEnum { |
22101 | pub const NONE: Self = Self(0); |
22102 | } |
22103 | impl From<FontEnum> for u8 { |
22104 | #[inline ] |
22105 | fn from(input: FontEnum) -> Self { |
22106 | input.0 |
22107 | } |
22108 | } |
22109 | impl From<FontEnum> for Option<u8> { |
22110 | #[inline ] |
22111 | fn from(input: FontEnum) -> Self { |
22112 | Some(input.0) |
22113 | } |
22114 | } |
22115 | impl From<FontEnum> for u16 { |
22116 | #[inline ] |
22117 | fn from(input: FontEnum) -> Self { |
22118 | u16::from(input.0) |
22119 | } |
22120 | } |
22121 | impl From<FontEnum> for Option<u16> { |
22122 | #[inline ] |
22123 | fn from(input: FontEnum) -> Self { |
22124 | Some(u16::from(input.0)) |
22125 | } |
22126 | } |
22127 | impl From<FontEnum> for u32 { |
22128 | #[inline ] |
22129 | fn from(input: FontEnum) -> Self { |
22130 | u32::from(input.0) |
22131 | } |
22132 | } |
22133 | impl From<FontEnum> for Option<u32> { |
22134 | #[inline ] |
22135 | fn from(input: FontEnum) -> Self { |
22136 | Some(u32::from(input.0)) |
22137 | } |
22138 | } |
22139 | impl From<u8> for FontEnum { |
22140 | #[inline ] |
22141 | fn from(value: u8) -> Self { |
22142 | Self(value) |
22143 | } |
22144 | } |
22145 | impl core::fmt::Debug for FontEnum { |
22146 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
22147 | let variants: [(u32, &str, &str); 1] = [ |
22148 | (Self::NONE.0.into(), "NONE" , "None" ), |
22149 | ]; |
22150 | pretty_print_enum(fmt, self.0.into(), &variants) |
22151 | } |
22152 | } |
22153 | |
22154 | /// Opcode for the CreateGlyphCursor request |
22155 | pub const CREATE_GLYPH_CURSOR_REQUEST: u8 = 94; |
22156 | /// create cursor. |
22157 | /// |
22158 | /// Creates a cursor from a font glyph. X provides a set of standard cursor shapes |
22159 | /// in a special font named cursor. Applications are encouraged to use this |
22160 | /// interface for their cursors because the font can be customized for the |
22161 | /// individual display type. |
22162 | /// |
22163 | /// All pixels which are set to 1 in the source will use the foreground color (as |
22164 | /// specified by `fore_red`, `fore_green` and `fore_blue`). All pixels set to 0 |
22165 | /// will use the background color (as specified by `back_red`, `back_green` and |
22166 | /// `back_blue`). |
22167 | /// |
22168 | /// # Fields |
22169 | /// |
22170 | /// * `cid` - The ID with which you will refer to the cursor, created by `xcb_generate_id`. |
22171 | /// * `source_font` - In which font to look for the cursor glyph. |
22172 | /// * `mask_font` - In which font to look for the mask glyph. |
22173 | /// * `source_char` - The glyph of `source_font` to use. |
22174 | /// * `mask_char` - The glyph of `mask_font` to use as a mask: Pixels which are set to 1 define |
22175 | /// which source pixels are displayed. All pixels which are set to 0 are not |
22176 | /// displayed. |
22177 | /// * `fore_red` - The red value of the foreground color. |
22178 | /// * `fore_green` - The green value of the foreground color. |
22179 | /// * `fore_blue` - The blue value of the foreground color. |
22180 | /// * `back_red` - The red value of the background color. |
22181 | /// * `back_green` - The green value of the background color. |
22182 | /// * `back_blue` - The blue value of the background color. |
22183 | /// |
22184 | /// # Errors |
22185 | /// |
22186 | /// * `Alloc` - The X server could not allocate the requested resources (no memory?). |
22187 | /// * `Font` - The specified `source_font` or `mask_font` does not exist. |
22188 | /// * `Value` - Either `source_char` or `mask_char` are not defined in `source_font` or `mask_font`, respectively. |
22189 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
22190 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
22191 | pub struct CreateGlyphCursorRequest { |
22192 | pub cid: Cursor, |
22193 | pub source_font: Font, |
22194 | pub mask_font: Font, |
22195 | pub source_char: u16, |
22196 | pub mask_char: u16, |
22197 | pub fore_red: u16, |
22198 | pub fore_green: u16, |
22199 | pub fore_blue: u16, |
22200 | pub back_red: u16, |
22201 | pub back_green: u16, |
22202 | pub back_blue: u16, |
22203 | } |
22204 | impl CreateGlyphCursorRequest { |
22205 | /// Serialize this request into bytes for the provided connection |
22206 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
22207 | let length_so_far = 0; |
22208 | let cid_bytes = self.cid.serialize(); |
22209 | let source_font_bytes = self.source_font.serialize(); |
22210 | let mask_font_bytes = self.mask_font.serialize(); |
22211 | let source_char_bytes = self.source_char.serialize(); |
22212 | let mask_char_bytes = self.mask_char.serialize(); |
22213 | let fore_red_bytes = self.fore_red.serialize(); |
22214 | let fore_green_bytes = self.fore_green.serialize(); |
22215 | let fore_blue_bytes = self.fore_blue.serialize(); |
22216 | let back_red_bytes = self.back_red.serialize(); |
22217 | let back_green_bytes = self.back_green.serialize(); |
22218 | let back_blue_bytes = self.back_blue.serialize(); |
22219 | let mut request0 = vec![ |
22220 | CREATE_GLYPH_CURSOR_REQUEST, |
22221 | 0, |
22222 | 0, |
22223 | 0, |
22224 | cid_bytes[0], |
22225 | cid_bytes[1], |
22226 | cid_bytes[2], |
22227 | cid_bytes[3], |
22228 | source_font_bytes[0], |
22229 | source_font_bytes[1], |
22230 | source_font_bytes[2], |
22231 | source_font_bytes[3], |
22232 | mask_font_bytes[0], |
22233 | mask_font_bytes[1], |
22234 | mask_font_bytes[2], |
22235 | mask_font_bytes[3], |
22236 | source_char_bytes[0], |
22237 | source_char_bytes[1], |
22238 | mask_char_bytes[0], |
22239 | mask_char_bytes[1], |
22240 | fore_red_bytes[0], |
22241 | fore_red_bytes[1], |
22242 | fore_green_bytes[0], |
22243 | fore_green_bytes[1], |
22244 | fore_blue_bytes[0], |
22245 | fore_blue_bytes[1], |
22246 | back_red_bytes[0], |
22247 | back_red_bytes[1], |
22248 | back_green_bytes[0], |
22249 | back_green_bytes[1], |
22250 | back_blue_bytes[0], |
22251 | back_blue_bytes[1], |
22252 | ]; |
22253 | let length_so_far = length_so_far + request0.len(); |
22254 | assert_eq!(length_so_far % 4, 0); |
22255 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
22256 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
22257 | (vec![request0.into()], vec![]) |
22258 | } |
22259 | /// Parse this request given its header, its body, and any fds that go along with it |
22260 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
22261 | if header.major_opcode != CREATE_GLYPH_CURSOR_REQUEST { |
22262 | return Err(ParseError::InvalidValue); |
22263 | } |
22264 | let remaining = &[header.minor_opcode]; |
22265 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
22266 | let _ = remaining; |
22267 | let (cid, remaining) = Cursor::try_parse(value)?; |
22268 | let (source_font, remaining) = Font::try_parse(remaining)?; |
22269 | let (mask_font, remaining) = Font::try_parse(remaining)?; |
22270 | let (source_char, remaining) = u16::try_parse(remaining)?; |
22271 | let (mask_char, remaining) = u16::try_parse(remaining)?; |
22272 | let (fore_red, remaining) = u16::try_parse(remaining)?; |
22273 | let (fore_green, remaining) = u16::try_parse(remaining)?; |
22274 | let (fore_blue, remaining) = u16::try_parse(remaining)?; |
22275 | let (back_red, remaining) = u16::try_parse(remaining)?; |
22276 | let (back_green, remaining) = u16::try_parse(remaining)?; |
22277 | let (back_blue, remaining) = u16::try_parse(remaining)?; |
22278 | let _ = remaining; |
22279 | Ok(CreateGlyphCursorRequest { |
22280 | cid, |
22281 | source_font, |
22282 | mask_font, |
22283 | source_char, |
22284 | mask_char, |
22285 | fore_red, |
22286 | fore_green, |
22287 | fore_blue, |
22288 | back_red, |
22289 | back_green, |
22290 | back_blue, |
22291 | }) |
22292 | } |
22293 | } |
22294 | impl Request for CreateGlyphCursorRequest { |
22295 | const EXTENSION_NAME: Option<&'static str> = None; |
22296 | |
22297 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
22298 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
22299 | // Flatten the buffers into a single vector |
22300 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
22301 | (buf, fds) |
22302 | } |
22303 | } |
22304 | impl crate::x11_utils::VoidRequest for CreateGlyphCursorRequest { |
22305 | } |
22306 | |
22307 | /// Opcode for the FreeCursor request |
22308 | pub const FREE_CURSOR_REQUEST: u8 = 95; |
22309 | /// Deletes a cursor. |
22310 | /// |
22311 | /// Deletes the association between the cursor resource ID and the specified |
22312 | /// cursor. The cursor is freed when no other resource references it. |
22313 | /// |
22314 | /// # Fields |
22315 | /// |
22316 | /// * `cursor` - The cursor to destroy. |
22317 | /// |
22318 | /// # Errors |
22319 | /// |
22320 | /// * `Cursor` - The specified cursor does not exist. |
22321 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
22322 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
22323 | pub struct FreeCursorRequest { |
22324 | pub cursor: Cursor, |
22325 | } |
22326 | impl FreeCursorRequest { |
22327 | /// Serialize this request into bytes for the provided connection |
22328 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
22329 | let length_so_far = 0; |
22330 | let cursor_bytes = self.cursor.serialize(); |
22331 | let mut request0 = vec![ |
22332 | FREE_CURSOR_REQUEST, |
22333 | 0, |
22334 | 0, |
22335 | 0, |
22336 | cursor_bytes[0], |
22337 | cursor_bytes[1], |
22338 | cursor_bytes[2], |
22339 | cursor_bytes[3], |
22340 | ]; |
22341 | let length_so_far = length_so_far + request0.len(); |
22342 | assert_eq!(length_so_far % 4, 0); |
22343 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
22344 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
22345 | (vec![request0.into()], vec![]) |
22346 | } |
22347 | /// Parse this request given its header, its body, and any fds that go along with it |
22348 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
22349 | if header.major_opcode != FREE_CURSOR_REQUEST { |
22350 | return Err(ParseError::InvalidValue); |
22351 | } |
22352 | let remaining = &[header.minor_opcode]; |
22353 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
22354 | let _ = remaining; |
22355 | let (cursor, remaining) = Cursor::try_parse(value)?; |
22356 | let _ = remaining; |
22357 | Ok(FreeCursorRequest { |
22358 | cursor, |
22359 | }) |
22360 | } |
22361 | } |
22362 | impl Request for FreeCursorRequest { |
22363 | const EXTENSION_NAME: Option<&'static str> = None; |
22364 | |
22365 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
22366 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
22367 | // Flatten the buffers into a single vector |
22368 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
22369 | (buf, fds) |
22370 | } |
22371 | } |
22372 | impl crate::x11_utils::VoidRequest for FreeCursorRequest { |
22373 | } |
22374 | |
22375 | /// Opcode for the RecolorCursor request |
22376 | pub const RECOLOR_CURSOR_REQUEST: u8 = 96; |
22377 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
22378 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
22379 | pub struct RecolorCursorRequest { |
22380 | pub cursor: Cursor, |
22381 | pub fore_red: u16, |
22382 | pub fore_green: u16, |
22383 | pub fore_blue: u16, |
22384 | pub back_red: u16, |
22385 | pub back_green: u16, |
22386 | pub back_blue: u16, |
22387 | } |
22388 | impl RecolorCursorRequest { |
22389 | /// Serialize this request into bytes for the provided connection |
22390 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
22391 | let length_so_far = 0; |
22392 | let cursor_bytes = self.cursor.serialize(); |
22393 | let fore_red_bytes = self.fore_red.serialize(); |
22394 | let fore_green_bytes = self.fore_green.serialize(); |
22395 | let fore_blue_bytes = self.fore_blue.serialize(); |
22396 | let back_red_bytes = self.back_red.serialize(); |
22397 | let back_green_bytes = self.back_green.serialize(); |
22398 | let back_blue_bytes = self.back_blue.serialize(); |
22399 | let mut request0 = vec![ |
22400 | RECOLOR_CURSOR_REQUEST, |
22401 | 0, |
22402 | 0, |
22403 | 0, |
22404 | cursor_bytes[0], |
22405 | cursor_bytes[1], |
22406 | cursor_bytes[2], |
22407 | cursor_bytes[3], |
22408 | fore_red_bytes[0], |
22409 | fore_red_bytes[1], |
22410 | fore_green_bytes[0], |
22411 | fore_green_bytes[1], |
22412 | fore_blue_bytes[0], |
22413 | fore_blue_bytes[1], |
22414 | back_red_bytes[0], |
22415 | back_red_bytes[1], |
22416 | back_green_bytes[0], |
22417 | back_green_bytes[1], |
22418 | back_blue_bytes[0], |
22419 | back_blue_bytes[1], |
22420 | ]; |
22421 | let length_so_far = length_so_far + request0.len(); |
22422 | assert_eq!(length_so_far % 4, 0); |
22423 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
22424 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
22425 | (vec![request0.into()], vec![]) |
22426 | } |
22427 | /// Parse this request given its header, its body, and any fds that go along with it |
22428 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
22429 | if header.major_opcode != RECOLOR_CURSOR_REQUEST { |
22430 | return Err(ParseError::InvalidValue); |
22431 | } |
22432 | let remaining = &[header.minor_opcode]; |
22433 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
22434 | let _ = remaining; |
22435 | let (cursor, remaining) = Cursor::try_parse(value)?; |
22436 | let (fore_red, remaining) = u16::try_parse(remaining)?; |
22437 | let (fore_green, remaining) = u16::try_parse(remaining)?; |
22438 | let (fore_blue, remaining) = u16::try_parse(remaining)?; |
22439 | let (back_red, remaining) = u16::try_parse(remaining)?; |
22440 | let (back_green, remaining) = u16::try_parse(remaining)?; |
22441 | let (back_blue, remaining) = u16::try_parse(remaining)?; |
22442 | let _ = remaining; |
22443 | Ok(RecolorCursorRequest { |
22444 | cursor, |
22445 | fore_red, |
22446 | fore_green, |
22447 | fore_blue, |
22448 | back_red, |
22449 | back_green, |
22450 | back_blue, |
22451 | }) |
22452 | } |
22453 | } |
22454 | impl Request for RecolorCursorRequest { |
22455 | const EXTENSION_NAME: Option<&'static str> = None; |
22456 | |
22457 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
22458 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
22459 | // Flatten the buffers into a single vector |
22460 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
22461 | (buf, fds) |
22462 | } |
22463 | } |
22464 | impl crate::x11_utils::VoidRequest for RecolorCursorRequest { |
22465 | } |
22466 | |
22467 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
22468 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
22469 | pub struct QueryShapeOf(u8); |
22470 | impl QueryShapeOf { |
22471 | pub const LARGEST_CURSOR: Self = Self(0); |
22472 | pub const FASTEST_TILE: Self = Self(1); |
22473 | pub const FASTEST_STIPPLE: Self = Self(2); |
22474 | } |
22475 | impl From<QueryShapeOf> for u8 { |
22476 | #[inline ] |
22477 | fn from(input: QueryShapeOf) -> Self { |
22478 | input.0 |
22479 | } |
22480 | } |
22481 | impl From<QueryShapeOf> for Option<u8> { |
22482 | #[inline ] |
22483 | fn from(input: QueryShapeOf) -> Self { |
22484 | Some(input.0) |
22485 | } |
22486 | } |
22487 | impl From<QueryShapeOf> for u16 { |
22488 | #[inline ] |
22489 | fn from(input: QueryShapeOf) -> Self { |
22490 | u16::from(input.0) |
22491 | } |
22492 | } |
22493 | impl From<QueryShapeOf> for Option<u16> { |
22494 | #[inline ] |
22495 | fn from(input: QueryShapeOf) -> Self { |
22496 | Some(u16::from(input.0)) |
22497 | } |
22498 | } |
22499 | impl From<QueryShapeOf> for u32 { |
22500 | #[inline ] |
22501 | fn from(input: QueryShapeOf) -> Self { |
22502 | u32::from(input.0) |
22503 | } |
22504 | } |
22505 | impl From<QueryShapeOf> for Option<u32> { |
22506 | #[inline ] |
22507 | fn from(input: QueryShapeOf) -> Self { |
22508 | Some(u32::from(input.0)) |
22509 | } |
22510 | } |
22511 | impl From<u8> for QueryShapeOf { |
22512 | #[inline ] |
22513 | fn from(value: u8) -> Self { |
22514 | Self(value) |
22515 | } |
22516 | } |
22517 | impl core::fmt::Debug for QueryShapeOf { |
22518 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
22519 | let variants: [(u32, &str, &str); 3] = [ |
22520 | (Self::LARGEST_CURSOR.0.into(), "LARGEST_CURSOR" , "LargestCursor" ), |
22521 | (Self::FASTEST_TILE.0.into(), "FASTEST_TILE" , "FastestTile" ), |
22522 | (Self::FASTEST_STIPPLE.0.into(), "FASTEST_STIPPLE" , "FastestStipple" ), |
22523 | ]; |
22524 | pretty_print_enum(fmt, self.0.into(), &variants) |
22525 | } |
22526 | } |
22527 | |
22528 | /// Opcode for the QueryBestSize request |
22529 | pub const QUERY_BEST_SIZE_REQUEST: u8 = 97; |
22530 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
22531 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
22532 | pub struct QueryBestSizeRequest { |
22533 | pub class: QueryShapeOf, |
22534 | pub drawable: Drawable, |
22535 | pub width: u16, |
22536 | pub height: u16, |
22537 | } |
22538 | impl QueryBestSizeRequest { |
22539 | /// Serialize this request into bytes for the provided connection |
22540 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
22541 | let length_so_far = 0; |
22542 | let class_bytes = u8::from(self.class).serialize(); |
22543 | let drawable_bytes = self.drawable.serialize(); |
22544 | let width_bytes = self.width.serialize(); |
22545 | let height_bytes = self.height.serialize(); |
22546 | let mut request0 = vec![ |
22547 | QUERY_BEST_SIZE_REQUEST, |
22548 | class_bytes[0], |
22549 | 0, |
22550 | 0, |
22551 | drawable_bytes[0], |
22552 | drawable_bytes[1], |
22553 | drawable_bytes[2], |
22554 | drawable_bytes[3], |
22555 | width_bytes[0], |
22556 | width_bytes[1], |
22557 | height_bytes[0], |
22558 | height_bytes[1], |
22559 | ]; |
22560 | let length_so_far = length_so_far + request0.len(); |
22561 | assert_eq!(length_so_far % 4, 0); |
22562 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
22563 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
22564 | (vec![request0.into()], vec![]) |
22565 | } |
22566 | /// Parse this request given its header, its body, and any fds that go along with it |
22567 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
22568 | if header.major_opcode != QUERY_BEST_SIZE_REQUEST { |
22569 | return Err(ParseError::InvalidValue); |
22570 | } |
22571 | let remaining = &[header.minor_opcode]; |
22572 | let (class, remaining) = u8::try_parse(remaining)?; |
22573 | let class = class.into(); |
22574 | let _ = remaining; |
22575 | let (drawable, remaining) = Drawable::try_parse(value)?; |
22576 | let (width, remaining) = u16::try_parse(remaining)?; |
22577 | let (height, remaining) = u16::try_parse(remaining)?; |
22578 | let _ = remaining; |
22579 | Ok(QueryBestSizeRequest { |
22580 | class, |
22581 | drawable, |
22582 | width, |
22583 | height, |
22584 | }) |
22585 | } |
22586 | } |
22587 | impl Request for QueryBestSizeRequest { |
22588 | const EXTENSION_NAME: Option<&'static str> = None; |
22589 | |
22590 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
22591 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
22592 | // Flatten the buffers into a single vector |
22593 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
22594 | (buf, fds) |
22595 | } |
22596 | } |
22597 | impl crate::x11_utils::ReplyRequest for QueryBestSizeRequest { |
22598 | type Reply = QueryBestSizeReply; |
22599 | } |
22600 | |
22601 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
22602 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
22603 | pub struct QueryBestSizeReply { |
22604 | pub sequence: u16, |
22605 | pub length: u32, |
22606 | pub width: u16, |
22607 | pub height: u16, |
22608 | } |
22609 | impl TryParse for QueryBestSizeReply { |
22610 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
22611 | let remaining: &[u8] = initial_value; |
22612 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
22613 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
22614 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
22615 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
22616 | let (width: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
22617 | let (height: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
22618 | if response_type != 1 { |
22619 | return Err(ParseError::InvalidValue); |
22620 | } |
22621 | let result: QueryBestSizeReply = QueryBestSizeReply { sequence, length, width, height }; |
22622 | let _ = remaining; |
22623 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
22624 | .ok_or(err:ParseError::InsufficientData)?; |
22625 | Ok((result, remaining)) |
22626 | } |
22627 | } |
22628 | impl Serialize for QueryBestSizeReply { |
22629 | type Bytes = [u8; 12]; |
22630 | fn serialize(&self) -> [u8; 12] { |
22631 | let response_type_bytes = &[1]; |
22632 | let sequence_bytes = self.sequence.serialize(); |
22633 | let length_bytes = self.length.serialize(); |
22634 | let width_bytes = self.width.serialize(); |
22635 | let height_bytes = self.height.serialize(); |
22636 | [ |
22637 | response_type_bytes[0], |
22638 | 0, |
22639 | sequence_bytes[0], |
22640 | sequence_bytes[1], |
22641 | length_bytes[0], |
22642 | length_bytes[1], |
22643 | length_bytes[2], |
22644 | length_bytes[3], |
22645 | width_bytes[0], |
22646 | width_bytes[1], |
22647 | height_bytes[0], |
22648 | height_bytes[1], |
22649 | ] |
22650 | } |
22651 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
22652 | bytes.reserve(12); |
22653 | let response_type_bytes = &[1]; |
22654 | bytes.push(response_type_bytes[0]); |
22655 | bytes.extend_from_slice(&[0; 1]); |
22656 | self.sequence.serialize_into(bytes); |
22657 | self.length.serialize_into(bytes); |
22658 | self.width.serialize_into(bytes); |
22659 | self.height.serialize_into(bytes); |
22660 | } |
22661 | } |
22662 | |
22663 | /// Opcode for the QueryExtension request |
22664 | pub const QUERY_EXTENSION_REQUEST: u8 = 98; |
22665 | /// check if extension is present. |
22666 | /// |
22667 | /// Determines if the specified extension is present on this X11 server. |
22668 | /// |
22669 | /// Every extension has a unique `major_opcode` to identify requests, the minor |
22670 | /// opcodes and request formats are extension-specific. If the extension provides |
22671 | /// events and errors, the `first_event` and `first_error` fields in the reply are |
22672 | /// set accordingly. |
22673 | /// |
22674 | /// There should rarely be a need to use this request directly, XCB provides the |
22675 | /// `xcb_get_extension_data` function instead. |
22676 | /// |
22677 | /// # Fields |
22678 | /// |
22679 | /// * `name` - The name of the extension to query, for example "RANDR". This is case |
22680 | /// sensitive! |
22681 | /// |
22682 | /// # See |
22683 | /// |
22684 | /// * `xdpyinfo`: program |
22685 | /// * `xcb_get_extension_data`: function |
22686 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
22687 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
22688 | pub struct QueryExtensionRequest<'input> { |
22689 | pub name: Cow<'input, [u8]>, |
22690 | } |
22691 | impl<'input> QueryExtensionRequest<'input> { |
22692 | /// Serialize this request into bytes for the provided connection |
22693 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
22694 | let length_so_far = 0; |
22695 | let name_len = u16::try_from(self.name.len()).expect("`name` has too many elements" ); |
22696 | let name_len_bytes = name_len.serialize(); |
22697 | let mut request0 = vec![ |
22698 | QUERY_EXTENSION_REQUEST, |
22699 | 0, |
22700 | 0, |
22701 | 0, |
22702 | name_len_bytes[0], |
22703 | name_len_bytes[1], |
22704 | 0, |
22705 | 0, |
22706 | ]; |
22707 | let length_so_far = length_so_far + request0.len(); |
22708 | let length_so_far = length_so_far + self.name.len(); |
22709 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
22710 | let length_so_far = length_so_far + padding0.len(); |
22711 | assert_eq!(length_so_far % 4, 0); |
22712 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
22713 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
22714 | (vec![request0.into(), self.name, padding0.into()], vec![]) |
22715 | } |
22716 | /// Parse this request given its header, its body, and any fds that go along with it |
22717 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
22718 | if header.major_opcode != QUERY_EXTENSION_REQUEST { |
22719 | return Err(ParseError::InvalidValue); |
22720 | } |
22721 | let remaining = &[header.minor_opcode]; |
22722 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
22723 | let _ = remaining; |
22724 | let (name_len, remaining) = u16::try_parse(value)?; |
22725 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
22726 | let (name, remaining) = crate::x11_utils::parse_u8_list(remaining, name_len.try_to_usize()?)?; |
22727 | let _ = remaining; |
22728 | Ok(QueryExtensionRequest { |
22729 | name: Cow::Borrowed(name), |
22730 | }) |
22731 | } |
22732 | /// Clone all borrowed data in this QueryExtensionRequest. |
22733 | pub fn into_owned(self) -> QueryExtensionRequest<'static> { |
22734 | QueryExtensionRequest { |
22735 | name: Cow::Owned(self.name.into_owned()), |
22736 | } |
22737 | } |
22738 | } |
22739 | impl<'input> Request for QueryExtensionRequest<'input> { |
22740 | const EXTENSION_NAME: Option<&'static str> = None; |
22741 | |
22742 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
22743 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
22744 | // Flatten the buffers into a single vector |
22745 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
22746 | (buf, fds) |
22747 | } |
22748 | } |
22749 | impl<'input> crate::x11_utils::ReplyRequest for QueryExtensionRequest<'input> { |
22750 | type Reply = QueryExtensionReply; |
22751 | } |
22752 | |
22753 | /// # Fields |
22754 | /// |
22755 | /// * `present` - Whether the extension is present on this X11 server. |
22756 | /// * `major_opcode` - The major opcode for requests. |
22757 | /// * `first_event` - The first event code, if any. |
22758 | /// * `first_error` - The first error code, if any. |
22759 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
22760 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
22761 | pub struct QueryExtensionReply { |
22762 | pub sequence: u16, |
22763 | pub length: u32, |
22764 | pub present: bool, |
22765 | pub major_opcode: u8, |
22766 | pub first_event: u8, |
22767 | pub first_error: u8, |
22768 | } |
22769 | impl TryParse for QueryExtensionReply { |
22770 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
22771 | let remaining: &[u8] = initial_value; |
22772 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
22773 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
22774 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
22775 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
22776 | let (present: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
22777 | let (major_opcode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
22778 | let (first_event: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
22779 | let (first_error: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
22780 | if response_type != 1 { |
22781 | return Err(ParseError::InvalidValue); |
22782 | } |
22783 | let result: QueryExtensionReply = QueryExtensionReply { sequence, length, present, major_opcode, first_event, first_error }; |
22784 | let _ = remaining; |
22785 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
22786 | .ok_or(err:ParseError::InsufficientData)?; |
22787 | Ok((result, remaining)) |
22788 | } |
22789 | } |
22790 | impl Serialize for QueryExtensionReply { |
22791 | type Bytes = [u8; 12]; |
22792 | fn serialize(&self) -> [u8; 12] { |
22793 | let response_type_bytes = &[1]; |
22794 | let sequence_bytes = self.sequence.serialize(); |
22795 | let length_bytes = self.length.serialize(); |
22796 | let present_bytes = self.present.serialize(); |
22797 | let major_opcode_bytes = self.major_opcode.serialize(); |
22798 | let first_event_bytes = self.first_event.serialize(); |
22799 | let first_error_bytes = self.first_error.serialize(); |
22800 | [ |
22801 | response_type_bytes[0], |
22802 | 0, |
22803 | sequence_bytes[0], |
22804 | sequence_bytes[1], |
22805 | length_bytes[0], |
22806 | length_bytes[1], |
22807 | length_bytes[2], |
22808 | length_bytes[3], |
22809 | present_bytes[0], |
22810 | major_opcode_bytes[0], |
22811 | first_event_bytes[0], |
22812 | first_error_bytes[0], |
22813 | ] |
22814 | } |
22815 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
22816 | bytes.reserve(12); |
22817 | let response_type_bytes = &[1]; |
22818 | bytes.push(response_type_bytes[0]); |
22819 | bytes.extend_from_slice(&[0; 1]); |
22820 | self.sequence.serialize_into(bytes); |
22821 | self.length.serialize_into(bytes); |
22822 | self.present.serialize_into(bytes); |
22823 | self.major_opcode.serialize_into(bytes); |
22824 | self.first_event.serialize_into(bytes); |
22825 | self.first_error.serialize_into(bytes); |
22826 | } |
22827 | } |
22828 | |
22829 | /// Opcode for the ListExtensions request |
22830 | pub const LIST_EXTENSIONS_REQUEST: u8 = 99; |
22831 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
22832 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
22833 | pub struct ListExtensionsRequest; |
22834 | impl ListExtensionsRequest { |
22835 | /// Serialize this request into bytes for the provided connection |
22836 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
22837 | let length_so_far = 0; |
22838 | let mut request0 = vec![ |
22839 | LIST_EXTENSIONS_REQUEST, |
22840 | 0, |
22841 | 0, |
22842 | 0, |
22843 | ]; |
22844 | let length_so_far = length_so_far + request0.len(); |
22845 | assert_eq!(length_so_far % 4, 0); |
22846 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
22847 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
22848 | (vec![request0.into()], vec![]) |
22849 | } |
22850 | /// Parse this request given its header, its body, and any fds that go along with it |
22851 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
22852 | if header.major_opcode != LIST_EXTENSIONS_REQUEST { |
22853 | return Err(ParseError::InvalidValue); |
22854 | } |
22855 | let remaining = &[header.minor_opcode]; |
22856 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
22857 | let _ = remaining; |
22858 | let _ = value; |
22859 | Ok(ListExtensionsRequest |
22860 | ) |
22861 | } |
22862 | } |
22863 | impl Request for ListExtensionsRequest { |
22864 | const EXTENSION_NAME: Option<&'static str> = None; |
22865 | |
22866 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
22867 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
22868 | // Flatten the buffers into a single vector |
22869 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
22870 | (buf, fds) |
22871 | } |
22872 | } |
22873 | impl crate::x11_utils::ReplyRequest for ListExtensionsRequest { |
22874 | type Reply = ListExtensionsReply; |
22875 | } |
22876 | |
22877 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
22878 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
22879 | pub struct ListExtensionsReply { |
22880 | pub sequence: u16, |
22881 | pub length: u32, |
22882 | pub names: Vec<Str>, |
22883 | } |
22884 | impl TryParse for ListExtensionsReply { |
22885 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
22886 | let remaining: &[u8] = initial_value; |
22887 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
22888 | let (names_len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
22889 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
22890 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
22891 | let remaining: &[u8] = remaining.get(24..).ok_or(err:ParseError::InsufficientData)?; |
22892 | let (names: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<Str>(data:remaining, list_length:names_len.try_to_usize()?)?; |
22893 | if response_type != 1 { |
22894 | return Err(ParseError::InvalidValue); |
22895 | } |
22896 | let result: ListExtensionsReply = ListExtensionsReply { sequence, length, names }; |
22897 | let _ = remaining; |
22898 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
22899 | .ok_or(err:ParseError::InsufficientData)?; |
22900 | Ok((result, remaining)) |
22901 | } |
22902 | } |
22903 | impl Serialize for ListExtensionsReply { |
22904 | type Bytes = Vec<u8>; |
22905 | fn serialize(&self) -> Vec<u8> { |
22906 | let mut result: Vec = Vec::new(); |
22907 | self.serialize_into(&mut result); |
22908 | result |
22909 | } |
22910 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
22911 | bytes.reserve(additional:32); |
22912 | let response_type_bytes: &[u8; 1] = &[1]; |
22913 | bytes.push(response_type_bytes[0]); |
22914 | let names_len: u8 = u8::try_from(self.names.len()).expect(msg:"`names` has too many elements" ); |
22915 | names_len.serialize_into(bytes); |
22916 | self.sequence.serialize_into(bytes); |
22917 | self.length.serialize_into(bytes); |
22918 | bytes.extend_from_slice(&[0; 24]); |
22919 | self.names.serialize_into(bytes); |
22920 | } |
22921 | } |
22922 | impl ListExtensionsReply { |
22923 | /// Get the value of the `names_len` field. |
22924 | /// |
22925 | /// The `names_len` field is used as the length field of the `names` field. |
22926 | /// This function computes the field's value again based on the length of the list. |
22927 | /// |
22928 | /// # Panics |
22929 | /// |
22930 | /// Panics if the value cannot be represented in the target type. This |
22931 | /// cannot happen with values of the struct received from the X11 server. |
22932 | pub fn names_len(&self) -> u8 { |
22933 | self.names.len() |
22934 | .try_into().unwrap() |
22935 | } |
22936 | } |
22937 | |
22938 | /// Opcode for the ChangeKeyboardMapping request |
22939 | pub const CHANGE_KEYBOARD_MAPPING_REQUEST: u8 = 100; |
22940 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
22941 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
22942 | pub struct ChangeKeyboardMappingRequest<'input> { |
22943 | pub keycode_count: u8, |
22944 | pub first_keycode: Keycode, |
22945 | pub keysyms_per_keycode: u8, |
22946 | pub keysyms: Cow<'input, [Keysym]>, |
22947 | } |
22948 | impl<'input> ChangeKeyboardMappingRequest<'input> { |
22949 | /// Serialize this request into bytes for the provided connection |
22950 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
22951 | let length_so_far = 0; |
22952 | let keycode_count_bytes = self.keycode_count.serialize(); |
22953 | let first_keycode_bytes = self.first_keycode.serialize(); |
22954 | let keysyms_per_keycode_bytes = self.keysyms_per_keycode.serialize(); |
22955 | let mut request0 = vec![ |
22956 | CHANGE_KEYBOARD_MAPPING_REQUEST, |
22957 | keycode_count_bytes[0], |
22958 | 0, |
22959 | 0, |
22960 | first_keycode_bytes[0], |
22961 | keysyms_per_keycode_bytes[0], |
22962 | 0, |
22963 | 0, |
22964 | ]; |
22965 | let length_so_far = length_so_far + request0.len(); |
22966 | assert_eq!(self.keysyms.len(), usize::try_from(u32::from(self.keycode_count).checked_mul(u32::from(self.keysyms_per_keycode)).unwrap()).unwrap(), "`keysyms` has an incorrect length" ); |
22967 | let keysyms_bytes = self.keysyms.serialize(); |
22968 | let length_so_far = length_so_far + keysyms_bytes.len(); |
22969 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
22970 | let length_so_far = length_so_far + padding0.len(); |
22971 | assert_eq!(length_so_far % 4, 0); |
22972 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
22973 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
22974 | (vec![request0.into(), keysyms_bytes.into(), padding0.into()], vec![]) |
22975 | } |
22976 | /// Parse this request given its header, its body, and any fds that go along with it |
22977 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
22978 | if header.major_opcode != CHANGE_KEYBOARD_MAPPING_REQUEST { |
22979 | return Err(ParseError::InvalidValue); |
22980 | } |
22981 | let remaining = &[header.minor_opcode]; |
22982 | let (keycode_count, remaining) = u8::try_parse(remaining)?; |
22983 | let _ = remaining; |
22984 | let (first_keycode, remaining) = Keycode::try_parse(value)?; |
22985 | let (keysyms_per_keycode, remaining) = u8::try_parse(remaining)?; |
22986 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
22987 | let (keysyms, remaining) = crate::x11_utils::parse_list::<Keysym>(remaining, u32::from(keycode_count).checked_mul(u32::from(keysyms_per_keycode)).ok_or(ParseError::InvalidExpression)?.try_to_usize()?)?; |
22988 | let _ = remaining; |
22989 | Ok(ChangeKeyboardMappingRequest { |
22990 | keycode_count, |
22991 | first_keycode, |
22992 | keysyms_per_keycode, |
22993 | keysyms: Cow::Owned(keysyms), |
22994 | }) |
22995 | } |
22996 | /// Clone all borrowed data in this ChangeKeyboardMappingRequest. |
22997 | pub fn into_owned(self) -> ChangeKeyboardMappingRequest<'static> { |
22998 | ChangeKeyboardMappingRequest { |
22999 | keycode_count: self.keycode_count, |
23000 | first_keycode: self.first_keycode, |
23001 | keysyms_per_keycode: self.keysyms_per_keycode, |
23002 | keysyms: Cow::Owned(self.keysyms.into_owned()), |
23003 | } |
23004 | } |
23005 | } |
23006 | impl<'input> Request for ChangeKeyboardMappingRequest<'input> { |
23007 | const EXTENSION_NAME: Option<&'static str> = None; |
23008 | |
23009 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
23010 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
23011 | // Flatten the buffers into a single vector |
23012 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
23013 | (buf, fds) |
23014 | } |
23015 | } |
23016 | impl<'input> crate::x11_utils::VoidRequest for ChangeKeyboardMappingRequest<'input> { |
23017 | } |
23018 | |
23019 | /// Opcode for the GetKeyboardMapping request |
23020 | pub const GET_KEYBOARD_MAPPING_REQUEST: u8 = 101; |
23021 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
23022 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
23023 | pub struct GetKeyboardMappingRequest { |
23024 | pub first_keycode: Keycode, |
23025 | pub count: u8, |
23026 | } |
23027 | impl GetKeyboardMappingRequest { |
23028 | /// Serialize this request into bytes for the provided connection |
23029 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
23030 | let length_so_far = 0; |
23031 | let first_keycode_bytes = self.first_keycode.serialize(); |
23032 | let count_bytes = self.count.serialize(); |
23033 | let mut request0 = vec![ |
23034 | GET_KEYBOARD_MAPPING_REQUEST, |
23035 | 0, |
23036 | 0, |
23037 | 0, |
23038 | first_keycode_bytes[0], |
23039 | count_bytes[0], |
23040 | 0, |
23041 | 0, |
23042 | ]; |
23043 | let length_so_far = length_so_far + request0.len(); |
23044 | assert_eq!(length_so_far % 4, 0); |
23045 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
23046 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
23047 | (vec![request0.into()], vec![]) |
23048 | } |
23049 | /// Parse this request given its header, its body, and any fds that go along with it |
23050 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
23051 | if header.major_opcode != GET_KEYBOARD_MAPPING_REQUEST { |
23052 | return Err(ParseError::InvalidValue); |
23053 | } |
23054 | let remaining = &[header.minor_opcode]; |
23055 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
23056 | let _ = remaining; |
23057 | let (first_keycode, remaining) = Keycode::try_parse(value)?; |
23058 | let (count, remaining) = u8::try_parse(remaining)?; |
23059 | let _ = remaining; |
23060 | Ok(GetKeyboardMappingRequest { |
23061 | first_keycode, |
23062 | count, |
23063 | }) |
23064 | } |
23065 | } |
23066 | impl Request for GetKeyboardMappingRequest { |
23067 | const EXTENSION_NAME: Option<&'static str> = None; |
23068 | |
23069 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
23070 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
23071 | // Flatten the buffers into a single vector |
23072 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
23073 | (buf, fds) |
23074 | } |
23075 | } |
23076 | impl crate::x11_utils::ReplyRequest for GetKeyboardMappingRequest { |
23077 | type Reply = GetKeyboardMappingReply; |
23078 | } |
23079 | |
23080 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
23081 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
23082 | pub struct GetKeyboardMappingReply { |
23083 | pub keysyms_per_keycode: u8, |
23084 | pub sequence: u16, |
23085 | pub keysyms: Vec<Keysym>, |
23086 | } |
23087 | impl TryParse for GetKeyboardMappingReply { |
23088 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
23089 | let remaining: &[u8] = initial_value; |
23090 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
23091 | let (keysyms_per_keycode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
23092 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
23093 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
23094 | let remaining: &[u8] = remaining.get(24..).ok_or(err:ParseError::InsufficientData)?; |
23095 | let (keysyms: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<Keysym>(data:remaining, list_length:length.try_to_usize()?)?; |
23096 | if response_type != 1 { |
23097 | return Err(ParseError::InvalidValue); |
23098 | } |
23099 | let result: GetKeyboardMappingReply = GetKeyboardMappingReply { keysyms_per_keycode, sequence, keysyms }; |
23100 | let _ = remaining; |
23101 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
23102 | .ok_or(err:ParseError::InsufficientData)?; |
23103 | Ok((result, remaining)) |
23104 | } |
23105 | } |
23106 | impl Serialize for GetKeyboardMappingReply { |
23107 | type Bytes = Vec<u8>; |
23108 | fn serialize(&self) -> Vec<u8> { |
23109 | let mut result: Vec = Vec::new(); |
23110 | self.serialize_into(&mut result); |
23111 | result |
23112 | } |
23113 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
23114 | bytes.reserve(additional:32); |
23115 | let response_type_bytes: &[u8; 1] = &[1]; |
23116 | bytes.push(response_type_bytes[0]); |
23117 | self.keysyms_per_keycode.serialize_into(bytes); |
23118 | self.sequence.serialize_into(bytes); |
23119 | let length: u32 = u32::try_from(self.keysyms.len()).expect(msg:"`keysyms` has too many elements" ); |
23120 | length.serialize_into(bytes); |
23121 | bytes.extend_from_slice(&[0; 24]); |
23122 | self.keysyms.serialize_into(bytes); |
23123 | } |
23124 | } |
23125 | impl GetKeyboardMappingReply { |
23126 | /// Get the value of the `length` field. |
23127 | /// |
23128 | /// The `length` field is used as the length field of the `keysyms` field. |
23129 | /// This function computes the field's value again based on the length of the list. |
23130 | /// |
23131 | /// # Panics |
23132 | /// |
23133 | /// Panics if the value cannot be represented in the target type. This |
23134 | /// cannot happen with values of the struct received from the X11 server. |
23135 | pub fn length(&self) -> u32 { |
23136 | self.keysyms.len() |
23137 | .try_into().unwrap() |
23138 | } |
23139 | } |
23140 | |
23141 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
23142 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
23143 | pub struct KB(u32); |
23144 | impl KB { |
23145 | pub const KEY_CLICK_PERCENT: Self = Self(1 << 0); |
23146 | pub const BELL_PERCENT: Self = Self(1 << 1); |
23147 | pub const BELL_PITCH: Self = Self(1 << 2); |
23148 | pub const BELL_DURATION: Self = Self(1 << 3); |
23149 | pub const LED: Self = Self(1 << 4); |
23150 | pub const LED_MODE: Self = Self(1 << 5); |
23151 | pub const KEY: Self = Self(1 << 6); |
23152 | pub const AUTO_REPEAT_MODE: Self = Self(1 << 7); |
23153 | } |
23154 | impl From<KB> for u32 { |
23155 | #[inline ] |
23156 | fn from(input: KB) -> Self { |
23157 | input.0 |
23158 | } |
23159 | } |
23160 | impl From<KB> for Option<u32> { |
23161 | #[inline ] |
23162 | fn from(input: KB) -> Self { |
23163 | Some(input.0) |
23164 | } |
23165 | } |
23166 | impl From<u8> for KB { |
23167 | #[inline ] |
23168 | fn from(value: u8) -> Self { |
23169 | Self(value.into()) |
23170 | } |
23171 | } |
23172 | impl From<u16> for KB { |
23173 | #[inline ] |
23174 | fn from(value: u16) -> Self { |
23175 | Self(value.into()) |
23176 | } |
23177 | } |
23178 | impl From<u32> for KB { |
23179 | #[inline ] |
23180 | fn from(value: u32) -> Self { |
23181 | Self(value) |
23182 | } |
23183 | } |
23184 | impl core::fmt::Debug for KB { |
23185 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
23186 | let variants: [(u32, &str, &str); 8] = [ |
23187 | (Self::KEY_CLICK_PERCENT.0, "KEY_CLICK_PERCENT" , "KeyClickPercent" ), |
23188 | (Self::BELL_PERCENT.0, "BELL_PERCENT" , "BellPercent" ), |
23189 | (Self::BELL_PITCH.0, "BELL_PITCH" , "BellPitch" ), |
23190 | (Self::BELL_DURATION.0, "BELL_DURATION" , "BellDuration" ), |
23191 | (Self::LED.0, "LED" , "Led" ), |
23192 | (Self::LED_MODE.0, "LED_MODE" , "LedMode" ), |
23193 | (Self::KEY.0, "KEY" , "Key" ), |
23194 | (Self::AUTO_REPEAT_MODE.0, "AUTO_REPEAT_MODE" , "AutoRepeatMode" ), |
23195 | ]; |
23196 | pretty_print_bitmask(fmt, self.0, &variants) |
23197 | } |
23198 | } |
23199 | bitmask_binop!(KB, u32); |
23200 | |
23201 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
23202 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
23203 | pub struct LedMode(u32); |
23204 | impl LedMode { |
23205 | pub const OFF: Self = Self(0); |
23206 | pub const ON: Self = Self(1); |
23207 | } |
23208 | impl From<LedMode> for u32 { |
23209 | #[inline ] |
23210 | fn from(input: LedMode) -> Self { |
23211 | input.0 |
23212 | } |
23213 | } |
23214 | impl From<LedMode> for Option<u32> { |
23215 | #[inline ] |
23216 | fn from(input: LedMode) -> Self { |
23217 | Some(input.0) |
23218 | } |
23219 | } |
23220 | impl From<u8> for LedMode { |
23221 | #[inline ] |
23222 | fn from(value: u8) -> Self { |
23223 | Self(value.into()) |
23224 | } |
23225 | } |
23226 | impl From<u16> for LedMode { |
23227 | #[inline ] |
23228 | fn from(value: u16) -> Self { |
23229 | Self(value.into()) |
23230 | } |
23231 | } |
23232 | impl From<u32> for LedMode { |
23233 | #[inline ] |
23234 | fn from(value: u32) -> Self { |
23235 | Self(value) |
23236 | } |
23237 | } |
23238 | impl core::fmt::Debug for LedMode { |
23239 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
23240 | let variants: [(u32, &str, &str); 2] = [ |
23241 | (Self::OFF.0, "OFF" , "Off" ), |
23242 | (Self::ON.0, "ON" , "On" ), |
23243 | ]; |
23244 | pretty_print_enum(fmt, self.0, &variants) |
23245 | } |
23246 | } |
23247 | |
23248 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
23249 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
23250 | pub struct AutoRepeatMode(u32); |
23251 | impl AutoRepeatMode { |
23252 | pub const OFF: Self = Self(0); |
23253 | pub const ON: Self = Self(1); |
23254 | pub const DEFAULT: Self = Self(2); |
23255 | } |
23256 | impl From<AutoRepeatMode> for u32 { |
23257 | #[inline ] |
23258 | fn from(input: AutoRepeatMode) -> Self { |
23259 | input.0 |
23260 | } |
23261 | } |
23262 | impl From<AutoRepeatMode> for Option<u32> { |
23263 | #[inline ] |
23264 | fn from(input: AutoRepeatMode) -> Self { |
23265 | Some(input.0) |
23266 | } |
23267 | } |
23268 | impl From<u8> for AutoRepeatMode { |
23269 | #[inline ] |
23270 | fn from(value: u8) -> Self { |
23271 | Self(value.into()) |
23272 | } |
23273 | } |
23274 | impl From<u16> for AutoRepeatMode { |
23275 | #[inline ] |
23276 | fn from(value: u16) -> Self { |
23277 | Self(value.into()) |
23278 | } |
23279 | } |
23280 | impl From<u32> for AutoRepeatMode { |
23281 | #[inline ] |
23282 | fn from(value: u32) -> Self { |
23283 | Self(value) |
23284 | } |
23285 | } |
23286 | impl core::fmt::Debug for AutoRepeatMode { |
23287 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
23288 | let variants: [(u32, &str, &str); 3] = [ |
23289 | (Self::OFF.0, "OFF" , "Off" ), |
23290 | (Self::ON.0, "ON" , "On" ), |
23291 | (Self::DEFAULT.0, "DEFAULT" , "Default" ), |
23292 | ]; |
23293 | pretty_print_enum(fmt, self.0, &variants) |
23294 | } |
23295 | } |
23296 | |
23297 | /// Auxiliary and optional information for the `change_keyboard_control` function |
23298 | #[derive (Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] |
23299 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
23300 | pub struct ChangeKeyboardControlAux { |
23301 | pub key_click_percent: Option<i32>, |
23302 | pub bell_percent: Option<i32>, |
23303 | pub bell_pitch: Option<i32>, |
23304 | pub bell_duration: Option<i32>, |
23305 | pub led: Option<u32>, |
23306 | pub led_mode: Option<LedMode>, |
23307 | pub key: Option<Keycode32>, |
23308 | pub auto_repeat_mode: Option<AutoRepeatMode>, |
23309 | } |
23310 | impl ChangeKeyboardControlAux { |
23311 | fn try_parse(value: &[u8], value_mask: u32) -> Result<(Self, &[u8]), ParseError> { |
23312 | let switch_expr = u32::from(value_mask); |
23313 | let mut outer_remaining = value; |
23314 | let key_click_percent = if switch_expr & u32::from(KB::KEY_CLICK_PERCENT) != 0 { |
23315 | let remaining = outer_remaining; |
23316 | let (key_click_percent, remaining) = i32::try_parse(remaining)?; |
23317 | outer_remaining = remaining; |
23318 | Some(key_click_percent) |
23319 | } else { |
23320 | None |
23321 | }; |
23322 | let bell_percent = if switch_expr & u32::from(KB::BELL_PERCENT) != 0 { |
23323 | let remaining = outer_remaining; |
23324 | let (bell_percent, remaining) = i32::try_parse(remaining)?; |
23325 | outer_remaining = remaining; |
23326 | Some(bell_percent) |
23327 | } else { |
23328 | None |
23329 | }; |
23330 | let bell_pitch = if switch_expr & u32::from(KB::BELL_PITCH) != 0 { |
23331 | let remaining = outer_remaining; |
23332 | let (bell_pitch, remaining) = i32::try_parse(remaining)?; |
23333 | outer_remaining = remaining; |
23334 | Some(bell_pitch) |
23335 | } else { |
23336 | None |
23337 | }; |
23338 | let bell_duration = if switch_expr & u32::from(KB::BELL_DURATION) != 0 { |
23339 | let remaining = outer_remaining; |
23340 | let (bell_duration, remaining) = i32::try_parse(remaining)?; |
23341 | outer_remaining = remaining; |
23342 | Some(bell_duration) |
23343 | } else { |
23344 | None |
23345 | }; |
23346 | let led = if switch_expr & u32::from(KB::LED) != 0 { |
23347 | let remaining = outer_remaining; |
23348 | let (led, remaining) = u32::try_parse(remaining)?; |
23349 | outer_remaining = remaining; |
23350 | Some(led) |
23351 | } else { |
23352 | None |
23353 | }; |
23354 | let led_mode = if switch_expr & u32::from(KB::LED_MODE) != 0 { |
23355 | let remaining = outer_remaining; |
23356 | let (led_mode, remaining) = u32::try_parse(remaining)?; |
23357 | let led_mode = led_mode.into(); |
23358 | outer_remaining = remaining; |
23359 | Some(led_mode) |
23360 | } else { |
23361 | None |
23362 | }; |
23363 | let key = if switch_expr & u32::from(KB::KEY) != 0 { |
23364 | let remaining = outer_remaining; |
23365 | let (key, remaining) = Keycode32::try_parse(remaining)?; |
23366 | outer_remaining = remaining; |
23367 | Some(key) |
23368 | } else { |
23369 | None |
23370 | }; |
23371 | let auto_repeat_mode = if switch_expr & u32::from(KB::AUTO_REPEAT_MODE) != 0 { |
23372 | let remaining = outer_remaining; |
23373 | let (auto_repeat_mode, remaining) = u32::try_parse(remaining)?; |
23374 | let auto_repeat_mode = auto_repeat_mode.into(); |
23375 | outer_remaining = remaining; |
23376 | Some(auto_repeat_mode) |
23377 | } else { |
23378 | None |
23379 | }; |
23380 | let result = ChangeKeyboardControlAux { key_click_percent, bell_percent, bell_pitch, bell_duration, led, led_mode, key, auto_repeat_mode }; |
23381 | Ok((result, outer_remaining)) |
23382 | } |
23383 | } |
23384 | impl ChangeKeyboardControlAux { |
23385 | #[allow (dead_code)] |
23386 | fn serialize(&self, value_mask: u32) -> Vec<u8> { |
23387 | let mut result = Vec::new(); |
23388 | self.serialize_into(&mut result, u32::from(value_mask)); |
23389 | result |
23390 | } |
23391 | fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) { |
23392 | assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant" ); |
23393 | if let Some(key_click_percent) = self.key_click_percent { |
23394 | key_click_percent.serialize_into(bytes); |
23395 | } |
23396 | if let Some(bell_percent) = self.bell_percent { |
23397 | bell_percent.serialize_into(bytes); |
23398 | } |
23399 | if let Some(bell_pitch) = self.bell_pitch { |
23400 | bell_pitch.serialize_into(bytes); |
23401 | } |
23402 | if let Some(bell_duration) = self.bell_duration { |
23403 | bell_duration.serialize_into(bytes); |
23404 | } |
23405 | if let Some(led) = self.led { |
23406 | led.serialize_into(bytes); |
23407 | } |
23408 | if let Some(led_mode) = self.led_mode { |
23409 | u32::from(led_mode).serialize_into(bytes); |
23410 | } |
23411 | if let Some(key) = self.key { |
23412 | key.serialize_into(bytes); |
23413 | } |
23414 | if let Some(auto_repeat_mode) = self.auto_repeat_mode { |
23415 | u32::from(auto_repeat_mode).serialize_into(bytes); |
23416 | } |
23417 | } |
23418 | } |
23419 | impl ChangeKeyboardControlAux { |
23420 | fn switch_expr(&self) -> u32 { |
23421 | let mut expr_value = 0; |
23422 | if self.key_click_percent.is_some() { |
23423 | expr_value |= u32::from(KB::KEY_CLICK_PERCENT); |
23424 | } |
23425 | if self.bell_percent.is_some() { |
23426 | expr_value |= u32::from(KB::BELL_PERCENT); |
23427 | } |
23428 | if self.bell_pitch.is_some() { |
23429 | expr_value |= u32::from(KB::BELL_PITCH); |
23430 | } |
23431 | if self.bell_duration.is_some() { |
23432 | expr_value |= u32::from(KB::BELL_DURATION); |
23433 | } |
23434 | if self.led.is_some() { |
23435 | expr_value |= u32::from(KB::LED); |
23436 | } |
23437 | if self.led_mode.is_some() { |
23438 | expr_value |= u32::from(KB::LED_MODE); |
23439 | } |
23440 | if self.key.is_some() { |
23441 | expr_value |= u32::from(KB::KEY); |
23442 | } |
23443 | if self.auto_repeat_mode.is_some() { |
23444 | expr_value |= u32::from(KB::AUTO_REPEAT_MODE); |
23445 | } |
23446 | expr_value |
23447 | } |
23448 | } |
23449 | impl ChangeKeyboardControlAux { |
23450 | /// Create a new instance with all fields unset / not present. |
23451 | pub fn new() -> Self { |
23452 | Default::default() |
23453 | } |
23454 | /// Set the `key_click_percent` field of this structure. |
23455 | #[must_use ] |
23456 | pub fn key_click_percent<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
23457 | self.key_click_percent = value.into(); |
23458 | self |
23459 | } |
23460 | /// Set the `bell_percent` field of this structure. |
23461 | #[must_use ] |
23462 | pub fn bell_percent<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
23463 | self.bell_percent = value.into(); |
23464 | self |
23465 | } |
23466 | /// Set the `bell_pitch` field of this structure. |
23467 | #[must_use ] |
23468 | pub fn bell_pitch<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
23469 | self.bell_pitch = value.into(); |
23470 | self |
23471 | } |
23472 | /// Set the `bell_duration` field of this structure. |
23473 | #[must_use ] |
23474 | pub fn bell_duration<I>(mut self, value: I) -> Self where I: Into<Option<i32>> { |
23475 | self.bell_duration = value.into(); |
23476 | self |
23477 | } |
23478 | /// Set the `led` field of this structure. |
23479 | #[must_use ] |
23480 | pub fn led<I>(mut self, value: I) -> Self where I: Into<Option<u32>> { |
23481 | self.led = value.into(); |
23482 | self |
23483 | } |
23484 | /// Set the `led_mode` field of this structure. |
23485 | #[must_use ] |
23486 | pub fn led_mode<I>(mut self, value: I) -> Self where I: Into<Option<LedMode>> { |
23487 | self.led_mode = value.into(); |
23488 | self |
23489 | } |
23490 | /// Set the `key` field of this structure. |
23491 | #[must_use ] |
23492 | pub fn key<I>(mut self, value: I) -> Self where I: Into<Option<Keycode32>> { |
23493 | self.key = value.into(); |
23494 | self |
23495 | } |
23496 | /// Set the `auto_repeat_mode` field of this structure. |
23497 | #[must_use ] |
23498 | pub fn auto_repeat_mode<I>(mut self, value: I) -> Self where I: Into<Option<AutoRepeatMode>> { |
23499 | self.auto_repeat_mode = value.into(); |
23500 | self |
23501 | } |
23502 | } |
23503 | |
23504 | /// Opcode for the ChangeKeyboardControl request |
23505 | pub const CHANGE_KEYBOARD_CONTROL_REQUEST: u8 = 102; |
23506 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
23507 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
23508 | pub struct ChangeKeyboardControlRequest<'input> { |
23509 | pub value_list: Cow<'input, ChangeKeyboardControlAux>, |
23510 | } |
23511 | impl<'input> ChangeKeyboardControlRequest<'input> { |
23512 | /// Serialize this request into bytes for the provided connection |
23513 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
23514 | let length_so_far = 0; |
23515 | let value_mask: u32 = self.value_list.switch_expr(); |
23516 | let value_mask_bytes = value_mask.serialize(); |
23517 | let mut request0 = vec![ |
23518 | CHANGE_KEYBOARD_CONTROL_REQUEST, |
23519 | 0, |
23520 | 0, |
23521 | 0, |
23522 | value_mask_bytes[0], |
23523 | value_mask_bytes[1], |
23524 | value_mask_bytes[2], |
23525 | value_mask_bytes[3], |
23526 | ]; |
23527 | let length_so_far = length_so_far + request0.len(); |
23528 | let value_list_bytes = self.value_list.serialize(u32::from(value_mask)); |
23529 | let length_so_far = length_so_far + value_list_bytes.len(); |
23530 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
23531 | let length_so_far = length_so_far + padding0.len(); |
23532 | assert_eq!(length_so_far % 4, 0); |
23533 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
23534 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
23535 | (vec![request0.into(), value_list_bytes.into(), padding0.into()], vec![]) |
23536 | } |
23537 | /// Parse this request given its header, its body, and any fds that go along with it |
23538 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
23539 | if header.major_opcode != CHANGE_KEYBOARD_CONTROL_REQUEST { |
23540 | return Err(ParseError::InvalidValue); |
23541 | } |
23542 | let remaining = &[header.minor_opcode]; |
23543 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
23544 | let _ = remaining; |
23545 | let (value_mask, remaining) = u32::try_parse(value)?; |
23546 | let (value_list, remaining) = ChangeKeyboardControlAux::try_parse(remaining, u32::from(value_mask))?; |
23547 | let _ = remaining; |
23548 | Ok(ChangeKeyboardControlRequest { |
23549 | value_list: Cow::Owned(value_list), |
23550 | }) |
23551 | } |
23552 | /// Clone all borrowed data in this ChangeKeyboardControlRequest. |
23553 | pub fn into_owned(self) -> ChangeKeyboardControlRequest<'static> { |
23554 | ChangeKeyboardControlRequest { |
23555 | value_list: Cow::Owned(self.value_list.into_owned()), |
23556 | } |
23557 | } |
23558 | } |
23559 | impl<'input> Request for ChangeKeyboardControlRequest<'input> { |
23560 | const EXTENSION_NAME: Option<&'static str> = None; |
23561 | |
23562 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
23563 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
23564 | // Flatten the buffers into a single vector |
23565 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
23566 | (buf, fds) |
23567 | } |
23568 | } |
23569 | impl<'input> crate::x11_utils::VoidRequest for ChangeKeyboardControlRequest<'input> { |
23570 | } |
23571 | |
23572 | /// Opcode for the GetKeyboardControl request |
23573 | pub const GET_KEYBOARD_CONTROL_REQUEST: u8 = 103; |
23574 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
23575 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
23576 | pub struct GetKeyboardControlRequest; |
23577 | impl GetKeyboardControlRequest { |
23578 | /// Serialize this request into bytes for the provided connection |
23579 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
23580 | let length_so_far = 0; |
23581 | let mut request0 = vec![ |
23582 | GET_KEYBOARD_CONTROL_REQUEST, |
23583 | 0, |
23584 | 0, |
23585 | 0, |
23586 | ]; |
23587 | let length_so_far = length_so_far + request0.len(); |
23588 | assert_eq!(length_so_far % 4, 0); |
23589 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
23590 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
23591 | (vec![request0.into()], vec![]) |
23592 | } |
23593 | /// Parse this request given its header, its body, and any fds that go along with it |
23594 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
23595 | if header.major_opcode != GET_KEYBOARD_CONTROL_REQUEST { |
23596 | return Err(ParseError::InvalidValue); |
23597 | } |
23598 | let remaining = &[header.minor_opcode]; |
23599 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
23600 | let _ = remaining; |
23601 | let _ = value; |
23602 | Ok(GetKeyboardControlRequest |
23603 | ) |
23604 | } |
23605 | } |
23606 | impl Request for GetKeyboardControlRequest { |
23607 | const EXTENSION_NAME: Option<&'static str> = None; |
23608 | |
23609 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
23610 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
23611 | // Flatten the buffers into a single vector |
23612 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
23613 | (buf, fds) |
23614 | } |
23615 | } |
23616 | impl crate::x11_utils::ReplyRequest for GetKeyboardControlRequest { |
23617 | type Reply = GetKeyboardControlReply; |
23618 | } |
23619 | |
23620 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
23621 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
23622 | pub struct GetKeyboardControlReply { |
23623 | pub global_auto_repeat: AutoRepeatMode, |
23624 | pub sequence: u16, |
23625 | pub length: u32, |
23626 | pub led_mask: u32, |
23627 | pub key_click_percent: u8, |
23628 | pub bell_percent: u8, |
23629 | pub bell_pitch: u16, |
23630 | pub bell_duration: u16, |
23631 | pub auto_repeats: [u8; 32], |
23632 | } |
23633 | impl TryParse for GetKeyboardControlReply { |
23634 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
23635 | let remaining = initial_value; |
23636 | let (response_type, remaining) = u8::try_parse(remaining)?; |
23637 | let (global_auto_repeat, remaining) = u8::try_parse(remaining)?; |
23638 | let (sequence, remaining) = u16::try_parse(remaining)?; |
23639 | let (length, remaining) = u32::try_parse(remaining)?; |
23640 | let (led_mask, remaining) = u32::try_parse(remaining)?; |
23641 | let (key_click_percent, remaining) = u8::try_parse(remaining)?; |
23642 | let (bell_percent, remaining) = u8::try_parse(remaining)?; |
23643 | let (bell_pitch, remaining) = u16::try_parse(remaining)?; |
23644 | let (bell_duration, remaining) = u16::try_parse(remaining)?; |
23645 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
23646 | let (auto_repeats, remaining) = crate::x11_utils::parse_u8_array::<32>(remaining)?; |
23647 | if response_type != 1 { |
23648 | return Err(ParseError::InvalidValue); |
23649 | } |
23650 | let global_auto_repeat = global_auto_repeat.into(); |
23651 | let result = GetKeyboardControlReply { global_auto_repeat, sequence, length, led_mask, key_click_percent, bell_percent, bell_pitch, bell_duration, auto_repeats }; |
23652 | let _ = remaining; |
23653 | let remaining = initial_value.get(32 + length as usize * 4..) |
23654 | .ok_or(ParseError::InsufficientData)?; |
23655 | Ok((result, remaining)) |
23656 | } |
23657 | } |
23658 | impl Serialize for GetKeyboardControlReply { |
23659 | type Bytes = [u8; 52]; |
23660 | fn serialize(&self) -> [u8; 52] { |
23661 | let response_type_bytes = &[1]; |
23662 | let global_auto_repeat_bytes = (u32::from(self.global_auto_repeat) as u8).serialize(); |
23663 | let sequence_bytes = self.sequence.serialize(); |
23664 | let length_bytes = self.length.serialize(); |
23665 | let led_mask_bytes = self.led_mask.serialize(); |
23666 | let key_click_percent_bytes = self.key_click_percent.serialize(); |
23667 | let bell_percent_bytes = self.bell_percent.serialize(); |
23668 | let bell_pitch_bytes = self.bell_pitch.serialize(); |
23669 | let bell_duration_bytes = self.bell_duration.serialize(); |
23670 | [ |
23671 | response_type_bytes[0], |
23672 | global_auto_repeat_bytes[0], |
23673 | sequence_bytes[0], |
23674 | sequence_bytes[1], |
23675 | length_bytes[0], |
23676 | length_bytes[1], |
23677 | length_bytes[2], |
23678 | length_bytes[3], |
23679 | led_mask_bytes[0], |
23680 | led_mask_bytes[1], |
23681 | led_mask_bytes[2], |
23682 | led_mask_bytes[3], |
23683 | key_click_percent_bytes[0], |
23684 | bell_percent_bytes[0], |
23685 | bell_pitch_bytes[0], |
23686 | bell_pitch_bytes[1], |
23687 | bell_duration_bytes[0], |
23688 | bell_duration_bytes[1], |
23689 | 0, |
23690 | 0, |
23691 | self.auto_repeats[0], |
23692 | self.auto_repeats[1], |
23693 | self.auto_repeats[2], |
23694 | self.auto_repeats[3], |
23695 | self.auto_repeats[4], |
23696 | self.auto_repeats[5], |
23697 | self.auto_repeats[6], |
23698 | self.auto_repeats[7], |
23699 | self.auto_repeats[8], |
23700 | self.auto_repeats[9], |
23701 | self.auto_repeats[10], |
23702 | self.auto_repeats[11], |
23703 | self.auto_repeats[12], |
23704 | self.auto_repeats[13], |
23705 | self.auto_repeats[14], |
23706 | self.auto_repeats[15], |
23707 | self.auto_repeats[16], |
23708 | self.auto_repeats[17], |
23709 | self.auto_repeats[18], |
23710 | self.auto_repeats[19], |
23711 | self.auto_repeats[20], |
23712 | self.auto_repeats[21], |
23713 | self.auto_repeats[22], |
23714 | self.auto_repeats[23], |
23715 | self.auto_repeats[24], |
23716 | self.auto_repeats[25], |
23717 | self.auto_repeats[26], |
23718 | self.auto_repeats[27], |
23719 | self.auto_repeats[28], |
23720 | self.auto_repeats[29], |
23721 | self.auto_repeats[30], |
23722 | self.auto_repeats[31], |
23723 | ] |
23724 | } |
23725 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
23726 | bytes.reserve(52); |
23727 | let response_type_bytes = &[1]; |
23728 | bytes.push(response_type_bytes[0]); |
23729 | (u32::from(self.global_auto_repeat) as u8).serialize_into(bytes); |
23730 | self.sequence.serialize_into(bytes); |
23731 | self.length.serialize_into(bytes); |
23732 | self.led_mask.serialize_into(bytes); |
23733 | self.key_click_percent.serialize_into(bytes); |
23734 | self.bell_percent.serialize_into(bytes); |
23735 | self.bell_pitch.serialize_into(bytes); |
23736 | self.bell_duration.serialize_into(bytes); |
23737 | bytes.extend_from_slice(&[0; 2]); |
23738 | bytes.extend_from_slice(&self.auto_repeats); |
23739 | } |
23740 | } |
23741 | |
23742 | /// Opcode for the Bell request |
23743 | pub const BELL_REQUEST: u8 = 104; |
23744 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
23745 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
23746 | pub struct BellRequest { |
23747 | pub percent: i8, |
23748 | } |
23749 | impl BellRequest { |
23750 | /// Serialize this request into bytes for the provided connection |
23751 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
23752 | let length_so_far = 0; |
23753 | let percent_bytes = self.percent.serialize(); |
23754 | let mut request0 = vec![ |
23755 | BELL_REQUEST, |
23756 | percent_bytes[0], |
23757 | 0, |
23758 | 0, |
23759 | ]; |
23760 | let length_so_far = length_so_far + request0.len(); |
23761 | assert_eq!(length_so_far % 4, 0); |
23762 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
23763 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
23764 | (vec![request0.into()], vec![]) |
23765 | } |
23766 | /// Parse this request given its header, its body, and any fds that go along with it |
23767 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
23768 | if header.major_opcode != BELL_REQUEST { |
23769 | return Err(ParseError::InvalidValue); |
23770 | } |
23771 | let remaining = &[header.minor_opcode]; |
23772 | let (percent, remaining) = i8::try_parse(remaining)?; |
23773 | let _ = remaining; |
23774 | let _ = value; |
23775 | Ok(BellRequest { |
23776 | percent, |
23777 | }) |
23778 | } |
23779 | } |
23780 | impl Request for BellRequest { |
23781 | const EXTENSION_NAME: Option<&'static str> = None; |
23782 | |
23783 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
23784 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
23785 | // Flatten the buffers into a single vector |
23786 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
23787 | (buf, fds) |
23788 | } |
23789 | } |
23790 | impl crate::x11_utils::VoidRequest for BellRequest { |
23791 | } |
23792 | |
23793 | /// Opcode for the ChangePointerControl request |
23794 | pub const CHANGE_POINTER_CONTROL_REQUEST: u8 = 105; |
23795 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
23796 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
23797 | pub struct ChangePointerControlRequest { |
23798 | pub acceleration_numerator: i16, |
23799 | pub acceleration_denominator: i16, |
23800 | pub threshold: i16, |
23801 | pub do_acceleration: bool, |
23802 | pub do_threshold: bool, |
23803 | } |
23804 | impl ChangePointerControlRequest { |
23805 | /// Serialize this request into bytes for the provided connection |
23806 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
23807 | let length_so_far = 0; |
23808 | let acceleration_numerator_bytes = self.acceleration_numerator.serialize(); |
23809 | let acceleration_denominator_bytes = self.acceleration_denominator.serialize(); |
23810 | let threshold_bytes = self.threshold.serialize(); |
23811 | let do_acceleration_bytes = self.do_acceleration.serialize(); |
23812 | let do_threshold_bytes = self.do_threshold.serialize(); |
23813 | let mut request0 = vec![ |
23814 | CHANGE_POINTER_CONTROL_REQUEST, |
23815 | 0, |
23816 | 0, |
23817 | 0, |
23818 | acceleration_numerator_bytes[0], |
23819 | acceleration_numerator_bytes[1], |
23820 | acceleration_denominator_bytes[0], |
23821 | acceleration_denominator_bytes[1], |
23822 | threshold_bytes[0], |
23823 | threshold_bytes[1], |
23824 | do_acceleration_bytes[0], |
23825 | do_threshold_bytes[0], |
23826 | ]; |
23827 | let length_so_far = length_so_far + request0.len(); |
23828 | assert_eq!(length_so_far % 4, 0); |
23829 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
23830 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
23831 | (vec![request0.into()], vec![]) |
23832 | } |
23833 | /// Parse this request given its header, its body, and any fds that go along with it |
23834 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
23835 | if header.major_opcode != CHANGE_POINTER_CONTROL_REQUEST { |
23836 | return Err(ParseError::InvalidValue); |
23837 | } |
23838 | let remaining = &[header.minor_opcode]; |
23839 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
23840 | let _ = remaining; |
23841 | let (acceleration_numerator, remaining) = i16::try_parse(value)?; |
23842 | let (acceleration_denominator, remaining) = i16::try_parse(remaining)?; |
23843 | let (threshold, remaining) = i16::try_parse(remaining)?; |
23844 | let (do_acceleration, remaining) = bool::try_parse(remaining)?; |
23845 | let (do_threshold, remaining) = bool::try_parse(remaining)?; |
23846 | let _ = remaining; |
23847 | Ok(ChangePointerControlRequest { |
23848 | acceleration_numerator, |
23849 | acceleration_denominator, |
23850 | threshold, |
23851 | do_acceleration, |
23852 | do_threshold, |
23853 | }) |
23854 | } |
23855 | } |
23856 | impl Request for ChangePointerControlRequest { |
23857 | const EXTENSION_NAME: Option<&'static str> = None; |
23858 | |
23859 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
23860 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
23861 | // Flatten the buffers into a single vector |
23862 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
23863 | (buf, fds) |
23864 | } |
23865 | } |
23866 | impl crate::x11_utils::VoidRequest for ChangePointerControlRequest { |
23867 | } |
23868 | |
23869 | /// Opcode for the GetPointerControl request |
23870 | pub const GET_POINTER_CONTROL_REQUEST: u8 = 106; |
23871 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
23872 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
23873 | pub struct GetPointerControlRequest; |
23874 | impl GetPointerControlRequest { |
23875 | /// Serialize this request into bytes for the provided connection |
23876 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
23877 | let length_so_far = 0; |
23878 | let mut request0 = vec![ |
23879 | GET_POINTER_CONTROL_REQUEST, |
23880 | 0, |
23881 | 0, |
23882 | 0, |
23883 | ]; |
23884 | let length_so_far = length_so_far + request0.len(); |
23885 | assert_eq!(length_so_far % 4, 0); |
23886 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
23887 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
23888 | (vec![request0.into()], vec![]) |
23889 | } |
23890 | /// Parse this request given its header, its body, and any fds that go along with it |
23891 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
23892 | if header.major_opcode != GET_POINTER_CONTROL_REQUEST { |
23893 | return Err(ParseError::InvalidValue); |
23894 | } |
23895 | let remaining = &[header.minor_opcode]; |
23896 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
23897 | let _ = remaining; |
23898 | let _ = value; |
23899 | Ok(GetPointerControlRequest |
23900 | ) |
23901 | } |
23902 | } |
23903 | impl Request for GetPointerControlRequest { |
23904 | const EXTENSION_NAME: Option<&'static str> = None; |
23905 | |
23906 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
23907 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
23908 | // Flatten the buffers into a single vector |
23909 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
23910 | (buf, fds) |
23911 | } |
23912 | } |
23913 | impl crate::x11_utils::ReplyRequest for GetPointerControlRequest { |
23914 | type Reply = GetPointerControlReply; |
23915 | } |
23916 | |
23917 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
23918 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
23919 | pub struct GetPointerControlReply { |
23920 | pub sequence: u16, |
23921 | pub length: u32, |
23922 | pub acceleration_numerator: u16, |
23923 | pub acceleration_denominator: u16, |
23924 | pub threshold: u16, |
23925 | } |
23926 | impl TryParse for GetPointerControlReply { |
23927 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
23928 | let remaining: &[u8] = initial_value; |
23929 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
23930 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
23931 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
23932 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
23933 | let (acceleration_numerator: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
23934 | let (acceleration_denominator: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
23935 | let (threshold: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
23936 | let remaining: &[u8] = remaining.get(18..).ok_or(err:ParseError::InsufficientData)?; |
23937 | if response_type != 1 { |
23938 | return Err(ParseError::InvalidValue); |
23939 | } |
23940 | let result: GetPointerControlReply = GetPointerControlReply { sequence, length, acceleration_numerator, acceleration_denominator, threshold }; |
23941 | let _ = remaining; |
23942 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
23943 | .ok_or(err:ParseError::InsufficientData)?; |
23944 | Ok((result, remaining)) |
23945 | } |
23946 | } |
23947 | impl Serialize for GetPointerControlReply { |
23948 | type Bytes = [u8; 32]; |
23949 | fn serialize(&self) -> [u8; 32] { |
23950 | let response_type_bytes = &[1]; |
23951 | let sequence_bytes = self.sequence.serialize(); |
23952 | let length_bytes = self.length.serialize(); |
23953 | let acceleration_numerator_bytes = self.acceleration_numerator.serialize(); |
23954 | let acceleration_denominator_bytes = self.acceleration_denominator.serialize(); |
23955 | let threshold_bytes = self.threshold.serialize(); |
23956 | [ |
23957 | response_type_bytes[0], |
23958 | 0, |
23959 | sequence_bytes[0], |
23960 | sequence_bytes[1], |
23961 | length_bytes[0], |
23962 | length_bytes[1], |
23963 | length_bytes[2], |
23964 | length_bytes[3], |
23965 | acceleration_numerator_bytes[0], |
23966 | acceleration_numerator_bytes[1], |
23967 | acceleration_denominator_bytes[0], |
23968 | acceleration_denominator_bytes[1], |
23969 | threshold_bytes[0], |
23970 | threshold_bytes[1], |
23971 | 0, |
23972 | 0, |
23973 | 0, |
23974 | 0, |
23975 | 0, |
23976 | 0, |
23977 | 0, |
23978 | 0, |
23979 | 0, |
23980 | 0, |
23981 | 0, |
23982 | 0, |
23983 | 0, |
23984 | 0, |
23985 | 0, |
23986 | 0, |
23987 | 0, |
23988 | 0, |
23989 | ] |
23990 | } |
23991 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
23992 | bytes.reserve(32); |
23993 | let response_type_bytes = &[1]; |
23994 | bytes.push(response_type_bytes[0]); |
23995 | bytes.extend_from_slice(&[0; 1]); |
23996 | self.sequence.serialize_into(bytes); |
23997 | self.length.serialize_into(bytes); |
23998 | self.acceleration_numerator.serialize_into(bytes); |
23999 | self.acceleration_denominator.serialize_into(bytes); |
24000 | self.threshold.serialize_into(bytes); |
24001 | bytes.extend_from_slice(&[0; 18]); |
24002 | } |
24003 | } |
24004 | |
24005 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24006 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24007 | pub struct Blanking(u8); |
24008 | impl Blanking { |
24009 | pub const NOT_PREFERRED: Self = Self(0); |
24010 | pub const PREFERRED: Self = Self(1); |
24011 | pub const DEFAULT: Self = Self(2); |
24012 | } |
24013 | impl From<Blanking> for u8 { |
24014 | #[inline ] |
24015 | fn from(input: Blanking) -> Self { |
24016 | input.0 |
24017 | } |
24018 | } |
24019 | impl From<Blanking> for Option<u8> { |
24020 | #[inline ] |
24021 | fn from(input: Blanking) -> Self { |
24022 | Some(input.0) |
24023 | } |
24024 | } |
24025 | impl From<Blanking> for u16 { |
24026 | #[inline ] |
24027 | fn from(input: Blanking) -> Self { |
24028 | u16::from(input.0) |
24029 | } |
24030 | } |
24031 | impl From<Blanking> for Option<u16> { |
24032 | #[inline ] |
24033 | fn from(input: Blanking) -> Self { |
24034 | Some(u16::from(input.0)) |
24035 | } |
24036 | } |
24037 | impl From<Blanking> for u32 { |
24038 | #[inline ] |
24039 | fn from(input: Blanking) -> Self { |
24040 | u32::from(input.0) |
24041 | } |
24042 | } |
24043 | impl From<Blanking> for Option<u32> { |
24044 | #[inline ] |
24045 | fn from(input: Blanking) -> Self { |
24046 | Some(u32::from(input.0)) |
24047 | } |
24048 | } |
24049 | impl From<u8> for Blanking { |
24050 | #[inline ] |
24051 | fn from(value: u8) -> Self { |
24052 | Self(value) |
24053 | } |
24054 | } |
24055 | impl core::fmt::Debug for Blanking { |
24056 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
24057 | let variants: [(u32, &str, &str); 3] = [ |
24058 | (Self::NOT_PREFERRED.0.into(), "NOT_PREFERRED" , "NotPreferred" ), |
24059 | (Self::PREFERRED.0.into(), "PREFERRED" , "Preferred" ), |
24060 | (Self::DEFAULT.0.into(), "DEFAULT" , "Default" ), |
24061 | ]; |
24062 | pretty_print_enum(fmt, self.0.into(), &variants) |
24063 | } |
24064 | } |
24065 | |
24066 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24067 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24068 | pub struct Exposures(u8); |
24069 | impl Exposures { |
24070 | pub const NOT_ALLOWED: Self = Self(0); |
24071 | pub const ALLOWED: Self = Self(1); |
24072 | pub const DEFAULT: Self = Self(2); |
24073 | } |
24074 | impl From<Exposures> for u8 { |
24075 | #[inline ] |
24076 | fn from(input: Exposures) -> Self { |
24077 | input.0 |
24078 | } |
24079 | } |
24080 | impl From<Exposures> for Option<u8> { |
24081 | #[inline ] |
24082 | fn from(input: Exposures) -> Self { |
24083 | Some(input.0) |
24084 | } |
24085 | } |
24086 | impl From<Exposures> for u16 { |
24087 | #[inline ] |
24088 | fn from(input: Exposures) -> Self { |
24089 | u16::from(input.0) |
24090 | } |
24091 | } |
24092 | impl From<Exposures> for Option<u16> { |
24093 | #[inline ] |
24094 | fn from(input: Exposures) -> Self { |
24095 | Some(u16::from(input.0)) |
24096 | } |
24097 | } |
24098 | impl From<Exposures> for u32 { |
24099 | #[inline ] |
24100 | fn from(input: Exposures) -> Self { |
24101 | u32::from(input.0) |
24102 | } |
24103 | } |
24104 | impl From<Exposures> for Option<u32> { |
24105 | #[inline ] |
24106 | fn from(input: Exposures) -> Self { |
24107 | Some(u32::from(input.0)) |
24108 | } |
24109 | } |
24110 | impl From<u8> for Exposures { |
24111 | #[inline ] |
24112 | fn from(value: u8) -> Self { |
24113 | Self(value) |
24114 | } |
24115 | } |
24116 | impl core::fmt::Debug for Exposures { |
24117 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
24118 | let variants: [(u32, &str, &str); 3] = [ |
24119 | (Self::NOT_ALLOWED.0.into(), "NOT_ALLOWED" , "NotAllowed" ), |
24120 | (Self::ALLOWED.0.into(), "ALLOWED" , "Allowed" ), |
24121 | (Self::DEFAULT.0.into(), "DEFAULT" , "Default" ), |
24122 | ]; |
24123 | pretty_print_enum(fmt, self.0.into(), &variants) |
24124 | } |
24125 | } |
24126 | |
24127 | /// Opcode for the SetScreenSaver request |
24128 | pub const SET_SCREEN_SAVER_REQUEST: u8 = 107; |
24129 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24130 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24131 | pub struct SetScreenSaverRequest { |
24132 | pub timeout: i16, |
24133 | pub interval: i16, |
24134 | pub prefer_blanking: Blanking, |
24135 | pub allow_exposures: Exposures, |
24136 | } |
24137 | impl SetScreenSaverRequest { |
24138 | /// Serialize this request into bytes for the provided connection |
24139 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
24140 | let length_so_far = 0; |
24141 | let timeout_bytes = self.timeout.serialize(); |
24142 | let interval_bytes = self.interval.serialize(); |
24143 | let prefer_blanking_bytes = u8::from(self.prefer_blanking).serialize(); |
24144 | let allow_exposures_bytes = u8::from(self.allow_exposures).serialize(); |
24145 | let mut request0 = vec![ |
24146 | SET_SCREEN_SAVER_REQUEST, |
24147 | 0, |
24148 | 0, |
24149 | 0, |
24150 | timeout_bytes[0], |
24151 | timeout_bytes[1], |
24152 | interval_bytes[0], |
24153 | interval_bytes[1], |
24154 | prefer_blanking_bytes[0], |
24155 | allow_exposures_bytes[0], |
24156 | 0, |
24157 | 0, |
24158 | ]; |
24159 | let length_so_far = length_so_far + request0.len(); |
24160 | assert_eq!(length_so_far % 4, 0); |
24161 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
24162 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
24163 | (vec![request0.into()], vec![]) |
24164 | } |
24165 | /// Parse this request given its header, its body, and any fds that go along with it |
24166 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
24167 | if header.major_opcode != SET_SCREEN_SAVER_REQUEST { |
24168 | return Err(ParseError::InvalidValue); |
24169 | } |
24170 | let remaining = &[header.minor_opcode]; |
24171 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
24172 | let _ = remaining; |
24173 | let (timeout, remaining) = i16::try_parse(value)?; |
24174 | let (interval, remaining) = i16::try_parse(remaining)?; |
24175 | let (prefer_blanking, remaining) = u8::try_parse(remaining)?; |
24176 | let prefer_blanking = prefer_blanking.into(); |
24177 | let (allow_exposures, remaining) = u8::try_parse(remaining)?; |
24178 | let allow_exposures = allow_exposures.into(); |
24179 | let _ = remaining; |
24180 | Ok(SetScreenSaverRequest { |
24181 | timeout, |
24182 | interval, |
24183 | prefer_blanking, |
24184 | allow_exposures, |
24185 | }) |
24186 | } |
24187 | } |
24188 | impl Request for SetScreenSaverRequest { |
24189 | const EXTENSION_NAME: Option<&'static str> = None; |
24190 | |
24191 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
24192 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
24193 | // Flatten the buffers into a single vector |
24194 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
24195 | (buf, fds) |
24196 | } |
24197 | } |
24198 | impl crate::x11_utils::VoidRequest for SetScreenSaverRequest { |
24199 | } |
24200 | |
24201 | /// Opcode for the GetScreenSaver request |
24202 | pub const GET_SCREEN_SAVER_REQUEST: u8 = 108; |
24203 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24204 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24205 | pub struct GetScreenSaverRequest; |
24206 | impl GetScreenSaverRequest { |
24207 | /// Serialize this request into bytes for the provided connection |
24208 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
24209 | let length_so_far = 0; |
24210 | let mut request0 = vec![ |
24211 | GET_SCREEN_SAVER_REQUEST, |
24212 | 0, |
24213 | 0, |
24214 | 0, |
24215 | ]; |
24216 | let length_so_far = length_so_far + request0.len(); |
24217 | assert_eq!(length_so_far % 4, 0); |
24218 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
24219 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
24220 | (vec![request0.into()], vec![]) |
24221 | } |
24222 | /// Parse this request given its header, its body, and any fds that go along with it |
24223 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
24224 | if header.major_opcode != GET_SCREEN_SAVER_REQUEST { |
24225 | return Err(ParseError::InvalidValue); |
24226 | } |
24227 | let remaining = &[header.minor_opcode]; |
24228 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
24229 | let _ = remaining; |
24230 | let _ = value; |
24231 | Ok(GetScreenSaverRequest |
24232 | ) |
24233 | } |
24234 | } |
24235 | impl Request for GetScreenSaverRequest { |
24236 | const EXTENSION_NAME: Option<&'static str> = None; |
24237 | |
24238 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
24239 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
24240 | // Flatten the buffers into a single vector |
24241 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
24242 | (buf, fds) |
24243 | } |
24244 | } |
24245 | impl crate::x11_utils::ReplyRequest for GetScreenSaverRequest { |
24246 | type Reply = GetScreenSaverReply; |
24247 | } |
24248 | |
24249 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24250 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24251 | pub struct GetScreenSaverReply { |
24252 | pub sequence: u16, |
24253 | pub length: u32, |
24254 | pub timeout: u16, |
24255 | pub interval: u16, |
24256 | pub prefer_blanking: Blanking, |
24257 | pub allow_exposures: Exposures, |
24258 | } |
24259 | impl TryParse for GetScreenSaverReply { |
24260 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
24261 | let remaining: &[u8] = initial_value; |
24262 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
24263 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
24264 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
24265 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
24266 | let (timeout: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
24267 | let (interval: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
24268 | let (prefer_blanking: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
24269 | let (allow_exposures: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
24270 | let remaining: &[u8] = remaining.get(18..).ok_or(err:ParseError::InsufficientData)?; |
24271 | if response_type != 1 { |
24272 | return Err(ParseError::InvalidValue); |
24273 | } |
24274 | let prefer_blanking: Blanking = prefer_blanking.into(); |
24275 | let allow_exposures: Exposures = allow_exposures.into(); |
24276 | let result: GetScreenSaverReply = GetScreenSaverReply { sequence, length, timeout, interval, prefer_blanking, allow_exposures }; |
24277 | let _ = remaining; |
24278 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
24279 | .ok_or(err:ParseError::InsufficientData)?; |
24280 | Ok((result, remaining)) |
24281 | } |
24282 | } |
24283 | impl Serialize for GetScreenSaverReply { |
24284 | type Bytes = [u8; 32]; |
24285 | fn serialize(&self) -> [u8; 32] { |
24286 | let response_type_bytes = &[1]; |
24287 | let sequence_bytes = self.sequence.serialize(); |
24288 | let length_bytes = self.length.serialize(); |
24289 | let timeout_bytes = self.timeout.serialize(); |
24290 | let interval_bytes = self.interval.serialize(); |
24291 | let prefer_blanking_bytes = u8::from(self.prefer_blanking).serialize(); |
24292 | let allow_exposures_bytes = u8::from(self.allow_exposures).serialize(); |
24293 | [ |
24294 | response_type_bytes[0], |
24295 | 0, |
24296 | sequence_bytes[0], |
24297 | sequence_bytes[1], |
24298 | length_bytes[0], |
24299 | length_bytes[1], |
24300 | length_bytes[2], |
24301 | length_bytes[3], |
24302 | timeout_bytes[0], |
24303 | timeout_bytes[1], |
24304 | interval_bytes[0], |
24305 | interval_bytes[1], |
24306 | prefer_blanking_bytes[0], |
24307 | allow_exposures_bytes[0], |
24308 | 0, |
24309 | 0, |
24310 | 0, |
24311 | 0, |
24312 | 0, |
24313 | 0, |
24314 | 0, |
24315 | 0, |
24316 | 0, |
24317 | 0, |
24318 | 0, |
24319 | 0, |
24320 | 0, |
24321 | 0, |
24322 | 0, |
24323 | 0, |
24324 | 0, |
24325 | 0, |
24326 | ] |
24327 | } |
24328 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
24329 | bytes.reserve(32); |
24330 | let response_type_bytes = &[1]; |
24331 | bytes.push(response_type_bytes[0]); |
24332 | bytes.extend_from_slice(&[0; 1]); |
24333 | self.sequence.serialize_into(bytes); |
24334 | self.length.serialize_into(bytes); |
24335 | self.timeout.serialize_into(bytes); |
24336 | self.interval.serialize_into(bytes); |
24337 | u8::from(self.prefer_blanking).serialize_into(bytes); |
24338 | u8::from(self.allow_exposures).serialize_into(bytes); |
24339 | bytes.extend_from_slice(&[0; 18]); |
24340 | } |
24341 | } |
24342 | |
24343 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24344 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24345 | pub struct HostMode(u8); |
24346 | impl HostMode { |
24347 | pub const INSERT: Self = Self(0); |
24348 | pub const DELETE: Self = Self(1); |
24349 | } |
24350 | impl From<HostMode> for u8 { |
24351 | #[inline ] |
24352 | fn from(input: HostMode) -> Self { |
24353 | input.0 |
24354 | } |
24355 | } |
24356 | impl From<HostMode> for Option<u8> { |
24357 | #[inline ] |
24358 | fn from(input: HostMode) -> Self { |
24359 | Some(input.0) |
24360 | } |
24361 | } |
24362 | impl From<HostMode> for u16 { |
24363 | #[inline ] |
24364 | fn from(input: HostMode) -> Self { |
24365 | u16::from(input.0) |
24366 | } |
24367 | } |
24368 | impl From<HostMode> for Option<u16> { |
24369 | #[inline ] |
24370 | fn from(input: HostMode) -> Self { |
24371 | Some(u16::from(input.0)) |
24372 | } |
24373 | } |
24374 | impl From<HostMode> for u32 { |
24375 | #[inline ] |
24376 | fn from(input: HostMode) -> Self { |
24377 | u32::from(input.0) |
24378 | } |
24379 | } |
24380 | impl From<HostMode> for Option<u32> { |
24381 | #[inline ] |
24382 | fn from(input: HostMode) -> Self { |
24383 | Some(u32::from(input.0)) |
24384 | } |
24385 | } |
24386 | impl From<u8> for HostMode { |
24387 | #[inline ] |
24388 | fn from(value: u8) -> Self { |
24389 | Self(value) |
24390 | } |
24391 | } |
24392 | impl core::fmt::Debug for HostMode { |
24393 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
24394 | let variants: [(u32, &str, &str); 2] = [ |
24395 | (Self::INSERT.0.into(), "INSERT" , "Insert" ), |
24396 | (Self::DELETE.0.into(), "DELETE" , "Delete" ), |
24397 | ]; |
24398 | pretty_print_enum(fmt, self.0.into(), &variants) |
24399 | } |
24400 | } |
24401 | |
24402 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24403 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24404 | pub struct Family(u8); |
24405 | impl Family { |
24406 | pub const INTERNET: Self = Self(0); |
24407 | pub const DEC_NET: Self = Self(1); |
24408 | pub const CHAOS: Self = Self(2); |
24409 | pub const SERVER_INTERPRETED: Self = Self(5); |
24410 | pub const INTERNET6: Self = Self(6); |
24411 | } |
24412 | impl From<Family> for u8 { |
24413 | #[inline ] |
24414 | fn from(input: Family) -> Self { |
24415 | input.0 |
24416 | } |
24417 | } |
24418 | impl From<Family> for Option<u8> { |
24419 | #[inline ] |
24420 | fn from(input: Family) -> Self { |
24421 | Some(input.0) |
24422 | } |
24423 | } |
24424 | impl From<Family> for u16 { |
24425 | #[inline ] |
24426 | fn from(input: Family) -> Self { |
24427 | u16::from(input.0) |
24428 | } |
24429 | } |
24430 | impl From<Family> for Option<u16> { |
24431 | #[inline ] |
24432 | fn from(input: Family) -> Self { |
24433 | Some(u16::from(input.0)) |
24434 | } |
24435 | } |
24436 | impl From<Family> for u32 { |
24437 | #[inline ] |
24438 | fn from(input: Family) -> Self { |
24439 | u32::from(input.0) |
24440 | } |
24441 | } |
24442 | impl From<Family> for Option<u32> { |
24443 | #[inline ] |
24444 | fn from(input: Family) -> Self { |
24445 | Some(u32::from(input.0)) |
24446 | } |
24447 | } |
24448 | impl From<u8> for Family { |
24449 | #[inline ] |
24450 | fn from(value: u8) -> Self { |
24451 | Self(value) |
24452 | } |
24453 | } |
24454 | impl core::fmt::Debug for Family { |
24455 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
24456 | let variants: [(u32, &str, &str); 5] = [ |
24457 | (Self::INTERNET.0.into(), "INTERNET" , "Internet" ), |
24458 | (Self::DEC_NET.0.into(), "DEC_NET" , "DECnet" ), |
24459 | (Self::CHAOS.0.into(), "CHAOS" , "Chaos" ), |
24460 | (Self::SERVER_INTERPRETED.0.into(), "SERVER_INTERPRETED" , "ServerInterpreted" ), |
24461 | (Self::INTERNET6.0.into(), "INTERNET6" , "Internet6" ), |
24462 | ]; |
24463 | pretty_print_enum(fmt, self.0.into(), &variants) |
24464 | } |
24465 | } |
24466 | |
24467 | /// Opcode for the ChangeHosts request |
24468 | pub const CHANGE_HOSTS_REQUEST: u8 = 109; |
24469 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24470 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24471 | pub struct ChangeHostsRequest<'input> { |
24472 | pub mode: HostMode, |
24473 | pub family: Family, |
24474 | pub address: Cow<'input, [u8]>, |
24475 | } |
24476 | impl<'input> ChangeHostsRequest<'input> { |
24477 | /// Serialize this request into bytes for the provided connection |
24478 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
24479 | let length_so_far = 0; |
24480 | let mode_bytes = u8::from(self.mode).serialize(); |
24481 | let family_bytes = u8::from(self.family).serialize(); |
24482 | let address_len = u16::try_from(self.address.len()).expect("`address` has too many elements" ); |
24483 | let address_len_bytes = address_len.serialize(); |
24484 | let mut request0 = vec![ |
24485 | CHANGE_HOSTS_REQUEST, |
24486 | mode_bytes[0], |
24487 | 0, |
24488 | 0, |
24489 | family_bytes[0], |
24490 | 0, |
24491 | address_len_bytes[0], |
24492 | address_len_bytes[1], |
24493 | ]; |
24494 | let length_so_far = length_so_far + request0.len(); |
24495 | let length_so_far = length_so_far + self.address.len(); |
24496 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
24497 | let length_so_far = length_so_far + padding0.len(); |
24498 | assert_eq!(length_so_far % 4, 0); |
24499 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
24500 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
24501 | (vec![request0.into(), self.address, padding0.into()], vec![]) |
24502 | } |
24503 | /// Parse this request given its header, its body, and any fds that go along with it |
24504 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
24505 | if header.major_opcode != CHANGE_HOSTS_REQUEST { |
24506 | return Err(ParseError::InvalidValue); |
24507 | } |
24508 | let remaining = &[header.minor_opcode]; |
24509 | let (mode, remaining) = u8::try_parse(remaining)?; |
24510 | let mode = mode.into(); |
24511 | let _ = remaining; |
24512 | let (family, remaining) = u8::try_parse(value)?; |
24513 | let family = family.into(); |
24514 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
24515 | let (address_len, remaining) = u16::try_parse(remaining)?; |
24516 | let (address, remaining) = crate::x11_utils::parse_u8_list(remaining, address_len.try_to_usize()?)?; |
24517 | let _ = remaining; |
24518 | Ok(ChangeHostsRequest { |
24519 | mode, |
24520 | family, |
24521 | address: Cow::Borrowed(address), |
24522 | }) |
24523 | } |
24524 | /// Clone all borrowed data in this ChangeHostsRequest. |
24525 | pub fn into_owned(self) -> ChangeHostsRequest<'static> { |
24526 | ChangeHostsRequest { |
24527 | mode: self.mode, |
24528 | family: self.family, |
24529 | address: Cow::Owned(self.address.into_owned()), |
24530 | } |
24531 | } |
24532 | } |
24533 | impl<'input> Request for ChangeHostsRequest<'input> { |
24534 | const EXTENSION_NAME: Option<&'static str> = None; |
24535 | |
24536 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
24537 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
24538 | // Flatten the buffers into a single vector |
24539 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
24540 | (buf, fds) |
24541 | } |
24542 | } |
24543 | impl<'input> crate::x11_utils::VoidRequest for ChangeHostsRequest<'input> { |
24544 | } |
24545 | |
24546 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24547 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24548 | pub struct Host { |
24549 | pub family: Family, |
24550 | pub address: Vec<u8>, |
24551 | } |
24552 | impl TryParse for Host { |
24553 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
24554 | let value: &[u8] = remaining; |
24555 | let (family: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
24556 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
24557 | let (address_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
24558 | let (address: &[u8], remaining: &[u8]) = crate::x11_utils::parse_u8_list(data:remaining, list_length:address_len.try_to_usize()?)?; |
24559 | let address: Vec = address.to_vec(); |
24560 | // Align offset to multiple of 4 |
24561 | let offset: usize = remaining.as_ptr() as usize - value.as_ptr() as usize; |
24562 | let misalignment: usize = (4 - (offset % 4)) % 4; |
24563 | let remaining: &[u8] = remaining.get(misalignment..).ok_or(err:ParseError::InsufficientData)?; |
24564 | let family: Family = family.into(); |
24565 | let result: Host = Host { family, address }; |
24566 | Ok((result, remaining)) |
24567 | } |
24568 | } |
24569 | impl Serialize for Host { |
24570 | type Bytes = Vec<u8>; |
24571 | fn serialize(&self) -> Vec<u8> { |
24572 | let mut result: Vec = Vec::new(); |
24573 | self.serialize_into(&mut result); |
24574 | result |
24575 | } |
24576 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
24577 | bytes.reserve(additional:4); |
24578 | u8::from(self.family).serialize_into(bytes); |
24579 | bytes.extend_from_slice(&[0; 1]); |
24580 | let address_len: u16 = u16::try_from(self.address.len()).expect(msg:"`address` has too many elements" ); |
24581 | address_len.serialize_into(bytes); |
24582 | bytes.extend_from_slice(&self.address); |
24583 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
24584 | } |
24585 | } |
24586 | impl Host { |
24587 | /// Get the value of the `address_len` field. |
24588 | /// |
24589 | /// The `address_len` field is used as the length field of the `address` field. |
24590 | /// This function computes the field's value again based on the length of the list. |
24591 | /// |
24592 | /// # Panics |
24593 | /// |
24594 | /// Panics if the value cannot be represented in the target type. This |
24595 | /// cannot happen with values of the struct received from the X11 server. |
24596 | pub fn address_len(&self) -> u16 { |
24597 | self.address.len() |
24598 | .try_into().unwrap() |
24599 | } |
24600 | } |
24601 | |
24602 | /// Opcode for the ListHosts request |
24603 | pub const LIST_HOSTS_REQUEST: u8 = 110; |
24604 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24605 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24606 | pub struct ListHostsRequest; |
24607 | impl ListHostsRequest { |
24608 | /// Serialize this request into bytes for the provided connection |
24609 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
24610 | let length_so_far = 0; |
24611 | let mut request0 = vec![ |
24612 | LIST_HOSTS_REQUEST, |
24613 | 0, |
24614 | 0, |
24615 | 0, |
24616 | ]; |
24617 | let length_so_far = length_so_far + request0.len(); |
24618 | assert_eq!(length_so_far % 4, 0); |
24619 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
24620 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
24621 | (vec![request0.into()], vec![]) |
24622 | } |
24623 | /// Parse this request given its header, its body, and any fds that go along with it |
24624 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
24625 | if header.major_opcode != LIST_HOSTS_REQUEST { |
24626 | return Err(ParseError::InvalidValue); |
24627 | } |
24628 | let remaining = &[header.minor_opcode]; |
24629 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
24630 | let _ = remaining; |
24631 | let _ = value; |
24632 | Ok(ListHostsRequest |
24633 | ) |
24634 | } |
24635 | } |
24636 | impl Request for ListHostsRequest { |
24637 | const EXTENSION_NAME: Option<&'static str> = None; |
24638 | |
24639 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
24640 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
24641 | // Flatten the buffers into a single vector |
24642 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
24643 | (buf, fds) |
24644 | } |
24645 | } |
24646 | impl crate::x11_utils::ReplyRequest for ListHostsRequest { |
24647 | type Reply = ListHostsReply; |
24648 | } |
24649 | |
24650 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24651 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24652 | pub struct ListHostsReply { |
24653 | pub mode: AccessControl, |
24654 | pub sequence: u16, |
24655 | pub length: u32, |
24656 | pub hosts: Vec<Host>, |
24657 | } |
24658 | impl TryParse for ListHostsReply { |
24659 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
24660 | let remaining: &[u8] = initial_value; |
24661 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
24662 | let (mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
24663 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
24664 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
24665 | let (hosts_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
24666 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
24667 | let (hosts: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<Host>(data:remaining, list_length:hosts_len.try_to_usize()?)?; |
24668 | if response_type != 1 { |
24669 | return Err(ParseError::InvalidValue); |
24670 | } |
24671 | let mode: AccessControl = mode.into(); |
24672 | let result: ListHostsReply = ListHostsReply { mode, sequence, length, hosts }; |
24673 | let _ = remaining; |
24674 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
24675 | .ok_or(err:ParseError::InsufficientData)?; |
24676 | Ok((result, remaining)) |
24677 | } |
24678 | } |
24679 | impl Serialize for ListHostsReply { |
24680 | type Bytes = Vec<u8>; |
24681 | fn serialize(&self) -> Vec<u8> { |
24682 | let mut result: Vec = Vec::new(); |
24683 | self.serialize_into(&mut result); |
24684 | result |
24685 | } |
24686 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
24687 | bytes.reserve(additional:32); |
24688 | let response_type_bytes: &[u8; 1] = &[1]; |
24689 | bytes.push(response_type_bytes[0]); |
24690 | u8::from(self.mode).serialize_into(bytes); |
24691 | self.sequence.serialize_into(bytes); |
24692 | self.length.serialize_into(bytes); |
24693 | let hosts_len: u16 = u16::try_from(self.hosts.len()).expect(msg:"`hosts` has too many elements" ); |
24694 | hosts_len.serialize_into(bytes); |
24695 | bytes.extend_from_slice(&[0; 22]); |
24696 | self.hosts.serialize_into(bytes); |
24697 | } |
24698 | } |
24699 | impl ListHostsReply { |
24700 | /// Get the value of the `hosts_len` field. |
24701 | /// |
24702 | /// The `hosts_len` field is used as the length field of the `hosts` field. |
24703 | /// This function computes the field's value again based on the length of the list. |
24704 | /// |
24705 | /// # Panics |
24706 | /// |
24707 | /// Panics if the value cannot be represented in the target type. This |
24708 | /// cannot happen with values of the struct received from the X11 server. |
24709 | pub fn hosts_len(&self) -> u16 { |
24710 | self.hosts.len() |
24711 | .try_into().unwrap() |
24712 | } |
24713 | } |
24714 | |
24715 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24716 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24717 | pub struct AccessControl(u8); |
24718 | impl AccessControl { |
24719 | pub const DISABLE: Self = Self(0); |
24720 | pub const ENABLE: Self = Self(1); |
24721 | } |
24722 | impl From<AccessControl> for u8 { |
24723 | #[inline ] |
24724 | fn from(input: AccessControl) -> Self { |
24725 | input.0 |
24726 | } |
24727 | } |
24728 | impl From<AccessControl> for Option<u8> { |
24729 | #[inline ] |
24730 | fn from(input: AccessControl) -> Self { |
24731 | Some(input.0) |
24732 | } |
24733 | } |
24734 | impl From<AccessControl> for u16 { |
24735 | #[inline ] |
24736 | fn from(input: AccessControl) -> Self { |
24737 | u16::from(input.0) |
24738 | } |
24739 | } |
24740 | impl From<AccessControl> for Option<u16> { |
24741 | #[inline ] |
24742 | fn from(input: AccessControl) -> Self { |
24743 | Some(u16::from(input.0)) |
24744 | } |
24745 | } |
24746 | impl From<AccessControl> for u32 { |
24747 | #[inline ] |
24748 | fn from(input: AccessControl) -> Self { |
24749 | u32::from(input.0) |
24750 | } |
24751 | } |
24752 | impl From<AccessControl> for Option<u32> { |
24753 | #[inline ] |
24754 | fn from(input: AccessControl) -> Self { |
24755 | Some(u32::from(input.0)) |
24756 | } |
24757 | } |
24758 | impl From<u8> for AccessControl { |
24759 | #[inline ] |
24760 | fn from(value: u8) -> Self { |
24761 | Self(value) |
24762 | } |
24763 | } |
24764 | impl core::fmt::Debug for AccessControl { |
24765 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
24766 | let variants: [(u32, &str, &str); 2] = [ |
24767 | (Self::DISABLE.0.into(), "DISABLE" , "Disable" ), |
24768 | (Self::ENABLE.0.into(), "ENABLE" , "Enable" ), |
24769 | ]; |
24770 | pretty_print_enum(fmt, self.0.into(), &variants) |
24771 | } |
24772 | } |
24773 | |
24774 | /// Opcode for the SetAccessControl request |
24775 | pub const SET_ACCESS_CONTROL_REQUEST: u8 = 111; |
24776 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24777 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24778 | pub struct SetAccessControlRequest { |
24779 | pub mode: AccessControl, |
24780 | } |
24781 | impl SetAccessControlRequest { |
24782 | /// Serialize this request into bytes for the provided connection |
24783 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
24784 | let length_so_far = 0; |
24785 | let mode_bytes = u8::from(self.mode).serialize(); |
24786 | let mut request0 = vec![ |
24787 | SET_ACCESS_CONTROL_REQUEST, |
24788 | mode_bytes[0], |
24789 | 0, |
24790 | 0, |
24791 | ]; |
24792 | let length_so_far = length_so_far + request0.len(); |
24793 | assert_eq!(length_so_far % 4, 0); |
24794 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
24795 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
24796 | (vec![request0.into()], vec![]) |
24797 | } |
24798 | /// Parse this request given its header, its body, and any fds that go along with it |
24799 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
24800 | if header.major_opcode != SET_ACCESS_CONTROL_REQUEST { |
24801 | return Err(ParseError::InvalidValue); |
24802 | } |
24803 | let remaining = &[header.minor_opcode]; |
24804 | let (mode, remaining) = u8::try_parse(remaining)?; |
24805 | let mode = mode.into(); |
24806 | let _ = remaining; |
24807 | let _ = value; |
24808 | Ok(SetAccessControlRequest { |
24809 | mode, |
24810 | }) |
24811 | } |
24812 | } |
24813 | impl Request for SetAccessControlRequest { |
24814 | const EXTENSION_NAME: Option<&'static str> = None; |
24815 | |
24816 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
24817 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
24818 | // Flatten the buffers into a single vector |
24819 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
24820 | (buf, fds) |
24821 | } |
24822 | } |
24823 | impl crate::x11_utils::VoidRequest for SetAccessControlRequest { |
24824 | } |
24825 | |
24826 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24827 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24828 | pub struct CloseDown(u8); |
24829 | impl CloseDown { |
24830 | pub const DESTROY_ALL: Self = Self(0); |
24831 | pub const RETAIN_PERMANENT: Self = Self(1); |
24832 | pub const RETAIN_TEMPORARY: Self = Self(2); |
24833 | } |
24834 | impl From<CloseDown> for u8 { |
24835 | #[inline ] |
24836 | fn from(input: CloseDown) -> Self { |
24837 | input.0 |
24838 | } |
24839 | } |
24840 | impl From<CloseDown> for Option<u8> { |
24841 | #[inline ] |
24842 | fn from(input: CloseDown) -> Self { |
24843 | Some(input.0) |
24844 | } |
24845 | } |
24846 | impl From<CloseDown> for u16 { |
24847 | #[inline ] |
24848 | fn from(input: CloseDown) -> Self { |
24849 | u16::from(input.0) |
24850 | } |
24851 | } |
24852 | impl From<CloseDown> for Option<u16> { |
24853 | #[inline ] |
24854 | fn from(input: CloseDown) -> Self { |
24855 | Some(u16::from(input.0)) |
24856 | } |
24857 | } |
24858 | impl From<CloseDown> for u32 { |
24859 | #[inline ] |
24860 | fn from(input: CloseDown) -> Self { |
24861 | u32::from(input.0) |
24862 | } |
24863 | } |
24864 | impl From<CloseDown> for Option<u32> { |
24865 | #[inline ] |
24866 | fn from(input: CloseDown) -> Self { |
24867 | Some(u32::from(input.0)) |
24868 | } |
24869 | } |
24870 | impl From<u8> for CloseDown { |
24871 | #[inline ] |
24872 | fn from(value: u8) -> Self { |
24873 | Self(value) |
24874 | } |
24875 | } |
24876 | impl core::fmt::Debug for CloseDown { |
24877 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
24878 | let variants: [(u32, &str, &str); 3] = [ |
24879 | (Self::DESTROY_ALL.0.into(), "DESTROY_ALL" , "DestroyAll" ), |
24880 | (Self::RETAIN_PERMANENT.0.into(), "RETAIN_PERMANENT" , "RetainPermanent" ), |
24881 | (Self::RETAIN_TEMPORARY.0.into(), "RETAIN_TEMPORARY" , "RetainTemporary" ), |
24882 | ]; |
24883 | pretty_print_enum(fmt, self.0.into(), &variants) |
24884 | } |
24885 | } |
24886 | |
24887 | /// Opcode for the SetCloseDownMode request |
24888 | pub const SET_CLOSE_DOWN_MODE_REQUEST: u8 = 112; |
24889 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24890 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24891 | pub struct SetCloseDownModeRequest { |
24892 | pub mode: CloseDown, |
24893 | } |
24894 | impl SetCloseDownModeRequest { |
24895 | /// Serialize this request into bytes for the provided connection |
24896 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
24897 | let length_so_far = 0; |
24898 | let mode_bytes = u8::from(self.mode).serialize(); |
24899 | let mut request0 = vec![ |
24900 | SET_CLOSE_DOWN_MODE_REQUEST, |
24901 | mode_bytes[0], |
24902 | 0, |
24903 | 0, |
24904 | ]; |
24905 | let length_so_far = length_so_far + request0.len(); |
24906 | assert_eq!(length_so_far % 4, 0); |
24907 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
24908 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
24909 | (vec![request0.into()], vec![]) |
24910 | } |
24911 | /// Parse this request given its header, its body, and any fds that go along with it |
24912 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
24913 | if header.major_opcode != SET_CLOSE_DOWN_MODE_REQUEST { |
24914 | return Err(ParseError::InvalidValue); |
24915 | } |
24916 | let remaining = &[header.minor_opcode]; |
24917 | let (mode, remaining) = u8::try_parse(remaining)?; |
24918 | let mode = mode.into(); |
24919 | let _ = remaining; |
24920 | let _ = value; |
24921 | Ok(SetCloseDownModeRequest { |
24922 | mode, |
24923 | }) |
24924 | } |
24925 | } |
24926 | impl Request for SetCloseDownModeRequest { |
24927 | const EXTENSION_NAME: Option<&'static str> = None; |
24928 | |
24929 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
24930 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
24931 | // Flatten the buffers into a single vector |
24932 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
24933 | (buf, fds) |
24934 | } |
24935 | } |
24936 | impl crate::x11_utils::VoidRequest for SetCloseDownModeRequest { |
24937 | } |
24938 | |
24939 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
24940 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
24941 | pub struct Kill(u8); |
24942 | impl Kill { |
24943 | pub const ALL_TEMPORARY: Self = Self(0); |
24944 | } |
24945 | impl From<Kill> for u8 { |
24946 | #[inline ] |
24947 | fn from(input: Kill) -> Self { |
24948 | input.0 |
24949 | } |
24950 | } |
24951 | impl From<Kill> for Option<u8> { |
24952 | #[inline ] |
24953 | fn from(input: Kill) -> Self { |
24954 | Some(input.0) |
24955 | } |
24956 | } |
24957 | impl From<Kill> for u16 { |
24958 | #[inline ] |
24959 | fn from(input: Kill) -> Self { |
24960 | u16::from(input.0) |
24961 | } |
24962 | } |
24963 | impl From<Kill> for Option<u16> { |
24964 | #[inline ] |
24965 | fn from(input: Kill) -> Self { |
24966 | Some(u16::from(input.0)) |
24967 | } |
24968 | } |
24969 | impl From<Kill> for u32 { |
24970 | #[inline ] |
24971 | fn from(input: Kill) -> Self { |
24972 | u32::from(input.0) |
24973 | } |
24974 | } |
24975 | impl From<Kill> for Option<u32> { |
24976 | #[inline ] |
24977 | fn from(input: Kill) -> Self { |
24978 | Some(u32::from(input.0)) |
24979 | } |
24980 | } |
24981 | impl From<u8> for Kill { |
24982 | #[inline ] |
24983 | fn from(value: u8) -> Self { |
24984 | Self(value) |
24985 | } |
24986 | } |
24987 | impl core::fmt::Debug for Kill { |
24988 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
24989 | let variants: [(u32, &str, &str); 1] = [ |
24990 | (Self::ALL_TEMPORARY.0.into(), "ALL_TEMPORARY" , "AllTemporary" ), |
24991 | ]; |
24992 | pretty_print_enum(fmt, self.0.into(), &variants) |
24993 | } |
24994 | } |
24995 | |
24996 | /// Opcode for the KillClient request |
24997 | pub const KILL_CLIENT_REQUEST: u8 = 113; |
24998 | /// kills a client. |
24999 | /// |
25000 | /// Forces a close down of the client that created the specified `resource`. |
25001 | /// |
25002 | /// # Fields |
25003 | /// |
25004 | /// * `resource` - Any resource belonging to the client (for example a Window), used to identify |
25005 | /// the client connection. |
25006 | /// |
25007 | /// The special value of `XCB_KILL_ALL_TEMPORARY`, the resources of all clients |
25008 | /// that have terminated in `RetainTemporary` (TODO) are destroyed. |
25009 | /// |
25010 | /// # Errors |
25011 | /// |
25012 | /// * `Value` - The specified `resource` does not exist. |
25013 | /// |
25014 | /// # See |
25015 | /// |
25016 | /// * `xkill`: program |
25017 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25018 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25019 | pub struct KillClientRequest { |
25020 | pub resource: u32, |
25021 | } |
25022 | impl KillClientRequest { |
25023 | /// Serialize this request into bytes for the provided connection |
25024 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
25025 | let length_so_far = 0; |
25026 | let resource_bytes = self.resource.serialize(); |
25027 | let mut request0 = vec![ |
25028 | KILL_CLIENT_REQUEST, |
25029 | 0, |
25030 | 0, |
25031 | 0, |
25032 | resource_bytes[0], |
25033 | resource_bytes[1], |
25034 | resource_bytes[2], |
25035 | resource_bytes[3], |
25036 | ]; |
25037 | let length_so_far = length_so_far + request0.len(); |
25038 | assert_eq!(length_so_far % 4, 0); |
25039 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
25040 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
25041 | (vec![request0.into()], vec![]) |
25042 | } |
25043 | /// Parse this request given its header, its body, and any fds that go along with it |
25044 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
25045 | if header.major_opcode != KILL_CLIENT_REQUEST { |
25046 | return Err(ParseError::InvalidValue); |
25047 | } |
25048 | let remaining = &[header.minor_opcode]; |
25049 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
25050 | let _ = remaining; |
25051 | let (resource, remaining) = u32::try_parse(value)?; |
25052 | let _ = remaining; |
25053 | Ok(KillClientRequest { |
25054 | resource, |
25055 | }) |
25056 | } |
25057 | } |
25058 | impl Request for KillClientRequest { |
25059 | const EXTENSION_NAME: Option<&'static str> = None; |
25060 | |
25061 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
25062 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
25063 | // Flatten the buffers into a single vector |
25064 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
25065 | (buf, fds) |
25066 | } |
25067 | } |
25068 | impl crate::x11_utils::VoidRequest for KillClientRequest { |
25069 | } |
25070 | |
25071 | /// Opcode for the RotateProperties request |
25072 | pub const ROTATE_PROPERTIES_REQUEST: u8 = 114; |
25073 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25074 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25075 | pub struct RotatePropertiesRequest<'input> { |
25076 | pub window: Window, |
25077 | pub delta: i16, |
25078 | pub atoms: Cow<'input, [Atom]>, |
25079 | } |
25080 | impl<'input> RotatePropertiesRequest<'input> { |
25081 | /// Serialize this request into bytes for the provided connection |
25082 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
25083 | let length_so_far = 0; |
25084 | let window_bytes = self.window.serialize(); |
25085 | let atoms_len = u16::try_from(self.atoms.len()).expect("`atoms` has too many elements" ); |
25086 | let atoms_len_bytes = atoms_len.serialize(); |
25087 | let delta_bytes = self.delta.serialize(); |
25088 | let mut request0 = vec![ |
25089 | ROTATE_PROPERTIES_REQUEST, |
25090 | 0, |
25091 | 0, |
25092 | 0, |
25093 | window_bytes[0], |
25094 | window_bytes[1], |
25095 | window_bytes[2], |
25096 | window_bytes[3], |
25097 | atoms_len_bytes[0], |
25098 | atoms_len_bytes[1], |
25099 | delta_bytes[0], |
25100 | delta_bytes[1], |
25101 | ]; |
25102 | let length_so_far = length_so_far + request0.len(); |
25103 | let atoms_bytes = self.atoms.serialize(); |
25104 | let length_so_far = length_so_far + atoms_bytes.len(); |
25105 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
25106 | let length_so_far = length_so_far + padding0.len(); |
25107 | assert_eq!(length_so_far % 4, 0); |
25108 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
25109 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
25110 | (vec![request0.into(), atoms_bytes.into(), padding0.into()], vec![]) |
25111 | } |
25112 | /// Parse this request given its header, its body, and any fds that go along with it |
25113 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
25114 | if header.major_opcode != ROTATE_PROPERTIES_REQUEST { |
25115 | return Err(ParseError::InvalidValue); |
25116 | } |
25117 | let remaining = &[header.minor_opcode]; |
25118 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
25119 | let _ = remaining; |
25120 | let (window, remaining) = Window::try_parse(value)?; |
25121 | let (atoms_len, remaining) = u16::try_parse(remaining)?; |
25122 | let (delta, remaining) = i16::try_parse(remaining)?; |
25123 | let (atoms, remaining) = crate::x11_utils::parse_list::<Atom>(remaining, atoms_len.try_to_usize()?)?; |
25124 | let _ = remaining; |
25125 | Ok(RotatePropertiesRequest { |
25126 | window, |
25127 | delta, |
25128 | atoms: Cow::Owned(atoms), |
25129 | }) |
25130 | } |
25131 | /// Clone all borrowed data in this RotatePropertiesRequest. |
25132 | pub fn into_owned(self) -> RotatePropertiesRequest<'static> { |
25133 | RotatePropertiesRequest { |
25134 | window: self.window, |
25135 | delta: self.delta, |
25136 | atoms: Cow::Owned(self.atoms.into_owned()), |
25137 | } |
25138 | } |
25139 | } |
25140 | impl<'input> Request for RotatePropertiesRequest<'input> { |
25141 | const EXTENSION_NAME: Option<&'static str> = None; |
25142 | |
25143 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
25144 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
25145 | // Flatten the buffers into a single vector |
25146 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
25147 | (buf, fds) |
25148 | } |
25149 | } |
25150 | impl<'input> crate::x11_utils::VoidRequest for RotatePropertiesRequest<'input> { |
25151 | } |
25152 | |
25153 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25154 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25155 | pub struct ScreenSaver(u8); |
25156 | impl ScreenSaver { |
25157 | pub const RESET: Self = Self(0); |
25158 | pub const ACTIVE: Self = Self(1); |
25159 | } |
25160 | impl From<ScreenSaver> for u8 { |
25161 | #[inline ] |
25162 | fn from(input: ScreenSaver) -> Self { |
25163 | input.0 |
25164 | } |
25165 | } |
25166 | impl From<ScreenSaver> for Option<u8> { |
25167 | #[inline ] |
25168 | fn from(input: ScreenSaver) -> Self { |
25169 | Some(input.0) |
25170 | } |
25171 | } |
25172 | impl From<ScreenSaver> for u16 { |
25173 | #[inline ] |
25174 | fn from(input: ScreenSaver) -> Self { |
25175 | u16::from(input.0) |
25176 | } |
25177 | } |
25178 | impl From<ScreenSaver> for Option<u16> { |
25179 | #[inline ] |
25180 | fn from(input: ScreenSaver) -> Self { |
25181 | Some(u16::from(input.0)) |
25182 | } |
25183 | } |
25184 | impl From<ScreenSaver> for u32 { |
25185 | #[inline ] |
25186 | fn from(input: ScreenSaver) -> Self { |
25187 | u32::from(input.0) |
25188 | } |
25189 | } |
25190 | impl From<ScreenSaver> for Option<u32> { |
25191 | #[inline ] |
25192 | fn from(input: ScreenSaver) -> Self { |
25193 | Some(u32::from(input.0)) |
25194 | } |
25195 | } |
25196 | impl From<u8> for ScreenSaver { |
25197 | #[inline ] |
25198 | fn from(value: u8) -> Self { |
25199 | Self(value) |
25200 | } |
25201 | } |
25202 | impl core::fmt::Debug for ScreenSaver { |
25203 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
25204 | let variants: [(u32, &str, &str); 2] = [ |
25205 | (Self::RESET.0.into(), "RESET" , "Reset" ), |
25206 | (Self::ACTIVE.0.into(), "ACTIVE" , "Active" ), |
25207 | ]; |
25208 | pretty_print_enum(fmt, self.0.into(), &variants) |
25209 | } |
25210 | } |
25211 | |
25212 | /// Opcode for the ForceScreenSaver request |
25213 | pub const FORCE_SCREEN_SAVER_REQUEST: u8 = 115; |
25214 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25215 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25216 | pub struct ForceScreenSaverRequest { |
25217 | pub mode: ScreenSaver, |
25218 | } |
25219 | impl ForceScreenSaverRequest { |
25220 | /// Serialize this request into bytes for the provided connection |
25221 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
25222 | let length_so_far = 0; |
25223 | let mode_bytes = u8::from(self.mode).serialize(); |
25224 | let mut request0 = vec![ |
25225 | FORCE_SCREEN_SAVER_REQUEST, |
25226 | mode_bytes[0], |
25227 | 0, |
25228 | 0, |
25229 | ]; |
25230 | let length_so_far = length_so_far + request0.len(); |
25231 | assert_eq!(length_so_far % 4, 0); |
25232 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
25233 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
25234 | (vec![request0.into()], vec![]) |
25235 | } |
25236 | /// Parse this request given its header, its body, and any fds that go along with it |
25237 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
25238 | if header.major_opcode != FORCE_SCREEN_SAVER_REQUEST { |
25239 | return Err(ParseError::InvalidValue); |
25240 | } |
25241 | let remaining = &[header.minor_opcode]; |
25242 | let (mode, remaining) = u8::try_parse(remaining)?; |
25243 | let mode = mode.into(); |
25244 | let _ = remaining; |
25245 | let _ = value; |
25246 | Ok(ForceScreenSaverRequest { |
25247 | mode, |
25248 | }) |
25249 | } |
25250 | } |
25251 | impl Request for ForceScreenSaverRequest { |
25252 | const EXTENSION_NAME: Option<&'static str> = None; |
25253 | |
25254 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
25255 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
25256 | // Flatten the buffers into a single vector |
25257 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
25258 | (buf, fds) |
25259 | } |
25260 | } |
25261 | impl crate::x11_utils::VoidRequest for ForceScreenSaverRequest { |
25262 | } |
25263 | |
25264 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25265 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25266 | pub struct MappingStatus(u8); |
25267 | impl MappingStatus { |
25268 | pub const SUCCESS: Self = Self(0); |
25269 | pub const BUSY: Self = Self(1); |
25270 | pub const FAILURE: Self = Self(2); |
25271 | } |
25272 | impl From<MappingStatus> for u8 { |
25273 | #[inline ] |
25274 | fn from(input: MappingStatus) -> Self { |
25275 | input.0 |
25276 | } |
25277 | } |
25278 | impl From<MappingStatus> for Option<u8> { |
25279 | #[inline ] |
25280 | fn from(input: MappingStatus) -> Self { |
25281 | Some(input.0) |
25282 | } |
25283 | } |
25284 | impl From<MappingStatus> for u16 { |
25285 | #[inline ] |
25286 | fn from(input: MappingStatus) -> Self { |
25287 | u16::from(input.0) |
25288 | } |
25289 | } |
25290 | impl From<MappingStatus> for Option<u16> { |
25291 | #[inline ] |
25292 | fn from(input: MappingStatus) -> Self { |
25293 | Some(u16::from(input.0)) |
25294 | } |
25295 | } |
25296 | impl From<MappingStatus> for u32 { |
25297 | #[inline ] |
25298 | fn from(input: MappingStatus) -> Self { |
25299 | u32::from(input.0) |
25300 | } |
25301 | } |
25302 | impl From<MappingStatus> for Option<u32> { |
25303 | #[inline ] |
25304 | fn from(input: MappingStatus) -> Self { |
25305 | Some(u32::from(input.0)) |
25306 | } |
25307 | } |
25308 | impl From<u8> for MappingStatus { |
25309 | #[inline ] |
25310 | fn from(value: u8) -> Self { |
25311 | Self(value) |
25312 | } |
25313 | } |
25314 | impl core::fmt::Debug for MappingStatus { |
25315 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
25316 | let variants: [(u32, &str, &str); 3] = [ |
25317 | (Self::SUCCESS.0.into(), "SUCCESS" , "Success" ), |
25318 | (Self::BUSY.0.into(), "BUSY" , "Busy" ), |
25319 | (Self::FAILURE.0.into(), "FAILURE" , "Failure" ), |
25320 | ]; |
25321 | pretty_print_enum(fmt, self.0.into(), &variants) |
25322 | } |
25323 | } |
25324 | |
25325 | /// Opcode for the SetPointerMapping request |
25326 | pub const SET_POINTER_MAPPING_REQUEST: u8 = 116; |
25327 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25328 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25329 | pub struct SetPointerMappingRequest<'input> { |
25330 | pub map: Cow<'input, [u8]>, |
25331 | } |
25332 | impl<'input> SetPointerMappingRequest<'input> { |
25333 | /// Serialize this request into bytes for the provided connection |
25334 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
25335 | let length_so_far = 0; |
25336 | let map_len = u8::try_from(self.map.len()).expect("`map` has too many elements" ); |
25337 | let map_len_bytes = map_len.serialize(); |
25338 | let mut request0 = vec![ |
25339 | SET_POINTER_MAPPING_REQUEST, |
25340 | map_len_bytes[0], |
25341 | 0, |
25342 | 0, |
25343 | ]; |
25344 | let length_so_far = length_so_far + request0.len(); |
25345 | let length_so_far = length_so_far + self.map.len(); |
25346 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
25347 | let length_so_far = length_so_far + padding0.len(); |
25348 | assert_eq!(length_so_far % 4, 0); |
25349 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
25350 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
25351 | (vec![request0.into(), self.map, padding0.into()], vec![]) |
25352 | } |
25353 | /// Parse this request given its header, its body, and any fds that go along with it |
25354 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
25355 | if header.major_opcode != SET_POINTER_MAPPING_REQUEST { |
25356 | return Err(ParseError::InvalidValue); |
25357 | } |
25358 | let remaining = &[header.minor_opcode]; |
25359 | let (map_len, remaining) = u8::try_parse(remaining)?; |
25360 | let _ = remaining; |
25361 | let (map, remaining) = crate::x11_utils::parse_u8_list(value, map_len.try_to_usize()?)?; |
25362 | let _ = remaining; |
25363 | Ok(SetPointerMappingRequest { |
25364 | map: Cow::Borrowed(map), |
25365 | }) |
25366 | } |
25367 | /// Clone all borrowed data in this SetPointerMappingRequest. |
25368 | pub fn into_owned(self) -> SetPointerMappingRequest<'static> { |
25369 | SetPointerMappingRequest { |
25370 | map: Cow::Owned(self.map.into_owned()), |
25371 | } |
25372 | } |
25373 | } |
25374 | impl<'input> Request for SetPointerMappingRequest<'input> { |
25375 | const EXTENSION_NAME: Option<&'static str> = None; |
25376 | |
25377 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
25378 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
25379 | // Flatten the buffers into a single vector |
25380 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
25381 | (buf, fds) |
25382 | } |
25383 | } |
25384 | impl<'input> crate::x11_utils::ReplyRequest for SetPointerMappingRequest<'input> { |
25385 | type Reply = SetPointerMappingReply; |
25386 | } |
25387 | |
25388 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25389 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25390 | pub struct SetPointerMappingReply { |
25391 | pub status: MappingStatus, |
25392 | pub sequence: u16, |
25393 | pub length: u32, |
25394 | } |
25395 | impl TryParse for SetPointerMappingReply { |
25396 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
25397 | let remaining: &[u8] = initial_value; |
25398 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
25399 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
25400 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
25401 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
25402 | if response_type != 1 { |
25403 | return Err(ParseError::InvalidValue); |
25404 | } |
25405 | let status: MappingStatus = status.into(); |
25406 | let result: SetPointerMappingReply = SetPointerMappingReply { status, sequence, length }; |
25407 | let _ = remaining; |
25408 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
25409 | .ok_or(err:ParseError::InsufficientData)?; |
25410 | Ok((result, remaining)) |
25411 | } |
25412 | } |
25413 | impl Serialize for SetPointerMappingReply { |
25414 | type Bytes = [u8; 8]; |
25415 | fn serialize(&self) -> [u8; 8] { |
25416 | let response_type_bytes = &[1]; |
25417 | let status_bytes = u8::from(self.status).serialize(); |
25418 | let sequence_bytes = self.sequence.serialize(); |
25419 | let length_bytes = self.length.serialize(); |
25420 | [ |
25421 | response_type_bytes[0], |
25422 | status_bytes[0], |
25423 | sequence_bytes[0], |
25424 | sequence_bytes[1], |
25425 | length_bytes[0], |
25426 | length_bytes[1], |
25427 | length_bytes[2], |
25428 | length_bytes[3], |
25429 | ] |
25430 | } |
25431 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
25432 | bytes.reserve(8); |
25433 | let response_type_bytes = &[1]; |
25434 | bytes.push(response_type_bytes[0]); |
25435 | u8::from(self.status).serialize_into(bytes); |
25436 | self.sequence.serialize_into(bytes); |
25437 | self.length.serialize_into(bytes); |
25438 | } |
25439 | } |
25440 | |
25441 | /// Opcode for the GetPointerMapping request |
25442 | pub const GET_POINTER_MAPPING_REQUEST: u8 = 117; |
25443 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25444 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25445 | pub struct GetPointerMappingRequest; |
25446 | impl GetPointerMappingRequest { |
25447 | /// Serialize this request into bytes for the provided connection |
25448 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
25449 | let length_so_far = 0; |
25450 | let mut request0 = vec![ |
25451 | GET_POINTER_MAPPING_REQUEST, |
25452 | 0, |
25453 | 0, |
25454 | 0, |
25455 | ]; |
25456 | let length_so_far = length_so_far + request0.len(); |
25457 | assert_eq!(length_so_far % 4, 0); |
25458 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
25459 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
25460 | (vec![request0.into()], vec![]) |
25461 | } |
25462 | /// Parse this request given its header, its body, and any fds that go along with it |
25463 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
25464 | if header.major_opcode != GET_POINTER_MAPPING_REQUEST { |
25465 | return Err(ParseError::InvalidValue); |
25466 | } |
25467 | let remaining = &[header.minor_opcode]; |
25468 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
25469 | let _ = remaining; |
25470 | let _ = value; |
25471 | Ok(GetPointerMappingRequest |
25472 | ) |
25473 | } |
25474 | } |
25475 | impl Request for GetPointerMappingRequest { |
25476 | const EXTENSION_NAME: Option<&'static str> = None; |
25477 | |
25478 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
25479 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
25480 | // Flatten the buffers into a single vector |
25481 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
25482 | (buf, fds) |
25483 | } |
25484 | } |
25485 | impl crate::x11_utils::ReplyRequest for GetPointerMappingRequest { |
25486 | type Reply = GetPointerMappingReply; |
25487 | } |
25488 | |
25489 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25490 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25491 | pub struct GetPointerMappingReply { |
25492 | pub sequence: u16, |
25493 | pub length: u32, |
25494 | pub map: Vec<u8>, |
25495 | } |
25496 | impl TryParse for GetPointerMappingReply { |
25497 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
25498 | let remaining: &[u8] = initial_value; |
25499 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
25500 | let (map_len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
25501 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
25502 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
25503 | let remaining: &[u8] = remaining.get(24..).ok_or(err:ParseError::InsufficientData)?; |
25504 | let (map: &[u8], remaining: &[u8]) = crate::x11_utils::parse_u8_list(data:remaining, list_length:map_len.try_to_usize()?)?; |
25505 | let map: Vec = map.to_vec(); |
25506 | if response_type != 1 { |
25507 | return Err(ParseError::InvalidValue); |
25508 | } |
25509 | let result: GetPointerMappingReply = GetPointerMappingReply { sequence, length, map }; |
25510 | let _ = remaining; |
25511 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
25512 | .ok_or(err:ParseError::InsufficientData)?; |
25513 | Ok((result, remaining)) |
25514 | } |
25515 | } |
25516 | impl Serialize for GetPointerMappingReply { |
25517 | type Bytes = Vec<u8>; |
25518 | fn serialize(&self) -> Vec<u8> { |
25519 | let mut result: Vec = Vec::new(); |
25520 | self.serialize_into(&mut result); |
25521 | result |
25522 | } |
25523 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
25524 | bytes.reserve(additional:32); |
25525 | let response_type_bytes: &[u8; 1] = &[1]; |
25526 | bytes.push(response_type_bytes[0]); |
25527 | let map_len: u8 = u8::try_from(self.map.len()).expect(msg:"`map` has too many elements" ); |
25528 | map_len.serialize_into(bytes); |
25529 | self.sequence.serialize_into(bytes); |
25530 | self.length.serialize_into(bytes); |
25531 | bytes.extend_from_slice(&[0; 24]); |
25532 | bytes.extend_from_slice(&self.map); |
25533 | } |
25534 | } |
25535 | impl GetPointerMappingReply { |
25536 | /// Get the value of the `map_len` field. |
25537 | /// |
25538 | /// The `map_len` field is used as the length field of the `map` field. |
25539 | /// This function computes the field's value again based on the length of the list. |
25540 | /// |
25541 | /// # Panics |
25542 | /// |
25543 | /// Panics if the value cannot be represented in the target type. This |
25544 | /// cannot happen with values of the struct received from the X11 server. |
25545 | pub fn map_len(&self) -> u8 { |
25546 | self.map.len() |
25547 | .try_into().unwrap() |
25548 | } |
25549 | } |
25550 | |
25551 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25552 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25553 | pub struct MapIndex(u8); |
25554 | impl MapIndex { |
25555 | pub const SHIFT: Self = Self(0); |
25556 | pub const LOCK: Self = Self(1); |
25557 | pub const CONTROL: Self = Self(2); |
25558 | pub const M1: Self = Self(3); |
25559 | pub const M2: Self = Self(4); |
25560 | pub const M3: Self = Self(5); |
25561 | pub const M4: Self = Self(6); |
25562 | pub const M5: Self = Self(7); |
25563 | } |
25564 | impl From<MapIndex> for u8 { |
25565 | #[inline ] |
25566 | fn from(input: MapIndex) -> Self { |
25567 | input.0 |
25568 | } |
25569 | } |
25570 | impl From<MapIndex> for Option<u8> { |
25571 | #[inline ] |
25572 | fn from(input: MapIndex) -> Self { |
25573 | Some(input.0) |
25574 | } |
25575 | } |
25576 | impl From<MapIndex> for u16 { |
25577 | #[inline ] |
25578 | fn from(input: MapIndex) -> Self { |
25579 | u16::from(input.0) |
25580 | } |
25581 | } |
25582 | impl From<MapIndex> for Option<u16> { |
25583 | #[inline ] |
25584 | fn from(input: MapIndex) -> Self { |
25585 | Some(u16::from(input.0)) |
25586 | } |
25587 | } |
25588 | impl From<MapIndex> for u32 { |
25589 | #[inline ] |
25590 | fn from(input: MapIndex) -> Self { |
25591 | u32::from(input.0) |
25592 | } |
25593 | } |
25594 | impl From<MapIndex> for Option<u32> { |
25595 | #[inline ] |
25596 | fn from(input: MapIndex) -> Self { |
25597 | Some(u32::from(input.0)) |
25598 | } |
25599 | } |
25600 | impl From<u8> for MapIndex { |
25601 | #[inline ] |
25602 | fn from(value: u8) -> Self { |
25603 | Self(value) |
25604 | } |
25605 | } |
25606 | impl core::fmt::Debug for MapIndex { |
25607 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
25608 | let variants: [(u32, &str, &str); 8] = [ |
25609 | (Self::SHIFT.0.into(), "SHIFT" , "Shift" ), |
25610 | (Self::LOCK.0.into(), "LOCK" , "Lock" ), |
25611 | (Self::CONTROL.0.into(), "CONTROL" , "Control" ), |
25612 | (Self::M1.0.into(), "M1" , "M1" ), |
25613 | (Self::M2.0.into(), "M2" , "M2" ), |
25614 | (Self::M3.0.into(), "M3" , "M3" ), |
25615 | (Self::M4.0.into(), "M4" , "M4" ), |
25616 | (Self::M5.0.into(), "M5" , "M5" ), |
25617 | ]; |
25618 | pretty_print_enum(fmt, self.0.into(), &variants) |
25619 | } |
25620 | } |
25621 | |
25622 | /// Opcode for the SetModifierMapping request |
25623 | pub const SET_MODIFIER_MAPPING_REQUEST: u8 = 118; |
25624 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25625 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25626 | pub struct SetModifierMappingRequest<'input> { |
25627 | pub keycodes: Cow<'input, [Keycode]>, |
25628 | } |
25629 | impl<'input> SetModifierMappingRequest<'input> { |
25630 | /// Serialize this request into bytes for the provided connection |
25631 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'input>> { |
25632 | let length_so_far = 0; |
25633 | assert_eq!(self.keycodes.len() % 8, 0, "`keycodes` has an incorrect length, must be a multiple of 8" ); |
25634 | let keycodes_per_modifier = u8::try_from(self.keycodes.len() / 8).expect("`keycodes` has too many elements" ); |
25635 | let keycodes_per_modifier_bytes = keycodes_per_modifier.serialize(); |
25636 | let mut request0 = vec![ |
25637 | SET_MODIFIER_MAPPING_REQUEST, |
25638 | keycodes_per_modifier_bytes[0], |
25639 | 0, |
25640 | 0, |
25641 | ]; |
25642 | let length_so_far = length_so_far + request0.len(); |
25643 | let length_so_far = length_so_far + self.keycodes.len(); |
25644 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
25645 | let length_so_far = length_so_far + padding0.len(); |
25646 | assert_eq!(length_so_far % 4, 0); |
25647 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
25648 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
25649 | (vec![request0.into(), self.keycodes, padding0.into()], vec![]) |
25650 | } |
25651 | /// Parse this request given its header, its body, and any fds that go along with it |
25652 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
25653 | if header.major_opcode != SET_MODIFIER_MAPPING_REQUEST { |
25654 | return Err(ParseError::InvalidValue); |
25655 | } |
25656 | let remaining = &[header.minor_opcode]; |
25657 | let (keycodes_per_modifier, remaining) = u8::try_parse(remaining)?; |
25658 | let _ = remaining; |
25659 | let (keycodes, remaining) = crate::x11_utils::parse_u8_list(value, u32::from(keycodes_per_modifier).checked_mul(8u32).ok_or(ParseError::InvalidExpression)?.try_to_usize()?)?; |
25660 | let _ = remaining; |
25661 | Ok(SetModifierMappingRequest { |
25662 | keycodes: Cow::Borrowed(keycodes), |
25663 | }) |
25664 | } |
25665 | /// Clone all borrowed data in this SetModifierMappingRequest. |
25666 | pub fn into_owned(self) -> SetModifierMappingRequest<'static> { |
25667 | SetModifierMappingRequest { |
25668 | keycodes: Cow::Owned(self.keycodes.into_owned()), |
25669 | } |
25670 | } |
25671 | } |
25672 | impl<'input> Request for SetModifierMappingRequest<'input> { |
25673 | const EXTENSION_NAME: Option<&'static str> = None; |
25674 | |
25675 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
25676 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
25677 | // Flatten the buffers into a single vector |
25678 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
25679 | (buf, fds) |
25680 | } |
25681 | } |
25682 | impl<'input> crate::x11_utils::ReplyRequest for SetModifierMappingRequest<'input> { |
25683 | type Reply = SetModifierMappingReply; |
25684 | } |
25685 | |
25686 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25687 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25688 | pub struct SetModifierMappingReply { |
25689 | pub status: MappingStatus, |
25690 | pub sequence: u16, |
25691 | pub length: u32, |
25692 | } |
25693 | impl TryParse for SetModifierMappingReply { |
25694 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
25695 | let remaining: &[u8] = initial_value; |
25696 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
25697 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
25698 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
25699 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
25700 | if response_type != 1 { |
25701 | return Err(ParseError::InvalidValue); |
25702 | } |
25703 | let status: MappingStatus = status.into(); |
25704 | let result: SetModifierMappingReply = SetModifierMappingReply { status, sequence, length }; |
25705 | let _ = remaining; |
25706 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
25707 | .ok_or(err:ParseError::InsufficientData)?; |
25708 | Ok((result, remaining)) |
25709 | } |
25710 | } |
25711 | impl Serialize for SetModifierMappingReply { |
25712 | type Bytes = [u8; 8]; |
25713 | fn serialize(&self) -> [u8; 8] { |
25714 | let response_type_bytes = &[1]; |
25715 | let status_bytes = u8::from(self.status).serialize(); |
25716 | let sequence_bytes = self.sequence.serialize(); |
25717 | let length_bytes = self.length.serialize(); |
25718 | [ |
25719 | response_type_bytes[0], |
25720 | status_bytes[0], |
25721 | sequence_bytes[0], |
25722 | sequence_bytes[1], |
25723 | length_bytes[0], |
25724 | length_bytes[1], |
25725 | length_bytes[2], |
25726 | length_bytes[3], |
25727 | ] |
25728 | } |
25729 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
25730 | bytes.reserve(8); |
25731 | let response_type_bytes = &[1]; |
25732 | bytes.push(response_type_bytes[0]); |
25733 | u8::from(self.status).serialize_into(bytes); |
25734 | self.sequence.serialize_into(bytes); |
25735 | self.length.serialize_into(bytes); |
25736 | } |
25737 | } |
25738 | |
25739 | /// Opcode for the GetModifierMapping request |
25740 | pub const GET_MODIFIER_MAPPING_REQUEST: u8 = 119; |
25741 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25742 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25743 | pub struct GetModifierMappingRequest; |
25744 | impl GetModifierMappingRequest { |
25745 | /// Serialize this request into bytes for the provided connection |
25746 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
25747 | let length_so_far = 0; |
25748 | let mut request0 = vec![ |
25749 | GET_MODIFIER_MAPPING_REQUEST, |
25750 | 0, |
25751 | 0, |
25752 | 0, |
25753 | ]; |
25754 | let length_so_far = length_so_far + request0.len(); |
25755 | assert_eq!(length_so_far % 4, 0); |
25756 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
25757 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
25758 | (vec![request0.into()], vec![]) |
25759 | } |
25760 | /// Parse this request given its header, its body, and any fds that go along with it |
25761 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
25762 | if header.major_opcode != GET_MODIFIER_MAPPING_REQUEST { |
25763 | return Err(ParseError::InvalidValue); |
25764 | } |
25765 | let remaining = &[header.minor_opcode]; |
25766 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
25767 | let _ = remaining; |
25768 | let _ = value; |
25769 | Ok(GetModifierMappingRequest |
25770 | ) |
25771 | } |
25772 | } |
25773 | impl Request for GetModifierMappingRequest { |
25774 | const EXTENSION_NAME: Option<&'static str> = None; |
25775 | |
25776 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
25777 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
25778 | // Flatten the buffers into a single vector |
25779 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
25780 | (buf, fds) |
25781 | } |
25782 | } |
25783 | impl crate::x11_utils::ReplyRequest for GetModifierMappingRequest { |
25784 | type Reply = GetModifierMappingReply; |
25785 | } |
25786 | |
25787 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25788 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25789 | pub struct GetModifierMappingReply { |
25790 | pub sequence: u16, |
25791 | pub length: u32, |
25792 | pub keycodes: Vec<Keycode>, |
25793 | } |
25794 | impl TryParse for GetModifierMappingReply { |
25795 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
25796 | let remaining: &[u8] = initial_value; |
25797 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
25798 | let (keycodes_per_modifier: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
25799 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
25800 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
25801 | let remaining: &[u8] = remaining.get(24..).ok_or(err:ParseError::InsufficientData)?; |
25802 | let (keycodes: &[u8], remaining: &[u8]) = crate::x11_utils::parse_u8_list(data:remaining, list_length:u32::from(keycodes_per_modifier).checked_mul(8u32).ok_or(err:ParseError::InvalidExpression)?.try_to_usize()?)?; |
25803 | let keycodes: Vec = keycodes.to_vec(); |
25804 | if response_type != 1 { |
25805 | return Err(ParseError::InvalidValue); |
25806 | } |
25807 | let result: GetModifierMappingReply = GetModifierMappingReply { sequence, length, keycodes }; |
25808 | let _ = remaining; |
25809 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
25810 | .ok_or(err:ParseError::InsufficientData)?; |
25811 | Ok((result, remaining)) |
25812 | } |
25813 | } |
25814 | impl Serialize for GetModifierMappingReply { |
25815 | type Bytes = Vec<u8>; |
25816 | fn serialize(&self) -> Vec<u8> { |
25817 | let mut result: Vec = Vec::new(); |
25818 | self.serialize_into(&mut result); |
25819 | result |
25820 | } |
25821 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
25822 | bytes.reserve(additional:32); |
25823 | let response_type_bytes: &[u8; 1] = &[1]; |
25824 | bytes.push(response_type_bytes[0]); |
25825 | assert_eq!(self.keycodes.len() % 8, 0, "`keycodes` has an incorrect length, must be a multiple of 8" ); |
25826 | let keycodes_per_modifier: u8 = u8::try_from(self.keycodes.len() / 8).expect(msg:"`keycodes` has too many elements" ); |
25827 | keycodes_per_modifier.serialize_into(bytes); |
25828 | self.sequence.serialize_into(bytes); |
25829 | self.length.serialize_into(bytes); |
25830 | bytes.extend_from_slice(&[0; 24]); |
25831 | bytes.extend_from_slice(&self.keycodes); |
25832 | } |
25833 | } |
25834 | impl GetModifierMappingReply { |
25835 | /// Get the value of the `keycodes_per_modifier` field. |
25836 | /// |
25837 | /// The `keycodes_per_modifier` field is used as the length field of the `keycodes` field. |
25838 | /// This function computes the field's value again based on the length of the list. |
25839 | /// |
25840 | /// # Panics |
25841 | /// |
25842 | /// Panics if the value cannot be represented in the target type. This |
25843 | /// cannot happen with values of the struct received from the X11 server. |
25844 | pub fn keycodes_per_modifier(&self) -> u8 { |
25845 | self.keycodes.len() |
25846 | .checked_div(8).unwrap() |
25847 | .try_into().unwrap() |
25848 | } |
25849 | } |
25850 | |
25851 | /// Opcode for the NoOperation request |
25852 | pub const NO_OPERATION_REQUEST: u8 = 127; |
25853 | #[derive (Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
25854 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
25855 | pub struct NoOperationRequest; |
25856 | impl NoOperationRequest { |
25857 | /// Serialize this request into bytes for the provided connection |
25858 | pub fn serialize(self) -> BufWithFds<PiecewiseBuf<'static>> { |
25859 | let length_so_far = 0; |
25860 | let mut request0 = vec![ |
25861 | NO_OPERATION_REQUEST, |
25862 | 0, |
25863 | 0, |
25864 | 0, |
25865 | ]; |
25866 | let length_so_far = length_so_far + request0.len(); |
25867 | assert_eq!(length_so_far % 4, 0); |
25868 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
25869 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
25870 | (vec![request0.into()], vec![]) |
25871 | } |
25872 | /// Parse this request given its header, its body, and any fds that go along with it |
25873 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
25874 | if header.major_opcode != NO_OPERATION_REQUEST { |
25875 | return Err(ParseError::InvalidValue); |
25876 | } |
25877 | let remaining = &[header.minor_opcode]; |
25878 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
25879 | let _ = remaining; |
25880 | let _ = value; |
25881 | Ok(NoOperationRequest |
25882 | ) |
25883 | } |
25884 | } |
25885 | impl Request for NoOperationRequest { |
25886 | const EXTENSION_NAME: Option<&'static str> = None; |
25887 | |
25888 | fn serialize(self, _major_opcode: u8) -> BufWithFds<Vec<u8>> { |
25889 | let (bufs: Vec>, fds: Vec) = self.serialize(); |
25890 | // Flatten the buffers into a single vector |
25891 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
25892 | (buf, fds) |
25893 | } |
25894 | } |
25895 | impl crate::x11_utils::VoidRequest for NoOperationRequest { |
25896 | } |
25897 | |
25898 | |