IEWebGameWindow.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "StdAfx.h"
  23. #include <shlobj.h>
  24. #include "IEWebGameWindow.h"
  25. #include "../common/webCommon.h"
  26. // We hook the keyboard at application level so we TAB, Backspace, other accelerator combos
  27. // are captured and don't cause us grief
  28. static HHOOK hHook = NULL;
  29. // Hook procedure for WH_GETMESSAGE hook type.
  30. LRESULT CALLBACK GetMessageProc(int nCode, WPARAM wParam, LPARAM lParam)
  31. {
  32. // If this is a keystrokes message, translate it in controls'
  33. LPMSG lpMsg = (LPMSG) lParam;
  34. if( (nCode >= 0) &&
  35. PM_REMOVE == wParam &&
  36. (lpMsg->message >= WM_KEYFIRST && lpMsg->message <= WM_KEYLAST) )
  37. {
  38. if (torque_directmessage)
  39. {
  40. // call directly into the Torque 3D message queue, bypassing the windows event queue
  41. // as we're hooking into the application level processing, this would cause a hang
  42. torque_directmessage(lpMsg->message, lpMsg->wParam, lpMsg->lParam);
  43. // The value returned from this hookproc is ignored, and it cannot
  44. // be used to tell Windows the message has been handled. To avoid
  45. // further processing, convert the message to WM_NULL before
  46. // returning.
  47. lpMsg->message = WM_NULL;
  48. lpMsg->lParam = 0L;
  49. lpMsg->wParam = 0;
  50. }
  51. }
  52. // Passes the hook information to the next hook procedure in
  53. // the current hook chain.
  54. return ::CallNextHookEx(hHook, nCode, wParam, lParam);
  55. }
  56. WebGameWindow::WebGameWindow(void)
  57. {
  58. mTimer = false;
  59. mInitialized = false;
  60. }
  61. WebGameWindow::~WebGameWindow(void)
  62. {
  63. //handling threads in event callbacks (onDestroy for instance) seems to cause loads of problems (deadlocks, etc)
  64. if (mInitialized)
  65. WebCommon::ShutdownTorque3D();
  66. }
  67. // we use a timer to update the Torque 3D game loop (tick) and handle rendering
  68. VOID CALLBACK MyTimerProc(
  69. HWND hwnd, // handle to window for timer messages
  70. UINT message, // WM_TIMER message
  71. UINT idTimer, // timer identifier
  72. DWORD dwTime) // current system time
  73. {
  74. static bool reentrant = false;
  75. if (!reentrant)
  76. {
  77. reentrant = true;
  78. torque_enginetick();
  79. reentrant = false;
  80. }
  81. }
  82. LRESULT
  83. WebGameWindow::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  84. {
  85. bHandled = TRUE;
  86. // check that the domain we're loading the plugin from is allowed
  87. if (!checkDomain())
  88. {
  89. return -1;
  90. }
  91. // load up the Torque 3D shared library and initialize it
  92. if (!WebCommon::InitTorque3D(this->m_hWnd))
  93. {
  94. return -1;
  95. }
  96. mTimer = true;
  97. mInitialized = true;
  98. // fire up timer for ticking Torque 3D update
  99. SetTimer( 1, // timer identifier
  100. 1, // 1 millisecond
  101. (TIMERPROC) MyTimerProc); // timer callback
  102. hHook = ::SetWindowsHookEx(
  103. WH_GETMESSAGE,
  104. GetMessageProc,
  105. WebCommon::gPluginModule,
  106. GetCurrentThreadId());
  107. return 0;
  108. }
  109. //------------------------------------------------------------------------------
  110. /**
  111. */
  112. LRESULT
  113. WebGameWindow::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  114. {
  115. // let the default handler run
  116. bHandled = FALSE;
  117. // kill update timer
  118. if (mTimer)
  119. KillTimer( 1);
  120. mTimer = false;
  121. if (hHook)
  122. ::UnhookWindowsHookEx (hHook);
  123. hHook = NULL;
  124. return 0;
  125. }
  126. //------------------------------------------------------------------------------
  127. /**
  128. */
  129. LRESULT
  130. WebGameWindow::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  131. {
  132. // let the default handler run
  133. bHandled = FALSE;
  134. // resize the Torque 3D child window depending on our browser's parent window
  135. if (mInitialized && torque_resizewindow)
  136. {
  137. int width = (int) LOWORD( lParam );
  138. int height = (int) HIWORD( lParam );
  139. torque_resizewindow(width,height);
  140. }
  141. return 0;
  142. }
  143. //------------------------------------------------------------------------------
  144. /**
  145. */
  146. LRESULT
  147. WebGameWindow::OnMouseActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  148. {
  149. return MA_ACTIVATE;
  150. }