core_graphics/
event_source.rs

1use core_foundation::base::{CFRelease, CFRetain, CFTypeID};
2use foreign_types::{foreign_type, ForeignType};
3
4/// Possible source states of an event source.
5#[repr(C)]
6#[derive(Clone, Copy, Debug)]
7pub enum CGEventSourceStateID {
8    Private = -1,
9    CombinedSessionState = 0,
10    HIDSystemState = 1,
11}
12
13foreign_type! {
14    #[doc(hidden)]
15    pub unsafe type CGEventSource {
16        type CType = crate::sys::CGEventSource;
17        fn drop = |p| CFRelease(p as *mut _);
18        fn clone = |p| CFRetain(p as *const _) as *mut _;
19    }
20}
21
22impl CGEventSource {
23    pub fn type_id() -> CFTypeID {
24        unsafe { CGEventSourceGetTypeID() }
25    }
26
27    pub fn new(state_id: CGEventSourceStateID) -> Result<Self, ()> {
28        unsafe {
29            let event_source_ref = CGEventSourceCreate(state_id);
30            if !event_source_ref.is_null() {
31                Ok(Self::from_ptr(event_source_ref))
32            } else {
33                Err(())
34            }
35        }
36    }
37}
38
39#[cfg_attr(feature = "link", link(name = "CoreGraphics", kind = "framework"))]
40extern "C" {
41    /// Return the type identifier for the opaque type [`CGEventSourceRef`].
42    ///
43    /// [`CGEventSourceRef`]: crate::sys::CGEventSourceRef
44    fn CGEventSourceGetTypeID() -> CFTypeID;
45
46    /// Return a Quartz event source created with a specified source state.
47    fn CGEventSourceCreate(stateID: CGEventSourceStateID) -> crate::sys::CGEventSourceRef;
48}