Update winapi (#69)

* Remove duplicate windows dependencies in favor of cfg attribute

* Replace user32 with winuser from winapi

* More replacements from winapi-rs

* Fix remaining type imports

* Remove user32, kernel32 and gdi32 sys crates

Everything comes from winapi-rs now.

* Specify only the features of winapi-rs we need

* Remove star glob imports
This commit is contained in:
Richard Hozák 2019-03-21 09:55:06 +01:00 committed by Daniel Collin
parent ac624f5a26
commit 6c4cba9895
2 changed files with 202 additions and 223 deletions

View file

@ -21,29 +21,14 @@ cc = "1.0"
[dependencies] [dependencies]
time = "0.1.34" time = "0.1.34"
[target.x86_64-pc-windows-msvc.dependencies] [target.'cfg(windows)'.dependencies.winapi]
user32-sys = "0.1.2" version = "0.3"
winapi = "0.2.4" features = [
kernel32-sys = "0.2.2" "winuser",
gdi32-sys = "0.1.1" "wingdi",
"libloaderapi",
[target.x86_64-pc-windows-gnu.dependencies] "errhandlingapi"
user32-sys = "0.1.2" ]
winapi = "0.2.4"
kernel32-sys = "0.2.2"
gdi32-sys = "0.1.1"
[target.i686-pc-windows-msvc.dependencies]
user32-sys = "0.1.2"
winapi = "0.2.4"
kernel32-sys = "0.2.2"
gdi32-sys = "0.1.1"
[target.i686-pc-windows-gnu.dependencies]
user32-sys = "0.1.2"
winapi = "0.2.4"
kernel32-sys = "0.2.2"
gdi32-sys = "0.1.1"
[target.i686-unknown-linux-gnu.dependencies] [target.i686-unknown-linux-gnu.dependencies]
x11-dl = "~2.14" x11-dl = "~2.14"

View file

@ -1,9 +1,6 @@
#![cfg(target_os = "windows")] #![cfg(target_os = "windows")]
extern crate user32;
extern crate kernel32;
extern crate winapi; extern crate winapi;
extern crate gdi32;
extern crate time; extern crate time;
const INVALID_ACCEL: usize = 0xffffffff; const INVALID_ACCEL: usize = 0xffffffff;
@ -23,22 +20,20 @@ use std::os::raw;
use mouse_handler; use mouse_handler;
use buffer_helper; use buffer_helper;
//use self::winapi::windef::HWND; use self::winapi::shared::basetsd;
//use self::winapi::windef::HDC; use self::winapi::um::winuser;
//use self::winapi::windef::HMENU; use self::winapi::shared::minwindef;
//use self::winapi::wingdi::BITMAPINFOHEADER; use self::winapi::shared::windef;
//use self::winapi::wingdi::RGBQUAD; use self::winapi::um::wingdi;
//use self::winapi::winuser::WNDCLASSW; use self::winapi::shared::ntdef;
//use self::winapi::winuser::ACCEL; use self::winapi::um::libloaderapi;
//use self::winapi::basetsd::UINT_PTR; use self::winapi::um::errhandlingapi;
use self::winapi::*;
//use self::winapi::winmindefs::BYTE;
// Wrap this so we can have a proper numbef of bmiColors to write in // Wrap this so we can have a proper numbef of bmiColors to write in
#[repr(C)] #[repr(C)]
struct BitmapInfo { struct BitmapInfo {
pub bmi_header: BITMAPINFOHEADER, pub bmi_header: wingdi::BITMAPINFOHEADER,
pub bmi_colors: [RGBQUAD; 3], pub bmi_colors: [wingdi::RGBQUAD; 3],
} }
fn update_key_state(window: &mut Window, wparam: u32, state: bool) { fn update_key_state(window: &mut Window, wparam: u32, state: bool) {
@ -153,44 +148,44 @@ fn char_down(window: &mut Window, code_point: u32) {
} }
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
unsafe fn set_window_long(window: winapi::HWND, data: winapi::LONG_PTR) -> winapi::LONG_PTR { unsafe fn set_window_long(window: windef::HWND, data: basetsd::LONG_PTR) -> basetsd::LONG_PTR {
user32::SetWindowLongPtrW(window, winapi::winuser::GWLP_USERDATA, data) winuser::SetWindowLongPtrW(window, winuser::GWLP_USERDATA, data)
} }
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
unsafe fn get_window_long(window: winapi::HWND) -> winapi::LONG_PTR { unsafe fn get_window_long(window: windef::HWND) -> basetsd::LONG_PTR {
user32::GetWindowLongPtrW(window, winapi::winuser::GWLP_USERDATA) winuser::GetWindowLongPtrW(window, winuser::GWLP_USERDATA)
} }
#[cfg(target_arch = "x86")] #[cfg(target_arch = "x86")]
unsafe fn set_window_long(window: winapi::HWND, data: winapi::LONG) -> winapi::LONG { unsafe fn set_window_long(window: windef::HWND, data: ntdef::LONG) -> ntdef::LONG {
user32::SetWindowLongW(window, winapi::winuser::GWLP_USERDATA, data) winuser::SetWindowLongW(window, winuser::GWLP_USERDATA, data)
} }
#[cfg(target_arch = "x86")] #[cfg(target_arch = "x86")]
unsafe fn get_window_long(window: winapi::HWND) -> winapi::LONG { unsafe fn get_window_long(window: windef::HWND) -> ntdef::LONG {
user32::GetWindowLongW(window, winapi::winuser::GWLP_USERDATA) winuser::GetWindowLongW(window, winuser::GWLP_USERDATA)
} }
unsafe extern "system" fn wnd_proc(window: winapi::HWND, unsafe extern "system" fn wnd_proc(window: windef::HWND,
msg: winapi::UINT, msg: minwindef::UINT,
wparam: winapi::WPARAM, wparam: minwindef::WPARAM,
lparam: winapi::LPARAM) lparam: minwindef::LPARAM)
-> winapi::LRESULT { -> minwindef::LRESULT {
// This make sure we actually don't do anything before the user data has been setup for the // This make sure we actually don't do anything before the user data has been setup for the
// window // window
let user_data = get_window_long(window); let user_data = get_window_long(window);
if user_data == 0 { if user_data == 0 {
return user32::DefWindowProcW(window, msg, wparam, lparam); return winuser::DefWindowProcW(window, msg, wparam, lparam);
} }
let mut wnd: &mut Window = mem::transmute(user_data); let mut wnd: &mut Window = mem::transmute(user_data);
match msg { match msg {
/* /*
winapi::winuser::WM_MOUSEMOVE => { winuser::WM_MOUSEMOVE => {
let mouse_coords = lparam as u32; let mouse_coords = lparam as u32;
let scale = user_data.scale as f32; let scale = user_data.scale as f32;
user_data.mouse.local_x = (((mouse_coords >> 16) & 0xffff) as f32) / scale; user_data.mouse.local_x = (((mouse_coords >> 16) & 0xffff) as f32) / scale;
@ -199,90 +194,90 @@ unsafe extern "system" fn wnd_proc(window: winapi::HWND,
return 0; return 0;
} }
*/ */
winapi::winuser::WM_MOUSEWHEEL => { winuser::WM_MOUSEWHEEL => {
let scroll = ((((wparam as u32) >> 16) & 0xffff) as i16) as f32 * 0.1; let scroll = ((((wparam as u32) >> 16) & 0xffff) as i16) as f32 * 0.1;
wnd.mouse.scroll = scroll; wnd.mouse.scroll = scroll;
} }
winapi::winuser::WM_KEYDOWN => { winuser::WM_KEYDOWN => {
update_key_state(wnd, (lparam as u32) >> 16, true); update_key_state(wnd, (lparam as u32) >> 16, true);
return 0; return 0;
} }
winapi::winuser::WM_CHAR => { winuser::WM_CHAR => {
char_down(wnd, wparam as u32); char_down(wnd, wparam as u32);
} }
winapi::winuser::WM_SYSCHAR => { winuser::WM_SYSCHAR => {
char_down(wnd, wparam as u32); char_down(wnd, wparam as u32);
} }
winapi::winuser::WM_LBUTTONDOWN => { winuser::WM_LBUTTONDOWN => {
wnd.mouse.state[0] = true wnd.mouse.state[0] = true
} }
winapi::winuser::WM_LBUTTONUP => { winuser::WM_LBUTTONUP => {
wnd.mouse.state[0] = false wnd.mouse.state[0] = false
} }
winapi::winuser::WM_MBUTTONDOWN => { winuser::WM_MBUTTONDOWN => {
wnd.mouse.state[1] = true wnd.mouse.state[1] = true
} }
winapi::winuser::WM_MBUTTONUP => { winuser::WM_MBUTTONUP => {
wnd.mouse.state[1] = false wnd.mouse.state[1] = false
} }
winapi::winuser::WM_RBUTTONDOWN => { winuser::WM_RBUTTONDOWN => {
wnd.mouse.state[2] = true wnd.mouse.state[2] = true
} }
winapi::winuser::WM_RBUTTONUP => { winuser::WM_RBUTTONUP => {
wnd.mouse.state[2] = false wnd.mouse.state[2] = false
} }
winapi::winuser::WM_CLOSE => { winuser::WM_CLOSE => {
wnd.is_open = false; wnd.is_open = false;
} }
winapi::winuser::WM_KEYUP => { winuser::WM_KEYUP => {
update_key_state(wnd, (lparam as u32) >> 16, false); update_key_state(wnd, (lparam as u32) >> 16, false);
return 0; return 0;
} }
winapi::winuser::WM_COMMAND => { winuser::WM_COMMAND => {
if lparam == 0 { if lparam == 0 {
wnd.accel_key = (wparam & 0xffff) as usize; wnd.accel_key = (wparam & 0xffff) as usize;
} }
} }
winapi::winuser::WM_SIZE => { winuser::WM_SIZE => {
let width = (lparam as u32) & 0xffff; let width = (lparam as u32) & 0xffff;
let height = ((lparam as u32) >> 16) & 0xffff; let height = ((lparam as u32) >> 16) & 0xffff;
wnd.width = width as i32; wnd.width = width as i32;
wnd.height = height as i32; wnd.height = height as i32;
} }
winapi::winuser::WM_PAINT => { winuser::WM_PAINT => {
// if we have nothing to draw here we return the default function // if we have nothing to draw here we return the default function
if wnd.buffer.len() == 0 { if wnd.buffer.len() == 0 {
return user32::DefWindowProcW(window, msg, wparam, lparam); return winuser::DefWindowProcW(window, msg, wparam, lparam);
} }
let mut bitmap_info: BitmapInfo = mem::zeroed(); let mut bitmap_info: BitmapInfo = mem::zeroed();
bitmap_info.bmi_header.biSize = mem::size_of::<BITMAPINFOHEADER>() as u32; bitmap_info.bmi_header.biSize = mem::size_of::<wingdi::BITMAPINFOHEADER>() as u32;
bitmap_info.bmi_header.biPlanes = 1; bitmap_info.bmi_header.biPlanes = 1;
bitmap_info.bmi_header.biBitCount = 32; bitmap_info.bmi_header.biBitCount = 32;
bitmap_info.bmi_header.biCompression = winapi::wingdi::BI_BITFIELDS; bitmap_info.bmi_header.biCompression = wingdi::BI_BITFIELDS;
bitmap_info.bmi_header.biWidth = wnd.width; bitmap_info.bmi_header.biWidth = wnd.width;
bitmap_info.bmi_header.biHeight = -wnd.height; bitmap_info.bmi_header.biHeight = -wnd.height;
bitmap_info.bmi_colors[0].rgbRed = 0xff; bitmap_info.bmi_colors[0].rgbRed = 0xff;
bitmap_info.bmi_colors[1].rgbGreen = 0xff; bitmap_info.bmi_colors[1].rgbGreen = 0xff;
bitmap_info.bmi_colors[2].rgbBlue = 0xff; bitmap_info.bmi_colors[2].rgbBlue = 0xff;
gdi32::StretchDIBits(wnd.dc.unwrap(), wingdi::StretchDIBits(wnd.dc.unwrap(),
0, 0,
0, 0,
wnd.width * wnd.scale_factor, wnd.width * wnd.scale_factor,
@ -293,10 +288,10 @@ unsafe extern "system" fn wnd_proc(window: winapi::HWND,
wnd.height, wnd.height,
mem::transmute(wnd.buffer.as_ptr()), mem::transmute(wnd.buffer.as_ptr()),
mem::transmute(&bitmap_info), mem::transmute(&bitmap_info),
winapi::wingdi::DIB_RGB_COLORS, wingdi::DIB_RGB_COLORS,
winapi::wingdi::SRCCOPY); wingdi::SRCCOPY);
user32::ValidateRect(window, ptr::null_mut()); winuser::ValidateRect(window, ptr::null_mut());
return 0; return 0;
} }
@ -304,7 +299,7 @@ unsafe extern "system" fn wnd_proc(window: winapi::HWND,
_ => (), _ => (),
} }
return user32::DefWindowProcW(window, msg, wparam, lparam); return winuser::DefWindowProcW(window, msg, wparam, lparam);
} }
pub enum MinifbError { pub enum MinifbError {
@ -334,8 +329,8 @@ struct MenuStore {
pub struct Window { pub struct Window {
mouse: MouseData, mouse: MouseData,
dc: Option<HDC>, dc: Option<windef::HDC>,
window: Option<HWND>, window: Option<windef::HWND>,
buffer: Vec<u32>, buffer: Vec<u32>,
is_open : bool, is_open : bool,
scale_factor: i32, scale_factor: i32,
@ -343,31 +338,30 @@ pub struct Window {
height: i32, height: i32,
menus: Vec<Menu>, menus: Vec<Menu>,
key_handler: KeyHandler, key_handler: KeyHandler,
accel_table: HACCEL, accel_table: windef::HACCEL,
accel_key: usize, accel_key: usize,
prev_cursor: CursorStyle, prev_cursor: CursorStyle,
cursors: [winapi::HCURSOR; 8], cursors: [windef::HCURSOR; 8],
} }
// TranslateAccelerator is currently missing in win-rs // TranslateAccelerator is currently missing in win-rs
#[cfg(target_family = "windows")] // #[cfg(target_family = "windows")]
#[link(name = "user32")] // #[link(name = "user32")]
#[allow(non_snake_case)] // #[allow(non_snake_case)]
extern "system" { // extern "system" {
fn TranslateAcceleratorW(hWnd: HWND, accel: *const ACCEL, pmsg: *const MSG) -> INT; // fn RemoveMenu(menu: HMENU, pos: UINT, flags: UINT) -> BOOL;
fn RemoveMenu(menu: HMENU, pos: UINT, flags: UINT) -> BOOL; // }
}
impl Window { impl Window {
fn open_window(name: &str, width: usize, height: usize, opts: WindowOptions, scale_factor: i32) -> Option<HWND> { fn open_window(name: &str, width: usize, height: usize, opts: WindowOptions, scale_factor: i32) -> Option<windef::HWND> {
unsafe { unsafe {
let class_name = to_wstring("minifb_window"); let class_name = to_wstring("minifb_window");
let class = WNDCLASSW { let class = winuser::WNDCLASSW {
style: winapi::CS_HREDRAW | winapi::CS_VREDRAW | winapi::CS_OWNDC, style: winuser::CS_HREDRAW | winuser::CS_VREDRAW | winuser::CS_OWNDC,
lpfnWndProc: Some(wnd_proc), lpfnWndProc: Some(wnd_proc),
cbClsExtra: 0, cbClsExtra: 0,
cbWndExtra: 0, cbWndExtra: 0,
hInstance: kernel32::GetModuleHandleA(ptr::null()), hInstance: libloaderapi::GetModuleHandleA(ptr::null()),
hIcon: ptr::null_mut(), hIcon: ptr::null_mut(),
hCursor: ptr::null_mut(), hCursor: ptr::null_mut(),
hbrBackground: ptr::null_mut(), hbrBackground: ptr::null_mut(),
@ -375,10 +369,10 @@ impl Window {
lpszClassName: class_name.as_ptr(), lpszClassName: class_name.as_ptr(),
}; };
if user32::RegisterClassW(&class) == 0 { if winuser::RegisterClassW(&class) == 0 {
// ignore the "Class already exists" error for multiple windows // ignore the "Class already exists" error for multiple windows
if kernel32::GetLastError() as u32 != 1410 { if errhandlingapi::GetLastError() as u32 != 1410 {
println!("Unable to register class, error {}", kernel32::GetLastError() as u32); println!("Unable to register class, error {}", errhandlingapi::GetLastError() as u32);
return None; return None;
} }
} }
@ -386,15 +380,15 @@ impl Window {
let new_width = width * scale_factor as usize; let new_width = width * scale_factor as usize;
let new_height = height * scale_factor as usize; let new_height = height * scale_factor as usize;
let mut rect = winapi::RECT { let mut rect = windef::RECT {
left: 0, left: 0,
right: new_width as winapi::LONG, right: new_width as ntdef::LONG,
top: 0, top: 0,
bottom: new_height as winapi::LONG, bottom: new_height as ntdef::LONG,
}; };
user32::AdjustWindowRect(&mut rect, winuser::AdjustWindowRect(&mut rect,
winapi::WS_POPUP | winapi::WS_SYSMENU | winapi::WS_CAPTION, winuser::WS_POPUP | winuser::WS_SYSMENU | winuser::WS_CAPTION,
0); 0);
rect.right -= rect.left; rect.right -= rect.left;
@ -405,27 +399,27 @@ impl Window {
let mut flags = 0; let mut flags = 0;
if opts.title { if opts.title {
flags |= winapi::WS_OVERLAPPEDWINDOW as u32; flags |= winuser::WS_OVERLAPPEDWINDOW as u32;
} }
if opts.resize { if opts.resize {
flags |= winapi::WS_THICKFRAME as u32 | winapi::WS_MAXIMIZEBOX as u32 ; flags |= winuser::WS_THICKFRAME as u32 | winuser::WS_MAXIMIZEBOX as u32 ;
} else { } else {
flags &= !winapi::WS_MAXIMIZEBOX; flags &= !winuser::WS_MAXIMIZEBOX;
flags &= !winapi::WS_THICKFRAME; flags &= !winuser::WS_THICKFRAME;
} }
if opts.borderless { if opts.borderless {
flags &= !winapi::WS_THICKFRAME; flags &= !winuser::WS_THICKFRAME;
} }
let handle = user32::CreateWindowExW(0, let handle = winuser::CreateWindowExW(0,
class_name.as_ptr(), class_name.as_ptr(),
window_name.as_ptr(), window_name.as_ptr(),
flags, flags,
winapi::CW_USEDEFAULT, winuser::CW_USEDEFAULT,
winapi::CW_USEDEFAULT, winuser::CW_USEDEFAULT,
rect.right, rect.right,
rect.bottom, rect.bottom,
ptr::null_mut(), ptr::null_mut(),
@ -433,11 +427,11 @@ impl Window {
ptr::null_mut(), ptr::null_mut(),
ptr::null_mut()); ptr::null_mut());
if handle.is_null() { if handle.is_null() {
println!("Unable to create window, error {}", kernel32::GetLastError() as u32); println!("Unable to create window, error {}", errhandlingapi::GetLastError() as u32);
return None; return None;
} }
user32::ShowWindow(handle, winapi::SW_NORMAL); winuser::ShowWindow(handle, winuser::SW_NORMAL);
return Some(handle); return Some(handle);
} }
@ -459,7 +453,7 @@ impl Window {
let window = Window { let window = Window {
mouse: MouseData::default(), mouse: MouseData::default(),
dc: Some(user32::GetDC(handle.unwrap())), dc: Some(winuser::GetDC(handle.unwrap())),
window: Some(handle.unwrap()), window: Some(handle.unwrap()),
buffer: Vec::new(), buffer: Vec::new(),
key_handler: KeyHandler::new(), key_handler: KeyHandler::new(),
@ -472,14 +466,14 @@ impl Window {
accel_key: INVALID_ACCEL, accel_key: INVALID_ACCEL,
prev_cursor: CursorStyle::Arrow, prev_cursor: CursorStyle::Arrow,
cursors: [ cursors: [
user32::LoadCursorW(ptr::null_mut(), winuser::IDC_ARROW), winuser::LoadCursorW(ptr::null_mut(), winuser::IDC_ARROW),
user32::LoadCursorW(ptr::null_mut(), winuser::IDC_IBEAM), winuser::LoadCursorW(ptr::null_mut(), winuser::IDC_IBEAM),
user32::LoadCursorW(ptr::null_mut(), winuser::IDC_CROSS), winuser::LoadCursorW(ptr::null_mut(), winuser::IDC_CROSS),
user32::LoadCursorW(ptr::null_mut(), winuser::IDC_HAND), winuser::LoadCursorW(ptr::null_mut(), winuser::IDC_HAND),
user32::LoadCursorW(ptr::null_mut(), winuser::IDC_HAND), winuser::LoadCursorW(ptr::null_mut(), winuser::IDC_HAND),
user32::LoadCursorW(ptr::null_mut(), winuser::IDC_SIZEWE), winuser::LoadCursorW(ptr::null_mut(), winuser::IDC_SIZEWE),
user32::LoadCursorW(ptr::null_mut(), winuser::IDC_SIZENS), winuser::LoadCursorW(ptr::null_mut(), winuser::IDC_SIZENS),
user32::LoadCursorW(ptr::null_mut(), winuser::IDC_SIZEALL), winuser::LoadCursorW(ptr::null_mut(), winuser::IDC_SIZEALL),
], ],
}; };
@ -491,7 +485,7 @@ impl Window {
pub fn set_title(&mut self, title: &str) { pub fn set_title(&mut self, title: &str) {
unsafe { unsafe {
let title_name = to_wstring(title); let title_name = to_wstring(title);
user32::SetWindowTextW(self.window.unwrap(), title_name.as_ptr()); winuser::SetWindowTextW(self.window.unwrap(), title_name.as_ptr());
} }
} }
@ -503,8 +497,8 @@ impl Window {
#[inline] #[inline]
pub fn set_position(&mut self, x: isize, y: isize) { pub fn set_position(&mut self, x: isize, y: isize) {
unsafe { unsafe {
user32::SetWindowPos(self.window.unwrap(), ptr::null_mut(), x as i32, y as i32, winuser::SetWindowPos(self.window.unwrap(), ptr::null_mut(), x as i32, y as i32,
0, 0, winapi::SWP_SHOWWINDOW | winapi::SWP_NOSIZE); 0, 0, winuser::SWP_SHOWWINDOW | winuser::SWP_NOSIZE);
} }
} }
@ -554,7 +548,7 @@ impl Window {
return; return;
} }
user32::SetCursor(self.cursors[cursor as usize]); winuser::SetCursor(self.cursors[cursor as usize]);
self.prev_cursor = cursor; self.prev_cursor = cursor;
} }
@ -605,11 +599,11 @@ impl Window {
return self.is_open return self.is_open
} }
fn generic_update(&mut self, window: HWND) { fn generic_update(&mut self, window: windef::HWND) {
unsafe { unsafe {
let mut point: winapi::POINT = mem::uninitialized(); let mut point: windef::POINT = mem::uninitialized();
user32::GetCursorPos(&mut point); winuser::GetCursorPos(&mut point);
user32::ScreenToClient(window, &mut point); winuser::ScreenToClient(window, &mut point);
self.mouse.x = point.x as f32; self.mouse.x = point.x as f32;
self.mouse.y = point.y as f32; self.mouse.y = point.y as f32;
@ -621,19 +615,19 @@ impl Window {
} }
} }
fn message_loop(&self, window: HWND) { fn message_loop(&self, window: windef::HWND) {
unsafe { unsafe {
let mut msg = mem::uninitialized(); let mut msg = mem::uninitialized();
while user32::PeekMessageW(&mut msg, window, 0, 0, winapi::winuser::PM_REMOVE) != 0 { while winuser::PeekMessageW(&mut msg, window, 0, 0, winuser::PM_REMOVE) != 0 {
// Make this code a bit nicer // Make this code a bit nicer
if self.accel_table == ptr::null_mut() { if self.accel_table == ptr::null_mut() {
user32::TranslateMessage(&mut msg); winuser::TranslateMessage(&mut msg);
user32::DispatchMessageW(&mut msg); winuser::DispatchMessageW(&mut msg);
} else { } else {
if TranslateAcceleratorW(msg.hwnd, mem::transmute(self.accel_table), &mut msg) == 0 { if winuser::TranslateAcceleratorW(msg.hwnd, mem::transmute(self.accel_table), &mut msg) == 0 {
user32::TranslateMessage(&mut msg); winuser::TranslateMessage(&mut msg);
user32::DispatchMessageW(&mut msg); winuser::DispatchMessageW(&mut msg);
} }
} }
} }
@ -655,7 +649,7 @@ impl Window {
self.buffer = buffer.to_vec(); self.buffer = buffer.to_vec();
unsafe { unsafe {
user32::InvalidateRect(window, ptr::null_mut(), winapi::TRUE); winuser::InvalidateRect(window, ptr::null_mut(), minwindef::TRUE);
} }
Self::message_loop(self, window); Self::message_loop(self, window);
@ -685,8 +679,8 @@ impl Window {
Scale::X16 => 16, Scale::X16 => 16,
Scale::X32 => 32, Scale::X32 => 32,
Scale::FitScreen => { Scale::FitScreen => {
let screen_x = user32::GetSystemMetrics(winapi::winuser::SM_CXSCREEN) as i32; let screen_x = winuser::GetSystemMetrics(winuser::SM_CXSCREEN) as i32;
let screen_y = user32::GetSystemMetrics(winapi::winuser::SM_CYSCREEN) as i32; let screen_y = winuser::GetSystemMetrics(winuser::SM_CYSCREEN) as i32;
let mut scale = 1i32; let mut scale = 1i32;
@ -712,13 +706,13 @@ impl Window {
// When attaching a menu to the window we need to resize it so // When attaching a menu to the window we need to resize it so
// the current client size is preserved and still show all pixels // the current client size is preserved and still show all pixels
// //
unsafe fn adjust_window_size_for_menu(handle: HWND) { unsafe fn adjust_window_size_for_menu(handle: windef::HWND) {
let mut rect: winapi::RECT = mem::uninitialized(); let mut rect: windef::RECT = mem::uninitialized();
let menu_height = user32::GetSystemMetrics(winapi::winuser::SM_CYMENU); let menu_height = winuser::GetSystemMetrics(winuser::SM_CYMENU);
user32::GetWindowRect(handle, &mut rect); winuser::GetWindowRect(handle, &mut rect);
user32::MoveWindow(handle, winuser::MoveWindow(handle,
rect.left, rect.left,
rect.top, rect.top,
rect.right - rect.left, rect.right - rect.left,
@ -727,7 +721,7 @@ impl Window {
} }
unsafe fn set_accel_table(&mut self) { unsafe fn set_accel_table(&mut self) {
let mut temp_accel_table = Vec::<ACCEL>::new(); let mut temp_accel_table = Vec::<winuser::ACCEL>::new();
for menu in self.menus.iter() { for menu in self.menus.iter() {
for item in menu.accel_table.iter() { for item in menu.accel_table.iter() {
@ -737,10 +731,10 @@ impl Window {
} }
if self.accel_table != ptr::null_mut() { if self.accel_table != ptr::null_mut() {
user32::DestroyAcceleratorTable(self.accel_table); winuser::DestroyAcceleratorTable(self.accel_table);
} }
self.accel_table = user32::CreateAcceleratorTableW(temp_accel_table.as_mut_ptr(), self.accel_table = winuser::CreateAcceleratorTableW(temp_accel_table.as_mut_ptr(),
temp_accel_table.len() as i32); temp_accel_table.len() as i32);
println!("accel {:?}", self.accel_table); println!("accel {:?}", self.accel_table);
@ -750,17 +744,17 @@ impl Window {
pub fn add_menu(&mut self, menu: &Menu) -> MenuHandle { pub fn add_menu(&mut self, menu: &Menu) -> MenuHandle {
unsafe { unsafe {
let window = self.window.unwrap(); let window = self.window.unwrap();
let mut main_menu = user32::GetMenu(window); let mut main_menu = winuser::GetMenu(window);
if main_menu == ptr::null_mut() { if main_menu == ptr::null_mut() {
main_menu = user32::CreateMenu(); main_menu = winuser::CreateMenu();
user32::SetMenu(window, main_menu); winuser::SetMenu(window, main_menu);
Self::adjust_window_size_for_menu(window); Self::adjust_window_size_for_menu(window);
} }
user32::AppendMenuW(main_menu, winuser::AppendMenuW(main_menu,
0x10, 0x10,
menu.menu_handle as UINT_PTR, menu.menu_handle as basetsd::UINT_PTR,
menu.name.as_ptr()); menu.name.as_ptr());
self.menus.push(menu.clone()); self.menus.push(menu.clone());
@ -769,7 +763,7 @@ impl Window {
//Self::add_menu_store(self, main_menu, menu_name, menu); //Self::add_menu_store(self, main_menu, menu_name, menu);
self.set_accel_table(); self.set_accel_table();
user32::DrawMenuBar(window); winuser::DrawMenuBar(window);
} }
// TODO: Proper handle // TODO: Proper handle
@ -779,13 +773,13 @@ impl Window {
pub fn remove_menu(&mut self, handle: MenuHandle) { pub fn remove_menu(&mut self, handle: MenuHandle) {
let window = self.window.unwrap(); let window = self.window.unwrap();
let main_menu = unsafe { user32::GetMenu(window) }; let main_menu = unsafe { winuser::GetMenu(window) };
for i in 0..self.menus.len() { for i in 0..self.menus.len() {
if self.menus[i].menu_handle == handle.0 as HMENU { if self.menus[i].menu_handle == handle.0 as windef::HMENU {
unsafe { unsafe {
println!("Removed menu at {}", i); println!("Removed menu at {}", i);
let _t = RemoveMenu(main_menu, i as UINT, 0); let _t = winuser::RemoveMenu(main_menu, i as minwindef::UINT, 0);
user32::DrawMenuBar(self.window.unwrap()); winuser::DrawMenuBar(self.window.unwrap());
} }
self.menus.swap_remove(i); self.menus.swap_remove(i);
return; return;
@ -806,16 +800,16 @@ impl Window {
#[derive(Clone)] #[derive(Clone)]
pub struct Menu { pub struct Menu {
menu_handle: HMENU, menu_handle: windef::HMENU,
name: Vec<u16>, name: Vec<u16>,
accel_table: Vec<ACCEL>, accel_table: Vec<winuser::ACCEL>,
} }
impl Menu { impl Menu {
pub fn new(name: &str) -> Result<Menu> { pub fn new(name: &str) -> Result<Menu> {
unsafe { unsafe {
Ok(Menu { Ok(Menu {
menu_handle: user32::CreatePopupMenu(), menu_handle: winuser::CreatePopupMenu(),
name: to_wstring(name), name: to_wstring(name),
accel_table: Vec::new(), accel_table: Vec::new(),
}) })
@ -862,74 +856,74 @@ impl Menu {
Key::Y => (0x59, "y"), Key::Y => (0x59, "y"),
Key::Z => (0x5a, "z"), Key::Z => (0x5a, "z"),
Key::F1 => (winapi::winuser::VK_F1, "F1"), Key::F1 => (winuser::VK_F1, "F1"),
Key::F2 => (winapi::winuser::VK_F2, "F2"), Key::F2 => (winuser::VK_F2, "F2"),
Key::F3 => (winapi::winuser::VK_F3, "F3"), Key::F3 => (winuser::VK_F3, "F3"),
Key::F4 => (winapi::winuser::VK_F4, "F4"), Key::F4 => (winuser::VK_F4, "F4"),
Key::F5 => (winapi::winuser::VK_F5, "F5"), Key::F5 => (winuser::VK_F5, "F5"),
Key::F6 => (winapi::winuser::VK_F6, "F6"), Key::F6 => (winuser::VK_F6, "F6"),
Key::F7 => (winapi::winuser::VK_F7, "F7"), Key::F7 => (winuser::VK_F7, "F7"),
Key::F8 => (winapi::winuser::VK_F8, "F8"), Key::F8 => (winuser::VK_F8, "F8"),
Key::F9 => (winapi::winuser::VK_F9, "F9"), Key::F9 => (winuser::VK_F9, "F9"),
Key::F10 => (winapi::winuser::VK_F10, "F10"), Key::F10 => (winuser::VK_F10, "F10"),
Key::F11 => (winapi::winuser::VK_F11, "F11"), Key::F11 => (winuser::VK_F11, "F11"),
Key::F12 => (winapi::winuser::VK_F12, "F12"), Key::F12 => (winuser::VK_F12, "F12"),
Key::F13 => (winapi::winuser::VK_F13, "F14"), Key::F13 => (winuser::VK_F13, "F14"),
Key::F14 => (winapi::winuser::VK_F14, "F14"), Key::F14 => (winuser::VK_F14, "F14"),
Key::F15 => (winapi::winuser::VK_F15, "F15"), Key::F15 => (winuser::VK_F15, "F15"),
Key::Down => (winapi::winuser::VK_DOWN, "Down"), Key::Down => (winuser::VK_DOWN, "Down"),
Key::Left => (winapi::winuser::VK_LEFT, "Left"), Key::Left => (winuser::VK_LEFT, "Left"),
Key::Right => (winapi::winuser::VK_RIGHT, "Right"), Key::Right => (winuser::VK_RIGHT, "Right"),
Key::Up => (winapi::winuser::VK_UP, "Up"), Key::Up => (winuser::VK_UP, "Up"),
Key::Backslash => (winapi::winuser::VK_OEM_102, "Backslash"), Key::Backslash => (winuser::VK_OEM_102, "Backslash"),
Key::Comma => (winapi::winuser::VK_OEM_COMMA, ","), Key::Comma => (winuser::VK_OEM_COMMA, ","),
Key::Minus => (winapi::winuser::VK_OEM_MINUS, "-"), Key::Minus => (winuser::VK_OEM_MINUS, "-"),
Key::Period => (winapi::winuser::VK_OEM_PERIOD, "."), Key::Period => (winuser::VK_OEM_PERIOD, "."),
Key::Backspace => (winapi::winuser::VK_BACK, "Back"), Key::Backspace => (winuser::VK_BACK, "Back"),
Key::Delete => (winapi::winuser::VK_DELETE, "Delete"), Key::Delete => (winuser::VK_DELETE, "Delete"),
Key::End => (winapi::winuser::VK_END, "End"), Key::End => (winuser::VK_END, "End"),
Key::Enter => (winapi::winuser::VK_RETURN, "Enter"), Key::Enter => (winuser::VK_RETURN, "Enter"),
Key::Escape => (winapi::winuser::VK_ESCAPE, "Esc"), Key::Escape => (winuser::VK_ESCAPE, "Esc"),
Key::Home => (winapi::winuser::VK_HOME, "Home"), Key::Home => (winuser::VK_HOME, "Home"),
Key::Insert => (winapi::winuser::VK_INSERT, "Insert"), Key::Insert => (winuser::VK_INSERT, "Insert"),
Key::Menu => (winapi::winuser::VK_MENU, "Menu"), Key::Menu => (winuser::VK_MENU, "Menu"),
Key::PageDown => (winapi::winuser::VK_NEXT, "PageDown"), Key::PageDown => (winuser::VK_NEXT, "PageDown"),
Key::PageUp => (winapi::winuser::VK_PRIOR, "PageUp"), Key::PageUp => (winuser::VK_PRIOR, "PageUp"),
Key::Pause => (winapi::winuser::VK_PAUSE, "Pause"), Key::Pause => (winuser::VK_PAUSE, "Pause"),
Key::Space => (winapi::winuser::VK_SPACE, "Space"), Key::Space => (winuser::VK_SPACE, "Space"),
Key::Tab => (winapi::winuser::VK_TAB, "Tab"), Key::Tab => (winuser::VK_TAB, "Tab"),
Key::NumLock => (winapi::winuser::VK_NUMLOCK, "NumLock"), Key::NumLock => (winuser::VK_NUMLOCK, "NumLock"),
Key::CapsLock => (winapi::winuser::VK_CAPITAL, "CapsLock"), Key::CapsLock => (winuser::VK_CAPITAL, "CapsLock"),
Key::ScrollLock => (winapi::winuser::VK_SCROLL, "Scroll"), Key::ScrollLock => (winuser::VK_SCROLL, "Scroll"),
Key::LeftShift => (winapi::winuser::VK_LSHIFT, "LeftShift"), Key::LeftShift => (winuser::VK_LSHIFT, "LeftShift"),
Key::RightShift => (winapi::winuser::VK_RSHIFT, "RightShift"), Key::RightShift => (winuser::VK_RSHIFT, "RightShift"),
Key::LeftCtrl => (winapi::winuser::VK_CONTROL, "Ctrl"), Key::LeftCtrl => (winuser::VK_CONTROL, "Ctrl"),
Key::RightCtrl => (winapi::winuser::VK_CONTROL, "Ctrl"), Key::RightCtrl => (winuser::VK_CONTROL, "Ctrl"),
Key::NumPad0 => (winapi::winuser::VK_NUMPAD0, "NumPad0"), Key::NumPad0 => (winuser::VK_NUMPAD0, "NumPad0"),
Key::NumPad1 => (winapi::winuser::VK_NUMPAD1, "NumPad1"), Key::NumPad1 => (winuser::VK_NUMPAD1, "NumPad1"),
Key::NumPad2 => (winapi::winuser::VK_NUMPAD2, "NumPad2"), Key::NumPad2 => (winuser::VK_NUMPAD2, "NumPad2"),
Key::NumPad3 => (winapi::winuser::VK_NUMPAD3, "NumPad3"), Key::NumPad3 => (winuser::VK_NUMPAD3, "NumPad3"),
Key::NumPad4 => (winapi::winuser::VK_NUMPAD4, "NumPad4"), Key::NumPad4 => (winuser::VK_NUMPAD4, "NumPad4"),
Key::NumPad5 => (winapi::winuser::VK_NUMPAD5, "NumPad5"), Key::NumPad5 => (winuser::VK_NUMPAD5, "NumPad5"),
Key::NumPad6 => (winapi::winuser::VK_NUMPAD6, "NumPad6"), Key::NumPad6 => (winuser::VK_NUMPAD6, "NumPad6"),
Key::NumPad7 => (winapi::winuser::VK_NUMPAD7, "NumPad7"), Key::NumPad7 => (winuser::VK_NUMPAD7, "NumPad7"),
Key::NumPad8 => (winapi::winuser::VK_NUMPAD8, "NumPad8"), Key::NumPad8 => (winuser::VK_NUMPAD8, "NumPad8"),
Key::NumPad9 => (winapi::winuser::VK_NUMPAD9, "NumPad9"), Key::NumPad9 => (winuser::VK_NUMPAD9, "NumPad9"),
Key::LeftAlt => (winapi::winuser::VK_MENU, "Alt"), Key::LeftAlt => (winuser::VK_MENU, "Alt"),
Key::RightAlt => (winapi::winuser::VK_MENU, "Alt"), Key::RightAlt => (winuser::VK_MENU, "Alt"),
Key::LeftSuper => (winapi::winuser::VK_LWIN, "LeftWin"), Key::LeftSuper => (winuser::VK_LWIN, "LeftWin"),
Key::RightSuper => (winapi::winuser::VK_RWIN, "RightWin"), Key::RightSuper => (winuser::VK_RWIN, "RightWin"),
_ => (0, "Unsupported"), _ => (0, "Unsupported"),
} }
@ -938,9 +932,9 @@ impl Menu {
pub fn add_sub_menu(&mut self, name: &str, menu: &Menu) { pub fn add_sub_menu(&mut self, name: &str, menu: &Menu) {
unsafe { unsafe {
let menu_name = to_wstring(name); let menu_name = to_wstring(name);
user32::AppendMenuW(self.menu_handle, winuser::AppendMenuW(self.menu_handle,
0x10, 0x10,
menu.menu_handle as UINT_PTR, menu.menu_handle as basetsd::UINT_PTR,
menu_name.as_ptr()); menu_name.as_ptr());
self.accel_table.extend_from_slice(menu.accel_table.as_slice()); self.accel_table.extend_from_slice(menu.accel_table.as_slice());
} }
@ -1008,9 +1002,9 @@ impl Menu {
let vk_accel = Self::map_key_to_vk_accel(menu_item.key); let vk_accel = Self::map_key_to_vk_accel(menu_item.key);
let virt = Self::get_virt_key(menu_item, vk); let virt = Self::get_virt_key(menu_item, vk);
let accel = winuser::ACCEL { let accel = winuser::ACCEL {
fVirt: virt as BYTE, fVirt: virt as minwindef::BYTE,
cmd: menu_item.id as WORD, cmd: menu_item.id as minwindef::WORD,
key: vk_accel.0 as WORD }; key: vk_accel.0 as minwindef::WORD };
self.accel_table.push(accel); self.accel_table.push(accel);
} }
@ -1022,12 +1016,12 @@ impl Menu {
match vk_accel.0 { match vk_accel.0 {
0 => { 0 => {
let item_name = to_wstring(&menu_item.label); let item_name = to_wstring(&menu_item.label);
user32::AppendMenuW(self.menu_handle, 0x10, menu_item.id as UINT_PTR, item_name.as_ptr()); winuser::AppendMenuW(self.menu_handle, 0x10, menu_item.id as basetsd::UINT_PTR, item_name.as_ptr());
}, },
_ => { _ => {
let menu_name = Self::format_name(menu_item, vk_accel.1); let menu_name = Self::format_name(menu_item, vk_accel.1);
let w_name = to_wstring(&menu_name); let w_name = to_wstring(&menu_name);
user32::AppendMenuW(self.menu_handle, 0x10, menu_item.id as UINT_PTR, w_name.as_ptr()); winuser::AppendMenuW(self.menu_handle, 0x10, menu_item.id as basetsd::UINT_PTR, w_name.as_ptr());
self.add_accel(vk_accel.0, menu_item); self.add_accel(vk_accel.0, menu_item);
} }
} }
@ -1050,11 +1044,11 @@ impl Drop for Window {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
if self.dc.is_some() { if self.dc.is_some() {
user32::ReleaseDC(self.window.unwrap(), self.dc.unwrap()); winuser::ReleaseDC(self.window.unwrap(), self.dc.unwrap());
} }
if self.window.is_some() { if self.window.is_some() {
user32::CloseWindow(self.window.unwrap()); winuser::CloseWindow(self.window.unwrap());
} }
} }
} }