]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc/mouse.c
pc, pc64: more conservative pcirouting
[plan9front.git] / sys / src / 9 / pc / mouse.c
1 #include "u.h"
2 #include "../port/lib.h"
3 #include "mem.h"
4 #include "dat.h"
5 #include "fns.h"
6 #include "../port/error.h"
7 #include "io.h"
8
9 #define Image   IMAGE
10 #include <draw.h>
11 #include <memdraw.h>
12 #include <cursor.h>
13 #include "screen.h"
14
15 /*
16  *  mouse types
17  */
18 enum
19 {
20         Mouseother=     0,
21         Mouseserial=    1,
22         MousePS2=       2,
23 };
24
25 static QLock mousectlqlock;
26 static int mousetype;
27 static int intellimouse;
28 static int packetsize;
29 static int resolution;
30 static int accelerated;
31 static int mousehwaccel;
32 static char mouseport[5];
33
34 enum
35 {
36         CMaccelerated,
37         CMhwaccel,
38         CMintellimouse,
39         CMlinear,
40         CMps2,
41         CMps2intellimouse,
42         CMres,
43         CMreset,
44         CMserial,
45 };
46
47 static Cmdtab mousectlmsg[] =
48 {
49         CMaccelerated,          "accelerated",          0,
50         CMhwaccel,              "hwaccel",              2,
51         CMintellimouse,         "intellimouse",         1,
52         CMlinear,               "linear",               1,
53         CMps2,                  "ps2",                  1,
54         CMps2intellimouse,      "ps2intellimouse",      1,
55         CMres,                  "res",                  0,
56         CMreset,                "reset",                1,
57         CMserial,               "serial",               0,
58 };
59
60 /*
61  *  ps/2 mouse message is three bytes
62  *
63  *      byte 0 -        0 0 SDY SDX 1 M R L
64  *      byte 1 -        DX
65  *      byte 2 -        DY
66  *
67  *  shift & right button is the same as middle button
68  *
69  * Intellimouse and AccuPoint with extra buttons deliver
70  *      byte 3 -        00 or 01 or FF according to extra button state.
71  * extra buttons are mapped in this code to buttons 4 and 5.
72  * AccuPoint generates repeated events for these buttons;
73 *  it and Intellimouse generate 'down' events only, so
74  * user-level code is required to generate button 'up' events
75  * if they are needed by the application.
76  * Also on laptops with AccuPoint AND external mouse, the
77  * controller may deliver 3 or 4 bytes according to the type
78  * of the external mouse; code must adapt.
79  *
80  * On the NEC Versa series (and perhaps others?) we seem to
81  * lose a byte from the packet every once in a while, which
82  * means we lose where we are in the instruction stream.
83  * To resynchronize, if we get a byte more than two seconds
84  * after the previous byte, we assume it's the first in a packet.
85  */
86 static void
87 ps2mouseputc(int c, int shift)
88 {
89         static short msg[4];
90         static int nb;
91         static uchar b[] = {0, 1, 4, 5, 2, 3, 6, 7, 0, 1, 2, 3, 2, 3, 6, 7 };
92         static ulong lasttick;
93         ulong m;
94         int buttons, dx, dy;
95
96         /*
97          * Resynchronize in stream with timing; see comment above.
98          */
99         m = MACHP(0)->ticks;
100         if(TK2SEC(m - lasttick) > 2)
101                 nb = 0;
102         lasttick = m;
103
104         /* 
105          *  check byte 0 for consistency
106          */
107         if(nb==0 && (c&0xc8)!=0x08){
108                 if(intellimouse && (c==0x00 || c==0x01 || c==0xFF)){
109                         /* last byte of 4-byte packet */
110                         packetsize = 4;
111                 }
112                 return;
113         }
114
115         msg[nb] = c;
116         if(++nb >= packetsize){
117                 nb = 0;
118                 if(msg[0] & 0x10)
119                         msg[1] |= 0xFF00;
120                 if(msg[0] & 0x20)
121                         msg[2] |= 0xFF00;
122
123                 buttons = b[(msg[0]&7) | (shift ? 8 : 0)];
124                 if(intellimouse && packetsize==4){
125                         if((msg[3]&0xc8) == 0x08){
126                                 /* first byte of 3-byte packet */
127                                 packetsize = 3;
128                                 msg[0] = msg[3];
129                                 nb = 1;
130                                 /* fall through to emit previous packet */
131                         }else{
132                                 /* The AccuPoint on the Toshiba 34[48]0CT
133                                  * encodes extra buttons as 4 and 5. They repeat
134                                  * and don't release, however, so user-level
135                                  * timing code is required. Furthermore,
136                                  * intellimice with 3buttons + scroll give a
137                                  * two's complement number in the lower 4 bits
138                                  * (bit 4 is sign extension) that describes
139                                  * the amount the scroll wheel has moved during
140                                  * the last sample. Here we use only the sign to
141                                  * decide whether the wheel is moving up or down
142                                  * and generate a single button 4 or 5 click
143                                  * accordingly.
144                                  */
145                                 if((msg[3] >> 3) & 1) 
146                                         buttons |= 1<<3;
147                                 else if(msg[3] & 0x7) 
148                                         buttons |= 1<<4;
149                         }
150                 }
151                 dx = msg[1];
152                 dy = -msg[2];
153                 mousetrack(dx, dy, buttons, TK2MS(MACHP(0)->ticks));
154         }
155 }
156
157 /*
158  *  set up a ps2 mouse
159  */
160 static void
161 ps2mouse(void)
162 {
163         if(mousetype == MousePS2)
164                 return;
165
166         mousetype = MousePS2;
167         packetsize = 3;
168         mousehwaccel = 0;
169
170         i8042auxenable(ps2mouseputc);
171         i8042auxcmd(0xEA);      /* set stream mode */
172 }
173
174 /*
175  * The PS/2 Trackpoint multiplexor on the IBM Thinkpad T23 ignores
176  * acceleration commands.  It is supposed to pass them on
177  * to the attached device, but my Logitech mouse is simply
178  * not behaving any differently.  For such devices, we allow
179  * the user to use "hwaccel off" to tell us to back off to
180  * software acceleration even if we're using the PS/2 port.
181  * (Serial mice are always software accelerated.)
182  * For more information on the Thinkpad multiplexor, see
183  * http://wwwcssrv.almaden.ibm.com/trackpoint/
184  */
185 static void
186 setaccelerated(int x)
187 {
188         accelerated = x;
189         if(mousehwaccel){
190                 switch(mousetype){
191                 case MousePS2:
192                         i8042auxcmd(0xE7);
193                         return;
194                 }
195         }
196         mouseaccelerate(x);
197 }
198
199 static void
200 setlinear(void)
201 {
202         accelerated = 0;
203         if(mousehwaccel){
204                 switch(mousetype){
205                 case MousePS2:
206                         i8042auxcmd(0xE6);
207                         return;
208                 }
209         }
210         mouseaccelerate(0);
211 }
212
213 static void
214 setres(int n)
215 {
216         resolution = n;
217         switch(mousetype){
218         case MousePS2:
219                 i8042auxcmd(0xE8);
220                 i8042auxcmd(n);
221                 break;
222         }
223 }
224
225 static void
226 setintellimouse(void)
227 {
228         intellimouse = 1;
229         packetsize = 4;
230         switch(mousetype){
231         case MousePS2:
232                 i8042auxcmd(0xF3);      /* set sample */
233                 i8042auxcmd(0xC8);
234                 i8042auxcmd(0xF3);      /* set sample */
235                 i8042auxcmd(0x64);
236                 i8042auxcmd(0xF3);      /* set sample */
237                 i8042auxcmd(0x50);
238                 break;
239         case Mouseserial:
240                 uartsetmouseputc(mouseport, m5mouseputc);
241                 break;
242         }
243 }
244
245 static void
246 resetmouse(void)
247 {
248         packetsize = 3;
249         switch(mousetype){
250         case MousePS2:
251                 i8042auxcmd(0xF6);
252                 i8042auxcmd(0xEA);      /* streaming */
253                 i8042auxcmd(0xE8);      /* set resolution */
254                 i8042auxcmd(3);
255                 break;
256         }
257 }
258
259 static void
260 setstream(int on)
261 {
262         int i;
263
264         switch(mousetype){
265         case MousePS2:
266                 /*
267                  * disabling streaming can fail when
268                  * a packet is currently transmitted.
269                  */
270                 for(i=0; i<4; i++){
271                         if(i8042auxcmd(on ? 0xF4 : 0xF5) != -1)
272                                 break;
273                         delay(50);
274                 }
275                 break;
276         }
277 }
278
279 void
280 mousectl(Cmdbuf *cb)
281 {
282         Cmdtab *ct;
283
284         qlock(&mousectlqlock);
285         if(waserror()){
286                 qunlock(&mousectlqlock);
287                 nexterror();
288         }
289
290         ct = lookupcmd(cb, mousectlmsg, nelem(mousectlmsg));
291         switch(ct->index){
292         case CMaccelerated:
293                 setstream(0);
294                 setaccelerated(cb->nf == 1 ? 1 : atoi(cb->f[1]));
295                 setstream(1);
296                 break;
297         case CMintellimouse:
298                 setstream(0);
299                 setintellimouse();
300                 setstream(1);
301                 break;
302         case CMlinear:
303                 setstream(0);
304                 setlinear();
305                 setstream(1);
306                 break;
307         case CMps2:
308                 intellimouse = 0;
309                 ps2mouse();
310                 setstream(1);
311                 break;
312         case CMps2intellimouse:
313                 ps2mouse();
314                 setintellimouse();
315                 setstream(1);
316                 break;
317         case CMres:
318                 setstream(0);
319                 if(cb->nf >= 2)
320                         setres(atoi(cb->f[1]));
321                 else
322                         setres(1);
323                 setstream(1);
324                 break;
325         case CMreset:
326                 resetmouse();
327                 if(accelerated)
328                         setaccelerated(accelerated);
329                 if(resolution)
330                         setres(resolution);
331                 if(intellimouse)
332                         setintellimouse();
333                 setstream(1);
334                 break;
335         case CMserial:
336                 if(mousetype == Mouseserial)
337                         error(Emouseset);
338
339                 if(cb->nf > 2){
340                         if(strcmp(cb->f[2], "M") == 0)
341                                 uartmouse(cb->f[1], m3mouseputc, 0);
342                         else if(strcmp(cb->f[2], "MI") == 0)
343                                 uartmouse(cb->f[1], m5mouseputc, 0);
344                         else
345                                 uartmouse(cb->f[1], mouseputc, cb->nf == 1);
346                 } else
347                         uartmouse(cb->f[1], mouseputc, cb->nf == 1);
348
349                 mousetype = Mouseserial;
350                 strncpy(mouseport, cb->f[1], sizeof(mouseport)-1);
351                 mouseport[sizeof(mouseport)-1] = 0;
352                 packetsize = 3;
353                 break;
354         case CMhwaccel:
355                 if(strcmp(cb->f[1], "on")==0)
356                         mousehwaccel = 1;
357                 else if(strcmp(cb->f[1], "off")==0)
358                         mousehwaccel = 0;
359                 else
360                         cmderror(cb, "bad mouse control message");
361         }
362
363         qunlock(&mousectlqlock);
364         poperror();
365 }