winit-sonoma-fix/src/win32/monitor.rs

117 lines
3.9 KiB
Rust
Raw Normal View History

2014-07-31 18:52:05 +10:00
use super::ffi;
/// Win32 implementation of the main `MonitorID` object.
2014-07-31 18:52:05 +10:00
pub struct MonitorID {
/// The system name of the monitor.
2014-07-31 18:52:05 +10:00
name: [ffi::WCHAR, ..32],
/// Name to give to the user.
2014-07-31 18:52:05 +10:00
readable_name: String,
/// See the `StateFlags` element here:
/// http://msdn.microsoft.com/en-us/library/dd183569(v=vs.85).aspx
2014-07-31 18:52:05 +10:00
flags: ffi::DWORD,
/// The position of the monitor in pixels on the desktop.
///
/// A window that is positionned at these coordinates will overlap the monitor.
2014-07-31 18:52:05 +10:00
position: (uint, uint),
}
/// Win32 implementation of the main `get_available_monitors` function.
2014-07-31 18:52:05 +10:00
pub fn get_available_monitors() -> Vec<MonitorID> {
use std::{iter, mem, ptr};
// return value
2014-07-31 18:52:05 +10:00
let mut result = Vec::new();
// enumerating the devices is done by querying device 0, then device 1, then device 2, etc.
// until the query function returns null
2014-07-31 18:52:05 +10:00
for id in iter::count(0u, 1) {
// getting the DISPLAY_DEVICEW object of the current device
let output = {
let mut output: ffi::DISPLAY_DEVICEW = unsafe { mem::zeroed() };
output.cb = mem::size_of::<ffi::DISPLAY_DEVICEW>() as ffi::DWORD;
2014-07-31 18:52:05 +10:00
if unsafe { ffi::EnumDisplayDevicesW(ptr::null(),
id as ffi::DWORD, &mut output, 0) } == 0
{
// the device doesn't exist, which means we have finished enumerating
break;
}
2014-07-31 18:52:05 +10:00
if (output.StateFlags & ffi::DISPLAY_DEVICE_ACTIVE) == 0 ||
(output.StateFlags & ffi::DISPLAY_DEVICE_MIRRORING_DRIVER) != 0
{
// the device is not active
// the Win32 api usually returns a lot of inactive devices
continue;
}
output
};
2014-07-31 18:52:05 +10:00
// computing the human-friendly name
2014-07-31 18:52:05 +10:00
let readable_name = String::from_utf16_lossy(output.DeviceString.as_slice());
let readable_name = readable_name.as_slice().trim_right_chars(0 as char).to_string();
// getting the position
2014-07-31 18:52:05 +10:00
let position = unsafe {
let mut dev: ffi::DEVMODE = mem::zeroed();
dev.dmSize = mem::size_of::<ffi::DEVMODE>() as ffi::WORD;
if ffi::EnumDisplaySettingsExW(output.DeviceName.as_ptr(), ffi::ENUM_CURRENT_SETTINGS,
&mut dev, 0) == 0
{
continue;
2014-07-31 18:52:05 +10:00
}
let point: &ffi::POINTL = mem::transmute(&dev.union1);
(point.x as uint, point.y as uint)
};
// adding to the resulting list
2014-07-31 18:52:05 +10:00
result.push(MonitorID {
name: output.DeviceName,
readable_name: readable_name,
flags: output.StateFlags,
position: position,
});
}
result
}
/// Win32 implementation of the main `get_primary_monitor` function.
2014-07-31 18:52:05 +10:00
pub fn get_primary_monitor() -> MonitorID {
// we simply get all available monitors and return the one with the `PRIMARY_DEVICE` flag
// TODO: it is possible to query the win32 API for the primary monitor, this should be done
// instead
2014-07-31 18:52:05 +10:00
for monitor in get_available_monitors().move_iter() {
if (monitor.flags & ffi::DISPLAY_DEVICE_PRIMARY_DEVICE) != 0 {
return monitor
}
}
fail!("Failed to find the primary monitor")
}
impl MonitorID {
/// See the docs if the crate root file.
2014-07-31 18:52:05 +10:00
pub fn get_name(&self) -> Option<String> {
Some(self.readable_name.clone())
}
/// This is a Win32-only function for `MonitorID` that returns the system name of the device.
2014-07-31 18:52:05 +10:00
pub fn get_system_name(&self) -> &[ffi::WCHAR] {
self.name.as_slice()
}
/// This is a Win32-only function for `MonitorID` that returns the position of the
/// monitor on the desktop.
/// A window that is positionned at these coordinates will overlap the monitor.
2014-07-31 18:52:05 +10:00
pub fn get_position(&self) -> (uint, uint) {
self.position
}
}