| 1 | // This file contains generated code. Do not edit directly. |
| 2 | // To regenerate this, run 'make'. |
| 3 | |
| 4 | //! Bindings to the `Input` X11 extension. |
| 5 | |
| 6 | #![allow (clippy::too_many_arguments)] |
| 7 | // The code generator is simpler if it can always use conversions |
| 8 | #![allow (clippy::useless_conversion)] |
| 9 | |
| 10 | #[allow (unused_imports)] |
| 11 | use alloc::borrow::Cow; |
| 12 | #[allow (unused_imports)] |
| 13 | use core::convert::TryInto; |
| 14 | use alloc::vec; |
| 15 | use alloc::vec::Vec; |
| 16 | use core::convert::TryFrom; |
| 17 | use crate::errors::ParseError; |
| 18 | #[allow (unused_imports)] |
| 19 | use crate::x11_utils::TryIntoUSize; |
| 20 | use crate::BufWithFds; |
| 21 | #[allow (unused_imports)] |
| 22 | use crate::utils::{RawFdContainer, pretty_print_bitmask, pretty_print_enum}; |
| 23 | #[allow (unused_imports)] |
| 24 | use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd}; |
| 25 | #[allow (unused_imports)] |
| 26 | use super::xfixes; |
| 27 | #[allow (unused_imports)] |
| 28 | use super::xproto; |
| 29 | |
| 30 | /// The X11 name of the extension for QueryExtension |
| 31 | pub const X11_EXTENSION_NAME: &str = "XInputExtension" ; |
| 32 | |
| 33 | /// The version number of this extension that this client library supports. |
| 34 | /// |
| 35 | /// This constant contains the version number of this extension that is supported |
| 36 | /// by this build of x11rb. For most things, it does not make sense to use this |
| 37 | /// information. If you need to send a `QueryVersion`, it is recommended to instead |
| 38 | /// send the maximum version of the extension that you need. |
| 39 | pub const X11_XML_VERSION: (u32, u32) = (2, 4); |
| 40 | |
| 41 | pub type EventClass = u32; |
| 42 | |
| 43 | pub type KeyCode = u8; |
| 44 | |
| 45 | pub type DeviceId = u16; |
| 46 | |
| 47 | pub type Fp1616 = i32; |
| 48 | |
| 49 | #[derive (Clone, Copy, Default)] |
| 50 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 51 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 52 | pub struct Fp3232 { |
| 53 | pub integral: i32, |
| 54 | pub frac: u32, |
| 55 | } |
| 56 | impl_debug_if_no_extra_traits!(Fp3232, "Fp3232" ); |
| 57 | impl TryParse for Fp3232 { |
| 58 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 59 | let (integral: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 60 | let (frac: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 61 | let result: Fp3232 = Fp3232 { integral, frac }; |
| 62 | Ok((result, remaining)) |
| 63 | } |
| 64 | } |
| 65 | impl Serialize for Fp3232 { |
| 66 | type Bytes = [u8; 8]; |
| 67 | fn serialize(&self) -> [u8; 8] { |
| 68 | let integral_bytes: [u8; 4] = self.integral.serialize(); |
| 69 | let frac_bytes: [u8; 4] = self.frac.serialize(); |
| 70 | [ |
| 71 | integral_bytes[0], |
| 72 | integral_bytes[1], |
| 73 | integral_bytes[2], |
| 74 | integral_bytes[3], |
| 75 | frac_bytes[0], |
| 76 | frac_bytes[1], |
| 77 | frac_bytes[2], |
| 78 | frac_bytes[3], |
| 79 | ] |
| 80 | } |
| 81 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 82 | bytes.reserve(additional:8); |
| 83 | self.integral.serialize_into(bytes); |
| 84 | self.frac.serialize_into(bytes); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Opcode for the GetExtensionVersion request |
| 89 | pub const GET_EXTENSION_VERSION_REQUEST: u8 = 1; |
| 90 | #[derive (Clone, Default)] |
| 91 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 92 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 93 | pub struct GetExtensionVersionRequest<'input> { |
| 94 | pub name: Cow<'input, [u8]>, |
| 95 | } |
| 96 | impl_debug_if_no_extra_traits!(GetExtensionVersionRequest<'_>, "GetExtensionVersionRequest" ); |
| 97 | impl<'input> GetExtensionVersionRequest<'input> { |
| 98 | /// Serialize this request into bytes for the provided connection |
| 99 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 100 | let length_so_far = 0; |
| 101 | let name_len = u16::try_from(self.name.len()).expect("`name` has too many elements" ); |
| 102 | let name_len_bytes = name_len.serialize(); |
| 103 | let mut request0 = vec![ |
| 104 | major_opcode, |
| 105 | GET_EXTENSION_VERSION_REQUEST, |
| 106 | 0, |
| 107 | 0, |
| 108 | name_len_bytes[0], |
| 109 | name_len_bytes[1], |
| 110 | 0, |
| 111 | 0, |
| 112 | ]; |
| 113 | let length_so_far = length_so_far + request0.len(); |
| 114 | let length_so_far = length_so_far + self.name.len(); |
| 115 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 116 | let length_so_far = length_so_far + padding0.len(); |
| 117 | assert_eq!(length_so_far % 4, 0); |
| 118 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 119 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 120 | ([request0.into(), self.name, padding0.into()], vec![]) |
| 121 | } |
| 122 | /// Parse this request given its header, its body, and any fds that go along with it |
| 123 | #[cfg (feature = "request-parsing" )] |
| 124 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 125 | if header.minor_opcode != GET_EXTENSION_VERSION_REQUEST { |
| 126 | return Err(ParseError::InvalidValue); |
| 127 | } |
| 128 | let (name_len, remaining) = u16::try_parse(value)?; |
| 129 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 130 | let (name, remaining) = crate::x11_utils::parse_u8_list(remaining, name_len.try_to_usize()?)?; |
| 131 | let _ = remaining; |
| 132 | Ok(GetExtensionVersionRequest { |
| 133 | name: Cow::Borrowed(name), |
| 134 | }) |
| 135 | } |
| 136 | /// Clone all borrowed data in this GetExtensionVersionRequest. |
| 137 | pub fn into_owned(self) -> GetExtensionVersionRequest<'static> { |
| 138 | GetExtensionVersionRequest { |
| 139 | name: Cow::Owned(self.name.into_owned()), |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | impl<'input> Request for GetExtensionVersionRequest<'input> { |
| 144 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 145 | |
| 146 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 147 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 148 | // Flatten the buffers into a single vector |
| 149 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 150 | (buf, fds) |
| 151 | } |
| 152 | } |
| 153 | impl<'input> crate::x11_utils::ReplyRequest for GetExtensionVersionRequest<'input> { |
| 154 | type Reply = GetExtensionVersionReply; |
| 155 | } |
| 156 | |
| 157 | #[derive (Clone, Copy, Default)] |
| 158 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 159 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 160 | pub struct GetExtensionVersionReply { |
| 161 | pub xi_reply_type: u8, |
| 162 | pub sequence: u16, |
| 163 | pub length: u32, |
| 164 | pub server_major: u16, |
| 165 | pub server_minor: u16, |
| 166 | pub present: bool, |
| 167 | } |
| 168 | impl_debug_if_no_extra_traits!(GetExtensionVersionReply, "GetExtensionVersionReply" ); |
| 169 | impl TryParse for GetExtensionVersionReply { |
| 170 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 171 | let remaining: &[u8] = initial_value; |
| 172 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 173 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 174 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 175 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 176 | let (server_major: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 177 | let (server_minor: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 178 | let (present: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
| 179 | let remaining: &[u8] = remaining.get(19..).ok_or(err:ParseError::InsufficientData)?; |
| 180 | if response_type != 1 { |
| 181 | return Err(ParseError::InvalidValue); |
| 182 | } |
| 183 | let result: GetExtensionVersionReply = GetExtensionVersionReply { xi_reply_type, sequence, length, server_major, server_minor, present }; |
| 184 | let _ = remaining; |
| 185 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 186 | .ok_or(err:ParseError::InsufficientData)?; |
| 187 | Ok((result, remaining)) |
| 188 | } |
| 189 | } |
| 190 | impl Serialize for GetExtensionVersionReply { |
| 191 | type Bytes = [u8; 32]; |
| 192 | fn serialize(&self) -> [u8; 32] { |
| 193 | let response_type_bytes = &[1]; |
| 194 | let xi_reply_type_bytes = self.xi_reply_type.serialize(); |
| 195 | let sequence_bytes = self.sequence.serialize(); |
| 196 | let length_bytes = self.length.serialize(); |
| 197 | let server_major_bytes = self.server_major.serialize(); |
| 198 | let server_minor_bytes = self.server_minor.serialize(); |
| 199 | let present_bytes = self.present.serialize(); |
| 200 | [ |
| 201 | response_type_bytes[0], |
| 202 | xi_reply_type_bytes[0], |
| 203 | sequence_bytes[0], |
| 204 | sequence_bytes[1], |
| 205 | length_bytes[0], |
| 206 | length_bytes[1], |
| 207 | length_bytes[2], |
| 208 | length_bytes[3], |
| 209 | server_major_bytes[0], |
| 210 | server_major_bytes[1], |
| 211 | server_minor_bytes[0], |
| 212 | server_minor_bytes[1], |
| 213 | present_bytes[0], |
| 214 | 0, |
| 215 | 0, |
| 216 | 0, |
| 217 | 0, |
| 218 | 0, |
| 219 | 0, |
| 220 | 0, |
| 221 | 0, |
| 222 | 0, |
| 223 | 0, |
| 224 | 0, |
| 225 | 0, |
| 226 | 0, |
| 227 | 0, |
| 228 | 0, |
| 229 | 0, |
| 230 | 0, |
| 231 | 0, |
| 232 | 0, |
| 233 | ] |
| 234 | } |
| 235 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 236 | bytes.reserve(32); |
| 237 | let response_type_bytes = &[1]; |
| 238 | bytes.push(response_type_bytes[0]); |
| 239 | self.xi_reply_type.serialize_into(bytes); |
| 240 | self.sequence.serialize_into(bytes); |
| 241 | self.length.serialize_into(bytes); |
| 242 | self.server_major.serialize_into(bytes); |
| 243 | self.server_minor.serialize_into(bytes); |
| 244 | self.present.serialize_into(bytes); |
| 245 | bytes.extend_from_slice(&[0; 19]); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 250 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 251 | pub struct DeviceUse(u8); |
| 252 | impl DeviceUse { |
| 253 | pub const IS_X_POINTER: Self = Self(0); |
| 254 | pub const IS_X_KEYBOARD: Self = Self(1); |
| 255 | pub const IS_X_EXTENSION_DEVICE: Self = Self(2); |
| 256 | pub const IS_X_EXTENSION_KEYBOARD: Self = Self(3); |
| 257 | pub const IS_X_EXTENSION_POINTER: Self = Self(4); |
| 258 | } |
| 259 | impl From<DeviceUse> for u8 { |
| 260 | #[inline ] |
| 261 | fn from(input: DeviceUse) -> Self { |
| 262 | input.0 |
| 263 | } |
| 264 | } |
| 265 | impl From<DeviceUse> for Option<u8> { |
| 266 | #[inline ] |
| 267 | fn from(input: DeviceUse) -> Self { |
| 268 | Some(input.0) |
| 269 | } |
| 270 | } |
| 271 | impl From<DeviceUse> for u16 { |
| 272 | #[inline ] |
| 273 | fn from(input: DeviceUse) -> Self { |
| 274 | u16::from(input.0) |
| 275 | } |
| 276 | } |
| 277 | impl From<DeviceUse> for Option<u16> { |
| 278 | #[inline ] |
| 279 | fn from(input: DeviceUse) -> Self { |
| 280 | Some(u16::from(input.0)) |
| 281 | } |
| 282 | } |
| 283 | impl From<DeviceUse> for u32 { |
| 284 | #[inline ] |
| 285 | fn from(input: DeviceUse) -> Self { |
| 286 | u32::from(input.0) |
| 287 | } |
| 288 | } |
| 289 | impl From<DeviceUse> for Option<u32> { |
| 290 | #[inline ] |
| 291 | fn from(input: DeviceUse) -> Self { |
| 292 | Some(u32::from(input.0)) |
| 293 | } |
| 294 | } |
| 295 | impl From<u8> for DeviceUse { |
| 296 | #[inline ] |
| 297 | fn from(value: u8) -> Self { |
| 298 | Self(value) |
| 299 | } |
| 300 | } |
| 301 | impl core::fmt::Debug for DeviceUse { |
| 302 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 303 | let variants: [(u32, &'static str, &'static …); 5] = [ |
| 304 | (Self::IS_X_POINTER.0.into(), "IS_X_POINTER" , "IsXPointer" ), |
| 305 | (Self::IS_X_KEYBOARD.0.into(), "IS_X_KEYBOARD" , "IsXKeyboard" ), |
| 306 | (Self::IS_X_EXTENSION_DEVICE.0.into(), "IS_X_EXTENSION_DEVICE" , "IsXExtensionDevice" ), |
| 307 | (Self::IS_X_EXTENSION_KEYBOARD.0.into(), "IS_X_EXTENSION_KEYBOARD" , "IsXExtensionKeyboard" ), |
| 308 | (Self::IS_X_EXTENSION_POINTER.0.into(), "IS_X_EXTENSION_POINTER" , "IsXExtensionPointer" ), |
| 309 | ]; |
| 310 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 315 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 316 | pub struct InputClass(u8); |
| 317 | impl InputClass { |
| 318 | pub const KEY: Self = Self(0); |
| 319 | pub const BUTTON: Self = Self(1); |
| 320 | pub const VALUATOR: Self = Self(2); |
| 321 | pub const FEEDBACK: Self = Self(3); |
| 322 | pub const PROXIMITY: Self = Self(4); |
| 323 | pub const FOCUS: Self = Self(5); |
| 324 | pub const OTHER: Self = Self(6); |
| 325 | } |
| 326 | impl From<InputClass> for u8 { |
| 327 | #[inline ] |
| 328 | fn from(input: InputClass) -> Self { |
| 329 | input.0 |
| 330 | } |
| 331 | } |
| 332 | impl From<InputClass> for Option<u8> { |
| 333 | #[inline ] |
| 334 | fn from(input: InputClass) -> Self { |
| 335 | Some(input.0) |
| 336 | } |
| 337 | } |
| 338 | impl From<InputClass> for u16 { |
| 339 | #[inline ] |
| 340 | fn from(input: InputClass) -> Self { |
| 341 | u16::from(input.0) |
| 342 | } |
| 343 | } |
| 344 | impl From<InputClass> for Option<u16> { |
| 345 | #[inline ] |
| 346 | fn from(input: InputClass) -> Self { |
| 347 | Some(u16::from(input.0)) |
| 348 | } |
| 349 | } |
| 350 | impl From<InputClass> for u32 { |
| 351 | #[inline ] |
| 352 | fn from(input: InputClass) -> Self { |
| 353 | u32::from(input.0) |
| 354 | } |
| 355 | } |
| 356 | impl From<InputClass> for Option<u32> { |
| 357 | #[inline ] |
| 358 | fn from(input: InputClass) -> Self { |
| 359 | Some(u32::from(input.0)) |
| 360 | } |
| 361 | } |
| 362 | impl From<u8> for InputClass { |
| 363 | #[inline ] |
| 364 | fn from(value: u8) -> Self { |
| 365 | Self(value) |
| 366 | } |
| 367 | } |
| 368 | impl core::fmt::Debug for InputClass { |
| 369 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 370 | let variants: [(u32, &'static str, &'static …); 7] = [ |
| 371 | (Self::KEY.0.into(), "KEY" , "Key" ), |
| 372 | (Self::BUTTON.0.into(), "BUTTON" , "Button" ), |
| 373 | (Self::VALUATOR.0.into(), "VALUATOR" , "Valuator" ), |
| 374 | (Self::FEEDBACK.0.into(), "FEEDBACK" , "Feedback" ), |
| 375 | (Self::PROXIMITY.0.into(), "PROXIMITY" , "Proximity" ), |
| 376 | (Self::FOCUS.0.into(), "FOCUS" , "Focus" ), |
| 377 | (Self::OTHER.0.into(), "OTHER" , "Other" ), |
| 378 | ]; |
| 379 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 384 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 385 | pub struct ValuatorMode(u8); |
| 386 | impl ValuatorMode { |
| 387 | pub const RELATIVE: Self = Self(0); |
| 388 | pub const ABSOLUTE: Self = Self(1); |
| 389 | } |
| 390 | impl From<ValuatorMode> for u8 { |
| 391 | #[inline ] |
| 392 | fn from(input: ValuatorMode) -> Self { |
| 393 | input.0 |
| 394 | } |
| 395 | } |
| 396 | impl From<ValuatorMode> for Option<u8> { |
| 397 | #[inline ] |
| 398 | fn from(input: ValuatorMode) -> Self { |
| 399 | Some(input.0) |
| 400 | } |
| 401 | } |
| 402 | impl From<ValuatorMode> for u16 { |
| 403 | #[inline ] |
| 404 | fn from(input: ValuatorMode) -> Self { |
| 405 | u16::from(input.0) |
| 406 | } |
| 407 | } |
| 408 | impl From<ValuatorMode> for Option<u16> { |
| 409 | #[inline ] |
| 410 | fn from(input: ValuatorMode) -> Self { |
| 411 | Some(u16::from(input.0)) |
| 412 | } |
| 413 | } |
| 414 | impl From<ValuatorMode> for u32 { |
| 415 | #[inline ] |
| 416 | fn from(input: ValuatorMode) -> Self { |
| 417 | u32::from(input.0) |
| 418 | } |
| 419 | } |
| 420 | impl From<ValuatorMode> for Option<u32> { |
| 421 | #[inline ] |
| 422 | fn from(input: ValuatorMode) -> Self { |
| 423 | Some(u32::from(input.0)) |
| 424 | } |
| 425 | } |
| 426 | impl From<u8> for ValuatorMode { |
| 427 | #[inline ] |
| 428 | fn from(value: u8) -> Self { |
| 429 | Self(value) |
| 430 | } |
| 431 | } |
| 432 | impl core::fmt::Debug for ValuatorMode { |
| 433 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 434 | let variants: [(u32, &'static str, &'static …); 2] = [ |
| 435 | (Self::RELATIVE.0.into(), "RELATIVE" , "Relative" ), |
| 436 | (Self::ABSOLUTE.0.into(), "ABSOLUTE" , "Absolute" ), |
| 437 | ]; |
| 438 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | #[derive (Clone, Copy, Default)] |
| 443 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 444 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 445 | pub struct DeviceInfo { |
| 446 | pub device_type: xproto::Atom, |
| 447 | pub device_id: u8, |
| 448 | pub num_class_info: u8, |
| 449 | pub device_use: DeviceUse, |
| 450 | } |
| 451 | impl_debug_if_no_extra_traits!(DeviceInfo, "DeviceInfo" ); |
| 452 | impl TryParse for DeviceInfo { |
| 453 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 454 | let (device_type: u32, remaining: &[u8]) = xproto::Atom::try_parse(remaining)?; |
| 455 | let (device_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 456 | let (num_class_info: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 457 | let (device_use: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 458 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 459 | let device_use: DeviceUse = device_use.into(); |
| 460 | let result: DeviceInfo = DeviceInfo { device_type, device_id, num_class_info, device_use }; |
| 461 | Ok((result, remaining)) |
| 462 | } |
| 463 | } |
| 464 | impl Serialize for DeviceInfo { |
| 465 | type Bytes = [u8; 8]; |
| 466 | fn serialize(&self) -> [u8; 8] { |
| 467 | let device_type_bytes = self.device_type.serialize(); |
| 468 | let device_id_bytes = self.device_id.serialize(); |
| 469 | let num_class_info_bytes = self.num_class_info.serialize(); |
| 470 | let device_use_bytes = u8::from(self.device_use).serialize(); |
| 471 | [ |
| 472 | device_type_bytes[0], |
| 473 | device_type_bytes[1], |
| 474 | device_type_bytes[2], |
| 475 | device_type_bytes[3], |
| 476 | device_id_bytes[0], |
| 477 | num_class_info_bytes[0], |
| 478 | device_use_bytes[0], |
| 479 | 0, |
| 480 | ] |
| 481 | } |
| 482 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 483 | bytes.reserve(8); |
| 484 | self.device_type.serialize_into(bytes); |
| 485 | self.device_id.serialize_into(bytes); |
| 486 | self.num_class_info.serialize_into(bytes); |
| 487 | u8::from(self.device_use).serialize_into(bytes); |
| 488 | bytes.extend_from_slice(&[0; 1]); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | #[derive (Clone, Copy, Default)] |
| 493 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 494 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 495 | pub struct KeyInfo { |
| 496 | pub class_id: InputClass, |
| 497 | pub len: u8, |
| 498 | pub min_keycode: KeyCode, |
| 499 | pub max_keycode: KeyCode, |
| 500 | pub num_keys: u16, |
| 501 | } |
| 502 | impl_debug_if_no_extra_traits!(KeyInfo, "KeyInfo" ); |
| 503 | impl TryParse for KeyInfo { |
| 504 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 505 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 506 | let (len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 507 | let (min_keycode: u8, remaining: &[u8]) = KeyCode::try_parse(remaining)?; |
| 508 | let (max_keycode: u8, remaining: &[u8]) = KeyCode::try_parse(remaining)?; |
| 509 | let (num_keys: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 510 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 511 | let class_id: InputClass = class_id.into(); |
| 512 | let result: KeyInfo = KeyInfo { class_id, len, min_keycode, max_keycode, num_keys }; |
| 513 | Ok((result, remaining)) |
| 514 | } |
| 515 | } |
| 516 | impl Serialize for KeyInfo { |
| 517 | type Bytes = [u8; 8]; |
| 518 | fn serialize(&self) -> [u8; 8] { |
| 519 | let class_id_bytes = u8::from(self.class_id).serialize(); |
| 520 | let len_bytes = self.len.serialize(); |
| 521 | let min_keycode_bytes = self.min_keycode.serialize(); |
| 522 | let max_keycode_bytes = self.max_keycode.serialize(); |
| 523 | let num_keys_bytes = self.num_keys.serialize(); |
| 524 | [ |
| 525 | class_id_bytes[0], |
| 526 | len_bytes[0], |
| 527 | min_keycode_bytes[0], |
| 528 | max_keycode_bytes[0], |
| 529 | num_keys_bytes[0], |
| 530 | num_keys_bytes[1], |
| 531 | 0, |
| 532 | 0, |
| 533 | ] |
| 534 | } |
| 535 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 536 | bytes.reserve(8); |
| 537 | u8::from(self.class_id).serialize_into(bytes); |
| 538 | self.len.serialize_into(bytes); |
| 539 | self.min_keycode.serialize_into(bytes); |
| 540 | self.max_keycode.serialize_into(bytes); |
| 541 | self.num_keys.serialize_into(bytes); |
| 542 | bytes.extend_from_slice(&[0; 2]); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | #[derive (Clone, Copy, Default)] |
| 547 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 548 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 549 | pub struct ButtonInfo { |
| 550 | pub class_id: InputClass, |
| 551 | pub len: u8, |
| 552 | pub num_buttons: u16, |
| 553 | } |
| 554 | impl_debug_if_no_extra_traits!(ButtonInfo, "ButtonInfo" ); |
| 555 | impl TryParse for ButtonInfo { |
| 556 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 557 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 558 | let (len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 559 | let (num_buttons: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 560 | let class_id: InputClass = class_id.into(); |
| 561 | let result: ButtonInfo = ButtonInfo { class_id, len, num_buttons }; |
| 562 | Ok((result, remaining)) |
| 563 | } |
| 564 | } |
| 565 | impl Serialize for ButtonInfo { |
| 566 | type Bytes = [u8; 4]; |
| 567 | fn serialize(&self) -> [u8; 4] { |
| 568 | let class_id_bytes: [u8; 1] = u8::from(self.class_id).serialize(); |
| 569 | let len_bytes: [u8; 1] = self.len.serialize(); |
| 570 | let num_buttons_bytes: [u8; 2] = self.num_buttons.serialize(); |
| 571 | [ |
| 572 | class_id_bytes[0], |
| 573 | len_bytes[0], |
| 574 | num_buttons_bytes[0], |
| 575 | num_buttons_bytes[1], |
| 576 | ] |
| 577 | } |
| 578 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 579 | bytes.reserve(additional:4); |
| 580 | u8::from(self.class_id).serialize_into(bytes); |
| 581 | self.len.serialize_into(bytes); |
| 582 | self.num_buttons.serialize_into(bytes); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | #[derive (Clone, Copy, Default)] |
| 587 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 588 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 589 | pub struct AxisInfo { |
| 590 | pub resolution: u32, |
| 591 | pub minimum: i32, |
| 592 | pub maximum: i32, |
| 593 | } |
| 594 | impl_debug_if_no_extra_traits!(AxisInfo, "AxisInfo" ); |
| 595 | impl TryParse for AxisInfo { |
| 596 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 597 | let (resolution: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 598 | let (minimum: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 599 | let (maximum: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 600 | let result: AxisInfo = AxisInfo { resolution, minimum, maximum }; |
| 601 | Ok((result, remaining)) |
| 602 | } |
| 603 | } |
| 604 | impl Serialize for AxisInfo { |
| 605 | type Bytes = [u8; 12]; |
| 606 | fn serialize(&self) -> [u8; 12] { |
| 607 | let resolution_bytes = self.resolution.serialize(); |
| 608 | let minimum_bytes = self.minimum.serialize(); |
| 609 | let maximum_bytes = self.maximum.serialize(); |
| 610 | [ |
| 611 | resolution_bytes[0], |
| 612 | resolution_bytes[1], |
| 613 | resolution_bytes[2], |
| 614 | resolution_bytes[3], |
| 615 | minimum_bytes[0], |
| 616 | minimum_bytes[1], |
| 617 | minimum_bytes[2], |
| 618 | minimum_bytes[3], |
| 619 | maximum_bytes[0], |
| 620 | maximum_bytes[1], |
| 621 | maximum_bytes[2], |
| 622 | maximum_bytes[3], |
| 623 | ] |
| 624 | } |
| 625 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 626 | bytes.reserve(12); |
| 627 | self.resolution.serialize_into(bytes); |
| 628 | self.minimum.serialize_into(bytes); |
| 629 | self.maximum.serialize_into(bytes); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | #[derive (Clone, Default)] |
| 634 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 635 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 636 | pub struct ValuatorInfo { |
| 637 | pub class_id: InputClass, |
| 638 | pub len: u8, |
| 639 | pub mode: ValuatorMode, |
| 640 | pub motion_size: u32, |
| 641 | pub axes: Vec<AxisInfo>, |
| 642 | } |
| 643 | impl_debug_if_no_extra_traits!(ValuatorInfo, "ValuatorInfo" ); |
| 644 | impl TryParse for ValuatorInfo { |
| 645 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 646 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 647 | let (len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 648 | let (axes_len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 649 | let (mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 650 | let (motion_size: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 651 | let (axes: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<AxisInfo>(data:remaining, list_length:axes_len.try_to_usize()?)?; |
| 652 | let class_id: InputClass = class_id.into(); |
| 653 | let mode: ValuatorMode = mode.into(); |
| 654 | let result: ValuatorInfo = ValuatorInfo { class_id, len, mode, motion_size, axes }; |
| 655 | Ok((result, remaining)) |
| 656 | } |
| 657 | } |
| 658 | impl Serialize for ValuatorInfo { |
| 659 | type Bytes = Vec<u8>; |
| 660 | fn serialize(&self) -> Vec<u8> { |
| 661 | let mut result: Vec = Vec::new(); |
| 662 | self.serialize_into(&mut result); |
| 663 | result |
| 664 | } |
| 665 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 666 | bytes.reserve(additional:8); |
| 667 | u8::from(self.class_id).serialize_into(bytes); |
| 668 | self.len.serialize_into(bytes); |
| 669 | let axes_len: u8 = u8::try_from(self.axes.len()).expect(msg:"`axes` has too many elements" ); |
| 670 | axes_len.serialize_into(bytes); |
| 671 | u8::from(self.mode).serialize_into(bytes); |
| 672 | self.motion_size.serialize_into(bytes); |
| 673 | self.axes.serialize_into(bytes); |
| 674 | } |
| 675 | } |
| 676 | impl ValuatorInfo { |
| 677 | /// Get the value of the `axes_len` field. |
| 678 | /// |
| 679 | /// The `axes_len` field is used as the length field of the `axes` field. |
| 680 | /// This function computes the field's value again based on the length of the list. |
| 681 | /// |
| 682 | /// # Panics |
| 683 | /// |
| 684 | /// Panics if the value cannot be represented in the target type. This |
| 685 | /// cannot happen with values of the struct received from the X11 server. |
| 686 | pub fn axes_len(&self) -> u8 { |
| 687 | self.axes.len() |
| 688 | .try_into().unwrap() |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | #[derive (Clone, Copy)] |
| 693 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 694 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 695 | pub struct InputInfoInfoKey { |
| 696 | pub min_keycode: KeyCode, |
| 697 | pub max_keycode: KeyCode, |
| 698 | pub num_keys: u16, |
| 699 | } |
| 700 | impl_debug_if_no_extra_traits!(InputInfoInfoKey, "InputInfoInfoKey" ); |
| 701 | impl TryParse for InputInfoInfoKey { |
| 702 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 703 | let (min_keycode: u8, remaining: &[u8]) = KeyCode::try_parse(remaining)?; |
| 704 | let (max_keycode: u8, remaining: &[u8]) = KeyCode::try_parse(remaining)?; |
| 705 | let (num_keys: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 706 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 707 | let result: InputInfoInfoKey = InputInfoInfoKey { min_keycode, max_keycode, num_keys }; |
| 708 | Ok((result, remaining)) |
| 709 | } |
| 710 | } |
| 711 | impl Serialize for InputInfoInfoKey { |
| 712 | type Bytes = [u8; 6]; |
| 713 | fn serialize(&self) -> [u8; 6] { |
| 714 | let min_keycode_bytes: [u8; 1] = self.min_keycode.serialize(); |
| 715 | let max_keycode_bytes: [u8; 1] = self.max_keycode.serialize(); |
| 716 | let num_keys_bytes: [u8; 2] = self.num_keys.serialize(); |
| 717 | [ |
| 718 | min_keycode_bytes[0], |
| 719 | max_keycode_bytes[0], |
| 720 | num_keys_bytes[0], |
| 721 | num_keys_bytes[1], |
| 722 | 0, |
| 723 | 0, |
| 724 | ] |
| 725 | } |
| 726 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 727 | bytes.reserve(additional:6); |
| 728 | self.min_keycode.serialize_into(bytes); |
| 729 | self.max_keycode.serialize_into(bytes); |
| 730 | self.num_keys.serialize_into(bytes); |
| 731 | bytes.extend_from_slice(&[0; 2]); |
| 732 | } |
| 733 | } |
| 734 | #[derive (Clone, Copy)] |
| 735 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 736 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 737 | pub struct InputInfoInfoButton { |
| 738 | pub num_buttons: u16, |
| 739 | } |
| 740 | impl_debug_if_no_extra_traits!(InputInfoInfoButton, "InputInfoInfoButton" ); |
| 741 | impl TryParse for InputInfoInfoButton { |
| 742 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 743 | let (num_buttons: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 744 | let result: InputInfoInfoButton = InputInfoInfoButton { num_buttons }; |
| 745 | Ok((result, remaining)) |
| 746 | } |
| 747 | } |
| 748 | impl Serialize for InputInfoInfoButton { |
| 749 | type Bytes = [u8; 2]; |
| 750 | fn serialize(&self) -> [u8; 2] { |
| 751 | let num_buttons_bytes: [u8; 2] = self.num_buttons.serialize(); |
| 752 | [ |
| 753 | num_buttons_bytes[0], |
| 754 | num_buttons_bytes[1], |
| 755 | ] |
| 756 | } |
| 757 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 758 | bytes.reserve(additional:2); |
| 759 | self.num_buttons.serialize_into(bytes); |
| 760 | } |
| 761 | } |
| 762 | #[derive (Clone)] |
| 763 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 764 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 765 | pub struct InputInfoInfoValuator { |
| 766 | pub mode: ValuatorMode, |
| 767 | pub motion_size: u32, |
| 768 | pub axes: Vec<AxisInfo>, |
| 769 | } |
| 770 | impl_debug_if_no_extra_traits!(InputInfoInfoValuator, "InputInfoInfoValuator" ); |
| 771 | impl TryParse for InputInfoInfoValuator { |
| 772 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 773 | let (axes_len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 774 | let (mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 775 | let (motion_size: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 776 | let (axes: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<AxisInfo>(data:remaining, list_length:axes_len.try_to_usize()?)?; |
| 777 | let mode: ValuatorMode = mode.into(); |
| 778 | let result: InputInfoInfoValuator = InputInfoInfoValuator { mode, motion_size, axes }; |
| 779 | Ok((result, remaining)) |
| 780 | } |
| 781 | } |
| 782 | impl Serialize for InputInfoInfoValuator { |
| 783 | type Bytes = Vec<u8>; |
| 784 | fn serialize(&self) -> Vec<u8> { |
| 785 | let mut result: Vec = Vec::new(); |
| 786 | self.serialize_into(&mut result); |
| 787 | result |
| 788 | } |
| 789 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 790 | bytes.reserve(additional:6); |
| 791 | let axes_len: u8 = u8::try_from(self.axes.len()).expect(msg:"`axes` has too many elements" ); |
| 792 | axes_len.serialize_into(bytes); |
| 793 | u8::from(self.mode).serialize_into(bytes); |
| 794 | self.motion_size.serialize_into(bytes); |
| 795 | self.axes.serialize_into(bytes); |
| 796 | } |
| 797 | } |
| 798 | impl InputInfoInfoValuator { |
| 799 | /// Get the value of the `axes_len` field. |
| 800 | /// |
| 801 | /// The `axes_len` field is used as the length field of the `axes` field. |
| 802 | /// This function computes the field's value again based on the length of the list. |
| 803 | /// |
| 804 | /// # Panics |
| 805 | /// |
| 806 | /// Panics if the value cannot be represented in the target type. This |
| 807 | /// cannot happen with values of the struct received from the X11 server. |
| 808 | pub fn axes_len(&self) -> u8 { |
| 809 | self.axes.len() |
| 810 | .try_into().unwrap() |
| 811 | } |
| 812 | } |
| 813 | #[derive (Clone)] |
| 814 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 815 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 816 | pub enum InputInfoInfo { |
| 817 | Key(InputInfoInfoKey), |
| 818 | Button(InputInfoInfoButton), |
| 819 | Valuator(InputInfoInfoValuator), |
| 820 | /// This variant is returned when the server sends a discriminant |
| 821 | /// value that does not match any of the defined by the protocol. |
| 822 | /// |
| 823 | /// Usually, this should be considered a parsing error, but there |
| 824 | /// are some cases where the server violates the protocol. |
| 825 | /// |
| 826 | /// Trying to use `serialize` or `serialize_into` with this variant |
| 827 | /// will raise a panic. |
| 828 | InvalidValue(u8), |
| 829 | } |
| 830 | impl_debug_if_no_extra_traits!(InputInfoInfo, "InputInfoInfo" ); |
| 831 | impl InputInfoInfo { |
| 832 | #[cfg_attr (not(feature = "request-parsing" ), allow(dead_code))] |
| 833 | fn try_parse(value: &[u8], class_id: u8) -> Result<(Self, &[u8]), ParseError> { |
| 834 | let switch_expr = u8::from(class_id); |
| 835 | let mut outer_remaining = value; |
| 836 | let mut parse_result = None; |
| 837 | if switch_expr == u8::from(InputClass::KEY) { |
| 838 | let (key, new_remaining) = InputInfoInfoKey::try_parse(outer_remaining)?; |
| 839 | outer_remaining = new_remaining; |
| 840 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 841 | parse_result = Some(InputInfoInfo::Key(key)); |
| 842 | } |
| 843 | if switch_expr == u8::from(InputClass::BUTTON) { |
| 844 | let (button, new_remaining) = InputInfoInfoButton::try_parse(outer_remaining)?; |
| 845 | outer_remaining = new_remaining; |
| 846 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 847 | parse_result = Some(InputInfoInfo::Button(button)); |
| 848 | } |
| 849 | if switch_expr == u8::from(InputClass::VALUATOR) { |
| 850 | let (valuator, new_remaining) = InputInfoInfoValuator::try_parse(outer_remaining)?; |
| 851 | outer_remaining = new_remaining; |
| 852 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 853 | parse_result = Some(InputInfoInfo::Valuator(valuator)); |
| 854 | } |
| 855 | match parse_result { |
| 856 | None => Ok((InputInfoInfo::InvalidValue(switch_expr), &[])), |
| 857 | Some(result) => Ok((result, outer_remaining)), |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | impl InputInfoInfo { |
| 862 | pub fn as_key(&self) -> Option<&InputInfoInfoKey> { |
| 863 | match self { |
| 864 | InputInfoInfo::Key(value: &InputInfoInfoKey) => Some(value), |
| 865 | _ => None, |
| 866 | } |
| 867 | } |
| 868 | pub fn as_button(&self) -> Option<&InputInfoInfoButton> { |
| 869 | match self { |
| 870 | InputInfoInfo::Button(value: &InputInfoInfoButton) => Some(value), |
| 871 | _ => None, |
| 872 | } |
| 873 | } |
| 874 | pub fn as_valuator(&self) -> Option<&InputInfoInfoValuator> { |
| 875 | match self { |
| 876 | InputInfoInfo::Valuator(value: &InputInfoInfoValuator) => Some(value), |
| 877 | _ => None, |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | impl InputInfoInfo { |
| 882 | #[allow (dead_code)] |
| 883 | fn serialize(&self, class_id: u8) -> Vec<u8> { |
| 884 | let mut result: Vec = Vec::new(); |
| 885 | self.serialize_into(&mut result, class_id:u8::from(class_id)); |
| 886 | result |
| 887 | } |
| 888 | fn serialize_into(&self, bytes: &mut Vec<u8>, class_id: u8) { |
| 889 | assert_eq!(self.switch_expr(), u8::from(class_id), "switch `info` has an inconsistent discriminant" ); |
| 890 | match self { |
| 891 | InputInfoInfo::Key(key: &InputInfoInfoKey) => key.serialize_into(bytes), |
| 892 | InputInfoInfo::Button(button: &InputInfoInfoButton) => button.serialize_into(bytes), |
| 893 | InputInfoInfo::Valuator(valuator: &InputInfoInfoValuator) => valuator.serialize_into(bytes), |
| 894 | InputInfoInfo::InvalidValue(_) => panic!("attempted to serialize invalid switch case" ), |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | impl InputInfoInfo { |
| 899 | fn switch_expr(&self) -> u8 { |
| 900 | match self { |
| 901 | InputInfoInfo::Key(_) => u8::from(InputClass::KEY), |
| 902 | InputInfoInfo::Button(_) => u8::from(InputClass::BUTTON), |
| 903 | InputInfoInfo::Valuator(_) => u8::from(InputClass::VALUATOR), |
| 904 | InputInfoInfo::InvalidValue(switch_expr: &u8) => *switch_expr, |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | #[derive (Clone)] |
| 910 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 911 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 912 | pub struct InputInfo { |
| 913 | pub len: u8, |
| 914 | pub info: InputInfoInfo, |
| 915 | } |
| 916 | impl_debug_if_no_extra_traits!(InputInfo, "InputInfo" ); |
| 917 | impl TryParse for InputInfo { |
| 918 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 919 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 920 | let (len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 921 | let (info: InputInfoInfo, remaining: &[u8]) = InputInfoInfo::try_parse(value:remaining, class_id:u8::from(class_id))?; |
| 922 | let result: InputInfo = InputInfo { len, info }; |
| 923 | Ok((result, remaining)) |
| 924 | } |
| 925 | } |
| 926 | impl Serialize for InputInfo { |
| 927 | type Bytes = Vec<u8>; |
| 928 | fn serialize(&self) -> Vec<u8> { |
| 929 | let mut result: Vec = Vec::new(); |
| 930 | self.serialize_into(&mut result); |
| 931 | result |
| 932 | } |
| 933 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 934 | bytes.reserve(additional:2); |
| 935 | let class_id: u8 = self.info.switch_expr(); |
| 936 | class_id.serialize_into(bytes); |
| 937 | self.len.serialize_into(bytes); |
| 938 | self.info.serialize_into(bytes, class_id:u8::from(class_id)); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | #[derive (Clone, Default)] |
| 943 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 944 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 945 | pub struct DeviceName { |
| 946 | pub string: Vec<u8>, |
| 947 | } |
| 948 | impl_debug_if_no_extra_traits!(DeviceName, "DeviceName" ); |
| 949 | impl TryParse for DeviceName { |
| 950 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 951 | let (len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 952 | let (string: &[u8], remaining: &[u8]) = crate::x11_utils::parse_u8_list(data:remaining, list_length:len.try_to_usize()?)?; |
| 953 | let string: Vec = string.to_vec(); |
| 954 | let result: DeviceName = DeviceName { string }; |
| 955 | Ok((result, remaining)) |
| 956 | } |
| 957 | } |
| 958 | impl Serialize for DeviceName { |
| 959 | type Bytes = Vec<u8>; |
| 960 | fn serialize(&self) -> Vec<u8> { |
| 961 | let mut result: Vec = Vec::new(); |
| 962 | self.serialize_into(&mut result); |
| 963 | result |
| 964 | } |
| 965 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 966 | let len: u8 = u8::try_from(self.string.len()).expect(msg:"`string` has too many elements" ); |
| 967 | len.serialize_into(bytes); |
| 968 | bytes.extend_from_slice(&self.string); |
| 969 | } |
| 970 | } |
| 971 | #[allow (clippy::len_without_is_empty)] // This is not a container and is_empty() makes no sense |
| 972 | impl DeviceName { |
| 973 | /// Get the value of the `len` field. |
| 974 | /// |
| 975 | /// The `len` field is used as the length field of the `string` field. |
| 976 | /// This function computes the field's value again based on the length of the list. |
| 977 | /// |
| 978 | /// # Panics |
| 979 | /// |
| 980 | /// Panics if the value cannot be represented in the target type. This |
| 981 | /// cannot happen with values of the struct received from the X11 server. |
| 982 | pub fn len(&self) -> u8 { |
| 983 | self.string.len() |
| 984 | .try_into().unwrap() |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | /// Opcode for the ListInputDevices request |
| 989 | pub const LIST_INPUT_DEVICES_REQUEST: u8 = 2; |
| 990 | #[derive (Clone, Copy, Default)] |
| 991 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 992 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 993 | pub struct ListInputDevicesRequest; |
| 994 | impl_debug_if_no_extra_traits!(ListInputDevicesRequest, "ListInputDevicesRequest" ); |
| 995 | impl ListInputDevicesRequest { |
| 996 | /// Serialize this request into bytes for the provided connection |
| 997 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 998 | let length_so_far = 0; |
| 999 | let mut request0 = vec![ |
| 1000 | major_opcode, |
| 1001 | LIST_INPUT_DEVICES_REQUEST, |
| 1002 | 0, |
| 1003 | 0, |
| 1004 | ]; |
| 1005 | let length_so_far = length_so_far + request0.len(); |
| 1006 | assert_eq!(length_so_far % 4, 0); |
| 1007 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 1008 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 1009 | ([request0.into()], vec![]) |
| 1010 | } |
| 1011 | /// Parse this request given its header, its body, and any fds that go along with it |
| 1012 | #[cfg (feature = "request-parsing" )] |
| 1013 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 1014 | if header.minor_opcode != LIST_INPUT_DEVICES_REQUEST { |
| 1015 | return Err(ParseError::InvalidValue); |
| 1016 | } |
| 1017 | let _ = value; |
| 1018 | Ok(ListInputDevicesRequest |
| 1019 | ) |
| 1020 | } |
| 1021 | } |
| 1022 | impl Request for ListInputDevicesRequest { |
| 1023 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 1024 | |
| 1025 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 1026 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 1027 | // Flatten the buffers into a single vector |
| 1028 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 1029 | (buf, fds) |
| 1030 | } |
| 1031 | } |
| 1032 | impl crate::x11_utils::ReplyRequest for ListInputDevicesRequest { |
| 1033 | type Reply = ListInputDevicesReply; |
| 1034 | } |
| 1035 | |
| 1036 | #[derive (Clone)] |
| 1037 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1038 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1039 | pub struct ListInputDevicesReply { |
| 1040 | pub xi_reply_type: u8, |
| 1041 | pub sequence: u16, |
| 1042 | pub length: u32, |
| 1043 | pub devices: Vec<DeviceInfo>, |
| 1044 | pub infos: Vec<InputInfo>, |
| 1045 | pub names: Vec<xproto::Str>, |
| 1046 | } |
| 1047 | impl_debug_if_no_extra_traits!(ListInputDevicesReply, "ListInputDevicesReply" ); |
| 1048 | impl TryParse for ListInputDevicesReply { |
| 1049 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 1050 | let remaining = initial_value; |
| 1051 | let value = remaining; |
| 1052 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 1053 | let (xi_reply_type, remaining) = u8::try_parse(remaining)?; |
| 1054 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 1055 | let (length, remaining) = u32::try_parse(remaining)?; |
| 1056 | let (devices_len, remaining) = u8::try_parse(remaining)?; |
| 1057 | let remaining = remaining.get(23..).ok_or(ParseError::InsufficientData)?; |
| 1058 | let (devices, remaining) = crate::x11_utils::parse_list::<DeviceInfo>(remaining, devices_len.try_to_usize()?)?; |
| 1059 | let (infos, remaining) = crate::x11_utils::parse_list::<InputInfo>(remaining, devices.iter().try_fold(0u32, |acc, x| acc.checked_add(u32::from(x.num_class_info)).ok_or(ParseError::InvalidExpression))?.try_to_usize()?)?; |
| 1060 | let (names, remaining) = crate::x11_utils::parse_list::<xproto::Str>(remaining, devices_len.try_to_usize()?)?; |
| 1061 | // Align offset to multiple of 4 |
| 1062 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 1063 | let misalignment = (4 - (offset % 4)) % 4; |
| 1064 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
| 1065 | if response_type != 1 { |
| 1066 | return Err(ParseError::InvalidValue); |
| 1067 | } |
| 1068 | let result = ListInputDevicesReply { xi_reply_type, sequence, length, devices, infos, names }; |
| 1069 | let _ = remaining; |
| 1070 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 1071 | .ok_or(ParseError::InsufficientData)?; |
| 1072 | Ok((result, remaining)) |
| 1073 | } |
| 1074 | } |
| 1075 | impl Serialize for ListInputDevicesReply { |
| 1076 | type Bytes = Vec<u8>; |
| 1077 | fn serialize(&self) -> Vec<u8> { |
| 1078 | let mut result = Vec::new(); |
| 1079 | self.serialize_into(&mut result); |
| 1080 | result |
| 1081 | } |
| 1082 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 1083 | bytes.reserve(32); |
| 1084 | let response_type_bytes = &[1]; |
| 1085 | bytes.push(response_type_bytes[0]); |
| 1086 | self.xi_reply_type.serialize_into(bytes); |
| 1087 | self.sequence.serialize_into(bytes); |
| 1088 | self.length.serialize_into(bytes); |
| 1089 | let devices_len = u8::try_from(self.devices.len()).expect("`devices` has too many elements" ); |
| 1090 | devices_len.serialize_into(bytes); |
| 1091 | bytes.extend_from_slice(&[0; 23]); |
| 1092 | self.devices.serialize_into(bytes); |
| 1093 | assert_eq!(self.infos.len(), usize::try_from(self.devices.iter().fold(0u32, |acc, x| acc.checked_add(u32::from(x.num_class_info)).unwrap())).unwrap(), "`infos` has an incorrect length" ); |
| 1094 | self.infos.serialize_into(bytes); |
| 1095 | assert_eq!(self.names.len(), usize::try_from(devices_len).unwrap(), "`names` has an incorrect length" ); |
| 1096 | self.names.serialize_into(bytes); |
| 1097 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 1098 | } |
| 1099 | } |
| 1100 | impl ListInputDevicesReply { |
| 1101 | /// Get the value of the `devices_len` field. |
| 1102 | /// |
| 1103 | /// The `devices_len` field is used as the length field of the `devices` field. |
| 1104 | /// This function computes the field's value again based on the length of the list. |
| 1105 | /// |
| 1106 | /// # Panics |
| 1107 | /// |
| 1108 | /// Panics if the value cannot be represented in the target type. This |
| 1109 | /// cannot happen with values of the struct received from the X11 server. |
| 1110 | pub fn devices_len(&self) -> u8 { |
| 1111 | self.devices.len() |
| 1112 | .try_into().unwrap() |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | pub type EventTypeBase = u8; |
| 1117 | |
| 1118 | #[derive (Clone, Copy, Default)] |
| 1119 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1120 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1121 | pub struct InputClassInfo { |
| 1122 | pub class_id: InputClass, |
| 1123 | pub event_type_base: EventTypeBase, |
| 1124 | } |
| 1125 | impl_debug_if_no_extra_traits!(InputClassInfo, "InputClassInfo" ); |
| 1126 | impl TryParse for InputClassInfo { |
| 1127 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 1128 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 1129 | let (event_type_base: u8, remaining: &[u8]) = EventTypeBase::try_parse(remaining)?; |
| 1130 | let class_id: InputClass = class_id.into(); |
| 1131 | let result: InputClassInfo = InputClassInfo { class_id, event_type_base }; |
| 1132 | Ok((result, remaining)) |
| 1133 | } |
| 1134 | } |
| 1135 | impl Serialize for InputClassInfo { |
| 1136 | type Bytes = [u8; 2]; |
| 1137 | fn serialize(&self) -> [u8; 2] { |
| 1138 | let class_id_bytes: [u8; 1] = u8::from(self.class_id).serialize(); |
| 1139 | let event_type_base_bytes: [u8; 1] = self.event_type_base.serialize(); |
| 1140 | [ |
| 1141 | class_id_bytes[0], |
| 1142 | event_type_base_bytes[0], |
| 1143 | ] |
| 1144 | } |
| 1145 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 1146 | bytes.reserve(additional:2); |
| 1147 | u8::from(self.class_id).serialize_into(bytes); |
| 1148 | self.event_type_base.serialize_into(bytes); |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | /// Opcode for the OpenDevice request |
| 1153 | pub const OPEN_DEVICE_REQUEST: u8 = 3; |
| 1154 | #[derive (Clone, Copy, Default)] |
| 1155 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1156 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1157 | pub struct OpenDeviceRequest { |
| 1158 | pub device_id: u8, |
| 1159 | } |
| 1160 | impl_debug_if_no_extra_traits!(OpenDeviceRequest, "OpenDeviceRequest" ); |
| 1161 | impl OpenDeviceRequest { |
| 1162 | /// Serialize this request into bytes for the provided connection |
| 1163 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 1164 | let length_so_far = 0; |
| 1165 | let device_id_bytes = self.device_id.serialize(); |
| 1166 | let mut request0 = vec![ |
| 1167 | major_opcode, |
| 1168 | OPEN_DEVICE_REQUEST, |
| 1169 | 0, |
| 1170 | 0, |
| 1171 | device_id_bytes[0], |
| 1172 | 0, |
| 1173 | 0, |
| 1174 | 0, |
| 1175 | ]; |
| 1176 | let length_so_far = length_so_far + request0.len(); |
| 1177 | assert_eq!(length_so_far % 4, 0); |
| 1178 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 1179 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 1180 | ([request0.into()], vec![]) |
| 1181 | } |
| 1182 | /// Parse this request given its header, its body, and any fds that go along with it |
| 1183 | #[cfg (feature = "request-parsing" )] |
| 1184 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 1185 | if header.minor_opcode != OPEN_DEVICE_REQUEST { |
| 1186 | return Err(ParseError::InvalidValue); |
| 1187 | } |
| 1188 | let (device_id, remaining) = u8::try_parse(value)?; |
| 1189 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 1190 | let _ = remaining; |
| 1191 | Ok(OpenDeviceRequest { |
| 1192 | device_id, |
| 1193 | }) |
| 1194 | } |
| 1195 | } |
| 1196 | impl Request for OpenDeviceRequest { |
| 1197 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 1198 | |
| 1199 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 1200 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 1201 | // Flatten the buffers into a single vector |
| 1202 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 1203 | (buf, fds) |
| 1204 | } |
| 1205 | } |
| 1206 | impl crate::x11_utils::ReplyRequest for OpenDeviceRequest { |
| 1207 | type Reply = OpenDeviceReply; |
| 1208 | } |
| 1209 | |
| 1210 | #[derive (Clone, Default)] |
| 1211 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1212 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1213 | pub struct OpenDeviceReply { |
| 1214 | pub xi_reply_type: u8, |
| 1215 | pub sequence: u16, |
| 1216 | pub length: u32, |
| 1217 | pub class_info: Vec<InputClassInfo>, |
| 1218 | } |
| 1219 | impl_debug_if_no_extra_traits!(OpenDeviceReply, "OpenDeviceReply" ); |
| 1220 | impl TryParse for OpenDeviceReply { |
| 1221 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 1222 | let remaining = initial_value; |
| 1223 | let value = remaining; |
| 1224 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 1225 | let (xi_reply_type, remaining) = u8::try_parse(remaining)?; |
| 1226 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 1227 | let (length, remaining) = u32::try_parse(remaining)?; |
| 1228 | let (num_classes, remaining) = u8::try_parse(remaining)?; |
| 1229 | let remaining = remaining.get(23..).ok_or(ParseError::InsufficientData)?; |
| 1230 | let (class_info, remaining) = crate::x11_utils::parse_list::<InputClassInfo>(remaining, num_classes.try_to_usize()?)?; |
| 1231 | // Align offset to multiple of 4 |
| 1232 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 1233 | let misalignment = (4 - (offset % 4)) % 4; |
| 1234 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
| 1235 | if response_type != 1 { |
| 1236 | return Err(ParseError::InvalidValue); |
| 1237 | } |
| 1238 | let result = OpenDeviceReply { xi_reply_type, sequence, length, class_info }; |
| 1239 | let _ = remaining; |
| 1240 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 1241 | .ok_or(ParseError::InsufficientData)?; |
| 1242 | Ok((result, remaining)) |
| 1243 | } |
| 1244 | } |
| 1245 | impl Serialize for OpenDeviceReply { |
| 1246 | type Bytes = Vec<u8>; |
| 1247 | fn serialize(&self) -> Vec<u8> { |
| 1248 | let mut result: Vec = Vec::new(); |
| 1249 | self.serialize_into(&mut result); |
| 1250 | result |
| 1251 | } |
| 1252 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 1253 | bytes.reserve(additional:32); |
| 1254 | let response_type_bytes: &[u8; 1] = &[1]; |
| 1255 | bytes.push(response_type_bytes[0]); |
| 1256 | self.xi_reply_type.serialize_into(bytes); |
| 1257 | self.sequence.serialize_into(bytes); |
| 1258 | self.length.serialize_into(bytes); |
| 1259 | let num_classes: u8 = u8::try_from(self.class_info.len()).expect(msg:"`class_info` has too many elements" ); |
| 1260 | num_classes.serialize_into(bytes); |
| 1261 | bytes.extend_from_slice(&[0; 23]); |
| 1262 | self.class_info.serialize_into(bytes); |
| 1263 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 1264 | } |
| 1265 | } |
| 1266 | impl OpenDeviceReply { |
| 1267 | /// Get the value of the `num_classes` field. |
| 1268 | /// |
| 1269 | /// The `num_classes` field is used as the length field of the `class_info` field. |
| 1270 | /// This function computes the field's value again based on the length of the list. |
| 1271 | /// |
| 1272 | /// # Panics |
| 1273 | /// |
| 1274 | /// Panics if the value cannot be represented in the target type. This |
| 1275 | /// cannot happen with values of the struct received from the X11 server. |
| 1276 | pub fn num_classes(&self) -> u8 { |
| 1277 | self.class_info.len() |
| 1278 | .try_into().unwrap() |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | /// Opcode for the CloseDevice request |
| 1283 | pub const CLOSE_DEVICE_REQUEST: u8 = 4; |
| 1284 | #[derive (Clone, Copy, Default)] |
| 1285 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1286 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1287 | pub struct CloseDeviceRequest { |
| 1288 | pub device_id: u8, |
| 1289 | } |
| 1290 | impl_debug_if_no_extra_traits!(CloseDeviceRequest, "CloseDeviceRequest" ); |
| 1291 | impl CloseDeviceRequest { |
| 1292 | /// Serialize this request into bytes for the provided connection |
| 1293 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 1294 | let length_so_far = 0; |
| 1295 | let device_id_bytes = self.device_id.serialize(); |
| 1296 | let mut request0 = vec![ |
| 1297 | major_opcode, |
| 1298 | CLOSE_DEVICE_REQUEST, |
| 1299 | 0, |
| 1300 | 0, |
| 1301 | device_id_bytes[0], |
| 1302 | 0, |
| 1303 | 0, |
| 1304 | 0, |
| 1305 | ]; |
| 1306 | let length_so_far = length_so_far + request0.len(); |
| 1307 | assert_eq!(length_so_far % 4, 0); |
| 1308 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 1309 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 1310 | ([request0.into()], vec![]) |
| 1311 | } |
| 1312 | /// Parse this request given its header, its body, and any fds that go along with it |
| 1313 | #[cfg (feature = "request-parsing" )] |
| 1314 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 1315 | if header.minor_opcode != CLOSE_DEVICE_REQUEST { |
| 1316 | return Err(ParseError::InvalidValue); |
| 1317 | } |
| 1318 | let (device_id, remaining) = u8::try_parse(value)?; |
| 1319 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 1320 | let _ = remaining; |
| 1321 | Ok(CloseDeviceRequest { |
| 1322 | device_id, |
| 1323 | }) |
| 1324 | } |
| 1325 | } |
| 1326 | impl Request for CloseDeviceRequest { |
| 1327 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 1328 | |
| 1329 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 1330 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 1331 | // Flatten the buffers into a single vector |
| 1332 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 1333 | (buf, fds) |
| 1334 | } |
| 1335 | } |
| 1336 | impl crate::x11_utils::VoidRequest for CloseDeviceRequest { |
| 1337 | } |
| 1338 | |
| 1339 | /// Opcode for the SetDeviceMode request |
| 1340 | pub const SET_DEVICE_MODE_REQUEST: u8 = 5; |
| 1341 | #[derive (Clone, Copy, Default)] |
| 1342 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1343 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1344 | pub struct SetDeviceModeRequest { |
| 1345 | pub device_id: u8, |
| 1346 | pub mode: ValuatorMode, |
| 1347 | } |
| 1348 | impl_debug_if_no_extra_traits!(SetDeviceModeRequest, "SetDeviceModeRequest" ); |
| 1349 | impl SetDeviceModeRequest { |
| 1350 | /// Serialize this request into bytes for the provided connection |
| 1351 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 1352 | let length_so_far = 0; |
| 1353 | let device_id_bytes = self.device_id.serialize(); |
| 1354 | let mode_bytes = u8::from(self.mode).serialize(); |
| 1355 | let mut request0 = vec![ |
| 1356 | major_opcode, |
| 1357 | SET_DEVICE_MODE_REQUEST, |
| 1358 | 0, |
| 1359 | 0, |
| 1360 | device_id_bytes[0], |
| 1361 | mode_bytes[0], |
| 1362 | 0, |
| 1363 | 0, |
| 1364 | ]; |
| 1365 | let length_so_far = length_so_far + request0.len(); |
| 1366 | assert_eq!(length_so_far % 4, 0); |
| 1367 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 1368 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 1369 | ([request0.into()], vec![]) |
| 1370 | } |
| 1371 | /// Parse this request given its header, its body, and any fds that go along with it |
| 1372 | #[cfg (feature = "request-parsing" )] |
| 1373 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 1374 | if header.minor_opcode != SET_DEVICE_MODE_REQUEST { |
| 1375 | return Err(ParseError::InvalidValue); |
| 1376 | } |
| 1377 | let (device_id, remaining) = u8::try_parse(value)?; |
| 1378 | let (mode, remaining) = u8::try_parse(remaining)?; |
| 1379 | let mode = mode.into(); |
| 1380 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 1381 | let _ = remaining; |
| 1382 | Ok(SetDeviceModeRequest { |
| 1383 | device_id, |
| 1384 | mode, |
| 1385 | }) |
| 1386 | } |
| 1387 | } |
| 1388 | impl Request for SetDeviceModeRequest { |
| 1389 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 1390 | |
| 1391 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 1392 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 1393 | // Flatten the buffers into a single vector |
| 1394 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 1395 | (buf, fds) |
| 1396 | } |
| 1397 | } |
| 1398 | impl crate::x11_utils::ReplyRequest for SetDeviceModeRequest { |
| 1399 | type Reply = SetDeviceModeReply; |
| 1400 | } |
| 1401 | |
| 1402 | #[derive (Clone, Copy, Default)] |
| 1403 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1404 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1405 | pub struct SetDeviceModeReply { |
| 1406 | pub xi_reply_type: u8, |
| 1407 | pub sequence: u16, |
| 1408 | pub length: u32, |
| 1409 | pub status: xproto::GrabStatus, |
| 1410 | } |
| 1411 | impl_debug_if_no_extra_traits!(SetDeviceModeReply, "SetDeviceModeReply" ); |
| 1412 | impl TryParse for SetDeviceModeReply { |
| 1413 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 1414 | let remaining: &[u8] = initial_value; |
| 1415 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 1416 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 1417 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 1418 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 1419 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 1420 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 1421 | if response_type != 1 { |
| 1422 | return Err(ParseError::InvalidValue); |
| 1423 | } |
| 1424 | let status: GrabStatus = status.into(); |
| 1425 | let result: SetDeviceModeReply = SetDeviceModeReply { xi_reply_type, sequence, length, status }; |
| 1426 | let _ = remaining; |
| 1427 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 1428 | .ok_or(err:ParseError::InsufficientData)?; |
| 1429 | Ok((result, remaining)) |
| 1430 | } |
| 1431 | } |
| 1432 | impl Serialize for SetDeviceModeReply { |
| 1433 | type Bytes = [u8; 32]; |
| 1434 | fn serialize(&self) -> [u8; 32] { |
| 1435 | let response_type_bytes = &[1]; |
| 1436 | let xi_reply_type_bytes = self.xi_reply_type.serialize(); |
| 1437 | let sequence_bytes = self.sequence.serialize(); |
| 1438 | let length_bytes = self.length.serialize(); |
| 1439 | let status_bytes = u8::from(self.status).serialize(); |
| 1440 | [ |
| 1441 | response_type_bytes[0], |
| 1442 | xi_reply_type_bytes[0], |
| 1443 | sequence_bytes[0], |
| 1444 | sequence_bytes[1], |
| 1445 | length_bytes[0], |
| 1446 | length_bytes[1], |
| 1447 | length_bytes[2], |
| 1448 | length_bytes[3], |
| 1449 | status_bytes[0], |
| 1450 | 0, |
| 1451 | 0, |
| 1452 | 0, |
| 1453 | 0, |
| 1454 | 0, |
| 1455 | 0, |
| 1456 | 0, |
| 1457 | 0, |
| 1458 | 0, |
| 1459 | 0, |
| 1460 | 0, |
| 1461 | 0, |
| 1462 | 0, |
| 1463 | 0, |
| 1464 | 0, |
| 1465 | 0, |
| 1466 | 0, |
| 1467 | 0, |
| 1468 | 0, |
| 1469 | 0, |
| 1470 | 0, |
| 1471 | 0, |
| 1472 | 0, |
| 1473 | ] |
| 1474 | } |
| 1475 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 1476 | bytes.reserve(32); |
| 1477 | let response_type_bytes = &[1]; |
| 1478 | bytes.push(response_type_bytes[0]); |
| 1479 | self.xi_reply_type.serialize_into(bytes); |
| 1480 | self.sequence.serialize_into(bytes); |
| 1481 | self.length.serialize_into(bytes); |
| 1482 | u8::from(self.status).serialize_into(bytes); |
| 1483 | bytes.extend_from_slice(&[0; 23]); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | /// Opcode for the SelectExtensionEvent request |
| 1488 | pub const SELECT_EXTENSION_EVENT_REQUEST: u8 = 6; |
| 1489 | #[derive (Clone, Default)] |
| 1490 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1491 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1492 | pub struct SelectExtensionEventRequest<'input> { |
| 1493 | pub window: xproto::Window, |
| 1494 | pub classes: Cow<'input, [EventClass]>, |
| 1495 | } |
| 1496 | impl_debug_if_no_extra_traits!(SelectExtensionEventRequest<'_>, "SelectExtensionEventRequest" ); |
| 1497 | impl<'input> SelectExtensionEventRequest<'input> { |
| 1498 | /// Serialize this request into bytes for the provided connection |
| 1499 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 1500 | let length_so_far = 0; |
| 1501 | let window_bytes = self.window.serialize(); |
| 1502 | let num_classes = u16::try_from(self.classes.len()).expect("`classes` has too many elements" ); |
| 1503 | let num_classes_bytes = num_classes.serialize(); |
| 1504 | let mut request0 = vec![ |
| 1505 | major_opcode, |
| 1506 | SELECT_EXTENSION_EVENT_REQUEST, |
| 1507 | 0, |
| 1508 | 0, |
| 1509 | window_bytes[0], |
| 1510 | window_bytes[1], |
| 1511 | window_bytes[2], |
| 1512 | window_bytes[3], |
| 1513 | num_classes_bytes[0], |
| 1514 | num_classes_bytes[1], |
| 1515 | 0, |
| 1516 | 0, |
| 1517 | ]; |
| 1518 | let length_so_far = length_so_far + request0.len(); |
| 1519 | let classes_bytes = self.classes.serialize(); |
| 1520 | let length_so_far = length_so_far + classes_bytes.len(); |
| 1521 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 1522 | let length_so_far = length_so_far + padding0.len(); |
| 1523 | assert_eq!(length_so_far % 4, 0); |
| 1524 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 1525 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 1526 | ([request0.into(), classes_bytes.into(), padding0.into()], vec![]) |
| 1527 | } |
| 1528 | /// Parse this request given its header, its body, and any fds that go along with it |
| 1529 | #[cfg (feature = "request-parsing" )] |
| 1530 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 1531 | if header.minor_opcode != SELECT_EXTENSION_EVENT_REQUEST { |
| 1532 | return Err(ParseError::InvalidValue); |
| 1533 | } |
| 1534 | let (window, remaining) = xproto::Window::try_parse(value)?; |
| 1535 | let (num_classes, remaining) = u16::try_parse(remaining)?; |
| 1536 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 1537 | let (classes, remaining) = crate::x11_utils::parse_list::<EventClass>(remaining, num_classes.try_to_usize()?)?; |
| 1538 | let _ = remaining; |
| 1539 | Ok(SelectExtensionEventRequest { |
| 1540 | window, |
| 1541 | classes: Cow::Owned(classes), |
| 1542 | }) |
| 1543 | } |
| 1544 | /// Clone all borrowed data in this SelectExtensionEventRequest. |
| 1545 | pub fn into_owned(self) -> SelectExtensionEventRequest<'static> { |
| 1546 | SelectExtensionEventRequest { |
| 1547 | window: self.window, |
| 1548 | classes: Cow::Owned(self.classes.into_owned()), |
| 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | impl<'input> Request for SelectExtensionEventRequest<'input> { |
| 1553 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 1554 | |
| 1555 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 1556 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 1557 | // Flatten the buffers into a single vector |
| 1558 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 1559 | (buf, fds) |
| 1560 | } |
| 1561 | } |
| 1562 | impl<'input> crate::x11_utils::VoidRequest for SelectExtensionEventRequest<'input> { |
| 1563 | } |
| 1564 | |
| 1565 | /// Opcode for the GetSelectedExtensionEvents request |
| 1566 | pub const GET_SELECTED_EXTENSION_EVENTS_REQUEST: u8 = 7; |
| 1567 | #[derive (Clone, Copy, Default)] |
| 1568 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1569 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1570 | pub struct GetSelectedExtensionEventsRequest { |
| 1571 | pub window: xproto::Window, |
| 1572 | } |
| 1573 | impl_debug_if_no_extra_traits!(GetSelectedExtensionEventsRequest, "GetSelectedExtensionEventsRequest" ); |
| 1574 | impl GetSelectedExtensionEventsRequest { |
| 1575 | /// Serialize this request into bytes for the provided connection |
| 1576 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 1577 | let length_so_far = 0; |
| 1578 | let window_bytes = self.window.serialize(); |
| 1579 | let mut request0 = vec![ |
| 1580 | major_opcode, |
| 1581 | GET_SELECTED_EXTENSION_EVENTS_REQUEST, |
| 1582 | 0, |
| 1583 | 0, |
| 1584 | window_bytes[0], |
| 1585 | window_bytes[1], |
| 1586 | window_bytes[2], |
| 1587 | window_bytes[3], |
| 1588 | ]; |
| 1589 | let length_so_far = length_so_far + request0.len(); |
| 1590 | assert_eq!(length_so_far % 4, 0); |
| 1591 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 1592 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 1593 | ([request0.into()], vec![]) |
| 1594 | } |
| 1595 | /// Parse this request given its header, its body, and any fds that go along with it |
| 1596 | #[cfg (feature = "request-parsing" )] |
| 1597 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 1598 | if header.minor_opcode != GET_SELECTED_EXTENSION_EVENTS_REQUEST { |
| 1599 | return Err(ParseError::InvalidValue); |
| 1600 | } |
| 1601 | let (window, remaining) = xproto::Window::try_parse(value)?; |
| 1602 | let _ = remaining; |
| 1603 | Ok(GetSelectedExtensionEventsRequest { |
| 1604 | window, |
| 1605 | }) |
| 1606 | } |
| 1607 | } |
| 1608 | impl Request for GetSelectedExtensionEventsRequest { |
| 1609 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 1610 | |
| 1611 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 1612 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 1613 | // Flatten the buffers into a single vector |
| 1614 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 1615 | (buf, fds) |
| 1616 | } |
| 1617 | } |
| 1618 | impl crate::x11_utils::ReplyRequest for GetSelectedExtensionEventsRequest { |
| 1619 | type Reply = GetSelectedExtensionEventsReply; |
| 1620 | } |
| 1621 | |
| 1622 | #[derive (Clone, Default)] |
| 1623 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1624 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1625 | pub struct GetSelectedExtensionEventsReply { |
| 1626 | pub xi_reply_type: u8, |
| 1627 | pub sequence: u16, |
| 1628 | pub length: u32, |
| 1629 | pub this_classes: Vec<EventClass>, |
| 1630 | pub all_classes: Vec<EventClass>, |
| 1631 | } |
| 1632 | impl_debug_if_no_extra_traits!(GetSelectedExtensionEventsReply, "GetSelectedExtensionEventsReply" ); |
| 1633 | impl TryParse for GetSelectedExtensionEventsReply { |
| 1634 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 1635 | let remaining: &[u8] = initial_value; |
| 1636 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 1637 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 1638 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 1639 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 1640 | let (num_this_classes: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 1641 | let (num_all_classes: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 1642 | let remaining: &[u8] = remaining.get(20..).ok_or(err:ParseError::InsufficientData)?; |
| 1643 | let (this_classes: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<EventClass>(data:remaining, list_length:num_this_classes.try_to_usize()?)?; |
| 1644 | let (all_classes: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<EventClass>(data:remaining, list_length:num_all_classes.try_to_usize()?)?; |
| 1645 | if response_type != 1 { |
| 1646 | return Err(ParseError::InvalidValue); |
| 1647 | } |
| 1648 | let result: GetSelectedExtensionEventsReply = GetSelectedExtensionEventsReply { xi_reply_type, sequence, length, this_classes, all_classes }; |
| 1649 | let _ = remaining; |
| 1650 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 1651 | .ok_or(err:ParseError::InsufficientData)?; |
| 1652 | Ok((result, remaining)) |
| 1653 | } |
| 1654 | } |
| 1655 | impl Serialize for GetSelectedExtensionEventsReply { |
| 1656 | type Bytes = Vec<u8>; |
| 1657 | fn serialize(&self) -> Vec<u8> { |
| 1658 | let mut result: Vec = Vec::new(); |
| 1659 | self.serialize_into(&mut result); |
| 1660 | result |
| 1661 | } |
| 1662 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 1663 | bytes.reserve(additional:32); |
| 1664 | let response_type_bytes: &[u8; 1] = &[1]; |
| 1665 | bytes.push(response_type_bytes[0]); |
| 1666 | self.xi_reply_type.serialize_into(bytes); |
| 1667 | self.sequence.serialize_into(bytes); |
| 1668 | self.length.serialize_into(bytes); |
| 1669 | let num_this_classes: u16 = u16::try_from(self.this_classes.len()).expect(msg:"`this_classes` has too many elements" ); |
| 1670 | num_this_classes.serialize_into(bytes); |
| 1671 | let num_all_classes: u16 = u16::try_from(self.all_classes.len()).expect(msg:"`all_classes` has too many elements" ); |
| 1672 | num_all_classes.serialize_into(bytes); |
| 1673 | bytes.extend_from_slice(&[0; 20]); |
| 1674 | self.this_classes.serialize_into(bytes); |
| 1675 | self.all_classes.serialize_into(bytes); |
| 1676 | } |
| 1677 | } |
| 1678 | impl GetSelectedExtensionEventsReply { |
| 1679 | /// Get the value of the `num_this_classes` field. |
| 1680 | /// |
| 1681 | /// The `num_this_classes` field is used as the length field of the `this_classes` field. |
| 1682 | /// This function computes the field's value again based on the length of the list. |
| 1683 | /// |
| 1684 | /// # Panics |
| 1685 | /// |
| 1686 | /// Panics if the value cannot be represented in the target type. This |
| 1687 | /// cannot happen with values of the struct received from the X11 server. |
| 1688 | pub fn num_this_classes(&self) -> u16 { |
| 1689 | self.this_classes.len() |
| 1690 | .try_into().unwrap() |
| 1691 | } |
| 1692 | /// Get the value of the `num_all_classes` field. |
| 1693 | /// |
| 1694 | /// The `num_all_classes` field is used as the length field of the `all_classes` field. |
| 1695 | /// This function computes the field's value again based on the length of the list. |
| 1696 | /// |
| 1697 | /// # Panics |
| 1698 | /// |
| 1699 | /// Panics if the value cannot be represented in the target type. This |
| 1700 | /// cannot happen with values of the struct received from the X11 server. |
| 1701 | pub fn num_all_classes(&self) -> u16 { |
| 1702 | self.all_classes.len() |
| 1703 | .try_into().unwrap() |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 1708 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1709 | pub struct PropagateMode(u8); |
| 1710 | impl PropagateMode { |
| 1711 | pub const ADD_TO_LIST: Self = Self(0); |
| 1712 | pub const DELETE_FROM_LIST: Self = Self(1); |
| 1713 | } |
| 1714 | impl From<PropagateMode> for u8 { |
| 1715 | #[inline ] |
| 1716 | fn from(input: PropagateMode) -> Self { |
| 1717 | input.0 |
| 1718 | } |
| 1719 | } |
| 1720 | impl From<PropagateMode> for Option<u8> { |
| 1721 | #[inline ] |
| 1722 | fn from(input: PropagateMode) -> Self { |
| 1723 | Some(input.0) |
| 1724 | } |
| 1725 | } |
| 1726 | impl From<PropagateMode> for u16 { |
| 1727 | #[inline ] |
| 1728 | fn from(input: PropagateMode) -> Self { |
| 1729 | u16::from(input.0) |
| 1730 | } |
| 1731 | } |
| 1732 | impl From<PropagateMode> for Option<u16> { |
| 1733 | #[inline ] |
| 1734 | fn from(input: PropagateMode) -> Self { |
| 1735 | Some(u16::from(input.0)) |
| 1736 | } |
| 1737 | } |
| 1738 | impl From<PropagateMode> for u32 { |
| 1739 | #[inline ] |
| 1740 | fn from(input: PropagateMode) -> Self { |
| 1741 | u32::from(input.0) |
| 1742 | } |
| 1743 | } |
| 1744 | impl From<PropagateMode> for Option<u32> { |
| 1745 | #[inline ] |
| 1746 | fn from(input: PropagateMode) -> Self { |
| 1747 | Some(u32::from(input.0)) |
| 1748 | } |
| 1749 | } |
| 1750 | impl From<u8> for PropagateMode { |
| 1751 | #[inline ] |
| 1752 | fn from(value: u8) -> Self { |
| 1753 | Self(value) |
| 1754 | } |
| 1755 | } |
| 1756 | impl core::fmt::Debug for PropagateMode { |
| 1757 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 1758 | let variants: [(u32, &'static str, &'static …); 2] = [ |
| 1759 | (Self::ADD_TO_LIST.0.into(), "ADD_TO_LIST" , "AddToList" ), |
| 1760 | (Self::DELETE_FROM_LIST.0.into(), "DELETE_FROM_LIST" , "DeleteFromList" ), |
| 1761 | ]; |
| 1762 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | /// Opcode for the ChangeDeviceDontPropagateList request |
| 1767 | pub const CHANGE_DEVICE_DONT_PROPAGATE_LIST_REQUEST: u8 = 8; |
| 1768 | #[derive (Clone, Default)] |
| 1769 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1770 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1771 | pub struct ChangeDeviceDontPropagateListRequest<'input> { |
| 1772 | pub window: xproto::Window, |
| 1773 | pub mode: PropagateMode, |
| 1774 | pub classes: Cow<'input, [EventClass]>, |
| 1775 | } |
| 1776 | impl_debug_if_no_extra_traits!(ChangeDeviceDontPropagateListRequest<'_>, "ChangeDeviceDontPropagateListRequest" ); |
| 1777 | impl<'input> ChangeDeviceDontPropagateListRequest<'input> { |
| 1778 | /// Serialize this request into bytes for the provided connection |
| 1779 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 1780 | let length_so_far = 0; |
| 1781 | let window_bytes = self.window.serialize(); |
| 1782 | let num_classes = u16::try_from(self.classes.len()).expect("`classes` has too many elements" ); |
| 1783 | let num_classes_bytes = num_classes.serialize(); |
| 1784 | let mode_bytes = u8::from(self.mode).serialize(); |
| 1785 | let mut request0 = vec![ |
| 1786 | major_opcode, |
| 1787 | CHANGE_DEVICE_DONT_PROPAGATE_LIST_REQUEST, |
| 1788 | 0, |
| 1789 | 0, |
| 1790 | window_bytes[0], |
| 1791 | window_bytes[1], |
| 1792 | window_bytes[2], |
| 1793 | window_bytes[3], |
| 1794 | num_classes_bytes[0], |
| 1795 | num_classes_bytes[1], |
| 1796 | mode_bytes[0], |
| 1797 | 0, |
| 1798 | ]; |
| 1799 | let length_so_far = length_so_far + request0.len(); |
| 1800 | let classes_bytes = self.classes.serialize(); |
| 1801 | let length_so_far = length_so_far + classes_bytes.len(); |
| 1802 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 1803 | let length_so_far = length_so_far + padding0.len(); |
| 1804 | assert_eq!(length_so_far % 4, 0); |
| 1805 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 1806 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 1807 | ([request0.into(), classes_bytes.into(), padding0.into()], vec![]) |
| 1808 | } |
| 1809 | /// Parse this request given its header, its body, and any fds that go along with it |
| 1810 | #[cfg (feature = "request-parsing" )] |
| 1811 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 1812 | if header.minor_opcode != CHANGE_DEVICE_DONT_PROPAGATE_LIST_REQUEST { |
| 1813 | return Err(ParseError::InvalidValue); |
| 1814 | } |
| 1815 | let (window, remaining) = xproto::Window::try_parse(value)?; |
| 1816 | let (num_classes, remaining) = u16::try_parse(remaining)?; |
| 1817 | let (mode, remaining) = u8::try_parse(remaining)?; |
| 1818 | let mode = mode.into(); |
| 1819 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
| 1820 | let (classes, remaining) = crate::x11_utils::parse_list::<EventClass>(remaining, num_classes.try_to_usize()?)?; |
| 1821 | let _ = remaining; |
| 1822 | Ok(ChangeDeviceDontPropagateListRequest { |
| 1823 | window, |
| 1824 | mode, |
| 1825 | classes: Cow::Owned(classes), |
| 1826 | }) |
| 1827 | } |
| 1828 | /// Clone all borrowed data in this ChangeDeviceDontPropagateListRequest. |
| 1829 | pub fn into_owned(self) -> ChangeDeviceDontPropagateListRequest<'static> { |
| 1830 | ChangeDeviceDontPropagateListRequest { |
| 1831 | window: self.window, |
| 1832 | mode: self.mode, |
| 1833 | classes: Cow::Owned(self.classes.into_owned()), |
| 1834 | } |
| 1835 | } |
| 1836 | } |
| 1837 | impl<'input> Request for ChangeDeviceDontPropagateListRequest<'input> { |
| 1838 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 1839 | |
| 1840 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 1841 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 1842 | // Flatten the buffers into a single vector |
| 1843 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 1844 | (buf, fds) |
| 1845 | } |
| 1846 | } |
| 1847 | impl<'input> crate::x11_utils::VoidRequest for ChangeDeviceDontPropagateListRequest<'input> { |
| 1848 | } |
| 1849 | |
| 1850 | /// Opcode for the GetDeviceDontPropagateList request |
| 1851 | pub const GET_DEVICE_DONT_PROPAGATE_LIST_REQUEST: u8 = 9; |
| 1852 | #[derive (Clone, Copy, Default)] |
| 1853 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1854 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1855 | pub struct GetDeviceDontPropagateListRequest { |
| 1856 | pub window: xproto::Window, |
| 1857 | } |
| 1858 | impl_debug_if_no_extra_traits!(GetDeviceDontPropagateListRequest, "GetDeviceDontPropagateListRequest" ); |
| 1859 | impl GetDeviceDontPropagateListRequest { |
| 1860 | /// Serialize this request into bytes for the provided connection |
| 1861 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 1862 | let length_so_far = 0; |
| 1863 | let window_bytes = self.window.serialize(); |
| 1864 | let mut request0 = vec![ |
| 1865 | major_opcode, |
| 1866 | GET_DEVICE_DONT_PROPAGATE_LIST_REQUEST, |
| 1867 | 0, |
| 1868 | 0, |
| 1869 | window_bytes[0], |
| 1870 | window_bytes[1], |
| 1871 | window_bytes[2], |
| 1872 | window_bytes[3], |
| 1873 | ]; |
| 1874 | let length_so_far = length_so_far + request0.len(); |
| 1875 | assert_eq!(length_so_far % 4, 0); |
| 1876 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 1877 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 1878 | ([request0.into()], vec![]) |
| 1879 | } |
| 1880 | /// Parse this request given its header, its body, and any fds that go along with it |
| 1881 | #[cfg (feature = "request-parsing" )] |
| 1882 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 1883 | if header.minor_opcode != GET_DEVICE_DONT_PROPAGATE_LIST_REQUEST { |
| 1884 | return Err(ParseError::InvalidValue); |
| 1885 | } |
| 1886 | let (window, remaining) = xproto::Window::try_parse(value)?; |
| 1887 | let _ = remaining; |
| 1888 | Ok(GetDeviceDontPropagateListRequest { |
| 1889 | window, |
| 1890 | }) |
| 1891 | } |
| 1892 | } |
| 1893 | impl Request for GetDeviceDontPropagateListRequest { |
| 1894 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 1895 | |
| 1896 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 1897 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 1898 | // Flatten the buffers into a single vector |
| 1899 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 1900 | (buf, fds) |
| 1901 | } |
| 1902 | } |
| 1903 | impl crate::x11_utils::ReplyRequest for GetDeviceDontPropagateListRequest { |
| 1904 | type Reply = GetDeviceDontPropagateListReply; |
| 1905 | } |
| 1906 | |
| 1907 | #[derive (Clone, Default)] |
| 1908 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1909 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1910 | pub struct GetDeviceDontPropagateListReply { |
| 1911 | pub xi_reply_type: u8, |
| 1912 | pub sequence: u16, |
| 1913 | pub length: u32, |
| 1914 | pub classes: Vec<EventClass>, |
| 1915 | } |
| 1916 | impl_debug_if_no_extra_traits!(GetDeviceDontPropagateListReply, "GetDeviceDontPropagateListReply" ); |
| 1917 | impl TryParse for GetDeviceDontPropagateListReply { |
| 1918 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 1919 | let remaining: &[u8] = initial_value; |
| 1920 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 1921 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 1922 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 1923 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 1924 | let (num_classes: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 1925 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
| 1926 | let (classes: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<EventClass>(data:remaining, list_length:num_classes.try_to_usize()?)?; |
| 1927 | if response_type != 1 { |
| 1928 | return Err(ParseError::InvalidValue); |
| 1929 | } |
| 1930 | let result: GetDeviceDontPropagateListReply = GetDeviceDontPropagateListReply { xi_reply_type, sequence, length, classes }; |
| 1931 | let _ = remaining; |
| 1932 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 1933 | .ok_or(err:ParseError::InsufficientData)?; |
| 1934 | Ok((result, remaining)) |
| 1935 | } |
| 1936 | } |
| 1937 | impl Serialize for GetDeviceDontPropagateListReply { |
| 1938 | type Bytes = Vec<u8>; |
| 1939 | fn serialize(&self) -> Vec<u8> { |
| 1940 | let mut result: Vec = Vec::new(); |
| 1941 | self.serialize_into(&mut result); |
| 1942 | result |
| 1943 | } |
| 1944 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 1945 | bytes.reserve(additional:32); |
| 1946 | let response_type_bytes: &[u8; 1] = &[1]; |
| 1947 | bytes.push(response_type_bytes[0]); |
| 1948 | self.xi_reply_type.serialize_into(bytes); |
| 1949 | self.sequence.serialize_into(bytes); |
| 1950 | self.length.serialize_into(bytes); |
| 1951 | let num_classes: u16 = u16::try_from(self.classes.len()).expect(msg:"`classes` has too many elements" ); |
| 1952 | num_classes.serialize_into(bytes); |
| 1953 | bytes.extend_from_slice(&[0; 22]); |
| 1954 | self.classes.serialize_into(bytes); |
| 1955 | } |
| 1956 | } |
| 1957 | impl GetDeviceDontPropagateListReply { |
| 1958 | /// Get the value of the `num_classes` field. |
| 1959 | /// |
| 1960 | /// The `num_classes` field is used as the length field of the `classes` field. |
| 1961 | /// This function computes the field's value again based on the length of the list. |
| 1962 | /// |
| 1963 | /// # Panics |
| 1964 | /// |
| 1965 | /// Panics if the value cannot be represented in the target type. This |
| 1966 | /// cannot happen with values of the struct received from the X11 server. |
| 1967 | pub fn num_classes(&self) -> u16 { |
| 1968 | self.classes.len() |
| 1969 | .try_into().unwrap() |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | #[derive (Clone, Default)] |
| 1974 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 1975 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 1976 | pub struct DeviceTimeCoord { |
| 1977 | pub time: xproto::Timestamp, |
| 1978 | pub axisvalues: Vec<i32>, |
| 1979 | } |
| 1980 | impl_debug_if_no_extra_traits!(DeviceTimeCoord, "DeviceTimeCoord" ); |
| 1981 | impl DeviceTimeCoord { |
| 1982 | pub fn try_parse(remaining: &[u8], num_axes: u8) -> Result<(Self, &[u8]), ParseError> { |
| 1983 | let (time: u32, remaining: &[u8]) = xproto::Timestamp::try_parse(remaining)?; |
| 1984 | let (axisvalues: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<i32>(data:remaining, list_length:num_axes.try_to_usize()?)?; |
| 1985 | let result: DeviceTimeCoord = DeviceTimeCoord { time, axisvalues }; |
| 1986 | Ok((result, remaining)) |
| 1987 | } |
| 1988 | } |
| 1989 | impl DeviceTimeCoord { |
| 1990 | #[allow (dead_code)] |
| 1991 | fn serialize(&self, num_axes: u8) -> Vec<u8> { |
| 1992 | let mut result: Vec = Vec::new(); |
| 1993 | self.serialize_into(&mut result, num_axes:u8::from(num_axes)); |
| 1994 | result |
| 1995 | } |
| 1996 | fn serialize_into(&self, bytes: &mut Vec<u8>, num_axes: u8) { |
| 1997 | self.time.serialize_into(bytes); |
| 1998 | assert_eq!(self.axisvalues.len(), usize::try_from(num_axes).unwrap(), "`axisvalues` has an incorrect length" ); |
| 1999 | self.axisvalues.serialize_into(bytes); |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | /// Opcode for the GetDeviceMotionEvents request |
| 2004 | pub const GET_DEVICE_MOTION_EVENTS_REQUEST: u8 = 10; |
| 2005 | #[derive (Clone, Copy, Default)] |
| 2006 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 2007 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 2008 | pub struct GetDeviceMotionEventsRequest { |
| 2009 | pub start: xproto::Timestamp, |
| 2010 | pub stop: xproto::Timestamp, |
| 2011 | pub device_id: u8, |
| 2012 | } |
| 2013 | impl_debug_if_no_extra_traits!(GetDeviceMotionEventsRequest, "GetDeviceMotionEventsRequest" ); |
| 2014 | impl GetDeviceMotionEventsRequest { |
| 2015 | /// Serialize this request into bytes for the provided connection |
| 2016 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 2017 | let length_so_far = 0; |
| 2018 | let start_bytes = self.start.serialize(); |
| 2019 | let stop_bytes = self.stop.serialize(); |
| 2020 | let device_id_bytes = self.device_id.serialize(); |
| 2021 | let mut request0 = vec![ |
| 2022 | major_opcode, |
| 2023 | GET_DEVICE_MOTION_EVENTS_REQUEST, |
| 2024 | 0, |
| 2025 | 0, |
| 2026 | start_bytes[0], |
| 2027 | start_bytes[1], |
| 2028 | start_bytes[2], |
| 2029 | start_bytes[3], |
| 2030 | stop_bytes[0], |
| 2031 | stop_bytes[1], |
| 2032 | stop_bytes[2], |
| 2033 | stop_bytes[3], |
| 2034 | device_id_bytes[0], |
| 2035 | 0, |
| 2036 | 0, |
| 2037 | 0, |
| 2038 | ]; |
| 2039 | let length_so_far = length_so_far + request0.len(); |
| 2040 | assert_eq!(length_so_far % 4, 0); |
| 2041 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 2042 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 2043 | ([request0.into()], vec![]) |
| 2044 | } |
| 2045 | /// Parse this request given its header, its body, and any fds that go along with it |
| 2046 | #[cfg (feature = "request-parsing" )] |
| 2047 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 2048 | if header.minor_opcode != GET_DEVICE_MOTION_EVENTS_REQUEST { |
| 2049 | return Err(ParseError::InvalidValue); |
| 2050 | } |
| 2051 | let (start, remaining) = xproto::Timestamp::try_parse(value)?; |
| 2052 | let (stop, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 2053 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 2054 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 2055 | let _ = remaining; |
| 2056 | Ok(GetDeviceMotionEventsRequest { |
| 2057 | start, |
| 2058 | stop, |
| 2059 | device_id, |
| 2060 | }) |
| 2061 | } |
| 2062 | } |
| 2063 | impl Request for GetDeviceMotionEventsRequest { |
| 2064 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 2065 | |
| 2066 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 2067 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 2068 | // Flatten the buffers into a single vector |
| 2069 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 2070 | (buf, fds) |
| 2071 | } |
| 2072 | } |
| 2073 | impl crate::x11_utils::ReplyRequest for GetDeviceMotionEventsRequest { |
| 2074 | type Reply = GetDeviceMotionEventsReply; |
| 2075 | } |
| 2076 | |
| 2077 | #[derive (Clone, Default)] |
| 2078 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 2079 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 2080 | pub struct GetDeviceMotionEventsReply { |
| 2081 | pub xi_reply_type: u8, |
| 2082 | pub sequence: u16, |
| 2083 | pub length: u32, |
| 2084 | pub num_axes: u8, |
| 2085 | pub device_mode: ValuatorMode, |
| 2086 | pub events: Vec<DeviceTimeCoord>, |
| 2087 | } |
| 2088 | impl_debug_if_no_extra_traits!(GetDeviceMotionEventsReply, "GetDeviceMotionEventsReply" ); |
| 2089 | impl TryParse for GetDeviceMotionEventsReply { |
| 2090 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 2091 | let remaining = initial_value; |
| 2092 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 2093 | let (xi_reply_type, remaining) = u8::try_parse(remaining)?; |
| 2094 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 2095 | let (length, remaining) = u32::try_parse(remaining)?; |
| 2096 | let (num_events, remaining) = u32::try_parse(remaining)?; |
| 2097 | let (num_axes, remaining) = u8::try_parse(remaining)?; |
| 2098 | let (device_mode, remaining) = u8::try_parse(remaining)?; |
| 2099 | let remaining = remaining.get(18..).ok_or(ParseError::InsufficientData)?; |
| 2100 | let mut remaining = remaining; |
| 2101 | let list_length = num_events.try_to_usize()?; |
| 2102 | let mut events = Vec::with_capacity(list_length); |
| 2103 | for _ in 0..list_length { |
| 2104 | let (v, new_remaining) = DeviceTimeCoord::try_parse(remaining, num_axes)?; |
| 2105 | remaining = new_remaining; |
| 2106 | events.push(v); |
| 2107 | } |
| 2108 | if response_type != 1 { |
| 2109 | return Err(ParseError::InvalidValue); |
| 2110 | } |
| 2111 | let device_mode = device_mode.into(); |
| 2112 | let result = GetDeviceMotionEventsReply { xi_reply_type, sequence, length, num_axes, device_mode, events }; |
| 2113 | let _ = remaining; |
| 2114 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 2115 | .ok_or(ParseError::InsufficientData)?; |
| 2116 | Ok((result, remaining)) |
| 2117 | } |
| 2118 | } |
| 2119 | impl Serialize for GetDeviceMotionEventsReply { |
| 2120 | type Bytes = Vec<u8>; |
| 2121 | fn serialize(&self) -> Vec<u8> { |
| 2122 | let mut result: Vec = Vec::new(); |
| 2123 | self.serialize_into(&mut result); |
| 2124 | result |
| 2125 | } |
| 2126 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 2127 | bytes.reserve(additional:32); |
| 2128 | let response_type_bytes: &[u8; 1] = &[1]; |
| 2129 | bytes.push(response_type_bytes[0]); |
| 2130 | self.xi_reply_type.serialize_into(bytes); |
| 2131 | self.sequence.serialize_into(bytes); |
| 2132 | self.length.serialize_into(bytes); |
| 2133 | let num_events: u32 = u32::try_from(self.events.len()).expect(msg:"`events` has too many elements" ); |
| 2134 | num_events.serialize_into(bytes); |
| 2135 | self.num_axes.serialize_into(bytes); |
| 2136 | u8::from(self.device_mode).serialize_into(bytes); |
| 2137 | bytes.extend_from_slice(&[0; 18]); |
| 2138 | for element: &DeviceTimeCoord in self.events.iter() { |
| 2139 | element.serialize_into(bytes, num_axes:u8::from(self.num_axes)); |
| 2140 | } |
| 2141 | } |
| 2142 | } |
| 2143 | impl GetDeviceMotionEventsReply { |
| 2144 | /// Get the value of the `num_events` field. |
| 2145 | /// |
| 2146 | /// The `num_events` field is used as the length field of the `events` field. |
| 2147 | /// This function computes the field's value again based on the length of the list. |
| 2148 | /// |
| 2149 | /// # Panics |
| 2150 | /// |
| 2151 | /// Panics if the value cannot be represented in the target type. This |
| 2152 | /// cannot happen with values of the struct received from the X11 server. |
| 2153 | pub fn num_events(&self) -> u32 { |
| 2154 | self.events.len() |
| 2155 | .try_into().unwrap() |
| 2156 | } |
| 2157 | } |
| 2158 | |
| 2159 | /// Opcode for the ChangeKeyboardDevice request |
| 2160 | pub const CHANGE_KEYBOARD_DEVICE_REQUEST: u8 = 11; |
| 2161 | #[derive (Clone, Copy, Default)] |
| 2162 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 2163 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 2164 | pub struct ChangeKeyboardDeviceRequest { |
| 2165 | pub device_id: u8, |
| 2166 | } |
| 2167 | impl_debug_if_no_extra_traits!(ChangeKeyboardDeviceRequest, "ChangeKeyboardDeviceRequest" ); |
| 2168 | impl ChangeKeyboardDeviceRequest { |
| 2169 | /// Serialize this request into bytes for the provided connection |
| 2170 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 2171 | let length_so_far = 0; |
| 2172 | let device_id_bytes = self.device_id.serialize(); |
| 2173 | let mut request0 = vec![ |
| 2174 | major_opcode, |
| 2175 | CHANGE_KEYBOARD_DEVICE_REQUEST, |
| 2176 | 0, |
| 2177 | 0, |
| 2178 | device_id_bytes[0], |
| 2179 | 0, |
| 2180 | 0, |
| 2181 | 0, |
| 2182 | ]; |
| 2183 | let length_so_far = length_so_far + request0.len(); |
| 2184 | assert_eq!(length_so_far % 4, 0); |
| 2185 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 2186 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 2187 | ([request0.into()], vec![]) |
| 2188 | } |
| 2189 | /// Parse this request given its header, its body, and any fds that go along with it |
| 2190 | #[cfg (feature = "request-parsing" )] |
| 2191 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 2192 | if header.minor_opcode != CHANGE_KEYBOARD_DEVICE_REQUEST { |
| 2193 | return Err(ParseError::InvalidValue); |
| 2194 | } |
| 2195 | let (device_id, remaining) = u8::try_parse(value)?; |
| 2196 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 2197 | let _ = remaining; |
| 2198 | Ok(ChangeKeyboardDeviceRequest { |
| 2199 | device_id, |
| 2200 | }) |
| 2201 | } |
| 2202 | } |
| 2203 | impl Request for ChangeKeyboardDeviceRequest { |
| 2204 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 2205 | |
| 2206 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 2207 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 2208 | // Flatten the buffers into a single vector |
| 2209 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 2210 | (buf, fds) |
| 2211 | } |
| 2212 | } |
| 2213 | impl crate::x11_utils::ReplyRequest for ChangeKeyboardDeviceRequest { |
| 2214 | type Reply = ChangeKeyboardDeviceReply; |
| 2215 | } |
| 2216 | |
| 2217 | #[derive (Clone, Copy, Default)] |
| 2218 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 2219 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 2220 | pub struct ChangeKeyboardDeviceReply { |
| 2221 | pub xi_reply_type: u8, |
| 2222 | pub sequence: u16, |
| 2223 | pub length: u32, |
| 2224 | pub status: xproto::GrabStatus, |
| 2225 | } |
| 2226 | impl_debug_if_no_extra_traits!(ChangeKeyboardDeviceReply, "ChangeKeyboardDeviceReply" ); |
| 2227 | impl TryParse for ChangeKeyboardDeviceReply { |
| 2228 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 2229 | let remaining: &[u8] = initial_value; |
| 2230 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 2231 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 2232 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 2233 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 2234 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 2235 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 2236 | if response_type != 1 { |
| 2237 | return Err(ParseError::InvalidValue); |
| 2238 | } |
| 2239 | let status: GrabStatus = status.into(); |
| 2240 | let result: ChangeKeyboardDeviceReply = ChangeKeyboardDeviceReply { xi_reply_type, sequence, length, status }; |
| 2241 | let _ = remaining; |
| 2242 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 2243 | .ok_or(err:ParseError::InsufficientData)?; |
| 2244 | Ok((result, remaining)) |
| 2245 | } |
| 2246 | } |
| 2247 | impl Serialize for ChangeKeyboardDeviceReply { |
| 2248 | type Bytes = [u8; 32]; |
| 2249 | fn serialize(&self) -> [u8; 32] { |
| 2250 | let response_type_bytes = &[1]; |
| 2251 | let xi_reply_type_bytes = self.xi_reply_type.serialize(); |
| 2252 | let sequence_bytes = self.sequence.serialize(); |
| 2253 | let length_bytes = self.length.serialize(); |
| 2254 | let status_bytes = u8::from(self.status).serialize(); |
| 2255 | [ |
| 2256 | response_type_bytes[0], |
| 2257 | xi_reply_type_bytes[0], |
| 2258 | sequence_bytes[0], |
| 2259 | sequence_bytes[1], |
| 2260 | length_bytes[0], |
| 2261 | length_bytes[1], |
| 2262 | length_bytes[2], |
| 2263 | length_bytes[3], |
| 2264 | status_bytes[0], |
| 2265 | 0, |
| 2266 | 0, |
| 2267 | 0, |
| 2268 | 0, |
| 2269 | 0, |
| 2270 | 0, |
| 2271 | 0, |
| 2272 | 0, |
| 2273 | 0, |
| 2274 | 0, |
| 2275 | 0, |
| 2276 | 0, |
| 2277 | 0, |
| 2278 | 0, |
| 2279 | 0, |
| 2280 | 0, |
| 2281 | 0, |
| 2282 | 0, |
| 2283 | 0, |
| 2284 | 0, |
| 2285 | 0, |
| 2286 | 0, |
| 2287 | 0, |
| 2288 | ] |
| 2289 | } |
| 2290 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 2291 | bytes.reserve(32); |
| 2292 | let response_type_bytes = &[1]; |
| 2293 | bytes.push(response_type_bytes[0]); |
| 2294 | self.xi_reply_type.serialize_into(bytes); |
| 2295 | self.sequence.serialize_into(bytes); |
| 2296 | self.length.serialize_into(bytes); |
| 2297 | u8::from(self.status).serialize_into(bytes); |
| 2298 | bytes.extend_from_slice(&[0; 23]); |
| 2299 | } |
| 2300 | } |
| 2301 | |
| 2302 | /// Opcode for the ChangePointerDevice request |
| 2303 | pub const CHANGE_POINTER_DEVICE_REQUEST: u8 = 12; |
| 2304 | #[derive (Clone, Copy, Default)] |
| 2305 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 2306 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 2307 | pub struct ChangePointerDeviceRequest { |
| 2308 | pub x_axis: u8, |
| 2309 | pub y_axis: u8, |
| 2310 | pub device_id: u8, |
| 2311 | } |
| 2312 | impl_debug_if_no_extra_traits!(ChangePointerDeviceRequest, "ChangePointerDeviceRequest" ); |
| 2313 | impl ChangePointerDeviceRequest { |
| 2314 | /// Serialize this request into bytes for the provided connection |
| 2315 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 2316 | let length_so_far = 0; |
| 2317 | let x_axis_bytes = self.x_axis.serialize(); |
| 2318 | let y_axis_bytes = self.y_axis.serialize(); |
| 2319 | let device_id_bytes = self.device_id.serialize(); |
| 2320 | let mut request0 = vec![ |
| 2321 | major_opcode, |
| 2322 | CHANGE_POINTER_DEVICE_REQUEST, |
| 2323 | 0, |
| 2324 | 0, |
| 2325 | x_axis_bytes[0], |
| 2326 | y_axis_bytes[0], |
| 2327 | device_id_bytes[0], |
| 2328 | 0, |
| 2329 | ]; |
| 2330 | let length_so_far = length_so_far + request0.len(); |
| 2331 | assert_eq!(length_so_far % 4, 0); |
| 2332 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 2333 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 2334 | ([request0.into()], vec![]) |
| 2335 | } |
| 2336 | /// Parse this request given its header, its body, and any fds that go along with it |
| 2337 | #[cfg (feature = "request-parsing" )] |
| 2338 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 2339 | if header.minor_opcode != CHANGE_POINTER_DEVICE_REQUEST { |
| 2340 | return Err(ParseError::InvalidValue); |
| 2341 | } |
| 2342 | let (x_axis, remaining) = u8::try_parse(value)?; |
| 2343 | let (y_axis, remaining) = u8::try_parse(remaining)?; |
| 2344 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 2345 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
| 2346 | let _ = remaining; |
| 2347 | Ok(ChangePointerDeviceRequest { |
| 2348 | x_axis, |
| 2349 | y_axis, |
| 2350 | device_id, |
| 2351 | }) |
| 2352 | } |
| 2353 | } |
| 2354 | impl Request for ChangePointerDeviceRequest { |
| 2355 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 2356 | |
| 2357 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 2358 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 2359 | // Flatten the buffers into a single vector |
| 2360 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 2361 | (buf, fds) |
| 2362 | } |
| 2363 | } |
| 2364 | impl crate::x11_utils::ReplyRequest for ChangePointerDeviceRequest { |
| 2365 | type Reply = ChangePointerDeviceReply; |
| 2366 | } |
| 2367 | |
| 2368 | #[derive (Clone, Copy, Default)] |
| 2369 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 2370 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 2371 | pub struct ChangePointerDeviceReply { |
| 2372 | pub xi_reply_type: u8, |
| 2373 | pub sequence: u16, |
| 2374 | pub length: u32, |
| 2375 | pub status: xproto::GrabStatus, |
| 2376 | } |
| 2377 | impl_debug_if_no_extra_traits!(ChangePointerDeviceReply, "ChangePointerDeviceReply" ); |
| 2378 | impl TryParse for ChangePointerDeviceReply { |
| 2379 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 2380 | let remaining: &[u8] = initial_value; |
| 2381 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 2382 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 2383 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 2384 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 2385 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 2386 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 2387 | if response_type != 1 { |
| 2388 | return Err(ParseError::InvalidValue); |
| 2389 | } |
| 2390 | let status: GrabStatus = status.into(); |
| 2391 | let result: ChangePointerDeviceReply = ChangePointerDeviceReply { xi_reply_type, sequence, length, status }; |
| 2392 | let _ = remaining; |
| 2393 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 2394 | .ok_or(err:ParseError::InsufficientData)?; |
| 2395 | Ok((result, remaining)) |
| 2396 | } |
| 2397 | } |
| 2398 | impl Serialize for ChangePointerDeviceReply { |
| 2399 | type Bytes = [u8; 32]; |
| 2400 | fn serialize(&self) -> [u8; 32] { |
| 2401 | let response_type_bytes = &[1]; |
| 2402 | let xi_reply_type_bytes = self.xi_reply_type.serialize(); |
| 2403 | let sequence_bytes = self.sequence.serialize(); |
| 2404 | let length_bytes = self.length.serialize(); |
| 2405 | let status_bytes = u8::from(self.status).serialize(); |
| 2406 | [ |
| 2407 | response_type_bytes[0], |
| 2408 | xi_reply_type_bytes[0], |
| 2409 | sequence_bytes[0], |
| 2410 | sequence_bytes[1], |
| 2411 | length_bytes[0], |
| 2412 | length_bytes[1], |
| 2413 | length_bytes[2], |
| 2414 | length_bytes[3], |
| 2415 | status_bytes[0], |
| 2416 | 0, |
| 2417 | 0, |
| 2418 | 0, |
| 2419 | 0, |
| 2420 | 0, |
| 2421 | 0, |
| 2422 | 0, |
| 2423 | 0, |
| 2424 | 0, |
| 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 | ] |
| 2440 | } |
| 2441 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 2442 | bytes.reserve(32); |
| 2443 | let response_type_bytes = &[1]; |
| 2444 | bytes.push(response_type_bytes[0]); |
| 2445 | self.xi_reply_type.serialize_into(bytes); |
| 2446 | self.sequence.serialize_into(bytes); |
| 2447 | self.length.serialize_into(bytes); |
| 2448 | u8::from(self.status).serialize_into(bytes); |
| 2449 | bytes.extend_from_slice(&[0; 23]); |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | /// Opcode for the GrabDevice request |
| 2454 | pub const GRAB_DEVICE_REQUEST: u8 = 13; |
| 2455 | #[derive (Clone, Default)] |
| 2456 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 2457 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 2458 | pub struct GrabDeviceRequest<'input> { |
| 2459 | pub grab_window: xproto::Window, |
| 2460 | pub time: xproto::Timestamp, |
| 2461 | pub this_device_mode: xproto::GrabMode, |
| 2462 | pub other_device_mode: xproto::GrabMode, |
| 2463 | pub owner_events: bool, |
| 2464 | pub device_id: u8, |
| 2465 | pub classes: Cow<'input, [EventClass]>, |
| 2466 | } |
| 2467 | impl_debug_if_no_extra_traits!(GrabDeviceRequest<'_>, "GrabDeviceRequest" ); |
| 2468 | impl<'input> GrabDeviceRequest<'input> { |
| 2469 | /// Serialize this request into bytes for the provided connection |
| 2470 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 2471 | let length_so_far = 0; |
| 2472 | let grab_window_bytes = self.grab_window.serialize(); |
| 2473 | let time_bytes = self.time.serialize(); |
| 2474 | let num_classes = u16::try_from(self.classes.len()).expect("`classes` has too many elements" ); |
| 2475 | let num_classes_bytes = num_classes.serialize(); |
| 2476 | let this_device_mode_bytes = u8::from(self.this_device_mode).serialize(); |
| 2477 | let other_device_mode_bytes = u8::from(self.other_device_mode).serialize(); |
| 2478 | let owner_events_bytes = self.owner_events.serialize(); |
| 2479 | let device_id_bytes = self.device_id.serialize(); |
| 2480 | let mut request0 = vec![ |
| 2481 | major_opcode, |
| 2482 | GRAB_DEVICE_REQUEST, |
| 2483 | 0, |
| 2484 | 0, |
| 2485 | grab_window_bytes[0], |
| 2486 | grab_window_bytes[1], |
| 2487 | grab_window_bytes[2], |
| 2488 | grab_window_bytes[3], |
| 2489 | time_bytes[0], |
| 2490 | time_bytes[1], |
| 2491 | time_bytes[2], |
| 2492 | time_bytes[3], |
| 2493 | num_classes_bytes[0], |
| 2494 | num_classes_bytes[1], |
| 2495 | this_device_mode_bytes[0], |
| 2496 | other_device_mode_bytes[0], |
| 2497 | owner_events_bytes[0], |
| 2498 | device_id_bytes[0], |
| 2499 | 0, |
| 2500 | 0, |
| 2501 | ]; |
| 2502 | let length_so_far = length_so_far + request0.len(); |
| 2503 | let classes_bytes = self.classes.serialize(); |
| 2504 | let length_so_far = length_so_far + classes_bytes.len(); |
| 2505 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 2506 | let length_so_far = length_so_far + padding0.len(); |
| 2507 | assert_eq!(length_so_far % 4, 0); |
| 2508 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 2509 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 2510 | ([request0.into(), classes_bytes.into(), padding0.into()], vec![]) |
| 2511 | } |
| 2512 | /// Parse this request given its header, its body, and any fds that go along with it |
| 2513 | #[cfg (feature = "request-parsing" )] |
| 2514 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 2515 | if header.minor_opcode != GRAB_DEVICE_REQUEST { |
| 2516 | return Err(ParseError::InvalidValue); |
| 2517 | } |
| 2518 | let (grab_window, remaining) = xproto::Window::try_parse(value)?; |
| 2519 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 2520 | let (num_classes, remaining) = u16::try_parse(remaining)?; |
| 2521 | let (this_device_mode, remaining) = u8::try_parse(remaining)?; |
| 2522 | let this_device_mode = this_device_mode.into(); |
| 2523 | let (other_device_mode, remaining) = u8::try_parse(remaining)?; |
| 2524 | let other_device_mode = other_device_mode.into(); |
| 2525 | let (owner_events, remaining) = bool::try_parse(remaining)?; |
| 2526 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 2527 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 2528 | let (classes, remaining) = crate::x11_utils::parse_list::<EventClass>(remaining, num_classes.try_to_usize()?)?; |
| 2529 | let _ = remaining; |
| 2530 | Ok(GrabDeviceRequest { |
| 2531 | grab_window, |
| 2532 | time, |
| 2533 | this_device_mode, |
| 2534 | other_device_mode, |
| 2535 | owner_events, |
| 2536 | device_id, |
| 2537 | classes: Cow::Owned(classes), |
| 2538 | }) |
| 2539 | } |
| 2540 | /// Clone all borrowed data in this GrabDeviceRequest. |
| 2541 | pub fn into_owned(self) -> GrabDeviceRequest<'static> { |
| 2542 | GrabDeviceRequest { |
| 2543 | grab_window: self.grab_window, |
| 2544 | time: self.time, |
| 2545 | this_device_mode: self.this_device_mode, |
| 2546 | other_device_mode: self.other_device_mode, |
| 2547 | owner_events: self.owner_events, |
| 2548 | device_id: self.device_id, |
| 2549 | classes: Cow::Owned(self.classes.into_owned()), |
| 2550 | } |
| 2551 | } |
| 2552 | } |
| 2553 | impl<'input> Request for GrabDeviceRequest<'input> { |
| 2554 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 2555 | |
| 2556 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 2557 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 2558 | // Flatten the buffers into a single vector |
| 2559 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 2560 | (buf, fds) |
| 2561 | } |
| 2562 | } |
| 2563 | impl<'input> crate::x11_utils::ReplyRequest for GrabDeviceRequest<'input> { |
| 2564 | type Reply = GrabDeviceReply; |
| 2565 | } |
| 2566 | |
| 2567 | #[derive (Clone, Copy, Default)] |
| 2568 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 2569 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 2570 | pub struct GrabDeviceReply { |
| 2571 | pub xi_reply_type: u8, |
| 2572 | pub sequence: u16, |
| 2573 | pub length: u32, |
| 2574 | pub status: xproto::GrabStatus, |
| 2575 | } |
| 2576 | impl_debug_if_no_extra_traits!(GrabDeviceReply, "GrabDeviceReply" ); |
| 2577 | impl TryParse for GrabDeviceReply { |
| 2578 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 2579 | let remaining: &[u8] = initial_value; |
| 2580 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 2581 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 2582 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 2583 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 2584 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 2585 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 2586 | if response_type != 1 { |
| 2587 | return Err(ParseError::InvalidValue); |
| 2588 | } |
| 2589 | let status: GrabStatus = status.into(); |
| 2590 | let result: GrabDeviceReply = GrabDeviceReply { xi_reply_type, sequence, length, status }; |
| 2591 | let _ = remaining; |
| 2592 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 2593 | .ok_or(err:ParseError::InsufficientData)?; |
| 2594 | Ok((result, remaining)) |
| 2595 | } |
| 2596 | } |
| 2597 | impl Serialize for GrabDeviceReply { |
| 2598 | type Bytes = [u8; 32]; |
| 2599 | fn serialize(&self) -> [u8; 32] { |
| 2600 | let response_type_bytes = &[1]; |
| 2601 | let xi_reply_type_bytes = self.xi_reply_type.serialize(); |
| 2602 | let sequence_bytes = self.sequence.serialize(); |
| 2603 | let length_bytes = self.length.serialize(); |
| 2604 | let status_bytes = u8::from(self.status).serialize(); |
| 2605 | [ |
| 2606 | response_type_bytes[0], |
| 2607 | xi_reply_type_bytes[0], |
| 2608 | sequence_bytes[0], |
| 2609 | sequence_bytes[1], |
| 2610 | length_bytes[0], |
| 2611 | length_bytes[1], |
| 2612 | length_bytes[2], |
| 2613 | length_bytes[3], |
| 2614 | status_bytes[0], |
| 2615 | 0, |
| 2616 | 0, |
| 2617 | 0, |
| 2618 | 0, |
| 2619 | 0, |
| 2620 | 0, |
| 2621 | 0, |
| 2622 | 0, |
| 2623 | 0, |
| 2624 | 0, |
| 2625 | 0, |
| 2626 | 0, |
| 2627 | 0, |
| 2628 | 0, |
| 2629 | 0, |
| 2630 | 0, |
| 2631 | 0, |
| 2632 | 0, |
| 2633 | 0, |
| 2634 | 0, |
| 2635 | 0, |
| 2636 | 0, |
| 2637 | 0, |
| 2638 | ] |
| 2639 | } |
| 2640 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 2641 | bytes.reserve(32); |
| 2642 | let response_type_bytes = &[1]; |
| 2643 | bytes.push(response_type_bytes[0]); |
| 2644 | self.xi_reply_type.serialize_into(bytes); |
| 2645 | self.sequence.serialize_into(bytes); |
| 2646 | self.length.serialize_into(bytes); |
| 2647 | u8::from(self.status).serialize_into(bytes); |
| 2648 | bytes.extend_from_slice(&[0; 23]); |
| 2649 | } |
| 2650 | } |
| 2651 | |
| 2652 | /// Opcode for the UngrabDevice request |
| 2653 | pub const UNGRAB_DEVICE_REQUEST: u8 = 14; |
| 2654 | #[derive (Clone, Copy, Default)] |
| 2655 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 2656 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 2657 | pub struct UngrabDeviceRequest { |
| 2658 | pub time: xproto::Timestamp, |
| 2659 | pub device_id: u8, |
| 2660 | } |
| 2661 | impl_debug_if_no_extra_traits!(UngrabDeviceRequest, "UngrabDeviceRequest" ); |
| 2662 | impl UngrabDeviceRequest { |
| 2663 | /// Serialize this request into bytes for the provided connection |
| 2664 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 2665 | let length_so_far = 0; |
| 2666 | let time_bytes = self.time.serialize(); |
| 2667 | let device_id_bytes = self.device_id.serialize(); |
| 2668 | let mut request0 = vec![ |
| 2669 | major_opcode, |
| 2670 | UNGRAB_DEVICE_REQUEST, |
| 2671 | 0, |
| 2672 | 0, |
| 2673 | time_bytes[0], |
| 2674 | time_bytes[1], |
| 2675 | time_bytes[2], |
| 2676 | time_bytes[3], |
| 2677 | device_id_bytes[0], |
| 2678 | 0, |
| 2679 | 0, |
| 2680 | 0, |
| 2681 | ]; |
| 2682 | let length_so_far = length_so_far + request0.len(); |
| 2683 | assert_eq!(length_so_far % 4, 0); |
| 2684 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 2685 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 2686 | ([request0.into()], vec![]) |
| 2687 | } |
| 2688 | /// Parse this request given its header, its body, and any fds that go along with it |
| 2689 | #[cfg (feature = "request-parsing" )] |
| 2690 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 2691 | if header.minor_opcode != UNGRAB_DEVICE_REQUEST { |
| 2692 | return Err(ParseError::InvalidValue); |
| 2693 | } |
| 2694 | let (time, remaining) = xproto::Timestamp::try_parse(value)?; |
| 2695 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 2696 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 2697 | let _ = remaining; |
| 2698 | Ok(UngrabDeviceRequest { |
| 2699 | time, |
| 2700 | device_id, |
| 2701 | }) |
| 2702 | } |
| 2703 | } |
| 2704 | impl Request for UngrabDeviceRequest { |
| 2705 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 2706 | |
| 2707 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 2708 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 2709 | // Flatten the buffers into a single vector |
| 2710 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 2711 | (buf, fds) |
| 2712 | } |
| 2713 | } |
| 2714 | impl crate::x11_utils::VoidRequest for UngrabDeviceRequest { |
| 2715 | } |
| 2716 | |
| 2717 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 2718 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 2719 | pub struct ModifierDevice(u8); |
| 2720 | impl ModifierDevice { |
| 2721 | pub const USE_X_KEYBOARD: Self = Self(255); |
| 2722 | } |
| 2723 | impl From<ModifierDevice> for u8 { |
| 2724 | #[inline ] |
| 2725 | fn from(input: ModifierDevice) -> Self { |
| 2726 | input.0 |
| 2727 | } |
| 2728 | } |
| 2729 | impl From<ModifierDevice> for Option<u8> { |
| 2730 | #[inline ] |
| 2731 | fn from(input: ModifierDevice) -> Self { |
| 2732 | Some(input.0) |
| 2733 | } |
| 2734 | } |
| 2735 | impl From<ModifierDevice> for u16 { |
| 2736 | #[inline ] |
| 2737 | fn from(input: ModifierDevice) -> Self { |
| 2738 | u16::from(input.0) |
| 2739 | } |
| 2740 | } |
| 2741 | impl From<ModifierDevice> for Option<u16> { |
| 2742 | #[inline ] |
| 2743 | fn from(input: ModifierDevice) -> Self { |
| 2744 | Some(u16::from(input.0)) |
| 2745 | } |
| 2746 | } |
| 2747 | impl From<ModifierDevice> for u32 { |
| 2748 | #[inline ] |
| 2749 | fn from(input: ModifierDevice) -> Self { |
| 2750 | u32::from(input.0) |
| 2751 | } |
| 2752 | } |
| 2753 | impl From<ModifierDevice> for Option<u32> { |
| 2754 | #[inline ] |
| 2755 | fn from(input: ModifierDevice) -> Self { |
| 2756 | Some(u32::from(input.0)) |
| 2757 | } |
| 2758 | } |
| 2759 | impl From<u8> for ModifierDevice { |
| 2760 | #[inline ] |
| 2761 | fn from(value: u8) -> Self { |
| 2762 | Self(value) |
| 2763 | } |
| 2764 | } |
| 2765 | impl core::fmt::Debug for ModifierDevice { |
| 2766 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 2767 | let variants: [(u32, &'static str, &'static …); 1] = [ |
| 2768 | (Self::USE_X_KEYBOARD.0.into(), "USE_X_KEYBOARD" , "UseXKeyboard" ), |
| 2769 | ]; |
| 2770 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | /// Opcode for the GrabDeviceKey request |
| 2775 | pub const GRAB_DEVICE_KEY_REQUEST: u8 = 15; |
| 2776 | #[derive (Clone, Default)] |
| 2777 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 2778 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 2779 | pub struct GrabDeviceKeyRequest<'input> { |
| 2780 | pub grab_window: xproto::Window, |
| 2781 | pub modifiers: xproto::ModMask, |
| 2782 | pub modifier_device: u8, |
| 2783 | pub grabbed_device: u8, |
| 2784 | pub key: u8, |
| 2785 | pub this_device_mode: xproto::GrabMode, |
| 2786 | pub other_device_mode: xproto::GrabMode, |
| 2787 | pub owner_events: bool, |
| 2788 | pub classes: Cow<'input, [EventClass]>, |
| 2789 | } |
| 2790 | impl_debug_if_no_extra_traits!(GrabDeviceKeyRequest<'_>, "GrabDeviceKeyRequest" ); |
| 2791 | impl<'input> GrabDeviceKeyRequest<'input> { |
| 2792 | /// Serialize this request into bytes for the provided connection |
| 2793 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 2794 | let length_so_far = 0; |
| 2795 | let grab_window_bytes = self.grab_window.serialize(); |
| 2796 | let num_classes = u16::try_from(self.classes.len()).expect("`classes` has too many elements" ); |
| 2797 | let num_classes_bytes = num_classes.serialize(); |
| 2798 | let modifiers_bytes = u16::from(self.modifiers).serialize(); |
| 2799 | let modifier_device_bytes = self.modifier_device.serialize(); |
| 2800 | let grabbed_device_bytes = self.grabbed_device.serialize(); |
| 2801 | let key_bytes = self.key.serialize(); |
| 2802 | let this_device_mode_bytes = u8::from(self.this_device_mode).serialize(); |
| 2803 | let other_device_mode_bytes = u8::from(self.other_device_mode).serialize(); |
| 2804 | let owner_events_bytes = self.owner_events.serialize(); |
| 2805 | let mut request0 = vec![ |
| 2806 | major_opcode, |
| 2807 | GRAB_DEVICE_KEY_REQUEST, |
| 2808 | 0, |
| 2809 | 0, |
| 2810 | grab_window_bytes[0], |
| 2811 | grab_window_bytes[1], |
| 2812 | grab_window_bytes[2], |
| 2813 | grab_window_bytes[3], |
| 2814 | num_classes_bytes[0], |
| 2815 | num_classes_bytes[1], |
| 2816 | modifiers_bytes[0], |
| 2817 | modifiers_bytes[1], |
| 2818 | modifier_device_bytes[0], |
| 2819 | grabbed_device_bytes[0], |
| 2820 | key_bytes[0], |
| 2821 | this_device_mode_bytes[0], |
| 2822 | other_device_mode_bytes[0], |
| 2823 | owner_events_bytes[0], |
| 2824 | 0, |
| 2825 | 0, |
| 2826 | ]; |
| 2827 | let length_so_far = length_so_far + request0.len(); |
| 2828 | let classes_bytes = self.classes.serialize(); |
| 2829 | let length_so_far = length_so_far + classes_bytes.len(); |
| 2830 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 2831 | let length_so_far = length_so_far + padding0.len(); |
| 2832 | assert_eq!(length_so_far % 4, 0); |
| 2833 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 2834 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 2835 | ([request0.into(), classes_bytes.into(), padding0.into()], vec![]) |
| 2836 | } |
| 2837 | /// Parse this request given its header, its body, and any fds that go along with it |
| 2838 | #[cfg (feature = "request-parsing" )] |
| 2839 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 2840 | if header.minor_opcode != GRAB_DEVICE_KEY_REQUEST { |
| 2841 | return Err(ParseError::InvalidValue); |
| 2842 | } |
| 2843 | let (grab_window, remaining) = xproto::Window::try_parse(value)?; |
| 2844 | let (num_classes, remaining) = u16::try_parse(remaining)?; |
| 2845 | let (modifiers, remaining) = u16::try_parse(remaining)?; |
| 2846 | let modifiers = modifiers.into(); |
| 2847 | let (modifier_device, remaining) = u8::try_parse(remaining)?; |
| 2848 | let (grabbed_device, remaining) = u8::try_parse(remaining)?; |
| 2849 | let (key, remaining) = u8::try_parse(remaining)?; |
| 2850 | let (this_device_mode, remaining) = u8::try_parse(remaining)?; |
| 2851 | let this_device_mode = this_device_mode.into(); |
| 2852 | let (other_device_mode, remaining) = u8::try_parse(remaining)?; |
| 2853 | let other_device_mode = other_device_mode.into(); |
| 2854 | let (owner_events, remaining) = bool::try_parse(remaining)?; |
| 2855 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 2856 | let (classes, remaining) = crate::x11_utils::parse_list::<EventClass>(remaining, num_classes.try_to_usize()?)?; |
| 2857 | let _ = remaining; |
| 2858 | Ok(GrabDeviceKeyRequest { |
| 2859 | grab_window, |
| 2860 | modifiers, |
| 2861 | modifier_device, |
| 2862 | grabbed_device, |
| 2863 | key, |
| 2864 | this_device_mode, |
| 2865 | other_device_mode, |
| 2866 | owner_events, |
| 2867 | classes: Cow::Owned(classes), |
| 2868 | }) |
| 2869 | } |
| 2870 | /// Clone all borrowed data in this GrabDeviceKeyRequest. |
| 2871 | pub fn into_owned(self) -> GrabDeviceKeyRequest<'static> { |
| 2872 | GrabDeviceKeyRequest { |
| 2873 | grab_window: self.grab_window, |
| 2874 | modifiers: self.modifiers, |
| 2875 | modifier_device: self.modifier_device, |
| 2876 | grabbed_device: self.grabbed_device, |
| 2877 | key: self.key, |
| 2878 | this_device_mode: self.this_device_mode, |
| 2879 | other_device_mode: self.other_device_mode, |
| 2880 | owner_events: self.owner_events, |
| 2881 | classes: Cow::Owned(self.classes.into_owned()), |
| 2882 | } |
| 2883 | } |
| 2884 | } |
| 2885 | impl<'input> Request for GrabDeviceKeyRequest<'input> { |
| 2886 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 2887 | |
| 2888 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 2889 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 2890 | // Flatten the buffers into a single vector |
| 2891 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 2892 | (buf, fds) |
| 2893 | } |
| 2894 | } |
| 2895 | impl<'input> crate::x11_utils::VoidRequest for GrabDeviceKeyRequest<'input> { |
| 2896 | } |
| 2897 | |
| 2898 | /// Opcode for the UngrabDeviceKey request |
| 2899 | pub const UNGRAB_DEVICE_KEY_REQUEST: u8 = 16; |
| 2900 | #[derive (Clone, Copy, Default)] |
| 2901 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 2902 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 2903 | pub struct UngrabDeviceKeyRequest { |
| 2904 | pub grab_window: xproto::Window, |
| 2905 | pub modifiers: xproto::ModMask, |
| 2906 | pub modifier_device: u8, |
| 2907 | pub key: u8, |
| 2908 | pub grabbed_device: u8, |
| 2909 | } |
| 2910 | impl_debug_if_no_extra_traits!(UngrabDeviceKeyRequest, "UngrabDeviceKeyRequest" ); |
| 2911 | impl UngrabDeviceKeyRequest { |
| 2912 | /// Serialize this request into bytes for the provided connection |
| 2913 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 2914 | let length_so_far = 0; |
| 2915 | let grab_window_bytes = self.grab_window.serialize(); |
| 2916 | let modifiers_bytes = u16::from(self.modifiers).serialize(); |
| 2917 | let modifier_device_bytes = self.modifier_device.serialize(); |
| 2918 | let key_bytes = self.key.serialize(); |
| 2919 | let grabbed_device_bytes = self.grabbed_device.serialize(); |
| 2920 | let mut request0 = vec![ |
| 2921 | major_opcode, |
| 2922 | UNGRAB_DEVICE_KEY_REQUEST, |
| 2923 | 0, |
| 2924 | 0, |
| 2925 | grab_window_bytes[0], |
| 2926 | grab_window_bytes[1], |
| 2927 | grab_window_bytes[2], |
| 2928 | grab_window_bytes[3], |
| 2929 | modifiers_bytes[0], |
| 2930 | modifiers_bytes[1], |
| 2931 | modifier_device_bytes[0], |
| 2932 | key_bytes[0], |
| 2933 | grabbed_device_bytes[0], |
| 2934 | 0, |
| 2935 | 0, |
| 2936 | 0, |
| 2937 | ]; |
| 2938 | let length_so_far = length_so_far + request0.len(); |
| 2939 | assert_eq!(length_so_far % 4, 0); |
| 2940 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 2941 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 2942 | ([request0.into()], vec![]) |
| 2943 | } |
| 2944 | /// Parse this request given its header, its body, and any fds that go along with it |
| 2945 | #[cfg (feature = "request-parsing" )] |
| 2946 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 2947 | if header.minor_opcode != UNGRAB_DEVICE_KEY_REQUEST { |
| 2948 | return Err(ParseError::InvalidValue); |
| 2949 | } |
| 2950 | let (grab_window, remaining) = xproto::Window::try_parse(value)?; |
| 2951 | let (modifiers, remaining) = u16::try_parse(remaining)?; |
| 2952 | let modifiers = modifiers.into(); |
| 2953 | let (modifier_device, remaining) = u8::try_parse(remaining)?; |
| 2954 | let (key, remaining) = u8::try_parse(remaining)?; |
| 2955 | let (grabbed_device, remaining) = u8::try_parse(remaining)?; |
| 2956 | let _ = remaining; |
| 2957 | Ok(UngrabDeviceKeyRequest { |
| 2958 | grab_window, |
| 2959 | modifiers, |
| 2960 | modifier_device, |
| 2961 | key, |
| 2962 | grabbed_device, |
| 2963 | }) |
| 2964 | } |
| 2965 | } |
| 2966 | impl Request for UngrabDeviceKeyRequest { |
| 2967 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 2968 | |
| 2969 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 2970 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 2971 | // Flatten the buffers into a single vector |
| 2972 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 2973 | (buf, fds) |
| 2974 | } |
| 2975 | } |
| 2976 | impl crate::x11_utils::VoidRequest for UngrabDeviceKeyRequest { |
| 2977 | } |
| 2978 | |
| 2979 | /// Opcode for the GrabDeviceButton request |
| 2980 | pub const GRAB_DEVICE_BUTTON_REQUEST: u8 = 17; |
| 2981 | #[derive (Clone, Default)] |
| 2982 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 2983 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 2984 | pub struct GrabDeviceButtonRequest<'input> { |
| 2985 | pub grab_window: xproto::Window, |
| 2986 | pub grabbed_device: u8, |
| 2987 | pub modifier_device: u8, |
| 2988 | pub modifiers: xproto::ModMask, |
| 2989 | pub this_device_mode: xproto::GrabMode, |
| 2990 | pub other_device_mode: xproto::GrabMode, |
| 2991 | pub button: u8, |
| 2992 | pub owner_events: bool, |
| 2993 | pub classes: Cow<'input, [EventClass]>, |
| 2994 | } |
| 2995 | impl_debug_if_no_extra_traits!(GrabDeviceButtonRequest<'_>, "GrabDeviceButtonRequest" ); |
| 2996 | impl<'input> GrabDeviceButtonRequest<'input> { |
| 2997 | /// Serialize this request into bytes for the provided connection |
| 2998 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 2999 | let length_so_far = 0; |
| 3000 | let grab_window_bytes = self.grab_window.serialize(); |
| 3001 | let grabbed_device_bytes = self.grabbed_device.serialize(); |
| 3002 | let modifier_device_bytes = self.modifier_device.serialize(); |
| 3003 | let num_classes = u16::try_from(self.classes.len()).expect("`classes` has too many elements" ); |
| 3004 | let num_classes_bytes = num_classes.serialize(); |
| 3005 | let modifiers_bytes = u16::from(self.modifiers).serialize(); |
| 3006 | let this_device_mode_bytes = u8::from(self.this_device_mode).serialize(); |
| 3007 | let other_device_mode_bytes = u8::from(self.other_device_mode).serialize(); |
| 3008 | let button_bytes = self.button.serialize(); |
| 3009 | let owner_events_bytes = self.owner_events.serialize(); |
| 3010 | let mut request0 = vec![ |
| 3011 | major_opcode, |
| 3012 | GRAB_DEVICE_BUTTON_REQUEST, |
| 3013 | 0, |
| 3014 | 0, |
| 3015 | grab_window_bytes[0], |
| 3016 | grab_window_bytes[1], |
| 3017 | grab_window_bytes[2], |
| 3018 | grab_window_bytes[3], |
| 3019 | grabbed_device_bytes[0], |
| 3020 | modifier_device_bytes[0], |
| 3021 | num_classes_bytes[0], |
| 3022 | num_classes_bytes[1], |
| 3023 | modifiers_bytes[0], |
| 3024 | modifiers_bytes[1], |
| 3025 | this_device_mode_bytes[0], |
| 3026 | other_device_mode_bytes[0], |
| 3027 | button_bytes[0], |
| 3028 | owner_events_bytes[0], |
| 3029 | 0, |
| 3030 | 0, |
| 3031 | ]; |
| 3032 | let length_so_far = length_so_far + request0.len(); |
| 3033 | let classes_bytes = self.classes.serialize(); |
| 3034 | let length_so_far = length_so_far + classes_bytes.len(); |
| 3035 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 3036 | let length_so_far = length_so_far + padding0.len(); |
| 3037 | assert_eq!(length_so_far % 4, 0); |
| 3038 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 3039 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 3040 | ([request0.into(), classes_bytes.into(), padding0.into()], vec![]) |
| 3041 | } |
| 3042 | /// Parse this request given its header, its body, and any fds that go along with it |
| 3043 | #[cfg (feature = "request-parsing" )] |
| 3044 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 3045 | if header.minor_opcode != GRAB_DEVICE_BUTTON_REQUEST { |
| 3046 | return Err(ParseError::InvalidValue); |
| 3047 | } |
| 3048 | let (grab_window, remaining) = xproto::Window::try_parse(value)?; |
| 3049 | let (grabbed_device, remaining) = u8::try_parse(remaining)?; |
| 3050 | let (modifier_device, remaining) = u8::try_parse(remaining)?; |
| 3051 | let (num_classes, remaining) = u16::try_parse(remaining)?; |
| 3052 | let (modifiers, remaining) = u16::try_parse(remaining)?; |
| 3053 | let modifiers = modifiers.into(); |
| 3054 | let (this_device_mode, remaining) = u8::try_parse(remaining)?; |
| 3055 | let this_device_mode = this_device_mode.into(); |
| 3056 | let (other_device_mode, remaining) = u8::try_parse(remaining)?; |
| 3057 | let other_device_mode = other_device_mode.into(); |
| 3058 | let (button, remaining) = u8::try_parse(remaining)?; |
| 3059 | let (owner_events, remaining) = bool::try_parse(remaining)?; |
| 3060 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 3061 | let (classes, remaining) = crate::x11_utils::parse_list::<EventClass>(remaining, num_classes.try_to_usize()?)?; |
| 3062 | let _ = remaining; |
| 3063 | Ok(GrabDeviceButtonRequest { |
| 3064 | grab_window, |
| 3065 | grabbed_device, |
| 3066 | modifier_device, |
| 3067 | modifiers, |
| 3068 | this_device_mode, |
| 3069 | other_device_mode, |
| 3070 | button, |
| 3071 | owner_events, |
| 3072 | classes: Cow::Owned(classes), |
| 3073 | }) |
| 3074 | } |
| 3075 | /// Clone all borrowed data in this GrabDeviceButtonRequest. |
| 3076 | pub fn into_owned(self) -> GrabDeviceButtonRequest<'static> { |
| 3077 | GrabDeviceButtonRequest { |
| 3078 | grab_window: self.grab_window, |
| 3079 | grabbed_device: self.grabbed_device, |
| 3080 | modifier_device: self.modifier_device, |
| 3081 | modifiers: self.modifiers, |
| 3082 | this_device_mode: self.this_device_mode, |
| 3083 | other_device_mode: self.other_device_mode, |
| 3084 | button: self.button, |
| 3085 | owner_events: self.owner_events, |
| 3086 | classes: Cow::Owned(self.classes.into_owned()), |
| 3087 | } |
| 3088 | } |
| 3089 | } |
| 3090 | impl<'input> Request for GrabDeviceButtonRequest<'input> { |
| 3091 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 3092 | |
| 3093 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 3094 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 3095 | // Flatten the buffers into a single vector |
| 3096 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 3097 | (buf, fds) |
| 3098 | } |
| 3099 | } |
| 3100 | impl<'input> crate::x11_utils::VoidRequest for GrabDeviceButtonRequest<'input> { |
| 3101 | } |
| 3102 | |
| 3103 | /// Opcode for the UngrabDeviceButton request |
| 3104 | pub const UNGRAB_DEVICE_BUTTON_REQUEST: u8 = 18; |
| 3105 | #[derive (Clone, Copy, Default)] |
| 3106 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 3107 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 3108 | pub struct UngrabDeviceButtonRequest { |
| 3109 | pub grab_window: xproto::Window, |
| 3110 | pub modifiers: xproto::ModMask, |
| 3111 | pub modifier_device: u8, |
| 3112 | pub button: u8, |
| 3113 | pub grabbed_device: u8, |
| 3114 | } |
| 3115 | impl_debug_if_no_extra_traits!(UngrabDeviceButtonRequest, "UngrabDeviceButtonRequest" ); |
| 3116 | impl UngrabDeviceButtonRequest { |
| 3117 | /// Serialize this request into bytes for the provided connection |
| 3118 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 3119 | let length_so_far = 0; |
| 3120 | let grab_window_bytes = self.grab_window.serialize(); |
| 3121 | let modifiers_bytes = u16::from(self.modifiers).serialize(); |
| 3122 | let modifier_device_bytes = self.modifier_device.serialize(); |
| 3123 | let button_bytes = self.button.serialize(); |
| 3124 | let grabbed_device_bytes = self.grabbed_device.serialize(); |
| 3125 | let mut request0 = vec![ |
| 3126 | major_opcode, |
| 3127 | UNGRAB_DEVICE_BUTTON_REQUEST, |
| 3128 | 0, |
| 3129 | 0, |
| 3130 | grab_window_bytes[0], |
| 3131 | grab_window_bytes[1], |
| 3132 | grab_window_bytes[2], |
| 3133 | grab_window_bytes[3], |
| 3134 | modifiers_bytes[0], |
| 3135 | modifiers_bytes[1], |
| 3136 | modifier_device_bytes[0], |
| 3137 | button_bytes[0], |
| 3138 | grabbed_device_bytes[0], |
| 3139 | 0, |
| 3140 | 0, |
| 3141 | 0, |
| 3142 | ]; |
| 3143 | let length_so_far = length_so_far + request0.len(); |
| 3144 | assert_eq!(length_so_far % 4, 0); |
| 3145 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 3146 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 3147 | ([request0.into()], vec![]) |
| 3148 | } |
| 3149 | /// Parse this request given its header, its body, and any fds that go along with it |
| 3150 | #[cfg (feature = "request-parsing" )] |
| 3151 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 3152 | if header.minor_opcode != UNGRAB_DEVICE_BUTTON_REQUEST { |
| 3153 | return Err(ParseError::InvalidValue); |
| 3154 | } |
| 3155 | let (grab_window, remaining) = xproto::Window::try_parse(value)?; |
| 3156 | let (modifiers, remaining) = u16::try_parse(remaining)?; |
| 3157 | let modifiers = modifiers.into(); |
| 3158 | let (modifier_device, remaining) = u8::try_parse(remaining)?; |
| 3159 | let (button, remaining) = u8::try_parse(remaining)?; |
| 3160 | let (grabbed_device, remaining) = u8::try_parse(remaining)?; |
| 3161 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 3162 | let _ = remaining; |
| 3163 | Ok(UngrabDeviceButtonRequest { |
| 3164 | grab_window, |
| 3165 | modifiers, |
| 3166 | modifier_device, |
| 3167 | button, |
| 3168 | grabbed_device, |
| 3169 | }) |
| 3170 | } |
| 3171 | } |
| 3172 | impl Request for UngrabDeviceButtonRequest { |
| 3173 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 3174 | |
| 3175 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 3176 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 3177 | // Flatten the buffers into a single vector |
| 3178 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 3179 | (buf, fds) |
| 3180 | } |
| 3181 | } |
| 3182 | impl crate::x11_utils::VoidRequest for UngrabDeviceButtonRequest { |
| 3183 | } |
| 3184 | |
| 3185 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 3186 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 3187 | pub struct DeviceInputMode(u8); |
| 3188 | impl DeviceInputMode { |
| 3189 | pub const ASYNC_THIS_DEVICE: Self = Self(0); |
| 3190 | pub const SYNC_THIS_DEVICE: Self = Self(1); |
| 3191 | pub const REPLAY_THIS_DEVICE: Self = Self(2); |
| 3192 | pub const ASYNC_OTHER_DEVICES: Self = Self(3); |
| 3193 | pub const ASYNC_ALL: Self = Self(4); |
| 3194 | pub const SYNC_ALL: Self = Self(5); |
| 3195 | } |
| 3196 | impl From<DeviceInputMode> for u8 { |
| 3197 | #[inline ] |
| 3198 | fn from(input: DeviceInputMode) -> Self { |
| 3199 | input.0 |
| 3200 | } |
| 3201 | } |
| 3202 | impl From<DeviceInputMode> for Option<u8> { |
| 3203 | #[inline ] |
| 3204 | fn from(input: DeviceInputMode) -> Self { |
| 3205 | Some(input.0) |
| 3206 | } |
| 3207 | } |
| 3208 | impl From<DeviceInputMode> for u16 { |
| 3209 | #[inline ] |
| 3210 | fn from(input: DeviceInputMode) -> Self { |
| 3211 | u16::from(input.0) |
| 3212 | } |
| 3213 | } |
| 3214 | impl From<DeviceInputMode> for Option<u16> { |
| 3215 | #[inline ] |
| 3216 | fn from(input: DeviceInputMode) -> Self { |
| 3217 | Some(u16::from(input.0)) |
| 3218 | } |
| 3219 | } |
| 3220 | impl From<DeviceInputMode> for u32 { |
| 3221 | #[inline ] |
| 3222 | fn from(input: DeviceInputMode) -> Self { |
| 3223 | u32::from(input.0) |
| 3224 | } |
| 3225 | } |
| 3226 | impl From<DeviceInputMode> for Option<u32> { |
| 3227 | #[inline ] |
| 3228 | fn from(input: DeviceInputMode) -> Self { |
| 3229 | Some(u32::from(input.0)) |
| 3230 | } |
| 3231 | } |
| 3232 | impl From<u8> for DeviceInputMode { |
| 3233 | #[inline ] |
| 3234 | fn from(value: u8) -> Self { |
| 3235 | Self(value) |
| 3236 | } |
| 3237 | } |
| 3238 | impl core::fmt::Debug for DeviceInputMode { |
| 3239 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 3240 | let variants: [(u32, &'static str, &'static …); 6] = [ |
| 3241 | (Self::ASYNC_THIS_DEVICE.0.into(), "ASYNC_THIS_DEVICE" , "AsyncThisDevice" ), |
| 3242 | (Self::SYNC_THIS_DEVICE.0.into(), "SYNC_THIS_DEVICE" , "SyncThisDevice" ), |
| 3243 | (Self::REPLAY_THIS_DEVICE.0.into(), "REPLAY_THIS_DEVICE" , "ReplayThisDevice" ), |
| 3244 | (Self::ASYNC_OTHER_DEVICES.0.into(), "ASYNC_OTHER_DEVICES" , "AsyncOtherDevices" ), |
| 3245 | (Self::ASYNC_ALL.0.into(), "ASYNC_ALL" , "AsyncAll" ), |
| 3246 | (Self::SYNC_ALL.0.into(), "SYNC_ALL" , "SyncAll" ), |
| 3247 | ]; |
| 3248 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 3249 | } |
| 3250 | } |
| 3251 | |
| 3252 | /// Opcode for the AllowDeviceEvents request |
| 3253 | pub const ALLOW_DEVICE_EVENTS_REQUEST: u8 = 19; |
| 3254 | #[derive (Clone, Copy, Default)] |
| 3255 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 3256 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 3257 | pub struct AllowDeviceEventsRequest { |
| 3258 | pub time: xproto::Timestamp, |
| 3259 | pub mode: DeviceInputMode, |
| 3260 | pub device_id: u8, |
| 3261 | } |
| 3262 | impl_debug_if_no_extra_traits!(AllowDeviceEventsRequest, "AllowDeviceEventsRequest" ); |
| 3263 | impl AllowDeviceEventsRequest { |
| 3264 | /// Serialize this request into bytes for the provided connection |
| 3265 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 3266 | let length_so_far = 0; |
| 3267 | let time_bytes = self.time.serialize(); |
| 3268 | let mode_bytes = u8::from(self.mode).serialize(); |
| 3269 | let device_id_bytes = self.device_id.serialize(); |
| 3270 | let mut request0 = vec![ |
| 3271 | major_opcode, |
| 3272 | ALLOW_DEVICE_EVENTS_REQUEST, |
| 3273 | 0, |
| 3274 | 0, |
| 3275 | time_bytes[0], |
| 3276 | time_bytes[1], |
| 3277 | time_bytes[2], |
| 3278 | time_bytes[3], |
| 3279 | mode_bytes[0], |
| 3280 | device_id_bytes[0], |
| 3281 | 0, |
| 3282 | 0, |
| 3283 | ]; |
| 3284 | let length_so_far = length_so_far + request0.len(); |
| 3285 | assert_eq!(length_so_far % 4, 0); |
| 3286 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 3287 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 3288 | ([request0.into()], vec![]) |
| 3289 | } |
| 3290 | /// Parse this request given its header, its body, and any fds that go along with it |
| 3291 | #[cfg (feature = "request-parsing" )] |
| 3292 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 3293 | if header.minor_opcode != ALLOW_DEVICE_EVENTS_REQUEST { |
| 3294 | return Err(ParseError::InvalidValue); |
| 3295 | } |
| 3296 | let (time, remaining) = xproto::Timestamp::try_parse(value)?; |
| 3297 | let (mode, remaining) = u8::try_parse(remaining)?; |
| 3298 | let mode = mode.into(); |
| 3299 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 3300 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 3301 | let _ = remaining; |
| 3302 | Ok(AllowDeviceEventsRequest { |
| 3303 | time, |
| 3304 | mode, |
| 3305 | device_id, |
| 3306 | }) |
| 3307 | } |
| 3308 | } |
| 3309 | impl Request for AllowDeviceEventsRequest { |
| 3310 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 3311 | |
| 3312 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 3313 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 3314 | // Flatten the buffers into a single vector |
| 3315 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 3316 | (buf, fds) |
| 3317 | } |
| 3318 | } |
| 3319 | impl crate::x11_utils::VoidRequest for AllowDeviceEventsRequest { |
| 3320 | } |
| 3321 | |
| 3322 | /// Opcode for the GetDeviceFocus request |
| 3323 | pub const GET_DEVICE_FOCUS_REQUEST: u8 = 20; |
| 3324 | #[derive (Clone, Copy, Default)] |
| 3325 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 3326 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 3327 | pub struct GetDeviceFocusRequest { |
| 3328 | pub device_id: u8, |
| 3329 | } |
| 3330 | impl_debug_if_no_extra_traits!(GetDeviceFocusRequest, "GetDeviceFocusRequest" ); |
| 3331 | impl GetDeviceFocusRequest { |
| 3332 | /// Serialize this request into bytes for the provided connection |
| 3333 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 3334 | let length_so_far = 0; |
| 3335 | let device_id_bytes = self.device_id.serialize(); |
| 3336 | let mut request0 = vec![ |
| 3337 | major_opcode, |
| 3338 | GET_DEVICE_FOCUS_REQUEST, |
| 3339 | 0, |
| 3340 | 0, |
| 3341 | device_id_bytes[0], |
| 3342 | 0, |
| 3343 | 0, |
| 3344 | 0, |
| 3345 | ]; |
| 3346 | let length_so_far = length_so_far + request0.len(); |
| 3347 | assert_eq!(length_so_far % 4, 0); |
| 3348 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 3349 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 3350 | ([request0.into()], vec![]) |
| 3351 | } |
| 3352 | /// Parse this request given its header, its body, and any fds that go along with it |
| 3353 | #[cfg (feature = "request-parsing" )] |
| 3354 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 3355 | if header.minor_opcode != GET_DEVICE_FOCUS_REQUEST { |
| 3356 | return Err(ParseError::InvalidValue); |
| 3357 | } |
| 3358 | let (device_id, remaining) = u8::try_parse(value)?; |
| 3359 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 3360 | let _ = remaining; |
| 3361 | Ok(GetDeviceFocusRequest { |
| 3362 | device_id, |
| 3363 | }) |
| 3364 | } |
| 3365 | } |
| 3366 | impl Request for GetDeviceFocusRequest { |
| 3367 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 3368 | |
| 3369 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 3370 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 3371 | // Flatten the buffers into a single vector |
| 3372 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 3373 | (buf, fds) |
| 3374 | } |
| 3375 | } |
| 3376 | impl crate::x11_utils::ReplyRequest for GetDeviceFocusRequest { |
| 3377 | type Reply = GetDeviceFocusReply; |
| 3378 | } |
| 3379 | |
| 3380 | #[derive (Clone, Copy, Default)] |
| 3381 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 3382 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 3383 | pub struct GetDeviceFocusReply { |
| 3384 | pub xi_reply_type: u8, |
| 3385 | pub sequence: u16, |
| 3386 | pub length: u32, |
| 3387 | pub focus: xproto::Window, |
| 3388 | pub time: xproto::Timestamp, |
| 3389 | pub revert_to: xproto::InputFocus, |
| 3390 | } |
| 3391 | impl_debug_if_no_extra_traits!(GetDeviceFocusReply, "GetDeviceFocusReply" ); |
| 3392 | impl TryParse for GetDeviceFocusReply { |
| 3393 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 3394 | let remaining: &[u8] = initial_value; |
| 3395 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3396 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3397 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3398 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 3399 | let (focus: u32, remaining: &[u8]) = xproto::Window::try_parse(remaining)?; |
| 3400 | let (time: u32, remaining: &[u8]) = xproto::Timestamp::try_parse(remaining)?; |
| 3401 | let (revert_to: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3402 | let remaining: &[u8] = remaining.get(15..).ok_or(err:ParseError::InsufficientData)?; |
| 3403 | if response_type != 1 { |
| 3404 | return Err(ParseError::InvalidValue); |
| 3405 | } |
| 3406 | let revert_to: InputFocus = revert_to.into(); |
| 3407 | let result: GetDeviceFocusReply = GetDeviceFocusReply { xi_reply_type, sequence, length, focus, time, revert_to }; |
| 3408 | let _ = remaining; |
| 3409 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 3410 | .ok_or(err:ParseError::InsufficientData)?; |
| 3411 | Ok((result, remaining)) |
| 3412 | } |
| 3413 | } |
| 3414 | impl Serialize for GetDeviceFocusReply { |
| 3415 | type Bytes = [u8; 32]; |
| 3416 | fn serialize(&self) -> [u8; 32] { |
| 3417 | let response_type_bytes = &[1]; |
| 3418 | let xi_reply_type_bytes = self.xi_reply_type.serialize(); |
| 3419 | let sequence_bytes = self.sequence.serialize(); |
| 3420 | let length_bytes = self.length.serialize(); |
| 3421 | let focus_bytes = self.focus.serialize(); |
| 3422 | let time_bytes = self.time.serialize(); |
| 3423 | let revert_to_bytes = u8::from(self.revert_to).serialize(); |
| 3424 | [ |
| 3425 | response_type_bytes[0], |
| 3426 | xi_reply_type_bytes[0], |
| 3427 | sequence_bytes[0], |
| 3428 | sequence_bytes[1], |
| 3429 | length_bytes[0], |
| 3430 | length_bytes[1], |
| 3431 | length_bytes[2], |
| 3432 | length_bytes[3], |
| 3433 | focus_bytes[0], |
| 3434 | focus_bytes[1], |
| 3435 | focus_bytes[2], |
| 3436 | focus_bytes[3], |
| 3437 | time_bytes[0], |
| 3438 | time_bytes[1], |
| 3439 | time_bytes[2], |
| 3440 | time_bytes[3], |
| 3441 | revert_to_bytes[0], |
| 3442 | 0, |
| 3443 | 0, |
| 3444 | 0, |
| 3445 | 0, |
| 3446 | 0, |
| 3447 | 0, |
| 3448 | 0, |
| 3449 | 0, |
| 3450 | 0, |
| 3451 | 0, |
| 3452 | 0, |
| 3453 | 0, |
| 3454 | 0, |
| 3455 | 0, |
| 3456 | 0, |
| 3457 | ] |
| 3458 | } |
| 3459 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 3460 | bytes.reserve(32); |
| 3461 | let response_type_bytes = &[1]; |
| 3462 | bytes.push(response_type_bytes[0]); |
| 3463 | self.xi_reply_type.serialize_into(bytes); |
| 3464 | self.sequence.serialize_into(bytes); |
| 3465 | self.length.serialize_into(bytes); |
| 3466 | self.focus.serialize_into(bytes); |
| 3467 | self.time.serialize_into(bytes); |
| 3468 | u8::from(self.revert_to).serialize_into(bytes); |
| 3469 | bytes.extend_from_slice(&[0; 15]); |
| 3470 | } |
| 3471 | } |
| 3472 | |
| 3473 | /// Opcode for the SetDeviceFocus request |
| 3474 | pub const SET_DEVICE_FOCUS_REQUEST: u8 = 21; |
| 3475 | #[derive (Clone, Copy, Default)] |
| 3476 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 3477 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 3478 | pub struct SetDeviceFocusRequest { |
| 3479 | pub focus: xproto::Window, |
| 3480 | pub time: xproto::Timestamp, |
| 3481 | pub revert_to: xproto::InputFocus, |
| 3482 | pub device_id: u8, |
| 3483 | } |
| 3484 | impl_debug_if_no_extra_traits!(SetDeviceFocusRequest, "SetDeviceFocusRequest" ); |
| 3485 | impl SetDeviceFocusRequest { |
| 3486 | /// Serialize this request into bytes for the provided connection |
| 3487 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 3488 | let length_so_far = 0; |
| 3489 | let focus_bytes = self.focus.serialize(); |
| 3490 | let time_bytes = self.time.serialize(); |
| 3491 | let revert_to_bytes = u8::from(self.revert_to).serialize(); |
| 3492 | let device_id_bytes = self.device_id.serialize(); |
| 3493 | let mut request0 = vec![ |
| 3494 | major_opcode, |
| 3495 | SET_DEVICE_FOCUS_REQUEST, |
| 3496 | 0, |
| 3497 | 0, |
| 3498 | focus_bytes[0], |
| 3499 | focus_bytes[1], |
| 3500 | focus_bytes[2], |
| 3501 | focus_bytes[3], |
| 3502 | time_bytes[0], |
| 3503 | time_bytes[1], |
| 3504 | time_bytes[2], |
| 3505 | time_bytes[3], |
| 3506 | revert_to_bytes[0], |
| 3507 | device_id_bytes[0], |
| 3508 | 0, |
| 3509 | 0, |
| 3510 | ]; |
| 3511 | let length_so_far = length_so_far + request0.len(); |
| 3512 | assert_eq!(length_so_far % 4, 0); |
| 3513 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 3514 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 3515 | ([request0.into()], vec![]) |
| 3516 | } |
| 3517 | /// Parse this request given its header, its body, and any fds that go along with it |
| 3518 | #[cfg (feature = "request-parsing" )] |
| 3519 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 3520 | if header.minor_opcode != SET_DEVICE_FOCUS_REQUEST { |
| 3521 | return Err(ParseError::InvalidValue); |
| 3522 | } |
| 3523 | let (focus, remaining) = xproto::Window::try_parse(value)?; |
| 3524 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 3525 | let (revert_to, remaining) = u8::try_parse(remaining)?; |
| 3526 | let revert_to = revert_to.into(); |
| 3527 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 3528 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 3529 | let _ = remaining; |
| 3530 | Ok(SetDeviceFocusRequest { |
| 3531 | focus, |
| 3532 | time, |
| 3533 | revert_to, |
| 3534 | device_id, |
| 3535 | }) |
| 3536 | } |
| 3537 | } |
| 3538 | impl Request for SetDeviceFocusRequest { |
| 3539 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 3540 | |
| 3541 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 3542 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 3543 | // Flatten the buffers into a single vector |
| 3544 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 3545 | (buf, fds) |
| 3546 | } |
| 3547 | } |
| 3548 | impl crate::x11_utils::VoidRequest for SetDeviceFocusRequest { |
| 3549 | } |
| 3550 | |
| 3551 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 3552 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 3553 | pub struct FeedbackClass(u8); |
| 3554 | impl FeedbackClass { |
| 3555 | pub const KEYBOARD: Self = Self(0); |
| 3556 | pub const POINTER: Self = Self(1); |
| 3557 | pub const STRING: Self = Self(2); |
| 3558 | pub const INTEGER: Self = Self(3); |
| 3559 | pub const LED: Self = Self(4); |
| 3560 | pub const BELL: Self = Self(5); |
| 3561 | } |
| 3562 | impl From<FeedbackClass> for u8 { |
| 3563 | #[inline ] |
| 3564 | fn from(input: FeedbackClass) -> Self { |
| 3565 | input.0 |
| 3566 | } |
| 3567 | } |
| 3568 | impl From<FeedbackClass> for Option<u8> { |
| 3569 | #[inline ] |
| 3570 | fn from(input: FeedbackClass) -> Self { |
| 3571 | Some(input.0) |
| 3572 | } |
| 3573 | } |
| 3574 | impl From<FeedbackClass> for u16 { |
| 3575 | #[inline ] |
| 3576 | fn from(input: FeedbackClass) -> Self { |
| 3577 | u16::from(input.0) |
| 3578 | } |
| 3579 | } |
| 3580 | impl From<FeedbackClass> for Option<u16> { |
| 3581 | #[inline ] |
| 3582 | fn from(input: FeedbackClass) -> Self { |
| 3583 | Some(u16::from(input.0)) |
| 3584 | } |
| 3585 | } |
| 3586 | impl From<FeedbackClass> for u32 { |
| 3587 | #[inline ] |
| 3588 | fn from(input: FeedbackClass) -> Self { |
| 3589 | u32::from(input.0) |
| 3590 | } |
| 3591 | } |
| 3592 | impl From<FeedbackClass> for Option<u32> { |
| 3593 | #[inline ] |
| 3594 | fn from(input: FeedbackClass) -> Self { |
| 3595 | Some(u32::from(input.0)) |
| 3596 | } |
| 3597 | } |
| 3598 | impl From<u8> for FeedbackClass { |
| 3599 | #[inline ] |
| 3600 | fn from(value: u8) -> Self { |
| 3601 | Self(value) |
| 3602 | } |
| 3603 | } |
| 3604 | impl core::fmt::Debug for FeedbackClass { |
| 3605 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 3606 | let variants: [(u32, &'static str, &'static …); 6] = [ |
| 3607 | (Self::KEYBOARD.0.into(), "KEYBOARD" , "Keyboard" ), |
| 3608 | (Self::POINTER.0.into(), "POINTER" , "Pointer" ), |
| 3609 | (Self::STRING.0.into(), "STRING" , "String" ), |
| 3610 | (Self::INTEGER.0.into(), "INTEGER" , "Integer" ), |
| 3611 | (Self::LED.0.into(), "LED" , "Led" ), |
| 3612 | (Self::BELL.0.into(), "BELL" , "Bell" ), |
| 3613 | ]; |
| 3614 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | #[derive (Clone, Copy, Default)] |
| 3619 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 3620 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 3621 | pub struct KbdFeedbackState { |
| 3622 | pub class_id: FeedbackClass, |
| 3623 | pub feedback_id: u8, |
| 3624 | pub len: u16, |
| 3625 | pub pitch: u16, |
| 3626 | pub duration: u16, |
| 3627 | pub led_mask: u32, |
| 3628 | pub led_values: u32, |
| 3629 | pub global_auto_repeat: bool, |
| 3630 | pub click: u8, |
| 3631 | pub percent: u8, |
| 3632 | pub auto_repeats: [u8; 32], |
| 3633 | } |
| 3634 | impl_debug_if_no_extra_traits!(KbdFeedbackState, "KbdFeedbackState" ); |
| 3635 | impl TryParse for KbdFeedbackState { |
| 3636 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 3637 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3638 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3639 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3640 | let (pitch: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3641 | let (duration: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3642 | let (led_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 3643 | let (led_values: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 3644 | let (global_auto_repeat: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
| 3645 | let (click: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3646 | let (percent: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3647 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 3648 | let (auto_repeats: [u8; 32], remaining: &[u8]) = crate::x11_utils::parse_u8_array::<32>(data:remaining)?; |
| 3649 | let class_id: FeedbackClass = class_id.into(); |
| 3650 | let result: KbdFeedbackState = KbdFeedbackState { class_id, feedback_id, len, pitch, duration, led_mask, led_values, global_auto_repeat, click, percent, auto_repeats }; |
| 3651 | Ok((result, remaining)) |
| 3652 | } |
| 3653 | } |
| 3654 | impl Serialize for KbdFeedbackState { |
| 3655 | type Bytes = [u8; 52]; |
| 3656 | fn serialize(&self) -> [u8; 52] { |
| 3657 | let class_id_bytes = u8::from(self.class_id).serialize(); |
| 3658 | let feedback_id_bytes = self.feedback_id.serialize(); |
| 3659 | let len_bytes = self.len.serialize(); |
| 3660 | let pitch_bytes = self.pitch.serialize(); |
| 3661 | let duration_bytes = self.duration.serialize(); |
| 3662 | let led_mask_bytes = self.led_mask.serialize(); |
| 3663 | let led_values_bytes = self.led_values.serialize(); |
| 3664 | let global_auto_repeat_bytes = self.global_auto_repeat.serialize(); |
| 3665 | let click_bytes = self.click.serialize(); |
| 3666 | let percent_bytes = self.percent.serialize(); |
| 3667 | [ |
| 3668 | class_id_bytes[0], |
| 3669 | feedback_id_bytes[0], |
| 3670 | len_bytes[0], |
| 3671 | len_bytes[1], |
| 3672 | pitch_bytes[0], |
| 3673 | pitch_bytes[1], |
| 3674 | duration_bytes[0], |
| 3675 | duration_bytes[1], |
| 3676 | led_mask_bytes[0], |
| 3677 | led_mask_bytes[1], |
| 3678 | led_mask_bytes[2], |
| 3679 | led_mask_bytes[3], |
| 3680 | led_values_bytes[0], |
| 3681 | led_values_bytes[1], |
| 3682 | led_values_bytes[2], |
| 3683 | led_values_bytes[3], |
| 3684 | global_auto_repeat_bytes[0], |
| 3685 | click_bytes[0], |
| 3686 | percent_bytes[0], |
| 3687 | 0, |
| 3688 | self.auto_repeats[0], |
| 3689 | self.auto_repeats[1], |
| 3690 | self.auto_repeats[2], |
| 3691 | self.auto_repeats[3], |
| 3692 | self.auto_repeats[4], |
| 3693 | self.auto_repeats[5], |
| 3694 | self.auto_repeats[6], |
| 3695 | self.auto_repeats[7], |
| 3696 | self.auto_repeats[8], |
| 3697 | self.auto_repeats[9], |
| 3698 | self.auto_repeats[10], |
| 3699 | self.auto_repeats[11], |
| 3700 | self.auto_repeats[12], |
| 3701 | self.auto_repeats[13], |
| 3702 | self.auto_repeats[14], |
| 3703 | self.auto_repeats[15], |
| 3704 | self.auto_repeats[16], |
| 3705 | self.auto_repeats[17], |
| 3706 | self.auto_repeats[18], |
| 3707 | self.auto_repeats[19], |
| 3708 | self.auto_repeats[20], |
| 3709 | self.auto_repeats[21], |
| 3710 | self.auto_repeats[22], |
| 3711 | self.auto_repeats[23], |
| 3712 | self.auto_repeats[24], |
| 3713 | self.auto_repeats[25], |
| 3714 | self.auto_repeats[26], |
| 3715 | self.auto_repeats[27], |
| 3716 | self.auto_repeats[28], |
| 3717 | self.auto_repeats[29], |
| 3718 | self.auto_repeats[30], |
| 3719 | self.auto_repeats[31], |
| 3720 | ] |
| 3721 | } |
| 3722 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 3723 | bytes.reserve(52); |
| 3724 | u8::from(self.class_id).serialize_into(bytes); |
| 3725 | self.feedback_id.serialize_into(bytes); |
| 3726 | self.len.serialize_into(bytes); |
| 3727 | self.pitch.serialize_into(bytes); |
| 3728 | self.duration.serialize_into(bytes); |
| 3729 | self.led_mask.serialize_into(bytes); |
| 3730 | self.led_values.serialize_into(bytes); |
| 3731 | self.global_auto_repeat.serialize_into(bytes); |
| 3732 | self.click.serialize_into(bytes); |
| 3733 | self.percent.serialize_into(bytes); |
| 3734 | bytes.extend_from_slice(&[0; 1]); |
| 3735 | bytes.extend_from_slice(&self.auto_repeats); |
| 3736 | } |
| 3737 | } |
| 3738 | |
| 3739 | #[derive (Clone, Copy, Default)] |
| 3740 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 3741 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 3742 | pub struct PtrFeedbackState { |
| 3743 | pub class_id: FeedbackClass, |
| 3744 | pub feedback_id: u8, |
| 3745 | pub len: u16, |
| 3746 | pub accel_num: u16, |
| 3747 | pub accel_denom: u16, |
| 3748 | pub threshold: u16, |
| 3749 | } |
| 3750 | impl_debug_if_no_extra_traits!(PtrFeedbackState, "PtrFeedbackState" ); |
| 3751 | impl TryParse for PtrFeedbackState { |
| 3752 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 3753 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3754 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3755 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3756 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 3757 | let (accel_num: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3758 | let (accel_denom: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3759 | let (threshold: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3760 | let class_id: FeedbackClass = class_id.into(); |
| 3761 | let result: PtrFeedbackState = PtrFeedbackState { class_id, feedback_id, len, accel_num, accel_denom, threshold }; |
| 3762 | Ok((result, remaining)) |
| 3763 | } |
| 3764 | } |
| 3765 | impl Serialize for PtrFeedbackState { |
| 3766 | type Bytes = [u8; 12]; |
| 3767 | fn serialize(&self) -> [u8; 12] { |
| 3768 | let class_id_bytes = u8::from(self.class_id).serialize(); |
| 3769 | let feedback_id_bytes = self.feedback_id.serialize(); |
| 3770 | let len_bytes = self.len.serialize(); |
| 3771 | let accel_num_bytes = self.accel_num.serialize(); |
| 3772 | let accel_denom_bytes = self.accel_denom.serialize(); |
| 3773 | let threshold_bytes = self.threshold.serialize(); |
| 3774 | [ |
| 3775 | class_id_bytes[0], |
| 3776 | feedback_id_bytes[0], |
| 3777 | len_bytes[0], |
| 3778 | len_bytes[1], |
| 3779 | 0, |
| 3780 | 0, |
| 3781 | accel_num_bytes[0], |
| 3782 | accel_num_bytes[1], |
| 3783 | accel_denom_bytes[0], |
| 3784 | accel_denom_bytes[1], |
| 3785 | threshold_bytes[0], |
| 3786 | threshold_bytes[1], |
| 3787 | ] |
| 3788 | } |
| 3789 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 3790 | bytes.reserve(12); |
| 3791 | u8::from(self.class_id).serialize_into(bytes); |
| 3792 | self.feedback_id.serialize_into(bytes); |
| 3793 | self.len.serialize_into(bytes); |
| 3794 | bytes.extend_from_slice(&[0; 2]); |
| 3795 | self.accel_num.serialize_into(bytes); |
| 3796 | self.accel_denom.serialize_into(bytes); |
| 3797 | self.threshold.serialize_into(bytes); |
| 3798 | } |
| 3799 | } |
| 3800 | |
| 3801 | #[derive (Clone, Copy, Default)] |
| 3802 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 3803 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 3804 | pub struct IntegerFeedbackState { |
| 3805 | pub class_id: FeedbackClass, |
| 3806 | pub feedback_id: u8, |
| 3807 | pub len: u16, |
| 3808 | pub resolution: u32, |
| 3809 | pub min_value: i32, |
| 3810 | pub max_value: i32, |
| 3811 | } |
| 3812 | impl_debug_if_no_extra_traits!(IntegerFeedbackState, "IntegerFeedbackState" ); |
| 3813 | impl TryParse for IntegerFeedbackState { |
| 3814 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 3815 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3816 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3817 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3818 | let (resolution: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 3819 | let (min_value: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 3820 | let (max_value: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 3821 | let class_id: FeedbackClass = class_id.into(); |
| 3822 | let result: IntegerFeedbackState = IntegerFeedbackState { class_id, feedback_id, len, resolution, min_value, max_value }; |
| 3823 | Ok((result, remaining)) |
| 3824 | } |
| 3825 | } |
| 3826 | impl Serialize for IntegerFeedbackState { |
| 3827 | type Bytes = [u8; 16]; |
| 3828 | fn serialize(&self) -> [u8; 16] { |
| 3829 | let class_id_bytes = u8::from(self.class_id).serialize(); |
| 3830 | let feedback_id_bytes = self.feedback_id.serialize(); |
| 3831 | let len_bytes = self.len.serialize(); |
| 3832 | let resolution_bytes = self.resolution.serialize(); |
| 3833 | let min_value_bytes = self.min_value.serialize(); |
| 3834 | let max_value_bytes = self.max_value.serialize(); |
| 3835 | [ |
| 3836 | class_id_bytes[0], |
| 3837 | feedback_id_bytes[0], |
| 3838 | len_bytes[0], |
| 3839 | len_bytes[1], |
| 3840 | resolution_bytes[0], |
| 3841 | resolution_bytes[1], |
| 3842 | resolution_bytes[2], |
| 3843 | resolution_bytes[3], |
| 3844 | min_value_bytes[0], |
| 3845 | min_value_bytes[1], |
| 3846 | min_value_bytes[2], |
| 3847 | min_value_bytes[3], |
| 3848 | max_value_bytes[0], |
| 3849 | max_value_bytes[1], |
| 3850 | max_value_bytes[2], |
| 3851 | max_value_bytes[3], |
| 3852 | ] |
| 3853 | } |
| 3854 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 3855 | bytes.reserve(16); |
| 3856 | u8::from(self.class_id).serialize_into(bytes); |
| 3857 | self.feedback_id.serialize_into(bytes); |
| 3858 | self.len.serialize_into(bytes); |
| 3859 | self.resolution.serialize_into(bytes); |
| 3860 | self.min_value.serialize_into(bytes); |
| 3861 | self.max_value.serialize_into(bytes); |
| 3862 | } |
| 3863 | } |
| 3864 | |
| 3865 | #[derive (Clone, Default)] |
| 3866 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 3867 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 3868 | pub struct StringFeedbackState { |
| 3869 | pub class_id: FeedbackClass, |
| 3870 | pub feedback_id: u8, |
| 3871 | pub len: u16, |
| 3872 | pub max_symbols: u16, |
| 3873 | pub keysyms: Vec<xproto::Keysym>, |
| 3874 | } |
| 3875 | impl_debug_if_no_extra_traits!(StringFeedbackState, "StringFeedbackState" ); |
| 3876 | impl TryParse for StringFeedbackState { |
| 3877 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 3878 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3879 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3880 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3881 | let (max_symbols: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3882 | let (num_keysyms: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3883 | let (keysyms: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<xproto::Keysym>(data:remaining, list_length:num_keysyms.try_to_usize()?)?; |
| 3884 | let class_id: FeedbackClass = class_id.into(); |
| 3885 | let result: StringFeedbackState = StringFeedbackState { class_id, feedback_id, len, max_symbols, keysyms }; |
| 3886 | Ok((result, remaining)) |
| 3887 | } |
| 3888 | } |
| 3889 | impl Serialize for StringFeedbackState { |
| 3890 | type Bytes = Vec<u8>; |
| 3891 | fn serialize(&self) -> Vec<u8> { |
| 3892 | let mut result: Vec = Vec::new(); |
| 3893 | self.serialize_into(&mut result); |
| 3894 | result |
| 3895 | } |
| 3896 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 3897 | bytes.reserve(additional:8); |
| 3898 | u8::from(self.class_id).serialize_into(bytes); |
| 3899 | self.feedback_id.serialize_into(bytes); |
| 3900 | self.len.serialize_into(bytes); |
| 3901 | self.max_symbols.serialize_into(bytes); |
| 3902 | let num_keysyms: u16 = u16::try_from(self.keysyms.len()).expect(msg:"`keysyms` has too many elements" ); |
| 3903 | num_keysyms.serialize_into(bytes); |
| 3904 | self.keysyms.serialize_into(bytes); |
| 3905 | } |
| 3906 | } |
| 3907 | impl StringFeedbackState { |
| 3908 | /// Get the value of the `num_keysyms` field. |
| 3909 | /// |
| 3910 | /// The `num_keysyms` field is used as the length field of the `keysyms` field. |
| 3911 | /// This function computes the field's value again based on the length of the list. |
| 3912 | /// |
| 3913 | /// # Panics |
| 3914 | /// |
| 3915 | /// Panics if the value cannot be represented in the target type. This |
| 3916 | /// cannot happen with values of the struct received from the X11 server. |
| 3917 | pub fn num_keysyms(&self) -> u16 { |
| 3918 | self.keysyms.len() |
| 3919 | .try_into().unwrap() |
| 3920 | } |
| 3921 | } |
| 3922 | |
| 3923 | #[derive (Clone, Copy, Default)] |
| 3924 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 3925 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 3926 | pub struct BellFeedbackState { |
| 3927 | pub class_id: FeedbackClass, |
| 3928 | pub feedback_id: u8, |
| 3929 | pub len: u16, |
| 3930 | pub percent: u8, |
| 3931 | pub pitch: u16, |
| 3932 | pub duration: u16, |
| 3933 | } |
| 3934 | impl_debug_if_no_extra_traits!(BellFeedbackState, "BellFeedbackState" ); |
| 3935 | impl TryParse for BellFeedbackState { |
| 3936 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 3937 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3938 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3939 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3940 | let (percent: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3941 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
| 3942 | let (pitch: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3943 | let (duration: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 3944 | let class_id: FeedbackClass = class_id.into(); |
| 3945 | let result: BellFeedbackState = BellFeedbackState { class_id, feedback_id, len, percent, pitch, duration }; |
| 3946 | Ok((result, remaining)) |
| 3947 | } |
| 3948 | } |
| 3949 | impl Serialize for BellFeedbackState { |
| 3950 | type Bytes = [u8; 12]; |
| 3951 | fn serialize(&self) -> [u8; 12] { |
| 3952 | let class_id_bytes = u8::from(self.class_id).serialize(); |
| 3953 | let feedback_id_bytes = self.feedback_id.serialize(); |
| 3954 | let len_bytes = self.len.serialize(); |
| 3955 | let percent_bytes = self.percent.serialize(); |
| 3956 | let pitch_bytes = self.pitch.serialize(); |
| 3957 | let duration_bytes = self.duration.serialize(); |
| 3958 | [ |
| 3959 | class_id_bytes[0], |
| 3960 | feedback_id_bytes[0], |
| 3961 | len_bytes[0], |
| 3962 | len_bytes[1], |
| 3963 | percent_bytes[0], |
| 3964 | 0, |
| 3965 | 0, |
| 3966 | 0, |
| 3967 | pitch_bytes[0], |
| 3968 | pitch_bytes[1], |
| 3969 | duration_bytes[0], |
| 3970 | duration_bytes[1], |
| 3971 | ] |
| 3972 | } |
| 3973 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 3974 | bytes.reserve(12); |
| 3975 | u8::from(self.class_id).serialize_into(bytes); |
| 3976 | self.feedback_id.serialize_into(bytes); |
| 3977 | self.len.serialize_into(bytes); |
| 3978 | self.percent.serialize_into(bytes); |
| 3979 | bytes.extend_from_slice(&[0; 3]); |
| 3980 | self.pitch.serialize_into(bytes); |
| 3981 | self.duration.serialize_into(bytes); |
| 3982 | } |
| 3983 | } |
| 3984 | |
| 3985 | #[derive (Clone, Copy, Default)] |
| 3986 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 3987 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 3988 | pub struct LedFeedbackState { |
| 3989 | pub class_id: FeedbackClass, |
| 3990 | pub feedback_id: u8, |
| 3991 | pub len: u16, |
| 3992 | pub led_mask: u32, |
| 3993 | pub led_values: u32, |
| 3994 | } |
| 3995 | impl_debug_if_no_extra_traits!(LedFeedbackState, "LedFeedbackState" ); |
| 3996 | impl TryParse for LedFeedbackState { |
| 3997 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 3998 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 3999 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4000 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4001 | let (led_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 4002 | let (led_values: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 4003 | let class_id: FeedbackClass = class_id.into(); |
| 4004 | let result: LedFeedbackState = LedFeedbackState { class_id, feedback_id, len, led_mask, led_values }; |
| 4005 | Ok((result, remaining)) |
| 4006 | } |
| 4007 | } |
| 4008 | impl Serialize for LedFeedbackState { |
| 4009 | type Bytes = [u8; 12]; |
| 4010 | fn serialize(&self) -> [u8; 12] { |
| 4011 | let class_id_bytes = u8::from(self.class_id).serialize(); |
| 4012 | let feedback_id_bytes = self.feedback_id.serialize(); |
| 4013 | let len_bytes = self.len.serialize(); |
| 4014 | let led_mask_bytes = self.led_mask.serialize(); |
| 4015 | let led_values_bytes = self.led_values.serialize(); |
| 4016 | [ |
| 4017 | class_id_bytes[0], |
| 4018 | feedback_id_bytes[0], |
| 4019 | len_bytes[0], |
| 4020 | len_bytes[1], |
| 4021 | led_mask_bytes[0], |
| 4022 | led_mask_bytes[1], |
| 4023 | led_mask_bytes[2], |
| 4024 | led_mask_bytes[3], |
| 4025 | led_values_bytes[0], |
| 4026 | led_values_bytes[1], |
| 4027 | led_values_bytes[2], |
| 4028 | led_values_bytes[3], |
| 4029 | ] |
| 4030 | } |
| 4031 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4032 | bytes.reserve(12); |
| 4033 | u8::from(self.class_id).serialize_into(bytes); |
| 4034 | self.feedback_id.serialize_into(bytes); |
| 4035 | self.len.serialize_into(bytes); |
| 4036 | self.led_mask.serialize_into(bytes); |
| 4037 | self.led_values.serialize_into(bytes); |
| 4038 | } |
| 4039 | } |
| 4040 | |
| 4041 | #[derive (Clone, Copy)] |
| 4042 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4043 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4044 | pub struct FeedbackStateDataKeyboard { |
| 4045 | pub pitch: u16, |
| 4046 | pub duration: u16, |
| 4047 | pub led_mask: u32, |
| 4048 | pub led_values: u32, |
| 4049 | pub global_auto_repeat: bool, |
| 4050 | pub click: u8, |
| 4051 | pub percent: u8, |
| 4052 | pub auto_repeats: [u8; 32], |
| 4053 | } |
| 4054 | impl_debug_if_no_extra_traits!(FeedbackStateDataKeyboard, "FeedbackStateDataKeyboard" ); |
| 4055 | impl TryParse for FeedbackStateDataKeyboard { |
| 4056 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4057 | let (pitch: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4058 | let (duration: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4059 | let (led_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 4060 | let (led_values: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 4061 | let (global_auto_repeat: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
| 4062 | let (click: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4063 | let (percent: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4064 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 4065 | let (auto_repeats: [u8; 32], remaining: &[u8]) = crate::x11_utils::parse_u8_array::<32>(data:remaining)?; |
| 4066 | let result: FeedbackStateDataKeyboard = FeedbackStateDataKeyboard { pitch, duration, led_mask, led_values, global_auto_repeat, click, percent, auto_repeats }; |
| 4067 | Ok((result, remaining)) |
| 4068 | } |
| 4069 | } |
| 4070 | impl Serialize for FeedbackStateDataKeyboard { |
| 4071 | type Bytes = [u8; 48]; |
| 4072 | fn serialize(&self) -> [u8; 48] { |
| 4073 | let pitch_bytes = self.pitch.serialize(); |
| 4074 | let duration_bytes = self.duration.serialize(); |
| 4075 | let led_mask_bytes = self.led_mask.serialize(); |
| 4076 | let led_values_bytes = self.led_values.serialize(); |
| 4077 | let global_auto_repeat_bytes = self.global_auto_repeat.serialize(); |
| 4078 | let click_bytes = self.click.serialize(); |
| 4079 | let percent_bytes = self.percent.serialize(); |
| 4080 | [ |
| 4081 | pitch_bytes[0], |
| 4082 | pitch_bytes[1], |
| 4083 | duration_bytes[0], |
| 4084 | duration_bytes[1], |
| 4085 | led_mask_bytes[0], |
| 4086 | led_mask_bytes[1], |
| 4087 | led_mask_bytes[2], |
| 4088 | led_mask_bytes[3], |
| 4089 | led_values_bytes[0], |
| 4090 | led_values_bytes[1], |
| 4091 | led_values_bytes[2], |
| 4092 | led_values_bytes[3], |
| 4093 | global_auto_repeat_bytes[0], |
| 4094 | click_bytes[0], |
| 4095 | percent_bytes[0], |
| 4096 | 0, |
| 4097 | self.auto_repeats[0], |
| 4098 | self.auto_repeats[1], |
| 4099 | self.auto_repeats[2], |
| 4100 | self.auto_repeats[3], |
| 4101 | self.auto_repeats[4], |
| 4102 | self.auto_repeats[5], |
| 4103 | self.auto_repeats[6], |
| 4104 | self.auto_repeats[7], |
| 4105 | self.auto_repeats[8], |
| 4106 | self.auto_repeats[9], |
| 4107 | self.auto_repeats[10], |
| 4108 | self.auto_repeats[11], |
| 4109 | self.auto_repeats[12], |
| 4110 | self.auto_repeats[13], |
| 4111 | self.auto_repeats[14], |
| 4112 | self.auto_repeats[15], |
| 4113 | self.auto_repeats[16], |
| 4114 | self.auto_repeats[17], |
| 4115 | self.auto_repeats[18], |
| 4116 | self.auto_repeats[19], |
| 4117 | self.auto_repeats[20], |
| 4118 | self.auto_repeats[21], |
| 4119 | self.auto_repeats[22], |
| 4120 | self.auto_repeats[23], |
| 4121 | self.auto_repeats[24], |
| 4122 | self.auto_repeats[25], |
| 4123 | self.auto_repeats[26], |
| 4124 | self.auto_repeats[27], |
| 4125 | self.auto_repeats[28], |
| 4126 | self.auto_repeats[29], |
| 4127 | self.auto_repeats[30], |
| 4128 | self.auto_repeats[31], |
| 4129 | ] |
| 4130 | } |
| 4131 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4132 | bytes.reserve(48); |
| 4133 | self.pitch.serialize_into(bytes); |
| 4134 | self.duration.serialize_into(bytes); |
| 4135 | self.led_mask.serialize_into(bytes); |
| 4136 | self.led_values.serialize_into(bytes); |
| 4137 | self.global_auto_repeat.serialize_into(bytes); |
| 4138 | self.click.serialize_into(bytes); |
| 4139 | self.percent.serialize_into(bytes); |
| 4140 | bytes.extend_from_slice(&[0; 1]); |
| 4141 | bytes.extend_from_slice(&self.auto_repeats); |
| 4142 | } |
| 4143 | } |
| 4144 | #[derive (Clone, Copy)] |
| 4145 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4146 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4147 | pub struct FeedbackStateDataPointer { |
| 4148 | pub accel_num: u16, |
| 4149 | pub accel_denom: u16, |
| 4150 | pub threshold: u16, |
| 4151 | } |
| 4152 | impl_debug_if_no_extra_traits!(FeedbackStateDataPointer, "FeedbackStateDataPointer" ); |
| 4153 | impl TryParse for FeedbackStateDataPointer { |
| 4154 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4155 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 4156 | let (accel_num: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4157 | let (accel_denom: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4158 | let (threshold: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4159 | let result: FeedbackStateDataPointer = FeedbackStateDataPointer { accel_num, accel_denom, threshold }; |
| 4160 | Ok((result, remaining)) |
| 4161 | } |
| 4162 | } |
| 4163 | impl Serialize for FeedbackStateDataPointer { |
| 4164 | type Bytes = [u8; 8]; |
| 4165 | fn serialize(&self) -> [u8; 8] { |
| 4166 | let accel_num_bytes = self.accel_num.serialize(); |
| 4167 | let accel_denom_bytes = self.accel_denom.serialize(); |
| 4168 | let threshold_bytes = self.threshold.serialize(); |
| 4169 | [ |
| 4170 | 0, |
| 4171 | 0, |
| 4172 | accel_num_bytes[0], |
| 4173 | accel_num_bytes[1], |
| 4174 | accel_denom_bytes[0], |
| 4175 | accel_denom_bytes[1], |
| 4176 | threshold_bytes[0], |
| 4177 | threshold_bytes[1], |
| 4178 | ] |
| 4179 | } |
| 4180 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4181 | bytes.reserve(8); |
| 4182 | bytes.extend_from_slice(&[0; 2]); |
| 4183 | self.accel_num.serialize_into(bytes); |
| 4184 | self.accel_denom.serialize_into(bytes); |
| 4185 | self.threshold.serialize_into(bytes); |
| 4186 | } |
| 4187 | } |
| 4188 | #[derive (Clone)] |
| 4189 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4190 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4191 | pub struct FeedbackStateDataString { |
| 4192 | pub max_symbols: u16, |
| 4193 | pub keysyms: Vec<xproto::Keysym>, |
| 4194 | } |
| 4195 | impl_debug_if_no_extra_traits!(FeedbackStateDataString, "FeedbackStateDataString" ); |
| 4196 | impl TryParse for FeedbackStateDataString { |
| 4197 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4198 | let (max_symbols: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4199 | let (num_keysyms: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4200 | let (keysyms: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<xproto::Keysym>(data:remaining, list_length:num_keysyms.try_to_usize()?)?; |
| 4201 | let result: FeedbackStateDataString = FeedbackStateDataString { max_symbols, keysyms }; |
| 4202 | Ok((result, remaining)) |
| 4203 | } |
| 4204 | } |
| 4205 | impl Serialize for FeedbackStateDataString { |
| 4206 | type Bytes = Vec<u8>; |
| 4207 | fn serialize(&self) -> Vec<u8> { |
| 4208 | let mut result: Vec = Vec::new(); |
| 4209 | self.serialize_into(&mut result); |
| 4210 | result |
| 4211 | } |
| 4212 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4213 | bytes.reserve(additional:4); |
| 4214 | self.max_symbols.serialize_into(bytes); |
| 4215 | let num_keysyms: u16 = u16::try_from(self.keysyms.len()).expect(msg:"`keysyms` has too many elements" ); |
| 4216 | num_keysyms.serialize_into(bytes); |
| 4217 | self.keysyms.serialize_into(bytes); |
| 4218 | } |
| 4219 | } |
| 4220 | impl FeedbackStateDataString { |
| 4221 | /// Get the value of the `num_keysyms` field. |
| 4222 | /// |
| 4223 | /// The `num_keysyms` field is used as the length field of the `keysyms` field. |
| 4224 | /// This function computes the field's value again based on the length of the list. |
| 4225 | /// |
| 4226 | /// # Panics |
| 4227 | /// |
| 4228 | /// Panics if the value cannot be represented in the target type. This |
| 4229 | /// cannot happen with values of the struct received from the X11 server. |
| 4230 | pub fn num_keysyms(&self) -> u16 { |
| 4231 | self.keysyms.len() |
| 4232 | .try_into().unwrap() |
| 4233 | } |
| 4234 | } |
| 4235 | #[derive (Clone, Copy)] |
| 4236 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4237 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4238 | pub struct FeedbackStateDataInteger { |
| 4239 | pub resolution: u32, |
| 4240 | pub min_value: i32, |
| 4241 | pub max_value: i32, |
| 4242 | } |
| 4243 | impl_debug_if_no_extra_traits!(FeedbackStateDataInteger, "FeedbackStateDataInteger" ); |
| 4244 | impl TryParse for FeedbackStateDataInteger { |
| 4245 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4246 | let (resolution: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 4247 | let (min_value: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 4248 | let (max_value: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 4249 | let result: FeedbackStateDataInteger = FeedbackStateDataInteger { resolution, min_value, max_value }; |
| 4250 | Ok((result, remaining)) |
| 4251 | } |
| 4252 | } |
| 4253 | impl Serialize for FeedbackStateDataInteger { |
| 4254 | type Bytes = [u8; 12]; |
| 4255 | fn serialize(&self) -> [u8; 12] { |
| 4256 | let resolution_bytes = self.resolution.serialize(); |
| 4257 | let min_value_bytes = self.min_value.serialize(); |
| 4258 | let max_value_bytes = self.max_value.serialize(); |
| 4259 | [ |
| 4260 | resolution_bytes[0], |
| 4261 | resolution_bytes[1], |
| 4262 | resolution_bytes[2], |
| 4263 | resolution_bytes[3], |
| 4264 | min_value_bytes[0], |
| 4265 | min_value_bytes[1], |
| 4266 | min_value_bytes[2], |
| 4267 | min_value_bytes[3], |
| 4268 | max_value_bytes[0], |
| 4269 | max_value_bytes[1], |
| 4270 | max_value_bytes[2], |
| 4271 | max_value_bytes[3], |
| 4272 | ] |
| 4273 | } |
| 4274 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4275 | bytes.reserve(12); |
| 4276 | self.resolution.serialize_into(bytes); |
| 4277 | self.min_value.serialize_into(bytes); |
| 4278 | self.max_value.serialize_into(bytes); |
| 4279 | } |
| 4280 | } |
| 4281 | #[derive (Clone, Copy)] |
| 4282 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4283 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4284 | pub struct FeedbackStateDataLed { |
| 4285 | pub led_mask: u32, |
| 4286 | pub led_values: u32, |
| 4287 | } |
| 4288 | impl_debug_if_no_extra_traits!(FeedbackStateDataLed, "FeedbackStateDataLed" ); |
| 4289 | impl TryParse for FeedbackStateDataLed { |
| 4290 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4291 | let (led_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 4292 | let (led_values: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 4293 | let result: FeedbackStateDataLed = FeedbackStateDataLed { led_mask, led_values }; |
| 4294 | Ok((result, remaining)) |
| 4295 | } |
| 4296 | } |
| 4297 | impl Serialize for FeedbackStateDataLed { |
| 4298 | type Bytes = [u8; 8]; |
| 4299 | fn serialize(&self) -> [u8; 8] { |
| 4300 | let led_mask_bytes: [u8; 4] = self.led_mask.serialize(); |
| 4301 | let led_values_bytes: [u8; 4] = self.led_values.serialize(); |
| 4302 | [ |
| 4303 | led_mask_bytes[0], |
| 4304 | led_mask_bytes[1], |
| 4305 | led_mask_bytes[2], |
| 4306 | led_mask_bytes[3], |
| 4307 | led_values_bytes[0], |
| 4308 | led_values_bytes[1], |
| 4309 | led_values_bytes[2], |
| 4310 | led_values_bytes[3], |
| 4311 | ] |
| 4312 | } |
| 4313 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4314 | bytes.reserve(additional:8); |
| 4315 | self.led_mask.serialize_into(bytes); |
| 4316 | self.led_values.serialize_into(bytes); |
| 4317 | } |
| 4318 | } |
| 4319 | #[derive (Clone, Copy)] |
| 4320 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4321 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4322 | pub struct FeedbackStateDataBell { |
| 4323 | pub percent: u8, |
| 4324 | pub pitch: u16, |
| 4325 | pub duration: u16, |
| 4326 | } |
| 4327 | impl_debug_if_no_extra_traits!(FeedbackStateDataBell, "FeedbackStateDataBell" ); |
| 4328 | impl TryParse for FeedbackStateDataBell { |
| 4329 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4330 | let (percent: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4331 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
| 4332 | let (pitch: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4333 | let (duration: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4334 | let result: FeedbackStateDataBell = FeedbackStateDataBell { percent, pitch, duration }; |
| 4335 | Ok((result, remaining)) |
| 4336 | } |
| 4337 | } |
| 4338 | impl Serialize for FeedbackStateDataBell { |
| 4339 | type Bytes = [u8; 8]; |
| 4340 | fn serialize(&self) -> [u8; 8] { |
| 4341 | let percent_bytes = self.percent.serialize(); |
| 4342 | let pitch_bytes = self.pitch.serialize(); |
| 4343 | let duration_bytes = self.duration.serialize(); |
| 4344 | [ |
| 4345 | percent_bytes[0], |
| 4346 | 0, |
| 4347 | 0, |
| 4348 | 0, |
| 4349 | pitch_bytes[0], |
| 4350 | pitch_bytes[1], |
| 4351 | duration_bytes[0], |
| 4352 | duration_bytes[1], |
| 4353 | ] |
| 4354 | } |
| 4355 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4356 | bytes.reserve(8); |
| 4357 | self.percent.serialize_into(bytes); |
| 4358 | bytes.extend_from_slice(&[0; 3]); |
| 4359 | self.pitch.serialize_into(bytes); |
| 4360 | self.duration.serialize_into(bytes); |
| 4361 | } |
| 4362 | } |
| 4363 | #[derive (Clone)] |
| 4364 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4365 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4366 | pub enum FeedbackStateData { |
| 4367 | Keyboard(FeedbackStateDataKeyboard), |
| 4368 | Pointer(FeedbackStateDataPointer), |
| 4369 | String(FeedbackStateDataString), |
| 4370 | Integer(FeedbackStateDataInteger), |
| 4371 | Led(FeedbackStateDataLed), |
| 4372 | Bell(FeedbackStateDataBell), |
| 4373 | /// This variant is returned when the server sends a discriminant |
| 4374 | /// value that does not match any of the defined by the protocol. |
| 4375 | /// |
| 4376 | /// Usually, this should be considered a parsing error, but there |
| 4377 | /// are some cases where the server violates the protocol. |
| 4378 | /// |
| 4379 | /// Trying to use `serialize` or `serialize_into` with this variant |
| 4380 | /// will raise a panic. |
| 4381 | InvalidValue(u8), |
| 4382 | } |
| 4383 | impl_debug_if_no_extra_traits!(FeedbackStateData, "FeedbackStateData" ); |
| 4384 | impl FeedbackStateData { |
| 4385 | #[cfg_attr (not(feature = "request-parsing" ), allow(dead_code))] |
| 4386 | fn try_parse(value: &[u8], class_id: u8) -> Result<(Self, &[u8]), ParseError> { |
| 4387 | let switch_expr = u8::from(class_id); |
| 4388 | let mut outer_remaining = value; |
| 4389 | let mut parse_result = None; |
| 4390 | if switch_expr == u8::from(FeedbackClass::KEYBOARD) { |
| 4391 | let (keyboard, new_remaining) = FeedbackStateDataKeyboard::try_parse(outer_remaining)?; |
| 4392 | outer_remaining = new_remaining; |
| 4393 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 4394 | parse_result = Some(FeedbackStateData::Keyboard(keyboard)); |
| 4395 | } |
| 4396 | if switch_expr == u8::from(FeedbackClass::POINTER) { |
| 4397 | let (pointer, new_remaining) = FeedbackStateDataPointer::try_parse(outer_remaining)?; |
| 4398 | outer_remaining = new_remaining; |
| 4399 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 4400 | parse_result = Some(FeedbackStateData::Pointer(pointer)); |
| 4401 | } |
| 4402 | if switch_expr == u8::from(FeedbackClass::STRING) { |
| 4403 | let (string, new_remaining) = FeedbackStateDataString::try_parse(outer_remaining)?; |
| 4404 | outer_remaining = new_remaining; |
| 4405 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 4406 | parse_result = Some(FeedbackStateData::String(string)); |
| 4407 | } |
| 4408 | if switch_expr == u8::from(FeedbackClass::INTEGER) { |
| 4409 | let (integer, new_remaining) = FeedbackStateDataInteger::try_parse(outer_remaining)?; |
| 4410 | outer_remaining = new_remaining; |
| 4411 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 4412 | parse_result = Some(FeedbackStateData::Integer(integer)); |
| 4413 | } |
| 4414 | if switch_expr == u8::from(FeedbackClass::LED) { |
| 4415 | let (led, new_remaining) = FeedbackStateDataLed::try_parse(outer_remaining)?; |
| 4416 | outer_remaining = new_remaining; |
| 4417 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 4418 | parse_result = Some(FeedbackStateData::Led(led)); |
| 4419 | } |
| 4420 | if switch_expr == u8::from(FeedbackClass::BELL) { |
| 4421 | let (bell, new_remaining) = FeedbackStateDataBell::try_parse(outer_remaining)?; |
| 4422 | outer_remaining = new_remaining; |
| 4423 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 4424 | parse_result = Some(FeedbackStateData::Bell(bell)); |
| 4425 | } |
| 4426 | match parse_result { |
| 4427 | None => Ok((FeedbackStateData::InvalidValue(switch_expr), &[])), |
| 4428 | Some(result) => Ok((result, outer_remaining)), |
| 4429 | } |
| 4430 | } |
| 4431 | } |
| 4432 | impl FeedbackStateData { |
| 4433 | pub fn as_keyboard(&self) -> Option<&FeedbackStateDataKeyboard> { |
| 4434 | match self { |
| 4435 | FeedbackStateData::Keyboard(value) => Some(value), |
| 4436 | _ => None, |
| 4437 | } |
| 4438 | } |
| 4439 | pub fn as_pointer(&self) -> Option<&FeedbackStateDataPointer> { |
| 4440 | match self { |
| 4441 | FeedbackStateData::Pointer(value) => Some(value), |
| 4442 | _ => None, |
| 4443 | } |
| 4444 | } |
| 4445 | pub fn as_string(&self) -> Option<&FeedbackStateDataString> { |
| 4446 | match self { |
| 4447 | FeedbackStateData::String(value) => Some(value), |
| 4448 | _ => None, |
| 4449 | } |
| 4450 | } |
| 4451 | pub fn as_integer(&self) -> Option<&FeedbackStateDataInteger> { |
| 4452 | match self { |
| 4453 | FeedbackStateData::Integer(value) => Some(value), |
| 4454 | _ => None, |
| 4455 | } |
| 4456 | } |
| 4457 | pub fn as_led(&self) -> Option<&FeedbackStateDataLed> { |
| 4458 | match self { |
| 4459 | FeedbackStateData::Led(value) => Some(value), |
| 4460 | _ => None, |
| 4461 | } |
| 4462 | } |
| 4463 | pub fn as_bell(&self) -> Option<&FeedbackStateDataBell> { |
| 4464 | match self { |
| 4465 | FeedbackStateData::Bell(value) => Some(value), |
| 4466 | _ => None, |
| 4467 | } |
| 4468 | } |
| 4469 | } |
| 4470 | impl FeedbackStateData { |
| 4471 | #[allow (dead_code)] |
| 4472 | fn serialize(&self, class_id: u8) -> Vec<u8> { |
| 4473 | let mut result: Vec = Vec::new(); |
| 4474 | self.serialize_into(&mut result, class_id:u8::from(class_id)); |
| 4475 | result |
| 4476 | } |
| 4477 | fn serialize_into(&self, bytes: &mut Vec<u8>, class_id: u8) { |
| 4478 | assert_eq!(self.switch_expr(), u8::from(class_id), "switch `data` has an inconsistent discriminant" ); |
| 4479 | match self { |
| 4480 | FeedbackStateData::Keyboard(keyboard: &FeedbackStateDataKeyboard) => keyboard.serialize_into(bytes), |
| 4481 | FeedbackStateData::Pointer(pointer: &FeedbackStateDataPointer) => pointer.serialize_into(bytes), |
| 4482 | FeedbackStateData::String(string: &FeedbackStateDataString) => string.serialize_into(bytes), |
| 4483 | FeedbackStateData::Integer(integer: &FeedbackStateDataInteger) => integer.serialize_into(bytes), |
| 4484 | FeedbackStateData::Led(led: &FeedbackStateDataLed) => led.serialize_into(bytes), |
| 4485 | FeedbackStateData::Bell(bell: &FeedbackStateDataBell) => bell.serialize_into(bytes), |
| 4486 | FeedbackStateData::InvalidValue(_) => panic!("attempted to serialize invalid switch case" ), |
| 4487 | } |
| 4488 | } |
| 4489 | } |
| 4490 | impl FeedbackStateData { |
| 4491 | fn switch_expr(&self) -> u8 { |
| 4492 | match self { |
| 4493 | FeedbackStateData::Keyboard(_) => u8::from(FeedbackClass::KEYBOARD), |
| 4494 | FeedbackStateData::Pointer(_) => u8::from(FeedbackClass::POINTER), |
| 4495 | FeedbackStateData::String(_) => u8::from(FeedbackClass::STRING), |
| 4496 | FeedbackStateData::Integer(_) => u8::from(FeedbackClass::INTEGER), |
| 4497 | FeedbackStateData::Led(_) => u8::from(FeedbackClass::LED), |
| 4498 | FeedbackStateData::Bell(_) => u8::from(FeedbackClass::BELL), |
| 4499 | FeedbackStateData::InvalidValue(switch_expr: &u8) => *switch_expr, |
| 4500 | } |
| 4501 | } |
| 4502 | } |
| 4503 | |
| 4504 | #[derive (Clone)] |
| 4505 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4506 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4507 | pub struct FeedbackState { |
| 4508 | pub feedback_id: u8, |
| 4509 | pub len: u16, |
| 4510 | pub data: FeedbackStateData, |
| 4511 | } |
| 4512 | impl_debug_if_no_extra_traits!(FeedbackState, "FeedbackState" ); |
| 4513 | impl TryParse for FeedbackState { |
| 4514 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4515 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4516 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4517 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4518 | let (data: FeedbackStateData, remaining: &[u8]) = FeedbackStateData::try_parse(value:remaining, class_id:u8::from(class_id))?; |
| 4519 | let result: FeedbackState = FeedbackState { feedback_id, len, data }; |
| 4520 | Ok((result, remaining)) |
| 4521 | } |
| 4522 | } |
| 4523 | impl Serialize for FeedbackState { |
| 4524 | type Bytes = Vec<u8>; |
| 4525 | fn serialize(&self) -> Vec<u8> { |
| 4526 | let mut result: Vec = Vec::new(); |
| 4527 | self.serialize_into(&mut result); |
| 4528 | result |
| 4529 | } |
| 4530 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4531 | bytes.reserve(additional:4); |
| 4532 | let class_id: u8 = self.data.switch_expr(); |
| 4533 | class_id.serialize_into(bytes); |
| 4534 | self.feedback_id.serialize_into(bytes); |
| 4535 | self.len.serialize_into(bytes); |
| 4536 | self.data.serialize_into(bytes, class_id:u8::from(class_id)); |
| 4537 | } |
| 4538 | } |
| 4539 | |
| 4540 | /// Opcode for the GetFeedbackControl request |
| 4541 | pub const GET_FEEDBACK_CONTROL_REQUEST: u8 = 22; |
| 4542 | #[derive (Clone, Copy, Default)] |
| 4543 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4544 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4545 | pub struct GetFeedbackControlRequest { |
| 4546 | pub device_id: u8, |
| 4547 | } |
| 4548 | impl_debug_if_no_extra_traits!(GetFeedbackControlRequest, "GetFeedbackControlRequest" ); |
| 4549 | impl GetFeedbackControlRequest { |
| 4550 | /// Serialize this request into bytes for the provided connection |
| 4551 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 4552 | let length_so_far = 0; |
| 4553 | let device_id_bytes = self.device_id.serialize(); |
| 4554 | let mut request0 = vec![ |
| 4555 | major_opcode, |
| 4556 | GET_FEEDBACK_CONTROL_REQUEST, |
| 4557 | 0, |
| 4558 | 0, |
| 4559 | device_id_bytes[0], |
| 4560 | 0, |
| 4561 | 0, |
| 4562 | 0, |
| 4563 | ]; |
| 4564 | let length_so_far = length_so_far + request0.len(); |
| 4565 | assert_eq!(length_so_far % 4, 0); |
| 4566 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 4567 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 4568 | ([request0.into()], vec![]) |
| 4569 | } |
| 4570 | /// Parse this request given its header, its body, and any fds that go along with it |
| 4571 | #[cfg (feature = "request-parsing" )] |
| 4572 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 4573 | if header.minor_opcode != GET_FEEDBACK_CONTROL_REQUEST { |
| 4574 | return Err(ParseError::InvalidValue); |
| 4575 | } |
| 4576 | let (device_id, remaining) = u8::try_parse(value)?; |
| 4577 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 4578 | let _ = remaining; |
| 4579 | Ok(GetFeedbackControlRequest { |
| 4580 | device_id, |
| 4581 | }) |
| 4582 | } |
| 4583 | } |
| 4584 | impl Request for GetFeedbackControlRequest { |
| 4585 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 4586 | |
| 4587 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 4588 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 4589 | // Flatten the buffers into a single vector |
| 4590 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 4591 | (buf, fds) |
| 4592 | } |
| 4593 | } |
| 4594 | impl crate::x11_utils::ReplyRequest for GetFeedbackControlRequest { |
| 4595 | type Reply = GetFeedbackControlReply; |
| 4596 | } |
| 4597 | |
| 4598 | #[derive (Clone)] |
| 4599 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4600 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4601 | pub struct GetFeedbackControlReply { |
| 4602 | pub xi_reply_type: u8, |
| 4603 | pub sequence: u16, |
| 4604 | pub length: u32, |
| 4605 | pub feedbacks: Vec<FeedbackState>, |
| 4606 | } |
| 4607 | impl_debug_if_no_extra_traits!(GetFeedbackControlReply, "GetFeedbackControlReply" ); |
| 4608 | impl TryParse for GetFeedbackControlReply { |
| 4609 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4610 | let remaining: &[u8] = initial_value; |
| 4611 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4612 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4613 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4614 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 4615 | let (num_feedbacks: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4616 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
| 4617 | let (feedbacks: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<FeedbackState>(data:remaining, list_length:num_feedbacks.try_to_usize()?)?; |
| 4618 | if response_type != 1 { |
| 4619 | return Err(ParseError::InvalidValue); |
| 4620 | } |
| 4621 | let result: GetFeedbackControlReply = GetFeedbackControlReply { xi_reply_type, sequence, length, feedbacks }; |
| 4622 | let _ = remaining; |
| 4623 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 4624 | .ok_or(err:ParseError::InsufficientData)?; |
| 4625 | Ok((result, remaining)) |
| 4626 | } |
| 4627 | } |
| 4628 | impl Serialize for GetFeedbackControlReply { |
| 4629 | type Bytes = Vec<u8>; |
| 4630 | fn serialize(&self) -> Vec<u8> { |
| 4631 | let mut result: Vec = Vec::new(); |
| 4632 | self.serialize_into(&mut result); |
| 4633 | result |
| 4634 | } |
| 4635 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4636 | bytes.reserve(additional:32); |
| 4637 | let response_type_bytes: &[u8; 1] = &[1]; |
| 4638 | bytes.push(response_type_bytes[0]); |
| 4639 | self.xi_reply_type.serialize_into(bytes); |
| 4640 | self.sequence.serialize_into(bytes); |
| 4641 | self.length.serialize_into(bytes); |
| 4642 | let num_feedbacks: u16 = u16::try_from(self.feedbacks.len()).expect(msg:"`feedbacks` has too many elements" ); |
| 4643 | num_feedbacks.serialize_into(bytes); |
| 4644 | bytes.extend_from_slice(&[0; 22]); |
| 4645 | self.feedbacks.serialize_into(bytes); |
| 4646 | } |
| 4647 | } |
| 4648 | impl GetFeedbackControlReply { |
| 4649 | /// Get the value of the `num_feedbacks` field. |
| 4650 | /// |
| 4651 | /// The `num_feedbacks` field is used as the length field of the `feedbacks` field. |
| 4652 | /// This function computes the field's value again based on the length of the list. |
| 4653 | /// |
| 4654 | /// # Panics |
| 4655 | /// |
| 4656 | /// Panics if the value cannot be represented in the target type. This |
| 4657 | /// cannot happen with values of the struct received from the X11 server. |
| 4658 | pub fn num_feedbacks(&self) -> u16 { |
| 4659 | self.feedbacks.len() |
| 4660 | .try_into().unwrap() |
| 4661 | } |
| 4662 | } |
| 4663 | |
| 4664 | #[derive (Clone, Copy, Default)] |
| 4665 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4666 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4667 | pub struct KbdFeedbackCtl { |
| 4668 | pub class_id: FeedbackClass, |
| 4669 | pub feedback_id: u8, |
| 4670 | pub len: u16, |
| 4671 | pub key: KeyCode, |
| 4672 | pub auto_repeat_mode: u8, |
| 4673 | pub key_click_percent: i8, |
| 4674 | pub bell_percent: i8, |
| 4675 | pub bell_pitch: i16, |
| 4676 | pub bell_duration: i16, |
| 4677 | pub led_mask: u32, |
| 4678 | pub led_values: u32, |
| 4679 | } |
| 4680 | impl_debug_if_no_extra_traits!(KbdFeedbackCtl, "KbdFeedbackCtl" ); |
| 4681 | impl TryParse for KbdFeedbackCtl { |
| 4682 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4683 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4684 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4685 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4686 | let (key: u8, remaining: &[u8]) = KeyCode::try_parse(remaining)?; |
| 4687 | let (auto_repeat_mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4688 | let (key_click_percent: i8, remaining: &[u8]) = i8::try_parse(remaining)?; |
| 4689 | let (bell_percent: i8, remaining: &[u8]) = i8::try_parse(remaining)?; |
| 4690 | let (bell_pitch: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 4691 | let (bell_duration: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 4692 | let (led_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 4693 | let (led_values: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 4694 | let class_id: FeedbackClass = class_id.into(); |
| 4695 | let result: KbdFeedbackCtl = KbdFeedbackCtl { class_id, feedback_id, len, key, auto_repeat_mode, key_click_percent, bell_percent, bell_pitch, bell_duration, led_mask, led_values }; |
| 4696 | Ok((result, remaining)) |
| 4697 | } |
| 4698 | } |
| 4699 | impl Serialize for KbdFeedbackCtl { |
| 4700 | type Bytes = [u8; 20]; |
| 4701 | fn serialize(&self) -> [u8; 20] { |
| 4702 | let class_id_bytes = u8::from(self.class_id).serialize(); |
| 4703 | let feedback_id_bytes = self.feedback_id.serialize(); |
| 4704 | let len_bytes = self.len.serialize(); |
| 4705 | let key_bytes = self.key.serialize(); |
| 4706 | let auto_repeat_mode_bytes = self.auto_repeat_mode.serialize(); |
| 4707 | let key_click_percent_bytes = self.key_click_percent.serialize(); |
| 4708 | let bell_percent_bytes = self.bell_percent.serialize(); |
| 4709 | let bell_pitch_bytes = self.bell_pitch.serialize(); |
| 4710 | let bell_duration_bytes = self.bell_duration.serialize(); |
| 4711 | let led_mask_bytes = self.led_mask.serialize(); |
| 4712 | let led_values_bytes = self.led_values.serialize(); |
| 4713 | [ |
| 4714 | class_id_bytes[0], |
| 4715 | feedback_id_bytes[0], |
| 4716 | len_bytes[0], |
| 4717 | len_bytes[1], |
| 4718 | key_bytes[0], |
| 4719 | auto_repeat_mode_bytes[0], |
| 4720 | key_click_percent_bytes[0], |
| 4721 | bell_percent_bytes[0], |
| 4722 | bell_pitch_bytes[0], |
| 4723 | bell_pitch_bytes[1], |
| 4724 | bell_duration_bytes[0], |
| 4725 | bell_duration_bytes[1], |
| 4726 | led_mask_bytes[0], |
| 4727 | led_mask_bytes[1], |
| 4728 | led_mask_bytes[2], |
| 4729 | led_mask_bytes[3], |
| 4730 | led_values_bytes[0], |
| 4731 | led_values_bytes[1], |
| 4732 | led_values_bytes[2], |
| 4733 | led_values_bytes[3], |
| 4734 | ] |
| 4735 | } |
| 4736 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4737 | bytes.reserve(20); |
| 4738 | u8::from(self.class_id).serialize_into(bytes); |
| 4739 | self.feedback_id.serialize_into(bytes); |
| 4740 | self.len.serialize_into(bytes); |
| 4741 | self.key.serialize_into(bytes); |
| 4742 | self.auto_repeat_mode.serialize_into(bytes); |
| 4743 | self.key_click_percent.serialize_into(bytes); |
| 4744 | self.bell_percent.serialize_into(bytes); |
| 4745 | self.bell_pitch.serialize_into(bytes); |
| 4746 | self.bell_duration.serialize_into(bytes); |
| 4747 | self.led_mask.serialize_into(bytes); |
| 4748 | self.led_values.serialize_into(bytes); |
| 4749 | } |
| 4750 | } |
| 4751 | |
| 4752 | #[derive (Clone, Copy, Default)] |
| 4753 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4754 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4755 | pub struct PtrFeedbackCtl { |
| 4756 | pub class_id: FeedbackClass, |
| 4757 | pub feedback_id: u8, |
| 4758 | pub len: u16, |
| 4759 | pub num: i16, |
| 4760 | pub denom: i16, |
| 4761 | pub threshold: i16, |
| 4762 | } |
| 4763 | impl_debug_if_no_extra_traits!(PtrFeedbackCtl, "PtrFeedbackCtl" ); |
| 4764 | impl TryParse for PtrFeedbackCtl { |
| 4765 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4766 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4767 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4768 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4769 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 4770 | let (num: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 4771 | let (denom: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 4772 | let (threshold: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 4773 | let class_id: FeedbackClass = class_id.into(); |
| 4774 | let result: PtrFeedbackCtl = PtrFeedbackCtl { class_id, feedback_id, len, num, denom, threshold }; |
| 4775 | Ok((result, remaining)) |
| 4776 | } |
| 4777 | } |
| 4778 | impl Serialize for PtrFeedbackCtl { |
| 4779 | type Bytes = [u8; 12]; |
| 4780 | fn serialize(&self) -> [u8; 12] { |
| 4781 | let class_id_bytes = u8::from(self.class_id).serialize(); |
| 4782 | let feedback_id_bytes = self.feedback_id.serialize(); |
| 4783 | let len_bytes = self.len.serialize(); |
| 4784 | let num_bytes = self.num.serialize(); |
| 4785 | let denom_bytes = self.denom.serialize(); |
| 4786 | let threshold_bytes = self.threshold.serialize(); |
| 4787 | [ |
| 4788 | class_id_bytes[0], |
| 4789 | feedback_id_bytes[0], |
| 4790 | len_bytes[0], |
| 4791 | len_bytes[1], |
| 4792 | 0, |
| 4793 | 0, |
| 4794 | num_bytes[0], |
| 4795 | num_bytes[1], |
| 4796 | denom_bytes[0], |
| 4797 | denom_bytes[1], |
| 4798 | threshold_bytes[0], |
| 4799 | threshold_bytes[1], |
| 4800 | ] |
| 4801 | } |
| 4802 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4803 | bytes.reserve(12); |
| 4804 | u8::from(self.class_id).serialize_into(bytes); |
| 4805 | self.feedback_id.serialize_into(bytes); |
| 4806 | self.len.serialize_into(bytes); |
| 4807 | bytes.extend_from_slice(&[0; 2]); |
| 4808 | self.num.serialize_into(bytes); |
| 4809 | self.denom.serialize_into(bytes); |
| 4810 | self.threshold.serialize_into(bytes); |
| 4811 | } |
| 4812 | } |
| 4813 | |
| 4814 | #[derive (Clone, Copy, Default)] |
| 4815 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4816 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4817 | pub struct IntegerFeedbackCtl { |
| 4818 | pub class_id: FeedbackClass, |
| 4819 | pub feedback_id: u8, |
| 4820 | pub len: u16, |
| 4821 | pub int_to_display: i32, |
| 4822 | } |
| 4823 | impl_debug_if_no_extra_traits!(IntegerFeedbackCtl, "IntegerFeedbackCtl" ); |
| 4824 | impl TryParse for IntegerFeedbackCtl { |
| 4825 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4826 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4827 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4828 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4829 | let (int_to_display: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 4830 | let class_id: FeedbackClass = class_id.into(); |
| 4831 | let result: IntegerFeedbackCtl = IntegerFeedbackCtl { class_id, feedback_id, len, int_to_display }; |
| 4832 | Ok((result, remaining)) |
| 4833 | } |
| 4834 | } |
| 4835 | impl Serialize for IntegerFeedbackCtl { |
| 4836 | type Bytes = [u8; 8]; |
| 4837 | fn serialize(&self) -> [u8; 8] { |
| 4838 | let class_id_bytes = u8::from(self.class_id).serialize(); |
| 4839 | let feedback_id_bytes = self.feedback_id.serialize(); |
| 4840 | let len_bytes = self.len.serialize(); |
| 4841 | let int_to_display_bytes = self.int_to_display.serialize(); |
| 4842 | [ |
| 4843 | class_id_bytes[0], |
| 4844 | feedback_id_bytes[0], |
| 4845 | len_bytes[0], |
| 4846 | len_bytes[1], |
| 4847 | int_to_display_bytes[0], |
| 4848 | int_to_display_bytes[1], |
| 4849 | int_to_display_bytes[2], |
| 4850 | int_to_display_bytes[3], |
| 4851 | ] |
| 4852 | } |
| 4853 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4854 | bytes.reserve(8); |
| 4855 | u8::from(self.class_id).serialize_into(bytes); |
| 4856 | self.feedback_id.serialize_into(bytes); |
| 4857 | self.len.serialize_into(bytes); |
| 4858 | self.int_to_display.serialize_into(bytes); |
| 4859 | } |
| 4860 | } |
| 4861 | |
| 4862 | #[derive (Clone, Default)] |
| 4863 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4864 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4865 | pub struct StringFeedbackCtl { |
| 4866 | pub class_id: FeedbackClass, |
| 4867 | pub feedback_id: u8, |
| 4868 | pub len: u16, |
| 4869 | pub keysyms: Vec<xproto::Keysym>, |
| 4870 | } |
| 4871 | impl_debug_if_no_extra_traits!(StringFeedbackCtl, "StringFeedbackCtl" ); |
| 4872 | impl TryParse for StringFeedbackCtl { |
| 4873 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4874 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4875 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4876 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4877 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 4878 | let (num_keysyms: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4879 | let (keysyms: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<xproto::Keysym>(data:remaining, list_length:num_keysyms.try_to_usize()?)?; |
| 4880 | let class_id: FeedbackClass = class_id.into(); |
| 4881 | let result: StringFeedbackCtl = StringFeedbackCtl { class_id, feedback_id, len, keysyms }; |
| 4882 | Ok((result, remaining)) |
| 4883 | } |
| 4884 | } |
| 4885 | impl Serialize for StringFeedbackCtl { |
| 4886 | type Bytes = Vec<u8>; |
| 4887 | fn serialize(&self) -> Vec<u8> { |
| 4888 | let mut result: Vec = Vec::new(); |
| 4889 | self.serialize_into(&mut result); |
| 4890 | result |
| 4891 | } |
| 4892 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4893 | bytes.reserve(additional:8); |
| 4894 | u8::from(self.class_id).serialize_into(bytes); |
| 4895 | self.feedback_id.serialize_into(bytes); |
| 4896 | self.len.serialize_into(bytes); |
| 4897 | bytes.extend_from_slice(&[0; 2]); |
| 4898 | let num_keysyms: u16 = u16::try_from(self.keysyms.len()).expect(msg:"`keysyms` has too many elements" ); |
| 4899 | num_keysyms.serialize_into(bytes); |
| 4900 | self.keysyms.serialize_into(bytes); |
| 4901 | } |
| 4902 | } |
| 4903 | impl StringFeedbackCtl { |
| 4904 | /// Get the value of the `num_keysyms` field. |
| 4905 | /// |
| 4906 | /// The `num_keysyms` field is used as the length field of the `keysyms` field. |
| 4907 | /// This function computes the field's value again based on the length of the list. |
| 4908 | /// |
| 4909 | /// # Panics |
| 4910 | /// |
| 4911 | /// Panics if the value cannot be represented in the target type. This |
| 4912 | /// cannot happen with values of the struct received from the X11 server. |
| 4913 | pub fn num_keysyms(&self) -> u16 { |
| 4914 | self.keysyms.len() |
| 4915 | .try_into().unwrap() |
| 4916 | } |
| 4917 | } |
| 4918 | |
| 4919 | #[derive (Clone, Copy, Default)] |
| 4920 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4921 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4922 | pub struct BellFeedbackCtl { |
| 4923 | pub class_id: FeedbackClass, |
| 4924 | pub feedback_id: u8, |
| 4925 | pub len: u16, |
| 4926 | pub percent: i8, |
| 4927 | pub pitch: i16, |
| 4928 | pub duration: i16, |
| 4929 | } |
| 4930 | impl_debug_if_no_extra_traits!(BellFeedbackCtl, "BellFeedbackCtl" ); |
| 4931 | impl TryParse for BellFeedbackCtl { |
| 4932 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4933 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4934 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4935 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4936 | let (percent: i8, remaining: &[u8]) = i8::try_parse(remaining)?; |
| 4937 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
| 4938 | let (pitch: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 4939 | let (duration: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 4940 | let class_id: FeedbackClass = class_id.into(); |
| 4941 | let result: BellFeedbackCtl = BellFeedbackCtl { class_id, feedback_id, len, percent, pitch, duration }; |
| 4942 | Ok((result, remaining)) |
| 4943 | } |
| 4944 | } |
| 4945 | impl Serialize for BellFeedbackCtl { |
| 4946 | type Bytes = [u8; 12]; |
| 4947 | fn serialize(&self) -> [u8; 12] { |
| 4948 | let class_id_bytes = u8::from(self.class_id).serialize(); |
| 4949 | let feedback_id_bytes = self.feedback_id.serialize(); |
| 4950 | let len_bytes = self.len.serialize(); |
| 4951 | let percent_bytes = self.percent.serialize(); |
| 4952 | let pitch_bytes = self.pitch.serialize(); |
| 4953 | let duration_bytes = self.duration.serialize(); |
| 4954 | [ |
| 4955 | class_id_bytes[0], |
| 4956 | feedback_id_bytes[0], |
| 4957 | len_bytes[0], |
| 4958 | len_bytes[1], |
| 4959 | percent_bytes[0], |
| 4960 | 0, |
| 4961 | 0, |
| 4962 | 0, |
| 4963 | pitch_bytes[0], |
| 4964 | pitch_bytes[1], |
| 4965 | duration_bytes[0], |
| 4966 | duration_bytes[1], |
| 4967 | ] |
| 4968 | } |
| 4969 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 4970 | bytes.reserve(12); |
| 4971 | u8::from(self.class_id).serialize_into(bytes); |
| 4972 | self.feedback_id.serialize_into(bytes); |
| 4973 | self.len.serialize_into(bytes); |
| 4974 | self.percent.serialize_into(bytes); |
| 4975 | bytes.extend_from_slice(&[0; 3]); |
| 4976 | self.pitch.serialize_into(bytes); |
| 4977 | self.duration.serialize_into(bytes); |
| 4978 | } |
| 4979 | } |
| 4980 | |
| 4981 | #[derive (Clone, Copy, Default)] |
| 4982 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 4983 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 4984 | pub struct LedFeedbackCtl { |
| 4985 | pub class_id: FeedbackClass, |
| 4986 | pub feedback_id: u8, |
| 4987 | pub len: u16, |
| 4988 | pub led_mask: u32, |
| 4989 | pub led_values: u32, |
| 4990 | } |
| 4991 | impl_debug_if_no_extra_traits!(LedFeedbackCtl, "LedFeedbackCtl" ); |
| 4992 | impl TryParse for LedFeedbackCtl { |
| 4993 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 4994 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4995 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 4996 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 4997 | let (led_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 4998 | let (led_values: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 4999 | let class_id: FeedbackClass = class_id.into(); |
| 5000 | let result: LedFeedbackCtl = LedFeedbackCtl { class_id, feedback_id, len, led_mask, led_values }; |
| 5001 | Ok((result, remaining)) |
| 5002 | } |
| 5003 | } |
| 5004 | impl Serialize for LedFeedbackCtl { |
| 5005 | type Bytes = [u8; 12]; |
| 5006 | fn serialize(&self) -> [u8; 12] { |
| 5007 | let class_id_bytes = u8::from(self.class_id).serialize(); |
| 5008 | let feedback_id_bytes = self.feedback_id.serialize(); |
| 5009 | let len_bytes = self.len.serialize(); |
| 5010 | let led_mask_bytes = self.led_mask.serialize(); |
| 5011 | let led_values_bytes = self.led_values.serialize(); |
| 5012 | [ |
| 5013 | class_id_bytes[0], |
| 5014 | feedback_id_bytes[0], |
| 5015 | len_bytes[0], |
| 5016 | len_bytes[1], |
| 5017 | led_mask_bytes[0], |
| 5018 | led_mask_bytes[1], |
| 5019 | led_mask_bytes[2], |
| 5020 | led_mask_bytes[3], |
| 5021 | led_values_bytes[0], |
| 5022 | led_values_bytes[1], |
| 5023 | led_values_bytes[2], |
| 5024 | led_values_bytes[3], |
| 5025 | ] |
| 5026 | } |
| 5027 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 5028 | bytes.reserve(12); |
| 5029 | u8::from(self.class_id).serialize_into(bytes); |
| 5030 | self.feedback_id.serialize_into(bytes); |
| 5031 | self.len.serialize_into(bytes); |
| 5032 | self.led_mask.serialize_into(bytes); |
| 5033 | self.led_values.serialize_into(bytes); |
| 5034 | } |
| 5035 | } |
| 5036 | |
| 5037 | #[derive (Clone, Copy)] |
| 5038 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5039 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5040 | pub struct FeedbackCtlDataKeyboard { |
| 5041 | pub key: KeyCode, |
| 5042 | pub auto_repeat_mode: u8, |
| 5043 | pub key_click_percent: i8, |
| 5044 | pub bell_percent: i8, |
| 5045 | pub bell_pitch: i16, |
| 5046 | pub bell_duration: i16, |
| 5047 | pub led_mask: u32, |
| 5048 | pub led_values: u32, |
| 5049 | } |
| 5050 | impl_debug_if_no_extra_traits!(FeedbackCtlDataKeyboard, "FeedbackCtlDataKeyboard" ); |
| 5051 | impl TryParse for FeedbackCtlDataKeyboard { |
| 5052 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 5053 | let (key: u8, remaining: &[u8]) = KeyCode::try_parse(remaining)?; |
| 5054 | let (auto_repeat_mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 5055 | let (key_click_percent: i8, remaining: &[u8]) = i8::try_parse(remaining)?; |
| 5056 | let (bell_percent: i8, remaining: &[u8]) = i8::try_parse(remaining)?; |
| 5057 | let (bell_pitch: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 5058 | let (bell_duration: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 5059 | let (led_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 5060 | let (led_values: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 5061 | let result: FeedbackCtlDataKeyboard = FeedbackCtlDataKeyboard { key, auto_repeat_mode, key_click_percent, bell_percent, bell_pitch, bell_duration, led_mask, led_values }; |
| 5062 | Ok((result, remaining)) |
| 5063 | } |
| 5064 | } |
| 5065 | impl Serialize for FeedbackCtlDataKeyboard { |
| 5066 | type Bytes = [u8; 16]; |
| 5067 | fn serialize(&self) -> [u8; 16] { |
| 5068 | let key_bytes = self.key.serialize(); |
| 5069 | let auto_repeat_mode_bytes = self.auto_repeat_mode.serialize(); |
| 5070 | let key_click_percent_bytes = self.key_click_percent.serialize(); |
| 5071 | let bell_percent_bytes = self.bell_percent.serialize(); |
| 5072 | let bell_pitch_bytes = self.bell_pitch.serialize(); |
| 5073 | let bell_duration_bytes = self.bell_duration.serialize(); |
| 5074 | let led_mask_bytes = self.led_mask.serialize(); |
| 5075 | let led_values_bytes = self.led_values.serialize(); |
| 5076 | [ |
| 5077 | key_bytes[0], |
| 5078 | auto_repeat_mode_bytes[0], |
| 5079 | key_click_percent_bytes[0], |
| 5080 | bell_percent_bytes[0], |
| 5081 | bell_pitch_bytes[0], |
| 5082 | bell_pitch_bytes[1], |
| 5083 | bell_duration_bytes[0], |
| 5084 | bell_duration_bytes[1], |
| 5085 | led_mask_bytes[0], |
| 5086 | led_mask_bytes[1], |
| 5087 | led_mask_bytes[2], |
| 5088 | led_mask_bytes[3], |
| 5089 | led_values_bytes[0], |
| 5090 | led_values_bytes[1], |
| 5091 | led_values_bytes[2], |
| 5092 | led_values_bytes[3], |
| 5093 | ] |
| 5094 | } |
| 5095 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 5096 | bytes.reserve(16); |
| 5097 | self.key.serialize_into(bytes); |
| 5098 | self.auto_repeat_mode.serialize_into(bytes); |
| 5099 | self.key_click_percent.serialize_into(bytes); |
| 5100 | self.bell_percent.serialize_into(bytes); |
| 5101 | self.bell_pitch.serialize_into(bytes); |
| 5102 | self.bell_duration.serialize_into(bytes); |
| 5103 | self.led_mask.serialize_into(bytes); |
| 5104 | self.led_values.serialize_into(bytes); |
| 5105 | } |
| 5106 | } |
| 5107 | #[derive (Clone, Copy)] |
| 5108 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5109 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5110 | pub struct FeedbackCtlDataPointer { |
| 5111 | pub num: i16, |
| 5112 | pub denom: i16, |
| 5113 | pub threshold: i16, |
| 5114 | } |
| 5115 | impl_debug_if_no_extra_traits!(FeedbackCtlDataPointer, "FeedbackCtlDataPointer" ); |
| 5116 | impl TryParse for FeedbackCtlDataPointer { |
| 5117 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 5118 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 5119 | let (num: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 5120 | let (denom: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 5121 | let (threshold: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 5122 | let result: FeedbackCtlDataPointer = FeedbackCtlDataPointer { num, denom, threshold }; |
| 5123 | Ok((result, remaining)) |
| 5124 | } |
| 5125 | } |
| 5126 | impl Serialize for FeedbackCtlDataPointer { |
| 5127 | type Bytes = [u8; 8]; |
| 5128 | fn serialize(&self) -> [u8; 8] { |
| 5129 | let num_bytes = self.num.serialize(); |
| 5130 | let denom_bytes = self.denom.serialize(); |
| 5131 | let threshold_bytes = self.threshold.serialize(); |
| 5132 | [ |
| 5133 | 0, |
| 5134 | 0, |
| 5135 | num_bytes[0], |
| 5136 | num_bytes[1], |
| 5137 | denom_bytes[0], |
| 5138 | denom_bytes[1], |
| 5139 | threshold_bytes[0], |
| 5140 | threshold_bytes[1], |
| 5141 | ] |
| 5142 | } |
| 5143 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 5144 | bytes.reserve(8); |
| 5145 | bytes.extend_from_slice(&[0; 2]); |
| 5146 | self.num.serialize_into(bytes); |
| 5147 | self.denom.serialize_into(bytes); |
| 5148 | self.threshold.serialize_into(bytes); |
| 5149 | } |
| 5150 | } |
| 5151 | #[derive (Clone)] |
| 5152 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5153 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5154 | pub struct FeedbackCtlDataString { |
| 5155 | pub keysyms: Vec<xproto::Keysym>, |
| 5156 | } |
| 5157 | impl_debug_if_no_extra_traits!(FeedbackCtlDataString, "FeedbackCtlDataString" ); |
| 5158 | impl TryParse for FeedbackCtlDataString { |
| 5159 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 5160 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 5161 | let (num_keysyms: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 5162 | let (keysyms: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<xproto::Keysym>(data:remaining, list_length:num_keysyms.try_to_usize()?)?; |
| 5163 | let result: FeedbackCtlDataString = FeedbackCtlDataString { keysyms }; |
| 5164 | Ok((result, remaining)) |
| 5165 | } |
| 5166 | } |
| 5167 | impl Serialize for FeedbackCtlDataString { |
| 5168 | type Bytes = Vec<u8>; |
| 5169 | fn serialize(&self) -> Vec<u8> { |
| 5170 | let mut result: Vec = Vec::new(); |
| 5171 | self.serialize_into(&mut result); |
| 5172 | result |
| 5173 | } |
| 5174 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 5175 | bytes.reserve(additional:4); |
| 5176 | bytes.extend_from_slice(&[0; 2]); |
| 5177 | let num_keysyms: u16 = u16::try_from(self.keysyms.len()).expect(msg:"`keysyms` has too many elements" ); |
| 5178 | num_keysyms.serialize_into(bytes); |
| 5179 | self.keysyms.serialize_into(bytes); |
| 5180 | } |
| 5181 | } |
| 5182 | impl FeedbackCtlDataString { |
| 5183 | /// Get the value of the `num_keysyms` field. |
| 5184 | /// |
| 5185 | /// The `num_keysyms` field is used as the length field of the `keysyms` field. |
| 5186 | /// This function computes the field's value again based on the length of the list. |
| 5187 | /// |
| 5188 | /// # Panics |
| 5189 | /// |
| 5190 | /// Panics if the value cannot be represented in the target type. This |
| 5191 | /// cannot happen with values of the struct received from the X11 server. |
| 5192 | pub fn num_keysyms(&self) -> u16 { |
| 5193 | self.keysyms.len() |
| 5194 | .try_into().unwrap() |
| 5195 | } |
| 5196 | } |
| 5197 | #[derive (Clone, Copy)] |
| 5198 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5199 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5200 | pub struct FeedbackCtlDataInteger { |
| 5201 | pub int_to_display: i32, |
| 5202 | } |
| 5203 | impl_debug_if_no_extra_traits!(FeedbackCtlDataInteger, "FeedbackCtlDataInteger" ); |
| 5204 | impl TryParse for FeedbackCtlDataInteger { |
| 5205 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 5206 | let (int_to_display: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 5207 | let result: FeedbackCtlDataInteger = FeedbackCtlDataInteger { int_to_display }; |
| 5208 | Ok((result, remaining)) |
| 5209 | } |
| 5210 | } |
| 5211 | impl Serialize for FeedbackCtlDataInteger { |
| 5212 | type Bytes = [u8; 4]; |
| 5213 | fn serialize(&self) -> [u8; 4] { |
| 5214 | let int_to_display_bytes: [u8; 4] = self.int_to_display.serialize(); |
| 5215 | [ |
| 5216 | int_to_display_bytes[0], |
| 5217 | int_to_display_bytes[1], |
| 5218 | int_to_display_bytes[2], |
| 5219 | int_to_display_bytes[3], |
| 5220 | ] |
| 5221 | } |
| 5222 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 5223 | bytes.reserve(additional:4); |
| 5224 | self.int_to_display.serialize_into(bytes); |
| 5225 | } |
| 5226 | } |
| 5227 | #[derive (Clone, Copy)] |
| 5228 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5229 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5230 | pub struct FeedbackCtlDataLed { |
| 5231 | pub led_mask: u32, |
| 5232 | pub led_values: u32, |
| 5233 | } |
| 5234 | impl_debug_if_no_extra_traits!(FeedbackCtlDataLed, "FeedbackCtlDataLed" ); |
| 5235 | impl TryParse for FeedbackCtlDataLed { |
| 5236 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 5237 | let (led_mask: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 5238 | let (led_values: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 5239 | let result: FeedbackCtlDataLed = FeedbackCtlDataLed { led_mask, led_values }; |
| 5240 | Ok((result, remaining)) |
| 5241 | } |
| 5242 | } |
| 5243 | impl Serialize for FeedbackCtlDataLed { |
| 5244 | type Bytes = [u8; 8]; |
| 5245 | fn serialize(&self) -> [u8; 8] { |
| 5246 | let led_mask_bytes: [u8; 4] = self.led_mask.serialize(); |
| 5247 | let led_values_bytes: [u8; 4] = self.led_values.serialize(); |
| 5248 | [ |
| 5249 | led_mask_bytes[0], |
| 5250 | led_mask_bytes[1], |
| 5251 | led_mask_bytes[2], |
| 5252 | led_mask_bytes[3], |
| 5253 | led_values_bytes[0], |
| 5254 | led_values_bytes[1], |
| 5255 | led_values_bytes[2], |
| 5256 | led_values_bytes[3], |
| 5257 | ] |
| 5258 | } |
| 5259 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 5260 | bytes.reserve(additional:8); |
| 5261 | self.led_mask.serialize_into(bytes); |
| 5262 | self.led_values.serialize_into(bytes); |
| 5263 | } |
| 5264 | } |
| 5265 | #[derive (Clone, Copy)] |
| 5266 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5267 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5268 | pub struct FeedbackCtlDataBell { |
| 5269 | pub percent: i8, |
| 5270 | pub pitch: i16, |
| 5271 | pub duration: i16, |
| 5272 | } |
| 5273 | impl_debug_if_no_extra_traits!(FeedbackCtlDataBell, "FeedbackCtlDataBell" ); |
| 5274 | impl TryParse for FeedbackCtlDataBell { |
| 5275 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 5276 | let (percent: i8, remaining: &[u8]) = i8::try_parse(remaining)?; |
| 5277 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
| 5278 | let (pitch: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 5279 | let (duration: i16, remaining: &[u8]) = i16::try_parse(remaining)?; |
| 5280 | let result: FeedbackCtlDataBell = FeedbackCtlDataBell { percent, pitch, duration }; |
| 5281 | Ok((result, remaining)) |
| 5282 | } |
| 5283 | } |
| 5284 | impl Serialize for FeedbackCtlDataBell { |
| 5285 | type Bytes = [u8; 8]; |
| 5286 | fn serialize(&self) -> [u8; 8] { |
| 5287 | let percent_bytes = self.percent.serialize(); |
| 5288 | let pitch_bytes = self.pitch.serialize(); |
| 5289 | let duration_bytes = self.duration.serialize(); |
| 5290 | [ |
| 5291 | percent_bytes[0], |
| 5292 | 0, |
| 5293 | 0, |
| 5294 | 0, |
| 5295 | pitch_bytes[0], |
| 5296 | pitch_bytes[1], |
| 5297 | duration_bytes[0], |
| 5298 | duration_bytes[1], |
| 5299 | ] |
| 5300 | } |
| 5301 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 5302 | bytes.reserve(8); |
| 5303 | self.percent.serialize_into(bytes); |
| 5304 | bytes.extend_from_slice(&[0; 3]); |
| 5305 | self.pitch.serialize_into(bytes); |
| 5306 | self.duration.serialize_into(bytes); |
| 5307 | } |
| 5308 | } |
| 5309 | #[derive (Clone)] |
| 5310 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5311 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5312 | pub enum FeedbackCtlData { |
| 5313 | Keyboard(FeedbackCtlDataKeyboard), |
| 5314 | Pointer(FeedbackCtlDataPointer), |
| 5315 | String(FeedbackCtlDataString), |
| 5316 | Integer(FeedbackCtlDataInteger), |
| 5317 | Led(FeedbackCtlDataLed), |
| 5318 | Bell(FeedbackCtlDataBell), |
| 5319 | /// This variant is returned when the server sends a discriminant |
| 5320 | /// value that does not match any of the defined by the protocol. |
| 5321 | /// |
| 5322 | /// Usually, this should be considered a parsing error, but there |
| 5323 | /// are some cases where the server violates the protocol. |
| 5324 | /// |
| 5325 | /// Trying to use `serialize` or `serialize_into` with this variant |
| 5326 | /// will raise a panic. |
| 5327 | InvalidValue(u8), |
| 5328 | } |
| 5329 | impl_debug_if_no_extra_traits!(FeedbackCtlData, "FeedbackCtlData" ); |
| 5330 | impl FeedbackCtlData { |
| 5331 | #[cfg_attr (not(feature = "request-parsing" ), allow(dead_code))] |
| 5332 | fn try_parse(value: &[u8], class_id: u8) -> Result<(Self, &[u8]), ParseError> { |
| 5333 | let switch_expr = u8::from(class_id); |
| 5334 | let mut outer_remaining = value; |
| 5335 | let mut parse_result = None; |
| 5336 | if switch_expr == u8::from(FeedbackClass::KEYBOARD) { |
| 5337 | let (keyboard, new_remaining) = FeedbackCtlDataKeyboard::try_parse(outer_remaining)?; |
| 5338 | outer_remaining = new_remaining; |
| 5339 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 5340 | parse_result = Some(FeedbackCtlData::Keyboard(keyboard)); |
| 5341 | } |
| 5342 | if switch_expr == u8::from(FeedbackClass::POINTER) { |
| 5343 | let (pointer, new_remaining) = FeedbackCtlDataPointer::try_parse(outer_remaining)?; |
| 5344 | outer_remaining = new_remaining; |
| 5345 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 5346 | parse_result = Some(FeedbackCtlData::Pointer(pointer)); |
| 5347 | } |
| 5348 | if switch_expr == u8::from(FeedbackClass::STRING) { |
| 5349 | let (string, new_remaining) = FeedbackCtlDataString::try_parse(outer_remaining)?; |
| 5350 | outer_remaining = new_remaining; |
| 5351 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 5352 | parse_result = Some(FeedbackCtlData::String(string)); |
| 5353 | } |
| 5354 | if switch_expr == u8::from(FeedbackClass::INTEGER) { |
| 5355 | let (integer, new_remaining) = FeedbackCtlDataInteger::try_parse(outer_remaining)?; |
| 5356 | outer_remaining = new_remaining; |
| 5357 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 5358 | parse_result = Some(FeedbackCtlData::Integer(integer)); |
| 5359 | } |
| 5360 | if switch_expr == u8::from(FeedbackClass::LED) { |
| 5361 | let (led, new_remaining) = FeedbackCtlDataLed::try_parse(outer_remaining)?; |
| 5362 | outer_remaining = new_remaining; |
| 5363 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 5364 | parse_result = Some(FeedbackCtlData::Led(led)); |
| 5365 | } |
| 5366 | if switch_expr == u8::from(FeedbackClass::BELL) { |
| 5367 | let (bell, new_remaining) = FeedbackCtlDataBell::try_parse(outer_remaining)?; |
| 5368 | outer_remaining = new_remaining; |
| 5369 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 5370 | parse_result = Some(FeedbackCtlData::Bell(bell)); |
| 5371 | } |
| 5372 | match parse_result { |
| 5373 | None => Ok((FeedbackCtlData::InvalidValue(switch_expr), &[])), |
| 5374 | Some(result) => Ok((result, outer_remaining)), |
| 5375 | } |
| 5376 | } |
| 5377 | } |
| 5378 | impl FeedbackCtlData { |
| 5379 | pub fn as_keyboard(&self) -> Option<&FeedbackCtlDataKeyboard> { |
| 5380 | match self { |
| 5381 | FeedbackCtlData::Keyboard(value) => Some(value), |
| 5382 | _ => None, |
| 5383 | } |
| 5384 | } |
| 5385 | pub fn as_pointer(&self) -> Option<&FeedbackCtlDataPointer> { |
| 5386 | match self { |
| 5387 | FeedbackCtlData::Pointer(value) => Some(value), |
| 5388 | _ => None, |
| 5389 | } |
| 5390 | } |
| 5391 | pub fn as_string(&self) -> Option<&FeedbackCtlDataString> { |
| 5392 | match self { |
| 5393 | FeedbackCtlData::String(value) => Some(value), |
| 5394 | _ => None, |
| 5395 | } |
| 5396 | } |
| 5397 | pub fn as_integer(&self) -> Option<&FeedbackCtlDataInteger> { |
| 5398 | match self { |
| 5399 | FeedbackCtlData::Integer(value) => Some(value), |
| 5400 | _ => None, |
| 5401 | } |
| 5402 | } |
| 5403 | pub fn as_led(&self) -> Option<&FeedbackCtlDataLed> { |
| 5404 | match self { |
| 5405 | FeedbackCtlData::Led(value) => Some(value), |
| 5406 | _ => None, |
| 5407 | } |
| 5408 | } |
| 5409 | pub fn as_bell(&self) -> Option<&FeedbackCtlDataBell> { |
| 5410 | match self { |
| 5411 | FeedbackCtlData::Bell(value) => Some(value), |
| 5412 | _ => None, |
| 5413 | } |
| 5414 | } |
| 5415 | } |
| 5416 | impl FeedbackCtlData { |
| 5417 | #[allow (dead_code)] |
| 5418 | fn serialize(&self, class_id: u8) -> Vec<u8> { |
| 5419 | let mut result: Vec = Vec::new(); |
| 5420 | self.serialize_into(&mut result, class_id:u8::from(class_id)); |
| 5421 | result |
| 5422 | } |
| 5423 | fn serialize_into(&self, bytes: &mut Vec<u8>, class_id: u8) { |
| 5424 | assert_eq!(self.switch_expr(), u8::from(class_id), "switch `data` has an inconsistent discriminant" ); |
| 5425 | match self { |
| 5426 | FeedbackCtlData::Keyboard(keyboard: &FeedbackCtlDataKeyboard) => keyboard.serialize_into(bytes), |
| 5427 | FeedbackCtlData::Pointer(pointer: &FeedbackCtlDataPointer) => pointer.serialize_into(bytes), |
| 5428 | FeedbackCtlData::String(string: &FeedbackCtlDataString) => string.serialize_into(bytes), |
| 5429 | FeedbackCtlData::Integer(integer: &FeedbackCtlDataInteger) => integer.serialize_into(bytes), |
| 5430 | FeedbackCtlData::Led(led: &FeedbackCtlDataLed) => led.serialize_into(bytes), |
| 5431 | FeedbackCtlData::Bell(bell: &FeedbackCtlDataBell) => bell.serialize_into(bytes), |
| 5432 | FeedbackCtlData::InvalidValue(_) => panic!("attempted to serialize invalid switch case" ), |
| 5433 | } |
| 5434 | } |
| 5435 | } |
| 5436 | impl FeedbackCtlData { |
| 5437 | fn switch_expr(&self) -> u8 { |
| 5438 | match self { |
| 5439 | FeedbackCtlData::Keyboard(_) => u8::from(FeedbackClass::KEYBOARD), |
| 5440 | FeedbackCtlData::Pointer(_) => u8::from(FeedbackClass::POINTER), |
| 5441 | FeedbackCtlData::String(_) => u8::from(FeedbackClass::STRING), |
| 5442 | FeedbackCtlData::Integer(_) => u8::from(FeedbackClass::INTEGER), |
| 5443 | FeedbackCtlData::Led(_) => u8::from(FeedbackClass::LED), |
| 5444 | FeedbackCtlData::Bell(_) => u8::from(FeedbackClass::BELL), |
| 5445 | FeedbackCtlData::InvalidValue(switch_expr: &u8) => *switch_expr, |
| 5446 | } |
| 5447 | } |
| 5448 | } |
| 5449 | |
| 5450 | #[derive (Clone)] |
| 5451 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5452 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5453 | pub struct FeedbackCtl { |
| 5454 | pub feedback_id: u8, |
| 5455 | pub len: u16, |
| 5456 | pub data: FeedbackCtlData, |
| 5457 | } |
| 5458 | impl_debug_if_no_extra_traits!(FeedbackCtl, "FeedbackCtl" ); |
| 5459 | impl TryParse for FeedbackCtl { |
| 5460 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 5461 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 5462 | let (feedback_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 5463 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 5464 | let (data: FeedbackCtlData, remaining: &[u8]) = FeedbackCtlData::try_parse(value:remaining, class_id:u8::from(class_id))?; |
| 5465 | let result: FeedbackCtl = FeedbackCtl { feedback_id, len, data }; |
| 5466 | Ok((result, remaining)) |
| 5467 | } |
| 5468 | } |
| 5469 | impl Serialize for FeedbackCtl { |
| 5470 | type Bytes = Vec<u8>; |
| 5471 | fn serialize(&self) -> Vec<u8> { |
| 5472 | let mut result: Vec = Vec::new(); |
| 5473 | self.serialize_into(&mut result); |
| 5474 | result |
| 5475 | } |
| 5476 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 5477 | bytes.reserve(additional:4); |
| 5478 | let class_id: u8 = self.data.switch_expr(); |
| 5479 | class_id.serialize_into(bytes); |
| 5480 | self.feedback_id.serialize_into(bytes); |
| 5481 | self.len.serialize_into(bytes); |
| 5482 | self.data.serialize_into(bytes, class_id:u8::from(class_id)); |
| 5483 | } |
| 5484 | } |
| 5485 | |
| 5486 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 5487 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5488 | pub struct ChangeFeedbackControlMask(u32); |
| 5489 | impl ChangeFeedbackControlMask { |
| 5490 | pub const KEY_CLICK_PERCENT: Self = Self(1 << 0); |
| 5491 | pub const PERCENT: Self = Self(1 << 1); |
| 5492 | pub const PITCH: Self = Self(1 << 2); |
| 5493 | pub const DURATION: Self = Self(1 << 3); |
| 5494 | pub const LED: Self = Self(1 << 4); |
| 5495 | pub const LED_MODE: Self = Self(1 << 5); |
| 5496 | pub const KEY: Self = Self(1 << 6); |
| 5497 | pub const AUTO_REPEAT_MODE: Self = Self(1 << 7); |
| 5498 | pub const STRING: Self = Self(1 << 0); |
| 5499 | pub const INTEGER: Self = Self(1 << 0); |
| 5500 | pub const ACCEL_NUM: Self = Self(1 << 0); |
| 5501 | pub const ACCEL_DENOM: Self = Self(1 << 1); |
| 5502 | pub const THRESHOLD: Self = Self(1 << 2); |
| 5503 | } |
| 5504 | impl From<ChangeFeedbackControlMask> for u32 { |
| 5505 | #[inline ] |
| 5506 | fn from(input: ChangeFeedbackControlMask) -> Self { |
| 5507 | input.0 |
| 5508 | } |
| 5509 | } |
| 5510 | impl From<ChangeFeedbackControlMask> for Option<u32> { |
| 5511 | #[inline ] |
| 5512 | fn from(input: ChangeFeedbackControlMask) -> Self { |
| 5513 | Some(input.0) |
| 5514 | } |
| 5515 | } |
| 5516 | impl From<u8> for ChangeFeedbackControlMask { |
| 5517 | #[inline ] |
| 5518 | fn from(value: u8) -> Self { |
| 5519 | Self(value.into()) |
| 5520 | } |
| 5521 | } |
| 5522 | impl From<u16> for ChangeFeedbackControlMask { |
| 5523 | #[inline ] |
| 5524 | fn from(value: u16) -> Self { |
| 5525 | Self(value.into()) |
| 5526 | } |
| 5527 | } |
| 5528 | impl From<u32> for ChangeFeedbackControlMask { |
| 5529 | #[inline ] |
| 5530 | fn from(value: u32) -> Self { |
| 5531 | Self(value) |
| 5532 | } |
| 5533 | } |
| 5534 | impl core::fmt::Debug for ChangeFeedbackControlMask { |
| 5535 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 5536 | let variants: [(u32, &'static str, &'static …); 13] = [ |
| 5537 | (Self::KEY_CLICK_PERCENT.0, "KEY_CLICK_PERCENT" , "KeyClickPercent" ), |
| 5538 | (Self::PERCENT.0, "PERCENT" , "Percent" ), |
| 5539 | (Self::PITCH.0, "PITCH" , "Pitch" ), |
| 5540 | (Self::DURATION.0, "DURATION" , "Duration" ), |
| 5541 | (Self::LED.0, "LED" , "Led" ), |
| 5542 | (Self::LED_MODE.0, "LED_MODE" , "LedMode" ), |
| 5543 | (Self::KEY.0, "KEY" , "Key" ), |
| 5544 | (Self::AUTO_REPEAT_MODE.0, "AUTO_REPEAT_MODE" , "AutoRepeatMode" ), |
| 5545 | (Self::STRING.0, "STRING" , "String" ), |
| 5546 | (Self::INTEGER.0, "INTEGER" , "Integer" ), |
| 5547 | (Self::ACCEL_NUM.0, "ACCEL_NUM" , "AccelNum" ), |
| 5548 | (Self::ACCEL_DENOM.0, "ACCEL_DENOM" , "AccelDenom" ), |
| 5549 | (Self::THRESHOLD.0, "THRESHOLD" , "Threshold" ), |
| 5550 | ]; |
| 5551 | pretty_print_bitmask(fmt, self.0, &variants) |
| 5552 | } |
| 5553 | } |
| 5554 | bitmask_binop!(ChangeFeedbackControlMask, u32); |
| 5555 | |
| 5556 | /// Opcode for the ChangeFeedbackControl request |
| 5557 | pub const CHANGE_FEEDBACK_CONTROL_REQUEST: u8 = 23; |
| 5558 | #[derive (Clone)] |
| 5559 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5560 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5561 | pub struct ChangeFeedbackControlRequest { |
| 5562 | pub mask: ChangeFeedbackControlMask, |
| 5563 | pub device_id: u8, |
| 5564 | pub feedback_id: u8, |
| 5565 | pub feedback: FeedbackCtl, |
| 5566 | } |
| 5567 | impl_debug_if_no_extra_traits!(ChangeFeedbackControlRequest, "ChangeFeedbackControlRequest" ); |
| 5568 | impl ChangeFeedbackControlRequest { |
| 5569 | /// Serialize this request into bytes for the provided connection |
| 5570 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 3]> { |
| 5571 | let length_so_far = 0; |
| 5572 | let mask_bytes = u32::from(self.mask).serialize(); |
| 5573 | let device_id_bytes = self.device_id.serialize(); |
| 5574 | let feedback_id_bytes = self.feedback_id.serialize(); |
| 5575 | let mut request0 = vec![ |
| 5576 | major_opcode, |
| 5577 | CHANGE_FEEDBACK_CONTROL_REQUEST, |
| 5578 | 0, |
| 5579 | 0, |
| 5580 | mask_bytes[0], |
| 5581 | mask_bytes[1], |
| 5582 | mask_bytes[2], |
| 5583 | mask_bytes[3], |
| 5584 | device_id_bytes[0], |
| 5585 | feedback_id_bytes[0], |
| 5586 | 0, |
| 5587 | 0, |
| 5588 | ]; |
| 5589 | let length_so_far = length_so_far + request0.len(); |
| 5590 | let feedback_bytes = self.feedback.serialize(); |
| 5591 | let length_so_far = length_so_far + feedback_bytes.len(); |
| 5592 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 5593 | let length_so_far = length_so_far + padding0.len(); |
| 5594 | assert_eq!(length_so_far % 4, 0); |
| 5595 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 5596 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 5597 | ([request0.into(), feedback_bytes.into(), padding0.into()], vec![]) |
| 5598 | } |
| 5599 | /// Parse this request given its header, its body, and any fds that go along with it |
| 5600 | #[cfg (feature = "request-parsing" )] |
| 5601 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 5602 | if header.minor_opcode != CHANGE_FEEDBACK_CONTROL_REQUEST { |
| 5603 | return Err(ParseError::InvalidValue); |
| 5604 | } |
| 5605 | let (mask, remaining) = u32::try_parse(value)?; |
| 5606 | let mask = mask.into(); |
| 5607 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 5608 | let (feedback_id, remaining) = u8::try_parse(remaining)?; |
| 5609 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 5610 | let (feedback, remaining) = FeedbackCtl::try_parse(remaining)?; |
| 5611 | let _ = remaining; |
| 5612 | Ok(ChangeFeedbackControlRequest { |
| 5613 | mask, |
| 5614 | device_id, |
| 5615 | feedback_id, |
| 5616 | feedback, |
| 5617 | }) |
| 5618 | } |
| 5619 | } |
| 5620 | impl Request for ChangeFeedbackControlRequest { |
| 5621 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 5622 | |
| 5623 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 5624 | let (bufs: [Cow<'static, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 5625 | // Flatten the buffers into a single vector |
| 5626 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 5627 | (buf, fds) |
| 5628 | } |
| 5629 | } |
| 5630 | impl crate::x11_utils::VoidRequest for ChangeFeedbackControlRequest { |
| 5631 | } |
| 5632 | |
| 5633 | /// Opcode for the GetDeviceKeyMapping request |
| 5634 | pub const GET_DEVICE_KEY_MAPPING_REQUEST: u8 = 24; |
| 5635 | #[derive (Clone, Copy, Default)] |
| 5636 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5637 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5638 | pub struct GetDeviceKeyMappingRequest { |
| 5639 | pub device_id: u8, |
| 5640 | pub first_keycode: KeyCode, |
| 5641 | pub count: u8, |
| 5642 | } |
| 5643 | impl_debug_if_no_extra_traits!(GetDeviceKeyMappingRequest, "GetDeviceKeyMappingRequest" ); |
| 5644 | impl GetDeviceKeyMappingRequest { |
| 5645 | /// Serialize this request into bytes for the provided connection |
| 5646 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 5647 | let length_so_far = 0; |
| 5648 | let device_id_bytes = self.device_id.serialize(); |
| 5649 | let first_keycode_bytes = self.first_keycode.serialize(); |
| 5650 | let count_bytes = self.count.serialize(); |
| 5651 | let mut request0 = vec![ |
| 5652 | major_opcode, |
| 5653 | GET_DEVICE_KEY_MAPPING_REQUEST, |
| 5654 | 0, |
| 5655 | 0, |
| 5656 | device_id_bytes[0], |
| 5657 | first_keycode_bytes[0], |
| 5658 | count_bytes[0], |
| 5659 | 0, |
| 5660 | ]; |
| 5661 | let length_so_far = length_so_far + request0.len(); |
| 5662 | assert_eq!(length_so_far % 4, 0); |
| 5663 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 5664 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 5665 | ([request0.into()], vec![]) |
| 5666 | } |
| 5667 | /// Parse this request given its header, its body, and any fds that go along with it |
| 5668 | #[cfg (feature = "request-parsing" )] |
| 5669 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 5670 | if header.minor_opcode != GET_DEVICE_KEY_MAPPING_REQUEST { |
| 5671 | return Err(ParseError::InvalidValue); |
| 5672 | } |
| 5673 | let (device_id, remaining) = u8::try_parse(value)?; |
| 5674 | let (first_keycode, remaining) = KeyCode::try_parse(remaining)?; |
| 5675 | let (count, remaining) = u8::try_parse(remaining)?; |
| 5676 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
| 5677 | let _ = remaining; |
| 5678 | Ok(GetDeviceKeyMappingRequest { |
| 5679 | device_id, |
| 5680 | first_keycode, |
| 5681 | count, |
| 5682 | }) |
| 5683 | } |
| 5684 | } |
| 5685 | impl Request for GetDeviceKeyMappingRequest { |
| 5686 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 5687 | |
| 5688 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 5689 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 5690 | // Flatten the buffers into a single vector |
| 5691 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 5692 | (buf, fds) |
| 5693 | } |
| 5694 | } |
| 5695 | impl crate::x11_utils::ReplyRequest for GetDeviceKeyMappingRequest { |
| 5696 | type Reply = GetDeviceKeyMappingReply; |
| 5697 | } |
| 5698 | |
| 5699 | #[derive (Clone, Default)] |
| 5700 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5701 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5702 | pub struct GetDeviceKeyMappingReply { |
| 5703 | pub xi_reply_type: u8, |
| 5704 | pub sequence: u16, |
| 5705 | pub keysyms_per_keycode: u8, |
| 5706 | pub keysyms: Vec<xproto::Keysym>, |
| 5707 | } |
| 5708 | impl_debug_if_no_extra_traits!(GetDeviceKeyMappingReply, "GetDeviceKeyMappingReply" ); |
| 5709 | impl TryParse for GetDeviceKeyMappingReply { |
| 5710 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 5711 | let remaining: &[u8] = initial_value; |
| 5712 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 5713 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 5714 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 5715 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 5716 | let (keysyms_per_keycode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 5717 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 5718 | let (keysyms: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<xproto::Keysym>(data:remaining, list_length:length.try_to_usize()?)?; |
| 5719 | if response_type != 1 { |
| 5720 | return Err(ParseError::InvalidValue); |
| 5721 | } |
| 5722 | let result: GetDeviceKeyMappingReply = GetDeviceKeyMappingReply { xi_reply_type, sequence, keysyms_per_keycode, keysyms }; |
| 5723 | let _ = remaining; |
| 5724 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 5725 | .ok_or(err:ParseError::InsufficientData)?; |
| 5726 | Ok((result, remaining)) |
| 5727 | } |
| 5728 | } |
| 5729 | impl Serialize for GetDeviceKeyMappingReply { |
| 5730 | type Bytes = Vec<u8>; |
| 5731 | fn serialize(&self) -> Vec<u8> { |
| 5732 | let mut result: Vec = Vec::new(); |
| 5733 | self.serialize_into(&mut result); |
| 5734 | result |
| 5735 | } |
| 5736 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 5737 | bytes.reserve(additional:32); |
| 5738 | let response_type_bytes: &[u8; 1] = &[1]; |
| 5739 | bytes.push(response_type_bytes[0]); |
| 5740 | self.xi_reply_type.serialize_into(bytes); |
| 5741 | self.sequence.serialize_into(bytes); |
| 5742 | let length: u32 = u32::try_from(self.keysyms.len()).expect(msg:"`keysyms` has too many elements" ); |
| 5743 | length.serialize_into(bytes); |
| 5744 | self.keysyms_per_keycode.serialize_into(bytes); |
| 5745 | bytes.extend_from_slice(&[0; 23]); |
| 5746 | self.keysyms.serialize_into(bytes); |
| 5747 | } |
| 5748 | } |
| 5749 | impl GetDeviceKeyMappingReply { |
| 5750 | /// Get the value of the `length` field. |
| 5751 | /// |
| 5752 | /// The `length` field is used as the length field of the `keysyms` field. |
| 5753 | /// This function computes the field's value again based on the length of the list. |
| 5754 | /// |
| 5755 | /// # Panics |
| 5756 | /// |
| 5757 | /// Panics if the value cannot be represented in the target type. This |
| 5758 | /// cannot happen with values of the struct received from the X11 server. |
| 5759 | pub fn length(&self) -> u32 { |
| 5760 | self.keysyms.len() |
| 5761 | .try_into().unwrap() |
| 5762 | } |
| 5763 | } |
| 5764 | |
| 5765 | /// Opcode for the ChangeDeviceKeyMapping request |
| 5766 | pub const CHANGE_DEVICE_KEY_MAPPING_REQUEST: u8 = 25; |
| 5767 | #[derive (Clone, Default)] |
| 5768 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5769 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5770 | pub struct ChangeDeviceKeyMappingRequest<'input> { |
| 5771 | pub device_id: u8, |
| 5772 | pub first_keycode: KeyCode, |
| 5773 | pub keysyms_per_keycode: u8, |
| 5774 | pub keycode_count: u8, |
| 5775 | pub keysyms: Cow<'input, [xproto::Keysym]>, |
| 5776 | } |
| 5777 | impl_debug_if_no_extra_traits!(ChangeDeviceKeyMappingRequest<'_>, "ChangeDeviceKeyMappingRequest" ); |
| 5778 | impl<'input> ChangeDeviceKeyMappingRequest<'input> { |
| 5779 | /// Serialize this request into bytes for the provided connection |
| 5780 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 5781 | let length_so_far = 0; |
| 5782 | let device_id_bytes = self.device_id.serialize(); |
| 5783 | let first_keycode_bytes = self.first_keycode.serialize(); |
| 5784 | let keysyms_per_keycode_bytes = self.keysyms_per_keycode.serialize(); |
| 5785 | let keycode_count_bytes = self.keycode_count.serialize(); |
| 5786 | let mut request0 = vec![ |
| 5787 | major_opcode, |
| 5788 | CHANGE_DEVICE_KEY_MAPPING_REQUEST, |
| 5789 | 0, |
| 5790 | 0, |
| 5791 | device_id_bytes[0], |
| 5792 | first_keycode_bytes[0], |
| 5793 | keysyms_per_keycode_bytes[0], |
| 5794 | keycode_count_bytes[0], |
| 5795 | ]; |
| 5796 | let length_so_far = length_so_far + request0.len(); |
| 5797 | 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" ); |
| 5798 | let keysyms_bytes = self.keysyms.serialize(); |
| 5799 | let length_so_far = length_so_far + keysyms_bytes.len(); |
| 5800 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 5801 | let length_so_far = length_so_far + padding0.len(); |
| 5802 | assert_eq!(length_so_far % 4, 0); |
| 5803 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 5804 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 5805 | ([request0.into(), keysyms_bytes.into(), padding0.into()], vec![]) |
| 5806 | } |
| 5807 | /// Parse this request given its header, its body, and any fds that go along with it |
| 5808 | #[cfg (feature = "request-parsing" )] |
| 5809 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 5810 | if header.minor_opcode != CHANGE_DEVICE_KEY_MAPPING_REQUEST { |
| 5811 | return Err(ParseError::InvalidValue); |
| 5812 | } |
| 5813 | let (device_id, remaining) = u8::try_parse(value)?; |
| 5814 | let (first_keycode, remaining) = KeyCode::try_parse(remaining)?; |
| 5815 | let (keysyms_per_keycode, remaining) = u8::try_parse(remaining)?; |
| 5816 | let (keycode_count, remaining) = u8::try_parse(remaining)?; |
| 5817 | let (keysyms, remaining) = crate::x11_utils::parse_list::<xproto::Keysym>(remaining, u32::from(keycode_count).checked_mul(u32::from(keysyms_per_keycode)).ok_or(ParseError::InvalidExpression)?.try_to_usize()?)?; |
| 5818 | let _ = remaining; |
| 5819 | Ok(ChangeDeviceKeyMappingRequest { |
| 5820 | device_id, |
| 5821 | first_keycode, |
| 5822 | keysyms_per_keycode, |
| 5823 | keycode_count, |
| 5824 | keysyms: Cow::Owned(keysyms), |
| 5825 | }) |
| 5826 | } |
| 5827 | /// Clone all borrowed data in this ChangeDeviceKeyMappingRequest. |
| 5828 | pub fn into_owned(self) -> ChangeDeviceKeyMappingRequest<'static> { |
| 5829 | ChangeDeviceKeyMappingRequest { |
| 5830 | device_id: self.device_id, |
| 5831 | first_keycode: self.first_keycode, |
| 5832 | keysyms_per_keycode: self.keysyms_per_keycode, |
| 5833 | keycode_count: self.keycode_count, |
| 5834 | keysyms: Cow::Owned(self.keysyms.into_owned()), |
| 5835 | } |
| 5836 | } |
| 5837 | } |
| 5838 | impl<'input> Request for ChangeDeviceKeyMappingRequest<'input> { |
| 5839 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 5840 | |
| 5841 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 5842 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 5843 | // Flatten the buffers into a single vector |
| 5844 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 5845 | (buf, fds) |
| 5846 | } |
| 5847 | } |
| 5848 | impl<'input> crate::x11_utils::VoidRequest for ChangeDeviceKeyMappingRequest<'input> { |
| 5849 | } |
| 5850 | |
| 5851 | /// Opcode for the GetDeviceModifierMapping request |
| 5852 | pub const GET_DEVICE_MODIFIER_MAPPING_REQUEST: u8 = 26; |
| 5853 | #[derive (Clone, Copy, Default)] |
| 5854 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5855 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5856 | pub struct GetDeviceModifierMappingRequest { |
| 5857 | pub device_id: u8, |
| 5858 | } |
| 5859 | impl_debug_if_no_extra_traits!(GetDeviceModifierMappingRequest, "GetDeviceModifierMappingRequest" ); |
| 5860 | impl GetDeviceModifierMappingRequest { |
| 5861 | /// Serialize this request into bytes for the provided connection |
| 5862 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 5863 | let length_so_far = 0; |
| 5864 | let device_id_bytes = self.device_id.serialize(); |
| 5865 | let mut request0 = vec![ |
| 5866 | major_opcode, |
| 5867 | GET_DEVICE_MODIFIER_MAPPING_REQUEST, |
| 5868 | 0, |
| 5869 | 0, |
| 5870 | device_id_bytes[0], |
| 5871 | 0, |
| 5872 | 0, |
| 5873 | 0, |
| 5874 | ]; |
| 5875 | let length_so_far = length_so_far + request0.len(); |
| 5876 | assert_eq!(length_so_far % 4, 0); |
| 5877 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 5878 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 5879 | ([request0.into()], vec![]) |
| 5880 | } |
| 5881 | /// Parse this request given its header, its body, and any fds that go along with it |
| 5882 | #[cfg (feature = "request-parsing" )] |
| 5883 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 5884 | if header.minor_opcode != GET_DEVICE_MODIFIER_MAPPING_REQUEST { |
| 5885 | return Err(ParseError::InvalidValue); |
| 5886 | } |
| 5887 | let (device_id, remaining) = u8::try_parse(value)?; |
| 5888 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 5889 | let _ = remaining; |
| 5890 | Ok(GetDeviceModifierMappingRequest { |
| 5891 | device_id, |
| 5892 | }) |
| 5893 | } |
| 5894 | } |
| 5895 | impl Request for GetDeviceModifierMappingRequest { |
| 5896 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 5897 | |
| 5898 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 5899 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 5900 | // Flatten the buffers into a single vector |
| 5901 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 5902 | (buf, fds) |
| 5903 | } |
| 5904 | } |
| 5905 | impl crate::x11_utils::ReplyRequest for GetDeviceModifierMappingRequest { |
| 5906 | type Reply = GetDeviceModifierMappingReply; |
| 5907 | } |
| 5908 | |
| 5909 | #[derive (Clone, Default)] |
| 5910 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5911 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5912 | pub struct GetDeviceModifierMappingReply { |
| 5913 | pub xi_reply_type: u8, |
| 5914 | pub sequence: u16, |
| 5915 | pub length: u32, |
| 5916 | pub keymaps: Vec<u8>, |
| 5917 | } |
| 5918 | impl_debug_if_no_extra_traits!(GetDeviceModifierMappingReply, "GetDeviceModifierMappingReply" ); |
| 5919 | impl TryParse for GetDeviceModifierMappingReply { |
| 5920 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 5921 | let remaining: &[u8] = initial_value; |
| 5922 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 5923 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 5924 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 5925 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 5926 | let (keycodes_per_modifier: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 5927 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 5928 | let (keymaps: &[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()?)?; |
| 5929 | let keymaps: Vec = keymaps.to_vec(); |
| 5930 | if response_type != 1 { |
| 5931 | return Err(ParseError::InvalidValue); |
| 5932 | } |
| 5933 | let result: GetDeviceModifierMappingReply = GetDeviceModifierMappingReply { xi_reply_type, sequence, length, keymaps }; |
| 5934 | let _ = remaining; |
| 5935 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 5936 | .ok_or(err:ParseError::InsufficientData)?; |
| 5937 | Ok((result, remaining)) |
| 5938 | } |
| 5939 | } |
| 5940 | impl Serialize for GetDeviceModifierMappingReply { |
| 5941 | type Bytes = Vec<u8>; |
| 5942 | fn serialize(&self) -> Vec<u8> { |
| 5943 | let mut result: Vec = Vec::new(); |
| 5944 | self.serialize_into(&mut result); |
| 5945 | result |
| 5946 | } |
| 5947 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 5948 | bytes.reserve(additional:32); |
| 5949 | let response_type_bytes: &[u8; 1] = &[1]; |
| 5950 | bytes.push(response_type_bytes[0]); |
| 5951 | self.xi_reply_type.serialize_into(bytes); |
| 5952 | self.sequence.serialize_into(bytes); |
| 5953 | self.length.serialize_into(bytes); |
| 5954 | assert_eq!(self.keymaps.len() % 8, 0, "`keymaps` has an incorrect length, must be a multiple of 8" ); |
| 5955 | let keycodes_per_modifier: u8 = u8::try_from(self.keymaps.len() / 8).expect(msg:"`keymaps` has too many elements" ); |
| 5956 | keycodes_per_modifier.serialize_into(bytes); |
| 5957 | bytes.extend_from_slice(&[0; 23]); |
| 5958 | bytes.extend_from_slice(&self.keymaps); |
| 5959 | } |
| 5960 | } |
| 5961 | impl GetDeviceModifierMappingReply { |
| 5962 | /// Get the value of the `keycodes_per_modifier` field. |
| 5963 | /// |
| 5964 | /// The `keycodes_per_modifier` field is used as the length field of the `keymaps` field. |
| 5965 | /// This function computes the field's value again based on the length of the list. |
| 5966 | /// |
| 5967 | /// # Panics |
| 5968 | /// |
| 5969 | /// Panics if the value cannot be represented in the target type. This |
| 5970 | /// cannot happen with values of the struct received from the X11 server. |
| 5971 | pub fn keycodes_per_modifier(&self) -> u8 { |
| 5972 | self.keymaps.len() |
| 5973 | .checked_div(8).unwrap() |
| 5974 | .try_into().unwrap() |
| 5975 | } |
| 5976 | } |
| 5977 | |
| 5978 | /// Opcode for the SetDeviceModifierMapping request |
| 5979 | pub const SET_DEVICE_MODIFIER_MAPPING_REQUEST: u8 = 27; |
| 5980 | #[derive (Clone, Default)] |
| 5981 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 5982 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 5983 | pub struct SetDeviceModifierMappingRequest<'input> { |
| 5984 | pub device_id: u8, |
| 5985 | pub keymaps: Cow<'input, [u8]>, |
| 5986 | } |
| 5987 | impl_debug_if_no_extra_traits!(SetDeviceModifierMappingRequest<'_>, "SetDeviceModifierMappingRequest" ); |
| 5988 | impl<'input> SetDeviceModifierMappingRequest<'input> { |
| 5989 | /// Serialize this request into bytes for the provided connection |
| 5990 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 5991 | let length_so_far = 0; |
| 5992 | let device_id_bytes = self.device_id.serialize(); |
| 5993 | assert_eq!(self.keymaps.len() % 8, 0, "`keymaps` has an incorrect length, must be a multiple of 8" ); |
| 5994 | let keycodes_per_modifier = u8::try_from(self.keymaps.len() / 8).expect("`keymaps` has too many elements" ); |
| 5995 | let keycodes_per_modifier_bytes = keycodes_per_modifier.serialize(); |
| 5996 | let mut request0 = vec![ |
| 5997 | major_opcode, |
| 5998 | SET_DEVICE_MODIFIER_MAPPING_REQUEST, |
| 5999 | 0, |
| 6000 | 0, |
| 6001 | device_id_bytes[0], |
| 6002 | keycodes_per_modifier_bytes[0], |
| 6003 | 0, |
| 6004 | 0, |
| 6005 | ]; |
| 6006 | let length_so_far = length_so_far + request0.len(); |
| 6007 | let length_so_far = length_so_far + self.keymaps.len(); |
| 6008 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 6009 | let length_so_far = length_so_far + padding0.len(); |
| 6010 | assert_eq!(length_so_far % 4, 0); |
| 6011 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 6012 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 6013 | ([request0.into(), self.keymaps, padding0.into()], vec![]) |
| 6014 | } |
| 6015 | /// Parse this request given its header, its body, and any fds that go along with it |
| 6016 | #[cfg (feature = "request-parsing" )] |
| 6017 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 6018 | if header.minor_opcode != SET_DEVICE_MODIFIER_MAPPING_REQUEST { |
| 6019 | return Err(ParseError::InvalidValue); |
| 6020 | } |
| 6021 | let (device_id, remaining) = u8::try_parse(value)?; |
| 6022 | let (keycodes_per_modifier, remaining) = u8::try_parse(remaining)?; |
| 6023 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 6024 | let (keymaps, remaining) = crate::x11_utils::parse_u8_list(remaining, u32::from(keycodes_per_modifier).checked_mul(8u32).ok_or(ParseError::InvalidExpression)?.try_to_usize()?)?; |
| 6025 | let _ = remaining; |
| 6026 | Ok(SetDeviceModifierMappingRequest { |
| 6027 | device_id, |
| 6028 | keymaps: Cow::Borrowed(keymaps), |
| 6029 | }) |
| 6030 | } |
| 6031 | /// Clone all borrowed data in this SetDeviceModifierMappingRequest. |
| 6032 | pub fn into_owned(self) -> SetDeviceModifierMappingRequest<'static> { |
| 6033 | SetDeviceModifierMappingRequest { |
| 6034 | device_id: self.device_id, |
| 6035 | keymaps: Cow::Owned(self.keymaps.into_owned()), |
| 6036 | } |
| 6037 | } |
| 6038 | } |
| 6039 | impl<'input> Request for SetDeviceModifierMappingRequest<'input> { |
| 6040 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 6041 | |
| 6042 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 6043 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 6044 | // Flatten the buffers into a single vector |
| 6045 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 6046 | (buf, fds) |
| 6047 | } |
| 6048 | } |
| 6049 | impl<'input> crate::x11_utils::ReplyRequest for SetDeviceModifierMappingRequest<'input> { |
| 6050 | type Reply = SetDeviceModifierMappingReply; |
| 6051 | } |
| 6052 | |
| 6053 | #[derive (Clone, Copy, Default)] |
| 6054 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 6055 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6056 | pub struct SetDeviceModifierMappingReply { |
| 6057 | pub xi_reply_type: u8, |
| 6058 | pub sequence: u16, |
| 6059 | pub length: u32, |
| 6060 | pub status: xproto::MappingStatus, |
| 6061 | } |
| 6062 | impl_debug_if_no_extra_traits!(SetDeviceModifierMappingReply, "SetDeviceModifierMappingReply" ); |
| 6063 | impl TryParse for SetDeviceModifierMappingReply { |
| 6064 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 6065 | let remaining: &[u8] = initial_value; |
| 6066 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6067 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6068 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 6069 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 6070 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6071 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 6072 | if response_type != 1 { |
| 6073 | return Err(ParseError::InvalidValue); |
| 6074 | } |
| 6075 | let status: MappingStatus = status.into(); |
| 6076 | let result: SetDeviceModifierMappingReply = SetDeviceModifierMappingReply { xi_reply_type, sequence, length, status }; |
| 6077 | let _ = remaining; |
| 6078 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 6079 | .ok_or(err:ParseError::InsufficientData)?; |
| 6080 | Ok((result, remaining)) |
| 6081 | } |
| 6082 | } |
| 6083 | impl Serialize for SetDeviceModifierMappingReply { |
| 6084 | type Bytes = [u8; 32]; |
| 6085 | fn serialize(&self) -> [u8; 32] { |
| 6086 | let response_type_bytes = &[1]; |
| 6087 | let xi_reply_type_bytes = self.xi_reply_type.serialize(); |
| 6088 | let sequence_bytes = self.sequence.serialize(); |
| 6089 | let length_bytes = self.length.serialize(); |
| 6090 | let status_bytes = u8::from(self.status).serialize(); |
| 6091 | [ |
| 6092 | response_type_bytes[0], |
| 6093 | xi_reply_type_bytes[0], |
| 6094 | sequence_bytes[0], |
| 6095 | sequence_bytes[1], |
| 6096 | length_bytes[0], |
| 6097 | length_bytes[1], |
| 6098 | length_bytes[2], |
| 6099 | length_bytes[3], |
| 6100 | status_bytes[0], |
| 6101 | 0, |
| 6102 | 0, |
| 6103 | 0, |
| 6104 | 0, |
| 6105 | 0, |
| 6106 | 0, |
| 6107 | 0, |
| 6108 | 0, |
| 6109 | 0, |
| 6110 | 0, |
| 6111 | 0, |
| 6112 | 0, |
| 6113 | 0, |
| 6114 | 0, |
| 6115 | 0, |
| 6116 | 0, |
| 6117 | 0, |
| 6118 | 0, |
| 6119 | 0, |
| 6120 | 0, |
| 6121 | 0, |
| 6122 | 0, |
| 6123 | 0, |
| 6124 | ] |
| 6125 | } |
| 6126 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 6127 | bytes.reserve(32); |
| 6128 | let response_type_bytes = &[1]; |
| 6129 | bytes.push(response_type_bytes[0]); |
| 6130 | self.xi_reply_type.serialize_into(bytes); |
| 6131 | self.sequence.serialize_into(bytes); |
| 6132 | self.length.serialize_into(bytes); |
| 6133 | u8::from(self.status).serialize_into(bytes); |
| 6134 | bytes.extend_from_slice(&[0; 23]); |
| 6135 | } |
| 6136 | } |
| 6137 | |
| 6138 | /// Opcode for the GetDeviceButtonMapping request |
| 6139 | pub const GET_DEVICE_BUTTON_MAPPING_REQUEST: u8 = 28; |
| 6140 | #[derive (Clone, Copy, Default)] |
| 6141 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 6142 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6143 | pub struct GetDeviceButtonMappingRequest { |
| 6144 | pub device_id: u8, |
| 6145 | } |
| 6146 | impl_debug_if_no_extra_traits!(GetDeviceButtonMappingRequest, "GetDeviceButtonMappingRequest" ); |
| 6147 | impl GetDeviceButtonMappingRequest { |
| 6148 | /// Serialize this request into bytes for the provided connection |
| 6149 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 6150 | let length_so_far = 0; |
| 6151 | let device_id_bytes = self.device_id.serialize(); |
| 6152 | let mut request0 = vec![ |
| 6153 | major_opcode, |
| 6154 | GET_DEVICE_BUTTON_MAPPING_REQUEST, |
| 6155 | 0, |
| 6156 | 0, |
| 6157 | device_id_bytes[0], |
| 6158 | 0, |
| 6159 | 0, |
| 6160 | 0, |
| 6161 | ]; |
| 6162 | let length_so_far = length_so_far + request0.len(); |
| 6163 | assert_eq!(length_so_far % 4, 0); |
| 6164 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 6165 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 6166 | ([request0.into()], vec![]) |
| 6167 | } |
| 6168 | /// Parse this request given its header, its body, and any fds that go along with it |
| 6169 | #[cfg (feature = "request-parsing" )] |
| 6170 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 6171 | if header.minor_opcode != GET_DEVICE_BUTTON_MAPPING_REQUEST { |
| 6172 | return Err(ParseError::InvalidValue); |
| 6173 | } |
| 6174 | let (device_id, remaining) = u8::try_parse(value)?; |
| 6175 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 6176 | let _ = remaining; |
| 6177 | Ok(GetDeviceButtonMappingRequest { |
| 6178 | device_id, |
| 6179 | }) |
| 6180 | } |
| 6181 | } |
| 6182 | impl Request for GetDeviceButtonMappingRequest { |
| 6183 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 6184 | |
| 6185 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 6186 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 6187 | // Flatten the buffers into a single vector |
| 6188 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 6189 | (buf, fds) |
| 6190 | } |
| 6191 | } |
| 6192 | impl crate::x11_utils::ReplyRequest for GetDeviceButtonMappingRequest { |
| 6193 | type Reply = GetDeviceButtonMappingReply; |
| 6194 | } |
| 6195 | |
| 6196 | #[derive (Clone, Default)] |
| 6197 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 6198 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6199 | pub struct GetDeviceButtonMappingReply { |
| 6200 | pub xi_reply_type: u8, |
| 6201 | pub sequence: u16, |
| 6202 | pub length: u32, |
| 6203 | pub map: Vec<u8>, |
| 6204 | } |
| 6205 | impl_debug_if_no_extra_traits!(GetDeviceButtonMappingReply, "GetDeviceButtonMappingReply" ); |
| 6206 | impl TryParse for GetDeviceButtonMappingReply { |
| 6207 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 6208 | let remaining = initial_value; |
| 6209 | let value = remaining; |
| 6210 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 6211 | let (xi_reply_type, remaining) = u8::try_parse(remaining)?; |
| 6212 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 6213 | let (length, remaining) = u32::try_parse(remaining)?; |
| 6214 | let (map_size, remaining) = u8::try_parse(remaining)?; |
| 6215 | let remaining = remaining.get(23..).ok_or(ParseError::InsufficientData)?; |
| 6216 | let (map, remaining) = crate::x11_utils::parse_u8_list(remaining, map_size.try_to_usize()?)?; |
| 6217 | let map = map.to_vec(); |
| 6218 | // Align offset to multiple of 4 |
| 6219 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 6220 | let misalignment = (4 - (offset % 4)) % 4; |
| 6221 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
| 6222 | if response_type != 1 { |
| 6223 | return Err(ParseError::InvalidValue); |
| 6224 | } |
| 6225 | let result = GetDeviceButtonMappingReply { xi_reply_type, sequence, length, map }; |
| 6226 | let _ = remaining; |
| 6227 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 6228 | .ok_or(ParseError::InsufficientData)?; |
| 6229 | Ok((result, remaining)) |
| 6230 | } |
| 6231 | } |
| 6232 | impl Serialize for GetDeviceButtonMappingReply { |
| 6233 | type Bytes = Vec<u8>; |
| 6234 | fn serialize(&self) -> Vec<u8> { |
| 6235 | let mut result: Vec = Vec::new(); |
| 6236 | self.serialize_into(&mut result); |
| 6237 | result |
| 6238 | } |
| 6239 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 6240 | bytes.reserve(additional:32); |
| 6241 | let response_type_bytes: &[u8; 1] = &[1]; |
| 6242 | bytes.push(response_type_bytes[0]); |
| 6243 | self.xi_reply_type.serialize_into(bytes); |
| 6244 | self.sequence.serialize_into(bytes); |
| 6245 | self.length.serialize_into(bytes); |
| 6246 | let map_size: u8 = u8::try_from(self.map.len()).expect(msg:"`map` has too many elements" ); |
| 6247 | map_size.serialize_into(bytes); |
| 6248 | bytes.extend_from_slice(&[0; 23]); |
| 6249 | bytes.extend_from_slice(&self.map); |
| 6250 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 6251 | } |
| 6252 | } |
| 6253 | impl GetDeviceButtonMappingReply { |
| 6254 | /// Get the value of the `map_size` field. |
| 6255 | /// |
| 6256 | /// The `map_size` field is used as the length field of the `map` field. |
| 6257 | /// This function computes the field's value again based on the length of the list. |
| 6258 | /// |
| 6259 | /// # Panics |
| 6260 | /// |
| 6261 | /// Panics if the value cannot be represented in the target type. This |
| 6262 | /// cannot happen with values of the struct received from the X11 server. |
| 6263 | pub fn map_size(&self) -> u8 { |
| 6264 | self.map.len() |
| 6265 | .try_into().unwrap() |
| 6266 | } |
| 6267 | } |
| 6268 | |
| 6269 | /// Opcode for the SetDeviceButtonMapping request |
| 6270 | pub const SET_DEVICE_BUTTON_MAPPING_REQUEST: u8 = 29; |
| 6271 | #[derive (Clone, Default)] |
| 6272 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 6273 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6274 | pub struct SetDeviceButtonMappingRequest<'input> { |
| 6275 | pub device_id: u8, |
| 6276 | pub map: Cow<'input, [u8]>, |
| 6277 | } |
| 6278 | impl_debug_if_no_extra_traits!(SetDeviceButtonMappingRequest<'_>, "SetDeviceButtonMappingRequest" ); |
| 6279 | impl<'input> SetDeviceButtonMappingRequest<'input> { |
| 6280 | /// Serialize this request into bytes for the provided connection |
| 6281 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 6282 | let length_so_far = 0; |
| 6283 | let device_id_bytes = self.device_id.serialize(); |
| 6284 | let map_size = u8::try_from(self.map.len()).expect("`map` has too many elements" ); |
| 6285 | let map_size_bytes = map_size.serialize(); |
| 6286 | let mut request0 = vec![ |
| 6287 | major_opcode, |
| 6288 | SET_DEVICE_BUTTON_MAPPING_REQUEST, |
| 6289 | 0, |
| 6290 | 0, |
| 6291 | device_id_bytes[0], |
| 6292 | map_size_bytes[0], |
| 6293 | 0, |
| 6294 | 0, |
| 6295 | ]; |
| 6296 | let length_so_far = length_so_far + request0.len(); |
| 6297 | let length_so_far = length_so_far + self.map.len(); |
| 6298 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 6299 | let length_so_far = length_so_far + padding0.len(); |
| 6300 | assert_eq!(length_so_far % 4, 0); |
| 6301 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 6302 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 6303 | ([request0.into(), self.map, padding0.into()], vec![]) |
| 6304 | } |
| 6305 | /// Parse this request given its header, its body, and any fds that go along with it |
| 6306 | #[cfg (feature = "request-parsing" )] |
| 6307 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 6308 | if header.minor_opcode != SET_DEVICE_BUTTON_MAPPING_REQUEST { |
| 6309 | return Err(ParseError::InvalidValue); |
| 6310 | } |
| 6311 | let (device_id, remaining) = u8::try_parse(value)?; |
| 6312 | let (map_size, remaining) = u8::try_parse(remaining)?; |
| 6313 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 6314 | let (map, remaining) = crate::x11_utils::parse_u8_list(remaining, map_size.try_to_usize()?)?; |
| 6315 | let _ = remaining; |
| 6316 | Ok(SetDeviceButtonMappingRequest { |
| 6317 | device_id, |
| 6318 | map: Cow::Borrowed(map), |
| 6319 | }) |
| 6320 | } |
| 6321 | /// Clone all borrowed data in this SetDeviceButtonMappingRequest. |
| 6322 | pub fn into_owned(self) -> SetDeviceButtonMappingRequest<'static> { |
| 6323 | SetDeviceButtonMappingRequest { |
| 6324 | device_id: self.device_id, |
| 6325 | map: Cow::Owned(self.map.into_owned()), |
| 6326 | } |
| 6327 | } |
| 6328 | } |
| 6329 | impl<'input> Request for SetDeviceButtonMappingRequest<'input> { |
| 6330 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 6331 | |
| 6332 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 6333 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 6334 | // Flatten the buffers into a single vector |
| 6335 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 6336 | (buf, fds) |
| 6337 | } |
| 6338 | } |
| 6339 | impl<'input> crate::x11_utils::ReplyRequest for SetDeviceButtonMappingRequest<'input> { |
| 6340 | type Reply = SetDeviceButtonMappingReply; |
| 6341 | } |
| 6342 | |
| 6343 | #[derive (Clone, Copy, Default)] |
| 6344 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 6345 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6346 | pub struct SetDeviceButtonMappingReply { |
| 6347 | pub xi_reply_type: u8, |
| 6348 | pub sequence: u16, |
| 6349 | pub length: u32, |
| 6350 | pub status: xproto::MappingStatus, |
| 6351 | } |
| 6352 | impl_debug_if_no_extra_traits!(SetDeviceButtonMappingReply, "SetDeviceButtonMappingReply" ); |
| 6353 | impl TryParse for SetDeviceButtonMappingReply { |
| 6354 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 6355 | let remaining: &[u8] = initial_value; |
| 6356 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6357 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6358 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 6359 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 6360 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6361 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 6362 | if response_type != 1 { |
| 6363 | return Err(ParseError::InvalidValue); |
| 6364 | } |
| 6365 | let status: MappingStatus = status.into(); |
| 6366 | let result: SetDeviceButtonMappingReply = SetDeviceButtonMappingReply { xi_reply_type, sequence, length, status }; |
| 6367 | let _ = remaining; |
| 6368 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 6369 | .ok_or(err:ParseError::InsufficientData)?; |
| 6370 | Ok((result, remaining)) |
| 6371 | } |
| 6372 | } |
| 6373 | impl Serialize for SetDeviceButtonMappingReply { |
| 6374 | type Bytes = [u8; 32]; |
| 6375 | fn serialize(&self) -> [u8; 32] { |
| 6376 | let response_type_bytes = &[1]; |
| 6377 | let xi_reply_type_bytes = self.xi_reply_type.serialize(); |
| 6378 | let sequence_bytes = self.sequence.serialize(); |
| 6379 | let length_bytes = self.length.serialize(); |
| 6380 | let status_bytes = u8::from(self.status).serialize(); |
| 6381 | [ |
| 6382 | response_type_bytes[0], |
| 6383 | xi_reply_type_bytes[0], |
| 6384 | sequence_bytes[0], |
| 6385 | sequence_bytes[1], |
| 6386 | length_bytes[0], |
| 6387 | length_bytes[1], |
| 6388 | length_bytes[2], |
| 6389 | length_bytes[3], |
| 6390 | status_bytes[0], |
| 6391 | 0, |
| 6392 | 0, |
| 6393 | 0, |
| 6394 | 0, |
| 6395 | 0, |
| 6396 | 0, |
| 6397 | 0, |
| 6398 | 0, |
| 6399 | 0, |
| 6400 | 0, |
| 6401 | 0, |
| 6402 | 0, |
| 6403 | 0, |
| 6404 | 0, |
| 6405 | 0, |
| 6406 | 0, |
| 6407 | 0, |
| 6408 | 0, |
| 6409 | 0, |
| 6410 | 0, |
| 6411 | 0, |
| 6412 | 0, |
| 6413 | 0, |
| 6414 | ] |
| 6415 | } |
| 6416 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 6417 | bytes.reserve(32); |
| 6418 | let response_type_bytes = &[1]; |
| 6419 | bytes.push(response_type_bytes[0]); |
| 6420 | self.xi_reply_type.serialize_into(bytes); |
| 6421 | self.sequence.serialize_into(bytes); |
| 6422 | self.length.serialize_into(bytes); |
| 6423 | u8::from(self.status).serialize_into(bytes); |
| 6424 | bytes.extend_from_slice(&[0; 23]); |
| 6425 | } |
| 6426 | } |
| 6427 | |
| 6428 | #[derive (Clone, Copy, Default)] |
| 6429 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 6430 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6431 | pub struct KeyState { |
| 6432 | pub class_id: InputClass, |
| 6433 | pub len: u8, |
| 6434 | pub num_keys: u8, |
| 6435 | pub keys: [u8; 32], |
| 6436 | } |
| 6437 | impl_debug_if_no_extra_traits!(KeyState, "KeyState" ); |
| 6438 | impl TryParse for KeyState { |
| 6439 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 6440 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6441 | let (len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6442 | let (num_keys: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6443 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 6444 | let (keys: [u8; 32], remaining: &[u8]) = crate::x11_utils::parse_u8_array::<32>(data:remaining)?; |
| 6445 | let class_id: InputClass = class_id.into(); |
| 6446 | let result: KeyState = KeyState { class_id, len, num_keys, keys }; |
| 6447 | Ok((result, remaining)) |
| 6448 | } |
| 6449 | } |
| 6450 | impl Serialize for KeyState { |
| 6451 | type Bytes = [u8; 36]; |
| 6452 | fn serialize(&self) -> [u8; 36] { |
| 6453 | let class_id_bytes = u8::from(self.class_id).serialize(); |
| 6454 | let len_bytes = self.len.serialize(); |
| 6455 | let num_keys_bytes = self.num_keys.serialize(); |
| 6456 | [ |
| 6457 | class_id_bytes[0], |
| 6458 | len_bytes[0], |
| 6459 | num_keys_bytes[0], |
| 6460 | 0, |
| 6461 | self.keys[0], |
| 6462 | self.keys[1], |
| 6463 | self.keys[2], |
| 6464 | self.keys[3], |
| 6465 | self.keys[4], |
| 6466 | self.keys[5], |
| 6467 | self.keys[6], |
| 6468 | self.keys[7], |
| 6469 | self.keys[8], |
| 6470 | self.keys[9], |
| 6471 | self.keys[10], |
| 6472 | self.keys[11], |
| 6473 | self.keys[12], |
| 6474 | self.keys[13], |
| 6475 | self.keys[14], |
| 6476 | self.keys[15], |
| 6477 | self.keys[16], |
| 6478 | self.keys[17], |
| 6479 | self.keys[18], |
| 6480 | self.keys[19], |
| 6481 | self.keys[20], |
| 6482 | self.keys[21], |
| 6483 | self.keys[22], |
| 6484 | self.keys[23], |
| 6485 | self.keys[24], |
| 6486 | self.keys[25], |
| 6487 | self.keys[26], |
| 6488 | self.keys[27], |
| 6489 | self.keys[28], |
| 6490 | self.keys[29], |
| 6491 | self.keys[30], |
| 6492 | self.keys[31], |
| 6493 | ] |
| 6494 | } |
| 6495 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 6496 | bytes.reserve(36); |
| 6497 | u8::from(self.class_id).serialize_into(bytes); |
| 6498 | self.len.serialize_into(bytes); |
| 6499 | self.num_keys.serialize_into(bytes); |
| 6500 | bytes.extend_from_slice(&[0; 1]); |
| 6501 | bytes.extend_from_slice(&self.keys); |
| 6502 | } |
| 6503 | } |
| 6504 | |
| 6505 | #[derive (Clone, Copy, Default)] |
| 6506 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 6507 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6508 | pub struct ButtonState { |
| 6509 | pub class_id: InputClass, |
| 6510 | pub len: u8, |
| 6511 | pub num_buttons: u8, |
| 6512 | pub buttons: [u8; 32], |
| 6513 | } |
| 6514 | impl_debug_if_no_extra_traits!(ButtonState, "ButtonState" ); |
| 6515 | impl TryParse for ButtonState { |
| 6516 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 6517 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6518 | let (len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6519 | let (num_buttons: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6520 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 6521 | let (buttons: [u8; 32], remaining: &[u8]) = crate::x11_utils::parse_u8_array::<32>(data:remaining)?; |
| 6522 | let class_id: InputClass = class_id.into(); |
| 6523 | let result: ButtonState = ButtonState { class_id, len, num_buttons, buttons }; |
| 6524 | Ok((result, remaining)) |
| 6525 | } |
| 6526 | } |
| 6527 | impl Serialize for ButtonState { |
| 6528 | type Bytes = [u8; 36]; |
| 6529 | fn serialize(&self) -> [u8; 36] { |
| 6530 | let class_id_bytes = u8::from(self.class_id).serialize(); |
| 6531 | let len_bytes = self.len.serialize(); |
| 6532 | let num_buttons_bytes = self.num_buttons.serialize(); |
| 6533 | [ |
| 6534 | class_id_bytes[0], |
| 6535 | len_bytes[0], |
| 6536 | num_buttons_bytes[0], |
| 6537 | 0, |
| 6538 | self.buttons[0], |
| 6539 | self.buttons[1], |
| 6540 | self.buttons[2], |
| 6541 | self.buttons[3], |
| 6542 | self.buttons[4], |
| 6543 | self.buttons[5], |
| 6544 | self.buttons[6], |
| 6545 | self.buttons[7], |
| 6546 | self.buttons[8], |
| 6547 | self.buttons[9], |
| 6548 | self.buttons[10], |
| 6549 | self.buttons[11], |
| 6550 | self.buttons[12], |
| 6551 | self.buttons[13], |
| 6552 | self.buttons[14], |
| 6553 | self.buttons[15], |
| 6554 | self.buttons[16], |
| 6555 | self.buttons[17], |
| 6556 | self.buttons[18], |
| 6557 | self.buttons[19], |
| 6558 | self.buttons[20], |
| 6559 | self.buttons[21], |
| 6560 | self.buttons[22], |
| 6561 | self.buttons[23], |
| 6562 | self.buttons[24], |
| 6563 | self.buttons[25], |
| 6564 | self.buttons[26], |
| 6565 | self.buttons[27], |
| 6566 | self.buttons[28], |
| 6567 | self.buttons[29], |
| 6568 | self.buttons[30], |
| 6569 | self.buttons[31], |
| 6570 | ] |
| 6571 | } |
| 6572 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 6573 | bytes.reserve(36); |
| 6574 | u8::from(self.class_id).serialize_into(bytes); |
| 6575 | self.len.serialize_into(bytes); |
| 6576 | self.num_buttons.serialize_into(bytes); |
| 6577 | bytes.extend_from_slice(&[0; 1]); |
| 6578 | bytes.extend_from_slice(&self.buttons); |
| 6579 | } |
| 6580 | } |
| 6581 | |
| 6582 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 6583 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6584 | pub struct ValuatorStateModeMask(u8); |
| 6585 | impl ValuatorStateModeMask { |
| 6586 | pub const DEVICE_MODE_ABSOLUTE: Self = Self(1 << 0); |
| 6587 | pub const OUT_OF_PROXIMITY: Self = Self(1 << 1); |
| 6588 | } |
| 6589 | impl From<ValuatorStateModeMask> for u8 { |
| 6590 | #[inline ] |
| 6591 | fn from(input: ValuatorStateModeMask) -> Self { |
| 6592 | input.0 |
| 6593 | } |
| 6594 | } |
| 6595 | impl From<ValuatorStateModeMask> for Option<u8> { |
| 6596 | #[inline ] |
| 6597 | fn from(input: ValuatorStateModeMask) -> Self { |
| 6598 | Some(input.0) |
| 6599 | } |
| 6600 | } |
| 6601 | impl From<ValuatorStateModeMask> for u16 { |
| 6602 | #[inline ] |
| 6603 | fn from(input: ValuatorStateModeMask) -> Self { |
| 6604 | u16::from(input.0) |
| 6605 | } |
| 6606 | } |
| 6607 | impl From<ValuatorStateModeMask> for Option<u16> { |
| 6608 | #[inline ] |
| 6609 | fn from(input: ValuatorStateModeMask) -> Self { |
| 6610 | Some(u16::from(input.0)) |
| 6611 | } |
| 6612 | } |
| 6613 | impl From<ValuatorStateModeMask> for u32 { |
| 6614 | #[inline ] |
| 6615 | fn from(input: ValuatorStateModeMask) -> Self { |
| 6616 | u32::from(input.0) |
| 6617 | } |
| 6618 | } |
| 6619 | impl From<ValuatorStateModeMask> for Option<u32> { |
| 6620 | #[inline ] |
| 6621 | fn from(input: ValuatorStateModeMask) -> Self { |
| 6622 | Some(u32::from(input.0)) |
| 6623 | } |
| 6624 | } |
| 6625 | impl From<u8> for ValuatorStateModeMask { |
| 6626 | #[inline ] |
| 6627 | fn from(value: u8) -> Self { |
| 6628 | Self(value) |
| 6629 | } |
| 6630 | } |
| 6631 | impl core::fmt::Debug for ValuatorStateModeMask { |
| 6632 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 6633 | let variants: [(u32, &'static str, &'static …); 2] = [ |
| 6634 | (Self::DEVICE_MODE_ABSOLUTE.0.into(), "DEVICE_MODE_ABSOLUTE" , "DeviceModeAbsolute" ), |
| 6635 | (Self::OUT_OF_PROXIMITY.0.into(), "OUT_OF_PROXIMITY" , "OutOfProximity" ), |
| 6636 | ]; |
| 6637 | pretty_print_bitmask(fmt, self.0.into(), &variants) |
| 6638 | } |
| 6639 | } |
| 6640 | bitmask_binop!(ValuatorStateModeMask, u8); |
| 6641 | |
| 6642 | #[derive (Clone, Default)] |
| 6643 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 6644 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6645 | pub struct ValuatorState { |
| 6646 | pub class_id: InputClass, |
| 6647 | pub len: u8, |
| 6648 | pub mode: ValuatorStateModeMask, |
| 6649 | pub valuators: Vec<i32>, |
| 6650 | } |
| 6651 | impl_debug_if_no_extra_traits!(ValuatorState, "ValuatorState" ); |
| 6652 | impl TryParse for ValuatorState { |
| 6653 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 6654 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6655 | let (len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6656 | let (num_valuators: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6657 | let (mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6658 | let (valuators: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<i32>(data:remaining, list_length:num_valuators.try_to_usize()?)?; |
| 6659 | let class_id: InputClass = class_id.into(); |
| 6660 | let mode: ValuatorStateModeMask = mode.into(); |
| 6661 | let result: ValuatorState = ValuatorState { class_id, len, mode, valuators }; |
| 6662 | Ok((result, remaining)) |
| 6663 | } |
| 6664 | } |
| 6665 | impl Serialize for ValuatorState { |
| 6666 | type Bytes = Vec<u8>; |
| 6667 | fn serialize(&self) -> Vec<u8> { |
| 6668 | let mut result: Vec = Vec::new(); |
| 6669 | self.serialize_into(&mut result); |
| 6670 | result |
| 6671 | } |
| 6672 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 6673 | bytes.reserve(additional:4); |
| 6674 | u8::from(self.class_id).serialize_into(bytes); |
| 6675 | self.len.serialize_into(bytes); |
| 6676 | let num_valuators: u8 = u8::try_from(self.valuators.len()).expect(msg:"`valuators` has too many elements" ); |
| 6677 | num_valuators.serialize_into(bytes); |
| 6678 | u8::from(self.mode).serialize_into(bytes); |
| 6679 | self.valuators.serialize_into(bytes); |
| 6680 | } |
| 6681 | } |
| 6682 | impl ValuatorState { |
| 6683 | /// Get the value of the `num_valuators` field. |
| 6684 | /// |
| 6685 | /// The `num_valuators` field is used as the length field of the `valuators` field. |
| 6686 | /// This function computes the field's value again based on the length of the list. |
| 6687 | /// |
| 6688 | /// # Panics |
| 6689 | /// |
| 6690 | /// Panics if the value cannot be represented in the target type. This |
| 6691 | /// cannot happen with values of the struct received from the X11 server. |
| 6692 | pub fn num_valuators(&self) -> u8 { |
| 6693 | self.valuators.len() |
| 6694 | .try_into().unwrap() |
| 6695 | } |
| 6696 | } |
| 6697 | |
| 6698 | #[derive (Clone, Copy)] |
| 6699 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 6700 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6701 | pub struct InputStateDataKey { |
| 6702 | pub num_keys: u8, |
| 6703 | pub keys: [u8; 32], |
| 6704 | } |
| 6705 | impl_debug_if_no_extra_traits!(InputStateDataKey, "InputStateDataKey" ); |
| 6706 | impl TryParse for InputStateDataKey { |
| 6707 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 6708 | let (num_keys: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6709 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 6710 | let (keys: [u8; 32], remaining: &[u8]) = crate::x11_utils::parse_u8_array::<32>(data:remaining)?; |
| 6711 | let result: InputStateDataKey = InputStateDataKey { num_keys, keys }; |
| 6712 | Ok((result, remaining)) |
| 6713 | } |
| 6714 | } |
| 6715 | impl Serialize for InputStateDataKey { |
| 6716 | type Bytes = [u8; 34]; |
| 6717 | fn serialize(&self) -> [u8; 34] { |
| 6718 | let num_keys_bytes = self.num_keys.serialize(); |
| 6719 | [ |
| 6720 | num_keys_bytes[0], |
| 6721 | 0, |
| 6722 | self.keys[0], |
| 6723 | self.keys[1], |
| 6724 | self.keys[2], |
| 6725 | self.keys[3], |
| 6726 | self.keys[4], |
| 6727 | self.keys[5], |
| 6728 | self.keys[6], |
| 6729 | self.keys[7], |
| 6730 | self.keys[8], |
| 6731 | self.keys[9], |
| 6732 | self.keys[10], |
| 6733 | self.keys[11], |
| 6734 | self.keys[12], |
| 6735 | self.keys[13], |
| 6736 | self.keys[14], |
| 6737 | self.keys[15], |
| 6738 | self.keys[16], |
| 6739 | self.keys[17], |
| 6740 | self.keys[18], |
| 6741 | self.keys[19], |
| 6742 | self.keys[20], |
| 6743 | self.keys[21], |
| 6744 | self.keys[22], |
| 6745 | self.keys[23], |
| 6746 | self.keys[24], |
| 6747 | self.keys[25], |
| 6748 | self.keys[26], |
| 6749 | self.keys[27], |
| 6750 | self.keys[28], |
| 6751 | self.keys[29], |
| 6752 | self.keys[30], |
| 6753 | self.keys[31], |
| 6754 | ] |
| 6755 | } |
| 6756 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 6757 | bytes.reserve(34); |
| 6758 | self.num_keys.serialize_into(bytes); |
| 6759 | bytes.extend_from_slice(&[0; 1]); |
| 6760 | bytes.extend_from_slice(&self.keys); |
| 6761 | } |
| 6762 | } |
| 6763 | #[derive (Clone, Copy)] |
| 6764 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 6765 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6766 | pub struct InputStateDataButton { |
| 6767 | pub num_buttons: u8, |
| 6768 | pub buttons: [u8; 32], |
| 6769 | } |
| 6770 | impl_debug_if_no_extra_traits!(InputStateDataButton, "InputStateDataButton" ); |
| 6771 | impl TryParse for InputStateDataButton { |
| 6772 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 6773 | let (num_buttons: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6774 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 6775 | let (buttons: [u8; 32], remaining: &[u8]) = crate::x11_utils::parse_u8_array::<32>(data:remaining)?; |
| 6776 | let result: InputStateDataButton = InputStateDataButton { num_buttons, buttons }; |
| 6777 | Ok((result, remaining)) |
| 6778 | } |
| 6779 | } |
| 6780 | impl Serialize for InputStateDataButton { |
| 6781 | type Bytes = [u8; 34]; |
| 6782 | fn serialize(&self) -> [u8; 34] { |
| 6783 | let num_buttons_bytes = self.num_buttons.serialize(); |
| 6784 | [ |
| 6785 | num_buttons_bytes[0], |
| 6786 | 0, |
| 6787 | self.buttons[0], |
| 6788 | self.buttons[1], |
| 6789 | self.buttons[2], |
| 6790 | self.buttons[3], |
| 6791 | self.buttons[4], |
| 6792 | self.buttons[5], |
| 6793 | self.buttons[6], |
| 6794 | self.buttons[7], |
| 6795 | self.buttons[8], |
| 6796 | self.buttons[9], |
| 6797 | self.buttons[10], |
| 6798 | self.buttons[11], |
| 6799 | self.buttons[12], |
| 6800 | self.buttons[13], |
| 6801 | self.buttons[14], |
| 6802 | self.buttons[15], |
| 6803 | self.buttons[16], |
| 6804 | self.buttons[17], |
| 6805 | self.buttons[18], |
| 6806 | self.buttons[19], |
| 6807 | self.buttons[20], |
| 6808 | self.buttons[21], |
| 6809 | self.buttons[22], |
| 6810 | self.buttons[23], |
| 6811 | self.buttons[24], |
| 6812 | self.buttons[25], |
| 6813 | self.buttons[26], |
| 6814 | self.buttons[27], |
| 6815 | self.buttons[28], |
| 6816 | self.buttons[29], |
| 6817 | self.buttons[30], |
| 6818 | self.buttons[31], |
| 6819 | ] |
| 6820 | } |
| 6821 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 6822 | bytes.reserve(34); |
| 6823 | self.num_buttons.serialize_into(bytes); |
| 6824 | bytes.extend_from_slice(&[0; 1]); |
| 6825 | bytes.extend_from_slice(&self.buttons); |
| 6826 | } |
| 6827 | } |
| 6828 | #[derive (Clone)] |
| 6829 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 6830 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6831 | pub struct InputStateDataValuator { |
| 6832 | pub mode: ValuatorStateModeMask, |
| 6833 | pub valuators: Vec<i32>, |
| 6834 | } |
| 6835 | impl_debug_if_no_extra_traits!(InputStateDataValuator, "InputStateDataValuator" ); |
| 6836 | impl TryParse for InputStateDataValuator { |
| 6837 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 6838 | let (num_valuators: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6839 | let (mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6840 | let (valuators: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<i32>(data:remaining, list_length:num_valuators.try_to_usize()?)?; |
| 6841 | let mode: ValuatorStateModeMask = mode.into(); |
| 6842 | let result: InputStateDataValuator = InputStateDataValuator { mode, valuators }; |
| 6843 | Ok((result, remaining)) |
| 6844 | } |
| 6845 | } |
| 6846 | impl Serialize for InputStateDataValuator { |
| 6847 | type Bytes = Vec<u8>; |
| 6848 | fn serialize(&self) -> Vec<u8> { |
| 6849 | let mut result: Vec = Vec::new(); |
| 6850 | self.serialize_into(&mut result); |
| 6851 | result |
| 6852 | } |
| 6853 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 6854 | bytes.reserve(additional:2); |
| 6855 | let num_valuators: u8 = u8::try_from(self.valuators.len()).expect(msg:"`valuators` has too many elements" ); |
| 6856 | num_valuators.serialize_into(bytes); |
| 6857 | u8::from(self.mode).serialize_into(bytes); |
| 6858 | self.valuators.serialize_into(bytes); |
| 6859 | } |
| 6860 | } |
| 6861 | impl InputStateDataValuator { |
| 6862 | /// Get the value of the `num_valuators` field. |
| 6863 | /// |
| 6864 | /// The `num_valuators` field is used as the length field of the `valuators` field. |
| 6865 | /// This function computes the field's value again based on the length of the list. |
| 6866 | /// |
| 6867 | /// # Panics |
| 6868 | /// |
| 6869 | /// Panics if the value cannot be represented in the target type. This |
| 6870 | /// cannot happen with values of the struct received from the X11 server. |
| 6871 | pub fn num_valuators(&self) -> u8 { |
| 6872 | self.valuators.len() |
| 6873 | .try_into().unwrap() |
| 6874 | } |
| 6875 | } |
| 6876 | #[derive (Clone)] |
| 6877 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 6878 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6879 | pub enum InputStateData { |
| 6880 | Key(InputStateDataKey), |
| 6881 | Button(InputStateDataButton), |
| 6882 | Valuator(InputStateDataValuator), |
| 6883 | /// This variant is returned when the server sends a discriminant |
| 6884 | /// value that does not match any of the defined by the protocol. |
| 6885 | /// |
| 6886 | /// Usually, this should be considered a parsing error, but there |
| 6887 | /// are some cases where the server violates the protocol. |
| 6888 | /// |
| 6889 | /// Trying to use `serialize` or `serialize_into` with this variant |
| 6890 | /// will raise a panic. |
| 6891 | InvalidValue(u8), |
| 6892 | } |
| 6893 | impl_debug_if_no_extra_traits!(InputStateData, "InputStateData" ); |
| 6894 | impl InputStateData { |
| 6895 | #[cfg_attr (not(feature = "request-parsing" ), allow(dead_code))] |
| 6896 | fn try_parse(value: &[u8], class_id: u8) -> Result<(Self, &[u8]), ParseError> { |
| 6897 | let switch_expr = u8::from(class_id); |
| 6898 | let mut outer_remaining = value; |
| 6899 | let mut parse_result = None; |
| 6900 | if switch_expr == u8::from(InputClass::KEY) { |
| 6901 | let (key, new_remaining) = InputStateDataKey::try_parse(outer_remaining)?; |
| 6902 | outer_remaining = new_remaining; |
| 6903 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 6904 | parse_result = Some(InputStateData::Key(key)); |
| 6905 | } |
| 6906 | if switch_expr == u8::from(InputClass::BUTTON) { |
| 6907 | let (button, new_remaining) = InputStateDataButton::try_parse(outer_remaining)?; |
| 6908 | outer_remaining = new_remaining; |
| 6909 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 6910 | parse_result = Some(InputStateData::Button(button)); |
| 6911 | } |
| 6912 | if switch_expr == u8::from(InputClass::VALUATOR) { |
| 6913 | let (valuator, new_remaining) = InputStateDataValuator::try_parse(outer_remaining)?; |
| 6914 | outer_remaining = new_remaining; |
| 6915 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 6916 | parse_result = Some(InputStateData::Valuator(valuator)); |
| 6917 | } |
| 6918 | match parse_result { |
| 6919 | None => Ok((InputStateData::InvalidValue(switch_expr), &[])), |
| 6920 | Some(result) => Ok((result, outer_remaining)), |
| 6921 | } |
| 6922 | } |
| 6923 | } |
| 6924 | impl InputStateData { |
| 6925 | pub fn as_key(&self) -> Option<&InputStateDataKey> { |
| 6926 | match self { |
| 6927 | InputStateData::Key(value: &InputStateDataKey) => Some(value), |
| 6928 | _ => None, |
| 6929 | } |
| 6930 | } |
| 6931 | pub fn as_button(&self) -> Option<&InputStateDataButton> { |
| 6932 | match self { |
| 6933 | InputStateData::Button(value: &InputStateDataButton) => Some(value), |
| 6934 | _ => None, |
| 6935 | } |
| 6936 | } |
| 6937 | pub fn as_valuator(&self) -> Option<&InputStateDataValuator> { |
| 6938 | match self { |
| 6939 | InputStateData::Valuator(value: &InputStateDataValuator) => Some(value), |
| 6940 | _ => None, |
| 6941 | } |
| 6942 | } |
| 6943 | } |
| 6944 | impl InputStateData { |
| 6945 | #[allow (dead_code)] |
| 6946 | fn serialize(&self, class_id: u8) -> Vec<u8> { |
| 6947 | let mut result: Vec = Vec::new(); |
| 6948 | self.serialize_into(&mut result, class_id:u8::from(class_id)); |
| 6949 | result |
| 6950 | } |
| 6951 | fn serialize_into(&self, bytes: &mut Vec<u8>, class_id: u8) { |
| 6952 | assert_eq!(self.switch_expr(), u8::from(class_id), "switch `data` has an inconsistent discriminant" ); |
| 6953 | match self { |
| 6954 | InputStateData::Key(key: &InputStateDataKey) => key.serialize_into(bytes), |
| 6955 | InputStateData::Button(button: &InputStateDataButton) => button.serialize_into(bytes), |
| 6956 | InputStateData::Valuator(valuator: &InputStateDataValuator) => valuator.serialize_into(bytes), |
| 6957 | InputStateData::InvalidValue(_) => panic!("attempted to serialize invalid switch case" ), |
| 6958 | } |
| 6959 | } |
| 6960 | } |
| 6961 | impl InputStateData { |
| 6962 | fn switch_expr(&self) -> u8 { |
| 6963 | match self { |
| 6964 | InputStateData::Key(_) => u8::from(InputClass::KEY), |
| 6965 | InputStateData::Button(_) => u8::from(InputClass::BUTTON), |
| 6966 | InputStateData::Valuator(_) => u8::from(InputClass::VALUATOR), |
| 6967 | InputStateData::InvalidValue(switch_expr: &u8) => *switch_expr, |
| 6968 | } |
| 6969 | } |
| 6970 | } |
| 6971 | |
| 6972 | #[derive (Clone)] |
| 6973 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 6974 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 6975 | pub struct InputState { |
| 6976 | pub len: u8, |
| 6977 | pub data: InputStateData, |
| 6978 | } |
| 6979 | impl_debug_if_no_extra_traits!(InputState, "InputState" ); |
| 6980 | impl TryParse for InputState { |
| 6981 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 6982 | let (class_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6983 | let (len: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 6984 | let (data: InputStateData, remaining: &[u8]) = InputStateData::try_parse(value:remaining, class_id:u8::from(class_id))?; |
| 6985 | let result: InputState = InputState { len, data }; |
| 6986 | Ok((result, remaining)) |
| 6987 | } |
| 6988 | } |
| 6989 | impl Serialize for InputState { |
| 6990 | type Bytes = Vec<u8>; |
| 6991 | fn serialize(&self) -> Vec<u8> { |
| 6992 | let mut result: Vec = Vec::new(); |
| 6993 | self.serialize_into(&mut result); |
| 6994 | result |
| 6995 | } |
| 6996 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 6997 | bytes.reserve(additional:2); |
| 6998 | let class_id: u8 = self.data.switch_expr(); |
| 6999 | class_id.serialize_into(bytes); |
| 7000 | self.len.serialize_into(bytes); |
| 7001 | self.data.serialize_into(bytes, class_id:u8::from(class_id)); |
| 7002 | } |
| 7003 | } |
| 7004 | |
| 7005 | /// Opcode for the QueryDeviceState request |
| 7006 | pub const QUERY_DEVICE_STATE_REQUEST: u8 = 30; |
| 7007 | #[derive (Clone, Copy, Default)] |
| 7008 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7009 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7010 | pub struct QueryDeviceStateRequest { |
| 7011 | pub device_id: u8, |
| 7012 | } |
| 7013 | impl_debug_if_no_extra_traits!(QueryDeviceStateRequest, "QueryDeviceStateRequest" ); |
| 7014 | impl QueryDeviceStateRequest { |
| 7015 | /// Serialize this request into bytes for the provided connection |
| 7016 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 7017 | let length_so_far = 0; |
| 7018 | let device_id_bytes = self.device_id.serialize(); |
| 7019 | let mut request0 = vec![ |
| 7020 | major_opcode, |
| 7021 | QUERY_DEVICE_STATE_REQUEST, |
| 7022 | 0, |
| 7023 | 0, |
| 7024 | device_id_bytes[0], |
| 7025 | 0, |
| 7026 | 0, |
| 7027 | 0, |
| 7028 | ]; |
| 7029 | let length_so_far = length_so_far + request0.len(); |
| 7030 | assert_eq!(length_so_far % 4, 0); |
| 7031 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 7032 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 7033 | ([request0.into()], vec![]) |
| 7034 | } |
| 7035 | /// Parse this request given its header, its body, and any fds that go along with it |
| 7036 | #[cfg (feature = "request-parsing" )] |
| 7037 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 7038 | if header.minor_opcode != QUERY_DEVICE_STATE_REQUEST { |
| 7039 | return Err(ParseError::InvalidValue); |
| 7040 | } |
| 7041 | let (device_id, remaining) = u8::try_parse(value)?; |
| 7042 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 7043 | let _ = remaining; |
| 7044 | Ok(QueryDeviceStateRequest { |
| 7045 | device_id, |
| 7046 | }) |
| 7047 | } |
| 7048 | } |
| 7049 | impl Request for QueryDeviceStateRequest { |
| 7050 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 7051 | |
| 7052 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 7053 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 7054 | // Flatten the buffers into a single vector |
| 7055 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 7056 | (buf, fds) |
| 7057 | } |
| 7058 | } |
| 7059 | impl crate::x11_utils::ReplyRequest for QueryDeviceStateRequest { |
| 7060 | type Reply = QueryDeviceStateReply; |
| 7061 | } |
| 7062 | |
| 7063 | #[derive (Clone)] |
| 7064 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7065 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7066 | pub struct QueryDeviceStateReply { |
| 7067 | pub xi_reply_type: u8, |
| 7068 | pub sequence: u16, |
| 7069 | pub length: u32, |
| 7070 | pub classes: Vec<InputState>, |
| 7071 | } |
| 7072 | impl_debug_if_no_extra_traits!(QueryDeviceStateReply, "QueryDeviceStateReply" ); |
| 7073 | impl TryParse for QueryDeviceStateReply { |
| 7074 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 7075 | let remaining: &[u8] = initial_value; |
| 7076 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 7077 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 7078 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 7079 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7080 | let (num_classes: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 7081 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 7082 | let (classes: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<InputState>(data:remaining, list_length:num_classes.try_to_usize()?)?; |
| 7083 | if response_type != 1 { |
| 7084 | return Err(ParseError::InvalidValue); |
| 7085 | } |
| 7086 | let result: QueryDeviceStateReply = QueryDeviceStateReply { xi_reply_type, sequence, length, classes }; |
| 7087 | let _ = remaining; |
| 7088 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 7089 | .ok_or(err:ParseError::InsufficientData)?; |
| 7090 | Ok((result, remaining)) |
| 7091 | } |
| 7092 | } |
| 7093 | impl Serialize for QueryDeviceStateReply { |
| 7094 | type Bytes = Vec<u8>; |
| 7095 | fn serialize(&self) -> Vec<u8> { |
| 7096 | let mut result: Vec = Vec::new(); |
| 7097 | self.serialize_into(&mut result); |
| 7098 | result |
| 7099 | } |
| 7100 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 7101 | bytes.reserve(additional:32); |
| 7102 | let response_type_bytes: &[u8; 1] = &[1]; |
| 7103 | bytes.push(response_type_bytes[0]); |
| 7104 | self.xi_reply_type.serialize_into(bytes); |
| 7105 | self.sequence.serialize_into(bytes); |
| 7106 | self.length.serialize_into(bytes); |
| 7107 | let num_classes: u8 = u8::try_from(self.classes.len()).expect(msg:"`classes` has too many elements" ); |
| 7108 | num_classes.serialize_into(bytes); |
| 7109 | bytes.extend_from_slice(&[0; 23]); |
| 7110 | self.classes.serialize_into(bytes); |
| 7111 | } |
| 7112 | } |
| 7113 | impl QueryDeviceStateReply { |
| 7114 | /// Get the value of the `num_classes` field. |
| 7115 | /// |
| 7116 | /// The `num_classes` field is used as the length field of the `classes` field. |
| 7117 | /// This function computes the field's value again based on the length of the list. |
| 7118 | /// |
| 7119 | /// # Panics |
| 7120 | /// |
| 7121 | /// Panics if the value cannot be represented in the target type. This |
| 7122 | /// cannot happen with values of the struct received from the X11 server. |
| 7123 | pub fn num_classes(&self) -> u8 { |
| 7124 | self.classes.len() |
| 7125 | .try_into().unwrap() |
| 7126 | } |
| 7127 | } |
| 7128 | |
| 7129 | /// Opcode for the DeviceBell request |
| 7130 | pub const DEVICE_BELL_REQUEST: u8 = 32; |
| 7131 | #[derive (Clone, Copy, Default)] |
| 7132 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7133 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7134 | pub struct DeviceBellRequest { |
| 7135 | pub device_id: u8, |
| 7136 | pub feedback_id: u8, |
| 7137 | pub feedback_class: u8, |
| 7138 | pub percent: i8, |
| 7139 | } |
| 7140 | impl_debug_if_no_extra_traits!(DeviceBellRequest, "DeviceBellRequest" ); |
| 7141 | impl DeviceBellRequest { |
| 7142 | /// Serialize this request into bytes for the provided connection |
| 7143 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 7144 | let length_so_far = 0; |
| 7145 | let device_id_bytes = self.device_id.serialize(); |
| 7146 | let feedback_id_bytes = self.feedback_id.serialize(); |
| 7147 | let feedback_class_bytes = self.feedback_class.serialize(); |
| 7148 | let percent_bytes = self.percent.serialize(); |
| 7149 | let mut request0 = vec![ |
| 7150 | major_opcode, |
| 7151 | DEVICE_BELL_REQUEST, |
| 7152 | 0, |
| 7153 | 0, |
| 7154 | device_id_bytes[0], |
| 7155 | feedback_id_bytes[0], |
| 7156 | feedback_class_bytes[0], |
| 7157 | percent_bytes[0], |
| 7158 | ]; |
| 7159 | let length_so_far = length_so_far + request0.len(); |
| 7160 | assert_eq!(length_so_far % 4, 0); |
| 7161 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 7162 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 7163 | ([request0.into()], vec![]) |
| 7164 | } |
| 7165 | /// Parse this request given its header, its body, and any fds that go along with it |
| 7166 | #[cfg (feature = "request-parsing" )] |
| 7167 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 7168 | if header.minor_opcode != DEVICE_BELL_REQUEST { |
| 7169 | return Err(ParseError::InvalidValue); |
| 7170 | } |
| 7171 | let (device_id, remaining) = u8::try_parse(value)?; |
| 7172 | let (feedback_id, remaining) = u8::try_parse(remaining)?; |
| 7173 | let (feedback_class, remaining) = u8::try_parse(remaining)?; |
| 7174 | let (percent, remaining) = i8::try_parse(remaining)?; |
| 7175 | let _ = remaining; |
| 7176 | Ok(DeviceBellRequest { |
| 7177 | device_id, |
| 7178 | feedback_id, |
| 7179 | feedback_class, |
| 7180 | percent, |
| 7181 | }) |
| 7182 | } |
| 7183 | } |
| 7184 | impl Request for DeviceBellRequest { |
| 7185 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 7186 | |
| 7187 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 7188 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 7189 | // Flatten the buffers into a single vector |
| 7190 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 7191 | (buf, fds) |
| 7192 | } |
| 7193 | } |
| 7194 | impl crate::x11_utils::VoidRequest for DeviceBellRequest { |
| 7195 | } |
| 7196 | |
| 7197 | /// Opcode for the SetDeviceValuators request |
| 7198 | pub const SET_DEVICE_VALUATORS_REQUEST: u8 = 33; |
| 7199 | #[derive (Clone, Default)] |
| 7200 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7201 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7202 | pub struct SetDeviceValuatorsRequest<'input> { |
| 7203 | pub device_id: u8, |
| 7204 | pub first_valuator: u8, |
| 7205 | pub valuators: Cow<'input, [i32]>, |
| 7206 | } |
| 7207 | impl_debug_if_no_extra_traits!(SetDeviceValuatorsRequest<'_>, "SetDeviceValuatorsRequest" ); |
| 7208 | impl<'input> SetDeviceValuatorsRequest<'input> { |
| 7209 | /// Serialize this request into bytes for the provided connection |
| 7210 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 7211 | let length_so_far = 0; |
| 7212 | let device_id_bytes = self.device_id.serialize(); |
| 7213 | let first_valuator_bytes = self.first_valuator.serialize(); |
| 7214 | let num_valuators = u8::try_from(self.valuators.len()).expect("`valuators` has too many elements" ); |
| 7215 | let num_valuators_bytes = num_valuators.serialize(); |
| 7216 | let mut request0 = vec![ |
| 7217 | major_opcode, |
| 7218 | SET_DEVICE_VALUATORS_REQUEST, |
| 7219 | 0, |
| 7220 | 0, |
| 7221 | device_id_bytes[0], |
| 7222 | first_valuator_bytes[0], |
| 7223 | num_valuators_bytes[0], |
| 7224 | 0, |
| 7225 | ]; |
| 7226 | let length_so_far = length_so_far + request0.len(); |
| 7227 | let valuators_bytes = self.valuators.serialize(); |
| 7228 | let length_so_far = length_so_far + valuators_bytes.len(); |
| 7229 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 7230 | let length_so_far = length_so_far + padding0.len(); |
| 7231 | assert_eq!(length_so_far % 4, 0); |
| 7232 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 7233 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 7234 | ([request0.into(), valuators_bytes.into(), padding0.into()], vec![]) |
| 7235 | } |
| 7236 | /// Parse this request given its header, its body, and any fds that go along with it |
| 7237 | #[cfg (feature = "request-parsing" )] |
| 7238 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 7239 | if header.minor_opcode != SET_DEVICE_VALUATORS_REQUEST { |
| 7240 | return Err(ParseError::InvalidValue); |
| 7241 | } |
| 7242 | let (device_id, remaining) = u8::try_parse(value)?; |
| 7243 | let (first_valuator, remaining) = u8::try_parse(remaining)?; |
| 7244 | let (num_valuators, remaining) = u8::try_parse(remaining)?; |
| 7245 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
| 7246 | let (valuators, remaining) = crate::x11_utils::parse_list::<i32>(remaining, num_valuators.try_to_usize()?)?; |
| 7247 | let _ = remaining; |
| 7248 | Ok(SetDeviceValuatorsRequest { |
| 7249 | device_id, |
| 7250 | first_valuator, |
| 7251 | valuators: Cow::Owned(valuators), |
| 7252 | }) |
| 7253 | } |
| 7254 | /// Clone all borrowed data in this SetDeviceValuatorsRequest. |
| 7255 | pub fn into_owned(self) -> SetDeviceValuatorsRequest<'static> { |
| 7256 | SetDeviceValuatorsRequest { |
| 7257 | device_id: self.device_id, |
| 7258 | first_valuator: self.first_valuator, |
| 7259 | valuators: Cow::Owned(self.valuators.into_owned()), |
| 7260 | } |
| 7261 | } |
| 7262 | } |
| 7263 | impl<'input> Request for SetDeviceValuatorsRequest<'input> { |
| 7264 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 7265 | |
| 7266 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 7267 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 7268 | // Flatten the buffers into a single vector |
| 7269 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 7270 | (buf, fds) |
| 7271 | } |
| 7272 | } |
| 7273 | impl<'input> crate::x11_utils::ReplyRequest for SetDeviceValuatorsRequest<'input> { |
| 7274 | type Reply = SetDeviceValuatorsReply; |
| 7275 | } |
| 7276 | |
| 7277 | #[derive (Clone, Copy, Default)] |
| 7278 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7279 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7280 | pub struct SetDeviceValuatorsReply { |
| 7281 | pub xi_reply_type: u8, |
| 7282 | pub sequence: u16, |
| 7283 | pub length: u32, |
| 7284 | pub status: xproto::GrabStatus, |
| 7285 | } |
| 7286 | impl_debug_if_no_extra_traits!(SetDeviceValuatorsReply, "SetDeviceValuatorsReply" ); |
| 7287 | impl TryParse for SetDeviceValuatorsReply { |
| 7288 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 7289 | let remaining: &[u8] = initial_value; |
| 7290 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 7291 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 7292 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 7293 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7294 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 7295 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 7296 | if response_type != 1 { |
| 7297 | return Err(ParseError::InvalidValue); |
| 7298 | } |
| 7299 | let status: GrabStatus = status.into(); |
| 7300 | let result: SetDeviceValuatorsReply = SetDeviceValuatorsReply { xi_reply_type, sequence, length, status }; |
| 7301 | let _ = remaining; |
| 7302 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 7303 | .ok_or(err:ParseError::InsufficientData)?; |
| 7304 | Ok((result, remaining)) |
| 7305 | } |
| 7306 | } |
| 7307 | impl Serialize for SetDeviceValuatorsReply { |
| 7308 | type Bytes = [u8; 32]; |
| 7309 | fn serialize(&self) -> [u8; 32] { |
| 7310 | let response_type_bytes = &[1]; |
| 7311 | let xi_reply_type_bytes = self.xi_reply_type.serialize(); |
| 7312 | let sequence_bytes = self.sequence.serialize(); |
| 7313 | let length_bytes = self.length.serialize(); |
| 7314 | let status_bytes = u8::from(self.status).serialize(); |
| 7315 | [ |
| 7316 | response_type_bytes[0], |
| 7317 | xi_reply_type_bytes[0], |
| 7318 | sequence_bytes[0], |
| 7319 | sequence_bytes[1], |
| 7320 | length_bytes[0], |
| 7321 | length_bytes[1], |
| 7322 | length_bytes[2], |
| 7323 | length_bytes[3], |
| 7324 | status_bytes[0], |
| 7325 | 0, |
| 7326 | 0, |
| 7327 | 0, |
| 7328 | 0, |
| 7329 | 0, |
| 7330 | 0, |
| 7331 | 0, |
| 7332 | 0, |
| 7333 | 0, |
| 7334 | 0, |
| 7335 | 0, |
| 7336 | 0, |
| 7337 | 0, |
| 7338 | 0, |
| 7339 | 0, |
| 7340 | 0, |
| 7341 | 0, |
| 7342 | 0, |
| 7343 | 0, |
| 7344 | 0, |
| 7345 | 0, |
| 7346 | 0, |
| 7347 | 0, |
| 7348 | ] |
| 7349 | } |
| 7350 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 7351 | bytes.reserve(32); |
| 7352 | let response_type_bytes = &[1]; |
| 7353 | bytes.push(response_type_bytes[0]); |
| 7354 | self.xi_reply_type.serialize_into(bytes); |
| 7355 | self.sequence.serialize_into(bytes); |
| 7356 | self.length.serialize_into(bytes); |
| 7357 | u8::from(self.status).serialize_into(bytes); |
| 7358 | bytes.extend_from_slice(&[0; 23]); |
| 7359 | } |
| 7360 | } |
| 7361 | |
| 7362 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 7363 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7364 | pub struct DeviceControl(u16); |
| 7365 | impl DeviceControl { |
| 7366 | pub const RESOLUTION: Self = Self(1); |
| 7367 | pub const ABSCALIB: Self = Self(2); |
| 7368 | pub const CORE: Self = Self(3); |
| 7369 | pub const ENABLE: Self = Self(4); |
| 7370 | pub const ABSAREA: Self = Self(5); |
| 7371 | } |
| 7372 | impl From<DeviceControl> for u16 { |
| 7373 | #[inline ] |
| 7374 | fn from(input: DeviceControl) -> Self { |
| 7375 | input.0 |
| 7376 | } |
| 7377 | } |
| 7378 | impl From<DeviceControl> for Option<u16> { |
| 7379 | #[inline ] |
| 7380 | fn from(input: DeviceControl) -> Self { |
| 7381 | Some(input.0) |
| 7382 | } |
| 7383 | } |
| 7384 | impl From<DeviceControl> for u32 { |
| 7385 | #[inline ] |
| 7386 | fn from(input: DeviceControl) -> Self { |
| 7387 | u32::from(input.0) |
| 7388 | } |
| 7389 | } |
| 7390 | impl From<DeviceControl> for Option<u32> { |
| 7391 | #[inline ] |
| 7392 | fn from(input: DeviceControl) -> Self { |
| 7393 | Some(u32::from(input.0)) |
| 7394 | } |
| 7395 | } |
| 7396 | impl From<u8> for DeviceControl { |
| 7397 | #[inline ] |
| 7398 | fn from(value: u8) -> Self { |
| 7399 | Self(value.into()) |
| 7400 | } |
| 7401 | } |
| 7402 | impl From<u16> for DeviceControl { |
| 7403 | #[inline ] |
| 7404 | fn from(value: u16) -> Self { |
| 7405 | Self(value) |
| 7406 | } |
| 7407 | } |
| 7408 | impl core::fmt::Debug for DeviceControl { |
| 7409 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 7410 | let variants: [(u32, &'static str, &'static …); 5] = [ |
| 7411 | (Self::RESOLUTION.0.into(), "RESOLUTION" , "Resolution" ), |
| 7412 | (Self::ABSCALIB.0.into(), "ABSCALIB" , "Abscalib" ), |
| 7413 | (Self::CORE.0.into(), "CORE" , "Core" ), |
| 7414 | (Self::ENABLE.0.into(), "ENABLE" , "Enable" ), |
| 7415 | (Self::ABSAREA.0.into(), "ABSAREA" , "Absarea" ), |
| 7416 | ]; |
| 7417 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 7418 | } |
| 7419 | } |
| 7420 | |
| 7421 | #[derive (Clone, Default)] |
| 7422 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7423 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7424 | pub struct DeviceResolutionState { |
| 7425 | pub control_id: DeviceControl, |
| 7426 | pub len: u16, |
| 7427 | pub resolution_values: Vec<u32>, |
| 7428 | pub resolution_min: Vec<u32>, |
| 7429 | pub resolution_max: Vec<u32>, |
| 7430 | } |
| 7431 | impl_debug_if_no_extra_traits!(DeviceResolutionState, "DeviceResolutionState" ); |
| 7432 | impl TryParse for DeviceResolutionState { |
| 7433 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 7434 | let (control_id: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 7435 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 7436 | let (num_valuators: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7437 | let (resolution_values: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:num_valuators.try_to_usize()?)?; |
| 7438 | let (resolution_min: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:num_valuators.try_to_usize()?)?; |
| 7439 | let (resolution_max: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:num_valuators.try_to_usize()?)?; |
| 7440 | let control_id: DeviceControl = control_id.into(); |
| 7441 | let result: DeviceResolutionState = DeviceResolutionState { control_id, len, resolution_values, resolution_min, resolution_max }; |
| 7442 | Ok((result, remaining)) |
| 7443 | } |
| 7444 | } |
| 7445 | impl Serialize for DeviceResolutionState { |
| 7446 | type Bytes = Vec<u8>; |
| 7447 | fn serialize(&self) -> Vec<u8> { |
| 7448 | let mut result: Vec = Vec::new(); |
| 7449 | self.serialize_into(&mut result); |
| 7450 | result |
| 7451 | } |
| 7452 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 7453 | bytes.reserve(additional:8); |
| 7454 | u16::from(self.control_id).serialize_into(bytes); |
| 7455 | self.len.serialize_into(bytes); |
| 7456 | let num_valuators: u32 = u32::try_from(self.resolution_values.len()).expect(msg:"`resolution_values` has too many elements" ); |
| 7457 | num_valuators.serialize_into(bytes); |
| 7458 | self.resolution_values.serialize_into(bytes); |
| 7459 | assert_eq!(self.resolution_min.len(), usize::try_from(num_valuators).unwrap(), "`resolution_min` has an incorrect length" ); |
| 7460 | self.resolution_min.serialize_into(bytes); |
| 7461 | assert_eq!(self.resolution_max.len(), usize::try_from(num_valuators).unwrap(), "`resolution_max` has an incorrect length" ); |
| 7462 | self.resolution_max.serialize_into(bytes); |
| 7463 | } |
| 7464 | } |
| 7465 | impl DeviceResolutionState { |
| 7466 | /// Get the value of the `num_valuators` field. |
| 7467 | /// |
| 7468 | /// The `num_valuators` field is used as the length field of the `resolution_values` field. |
| 7469 | /// This function computes the field's value again based on the length of the list. |
| 7470 | /// |
| 7471 | /// # Panics |
| 7472 | /// |
| 7473 | /// Panics if the value cannot be represented in the target type. This |
| 7474 | /// cannot happen with values of the struct received from the X11 server. |
| 7475 | pub fn num_valuators(&self) -> u32 { |
| 7476 | self.resolution_values.len() |
| 7477 | .try_into().unwrap() |
| 7478 | } |
| 7479 | } |
| 7480 | |
| 7481 | #[derive (Clone, Copy, Default)] |
| 7482 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7483 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7484 | pub struct DeviceAbsCalibState { |
| 7485 | pub control_id: DeviceControl, |
| 7486 | pub len: u16, |
| 7487 | pub min_x: i32, |
| 7488 | pub max_x: i32, |
| 7489 | pub min_y: i32, |
| 7490 | pub max_y: i32, |
| 7491 | pub flip_x: u32, |
| 7492 | pub flip_y: u32, |
| 7493 | pub rotation: u32, |
| 7494 | pub button_threshold: u32, |
| 7495 | } |
| 7496 | impl_debug_if_no_extra_traits!(DeviceAbsCalibState, "DeviceAbsCalibState" ); |
| 7497 | impl TryParse for DeviceAbsCalibState { |
| 7498 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 7499 | let (control_id: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 7500 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 7501 | let (min_x: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 7502 | let (max_x: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 7503 | let (min_y: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 7504 | let (max_y: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 7505 | let (flip_x: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7506 | let (flip_y: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7507 | let (rotation: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7508 | let (button_threshold: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7509 | let control_id: DeviceControl = control_id.into(); |
| 7510 | let result: DeviceAbsCalibState = DeviceAbsCalibState { control_id, len, min_x, max_x, min_y, max_y, flip_x, flip_y, rotation, button_threshold }; |
| 7511 | Ok((result, remaining)) |
| 7512 | } |
| 7513 | } |
| 7514 | impl Serialize for DeviceAbsCalibState { |
| 7515 | type Bytes = [u8; 36]; |
| 7516 | fn serialize(&self) -> [u8; 36] { |
| 7517 | let control_id_bytes = u16::from(self.control_id).serialize(); |
| 7518 | let len_bytes = self.len.serialize(); |
| 7519 | let min_x_bytes = self.min_x.serialize(); |
| 7520 | let max_x_bytes = self.max_x.serialize(); |
| 7521 | let min_y_bytes = self.min_y.serialize(); |
| 7522 | let max_y_bytes = self.max_y.serialize(); |
| 7523 | let flip_x_bytes = self.flip_x.serialize(); |
| 7524 | let flip_y_bytes = self.flip_y.serialize(); |
| 7525 | let rotation_bytes = self.rotation.serialize(); |
| 7526 | let button_threshold_bytes = self.button_threshold.serialize(); |
| 7527 | [ |
| 7528 | control_id_bytes[0], |
| 7529 | control_id_bytes[1], |
| 7530 | len_bytes[0], |
| 7531 | len_bytes[1], |
| 7532 | min_x_bytes[0], |
| 7533 | min_x_bytes[1], |
| 7534 | min_x_bytes[2], |
| 7535 | min_x_bytes[3], |
| 7536 | max_x_bytes[0], |
| 7537 | max_x_bytes[1], |
| 7538 | max_x_bytes[2], |
| 7539 | max_x_bytes[3], |
| 7540 | min_y_bytes[0], |
| 7541 | min_y_bytes[1], |
| 7542 | min_y_bytes[2], |
| 7543 | min_y_bytes[3], |
| 7544 | max_y_bytes[0], |
| 7545 | max_y_bytes[1], |
| 7546 | max_y_bytes[2], |
| 7547 | max_y_bytes[3], |
| 7548 | flip_x_bytes[0], |
| 7549 | flip_x_bytes[1], |
| 7550 | flip_x_bytes[2], |
| 7551 | flip_x_bytes[3], |
| 7552 | flip_y_bytes[0], |
| 7553 | flip_y_bytes[1], |
| 7554 | flip_y_bytes[2], |
| 7555 | flip_y_bytes[3], |
| 7556 | rotation_bytes[0], |
| 7557 | rotation_bytes[1], |
| 7558 | rotation_bytes[2], |
| 7559 | rotation_bytes[3], |
| 7560 | button_threshold_bytes[0], |
| 7561 | button_threshold_bytes[1], |
| 7562 | button_threshold_bytes[2], |
| 7563 | button_threshold_bytes[3], |
| 7564 | ] |
| 7565 | } |
| 7566 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 7567 | bytes.reserve(36); |
| 7568 | u16::from(self.control_id).serialize_into(bytes); |
| 7569 | self.len.serialize_into(bytes); |
| 7570 | self.min_x.serialize_into(bytes); |
| 7571 | self.max_x.serialize_into(bytes); |
| 7572 | self.min_y.serialize_into(bytes); |
| 7573 | self.max_y.serialize_into(bytes); |
| 7574 | self.flip_x.serialize_into(bytes); |
| 7575 | self.flip_y.serialize_into(bytes); |
| 7576 | self.rotation.serialize_into(bytes); |
| 7577 | self.button_threshold.serialize_into(bytes); |
| 7578 | } |
| 7579 | } |
| 7580 | |
| 7581 | #[derive (Clone, Copy, Default)] |
| 7582 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7583 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7584 | pub struct DeviceAbsAreaState { |
| 7585 | pub control_id: DeviceControl, |
| 7586 | pub len: u16, |
| 7587 | pub offset_x: u32, |
| 7588 | pub offset_y: u32, |
| 7589 | pub width: u32, |
| 7590 | pub height: u32, |
| 7591 | pub screen: u32, |
| 7592 | pub following: u32, |
| 7593 | } |
| 7594 | impl_debug_if_no_extra_traits!(DeviceAbsAreaState, "DeviceAbsAreaState" ); |
| 7595 | impl TryParse for DeviceAbsAreaState { |
| 7596 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 7597 | let (control_id: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 7598 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 7599 | let (offset_x: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7600 | let (offset_y: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7601 | let (width: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7602 | let (height: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7603 | let (screen: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7604 | let (following: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7605 | let control_id: DeviceControl = control_id.into(); |
| 7606 | let result: DeviceAbsAreaState = DeviceAbsAreaState { control_id, len, offset_x, offset_y, width, height, screen, following }; |
| 7607 | Ok((result, remaining)) |
| 7608 | } |
| 7609 | } |
| 7610 | impl Serialize for DeviceAbsAreaState { |
| 7611 | type Bytes = [u8; 28]; |
| 7612 | fn serialize(&self) -> [u8; 28] { |
| 7613 | let control_id_bytes = u16::from(self.control_id).serialize(); |
| 7614 | let len_bytes = self.len.serialize(); |
| 7615 | let offset_x_bytes = self.offset_x.serialize(); |
| 7616 | let offset_y_bytes = self.offset_y.serialize(); |
| 7617 | let width_bytes = self.width.serialize(); |
| 7618 | let height_bytes = self.height.serialize(); |
| 7619 | let screen_bytes = self.screen.serialize(); |
| 7620 | let following_bytes = self.following.serialize(); |
| 7621 | [ |
| 7622 | control_id_bytes[0], |
| 7623 | control_id_bytes[1], |
| 7624 | len_bytes[0], |
| 7625 | len_bytes[1], |
| 7626 | offset_x_bytes[0], |
| 7627 | offset_x_bytes[1], |
| 7628 | offset_x_bytes[2], |
| 7629 | offset_x_bytes[3], |
| 7630 | offset_y_bytes[0], |
| 7631 | offset_y_bytes[1], |
| 7632 | offset_y_bytes[2], |
| 7633 | offset_y_bytes[3], |
| 7634 | width_bytes[0], |
| 7635 | width_bytes[1], |
| 7636 | width_bytes[2], |
| 7637 | width_bytes[3], |
| 7638 | height_bytes[0], |
| 7639 | height_bytes[1], |
| 7640 | height_bytes[2], |
| 7641 | height_bytes[3], |
| 7642 | screen_bytes[0], |
| 7643 | screen_bytes[1], |
| 7644 | screen_bytes[2], |
| 7645 | screen_bytes[3], |
| 7646 | following_bytes[0], |
| 7647 | following_bytes[1], |
| 7648 | following_bytes[2], |
| 7649 | following_bytes[3], |
| 7650 | ] |
| 7651 | } |
| 7652 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 7653 | bytes.reserve(28); |
| 7654 | u16::from(self.control_id).serialize_into(bytes); |
| 7655 | self.len.serialize_into(bytes); |
| 7656 | self.offset_x.serialize_into(bytes); |
| 7657 | self.offset_y.serialize_into(bytes); |
| 7658 | self.width.serialize_into(bytes); |
| 7659 | self.height.serialize_into(bytes); |
| 7660 | self.screen.serialize_into(bytes); |
| 7661 | self.following.serialize_into(bytes); |
| 7662 | } |
| 7663 | } |
| 7664 | |
| 7665 | #[derive (Clone, Copy, Default)] |
| 7666 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7667 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7668 | pub struct DeviceCoreState { |
| 7669 | pub control_id: DeviceControl, |
| 7670 | pub len: u16, |
| 7671 | pub status: u8, |
| 7672 | pub iscore: u8, |
| 7673 | } |
| 7674 | impl_debug_if_no_extra_traits!(DeviceCoreState, "DeviceCoreState" ); |
| 7675 | impl TryParse for DeviceCoreState { |
| 7676 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 7677 | let (control_id: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 7678 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 7679 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 7680 | let (iscore: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 7681 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 7682 | let control_id: DeviceControl = control_id.into(); |
| 7683 | let result: DeviceCoreState = DeviceCoreState { control_id, len, status, iscore }; |
| 7684 | Ok((result, remaining)) |
| 7685 | } |
| 7686 | } |
| 7687 | impl Serialize for DeviceCoreState { |
| 7688 | type Bytes = [u8; 8]; |
| 7689 | fn serialize(&self) -> [u8; 8] { |
| 7690 | let control_id_bytes = u16::from(self.control_id).serialize(); |
| 7691 | let len_bytes = self.len.serialize(); |
| 7692 | let status_bytes = self.status.serialize(); |
| 7693 | let iscore_bytes = self.iscore.serialize(); |
| 7694 | [ |
| 7695 | control_id_bytes[0], |
| 7696 | control_id_bytes[1], |
| 7697 | len_bytes[0], |
| 7698 | len_bytes[1], |
| 7699 | status_bytes[0], |
| 7700 | iscore_bytes[0], |
| 7701 | 0, |
| 7702 | 0, |
| 7703 | ] |
| 7704 | } |
| 7705 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 7706 | bytes.reserve(8); |
| 7707 | u16::from(self.control_id).serialize_into(bytes); |
| 7708 | self.len.serialize_into(bytes); |
| 7709 | self.status.serialize_into(bytes); |
| 7710 | self.iscore.serialize_into(bytes); |
| 7711 | bytes.extend_from_slice(&[0; 2]); |
| 7712 | } |
| 7713 | } |
| 7714 | |
| 7715 | #[derive (Clone, Copy, Default)] |
| 7716 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7717 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7718 | pub struct DeviceEnableState { |
| 7719 | pub control_id: DeviceControl, |
| 7720 | pub len: u16, |
| 7721 | pub enable: u8, |
| 7722 | } |
| 7723 | impl_debug_if_no_extra_traits!(DeviceEnableState, "DeviceEnableState" ); |
| 7724 | impl TryParse for DeviceEnableState { |
| 7725 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 7726 | let (control_id: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 7727 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 7728 | let (enable: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 7729 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
| 7730 | let control_id: DeviceControl = control_id.into(); |
| 7731 | let result: DeviceEnableState = DeviceEnableState { control_id, len, enable }; |
| 7732 | Ok((result, remaining)) |
| 7733 | } |
| 7734 | } |
| 7735 | impl Serialize for DeviceEnableState { |
| 7736 | type Bytes = [u8; 8]; |
| 7737 | fn serialize(&self) -> [u8; 8] { |
| 7738 | let control_id_bytes = u16::from(self.control_id).serialize(); |
| 7739 | let len_bytes = self.len.serialize(); |
| 7740 | let enable_bytes = self.enable.serialize(); |
| 7741 | [ |
| 7742 | control_id_bytes[0], |
| 7743 | control_id_bytes[1], |
| 7744 | len_bytes[0], |
| 7745 | len_bytes[1], |
| 7746 | enable_bytes[0], |
| 7747 | 0, |
| 7748 | 0, |
| 7749 | 0, |
| 7750 | ] |
| 7751 | } |
| 7752 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 7753 | bytes.reserve(8); |
| 7754 | u16::from(self.control_id).serialize_into(bytes); |
| 7755 | self.len.serialize_into(bytes); |
| 7756 | self.enable.serialize_into(bytes); |
| 7757 | bytes.extend_from_slice(&[0; 3]); |
| 7758 | } |
| 7759 | } |
| 7760 | |
| 7761 | #[derive (Clone)] |
| 7762 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7763 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7764 | pub struct DeviceStateDataResolution { |
| 7765 | pub resolution_values: Vec<u32>, |
| 7766 | pub resolution_min: Vec<u32>, |
| 7767 | pub resolution_max: Vec<u32>, |
| 7768 | } |
| 7769 | impl_debug_if_no_extra_traits!(DeviceStateDataResolution, "DeviceStateDataResolution" ); |
| 7770 | impl TryParse for DeviceStateDataResolution { |
| 7771 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 7772 | let (num_valuators: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7773 | let (resolution_values: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:num_valuators.try_to_usize()?)?; |
| 7774 | let (resolution_min: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:num_valuators.try_to_usize()?)?; |
| 7775 | let (resolution_max: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:num_valuators.try_to_usize()?)?; |
| 7776 | let result: DeviceStateDataResolution = DeviceStateDataResolution { resolution_values, resolution_min, resolution_max }; |
| 7777 | Ok((result, remaining)) |
| 7778 | } |
| 7779 | } |
| 7780 | impl Serialize for DeviceStateDataResolution { |
| 7781 | type Bytes = Vec<u8>; |
| 7782 | fn serialize(&self) -> Vec<u8> { |
| 7783 | let mut result: Vec = Vec::new(); |
| 7784 | self.serialize_into(&mut result); |
| 7785 | result |
| 7786 | } |
| 7787 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 7788 | let num_valuators: u32 = u32::try_from(self.resolution_values.len()).expect(msg:"`resolution_values` has too many elements" ); |
| 7789 | num_valuators.serialize_into(bytes); |
| 7790 | self.resolution_values.serialize_into(bytes); |
| 7791 | assert_eq!(self.resolution_min.len(), usize::try_from(num_valuators).unwrap(), "`resolution_min` has an incorrect length" ); |
| 7792 | self.resolution_min.serialize_into(bytes); |
| 7793 | assert_eq!(self.resolution_max.len(), usize::try_from(num_valuators).unwrap(), "`resolution_max` has an incorrect length" ); |
| 7794 | self.resolution_max.serialize_into(bytes); |
| 7795 | } |
| 7796 | } |
| 7797 | impl DeviceStateDataResolution { |
| 7798 | /// Get the value of the `num_valuators` field. |
| 7799 | /// |
| 7800 | /// The `num_valuators` field is used as the length field of the `resolution_values` field. |
| 7801 | /// This function computes the field's value again based on the length of the list. |
| 7802 | /// |
| 7803 | /// # Panics |
| 7804 | /// |
| 7805 | /// Panics if the value cannot be represented in the target type. This |
| 7806 | /// cannot happen with values of the struct received from the X11 server. |
| 7807 | pub fn num_valuators(&self) -> u32 { |
| 7808 | self.resolution_values.len() |
| 7809 | .try_into().unwrap() |
| 7810 | } |
| 7811 | } |
| 7812 | #[derive (Clone, Copy)] |
| 7813 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7814 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7815 | pub struct DeviceStateDataAbsCalib { |
| 7816 | pub min_x: i32, |
| 7817 | pub max_x: i32, |
| 7818 | pub min_y: i32, |
| 7819 | pub max_y: i32, |
| 7820 | pub flip_x: u32, |
| 7821 | pub flip_y: u32, |
| 7822 | pub rotation: u32, |
| 7823 | pub button_threshold: u32, |
| 7824 | } |
| 7825 | impl_debug_if_no_extra_traits!(DeviceStateDataAbsCalib, "DeviceStateDataAbsCalib" ); |
| 7826 | impl TryParse for DeviceStateDataAbsCalib { |
| 7827 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 7828 | let (min_x: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 7829 | let (max_x: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 7830 | let (min_y: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 7831 | let (max_y: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 7832 | let (flip_x: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7833 | let (flip_y: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7834 | let (rotation: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7835 | let (button_threshold: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7836 | let result: DeviceStateDataAbsCalib = DeviceStateDataAbsCalib { min_x, max_x, min_y, max_y, flip_x, flip_y, rotation, button_threshold }; |
| 7837 | Ok((result, remaining)) |
| 7838 | } |
| 7839 | } |
| 7840 | impl Serialize for DeviceStateDataAbsCalib { |
| 7841 | type Bytes = [u8; 32]; |
| 7842 | fn serialize(&self) -> [u8; 32] { |
| 7843 | let min_x_bytes = self.min_x.serialize(); |
| 7844 | let max_x_bytes = self.max_x.serialize(); |
| 7845 | let min_y_bytes = self.min_y.serialize(); |
| 7846 | let max_y_bytes = self.max_y.serialize(); |
| 7847 | let flip_x_bytes = self.flip_x.serialize(); |
| 7848 | let flip_y_bytes = self.flip_y.serialize(); |
| 7849 | let rotation_bytes = self.rotation.serialize(); |
| 7850 | let button_threshold_bytes = self.button_threshold.serialize(); |
| 7851 | [ |
| 7852 | min_x_bytes[0], |
| 7853 | min_x_bytes[1], |
| 7854 | min_x_bytes[2], |
| 7855 | min_x_bytes[3], |
| 7856 | max_x_bytes[0], |
| 7857 | max_x_bytes[1], |
| 7858 | max_x_bytes[2], |
| 7859 | max_x_bytes[3], |
| 7860 | min_y_bytes[0], |
| 7861 | min_y_bytes[1], |
| 7862 | min_y_bytes[2], |
| 7863 | min_y_bytes[3], |
| 7864 | max_y_bytes[0], |
| 7865 | max_y_bytes[1], |
| 7866 | max_y_bytes[2], |
| 7867 | max_y_bytes[3], |
| 7868 | flip_x_bytes[0], |
| 7869 | flip_x_bytes[1], |
| 7870 | flip_x_bytes[2], |
| 7871 | flip_x_bytes[3], |
| 7872 | flip_y_bytes[0], |
| 7873 | flip_y_bytes[1], |
| 7874 | flip_y_bytes[2], |
| 7875 | flip_y_bytes[3], |
| 7876 | rotation_bytes[0], |
| 7877 | rotation_bytes[1], |
| 7878 | rotation_bytes[2], |
| 7879 | rotation_bytes[3], |
| 7880 | button_threshold_bytes[0], |
| 7881 | button_threshold_bytes[1], |
| 7882 | button_threshold_bytes[2], |
| 7883 | button_threshold_bytes[3], |
| 7884 | ] |
| 7885 | } |
| 7886 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 7887 | bytes.reserve(32); |
| 7888 | self.min_x.serialize_into(bytes); |
| 7889 | self.max_x.serialize_into(bytes); |
| 7890 | self.min_y.serialize_into(bytes); |
| 7891 | self.max_y.serialize_into(bytes); |
| 7892 | self.flip_x.serialize_into(bytes); |
| 7893 | self.flip_y.serialize_into(bytes); |
| 7894 | self.rotation.serialize_into(bytes); |
| 7895 | self.button_threshold.serialize_into(bytes); |
| 7896 | } |
| 7897 | } |
| 7898 | #[derive (Clone, Copy)] |
| 7899 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7900 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7901 | pub struct DeviceStateDataCore { |
| 7902 | pub status: u8, |
| 7903 | pub iscore: u8, |
| 7904 | } |
| 7905 | impl_debug_if_no_extra_traits!(DeviceStateDataCore, "DeviceStateDataCore" ); |
| 7906 | impl TryParse for DeviceStateDataCore { |
| 7907 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 7908 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 7909 | let (iscore: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 7910 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 7911 | let result: DeviceStateDataCore = DeviceStateDataCore { status, iscore }; |
| 7912 | Ok((result, remaining)) |
| 7913 | } |
| 7914 | } |
| 7915 | impl Serialize for DeviceStateDataCore { |
| 7916 | type Bytes = [u8; 4]; |
| 7917 | fn serialize(&self) -> [u8; 4] { |
| 7918 | let status_bytes: [u8; 1] = self.status.serialize(); |
| 7919 | let iscore_bytes: [u8; 1] = self.iscore.serialize(); |
| 7920 | [ |
| 7921 | status_bytes[0], |
| 7922 | iscore_bytes[0], |
| 7923 | 0, |
| 7924 | 0, |
| 7925 | ] |
| 7926 | } |
| 7927 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 7928 | bytes.reserve(additional:4); |
| 7929 | self.status.serialize_into(bytes); |
| 7930 | self.iscore.serialize_into(bytes); |
| 7931 | bytes.extend_from_slice(&[0; 2]); |
| 7932 | } |
| 7933 | } |
| 7934 | #[derive (Clone, Copy)] |
| 7935 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 7936 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7937 | pub struct DeviceStateDataAbsArea { |
| 7938 | pub offset_x: u32, |
| 7939 | pub offset_y: u32, |
| 7940 | pub width: u32, |
| 7941 | pub height: u32, |
| 7942 | pub screen: u32, |
| 7943 | pub following: u32, |
| 7944 | } |
| 7945 | impl_debug_if_no_extra_traits!(DeviceStateDataAbsArea, "DeviceStateDataAbsArea" ); |
| 7946 | impl TryParse for DeviceStateDataAbsArea { |
| 7947 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 7948 | let (offset_x: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7949 | let (offset_y: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7950 | let (width: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7951 | let (height: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7952 | let (screen: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7953 | let (following: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 7954 | let result: DeviceStateDataAbsArea = DeviceStateDataAbsArea { offset_x, offset_y, width, height, screen, following }; |
| 7955 | Ok((result, remaining)) |
| 7956 | } |
| 7957 | } |
| 7958 | impl Serialize for DeviceStateDataAbsArea { |
| 7959 | type Bytes = [u8; 24]; |
| 7960 | fn serialize(&self) -> [u8; 24] { |
| 7961 | let offset_x_bytes = self.offset_x.serialize(); |
| 7962 | let offset_y_bytes = self.offset_y.serialize(); |
| 7963 | let width_bytes = self.width.serialize(); |
| 7964 | let height_bytes = self.height.serialize(); |
| 7965 | let screen_bytes = self.screen.serialize(); |
| 7966 | let following_bytes = self.following.serialize(); |
| 7967 | [ |
| 7968 | offset_x_bytes[0], |
| 7969 | offset_x_bytes[1], |
| 7970 | offset_x_bytes[2], |
| 7971 | offset_x_bytes[3], |
| 7972 | offset_y_bytes[0], |
| 7973 | offset_y_bytes[1], |
| 7974 | offset_y_bytes[2], |
| 7975 | offset_y_bytes[3], |
| 7976 | width_bytes[0], |
| 7977 | width_bytes[1], |
| 7978 | width_bytes[2], |
| 7979 | width_bytes[3], |
| 7980 | height_bytes[0], |
| 7981 | height_bytes[1], |
| 7982 | height_bytes[2], |
| 7983 | height_bytes[3], |
| 7984 | screen_bytes[0], |
| 7985 | screen_bytes[1], |
| 7986 | screen_bytes[2], |
| 7987 | screen_bytes[3], |
| 7988 | following_bytes[0], |
| 7989 | following_bytes[1], |
| 7990 | following_bytes[2], |
| 7991 | following_bytes[3], |
| 7992 | ] |
| 7993 | } |
| 7994 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 7995 | bytes.reserve(24); |
| 7996 | self.offset_x.serialize_into(bytes); |
| 7997 | self.offset_y.serialize_into(bytes); |
| 7998 | self.width.serialize_into(bytes); |
| 7999 | self.height.serialize_into(bytes); |
| 8000 | self.screen.serialize_into(bytes); |
| 8001 | self.following.serialize_into(bytes); |
| 8002 | } |
| 8003 | } |
| 8004 | #[derive (Clone)] |
| 8005 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8006 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8007 | pub enum DeviceStateData { |
| 8008 | Resolution(DeviceStateDataResolution), |
| 8009 | AbsCalib(DeviceStateDataAbsCalib), |
| 8010 | Core(DeviceStateDataCore), |
| 8011 | Enable(u8), |
| 8012 | AbsArea(DeviceStateDataAbsArea), |
| 8013 | /// This variant is returned when the server sends a discriminant |
| 8014 | /// value that does not match any of the defined by the protocol. |
| 8015 | /// |
| 8016 | /// Usually, this should be considered a parsing error, but there |
| 8017 | /// are some cases where the server violates the protocol. |
| 8018 | /// |
| 8019 | /// Trying to use `serialize` or `serialize_into` with this variant |
| 8020 | /// will raise a panic. |
| 8021 | InvalidValue(u16), |
| 8022 | } |
| 8023 | impl_debug_if_no_extra_traits!(DeviceStateData, "DeviceStateData" ); |
| 8024 | impl DeviceStateData { |
| 8025 | #[cfg_attr (not(feature = "request-parsing" ), allow(dead_code))] |
| 8026 | fn try_parse(value: &[u8], control_id: u16) -> Result<(Self, &[u8]), ParseError> { |
| 8027 | let switch_expr = u16::from(control_id); |
| 8028 | let mut outer_remaining = value; |
| 8029 | let mut parse_result = None; |
| 8030 | if switch_expr == u16::from(DeviceControl::RESOLUTION) { |
| 8031 | let (resolution, new_remaining) = DeviceStateDataResolution::try_parse(outer_remaining)?; |
| 8032 | outer_remaining = new_remaining; |
| 8033 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 8034 | parse_result = Some(DeviceStateData::Resolution(resolution)); |
| 8035 | } |
| 8036 | if switch_expr == u16::from(DeviceControl::ABSCALIB) { |
| 8037 | let (abs_calib, new_remaining) = DeviceStateDataAbsCalib::try_parse(outer_remaining)?; |
| 8038 | outer_remaining = new_remaining; |
| 8039 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 8040 | parse_result = Some(DeviceStateData::AbsCalib(abs_calib)); |
| 8041 | } |
| 8042 | if switch_expr == u16::from(DeviceControl::CORE) { |
| 8043 | let (core, new_remaining) = DeviceStateDataCore::try_parse(outer_remaining)?; |
| 8044 | outer_remaining = new_remaining; |
| 8045 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 8046 | parse_result = Some(DeviceStateData::Core(core)); |
| 8047 | } |
| 8048 | if switch_expr == u16::from(DeviceControl::ENABLE) { |
| 8049 | let remaining = outer_remaining; |
| 8050 | let (enable, remaining) = u8::try_parse(remaining)?; |
| 8051 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 8052 | outer_remaining = remaining; |
| 8053 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 8054 | parse_result = Some(DeviceStateData::Enable(enable)); |
| 8055 | } |
| 8056 | if switch_expr == u16::from(DeviceControl::ABSAREA) { |
| 8057 | let (abs_area, new_remaining) = DeviceStateDataAbsArea::try_parse(outer_remaining)?; |
| 8058 | outer_remaining = new_remaining; |
| 8059 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 8060 | parse_result = Some(DeviceStateData::AbsArea(abs_area)); |
| 8061 | } |
| 8062 | match parse_result { |
| 8063 | None => Ok((DeviceStateData::InvalidValue(switch_expr), &[])), |
| 8064 | Some(result) => Ok((result, outer_remaining)), |
| 8065 | } |
| 8066 | } |
| 8067 | } |
| 8068 | impl DeviceStateData { |
| 8069 | pub fn as_resolution(&self) -> Option<&DeviceStateDataResolution> { |
| 8070 | match self { |
| 8071 | DeviceStateData::Resolution(value) => Some(value), |
| 8072 | _ => None, |
| 8073 | } |
| 8074 | } |
| 8075 | pub fn as_abs_calib(&self) -> Option<&DeviceStateDataAbsCalib> { |
| 8076 | match self { |
| 8077 | DeviceStateData::AbsCalib(value) => Some(value), |
| 8078 | _ => None, |
| 8079 | } |
| 8080 | } |
| 8081 | pub fn as_core(&self) -> Option<&DeviceStateDataCore> { |
| 8082 | match self { |
| 8083 | DeviceStateData::Core(value) => Some(value), |
| 8084 | _ => None, |
| 8085 | } |
| 8086 | } |
| 8087 | pub fn as_enable(&self) -> Option<&u8> { |
| 8088 | match self { |
| 8089 | DeviceStateData::Enable(value) => Some(value), |
| 8090 | _ => None, |
| 8091 | } |
| 8092 | } |
| 8093 | pub fn as_abs_area(&self) -> Option<&DeviceStateDataAbsArea> { |
| 8094 | match self { |
| 8095 | DeviceStateData::AbsArea(value) => Some(value), |
| 8096 | _ => None, |
| 8097 | } |
| 8098 | } |
| 8099 | } |
| 8100 | impl DeviceStateData { |
| 8101 | #[allow (dead_code)] |
| 8102 | fn serialize(&self, control_id: u16) -> Vec<u8> { |
| 8103 | let mut result: Vec = Vec::new(); |
| 8104 | self.serialize_into(&mut result, control_id:u16::from(control_id)); |
| 8105 | result |
| 8106 | } |
| 8107 | fn serialize_into(&self, bytes: &mut Vec<u8>, control_id: u16) { |
| 8108 | assert_eq!(self.switch_expr(), u16::from(control_id), "switch `data` has an inconsistent discriminant" ); |
| 8109 | match self { |
| 8110 | DeviceStateData::Resolution(resolution: &DeviceStateDataResolution) => resolution.serialize_into(bytes), |
| 8111 | DeviceStateData::AbsCalib(abs_calib: &DeviceStateDataAbsCalib) => abs_calib.serialize_into(bytes), |
| 8112 | DeviceStateData::Core(core: &DeviceStateDataCore) => core.serialize_into(bytes), |
| 8113 | DeviceStateData::Enable(enable: &u8) => { |
| 8114 | bytes.reserve(additional:4); |
| 8115 | enable.serialize_into(bytes); |
| 8116 | bytes.extend_from_slice(&[0; 3]); |
| 8117 | } |
| 8118 | DeviceStateData::AbsArea(abs_area: &DeviceStateDataAbsArea) => abs_area.serialize_into(bytes), |
| 8119 | DeviceStateData::InvalidValue(_) => panic!("attempted to serialize invalid switch case" ), |
| 8120 | } |
| 8121 | } |
| 8122 | } |
| 8123 | impl DeviceStateData { |
| 8124 | fn switch_expr(&self) -> u16 { |
| 8125 | match self { |
| 8126 | DeviceStateData::Resolution(_) => u16::from(DeviceControl::RESOLUTION), |
| 8127 | DeviceStateData::AbsCalib(_) => u16::from(DeviceControl::ABSCALIB), |
| 8128 | DeviceStateData::Core(_) => u16::from(DeviceControl::CORE), |
| 8129 | DeviceStateData::Enable(_) => u16::from(DeviceControl::ENABLE), |
| 8130 | DeviceStateData::AbsArea(_) => u16::from(DeviceControl::ABSAREA), |
| 8131 | DeviceStateData::InvalidValue(switch_expr: &u16) => *switch_expr, |
| 8132 | } |
| 8133 | } |
| 8134 | } |
| 8135 | |
| 8136 | #[derive (Clone)] |
| 8137 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8138 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8139 | pub struct DeviceState { |
| 8140 | pub len: u16, |
| 8141 | pub data: DeviceStateData, |
| 8142 | } |
| 8143 | impl_debug_if_no_extra_traits!(DeviceState, "DeviceState" ); |
| 8144 | impl TryParse for DeviceState { |
| 8145 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 8146 | let (control_id: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8147 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8148 | let (data: DeviceStateData, remaining: &[u8]) = DeviceStateData::try_parse(value:remaining, control_id:u16::from(control_id))?; |
| 8149 | let result: DeviceState = DeviceState { len, data }; |
| 8150 | Ok((result, remaining)) |
| 8151 | } |
| 8152 | } |
| 8153 | impl Serialize for DeviceState { |
| 8154 | type Bytes = Vec<u8>; |
| 8155 | fn serialize(&self) -> Vec<u8> { |
| 8156 | let mut result: Vec = Vec::new(); |
| 8157 | self.serialize_into(&mut result); |
| 8158 | result |
| 8159 | } |
| 8160 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 8161 | bytes.reserve(additional:4); |
| 8162 | let control_id: u16 = self.data.switch_expr(); |
| 8163 | control_id.serialize_into(bytes); |
| 8164 | self.len.serialize_into(bytes); |
| 8165 | self.data.serialize_into(bytes, control_id:u16::from(control_id)); |
| 8166 | } |
| 8167 | } |
| 8168 | |
| 8169 | /// Opcode for the GetDeviceControl request |
| 8170 | pub const GET_DEVICE_CONTROL_REQUEST: u8 = 34; |
| 8171 | #[derive (Clone, Copy, Default)] |
| 8172 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8173 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8174 | pub struct GetDeviceControlRequest { |
| 8175 | pub control_id: DeviceControl, |
| 8176 | pub device_id: u8, |
| 8177 | } |
| 8178 | impl_debug_if_no_extra_traits!(GetDeviceControlRequest, "GetDeviceControlRequest" ); |
| 8179 | impl GetDeviceControlRequest { |
| 8180 | /// Serialize this request into bytes for the provided connection |
| 8181 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 8182 | let length_so_far = 0; |
| 8183 | let control_id_bytes = u16::from(self.control_id).serialize(); |
| 8184 | let device_id_bytes = self.device_id.serialize(); |
| 8185 | let mut request0 = vec![ |
| 8186 | major_opcode, |
| 8187 | GET_DEVICE_CONTROL_REQUEST, |
| 8188 | 0, |
| 8189 | 0, |
| 8190 | control_id_bytes[0], |
| 8191 | control_id_bytes[1], |
| 8192 | device_id_bytes[0], |
| 8193 | 0, |
| 8194 | ]; |
| 8195 | let length_so_far = length_so_far + request0.len(); |
| 8196 | assert_eq!(length_so_far % 4, 0); |
| 8197 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 8198 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 8199 | ([request0.into()], vec![]) |
| 8200 | } |
| 8201 | /// Parse this request given its header, its body, and any fds that go along with it |
| 8202 | #[cfg (feature = "request-parsing" )] |
| 8203 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 8204 | if header.minor_opcode != GET_DEVICE_CONTROL_REQUEST { |
| 8205 | return Err(ParseError::InvalidValue); |
| 8206 | } |
| 8207 | let (control_id, remaining) = u16::try_parse(value)?; |
| 8208 | let control_id = control_id.into(); |
| 8209 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 8210 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
| 8211 | let _ = remaining; |
| 8212 | Ok(GetDeviceControlRequest { |
| 8213 | control_id, |
| 8214 | device_id, |
| 8215 | }) |
| 8216 | } |
| 8217 | } |
| 8218 | impl Request for GetDeviceControlRequest { |
| 8219 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 8220 | |
| 8221 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 8222 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 8223 | // Flatten the buffers into a single vector |
| 8224 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 8225 | (buf, fds) |
| 8226 | } |
| 8227 | } |
| 8228 | impl crate::x11_utils::ReplyRequest for GetDeviceControlRequest { |
| 8229 | type Reply = GetDeviceControlReply; |
| 8230 | } |
| 8231 | |
| 8232 | #[derive (Clone)] |
| 8233 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8234 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8235 | pub struct GetDeviceControlReply { |
| 8236 | pub xi_reply_type: u8, |
| 8237 | pub sequence: u16, |
| 8238 | pub length: u32, |
| 8239 | pub status: u8, |
| 8240 | pub control: DeviceState, |
| 8241 | } |
| 8242 | impl_debug_if_no_extra_traits!(GetDeviceControlReply, "GetDeviceControlReply" ); |
| 8243 | impl TryParse for GetDeviceControlReply { |
| 8244 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 8245 | let remaining: &[u8] = initial_value; |
| 8246 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 8247 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 8248 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8249 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8250 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 8251 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 8252 | let (control: DeviceState, remaining: &[u8]) = DeviceState::try_parse(remaining)?; |
| 8253 | if response_type != 1 { |
| 8254 | return Err(ParseError::InvalidValue); |
| 8255 | } |
| 8256 | let result: GetDeviceControlReply = GetDeviceControlReply { xi_reply_type, sequence, length, status, control }; |
| 8257 | let _ = remaining; |
| 8258 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 8259 | .ok_or(err:ParseError::InsufficientData)?; |
| 8260 | Ok((result, remaining)) |
| 8261 | } |
| 8262 | } |
| 8263 | impl Serialize for GetDeviceControlReply { |
| 8264 | type Bytes = Vec<u8>; |
| 8265 | fn serialize(&self) -> Vec<u8> { |
| 8266 | let mut result: Vec = Vec::new(); |
| 8267 | self.serialize_into(&mut result); |
| 8268 | result |
| 8269 | } |
| 8270 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 8271 | bytes.reserve(additional:32); |
| 8272 | let response_type_bytes: &[u8; 1] = &[1]; |
| 8273 | bytes.push(response_type_bytes[0]); |
| 8274 | self.xi_reply_type.serialize_into(bytes); |
| 8275 | self.sequence.serialize_into(bytes); |
| 8276 | self.length.serialize_into(bytes); |
| 8277 | self.status.serialize_into(bytes); |
| 8278 | bytes.extend_from_slice(&[0; 23]); |
| 8279 | self.control.serialize_into(bytes); |
| 8280 | } |
| 8281 | } |
| 8282 | |
| 8283 | #[derive (Clone, Default)] |
| 8284 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8285 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8286 | pub struct DeviceResolutionCtl { |
| 8287 | pub control_id: DeviceControl, |
| 8288 | pub len: u16, |
| 8289 | pub first_valuator: u8, |
| 8290 | pub resolution_values: Vec<u32>, |
| 8291 | } |
| 8292 | impl_debug_if_no_extra_traits!(DeviceResolutionCtl, "DeviceResolutionCtl" ); |
| 8293 | impl TryParse for DeviceResolutionCtl { |
| 8294 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 8295 | let (control_id: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8296 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8297 | let (first_valuator: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 8298 | let (num_valuators: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 8299 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 8300 | let (resolution_values: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:num_valuators.try_to_usize()?)?; |
| 8301 | let control_id: DeviceControl = control_id.into(); |
| 8302 | let result: DeviceResolutionCtl = DeviceResolutionCtl { control_id, len, first_valuator, resolution_values }; |
| 8303 | Ok((result, remaining)) |
| 8304 | } |
| 8305 | } |
| 8306 | impl Serialize for DeviceResolutionCtl { |
| 8307 | type Bytes = Vec<u8>; |
| 8308 | fn serialize(&self) -> Vec<u8> { |
| 8309 | let mut result: Vec = Vec::new(); |
| 8310 | self.serialize_into(&mut result); |
| 8311 | result |
| 8312 | } |
| 8313 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 8314 | bytes.reserve(additional:8); |
| 8315 | u16::from(self.control_id).serialize_into(bytes); |
| 8316 | self.len.serialize_into(bytes); |
| 8317 | self.first_valuator.serialize_into(bytes); |
| 8318 | let num_valuators: u8 = u8::try_from(self.resolution_values.len()).expect(msg:"`resolution_values` has too many elements" ); |
| 8319 | num_valuators.serialize_into(bytes); |
| 8320 | bytes.extend_from_slice(&[0; 2]); |
| 8321 | self.resolution_values.serialize_into(bytes); |
| 8322 | } |
| 8323 | } |
| 8324 | impl DeviceResolutionCtl { |
| 8325 | /// Get the value of the `num_valuators` field. |
| 8326 | /// |
| 8327 | /// The `num_valuators` field is used as the length field of the `resolution_values` field. |
| 8328 | /// This function computes the field's value again based on the length of the list. |
| 8329 | /// |
| 8330 | /// # Panics |
| 8331 | /// |
| 8332 | /// Panics if the value cannot be represented in the target type. This |
| 8333 | /// cannot happen with values of the struct received from the X11 server. |
| 8334 | pub fn num_valuators(&self) -> u8 { |
| 8335 | self.resolution_values.len() |
| 8336 | .try_into().unwrap() |
| 8337 | } |
| 8338 | } |
| 8339 | |
| 8340 | #[derive (Clone, Copy, Default)] |
| 8341 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8342 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8343 | pub struct DeviceAbsCalibCtl { |
| 8344 | pub control_id: DeviceControl, |
| 8345 | pub len: u16, |
| 8346 | pub min_x: i32, |
| 8347 | pub max_x: i32, |
| 8348 | pub min_y: i32, |
| 8349 | pub max_y: i32, |
| 8350 | pub flip_x: u32, |
| 8351 | pub flip_y: u32, |
| 8352 | pub rotation: u32, |
| 8353 | pub button_threshold: u32, |
| 8354 | } |
| 8355 | impl_debug_if_no_extra_traits!(DeviceAbsCalibCtl, "DeviceAbsCalibCtl" ); |
| 8356 | impl TryParse for DeviceAbsCalibCtl { |
| 8357 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 8358 | let (control_id: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8359 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8360 | let (min_x: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8361 | let (max_x: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8362 | let (min_y: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8363 | let (max_y: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8364 | let (flip_x: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8365 | let (flip_y: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8366 | let (rotation: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8367 | let (button_threshold: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8368 | let control_id: DeviceControl = control_id.into(); |
| 8369 | let result: DeviceAbsCalibCtl = DeviceAbsCalibCtl { control_id, len, min_x, max_x, min_y, max_y, flip_x, flip_y, rotation, button_threshold }; |
| 8370 | Ok((result, remaining)) |
| 8371 | } |
| 8372 | } |
| 8373 | impl Serialize for DeviceAbsCalibCtl { |
| 8374 | type Bytes = [u8; 36]; |
| 8375 | fn serialize(&self) -> [u8; 36] { |
| 8376 | let control_id_bytes = u16::from(self.control_id).serialize(); |
| 8377 | let len_bytes = self.len.serialize(); |
| 8378 | let min_x_bytes = self.min_x.serialize(); |
| 8379 | let max_x_bytes = self.max_x.serialize(); |
| 8380 | let min_y_bytes = self.min_y.serialize(); |
| 8381 | let max_y_bytes = self.max_y.serialize(); |
| 8382 | let flip_x_bytes = self.flip_x.serialize(); |
| 8383 | let flip_y_bytes = self.flip_y.serialize(); |
| 8384 | let rotation_bytes = self.rotation.serialize(); |
| 8385 | let button_threshold_bytes = self.button_threshold.serialize(); |
| 8386 | [ |
| 8387 | control_id_bytes[0], |
| 8388 | control_id_bytes[1], |
| 8389 | len_bytes[0], |
| 8390 | len_bytes[1], |
| 8391 | min_x_bytes[0], |
| 8392 | min_x_bytes[1], |
| 8393 | min_x_bytes[2], |
| 8394 | min_x_bytes[3], |
| 8395 | max_x_bytes[0], |
| 8396 | max_x_bytes[1], |
| 8397 | max_x_bytes[2], |
| 8398 | max_x_bytes[3], |
| 8399 | min_y_bytes[0], |
| 8400 | min_y_bytes[1], |
| 8401 | min_y_bytes[2], |
| 8402 | min_y_bytes[3], |
| 8403 | max_y_bytes[0], |
| 8404 | max_y_bytes[1], |
| 8405 | max_y_bytes[2], |
| 8406 | max_y_bytes[3], |
| 8407 | flip_x_bytes[0], |
| 8408 | flip_x_bytes[1], |
| 8409 | flip_x_bytes[2], |
| 8410 | flip_x_bytes[3], |
| 8411 | flip_y_bytes[0], |
| 8412 | flip_y_bytes[1], |
| 8413 | flip_y_bytes[2], |
| 8414 | flip_y_bytes[3], |
| 8415 | rotation_bytes[0], |
| 8416 | rotation_bytes[1], |
| 8417 | rotation_bytes[2], |
| 8418 | rotation_bytes[3], |
| 8419 | button_threshold_bytes[0], |
| 8420 | button_threshold_bytes[1], |
| 8421 | button_threshold_bytes[2], |
| 8422 | button_threshold_bytes[3], |
| 8423 | ] |
| 8424 | } |
| 8425 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 8426 | bytes.reserve(36); |
| 8427 | u16::from(self.control_id).serialize_into(bytes); |
| 8428 | self.len.serialize_into(bytes); |
| 8429 | self.min_x.serialize_into(bytes); |
| 8430 | self.max_x.serialize_into(bytes); |
| 8431 | self.min_y.serialize_into(bytes); |
| 8432 | self.max_y.serialize_into(bytes); |
| 8433 | self.flip_x.serialize_into(bytes); |
| 8434 | self.flip_y.serialize_into(bytes); |
| 8435 | self.rotation.serialize_into(bytes); |
| 8436 | self.button_threshold.serialize_into(bytes); |
| 8437 | } |
| 8438 | } |
| 8439 | |
| 8440 | #[derive (Clone, Copy, Default)] |
| 8441 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8442 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8443 | pub struct DeviceAbsAreaCtrl { |
| 8444 | pub control_id: DeviceControl, |
| 8445 | pub len: u16, |
| 8446 | pub offset_x: u32, |
| 8447 | pub offset_y: u32, |
| 8448 | pub width: i32, |
| 8449 | pub height: i32, |
| 8450 | pub screen: i32, |
| 8451 | pub following: u32, |
| 8452 | } |
| 8453 | impl_debug_if_no_extra_traits!(DeviceAbsAreaCtrl, "DeviceAbsAreaCtrl" ); |
| 8454 | impl TryParse for DeviceAbsAreaCtrl { |
| 8455 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 8456 | let (control_id: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8457 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8458 | let (offset_x: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8459 | let (offset_y: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8460 | let (width: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8461 | let (height: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8462 | let (screen: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8463 | let (following: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8464 | let control_id: DeviceControl = control_id.into(); |
| 8465 | let result: DeviceAbsAreaCtrl = DeviceAbsAreaCtrl { control_id, len, offset_x, offset_y, width, height, screen, following }; |
| 8466 | Ok((result, remaining)) |
| 8467 | } |
| 8468 | } |
| 8469 | impl Serialize for DeviceAbsAreaCtrl { |
| 8470 | type Bytes = [u8; 28]; |
| 8471 | fn serialize(&self) -> [u8; 28] { |
| 8472 | let control_id_bytes = u16::from(self.control_id).serialize(); |
| 8473 | let len_bytes = self.len.serialize(); |
| 8474 | let offset_x_bytes = self.offset_x.serialize(); |
| 8475 | let offset_y_bytes = self.offset_y.serialize(); |
| 8476 | let width_bytes = self.width.serialize(); |
| 8477 | let height_bytes = self.height.serialize(); |
| 8478 | let screen_bytes = self.screen.serialize(); |
| 8479 | let following_bytes = self.following.serialize(); |
| 8480 | [ |
| 8481 | control_id_bytes[0], |
| 8482 | control_id_bytes[1], |
| 8483 | len_bytes[0], |
| 8484 | len_bytes[1], |
| 8485 | offset_x_bytes[0], |
| 8486 | offset_x_bytes[1], |
| 8487 | offset_x_bytes[2], |
| 8488 | offset_x_bytes[3], |
| 8489 | offset_y_bytes[0], |
| 8490 | offset_y_bytes[1], |
| 8491 | offset_y_bytes[2], |
| 8492 | offset_y_bytes[3], |
| 8493 | width_bytes[0], |
| 8494 | width_bytes[1], |
| 8495 | width_bytes[2], |
| 8496 | width_bytes[3], |
| 8497 | height_bytes[0], |
| 8498 | height_bytes[1], |
| 8499 | height_bytes[2], |
| 8500 | height_bytes[3], |
| 8501 | screen_bytes[0], |
| 8502 | screen_bytes[1], |
| 8503 | screen_bytes[2], |
| 8504 | screen_bytes[3], |
| 8505 | following_bytes[0], |
| 8506 | following_bytes[1], |
| 8507 | following_bytes[2], |
| 8508 | following_bytes[3], |
| 8509 | ] |
| 8510 | } |
| 8511 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 8512 | bytes.reserve(28); |
| 8513 | u16::from(self.control_id).serialize_into(bytes); |
| 8514 | self.len.serialize_into(bytes); |
| 8515 | self.offset_x.serialize_into(bytes); |
| 8516 | self.offset_y.serialize_into(bytes); |
| 8517 | self.width.serialize_into(bytes); |
| 8518 | self.height.serialize_into(bytes); |
| 8519 | self.screen.serialize_into(bytes); |
| 8520 | self.following.serialize_into(bytes); |
| 8521 | } |
| 8522 | } |
| 8523 | |
| 8524 | #[derive (Clone, Copy, Default)] |
| 8525 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8526 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8527 | pub struct DeviceCoreCtrl { |
| 8528 | pub control_id: DeviceControl, |
| 8529 | pub len: u16, |
| 8530 | pub status: u8, |
| 8531 | } |
| 8532 | impl_debug_if_no_extra_traits!(DeviceCoreCtrl, "DeviceCoreCtrl" ); |
| 8533 | impl TryParse for DeviceCoreCtrl { |
| 8534 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 8535 | let (control_id: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8536 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8537 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 8538 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
| 8539 | let control_id: DeviceControl = control_id.into(); |
| 8540 | let result: DeviceCoreCtrl = DeviceCoreCtrl { control_id, len, status }; |
| 8541 | Ok((result, remaining)) |
| 8542 | } |
| 8543 | } |
| 8544 | impl Serialize for DeviceCoreCtrl { |
| 8545 | type Bytes = [u8; 8]; |
| 8546 | fn serialize(&self) -> [u8; 8] { |
| 8547 | let control_id_bytes = u16::from(self.control_id).serialize(); |
| 8548 | let len_bytes = self.len.serialize(); |
| 8549 | let status_bytes = self.status.serialize(); |
| 8550 | [ |
| 8551 | control_id_bytes[0], |
| 8552 | control_id_bytes[1], |
| 8553 | len_bytes[0], |
| 8554 | len_bytes[1], |
| 8555 | status_bytes[0], |
| 8556 | 0, |
| 8557 | 0, |
| 8558 | 0, |
| 8559 | ] |
| 8560 | } |
| 8561 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 8562 | bytes.reserve(8); |
| 8563 | u16::from(self.control_id).serialize_into(bytes); |
| 8564 | self.len.serialize_into(bytes); |
| 8565 | self.status.serialize_into(bytes); |
| 8566 | bytes.extend_from_slice(&[0; 3]); |
| 8567 | } |
| 8568 | } |
| 8569 | |
| 8570 | #[derive (Clone, Copy, Default)] |
| 8571 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8572 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8573 | pub struct DeviceEnableCtrl { |
| 8574 | pub control_id: DeviceControl, |
| 8575 | pub len: u16, |
| 8576 | pub enable: u8, |
| 8577 | } |
| 8578 | impl_debug_if_no_extra_traits!(DeviceEnableCtrl, "DeviceEnableCtrl" ); |
| 8579 | impl TryParse for DeviceEnableCtrl { |
| 8580 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 8581 | let (control_id: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8582 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8583 | let (enable: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 8584 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
| 8585 | let control_id: DeviceControl = control_id.into(); |
| 8586 | let result: DeviceEnableCtrl = DeviceEnableCtrl { control_id, len, enable }; |
| 8587 | Ok((result, remaining)) |
| 8588 | } |
| 8589 | } |
| 8590 | impl Serialize for DeviceEnableCtrl { |
| 8591 | type Bytes = [u8; 8]; |
| 8592 | fn serialize(&self) -> [u8; 8] { |
| 8593 | let control_id_bytes = u16::from(self.control_id).serialize(); |
| 8594 | let len_bytes = self.len.serialize(); |
| 8595 | let enable_bytes = self.enable.serialize(); |
| 8596 | [ |
| 8597 | control_id_bytes[0], |
| 8598 | control_id_bytes[1], |
| 8599 | len_bytes[0], |
| 8600 | len_bytes[1], |
| 8601 | enable_bytes[0], |
| 8602 | 0, |
| 8603 | 0, |
| 8604 | 0, |
| 8605 | ] |
| 8606 | } |
| 8607 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 8608 | bytes.reserve(8); |
| 8609 | u16::from(self.control_id).serialize_into(bytes); |
| 8610 | self.len.serialize_into(bytes); |
| 8611 | self.enable.serialize_into(bytes); |
| 8612 | bytes.extend_from_slice(&[0; 3]); |
| 8613 | } |
| 8614 | } |
| 8615 | |
| 8616 | #[derive (Clone)] |
| 8617 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8618 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8619 | pub struct DeviceCtlDataResolution { |
| 8620 | pub first_valuator: u8, |
| 8621 | pub resolution_values: Vec<u32>, |
| 8622 | } |
| 8623 | impl_debug_if_no_extra_traits!(DeviceCtlDataResolution, "DeviceCtlDataResolution" ); |
| 8624 | impl TryParse for DeviceCtlDataResolution { |
| 8625 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 8626 | let (first_valuator: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 8627 | let (num_valuators: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 8628 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 8629 | let (resolution_values: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:num_valuators.try_to_usize()?)?; |
| 8630 | let result: DeviceCtlDataResolution = DeviceCtlDataResolution { first_valuator, resolution_values }; |
| 8631 | Ok((result, remaining)) |
| 8632 | } |
| 8633 | } |
| 8634 | impl Serialize for DeviceCtlDataResolution { |
| 8635 | type Bytes = Vec<u8>; |
| 8636 | fn serialize(&self) -> Vec<u8> { |
| 8637 | let mut result: Vec = Vec::new(); |
| 8638 | self.serialize_into(&mut result); |
| 8639 | result |
| 8640 | } |
| 8641 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 8642 | bytes.reserve(additional:4); |
| 8643 | self.first_valuator.serialize_into(bytes); |
| 8644 | let num_valuators: u8 = u8::try_from(self.resolution_values.len()).expect(msg:"`resolution_values` has too many elements" ); |
| 8645 | num_valuators.serialize_into(bytes); |
| 8646 | bytes.extend_from_slice(&[0; 2]); |
| 8647 | self.resolution_values.serialize_into(bytes); |
| 8648 | } |
| 8649 | } |
| 8650 | impl DeviceCtlDataResolution { |
| 8651 | /// Get the value of the `num_valuators` field. |
| 8652 | /// |
| 8653 | /// The `num_valuators` field is used as the length field of the `resolution_values` field. |
| 8654 | /// This function computes the field's value again based on the length of the list. |
| 8655 | /// |
| 8656 | /// # Panics |
| 8657 | /// |
| 8658 | /// Panics if the value cannot be represented in the target type. This |
| 8659 | /// cannot happen with values of the struct received from the X11 server. |
| 8660 | pub fn num_valuators(&self) -> u8 { |
| 8661 | self.resolution_values.len() |
| 8662 | .try_into().unwrap() |
| 8663 | } |
| 8664 | } |
| 8665 | #[derive (Clone, Copy)] |
| 8666 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8667 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8668 | pub struct DeviceCtlDataAbsCalib { |
| 8669 | pub min_x: i32, |
| 8670 | pub max_x: i32, |
| 8671 | pub min_y: i32, |
| 8672 | pub max_y: i32, |
| 8673 | pub flip_x: u32, |
| 8674 | pub flip_y: u32, |
| 8675 | pub rotation: u32, |
| 8676 | pub button_threshold: u32, |
| 8677 | } |
| 8678 | impl_debug_if_no_extra_traits!(DeviceCtlDataAbsCalib, "DeviceCtlDataAbsCalib" ); |
| 8679 | impl TryParse for DeviceCtlDataAbsCalib { |
| 8680 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 8681 | let (min_x: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8682 | let (max_x: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8683 | let (min_y: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8684 | let (max_y: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8685 | let (flip_x: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8686 | let (flip_y: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8687 | let (rotation: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8688 | let (button_threshold: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8689 | let result: DeviceCtlDataAbsCalib = DeviceCtlDataAbsCalib { min_x, max_x, min_y, max_y, flip_x, flip_y, rotation, button_threshold }; |
| 8690 | Ok((result, remaining)) |
| 8691 | } |
| 8692 | } |
| 8693 | impl Serialize for DeviceCtlDataAbsCalib { |
| 8694 | type Bytes = [u8; 32]; |
| 8695 | fn serialize(&self) -> [u8; 32] { |
| 8696 | let min_x_bytes = self.min_x.serialize(); |
| 8697 | let max_x_bytes = self.max_x.serialize(); |
| 8698 | let min_y_bytes = self.min_y.serialize(); |
| 8699 | let max_y_bytes = self.max_y.serialize(); |
| 8700 | let flip_x_bytes = self.flip_x.serialize(); |
| 8701 | let flip_y_bytes = self.flip_y.serialize(); |
| 8702 | let rotation_bytes = self.rotation.serialize(); |
| 8703 | let button_threshold_bytes = self.button_threshold.serialize(); |
| 8704 | [ |
| 8705 | min_x_bytes[0], |
| 8706 | min_x_bytes[1], |
| 8707 | min_x_bytes[2], |
| 8708 | min_x_bytes[3], |
| 8709 | max_x_bytes[0], |
| 8710 | max_x_bytes[1], |
| 8711 | max_x_bytes[2], |
| 8712 | max_x_bytes[3], |
| 8713 | min_y_bytes[0], |
| 8714 | min_y_bytes[1], |
| 8715 | min_y_bytes[2], |
| 8716 | min_y_bytes[3], |
| 8717 | max_y_bytes[0], |
| 8718 | max_y_bytes[1], |
| 8719 | max_y_bytes[2], |
| 8720 | max_y_bytes[3], |
| 8721 | flip_x_bytes[0], |
| 8722 | flip_x_bytes[1], |
| 8723 | flip_x_bytes[2], |
| 8724 | flip_x_bytes[3], |
| 8725 | flip_y_bytes[0], |
| 8726 | flip_y_bytes[1], |
| 8727 | flip_y_bytes[2], |
| 8728 | flip_y_bytes[3], |
| 8729 | rotation_bytes[0], |
| 8730 | rotation_bytes[1], |
| 8731 | rotation_bytes[2], |
| 8732 | rotation_bytes[3], |
| 8733 | button_threshold_bytes[0], |
| 8734 | button_threshold_bytes[1], |
| 8735 | button_threshold_bytes[2], |
| 8736 | button_threshold_bytes[3], |
| 8737 | ] |
| 8738 | } |
| 8739 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 8740 | bytes.reserve(32); |
| 8741 | self.min_x.serialize_into(bytes); |
| 8742 | self.max_x.serialize_into(bytes); |
| 8743 | self.min_y.serialize_into(bytes); |
| 8744 | self.max_y.serialize_into(bytes); |
| 8745 | self.flip_x.serialize_into(bytes); |
| 8746 | self.flip_y.serialize_into(bytes); |
| 8747 | self.rotation.serialize_into(bytes); |
| 8748 | self.button_threshold.serialize_into(bytes); |
| 8749 | } |
| 8750 | } |
| 8751 | #[derive (Clone, Copy)] |
| 8752 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8753 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8754 | pub struct DeviceCtlDataCore { |
| 8755 | pub status: u8, |
| 8756 | } |
| 8757 | impl_debug_if_no_extra_traits!(DeviceCtlDataCore, "DeviceCtlDataCore" ); |
| 8758 | impl TryParse for DeviceCtlDataCore { |
| 8759 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 8760 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 8761 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
| 8762 | let result: DeviceCtlDataCore = DeviceCtlDataCore { status }; |
| 8763 | Ok((result, remaining)) |
| 8764 | } |
| 8765 | } |
| 8766 | impl Serialize for DeviceCtlDataCore { |
| 8767 | type Bytes = [u8; 4]; |
| 8768 | fn serialize(&self) -> [u8; 4] { |
| 8769 | let status_bytes: [u8; 1] = self.status.serialize(); |
| 8770 | [ |
| 8771 | status_bytes[0], |
| 8772 | 0, |
| 8773 | 0, |
| 8774 | 0, |
| 8775 | ] |
| 8776 | } |
| 8777 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 8778 | bytes.reserve(additional:4); |
| 8779 | self.status.serialize_into(bytes); |
| 8780 | bytes.extend_from_slice(&[0; 3]); |
| 8781 | } |
| 8782 | } |
| 8783 | #[derive (Clone, Copy)] |
| 8784 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8785 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8786 | pub struct DeviceCtlDataAbsArea { |
| 8787 | pub offset_x: u32, |
| 8788 | pub offset_y: u32, |
| 8789 | pub width: i32, |
| 8790 | pub height: i32, |
| 8791 | pub screen: i32, |
| 8792 | pub following: u32, |
| 8793 | } |
| 8794 | impl_debug_if_no_extra_traits!(DeviceCtlDataAbsArea, "DeviceCtlDataAbsArea" ); |
| 8795 | impl TryParse for DeviceCtlDataAbsArea { |
| 8796 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 8797 | let (offset_x: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8798 | let (offset_y: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8799 | let (width: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8800 | let (height: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8801 | let (screen: i32, remaining: &[u8]) = i32::try_parse(remaining)?; |
| 8802 | let (following: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 8803 | let result: DeviceCtlDataAbsArea = DeviceCtlDataAbsArea { offset_x, offset_y, width, height, screen, following }; |
| 8804 | Ok((result, remaining)) |
| 8805 | } |
| 8806 | } |
| 8807 | impl Serialize for DeviceCtlDataAbsArea { |
| 8808 | type Bytes = [u8; 24]; |
| 8809 | fn serialize(&self) -> [u8; 24] { |
| 8810 | let offset_x_bytes = self.offset_x.serialize(); |
| 8811 | let offset_y_bytes = self.offset_y.serialize(); |
| 8812 | let width_bytes = self.width.serialize(); |
| 8813 | let height_bytes = self.height.serialize(); |
| 8814 | let screen_bytes = self.screen.serialize(); |
| 8815 | let following_bytes = self.following.serialize(); |
| 8816 | [ |
| 8817 | offset_x_bytes[0], |
| 8818 | offset_x_bytes[1], |
| 8819 | offset_x_bytes[2], |
| 8820 | offset_x_bytes[3], |
| 8821 | offset_y_bytes[0], |
| 8822 | offset_y_bytes[1], |
| 8823 | offset_y_bytes[2], |
| 8824 | offset_y_bytes[3], |
| 8825 | width_bytes[0], |
| 8826 | width_bytes[1], |
| 8827 | width_bytes[2], |
| 8828 | width_bytes[3], |
| 8829 | height_bytes[0], |
| 8830 | height_bytes[1], |
| 8831 | height_bytes[2], |
| 8832 | height_bytes[3], |
| 8833 | screen_bytes[0], |
| 8834 | screen_bytes[1], |
| 8835 | screen_bytes[2], |
| 8836 | screen_bytes[3], |
| 8837 | following_bytes[0], |
| 8838 | following_bytes[1], |
| 8839 | following_bytes[2], |
| 8840 | following_bytes[3], |
| 8841 | ] |
| 8842 | } |
| 8843 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 8844 | bytes.reserve(24); |
| 8845 | self.offset_x.serialize_into(bytes); |
| 8846 | self.offset_y.serialize_into(bytes); |
| 8847 | self.width.serialize_into(bytes); |
| 8848 | self.height.serialize_into(bytes); |
| 8849 | self.screen.serialize_into(bytes); |
| 8850 | self.following.serialize_into(bytes); |
| 8851 | } |
| 8852 | } |
| 8853 | #[derive (Clone)] |
| 8854 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8855 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8856 | pub enum DeviceCtlData { |
| 8857 | Resolution(DeviceCtlDataResolution), |
| 8858 | AbsCalib(DeviceCtlDataAbsCalib), |
| 8859 | Core(DeviceCtlDataCore), |
| 8860 | Enable(u8), |
| 8861 | AbsArea(DeviceCtlDataAbsArea), |
| 8862 | /// This variant is returned when the server sends a discriminant |
| 8863 | /// value that does not match any of the defined by the protocol. |
| 8864 | /// |
| 8865 | /// Usually, this should be considered a parsing error, but there |
| 8866 | /// are some cases where the server violates the protocol. |
| 8867 | /// |
| 8868 | /// Trying to use `serialize` or `serialize_into` with this variant |
| 8869 | /// will raise a panic. |
| 8870 | InvalidValue(u16), |
| 8871 | } |
| 8872 | impl_debug_if_no_extra_traits!(DeviceCtlData, "DeviceCtlData" ); |
| 8873 | impl DeviceCtlData { |
| 8874 | #[cfg_attr (not(feature = "request-parsing" ), allow(dead_code))] |
| 8875 | fn try_parse(value: &[u8], control_id: u16) -> Result<(Self, &[u8]), ParseError> { |
| 8876 | let switch_expr = u16::from(control_id); |
| 8877 | let mut outer_remaining = value; |
| 8878 | let mut parse_result = None; |
| 8879 | if switch_expr == u16::from(DeviceControl::RESOLUTION) { |
| 8880 | let (resolution, new_remaining) = DeviceCtlDataResolution::try_parse(outer_remaining)?; |
| 8881 | outer_remaining = new_remaining; |
| 8882 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 8883 | parse_result = Some(DeviceCtlData::Resolution(resolution)); |
| 8884 | } |
| 8885 | if switch_expr == u16::from(DeviceControl::ABSCALIB) { |
| 8886 | let (abs_calib, new_remaining) = DeviceCtlDataAbsCalib::try_parse(outer_remaining)?; |
| 8887 | outer_remaining = new_remaining; |
| 8888 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 8889 | parse_result = Some(DeviceCtlData::AbsCalib(abs_calib)); |
| 8890 | } |
| 8891 | if switch_expr == u16::from(DeviceControl::CORE) { |
| 8892 | let (core, new_remaining) = DeviceCtlDataCore::try_parse(outer_remaining)?; |
| 8893 | outer_remaining = new_remaining; |
| 8894 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 8895 | parse_result = Some(DeviceCtlData::Core(core)); |
| 8896 | } |
| 8897 | if switch_expr == u16::from(DeviceControl::ENABLE) { |
| 8898 | let remaining = outer_remaining; |
| 8899 | let (enable, remaining) = u8::try_parse(remaining)?; |
| 8900 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 8901 | outer_remaining = remaining; |
| 8902 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 8903 | parse_result = Some(DeviceCtlData::Enable(enable)); |
| 8904 | } |
| 8905 | if switch_expr == u16::from(DeviceControl::ABSAREA) { |
| 8906 | let (abs_area, new_remaining) = DeviceCtlDataAbsArea::try_parse(outer_remaining)?; |
| 8907 | outer_remaining = new_remaining; |
| 8908 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 8909 | parse_result = Some(DeviceCtlData::AbsArea(abs_area)); |
| 8910 | } |
| 8911 | match parse_result { |
| 8912 | None => Ok((DeviceCtlData::InvalidValue(switch_expr), &[])), |
| 8913 | Some(result) => Ok((result, outer_remaining)), |
| 8914 | } |
| 8915 | } |
| 8916 | } |
| 8917 | impl DeviceCtlData { |
| 8918 | pub fn as_resolution(&self) -> Option<&DeviceCtlDataResolution> { |
| 8919 | match self { |
| 8920 | DeviceCtlData::Resolution(value) => Some(value), |
| 8921 | _ => None, |
| 8922 | } |
| 8923 | } |
| 8924 | pub fn as_abs_calib(&self) -> Option<&DeviceCtlDataAbsCalib> { |
| 8925 | match self { |
| 8926 | DeviceCtlData::AbsCalib(value) => Some(value), |
| 8927 | _ => None, |
| 8928 | } |
| 8929 | } |
| 8930 | pub fn as_core(&self) -> Option<&DeviceCtlDataCore> { |
| 8931 | match self { |
| 8932 | DeviceCtlData::Core(value) => Some(value), |
| 8933 | _ => None, |
| 8934 | } |
| 8935 | } |
| 8936 | pub fn as_enable(&self) -> Option<&u8> { |
| 8937 | match self { |
| 8938 | DeviceCtlData::Enable(value) => Some(value), |
| 8939 | _ => None, |
| 8940 | } |
| 8941 | } |
| 8942 | pub fn as_abs_area(&self) -> Option<&DeviceCtlDataAbsArea> { |
| 8943 | match self { |
| 8944 | DeviceCtlData::AbsArea(value) => Some(value), |
| 8945 | _ => None, |
| 8946 | } |
| 8947 | } |
| 8948 | } |
| 8949 | impl DeviceCtlData { |
| 8950 | #[allow (dead_code)] |
| 8951 | fn serialize(&self, control_id: u16) -> Vec<u8> { |
| 8952 | let mut result: Vec = Vec::new(); |
| 8953 | self.serialize_into(&mut result, control_id:u16::from(control_id)); |
| 8954 | result |
| 8955 | } |
| 8956 | fn serialize_into(&self, bytes: &mut Vec<u8>, control_id: u16) { |
| 8957 | assert_eq!(self.switch_expr(), u16::from(control_id), "switch `data` has an inconsistent discriminant" ); |
| 8958 | match self { |
| 8959 | DeviceCtlData::Resolution(resolution: &DeviceCtlDataResolution) => resolution.serialize_into(bytes), |
| 8960 | DeviceCtlData::AbsCalib(abs_calib: &DeviceCtlDataAbsCalib) => abs_calib.serialize_into(bytes), |
| 8961 | DeviceCtlData::Core(core: &DeviceCtlDataCore) => core.serialize_into(bytes), |
| 8962 | DeviceCtlData::Enable(enable: &u8) => { |
| 8963 | bytes.reserve(additional:4); |
| 8964 | enable.serialize_into(bytes); |
| 8965 | bytes.extend_from_slice(&[0; 3]); |
| 8966 | } |
| 8967 | DeviceCtlData::AbsArea(abs_area: &DeviceCtlDataAbsArea) => abs_area.serialize_into(bytes), |
| 8968 | DeviceCtlData::InvalidValue(_) => panic!("attempted to serialize invalid switch case" ), |
| 8969 | } |
| 8970 | } |
| 8971 | } |
| 8972 | impl DeviceCtlData { |
| 8973 | fn switch_expr(&self) -> u16 { |
| 8974 | match self { |
| 8975 | DeviceCtlData::Resolution(_) => u16::from(DeviceControl::RESOLUTION), |
| 8976 | DeviceCtlData::AbsCalib(_) => u16::from(DeviceControl::ABSCALIB), |
| 8977 | DeviceCtlData::Core(_) => u16::from(DeviceControl::CORE), |
| 8978 | DeviceCtlData::Enable(_) => u16::from(DeviceControl::ENABLE), |
| 8979 | DeviceCtlData::AbsArea(_) => u16::from(DeviceControl::ABSAREA), |
| 8980 | DeviceCtlData::InvalidValue(switch_expr: &u16) => *switch_expr, |
| 8981 | } |
| 8982 | } |
| 8983 | } |
| 8984 | |
| 8985 | #[derive (Clone)] |
| 8986 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 8987 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 8988 | pub struct DeviceCtl { |
| 8989 | pub len: u16, |
| 8990 | pub data: DeviceCtlData, |
| 8991 | } |
| 8992 | impl_debug_if_no_extra_traits!(DeviceCtl, "DeviceCtl" ); |
| 8993 | impl TryParse for DeviceCtl { |
| 8994 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 8995 | let (control_id: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8996 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 8997 | let (data: DeviceCtlData, remaining: &[u8]) = DeviceCtlData::try_parse(value:remaining, control_id:u16::from(control_id))?; |
| 8998 | let result: DeviceCtl = DeviceCtl { len, data }; |
| 8999 | Ok((result, remaining)) |
| 9000 | } |
| 9001 | } |
| 9002 | impl Serialize for DeviceCtl { |
| 9003 | type Bytes = Vec<u8>; |
| 9004 | fn serialize(&self) -> Vec<u8> { |
| 9005 | let mut result: Vec = Vec::new(); |
| 9006 | self.serialize_into(&mut result); |
| 9007 | result |
| 9008 | } |
| 9009 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 9010 | bytes.reserve(additional:4); |
| 9011 | let control_id: u16 = self.data.switch_expr(); |
| 9012 | control_id.serialize_into(bytes); |
| 9013 | self.len.serialize_into(bytes); |
| 9014 | self.data.serialize_into(bytes, control_id:u16::from(control_id)); |
| 9015 | } |
| 9016 | } |
| 9017 | |
| 9018 | /// Opcode for the ChangeDeviceControl request |
| 9019 | pub const CHANGE_DEVICE_CONTROL_REQUEST: u8 = 35; |
| 9020 | #[derive (Clone)] |
| 9021 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 9022 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 9023 | pub struct ChangeDeviceControlRequest { |
| 9024 | pub control_id: DeviceControl, |
| 9025 | pub device_id: u8, |
| 9026 | pub control: DeviceCtl, |
| 9027 | } |
| 9028 | impl_debug_if_no_extra_traits!(ChangeDeviceControlRequest, "ChangeDeviceControlRequest" ); |
| 9029 | impl ChangeDeviceControlRequest { |
| 9030 | /// Serialize this request into bytes for the provided connection |
| 9031 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 3]> { |
| 9032 | let length_so_far = 0; |
| 9033 | let control_id_bytes = u16::from(self.control_id).serialize(); |
| 9034 | let device_id_bytes = self.device_id.serialize(); |
| 9035 | let mut request0 = vec![ |
| 9036 | major_opcode, |
| 9037 | CHANGE_DEVICE_CONTROL_REQUEST, |
| 9038 | 0, |
| 9039 | 0, |
| 9040 | control_id_bytes[0], |
| 9041 | control_id_bytes[1], |
| 9042 | device_id_bytes[0], |
| 9043 | 0, |
| 9044 | ]; |
| 9045 | let length_so_far = length_so_far + request0.len(); |
| 9046 | let control_bytes = self.control.serialize(); |
| 9047 | let length_so_far = length_so_far + control_bytes.len(); |
| 9048 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 9049 | let length_so_far = length_so_far + padding0.len(); |
| 9050 | assert_eq!(length_so_far % 4, 0); |
| 9051 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 9052 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 9053 | ([request0.into(), control_bytes.into(), padding0.into()], vec![]) |
| 9054 | } |
| 9055 | /// Parse this request given its header, its body, and any fds that go along with it |
| 9056 | #[cfg (feature = "request-parsing" )] |
| 9057 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 9058 | if header.minor_opcode != CHANGE_DEVICE_CONTROL_REQUEST { |
| 9059 | return Err(ParseError::InvalidValue); |
| 9060 | } |
| 9061 | let (control_id, remaining) = u16::try_parse(value)?; |
| 9062 | let control_id = control_id.into(); |
| 9063 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 9064 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
| 9065 | let (control, remaining) = DeviceCtl::try_parse(remaining)?; |
| 9066 | let _ = remaining; |
| 9067 | Ok(ChangeDeviceControlRequest { |
| 9068 | control_id, |
| 9069 | device_id, |
| 9070 | control, |
| 9071 | }) |
| 9072 | } |
| 9073 | } |
| 9074 | impl Request for ChangeDeviceControlRequest { |
| 9075 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 9076 | |
| 9077 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 9078 | let (bufs: [Cow<'static, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 9079 | // Flatten the buffers into a single vector |
| 9080 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 9081 | (buf, fds) |
| 9082 | } |
| 9083 | } |
| 9084 | impl crate::x11_utils::ReplyRequest for ChangeDeviceControlRequest { |
| 9085 | type Reply = ChangeDeviceControlReply; |
| 9086 | } |
| 9087 | |
| 9088 | #[derive (Clone, Copy, Default)] |
| 9089 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 9090 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 9091 | pub struct ChangeDeviceControlReply { |
| 9092 | pub xi_reply_type: u8, |
| 9093 | pub sequence: u16, |
| 9094 | pub length: u32, |
| 9095 | pub status: u8, |
| 9096 | } |
| 9097 | impl_debug_if_no_extra_traits!(ChangeDeviceControlReply, "ChangeDeviceControlReply" ); |
| 9098 | impl TryParse for ChangeDeviceControlReply { |
| 9099 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 9100 | let remaining: &[u8] = initial_value; |
| 9101 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 9102 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 9103 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 9104 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 9105 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 9106 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 9107 | if response_type != 1 { |
| 9108 | return Err(ParseError::InvalidValue); |
| 9109 | } |
| 9110 | let result: ChangeDeviceControlReply = ChangeDeviceControlReply { xi_reply_type, sequence, length, status }; |
| 9111 | let _ = remaining; |
| 9112 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 9113 | .ok_or(err:ParseError::InsufficientData)?; |
| 9114 | Ok((result, remaining)) |
| 9115 | } |
| 9116 | } |
| 9117 | impl Serialize for ChangeDeviceControlReply { |
| 9118 | type Bytes = [u8; 32]; |
| 9119 | fn serialize(&self) -> [u8; 32] { |
| 9120 | let response_type_bytes = &[1]; |
| 9121 | let xi_reply_type_bytes = self.xi_reply_type.serialize(); |
| 9122 | let sequence_bytes = self.sequence.serialize(); |
| 9123 | let length_bytes = self.length.serialize(); |
| 9124 | let status_bytes = self.status.serialize(); |
| 9125 | [ |
| 9126 | response_type_bytes[0], |
| 9127 | xi_reply_type_bytes[0], |
| 9128 | sequence_bytes[0], |
| 9129 | sequence_bytes[1], |
| 9130 | length_bytes[0], |
| 9131 | length_bytes[1], |
| 9132 | length_bytes[2], |
| 9133 | length_bytes[3], |
| 9134 | status_bytes[0], |
| 9135 | 0, |
| 9136 | 0, |
| 9137 | 0, |
| 9138 | 0, |
| 9139 | 0, |
| 9140 | 0, |
| 9141 | 0, |
| 9142 | 0, |
| 9143 | 0, |
| 9144 | 0, |
| 9145 | 0, |
| 9146 | 0, |
| 9147 | 0, |
| 9148 | 0, |
| 9149 | 0, |
| 9150 | 0, |
| 9151 | 0, |
| 9152 | 0, |
| 9153 | 0, |
| 9154 | 0, |
| 9155 | 0, |
| 9156 | 0, |
| 9157 | 0, |
| 9158 | ] |
| 9159 | } |
| 9160 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 9161 | bytes.reserve(32); |
| 9162 | let response_type_bytes = &[1]; |
| 9163 | bytes.push(response_type_bytes[0]); |
| 9164 | self.xi_reply_type.serialize_into(bytes); |
| 9165 | self.sequence.serialize_into(bytes); |
| 9166 | self.length.serialize_into(bytes); |
| 9167 | self.status.serialize_into(bytes); |
| 9168 | bytes.extend_from_slice(&[0; 23]); |
| 9169 | } |
| 9170 | } |
| 9171 | |
| 9172 | /// Opcode for the ListDeviceProperties request |
| 9173 | pub const LIST_DEVICE_PROPERTIES_REQUEST: u8 = 36; |
| 9174 | #[derive (Clone, Copy, Default)] |
| 9175 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 9176 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 9177 | pub struct ListDevicePropertiesRequest { |
| 9178 | pub device_id: u8, |
| 9179 | } |
| 9180 | impl_debug_if_no_extra_traits!(ListDevicePropertiesRequest, "ListDevicePropertiesRequest" ); |
| 9181 | impl ListDevicePropertiesRequest { |
| 9182 | /// Serialize this request into bytes for the provided connection |
| 9183 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 9184 | let length_so_far = 0; |
| 9185 | let device_id_bytes = self.device_id.serialize(); |
| 9186 | let mut request0 = vec![ |
| 9187 | major_opcode, |
| 9188 | LIST_DEVICE_PROPERTIES_REQUEST, |
| 9189 | 0, |
| 9190 | 0, |
| 9191 | device_id_bytes[0], |
| 9192 | 0, |
| 9193 | 0, |
| 9194 | 0, |
| 9195 | ]; |
| 9196 | let length_so_far = length_so_far + request0.len(); |
| 9197 | assert_eq!(length_so_far % 4, 0); |
| 9198 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 9199 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 9200 | ([request0.into()], vec![]) |
| 9201 | } |
| 9202 | /// Parse this request given its header, its body, and any fds that go along with it |
| 9203 | #[cfg (feature = "request-parsing" )] |
| 9204 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 9205 | if header.minor_opcode != LIST_DEVICE_PROPERTIES_REQUEST { |
| 9206 | return Err(ParseError::InvalidValue); |
| 9207 | } |
| 9208 | let (device_id, remaining) = u8::try_parse(value)?; |
| 9209 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 9210 | let _ = remaining; |
| 9211 | Ok(ListDevicePropertiesRequest { |
| 9212 | device_id, |
| 9213 | }) |
| 9214 | } |
| 9215 | } |
| 9216 | impl Request for ListDevicePropertiesRequest { |
| 9217 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 9218 | |
| 9219 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 9220 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 9221 | // Flatten the buffers into a single vector |
| 9222 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 9223 | (buf, fds) |
| 9224 | } |
| 9225 | } |
| 9226 | impl crate::x11_utils::ReplyRequest for ListDevicePropertiesRequest { |
| 9227 | type Reply = ListDevicePropertiesReply; |
| 9228 | } |
| 9229 | |
| 9230 | #[derive (Clone, Default)] |
| 9231 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 9232 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 9233 | pub struct ListDevicePropertiesReply { |
| 9234 | pub xi_reply_type: u8, |
| 9235 | pub sequence: u16, |
| 9236 | pub length: u32, |
| 9237 | pub atoms: Vec<xproto::Atom>, |
| 9238 | } |
| 9239 | impl_debug_if_no_extra_traits!(ListDevicePropertiesReply, "ListDevicePropertiesReply" ); |
| 9240 | impl TryParse for ListDevicePropertiesReply { |
| 9241 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 9242 | let remaining: &[u8] = initial_value; |
| 9243 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 9244 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 9245 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 9246 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 9247 | let (num_atoms: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 9248 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
| 9249 | let (atoms: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<xproto::Atom>(data:remaining, list_length:num_atoms.try_to_usize()?)?; |
| 9250 | if response_type != 1 { |
| 9251 | return Err(ParseError::InvalidValue); |
| 9252 | } |
| 9253 | let result: ListDevicePropertiesReply = ListDevicePropertiesReply { xi_reply_type, sequence, length, atoms }; |
| 9254 | let _ = remaining; |
| 9255 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 9256 | .ok_or(err:ParseError::InsufficientData)?; |
| 9257 | Ok((result, remaining)) |
| 9258 | } |
| 9259 | } |
| 9260 | impl Serialize for ListDevicePropertiesReply { |
| 9261 | type Bytes = Vec<u8>; |
| 9262 | fn serialize(&self) -> Vec<u8> { |
| 9263 | let mut result: Vec = Vec::new(); |
| 9264 | self.serialize_into(&mut result); |
| 9265 | result |
| 9266 | } |
| 9267 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 9268 | bytes.reserve(additional:32); |
| 9269 | let response_type_bytes: &[u8; 1] = &[1]; |
| 9270 | bytes.push(response_type_bytes[0]); |
| 9271 | self.xi_reply_type.serialize_into(bytes); |
| 9272 | self.sequence.serialize_into(bytes); |
| 9273 | self.length.serialize_into(bytes); |
| 9274 | let num_atoms: u16 = u16::try_from(self.atoms.len()).expect(msg:"`atoms` has too many elements" ); |
| 9275 | num_atoms.serialize_into(bytes); |
| 9276 | bytes.extend_from_slice(&[0; 22]); |
| 9277 | self.atoms.serialize_into(bytes); |
| 9278 | } |
| 9279 | } |
| 9280 | impl ListDevicePropertiesReply { |
| 9281 | /// Get the value of the `num_atoms` field. |
| 9282 | /// |
| 9283 | /// The `num_atoms` field is used as the length field of the `atoms` field. |
| 9284 | /// This function computes the field's value again based on the length of the list. |
| 9285 | /// |
| 9286 | /// # Panics |
| 9287 | /// |
| 9288 | /// Panics if the value cannot be represented in the target type. This |
| 9289 | /// cannot happen with values of the struct received from the X11 server. |
| 9290 | pub fn num_atoms(&self) -> u16 { |
| 9291 | self.atoms.len() |
| 9292 | .try_into().unwrap() |
| 9293 | } |
| 9294 | } |
| 9295 | |
| 9296 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 9297 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 9298 | pub struct PropertyFormat(u8); |
| 9299 | impl PropertyFormat { |
| 9300 | pub const M8_BITS: Self = Self(8); |
| 9301 | pub const M16_BITS: Self = Self(16); |
| 9302 | pub const M32_BITS: Self = Self(32); |
| 9303 | } |
| 9304 | impl From<PropertyFormat> for u8 { |
| 9305 | #[inline ] |
| 9306 | fn from(input: PropertyFormat) -> Self { |
| 9307 | input.0 |
| 9308 | } |
| 9309 | } |
| 9310 | impl From<PropertyFormat> for Option<u8> { |
| 9311 | #[inline ] |
| 9312 | fn from(input: PropertyFormat) -> Self { |
| 9313 | Some(input.0) |
| 9314 | } |
| 9315 | } |
| 9316 | impl From<PropertyFormat> for u16 { |
| 9317 | #[inline ] |
| 9318 | fn from(input: PropertyFormat) -> Self { |
| 9319 | u16::from(input.0) |
| 9320 | } |
| 9321 | } |
| 9322 | impl From<PropertyFormat> for Option<u16> { |
| 9323 | #[inline ] |
| 9324 | fn from(input: PropertyFormat) -> Self { |
| 9325 | Some(u16::from(input.0)) |
| 9326 | } |
| 9327 | } |
| 9328 | impl From<PropertyFormat> for u32 { |
| 9329 | #[inline ] |
| 9330 | fn from(input: PropertyFormat) -> Self { |
| 9331 | u32::from(input.0) |
| 9332 | } |
| 9333 | } |
| 9334 | impl From<PropertyFormat> for Option<u32> { |
| 9335 | #[inline ] |
| 9336 | fn from(input: PropertyFormat) -> Self { |
| 9337 | Some(u32::from(input.0)) |
| 9338 | } |
| 9339 | } |
| 9340 | impl From<u8> for PropertyFormat { |
| 9341 | #[inline ] |
| 9342 | fn from(value: u8) -> Self { |
| 9343 | Self(value) |
| 9344 | } |
| 9345 | } |
| 9346 | impl core::fmt::Debug for PropertyFormat { |
| 9347 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 9348 | let variants: [(u32, &'static str, &'static …); 3] = [ |
| 9349 | (Self::M8_BITS.0.into(), "M8_BITS" , "M8Bits" ), |
| 9350 | (Self::M16_BITS.0.into(), "M16_BITS" , "M16Bits" ), |
| 9351 | (Self::M32_BITS.0.into(), "M32_BITS" , "M32Bits" ), |
| 9352 | ]; |
| 9353 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 9354 | } |
| 9355 | } |
| 9356 | |
| 9357 | #[derive (Clone)] |
| 9358 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 9359 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 9360 | pub enum ChangeDevicePropertyAux { |
| 9361 | Data8(Vec<u8>), |
| 9362 | Data16(Vec<u16>), |
| 9363 | Data32(Vec<u32>), |
| 9364 | /// This variant is returned when the server sends a discriminant |
| 9365 | /// value that does not match any of the defined by the protocol. |
| 9366 | /// |
| 9367 | /// Usually, this should be considered a parsing error, but there |
| 9368 | /// are some cases where the server violates the protocol. |
| 9369 | /// |
| 9370 | /// Trying to use `serialize` or `serialize_into` with this variant |
| 9371 | /// will raise a panic. |
| 9372 | InvalidValue(u8), |
| 9373 | } |
| 9374 | impl_debug_if_no_extra_traits!(ChangeDevicePropertyAux, "ChangeDevicePropertyAux" ); |
| 9375 | impl ChangeDevicePropertyAux { |
| 9376 | #[cfg_attr (not(feature = "request-parsing" ), allow(dead_code))] |
| 9377 | fn try_parse(value: &[u8], format: u8, num_items: u32) -> Result<(Self, &[u8]), ParseError> { |
| 9378 | let switch_expr = u8::from(format); |
| 9379 | let mut outer_remaining = value; |
| 9380 | let mut parse_result = None; |
| 9381 | if switch_expr == u8::from(PropertyFormat::M8_BITS) { |
| 9382 | let remaining = outer_remaining; |
| 9383 | let value = remaining; |
| 9384 | let (data8, remaining) = crate::x11_utils::parse_u8_list(remaining, num_items.try_to_usize()?)?; |
| 9385 | let data8 = data8.to_vec(); |
| 9386 | // Align offset to multiple of 4 |
| 9387 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 9388 | let misalignment = (4 - (offset % 4)) % 4; |
| 9389 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
| 9390 | outer_remaining = remaining; |
| 9391 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 9392 | parse_result = Some(ChangeDevicePropertyAux::Data8(data8)); |
| 9393 | } |
| 9394 | if switch_expr == u8::from(PropertyFormat::M16_BITS) { |
| 9395 | let remaining = outer_remaining; |
| 9396 | let value = remaining; |
| 9397 | let (data16, remaining) = crate::x11_utils::parse_list::<u16>(remaining, num_items.try_to_usize()?)?; |
| 9398 | // Align offset to multiple of 4 |
| 9399 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 9400 | let misalignment = (4 - (offset % 4)) % 4; |
| 9401 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
| 9402 | outer_remaining = remaining; |
| 9403 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 9404 | parse_result = Some(ChangeDevicePropertyAux::Data16(data16)); |
| 9405 | } |
| 9406 | if switch_expr == u8::from(PropertyFormat::M32_BITS) { |
| 9407 | let remaining = outer_remaining; |
| 9408 | let (data32, remaining) = crate::x11_utils::parse_list::<u32>(remaining, num_items.try_to_usize()?)?; |
| 9409 | outer_remaining = remaining; |
| 9410 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 9411 | parse_result = Some(ChangeDevicePropertyAux::Data32(data32)); |
| 9412 | } |
| 9413 | match parse_result { |
| 9414 | None => Ok((ChangeDevicePropertyAux::InvalidValue(switch_expr), &[])), |
| 9415 | Some(result) => Ok((result, outer_remaining)), |
| 9416 | } |
| 9417 | } |
| 9418 | } |
| 9419 | impl ChangeDevicePropertyAux { |
| 9420 | pub fn as_data8(&self) -> Option<&Vec<u8>> { |
| 9421 | match self { |
| 9422 | ChangeDevicePropertyAux::Data8(value: &Vec) => Some(value), |
| 9423 | _ => None, |
| 9424 | } |
| 9425 | } |
| 9426 | pub fn as_data16(&self) -> Option<&Vec<u16>> { |
| 9427 | match self { |
| 9428 | ChangeDevicePropertyAux::Data16(value: &Vec) => Some(value), |
| 9429 | _ => None, |
| 9430 | } |
| 9431 | } |
| 9432 | pub fn as_data32(&self) -> Option<&Vec<u32>> { |
| 9433 | match self { |
| 9434 | ChangeDevicePropertyAux::Data32(value: &Vec) => Some(value), |
| 9435 | _ => None, |
| 9436 | } |
| 9437 | } |
| 9438 | } |
| 9439 | impl ChangeDevicePropertyAux { |
| 9440 | #[allow (dead_code)] |
| 9441 | fn serialize(&self, format: u8, num_items: u32) -> Vec<u8> { |
| 9442 | let mut result = Vec::new(); |
| 9443 | self.serialize_into(&mut result, u8::from(format), u32::from(num_items)); |
| 9444 | result |
| 9445 | } |
| 9446 | fn serialize_into(&self, bytes: &mut Vec<u8>, format: u8, num_items: u32) { |
| 9447 | assert_eq!(self.switch_expr(), u8::from(format), "switch `items` has an inconsistent discriminant" ); |
| 9448 | match self { |
| 9449 | ChangeDevicePropertyAux::Data8(data8) => { |
| 9450 | assert_eq!(data8.len(), usize::try_from(num_items).unwrap(), "`data8` has an incorrect length" ); |
| 9451 | bytes.extend_from_slice(&data8); |
| 9452 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 9453 | } |
| 9454 | ChangeDevicePropertyAux::Data16(data16) => { |
| 9455 | assert_eq!(data16.len(), usize::try_from(num_items).unwrap(), "`data16` has an incorrect length" ); |
| 9456 | data16.serialize_into(bytes); |
| 9457 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 9458 | } |
| 9459 | ChangeDevicePropertyAux::Data32(data32) => { |
| 9460 | assert_eq!(data32.len(), usize::try_from(num_items).unwrap(), "`data32` has an incorrect length" ); |
| 9461 | data32.serialize_into(bytes); |
| 9462 | } |
| 9463 | ChangeDevicePropertyAux::InvalidValue(_) => panic!("attempted to serialize invalid switch case" ), |
| 9464 | } |
| 9465 | } |
| 9466 | } |
| 9467 | impl ChangeDevicePropertyAux { |
| 9468 | fn switch_expr(&self) -> u8 { |
| 9469 | match self { |
| 9470 | ChangeDevicePropertyAux::Data8(_) => u8::from(PropertyFormat::M8_BITS), |
| 9471 | ChangeDevicePropertyAux::Data16(_) => u8::from(PropertyFormat::M16_BITS), |
| 9472 | ChangeDevicePropertyAux::Data32(_) => u8::from(PropertyFormat::M32_BITS), |
| 9473 | ChangeDevicePropertyAux::InvalidValue(switch_expr: &u8) => *switch_expr, |
| 9474 | } |
| 9475 | } |
| 9476 | } |
| 9477 | |
| 9478 | /// Opcode for the ChangeDeviceProperty request |
| 9479 | pub const CHANGE_DEVICE_PROPERTY_REQUEST: u8 = 37; |
| 9480 | #[derive (Clone)] |
| 9481 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 9482 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 9483 | pub struct ChangeDevicePropertyRequest<'input> { |
| 9484 | pub property: xproto::Atom, |
| 9485 | pub type_: xproto::Atom, |
| 9486 | pub device_id: u8, |
| 9487 | pub mode: xproto::PropMode, |
| 9488 | pub num_items: u32, |
| 9489 | pub items: Cow<'input, ChangeDevicePropertyAux>, |
| 9490 | } |
| 9491 | impl_debug_if_no_extra_traits!(ChangeDevicePropertyRequest<'_>, "ChangeDevicePropertyRequest" ); |
| 9492 | impl<'input> ChangeDevicePropertyRequest<'input> { |
| 9493 | /// Serialize this request into bytes for the provided connection |
| 9494 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 9495 | let length_so_far = 0; |
| 9496 | let property_bytes = self.property.serialize(); |
| 9497 | let type_bytes = self.type_.serialize(); |
| 9498 | let device_id_bytes = self.device_id.serialize(); |
| 9499 | let format: u8 = self.items.switch_expr(); |
| 9500 | let format_bytes = format.serialize(); |
| 9501 | let mode_bytes = u8::from(self.mode).serialize(); |
| 9502 | let num_items_bytes = self.num_items.serialize(); |
| 9503 | let mut request0 = vec![ |
| 9504 | major_opcode, |
| 9505 | CHANGE_DEVICE_PROPERTY_REQUEST, |
| 9506 | 0, |
| 9507 | 0, |
| 9508 | property_bytes[0], |
| 9509 | property_bytes[1], |
| 9510 | property_bytes[2], |
| 9511 | property_bytes[3], |
| 9512 | type_bytes[0], |
| 9513 | type_bytes[1], |
| 9514 | type_bytes[2], |
| 9515 | type_bytes[3], |
| 9516 | device_id_bytes[0], |
| 9517 | format_bytes[0], |
| 9518 | mode_bytes[0], |
| 9519 | 0, |
| 9520 | num_items_bytes[0], |
| 9521 | num_items_bytes[1], |
| 9522 | num_items_bytes[2], |
| 9523 | num_items_bytes[3], |
| 9524 | ]; |
| 9525 | let length_so_far = length_so_far + request0.len(); |
| 9526 | let items_bytes = self.items.serialize(u8::from(format), u32::from(self.num_items)); |
| 9527 | let length_so_far = length_so_far + items_bytes.len(); |
| 9528 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 9529 | let length_so_far = length_so_far + padding0.len(); |
| 9530 | assert_eq!(length_so_far % 4, 0); |
| 9531 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 9532 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 9533 | ([request0.into(), items_bytes.into(), padding0.into()], vec![]) |
| 9534 | } |
| 9535 | /// Parse this request given its header, its body, and any fds that go along with it |
| 9536 | #[cfg (feature = "request-parsing" )] |
| 9537 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 9538 | if header.minor_opcode != CHANGE_DEVICE_PROPERTY_REQUEST { |
| 9539 | return Err(ParseError::InvalidValue); |
| 9540 | } |
| 9541 | let (property, remaining) = xproto::Atom::try_parse(value)?; |
| 9542 | let (type_, remaining) = xproto::Atom::try_parse(remaining)?; |
| 9543 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 9544 | let (format, remaining) = u8::try_parse(remaining)?; |
| 9545 | let (mode, remaining) = u8::try_parse(remaining)?; |
| 9546 | let mode = mode.into(); |
| 9547 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
| 9548 | let (num_items, remaining) = u32::try_parse(remaining)?; |
| 9549 | let (items, remaining) = ChangeDevicePropertyAux::try_parse(remaining, u8::from(format), u32::from(num_items))?; |
| 9550 | let _ = remaining; |
| 9551 | Ok(ChangeDevicePropertyRequest { |
| 9552 | property, |
| 9553 | type_, |
| 9554 | device_id, |
| 9555 | mode, |
| 9556 | num_items, |
| 9557 | items: Cow::Owned(items), |
| 9558 | }) |
| 9559 | } |
| 9560 | /// Clone all borrowed data in this ChangeDevicePropertyRequest. |
| 9561 | pub fn into_owned(self) -> ChangeDevicePropertyRequest<'static> { |
| 9562 | ChangeDevicePropertyRequest { |
| 9563 | property: self.property, |
| 9564 | type_: self.type_, |
| 9565 | device_id: self.device_id, |
| 9566 | mode: self.mode, |
| 9567 | num_items: self.num_items, |
| 9568 | items: Cow::Owned(self.items.into_owned()), |
| 9569 | } |
| 9570 | } |
| 9571 | } |
| 9572 | impl<'input> Request for ChangeDevicePropertyRequest<'input> { |
| 9573 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 9574 | |
| 9575 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 9576 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 9577 | // Flatten the buffers into a single vector |
| 9578 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 9579 | (buf, fds) |
| 9580 | } |
| 9581 | } |
| 9582 | impl<'input> crate::x11_utils::VoidRequest for ChangeDevicePropertyRequest<'input> { |
| 9583 | } |
| 9584 | |
| 9585 | /// Opcode for the DeleteDeviceProperty request |
| 9586 | pub const DELETE_DEVICE_PROPERTY_REQUEST: u8 = 38; |
| 9587 | #[derive (Clone, Copy, Default)] |
| 9588 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 9589 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 9590 | pub struct DeleteDevicePropertyRequest { |
| 9591 | pub property: xproto::Atom, |
| 9592 | pub device_id: u8, |
| 9593 | } |
| 9594 | impl_debug_if_no_extra_traits!(DeleteDevicePropertyRequest, "DeleteDevicePropertyRequest" ); |
| 9595 | impl DeleteDevicePropertyRequest { |
| 9596 | /// Serialize this request into bytes for the provided connection |
| 9597 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 9598 | let length_so_far = 0; |
| 9599 | let property_bytes = self.property.serialize(); |
| 9600 | let device_id_bytes = self.device_id.serialize(); |
| 9601 | let mut request0 = vec![ |
| 9602 | major_opcode, |
| 9603 | DELETE_DEVICE_PROPERTY_REQUEST, |
| 9604 | 0, |
| 9605 | 0, |
| 9606 | property_bytes[0], |
| 9607 | property_bytes[1], |
| 9608 | property_bytes[2], |
| 9609 | property_bytes[3], |
| 9610 | device_id_bytes[0], |
| 9611 | 0, |
| 9612 | 0, |
| 9613 | 0, |
| 9614 | ]; |
| 9615 | let length_so_far = length_so_far + request0.len(); |
| 9616 | assert_eq!(length_so_far % 4, 0); |
| 9617 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 9618 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 9619 | ([request0.into()], vec![]) |
| 9620 | } |
| 9621 | /// Parse this request given its header, its body, and any fds that go along with it |
| 9622 | #[cfg (feature = "request-parsing" )] |
| 9623 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 9624 | if header.minor_opcode != DELETE_DEVICE_PROPERTY_REQUEST { |
| 9625 | return Err(ParseError::InvalidValue); |
| 9626 | } |
| 9627 | let (property, remaining) = xproto::Atom::try_parse(value)?; |
| 9628 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 9629 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 9630 | let _ = remaining; |
| 9631 | Ok(DeleteDevicePropertyRequest { |
| 9632 | property, |
| 9633 | device_id, |
| 9634 | }) |
| 9635 | } |
| 9636 | } |
| 9637 | impl Request for DeleteDevicePropertyRequest { |
| 9638 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 9639 | |
| 9640 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 9641 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 9642 | // Flatten the buffers into a single vector |
| 9643 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 9644 | (buf, fds) |
| 9645 | } |
| 9646 | } |
| 9647 | impl crate::x11_utils::VoidRequest for DeleteDevicePropertyRequest { |
| 9648 | } |
| 9649 | |
| 9650 | /// Opcode for the GetDeviceProperty request |
| 9651 | pub const GET_DEVICE_PROPERTY_REQUEST: u8 = 39; |
| 9652 | #[derive (Clone, Copy, Default)] |
| 9653 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 9654 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 9655 | pub struct GetDevicePropertyRequest { |
| 9656 | pub property: xproto::Atom, |
| 9657 | pub type_: xproto::Atom, |
| 9658 | pub offset: u32, |
| 9659 | pub len: u32, |
| 9660 | pub device_id: u8, |
| 9661 | pub delete: bool, |
| 9662 | } |
| 9663 | impl_debug_if_no_extra_traits!(GetDevicePropertyRequest, "GetDevicePropertyRequest" ); |
| 9664 | impl GetDevicePropertyRequest { |
| 9665 | /// Serialize this request into bytes for the provided connection |
| 9666 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 9667 | let length_so_far = 0; |
| 9668 | let property_bytes = self.property.serialize(); |
| 9669 | let type_bytes = self.type_.serialize(); |
| 9670 | let offset_bytes = self.offset.serialize(); |
| 9671 | let len_bytes = self.len.serialize(); |
| 9672 | let device_id_bytes = self.device_id.serialize(); |
| 9673 | let delete_bytes = self.delete.serialize(); |
| 9674 | let mut request0 = vec![ |
| 9675 | major_opcode, |
| 9676 | GET_DEVICE_PROPERTY_REQUEST, |
| 9677 | 0, |
| 9678 | 0, |
| 9679 | property_bytes[0], |
| 9680 | property_bytes[1], |
| 9681 | property_bytes[2], |
| 9682 | property_bytes[3], |
| 9683 | type_bytes[0], |
| 9684 | type_bytes[1], |
| 9685 | type_bytes[2], |
| 9686 | type_bytes[3], |
| 9687 | offset_bytes[0], |
| 9688 | offset_bytes[1], |
| 9689 | offset_bytes[2], |
| 9690 | offset_bytes[3], |
| 9691 | len_bytes[0], |
| 9692 | len_bytes[1], |
| 9693 | len_bytes[2], |
| 9694 | len_bytes[3], |
| 9695 | device_id_bytes[0], |
| 9696 | delete_bytes[0], |
| 9697 | 0, |
| 9698 | 0, |
| 9699 | ]; |
| 9700 | let length_so_far = length_so_far + request0.len(); |
| 9701 | assert_eq!(length_so_far % 4, 0); |
| 9702 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 9703 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 9704 | ([request0.into()], vec![]) |
| 9705 | } |
| 9706 | /// Parse this request given its header, its body, and any fds that go along with it |
| 9707 | #[cfg (feature = "request-parsing" )] |
| 9708 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 9709 | if header.minor_opcode != GET_DEVICE_PROPERTY_REQUEST { |
| 9710 | return Err(ParseError::InvalidValue); |
| 9711 | } |
| 9712 | let (property, remaining) = xproto::Atom::try_parse(value)?; |
| 9713 | let (type_, remaining) = xproto::Atom::try_parse(remaining)?; |
| 9714 | let (offset, remaining) = u32::try_parse(remaining)?; |
| 9715 | let (len, remaining) = u32::try_parse(remaining)?; |
| 9716 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 9717 | let (delete, remaining) = bool::try_parse(remaining)?; |
| 9718 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 9719 | let _ = remaining; |
| 9720 | Ok(GetDevicePropertyRequest { |
| 9721 | property, |
| 9722 | type_, |
| 9723 | offset, |
| 9724 | len, |
| 9725 | device_id, |
| 9726 | delete, |
| 9727 | }) |
| 9728 | } |
| 9729 | } |
| 9730 | impl Request for GetDevicePropertyRequest { |
| 9731 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 9732 | |
| 9733 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 9734 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 9735 | // Flatten the buffers into a single vector |
| 9736 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 9737 | (buf, fds) |
| 9738 | } |
| 9739 | } |
| 9740 | impl crate::x11_utils::ReplyRequest for GetDevicePropertyRequest { |
| 9741 | type Reply = GetDevicePropertyReply; |
| 9742 | } |
| 9743 | |
| 9744 | #[derive (Clone)] |
| 9745 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 9746 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 9747 | pub enum GetDevicePropertyItems { |
| 9748 | Data8(Vec<u8>), |
| 9749 | Data16(Vec<u16>), |
| 9750 | Data32(Vec<u32>), |
| 9751 | /// This variant is returned when the server sends a discriminant |
| 9752 | /// value that does not match any of the defined by the protocol. |
| 9753 | /// |
| 9754 | /// Usually, this should be considered a parsing error, but there |
| 9755 | /// are some cases where the server violates the protocol. |
| 9756 | /// |
| 9757 | /// Trying to use `serialize` or `serialize_into` with this variant |
| 9758 | /// will raise a panic. |
| 9759 | InvalidValue(u8), |
| 9760 | } |
| 9761 | impl_debug_if_no_extra_traits!(GetDevicePropertyItems, "GetDevicePropertyItems" ); |
| 9762 | impl GetDevicePropertyItems { |
| 9763 | #[cfg_attr (not(feature = "request-parsing" ), allow(dead_code))] |
| 9764 | fn try_parse(value: &[u8], format: u8, num_items: u32) -> Result<(Self, &[u8]), ParseError> { |
| 9765 | let switch_expr = u8::from(format); |
| 9766 | let mut outer_remaining = value; |
| 9767 | let mut parse_result = None; |
| 9768 | if switch_expr == u8::from(PropertyFormat::M8_BITS) { |
| 9769 | let remaining = outer_remaining; |
| 9770 | let value = remaining; |
| 9771 | let (data8, remaining) = crate::x11_utils::parse_u8_list(remaining, num_items.try_to_usize()?)?; |
| 9772 | let data8 = data8.to_vec(); |
| 9773 | // Align offset to multiple of 4 |
| 9774 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 9775 | let misalignment = (4 - (offset % 4)) % 4; |
| 9776 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
| 9777 | outer_remaining = remaining; |
| 9778 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 9779 | parse_result = Some(GetDevicePropertyItems::Data8(data8)); |
| 9780 | } |
| 9781 | if switch_expr == u8::from(PropertyFormat::M16_BITS) { |
| 9782 | let remaining = outer_remaining; |
| 9783 | let value = remaining; |
| 9784 | let (data16, remaining) = crate::x11_utils::parse_list::<u16>(remaining, num_items.try_to_usize()?)?; |
| 9785 | // Align offset to multiple of 4 |
| 9786 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 9787 | let misalignment = (4 - (offset % 4)) % 4; |
| 9788 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
| 9789 | outer_remaining = remaining; |
| 9790 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 9791 | parse_result = Some(GetDevicePropertyItems::Data16(data16)); |
| 9792 | } |
| 9793 | if switch_expr == u8::from(PropertyFormat::M32_BITS) { |
| 9794 | let remaining = outer_remaining; |
| 9795 | let (data32, remaining) = crate::x11_utils::parse_list::<u32>(remaining, num_items.try_to_usize()?)?; |
| 9796 | outer_remaining = remaining; |
| 9797 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 9798 | parse_result = Some(GetDevicePropertyItems::Data32(data32)); |
| 9799 | } |
| 9800 | match parse_result { |
| 9801 | None => Ok((GetDevicePropertyItems::InvalidValue(switch_expr), &[])), |
| 9802 | Some(result) => Ok((result, outer_remaining)), |
| 9803 | } |
| 9804 | } |
| 9805 | } |
| 9806 | impl GetDevicePropertyItems { |
| 9807 | pub fn as_data8(&self) -> Option<&Vec<u8>> { |
| 9808 | match self { |
| 9809 | GetDevicePropertyItems::Data8(value: &Vec) => Some(value), |
| 9810 | _ => None, |
| 9811 | } |
| 9812 | } |
| 9813 | pub fn as_data16(&self) -> Option<&Vec<u16>> { |
| 9814 | match self { |
| 9815 | GetDevicePropertyItems::Data16(value: &Vec) => Some(value), |
| 9816 | _ => None, |
| 9817 | } |
| 9818 | } |
| 9819 | pub fn as_data32(&self) -> Option<&Vec<u32>> { |
| 9820 | match self { |
| 9821 | GetDevicePropertyItems::Data32(value: &Vec) => Some(value), |
| 9822 | _ => None, |
| 9823 | } |
| 9824 | } |
| 9825 | } |
| 9826 | impl GetDevicePropertyItems { |
| 9827 | #[allow (dead_code)] |
| 9828 | fn serialize(&self, format: u8, num_items: u32) -> Vec<u8> { |
| 9829 | let mut result = Vec::new(); |
| 9830 | self.serialize_into(&mut result, u8::from(format), u32::from(num_items)); |
| 9831 | result |
| 9832 | } |
| 9833 | fn serialize_into(&self, bytes: &mut Vec<u8>, format: u8, num_items: u32) { |
| 9834 | assert_eq!(self.switch_expr(), u8::from(format), "switch `items` has an inconsistent discriminant" ); |
| 9835 | match self { |
| 9836 | GetDevicePropertyItems::Data8(data8) => { |
| 9837 | assert_eq!(data8.len(), usize::try_from(num_items).unwrap(), "`data8` has an incorrect length" ); |
| 9838 | bytes.extend_from_slice(&data8); |
| 9839 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 9840 | } |
| 9841 | GetDevicePropertyItems::Data16(data16) => { |
| 9842 | assert_eq!(data16.len(), usize::try_from(num_items).unwrap(), "`data16` has an incorrect length" ); |
| 9843 | data16.serialize_into(bytes); |
| 9844 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 9845 | } |
| 9846 | GetDevicePropertyItems::Data32(data32) => { |
| 9847 | assert_eq!(data32.len(), usize::try_from(num_items).unwrap(), "`data32` has an incorrect length" ); |
| 9848 | data32.serialize_into(bytes); |
| 9849 | } |
| 9850 | GetDevicePropertyItems::InvalidValue(_) => panic!("attempted to serialize invalid switch case" ), |
| 9851 | } |
| 9852 | } |
| 9853 | } |
| 9854 | impl GetDevicePropertyItems { |
| 9855 | fn switch_expr(&self) -> u8 { |
| 9856 | match self { |
| 9857 | GetDevicePropertyItems::Data8(_) => u8::from(PropertyFormat::M8_BITS), |
| 9858 | GetDevicePropertyItems::Data16(_) => u8::from(PropertyFormat::M16_BITS), |
| 9859 | GetDevicePropertyItems::Data32(_) => u8::from(PropertyFormat::M32_BITS), |
| 9860 | GetDevicePropertyItems::InvalidValue(switch_expr: &u8) => *switch_expr, |
| 9861 | } |
| 9862 | } |
| 9863 | } |
| 9864 | |
| 9865 | #[derive (Clone)] |
| 9866 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 9867 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 9868 | pub struct GetDevicePropertyReply { |
| 9869 | pub xi_reply_type: u8, |
| 9870 | pub sequence: u16, |
| 9871 | pub length: u32, |
| 9872 | pub type_: xproto::Atom, |
| 9873 | pub bytes_after: u32, |
| 9874 | pub num_items: u32, |
| 9875 | pub device_id: u8, |
| 9876 | pub items: GetDevicePropertyItems, |
| 9877 | } |
| 9878 | impl_debug_if_no_extra_traits!(GetDevicePropertyReply, "GetDevicePropertyReply" ); |
| 9879 | impl TryParse for GetDevicePropertyReply { |
| 9880 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 9881 | let remaining: &[u8] = initial_value; |
| 9882 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 9883 | let (xi_reply_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 9884 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 9885 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 9886 | let (type_: u32, remaining: &[u8]) = xproto::Atom::try_parse(remaining)?; |
| 9887 | let (bytes_after: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 9888 | let (num_items: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 9889 | let (format: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 9890 | let (device_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 9891 | let remaining: &[u8] = remaining.get(10..).ok_or(err:ParseError::InsufficientData)?; |
| 9892 | let (items: GetDevicePropertyItems, remaining: &[u8]) = GetDevicePropertyItems::try_parse(value:remaining, format:u8::from(format), num_items:u32::from(num_items))?; |
| 9893 | if response_type != 1 { |
| 9894 | return Err(ParseError::InvalidValue); |
| 9895 | } |
| 9896 | let result: GetDevicePropertyReply = GetDevicePropertyReply { xi_reply_type, sequence, length, type_, bytes_after, num_items, device_id, items }; |
| 9897 | let _ = remaining; |
| 9898 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 9899 | .ok_or(err:ParseError::InsufficientData)?; |
| 9900 | Ok((result, remaining)) |
| 9901 | } |
| 9902 | } |
| 9903 | impl Serialize for GetDevicePropertyReply { |
| 9904 | type Bytes = Vec<u8>; |
| 9905 | fn serialize(&self) -> Vec<u8> { |
| 9906 | let mut result: Vec = Vec::new(); |
| 9907 | self.serialize_into(&mut result); |
| 9908 | result |
| 9909 | } |
| 9910 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 9911 | bytes.reserve(additional:32); |
| 9912 | let response_type_bytes: &[u8; 1] = &[1]; |
| 9913 | bytes.push(response_type_bytes[0]); |
| 9914 | self.xi_reply_type.serialize_into(bytes); |
| 9915 | self.sequence.serialize_into(bytes); |
| 9916 | self.length.serialize_into(bytes); |
| 9917 | self.type_.serialize_into(bytes); |
| 9918 | self.bytes_after.serialize_into(bytes); |
| 9919 | self.num_items.serialize_into(bytes); |
| 9920 | let format: u8 = self.items.switch_expr(); |
| 9921 | format.serialize_into(bytes); |
| 9922 | self.device_id.serialize_into(bytes); |
| 9923 | bytes.extend_from_slice(&[0; 10]); |
| 9924 | self.items.serialize_into(bytes, format:u8::from(format), num_items:u32::from(self.num_items)); |
| 9925 | } |
| 9926 | } |
| 9927 | |
| 9928 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 9929 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 9930 | pub struct Device(bool); |
| 9931 | impl Device { |
| 9932 | pub const ALL: Self = Self(false); |
| 9933 | pub const ALL_MASTER: Self = Self(true); |
| 9934 | } |
| 9935 | impl From<Device> for bool { |
| 9936 | #[inline ] |
| 9937 | fn from(input: Device) -> Self { |
| 9938 | input.0 |
| 9939 | } |
| 9940 | } |
| 9941 | impl From<Device> for Option<bool> { |
| 9942 | #[inline ] |
| 9943 | fn from(input: Device) -> Self { |
| 9944 | Some(input.0) |
| 9945 | } |
| 9946 | } |
| 9947 | impl From<Device> for u8 { |
| 9948 | #[inline ] |
| 9949 | fn from(input: Device) -> Self { |
| 9950 | u8::from(input.0) |
| 9951 | } |
| 9952 | } |
| 9953 | impl From<Device> for Option<u8> { |
| 9954 | #[inline ] |
| 9955 | fn from(input: Device) -> Self { |
| 9956 | Some(u8::from(input.0)) |
| 9957 | } |
| 9958 | } |
| 9959 | impl From<Device> for u16 { |
| 9960 | #[inline ] |
| 9961 | fn from(input: Device) -> Self { |
| 9962 | u16::from(input.0) |
| 9963 | } |
| 9964 | } |
| 9965 | impl From<Device> for Option<u16> { |
| 9966 | #[inline ] |
| 9967 | fn from(input: Device) -> Self { |
| 9968 | Some(u16::from(input.0)) |
| 9969 | } |
| 9970 | } |
| 9971 | impl From<Device> for u32 { |
| 9972 | #[inline ] |
| 9973 | fn from(input: Device) -> Self { |
| 9974 | u32::from(input.0) |
| 9975 | } |
| 9976 | } |
| 9977 | impl From<Device> for Option<u32> { |
| 9978 | #[inline ] |
| 9979 | fn from(input: Device) -> Self { |
| 9980 | Some(u32::from(input.0)) |
| 9981 | } |
| 9982 | } |
| 9983 | impl From<bool> for Device { |
| 9984 | #[inline ] |
| 9985 | fn from(value: bool) -> Self { |
| 9986 | Self(value) |
| 9987 | } |
| 9988 | } |
| 9989 | impl core::fmt::Debug for Device { |
| 9990 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 9991 | let variants: [(u32, &'static str, &'static …); 2] = [ |
| 9992 | (Self::ALL.0.into(), "ALL" , "All" ), |
| 9993 | (Self::ALL_MASTER.0.into(), "ALL_MASTER" , "AllMaster" ), |
| 9994 | ]; |
| 9995 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 9996 | } |
| 9997 | } |
| 9998 | |
| 9999 | #[derive (Clone, Copy, Default)] |
| 10000 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10001 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10002 | pub struct GroupInfo { |
| 10003 | pub base: u8, |
| 10004 | pub latched: u8, |
| 10005 | pub locked: u8, |
| 10006 | pub effective: u8, |
| 10007 | } |
| 10008 | impl_debug_if_no_extra_traits!(GroupInfo, "GroupInfo" ); |
| 10009 | impl TryParse for GroupInfo { |
| 10010 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 10011 | let (base: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 10012 | let (latched: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 10013 | let (locked: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 10014 | let (effective: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 10015 | let result: GroupInfo = GroupInfo { base, latched, locked, effective }; |
| 10016 | Ok((result, remaining)) |
| 10017 | } |
| 10018 | } |
| 10019 | impl Serialize for GroupInfo { |
| 10020 | type Bytes = [u8; 4]; |
| 10021 | fn serialize(&self) -> [u8; 4] { |
| 10022 | let base_bytes: [u8; 1] = self.base.serialize(); |
| 10023 | let latched_bytes: [u8; 1] = self.latched.serialize(); |
| 10024 | let locked_bytes: [u8; 1] = self.locked.serialize(); |
| 10025 | let effective_bytes: [u8; 1] = self.effective.serialize(); |
| 10026 | [ |
| 10027 | base_bytes[0], |
| 10028 | latched_bytes[0], |
| 10029 | locked_bytes[0], |
| 10030 | effective_bytes[0], |
| 10031 | ] |
| 10032 | } |
| 10033 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 10034 | bytes.reserve(additional:4); |
| 10035 | self.base.serialize_into(bytes); |
| 10036 | self.latched.serialize_into(bytes); |
| 10037 | self.locked.serialize_into(bytes); |
| 10038 | self.effective.serialize_into(bytes); |
| 10039 | } |
| 10040 | } |
| 10041 | |
| 10042 | #[derive (Clone, Copy, Default)] |
| 10043 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10044 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10045 | pub struct ModifierInfo { |
| 10046 | pub base: u32, |
| 10047 | pub latched: u32, |
| 10048 | pub locked: u32, |
| 10049 | pub effective: u32, |
| 10050 | } |
| 10051 | impl_debug_if_no_extra_traits!(ModifierInfo, "ModifierInfo" ); |
| 10052 | impl TryParse for ModifierInfo { |
| 10053 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 10054 | let (base: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 10055 | let (latched: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 10056 | let (locked: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 10057 | let (effective: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 10058 | let result: ModifierInfo = ModifierInfo { base, latched, locked, effective }; |
| 10059 | Ok((result, remaining)) |
| 10060 | } |
| 10061 | } |
| 10062 | impl Serialize for ModifierInfo { |
| 10063 | type Bytes = [u8; 16]; |
| 10064 | fn serialize(&self) -> [u8; 16] { |
| 10065 | let base_bytes = self.base.serialize(); |
| 10066 | let latched_bytes = self.latched.serialize(); |
| 10067 | let locked_bytes = self.locked.serialize(); |
| 10068 | let effective_bytes = self.effective.serialize(); |
| 10069 | [ |
| 10070 | base_bytes[0], |
| 10071 | base_bytes[1], |
| 10072 | base_bytes[2], |
| 10073 | base_bytes[3], |
| 10074 | latched_bytes[0], |
| 10075 | latched_bytes[1], |
| 10076 | latched_bytes[2], |
| 10077 | latched_bytes[3], |
| 10078 | locked_bytes[0], |
| 10079 | locked_bytes[1], |
| 10080 | locked_bytes[2], |
| 10081 | locked_bytes[3], |
| 10082 | effective_bytes[0], |
| 10083 | effective_bytes[1], |
| 10084 | effective_bytes[2], |
| 10085 | effective_bytes[3], |
| 10086 | ] |
| 10087 | } |
| 10088 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 10089 | bytes.reserve(16); |
| 10090 | self.base.serialize_into(bytes); |
| 10091 | self.latched.serialize_into(bytes); |
| 10092 | self.locked.serialize_into(bytes); |
| 10093 | self.effective.serialize_into(bytes); |
| 10094 | } |
| 10095 | } |
| 10096 | |
| 10097 | /// Opcode for the XIQueryPointer request |
| 10098 | pub const XI_QUERY_POINTER_REQUEST: u8 = 40; |
| 10099 | #[derive (Clone, Copy, Default)] |
| 10100 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10101 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10102 | pub struct XIQueryPointerRequest { |
| 10103 | pub window: xproto::Window, |
| 10104 | pub deviceid: DeviceId, |
| 10105 | } |
| 10106 | impl_debug_if_no_extra_traits!(XIQueryPointerRequest, "XIQueryPointerRequest" ); |
| 10107 | impl XIQueryPointerRequest { |
| 10108 | /// Serialize this request into bytes for the provided connection |
| 10109 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 10110 | let length_so_far = 0; |
| 10111 | let window_bytes = self.window.serialize(); |
| 10112 | let deviceid_bytes = self.deviceid.serialize(); |
| 10113 | let mut request0 = vec![ |
| 10114 | major_opcode, |
| 10115 | XI_QUERY_POINTER_REQUEST, |
| 10116 | 0, |
| 10117 | 0, |
| 10118 | window_bytes[0], |
| 10119 | window_bytes[1], |
| 10120 | window_bytes[2], |
| 10121 | window_bytes[3], |
| 10122 | deviceid_bytes[0], |
| 10123 | deviceid_bytes[1], |
| 10124 | 0, |
| 10125 | 0, |
| 10126 | ]; |
| 10127 | let length_so_far = length_so_far + request0.len(); |
| 10128 | assert_eq!(length_so_far % 4, 0); |
| 10129 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 10130 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 10131 | ([request0.into()], vec![]) |
| 10132 | } |
| 10133 | /// Parse this request given its header, its body, and any fds that go along with it |
| 10134 | #[cfg (feature = "request-parsing" )] |
| 10135 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 10136 | if header.minor_opcode != XI_QUERY_POINTER_REQUEST { |
| 10137 | return Err(ParseError::InvalidValue); |
| 10138 | } |
| 10139 | let (window, remaining) = xproto::Window::try_parse(value)?; |
| 10140 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 10141 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 10142 | let _ = remaining; |
| 10143 | Ok(XIQueryPointerRequest { |
| 10144 | window, |
| 10145 | deviceid, |
| 10146 | }) |
| 10147 | } |
| 10148 | } |
| 10149 | impl Request for XIQueryPointerRequest { |
| 10150 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 10151 | |
| 10152 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 10153 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 10154 | // Flatten the buffers into a single vector |
| 10155 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 10156 | (buf, fds) |
| 10157 | } |
| 10158 | } |
| 10159 | impl crate::x11_utils::ReplyRequest for XIQueryPointerRequest { |
| 10160 | type Reply = XIQueryPointerReply; |
| 10161 | } |
| 10162 | |
| 10163 | #[derive (Clone, Default)] |
| 10164 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10165 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10166 | pub struct XIQueryPointerReply { |
| 10167 | pub sequence: u16, |
| 10168 | pub length: u32, |
| 10169 | pub root: xproto::Window, |
| 10170 | pub child: xproto::Window, |
| 10171 | pub root_x: Fp1616, |
| 10172 | pub root_y: Fp1616, |
| 10173 | pub win_x: Fp1616, |
| 10174 | pub win_y: Fp1616, |
| 10175 | pub same_screen: bool, |
| 10176 | pub mods: ModifierInfo, |
| 10177 | pub group: GroupInfo, |
| 10178 | pub buttons: Vec<u32>, |
| 10179 | } |
| 10180 | impl_debug_if_no_extra_traits!(XIQueryPointerReply, "XIQueryPointerReply" ); |
| 10181 | impl TryParse for XIQueryPointerReply { |
| 10182 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 10183 | let remaining = initial_value; |
| 10184 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 10185 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
| 10186 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 10187 | let (length, remaining) = u32::try_parse(remaining)?; |
| 10188 | let (root, remaining) = xproto::Window::try_parse(remaining)?; |
| 10189 | let (child, remaining) = xproto::Window::try_parse(remaining)?; |
| 10190 | let (root_x, remaining) = Fp1616::try_parse(remaining)?; |
| 10191 | let (root_y, remaining) = Fp1616::try_parse(remaining)?; |
| 10192 | let (win_x, remaining) = Fp1616::try_parse(remaining)?; |
| 10193 | let (win_y, remaining) = Fp1616::try_parse(remaining)?; |
| 10194 | let (same_screen, remaining) = bool::try_parse(remaining)?; |
| 10195 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
| 10196 | let (buttons_len, remaining) = u16::try_parse(remaining)?; |
| 10197 | let (mods, remaining) = ModifierInfo::try_parse(remaining)?; |
| 10198 | let (group, remaining) = GroupInfo::try_parse(remaining)?; |
| 10199 | let (buttons, remaining) = crate::x11_utils::parse_list::<u32>(remaining, buttons_len.try_to_usize()?)?; |
| 10200 | if response_type != 1 { |
| 10201 | return Err(ParseError::InvalidValue); |
| 10202 | } |
| 10203 | let result = XIQueryPointerReply { sequence, length, root, child, root_x, root_y, win_x, win_y, same_screen, mods, group, buttons }; |
| 10204 | let _ = remaining; |
| 10205 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 10206 | .ok_or(ParseError::InsufficientData)?; |
| 10207 | Ok((result, remaining)) |
| 10208 | } |
| 10209 | } |
| 10210 | impl Serialize for XIQueryPointerReply { |
| 10211 | type Bytes = Vec<u8>; |
| 10212 | fn serialize(&self) -> Vec<u8> { |
| 10213 | let mut result = Vec::new(); |
| 10214 | self.serialize_into(&mut result); |
| 10215 | result |
| 10216 | } |
| 10217 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 10218 | bytes.reserve(56); |
| 10219 | let response_type_bytes = &[1]; |
| 10220 | bytes.push(response_type_bytes[0]); |
| 10221 | bytes.extend_from_slice(&[0; 1]); |
| 10222 | self.sequence.serialize_into(bytes); |
| 10223 | self.length.serialize_into(bytes); |
| 10224 | self.root.serialize_into(bytes); |
| 10225 | self.child.serialize_into(bytes); |
| 10226 | self.root_x.serialize_into(bytes); |
| 10227 | self.root_y.serialize_into(bytes); |
| 10228 | self.win_x.serialize_into(bytes); |
| 10229 | self.win_y.serialize_into(bytes); |
| 10230 | self.same_screen.serialize_into(bytes); |
| 10231 | bytes.extend_from_slice(&[0; 1]); |
| 10232 | let buttons_len = u16::try_from(self.buttons.len()).expect("`buttons` has too many elements" ); |
| 10233 | buttons_len.serialize_into(bytes); |
| 10234 | self.mods.serialize_into(bytes); |
| 10235 | self.group.serialize_into(bytes); |
| 10236 | self.buttons.serialize_into(bytes); |
| 10237 | } |
| 10238 | } |
| 10239 | impl XIQueryPointerReply { |
| 10240 | /// Get the value of the `buttons_len` field. |
| 10241 | /// |
| 10242 | /// The `buttons_len` field is used as the length field of the `buttons` field. |
| 10243 | /// This function computes the field's value again based on the length of the list. |
| 10244 | /// |
| 10245 | /// # Panics |
| 10246 | /// |
| 10247 | /// Panics if the value cannot be represented in the target type. This |
| 10248 | /// cannot happen with values of the struct received from the X11 server. |
| 10249 | pub fn buttons_len(&self) -> u16 { |
| 10250 | self.buttons.len() |
| 10251 | .try_into().unwrap() |
| 10252 | } |
| 10253 | } |
| 10254 | |
| 10255 | /// Opcode for the XIWarpPointer request |
| 10256 | pub const XI_WARP_POINTER_REQUEST: u8 = 41; |
| 10257 | #[derive (Clone, Copy, Default)] |
| 10258 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10259 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10260 | pub struct XIWarpPointerRequest { |
| 10261 | pub src_win: xproto::Window, |
| 10262 | pub dst_win: xproto::Window, |
| 10263 | pub src_x: Fp1616, |
| 10264 | pub src_y: Fp1616, |
| 10265 | pub src_width: u16, |
| 10266 | pub src_height: u16, |
| 10267 | pub dst_x: Fp1616, |
| 10268 | pub dst_y: Fp1616, |
| 10269 | pub deviceid: DeviceId, |
| 10270 | } |
| 10271 | impl_debug_if_no_extra_traits!(XIWarpPointerRequest, "XIWarpPointerRequest" ); |
| 10272 | impl XIWarpPointerRequest { |
| 10273 | /// Serialize this request into bytes for the provided connection |
| 10274 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 10275 | let length_so_far = 0; |
| 10276 | let src_win_bytes = self.src_win.serialize(); |
| 10277 | let dst_win_bytes = self.dst_win.serialize(); |
| 10278 | let src_x_bytes = self.src_x.serialize(); |
| 10279 | let src_y_bytes = self.src_y.serialize(); |
| 10280 | let src_width_bytes = self.src_width.serialize(); |
| 10281 | let src_height_bytes = self.src_height.serialize(); |
| 10282 | let dst_x_bytes = self.dst_x.serialize(); |
| 10283 | let dst_y_bytes = self.dst_y.serialize(); |
| 10284 | let deviceid_bytes = self.deviceid.serialize(); |
| 10285 | let mut request0 = vec![ |
| 10286 | major_opcode, |
| 10287 | XI_WARP_POINTER_REQUEST, |
| 10288 | 0, |
| 10289 | 0, |
| 10290 | src_win_bytes[0], |
| 10291 | src_win_bytes[1], |
| 10292 | src_win_bytes[2], |
| 10293 | src_win_bytes[3], |
| 10294 | dst_win_bytes[0], |
| 10295 | dst_win_bytes[1], |
| 10296 | dst_win_bytes[2], |
| 10297 | dst_win_bytes[3], |
| 10298 | src_x_bytes[0], |
| 10299 | src_x_bytes[1], |
| 10300 | src_x_bytes[2], |
| 10301 | src_x_bytes[3], |
| 10302 | src_y_bytes[0], |
| 10303 | src_y_bytes[1], |
| 10304 | src_y_bytes[2], |
| 10305 | src_y_bytes[3], |
| 10306 | src_width_bytes[0], |
| 10307 | src_width_bytes[1], |
| 10308 | src_height_bytes[0], |
| 10309 | src_height_bytes[1], |
| 10310 | dst_x_bytes[0], |
| 10311 | dst_x_bytes[1], |
| 10312 | dst_x_bytes[2], |
| 10313 | dst_x_bytes[3], |
| 10314 | dst_y_bytes[0], |
| 10315 | dst_y_bytes[1], |
| 10316 | dst_y_bytes[2], |
| 10317 | dst_y_bytes[3], |
| 10318 | deviceid_bytes[0], |
| 10319 | deviceid_bytes[1], |
| 10320 | 0, |
| 10321 | 0, |
| 10322 | ]; |
| 10323 | let length_so_far = length_so_far + request0.len(); |
| 10324 | assert_eq!(length_so_far % 4, 0); |
| 10325 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 10326 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 10327 | ([request0.into()], vec![]) |
| 10328 | } |
| 10329 | /// Parse this request given its header, its body, and any fds that go along with it |
| 10330 | #[cfg (feature = "request-parsing" )] |
| 10331 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 10332 | if header.minor_opcode != XI_WARP_POINTER_REQUEST { |
| 10333 | return Err(ParseError::InvalidValue); |
| 10334 | } |
| 10335 | let (src_win, remaining) = xproto::Window::try_parse(value)?; |
| 10336 | let (dst_win, remaining) = xproto::Window::try_parse(remaining)?; |
| 10337 | let (src_x, remaining) = Fp1616::try_parse(remaining)?; |
| 10338 | let (src_y, remaining) = Fp1616::try_parse(remaining)?; |
| 10339 | let (src_width, remaining) = u16::try_parse(remaining)?; |
| 10340 | let (src_height, remaining) = u16::try_parse(remaining)?; |
| 10341 | let (dst_x, remaining) = Fp1616::try_parse(remaining)?; |
| 10342 | let (dst_y, remaining) = Fp1616::try_parse(remaining)?; |
| 10343 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 10344 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 10345 | let _ = remaining; |
| 10346 | Ok(XIWarpPointerRequest { |
| 10347 | src_win, |
| 10348 | dst_win, |
| 10349 | src_x, |
| 10350 | src_y, |
| 10351 | src_width, |
| 10352 | src_height, |
| 10353 | dst_x, |
| 10354 | dst_y, |
| 10355 | deviceid, |
| 10356 | }) |
| 10357 | } |
| 10358 | } |
| 10359 | impl Request for XIWarpPointerRequest { |
| 10360 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 10361 | |
| 10362 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 10363 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 10364 | // Flatten the buffers into a single vector |
| 10365 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 10366 | (buf, fds) |
| 10367 | } |
| 10368 | } |
| 10369 | impl crate::x11_utils::VoidRequest for XIWarpPointerRequest { |
| 10370 | } |
| 10371 | |
| 10372 | /// Opcode for the XIChangeCursor request |
| 10373 | pub const XI_CHANGE_CURSOR_REQUEST: u8 = 42; |
| 10374 | #[derive (Clone, Copy, Default)] |
| 10375 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10376 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10377 | pub struct XIChangeCursorRequest { |
| 10378 | pub window: xproto::Window, |
| 10379 | pub cursor: xproto::Cursor, |
| 10380 | pub deviceid: DeviceId, |
| 10381 | } |
| 10382 | impl_debug_if_no_extra_traits!(XIChangeCursorRequest, "XIChangeCursorRequest" ); |
| 10383 | impl XIChangeCursorRequest { |
| 10384 | /// Serialize this request into bytes for the provided connection |
| 10385 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 10386 | let length_so_far = 0; |
| 10387 | let window_bytes = self.window.serialize(); |
| 10388 | let cursor_bytes = self.cursor.serialize(); |
| 10389 | let deviceid_bytes = self.deviceid.serialize(); |
| 10390 | let mut request0 = vec![ |
| 10391 | major_opcode, |
| 10392 | XI_CHANGE_CURSOR_REQUEST, |
| 10393 | 0, |
| 10394 | 0, |
| 10395 | window_bytes[0], |
| 10396 | window_bytes[1], |
| 10397 | window_bytes[2], |
| 10398 | window_bytes[3], |
| 10399 | cursor_bytes[0], |
| 10400 | cursor_bytes[1], |
| 10401 | cursor_bytes[2], |
| 10402 | cursor_bytes[3], |
| 10403 | deviceid_bytes[0], |
| 10404 | deviceid_bytes[1], |
| 10405 | 0, |
| 10406 | 0, |
| 10407 | ]; |
| 10408 | let length_so_far = length_so_far + request0.len(); |
| 10409 | assert_eq!(length_so_far % 4, 0); |
| 10410 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 10411 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 10412 | ([request0.into()], vec![]) |
| 10413 | } |
| 10414 | /// Parse this request given its header, its body, and any fds that go along with it |
| 10415 | #[cfg (feature = "request-parsing" )] |
| 10416 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 10417 | if header.minor_opcode != XI_CHANGE_CURSOR_REQUEST { |
| 10418 | return Err(ParseError::InvalidValue); |
| 10419 | } |
| 10420 | let (window, remaining) = xproto::Window::try_parse(value)?; |
| 10421 | let (cursor, remaining) = xproto::Cursor::try_parse(remaining)?; |
| 10422 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 10423 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 10424 | let _ = remaining; |
| 10425 | Ok(XIChangeCursorRequest { |
| 10426 | window, |
| 10427 | cursor, |
| 10428 | deviceid, |
| 10429 | }) |
| 10430 | } |
| 10431 | } |
| 10432 | impl Request for XIChangeCursorRequest { |
| 10433 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 10434 | |
| 10435 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 10436 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 10437 | // Flatten the buffers into a single vector |
| 10438 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 10439 | (buf, fds) |
| 10440 | } |
| 10441 | } |
| 10442 | impl crate::x11_utils::VoidRequest for XIChangeCursorRequest { |
| 10443 | } |
| 10444 | |
| 10445 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 10446 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10447 | pub struct HierarchyChangeType(u16); |
| 10448 | impl HierarchyChangeType { |
| 10449 | pub const ADD_MASTER: Self = Self(1); |
| 10450 | pub const REMOVE_MASTER: Self = Self(2); |
| 10451 | pub const ATTACH_SLAVE: Self = Self(3); |
| 10452 | pub const DETACH_SLAVE: Self = Self(4); |
| 10453 | } |
| 10454 | impl From<HierarchyChangeType> for u16 { |
| 10455 | #[inline ] |
| 10456 | fn from(input: HierarchyChangeType) -> Self { |
| 10457 | input.0 |
| 10458 | } |
| 10459 | } |
| 10460 | impl From<HierarchyChangeType> for Option<u16> { |
| 10461 | #[inline ] |
| 10462 | fn from(input: HierarchyChangeType) -> Self { |
| 10463 | Some(input.0) |
| 10464 | } |
| 10465 | } |
| 10466 | impl From<HierarchyChangeType> for u32 { |
| 10467 | #[inline ] |
| 10468 | fn from(input: HierarchyChangeType) -> Self { |
| 10469 | u32::from(input.0) |
| 10470 | } |
| 10471 | } |
| 10472 | impl From<HierarchyChangeType> for Option<u32> { |
| 10473 | #[inline ] |
| 10474 | fn from(input: HierarchyChangeType) -> Self { |
| 10475 | Some(u32::from(input.0)) |
| 10476 | } |
| 10477 | } |
| 10478 | impl From<u8> for HierarchyChangeType { |
| 10479 | #[inline ] |
| 10480 | fn from(value: u8) -> Self { |
| 10481 | Self(value.into()) |
| 10482 | } |
| 10483 | } |
| 10484 | impl From<u16> for HierarchyChangeType { |
| 10485 | #[inline ] |
| 10486 | fn from(value: u16) -> Self { |
| 10487 | Self(value) |
| 10488 | } |
| 10489 | } |
| 10490 | impl core::fmt::Debug for HierarchyChangeType { |
| 10491 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 10492 | let variants: [(u32, &'static str, &'static …); 4] = [ |
| 10493 | (Self::ADD_MASTER.0.into(), "ADD_MASTER" , "AddMaster" ), |
| 10494 | (Self::REMOVE_MASTER.0.into(), "REMOVE_MASTER" , "RemoveMaster" ), |
| 10495 | (Self::ATTACH_SLAVE.0.into(), "ATTACH_SLAVE" , "AttachSlave" ), |
| 10496 | (Self::DETACH_SLAVE.0.into(), "DETACH_SLAVE" , "DetachSlave" ), |
| 10497 | ]; |
| 10498 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 10499 | } |
| 10500 | } |
| 10501 | |
| 10502 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 10503 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10504 | pub struct ChangeMode(u8); |
| 10505 | impl ChangeMode { |
| 10506 | pub const ATTACH: Self = Self(1); |
| 10507 | pub const FLOAT: Self = Self(2); |
| 10508 | } |
| 10509 | impl From<ChangeMode> for u8 { |
| 10510 | #[inline ] |
| 10511 | fn from(input: ChangeMode) -> Self { |
| 10512 | input.0 |
| 10513 | } |
| 10514 | } |
| 10515 | impl From<ChangeMode> for Option<u8> { |
| 10516 | #[inline ] |
| 10517 | fn from(input: ChangeMode) -> Self { |
| 10518 | Some(input.0) |
| 10519 | } |
| 10520 | } |
| 10521 | impl From<ChangeMode> for u16 { |
| 10522 | #[inline ] |
| 10523 | fn from(input: ChangeMode) -> Self { |
| 10524 | u16::from(input.0) |
| 10525 | } |
| 10526 | } |
| 10527 | impl From<ChangeMode> for Option<u16> { |
| 10528 | #[inline ] |
| 10529 | fn from(input: ChangeMode) -> Self { |
| 10530 | Some(u16::from(input.0)) |
| 10531 | } |
| 10532 | } |
| 10533 | impl From<ChangeMode> for u32 { |
| 10534 | #[inline ] |
| 10535 | fn from(input: ChangeMode) -> Self { |
| 10536 | u32::from(input.0) |
| 10537 | } |
| 10538 | } |
| 10539 | impl From<ChangeMode> for Option<u32> { |
| 10540 | #[inline ] |
| 10541 | fn from(input: ChangeMode) -> Self { |
| 10542 | Some(u32::from(input.0)) |
| 10543 | } |
| 10544 | } |
| 10545 | impl From<u8> for ChangeMode { |
| 10546 | #[inline ] |
| 10547 | fn from(value: u8) -> Self { |
| 10548 | Self(value) |
| 10549 | } |
| 10550 | } |
| 10551 | impl core::fmt::Debug for ChangeMode { |
| 10552 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 10553 | let variants: [(u32, &'static str, &'static …); 2] = [ |
| 10554 | (Self::ATTACH.0.into(), "ATTACH" , "Attach" ), |
| 10555 | (Self::FLOAT.0.into(), "FLOAT" , "Float" ), |
| 10556 | ]; |
| 10557 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 10558 | } |
| 10559 | } |
| 10560 | |
| 10561 | #[derive (Clone, Default)] |
| 10562 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10563 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10564 | pub struct AddMaster { |
| 10565 | pub type_: HierarchyChangeType, |
| 10566 | pub len: u16, |
| 10567 | pub send_core: bool, |
| 10568 | pub enable: bool, |
| 10569 | pub name: Vec<u8>, |
| 10570 | } |
| 10571 | impl_debug_if_no_extra_traits!(AddMaster, "AddMaster" ); |
| 10572 | impl TryParse for AddMaster { |
| 10573 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 10574 | let value: &[u8] = remaining; |
| 10575 | let (type_: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 10576 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 10577 | let (name_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 10578 | let (send_core: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
| 10579 | let (enable: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
| 10580 | let (name: &[u8], remaining: &[u8]) = crate::x11_utils::parse_u8_list(data:remaining, list_length:name_len.try_to_usize()?)?; |
| 10581 | let name: Vec = name.to_vec(); |
| 10582 | // Align offset to multiple of 4 |
| 10583 | let offset: usize = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 10584 | let misalignment: usize = (4 - (offset % 4)) % 4; |
| 10585 | let remaining: &[u8] = remaining.get(misalignment..).ok_or(err:ParseError::InsufficientData)?; |
| 10586 | let type_: HierarchyChangeType = type_.into(); |
| 10587 | let result: AddMaster = AddMaster { type_, len, send_core, enable, name }; |
| 10588 | Ok((result, remaining)) |
| 10589 | } |
| 10590 | } |
| 10591 | impl Serialize for AddMaster { |
| 10592 | type Bytes = Vec<u8>; |
| 10593 | fn serialize(&self) -> Vec<u8> { |
| 10594 | let mut result: Vec = Vec::new(); |
| 10595 | self.serialize_into(&mut result); |
| 10596 | result |
| 10597 | } |
| 10598 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 10599 | bytes.reserve(additional:8); |
| 10600 | u16::from(self.type_).serialize_into(bytes); |
| 10601 | self.len.serialize_into(bytes); |
| 10602 | let name_len: u16 = u16::try_from(self.name.len()).expect(msg:"`name` has too many elements" ); |
| 10603 | name_len.serialize_into(bytes); |
| 10604 | self.send_core.serialize_into(bytes); |
| 10605 | self.enable.serialize_into(bytes); |
| 10606 | bytes.extend_from_slice(&self.name); |
| 10607 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 10608 | } |
| 10609 | } |
| 10610 | impl AddMaster { |
| 10611 | /// Get the value of the `name_len` field. |
| 10612 | /// |
| 10613 | /// The `name_len` field is used as the length field of the `name` field. |
| 10614 | /// This function computes the field's value again based on the length of the list. |
| 10615 | /// |
| 10616 | /// # Panics |
| 10617 | /// |
| 10618 | /// Panics if the value cannot be represented in the target type. This |
| 10619 | /// cannot happen with values of the struct received from the X11 server. |
| 10620 | pub fn name_len(&self) -> u16 { |
| 10621 | self.name.len() |
| 10622 | .try_into().unwrap() |
| 10623 | } |
| 10624 | } |
| 10625 | |
| 10626 | #[derive (Clone, Copy, Default)] |
| 10627 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10628 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10629 | pub struct RemoveMaster { |
| 10630 | pub type_: HierarchyChangeType, |
| 10631 | pub len: u16, |
| 10632 | pub deviceid: DeviceId, |
| 10633 | pub return_mode: ChangeMode, |
| 10634 | pub return_pointer: DeviceId, |
| 10635 | pub return_keyboard: DeviceId, |
| 10636 | } |
| 10637 | impl_debug_if_no_extra_traits!(RemoveMaster, "RemoveMaster" ); |
| 10638 | impl TryParse for RemoveMaster { |
| 10639 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 10640 | let (type_: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 10641 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 10642 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 10643 | let (return_mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 10644 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 10645 | let (return_pointer: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 10646 | let (return_keyboard: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 10647 | let type_: HierarchyChangeType = type_.into(); |
| 10648 | let return_mode: ChangeMode = return_mode.into(); |
| 10649 | let result: RemoveMaster = RemoveMaster { type_, len, deviceid, return_mode, return_pointer, return_keyboard }; |
| 10650 | Ok((result, remaining)) |
| 10651 | } |
| 10652 | } |
| 10653 | impl Serialize for RemoveMaster { |
| 10654 | type Bytes = [u8; 12]; |
| 10655 | fn serialize(&self) -> [u8; 12] { |
| 10656 | let type_bytes = u16::from(self.type_).serialize(); |
| 10657 | let len_bytes = self.len.serialize(); |
| 10658 | let deviceid_bytes = self.deviceid.serialize(); |
| 10659 | let return_mode_bytes = u8::from(self.return_mode).serialize(); |
| 10660 | let return_pointer_bytes = self.return_pointer.serialize(); |
| 10661 | let return_keyboard_bytes = self.return_keyboard.serialize(); |
| 10662 | [ |
| 10663 | type_bytes[0], |
| 10664 | type_bytes[1], |
| 10665 | len_bytes[0], |
| 10666 | len_bytes[1], |
| 10667 | deviceid_bytes[0], |
| 10668 | deviceid_bytes[1], |
| 10669 | return_mode_bytes[0], |
| 10670 | 0, |
| 10671 | return_pointer_bytes[0], |
| 10672 | return_pointer_bytes[1], |
| 10673 | return_keyboard_bytes[0], |
| 10674 | return_keyboard_bytes[1], |
| 10675 | ] |
| 10676 | } |
| 10677 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 10678 | bytes.reserve(12); |
| 10679 | u16::from(self.type_).serialize_into(bytes); |
| 10680 | self.len.serialize_into(bytes); |
| 10681 | self.deviceid.serialize_into(bytes); |
| 10682 | u8::from(self.return_mode).serialize_into(bytes); |
| 10683 | bytes.extend_from_slice(&[0; 1]); |
| 10684 | self.return_pointer.serialize_into(bytes); |
| 10685 | self.return_keyboard.serialize_into(bytes); |
| 10686 | } |
| 10687 | } |
| 10688 | |
| 10689 | #[derive (Clone, Copy, Default)] |
| 10690 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10691 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10692 | pub struct AttachSlave { |
| 10693 | pub type_: HierarchyChangeType, |
| 10694 | pub len: u16, |
| 10695 | pub deviceid: DeviceId, |
| 10696 | pub master: DeviceId, |
| 10697 | } |
| 10698 | impl_debug_if_no_extra_traits!(AttachSlave, "AttachSlave" ); |
| 10699 | impl TryParse for AttachSlave { |
| 10700 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 10701 | let (type_: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 10702 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 10703 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 10704 | let (master: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 10705 | let type_: HierarchyChangeType = type_.into(); |
| 10706 | let result: AttachSlave = AttachSlave { type_, len, deviceid, master }; |
| 10707 | Ok((result, remaining)) |
| 10708 | } |
| 10709 | } |
| 10710 | impl Serialize for AttachSlave { |
| 10711 | type Bytes = [u8; 8]; |
| 10712 | fn serialize(&self) -> [u8; 8] { |
| 10713 | let type_bytes = u16::from(self.type_).serialize(); |
| 10714 | let len_bytes = self.len.serialize(); |
| 10715 | let deviceid_bytes = self.deviceid.serialize(); |
| 10716 | let master_bytes = self.master.serialize(); |
| 10717 | [ |
| 10718 | type_bytes[0], |
| 10719 | type_bytes[1], |
| 10720 | len_bytes[0], |
| 10721 | len_bytes[1], |
| 10722 | deviceid_bytes[0], |
| 10723 | deviceid_bytes[1], |
| 10724 | master_bytes[0], |
| 10725 | master_bytes[1], |
| 10726 | ] |
| 10727 | } |
| 10728 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 10729 | bytes.reserve(8); |
| 10730 | u16::from(self.type_).serialize_into(bytes); |
| 10731 | self.len.serialize_into(bytes); |
| 10732 | self.deviceid.serialize_into(bytes); |
| 10733 | self.master.serialize_into(bytes); |
| 10734 | } |
| 10735 | } |
| 10736 | |
| 10737 | #[derive (Clone, Copy, Default)] |
| 10738 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10739 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10740 | pub struct DetachSlave { |
| 10741 | pub type_: HierarchyChangeType, |
| 10742 | pub len: u16, |
| 10743 | pub deviceid: DeviceId, |
| 10744 | } |
| 10745 | impl_debug_if_no_extra_traits!(DetachSlave, "DetachSlave" ); |
| 10746 | impl TryParse for DetachSlave { |
| 10747 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 10748 | let (type_: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 10749 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 10750 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 10751 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 10752 | let type_: HierarchyChangeType = type_.into(); |
| 10753 | let result: DetachSlave = DetachSlave { type_, len, deviceid }; |
| 10754 | Ok((result, remaining)) |
| 10755 | } |
| 10756 | } |
| 10757 | impl Serialize for DetachSlave { |
| 10758 | type Bytes = [u8; 8]; |
| 10759 | fn serialize(&self) -> [u8; 8] { |
| 10760 | let type_bytes = u16::from(self.type_).serialize(); |
| 10761 | let len_bytes = self.len.serialize(); |
| 10762 | let deviceid_bytes = self.deviceid.serialize(); |
| 10763 | [ |
| 10764 | type_bytes[0], |
| 10765 | type_bytes[1], |
| 10766 | len_bytes[0], |
| 10767 | len_bytes[1], |
| 10768 | deviceid_bytes[0], |
| 10769 | deviceid_bytes[1], |
| 10770 | 0, |
| 10771 | 0, |
| 10772 | ] |
| 10773 | } |
| 10774 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 10775 | bytes.reserve(8); |
| 10776 | u16::from(self.type_).serialize_into(bytes); |
| 10777 | self.len.serialize_into(bytes); |
| 10778 | self.deviceid.serialize_into(bytes); |
| 10779 | bytes.extend_from_slice(&[0; 2]); |
| 10780 | } |
| 10781 | } |
| 10782 | |
| 10783 | #[derive (Clone)] |
| 10784 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10785 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10786 | pub struct HierarchyChangeDataAddMaster { |
| 10787 | pub send_core: bool, |
| 10788 | pub enable: bool, |
| 10789 | pub name: Vec<u8>, |
| 10790 | } |
| 10791 | impl_debug_if_no_extra_traits!(HierarchyChangeDataAddMaster, "HierarchyChangeDataAddMaster" ); |
| 10792 | impl TryParse for HierarchyChangeDataAddMaster { |
| 10793 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 10794 | let value: &[u8] = remaining; |
| 10795 | let (name_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 10796 | let (send_core: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
| 10797 | let (enable: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
| 10798 | let (name: &[u8], remaining: &[u8]) = crate::x11_utils::parse_u8_list(data:remaining, list_length:name_len.try_to_usize()?)?; |
| 10799 | let name: Vec = name.to_vec(); |
| 10800 | // Align offset to multiple of 4 |
| 10801 | let offset: usize = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 10802 | let misalignment: usize = (4 - (offset % 4)) % 4; |
| 10803 | let remaining: &[u8] = remaining.get(misalignment..).ok_or(err:ParseError::InsufficientData)?; |
| 10804 | let result: HierarchyChangeDataAddMaster = HierarchyChangeDataAddMaster { send_core, enable, name }; |
| 10805 | Ok((result, remaining)) |
| 10806 | } |
| 10807 | } |
| 10808 | impl Serialize for HierarchyChangeDataAddMaster { |
| 10809 | type Bytes = Vec<u8>; |
| 10810 | fn serialize(&self) -> Vec<u8> { |
| 10811 | let mut result: Vec = Vec::new(); |
| 10812 | self.serialize_into(&mut result); |
| 10813 | result |
| 10814 | } |
| 10815 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 10816 | bytes.reserve(additional:4); |
| 10817 | let name_len: u16 = u16::try_from(self.name.len()).expect(msg:"`name` has too many elements" ); |
| 10818 | name_len.serialize_into(bytes); |
| 10819 | self.send_core.serialize_into(bytes); |
| 10820 | self.enable.serialize_into(bytes); |
| 10821 | bytes.extend_from_slice(&self.name); |
| 10822 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 10823 | } |
| 10824 | } |
| 10825 | impl HierarchyChangeDataAddMaster { |
| 10826 | /// Get the value of the `name_len` field. |
| 10827 | /// |
| 10828 | /// The `name_len` field is used as the length field of the `name` field. |
| 10829 | /// This function computes the field's value again based on the length of the list. |
| 10830 | /// |
| 10831 | /// # Panics |
| 10832 | /// |
| 10833 | /// Panics if the value cannot be represented in the target type. This |
| 10834 | /// cannot happen with values of the struct received from the X11 server. |
| 10835 | pub fn name_len(&self) -> u16 { |
| 10836 | self.name.len() |
| 10837 | .try_into().unwrap() |
| 10838 | } |
| 10839 | } |
| 10840 | #[derive (Clone, Copy)] |
| 10841 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10842 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10843 | pub struct HierarchyChangeDataRemoveMaster { |
| 10844 | pub deviceid: DeviceId, |
| 10845 | pub return_mode: ChangeMode, |
| 10846 | pub return_pointer: DeviceId, |
| 10847 | pub return_keyboard: DeviceId, |
| 10848 | } |
| 10849 | impl_debug_if_no_extra_traits!(HierarchyChangeDataRemoveMaster, "HierarchyChangeDataRemoveMaster" ); |
| 10850 | impl TryParse for HierarchyChangeDataRemoveMaster { |
| 10851 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 10852 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 10853 | let (return_mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 10854 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 10855 | let (return_pointer: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 10856 | let (return_keyboard: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 10857 | let return_mode: ChangeMode = return_mode.into(); |
| 10858 | let result: HierarchyChangeDataRemoveMaster = HierarchyChangeDataRemoveMaster { deviceid, return_mode, return_pointer, return_keyboard }; |
| 10859 | Ok((result, remaining)) |
| 10860 | } |
| 10861 | } |
| 10862 | impl Serialize for HierarchyChangeDataRemoveMaster { |
| 10863 | type Bytes = [u8; 8]; |
| 10864 | fn serialize(&self) -> [u8; 8] { |
| 10865 | let deviceid_bytes = self.deviceid.serialize(); |
| 10866 | let return_mode_bytes = u8::from(self.return_mode).serialize(); |
| 10867 | let return_pointer_bytes = self.return_pointer.serialize(); |
| 10868 | let return_keyboard_bytes = self.return_keyboard.serialize(); |
| 10869 | [ |
| 10870 | deviceid_bytes[0], |
| 10871 | deviceid_bytes[1], |
| 10872 | return_mode_bytes[0], |
| 10873 | 0, |
| 10874 | return_pointer_bytes[0], |
| 10875 | return_pointer_bytes[1], |
| 10876 | return_keyboard_bytes[0], |
| 10877 | return_keyboard_bytes[1], |
| 10878 | ] |
| 10879 | } |
| 10880 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 10881 | bytes.reserve(8); |
| 10882 | self.deviceid.serialize_into(bytes); |
| 10883 | u8::from(self.return_mode).serialize_into(bytes); |
| 10884 | bytes.extend_from_slice(&[0; 1]); |
| 10885 | self.return_pointer.serialize_into(bytes); |
| 10886 | self.return_keyboard.serialize_into(bytes); |
| 10887 | } |
| 10888 | } |
| 10889 | #[derive (Clone, Copy)] |
| 10890 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10891 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10892 | pub struct HierarchyChangeDataAttachSlave { |
| 10893 | pub deviceid: DeviceId, |
| 10894 | pub master: DeviceId, |
| 10895 | } |
| 10896 | impl_debug_if_no_extra_traits!(HierarchyChangeDataAttachSlave, "HierarchyChangeDataAttachSlave" ); |
| 10897 | impl TryParse for HierarchyChangeDataAttachSlave { |
| 10898 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 10899 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 10900 | let (master: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 10901 | let result: HierarchyChangeDataAttachSlave = HierarchyChangeDataAttachSlave { deviceid, master }; |
| 10902 | Ok((result, remaining)) |
| 10903 | } |
| 10904 | } |
| 10905 | impl Serialize for HierarchyChangeDataAttachSlave { |
| 10906 | type Bytes = [u8; 4]; |
| 10907 | fn serialize(&self) -> [u8; 4] { |
| 10908 | let deviceid_bytes: [u8; 2] = self.deviceid.serialize(); |
| 10909 | let master_bytes: [u8; 2] = self.master.serialize(); |
| 10910 | [ |
| 10911 | deviceid_bytes[0], |
| 10912 | deviceid_bytes[1], |
| 10913 | master_bytes[0], |
| 10914 | master_bytes[1], |
| 10915 | ] |
| 10916 | } |
| 10917 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 10918 | bytes.reserve(additional:4); |
| 10919 | self.deviceid.serialize_into(bytes); |
| 10920 | self.master.serialize_into(bytes); |
| 10921 | } |
| 10922 | } |
| 10923 | #[derive (Clone, Copy)] |
| 10924 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10925 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10926 | pub struct HierarchyChangeDataDetachSlave { |
| 10927 | pub deviceid: DeviceId, |
| 10928 | } |
| 10929 | impl_debug_if_no_extra_traits!(HierarchyChangeDataDetachSlave, "HierarchyChangeDataDetachSlave" ); |
| 10930 | impl TryParse for HierarchyChangeDataDetachSlave { |
| 10931 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 10932 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 10933 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 10934 | let result: HierarchyChangeDataDetachSlave = HierarchyChangeDataDetachSlave { deviceid }; |
| 10935 | Ok((result, remaining)) |
| 10936 | } |
| 10937 | } |
| 10938 | impl Serialize for HierarchyChangeDataDetachSlave { |
| 10939 | type Bytes = [u8; 4]; |
| 10940 | fn serialize(&self) -> [u8; 4] { |
| 10941 | let deviceid_bytes: [u8; 2] = self.deviceid.serialize(); |
| 10942 | [ |
| 10943 | deviceid_bytes[0], |
| 10944 | deviceid_bytes[1], |
| 10945 | 0, |
| 10946 | 0, |
| 10947 | ] |
| 10948 | } |
| 10949 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 10950 | bytes.reserve(additional:4); |
| 10951 | self.deviceid.serialize_into(bytes); |
| 10952 | bytes.extend_from_slice(&[0; 2]); |
| 10953 | } |
| 10954 | } |
| 10955 | #[derive (Clone)] |
| 10956 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 10957 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 10958 | pub enum HierarchyChangeData { |
| 10959 | AddMaster(HierarchyChangeDataAddMaster), |
| 10960 | RemoveMaster(HierarchyChangeDataRemoveMaster), |
| 10961 | AttachSlave(HierarchyChangeDataAttachSlave), |
| 10962 | DetachSlave(HierarchyChangeDataDetachSlave), |
| 10963 | /// This variant is returned when the server sends a discriminant |
| 10964 | /// value that does not match any of the defined by the protocol. |
| 10965 | /// |
| 10966 | /// Usually, this should be considered a parsing error, but there |
| 10967 | /// are some cases where the server violates the protocol. |
| 10968 | /// |
| 10969 | /// Trying to use `serialize` or `serialize_into` with this variant |
| 10970 | /// will raise a panic. |
| 10971 | InvalidValue(u16), |
| 10972 | } |
| 10973 | impl_debug_if_no_extra_traits!(HierarchyChangeData, "HierarchyChangeData" ); |
| 10974 | impl HierarchyChangeData { |
| 10975 | #[cfg_attr (not(feature = "request-parsing" ), allow(dead_code))] |
| 10976 | fn try_parse(value: &[u8], type_: u16) -> Result<(Self, &[u8]), ParseError> { |
| 10977 | let switch_expr = u16::from(type_); |
| 10978 | let mut outer_remaining = value; |
| 10979 | let mut parse_result = None; |
| 10980 | if switch_expr == u16::from(HierarchyChangeType::ADD_MASTER) { |
| 10981 | let (add_master, new_remaining) = HierarchyChangeDataAddMaster::try_parse(outer_remaining)?; |
| 10982 | outer_remaining = new_remaining; |
| 10983 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 10984 | parse_result = Some(HierarchyChangeData::AddMaster(add_master)); |
| 10985 | } |
| 10986 | if switch_expr == u16::from(HierarchyChangeType::REMOVE_MASTER) { |
| 10987 | let (remove_master, new_remaining) = HierarchyChangeDataRemoveMaster::try_parse(outer_remaining)?; |
| 10988 | outer_remaining = new_remaining; |
| 10989 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 10990 | parse_result = Some(HierarchyChangeData::RemoveMaster(remove_master)); |
| 10991 | } |
| 10992 | if switch_expr == u16::from(HierarchyChangeType::ATTACH_SLAVE) { |
| 10993 | let (attach_slave, new_remaining) = HierarchyChangeDataAttachSlave::try_parse(outer_remaining)?; |
| 10994 | outer_remaining = new_remaining; |
| 10995 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 10996 | parse_result = Some(HierarchyChangeData::AttachSlave(attach_slave)); |
| 10997 | } |
| 10998 | if switch_expr == u16::from(HierarchyChangeType::DETACH_SLAVE) { |
| 10999 | let (detach_slave, new_remaining) = HierarchyChangeDataDetachSlave::try_parse(outer_remaining)?; |
| 11000 | outer_remaining = new_remaining; |
| 11001 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 11002 | parse_result = Some(HierarchyChangeData::DetachSlave(detach_slave)); |
| 11003 | } |
| 11004 | match parse_result { |
| 11005 | None => Ok((HierarchyChangeData::InvalidValue(switch_expr), &[])), |
| 11006 | Some(result) => Ok((result, outer_remaining)), |
| 11007 | } |
| 11008 | } |
| 11009 | } |
| 11010 | impl HierarchyChangeData { |
| 11011 | pub fn as_add_master(&self) -> Option<&HierarchyChangeDataAddMaster> { |
| 11012 | match self { |
| 11013 | HierarchyChangeData::AddMaster(value) => Some(value), |
| 11014 | _ => None, |
| 11015 | } |
| 11016 | } |
| 11017 | pub fn as_remove_master(&self) -> Option<&HierarchyChangeDataRemoveMaster> { |
| 11018 | match self { |
| 11019 | HierarchyChangeData::RemoveMaster(value) => Some(value), |
| 11020 | _ => None, |
| 11021 | } |
| 11022 | } |
| 11023 | pub fn as_attach_slave(&self) -> Option<&HierarchyChangeDataAttachSlave> { |
| 11024 | match self { |
| 11025 | HierarchyChangeData::AttachSlave(value) => Some(value), |
| 11026 | _ => None, |
| 11027 | } |
| 11028 | } |
| 11029 | pub fn as_detach_slave(&self) -> Option<&HierarchyChangeDataDetachSlave> { |
| 11030 | match self { |
| 11031 | HierarchyChangeData::DetachSlave(value) => Some(value), |
| 11032 | _ => None, |
| 11033 | } |
| 11034 | } |
| 11035 | } |
| 11036 | impl HierarchyChangeData { |
| 11037 | #[allow (dead_code)] |
| 11038 | fn serialize(&self, type_: u16) -> Vec<u8> { |
| 11039 | let mut result: Vec = Vec::new(); |
| 11040 | self.serialize_into(&mut result, type_:u16::from(type_)); |
| 11041 | result |
| 11042 | } |
| 11043 | fn serialize_into(&self, bytes: &mut Vec<u8>, type_: u16) { |
| 11044 | assert_eq!(self.switch_expr(), u16::from(type_), "switch `data` has an inconsistent discriminant" ); |
| 11045 | match self { |
| 11046 | HierarchyChangeData::AddMaster(add_master: &HierarchyChangeDataAddMaster) => add_master.serialize_into(bytes), |
| 11047 | HierarchyChangeData::RemoveMaster(remove_master: &HierarchyChangeDataRemoveMaster) => remove_master.serialize_into(bytes), |
| 11048 | HierarchyChangeData::AttachSlave(attach_slave: &HierarchyChangeDataAttachSlave) => attach_slave.serialize_into(bytes), |
| 11049 | HierarchyChangeData::DetachSlave(detach_slave: &HierarchyChangeDataDetachSlave) => detach_slave.serialize_into(bytes), |
| 11050 | HierarchyChangeData::InvalidValue(_) => panic!("attempted to serialize invalid switch case" ), |
| 11051 | } |
| 11052 | } |
| 11053 | } |
| 11054 | impl HierarchyChangeData { |
| 11055 | fn switch_expr(&self) -> u16 { |
| 11056 | match self { |
| 11057 | HierarchyChangeData::AddMaster(_) => u16::from(HierarchyChangeType::ADD_MASTER), |
| 11058 | HierarchyChangeData::RemoveMaster(_) => u16::from(HierarchyChangeType::REMOVE_MASTER), |
| 11059 | HierarchyChangeData::AttachSlave(_) => u16::from(HierarchyChangeType::ATTACH_SLAVE), |
| 11060 | HierarchyChangeData::DetachSlave(_) => u16::from(HierarchyChangeType::DETACH_SLAVE), |
| 11061 | HierarchyChangeData::InvalidValue(switch_expr: &u16) => *switch_expr, |
| 11062 | } |
| 11063 | } |
| 11064 | } |
| 11065 | |
| 11066 | #[derive (Clone)] |
| 11067 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 11068 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11069 | pub struct HierarchyChange { |
| 11070 | pub len: u16, |
| 11071 | pub data: HierarchyChangeData, |
| 11072 | } |
| 11073 | impl_debug_if_no_extra_traits!(HierarchyChange, "HierarchyChange" ); |
| 11074 | impl TryParse for HierarchyChange { |
| 11075 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 11076 | let (type_: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 11077 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 11078 | let (data: HierarchyChangeData, remaining: &[u8]) = HierarchyChangeData::try_parse(value:remaining, type_:u16::from(type_))?; |
| 11079 | let result: HierarchyChange = HierarchyChange { len, data }; |
| 11080 | Ok((result, remaining)) |
| 11081 | } |
| 11082 | } |
| 11083 | impl Serialize for HierarchyChange { |
| 11084 | type Bytes = Vec<u8>; |
| 11085 | fn serialize(&self) -> Vec<u8> { |
| 11086 | let mut result: Vec = Vec::new(); |
| 11087 | self.serialize_into(&mut result); |
| 11088 | result |
| 11089 | } |
| 11090 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 11091 | bytes.reserve(additional:4); |
| 11092 | let type_: u16 = self.data.switch_expr(); |
| 11093 | type_.serialize_into(bytes); |
| 11094 | self.len.serialize_into(bytes); |
| 11095 | self.data.serialize_into(bytes, type_:u16::from(type_)); |
| 11096 | } |
| 11097 | } |
| 11098 | |
| 11099 | /// Opcode for the XIChangeHierarchy request |
| 11100 | pub const XI_CHANGE_HIERARCHY_REQUEST: u8 = 43; |
| 11101 | #[derive (Clone)] |
| 11102 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 11103 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11104 | pub struct XIChangeHierarchyRequest<'input> { |
| 11105 | pub changes: Cow<'input, [HierarchyChange]>, |
| 11106 | } |
| 11107 | impl_debug_if_no_extra_traits!(XIChangeHierarchyRequest<'_>, "XIChangeHierarchyRequest" ); |
| 11108 | impl<'input> XIChangeHierarchyRequest<'input> { |
| 11109 | /// Serialize this request into bytes for the provided connection |
| 11110 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 11111 | let length_so_far = 0; |
| 11112 | let num_changes = u8::try_from(self.changes.len()).expect("`changes` has too many elements" ); |
| 11113 | let num_changes_bytes = num_changes.serialize(); |
| 11114 | let mut request0 = vec![ |
| 11115 | major_opcode, |
| 11116 | XI_CHANGE_HIERARCHY_REQUEST, |
| 11117 | 0, |
| 11118 | 0, |
| 11119 | num_changes_bytes[0], |
| 11120 | 0, |
| 11121 | 0, |
| 11122 | 0, |
| 11123 | ]; |
| 11124 | let length_so_far = length_so_far + request0.len(); |
| 11125 | let changes_bytes = self.changes.serialize(); |
| 11126 | let length_so_far = length_so_far + changes_bytes.len(); |
| 11127 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 11128 | let length_so_far = length_so_far + padding0.len(); |
| 11129 | assert_eq!(length_so_far % 4, 0); |
| 11130 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 11131 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 11132 | ([request0.into(), changes_bytes.into(), padding0.into()], vec![]) |
| 11133 | } |
| 11134 | /// Parse this request given its header, its body, and any fds that go along with it |
| 11135 | #[cfg (feature = "request-parsing" )] |
| 11136 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 11137 | if header.minor_opcode != XI_CHANGE_HIERARCHY_REQUEST { |
| 11138 | return Err(ParseError::InvalidValue); |
| 11139 | } |
| 11140 | let (num_changes, remaining) = u8::try_parse(value)?; |
| 11141 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 11142 | let (changes, remaining) = crate::x11_utils::parse_list::<HierarchyChange>(remaining, num_changes.try_to_usize()?)?; |
| 11143 | let _ = remaining; |
| 11144 | Ok(XIChangeHierarchyRequest { |
| 11145 | changes: Cow::Owned(changes), |
| 11146 | }) |
| 11147 | } |
| 11148 | /// Clone all borrowed data in this XIChangeHierarchyRequest. |
| 11149 | pub fn into_owned(self) -> XIChangeHierarchyRequest<'static> { |
| 11150 | XIChangeHierarchyRequest { |
| 11151 | changes: Cow::Owned(self.changes.into_owned()), |
| 11152 | } |
| 11153 | } |
| 11154 | } |
| 11155 | impl<'input> Request for XIChangeHierarchyRequest<'input> { |
| 11156 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 11157 | |
| 11158 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 11159 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 11160 | // Flatten the buffers into a single vector |
| 11161 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 11162 | (buf, fds) |
| 11163 | } |
| 11164 | } |
| 11165 | impl<'input> crate::x11_utils::VoidRequest for XIChangeHierarchyRequest<'input> { |
| 11166 | } |
| 11167 | |
| 11168 | /// Opcode for the XISetClientPointer request |
| 11169 | pub const XI_SET_CLIENT_POINTER_REQUEST: u8 = 44; |
| 11170 | #[derive (Clone, Copy, Default)] |
| 11171 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 11172 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11173 | pub struct XISetClientPointerRequest { |
| 11174 | pub window: xproto::Window, |
| 11175 | pub deviceid: DeviceId, |
| 11176 | } |
| 11177 | impl_debug_if_no_extra_traits!(XISetClientPointerRequest, "XISetClientPointerRequest" ); |
| 11178 | impl XISetClientPointerRequest { |
| 11179 | /// Serialize this request into bytes for the provided connection |
| 11180 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 11181 | let length_so_far = 0; |
| 11182 | let window_bytes = self.window.serialize(); |
| 11183 | let deviceid_bytes = self.deviceid.serialize(); |
| 11184 | let mut request0 = vec![ |
| 11185 | major_opcode, |
| 11186 | XI_SET_CLIENT_POINTER_REQUEST, |
| 11187 | 0, |
| 11188 | 0, |
| 11189 | window_bytes[0], |
| 11190 | window_bytes[1], |
| 11191 | window_bytes[2], |
| 11192 | window_bytes[3], |
| 11193 | deviceid_bytes[0], |
| 11194 | deviceid_bytes[1], |
| 11195 | 0, |
| 11196 | 0, |
| 11197 | ]; |
| 11198 | let length_so_far = length_so_far + request0.len(); |
| 11199 | assert_eq!(length_so_far % 4, 0); |
| 11200 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 11201 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 11202 | ([request0.into()], vec![]) |
| 11203 | } |
| 11204 | /// Parse this request given its header, its body, and any fds that go along with it |
| 11205 | #[cfg (feature = "request-parsing" )] |
| 11206 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 11207 | if header.minor_opcode != XI_SET_CLIENT_POINTER_REQUEST { |
| 11208 | return Err(ParseError::InvalidValue); |
| 11209 | } |
| 11210 | let (window, remaining) = xproto::Window::try_parse(value)?; |
| 11211 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 11212 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 11213 | let _ = remaining; |
| 11214 | Ok(XISetClientPointerRequest { |
| 11215 | window, |
| 11216 | deviceid, |
| 11217 | }) |
| 11218 | } |
| 11219 | } |
| 11220 | impl Request for XISetClientPointerRequest { |
| 11221 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 11222 | |
| 11223 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 11224 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 11225 | // Flatten the buffers into a single vector |
| 11226 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 11227 | (buf, fds) |
| 11228 | } |
| 11229 | } |
| 11230 | impl crate::x11_utils::VoidRequest for XISetClientPointerRequest { |
| 11231 | } |
| 11232 | |
| 11233 | /// Opcode for the XIGetClientPointer request |
| 11234 | pub const XI_GET_CLIENT_POINTER_REQUEST: u8 = 45; |
| 11235 | #[derive (Clone, Copy, Default)] |
| 11236 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 11237 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11238 | pub struct XIGetClientPointerRequest { |
| 11239 | pub window: xproto::Window, |
| 11240 | } |
| 11241 | impl_debug_if_no_extra_traits!(XIGetClientPointerRequest, "XIGetClientPointerRequest" ); |
| 11242 | impl XIGetClientPointerRequest { |
| 11243 | /// Serialize this request into bytes for the provided connection |
| 11244 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 11245 | let length_so_far = 0; |
| 11246 | let window_bytes = self.window.serialize(); |
| 11247 | let mut request0 = vec![ |
| 11248 | major_opcode, |
| 11249 | XI_GET_CLIENT_POINTER_REQUEST, |
| 11250 | 0, |
| 11251 | 0, |
| 11252 | window_bytes[0], |
| 11253 | window_bytes[1], |
| 11254 | window_bytes[2], |
| 11255 | window_bytes[3], |
| 11256 | ]; |
| 11257 | let length_so_far = length_so_far + request0.len(); |
| 11258 | assert_eq!(length_so_far % 4, 0); |
| 11259 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 11260 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 11261 | ([request0.into()], vec![]) |
| 11262 | } |
| 11263 | /// Parse this request given its header, its body, and any fds that go along with it |
| 11264 | #[cfg (feature = "request-parsing" )] |
| 11265 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 11266 | if header.minor_opcode != XI_GET_CLIENT_POINTER_REQUEST { |
| 11267 | return Err(ParseError::InvalidValue); |
| 11268 | } |
| 11269 | let (window, remaining) = xproto::Window::try_parse(value)?; |
| 11270 | let _ = remaining; |
| 11271 | Ok(XIGetClientPointerRequest { |
| 11272 | window, |
| 11273 | }) |
| 11274 | } |
| 11275 | } |
| 11276 | impl Request for XIGetClientPointerRequest { |
| 11277 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 11278 | |
| 11279 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 11280 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 11281 | // Flatten the buffers into a single vector |
| 11282 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 11283 | (buf, fds) |
| 11284 | } |
| 11285 | } |
| 11286 | impl crate::x11_utils::ReplyRequest for XIGetClientPointerRequest { |
| 11287 | type Reply = XIGetClientPointerReply; |
| 11288 | } |
| 11289 | |
| 11290 | #[derive (Clone, Copy, Default)] |
| 11291 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 11292 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11293 | pub struct XIGetClientPointerReply { |
| 11294 | pub sequence: u16, |
| 11295 | pub length: u32, |
| 11296 | pub set: bool, |
| 11297 | pub deviceid: DeviceId, |
| 11298 | } |
| 11299 | impl_debug_if_no_extra_traits!(XIGetClientPointerReply, "XIGetClientPointerReply" ); |
| 11300 | impl TryParse for XIGetClientPointerReply { |
| 11301 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 11302 | let remaining: &[u8] = initial_value; |
| 11303 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 11304 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 11305 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 11306 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 11307 | let (set: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
| 11308 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 11309 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 11310 | let remaining: &[u8] = remaining.get(20..).ok_or(err:ParseError::InsufficientData)?; |
| 11311 | if response_type != 1 { |
| 11312 | return Err(ParseError::InvalidValue); |
| 11313 | } |
| 11314 | let result: XIGetClientPointerReply = XIGetClientPointerReply { sequence, length, set, deviceid }; |
| 11315 | let _ = remaining; |
| 11316 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 11317 | .ok_or(err:ParseError::InsufficientData)?; |
| 11318 | Ok((result, remaining)) |
| 11319 | } |
| 11320 | } |
| 11321 | impl Serialize for XIGetClientPointerReply { |
| 11322 | type Bytes = [u8; 32]; |
| 11323 | fn serialize(&self) -> [u8; 32] { |
| 11324 | let response_type_bytes = &[1]; |
| 11325 | let sequence_bytes = self.sequence.serialize(); |
| 11326 | let length_bytes = self.length.serialize(); |
| 11327 | let set_bytes = self.set.serialize(); |
| 11328 | let deviceid_bytes = self.deviceid.serialize(); |
| 11329 | [ |
| 11330 | response_type_bytes[0], |
| 11331 | 0, |
| 11332 | sequence_bytes[0], |
| 11333 | sequence_bytes[1], |
| 11334 | length_bytes[0], |
| 11335 | length_bytes[1], |
| 11336 | length_bytes[2], |
| 11337 | length_bytes[3], |
| 11338 | set_bytes[0], |
| 11339 | 0, |
| 11340 | deviceid_bytes[0], |
| 11341 | deviceid_bytes[1], |
| 11342 | 0, |
| 11343 | 0, |
| 11344 | 0, |
| 11345 | 0, |
| 11346 | 0, |
| 11347 | 0, |
| 11348 | 0, |
| 11349 | 0, |
| 11350 | 0, |
| 11351 | 0, |
| 11352 | 0, |
| 11353 | 0, |
| 11354 | 0, |
| 11355 | 0, |
| 11356 | 0, |
| 11357 | 0, |
| 11358 | 0, |
| 11359 | 0, |
| 11360 | 0, |
| 11361 | 0, |
| 11362 | ] |
| 11363 | } |
| 11364 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 11365 | bytes.reserve(32); |
| 11366 | let response_type_bytes = &[1]; |
| 11367 | bytes.push(response_type_bytes[0]); |
| 11368 | bytes.extend_from_slice(&[0; 1]); |
| 11369 | self.sequence.serialize_into(bytes); |
| 11370 | self.length.serialize_into(bytes); |
| 11371 | self.set.serialize_into(bytes); |
| 11372 | bytes.extend_from_slice(&[0; 1]); |
| 11373 | self.deviceid.serialize_into(bytes); |
| 11374 | bytes.extend_from_slice(&[0; 20]); |
| 11375 | } |
| 11376 | } |
| 11377 | |
| 11378 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 11379 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11380 | pub struct XIEventMask(u32); |
| 11381 | impl XIEventMask { |
| 11382 | pub const DEVICE_CHANGED: Self = Self(1 << 1); |
| 11383 | pub const KEY_PRESS: Self = Self(1 << 2); |
| 11384 | pub const KEY_RELEASE: Self = Self(1 << 3); |
| 11385 | pub const BUTTON_PRESS: Self = Self(1 << 4); |
| 11386 | pub const BUTTON_RELEASE: Self = Self(1 << 5); |
| 11387 | pub const MOTION: Self = Self(1 << 6); |
| 11388 | pub const ENTER: Self = Self(1 << 7); |
| 11389 | pub const LEAVE: Self = Self(1 << 8); |
| 11390 | pub const FOCUS_IN: Self = Self(1 << 9); |
| 11391 | pub const FOCUS_OUT: Self = Self(1 << 10); |
| 11392 | pub const HIERARCHY: Self = Self(1 << 11); |
| 11393 | pub const PROPERTY: Self = Self(1 << 12); |
| 11394 | pub const RAW_KEY_PRESS: Self = Self(1 << 13); |
| 11395 | pub const RAW_KEY_RELEASE: Self = Self(1 << 14); |
| 11396 | pub const RAW_BUTTON_PRESS: Self = Self(1 << 15); |
| 11397 | pub const RAW_BUTTON_RELEASE: Self = Self(1 << 16); |
| 11398 | pub const RAW_MOTION: Self = Self(1 << 17); |
| 11399 | pub const TOUCH_BEGIN: Self = Self(1 << 18); |
| 11400 | pub const TOUCH_UPDATE: Self = Self(1 << 19); |
| 11401 | pub const TOUCH_END: Self = Self(1 << 20); |
| 11402 | pub const TOUCH_OWNERSHIP: Self = Self(1 << 21); |
| 11403 | pub const RAW_TOUCH_BEGIN: Self = Self(1 << 22); |
| 11404 | pub const RAW_TOUCH_UPDATE: Self = Self(1 << 23); |
| 11405 | pub const RAW_TOUCH_END: Self = Self(1 << 24); |
| 11406 | pub const BARRIER_HIT: Self = Self(1 << 25); |
| 11407 | pub const BARRIER_LEAVE: Self = Self(1 << 26); |
| 11408 | } |
| 11409 | impl From<XIEventMask> for u32 { |
| 11410 | #[inline ] |
| 11411 | fn from(input: XIEventMask) -> Self { |
| 11412 | input.0 |
| 11413 | } |
| 11414 | } |
| 11415 | impl From<XIEventMask> for Option<u32> { |
| 11416 | #[inline ] |
| 11417 | fn from(input: XIEventMask) -> Self { |
| 11418 | Some(input.0) |
| 11419 | } |
| 11420 | } |
| 11421 | impl From<u8> for XIEventMask { |
| 11422 | #[inline ] |
| 11423 | fn from(value: u8) -> Self { |
| 11424 | Self(value.into()) |
| 11425 | } |
| 11426 | } |
| 11427 | impl From<u16> for XIEventMask { |
| 11428 | #[inline ] |
| 11429 | fn from(value: u16) -> Self { |
| 11430 | Self(value.into()) |
| 11431 | } |
| 11432 | } |
| 11433 | impl From<u32> for XIEventMask { |
| 11434 | #[inline ] |
| 11435 | fn from(value: u32) -> Self { |
| 11436 | Self(value) |
| 11437 | } |
| 11438 | } |
| 11439 | impl core::fmt::Debug for XIEventMask { |
| 11440 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 11441 | let variants = [ |
| 11442 | (Self::DEVICE_CHANGED.0, "DEVICE_CHANGED" , "DeviceChanged" ), |
| 11443 | (Self::KEY_PRESS.0, "KEY_PRESS" , "KeyPress" ), |
| 11444 | (Self::KEY_RELEASE.0, "KEY_RELEASE" , "KeyRelease" ), |
| 11445 | (Self::BUTTON_PRESS.0, "BUTTON_PRESS" , "ButtonPress" ), |
| 11446 | (Self::BUTTON_RELEASE.0, "BUTTON_RELEASE" , "ButtonRelease" ), |
| 11447 | (Self::MOTION.0, "MOTION" , "Motion" ), |
| 11448 | (Self::ENTER.0, "ENTER" , "Enter" ), |
| 11449 | (Self::LEAVE.0, "LEAVE" , "Leave" ), |
| 11450 | (Self::FOCUS_IN.0, "FOCUS_IN" , "FocusIn" ), |
| 11451 | (Self::FOCUS_OUT.0, "FOCUS_OUT" , "FocusOut" ), |
| 11452 | (Self::HIERARCHY.0, "HIERARCHY" , "Hierarchy" ), |
| 11453 | (Self::PROPERTY.0, "PROPERTY" , "Property" ), |
| 11454 | (Self::RAW_KEY_PRESS.0, "RAW_KEY_PRESS" , "RawKeyPress" ), |
| 11455 | (Self::RAW_KEY_RELEASE.0, "RAW_KEY_RELEASE" , "RawKeyRelease" ), |
| 11456 | (Self::RAW_BUTTON_PRESS.0, "RAW_BUTTON_PRESS" , "RawButtonPress" ), |
| 11457 | (Self::RAW_BUTTON_RELEASE.0, "RAW_BUTTON_RELEASE" , "RawButtonRelease" ), |
| 11458 | (Self::RAW_MOTION.0, "RAW_MOTION" , "RawMotion" ), |
| 11459 | (Self::TOUCH_BEGIN.0, "TOUCH_BEGIN" , "TouchBegin" ), |
| 11460 | (Self::TOUCH_UPDATE.0, "TOUCH_UPDATE" , "TouchUpdate" ), |
| 11461 | (Self::TOUCH_END.0, "TOUCH_END" , "TouchEnd" ), |
| 11462 | (Self::TOUCH_OWNERSHIP.0, "TOUCH_OWNERSHIP" , "TouchOwnership" ), |
| 11463 | (Self::RAW_TOUCH_BEGIN.0, "RAW_TOUCH_BEGIN" , "RawTouchBegin" ), |
| 11464 | (Self::RAW_TOUCH_UPDATE.0, "RAW_TOUCH_UPDATE" , "RawTouchUpdate" ), |
| 11465 | (Self::RAW_TOUCH_END.0, "RAW_TOUCH_END" , "RawTouchEnd" ), |
| 11466 | (Self::BARRIER_HIT.0, "BARRIER_HIT" , "BarrierHit" ), |
| 11467 | (Self::BARRIER_LEAVE.0, "BARRIER_LEAVE" , "BarrierLeave" ), |
| 11468 | ]; |
| 11469 | pretty_print_bitmask(fmt, self.0, &variants) |
| 11470 | } |
| 11471 | } |
| 11472 | bitmask_binop!(XIEventMask, u32); |
| 11473 | |
| 11474 | #[derive (Clone, Default)] |
| 11475 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 11476 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11477 | pub struct EventMask { |
| 11478 | pub deviceid: DeviceId, |
| 11479 | pub mask: Vec<XIEventMask>, |
| 11480 | } |
| 11481 | impl_debug_if_no_extra_traits!(EventMask, "EventMask" ); |
| 11482 | impl TryParse for EventMask { |
| 11483 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 11484 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 11485 | let (mask_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 11486 | let mut remaining: &[u8] = remaining; |
| 11487 | let list_length: usize = mask_len.try_to_usize()?; |
| 11488 | let mut mask: Vec = Vec::with_capacity(list_length); |
| 11489 | for _ in 0..list_length { |
| 11490 | let (v: u32, new_remaining: &[u8]) = u32::try_parse(remaining)?; |
| 11491 | let v: XIEventMask = v.into(); |
| 11492 | remaining = new_remaining; |
| 11493 | mask.push(v); |
| 11494 | } |
| 11495 | let result: EventMask = EventMask { deviceid, mask }; |
| 11496 | Ok((result, remaining)) |
| 11497 | } |
| 11498 | } |
| 11499 | impl Serialize for EventMask { |
| 11500 | type Bytes = Vec<u8>; |
| 11501 | fn serialize(&self) -> Vec<u8> { |
| 11502 | let mut result: Vec = Vec::new(); |
| 11503 | self.serialize_into(&mut result); |
| 11504 | result |
| 11505 | } |
| 11506 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 11507 | bytes.reserve(additional:4); |
| 11508 | self.deviceid.serialize_into(bytes); |
| 11509 | let mask_len: u16 = u16::try_from(self.mask.len()).expect(msg:"`mask` has too many elements" ); |
| 11510 | mask_len.serialize_into(bytes); |
| 11511 | for element: XIEventMask in self.mask.iter().copied() { |
| 11512 | u32::from(element).serialize_into(bytes); |
| 11513 | } |
| 11514 | } |
| 11515 | } |
| 11516 | impl EventMask { |
| 11517 | /// Get the value of the `mask_len` field. |
| 11518 | /// |
| 11519 | /// The `mask_len` field is used as the length field of the `mask` field. |
| 11520 | /// This function computes the field's value again based on the length of the list. |
| 11521 | /// |
| 11522 | /// # Panics |
| 11523 | /// |
| 11524 | /// Panics if the value cannot be represented in the target type. This |
| 11525 | /// cannot happen with values of the struct received from the X11 server. |
| 11526 | pub fn mask_len(&self) -> u16 { |
| 11527 | self.mask.len() |
| 11528 | .try_into().unwrap() |
| 11529 | } |
| 11530 | } |
| 11531 | |
| 11532 | /// Opcode for the XISelectEvents request |
| 11533 | pub const XI_SELECT_EVENTS_REQUEST: u8 = 46; |
| 11534 | #[derive (Clone, Default)] |
| 11535 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 11536 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11537 | pub struct XISelectEventsRequest<'input> { |
| 11538 | pub window: xproto::Window, |
| 11539 | pub masks: Cow<'input, [EventMask]>, |
| 11540 | } |
| 11541 | impl_debug_if_no_extra_traits!(XISelectEventsRequest<'_>, "XISelectEventsRequest" ); |
| 11542 | impl<'input> XISelectEventsRequest<'input> { |
| 11543 | /// Serialize this request into bytes for the provided connection |
| 11544 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 11545 | let length_so_far = 0; |
| 11546 | let window_bytes = self.window.serialize(); |
| 11547 | let num_mask = u16::try_from(self.masks.len()).expect("`masks` has too many elements" ); |
| 11548 | let num_mask_bytes = num_mask.serialize(); |
| 11549 | let mut request0 = vec![ |
| 11550 | major_opcode, |
| 11551 | XI_SELECT_EVENTS_REQUEST, |
| 11552 | 0, |
| 11553 | 0, |
| 11554 | window_bytes[0], |
| 11555 | window_bytes[1], |
| 11556 | window_bytes[2], |
| 11557 | window_bytes[3], |
| 11558 | num_mask_bytes[0], |
| 11559 | num_mask_bytes[1], |
| 11560 | 0, |
| 11561 | 0, |
| 11562 | ]; |
| 11563 | let length_so_far = length_so_far + request0.len(); |
| 11564 | let masks_bytes = self.masks.serialize(); |
| 11565 | let length_so_far = length_so_far + masks_bytes.len(); |
| 11566 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 11567 | let length_so_far = length_so_far + padding0.len(); |
| 11568 | assert_eq!(length_so_far % 4, 0); |
| 11569 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 11570 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 11571 | ([request0.into(), masks_bytes.into(), padding0.into()], vec![]) |
| 11572 | } |
| 11573 | /// Parse this request given its header, its body, and any fds that go along with it |
| 11574 | #[cfg (feature = "request-parsing" )] |
| 11575 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 11576 | if header.minor_opcode != XI_SELECT_EVENTS_REQUEST { |
| 11577 | return Err(ParseError::InvalidValue); |
| 11578 | } |
| 11579 | let (window, remaining) = xproto::Window::try_parse(value)?; |
| 11580 | let (num_mask, remaining) = u16::try_parse(remaining)?; |
| 11581 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 11582 | let (masks, remaining) = crate::x11_utils::parse_list::<EventMask>(remaining, num_mask.try_to_usize()?)?; |
| 11583 | let _ = remaining; |
| 11584 | Ok(XISelectEventsRequest { |
| 11585 | window, |
| 11586 | masks: Cow::Owned(masks), |
| 11587 | }) |
| 11588 | } |
| 11589 | /// Clone all borrowed data in this XISelectEventsRequest. |
| 11590 | pub fn into_owned(self) -> XISelectEventsRequest<'static> { |
| 11591 | XISelectEventsRequest { |
| 11592 | window: self.window, |
| 11593 | masks: Cow::Owned(self.masks.into_owned()), |
| 11594 | } |
| 11595 | } |
| 11596 | } |
| 11597 | impl<'input> Request for XISelectEventsRequest<'input> { |
| 11598 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 11599 | |
| 11600 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 11601 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 11602 | // Flatten the buffers into a single vector |
| 11603 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 11604 | (buf, fds) |
| 11605 | } |
| 11606 | } |
| 11607 | impl<'input> crate::x11_utils::VoidRequest for XISelectEventsRequest<'input> { |
| 11608 | } |
| 11609 | |
| 11610 | /// Opcode for the XIQueryVersion request |
| 11611 | pub const XI_QUERY_VERSION_REQUEST: u8 = 47; |
| 11612 | #[derive (Clone, Copy, Default)] |
| 11613 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 11614 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11615 | pub struct XIQueryVersionRequest { |
| 11616 | pub major_version: u16, |
| 11617 | pub minor_version: u16, |
| 11618 | } |
| 11619 | impl_debug_if_no_extra_traits!(XIQueryVersionRequest, "XIQueryVersionRequest" ); |
| 11620 | impl XIQueryVersionRequest { |
| 11621 | /// Serialize this request into bytes for the provided connection |
| 11622 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 11623 | let length_so_far = 0; |
| 11624 | let major_version_bytes = self.major_version.serialize(); |
| 11625 | let minor_version_bytes = self.minor_version.serialize(); |
| 11626 | let mut request0 = vec![ |
| 11627 | major_opcode, |
| 11628 | XI_QUERY_VERSION_REQUEST, |
| 11629 | 0, |
| 11630 | 0, |
| 11631 | major_version_bytes[0], |
| 11632 | major_version_bytes[1], |
| 11633 | minor_version_bytes[0], |
| 11634 | minor_version_bytes[1], |
| 11635 | ]; |
| 11636 | let length_so_far = length_so_far + request0.len(); |
| 11637 | assert_eq!(length_so_far % 4, 0); |
| 11638 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 11639 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 11640 | ([request0.into()], vec![]) |
| 11641 | } |
| 11642 | /// Parse this request given its header, its body, and any fds that go along with it |
| 11643 | #[cfg (feature = "request-parsing" )] |
| 11644 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 11645 | if header.minor_opcode != XI_QUERY_VERSION_REQUEST { |
| 11646 | return Err(ParseError::InvalidValue); |
| 11647 | } |
| 11648 | let (major_version, remaining) = u16::try_parse(value)?; |
| 11649 | let (minor_version, remaining) = u16::try_parse(remaining)?; |
| 11650 | let _ = remaining; |
| 11651 | Ok(XIQueryVersionRequest { |
| 11652 | major_version, |
| 11653 | minor_version, |
| 11654 | }) |
| 11655 | } |
| 11656 | } |
| 11657 | impl Request for XIQueryVersionRequest { |
| 11658 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 11659 | |
| 11660 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 11661 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 11662 | // Flatten the buffers into a single vector |
| 11663 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 11664 | (buf, fds) |
| 11665 | } |
| 11666 | } |
| 11667 | impl crate::x11_utils::ReplyRequest for XIQueryVersionRequest { |
| 11668 | type Reply = XIQueryVersionReply; |
| 11669 | } |
| 11670 | |
| 11671 | #[derive (Clone, Copy, Default)] |
| 11672 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 11673 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11674 | pub struct XIQueryVersionReply { |
| 11675 | pub sequence: u16, |
| 11676 | pub length: u32, |
| 11677 | pub major_version: u16, |
| 11678 | pub minor_version: u16, |
| 11679 | } |
| 11680 | impl_debug_if_no_extra_traits!(XIQueryVersionReply, "XIQueryVersionReply" ); |
| 11681 | impl TryParse for XIQueryVersionReply { |
| 11682 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 11683 | let remaining: &[u8] = initial_value; |
| 11684 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 11685 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 11686 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 11687 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 11688 | let (major_version: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 11689 | let (minor_version: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 11690 | let remaining: &[u8] = remaining.get(20..).ok_or(err:ParseError::InsufficientData)?; |
| 11691 | if response_type != 1 { |
| 11692 | return Err(ParseError::InvalidValue); |
| 11693 | } |
| 11694 | let result: XIQueryVersionReply = XIQueryVersionReply { sequence, length, major_version, minor_version }; |
| 11695 | let _ = remaining; |
| 11696 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 11697 | .ok_or(err:ParseError::InsufficientData)?; |
| 11698 | Ok((result, remaining)) |
| 11699 | } |
| 11700 | } |
| 11701 | impl Serialize for XIQueryVersionReply { |
| 11702 | type Bytes = [u8; 32]; |
| 11703 | fn serialize(&self) -> [u8; 32] { |
| 11704 | let response_type_bytes = &[1]; |
| 11705 | let sequence_bytes = self.sequence.serialize(); |
| 11706 | let length_bytes = self.length.serialize(); |
| 11707 | let major_version_bytes = self.major_version.serialize(); |
| 11708 | let minor_version_bytes = self.minor_version.serialize(); |
| 11709 | [ |
| 11710 | response_type_bytes[0], |
| 11711 | 0, |
| 11712 | sequence_bytes[0], |
| 11713 | sequence_bytes[1], |
| 11714 | length_bytes[0], |
| 11715 | length_bytes[1], |
| 11716 | length_bytes[2], |
| 11717 | length_bytes[3], |
| 11718 | major_version_bytes[0], |
| 11719 | major_version_bytes[1], |
| 11720 | minor_version_bytes[0], |
| 11721 | minor_version_bytes[1], |
| 11722 | 0, |
| 11723 | 0, |
| 11724 | 0, |
| 11725 | 0, |
| 11726 | 0, |
| 11727 | 0, |
| 11728 | 0, |
| 11729 | 0, |
| 11730 | 0, |
| 11731 | 0, |
| 11732 | 0, |
| 11733 | 0, |
| 11734 | 0, |
| 11735 | 0, |
| 11736 | 0, |
| 11737 | 0, |
| 11738 | 0, |
| 11739 | 0, |
| 11740 | 0, |
| 11741 | 0, |
| 11742 | ] |
| 11743 | } |
| 11744 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 11745 | bytes.reserve(32); |
| 11746 | let response_type_bytes = &[1]; |
| 11747 | bytes.push(response_type_bytes[0]); |
| 11748 | bytes.extend_from_slice(&[0; 1]); |
| 11749 | self.sequence.serialize_into(bytes); |
| 11750 | self.length.serialize_into(bytes); |
| 11751 | self.major_version.serialize_into(bytes); |
| 11752 | self.minor_version.serialize_into(bytes); |
| 11753 | bytes.extend_from_slice(&[0; 20]); |
| 11754 | } |
| 11755 | } |
| 11756 | |
| 11757 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 11758 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11759 | pub struct DeviceClassType(u16); |
| 11760 | impl DeviceClassType { |
| 11761 | pub const KEY: Self = Self(0); |
| 11762 | pub const BUTTON: Self = Self(1); |
| 11763 | pub const VALUATOR: Self = Self(2); |
| 11764 | pub const SCROLL: Self = Self(3); |
| 11765 | pub const TOUCH: Self = Self(8); |
| 11766 | pub const GESTURE: Self = Self(9); |
| 11767 | } |
| 11768 | impl From<DeviceClassType> for u16 { |
| 11769 | #[inline ] |
| 11770 | fn from(input: DeviceClassType) -> Self { |
| 11771 | input.0 |
| 11772 | } |
| 11773 | } |
| 11774 | impl From<DeviceClassType> for Option<u16> { |
| 11775 | #[inline ] |
| 11776 | fn from(input: DeviceClassType) -> Self { |
| 11777 | Some(input.0) |
| 11778 | } |
| 11779 | } |
| 11780 | impl From<DeviceClassType> for u32 { |
| 11781 | #[inline ] |
| 11782 | fn from(input: DeviceClassType) -> Self { |
| 11783 | u32::from(input.0) |
| 11784 | } |
| 11785 | } |
| 11786 | impl From<DeviceClassType> for Option<u32> { |
| 11787 | #[inline ] |
| 11788 | fn from(input: DeviceClassType) -> Self { |
| 11789 | Some(u32::from(input.0)) |
| 11790 | } |
| 11791 | } |
| 11792 | impl From<u8> for DeviceClassType { |
| 11793 | #[inline ] |
| 11794 | fn from(value: u8) -> Self { |
| 11795 | Self(value.into()) |
| 11796 | } |
| 11797 | } |
| 11798 | impl From<u16> for DeviceClassType { |
| 11799 | #[inline ] |
| 11800 | fn from(value: u16) -> Self { |
| 11801 | Self(value) |
| 11802 | } |
| 11803 | } |
| 11804 | impl core::fmt::Debug for DeviceClassType { |
| 11805 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 11806 | let variants: [(u32, &'static str, &'static …); 6] = [ |
| 11807 | (Self::KEY.0.into(), "KEY" , "Key" ), |
| 11808 | (Self::BUTTON.0.into(), "BUTTON" , "Button" ), |
| 11809 | (Self::VALUATOR.0.into(), "VALUATOR" , "Valuator" ), |
| 11810 | (Self::SCROLL.0.into(), "SCROLL" , "Scroll" ), |
| 11811 | (Self::TOUCH.0.into(), "TOUCH" , "Touch" ), |
| 11812 | (Self::GESTURE.0.into(), "GESTURE" , "Gesture" ), |
| 11813 | ]; |
| 11814 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 11815 | } |
| 11816 | } |
| 11817 | |
| 11818 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 11819 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11820 | pub struct DeviceType(u16); |
| 11821 | impl DeviceType { |
| 11822 | pub const MASTER_POINTER: Self = Self(1); |
| 11823 | pub const MASTER_KEYBOARD: Self = Self(2); |
| 11824 | pub const SLAVE_POINTER: Self = Self(3); |
| 11825 | pub const SLAVE_KEYBOARD: Self = Self(4); |
| 11826 | pub const FLOATING_SLAVE: Self = Self(5); |
| 11827 | } |
| 11828 | impl From<DeviceType> for u16 { |
| 11829 | #[inline ] |
| 11830 | fn from(input: DeviceType) -> Self { |
| 11831 | input.0 |
| 11832 | } |
| 11833 | } |
| 11834 | impl From<DeviceType> for Option<u16> { |
| 11835 | #[inline ] |
| 11836 | fn from(input: DeviceType) -> Self { |
| 11837 | Some(input.0) |
| 11838 | } |
| 11839 | } |
| 11840 | impl From<DeviceType> for u32 { |
| 11841 | #[inline ] |
| 11842 | fn from(input: DeviceType) -> Self { |
| 11843 | u32::from(input.0) |
| 11844 | } |
| 11845 | } |
| 11846 | impl From<DeviceType> for Option<u32> { |
| 11847 | #[inline ] |
| 11848 | fn from(input: DeviceType) -> Self { |
| 11849 | Some(u32::from(input.0)) |
| 11850 | } |
| 11851 | } |
| 11852 | impl From<u8> for DeviceType { |
| 11853 | #[inline ] |
| 11854 | fn from(value: u8) -> Self { |
| 11855 | Self(value.into()) |
| 11856 | } |
| 11857 | } |
| 11858 | impl From<u16> for DeviceType { |
| 11859 | #[inline ] |
| 11860 | fn from(value: u16) -> Self { |
| 11861 | Self(value) |
| 11862 | } |
| 11863 | } |
| 11864 | impl core::fmt::Debug for DeviceType { |
| 11865 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 11866 | let variants: [(u32, &'static str, &'static …); 5] = [ |
| 11867 | (Self::MASTER_POINTER.0.into(), "MASTER_POINTER" , "MasterPointer" ), |
| 11868 | (Self::MASTER_KEYBOARD.0.into(), "MASTER_KEYBOARD" , "MasterKeyboard" ), |
| 11869 | (Self::SLAVE_POINTER.0.into(), "SLAVE_POINTER" , "SlavePointer" ), |
| 11870 | (Self::SLAVE_KEYBOARD.0.into(), "SLAVE_KEYBOARD" , "SlaveKeyboard" ), |
| 11871 | (Self::FLOATING_SLAVE.0.into(), "FLOATING_SLAVE" , "FloatingSlave" ), |
| 11872 | ]; |
| 11873 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 11874 | } |
| 11875 | } |
| 11876 | |
| 11877 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 11878 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11879 | pub struct ScrollFlags(u32); |
| 11880 | impl ScrollFlags { |
| 11881 | pub const NO_EMULATION: Self = Self(1 << 0); |
| 11882 | pub const PREFERRED: Self = Self(1 << 1); |
| 11883 | } |
| 11884 | impl From<ScrollFlags> for u32 { |
| 11885 | #[inline ] |
| 11886 | fn from(input: ScrollFlags) -> Self { |
| 11887 | input.0 |
| 11888 | } |
| 11889 | } |
| 11890 | impl From<ScrollFlags> for Option<u32> { |
| 11891 | #[inline ] |
| 11892 | fn from(input: ScrollFlags) -> Self { |
| 11893 | Some(input.0) |
| 11894 | } |
| 11895 | } |
| 11896 | impl From<u8> for ScrollFlags { |
| 11897 | #[inline ] |
| 11898 | fn from(value: u8) -> Self { |
| 11899 | Self(value.into()) |
| 11900 | } |
| 11901 | } |
| 11902 | impl From<u16> for ScrollFlags { |
| 11903 | #[inline ] |
| 11904 | fn from(value: u16) -> Self { |
| 11905 | Self(value.into()) |
| 11906 | } |
| 11907 | } |
| 11908 | impl From<u32> for ScrollFlags { |
| 11909 | #[inline ] |
| 11910 | fn from(value: u32) -> Self { |
| 11911 | Self(value) |
| 11912 | } |
| 11913 | } |
| 11914 | impl core::fmt::Debug for ScrollFlags { |
| 11915 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 11916 | let variants: [(u32, &'static str, &'static …); 2] = [ |
| 11917 | (Self::NO_EMULATION.0, "NO_EMULATION" , "NoEmulation" ), |
| 11918 | (Self::PREFERRED.0, "PREFERRED" , "Preferred" ), |
| 11919 | ]; |
| 11920 | pretty_print_bitmask(fmt, self.0, &variants) |
| 11921 | } |
| 11922 | } |
| 11923 | bitmask_binop!(ScrollFlags, u32); |
| 11924 | |
| 11925 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 11926 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11927 | pub struct ScrollType(u16); |
| 11928 | impl ScrollType { |
| 11929 | pub const VERTICAL: Self = Self(1); |
| 11930 | pub const HORIZONTAL: Self = Self(2); |
| 11931 | } |
| 11932 | impl From<ScrollType> for u16 { |
| 11933 | #[inline ] |
| 11934 | fn from(input: ScrollType) -> Self { |
| 11935 | input.0 |
| 11936 | } |
| 11937 | } |
| 11938 | impl From<ScrollType> for Option<u16> { |
| 11939 | #[inline ] |
| 11940 | fn from(input: ScrollType) -> Self { |
| 11941 | Some(input.0) |
| 11942 | } |
| 11943 | } |
| 11944 | impl From<ScrollType> for u32 { |
| 11945 | #[inline ] |
| 11946 | fn from(input: ScrollType) -> Self { |
| 11947 | u32::from(input.0) |
| 11948 | } |
| 11949 | } |
| 11950 | impl From<ScrollType> for Option<u32> { |
| 11951 | #[inline ] |
| 11952 | fn from(input: ScrollType) -> Self { |
| 11953 | Some(u32::from(input.0)) |
| 11954 | } |
| 11955 | } |
| 11956 | impl From<u8> for ScrollType { |
| 11957 | #[inline ] |
| 11958 | fn from(value: u8) -> Self { |
| 11959 | Self(value.into()) |
| 11960 | } |
| 11961 | } |
| 11962 | impl From<u16> for ScrollType { |
| 11963 | #[inline ] |
| 11964 | fn from(value: u16) -> Self { |
| 11965 | Self(value) |
| 11966 | } |
| 11967 | } |
| 11968 | impl core::fmt::Debug for ScrollType { |
| 11969 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 11970 | let variants: [(u32, &'static str, &'static …); 2] = [ |
| 11971 | (Self::VERTICAL.0.into(), "VERTICAL" , "Vertical" ), |
| 11972 | (Self::HORIZONTAL.0.into(), "HORIZONTAL" , "Horizontal" ), |
| 11973 | ]; |
| 11974 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 11975 | } |
| 11976 | } |
| 11977 | |
| 11978 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 11979 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 11980 | pub struct TouchMode(u8); |
| 11981 | impl TouchMode { |
| 11982 | pub const DIRECT: Self = Self(1); |
| 11983 | pub const DEPENDENT: Self = Self(2); |
| 11984 | } |
| 11985 | impl From<TouchMode> for u8 { |
| 11986 | #[inline ] |
| 11987 | fn from(input: TouchMode) -> Self { |
| 11988 | input.0 |
| 11989 | } |
| 11990 | } |
| 11991 | impl From<TouchMode> for Option<u8> { |
| 11992 | #[inline ] |
| 11993 | fn from(input: TouchMode) -> Self { |
| 11994 | Some(input.0) |
| 11995 | } |
| 11996 | } |
| 11997 | impl From<TouchMode> for u16 { |
| 11998 | #[inline ] |
| 11999 | fn from(input: TouchMode) -> Self { |
| 12000 | u16::from(input.0) |
| 12001 | } |
| 12002 | } |
| 12003 | impl From<TouchMode> for Option<u16> { |
| 12004 | #[inline ] |
| 12005 | fn from(input: TouchMode) -> Self { |
| 12006 | Some(u16::from(input.0)) |
| 12007 | } |
| 12008 | } |
| 12009 | impl From<TouchMode> for u32 { |
| 12010 | #[inline ] |
| 12011 | fn from(input: TouchMode) -> Self { |
| 12012 | u32::from(input.0) |
| 12013 | } |
| 12014 | } |
| 12015 | impl From<TouchMode> for Option<u32> { |
| 12016 | #[inline ] |
| 12017 | fn from(input: TouchMode) -> Self { |
| 12018 | Some(u32::from(input.0)) |
| 12019 | } |
| 12020 | } |
| 12021 | impl From<u8> for TouchMode { |
| 12022 | #[inline ] |
| 12023 | fn from(value: u8) -> Self { |
| 12024 | Self(value) |
| 12025 | } |
| 12026 | } |
| 12027 | impl core::fmt::Debug for TouchMode { |
| 12028 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 12029 | let variants: [(u32, &'static str, &'static …); 2] = [ |
| 12030 | (Self::DIRECT.0.into(), "DIRECT" , "Direct" ), |
| 12031 | (Self::DEPENDENT.0.into(), "DEPENDENT" , "Dependent" ), |
| 12032 | ]; |
| 12033 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 12034 | } |
| 12035 | } |
| 12036 | |
| 12037 | #[derive (Clone, Default)] |
| 12038 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12039 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12040 | pub struct ButtonClass { |
| 12041 | pub type_: DeviceClassType, |
| 12042 | pub len: u16, |
| 12043 | pub sourceid: DeviceId, |
| 12044 | pub state: Vec<u32>, |
| 12045 | pub labels: Vec<xproto::Atom>, |
| 12046 | } |
| 12047 | impl_debug_if_no_extra_traits!(ButtonClass, "ButtonClass" ); |
| 12048 | impl TryParse for ButtonClass { |
| 12049 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12050 | let (type_: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12051 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12052 | let (sourceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 12053 | let (num_buttons: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12054 | let (state: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:u32::from(num_buttons).checked_add(31u32).ok_or(ParseError::InvalidExpression)?.checked_div(32u32).ok_or(err:ParseError::InvalidExpression)?.try_to_usize()?)?; |
| 12055 | let (labels: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<xproto::Atom>(data:remaining, list_length:num_buttons.try_to_usize()?)?; |
| 12056 | let type_: DeviceClassType = type_.into(); |
| 12057 | let result: ButtonClass = ButtonClass { type_, len, sourceid, state, labels }; |
| 12058 | Ok((result, remaining)) |
| 12059 | } |
| 12060 | } |
| 12061 | impl Serialize for ButtonClass { |
| 12062 | type Bytes = Vec<u8>; |
| 12063 | fn serialize(&self) -> Vec<u8> { |
| 12064 | let mut result: Vec = Vec::new(); |
| 12065 | self.serialize_into(&mut result); |
| 12066 | result |
| 12067 | } |
| 12068 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12069 | bytes.reserve(additional:8); |
| 12070 | u16::from(self.type_).serialize_into(bytes); |
| 12071 | self.len.serialize_into(bytes); |
| 12072 | self.sourceid.serialize_into(bytes); |
| 12073 | let num_buttons: u16 = u16::try_from(self.labels.len()).expect(msg:"`labels` has too many elements" ); |
| 12074 | num_buttons.serialize_into(bytes); |
| 12075 | assert_eq!(self.state.len(), usize::try_from(u32::from(num_buttons).checked_add(31u32).unwrap().checked_div(32u32).unwrap()).unwrap(), "`state` has an incorrect length" ); |
| 12076 | self.state.serialize_into(bytes); |
| 12077 | self.labels.serialize_into(bytes); |
| 12078 | } |
| 12079 | } |
| 12080 | impl ButtonClass { |
| 12081 | /// Get the value of the `num_buttons` field. |
| 12082 | /// |
| 12083 | /// The `num_buttons` field is used as the length field of the `labels` field. |
| 12084 | /// This function computes the field's value again based on the length of the list. |
| 12085 | /// |
| 12086 | /// # Panics |
| 12087 | /// |
| 12088 | /// Panics if the value cannot be represented in the target type. This |
| 12089 | /// cannot happen with values of the struct received from the X11 server. |
| 12090 | pub fn num_buttons(&self) -> u16 { |
| 12091 | self.labels.len() |
| 12092 | .try_into().unwrap() |
| 12093 | } |
| 12094 | } |
| 12095 | |
| 12096 | #[derive (Clone, Default)] |
| 12097 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12098 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12099 | pub struct KeyClass { |
| 12100 | pub type_: DeviceClassType, |
| 12101 | pub len: u16, |
| 12102 | pub sourceid: DeviceId, |
| 12103 | pub keys: Vec<u32>, |
| 12104 | } |
| 12105 | impl_debug_if_no_extra_traits!(KeyClass, "KeyClass" ); |
| 12106 | impl TryParse for KeyClass { |
| 12107 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12108 | let (type_: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12109 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12110 | let (sourceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 12111 | let (num_keys: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12112 | let (keys: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:num_keys.try_to_usize()?)?; |
| 12113 | let type_: DeviceClassType = type_.into(); |
| 12114 | let result: KeyClass = KeyClass { type_, len, sourceid, keys }; |
| 12115 | Ok((result, remaining)) |
| 12116 | } |
| 12117 | } |
| 12118 | impl Serialize for KeyClass { |
| 12119 | type Bytes = Vec<u8>; |
| 12120 | fn serialize(&self) -> Vec<u8> { |
| 12121 | let mut result: Vec = Vec::new(); |
| 12122 | self.serialize_into(&mut result); |
| 12123 | result |
| 12124 | } |
| 12125 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12126 | bytes.reserve(additional:8); |
| 12127 | u16::from(self.type_).serialize_into(bytes); |
| 12128 | self.len.serialize_into(bytes); |
| 12129 | self.sourceid.serialize_into(bytes); |
| 12130 | let num_keys: u16 = u16::try_from(self.keys.len()).expect(msg:"`keys` has too many elements" ); |
| 12131 | num_keys.serialize_into(bytes); |
| 12132 | self.keys.serialize_into(bytes); |
| 12133 | } |
| 12134 | } |
| 12135 | impl KeyClass { |
| 12136 | /// Get the value of the `num_keys` field. |
| 12137 | /// |
| 12138 | /// The `num_keys` field is used as the length field of the `keys` field. |
| 12139 | /// This function computes the field's value again based on the length of the list. |
| 12140 | /// |
| 12141 | /// # Panics |
| 12142 | /// |
| 12143 | /// Panics if the value cannot be represented in the target type. This |
| 12144 | /// cannot happen with values of the struct received from the X11 server. |
| 12145 | pub fn num_keys(&self) -> u16 { |
| 12146 | self.keys.len() |
| 12147 | .try_into().unwrap() |
| 12148 | } |
| 12149 | } |
| 12150 | |
| 12151 | #[derive (Clone, Copy, Default)] |
| 12152 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12153 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12154 | pub struct ScrollClass { |
| 12155 | pub type_: DeviceClassType, |
| 12156 | pub len: u16, |
| 12157 | pub sourceid: DeviceId, |
| 12158 | pub number: u16, |
| 12159 | pub scroll_type: ScrollType, |
| 12160 | pub flags: ScrollFlags, |
| 12161 | pub increment: Fp3232, |
| 12162 | } |
| 12163 | impl_debug_if_no_extra_traits!(ScrollClass, "ScrollClass" ); |
| 12164 | impl TryParse for ScrollClass { |
| 12165 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12166 | let (type_: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12167 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12168 | let (sourceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 12169 | let (number: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12170 | let (scroll_type: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12171 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 12172 | let (flags: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 12173 | let (increment: Fp3232, remaining: &[u8]) = Fp3232::try_parse(remaining)?; |
| 12174 | let type_: DeviceClassType = type_.into(); |
| 12175 | let scroll_type: ScrollType = scroll_type.into(); |
| 12176 | let flags: ScrollFlags = flags.into(); |
| 12177 | let result: ScrollClass = ScrollClass { type_, len, sourceid, number, scroll_type, flags, increment }; |
| 12178 | Ok((result, remaining)) |
| 12179 | } |
| 12180 | } |
| 12181 | impl Serialize for ScrollClass { |
| 12182 | type Bytes = [u8; 24]; |
| 12183 | fn serialize(&self) -> [u8; 24] { |
| 12184 | let type_bytes = u16::from(self.type_).serialize(); |
| 12185 | let len_bytes = self.len.serialize(); |
| 12186 | let sourceid_bytes = self.sourceid.serialize(); |
| 12187 | let number_bytes = self.number.serialize(); |
| 12188 | let scroll_type_bytes = u16::from(self.scroll_type).serialize(); |
| 12189 | let flags_bytes = u32::from(self.flags).serialize(); |
| 12190 | let increment_bytes = self.increment.serialize(); |
| 12191 | [ |
| 12192 | type_bytes[0], |
| 12193 | type_bytes[1], |
| 12194 | len_bytes[0], |
| 12195 | len_bytes[1], |
| 12196 | sourceid_bytes[0], |
| 12197 | sourceid_bytes[1], |
| 12198 | number_bytes[0], |
| 12199 | number_bytes[1], |
| 12200 | scroll_type_bytes[0], |
| 12201 | scroll_type_bytes[1], |
| 12202 | 0, |
| 12203 | 0, |
| 12204 | flags_bytes[0], |
| 12205 | flags_bytes[1], |
| 12206 | flags_bytes[2], |
| 12207 | flags_bytes[3], |
| 12208 | increment_bytes[0], |
| 12209 | increment_bytes[1], |
| 12210 | increment_bytes[2], |
| 12211 | increment_bytes[3], |
| 12212 | increment_bytes[4], |
| 12213 | increment_bytes[5], |
| 12214 | increment_bytes[6], |
| 12215 | increment_bytes[7], |
| 12216 | ] |
| 12217 | } |
| 12218 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12219 | bytes.reserve(24); |
| 12220 | u16::from(self.type_).serialize_into(bytes); |
| 12221 | self.len.serialize_into(bytes); |
| 12222 | self.sourceid.serialize_into(bytes); |
| 12223 | self.number.serialize_into(bytes); |
| 12224 | u16::from(self.scroll_type).serialize_into(bytes); |
| 12225 | bytes.extend_from_slice(&[0; 2]); |
| 12226 | u32::from(self.flags).serialize_into(bytes); |
| 12227 | self.increment.serialize_into(bytes); |
| 12228 | } |
| 12229 | } |
| 12230 | |
| 12231 | #[derive (Clone, Copy, Default)] |
| 12232 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12233 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12234 | pub struct TouchClass { |
| 12235 | pub type_: DeviceClassType, |
| 12236 | pub len: u16, |
| 12237 | pub sourceid: DeviceId, |
| 12238 | pub mode: TouchMode, |
| 12239 | pub num_touches: u8, |
| 12240 | } |
| 12241 | impl_debug_if_no_extra_traits!(TouchClass, "TouchClass" ); |
| 12242 | impl TryParse for TouchClass { |
| 12243 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12244 | let (type_: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12245 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12246 | let (sourceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 12247 | let (mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 12248 | let (num_touches: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 12249 | let type_: DeviceClassType = type_.into(); |
| 12250 | let mode: TouchMode = mode.into(); |
| 12251 | let result: TouchClass = TouchClass { type_, len, sourceid, mode, num_touches }; |
| 12252 | Ok((result, remaining)) |
| 12253 | } |
| 12254 | } |
| 12255 | impl Serialize for TouchClass { |
| 12256 | type Bytes = [u8; 8]; |
| 12257 | fn serialize(&self) -> [u8; 8] { |
| 12258 | let type_bytes = u16::from(self.type_).serialize(); |
| 12259 | let len_bytes = self.len.serialize(); |
| 12260 | let sourceid_bytes = self.sourceid.serialize(); |
| 12261 | let mode_bytes = u8::from(self.mode).serialize(); |
| 12262 | let num_touches_bytes = self.num_touches.serialize(); |
| 12263 | [ |
| 12264 | type_bytes[0], |
| 12265 | type_bytes[1], |
| 12266 | len_bytes[0], |
| 12267 | len_bytes[1], |
| 12268 | sourceid_bytes[0], |
| 12269 | sourceid_bytes[1], |
| 12270 | mode_bytes[0], |
| 12271 | num_touches_bytes[0], |
| 12272 | ] |
| 12273 | } |
| 12274 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12275 | bytes.reserve(8); |
| 12276 | u16::from(self.type_).serialize_into(bytes); |
| 12277 | self.len.serialize_into(bytes); |
| 12278 | self.sourceid.serialize_into(bytes); |
| 12279 | u8::from(self.mode).serialize_into(bytes); |
| 12280 | self.num_touches.serialize_into(bytes); |
| 12281 | } |
| 12282 | } |
| 12283 | |
| 12284 | #[derive (Clone, Copy, Default)] |
| 12285 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12286 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12287 | pub struct GestureClass { |
| 12288 | pub type_: DeviceClassType, |
| 12289 | pub len: u16, |
| 12290 | pub sourceid: DeviceId, |
| 12291 | pub num_touches: u8, |
| 12292 | } |
| 12293 | impl_debug_if_no_extra_traits!(GestureClass, "GestureClass" ); |
| 12294 | impl TryParse for GestureClass { |
| 12295 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12296 | let (type_: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12297 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12298 | let (sourceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 12299 | let (num_touches: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 12300 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 12301 | let type_: DeviceClassType = type_.into(); |
| 12302 | let result: GestureClass = GestureClass { type_, len, sourceid, num_touches }; |
| 12303 | Ok((result, remaining)) |
| 12304 | } |
| 12305 | } |
| 12306 | impl Serialize for GestureClass { |
| 12307 | type Bytes = [u8; 8]; |
| 12308 | fn serialize(&self) -> [u8; 8] { |
| 12309 | let type_bytes = u16::from(self.type_).serialize(); |
| 12310 | let len_bytes = self.len.serialize(); |
| 12311 | let sourceid_bytes = self.sourceid.serialize(); |
| 12312 | let num_touches_bytes = self.num_touches.serialize(); |
| 12313 | [ |
| 12314 | type_bytes[0], |
| 12315 | type_bytes[1], |
| 12316 | len_bytes[0], |
| 12317 | len_bytes[1], |
| 12318 | sourceid_bytes[0], |
| 12319 | sourceid_bytes[1], |
| 12320 | num_touches_bytes[0], |
| 12321 | 0, |
| 12322 | ] |
| 12323 | } |
| 12324 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12325 | bytes.reserve(8); |
| 12326 | u16::from(self.type_).serialize_into(bytes); |
| 12327 | self.len.serialize_into(bytes); |
| 12328 | self.sourceid.serialize_into(bytes); |
| 12329 | self.num_touches.serialize_into(bytes); |
| 12330 | bytes.extend_from_slice(&[0; 1]); |
| 12331 | } |
| 12332 | } |
| 12333 | |
| 12334 | #[derive (Clone, Copy, Default)] |
| 12335 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12336 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12337 | pub struct ValuatorClass { |
| 12338 | pub type_: DeviceClassType, |
| 12339 | pub len: u16, |
| 12340 | pub sourceid: DeviceId, |
| 12341 | pub number: u16, |
| 12342 | pub label: xproto::Atom, |
| 12343 | pub min: Fp3232, |
| 12344 | pub max: Fp3232, |
| 12345 | pub value: Fp3232, |
| 12346 | pub resolution: u32, |
| 12347 | pub mode: ValuatorMode, |
| 12348 | } |
| 12349 | impl_debug_if_no_extra_traits!(ValuatorClass, "ValuatorClass" ); |
| 12350 | impl TryParse for ValuatorClass { |
| 12351 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12352 | let (type_: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12353 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12354 | let (sourceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 12355 | let (number: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12356 | let (label: u32, remaining: &[u8]) = xproto::Atom::try_parse(remaining)?; |
| 12357 | let (min: Fp3232, remaining: &[u8]) = Fp3232::try_parse(remaining)?; |
| 12358 | let (max: Fp3232, remaining: &[u8]) = Fp3232::try_parse(remaining)?; |
| 12359 | let (value: Fp3232, remaining: &[u8]) = Fp3232::try_parse(remaining)?; |
| 12360 | let (resolution: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 12361 | let (mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 12362 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
| 12363 | let type_: DeviceClassType = type_.into(); |
| 12364 | let mode: ValuatorMode = mode.into(); |
| 12365 | let result: ValuatorClass = ValuatorClass { type_, len, sourceid, number, label, min, max, value, resolution, mode }; |
| 12366 | Ok((result, remaining)) |
| 12367 | } |
| 12368 | } |
| 12369 | impl Serialize for ValuatorClass { |
| 12370 | type Bytes = [u8; 44]; |
| 12371 | fn serialize(&self) -> [u8; 44] { |
| 12372 | let type_bytes = u16::from(self.type_).serialize(); |
| 12373 | let len_bytes = self.len.serialize(); |
| 12374 | let sourceid_bytes = self.sourceid.serialize(); |
| 12375 | let number_bytes = self.number.serialize(); |
| 12376 | let label_bytes = self.label.serialize(); |
| 12377 | let min_bytes = self.min.serialize(); |
| 12378 | let max_bytes = self.max.serialize(); |
| 12379 | let value_bytes = self.value.serialize(); |
| 12380 | let resolution_bytes = self.resolution.serialize(); |
| 12381 | let mode_bytes = u8::from(self.mode).serialize(); |
| 12382 | [ |
| 12383 | type_bytes[0], |
| 12384 | type_bytes[1], |
| 12385 | len_bytes[0], |
| 12386 | len_bytes[1], |
| 12387 | sourceid_bytes[0], |
| 12388 | sourceid_bytes[1], |
| 12389 | number_bytes[0], |
| 12390 | number_bytes[1], |
| 12391 | label_bytes[0], |
| 12392 | label_bytes[1], |
| 12393 | label_bytes[2], |
| 12394 | label_bytes[3], |
| 12395 | min_bytes[0], |
| 12396 | min_bytes[1], |
| 12397 | min_bytes[2], |
| 12398 | min_bytes[3], |
| 12399 | min_bytes[4], |
| 12400 | min_bytes[5], |
| 12401 | min_bytes[6], |
| 12402 | min_bytes[7], |
| 12403 | max_bytes[0], |
| 12404 | max_bytes[1], |
| 12405 | max_bytes[2], |
| 12406 | max_bytes[3], |
| 12407 | max_bytes[4], |
| 12408 | max_bytes[5], |
| 12409 | max_bytes[6], |
| 12410 | max_bytes[7], |
| 12411 | value_bytes[0], |
| 12412 | value_bytes[1], |
| 12413 | value_bytes[2], |
| 12414 | value_bytes[3], |
| 12415 | value_bytes[4], |
| 12416 | value_bytes[5], |
| 12417 | value_bytes[6], |
| 12418 | value_bytes[7], |
| 12419 | resolution_bytes[0], |
| 12420 | resolution_bytes[1], |
| 12421 | resolution_bytes[2], |
| 12422 | resolution_bytes[3], |
| 12423 | mode_bytes[0], |
| 12424 | 0, |
| 12425 | 0, |
| 12426 | 0, |
| 12427 | ] |
| 12428 | } |
| 12429 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12430 | bytes.reserve(44); |
| 12431 | u16::from(self.type_).serialize_into(bytes); |
| 12432 | self.len.serialize_into(bytes); |
| 12433 | self.sourceid.serialize_into(bytes); |
| 12434 | self.number.serialize_into(bytes); |
| 12435 | self.label.serialize_into(bytes); |
| 12436 | self.min.serialize_into(bytes); |
| 12437 | self.max.serialize_into(bytes); |
| 12438 | self.value.serialize_into(bytes); |
| 12439 | self.resolution.serialize_into(bytes); |
| 12440 | u8::from(self.mode).serialize_into(bytes); |
| 12441 | bytes.extend_from_slice(&[0; 3]); |
| 12442 | } |
| 12443 | } |
| 12444 | |
| 12445 | #[derive (Clone)] |
| 12446 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12447 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12448 | pub struct DeviceClassDataKey { |
| 12449 | pub keys: Vec<u32>, |
| 12450 | } |
| 12451 | impl_debug_if_no_extra_traits!(DeviceClassDataKey, "DeviceClassDataKey" ); |
| 12452 | impl TryParse for DeviceClassDataKey { |
| 12453 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12454 | let (num_keys: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12455 | let (keys: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:num_keys.try_to_usize()?)?; |
| 12456 | let result: DeviceClassDataKey = DeviceClassDataKey { keys }; |
| 12457 | Ok((result, remaining)) |
| 12458 | } |
| 12459 | } |
| 12460 | impl Serialize for DeviceClassDataKey { |
| 12461 | type Bytes = Vec<u8>; |
| 12462 | fn serialize(&self) -> Vec<u8> { |
| 12463 | let mut result: Vec = Vec::new(); |
| 12464 | self.serialize_into(&mut result); |
| 12465 | result |
| 12466 | } |
| 12467 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12468 | let num_keys: u16 = u16::try_from(self.keys.len()).expect(msg:"`keys` has too many elements" ); |
| 12469 | num_keys.serialize_into(bytes); |
| 12470 | self.keys.serialize_into(bytes); |
| 12471 | } |
| 12472 | } |
| 12473 | impl DeviceClassDataKey { |
| 12474 | /// Get the value of the `num_keys` field. |
| 12475 | /// |
| 12476 | /// The `num_keys` field is used as the length field of the `keys` field. |
| 12477 | /// This function computes the field's value again based on the length of the list. |
| 12478 | /// |
| 12479 | /// # Panics |
| 12480 | /// |
| 12481 | /// Panics if the value cannot be represented in the target type. This |
| 12482 | /// cannot happen with values of the struct received from the X11 server. |
| 12483 | pub fn num_keys(&self) -> u16 { |
| 12484 | self.keys.len() |
| 12485 | .try_into().unwrap() |
| 12486 | } |
| 12487 | } |
| 12488 | #[derive (Clone)] |
| 12489 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12490 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12491 | pub struct DeviceClassDataButton { |
| 12492 | pub state: Vec<u32>, |
| 12493 | pub labels: Vec<xproto::Atom>, |
| 12494 | } |
| 12495 | impl_debug_if_no_extra_traits!(DeviceClassDataButton, "DeviceClassDataButton" ); |
| 12496 | impl TryParse for DeviceClassDataButton { |
| 12497 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12498 | let (num_buttons: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12499 | let (state: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<u32>(data:remaining, list_length:u32::from(num_buttons).checked_add(31u32).ok_or(ParseError::InvalidExpression)?.checked_div(32u32).ok_or(err:ParseError::InvalidExpression)?.try_to_usize()?)?; |
| 12500 | let (labels: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<xproto::Atom>(data:remaining, list_length:num_buttons.try_to_usize()?)?; |
| 12501 | let result: DeviceClassDataButton = DeviceClassDataButton { state, labels }; |
| 12502 | Ok((result, remaining)) |
| 12503 | } |
| 12504 | } |
| 12505 | impl Serialize for DeviceClassDataButton { |
| 12506 | type Bytes = Vec<u8>; |
| 12507 | fn serialize(&self) -> Vec<u8> { |
| 12508 | let mut result: Vec = Vec::new(); |
| 12509 | self.serialize_into(&mut result); |
| 12510 | result |
| 12511 | } |
| 12512 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12513 | let num_buttons: u16 = u16::try_from(self.labels.len()).expect(msg:"`labels` has too many elements" ); |
| 12514 | num_buttons.serialize_into(bytes); |
| 12515 | assert_eq!(self.state.len(), usize::try_from(u32::from(num_buttons).checked_add(31u32).unwrap().checked_div(32u32).unwrap()).unwrap(), "`state` has an incorrect length" ); |
| 12516 | self.state.serialize_into(bytes); |
| 12517 | self.labels.serialize_into(bytes); |
| 12518 | } |
| 12519 | } |
| 12520 | impl DeviceClassDataButton { |
| 12521 | /// Get the value of the `num_buttons` field. |
| 12522 | /// |
| 12523 | /// The `num_buttons` field is used as the length field of the `labels` field. |
| 12524 | /// This function computes the field's value again based on the length of the list. |
| 12525 | /// |
| 12526 | /// # Panics |
| 12527 | /// |
| 12528 | /// Panics if the value cannot be represented in the target type. This |
| 12529 | /// cannot happen with values of the struct received from the X11 server. |
| 12530 | pub fn num_buttons(&self) -> u16 { |
| 12531 | self.labels.len() |
| 12532 | .try_into().unwrap() |
| 12533 | } |
| 12534 | } |
| 12535 | #[derive (Clone, Copy)] |
| 12536 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12537 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12538 | pub struct DeviceClassDataValuator { |
| 12539 | pub number: u16, |
| 12540 | pub label: xproto::Atom, |
| 12541 | pub min: Fp3232, |
| 12542 | pub max: Fp3232, |
| 12543 | pub value: Fp3232, |
| 12544 | pub resolution: u32, |
| 12545 | pub mode: ValuatorMode, |
| 12546 | } |
| 12547 | impl_debug_if_no_extra_traits!(DeviceClassDataValuator, "DeviceClassDataValuator" ); |
| 12548 | impl TryParse for DeviceClassDataValuator { |
| 12549 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12550 | let (number: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12551 | let (label: u32, remaining: &[u8]) = xproto::Atom::try_parse(remaining)?; |
| 12552 | let (min: Fp3232, remaining: &[u8]) = Fp3232::try_parse(remaining)?; |
| 12553 | let (max: Fp3232, remaining: &[u8]) = Fp3232::try_parse(remaining)?; |
| 12554 | let (value: Fp3232, remaining: &[u8]) = Fp3232::try_parse(remaining)?; |
| 12555 | let (resolution: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 12556 | let (mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 12557 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
| 12558 | let mode: ValuatorMode = mode.into(); |
| 12559 | let result: DeviceClassDataValuator = DeviceClassDataValuator { number, label, min, max, value, resolution, mode }; |
| 12560 | Ok((result, remaining)) |
| 12561 | } |
| 12562 | } |
| 12563 | impl Serialize for DeviceClassDataValuator { |
| 12564 | type Bytes = [u8; 38]; |
| 12565 | fn serialize(&self) -> [u8; 38] { |
| 12566 | let number_bytes = self.number.serialize(); |
| 12567 | let label_bytes = self.label.serialize(); |
| 12568 | let min_bytes = self.min.serialize(); |
| 12569 | let max_bytes = self.max.serialize(); |
| 12570 | let value_bytes = self.value.serialize(); |
| 12571 | let resolution_bytes = self.resolution.serialize(); |
| 12572 | let mode_bytes = u8::from(self.mode).serialize(); |
| 12573 | [ |
| 12574 | number_bytes[0], |
| 12575 | number_bytes[1], |
| 12576 | label_bytes[0], |
| 12577 | label_bytes[1], |
| 12578 | label_bytes[2], |
| 12579 | label_bytes[3], |
| 12580 | min_bytes[0], |
| 12581 | min_bytes[1], |
| 12582 | min_bytes[2], |
| 12583 | min_bytes[3], |
| 12584 | min_bytes[4], |
| 12585 | min_bytes[5], |
| 12586 | min_bytes[6], |
| 12587 | min_bytes[7], |
| 12588 | max_bytes[0], |
| 12589 | max_bytes[1], |
| 12590 | max_bytes[2], |
| 12591 | max_bytes[3], |
| 12592 | max_bytes[4], |
| 12593 | max_bytes[5], |
| 12594 | max_bytes[6], |
| 12595 | max_bytes[7], |
| 12596 | value_bytes[0], |
| 12597 | value_bytes[1], |
| 12598 | value_bytes[2], |
| 12599 | value_bytes[3], |
| 12600 | value_bytes[4], |
| 12601 | value_bytes[5], |
| 12602 | value_bytes[6], |
| 12603 | value_bytes[7], |
| 12604 | resolution_bytes[0], |
| 12605 | resolution_bytes[1], |
| 12606 | resolution_bytes[2], |
| 12607 | resolution_bytes[3], |
| 12608 | mode_bytes[0], |
| 12609 | 0, |
| 12610 | 0, |
| 12611 | 0, |
| 12612 | ] |
| 12613 | } |
| 12614 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12615 | bytes.reserve(38); |
| 12616 | self.number.serialize_into(bytes); |
| 12617 | self.label.serialize_into(bytes); |
| 12618 | self.min.serialize_into(bytes); |
| 12619 | self.max.serialize_into(bytes); |
| 12620 | self.value.serialize_into(bytes); |
| 12621 | self.resolution.serialize_into(bytes); |
| 12622 | u8::from(self.mode).serialize_into(bytes); |
| 12623 | bytes.extend_from_slice(&[0; 3]); |
| 12624 | } |
| 12625 | } |
| 12626 | #[derive (Clone, Copy)] |
| 12627 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12628 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12629 | pub struct DeviceClassDataScroll { |
| 12630 | pub number: u16, |
| 12631 | pub scroll_type: ScrollType, |
| 12632 | pub flags: ScrollFlags, |
| 12633 | pub increment: Fp3232, |
| 12634 | } |
| 12635 | impl_debug_if_no_extra_traits!(DeviceClassDataScroll, "DeviceClassDataScroll" ); |
| 12636 | impl TryParse for DeviceClassDataScroll { |
| 12637 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12638 | let (number: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12639 | let (scroll_type: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12640 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 12641 | let (flags: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 12642 | let (increment: Fp3232, remaining: &[u8]) = Fp3232::try_parse(remaining)?; |
| 12643 | let scroll_type: ScrollType = scroll_type.into(); |
| 12644 | let flags: ScrollFlags = flags.into(); |
| 12645 | let result: DeviceClassDataScroll = DeviceClassDataScroll { number, scroll_type, flags, increment }; |
| 12646 | Ok((result, remaining)) |
| 12647 | } |
| 12648 | } |
| 12649 | impl Serialize for DeviceClassDataScroll { |
| 12650 | type Bytes = [u8; 18]; |
| 12651 | fn serialize(&self) -> [u8; 18] { |
| 12652 | let number_bytes = self.number.serialize(); |
| 12653 | let scroll_type_bytes = u16::from(self.scroll_type).serialize(); |
| 12654 | let flags_bytes = u32::from(self.flags).serialize(); |
| 12655 | let increment_bytes = self.increment.serialize(); |
| 12656 | [ |
| 12657 | number_bytes[0], |
| 12658 | number_bytes[1], |
| 12659 | scroll_type_bytes[0], |
| 12660 | scroll_type_bytes[1], |
| 12661 | 0, |
| 12662 | 0, |
| 12663 | flags_bytes[0], |
| 12664 | flags_bytes[1], |
| 12665 | flags_bytes[2], |
| 12666 | flags_bytes[3], |
| 12667 | increment_bytes[0], |
| 12668 | increment_bytes[1], |
| 12669 | increment_bytes[2], |
| 12670 | increment_bytes[3], |
| 12671 | increment_bytes[4], |
| 12672 | increment_bytes[5], |
| 12673 | increment_bytes[6], |
| 12674 | increment_bytes[7], |
| 12675 | ] |
| 12676 | } |
| 12677 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12678 | bytes.reserve(18); |
| 12679 | self.number.serialize_into(bytes); |
| 12680 | u16::from(self.scroll_type).serialize_into(bytes); |
| 12681 | bytes.extend_from_slice(&[0; 2]); |
| 12682 | u32::from(self.flags).serialize_into(bytes); |
| 12683 | self.increment.serialize_into(bytes); |
| 12684 | } |
| 12685 | } |
| 12686 | #[derive (Clone, Copy)] |
| 12687 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12688 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12689 | pub struct DeviceClassDataTouch { |
| 12690 | pub mode: TouchMode, |
| 12691 | pub num_touches: u8, |
| 12692 | } |
| 12693 | impl_debug_if_no_extra_traits!(DeviceClassDataTouch, "DeviceClassDataTouch" ); |
| 12694 | impl TryParse for DeviceClassDataTouch { |
| 12695 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12696 | let (mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 12697 | let (num_touches: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 12698 | let mode: TouchMode = mode.into(); |
| 12699 | let result: DeviceClassDataTouch = DeviceClassDataTouch { mode, num_touches }; |
| 12700 | Ok((result, remaining)) |
| 12701 | } |
| 12702 | } |
| 12703 | impl Serialize for DeviceClassDataTouch { |
| 12704 | type Bytes = [u8; 2]; |
| 12705 | fn serialize(&self) -> [u8; 2] { |
| 12706 | let mode_bytes: [u8; 1] = u8::from(self.mode).serialize(); |
| 12707 | let num_touches_bytes: [u8; 1] = self.num_touches.serialize(); |
| 12708 | [ |
| 12709 | mode_bytes[0], |
| 12710 | num_touches_bytes[0], |
| 12711 | ] |
| 12712 | } |
| 12713 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12714 | bytes.reserve(additional:2); |
| 12715 | u8::from(self.mode).serialize_into(bytes); |
| 12716 | self.num_touches.serialize_into(bytes); |
| 12717 | } |
| 12718 | } |
| 12719 | #[derive (Clone, Copy)] |
| 12720 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12721 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12722 | pub struct DeviceClassDataGesture { |
| 12723 | pub num_touches: u8, |
| 12724 | } |
| 12725 | impl_debug_if_no_extra_traits!(DeviceClassDataGesture, "DeviceClassDataGesture" ); |
| 12726 | impl TryParse for DeviceClassDataGesture { |
| 12727 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12728 | let (num_touches: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 12729 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 12730 | let result: DeviceClassDataGesture = DeviceClassDataGesture { num_touches }; |
| 12731 | Ok((result, remaining)) |
| 12732 | } |
| 12733 | } |
| 12734 | impl Serialize for DeviceClassDataGesture { |
| 12735 | type Bytes = [u8; 2]; |
| 12736 | fn serialize(&self) -> [u8; 2] { |
| 12737 | let num_touches_bytes: [u8; 1] = self.num_touches.serialize(); |
| 12738 | [ |
| 12739 | num_touches_bytes[0], |
| 12740 | 0, |
| 12741 | ] |
| 12742 | } |
| 12743 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12744 | bytes.reserve(additional:2); |
| 12745 | self.num_touches.serialize_into(bytes); |
| 12746 | bytes.extend_from_slice(&[0; 1]); |
| 12747 | } |
| 12748 | } |
| 12749 | #[derive (Clone)] |
| 12750 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12751 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12752 | pub enum DeviceClassData { |
| 12753 | Key(DeviceClassDataKey), |
| 12754 | Button(DeviceClassDataButton), |
| 12755 | Valuator(DeviceClassDataValuator), |
| 12756 | Scroll(DeviceClassDataScroll), |
| 12757 | Touch(DeviceClassDataTouch), |
| 12758 | Gesture(DeviceClassDataGesture), |
| 12759 | /// This variant is returned when the server sends a discriminant |
| 12760 | /// value that does not match any of the defined by the protocol. |
| 12761 | /// |
| 12762 | /// Usually, this should be considered a parsing error, but there |
| 12763 | /// are some cases where the server violates the protocol. |
| 12764 | /// |
| 12765 | /// Trying to use `serialize` or `serialize_into` with this variant |
| 12766 | /// will raise a panic. |
| 12767 | InvalidValue(u16), |
| 12768 | } |
| 12769 | impl_debug_if_no_extra_traits!(DeviceClassData, "DeviceClassData" ); |
| 12770 | impl DeviceClassData { |
| 12771 | #[cfg_attr (not(feature = "request-parsing" ), allow(dead_code))] |
| 12772 | fn try_parse(value: &[u8], type_: u16) -> Result<(Self, &[u8]), ParseError> { |
| 12773 | let switch_expr = u16::from(type_); |
| 12774 | let mut outer_remaining = value; |
| 12775 | let mut parse_result = None; |
| 12776 | if switch_expr == u16::from(DeviceClassType::KEY) { |
| 12777 | let (key, new_remaining) = DeviceClassDataKey::try_parse(outer_remaining)?; |
| 12778 | outer_remaining = new_remaining; |
| 12779 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 12780 | parse_result = Some(DeviceClassData::Key(key)); |
| 12781 | } |
| 12782 | if switch_expr == u16::from(DeviceClassType::BUTTON) { |
| 12783 | let (button, new_remaining) = DeviceClassDataButton::try_parse(outer_remaining)?; |
| 12784 | outer_remaining = new_remaining; |
| 12785 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 12786 | parse_result = Some(DeviceClassData::Button(button)); |
| 12787 | } |
| 12788 | if switch_expr == u16::from(DeviceClassType::VALUATOR) { |
| 12789 | let (valuator, new_remaining) = DeviceClassDataValuator::try_parse(outer_remaining)?; |
| 12790 | outer_remaining = new_remaining; |
| 12791 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 12792 | parse_result = Some(DeviceClassData::Valuator(valuator)); |
| 12793 | } |
| 12794 | if switch_expr == u16::from(DeviceClassType::SCROLL) { |
| 12795 | let (scroll, new_remaining) = DeviceClassDataScroll::try_parse(outer_remaining)?; |
| 12796 | outer_remaining = new_remaining; |
| 12797 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 12798 | parse_result = Some(DeviceClassData::Scroll(scroll)); |
| 12799 | } |
| 12800 | if switch_expr == u16::from(DeviceClassType::TOUCH) { |
| 12801 | let (touch, new_remaining) = DeviceClassDataTouch::try_parse(outer_remaining)?; |
| 12802 | outer_remaining = new_remaining; |
| 12803 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 12804 | parse_result = Some(DeviceClassData::Touch(touch)); |
| 12805 | } |
| 12806 | if switch_expr == u16::from(DeviceClassType::GESTURE) { |
| 12807 | let (gesture, new_remaining) = DeviceClassDataGesture::try_parse(outer_remaining)?; |
| 12808 | outer_remaining = new_remaining; |
| 12809 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 12810 | parse_result = Some(DeviceClassData::Gesture(gesture)); |
| 12811 | } |
| 12812 | match parse_result { |
| 12813 | None => Ok((DeviceClassData::InvalidValue(switch_expr), &[])), |
| 12814 | Some(result) => Ok((result, outer_remaining)), |
| 12815 | } |
| 12816 | } |
| 12817 | } |
| 12818 | impl DeviceClassData { |
| 12819 | pub fn as_key(&self) -> Option<&DeviceClassDataKey> { |
| 12820 | match self { |
| 12821 | DeviceClassData::Key(value) => Some(value), |
| 12822 | _ => None, |
| 12823 | } |
| 12824 | } |
| 12825 | pub fn as_button(&self) -> Option<&DeviceClassDataButton> { |
| 12826 | match self { |
| 12827 | DeviceClassData::Button(value) => Some(value), |
| 12828 | _ => None, |
| 12829 | } |
| 12830 | } |
| 12831 | pub fn as_valuator(&self) -> Option<&DeviceClassDataValuator> { |
| 12832 | match self { |
| 12833 | DeviceClassData::Valuator(value) => Some(value), |
| 12834 | _ => None, |
| 12835 | } |
| 12836 | } |
| 12837 | pub fn as_scroll(&self) -> Option<&DeviceClassDataScroll> { |
| 12838 | match self { |
| 12839 | DeviceClassData::Scroll(value) => Some(value), |
| 12840 | _ => None, |
| 12841 | } |
| 12842 | } |
| 12843 | pub fn as_touch(&self) -> Option<&DeviceClassDataTouch> { |
| 12844 | match self { |
| 12845 | DeviceClassData::Touch(value) => Some(value), |
| 12846 | _ => None, |
| 12847 | } |
| 12848 | } |
| 12849 | pub fn as_gesture(&self) -> Option<&DeviceClassDataGesture> { |
| 12850 | match self { |
| 12851 | DeviceClassData::Gesture(value) => Some(value), |
| 12852 | _ => None, |
| 12853 | } |
| 12854 | } |
| 12855 | } |
| 12856 | impl DeviceClassData { |
| 12857 | #[allow (dead_code)] |
| 12858 | fn serialize(&self, type_: u16) -> Vec<u8> { |
| 12859 | let mut result: Vec = Vec::new(); |
| 12860 | self.serialize_into(&mut result, type_:u16::from(type_)); |
| 12861 | result |
| 12862 | } |
| 12863 | fn serialize_into(&self, bytes: &mut Vec<u8>, type_: u16) { |
| 12864 | assert_eq!(self.switch_expr(), u16::from(type_), "switch `data` has an inconsistent discriminant" ); |
| 12865 | match self { |
| 12866 | DeviceClassData::Key(key: &DeviceClassDataKey) => key.serialize_into(bytes), |
| 12867 | DeviceClassData::Button(button: &DeviceClassDataButton) => button.serialize_into(bytes), |
| 12868 | DeviceClassData::Valuator(valuator: &DeviceClassDataValuator) => valuator.serialize_into(bytes), |
| 12869 | DeviceClassData::Scroll(scroll: &DeviceClassDataScroll) => scroll.serialize_into(bytes), |
| 12870 | DeviceClassData::Touch(touch: &DeviceClassDataTouch) => touch.serialize_into(bytes), |
| 12871 | DeviceClassData::Gesture(gesture: &DeviceClassDataGesture) => gesture.serialize_into(bytes), |
| 12872 | DeviceClassData::InvalidValue(_) => panic!("attempted to serialize invalid switch case" ), |
| 12873 | } |
| 12874 | } |
| 12875 | } |
| 12876 | impl DeviceClassData { |
| 12877 | fn switch_expr(&self) -> u16 { |
| 12878 | match self { |
| 12879 | DeviceClassData::Key(_) => u16::from(DeviceClassType::KEY), |
| 12880 | DeviceClassData::Button(_) => u16::from(DeviceClassType::BUTTON), |
| 12881 | DeviceClassData::Valuator(_) => u16::from(DeviceClassType::VALUATOR), |
| 12882 | DeviceClassData::Scroll(_) => u16::from(DeviceClassType::SCROLL), |
| 12883 | DeviceClassData::Touch(_) => u16::from(DeviceClassType::TOUCH), |
| 12884 | DeviceClassData::Gesture(_) => u16::from(DeviceClassType::GESTURE), |
| 12885 | DeviceClassData::InvalidValue(switch_expr: &u16) => *switch_expr, |
| 12886 | } |
| 12887 | } |
| 12888 | } |
| 12889 | |
| 12890 | #[derive (Clone)] |
| 12891 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12892 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12893 | pub struct DeviceClass { |
| 12894 | pub len: u16, |
| 12895 | pub sourceid: DeviceId, |
| 12896 | pub data: DeviceClassData, |
| 12897 | } |
| 12898 | impl_debug_if_no_extra_traits!(DeviceClass, "DeviceClass" ); |
| 12899 | impl TryParse for DeviceClass { |
| 12900 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12901 | let remaining: &[u8] = initial_value; |
| 12902 | let (type_: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12903 | let (len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12904 | let (sourceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 12905 | let (data: DeviceClassData, remaining: &[u8]) = DeviceClassData::try_parse(value:remaining, type_:u16::from(type_))?; |
| 12906 | let result: DeviceClass = DeviceClass { len, sourceid, data }; |
| 12907 | let length: usize = u32::from(len).checked_mul(4u32).ok_or(err:ParseError::InvalidExpression)?.try_to_usize()?; |
| 12908 | let _ = remaining; |
| 12909 | let remaining: &[u8] = initial_value.get(length..) |
| 12910 | .ok_or(err:ParseError::InsufficientData)?; |
| 12911 | Ok((result, remaining)) |
| 12912 | } |
| 12913 | } |
| 12914 | impl Serialize for DeviceClass { |
| 12915 | type Bytes = Vec<u8>; |
| 12916 | fn serialize(&self) -> Vec<u8> { |
| 12917 | let mut result: Vec = Vec::new(); |
| 12918 | self.serialize_into(&mut result); |
| 12919 | result |
| 12920 | } |
| 12921 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12922 | bytes.reserve(additional:6); |
| 12923 | let type_: u16 = self.data.switch_expr(); |
| 12924 | type_.serialize_into(bytes); |
| 12925 | self.len.serialize_into(bytes); |
| 12926 | self.sourceid.serialize_into(bytes); |
| 12927 | self.data.serialize_into(bytes, type_:u16::from(type_)); |
| 12928 | } |
| 12929 | } |
| 12930 | |
| 12931 | #[derive (Clone)] |
| 12932 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 12933 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 12934 | pub struct XIDeviceInfo { |
| 12935 | pub deviceid: DeviceId, |
| 12936 | pub type_: DeviceType, |
| 12937 | pub attachment: DeviceId, |
| 12938 | pub enabled: bool, |
| 12939 | pub name: Vec<u8>, |
| 12940 | pub classes: Vec<DeviceClass>, |
| 12941 | } |
| 12942 | impl_debug_if_no_extra_traits!(XIDeviceInfo, "XIDeviceInfo" ); |
| 12943 | impl TryParse for XIDeviceInfo { |
| 12944 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 12945 | let value: &[u8] = remaining; |
| 12946 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 12947 | let (type_: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12948 | let (attachment: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 12949 | let (num_classes: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12950 | let (name_len: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 12951 | let (enabled: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
| 12952 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 12953 | let (name: &[u8], remaining: &[u8]) = crate::x11_utils::parse_u8_list(data:remaining, list_length:name_len.try_to_usize()?)?; |
| 12954 | let name: Vec = name.to_vec(); |
| 12955 | // Align offset to multiple of 4 |
| 12956 | let offset: usize = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 12957 | let misalignment: usize = (4 - (offset % 4)) % 4; |
| 12958 | let remaining: &[u8] = remaining.get(misalignment..).ok_or(err:ParseError::InsufficientData)?; |
| 12959 | let (classes: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<DeviceClass>(data:remaining, list_length:num_classes.try_to_usize()?)?; |
| 12960 | let type_: DeviceType = type_.into(); |
| 12961 | let result: XIDeviceInfo = XIDeviceInfo { deviceid, type_, attachment, enabled, name, classes }; |
| 12962 | Ok((result, remaining)) |
| 12963 | } |
| 12964 | } |
| 12965 | impl Serialize for XIDeviceInfo { |
| 12966 | type Bytes = Vec<u8>; |
| 12967 | fn serialize(&self) -> Vec<u8> { |
| 12968 | let mut result: Vec = Vec::new(); |
| 12969 | self.serialize_into(&mut result); |
| 12970 | result |
| 12971 | } |
| 12972 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 12973 | bytes.reserve(additional:12); |
| 12974 | self.deviceid.serialize_into(bytes); |
| 12975 | u16::from(self.type_).serialize_into(bytes); |
| 12976 | self.attachment.serialize_into(bytes); |
| 12977 | let num_classes: u16 = u16::try_from(self.classes.len()).expect(msg:"`classes` has too many elements" ); |
| 12978 | num_classes.serialize_into(bytes); |
| 12979 | let name_len: u16 = u16::try_from(self.name.len()).expect(msg:"`name` has too many elements" ); |
| 12980 | name_len.serialize_into(bytes); |
| 12981 | self.enabled.serialize_into(bytes); |
| 12982 | bytes.extend_from_slice(&[0; 1]); |
| 12983 | bytes.extend_from_slice(&self.name); |
| 12984 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 12985 | self.classes.serialize_into(bytes); |
| 12986 | } |
| 12987 | } |
| 12988 | impl XIDeviceInfo { |
| 12989 | /// Get the value of the `num_classes` field. |
| 12990 | /// |
| 12991 | /// The `num_classes` field is used as the length field of the `classes` field. |
| 12992 | /// This function computes the field's value again based on the length of the list. |
| 12993 | /// |
| 12994 | /// # Panics |
| 12995 | /// |
| 12996 | /// Panics if the value cannot be represented in the target type. This |
| 12997 | /// cannot happen with values of the struct received from the X11 server. |
| 12998 | pub fn num_classes(&self) -> u16 { |
| 12999 | self.classes.len() |
| 13000 | .try_into().unwrap() |
| 13001 | } |
| 13002 | /// Get the value of the `name_len` field. |
| 13003 | /// |
| 13004 | /// The `name_len` field is used as the length field of the `name` field. |
| 13005 | /// This function computes the field's value again based on the length of the list. |
| 13006 | /// |
| 13007 | /// # Panics |
| 13008 | /// |
| 13009 | /// Panics if the value cannot be represented in the target type. This |
| 13010 | /// cannot happen with values of the struct received from the X11 server. |
| 13011 | pub fn name_len(&self) -> u16 { |
| 13012 | self.name.len() |
| 13013 | .try_into().unwrap() |
| 13014 | } |
| 13015 | } |
| 13016 | |
| 13017 | /// Opcode for the XIQueryDevice request |
| 13018 | pub const XI_QUERY_DEVICE_REQUEST: u8 = 48; |
| 13019 | #[derive (Clone, Copy, Default)] |
| 13020 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 13021 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13022 | pub struct XIQueryDeviceRequest { |
| 13023 | pub deviceid: DeviceId, |
| 13024 | } |
| 13025 | impl_debug_if_no_extra_traits!(XIQueryDeviceRequest, "XIQueryDeviceRequest" ); |
| 13026 | impl XIQueryDeviceRequest { |
| 13027 | /// Serialize this request into bytes for the provided connection |
| 13028 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 13029 | let length_so_far = 0; |
| 13030 | let deviceid_bytes = self.deviceid.serialize(); |
| 13031 | let mut request0 = vec![ |
| 13032 | major_opcode, |
| 13033 | XI_QUERY_DEVICE_REQUEST, |
| 13034 | 0, |
| 13035 | 0, |
| 13036 | deviceid_bytes[0], |
| 13037 | deviceid_bytes[1], |
| 13038 | 0, |
| 13039 | 0, |
| 13040 | ]; |
| 13041 | let length_so_far = length_so_far + request0.len(); |
| 13042 | assert_eq!(length_so_far % 4, 0); |
| 13043 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 13044 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 13045 | ([request0.into()], vec![]) |
| 13046 | } |
| 13047 | /// Parse this request given its header, its body, and any fds that go along with it |
| 13048 | #[cfg (feature = "request-parsing" )] |
| 13049 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 13050 | if header.minor_opcode != XI_QUERY_DEVICE_REQUEST { |
| 13051 | return Err(ParseError::InvalidValue); |
| 13052 | } |
| 13053 | let (deviceid, remaining) = DeviceId::try_parse(value)?; |
| 13054 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 13055 | let _ = remaining; |
| 13056 | Ok(XIQueryDeviceRequest { |
| 13057 | deviceid, |
| 13058 | }) |
| 13059 | } |
| 13060 | } |
| 13061 | impl Request for XIQueryDeviceRequest { |
| 13062 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 13063 | |
| 13064 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 13065 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 13066 | // Flatten the buffers into a single vector |
| 13067 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 13068 | (buf, fds) |
| 13069 | } |
| 13070 | } |
| 13071 | impl crate::x11_utils::ReplyRequest for XIQueryDeviceRequest { |
| 13072 | type Reply = XIQueryDeviceReply; |
| 13073 | } |
| 13074 | |
| 13075 | #[derive (Clone)] |
| 13076 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 13077 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13078 | pub struct XIQueryDeviceReply { |
| 13079 | pub sequence: u16, |
| 13080 | pub length: u32, |
| 13081 | pub infos: Vec<XIDeviceInfo>, |
| 13082 | } |
| 13083 | impl_debug_if_no_extra_traits!(XIQueryDeviceReply, "XIQueryDeviceReply" ); |
| 13084 | impl TryParse for XIQueryDeviceReply { |
| 13085 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 13086 | let remaining: &[u8] = initial_value; |
| 13087 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 13088 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 13089 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 13090 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 13091 | let (num_infos: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 13092 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
| 13093 | let (infos: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<XIDeviceInfo>(data:remaining, list_length:num_infos.try_to_usize()?)?; |
| 13094 | if response_type != 1 { |
| 13095 | return Err(ParseError::InvalidValue); |
| 13096 | } |
| 13097 | let result: XIQueryDeviceReply = XIQueryDeviceReply { sequence, length, infos }; |
| 13098 | let _ = remaining; |
| 13099 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 13100 | .ok_or(err:ParseError::InsufficientData)?; |
| 13101 | Ok((result, remaining)) |
| 13102 | } |
| 13103 | } |
| 13104 | impl Serialize for XIQueryDeviceReply { |
| 13105 | type Bytes = Vec<u8>; |
| 13106 | fn serialize(&self) -> Vec<u8> { |
| 13107 | let mut result: Vec = Vec::new(); |
| 13108 | self.serialize_into(&mut result); |
| 13109 | result |
| 13110 | } |
| 13111 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 13112 | bytes.reserve(additional:32); |
| 13113 | let response_type_bytes: &[u8; 1] = &[1]; |
| 13114 | bytes.push(response_type_bytes[0]); |
| 13115 | bytes.extend_from_slice(&[0; 1]); |
| 13116 | self.sequence.serialize_into(bytes); |
| 13117 | self.length.serialize_into(bytes); |
| 13118 | let num_infos: u16 = u16::try_from(self.infos.len()).expect(msg:"`infos` has too many elements" ); |
| 13119 | num_infos.serialize_into(bytes); |
| 13120 | bytes.extend_from_slice(&[0; 22]); |
| 13121 | self.infos.serialize_into(bytes); |
| 13122 | } |
| 13123 | } |
| 13124 | impl XIQueryDeviceReply { |
| 13125 | /// Get the value of the `num_infos` field. |
| 13126 | /// |
| 13127 | /// The `num_infos` field is used as the length field of the `infos` field. |
| 13128 | /// This function computes the field's value again based on the length of the list. |
| 13129 | /// |
| 13130 | /// # Panics |
| 13131 | /// |
| 13132 | /// Panics if the value cannot be represented in the target type. This |
| 13133 | /// cannot happen with values of the struct received from the X11 server. |
| 13134 | pub fn num_infos(&self) -> u16 { |
| 13135 | self.infos.len() |
| 13136 | .try_into().unwrap() |
| 13137 | } |
| 13138 | } |
| 13139 | |
| 13140 | /// Opcode for the XISetFocus request |
| 13141 | pub const XI_SET_FOCUS_REQUEST: u8 = 49; |
| 13142 | #[derive (Clone, Copy, Default)] |
| 13143 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 13144 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13145 | pub struct XISetFocusRequest { |
| 13146 | pub window: xproto::Window, |
| 13147 | pub time: xproto::Timestamp, |
| 13148 | pub deviceid: DeviceId, |
| 13149 | } |
| 13150 | impl_debug_if_no_extra_traits!(XISetFocusRequest, "XISetFocusRequest" ); |
| 13151 | impl XISetFocusRequest { |
| 13152 | /// Serialize this request into bytes for the provided connection |
| 13153 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 13154 | let length_so_far = 0; |
| 13155 | let window_bytes = self.window.serialize(); |
| 13156 | let time_bytes = self.time.serialize(); |
| 13157 | let deviceid_bytes = self.deviceid.serialize(); |
| 13158 | let mut request0 = vec![ |
| 13159 | major_opcode, |
| 13160 | XI_SET_FOCUS_REQUEST, |
| 13161 | 0, |
| 13162 | 0, |
| 13163 | window_bytes[0], |
| 13164 | window_bytes[1], |
| 13165 | window_bytes[2], |
| 13166 | window_bytes[3], |
| 13167 | time_bytes[0], |
| 13168 | time_bytes[1], |
| 13169 | time_bytes[2], |
| 13170 | time_bytes[3], |
| 13171 | deviceid_bytes[0], |
| 13172 | deviceid_bytes[1], |
| 13173 | 0, |
| 13174 | 0, |
| 13175 | ]; |
| 13176 | let length_so_far = length_so_far + request0.len(); |
| 13177 | assert_eq!(length_so_far % 4, 0); |
| 13178 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 13179 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 13180 | ([request0.into()], vec![]) |
| 13181 | } |
| 13182 | /// Parse this request given its header, its body, and any fds that go along with it |
| 13183 | #[cfg (feature = "request-parsing" )] |
| 13184 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 13185 | if header.minor_opcode != XI_SET_FOCUS_REQUEST { |
| 13186 | return Err(ParseError::InvalidValue); |
| 13187 | } |
| 13188 | let (window, remaining) = xproto::Window::try_parse(value)?; |
| 13189 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 13190 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 13191 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 13192 | let _ = remaining; |
| 13193 | Ok(XISetFocusRequest { |
| 13194 | window, |
| 13195 | time, |
| 13196 | deviceid, |
| 13197 | }) |
| 13198 | } |
| 13199 | } |
| 13200 | impl Request for XISetFocusRequest { |
| 13201 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 13202 | |
| 13203 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 13204 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 13205 | // Flatten the buffers into a single vector |
| 13206 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 13207 | (buf, fds) |
| 13208 | } |
| 13209 | } |
| 13210 | impl crate::x11_utils::VoidRequest for XISetFocusRequest { |
| 13211 | } |
| 13212 | |
| 13213 | /// Opcode for the XIGetFocus request |
| 13214 | pub const XI_GET_FOCUS_REQUEST: u8 = 50; |
| 13215 | #[derive (Clone, Copy, Default)] |
| 13216 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 13217 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13218 | pub struct XIGetFocusRequest { |
| 13219 | pub deviceid: DeviceId, |
| 13220 | } |
| 13221 | impl_debug_if_no_extra_traits!(XIGetFocusRequest, "XIGetFocusRequest" ); |
| 13222 | impl XIGetFocusRequest { |
| 13223 | /// Serialize this request into bytes for the provided connection |
| 13224 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 13225 | let length_so_far = 0; |
| 13226 | let deviceid_bytes = self.deviceid.serialize(); |
| 13227 | let mut request0 = vec![ |
| 13228 | major_opcode, |
| 13229 | XI_GET_FOCUS_REQUEST, |
| 13230 | 0, |
| 13231 | 0, |
| 13232 | deviceid_bytes[0], |
| 13233 | deviceid_bytes[1], |
| 13234 | 0, |
| 13235 | 0, |
| 13236 | ]; |
| 13237 | let length_so_far = length_so_far + request0.len(); |
| 13238 | assert_eq!(length_so_far % 4, 0); |
| 13239 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 13240 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 13241 | ([request0.into()], vec![]) |
| 13242 | } |
| 13243 | /// Parse this request given its header, its body, and any fds that go along with it |
| 13244 | #[cfg (feature = "request-parsing" )] |
| 13245 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 13246 | if header.minor_opcode != XI_GET_FOCUS_REQUEST { |
| 13247 | return Err(ParseError::InvalidValue); |
| 13248 | } |
| 13249 | let (deviceid, remaining) = DeviceId::try_parse(value)?; |
| 13250 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 13251 | let _ = remaining; |
| 13252 | Ok(XIGetFocusRequest { |
| 13253 | deviceid, |
| 13254 | }) |
| 13255 | } |
| 13256 | } |
| 13257 | impl Request for XIGetFocusRequest { |
| 13258 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 13259 | |
| 13260 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 13261 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 13262 | // Flatten the buffers into a single vector |
| 13263 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 13264 | (buf, fds) |
| 13265 | } |
| 13266 | } |
| 13267 | impl crate::x11_utils::ReplyRequest for XIGetFocusRequest { |
| 13268 | type Reply = XIGetFocusReply; |
| 13269 | } |
| 13270 | |
| 13271 | #[derive (Clone, Copy, Default)] |
| 13272 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 13273 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13274 | pub struct XIGetFocusReply { |
| 13275 | pub sequence: u16, |
| 13276 | pub length: u32, |
| 13277 | pub focus: xproto::Window, |
| 13278 | } |
| 13279 | impl_debug_if_no_extra_traits!(XIGetFocusReply, "XIGetFocusReply" ); |
| 13280 | impl TryParse for XIGetFocusReply { |
| 13281 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 13282 | let remaining: &[u8] = initial_value; |
| 13283 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 13284 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 13285 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 13286 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 13287 | let (focus: u32, remaining: &[u8]) = xproto::Window::try_parse(remaining)?; |
| 13288 | let remaining: &[u8] = remaining.get(20..).ok_or(err:ParseError::InsufficientData)?; |
| 13289 | if response_type != 1 { |
| 13290 | return Err(ParseError::InvalidValue); |
| 13291 | } |
| 13292 | let result: XIGetFocusReply = XIGetFocusReply { sequence, length, focus }; |
| 13293 | let _ = remaining; |
| 13294 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 13295 | .ok_or(err:ParseError::InsufficientData)?; |
| 13296 | Ok((result, remaining)) |
| 13297 | } |
| 13298 | } |
| 13299 | impl Serialize for XIGetFocusReply { |
| 13300 | type Bytes = [u8; 32]; |
| 13301 | fn serialize(&self) -> [u8; 32] { |
| 13302 | let response_type_bytes = &[1]; |
| 13303 | let sequence_bytes = self.sequence.serialize(); |
| 13304 | let length_bytes = self.length.serialize(); |
| 13305 | let focus_bytes = self.focus.serialize(); |
| 13306 | [ |
| 13307 | response_type_bytes[0], |
| 13308 | 0, |
| 13309 | sequence_bytes[0], |
| 13310 | sequence_bytes[1], |
| 13311 | length_bytes[0], |
| 13312 | length_bytes[1], |
| 13313 | length_bytes[2], |
| 13314 | length_bytes[3], |
| 13315 | focus_bytes[0], |
| 13316 | focus_bytes[1], |
| 13317 | focus_bytes[2], |
| 13318 | focus_bytes[3], |
| 13319 | 0, |
| 13320 | 0, |
| 13321 | 0, |
| 13322 | 0, |
| 13323 | 0, |
| 13324 | 0, |
| 13325 | 0, |
| 13326 | 0, |
| 13327 | 0, |
| 13328 | 0, |
| 13329 | 0, |
| 13330 | 0, |
| 13331 | 0, |
| 13332 | 0, |
| 13333 | 0, |
| 13334 | 0, |
| 13335 | 0, |
| 13336 | 0, |
| 13337 | 0, |
| 13338 | 0, |
| 13339 | ] |
| 13340 | } |
| 13341 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 13342 | bytes.reserve(32); |
| 13343 | let response_type_bytes = &[1]; |
| 13344 | bytes.push(response_type_bytes[0]); |
| 13345 | bytes.extend_from_slice(&[0; 1]); |
| 13346 | self.sequence.serialize_into(bytes); |
| 13347 | self.length.serialize_into(bytes); |
| 13348 | self.focus.serialize_into(bytes); |
| 13349 | bytes.extend_from_slice(&[0; 20]); |
| 13350 | } |
| 13351 | } |
| 13352 | |
| 13353 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 13354 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13355 | pub struct GrabOwner(bool); |
| 13356 | impl GrabOwner { |
| 13357 | pub const NO_OWNER: Self = Self(false); |
| 13358 | pub const OWNER: Self = Self(true); |
| 13359 | } |
| 13360 | impl From<GrabOwner> for bool { |
| 13361 | #[inline ] |
| 13362 | fn from(input: GrabOwner) -> Self { |
| 13363 | input.0 |
| 13364 | } |
| 13365 | } |
| 13366 | impl From<GrabOwner> for Option<bool> { |
| 13367 | #[inline ] |
| 13368 | fn from(input: GrabOwner) -> Self { |
| 13369 | Some(input.0) |
| 13370 | } |
| 13371 | } |
| 13372 | impl From<GrabOwner> for u8 { |
| 13373 | #[inline ] |
| 13374 | fn from(input: GrabOwner) -> Self { |
| 13375 | u8::from(input.0) |
| 13376 | } |
| 13377 | } |
| 13378 | impl From<GrabOwner> for Option<u8> { |
| 13379 | #[inline ] |
| 13380 | fn from(input: GrabOwner) -> Self { |
| 13381 | Some(u8::from(input.0)) |
| 13382 | } |
| 13383 | } |
| 13384 | impl From<GrabOwner> for u16 { |
| 13385 | #[inline ] |
| 13386 | fn from(input: GrabOwner) -> Self { |
| 13387 | u16::from(input.0) |
| 13388 | } |
| 13389 | } |
| 13390 | impl From<GrabOwner> for Option<u16> { |
| 13391 | #[inline ] |
| 13392 | fn from(input: GrabOwner) -> Self { |
| 13393 | Some(u16::from(input.0)) |
| 13394 | } |
| 13395 | } |
| 13396 | impl From<GrabOwner> for u32 { |
| 13397 | #[inline ] |
| 13398 | fn from(input: GrabOwner) -> Self { |
| 13399 | u32::from(input.0) |
| 13400 | } |
| 13401 | } |
| 13402 | impl From<GrabOwner> for Option<u32> { |
| 13403 | #[inline ] |
| 13404 | fn from(input: GrabOwner) -> Self { |
| 13405 | Some(u32::from(input.0)) |
| 13406 | } |
| 13407 | } |
| 13408 | impl From<bool> for GrabOwner { |
| 13409 | #[inline ] |
| 13410 | fn from(value: bool) -> Self { |
| 13411 | Self(value) |
| 13412 | } |
| 13413 | } |
| 13414 | impl core::fmt::Debug for GrabOwner { |
| 13415 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 13416 | let variants: [(u32, &'static str, &'static …); 2] = [ |
| 13417 | (Self::NO_OWNER.0.into(), "NO_OWNER" , "NoOwner" ), |
| 13418 | (Self::OWNER.0.into(), "OWNER" , "Owner" ), |
| 13419 | ]; |
| 13420 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 13421 | } |
| 13422 | } |
| 13423 | |
| 13424 | /// Opcode for the XIGrabDevice request |
| 13425 | pub const XI_GRAB_DEVICE_REQUEST: u8 = 51; |
| 13426 | #[derive (Clone, Default)] |
| 13427 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 13428 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13429 | pub struct XIGrabDeviceRequest<'input> { |
| 13430 | pub window: xproto::Window, |
| 13431 | pub time: xproto::Timestamp, |
| 13432 | pub cursor: xproto::Cursor, |
| 13433 | pub deviceid: DeviceId, |
| 13434 | pub mode: xproto::GrabMode, |
| 13435 | pub paired_device_mode: xproto::GrabMode, |
| 13436 | pub owner_events: GrabOwner, |
| 13437 | pub mask: Cow<'input, [u32]>, |
| 13438 | } |
| 13439 | impl_debug_if_no_extra_traits!(XIGrabDeviceRequest<'_>, "XIGrabDeviceRequest" ); |
| 13440 | impl<'input> XIGrabDeviceRequest<'input> { |
| 13441 | /// Serialize this request into bytes for the provided connection |
| 13442 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 13443 | let length_so_far = 0; |
| 13444 | let window_bytes = self.window.serialize(); |
| 13445 | let time_bytes = self.time.serialize(); |
| 13446 | let cursor_bytes = self.cursor.serialize(); |
| 13447 | let deviceid_bytes = self.deviceid.serialize(); |
| 13448 | let mode_bytes = u8::from(self.mode).serialize(); |
| 13449 | let paired_device_mode_bytes = u8::from(self.paired_device_mode).serialize(); |
| 13450 | let owner_events_bytes = bool::from(self.owner_events).serialize(); |
| 13451 | let mask_len = u16::try_from(self.mask.len()).expect("`mask` has too many elements" ); |
| 13452 | let mask_len_bytes = mask_len.serialize(); |
| 13453 | let mut request0 = vec![ |
| 13454 | major_opcode, |
| 13455 | XI_GRAB_DEVICE_REQUEST, |
| 13456 | 0, |
| 13457 | 0, |
| 13458 | window_bytes[0], |
| 13459 | window_bytes[1], |
| 13460 | window_bytes[2], |
| 13461 | window_bytes[3], |
| 13462 | time_bytes[0], |
| 13463 | time_bytes[1], |
| 13464 | time_bytes[2], |
| 13465 | time_bytes[3], |
| 13466 | cursor_bytes[0], |
| 13467 | cursor_bytes[1], |
| 13468 | cursor_bytes[2], |
| 13469 | cursor_bytes[3], |
| 13470 | deviceid_bytes[0], |
| 13471 | deviceid_bytes[1], |
| 13472 | mode_bytes[0], |
| 13473 | paired_device_mode_bytes[0], |
| 13474 | owner_events_bytes[0], |
| 13475 | 0, |
| 13476 | mask_len_bytes[0], |
| 13477 | mask_len_bytes[1], |
| 13478 | ]; |
| 13479 | let length_so_far = length_so_far + request0.len(); |
| 13480 | let mask_bytes = self.mask.serialize(); |
| 13481 | let length_so_far = length_so_far + mask_bytes.len(); |
| 13482 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 13483 | let length_so_far = length_so_far + padding0.len(); |
| 13484 | assert_eq!(length_so_far % 4, 0); |
| 13485 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 13486 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 13487 | ([request0.into(), mask_bytes.into(), padding0.into()], vec![]) |
| 13488 | } |
| 13489 | /// Parse this request given its header, its body, and any fds that go along with it |
| 13490 | #[cfg (feature = "request-parsing" )] |
| 13491 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 13492 | if header.minor_opcode != XI_GRAB_DEVICE_REQUEST { |
| 13493 | return Err(ParseError::InvalidValue); |
| 13494 | } |
| 13495 | let (window, remaining) = xproto::Window::try_parse(value)?; |
| 13496 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 13497 | let (cursor, remaining) = xproto::Cursor::try_parse(remaining)?; |
| 13498 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 13499 | let (mode, remaining) = u8::try_parse(remaining)?; |
| 13500 | let mode = mode.into(); |
| 13501 | let (paired_device_mode, remaining) = u8::try_parse(remaining)?; |
| 13502 | let paired_device_mode = paired_device_mode.into(); |
| 13503 | let (owner_events, remaining) = bool::try_parse(remaining)?; |
| 13504 | let owner_events = owner_events.into(); |
| 13505 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
| 13506 | let (mask_len, remaining) = u16::try_parse(remaining)?; |
| 13507 | let (mask, remaining) = crate::x11_utils::parse_list::<u32>(remaining, mask_len.try_to_usize()?)?; |
| 13508 | let _ = remaining; |
| 13509 | Ok(XIGrabDeviceRequest { |
| 13510 | window, |
| 13511 | time, |
| 13512 | cursor, |
| 13513 | deviceid, |
| 13514 | mode, |
| 13515 | paired_device_mode, |
| 13516 | owner_events, |
| 13517 | mask: Cow::Owned(mask), |
| 13518 | }) |
| 13519 | } |
| 13520 | /// Clone all borrowed data in this XIGrabDeviceRequest. |
| 13521 | pub fn into_owned(self) -> XIGrabDeviceRequest<'static> { |
| 13522 | XIGrabDeviceRequest { |
| 13523 | window: self.window, |
| 13524 | time: self.time, |
| 13525 | cursor: self.cursor, |
| 13526 | deviceid: self.deviceid, |
| 13527 | mode: self.mode, |
| 13528 | paired_device_mode: self.paired_device_mode, |
| 13529 | owner_events: self.owner_events, |
| 13530 | mask: Cow::Owned(self.mask.into_owned()), |
| 13531 | } |
| 13532 | } |
| 13533 | } |
| 13534 | impl<'input> Request for XIGrabDeviceRequest<'input> { |
| 13535 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 13536 | |
| 13537 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 13538 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 13539 | // Flatten the buffers into a single vector |
| 13540 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 13541 | (buf, fds) |
| 13542 | } |
| 13543 | } |
| 13544 | impl<'input> crate::x11_utils::ReplyRequest for XIGrabDeviceRequest<'input> { |
| 13545 | type Reply = XIGrabDeviceReply; |
| 13546 | } |
| 13547 | |
| 13548 | #[derive (Clone, Copy, Default)] |
| 13549 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 13550 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13551 | pub struct XIGrabDeviceReply { |
| 13552 | pub sequence: u16, |
| 13553 | pub length: u32, |
| 13554 | pub status: xproto::GrabStatus, |
| 13555 | } |
| 13556 | impl_debug_if_no_extra_traits!(XIGrabDeviceReply, "XIGrabDeviceReply" ); |
| 13557 | impl TryParse for XIGrabDeviceReply { |
| 13558 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 13559 | let remaining: &[u8] = initial_value; |
| 13560 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 13561 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 13562 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 13563 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 13564 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 13565 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 13566 | if response_type != 1 { |
| 13567 | return Err(ParseError::InvalidValue); |
| 13568 | } |
| 13569 | let status: GrabStatus = status.into(); |
| 13570 | let result: XIGrabDeviceReply = XIGrabDeviceReply { sequence, length, status }; |
| 13571 | let _ = remaining; |
| 13572 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 13573 | .ok_or(err:ParseError::InsufficientData)?; |
| 13574 | Ok((result, remaining)) |
| 13575 | } |
| 13576 | } |
| 13577 | impl Serialize for XIGrabDeviceReply { |
| 13578 | type Bytes = [u8; 32]; |
| 13579 | fn serialize(&self) -> [u8; 32] { |
| 13580 | let response_type_bytes = &[1]; |
| 13581 | let sequence_bytes = self.sequence.serialize(); |
| 13582 | let length_bytes = self.length.serialize(); |
| 13583 | let status_bytes = u8::from(self.status).serialize(); |
| 13584 | [ |
| 13585 | response_type_bytes[0], |
| 13586 | 0, |
| 13587 | sequence_bytes[0], |
| 13588 | sequence_bytes[1], |
| 13589 | length_bytes[0], |
| 13590 | length_bytes[1], |
| 13591 | length_bytes[2], |
| 13592 | length_bytes[3], |
| 13593 | status_bytes[0], |
| 13594 | 0, |
| 13595 | 0, |
| 13596 | 0, |
| 13597 | 0, |
| 13598 | 0, |
| 13599 | 0, |
| 13600 | 0, |
| 13601 | 0, |
| 13602 | 0, |
| 13603 | 0, |
| 13604 | 0, |
| 13605 | 0, |
| 13606 | 0, |
| 13607 | 0, |
| 13608 | 0, |
| 13609 | 0, |
| 13610 | 0, |
| 13611 | 0, |
| 13612 | 0, |
| 13613 | 0, |
| 13614 | 0, |
| 13615 | 0, |
| 13616 | 0, |
| 13617 | ] |
| 13618 | } |
| 13619 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 13620 | bytes.reserve(32); |
| 13621 | let response_type_bytes = &[1]; |
| 13622 | bytes.push(response_type_bytes[0]); |
| 13623 | bytes.extend_from_slice(&[0; 1]); |
| 13624 | self.sequence.serialize_into(bytes); |
| 13625 | self.length.serialize_into(bytes); |
| 13626 | u8::from(self.status).serialize_into(bytes); |
| 13627 | bytes.extend_from_slice(&[0; 23]); |
| 13628 | } |
| 13629 | } |
| 13630 | |
| 13631 | /// Opcode for the XIUngrabDevice request |
| 13632 | pub const XI_UNGRAB_DEVICE_REQUEST: u8 = 52; |
| 13633 | #[derive (Clone, Copy, Default)] |
| 13634 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 13635 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13636 | pub struct XIUngrabDeviceRequest { |
| 13637 | pub time: xproto::Timestamp, |
| 13638 | pub deviceid: DeviceId, |
| 13639 | } |
| 13640 | impl_debug_if_no_extra_traits!(XIUngrabDeviceRequest, "XIUngrabDeviceRequest" ); |
| 13641 | impl XIUngrabDeviceRequest { |
| 13642 | /// Serialize this request into bytes for the provided connection |
| 13643 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 13644 | let length_so_far = 0; |
| 13645 | let time_bytes = self.time.serialize(); |
| 13646 | let deviceid_bytes = self.deviceid.serialize(); |
| 13647 | let mut request0 = vec![ |
| 13648 | major_opcode, |
| 13649 | XI_UNGRAB_DEVICE_REQUEST, |
| 13650 | 0, |
| 13651 | 0, |
| 13652 | time_bytes[0], |
| 13653 | time_bytes[1], |
| 13654 | time_bytes[2], |
| 13655 | time_bytes[3], |
| 13656 | deviceid_bytes[0], |
| 13657 | deviceid_bytes[1], |
| 13658 | 0, |
| 13659 | 0, |
| 13660 | ]; |
| 13661 | let length_so_far = length_so_far + request0.len(); |
| 13662 | assert_eq!(length_so_far % 4, 0); |
| 13663 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 13664 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 13665 | ([request0.into()], vec![]) |
| 13666 | } |
| 13667 | /// Parse this request given its header, its body, and any fds that go along with it |
| 13668 | #[cfg (feature = "request-parsing" )] |
| 13669 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 13670 | if header.minor_opcode != XI_UNGRAB_DEVICE_REQUEST { |
| 13671 | return Err(ParseError::InvalidValue); |
| 13672 | } |
| 13673 | let (time, remaining) = xproto::Timestamp::try_parse(value)?; |
| 13674 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 13675 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 13676 | let _ = remaining; |
| 13677 | Ok(XIUngrabDeviceRequest { |
| 13678 | time, |
| 13679 | deviceid, |
| 13680 | }) |
| 13681 | } |
| 13682 | } |
| 13683 | impl Request for XIUngrabDeviceRequest { |
| 13684 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 13685 | |
| 13686 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 13687 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 13688 | // Flatten the buffers into a single vector |
| 13689 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 13690 | (buf, fds) |
| 13691 | } |
| 13692 | } |
| 13693 | impl crate::x11_utils::VoidRequest for XIUngrabDeviceRequest { |
| 13694 | } |
| 13695 | |
| 13696 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 13697 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13698 | pub struct EventMode(u8); |
| 13699 | impl EventMode { |
| 13700 | pub const ASYNC_DEVICE: Self = Self(0); |
| 13701 | pub const SYNC_DEVICE: Self = Self(1); |
| 13702 | pub const REPLAY_DEVICE: Self = Self(2); |
| 13703 | pub const ASYNC_PAIRED_DEVICE: Self = Self(3); |
| 13704 | pub const ASYNC_PAIR: Self = Self(4); |
| 13705 | pub const SYNC_PAIR: Self = Self(5); |
| 13706 | pub const ACCEPT_TOUCH: Self = Self(6); |
| 13707 | pub const REJECT_TOUCH: Self = Self(7); |
| 13708 | } |
| 13709 | impl From<EventMode> for u8 { |
| 13710 | #[inline ] |
| 13711 | fn from(input: EventMode) -> Self { |
| 13712 | input.0 |
| 13713 | } |
| 13714 | } |
| 13715 | impl From<EventMode> for Option<u8> { |
| 13716 | #[inline ] |
| 13717 | fn from(input: EventMode) -> Self { |
| 13718 | Some(input.0) |
| 13719 | } |
| 13720 | } |
| 13721 | impl From<EventMode> for u16 { |
| 13722 | #[inline ] |
| 13723 | fn from(input: EventMode) -> Self { |
| 13724 | u16::from(input.0) |
| 13725 | } |
| 13726 | } |
| 13727 | impl From<EventMode> for Option<u16> { |
| 13728 | #[inline ] |
| 13729 | fn from(input: EventMode) -> Self { |
| 13730 | Some(u16::from(input.0)) |
| 13731 | } |
| 13732 | } |
| 13733 | impl From<EventMode> for u32 { |
| 13734 | #[inline ] |
| 13735 | fn from(input: EventMode) -> Self { |
| 13736 | u32::from(input.0) |
| 13737 | } |
| 13738 | } |
| 13739 | impl From<EventMode> for Option<u32> { |
| 13740 | #[inline ] |
| 13741 | fn from(input: EventMode) -> Self { |
| 13742 | Some(u32::from(input.0)) |
| 13743 | } |
| 13744 | } |
| 13745 | impl From<u8> for EventMode { |
| 13746 | #[inline ] |
| 13747 | fn from(value: u8) -> Self { |
| 13748 | Self(value) |
| 13749 | } |
| 13750 | } |
| 13751 | impl core::fmt::Debug for EventMode { |
| 13752 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 13753 | let variants: [(u32, &'static str, &'static …); 8] = [ |
| 13754 | (Self::ASYNC_DEVICE.0.into(), "ASYNC_DEVICE" , "AsyncDevice" ), |
| 13755 | (Self::SYNC_DEVICE.0.into(), "SYNC_DEVICE" , "SyncDevice" ), |
| 13756 | (Self::REPLAY_DEVICE.0.into(), "REPLAY_DEVICE" , "ReplayDevice" ), |
| 13757 | (Self::ASYNC_PAIRED_DEVICE.0.into(), "ASYNC_PAIRED_DEVICE" , "AsyncPairedDevice" ), |
| 13758 | (Self::ASYNC_PAIR.0.into(), "ASYNC_PAIR" , "AsyncPair" ), |
| 13759 | (Self::SYNC_PAIR.0.into(), "SYNC_PAIR" , "SyncPair" ), |
| 13760 | (Self::ACCEPT_TOUCH.0.into(), "ACCEPT_TOUCH" , "AcceptTouch" ), |
| 13761 | (Self::REJECT_TOUCH.0.into(), "REJECT_TOUCH" , "RejectTouch" ), |
| 13762 | ]; |
| 13763 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 13764 | } |
| 13765 | } |
| 13766 | |
| 13767 | /// Opcode for the XIAllowEvents request |
| 13768 | pub const XI_ALLOW_EVENTS_REQUEST: u8 = 53; |
| 13769 | #[derive (Clone, Copy, Default)] |
| 13770 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 13771 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13772 | pub struct XIAllowEventsRequest { |
| 13773 | pub time: xproto::Timestamp, |
| 13774 | pub deviceid: DeviceId, |
| 13775 | pub event_mode: EventMode, |
| 13776 | pub touchid: u32, |
| 13777 | pub grab_window: xproto::Window, |
| 13778 | } |
| 13779 | impl_debug_if_no_extra_traits!(XIAllowEventsRequest, "XIAllowEventsRequest" ); |
| 13780 | impl XIAllowEventsRequest { |
| 13781 | /// Serialize this request into bytes for the provided connection |
| 13782 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 13783 | let length_so_far = 0; |
| 13784 | let time_bytes = self.time.serialize(); |
| 13785 | let deviceid_bytes = self.deviceid.serialize(); |
| 13786 | let event_mode_bytes = u8::from(self.event_mode).serialize(); |
| 13787 | let touchid_bytes = self.touchid.serialize(); |
| 13788 | let grab_window_bytes = self.grab_window.serialize(); |
| 13789 | let mut request0 = vec![ |
| 13790 | major_opcode, |
| 13791 | XI_ALLOW_EVENTS_REQUEST, |
| 13792 | 0, |
| 13793 | 0, |
| 13794 | time_bytes[0], |
| 13795 | time_bytes[1], |
| 13796 | time_bytes[2], |
| 13797 | time_bytes[3], |
| 13798 | deviceid_bytes[0], |
| 13799 | deviceid_bytes[1], |
| 13800 | event_mode_bytes[0], |
| 13801 | 0, |
| 13802 | touchid_bytes[0], |
| 13803 | touchid_bytes[1], |
| 13804 | touchid_bytes[2], |
| 13805 | touchid_bytes[3], |
| 13806 | grab_window_bytes[0], |
| 13807 | grab_window_bytes[1], |
| 13808 | grab_window_bytes[2], |
| 13809 | grab_window_bytes[3], |
| 13810 | ]; |
| 13811 | let length_so_far = length_so_far + request0.len(); |
| 13812 | assert_eq!(length_so_far % 4, 0); |
| 13813 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 13814 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 13815 | ([request0.into()], vec![]) |
| 13816 | } |
| 13817 | /// Parse this request given its header, its body, and any fds that go along with it |
| 13818 | #[cfg (feature = "request-parsing" )] |
| 13819 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 13820 | if header.minor_opcode != XI_ALLOW_EVENTS_REQUEST { |
| 13821 | return Err(ParseError::InvalidValue); |
| 13822 | } |
| 13823 | let (time, remaining) = xproto::Timestamp::try_parse(value)?; |
| 13824 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 13825 | let (event_mode, remaining) = u8::try_parse(remaining)?; |
| 13826 | let event_mode = event_mode.into(); |
| 13827 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
| 13828 | let (touchid, remaining) = u32::try_parse(remaining)?; |
| 13829 | let (grab_window, remaining) = xproto::Window::try_parse(remaining)?; |
| 13830 | let _ = remaining; |
| 13831 | Ok(XIAllowEventsRequest { |
| 13832 | time, |
| 13833 | deviceid, |
| 13834 | event_mode, |
| 13835 | touchid, |
| 13836 | grab_window, |
| 13837 | }) |
| 13838 | } |
| 13839 | } |
| 13840 | impl Request for XIAllowEventsRequest { |
| 13841 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 13842 | |
| 13843 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 13844 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 13845 | // Flatten the buffers into a single vector |
| 13846 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 13847 | (buf, fds) |
| 13848 | } |
| 13849 | } |
| 13850 | impl crate::x11_utils::VoidRequest for XIAllowEventsRequest { |
| 13851 | } |
| 13852 | |
| 13853 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 13854 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13855 | pub struct GrabMode22(u8); |
| 13856 | impl GrabMode22 { |
| 13857 | pub const SYNC: Self = Self(0); |
| 13858 | pub const ASYNC: Self = Self(1); |
| 13859 | pub const TOUCH: Self = Self(2); |
| 13860 | } |
| 13861 | impl From<GrabMode22> for u8 { |
| 13862 | #[inline ] |
| 13863 | fn from(input: GrabMode22) -> Self { |
| 13864 | input.0 |
| 13865 | } |
| 13866 | } |
| 13867 | impl From<GrabMode22> for Option<u8> { |
| 13868 | #[inline ] |
| 13869 | fn from(input: GrabMode22) -> Self { |
| 13870 | Some(input.0) |
| 13871 | } |
| 13872 | } |
| 13873 | impl From<GrabMode22> for u16 { |
| 13874 | #[inline ] |
| 13875 | fn from(input: GrabMode22) -> Self { |
| 13876 | u16::from(input.0) |
| 13877 | } |
| 13878 | } |
| 13879 | impl From<GrabMode22> for Option<u16> { |
| 13880 | #[inline ] |
| 13881 | fn from(input: GrabMode22) -> Self { |
| 13882 | Some(u16::from(input.0)) |
| 13883 | } |
| 13884 | } |
| 13885 | impl From<GrabMode22> for u32 { |
| 13886 | #[inline ] |
| 13887 | fn from(input: GrabMode22) -> Self { |
| 13888 | u32::from(input.0) |
| 13889 | } |
| 13890 | } |
| 13891 | impl From<GrabMode22> for Option<u32> { |
| 13892 | #[inline ] |
| 13893 | fn from(input: GrabMode22) -> Self { |
| 13894 | Some(u32::from(input.0)) |
| 13895 | } |
| 13896 | } |
| 13897 | impl From<u8> for GrabMode22 { |
| 13898 | #[inline ] |
| 13899 | fn from(value: u8) -> Self { |
| 13900 | Self(value) |
| 13901 | } |
| 13902 | } |
| 13903 | impl core::fmt::Debug for GrabMode22 { |
| 13904 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 13905 | let variants: [(u32, &'static str, &'static …); 3] = [ |
| 13906 | (Self::SYNC.0.into(), "SYNC" , "Sync" ), |
| 13907 | (Self::ASYNC.0.into(), "ASYNC" , "Async" ), |
| 13908 | (Self::TOUCH.0.into(), "TOUCH" , "Touch" ), |
| 13909 | ]; |
| 13910 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 13911 | } |
| 13912 | } |
| 13913 | |
| 13914 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 13915 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13916 | pub struct GrabType(u8); |
| 13917 | impl GrabType { |
| 13918 | pub const BUTTON: Self = Self(0); |
| 13919 | pub const KEYCODE: Self = Self(1); |
| 13920 | pub const ENTER: Self = Self(2); |
| 13921 | pub const FOCUS_IN: Self = Self(3); |
| 13922 | pub const TOUCH_BEGIN: Self = Self(4); |
| 13923 | pub const GESTURE_PINCH_BEGIN: Self = Self(5); |
| 13924 | pub const GESTURE_SWIPE_BEGIN: Self = Self(6); |
| 13925 | } |
| 13926 | impl From<GrabType> for u8 { |
| 13927 | #[inline ] |
| 13928 | fn from(input: GrabType) -> Self { |
| 13929 | input.0 |
| 13930 | } |
| 13931 | } |
| 13932 | impl From<GrabType> for Option<u8> { |
| 13933 | #[inline ] |
| 13934 | fn from(input: GrabType) -> Self { |
| 13935 | Some(input.0) |
| 13936 | } |
| 13937 | } |
| 13938 | impl From<GrabType> for u16 { |
| 13939 | #[inline ] |
| 13940 | fn from(input: GrabType) -> Self { |
| 13941 | u16::from(input.0) |
| 13942 | } |
| 13943 | } |
| 13944 | impl From<GrabType> for Option<u16> { |
| 13945 | #[inline ] |
| 13946 | fn from(input: GrabType) -> Self { |
| 13947 | Some(u16::from(input.0)) |
| 13948 | } |
| 13949 | } |
| 13950 | impl From<GrabType> for u32 { |
| 13951 | #[inline ] |
| 13952 | fn from(input: GrabType) -> Self { |
| 13953 | u32::from(input.0) |
| 13954 | } |
| 13955 | } |
| 13956 | impl From<GrabType> for Option<u32> { |
| 13957 | #[inline ] |
| 13958 | fn from(input: GrabType) -> Self { |
| 13959 | Some(u32::from(input.0)) |
| 13960 | } |
| 13961 | } |
| 13962 | impl From<u8> for GrabType { |
| 13963 | #[inline ] |
| 13964 | fn from(value: u8) -> Self { |
| 13965 | Self(value) |
| 13966 | } |
| 13967 | } |
| 13968 | impl core::fmt::Debug for GrabType { |
| 13969 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 13970 | let variants: [(u32, &'static str, &'static …); 7] = [ |
| 13971 | (Self::BUTTON.0.into(), "BUTTON" , "Button" ), |
| 13972 | (Self::KEYCODE.0.into(), "KEYCODE" , "Keycode" ), |
| 13973 | (Self::ENTER.0.into(), "ENTER" , "Enter" ), |
| 13974 | (Self::FOCUS_IN.0.into(), "FOCUS_IN" , "FocusIn" ), |
| 13975 | (Self::TOUCH_BEGIN.0.into(), "TOUCH_BEGIN" , "TouchBegin" ), |
| 13976 | (Self::GESTURE_PINCH_BEGIN.0.into(), "GESTURE_PINCH_BEGIN" , "GesturePinchBegin" ), |
| 13977 | (Self::GESTURE_SWIPE_BEGIN.0.into(), "GESTURE_SWIPE_BEGIN" , "GestureSwipeBegin" ), |
| 13978 | ]; |
| 13979 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 13980 | } |
| 13981 | } |
| 13982 | |
| 13983 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 13984 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 13985 | pub struct ModifierMask(u32); |
| 13986 | impl ModifierMask { |
| 13987 | pub const ANY: Self = Self(1 << 31); |
| 13988 | } |
| 13989 | impl From<ModifierMask> for u32 { |
| 13990 | #[inline ] |
| 13991 | fn from(input: ModifierMask) -> Self { |
| 13992 | input.0 |
| 13993 | } |
| 13994 | } |
| 13995 | impl From<ModifierMask> for Option<u32> { |
| 13996 | #[inline ] |
| 13997 | fn from(input: ModifierMask) -> Self { |
| 13998 | Some(input.0) |
| 13999 | } |
| 14000 | } |
| 14001 | impl From<u8> for ModifierMask { |
| 14002 | #[inline ] |
| 14003 | fn from(value: u8) -> Self { |
| 14004 | Self(value.into()) |
| 14005 | } |
| 14006 | } |
| 14007 | impl From<u16> for ModifierMask { |
| 14008 | #[inline ] |
| 14009 | fn from(value: u16) -> Self { |
| 14010 | Self(value.into()) |
| 14011 | } |
| 14012 | } |
| 14013 | impl From<u32> for ModifierMask { |
| 14014 | #[inline ] |
| 14015 | fn from(value: u32) -> Self { |
| 14016 | Self(value) |
| 14017 | } |
| 14018 | } |
| 14019 | impl core::fmt::Debug for ModifierMask { |
| 14020 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 14021 | let variants: [(u32, &'static str, &'static …); 1] = [ |
| 14022 | (Self::ANY.0, "ANY" , "Any" ), |
| 14023 | ]; |
| 14024 | pretty_print_bitmask(fmt, self.0, &variants) |
| 14025 | } |
| 14026 | } |
| 14027 | bitmask_binop!(ModifierMask, u32); |
| 14028 | |
| 14029 | #[derive (Clone, Copy, Default)] |
| 14030 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 14031 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 14032 | pub struct GrabModifierInfo { |
| 14033 | pub modifiers: u32, |
| 14034 | pub status: xproto::GrabStatus, |
| 14035 | } |
| 14036 | impl_debug_if_no_extra_traits!(GrabModifierInfo, "GrabModifierInfo" ); |
| 14037 | impl TryParse for GrabModifierInfo { |
| 14038 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 14039 | let (modifiers: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 14040 | let (status: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 14041 | let remaining: &[u8] = remaining.get(3..).ok_or(err:ParseError::InsufficientData)?; |
| 14042 | let status: GrabStatus = status.into(); |
| 14043 | let result: GrabModifierInfo = GrabModifierInfo { modifiers, status }; |
| 14044 | Ok((result, remaining)) |
| 14045 | } |
| 14046 | } |
| 14047 | impl Serialize for GrabModifierInfo { |
| 14048 | type Bytes = [u8; 8]; |
| 14049 | fn serialize(&self) -> [u8; 8] { |
| 14050 | let modifiers_bytes: [u8; 4] = self.modifiers.serialize(); |
| 14051 | let status_bytes: [u8; 1] = u8::from(self.status).serialize(); |
| 14052 | [ |
| 14053 | modifiers_bytes[0], |
| 14054 | modifiers_bytes[1], |
| 14055 | modifiers_bytes[2], |
| 14056 | modifiers_bytes[3], |
| 14057 | status_bytes[0], |
| 14058 | 0, |
| 14059 | 0, |
| 14060 | 0, |
| 14061 | ] |
| 14062 | } |
| 14063 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 14064 | bytes.reserve(additional:8); |
| 14065 | self.modifiers.serialize_into(bytes); |
| 14066 | u8::from(self.status).serialize_into(bytes); |
| 14067 | bytes.extend_from_slice(&[0; 3]); |
| 14068 | } |
| 14069 | } |
| 14070 | |
| 14071 | /// Opcode for the XIPassiveGrabDevice request |
| 14072 | pub const XI_PASSIVE_GRAB_DEVICE_REQUEST: u8 = 54; |
| 14073 | #[derive (Clone, Default)] |
| 14074 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 14075 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 14076 | pub struct XIPassiveGrabDeviceRequest<'input> { |
| 14077 | pub time: xproto::Timestamp, |
| 14078 | pub grab_window: xproto::Window, |
| 14079 | pub cursor: xproto::Cursor, |
| 14080 | pub detail: u32, |
| 14081 | pub deviceid: DeviceId, |
| 14082 | pub grab_type: GrabType, |
| 14083 | pub grab_mode: GrabMode22, |
| 14084 | pub paired_device_mode: xproto::GrabMode, |
| 14085 | pub owner_events: GrabOwner, |
| 14086 | pub mask: Cow<'input, [u32]>, |
| 14087 | pub modifiers: Cow<'input, [u32]>, |
| 14088 | } |
| 14089 | impl_debug_if_no_extra_traits!(XIPassiveGrabDeviceRequest<'_>, "XIPassiveGrabDeviceRequest" ); |
| 14090 | impl<'input> XIPassiveGrabDeviceRequest<'input> { |
| 14091 | /// Serialize this request into bytes for the provided connection |
| 14092 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 4]> { |
| 14093 | let length_so_far = 0; |
| 14094 | let time_bytes = self.time.serialize(); |
| 14095 | let grab_window_bytes = self.grab_window.serialize(); |
| 14096 | let cursor_bytes = self.cursor.serialize(); |
| 14097 | let detail_bytes = self.detail.serialize(); |
| 14098 | let deviceid_bytes = self.deviceid.serialize(); |
| 14099 | let num_modifiers = u16::try_from(self.modifiers.len()).expect("`modifiers` has too many elements" ); |
| 14100 | let num_modifiers_bytes = num_modifiers.serialize(); |
| 14101 | let mask_len = u16::try_from(self.mask.len()).expect("`mask` has too many elements" ); |
| 14102 | let mask_len_bytes = mask_len.serialize(); |
| 14103 | let grab_type_bytes = u8::from(self.grab_type).serialize(); |
| 14104 | let grab_mode_bytes = u8::from(self.grab_mode).serialize(); |
| 14105 | let paired_device_mode_bytes = u8::from(self.paired_device_mode).serialize(); |
| 14106 | let owner_events_bytes = bool::from(self.owner_events).serialize(); |
| 14107 | let mut request0 = vec![ |
| 14108 | major_opcode, |
| 14109 | XI_PASSIVE_GRAB_DEVICE_REQUEST, |
| 14110 | 0, |
| 14111 | 0, |
| 14112 | time_bytes[0], |
| 14113 | time_bytes[1], |
| 14114 | time_bytes[2], |
| 14115 | time_bytes[3], |
| 14116 | grab_window_bytes[0], |
| 14117 | grab_window_bytes[1], |
| 14118 | grab_window_bytes[2], |
| 14119 | grab_window_bytes[3], |
| 14120 | cursor_bytes[0], |
| 14121 | cursor_bytes[1], |
| 14122 | cursor_bytes[2], |
| 14123 | cursor_bytes[3], |
| 14124 | detail_bytes[0], |
| 14125 | detail_bytes[1], |
| 14126 | detail_bytes[2], |
| 14127 | detail_bytes[3], |
| 14128 | deviceid_bytes[0], |
| 14129 | deviceid_bytes[1], |
| 14130 | num_modifiers_bytes[0], |
| 14131 | num_modifiers_bytes[1], |
| 14132 | mask_len_bytes[0], |
| 14133 | mask_len_bytes[1], |
| 14134 | grab_type_bytes[0], |
| 14135 | grab_mode_bytes[0], |
| 14136 | paired_device_mode_bytes[0], |
| 14137 | owner_events_bytes[0], |
| 14138 | 0, |
| 14139 | 0, |
| 14140 | ]; |
| 14141 | let length_so_far = length_so_far + request0.len(); |
| 14142 | let mask_bytes = self.mask.serialize(); |
| 14143 | let length_so_far = length_so_far + mask_bytes.len(); |
| 14144 | let modifiers_bytes = self.modifiers.serialize(); |
| 14145 | let length_so_far = length_so_far + modifiers_bytes.len(); |
| 14146 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 14147 | let length_so_far = length_so_far + padding0.len(); |
| 14148 | assert_eq!(length_so_far % 4, 0); |
| 14149 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 14150 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 14151 | ([request0.into(), mask_bytes.into(), modifiers_bytes.into(), padding0.into()], vec![]) |
| 14152 | } |
| 14153 | /// Parse this request given its header, its body, and any fds that go along with it |
| 14154 | #[cfg (feature = "request-parsing" )] |
| 14155 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 14156 | if header.minor_opcode != XI_PASSIVE_GRAB_DEVICE_REQUEST { |
| 14157 | return Err(ParseError::InvalidValue); |
| 14158 | } |
| 14159 | let (time, remaining) = xproto::Timestamp::try_parse(value)?; |
| 14160 | let (grab_window, remaining) = xproto::Window::try_parse(remaining)?; |
| 14161 | let (cursor, remaining) = xproto::Cursor::try_parse(remaining)?; |
| 14162 | let (detail, remaining) = u32::try_parse(remaining)?; |
| 14163 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 14164 | let (num_modifiers, remaining) = u16::try_parse(remaining)?; |
| 14165 | let (mask_len, remaining) = u16::try_parse(remaining)?; |
| 14166 | let (grab_type, remaining) = u8::try_parse(remaining)?; |
| 14167 | let grab_type = grab_type.into(); |
| 14168 | let (grab_mode, remaining) = u8::try_parse(remaining)?; |
| 14169 | let grab_mode = grab_mode.into(); |
| 14170 | let (paired_device_mode, remaining) = u8::try_parse(remaining)?; |
| 14171 | let paired_device_mode = paired_device_mode.into(); |
| 14172 | let (owner_events, remaining) = bool::try_parse(remaining)?; |
| 14173 | let owner_events = owner_events.into(); |
| 14174 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 14175 | let (mask, remaining) = crate::x11_utils::parse_list::<u32>(remaining, mask_len.try_to_usize()?)?; |
| 14176 | let (modifiers, remaining) = crate::x11_utils::parse_list::<u32>(remaining, num_modifiers.try_to_usize()?)?; |
| 14177 | let _ = remaining; |
| 14178 | Ok(XIPassiveGrabDeviceRequest { |
| 14179 | time, |
| 14180 | grab_window, |
| 14181 | cursor, |
| 14182 | detail, |
| 14183 | deviceid, |
| 14184 | grab_type, |
| 14185 | grab_mode, |
| 14186 | paired_device_mode, |
| 14187 | owner_events, |
| 14188 | mask: Cow::Owned(mask), |
| 14189 | modifiers: Cow::Owned(modifiers), |
| 14190 | }) |
| 14191 | } |
| 14192 | /// Clone all borrowed data in this XIPassiveGrabDeviceRequest. |
| 14193 | pub fn into_owned(self) -> XIPassiveGrabDeviceRequest<'static> { |
| 14194 | XIPassiveGrabDeviceRequest { |
| 14195 | time: self.time, |
| 14196 | grab_window: self.grab_window, |
| 14197 | cursor: self.cursor, |
| 14198 | detail: self.detail, |
| 14199 | deviceid: self.deviceid, |
| 14200 | grab_type: self.grab_type, |
| 14201 | grab_mode: self.grab_mode, |
| 14202 | paired_device_mode: self.paired_device_mode, |
| 14203 | owner_events: self.owner_events, |
| 14204 | mask: Cow::Owned(self.mask.into_owned()), |
| 14205 | modifiers: Cow::Owned(self.modifiers.into_owned()), |
| 14206 | } |
| 14207 | } |
| 14208 | } |
| 14209 | impl<'input> Request for XIPassiveGrabDeviceRequest<'input> { |
| 14210 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 14211 | |
| 14212 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 14213 | let (bufs: [Cow<'_, [u8]>; 4], fds: Vec) = self.serialize(major_opcode); |
| 14214 | // Flatten the buffers into a single vector |
| 14215 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 14216 | (buf, fds) |
| 14217 | } |
| 14218 | } |
| 14219 | impl<'input> crate::x11_utils::ReplyRequest for XIPassiveGrabDeviceRequest<'input> { |
| 14220 | type Reply = XIPassiveGrabDeviceReply; |
| 14221 | } |
| 14222 | |
| 14223 | #[derive (Clone, Default)] |
| 14224 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 14225 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 14226 | pub struct XIPassiveGrabDeviceReply { |
| 14227 | pub sequence: u16, |
| 14228 | pub length: u32, |
| 14229 | pub modifiers: Vec<GrabModifierInfo>, |
| 14230 | } |
| 14231 | impl_debug_if_no_extra_traits!(XIPassiveGrabDeviceReply, "XIPassiveGrabDeviceReply" ); |
| 14232 | impl TryParse for XIPassiveGrabDeviceReply { |
| 14233 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 14234 | let remaining: &[u8] = initial_value; |
| 14235 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 14236 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 14237 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 14238 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 14239 | let (num_modifiers: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 14240 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
| 14241 | let (modifiers: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<GrabModifierInfo>(data:remaining, list_length:num_modifiers.try_to_usize()?)?; |
| 14242 | if response_type != 1 { |
| 14243 | return Err(ParseError::InvalidValue); |
| 14244 | } |
| 14245 | let result: XIPassiveGrabDeviceReply = XIPassiveGrabDeviceReply { sequence, length, modifiers }; |
| 14246 | let _ = remaining; |
| 14247 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 14248 | .ok_or(err:ParseError::InsufficientData)?; |
| 14249 | Ok((result, remaining)) |
| 14250 | } |
| 14251 | } |
| 14252 | impl Serialize for XIPassiveGrabDeviceReply { |
| 14253 | type Bytes = Vec<u8>; |
| 14254 | fn serialize(&self) -> Vec<u8> { |
| 14255 | let mut result: Vec = Vec::new(); |
| 14256 | self.serialize_into(&mut result); |
| 14257 | result |
| 14258 | } |
| 14259 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 14260 | bytes.reserve(additional:32); |
| 14261 | let response_type_bytes: &[u8; 1] = &[1]; |
| 14262 | bytes.push(response_type_bytes[0]); |
| 14263 | bytes.extend_from_slice(&[0; 1]); |
| 14264 | self.sequence.serialize_into(bytes); |
| 14265 | self.length.serialize_into(bytes); |
| 14266 | let num_modifiers: u16 = u16::try_from(self.modifiers.len()).expect(msg:"`modifiers` has too many elements" ); |
| 14267 | num_modifiers.serialize_into(bytes); |
| 14268 | bytes.extend_from_slice(&[0; 22]); |
| 14269 | self.modifiers.serialize_into(bytes); |
| 14270 | } |
| 14271 | } |
| 14272 | impl XIPassiveGrabDeviceReply { |
| 14273 | /// Get the value of the `num_modifiers` field. |
| 14274 | /// |
| 14275 | /// The `num_modifiers` field is used as the length field of the `modifiers` field. |
| 14276 | /// This function computes the field's value again based on the length of the list. |
| 14277 | /// |
| 14278 | /// # Panics |
| 14279 | /// |
| 14280 | /// Panics if the value cannot be represented in the target type. This |
| 14281 | /// cannot happen with values of the struct received from the X11 server. |
| 14282 | pub fn num_modifiers(&self) -> u16 { |
| 14283 | self.modifiers.len() |
| 14284 | .try_into().unwrap() |
| 14285 | } |
| 14286 | } |
| 14287 | |
| 14288 | /// Opcode for the XIPassiveUngrabDevice request |
| 14289 | pub const XI_PASSIVE_UNGRAB_DEVICE_REQUEST: u8 = 55; |
| 14290 | #[derive (Clone, Default)] |
| 14291 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 14292 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 14293 | pub struct XIPassiveUngrabDeviceRequest<'input> { |
| 14294 | pub grab_window: xproto::Window, |
| 14295 | pub detail: u32, |
| 14296 | pub deviceid: DeviceId, |
| 14297 | pub grab_type: GrabType, |
| 14298 | pub modifiers: Cow<'input, [u32]>, |
| 14299 | } |
| 14300 | impl_debug_if_no_extra_traits!(XIPassiveUngrabDeviceRequest<'_>, "XIPassiveUngrabDeviceRequest" ); |
| 14301 | impl<'input> XIPassiveUngrabDeviceRequest<'input> { |
| 14302 | /// Serialize this request into bytes for the provided connection |
| 14303 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 14304 | let length_so_far = 0; |
| 14305 | let grab_window_bytes = self.grab_window.serialize(); |
| 14306 | let detail_bytes = self.detail.serialize(); |
| 14307 | let deviceid_bytes = self.deviceid.serialize(); |
| 14308 | let num_modifiers = u16::try_from(self.modifiers.len()).expect("`modifiers` has too many elements" ); |
| 14309 | let num_modifiers_bytes = num_modifiers.serialize(); |
| 14310 | let grab_type_bytes = u8::from(self.grab_type).serialize(); |
| 14311 | let mut request0 = vec![ |
| 14312 | major_opcode, |
| 14313 | XI_PASSIVE_UNGRAB_DEVICE_REQUEST, |
| 14314 | 0, |
| 14315 | 0, |
| 14316 | grab_window_bytes[0], |
| 14317 | grab_window_bytes[1], |
| 14318 | grab_window_bytes[2], |
| 14319 | grab_window_bytes[3], |
| 14320 | detail_bytes[0], |
| 14321 | detail_bytes[1], |
| 14322 | detail_bytes[2], |
| 14323 | detail_bytes[3], |
| 14324 | deviceid_bytes[0], |
| 14325 | deviceid_bytes[1], |
| 14326 | num_modifiers_bytes[0], |
| 14327 | num_modifiers_bytes[1], |
| 14328 | grab_type_bytes[0], |
| 14329 | 0, |
| 14330 | 0, |
| 14331 | 0, |
| 14332 | ]; |
| 14333 | let length_so_far = length_so_far + request0.len(); |
| 14334 | let modifiers_bytes = self.modifiers.serialize(); |
| 14335 | let length_so_far = length_so_far + modifiers_bytes.len(); |
| 14336 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 14337 | let length_so_far = length_so_far + padding0.len(); |
| 14338 | assert_eq!(length_so_far % 4, 0); |
| 14339 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 14340 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 14341 | ([request0.into(), modifiers_bytes.into(), padding0.into()], vec![]) |
| 14342 | } |
| 14343 | /// Parse this request given its header, its body, and any fds that go along with it |
| 14344 | #[cfg (feature = "request-parsing" )] |
| 14345 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 14346 | if header.minor_opcode != XI_PASSIVE_UNGRAB_DEVICE_REQUEST { |
| 14347 | return Err(ParseError::InvalidValue); |
| 14348 | } |
| 14349 | let (grab_window, remaining) = xproto::Window::try_parse(value)?; |
| 14350 | let (detail, remaining) = u32::try_parse(remaining)?; |
| 14351 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 14352 | let (num_modifiers, remaining) = u16::try_parse(remaining)?; |
| 14353 | let (grab_type, remaining) = u8::try_parse(remaining)?; |
| 14354 | let grab_type = grab_type.into(); |
| 14355 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 14356 | let (modifiers, remaining) = crate::x11_utils::parse_list::<u32>(remaining, num_modifiers.try_to_usize()?)?; |
| 14357 | let _ = remaining; |
| 14358 | Ok(XIPassiveUngrabDeviceRequest { |
| 14359 | grab_window, |
| 14360 | detail, |
| 14361 | deviceid, |
| 14362 | grab_type, |
| 14363 | modifiers: Cow::Owned(modifiers), |
| 14364 | }) |
| 14365 | } |
| 14366 | /// Clone all borrowed data in this XIPassiveUngrabDeviceRequest. |
| 14367 | pub fn into_owned(self) -> XIPassiveUngrabDeviceRequest<'static> { |
| 14368 | XIPassiveUngrabDeviceRequest { |
| 14369 | grab_window: self.grab_window, |
| 14370 | detail: self.detail, |
| 14371 | deviceid: self.deviceid, |
| 14372 | grab_type: self.grab_type, |
| 14373 | modifiers: Cow::Owned(self.modifiers.into_owned()), |
| 14374 | } |
| 14375 | } |
| 14376 | } |
| 14377 | impl<'input> Request for XIPassiveUngrabDeviceRequest<'input> { |
| 14378 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 14379 | |
| 14380 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 14381 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 14382 | // Flatten the buffers into a single vector |
| 14383 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 14384 | (buf, fds) |
| 14385 | } |
| 14386 | } |
| 14387 | impl<'input> crate::x11_utils::VoidRequest for XIPassiveUngrabDeviceRequest<'input> { |
| 14388 | } |
| 14389 | |
| 14390 | /// Opcode for the XIListProperties request |
| 14391 | pub const XI_LIST_PROPERTIES_REQUEST: u8 = 56; |
| 14392 | #[derive (Clone, Copy, Default)] |
| 14393 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 14394 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 14395 | pub struct XIListPropertiesRequest { |
| 14396 | pub deviceid: DeviceId, |
| 14397 | } |
| 14398 | impl_debug_if_no_extra_traits!(XIListPropertiesRequest, "XIListPropertiesRequest" ); |
| 14399 | impl XIListPropertiesRequest { |
| 14400 | /// Serialize this request into bytes for the provided connection |
| 14401 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 14402 | let length_so_far = 0; |
| 14403 | let deviceid_bytes = self.deviceid.serialize(); |
| 14404 | let mut request0 = vec![ |
| 14405 | major_opcode, |
| 14406 | XI_LIST_PROPERTIES_REQUEST, |
| 14407 | 0, |
| 14408 | 0, |
| 14409 | deviceid_bytes[0], |
| 14410 | deviceid_bytes[1], |
| 14411 | 0, |
| 14412 | 0, |
| 14413 | ]; |
| 14414 | let length_so_far = length_so_far + request0.len(); |
| 14415 | assert_eq!(length_so_far % 4, 0); |
| 14416 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 14417 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 14418 | ([request0.into()], vec![]) |
| 14419 | } |
| 14420 | /// Parse this request given its header, its body, and any fds that go along with it |
| 14421 | #[cfg (feature = "request-parsing" )] |
| 14422 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 14423 | if header.minor_opcode != XI_LIST_PROPERTIES_REQUEST { |
| 14424 | return Err(ParseError::InvalidValue); |
| 14425 | } |
| 14426 | let (deviceid, remaining) = DeviceId::try_parse(value)?; |
| 14427 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 14428 | let _ = remaining; |
| 14429 | Ok(XIListPropertiesRequest { |
| 14430 | deviceid, |
| 14431 | }) |
| 14432 | } |
| 14433 | } |
| 14434 | impl Request for XIListPropertiesRequest { |
| 14435 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 14436 | |
| 14437 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 14438 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 14439 | // Flatten the buffers into a single vector |
| 14440 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 14441 | (buf, fds) |
| 14442 | } |
| 14443 | } |
| 14444 | impl crate::x11_utils::ReplyRequest for XIListPropertiesRequest { |
| 14445 | type Reply = XIListPropertiesReply; |
| 14446 | } |
| 14447 | |
| 14448 | #[derive (Clone, Default)] |
| 14449 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 14450 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 14451 | pub struct XIListPropertiesReply { |
| 14452 | pub sequence: u16, |
| 14453 | pub length: u32, |
| 14454 | pub properties: Vec<xproto::Atom>, |
| 14455 | } |
| 14456 | impl_debug_if_no_extra_traits!(XIListPropertiesReply, "XIListPropertiesReply" ); |
| 14457 | impl TryParse for XIListPropertiesReply { |
| 14458 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 14459 | let remaining: &[u8] = initial_value; |
| 14460 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 14461 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 14462 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 14463 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 14464 | let (num_properties: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 14465 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
| 14466 | let (properties: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<xproto::Atom>(data:remaining, list_length:num_properties.try_to_usize()?)?; |
| 14467 | if response_type != 1 { |
| 14468 | return Err(ParseError::InvalidValue); |
| 14469 | } |
| 14470 | let result: XIListPropertiesReply = XIListPropertiesReply { sequence, length, properties }; |
| 14471 | let _ = remaining; |
| 14472 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 14473 | .ok_or(err:ParseError::InsufficientData)?; |
| 14474 | Ok((result, remaining)) |
| 14475 | } |
| 14476 | } |
| 14477 | impl Serialize for XIListPropertiesReply { |
| 14478 | type Bytes = Vec<u8>; |
| 14479 | fn serialize(&self) -> Vec<u8> { |
| 14480 | let mut result: Vec = Vec::new(); |
| 14481 | self.serialize_into(&mut result); |
| 14482 | result |
| 14483 | } |
| 14484 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 14485 | bytes.reserve(additional:32); |
| 14486 | let response_type_bytes: &[u8; 1] = &[1]; |
| 14487 | bytes.push(response_type_bytes[0]); |
| 14488 | bytes.extend_from_slice(&[0; 1]); |
| 14489 | self.sequence.serialize_into(bytes); |
| 14490 | self.length.serialize_into(bytes); |
| 14491 | let num_properties: u16 = u16::try_from(self.properties.len()).expect(msg:"`properties` has too many elements" ); |
| 14492 | num_properties.serialize_into(bytes); |
| 14493 | bytes.extend_from_slice(&[0; 22]); |
| 14494 | self.properties.serialize_into(bytes); |
| 14495 | } |
| 14496 | } |
| 14497 | impl XIListPropertiesReply { |
| 14498 | /// Get the value of the `num_properties` field. |
| 14499 | /// |
| 14500 | /// The `num_properties` field is used as the length field of the `properties` field. |
| 14501 | /// This function computes the field's value again based on the length of the list. |
| 14502 | /// |
| 14503 | /// # Panics |
| 14504 | /// |
| 14505 | /// Panics if the value cannot be represented in the target type. This |
| 14506 | /// cannot happen with values of the struct received from the X11 server. |
| 14507 | pub fn num_properties(&self) -> u16 { |
| 14508 | self.properties.len() |
| 14509 | .try_into().unwrap() |
| 14510 | } |
| 14511 | } |
| 14512 | |
| 14513 | #[derive (Clone)] |
| 14514 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 14515 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 14516 | pub enum XIChangePropertyAux { |
| 14517 | Data8(Vec<u8>), |
| 14518 | Data16(Vec<u16>), |
| 14519 | Data32(Vec<u32>), |
| 14520 | /// This variant is returned when the server sends a discriminant |
| 14521 | /// value that does not match any of the defined by the protocol. |
| 14522 | /// |
| 14523 | /// Usually, this should be considered a parsing error, but there |
| 14524 | /// are some cases where the server violates the protocol. |
| 14525 | /// |
| 14526 | /// Trying to use `serialize` or `serialize_into` with this variant |
| 14527 | /// will raise a panic. |
| 14528 | InvalidValue(u8), |
| 14529 | } |
| 14530 | impl_debug_if_no_extra_traits!(XIChangePropertyAux, "XIChangePropertyAux" ); |
| 14531 | impl XIChangePropertyAux { |
| 14532 | #[cfg_attr (not(feature = "request-parsing" ), allow(dead_code))] |
| 14533 | fn try_parse(value: &[u8], format: u8, num_items: u32) -> Result<(Self, &[u8]), ParseError> { |
| 14534 | let switch_expr = u8::from(format); |
| 14535 | let mut outer_remaining = value; |
| 14536 | let mut parse_result = None; |
| 14537 | if switch_expr == u8::from(PropertyFormat::M8_BITS) { |
| 14538 | let remaining = outer_remaining; |
| 14539 | let value = remaining; |
| 14540 | let (data8, remaining) = crate::x11_utils::parse_u8_list(remaining, num_items.try_to_usize()?)?; |
| 14541 | let data8 = data8.to_vec(); |
| 14542 | // Align offset to multiple of 4 |
| 14543 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 14544 | let misalignment = (4 - (offset % 4)) % 4; |
| 14545 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
| 14546 | outer_remaining = remaining; |
| 14547 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 14548 | parse_result = Some(XIChangePropertyAux::Data8(data8)); |
| 14549 | } |
| 14550 | if switch_expr == u8::from(PropertyFormat::M16_BITS) { |
| 14551 | let remaining = outer_remaining; |
| 14552 | let value = remaining; |
| 14553 | let (data16, remaining) = crate::x11_utils::parse_list::<u16>(remaining, num_items.try_to_usize()?)?; |
| 14554 | // Align offset to multiple of 4 |
| 14555 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 14556 | let misalignment = (4 - (offset % 4)) % 4; |
| 14557 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
| 14558 | outer_remaining = remaining; |
| 14559 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 14560 | parse_result = Some(XIChangePropertyAux::Data16(data16)); |
| 14561 | } |
| 14562 | if switch_expr == u8::from(PropertyFormat::M32_BITS) { |
| 14563 | let remaining = outer_remaining; |
| 14564 | let (data32, remaining) = crate::x11_utils::parse_list::<u32>(remaining, num_items.try_to_usize()?)?; |
| 14565 | outer_remaining = remaining; |
| 14566 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 14567 | parse_result = Some(XIChangePropertyAux::Data32(data32)); |
| 14568 | } |
| 14569 | match parse_result { |
| 14570 | None => Ok((XIChangePropertyAux::InvalidValue(switch_expr), &[])), |
| 14571 | Some(result) => Ok((result, outer_remaining)), |
| 14572 | } |
| 14573 | } |
| 14574 | } |
| 14575 | impl XIChangePropertyAux { |
| 14576 | pub fn as_data8(&self) -> Option<&Vec<u8>> { |
| 14577 | match self { |
| 14578 | XIChangePropertyAux::Data8(value: &Vec) => Some(value), |
| 14579 | _ => None, |
| 14580 | } |
| 14581 | } |
| 14582 | pub fn as_data16(&self) -> Option<&Vec<u16>> { |
| 14583 | match self { |
| 14584 | XIChangePropertyAux::Data16(value: &Vec) => Some(value), |
| 14585 | _ => None, |
| 14586 | } |
| 14587 | } |
| 14588 | pub fn as_data32(&self) -> Option<&Vec<u32>> { |
| 14589 | match self { |
| 14590 | XIChangePropertyAux::Data32(value: &Vec) => Some(value), |
| 14591 | _ => None, |
| 14592 | } |
| 14593 | } |
| 14594 | } |
| 14595 | impl XIChangePropertyAux { |
| 14596 | #[allow (dead_code)] |
| 14597 | fn serialize(&self, format: u8, num_items: u32) -> Vec<u8> { |
| 14598 | let mut result = Vec::new(); |
| 14599 | self.serialize_into(&mut result, u8::from(format), u32::from(num_items)); |
| 14600 | result |
| 14601 | } |
| 14602 | fn serialize_into(&self, bytes: &mut Vec<u8>, format: u8, num_items: u32) { |
| 14603 | assert_eq!(self.switch_expr(), u8::from(format), "switch `items` has an inconsistent discriminant" ); |
| 14604 | match self { |
| 14605 | XIChangePropertyAux::Data8(data8) => { |
| 14606 | assert_eq!(data8.len(), usize::try_from(num_items).unwrap(), "`data8` has an incorrect length" ); |
| 14607 | bytes.extend_from_slice(&data8); |
| 14608 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 14609 | } |
| 14610 | XIChangePropertyAux::Data16(data16) => { |
| 14611 | assert_eq!(data16.len(), usize::try_from(num_items).unwrap(), "`data16` has an incorrect length" ); |
| 14612 | data16.serialize_into(bytes); |
| 14613 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 14614 | } |
| 14615 | XIChangePropertyAux::Data32(data32) => { |
| 14616 | assert_eq!(data32.len(), usize::try_from(num_items).unwrap(), "`data32` has an incorrect length" ); |
| 14617 | data32.serialize_into(bytes); |
| 14618 | } |
| 14619 | XIChangePropertyAux::InvalidValue(_) => panic!("attempted to serialize invalid switch case" ), |
| 14620 | } |
| 14621 | } |
| 14622 | } |
| 14623 | impl XIChangePropertyAux { |
| 14624 | fn switch_expr(&self) -> u8 { |
| 14625 | match self { |
| 14626 | XIChangePropertyAux::Data8(_) => u8::from(PropertyFormat::M8_BITS), |
| 14627 | XIChangePropertyAux::Data16(_) => u8::from(PropertyFormat::M16_BITS), |
| 14628 | XIChangePropertyAux::Data32(_) => u8::from(PropertyFormat::M32_BITS), |
| 14629 | XIChangePropertyAux::InvalidValue(switch_expr: &u8) => *switch_expr, |
| 14630 | } |
| 14631 | } |
| 14632 | } |
| 14633 | |
| 14634 | /// Opcode for the XIChangeProperty request |
| 14635 | pub const XI_CHANGE_PROPERTY_REQUEST: u8 = 57; |
| 14636 | #[derive (Clone)] |
| 14637 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 14638 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 14639 | pub struct XIChangePropertyRequest<'input> { |
| 14640 | pub deviceid: DeviceId, |
| 14641 | pub mode: xproto::PropMode, |
| 14642 | pub property: xproto::Atom, |
| 14643 | pub type_: xproto::Atom, |
| 14644 | pub num_items: u32, |
| 14645 | pub items: Cow<'input, XIChangePropertyAux>, |
| 14646 | } |
| 14647 | impl_debug_if_no_extra_traits!(XIChangePropertyRequest<'_>, "XIChangePropertyRequest" ); |
| 14648 | impl<'input> XIChangePropertyRequest<'input> { |
| 14649 | /// Serialize this request into bytes for the provided connection |
| 14650 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 14651 | let length_so_far = 0; |
| 14652 | let deviceid_bytes = self.deviceid.serialize(); |
| 14653 | let mode_bytes = u8::from(self.mode).serialize(); |
| 14654 | let format: u8 = self.items.switch_expr(); |
| 14655 | let format_bytes = format.serialize(); |
| 14656 | let property_bytes = self.property.serialize(); |
| 14657 | let type_bytes = self.type_.serialize(); |
| 14658 | let num_items_bytes = self.num_items.serialize(); |
| 14659 | let mut request0 = vec![ |
| 14660 | major_opcode, |
| 14661 | XI_CHANGE_PROPERTY_REQUEST, |
| 14662 | 0, |
| 14663 | 0, |
| 14664 | deviceid_bytes[0], |
| 14665 | deviceid_bytes[1], |
| 14666 | mode_bytes[0], |
| 14667 | format_bytes[0], |
| 14668 | property_bytes[0], |
| 14669 | property_bytes[1], |
| 14670 | property_bytes[2], |
| 14671 | property_bytes[3], |
| 14672 | type_bytes[0], |
| 14673 | type_bytes[1], |
| 14674 | type_bytes[2], |
| 14675 | type_bytes[3], |
| 14676 | num_items_bytes[0], |
| 14677 | num_items_bytes[1], |
| 14678 | num_items_bytes[2], |
| 14679 | num_items_bytes[3], |
| 14680 | ]; |
| 14681 | let length_so_far = length_so_far + request0.len(); |
| 14682 | let items_bytes = self.items.serialize(u8::from(format), u32::from(self.num_items)); |
| 14683 | let length_so_far = length_so_far + items_bytes.len(); |
| 14684 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 14685 | let length_so_far = length_so_far + padding0.len(); |
| 14686 | assert_eq!(length_so_far % 4, 0); |
| 14687 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 14688 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 14689 | ([request0.into(), items_bytes.into(), padding0.into()], vec![]) |
| 14690 | } |
| 14691 | /// Parse this request given its header, its body, and any fds that go along with it |
| 14692 | #[cfg (feature = "request-parsing" )] |
| 14693 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 14694 | if header.minor_opcode != XI_CHANGE_PROPERTY_REQUEST { |
| 14695 | return Err(ParseError::InvalidValue); |
| 14696 | } |
| 14697 | let (deviceid, remaining) = DeviceId::try_parse(value)?; |
| 14698 | let (mode, remaining) = u8::try_parse(remaining)?; |
| 14699 | let mode = mode.into(); |
| 14700 | let (format, remaining) = u8::try_parse(remaining)?; |
| 14701 | let (property, remaining) = xproto::Atom::try_parse(remaining)?; |
| 14702 | let (type_, remaining) = xproto::Atom::try_parse(remaining)?; |
| 14703 | let (num_items, remaining) = u32::try_parse(remaining)?; |
| 14704 | let (items, remaining) = XIChangePropertyAux::try_parse(remaining, u8::from(format), u32::from(num_items))?; |
| 14705 | let _ = remaining; |
| 14706 | Ok(XIChangePropertyRequest { |
| 14707 | deviceid, |
| 14708 | mode, |
| 14709 | property, |
| 14710 | type_, |
| 14711 | num_items, |
| 14712 | items: Cow::Owned(items), |
| 14713 | }) |
| 14714 | } |
| 14715 | /// Clone all borrowed data in this XIChangePropertyRequest. |
| 14716 | pub fn into_owned(self) -> XIChangePropertyRequest<'static> { |
| 14717 | XIChangePropertyRequest { |
| 14718 | deviceid: self.deviceid, |
| 14719 | mode: self.mode, |
| 14720 | property: self.property, |
| 14721 | type_: self.type_, |
| 14722 | num_items: self.num_items, |
| 14723 | items: Cow::Owned(self.items.into_owned()), |
| 14724 | } |
| 14725 | } |
| 14726 | } |
| 14727 | impl<'input> Request for XIChangePropertyRequest<'input> { |
| 14728 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 14729 | |
| 14730 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 14731 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 14732 | // Flatten the buffers into a single vector |
| 14733 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 14734 | (buf, fds) |
| 14735 | } |
| 14736 | } |
| 14737 | impl<'input> crate::x11_utils::VoidRequest for XIChangePropertyRequest<'input> { |
| 14738 | } |
| 14739 | |
| 14740 | /// Opcode for the XIDeleteProperty request |
| 14741 | pub const XI_DELETE_PROPERTY_REQUEST: u8 = 58; |
| 14742 | #[derive (Clone, Copy, Default)] |
| 14743 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 14744 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 14745 | pub struct XIDeletePropertyRequest { |
| 14746 | pub deviceid: DeviceId, |
| 14747 | pub property: xproto::Atom, |
| 14748 | } |
| 14749 | impl_debug_if_no_extra_traits!(XIDeletePropertyRequest, "XIDeletePropertyRequest" ); |
| 14750 | impl XIDeletePropertyRequest { |
| 14751 | /// Serialize this request into bytes for the provided connection |
| 14752 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 14753 | let length_so_far = 0; |
| 14754 | let deviceid_bytes = self.deviceid.serialize(); |
| 14755 | let property_bytes = self.property.serialize(); |
| 14756 | let mut request0 = vec![ |
| 14757 | major_opcode, |
| 14758 | XI_DELETE_PROPERTY_REQUEST, |
| 14759 | 0, |
| 14760 | 0, |
| 14761 | deviceid_bytes[0], |
| 14762 | deviceid_bytes[1], |
| 14763 | 0, |
| 14764 | 0, |
| 14765 | property_bytes[0], |
| 14766 | property_bytes[1], |
| 14767 | property_bytes[2], |
| 14768 | property_bytes[3], |
| 14769 | ]; |
| 14770 | let length_so_far = length_so_far + request0.len(); |
| 14771 | assert_eq!(length_so_far % 4, 0); |
| 14772 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 14773 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 14774 | ([request0.into()], vec![]) |
| 14775 | } |
| 14776 | /// Parse this request given its header, its body, and any fds that go along with it |
| 14777 | #[cfg (feature = "request-parsing" )] |
| 14778 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 14779 | if header.minor_opcode != XI_DELETE_PROPERTY_REQUEST { |
| 14780 | return Err(ParseError::InvalidValue); |
| 14781 | } |
| 14782 | let (deviceid, remaining) = DeviceId::try_parse(value)?; |
| 14783 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 14784 | let (property, remaining) = xproto::Atom::try_parse(remaining)?; |
| 14785 | let _ = remaining; |
| 14786 | Ok(XIDeletePropertyRequest { |
| 14787 | deviceid, |
| 14788 | property, |
| 14789 | }) |
| 14790 | } |
| 14791 | } |
| 14792 | impl Request for XIDeletePropertyRequest { |
| 14793 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 14794 | |
| 14795 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 14796 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 14797 | // Flatten the buffers into a single vector |
| 14798 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 14799 | (buf, fds) |
| 14800 | } |
| 14801 | } |
| 14802 | impl crate::x11_utils::VoidRequest for XIDeletePropertyRequest { |
| 14803 | } |
| 14804 | |
| 14805 | /// Opcode for the XIGetProperty request |
| 14806 | pub const XI_GET_PROPERTY_REQUEST: u8 = 59; |
| 14807 | #[derive (Clone, Copy, Default)] |
| 14808 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 14809 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 14810 | pub struct XIGetPropertyRequest { |
| 14811 | pub deviceid: DeviceId, |
| 14812 | pub delete: bool, |
| 14813 | pub property: xproto::Atom, |
| 14814 | pub type_: xproto::Atom, |
| 14815 | pub offset: u32, |
| 14816 | pub len: u32, |
| 14817 | } |
| 14818 | impl_debug_if_no_extra_traits!(XIGetPropertyRequest, "XIGetPropertyRequest" ); |
| 14819 | impl XIGetPropertyRequest { |
| 14820 | /// Serialize this request into bytes for the provided connection |
| 14821 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 14822 | let length_so_far = 0; |
| 14823 | let deviceid_bytes = self.deviceid.serialize(); |
| 14824 | let delete_bytes = self.delete.serialize(); |
| 14825 | let property_bytes = self.property.serialize(); |
| 14826 | let type_bytes = self.type_.serialize(); |
| 14827 | let offset_bytes = self.offset.serialize(); |
| 14828 | let len_bytes = self.len.serialize(); |
| 14829 | let mut request0 = vec![ |
| 14830 | major_opcode, |
| 14831 | XI_GET_PROPERTY_REQUEST, |
| 14832 | 0, |
| 14833 | 0, |
| 14834 | deviceid_bytes[0], |
| 14835 | deviceid_bytes[1], |
| 14836 | delete_bytes[0], |
| 14837 | 0, |
| 14838 | property_bytes[0], |
| 14839 | property_bytes[1], |
| 14840 | property_bytes[2], |
| 14841 | property_bytes[3], |
| 14842 | type_bytes[0], |
| 14843 | type_bytes[1], |
| 14844 | type_bytes[2], |
| 14845 | type_bytes[3], |
| 14846 | offset_bytes[0], |
| 14847 | offset_bytes[1], |
| 14848 | offset_bytes[2], |
| 14849 | offset_bytes[3], |
| 14850 | len_bytes[0], |
| 14851 | len_bytes[1], |
| 14852 | len_bytes[2], |
| 14853 | len_bytes[3], |
| 14854 | ]; |
| 14855 | let length_so_far = length_so_far + request0.len(); |
| 14856 | assert_eq!(length_so_far % 4, 0); |
| 14857 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 14858 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 14859 | ([request0.into()], vec![]) |
| 14860 | } |
| 14861 | /// Parse this request given its header, its body, and any fds that go along with it |
| 14862 | #[cfg (feature = "request-parsing" )] |
| 14863 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 14864 | if header.minor_opcode != XI_GET_PROPERTY_REQUEST { |
| 14865 | return Err(ParseError::InvalidValue); |
| 14866 | } |
| 14867 | let (deviceid, remaining) = DeviceId::try_parse(value)?; |
| 14868 | let (delete, remaining) = bool::try_parse(remaining)?; |
| 14869 | let remaining = remaining.get(1..).ok_or(ParseError::InsufficientData)?; |
| 14870 | let (property, remaining) = xproto::Atom::try_parse(remaining)?; |
| 14871 | let (type_, remaining) = xproto::Atom::try_parse(remaining)?; |
| 14872 | let (offset, remaining) = u32::try_parse(remaining)?; |
| 14873 | let (len, remaining) = u32::try_parse(remaining)?; |
| 14874 | let _ = remaining; |
| 14875 | Ok(XIGetPropertyRequest { |
| 14876 | deviceid, |
| 14877 | delete, |
| 14878 | property, |
| 14879 | type_, |
| 14880 | offset, |
| 14881 | len, |
| 14882 | }) |
| 14883 | } |
| 14884 | } |
| 14885 | impl Request for XIGetPropertyRequest { |
| 14886 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 14887 | |
| 14888 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 14889 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 14890 | // Flatten the buffers into a single vector |
| 14891 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 14892 | (buf, fds) |
| 14893 | } |
| 14894 | } |
| 14895 | impl crate::x11_utils::ReplyRequest for XIGetPropertyRequest { |
| 14896 | type Reply = XIGetPropertyReply; |
| 14897 | } |
| 14898 | |
| 14899 | #[derive (Clone)] |
| 14900 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 14901 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 14902 | pub enum XIGetPropertyItems { |
| 14903 | Data8(Vec<u8>), |
| 14904 | Data16(Vec<u16>), |
| 14905 | Data32(Vec<u32>), |
| 14906 | /// This variant is returned when the server sends a discriminant |
| 14907 | /// value that does not match any of the defined by the protocol. |
| 14908 | /// |
| 14909 | /// Usually, this should be considered a parsing error, but there |
| 14910 | /// are some cases where the server violates the protocol. |
| 14911 | /// |
| 14912 | /// Trying to use `serialize` or `serialize_into` with this variant |
| 14913 | /// will raise a panic. |
| 14914 | InvalidValue(u8), |
| 14915 | } |
| 14916 | impl_debug_if_no_extra_traits!(XIGetPropertyItems, "XIGetPropertyItems" ); |
| 14917 | impl XIGetPropertyItems { |
| 14918 | #[cfg_attr (not(feature = "request-parsing" ), allow(dead_code))] |
| 14919 | fn try_parse(value: &[u8], format: u8, num_items: u32) -> Result<(Self, &[u8]), ParseError> { |
| 14920 | let switch_expr = u8::from(format); |
| 14921 | let mut outer_remaining = value; |
| 14922 | let mut parse_result = None; |
| 14923 | if switch_expr == u8::from(PropertyFormat::M8_BITS) { |
| 14924 | let remaining = outer_remaining; |
| 14925 | let value = remaining; |
| 14926 | let (data8, remaining) = crate::x11_utils::parse_u8_list(remaining, num_items.try_to_usize()?)?; |
| 14927 | let data8 = data8.to_vec(); |
| 14928 | // Align offset to multiple of 4 |
| 14929 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 14930 | let misalignment = (4 - (offset % 4)) % 4; |
| 14931 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
| 14932 | outer_remaining = remaining; |
| 14933 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 14934 | parse_result = Some(XIGetPropertyItems::Data8(data8)); |
| 14935 | } |
| 14936 | if switch_expr == u8::from(PropertyFormat::M16_BITS) { |
| 14937 | let remaining = outer_remaining; |
| 14938 | let value = remaining; |
| 14939 | let (data16, remaining) = crate::x11_utils::parse_list::<u16>(remaining, num_items.try_to_usize()?)?; |
| 14940 | // Align offset to multiple of 4 |
| 14941 | let offset = remaining.as_ptr() as usize - value.as_ptr() as usize; |
| 14942 | let misalignment = (4 - (offset % 4)) % 4; |
| 14943 | let remaining = remaining.get(misalignment..).ok_or(ParseError::InsufficientData)?; |
| 14944 | outer_remaining = remaining; |
| 14945 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 14946 | parse_result = Some(XIGetPropertyItems::Data16(data16)); |
| 14947 | } |
| 14948 | if switch_expr == u8::from(PropertyFormat::M32_BITS) { |
| 14949 | let remaining = outer_remaining; |
| 14950 | let (data32, remaining) = crate::x11_utils::parse_list::<u32>(remaining, num_items.try_to_usize()?)?; |
| 14951 | outer_remaining = remaining; |
| 14952 | assert!(parse_result.is_none(), "The XML should prevent more than one 'if' from matching" ); |
| 14953 | parse_result = Some(XIGetPropertyItems::Data32(data32)); |
| 14954 | } |
| 14955 | match parse_result { |
| 14956 | None => Ok((XIGetPropertyItems::InvalidValue(switch_expr), &[])), |
| 14957 | Some(result) => Ok((result, outer_remaining)), |
| 14958 | } |
| 14959 | } |
| 14960 | } |
| 14961 | impl XIGetPropertyItems { |
| 14962 | pub fn as_data8(&self) -> Option<&Vec<u8>> { |
| 14963 | match self { |
| 14964 | XIGetPropertyItems::Data8(value: &Vec) => Some(value), |
| 14965 | _ => None, |
| 14966 | } |
| 14967 | } |
| 14968 | pub fn as_data16(&self) -> Option<&Vec<u16>> { |
| 14969 | match self { |
| 14970 | XIGetPropertyItems::Data16(value: &Vec) => Some(value), |
| 14971 | _ => None, |
| 14972 | } |
| 14973 | } |
| 14974 | pub fn as_data32(&self) -> Option<&Vec<u32>> { |
| 14975 | match self { |
| 14976 | XIGetPropertyItems::Data32(value: &Vec) => Some(value), |
| 14977 | _ => None, |
| 14978 | } |
| 14979 | } |
| 14980 | } |
| 14981 | impl XIGetPropertyItems { |
| 14982 | #[allow (dead_code)] |
| 14983 | fn serialize(&self, format: u8, num_items: u32) -> Vec<u8> { |
| 14984 | let mut result = Vec::new(); |
| 14985 | self.serialize_into(&mut result, u8::from(format), u32::from(num_items)); |
| 14986 | result |
| 14987 | } |
| 14988 | fn serialize_into(&self, bytes: &mut Vec<u8>, format: u8, num_items: u32) { |
| 14989 | assert_eq!(self.switch_expr(), u8::from(format), "switch `items` has an inconsistent discriminant" ); |
| 14990 | match self { |
| 14991 | XIGetPropertyItems::Data8(data8) => { |
| 14992 | assert_eq!(data8.len(), usize::try_from(num_items).unwrap(), "`data8` has an incorrect length" ); |
| 14993 | bytes.extend_from_slice(&data8); |
| 14994 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 14995 | } |
| 14996 | XIGetPropertyItems::Data16(data16) => { |
| 14997 | assert_eq!(data16.len(), usize::try_from(num_items).unwrap(), "`data16` has an incorrect length" ); |
| 14998 | data16.serialize_into(bytes); |
| 14999 | bytes.extend_from_slice(&[0; 3][..(4 - (bytes.len() % 4)) % 4]); |
| 15000 | } |
| 15001 | XIGetPropertyItems::Data32(data32) => { |
| 15002 | assert_eq!(data32.len(), usize::try_from(num_items).unwrap(), "`data32` has an incorrect length" ); |
| 15003 | data32.serialize_into(bytes); |
| 15004 | } |
| 15005 | XIGetPropertyItems::InvalidValue(_) => panic!("attempted to serialize invalid switch case" ), |
| 15006 | } |
| 15007 | } |
| 15008 | } |
| 15009 | impl XIGetPropertyItems { |
| 15010 | fn switch_expr(&self) -> u8 { |
| 15011 | match self { |
| 15012 | XIGetPropertyItems::Data8(_) => u8::from(PropertyFormat::M8_BITS), |
| 15013 | XIGetPropertyItems::Data16(_) => u8::from(PropertyFormat::M16_BITS), |
| 15014 | XIGetPropertyItems::Data32(_) => u8::from(PropertyFormat::M32_BITS), |
| 15015 | XIGetPropertyItems::InvalidValue(switch_expr: &u8) => *switch_expr, |
| 15016 | } |
| 15017 | } |
| 15018 | } |
| 15019 | |
| 15020 | #[derive (Clone)] |
| 15021 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 15022 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 15023 | pub struct XIGetPropertyReply { |
| 15024 | pub sequence: u16, |
| 15025 | pub length: u32, |
| 15026 | pub type_: xproto::Atom, |
| 15027 | pub bytes_after: u32, |
| 15028 | pub num_items: u32, |
| 15029 | pub items: XIGetPropertyItems, |
| 15030 | } |
| 15031 | impl_debug_if_no_extra_traits!(XIGetPropertyReply, "XIGetPropertyReply" ); |
| 15032 | impl TryParse for XIGetPropertyReply { |
| 15033 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 15034 | let remaining: &[u8] = initial_value; |
| 15035 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 15036 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 15037 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 15038 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 15039 | let (type_: u32, remaining: &[u8]) = xproto::Atom::try_parse(remaining)?; |
| 15040 | let (bytes_after: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 15041 | let (num_items: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 15042 | let (format: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 15043 | let remaining: &[u8] = remaining.get(11..).ok_or(err:ParseError::InsufficientData)?; |
| 15044 | let (items: XIGetPropertyItems, remaining: &[u8]) = XIGetPropertyItems::try_parse(value:remaining, format:u8::from(format), num_items:u32::from(num_items))?; |
| 15045 | if response_type != 1 { |
| 15046 | return Err(ParseError::InvalidValue); |
| 15047 | } |
| 15048 | let result: XIGetPropertyReply = XIGetPropertyReply { sequence, length, type_, bytes_after, num_items, items }; |
| 15049 | let _ = remaining; |
| 15050 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 15051 | .ok_or(err:ParseError::InsufficientData)?; |
| 15052 | Ok((result, remaining)) |
| 15053 | } |
| 15054 | } |
| 15055 | impl Serialize for XIGetPropertyReply { |
| 15056 | type Bytes = Vec<u8>; |
| 15057 | fn serialize(&self) -> Vec<u8> { |
| 15058 | let mut result: Vec = Vec::new(); |
| 15059 | self.serialize_into(&mut result); |
| 15060 | result |
| 15061 | } |
| 15062 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 15063 | bytes.reserve(additional:32); |
| 15064 | let response_type_bytes: &[u8; 1] = &[1]; |
| 15065 | bytes.push(response_type_bytes[0]); |
| 15066 | bytes.extend_from_slice(&[0; 1]); |
| 15067 | self.sequence.serialize_into(bytes); |
| 15068 | self.length.serialize_into(bytes); |
| 15069 | self.type_.serialize_into(bytes); |
| 15070 | self.bytes_after.serialize_into(bytes); |
| 15071 | self.num_items.serialize_into(bytes); |
| 15072 | let format: u8 = self.items.switch_expr(); |
| 15073 | format.serialize_into(bytes); |
| 15074 | bytes.extend_from_slice(&[0; 11]); |
| 15075 | self.items.serialize_into(bytes, format:u8::from(format), num_items:u32::from(self.num_items)); |
| 15076 | } |
| 15077 | } |
| 15078 | |
| 15079 | /// Opcode for the XIGetSelectedEvents request |
| 15080 | pub const XI_GET_SELECTED_EVENTS_REQUEST: u8 = 60; |
| 15081 | #[derive (Clone, Copy, Default)] |
| 15082 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 15083 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 15084 | pub struct XIGetSelectedEventsRequest { |
| 15085 | pub window: xproto::Window, |
| 15086 | } |
| 15087 | impl_debug_if_no_extra_traits!(XIGetSelectedEventsRequest, "XIGetSelectedEventsRequest" ); |
| 15088 | impl XIGetSelectedEventsRequest { |
| 15089 | /// Serialize this request into bytes for the provided connection |
| 15090 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'static, [u8]>; 1]> { |
| 15091 | let length_so_far = 0; |
| 15092 | let window_bytes = self.window.serialize(); |
| 15093 | let mut request0 = vec![ |
| 15094 | major_opcode, |
| 15095 | XI_GET_SELECTED_EVENTS_REQUEST, |
| 15096 | 0, |
| 15097 | 0, |
| 15098 | window_bytes[0], |
| 15099 | window_bytes[1], |
| 15100 | window_bytes[2], |
| 15101 | window_bytes[3], |
| 15102 | ]; |
| 15103 | let length_so_far = length_so_far + request0.len(); |
| 15104 | assert_eq!(length_so_far % 4, 0); |
| 15105 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 15106 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 15107 | ([request0.into()], vec![]) |
| 15108 | } |
| 15109 | /// Parse this request given its header, its body, and any fds that go along with it |
| 15110 | #[cfg (feature = "request-parsing" )] |
| 15111 | pub fn try_parse_request(header: RequestHeader, value: &[u8]) -> Result<Self, ParseError> { |
| 15112 | if header.minor_opcode != XI_GET_SELECTED_EVENTS_REQUEST { |
| 15113 | return Err(ParseError::InvalidValue); |
| 15114 | } |
| 15115 | let (window, remaining) = xproto::Window::try_parse(value)?; |
| 15116 | let _ = remaining; |
| 15117 | Ok(XIGetSelectedEventsRequest { |
| 15118 | window, |
| 15119 | }) |
| 15120 | } |
| 15121 | } |
| 15122 | impl Request for XIGetSelectedEventsRequest { |
| 15123 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 15124 | |
| 15125 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 15126 | let (bufs: [Cow<'static, [u8]>; 1], fds: Vec) = self.serialize(major_opcode); |
| 15127 | // Flatten the buffers into a single vector |
| 15128 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 15129 | (buf, fds) |
| 15130 | } |
| 15131 | } |
| 15132 | impl crate::x11_utils::ReplyRequest for XIGetSelectedEventsRequest { |
| 15133 | type Reply = XIGetSelectedEventsReply; |
| 15134 | } |
| 15135 | |
| 15136 | #[derive (Clone, Default)] |
| 15137 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 15138 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 15139 | pub struct XIGetSelectedEventsReply { |
| 15140 | pub sequence: u16, |
| 15141 | pub length: u32, |
| 15142 | pub masks: Vec<EventMask>, |
| 15143 | } |
| 15144 | impl_debug_if_no_extra_traits!(XIGetSelectedEventsReply, "XIGetSelectedEventsReply" ); |
| 15145 | impl TryParse for XIGetSelectedEventsReply { |
| 15146 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 15147 | let remaining: &[u8] = initial_value; |
| 15148 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 15149 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 15150 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 15151 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 15152 | let (num_masks: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 15153 | let remaining: &[u8] = remaining.get(22..).ok_or(err:ParseError::InsufficientData)?; |
| 15154 | let (masks: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<EventMask>(data:remaining, list_length:num_masks.try_to_usize()?)?; |
| 15155 | if response_type != 1 { |
| 15156 | return Err(ParseError::InvalidValue); |
| 15157 | } |
| 15158 | let result: XIGetSelectedEventsReply = XIGetSelectedEventsReply { sequence, length, masks }; |
| 15159 | let _ = remaining; |
| 15160 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 15161 | .ok_or(err:ParseError::InsufficientData)?; |
| 15162 | Ok((result, remaining)) |
| 15163 | } |
| 15164 | } |
| 15165 | impl Serialize for XIGetSelectedEventsReply { |
| 15166 | type Bytes = Vec<u8>; |
| 15167 | fn serialize(&self) -> Vec<u8> { |
| 15168 | let mut result: Vec = Vec::new(); |
| 15169 | self.serialize_into(&mut result); |
| 15170 | result |
| 15171 | } |
| 15172 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 15173 | bytes.reserve(additional:32); |
| 15174 | let response_type_bytes: &[u8; 1] = &[1]; |
| 15175 | bytes.push(response_type_bytes[0]); |
| 15176 | bytes.extend_from_slice(&[0; 1]); |
| 15177 | self.sequence.serialize_into(bytes); |
| 15178 | self.length.serialize_into(bytes); |
| 15179 | let num_masks: u16 = u16::try_from(self.masks.len()).expect(msg:"`masks` has too many elements" ); |
| 15180 | num_masks.serialize_into(bytes); |
| 15181 | bytes.extend_from_slice(&[0; 22]); |
| 15182 | self.masks.serialize_into(bytes); |
| 15183 | } |
| 15184 | } |
| 15185 | impl XIGetSelectedEventsReply { |
| 15186 | /// Get the value of the `num_masks` field. |
| 15187 | /// |
| 15188 | /// The `num_masks` field is used as the length field of the `masks` field. |
| 15189 | /// This function computes the field's value again based on the length of the list. |
| 15190 | /// |
| 15191 | /// # Panics |
| 15192 | /// |
| 15193 | /// Panics if the value cannot be represented in the target type. This |
| 15194 | /// cannot happen with values of the struct received from the X11 server. |
| 15195 | pub fn num_masks(&self) -> u16 { |
| 15196 | self.masks.len() |
| 15197 | .try_into().unwrap() |
| 15198 | } |
| 15199 | } |
| 15200 | |
| 15201 | #[derive (Clone, Copy, Default)] |
| 15202 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 15203 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 15204 | pub struct BarrierReleasePointerInfo { |
| 15205 | pub deviceid: DeviceId, |
| 15206 | pub barrier: xfixes::Barrier, |
| 15207 | pub eventid: u32, |
| 15208 | } |
| 15209 | impl_debug_if_no_extra_traits!(BarrierReleasePointerInfo, "BarrierReleasePointerInfo" ); |
| 15210 | impl TryParse for BarrierReleasePointerInfo { |
| 15211 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 15212 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 15213 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 15214 | let (barrier: u32, remaining: &[u8]) = xfixes::Barrier::try_parse(remaining)?; |
| 15215 | let (eventid: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 15216 | let result: BarrierReleasePointerInfo = BarrierReleasePointerInfo { deviceid, barrier, eventid }; |
| 15217 | Ok((result, remaining)) |
| 15218 | } |
| 15219 | } |
| 15220 | impl Serialize for BarrierReleasePointerInfo { |
| 15221 | type Bytes = [u8; 12]; |
| 15222 | fn serialize(&self) -> [u8; 12] { |
| 15223 | let deviceid_bytes = self.deviceid.serialize(); |
| 15224 | let barrier_bytes = self.barrier.serialize(); |
| 15225 | let eventid_bytes = self.eventid.serialize(); |
| 15226 | [ |
| 15227 | deviceid_bytes[0], |
| 15228 | deviceid_bytes[1], |
| 15229 | 0, |
| 15230 | 0, |
| 15231 | barrier_bytes[0], |
| 15232 | barrier_bytes[1], |
| 15233 | barrier_bytes[2], |
| 15234 | barrier_bytes[3], |
| 15235 | eventid_bytes[0], |
| 15236 | eventid_bytes[1], |
| 15237 | eventid_bytes[2], |
| 15238 | eventid_bytes[3], |
| 15239 | ] |
| 15240 | } |
| 15241 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 15242 | bytes.reserve(12); |
| 15243 | self.deviceid.serialize_into(bytes); |
| 15244 | bytes.extend_from_slice(&[0; 2]); |
| 15245 | self.barrier.serialize_into(bytes); |
| 15246 | self.eventid.serialize_into(bytes); |
| 15247 | } |
| 15248 | } |
| 15249 | |
| 15250 | /// Opcode for the XIBarrierReleasePointer request |
| 15251 | pub const XI_BARRIER_RELEASE_POINTER_REQUEST: u8 = 61; |
| 15252 | #[derive (Clone, Default)] |
| 15253 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 15254 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 15255 | pub struct XIBarrierReleasePointerRequest<'input> { |
| 15256 | pub barriers: Cow<'input, [BarrierReleasePointerInfo]>, |
| 15257 | } |
| 15258 | impl_debug_if_no_extra_traits!(XIBarrierReleasePointerRequest<'_>, "XIBarrierReleasePointerRequest" ); |
| 15259 | impl<'input> XIBarrierReleasePointerRequest<'input> { |
| 15260 | /// Serialize this request into bytes for the provided connection |
| 15261 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 3]> { |
| 15262 | let length_so_far = 0; |
| 15263 | let num_barriers = u32::try_from(self.barriers.len()).expect("`barriers` has too many elements" ); |
| 15264 | let num_barriers_bytes = num_barriers.serialize(); |
| 15265 | let mut request0 = vec![ |
| 15266 | major_opcode, |
| 15267 | XI_BARRIER_RELEASE_POINTER_REQUEST, |
| 15268 | 0, |
| 15269 | 0, |
| 15270 | num_barriers_bytes[0], |
| 15271 | num_barriers_bytes[1], |
| 15272 | num_barriers_bytes[2], |
| 15273 | num_barriers_bytes[3], |
| 15274 | ]; |
| 15275 | let length_so_far = length_so_far + request0.len(); |
| 15276 | let barriers_bytes = self.barriers.serialize(); |
| 15277 | let length_so_far = length_so_far + barriers_bytes.len(); |
| 15278 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 15279 | let length_so_far = length_so_far + padding0.len(); |
| 15280 | assert_eq!(length_so_far % 4, 0); |
| 15281 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 15282 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 15283 | ([request0.into(), barriers_bytes.into(), padding0.into()], vec![]) |
| 15284 | } |
| 15285 | /// Parse this request given its header, its body, and any fds that go along with it |
| 15286 | #[cfg (feature = "request-parsing" )] |
| 15287 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 15288 | if header.minor_opcode != XI_BARRIER_RELEASE_POINTER_REQUEST { |
| 15289 | return Err(ParseError::InvalidValue); |
| 15290 | } |
| 15291 | let (num_barriers, remaining) = u32::try_parse(value)?; |
| 15292 | let (barriers, remaining) = crate::x11_utils::parse_list::<BarrierReleasePointerInfo>(remaining, num_barriers.try_to_usize()?)?; |
| 15293 | let _ = remaining; |
| 15294 | Ok(XIBarrierReleasePointerRequest { |
| 15295 | barriers: Cow::Owned(barriers), |
| 15296 | }) |
| 15297 | } |
| 15298 | /// Clone all borrowed data in this XIBarrierReleasePointerRequest. |
| 15299 | pub fn into_owned(self) -> XIBarrierReleasePointerRequest<'static> { |
| 15300 | XIBarrierReleasePointerRequest { |
| 15301 | barriers: Cow::Owned(self.barriers.into_owned()), |
| 15302 | } |
| 15303 | } |
| 15304 | } |
| 15305 | impl<'input> Request for XIBarrierReleasePointerRequest<'input> { |
| 15306 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 15307 | |
| 15308 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 15309 | let (bufs: [Cow<'_, [u8]>; 3], fds: Vec) = self.serialize(major_opcode); |
| 15310 | // Flatten the buffers into a single vector |
| 15311 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 15312 | (buf, fds) |
| 15313 | } |
| 15314 | } |
| 15315 | impl<'input> crate::x11_utils::VoidRequest for XIBarrierReleasePointerRequest<'input> { |
| 15316 | } |
| 15317 | |
| 15318 | /// Opcode for the DeviceValuator event |
| 15319 | pub const DEVICE_VALUATOR_EVENT: u8 = 0; |
| 15320 | #[derive (Clone, Copy, Default)] |
| 15321 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 15322 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 15323 | pub struct DeviceValuatorEvent { |
| 15324 | pub response_type: u8, |
| 15325 | pub device_id: u8, |
| 15326 | pub sequence: u16, |
| 15327 | pub device_state: u16, |
| 15328 | pub num_valuators: u8, |
| 15329 | pub first_valuator: u8, |
| 15330 | pub valuators: [i32; 6], |
| 15331 | } |
| 15332 | impl_debug_if_no_extra_traits!(DeviceValuatorEvent, "DeviceValuatorEvent" ); |
| 15333 | impl TryParse for DeviceValuatorEvent { |
| 15334 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 15335 | let remaining = initial_value; |
| 15336 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 15337 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 15338 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 15339 | let (device_state, remaining) = u16::try_parse(remaining)?; |
| 15340 | let (num_valuators, remaining) = u8::try_parse(remaining)?; |
| 15341 | let (first_valuator, remaining) = u8::try_parse(remaining)?; |
| 15342 | let (valuators_0, remaining) = i32::try_parse(remaining)?; |
| 15343 | let (valuators_1, remaining) = i32::try_parse(remaining)?; |
| 15344 | let (valuators_2, remaining) = i32::try_parse(remaining)?; |
| 15345 | let (valuators_3, remaining) = i32::try_parse(remaining)?; |
| 15346 | let (valuators_4, remaining) = i32::try_parse(remaining)?; |
| 15347 | let (valuators_5, remaining) = i32::try_parse(remaining)?; |
| 15348 | let valuators = [ |
| 15349 | valuators_0, |
| 15350 | valuators_1, |
| 15351 | valuators_2, |
| 15352 | valuators_3, |
| 15353 | valuators_4, |
| 15354 | valuators_5, |
| 15355 | ]; |
| 15356 | let result = DeviceValuatorEvent { response_type, device_id, sequence, device_state, num_valuators, first_valuator, valuators }; |
| 15357 | let _ = remaining; |
| 15358 | let remaining = initial_value.get(32..) |
| 15359 | .ok_or(ParseError::InsufficientData)?; |
| 15360 | Ok((result, remaining)) |
| 15361 | } |
| 15362 | } |
| 15363 | impl Serialize for DeviceValuatorEvent { |
| 15364 | type Bytes = [u8; 32]; |
| 15365 | fn serialize(&self) -> [u8; 32] { |
| 15366 | let response_type_bytes = self.response_type.serialize(); |
| 15367 | let device_id_bytes = self.device_id.serialize(); |
| 15368 | let sequence_bytes = self.sequence.serialize(); |
| 15369 | let device_state_bytes = self.device_state.serialize(); |
| 15370 | let num_valuators_bytes = self.num_valuators.serialize(); |
| 15371 | let first_valuator_bytes = self.first_valuator.serialize(); |
| 15372 | let valuators_0_bytes = self.valuators[0].serialize(); |
| 15373 | let valuators_1_bytes = self.valuators[1].serialize(); |
| 15374 | let valuators_2_bytes = self.valuators[2].serialize(); |
| 15375 | let valuators_3_bytes = self.valuators[3].serialize(); |
| 15376 | let valuators_4_bytes = self.valuators[4].serialize(); |
| 15377 | let valuators_5_bytes = self.valuators[5].serialize(); |
| 15378 | [ |
| 15379 | response_type_bytes[0], |
| 15380 | device_id_bytes[0], |
| 15381 | sequence_bytes[0], |
| 15382 | sequence_bytes[1], |
| 15383 | device_state_bytes[0], |
| 15384 | device_state_bytes[1], |
| 15385 | num_valuators_bytes[0], |
| 15386 | first_valuator_bytes[0], |
| 15387 | valuators_0_bytes[0], |
| 15388 | valuators_0_bytes[1], |
| 15389 | valuators_0_bytes[2], |
| 15390 | valuators_0_bytes[3], |
| 15391 | valuators_1_bytes[0], |
| 15392 | valuators_1_bytes[1], |
| 15393 | valuators_1_bytes[2], |
| 15394 | valuators_1_bytes[3], |
| 15395 | valuators_2_bytes[0], |
| 15396 | valuators_2_bytes[1], |
| 15397 | valuators_2_bytes[2], |
| 15398 | valuators_2_bytes[3], |
| 15399 | valuators_3_bytes[0], |
| 15400 | valuators_3_bytes[1], |
| 15401 | valuators_3_bytes[2], |
| 15402 | valuators_3_bytes[3], |
| 15403 | valuators_4_bytes[0], |
| 15404 | valuators_4_bytes[1], |
| 15405 | valuators_4_bytes[2], |
| 15406 | valuators_4_bytes[3], |
| 15407 | valuators_5_bytes[0], |
| 15408 | valuators_5_bytes[1], |
| 15409 | valuators_5_bytes[2], |
| 15410 | valuators_5_bytes[3], |
| 15411 | ] |
| 15412 | } |
| 15413 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 15414 | bytes.reserve(32); |
| 15415 | self.response_type.serialize_into(bytes); |
| 15416 | self.device_id.serialize_into(bytes); |
| 15417 | self.sequence.serialize_into(bytes); |
| 15418 | self.device_state.serialize_into(bytes); |
| 15419 | self.num_valuators.serialize_into(bytes); |
| 15420 | self.first_valuator.serialize_into(bytes); |
| 15421 | self.valuators.serialize_into(bytes); |
| 15422 | } |
| 15423 | } |
| 15424 | impl From<&DeviceValuatorEvent> for [u8; 32] { |
| 15425 | fn from(input: &DeviceValuatorEvent) -> Self { |
| 15426 | let response_type_bytes = input.response_type.serialize(); |
| 15427 | let device_id_bytes = input.device_id.serialize(); |
| 15428 | let sequence_bytes = input.sequence.serialize(); |
| 15429 | let device_state_bytes = input.device_state.serialize(); |
| 15430 | let num_valuators_bytes = input.num_valuators.serialize(); |
| 15431 | let first_valuator_bytes = input.first_valuator.serialize(); |
| 15432 | let valuators_0_bytes = input.valuators[0].serialize(); |
| 15433 | let valuators_1_bytes = input.valuators[1].serialize(); |
| 15434 | let valuators_2_bytes = input.valuators[2].serialize(); |
| 15435 | let valuators_3_bytes = input.valuators[3].serialize(); |
| 15436 | let valuators_4_bytes = input.valuators[4].serialize(); |
| 15437 | let valuators_5_bytes = input.valuators[5].serialize(); |
| 15438 | [ |
| 15439 | response_type_bytes[0], |
| 15440 | device_id_bytes[0], |
| 15441 | sequence_bytes[0], |
| 15442 | sequence_bytes[1], |
| 15443 | device_state_bytes[0], |
| 15444 | device_state_bytes[1], |
| 15445 | num_valuators_bytes[0], |
| 15446 | first_valuator_bytes[0], |
| 15447 | valuators_0_bytes[0], |
| 15448 | valuators_0_bytes[1], |
| 15449 | valuators_0_bytes[2], |
| 15450 | valuators_0_bytes[3], |
| 15451 | valuators_1_bytes[0], |
| 15452 | valuators_1_bytes[1], |
| 15453 | valuators_1_bytes[2], |
| 15454 | valuators_1_bytes[3], |
| 15455 | valuators_2_bytes[0], |
| 15456 | valuators_2_bytes[1], |
| 15457 | valuators_2_bytes[2], |
| 15458 | valuators_2_bytes[3], |
| 15459 | valuators_3_bytes[0], |
| 15460 | valuators_3_bytes[1], |
| 15461 | valuators_3_bytes[2], |
| 15462 | valuators_3_bytes[3], |
| 15463 | valuators_4_bytes[0], |
| 15464 | valuators_4_bytes[1], |
| 15465 | valuators_4_bytes[2], |
| 15466 | valuators_4_bytes[3], |
| 15467 | valuators_5_bytes[0], |
| 15468 | valuators_5_bytes[1], |
| 15469 | valuators_5_bytes[2], |
| 15470 | valuators_5_bytes[3], |
| 15471 | ] |
| 15472 | } |
| 15473 | } |
| 15474 | impl From<DeviceValuatorEvent> for [u8; 32] { |
| 15475 | fn from(input: DeviceValuatorEvent) -> Self { |
| 15476 | Self::from(&input) |
| 15477 | } |
| 15478 | } |
| 15479 | |
| 15480 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 15481 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 15482 | pub struct MoreEventsMask(u8); |
| 15483 | impl MoreEventsMask { |
| 15484 | pub const MORE_EVENTS: Self = Self(1 << 7); |
| 15485 | } |
| 15486 | impl From<MoreEventsMask> for u8 { |
| 15487 | #[inline ] |
| 15488 | fn from(input: MoreEventsMask) -> Self { |
| 15489 | input.0 |
| 15490 | } |
| 15491 | } |
| 15492 | impl From<MoreEventsMask> for Option<u8> { |
| 15493 | #[inline ] |
| 15494 | fn from(input: MoreEventsMask) -> Self { |
| 15495 | Some(input.0) |
| 15496 | } |
| 15497 | } |
| 15498 | impl From<MoreEventsMask> for u16 { |
| 15499 | #[inline ] |
| 15500 | fn from(input: MoreEventsMask) -> Self { |
| 15501 | u16::from(input.0) |
| 15502 | } |
| 15503 | } |
| 15504 | impl From<MoreEventsMask> for Option<u16> { |
| 15505 | #[inline ] |
| 15506 | fn from(input: MoreEventsMask) -> Self { |
| 15507 | Some(u16::from(input.0)) |
| 15508 | } |
| 15509 | } |
| 15510 | impl From<MoreEventsMask> for u32 { |
| 15511 | #[inline ] |
| 15512 | fn from(input: MoreEventsMask) -> Self { |
| 15513 | u32::from(input.0) |
| 15514 | } |
| 15515 | } |
| 15516 | impl From<MoreEventsMask> for Option<u32> { |
| 15517 | #[inline ] |
| 15518 | fn from(input: MoreEventsMask) -> Self { |
| 15519 | Some(u32::from(input.0)) |
| 15520 | } |
| 15521 | } |
| 15522 | impl From<u8> for MoreEventsMask { |
| 15523 | #[inline ] |
| 15524 | fn from(value: u8) -> Self { |
| 15525 | Self(value) |
| 15526 | } |
| 15527 | } |
| 15528 | impl core::fmt::Debug for MoreEventsMask { |
| 15529 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 15530 | let variants: [(u32, &'static str, &'static …); 1] = [ |
| 15531 | (Self::MORE_EVENTS.0.into(), "MORE_EVENTS" , "MoreEvents" ), |
| 15532 | ]; |
| 15533 | pretty_print_bitmask(fmt, self.0.into(), &variants) |
| 15534 | } |
| 15535 | } |
| 15536 | bitmask_binop!(MoreEventsMask, u8); |
| 15537 | |
| 15538 | /// Opcode for the DeviceKeyPress event |
| 15539 | pub const DEVICE_KEY_PRESS_EVENT: u8 = 1; |
| 15540 | #[derive (Clone, Copy, Default)] |
| 15541 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 15542 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 15543 | pub struct DeviceKeyPressEvent { |
| 15544 | pub response_type: u8, |
| 15545 | pub detail: u8, |
| 15546 | pub sequence: u16, |
| 15547 | pub time: xproto::Timestamp, |
| 15548 | pub root: xproto::Window, |
| 15549 | pub event: xproto::Window, |
| 15550 | pub child: xproto::Window, |
| 15551 | pub root_x: i16, |
| 15552 | pub root_y: i16, |
| 15553 | pub event_x: i16, |
| 15554 | pub event_y: i16, |
| 15555 | pub state: xproto::KeyButMask, |
| 15556 | pub same_screen: bool, |
| 15557 | pub device_id: u8, |
| 15558 | } |
| 15559 | impl_debug_if_no_extra_traits!(DeviceKeyPressEvent, "DeviceKeyPressEvent" ); |
| 15560 | impl TryParse for DeviceKeyPressEvent { |
| 15561 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 15562 | let remaining = initial_value; |
| 15563 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 15564 | let (detail, remaining) = u8::try_parse(remaining)?; |
| 15565 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 15566 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 15567 | let (root, remaining) = xproto::Window::try_parse(remaining)?; |
| 15568 | let (event, remaining) = xproto::Window::try_parse(remaining)?; |
| 15569 | let (child, remaining) = xproto::Window::try_parse(remaining)?; |
| 15570 | let (root_x, remaining) = i16::try_parse(remaining)?; |
| 15571 | let (root_y, remaining) = i16::try_parse(remaining)?; |
| 15572 | let (event_x, remaining) = i16::try_parse(remaining)?; |
| 15573 | let (event_y, remaining) = i16::try_parse(remaining)?; |
| 15574 | let (state, remaining) = u16::try_parse(remaining)?; |
| 15575 | let (same_screen, remaining) = bool::try_parse(remaining)?; |
| 15576 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 15577 | let state = state.into(); |
| 15578 | let result = DeviceKeyPressEvent { response_type, detail, sequence, time, root, event, child, root_x, root_y, event_x, event_y, state, same_screen, device_id }; |
| 15579 | let _ = remaining; |
| 15580 | let remaining = initial_value.get(32..) |
| 15581 | .ok_or(ParseError::InsufficientData)?; |
| 15582 | Ok((result, remaining)) |
| 15583 | } |
| 15584 | } |
| 15585 | impl Serialize for DeviceKeyPressEvent { |
| 15586 | type Bytes = [u8; 32]; |
| 15587 | fn serialize(&self) -> [u8; 32] { |
| 15588 | let response_type_bytes = self.response_type.serialize(); |
| 15589 | let detail_bytes = self.detail.serialize(); |
| 15590 | let sequence_bytes = self.sequence.serialize(); |
| 15591 | let time_bytes = self.time.serialize(); |
| 15592 | let root_bytes = self.root.serialize(); |
| 15593 | let event_bytes = self.event.serialize(); |
| 15594 | let child_bytes = self.child.serialize(); |
| 15595 | let root_x_bytes = self.root_x.serialize(); |
| 15596 | let root_y_bytes = self.root_y.serialize(); |
| 15597 | let event_x_bytes = self.event_x.serialize(); |
| 15598 | let event_y_bytes = self.event_y.serialize(); |
| 15599 | let state_bytes = u16::from(self.state).serialize(); |
| 15600 | let same_screen_bytes = self.same_screen.serialize(); |
| 15601 | let device_id_bytes = self.device_id.serialize(); |
| 15602 | [ |
| 15603 | response_type_bytes[0], |
| 15604 | detail_bytes[0], |
| 15605 | sequence_bytes[0], |
| 15606 | sequence_bytes[1], |
| 15607 | time_bytes[0], |
| 15608 | time_bytes[1], |
| 15609 | time_bytes[2], |
| 15610 | time_bytes[3], |
| 15611 | root_bytes[0], |
| 15612 | root_bytes[1], |
| 15613 | root_bytes[2], |
| 15614 | root_bytes[3], |
| 15615 | event_bytes[0], |
| 15616 | event_bytes[1], |
| 15617 | event_bytes[2], |
| 15618 | event_bytes[3], |
| 15619 | child_bytes[0], |
| 15620 | child_bytes[1], |
| 15621 | child_bytes[2], |
| 15622 | child_bytes[3], |
| 15623 | root_x_bytes[0], |
| 15624 | root_x_bytes[1], |
| 15625 | root_y_bytes[0], |
| 15626 | root_y_bytes[1], |
| 15627 | event_x_bytes[0], |
| 15628 | event_x_bytes[1], |
| 15629 | event_y_bytes[0], |
| 15630 | event_y_bytes[1], |
| 15631 | state_bytes[0], |
| 15632 | state_bytes[1], |
| 15633 | same_screen_bytes[0], |
| 15634 | device_id_bytes[0], |
| 15635 | ] |
| 15636 | } |
| 15637 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 15638 | bytes.reserve(32); |
| 15639 | self.response_type.serialize_into(bytes); |
| 15640 | self.detail.serialize_into(bytes); |
| 15641 | self.sequence.serialize_into(bytes); |
| 15642 | self.time.serialize_into(bytes); |
| 15643 | self.root.serialize_into(bytes); |
| 15644 | self.event.serialize_into(bytes); |
| 15645 | self.child.serialize_into(bytes); |
| 15646 | self.root_x.serialize_into(bytes); |
| 15647 | self.root_y.serialize_into(bytes); |
| 15648 | self.event_x.serialize_into(bytes); |
| 15649 | self.event_y.serialize_into(bytes); |
| 15650 | u16::from(self.state).serialize_into(bytes); |
| 15651 | self.same_screen.serialize_into(bytes); |
| 15652 | self.device_id.serialize_into(bytes); |
| 15653 | } |
| 15654 | } |
| 15655 | impl From<&DeviceKeyPressEvent> for [u8; 32] { |
| 15656 | fn from(input: &DeviceKeyPressEvent) -> Self { |
| 15657 | let response_type_bytes = input.response_type.serialize(); |
| 15658 | let detail_bytes = input.detail.serialize(); |
| 15659 | let sequence_bytes = input.sequence.serialize(); |
| 15660 | let time_bytes = input.time.serialize(); |
| 15661 | let root_bytes = input.root.serialize(); |
| 15662 | let event_bytes = input.event.serialize(); |
| 15663 | let child_bytes = input.child.serialize(); |
| 15664 | let root_x_bytes = input.root_x.serialize(); |
| 15665 | let root_y_bytes = input.root_y.serialize(); |
| 15666 | let event_x_bytes = input.event_x.serialize(); |
| 15667 | let event_y_bytes = input.event_y.serialize(); |
| 15668 | let state_bytes = u16::from(input.state).serialize(); |
| 15669 | let same_screen_bytes = input.same_screen.serialize(); |
| 15670 | let device_id_bytes = input.device_id.serialize(); |
| 15671 | [ |
| 15672 | response_type_bytes[0], |
| 15673 | detail_bytes[0], |
| 15674 | sequence_bytes[0], |
| 15675 | sequence_bytes[1], |
| 15676 | time_bytes[0], |
| 15677 | time_bytes[1], |
| 15678 | time_bytes[2], |
| 15679 | time_bytes[3], |
| 15680 | root_bytes[0], |
| 15681 | root_bytes[1], |
| 15682 | root_bytes[2], |
| 15683 | root_bytes[3], |
| 15684 | event_bytes[0], |
| 15685 | event_bytes[1], |
| 15686 | event_bytes[2], |
| 15687 | event_bytes[3], |
| 15688 | child_bytes[0], |
| 15689 | child_bytes[1], |
| 15690 | child_bytes[2], |
| 15691 | child_bytes[3], |
| 15692 | root_x_bytes[0], |
| 15693 | root_x_bytes[1], |
| 15694 | root_y_bytes[0], |
| 15695 | root_y_bytes[1], |
| 15696 | event_x_bytes[0], |
| 15697 | event_x_bytes[1], |
| 15698 | event_y_bytes[0], |
| 15699 | event_y_bytes[1], |
| 15700 | state_bytes[0], |
| 15701 | state_bytes[1], |
| 15702 | same_screen_bytes[0], |
| 15703 | device_id_bytes[0], |
| 15704 | ] |
| 15705 | } |
| 15706 | } |
| 15707 | impl From<DeviceKeyPressEvent> for [u8; 32] { |
| 15708 | fn from(input: DeviceKeyPressEvent) -> Self { |
| 15709 | Self::from(&input) |
| 15710 | } |
| 15711 | } |
| 15712 | |
| 15713 | /// Opcode for the DeviceKeyRelease event |
| 15714 | pub const DEVICE_KEY_RELEASE_EVENT: u8 = 2; |
| 15715 | pub type DeviceKeyReleaseEvent = DeviceKeyPressEvent; |
| 15716 | |
| 15717 | /// Opcode for the DeviceButtonPress event |
| 15718 | pub const DEVICE_BUTTON_PRESS_EVENT: u8 = 3; |
| 15719 | pub type DeviceButtonPressEvent = DeviceKeyPressEvent; |
| 15720 | |
| 15721 | /// Opcode for the DeviceButtonRelease event |
| 15722 | pub const DEVICE_BUTTON_RELEASE_EVENT: u8 = 4; |
| 15723 | pub type DeviceButtonReleaseEvent = DeviceKeyPressEvent; |
| 15724 | |
| 15725 | /// Opcode for the DeviceMotionNotify event |
| 15726 | pub const DEVICE_MOTION_NOTIFY_EVENT: u8 = 5; |
| 15727 | pub type DeviceMotionNotifyEvent = DeviceKeyPressEvent; |
| 15728 | |
| 15729 | /// Opcode for the DeviceFocusIn event |
| 15730 | pub const DEVICE_FOCUS_IN_EVENT: u8 = 6; |
| 15731 | #[derive (Clone, Copy, Default)] |
| 15732 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 15733 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 15734 | pub struct DeviceFocusInEvent { |
| 15735 | pub response_type: u8, |
| 15736 | pub detail: xproto::NotifyDetail, |
| 15737 | pub sequence: u16, |
| 15738 | pub time: xproto::Timestamp, |
| 15739 | pub window: xproto::Window, |
| 15740 | pub mode: xproto::NotifyMode, |
| 15741 | pub device_id: u8, |
| 15742 | } |
| 15743 | impl_debug_if_no_extra_traits!(DeviceFocusInEvent, "DeviceFocusInEvent" ); |
| 15744 | impl TryParse for DeviceFocusInEvent { |
| 15745 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 15746 | let remaining: &[u8] = initial_value; |
| 15747 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 15748 | let (detail: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 15749 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 15750 | let (time: u32, remaining: &[u8]) = xproto::Timestamp::try_parse(remaining)?; |
| 15751 | let (window: u32, remaining: &[u8]) = xproto::Window::try_parse(remaining)?; |
| 15752 | let (mode: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 15753 | let (device_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 15754 | let remaining: &[u8] = remaining.get(18..).ok_or(err:ParseError::InsufficientData)?; |
| 15755 | let detail: NotifyDetail = detail.into(); |
| 15756 | let mode: NotifyMode = mode.into(); |
| 15757 | let result: DeviceFocusInEvent = DeviceFocusInEvent { response_type, detail, sequence, time, window, mode, device_id }; |
| 15758 | let _ = remaining; |
| 15759 | let remaining: &[u8] = initial_value.get(32..) |
| 15760 | .ok_or(err:ParseError::InsufficientData)?; |
| 15761 | Ok((result, remaining)) |
| 15762 | } |
| 15763 | } |
| 15764 | impl Serialize for DeviceFocusInEvent { |
| 15765 | type Bytes = [u8; 32]; |
| 15766 | fn serialize(&self) -> [u8; 32] { |
| 15767 | let response_type_bytes = self.response_type.serialize(); |
| 15768 | let detail_bytes = u8::from(self.detail).serialize(); |
| 15769 | let sequence_bytes = self.sequence.serialize(); |
| 15770 | let time_bytes = self.time.serialize(); |
| 15771 | let window_bytes = self.window.serialize(); |
| 15772 | let mode_bytes = u8::from(self.mode).serialize(); |
| 15773 | let device_id_bytes = self.device_id.serialize(); |
| 15774 | [ |
| 15775 | response_type_bytes[0], |
| 15776 | detail_bytes[0], |
| 15777 | sequence_bytes[0], |
| 15778 | sequence_bytes[1], |
| 15779 | time_bytes[0], |
| 15780 | time_bytes[1], |
| 15781 | time_bytes[2], |
| 15782 | time_bytes[3], |
| 15783 | window_bytes[0], |
| 15784 | window_bytes[1], |
| 15785 | window_bytes[2], |
| 15786 | window_bytes[3], |
| 15787 | mode_bytes[0], |
| 15788 | device_id_bytes[0], |
| 15789 | 0, |
| 15790 | 0, |
| 15791 | 0, |
| 15792 | 0, |
| 15793 | 0, |
| 15794 | 0, |
| 15795 | 0, |
| 15796 | 0, |
| 15797 | 0, |
| 15798 | 0, |
| 15799 | 0, |
| 15800 | 0, |
| 15801 | 0, |
| 15802 | 0, |
| 15803 | 0, |
| 15804 | 0, |
| 15805 | 0, |
| 15806 | 0, |
| 15807 | ] |
| 15808 | } |
| 15809 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 15810 | bytes.reserve(32); |
| 15811 | self.response_type.serialize_into(bytes); |
| 15812 | u8::from(self.detail).serialize_into(bytes); |
| 15813 | self.sequence.serialize_into(bytes); |
| 15814 | self.time.serialize_into(bytes); |
| 15815 | self.window.serialize_into(bytes); |
| 15816 | u8::from(self.mode).serialize_into(bytes); |
| 15817 | self.device_id.serialize_into(bytes); |
| 15818 | bytes.extend_from_slice(&[0; 18]); |
| 15819 | } |
| 15820 | } |
| 15821 | impl From<&DeviceFocusInEvent> for [u8; 32] { |
| 15822 | fn from(input: &DeviceFocusInEvent) -> Self { |
| 15823 | let response_type_bytes = input.response_type.serialize(); |
| 15824 | let detail_bytes = u8::from(input.detail).serialize(); |
| 15825 | let sequence_bytes = input.sequence.serialize(); |
| 15826 | let time_bytes = input.time.serialize(); |
| 15827 | let window_bytes = input.window.serialize(); |
| 15828 | let mode_bytes = u8::from(input.mode).serialize(); |
| 15829 | let device_id_bytes = input.device_id.serialize(); |
| 15830 | [ |
| 15831 | response_type_bytes[0], |
| 15832 | detail_bytes[0], |
| 15833 | sequence_bytes[0], |
| 15834 | sequence_bytes[1], |
| 15835 | time_bytes[0], |
| 15836 | time_bytes[1], |
| 15837 | time_bytes[2], |
| 15838 | time_bytes[3], |
| 15839 | window_bytes[0], |
| 15840 | window_bytes[1], |
| 15841 | window_bytes[2], |
| 15842 | window_bytes[3], |
| 15843 | mode_bytes[0], |
| 15844 | device_id_bytes[0], |
| 15845 | 0, |
| 15846 | 0, |
| 15847 | 0, |
| 15848 | 0, |
| 15849 | 0, |
| 15850 | 0, |
| 15851 | 0, |
| 15852 | 0, |
| 15853 | 0, |
| 15854 | 0, |
| 15855 | 0, |
| 15856 | 0, |
| 15857 | 0, |
| 15858 | 0, |
| 15859 | 0, |
| 15860 | 0, |
| 15861 | 0, |
| 15862 | 0, |
| 15863 | ] |
| 15864 | } |
| 15865 | } |
| 15866 | impl From<DeviceFocusInEvent> for [u8; 32] { |
| 15867 | fn from(input: DeviceFocusInEvent) -> Self { |
| 15868 | Self::from(&input) |
| 15869 | } |
| 15870 | } |
| 15871 | |
| 15872 | /// Opcode for the DeviceFocusOut event |
| 15873 | pub const DEVICE_FOCUS_OUT_EVENT: u8 = 7; |
| 15874 | pub type DeviceFocusOutEvent = DeviceFocusInEvent; |
| 15875 | |
| 15876 | /// Opcode for the ProximityIn event |
| 15877 | pub const PROXIMITY_IN_EVENT: u8 = 8; |
| 15878 | pub type ProximityInEvent = DeviceKeyPressEvent; |
| 15879 | |
| 15880 | /// Opcode for the ProximityOut event |
| 15881 | pub const PROXIMITY_OUT_EVENT: u8 = 9; |
| 15882 | pub type ProximityOutEvent = DeviceKeyPressEvent; |
| 15883 | |
| 15884 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 15885 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 15886 | pub struct ClassesReportedMask(u8); |
| 15887 | impl ClassesReportedMask { |
| 15888 | pub const OUT_OF_PROXIMITY: Self = Self(1 << 7); |
| 15889 | pub const DEVICE_MODE_ABSOLUTE: Self = Self(1 << 6); |
| 15890 | pub const REPORTING_VALUATORS: Self = Self(1 << 2); |
| 15891 | pub const REPORTING_BUTTONS: Self = Self(1 << 1); |
| 15892 | pub const REPORTING_KEYS: Self = Self(1 << 0); |
| 15893 | } |
| 15894 | impl From<ClassesReportedMask> for u8 { |
| 15895 | #[inline ] |
| 15896 | fn from(input: ClassesReportedMask) -> Self { |
| 15897 | input.0 |
| 15898 | } |
| 15899 | } |
| 15900 | impl From<ClassesReportedMask> for Option<u8> { |
| 15901 | #[inline ] |
| 15902 | fn from(input: ClassesReportedMask) -> Self { |
| 15903 | Some(input.0) |
| 15904 | } |
| 15905 | } |
| 15906 | impl From<ClassesReportedMask> for u16 { |
| 15907 | #[inline ] |
| 15908 | fn from(input: ClassesReportedMask) -> Self { |
| 15909 | u16::from(input.0) |
| 15910 | } |
| 15911 | } |
| 15912 | impl From<ClassesReportedMask> for Option<u16> { |
| 15913 | #[inline ] |
| 15914 | fn from(input: ClassesReportedMask) -> Self { |
| 15915 | Some(u16::from(input.0)) |
| 15916 | } |
| 15917 | } |
| 15918 | impl From<ClassesReportedMask> for u32 { |
| 15919 | #[inline ] |
| 15920 | fn from(input: ClassesReportedMask) -> Self { |
| 15921 | u32::from(input.0) |
| 15922 | } |
| 15923 | } |
| 15924 | impl From<ClassesReportedMask> for Option<u32> { |
| 15925 | #[inline ] |
| 15926 | fn from(input: ClassesReportedMask) -> Self { |
| 15927 | Some(u32::from(input.0)) |
| 15928 | } |
| 15929 | } |
| 15930 | impl From<u8> for ClassesReportedMask { |
| 15931 | #[inline ] |
| 15932 | fn from(value: u8) -> Self { |
| 15933 | Self(value) |
| 15934 | } |
| 15935 | } |
| 15936 | impl core::fmt::Debug for ClassesReportedMask { |
| 15937 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 15938 | let variants: [(u32, &'static str, &'static …); 5] = [ |
| 15939 | (Self::OUT_OF_PROXIMITY.0.into(), "OUT_OF_PROXIMITY" , "OutOfProximity" ), |
| 15940 | (Self::DEVICE_MODE_ABSOLUTE.0.into(), "DEVICE_MODE_ABSOLUTE" , "DeviceModeAbsolute" ), |
| 15941 | (Self::REPORTING_VALUATORS.0.into(), "REPORTING_VALUATORS" , "ReportingValuators" ), |
| 15942 | (Self::REPORTING_BUTTONS.0.into(), "REPORTING_BUTTONS" , "ReportingButtons" ), |
| 15943 | (Self::REPORTING_KEYS.0.into(), "REPORTING_KEYS" , "ReportingKeys" ), |
| 15944 | ]; |
| 15945 | pretty_print_bitmask(fmt, self.0.into(), &variants) |
| 15946 | } |
| 15947 | } |
| 15948 | bitmask_binop!(ClassesReportedMask, u8); |
| 15949 | |
| 15950 | /// Opcode for the DeviceStateNotify event |
| 15951 | pub const DEVICE_STATE_NOTIFY_EVENT: u8 = 10; |
| 15952 | #[derive (Clone, Copy, Default)] |
| 15953 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 15954 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 15955 | pub struct DeviceStateNotifyEvent { |
| 15956 | pub response_type: u8, |
| 15957 | pub device_id: u8, |
| 15958 | pub sequence: u16, |
| 15959 | pub time: xproto::Timestamp, |
| 15960 | pub num_keys: u8, |
| 15961 | pub num_buttons: u8, |
| 15962 | pub num_valuators: u8, |
| 15963 | pub classes_reported: ClassesReportedMask, |
| 15964 | pub buttons: [u8; 4], |
| 15965 | pub keys: [u8; 4], |
| 15966 | pub valuators: [u32; 3], |
| 15967 | } |
| 15968 | impl_debug_if_no_extra_traits!(DeviceStateNotifyEvent, "DeviceStateNotifyEvent" ); |
| 15969 | impl TryParse for DeviceStateNotifyEvent { |
| 15970 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 15971 | let remaining = initial_value; |
| 15972 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 15973 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 15974 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 15975 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 15976 | let (num_keys, remaining) = u8::try_parse(remaining)?; |
| 15977 | let (num_buttons, remaining) = u8::try_parse(remaining)?; |
| 15978 | let (num_valuators, remaining) = u8::try_parse(remaining)?; |
| 15979 | let (classes_reported, remaining) = u8::try_parse(remaining)?; |
| 15980 | let (buttons, remaining) = crate::x11_utils::parse_u8_array::<4>(remaining)?; |
| 15981 | let (keys, remaining) = crate::x11_utils::parse_u8_array::<4>(remaining)?; |
| 15982 | let (valuators_0, remaining) = u32::try_parse(remaining)?; |
| 15983 | let (valuators_1, remaining) = u32::try_parse(remaining)?; |
| 15984 | let (valuators_2, remaining) = u32::try_parse(remaining)?; |
| 15985 | let valuators = [ |
| 15986 | valuators_0, |
| 15987 | valuators_1, |
| 15988 | valuators_2, |
| 15989 | ]; |
| 15990 | let classes_reported = classes_reported.into(); |
| 15991 | let result = DeviceStateNotifyEvent { response_type, device_id, sequence, time, num_keys, num_buttons, num_valuators, classes_reported, buttons, keys, valuators }; |
| 15992 | let _ = remaining; |
| 15993 | let remaining = initial_value.get(32..) |
| 15994 | .ok_or(ParseError::InsufficientData)?; |
| 15995 | Ok((result, remaining)) |
| 15996 | } |
| 15997 | } |
| 15998 | impl Serialize for DeviceStateNotifyEvent { |
| 15999 | type Bytes = [u8; 32]; |
| 16000 | fn serialize(&self) -> [u8; 32] { |
| 16001 | let response_type_bytes = self.response_type.serialize(); |
| 16002 | let device_id_bytes = self.device_id.serialize(); |
| 16003 | let sequence_bytes = self.sequence.serialize(); |
| 16004 | let time_bytes = self.time.serialize(); |
| 16005 | let num_keys_bytes = self.num_keys.serialize(); |
| 16006 | let num_buttons_bytes = self.num_buttons.serialize(); |
| 16007 | let num_valuators_bytes = self.num_valuators.serialize(); |
| 16008 | let classes_reported_bytes = u8::from(self.classes_reported).serialize(); |
| 16009 | let valuators_0_bytes = self.valuators[0].serialize(); |
| 16010 | let valuators_1_bytes = self.valuators[1].serialize(); |
| 16011 | let valuators_2_bytes = self.valuators[2].serialize(); |
| 16012 | [ |
| 16013 | response_type_bytes[0], |
| 16014 | device_id_bytes[0], |
| 16015 | sequence_bytes[0], |
| 16016 | sequence_bytes[1], |
| 16017 | time_bytes[0], |
| 16018 | time_bytes[1], |
| 16019 | time_bytes[2], |
| 16020 | time_bytes[3], |
| 16021 | num_keys_bytes[0], |
| 16022 | num_buttons_bytes[0], |
| 16023 | num_valuators_bytes[0], |
| 16024 | classes_reported_bytes[0], |
| 16025 | self.buttons[0], |
| 16026 | self.buttons[1], |
| 16027 | self.buttons[2], |
| 16028 | self.buttons[3], |
| 16029 | self.keys[0], |
| 16030 | self.keys[1], |
| 16031 | self.keys[2], |
| 16032 | self.keys[3], |
| 16033 | valuators_0_bytes[0], |
| 16034 | valuators_0_bytes[1], |
| 16035 | valuators_0_bytes[2], |
| 16036 | valuators_0_bytes[3], |
| 16037 | valuators_1_bytes[0], |
| 16038 | valuators_1_bytes[1], |
| 16039 | valuators_1_bytes[2], |
| 16040 | valuators_1_bytes[3], |
| 16041 | valuators_2_bytes[0], |
| 16042 | valuators_2_bytes[1], |
| 16043 | valuators_2_bytes[2], |
| 16044 | valuators_2_bytes[3], |
| 16045 | ] |
| 16046 | } |
| 16047 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 16048 | bytes.reserve(32); |
| 16049 | self.response_type.serialize_into(bytes); |
| 16050 | self.device_id.serialize_into(bytes); |
| 16051 | self.sequence.serialize_into(bytes); |
| 16052 | self.time.serialize_into(bytes); |
| 16053 | self.num_keys.serialize_into(bytes); |
| 16054 | self.num_buttons.serialize_into(bytes); |
| 16055 | self.num_valuators.serialize_into(bytes); |
| 16056 | u8::from(self.classes_reported).serialize_into(bytes); |
| 16057 | bytes.extend_from_slice(&self.buttons); |
| 16058 | bytes.extend_from_slice(&self.keys); |
| 16059 | self.valuators.serialize_into(bytes); |
| 16060 | } |
| 16061 | } |
| 16062 | impl From<&DeviceStateNotifyEvent> for [u8; 32] { |
| 16063 | fn from(input: &DeviceStateNotifyEvent) -> Self { |
| 16064 | let response_type_bytes = input.response_type.serialize(); |
| 16065 | let device_id_bytes = input.device_id.serialize(); |
| 16066 | let sequence_bytes = input.sequence.serialize(); |
| 16067 | let time_bytes = input.time.serialize(); |
| 16068 | let num_keys_bytes = input.num_keys.serialize(); |
| 16069 | let num_buttons_bytes = input.num_buttons.serialize(); |
| 16070 | let num_valuators_bytes = input.num_valuators.serialize(); |
| 16071 | let classes_reported_bytes = u8::from(input.classes_reported).serialize(); |
| 16072 | let valuators_0_bytes = input.valuators[0].serialize(); |
| 16073 | let valuators_1_bytes = input.valuators[1].serialize(); |
| 16074 | let valuators_2_bytes = input.valuators[2].serialize(); |
| 16075 | [ |
| 16076 | response_type_bytes[0], |
| 16077 | device_id_bytes[0], |
| 16078 | sequence_bytes[0], |
| 16079 | sequence_bytes[1], |
| 16080 | time_bytes[0], |
| 16081 | time_bytes[1], |
| 16082 | time_bytes[2], |
| 16083 | time_bytes[3], |
| 16084 | num_keys_bytes[0], |
| 16085 | num_buttons_bytes[0], |
| 16086 | num_valuators_bytes[0], |
| 16087 | classes_reported_bytes[0], |
| 16088 | input.buttons[0], |
| 16089 | input.buttons[1], |
| 16090 | input.buttons[2], |
| 16091 | input.buttons[3], |
| 16092 | input.keys[0], |
| 16093 | input.keys[1], |
| 16094 | input.keys[2], |
| 16095 | input.keys[3], |
| 16096 | valuators_0_bytes[0], |
| 16097 | valuators_0_bytes[1], |
| 16098 | valuators_0_bytes[2], |
| 16099 | valuators_0_bytes[3], |
| 16100 | valuators_1_bytes[0], |
| 16101 | valuators_1_bytes[1], |
| 16102 | valuators_1_bytes[2], |
| 16103 | valuators_1_bytes[3], |
| 16104 | valuators_2_bytes[0], |
| 16105 | valuators_2_bytes[1], |
| 16106 | valuators_2_bytes[2], |
| 16107 | valuators_2_bytes[3], |
| 16108 | ] |
| 16109 | } |
| 16110 | } |
| 16111 | impl From<DeviceStateNotifyEvent> for [u8; 32] { |
| 16112 | fn from(input: DeviceStateNotifyEvent) -> Self { |
| 16113 | Self::from(&input) |
| 16114 | } |
| 16115 | } |
| 16116 | |
| 16117 | /// Opcode for the DeviceMappingNotify event |
| 16118 | pub const DEVICE_MAPPING_NOTIFY_EVENT: u8 = 11; |
| 16119 | #[derive (Clone, Copy, Default)] |
| 16120 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 16121 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 16122 | pub struct DeviceMappingNotifyEvent { |
| 16123 | pub response_type: u8, |
| 16124 | pub device_id: u8, |
| 16125 | pub sequence: u16, |
| 16126 | pub request: xproto::Mapping, |
| 16127 | pub first_keycode: KeyCode, |
| 16128 | pub count: u8, |
| 16129 | pub time: xproto::Timestamp, |
| 16130 | } |
| 16131 | impl_debug_if_no_extra_traits!(DeviceMappingNotifyEvent, "DeviceMappingNotifyEvent" ); |
| 16132 | impl TryParse for DeviceMappingNotifyEvent { |
| 16133 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 16134 | let remaining: &[u8] = initial_value; |
| 16135 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16136 | let (device_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16137 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 16138 | let (request: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16139 | let (first_keycode: u8, remaining: &[u8]) = KeyCode::try_parse(remaining)?; |
| 16140 | let (count: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16141 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 16142 | let (time: u32, remaining: &[u8]) = xproto::Timestamp::try_parse(remaining)?; |
| 16143 | let remaining: &[u8] = remaining.get(20..).ok_or(err:ParseError::InsufficientData)?; |
| 16144 | let request: Mapping = request.into(); |
| 16145 | let result: DeviceMappingNotifyEvent = DeviceMappingNotifyEvent { response_type, device_id, sequence, request, first_keycode, count, time }; |
| 16146 | let _ = remaining; |
| 16147 | let remaining: &[u8] = initial_value.get(32..) |
| 16148 | .ok_or(err:ParseError::InsufficientData)?; |
| 16149 | Ok((result, remaining)) |
| 16150 | } |
| 16151 | } |
| 16152 | impl Serialize for DeviceMappingNotifyEvent { |
| 16153 | type Bytes = [u8; 32]; |
| 16154 | fn serialize(&self) -> [u8; 32] { |
| 16155 | let response_type_bytes = self.response_type.serialize(); |
| 16156 | let device_id_bytes = self.device_id.serialize(); |
| 16157 | let sequence_bytes = self.sequence.serialize(); |
| 16158 | let request_bytes = u8::from(self.request).serialize(); |
| 16159 | let first_keycode_bytes = self.first_keycode.serialize(); |
| 16160 | let count_bytes = self.count.serialize(); |
| 16161 | let time_bytes = self.time.serialize(); |
| 16162 | [ |
| 16163 | response_type_bytes[0], |
| 16164 | device_id_bytes[0], |
| 16165 | sequence_bytes[0], |
| 16166 | sequence_bytes[1], |
| 16167 | request_bytes[0], |
| 16168 | first_keycode_bytes[0], |
| 16169 | count_bytes[0], |
| 16170 | 0, |
| 16171 | time_bytes[0], |
| 16172 | time_bytes[1], |
| 16173 | time_bytes[2], |
| 16174 | time_bytes[3], |
| 16175 | 0, |
| 16176 | 0, |
| 16177 | 0, |
| 16178 | 0, |
| 16179 | 0, |
| 16180 | 0, |
| 16181 | 0, |
| 16182 | 0, |
| 16183 | 0, |
| 16184 | 0, |
| 16185 | 0, |
| 16186 | 0, |
| 16187 | 0, |
| 16188 | 0, |
| 16189 | 0, |
| 16190 | 0, |
| 16191 | 0, |
| 16192 | 0, |
| 16193 | 0, |
| 16194 | 0, |
| 16195 | ] |
| 16196 | } |
| 16197 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 16198 | bytes.reserve(32); |
| 16199 | self.response_type.serialize_into(bytes); |
| 16200 | self.device_id.serialize_into(bytes); |
| 16201 | self.sequence.serialize_into(bytes); |
| 16202 | u8::from(self.request).serialize_into(bytes); |
| 16203 | self.first_keycode.serialize_into(bytes); |
| 16204 | self.count.serialize_into(bytes); |
| 16205 | bytes.extend_from_slice(&[0; 1]); |
| 16206 | self.time.serialize_into(bytes); |
| 16207 | bytes.extend_from_slice(&[0; 20]); |
| 16208 | } |
| 16209 | } |
| 16210 | impl From<&DeviceMappingNotifyEvent> for [u8; 32] { |
| 16211 | fn from(input: &DeviceMappingNotifyEvent) -> Self { |
| 16212 | let response_type_bytes = input.response_type.serialize(); |
| 16213 | let device_id_bytes = input.device_id.serialize(); |
| 16214 | let sequence_bytes = input.sequence.serialize(); |
| 16215 | let request_bytes = u8::from(input.request).serialize(); |
| 16216 | let first_keycode_bytes = input.first_keycode.serialize(); |
| 16217 | let count_bytes = input.count.serialize(); |
| 16218 | let time_bytes = input.time.serialize(); |
| 16219 | [ |
| 16220 | response_type_bytes[0], |
| 16221 | device_id_bytes[0], |
| 16222 | sequence_bytes[0], |
| 16223 | sequence_bytes[1], |
| 16224 | request_bytes[0], |
| 16225 | first_keycode_bytes[0], |
| 16226 | count_bytes[0], |
| 16227 | 0, |
| 16228 | time_bytes[0], |
| 16229 | time_bytes[1], |
| 16230 | time_bytes[2], |
| 16231 | time_bytes[3], |
| 16232 | 0, |
| 16233 | 0, |
| 16234 | 0, |
| 16235 | 0, |
| 16236 | 0, |
| 16237 | 0, |
| 16238 | 0, |
| 16239 | 0, |
| 16240 | 0, |
| 16241 | 0, |
| 16242 | 0, |
| 16243 | 0, |
| 16244 | 0, |
| 16245 | 0, |
| 16246 | 0, |
| 16247 | 0, |
| 16248 | 0, |
| 16249 | 0, |
| 16250 | 0, |
| 16251 | 0, |
| 16252 | ] |
| 16253 | } |
| 16254 | } |
| 16255 | impl From<DeviceMappingNotifyEvent> for [u8; 32] { |
| 16256 | fn from(input: DeviceMappingNotifyEvent) -> Self { |
| 16257 | Self::from(&input) |
| 16258 | } |
| 16259 | } |
| 16260 | |
| 16261 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 16262 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 16263 | pub struct ChangeDevice(u8); |
| 16264 | impl ChangeDevice { |
| 16265 | pub const NEW_POINTER: Self = Self(0); |
| 16266 | pub const NEW_KEYBOARD: Self = Self(1); |
| 16267 | } |
| 16268 | impl From<ChangeDevice> for u8 { |
| 16269 | #[inline ] |
| 16270 | fn from(input: ChangeDevice) -> Self { |
| 16271 | input.0 |
| 16272 | } |
| 16273 | } |
| 16274 | impl From<ChangeDevice> for Option<u8> { |
| 16275 | #[inline ] |
| 16276 | fn from(input: ChangeDevice) -> Self { |
| 16277 | Some(input.0) |
| 16278 | } |
| 16279 | } |
| 16280 | impl From<ChangeDevice> for u16 { |
| 16281 | #[inline ] |
| 16282 | fn from(input: ChangeDevice) -> Self { |
| 16283 | u16::from(input.0) |
| 16284 | } |
| 16285 | } |
| 16286 | impl From<ChangeDevice> for Option<u16> { |
| 16287 | #[inline ] |
| 16288 | fn from(input: ChangeDevice) -> Self { |
| 16289 | Some(u16::from(input.0)) |
| 16290 | } |
| 16291 | } |
| 16292 | impl From<ChangeDevice> for u32 { |
| 16293 | #[inline ] |
| 16294 | fn from(input: ChangeDevice) -> Self { |
| 16295 | u32::from(input.0) |
| 16296 | } |
| 16297 | } |
| 16298 | impl From<ChangeDevice> for Option<u32> { |
| 16299 | #[inline ] |
| 16300 | fn from(input: ChangeDevice) -> Self { |
| 16301 | Some(u32::from(input.0)) |
| 16302 | } |
| 16303 | } |
| 16304 | impl From<u8> for ChangeDevice { |
| 16305 | #[inline ] |
| 16306 | fn from(value: u8) -> Self { |
| 16307 | Self(value) |
| 16308 | } |
| 16309 | } |
| 16310 | impl core::fmt::Debug for ChangeDevice { |
| 16311 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 16312 | let variants: [(u32, &'static str, &'static …); 2] = [ |
| 16313 | (Self::NEW_POINTER.0.into(), "NEW_POINTER" , "NewPointer" ), |
| 16314 | (Self::NEW_KEYBOARD.0.into(), "NEW_KEYBOARD" , "NewKeyboard" ), |
| 16315 | ]; |
| 16316 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 16317 | } |
| 16318 | } |
| 16319 | |
| 16320 | /// Opcode for the ChangeDeviceNotify event |
| 16321 | pub const CHANGE_DEVICE_NOTIFY_EVENT: u8 = 12; |
| 16322 | #[derive (Clone, Copy, Default)] |
| 16323 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 16324 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 16325 | pub struct ChangeDeviceNotifyEvent { |
| 16326 | pub response_type: u8, |
| 16327 | pub device_id: u8, |
| 16328 | pub sequence: u16, |
| 16329 | pub time: xproto::Timestamp, |
| 16330 | pub request: ChangeDevice, |
| 16331 | } |
| 16332 | impl_debug_if_no_extra_traits!(ChangeDeviceNotifyEvent, "ChangeDeviceNotifyEvent" ); |
| 16333 | impl TryParse for ChangeDeviceNotifyEvent { |
| 16334 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 16335 | let remaining: &[u8] = initial_value; |
| 16336 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16337 | let (device_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16338 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 16339 | let (time: u32, remaining: &[u8]) = xproto::Timestamp::try_parse(remaining)?; |
| 16340 | let (request: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16341 | let remaining: &[u8] = remaining.get(23..).ok_or(err:ParseError::InsufficientData)?; |
| 16342 | let request: ChangeDevice = request.into(); |
| 16343 | let result: ChangeDeviceNotifyEvent = ChangeDeviceNotifyEvent { response_type, device_id, sequence, time, request }; |
| 16344 | let _ = remaining; |
| 16345 | let remaining: &[u8] = initial_value.get(32..) |
| 16346 | .ok_or(err:ParseError::InsufficientData)?; |
| 16347 | Ok((result, remaining)) |
| 16348 | } |
| 16349 | } |
| 16350 | impl Serialize for ChangeDeviceNotifyEvent { |
| 16351 | type Bytes = [u8; 32]; |
| 16352 | fn serialize(&self) -> [u8; 32] { |
| 16353 | let response_type_bytes = self.response_type.serialize(); |
| 16354 | let device_id_bytes = self.device_id.serialize(); |
| 16355 | let sequence_bytes = self.sequence.serialize(); |
| 16356 | let time_bytes = self.time.serialize(); |
| 16357 | let request_bytes = u8::from(self.request).serialize(); |
| 16358 | [ |
| 16359 | response_type_bytes[0], |
| 16360 | device_id_bytes[0], |
| 16361 | sequence_bytes[0], |
| 16362 | sequence_bytes[1], |
| 16363 | time_bytes[0], |
| 16364 | time_bytes[1], |
| 16365 | time_bytes[2], |
| 16366 | time_bytes[3], |
| 16367 | request_bytes[0], |
| 16368 | 0, |
| 16369 | 0, |
| 16370 | 0, |
| 16371 | 0, |
| 16372 | 0, |
| 16373 | 0, |
| 16374 | 0, |
| 16375 | 0, |
| 16376 | 0, |
| 16377 | 0, |
| 16378 | 0, |
| 16379 | 0, |
| 16380 | 0, |
| 16381 | 0, |
| 16382 | 0, |
| 16383 | 0, |
| 16384 | 0, |
| 16385 | 0, |
| 16386 | 0, |
| 16387 | 0, |
| 16388 | 0, |
| 16389 | 0, |
| 16390 | 0, |
| 16391 | ] |
| 16392 | } |
| 16393 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 16394 | bytes.reserve(32); |
| 16395 | self.response_type.serialize_into(bytes); |
| 16396 | self.device_id.serialize_into(bytes); |
| 16397 | self.sequence.serialize_into(bytes); |
| 16398 | self.time.serialize_into(bytes); |
| 16399 | u8::from(self.request).serialize_into(bytes); |
| 16400 | bytes.extend_from_slice(&[0; 23]); |
| 16401 | } |
| 16402 | } |
| 16403 | impl From<&ChangeDeviceNotifyEvent> for [u8; 32] { |
| 16404 | fn from(input: &ChangeDeviceNotifyEvent) -> Self { |
| 16405 | let response_type_bytes = input.response_type.serialize(); |
| 16406 | let device_id_bytes = input.device_id.serialize(); |
| 16407 | let sequence_bytes = input.sequence.serialize(); |
| 16408 | let time_bytes = input.time.serialize(); |
| 16409 | let request_bytes = u8::from(input.request).serialize(); |
| 16410 | [ |
| 16411 | response_type_bytes[0], |
| 16412 | device_id_bytes[0], |
| 16413 | sequence_bytes[0], |
| 16414 | sequence_bytes[1], |
| 16415 | time_bytes[0], |
| 16416 | time_bytes[1], |
| 16417 | time_bytes[2], |
| 16418 | time_bytes[3], |
| 16419 | request_bytes[0], |
| 16420 | 0, |
| 16421 | 0, |
| 16422 | 0, |
| 16423 | 0, |
| 16424 | 0, |
| 16425 | 0, |
| 16426 | 0, |
| 16427 | 0, |
| 16428 | 0, |
| 16429 | 0, |
| 16430 | 0, |
| 16431 | 0, |
| 16432 | 0, |
| 16433 | 0, |
| 16434 | 0, |
| 16435 | 0, |
| 16436 | 0, |
| 16437 | 0, |
| 16438 | 0, |
| 16439 | 0, |
| 16440 | 0, |
| 16441 | 0, |
| 16442 | 0, |
| 16443 | ] |
| 16444 | } |
| 16445 | } |
| 16446 | impl From<ChangeDeviceNotifyEvent> for [u8; 32] { |
| 16447 | fn from(input: ChangeDeviceNotifyEvent) -> Self { |
| 16448 | Self::from(&input) |
| 16449 | } |
| 16450 | } |
| 16451 | |
| 16452 | /// Opcode for the DeviceKeyStateNotify event |
| 16453 | pub const DEVICE_KEY_STATE_NOTIFY_EVENT: u8 = 13; |
| 16454 | #[derive (Clone, Copy, Default)] |
| 16455 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 16456 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 16457 | pub struct DeviceKeyStateNotifyEvent { |
| 16458 | pub response_type: u8, |
| 16459 | pub device_id: u8, |
| 16460 | pub sequence: u16, |
| 16461 | pub keys: [u8; 28], |
| 16462 | } |
| 16463 | impl_debug_if_no_extra_traits!(DeviceKeyStateNotifyEvent, "DeviceKeyStateNotifyEvent" ); |
| 16464 | impl TryParse for DeviceKeyStateNotifyEvent { |
| 16465 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 16466 | let remaining: &[u8] = initial_value; |
| 16467 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16468 | let (device_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16469 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 16470 | let (keys: [u8; 28], remaining: &[u8]) = crate::x11_utils::parse_u8_array::<28>(data:remaining)?; |
| 16471 | let result: DeviceKeyStateNotifyEvent = DeviceKeyStateNotifyEvent { response_type, device_id, sequence, keys }; |
| 16472 | let _ = remaining; |
| 16473 | let remaining: &[u8] = initial_value.get(32..) |
| 16474 | .ok_or(err:ParseError::InsufficientData)?; |
| 16475 | Ok((result, remaining)) |
| 16476 | } |
| 16477 | } |
| 16478 | impl Serialize for DeviceKeyStateNotifyEvent { |
| 16479 | type Bytes = [u8; 32]; |
| 16480 | fn serialize(&self) -> [u8; 32] { |
| 16481 | let response_type_bytes = self.response_type.serialize(); |
| 16482 | let device_id_bytes = self.device_id.serialize(); |
| 16483 | let sequence_bytes = self.sequence.serialize(); |
| 16484 | [ |
| 16485 | response_type_bytes[0], |
| 16486 | device_id_bytes[0], |
| 16487 | sequence_bytes[0], |
| 16488 | sequence_bytes[1], |
| 16489 | self.keys[0], |
| 16490 | self.keys[1], |
| 16491 | self.keys[2], |
| 16492 | self.keys[3], |
| 16493 | self.keys[4], |
| 16494 | self.keys[5], |
| 16495 | self.keys[6], |
| 16496 | self.keys[7], |
| 16497 | self.keys[8], |
| 16498 | self.keys[9], |
| 16499 | self.keys[10], |
| 16500 | self.keys[11], |
| 16501 | self.keys[12], |
| 16502 | self.keys[13], |
| 16503 | self.keys[14], |
| 16504 | self.keys[15], |
| 16505 | self.keys[16], |
| 16506 | self.keys[17], |
| 16507 | self.keys[18], |
| 16508 | self.keys[19], |
| 16509 | self.keys[20], |
| 16510 | self.keys[21], |
| 16511 | self.keys[22], |
| 16512 | self.keys[23], |
| 16513 | self.keys[24], |
| 16514 | self.keys[25], |
| 16515 | self.keys[26], |
| 16516 | self.keys[27], |
| 16517 | ] |
| 16518 | } |
| 16519 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 16520 | bytes.reserve(32); |
| 16521 | self.response_type.serialize_into(bytes); |
| 16522 | self.device_id.serialize_into(bytes); |
| 16523 | self.sequence.serialize_into(bytes); |
| 16524 | bytes.extend_from_slice(&self.keys); |
| 16525 | } |
| 16526 | } |
| 16527 | impl From<&DeviceKeyStateNotifyEvent> for [u8; 32] { |
| 16528 | fn from(input: &DeviceKeyStateNotifyEvent) -> Self { |
| 16529 | let response_type_bytes = input.response_type.serialize(); |
| 16530 | let device_id_bytes = input.device_id.serialize(); |
| 16531 | let sequence_bytes = input.sequence.serialize(); |
| 16532 | [ |
| 16533 | response_type_bytes[0], |
| 16534 | device_id_bytes[0], |
| 16535 | sequence_bytes[0], |
| 16536 | sequence_bytes[1], |
| 16537 | input.keys[0], |
| 16538 | input.keys[1], |
| 16539 | input.keys[2], |
| 16540 | input.keys[3], |
| 16541 | input.keys[4], |
| 16542 | input.keys[5], |
| 16543 | input.keys[6], |
| 16544 | input.keys[7], |
| 16545 | input.keys[8], |
| 16546 | input.keys[9], |
| 16547 | input.keys[10], |
| 16548 | input.keys[11], |
| 16549 | input.keys[12], |
| 16550 | input.keys[13], |
| 16551 | input.keys[14], |
| 16552 | input.keys[15], |
| 16553 | input.keys[16], |
| 16554 | input.keys[17], |
| 16555 | input.keys[18], |
| 16556 | input.keys[19], |
| 16557 | input.keys[20], |
| 16558 | input.keys[21], |
| 16559 | input.keys[22], |
| 16560 | input.keys[23], |
| 16561 | input.keys[24], |
| 16562 | input.keys[25], |
| 16563 | input.keys[26], |
| 16564 | input.keys[27], |
| 16565 | ] |
| 16566 | } |
| 16567 | } |
| 16568 | impl From<DeviceKeyStateNotifyEvent> for [u8; 32] { |
| 16569 | fn from(input: DeviceKeyStateNotifyEvent) -> Self { |
| 16570 | Self::from(&input) |
| 16571 | } |
| 16572 | } |
| 16573 | |
| 16574 | /// Opcode for the DeviceButtonStateNotify event |
| 16575 | pub const DEVICE_BUTTON_STATE_NOTIFY_EVENT: u8 = 14; |
| 16576 | #[derive (Clone, Copy, Default)] |
| 16577 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 16578 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 16579 | pub struct DeviceButtonStateNotifyEvent { |
| 16580 | pub response_type: u8, |
| 16581 | pub device_id: u8, |
| 16582 | pub sequence: u16, |
| 16583 | pub buttons: [u8; 28], |
| 16584 | } |
| 16585 | impl_debug_if_no_extra_traits!(DeviceButtonStateNotifyEvent, "DeviceButtonStateNotifyEvent" ); |
| 16586 | impl TryParse for DeviceButtonStateNotifyEvent { |
| 16587 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 16588 | let remaining: &[u8] = initial_value; |
| 16589 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16590 | let (device_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16591 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 16592 | let (buttons: [u8; 28], remaining: &[u8]) = crate::x11_utils::parse_u8_array::<28>(data:remaining)?; |
| 16593 | let result: DeviceButtonStateNotifyEvent = DeviceButtonStateNotifyEvent { response_type, device_id, sequence, buttons }; |
| 16594 | let _ = remaining; |
| 16595 | let remaining: &[u8] = initial_value.get(32..) |
| 16596 | .ok_or(err:ParseError::InsufficientData)?; |
| 16597 | Ok((result, remaining)) |
| 16598 | } |
| 16599 | } |
| 16600 | impl Serialize for DeviceButtonStateNotifyEvent { |
| 16601 | type Bytes = [u8; 32]; |
| 16602 | fn serialize(&self) -> [u8; 32] { |
| 16603 | let response_type_bytes = self.response_type.serialize(); |
| 16604 | let device_id_bytes = self.device_id.serialize(); |
| 16605 | let sequence_bytes = self.sequence.serialize(); |
| 16606 | [ |
| 16607 | response_type_bytes[0], |
| 16608 | device_id_bytes[0], |
| 16609 | sequence_bytes[0], |
| 16610 | sequence_bytes[1], |
| 16611 | self.buttons[0], |
| 16612 | self.buttons[1], |
| 16613 | self.buttons[2], |
| 16614 | self.buttons[3], |
| 16615 | self.buttons[4], |
| 16616 | self.buttons[5], |
| 16617 | self.buttons[6], |
| 16618 | self.buttons[7], |
| 16619 | self.buttons[8], |
| 16620 | self.buttons[9], |
| 16621 | self.buttons[10], |
| 16622 | self.buttons[11], |
| 16623 | self.buttons[12], |
| 16624 | self.buttons[13], |
| 16625 | self.buttons[14], |
| 16626 | self.buttons[15], |
| 16627 | self.buttons[16], |
| 16628 | self.buttons[17], |
| 16629 | self.buttons[18], |
| 16630 | self.buttons[19], |
| 16631 | self.buttons[20], |
| 16632 | self.buttons[21], |
| 16633 | self.buttons[22], |
| 16634 | self.buttons[23], |
| 16635 | self.buttons[24], |
| 16636 | self.buttons[25], |
| 16637 | self.buttons[26], |
| 16638 | self.buttons[27], |
| 16639 | ] |
| 16640 | } |
| 16641 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 16642 | bytes.reserve(32); |
| 16643 | self.response_type.serialize_into(bytes); |
| 16644 | self.device_id.serialize_into(bytes); |
| 16645 | self.sequence.serialize_into(bytes); |
| 16646 | bytes.extend_from_slice(&self.buttons); |
| 16647 | } |
| 16648 | } |
| 16649 | impl From<&DeviceButtonStateNotifyEvent> for [u8; 32] { |
| 16650 | fn from(input: &DeviceButtonStateNotifyEvent) -> Self { |
| 16651 | let response_type_bytes = input.response_type.serialize(); |
| 16652 | let device_id_bytes = input.device_id.serialize(); |
| 16653 | let sequence_bytes = input.sequence.serialize(); |
| 16654 | [ |
| 16655 | response_type_bytes[0], |
| 16656 | device_id_bytes[0], |
| 16657 | sequence_bytes[0], |
| 16658 | sequence_bytes[1], |
| 16659 | input.buttons[0], |
| 16660 | input.buttons[1], |
| 16661 | input.buttons[2], |
| 16662 | input.buttons[3], |
| 16663 | input.buttons[4], |
| 16664 | input.buttons[5], |
| 16665 | input.buttons[6], |
| 16666 | input.buttons[7], |
| 16667 | input.buttons[8], |
| 16668 | input.buttons[9], |
| 16669 | input.buttons[10], |
| 16670 | input.buttons[11], |
| 16671 | input.buttons[12], |
| 16672 | input.buttons[13], |
| 16673 | input.buttons[14], |
| 16674 | input.buttons[15], |
| 16675 | input.buttons[16], |
| 16676 | input.buttons[17], |
| 16677 | input.buttons[18], |
| 16678 | input.buttons[19], |
| 16679 | input.buttons[20], |
| 16680 | input.buttons[21], |
| 16681 | input.buttons[22], |
| 16682 | input.buttons[23], |
| 16683 | input.buttons[24], |
| 16684 | input.buttons[25], |
| 16685 | input.buttons[26], |
| 16686 | input.buttons[27], |
| 16687 | ] |
| 16688 | } |
| 16689 | } |
| 16690 | impl From<DeviceButtonStateNotifyEvent> for [u8; 32] { |
| 16691 | fn from(input: DeviceButtonStateNotifyEvent) -> Self { |
| 16692 | Self::from(&input) |
| 16693 | } |
| 16694 | } |
| 16695 | |
| 16696 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 16697 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 16698 | pub struct DeviceChange(u8); |
| 16699 | impl DeviceChange { |
| 16700 | pub const ADDED: Self = Self(0); |
| 16701 | pub const REMOVED: Self = Self(1); |
| 16702 | pub const ENABLED: Self = Self(2); |
| 16703 | pub const DISABLED: Self = Self(3); |
| 16704 | pub const UNRECOVERABLE: Self = Self(4); |
| 16705 | pub const CONTROL_CHANGED: Self = Self(5); |
| 16706 | } |
| 16707 | impl From<DeviceChange> for u8 { |
| 16708 | #[inline ] |
| 16709 | fn from(input: DeviceChange) -> Self { |
| 16710 | input.0 |
| 16711 | } |
| 16712 | } |
| 16713 | impl From<DeviceChange> for Option<u8> { |
| 16714 | #[inline ] |
| 16715 | fn from(input: DeviceChange) -> Self { |
| 16716 | Some(input.0) |
| 16717 | } |
| 16718 | } |
| 16719 | impl From<DeviceChange> for u16 { |
| 16720 | #[inline ] |
| 16721 | fn from(input: DeviceChange) -> Self { |
| 16722 | u16::from(input.0) |
| 16723 | } |
| 16724 | } |
| 16725 | impl From<DeviceChange> for Option<u16> { |
| 16726 | #[inline ] |
| 16727 | fn from(input: DeviceChange) -> Self { |
| 16728 | Some(u16::from(input.0)) |
| 16729 | } |
| 16730 | } |
| 16731 | impl From<DeviceChange> for u32 { |
| 16732 | #[inline ] |
| 16733 | fn from(input: DeviceChange) -> Self { |
| 16734 | u32::from(input.0) |
| 16735 | } |
| 16736 | } |
| 16737 | impl From<DeviceChange> for Option<u32> { |
| 16738 | #[inline ] |
| 16739 | fn from(input: DeviceChange) -> Self { |
| 16740 | Some(u32::from(input.0)) |
| 16741 | } |
| 16742 | } |
| 16743 | impl From<u8> for DeviceChange { |
| 16744 | #[inline ] |
| 16745 | fn from(value: u8) -> Self { |
| 16746 | Self(value) |
| 16747 | } |
| 16748 | } |
| 16749 | impl core::fmt::Debug for DeviceChange { |
| 16750 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 16751 | let variants: [(u32, &'static str, &'static …); 6] = [ |
| 16752 | (Self::ADDED.0.into(), "ADDED" , "Added" ), |
| 16753 | (Self::REMOVED.0.into(), "REMOVED" , "Removed" ), |
| 16754 | (Self::ENABLED.0.into(), "ENABLED" , "Enabled" ), |
| 16755 | (Self::DISABLED.0.into(), "DISABLED" , "Disabled" ), |
| 16756 | (Self::UNRECOVERABLE.0.into(), "UNRECOVERABLE" , "Unrecoverable" ), |
| 16757 | (Self::CONTROL_CHANGED.0.into(), "CONTROL_CHANGED" , "ControlChanged" ), |
| 16758 | ]; |
| 16759 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 16760 | } |
| 16761 | } |
| 16762 | |
| 16763 | /// Opcode for the DevicePresenceNotify event |
| 16764 | pub const DEVICE_PRESENCE_NOTIFY_EVENT: u8 = 15; |
| 16765 | #[derive (Clone, Copy, Default)] |
| 16766 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 16767 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 16768 | pub struct DevicePresenceNotifyEvent { |
| 16769 | pub response_type: u8, |
| 16770 | pub sequence: u16, |
| 16771 | pub time: xproto::Timestamp, |
| 16772 | pub devchange: DeviceChange, |
| 16773 | pub device_id: u8, |
| 16774 | pub control: u16, |
| 16775 | } |
| 16776 | impl_debug_if_no_extra_traits!(DevicePresenceNotifyEvent, "DevicePresenceNotifyEvent" ); |
| 16777 | impl TryParse for DevicePresenceNotifyEvent { |
| 16778 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 16779 | let remaining: &[u8] = initial_value; |
| 16780 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16781 | let remaining: &[u8] = remaining.get(1..).ok_or(err:ParseError::InsufficientData)?; |
| 16782 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 16783 | let (time: u32, remaining: &[u8]) = xproto::Timestamp::try_parse(remaining)?; |
| 16784 | let (devchange: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16785 | let (device_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16786 | let (control: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 16787 | let remaining: &[u8] = remaining.get(20..).ok_or(err:ParseError::InsufficientData)?; |
| 16788 | let devchange: DeviceChange = devchange.into(); |
| 16789 | let result: DevicePresenceNotifyEvent = DevicePresenceNotifyEvent { response_type, sequence, time, devchange, device_id, control }; |
| 16790 | let _ = remaining; |
| 16791 | let remaining: &[u8] = initial_value.get(32..) |
| 16792 | .ok_or(err:ParseError::InsufficientData)?; |
| 16793 | Ok((result, remaining)) |
| 16794 | } |
| 16795 | } |
| 16796 | impl Serialize for DevicePresenceNotifyEvent { |
| 16797 | type Bytes = [u8; 32]; |
| 16798 | fn serialize(&self) -> [u8; 32] { |
| 16799 | let response_type_bytes = self.response_type.serialize(); |
| 16800 | let sequence_bytes = self.sequence.serialize(); |
| 16801 | let time_bytes = self.time.serialize(); |
| 16802 | let devchange_bytes = u8::from(self.devchange).serialize(); |
| 16803 | let device_id_bytes = self.device_id.serialize(); |
| 16804 | let control_bytes = self.control.serialize(); |
| 16805 | [ |
| 16806 | response_type_bytes[0], |
| 16807 | 0, |
| 16808 | sequence_bytes[0], |
| 16809 | sequence_bytes[1], |
| 16810 | time_bytes[0], |
| 16811 | time_bytes[1], |
| 16812 | time_bytes[2], |
| 16813 | time_bytes[3], |
| 16814 | devchange_bytes[0], |
| 16815 | device_id_bytes[0], |
| 16816 | control_bytes[0], |
| 16817 | control_bytes[1], |
| 16818 | 0, |
| 16819 | 0, |
| 16820 | 0, |
| 16821 | 0, |
| 16822 | 0, |
| 16823 | 0, |
| 16824 | 0, |
| 16825 | 0, |
| 16826 | 0, |
| 16827 | 0, |
| 16828 | 0, |
| 16829 | 0, |
| 16830 | 0, |
| 16831 | 0, |
| 16832 | 0, |
| 16833 | 0, |
| 16834 | 0, |
| 16835 | 0, |
| 16836 | 0, |
| 16837 | 0, |
| 16838 | ] |
| 16839 | } |
| 16840 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 16841 | bytes.reserve(32); |
| 16842 | self.response_type.serialize_into(bytes); |
| 16843 | bytes.extend_from_slice(&[0; 1]); |
| 16844 | self.sequence.serialize_into(bytes); |
| 16845 | self.time.serialize_into(bytes); |
| 16846 | u8::from(self.devchange).serialize_into(bytes); |
| 16847 | self.device_id.serialize_into(bytes); |
| 16848 | self.control.serialize_into(bytes); |
| 16849 | bytes.extend_from_slice(&[0; 20]); |
| 16850 | } |
| 16851 | } |
| 16852 | impl From<&DevicePresenceNotifyEvent> for [u8; 32] { |
| 16853 | fn from(input: &DevicePresenceNotifyEvent) -> Self { |
| 16854 | let response_type_bytes = input.response_type.serialize(); |
| 16855 | let sequence_bytes = input.sequence.serialize(); |
| 16856 | let time_bytes = input.time.serialize(); |
| 16857 | let devchange_bytes = u8::from(input.devchange).serialize(); |
| 16858 | let device_id_bytes = input.device_id.serialize(); |
| 16859 | let control_bytes = input.control.serialize(); |
| 16860 | [ |
| 16861 | response_type_bytes[0], |
| 16862 | 0, |
| 16863 | sequence_bytes[0], |
| 16864 | sequence_bytes[1], |
| 16865 | time_bytes[0], |
| 16866 | time_bytes[1], |
| 16867 | time_bytes[2], |
| 16868 | time_bytes[3], |
| 16869 | devchange_bytes[0], |
| 16870 | device_id_bytes[0], |
| 16871 | control_bytes[0], |
| 16872 | control_bytes[1], |
| 16873 | 0, |
| 16874 | 0, |
| 16875 | 0, |
| 16876 | 0, |
| 16877 | 0, |
| 16878 | 0, |
| 16879 | 0, |
| 16880 | 0, |
| 16881 | 0, |
| 16882 | 0, |
| 16883 | 0, |
| 16884 | 0, |
| 16885 | 0, |
| 16886 | 0, |
| 16887 | 0, |
| 16888 | 0, |
| 16889 | 0, |
| 16890 | 0, |
| 16891 | 0, |
| 16892 | 0, |
| 16893 | ] |
| 16894 | } |
| 16895 | } |
| 16896 | impl From<DevicePresenceNotifyEvent> for [u8; 32] { |
| 16897 | fn from(input: DevicePresenceNotifyEvent) -> Self { |
| 16898 | Self::from(&input) |
| 16899 | } |
| 16900 | } |
| 16901 | |
| 16902 | /// Opcode for the DevicePropertyNotify event |
| 16903 | pub const DEVICE_PROPERTY_NOTIFY_EVENT: u8 = 16; |
| 16904 | #[derive (Clone, Copy, Default)] |
| 16905 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 16906 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 16907 | pub struct DevicePropertyNotifyEvent { |
| 16908 | pub response_type: u8, |
| 16909 | pub state: xproto::Property, |
| 16910 | pub sequence: u16, |
| 16911 | pub time: xproto::Timestamp, |
| 16912 | pub property: xproto::Atom, |
| 16913 | pub device_id: u8, |
| 16914 | } |
| 16915 | impl_debug_if_no_extra_traits!(DevicePropertyNotifyEvent, "DevicePropertyNotifyEvent" ); |
| 16916 | impl TryParse for DevicePropertyNotifyEvent { |
| 16917 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 16918 | let remaining: &[u8] = initial_value; |
| 16919 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16920 | let (state: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16921 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 16922 | let (time: u32, remaining: &[u8]) = xproto::Timestamp::try_parse(remaining)?; |
| 16923 | let (property: u32, remaining: &[u8]) = xproto::Atom::try_parse(remaining)?; |
| 16924 | let remaining: &[u8] = remaining.get(19..).ok_or(err:ParseError::InsufficientData)?; |
| 16925 | let (device_id: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 16926 | let state: Property = state.into(); |
| 16927 | let result: DevicePropertyNotifyEvent = DevicePropertyNotifyEvent { response_type, state, sequence, time, property, device_id }; |
| 16928 | let _ = remaining; |
| 16929 | let remaining: &[u8] = initial_value.get(32..) |
| 16930 | .ok_or(err:ParseError::InsufficientData)?; |
| 16931 | Ok((result, remaining)) |
| 16932 | } |
| 16933 | } |
| 16934 | impl Serialize for DevicePropertyNotifyEvent { |
| 16935 | type Bytes = [u8; 32]; |
| 16936 | fn serialize(&self) -> [u8; 32] { |
| 16937 | let response_type_bytes = self.response_type.serialize(); |
| 16938 | let state_bytes = u8::from(self.state).serialize(); |
| 16939 | let sequence_bytes = self.sequence.serialize(); |
| 16940 | let time_bytes = self.time.serialize(); |
| 16941 | let property_bytes = self.property.serialize(); |
| 16942 | let device_id_bytes = self.device_id.serialize(); |
| 16943 | [ |
| 16944 | response_type_bytes[0], |
| 16945 | state_bytes[0], |
| 16946 | sequence_bytes[0], |
| 16947 | sequence_bytes[1], |
| 16948 | time_bytes[0], |
| 16949 | time_bytes[1], |
| 16950 | time_bytes[2], |
| 16951 | time_bytes[3], |
| 16952 | property_bytes[0], |
| 16953 | property_bytes[1], |
| 16954 | property_bytes[2], |
| 16955 | property_bytes[3], |
| 16956 | 0, |
| 16957 | 0, |
| 16958 | 0, |
| 16959 | 0, |
| 16960 | 0, |
| 16961 | 0, |
| 16962 | 0, |
| 16963 | 0, |
| 16964 | 0, |
| 16965 | 0, |
| 16966 | 0, |
| 16967 | 0, |
| 16968 | 0, |
| 16969 | 0, |
| 16970 | 0, |
| 16971 | 0, |
| 16972 | 0, |
| 16973 | 0, |
| 16974 | 0, |
| 16975 | device_id_bytes[0], |
| 16976 | ] |
| 16977 | } |
| 16978 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 16979 | bytes.reserve(32); |
| 16980 | self.response_type.serialize_into(bytes); |
| 16981 | u8::from(self.state).serialize_into(bytes); |
| 16982 | self.sequence.serialize_into(bytes); |
| 16983 | self.time.serialize_into(bytes); |
| 16984 | self.property.serialize_into(bytes); |
| 16985 | bytes.extend_from_slice(&[0; 19]); |
| 16986 | self.device_id.serialize_into(bytes); |
| 16987 | } |
| 16988 | } |
| 16989 | impl From<&DevicePropertyNotifyEvent> for [u8; 32] { |
| 16990 | fn from(input: &DevicePropertyNotifyEvent) -> Self { |
| 16991 | let response_type_bytes = input.response_type.serialize(); |
| 16992 | let state_bytes = u8::from(input.state).serialize(); |
| 16993 | let sequence_bytes = input.sequence.serialize(); |
| 16994 | let time_bytes = input.time.serialize(); |
| 16995 | let property_bytes = input.property.serialize(); |
| 16996 | let device_id_bytes = input.device_id.serialize(); |
| 16997 | [ |
| 16998 | response_type_bytes[0], |
| 16999 | state_bytes[0], |
| 17000 | sequence_bytes[0], |
| 17001 | sequence_bytes[1], |
| 17002 | time_bytes[0], |
| 17003 | time_bytes[1], |
| 17004 | time_bytes[2], |
| 17005 | time_bytes[3], |
| 17006 | property_bytes[0], |
| 17007 | property_bytes[1], |
| 17008 | property_bytes[2], |
| 17009 | property_bytes[3], |
| 17010 | 0, |
| 17011 | 0, |
| 17012 | 0, |
| 17013 | 0, |
| 17014 | 0, |
| 17015 | 0, |
| 17016 | 0, |
| 17017 | 0, |
| 17018 | 0, |
| 17019 | 0, |
| 17020 | 0, |
| 17021 | 0, |
| 17022 | 0, |
| 17023 | 0, |
| 17024 | 0, |
| 17025 | 0, |
| 17026 | 0, |
| 17027 | 0, |
| 17028 | 0, |
| 17029 | device_id_bytes[0], |
| 17030 | ] |
| 17031 | } |
| 17032 | } |
| 17033 | impl From<DevicePropertyNotifyEvent> for [u8; 32] { |
| 17034 | fn from(input: DevicePropertyNotifyEvent) -> Self { |
| 17035 | Self::from(&input) |
| 17036 | } |
| 17037 | } |
| 17038 | |
| 17039 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 17040 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 17041 | pub struct ChangeReason(u8); |
| 17042 | impl ChangeReason { |
| 17043 | pub const SLAVE_SWITCH: Self = Self(1); |
| 17044 | pub const DEVICE_CHANGE: Self = Self(2); |
| 17045 | } |
| 17046 | impl From<ChangeReason> for u8 { |
| 17047 | #[inline ] |
| 17048 | fn from(input: ChangeReason) -> Self { |
| 17049 | input.0 |
| 17050 | } |
| 17051 | } |
| 17052 | impl From<ChangeReason> for Option<u8> { |
| 17053 | #[inline ] |
| 17054 | fn from(input: ChangeReason) -> Self { |
| 17055 | Some(input.0) |
| 17056 | } |
| 17057 | } |
| 17058 | impl From<ChangeReason> for u16 { |
| 17059 | #[inline ] |
| 17060 | fn from(input: ChangeReason) -> Self { |
| 17061 | u16::from(input.0) |
| 17062 | } |
| 17063 | } |
| 17064 | impl From<ChangeReason> for Option<u16> { |
| 17065 | #[inline ] |
| 17066 | fn from(input: ChangeReason) -> Self { |
| 17067 | Some(u16::from(input.0)) |
| 17068 | } |
| 17069 | } |
| 17070 | impl From<ChangeReason> for u32 { |
| 17071 | #[inline ] |
| 17072 | fn from(input: ChangeReason) -> Self { |
| 17073 | u32::from(input.0) |
| 17074 | } |
| 17075 | } |
| 17076 | impl From<ChangeReason> for Option<u32> { |
| 17077 | #[inline ] |
| 17078 | fn from(input: ChangeReason) -> Self { |
| 17079 | Some(u32::from(input.0)) |
| 17080 | } |
| 17081 | } |
| 17082 | impl From<u8> for ChangeReason { |
| 17083 | #[inline ] |
| 17084 | fn from(value: u8) -> Self { |
| 17085 | Self(value) |
| 17086 | } |
| 17087 | } |
| 17088 | impl core::fmt::Debug for ChangeReason { |
| 17089 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 17090 | let variants: [(u32, &'static str, &'static …); 2] = [ |
| 17091 | (Self::SLAVE_SWITCH.0.into(), "SLAVE_SWITCH" , "SlaveSwitch" ), |
| 17092 | (Self::DEVICE_CHANGE.0.into(), "DEVICE_CHANGE" , "DeviceChange" ), |
| 17093 | ]; |
| 17094 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 17095 | } |
| 17096 | } |
| 17097 | |
| 17098 | /// Opcode for the DeviceChanged event |
| 17099 | pub const DEVICE_CHANGED_EVENT: u16 = 1; |
| 17100 | #[derive (Clone)] |
| 17101 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 17102 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 17103 | pub struct DeviceChangedEvent { |
| 17104 | pub response_type: u8, |
| 17105 | pub extension: u8, |
| 17106 | pub sequence: u16, |
| 17107 | pub length: u32, |
| 17108 | pub event_type: u16, |
| 17109 | pub deviceid: DeviceId, |
| 17110 | pub time: xproto::Timestamp, |
| 17111 | pub sourceid: DeviceId, |
| 17112 | pub reason: ChangeReason, |
| 17113 | pub classes: Vec<DeviceClass>, |
| 17114 | } |
| 17115 | impl_debug_if_no_extra_traits!(DeviceChangedEvent, "DeviceChangedEvent" ); |
| 17116 | impl TryParse for DeviceChangedEvent { |
| 17117 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 17118 | let remaining: &[u8] = initial_value; |
| 17119 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 17120 | let (extension: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 17121 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 17122 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 17123 | let (event_type: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 17124 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 17125 | let (time: u32, remaining: &[u8]) = xproto::Timestamp::try_parse(remaining)?; |
| 17126 | let (num_classes: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 17127 | let (sourceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 17128 | let (reason: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 17129 | let remaining: &[u8] = remaining.get(11..).ok_or(err:ParseError::InsufficientData)?; |
| 17130 | let (classes: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<DeviceClass>(data:remaining, list_length:num_classes.try_to_usize()?)?; |
| 17131 | let reason: ChangeReason = reason.into(); |
| 17132 | let result: DeviceChangedEvent = DeviceChangedEvent { response_type, extension, sequence, length, event_type, deviceid, time, sourceid, reason, classes }; |
| 17133 | let _ = remaining; |
| 17134 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 17135 | .ok_or(err:ParseError::InsufficientData)?; |
| 17136 | Ok((result, remaining)) |
| 17137 | } |
| 17138 | } |
| 17139 | impl Serialize for DeviceChangedEvent { |
| 17140 | type Bytes = Vec<u8>; |
| 17141 | fn serialize(&self) -> Vec<u8> { |
| 17142 | let mut result: Vec = Vec::new(); |
| 17143 | self.serialize_into(&mut result); |
| 17144 | result |
| 17145 | } |
| 17146 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 17147 | bytes.reserve(additional:32); |
| 17148 | self.response_type.serialize_into(bytes); |
| 17149 | self.extension.serialize_into(bytes); |
| 17150 | self.sequence.serialize_into(bytes); |
| 17151 | self.length.serialize_into(bytes); |
| 17152 | self.event_type.serialize_into(bytes); |
| 17153 | self.deviceid.serialize_into(bytes); |
| 17154 | self.time.serialize_into(bytes); |
| 17155 | let num_classes: u16 = u16::try_from(self.classes.len()).expect(msg:"`classes` has too many elements" ); |
| 17156 | num_classes.serialize_into(bytes); |
| 17157 | self.sourceid.serialize_into(bytes); |
| 17158 | u8::from(self.reason).serialize_into(bytes); |
| 17159 | bytes.extend_from_slice(&[0; 11]); |
| 17160 | self.classes.serialize_into(bytes); |
| 17161 | } |
| 17162 | } |
| 17163 | impl DeviceChangedEvent { |
| 17164 | /// Get the value of the `num_classes` field. |
| 17165 | /// |
| 17166 | /// The `num_classes` field is used as the length field of the `classes` field. |
| 17167 | /// This function computes the field's value again based on the length of the list. |
| 17168 | /// |
| 17169 | /// # Panics |
| 17170 | /// |
| 17171 | /// Panics if the value cannot be represented in the target type. This |
| 17172 | /// cannot happen with values of the struct received from the X11 server. |
| 17173 | pub fn num_classes(&self) -> u16 { |
| 17174 | self.classes.len() |
| 17175 | .try_into().unwrap() |
| 17176 | } |
| 17177 | } |
| 17178 | |
| 17179 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 17180 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 17181 | pub struct KeyEventFlags(u32); |
| 17182 | impl KeyEventFlags { |
| 17183 | pub const KEY_REPEAT: Self = Self(1 << 16); |
| 17184 | } |
| 17185 | impl From<KeyEventFlags> for u32 { |
| 17186 | #[inline ] |
| 17187 | fn from(input: KeyEventFlags) -> Self { |
| 17188 | input.0 |
| 17189 | } |
| 17190 | } |
| 17191 | impl From<KeyEventFlags> for Option<u32> { |
| 17192 | #[inline ] |
| 17193 | fn from(input: KeyEventFlags) -> Self { |
| 17194 | Some(input.0) |
| 17195 | } |
| 17196 | } |
| 17197 | impl From<u8> for KeyEventFlags { |
| 17198 | #[inline ] |
| 17199 | fn from(value: u8) -> Self { |
| 17200 | Self(value.into()) |
| 17201 | } |
| 17202 | } |
| 17203 | impl From<u16> for KeyEventFlags { |
| 17204 | #[inline ] |
| 17205 | fn from(value: u16) -> Self { |
| 17206 | Self(value.into()) |
| 17207 | } |
| 17208 | } |
| 17209 | impl From<u32> for KeyEventFlags { |
| 17210 | #[inline ] |
| 17211 | fn from(value: u32) -> Self { |
| 17212 | Self(value) |
| 17213 | } |
| 17214 | } |
| 17215 | impl core::fmt::Debug for KeyEventFlags { |
| 17216 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 17217 | let variants: [(u32, &'static str, &'static …); 1] = [ |
| 17218 | (Self::KEY_REPEAT.0, "KEY_REPEAT" , "KeyRepeat" ), |
| 17219 | ]; |
| 17220 | pretty_print_bitmask(fmt, self.0, &variants) |
| 17221 | } |
| 17222 | } |
| 17223 | bitmask_binop!(KeyEventFlags, u32); |
| 17224 | |
| 17225 | /// Opcode for the KeyPress event |
| 17226 | pub const KEY_PRESS_EVENT: u16 = 2; |
| 17227 | #[derive (Clone, Default)] |
| 17228 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 17229 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 17230 | pub struct KeyPressEvent { |
| 17231 | pub response_type: u8, |
| 17232 | pub extension: u8, |
| 17233 | pub sequence: u16, |
| 17234 | pub length: u32, |
| 17235 | pub event_type: u16, |
| 17236 | pub deviceid: DeviceId, |
| 17237 | pub time: xproto::Timestamp, |
| 17238 | pub detail: u32, |
| 17239 | pub root: xproto::Window, |
| 17240 | pub event: xproto::Window, |
| 17241 | pub child: xproto::Window, |
| 17242 | pub root_x: Fp1616, |
| 17243 | pub root_y: Fp1616, |
| 17244 | pub event_x: Fp1616, |
| 17245 | pub event_y: Fp1616, |
| 17246 | pub sourceid: DeviceId, |
| 17247 | pub flags: KeyEventFlags, |
| 17248 | pub mods: ModifierInfo, |
| 17249 | pub group: GroupInfo, |
| 17250 | pub button_mask: Vec<u32>, |
| 17251 | pub valuator_mask: Vec<u32>, |
| 17252 | pub axisvalues: Vec<Fp3232>, |
| 17253 | } |
| 17254 | impl_debug_if_no_extra_traits!(KeyPressEvent, "KeyPressEvent" ); |
| 17255 | impl TryParse for KeyPressEvent { |
| 17256 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 17257 | let remaining = initial_value; |
| 17258 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 17259 | let (extension, remaining) = u8::try_parse(remaining)?; |
| 17260 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 17261 | let (length, remaining) = u32::try_parse(remaining)?; |
| 17262 | let (event_type, remaining) = u16::try_parse(remaining)?; |
| 17263 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 17264 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 17265 | let (detail, remaining) = u32::try_parse(remaining)?; |
| 17266 | let (root, remaining) = xproto::Window::try_parse(remaining)?; |
| 17267 | let (event, remaining) = xproto::Window::try_parse(remaining)?; |
| 17268 | let (child, remaining) = xproto::Window::try_parse(remaining)?; |
| 17269 | let (root_x, remaining) = Fp1616::try_parse(remaining)?; |
| 17270 | let (root_y, remaining) = Fp1616::try_parse(remaining)?; |
| 17271 | let (event_x, remaining) = Fp1616::try_parse(remaining)?; |
| 17272 | let (event_y, remaining) = Fp1616::try_parse(remaining)?; |
| 17273 | let (buttons_len, remaining) = u16::try_parse(remaining)?; |
| 17274 | let (valuators_len, remaining) = u16::try_parse(remaining)?; |
| 17275 | let (sourceid, remaining) = DeviceId::try_parse(remaining)?; |
| 17276 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 17277 | let (flags, remaining) = u32::try_parse(remaining)?; |
| 17278 | let (mods, remaining) = ModifierInfo::try_parse(remaining)?; |
| 17279 | let (group, remaining) = GroupInfo::try_parse(remaining)?; |
| 17280 | let (button_mask, remaining) = crate::x11_utils::parse_list::<u32>(remaining, buttons_len.try_to_usize()?)?; |
| 17281 | let (valuator_mask, remaining) = crate::x11_utils::parse_list::<u32>(remaining, valuators_len.try_to_usize()?)?; |
| 17282 | let (axisvalues, remaining) = crate::x11_utils::parse_list::<Fp3232>(remaining, valuator_mask.iter().try_fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).ok_or(ParseError::InvalidExpression))?.try_to_usize()?)?; |
| 17283 | let flags = flags.into(); |
| 17284 | let result = KeyPressEvent { response_type, extension, sequence, length, event_type, deviceid, time, detail, root, event, child, root_x, root_y, event_x, event_y, sourceid, flags, mods, group, button_mask, valuator_mask, axisvalues }; |
| 17285 | let _ = remaining; |
| 17286 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 17287 | .ok_or(ParseError::InsufficientData)?; |
| 17288 | Ok((result, remaining)) |
| 17289 | } |
| 17290 | } |
| 17291 | impl Serialize for KeyPressEvent { |
| 17292 | type Bytes = Vec<u8>; |
| 17293 | fn serialize(&self) -> Vec<u8> { |
| 17294 | let mut result = Vec::new(); |
| 17295 | self.serialize_into(&mut result); |
| 17296 | result |
| 17297 | } |
| 17298 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 17299 | bytes.reserve(80); |
| 17300 | self.response_type.serialize_into(bytes); |
| 17301 | self.extension.serialize_into(bytes); |
| 17302 | self.sequence.serialize_into(bytes); |
| 17303 | self.length.serialize_into(bytes); |
| 17304 | self.event_type.serialize_into(bytes); |
| 17305 | self.deviceid.serialize_into(bytes); |
| 17306 | self.time.serialize_into(bytes); |
| 17307 | self.detail.serialize_into(bytes); |
| 17308 | self.root.serialize_into(bytes); |
| 17309 | self.event.serialize_into(bytes); |
| 17310 | self.child.serialize_into(bytes); |
| 17311 | self.root_x.serialize_into(bytes); |
| 17312 | self.root_y.serialize_into(bytes); |
| 17313 | self.event_x.serialize_into(bytes); |
| 17314 | self.event_y.serialize_into(bytes); |
| 17315 | let buttons_len = u16::try_from(self.button_mask.len()).expect("`button_mask` has too many elements" ); |
| 17316 | buttons_len.serialize_into(bytes); |
| 17317 | let valuators_len = u16::try_from(self.valuator_mask.len()).expect("`valuator_mask` has too many elements" ); |
| 17318 | valuators_len.serialize_into(bytes); |
| 17319 | self.sourceid.serialize_into(bytes); |
| 17320 | bytes.extend_from_slice(&[0; 2]); |
| 17321 | u32::from(self.flags).serialize_into(bytes); |
| 17322 | self.mods.serialize_into(bytes); |
| 17323 | self.group.serialize_into(bytes); |
| 17324 | self.button_mask.serialize_into(bytes); |
| 17325 | self.valuator_mask.serialize_into(bytes); |
| 17326 | assert_eq!(self.axisvalues.len(), usize::try_from(self.valuator_mask.iter().fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).unwrap())).unwrap(), "`axisvalues` has an incorrect length" ); |
| 17327 | self.axisvalues.serialize_into(bytes); |
| 17328 | } |
| 17329 | } |
| 17330 | impl KeyPressEvent { |
| 17331 | /// Get the value of the `buttons_len` field. |
| 17332 | /// |
| 17333 | /// The `buttons_len` field is used as the length field of the `button_mask` field. |
| 17334 | /// This function computes the field's value again based on the length of the list. |
| 17335 | /// |
| 17336 | /// # Panics |
| 17337 | /// |
| 17338 | /// Panics if the value cannot be represented in the target type. This |
| 17339 | /// cannot happen with values of the struct received from the X11 server. |
| 17340 | pub fn buttons_len(&self) -> u16 { |
| 17341 | self.button_mask.len() |
| 17342 | .try_into().unwrap() |
| 17343 | } |
| 17344 | /// Get the value of the `valuators_len` field. |
| 17345 | /// |
| 17346 | /// The `valuators_len` field is used as the length field of the `valuator_mask` field. |
| 17347 | /// This function computes the field's value again based on the length of the list. |
| 17348 | /// |
| 17349 | /// # Panics |
| 17350 | /// |
| 17351 | /// Panics if the value cannot be represented in the target type. This |
| 17352 | /// cannot happen with values of the struct received from the X11 server. |
| 17353 | pub fn valuators_len(&self) -> u16 { |
| 17354 | self.valuator_mask.len() |
| 17355 | .try_into().unwrap() |
| 17356 | } |
| 17357 | } |
| 17358 | |
| 17359 | /// Opcode for the KeyRelease event |
| 17360 | pub const KEY_RELEASE_EVENT: u16 = 3; |
| 17361 | pub type KeyReleaseEvent = KeyPressEvent; |
| 17362 | |
| 17363 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 17364 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 17365 | pub struct PointerEventFlags(u32); |
| 17366 | impl PointerEventFlags { |
| 17367 | pub const POINTER_EMULATED: Self = Self(1 << 16); |
| 17368 | } |
| 17369 | impl From<PointerEventFlags> for u32 { |
| 17370 | #[inline ] |
| 17371 | fn from(input: PointerEventFlags) -> Self { |
| 17372 | input.0 |
| 17373 | } |
| 17374 | } |
| 17375 | impl From<PointerEventFlags> for Option<u32> { |
| 17376 | #[inline ] |
| 17377 | fn from(input: PointerEventFlags) -> Self { |
| 17378 | Some(input.0) |
| 17379 | } |
| 17380 | } |
| 17381 | impl From<u8> for PointerEventFlags { |
| 17382 | #[inline ] |
| 17383 | fn from(value: u8) -> Self { |
| 17384 | Self(value.into()) |
| 17385 | } |
| 17386 | } |
| 17387 | impl From<u16> for PointerEventFlags { |
| 17388 | #[inline ] |
| 17389 | fn from(value: u16) -> Self { |
| 17390 | Self(value.into()) |
| 17391 | } |
| 17392 | } |
| 17393 | impl From<u32> for PointerEventFlags { |
| 17394 | #[inline ] |
| 17395 | fn from(value: u32) -> Self { |
| 17396 | Self(value) |
| 17397 | } |
| 17398 | } |
| 17399 | impl core::fmt::Debug for PointerEventFlags { |
| 17400 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 17401 | let variants: [(u32, &'static str, &'static …); 1] = [ |
| 17402 | (Self::POINTER_EMULATED.0, "POINTER_EMULATED" , "PointerEmulated" ), |
| 17403 | ]; |
| 17404 | pretty_print_bitmask(fmt, self.0, &variants) |
| 17405 | } |
| 17406 | } |
| 17407 | bitmask_binop!(PointerEventFlags, u32); |
| 17408 | |
| 17409 | /// Opcode for the ButtonPress event |
| 17410 | pub const BUTTON_PRESS_EVENT: u16 = 4; |
| 17411 | #[derive (Clone, Default)] |
| 17412 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 17413 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 17414 | pub struct ButtonPressEvent { |
| 17415 | pub response_type: u8, |
| 17416 | pub extension: u8, |
| 17417 | pub sequence: u16, |
| 17418 | pub length: u32, |
| 17419 | pub event_type: u16, |
| 17420 | pub deviceid: DeviceId, |
| 17421 | pub time: xproto::Timestamp, |
| 17422 | pub detail: u32, |
| 17423 | pub root: xproto::Window, |
| 17424 | pub event: xproto::Window, |
| 17425 | pub child: xproto::Window, |
| 17426 | pub root_x: Fp1616, |
| 17427 | pub root_y: Fp1616, |
| 17428 | pub event_x: Fp1616, |
| 17429 | pub event_y: Fp1616, |
| 17430 | pub sourceid: DeviceId, |
| 17431 | pub flags: PointerEventFlags, |
| 17432 | pub mods: ModifierInfo, |
| 17433 | pub group: GroupInfo, |
| 17434 | pub button_mask: Vec<u32>, |
| 17435 | pub valuator_mask: Vec<u32>, |
| 17436 | pub axisvalues: Vec<Fp3232>, |
| 17437 | } |
| 17438 | impl_debug_if_no_extra_traits!(ButtonPressEvent, "ButtonPressEvent" ); |
| 17439 | impl TryParse for ButtonPressEvent { |
| 17440 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 17441 | let remaining = initial_value; |
| 17442 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 17443 | let (extension, remaining) = u8::try_parse(remaining)?; |
| 17444 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 17445 | let (length, remaining) = u32::try_parse(remaining)?; |
| 17446 | let (event_type, remaining) = u16::try_parse(remaining)?; |
| 17447 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 17448 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 17449 | let (detail, remaining) = u32::try_parse(remaining)?; |
| 17450 | let (root, remaining) = xproto::Window::try_parse(remaining)?; |
| 17451 | let (event, remaining) = xproto::Window::try_parse(remaining)?; |
| 17452 | let (child, remaining) = xproto::Window::try_parse(remaining)?; |
| 17453 | let (root_x, remaining) = Fp1616::try_parse(remaining)?; |
| 17454 | let (root_y, remaining) = Fp1616::try_parse(remaining)?; |
| 17455 | let (event_x, remaining) = Fp1616::try_parse(remaining)?; |
| 17456 | let (event_y, remaining) = Fp1616::try_parse(remaining)?; |
| 17457 | let (buttons_len, remaining) = u16::try_parse(remaining)?; |
| 17458 | let (valuators_len, remaining) = u16::try_parse(remaining)?; |
| 17459 | let (sourceid, remaining) = DeviceId::try_parse(remaining)?; |
| 17460 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 17461 | let (flags, remaining) = u32::try_parse(remaining)?; |
| 17462 | let (mods, remaining) = ModifierInfo::try_parse(remaining)?; |
| 17463 | let (group, remaining) = GroupInfo::try_parse(remaining)?; |
| 17464 | let (button_mask, remaining) = crate::x11_utils::parse_list::<u32>(remaining, buttons_len.try_to_usize()?)?; |
| 17465 | let (valuator_mask, remaining) = crate::x11_utils::parse_list::<u32>(remaining, valuators_len.try_to_usize()?)?; |
| 17466 | let (axisvalues, remaining) = crate::x11_utils::parse_list::<Fp3232>(remaining, valuator_mask.iter().try_fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).ok_or(ParseError::InvalidExpression))?.try_to_usize()?)?; |
| 17467 | let flags = flags.into(); |
| 17468 | let result = ButtonPressEvent { response_type, extension, sequence, length, event_type, deviceid, time, detail, root, event, child, root_x, root_y, event_x, event_y, sourceid, flags, mods, group, button_mask, valuator_mask, axisvalues }; |
| 17469 | let _ = remaining; |
| 17470 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 17471 | .ok_or(ParseError::InsufficientData)?; |
| 17472 | Ok((result, remaining)) |
| 17473 | } |
| 17474 | } |
| 17475 | impl Serialize for ButtonPressEvent { |
| 17476 | type Bytes = Vec<u8>; |
| 17477 | fn serialize(&self) -> Vec<u8> { |
| 17478 | let mut result = Vec::new(); |
| 17479 | self.serialize_into(&mut result); |
| 17480 | result |
| 17481 | } |
| 17482 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 17483 | bytes.reserve(80); |
| 17484 | self.response_type.serialize_into(bytes); |
| 17485 | self.extension.serialize_into(bytes); |
| 17486 | self.sequence.serialize_into(bytes); |
| 17487 | self.length.serialize_into(bytes); |
| 17488 | self.event_type.serialize_into(bytes); |
| 17489 | self.deviceid.serialize_into(bytes); |
| 17490 | self.time.serialize_into(bytes); |
| 17491 | self.detail.serialize_into(bytes); |
| 17492 | self.root.serialize_into(bytes); |
| 17493 | self.event.serialize_into(bytes); |
| 17494 | self.child.serialize_into(bytes); |
| 17495 | self.root_x.serialize_into(bytes); |
| 17496 | self.root_y.serialize_into(bytes); |
| 17497 | self.event_x.serialize_into(bytes); |
| 17498 | self.event_y.serialize_into(bytes); |
| 17499 | let buttons_len = u16::try_from(self.button_mask.len()).expect("`button_mask` has too many elements" ); |
| 17500 | buttons_len.serialize_into(bytes); |
| 17501 | let valuators_len = u16::try_from(self.valuator_mask.len()).expect("`valuator_mask` has too many elements" ); |
| 17502 | valuators_len.serialize_into(bytes); |
| 17503 | self.sourceid.serialize_into(bytes); |
| 17504 | bytes.extend_from_slice(&[0; 2]); |
| 17505 | u32::from(self.flags).serialize_into(bytes); |
| 17506 | self.mods.serialize_into(bytes); |
| 17507 | self.group.serialize_into(bytes); |
| 17508 | self.button_mask.serialize_into(bytes); |
| 17509 | self.valuator_mask.serialize_into(bytes); |
| 17510 | assert_eq!(self.axisvalues.len(), usize::try_from(self.valuator_mask.iter().fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).unwrap())).unwrap(), "`axisvalues` has an incorrect length" ); |
| 17511 | self.axisvalues.serialize_into(bytes); |
| 17512 | } |
| 17513 | } |
| 17514 | impl ButtonPressEvent { |
| 17515 | /// Get the value of the `buttons_len` field. |
| 17516 | /// |
| 17517 | /// The `buttons_len` field is used as the length field of the `button_mask` field. |
| 17518 | /// This function computes the field's value again based on the length of the list. |
| 17519 | /// |
| 17520 | /// # Panics |
| 17521 | /// |
| 17522 | /// Panics if the value cannot be represented in the target type. This |
| 17523 | /// cannot happen with values of the struct received from the X11 server. |
| 17524 | pub fn buttons_len(&self) -> u16 { |
| 17525 | self.button_mask.len() |
| 17526 | .try_into().unwrap() |
| 17527 | } |
| 17528 | /// Get the value of the `valuators_len` field. |
| 17529 | /// |
| 17530 | /// The `valuators_len` field is used as the length field of the `valuator_mask` field. |
| 17531 | /// This function computes the field's value again based on the length of the list. |
| 17532 | /// |
| 17533 | /// # Panics |
| 17534 | /// |
| 17535 | /// Panics if the value cannot be represented in the target type. This |
| 17536 | /// cannot happen with values of the struct received from the X11 server. |
| 17537 | pub fn valuators_len(&self) -> u16 { |
| 17538 | self.valuator_mask.len() |
| 17539 | .try_into().unwrap() |
| 17540 | } |
| 17541 | } |
| 17542 | |
| 17543 | /// Opcode for the ButtonRelease event |
| 17544 | pub const BUTTON_RELEASE_EVENT: u16 = 5; |
| 17545 | pub type ButtonReleaseEvent = ButtonPressEvent; |
| 17546 | |
| 17547 | /// Opcode for the Motion event |
| 17548 | pub const MOTION_EVENT: u16 = 6; |
| 17549 | pub type MotionEvent = ButtonPressEvent; |
| 17550 | |
| 17551 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 17552 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 17553 | pub struct NotifyMode(u8); |
| 17554 | impl NotifyMode { |
| 17555 | pub const NORMAL: Self = Self(0); |
| 17556 | pub const GRAB: Self = Self(1); |
| 17557 | pub const UNGRAB: Self = Self(2); |
| 17558 | pub const WHILE_GRABBED: Self = Self(3); |
| 17559 | pub const PASSIVE_GRAB: Self = Self(4); |
| 17560 | pub const PASSIVE_UNGRAB: Self = Self(5); |
| 17561 | } |
| 17562 | impl From<NotifyMode> for u8 { |
| 17563 | #[inline ] |
| 17564 | fn from(input: NotifyMode) -> Self { |
| 17565 | input.0 |
| 17566 | } |
| 17567 | } |
| 17568 | impl From<NotifyMode> for Option<u8> { |
| 17569 | #[inline ] |
| 17570 | fn from(input: NotifyMode) -> Self { |
| 17571 | Some(input.0) |
| 17572 | } |
| 17573 | } |
| 17574 | impl From<NotifyMode> for u16 { |
| 17575 | #[inline ] |
| 17576 | fn from(input: NotifyMode) -> Self { |
| 17577 | u16::from(input.0) |
| 17578 | } |
| 17579 | } |
| 17580 | impl From<NotifyMode> for Option<u16> { |
| 17581 | #[inline ] |
| 17582 | fn from(input: NotifyMode) -> Self { |
| 17583 | Some(u16::from(input.0)) |
| 17584 | } |
| 17585 | } |
| 17586 | impl From<NotifyMode> for u32 { |
| 17587 | #[inline ] |
| 17588 | fn from(input: NotifyMode) -> Self { |
| 17589 | u32::from(input.0) |
| 17590 | } |
| 17591 | } |
| 17592 | impl From<NotifyMode> for Option<u32> { |
| 17593 | #[inline ] |
| 17594 | fn from(input: NotifyMode) -> Self { |
| 17595 | Some(u32::from(input.0)) |
| 17596 | } |
| 17597 | } |
| 17598 | impl From<u8> for NotifyMode { |
| 17599 | #[inline ] |
| 17600 | fn from(value: u8) -> Self { |
| 17601 | Self(value) |
| 17602 | } |
| 17603 | } |
| 17604 | impl core::fmt::Debug for NotifyMode { |
| 17605 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 17606 | let variants: [(u32, &'static str, &'static …); 6] = [ |
| 17607 | (Self::NORMAL.0.into(), "NORMAL" , "Normal" ), |
| 17608 | (Self::GRAB.0.into(), "GRAB" , "Grab" ), |
| 17609 | (Self::UNGRAB.0.into(), "UNGRAB" , "Ungrab" ), |
| 17610 | (Self::WHILE_GRABBED.0.into(), "WHILE_GRABBED" , "WhileGrabbed" ), |
| 17611 | (Self::PASSIVE_GRAB.0.into(), "PASSIVE_GRAB" , "PassiveGrab" ), |
| 17612 | (Self::PASSIVE_UNGRAB.0.into(), "PASSIVE_UNGRAB" , "PassiveUngrab" ), |
| 17613 | ]; |
| 17614 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 17615 | } |
| 17616 | } |
| 17617 | |
| 17618 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 17619 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 17620 | pub struct NotifyDetail(u8); |
| 17621 | impl NotifyDetail { |
| 17622 | pub const ANCESTOR: Self = Self(0); |
| 17623 | pub const VIRTUAL: Self = Self(1); |
| 17624 | pub const INFERIOR: Self = Self(2); |
| 17625 | pub const NONLINEAR: Self = Self(3); |
| 17626 | pub const NONLINEAR_VIRTUAL: Self = Self(4); |
| 17627 | pub const POINTER: Self = Self(5); |
| 17628 | pub const POINTER_ROOT: Self = Self(6); |
| 17629 | pub const NONE: Self = Self(7); |
| 17630 | } |
| 17631 | impl From<NotifyDetail> for u8 { |
| 17632 | #[inline ] |
| 17633 | fn from(input: NotifyDetail) -> Self { |
| 17634 | input.0 |
| 17635 | } |
| 17636 | } |
| 17637 | impl From<NotifyDetail> for Option<u8> { |
| 17638 | #[inline ] |
| 17639 | fn from(input: NotifyDetail) -> Self { |
| 17640 | Some(input.0) |
| 17641 | } |
| 17642 | } |
| 17643 | impl From<NotifyDetail> for u16 { |
| 17644 | #[inline ] |
| 17645 | fn from(input: NotifyDetail) -> Self { |
| 17646 | u16::from(input.0) |
| 17647 | } |
| 17648 | } |
| 17649 | impl From<NotifyDetail> for Option<u16> { |
| 17650 | #[inline ] |
| 17651 | fn from(input: NotifyDetail) -> Self { |
| 17652 | Some(u16::from(input.0)) |
| 17653 | } |
| 17654 | } |
| 17655 | impl From<NotifyDetail> for u32 { |
| 17656 | #[inline ] |
| 17657 | fn from(input: NotifyDetail) -> Self { |
| 17658 | u32::from(input.0) |
| 17659 | } |
| 17660 | } |
| 17661 | impl From<NotifyDetail> for Option<u32> { |
| 17662 | #[inline ] |
| 17663 | fn from(input: NotifyDetail) -> Self { |
| 17664 | Some(u32::from(input.0)) |
| 17665 | } |
| 17666 | } |
| 17667 | impl From<u8> for NotifyDetail { |
| 17668 | #[inline ] |
| 17669 | fn from(value: u8) -> Self { |
| 17670 | Self(value) |
| 17671 | } |
| 17672 | } |
| 17673 | impl core::fmt::Debug for NotifyDetail { |
| 17674 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 17675 | let variants: [(u32, &'static str, &'static …); 8] = [ |
| 17676 | (Self::ANCESTOR.0.into(), "ANCESTOR" , "Ancestor" ), |
| 17677 | (Self::VIRTUAL.0.into(), "VIRTUAL" , "Virtual" ), |
| 17678 | (Self::INFERIOR.0.into(), "INFERIOR" , "Inferior" ), |
| 17679 | (Self::NONLINEAR.0.into(), "NONLINEAR" , "Nonlinear" ), |
| 17680 | (Self::NONLINEAR_VIRTUAL.0.into(), "NONLINEAR_VIRTUAL" , "NonlinearVirtual" ), |
| 17681 | (Self::POINTER.0.into(), "POINTER" , "Pointer" ), |
| 17682 | (Self::POINTER_ROOT.0.into(), "POINTER_ROOT" , "PointerRoot" ), |
| 17683 | (Self::NONE.0.into(), "NONE" , "None" ), |
| 17684 | ]; |
| 17685 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 17686 | } |
| 17687 | } |
| 17688 | |
| 17689 | /// Opcode for the Enter event |
| 17690 | pub const ENTER_EVENT: u16 = 7; |
| 17691 | #[derive (Clone, Default)] |
| 17692 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 17693 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 17694 | pub struct EnterEvent { |
| 17695 | pub response_type: u8, |
| 17696 | pub extension: u8, |
| 17697 | pub sequence: u16, |
| 17698 | pub length: u32, |
| 17699 | pub event_type: u16, |
| 17700 | pub deviceid: DeviceId, |
| 17701 | pub time: xproto::Timestamp, |
| 17702 | pub sourceid: DeviceId, |
| 17703 | pub mode: NotifyMode, |
| 17704 | pub detail: NotifyDetail, |
| 17705 | pub root: xproto::Window, |
| 17706 | pub event: xproto::Window, |
| 17707 | pub child: xproto::Window, |
| 17708 | pub root_x: Fp1616, |
| 17709 | pub root_y: Fp1616, |
| 17710 | pub event_x: Fp1616, |
| 17711 | pub event_y: Fp1616, |
| 17712 | pub same_screen: bool, |
| 17713 | pub focus: bool, |
| 17714 | pub mods: ModifierInfo, |
| 17715 | pub group: GroupInfo, |
| 17716 | pub buttons: Vec<u32>, |
| 17717 | } |
| 17718 | impl_debug_if_no_extra_traits!(EnterEvent, "EnterEvent" ); |
| 17719 | impl TryParse for EnterEvent { |
| 17720 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 17721 | let remaining = initial_value; |
| 17722 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 17723 | let (extension, remaining) = u8::try_parse(remaining)?; |
| 17724 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 17725 | let (length, remaining) = u32::try_parse(remaining)?; |
| 17726 | let (event_type, remaining) = u16::try_parse(remaining)?; |
| 17727 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 17728 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 17729 | let (sourceid, remaining) = DeviceId::try_parse(remaining)?; |
| 17730 | let (mode, remaining) = u8::try_parse(remaining)?; |
| 17731 | let (detail, remaining) = u8::try_parse(remaining)?; |
| 17732 | let (root, remaining) = xproto::Window::try_parse(remaining)?; |
| 17733 | let (event, remaining) = xproto::Window::try_parse(remaining)?; |
| 17734 | let (child, remaining) = xproto::Window::try_parse(remaining)?; |
| 17735 | let (root_x, remaining) = Fp1616::try_parse(remaining)?; |
| 17736 | let (root_y, remaining) = Fp1616::try_parse(remaining)?; |
| 17737 | let (event_x, remaining) = Fp1616::try_parse(remaining)?; |
| 17738 | let (event_y, remaining) = Fp1616::try_parse(remaining)?; |
| 17739 | let (same_screen, remaining) = bool::try_parse(remaining)?; |
| 17740 | let (focus, remaining) = bool::try_parse(remaining)?; |
| 17741 | let (buttons_len, remaining) = u16::try_parse(remaining)?; |
| 17742 | let (mods, remaining) = ModifierInfo::try_parse(remaining)?; |
| 17743 | let (group, remaining) = GroupInfo::try_parse(remaining)?; |
| 17744 | let (buttons, remaining) = crate::x11_utils::parse_list::<u32>(remaining, buttons_len.try_to_usize()?)?; |
| 17745 | let mode = mode.into(); |
| 17746 | let detail = detail.into(); |
| 17747 | let result = EnterEvent { response_type, extension, sequence, length, event_type, deviceid, time, sourceid, mode, detail, root, event, child, root_x, root_y, event_x, event_y, same_screen, focus, mods, group, buttons }; |
| 17748 | let _ = remaining; |
| 17749 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 17750 | .ok_or(ParseError::InsufficientData)?; |
| 17751 | Ok((result, remaining)) |
| 17752 | } |
| 17753 | } |
| 17754 | impl Serialize for EnterEvent { |
| 17755 | type Bytes = Vec<u8>; |
| 17756 | fn serialize(&self) -> Vec<u8> { |
| 17757 | let mut result = Vec::new(); |
| 17758 | self.serialize_into(&mut result); |
| 17759 | result |
| 17760 | } |
| 17761 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 17762 | bytes.reserve(72); |
| 17763 | self.response_type.serialize_into(bytes); |
| 17764 | self.extension.serialize_into(bytes); |
| 17765 | self.sequence.serialize_into(bytes); |
| 17766 | self.length.serialize_into(bytes); |
| 17767 | self.event_type.serialize_into(bytes); |
| 17768 | self.deviceid.serialize_into(bytes); |
| 17769 | self.time.serialize_into(bytes); |
| 17770 | self.sourceid.serialize_into(bytes); |
| 17771 | u8::from(self.mode).serialize_into(bytes); |
| 17772 | u8::from(self.detail).serialize_into(bytes); |
| 17773 | self.root.serialize_into(bytes); |
| 17774 | self.event.serialize_into(bytes); |
| 17775 | self.child.serialize_into(bytes); |
| 17776 | self.root_x.serialize_into(bytes); |
| 17777 | self.root_y.serialize_into(bytes); |
| 17778 | self.event_x.serialize_into(bytes); |
| 17779 | self.event_y.serialize_into(bytes); |
| 17780 | self.same_screen.serialize_into(bytes); |
| 17781 | self.focus.serialize_into(bytes); |
| 17782 | let buttons_len = u16::try_from(self.buttons.len()).expect("`buttons` has too many elements" ); |
| 17783 | buttons_len.serialize_into(bytes); |
| 17784 | self.mods.serialize_into(bytes); |
| 17785 | self.group.serialize_into(bytes); |
| 17786 | self.buttons.serialize_into(bytes); |
| 17787 | } |
| 17788 | } |
| 17789 | impl EnterEvent { |
| 17790 | /// Get the value of the `buttons_len` field. |
| 17791 | /// |
| 17792 | /// The `buttons_len` field is used as the length field of the `buttons` field. |
| 17793 | /// This function computes the field's value again based on the length of the list. |
| 17794 | /// |
| 17795 | /// # Panics |
| 17796 | /// |
| 17797 | /// Panics if the value cannot be represented in the target type. This |
| 17798 | /// cannot happen with values of the struct received from the X11 server. |
| 17799 | pub fn buttons_len(&self) -> u16 { |
| 17800 | self.buttons.len() |
| 17801 | .try_into().unwrap() |
| 17802 | } |
| 17803 | } |
| 17804 | |
| 17805 | /// Opcode for the Leave event |
| 17806 | pub const LEAVE_EVENT: u16 = 8; |
| 17807 | pub type LeaveEvent = EnterEvent; |
| 17808 | |
| 17809 | /// Opcode for the FocusIn event |
| 17810 | pub const FOCUS_IN_EVENT: u16 = 9; |
| 17811 | pub type FocusInEvent = EnterEvent; |
| 17812 | |
| 17813 | /// Opcode for the FocusOut event |
| 17814 | pub const FOCUS_OUT_EVENT: u16 = 10; |
| 17815 | pub type FocusOutEvent = EnterEvent; |
| 17816 | |
| 17817 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 17818 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 17819 | pub struct HierarchyMask(u32); |
| 17820 | impl HierarchyMask { |
| 17821 | pub const MASTER_ADDED: Self = Self(1 << 0); |
| 17822 | pub const MASTER_REMOVED: Self = Self(1 << 1); |
| 17823 | pub const SLAVE_ADDED: Self = Self(1 << 2); |
| 17824 | pub const SLAVE_REMOVED: Self = Self(1 << 3); |
| 17825 | pub const SLAVE_ATTACHED: Self = Self(1 << 4); |
| 17826 | pub const SLAVE_DETACHED: Self = Self(1 << 5); |
| 17827 | pub const DEVICE_ENABLED: Self = Self(1 << 6); |
| 17828 | pub const DEVICE_DISABLED: Self = Self(1 << 7); |
| 17829 | } |
| 17830 | impl From<HierarchyMask> for u32 { |
| 17831 | #[inline ] |
| 17832 | fn from(input: HierarchyMask) -> Self { |
| 17833 | input.0 |
| 17834 | } |
| 17835 | } |
| 17836 | impl From<HierarchyMask> for Option<u32> { |
| 17837 | #[inline ] |
| 17838 | fn from(input: HierarchyMask) -> Self { |
| 17839 | Some(input.0) |
| 17840 | } |
| 17841 | } |
| 17842 | impl From<u8> for HierarchyMask { |
| 17843 | #[inline ] |
| 17844 | fn from(value: u8) -> Self { |
| 17845 | Self(value.into()) |
| 17846 | } |
| 17847 | } |
| 17848 | impl From<u16> for HierarchyMask { |
| 17849 | #[inline ] |
| 17850 | fn from(value: u16) -> Self { |
| 17851 | Self(value.into()) |
| 17852 | } |
| 17853 | } |
| 17854 | impl From<u32> for HierarchyMask { |
| 17855 | #[inline ] |
| 17856 | fn from(value: u32) -> Self { |
| 17857 | Self(value) |
| 17858 | } |
| 17859 | } |
| 17860 | impl core::fmt::Debug for HierarchyMask { |
| 17861 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 17862 | let variants: [(u32, &'static str, &'static …); 8] = [ |
| 17863 | (Self::MASTER_ADDED.0, "MASTER_ADDED" , "MasterAdded" ), |
| 17864 | (Self::MASTER_REMOVED.0, "MASTER_REMOVED" , "MasterRemoved" ), |
| 17865 | (Self::SLAVE_ADDED.0, "SLAVE_ADDED" , "SlaveAdded" ), |
| 17866 | (Self::SLAVE_REMOVED.0, "SLAVE_REMOVED" , "SlaveRemoved" ), |
| 17867 | (Self::SLAVE_ATTACHED.0, "SLAVE_ATTACHED" , "SlaveAttached" ), |
| 17868 | (Self::SLAVE_DETACHED.0, "SLAVE_DETACHED" , "SlaveDetached" ), |
| 17869 | (Self::DEVICE_ENABLED.0, "DEVICE_ENABLED" , "DeviceEnabled" ), |
| 17870 | (Self::DEVICE_DISABLED.0, "DEVICE_DISABLED" , "DeviceDisabled" ), |
| 17871 | ]; |
| 17872 | pretty_print_bitmask(fmt, self.0, &variants) |
| 17873 | } |
| 17874 | } |
| 17875 | bitmask_binop!(HierarchyMask, u32); |
| 17876 | |
| 17877 | #[derive (Clone, Copy, Default)] |
| 17878 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 17879 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 17880 | pub struct HierarchyInfo { |
| 17881 | pub deviceid: DeviceId, |
| 17882 | pub attachment: DeviceId, |
| 17883 | pub type_: DeviceType, |
| 17884 | pub enabled: bool, |
| 17885 | pub flags: HierarchyMask, |
| 17886 | } |
| 17887 | impl_debug_if_no_extra_traits!(HierarchyInfo, "HierarchyInfo" ); |
| 17888 | impl TryParse for HierarchyInfo { |
| 17889 | fn try_parse(remaining: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 17890 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 17891 | let (attachment: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 17892 | let (type_: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 17893 | let (enabled: bool, remaining: &[u8]) = bool::try_parse(remaining)?; |
| 17894 | let remaining: &[u8] = remaining.get(2..).ok_or(err:ParseError::InsufficientData)?; |
| 17895 | let (flags: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 17896 | let type_: DeviceType = type_.into(); |
| 17897 | let flags: HierarchyMask = flags.into(); |
| 17898 | let result: HierarchyInfo = HierarchyInfo { deviceid, attachment, type_, enabled, flags }; |
| 17899 | Ok((result, remaining)) |
| 17900 | } |
| 17901 | } |
| 17902 | impl Serialize for HierarchyInfo { |
| 17903 | type Bytes = [u8; 12]; |
| 17904 | fn serialize(&self) -> [u8; 12] { |
| 17905 | let deviceid_bytes = self.deviceid.serialize(); |
| 17906 | let attachment_bytes = self.attachment.serialize(); |
| 17907 | let type_bytes = (u16::from(self.type_) as u8).serialize(); |
| 17908 | let enabled_bytes = self.enabled.serialize(); |
| 17909 | let flags_bytes = u32::from(self.flags).serialize(); |
| 17910 | [ |
| 17911 | deviceid_bytes[0], |
| 17912 | deviceid_bytes[1], |
| 17913 | attachment_bytes[0], |
| 17914 | attachment_bytes[1], |
| 17915 | type_bytes[0], |
| 17916 | enabled_bytes[0], |
| 17917 | 0, |
| 17918 | 0, |
| 17919 | flags_bytes[0], |
| 17920 | flags_bytes[1], |
| 17921 | flags_bytes[2], |
| 17922 | flags_bytes[3], |
| 17923 | ] |
| 17924 | } |
| 17925 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 17926 | bytes.reserve(12); |
| 17927 | self.deviceid.serialize_into(bytes); |
| 17928 | self.attachment.serialize_into(bytes); |
| 17929 | (u16::from(self.type_) as u8).serialize_into(bytes); |
| 17930 | self.enabled.serialize_into(bytes); |
| 17931 | bytes.extend_from_slice(&[0; 2]); |
| 17932 | u32::from(self.flags).serialize_into(bytes); |
| 17933 | } |
| 17934 | } |
| 17935 | |
| 17936 | /// Opcode for the Hierarchy event |
| 17937 | pub const HIERARCHY_EVENT: u16 = 11; |
| 17938 | #[derive (Clone, Default)] |
| 17939 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 17940 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 17941 | pub struct HierarchyEvent { |
| 17942 | pub response_type: u8, |
| 17943 | pub extension: u8, |
| 17944 | pub sequence: u16, |
| 17945 | pub length: u32, |
| 17946 | pub event_type: u16, |
| 17947 | pub deviceid: DeviceId, |
| 17948 | pub time: xproto::Timestamp, |
| 17949 | pub flags: HierarchyMask, |
| 17950 | pub infos: Vec<HierarchyInfo>, |
| 17951 | } |
| 17952 | impl_debug_if_no_extra_traits!(HierarchyEvent, "HierarchyEvent" ); |
| 17953 | impl TryParse for HierarchyEvent { |
| 17954 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 17955 | let remaining: &[u8] = initial_value; |
| 17956 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 17957 | let (extension: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 17958 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 17959 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 17960 | let (event_type: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 17961 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 17962 | let (time: u32, remaining: &[u8]) = xproto::Timestamp::try_parse(remaining)?; |
| 17963 | let (flags: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 17964 | let (num_infos: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 17965 | let remaining: &[u8] = remaining.get(10..).ok_or(err:ParseError::InsufficientData)?; |
| 17966 | let (infos: Vec, remaining: &[u8]) = crate::x11_utils::parse_list::<HierarchyInfo>(data:remaining, list_length:num_infos.try_to_usize()?)?; |
| 17967 | let flags: HierarchyMask = flags.into(); |
| 17968 | let result: HierarchyEvent = HierarchyEvent { response_type, extension, sequence, length, event_type, deviceid, time, flags, infos }; |
| 17969 | let _ = remaining; |
| 17970 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 17971 | .ok_or(err:ParseError::InsufficientData)?; |
| 17972 | Ok((result, remaining)) |
| 17973 | } |
| 17974 | } |
| 17975 | impl Serialize for HierarchyEvent { |
| 17976 | type Bytes = Vec<u8>; |
| 17977 | fn serialize(&self) -> Vec<u8> { |
| 17978 | let mut result: Vec = Vec::new(); |
| 17979 | self.serialize_into(&mut result); |
| 17980 | result |
| 17981 | } |
| 17982 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 17983 | bytes.reserve(additional:32); |
| 17984 | self.response_type.serialize_into(bytes); |
| 17985 | self.extension.serialize_into(bytes); |
| 17986 | self.sequence.serialize_into(bytes); |
| 17987 | self.length.serialize_into(bytes); |
| 17988 | self.event_type.serialize_into(bytes); |
| 17989 | self.deviceid.serialize_into(bytes); |
| 17990 | self.time.serialize_into(bytes); |
| 17991 | u32::from(self.flags).serialize_into(bytes); |
| 17992 | let num_infos: u16 = u16::try_from(self.infos.len()).expect(msg:"`infos` has too many elements" ); |
| 17993 | num_infos.serialize_into(bytes); |
| 17994 | bytes.extend_from_slice(&[0; 10]); |
| 17995 | self.infos.serialize_into(bytes); |
| 17996 | } |
| 17997 | } |
| 17998 | impl HierarchyEvent { |
| 17999 | /// Get the value of the `num_infos` field. |
| 18000 | /// |
| 18001 | /// The `num_infos` field is used as the length field of the `infos` field. |
| 18002 | /// This function computes the field's value again based on the length of the list. |
| 18003 | /// |
| 18004 | /// # Panics |
| 18005 | /// |
| 18006 | /// Panics if the value cannot be represented in the target type. This |
| 18007 | /// cannot happen with values of the struct received from the X11 server. |
| 18008 | pub fn num_infos(&self) -> u16 { |
| 18009 | self.infos.len() |
| 18010 | .try_into().unwrap() |
| 18011 | } |
| 18012 | } |
| 18013 | |
| 18014 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 18015 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 18016 | pub struct PropertyFlag(u8); |
| 18017 | impl PropertyFlag { |
| 18018 | pub const DELETED: Self = Self(0); |
| 18019 | pub const CREATED: Self = Self(1); |
| 18020 | pub const MODIFIED: Self = Self(2); |
| 18021 | } |
| 18022 | impl From<PropertyFlag> for u8 { |
| 18023 | #[inline ] |
| 18024 | fn from(input: PropertyFlag) -> Self { |
| 18025 | input.0 |
| 18026 | } |
| 18027 | } |
| 18028 | impl From<PropertyFlag> for Option<u8> { |
| 18029 | #[inline ] |
| 18030 | fn from(input: PropertyFlag) -> Self { |
| 18031 | Some(input.0) |
| 18032 | } |
| 18033 | } |
| 18034 | impl From<PropertyFlag> for u16 { |
| 18035 | #[inline ] |
| 18036 | fn from(input: PropertyFlag) -> Self { |
| 18037 | u16::from(input.0) |
| 18038 | } |
| 18039 | } |
| 18040 | impl From<PropertyFlag> for Option<u16> { |
| 18041 | #[inline ] |
| 18042 | fn from(input: PropertyFlag) -> Self { |
| 18043 | Some(u16::from(input.0)) |
| 18044 | } |
| 18045 | } |
| 18046 | impl From<PropertyFlag> for u32 { |
| 18047 | #[inline ] |
| 18048 | fn from(input: PropertyFlag) -> Self { |
| 18049 | u32::from(input.0) |
| 18050 | } |
| 18051 | } |
| 18052 | impl From<PropertyFlag> for Option<u32> { |
| 18053 | #[inline ] |
| 18054 | fn from(input: PropertyFlag) -> Self { |
| 18055 | Some(u32::from(input.0)) |
| 18056 | } |
| 18057 | } |
| 18058 | impl From<u8> for PropertyFlag { |
| 18059 | #[inline ] |
| 18060 | fn from(value: u8) -> Self { |
| 18061 | Self(value) |
| 18062 | } |
| 18063 | } |
| 18064 | impl core::fmt::Debug for PropertyFlag { |
| 18065 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 18066 | let variants: [(u32, &'static str, &'static …); 3] = [ |
| 18067 | (Self::DELETED.0.into(), "DELETED" , "Deleted" ), |
| 18068 | (Self::CREATED.0.into(), "CREATED" , "Created" ), |
| 18069 | (Self::MODIFIED.0.into(), "MODIFIED" , "Modified" ), |
| 18070 | ]; |
| 18071 | pretty_print_enum(fmt, self.0.into(), &variants) |
| 18072 | } |
| 18073 | } |
| 18074 | |
| 18075 | /// Opcode for the Property event |
| 18076 | pub const PROPERTY_EVENT: u16 = 12; |
| 18077 | #[derive (Clone, Copy, Default)] |
| 18078 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 18079 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 18080 | pub struct PropertyEvent { |
| 18081 | pub response_type: u8, |
| 18082 | pub extension: u8, |
| 18083 | pub sequence: u16, |
| 18084 | pub length: u32, |
| 18085 | pub event_type: u16, |
| 18086 | pub deviceid: DeviceId, |
| 18087 | pub time: xproto::Timestamp, |
| 18088 | pub property: xproto::Atom, |
| 18089 | pub what: PropertyFlag, |
| 18090 | } |
| 18091 | impl_debug_if_no_extra_traits!(PropertyEvent, "PropertyEvent" ); |
| 18092 | impl TryParse for PropertyEvent { |
| 18093 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 18094 | let remaining: &[u8] = initial_value; |
| 18095 | let (response_type: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 18096 | let (extension: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 18097 | let (sequence: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 18098 | let (length: u32, remaining: &[u8]) = u32::try_parse(remaining)?; |
| 18099 | let (event_type: u16, remaining: &[u8]) = u16::try_parse(remaining)?; |
| 18100 | let (deviceid: u16, remaining: &[u8]) = DeviceId::try_parse(remaining)?; |
| 18101 | let (time: u32, remaining: &[u8]) = xproto::Timestamp::try_parse(remaining)?; |
| 18102 | let (property: u32, remaining: &[u8]) = xproto::Atom::try_parse(remaining)?; |
| 18103 | let (what: u8, remaining: &[u8]) = u8::try_parse(remaining)?; |
| 18104 | let remaining: &[u8] = remaining.get(11..).ok_or(err:ParseError::InsufficientData)?; |
| 18105 | let what: PropertyFlag = what.into(); |
| 18106 | let result: PropertyEvent = PropertyEvent { response_type, extension, sequence, length, event_type, deviceid, time, property, what }; |
| 18107 | let _ = remaining; |
| 18108 | let remaining: &[u8] = initial_value.get(32 + length as usize * 4..) |
| 18109 | .ok_or(err:ParseError::InsufficientData)?; |
| 18110 | Ok((result, remaining)) |
| 18111 | } |
| 18112 | } |
| 18113 | impl Serialize for PropertyEvent { |
| 18114 | type Bytes = [u8; 32]; |
| 18115 | fn serialize(&self) -> [u8; 32] { |
| 18116 | let response_type_bytes = self.response_type.serialize(); |
| 18117 | let extension_bytes = self.extension.serialize(); |
| 18118 | let sequence_bytes = self.sequence.serialize(); |
| 18119 | let length_bytes = self.length.serialize(); |
| 18120 | let event_type_bytes = self.event_type.serialize(); |
| 18121 | let deviceid_bytes = self.deviceid.serialize(); |
| 18122 | let time_bytes = self.time.serialize(); |
| 18123 | let property_bytes = self.property.serialize(); |
| 18124 | let what_bytes = u8::from(self.what).serialize(); |
| 18125 | [ |
| 18126 | response_type_bytes[0], |
| 18127 | extension_bytes[0], |
| 18128 | sequence_bytes[0], |
| 18129 | sequence_bytes[1], |
| 18130 | length_bytes[0], |
| 18131 | length_bytes[1], |
| 18132 | length_bytes[2], |
| 18133 | length_bytes[3], |
| 18134 | event_type_bytes[0], |
| 18135 | event_type_bytes[1], |
| 18136 | deviceid_bytes[0], |
| 18137 | deviceid_bytes[1], |
| 18138 | time_bytes[0], |
| 18139 | time_bytes[1], |
| 18140 | time_bytes[2], |
| 18141 | time_bytes[3], |
| 18142 | property_bytes[0], |
| 18143 | property_bytes[1], |
| 18144 | property_bytes[2], |
| 18145 | property_bytes[3], |
| 18146 | what_bytes[0], |
| 18147 | 0, |
| 18148 | 0, |
| 18149 | 0, |
| 18150 | 0, |
| 18151 | 0, |
| 18152 | 0, |
| 18153 | 0, |
| 18154 | 0, |
| 18155 | 0, |
| 18156 | 0, |
| 18157 | 0, |
| 18158 | ] |
| 18159 | } |
| 18160 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 18161 | bytes.reserve(32); |
| 18162 | self.response_type.serialize_into(bytes); |
| 18163 | self.extension.serialize_into(bytes); |
| 18164 | self.sequence.serialize_into(bytes); |
| 18165 | self.length.serialize_into(bytes); |
| 18166 | self.event_type.serialize_into(bytes); |
| 18167 | self.deviceid.serialize_into(bytes); |
| 18168 | self.time.serialize_into(bytes); |
| 18169 | self.property.serialize_into(bytes); |
| 18170 | u8::from(self.what).serialize_into(bytes); |
| 18171 | bytes.extend_from_slice(&[0; 11]); |
| 18172 | } |
| 18173 | } |
| 18174 | |
| 18175 | /// Opcode for the RawKeyPress event |
| 18176 | pub const RAW_KEY_PRESS_EVENT: u16 = 13; |
| 18177 | #[derive (Clone, Default)] |
| 18178 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 18179 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 18180 | pub struct RawKeyPressEvent { |
| 18181 | pub response_type: u8, |
| 18182 | pub extension: u8, |
| 18183 | pub sequence: u16, |
| 18184 | pub length: u32, |
| 18185 | pub event_type: u16, |
| 18186 | pub deviceid: DeviceId, |
| 18187 | pub time: xproto::Timestamp, |
| 18188 | pub detail: u32, |
| 18189 | pub sourceid: DeviceId, |
| 18190 | pub flags: KeyEventFlags, |
| 18191 | pub valuator_mask: Vec<u32>, |
| 18192 | pub axisvalues: Vec<Fp3232>, |
| 18193 | pub axisvalues_raw: Vec<Fp3232>, |
| 18194 | } |
| 18195 | impl_debug_if_no_extra_traits!(RawKeyPressEvent, "RawKeyPressEvent" ); |
| 18196 | impl TryParse for RawKeyPressEvent { |
| 18197 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 18198 | let remaining = initial_value; |
| 18199 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 18200 | let (extension, remaining) = u8::try_parse(remaining)?; |
| 18201 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 18202 | let (length, remaining) = u32::try_parse(remaining)?; |
| 18203 | let (event_type, remaining) = u16::try_parse(remaining)?; |
| 18204 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 18205 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 18206 | let (detail, remaining) = u32::try_parse(remaining)?; |
| 18207 | let (sourceid, remaining) = DeviceId::try_parse(remaining)?; |
| 18208 | let (valuators_len, remaining) = u16::try_parse(remaining)?; |
| 18209 | let (flags, remaining) = u32::try_parse(remaining)?; |
| 18210 | let remaining = remaining.get(4..).ok_or(ParseError::InsufficientData)?; |
| 18211 | let (valuator_mask, remaining) = crate::x11_utils::parse_list::<u32>(remaining, valuators_len.try_to_usize()?)?; |
| 18212 | let (axisvalues, remaining) = crate::x11_utils::parse_list::<Fp3232>(remaining, valuator_mask.iter().try_fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).ok_or(ParseError::InvalidExpression))?.try_to_usize()?)?; |
| 18213 | let (axisvalues_raw, remaining) = crate::x11_utils::parse_list::<Fp3232>(remaining, valuator_mask.iter().try_fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).ok_or(ParseError::InvalidExpression))?.try_to_usize()?)?; |
| 18214 | let flags = flags.into(); |
| 18215 | let result = RawKeyPressEvent { response_type, extension, sequence, length, event_type, deviceid, time, detail, sourceid, flags, valuator_mask, axisvalues, axisvalues_raw }; |
| 18216 | let _ = remaining; |
| 18217 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 18218 | .ok_or(ParseError::InsufficientData)?; |
| 18219 | Ok((result, remaining)) |
| 18220 | } |
| 18221 | } |
| 18222 | impl Serialize for RawKeyPressEvent { |
| 18223 | type Bytes = Vec<u8>; |
| 18224 | fn serialize(&self) -> Vec<u8> { |
| 18225 | let mut result = Vec::new(); |
| 18226 | self.serialize_into(&mut result); |
| 18227 | result |
| 18228 | } |
| 18229 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 18230 | bytes.reserve(32); |
| 18231 | self.response_type.serialize_into(bytes); |
| 18232 | self.extension.serialize_into(bytes); |
| 18233 | self.sequence.serialize_into(bytes); |
| 18234 | self.length.serialize_into(bytes); |
| 18235 | self.event_type.serialize_into(bytes); |
| 18236 | self.deviceid.serialize_into(bytes); |
| 18237 | self.time.serialize_into(bytes); |
| 18238 | self.detail.serialize_into(bytes); |
| 18239 | self.sourceid.serialize_into(bytes); |
| 18240 | let valuators_len = u16::try_from(self.valuator_mask.len()).expect("`valuator_mask` has too many elements" ); |
| 18241 | valuators_len.serialize_into(bytes); |
| 18242 | u32::from(self.flags).serialize_into(bytes); |
| 18243 | bytes.extend_from_slice(&[0; 4]); |
| 18244 | self.valuator_mask.serialize_into(bytes); |
| 18245 | assert_eq!(self.axisvalues.len(), usize::try_from(self.valuator_mask.iter().fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).unwrap())).unwrap(), "`axisvalues` has an incorrect length" ); |
| 18246 | self.axisvalues.serialize_into(bytes); |
| 18247 | assert_eq!(self.axisvalues_raw.len(), usize::try_from(self.valuator_mask.iter().fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).unwrap())).unwrap(), "`axisvalues_raw` has an incorrect length" ); |
| 18248 | self.axisvalues_raw.serialize_into(bytes); |
| 18249 | } |
| 18250 | } |
| 18251 | impl RawKeyPressEvent { |
| 18252 | /// Get the value of the `valuators_len` field. |
| 18253 | /// |
| 18254 | /// The `valuators_len` field is used as the length field of the `valuator_mask` field. |
| 18255 | /// This function computes the field's value again based on the length of the list. |
| 18256 | /// |
| 18257 | /// # Panics |
| 18258 | /// |
| 18259 | /// Panics if the value cannot be represented in the target type. This |
| 18260 | /// cannot happen with values of the struct received from the X11 server. |
| 18261 | pub fn valuators_len(&self) -> u16 { |
| 18262 | self.valuator_mask.len() |
| 18263 | .try_into().unwrap() |
| 18264 | } |
| 18265 | } |
| 18266 | |
| 18267 | /// Opcode for the RawKeyRelease event |
| 18268 | pub const RAW_KEY_RELEASE_EVENT: u16 = 14; |
| 18269 | pub type RawKeyReleaseEvent = RawKeyPressEvent; |
| 18270 | |
| 18271 | /// Opcode for the RawButtonPress event |
| 18272 | pub const RAW_BUTTON_PRESS_EVENT: u16 = 15; |
| 18273 | #[derive (Clone, Default)] |
| 18274 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 18275 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 18276 | pub struct RawButtonPressEvent { |
| 18277 | pub response_type: u8, |
| 18278 | pub extension: u8, |
| 18279 | pub sequence: u16, |
| 18280 | pub length: u32, |
| 18281 | pub event_type: u16, |
| 18282 | pub deviceid: DeviceId, |
| 18283 | pub time: xproto::Timestamp, |
| 18284 | pub detail: u32, |
| 18285 | pub sourceid: DeviceId, |
| 18286 | pub flags: PointerEventFlags, |
| 18287 | pub valuator_mask: Vec<u32>, |
| 18288 | pub axisvalues: Vec<Fp3232>, |
| 18289 | pub axisvalues_raw: Vec<Fp3232>, |
| 18290 | } |
| 18291 | impl_debug_if_no_extra_traits!(RawButtonPressEvent, "RawButtonPressEvent" ); |
| 18292 | impl TryParse for RawButtonPressEvent { |
| 18293 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 18294 | let remaining = initial_value; |
| 18295 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 18296 | let (extension, remaining) = u8::try_parse(remaining)?; |
| 18297 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 18298 | let (length, remaining) = u32::try_parse(remaining)?; |
| 18299 | let (event_type, remaining) = u16::try_parse(remaining)?; |
| 18300 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 18301 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 18302 | let (detail, remaining) = u32::try_parse(remaining)?; |
| 18303 | let (sourceid, remaining) = DeviceId::try_parse(remaining)?; |
| 18304 | let (valuators_len, remaining) = u16::try_parse(remaining)?; |
| 18305 | let (flags, remaining) = u32::try_parse(remaining)?; |
| 18306 | let remaining = remaining.get(4..).ok_or(ParseError::InsufficientData)?; |
| 18307 | let (valuator_mask, remaining) = crate::x11_utils::parse_list::<u32>(remaining, valuators_len.try_to_usize()?)?; |
| 18308 | let (axisvalues, remaining) = crate::x11_utils::parse_list::<Fp3232>(remaining, valuator_mask.iter().try_fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).ok_or(ParseError::InvalidExpression))?.try_to_usize()?)?; |
| 18309 | let (axisvalues_raw, remaining) = crate::x11_utils::parse_list::<Fp3232>(remaining, valuator_mask.iter().try_fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).ok_or(ParseError::InvalidExpression))?.try_to_usize()?)?; |
| 18310 | let flags = flags.into(); |
| 18311 | let result = RawButtonPressEvent { response_type, extension, sequence, length, event_type, deviceid, time, detail, sourceid, flags, valuator_mask, axisvalues, axisvalues_raw }; |
| 18312 | let _ = remaining; |
| 18313 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 18314 | .ok_or(ParseError::InsufficientData)?; |
| 18315 | Ok((result, remaining)) |
| 18316 | } |
| 18317 | } |
| 18318 | impl Serialize for RawButtonPressEvent { |
| 18319 | type Bytes = Vec<u8>; |
| 18320 | fn serialize(&self) -> Vec<u8> { |
| 18321 | let mut result = Vec::new(); |
| 18322 | self.serialize_into(&mut result); |
| 18323 | result |
| 18324 | } |
| 18325 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 18326 | bytes.reserve(32); |
| 18327 | self.response_type.serialize_into(bytes); |
| 18328 | self.extension.serialize_into(bytes); |
| 18329 | self.sequence.serialize_into(bytes); |
| 18330 | self.length.serialize_into(bytes); |
| 18331 | self.event_type.serialize_into(bytes); |
| 18332 | self.deviceid.serialize_into(bytes); |
| 18333 | self.time.serialize_into(bytes); |
| 18334 | self.detail.serialize_into(bytes); |
| 18335 | self.sourceid.serialize_into(bytes); |
| 18336 | let valuators_len = u16::try_from(self.valuator_mask.len()).expect("`valuator_mask` has too many elements" ); |
| 18337 | valuators_len.serialize_into(bytes); |
| 18338 | u32::from(self.flags).serialize_into(bytes); |
| 18339 | bytes.extend_from_slice(&[0; 4]); |
| 18340 | self.valuator_mask.serialize_into(bytes); |
| 18341 | assert_eq!(self.axisvalues.len(), usize::try_from(self.valuator_mask.iter().fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).unwrap())).unwrap(), "`axisvalues` has an incorrect length" ); |
| 18342 | self.axisvalues.serialize_into(bytes); |
| 18343 | assert_eq!(self.axisvalues_raw.len(), usize::try_from(self.valuator_mask.iter().fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).unwrap())).unwrap(), "`axisvalues_raw` has an incorrect length" ); |
| 18344 | self.axisvalues_raw.serialize_into(bytes); |
| 18345 | } |
| 18346 | } |
| 18347 | impl RawButtonPressEvent { |
| 18348 | /// Get the value of the `valuators_len` field. |
| 18349 | /// |
| 18350 | /// The `valuators_len` field is used as the length field of the `valuator_mask` field. |
| 18351 | /// This function computes the field's value again based on the length of the list. |
| 18352 | /// |
| 18353 | /// # Panics |
| 18354 | /// |
| 18355 | /// Panics if the value cannot be represented in the target type. This |
| 18356 | /// cannot happen with values of the struct received from the X11 server. |
| 18357 | pub fn valuators_len(&self) -> u16 { |
| 18358 | self.valuator_mask.len() |
| 18359 | .try_into().unwrap() |
| 18360 | } |
| 18361 | } |
| 18362 | |
| 18363 | /// Opcode for the RawButtonRelease event |
| 18364 | pub const RAW_BUTTON_RELEASE_EVENT: u16 = 16; |
| 18365 | pub type RawButtonReleaseEvent = RawButtonPressEvent; |
| 18366 | |
| 18367 | /// Opcode for the RawMotion event |
| 18368 | pub const RAW_MOTION_EVENT: u16 = 17; |
| 18369 | pub type RawMotionEvent = RawButtonPressEvent; |
| 18370 | |
| 18371 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 18372 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 18373 | pub struct TouchEventFlags(u32); |
| 18374 | impl TouchEventFlags { |
| 18375 | pub const TOUCH_PENDING_END: Self = Self(1 << 16); |
| 18376 | pub const TOUCH_EMULATING_POINTER: Self = Self(1 << 17); |
| 18377 | } |
| 18378 | impl From<TouchEventFlags> for u32 { |
| 18379 | #[inline ] |
| 18380 | fn from(input: TouchEventFlags) -> Self { |
| 18381 | input.0 |
| 18382 | } |
| 18383 | } |
| 18384 | impl From<TouchEventFlags> for Option<u32> { |
| 18385 | #[inline ] |
| 18386 | fn from(input: TouchEventFlags) -> Self { |
| 18387 | Some(input.0) |
| 18388 | } |
| 18389 | } |
| 18390 | impl From<u8> for TouchEventFlags { |
| 18391 | #[inline ] |
| 18392 | fn from(value: u8) -> Self { |
| 18393 | Self(value.into()) |
| 18394 | } |
| 18395 | } |
| 18396 | impl From<u16> for TouchEventFlags { |
| 18397 | #[inline ] |
| 18398 | fn from(value: u16) -> Self { |
| 18399 | Self(value.into()) |
| 18400 | } |
| 18401 | } |
| 18402 | impl From<u32> for TouchEventFlags { |
| 18403 | #[inline ] |
| 18404 | fn from(value: u32) -> Self { |
| 18405 | Self(value) |
| 18406 | } |
| 18407 | } |
| 18408 | impl core::fmt::Debug for TouchEventFlags { |
| 18409 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 18410 | let variants: [(u32, &'static str, &'static …); 2] = [ |
| 18411 | (Self::TOUCH_PENDING_END.0, "TOUCH_PENDING_END" , "TouchPendingEnd" ), |
| 18412 | (Self::TOUCH_EMULATING_POINTER.0, "TOUCH_EMULATING_POINTER" , "TouchEmulatingPointer" ), |
| 18413 | ]; |
| 18414 | pretty_print_bitmask(fmt, self.0, &variants) |
| 18415 | } |
| 18416 | } |
| 18417 | bitmask_binop!(TouchEventFlags, u32); |
| 18418 | |
| 18419 | /// Opcode for the TouchBegin event |
| 18420 | pub const TOUCH_BEGIN_EVENT: u16 = 18; |
| 18421 | #[derive (Clone, Default)] |
| 18422 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 18423 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 18424 | pub struct TouchBeginEvent { |
| 18425 | pub response_type: u8, |
| 18426 | pub extension: u8, |
| 18427 | pub sequence: u16, |
| 18428 | pub length: u32, |
| 18429 | pub event_type: u16, |
| 18430 | pub deviceid: DeviceId, |
| 18431 | pub time: xproto::Timestamp, |
| 18432 | pub detail: u32, |
| 18433 | pub root: xproto::Window, |
| 18434 | pub event: xproto::Window, |
| 18435 | pub child: xproto::Window, |
| 18436 | pub root_x: Fp1616, |
| 18437 | pub root_y: Fp1616, |
| 18438 | pub event_x: Fp1616, |
| 18439 | pub event_y: Fp1616, |
| 18440 | pub sourceid: DeviceId, |
| 18441 | pub flags: TouchEventFlags, |
| 18442 | pub mods: ModifierInfo, |
| 18443 | pub group: GroupInfo, |
| 18444 | pub button_mask: Vec<u32>, |
| 18445 | pub valuator_mask: Vec<u32>, |
| 18446 | pub axisvalues: Vec<Fp3232>, |
| 18447 | } |
| 18448 | impl_debug_if_no_extra_traits!(TouchBeginEvent, "TouchBeginEvent" ); |
| 18449 | impl TryParse for TouchBeginEvent { |
| 18450 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 18451 | let remaining = initial_value; |
| 18452 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 18453 | let (extension, remaining) = u8::try_parse(remaining)?; |
| 18454 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 18455 | let (length, remaining) = u32::try_parse(remaining)?; |
| 18456 | let (event_type, remaining) = u16::try_parse(remaining)?; |
| 18457 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 18458 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 18459 | let (detail, remaining) = u32::try_parse(remaining)?; |
| 18460 | let (root, remaining) = xproto::Window::try_parse(remaining)?; |
| 18461 | let (event, remaining) = xproto::Window::try_parse(remaining)?; |
| 18462 | let (child, remaining) = xproto::Window::try_parse(remaining)?; |
| 18463 | let (root_x, remaining) = Fp1616::try_parse(remaining)?; |
| 18464 | let (root_y, remaining) = Fp1616::try_parse(remaining)?; |
| 18465 | let (event_x, remaining) = Fp1616::try_parse(remaining)?; |
| 18466 | let (event_y, remaining) = Fp1616::try_parse(remaining)?; |
| 18467 | let (buttons_len, remaining) = u16::try_parse(remaining)?; |
| 18468 | let (valuators_len, remaining) = u16::try_parse(remaining)?; |
| 18469 | let (sourceid, remaining) = DeviceId::try_parse(remaining)?; |
| 18470 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 18471 | let (flags, remaining) = u32::try_parse(remaining)?; |
| 18472 | let (mods, remaining) = ModifierInfo::try_parse(remaining)?; |
| 18473 | let (group, remaining) = GroupInfo::try_parse(remaining)?; |
| 18474 | let (button_mask, remaining) = crate::x11_utils::parse_list::<u32>(remaining, buttons_len.try_to_usize()?)?; |
| 18475 | let (valuator_mask, remaining) = crate::x11_utils::parse_list::<u32>(remaining, valuators_len.try_to_usize()?)?; |
| 18476 | let (axisvalues, remaining) = crate::x11_utils::parse_list::<Fp3232>(remaining, valuator_mask.iter().try_fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).ok_or(ParseError::InvalidExpression))?.try_to_usize()?)?; |
| 18477 | let flags = flags.into(); |
| 18478 | let result = TouchBeginEvent { response_type, extension, sequence, length, event_type, deviceid, time, detail, root, event, child, root_x, root_y, event_x, event_y, sourceid, flags, mods, group, button_mask, valuator_mask, axisvalues }; |
| 18479 | let _ = remaining; |
| 18480 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 18481 | .ok_or(ParseError::InsufficientData)?; |
| 18482 | Ok((result, remaining)) |
| 18483 | } |
| 18484 | } |
| 18485 | impl Serialize for TouchBeginEvent { |
| 18486 | type Bytes = Vec<u8>; |
| 18487 | fn serialize(&self) -> Vec<u8> { |
| 18488 | let mut result = Vec::new(); |
| 18489 | self.serialize_into(&mut result); |
| 18490 | result |
| 18491 | } |
| 18492 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 18493 | bytes.reserve(80); |
| 18494 | self.response_type.serialize_into(bytes); |
| 18495 | self.extension.serialize_into(bytes); |
| 18496 | self.sequence.serialize_into(bytes); |
| 18497 | self.length.serialize_into(bytes); |
| 18498 | self.event_type.serialize_into(bytes); |
| 18499 | self.deviceid.serialize_into(bytes); |
| 18500 | self.time.serialize_into(bytes); |
| 18501 | self.detail.serialize_into(bytes); |
| 18502 | self.root.serialize_into(bytes); |
| 18503 | self.event.serialize_into(bytes); |
| 18504 | self.child.serialize_into(bytes); |
| 18505 | self.root_x.serialize_into(bytes); |
| 18506 | self.root_y.serialize_into(bytes); |
| 18507 | self.event_x.serialize_into(bytes); |
| 18508 | self.event_y.serialize_into(bytes); |
| 18509 | let buttons_len = u16::try_from(self.button_mask.len()).expect("`button_mask` has too many elements" ); |
| 18510 | buttons_len.serialize_into(bytes); |
| 18511 | let valuators_len = u16::try_from(self.valuator_mask.len()).expect("`valuator_mask` has too many elements" ); |
| 18512 | valuators_len.serialize_into(bytes); |
| 18513 | self.sourceid.serialize_into(bytes); |
| 18514 | bytes.extend_from_slice(&[0; 2]); |
| 18515 | u32::from(self.flags).serialize_into(bytes); |
| 18516 | self.mods.serialize_into(bytes); |
| 18517 | self.group.serialize_into(bytes); |
| 18518 | self.button_mask.serialize_into(bytes); |
| 18519 | self.valuator_mask.serialize_into(bytes); |
| 18520 | assert_eq!(self.axisvalues.len(), usize::try_from(self.valuator_mask.iter().fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).unwrap())).unwrap(), "`axisvalues` has an incorrect length" ); |
| 18521 | self.axisvalues.serialize_into(bytes); |
| 18522 | } |
| 18523 | } |
| 18524 | impl TouchBeginEvent { |
| 18525 | /// Get the value of the `buttons_len` field. |
| 18526 | /// |
| 18527 | /// The `buttons_len` field is used as the length field of the `button_mask` field. |
| 18528 | /// This function computes the field's value again based on the length of the list. |
| 18529 | /// |
| 18530 | /// # Panics |
| 18531 | /// |
| 18532 | /// Panics if the value cannot be represented in the target type. This |
| 18533 | /// cannot happen with values of the struct received from the X11 server. |
| 18534 | pub fn buttons_len(&self) -> u16 { |
| 18535 | self.button_mask.len() |
| 18536 | .try_into().unwrap() |
| 18537 | } |
| 18538 | /// Get the value of the `valuators_len` field. |
| 18539 | /// |
| 18540 | /// The `valuators_len` field is used as the length field of the `valuator_mask` field. |
| 18541 | /// This function computes the field's value again based on the length of the list. |
| 18542 | /// |
| 18543 | /// # Panics |
| 18544 | /// |
| 18545 | /// Panics if the value cannot be represented in the target type. This |
| 18546 | /// cannot happen with values of the struct received from the X11 server. |
| 18547 | pub fn valuators_len(&self) -> u16 { |
| 18548 | self.valuator_mask.len() |
| 18549 | .try_into().unwrap() |
| 18550 | } |
| 18551 | } |
| 18552 | |
| 18553 | /// Opcode for the TouchUpdate event |
| 18554 | pub const TOUCH_UPDATE_EVENT: u16 = 19; |
| 18555 | pub type TouchUpdateEvent = TouchBeginEvent; |
| 18556 | |
| 18557 | /// Opcode for the TouchEnd event |
| 18558 | pub const TOUCH_END_EVENT: u16 = 20; |
| 18559 | pub type TouchEndEvent = TouchBeginEvent; |
| 18560 | |
| 18561 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 18562 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 18563 | pub struct TouchOwnershipFlags(u32); |
| 18564 | impl TouchOwnershipFlags { |
| 18565 | pub const NONE: Self = Self(0); |
| 18566 | } |
| 18567 | impl From<TouchOwnershipFlags> for u32 { |
| 18568 | #[inline ] |
| 18569 | fn from(input: TouchOwnershipFlags) -> Self { |
| 18570 | input.0 |
| 18571 | } |
| 18572 | } |
| 18573 | impl From<TouchOwnershipFlags> for Option<u32> { |
| 18574 | #[inline ] |
| 18575 | fn from(input: TouchOwnershipFlags) -> Self { |
| 18576 | Some(input.0) |
| 18577 | } |
| 18578 | } |
| 18579 | impl From<u8> for TouchOwnershipFlags { |
| 18580 | #[inline ] |
| 18581 | fn from(value: u8) -> Self { |
| 18582 | Self(value.into()) |
| 18583 | } |
| 18584 | } |
| 18585 | impl From<u16> for TouchOwnershipFlags { |
| 18586 | #[inline ] |
| 18587 | fn from(value: u16) -> Self { |
| 18588 | Self(value.into()) |
| 18589 | } |
| 18590 | } |
| 18591 | impl From<u32> for TouchOwnershipFlags { |
| 18592 | #[inline ] |
| 18593 | fn from(value: u32) -> Self { |
| 18594 | Self(value) |
| 18595 | } |
| 18596 | } |
| 18597 | impl core::fmt::Debug for TouchOwnershipFlags { |
| 18598 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 18599 | let variants: [(u32, &'static str, &'static …); 1] = [ |
| 18600 | (Self::NONE.0, "NONE" , "None" ), |
| 18601 | ]; |
| 18602 | pretty_print_enum(fmt, self.0, &variants) |
| 18603 | } |
| 18604 | } |
| 18605 | |
| 18606 | /// Opcode for the TouchOwnership event |
| 18607 | pub const TOUCH_OWNERSHIP_EVENT: u16 = 21; |
| 18608 | #[derive (Clone, Copy, Default)] |
| 18609 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 18610 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 18611 | pub struct TouchOwnershipEvent { |
| 18612 | pub response_type: u8, |
| 18613 | pub extension: u8, |
| 18614 | pub sequence: u16, |
| 18615 | pub length: u32, |
| 18616 | pub event_type: u16, |
| 18617 | pub deviceid: DeviceId, |
| 18618 | pub time: xproto::Timestamp, |
| 18619 | pub touchid: u32, |
| 18620 | pub root: xproto::Window, |
| 18621 | pub event: xproto::Window, |
| 18622 | pub child: xproto::Window, |
| 18623 | pub sourceid: DeviceId, |
| 18624 | pub flags: TouchOwnershipFlags, |
| 18625 | } |
| 18626 | impl_debug_if_no_extra_traits!(TouchOwnershipEvent, "TouchOwnershipEvent" ); |
| 18627 | impl TryParse for TouchOwnershipEvent { |
| 18628 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 18629 | let remaining = initial_value; |
| 18630 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 18631 | let (extension, remaining) = u8::try_parse(remaining)?; |
| 18632 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 18633 | let (length, remaining) = u32::try_parse(remaining)?; |
| 18634 | let (event_type, remaining) = u16::try_parse(remaining)?; |
| 18635 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 18636 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 18637 | let (touchid, remaining) = u32::try_parse(remaining)?; |
| 18638 | let (root, remaining) = xproto::Window::try_parse(remaining)?; |
| 18639 | let (event, remaining) = xproto::Window::try_parse(remaining)?; |
| 18640 | let (child, remaining) = xproto::Window::try_parse(remaining)?; |
| 18641 | let (sourceid, remaining) = DeviceId::try_parse(remaining)?; |
| 18642 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 18643 | let (flags, remaining) = u32::try_parse(remaining)?; |
| 18644 | let remaining = remaining.get(8..).ok_or(ParseError::InsufficientData)?; |
| 18645 | let flags = flags.into(); |
| 18646 | let result = TouchOwnershipEvent { response_type, extension, sequence, length, event_type, deviceid, time, touchid, root, event, child, sourceid, flags }; |
| 18647 | let _ = remaining; |
| 18648 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 18649 | .ok_or(ParseError::InsufficientData)?; |
| 18650 | Ok((result, remaining)) |
| 18651 | } |
| 18652 | } |
| 18653 | impl Serialize for TouchOwnershipEvent { |
| 18654 | type Bytes = [u8; 48]; |
| 18655 | fn serialize(&self) -> [u8; 48] { |
| 18656 | let response_type_bytes = self.response_type.serialize(); |
| 18657 | let extension_bytes = self.extension.serialize(); |
| 18658 | let sequence_bytes = self.sequence.serialize(); |
| 18659 | let length_bytes = self.length.serialize(); |
| 18660 | let event_type_bytes = self.event_type.serialize(); |
| 18661 | let deviceid_bytes = self.deviceid.serialize(); |
| 18662 | let time_bytes = self.time.serialize(); |
| 18663 | let touchid_bytes = self.touchid.serialize(); |
| 18664 | let root_bytes = self.root.serialize(); |
| 18665 | let event_bytes = self.event.serialize(); |
| 18666 | let child_bytes = self.child.serialize(); |
| 18667 | let sourceid_bytes = self.sourceid.serialize(); |
| 18668 | let flags_bytes = u32::from(self.flags).serialize(); |
| 18669 | [ |
| 18670 | response_type_bytes[0], |
| 18671 | extension_bytes[0], |
| 18672 | sequence_bytes[0], |
| 18673 | sequence_bytes[1], |
| 18674 | length_bytes[0], |
| 18675 | length_bytes[1], |
| 18676 | length_bytes[2], |
| 18677 | length_bytes[3], |
| 18678 | event_type_bytes[0], |
| 18679 | event_type_bytes[1], |
| 18680 | deviceid_bytes[0], |
| 18681 | deviceid_bytes[1], |
| 18682 | time_bytes[0], |
| 18683 | time_bytes[1], |
| 18684 | time_bytes[2], |
| 18685 | time_bytes[3], |
| 18686 | touchid_bytes[0], |
| 18687 | touchid_bytes[1], |
| 18688 | touchid_bytes[2], |
| 18689 | touchid_bytes[3], |
| 18690 | root_bytes[0], |
| 18691 | root_bytes[1], |
| 18692 | root_bytes[2], |
| 18693 | root_bytes[3], |
| 18694 | event_bytes[0], |
| 18695 | event_bytes[1], |
| 18696 | event_bytes[2], |
| 18697 | event_bytes[3], |
| 18698 | child_bytes[0], |
| 18699 | child_bytes[1], |
| 18700 | child_bytes[2], |
| 18701 | child_bytes[3], |
| 18702 | sourceid_bytes[0], |
| 18703 | sourceid_bytes[1], |
| 18704 | 0, |
| 18705 | 0, |
| 18706 | flags_bytes[0], |
| 18707 | flags_bytes[1], |
| 18708 | flags_bytes[2], |
| 18709 | flags_bytes[3], |
| 18710 | 0, |
| 18711 | 0, |
| 18712 | 0, |
| 18713 | 0, |
| 18714 | 0, |
| 18715 | 0, |
| 18716 | 0, |
| 18717 | 0, |
| 18718 | ] |
| 18719 | } |
| 18720 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 18721 | bytes.reserve(48); |
| 18722 | self.response_type.serialize_into(bytes); |
| 18723 | self.extension.serialize_into(bytes); |
| 18724 | self.sequence.serialize_into(bytes); |
| 18725 | self.length.serialize_into(bytes); |
| 18726 | self.event_type.serialize_into(bytes); |
| 18727 | self.deviceid.serialize_into(bytes); |
| 18728 | self.time.serialize_into(bytes); |
| 18729 | self.touchid.serialize_into(bytes); |
| 18730 | self.root.serialize_into(bytes); |
| 18731 | self.event.serialize_into(bytes); |
| 18732 | self.child.serialize_into(bytes); |
| 18733 | self.sourceid.serialize_into(bytes); |
| 18734 | bytes.extend_from_slice(&[0; 2]); |
| 18735 | u32::from(self.flags).serialize_into(bytes); |
| 18736 | bytes.extend_from_slice(&[0; 8]); |
| 18737 | } |
| 18738 | } |
| 18739 | |
| 18740 | /// Opcode for the RawTouchBegin event |
| 18741 | pub const RAW_TOUCH_BEGIN_EVENT: u16 = 22; |
| 18742 | #[derive (Clone, Default)] |
| 18743 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 18744 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 18745 | pub struct RawTouchBeginEvent { |
| 18746 | pub response_type: u8, |
| 18747 | pub extension: u8, |
| 18748 | pub sequence: u16, |
| 18749 | pub length: u32, |
| 18750 | pub event_type: u16, |
| 18751 | pub deviceid: DeviceId, |
| 18752 | pub time: xproto::Timestamp, |
| 18753 | pub detail: u32, |
| 18754 | pub sourceid: DeviceId, |
| 18755 | pub flags: TouchEventFlags, |
| 18756 | pub valuator_mask: Vec<u32>, |
| 18757 | pub axisvalues: Vec<Fp3232>, |
| 18758 | pub axisvalues_raw: Vec<Fp3232>, |
| 18759 | } |
| 18760 | impl_debug_if_no_extra_traits!(RawTouchBeginEvent, "RawTouchBeginEvent" ); |
| 18761 | impl TryParse for RawTouchBeginEvent { |
| 18762 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 18763 | let remaining = initial_value; |
| 18764 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 18765 | let (extension, remaining) = u8::try_parse(remaining)?; |
| 18766 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 18767 | let (length, remaining) = u32::try_parse(remaining)?; |
| 18768 | let (event_type, remaining) = u16::try_parse(remaining)?; |
| 18769 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 18770 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 18771 | let (detail, remaining) = u32::try_parse(remaining)?; |
| 18772 | let (sourceid, remaining) = DeviceId::try_parse(remaining)?; |
| 18773 | let (valuators_len, remaining) = u16::try_parse(remaining)?; |
| 18774 | let (flags, remaining) = u32::try_parse(remaining)?; |
| 18775 | let remaining = remaining.get(4..).ok_or(ParseError::InsufficientData)?; |
| 18776 | let (valuator_mask, remaining) = crate::x11_utils::parse_list::<u32>(remaining, valuators_len.try_to_usize()?)?; |
| 18777 | let (axisvalues, remaining) = crate::x11_utils::parse_list::<Fp3232>(remaining, valuator_mask.iter().try_fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).ok_or(ParseError::InvalidExpression))?.try_to_usize()?)?; |
| 18778 | let (axisvalues_raw, remaining) = crate::x11_utils::parse_list::<Fp3232>(remaining, valuator_mask.iter().try_fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).ok_or(ParseError::InvalidExpression))?.try_to_usize()?)?; |
| 18779 | let flags = flags.into(); |
| 18780 | let result = RawTouchBeginEvent { response_type, extension, sequence, length, event_type, deviceid, time, detail, sourceid, flags, valuator_mask, axisvalues, axisvalues_raw }; |
| 18781 | let _ = remaining; |
| 18782 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 18783 | .ok_or(ParseError::InsufficientData)?; |
| 18784 | Ok((result, remaining)) |
| 18785 | } |
| 18786 | } |
| 18787 | impl Serialize for RawTouchBeginEvent { |
| 18788 | type Bytes = Vec<u8>; |
| 18789 | fn serialize(&self) -> Vec<u8> { |
| 18790 | let mut result = Vec::new(); |
| 18791 | self.serialize_into(&mut result); |
| 18792 | result |
| 18793 | } |
| 18794 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 18795 | bytes.reserve(32); |
| 18796 | self.response_type.serialize_into(bytes); |
| 18797 | self.extension.serialize_into(bytes); |
| 18798 | self.sequence.serialize_into(bytes); |
| 18799 | self.length.serialize_into(bytes); |
| 18800 | self.event_type.serialize_into(bytes); |
| 18801 | self.deviceid.serialize_into(bytes); |
| 18802 | self.time.serialize_into(bytes); |
| 18803 | self.detail.serialize_into(bytes); |
| 18804 | self.sourceid.serialize_into(bytes); |
| 18805 | let valuators_len = u16::try_from(self.valuator_mask.len()).expect("`valuator_mask` has too many elements" ); |
| 18806 | valuators_len.serialize_into(bytes); |
| 18807 | u32::from(self.flags).serialize_into(bytes); |
| 18808 | bytes.extend_from_slice(&[0; 4]); |
| 18809 | self.valuator_mask.serialize_into(bytes); |
| 18810 | assert_eq!(self.axisvalues.len(), usize::try_from(self.valuator_mask.iter().fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).unwrap())).unwrap(), "`axisvalues` has an incorrect length" ); |
| 18811 | self.axisvalues.serialize_into(bytes); |
| 18812 | assert_eq!(self.axisvalues_raw.len(), usize::try_from(self.valuator_mask.iter().fold(0u32, |acc, x| acc.checked_add(u32::from(*x).count_ones()).unwrap())).unwrap(), "`axisvalues_raw` has an incorrect length" ); |
| 18813 | self.axisvalues_raw.serialize_into(bytes); |
| 18814 | } |
| 18815 | } |
| 18816 | impl RawTouchBeginEvent { |
| 18817 | /// Get the value of the `valuators_len` field. |
| 18818 | /// |
| 18819 | /// The `valuators_len` field is used as the length field of the `valuator_mask` field. |
| 18820 | /// This function computes the field's value again based on the length of the list. |
| 18821 | /// |
| 18822 | /// # Panics |
| 18823 | /// |
| 18824 | /// Panics if the value cannot be represented in the target type. This |
| 18825 | /// cannot happen with values of the struct received from the X11 server. |
| 18826 | pub fn valuators_len(&self) -> u16 { |
| 18827 | self.valuator_mask.len() |
| 18828 | .try_into().unwrap() |
| 18829 | } |
| 18830 | } |
| 18831 | |
| 18832 | /// Opcode for the RawTouchUpdate event |
| 18833 | pub const RAW_TOUCH_UPDATE_EVENT: u16 = 23; |
| 18834 | pub type RawTouchUpdateEvent = RawTouchBeginEvent; |
| 18835 | |
| 18836 | /// Opcode for the RawTouchEnd event |
| 18837 | pub const RAW_TOUCH_END_EVENT: u16 = 24; |
| 18838 | pub type RawTouchEndEvent = RawTouchBeginEvent; |
| 18839 | |
| 18840 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 18841 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 18842 | pub struct BarrierFlags(u32); |
| 18843 | impl BarrierFlags { |
| 18844 | pub const POINTER_RELEASED: Self = Self(1 << 0); |
| 18845 | pub const DEVICE_IS_GRABBED: Self = Self(1 << 1); |
| 18846 | } |
| 18847 | impl From<BarrierFlags> for u32 { |
| 18848 | #[inline ] |
| 18849 | fn from(input: BarrierFlags) -> Self { |
| 18850 | input.0 |
| 18851 | } |
| 18852 | } |
| 18853 | impl From<BarrierFlags> for Option<u32> { |
| 18854 | #[inline ] |
| 18855 | fn from(input: BarrierFlags) -> Self { |
| 18856 | Some(input.0) |
| 18857 | } |
| 18858 | } |
| 18859 | impl From<u8> for BarrierFlags { |
| 18860 | #[inline ] |
| 18861 | fn from(value: u8) -> Self { |
| 18862 | Self(value.into()) |
| 18863 | } |
| 18864 | } |
| 18865 | impl From<u16> for BarrierFlags { |
| 18866 | #[inline ] |
| 18867 | fn from(value: u16) -> Self { |
| 18868 | Self(value.into()) |
| 18869 | } |
| 18870 | } |
| 18871 | impl From<u32> for BarrierFlags { |
| 18872 | #[inline ] |
| 18873 | fn from(value: u32) -> Self { |
| 18874 | Self(value) |
| 18875 | } |
| 18876 | } |
| 18877 | impl core::fmt::Debug for BarrierFlags { |
| 18878 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 18879 | let variants: [(u32, &'static str, &'static …); 2] = [ |
| 18880 | (Self::POINTER_RELEASED.0, "POINTER_RELEASED" , "PointerReleased" ), |
| 18881 | (Self::DEVICE_IS_GRABBED.0, "DEVICE_IS_GRABBED" , "DeviceIsGrabbed" ), |
| 18882 | ]; |
| 18883 | pretty_print_bitmask(fmt, self.0, &variants) |
| 18884 | } |
| 18885 | } |
| 18886 | bitmask_binop!(BarrierFlags, u32); |
| 18887 | |
| 18888 | /// Opcode for the BarrierHit event |
| 18889 | pub const BARRIER_HIT_EVENT: u16 = 25; |
| 18890 | #[derive (Clone, Copy, Default)] |
| 18891 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 18892 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 18893 | pub struct BarrierHitEvent { |
| 18894 | pub response_type: u8, |
| 18895 | pub extension: u8, |
| 18896 | pub sequence: u16, |
| 18897 | pub length: u32, |
| 18898 | pub event_type: u16, |
| 18899 | pub deviceid: DeviceId, |
| 18900 | pub time: xproto::Timestamp, |
| 18901 | pub eventid: u32, |
| 18902 | pub root: xproto::Window, |
| 18903 | pub event: xproto::Window, |
| 18904 | pub barrier: xfixes::Barrier, |
| 18905 | pub dtime: u32, |
| 18906 | pub flags: BarrierFlags, |
| 18907 | pub sourceid: DeviceId, |
| 18908 | pub root_x: Fp1616, |
| 18909 | pub root_y: Fp1616, |
| 18910 | pub dx: Fp3232, |
| 18911 | pub dy: Fp3232, |
| 18912 | } |
| 18913 | impl_debug_if_no_extra_traits!(BarrierHitEvent, "BarrierHitEvent" ); |
| 18914 | impl TryParse for BarrierHitEvent { |
| 18915 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 18916 | let remaining = initial_value; |
| 18917 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 18918 | let (extension, remaining) = u8::try_parse(remaining)?; |
| 18919 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 18920 | let (length, remaining) = u32::try_parse(remaining)?; |
| 18921 | let (event_type, remaining) = u16::try_parse(remaining)?; |
| 18922 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 18923 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 18924 | let (eventid, remaining) = u32::try_parse(remaining)?; |
| 18925 | let (root, remaining) = xproto::Window::try_parse(remaining)?; |
| 18926 | let (event, remaining) = xproto::Window::try_parse(remaining)?; |
| 18927 | let (barrier, remaining) = xfixes::Barrier::try_parse(remaining)?; |
| 18928 | let (dtime, remaining) = u32::try_parse(remaining)?; |
| 18929 | let (flags, remaining) = u32::try_parse(remaining)?; |
| 18930 | let (sourceid, remaining) = DeviceId::try_parse(remaining)?; |
| 18931 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 18932 | let (root_x, remaining) = Fp1616::try_parse(remaining)?; |
| 18933 | let (root_y, remaining) = Fp1616::try_parse(remaining)?; |
| 18934 | let (dx, remaining) = Fp3232::try_parse(remaining)?; |
| 18935 | let (dy, remaining) = Fp3232::try_parse(remaining)?; |
| 18936 | let flags = flags.into(); |
| 18937 | let result = BarrierHitEvent { response_type, extension, sequence, length, event_type, deviceid, time, eventid, root, event, barrier, dtime, flags, sourceid, root_x, root_y, dx, dy }; |
| 18938 | let _ = remaining; |
| 18939 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 18940 | .ok_or(ParseError::InsufficientData)?; |
| 18941 | Ok((result, remaining)) |
| 18942 | } |
| 18943 | } |
| 18944 | impl Serialize for BarrierHitEvent { |
| 18945 | type Bytes = [u8; 68]; |
| 18946 | fn serialize(&self) -> [u8; 68] { |
| 18947 | let response_type_bytes = self.response_type.serialize(); |
| 18948 | let extension_bytes = self.extension.serialize(); |
| 18949 | let sequence_bytes = self.sequence.serialize(); |
| 18950 | let length_bytes = self.length.serialize(); |
| 18951 | let event_type_bytes = self.event_type.serialize(); |
| 18952 | let deviceid_bytes = self.deviceid.serialize(); |
| 18953 | let time_bytes = self.time.serialize(); |
| 18954 | let eventid_bytes = self.eventid.serialize(); |
| 18955 | let root_bytes = self.root.serialize(); |
| 18956 | let event_bytes = self.event.serialize(); |
| 18957 | let barrier_bytes = self.barrier.serialize(); |
| 18958 | let dtime_bytes = self.dtime.serialize(); |
| 18959 | let flags_bytes = u32::from(self.flags).serialize(); |
| 18960 | let sourceid_bytes = self.sourceid.serialize(); |
| 18961 | let root_x_bytes = self.root_x.serialize(); |
| 18962 | let root_y_bytes = self.root_y.serialize(); |
| 18963 | let dx_bytes = self.dx.serialize(); |
| 18964 | let dy_bytes = self.dy.serialize(); |
| 18965 | [ |
| 18966 | response_type_bytes[0], |
| 18967 | extension_bytes[0], |
| 18968 | sequence_bytes[0], |
| 18969 | sequence_bytes[1], |
| 18970 | length_bytes[0], |
| 18971 | length_bytes[1], |
| 18972 | length_bytes[2], |
| 18973 | length_bytes[3], |
| 18974 | event_type_bytes[0], |
| 18975 | event_type_bytes[1], |
| 18976 | deviceid_bytes[0], |
| 18977 | deviceid_bytes[1], |
| 18978 | time_bytes[0], |
| 18979 | time_bytes[1], |
| 18980 | time_bytes[2], |
| 18981 | time_bytes[3], |
| 18982 | eventid_bytes[0], |
| 18983 | eventid_bytes[1], |
| 18984 | eventid_bytes[2], |
| 18985 | eventid_bytes[3], |
| 18986 | root_bytes[0], |
| 18987 | root_bytes[1], |
| 18988 | root_bytes[2], |
| 18989 | root_bytes[3], |
| 18990 | event_bytes[0], |
| 18991 | event_bytes[1], |
| 18992 | event_bytes[2], |
| 18993 | event_bytes[3], |
| 18994 | barrier_bytes[0], |
| 18995 | barrier_bytes[1], |
| 18996 | barrier_bytes[2], |
| 18997 | barrier_bytes[3], |
| 18998 | dtime_bytes[0], |
| 18999 | dtime_bytes[1], |
| 19000 | dtime_bytes[2], |
| 19001 | dtime_bytes[3], |
| 19002 | flags_bytes[0], |
| 19003 | flags_bytes[1], |
| 19004 | flags_bytes[2], |
| 19005 | flags_bytes[3], |
| 19006 | sourceid_bytes[0], |
| 19007 | sourceid_bytes[1], |
| 19008 | 0, |
| 19009 | 0, |
| 19010 | root_x_bytes[0], |
| 19011 | root_x_bytes[1], |
| 19012 | root_x_bytes[2], |
| 19013 | root_x_bytes[3], |
| 19014 | root_y_bytes[0], |
| 19015 | root_y_bytes[1], |
| 19016 | root_y_bytes[2], |
| 19017 | root_y_bytes[3], |
| 19018 | dx_bytes[0], |
| 19019 | dx_bytes[1], |
| 19020 | dx_bytes[2], |
| 19021 | dx_bytes[3], |
| 19022 | dx_bytes[4], |
| 19023 | dx_bytes[5], |
| 19024 | dx_bytes[6], |
| 19025 | dx_bytes[7], |
| 19026 | dy_bytes[0], |
| 19027 | dy_bytes[1], |
| 19028 | dy_bytes[2], |
| 19029 | dy_bytes[3], |
| 19030 | dy_bytes[4], |
| 19031 | dy_bytes[5], |
| 19032 | dy_bytes[6], |
| 19033 | dy_bytes[7], |
| 19034 | ] |
| 19035 | } |
| 19036 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 19037 | bytes.reserve(68); |
| 19038 | self.response_type.serialize_into(bytes); |
| 19039 | self.extension.serialize_into(bytes); |
| 19040 | self.sequence.serialize_into(bytes); |
| 19041 | self.length.serialize_into(bytes); |
| 19042 | self.event_type.serialize_into(bytes); |
| 19043 | self.deviceid.serialize_into(bytes); |
| 19044 | self.time.serialize_into(bytes); |
| 19045 | self.eventid.serialize_into(bytes); |
| 19046 | self.root.serialize_into(bytes); |
| 19047 | self.event.serialize_into(bytes); |
| 19048 | self.barrier.serialize_into(bytes); |
| 19049 | self.dtime.serialize_into(bytes); |
| 19050 | u32::from(self.flags).serialize_into(bytes); |
| 19051 | self.sourceid.serialize_into(bytes); |
| 19052 | bytes.extend_from_slice(&[0; 2]); |
| 19053 | self.root_x.serialize_into(bytes); |
| 19054 | self.root_y.serialize_into(bytes); |
| 19055 | self.dx.serialize_into(bytes); |
| 19056 | self.dy.serialize_into(bytes); |
| 19057 | } |
| 19058 | } |
| 19059 | |
| 19060 | /// Opcode for the BarrierLeave event |
| 19061 | pub const BARRIER_LEAVE_EVENT: u16 = 26; |
| 19062 | pub type BarrierLeaveEvent = BarrierHitEvent; |
| 19063 | |
| 19064 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 19065 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 19066 | pub struct GesturePinchEventFlags(u32); |
| 19067 | impl GesturePinchEventFlags { |
| 19068 | pub const GESTURE_PINCH_CANCELLED: Self = Self(1 << 0); |
| 19069 | } |
| 19070 | impl From<GesturePinchEventFlags> for u32 { |
| 19071 | #[inline ] |
| 19072 | fn from(input: GesturePinchEventFlags) -> Self { |
| 19073 | input.0 |
| 19074 | } |
| 19075 | } |
| 19076 | impl From<GesturePinchEventFlags> for Option<u32> { |
| 19077 | #[inline ] |
| 19078 | fn from(input: GesturePinchEventFlags) -> Self { |
| 19079 | Some(input.0) |
| 19080 | } |
| 19081 | } |
| 19082 | impl From<u8> for GesturePinchEventFlags { |
| 19083 | #[inline ] |
| 19084 | fn from(value: u8) -> Self { |
| 19085 | Self(value.into()) |
| 19086 | } |
| 19087 | } |
| 19088 | impl From<u16> for GesturePinchEventFlags { |
| 19089 | #[inline ] |
| 19090 | fn from(value: u16) -> Self { |
| 19091 | Self(value.into()) |
| 19092 | } |
| 19093 | } |
| 19094 | impl From<u32> for GesturePinchEventFlags { |
| 19095 | #[inline ] |
| 19096 | fn from(value: u32) -> Self { |
| 19097 | Self(value) |
| 19098 | } |
| 19099 | } |
| 19100 | impl core::fmt::Debug for GesturePinchEventFlags { |
| 19101 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 19102 | let variants: [(u32, &'static str, &'static …); 1] = [ |
| 19103 | (Self::GESTURE_PINCH_CANCELLED.0, "GESTURE_PINCH_CANCELLED" , "GesturePinchCancelled" ), |
| 19104 | ]; |
| 19105 | pretty_print_bitmask(fmt, self.0, &variants) |
| 19106 | } |
| 19107 | } |
| 19108 | bitmask_binop!(GesturePinchEventFlags, u32); |
| 19109 | |
| 19110 | /// Opcode for the GesturePinchBegin event |
| 19111 | pub const GESTURE_PINCH_BEGIN_EVENT: u16 = 27; |
| 19112 | #[derive (Clone, Copy, Default)] |
| 19113 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 19114 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 19115 | pub struct GesturePinchBeginEvent { |
| 19116 | pub response_type: u8, |
| 19117 | pub extension: u8, |
| 19118 | pub sequence: u16, |
| 19119 | pub length: u32, |
| 19120 | pub event_type: u16, |
| 19121 | pub deviceid: DeviceId, |
| 19122 | pub time: xproto::Timestamp, |
| 19123 | pub detail: u32, |
| 19124 | pub root: xproto::Window, |
| 19125 | pub event: xproto::Window, |
| 19126 | pub child: xproto::Window, |
| 19127 | pub root_x: Fp1616, |
| 19128 | pub root_y: Fp1616, |
| 19129 | pub event_x: Fp1616, |
| 19130 | pub event_y: Fp1616, |
| 19131 | pub delta_x: Fp1616, |
| 19132 | pub delta_y: Fp1616, |
| 19133 | pub delta_unaccel_x: Fp1616, |
| 19134 | pub delta_unaccel_y: Fp1616, |
| 19135 | pub scale: Fp1616, |
| 19136 | pub delta_angle: Fp1616, |
| 19137 | pub sourceid: DeviceId, |
| 19138 | pub mods: ModifierInfo, |
| 19139 | pub group: GroupInfo, |
| 19140 | pub flags: GesturePinchEventFlags, |
| 19141 | } |
| 19142 | impl_debug_if_no_extra_traits!(GesturePinchBeginEvent, "GesturePinchBeginEvent" ); |
| 19143 | impl TryParse for GesturePinchBeginEvent { |
| 19144 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 19145 | let remaining = initial_value; |
| 19146 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 19147 | let (extension, remaining) = u8::try_parse(remaining)?; |
| 19148 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 19149 | let (length, remaining) = u32::try_parse(remaining)?; |
| 19150 | let (event_type, remaining) = u16::try_parse(remaining)?; |
| 19151 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 19152 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 19153 | let (detail, remaining) = u32::try_parse(remaining)?; |
| 19154 | let (root, remaining) = xproto::Window::try_parse(remaining)?; |
| 19155 | let (event, remaining) = xproto::Window::try_parse(remaining)?; |
| 19156 | let (child, remaining) = xproto::Window::try_parse(remaining)?; |
| 19157 | let (root_x, remaining) = Fp1616::try_parse(remaining)?; |
| 19158 | let (root_y, remaining) = Fp1616::try_parse(remaining)?; |
| 19159 | let (event_x, remaining) = Fp1616::try_parse(remaining)?; |
| 19160 | let (event_y, remaining) = Fp1616::try_parse(remaining)?; |
| 19161 | let (delta_x, remaining) = Fp1616::try_parse(remaining)?; |
| 19162 | let (delta_y, remaining) = Fp1616::try_parse(remaining)?; |
| 19163 | let (delta_unaccel_x, remaining) = Fp1616::try_parse(remaining)?; |
| 19164 | let (delta_unaccel_y, remaining) = Fp1616::try_parse(remaining)?; |
| 19165 | let (scale, remaining) = Fp1616::try_parse(remaining)?; |
| 19166 | let (delta_angle, remaining) = Fp1616::try_parse(remaining)?; |
| 19167 | let (sourceid, remaining) = DeviceId::try_parse(remaining)?; |
| 19168 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 19169 | let (mods, remaining) = ModifierInfo::try_parse(remaining)?; |
| 19170 | let (group, remaining) = GroupInfo::try_parse(remaining)?; |
| 19171 | let (flags, remaining) = u32::try_parse(remaining)?; |
| 19172 | let flags = flags.into(); |
| 19173 | let result = GesturePinchBeginEvent { response_type, extension, sequence, length, event_type, deviceid, time, detail, root, event, child, root_x, root_y, event_x, event_y, delta_x, delta_y, delta_unaccel_x, delta_unaccel_y, scale, delta_angle, sourceid, mods, group, flags }; |
| 19174 | let _ = remaining; |
| 19175 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 19176 | .ok_or(ParseError::InsufficientData)?; |
| 19177 | Ok((result, remaining)) |
| 19178 | } |
| 19179 | } |
| 19180 | impl Serialize for GesturePinchBeginEvent { |
| 19181 | type Bytes = [u8; 100]; |
| 19182 | fn serialize(&self) -> [u8; 100] { |
| 19183 | let response_type_bytes = self.response_type.serialize(); |
| 19184 | let extension_bytes = self.extension.serialize(); |
| 19185 | let sequence_bytes = self.sequence.serialize(); |
| 19186 | let length_bytes = self.length.serialize(); |
| 19187 | let event_type_bytes = self.event_type.serialize(); |
| 19188 | let deviceid_bytes = self.deviceid.serialize(); |
| 19189 | let time_bytes = self.time.serialize(); |
| 19190 | let detail_bytes = self.detail.serialize(); |
| 19191 | let root_bytes = self.root.serialize(); |
| 19192 | let event_bytes = self.event.serialize(); |
| 19193 | let child_bytes = self.child.serialize(); |
| 19194 | let root_x_bytes = self.root_x.serialize(); |
| 19195 | let root_y_bytes = self.root_y.serialize(); |
| 19196 | let event_x_bytes = self.event_x.serialize(); |
| 19197 | let event_y_bytes = self.event_y.serialize(); |
| 19198 | let delta_x_bytes = self.delta_x.serialize(); |
| 19199 | let delta_y_bytes = self.delta_y.serialize(); |
| 19200 | let delta_unaccel_x_bytes = self.delta_unaccel_x.serialize(); |
| 19201 | let delta_unaccel_y_bytes = self.delta_unaccel_y.serialize(); |
| 19202 | let scale_bytes = self.scale.serialize(); |
| 19203 | let delta_angle_bytes = self.delta_angle.serialize(); |
| 19204 | let sourceid_bytes = self.sourceid.serialize(); |
| 19205 | let mods_bytes = self.mods.serialize(); |
| 19206 | let group_bytes = self.group.serialize(); |
| 19207 | let flags_bytes = u32::from(self.flags).serialize(); |
| 19208 | [ |
| 19209 | response_type_bytes[0], |
| 19210 | extension_bytes[0], |
| 19211 | sequence_bytes[0], |
| 19212 | sequence_bytes[1], |
| 19213 | length_bytes[0], |
| 19214 | length_bytes[1], |
| 19215 | length_bytes[2], |
| 19216 | length_bytes[3], |
| 19217 | event_type_bytes[0], |
| 19218 | event_type_bytes[1], |
| 19219 | deviceid_bytes[0], |
| 19220 | deviceid_bytes[1], |
| 19221 | time_bytes[0], |
| 19222 | time_bytes[1], |
| 19223 | time_bytes[2], |
| 19224 | time_bytes[3], |
| 19225 | detail_bytes[0], |
| 19226 | detail_bytes[1], |
| 19227 | detail_bytes[2], |
| 19228 | detail_bytes[3], |
| 19229 | root_bytes[0], |
| 19230 | root_bytes[1], |
| 19231 | root_bytes[2], |
| 19232 | root_bytes[3], |
| 19233 | event_bytes[0], |
| 19234 | event_bytes[1], |
| 19235 | event_bytes[2], |
| 19236 | event_bytes[3], |
| 19237 | child_bytes[0], |
| 19238 | child_bytes[1], |
| 19239 | child_bytes[2], |
| 19240 | child_bytes[3], |
| 19241 | root_x_bytes[0], |
| 19242 | root_x_bytes[1], |
| 19243 | root_x_bytes[2], |
| 19244 | root_x_bytes[3], |
| 19245 | root_y_bytes[0], |
| 19246 | root_y_bytes[1], |
| 19247 | root_y_bytes[2], |
| 19248 | root_y_bytes[3], |
| 19249 | event_x_bytes[0], |
| 19250 | event_x_bytes[1], |
| 19251 | event_x_bytes[2], |
| 19252 | event_x_bytes[3], |
| 19253 | event_y_bytes[0], |
| 19254 | event_y_bytes[1], |
| 19255 | event_y_bytes[2], |
| 19256 | event_y_bytes[3], |
| 19257 | delta_x_bytes[0], |
| 19258 | delta_x_bytes[1], |
| 19259 | delta_x_bytes[2], |
| 19260 | delta_x_bytes[3], |
| 19261 | delta_y_bytes[0], |
| 19262 | delta_y_bytes[1], |
| 19263 | delta_y_bytes[2], |
| 19264 | delta_y_bytes[3], |
| 19265 | delta_unaccel_x_bytes[0], |
| 19266 | delta_unaccel_x_bytes[1], |
| 19267 | delta_unaccel_x_bytes[2], |
| 19268 | delta_unaccel_x_bytes[3], |
| 19269 | delta_unaccel_y_bytes[0], |
| 19270 | delta_unaccel_y_bytes[1], |
| 19271 | delta_unaccel_y_bytes[2], |
| 19272 | delta_unaccel_y_bytes[3], |
| 19273 | scale_bytes[0], |
| 19274 | scale_bytes[1], |
| 19275 | scale_bytes[2], |
| 19276 | scale_bytes[3], |
| 19277 | delta_angle_bytes[0], |
| 19278 | delta_angle_bytes[1], |
| 19279 | delta_angle_bytes[2], |
| 19280 | delta_angle_bytes[3], |
| 19281 | sourceid_bytes[0], |
| 19282 | sourceid_bytes[1], |
| 19283 | 0, |
| 19284 | 0, |
| 19285 | mods_bytes[0], |
| 19286 | mods_bytes[1], |
| 19287 | mods_bytes[2], |
| 19288 | mods_bytes[3], |
| 19289 | mods_bytes[4], |
| 19290 | mods_bytes[5], |
| 19291 | mods_bytes[6], |
| 19292 | mods_bytes[7], |
| 19293 | mods_bytes[8], |
| 19294 | mods_bytes[9], |
| 19295 | mods_bytes[10], |
| 19296 | mods_bytes[11], |
| 19297 | mods_bytes[12], |
| 19298 | mods_bytes[13], |
| 19299 | mods_bytes[14], |
| 19300 | mods_bytes[15], |
| 19301 | group_bytes[0], |
| 19302 | group_bytes[1], |
| 19303 | group_bytes[2], |
| 19304 | group_bytes[3], |
| 19305 | flags_bytes[0], |
| 19306 | flags_bytes[1], |
| 19307 | flags_bytes[2], |
| 19308 | flags_bytes[3], |
| 19309 | ] |
| 19310 | } |
| 19311 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 19312 | bytes.reserve(100); |
| 19313 | self.response_type.serialize_into(bytes); |
| 19314 | self.extension.serialize_into(bytes); |
| 19315 | self.sequence.serialize_into(bytes); |
| 19316 | self.length.serialize_into(bytes); |
| 19317 | self.event_type.serialize_into(bytes); |
| 19318 | self.deviceid.serialize_into(bytes); |
| 19319 | self.time.serialize_into(bytes); |
| 19320 | self.detail.serialize_into(bytes); |
| 19321 | self.root.serialize_into(bytes); |
| 19322 | self.event.serialize_into(bytes); |
| 19323 | self.child.serialize_into(bytes); |
| 19324 | self.root_x.serialize_into(bytes); |
| 19325 | self.root_y.serialize_into(bytes); |
| 19326 | self.event_x.serialize_into(bytes); |
| 19327 | self.event_y.serialize_into(bytes); |
| 19328 | self.delta_x.serialize_into(bytes); |
| 19329 | self.delta_y.serialize_into(bytes); |
| 19330 | self.delta_unaccel_x.serialize_into(bytes); |
| 19331 | self.delta_unaccel_y.serialize_into(bytes); |
| 19332 | self.scale.serialize_into(bytes); |
| 19333 | self.delta_angle.serialize_into(bytes); |
| 19334 | self.sourceid.serialize_into(bytes); |
| 19335 | bytes.extend_from_slice(&[0; 2]); |
| 19336 | self.mods.serialize_into(bytes); |
| 19337 | self.group.serialize_into(bytes); |
| 19338 | u32::from(self.flags).serialize_into(bytes); |
| 19339 | } |
| 19340 | } |
| 19341 | |
| 19342 | /// Opcode for the GesturePinchUpdate event |
| 19343 | pub const GESTURE_PINCH_UPDATE_EVENT: u16 = 28; |
| 19344 | pub type GesturePinchUpdateEvent = GesturePinchBeginEvent; |
| 19345 | |
| 19346 | /// Opcode for the GesturePinchEnd event |
| 19347 | pub const GESTURE_PINCH_END_EVENT: u16 = 29; |
| 19348 | pub type GesturePinchEndEvent = GesturePinchBeginEvent; |
| 19349 | |
| 19350 | #[derive (Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 19351 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 19352 | pub struct GestureSwipeEventFlags(u32); |
| 19353 | impl GestureSwipeEventFlags { |
| 19354 | pub const GESTURE_SWIPE_CANCELLED: Self = Self(1 << 0); |
| 19355 | } |
| 19356 | impl From<GestureSwipeEventFlags> for u32 { |
| 19357 | #[inline ] |
| 19358 | fn from(input: GestureSwipeEventFlags) -> Self { |
| 19359 | input.0 |
| 19360 | } |
| 19361 | } |
| 19362 | impl From<GestureSwipeEventFlags> for Option<u32> { |
| 19363 | #[inline ] |
| 19364 | fn from(input: GestureSwipeEventFlags) -> Self { |
| 19365 | Some(input.0) |
| 19366 | } |
| 19367 | } |
| 19368 | impl From<u8> for GestureSwipeEventFlags { |
| 19369 | #[inline ] |
| 19370 | fn from(value: u8) -> Self { |
| 19371 | Self(value.into()) |
| 19372 | } |
| 19373 | } |
| 19374 | impl From<u16> for GestureSwipeEventFlags { |
| 19375 | #[inline ] |
| 19376 | fn from(value: u16) -> Self { |
| 19377 | Self(value.into()) |
| 19378 | } |
| 19379 | } |
| 19380 | impl From<u32> for GestureSwipeEventFlags { |
| 19381 | #[inline ] |
| 19382 | fn from(value: u32) -> Self { |
| 19383 | Self(value) |
| 19384 | } |
| 19385 | } |
| 19386 | impl core::fmt::Debug for GestureSwipeEventFlags { |
| 19387 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 19388 | let variants: [(u32, &'static str, &'static …); 1] = [ |
| 19389 | (Self::GESTURE_SWIPE_CANCELLED.0, "GESTURE_SWIPE_CANCELLED" , "GestureSwipeCancelled" ), |
| 19390 | ]; |
| 19391 | pretty_print_bitmask(fmt, self.0, &variants) |
| 19392 | } |
| 19393 | } |
| 19394 | bitmask_binop!(GestureSwipeEventFlags, u32); |
| 19395 | |
| 19396 | /// Opcode for the GestureSwipeBegin event |
| 19397 | pub const GESTURE_SWIPE_BEGIN_EVENT: u16 = 30; |
| 19398 | #[derive (Clone, Copy, Default)] |
| 19399 | #[cfg_attr (feature = "extra-traits" , derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash))] |
| 19400 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 19401 | pub struct GestureSwipeBeginEvent { |
| 19402 | pub response_type: u8, |
| 19403 | pub extension: u8, |
| 19404 | pub sequence: u16, |
| 19405 | pub length: u32, |
| 19406 | pub event_type: u16, |
| 19407 | pub deviceid: DeviceId, |
| 19408 | pub time: xproto::Timestamp, |
| 19409 | pub detail: u32, |
| 19410 | pub root: xproto::Window, |
| 19411 | pub event: xproto::Window, |
| 19412 | pub child: xproto::Window, |
| 19413 | pub root_x: Fp1616, |
| 19414 | pub root_y: Fp1616, |
| 19415 | pub event_x: Fp1616, |
| 19416 | pub event_y: Fp1616, |
| 19417 | pub delta_x: Fp1616, |
| 19418 | pub delta_y: Fp1616, |
| 19419 | pub delta_unaccel_x: Fp1616, |
| 19420 | pub delta_unaccel_y: Fp1616, |
| 19421 | pub sourceid: DeviceId, |
| 19422 | pub mods: ModifierInfo, |
| 19423 | pub group: GroupInfo, |
| 19424 | pub flags: GestureSwipeEventFlags, |
| 19425 | } |
| 19426 | impl_debug_if_no_extra_traits!(GestureSwipeBeginEvent, "GestureSwipeBeginEvent" ); |
| 19427 | impl TryParse for GestureSwipeBeginEvent { |
| 19428 | fn try_parse(initial_value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 19429 | let remaining = initial_value; |
| 19430 | let (response_type, remaining) = u8::try_parse(remaining)?; |
| 19431 | let (extension, remaining) = u8::try_parse(remaining)?; |
| 19432 | let (sequence, remaining) = u16::try_parse(remaining)?; |
| 19433 | let (length, remaining) = u32::try_parse(remaining)?; |
| 19434 | let (event_type, remaining) = u16::try_parse(remaining)?; |
| 19435 | let (deviceid, remaining) = DeviceId::try_parse(remaining)?; |
| 19436 | let (time, remaining) = xproto::Timestamp::try_parse(remaining)?; |
| 19437 | let (detail, remaining) = u32::try_parse(remaining)?; |
| 19438 | let (root, remaining) = xproto::Window::try_parse(remaining)?; |
| 19439 | let (event, remaining) = xproto::Window::try_parse(remaining)?; |
| 19440 | let (child, remaining) = xproto::Window::try_parse(remaining)?; |
| 19441 | let (root_x, remaining) = Fp1616::try_parse(remaining)?; |
| 19442 | let (root_y, remaining) = Fp1616::try_parse(remaining)?; |
| 19443 | let (event_x, remaining) = Fp1616::try_parse(remaining)?; |
| 19444 | let (event_y, remaining) = Fp1616::try_parse(remaining)?; |
| 19445 | let (delta_x, remaining) = Fp1616::try_parse(remaining)?; |
| 19446 | let (delta_y, remaining) = Fp1616::try_parse(remaining)?; |
| 19447 | let (delta_unaccel_x, remaining) = Fp1616::try_parse(remaining)?; |
| 19448 | let (delta_unaccel_y, remaining) = Fp1616::try_parse(remaining)?; |
| 19449 | let (sourceid, remaining) = DeviceId::try_parse(remaining)?; |
| 19450 | let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?; |
| 19451 | let (mods, remaining) = ModifierInfo::try_parse(remaining)?; |
| 19452 | let (group, remaining) = GroupInfo::try_parse(remaining)?; |
| 19453 | let (flags, remaining) = u32::try_parse(remaining)?; |
| 19454 | let flags = flags.into(); |
| 19455 | let result = GestureSwipeBeginEvent { response_type, extension, sequence, length, event_type, deviceid, time, detail, root, event, child, root_x, root_y, event_x, event_y, delta_x, delta_y, delta_unaccel_x, delta_unaccel_y, sourceid, mods, group, flags }; |
| 19456 | let _ = remaining; |
| 19457 | let remaining = initial_value.get(32 + length as usize * 4..) |
| 19458 | .ok_or(ParseError::InsufficientData)?; |
| 19459 | Ok((result, remaining)) |
| 19460 | } |
| 19461 | } |
| 19462 | impl Serialize for GestureSwipeBeginEvent { |
| 19463 | type Bytes = [u8; 92]; |
| 19464 | fn serialize(&self) -> [u8; 92] { |
| 19465 | let response_type_bytes = self.response_type.serialize(); |
| 19466 | let extension_bytes = self.extension.serialize(); |
| 19467 | let sequence_bytes = self.sequence.serialize(); |
| 19468 | let length_bytes = self.length.serialize(); |
| 19469 | let event_type_bytes = self.event_type.serialize(); |
| 19470 | let deviceid_bytes = self.deviceid.serialize(); |
| 19471 | let time_bytes = self.time.serialize(); |
| 19472 | let detail_bytes = self.detail.serialize(); |
| 19473 | let root_bytes = self.root.serialize(); |
| 19474 | let event_bytes = self.event.serialize(); |
| 19475 | let child_bytes = self.child.serialize(); |
| 19476 | let root_x_bytes = self.root_x.serialize(); |
| 19477 | let root_y_bytes = self.root_y.serialize(); |
| 19478 | let event_x_bytes = self.event_x.serialize(); |
| 19479 | let event_y_bytes = self.event_y.serialize(); |
| 19480 | let delta_x_bytes = self.delta_x.serialize(); |
| 19481 | let delta_y_bytes = self.delta_y.serialize(); |
| 19482 | let delta_unaccel_x_bytes = self.delta_unaccel_x.serialize(); |
| 19483 | let delta_unaccel_y_bytes = self.delta_unaccel_y.serialize(); |
| 19484 | let sourceid_bytes = self.sourceid.serialize(); |
| 19485 | let mods_bytes = self.mods.serialize(); |
| 19486 | let group_bytes = self.group.serialize(); |
| 19487 | let flags_bytes = u32::from(self.flags).serialize(); |
| 19488 | [ |
| 19489 | response_type_bytes[0], |
| 19490 | extension_bytes[0], |
| 19491 | sequence_bytes[0], |
| 19492 | sequence_bytes[1], |
| 19493 | length_bytes[0], |
| 19494 | length_bytes[1], |
| 19495 | length_bytes[2], |
| 19496 | length_bytes[3], |
| 19497 | event_type_bytes[0], |
| 19498 | event_type_bytes[1], |
| 19499 | deviceid_bytes[0], |
| 19500 | deviceid_bytes[1], |
| 19501 | time_bytes[0], |
| 19502 | time_bytes[1], |
| 19503 | time_bytes[2], |
| 19504 | time_bytes[3], |
| 19505 | detail_bytes[0], |
| 19506 | detail_bytes[1], |
| 19507 | detail_bytes[2], |
| 19508 | detail_bytes[3], |
| 19509 | root_bytes[0], |
| 19510 | root_bytes[1], |
| 19511 | root_bytes[2], |
| 19512 | root_bytes[3], |
| 19513 | event_bytes[0], |
| 19514 | event_bytes[1], |
| 19515 | event_bytes[2], |
| 19516 | event_bytes[3], |
| 19517 | child_bytes[0], |
| 19518 | child_bytes[1], |
| 19519 | child_bytes[2], |
| 19520 | child_bytes[3], |
| 19521 | root_x_bytes[0], |
| 19522 | root_x_bytes[1], |
| 19523 | root_x_bytes[2], |
| 19524 | root_x_bytes[3], |
| 19525 | root_y_bytes[0], |
| 19526 | root_y_bytes[1], |
| 19527 | root_y_bytes[2], |
| 19528 | root_y_bytes[3], |
| 19529 | event_x_bytes[0], |
| 19530 | event_x_bytes[1], |
| 19531 | event_x_bytes[2], |
| 19532 | event_x_bytes[3], |
| 19533 | event_y_bytes[0], |
| 19534 | event_y_bytes[1], |
| 19535 | event_y_bytes[2], |
| 19536 | event_y_bytes[3], |
| 19537 | delta_x_bytes[0], |
| 19538 | delta_x_bytes[1], |
| 19539 | delta_x_bytes[2], |
| 19540 | delta_x_bytes[3], |
| 19541 | delta_y_bytes[0], |
| 19542 | delta_y_bytes[1], |
| 19543 | delta_y_bytes[2], |
| 19544 | delta_y_bytes[3], |
| 19545 | delta_unaccel_x_bytes[0], |
| 19546 | delta_unaccel_x_bytes[1], |
| 19547 | delta_unaccel_x_bytes[2], |
| 19548 | delta_unaccel_x_bytes[3], |
| 19549 | delta_unaccel_y_bytes[0], |
| 19550 | delta_unaccel_y_bytes[1], |
| 19551 | delta_unaccel_y_bytes[2], |
| 19552 | delta_unaccel_y_bytes[3], |
| 19553 | sourceid_bytes[0], |
| 19554 | sourceid_bytes[1], |
| 19555 | 0, |
| 19556 | 0, |
| 19557 | mods_bytes[0], |
| 19558 | mods_bytes[1], |
| 19559 | mods_bytes[2], |
| 19560 | mods_bytes[3], |
| 19561 | mods_bytes[4], |
| 19562 | mods_bytes[5], |
| 19563 | mods_bytes[6], |
| 19564 | mods_bytes[7], |
| 19565 | mods_bytes[8], |
| 19566 | mods_bytes[9], |
| 19567 | mods_bytes[10], |
| 19568 | mods_bytes[11], |
| 19569 | mods_bytes[12], |
| 19570 | mods_bytes[13], |
| 19571 | mods_bytes[14], |
| 19572 | mods_bytes[15], |
| 19573 | group_bytes[0], |
| 19574 | group_bytes[1], |
| 19575 | group_bytes[2], |
| 19576 | group_bytes[3], |
| 19577 | flags_bytes[0], |
| 19578 | flags_bytes[1], |
| 19579 | flags_bytes[2], |
| 19580 | flags_bytes[3], |
| 19581 | ] |
| 19582 | } |
| 19583 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 19584 | bytes.reserve(92); |
| 19585 | self.response_type.serialize_into(bytes); |
| 19586 | self.extension.serialize_into(bytes); |
| 19587 | self.sequence.serialize_into(bytes); |
| 19588 | self.length.serialize_into(bytes); |
| 19589 | self.event_type.serialize_into(bytes); |
| 19590 | self.deviceid.serialize_into(bytes); |
| 19591 | self.time.serialize_into(bytes); |
| 19592 | self.detail.serialize_into(bytes); |
| 19593 | self.root.serialize_into(bytes); |
| 19594 | self.event.serialize_into(bytes); |
| 19595 | self.child.serialize_into(bytes); |
| 19596 | self.root_x.serialize_into(bytes); |
| 19597 | self.root_y.serialize_into(bytes); |
| 19598 | self.event_x.serialize_into(bytes); |
| 19599 | self.event_y.serialize_into(bytes); |
| 19600 | self.delta_x.serialize_into(bytes); |
| 19601 | self.delta_y.serialize_into(bytes); |
| 19602 | self.delta_unaccel_x.serialize_into(bytes); |
| 19603 | self.delta_unaccel_y.serialize_into(bytes); |
| 19604 | self.sourceid.serialize_into(bytes); |
| 19605 | bytes.extend_from_slice(&[0; 2]); |
| 19606 | self.mods.serialize_into(bytes); |
| 19607 | self.group.serialize_into(bytes); |
| 19608 | u32::from(self.flags).serialize_into(bytes); |
| 19609 | } |
| 19610 | } |
| 19611 | |
| 19612 | /// Opcode for the GestureSwipeUpdate event |
| 19613 | pub const GESTURE_SWIPE_UPDATE_EVENT: u16 = 31; |
| 19614 | pub type GestureSwipeUpdateEvent = GestureSwipeBeginEvent; |
| 19615 | |
| 19616 | /// Opcode for the GestureSwipeEnd event |
| 19617 | pub const GESTURE_SWIPE_END_EVENT: u16 = 32; |
| 19618 | pub type GestureSwipeEndEvent = GestureSwipeBeginEvent; |
| 19619 | |
| 19620 | #[derive (Debug, Copy, Clone)] |
| 19621 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 19622 | pub struct EventForSend([u8; 32]); |
| 19623 | impl EventForSend { |
| 19624 | pub fn as_device_valuator_event(&self) -> Result<DeviceValuatorEvent, ParseError> { |
| 19625 | let value: &[u8] = &self.0; |
| 19626 | DeviceValuatorEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19627 | } |
| 19628 | pub fn as_device_key_press_event(&self) -> Result<DeviceKeyPressEvent, ParseError> { |
| 19629 | let value: &[u8] = &self.0; |
| 19630 | DeviceKeyPressEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19631 | } |
| 19632 | pub fn as_device_key_release_event(&self) -> Result<DeviceKeyReleaseEvent, ParseError> { |
| 19633 | let value: &[u8] = &self.0; |
| 19634 | DeviceKeyReleaseEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19635 | } |
| 19636 | pub fn as_device_button_press_event(&self) -> Result<DeviceButtonPressEvent, ParseError> { |
| 19637 | let value: &[u8] = &self.0; |
| 19638 | DeviceButtonPressEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19639 | } |
| 19640 | pub fn as_device_button_release_event(&self) -> Result<DeviceButtonReleaseEvent, ParseError> { |
| 19641 | let value: &[u8] = &self.0; |
| 19642 | DeviceButtonReleaseEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19643 | } |
| 19644 | pub fn as_device_motion_notify_event(&self) -> Result<DeviceMotionNotifyEvent, ParseError> { |
| 19645 | let value: &[u8] = &self.0; |
| 19646 | DeviceMotionNotifyEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19647 | } |
| 19648 | pub fn as_device_focus_in_event(&self) -> Result<DeviceFocusInEvent, ParseError> { |
| 19649 | let value: &[u8] = &self.0; |
| 19650 | DeviceFocusInEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19651 | } |
| 19652 | pub fn as_device_focus_out_event(&self) -> Result<DeviceFocusOutEvent, ParseError> { |
| 19653 | let value: &[u8] = &self.0; |
| 19654 | DeviceFocusOutEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19655 | } |
| 19656 | pub fn as_proximity_in_event(&self) -> Result<ProximityInEvent, ParseError> { |
| 19657 | let value: &[u8] = &self.0; |
| 19658 | ProximityInEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19659 | } |
| 19660 | pub fn as_proximity_out_event(&self) -> Result<ProximityOutEvent, ParseError> { |
| 19661 | let value: &[u8] = &self.0; |
| 19662 | ProximityOutEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19663 | } |
| 19664 | pub fn as_device_state_notify_event(&self) -> Result<DeviceStateNotifyEvent, ParseError> { |
| 19665 | let value: &[u8] = &self.0; |
| 19666 | DeviceStateNotifyEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19667 | } |
| 19668 | pub fn as_device_mapping_notify_event(&self) -> Result<DeviceMappingNotifyEvent, ParseError> { |
| 19669 | let value: &[u8] = &self.0; |
| 19670 | DeviceMappingNotifyEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19671 | } |
| 19672 | pub fn as_change_device_notify_event(&self) -> Result<ChangeDeviceNotifyEvent, ParseError> { |
| 19673 | let value: &[u8] = &self.0; |
| 19674 | ChangeDeviceNotifyEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19675 | } |
| 19676 | pub fn as_device_key_state_notify_event(&self) -> Result<DeviceKeyStateNotifyEvent, ParseError> { |
| 19677 | let value: &[u8] = &self.0; |
| 19678 | DeviceKeyStateNotifyEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19679 | } |
| 19680 | pub fn as_device_button_state_notify_event(&self) -> Result<DeviceButtonStateNotifyEvent, ParseError> { |
| 19681 | let value: &[u8] = &self.0; |
| 19682 | DeviceButtonStateNotifyEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19683 | } |
| 19684 | pub fn as_device_presence_notify_event(&self) -> Result<DevicePresenceNotifyEvent, ParseError> { |
| 19685 | let value: &[u8] = &self.0; |
| 19686 | DevicePresenceNotifyEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19687 | } |
| 19688 | pub fn as_device_property_notify_event(&self) -> Result<DevicePropertyNotifyEvent, ParseError> { |
| 19689 | let value: &[u8] = &self.0; |
| 19690 | DevicePropertyNotifyEvent::try_parse(value).map(|(result, _remaining)| result) |
| 19691 | } |
| 19692 | } |
| 19693 | impl Serialize for EventForSend { |
| 19694 | type Bytes = [u8; 32]; |
| 19695 | fn serialize(&self) -> [u8; 32] { |
| 19696 | self.0 |
| 19697 | } |
| 19698 | fn serialize_into(&self, bytes: &mut Vec<u8>) { |
| 19699 | bytes.extend_from_slice(&self.0); |
| 19700 | } |
| 19701 | } |
| 19702 | impl TryParse for EventForSend { |
| 19703 | fn try_parse(value: &[u8]) -> Result<(Self, &[u8]), ParseError> { |
| 19704 | let inner: [u8; 32] = valueResult<[u8; 32], TryFromSliceError>.get(..32) |
| 19705 | .ok_or(err:ParseError::InsufficientData)? |
| 19706 | .try_into() |
| 19707 | .unwrap(); |
| 19708 | let result: EventForSend = EventForSend(inner); |
| 19709 | Ok((result, &value[32..])) |
| 19710 | } |
| 19711 | } |
| 19712 | impl From<DeviceValuatorEvent> for EventForSend { |
| 19713 | fn from(value: DeviceValuatorEvent) -> Self { |
| 19714 | Self(<[u8; 32]>::from(value)) |
| 19715 | } |
| 19716 | } |
| 19717 | impl From<&DeviceValuatorEvent> for EventForSend { |
| 19718 | fn from(value: &DeviceValuatorEvent) -> Self { |
| 19719 | Self(<[u8; 32]>::from(value)) |
| 19720 | } |
| 19721 | } |
| 19722 | impl From<DeviceKeyPressEvent> for EventForSend { |
| 19723 | fn from(value: DeviceKeyPressEvent) -> Self { |
| 19724 | Self(<[u8; 32]>::from(value)) |
| 19725 | } |
| 19726 | } |
| 19727 | impl From<&DeviceKeyPressEvent> for EventForSend { |
| 19728 | fn from(value: &DeviceKeyPressEvent) -> Self { |
| 19729 | Self(<[u8; 32]>::from(value)) |
| 19730 | } |
| 19731 | } |
| 19732 | impl From<DeviceFocusInEvent> for EventForSend { |
| 19733 | fn from(value: DeviceFocusInEvent) -> Self { |
| 19734 | Self(<[u8; 32]>::from(value)) |
| 19735 | } |
| 19736 | } |
| 19737 | impl From<&DeviceFocusInEvent> for EventForSend { |
| 19738 | fn from(value: &DeviceFocusInEvent) -> Self { |
| 19739 | Self(<[u8; 32]>::from(value)) |
| 19740 | } |
| 19741 | } |
| 19742 | impl From<DeviceStateNotifyEvent> for EventForSend { |
| 19743 | fn from(value: DeviceStateNotifyEvent) -> Self { |
| 19744 | Self(<[u8; 32]>::from(value)) |
| 19745 | } |
| 19746 | } |
| 19747 | impl From<&DeviceStateNotifyEvent> for EventForSend { |
| 19748 | fn from(value: &DeviceStateNotifyEvent) -> Self { |
| 19749 | Self(<[u8; 32]>::from(value)) |
| 19750 | } |
| 19751 | } |
| 19752 | impl From<DeviceMappingNotifyEvent> for EventForSend { |
| 19753 | fn from(value: DeviceMappingNotifyEvent) -> Self { |
| 19754 | Self(<[u8; 32]>::from(value)) |
| 19755 | } |
| 19756 | } |
| 19757 | impl From<&DeviceMappingNotifyEvent> for EventForSend { |
| 19758 | fn from(value: &DeviceMappingNotifyEvent) -> Self { |
| 19759 | Self(<[u8; 32]>::from(value)) |
| 19760 | } |
| 19761 | } |
| 19762 | impl From<ChangeDeviceNotifyEvent> for EventForSend { |
| 19763 | fn from(value: ChangeDeviceNotifyEvent) -> Self { |
| 19764 | Self(<[u8; 32]>::from(value)) |
| 19765 | } |
| 19766 | } |
| 19767 | impl From<&ChangeDeviceNotifyEvent> for EventForSend { |
| 19768 | fn from(value: &ChangeDeviceNotifyEvent) -> Self { |
| 19769 | Self(<[u8; 32]>::from(value)) |
| 19770 | } |
| 19771 | } |
| 19772 | impl From<DeviceKeyStateNotifyEvent> for EventForSend { |
| 19773 | fn from(value: DeviceKeyStateNotifyEvent) -> Self { |
| 19774 | Self(<[u8; 32]>::from(value)) |
| 19775 | } |
| 19776 | } |
| 19777 | impl From<&DeviceKeyStateNotifyEvent> for EventForSend { |
| 19778 | fn from(value: &DeviceKeyStateNotifyEvent) -> Self { |
| 19779 | Self(<[u8; 32]>::from(value)) |
| 19780 | } |
| 19781 | } |
| 19782 | impl From<DeviceButtonStateNotifyEvent> for EventForSend { |
| 19783 | fn from(value: DeviceButtonStateNotifyEvent) -> Self { |
| 19784 | Self(<[u8; 32]>::from(value)) |
| 19785 | } |
| 19786 | } |
| 19787 | impl From<&DeviceButtonStateNotifyEvent> for EventForSend { |
| 19788 | fn from(value: &DeviceButtonStateNotifyEvent) -> Self { |
| 19789 | Self(<[u8; 32]>::from(value)) |
| 19790 | } |
| 19791 | } |
| 19792 | impl From<DevicePresenceNotifyEvent> for EventForSend { |
| 19793 | fn from(value: DevicePresenceNotifyEvent) -> Self { |
| 19794 | Self(<[u8; 32]>::from(value)) |
| 19795 | } |
| 19796 | } |
| 19797 | impl From<&DevicePresenceNotifyEvent> for EventForSend { |
| 19798 | fn from(value: &DevicePresenceNotifyEvent) -> Self { |
| 19799 | Self(<[u8; 32]>::from(value)) |
| 19800 | } |
| 19801 | } |
| 19802 | impl From<DevicePropertyNotifyEvent> for EventForSend { |
| 19803 | fn from(value: DevicePropertyNotifyEvent) -> Self { |
| 19804 | Self(<[u8; 32]>::from(value)) |
| 19805 | } |
| 19806 | } |
| 19807 | impl From<&DevicePropertyNotifyEvent> for EventForSend { |
| 19808 | fn from(value: &DevicePropertyNotifyEvent) -> Self { |
| 19809 | Self(<[u8; 32]>::from(value)) |
| 19810 | } |
| 19811 | } |
| 19812 | |
| 19813 | /// Opcode for the SendExtensionEvent request |
| 19814 | pub const SEND_EXTENSION_EVENT_REQUEST: u8 = 31; |
| 19815 | #[derive (Clone)] |
| 19816 | #[cfg_attr (feature = "extra-traits" , derive(Debug))] |
| 19817 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 19818 | pub struct SendExtensionEventRequest<'input> { |
| 19819 | pub destination: xproto::Window, |
| 19820 | pub device_id: u8, |
| 19821 | pub propagate: bool, |
| 19822 | pub events: Cow<'input, [EventForSend]>, |
| 19823 | pub classes: Cow<'input, [EventClass]>, |
| 19824 | } |
| 19825 | impl_debug_if_no_extra_traits!(SendExtensionEventRequest<'_>, "SendExtensionEventRequest" ); |
| 19826 | impl<'input> SendExtensionEventRequest<'input> { |
| 19827 | /// Serialize this request into bytes for the provided connection |
| 19828 | pub fn serialize(self, major_opcode: u8) -> BufWithFds<[Cow<'input, [u8]>; 4]> { |
| 19829 | let length_so_far = 0; |
| 19830 | let destination_bytes = self.destination.serialize(); |
| 19831 | let device_id_bytes = self.device_id.serialize(); |
| 19832 | let propagate_bytes = self.propagate.serialize(); |
| 19833 | let num_classes = u16::try_from(self.classes.len()).expect("`classes` has too many elements" ); |
| 19834 | let num_classes_bytes = num_classes.serialize(); |
| 19835 | let num_events = u8::try_from(self.events.len()).expect("`events` has too many elements" ); |
| 19836 | let num_events_bytes = num_events.serialize(); |
| 19837 | let mut request0 = vec![ |
| 19838 | major_opcode, |
| 19839 | SEND_EXTENSION_EVENT_REQUEST, |
| 19840 | 0, |
| 19841 | 0, |
| 19842 | destination_bytes[0], |
| 19843 | destination_bytes[1], |
| 19844 | destination_bytes[2], |
| 19845 | destination_bytes[3], |
| 19846 | device_id_bytes[0], |
| 19847 | propagate_bytes[0], |
| 19848 | num_classes_bytes[0], |
| 19849 | num_classes_bytes[1], |
| 19850 | num_events_bytes[0], |
| 19851 | 0, |
| 19852 | 0, |
| 19853 | 0, |
| 19854 | ]; |
| 19855 | let length_so_far = length_so_far + request0.len(); |
| 19856 | let events_bytes = self.events.serialize(); |
| 19857 | let length_so_far = length_so_far + events_bytes.len(); |
| 19858 | let classes_bytes = self.classes.serialize(); |
| 19859 | let length_so_far = length_so_far + classes_bytes.len(); |
| 19860 | let padding0 = &[0; 3][..(4 - (length_so_far % 4)) % 4]; |
| 19861 | let length_so_far = length_so_far + padding0.len(); |
| 19862 | assert_eq!(length_so_far % 4, 0); |
| 19863 | let length = u16::try_from(length_so_far / 4).unwrap_or(0); |
| 19864 | request0[2..4].copy_from_slice(&length.to_ne_bytes()); |
| 19865 | ([request0.into(), events_bytes.into(), classes_bytes.into(), padding0.into()], vec![]) |
| 19866 | } |
| 19867 | /// Parse this request given its header, its body, and any fds that go along with it |
| 19868 | #[cfg (feature = "request-parsing" )] |
| 19869 | pub fn try_parse_request(header: RequestHeader, value: &'input [u8]) -> Result<Self, ParseError> { |
| 19870 | if header.minor_opcode != SEND_EXTENSION_EVENT_REQUEST { |
| 19871 | return Err(ParseError::InvalidValue); |
| 19872 | } |
| 19873 | let (destination, remaining) = xproto::Window::try_parse(value)?; |
| 19874 | let (device_id, remaining) = u8::try_parse(remaining)?; |
| 19875 | let (propagate, remaining) = bool::try_parse(remaining)?; |
| 19876 | let (num_classes, remaining) = u16::try_parse(remaining)?; |
| 19877 | let (num_events, remaining) = u8::try_parse(remaining)?; |
| 19878 | let remaining = remaining.get(3..).ok_or(ParseError::InsufficientData)?; |
| 19879 | let (events, remaining) = crate::x11_utils::parse_list::<EventForSend>(remaining, num_events.try_to_usize()?)?; |
| 19880 | let (classes, remaining) = crate::x11_utils::parse_list::<EventClass>(remaining, num_classes.try_to_usize()?)?; |
| 19881 | let _ = remaining; |
| 19882 | Ok(SendExtensionEventRequest { |
| 19883 | destination, |
| 19884 | device_id, |
| 19885 | propagate, |
| 19886 | events: Cow::Owned(events), |
| 19887 | classes: Cow::Owned(classes), |
| 19888 | }) |
| 19889 | } |
| 19890 | /// Clone all borrowed data in this SendExtensionEventRequest. |
| 19891 | pub fn into_owned(self) -> SendExtensionEventRequest<'static> { |
| 19892 | SendExtensionEventRequest { |
| 19893 | destination: self.destination, |
| 19894 | device_id: self.device_id, |
| 19895 | propagate: self.propagate, |
| 19896 | events: Cow::Owned(self.events.into_owned()), |
| 19897 | classes: Cow::Owned(self.classes.into_owned()), |
| 19898 | } |
| 19899 | } |
| 19900 | } |
| 19901 | impl<'input> Request for SendExtensionEventRequest<'input> { |
| 19902 | const EXTENSION_NAME: Option<&'static str> = Some(X11_EXTENSION_NAME); |
| 19903 | |
| 19904 | fn serialize(self, major_opcode: u8) -> BufWithFds<Vec<u8>> { |
| 19905 | let (bufs: [Cow<'_, [u8]>; 4], fds: Vec) = self.serialize(major_opcode); |
| 19906 | // Flatten the buffers into a single vector |
| 19907 | let buf: Vec = bufs.iter().flat_map(|buf: &Cow<'_, [u8]>| buf.iter().copied()).collect(); |
| 19908 | (buf, fds) |
| 19909 | } |
| 19910 | } |
| 19911 | impl<'input> crate::x11_utils::VoidRequest for SendExtensionEventRequest<'input> { |
| 19912 | } |
| 19913 | |
| 19914 | /// Opcode for the Device error |
| 19915 | pub const DEVICE_ERROR: u8 = 0; |
| 19916 | |
| 19917 | /// Opcode for the Event error |
| 19918 | pub const EVENT_ERROR: u8 = 1; |
| 19919 | |
| 19920 | /// Opcode for the Mode error |
| 19921 | pub const MODE_ERROR: u8 = 2; |
| 19922 | |
| 19923 | /// Opcode for the DeviceBusy error |
| 19924 | pub const DEVICE_BUSY_ERROR: u8 = 3; |
| 19925 | |
| 19926 | /// Opcode for the Class error |
| 19927 | pub const CLASS_ERROR: u8 = 4; |
| 19928 | |
| 19929 | |