2
0

adb-mouse.c 7.5 KB

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