1use crate::{ffi, DeviceTool, Display, InputSource, ModifierType, Seat, Surface};
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{connect_raw, SignalHandlerId},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 #[doc(alias = "GdkDevice")]
16 pub struct Device(Object<ffi::GdkDevice>);
17
18 match fn {
19 type_ => || ffi::gdk_device_get_type(),
20 }
21}
22
23impl Device {
24 pub const NONE: Option<&'static Device> = None;
25}
26
27impl std::fmt::Display for Device {
28 #[inline]
29 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
30 f.write_str(&DeviceExt::name(self))
31 }
32}
33
34pub trait DeviceExt: IsA<Device> + 'static {
35 #[cfg(feature = "v4_18")]
36 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
37 #[doc(alias = "gdk_device_get_active_layout_index")]
38 #[doc(alias = "get_active_layout_index")]
39 #[doc(alias = "active-layout-index")]
40 fn active_layout_index(&self) -> i32 {
41 unsafe { ffi::gdk_device_get_active_layout_index(self.as_ref().to_glib_none().0) }
42 }
43
44 #[doc(alias = "gdk_device_get_caps_lock_state")]
45 #[doc(alias = "get_caps_lock_state")]
46 #[doc(alias = "caps-lock-state")]
47 fn is_caps_locked(&self) -> bool {
48 unsafe {
49 from_glib(ffi::gdk_device_get_caps_lock_state(
50 self.as_ref().to_glib_none().0,
51 ))
52 }
53 }
54
55 #[doc(alias = "gdk_device_get_device_tool")]
56 #[doc(alias = "get_device_tool")]
57 #[doc(alias = "tool")]
58 fn device_tool(&self) -> Option<DeviceTool> {
59 unsafe {
60 from_glib_none(ffi::gdk_device_get_device_tool(
61 self.as_ref().to_glib_none().0,
62 ))
63 }
64 }
65
66 #[doc(alias = "gdk_device_get_direction")]
67 #[doc(alias = "get_direction")]
68 fn direction(&self) -> pango::Direction {
69 unsafe {
70 from_glib(ffi::gdk_device_get_direction(
71 self.as_ref().to_glib_none().0,
72 ))
73 }
74 }
75
76 #[doc(alias = "gdk_device_get_display")]
77 #[doc(alias = "get_display")]
78 fn display(&self) -> Display {
79 unsafe { from_glib_none(ffi::gdk_device_get_display(self.as_ref().to_glib_none().0)) }
80 }
81
82 #[doc(alias = "gdk_device_get_has_cursor")]
83 #[doc(alias = "get_has_cursor")]
84 #[doc(alias = "has-cursor")]
85 fn has_cursor(&self) -> bool {
86 unsafe {
87 from_glib(ffi::gdk_device_get_has_cursor(
88 self.as_ref().to_glib_none().0,
89 ))
90 }
91 }
92
93 #[cfg(feature = "v4_18")]
94 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
95 #[doc(alias = "gdk_device_get_layout_names")]
96 #[doc(alias = "get_layout_names")]
97 #[doc(alias = "layout-names")]
98 fn layout_names(&self) -> Vec<glib::GString> {
99 unsafe {
100 FromGlibPtrContainer::from_glib_full(ffi::gdk_device_get_layout_names(
101 self.as_ref().to_glib_none().0,
102 ))
103 }
104 }
105
106 #[doc(alias = "gdk_device_get_modifier_state")]
107 #[doc(alias = "get_modifier_state")]
108 #[doc(alias = "modifier-state")]
109 fn modifier_state(&self) -> ModifierType {
110 unsafe {
111 from_glib(ffi::gdk_device_get_modifier_state(
112 self.as_ref().to_glib_none().0,
113 ))
114 }
115 }
116
117 #[doc(alias = "gdk_device_get_name")]
118 #[doc(alias = "get_name")]
119 fn name(&self) -> glib::GString {
120 unsafe { from_glib_none(ffi::gdk_device_get_name(self.as_ref().to_glib_none().0)) }
121 }
122
123 #[doc(alias = "gdk_device_get_num_lock_state")]
124 #[doc(alias = "get_num_lock_state")]
125 #[doc(alias = "num-lock-state")]
126 fn is_num_locked(&self) -> bool {
127 unsafe {
128 from_glib(ffi::gdk_device_get_num_lock_state(
129 self.as_ref().to_glib_none().0,
130 ))
131 }
132 }
133
134 #[doc(alias = "gdk_device_get_num_touches")]
135 #[doc(alias = "get_num_touches")]
136 #[doc(alias = "num-touches")]
137 fn num_touches(&self) -> u32 {
138 unsafe { ffi::gdk_device_get_num_touches(self.as_ref().to_glib_none().0) }
139 }
140
141 #[doc(alias = "gdk_device_get_product_id")]
142 #[doc(alias = "get_product_id")]
143 #[doc(alias = "product-id")]
144 fn product_id(&self) -> Option<glib::GString> {
145 unsafe {
146 from_glib_none(ffi::gdk_device_get_product_id(
147 self.as_ref().to_glib_none().0,
148 ))
149 }
150 }
151
152 #[doc(alias = "gdk_device_get_scroll_lock_state")]
153 #[doc(alias = "get_scroll_lock_state")]
154 #[doc(alias = "scroll-lock-state")]
155 fn is_scroll_locked(&self) -> bool {
156 unsafe {
157 from_glib(ffi::gdk_device_get_scroll_lock_state(
158 self.as_ref().to_glib_none().0,
159 ))
160 }
161 }
162
163 #[doc(alias = "gdk_device_get_seat")]
164 #[doc(alias = "get_seat")]
165 fn seat(&self) -> Seat {
166 unsafe { from_glib_none(ffi::gdk_device_get_seat(self.as_ref().to_glib_none().0)) }
167 }
168
169 #[doc(alias = "gdk_device_get_source")]
170 #[doc(alias = "get_source")]
171 fn source(&self) -> InputSource {
172 unsafe { from_glib(ffi::gdk_device_get_source(self.as_ref().to_glib_none().0)) }
173 }
174
175 #[doc(alias = "gdk_device_get_surface_at_position")]
176 #[doc(alias = "get_surface_at_position")]
177 fn surface_at_position(&self) -> (Option<Surface>, f64, f64) {
178 unsafe {
179 let mut win_x = std::mem::MaybeUninit::uninit();
180 let mut win_y = std::mem::MaybeUninit::uninit();
181 let ret = from_glib_none(ffi::gdk_device_get_surface_at_position(
182 self.as_ref().to_glib_none().0,
183 win_x.as_mut_ptr(),
184 win_y.as_mut_ptr(),
185 ));
186 (ret, win_x.assume_init(), win_y.assume_init())
187 }
188 }
189
190 #[cfg(feature = "v4_2")]
191 #[cfg_attr(docsrs, doc(cfg(feature = "v4_2")))]
192 #[doc(alias = "gdk_device_get_timestamp")]
193 #[doc(alias = "get_timestamp")]
194 fn timestamp(&self) -> u32 {
195 unsafe { ffi::gdk_device_get_timestamp(self.as_ref().to_glib_none().0) }
196 }
197
198 #[doc(alias = "gdk_device_get_vendor_id")]
199 #[doc(alias = "get_vendor_id")]
200 #[doc(alias = "vendor-id")]
201 fn vendor_id(&self) -> Option<glib::GString> {
202 unsafe {
203 from_glib_none(ffi::gdk_device_get_vendor_id(
204 self.as_ref().to_glib_none().0,
205 ))
206 }
207 }
208
209 #[doc(alias = "gdk_device_has_bidi_layouts")]
210 #[doc(alias = "has-bidi-layouts")]
211 fn has_bidi_layouts(&self) -> bool {
212 unsafe {
213 from_glib(ffi::gdk_device_has_bidi_layouts(
214 self.as_ref().to_glib_none().0,
215 ))
216 }
217 }
218
219 #[doc(alias = "n-axes")]
220 fn n_axes(&self) -> u32 {
221 ObjectExt::property(self.as_ref(), "n-axes")
222 }
223
224 fn set_seat<P: IsA<Seat>>(&self, seat: Option<&P>) {
225 ObjectExt::set_property(self.as_ref(), "seat", seat)
226 }
227
228 #[doc(alias = "changed")]
229 fn connect_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
230 unsafe extern "C" fn changed_trampoline<P: IsA<Device>, F: Fn(&P) + 'static>(
231 this: *mut ffi::GdkDevice,
232 f: glib::ffi::gpointer,
233 ) {
234 let f: &F = &*(f as *const F);
235 f(Device::from_glib_borrow(this).unsafe_cast_ref())
236 }
237 unsafe {
238 let f: Box_<F> = Box_::new(f);
239 connect_raw(
240 self.as_ptr() as *mut _,
241 c"changed".as_ptr() as *const _,
242 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
243 changed_trampoline::<Self, F> as *const (),
244 )),
245 Box_::into_raw(f),
246 )
247 }
248 }
249
250 #[doc(alias = "tool-changed")]
251 fn connect_tool_changed<F: Fn(&Self, &DeviceTool) + 'static>(&self, f: F) -> SignalHandlerId {
252 unsafe extern "C" fn tool_changed_trampoline<
253 P: IsA<Device>,
254 F: Fn(&P, &DeviceTool) + 'static,
255 >(
256 this: *mut ffi::GdkDevice,
257 tool: *mut ffi::GdkDeviceTool,
258 f: glib::ffi::gpointer,
259 ) {
260 let f: &F = &*(f as *const F);
261 f(
262 Device::from_glib_borrow(this).unsafe_cast_ref(),
263 &from_glib_borrow(tool),
264 )
265 }
266 unsafe {
267 let f: Box_<F> = Box_::new(f);
268 connect_raw(
269 self.as_ptr() as *mut _,
270 c"tool-changed".as_ptr() as *const _,
271 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
272 tool_changed_trampoline::<Self, F> as *const (),
273 )),
274 Box_::into_raw(f),
275 )
276 }
277 }
278
279 #[cfg(feature = "v4_18")]
280 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
281 #[doc(alias = "active-layout-index")]
282 fn connect_active_layout_index_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
283 unsafe extern "C" fn notify_active_layout_index_trampoline<
284 P: IsA<Device>,
285 F: Fn(&P) + 'static,
286 >(
287 this: *mut ffi::GdkDevice,
288 _param_spec: glib::ffi::gpointer,
289 f: glib::ffi::gpointer,
290 ) {
291 let f: &F = &*(f as *const F);
292 f(Device::from_glib_borrow(this).unsafe_cast_ref())
293 }
294 unsafe {
295 let f: Box_<F> = Box_::new(f);
296 connect_raw(
297 self.as_ptr() as *mut _,
298 c"notify::active-layout-index".as_ptr() as *const _,
299 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
300 notify_active_layout_index_trampoline::<Self, F> as *const (),
301 )),
302 Box_::into_raw(f),
303 )
304 }
305 }
306
307 #[doc(alias = "caps-lock-state")]
308 fn connect_caps_lock_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
309 unsafe extern "C" fn notify_caps_lock_state_trampoline<
310 P: IsA<Device>,
311 F: Fn(&P) + 'static,
312 >(
313 this: *mut ffi::GdkDevice,
314 _param_spec: glib::ffi::gpointer,
315 f: glib::ffi::gpointer,
316 ) {
317 let f: &F = &*(f as *const F);
318 f(Device::from_glib_borrow(this).unsafe_cast_ref())
319 }
320 unsafe {
321 let f: Box_<F> = Box_::new(f);
322 connect_raw(
323 self.as_ptr() as *mut _,
324 c"notify::caps-lock-state".as_ptr() as *const _,
325 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
326 notify_caps_lock_state_trampoline::<Self, F> as *const (),
327 )),
328 Box_::into_raw(f),
329 )
330 }
331 }
332
333 #[doc(alias = "direction")]
334 fn connect_direction_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
335 unsafe extern "C" fn notify_direction_trampoline<P: IsA<Device>, F: Fn(&P) + 'static>(
336 this: *mut ffi::GdkDevice,
337 _param_spec: glib::ffi::gpointer,
338 f: glib::ffi::gpointer,
339 ) {
340 let f: &F = &*(f as *const F);
341 f(Device::from_glib_borrow(this).unsafe_cast_ref())
342 }
343 unsafe {
344 let f: Box_<F> = Box_::new(f);
345 connect_raw(
346 self.as_ptr() as *mut _,
347 c"notify::direction".as_ptr() as *const _,
348 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
349 notify_direction_trampoline::<Self, F> as *const (),
350 )),
351 Box_::into_raw(f),
352 )
353 }
354 }
355
356 #[doc(alias = "has-bidi-layouts")]
357 fn connect_has_bidi_layouts_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
358 unsafe extern "C" fn notify_has_bidi_layouts_trampoline<
359 P: IsA<Device>,
360 F: Fn(&P) + 'static,
361 >(
362 this: *mut ffi::GdkDevice,
363 _param_spec: glib::ffi::gpointer,
364 f: glib::ffi::gpointer,
365 ) {
366 let f: &F = &*(f as *const F);
367 f(Device::from_glib_borrow(this).unsafe_cast_ref())
368 }
369 unsafe {
370 let f: Box_<F> = Box_::new(f);
371 connect_raw(
372 self.as_ptr() as *mut _,
373 c"notify::has-bidi-layouts".as_ptr() as *const _,
374 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
375 notify_has_bidi_layouts_trampoline::<Self, F> as *const (),
376 )),
377 Box_::into_raw(f),
378 )
379 }
380 }
381
382 #[cfg(feature = "v4_18")]
383 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
384 #[doc(alias = "layout-names")]
385 fn connect_layout_names_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
386 unsafe extern "C" fn notify_layout_names_trampoline<P: IsA<Device>, F: Fn(&P) + 'static>(
387 this: *mut ffi::GdkDevice,
388 _param_spec: glib::ffi::gpointer,
389 f: glib::ffi::gpointer,
390 ) {
391 let f: &F = &*(f as *const F);
392 f(Device::from_glib_borrow(this).unsafe_cast_ref())
393 }
394 unsafe {
395 let f: Box_<F> = Box_::new(f);
396 connect_raw(
397 self.as_ptr() as *mut _,
398 c"notify::layout-names".as_ptr() as *const _,
399 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
400 notify_layout_names_trampoline::<Self, F> as *const (),
401 )),
402 Box_::into_raw(f),
403 )
404 }
405 }
406
407 #[doc(alias = "modifier-state")]
408 fn connect_modifier_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
409 unsafe extern "C" fn notify_modifier_state_trampoline<
410 P: IsA<Device>,
411 F: Fn(&P) + 'static,
412 >(
413 this: *mut ffi::GdkDevice,
414 _param_spec: glib::ffi::gpointer,
415 f: glib::ffi::gpointer,
416 ) {
417 let f: &F = &*(f as *const F);
418 f(Device::from_glib_borrow(this).unsafe_cast_ref())
419 }
420 unsafe {
421 let f: Box_<F> = Box_::new(f);
422 connect_raw(
423 self.as_ptr() as *mut _,
424 c"notify::modifier-state".as_ptr() as *const _,
425 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
426 notify_modifier_state_trampoline::<Self, F> as *const (),
427 )),
428 Box_::into_raw(f),
429 )
430 }
431 }
432
433 #[doc(alias = "n-axes")]
434 fn connect_n_axes_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
435 unsafe extern "C" fn notify_n_axes_trampoline<P: IsA<Device>, F: Fn(&P) + 'static>(
436 this: *mut ffi::GdkDevice,
437 _param_spec: glib::ffi::gpointer,
438 f: glib::ffi::gpointer,
439 ) {
440 let f: &F = &*(f as *const F);
441 f(Device::from_glib_borrow(this).unsafe_cast_ref())
442 }
443 unsafe {
444 let f: Box_<F> = Box_::new(f);
445 connect_raw(
446 self.as_ptr() as *mut _,
447 c"notify::n-axes".as_ptr() as *const _,
448 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
449 notify_n_axes_trampoline::<Self, F> as *const (),
450 )),
451 Box_::into_raw(f),
452 )
453 }
454 }
455
456 #[doc(alias = "num-lock-state")]
457 fn connect_num_lock_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
458 unsafe extern "C" fn notify_num_lock_state_trampoline<
459 P: IsA<Device>,
460 F: Fn(&P) + 'static,
461 >(
462 this: *mut ffi::GdkDevice,
463 _param_spec: glib::ffi::gpointer,
464 f: glib::ffi::gpointer,
465 ) {
466 let f: &F = &*(f as *const F);
467 f(Device::from_glib_borrow(this).unsafe_cast_ref())
468 }
469 unsafe {
470 let f: Box_<F> = Box_::new(f);
471 connect_raw(
472 self.as_ptr() as *mut _,
473 c"notify::num-lock-state".as_ptr() as *const _,
474 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
475 notify_num_lock_state_trampoline::<Self, F> as *const (),
476 )),
477 Box_::into_raw(f),
478 )
479 }
480 }
481
482 #[doc(alias = "scroll-lock-state")]
483 fn connect_scroll_lock_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
484 unsafe extern "C" fn notify_scroll_lock_state_trampoline<
485 P: IsA<Device>,
486 F: Fn(&P) + 'static,
487 >(
488 this: *mut ffi::GdkDevice,
489 _param_spec: glib::ffi::gpointer,
490 f: glib::ffi::gpointer,
491 ) {
492 let f: &F = &*(f as *const F);
493 f(Device::from_glib_borrow(this).unsafe_cast_ref())
494 }
495 unsafe {
496 let f: Box_<F> = Box_::new(f);
497 connect_raw(
498 self.as_ptr() as *mut _,
499 c"notify::scroll-lock-state".as_ptr() as *const _,
500 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
501 notify_scroll_lock_state_trampoline::<Self, F> as *const (),
502 )),
503 Box_::into_raw(f),
504 )
505 }
506 }
507
508 #[doc(alias = "seat")]
509 fn connect_seat_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
510 unsafe extern "C" fn notify_seat_trampoline<P: IsA<Device>, F: Fn(&P) + 'static>(
511 this: *mut ffi::GdkDevice,
512 _param_spec: glib::ffi::gpointer,
513 f: glib::ffi::gpointer,
514 ) {
515 let f: &F = &*(f as *const F);
516 f(Device::from_glib_borrow(this).unsafe_cast_ref())
517 }
518 unsafe {
519 let f: Box_<F> = Box_::new(f);
520 connect_raw(
521 self.as_ptr() as *mut _,
522 c"notify::seat".as_ptr() as *const _,
523 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
524 notify_seat_trampoline::<Self, F> as *const (),
525 )),
526 Box_::into_raw(f),
527 )
528 }
529 }
530
531 #[doc(alias = "tool")]
532 fn connect_tool_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
533 unsafe extern "C" fn notify_tool_trampoline<P: IsA<Device>, F: Fn(&P) + 'static>(
534 this: *mut ffi::GdkDevice,
535 _param_spec: glib::ffi::gpointer,
536 f: glib::ffi::gpointer,
537 ) {
538 let f: &F = &*(f as *const F);
539 f(Device::from_glib_borrow(this).unsafe_cast_ref())
540 }
541 unsafe {
542 let f: Box_<F> = Box_::new(f);
543 connect_raw(
544 self.as_ptr() as *mut _,
545 c"notify::tool".as_ptr() as *const _,
546 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
547 notify_tool_trampoline::<Self, F> as *const (),
548 )),
549 Box_::into_raw(f),
550 )
551 }
552 }
553}
554
555impl<O: IsA<Device>> DeviceExt for O {}