1 | /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
|
---|
2 |
|
---|
3 | This software may be distributed and modified under the terms of the GNU
|
---|
4 | General Public License version 2 (GPL2) as published by the Free Software
|
---|
5 | Foundation and appearing in the file GPL2.TXT included in the packaging of
|
---|
6 | this file. Please note that GPL2 Section 2[b] requires that all works based
|
---|
7 | on this software must also be made publicly available under the terms of
|
---|
8 | the GPL2 ("Copyleft").
|
---|
9 |
|
---|
10 | Contact information
|
---|
11 | -------------------
|
---|
12 |
|
---|
13 | Circuits At Home, LTD
|
---|
14 | Web : http://www.circuitsathome.com
|
---|
15 | e-mail : support@circuitsathome.com
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "hidboot.h"
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * \brief Parse HID mouse report.
|
---|
22 | *
|
---|
23 | * \param hid HID device pointer.
|
---|
24 | * \param is_rpt_id True if this is a report ID.
|
---|
25 | * \param len Buffer length.
|
---|
26 | * \param buf Buffer containing report data.
|
---|
27 | */
|
---|
28 | void MouseReportParser::Parse(HID *hid, bool is_rpt_id, uint32_t len, uint8_t *buf)
|
---|
29 | {
|
---|
30 | MOUSEINFO *pmi = (MOUSEINFO*)buf;
|
---|
31 |
|
---|
32 | if (prevState.mouseInfo.bmLeftButton == 0 && pmi->bmLeftButton == 1)
|
---|
33 | OnLeftButtonDown(pmi);
|
---|
34 |
|
---|
35 | if (prevState.mouseInfo.bmLeftButton == 1 && pmi->bmLeftButton == 0)
|
---|
36 | OnLeftButtonUp(pmi);
|
---|
37 |
|
---|
38 | if (prevState.mouseInfo.bmRightButton == 0 && pmi->bmRightButton == 1)
|
---|
39 | OnRightButtonDown(pmi);
|
---|
40 |
|
---|
41 | if (prevState.mouseInfo.bmRightButton == 1 && pmi->bmRightButton == 0)
|
---|
42 | OnRightButtonUp(pmi);
|
---|
43 |
|
---|
44 | if (prevState.mouseInfo.bmMiddleButton == 0 && pmi->bmMiddleButton == 1)
|
---|
45 | OnMiddleButtonDown(pmi);
|
---|
46 |
|
---|
47 | if (prevState.mouseInfo.bmMiddleButton == 1 && pmi->bmMiddleButton == 0)
|
---|
48 | OnMiddleButtonUp(pmi);
|
---|
49 |
|
---|
50 | if (prevState.mouseInfo.dX != pmi->dX || prevState.mouseInfo.dY != pmi->dY)
|
---|
51 | OnMouseMove(pmi);
|
---|
52 |
|
---|
53 | for (uint32_t i = 0; i < 3; ++i)
|
---|
54 | prevState.bInfo[i] = buf[i];
|
---|
55 | };
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * \brief Parse HID keyboard report.
|
---|
59 | *
|
---|
60 | * \param hid HID device pointer.
|
---|
61 | * \param is_rpt_id True if this is a report ID.
|
---|
62 | * \param len Buffer length.
|
---|
63 | * \param buf Buffer containing report data.
|
---|
64 | */
|
---|
65 | void KeyboardReportParser::Parse(HID *hid, bool is_rpt_id, uint32_t len, uint8_t *buf)
|
---|
66 | {
|
---|
67 | // On error - return
|
---|
68 | if (buf[2] == 1)
|
---|
69 | return;
|
---|
70 |
|
---|
71 | //KBDINFO *pki = (KBDINFO*)buf;
|
---|
72 |
|
---|
73 | for (uint32_t i = 2; i < 8; ++i)
|
---|
74 | {
|
---|
75 | bool down = false;
|
---|
76 | bool up = false;
|
---|
77 |
|
---|
78 | for (uint8_t j=2; j<8; j++)
|
---|
79 | {
|
---|
80 | if (buf[i] == prevState.bInfo[j] && buf[i] != 1)
|
---|
81 | down = true;
|
---|
82 | if (buf[j] == prevState.bInfo[i] && prevState.bInfo[i] != 1)
|
---|
83 | up = true;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (!down)
|
---|
87 | {
|
---|
88 | HandleLockingKeys(hid, buf[i]);
|
---|
89 | OnKeyDown(*buf, buf[i]);
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (!up)
|
---|
93 | OnKeyUp(prevState.bInfo[0], prevState.bInfo[i]);
|
---|
94 | }
|
---|
95 |
|
---|
96 | for (uint32_t i = 0; i < 8; ++i)
|
---|
97 | prevState.bInfo[i] = buf[i];
|
---|
98 | };
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * \brief Handle keyboard locking keys and manage keyboard leds using USB
|
---|
102 | * report.
|
---|
103 | *
|
---|
104 | * \param hid HID device pointer.
|
---|
105 | * \param key Locking key.
|
---|
106 | *
|
---|
107 | * \return 0 on success, error code otherwise.
|
---|
108 | */
|
---|
109 | uint8_t KeyboardReportParser::HandleLockingKeys(HID *hid, uint8_t key)
|
---|
110 | {
|
---|
111 | uint8_t old_keys = kbdLockingKeys.bLeds;
|
---|
112 |
|
---|
113 | switch (key)
|
---|
114 | {
|
---|
115 | case KEY_NUM_LOCK:
|
---|
116 | kbdLockingKeys.kbdLeds.bmNumLock = ~kbdLockingKeys.kbdLeds.bmNumLock;
|
---|
117 | break;
|
---|
118 | case KEY_CAPS_LOCK:
|
---|
119 | kbdLockingKeys.kbdLeds.bmCapsLock = ~kbdLockingKeys.kbdLeds.bmCapsLock;
|
---|
120 | break;
|
---|
121 | case KEY_SCROLL_LOCK:
|
---|
122 | kbdLockingKeys.kbdLeds.bmScrollLock = ~kbdLockingKeys.kbdLeds.bmScrollLock;
|
---|
123 | break;
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (old_keys != kbdLockingKeys.bLeds && hid)
|
---|
127 | return (hid->SetReport(0, 0/*hid->GetIface()*/, 2, 0, 1, &kbdLockingKeys.bLeds));
|
---|
128 |
|
---|
129 | return 0;
|
---|
130 | }
|
---|
131 |
|
---|
132 | const uint8_t KeyboardReportParser::numKeys[] = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')' };
|
---|
133 | const uint8_t KeyboardReportParser::symKeysUp[] = { '_', '+', '{', '}', '|', '~', ':', '"', '~', '<', '>', '?' };
|
---|
134 | const uint8_t KeyboardReportParser::symKeysLo[] = { '-', '=', '[', ']', '\\', ' ', ';', '\'', '`', ',', '.', '/' };
|
---|
135 | const uint8_t KeyboardReportParser::padKeys[] = { '/', '*', '-', '+', 0x13 };
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * \brief Manage keyboard OEM to ASCII conversion.
|
---|
139 | *
|
---|
140 | * \param mod Keyboard modifier.
|
---|
141 | * \param key Key value to convert.
|
---|
142 | *
|
---|
143 | * \return Keyboard corresponding ASCII value on success, 0 otherwise.
|
---|
144 | */
|
---|
145 | uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key)
|
---|
146 | {
|
---|
147 | uint8_t shift = (mod & 0x22);
|
---|
148 |
|
---|
149 | // [a-z]
|
---|
150 | if (key > 0x03 && key < 0x1e)
|
---|
151 | {
|
---|
152 | // Upper case letters
|
---|
153 | if ( (kbdLockingKeys.kbdLeds.bmCapsLock == 0 && (mod & 2)) ||
|
---|
154 | (kbdLockingKeys.kbdLeds.bmCapsLock == 1 && (mod & 2) == 0) )
|
---|
155 | return (key - 4 + 'A');
|
---|
156 |
|
---|
157 | // Lower case letters
|
---|
158 | else
|
---|
159 | return (key - 4 + 'a');
|
---|
160 | }
|
---|
161 | // Numbers
|
---|
162 | else if (key > 0x1d && key < 0x27)
|
---|
163 | {
|
---|
164 | if (shift)
|
---|
165 | return (numKeys[key - 0x1e]);
|
---|
166 | else
|
---|
167 | return (key - 0x1e + '1');
|
---|
168 | }
|
---|
169 | // Keypad Numbers
|
---|
170 | else if (key > 0x58 && key < 0x62)
|
---|
171 | {
|
---|
172 | if (kbdLockingKeys.kbdLeds.bmNumLock == 1)
|
---|
173 | return (key - 0x59 + '1');
|
---|
174 | }
|
---|
175 | else if (key > 0x2c && key < 0x39)
|
---|
176 | return ((shift) ? symKeysUp[key-0x2d] : symKeysLo[key-0x2d]);
|
---|
177 | else if (key > 0x53 && key < 0x59)
|
---|
178 | return padKeys[key - 0x54];
|
---|
179 | else
|
---|
180 | {
|
---|
181 | switch (key)
|
---|
182 | {
|
---|
183 | case KEY_SPACE: return (0x20);
|
---|
184 | case KEY_ENTER: return (0x13);
|
---|
185 | case KEY_ZERO: return ((shift) ? ')' : '0');
|
---|
186 | case KEY_ZERO2: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '0' : 0);
|
---|
187 | case KEY_PERIOD: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '.' : 0);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | return 0;
|
---|
192 | }
|
---|