uinput.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #include <time.h>
  34. /****************************************************************
  35. * Defines
  36. ****************************************************************/
  37. #define DEBUG_UINPUT_PRINTF (0)
  38. #if DEBUG_UINPUT_PRINTF
  39. #define UINPUT_PRINTF(...) printf(__VA_ARGS__);
  40. #else
  41. #define UINPUT_PRINTF(...)
  42. #endif
  43. //#define SLEEP_TIME_SEND_STOP_KEY_US 20*1000
  44. #define SLEEP_TIME_SEND_STOP_KEY_US 200*1000
  45. #define die(str, args...) do { \
  46. perror(str); \
  47. return(EXIT_FAILURE); \
  48. } while(0)
  49. // For compatibility with kernels having dates on 32 bits
  50. struct timeval_compat
  51. {
  52. unsigned int tv_sec;
  53. long int tv_usec;
  54. };
  55. struct input_event_compat {
  56. struct timeval_compat time;
  57. unsigned short type;
  58. unsigned short code;
  59. unsigned int value;
  60. };
  61. /****************************************************************
  62. * Static functions declaration
  63. ****************************************************************/
  64. static int sendRel(int dx, int dy);
  65. static int sendSync(void);
  66. /****************************************************************
  67. * Static variables
  68. ****************************************************************/
  69. static struct input_event uidev_ev;
  70. static int uidev_fd;
  71. /*static keyinfo_s lastkey;*/
  72. /****************************************************************
  73. * Functions Implementation
  74. ****************************************************************/
  75. int init_uinput(void)
  76. {
  77. int fd;
  78. struct uinput_user_dev uidev;
  79. int i;
  80. fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
  81. if(fd < 0)
  82. die("/dev/uinput");
  83. if(ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0)
  84. die("error: ioctl");
  85. if(ioctl(fd, UI_SET_EVBIT, EV_REP) < 0)
  86. die("error: ioctl");
  87. if(ioctl(fd, UI_SET_KEYBIT, BTN_LEFT) < 0)
  88. die("error: ioctl");
  89. if(ioctl(fd, UI_SET_EVBIT, EV_REL) < 0)
  90. die("error: ioctl");
  91. if(ioctl(fd, UI_SET_RELBIT, REL_X) < 0)
  92. die("error: ioctl");
  93. if(ioctl(fd, UI_SET_RELBIT, REL_Y) < 0)
  94. die("error: ioctl");
  95. /* don't forget to add all the keys! */
  96. for(i=0; i<256; i++){
  97. if(ioctl(fd, UI_SET_KEYBIT, i) < 0)
  98. die("error: ioctl");
  99. }
  100. memset(&uidev, 0, sizeof(uidev));
  101. snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
  102. uidev.id.bustype = BUS_USB;
  103. uidev.id.vendor = 0x1;
  104. uidev.id.product = 0x1;
  105. uidev.id.version = 1;
  106. /*if(write(fd, &uidev, sizeof(uidev)) < 0)
  107. die("error: write");*/
  108. if(ioctl(fd, UI_DEV_SETUP, &uidev) < 0)
  109. die("error: ioctl");
  110. if(ioctl(fd, UI_DEV_CREATE) < 0)
  111. die("error: ioctl");
  112. uidev_fd = fd;
  113. sleep(1);
  114. return 0;
  115. }
  116. int test_uinput(void)
  117. {
  118. int dx, dy;
  119. int i,k;
  120. //srand(time(NULL));
  121. int op = 1;
  122. while(1) {
  123. //switch(rand() % 4) {
  124. switch(op) {
  125. case 0:
  126. dx = -10;
  127. dy = -1;
  128. break;
  129. case 1:
  130. dx = 10;
  131. dy = 1;
  132. break;
  133. case 2:
  134. dx = -1;
  135. dy = 10;
  136. break;
  137. case 3:
  138. dx = 1;
  139. dy = -10;
  140. break;
  141. }
  142. /*k = send_gpio_keys(21, 1);
  143. sendKey(k, 0);*/
  144. sendKey(KEY_D, 0);
  145. for(i = 0; i < 20; i++) {
  146. //sendKey(KEY_D, 1);
  147. //sendKey(KEY_D, 0);
  148. sendRel(dx, dy);
  149. usleep(15000);
  150. }
  151. sleep(10);
  152. }
  153. }
  154. int close_uinput(void)
  155. {
  156. sleep(2);
  157. if(ioctl(uidev_fd, UI_DEV_DESTROY) < 0)
  158. die("error: ioctl");
  159. close(uidev_fd);
  160. return 0;
  161. }
  162. int sendKey(int key, int value)
  163. {
  164. struct input_event_compat ie;
  165. //memset(&uidev_ev, 0, sizeof(struct input_event));
  166. //gettimeofday(&uidev_ev.time, NULL);
  167. ie.type = EV_KEY;
  168. ie.code = key;
  169. ie.value = value;
  170. ie.time.tv_sec = 0;
  171. ie.time.tv_usec = 0;
  172. UINPUT_PRINTF("sendKey: %d = %d\n", key, value);
  173. if(write(uidev_fd, &ie, sizeof(struct input_event_compat)) < 0)
  174. die("error: write");
  175. sendSync();
  176. return 0;
  177. }
  178. int sendKeyAndStopKey(int key)
  179. {
  180. sendKey(key, 1);
  181. usleep(SLEEP_TIME_SEND_STOP_KEY_US);
  182. sendKey(key, 0);
  183. return 0;
  184. }
  185. static int sendRel(int dx, int dy)
  186. {
  187. memset(&uidev_ev, 0, sizeof(struct input_event));
  188. uidev_ev.type = EV_REL;
  189. uidev_ev.code = REL_X;
  190. uidev_ev.value = dx;
  191. if(write(uidev_fd, &uidev_ev, sizeof(struct input_event)) < 0)
  192. die("error: write");
  193. memset(&uidev_ev, 0, sizeof(struct input_event));
  194. uidev_ev.type = EV_REL;
  195. uidev_ev.code = REL_Y;
  196. uidev_ev.value = dy;
  197. if(write(uidev_fd, &uidev_ev, sizeof(struct input_event)) < 0)
  198. die("error: write");
  199. sendSync();
  200. return 0;
  201. }
  202. static int sendSync(void)
  203. {
  204. UINPUT_PRINTF("sendSync\n");
  205. //memset(&uidev_ev, 0, sizeof(struct input_event));
  206. struct input_event ie;
  207. ie.type = EV_SYN;
  208. ie.code = SYN_REPORT;
  209. ie.value = 0;
  210. ie.time.tv_sec = 0;
  211. ie.time.tv_usec = 0;
  212. if(write(uidev_fd, &ie, sizeof(struct input_event)) < 0)
  213. die("error: sendSync - write");
  214. return 0;
  215. }