rustimal 0.8.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
//! Windows calls, types and checks that more than one module needs. What only one module
//! uses is declared beside it.

use winit::raw_window_handle::{HasWindowHandle, RawWindowHandle};
use winit::window::Window;

/// A window handle (`HWND`).
pub type Hwnd = isize;

/// A message of a thread's queue (`MSG`).
#[repr(C)]
pub struct Msg {
    pub hwnd: Hwnd,
    pub message: u32,
    pub wparam: usize,
    pub lparam: isize,
    pub time: u32,
    pub pt_x: i32,
    pub pt_y: i32,
}

impl Msg {
    pub fn empty() -> Msg {
        Msg { hwnd: 0, message: 0, wparam: 0, lparam: 0, time: 0, pt_x: 0, pt_y: 0 }
    }
}

#[link(name = "user32")]
unsafe extern "system" {
    pub fn RegisterHotKey(hwnd: Hwnd, id: i32, modifiers: u32, vk: u32) -> i32;
    pub fn UnregisterHotKey(hwnd: Hwnd, id: i32) -> i32;
    pub fn GetMessageW(msg: *mut Msg, hwnd: Hwnd, min: u32, max: u32) -> i32;
    pub fn PostThreadMessageW(thread: u32, msg: u32, wparam: usize, lparam: isize) -> i32;
    pub fn GetForegroundWindow() -> Hwnd;
    pub fn IsChild(parent: Hwnd, child: Hwnd) -> i32;
    pub fn GetWindow(hwnd: Hwnd, command: u32) -> Hwnd;
    pub fn GetWindowLongPtrW(hwnd: Hwnd, index: i32) -> isize;
    pub fn SetWindowLongPtrW(hwnd: Hwnd, index: i32, value: isize) -> isize;
    pub fn SetWindowPos(hwnd: Hwnd, after: Hwnd, x: i32, y: i32, cx: i32, cy: i32, flags: u32) -> i32;
}

#[link(name = "kernel32")]
unsafe extern "system" {
    pub fn GetCurrentThreadId() -> u32;
    pub fn CloseHandle(handle: isize) -> i32;
    pub fn GetStdHandle(which: u32) -> isize;
}

/// The native handle of a winit window.
pub fn handle_of(window: &Window) -> Option<Hwnd> {
    match window.window_handle().ok()?.as_raw() {
        RawWindowHandle::Win32(h) => Some(h.hwnd.get()),
        _ => None,
    }
}

/// Is this window the one in front, or one that belongs to it? A program with the keyboard
/// may be what Windows names as the one in front (Chromium brings itself to the front); laid
/// over a pane it is a child of the window, or owned by it (and what it opens, a dialog or a
/// menu, is owned by it in turn), so those count.
pub fn is_in_front(window: Hwnd) -> bool {
    const GW_OWNER: u32 = 4;
    // SAFETY: only asks about handles.
    unsafe {
        let front = GetForegroundWindow();
        if front == 0 || IsChild(window, front) != 0 {
            return front != 0;
        }
        let mut at = front;
        for _ in 0..4 {
            if at == window {
                return true;
            }
            at = GetWindow(at, GW_OWNER);
            if at == 0 {
                break;
            }
        }
        false
    }
}