From 298e535de22782a8585f76c8c002f68b94e89c4d Mon Sep 17 00:00:00 2001 From: Daniel Collin Date: Tue, 24 Nov 2015 21:46:52 +0100 Subject: [PATCH] Started to work on Rust only version for Windows --- Cargo.toml | 1 + build.rs | 6 +++--- src/lib.rs | 8 ++++++++ src/windows.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/windows.rs diff --git a/Cargo.toml b/Cargo.toml index 86c6be6..9d79e7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ gcc = "0.3.19" [dependencies] libc = "0.1.10" +user32-sys = "0.1.2" diff --git a/build.rs b/build.rs index 308dd66..d8317c4 100644 --- a/build.rs +++ b/build.rs @@ -8,9 +8,9 @@ fn main() { &["src/native/macosx/MacMiniFB.m", "src/native/macosx/OSXWindow.m", "src/native/macosx/OSXWindowFrameView.m"]); // MacOS - } else if env.contains("windows") { - gcc::compile_library("libminifb_native.a", &["src/native/windows/WinMiniFB.c"]); // Windows - } else { + // } else if env.contains("windows") { + // gcc::compile_library("libminifb_native.a", &["src/native/windows/WinMiniFB.c"]); // Windows + } else if env.contains("linux") { gcc::compile_library("libminifb_native.a", &["src/native/x11/X11MiniFB.c"]); // Unix } } diff --git a/src/lib.rs b/src/lib.rs index 63dc6a0..72aa947 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,9 @@ use std::ffi::CString; use std::mem::transmute; use libc::{c_char, c_int, c_void}; +#[cfg(target_os = "windows")] +pub mod windows; + #[cfg(target_os = "macos")] #[link(name = "Cocoa", kind = "framework")] extern { @@ -11,6 +14,7 @@ extern { fn mfb_close(); } +/* #[cfg(target_os = "windows")] #[link(name = "gdi32")] extern { @@ -18,6 +22,7 @@ extern { fn mfb_update(buffer: *mut c_void) -> c_int; fn mfb_close(); } +*/ #[cfg(target_os = "linux")] #[link(name = "X11")] @@ -30,6 +35,7 @@ extern { /// /// Open up a window /// +#[cfg(any(target_os = "linux", target_os = "mac"))] pub fn open(name: &str, width: usize, height: usize) -> bool { let s = CString::new(name).unwrap(); let ret; @@ -47,6 +53,7 @@ pub fn open(name: &str, width: usize, height: usize) -> bool { /// /// Update /// +#[cfg(any(target_os = "linux", target_os = "mac"))] pub fn update(buffer: &[u32]) -> bool { let ret; unsafe { @@ -63,6 +70,7 @@ pub fn update(buffer: &[u32]) -> bool { /// /// Close /// +#[cfg(any(target_os = "linux", target_os = "mac"))] pub fn close() { unsafe { mfb_close(); diff --git a/src/windows.rs b/src/windows.rs new file mode 100644 index 0000000..dcd69e9 --- /dev/null +++ b/src/windows.rs @@ -0,0 +1,47 @@ +extern crate user; + +pub fn open(name: &str, width: usize, height: usize) -> bool { + let s = CString::new(name).unwrap(); + + unsafe { + let class = winapi::WNDCLASSEXA { + cbSize: mem::size_of::() as winapi::UINT, + style: winapi::CS_HREDRAW | winapi::CS_VREDRAW | winapi::CS_OWNDC, + lpfnWndProc: Some(callback::callback), + cbClsExtra: 0, + cbWndExtra: 0, + hInstance: kernel32::GetModuleHandleA(ptr::null()), + hIcon: ptr::null_mut(), + hCursor: ptr::null_mut(), + hbrBackground: ptr::null_mut(), + lpszMenuName: ptr::null(), + lpszClassName: s.as_ptr(), + hIconSm: ptr::null_mut(), + }; + + user32::RegisterClassExA(&class); + + let mut rect = winapi::RECT { + left: 0, right: width as winapi::LONG, + top: 0, bottom: height as winapi::LONG, + } + + user32::AdjustWindowRect(&mut rect, winapi::WS_POPUP | winapi::WS_SYSMENU | winapi::WS_CAPTION, 0); + + rect.right -= rect.left; + rect.bottom -= rect.top; + + let handle = user32::CreateWindowExA(0, + s.as_ptr(), s.as_ptr(), + winapi::WS_OVERLAPPEDWINDOW & ~winapi::WS_MAXIMIZEBOX & ~winapi::WS_TICKFRAME, + winapi::CW_USEDEFAULT, winapi::CW_USEDEFAULT, + rect.right, rect.bottom, + ptr::null_mut(), ptr::null_mut(), ptr::null_mut(), ptr::null_mut()); + + if handle.is_null() { + return false; + } + + user32::ShowWindow(handle, winapi::SW_NORMAL); +} +