]>
Commit | Line | Data |
---|---|---|
2e4d9fb1 AJ |
1 | /* |
2 | * QEMU Baum Braille Device | |
3 | * | |
0fb7c882 | 4 | * Copyright (c) 2008, 2010-2011, 2016 Samuel Thibault |
2e4d9fb1 AJ |
5 | * |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
9c058332 | 24 | #include "qemu/osdep.h" |
da34e65c | 25 | #include "qapi/error.h" |
2e4d9fb1 | 26 | #include "qemu-common.h" |
dccfcd0e | 27 | #include "sysemu/char.h" |
1de7afc9 | 28 | #include "qemu/timer.h" |
159b6e9f | 29 | #include "hw/usb.h" |
f29b3431 | 30 | #include "ui/console.h" |
2e4d9fb1 AJ |
31 | #include <brlapi.h> |
32 | #include <brlapi_constants.h> | |
33 | #include <brlapi_keycodes.h> | |
2e4d9fb1 AJ |
34 | |
35 | #if 0 | |
36 | #define DPRINTF(fmt, ...) \ | |
37 | printf(fmt, ## __VA_ARGS__) | |
38 | #else | |
39 | #define DPRINTF(fmt, ...) | |
40 | #endif | |
41 | ||
42 | #define ESC 0x1B | |
43 | ||
44 | #define BAUM_REQ_DisplayData 0x01 | |
45 | #define BAUM_REQ_GetVersionNumber 0x05 | |
46 | #define BAUM_REQ_GetKeys 0x08 | |
47 | #define BAUM_REQ_SetMode 0x12 | |
48 | #define BAUM_REQ_SetProtocol 0x15 | |
49 | #define BAUM_REQ_GetDeviceIdentity 0x84 | |
50 | #define BAUM_REQ_GetSerialNumber 0x8A | |
51 | ||
52 | #define BAUM_RSP_CellCount 0x01 | |
53 | #define BAUM_RSP_VersionNumber 0x05 | |
54 | #define BAUM_RSP_ModeSetting 0x11 | |
55 | #define BAUM_RSP_CommunicationChannel 0x16 | |
56 | #define BAUM_RSP_PowerdownSignal 0x17 | |
57 | #define BAUM_RSP_HorizontalSensors 0x20 | |
58 | #define BAUM_RSP_VerticalSensors 0x21 | |
59 | #define BAUM_RSP_RoutingKeys 0x22 | |
60 | #define BAUM_RSP_Switches 0x23 | |
61 | #define BAUM_RSP_TopKeys 0x24 | |
62 | #define BAUM_RSP_HorizontalSensor 0x25 | |
63 | #define BAUM_RSP_VerticalSensor 0x26 | |
64 | #define BAUM_RSP_RoutingKey 0x27 | |
65 | #define BAUM_RSP_FrontKeys6 0x28 | |
66 | #define BAUM_RSP_BackKeys6 0x29 | |
67 | #define BAUM_RSP_CommandKeys 0x2B | |
68 | #define BAUM_RSP_FrontKeys10 0x2C | |
69 | #define BAUM_RSP_BackKeys10 0x2D | |
70 | #define BAUM_RSP_EntryKeys 0x33 | |
71 | #define BAUM_RSP_JoyStick 0x34 | |
72 | #define BAUM_RSP_ErrorCode 0x40 | |
73 | #define BAUM_RSP_InfoBlock 0x42 | |
74 | #define BAUM_RSP_DeviceIdentity 0x84 | |
75 | #define BAUM_RSP_SerialNumber 0x8A | |
76 | #define BAUM_RSP_BluetoothName 0x8C | |
77 | ||
78 | #define BAUM_TL1 0x01 | |
79 | #define BAUM_TL2 0x02 | |
80 | #define BAUM_TL3 0x04 | |
81 | #define BAUM_TR1 0x08 | |
82 | #define BAUM_TR2 0x10 | |
83 | #define BAUM_TR3 0x20 | |
84 | ||
85 | #define BUF_SIZE 256 | |
86 | ||
87 | typedef struct { | |
41ac54b2 | 88 | CharDriverState parent; |
2e4d9fb1 AJ |
89 | |
90 | brlapi_handle_t *brlapi; | |
91 | int brlapi_fd; | |
4f72c4dd | 92 | unsigned int x, y; |
cb78d4a1 | 93 | bool deferred_init; |
2e4d9fb1 AJ |
94 | |
95 | uint8_t in_buf[BUF_SIZE]; | |
96 | uint8_t in_buf_used; | |
97 | uint8_t out_buf[BUF_SIZE]; | |
98 | uint8_t out_buf_used, out_buf_ptr; | |
99 | ||
100 | QEMUTimer *cellCount_timer; | |
101 | } BaumDriverState; | |
102 | ||
103 | /* Let's assume NABCC by default */ | |
0fb7c882 ST |
104 | enum way { |
105 | DOTS2ASCII, | |
106 | ASCII2DOTS | |
107 | }; | |
108 | static const uint8_t nabcc_translation[2][256] = { | |
2e4d9fb1 AJ |
109 | #ifndef BRLAPI_DOTS |
110 | #define BRLAPI_DOTS(d1,d2,d3,d4,d5,d6,d7,d8) \ | |
111 | ((d1?BRLAPI_DOT1:0)|\ | |
112 | (d2?BRLAPI_DOT2:0)|\ | |
113 | (d3?BRLAPI_DOT3:0)|\ | |
114 | (d4?BRLAPI_DOT4:0)|\ | |
115 | (d5?BRLAPI_DOT5:0)|\ | |
116 | (d6?BRLAPI_DOT6:0)|\ | |
117 | (d7?BRLAPI_DOT7:0)|\ | |
118 | (d8?BRLAPI_DOT8:0)) | |
119 | #endif | |
0fb7c882 ST |
120 | #define DO(dots, ascii) \ |
121 | [DOTS2ASCII][dots] = ascii, \ | |
122 | [ASCII2DOTS][ascii] = dots | |
123 | DO(0, ' '), | |
124 | DO(BRLAPI_DOTS(1, 0, 0, 0, 0, 0, 0, 0), 'a'), | |
125 | DO(BRLAPI_DOTS(1, 1, 0, 0, 0, 0, 0, 0), 'b'), | |
126 | DO(BRLAPI_DOTS(1, 0, 0, 1, 0, 0, 0, 0), 'c'), | |
127 | DO(BRLAPI_DOTS(1, 0, 0, 1, 1, 0, 0, 0), 'd'), | |
128 | DO(BRLAPI_DOTS(1, 0, 0, 0, 1, 0, 0, 0), 'e'), | |
129 | DO(BRLAPI_DOTS(1, 1, 0, 1, 0, 0, 0, 0), 'f'), | |
130 | DO(BRLAPI_DOTS(1, 1, 0, 1, 1, 0, 0, 0), 'g'), | |
131 | DO(BRLAPI_DOTS(1, 1, 0, 0, 1, 0, 0, 0), 'h'), | |
132 | DO(BRLAPI_DOTS(0, 1, 0, 1, 0, 0, 0, 0), 'i'), | |
133 | DO(BRLAPI_DOTS(0, 1, 0, 1, 1, 0, 0, 0), 'j'), | |
134 | DO(BRLAPI_DOTS(1, 0, 1, 0, 0, 0, 0, 0), 'k'), | |
135 | DO(BRLAPI_DOTS(1, 1, 1, 0, 0, 0, 0, 0), 'l'), | |
136 | DO(BRLAPI_DOTS(1, 0, 1, 1, 0, 0, 0, 0), 'm'), | |
137 | DO(BRLAPI_DOTS(1, 0, 1, 1, 1, 0, 0, 0), 'n'), | |
138 | DO(BRLAPI_DOTS(1, 0, 1, 0, 1, 0, 0, 0), 'o'), | |
139 | DO(BRLAPI_DOTS(1, 1, 1, 1, 0, 0, 0, 0), 'p'), | |
140 | DO(BRLAPI_DOTS(1, 1, 1, 1, 1, 0, 0, 0), 'q'), | |
141 | DO(BRLAPI_DOTS(1, 1, 1, 0, 1, 0, 0, 0), 'r'), | |
142 | DO(BRLAPI_DOTS(0, 1, 1, 1, 0, 0, 0, 0), 's'), | |
143 | DO(BRLAPI_DOTS(0, 1, 1, 1, 1, 0, 0, 0), 't'), | |
144 | DO(BRLAPI_DOTS(1, 0, 1, 0, 0, 1, 0, 0), 'u'), | |
145 | DO(BRLAPI_DOTS(1, 1, 1, 0, 0, 1, 0, 0), 'v'), | |
146 | DO(BRLAPI_DOTS(0, 1, 0, 1, 1, 1, 0, 0), 'w'), | |
147 | DO(BRLAPI_DOTS(1, 0, 1, 1, 0, 1, 0, 0), 'x'), | |
148 | DO(BRLAPI_DOTS(1, 0, 1, 1, 1, 1, 0, 0), 'y'), | |
149 | DO(BRLAPI_DOTS(1, 0, 1, 0, 1, 1, 0, 0), 'z'), | |
150 | ||
151 | DO(BRLAPI_DOTS(1, 0, 0, 0, 0, 0, 1, 0), 'A'), | |
152 | DO(BRLAPI_DOTS(1, 1, 0, 0, 0, 0, 1, 0), 'B'), | |
153 | DO(BRLAPI_DOTS(1, 0, 0, 1, 0, 0, 1, 0), 'C'), | |
154 | DO(BRLAPI_DOTS(1, 0, 0, 1, 1, 0, 1, 0), 'D'), | |
155 | DO(BRLAPI_DOTS(1, 0, 0, 0, 1, 0, 1, 0), 'E'), | |
156 | DO(BRLAPI_DOTS(1, 1, 0, 1, 0, 0, 1, 0), 'F'), | |
157 | DO(BRLAPI_DOTS(1, 1, 0, 1, 1, 0, 1, 0), 'G'), | |
158 | DO(BRLAPI_DOTS(1, 1, 0, 0, 1, 0, 1, 0), 'H'), | |
159 | DO(BRLAPI_DOTS(0, 1, 0, 1, 0, 0, 1, 0), 'I'), | |
160 | DO(BRLAPI_DOTS(0, 1, 0, 1, 1, 0, 1, 0), 'J'), | |
161 | DO(BRLAPI_DOTS(1, 0, 1, 0, 0, 0, 1, 0), 'K'), | |
162 | DO(BRLAPI_DOTS(1, 1, 1, 0, 0, 0, 1, 0), 'L'), | |
163 | DO(BRLAPI_DOTS(1, 0, 1, 1, 0, 0, 1, 0), 'M'), | |
164 | DO(BRLAPI_DOTS(1, 0, 1, 1, 1, 0, 1, 0), 'N'), | |
165 | DO(BRLAPI_DOTS(1, 0, 1, 0, 1, 0, 1, 0), 'O'), | |
166 | DO(BRLAPI_DOTS(1, 1, 1, 1, 0, 0, 1, 0), 'P'), | |
167 | DO(BRLAPI_DOTS(1, 1, 1, 1, 1, 0, 1, 0), 'Q'), | |
168 | DO(BRLAPI_DOTS(1, 1, 1, 0, 1, 0, 1, 0), 'R'), | |
169 | DO(BRLAPI_DOTS(0, 1, 1, 1, 0, 0, 1, 0), 'S'), | |
170 | DO(BRLAPI_DOTS(0, 1, 1, 1, 1, 0, 1, 0), 'T'), | |
171 | DO(BRLAPI_DOTS(1, 0, 1, 0, 0, 1, 1, 0), 'U'), | |
172 | DO(BRLAPI_DOTS(1, 1, 1, 0, 0, 1, 1, 0), 'V'), | |
173 | DO(BRLAPI_DOTS(0, 1, 0, 1, 1, 1, 1, 0), 'W'), | |
174 | DO(BRLAPI_DOTS(1, 0, 1, 1, 0, 1, 1, 0), 'X'), | |
175 | DO(BRLAPI_DOTS(1, 0, 1, 1, 1, 1, 1, 0), 'Y'), | |
176 | DO(BRLAPI_DOTS(1, 0, 1, 0, 1, 1, 1, 0), 'Z'), | |
177 | ||
178 | DO(BRLAPI_DOTS(0, 0, 1, 0, 1, 1, 0, 0), '0'), | |
179 | DO(BRLAPI_DOTS(0, 1, 0, 0, 0, 0, 0, 0), '1'), | |
180 | DO(BRLAPI_DOTS(0, 1, 1, 0, 0, 0, 0, 0), '2'), | |
181 | DO(BRLAPI_DOTS(0, 1, 0, 0, 1, 0, 0, 0), '3'), | |
182 | DO(BRLAPI_DOTS(0, 1, 0, 0, 1, 1, 0, 0), '4'), | |
183 | DO(BRLAPI_DOTS(0, 1, 0, 0, 0, 1, 0, 0), '5'), | |
184 | DO(BRLAPI_DOTS(0, 1, 1, 0, 1, 0, 0, 0), '6'), | |
185 | DO(BRLAPI_DOTS(0, 1, 1, 0, 1, 1, 0, 0), '7'), | |
186 | DO(BRLAPI_DOTS(0, 1, 1, 0, 0, 1, 0, 0), '8'), | |
187 | DO(BRLAPI_DOTS(0, 0, 1, 0, 1, 0, 0, 0), '9'), | |
188 | ||
189 | DO(BRLAPI_DOTS(0, 0, 0, 1, 0, 1, 0, 0), '.'), | |
190 | DO(BRLAPI_DOTS(0, 0, 1, 1, 0, 1, 0, 0), '+'), | |
191 | DO(BRLAPI_DOTS(0, 0, 1, 0, 0, 1, 0, 0), '-'), | |
192 | DO(BRLAPI_DOTS(1, 0, 0, 0, 0, 1, 0, 0), '*'), | |
193 | DO(BRLAPI_DOTS(0, 0, 1, 1, 0, 0, 0, 0), '/'), | |
194 | DO(BRLAPI_DOTS(1, 1, 1, 0, 1, 1, 0, 0), '('), | |
195 | DO(BRLAPI_DOTS(0, 1, 1, 1, 1, 1, 0, 0), ')'), | |
196 | ||
197 | DO(BRLAPI_DOTS(1, 1, 1, 1, 0, 1, 0, 0), '&'), | |
198 | DO(BRLAPI_DOTS(0, 0, 1, 1, 1, 1, 0, 0), '#'), | |
199 | ||
200 | DO(BRLAPI_DOTS(0, 0, 0, 0, 0, 1, 0, 0), ','), | |
201 | DO(BRLAPI_DOTS(0, 0, 0, 0, 1, 1, 0, 0), ';'), | |
202 | DO(BRLAPI_DOTS(1, 0, 0, 0, 1, 1, 0, 0), ':'), | |
203 | DO(BRLAPI_DOTS(0, 1, 1, 1, 0, 1, 0, 0), '!'), | |
204 | DO(BRLAPI_DOTS(1, 0, 0, 1, 1, 1, 0, 0), '?'), | |
205 | DO(BRLAPI_DOTS(0, 0, 0, 0, 1, 0, 0, 0), '"'), | |
206 | DO(BRLAPI_DOTS(0, 0, 1, 0, 0, 0, 0, 0), '\''), | |
207 | DO(BRLAPI_DOTS(0, 0, 0, 1, 0, 0, 0, 0), '`'), | |
208 | DO(BRLAPI_DOTS(0, 0, 0, 1, 1, 0, 1, 0), '^'), | |
209 | DO(BRLAPI_DOTS(0, 0, 0, 1, 1, 0, 0, 0), '~'), | |
210 | DO(BRLAPI_DOTS(0, 1, 0, 1, 0, 1, 1, 0), '['), | |
211 | DO(BRLAPI_DOTS(1, 1, 0, 1, 1, 1, 1, 0), ']'), | |
212 | DO(BRLAPI_DOTS(0, 1, 0, 1, 0, 1, 0, 0), '{'), | |
213 | DO(BRLAPI_DOTS(1, 1, 0, 1, 1, 1, 0, 0), '}'), | |
214 | DO(BRLAPI_DOTS(1, 1, 1, 1, 1, 1, 0, 0), '='), | |
215 | DO(BRLAPI_DOTS(1, 1, 0, 0, 0, 1, 0, 0), '<'), | |
216 | DO(BRLAPI_DOTS(0, 0, 1, 1, 1, 0, 0, 0), '>'), | |
217 | DO(BRLAPI_DOTS(1, 1, 0, 1, 0, 1, 0, 0), '$'), | |
218 | DO(BRLAPI_DOTS(1, 0, 0, 1, 0, 1, 0, 0), '%'), | |
219 | DO(BRLAPI_DOTS(0, 0, 0, 1, 0, 0, 1, 0), '@'), | |
220 | DO(BRLAPI_DOTS(1, 1, 0, 0, 1, 1, 0, 0), '|'), | |
221 | DO(BRLAPI_DOTS(1, 1, 0, 0, 1, 1, 1, 0), '\\'), | |
222 | DO(BRLAPI_DOTS(0, 0, 0, 1, 1, 1, 0, 0), '_'), | |
2e4d9fb1 AJ |
223 | }; |
224 | ||
cb78d4a1 ST |
225 | /* The guest OS has started discussing with us, finish initializing BrlAPI */ |
226 | static int baum_deferred_init(BaumDriverState *baum) | |
227 | { | |
f29b3431 ST |
228 | int tty = BRLAPI_TTY_DEFAULT; |
229 | QemuConsole *con; | |
cb78d4a1 ST |
230 | |
231 | if (baum->deferred_init) { | |
232 | return 1; | |
233 | } | |
234 | ||
235 | if (brlapi__getDisplaySize(baum->brlapi, &baum->x, &baum->y) == -1) { | |
236 | brlapi_perror("baum: brlapi__getDisplaySize"); | |
237 | return 0; | |
238 | } | |
239 | ||
f29b3431 ST |
240 | con = qemu_console_lookup_by_index(0); |
241 | if (con && qemu_console_is_graphic(con)) { | |
242 | tty = qemu_console_get_window_id(con); | |
243 | if (tty == -1) | |
244 | tty = BRLAPI_TTY_DEFAULT; | |
cb78d4a1 | 245 | } |
cb78d4a1 ST |
246 | |
247 | if (brlapi__enterTtyMode(baum->brlapi, tty, NULL) == -1) { | |
248 | brlapi_perror("baum: brlapi__enterTtyMode"); | |
249 | return 0; | |
250 | } | |
251 | baum->deferred_init = 1; | |
252 | return 1; | |
253 | } | |
254 | ||
2e4d9fb1 AJ |
255 | /* The serial port can receive more of our data */ |
256 | static void baum_accept_input(struct CharDriverState *chr) | |
257 | { | |
41ac54b2 | 258 | BaumDriverState *baum = (BaumDriverState *)chr; |
2e4d9fb1 AJ |
259 | int room, first; |
260 | ||
261 | if (!baum->out_buf_used) | |
262 | return; | |
909cda12 | 263 | room = qemu_chr_be_can_write(chr); |
2e4d9fb1 AJ |
264 | if (!room) |
265 | return; | |
266 | if (room > baum->out_buf_used) | |
267 | room = baum->out_buf_used; | |
268 | ||
269 | first = BUF_SIZE - baum->out_buf_ptr; | |
270 | if (room > first) { | |
fa5efccb | 271 | qemu_chr_be_write(chr, baum->out_buf + baum->out_buf_ptr, first); |
2e4d9fb1 AJ |
272 | baum->out_buf_ptr = 0; |
273 | baum->out_buf_used -= first; | |
274 | room -= first; | |
275 | } | |
fa5efccb | 276 | qemu_chr_be_write(chr, baum->out_buf + baum->out_buf_ptr, room); |
2e4d9fb1 AJ |
277 | baum->out_buf_ptr += room; |
278 | baum->out_buf_used -= room; | |
279 | } | |
280 | ||
281 | /* We want to send a packet */ | |
282 | static void baum_write_packet(BaumDriverState *baum, const uint8_t *buf, int len) | |
283 | { | |
41ac54b2 | 284 | CharDriverState *chr = (CharDriverState *)baum; |
2e4d9fb1 AJ |
285 | uint8_t io_buf[1 + 2 * len], *cur = io_buf; |
286 | int room; | |
287 | *cur++ = ESC; | |
288 | while (len--) | |
289 | if ((*cur++ = *buf++) == ESC) | |
290 | *cur++ = ESC; | |
41ac54b2 | 291 | room = qemu_chr_be_can_write(chr); |
2e4d9fb1 AJ |
292 | len = cur - io_buf; |
293 | if (len <= room) { | |
294 | /* Fits */ | |
41ac54b2 | 295 | qemu_chr_be_write(chr, io_buf, len); |
2e4d9fb1 AJ |
296 | } else { |
297 | int first; | |
298 | uint8_t out; | |
299 | /* Can't fit all, send what can be, and store the rest. */ | |
41ac54b2 | 300 | qemu_chr_be_write(chr, io_buf, room); |
2e4d9fb1 AJ |
301 | len -= room; |
302 | cur = io_buf + room; | |
303 | if (len > BUF_SIZE - baum->out_buf_used) { | |
304 | /* Can't even store it, drop the previous data... */ | |
305 | assert(len <= BUF_SIZE); | |
306 | baum->out_buf_used = 0; | |
307 | baum->out_buf_ptr = 0; | |
308 | } | |
309 | out = baum->out_buf_ptr; | |
310 | baum->out_buf_used += len; | |
311 | first = BUF_SIZE - baum->out_buf_ptr; | |
312 | if (len > first) { | |
313 | memcpy(baum->out_buf + out, cur, first); | |
314 | out = 0; | |
315 | len -= first; | |
316 | cur += first; | |
317 | } | |
318 | memcpy(baum->out_buf + out, cur, len); | |
319 | } | |
320 | } | |
321 | ||
322 | /* Called when the other end seems to have a wrong idea of our display size */ | |
323 | static void baum_cellCount_timer_cb(void *opaque) | |
324 | { | |
325 | BaumDriverState *baum = opaque; | |
326 | uint8_t cell_count[] = { BAUM_RSP_CellCount, baum->x * baum->y }; | |
327 | DPRINTF("Timeout waiting for DisplayData, sending cell count\n"); | |
328 | baum_write_packet(baum, cell_count, sizeof(cell_count)); | |
329 | } | |
330 | ||
331 | /* Try to interpret a whole incoming packet */ | |
332 | static int baum_eat_packet(BaumDriverState *baum, const uint8_t *buf, int len) | |
333 | { | |
334 | const uint8_t *cur = buf; | |
335 | uint8_t req = 0; | |
336 | ||
337 | if (!len--) | |
338 | return 0; | |
339 | if (*cur++ != ESC) { | |
340 | while (*cur != ESC) { | |
341 | if (!len--) | |
342 | return 0; | |
343 | cur++; | |
344 | } | |
70cbae1d | 345 | DPRINTF("Dropped %td bytes!\n", cur - buf); |
2e4d9fb1 AJ |
346 | } |
347 | ||
348 | #define EAT(c) do {\ | |
349 | if (!len--) \ | |
350 | return 0; \ | |
351 | if ((c = *cur++) == ESC) { \ | |
352 | if (!len--) \ | |
353 | return 0; \ | |
354 | if (*cur++ != ESC) { \ | |
355 | DPRINTF("Broken packet %#2x, tossing\n", req); \ | |
e93379b0 | 356 | if (timer_pending(baum->cellCount_timer)) { \ |
bc72ad67 | 357 | timer_del(baum->cellCount_timer); \ |
e93379b0 | 358 | baum_cellCount_timer_cb(baum); \ |
2e4d9fb1 AJ |
359 | } \ |
360 | return (cur - 2 - buf); \ | |
361 | } \ | |
362 | } \ | |
363 | } while (0) | |
364 | ||
365 | EAT(req); | |
366 | switch (req) { | |
367 | case BAUM_REQ_DisplayData: | |
368 | { | |
369 | uint8_t cells[baum->x * baum->y], c; | |
370 | uint8_t text[baum->x * baum->y]; | |
371 | uint8_t zero[baum->x * baum->y]; | |
372 | int cursor = BRLAPI_CURSOR_OFF; | |
373 | int i; | |
374 | ||
375 | /* Allow 100ms to complete the DisplayData packet */ | |
bc72ad67 | 376 | timer_mod(baum->cellCount_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
73bcb24d | 377 | NANOSECONDS_PER_SECOND / 10); |
2e4d9fb1 AJ |
378 | for (i = 0; i < baum->x * baum->y ; i++) { |
379 | EAT(c); | |
380 | cells[i] = c; | |
381 | if ((c & (BRLAPI_DOT7|BRLAPI_DOT8)) | |
382 | == (BRLAPI_DOT7|BRLAPI_DOT8)) { | |
383 | cursor = i + 1; | |
384 | c &= ~(BRLAPI_DOT7|BRLAPI_DOT8); | |
385 | } | |
0fb7c882 ST |
386 | c = nabcc_translation[DOTS2ASCII][c]; |
387 | if (!c) { | |
2e4d9fb1 | 388 | c = '?'; |
0fb7c882 | 389 | } |
2e4d9fb1 AJ |
390 | text[i] = c; |
391 | } | |
bc72ad67 | 392 | timer_del(baum->cellCount_timer); |
2e4d9fb1 AJ |
393 | |
394 | memset(zero, 0, sizeof(zero)); | |
395 | ||
396 | brlapi_writeArguments_t wa = { | |
397 | .displayNumber = BRLAPI_DISPLAY_DEFAULT, | |
398 | .regionBegin = 1, | |
399 | .regionSize = baum->x * baum->y, | |
4f72c4dd | 400 | .text = (char *)text, |
2e4d9fb1 AJ |
401 | .textSize = baum->x * baum->y, |
402 | .andMask = zero, | |
403 | .orMask = cells, | |
404 | .cursor = cursor, | |
4f72c4dd | 405 | .charset = (char *)"ISO-8859-1", |
2e4d9fb1 AJ |
406 | }; |
407 | ||
408 | if (brlapi__write(baum->brlapi, &wa) == -1) | |
409 | brlapi_perror("baum brlapi_write"); | |
410 | break; | |
411 | } | |
412 | case BAUM_REQ_SetMode: | |
413 | { | |
414 | uint8_t mode, setting; | |
415 | DPRINTF("SetMode\n"); | |
416 | EAT(mode); | |
417 | EAT(setting); | |
418 | /* ignore */ | |
419 | break; | |
420 | } | |
421 | case BAUM_REQ_SetProtocol: | |
422 | { | |
423 | uint8_t protocol; | |
424 | DPRINTF("SetProtocol\n"); | |
425 | EAT(protocol); | |
426 | /* ignore */ | |
427 | break; | |
428 | } | |
429 | case BAUM_REQ_GetDeviceIdentity: | |
430 | { | |
431 | uint8_t identity[17] = { BAUM_RSP_DeviceIdentity, | |
432 | 'B','a','u','m',' ','V','a','r','i','o' }; | |
433 | DPRINTF("GetDeviceIdentity\n"); | |
434 | identity[11] = '0' + baum->x / 10; | |
435 | identity[12] = '0' + baum->x % 10; | |
436 | baum_write_packet(baum, identity, sizeof(identity)); | |
437 | break; | |
438 | } | |
439 | case BAUM_REQ_GetVersionNumber: | |
440 | { | |
441 | uint8_t version[] = { BAUM_RSP_VersionNumber, 1 }; /* ? */ | |
442 | DPRINTF("GetVersionNumber\n"); | |
443 | baum_write_packet(baum, version, sizeof(version)); | |
444 | break; | |
445 | } | |
446 | case BAUM_REQ_GetSerialNumber: | |
447 | { | |
448 | uint8_t serial[] = { BAUM_RSP_SerialNumber, | |
449 | '0','0','0','0','0','0','0','0' }; | |
450 | DPRINTF("GetSerialNumber\n"); | |
451 | baum_write_packet(baum, serial, sizeof(serial)); | |
452 | break; | |
453 | } | |
454 | case BAUM_REQ_GetKeys: | |
455 | { | |
456 | DPRINTF("Get%0#2x\n", req); | |
457 | /* ignore */ | |
458 | break; | |
459 | } | |
460 | default: | |
461 | DPRINTF("unrecognized request %0#2x\n", req); | |
462 | do | |
463 | if (!len--) | |
464 | return 0; | |
465 | while (*cur++ != ESC); | |
466 | cur--; | |
467 | break; | |
468 | } | |
469 | return cur - buf; | |
470 | } | |
471 | ||
472 | /* The other end is writing some data. Store it and try to interpret */ | |
473 | static int baum_write(CharDriverState *chr, const uint8_t *buf, int len) | |
474 | { | |
41ac54b2 | 475 | BaumDriverState *baum = (BaumDriverState *)chr; |
2e4d9fb1 AJ |
476 | int tocopy, cur, eaten, orig_len = len; |
477 | ||
478 | if (!len) | |
479 | return 0; | |
480 | if (!baum->brlapi) | |
481 | return len; | |
cb78d4a1 ST |
482 | if (!baum_deferred_init(baum)) |
483 | return len; | |
2e4d9fb1 AJ |
484 | |
485 | while (len) { | |
486 | /* Complete our buffer as much as possible */ | |
487 | tocopy = len; | |
488 | if (tocopy > BUF_SIZE - baum->in_buf_used) | |
489 | tocopy = BUF_SIZE - baum->in_buf_used; | |
490 | ||
491 | memcpy(baum->in_buf + baum->in_buf_used, buf, tocopy); | |
492 | baum->in_buf_used += tocopy; | |
493 | buf += tocopy; | |
494 | len -= tocopy; | |
495 | ||
496 | /* Interpret it as much as possible */ | |
497 | cur = 0; | |
498 | while (cur < baum->in_buf_used && | |
499 | (eaten = baum_eat_packet(baum, baum->in_buf + cur, baum->in_buf_used - cur))) | |
500 | cur += eaten; | |
501 | ||
502 | /* Shift the remainder */ | |
503 | if (cur) { | |
504 | memmove(baum->in_buf, baum->in_buf + cur, baum->in_buf_used - cur); | |
505 | baum->in_buf_used -= cur; | |
506 | } | |
507 | ||
508 | /* And continue if any data left */ | |
509 | } | |
510 | return orig_len; | |
511 | } | |
512 | ||
2e4d9fb1 AJ |
513 | /* Send the key code to the other end */ |
514 | static void baum_send_key(BaumDriverState *baum, uint8_t type, uint8_t value) { | |
515 | uint8_t packet[] = { type, value }; | |
516 | DPRINTF("writing key %x %x\n", type, value); | |
517 | baum_write_packet(baum, packet, sizeof(packet)); | |
518 | } | |
519 | ||
0fb7c882 ST |
520 | static void baum_send_key2(BaumDriverState *baum, uint8_t type, uint8_t value, |
521 | uint8_t value2) { | |
522 | uint8_t packet[] = { type, value, value2 }; | |
523 | DPRINTF("writing key %x %x\n", type, value); | |
524 | baum_write_packet(baum, packet, sizeof(packet)); | |
525 | } | |
526 | ||
2e4d9fb1 AJ |
527 | /* We got some data on the BrlAPI socket */ |
528 | static void baum_chr_read(void *opaque) | |
529 | { | |
530 | BaumDriverState *baum = opaque; | |
531 | brlapi_keyCode_t code; | |
532 | int ret; | |
533 | if (!baum->brlapi) | |
534 | return; | |
cb78d4a1 ST |
535 | if (!baum_deferred_init(baum)) |
536 | return; | |
2e4d9fb1 AJ |
537 | while ((ret = brlapi__readKey(baum->brlapi, 0, &code)) == 1) { |
538 | DPRINTF("got key %"BRLAPI_PRIxKEYCODE"\n", code); | |
539 | /* Emulate */ | |
540 | switch (code & BRLAPI_KEY_TYPE_MASK) { | |
541 | case BRLAPI_KEY_TYPE_CMD: | |
542 | switch (code & BRLAPI_KEY_CMD_BLK_MASK) { | |
543 | case BRLAPI_KEY_CMD_ROUTE: | |
544 | baum_send_key(baum, BAUM_RSP_RoutingKey, (code & BRLAPI_KEY_CMD_ARG_MASK)+1); | |
545 | baum_send_key(baum, BAUM_RSP_RoutingKey, 0); | |
546 | break; | |
547 | case 0: | |
548 | switch (code & BRLAPI_KEY_CMD_ARG_MASK) { | |
549 | case BRLAPI_KEY_CMD_FWINLT: | |
550 | baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL2); | |
551 | baum_send_key(baum, BAUM_RSP_TopKeys, 0); | |
552 | break; | |
553 | case BRLAPI_KEY_CMD_FWINRT: | |
554 | baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TR2); | |
555 | baum_send_key(baum, BAUM_RSP_TopKeys, 0); | |
556 | break; | |
557 | case BRLAPI_KEY_CMD_LNUP: | |
558 | baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TR1); | |
559 | baum_send_key(baum, BAUM_RSP_TopKeys, 0); | |
560 | break; | |
561 | case BRLAPI_KEY_CMD_LNDN: | |
562 | baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TR3); | |
563 | baum_send_key(baum, BAUM_RSP_TopKeys, 0); | |
564 | break; | |
565 | case BRLAPI_KEY_CMD_TOP: | |
566 | baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL1|BAUM_TR1); | |
567 | baum_send_key(baum, BAUM_RSP_TopKeys, 0); | |
568 | break; | |
569 | case BRLAPI_KEY_CMD_BOT: | |
570 | baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL3|BAUM_TR3); | |
571 | baum_send_key(baum, BAUM_RSP_TopKeys, 0); | |
572 | break; | |
573 | case BRLAPI_KEY_CMD_TOP_LEFT: | |
574 | baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL2|BAUM_TR1); | |
575 | baum_send_key(baum, BAUM_RSP_TopKeys, 0); | |
576 | break; | |
577 | case BRLAPI_KEY_CMD_BOT_LEFT: | |
578 | baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL2|BAUM_TR3); | |
579 | baum_send_key(baum, BAUM_RSP_TopKeys, 0); | |
580 | break; | |
581 | case BRLAPI_KEY_CMD_HOME: | |
582 | baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL2|BAUM_TR1|BAUM_TR3); | |
583 | baum_send_key(baum, BAUM_RSP_TopKeys, 0); | |
584 | break; | |
585 | case BRLAPI_KEY_CMD_PREFMENU: | |
586 | baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL1|BAUM_TL3|BAUM_TR1); | |
587 | baum_send_key(baum, BAUM_RSP_TopKeys, 0); | |
588 | break; | |
589 | } | |
590 | } | |
591 | break; | |
592 | case BRLAPI_KEY_TYPE_SYM: | |
0fb7c882 ST |
593 | { |
594 | brlapi_keyCode_t keysym = code & BRLAPI_KEY_CODE_MASK; | |
595 | if (keysym < 0x100) { | |
596 | uint8_t dots = nabcc_translation[ASCII2DOTS][keysym]; | |
597 | if (dots) { | |
598 | baum_send_key2(baum, BAUM_RSP_EntryKeys, 0, dots); | |
599 | baum_send_key2(baum, BAUM_RSP_EntryKeys, 0, 0); | |
600 | } | |
601 | } | |
602 | break; | |
603 | } | |
2e4d9fb1 AJ |
604 | } |
605 | } | |
606 | if (ret == -1 && (brlapi_errno != BRLAPI_ERROR_LIBCERR || errno != EINTR)) { | |
607 | brlapi_perror("baum: brlapi_readKey"); | |
608 | brlapi__closeConnection(baum->brlapi); | |
7267c094 | 609 | g_free(baum->brlapi); |
2e4d9fb1 AJ |
610 | baum->brlapi = NULL; |
611 | } | |
612 | } | |
613 | ||
72ac8762 | 614 | static void baum_free(struct CharDriverState *chr) |
2c7faf31 | 615 | { |
41ac54b2 | 616 | BaumDriverState *baum = (BaumDriverState *)chr; |
2c7faf31 | 617 | |
bc72ad67 | 618 | timer_free(baum->cellCount_timer); |
2c7faf31 ST |
619 | if (baum->brlapi) { |
620 | brlapi__closeConnection(baum->brlapi); | |
7267c094 | 621 | g_free(baum->brlapi); |
2c7faf31 | 622 | } |
2c7faf31 ST |
623 | } |
624 | ||
b68e956a MAL |
625 | static CharDriverState *chr_baum_init(const CharDriver *driver, |
626 | const char *id, | |
e47666b8 PB |
627 | ChardevBackend *backend, |
628 | ChardevReturn *ret, | |
82878dac | 629 | bool *be_opened, |
e47666b8 | 630 | Error **errp) |
2e4d9fb1 | 631 | { |
32bafa8f | 632 | ChardevCommon *common = backend->u.braille.data; |
2e4d9fb1 AJ |
633 | BaumDriverState *baum; |
634 | CharDriverState *chr; | |
635 | brlapi_handle_t *handle; | |
2e4d9fb1 | 636 | |
b68e956a | 637 | chr = qemu_chr_alloc(driver, common, errp); |
d0d7708b DB |
638 | if (!chr) { |
639 | return NULL; | |
640 | } | |
41ac54b2 | 641 | baum = (BaumDriverState *)chr; |
2e4d9fb1 | 642 | |
7267c094 | 643 | handle = g_malloc0(brlapi_getHandleSize()); |
2e4d9fb1 AJ |
644 | baum->brlapi = handle; |
645 | ||
646 | baum->brlapi_fd = brlapi__openConnection(handle, NULL, NULL); | |
647 | if (baum->brlapi_fd == -1) { | |
e47666b8 PB |
648 | error_setg(errp, "brlapi__openConnection: %s", |
649 | brlapi_strerror(brlapi_error_location())); | |
2e4d9fb1 AJ |
650 | goto fail_handle; |
651 | } | |
cb78d4a1 | 652 | baum->deferred_init = 0; |
2e4d9fb1 | 653 | |
bc72ad67 | 654 | baum->cellCount_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, baum_cellCount_timer_cb, baum); |
2e4d9fb1 | 655 | |
2e4d9fb1 AJ |
656 | qemu_set_fd_handler(baum->brlapi_fd, baum_chr_read, NULL, baum); |
657 | ||
1f51470d | 658 | return chr; |
2e4d9fb1 | 659 | |
260cfc43 | 660 | fail_handle: |
7267c094 AL |
661 | g_free(handle); |
662 | g_free(chr); | |
1f51470d | 663 | return NULL; |
2e4d9fb1 | 664 | } |
08744c98 AL |
665 | |
666 | static void register_types(void) | |
667 | { | |
0b812f31 | 668 | static const CharDriver driver = { |
41ac54b2 | 669 | .instance_size = sizeof(BaumDriverState), |
0b812f31 MAL |
670 | .kind = CHARDEV_BACKEND_KIND_BRAILLE, |
671 | .create = chr_baum_init, | |
b68e956a MAL |
672 | .chr_write = baum_write, |
673 | .chr_accept_input = baum_accept_input, | |
674 | .chr_free = baum_free, | |
0b812f31 MAL |
675 | }; |
676 | ||
677 | register_char_driver(&driver); | |
08744c98 AL |
678 | } |
679 | ||
680 | type_init(register_types); |