2
0

baum.c 20 KB

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