win32directxhook.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. {
  2. Free Pascal port of the OpenPTC C++ library.
  3. Copyright (C) 2001-2003, 2006, 2009-2012 Nikolay Nikolov ([email protected])
  4. Original C++ version by Glenn Fiedler ([email protected])
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version
  9. with the following modification:
  10. As a special exception, the copyright holders of this library give you
  11. permission to link this library with independent modules to produce an
  12. executable, regardless of the license terms of these independent modules,and
  13. to copy and distribute the resulting executable under terms of your choice,
  14. provided that you also meet, for each linked independent module, the terms
  15. and conditions of the license of that module. An independent module is a
  16. module which is not derived from or based on this library. If you modify
  17. this library, you may extend this exception to your version of the library,
  18. but you are not obligated to do so. If you do not wish to do so, delete this
  19. exception statement from your version.
  20. This library is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. Lesser General Public License for more details.
  24. You should have received a copy of the GNU Lesser General Public
  25. License along with this library; if not, write to the Free Software
  26. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. }
  28. constructor TDirectXHook.Create(AConsole: TDirectXConsole; AWindow: HWND; AThread: DWord; ACursor, AManaged, AFullscreen: Boolean);
  29. begin
  30. FConsole := AConsole;
  31. FCursor := ACursor;
  32. FManaged := AManaged;
  33. FFullscreen := AFullscreen;
  34. LOG('creating window hook');
  35. inherited Create(AWindow, AThread);
  36. end;
  37. destructor TDirectXHook.Destroy;
  38. begin
  39. LOG('destroying window hook');
  40. inherited Destroy;
  41. end;
  42. procedure TDirectXHook.Cursor(AFlag: Boolean);
  43. begin
  44. FCursor := AFlag;
  45. end;
  46. function TDirectXHook.WndProc(hWnd: HWND; message: DWord; wParam: WPARAM; lParam: LPARAM): LRESULT;
  47. var
  48. active: Boolean;
  49. thread: DWord;
  50. placement: WINDOWPLACEMENT;
  51. console: TDirectXConsole;
  52. begin
  53. case message of
  54. WM_PAINT: begin
  55. LOG('TDirectXHook WM_PAINT');
  56. { paint console }
  57. FConsole.Paint;
  58. end;
  59. WM_ACTIVATEAPP: begin
  60. LOG('TDirectXHook WM_ACTIVATEAPP');
  61. { get window message data }
  62. active := wParam <> 0;
  63. thread := DWord(lParam);
  64. { check active flag }
  65. if active = False then
  66. begin
  67. if FConsole.FGrabMouse and (not FFullscreen) then
  68. begin
  69. FConsole.FWindow.ConfineCursor(False);
  70. end;
  71. { deactivate }
  72. Deactivate;
  73. { check cursor and fullscreen }
  74. if (not FCursor) and FFullscreen then
  75. { show cursor }
  76. FConsole.FWin32Cursor.Show;
  77. end
  78. else
  79. begin
  80. { get window placement for active app }
  81. if not GetWindowPlacement(hWnd, placement) then
  82. { on failure set to normal show }
  83. placement.showCmd := SW_SHOWNORMAL;
  84. { check cursor and fullscreen }
  85. if (not FCursor) and FFullscreen then
  86. begin
  87. { check show command is not minimize }
  88. if placement.showCmd <> SW_SHOWMINIMIZED then
  89. begin
  90. {hide cursor}
  91. FConsole.FWin32Cursor.Hide;
  92. end;
  93. end;
  94. if FConsole.FGrabMouse and (not FFullscreen) then
  95. begin
  96. if placement.showCmd <> SW_SHOWMINIMIZED then
  97. begin
  98. FConsole.FWindow.ConfineCursor(True);
  99. end;
  100. end;
  101. { activate }
  102. Activate;
  103. end;
  104. { pass to the next handler (or DefWindowProc) }
  105. Result := 0;
  106. exit;
  107. end;
  108. WM_SETCURSOR: begin
  109. { check cursor }
  110. if not FCursor then
  111. begin
  112. if FFullscreen or (LOWORD(lParam) = HTCLIENT) then
  113. begin
  114. { hide cursor }
  115. SetCursor(0);
  116. { handled }
  117. Result := 1;
  118. end;
  119. end;
  120. end;
  121. WM_PALETTECHANGED:
  122. begin
  123. LOG('TDirectXHook WM_PALETTECHANGED');
  124. if Windows.HWND(wParam) <> hWnd then
  125. begin
  126. LOG('not our window');
  127. if FConsole.FPrimary.Active then
  128. begin
  129. FConsole.FPrimary.ResetPalette;
  130. end;
  131. end;
  132. end;
  133. WM_QUERYNEWPALETTE:
  134. begin
  135. LOG('TDirectXHook WM_QUERYNEWPALETTE');
  136. end;
  137. WM_CLOSE: begin
  138. LOG('TDirectXHook WM_CLOSE');
  139. if FManaged then
  140. begin
  141. console := FConsole;
  142. { close console }
  143. console.Close;
  144. { note: at this point the hook object has been destroyed by the console! }
  145. { internal console shutdown }
  146. console.internal_shutdown;
  147. { halt }
  148. Halt(0);
  149. end;
  150. { handled }
  151. Result := 1;
  152. exit;
  153. end;
  154. end;
  155. { unhandled }
  156. Result := 0;
  157. end;
  158. procedure TDirectXHook.Activate;
  159. var
  160. display: TDirectXDisplay;
  161. primary: TDirectXPrimary;
  162. begin
  163. { check if open }
  164. if FConsole.FOpen then
  165. begin
  166. LOG('activate');
  167. { get console object references }
  168. display := FConsole.FDisplay;
  169. primary := FConsole.FPrimary;
  170. { check if primary is not active }
  171. if not primary.Active then
  172. begin
  173. { save display }
  174. display.Save;
  175. { activate primary }
  176. primary.Activate;
  177. end;
  178. end;
  179. end;
  180. procedure TDirectXHook.Deactivate;
  181. var
  182. display: TDirectXDisplay;
  183. primary: TDirectXPrimary;
  184. begin
  185. { check if open }
  186. if FConsole.FOpen then
  187. begin
  188. LOG('deactivate');
  189. { get console object references }
  190. display := FConsole.FDisplay;
  191. primary := FConsole.FPrimary;
  192. { check if primary is not active }
  193. if primary.Active then
  194. begin
  195. { save primary }
  196. primary.Save;
  197. { deactivate primary }
  198. primary.Deactivate;
  199. { restore display }
  200. display.Restore;
  201. end;
  202. end;
  203. end;