xref: /inferno-os/os/pc/mouse.c (revision 74a4d8c26dd3c1e9febcb717cfd6cb6512991a7a)
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 /*
10  *  mouse types
11  */
12 enum
13 {
14 	Mouseother=	0,
15 	Mouseserial=	1,
16 	MousePS2=	2,
17 };
18 
19 static int mousetype;
20 
21 /*
22  *  ps/2 mouse message is three bytes
23  *
24  *	byte 0 -	0 0 SDY SDX 1 M R L
25  *	byte 1 -	DX
26  *	byte 2 -	DY
27  *
28  *  shift & left button is the same as middle button
29  */
30 static void
ps2mouseputc(int c,int shift)31 ps2mouseputc(int c, int shift)
32 {
33 	static short msg[3];
34 	static int nb;
35 	static uchar b[] = {0, 1, 4, 5, 2, 3, 6, 7, 0, 1, 2, 5, 2, 3, 6, 7 };
36 	int buttons, dx, dy;
37 
38 	/*
39 	 *  check byte 0 for consistency
40 	 */
41 	if(nb==0 && (c&0xc8)!=0x08)
42 		return;
43 
44 	msg[nb] = c;
45 	if(++nb == 3){
46 		nb = 0;
47 		if(msg[0] & 0x10)
48 			msg[1] |= 0xFF00;
49 		if(msg[0] & 0x20)
50 			msg[2] |= 0xFF00;
51 
52 		buttons = b[(msg[0]&7) | (shift ? 8 : 0)];
53 		dx = msg[1];
54 		dy = -msg[2];
55 		mousetrack(buttons, dx, dy, 1);
56 	}
57 	return;
58 }
59 
60 /*
61  *  set up a ps2 mouse
62  */
63 static void
ps2mouse(void)64 ps2mouse(void)
65 {
66 	if(mousetype == MousePS2)
67 		return;
68 
69 	i8042auxenable(ps2mouseputc);
70 	/* make mouse streaming, enabled */
71 	i8042auxcmd(0xEA);
72 	i8042auxcmd(0xF4);
73 
74 	mousetype = MousePS2;
75 }
76 
77 void
ps2mouselink(void)78 ps2mouselink(void)
79 {
80 	/*
81 	 * hack
82 	 */
83 	ps2mouse();
84 }
85