]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc/mouse.c
merge
[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                         return;
112                 }
113
114         msg[nb] = c;
115         if(++nb == packetsize){
116                 nb = 0;
117                 if(msg[0] & 0x10)
118                         msg[1] |= 0xFF00;
119                 if(msg[0] & 0x20)
120                         msg[2] |= 0xFF00;
121
122                 buttons = b[(msg[0]&7) | (shift ? 8 : 0)];
123                 if(intellimouse && packetsize==4){
124                         if((msg[3]&0xc8) == 0x08){
125                                 /* first byte of 3-byte packet */
126                                 packetsize = 3;
127                                 msg[0] = msg[3];
128                                 nb = 1;
129                                 /* fall through to emit previous packet */
130                         }else{
131                                 /* The AccuPoint on the Toshiba 34[48]0CT
132                                  * encodes extra buttons as 4 and 5. They repeat
133                                  * and don't release, however, so user-level
134                                  * timing code is required. Furthermore,
135                                  * intellimice with 3buttons + scroll give a
136                                  * two's complement number in the lower 4 bits
137                                  * (bit 4 is sign extension) that describes
138                                  * the amount the scroll wheel has moved during
139                                  * the last sample. Here we use only the sign to
140                                  * decide whether the wheel is moving up or down
141                                  * and generate a single button 4 or 5 click
142                                  * accordingly.
143                                  */
144                                 if((msg[3] >> 3) & 1) 
145                                         buttons |= 1<<3;
146                                 else if(msg[3] & 0x7) 
147                                         buttons |= 1<<4;
148                         }
149                 }
150                 dx = msg[1];
151                 dy = -msg[2];
152                 mousetrack(dx, dy, buttons, TK2MS(MACHP(0)->ticks));
153         }
154         return;
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         i8042auxenable(ps2mouseputc);
167         /* make mouse streaming, enabled */
168         i8042auxcmd(0xEA);
169         i8042auxcmd(0xF4);
170
171         mousetype = MousePS2;
172         packetsize = 3;
173         mousehwaccel = 1;
174 }
175
176 /*
177  * The PS/2 Trackpoint multiplexor on the IBM Thinkpad T23 ignores
178  * acceleration commands.  It is supposed to pass them on
179  * to the attached device, but my Logitech mouse is simply
180  * not behaving any differently.  For such devices, we allow
181  * the user to use "hwaccel off" to tell us to back off to
182  * software acceleration even if we're using the PS/2 port.
183  * (Serial mice are always software accelerated.)
184  * For more information on the Thinkpad multiplexor, see
185  * http://wwwcssrv.almaden.ibm.com/trackpoint/
186  */
187 static void
188 setaccelerated(int x)
189 {
190         accelerated = x;
191         if(mousehwaccel){
192                 switch(mousetype){
193                 case MousePS2:
194                         i8042auxcmd(0xE7);
195                         return;
196                 }
197         }
198         mouseaccelerate(x);
199 }
200
201 static void
202 setlinear(void)
203 {
204         accelerated = 0;
205         if(mousehwaccel){
206                 switch(mousetype){
207                 case MousePS2:
208                         i8042auxcmd(0xE6);
209                         return;
210                 }
211         }
212         mouseaccelerate(0);
213 }
214
215 static void
216 setres(int n)
217 {
218         resolution = n;
219         switch(mousetype){
220         case MousePS2:
221                 i8042auxcmd(0xE8);
222                 i8042auxcmd(n);
223                 break;
224         }
225 }
226
227 static void
228 setintellimouse(void)
229 {
230         intellimouse = 1;
231         packetsize = 4;
232         switch(mousetype){
233         case MousePS2:
234                 i8042auxcmd(0xF3);      /* set sample */
235                 i8042auxcmd(0xC8);
236                 i8042auxcmd(0xF3);      /* set sample */
237                 i8042auxcmd(0x64);
238                 i8042auxcmd(0xF3);      /* set sample */
239                 i8042auxcmd(0x50);
240                 break;
241         case Mouseserial:
242                 i8250setmouseputc(mouseport, m5mouseputc);
243                 break;
244         }
245 }
246
247 static void
248 resetmouse(void)
249 {
250         packetsize = 3;
251         switch(mousetype){
252         case MousePS2:
253                 i8042auxcmd(0xF6);
254                 i8042auxcmd(0xEA);      /* streaming */
255                 i8042auxcmd(0xE8);      /* set resolution */
256                 i8042auxcmd(3);
257                 i8042auxcmd(0xF4);      /* enabled */
258                 break;
259         }
260 }
261
262 void
263 mousectl(Cmdbuf *cb)
264 {
265         Cmdtab *ct;
266
267         qlock(&mousectlqlock);
268         if(waserror()){
269                 qunlock(&mousectlqlock);
270                 nexterror();
271         }
272
273         ct = lookupcmd(cb, mousectlmsg, nelem(mousectlmsg));
274         switch(ct->index){
275         case CMaccelerated:
276                 setaccelerated(cb->nf == 1 ? 1 : atoi(cb->f[1]));
277                 break;
278         case CMintellimouse:
279                 setintellimouse();
280                 break;
281         case CMlinear:
282                 setlinear();
283                 break;
284         case CMps2:
285                 intellimouse = 0;
286                 ps2mouse();
287                 break;
288         case CMps2intellimouse:
289                 ps2mouse();
290                 setintellimouse();
291                 break;
292         case CMres:
293                 if(cb->nf >= 2)
294                         setres(atoi(cb->f[1]));
295                 else
296                         setres(1);
297                 break;
298         case CMreset:
299                 resetmouse();
300                 if(accelerated)
301                         setaccelerated(accelerated);
302                 if(resolution)
303                         setres(resolution);
304                 if(intellimouse)
305                         setintellimouse();
306                 break;
307         case CMserial:
308                 if(mousetype == Mouseserial)
309                         error(Emouseset);
310
311                 if(cb->nf > 2){
312                         if(strcmp(cb->f[2], "M") == 0)
313                                 i8250mouse(cb->f[1], m3mouseputc, 0);
314                         else if(strcmp(cb->f[2], "MI") == 0)
315                                 i8250mouse(cb->f[1], m5mouseputc, 0);
316                         else
317                                 i8250mouse(cb->f[1], mouseputc, cb->nf == 1);
318                 } else
319                         i8250mouse(cb->f[1], mouseputc, cb->nf == 1);
320
321                 mousetype = Mouseserial;
322                 strncpy(mouseport, cb->f[1], sizeof(mouseport)-1);
323                 packetsize = 3;
324                 break;
325         case CMhwaccel:
326                 if(strcmp(cb->f[1], "on")==0)
327                         mousehwaccel = 1;
328                 else if(strcmp(cb->f[1], "off")==0)
329                         mousehwaccel = 0;
330                 else
331                         cmderror(cb, "bad mouse control message");
332         }
333
334         qunlock(&mousectlqlock);
335         poperror();
336 }