baum.c 23 KB

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