2017-12-25 00:46:47 +11:00
|
|
|
use winapi::ctypes::wchar_t;
|
|
|
|
use winapi::shared::minwindef::{DWORD, LPARAM, BOOL, TRUE};
|
|
|
|
use winapi::shared::windef::{HMONITOR, HDC, LPRECT};
|
|
|
|
use winapi::um::winuser;
|
2014-07-31 18:52:05 +10:00
|
|
|
|
2015-02-22 00:40:23 +11:00
|
|
|
use std::collections::VecDeque;
|
2017-10-26 02:12:39 +11:00
|
|
|
use std::{mem, ptr};
|
2015-01-02 18:09:16 +11:00
|
|
|
|
2017-09-01 19:04:57 +10:00
|
|
|
use super::EventsLoop;
|
|
|
|
|
2015-09-24 17:11:59 +10:00
|
|
|
/// Win32 implementation of the main `MonitorId` object.
|
2015-07-19 21:53:40 +10:00
|
|
|
#[derive(Clone)]
|
2015-09-24 17:11:59 +10:00
|
|
|
pub struct MonitorId {
|
2015-03-25 07:29:17 +11:00
|
|
|
/// The system name of the adapter.
|
2017-12-25 00:46:47 +11:00
|
|
|
adapter_name: [wchar_t; 32],
|
2015-03-25 07:29:17 +11:00
|
|
|
|
2017-10-26 02:12:39 +11:00
|
|
|
/// Monitor handle.
|
|
|
|
hmonitor: HMonitor,
|
|
|
|
|
2014-08-02 07:02:26 +10:00
|
|
|
/// The system name of the monitor.
|
2015-03-25 07:29:17 +11:00
|
|
|
monitor_name: String,
|
2014-08-02 07:02:26 +10:00
|
|
|
|
2015-04-12 17:32:25 +10:00
|
|
|
/// True if this is the primary monitor.
|
|
|
|
primary: bool,
|
|
|
|
|
2014-08-02 07:02:26 +10:00
|
|
|
/// The position of the monitor in pixels on the desktop.
|
|
|
|
///
|
2017-10-20 04:08:05 +11:00
|
|
|
/// A window that is positioned at these coordinates will overlap the monitor.
|
|
|
|
position: (i32, i32),
|
2014-08-02 19:17:49 +10:00
|
|
|
|
|
|
|
/// The current resolution in pixels on the monitor.
|
2015-01-13 23:21:36 +11:00
|
|
|
dimensions: (u32, u32),
|
2014-07-31 18:52:05 +10:00
|
|
|
|
2017-10-26 02:12:39 +11:00
|
|
|
/// DPI scaling factor.
|
|
|
|
hidpi_factor: f32,
|
2015-03-25 07:29:17 +11:00
|
|
|
}
|
2014-07-31 18:52:05 +10:00
|
|
|
|
2017-10-26 02:12:39 +11:00
|
|
|
// Send is not implemented for HMONITOR, we have to wrap it and implement it manually.
|
|
|
|
// For more info see:
|
|
|
|
// https://github.com/retep998/winapi-rs/issues/360
|
|
|
|
// https://github.com/retep998/winapi-rs/issues/396
|
|
|
|
#[derive(Clone)]
|
2017-12-25 00:46:47 +11:00
|
|
|
struct HMonitor(HMONITOR);
|
2014-08-02 07:02:26 +10:00
|
|
|
|
2017-10-26 02:12:39 +11:00
|
|
|
unsafe impl Send for HMonitor {}
|
2014-07-31 18:52:05 +10:00
|
|
|
|
2017-12-25 00:46:47 +11:00
|
|
|
fn wchar_as_string(wchar: &[wchar_t]) -> String {
|
2015-03-25 07:29:17 +11:00
|
|
|
String::from_utf16_lossy(wchar)
|
|
|
|
.trim_right_matches(0 as char)
|
|
|
|
.to_string()
|
|
|
|
}
|
2014-07-31 18:52:05 +10:00
|
|
|
|
2017-12-25 00:46:47 +11:00
|
|
|
unsafe extern "system" fn monitor_enum_proc(hmonitor: HMONITOR, _: HDC, place: LPRECT, data: LPARAM) -> BOOL {
|
2017-10-26 02:12:39 +11:00
|
|
|
let monitors = data as *mut VecDeque<MonitorId>;
|
|
|
|
|
|
|
|
let place = *place;
|
|
|
|
let position = (place.left as i32, place.top as i32);
|
|
|
|
let dimensions = ((place.right - place.left) as u32, (place.bottom - place.top) as u32);
|
|
|
|
|
2017-12-25 00:46:47 +11:00
|
|
|
let mut monitor_info: winuser::MONITORINFOEXW = mem::zeroed();
|
|
|
|
monitor_info.cbSize = mem::size_of::<winuser::MONITORINFOEXW>() as DWORD;
|
|
|
|
if winuser::GetMonitorInfoW(hmonitor, &mut monitor_info as *mut winuser::MONITORINFOEXW as *mut winuser::MONITORINFO) == 0 {
|
2017-10-26 02:12:39 +11:00
|
|
|
// Some error occurred, just skip this monitor and go on.
|
2017-12-25 00:46:47 +11:00
|
|
|
return TRUE;
|
2017-10-26 02:12:39 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
(*monitors).push_back(MonitorId {
|
|
|
|
adapter_name: monitor_info.szDevice,
|
|
|
|
hmonitor: HMonitor(hmonitor),
|
|
|
|
monitor_name: wchar_as_string(&monitor_info.szDevice),
|
2017-12-25 00:46:47 +11:00
|
|
|
primary: monitor_info.dwFlags & winuser::MONITORINFOF_PRIMARY != 0,
|
2017-10-26 02:12:39 +11:00
|
|
|
position,
|
|
|
|
dimensions,
|
|
|
|
hidpi_factor: 1.0,
|
|
|
|
});
|
|
|
|
|
|
|
|
// TRUE means continue enumeration.
|
2017-12-25 00:46:47 +11:00
|
|
|
TRUE
|
2017-10-26 02:12:39 +11:00
|
|
|
}
|
|
|
|
|
2017-09-01 19:04:57 +10:00
|
|
|
impl EventsLoop {
|
|
|
|
pub fn get_available_monitors(&self) -> VecDeque<MonitorId> {
|
2017-10-26 02:12:39 +11:00
|
|
|
unsafe {
|
|
|
|
let mut result: VecDeque<MonitorId> = VecDeque::new();
|
2017-12-25 00:46:47 +11:00
|
|
|
winuser::EnumDisplayMonitors(ptr::null_mut(), ptr::null_mut(), Some(monitor_enum_proc), &mut result as *mut _ as LPARAM);
|
2017-10-26 02:12:39 +11:00
|
|
|
result
|
2015-03-25 07:29:17 +11:00
|
|
|
}
|
2014-07-31 18:52:05 +10:00
|
|
|
}
|
|
|
|
|
2017-09-01 19:04:57 +10:00
|
|
|
pub fn get_primary_monitor(&self) -> MonitorId {
|
2017-10-26 02:12:39 +11:00
|
|
|
// we simply get all available monitors and return the one with the `MONITORINFOF_PRIMARY` flag
|
2017-09-01 19:04:57 +10:00
|
|
|
// TODO: it is possible to query the win32 API for the primary monitor, this should be done
|
|
|
|
// instead
|
|
|
|
for monitor in self.get_available_monitors().into_iter() {
|
|
|
|
if monitor.primary {
|
|
|
|
return monitor;
|
|
|
|
}
|
2014-07-31 18:52:05 +10:00
|
|
|
}
|
|
|
|
|
2017-09-01 19:04:57 +10:00
|
|
|
panic!("Failed to find the primary monitor")
|
|
|
|
}
|
2014-07-31 18:52:05 +10:00
|
|
|
}
|
|
|
|
|
2015-09-24 17:11:59 +10:00
|
|
|
impl MonitorId {
|
2014-08-02 07:02:26 +10:00
|
|
|
/// See the docs if the crate root file.
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2014-07-31 18:52:05 +10:00
|
|
|
pub fn get_name(&self) -> Option<String> {
|
2017-10-26 02:12:39 +11:00
|
|
|
Some(self.monitor_name.clone())
|
2014-07-31 18:52:05 +10:00
|
|
|
}
|
|
|
|
|
2015-03-17 07:52:58 +11:00
|
|
|
/// See the docs of the crate root file.
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2017-08-30 16:49:18 +10:00
|
|
|
pub fn get_native_identifier(&self) -> String {
|
|
|
|
self.monitor_name.clone()
|
2015-03-17 07:52:58 +11:00
|
|
|
}
|
|
|
|
|
2017-10-26 02:12:39 +11:00
|
|
|
/// See the docs of the crate root file.
|
|
|
|
#[inline]
|
2017-12-25 00:46:47 +11:00
|
|
|
pub fn get_hmonitor(&self) -> HMONITOR {
|
2017-10-26 02:12:39 +11:00
|
|
|
self.hmonitor.0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// See the docs of the crate root file.
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-01-13 23:21:36 +11:00
|
|
|
pub fn get_dimensions(&self) -> (u32, u32) {
|
2017-10-26 02:12:39 +11:00
|
|
|
// TODO: retrieve the dimensions every time this is called
|
2014-08-02 19:17:49 +10:00
|
|
|
self.dimensions
|
|
|
|
}
|
|
|
|
|
2015-09-24 17:11:59 +10:00
|
|
|
/// This is a Win32-only function for `MonitorId` that returns the system name of the adapter
|
2015-03-25 07:29:17 +11:00
|
|
|
/// device.
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2017-12-25 00:46:47 +11:00
|
|
|
pub fn get_adapter_name(&self) -> &[wchar_t] {
|
2015-03-26 05:57:38 +11:00
|
|
|
&self.adapter_name
|
2014-07-31 18:52:05 +10:00
|
|
|
}
|
|
|
|
|
2017-10-20 04:08:05 +11:00
|
|
|
/// A window that is positioned at these coordinates will overlap the monitor.
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2017-10-20 04:08:05 +11:00
|
|
|
pub fn get_position(&self) -> (i32, i32) {
|
2014-07-31 18:52:05 +10:00
|
|
|
self.position
|
|
|
|
}
|
2017-10-17 22:56:38 +11:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn get_hidpi_factor(&self) -> f32 {
|
2017-10-26 02:12:39 +11:00
|
|
|
self.hidpi_factor
|
2017-10-17 22:56:38 +11:00
|
|
|
}
|
2014-07-31 18:52:05 +10:00
|
|
|
}
|