randomusert_lib/platform/
mod.rs

1//mod platform;
2
3//windows
4
5#[cfg(target_os = "windows")]
6mod platform {
7    use std::ptr::{null, null_mut};
8    use winapi::um::winuser::*;
9    use winapi::um::libloaderapi::GetModuleHandleW;
10    use winapi::shared::windef::HWND;
11
12    pub fn create_window(title: &str, width: u32, height: u32) {
13        unsafe {
14            let h_instance = GetModuleHandleW(null());
15            let class_name = wide_null("CrossPlatformWindowClass");
16
17            let wnd_class = WNDCLASSW {
18                style: CS_HREDRAW | CS_VREDRAW,
19                lpfnWndProc: Some(def_window_proc),
20                hInstance: h_instance,
21                lpszClassName: class_name.as_ptr(),
22                hbrBackground: (COLOR_WINDOW + 1) as _,
23                hCursor: LoadCursorW(null_mut(), IDC_ARROW),
24                lpszMenuName: null(),
25                cbClsExtra: 0,
26                cbWndExtra: 0,
27                hIcon: null_mut(),
28            };
29
30            RegisterClassW(&wnd_class);
31
32            let hwnd: HWND = CreateWindowExW(
33                0,
34                class_name.as_ptr(),
35                wide_null(title).as_ptr(),
36                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
37                CW_USEDEFAULT,
38                CW_USEDEFAULT,
39                width as i32,
40                height as i32,
41                null_mut(),
42                null_mut(),
43                h_instance,
44                null_mut(),
45            );
46
47            let mut msg = std::mem::zeroed();
48            while GetMessageW(&mut msg, null_mut(), 0, 0) > 0 {
49                TranslateMessage(&msg);
50                DispatchMessageW(&msg);
51            }
52        }
53    }
54
55    extern "system" fn def_window_proc(hwnd: HWND, msg: u32, w_param: usize, l_param: isize) -> isize {
56        match msg {
57            WM_DESTROY => {
58                unsafe { PostQuitMessage(0) };
59                0
60            }
61            _ => unsafe { DefWindowProcW(hwnd, msg, w_param, l_param) },
62        }
63    }
64
65    fn wide_null(s: &str) -> Vec<u16> {
66        s.encode_utf16().chain(std::iter::once(0)).collect()
67    }
68}
69
70//linux
71#[cfg(target_os = "linux")]
72mod platform {
73    use std::ptr;
74    use x11::xlib::*;
75
76    pub fn create_window(title: &str, width: u32, height: u32) {
77        unsafe {
78            let display = XOpenDisplay(ptr::null());
79            if display.is_null() {
80                panic!("Cannot open X display");
81            }
82
83            let screen = XDefaultScreen(display);
84            let root = XRootWindow(display, screen);
85
86            let window = XCreateSimpleWindow(
87                display,
88                root,
89                0,
90                0,
91                width,
92                height,
93                1,
94                XBlackPixel(display, screen),
95                XWhitePixel(display, screen),
96            );
97
98            XStoreName(display, window, title.as_ptr() as *const i8);
99            XSelectInput(display, window, ExposureMask | KeyPressMask);
100            XMapWindow(display, window);
101
102            let mut event = std::mem::zeroed();
103            loop {
104                XNextEvent(display, &mut event);
105                if event.type_ == KeyPress {
106                    break;
107                }
108            }
109
110            XDestroyWindow(display, window);
111            XCloseDisplay(display);
112        }
113    }
114}