mouse.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. {
  2. $Id$
  3. System independent mouse interface for win32
  4. Copyright (c) 1999 by Florian Klaempfl
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free
  15. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. }
  17. uses
  18. windows,dos,event;
  19. var
  20. ChangeMouseEvents : TCriticalSection;
  21. Const
  22. MouseEventActive : Boolean = false;
  23. procedure MouseEventHandler;
  24. var
  25. ir : INPUT_RECORD;
  26. dwRead : DWord;
  27. i: longint;
  28. e : TMouseEvent;
  29. begin
  30. ReadConsoleInput(TextRec(Input).Handle,ir,1,dwRead);
  31. if (dwRead=1) and (ir.EventType=_MOUSE_EVENT) then
  32. begin
  33. EnterCriticalSection(ChangeMouseEvents);
  34. e.x:=ir.MouseEvent.dwMousePosition.x;
  35. e.y:=ir.MouseEvent.dwMousePosition.y;
  36. e.buttons:=0;
  37. e.action:=0;
  38. if (ir.MouseEvent.dwButtonState and FROM_LEFT_1ST_BUTTON_PRESSED<>0) then
  39. e.buttons:=e.buttons or MouseLeftButton;
  40. if (ir.MouseEvent.dwButtonState and FROM_LEFT_2ND_BUTTON_PRESSED<>0) then
  41. e.buttons:=e.buttons or MouseMiddleButton;
  42. if (ir.MouseEvent.dwButtonState and RIGHTMOST_BUTTON_PRESSED<>0) then
  43. e.buttons:=e.buttons or MouseRightButton;
  44. { can we compress the events? }
  45. if (PendingMouseEvents>0) and
  46. (e.buttons=PendingMouseTail^.buttons) and
  47. (e.action=PendingMouseTail^.action) then
  48. begin
  49. PendingMouseTail^.x:=e.x;
  50. PendingMouseTail^.y:=e.y;
  51. end
  52. else
  53. begin
  54. PutMouseEvent(e);
  55. // this should be done in PutMouseEvent
  56. inc(PendingMouseEvents);
  57. end;
  58. LeaveCriticalSection(ChangeMouseEvents);
  59. end;
  60. end;
  61. procedure InitMouse;
  62. var
  63. mode : dword;
  64. begin
  65. if MouseEventActive then
  66. exit;
  67. // enable mouse events
  68. GetConsoleMode(TextRec(Input).Handle,@mode);
  69. mode:=mode or ENABLE_MOUSE_INPUT;
  70. SetConsoleMode(TextRec(Input).Handle,mode);
  71. PendingMouseHead:=@PendingMouseEvent;
  72. PendingMouseTail:=@PendingMouseEvent;
  73. PendingMouseEvents:=0;
  74. FillChar(LastMouseEvent,sizeof(TMouseEvent),0);
  75. InitializeCriticalSection(ChangeMouseEvents);
  76. SetMouseEventHandler(@MouseEventHandler);
  77. ShowMouse;
  78. MouseEventActive:=true;
  79. end;
  80. procedure DoneMouse;
  81. var
  82. mode : dword;
  83. begin
  84. if not MouseEventActive then
  85. exit;
  86. HideMouse;
  87. // disable mouse events
  88. GetConsoleMode(TextRec(Input).Handle,@mode);
  89. mode:=mode and (not ENABLE_MOUSE_INPUT);
  90. SetConsoleMode(TextRec(Input).Handle,mode);
  91. SetMouseEventHandler(nil);
  92. DeleteCriticalSection(ChangeMouseEvents);
  93. MouseEventActive:=false;
  94. end;
  95. function DetectMouse:byte;
  96. var
  97. num : dword;
  98. begin
  99. GetNumberOfConsoleMouseButtons(@num);
  100. DetectMouse:=num;
  101. end;
  102. procedure ShowMouse;
  103. begin
  104. end;
  105. procedure HideMouse;
  106. begin
  107. end;
  108. function GetMouseX:word;
  109. begin
  110. GetMouseX:=0;
  111. end;
  112. function GetMouseY:word;
  113. begin
  114. GetMouseY:=0;
  115. end;
  116. function GetMouseButtons:word;
  117. begin
  118. GetMouseButtons:=0;
  119. end;
  120. procedure SetMouseXY(x,y:word);
  121. begin
  122. end;
  123. procedure GetMouseEvent(var MouseEvent: TMouseEvent);
  124. var
  125. b : byte;
  126. begin
  127. repeat
  128. EnterCriticalSection(ChangeMouseEvents);
  129. b:=PendingMouseEvents;
  130. LeaveCriticalSection(ChangeMouseEvents);
  131. if b>0 then
  132. break
  133. else
  134. sleep(50);
  135. until false;
  136. EnterCriticalSection(ChangeMouseEvents);
  137. MouseEvent:=PendingMouseHead^;
  138. inc(PendingMouseHead);
  139. if longint(PendingMouseHead)=longint(@PendingMouseEvent)+sizeof(PendingMouseEvent) then
  140. PendingMouseHead:=@PendingMouseEvent;
  141. dec(PendingMouseEvents);
  142. if (LastMouseEvent.x<>MouseEvent.x) or (LastMouseEvent.y<>MouseEvent.y) then
  143. MouseEvent.Action:=MouseActionMove;
  144. if (LastMouseEvent.Buttons<>MouseEvent.Buttons) then
  145. begin
  146. if (LastMouseEvent.Buttons=0) then
  147. MouseEvent.Action:=MouseActionDown
  148. else
  149. MouseEvent.Action:=MouseActionUp;
  150. end;
  151. LastMouseEvent:=MouseEvent;
  152. LeaveCriticalSection(ChangeMouseEvents);
  153. end;
  154. function PollMouseEvent(var MouseEvent: TMouseEvent):boolean;
  155. begin
  156. EnterCriticalSection(ChangeMouseEvents);
  157. if PendingMouseEvents>0 then
  158. begin
  159. MouseEvent:=PendingMouseHead^;
  160. PollMouseEvent:=true;
  161. end
  162. else
  163. PollMouseEvent:=false;
  164. LeaveCriticalSection(ChangeMouseEvents);
  165. end;
  166. {
  167. $Log$
  168. Revision 1.2 2000-07-13 11:32:27 michael
  169. + removed logs
  170. }