| 1 |
// Copyright 2016 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 |
//! Evil private APIs. |
| 11 |
//! |
| 12 |
//! These are liable to change at any time. Use with caution! |
| 13 |
|
| 14 |
use geometry::CGRect; |
| 15 |
use libc::{c_int, c_uint}; |
| 16 |
use std::ptr; |
| 17 |
|
| 18 |
pub struct CGSRegion { |
| 19 |
region: ffi::CGSRegionRef, |
| 20 |
} |
| 21 |
|
| 22 |
impl Drop for CGSRegion { |
| 23 |
fn drop(&mut self) { |
| 24 |
unsafe { |
| 25 |
ffi::CGSRegionRelease(self.region) |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
impl CGSRegion { |
| 31 |
#[inline] |
| 32 |
pub fn from_rect(rect: &CGRect) -> CGSRegion { |
| 33 |
unsafe { |
| 34 |
let mut region = ptr::null_mut(); |
| 35 |
assert!(ffi::CGSNewRegionWithRect(rect, &mut region) == 0); |
| 36 |
CGSRegion { |
| 37 |
region: region, |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
#[inline] |
| 43 |
pub fn from_rects(rects: &[CGRect]) -> CGSRegion { |
| 44 |
unsafe { |
| 45 |
let mut region = ptr::null_mut(); |
| 46 |
assert!(ffi::CGSNewRegionWithRectList(rects.as_ptr(), |
| 47 |
rects.len() as c_uint, |
| 48 |
&mut region) == 0); |
| 49 |
CGSRegion { |
| 50 |
region: region, |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/// This should always be memory-safe; the window server rejects any invalid surface IDs. |
| 57 |
pub struct CGSSurface { |
| 58 |
context_id: c_uint, |
| 59 |
window_number: c_int, |
| 60 |
surface_id: c_uint, |
| 61 |
} |
| 62 |
|
| 63 |
impl CGSSurface { |
| 64 |
#[inline] |
| 65 |
pub fn from_ids(context_id: c_uint, window_number: c_int, surface_id: c_uint) -> CGSSurface { |
| 66 |
CGSSurface { |
| 67 |
context_id: context_id, |
| 68 |
window_number: window_number, |
| 69 |
surface_id: surface_id, |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
#[inline] |
| 74 |
pub fn id(&self) -> c_uint { |
| 75 |
self.surface_id |
| 76 |
} |
| 77 |
|
| 78 |
#[inline] |
| 79 |
pub fn set_shape(&self, region: &CGSRegion) { |
| 80 |
unsafe { |
| 81 |
assert!(ffi::CGSSetSurfaceShape(self.context_id, |
| 82 |
self.window_number, |
| 83 |
self.surface_id, |
| 84 |
region.region) == 0) |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
mod ffi { |
| 90 |
use geometry::CGRect; |
| 91 |
use libc::{c_int, c_uint}; |
| 92 |
|
| 93 |
// This is an enum so that we can't easily make instances of this opaque type. |
| 94 |
pub enum CGSRegionObject {} |
| 95 |
|
| 96 |
pub type CGError = OSStatus; |
| 97 |
pub type CGSRegionRef = *mut CGSRegionObject; |
| 98 |
pub type OSStatus = i32; |
| 99 |
|
| 100 |
#[link(name = "CoreGraphics", kind = "framework")] |
| 101 |
extern { |
| 102 |
pub fn CGSRegionRelease(region: CGSRegionRef); |
| 103 |
pub fn CGSNewRegionWithRect(rect: *const CGRect, outRegion: *mut CGSRegionRef) -> CGError; |
| 104 |
pub fn CGSNewRegionWithRectList(rects: *const CGRect, |
| 105 |
rectCount: c_uint, |
| 106 |
outRegion: *mut CGSRegionRef) |
| 107 |
-> CGError; |
| 108 |
|
| 109 |
pub fn CGSSetSurfaceShape(contextID: c_uint, |
| 110 |
windowNumber: c_int, |
| 111 |
surfaceID: c_uint, |
| 112 |
region: CGSRegionRef) |
| 113 |
-> CGError; |
| 114 |
} |
| 115 |
} |
| 116 |
|