mouse.pp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. Gpm_GetSnapshot(e);
  252. if e.x>0 then
  253. MouseEvent.x:=e.x-1
  254. else
  255. MouseEvent.x:=0;
  256. if e.y>0 then
  257. MouseEvent.y:=e.y-1
  258. else
  259. MouseEvent.y:=0;
  260. MouseEvent.buttons:=0;
  261. if e.buttons and Gpm_b_left<>0 then
  262. inc(MouseEvent.buttons,1);
  263. if e.buttons and Gpm_b_right<>0 then
  264. inc(MouseEvent.buttons,2);
  265. if e.buttons and Gpm_b_middle<>0 then
  266. inc(MouseEvent.buttons,4);
  267. case (e.EventType and $f) of
  268. GPM_MOVE,
  269. GPM_DRAG : MouseEvent.Action:=MouseActionMove;
  270. GPM_DOWN : MouseEvent.Action:=MouseActionDown;
  271. GPM_UP : MouseEvent.Action:=MouseActionUp;
  272. else
  273. MouseEvent.Action:=0;
  274. end;
  275. if (gpm_fs<>-2) or (MouseEvent.Action<>0) then
  276. SysPollMouseEvent:=true
  277. else
  278. SysPollMouseEvent:=false;
  279. end
  280. else
  281. SysPollMouseEvent:=false;
  282. {$endif ndef NOGPM}
  283. end;
  284. Const
  285. SysMouseDriver : TMouseDriver = (
  286. UseDefaultQueue : true;
  287. InitDriver : @SysInitMouse;
  288. DoneDriver : @SysDoneMouse;
  289. DetectMouse : @SysDetectMouse;
  290. ShowMouse : @SysShowMouse;
  291. HideMouse : @SysHideMouse;
  292. GetMouseX : @SysGetMouseX;
  293. GetMouseY : @SysGetMouseY;
  294. GetMouseButtons : @SysGetMouseButtons;
  295. SetMouseXY : Nil;
  296. GetMouseEvent : @SysGetMouseEvent;
  297. PollMouseEvent : @SysPollMouseEvent;
  298. PutMouseEvent : Nil;
  299. );
  300. {$else ifndef NOMOUSE}
  301. Const
  302. SysMouseDriver : TMouseDriver = (
  303. UseDefaultQueue : true;
  304. InitDriver : Nil;
  305. DoneDriver : Nil;
  306. DetectMouse : Nil;
  307. ShowMouse : Nil;
  308. HideMouse : Nil;
  309. GetMouseX : Nil;
  310. GetMouseY : Nil;
  311. GetMouseButtons : Nil;
  312. SetMouseXY : Nil;
  313. GetMouseEvent : Nil;
  314. PollMouseEvent : Nil;
  315. PutMouseEvent : Nil;
  316. );
  317. {$endif}
  318. Begin
  319. SetMouseDriver(SysMouseDriver);
  320. end.
  321. {
  322. $Log$
  323. Revision 1.5 2001-09-22 00:01:43 michael
  324. + Merged driver support for mouse from fixbranch
  325. Revision 1.4 2001/09/17 21:36:31 peter
  326. * merged fixes
  327. Revision 1.2.2.6 2001/09/21 23:53:48 michael
  328. + Added mouse driver support.
  329. Revision 1.2.2.5 2001/09/06 09:05:08 pierre
  330. * fix NOGPM code
  331. Revision 1.2.2.4 2001/09/06 08:33:34 pierre
  332. * fix NOGPM cond to not include gpm unit
  333. Revision 1.2.2.3 2001/08/05 12:25:55 peter
  334. * fix possible range check errors
  335. Revision 1.2.2.2 2001/01/30 22:23:44 peter
  336. * unix back to linux
  337. Revision 1.3 2001/08/05 12:24:20 peter
  338. * m68k merges
  339. Revision 1.2 2001/01/21 20:21:40 marco
  340. * Rename fest II. Rtl OK
  341. Revision 1.1 2001/01/13 11:03:58 peter
  342. * API 2 RTL commit
  343. }