mouse.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. mouseenable();
  97. /* make mouse streaming, enabled */
  98. mousecmd(0xEA);
  99. mousecmd(0xF4);
  100. mousetype = MousePS2;
  101. packetsize = 3;
  102. mousehwaccel = 1;
  103. }
  104. /*
  105. * The PS/2 Trackpoint multiplexor on the IBM Thinkpad T23 ignores
  106. * acceleration commands. It is supposed to pass them on
  107. * to the attached device, but my Logitech mouse is simply
  108. * not behaving any differently. For such devices, we allow
  109. * the user to use "hwaccel off" to tell us to back off to
  110. * software acceleration even if we're using the PS/2 port.
  111. * (Serial mice are always software accelerated.)
  112. * For more information on the Thinkpad multiplexor, see
  113. * http://wwwcssrv.almaden.ibm.com/trackpoint/
  114. */
  115. static void
  116. setaccelerated(int x)
  117. {
  118. accelerated = x;
  119. if(mousehwaccel){
  120. switch(mousetype){
  121. case MousePS2:
  122. mousecmd(0xE7);
  123. return;
  124. }
  125. }
  126. mouseaccelerate(x);
  127. }
  128. static void
  129. setlinear(void)
  130. {
  131. accelerated = 0;
  132. if(mousehwaccel){
  133. switch(mousetype){
  134. case MousePS2:
  135. mousecmd(0xE6);
  136. return;
  137. }
  138. }
  139. mouseaccelerate(0);
  140. }
  141. static void
  142. setres(int n)
  143. {
  144. resolution = n;
  145. switch(mousetype){
  146. case MousePS2:
  147. mousecmd(0xE8);
  148. mousecmd(n);
  149. break;
  150. }
  151. }
  152. static void
  153. setintellimouse(void)
  154. {
  155. intellimouse = 1;
  156. packetsize = 4;
  157. switch(mousetype){
  158. case MousePS2:
  159. mousecmd(0xF3); /* set sample */
  160. mousecmd(0xC8);
  161. mousecmd(0xF3); /* set sample */
  162. mousecmd(0x64);
  163. mousecmd(0xF3); /* set sample */
  164. mousecmd(0x50);
  165. break;
  166. }
  167. }
  168. static void
  169. resetmouse(void)
  170. {
  171. packetsize = 3;
  172. switch(mousetype){
  173. case MousePS2:
  174. mousecmd(0xF6);
  175. mousecmd(0xEA); /* streaming */
  176. mousecmd(0xE8); /* set resolution */
  177. mousecmd(3);
  178. mousecmd(0xF4); /* enabled */
  179. break;
  180. }
  181. }
  182. void
  183. mousectl(Cmdbuf *cb)
  184. {
  185. Proc *up = externup();
  186. Cmdtab *ct;
  187. qlock(&mousectlqlock);
  188. if(waserror()){
  189. qunlock(&mousectlqlock);
  190. nexterror();
  191. }
  192. ct = lookupcmd(cb, mousectlmsg, nelem(mousectlmsg));
  193. switch(ct->index){
  194. case CMaccelerated:
  195. setaccelerated(cb->nf == 1 ? 1 : atoi(cb->f[1]));
  196. break;
  197. case CMintellimouse:
  198. setintellimouse();
  199. break;
  200. case CMlinear:
  201. setlinear();
  202. break;
  203. case CMps2:
  204. intellimouse = 0;
  205. ps2mouse();
  206. break;
  207. case CMps2intellimouse:
  208. ps2mouse();
  209. setintellimouse();
  210. break;
  211. case CMres:
  212. if(cb->nf >= 2)
  213. setres(atoi(cb->f[1]));
  214. else
  215. setres(1);
  216. break;
  217. case CMreset:
  218. resetmouse();
  219. if(accelerated)
  220. setaccelerated(accelerated);
  221. if(resolution)
  222. setres(resolution);
  223. if(intellimouse)
  224. setintellimouse();
  225. break;
  226. case CMhwaccel:
  227. if(strcmp(cb->f[1], "on")==0)
  228. mousehwaccel = 1;
  229. else if(strcmp(cb->f[1], "off")==0)
  230. mousehwaccel = 0;
  231. else
  232. cmderror(cb, "bad mouse control message");
  233. }
  234. qunlock(&mousectlqlock);
  235. poperror();
  236. }