fix event fallout in sh_serial.c
[qemu/aliguori.git] / hw / baum.c
blob0b397fd1d926f1a22af92d5c2985ef6c2fc99aa7
1 /*
2 * QEMU Baum Braille Device
4 * Copyright (c) 2008 Samuel Thibault
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu-common.h"
25 #include "qemu-char.h"
26 #include "qemu-timer.h"
27 #include "usb.h"
28 #include "baum.h"
29 #include <brlapi.h>
30 #include <brlapi_constants.h>
31 #include <brlapi_keycodes.h>
32 #ifdef CONFIG_SDL
33 #include <SDL_syswm.h>
34 #endif
36 #if 0
37 #define DPRINTF(fmt, ...) \
38 printf(fmt, ## __VA_ARGS__)
39 #else
40 #define DPRINTF(fmt, ...)
41 #endif
43 #define ESC 0x1B
45 #define BAUM_REQ_DisplayData 0x01
46 #define BAUM_REQ_GetVersionNumber 0x05
47 #define BAUM_REQ_GetKeys 0x08
48 #define BAUM_REQ_SetMode 0x12
49 #define BAUM_REQ_SetProtocol 0x15
50 #define BAUM_REQ_GetDeviceIdentity 0x84
51 #define BAUM_REQ_GetSerialNumber 0x8A
53 #define BAUM_RSP_CellCount 0x01
54 #define BAUM_RSP_VersionNumber 0x05
55 #define BAUM_RSP_ModeSetting 0x11
56 #define BAUM_RSP_CommunicationChannel 0x16
57 #define BAUM_RSP_PowerdownSignal 0x17
58 #define BAUM_RSP_HorizontalSensors 0x20
59 #define BAUM_RSP_VerticalSensors 0x21
60 #define BAUM_RSP_RoutingKeys 0x22
61 #define BAUM_RSP_Switches 0x23
62 #define BAUM_RSP_TopKeys 0x24
63 #define BAUM_RSP_HorizontalSensor 0x25
64 #define BAUM_RSP_VerticalSensor 0x26
65 #define BAUM_RSP_RoutingKey 0x27
66 #define BAUM_RSP_FrontKeys6 0x28
67 #define BAUM_RSP_BackKeys6 0x29
68 #define BAUM_RSP_CommandKeys 0x2B
69 #define BAUM_RSP_FrontKeys10 0x2C
70 #define BAUM_RSP_BackKeys10 0x2D
71 #define BAUM_RSP_EntryKeys 0x33
72 #define BAUM_RSP_JoyStick 0x34
73 #define BAUM_RSP_ErrorCode 0x40
74 #define BAUM_RSP_InfoBlock 0x42
75 #define BAUM_RSP_DeviceIdentity 0x84
76 #define BAUM_RSP_SerialNumber 0x8A
77 #define BAUM_RSP_BluetoothName 0x8C
79 #define BAUM_TL1 0x01
80 #define BAUM_TL2 0x02
81 #define BAUM_TL3 0x04
82 #define BAUM_TR1 0x08
83 #define BAUM_TR2 0x10
84 #define BAUM_TR3 0x20
86 #define BUF_SIZE 256
88 typedef struct {
89 CharDriverState *chr;
91 brlapi_handle_t *brlapi;
92 int brlapi_fd;
93 unsigned int x, y;
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;
100 QEMUTimer *cellCount_timer;
101 } BaumDriverState;
103 /* Let's assume NABCC by default */
104 static const uint8_t nabcc_translation[256] = {
105 [0] = ' ',
106 #ifndef BRLAPI_DOTS
107 #define BRLAPI_DOTS(d1,d2,d3,d4,d5,d6,d7,d8) \
108 ((d1?BRLAPI_DOT1:0)|\
109 (d2?BRLAPI_DOT2:0)|\
110 (d3?BRLAPI_DOT3:0)|\
111 (d4?BRLAPI_DOT4:0)|\
112 (d5?BRLAPI_DOT5:0)|\
113 (d6?BRLAPI_DOT6:0)|\
114 (d7?BRLAPI_DOT7:0)|\
115 (d8?BRLAPI_DOT8:0))
116 #endif
117 [BRLAPI_DOTS(1,0,0,0,0,0,0,0)] = 'a',
118 [BRLAPI_DOTS(1,1,0,0,0,0,0,0)] = 'b',
119 [BRLAPI_DOTS(1,0,0,1,0,0,0,0)] = 'c',
120 [BRLAPI_DOTS(1,0,0,1,1,0,0,0)] = 'd',
121 [BRLAPI_DOTS(1,0,0,0,1,0,0,0)] = 'e',
122 [BRLAPI_DOTS(1,1,0,1,0,0,0,0)] = 'f',
123 [BRLAPI_DOTS(1,1,0,1,1,0,0,0)] = 'g',
124 [BRLAPI_DOTS(1,1,0,0,1,0,0,0)] = 'h',
125 [BRLAPI_DOTS(0,1,0,1,0,0,0,0)] = 'i',
126 [BRLAPI_DOTS(0,1,0,1,1,0,0,0)] = 'j',
127 [BRLAPI_DOTS(1,0,1,0,0,0,0,0)] = 'k',
128 [BRLAPI_DOTS(1,1,1,0,0,0,0,0)] = 'l',
129 [BRLAPI_DOTS(1,0,1,1,0,0,0,0)] = 'm',
130 [BRLAPI_DOTS(1,0,1,1,1,0,0,0)] = 'n',
131 [BRLAPI_DOTS(1,0,1,0,1,0,0,0)] = 'o',
132 [BRLAPI_DOTS(1,1,1,1,0,0,0,0)] = 'p',
133 [BRLAPI_DOTS(1,1,1,1,1,0,0,0)] = 'q',
134 [BRLAPI_DOTS(1,1,1,0,1,0,0,0)] = 'r',
135 [BRLAPI_DOTS(0,1,1,1,0,0,0,0)] = 's',
136 [BRLAPI_DOTS(0,1,1,1,1,0,0,0)] = 't',
137 [BRLAPI_DOTS(1,0,1,0,0,1,0,0)] = 'u',
138 [BRLAPI_DOTS(1,1,1,0,0,1,0,0)] = 'v',
139 [BRLAPI_DOTS(0,1,0,1,1,1,0,0)] = 'w',
140 [BRLAPI_DOTS(1,0,1,1,0,1,0,0)] = 'x',
141 [BRLAPI_DOTS(1,0,1,1,1,1,0,0)] = 'y',
142 [BRLAPI_DOTS(1,0,1,0,1,1,0,0)] = 'z',
144 [BRLAPI_DOTS(1,0,0,0,0,0,1,0)] = 'A',
145 [BRLAPI_DOTS(1,1,0,0,0,0,1,0)] = 'B',
146 [BRLAPI_DOTS(1,0,0,1,0,0,1,0)] = 'C',
147 [BRLAPI_DOTS(1,0,0,1,1,0,1,0)] = 'D',
148 [BRLAPI_DOTS(1,0,0,0,1,0,1,0)] = 'E',
149 [BRLAPI_DOTS(1,1,0,1,0,0,1,0)] = 'F',
150 [BRLAPI_DOTS(1,1,0,1,1,0,1,0)] = 'G',
151 [BRLAPI_DOTS(1,1,0,0,1,0,1,0)] = 'H',
152 [BRLAPI_DOTS(0,1,0,1,0,0,1,0)] = 'I',
153 [BRLAPI_DOTS(0,1,0,1,1,0,1,0)] = 'J',
154 [BRLAPI_DOTS(1,0,1,0,0,0,1,0)] = 'K',
155 [BRLAPI_DOTS(1,1,1,0,0,0,1,0)] = 'L',
156 [BRLAPI_DOTS(1,0,1,1,0,0,1,0)] = 'M',
157 [BRLAPI_DOTS(1,0,1,1,1,0,1,0)] = 'N',
158 [BRLAPI_DOTS(1,0,1,0,1,0,1,0)] = 'O',
159 [BRLAPI_DOTS(1,1,1,1,0,0,1,0)] = 'P',
160 [BRLAPI_DOTS(1,1,1,1,1,0,1,0)] = 'Q',
161 [BRLAPI_DOTS(1,1,1,0,1,0,1,0)] = 'R',
162 [BRLAPI_DOTS(0,1,1,1,0,0,1,0)] = 'S',
163 [BRLAPI_DOTS(0,1,1,1,1,0,1,0)] = 'T',
164 [BRLAPI_DOTS(1,0,1,0,0,1,1,0)] = 'U',
165 [BRLAPI_DOTS(1,1,1,0,0,1,1,0)] = 'V',
166 [BRLAPI_DOTS(0,1,0,1,1,1,1,0)] = 'W',
167 [BRLAPI_DOTS(1,0,1,1,0,1,1,0)] = 'X',
168 [BRLAPI_DOTS(1,0,1,1,1,1,1,0)] = 'Y',
169 [BRLAPI_DOTS(1,0,1,0,1,1,1,0)] = 'Z',
171 [BRLAPI_DOTS(0,0,1,0,1,1,0,0)] = '0',
172 [BRLAPI_DOTS(0,1,0,0,0,0,0,0)] = '1',
173 [BRLAPI_DOTS(0,1,1,0,0,0,0,0)] = '2',
174 [BRLAPI_DOTS(0,1,0,0,1,0,0,0)] = '3',
175 [BRLAPI_DOTS(0,1,0,0,1,1,0,0)] = '4',
176 [BRLAPI_DOTS(0,1,0,0,0,1,0,0)] = '5',
177 [BRLAPI_DOTS(0,1,1,0,1,0,0,0)] = '6',
178 [BRLAPI_DOTS(0,1,1,0,1,1,0,0)] = '7',
179 [BRLAPI_DOTS(0,1,1,0,0,1,0,0)] = '8',
180 [BRLAPI_DOTS(0,0,1,0,1,0,0,0)] = '9',
182 [BRLAPI_DOTS(0,0,0,1,0,1,0,0)] = '.',
183 [BRLAPI_DOTS(0,0,1,1,0,1,0,0)] = '+',
184 [BRLAPI_DOTS(0,0,1,0,0,1,0,0)] = '-',
185 [BRLAPI_DOTS(1,0,0,0,0,1,0,0)] = '*',
186 [BRLAPI_DOTS(0,0,1,1,0,0,0,0)] = '/',
187 [BRLAPI_DOTS(1,1,1,0,1,1,0,0)] = '(',
188 [BRLAPI_DOTS(0,1,1,1,1,1,0,0)] = ')',
190 [BRLAPI_DOTS(1,1,1,1,0,1,0,0)] = '&',
191 [BRLAPI_DOTS(0,0,1,1,1,1,0,0)] = '#',
193 [BRLAPI_DOTS(0,0,0,0,0,1,0,0)] = ',',
194 [BRLAPI_DOTS(0,0,0,0,1,1,0,0)] = ';',
195 [BRLAPI_DOTS(1,0,0,0,1,1,0,0)] = ':',
196 [BRLAPI_DOTS(0,1,1,1,0,1,0,0)] = '!',
197 [BRLAPI_DOTS(1,0,0,1,1,1,0,0)] = '?',
198 [BRLAPI_DOTS(0,0,0,0,1,0,0,0)] = '"',
199 [BRLAPI_DOTS(0,0,1,0,0,0,0,0)] ='\'',
200 [BRLAPI_DOTS(0,0,0,1,0,0,0,0)] = '`',
201 [BRLAPI_DOTS(0,0,0,1,1,0,1,0)] = '^',
202 [BRLAPI_DOTS(0,0,0,1,1,0,0,0)] = '~',
203 [BRLAPI_DOTS(0,1,0,1,0,1,1,0)] = '[',
204 [BRLAPI_DOTS(1,1,0,1,1,1,1,0)] = ']',
205 [BRLAPI_DOTS(0,1,0,1,0,1,0,0)] = '{',
206 [BRLAPI_DOTS(1,1,0,1,1,1,0,0)] = '}',
207 [BRLAPI_DOTS(1,1,1,1,1,1,0,0)] = '=',
208 [BRLAPI_DOTS(1,1,0,0,0,1,0,0)] = '<',
209 [BRLAPI_DOTS(0,0,1,1,1,0,0,0)] = '>',
210 [BRLAPI_DOTS(1,1,0,1,0,1,0,0)] = '$',
211 [BRLAPI_DOTS(1,0,0,1,0,1,0,0)] = '%',
212 [BRLAPI_DOTS(0,0,0,1,0,0,1,0)] = '@',
213 [BRLAPI_DOTS(1,1,0,0,1,1,0,0)] = '|',
214 [BRLAPI_DOTS(1,1,0,0,1,1,1,0)] ='\\',
215 [BRLAPI_DOTS(0,0,0,1,1,1,0,0)] = '_',
218 #if 0
219 /* The serial port can receive more of our data */
220 static void baum_accept_input(struct CharDriverState *chr)
222 BaumDriverState *baum = chr->opaque;
223 int room, first;
225 if (!baum->out_buf_used)
226 return;
227 room = qemu_chr_be_can_write(chr);
228 if (!room)
229 return;
230 if (room > baum->out_buf_used)
231 room = baum->out_buf_used;
233 first = BUF_SIZE - baum->out_buf_ptr;
234 if (room > first) {
235 qemu_chr_be_write(chr, baum->out_buf + baum->out_buf_ptr, first);
236 baum->out_buf_ptr = 0;
237 baum->out_buf_used -= first;
238 room -= first;
240 qemu_chr_be_write(chr, baum->out_buf + baum->out_buf_ptr, room);
241 baum->out_buf_ptr += room;
242 baum->out_buf_used -= room;
244 #endif
246 /* We want to send a packet */
247 static void baum_write_packet(BaumDriverState *baum, const uint8_t *buf, int len)
249 uint8_t io_buf[1 + 2 * len], *cur = io_buf;
250 int room;
251 *cur++ = ESC;
252 while (len--)
253 if ((*cur++ = *buf++) == ESC)
254 *cur++ = ESC;
255 room = qemu_chr_be_can_write(baum->chr);
256 len = cur - io_buf;
257 if (len <= room) {
258 /* Fits */
259 qemu_chr_be_write(baum->chr, io_buf, len);
260 } else {
261 int first;
262 uint8_t out;
263 /* Can't fit all, send what can be, and store the rest. */
264 qemu_chr_be_write(baum->chr, io_buf, room);
265 len -= room;
266 cur = io_buf + room;
267 if (len > BUF_SIZE - baum->out_buf_used) {
268 /* Can't even store it, drop the previous data... */
269 assert(len <= BUF_SIZE);
270 baum->out_buf_used = 0;
271 baum->out_buf_ptr = 0;
273 out = baum->out_buf_ptr;
274 baum->out_buf_used += len;
275 first = BUF_SIZE - baum->out_buf_ptr;
276 if (len > first) {
277 memcpy(baum->out_buf + out, cur, first);
278 out = 0;
279 len -= first;
280 cur += first;
282 memcpy(baum->out_buf + out, cur, len);
286 /* Called when the other end seems to have a wrong idea of our display size */
287 static void baum_cellCount_timer_cb(void *opaque)
289 BaumDriverState *baum = opaque;
290 uint8_t cell_count[] = { BAUM_RSP_CellCount, baum->x * baum->y };
291 DPRINTF("Timeout waiting for DisplayData, sending cell count\n");
292 baum_write_packet(baum, cell_count, sizeof(cell_count));
295 /* Try to interpret a whole incoming packet */
296 static int baum_eat_packet(BaumDriverState *baum, const uint8_t *buf, int len)
298 const uint8_t *cur = buf;
299 uint8_t req = 0;
301 if (!len--)
302 return 0;
303 if (*cur++ != ESC) {
304 while (*cur != ESC) {
305 if (!len--)
306 return 0;
307 cur++;
309 DPRINTF("Dropped %d bytes!\n", cur - buf);
312 #define EAT(c) do {\
313 if (!len--) \
314 return 0; \
315 if ((c = *cur++) == ESC) { \
316 if (!len--) \
317 return 0; \
318 if (*cur++ != ESC) { \
319 DPRINTF("Broken packet %#2x, tossing\n", req); \
320 if (qemu_timer_pending(baum->cellCount_timer)) { \
321 qemu_del_timer(baum->cellCount_timer); \
322 baum_cellCount_timer_cb(baum); \
324 return (cur - 2 - buf); \
327 } while (0)
329 EAT(req);
330 switch (req) {
331 case BAUM_REQ_DisplayData:
333 uint8_t cells[baum->x * baum->y], c;
334 uint8_t text[baum->x * baum->y];
335 uint8_t zero[baum->x * baum->y];
336 int cursor = BRLAPI_CURSOR_OFF;
337 int i;
339 /* Allow 100ms to complete the DisplayData packet */
340 qemu_mod_timer(baum->cellCount_timer, qemu_get_clock_ns(vm_clock) +
341 get_ticks_per_sec() / 10);
342 for (i = 0; i < baum->x * baum->y ; i++) {
343 EAT(c);
344 cells[i] = c;
345 if ((c & (BRLAPI_DOT7|BRLAPI_DOT8))
346 == (BRLAPI_DOT7|BRLAPI_DOT8)) {
347 cursor = i + 1;
348 c &= ~(BRLAPI_DOT7|BRLAPI_DOT8);
350 if (!(c = nabcc_translation[c]))
351 c = '?';
352 text[i] = c;
354 qemu_del_timer(baum->cellCount_timer);
356 memset(zero, 0, sizeof(zero));
358 brlapi_writeArguments_t wa = {
359 .displayNumber = BRLAPI_DISPLAY_DEFAULT,
360 .regionBegin = 1,
361 .regionSize = baum->x * baum->y,
362 .text = (char *)text,
363 .textSize = baum->x * baum->y,
364 .andMask = zero,
365 .orMask = cells,
366 .cursor = cursor,
367 .charset = (char *)"ISO-8859-1",
370 if (brlapi__write(baum->brlapi, &wa) == -1)
371 brlapi_perror("baum brlapi_write");
372 break;
374 case BAUM_REQ_SetMode:
376 uint8_t mode, setting;
377 DPRINTF("SetMode\n");
378 EAT(mode);
379 EAT(setting);
380 /* ignore */
381 break;
383 case BAUM_REQ_SetProtocol:
385 uint8_t protocol;
386 DPRINTF("SetProtocol\n");
387 EAT(protocol);
388 /* ignore */
389 break;
391 case BAUM_REQ_GetDeviceIdentity:
393 uint8_t identity[17] = { BAUM_RSP_DeviceIdentity,
394 'B','a','u','m',' ','V','a','r','i','o' };
395 DPRINTF("GetDeviceIdentity\n");
396 identity[11] = '0' + baum->x / 10;
397 identity[12] = '0' + baum->x % 10;
398 baum_write_packet(baum, identity, sizeof(identity));
399 break;
401 case BAUM_REQ_GetVersionNumber:
403 uint8_t version[] = { BAUM_RSP_VersionNumber, 1 }; /* ? */
404 DPRINTF("GetVersionNumber\n");
405 baum_write_packet(baum, version, sizeof(version));
406 break;
408 case BAUM_REQ_GetSerialNumber:
410 uint8_t serial[] = { BAUM_RSP_SerialNumber,
411 '0','0','0','0','0','0','0','0' };
412 DPRINTF("GetSerialNumber\n");
413 baum_write_packet(baum, serial, sizeof(serial));
414 break;
416 case BAUM_REQ_GetKeys:
418 DPRINTF("Get%0#2x\n", req);
419 /* ignore */
420 break;
422 default:
423 DPRINTF("unrecognized request %0#2x\n", req);
425 if (!len--)
426 return 0;
427 while (*cur++ != ESC);
428 cur--;
429 break;
431 return cur - buf;
434 /* The other end is writing some data. Store it and try to interpret */
435 static int baum_write(CharDriverState *chr, const uint8_t *buf, int len)
437 BaumDriverState *baum = chr->opaque;
438 int tocopy, cur, eaten, orig_len = len;
440 if (!len)
441 return 0;
442 if (!baum->brlapi)
443 return len;
445 while (len) {
446 /* Complete our buffer as much as possible */
447 tocopy = len;
448 if (tocopy > BUF_SIZE - baum->in_buf_used)
449 tocopy = BUF_SIZE - baum->in_buf_used;
451 memcpy(baum->in_buf + baum->in_buf_used, buf, tocopy);
452 baum->in_buf_used += tocopy;
453 buf += tocopy;
454 len -= tocopy;
456 /* Interpret it as much as possible */
457 cur = 0;
458 while (cur < baum->in_buf_used &&
459 (eaten = baum_eat_packet(baum, baum->in_buf + cur, baum->in_buf_used - cur)))
460 cur += eaten;
462 /* Shift the remainder */
463 if (cur) {
464 memmove(baum->in_buf, baum->in_buf + cur, baum->in_buf_used - cur);
465 baum->in_buf_used -= cur;
468 /* And continue if any data left */
470 return orig_len;
473 /* The other end sent us some event */
474 static void baum_send_event(CharDriverState *chr, int event)
476 BaumDriverState *baum = chr->opaque;
477 switch (event) {
478 case CHR_EVENT_BREAK:
479 break;
480 case CHR_EVENT_OPENED:
481 /* Reset state */
482 baum->in_buf_used = 0;
483 break;
487 /* Send the key code to the other end */
488 static void baum_send_key(BaumDriverState *baum, uint8_t type, uint8_t value) {
489 uint8_t packet[] = { type, value };
490 DPRINTF("writing key %x %x\n", type, value);
491 baum_write_packet(baum, packet, sizeof(packet));
494 /* We got some data on the BrlAPI socket */
495 static void baum_chr_read(void *opaque)
497 BaumDriverState *baum = opaque;
498 brlapi_keyCode_t code;
499 int ret;
500 if (!baum->brlapi)
501 return;
502 while ((ret = brlapi__readKey(baum->brlapi, 0, &code)) == 1) {
503 DPRINTF("got key %"BRLAPI_PRIxKEYCODE"\n", code);
504 /* Emulate */
505 switch (code & BRLAPI_KEY_TYPE_MASK) {
506 case BRLAPI_KEY_TYPE_CMD:
507 switch (code & BRLAPI_KEY_CMD_BLK_MASK) {
508 case BRLAPI_KEY_CMD_ROUTE:
509 baum_send_key(baum, BAUM_RSP_RoutingKey, (code & BRLAPI_KEY_CMD_ARG_MASK)+1);
510 baum_send_key(baum, BAUM_RSP_RoutingKey, 0);
511 break;
512 case 0:
513 switch (code & BRLAPI_KEY_CMD_ARG_MASK) {
514 case BRLAPI_KEY_CMD_FWINLT:
515 baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL2);
516 baum_send_key(baum, BAUM_RSP_TopKeys, 0);
517 break;
518 case BRLAPI_KEY_CMD_FWINRT:
519 baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TR2);
520 baum_send_key(baum, BAUM_RSP_TopKeys, 0);
521 break;
522 case BRLAPI_KEY_CMD_LNUP:
523 baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TR1);
524 baum_send_key(baum, BAUM_RSP_TopKeys, 0);
525 break;
526 case BRLAPI_KEY_CMD_LNDN:
527 baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TR3);
528 baum_send_key(baum, BAUM_RSP_TopKeys, 0);
529 break;
530 case BRLAPI_KEY_CMD_TOP:
531 baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL1|BAUM_TR1);
532 baum_send_key(baum, BAUM_RSP_TopKeys, 0);
533 break;
534 case BRLAPI_KEY_CMD_BOT:
535 baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL3|BAUM_TR3);
536 baum_send_key(baum, BAUM_RSP_TopKeys, 0);
537 break;
538 case BRLAPI_KEY_CMD_TOP_LEFT:
539 baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL2|BAUM_TR1);
540 baum_send_key(baum, BAUM_RSP_TopKeys, 0);
541 break;
542 case BRLAPI_KEY_CMD_BOT_LEFT:
543 baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL2|BAUM_TR3);
544 baum_send_key(baum, BAUM_RSP_TopKeys, 0);
545 break;
546 case BRLAPI_KEY_CMD_HOME:
547 baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL2|BAUM_TR1|BAUM_TR3);
548 baum_send_key(baum, BAUM_RSP_TopKeys, 0);
549 break;
550 case BRLAPI_KEY_CMD_PREFMENU:
551 baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL1|BAUM_TL3|BAUM_TR1);
552 baum_send_key(baum, BAUM_RSP_TopKeys, 0);
553 break;
556 break;
557 case BRLAPI_KEY_TYPE_SYM:
558 break;
561 if (ret == -1 && (brlapi_errno != BRLAPI_ERROR_LIBCERR || errno != EINTR)) {
562 brlapi_perror("baum: brlapi_readKey");
563 brlapi__closeConnection(baum->brlapi);
564 qemu_free(baum->brlapi);
565 baum->brlapi = NULL;
569 static void baum_close(struct CharDriverState *chr)
571 BaumDriverState *baum = chr->opaque;
573 qemu_free_timer(baum->cellCount_timer);
574 if (baum->brlapi) {
575 brlapi__closeConnection(baum->brlapi);
576 qemu_free(baum->brlapi);
578 qemu_free(baum);
581 int chr_baum_init(QemuOpts *opts, CharDriverState **_chr)
583 BaumDriverState *baum;
584 CharDriverState *chr;
585 brlapi_handle_t *handle;
586 #ifdef CONFIG_SDL
587 SDL_SysWMinfo info;
588 #endif
589 int tty;
591 baum = qemu_mallocz(sizeof(BaumDriverState));
592 baum->chr = chr = qemu_mallocz(sizeof(CharDriverState));
594 chr->opaque = baum;
595 chr->chr_write = baum_write;
596 chr->chr_send_event = baum_send_event;
597 chr->chr_close = baum_close;
599 handle = qemu_mallocz(brlapi_getHandleSize());
600 baum->brlapi = handle;
602 baum->brlapi_fd = brlapi__openConnection(handle, NULL, NULL);
603 if (baum->brlapi_fd == -1) {
604 brlapi_perror("baum_init: brlapi_openConnection");
605 goto fail_handle;
608 baum->cellCount_timer = qemu_new_timer_ns(vm_clock, baum_cellCount_timer_cb, baum);
610 if (brlapi__getDisplaySize(handle, &baum->x, &baum->y) == -1) {
611 brlapi_perror("baum_init: brlapi_getDisplaySize");
612 goto fail;
615 #ifdef CONFIG_SDL
616 memset(&info, 0, sizeof(info));
617 SDL_VERSION(&info.version);
618 if (SDL_GetWMInfo(&info))
619 tty = info.info.x11.wmwindow;
620 else
621 #endif
622 tty = BRLAPI_TTY_DEFAULT;
624 if (brlapi__enterTtyMode(handle, tty, NULL) == -1) {
625 brlapi_perror("baum_init: brlapi_enterTtyMode");
626 goto fail;
629 qemu_set_fd_handler(baum->brlapi_fd, baum_chr_read, NULL, baum);
631 qemu_chr_generic_open(chr);
633 *_chr = chr;
634 return 0;
636 fail:
637 qemu_free_timer(baum->cellCount_timer);
638 brlapi__closeConnection(handle);
639 fail_handle:
640 qemu_free(handle);
641 qemu_free(chr);
642 qemu_free(baum);
643 return -EIO;