atspi_common/events/
mouse.rs

1#[cfg(feature = "zbus")]
2use crate::events::MessageConversion;
3use crate::events::{
4	DBusInterface, DBusMatchRule, DBusMember, EventBody, EventBodyOwned, RegistryEventString,
5};
6use crate::object_ref::ObjectRefOwned;
7#[cfg(feature = "zbus")]
8use crate::EventProperties;
9#[cfg(feature = "zbus")]
10use crate::{error::AtspiError, ObjectRef};
11#[cfg(feature = "zbus")]
12use zbus::message::{Body as DbusBody, Header};
13
14#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)]
15pub struct AbsEvent {
16	/// The [`crate::ObjectRef`] which the event applies to.
17	pub item: ObjectRefOwned,
18	pub x: i32,
19	pub y: i32,
20}
21
22impl_event_type_properties_for_event!(AbsEvent);
23
24#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)]
25pub struct RelEvent {
26	/// The [`crate::ObjectRef`] which the event applies to.
27	pub item: ObjectRefOwned,
28	pub x: i32,
29	pub y: i32,
30}
31
32impl_event_type_properties_for_event!(RelEvent);
33
34#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)]
35pub struct ButtonEvent {
36	/// The [`crate::ObjectRef`] which the event applies to.
37	pub item: ObjectRefOwned,
38	pub detail: String,
39	pub mouse_x: i32,
40	pub mouse_y: i32,
41}
42
43impl_event_type_properties_for_event!(ButtonEvent);
44
45impl_member_interface_registry_string_and_match_rule_for_event! {
46	AbsEvent,
47	"Abs",
48	"org.a11y.atspi.Event.Mouse",
49	"mouse:abs",
50	"type='signal',interface='org.a11y.atspi.Event.Mouse',member='Abs'"
51}
52
53impl_member_interface_registry_string_and_match_rule_for_event! {
54	RelEvent,
55	"Rel",
56	"org.a11y.atspi.Event.Mouse",
57	"mouse:rel",
58	"type='signal',interface='org.a11y.atspi.Event.Mouse',member='Rel'"
59}
60
61impl_member_interface_registry_string_and_match_rule_for_event! {
62	ButtonEvent,
63	"Button",
64	"org.a11y.atspi.Event.Mouse",
65	"mouse:button",
66	"type='signal',interface='org.a11y.atspi.Event.Mouse',member='Button'"
67}
68
69#[cfg(feature = "zbus")]
70impl MessageConversion<'_> for AbsEvent {
71	type Body<'a> = EventBody<'a>;
72
73	fn from_message_unchecked_parts(item: ObjectRef, body: DbusBody) -> Result<Self, AtspiError> {
74		let body = body.deserialize_unchecked::<Self::Body<'_>>()?;
75		Ok(Self { item: item.into(), x: body.detail1(), y: body.detail2() })
76	}
77	fn from_message_unchecked(msg: &zbus::Message, header: &Header) -> Result<Self, AtspiError> {
78		let item = header.try_into()?;
79		let body = msg.body();
80		Self::from_message_unchecked_parts(item, body)
81	}
82	fn body(&self) -> Self::Body<'_> {
83		EventBodyOwned { detail1: self.x, detail2: self.y, ..Default::default() }.into()
84	}
85}
86
87#[cfg(feature = "zbus")]
88impl MessageConversion<'_> for RelEvent {
89	type Body<'a> = EventBody<'a>;
90
91	fn from_message_unchecked_parts(item: ObjectRef, body: DbusBody) -> Result<Self, AtspiError> {
92		let body = body.deserialize_unchecked::<Self::Body<'_>>()?;
93		Ok(Self { item: item.into(), x: body.detail1(), y: body.detail2() })
94	}
95
96	fn from_message_unchecked(msg: &zbus::Message, header: &Header) -> Result<Self, AtspiError> {
97		let item = header.try_into()?;
98		let body = msg.body();
99		Self::from_message_unchecked_parts(item, body)
100	}
101
102	fn body(&self) -> Self::Body<'_> {
103		EventBodyOwned { detail1: self.x, detail2: self.y, ..Default::default() }.into()
104	}
105}
106
107#[cfg(feature = "zbus")]
108impl MessageConversion<'_> for ButtonEvent {
109	type Body<'a> = EventBody<'a>;
110
111	fn from_message_unchecked_parts(item: ObjectRef, body: DbusBody) -> Result<Self, AtspiError> {
112		let mut body = body.deserialize_unchecked::<Self::Body<'_>>()?;
113		Ok(Self {
114			item: item.into(),
115			detail: body.take_kind(),
116			mouse_x: body.detail1(),
117			mouse_y: body.detail2(),
118		})
119	}
120
121	fn from_message_unchecked(msg: &zbus::Message, header: &Header) -> Result<Self, AtspiError> {
122		let item = header.try_into()?;
123		let body = msg.body();
124		Self::from_message_unchecked_parts(item, body)
125	}
126
127	fn body(&self) -> Self::Body<'_> {
128		EventBodyOwned::from(self).into()
129	}
130}
131
132event_test_cases!(AbsEvent);
133impl_to_dbus_message!(AbsEvent);
134impl_from_dbus_message!(AbsEvent);
135impl_event_properties!(AbsEvent);
136
137impl From<AbsEvent> for EventBodyOwned {
138	fn from(event: AbsEvent) -> Self {
139		EventBodyOwned { detail1: event.x, detail2: event.y, ..Default::default() }
140	}
141}
142
143impl From<&AbsEvent> for EventBodyOwned {
144	fn from(event: &AbsEvent) -> Self {
145		EventBodyOwned { detail1: event.x, detail2: event.y, ..Default::default() }
146	}
147}
148
149impl From<AbsEvent> for EventBody<'_> {
150	fn from(event: AbsEvent) -> Self {
151		EventBodyOwned::from(event).into()
152	}
153}
154
155event_test_cases!(RelEvent);
156impl_to_dbus_message!(RelEvent);
157impl_from_dbus_message!(RelEvent);
158impl_event_properties!(RelEvent);
159
160impl From<RelEvent> for EventBodyOwned {
161	fn from(event: RelEvent) -> Self {
162		EventBodyOwned { detail1: event.x, detail2: event.y, ..Default::default() }
163	}
164}
165
166impl From<&RelEvent> for EventBodyOwned {
167	fn from(event: &RelEvent) -> Self {
168		EventBodyOwned { detail1: event.x, detail2: event.y, ..Default::default() }
169	}
170}
171
172impl From<RelEvent> for EventBody<'_> {
173	fn from(event: RelEvent) -> Self {
174		EventBodyOwned::from(event).into()
175	}
176}
177
178event_test_cases!(ButtonEvent);
179impl_to_dbus_message!(ButtonEvent);
180impl_from_dbus_message!(ButtonEvent);
181
182impl_event_properties!(ButtonEvent);
183impl From<ButtonEvent> for EventBodyOwned {
184	fn from(event: ButtonEvent) -> Self {
185		EventBodyOwned {
186			kind: event.detail,
187			detail1: event.mouse_x,
188			detail2: event.mouse_y,
189			..Default::default()
190		}
191	}
192}
193
194impl From<ButtonEvent> for EventBody<'_> {
195	fn from(event: ButtonEvent) -> Self {
196		EventBodyOwned::from(event).into()
197	}
198}
199
200impl From<&ButtonEvent> for EventBodyOwned {
201	fn from(event: &ButtonEvent) -> Self {
202		EventBodyOwned {
203			kind: event.detail.clone(),
204			detail1: event.mouse_x,
205			detail2: event.mouse_y,
206			..Default::default()
207		}
208	}
209}
210
211impl_msg_conversion_ext_for_target_type!(AbsEvent);
212impl_msg_conversion_ext_for_target_type!(RelEvent);
213impl_msg_conversion_ext_for_target_type!(ButtonEvent);