baum.c 20 KB

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