core_graphics2/
event_source.rs1use core_foundation::{
2 base::{CFTypeID, TCFType},
3 date::CFTimeInterval,
4 declare_TCFType, impl_CFTypeDescription, impl_TCFType,
5};
6use libc::c_double;
7
8use crate::{
9 event_types::{CGEventFlags, CGEventSourceKeyboardType, CGEventSourceRef, CGEventSourceStateID, CGEventType, CGMouseButton},
10 remote_operation::CGKeyCode,
11};
12
13extern "C" {
14 pub fn CGEventSourceGetTypeID() -> CFTypeID;
15 pub fn CGEventSourceCreate(stateID: CGEventSourceStateID) -> CGEventSourceRef;
16 pub fn CGEventSourceGetKeyboardType(source: CGEventSourceRef) -> CGEventSourceKeyboardType;
17 pub fn CGEventSourceSetKeyboardType(source: CGEventSourceRef, keyboardType: CGEventSourceKeyboardType);
18 pub fn CGEventSourceGetPixelsPerLine(source: CGEventSourceRef) -> c_double;
19 pub fn CGEventSourceSetPixelsPerLine(source: CGEventSourceRef, pixelsPerLine: c_double);
20 pub fn CGEventSourceGetSourceStateID(source: CGEventSourceRef) -> CGEventSourceStateID;
21 pub fn CGEventSourceButtonState(stateID: CGEventSourceStateID, button: CGMouseButton) -> bool;
22 pub fn CGEventSourceKeyState(stateID: CGEventSourceStateID, key: CGKeyCode) -> bool;
23 pub fn CGEventSourceFlagsState(stateID: CGEventSourceStateID) -> CGEventFlags;
24 pub fn CGEventSourceSecondsSinceLastEventType(stateID: CGEventSourceStateID, eventType: CGEventType) -> CFTimeInterval;
25 pub fn CGEventSourceCounterForEventType(stateID: CGEventSourceStateID, eventType: CGEventType) -> u32;
26 pub fn CGEventSourceSetUserData(source: CGEventSourceRef, userData: i64);
27 pub fn CGEventSourceGetUserData(source: CGEventSourceRef) -> i64;
28 pub fn CGEventSourceSetLocalEventsSuppressionInterval(source: CGEventSourceRef, seconds: CFTimeInterval);
29 pub fn CGEventSourceGetLocalEventsSuppressionInterval(source: CGEventSourceRef) -> CFTimeInterval;
30}
31
32declare_TCFType!(CGEventSource, CGEventSourceRef);
33impl_TCFType!(CGEventSource, CGEventSourceRef, CGEventSourceGetTypeID);
34impl_CFTypeDescription!(CGEventSource);
35
36impl CGEventSource {
37 pub fn new(state_id: CGEventSourceStateID) -> Result<Self, ()> {
38 unsafe {
39 let event_source = CGEventSourceCreate(state_id);
40 if event_source.is_null() {
41 Err(())
42 } else {
43 Ok(TCFType::wrap_under_create_rule(event_source))
44 }
45 }
46 }
47
48 pub fn get_keyboard_type(&self) -> CGEventSourceKeyboardType {
49 unsafe { CGEventSourceGetKeyboardType(self.as_concrete_TypeRef()) }
50 }
51
52 pub fn set_keyboard_type(&self, keyboard_type: CGEventSourceKeyboardType) {
53 unsafe { CGEventSourceSetKeyboardType(self.as_concrete_TypeRef(), keyboard_type) }
54 }
55
56 pub fn get_pixels_per_line(&self) -> c_double {
57 unsafe { CGEventSourceGetPixelsPerLine(self.as_concrete_TypeRef()) }
58 }
59
60 pub fn set_pixels_per_line(&self, pixels_per_line: c_double) {
61 unsafe { CGEventSourceSetPixelsPerLine(self.as_concrete_TypeRef(), pixels_per_line) }
62 }
63
64 pub fn get_source_state_id(&self) -> CGEventSourceStateID {
65 unsafe { CGEventSourceGetSourceStateID(self.as_concrete_TypeRef()) }
66 }
67
68 pub fn button_state(state_id: CGEventSourceStateID, button: CGMouseButton) -> bool {
69 unsafe { CGEventSourceButtonState(state_id, button) }
70 }
71
72 pub fn key_state(state_id: CGEventSourceStateID, key: CGKeyCode) -> bool {
73 unsafe { CGEventSourceKeyState(state_id, key) }
74 }
75
76 pub fn flags_state(state_id: CGEventSourceStateID) -> CGEventFlags {
77 unsafe { CGEventSourceFlagsState(state_id) }
78 }
79
80 pub fn seconds_since_last_event_type(state_id: CGEventSourceStateID, event_type: CGEventType) -> CFTimeInterval {
81 unsafe { CGEventSourceSecondsSinceLastEventType(state_id, event_type) }
82 }
83
84 pub fn counter_for_event_type(state_id: CGEventSourceStateID, event_type: CGEventType) -> u32 {
85 unsafe { CGEventSourceCounterForEventType(state_id, event_type) }
86 }
87
88 pub fn set_user_data(&self, user_data: i64) {
89 unsafe { CGEventSourceSetUserData(self.as_concrete_TypeRef(), user_data) }
90 }
91
92 pub fn get_user_data(&self) -> i64 {
93 unsafe { CGEventSourceGetUserData(self.as_concrete_TypeRef()) }
94 }
95
96 pub fn set_local_events_suppression_interval(&self, seconds: CFTimeInterval) {
97 unsafe { CGEventSourceSetLocalEventsSuppressionInterval(self.as_concrete_TypeRef(), seconds) }
98 }
99
100 pub fn get_local_events_suppression_interval(&self) -> CFTimeInterval {
101 unsafe { CGEventSourceGetLocalEventsSuppressionInterval(self.as_concrete_TypeRef()) }
102 }
103}