| 1 | use crate::{ |
| 2 | error::AtspiError, |
| 3 | events::{BusProperties, EventBodyOwned, HasMatchRule, HasRegistryEventString, ObjectRef}, |
| 4 | Event, EventProperties, EventTypeProperties, |
| 5 | }; |
| 6 | use zbus_names::UniqueName; |
| 7 | use zvariant::{ObjectPath, OwnedValue}; |
| 8 | |
| 9 | #[derive (Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq, Hash)] |
| 10 | pub enum KeyboardEvents { |
| 11 | /// See: [`ModifiersEvent`]. |
| 12 | Modifiers(ModifiersEvent), |
| 13 | } |
| 14 | |
| 15 | impl EventTypeProperties for KeyboardEvents { |
| 16 | fn member(&self) -> &'static str { |
| 17 | match self { |
| 18 | Self::Modifiers(inner: &ModifiersEvent) => inner.member(), |
| 19 | } |
| 20 | } |
| 21 | fn match_rule(&self) -> &'static str { |
| 22 | match self { |
| 23 | Self::Modifiers(inner: &ModifiersEvent) => inner.match_rule(), |
| 24 | } |
| 25 | } |
| 26 | fn interface(&self) -> &'static str { |
| 27 | match self { |
| 28 | Self::Modifiers(inner: &ModifiersEvent) => inner.interface(), |
| 29 | } |
| 30 | } |
| 31 | fn registry_string(&self) -> &'static str { |
| 32 | match self { |
| 33 | Self::Modifiers(inner: &ModifiersEvent) => inner.registry_string(), |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | impl EventProperties for KeyboardEvents { |
| 39 | fn path(&self) -> ObjectPath<'_> { |
| 40 | match self { |
| 41 | Self::Modifiers(inner: &ModifiersEvent) => inner.path(), |
| 42 | } |
| 43 | } |
| 44 | fn sender(&self) -> UniqueName<'_> { |
| 45 | match self { |
| 46 | Self::Modifiers(inner: &ModifiersEvent) => inner.sender(), |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl_from_interface_event_enum_for_event!(KeyboardEvents, Event::Keyboard); |
| 52 | impl_try_from_event_for_user_facing_event_type!(KeyboardEvents, Event::Keyboard); |
| 53 | |
| 54 | event_wrapper_test_cases!(KeyboardEvents, ModifiersEvent); |
| 55 | |
| 56 | impl HasMatchRule for KeyboardEvents { |
| 57 | const MATCH_RULE_STRING: &'static str = |
| 58 | "type='signal',interface='org.a11y.atspi.Event.Keyboard'" ; |
| 59 | } |
| 60 | |
| 61 | #[derive (Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)] |
| 62 | pub struct ModifiersEvent { |
| 63 | /// The [`ObjectRef`] which the event applies to. |
| 64 | pub item: crate::events::ObjectRef, |
| 65 | pub previous_modifiers: i32, |
| 66 | pub current_modifiers: i32, |
| 67 | } |
| 68 | |
| 69 | impl BusProperties for ModifiersEvent { |
| 70 | const DBUS_MEMBER: &'static str = "Modifiers" ; |
| 71 | const DBUS_INTERFACE: &'static str = "org.a11y.atspi.Event.Keyboard" ; |
| 72 | const MATCH_RULE_STRING: &'static str = |
| 73 | "type='signal',interface='org.a11y.atspi.Event.Keyboard',member='Modifiers'" ; |
| 74 | const REGISTRY_EVENT_STRING: &'static str = "Keyboard:" ; |
| 75 | |
| 76 | type Body = EventBodyOwned; |
| 77 | |
| 78 | fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result<Self, AtspiError> { |
| 79 | Ok(Self { item, previous_modifiers: body.detail1, current_modifiers: body.detail2 }) |
| 80 | } |
| 81 | fn body(&self) -> Self::Body { |
| 82 | let copy: ModifiersEvent = self.clone(); |
| 83 | copy.into() |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | #[cfg (feature = "zbus" )] |
| 88 | impl TryFrom<&zbus::Message> for KeyboardEvents { |
| 89 | type Error = AtspiError; |
| 90 | fn try_from(ev: &zbus::Message) -> Result<Self, Self::Error> { |
| 91 | let header: Header<'_> = ev.header(); |
| 92 | let member: &MemberName<'_> = header |
| 93 | .member() |
| 94 | .ok_or(err:AtspiError::MemberMatch("Event without member" .into()))?; |
| 95 | match member.as_str() { |
| 96 | "Modifiers" => Ok(KeyboardEvents::Modifiers(ev.try_into()?)), |
| 97 | _ => Err(AtspiError::MemberMatch("No matching member for Keyboard" .into())), |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl_from_user_facing_event_for_interface_event_enum!( |
| 103 | ModifiersEvent, |
| 104 | KeyboardEvents, |
| 105 | KeyboardEvents::Modifiers |
| 106 | ); |
| 107 | impl_from_user_facing_type_for_event_enum!(ModifiersEvent, Event::Keyboard); |
| 108 | impl_try_from_event_for_user_facing_type!( |
| 109 | ModifiersEvent, |
| 110 | KeyboardEvents::Modifiers, |
| 111 | Event::Keyboard |
| 112 | ); |
| 113 | |
| 114 | event_test_cases!(ModifiersEvent); |
| 115 | impl_to_dbus_message!(ModifiersEvent); |
| 116 | impl_from_dbus_message!(ModifiersEvent); |
| 117 | impl_event_properties!(ModifiersEvent); |
| 118 | impl From<ModifiersEvent> for EventBodyOwned { |
| 119 | fn from(event: ModifiersEvent) -> Self { |
| 120 | EventBodyOwned { |
| 121 | properties: std::collections::HashMap::new(), |
| 122 | kind: String::default(), |
| 123 | detail1: event.previous_modifiers, |
| 124 | detail2: event.current_modifiers, |
| 125 | any_data: OwnedValue::from(0u8), |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | impl HasRegistryEventString for KeyboardEvents { |
| 131 | const REGISTRY_EVENT_STRING: &'static str = "Keyboard:" ; |
| 132 | } |
| 133 | |