3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
32 * Copyright (C) 2001 NaN Technologies B.V.
33 * @author Maarten Gribnau
39 #include "GHOST_SystemWin32.h"
40 #include "GHOST_EventDragnDrop.h"
42 #define WIN32_LEAN_AND_MEAN
46 #define _WIN32_IE 0x0501
50 // win64 doesn't define GWL_USERDATA
53 #define GWL_USERDATA GWLP_USERDATA
54 #define GWL_WNDPROC GWLP_WNDPROC
59 * According to the docs the mouse wheel message is supported from windows 98
60 * upwards. Leaving WINVER at default value, the WM_MOUSEWHEEL message and the
61 * wheel detent value are undefined.
64 #define WM_MOUSEWHEEL 0x020A
65 #endif // WM_MOUSEWHEEL
67 #define WHEEL_DELTA 120 /* Value for rolling one detent, (old convention! MS changed it) */
71 * Defines for mouse buttons 4 and 5 aka xbutton1 and xbutton2.
72 * MSDN: Declared in Winuser.h, include Windows.h
73 * This does not seem to work with MinGW so we define our own here.
76 #define XBUTTON1 0x0001
79 #define XBUTTON2 0x0002
82 #define WM_XBUTTONUP 524
83 #endif // WM_XBUTTONUP
84 #ifndef WM_XBUTTONDOWN
85 #define WM_XBUTTONDOWN 523
86 #endif // WM_XBUTTONDOWN
88 #include "GHOST_Debug.h"
89 #include "GHOST_DisplayManagerWin32.h"
90 #include "GHOST_EventButton.h"
91 #include "GHOST_EventCursor.h"
92 #include "GHOST_EventKey.h"
93 #include "GHOST_EventWheel.h"
94 #include "GHOST_EventNDOF.h"
95 #include "GHOST_TimerTask.h"
96 #include "GHOST_TimerManager.h"
97 #include "GHOST_WindowManager.h"
98 #include "GHOST_WindowWin32.h"
99 #include "GHOST_NDOFManager.h"
101 // Key code values not found in winuser.h
103 #define VK_MINUS 0xBD
106 #define VK_SEMICOLON 0xBA
107 #endif // VK_SEMICOLON
109 #define VK_PERIOD 0xBE
112 #define VK_COMMA 0xBC
115 #define VK_QUOTE 0xDE
117 #ifndef VK_BACK_QUOTE
118 #define VK_BACK_QUOTE 0xC0
119 #endif // VK_BACK_QUOTE
121 #define VK_SLASH 0xBF
123 #ifndef VK_BACK_SLASH
124 #define VK_BACK_SLASH 0xDC
125 #endif // VK_BACK_SLASH
127 #define VK_EQUALS 0xBB
129 #ifndef VK_OPEN_BRACKET
130 #define VK_OPEN_BRACKET 0xDB
131 #endif // VK_OPEN_BRACKET
132 #ifndef VK_CLOSE_BRACKET
133 #define VK_CLOSE_BRACKET 0xDD
134 #endif // VK_CLOSE_BRACKET
136 #define VK_GR_LESS 0xE2
140 GHOST_SystemWin32::GHOST_SystemWin32()
141 : m_hasPerformanceCounter(false), m_freq(0), m_start(0)
143 m_displayManager = new GHOST_DisplayManagerWin32 ();
144 GHOST_ASSERT(m_displayManager, "GHOST_SystemWin32::GHOST_SystemWin32(): m_displayManager==0\n");
145 m_displayManager->initialize();
147 // Check if current keyboard layout uses AltGr and save keylayout ID for
148 // specialized handling if keys like VK_OEM_*. I.e. french keylayout
149 // generates VK_OEM_8 for their exclamation key (key left of right shift)
150 this->handleKeyboardChange();
151 // Require COM for GHOST_DropTargetWin32 created in GHOST_WindowWin32.
155 GHOST_SystemWin32::~GHOST_SystemWin32()
162 GHOST_TUns64 GHOST_SystemWin32::getMilliSeconds() const
164 // Hardware does not support high resolution timers. We will use GetTickCount instead then.
165 if (!m_hasPerformanceCounter) {
166 return ::GetTickCount();
169 // Retrieve current count
171 ::QueryPerformanceCounter((LARGE_INTEGER*)&count);
173 // Calculate the time passed since system initialization.
174 __int64 delta = 1000*(count-m_start);
176 GHOST_TUns64 t = (GHOST_TUns64)(delta/m_freq);
181 GHOST_TUns8 GHOST_SystemWin32::getNumDisplays() const
183 GHOST_ASSERT(m_displayManager, "GHOST_SystemWin32::getNumDisplays(): m_displayManager==0\n");
184 GHOST_TUns8 numDisplays;
185 m_displayManager->getNumDisplays(numDisplays);
190 void GHOST_SystemWin32::getMainDisplayDimensions(GHOST_TUns32& width, GHOST_TUns32& height) const
192 width = ::GetSystemMetrics(SM_CXSCREEN);
193 height= ::GetSystemMetrics(SM_CYSCREEN);
197 GHOST_IWindow* GHOST_SystemWin32::createWindow(
198 const STR_String& title,
199 GHOST_TInt32 left, GHOST_TInt32 top, GHOST_TUns32 width, GHOST_TUns32 height,
200 GHOST_TWindowState state, GHOST_TDrawingContextType type,
201 bool stereoVisual, const GHOST_TUns16 numOfAASamples, const GHOST_TEmbedderWindowID parentWindow )
203 GHOST_Window* window = 0;
204 window = new GHOST_WindowWin32 (this, title, left, top, width, height, state, type, stereoVisual, numOfAASamples);
206 if (window->getValid()) {
207 // Store the pointer to the window
208 // if (state != GHOST_kWindowStateFullScreen) {
209 m_windowManager->addWindow(window);
213 // An invalid window could be one that was used to test for AA
214 window = ((GHOST_WindowWin32*)window)->getNextWindow();
216 // If another window is found, let the wm know about that one, but not the old one
217 if (window->getValid()) {
218 m_windowManager->addWindow(window);
231 bool GHOST_SystemWin32::processEvents(bool waitForEvent)
234 bool anyProcessed = false;
237 GHOST_TimerManager* timerMgr = getTimerManager();
239 if (waitForEvent && !::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE)) {
243 GHOST_TUns64 next = timerMgr->nextFireTime();
244 GHOST_TInt64 maxSleep = next - getMilliSeconds();
246 if (next == GHOST_kFireTimeNever) {
248 } else if(maxSleep >= 0.0) {
249 ::SetTimer(NULL, 0, maxSleep, NULL);
251 ::KillTimer(NULL, 0);
256 if (timerMgr->fireTimers(getMilliSeconds())) {
260 // Process all the events waiting for us
261 while (::PeekMessage(&msg, 0, 0, 0, PM_REMOVE) != 0) {
262 ::TranslateMessage(&msg);
263 ::DispatchMessage(&msg);
266 } while (waitForEvent && !anyProcessed);
272 GHOST_TSuccess GHOST_SystemWin32::getCursorPosition(GHOST_TInt32& x, GHOST_TInt32& y) const
275 if(::GetCursorPos(&point)){
278 return GHOST_kSuccess;
280 return GHOST_kFailure;
284 GHOST_TSuccess GHOST_SystemWin32::setCursorPosition(GHOST_TInt32 x, GHOST_TInt32 y)
286 if (!GetActiveWindow())
287 return GHOST_kFailure;
288 return ::SetCursorPos(x, y) == TRUE ? GHOST_kSuccess : GHOST_kFailure;
292 GHOST_TSuccess GHOST_SystemWin32::getModifierKeys(GHOST_ModifierKeys& keys) const
294 bool down = HIBYTE(::GetKeyState(VK_LSHIFT)) != 0;
295 keys.set(GHOST_kModifierKeyLeftShift, down);
296 down = HIBYTE(::GetKeyState(VK_RSHIFT)) != 0;
297 keys.set(GHOST_kModifierKeyRightShift, down);
299 down = HIBYTE(::GetKeyState(VK_LMENU)) != 0;
300 keys.set(GHOST_kModifierKeyLeftAlt, down);
301 down = HIBYTE(::GetKeyState(VK_RMENU)) != 0;
302 keys.set(GHOST_kModifierKeyRightAlt, down);
304 down = HIBYTE(::GetKeyState(VK_LCONTROL)) != 0;
305 keys.set(GHOST_kModifierKeyLeftControl, down);
306 down = HIBYTE(::GetKeyState(VK_RCONTROL)) != 0;
307 keys.set(GHOST_kModifierKeyRightControl, down);
309 bool lwindown = HIBYTE(::GetKeyState(VK_LWIN)) != 0;
310 bool rwindown = HIBYTE(::GetKeyState(VK_RWIN)) != 0;
311 if(lwindown || rwindown)
312 keys.set(GHOST_kModifierKeyOS, true);
314 keys.set(GHOST_kModifierKeyOS, false);
315 return GHOST_kSuccess;
319 GHOST_TSuccess GHOST_SystemWin32::getButtons(GHOST_Buttons& buttons) const
321 /* Check for swapped buttons (left-handed mouse buttons)
322 * GetAsyncKeyState() will give back the state of the physical mouse buttons.
324 bool swapped = ::GetSystemMetrics(SM_SWAPBUTTON) == TRUE;
326 bool down = HIBYTE(::GetKeyState(VK_LBUTTON)) != 0;
327 buttons.set(swapped ? GHOST_kButtonMaskRight : GHOST_kButtonMaskLeft, down);
329 down = HIBYTE(::GetKeyState(VK_MBUTTON)) != 0;
330 buttons.set(GHOST_kButtonMaskMiddle, down);
332 down = HIBYTE(::GetKeyState(VK_RBUTTON)) != 0;
333 buttons.set(swapped ? GHOST_kButtonMaskLeft : GHOST_kButtonMaskRight, down);
334 return GHOST_kSuccess;
338 GHOST_TSuccess GHOST_SystemWin32::init()
340 GHOST_TSuccess success = GHOST_System::init();
342 for(int i = 0; i < 255; i++) {
343 m_prevKeyStatus[i] = false;
344 m_curKeyStatus[i] = false;
347 /* Disable scaling on high DPI displays on Vista */
348 HMODULE user32 = ::LoadLibraryA("user32.dll");
349 typedef BOOL (WINAPI * LPFNSETPROCESSDPIAWARE)();
350 LPFNSETPROCESSDPIAWARE SetProcessDPIAware =
351 (LPFNSETPROCESSDPIAWARE)GetProcAddress(user32, "SetProcessDPIAware");
352 if (SetProcessDPIAware)
353 SetProcessDPIAware();
356 // Determine whether this system has a high frequency performance counter. */
357 m_hasPerformanceCounter = ::QueryPerformanceFrequency((LARGE_INTEGER*)&m_freq) == TRUE;
358 if (m_hasPerformanceCounter) {
359 GHOST_PRINT("GHOST_SystemWin32::init: High Frequency Performance Timer available\n")
360 ::QueryPerformanceCounter((LARGE_INTEGER*)&m_start);
363 GHOST_PRINT("GHOST_SystemWin32::init: High Frequency Performance Timer not available\n")
368 wc.style= CS_HREDRAW | CS_VREDRAW;
369 wc.lpfnWndProc= s_wndProc;
372 wc.hInstance= ::GetModuleHandle(0);
373 wc.hIcon = ::LoadIcon(wc.hInstance, "APPICON");
376 ::LoadIcon(NULL, IDI_APPLICATION);
378 wc.hCursor = ::LoadCursor(0, IDC_ARROW);
379 wc.hbrBackground= (HBRUSH)::GetStockObject(BLACK_BRUSH);
381 wc.lpszClassName= GHOST_WindowWin32::getWindowClassName();
383 // Use RegisterClassEx for setting small icon
384 if (::RegisterClass(&wc) == 0) {
385 success = GHOST_kFailure;
388 // Add low-level keyboard hook for our process.
389 m_llKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, s_llKeyboardProc, wc.hInstance, 0);
396 GHOST_TSuccess GHOST_SystemWin32::exit()
398 // remove our low-level keyboard hook.
399 UnhookWindowsHookEx(m_llKeyboardHook);
401 return GHOST_System::exit();
404 void GHOST_SystemWin32::triggerKey(GHOST_IWindow *window, bool down, GHOST_TKey key)
406 GHOST_Event *extra = new GHOST_EventKey(getSystem()->getMilliSeconds(), down ? GHOST_kEventKeyDown : GHOST_kEventKeyUp, window, key, '\0');
407 ((GHOST_SystemWin32*)getSystem())->pushEvent(extra);
409 void GHOST_SystemWin32::handleModifierKeys(GHOST_IWindow *window, WPARAM wParam, LPARAM lParam, GHOST_ModifierKeys &oldModifiers, GHOST_ModifierKeys &newModifiers) const
414 bool lchanged = oldModifiers.get(GHOST_kModifierKeyLeftAlt) != newModifiers.get(GHOST_kModifierKeyLeftAlt);
416 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyLeftAlt), GHOST_kKeyLeftAlt);
418 bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightAlt) != newModifiers.get(GHOST_kModifierKeyRightAlt);
420 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyRightAlt), GHOST_kKeyRightAlt);
423 lchanged = oldModifiers.get(GHOST_kModifierKeyLeftControl) != newModifiers.get(GHOST_kModifierKeyLeftControl);
425 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyLeftControl), GHOST_kKeyLeftControl);
427 bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightControl) != newModifiers.get(GHOST_kModifierKeyRightControl);
429 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyRightControl), GHOST_kKeyRightControl);
436 bool lchanged = oldModifiers.get(GHOST_kModifierKeyLeftAlt) != newModifiers.get(GHOST_kModifierKeyLeftAlt);
438 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyLeftAlt), GHOST_kKeyLeftAlt);
440 bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightAlt) != newModifiers.get(GHOST_kModifierKeyRightAlt);
442 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyRightAlt), GHOST_kKeyRightAlt);
445 lchanged = oldModifiers.get(GHOST_kModifierKeyLeftShift) != newModifiers.get(GHOST_kModifierKeyLeftShift);
447 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyLeftShift), GHOST_kKeyLeftShift);
449 bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightShift) != newModifiers.get(GHOST_kModifierKeyRightShift);
451 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyRightShift), GHOST_kKeyRightShift);
458 bool lchanged = oldModifiers.get(GHOST_kModifierKeyLeftShift) != newModifiers.get(GHOST_kModifierKeyLeftShift);
460 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyLeftShift), GHOST_kKeyLeftShift);
462 bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightShift) != newModifiers.get(GHOST_kModifierKeyRightShift);
464 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyRightShift), GHOST_kKeyRightShift);
467 lchanged = oldModifiers.get(GHOST_kModifierKeyLeftControl) != newModifiers.get(GHOST_kModifierKeyLeftControl);
469 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyLeftControl), GHOST_kKeyLeftControl);
471 bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightControl) != newModifiers.get(GHOST_kModifierKeyRightControl);
473 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyRightControl), GHOST_kKeyRightControl);
483 //! note: this function can be extended to include other exotic cases as they arise.
484 // This function was added in response to bug [#25715]
485 GHOST_TKey GHOST_SystemWin32::processSpecialKey(GHOST_IWindow *window, WPARAM wParam, LPARAM lParam) const
487 GHOST_TKey key = GHOST_kKeyUnknown;
488 switch(PRIMARYLANGID(m_langId)) {
490 if(wParam==VK_OEM_8) key = GHOST_kKey1; // on 'normal' shift + 1 to create '!' we also get GHOST_kKey1. ASCII will be '!'.
497 GHOST_TKey GHOST_SystemWin32::convertKey(GHOST_IWindow *window, WPARAM wParam, LPARAM lParam) const
499 GHOST_SystemWin32 *system = (GHOST_SystemWin32 *)getSystem();
500 bool isExtended = (lParam&(1<<24))?true:false;
503 GHOST_ModifierKeys oldModifiers, newModifiers;
504 system->retrieveModifierKeys(oldModifiers);
505 system->getModifierKeys(newModifiers);
507 //std::cout << wParam << " " << system->m_curKeyStatus[wParam] << " shift pressed: " << system->shiftPressed() << std::endl;
509 if ((wParam >= '0') && (wParam <= '9')) {
510 // VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39)
511 key = (GHOST_TKey)(wParam - '0' + GHOST_kKey0);
513 else if ((wParam >= 'A') && (wParam <= 'Z')) {
514 // VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A)
515 key = (GHOST_TKey)(wParam - 'A' + GHOST_kKeyA);
517 else if ((wParam >= VK_F1) && (wParam <= VK_F24)) {
518 key = (GHOST_TKey)(wParam - VK_F1 + GHOST_kKeyF1);
523 key = isExtended?GHOST_kKeyNumpadEnter:GHOST_kKeyEnter;
526 case VK_BACK: key = GHOST_kKeyBackSpace; break;
527 case VK_TAB: key = GHOST_kKeyTab; break;
528 case VK_ESCAPE: key = GHOST_kKeyEsc; break;
529 case VK_SPACE: key = GHOST_kKeySpace; break;
530 case VK_PRIOR: key = GHOST_kKeyUpPage; break;
531 case VK_NEXT: key = GHOST_kKeyDownPage; break;
532 case VK_END: key = GHOST_kKeyEnd; break;
535 if(system->m_curKeyStatus[VK_NUMPAD7] && system->shiftPressed())
536 key = GHOST_kKeyNumpad7;
538 key = GHOST_kKeyHome;
541 case VK_INSERT: key = GHOST_kKeyInsert; break;
542 case VK_DELETE: key = GHOST_kKeyDelete; break;
543 case VK_LEFT: key = GHOST_kKeyLeftArrow; break;
544 case VK_RIGHT: key = GHOST_kKeyRightArrow; break;
545 case VK_UP: key = GHOST_kKeyUpArrow; break;
546 case VK_DOWN: key = GHOST_kKeyDownArrow; break;
547 case VK_NUMPAD0: key = GHOST_kKeyNumpad0; break;
548 case VK_NUMPAD1: key = GHOST_kKeyNumpad1; break;
549 case VK_NUMPAD2: key = GHOST_kKeyNumpad2; break;
550 case VK_NUMPAD3: key = GHOST_kKeyNumpad3; break;
551 case VK_NUMPAD4: key = GHOST_kKeyNumpad4; break;
552 case VK_NUMPAD5: key = GHOST_kKeyNumpad5; break;
553 case VK_NUMPAD6: key = GHOST_kKeyNumpad6; break;
554 case VK_NUMPAD7: key = GHOST_kKeyNumpad7; break;
555 case VK_NUMPAD8: key = GHOST_kKeyNumpad8; break;
556 case VK_NUMPAD9: key = GHOST_kKeyNumpad9; break;
557 case VK_SNAPSHOT: key = GHOST_kKeyPrintScreen; break;
558 case VK_PAUSE: key = GHOST_kKeyPause; break;
559 case VK_MULTIPLY: key = GHOST_kKeyNumpadAsterisk; break;
560 case VK_SUBTRACT: key = GHOST_kKeyNumpadMinus; break;
561 case VK_DECIMAL: key = GHOST_kKeyNumpadPeriod; break;
562 case VK_DIVIDE: key = GHOST_kKeyNumpadSlash; break;
563 case VK_ADD: key = GHOST_kKeyNumpadPlus; break;
565 case VK_SEMICOLON: key = GHOST_kKeySemicolon; break;
566 case VK_EQUALS: key = GHOST_kKeyEqual; break;
567 case VK_COMMA: key = GHOST_kKeyComma; break;
568 case VK_MINUS: key = GHOST_kKeyMinus; break;
569 case VK_PERIOD: key = GHOST_kKeyPeriod; break;
570 case VK_SLASH: key = GHOST_kKeySlash; break;
571 case VK_BACK_QUOTE: key = GHOST_kKeyAccentGrave; break;
572 case VK_OPEN_BRACKET: key = GHOST_kKeyLeftBracket; break;
573 case VK_BACK_SLASH: key = GHOST_kKeyBackslash; break;
574 case VK_CLOSE_BRACKET: key = GHOST_kKeyRightBracket; break;
575 case VK_QUOTE: key = GHOST_kKeyQuote; break;
576 case VK_GR_LESS: key = GHOST_kKeyGrLess; break;
580 bool lchanged = oldModifiers.get(GHOST_kModifierKeyLeftShift) != newModifiers.get(GHOST_kModifierKeyLeftShift);
582 key = GHOST_kKeyLeftShift;
584 bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightShift) != newModifiers.get(GHOST_kModifierKeyRightShift);
586 key = GHOST_kKeyRightShift;
588 key = GHOST_kKeyUnknown;
595 bool lchanged = oldModifiers.get(GHOST_kModifierKeyLeftControl) != newModifiers.get(GHOST_kModifierKeyLeftControl);
597 key = GHOST_kKeyLeftControl;
599 bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightControl) != newModifiers.get(GHOST_kModifierKeyRightControl);
601 key = GHOST_kKeyRightControl;
603 key = GHOST_kKeyUnknown;
610 if(m_hasAltGr && isExtended) {
611 // We have here an extended RAlt, which is AltGr. The keyboard driver on Windows sends before this a LControl, so
612 // to be able to input characters created with AltGr (normal on German, French, Finnish and other keyboards) we
613 // push an extra LControl up event. This ensures we don't have a 'hanging' ctrl event in Blender windowmanager
614 // when typing in Text editor or Console.
615 GHOST_Event *extra = new GHOST_EventKey(getSystem()->getMilliSeconds(), GHOST_kEventKeyUp, window, GHOST_kKeyLeftControl, '\0');
616 ((GHOST_SystemWin32*)getSystem())->pushEvent(extra);
617 newModifiers.set(GHOST_kModifierKeyRightControl, false);
618 newModifiers.set(GHOST_kModifierKeyLeftControl, false);
620 bool lchanged = oldModifiers.get(GHOST_kModifierKeyLeftAlt) != newModifiers.get(GHOST_kModifierKeyLeftAlt);
622 key = GHOST_kKeyLeftAlt;
624 bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightAlt) != newModifiers.get(GHOST_kModifierKeyRightAlt);
626 key = GHOST_kKeyRightAlt;
628 key = GHOST_kKeyUnknown;
637 case VK_NUMLOCK: key = GHOST_kKeyNumLock; break;
638 case VK_SCROLL: key = GHOST_kKeyScrollLock; break;
639 case VK_CAPITAL: key = GHOST_kKeyCapsLock; break;
641 key = ((GHOST_SystemWin32*)getSystem())->processSpecialKey(window, wParam, lParam);
644 key = GHOST_kKeyUnknown;
648 ((GHOST_SystemWin32*)getSystem())->storeModifierKeys(newModifiers);
652 GHOST_EventButton* GHOST_SystemWin32::processButtonEvent(GHOST_TEventType type, GHOST_IWindow *window, GHOST_TButtonMask mask)
654 return new GHOST_EventButton (getSystem()->getMilliSeconds(), type, window, mask);
658 GHOST_EventCursor* GHOST_SystemWin32::processCursorEvent(GHOST_TEventType type, GHOST_IWindow *Iwindow)
660 GHOST_TInt32 x_screen, y_screen;
661 GHOST_SystemWin32 * system = ((GHOST_SystemWin32 * ) getSystem());
662 GHOST_WindowWin32 * window = ( GHOST_WindowWin32 * ) Iwindow;
664 system->getCursorPosition(x_screen, y_screen);
666 if(window->getCursorGrabMode() != GHOST_kGrabDisable && window->getCursorGrabMode() != GHOST_kGrabNormal)
668 GHOST_TInt32 x_new= x_screen;
669 GHOST_TInt32 y_new= y_screen;
670 GHOST_TInt32 x_accum, y_accum;
673 /* fallback to window bounds */
674 if(window->getCursorGrabBounds(bounds)==GHOST_kFailure){
675 window->getClientBounds(bounds);
678 /* could also clamp to screen bounds
679 * wrap with a window outside the view will fail atm */
681 bounds.wrapPoint(x_new, y_new, 2); /* offset of one incase blender is at screen bounds */
683 window->getCursorGrabAccum(x_accum, y_accum);
684 if(x_new != x_screen|| y_new != y_screen) {
685 /* when wrapping we don't need to add an event because the
686 * setCursorPosition call will cause a new event after */
687 system->setCursorPosition(x_new, y_new); /* wrap */
688 window->setCursorGrabAccum(x_accum + (x_screen - x_new), y_accum + (y_screen - y_new));
690 return new GHOST_EventCursor(system->getMilliSeconds(),
691 GHOST_kEventCursorMove,
700 return new GHOST_EventCursor(system->getMilliSeconds(),
701 GHOST_kEventCursorMove,
711 GHOST_EventWheel* GHOST_SystemWin32::processWheelEvent(GHOST_IWindow *window, WPARAM wParam, LPARAM lParam)
713 // short fwKeys = LOWORD(wParam); // key flags
714 int zDelta = (short) HIWORD(wParam); // wheel rotation
716 // zDelta /= WHEEL_DELTA;
717 // temporary fix below: microsoft now has added more precision, making the above division not work
718 if (zDelta <= 0 ) zDelta= -1; else zDelta= 1;
720 // short xPos = (short) LOWORD(lParam); // horizontal position of pointer
721 // short yPos = (short) HIWORD(lParam); // vertical position of pointer
722 return new GHOST_EventWheel (getSystem()->getMilliSeconds(), window, zDelta);
726 GHOST_EventKey* GHOST_SystemWin32::processKeyEvent(GHOST_IWindow *window, bool keyDown, WPARAM wParam, LPARAM lParam)
728 GHOST_TKey key = ((GHOST_SystemWin32*)getSystem())->convertKey(window, wParam, lParam);
729 GHOST_EventKey* event;
730 if (key != GHOST_kKeyUnknown) {
734 /* Eat any character related messages */
735 if (::PeekMessage(&keyMsg, NULL, WM_CHAR, WM_SYSDEADCHAR, PM_REMOVE)) {
736 ascii = (char) keyMsg.wParam;
740 event = new GHOST_EventKey(getSystem()->getMilliSeconds(), keyDown ? GHOST_kEventKeyDown: GHOST_kEventKeyUp, window, key, ascii);
741 std::cout << ascii << std::endl;
750 GHOST_Event* GHOST_SystemWin32::processWindowEvent(GHOST_TEventType type, GHOST_IWindow* window)
752 return new GHOST_Event(getSystem()->getMilliSeconds(), type, window);
755 GHOST_TSuccess GHOST_SystemWin32::pushDragDropEvent(GHOST_TEventType eventType,
756 GHOST_TDragnDropTypes draggedObjectType,
757 GHOST_IWindow* window,
758 int mouseX, int mouseY,
761 GHOST_SystemWin32* system = ((GHOST_SystemWin32*)getSystem());
762 return system->pushEvent(new GHOST_EventDragnDrop(system->getMilliSeconds(),
765 window,mouseX,mouseY,data)
769 void GHOST_SystemWin32::processMinMaxInfo(MINMAXINFO * minmax)
771 minmax->ptMinTrackSize.x=320;
772 minmax->ptMinTrackSize.y=240;
775 /* Note that this function gets *all* key events from the entire system (all
776 * threads running in this desktop session. So when getting event here, don't assume
777 * it's for Blender. Thus we only do status bookkeeping, so we can check
778 * in s_wndProc and processKeyEvent what the real keyboard status is.
779 * This is needed for proper handling of shift+numpad keys for instance.
781 LRESULT CALLBACK GHOST_SystemWin32::s_llKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
783 GHOST_SystemWin32* system = ((GHOST_SystemWin32*)getSystem());
786 if(wParam==WM_KEYDOWN || wParam==WM_SYSKEYDOWN ){
791 return CallNextHookEx(system->m_llKeyboardHook, nCode, wParam, lParam);
793 KBDLLHOOKSTRUCT &keyb = *(PKBDLLHOOKSTRUCT)(lParam);
794 system->m_prevKeyStatus[keyb.vkCode] = system->m_curKeyStatus[keyb.vkCode];
795 //std::cout << "ll: " << keyb.vkCode << " " << down << " ||| ";
797 if((keyb.flags & LLKHF_EXTENDED) == LLKHF_EXTENDED) {
798 //std::cout << "extended ";
800 if((keyb.flags & LLKHF_ALTDOWN) == LLKHF_ALTDOWN) {
801 //std::cout << "alt ";
803 if((keyb.flags & LLKHF_INJECTED)== LLKHF_INJECTED) {
804 //std::cout << "injected ";
806 if((keyb.flags & LLKHF_UP) == LLKHF_UP) {
807 system->m_curKeyStatus[keyb.vkCode] = false;
808 //std::cout << "up" << std::endl;
810 system->m_curKeyStatus[keyb.vkCode] = true;
811 //std::cout << "down" << std::endl;
815 system->m_curKeyStatus[keyb.vkCode] = true;
816 //std::cout << "down" << std::endl;
819 return CallNextHookEx(system->m_llKeyboardHook, nCode, wParam, lParam);
822 LRESULT WINAPI GHOST_SystemWin32::s_wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
824 GHOST_Event* event = 0;
826 GHOST_SystemWin32* system = ((GHOST_SystemWin32*)getSystem());
827 GHOST_ASSERT(system, "GHOST_SystemWin32::s_wndProc(): system not initialized")
830 GHOST_WindowWin32* window = (GHOST_WindowWin32*)::GetWindowLong(hwnd, GWL_USERDATA);
833 // we need to check if new key layout has AltGr
834 case WM_INPUTLANGCHANGE:
835 system->handleKeyboardChange();
837 ////////////////////////////////////////////////////////////////////////
838 // Keyboard events, processed
839 ////////////////////////////////////////////////////////////////////////
842 event = processKeyEvent(window, true, wParam, lParam);
844 GHOST_PRINT("GHOST_SystemWin32::wndProc: key event ")
846 GHOST_PRINT(" key ignored\n")
852 event = processKeyEvent(window, false, wParam, lParam);
854 GHOST_PRINT("GHOST_SystemWin32::wndProc: key event ")
856 GHOST_PRINT(" key ignored\n")
860 ////////////////////////////////////////////////////////////////////////
861 // Keyboard events, ignored
862 ////////////////////////////////////////////////////////////////////////
864 /* The WM_CHAR message is posted to the window with the keyboard focus when
865 * a WM_KEYDOWN message is translated by the TranslateMessage function. WM_CHAR
866 * contains the character code of the key that was pressed.
869 /* The WM_DEADCHAR message is posted to the window with the keyboard focus when a
870 * WM_KEYUP message is translated by the TranslateMessage function. WM_DEADCHAR
871 * specifies a character code generated by a dead key. A dead key is a key that
872 * generates a character, such as the umlaut (double-dot), that is combined with
873 * another character to form a composite character. For example, the umlaut-O
874 * character (Ö) is generated by typing the dead key for the umlaut character, and
875 * then typing the O key.
878 /* The WM_SYSDEADCHAR message is sent to the window with the keyboard focus when
879 * a WM_SYSKEYDOWN message is translated by the TranslateMessage function.
880 * WM_SYSDEADCHAR specifies the character code of a system dead key - that is,
881 * a dead key that is pressed while holding down the alt key.
884 ////////////////////////////////////////////////////////////////////////
885 // Tablet events, processed
886 ////////////////////////////////////////////////////////////////////////
888 ((GHOST_WindowWin32*)window)->processWin32TabletEvent(wParam, lParam);
892 ((GHOST_WindowWin32*)window)->processWin32TabletInitEvent();
894 ////////////////////////////////////////////////////////////////////////
895 // Mouse events, processed
896 ////////////////////////////////////////////////////////////////////////
898 window->registerMouseClickEvent(true);
899 event = processButtonEvent(GHOST_kEventButtonDown, window, GHOST_kButtonMaskLeft);
902 window->registerMouseClickEvent(true);
903 event = processButtonEvent(GHOST_kEventButtonDown, window, GHOST_kButtonMaskMiddle);
906 window->registerMouseClickEvent(true);
907 event = processButtonEvent(GHOST_kEventButtonDown, window, GHOST_kButtonMaskRight);
910 window->registerMouseClickEvent(true);
911 if ((short) HIWORD(wParam) == XBUTTON1){
912 event = processButtonEvent(GHOST_kEventButtonDown, window, GHOST_kButtonMaskButton4);
913 }else if((short) HIWORD(wParam) == XBUTTON2){
914 event = processButtonEvent(GHOST_kEventButtonDown, window, GHOST_kButtonMaskButton5);
918 window->registerMouseClickEvent(false);
919 event = processButtonEvent(GHOST_kEventButtonUp, window, GHOST_kButtonMaskLeft);
922 window->registerMouseClickEvent(false);
923 event = processButtonEvent(GHOST_kEventButtonUp, window, GHOST_kButtonMaskMiddle);
926 window->registerMouseClickEvent(false);
927 event = processButtonEvent(GHOST_kEventButtonUp, window, GHOST_kButtonMaskRight);
930 window->registerMouseClickEvent(false);
931 if ((short) HIWORD(wParam) == XBUTTON1){
932 event = processButtonEvent(GHOST_kEventButtonUp, window, GHOST_kButtonMaskButton4);
933 }else if((short) HIWORD(wParam) == XBUTTON2){
934 event = processButtonEvent(GHOST_kEventButtonUp, window, GHOST_kButtonMaskButton5);
938 event = processCursorEvent(GHOST_kEventCursorMove, window);
941 /* The WM_MOUSEWHEEL message is sent to the focus window
942 * when the mouse wheel is rotated. The DefWindowProc
943 * function propagates the message to the window's parent.
944 * There should be no internal forwarding of the message,
945 * since DefWindowProc propagates it up the parent chain
946 * until it finds a window that processes it.
948 event = processWheelEvent(window, wParam, lParam);
951 /* The WM_SETCURSOR message is sent to a window if the mouse causes the cursor
952 * to move within a window and mouse input is not captured.
953 * This means we have to set the cursor shape every time the mouse moves!
954 * The DefWindowProc function uses this message to set the cursor to an
955 * arrow if it is not in the client area.
957 if (LOWORD(lParam) == HTCLIENT) {
958 // Load the current cursor
959 window->loadCursor(window->getCursorVisibility(), window->getCursorShape());
960 // Bypass call to DefWindowProc
964 // Outside of client area show standard cursor
965 window->loadCursor(true, GHOST_kStandardCursorDefault);
969 ////////////////////////////////////////////////////////////////////////
970 // Mouse events, ignored
971 ////////////////////////////////////////////////////////////////////////
973 /* The WM_NCMOUSEMOVE message is posted to a window when the cursor is moved
974 * within the nonclient area of the window. This message is posted to the window
975 * that contains the cursor. If a window has captured the mouse, this message is not posted.
978 /* The WM_NCHITTEST message is sent to a window when the cursor moves, or
979 * when a mouse button is pressed or released. If the mouse is not captured,
980 * the message is sent to the window beneath the cursor. Otherwise, the message
981 * is sent to the window that has captured the mouse.
985 ////////////////////////////////////////////////////////////////////////
986 // Window events, processed
987 ////////////////////////////////////////////////////////////////////////
989 /* The WM_CLOSE message is sent as a signal that a window or an application should terminate. */
990 event = processWindowEvent(GHOST_kEventWindowClose, window);
993 /* The WM_ACTIVATE message is sent to both the window being activated and the window being
994 * deactivated. If the windows use the same input queue, the message is sent synchronously,
995 * first to the window procedure of the top-level window being deactivated, then to the window
996 * procedure of the top-level window being activated. If the windows use different input queues,
997 * the message is sent asynchronously, so the window is activated immediately.
999 event = processWindowEvent(LOWORD(wParam) ? GHOST_kEventWindowActivate : GHOST_kEventWindowDeactivate, window);
1000 /* WARNING: Let DefWindowProc handle WM_ACTIVATE, otherwise WM_MOUSEWHEEL
1001 will not be dispatched to OUR active window if we minimize one of OUR windows. */
1002 lResult = ::DefWindowProc(hwnd, msg, wParam, lParam);
1005 /* An application sends the WM_PAINT message when the system or another application
1006 * makes a request to paint a portion of an application's window. The message is sent
1007 * when the UpdateWindow or RedrawWindow function is called, or by the DispatchMessage
1008 * function when the application obtains a WM_PAINT message by using the GetMessage or
1009 * PeekMessage function.
1011 event = processWindowEvent(GHOST_kEventWindowUpdate, window);
1012 ::ValidateRect(hwnd, NULL);
1014 case WM_GETMINMAXINFO:
1015 /* The WM_GETMINMAXINFO message is sent to a window when the size or
1016 * position of the window is about to change. An application can use
1017 * this message to override the window's default maximized size and
1018 * position, or its default minimum or maximum tracking size.
1020 processMinMaxInfo((MINMAXINFO *) lParam);
1021 /* Let DefWindowProc handle it. */
1024 /* The WM_SIZE message is sent to a window after its size has changed.
1025 * The WM_SIZE and WM_MOVE messages are not sent if an application handles the
1026 * WM_WINDOWPOSCHANGED message without calling DefWindowProc. It is more efficient
1027 * to perform any move or size change processing during the WM_WINDOWPOSCHANGED
1028 * message without calling DefWindowProc.
1030 event = processWindowEvent(GHOST_kEventWindowSize, window);
1032 case WM_CAPTURECHANGED:
1033 window->lostMouseCapture();
1036 /* The WM_MOVING message is sent to a window that the user is moving. By processing
1037 * this message, an application can monitor the size and position of the drag rectangle
1038 * and, if needed, change its size or position.
1041 /* The WM_SIZE and WM_MOVE messages are not sent if an application handles the
1042 * WM_WINDOWPOSCHANGED message without calling DefWindowProc. It is more efficient
1043 * to perform any move or size change processing during the WM_WINDOWPOSCHANGED
1044 * message without calling DefWindowProc.
1046 event = processWindowEvent(GHOST_kEventWindowMove, window);
1048 ////////////////////////////////////////////////////////////////////////
1049 // Window events, ignored
1050 ////////////////////////////////////////////////////////////////////////
1051 case WM_WINDOWPOSCHANGED:
1052 /* The WM_WINDOWPOSCHANGED message is sent to a window whose size, position, or place
1053 * in the Z order has changed as a result of a call to the SetWindowPos function or
1054 * another window-management function.
1055 * The WM_SIZE and WM_MOVE messages are not sent if an application handles the
1056 * WM_WINDOWPOSCHANGED message without calling DefWindowProc. It is more efficient
1057 * to perform any move or size change processing during the WM_WINDOWPOSCHANGED
1058 * message without calling DefWindowProc.
1061 /* An application sends the WM_ERASEBKGND message when the window background must be
1062 * erased (for example, when a window is resized). The message is sent to prepare an
1063 * invalidated portion of a window for painting.
1066 /* An application sends the WM_NCPAINT message to a window when its frame must be painted. */
1068 /* The WM_NCACTIVATE message is sent to a window when its nonclient area needs to be changed
1069 * to indicate an active or inactive state.
1072 /* The WM_DESTROY message is sent when a window is being destroyed. It is sent to the window
1073 * procedure of the window being destroyed after the window is removed from the screen.
1074 * This message is sent first to the window being destroyed and then to the child windows
1075 * (if any) as they are destroyed. During the processing of the message, it can be assumed
1076 * that all child windows still exist.
1079 /* The WM_NCDESTROY message informs a window that its nonclient area is being destroyed. The
1080 * DestroyWindow function sends the WM_NCDESTROY message to the window following the WM_DESTROY
1081 * message. WM_DESTROY is used to free the allocated memory object associated with the window.
1084 /* The WM_KILLFOCUS message is sent to a window immediately before it loses the keyboard focus. */
1086 /* The WM_SHOWWINDOW message is sent to a window when the window is about to be hidden or shown. */
1087 case WM_WINDOWPOSCHANGING:
1088 /* The WM_WINDOWPOSCHANGING message is sent to a window whose size, position, or place in
1089 * the Z order is about to change as a result of a call to the SetWindowPos function or
1090 * another window-management function.
1093 /* The WM_SETFOCUS message is sent to a window after it has gained the keyboard focus. */
1094 case WM_ENTERSIZEMOVE:
1095 /* The WM_ENTERSIZEMOVE message is sent one time to a window after it enters the moving
1096 * or sizing modal loop. The window enters the moving or sizing modal loop when the user
1097 * clicks the window's title bar or sizing border, or when the window passes the
1098 * WM_SYSCOMMAND message to the DefWindowProc function and the wParam parameter of the
1099 * message specifies the SC_MOVE or SC_SIZE value. The operation is complete when
1100 * DefWindowProc returns.
1104 ////////////////////////////////////////////////////////////////////////
1106 ////////////////////////////////////////////////////////////////////////
1108 /* An application sends a WM_GETTEXT message to copy the text that
1109 * corresponds to a window into a buffer provided by the caller.
1111 case WM_ACTIVATEAPP:
1112 /* The WM_ACTIVATEAPP message is sent when a window belonging to a
1113 * different application than the active window is about to be activated.
1114 * The message is sent to the application whose window is being activated
1115 * and to the application whose window is being deactivated.
1118 /* The WIN32 docs say:
1119 * The WM_TIMER message is posted to the installing thread's message queue
1120 * when a timer expires. You can process the message by providing a WM_TIMER
1121 * case in the window procedure. Otherwise, the default window procedure will
1122 * call the TimerProc callback function specified in the call to the SetTimer
1123 * function used to install the timer.
1125 * In GHOST, we let DefWindowProc call the timer callback.
1128 case WM_BLND_NDOF_AXIS:
1130 GHOST_TEventNDOFData ndofdata;
1131 system->m_ndofManager->GHOST_NDOFGetDatas(ndofdata);
1132 system->m_eventManager->
1133 pushEvent(new GHOST_EventNDOF(
1134 system->getMilliSeconds(),
1135 GHOST_kEventNDOFMotion,
1139 case WM_BLND_NDOF_BTN:
1141 GHOST_TEventNDOFData ndofdata;
1142 system->m_ndofManager->GHOST_NDOFGetDatas(ndofdata);
1143 system->m_eventManager->
1144 pushEvent(new GHOST_EventNDOF(
1145 system->getMilliSeconds(),
1146 GHOST_kEventNDOFButton,
1153 // Event found for a window before the pointer to the class has been set.
1154 GHOST_PRINT("GHOST_SystemWin32::wndProc: GHOST window event before creation\n")
1155 /* These are events we typically miss at this point:
1156 WM_GETMINMAXINFO 0x24
1160 We let DefWindowProc do the work.
1165 // Events without valid hwnd
1166 GHOST_PRINT("GHOST_SystemWin32::wndProc: event without window\n")
1170 system->pushEvent(event);
1173 lResult = ::DefWindowProc(hwnd, msg, wParam, lParam);
1178 GHOST_TUns8* GHOST_SystemWin32::getClipboard(bool selection) const
1183 if ( IsClipboardFormatAvailable(CF_TEXT) && OpenClipboard(NULL) ) {
1184 HANDLE hData = GetClipboardData( CF_TEXT );
1185 if (hData == NULL) {
1189 buffer = (char*)GlobalLock( hData );
1191 temp_buff = (char*) malloc(strlen(buffer)+1);
1192 strcpy(temp_buff, buffer);
1194 GlobalUnlock( hData );
1197 temp_buff[strlen(buffer)] = '\0';
1199 return (GHOST_TUns8*)temp_buff;
1208 void GHOST_SystemWin32::putClipboard(GHOST_TInt8 *buffer, bool selection) const
1210 if(selection) {return;} // for copying the selection, used on X11
1212 if (OpenClipboard(NULL)) {
1219 clipbuffer = LocalAlloc(LMEM_FIXED,((strlen(buffer)+1)));
1220 data = (char*)GlobalLock(clipbuffer);
1222 strcpy(data, (char*)buffer);
1223 data[strlen(buffer)] = '\0';
1224 LocalUnlock(clipbuffer);
1225 SetClipboardData(CF_TEXT,clipbuffer);