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 
10 use std::cmp::Ordering;
11 use std::os::raw::{c_uint, c_void, c_int};
12 use string::CFStringRef;
13 
14 pub type Boolean = u8;
15 pub type mach_port_t = c_uint;
16 pub type CFAllocatorRef = *const c_void;
17 pub type CFNullRef = *const c_void;
18 pub type CFTypeRef = *const c_void;
19 pub type OSStatus = i32;
20 pub type SInt32 = c_int;
21 pub type CFTypeID = usize;
22 pub type CFOptionFlags = usize;
23 pub type CFHashCode = usize;
24 pub type CFIndex = isize;
25 
26 #[repr(isize)]
27 #[derive(Clone, Copy, Debug, PartialEq)]
28 pub enum CFComparisonResult {
29     LessThan = -1,
30     EqualTo = 0,
31     GreaterThan = 1,
32 }
33 
34 impl Into<Ordering> for CFComparisonResult {
into(self) -> Ordering35     fn into(self) -> Ordering {
36         match self {
37             CFComparisonResult::LessThan => Ordering::Less,
38             CFComparisonResult::EqualTo => Ordering::Equal,
39             CFComparisonResult::GreaterThan => Ordering::Greater
40         }
41     }
42 }
43 
44 #[repr(C)]
45 #[derive(Clone, Copy)]
46 pub struct CFRange {
47     pub location: CFIndex,
48     pub length: CFIndex
49 }
50 
51 // for back-compat
52 impl CFRange {
init(location: CFIndex, length: CFIndex) -> CFRange53     pub fn init(location: CFIndex, length: CFIndex) -> CFRange {
54         CFRange {
55             location: location,
56             length: length,
57         }
58     }
59 }
60 
61 pub type CFAllocatorRetainCallBack = extern "C" fn(info: *mut c_void) -> *mut c_void;
62 pub type CFAllocatorReleaseCallBack = extern "C" fn(info: *mut c_void);
63 pub type CFAllocatorCopyDescriptionCallBack = extern "C" fn(info: *mut c_void) -> CFStringRef;
64 pub type CFAllocatorAllocateCallBack = extern "C" fn(allocSize: CFIndex, hint: CFOptionFlags, info: *mut c_void) -> *mut c_void;
65 pub type CFAllocatorReallocateCallBack = extern "C" fn(ptr: *mut c_void, newsize: CFIndex, hint: CFOptionFlags, info: *mut c_void) -> *mut c_void;
66 pub type CFAllocatorDeallocateCallBack = extern "C" fn(ptr: *mut c_void, info: *mut c_void);
67 pub type CFAllocatorPreferredSizeCallBack = extern "C" fn(size: CFIndex, hint: CFOptionFlags, info: *mut c_void) -> CFIndex;
68 
69 #[repr(C)]
70 #[derive(Clone, Copy)]
71 pub struct CFAllocatorContext {
72     pub version: CFIndex,
73     pub info: *mut c_void,
74     pub retain: CFAllocatorRetainCallBack,
75     pub release: CFAllocatorReleaseCallBack,
76     pub copyDescription: CFAllocatorCopyDescriptionCallBack,
77     pub allocate: CFAllocatorAllocateCallBack,
78     pub reallocate: CFAllocatorReallocateCallBack,
79     pub deallocate: CFAllocatorDeallocateCallBack,
80     pub preferredSize: CFAllocatorPreferredSizeCallBack
81 }
82 
83 /// Trait for all types which are Core Foundation reference types.
84 pub trait TCFTypeRef {
as_void_ptr(&self) -> *const c_void85     fn as_void_ptr(&self) -> *const c_void;
86 
from_void_ptr(ptr: *const c_void) -> Self87     unsafe fn from_void_ptr(ptr: *const c_void) -> Self;
88 }
89 
90 impl<T> TCFTypeRef for *const T {
as_void_ptr(&self) -> *const c_void91     fn as_void_ptr(&self) -> *const c_void {
92         (*self) as *const c_void
93     }
94 
from_void_ptr(ptr: *const c_void) -> Self95     unsafe fn from_void_ptr(ptr: *const c_void) -> Self {
96         ptr as *const T
97     }
98 }
99 
100 impl<T> TCFTypeRef for *mut T {
as_void_ptr(&self) -> *const c_void101     fn as_void_ptr(&self) -> *const c_void {
102         (*self) as *const T as *const c_void
103     }
104 
from_void_ptr(ptr: *const c_void) -> Self105     unsafe fn from_void_ptr(ptr: *const c_void) -> Self {
106         ptr as *const T as *mut T
107     }
108 }
109 
110 extern {
111     /*
112      * CFBase.h
113      */
114 
115     /* CFAllocator Reference */
116 
117     pub static kCFAllocatorDefault: CFAllocatorRef;
118     pub static kCFAllocatorSystemDefault: CFAllocatorRef;
119     pub static kCFAllocatorMalloc: CFAllocatorRef;
120     pub static kCFAllocatorMallocZone: CFAllocatorRef;
121     pub static kCFAllocatorNull: CFAllocatorRef;
122     pub static kCFAllocatorUseContext: CFAllocatorRef;
123 
CFAllocatorCreate(allocator: CFAllocatorRef, context: *mut CFAllocatorContext) -> CFAllocatorRef124     pub fn CFAllocatorCreate(allocator: CFAllocatorRef, context: *mut CFAllocatorContext) -> CFAllocatorRef;
CFAllocatorAllocate(allocator: CFAllocatorRef, size: CFIndex, hint: CFOptionFlags) -> *mut c_void125     pub fn CFAllocatorAllocate(allocator: CFAllocatorRef, size: CFIndex, hint: CFOptionFlags) -> *mut c_void;
CFAllocatorDeallocate(allocator: CFAllocatorRef, ptr: *mut c_void)126     pub fn CFAllocatorDeallocate(allocator: CFAllocatorRef, ptr: *mut c_void);
CFAllocatorGetPreferredSizeForSize(allocator: CFAllocatorRef, size: CFIndex, hint: CFOptionFlags) -> CFIndex127     pub fn CFAllocatorGetPreferredSizeForSize(allocator: CFAllocatorRef, size: CFIndex, hint: CFOptionFlags) -> CFIndex;
CFAllocatorReallocate(allocator: CFAllocatorRef, ptr: *mut c_void, newsize: CFIndex, hint: CFOptionFlags) -> *mut c_void128     pub fn CFAllocatorReallocate(allocator: CFAllocatorRef, ptr: *mut c_void, newsize: CFIndex, hint: CFOptionFlags) -> *mut c_void;
CFAllocatorGetDefault() -> CFAllocatorRef129     pub fn CFAllocatorGetDefault() -> CFAllocatorRef;
CFAllocatorSetDefault(allocator: CFAllocatorRef)130     pub fn CFAllocatorSetDefault(allocator: CFAllocatorRef);
CFAllocatorGetContext(allocator: CFAllocatorRef, context: *mut CFAllocatorContext)131     pub fn CFAllocatorGetContext(allocator: CFAllocatorRef, context: *mut CFAllocatorContext);
CFAllocatorGetTypeID() -> CFTypeID132     pub fn CFAllocatorGetTypeID() -> CFTypeID;
133 
134     /* CFNull Reference */
135 
136     pub static kCFNull: CFNullRef;
137 
138     /* CFType Reference */
139 
140     //fn CFCopyTypeIDDescription
141     //fn CFGetAllocator
CFCopyDescription(cf: CFTypeRef) -> CFStringRef142     pub fn CFCopyDescription(cf: CFTypeRef) -> CFStringRef;
CFEqual(cf1: CFTypeRef, cf2: CFTypeRef) -> Boolean143     pub fn CFEqual(cf1: CFTypeRef, cf2: CFTypeRef) -> Boolean;
CFGetRetainCount(cf: CFTypeRef) -> CFIndex144     pub fn CFGetRetainCount(cf: CFTypeRef) -> CFIndex;
CFGetTypeID(cf: CFTypeRef) -> CFTypeID145     pub fn CFGetTypeID(cf: CFTypeRef) -> CFTypeID;
CFHash(cf: CFTypeRef) -> CFHashCode146     pub fn CFHash(cf: CFTypeRef) -> CFHashCode;
147     //fn CFMakeCollectable
CFRelease(cf: CFTypeRef)148     pub fn CFRelease(cf: CFTypeRef);
CFRetain(cf: CFTypeRef) -> CFTypeRef149     pub fn CFRetain(cf: CFTypeRef) -> CFTypeRef;
CFShow(obj: CFTypeRef)150     pub fn CFShow(obj: CFTypeRef);
151 
152     /* Base Utilities Reference */
153     // N.B. Some things missing here.
154 }
155