Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add runloop source and observer functionality #130

Merged
merged 1 commit into from Nov 23, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Add runloop source and observer functionality

  • Loading branch information
faern committed Nov 23, 2017
commit 7468b455c7a50ee7909a94cb98852a66a99f37b5
@@ -18,6 +18,8 @@ use base::{TCFType};
use date::{CFAbsoluteTime, CFTimeInterval};
use string::{CFString};

pub type CFRunLoopMode = CFStringRef;

pub struct CFRunLoop(CFRunLoopRef);

impl Drop for CFRunLoop {
@@ -70,18 +72,60 @@ impl CFRunLoop {
}
}

pub fn contains_timer(&self, timer: &CFRunLoopTimer, mode: CFStringRef) -> bool {
pub fn contains_timer(&self, timer: &CFRunLoopTimer, mode: CFRunLoopMode) -> bool {
unsafe {
CFRunLoopContainsTimer(self.0, timer.0, mode) != 0
}
}

pub fn add_timer(&self, timer: &CFRunLoopTimer, mode: CFStringRef) {
pub fn add_timer(&self, timer: &CFRunLoopTimer, mode: CFRunLoopMode) {
unsafe {
CFRunLoopAddTimer(self.0, timer.0, mode);
}
}

pub fn remove_timer(&self, timer: &CFRunLoopTimer, mode: CFRunLoopMode) {
unsafe {
CFRunLoopRemoveTimer(self.0, timer.0, mode);
}
}

pub fn contains_source(&self, source: &CFRunLoopSource, mode: CFRunLoopMode) -> bool {
unsafe {
CFRunLoopContainsSource(self.0, source.0, mode) != 0
}
}

pub fn add_source(&self, source: &CFRunLoopSource, mode: CFRunLoopMode) {
unsafe {
CFRunLoopAddSource(self.0, source.0, mode);
}
}

pub fn remove_source(&self, source: &CFRunLoopSource, mode: CFRunLoopMode) {
unsafe {
CFRunLoopRemoveSource(self.0, source.0, mode);
}
}

pub fn contains_observer(&self, observer: &CFRunLoopObserver, mode: CFRunLoopMode) -> bool {
unsafe {
CFRunLoopContainsObserver(self.0, observer.0, mode) != 0
}
}

pub fn add_observer(&self, observer: &CFRunLoopObserver, mode: CFRunLoopMode) {
unsafe {
CFRunLoopAddObserver(self.0, observer.0, mode);
}
}

pub fn remove_observer(&self, observer: &CFRunLoopObserver, mode: CFRunLoopMode) {
unsafe {
CFRunLoopRemoveObserver(self.0, observer.0, mode);
}
}

}

pub struct CFRunLoopTimer(CFRunLoopTimerRef);
@@ -105,6 +149,32 @@ impl CFRunLoopTimer {
}
}


pub struct CFRunLoopSource(CFRunLoopSourceRef);

impl Drop for CFRunLoopSource {
fn drop(&mut self) {
unsafe {
CFRelease(self.as_CFTypeRef())
}
}
}

impl_TCFType!(CFRunLoopSource, CFRunLoopSourceRef, CFRunLoopSourceGetTypeID);


pub struct CFRunLoopObserver(CFRunLoopObserverRef);

impl Drop for CFRunLoopObserver {
fn drop(&mut self) {
unsafe {
CFRelease(self.as_CFTypeRef())
}
}
}

impl_TCFType!(CFRunLoopObserver, CFRunLoopObserverRef, CFRunLoopObserverGetTypeID);

#[cfg(test)]
mod test {
use super::*;
@@ -132,8 +202,8 @@ mod test {
CFRunLoop::run_current();
}

extern "C" fn timer_popped(_timer: CFRunLoopTimerRef, _info: *mut c_void) {
let previous_now_ptr: *const CFAbsoluteTime = unsafe { mem::transmute(_info) };
extern "C" fn timer_popped(_timer: CFRunLoopTimerRef, info: *mut c_void) {
let previous_now_ptr: *const CFAbsoluteTime = unsafe { mem::transmute(info) };
let previous_now = unsafe { *previous_now_ptr };
let now = CFDate::now().abs_time();
assert!(now - previous_now > 0.19 && now - previous_now < 0.21);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.