| 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 crate::string::CFStringRef; |
| 11 |
use std::cmp::Ordering; |
| 12 |
use std::os::raw::{c_int, c_short, c_uchar, c_uint, c_ushort, c_void}; |
| 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 ConstStr255Param = *const c_uchar; |
| 20 |
pub type StringPtr = *mut c_uchar; |
| 21 |
pub type ConstStringPtr = *const c_uchar; |
| 22 |
pub type OSStatus = i32; |
| 23 |
pub type UInt8 = c_uchar; |
| 24 |
pub type UInt16 = c_ushort; |
| 25 |
pub type SInt16 = c_short; |
| 26 |
pub type SInt32 = c_int; |
| 27 |
pub type UInt32 = c_uint; |
| 28 |
pub type CFTypeID = usize; |
| 29 |
pub type CFOptionFlags = usize; |
| 30 |
pub type CFHashCode = usize; |
| 31 |
pub type CFIndex = isize; |
| 32 |
pub type LangCode = SInt16; |
| 33 |
pub type RegionCode = SInt16; |
| 34 |
pub type UTF32Char = c_uint; |
| 35 |
pub type UTF16Char = c_ushort; |
| 36 |
pub type UTF8Char = c_uchar; |
| 37 |
|
| 38 |
#[repr(isize)] |
| 39 |
#[derive(Clone, Copy, Debug, PartialEq)] |
| 40 |
pub enum CFComparisonResult { |
| 41 |
LessThan = -1, |
| 42 |
EqualTo = 0, |
| 43 |
GreaterThan = 1, |
| 44 |
} |
| 45 |
|
| 46 |
pub type CFComparatorFunction = extern "C" fn( |
| 47 |
val1: *const c_void, |
| 48 |
val2: *const c_void, |
| 49 |
context: *mut c_void, |
| 50 |
) -> CFComparisonResult; |
| 51 |
|
| 52 |
impl From<CFComparisonResult> for Ordering { |
| 53 |
fn from(val: CFComparisonResult) -> Self { |
| 54 |
match val { |
| 55 |
CFComparisonResult::LessThan => Ordering::Less, |
| 56 |
CFComparisonResult::EqualTo => Ordering::Equal, |
| 57 |
CFComparisonResult::GreaterThan => Ordering::Greater, |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
#[repr(C)] |
| 63 |
#[derive(Clone, Copy, Debug, PartialEq)] |
| 64 |
pub struct CFRange { |
| 65 |
pub location: CFIndex, |
| 66 |
pub length: CFIndex, |
| 67 |
} |
| 68 |
|
| 69 |
// for back-compat |
| 70 |
impl CFRange { |
| 71 |
pub fn init(location: CFIndex, length: CFIndex) -> CFRange { |
| 72 |
CFRange { location, length } |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
pub type CFAllocatorRetainCallBack = extern "C" fn(info: *mut c_void) -> *mut c_void; |
| 77 |
pub type CFAllocatorReleaseCallBack = extern "C" fn(info: *mut c_void); |
| 78 |
pub type CFAllocatorCopyDescriptionCallBack = extern "C" fn(info: *mut c_void) -> CFStringRef; |
| 79 |
pub type CFAllocatorAllocateCallBack = |
| 80 |
extern "C" fn(allocSize: CFIndex, hint: CFOptionFlags, info: *mut c_void) -> *mut c_void; |
| 81 |
pub type CFAllocatorReallocateCallBack = extern "C" fn( |
| 82 |
ptr: *mut c_void, |
| 83 |
newsize: CFIndex, |
| 84 |
hint: CFOptionFlags, |
| 85 |
info: *mut c_void, |
| 86 |
) -> *mut c_void; |
| 87 |
pub type CFAllocatorDeallocateCallBack = extern "C" fn(ptr: *mut c_void, info: *mut c_void); |
| 88 |
pub type CFAllocatorPreferredSizeCallBack = |
| 89 |
extern "C" fn(size: CFIndex, hint: CFOptionFlags, info: *mut c_void) -> CFIndex; |
| 90 |
|
| 91 |
#[repr(C)] |
| 92 |
#[derive(Clone, Copy, Debug)] |
| 93 |
pub struct CFAllocatorContext { |
| 94 |
pub version: CFIndex, |
| 95 |
pub info: *mut c_void, |
| 96 |
pub retain: Option<CFAllocatorRetainCallBack>, |
| 97 |
pub release: Option<CFAllocatorReleaseCallBack>, |
| 98 |
pub copyDescription: Option<CFAllocatorCopyDescriptionCallBack>, |
| 99 |
pub allocate: Option<CFAllocatorAllocateCallBack>, |
| 100 |
pub reallocate: Option<CFAllocatorReallocateCallBack>, |
| 101 |
pub deallocate: Option<CFAllocatorDeallocateCallBack>, |
| 102 |
pub preferredSize: Option<CFAllocatorPreferredSizeCallBack>, |
| 103 |
} |
| 104 |
|
| 105 |
/// Trait for all types which are Core Foundation reference types. |
| 106 |
pub trait TCFTypeRef { |
| 107 |
fn as_void_ptr(&self) -> *const c_void; |
| 108 |
|
| 109 |
unsafe fn from_void_ptr(ptr: *const c_void) -> Self; |
| 110 |
} |
| 111 |
|
| 112 |
impl<T> TCFTypeRef for *const T { |
| 113 |
fn as_void_ptr(&self) -> *const c_void { |
| 114 |
(*self) as *const c_void |
| 115 |
} |
| 116 |
|
| 117 |
unsafe fn from_void_ptr(ptr: *const c_void) -> Self { |
| 118 |
ptr as *const T |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
impl<T> TCFTypeRef for *mut T { |
| 123 |
fn as_void_ptr(&self) -> *const c_void { |
| 124 |
(*self) as *const T as *const c_void |
| 125 |
} |
| 126 |
|
| 127 |
unsafe fn from_void_ptr(ptr: *const c_void) -> Self { |
| 128 |
ptr as *const T as *mut T |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/// Constant used by some functions to indicate failed searches. |
| 133 |
pub static kCFNotFound: CFIndex = -1; |
| 134 |
|
| 135 |
extern "C" { |
| 136 |
/* |
| 137 |
* CFBase.h |
| 138 |
*/ |
| 139 |
|
| 140 |
/* CFAllocator Reference */ |
| 141 |
|
| 142 |
pub static kCFAllocatorDefault: CFAllocatorRef; |
| 143 |
pub static kCFAllocatorSystemDefault: CFAllocatorRef; |
| 144 |
pub static kCFAllocatorMalloc: CFAllocatorRef; |
| 145 |
pub static kCFAllocatorMallocZone: CFAllocatorRef; |
| 146 |
pub static kCFAllocatorNull: CFAllocatorRef; |
| 147 |
pub static kCFAllocatorUseContext: CFAllocatorRef; |
| 148 |
|
| 149 |
pub fn CFAllocatorCreate( |
| 150 |
allocator: CFAllocatorRef, |
| 151 |
context: *mut CFAllocatorContext, |
| 152 |
) -> CFAllocatorRef; |
| 153 |
pub fn CFAllocatorAllocate( |
| 154 |
allocator: CFAllocatorRef, |
| 155 |
size: CFIndex, |
| 156 |
hint: CFOptionFlags, |
| 157 |
) -> *mut c_void; |
| 158 |
pub fn CFAllocatorDeallocate(allocator: CFAllocatorRef, ptr: *mut c_void); |
| 159 |
pub fn CFAllocatorGetPreferredSizeForSize( |
| 160 |
allocator: CFAllocatorRef, |
| 161 |
size: CFIndex, |
| 162 |
hint: CFOptionFlags, |
| 163 |
) -> CFIndex; |
| 164 |
pub fn CFAllocatorReallocate( |
| 165 |
allocator: CFAllocatorRef, |
| 166 |
ptr: *mut c_void, |
| 167 |
newsize: CFIndex, |
| 168 |
hint: CFOptionFlags, |
| 169 |
) -> *mut c_void; |
| 170 |
pub fn CFAllocatorGetDefault() -> CFAllocatorRef; |
| 171 |
pub fn CFAllocatorSetDefault(allocator: CFAllocatorRef); |
| 172 |
pub fn CFAllocatorGetContext(allocator: CFAllocatorRef, context: *mut CFAllocatorContext); |
| 173 |
pub fn CFAllocatorGetTypeID() -> CFTypeID; |
| 174 |
|
| 175 |
/* CFNull Reference */ |
| 176 |
|
| 177 |
pub static kCFNull: CFNullRef; |
| 178 |
|
| 179 |
pub fn CFNullGetTypeID() -> CFTypeID; |
| 180 |
|
| 181 |
/* CFType Reference */ |
| 182 |
|
| 183 |
pub fn CFCopyTypeIDDescription(type_id: CFTypeID) -> CFStringRef; |
| 184 |
pub fn CFGetAllocator(cf: CFTypeRef) -> CFAllocatorRef; |
| 185 |
pub fn CFCopyDescription(cf: CFTypeRef) -> CFStringRef; |
| 186 |
pub fn CFEqual(cf1: CFTypeRef, cf2: CFTypeRef) -> Boolean; |
| 187 |
pub fn CFGetRetainCount(cf: CFTypeRef) -> CFIndex; |
| 188 |
pub fn CFGetTypeID(cf: CFTypeRef) -> CFTypeID; |
| 189 |
pub fn CFHash(cf: CFTypeRef) -> CFHashCode; |
| 190 |
//fn CFMakeCollectable |
| 191 |
pub fn CFRelease(cf: CFTypeRef); |
| 192 |
pub fn CFRetain(cf: CFTypeRef) -> CFTypeRef; |
| 193 |
pub fn CFShow(obj: CFTypeRef); |
| 194 |
|
| 195 |
/* Base Utilities Reference */ |
| 196 |
// N.B. Some things missing here. |
| 197 |
} |