adb-mouse.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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_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_mouse_writereg(reg, buf[1]);
  113. switch (reg) {
  114. case 2:
  115. break;
  116. case 3:
  117. switch (buf[2]) {
  118. case ADB_CMD_SELF_TEST:
  119. break;
  120. case ADB_CMD_CHANGE_ID:
  121. case ADB_CMD_CHANGE_ID_AND_ACT:
  122. case ADB_CMD_CHANGE_ID_AND_ENABLE:
  123. d->devaddr = buf[1] & 0xf;
  124. trace_adb_mouse_request_change_addr(d->devaddr);
  125. break;
  126. default:
  127. if (!d->disable_direct_reg3_writes) {
  128. d->devaddr = buf[1] & 0xf;
  129. /* we support handlers:
  130. * 0x01: Classic Apple Mouse Protocol / 100 cpi operations
  131. * 0x02: Classic Apple Mouse Protocol / 200 cpi operations
  132. * we don't support handlers (at least):
  133. * 0x03: Mouse systems A3 trackball
  134. * 0x04: Extended Apple Mouse Protocol
  135. * 0x2f: Microspeed mouse
  136. * 0x42: Macally
  137. * 0x5f: Microspeed mouse
  138. * 0x66: Microspeed mouse
  139. */
  140. if (buf[2] == 1 || buf[2] == 2) {
  141. d->handler = buf[2];
  142. }
  143. trace_adb_mouse_request_change_addr_and_handler(
  144. d->devaddr, d->handler);
  145. }
  146. break;
  147. }
  148. }
  149. break;
  150. case ADB_READREG:
  151. switch (reg) {
  152. case 0:
  153. olen = adb_mouse_poll(d, obuf);
  154. break;
  155. case 1:
  156. break;
  157. case 3:
  158. obuf[0] = d->devaddr;
  159. obuf[1] = d->handler;
  160. olen = 2;
  161. break;
  162. }
  163. trace_adb_mouse_readreg(reg, obuf[0], obuf[1]);
  164. break;
  165. }
  166. return olen;
  167. }
  168. static void adb_mouse_reset(DeviceState *dev)
  169. {
  170. ADBDevice *d = ADB_DEVICE(dev);
  171. MouseState *s = ADB_MOUSE(dev);
  172. d->handler = 2;
  173. d->devaddr = ADB_DEVID_MOUSE;
  174. s->last_buttons_state = s->buttons_state = 0;
  175. s->dx = s->dy = s->dz = 0;
  176. }
  177. static const VMStateDescription vmstate_adb_mouse = {
  178. .name = "adb_mouse",
  179. .version_id = 2,
  180. .minimum_version_id = 2,
  181. .fields = (VMStateField[]) {
  182. VMSTATE_STRUCT(parent_obj, MouseState, 0, vmstate_adb_device,
  183. ADBDevice),
  184. VMSTATE_INT32(buttons_state, MouseState),
  185. VMSTATE_INT32(last_buttons_state, MouseState),
  186. VMSTATE_INT32(dx, MouseState),
  187. VMSTATE_INT32(dy, MouseState),
  188. VMSTATE_INT32(dz, MouseState),
  189. VMSTATE_END_OF_LIST()
  190. }
  191. };
  192. static void adb_mouse_realizefn(DeviceState *dev, Error **errp)
  193. {
  194. MouseState *s = ADB_MOUSE(dev);
  195. ADBMouseClass *amc = ADB_MOUSE_GET_CLASS(dev);
  196. amc->parent_realize(dev, errp);
  197. qemu_add_mouse_event_handler(adb_mouse_event, s, 0, "QEMU ADB Mouse");
  198. }
  199. static void adb_mouse_initfn(Object *obj)
  200. {
  201. ADBDevice *d = ADB_DEVICE(obj);
  202. d->devaddr = ADB_DEVID_MOUSE;
  203. }
  204. static void adb_mouse_class_init(ObjectClass *oc, void *data)
  205. {
  206. DeviceClass *dc = DEVICE_CLASS(oc);
  207. ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc);
  208. ADBMouseClass *amc = ADB_MOUSE_CLASS(oc);
  209. device_class_set_parent_realize(dc, adb_mouse_realizefn,
  210. &amc->parent_realize);
  211. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  212. adc->devreq = adb_mouse_request;
  213. dc->reset = adb_mouse_reset;
  214. dc->vmsd = &vmstate_adb_mouse;
  215. }
  216. static const TypeInfo adb_mouse_type_info = {
  217. .name = TYPE_ADB_MOUSE,
  218. .parent = TYPE_ADB_DEVICE,
  219. .instance_size = sizeof(MouseState),
  220. .instance_init = adb_mouse_initfn,
  221. .class_init = adb_mouse_class_init,
  222. .class_size = sizeof(ADBMouseClass),
  223. };
  224. static void adb_mouse_register_types(void)
  225. {
  226. type_register_static(&adb_mouse_type_info);
  227. }
  228. type_init(adb_mouse_register_types)