From 89bd912a7398f8ccdeac486a1038564ef6e37a09 Mon Sep 17 00:00:00 2001 From: Micah Johnston Date: Mon, 7 Sep 2020 00:37:27 -0500 Subject: [PATCH] call DispatchMessage in event loop and combine handle_message with wnd_proc --- src/win/event.rs | 41 -------------------- src/win/mod.rs | 1 - src/win/window.rs | 99 +++++++++++++++++++++++++++-------------------- 3 files changed, 56 insertions(+), 85 deletions(-) delete mode 100644 src/win/event.rs diff --git a/src/win/event.rs b/src/win/event.rs deleted file mode 100644 index fc11ed9..0000000 --- a/src/win/event.rs +++ /dev/null @@ -1,41 +0,0 @@ -use std::sync::{Arc, Mutex}; - -use winapi::shared::minwindef::{LPARAM, LRESULT, UINT, WPARAM}; - -use crate::{AppWindow, Window}; -use winapi::um::winuser::{DefWindowProcA, WM_MOUSEMOVE, WM_PAINT, WM_TIMER}; - -const WIN_FRAME_TIMER: usize = 4242; - -unsafe fn handle_timer(win: &Arc>>, timer_id: usize) { - match timer_id { - WIN_FRAME_TIMER => {} - _ => (), - } -} - -pub(crate) unsafe fn handle_message( - win: &Arc>>, - message: UINT, - wparam: WPARAM, - lparam: LPARAM, -) -> LRESULT { - let hwnd; - { - hwnd = win.lock().unwrap().hwnd; - } - match message { - WM_MOUSEMOVE => { - let x = (lparam & 0xFFFF) as i32; - let y = ((lparam >> 16) & 0xFFFF) as i32; - win.lock().unwrap().handle_mouse_motion(x, y); - 0 - } - WM_TIMER => { - handle_timer(win, wparam); - 0 - } - WM_PAINT => 0, - _ => DefWindowProcA(hwnd, message, wparam, lparam), - } -} diff --git a/src/win/mod.rs b/src/win/mod.rs index 6409d1c..dfee58e 100644 --- a/src/win/mod.rs +++ b/src/win/mod.rs @@ -1,4 +1,3 @@ -mod event; mod window; pub use window::*; diff --git a/src/win/window.rs b/src/win/window.rs index 70a371c..7d3ccbe 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -1,25 +1,23 @@ -extern crate winapi; - -use std::ptr::null_mut; -use std::sync::mpsc; - -use self::winapi::shared::guiddef::GUID; -use self::winapi::shared::minwindef::{ATOM, FALSE, LPARAM, LRESULT, UINT, WPARAM}; -use self::winapi::shared::windef::{HWND, RECT}; -use self::winapi::um::combaseapi::CoCreateGuid; -use self::winapi::um::winuser::{ - AdjustWindowRectEx, CreateWindowExA, DefWindowProcA, DestroyWindow, GetMessageA, - GetWindowLongPtrA, MessageBoxA, PostMessageA, RegisterClassA, SetTimer, SetWindowLongPtrA, - TranslateMessage, UnregisterClassA, CS_OWNDC, GWLP_USERDATA, MB_ICONERROR, MB_OK, MB_TOPMOST, - MSG, WM_CREATE, WM_SHOWWINDOW, WNDCLASSA, WS_CAPTION, WS_CHILD, WS_CLIPSIBLINGS, - WS_MAXIMIZEBOX, WS_MINIMIZEBOX, WS_POPUPWINDOW, WS_SIZEBOX, WS_VISIBLE, +use winapi::shared::guiddef::GUID; +use winapi::shared::minwindef::{ATOM, FALSE, LPARAM, LRESULT, UINT, WPARAM}; +use winapi::shared::windef::{HWND, RECT}; +use winapi::um::combaseapi::CoCreateGuid; +use winapi::um::winuser::{ + AdjustWindowRectEx, CreateWindowExA, DefWindowProcA, DestroyWindow, DispatchMessageA, + GetMessageA, GetWindowLongPtrA, MessageBoxA, PostMessageA, RegisterClassA, SetTimer, + SetWindowLongPtrA, TranslateMessage, UnregisterClassA, CS_OWNDC, GWLP_USERDATA, MB_ICONERROR, + MB_OK, MB_TOPMOST, MSG, WM_CREATE, WM_MOUSEMOVE, WM_PAINT, WM_SHOWWINDOW, WM_TIMER, WNDCLASSA, + WS_CAPTION, WS_CHILD, WS_CLIPSIBLINGS, WS_MAXIMIZEBOX, WS_MINIMIZEBOX, WS_POPUPWINDOW, + WS_SIZEBOX, WS_VISIBLE, }; -use self::winapi::ctypes::c_void; -use super::event::handle_message; -use crate::{AppWindow, Event, Parent::WithParent, RawWindow, WindowInfo, WindowOpenOptions}; +use std::ffi::c_void; +use std::ptr::null_mut; +use std::sync::mpsc; use std::sync::{Arc, Mutex}; +use crate::{AppWindow, Event, Parent::WithParent, RawWindow, WindowInfo, WindowOpenOptions}; + unsafe fn message_box(title: &str, msg: &str) { let title = (title.to_owned() + "\0").as_ptr() as *const i8; let msg = (msg.to_owned() + "\0").as_ptr() as *const i8; @@ -45,36 +43,52 @@ unsafe fn generate_guid() -> String { ) } +const WIN_FRAME_TIMER: usize = 4242; + +unsafe fn handle_timer(win: &Arc>>, timer_id: usize) { + match timer_id { + WIN_FRAME_TIMER => {} + _ => (), + } +} + unsafe extern "system" fn wnd_proc( hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM, ) -> LRESULT { - let win_ptr = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void; - match msg { - WM_CREATE => { - PostMessageA(hwnd, WM_SHOWWINDOW, 0, 0); - 0 - } - _ => { - if !win_ptr.is_null() { - let win: Arc>> = - Arc::from_raw(win_ptr as *mut Mutex>); - - let ret = handle_message(&win, msg, wparam, lparam); - - // todo: need_reconfigure thing? - - // If we don't do this, the Arc will be dropped and we'll get a crash. - let _ = Arc::into_raw(win); - - return ret; - } - - return DefWindowProcA(hwnd, msg, wparam, lparam); - } + if msg == WM_CREATE { + PostMessageA(hwnd, WM_SHOWWINDOW, 0, 0); + return 0; } + + let win_ptr = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void; + if !win_ptr.is_null() { + let win: Arc>> = Arc::from_raw(win_ptr as *mut Mutex>); + + match msg { + WM_MOUSEMOVE => { + let x = (lparam & 0xFFFF) as i32; + let y = ((lparam >> 16) & 0xFFFF) as i32; + win.lock().unwrap().handle_mouse_motion(x, y); + return 0; + } + WM_TIMER => { + handle_timer(&win, wparam); + return 0; + } + WM_PAINT => { + return 0; + } + _ => {} + } + + // If we don't do this, the Arc will be dropped and we'll get a crash. + let _ = Arc::into_raw(win); + } + + return DefWindowProcA(hwnd, msg, wparam, lparam); } unsafe fn register_wnd_class() -> ATOM { @@ -182,7 +196,6 @@ impl Window { }; let win = Arc::new(Mutex::new(window)); - let win_p = Arc::clone(&win); SetWindowLongPtrA(hwnd, GWLP_USERDATA, Arc::into_raw(win) as *const _ as _); @@ -197,7 +210,7 @@ impl Window { break; } TranslateMessage(&mut msg); - handle_message(&win_p, msg.message, msg.wParam, msg.lParam); + DispatchMessageA(&mut msg); } } }