1
0
Fork 0

call DispatchMessage in event loop and combine handle_message with wnd_proc

This commit is contained in:
Micah Johnston 2020-09-07 00:37:27 -05:00
parent 1303c365d9
commit 89bd912a73
3 changed files with 56 additions and 85 deletions

View file

@ -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<A: AppWindow>(win: &Arc<Mutex<Window<A>>>, timer_id: usize) {
match timer_id {
WIN_FRAME_TIMER => {}
_ => (),
}
}
pub(crate) unsafe fn handle_message<A: AppWindow>(
win: &Arc<Mutex<Window<A>>>,
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),
}
}

View file

@ -1,4 +1,3 @@
mod event;
mod window; mod window;
pub use window::*; pub use window::*;

View file

@ -1,25 +1,23 @@
extern crate winapi; use winapi::shared::guiddef::GUID;
use winapi::shared::minwindef::{ATOM, FALSE, LPARAM, LRESULT, UINT, WPARAM};
use std::ptr::null_mut; use winapi::shared::windef::{HWND, RECT};
use std::sync::mpsc; use winapi::um::combaseapi::CoCreateGuid;
use winapi::um::winuser::{
use self::winapi::shared::guiddef::GUID; AdjustWindowRectEx, CreateWindowExA, DefWindowProcA, DestroyWindow, DispatchMessageA,
use self::winapi::shared::minwindef::{ATOM, FALSE, LPARAM, LRESULT, UINT, WPARAM}; GetMessageA, GetWindowLongPtrA, MessageBoxA, PostMessageA, RegisterClassA, SetTimer,
use self::winapi::shared::windef::{HWND, RECT}; SetWindowLongPtrA, TranslateMessage, UnregisterClassA, CS_OWNDC, GWLP_USERDATA, MB_ICONERROR,
use self::winapi::um::combaseapi::CoCreateGuid; MB_OK, MB_TOPMOST, MSG, WM_CREATE, WM_MOUSEMOVE, WM_PAINT, WM_SHOWWINDOW, WM_TIMER, WNDCLASSA,
use self::winapi::um::winuser::{ WS_CAPTION, WS_CHILD, WS_CLIPSIBLINGS, WS_MAXIMIZEBOX, WS_MINIMIZEBOX, WS_POPUPWINDOW,
AdjustWindowRectEx, CreateWindowExA, DefWindowProcA, DestroyWindow, GetMessageA, WS_SIZEBOX, WS_VISIBLE,
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 self::winapi::ctypes::c_void; use std::ffi::c_void;
use super::event::handle_message; use std::ptr::null_mut;
use crate::{AppWindow, Event, Parent::WithParent, RawWindow, WindowInfo, WindowOpenOptions}; use std::sync::mpsc;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use crate::{AppWindow, Event, Parent::WithParent, RawWindow, WindowInfo, WindowOpenOptions};
unsafe fn message_box(title: &str, msg: &str) { unsafe fn message_box(title: &str, msg: &str) {
let title = (title.to_owned() + "\0").as_ptr() as *const i8; let title = (title.to_owned() + "\0").as_ptr() as *const i8;
let msg = (msg.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<A: AppWindow>(win: &Arc<Mutex<Window<A>>>, timer_id: usize) {
match timer_id {
WIN_FRAME_TIMER => {}
_ => (),
}
}
unsafe extern "system" fn wnd_proc<A: AppWindow>( unsafe extern "system" fn wnd_proc<A: AppWindow>(
hwnd: HWND, hwnd: HWND,
msg: UINT, msg: UINT,
wparam: WPARAM, wparam: WPARAM,
lparam: LPARAM, lparam: LPARAM,
) -> LRESULT { ) -> LRESULT {
let win_ptr = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void; if msg == WM_CREATE {
match msg { PostMessageA(hwnd, WM_SHOWWINDOW, 0, 0);
WM_CREATE => { return 0;
PostMessageA(hwnd, WM_SHOWWINDOW, 0, 0);
0
}
_ => {
if !win_ptr.is_null() {
let win: Arc<Mutex<Window<A>>> =
Arc::from_raw(win_ptr as *mut Mutex<Window<A>>);
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);
}
} }
let win_ptr = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void;
if !win_ptr.is_null() {
let win: Arc<Mutex<Window<A>>> = Arc::from_raw(win_ptr as *mut Mutex<Window<A>>);
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<A: AppWindow>() -> ATOM { unsafe fn register_wnd_class<A: AppWindow>() -> ATOM {
@ -182,7 +196,6 @@ impl<A: AppWindow> Window<A> {
}; };
let win = Arc::new(Mutex::new(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 _); SetWindowLongPtrA(hwnd, GWLP_USERDATA, Arc::into_raw(win) as *const _ as _);
@ -197,7 +210,7 @@ impl<A: AppWindow> Window<A> {
break; break;
} }
TranslateMessage(&mut msg); TranslateMessage(&mut msg);
handle_message(&win_p, msg.message, msg.wParam, msg.lParam); DispatchMessageA(&mut msg);
} }
} }
} }