input-linux.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * This work is licensed under the terms of the GNU GPL, version 2 or
  3. * (at your option) any later version. See the COPYING file in the
  4. * top-level directory.
  5. */
  6. #include "qemu/osdep.h"
  7. #include "qapi/error.h"
  8. #include "qemu/config-file.h"
  9. #include "qemu/main-loop.h"
  10. #include "qemu/module.h"
  11. #include "qemu/sockets.h"
  12. #include "ui/input.h"
  13. #include "qom/object_interfaces.h"
  14. #include "sysemu/iothread.h"
  15. #include "block/aio.h"
  16. #include <sys/ioctl.h>
  17. #include "standard-headers/linux/input.h"
  18. static bool linux_is_button(unsigned int lnx)
  19. {
  20. if (lnx < 0x100) {
  21. return false;
  22. }
  23. if (lnx >= 0x160 && lnx < 0x2c0) {
  24. return false;
  25. }
  26. return true;
  27. }
  28. #define TYPE_INPUT_LINUX "input-linux"
  29. #define INPUT_LINUX(obj) \
  30. OBJECT_CHECK(InputLinux, (obj), TYPE_INPUT_LINUX)
  31. #define INPUT_LINUX_GET_CLASS(obj) \
  32. OBJECT_GET_CLASS(InputLinuxClass, (obj), TYPE_INPUT_LINUX)
  33. #define INPUT_LINUX_CLASS(klass) \
  34. OBJECT_CLASS_CHECK(InputLinuxClass, (klass), TYPE_INPUT_LINUX)
  35. typedef struct InputLinux InputLinux;
  36. typedef struct InputLinuxClass InputLinuxClass;
  37. struct InputLinux {
  38. Object parent;
  39. char *evdev;
  40. int fd;
  41. bool repeat;
  42. bool grab_request;
  43. bool grab_active;
  44. bool grab_all;
  45. bool keydown[KEY_CNT];
  46. int keycount;
  47. int wheel;
  48. bool initialized;
  49. bool has_rel_x;
  50. bool has_abs_x;
  51. int num_keys;
  52. int num_btns;
  53. int abs_x_min;
  54. int abs_x_max;
  55. int abs_y_min;
  56. int abs_y_max;
  57. struct input_event event;
  58. int read_offset;
  59. enum GrabToggleKeys grab_toggle;
  60. QTAILQ_ENTRY(InputLinux) next;
  61. };
  62. struct InputLinuxClass {
  63. ObjectClass parent_class;
  64. };
  65. static QTAILQ_HEAD(, InputLinux) inputs = QTAILQ_HEAD_INITIALIZER(inputs);
  66. static void input_linux_toggle_grab(InputLinux *il)
  67. {
  68. intptr_t request = !il->grab_active;
  69. InputLinux *item;
  70. int rc;
  71. rc = ioctl(il->fd, EVIOCGRAB, request);
  72. if (rc < 0) {
  73. return;
  74. }
  75. il->grab_active = !il->grab_active;
  76. if (!il->grab_all) {
  77. return;
  78. }
  79. QTAILQ_FOREACH(item, &inputs, next) {
  80. if (item == il || item->grab_all) {
  81. /* avoid endless loops */
  82. continue;
  83. }
  84. if (item->grab_active != il->grab_active) {
  85. input_linux_toggle_grab(item);
  86. }
  87. }
  88. }
  89. static bool input_linux_check_toggle(InputLinux *il)
  90. {
  91. switch (il->grab_toggle) {
  92. case GRAB_TOGGLE_KEYS_CTRL_CTRL:
  93. return il->keydown[KEY_LEFTCTRL] &&
  94. il->keydown[KEY_RIGHTCTRL];
  95. case GRAB_TOGGLE_KEYS_ALT_ALT:
  96. return il->keydown[KEY_LEFTALT] &&
  97. il->keydown[KEY_RIGHTALT];
  98. case GRAB_TOGGLE_KEYS_SHIFT_SHIFT:
  99. return il->keydown[KEY_LEFTSHIFT] &&
  100. il->keydown[KEY_RIGHTSHIFT];
  101. case GRAB_TOGGLE_KEYS_META_META:
  102. return il->keydown[KEY_LEFTMETA] &&
  103. il->keydown[KEY_RIGHTMETA];
  104. case GRAB_TOGGLE_KEYS_SCROLLLOCK:
  105. return il->keydown[KEY_SCROLLLOCK];
  106. case GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK:
  107. return (il->keydown[KEY_LEFTCTRL] ||
  108. il->keydown[KEY_RIGHTCTRL]) &&
  109. il->keydown[KEY_SCROLLLOCK];
  110. case GRAB_TOGGLE_KEYS__MAX:
  111. /* avoid gcc error */
  112. break;
  113. }
  114. return false;
  115. }
  116. static bool input_linux_should_skip(InputLinux *il,
  117. struct input_event *event)
  118. {
  119. return (il->grab_toggle == GRAB_TOGGLE_KEYS_SCROLLLOCK ||
  120. il->grab_toggle == GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK) &&
  121. event->code == KEY_SCROLLLOCK;
  122. }
  123. static void input_linux_handle_keyboard(InputLinux *il,
  124. struct input_event *event)
  125. {
  126. if (event->type == EV_KEY) {
  127. if (event->value > 2 || (event->value > 1 && !il->repeat)) {
  128. /*
  129. * ignore autorepeat + unknown key events
  130. * 0 == up, 1 == down, 2 == autorepeat, other == undefined
  131. */
  132. return;
  133. }
  134. if (event->code >= KEY_CNT) {
  135. /*
  136. * Should not happen. But better safe than sorry,
  137. * and we make Coverity happy too.
  138. */
  139. return;
  140. }
  141. /* keep track of key state */
  142. if (!il->keydown[event->code] && event->value) {
  143. il->keydown[event->code] = true;
  144. il->keycount++;
  145. }
  146. if (il->keydown[event->code] && !event->value) {
  147. il->keydown[event->code] = false;
  148. il->keycount--;
  149. }
  150. /* send event to guest when grab is active */
  151. if (il->grab_active && !input_linux_should_skip(il, event)) {
  152. int qcode = qemu_input_linux_to_qcode(event->code);
  153. qemu_input_event_send_key_qcode(NULL, qcode, event->value);
  154. }
  155. /* hotkey -> record switch request ... */
  156. if (input_linux_check_toggle(il)) {
  157. il->grab_request = true;
  158. }
  159. /*
  160. * ... and do the switch when all keys are lifted, so we
  161. * confuse neither guest nor host with keys which seem to
  162. * be stuck due to missing key-up events.
  163. */
  164. if (il->grab_request && !il->keycount) {
  165. il->grab_request = false;
  166. input_linux_toggle_grab(il);
  167. }
  168. }
  169. }
  170. static void input_linux_event_mouse_button(int button)
  171. {
  172. qemu_input_queue_btn(NULL, button, true);
  173. qemu_input_event_sync();
  174. qemu_input_queue_btn(NULL, button, false);
  175. qemu_input_event_sync();
  176. }
  177. static void input_linux_handle_mouse(InputLinux *il, struct input_event *event)
  178. {
  179. if (!il->grab_active) {
  180. return;
  181. }
  182. switch (event->type) {
  183. case EV_KEY:
  184. switch (event->code) {
  185. case BTN_LEFT:
  186. qemu_input_queue_btn(NULL, INPUT_BUTTON_LEFT, event->value);
  187. break;
  188. case BTN_RIGHT:
  189. qemu_input_queue_btn(NULL, INPUT_BUTTON_RIGHT, event->value);
  190. break;
  191. case BTN_MIDDLE:
  192. qemu_input_queue_btn(NULL, INPUT_BUTTON_MIDDLE, event->value);
  193. break;
  194. case BTN_GEAR_UP:
  195. qemu_input_queue_btn(NULL, INPUT_BUTTON_WHEEL_UP, event->value);
  196. break;
  197. case BTN_GEAR_DOWN:
  198. qemu_input_queue_btn(NULL, INPUT_BUTTON_WHEEL_DOWN,
  199. event->value);
  200. break;
  201. case BTN_SIDE:
  202. qemu_input_queue_btn(NULL, INPUT_BUTTON_SIDE, event->value);
  203. break;
  204. case BTN_EXTRA:
  205. qemu_input_queue_btn(NULL, INPUT_BUTTON_EXTRA, event->value);
  206. break;
  207. };
  208. break;
  209. case EV_REL:
  210. switch (event->code) {
  211. case REL_X:
  212. qemu_input_queue_rel(NULL, INPUT_AXIS_X, event->value);
  213. break;
  214. case REL_Y:
  215. qemu_input_queue_rel(NULL, INPUT_AXIS_Y, event->value);
  216. break;
  217. case REL_WHEEL:
  218. il->wheel = event->value;
  219. break;
  220. }
  221. break;
  222. case EV_ABS:
  223. switch (event->code) {
  224. case ABS_X:
  225. qemu_input_queue_abs(NULL, INPUT_AXIS_X, event->value,
  226. il->abs_x_min, il->abs_x_max);
  227. break;
  228. case ABS_Y:
  229. qemu_input_queue_abs(NULL, INPUT_AXIS_Y, event->value,
  230. il->abs_y_min, il->abs_y_max);
  231. break;
  232. }
  233. break;
  234. case EV_SYN:
  235. qemu_input_event_sync();
  236. if (il->wheel != 0) {
  237. input_linux_event_mouse_button((il->wheel > 0)
  238. ? INPUT_BUTTON_WHEEL_UP
  239. : INPUT_BUTTON_WHEEL_DOWN);
  240. il->wheel = 0;
  241. }
  242. break;
  243. }
  244. }
  245. static void input_linux_event(void *opaque)
  246. {
  247. InputLinux *il = opaque;
  248. int rc;
  249. int read_size;
  250. uint8_t *p = (uint8_t *)&il->event;
  251. for (;;) {
  252. read_size = sizeof(il->event) - il->read_offset;
  253. rc = read(il->fd, &p[il->read_offset], read_size);
  254. if (rc != read_size) {
  255. if (rc < 0 && errno != EAGAIN) {
  256. fprintf(stderr, "%s: read: %s\n", __func__, strerror(errno));
  257. qemu_set_fd_handler(il->fd, NULL, NULL, NULL);
  258. close(il->fd);
  259. } else if (rc > 0) {
  260. il->read_offset += rc;
  261. }
  262. break;
  263. }
  264. il->read_offset = 0;
  265. if (il->num_keys) {
  266. input_linux_handle_keyboard(il, &il->event);
  267. }
  268. if ((il->has_rel_x || il->has_abs_x) && il->num_btns) {
  269. input_linux_handle_mouse(il, &il->event);
  270. }
  271. }
  272. }
  273. static void input_linux_complete(UserCreatable *uc, Error **errp)
  274. {
  275. InputLinux *il = INPUT_LINUX(uc);
  276. uint8_t evtmap, relmap, absmap;
  277. uint8_t keymap[KEY_CNT / 8], keystate[KEY_CNT / 8];
  278. unsigned int i;
  279. int rc, ver;
  280. struct input_absinfo absinfo;
  281. if (!il->evdev) {
  282. error_setg(errp, "no input device specified");
  283. return;
  284. }
  285. il->fd = open(il->evdev, O_RDWR);
  286. if (il->fd < 0) {
  287. error_setg_file_open(errp, errno, il->evdev);
  288. return;
  289. }
  290. qemu_set_nonblock(il->fd);
  291. rc = ioctl(il->fd, EVIOCGVERSION, &ver);
  292. if (rc < 0) {
  293. error_setg(errp, "%s: is not an evdev device", il->evdev);
  294. goto err_close;
  295. }
  296. rc = ioctl(il->fd, EVIOCGBIT(0, sizeof(evtmap)), &evtmap);
  297. if (rc < 0) {
  298. error_setg(errp, "%s: failed to read event bits", il->evdev);
  299. goto err_close;
  300. }
  301. if (evtmap & (1 << EV_REL)) {
  302. relmap = 0;
  303. rc = ioctl(il->fd, EVIOCGBIT(EV_REL, sizeof(relmap)), &relmap);
  304. if (relmap & (1 << REL_X)) {
  305. il->has_rel_x = true;
  306. }
  307. }
  308. if (evtmap & (1 << EV_ABS)) {
  309. absmap = 0;
  310. rc = ioctl(il->fd, EVIOCGBIT(EV_ABS, sizeof(absmap)), &absmap);
  311. if (absmap & (1 << ABS_X)) {
  312. il->has_abs_x = true;
  313. rc = ioctl(il->fd, EVIOCGABS(ABS_X), &absinfo);
  314. il->abs_x_min = absinfo.minimum;
  315. il->abs_x_max = absinfo.maximum;
  316. rc = ioctl(il->fd, EVIOCGABS(ABS_Y), &absinfo);
  317. il->abs_y_min = absinfo.minimum;
  318. il->abs_y_max = absinfo.maximum;
  319. }
  320. }
  321. if (evtmap & (1 << EV_KEY)) {
  322. memset(keymap, 0, sizeof(keymap));
  323. rc = ioctl(il->fd, EVIOCGBIT(EV_KEY, sizeof(keymap)), keymap);
  324. rc = ioctl(il->fd, EVIOCGKEY(sizeof(keystate)), keystate);
  325. for (i = 0; i < KEY_CNT; i++) {
  326. if (keymap[i / 8] & (1 << (i % 8))) {
  327. if (linux_is_button(i)) {
  328. il->num_btns++;
  329. } else {
  330. il->num_keys++;
  331. }
  332. if (keystate[i / 8] & (1 << (i % 8))) {
  333. il->keydown[i] = true;
  334. il->keycount++;
  335. }
  336. }
  337. }
  338. }
  339. qemu_set_fd_handler(il->fd, input_linux_event, NULL, il);
  340. if (il->keycount) {
  341. /* delay grab until all keys are released */
  342. il->grab_request = true;
  343. } else {
  344. input_linux_toggle_grab(il);
  345. }
  346. QTAILQ_INSERT_TAIL(&inputs, il, next);
  347. il->initialized = true;
  348. return;
  349. err_close:
  350. close(il->fd);
  351. return;
  352. }
  353. static void input_linux_instance_finalize(Object *obj)
  354. {
  355. InputLinux *il = INPUT_LINUX(obj);
  356. if (il->initialized) {
  357. QTAILQ_REMOVE(&inputs, il, next);
  358. close(il->fd);
  359. }
  360. g_free(il->evdev);
  361. }
  362. static char *input_linux_get_evdev(Object *obj, Error **errp)
  363. {
  364. InputLinux *il = INPUT_LINUX(obj);
  365. return g_strdup(il->evdev);
  366. }
  367. static void input_linux_set_evdev(Object *obj, const char *value,
  368. Error **errp)
  369. {
  370. InputLinux *il = INPUT_LINUX(obj);
  371. if (il->evdev) {
  372. error_setg(errp, "evdev property already set");
  373. return;
  374. }
  375. il->evdev = g_strdup(value);
  376. }
  377. static bool input_linux_get_grab_all(Object *obj, Error **errp)
  378. {
  379. InputLinux *il = INPUT_LINUX(obj);
  380. return il->grab_all;
  381. }
  382. static void input_linux_set_grab_all(Object *obj, bool value,
  383. Error **errp)
  384. {
  385. InputLinux *il = INPUT_LINUX(obj);
  386. il->grab_all = value;
  387. }
  388. static bool input_linux_get_repeat(Object *obj, Error **errp)
  389. {
  390. InputLinux *il = INPUT_LINUX(obj);
  391. return il->repeat;
  392. }
  393. static void input_linux_set_repeat(Object *obj, bool value,
  394. Error **errp)
  395. {
  396. InputLinux *il = INPUT_LINUX(obj);
  397. il->repeat = value;
  398. }
  399. static int input_linux_get_grab_toggle(Object *obj, Error **errp)
  400. {
  401. InputLinux *il = INPUT_LINUX(obj);
  402. return il->grab_toggle;
  403. }
  404. static void input_linux_set_grab_toggle(Object *obj, int value,
  405. Error **errp)
  406. {
  407. InputLinux *il = INPUT_LINUX(obj);
  408. il->grab_toggle = value;
  409. }
  410. static void input_linux_instance_init(Object *obj)
  411. {
  412. object_property_add_str(obj, "evdev",
  413. input_linux_get_evdev,
  414. input_linux_set_evdev, NULL);
  415. object_property_add_bool(obj, "grab_all",
  416. input_linux_get_grab_all,
  417. input_linux_set_grab_all, NULL);
  418. object_property_add_bool(obj, "repeat",
  419. input_linux_get_repeat,
  420. input_linux_set_repeat, NULL);
  421. object_property_add_enum(obj, "grab-toggle", "GrabToggleKeys",
  422. &GrabToggleKeys_lookup,
  423. input_linux_get_grab_toggle,
  424. input_linux_set_grab_toggle, NULL);
  425. }
  426. static void input_linux_class_init(ObjectClass *oc, void *data)
  427. {
  428. UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
  429. ucc->complete = input_linux_complete;
  430. }
  431. static const TypeInfo input_linux_info = {
  432. .name = TYPE_INPUT_LINUX,
  433. .parent = TYPE_OBJECT,
  434. .class_size = sizeof(InputLinuxClass),
  435. .class_init = input_linux_class_init,
  436. .instance_size = sizeof(InputLinux),
  437. .instance_init = input_linux_instance_init,
  438. .instance_finalize = input_linux_instance_finalize,
  439. .interfaces = (InterfaceInfo[]) {
  440. { TYPE_USER_CREATABLE },
  441. { }
  442. }
  443. };
  444. static void register_types(void)
  445. {
  446. type_register_static(&input_linux_info);
  447. }
  448. type_init(register_types);