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
extern crate image;
use self::image::{GenericImage, ImageResult, Rgba};
use bitmap;
use geometry::{Point, Rect, Size};
#[cfg(target_os = "macos")]
use core_graphics::display::CGDisplay;
#[cfg(target_os = "linux")]
use internal;
#[cfg(target_os = "linux")]
use x11;
pub fn size() -> Size {
system_size()
}
pub fn scale() -> f64 {
system_scale()
}
pub fn is_point_visible(point: Point) -> bool {
Rect::new(Point::ZERO, size()).is_point_visible(point)
}
pub fn is_rect_visible(rect: Rect) -> bool {
Rect::new(Point::ZERO, size()).is_rect_visible(rect)
}
pub fn get_color(point: Point) -> ImageResult<Rgba<u8>> {
let bmp = try!(bitmap::capture_screen_portion(Rect::new(
point,
Size::new(1.0, 1.0)
)));
Ok(bmp.image.get_pixel(0, 0))
}
#[cfg(target_os = "macos")]
fn system_size() -> Size {
Size::from(CGDisplay::main().bounds().size)
}
#[cfg(target_os = "macos")]
fn system_scale() -> f64 {
let mode = CGDisplay::main().display_mode().unwrap();
mode.pixel_height() as f64 / mode.height() as f64
}
#[cfg(windows)]
fn system_size() -> Size {
use winapi::um::winuser::{GetSystemMetrics, SM_CXSCREEN, SM_CYSCREEN};
let scale_factor = scale();
let width = unsafe { GetSystemMetrics(SM_CXSCREEN) } as f64;
let height = unsafe { GetSystemMetrics(SM_CYSCREEN) } as f64;
Size::new(width, height).scaled(1.0 / scale_factor)
}
#[cfg(windows)]
fn system_scale() -> f64 {
use std;
use std::ffi::CString;
use winapi::shared::minwindef::FARPROC;
use winapi::um::libloaderapi::{GetProcAddress, LoadLibraryA};
use winapi::um::winuser::GetDesktopWindow;
let user32_module = unsafe { LoadLibraryA(CString::new("user32.dll").unwrap().as_ptr()) };
let set_process_dpi_aware_ptr: FARPROC = unsafe {
GetProcAddress(
user32_module,
CString::new("SetProcessDPIAware").unwrap().as_ptr(),
)
};
let get_dpi_for_window_ptr: FARPROC = unsafe {
GetProcAddress(
user32_module,
CString::new("GetDpiForWindow").unwrap().as_ptr(),
)
};
if set_process_dpi_aware_ptr != std::ptr::null_mut()
&& get_dpi_for_window_ptr != std::ptr::null_mut()
{
let set_process_dpi_aware: SetProcessDPIAwareSignature =
unsafe { std::mem::transmute(set_process_dpi_aware_ptr) };
let get_dpi_for_window: GetDPIForWindowSignature =
unsafe { std::mem::transmute(get_dpi_for_window_ptr) };
unsafe { set_process_dpi_aware() };
let window = unsafe { GetDesktopWindow() };
let dpi = unsafe { get_dpi_for_window(window) };
dpi as f64 / 96.0
} else {
1.0
}
}
#[cfg(target_os = "linux")]
fn system_size() -> Size {
internal::X_MAIN_DISPLAY.with(|display| unsafe {
let scale_factor = scale();
let screen = x11::xlib::XDefaultScreen(*display);
let width = x11::xlib::XDisplayWidth(*display, screen) as f64;
let height = x11::xlib::XDisplayHeight(*display, screen) as f64;
Size::new(width, height).scaled(1.0 / scale_factor)
})
}
#[cfg(target_os = "linux")]
fn system_scale() -> f64 {
internal::X_SCALE_FACTOR.with(|scale| *scale)
}
#[cfg(windows)]
use libc;
#[cfg(windows)]
use winapi::shared::windef::HWND;
#[cfg(windows)]
type SetProcessDPIAwareSignature = unsafe extern "C" fn();
#[cfg(windows)]
type GetDPIForWindowSignature = unsafe extern "C" fn(HWND) -> libc::c_uint;