apple-cf 0.7.0

Safe Rust bindings for Apple's shared Core* frameworks (CoreFoundation, CoreMedia, CoreVideo, CoreGraphics, IOSurface, Dispatch).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#![allow(warnings)]
#![allow(clippy::all)]

use super::generated::*;
use core::ptr;

#[must_use]
#[inline]
pub const fn CFRangeMake(loc: CFIndex, len: CFIndex) -> CFRange {
    CFRange {
        location: loc,
        length: len,
    }
}

pub const CFByteOrderUnknown: CFByteOrder = 0;
pub const CFByteOrderLittleEndian: CFByteOrder = 1;
pub const CFByteOrderBigEndian: CFByteOrder = 2;

#[must_use]
#[inline]
pub const fn CFByteOrderGetCurrent() -> CFByteOrder {
    if cfg!(target_endian = "little") {
        CFByteOrderLittleEndian
    } else if cfg!(target_endian = "big") {
        CFByteOrderBigEndian
    } else {
        CFByteOrderUnknown
    }
}

#[must_use]
#[inline]
pub const fn CFSwapInt16(arg: u16) -> u16 {
    arg.swap_bytes()
}

#[must_use]
#[inline]
pub const fn CFSwapInt32(arg: u32) -> u32 {
    arg.swap_bytes()
}

#[must_use]
#[inline]
pub const fn CFSwapInt64(arg: u64) -> u64 {
    arg.swap_bytes()
}

#[must_use]
#[inline]
pub const fn CFSwapInt16BigToHost(arg: u16) -> u16 {
    if cfg!(target_endian = "big") {
        arg
    } else {
        CFSwapInt16(arg)
    }
}

#[must_use]
#[inline]
pub const fn CFSwapInt32BigToHost(arg: u32) -> u32 {
    if cfg!(target_endian = "big") {
        arg
    } else {
        CFSwapInt32(arg)
    }
}

#[must_use]
#[inline]
pub const fn CFSwapInt64BigToHost(arg: u64) -> u64 {
    if cfg!(target_endian = "big") {
        arg
    } else {
        CFSwapInt64(arg)
    }
}

#[must_use]
#[inline]
pub const fn CFSwapInt16HostToBig(arg: u16) -> u16 {
    if cfg!(target_endian = "big") {
        arg
    } else {
        CFSwapInt16(arg)
    }
}

#[must_use]
#[inline]
pub const fn CFSwapInt32HostToBig(arg: u32) -> u32 {
    if cfg!(target_endian = "big") {
        arg
    } else {
        CFSwapInt32(arg)
    }
}

#[must_use]
#[inline]
pub const fn CFSwapInt64HostToBig(arg: u64) -> u64 {
    if cfg!(target_endian = "big") {
        arg
    } else {
        CFSwapInt64(arg)
    }
}

#[must_use]
#[inline]
pub const fn CFSwapInt16LittleToHost(arg: u16) -> u16 {
    if cfg!(target_endian = "little") {
        arg
    } else {
        CFSwapInt16(arg)
    }
}

#[must_use]
#[inline]
pub const fn CFSwapInt32LittleToHost(arg: u32) -> u32 {
    if cfg!(target_endian = "little") {
        arg
    } else {
        CFSwapInt32(arg)
    }
}

#[must_use]
#[inline]
pub const fn CFSwapInt64LittleToHost(arg: u64) -> u64 {
    if cfg!(target_endian = "little") {
        arg
    } else {
        CFSwapInt64(arg)
    }
}

#[must_use]
#[inline]
pub const fn CFSwapInt16HostToLittle(arg: u16) -> u16 {
    if cfg!(target_endian = "little") {
        arg
    } else {
        CFSwapInt16(arg)
    }
}

#[must_use]
#[inline]
pub const fn CFSwapInt32HostToLittle(arg: u32) -> u32 {
    if cfg!(target_endian = "little") {
        arg
    } else {
        CFSwapInt32(arg)
    }
}

#[must_use]
#[inline]
pub const fn CFSwapInt64HostToLittle(arg: u64) -> u64 {
    if cfg!(target_endian = "little") {
        arg
    } else {
        CFSwapInt64(arg)
    }
}

#[must_use]
#[inline]
pub fn CFConvertFloat32HostToSwapped(arg: Float32) -> CFSwappedFloat32 {
    let mut bits = arg.to_bits();
    if cfg!(target_endian = "little") {
        bits = bits.swap_bytes();
    }
    CFSwappedFloat32 { v: bits }
}

#[must_use]
#[inline]
pub fn CFConvertFloat32SwappedToHost(arg: CFSwappedFloat32) -> Float32 {
    let mut bits = arg.v;
    if cfg!(target_endian = "little") {
        bits = bits.swap_bytes();
    }
    Float32::from_bits(bits)
}

#[must_use]
#[inline]
pub fn CFConvertFloat64HostToSwapped(arg: Float64) -> CFSwappedFloat64 {
    let mut bits = arg.to_bits();
    if cfg!(target_endian = "little") {
        bits = bits.swap_bytes();
    }
    CFSwappedFloat64 { v: bits }
}

#[must_use]
#[inline]
pub fn CFConvertFloat64SwappedToHost(arg: CFSwappedFloat64) -> Float64 {
    let mut bits = arg.v;
    if cfg!(target_endian = "little") {
        bits = bits.swap_bytes();
    }
    Float64::from_bits(bits)
}

#[must_use]
#[inline]
pub fn CFConvertFloatHostToSwapped(arg: f32) -> CFSwappedFloat32 {
    CFConvertFloat32HostToSwapped(arg)
}

#[must_use]
#[inline]
pub fn CFConvertFloatSwappedToHost(arg: CFSwappedFloat32) -> f32 {
    CFConvertFloat32SwappedToHost(arg)
}

#[must_use]
#[inline]
pub fn CFConvertDoubleHostToSwapped(arg: f64) -> CFSwappedFloat64 {
    CFConvertFloat64HostToSwapped(arg)
}

#[must_use]
#[inline]
pub fn CFConvertDoubleSwappedToHost(arg: CFSwappedFloat64) -> f64 {
    CFConvertFloat64SwappedToHost(arg)
}

#[must_use]
#[inline]
pub const fn CFUserNotificationCheckBoxChecked(i: CFIndex) -> CFOptionFlags {
    (1_u64 << (8 + i as u32)) as CFOptionFlags
}

#[must_use]
#[inline]
pub const fn CFUserNotificationSecureTextField(i: CFIndex) -> CFOptionFlags {
    (1_u64 << (16 + i as u32)) as CFOptionFlags
}

#[must_use]
#[inline]
pub const fn CFUserNotificationPopUpSelection(n: CFIndex) -> CFOptionFlags {
    ((n as u64) << 24) as CFOptionFlags
}

#[inline]
pub unsafe fn CFStringInitInlineBuffer(
    str_: CFStringRef,
    buf: *mut CFStringInlineBuffer,
    range: CFRange,
) {
    if let Some(buf) = unsafe { buf.as_mut() } {
        buf.theString = str_;
        buf.rangeToBuffer = range;
        let direct_uni = unsafe { CFStringGetCharactersPtr(str_) };
        buf.directUniCharBuffer = direct_uni;
        buf.directCStringBuffer = if direct_uni.is_null() {
            unsafe { CFStringGetCStringPtr(str_, kCFStringEncodingASCII) }
        } else {
            ptr::null()
        };
        buf.bufferedRangeStart = 0;
        buf.bufferedRangeEnd = 0;
    }
}

#[must_use]
#[inline]
pub unsafe fn CFStringGetCharacterFromInlineBuffer(
    buf: *mut CFStringInlineBuffer,
    idx: CFIndex,
) -> UniChar {
    let Some(buf) = (unsafe { buf.as_mut() }) else {
        return 0;
    };
    if idx < 0 || idx >= buf.rangeToBuffer.length {
        return 0;
    }
    if !buf.directUniCharBuffer.is_null() {
        return unsafe {
            *buf.directUniCharBuffer
                .add((idx + buf.rangeToBuffer.location) as usize)
        };
    }
    if !buf.directCStringBuffer.is_null() {
        return unsafe {
            *buf.directCStringBuffer
                .add((idx + buf.rangeToBuffer.location) as usize) as u8
        } as UniChar;
    }
    if idx >= buf.bufferedRangeEnd || idx < buf.bufferedRangeStart {
        buf.bufferedRangeStart = (idx - 4).max(0);
        buf.bufferedRangeEnd = (buf.bufferedRangeStart + 64).min(buf.rangeToBuffer.length);
        unsafe {
            CFStringGetCharacters(
                buf.theString,
                CFRangeMake(
                    buf.rangeToBuffer.location + buf.bufferedRangeStart,
                    buf.bufferedRangeEnd - buf.bufferedRangeStart,
                ),
                buf.buffer.as_mut_ptr(),
            );
        }
    }
    buf.buffer[(idx - buf.bufferedRangeStart) as usize]
}

#[must_use]
#[inline]
pub const fn CFStringIsSurrogateHighCharacter(character: UniChar) -> Boolean {
    ((character >= 0xD800) && (character <= 0xDBFF)) as Boolean
}

#[must_use]
#[inline]
pub const fn CFStringIsSurrogateLowCharacter(character: UniChar) -> Boolean {
    ((character >= 0xDC00) && (character <= 0xDFFF)) as Boolean
}

#[must_use]
#[inline]
pub const fn CFStringGetLongCharacterForSurrogatePair(
    surrogateHigh: UniChar,
    surrogateLow: UniChar,
) -> UTF32Char {
    (((surrogateHigh as UTF32Char - 0xD800) << 10) + (surrogateLow as UTF32Char - 0xDC00) + 0x10000)
        as UTF32Char
}

#[must_use]
#[inline]
pub unsafe fn CFStringGetSurrogatePairForLongCharacter(
    character: UTF32Char,
    surrogates: *mut UniChar,
) -> Boolean {
    if !(0x10000..0x110000).contains(&character) {
        return 0;
    }
    let scalar = character - 0x10000;
    if !surrogates.is_null() {
        unsafe {
            *surrogates = (0xD800 + (scalar >> 10)) as UniChar;
            *surrogates.add(1) = (0xDC00 + (scalar & 0x3ff)) as UniChar;
        }
    }
    1
}

#[must_use]
#[inline]
pub fn CMTagGetCategory(tag: CMTag) -> CMTagCategory {
    tag.category
}

#[must_use]
#[inline]
pub fn CMTagGetValue(tag: CMTag) -> CMTagValue {
    tag.value
}

#[must_use]
#[inline]
pub fn CMTagHasCategory(tag: CMTag, category: CMTagCategory) -> Boolean {
    (CMTagGetCategory(tag) == category) as Boolean
}

#[must_use]
#[inline]
pub fn CMTagCategoryEqualToTagCategory(tag1: CMTag, tag2: CMTag) -> Boolean {
    (tag1.category == tag2.category) as Boolean
}

#[must_use]
#[inline]
pub fn CMTagIsValid(tag: CMTag) -> Boolean {
    (unsafe { CMTagGetValueDataType(tag) } != kCMTagDataType_Invalid as CMTagDataType) as Boolean
}

#[must_use]
#[inline]
pub fn CMTagCategoryValueEqualToValue(tag1: CMTag, tag2: CMTag) -> Boolean {
    ((tag1.category == tag2.category)
        && (unsafe { CMTagGetValueDataType(tag1) } == unsafe { CMTagGetValueDataType(tag2) })
        && (tag1.value == tag2.value)) as Boolean
}

#[must_use]
#[inline]
pub unsafe fn CMTimebaseCreateWithMasterTimebase(
    allocator: CFAllocatorRef,
    masterTimebase: CMTimebaseRef,
    timebaseOut: *mut CMTimebaseRef,
) -> OSStatus {
    unsafe { CMTimebaseCreateWithSourceTimebase(allocator, masterTimebase, timebaseOut) }
}

#[must_use]
#[inline]
pub unsafe fn CMTimebaseSetMasterClock(
    timebase: CMTimebaseRef,
    newMasterClock: CMClockRef,
) -> OSStatus {
    unsafe { CMTimebaseSetSourceClock(timebase, newMasterClock) }
}

#[must_use]
#[inline]
pub unsafe fn CMTimebaseSetMasterTimebase(
    timebase: CMTimebaseRef,
    newMasterTimebase: CMTimebaseRef,
) -> OSStatus {
    unsafe { CMTimebaseSetSourceTimebase(timebase, newMasterTimebase) }
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct __CVMetalTextureCache {
    _unused: [u8; 0],
}

pub type CVMetalTextureCacheRef = *mut __CVMetalTextureCache;
pub type CVMetalTextureRef = CVImageBufferRef;

extern "C" {
    pub fn CVMetalTextureGetTypeID() -> CFTypeID;
    pub fn CVMetalTextureGetTexture(image: CVMetalTextureRef) -> *mut core::ffi::c_void;
    pub fn CVMetalTextureIsFlipped(image: CVMetalTextureRef) -> Boolean;
    pub fn CVMetalTextureGetCleanTexCoords(
        image: CVMetalTextureRef,
        lowerLeft: *mut f32,
        lowerRight: *mut f32,
        upperRight: *mut f32,
        upperLeft: *mut f32,
    );
    pub fn CVMetalTextureCacheCreateTextureFromImage(
        allocator: CFAllocatorRef,
        textureCache: CVMetalTextureCacheRef,
        sourceImage: CVImageBufferRef,
        textureAttributes: CFDictionaryRef,
        pixelFormat: usize,
        width: usize,
        height: usize,
        planeIndex: usize,
        textureOut: *mut CVMetalTextureRef,
    ) -> CVReturn;
    pub fn CVMetalBufferGetBuffer(buffer: CVMetalBufferRef) -> *mut core::ffi::c_void;
    pub fn CVMetalBufferCacheCreate(
        allocator: CFAllocatorRef,
        cacheAttributes: CFDictionaryRef,
        metalDevice: *mut core::ffi::c_void,
        metalBufferAttributes: CFDictionaryRef,
        cacheOut: *mut CVMetalBufferCacheRef,
    ) -> CVReturn;
}

#[must_use]
#[inline]
pub unsafe fn dispatch_get_main_queue() -> dispatch_queue_main_t {
    ptr::addr_of_mut!(_dispatch_main_q)
}