atspi_common/events/
keyboard.rs

1use crate::{
2	error::AtspiError,
3	events::{BusProperties, EventBodyOwned, HasMatchRule, HasRegistryEventString, ObjectRef},
4	Event, EventProperties, EventTypeProperties,
5};
6use zbus_names::UniqueName;
7use zvariant::{ObjectPath, OwnedValue};
8
9#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq, Hash)]
10pub enum KeyboardEvents {
11	/// See: [`ModifiersEvent`].
12	Modifiers(ModifiersEvent),
13}
14
15impl EventTypeProperties for KeyboardEvents {
16	fn member(&self) -> &'static str {
17		match self {
18			Self::Modifiers(inner) => inner.member(),
19		}
20	}
21	fn match_rule(&self) -> &'static str {
22		match self {
23			Self::Modifiers(inner) => inner.match_rule(),
24		}
25	}
26	fn interface(&self) -> &'static str {
27		match self {
28			Self::Modifiers(inner) => inner.interface(),
29		}
30	}
31	fn registry_string(&self) -> &'static str {
32		match self {
33			Self::Modifiers(inner) => inner.registry_string(),
34		}
35	}
36}
37
38impl EventProperties for KeyboardEvents {
39	fn path(&self) -> ObjectPath<'_> {
40		match self {
41			Self::Modifiers(inner) => inner.path(),
42		}
43	}
44	fn sender(&self) -> UniqueName<'_> {
45		match self {
46			Self::Modifiers(inner) => inner.sender(),
47		}
48	}
49}
50
51impl_from_interface_event_enum_for_event!(KeyboardEvents, Event::Keyboard);
52impl_try_from_event_for_user_facing_event_type!(KeyboardEvents, Event::Keyboard);
53
54event_wrapper_test_cases!(KeyboardEvents, ModifiersEvent);
55
56impl 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)]
62pub 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
69impl 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 = self.clone();
83		copy.into()
84	}
85}
86
87#[cfg(feature = "zbus")]
88impl TryFrom<&zbus::Message> for KeyboardEvents {
89	type Error = AtspiError;
90	fn try_from(ev: &zbus::Message) -> Result<Self, Self::Error> {
91		let header = ev.header();
92		let member = header
93			.member()
94			.ok_or(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
102impl_from_user_facing_event_for_interface_event_enum!(
103	ModifiersEvent,
104	KeyboardEvents,
105	KeyboardEvents::Modifiers
106);
107impl_from_user_facing_type_for_event_enum!(ModifiersEvent, Event::Keyboard);
108impl_try_from_event_for_user_facing_type!(
109	ModifiersEvent,
110	KeyboardEvents::Modifiers,
111	Event::Keyboard
112);
113
114event_test_cases!(ModifiersEvent);
115impl_to_dbus_message!(ModifiersEvent);
116impl_from_dbus_message!(ModifiersEvent);
117impl_event_properties!(ModifiersEvent);
118impl 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
130impl HasRegistryEventString for KeyboardEvents {
131	const REGISTRY_EVENT_STRING: &'static str = "Keyboard:";
132}