gpm.pp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Peter Vreman
  4. GPM (>v1.17) mouse Interface for linux
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY;without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit gpm;
  12. {Note: Libgpm is *the* interface for Linux text-mode programs.
  13. Unfortunately it isn't suitable for anything else besides a blocky
  14. cursor on a text mode interface. The GPM protocol suffers from serious
  15. defficiencies and ideally, gpm is abolished as quickly as possible.
  16. With lack of a good alternative, GPM deserves good support. But
  17. please keep this in mind while coding.}
  18. {*****************************************************************************}
  19. interface
  20. {*****************************************************************************}
  21. uses
  22. baseUnix;
  23. {$ifdef use_external}
  24. {$linklib gpm}
  25. {$linklib c}
  26. {$endif}
  27. {$inline on}
  28. {$goto on}
  29. const
  30. _PATH_VARRUN = '/var/run/';
  31. _PATH_DEV = '/dev/';
  32. GPM_NODE_DIR = _PATH_VARRUN;
  33. GPM_NODE_DIR_MODE = 0775;
  34. GPM_NODE_PID = '/var/run/gpm.pid';
  35. GPM_NODE_DEV = '/dev/gpmctl';
  36. GPM_NODE_CTL = GPM_NODE_DEV;
  37. GPM_NODE_FIFO = '/dev/gpmdata';
  38. GPM_B_LEFT = 4;
  39. GPM_B_MIDDLE = 2;
  40. GPM_B_RIGHT = 1;
  41. type
  42. TGpmEtype = longint;
  43. TGpmMargin = longint;
  44. const
  45. GPM_MOVE = 1;
  46. GPM_DRAG = 2;
  47. GPM_DOWN = 4;
  48. GPM_UP = 8;
  49. GPM_SINGLE = 16;
  50. GPM_DOUBLE = 32;
  51. GPM_TRIPLE = 64;
  52. GPM_MFLAG = 128;
  53. GPM_HARD = 256;
  54. GPM_ENTER = 512;
  55. GPM_LEAVE = 1024;
  56. GPM_TOP = 1;
  57. GPM_BOT = 2;
  58. GPM_LFT = 4;
  59. GPM_RGT = 8;
  60. type
  61. {$PACKRECORDS c}
  62. Pgpm_event=^Tgpm_event;
  63. Tgpm_event=packed record
  64. buttons : byte;
  65. modifiers : byte;
  66. vc : word;
  67. dx : word;
  68. dy : word;
  69. x,y : word;
  70. EventType : TGpmEType;
  71. clicks : longint;
  72. margin : TGpmMargin;
  73. wdx,wdy : word;
  74. end;
  75. Pgpmevent=Pgpm_event;
  76. Tgpmevent=Tgpm_event;
  77. TGpmHandler=function(var event:TGpmEvent;clientdata:pointer):longint;cdecl;
  78. const
  79. GPM_MAGIC = $47706D4C;
  80. type
  81. Pgpm_connect = ^TGpm_connect;
  82. Tgpm_connect = packed record
  83. eventMask : word;
  84. defaultMask : word;
  85. minMod : word;
  86. maxMod : word;
  87. pid : longint;
  88. vc : longint;
  89. end;
  90. Pgpmconnect=Pgpm_connect;
  91. Tgpmconnect=Tgpm_connect;
  92. Pgpm_roi=^Tgpm_roi;
  93. Tgpm_roi=packed record
  94. xmin,xmax:integer;
  95. ymin,ymax:integer;
  96. minmod,maxmod:word;
  97. eventmask:word;
  98. owned:word;
  99. handler:Tgpmhandler;
  100. clientdata:pointer;
  101. prev,next:Pgpm_roi;
  102. end;
  103. Pgpmroi=Pgpm_roi;
  104. Tgpmroi=Tgpm_roi;
  105. {$ifdef external}
  106. var
  107. gpm_flag : longint;cvar;external;
  108. gpm_fd : longint;cvar;external;
  109. gpm_hflag : longint;cvar;external;
  110. gpm_morekeys : Longbool;cvar;external;
  111. gpm_zerobased : Longbool;cvar;external;
  112. gpm_visiblepointer : Longbool;cvar;external;
  113. gpm_mx : longint;cvar;external;
  114. gpm_my : longint;cvar;external;
  115. gpm_timeout : TTimeVal;cvar;external;
  116. _gpm_buf : array[0..0] of char;cvar;external;
  117. _gpm_arg : ^word;cvar;external;
  118. gpm_handler : TGpmHandler;cvar;external;
  119. gpm_data : pointer;cvar;external;
  120. gpm_roi_handler : TGpmHandler;cvar;external;
  121. gpm_roi_data : pointer;cvar;external;
  122. gpm_roi : PGpmRoi;cvar;external;
  123. gpm_current_roi : PGpmRoi;cvar;external;
  124. gpm_consolefd : longint;cvar;external;
  125. Gpm_HandleRoi : TGpmHandler;cvar;external;
  126. {$else}
  127. var gpm_roi:Pgpm_roi;
  128. gpm_handler,gpm_roi_handler:Tgpmhandler;
  129. gpm_current_roi:Pgpm_roi;
  130. gpm_roi_data:pointer;
  131. {$endif}
  132. function Gpm_StrictSingle(EventType : longint) : boolean;
  133. function Gpm_AnySingle(EventType : longint) : boolean;
  134. function Gpm_StrictDouble(EventType : longint) : boolean;
  135. function Gpm_AnyDouble(EventType : longint) : boolean;
  136. function Gpm_StrictTriple(EventType : longint) : boolean;
  137. function Gpm_AnyTriple(EventType : longint) : boolean;
  138. {$ifdef use_external}
  139. function Gpm_Open(var _para1:TGpmConnect; _para2:longint):longint;cdecl;external name 'Gpm_Open';
  140. function Gpm_Close:longint;cdecl;external name 'Gpm_Close';
  141. function Gpm_GetEvent(var _para1:TGpmEvent):longint;cdecl;external name 'Gpm_GetEvent';
  142. {function Gpm_Getc(_para1:pFILE):longint;cdecl;external;
  143. function Gpm_Getchar : longint;}
  144. function Gpm_Repeat(millisec:longint):longint;cdecl;external name 'Gpm_Repeat';
  145. function Gpm_FitValuesM(var x,y:longint; margin:longint):longint;cdecl;external name 'Gpm_FitValuesM';
  146. function Gpm_FitValues(var x,y:longint):longint;cdecl;external name 'Gpm_FitValues';
  147. {function GPM_DRAWPOINTER(ePtr : longint) : longint;}
  148. function Gpm_PushRoi(x1:longint; y1:longint; X2:longint; Y2:longint; mask:longint; fun:TGpmHandler; xtradata:pointer):PGpmRoi;cdecl;external name 'Gpm_PushRoi';
  149. function Gpm_PopRoi(which:PGpmRoi):PGpmRoi;cdecl;external name 'Gpm_PopRoi';
  150. function Gpm_RaiseRoi(which:PGpmRoi; before:PGpmRoi):PGpmRoi;cdecl;external name 'Gpm_RaiseRoi';
  151. function Gpm_LowerRoi(which:PGpmRoi; after:PGpmRoi):PGpmRoi;cdecl;external name 'Gpm_LowerRoi';
  152. {function Gpm_Wgetch:longint;cdecl;external;
  153. function Gpm_Getch:longint;}
  154. function Gpm_GetLibVersion(var where:longint):pchar;cdecl;external name 'Gpm_GetLibVersion';
  155. function Gpm_GetServerVersion(var where:longint):pchar;cdecl;external name 'Gpm_GetServerVersion';
  156. function gpm_getsnapshot(eptr:Pgpmevent):longint;cdecl;external name 'Gpm_GetSnapshot';
  157. function Gpm_GetSnapshot(var ePtr:TGpmEvent):longint;cdecl;external name 'Gpm_GetSnapshot';
  158. {$else}
  159. function gpm_open(var conn:Tgpm_connect;flag:longint):longint;
  160. function gpm_close:longint;
  161. function gpm_getevent(var event:Tgpm_event):longint;
  162. {function Gpm_Getc(_para1:pFILE):longint;cdecl;external;
  163. function Gpm_Getchar : longint;}
  164. function gpm_repeat(millisec:longint):longint;
  165. function gpm_fitvaluesM(var x,y:longint; margin:longint):longint;
  166. function gpm_fitvalues(var x,y:longint):longint;inline;
  167. function gpm_pushroi(x1:longint;y1:longint;x2:longint;y2:longint;
  168. mask:longint;fun:Tgpmhandler;xtradata:pointer):Pgpm_roi;
  169. function gpm_poproi(which:Pgpm_roi):Pgpm_roi;
  170. function gpm_raiseroi(which:Pgpm_roi;before:Pgpm_roi):Pgpm_roi;
  171. function gpm_lowerroi(which:Pgpm_roi;after:Pgpm_roi):Pgpm_roi;
  172. {Should be pointer because proc accepts nil.}
  173. function gpm_getsnapshot(eptr:Pgpmevent):longint;
  174. {Overload for compatibility.}
  175. function gpm_getsnapshot(var eptr:Tgpmevent):longint;inline;
  176. {$endif}
  177. {*****************************************************************************}
  178. implementation
  179. {*****************************************************************************}
  180. {$ifndef use_external}
  181. uses termio,sockets,strings,unix;
  182. type Pgpm_stst=^Tgpm_stst;
  183. Tgpm_stst=record
  184. info:Tgpmconnect;
  185. next:Pgpm_stst;
  186. end;
  187. Pmicetab=^Tmicetab;
  188. Tmicetab=record
  189. next:Pmicetab;
  190. device,protocol,options:Pchar;
  191. end;
  192. string63=string[63];
  193. Toptions=record
  194. autodetect:longint;
  195. mice_count:longint;
  196. repeater:longint;
  197. repeater_type:Pchar;
  198. run_status:longint;
  199. micelist:Pmicetab;
  200. progname,
  201. consolename:string63;
  202. end;
  203. var options:Toptions;
  204. gpm_stack:Pgpm_stst;
  205. gpm_mx,gpm_my:longint;
  206. gpm_saved_winch_hook,gpm_saved_suspend_hook:sigactionrec;
  207. const gpm_flag:boolean=false; {almost unuseful now -- where was it used for ? can
  208. we remove it now ? FIXME}
  209. gpm_tried:boolean=false;
  210. gpm_hflag:boolean=false;
  211. gpm_fd:longint=-1;
  212. gpm_consolefd:longint=-1;
  213. gpm_zerobased:longint=0;
  214. const GPM_DEVFS_CONSOLE='/dev/vc/0';
  215. GPM_OLD_CONSOLE='/dev/tty0';
  216. GPM_REQ_SNAPSHOT=0;
  217. GPM_REQ_BUTTONS=1;
  218. GPM_REQ_CONFIG=2;
  219. GPM_REQ_NOPASTE=3;
  220. {$endif}
  221. function Gpm_StrictSingle(EventType : longint) : boolean;
  222. begin
  223. Gpm_StrictSingle:=(EventType and GPM_SINGLE<>0) and not(EventType and GPM_MFLAG<>0);
  224. end;
  225. function Gpm_AnySingle(EventType : longint) : boolean;
  226. begin
  227. Gpm_AnySingle:=(EventType and GPM_SINGLE<>0);
  228. end;
  229. function Gpm_StrictDouble(EventType : longint) : boolean;
  230. begin
  231. Gpm_StrictDouble:=(EventType and GPM_DOUBLE<>0) and not(EventType and GPM_MFLAG<>0);
  232. end;
  233. function Gpm_AnyDouble(EventType : longint) : boolean;
  234. begin
  235. Gpm_AnyDouble:=(EventType and GPM_DOUBLE<>0);
  236. end;
  237. function Gpm_StrictTriple(EventType : longint) : boolean;
  238. begin
  239. Gpm_StrictTriple:=(EventType and GPM_TRIPLE<>0) and not(EventType and GPM_MFLAG<>0);
  240. end;
  241. function Gpm_AnyTriple(EventType : longint) : boolean;
  242. begin
  243. Gpm_AnyTriple:=(EventType and GPM_TRIPLE<>0);
  244. end;
  245. {$ifdef use_external}
  246. procedure Gpm_CheckVersion;
  247. var
  248. l : longint;
  249. begin
  250. Gpm_GetLibVersion(l);
  251. if l<11700 then
  252. begin
  253. writeln('You need at least gpm 1.17');
  254. halt(1);
  255. end;
  256. end;
  257. {$else}
  258. const checked_con:boolean=false;
  259. function putdata(where:longint;const what:Tgpmconnect):boolean;
  260. begin
  261. putdata:=true;
  262. if fpwrite(where,what,sizeof(Tgpmconnect))<>sizeof(Tgpmconnect) then
  263. begin
  264. { gpm_report(GPM_PR_ERR,GPM_MESS_WRITE_ERR,strerror(errno));}
  265. putdata:=false;
  266. end;
  267. end;
  268. function gpm_get_console:string63;
  269. var buf:stat;
  270. begin
  271. {First try the devfs device, because in the next time this will be
  272. the preferred one. If that fails, take the old console.}
  273. {Check for open new console.}
  274. if fpstat(GPM_DEVFS_CONSOLE,buf)=0 then
  275. gpm_get_console:=GPM_DEVFS_CONSOLE
  276. {Failed, try OLD console.}
  277. else if fpstat(GPM_OLD_CONSOLE,buf)=0 then
  278. gpm_get_console:=GPM_OLD_CONSOLE
  279. else
  280. gpm_get_console:='';
  281. end;
  282. procedure gpm_winch_hook(signum:longint;SigInfo: PSigInfo; SigContext: PSigContext);cdecl;
  283. var win:winsize;
  284. begin
  285. if (sigactionhandler(SIG_IGN)<>gpm_saved_winch_hook.sa_handler) and
  286. (sigactionhandler(SIG_DFL)<>gpm_saved_winch_hook.sa_handler) then
  287. gpm_saved_winch_hook.sa_handler(signum,nil,nil);
  288. if fpioctl(gpm_consolefd,TIOCGWINSZ,@win)=-1 then
  289. exit;
  290. if (win.ws_col=0) or (win.ws_row=0) then
  291. begin
  292. win.ws_col:=80;
  293. win.ws_row:=25;
  294. end;
  295. gpm_mx:=win.ws_col - gpm_zerobased;
  296. gpm_my:=win.ws_row - gpm_zerobased;
  297. end;
  298. procedure gpm_suspend_hook(signum:longint;SigInfo: PSigInfo; SigContext: PSigContext);cdecl;
  299. var conn:Tgpmconnect;
  300. old_sigset,new_sigset:Tsigset;
  301. sa:sigactionrec;
  302. success:boolean;
  303. begin
  304. fpsigemptyset(new_sigset);
  305. fpsigaddset(new_sigset,SIGTSTP);
  306. fpsigprocmask(SIG_BLOCK,new_sigset,old_sigset);
  307. {Open a completely transparent gpm connection.}
  308. conn.eventmask:=0;
  309. conn.defaultMask:=$ffff;
  310. conn.minmod:=$ffff;
  311. conn.maxmod:=0;
  312. {cannot do this under xterm, tough}
  313. success:=gpm_open(conn,0)>=0;
  314. {take the default action, whatever it is (probably a stop :)}
  315. fpsigprocmask(SIG_SETMASK,@old_sigset,nil);
  316. fpsigaction(SIGTSTP,@gpm_saved_suspend_hook,nil);
  317. fpkill(fpgetpid,SIGTSTP);
  318. { in bardo here }
  319. { Reincarnation. Prepare for another death early. }
  320. fpsigemptyset(sa.sa_mask);
  321. sa.sa_handler:=@gpm_suspend_hook;
  322. sa.sa_flags:=SA_NOMASK;
  323. fpsigaction(SIGTSTP,@sa,nil);
  324. { Pop the gpm stack by closing the useless connection }
  325. { but do it only when we know we opened one.. }
  326. if success then
  327. gpm_close;
  328. end;
  329. function gpm_open(var conn:Tgpmconnect;flag:longint):longint;
  330. var tty:string;
  331. flagstr:string[10];
  332. term:Pchar;
  333. i:cardinal;
  334. addr:Tunixsockaddr;
  335. win:Twinsize;
  336. n:Pgpm_stst;
  337. l:byte;
  338. p:byte; {there max 256 console ttys}
  339. buf:stat;
  340. sa:sigactionrec;
  341. label err;
  342. begin
  343. tty:='';
  344. options.consolename:='';
  345. { gpm_report(GPM_PR_DEBUG,"VC: %d",flag);}
  346. {....................................... First of all, check xterm}
  347. (*
  348. term:=fpgetenv('TERM');
  349. if (term<>nil) and (strcomp(term,'xterm')=0) then
  350. begin
  351. if gpm_tried then
  352. begin
  353. gpm_open:=gpm_fd; { no stack }
  354. exit;
  355. end;
  356. gpm_fd:=-2;
  357. {save old hilit tracking and enable mouse tracking}
  358. write(#27'[?1001s'#27'[?1000h');
  359. flush(output);
  360. gpm_flag:=true;
  361. gpm_open:=gpm_fd;
  362. exit;
  363. end;
  364. *)
  365. {....................................... No xterm, go on}
  366. { check whether we know what name the console is: what's with the lib??? }
  367. if not checked_con then
  368. begin
  369. options.consolename:=gpm_get_console;
  370. checked_con:=true;
  371. end;
  372. { So I chose to use the current tty, instead of /dev/console, which
  373. has permission problems. (I am fool, and my console is
  374. readable/writeable by everybody.
  375. However, making this piece of code work has been a real hassle.}
  376. if not gpm_flag and gpm_tried then
  377. begin
  378. gpm_open:=-1;
  379. exit;
  380. end;
  381. gpm_tried:=true; {do or die}
  382. new(n);
  383. n^.next:=gpm_stack;
  384. gpm_stack:=n;
  385. conn.pid:=fpgetpid; { fill obvious values }
  386. if n^.next<>nil then
  387. conn.vc:=n^.next^.info.vc {inherit}
  388. else
  389. begin
  390. conn.vc:=0; { default handler }
  391. if (flag>0) then
  392. begin { forced vc number }
  393. conn.vc:=flag;
  394. str(flag,flagstr);
  395. tty:=options.consolename+flagstr;
  396. end
  397. else
  398. begin {use your current vc}
  399. if isatty(0)<>0 then
  400. tty:=ttyname(0); { stdin }
  401. if (tty='') and (isatty(1)<>0) then
  402. tty:=ttyname(1); { stdout }
  403. if (tty='') and (isatty(2)<>0) then
  404. tty:=ttyname(2); { stderr }
  405. if (tty='') then
  406. begin
  407. { gpm_report(GPM_PR_ERR,"checking tty name failed");}
  408. goto err;
  409. end;
  410. conn.vc:=0;
  411. l:=length(tty);
  412. p:=1;
  413. while tty[l] in ['0'..'9'] do
  414. begin
  415. inc(conn.vc,p*(byte(tty[l])-byte('0')));
  416. p:=p*10;
  417. dec(l);
  418. end;
  419. end;
  420. if (gpm_consolefd=-1) then
  421. begin
  422. gpm_consolefd:=fpopen(tty,O_WRONLY);
  423. if gpm_consolefd<0 then
  424. begin
  425. { gpm_report(GPM_PR_ERR,GPM_MESS_DOUBLE_S,tty,strerror(errno));}
  426. goto err;
  427. end;
  428. end;
  429. end;
  430. n^.info:=conn;
  431. {....................................... Get screen dimensions }
  432. fpioctl(gpm_consolefd, TIOCGWINSZ, @win);
  433. if (win.ws_col or win.ws_row)=0 then
  434. begin
  435. {Hmmmm. The mad terminal didn't return it's size :/ }
  436. { fprintf(stderr, "libgpm: zero screen dimension, assuming 80x25.\n");}
  437. win.ws_col:=80;
  438. win.ws_row:=25;
  439. end;
  440. gpm_mx:=win.ws_col-gpm_zerobased;
  441. gpm_my:=win.ws_row-gpm_zerobased;
  442. {....................................... Connect to the control socket}
  443. if not gpm_flag then
  444. begin
  445. gpm_fd:=socket(AF_UNIX,SOCK_STREAM,0);
  446. if gpm_fd<0 then
  447. begin
  448. { gpm_report(GPM_PR_ERR,GPM_MESS_SOCKET,strerror(errno));}
  449. goto err;
  450. end;
  451. end;
  452. fillchar(addr,sizeof(addr),0);
  453. addr.family:=PF_UNIX;
  454. strcopy(addr.path, GPM_NODE_CTL);
  455. i:=sizeof(addr.family)+length(GPM_NODE_CTL);
  456. if fpconnect(gpm_fd,psockaddr(@addr),i)<0 then
  457. begin
  458. { gpm_report(GPM_PR_INFO,GPM_MESS_DOUBLE_S,GPM_NODE_CTL,strerror(errno));}
  459. {Well, try to open a chr device called /dev/gpmctl. This should
  460. be forward-compatible with a kernel server.}
  461. fpclose(gpm_fd); {the socket}
  462. gpm_fd:=fpopen(GPM_NODE_DEV,O_RDWR);
  463. if gpm_fd=-1 then
  464. begin
  465. { gpm_report(GPM_PR_ERR,GPM_MESS_DOUBLE_S,GPM_NODE_DEV
  466. ,strerror(errno));}
  467. goto err;
  468. end;
  469. if (fpfstat(gpm_fd,buf)=-1) or (buf.st_mode and STAT_IFMT<>STAT_IFCHR) then
  470. goto err;
  471. end;
  472. {....................................... Put your data}
  473. if putdata(gpm_fd,conn) then
  474. begin
  475. { itz Wed Dec 16 23:22:16 PST 1998 use sigaction, the old
  476. code caused a signal loop under XEmacs }
  477. fpsigemptyset(sa.sa_mask);
  478. { And the winch (window-resize) hook .. }
  479. sa.sa_handler:=@gpm_winch_hook;
  480. sa.sa_flags:=0;
  481. fpsigaction(SIGWINCH,@sa,@gpm_saved_winch_hook);
  482. if gpm_flag then
  483. begin
  484. { Install suspend hook }
  485. sa.sa_handler:=sigactionhandler(SIG_IGN);
  486. fpsigaction(SIGTSTP,@sa,@gpm_saved_suspend_hook);
  487. {if signal was originally ignored, job control is not supported}
  488. if gpm_saved_suspend_hook.sa_handler<>sigactionhandler(SIG_IGN) then
  489. begin
  490. sa.sa_flags:=SA_NOMASK;
  491. sa.sa_handler:=@gpm_suspend_hook;
  492. fpsigaction(SIGTSTP,@sa,nil);
  493. end;
  494. end;
  495. end;
  496. gpm_open:=gpm_fd;
  497. exit;
  498. {....................................... Error: free all memory}
  499. err:
  500. { gpm_report(GPM_PR_ERR,'Oh, oh, it''s an error! possibly I die! ');}
  501. repeat
  502. n:=gpm_stack^.next;
  503. dispose(gpm_stack);
  504. gpm_stack:=n;
  505. until gpm_stack=nil;
  506. if gpm_fd>=0 then
  507. fpclose(gpm_fd);
  508. gpm_flag:=false;
  509. gpm_open:=-1;
  510. end;
  511. function gpm_close:longint;
  512. var next:Pgpm_stst;
  513. begin
  514. gpm_tried:=false; { reset the error flag for next time }
  515. (*
  516. if gpm_fd=-2 then { xterm }
  517. begin
  518. write(#27'[?1000l'#27'[?1001r');
  519. flush(output);
  520. end
  521. else { linux }
  522. *)
  523. begin
  524. if not gpm_flag then
  525. gpm_close:=0
  526. else
  527. begin
  528. next:=gpm_stack^.next;
  529. dispose(gpm_stack);
  530. gpm_stack:=next;
  531. if next<>nil then
  532. putdata(gpm_fd,next^.info);
  533. gpm_flag:=false;
  534. end;
  535. end;
  536. if gpm_fd>=0 then
  537. fpclose(gpm_fd);
  538. gpm_fd:=-1;
  539. fpsigaction(SIGTSTP,@gpm_saved_suspend_hook,nil);
  540. fpsigaction(SIGWINCH,@gpm_saved_winch_hook,nil);
  541. fpclose(gpm_consolefd);
  542. gpm_consolefd:=-1;
  543. gpm_close:=0;
  544. end;
  545. function gpm_getevent(var event:Tgpm_event):longint;
  546. var count:longint;
  547. begin
  548. gpm_getevent:=0;
  549. if gpm_fd=-1 then
  550. exit;
  551. count:=fpread(gpm_fd,event,sizeof(Tgpm_event));
  552. if count<>sizeof(Tgpm_event) then
  553. begin
  554. {avoid to send the message if there is no data; sometimes it makes
  555. sense to poll the mouse descriptor any now an then using a
  556. non-blocking descriptor}
  557. { if (count<>-1) or (errno<>EAGAIN)
  558. gpm_report(GPM_PR_INFO,"Read too few bytes (%i) at %s:%d",
  559. count,__FILE__,__LINE__);}
  560. gpm_getevent:=-1;
  561. exit;
  562. end;
  563. dec(event.x,gpm_zerobased);
  564. dec(event.y,gpm_zerobased);
  565. gpm_getevent:=1;
  566. end;
  567. function gpm_repeat(millisec:longint):longint;
  568. var fd:longint;
  569. selset:Tfdset;
  570. begin
  571. fd:=0; {Default to stdin (xterm).}
  572. if gpm_fd>=0 then
  573. fd:=gpm_fd;
  574. fpFD_ZERO(selset);
  575. fpFD_SET(fd,selset);
  576. gpm_repeat:=fpselect(fd+1,@selset,nil,nil,millisec);
  577. end;
  578. function gpm_fitvaluesM(var x,y:longint;margin:longint):longint;
  579. begin
  580. gpm_fitvaluesM:=0;
  581. if margin=-1 then
  582. begin
  583. if x<gpm_zerobased then
  584. x:=gpm_zerobased
  585. else if x>gpm_mx then
  586. x:=gpm_mx;
  587. if y<gpm_zerobased then
  588. y:=gpm_zerobased
  589. else if y>gpm_my then
  590. y:=gpm_my;
  591. end
  592. else
  593. case margin of
  594. GPM_TOP:
  595. inc(y);
  596. GPM_BOT:
  597. dec(y);
  598. GPM_RGT:
  599. dec(x);
  600. GPM_LFT:
  601. inc(x);
  602. end;
  603. end;
  604. function gpm_fitvalues(var x,y:longint):longint;inline;
  605. begin
  606. gpm_fitvalues:=gpm_fitvaluesm(x,y,-1);
  607. end;
  608. function gpm_handle_roi(var eptr:Tgpm_event;clientdata:pointer):longint;cdecl;
  609. var backevent:Tgpm_event;
  610. roi:Pgpm_roi;
  611. begin
  612. roi:=gpm_current_roi;
  613. {If motion or press, look for the interested roi.
  614. Drag and release will be reported to the old roi.}
  615. if eptr.eventtype and (GPM_MOVE or GPM_DOWN)<>0 then
  616. begin
  617. roi:=gpm_roi;
  618. while roi<>nil do
  619. begin
  620. if not ((roi^.xmin>eptr.x) or (roi^.xmax<eptr.x)) and
  621. not ((roi^.ymin>eptr.y) or (roi^.ymax<eptr.y)) and
  622. not ((roi^.minmod and eptr.modifiers)<roi^.minmod) and
  623. not ((roi^.maxmod and eptr.modifiers)<eptr.modifiers) then
  624. break;
  625. roi:=roi^.next;
  626. end;
  627. end;
  628. {Now generate the leave/enter events}
  629. if roi<>gpm_current_roi then
  630. begin
  631. if (gpm_current_roi<>nil) and (gpm_current_roi^.eventmask and GPM_LEAVE<>0) then
  632. begin
  633. backevent.eventtype:=GPM_LEAVE;
  634. gpm_current_roi^.handler(backevent,gpm_current_roi^.clientdata);
  635. end;
  636. if (roi<>nil) and (roi^.eventmask and GPM_ENTER<>0) then
  637. begin
  638. backevent.eventtype:=GPM_ENTER;
  639. roi^.handler(backevent,roi^.clientdata);
  640. end;
  641. end;
  642. gpm_current_roi:=roi;
  643. {events not requested are discarded}
  644. if (roi<>nil) and (eptr.eventtype and ($0f or GPM_ENTER or GPM_LEAVE) and roi^.eventmask=0) then
  645. gpm_handle_roi:=0
  646. else
  647. begin
  648. backevent:=eptr; {copy it, so the main one is unchanged}
  649. if roi=nil then
  650. if gpm_roi_handler<>nil then
  651. gpm_handle_roi:=gpm_roi_handler(backevent,gpm_roi_data)
  652. else
  653. gpm_handle_roi:=0
  654. else
  655. begin
  656. {Ok, now report the event as it is, after modifying x and y}
  657. dec(backevent.x,roi^.xmin);
  658. dec(backevent.y,roi^.ymin);
  659. roi^.handler(backevent,roi^.clientdata);
  660. end;
  661. end;
  662. end;
  663. function gpm_pushroi(x1:longint;y1:longint;x2:longint;y2:longint;
  664. mask:longint;fun:Tgpmhandler;xtradata:pointer):Pgpm_roi;
  665. var n:Pgpm_roi;
  666. begin
  667. {create a roi and push it}
  668. new(n);
  669. {use the roi handler, if still null}
  670. if (gpm_roi<>nil) and (gpm_handler<>nil) then
  671. gpm_handler:=@gpm_handle_roi;
  672. n^.xmin:=x1; n^.xmax:=x2;
  673. n^.ymin:=y1; n^.ymax:=y2;
  674. n^.minmod:=0; n^.maxmod:=$ffff;
  675. n^.prev:=nil; n^.next:=nil;
  676. n^.eventmask:=mask;
  677. n^.owned:=0; { use dispose }
  678. n^.handler:=fun;
  679. if xtradata=nil then
  680. n^.clientdata:=n
  681. else
  682. n^.clientdata:=xtradata;
  683. gpm_pushroi:=gpm_raiseroi(n,nil);
  684. end;
  685. function gpm_useroi(n:Pgpm_roi):Pgpm_roi;
  686. begin
  687. { use a Roi by pushing it }
  688. n^.prev:=nil;
  689. n^.next:=nil;
  690. n^.owned:=1;
  691. { use the roi handler, if still nil }
  692. if (gpm_roi=nil) and (gpm_handler=nil) then
  693. gpm_handler:=@gpm_handle_roi;
  694. gpm_useroi:=gpm_raiseroi(n,nil);
  695. end;
  696. function gpm_poproi(which:Pgpmroi):Pgpmroi;
  697. begin
  698. {extract the Roi and remove it}
  699. if which^.prev<>nil then
  700. which^.prev^.next:=which^.next;
  701. if which^.next<>nil then
  702. which^.next^.prev:=which^.prev;
  703. if gpm_roi=which then
  704. gpm_roi:=which^.next;
  705. if which^.owned=0 then
  706. dispose(which);
  707. if gpm_current_roi=which then
  708. gpm_current_roi:=nil;
  709. gpm_poproi:=gpm_roi; {return the new top-of-stack}
  710. end;
  711. function gpm_raiseroi(which:Pgpmroi;before:Pgpmroi):Pgpmroi;
  712. begin
  713. {raise a Roi above another, or to top-of-stack}
  714. if gpm_roi=nil then
  715. begin
  716. gpm_roi:=which;
  717. gpm_raiseroi:=which;
  718. exit;
  719. end;
  720. if before=nil then
  721. before:=gpm_roi;
  722. if before=which then
  723. begin
  724. gpm_raiseroi:=gpm_roi;
  725. exit;
  726. end;
  727. if which^.prev<>nil then
  728. which^.prev^.next:=which^.next;
  729. if which^.next<>nil then
  730. which^.next^.prev:=which^.prev;
  731. if gpm_roi=which then
  732. gpm_roi:=which^.next;
  733. which^.prev:=before^.prev;
  734. before^.prev:=which;
  735. which^.next:=before;
  736. if which^.prev<>nil then
  737. which^.prev^.next:=which
  738. else
  739. gpm_roi:=which;
  740. gpm_raiseroi:=gpm_roi; { return the new top-of-stack }
  741. end;
  742. function gpm_lowerroi(which:Pgpmroi;after:Pgpmroi):Pgpmroi;
  743. begin
  744. {lower a Roi below another, or to bottom-of-stack}
  745. if after=nil then
  746. begin
  747. after:=gpm_roi;
  748. while after^.next<>nil do
  749. after:=after^.next;
  750. end;
  751. if after=which then
  752. begin
  753. gpm_lowerroi:=gpm_roi;
  754. exit;
  755. end;
  756. if which^.prev<>nil then
  757. which^.prev^.next:=which^.next;
  758. if which^.next<>nil then
  759. which^.next^.prev:=which^.prev;
  760. if gpm_roi=which then
  761. gpm_roi:=which^.next;
  762. which^.next:=after^.next;
  763. after^.next:=which;
  764. which^.prev:=after;
  765. if which^.next<>nil then
  766. which^.next^.prev:=which;
  767. gpm_lowerroi:=gpm_roi; {return the new top-of-stack}
  768. end;
  769. function gpm_getsnapshot(eptr:Pgpm_event):longint;
  770. var conn:Tgpm_connect;
  771. event:Tgpm_event;
  772. sillyset:Tfdset;
  773. i:longint;
  774. begin
  775. fillchar(conn,sizeof(conn),0);
  776. if eptr<>nil then
  777. conn.vc:=GPM_REQ_SNAPSHOT
  778. else
  779. begin
  780. conn.vc:=GPM_REQ_BUTTONS;
  781. eptr:=@event;
  782. end;
  783. if gpm_fd=-1 then
  784. begin
  785. gpm_getsnapshot:=-1;
  786. exit;
  787. end;
  788. fpFD_ZERO(sillyset);
  789. fpFD_SET(gpm_fd,sillyset);
  790. if fpselect(gpm_fd+1,@sillyset,nil,nil,0)=1 then
  791. gpm_getsnapshot:=0
  792. else
  793. begin
  794. fpwrite(gpm_fd,conn,sizeof(Tgpm_connect));
  795. i:=gpm_getevent(eptr^);
  796. if i<>1 then
  797. gpm_getsnapshot:=-1
  798. else
  799. begin
  800. gpm_getsnapshot:=eptr^.eventtype; { number of buttons }
  801. if eptr^.eventtype=0 then
  802. gpm_getsnapshot:=15;
  803. eptr^.eventtype:=0;
  804. end;
  805. end;
  806. end;
  807. function gpm_getsnapshot(var eptr:Tgpmevent):longint;inline;
  808. begin
  809. gpm_getsnapshot:=gpm_getsnapshot(@eptr);
  810. end;
  811. {$endif}
  812. end.