mouse.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {$T+}
  11. Var
  12. CurrentMouseDriver : TMouseDriver;
  13. MouseInitialized : Boolean;
  14. // Mouse queue event mechanism
  15. PendingMouseEvent : array[0..MouseEventBufSize-1] of TMouseEvent;
  16. PendingMouseHead,
  17. PendingMouseTail : PMouseEvent;
  18. PendingMouseEvents : byte;
  19. LastMouseEvent : TMouseEvent;
  20. Procedure ClearMouseEventQueue;
  21. begin
  22. PendingMouseHead:=@PendingMouseEvent[0];
  23. PendingMouseTail:=@PendingMouseEvent[0];
  24. PendingMouseEvents:=0;
  25. FillChar(LastMouseEvent,sizeof(TMouseEvent),0);
  26. end;
  27. procedure InitMouse;
  28. begin
  29. If Not MouseInitialized then
  30. begin
  31. If Assigned(CurrentMouseDriver.InitDriver) Then
  32. CurrentMouseDriver.InitDriver();
  33. ClearMouseEventQueue;
  34. MouseInitialized:=True;
  35. end;
  36. end;
  37. procedure DoneMouse;
  38. begin
  39. If MouseInitialized then
  40. begin
  41. If Assigned(CurrentMouseDriver.DoneDriver) Then
  42. CurrentMouseDriver.DoneDriver();
  43. ClearMouseEventQueue;
  44. MouseInitialized:=False;
  45. end;
  46. end;
  47. function DetectMouse:byte;
  48. begin
  49. If Assigned(CurrentMouseDriver.DetectMouse) Then
  50. DetectMouse:=CurrentMouseDriver.DetectMouse()
  51. else
  52. DetectMouse:=0;
  53. end;
  54. procedure ShowMouse;
  55. begin
  56. If Assigned(CurrentMouseDriver.ShowMouse) Then
  57. CurrentMouseDriver.ShowMouse();
  58. end;
  59. procedure HideMouse;
  60. begin
  61. If Assigned(CurrentMouseDriver.HideMouse) Then
  62. CurrentMouseDriver.HideMouse();
  63. end;
  64. function GetMouseX:word;
  65. begin
  66. If Assigned(CurrentMouseDriver.GetMouseX) Then
  67. GetMouseX:=CurrentMouseDriver.GetMouseX()
  68. else
  69. GetMouseX:=0;
  70. end;
  71. function GetMouseY:word;
  72. begin
  73. If Assigned(CurrentMouseDriver.GetMouseY) Then
  74. GetMouseY:=CurrentMouseDriver.GetMouseY()
  75. else
  76. GetMouseY:=0;
  77. end;
  78. function GetMouseButtons:word;
  79. begin
  80. If Assigned(CurrentMouseDriver.GetMouseButtons) Then
  81. GetMouseButtons:=CurrentMouseDriver.GetMouseButtons()
  82. else
  83. GetMouseButtons:=0;
  84. end;
  85. procedure SetMouseXY(x,y:word);
  86. begin
  87. If Assigned(CurrentMouseDriver.SetMouseXY) Then
  88. CurrentMouseDriver.SetMouseXY(X,Y)
  89. end;
  90. Procedure GetPendingEvent(Var MouseEvent:TMouseEvent);
  91. begin
  92. MouseEvent:=PendingMouseHead^;
  93. inc(PendingMouseHead);
  94. if PendingMouseHead=@PendingMouseEvent[0]+MouseEventBufSize then
  95. PendingMouseHead:=@PendingMouseEvent[0];
  96. dec(PendingMouseEvents);
  97. if (LastMouseEvent.x<>MouseEvent.x) or
  98. (LastMouseEvent.y<>MouseEvent.y) then
  99. MouseEvent.Action:=MouseActionMove;
  100. if (LastMouseEvent.Buttons<>MouseEvent.Buttons) then
  101. begin
  102. if (LastMouseEvent.Buttons=0) then
  103. MouseEvent.Action:=MouseActionDown
  104. else
  105. MouseEvent.Action:=MouseActionUp;
  106. end;
  107. LastMouseEvent:=MouseEvent;
  108. end;
  109. procedure GetMouseEvent(var MouseEvent:TMouseEvent);
  110. begin
  111. if CurrentMouseDriver.UseDefaultQueue then
  112. begin
  113. if (PendingMouseEvents>0) then
  114. GetPendingEvent(MouseEvent)
  115. else
  116. CurrentMouseDriver.GetMouseEvent(MouseEvent);
  117. { FillChar(MouseEvent,sizeof(MouseEvent),0);}
  118. end
  119. else
  120. If Assigned(CurrentMouseDriver.GetMouseEvent) Then
  121. begin
  122. LastMouseEvent:=MouseEvent;
  123. CurrentMouseDriver.GetMouseEvent(MouseEvent);
  124. end
  125. else
  126. FillChar(MouseEvent,sizeof(TMouseEvent),0);
  127. end;
  128. procedure PutMouseEvent(const MouseEvent: TMouseEvent);
  129. begin
  130. if CurrentMouseDriver.UseDefaultQueue then
  131. begin
  132. PendingMouseTail^:=MouseEvent;
  133. inc(PendingMouseTail);
  134. if PendingMouseTail=@PendingMouseEvent[0]+MouseEventBufSize then
  135. PendingMouseTail:=@PendingMouseEvent[0];
  136. inc(PendingMouseEvents);
  137. end
  138. else
  139. If Assigned(CurrentMouseDriver.PutMouseEvent) then
  140. CurrentMouseDriver.PutMouseEvent(MouseEvent);
  141. end;
  142. function PollMouseEvent(var MouseEvent: TMouseEvent):boolean;
  143. begin
  144. if (CurrentMouseDriver.UseDefaultQueue) and
  145. (PendingMouseEvents>0) then
  146. begin
  147. MouseEvent:=PendingMouseHead^;
  148. PollMouseEvent:=true;
  149. end
  150. else
  151. If Assigned(CurrentMouseDriver.PollMouseEvent) Then
  152. begin
  153. PollMouseEvent:=CurrentMouseDriver.PollMouseEvent(MouseEvent);
  154. // Put it in queue, so next poll/get will be faster.
  155. // Only if an event was found PM
  156. // If PollMouseEvent then
  157. // PutMouseEvent(MouseEvent);
  158. // This is all wrong, because the Event might already
  159. // have been pushed in the Event Array.
  160. end
  161. else
  162. PollMouseEvent:=false;
  163. end;
  164. Procedure SetMouseDriver(Const Driver : TMouseDriver);
  165. begin
  166. If Not MouseInitialized then
  167. CurrentMouseDriver:=Driver;
  168. end;
  169. Procedure GetMouseDriver(Var Driver : TMouseDriver);
  170. begin
  171. Driver:=CurrentMouseDriver;
  172. end;