sysmousetest.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. Program Sysmousetest;
  2. {
  3. This program is part of the FPC demoes.
  4. Copyright (C) 2000 by Marco van de Voort
  5. Originally for a FPC on FreeBSD article in a 2000 edition of
  6. the German magazine FreeX
  7. A test for sysmouse. Moused must be loaded. Maybe works in xterm too if
  8. X Windows is configured to use sysmouse.
  9. See the file COPYING.FPC, included in this distribution,
  10. for details about the copyright.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. **********************************************************************}
  15. Uses BaseUnix,Unix,Console;
  16. CONST STDIN=0;
  17. TYPE ActionType=(click,paste);
  18. procedure Window1Handler(X,Y:LONGINT;Action:ActionType);
  19. begin
  20. IF Action=Click THEN
  21. Writeln('Click in Window 1, relative coordinates: ',X,':',Y)
  22. ELSE
  23. Writeln('Paste in Window 1, relative coordinates: ',X,':',Y);
  24. end;
  25. procedure Window2Handler(X,Y:LONGINT;Action:ActionType);
  26. begin
  27. IF Action=Click THEN
  28. Writeln('Click in Window 2, relative coordinates: ',X,':',Y)
  29. ELSE
  30. Writeln('Paste in Window 2, relative coordinates: ',X,':',Y);
  31. end;
  32. procedure Window3Handler(X,Y:LONGINT;Action:ActionType);
  33. begin
  34. IF Action=Click THEN
  35. Writeln('Click in Window 3, relative coordinates: ',X,':',Y)
  36. ELSE
  37. Writeln('Paste in Window 3, relative coordinates: ',X,':',Y);
  38. end;
  39. procedure Window4Handler(X,Y:LONGINT;Action:ActionType);
  40. begin
  41. IF Action=Click THEN
  42. Writeln('Click in Window 4, relative coordinates: ',X,':',Y)
  43. ELSE
  44. Writeln('Paste in Window 4, relative coordinates: ',X,':',Y);
  45. end;
  46. {Of course in a real window manager, all this would be more dynamic (so you
  47. can change windows, and have them stacked. }
  48. TYPE WindowHandlerProc = procedure (X,Y:longint;Action:ActionType);
  49. WindowListType = ARRAY[1..4] OF WindowHandlerProc;
  50. CONST WindowList : WindowListType=(@Window1Handler,@Window2Handler,
  51. @Window3Handler,@Window4Handler);
  52. var cwidth,cheight : longint; {Dimensions of a char cell.
  53. For pixels to chars}
  54. xpos,ypos,buttons : longint; {Location and type of last mouseclick}
  55. Focus : Longint; {Quarter of screen that has focus now}
  56. TermApp : Boolean;
  57. {
  58. * Signal handler for SIGUSR2: Retrieves mouse coordinates; converts pixels
  59. * to rows and columns.
  60. }
  61. procedure Sysmousehandler(Sig:Longint);cdecl; {IMPORTANT! call back has C calling convention}
  62. var mi : MOUSE_INFO;
  63. fd : longint;
  64. begin
  65. fd:=STDIN;
  66. mi.operation := MOUSE_GETINFO;
  67. IF NOT CONS_MOUSECTL(fd, mi) THEN
  68. {Mouse call failed, don't update vars}
  69. exit;
  70. xpos := mi.u.data.x;
  71. ypos := mi.u.data.y;
  72. buttons := mi.u.data.buttons and 7;
  73. end;
  74. procedure StartMouse;
  75. {initialise the mouse and determine the sizes of a standard character cell}
  76. var
  77. mi : mouse_info_t;
  78. vi : video_info_t;
  79. fd : longint;
  80. begin
  81. fd:=longint(stdin);
  82. if FBIO_GETMODE(fd,vi.vi_mode) AND FBIO_MODEINFO(fd,vi) then
  83. begin
  84. cwidth:=vi.vi_cwidth;
  85. cheight:=vi.vi_cheight;
  86. Writeln('Dimensions of a character cell (width :height) :',Cwidth,':',cheight);
  87. end;
  88. {ignore SIGUSR2 for a while, otherwise moving the mouse before handler
  89. installation will terminate the application}
  90. fpSignal(SIGUSR2,SignalHandler(SIG_IGN));
  91. { Have sysmouse send us SIGUSR2 for mouse state changes. }
  92. mi.operation := _MOUSE_MODE; { Note: underscore added!}
  93. mi.u.mode.mode := 0;
  94. mi.u.mode.signal := SIGUSR2;
  95. {If successful, register signal handler}
  96. if CONS_MOUSECTL(fd,mi) then
  97. begin
  98. { Connect SIGUSR2 to our (C calling convention!) mousehandler}
  99. fpsignal(SIGUSR2, @SysmouseHandler);
  100. {show mouse}
  101. mi.operation := MOUSE_SHOW;
  102. CONS_MOUSECTL(fd, mi);
  103. exit;
  104. end;
  105. end;
  106. procedure myhandler(x,y,but :longint);
  107. VAR WindowNr : Longint;
  108. begin
  109. {Upper left 2x2 character cell block terminates program}
  110. if (X<3) AND (Y<3) then
  111. begin
  112. TermApp:=TRUE;
  113. EXIT;
  114. end;
  115. {The screen is divided in four windows and are numbered as follows:
  116. 1|2
  117. ---
  118. 3|4}
  119. if (x<=40) then
  120. WindowNr:=1
  121. else
  122. WindowNr:=2;
  123. IF Y>12 THEN
  124. INC(WindowNr,2);
  125. IF WindowNr=Focus THEN
  126. BEGIN
  127. {This window already has focus. Normalise coordinates and
  128. pass the event to the window}
  129. IF X>40 THEN Dec(X,40);
  130. IF Y>12 THEN Dec(Y,12);
  131. IF (But and 1)=1 THEN
  132. WindowList[WindowNr](x,y,click)
  133. else
  134. IF (But and 4)=4 THEN
  135. WindowList[WindowNr](x,y,paste)
  136. else
  137. Writeln('I only have a two button mouse, so this one does nothing');
  138. END
  139. else
  140. BEGIN
  141. Writeln('Main handler is changing focus from to window',WindowNr);
  142. Focus:=WindowNr;
  143. end;
  144. end;
  145. procedure WaitForEvent;
  146. {
  147. * Wait in select() loop. If interrupted, check for mouse button press and
  148. * construct a minimal gpm pseudo-event and call MouseHandler(). Otherwise
  149. * hand over to wgetch().
  150. }
  151. var rfds : tsigset;
  152. begin
  153. fpsigemptyset(rfds);
  154. fpsigaddset(rfds,STDIN);
  155. while fpselect(1, @rfds,nil,nil,nil)<=0 DO
  156. begin
  157. IF TermApp THEN Exit;
  158. if (fpgeterrno= ESYSEINTR) AND (buttons<>0) THEN
  159. MyHandler ((xpos DIV cwidth)+1,(ypos DIV cheight)+1,buttons);
  160. end;
  161. end;
  162. begin
  163. // if physicalconsole(0) then
  164. // begin
  165. {Don't want to use ncurses, to easier link static}
  166. Write(#27'[?1001s'); { save old hilight tracking }
  167. Write(#27'[?1000h'); { enable mouse tracking }
  168. for cwidth:=1 to 25 DO Writeln;
  169. Writeln('Sysmouse demo, click upper-left corner to exit this program');
  170. Writeln;
  171. Writeln('Sysmouse implements a very simple mouse event driven windowing program');
  172. Writeln('The 4 quadrants of the screen act as windows, and focus is implemented');
  173. Writeln('Try to click (left or right) the different quadrants, and see what happens');
  174. Writeln;
  175. cwidth := 8; cheight := 16;
  176. Focus:=0;
  177. StartMouse;
  178. TermApp:=FALSE;
  179. while not TermApp do WaitForEvent;
  180. // end
  181. //else
  182. // Writeln('This program must be run from the physical console, not over telnet or under X');
  183. end.