]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/dev/syscons/sysmouse.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / dev / syscons / sysmouse.c
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_syscons.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/priv.h>
36 #include <sys/serial.h>
37 #include <sys/tty.h>
38 #include <sys/ttydefaults.h>
39 #include <sys/kernel.h>
40 #include <sys/cons.h>
41 #include <sys/consio.h>
42 #include <sys/mouse.h>
43
44 #include <dev/syscons/syscons.h>
45
46 #ifndef SC_NO_SYSMOUSE
47
48 /* local variables */
49 static struct tty       *sysmouse_tty;
50 static int              mouse_level;    /* sysmouse protocol level */
51 static mousestatus_t    mouse_status;
52
53 static void
54 smdev_close(struct tty *tp)
55 {
56         mouse_level = 0;
57 }
58
59 static int
60 smdev_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
61 {
62         mousehw_t *hw;
63         mousemode_t *mode;
64
65         switch (cmd) {
66
67         case MOUSE_GETHWINFO:   /* get device information */
68                 hw = (mousehw_t *)data;
69                 hw->buttons = 10;               /* XXX unknown */
70                 hw->iftype = MOUSE_IF_SYSMOUSE;
71                 hw->type = MOUSE_MOUSE;
72                 hw->model = MOUSE_MODEL_GENERIC;
73                 hw->hwid = 0;
74                 return 0;
75
76         case MOUSE_GETMODE:     /* get protocol/mode */
77                 mode = (mousemode_t *)data;
78                 mode->level = mouse_level;
79                 switch (mode->level) {
80                 case 0: /* emulate MouseSystems protocol */
81                         mode->protocol = MOUSE_PROTO_MSC;
82                         mode->rate = -1;                /* unknown */
83                         mode->resolution = -1;  /* unknown */
84                         mode->accelfactor = 0;  /* disabled */
85                         mode->packetsize = MOUSE_MSC_PACKETSIZE;
86                         mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
87                         mode->syncmask[1] = MOUSE_MSC_SYNC;
88                         break;
89
90                 case 1: /* sysmouse protocol */
91                         mode->protocol = MOUSE_PROTO_SYSMOUSE;
92                         mode->rate = -1;
93                         mode->resolution = -1;
94                         mode->accelfactor = 0;
95                         mode->packetsize = MOUSE_SYS_PACKETSIZE;
96                         mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
97                         mode->syncmask[1] = MOUSE_SYS_SYNC;
98                         break;
99                 }
100                 return 0;
101
102         case MOUSE_SETMODE:     /* set protocol/mode */
103                 mode = (mousemode_t *)data;
104                 if (mode->level == -1)
105                         ;       /* don't change the current setting */
106                 else if ((mode->level < 0) || (mode->level > 1))
107                         return EINVAL;
108                 else
109                         mouse_level = mode->level;
110                 return 0;
111
112         case MOUSE_GETLEVEL:    /* get operation level */
113                 *(int *)data = mouse_level;
114                 return 0;
115
116         case MOUSE_SETLEVEL:    /* set operation level */
117                 if ((*(int *)data  < 0) || (*(int *)data > 1))
118                         return EINVAL;
119                 mouse_level = *(int *)data;
120                 return 0;
121
122         case MOUSE_GETSTATUS:   /* get accumulated mouse events */
123                 *(mousestatus_t *)data = mouse_status;
124                 mouse_status.flags = 0;
125                 mouse_status.obutton = mouse_status.button;
126                 mouse_status.dx = 0;
127                 mouse_status.dy = 0;
128                 mouse_status.dz = 0;
129                 return 0;
130
131 #ifdef notyet
132         case MOUSE_GETVARS:     /* get internal mouse variables */
133         case MOUSE_SETVARS:     /* set internal mouse variables */
134                 return ENODEV;
135 #endif
136
137         case MOUSE_READSTATE:   /* read status from the device */
138         case MOUSE_READDATA:    /* read data from the device */
139                 return ENODEV;
140         }
141
142         return (ENOIOCTL);
143 }
144
145 static int
146 smdev_param(struct tty *tp, struct termios *t)
147 {
148
149         /*
150          * Set the output baud rate to zero. The mouse device supports
151          * no output, so we don't want to waste buffers.
152          */
153         t->c_ispeed = TTYDEF_SPEED;
154         t->c_ospeed = B0;
155
156         return (0);
157 }
158
159 static struct ttydevsw smdev_ttydevsw = {
160         .tsw_flags      = TF_NOPREFIX,
161         .tsw_close      = smdev_close,
162         .tsw_ioctl      = smdev_ioctl,
163         .tsw_param      = smdev_param,
164 };
165
166 static void
167 sm_attach_mouse(void *unused)
168 {
169         if (!vty_enabled(VTY_SC))
170                 return;
171         sysmouse_tty = tty_alloc(&smdev_ttydevsw, NULL);
172         tty_makedev(sysmouse_tty, NULL, "sysmouse");
173 }
174
175 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sm_attach_mouse, NULL);
176
177 int
178 sysmouse_event(mouse_info_t *info)
179 {
180         /* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
181         static int butmap[8] = {
182             MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
183             MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
184             MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
185             MOUSE_MSC_BUTTON3UP,
186             MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
187             MOUSE_MSC_BUTTON2UP,
188             MOUSE_MSC_BUTTON1UP,
189             0,
190         };
191         u_char buf[8];
192         int x, y, z;
193         int i, flags = 0;
194
195         tty_lock(sysmouse_tty);
196
197         switch (info->operation) {
198         case MOUSE_ACTION:
199                 mouse_status.button = info->u.data.buttons;
200                 /* FALL THROUGH */
201         case MOUSE_MOTION_EVENT:
202                 x = info->u.data.x;
203                 y = info->u.data.y;
204                 z = info->u.data.z;
205                 break;
206         case MOUSE_BUTTON_EVENT:
207                 x = y = z = 0;
208                 if (info->u.event.value > 0)
209                         mouse_status.button |= info->u.event.id;
210                 else
211                         mouse_status.button &= ~info->u.event.id;
212                 break;
213         default:
214                 goto done;
215         }
216
217         mouse_status.dx += x;
218         mouse_status.dy += y;
219         mouse_status.dz += z;
220         mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0)
221                               | (mouse_status.obutton ^ mouse_status.button);
222         flags = mouse_status.flags;
223         if (flags == 0 || !tty_opened(sysmouse_tty))
224                 goto done;
225
226         /* the first five bytes are compatible with MouseSystems' */
227         buf[0] = MOUSE_MSC_SYNC
228                  | butmap[mouse_status.button & MOUSE_STDBUTTONS];
229         x = imax(imin(x, 255), -256);
230         buf[1] = x >> 1;
231         buf[3] = x - buf[1];
232         y = -imax(imin(y, 255), -256);
233         buf[2] = y >> 1;
234         buf[4] = y - buf[2];
235         for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i)
236                 ttydisc_rint(sysmouse_tty, buf[i], 0);
237         if (mouse_level >= 1) {
238                 /* extended part */
239                 z = imax(imin(z, 127), -128);
240                 buf[5] = (z >> 1) & 0x7f;
241                 buf[6] = (z - (z >> 1)) & 0x7f;
242                 /* buttons 4-10 */
243                 buf[7] = (~mouse_status.button >> 3) & 0x7f;
244                 for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i)
245                         ttydisc_rint(sysmouse_tty, buf[i], 0);
246         }
247         ttydisc_rint_done(sysmouse_tty);
248
249 done:   tty_unlock(sysmouse_tty);
250         return (flags);
251 }
252
253 #endif /* !SC_NO_SYSMOUSE */