baum.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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 "ui/console.h"
  31. #include <brlapi.h>
  32. #include <brlapi_constants.h>
  33. #include <brlapi_keycodes.h>
  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. Chardev parent;
  82. brlapi_handle_t *brlapi;
  83. int brlapi_fd;
  84. unsigned int x, y;
  85. bool deferred_init;
  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. } BaumChardev;
  92. #define TYPE_CHARDEV_BRAILLE "chardev-braille"
  93. #define BAUM_CHARDEV(obj) OBJECT_CHECK(BaumChardev, (obj), TYPE_CHARDEV_BRAILLE)
  94. /* Let's assume NABCC by default */
  95. enum way {
  96. DOTS2ASCII,
  97. ASCII2DOTS
  98. };
  99. static const uint8_t nabcc_translation[2][256] = {
  100. #ifndef BRLAPI_DOTS
  101. #define BRLAPI_DOTS(d1,d2,d3,d4,d5,d6,d7,d8) \
  102. ((d1?BRLAPI_DOT1:0)|\
  103. (d2?BRLAPI_DOT2:0)|\
  104. (d3?BRLAPI_DOT3:0)|\
  105. (d4?BRLAPI_DOT4:0)|\
  106. (d5?BRLAPI_DOT5:0)|\
  107. (d6?BRLAPI_DOT6:0)|\
  108. (d7?BRLAPI_DOT7:0)|\
  109. (d8?BRLAPI_DOT8:0))
  110. #endif
  111. #define DO(dots, ascii) \
  112. [DOTS2ASCII][dots] = ascii, \
  113. [ASCII2DOTS][ascii] = dots
  114. DO(0, ' '),
  115. DO(BRLAPI_DOTS(1, 0, 0, 0, 0, 0, 0, 0), 'a'),
  116. DO(BRLAPI_DOTS(1, 1, 0, 0, 0, 0, 0, 0), 'b'),
  117. DO(BRLAPI_DOTS(1, 0, 0, 1, 0, 0, 0, 0), 'c'),
  118. DO(BRLAPI_DOTS(1, 0, 0, 1, 1, 0, 0, 0), 'd'),
  119. DO(BRLAPI_DOTS(1, 0, 0, 0, 1, 0, 0, 0), 'e'),
  120. DO(BRLAPI_DOTS(1, 1, 0, 1, 0, 0, 0, 0), 'f'),
  121. DO(BRLAPI_DOTS(1, 1, 0, 1, 1, 0, 0, 0), 'g'),
  122. DO(BRLAPI_DOTS(1, 1, 0, 0, 1, 0, 0, 0), 'h'),
  123. DO(BRLAPI_DOTS(0, 1, 0, 1, 0, 0, 0, 0), 'i'),
  124. DO(BRLAPI_DOTS(0, 1, 0, 1, 1, 0, 0, 0), 'j'),
  125. DO(BRLAPI_DOTS(1, 0, 1, 0, 0, 0, 0, 0), 'k'),
  126. DO(BRLAPI_DOTS(1, 1, 1, 0, 0, 0, 0, 0), 'l'),
  127. DO(BRLAPI_DOTS(1, 0, 1, 1, 0, 0, 0, 0), 'm'),
  128. DO(BRLAPI_DOTS(1, 0, 1, 1, 1, 0, 0, 0), 'n'),
  129. DO(BRLAPI_DOTS(1, 0, 1, 0, 1, 0, 0, 0), 'o'),
  130. DO(BRLAPI_DOTS(1, 1, 1, 1, 0, 0, 0, 0), 'p'),
  131. DO(BRLAPI_DOTS(1, 1, 1, 1, 1, 0, 0, 0), 'q'),
  132. DO(BRLAPI_DOTS(1, 1, 1, 0, 1, 0, 0, 0), 'r'),
  133. DO(BRLAPI_DOTS(0, 1, 1, 1, 0, 0, 0, 0), 's'),
  134. DO(BRLAPI_DOTS(0, 1, 1, 1, 1, 0, 0, 0), 't'),
  135. DO(BRLAPI_DOTS(1, 0, 1, 0, 0, 1, 0, 0), 'u'),
  136. DO(BRLAPI_DOTS(1, 1, 1, 0, 0, 1, 0, 0), 'v'),
  137. DO(BRLAPI_DOTS(0, 1, 0, 1, 1, 1, 0, 0), 'w'),
  138. DO(BRLAPI_DOTS(1, 0, 1, 1, 0, 1, 0, 0), 'x'),
  139. DO(BRLAPI_DOTS(1, 0, 1, 1, 1, 1, 0, 0), 'y'),
  140. DO(BRLAPI_DOTS(1, 0, 1, 0, 1, 1, 0, 0), 'z'),
  141. DO(BRLAPI_DOTS(1, 0, 0, 0, 0, 0, 1, 0), 'A'),
  142. DO(BRLAPI_DOTS(1, 1, 0, 0, 0, 0, 1, 0), 'B'),
  143. DO(BRLAPI_DOTS(1, 0, 0, 1, 0, 0, 1, 0), 'C'),
  144. DO(BRLAPI_DOTS(1, 0, 0, 1, 1, 0, 1, 0), 'D'),
  145. DO(BRLAPI_DOTS(1, 0, 0, 0, 1, 0, 1, 0), 'E'),
  146. DO(BRLAPI_DOTS(1, 1, 0, 1, 0, 0, 1, 0), 'F'),
  147. DO(BRLAPI_DOTS(1, 1, 0, 1, 1, 0, 1, 0), 'G'),
  148. DO(BRLAPI_DOTS(1, 1, 0, 0, 1, 0, 1, 0), 'H'),
  149. DO(BRLAPI_DOTS(0, 1, 0, 1, 0, 0, 1, 0), 'I'),
  150. DO(BRLAPI_DOTS(0, 1, 0, 1, 1, 0, 1, 0), 'J'),
  151. DO(BRLAPI_DOTS(1, 0, 1, 0, 0, 0, 1, 0), 'K'),
  152. DO(BRLAPI_DOTS(1, 1, 1, 0, 0, 0, 1, 0), 'L'),
  153. DO(BRLAPI_DOTS(1, 0, 1, 1, 0, 0, 1, 0), 'M'),
  154. DO(BRLAPI_DOTS(1, 0, 1, 1, 1, 0, 1, 0), 'N'),
  155. DO(BRLAPI_DOTS(1, 0, 1, 0, 1, 0, 1, 0), 'O'),
  156. DO(BRLAPI_DOTS(1, 1, 1, 1, 0, 0, 1, 0), 'P'),
  157. DO(BRLAPI_DOTS(1, 1, 1, 1, 1, 0, 1, 0), 'Q'),
  158. DO(BRLAPI_DOTS(1, 1, 1, 0, 1, 0, 1, 0), 'R'),
  159. DO(BRLAPI_DOTS(0, 1, 1, 1, 0, 0, 1, 0), 'S'),
  160. DO(BRLAPI_DOTS(0, 1, 1, 1, 1, 0, 1, 0), 'T'),
  161. DO(BRLAPI_DOTS(1, 0, 1, 0, 0, 1, 1, 0), 'U'),
  162. DO(BRLAPI_DOTS(1, 1, 1, 0, 0, 1, 1, 0), 'V'),
  163. DO(BRLAPI_DOTS(0, 1, 0, 1, 1, 1, 1, 0), 'W'),
  164. DO(BRLAPI_DOTS(1, 0, 1, 1, 0, 1, 1, 0), 'X'),
  165. DO(BRLAPI_DOTS(1, 0, 1, 1, 1, 1, 1, 0), 'Y'),
  166. DO(BRLAPI_DOTS(1, 0, 1, 0, 1, 1, 1, 0), 'Z'),
  167. DO(BRLAPI_DOTS(0, 0, 1, 0, 1, 1, 0, 0), '0'),
  168. DO(BRLAPI_DOTS(0, 1, 0, 0, 0, 0, 0, 0), '1'),
  169. DO(BRLAPI_DOTS(0, 1, 1, 0, 0, 0, 0, 0), '2'),
  170. DO(BRLAPI_DOTS(0, 1, 0, 0, 1, 0, 0, 0), '3'),
  171. DO(BRLAPI_DOTS(0, 1, 0, 0, 1, 1, 0, 0), '4'),
  172. DO(BRLAPI_DOTS(0, 1, 0, 0, 0, 1, 0, 0), '5'),
  173. DO(BRLAPI_DOTS(0, 1, 1, 0, 1, 0, 0, 0), '6'),
  174. DO(BRLAPI_DOTS(0, 1, 1, 0, 1, 1, 0, 0), '7'),
  175. DO(BRLAPI_DOTS(0, 1, 1, 0, 0, 1, 0, 0), '8'),
  176. DO(BRLAPI_DOTS(0, 0, 1, 0, 1, 0, 0, 0), '9'),
  177. DO(BRLAPI_DOTS(0, 0, 0, 1, 0, 1, 0, 0), '.'),
  178. DO(BRLAPI_DOTS(0, 0, 1, 1, 0, 1, 0, 0), '+'),
  179. DO(BRLAPI_DOTS(0, 0, 1, 0, 0, 1, 0, 0), '-'),
  180. DO(BRLAPI_DOTS(1, 0, 0, 0, 0, 1, 0, 0), '*'),
  181. DO(BRLAPI_DOTS(0, 0, 1, 1, 0, 0, 0, 0), '/'),
  182. DO(BRLAPI_DOTS(1, 1, 1, 0, 1, 1, 0, 0), '('),
  183. DO(BRLAPI_DOTS(0, 1, 1, 1, 1, 1, 0, 0), ')'),
  184. DO(BRLAPI_DOTS(1, 1, 1, 1, 0, 1, 0, 0), '&'),
  185. DO(BRLAPI_DOTS(0, 0, 1, 1, 1, 1, 0, 0), '#'),
  186. DO(BRLAPI_DOTS(0, 0, 0, 0, 0, 1, 0, 0), ','),
  187. DO(BRLAPI_DOTS(0, 0, 0, 0, 1, 1, 0, 0), ';'),
  188. DO(BRLAPI_DOTS(1, 0, 0, 0, 1, 1, 0, 0), ':'),
  189. DO(BRLAPI_DOTS(0, 1, 1, 1, 0, 1, 0, 0), '!'),
  190. DO(BRLAPI_DOTS(1, 0, 0, 1, 1, 1, 0, 0), '?'),
  191. DO(BRLAPI_DOTS(0, 0, 0, 0, 1, 0, 0, 0), '"'),
  192. DO(BRLAPI_DOTS(0, 0, 1, 0, 0, 0, 0, 0), '\''),
  193. DO(BRLAPI_DOTS(0, 0, 0, 1, 0, 0, 0, 0), '`'),
  194. DO(BRLAPI_DOTS(0, 0, 0, 1, 1, 0, 1, 0), '^'),
  195. DO(BRLAPI_DOTS(0, 0, 0, 1, 1, 0, 0, 0), '~'),
  196. DO(BRLAPI_DOTS(0, 1, 0, 1, 0, 1, 1, 0), '['),
  197. DO(BRLAPI_DOTS(1, 1, 0, 1, 1, 1, 1, 0), ']'),
  198. DO(BRLAPI_DOTS(0, 1, 0, 1, 0, 1, 0, 0), '{'),
  199. DO(BRLAPI_DOTS(1, 1, 0, 1, 1, 1, 0, 0), '}'),
  200. DO(BRLAPI_DOTS(1, 1, 1, 1, 1, 1, 0, 0), '='),
  201. DO(BRLAPI_DOTS(1, 1, 0, 0, 0, 1, 0, 0), '<'),
  202. DO(BRLAPI_DOTS(0, 0, 1, 1, 1, 0, 0, 0), '>'),
  203. DO(BRLAPI_DOTS(1, 1, 0, 1, 0, 1, 0, 0), '$'),
  204. DO(BRLAPI_DOTS(1, 0, 0, 1, 0, 1, 0, 0), '%'),
  205. DO(BRLAPI_DOTS(0, 0, 0, 1, 0, 0, 1, 0), '@'),
  206. DO(BRLAPI_DOTS(1, 1, 0, 0, 1, 1, 0, 0), '|'),
  207. DO(BRLAPI_DOTS(1, 1, 0, 0, 1, 1, 1, 0), '\\'),
  208. DO(BRLAPI_DOTS(0, 0, 0, 1, 1, 1, 0, 0), '_'),
  209. };
  210. /* The guest OS has started discussing with us, finish initializing BrlAPI */
  211. static int baum_deferred_init(BaumChardev *baum)
  212. {
  213. int tty = BRLAPI_TTY_DEFAULT;
  214. QemuConsole *con;
  215. if (baum->deferred_init) {
  216. return 1;
  217. }
  218. if (brlapi__getDisplaySize(baum->brlapi, &baum->x, &baum->y) == -1) {
  219. brlapi_perror("baum: brlapi__getDisplaySize");
  220. return 0;
  221. }
  222. con = qemu_console_lookup_by_index(0);
  223. if (con && qemu_console_is_graphic(con)) {
  224. tty = qemu_console_get_window_id(con);
  225. if (tty == -1)
  226. tty = BRLAPI_TTY_DEFAULT;
  227. }
  228. if (brlapi__enterTtyMode(baum->brlapi, tty, NULL) == -1) {
  229. brlapi_perror("baum: brlapi__enterTtyMode");
  230. return 0;
  231. }
  232. baum->deferred_init = 1;
  233. return 1;
  234. }
  235. /* The serial port can receive more of our data */
  236. static void baum_chr_accept_input(struct Chardev *chr)
  237. {
  238. BaumChardev *baum = BAUM_CHARDEV(chr);
  239. int room, first;
  240. if (!baum->out_buf_used)
  241. return;
  242. room = qemu_chr_be_can_write(chr);
  243. if (!room)
  244. return;
  245. if (room > baum->out_buf_used)
  246. room = baum->out_buf_used;
  247. first = BUF_SIZE - baum->out_buf_ptr;
  248. if (room > first) {
  249. qemu_chr_be_write(chr, baum->out_buf + baum->out_buf_ptr, first);
  250. baum->out_buf_ptr = 0;
  251. baum->out_buf_used -= first;
  252. room -= first;
  253. }
  254. qemu_chr_be_write(chr, baum->out_buf + baum->out_buf_ptr, room);
  255. baum->out_buf_ptr += room;
  256. baum->out_buf_used -= room;
  257. }
  258. /* We want to send a packet */
  259. static void baum_write_packet(BaumChardev *baum, const uint8_t *buf, int len)
  260. {
  261. Chardev *chr = CHARDEV(baum);
  262. uint8_t io_buf[1 + 2 * len], *cur = io_buf;
  263. int room;
  264. *cur++ = ESC;
  265. while (len--)
  266. if ((*cur++ = *buf++) == ESC)
  267. *cur++ = ESC;
  268. room = qemu_chr_be_can_write(chr);
  269. len = cur - io_buf;
  270. if (len <= room) {
  271. /* Fits */
  272. qemu_chr_be_write(chr, io_buf, len);
  273. } else {
  274. int first;
  275. uint8_t out;
  276. /* Can't fit all, send what can be, and store the rest. */
  277. qemu_chr_be_write(chr, io_buf, room);
  278. len -= room;
  279. cur = io_buf + room;
  280. if (len > BUF_SIZE - baum->out_buf_used) {
  281. /* Can't even store it, drop the previous data... */
  282. assert(len <= BUF_SIZE);
  283. baum->out_buf_used = 0;
  284. baum->out_buf_ptr = 0;
  285. }
  286. out = baum->out_buf_ptr;
  287. baum->out_buf_used += len;
  288. first = BUF_SIZE - baum->out_buf_ptr;
  289. if (len > first) {
  290. memcpy(baum->out_buf + out, cur, first);
  291. out = 0;
  292. len -= first;
  293. cur += first;
  294. }
  295. memcpy(baum->out_buf + out, cur, len);
  296. }
  297. }
  298. /* Called when the other end seems to have a wrong idea of our display size */
  299. static void baum_cellCount_timer_cb(void *opaque)
  300. {
  301. BaumChardev *baum = BAUM_CHARDEV(opaque);
  302. uint8_t cell_count[] = { BAUM_RSP_CellCount, baum->x * baum->y };
  303. DPRINTF("Timeout waiting for DisplayData, sending cell count\n");
  304. baum_write_packet(baum, cell_count, sizeof(cell_count));
  305. }
  306. /* Try to interpret a whole incoming packet */
  307. static int baum_eat_packet(BaumChardev *baum, const uint8_t *buf, int len)
  308. {
  309. const uint8_t *cur = buf;
  310. uint8_t req = 0;
  311. if (!len--)
  312. return 0;
  313. if (*cur++ != ESC) {
  314. while (*cur != ESC) {
  315. if (!len--)
  316. return 0;
  317. cur++;
  318. }
  319. DPRINTF("Dropped %td bytes!\n", cur - buf);
  320. }
  321. #define EAT(c) do {\
  322. if (!len--) \
  323. return 0; \
  324. if ((c = *cur++) == ESC) { \
  325. if (!len--) \
  326. return 0; \
  327. if (*cur++ != ESC) { \
  328. DPRINTF("Broken packet %#2x, tossing\n", req); \
  329. if (timer_pending(baum->cellCount_timer)) { \
  330. timer_del(baum->cellCount_timer); \
  331. baum_cellCount_timer_cb(baum); \
  332. } \
  333. return (cur - 2 - buf); \
  334. } \
  335. } \
  336. } while (0)
  337. EAT(req);
  338. switch (req) {
  339. case BAUM_REQ_DisplayData:
  340. {
  341. uint8_t cells[baum->x * baum->y], c;
  342. uint8_t text[baum->x * baum->y];
  343. uint8_t zero[baum->x * baum->y];
  344. int cursor = BRLAPI_CURSOR_OFF;
  345. int i;
  346. /* Allow 100ms to complete the DisplayData packet */
  347. timer_mod(baum->cellCount_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  348. NANOSECONDS_PER_SECOND / 10);
  349. for (i = 0; i < baum->x * baum->y ; i++) {
  350. EAT(c);
  351. cells[i] = c;
  352. if ((c & (BRLAPI_DOT7|BRLAPI_DOT8))
  353. == (BRLAPI_DOT7|BRLAPI_DOT8)) {
  354. cursor = i + 1;
  355. c &= ~(BRLAPI_DOT7|BRLAPI_DOT8);
  356. }
  357. c = nabcc_translation[DOTS2ASCII][c];
  358. if (!c) {
  359. c = '?';
  360. }
  361. text[i] = c;
  362. }
  363. timer_del(baum->cellCount_timer);
  364. memset(zero, 0, sizeof(zero));
  365. brlapi_writeArguments_t wa = {
  366. .displayNumber = BRLAPI_DISPLAY_DEFAULT,
  367. .regionBegin = 1,
  368. .regionSize = baum->x * baum->y,
  369. .text = (char *)text,
  370. .textSize = baum->x * baum->y,
  371. .andMask = zero,
  372. .orMask = cells,
  373. .cursor = cursor,
  374. .charset = (char *)"ISO-8859-1",
  375. };
  376. if (brlapi__write(baum->brlapi, &wa) == -1)
  377. brlapi_perror("baum brlapi_write");
  378. break;
  379. }
  380. case BAUM_REQ_SetMode:
  381. {
  382. uint8_t mode, setting;
  383. DPRINTF("SetMode\n");
  384. EAT(mode);
  385. EAT(setting);
  386. /* ignore */
  387. break;
  388. }
  389. case BAUM_REQ_SetProtocol:
  390. {
  391. uint8_t protocol;
  392. DPRINTF("SetProtocol\n");
  393. EAT(protocol);
  394. /* ignore */
  395. break;
  396. }
  397. case BAUM_REQ_GetDeviceIdentity:
  398. {
  399. uint8_t identity[17] = { BAUM_RSP_DeviceIdentity,
  400. 'B','a','u','m',' ','V','a','r','i','o' };
  401. DPRINTF("GetDeviceIdentity\n");
  402. identity[11] = '0' + baum->x / 10;
  403. identity[12] = '0' + baum->x % 10;
  404. baum_write_packet(baum, identity, sizeof(identity));
  405. break;
  406. }
  407. case BAUM_REQ_GetVersionNumber:
  408. {
  409. uint8_t version[] = { BAUM_RSP_VersionNumber, 1 }; /* ? */
  410. DPRINTF("GetVersionNumber\n");
  411. baum_write_packet(baum, version, sizeof(version));
  412. break;
  413. }
  414. case BAUM_REQ_GetSerialNumber:
  415. {
  416. uint8_t serial[] = { BAUM_RSP_SerialNumber,
  417. '0','0','0','0','0','0','0','0' };
  418. DPRINTF("GetSerialNumber\n");
  419. baum_write_packet(baum, serial, sizeof(serial));
  420. break;
  421. }
  422. case BAUM_REQ_GetKeys:
  423. {
  424. DPRINTF("Get%0#2x\n", req);
  425. /* ignore */
  426. break;
  427. }
  428. default:
  429. DPRINTF("unrecognized request %0#2x\n", req);
  430. do
  431. if (!len--)
  432. return 0;
  433. while (*cur++ != ESC);
  434. cur--;
  435. break;
  436. }
  437. return cur - buf;
  438. }
  439. /* The other end is writing some data. Store it and try to interpret */
  440. static int baum_chr_write(Chardev *chr, const uint8_t *buf, int len)
  441. {
  442. BaumChardev *baum = BAUM_CHARDEV(chr);
  443. int tocopy, cur, eaten, orig_len = len;
  444. if (!len)
  445. return 0;
  446. if (!baum->brlapi)
  447. return len;
  448. if (!baum_deferred_init(baum))
  449. return len;
  450. while (len) {
  451. /* Complete our buffer as much as possible */
  452. tocopy = len;
  453. if (tocopy > BUF_SIZE - baum->in_buf_used)
  454. tocopy = BUF_SIZE - baum->in_buf_used;
  455. memcpy(baum->in_buf + baum->in_buf_used, buf, tocopy);
  456. baum->in_buf_used += tocopy;
  457. buf += tocopy;
  458. len -= tocopy;
  459. /* Interpret it as much as possible */
  460. cur = 0;
  461. while (cur < baum->in_buf_used &&
  462. (eaten = baum_eat_packet(baum, baum->in_buf + cur, baum->in_buf_used - cur)))
  463. cur += eaten;
  464. /* Shift the remainder */
  465. if (cur) {
  466. memmove(baum->in_buf, baum->in_buf + cur, baum->in_buf_used - cur);
  467. baum->in_buf_used -= cur;
  468. }
  469. /* And continue if any data left */
  470. }
  471. return orig_len;
  472. }
  473. /* Send the key code to the other end */
  474. static void baum_send_key(BaumChardev *baum, uint8_t type, uint8_t value)
  475. {
  476. uint8_t packet[] = { type, value };
  477. DPRINTF("writing key %x %x\n", type, value);
  478. baum_write_packet(baum, packet, sizeof(packet));
  479. }
  480. static void baum_send_key2(BaumChardev *baum, uint8_t type, uint8_t value,
  481. uint8_t value2)
  482. {
  483. uint8_t packet[] = { type, value, value2 };
  484. DPRINTF("writing key %x %x\n", type, value);
  485. baum_write_packet(baum, packet, sizeof(packet));
  486. }
  487. /* We got some data on the BrlAPI socket */
  488. static void baum_chr_read(void *opaque)
  489. {
  490. BaumChardev *baum = BAUM_CHARDEV(opaque);
  491. brlapi_keyCode_t code;
  492. int ret;
  493. if (!baum->brlapi)
  494. return;
  495. if (!baum_deferred_init(baum))
  496. return;
  497. while ((ret = brlapi__readKey(baum->brlapi, 0, &code)) == 1) {
  498. DPRINTF("got key %"BRLAPI_PRIxKEYCODE"\n", code);
  499. /* Emulate */
  500. switch (code & BRLAPI_KEY_TYPE_MASK) {
  501. case BRLAPI_KEY_TYPE_CMD:
  502. switch (code & BRLAPI_KEY_CMD_BLK_MASK) {
  503. case BRLAPI_KEY_CMD_ROUTE:
  504. baum_send_key(baum, BAUM_RSP_RoutingKey, (code & BRLAPI_KEY_CMD_ARG_MASK)+1);
  505. baum_send_key(baum, BAUM_RSP_RoutingKey, 0);
  506. break;
  507. case 0:
  508. switch (code & BRLAPI_KEY_CMD_ARG_MASK) {
  509. case BRLAPI_KEY_CMD_FWINLT:
  510. baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL2);
  511. baum_send_key(baum, BAUM_RSP_TopKeys, 0);
  512. break;
  513. case BRLAPI_KEY_CMD_FWINRT:
  514. baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TR2);
  515. baum_send_key(baum, BAUM_RSP_TopKeys, 0);
  516. break;
  517. case BRLAPI_KEY_CMD_LNUP:
  518. baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TR1);
  519. baum_send_key(baum, BAUM_RSP_TopKeys, 0);
  520. break;
  521. case BRLAPI_KEY_CMD_LNDN:
  522. baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TR3);
  523. baum_send_key(baum, BAUM_RSP_TopKeys, 0);
  524. break;
  525. case BRLAPI_KEY_CMD_TOP:
  526. baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL1|BAUM_TR1);
  527. baum_send_key(baum, BAUM_RSP_TopKeys, 0);
  528. break;
  529. case BRLAPI_KEY_CMD_BOT:
  530. baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL3|BAUM_TR3);
  531. baum_send_key(baum, BAUM_RSP_TopKeys, 0);
  532. break;
  533. case BRLAPI_KEY_CMD_TOP_LEFT:
  534. baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL2|BAUM_TR1);
  535. baum_send_key(baum, BAUM_RSP_TopKeys, 0);
  536. break;
  537. case BRLAPI_KEY_CMD_BOT_LEFT:
  538. baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL2|BAUM_TR3);
  539. baum_send_key(baum, BAUM_RSP_TopKeys, 0);
  540. break;
  541. case BRLAPI_KEY_CMD_HOME:
  542. baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL2|BAUM_TR1|BAUM_TR3);
  543. baum_send_key(baum, BAUM_RSP_TopKeys, 0);
  544. break;
  545. case BRLAPI_KEY_CMD_PREFMENU:
  546. baum_send_key(baum, BAUM_RSP_TopKeys, BAUM_TL1|BAUM_TL3|BAUM_TR1);
  547. baum_send_key(baum, BAUM_RSP_TopKeys, 0);
  548. break;
  549. }
  550. }
  551. break;
  552. case BRLAPI_KEY_TYPE_SYM:
  553. {
  554. brlapi_keyCode_t keysym = code & BRLAPI_KEY_CODE_MASK;
  555. if (keysym < 0x100) {
  556. uint8_t dots = nabcc_translation[ASCII2DOTS][keysym];
  557. if (dots) {
  558. baum_send_key2(baum, BAUM_RSP_EntryKeys, 0, dots);
  559. baum_send_key2(baum, BAUM_RSP_EntryKeys, 0, 0);
  560. }
  561. }
  562. break;
  563. }
  564. }
  565. }
  566. if (ret == -1 && (brlapi_errno != BRLAPI_ERROR_LIBCERR || errno != EINTR)) {
  567. brlapi_perror("baum: brlapi_readKey");
  568. brlapi__closeConnection(baum->brlapi);
  569. g_free(baum->brlapi);
  570. baum->brlapi = NULL;
  571. }
  572. }
  573. static void char_braille_finalize(Object *obj)
  574. {
  575. BaumChardev *baum = BAUM_CHARDEV(obj);
  576. timer_free(baum->cellCount_timer);
  577. if (baum->brlapi) {
  578. brlapi__closeConnection(baum->brlapi);
  579. g_free(baum->brlapi);
  580. }
  581. }
  582. static void baum_chr_open(Chardev *chr,
  583. ChardevBackend *backend,
  584. bool *be_opened,
  585. Error **errp)
  586. {
  587. BaumChardev *baum = BAUM_CHARDEV(chr);
  588. brlapi_handle_t *handle;
  589. handle = g_malloc0(brlapi_getHandleSize());
  590. baum->brlapi = handle;
  591. baum->brlapi_fd = brlapi__openConnection(handle, NULL, NULL);
  592. if (baum->brlapi_fd == -1) {
  593. error_setg(errp, "brlapi__openConnection: %s",
  594. brlapi_strerror(brlapi_error_location()));
  595. g_free(handle);
  596. return;
  597. }
  598. baum->deferred_init = 0;
  599. baum->cellCount_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, baum_cellCount_timer_cb, baum);
  600. qemu_set_fd_handler(baum->brlapi_fd, baum_chr_read, NULL, baum);
  601. }
  602. static void char_braille_class_init(ObjectClass *oc, void *data)
  603. {
  604. ChardevClass *cc = CHARDEV_CLASS(oc);
  605. cc->open = baum_chr_open;
  606. cc->chr_write = baum_chr_write;
  607. cc->chr_accept_input = baum_chr_accept_input;
  608. }
  609. static const TypeInfo char_braille_type_info = {
  610. .name = TYPE_CHARDEV_BRAILLE,
  611. .parent = TYPE_CHARDEV,
  612. .instance_size = sizeof(BaumChardev),
  613. .instance_finalize = char_braille_finalize,
  614. .class_init = char_braille_class_init,
  615. };
  616. static void register_types(void)
  617. {
  618. type_register_static(&char_braille_type_info);
  619. }
  620. type_init(register_types);