| 33 | |
| 34 | impl CapturerGDI { |
| 35 | pub fn new(name: &[u16], width: i32, height: i32) -> Result<Self, Box<dyn std::error::Error>> { |
| 36 | /* or Enumerate monitors with EnumDisplayMonitors, |
| 37 | https://stackoverflow.com/questions/34987695/how-can-i-get-an-hmonitor-handle-from-a-display-device-name |
| 38 | #[no_mangle] |
| 39 | pub extern "C" fn callback(m: HMONITOR, dc: HDC, rect: LPRECT, lp: LPARAM) -> BOOL {} |
| 40 | */ |
| 41 | /* |
| 42 | shared::windef::HMONITOR, |
| 43 | winuser::{GetMonitorInfoW, GetSystemMetrics, MONITORINFOEXW}, |
| 44 | let mut mi: MONITORINFOEXW = std::mem::MaybeUninit::uninit().assume_init(); |
| 45 | mi.cbSize = size_of::<MONITORINFOEXW>() as _; |
| 46 | if GetMonitorInfoW(m, &mut mi as *mut MONITORINFOEXW as _) == 0 { |
| 47 | return Err(format!("Failed to get monitor information of: {:?}", m).into()); |
| 48 | } |
| 49 | */ |
| 50 | unsafe { |
| 51 | if name.is_empty() { |
| 52 | return Err("Empty display name".into()); |
| 53 | } |
| 54 | let screen_dc = CreateDCW(&name[0], 0 as _, 0 as _, 0 as _); |
| 55 | if screen_dc.is_null() { |
| 56 | return Err("Failed to create dc from monitor name".into()); |
| 57 | } |
| 58 | |
| 59 | // Create a Windows Bitmap, and copy the bits into it |
| 60 | let dc = CreateCompatibleDC(screen_dc); |
| 61 | if dc.is_null() { |
| 62 | DeleteDC(screen_dc); |
| 63 | return Err("Can't get a Windows display".into()); |
| 64 | } |
| 65 | |
| 66 | let bmp = CreateCompatibleBitmap(screen_dc, width, height); |
| 67 | if bmp.is_null() { |
| 68 | DeleteDC(screen_dc); |
| 69 | DeleteDC(dc); |
| 70 | return Err("Can't create a Windows buffer".into()); |
| 71 | } |
| 72 | |
| 73 | let res = SelectObject(dc, bmp as _); |
| 74 | if res.is_null() || res == HGDI_ERROR { |
| 75 | DeleteDC(screen_dc); |
| 76 | DeleteDC(dc); |
| 77 | DeleteObject(bmp as _); |
| 78 | return Err("Can't select Windows buffer".into()); |
| 79 | } |
| 80 | Ok(Self { |
| 81 | screen_dc, |
| 82 | dc, |
| 83 | bmp, |
| 84 | width, |
| 85 | height, |
| 86 | }) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | pub fn frame(&self, data: &mut Vec<u8>) -> Result<(), Box<dyn std::error::Error>> { |
| 91 | unsafe { |