1use crate::{DeviceTool, Display, InputSource, ModifierType, Seat, Surface, ffi};
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{SignalHandlerId, connect_raw},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 #[doc(alias = "GdkDevice")]
181 pub struct Device(Object<ffi::GdkDevice>);
182
183 match fn {
184 type_ => || ffi::gdk_device_get_type(),
185 }
186}
187
188impl Device {
189 pub const NONE: Option<&'static Device> = None;
190}
191
192impl std::fmt::Display for Device {
193 #[inline]
194 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
195 f.write_str(&DeviceExt::name(self))
196 }
197}
198
199pub trait DeviceExt: IsA<Device> + 'static {
205 #[cfg(feature = "v4_18")]
216 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
217 #[doc(alias = "gdk_device_get_active_layout_index")]
218 #[doc(alias = "get_active_layout_index")]
219 #[doc(alias = "active-layout-index")]
220 fn active_layout_index(&self) -> i32 {
221 unsafe { ffi::gdk_device_get_active_layout_index(self.as_ref().to_glib_none().0) }
222 }
223
224 #[doc(alias = "gdk_device_get_caps_lock_state")]
232 #[doc(alias = "get_caps_lock_state")]
233 #[doc(alias = "caps-lock-state")]
234 fn is_caps_locked(&self) -> bool {
235 unsafe {
236 from_glib(ffi::gdk_device_get_caps_lock_state(
237 self.as_ref().to_glib_none().0,
238 ))
239 }
240 }
241
242 #[doc(alias = "gdk_device_get_device_tool")]
248 #[doc(alias = "get_device_tool")]
249 #[doc(alias = "tool")]
250 fn device_tool(&self) -> Option<DeviceTool> {
251 unsafe {
252 from_glib_none(ffi::gdk_device_get_device_tool(
253 self.as_ref().to_glib_none().0,
254 ))
255 }
256 }
257
258 #[doc(alias = "gdk_device_get_direction")]
271 #[doc(alias = "get_direction")]
272 fn direction(&self) -> pango::Direction {
273 unsafe {
274 from_glib(ffi::gdk_device_get_direction(
275 self.as_ref().to_glib_none().0,
276 ))
277 }
278 }
279
280 #[doc(alias = "gdk_device_get_display")]
286 #[doc(alias = "get_display")]
287 fn display(&self) -> Display {
288 unsafe { from_glib_none(ffi::gdk_device_get_display(self.as_ref().to_glib_none().0)) }
289 }
290
291 #[doc(alias = "gdk_device_get_has_cursor")]
300 #[doc(alias = "get_has_cursor")]
301 #[doc(alias = "has-cursor")]
302 fn has_cursor(&self) -> bool {
303 unsafe {
304 from_glib(ffi::gdk_device_get_has_cursor(
305 self.as_ref().to_glib_none().0,
306 ))
307 }
308 }
309
310 #[cfg(feature = "v4_18")]
319 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
320 #[doc(alias = "gdk_device_get_layout_names")]
321 #[doc(alias = "get_layout_names")]
322 #[doc(alias = "layout-names")]
323 fn layout_names(&self) -> Vec<glib::GString> {
324 unsafe {
325 FromGlibPtrContainer::from_glib_full(ffi::gdk_device_get_layout_names(
326 self.as_ref().to_glib_none().0,
327 ))
328 }
329 }
330
331 #[doc(alias = "gdk_device_get_modifier_state")]
339 #[doc(alias = "get_modifier_state")]
340 #[doc(alias = "modifier-state")]
341 fn modifier_state(&self) -> ModifierType {
342 unsafe {
343 from_glib(ffi::gdk_device_get_modifier_state(
344 self.as_ref().to_glib_none().0,
345 ))
346 }
347 }
348
349 #[doc(alias = "gdk_device_get_name")]
355 #[doc(alias = "get_name")]
356 fn name(&self) -> glib::GString {
357 unsafe { from_glib_none(ffi::gdk_device_get_name(self.as_ref().to_glib_none().0)) }
358 }
359
360 #[doc(alias = "gdk_device_get_num_lock_state")]
368 #[doc(alias = "get_num_lock_state")]
369 #[doc(alias = "num-lock-state")]
370 fn is_num_locked(&self) -> bool {
371 unsafe {
372 from_glib(ffi::gdk_device_get_num_lock_state(
373 self.as_ref().to_glib_none().0,
374 ))
375 }
376 }
377
378 #[doc(alias = "gdk_device_get_num_touches")]
384 #[doc(alias = "get_num_touches")]
385 #[doc(alias = "num-touches")]
386 fn num_touches(&self) -> u32 {
387 unsafe { ffi::gdk_device_get_num_touches(self.as_ref().to_glib_none().0) }
388 }
389
390 #[doc(alias = "gdk_device_get_product_id")]
399 #[doc(alias = "get_product_id")]
400 #[doc(alias = "product-id")]
401 fn product_id(&self) -> Option<glib::GString> {
402 unsafe {
403 from_glib_none(ffi::gdk_device_get_product_id(
404 self.as_ref().to_glib_none().0,
405 ))
406 }
407 }
408
409 #[doc(alias = "gdk_device_get_scroll_lock_state")]
417 #[doc(alias = "get_scroll_lock_state")]
418 #[doc(alias = "scroll-lock-state")]
419 fn is_scroll_locked(&self) -> bool {
420 unsafe {
421 from_glib(ffi::gdk_device_get_scroll_lock_state(
422 self.as_ref().to_glib_none().0,
423 ))
424 }
425 }
426
427 #[doc(alias = "gdk_device_get_seat")]
433 #[doc(alias = "get_seat")]
434 fn seat(&self) -> Seat {
435 unsafe { from_glib_none(ffi::gdk_device_get_seat(self.as_ref().to_glib_none().0)) }
436 }
437
438 #[doc(alias = "gdk_device_get_source")]
444 #[doc(alias = "get_source")]
445 fn source(&self) -> InputSource {
446 unsafe { from_glib(ffi::gdk_device_get_source(self.as_ref().to_glib_none().0)) }
447 }
448
449 #[doc(alias = "gdk_device_get_surface_at_position")]
468 #[doc(alias = "get_surface_at_position")]
469 fn surface_at_position(&self) -> (Option<Surface>, f64, f64) {
470 unsafe {
471 let mut win_x = std::mem::MaybeUninit::uninit();
472 let mut win_y = std::mem::MaybeUninit::uninit();
473 let ret = from_glib_none(ffi::gdk_device_get_surface_at_position(
474 self.as_ref().to_glib_none().0,
475 win_x.as_mut_ptr(),
476 win_y.as_mut_ptr(),
477 ));
478 (ret, win_x.assume_init(), win_y.assume_init())
479 }
480 }
481
482 #[cfg(feature = "v4_2")]
493 #[cfg_attr(docsrs, doc(cfg(feature = "v4_2")))]
494 #[doc(alias = "gdk_device_get_timestamp")]
495 #[doc(alias = "get_timestamp")]
496 fn timestamp(&self) -> u32 {
497 unsafe { ffi::gdk_device_get_timestamp(self.as_ref().to_glib_none().0) }
498 }
499
500 #[doc(alias = "gdk_device_get_vendor_id")]
534 #[doc(alias = "get_vendor_id")]
535 #[doc(alias = "vendor-id")]
536 fn vendor_id(&self) -> Option<glib::GString> {
537 unsafe {
538 from_glib_none(ffi::gdk_device_get_vendor_id(
539 self.as_ref().to_glib_none().0,
540 ))
541 }
542 }
543
544 #[doc(alias = "gdk_device_has_bidi_layouts")]
553 #[doc(alias = "has-bidi-layouts")]
554 fn has_bidi_layouts(&self) -> bool {
555 unsafe {
556 from_glib(ffi::gdk_device_has_bidi_layouts(
557 self.as_ref().to_glib_none().0,
558 ))
559 }
560 }
561
562 #[doc(alias = "n-axes")]
564 fn n_axes(&self) -> u32 {
565 ObjectExt::property(self.as_ref(), "n-axes")
566 }
567
568 fn set_seat<P: IsA<Seat>>(&self, seat: Option<&P>) {
570 ObjectExt::set_property(self.as_ref(), "seat", seat)
571 }
572
573 #[doc(alias = "changed")]
581 fn connect_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
582 unsafe extern "C" fn changed_trampoline<P: IsA<Device>, F: Fn(&P) + 'static>(
583 this: *mut ffi::GdkDevice,
584 f: glib::ffi::gpointer,
585 ) {
586 unsafe {
587 let f: &F = &*(f as *const F);
588 f(Device::from_glib_borrow(this).unsafe_cast_ref())
589 }
590 }
591 unsafe {
592 let f: Box_<F> = Box_::new(f);
593 connect_raw(
594 self.as_ptr() as *mut _,
595 c"changed".as_ptr() as *const _,
596 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
597 changed_trampoline::<Self, F> as *const (),
598 )),
599 Box_::into_raw(f),
600 )
601 }
602 }
603
604 #[doc(alias = "tool-changed")]
608 fn connect_tool_changed<F: Fn(&Self, &DeviceTool) + 'static>(&self, f: F) -> SignalHandlerId {
609 unsafe extern "C" fn tool_changed_trampoline<
610 P: IsA<Device>,
611 F: Fn(&P, &DeviceTool) + 'static,
612 >(
613 this: *mut ffi::GdkDevice,
614 tool: *mut ffi::GdkDeviceTool,
615 f: glib::ffi::gpointer,
616 ) {
617 unsafe {
618 let f: &F = &*(f as *const F);
619 f(
620 Device::from_glib_borrow(this).unsafe_cast_ref(),
621 &from_glib_borrow(tool),
622 )
623 }
624 }
625 unsafe {
626 let f: Box_<F> = Box_::new(f);
627 connect_raw(
628 self.as_ptr() as *mut _,
629 c"tool-changed".as_ptr() as *const _,
630 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
631 tool_changed_trampoline::<Self, F> as *const (),
632 )),
633 Box_::into_raw(f),
634 )
635 }
636 }
637
638 #[cfg(feature = "v4_18")]
639 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
640 #[doc(alias = "active-layout-index")]
641 fn connect_active_layout_index_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
642 unsafe extern "C" fn notify_active_layout_index_trampoline<
643 P: IsA<Device>,
644 F: Fn(&P) + 'static,
645 >(
646 this: *mut ffi::GdkDevice,
647 _param_spec: glib::ffi::gpointer,
648 f: glib::ffi::gpointer,
649 ) {
650 unsafe {
651 let f: &F = &*(f as *const F);
652 f(Device::from_glib_borrow(this).unsafe_cast_ref())
653 }
654 }
655 unsafe {
656 let f: Box_<F> = Box_::new(f);
657 connect_raw(
658 self.as_ptr() as *mut _,
659 c"notify::active-layout-index".as_ptr() as *const _,
660 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
661 notify_active_layout_index_trampoline::<Self, F> as *const (),
662 )),
663 Box_::into_raw(f),
664 )
665 }
666 }
667
668 #[doc(alias = "caps-lock-state")]
669 fn connect_caps_lock_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
670 unsafe extern "C" fn notify_caps_lock_state_trampoline<
671 P: IsA<Device>,
672 F: Fn(&P) + 'static,
673 >(
674 this: *mut ffi::GdkDevice,
675 _param_spec: glib::ffi::gpointer,
676 f: glib::ffi::gpointer,
677 ) {
678 unsafe {
679 let f: &F = &*(f as *const F);
680 f(Device::from_glib_borrow(this).unsafe_cast_ref())
681 }
682 }
683 unsafe {
684 let f: Box_<F> = Box_::new(f);
685 connect_raw(
686 self.as_ptr() as *mut _,
687 c"notify::caps-lock-state".as_ptr() as *const _,
688 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
689 notify_caps_lock_state_trampoline::<Self, F> as *const (),
690 )),
691 Box_::into_raw(f),
692 )
693 }
694 }
695
696 #[doc(alias = "direction")]
697 fn connect_direction_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
698 unsafe extern "C" fn notify_direction_trampoline<P: IsA<Device>, F: Fn(&P) + 'static>(
699 this: *mut ffi::GdkDevice,
700 _param_spec: glib::ffi::gpointer,
701 f: glib::ffi::gpointer,
702 ) {
703 unsafe {
704 let f: &F = &*(f as *const F);
705 f(Device::from_glib_borrow(this).unsafe_cast_ref())
706 }
707 }
708 unsafe {
709 let f: Box_<F> = Box_::new(f);
710 connect_raw(
711 self.as_ptr() as *mut _,
712 c"notify::direction".as_ptr() as *const _,
713 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
714 notify_direction_trampoline::<Self, F> as *const (),
715 )),
716 Box_::into_raw(f),
717 )
718 }
719 }
720
721 #[doc(alias = "has-bidi-layouts")]
722 fn connect_has_bidi_layouts_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
723 unsafe extern "C" fn notify_has_bidi_layouts_trampoline<
724 P: IsA<Device>,
725 F: Fn(&P) + 'static,
726 >(
727 this: *mut ffi::GdkDevice,
728 _param_spec: glib::ffi::gpointer,
729 f: glib::ffi::gpointer,
730 ) {
731 unsafe {
732 let f: &F = &*(f as *const F);
733 f(Device::from_glib_borrow(this).unsafe_cast_ref())
734 }
735 }
736 unsafe {
737 let f: Box_<F> = Box_::new(f);
738 connect_raw(
739 self.as_ptr() as *mut _,
740 c"notify::has-bidi-layouts".as_ptr() as *const _,
741 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
742 notify_has_bidi_layouts_trampoline::<Self, F> as *const (),
743 )),
744 Box_::into_raw(f),
745 )
746 }
747 }
748
749 #[cfg(feature = "v4_18")]
750 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
751 #[doc(alias = "layout-names")]
752 fn connect_layout_names_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
753 unsafe extern "C" fn notify_layout_names_trampoline<P: IsA<Device>, F: Fn(&P) + 'static>(
754 this: *mut ffi::GdkDevice,
755 _param_spec: glib::ffi::gpointer,
756 f: glib::ffi::gpointer,
757 ) {
758 unsafe {
759 let f: &F = &*(f as *const F);
760 f(Device::from_glib_borrow(this).unsafe_cast_ref())
761 }
762 }
763 unsafe {
764 let f: Box_<F> = Box_::new(f);
765 connect_raw(
766 self.as_ptr() as *mut _,
767 c"notify::layout-names".as_ptr() as *const _,
768 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
769 notify_layout_names_trampoline::<Self, F> as *const (),
770 )),
771 Box_::into_raw(f),
772 )
773 }
774 }
775
776 #[doc(alias = "modifier-state")]
777 fn connect_modifier_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
778 unsafe extern "C" fn notify_modifier_state_trampoline<
779 P: IsA<Device>,
780 F: Fn(&P) + 'static,
781 >(
782 this: *mut ffi::GdkDevice,
783 _param_spec: glib::ffi::gpointer,
784 f: glib::ffi::gpointer,
785 ) {
786 unsafe {
787 let f: &F = &*(f as *const F);
788 f(Device::from_glib_borrow(this).unsafe_cast_ref())
789 }
790 }
791 unsafe {
792 let f: Box_<F> = Box_::new(f);
793 connect_raw(
794 self.as_ptr() as *mut _,
795 c"notify::modifier-state".as_ptr() as *const _,
796 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
797 notify_modifier_state_trampoline::<Self, F> as *const (),
798 )),
799 Box_::into_raw(f),
800 )
801 }
802 }
803
804 #[doc(alias = "n-axes")]
805 fn connect_n_axes_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
806 unsafe extern "C" fn notify_n_axes_trampoline<P: IsA<Device>, F: Fn(&P) + 'static>(
807 this: *mut ffi::GdkDevice,
808 _param_spec: glib::ffi::gpointer,
809 f: glib::ffi::gpointer,
810 ) {
811 unsafe {
812 let f: &F = &*(f as *const F);
813 f(Device::from_glib_borrow(this).unsafe_cast_ref())
814 }
815 }
816 unsafe {
817 let f: Box_<F> = Box_::new(f);
818 connect_raw(
819 self.as_ptr() as *mut _,
820 c"notify::n-axes".as_ptr() as *const _,
821 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
822 notify_n_axes_trampoline::<Self, F> as *const (),
823 )),
824 Box_::into_raw(f),
825 )
826 }
827 }
828
829 #[doc(alias = "num-lock-state")]
830 fn connect_num_lock_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
831 unsafe extern "C" fn notify_num_lock_state_trampoline<
832 P: IsA<Device>,
833 F: Fn(&P) + 'static,
834 >(
835 this: *mut ffi::GdkDevice,
836 _param_spec: glib::ffi::gpointer,
837 f: glib::ffi::gpointer,
838 ) {
839 unsafe {
840 let f: &F = &*(f as *const F);
841 f(Device::from_glib_borrow(this).unsafe_cast_ref())
842 }
843 }
844 unsafe {
845 let f: Box_<F> = Box_::new(f);
846 connect_raw(
847 self.as_ptr() as *mut _,
848 c"notify::num-lock-state".as_ptr() as *const _,
849 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
850 notify_num_lock_state_trampoline::<Self, F> as *const (),
851 )),
852 Box_::into_raw(f),
853 )
854 }
855 }
856
857 #[doc(alias = "scroll-lock-state")]
858 fn connect_scroll_lock_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
859 unsafe extern "C" fn notify_scroll_lock_state_trampoline<
860 P: IsA<Device>,
861 F: Fn(&P) + 'static,
862 >(
863 this: *mut ffi::GdkDevice,
864 _param_spec: glib::ffi::gpointer,
865 f: glib::ffi::gpointer,
866 ) {
867 unsafe {
868 let f: &F = &*(f as *const F);
869 f(Device::from_glib_borrow(this).unsafe_cast_ref())
870 }
871 }
872 unsafe {
873 let f: Box_<F> = Box_::new(f);
874 connect_raw(
875 self.as_ptr() as *mut _,
876 c"notify::scroll-lock-state".as_ptr() as *const _,
877 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
878 notify_scroll_lock_state_trampoline::<Self, F> as *const (),
879 )),
880 Box_::into_raw(f),
881 )
882 }
883 }
884
885 #[doc(alias = "seat")]
886 fn connect_seat_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
887 unsafe extern "C" fn notify_seat_trampoline<P: IsA<Device>, F: Fn(&P) + 'static>(
888 this: *mut ffi::GdkDevice,
889 _param_spec: glib::ffi::gpointer,
890 f: glib::ffi::gpointer,
891 ) {
892 unsafe {
893 let f: &F = &*(f as *const F);
894 f(Device::from_glib_borrow(this).unsafe_cast_ref())
895 }
896 }
897 unsafe {
898 let f: Box_<F> = Box_::new(f);
899 connect_raw(
900 self.as_ptr() as *mut _,
901 c"notify::seat".as_ptr() as *const _,
902 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
903 notify_seat_trampoline::<Self, F> as *const (),
904 )),
905 Box_::into_raw(f),
906 )
907 }
908 }
909
910 #[doc(alias = "tool")]
911 fn connect_tool_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
912 unsafe extern "C" fn notify_tool_trampoline<P: IsA<Device>, F: Fn(&P) + 'static>(
913 this: *mut ffi::GdkDevice,
914 _param_spec: glib::ffi::gpointer,
915 f: glib::ffi::gpointer,
916 ) {
917 unsafe {
918 let f: &F = &*(f as *const F);
919 f(Device::from_glib_borrow(this).unsafe_cast_ref())
920 }
921 }
922 unsafe {
923 let f: Box_<F> = Box_::new(f);
924 connect_raw(
925 self.as_ptr() as *mut _,
926 c"notify::tool".as_ptr() as *const _,
927 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
928 notify_tool_trampoline::<Self, F> as *const (),
929 )),
930 Box_::into_raw(f),
931 )
932 }
933 }
934}
935
936impl<O: IsA<Device>> DeviceExt for O {}