17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
522eb7cb5Sgd78059 * Common Development and Distribution License (the "License").
622eb7cb5Sgd78059 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*57f4a14aSGarrett D'Amore * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * 3-Byte Mouse Protocol
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/stream.h>
3222eb7cb5Sgd78059 #include <sys/strsun.h>
337c478bd9Sstevel@tonic-gate #include <sys/vuid_event.h>
34*57f4a14aSGarrett D'Amore #include "vuidmice.h"
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #define VUID_BUT(b) BUT((b*2)+1)
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * VUID_BUT(0) BUT(1) LEFT BUTTON
407c478bd9Sstevel@tonic-gate * VUID_BUT(1) BUT(3) RIGHT BUTTON
417c478bd9Sstevel@tonic-gate */
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate #define MOUSE_BUTTON_L (uchar_t)(0x20) /* Left button pressed */
447c478bd9Sstevel@tonic-gate #define MOUSE_BUTTON_R (uchar_t)(0x10) /* Right button pressed */
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #define MOUSE_START_CODE (uchar_t)(0x40) /* Start code in char */
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #define MOUSE_START 0 /* Beginning of packet */
497c478bd9Sstevel@tonic-gate #define MOUSE_BUTTON 1 /* Got button status */
507c478bd9Sstevel@tonic-gate #define MOUSE_DELTA_X 2 /* got delta X */
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate extern void VUID_PUTNEXT(queue_t *const, uchar_t, uchar_t, uchar_t, int);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate int
VUID_OPEN(queue_t * const qp)557c478bd9Sstevel@tonic-gate VUID_OPEN(queue_t *const qp)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate STATEP->nbuttons = 2;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate return (0);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate static void
vuidm3p_sendButtonEvent(queue_t * const qp)637c478bd9Sstevel@tonic-gate vuidm3p_sendButtonEvent(queue_t *const qp)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate int b;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate if ((STATEP->buttons == 0x30) && (!STATEP->oldbuttons)) {
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate * both buttons going down simultaneously means button
707c478bd9Sstevel@tonic-gate * two going down
717c478bd9Sstevel@tonic-gate */
727c478bd9Sstevel@tonic-gate vuidm3p_putnext(qp, (uchar_t)MS_MIDDLE, FE_PAIR_NONE, 0, 1);
737c478bd9Sstevel@tonic-gate return;
747c478bd9Sstevel@tonic-gate } else if ((!STATEP->buttons) && (STATEP->oldbuttons == 0x30)) {
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate * both buttons going up simultaneously means button
777c478bd9Sstevel@tonic-gate * two going up
787c478bd9Sstevel@tonic-gate */
797c478bd9Sstevel@tonic-gate vuidm3p_putnext(qp, (uchar_t)MS_MIDDLE, FE_PAIR_NONE, 0, 0);
807c478bd9Sstevel@tonic-gate return;
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate * for each button, see if it has changed
857c478bd9Sstevel@tonic-gate */
867c478bd9Sstevel@tonic-gate for (b = 0; b < 2; b++) {
877c478bd9Sstevel@tonic-gate uchar_t mask = 0x20 >> b;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate if ((STATEP->buttons & mask) != (STATEP->oldbuttons & mask))
907c478bd9Sstevel@tonic-gate VUID_PUTNEXT(qp, VUID_BUT(b), FE_PAIR_NONE, 0,
917c478bd9Sstevel@tonic-gate (STATEP->buttons & mask ? 1 : 0));
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate void
vuidm3p(queue_t * const qp,mblk_t * mp)967c478bd9Sstevel@tonic-gate vuidm3p(queue_t *const qp, mblk_t *mp)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate int r, code;
997c478bd9Sstevel@tonic-gate uchar_t *bufp;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate bufp = mp->b_rptr;
10222eb7cb5Sgd78059 r = MBLKL(mp);
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate for (r--; r >= 0; r--) {
1057c478bd9Sstevel@tonic-gate code = *bufp++;
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate /* strip the high-order bit (mouse sends 7-bit data) */
1087c478bd9Sstevel@tonic-gate code &= 0x7f;
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate switch (STATEP->state) {
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate * Start state. We stay here if the start code is not
1147c478bd9Sstevel@tonic-gate * received thus forcing us back into sync. When we
1157c478bd9Sstevel@tonic-gate * get a start code the button mask comes with it
1167c478bd9Sstevel@tonic-gate * forcing us to the next state.
1177c478bd9Sstevel@tonic-gate */
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate default:
1207c478bd9Sstevel@tonic-gate case MOUSE_START:
1217c478bd9Sstevel@tonic-gate start_code:
1227c478bd9Sstevel@tonic-gate STATEP->deltax = STATEP->deltay = 0;
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate /* look for sync */
1257c478bd9Sstevel@tonic-gate if ((code & MOUSE_START_CODE) == 0)
1267c478bd9Sstevel@tonic-gate break;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate STATEP->buttons = code & 0x30;
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate if (STATEP->buttons != STATEP->oldbuttons) {
1317c478bd9Sstevel@tonic-gate vuidm3p_sendButtonEvent(qp);
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate * remember state
1347c478bd9Sstevel@tonic-gate */
1357c478bd9Sstevel@tonic-gate STATEP->oldbuttons = STATEP->buttons;
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * bits 0 & 1 are bits 6 & 7 of X value
1407c478bd9Sstevel@tonic-gate * (Sign extend them with the cast.)
1417c478bd9Sstevel@tonic-gate */
1427c478bd9Sstevel@tonic-gate STATEP->deltax = (signed char)((code & 0x03) << 6);
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate * bits 2 & 3 are bits 6 & 7 of Y value
1467c478bd9Sstevel@tonic-gate * (Sign extend them with the cast.)
1477c478bd9Sstevel@tonic-gate */
1487c478bd9Sstevel@tonic-gate STATEP->deltay = (signed char)((code & 0x0c) << 4);
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate * go to the next state
1527c478bd9Sstevel@tonic-gate */
1537c478bd9Sstevel@tonic-gate STATEP->state = MOUSE_BUTTON;
1547c478bd9Sstevel@tonic-gate break;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate * We receive the remaining 6 bits of delta x, forcing
1587c478bd9Sstevel@tonic-gate * us to the next state. We just piece the value of
1597c478bd9Sstevel@tonic-gate * delta x together.
1607c478bd9Sstevel@tonic-gate */
1617c478bd9Sstevel@tonic-gate case MOUSE_BUTTON:
1627c478bd9Sstevel@tonic-gate if (code & MOUSE_START_CODE) {
1637c478bd9Sstevel@tonic-gate STATEP->state = MOUSE_START;
1647c478bd9Sstevel@tonic-gate goto start_code; /* restart */
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate STATEP->deltax |= code & 0x3f;
1687c478bd9Sstevel@tonic-gate STATEP->state = MOUSE_DELTA_X;
1697c478bd9Sstevel@tonic-gate break;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate * The last part of delta Y, and the packet
1737c478bd9Sstevel@tonic-gate * *may be* complete
1747c478bd9Sstevel@tonic-gate */
1757c478bd9Sstevel@tonic-gate case MOUSE_DELTA_X:
1767c478bd9Sstevel@tonic-gate if (code & MOUSE_START_CODE) {
1777c478bd9Sstevel@tonic-gate STATEP->state = MOUSE_START;
1787c478bd9Sstevel@tonic-gate goto start_code; /* restart */
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate STATEP->deltay |= code & 0x3f;
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate * generate motion Event
1857c478bd9Sstevel@tonic-gate */
1867c478bd9Sstevel@tonic-gate if (STATEP->deltax)
1877c478bd9Sstevel@tonic-gate VUID_PUTNEXT(qp, (uchar_t)LOC_X_DELTA,
1887c478bd9Sstevel@tonic-gate FE_PAIR_ABSOLUTE, (uchar_t)LOC_X_ABSOLUTE,
1897c478bd9Sstevel@tonic-gate STATEP->deltax);
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate if (STATEP->deltay)
1927c478bd9Sstevel@tonic-gate VUID_PUTNEXT(qp, (uchar_t)LOC_Y_DELTA,
1937c478bd9Sstevel@tonic-gate FE_PAIR_ABSOLUTE, (uchar_t)LOC_Y_ABSOLUTE,
1947c478bd9Sstevel@tonic-gate -STATEP->deltay);
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate STATEP->deltax = STATEP->deltay = 0;
1977c478bd9Sstevel@tonic-gate break;
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate freemsg(mp);
2027c478bd9Sstevel@tonic-gate }
203