core_foundation_sys/
runloop.rs

1// Copyright 2013-2015 The Servo Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10use std::os::raw::c_void;
11
12use crate::array::CFArrayRef;
13use crate::base::{
14    mach_port_t, Boolean, CFAllocatorRef, CFHashCode, CFIndex, CFOptionFlags, CFTypeID,
15};
16use crate::date::{CFAbsoluteTime, CFTimeInterval};
17use crate::string::CFStringRef;
18
19#[repr(C)]
20pub struct __CFRunLoop(c_void);
21
22pub type CFRunLoopRef = *mut __CFRunLoop;
23
24#[repr(C)]
25pub struct __CFRunLoopSource(c_void);
26
27pub type CFRunLoopSourceRef = *mut __CFRunLoopSource;
28
29#[repr(C)]
30pub struct __CFRunLoopObserver(c_void);
31
32pub type CFRunLoopObserverRef = *mut __CFRunLoopObserver;
33
34// Reasons for CFRunLoopRunInMode() to Return
35pub const kCFRunLoopRunFinished: i32 = 1;
36pub const kCFRunLoopRunStopped: i32 = 2;
37pub const kCFRunLoopRunTimedOut: i32 = 3;
38pub const kCFRunLoopRunHandledSource: i32 = 4;
39
40// Run Loop Observer Activities
41//typedef CF_OPTIONS(CFOptionFlags, CFRunLoopActivity) {
42pub type CFRunLoopActivity = CFOptionFlags;
43pub const kCFRunLoopEntry: CFOptionFlags = 1 << 0;
44pub const kCFRunLoopBeforeTimers: CFOptionFlags = 1 << 1;
45pub const kCFRunLoopBeforeSources: CFOptionFlags = 1 << 2;
46pub const kCFRunLoopBeforeWaiting: CFOptionFlags = 1 << 5;
47pub const kCFRunLoopAfterWaiting: CFOptionFlags = 1 << 6;
48pub const kCFRunLoopExit: CFOptionFlags = 1 << 7;
49pub const kCFRunLoopAllActivities: CFOptionFlags = 0x0FFFFFFF;
50
51#[repr(C)]
52#[derive(Debug, Clone, Copy)]
53pub struct CFRunLoopSourceContext {
54    pub version: CFIndex,
55    pub info: *mut c_void,
56    pub retain: Option<extern "C" fn(info: *const c_void) -> *const c_void>,
57    pub release: Option<extern "C" fn(info: *const c_void)>,
58    pub copyDescription: Option<extern "C" fn(info: *const c_void) -> CFStringRef>,
59    pub equal: Option<extern "C" fn(info1: *const c_void, info2: *const c_void) -> Boolean>,
60    pub hash: Option<extern "C" fn(info: *const c_void) -> CFHashCode>,
61    pub schedule: Option<extern "C" fn(info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef)>,
62    pub cancel: Option<extern "C" fn(info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef)>,
63    pub perform: extern "C" fn(info: *const c_void),
64}
65
66#[repr(C)]
67#[derive(Debug, Clone, Copy)]
68pub struct CFRunLoopSourceContext1 {
69    pub version: CFIndex,
70    pub info: *mut c_void,
71    pub retain: Option<extern "C" fn(info: *const c_void) -> *const c_void>,
72    pub release: Option<extern "C" fn(info: *const c_void)>,
73    pub copyDescription: Option<extern "C" fn(info: *const c_void) -> CFStringRef>,
74    pub equal: Option<extern "C" fn(info1: *const c_void, info2: *const c_void) -> Boolean>,
75    pub hash: Option<extern "C" fn(info: *const c_void) -> CFHashCode>,
76
77    #[cfg(any(target_os = "macos", target_os = "ios"))]
78    pub getPort: extern "C" fn(info: *mut c_void) -> mach_port_t,
79    #[cfg(any(target_os = "macos", target_os = "ios"))]
80    pub perform: extern "C" fn(
81        msg: *mut c_void,
82        size: CFIndex,
83        allocator: CFAllocatorRef,
84        info: *mut c_void,
85    ) -> *mut c_void,
86
87    #[cfg(not(any(target_os = "macos", target_os = "ios")))]
88    pub getPort: extern "C" fn(info: *mut c_void) -> *mut c_void,
89    #[cfg(not(any(target_os = "macos", target_os = "ios")))]
90    pub perform: extern "C" fn(info: *mut c_void) -> *mut c_void,
91}
92
93#[repr(C)]
94#[derive(Debug, Clone, Copy)]
95pub struct CFRunLoopObserverContext {
96    pub version: CFIndex,
97    pub info: *mut c_void,
98    pub retain: Option<extern "C" fn(info: *const c_void) -> *const c_void>,
99    pub release: Option<extern "C" fn(info: *const c_void)>,
100    pub copyDescription: Option<extern "C" fn(info: *const c_void) -> CFStringRef>,
101}
102
103pub type CFRunLoopObserverCallBack =
104    extern "C" fn(observer: CFRunLoopObserverRef, activity: CFRunLoopActivity, info: *mut c_void);
105
106#[repr(C)]
107#[derive(Clone, Copy, Debug)]
108pub struct CFRunLoopTimerContext {
109    pub version: CFIndex,
110    pub info: *mut c_void,
111    pub retain: Option<extern "C" fn(info: *const c_void) -> *const c_void>,
112    pub release: Option<extern "C" fn(info: *const c_void)>,
113    pub copyDescription: Option<extern "C" fn(info: *const c_void) -> CFStringRef>,
114}
115
116pub type CFRunLoopTimerCallBack = extern "C" fn(timer: CFRunLoopTimerRef, info: *mut c_void);
117
118#[repr(C)]
119pub struct __CFRunLoopTimer(c_void);
120
121pub type CFRunLoopTimerRef = *mut __CFRunLoopTimer;
122
123extern "C" {
124    /*
125     * CFRunLoop.h
126     */
127
128    pub static kCFRunLoopDefaultMode: CFStringRef;
129    pub static kCFRunLoopCommonModes: CFStringRef;
130
131    /* CFRunLoop */
132    /* Getting a Run Loop */
133    pub fn CFRunLoopGetCurrent() -> CFRunLoopRef;
134    pub fn CFRunLoopGetMain() -> CFRunLoopRef;
135
136    /* Starting and Stopping a Run Loop */
137    pub fn CFRunLoopRun();
138    pub fn CFRunLoopRunInMode(
139        mode: CFStringRef,
140        seconds: CFTimeInterval,
141        returnAfterSourceHandled: Boolean,
142    ) -> i32;
143    pub fn CFRunLoopWakeUp(rl: CFRunLoopRef);
144    pub fn CFRunLoopStop(rl: CFRunLoopRef);
145    pub fn CFRunLoopIsWaiting(rl: CFRunLoopRef) -> Boolean;
146
147    /* Managing Sources */
148    pub fn CFRunLoopAddSource(rl: CFRunLoopRef, source: CFRunLoopSourceRef, mode: CFStringRef);
149    pub fn CFRunLoopContainsSource(
150        rl: CFRunLoopRef,
151        source: CFRunLoopSourceRef,
152        mode: CFStringRef,
153    ) -> Boolean;
154    pub fn CFRunLoopRemoveSource(rl: CFRunLoopRef, source: CFRunLoopSourceRef, mode: CFStringRef);
155
156    /* Managing Observers */
157    pub fn CFRunLoopAddObserver(
158        rl: CFRunLoopRef,
159        observer: CFRunLoopObserverRef,
160        mode: CFStringRef,
161    );
162    pub fn CFRunLoopContainsObserver(
163        rl: CFRunLoopRef,
164        observer: CFRunLoopObserverRef,
165        mode: CFStringRef,
166    ) -> Boolean;
167    pub fn CFRunLoopRemoveObserver(
168        rl: CFRunLoopRef,
169        observer: CFRunLoopObserverRef,
170        mode: CFStringRef,
171    );
172
173    /* Managing Run Loop Modes */
174    pub fn CFRunLoopAddCommonMode(rl: CFRunLoopRef, mode: CFStringRef);
175    pub fn CFRunLoopCopyAllModes(rl: CFRunLoopRef) -> CFArrayRef;
176    pub fn CFRunLoopCopyCurrentMode(rl: CFRunLoopRef) -> CFStringRef;
177
178    /* Managing Timers */
179    pub fn CFRunLoopAddTimer(rl: CFRunLoopRef, timer: CFRunLoopTimerRef, mode: CFStringRef);
180    pub fn CFRunLoopGetNextTimerFireDate(rl: CFRunLoopRef, mode: CFStringRef) -> CFAbsoluteTime;
181    pub fn CFRunLoopRemoveTimer(rl: CFRunLoopRef, timer: CFRunLoopTimerRef, mode: CFStringRef);
182    pub fn CFRunLoopContainsTimer(
183        rl: CFRunLoopRef,
184        timer: CFRunLoopTimerRef,
185        mode: CFStringRef,
186    ) -> Boolean;
187
188    /* Scheduling Blocks */
189    // fn CFRunLoopPerformBlock(rl: CFRunLoopRef, mode: CFTypeRef, block: void (^)(void));
190
191    /* Getting the CFRunLoop Type ID */
192    pub fn CFRunLoopGetTypeID() -> CFTypeID;
193
194    /* CFRunLoopSource */
195    /* CFRunLoopSource Miscellaneous Functions */
196    pub fn CFRunLoopSourceCreate(
197        allocator: CFAllocatorRef,
198        order: CFIndex,
199        context: *mut CFRunLoopSourceContext,
200    ) -> CFRunLoopSourceRef;
201    pub fn CFRunLoopSourceGetContext(
202        source: CFRunLoopSourceRef,
203        context: *mut CFRunLoopSourceContext,
204    );
205    pub fn CFRunLoopSourceGetOrder(source: CFRunLoopSourceRef) -> CFIndex;
206    pub fn CFRunLoopSourceGetTypeID() -> CFTypeID;
207    pub fn CFRunLoopSourceInvalidate(source: CFRunLoopSourceRef);
208    pub fn CFRunLoopSourceIsValid(source: CFRunLoopSourceRef) -> Boolean;
209    pub fn CFRunLoopSourceSignal(source: CFRunLoopSourceRef);
210
211    /* CFRunLoopObserver */
212    /* CFRunLoopObserver Miscellaneous Functions */
213    // fn CFRunLoopObserverCreateWithHandler(allocator: CFAllocatorRef, activities: CFOptionFlags, repeats: Boolean, order: CFIndex, block: void (^) (CFRunLoopObserverRef observer, CFRunLoopActivity activity)) -> CFRunLoopObserverRef;
214    pub fn CFRunLoopObserverCreate(
215        allocator: CFAllocatorRef,
216        activities: CFOptionFlags,
217        repeats: Boolean,
218        order: CFIndex,
219        callout: CFRunLoopObserverCallBack,
220        context: *mut CFRunLoopObserverContext,
221    ) -> CFRunLoopObserverRef;
222    pub fn CFRunLoopObserverDoesRepeat(observer: CFRunLoopObserverRef) -> Boolean;
223    pub fn CFRunLoopObserverGetActivities(observer: CFRunLoopObserverRef) -> CFOptionFlags;
224    pub fn CFRunLoopObserverGetContext(
225        observer: CFRunLoopObserverRef,
226        context: *mut CFRunLoopObserverContext,
227    );
228    pub fn CFRunLoopObserverGetOrder(observer: CFRunLoopObserverRef) -> CFIndex;
229    pub fn CFRunLoopObserverGetTypeID() -> CFTypeID;
230    pub fn CFRunLoopObserverInvalidate(observer: CFRunLoopObserverRef);
231    pub fn CFRunLoopObserverIsValid(observer: CFRunLoopObserverRef) -> Boolean;
232
233    /* CFRunLoopTimer */
234    /* CFRunLoopTimer Miscellaneous Functions */
235    // fn CFRunLoopTimerCreateWithHandler(allocator: CFAllocatorRef, fireDate: CFAbsoluteTime, interval: CFTimeInterval, flags: CFOptionFlags, order: CFIndex, block: void (^) (CFRunLoopTimerRef timer)) -> CFRunLoopTimerRef;
236    pub fn CFRunLoopTimerCreate(
237        allocator: CFAllocatorRef,
238        fireDate: CFAbsoluteTime,
239        interval: CFTimeInterval,
240        flags: CFOptionFlags,
241        order: CFIndex,
242        callout: CFRunLoopTimerCallBack,
243        context: *mut CFRunLoopTimerContext,
244    ) -> CFRunLoopTimerRef;
245    pub fn CFRunLoopTimerDoesRepeat(timer: CFRunLoopTimerRef) -> Boolean;
246    pub fn CFRunLoopTimerGetContext(timer: CFRunLoopTimerRef, context: *mut CFRunLoopTimerContext);
247    pub fn CFRunLoopTimerGetInterval(timer: CFRunLoopTimerRef) -> CFTimeInterval;
248    pub fn CFRunLoopTimerGetNextFireDate(timer: CFRunLoopTimerRef) -> CFAbsoluteTime;
249    pub fn CFRunLoopTimerGetOrder(timer: CFRunLoopTimerRef) -> CFIndex;
250    pub fn CFRunLoopTimerGetTypeID() -> CFTypeID;
251    pub fn CFRunLoopTimerInvalidate(timer: CFRunLoopTimerRef);
252    pub fn CFRunLoopTimerIsValid(timer: CFRunLoopTimerRef) -> Boolean;
253    pub fn CFRunLoopTimerSetNextFireDate(timer: CFRunLoopTimerRef, fireDate: CFAbsoluteTime);
254    pub fn CFRunLoopTimerGetTolerance(timer: CFRunLoopTimerRef) -> CFTimeInterval; //macos(10.9)+
255    pub fn CFRunLoopTimerSetTolerance(timer: CFRunLoopTimerRef, tolerance: CFTimeInterval); //macos(10.9)+
256}