|
|
1.1 root 1: /* $Id: ms-mssystems.c,v 1.2 2003/08/05 03:38:28 fredette Exp $ */
2:
3: /* serial/ms-mssystems.c - MouseSystems serial mouse emulation: */
4:
5: /*
6: * Copyright (c) 2003 Matt Fredette
7: * All rights reserved.
8: *
9: * Redistribution and use in source and binary forms, with or without
10: * modification, are permitted provided that the following conditions
11: * are met:
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: * 2. Redistributions in binary form must reproduce the above copyright
15: * notice, this list of conditions and the following disclaimer in the
16: * documentation and/or other materials provided with the distribution.
17: * 3. All advertising materials mentioning features or use of this software
18: * must display the following acknowledgement:
19: * This product includes software developed by Matt Fredette.
20: * 4. The name of the author may not be used to endorse or promote products
21: * derived from this software without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26: * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33: * POSSIBILITY OF SUCH DAMAGE.
34: */
35:
36: #include <tme/common.h>
37: _TME_RCSID("$Id: ms-mssystems.c,v 1.2 2003/08/05 03:38:28 fredette Exp $");
38:
39: /* includes: */
40: #include "serial-ms.h"
41:
42: /* macros: */
43:
44: /* the buttons: */
45: #define TME_MS_MSSYSTEMS5_BUTTON_2 TME_BIT(0)
46: #define TME_MS_MSSYSTEMS5_BUTTON_1 TME_BIT(1)
47: #define TME_MS_MSSYSTEMS5_BUTTON_0 TME_BIT(2)
48:
49: /* types: */
50:
51: /* this subroutine sets the axis delta field in a packet, given an
52: original axis delta and the offset of the first byte in the packet
53: for the field.
54:
55: this is necessary because the first byte of a packet is the only
56: one allowed to have a value in the range 0x80..0x8f: */
57: static inline void
58: _tme_ms_mssystems5_delta(int delta, tme_uint8_t *d0)
59: {
60:
61: if (delta > 0x7f) {
62: d0[0] = 0x7f;
63: delta -= 0x7f;
64: d0[2] = TME_MIN(delta, 0x7f);
65: }
66:
67: else if (delta <= (char) 0x8f) {
68: d0[0] = 0x90;
69: delta -= (char) 0x90;
70: d0[2] = TME_MAX(delta, (char) 0x90);
71: }
72:
73: else {
74: d0[0] = delta;
75: d0[2] = 0;
76: }
77: }
78:
79: /* this is called to make serial data from mouse events: */
80: static int
81: _tme_ms_mssystems5_events(struct tme_serial_ms *serial_ms,
82: const struct tme_mouse_event *event,
83: unsigned int count)
84: {
85: tme_uint8_t packet[5], byte0;
86: unsigned int buttons;
87:
88: for (; count-- > 0; event++) {
89:
90: /* start the first byte of the packet. this byte has
91: button and packet length information: */
92: byte0 = 0x80;
93:
94: /* add in the buttons. a set bit means a button is released: */
95: byte0 |= (TME_MS_MSSYSTEMS5_BUTTON_0
96: | TME_MS_MSSYSTEMS5_BUTTON_1
97: | TME_MS_MSSYSTEMS5_BUTTON_2);
98: buttons = event->tme_mouse_event_buttons;
99: if (buttons & TME_MOUSE_BUTTON_0) {
100: byte0 ^= TME_MS_MSSYSTEMS5_BUTTON_0;
101: }
102: if (buttons & TME_MOUSE_BUTTON_1) {
103: byte0 ^= TME_MS_MSSYSTEMS5_BUTTON_1;
104: }
105: if (buttons & TME_MOUSE_BUTTON_2) {
106: byte0 ^= TME_MS_MSSYSTEMS5_BUTTON_2;
107: }
108:
109: /* put in the deltas. in the MouseSystems protocol, a negative
110: y delta corresponds to "down" mouse movement: */
111: _tme_ms_mssystems5_delta(event->tme_mouse_event_delta_x,
112: &packet[1]);
113: _tme_ms_mssystems5_delta(-event->tme_mouse_event_delta_y,
114: &packet[2]);
115:
116: /* put in the first byte: */
117: packet[0] = byte0;
118:
119: /* copy the packet into the serial buffer: */
120: tme_serial_buffer_copyin(&serial_ms->tme_serial_ms_serial_buffer,
121: packet,
122: sizeof(packet),
123: TME_SERIAL_DATA_NORMAL,
124: TME_SERIAL_COPY_FULL_IS_OVERRUN);
125: }
126:
127: return (TME_OK);
128: }
129:
130: /* this initializes MouseSystems serial mouse emulation: */
131: int
132: _tme_serial_ms_mssystems5_init(struct tme_serial_ms *serial_ms)
133: {
134: struct tme_serial_config *config;
135:
136: /* set our type-dependent functions: */
137: serial_ms->tme_serial_ms_type_events = _tme_ms_mssystems5_events;
138: serial_ms->tme_serial_ms_type_serial_ctrl = NULL;
139: serial_ms->tme_serial_ms_type_serial_input = NULL;
140:
141: /* set our serial configuration: */
142: config = &serial_ms->tme_serial_ms_type_config;
143: memset(config, 0, sizeof(*config));
144: config->tme_serial_config_baud = 1200;
145: config->tme_serial_config_bits_data = 8;
146: config->tme_serial_config_bits_stop = 1;
147: config->tme_serial_config_parity = TME_SERIAL_PARITY_NONE;
148:
149: return (TME_OK);
150: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.