mouse.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. #include "io.h"
  16. #define Image IMAGE
  17. #include <draw.h>
  18. #include <memdraw.h>
  19. #include <cursor.h>
  20. #include "screen.h"
  21. /*
  22. * mouse types
  23. */
  24. enum
  25. {
  26. Mouseother= 0,
  27. Mouseserial= 1,
  28. MousePS2= 2,
  29. };
  30. extern int mouseshifted;
  31. static QLock mousectlqlock;
  32. static int mousetype;
  33. static int intellimouse;
  34. static int packetsize;
  35. static int resolution;
  36. static int accelerated;
  37. static int mousehwaccel;
  38. enum
  39. {
  40. CMaccelerated,
  41. CMhwaccel,
  42. CMintellimouse,
  43. CMlinear,
  44. CMps2,
  45. CMps2intellimouse,
  46. CMres,
  47. CMreset,
  48. CMserial,
  49. };
  50. static Cmdtab mousectlmsg[] =
  51. {
  52. CMaccelerated, "accelerated", 0,
  53. CMhwaccel, "hwaccel", 2,
  54. CMintellimouse, "intellimouse", 1,
  55. CMlinear, "linear", 1,
  56. CMps2, "ps2", 1,
  57. CMps2intellimouse, "ps2intellimouse", 1,
  58. CMres, "res", 0,
  59. CMreset, "reset", 1,
  60. CMserial, "serial", 0,
  61. };
  62. /*
  63. * ps/2 mouse message is three bytes
  64. *
  65. * byte 0 - 0 0 SDY SDX 1 M R L
  66. * byte 1 - DX
  67. * byte 2 - DY
  68. *
  69. * shift & right button is the same as middle button
  70. *
  71. * Intellimouse and AccuPoint with extra buttons deliver
  72. * byte 3 - 00 or 01 or FF according to extra button state.
  73. * extra buttons are mapped in this code to buttons 4 and 5.
  74. * AccuPoint generates repeated events for these buttons;
  75. * it and Intellimouse generate 'down' events only, so
  76. * user-level code is required to generate button 'up' events
  77. * if they are needed by the application.
  78. * Also on laptops with AccuPoint AND external mouse, the
  79. * controller may deliver 3 or 4 bytes according to the type
  80. * of the external mouse; code must adapt.
  81. *
  82. * On the NEC Versa series (and perhaps others?) we seem to
  83. * lose a byte from the packet every once in a while, which
  84. * means we lose where we are in the instruction stream.
  85. * To resynchronize, if we get a byte more than two seconds
  86. * after the previous byte, we assume it's the first in a packet.
  87. */
  88. /*
  89. * set up a ps2 mouse
  90. */
  91. static void
  92. ps2mouse(void)
  93. {
  94. if(mousetype == MousePS2)
  95. return;
  96. if (0) /* For now, this is done in main.c */
  97. mouseenable();
  98. /* make mouse streaming, enabled */
  99. mousecmd(0xEA);
  100. mousecmd(0xF4);
  101. mousetype = MousePS2;
  102. packetsize = 3;
  103. mousehwaccel = 1;
  104. }
  105. /*
  106. * The PS/2 Trackpoint multiplexor on the IBM Thinkpad T23 ignores
  107. * acceleration commands. It is supposed to pass them on
  108. * to the attached device, but my Logitech mouse is simply
  109. * not behaving any differently. For such devices, we allow
  110. * the user to use "hwaccel off" to tell us to back off to
  111. * software acceleration even if we're using the PS/2 port.
  112. * (Serial mice are always software accelerated.)
  113. * For more information on the Thinkpad multiplexor, see
  114. * http://wwwcssrv.almaden.ibm.com/trackpoint/
  115. */
  116. static void
  117. setaccelerated(int x)
  118. {
  119. accelerated = x;
  120. if(mousehwaccel){
  121. switch(mousetype){
  122. case MousePS2:
  123. mousecmd(0xE7);
  124. return;
  125. }
  126. }
  127. mouseaccelerate(x);
  128. }
  129. static void
  130. setlinear(void)
  131. {
  132. accelerated = 0;
  133. if(mousehwaccel){
  134. switch(mousetype){
  135. case MousePS2:
  136. mousecmd(0xE6);
  137. return;
  138. }
  139. }
  140. mouseaccelerate(0);
  141. }
  142. static void
  143. setres(int n)
  144. {
  145. resolution = n;
  146. switch(mousetype){
  147. case MousePS2:
  148. mousecmd(0xE8);
  149. mousecmd(n);
  150. break;
  151. }
  152. }
  153. static void
  154. setintellimouse(void)
  155. {
  156. intellimouse = 1;
  157. packetsize = 4;
  158. switch(mousetype){
  159. case MousePS2:
  160. mousecmd(0xF3); /* set sample */
  161. mousecmd(0xC8);
  162. mousecmd(0xF3); /* set sample */
  163. mousecmd(0x64);
  164. mousecmd(0xF3); /* set sample */
  165. mousecmd(0x50);
  166. break;
  167. }
  168. }
  169. static void
  170. resetmouse(void)
  171. {
  172. packetsize = 3;
  173. switch(mousetype){
  174. case MousePS2:
  175. mousecmd(0xF6);
  176. mousecmd(0xEA); /* streaming */
  177. mousecmd(0xE8); /* set resolution */
  178. mousecmd(3);
  179. mousecmd(0xF4); /* enabled */
  180. break;
  181. }
  182. }
  183. void
  184. mousectl(Cmdbuf *cb)
  185. {
  186. Proc *up = externup();
  187. Cmdtab *ct;
  188. qlock(&mousectlqlock);
  189. if(waserror()){
  190. qunlock(&mousectlqlock);
  191. nexterror();
  192. }
  193. ct = lookupcmd(cb, mousectlmsg, nelem(mousectlmsg));
  194. switch(ct->index){
  195. case CMaccelerated:
  196. setaccelerated(cb->nf == 1 ? 1 : atoi(cb->f[1]));
  197. break;
  198. case CMintellimouse:
  199. setintellimouse();
  200. break;
  201. case CMlinear:
  202. setlinear();
  203. break;
  204. case CMps2:
  205. intellimouse = 0;
  206. ps2mouse();
  207. break;
  208. case CMps2intellimouse:
  209. ps2mouse();
  210. setintellimouse();
  211. break;
  212. case CMres:
  213. if(cb->nf >= 2)
  214. setres(atoi(cb->f[1]));
  215. else
  216. setres(1);
  217. break;
  218. case CMreset:
  219. resetmouse();
  220. if(accelerated)
  221. setaccelerated(accelerated);
  222. if(resolution)
  223. setres(resolution);
  224. if(intellimouse)
  225. setintellimouse();
  226. break;
  227. case CMhwaccel:
  228. if(strcmp(cb->f[1], "on")==0)
  229. mousehwaccel = 1;
  230. else if(strcmp(cb->f[1], "off")==0)
  231. mousehwaccel = 0;
  232. else
  233. cmderror(cb, "bad mouse control message");
  234. }
  235. qunlock(&mousectlqlock);
  236. poperror();
  237. }