callback.tex 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. \begin{FPCList}
  2. \item[Example]
  3. \begin{verbatim}
  4. Program callback;
  5. uses crt,
  6. go32;
  7. const mouseint = $33;
  8. var mouse_regs : trealregs;
  9. mouse_seginfo : tseginfo;
  10. var mouse_numbuttons : longint;
  11. mouse_action : word;
  12. mouse_x, mouse_y : Word;
  13. mouse_b : Word;
  14. userproc_installed : Longbool;
  15. userproc_length : Longint;
  16. userproc_proc : pointer;
  17. {$ASMMODE DIRECT}
  18. procedure callback_handler; assembler;
  19. asm
  20. pushw %es
  21. pushw %ds
  22. pushl %edi
  23. pushl %esi
  24. cmpl $1, _USERPROC_INSTALLED
  25. je .LNoCallback
  26. pushal
  27. movw %es, %ax
  28. movw %ax, %ds
  29. movw U_GO32_DOSMEMSELECTOR, %ax
  30. movw %ax, %fs
  31. call *_USERPROC_PROC
  32. popal
  33. .LNoCallback:
  34. popl %esi
  35. popl %edi
  36. popw %ds
  37. popw %es
  38. movl (%esi), %eax
  39. movl %eax, %es: 42(%edi)
  40. addw $4, %es: 46(%edi)
  41. iret
  42. end;
  43. procedure mouse_dummy; begin end;
  44. procedure textuserproc;
  45. begin
  46. mouse_b := mouse_regs.bx;
  47. mouse_x := (mouse_regs.cx shr 3) + 1;
  48. mouse_y := (mouse_regs.dx shr 3) + 1;
  49. end;
  50. procedure install_mouse (userproc : pointer;
  51. userproclen : longint);
  52. var r : trealregs;
  53. begin
  54. r.eax := $0; realintr(mouseint, r);
  55. if (r.eax <> $FFFF) then begin
  56. Writeln('No Mircosoft compatible mouse found');
  57. Write('A Mircosoft compatible mouse driver is');
  58. writeln(' necessary to run this example');
  59. halt;
  60. end;
  61. if (r.bx = $ffff) then mouse_numbuttons := 2
  62. else mouse_numbuttons := r.bx;
  63. Writeln(mouse_numbuttons,
  64. ' button Mircosoft compatible mouse found.');
  65. if (userproc <> nil) then begin
  66. userproc_proc := userproc;
  67. userproc_installed := true;
  68. userproc_length := userproclen;
  69. lock_code(userproc_proc, userproc_length);
  70. end else begin
  71. userproc_proc := nil;
  72. userproc_length := 0;
  73. userproc_installed := false;
  74. end;
  75. lock_data(mouse_x, sizeof(mouse_x));
  76. lock_data(mouse_y, sizeof(mouse_y));
  77. lock_data(mouse_b, sizeof(mouse_b));
  78. lock_data(mouse_action, sizeof(mouse_action));
  79. lock_data(userproc_installed, sizeof(userproc_installed));
  80. lock_data(@userproc_proc, sizeof(userproc_proc));
  81. lock_data(mouse_regs, sizeof(mouse_regs));
  82. lock_data(mouse_seginfo, sizeof(mouse_seginfo));
  83. lock_code(@callback_handler,
  84. longint(@mouse_dummy)
  85. - longint(@callback_handler));
  86. get_rm_callback(@callback_handler, mouse_regs, mouse_seginfo);
  87. r.eax := $0c; r.ecx := $7f; r.edx := longint(mouse_seginfo.offset);
  88. r.es := mouse_seginfo.segment;
  89. realintr(mouseint, r);
  90. r.eax := $01;
  91. realintr(mouseint, r);
  92. end;
  93. procedure remove_mouse;
  94. var r : trealregs;
  95. begin
  96. r.eax := $02; realintr(mouseint, r);
  97. r.eax := $0c; r.ecx := 0; r.edx := 0; r.es := 0;
  98. realintr(mouseint, r);
  99. free_rm_callback(mouse_seginfo);
  100. if (userproc_installed) then begin
  101. unlock_code(userproc_proc, userproc_length);
  102. userproc_proc := nil;
  103. userproc_length := 0;
  104. userproc_installed := false;
  105. end;
  106. unlock_data(mouse_x, sizeof(mouse_x));
  107. unlock_data(mouse_y, sizeof(mouse_y));
  108. unlock_data(mouse_b, sizeof(mouse_b));
  109. unlock_data(mouse_action, sizeof(mouse_action));
  110. unlock_data(@userproc_proc, sizeof(userproc_proc));
  111. unlock_data(userproc_installed,
  112. sizeof(userproc_installed));
  113. unlock_data(mouse_regs, sizeof(mouse_regs));
  114. unlock_data(mouse_seginfo, sizeof(mouse_seginfo));
  115. unlock_code(@callback_handler,
  116. longint(@mouse_dummy)
  117. - longint(@callback_handler));
  118. fillchar(mouse_seginfo, sizeof(mouse_seginfo), 0);
  119. end;
  120. begin
  121. install_mouse(@textuserproc, 400);
  122. Writeln('Press any key to exit...');
  123. while (not keypressed) do begin
  124. { write mouse state info }
  125. gotoxy(1, wherey);
  126. write('MouseX : ', mouse_x:2,
  127. ' MouseY : ', mouse_y:2,
  128. ' Buttons : ', mouse_b:2);
  129. end;
  130. remove_mouse;
  131. end.\end{verbatim}
  132. \end{FPCList}