mouse.pp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. Mouse unit for linux
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program 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.
  12. **********************************************************************}
  13. unit Mouse;
  14. interface
  15. {$ifdef NOMOUSE}
  16. {$DEFINE NOGPM}
  17. {$ENDIF}
  18. {$i mouseh.inc}
  19. implementation
  20. uses
  21. Unix,Video
  22. {$ifndef NOGPM}
  23. ,gpm
  24. {$endif ndef NOGPM}
  25. ;
  26. {$i mouse.inc}
  27. {$ifndef NOMOUSE}
  28. const
  29. mousecur : boolean = false;
  30. mousecurofs : longint = -1;
  31. var
  32. mousecurcell : TVideoCell;
  33. const
  34. gpm_fs : longint = -1;
  35. procedure PlaceMouseCur(ofs:longint);
  36. var
  37. upd : boolean;
  38. begin
  39. if VideoBuf=nil then
  40. exit;
  41. upd:=false;
  42. if (MouseCurOfs<>-1) and (VideoBuf^[MouseCurOfs]=MouseCurCell) then
  43. begin
  44. VideoBuf^[MouseCurOfs]:=MouseCurCell xor $7f00;
  45. upd:=true;
  46. end;
  47. MouseCurOfs:=ofs;
  48. if (MouseCurOfs<>-1) then
  49. begin
  50. MouseCurCell:=VideoBuf^[MouseCurOfs] xor $7f00;
  51. VideoBuf^[MouseCurOfs]:=MouseCurCell;
  52. upd:=true;
  53. end;
  54. if upd then
  55. Updatescreen(false);
  56. end;
  57. procedure SysInitMouse;
  58. {$ifndef NOGPM}
  59. var
  60. connect : TGPMConnect;
  61. {$endif ndef NOGPM}
  62. begin
  63. {$ifndef NOGPM}
  64. if gpm_fs=-1 then
  65. begin
  66. { open gpm }
  67. connect.EventMask:=GPM_MOVE or GPM_DRAG or GPM_DOWN or GPM_UP;
  68. connect.DefaultMask:=0;
  69. connect.MinMod:=0;
  70. connect.MaxMod:=0;
  71. gpm_fs:=Gpm_Open(connect,0);
  72. if (gpm_fs=-2) and (getenv('TERM')<>'xterm') then
  73. begin
  74. gpm_fs:=-1;
  75. Gpm_Close;
  76. end;
  77. end;
  78. { show mousepointer }
  79. if gpm_fs<>-1 then
  80. ShowMouse;
  81. {$else ifdef NOGPM}
  82. if (getenv('TERM')='xterm') then
  83. begin
  84. gpm_fs:=-2;
  85. Write(#27'[?1001s'); { save old hilit tracking }
  86. Write(#27'[?1000h'); { enable mouse tracking }
  87. end;
  88. {$endif NOGPM}
  89. end;
  90. procedure SysDoneMouse;
  91. begin
  92. If gpm_fs<>-1 then
  93. begin
  94. HideMouse;
  95. {$ifndef NOGPM}
  96. Gpm_Close;
  97. {$else ifdef NOGPM}
  98. Write(#27'[?1000l'); { disable mouse tracking }
  99. Write(#27'[?1001r'); { Restore old hilit tracking }
  100. {$endif ifdef NOGPM}
  101. gpm_fs:=-1;
  102. end;
  103. end;
  104. function SysDetectMouse:byte;
  105. {$ifndef NOGPM}
  106. var
  107. x : longint;
  108. e : TGPMEvent;
  109. connect : TGPMConnect;
  110. {$endif ndef NOGPM}
  111. begin
  112. {$ifndef NOGPM}
  113. if gpm_fs=-1 then
  114. begin
  115. connect.EventMask:=GPM_MOVE or GPM_DRAG or GPM_DOWN or GPM_UP;
  116. connect.DefaultMask:=0;
  117. connect.MinMod:=0;
  118. connect.MaxMod:=0;
  119. gpm_fs:=Gpm_Open(connect,0);
  120. if (gpm_fs=-2) and (getenv('TERM')<>'xterm') then
  121. begin
  122. Gpm_Close;
  123. gpm_fs:=-1;
  124. end;
  125. end;
  126. { always a mouse deamon present }
  127. if gpm_fs<>-1 then
  128. begin
  129. x:=Gpm_GetSnapshot(e);
  130. if x<>-1 then
  131. SysDetectMouse:=x
  132. else
  133. SysDetectMouse:=2;
  134. end
  135. else
  136. SysDetectMouse:=0;
  137. {$else ifdef NOGPM}
  138. if (getenv('TERM')='xterm') then
  139. SysDetectMouse:=2;
  140. {$endif NOGPM}
  141. end;
  142. procedure SysShowMouse;
  143. begin
  144. PlaceMouseCur(MouseCurOfs);
  145. mousecur:=true;
  146. end;
  147. procedure SysHideMouse;
  148. begin
  149. PlaceMouseCur(-1);
  150. mousecur:=false;
  151. end;
  152. function SysGetMouseX:word;
  153. {$ifndef NOGPM}
  154. var
  155. e : TGPMEvent;
  156. {$endif ndef NOGPM}
  157. begin
  158. if gpm_fs<0 then
  159. exit(0);
  160. {$ifndef NOGPM}
  161. Gpm_GetSnapshot(e);
  162. SysGetMouseX:=e.x-1;
  163. {$endif ndef NOGPM}
  164. end;
  165. function SysGetMouseY:word;
  166. {$ifndef NOGPM}
  167. var
  168. e : TGPMEvent;
  169. {$endif ndef NOGPM}
  170. begin
  171. if gpm_fs<0 then
  172. exit(0);
  173. {$ifndef NOGPM}
  174. Gpm_GetSnapshot(e);
  175. if e.y>0 then
  176. SysGetMouseY:=e.y-1
  177. else
  178. SysGetMouseY:=0;
  179. {$endif ndef NOGPM}
  180. end;
  181. function SysGetMouseButtons:word;
  182. {$ifndef NOGPM}
  183. var
  184. e : TGPMEvent;
  185. {$endif ndef NOGPM}
  186. begin
  187. if gpm_fs<0 then
  188. exit(0);
  189. {$ifndef NOGPM}
  190. Gpm_GetSnapshot(e);
  191. SysGetMouseButtons:=e.buttons;
  192. {$endif ndef NOGPM}
  193. end;
  194. procedure SysGetMouseEvent(var MouseEvent: TMouseEvent);
  195. {$ifndef NOGPM}
  196. var
  197. e : TGPMEvent;
  198. {$endif ndef NOGPM}
  199. begin
  200. fillchar(MouseEvent,SizeOf(TMouseEvent),#0);
  201. if gpm_fs<0 then
  202. exit;
  203. {$ifndef NOGPM}
  204. Gpm_GetEvent(e);
  205. if e.x>0 then
  206. MouseEvent.x:=e.x-1
  207. else
  208. MouseEvent.x:=0;
  209. if e.y>0 then
  210. MouseEvent.y:=e.y-1
  211. else
  212. MouseEvent.y:=0;
  213. MouseEvent.buttons:=0;
  214. if e.buttons and Gpm_b_left<>0 then
  215. inc(MouseEvent.buttons,1);
  216. if e.buttons and Gpm_b_right<>0 then
  217. inc(MouseEvent.buttons,2);
  218. if e.buttons and Gpm_b_middle<>0 then
  219. inc(MouseEvent.buttons,4);
  220. case (e.EventType and $f) of
  221. GPM_MOVE,
  222. GPM_DRAG : MouseEvent.Action:=MouseActionMove;
  223. GPM_DOWN : MouseEvent.Action:=MouseActionDown;
  224. GPM_UP : MouseEvent.Action:=MouseActionUp;
  225. else
  226. MouseEvent.Action:=0;
  227. end;
  228. { update mouse cursor }
  229. if mousecur then
  230. PlaceMouseCur(MouseEvent.y*ScreenWidth+MouseEvent.x);
  231. {$endif ndef NOGPM}
  232. end;
  233. function SysPollMouseEvent(var MouseEvent: TMouseEvent):boolean;
  234. {$ifndef NOGPM}
  235. var
  236. e : TGPMEvent;
  237. fds : FDSet;
  238. {$endif ndef NOGPM}
  239. begin
  240. fillchar(MouseEvent,SizeOf(TMouseEvent),#0);
  241. {$ifndef NOGPM}
  242. if gpm_fs<0 then
  243. exit(false);
  244. if gpm_fs>0 then
  245. begin
  246. FD_Zero(fds);
  247. FD_Set(gpm_fd,fds);
  248. end;
  249. if (gpm_fs=-2) or (Select(gpm_fs+1,@fds,nil,nil,1)>0) then
  250. begin
  251. FillChar(e,SizeOf(e),#0);
  252. Gpm_GetSnapshot(e);
  253. if e.x>0 then
  254. MouseEvent.x:=e.x-1
  255. else
  256. MouseEvent.x:=0;
  257. if e.y>0 then
  258. MouseEvent.y:=e.y-1
  259. else
  260. MouseEvent.y:=0;
  261. MouseEvent.buttons:=0;
  262. if e.buttons and Gpm_b_left<>0 then
  263. inc(MouseEvent.buttons,1);
  264. if e.buttons and Gpm_b_right<>0 then
  265. inc(MouseEvent.buttons,2);
  266. if e.buttons and Gpm_b_middle<>0 then
  267. inc(MouseEvent.buttons,4);
  268. case (e.EventType and $f) of
  269. GPM_MOVE,
  270. GPM_DRAG : MouseEvent.Action:=MouseActionMove;
  271. GPM_DOWN : MouseEvent.Action:=MouseActionDown;
  272. GPM_UP : MouseEvent.Action:=MouseActionUp;
  273. else
  274. MouseEvent.Action:=0;
  275. end;
  276. if {(gpm_fs<>-2) or} (MouseEvent.Action<>0) then
  277. SysPollMouseEvent:=true
  278. else
  279. SysPollMouseEvent:=false;
  280. end
  281. else
  282. SysPollMouseEvent:=false;
  283. {$endif ndef NOGPM}
  284. end;
  285. Const
  286. SysMouseDriver : TMouseDriver = (
  287. UseDefaultQueue : true;
  288. InitDriver : @SysInitMouse;
  289. DoneDriver : @SysDoneMouse;
  290. DetectMouse : @SysDetectMouse;
  291. ShowMouse : @SysShowMouse;
  292. HideMouse : @SysHideMouse;
  293. GetMouseX : @SysGetMouseX;
  294. GetMouseY : @SysGetMouseY;
  295. GetMouseButtons : @SysGetMouseButtons;
  296. SetMouseXY : Nil;
  297. GetMouseEvent : @SysGetMouseEvent;
  298. PollMouseEvent : @SysPollMouseEvent;
  299. PutMouseEvent : Nil;
  300. );
  301. {$else ifndef NOMOUSE}
  302. Const
  303. SysMouseDriver : TMouseDriver = (
  304. UseDefaultQueue : true;
  305. InitDriver : Nil;
  306. DoneDriver : Nil;
  307. DetectMouse : Nil;
  308. ShowMouse : Nil;
  309. HideMouse : Nil;
  310. GetMouseX : Nil;
  311. GetMouseY : Nil;
  312. GetMouseButtons : Nil;
  313. SetMouseXY : Nil;
  314. GetMouseEvent : Nil;
  315. PollMouseEvent : Nil;
  316. PutMouseEvent : Nil;
  317. );
  318. {$endif}
  319. Begin
  320. SetMouseDriver(SysMouseDriver);
  321. end.
  322. {
  323. $Log$
  324. Revision 1.6 2001-12-02 17:21:25 peter
  325. * merged fixes from 1.0
  326. Revision 1.5 2001/09/22 00:01:43 michael
  327. + Merged driver support for mouse from fixbranch
  328. Revision 1.4 2001/09/17 21:36:31 peter
  329. * merged fixes
  330. Revision 1.2.2.6 2001/09/21 23:53:48 michael
  331. + Added mouse driver support.
  332. Revision 1.2.2.5 2001/09/06 09:05:08 pierre
  333. * fix NOGPM code
  334. Revision 1.2.2.4 2001/09/06 08:33:34 pierre
  335. * fix NOGPM cond to not include gpm unit
  336. Revision 1.2.2.3 2001/08/05 12:25:55 peter
  337. * fix possible range check errors
  338. Revision 1.2.2.2 2001/01/30 22:23:44 peter
  339. * unix back to linux
  340. Revision 1.3 2001/08/05 12:24:20 peter
  341. * m68k merges
  342. Revision 1.2 2001/01/21 20:21:40 marco
  343. * Rename fest II. Rtl OK
  344. Revision 1.1 2001/01/13 11:03:58 peter
  345. * API 2 RTL commit
  346. }