adb-mouse.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * QEMU ADB mouse support
  3. *
  4. * Copyright (c) 2004 Fabrice Bellard
  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 "ui/console.h"
  26. #include "hw/input/adb.h"
  27. #include "migration/vmstate.h"
  28. #include "qemu/module.h"
  29. #include "adb-internal.h"
  30. #include "trace.h"
  31. #define ADB_MOUSE(obj) OBJECT_CHECK(MouseState, (obj), TYPE_ADB_MOUSE)
  32. typedef struct MouseState {
  33. /*< public >*/
  34. ADBDevice parent_obj;
  35. /*< private >*/
  36. int buttons_state, last_buttons_state;
  37. int dx, dy, dz;
  38. } MouseState;
  39. #define ADB_MOUSE_CLASS(class) \
  40. OBJECT_CLASS_CHECK(ADBMouseClass, (class), TYPE_ADB_MOUSE)
  41. #define ADB_MOUSE_GET_CLASS(obj) \
  42. OBJECT_GET_CLASS(ADBMouseClass, (obj), TYPE_ADB_MOUSE)
  43. typedef struct ADBMouseClass {
  44. /*< public >*/
  45. ADBDeviceClass parent_class;
  46. /*< private >*/
  47. DeviceRealize parent_realize;
  48. } ADBMouseClass;
  49. static void adb_mouse_event(void *opaque,
  50. int dx1, int dy1, int dz1, int buttons_state)
  51. {
  52. MouseState *s = opaque;
  53. s->dx += dx1;
  54. s->dy += dy1;
  55. s->dz += dz1;
  56. s->buttons_state = buttons_state;
  57. }
  58. static int adb_mouse_poll(ADBDevice *d, uint8_t *obuf)
  59. {
  60. MouseState *s = ADB_MOUSE(d);
  61. int dx, dy;
  62. if (s->last_buttons_state == s->buttons_state &&
  63. s->dx == 0 && s->dy == 0) {
  64. return 0;
  65. }
  66. dx = s->dx;
  67. if (dx < -63) {
  68. dx = -63;
  69. } else if (dx > 63) {
  70. dx = 63;
  71. }
  72. dy = s->dy;
  73. if (dy < -63) {
  74. dy = -63;
  75. } else if (dy > 63) {
  76. dy = 63;
  77. }
  78. s->dx -= dx;
  79. s->dy -= dy;
  80. s->last_buttons_state = s->buttons_state;
  81. dx &= 0x7f;
  82. dy &= 0x7f;
  83. if (!(s->buttons_state & MOUSE_EVENT_LBUTTON)) {
  84. dy |= 0x80;
  85. }
  86. if (!(s->buttons_state & MOUSE_EVENT_RBUTTON)) {
  87. dx |= 0x80;
  88. }
  89. obuf[0] = dy;
  90. obuf[1] = dx;
  91. return 2;
  92. }
  93. static int adb_mouse_request(ADBDevice *d, uint8_t *obuf,
  94. const uint8_t *buf, int len)
  95. {
  96. MouseState *s = ADB_MOUSE(d);
  97. int cmd, reg, olen;
  98. if ((buf[0] & 0x0f) == ADB_FLUSH) {
  99. /* flush mouse fifo */
  100. s->buttons_state = s->last_buttons_state;
  101. s->dx = 0;
  102. s->dy = 0;
  103. s->dz = 0;
  104. trace_adb_device_mouse_flush();
  105. return 0;
  106. }
  107. cmd = buf[0] & 0xc;
  108. reg = buf[0] & 0x3;
  109. olen = 0;
  110. switch (cmd) {
  111. case ADB_WRITEREG:
  112. trace_adb_device_mouse_writereg(reg, buf[1]);
  113. switch (reg) {
  114. case 2:
  115. break;
  116. case 3:
  117. /*
  118. * MacOS 9 has a bug in its ADB driver whereby after configuring
  119. * the ADB bus devices it sends another write of invalid length
  120. * to reg 3. Make sure we ignore it to prevent an address clash
  121. * with the previous device.
  122. */
  123. if (len != 3) {
  124. return 0;
  125. }
  126. switch (buf[2]) {
  127. case ADB_CMD_SELF_TEST:
  128. break;
  129. case ADB_CMD_CHANGE_ID:
  130. case ADB_CMD_CHANGE_ID_AND_ACT:
  131. case ADB_CMD_CHANGE_ID_AND_ENABLE:
  132. d->devaddr = buf[1] & 0xf;
  133. trace_adb_device_mouse_request_change_addr(d->devaddr);
  134. break;
  135. default:
  136. d->devaddr = buf[1] & 0xf;
  137. /*
  138. * we support handlers:
  139. * 0x01: Classic Apple Mouse Protocol / 100 cpi operations
  140. * 0x02: Classic Apple Mouse Protocol / 200 cpi operations
  141. * we don't support handlers (at least):
  142. * 0x03: Mouse systems A3 trackball
  143. * 0x04: Extended Apple Mouse Protocol
  144. * 0x2f: Microspeed mouse
  145. * 0x42: Macally
  146. * 0x5f: Microspeed mouse
  147. * 0x66: Microspeed mouse
  148. */
  149. if (buf[2] == 1 || buf[2] == 2) {
  150. d->handler = buf[2];
  151. }
  152. trace_adb_device_mouse_request_change_addr_and_handler(
  153. d->devaddr, d->handler);
  154. break;
  155. }
  156. }
  157. break;
  158. case ADB_READREG:
  159. switch (reg) {
  160. case 0:
  161. olen = adb_mouse_poll(d, obuf);
  162. break;
  163. case 1:
  164. break;
  165. case 3:
  166. obuf[0] = d->devaddr;
  167. obuf[1] = d->handler;
  168. olen = 2;
  169. break;
  170. }
  171. trace_adb_device_mouse_readreg(reg, obuf[0], obuf[1]);
  172. break;
  173. }
  174. return olen;
  175. }
  176. static bool adb_mouse_has_data(ADBDevice *d)
  177. {
  178. MouseState *s = ADB_MOUSE(d);
  179. return !(s->last_buttons_state == s->buttons_state &&
  180. s->dx == 0 && s->dy == 0);
  181. }
  182. static void adb_mouse_reset(DeviceState *dev)
  183. {
  184. ADBDevice *d = ADB_DEVICE(dev);
  185. MouseState *s = ADB_MOUSE(dev);
  186. d->handler = 2;
  187. d->devaddr = ADB_DEVID_MOUSE;
  188. s->last_buttons_state = s->buttons_state = 0;
  189. s->dx = s->dy = s->dz = 0;
  190. }
  191. static const VMStateDescription vmstate_adb_mouse = {
  192. .name = "adb_mouse",
  193. .version_id = 2,
  194. .minimum_version_id = 2,
  195. .fields = (VMStateField[]) {
  196. VMSTATE_STRUCT(parent_obj, MouseState, 0, vmstate_adb_device,
  197. ADBDevice),
  198. VMSTATE_INT32(buttons_state, MouseState),
  199. VMSTATE_INT32(last_buttons_state, MouseState),
  200. VMSTATE_INT32(dx, MouseState),
  201. VMSTATE_INT32(dy, MouseState),
  202. VMSTATE_INT32(dz, MouseState),
  203. VMSTATE_END_OF_LIST()
  204. }
  205. };
  206. static void adb_mouse_realizefn(DeviceState *dev, Error **errp)
  207. {
  208. MouseState *s = ADB_MOUSE(dev);
  209. ADBMouseClass *amc = ADB_MOUSE_GET_CLASS(dev);
  210. amc->parent_realize(dev, errp);
  211. qemu_add_mouse_event_handler(adb_mouse_event, s, 0, "QEMU ADB Mouse");
  212. }
  213. static void adb_mouse_initfn(Object *obj)
  214. {
  215. ADBDevice *d = ADB_DEVICE(obj);
  216. d->devaddr = ADB_DEVID_MOUSE;
  217. }
  218. static void adb_mouse_class_init(ObjectClass *oc, void *data)
  219. {
  220. DeviceClass *dc = DEVICE_CLASS(oc);
  221. ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc);
  222. ADBMouseClass *amc = ADB_MOUSE_CLASS(oc);
  223. device_class_set_parent_realize(dc, adb_mouse_realizefn,
  224. &amc->parent_realize);
  225. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  226. adc->devreq = adb_mouse_request;
  227. adc->devhasdata = adb_mouse_has_data;
  228. dc->reset = adb_mouse_reset;
  229. dc->vmsd = &vmstate_adb_mouse;
  230. }
  231. static const TypeInfo adb_mouse_type_info = {
  232. .name = TYPE_ADB_MOUSE,
  233. .parent = TYPE_ADB_DEVICE,
  234. .instance_size = sizeof(MouseState),
  235. .instance_init = adb_mouse_initfn,
  236. .class_init = adb_mouse_class_init,
  237. .class_size = sizeof(ADBMouseClass),
  238. };
  239. static void adb_mouse_register_types(void)
  240. {
  241. type_register_static(&adb_mouse_type_info);
  242. }
  243. type_init(adb_mouse_register_types)