diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/win32/callback.rs | 194 | ||||
-rw-r--r-- | src/win32/event.rs | 356 | ||||
-rw-r--r-- | src/win32/headless.rs | 35 | ||||
-rw-r--r-- | src/win32/init.rs | 238 | ||||
-rw-r--r-- | src/win32/mod.rs | 37 |
5 files changed, 415 insertions, 445 deletions
diff --git a/src/win32/callback.rs b/src/win32/callback.rs new file mode 100644 index 0000000..5423fc3 --- /dev/null +++ b/src/win32/callback.rs @@ -0,0 +1,194 @@ +use std::rc::Rc; +use std::cell::RefCell; +use std::sync::mpsc::{Sender, Receiver, channel}; + +use Event; +use super::event; + +use user32; +use winapi; + +/// Stores the current window and its events dispatcher. +/// +/// We only have one window per thread. We still store the HWND in case where we +/// receive an event for another window. +thread_local!(pub static WINDOW: Rc<RefCell<Option<(winapi::HWND, Sender<Event>)>>> = Rc::new(RefCell::new(None))); + +/// Checks that the window is the good one, and if so send the event to it. +fn send_event(input_window: winapi::HWND, event: Event) { + WINDOW.with(|window| { + let window = window.borrow(); + let stored = match *window { + None => return, + Some(ref v) => v + }; + + let &(ref win, ref sender) = stored; + + if win != &input_window { + return; + } + + sender.send(event).ok(); // ignoring if closed + }); +} + +/// This is the callback that is called by `DispatchMessage` in the events loop. +/// +/// Returning 0 tells the Win32 API that the message has been processed. +pub extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT, + wparam: winapi::WPARAM, lparam: winapi::LPARAM) -> winapi::LRESULT +{ + match msg { + winapi::WM_DESTROY => { + use events::Event::Closed; + + WINDOW.with(|w| { + let w = w.borrow(); + let &(ref win, _) = match *w { + None => return, + Some(ref v) => v + }; + + if win == &window { + unsafe { user32::PostQuitMessage(0); } + } + }); + + send_event(window, Closed); + 0 + }, + + winapi::WM_ERASEBKGND => { + 1 + }, + + winapi::WM_SIZE => { + use events::Event::Resized; + let w = winapi::LOWORD(lparam as winapi::DWORD) as u32; + let h = winapi::HIWORD(lparam as winapi::DWORD) as u32; + send_event(window, Resized(w, h)); + 0 + }, + + winapi::WM_MOVE => { + use events::Event::Moved; + let x = winapi::LOWORD(lparam as winapi::DWORD) as i32; + let y = winapi::HIWORD(lparam as winapi::DWORD) as i32; + send_event(window, Moved(x, y)); + 0 + }, + + winapi::WM_CHAR => { + use std::mem; + use events::Event::ReceivedCharacter; + let chr: char = unsafe { mem::transmute(wparam as u32) }; + send_event(window, ReceivedCharacter(chr)); + 0 + }, + + winapi::WM_MOUSEMOVE => { + use events::Event::MouseMoved; + + let x = winapi::GET_X_LPARAM(lparam) as i32; + let y = winapi::GET_Y_LPARAM(lparam) as i32; + + send_event(window, MouseMoved((x, y))); + + 0 + }, + + winapi::WM_MOUSEWHEEL => { + use events::Event::MouseWheel; + + let value = (wparam >> 16) as i16; + let value = value as i32; + + send_event(window, MouseWheel(value)); + + 0 + }, + + winapi::WM_KEYDOWN => { + use events::Event::KeyboardInput; + use events::ElementState::Pressed; + let scancode = ((lparam >> 16) & 0xff) as u8; + let vkey = event::vkeycode_to_element(wparam); + send_event(window, KeyboardInput(Pressed, scancode, vkey)); + 0 + }, + + winapi::WM_KEYUP => { + use events::Event::KeyboardInput; + use events::ElementState::Released; + let scancode = ((lparam >> 16) & 0xff) as u8; + let vkey = event::vkeycode_to_element(wparam); + send_event(window, KeyboardInput(Released, scancode, vkey)); + 0 + }, + + winapi::WM_LBUTTONDOWN => { + use events::Event::MouseInput; + use events::MouseButton::Left; + use events::ElementState::Pressed; + send_event(window, MouseInput(Pressed, Left)); + 0 + }, + + winapi::WM_LBUTTONUP => { + use events::Event::MouseInput; + use events::MouseButton::Left; + use events::ElementState::Released; + send_event(window, MouseInput(Released, Left)); + 0 + }, + + winapi::WM_RBUTTONDOWN => { + use events::Event::MouseInput; + use events::MouseButton::Right; + use events::ElementState::Pressed; + send_event(window, MouseInput(Pressed, Right)); + 0 + }, + + winapi::WM_RBUTTONUP => { + use events::Event::MouseInput; + use events::MouseButton::Right; + use events::ElementState::Released; + send_event(window, MouseInput(Released, Right)); + 0 + }, + + winapi::WM_MBUTTONDOWN => { + use events::Event::MouseInput; + use events::MouseButton::Middle; + use events::ElementState::Pressed; + send_event(window, MouseInput(Pressed, Middle)); + 0 + }, + + winapi::WM_MBUTTONUP => { + use events::Event::MouseInput; + use events::MouseButton::Middle; + use events::ElementState::Released; + send_event(window, MouseInput(Released, Middle)); + 0 + }, + + winapi::WM_SETFOCUS => { + use events::Event::Focused; + send_event(window, Focused(true)); + 0 + }, + + winapi::WM_KILLFOCUS => { + use events::Event::Focused; + send_event(window, Focused(false)); + 0 + }, + + _ => unsafe { + user32::DefWindowProcW(window, msg, wparam, lparam) + } + } +} diff --git a/src/win32/event.rs b/src/win32/event.rs index 16080eb..4c8f4f3 100644 --- a/src/win32/event.rs +++ b/src/win32/event.rs @@ -1,181 +1,181 @@ -use events; +use events::VirtualKeyCode; use winapi; -pub fn vkeycode_to_element(code: winapi::WPARAM) -> Option<events::VirtualKeyCode> { - Some(match code { - //winapi::VK_LBUTTON => events::VirtualKeyCode::Lbutton, - //winapi::VK_RBUTTON => events::VirtualKeyCode::Rbutton, - //winapi::VK_CANCEL => events::VirtualKeyCode::Cancel, - //winapi::VK_MBUTTON => events::VirtualKeyCode::Mbutton, - //winapi::VK_XBUTTON1 => events::VirtualKeyCode::Xbutton1, - //winapi::VK_XBUTTON2 => events::VirtualKeyCode::Xbutton2, - winapi::VK_BACK => events::VirtualKeyCode::Back, - winapi::VK_TAB => events::VirtualKeyCode::Tab, - //winapi::VK_CLEAR => events::VirtualKeyCode::Clear, - winapi::VK_RETURN => events::VirtualKeyCode::Return, - //winapi::VK_SHIFT => events::VirtualKeyCode::Shift, - //winapi::VK_CONTROL => events::VirtualKeyCode::Control, - //winapi::VK_MENU => events::VirtualKeyCode::Menu, - winapi::VK_PAUSE => events::VirtualKeyCode::Pause, - winapi::VK_CAPITAL => events::VirtualKeyCode::Capital, - winapi::VK_KANA => events::VirtualKeyCode::Kana, - //winapi::VK_HANGUEL => events::VirtualKeyCode::Hanguel, - //winapi::VK_HANGUL => events::VirtualKeyCode::Hangul, - //winapi::VK_JUNJA => events::VirtualKeyCode::Junja, - //winapi::VK_FINAL => events::VirtualKeyCode::Final, - //winapi::VK_HANJA => events::VirtualKeyCode::Hanja, - winapi::VK_KANJI => events::VirtualKeyCode::Kanji, - winapi::VK_ESCAPE => events::VirtualKeyCode::Escape, - winapi::VK_CONVERT => events::VirtualKeyCode::Convert, - //winapi::VK_NONCONVERT => events::VirtualKeyCode::Nonconvert, - //winapi::VK_ACCEPT => events::VirtualKeyCode::Accept, - //winapi::VK_MODECHANGE => events::VirtualKeyCode::Modechange, - winapi::VK_SPACE => events::VirtualKeyCode::Space, - winapi::VK_PRIOR => events::VirtualKeyCode::PageUp, - winapi::VK_NEXT => events::VirtualKeyCode::PageDown, - winapi::VK_END => events::VirtualKeyCode::End, - winapi::VK_HOME => events::VirtualKeyCode::Home, - winapi::VK_LEFT => events::VirtualKeyCode::Left, - winapi::VK_UP => events::VirtualKeyCode::Up, - winapi::VK_RIGHT => events::VirtualKeyCode::Right, - winapi::VK_DOWN => events::VirtualKeyCode::Down, - //winapi::VK_SELECT => events::VirtualKeyCode::Select, - //winapi::VK_PRINT => events::VirtualKeyCode::Print, - //winapi::VK_EXECUTE => events::VirtualKeyCode::Execute, - winapi::VK_SNAPSHOT => events::VirtualKeyCode::Snapshot, - winapi::VK_INSERT => events::VirtualKeyCode::Insert, - winapi::VK_DELETE => events::VirtualKeyCode::Delete, - //winapi::VK_HELP => events::VirtualKeyCode::Help, - 0x30 => events::VirtualKeyCode::Key0, - 0x31 => events::VirtualKeyCode::Key1, - 0x32 => events::VirtualKeyCode::Key2, - 0x33 => events::VirtualKeyCode::Key3, - 0x34 => events::VirtualKeyCode::Key4, - 0x35 => events::VirtualKeyCode::Key5, - 0x36 => events::VirtualKeyCode::Key6, - 0x37 => events::VirtualKeyCode::Key7, - 0x38 => events::VirtualKeyCode::Key8, - 0x39 => events::VirtualKeyCode::Key9, - 0x41 => events::VirtualKeyCode::A, - 0x42 => events::VirtualKeyCode::B, - 0x43 => events::VirtualKeyCode::C, - 0x44 => events::VirtualKeyCode::D, - 0x45 => events::VirtualKeyCode::E, - 0x46 => events::VirtualKeyCode::F, - 0x47 => events::VirtualKeyCode::G, - 0x48 => events::VirtualKeyCode::H, - 0x49 => events::VirtualKeyCode::I, - 0x4A => events::VirtualKeyCode::J, - 0x4B => events::VirtualKeyCode::K, - 0x4C => events::VirtualKeyCode::L, - 0x4D => events::VirtualKeyCode::M, - 0x4E => events::VirtualKeyCode::N, - 0x4F => events::VirtualKeyCode::O, - 0x50 => events::VirtualKeyCode::P, - 0x51 => events::VirtualKeyCode::Q, - 0x52 => events::VirtualKeyCode::R, - 0x53 => events::VirtualKeyCode::S, - 0x54 => events::VirtualKeyCode::T, - 0x55 => events::VirtualKeyCode::U, - 0x56 => events::VirtualKeyCode::V, - 0x57 => events::VirtualKeyCode::W, - 0x58 => events::VirtualKeyCode::X, - 0x59 => events::VirtualKeyCode::Y, - 0x5A => events::VirtualKeyCode::Z, - //winapi::VK_LWIN => events::VirtualKeyCode::Lwin, - //winapi::VK_RWIN => events::VirtualKeyCode::Rwin, - winapi::VK_APPS => events::VirtualKeyCode::Apps, - winapi::VK_SLEEP => events::VirtualKeyCode::Sleep, - winapi::VK_NUMPAD0 => events::VirtualKeyCode::Numpad0, - winapi::VK_NUMPAD1 => events::VirtualKeyCode::Numpad1, - winapi::VK_NUMPAD2 => events::VirtualKeyCode::Numpad2, - winapi::VK_NUMPAD3 => events::VirtualKeyCode::Numpad3, - winapi::VK_NUMPAD4 => events::VirtualKeyCode::Numpad4, - winapi::VK_NUMPAD5 => events::VirtualKeyCode::Numpad5, - winapi::VK_NUMPAD6 => events::VirtualKeyCode::Numpad6, - winapi::VK_NUMPAD7 => events::VirtualKeyCode::Numpad7, - winapi::VK_NUMPAD8 => events::VirtualKeyCode::Numpad8, - winapi::VK_NUMPAD9 => events::VirtualKeyCode::Numpad9, - winapi::VK_MULTIPLY => events::VirtualKeyCode::Multiply, - winapi::VK_ADD => events::VirtualKeyCode::Add, - //winapi::VK_SEPARATOR => events::VirtualKeyCode::Separator, - winapi::VK_SUBTRACT => events::VirtualKeyCode::Subtract, - winapi::VK_DECIMAL => events::VirtualKeyCode::Decimal, - winapi::VK_DIVIDE => events::VirtualKeyCode::Divide, - winapi::VK_F1 => events::VirtualKeyCode::F1, - winapi::VK_F2 => events::VirtualKeyCode::F2, - winapi::VK_F3 => events::VirtualKeyCode::F3, - winapi::VK_F4 => events::VirtualKeyCode::F4, - winapi::VK_F5 => events::VirtualKeyCode::F5, - winapi::VK_F6 => events::VirtualKeyCode::F6, - winapi::VK_F7 => events::VirtualKeyCode::F7, - winapi::VK_F8 => events::VirtualKeyCode::F8, - winapi::VK_F9 => events::VirtualKeyCode::F9, - winapi::VK_F10 => events::VirtualKeyCode::F10, - winapi::VK_F11 => events::VirtualKeyCode::F11, - winapi::VK_F12 => events::VirtualKeyCode::F12, - winapi::VK_F13 => events::VirtualKeyCode::F13, - winapi::VK_F14 => events::VirtualKeyCode::F14, - winapi::VK_F15 => events::VirtualKeyCode::F15, - /*winapi::VK_F16 => events::VirtualKeyCode::F16, - winapi::VK_F17 => events::VirtualKeyCode::F17, - winapi::VK_F18 => events::VirtualKeyCode::F18, - winapi::VK_F19 => events::VirtualKeyCode::F19, - winapi::VK_F20 => events::VirtualKeyCode::F20, - winapi::VK_F21 => events::VirtualKeyCode::F21, - winapi::VK_F22 => events::VirtualKeyCode::F22, - winapi::VK_F23 => events::VirtualKeyCode::F23, - winapi::VK_F24 => events::VirtualKeyCode::F24,*/ - winapi::VK_NUMLOCK => events::VirtualKeyCode::Numlock, - winapi::VK_SCROLL => events::VirtualKeyCode::Scroll, - /*winapi::VK_LSHIFT => events::VirtualKeyCode::Lshift, - winapi::VK_RSHIFT => events::VirtualKeyCode::Rshift, - winapi::VK_LCONTROL => events::VirtualKeyCode::Lcontrol, - winapi::VK_RCONTROL => events::VirtualKeyCode::Rcontrol, - winapi::VK_LMENU => events::VirtualKeyCode::Lmenu, - winapi::VK_RMENU => events::VirtualKeyCode::Rmenu, - winapi::VK_BROWSER_BACK => events::VirtualKeyCode::Browser_back, - winapi::VK_BROWSER_FORWARD => events::VirtualKeyCode::Browser_forward, - winapi::VK_BROWSER_REFRESH => events::VirtualKeyCode::Browser_refresh, - winapi::VK_BROWSER_STOP => events::VirtualKeyCode::Browser_stop, - winapi::VK_BROWSER_SEARCH => events::VirtualKeyCode::Browser_search, - winapi::VK_BROWSER_FAVORITES => events::VirtualKeyCode::Browser_favorites, - winapi::VK_BROWSER_HOME => events::VirtualKeyCode::Browser_home, - winapi::VK_VOLUME_MUTE => events::VirtualKeyCode::Volume_mute, - winapi::VK_VOLUME_DOWN => events::VirtualKeyCode::Volume_down, - winapi::VK_VOLUME_UP => events::VirtualKeyCode::Volume_up, - winapi::VK_MEDIA_NEXT_TRACK => events::VirtualKeyCode::Media_next_track, - winapi::VK_MEDIA_PREV_TRACK => events::VirtualKeyCode::Media_prev_track, - winapi::VK_MEDIA_STOP => events::VirtualKeyCode::Media_stop, - winapi::VK_MEDIA_PLAY_PAUSE => events::VirtualKeyCode::Media_play_pause, - winapi::VK_LAUNCH_MAIL => events::VirtualKeyCode::Launch_mail, - winapi::VK_LAUNCH_MEDIA_SELECT => events::VirtualKeyCode::Launch_media_select, - winapi::VK_LAUNCH_APP1 => events::VirtualKeyCode::Launch_app1, - winapi::VK_LAUNCH_APP2 => events::VirtualKeyCode::Launch_app2, - winapi::VK_OEM_1 => events::VirtualKeyCode::Oem_1, - winapi::VK_OEM_PLUS => events::VirtualKeyCode::Oem_plus, - winapi::VK_OEM_COMMA => events::VirtualKeyCode::Oem_comma, - winapi::VK_OEM_MINUS => events::VirtualKeyCode::Oem_minus, - winapi::VK_OEM_PERIOD => events::VirtualKeyCode::Oem_period, - winapi::VK_OEM_2 => events::VirtualKeyCode::Oem_2, - winapi::VK_OEM_3 => events::VirtualKeyCode::Oem_3, - winapi::VK_OEM_4 => events::VirtualKeyCode::Oem_4, - winapi::VK_OEM_5 => events::VirtualKeyCode::Oem_5, - winapi::VK_OEM_6 => events::VirtualKeyCode::Oem_6, - winapi::VK_OEM_7 => events::VirtualKeyCode::Oem_7, - winapi::VK_OEM_8 => events::VirtualKeyCode::Oem_8, - winapi::VK_OEM_102 => events::VirtualKeyCode::Oem_102, - winapi::VK_PROCESSKEY => events::VirtualKeyCode::Processkey, - winapi::VK_PACKET => events::VirtualKeyCode::Packet, - winapi::VK_ATTN => events::VirtualKeyCode::Attn, - winapi::VK_CRSEL => events::VirtualKeyCode::Crsel, - winapi::VK_EXSEL => events::VirtualKeyCode::Exsel, - winapi::VK_EREOF => events::VirtualKeyCode::Ereof, - winapi::VK_PLAY => events::VirtualKeyCode::Play, - winapi::VK_ZOOM => events::VirtualKeyCode::Zoom, - winapi::VK_NONAME => events::VirtualKeyCode::Noname, - winapi::VK_PA1 => events::VirtualKeyCode::Pa1, - winapi::VK_OEM_CLEAR => events::VirtualKeyCode::Oem_clear,*/ - _ => return None - }) +pub fn vkeycode_to_element(code: winapi::WPARAM) -> Option<VirtualKeyCode> { + match code { + //winapi::VK_LBUTTON => Some(VirtualKeyCode::Lbutton), + //winapi::VK_RBUTTON => Some(VirtualKeyCode::Rbutton), + //winapi::VK_CANCEL => Some(VirtualKeyCode::Cancel), + //winapi::VK_MBUTTON => Some(VirtualKeyCode::Mbutton), + //winapi::VK_XBUTTON1 => Some(VirtualKeyCode::Xbutton1), + //winapi::VK_XBUTTON2 => Some(VirtualKeyCode::Xbutton2), + winapi::VK_BACK => Some(VirtualKeyCode::Back), + winapi::VK_TAB => Some(VirtualKeyCode::Tab), + //winapi::VK_CLEAR => Some(VirtualKeyCode::Clear), + winapi::VK_RETURN => Some(VirtualKeyCode::Return), + //winapi::VK_SHIFT => Some(VirtualKeyCode::Shift), + //winapi::VK_CONTROL => Some(VirtualKeyCode::Control), + //winapi::VK_MENU => Some(VirtualKeyCode::Menu), + winapi::VK_PAUSE => Some(VirtualKeyCode::Pause), + winapi::VK_CAPITAL => Some(VirtualKeyCode::Capital), + winapi::VK_KANA => Some(VirtualKeyCode::Kana), + //winapi::VK_HANGUEL => Some(VirtualKeyCode::Hanguel), + //winapi::VK_HANGUL => Some(VirtualKeyCode::Hangul), + //winapi::VK_JUNJA => Some(VirtualKeyCode::Junja), + //winapi::VK_FINAL => Some(VirtualKeyCode::Final), + //winapi::VK_HANJA => Some(VirtualKeyCode::Hanja), + winapi::VK_KANJI => Some(VirtualKeyCode::Kanji), + winapi::VK_ESCAPE => Some(VirtualKeyCode::Escape), + winapi::VK_CONVERT => Some(VirtualKeyCode::Convert), + //winapi::VK_NONCONVERT => Some(VirtualKeyCode::Nonconvert), + //winapi::VK_ACCEPT => Some(VirtualKeyCode::Accept), + //winapi::VK_MODECHANGE => Some(VirtualKeyCode::Modechange), + winapi::VK_SPACE => Some(VirtualKeyCode::Space), + winapi::VK_PRIOR => Some(VirtualKeyCode::PageUp), + winapi::VK_NEXT => Some(VirtualKeyCode::PageDown), + winapi::VK_END => Some(VirtualKeyCode::End), + winapi::VK_HOME => Some(VirtualKeyCode::Home), + winapi::VK_LEFT => Some(VirtualKeyCode::Left), + winapi::VK_UP => Some(VirtualKeyCode::Up), + winapi::VK_RIGHT => Some(VirtualKeyCode::Right), + winapi::VK_DOWN => Some(VirtualKeyCode::Down), + //winapi::VK_SELECT => Some(VirtualKeyCode::Select), + //winapi::VK_PRINT => Some(VirtualKeyCode::Print), + //winapi::VK_EXECUTE => Some(VirtualKeyCode::Execute), + winapi::VK_SNAPSHOT => Some(VirtualKeyCode::Snapshot), + winapi::VK_INSERT => Some(VirtualKeyCode::Insert), + winapi::VK_DELETE => Some(VirtualKeyCode::Delete), + //winapi::VK_HELP => Some(VirtualKeyCode::Help), + 0x30 => Some(VirtualKeyCode::Key0), + 0x31 => Some(VirtualKeyCode::Key1), + 0x32 => Some(VirtualKeyCode::Key2), + 0x33 => Some(VirtualKeyCode::Key3), + 0x34 => Some(VirtualKeyCode::Key4), + 0x35 => Some(VirtualKeyCode::Key5), + 0x36 => Some(VirtualKeyCode::Key6), + 0x37 => Some(VirtualKeyCode::Key7), + 0x38 => Some(VirtualKeyCode::Key8), + 0x39 => Some(VirtualKeyCode::Key9), + 0x41 => Some(VirtualKeyCode::A), + 0x42 => Some(VirtualKeyCode::B), + 0x43 => Some(VirtualKeyCode::C), + 0x44 => Some(VirtualKeyCode::D), + 0x45 => Some(VirtualKeyCode::E), + 0x46 => Some(VirtualKeyCode::F), + 0x47 => Some(VirtualKeyCode::G), + 0x48 => Some(VirtualKeyCode::H), + 0x49 => Some(VirtualKeyCode::I), + 0x4A => Some(VirtualKeyCode::J), + 0x4B => Some(VirtualKeyCode::K), + 0x4C => Some(VirtualKeyCode::L), + 0x4D => Some(VirtualKeyCode::M), + 0x4E => Some(VirtualKeyCode::N), + 0x4F => Some(VirtualKeyCode::O), + 0x50 => Some(VirtualKeyCode::P), + 0x51 => Some(VirtualKeyCode::Q), + 0x52 => Some(VirtualKeyCode::R), + 0x53 => Some(VirtualKeyCode::S), + 0x54 => Some(VirtualKeyCode::T), + 0x55 => Some(VirtualKeyCode::U), + 0x56 => Some(VirtualKeyCode::V), + 0x57 => Some(VirtualKeyCode::W), + 0x58 => Some(VirtualKeyCode::X), + 0x59 => Some(VirtualKeyCode::Y), + 0x5A => Some(VirtualKeyCode::Z), + //winapi::VK_LWIN => Some(VirtualKeyCode::Lwin), + //winapi::VK_RWIN => Some(VirtualKeyCode::Rwin), + winapi::VK_APPS => Some(VirtualKeyCode::Apps), + winapi::VK_SLEEP => Some(VirtualKeyCode::Sleep), + winapi::VK_NUMPAD0 => Some(VirtualKeyCode::Numpad0), + winapi::VK_NUMPAD1 => Some(VirtualKeyCode::Numpad1), + winapi::VK_NUMPAD2 => Some(VirtualKeyCode::Numpad2), + winapi::VK_NUMPAD3 => Some(VirtualKeyCode::Numpad3), + winapi::VK_NUMPAD4 => Some(VirtualKeyCode::Numpad4), + winapi::VK_NUMPAD5 => Some(VirtualKeyCode::Numpad5), + winapi::VK_NUMPAD6 => Some(VirtualKeyCode::Numpad6), + winapi::VK_NUMPAD7 => Some(VirtualKeyCode::Numpad7), + winapi::VK_NUMPAD8 => Some(VirtualKeyCode::Numpad8), + winapi::VK_NUMPAD9 => Some(VirtualKeyCode::Numpad9), + winapi::VK_MULTIPLY => Some(VirtualKeyCode::Multiply), + winapi::VK_ADD => Some(VirtualKeyCode::Add), + //winapi::VK_SEPARATOR => Some(VirtualKeyCode::Separator), + winapi::VK_SUBTRACT => Some(VirtualKeyCode::Subtract), + winapi::VK_DECIMAL => Some(VirtualKeyCode::Decimal), + winapi::VK_DIVIDE => Some(VirtualKeyCode::Divide), + winapi::VK_F1 => Some(VirtualKeyCode::F1), + winapi::VK_F2 => Some(VirtualKeyCode::F2), + winapi::VK_F3 => Some(VirtualKeyCode::F3), + winapi::VK_F4 => Some(VirtualKeyCode::F4), + winapi::VK_F5 => Some(VirtualKeyCode::F5), + winapi::VK_F6 => Some(VirtualKeyCode::F6), + winapi::VK_F7 => Some(VirtualKeyCode::F7), + winapi::VK_F8 => Some(VirtualKeyCode::F8), + winapi::VK_F9 => Some(VirtualKeyCode::F9), + winapi::VK_F10 => Some(VirtualKeyCode::F10), + winapi::VK_F11 => Some(VirtualKeyCode::F11), + winapi::VK_F12 => Some(VirtualKeyCode::F12), + winapi::VK_F13 => Some(VirtualKeyCode::F13), + winapi::VK_F14 => Some(VirtualKeyCode::F14), + winapi::VK_F15 => Some(VirtualKeyCode::F15), + /*winapi::VK_F16 => Some(VirtualKeyCode::F16), + winapi::VK_F17 => Some(VirtualKeyCode::F17), + winapi::VK_F18 => Some(VirtualKeyCode::F18), + winapi::VK_F19 => Some(VirtualKeyCode::F19), + winapi::VK_F20 => Some(VirtualKeyCode::F20), + winapi::VK_F21 => Some(VirtualKeyCode::F21), + winapi::VK_F22 => Some(VirtualKeyCode::F22), + winapi::VK_F23 => Some(VirtualKeyCode::F23), + winapi::VK_F24 => Some(VirtualKeyCode::F24),*/ + winapi::VK_NUMLOCK => Some(VirtualKeyCode::Numlock), + winapi::VK_SCROLL => Some(VirtualKeyCode::Scroll), + /*winapi::VK_LSHIFT => Some(VirtualKeyCode::Lshift), + winapi::VK_RSHIFT => Some(VirtualKeyCode::Rshift), + winapi::VK_LCONTROL => Some(VirtualKeyCode::Lcontrol), + winapi::VK_RCONTROL => Some(VirtualKeyCode::Rcontrol), + winapi::VK_LMENU => Some(VirtualKeyCode::Lmenu), + winapi::VK_RMENU => Some(VirtualKeyCode::Rmenu), + winapi::VK_BROWSER_BACK => Some(VirtualKeyCode::Browser_back), + winapi::VK_BROWSER_FORWARD => Some(VirtualKeyCode::Browser_forward), + winapi::VK_BROWSER_REFRESH => Some(VirtualKeyCode::Browser_refresh), + winapi::VK_BROWSER_STOP => Some(VirtualKeyCode::Browser_stop), + winapi::VK_BROWSER_SEARCH => Some(VirtualKeyCode::Browser_search), + winapi::VK_BROWSER_FAVORITES => Some(VirtualKeyCode::Browser_favorites), + winapi::VK_BROWSER_HOME => Some(VirtualKeyCode::Browser_home), + winapi::VK_VOLUME_MUTE => Some(VirtualKeyCode::Volume_mute), + winapi::VK_VOLUME_DOWN => Some(VirtualKeyCode::Volume_down), + winapi::VK_VOLUME_UP => Some(VirtualKeyCode::Volume_up), + winapi::VK_MEDIA_NEXT_TRACK => Some(VirtualKeyCode::Media_next_track), + winapi::VK_MEDIA_PREV_TRACK => Some(VirtualKeyCode::Media_prev_track), + winapi::VK_MEDIA_STOP => Some(VirtualKeyCode::Media_stop), + winapi::VK_MEDIA_PLAY_PAUSE => Some(VirtualKeyCode::Media_play_pause), + winapi::VK_LAUNCH_MAIL => Some(VirtualKeyCode::Launch_mail), + winapi::VK_LAUNCH_MEDIA_SELECT => Some(VirtualKeyCode::Launch_media_select), + winapi::VK_LAUNCH_APP1 => Some(VirtualKeyCode::Launch_app1), + winapi::VK_LAUNCH_APP2 => Some(VirtualKeyCode::Launch_app2), + winapi::VK_OEM_1 => Some(VirtualKeyCode::Oem_1), + winapi::VK_OEM_PLUS => Some(VirtualKeyCode::Oem_plus), + winapi::VK_OEM_COMMA => Some(VirtualKeyCode::Oem_comma), + winapi::VK_OEM_MINUS => Some(VirtualKeyCode::Oem_minus), + winapi::VK_OEM_PERIOD => Some(VirtualKeyCode::Oem_period), + winapi::VK_OEM_2 => Some(VirtualKeyCode::Oem_2), + winapi::VK_OEM_3 => Some(VirtualKeyCode::Oem_3), + winapi::VK_OEM_4 => Some(VirtualKeyCode::Oem_4), + winapi::VK_OEM_5 => Some(VirtualKeyCode::Oem_5), + winapi::VK_OEM_6 => Some(VirtualKeyCode::Oem_6), + winapi::VK_OEM_7 => Some(VirtualKeyCode::Oem_7), + winapi::VK_OEM_8 => Some(VirtualKeyCode::Oem_8), + winapi::VK_OEM_102 => Some(VirtualKeyCode::Oem_102), + winapi::VK_PROCESSKEY => Some(VirtualKeyCode::Processkey), + winapi::VK_PACKET => Some(VirtualKeyCode::Packet), + winapi::VK_ATTN => Some(VirtualKeyCode::Attn), + winapi::VK_CRSEL => Some(VirtualKeyCode::Crsel), + winapi::VK_EXSEL => Some(VirtualKeyCode::Exsel), + winapi::VK_EREOF => Some(VirtualKeyCode::Ereof), + winapi::VK_PLAY => Some(VirtualKeyCode::Play), + winapi::VK_ZOOM => Some(VirtualKeyCode::Zoom), + winapi::VK_NONAME => Some(VirtualKeyCode::Noname), + winapi::VK_PA1 => Some(VirtualKeyCode::Pa1), + winapi::VK_OEM_CLEAR => Some(VirtualKeyCode::Oem_clear),*/ + _ => None + } } diff --git a/src/win32/headless.rs b/src/win32/headless.rs new file mode 100644 index 0000000..1da8b02 --- /dev/null +++ b/src/win32/headless.rs @@ -0,0 +1,35 @@ +use super::Window; +use super::init; + +use Api; +use BuilderAttribs; +use CreationError; + +/// +pub struct HeadlessContext(Window); + +impl HeadlessContext { + /// See the docs in the crate root file. + pub fn new(builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> { + let (builder, _) = builder.extract_non_static(); + init::new_window(builder, None).map(|w| HeadlessContext(w)) + } + + /// See the docs in the crate root file. + pub unsafe fn make_current(&self) { + self.0.make_current() + } + + /// See the docs in the crate root file. + pub fn get_proc_address(&self, addr: &str) -> *const () { + self.0.get_proc_address(addr) + } + + /// See the docs in the crate root file. + pub fn get_api(&self) -> Api { + Api::OpenGl + } + + pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) { + } +} diff --git a/src/win32/init.rs b/src/win32/init.rs index 9e0e3a7..c3352b6 100644 --- a/src/win32/init.rs +++ b/src/win32/init.rs @@ -1,15 +1,13 @@ use std::sync::atomic::AtomicBool; use std::ptr; -use super::event; +use super::callback; use super::Window; use BuilderAttribs; -use {CreationError, Event}; +use CreationError; use CreationError::OsError; -use std::cell::RefCell; use std::ffi::CString; -use std::rc::Rc; -use std::sync::mpsc::{Sender, Receiver, channel}; +use std::sync::mpsc::channel; use libc; use super::gl; @@ -18,12 +16,6 @@ use kernel32; use user32; use gdi32; -/// Stores the current window and its events dispatcher. -/// -/// We only have one window per thread. We still store the HWND in case where we -/// receive an event for another window. -thread_local!(static WINDOW: Rc<RefCell<Option<(winapi::HWND, Sender<Event>)>>> = Rc::new(RefCell::new(None))); - /// Work-around the fact that HGLRC doesn't implement Send pub struct ContextHack(pub winapi::HGLRC); unsafe impl Send for ContextHack {} @@ -54,7 +46,7 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<C let class = winapi::WNDCLASSEXW { cbSize: mem::size_of::<winapi::WNDCLASSEXW>() as winapi::UINT, style: winapi::CS_HREDRAW | winapi::CS_VREDRAW | winapi::CS_OWNDC, - lpfnWndProc: callback, + lpfnWndProc: callback::callback, cbClsExtra: 0, cbWndExtra: 0, hInstance: unsafe { kernel32::GetModuleHandleW(ptr::null()) }, @@ -356,7 +348,7 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<C let events_receiver = { let (tx, rx) = channel(); let mut tx = Some(tx); - WINDOW.with(|window| { + callback::WINDOW.with(|window| { (*window.borrow_mut()) = Some((real_window, tx.take().unwrap())); }); rx @@ -423,223 +415,3 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<C rx.recv().unwrap() } - -/// Checks that the window is the good one, and if so send the event to it. -fn send_event(input_window: winapi::HWND, event: Event) { - WINDOW.with(|window| { - let window = window.borrow(); - let stored = match *window { - None => return, - Some(ref v) => v - }; - - let &(ref win, ref sender) = stored; - - if win != &input_window { - return; - } - - sender.send(event).ok(); // ignoring if closed - }); -} - -/// This is the callback that is called by `DispatchMessage` in the events loop. -/// -/// Returning 0 tells the Win32 API that the message has been processed. -extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT, - wparam: winapi::WPARAM, lparam: winapi::LPARAM) -> winapi::LRESULT -{ - match msg { - winapi::WM_DESTROY => { - use events::Event::Closed; - - WINDOW.with(|w| { - let w = w.borrow(); - let &(ref win, _) = match *w { - None => return, - Some(ref v) => v - }; - - if win == &window { - unsafe { user32::PostQuitMessage(0); } - } - }); - - send_event(window, Closed); - 0 - }, - - winapi::WM_ERASEBKGND => { - 1 - }, - - winapi::WM_SIZE => { - use events::Event::Resized; - let w = winapi::LOWORD(lparam as winapi::DWORD) as u32; - let h = winapi::HIWORD(lparam as winapi::DWORD) as u32; - send_event(window, Resized(w, h)); - 0 - }, - - winapi::WM_MOVE => { - use events::Event::Moved; - let x = winapi::LOWORD(lparam as winapi::DWORD) as i32; - let y = winapi::HIWORD(lparam as winapi::DWORD) as i32; - send_event(window, Moved(x, y)); - 0 - }, - - winapi::WM_CHAR => { - use std::mem; - use events::Event::ReceivedCharacter; - let chr: char = unsafe { mem::transmute(wparam as u32) }; - send_event(window, ReceivedCharacter(chr)); - 0 - }, - - winapi::WM_MOUSEMOVE => { - use events::Event::MouseMoved; - - let x = winapi::GET_X_LPARAM(lparam) as i32; - let y = winapi::GET_Y_LPARAM(lparam) as i32; - - send_event(window, MouseMoved((x, y))); - - 0 - }, - - winapi::WM_MOUSEWHEEL => { - use events::Event::MouseWheel; - - let value = (wparam >> 16) as i16; - let value = value as i32; - - send_event(window, MouseWheel(value)); - - 0 - }, - - winapi::WM_KEYDOWN => { - use events::Event::KeyboardInput; - use events::ElementState::Pressed; - let scancode = ((lparam >> 16) & 0xff) as u8; - let vkey = event::vkeycode_to_element(wparam); - send_event(window, KeyboardInput(Pressed, scancode, vkey)); - 0 - }, - - winapi::WM_KEYUP => { - use events::Event::KeyboardInput; - use events::ElementState::Released; - let scancode = ((lparam >> 16) & 0xff) as u8; - let vkey = event::vkeycode_to_element(wparam); - send_event(window, KeyboardInput(Released, scancode, vkey)); - 0 - }, - - winapi::WM_LBUTTONDOWN => { - use events::Event::MouseInput; - use events::MouseButton::Left; - use events::ElementState::Pressed; - send_event(window, MouseInput(Pressed, Left)); - 0 - }, - - winapi::WM_LBUTTONUP => { - use events::Event::MouseInput; - use events::MouseButton::Left; - use events::ElementState::Released; - send_event(window, MouseInput(Released, Left)); - 0 - }, - - winapi::WM_RBUTTONDOWN => { - use events::Event::MouseInput; - use events::MouseButton::Right; - use events::ElementState::Pressed; - send_event(window, MouseInput(Pressed, Right)); - 0 - }, - - winapi::WM_RBUTTONUP => { - use events::Event::MouseInput; - use events::MouseButton::Right; - use events::ElementState::Released; - send_event(window, MouseInput(Released, Right)); - 0 - }, - - winapi::WM_MBUTTONDOWN => { - use events::Event::MouseInput; - use events::MouseButton::Middle; - use events::ElementState::Pressed; - send_event(window, MouseInput(Pressed, Middle)); - 0 - }, - - winapi::WM_MBUTTONUP => { - use events::Event::MouseInput; - use events::MouseButton::Middle; - use events::ElementState::Released; - send_event(window, MouseInput(Released, Middle)); - 0 - }, - - winapi::WM_SETFOCUS => { - use events::Event::Focused; - send_event(window, Focused(true)); - 0 - }, - - winapi::WM_KILLFOCUS => { - use events::Event::Focused; - send_event(window, Focused(false)); - 0 - }, - - _ => unsafe { - user32::DefWindowProcW(window, msg, wparam, lparam) - } - } -} - -/*fn hints_to_pixelformat(hints: &Hints) -> winapi::PIXELFORMATDESCRIPTOR { - use std::mem; - - winapi::PIXELFORMATDESCRIPTOR { - nSize: size_of::<winapi::PIXELFORMATDESCRIPTOR>(), - nVersion: 1, - dwFlags: - if hints.stereo { PFD_STEREO } else { 0 }, - iPixelType: PFD_TYPE_RGBA, - cColorBits: hints.red_bits + hints.green_bits + hints.blue_bits, - cRedBits: - - pub nSize: WORD, - pub nVersion: WORD, - pub dwFlags: DWORD, - pub iPixelType: BYTE, - pub cColorBits: BYTE, - pub cRedBits: BYTE, - pub cRedShift: BYTE, - pub cGreenBits: BYTE, - pub cGreenShift: BYTE, - pub cBlueBits: BYTE, - pub cBlueShift: BYTE, - pub cAlphaBits: BYTE, - pub cAlphaShift: BYTE, - pub cAccumBits: BYTE, - pub cAccumRedBits: BYTE, - pub cAccumGreenBits: BYTE, - pub cAccumBlueBits: BYTE, - pub cAccumAlphaBits: BYTE, - pub cDepthBits: BYTE, - pub cStencilBits: BYTE, - pub cAuxBuffers: BYTE, - pub iLayerType: BYTE, - pub bReserved: BYTE, - pub dwLayerMask: DWORD, - pub dwVisibleMask: DWORD, - pub dwDamageMask: DWORD, - } -}*/ diff --git a/src/win32/mod.rs b/src/win32/mod.rs index c2fd103..8076bcc 100644 --- a/src/win32/mod.rs +++ b/src/win32/mod.rs @@ -8,6 +8,7 @@ use {CreationError, Event, MouseCursor}; use BuilderAttribs; +pub use self::headless::HeadlessContext; pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor}; use winapi; @@ -15,45 +16,13 @@ use user32; use kernel32; use gdi32; +mod callback; mod event; mod gl; +mod headless; mod init; mod monitor; -/// -pub struct HeadlessContext(Window); - -impl HeadlessContext { - /// See the docs in the crate root file. - pub fn new(builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> { - let (builder, _) = builder.extract_non_static(); - init::new_window(builder, None).map(|w| HeadlessContext(w)) - } - - /// See the docs in the crate root file. - pub unsafe fn make_current(&self) { - self.0.make_current() - } - - /// See the docs in the crate root file. - pub fn get_proc_address(&self, addr: &str) -> *const () { - self.0.get_proc_address(addr) - } - - /// See the docs in the crate root file. - pub fn get_api(&self) -> ::Api { - ::Api::OpenGl - } - - pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) { - } -} - -#[cfg(feature = "headless")] -unsafe impl Send for HeadlessContext {} -#[cfg(feature = "headless")] -unsafe impl Sync for HeadlessContext {} - /// The Win32 implementation of the main `Window` object. pub struct Window { /// Main handle for the window. |