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