uinput.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**** uinput.c *****************************/
  2. /* M. Moller 2013-01-16 */
  3. /* Universal RPi GPIO keyboard daemon */
  4. /*******************************************/
  5. /*
  6. Copyright (C) 2013 Michael Moller.
  7. This file is part of the Universal Raspberry Pi GPIO keyboard daemon.
  8. This is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. The software is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with the GNU C Library; if not, write to the Free
  18. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. 02111-1307 USA.
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <linux/input.h>
  28. //#include <linux/uinput.h>
  29. #include "uinput_linux.h"
  30. //#include "config.h"
  31. //include "daemon.h"
  32. #include "uinput.h"
  33. static int sendRel(int dx, int dy);
  34. static int sendSync(void);
  35. static struct input_event uidev_ev;
  36. static int uidev_fd;
  37. /*static keyinfo_s lastkey;*/
  38. #define die(str, args...) do { \
  39. perror(str); \
  40. return(EXIT_FAILURE); \
  41. } while(0)
  42. #define DEBUG_UINPUT_PRINTF (0)
  43. #if DEBUG_UINPUT_PRINTF
  44. #define UINPUT_PRINTF(...) printf(__VA_ARGS__);
  45. #else
  46. #define UINPUT_PRINTF(...)
  47. #endif
  48. int init_uinput(void)
  49. {
  50. int fd;
  51. struct uinput_user_dev uidev;
  52. int i;
  53. fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
  54. if(fd < 0)
  55. die("/dev/uinput");
  56. if(ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0)
  57. die("error: ioctl");
  58. if(ioctl(fd, UI_SET_EVBIT, EV_REP) < 0)
  59. die("error: ioctl");
  60. if(ioctl(fd, UI_SET_KEYBIT, BTN_LEFT) < 0)
  61. die("error: ioctl");
  62. if(ioctl(fd, UI_SET_EVBIT, EV_REL) < 0)
  63. die("error: ioctl");
  64. if(ioctl(fd, UI_SET_RELBIT, REL_X) < 0)
  65. die("error: ioctl");
  66. if(ioctl(fd, UI_SET_RELBIT, REL_Y) < 0)
  67. die("error: ioctl");
  68. /* don't forget to add all the keys! */
  69. for(i=0; i<256; i++){
  70. if(ioctl(fd, UI_SET_KEYBIT, i) < 0)
  71. die("error: ioctl");
  72. }
  73. memset(&uidev, 0, sizeof(uidev));
  74. snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
  75. uidev.id.bustype = BUS_USB;
  76. uidev.id.vendor = 0x1;
  77. uidev.id.product = 0x1;
  78. uidev.id.version = 1;
  79. /*if(write(fd, &uidev, sizeof(uidev)) < 0)
  80. die("error: write");*/
  81. if(ioctl(fd, UI_DEV_SETUP, &uidev) < 0)
  82. die("error: ioctl");
  83. if(ioctl(fd, UI_DEV_CREATE) < 0)
  84. die("error: ioctl");
  85. uidev_fd = fd;
  86. sleep(1);
  87. return 0;
  88. }
  89. int test_uinput(void)
  90. {
  91. int dx, dy;
  92. int i,k;
  93. //srand(time(NULL));
  94. int op = 1;
  95. while(1) {
  96. //switch(rand() % 4) {
  97. switch(op) {
  98. case 0:
  99. dx = -10;
  100. dy = -1;
  101. break;
  102. case 1:
  103. dx = 10;
  104. dy = 1;
  105. break;
  106. case 2:
  107. dx = -1;
  108. dy = 10;
  109. break;
  110. case 3:
  111. dx = 1;
  112. dy = -10;
  113. break;
  114. }
  115. /*k = send_gpio_keys(21, 1);
  116. sendKey(k, 0);*/
  117. sendKey(KEY_D, 0);
  118. for(i = 0; i < 20; i++) {
  119. //sendKey(KEY_D, 1);
  120. //sendKey(KEY_D, 0);
  121. sendRel(dx, dy);
  122. usleep(15000);
  123. }
  124. sleep(10);
  125. }
  126. }
  127. int close_uinput(void)
  128. {
  129. sleep(2);
  130. if(ioctl(uidev_fd, UI_DEV_DESTROY) < 0)
  131. die("error: ioctl");
  132. close(uidev_fd);
  133. return 0;
  134. }
  135. int sendKey(int key, int value)
  136. {
  137. struct input_event ie;
  138. //memset(&uidev_ev, 0, sizeof(struct input_event));
  139. //gettimeofday(&uidev_ev.time, NULL);
  140. ie.type = EV_KEY;
  141. ie.code = key;
  142. ie.value = value;
  143. ie.time.tv_sec = 0;
  144. ie.time.tv_usec = 0;
  145. UINPUT_PRINTF("sendKey: %d = %d\n", key, value);
  146. if(write(uidev_fd, &ie, sizeof(struct input_event)) < 0)
  147. die("error: write");
  148. sendSync();
  149. return 0;
  150. }
  151. int sendKeyAndStopKey(int key)
  152. {
  153. sendKey(key, 1);
  154. sendKey(key, 0);
  155. return 0;
  156. }
  157. static int sendRel(int dx, int dy)
  158. {
  159. memset(&uidev_ev, 0, sizeof(struct input_event));
  160. uidev_ev.type = EV_REL;
  161. uidev_ev.code = REL_X;
  162. uidev_ev.value = dx;
  163. if(write(uidev_fd, &uidev_ev, sizeof(struct input_event)) < 0)
  164. die("error: write");
  165. memset(&uidev_ev, 0, sizeof(struct input_event));
  166. uidev_ev.type = EV_REL;
  167. uidev_ev.code = REL_Y;
  168. uidev_ev.value = dy;
  169. if(write(uidev_fd, &uidev_ev, sizeof(struct input_event)) < 0)
  170. die("error: write");
  171. sendSync();
  172. return 0;
  173. }
  174. static int sendSync(void)
  175. {
  176. UINPUT_PRINTF("sendSync\n");
  177. //memset(&uidev_ev, 0, sizeof(struct input_event));
  178. struct input_event ie;
  179. ie.type = EV_SYN;
  180. ie.code = SYN_REPORT;
  181. ie.value = 0;
  182. ie.time.tv_sec = 0;
  183. ie.time.tv_usec = 0;
  184. if(write(uidev_fd, &ie, sizeof(struct input_event)) < 0)
  185. die("error: sendSync - write");
  186. return 0;
  187. }