1 // Copyright 2018 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 #![allow(non_upper_case_globals)]
11 
12 use core_foundation::array::{CFArray, CFArrayRef};
13 use core_foundation::base::{CFType, TCFType};
14 use core_foundation::dictionary::CFDictionary;
15 use core_foundation::string::{CFString, CFStringRef};
16 use foreign_types::ForeignType;
17 
18 use geometry::CGRect;
19 use image::CGImage;
20 use sys;
21 
22 pub type CGWindowID = u32;
23 
24 pub type CGWindowSharingType = u32;
25 pub const kCGWindowSharingNone: CGWindowSharingType = 0;
26 pub const kCGWindowSharingReadOnly: CGWindowSharingType = 1;
27 pub const kCGWindowSharingReadWrite: CGWindowSharingType = 1;
28 
29 pub type CGWindowBackingType = u32;
30 pub const kCGWindowBackingStoreRetained: CGWindowBackingType = 0;
31 pub const kCGWindowBackingStoreNonretained: CGWindowBackingType = 1;
32 pub const kCGWindowBackingStoreBuffered: CGWindowBackingType = 2;
33 
34 pub type CGWindowListOption = u32;
35 pub const kCGWindowListOptionAll: CGWindowListOption = 1 << 0;
36 pub const kCGWindowListOptionOnScreenOnly: CGWindowListOption = 1 << 1;
37 pub const kCGWindowListOptionOnScreenAboveWindow: CGWindowListOption = 1 << 2;
38 pub const kCGWindowListOptionOnScreenBelowWindow: CGWindowListOption = 1 << 3;
39 pub const kCGWindowListOptionIncludingWindow: CGWindowListOption = 1 << 4;
40 pub const kCGWindowListOptionExcludeDesktopElements: CGWindowListOption = 1 << 5;
41 
42 pub type CGWindowImageOption = u32;
43 pub const kCGWindowImageDefault: CGWindowImageOption = 0;
44 pub const kCGWindowImageBoundsIgnoreFraming: CGWindowImageOption = 1 << 0;
45 pub const kCGWindowImageShouldBeOpaque: CGWindowImageOption = 1 << 1;
46 pub const kCGWindowImageOnlyShadows: CGWindowImageOption = 1 << 2;
47 pub const kCGWindowImageBestResolution: CGWindowImageOption = 1 << 3;
48 pub const kCGWindowImageNominalResolution: CGWindowImageOption = 1 << 4;
49 
50 pub const kCGNullWindowID: CGWindowID = 0;
51 
copy_window_info(option: CGWindowListOption, relative_to_window: CGWindowID) -> Option<CFArray>52 pub fn copy_window_info(option: CGWindowListOption, relative_to_window: CGWindowID)
53                         -> Option<CFArray> {
54     unsafe {
55         let array = CGWindowListCopyWindowInfo(option, relative_to_window);
56         if array.is_null() {
57             None
58         } else {
59             Some(TCFType::wrap_under_create_rule(array))
60         }
61     }
62 }
63 
create_window_list(option: CGWindowListOption, relative_to_window: CGWindowID) -> Option<CFArray<CGWindowID>>64 pub fn create_window_list(option: CGWindowListOption, relative_to_window: CGWindowID)
65                           -> Option<CFArray<CGWindowID>> {
66     unsafe {
67         let array = CGWindowListCreate(option, relative_to_window);
68         if array.is_null() {
69             None
70         } else {
71             Some(TCFType::wrap_under_create_rule(array))
72         }
73     }
74 }
75 
create_description_from_array(window_array: CFArray<CGWindowID>) -> Option<CFArray<CFDictionary<CFString, CFType>>>76 pub fn create_description_from_array(window_array: CFArray<CGWindowID>) ->
77                                      Option<CFArray<CFDictionary<CFString, CFType>>> {
78     unsafe {
79         let array = CGWindowListCreateDescriptionFromArray(window_array.as_concrete_TypeRef());
80         if array.is_null() {
81             None
82         } else {
83             Some(TCFType::wrap_under_create_rule(array))
84         }
85     }
86 }
87 
create_image(screen_bounds: CGRect, list_option: CGWindowListOption, window_id: CGWindowID, image_option: CGWindowImageOption) -> Option<CGImage>88 pub fn create_image(screen_bounds: CGRect,
89                     list_option: CGWindowListOption,
90                     window_id: CGWindowID,
91                     image_option: CGWindowImageOption)
92                     -> Option<CGImage> {
93     unsafe {
94         let image = CGWindowListCreateImage(screen_bounds, list_option, window_id, image_option);
95         if image.is_null() {
96             None
97         } else {
98             Some(CGImage::from_ptr(image))
99         }
100     }
101 }
102 
create_image_from_array(screen_bounds: CGRect, window_array: CFArray, image_option: CGWindowImageOption) -> Option<CGImage>103 pub fn create_image_from_array(screen_bounds: CGRect,
104                                window_array: CFArray,
105                                image_option: CGWindowImageOption)
106                                -> Option<CGImage> {
107     unsafe {
108         let image = CGWindowListCreateImageFromArray(screen_bounds,
109                                                      window_array.as_concrete_TypeRef(),
110                                                      image_option);
111         if image.is_null() {
112             None
113         } else {
114             Some(CGImage::from_ptr(image))
115         }
116     }
117 }
118 
119 #[link(name = "CoreGraphics", kind = "framework")]
120 extern {
121     pub static kCGWindowNumber: CFStringRef;
122     pub static kCGWindowStoreType: CFStringRef;
123     pub static kCGWindowLayer: CFStringRef;
124     pub static kCGWindowBounds: CFStringRef;
125     pub static kCGWindowSharingState: CFStringRef;
126     pub static kCGWindowAlpha: CFStringRef;
127     pub static kCGWindowOwnerPID: CFStringRef;
128     pub static kCGWindowMemoryUsage: CFStringRef;
129     pub static kCGWindowWorkspace: CFStringRef;
130     pub static kCGWindowOwnerName: CFStringRef;
131     pub static kCGWindowName: CFStringRef;
132     pub static kCGWindowIsOnscreen: CFStringRef;
133     pub static kCGWindowBackingLocationVideoMemory: CFStringRef;
134 
CGWindowListCopyWindowInfo(option: CGWindowListOption, relativeToWindow: CGWindowID) -> CFArrayRef135     pub fn CGWindowListCopyWindowInfo(option: CGWindowListOption, relativeToWindow: CGWindowID)
136                                       -> CFArrayRef;
CGWindowListCreate(option: CGWindowListOption, relativeToWindow: CGWindowID) -> CFArrayRef137     pub fn CGWindowListCreate(option: CGWindowListOption, relativeToWindow: CGWindowID)
138                               -> CFArrayRef;
CGWindowListCreateDescriptionFromArray(windowArray: CFArrayRef) -> CFArrayRef139     pub fn CGWindowListCreateDescriptionFromArray(windowArray: CFArrayRef) -> CFArrayRef;
CGWindowListCreateImage(screenBounds: CGRect, listOption: CGWindowListOption, windowID: CGWindowID, imageOption: CGWindowImageOption) -> *mut sys::CGImage140     pub fn CGWindowListCreateImage(screenBounds: CGRect,
141                                    listOption: CGWindowListOption,
142                                    windowID: CGWindowID,
143                                    imageOption: CGWindowImageOption)
144                                    -> *mut sys::CGImage;
CGWindowListCreateImageFromArray(screenBounds: CGRect, windowArray: CFArrayRef, imageOption: CGWindowImageOption) -> *mut sys::CGImage145     pub fn CGWindowListCreateImageFromArray(screenBounds: CGRect,
146                                             windowArray: CFArrayRef,
147                                             imageOption: CGWindowImageOption)
148                                             -> *mut sys::CGImage;
149 }
150