1 ! Copyright (C) 2006, 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.strings alien.syntax kernel
4 math sequences io.encodings.utf16 destructors accessors ;
7 TYPEDEF: void* CFAllocatorRef
8 TYPEDEF: void* CFArrayRef
9 TYPEDEF: void* CFDataRef
10 TYPEDEF: void* CFDictionaryRef
11 TYPEDEF: void* CFMutableDictionaryRef
12 TYPEDEF: void* CFNumberRef
13 TYPEDEF: void* CFBundleRef
14 TYPEDEF: void* CFSetRef
15 TYPEDEF: void* CFStringRef
16 TYPEDEF: void* CFURLRef
17 TYPEDEF: void* CFUUIDRef
18 TYPEDEF: void* CFTypeRef
23 TYPEDEF: uint CFTypeID
24 TYPEDEF: double CFTimeInterval
25 TYPEDEF: double CFAbsoluteTime
27 TYPEDEF: int CFNumberType
28 : kCFNumberSInt8Type 1 ; inline
29 : kCFNumberSInt16Type 2 ; inline
30 : kCFNumberSInt32Type 3 ; inline
31 : kCFNumberSInt64Type 4 ; inline
32 : kCFNumberFloat32Type 5 ; inline
33 : kCFNumberFloat64Type 6 ; inline
34 : kCFNumberCharType 7 ; inline
35 : kCFNumberShortType 8 ; inline
36 : kCFNumberIntType 9 ; inline
37 : kCFNumberLongType 10 ; inline
38 : kCFNumberLongLongType 11 ; inline
39 : kCFNumberFloatType 12 ; inline
40 : kCFNumberDoubleType 13 ; inline
41 : kCFNumberCFIndexType 14 ; inline
42 : kCFNumberNSIntegerType 15 ; inline
43 : kCFNumberCGFloatType 16 ; inline
44 : kCFNumberMaxType 16 ; inline
46 TYPEDEF: int CFPropertyListMutabilityOptions
47 : kCFPropertyListImmutable 0 ; inline
48 : kCFPropertyListMutableContainers 1 ; inline
49 : kCFPropertyListMutableContainersAndLeaves 2 ; inline
51 FUNCTION: CFArrayRef CFArrayCreateMutable ( CFAllocatorRef allocator, CFIndex capacity, void* callbacks ) ;
53 FUNCTION: void* CFArrayGetValueAtIndex ( CFArrayRef array, CFIndex idx ) ;
55 FUNCTION: void CFArraySetValueAtIndex ( CFArrayRef array, CFIndex index, void* value ) ;
57 FUNCTION: CFIndex CFArrayGetCount ( CFArrayRef array ) ;
59 : kCFURLPOSIXPathStyle 0 ; inline
60 : kCFAllocatorDefault f ; inline
62 FUNCTION: CFURLRef CFURLCreateWithFileSystemPath ( CFAllocatorRef allocator, CFStringRef filePath, int pathStyle, Boolean isDirectory ) ;
64 FUNCTION: CFURLRef CFURLCreateWithString ( CFAllocatorRef allocator, CFStringRef string, CFURLRef base ) ;
66 FUNCTION: CFURLRef CFURLCopyFileSystemPath ( CFURLRef url, int pathStyle ) ;
68 FUNCTION: CFStringRef CFStringCreateWithCharacters ( CFAllocatorRef allocator, wchar_t* cStr, CFIndex numChars ) ;
70 FUNCTION: CFIndex CFStringGetLength ( CFStringRef theString ) ;
72 FUNCTION: void CFStringGetCharacters ( void* theString, CFIndex start, CFIndex length, void* buffer ) ;
74 FUNCTION: CFNumberRef CFNumberCreate ( CFAllocatorRef allocator, CFNumberType theType, void* valuePtr ) ;
76 FUNCTION: CFDataRef CFDataCreate ( CFAllocatorRef allocator, uchar* bytes, CFIndex length ) ;
78 FUNCTION: CFBundleRef CFBundleCreate ( CFAllocatorRef allocator, CFURLRef bundleURL ) ;
80 FUNCTION: Boolean CFBundleLoadExecutable ( CFBundleRef bundle ) ;
82 FUNCTION: CFTypeRef CFRetain ( CFTypeRef cf ) ;
83 FUNCTION: void CFRelease ( CFTypeRef cf ) ;
85 FUNCTION: CFTypeID CFGetTypeID ( CFTypeRef cf ) ;
87 : CF>array ( alien -- array )
88 dup CFArrayGetCount [ CFArrayGetValueAtIndex ] with map ;
90 : <CFArray> ( seq -- alien )
91 [ f swap length f CFArrayCreateMutable ] keep
93 [ >r dupd r> CFArraySetValueAtIndex ] 2each ;
95 : <CFString> ( string -- alien )
96 f swap dup length CFStringCreateWithCharacters ;
98 : CF>string ( alien -- string )
99 dup CFStringGetLength 1+ "ushort" <c-array> [
100 >r 0 over CFStringGetLength r> CFStringGetCharacters
101 ] keep utf16n alien>string ;
103 : CF>string-array ( alien -- seq )
104 CF>array [ CF>string ] map ;
106 : <CFStringArray> ( seq -- alien )
107 [ <CFString> ] map dup <CFArray> swap [ CFRelease ] each ;
109 : <CFFileSystemURL> ( string dir? -- url )
110 >r <CFString> f over kCFURLPOSIXPathStyle
111 r> CFURLCreateWithFileSystemPath swap CFRelease ;
113 : <CFURL> ( string -- url )
115 [ f swap f CFURLCreateWithString ] keep
118 : <CFBundle> ( string -- bundle )
119 t <CFFileSystemURL> [
120 f swap CFBundleCreate
123 GENERIC: <CFNumber> ( number -- alien )
124 M: integer <CFNumber>
125 [ f kCFNumberLongLongType ] dip <longlong> CFNumberCreate ;
127 [ f kCFNumberDoubleType ] dip <double> CFNumberCreate ;
129 drop f kCFNumberIntType 1 <int> CFNumberCreate ;
131 drop f kCFNumberIntType 0 <int> CFNumberCreate ;
133 : <CFData> ( byte-array -- alien )
134 [ f ] dip dup length CFDataCreate ;
136 : load-framework ( name -- )
138 CFBundleLoadExecutable drop
140 "Cannot load bundled named " prepend throw
143 TUPLE: CFRelease-destructor alien disposed ;
144 M: CFRelease-destructor dispose* alien>> CFRelease ;
145 : &CFRelease ( alien -- alien )
146 dup f CFRelease-destructor boa &dispose drop ; inline
147 : |CFRelease ( alien -- alien )
148 dup f CFRelease-destructor boa |dispose drop ; inline