freejoy.linux.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // freejoy.linux.c
  2. #include "freejoy.h"
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <pthread.h>
  7. #include <sys/ioctl.h>
  8. #include <linux/joystick.h>
  9. struct linuxjoy
  10. {
  11. pthread_t thread;
  12. int threadid;
  13. int open,fd,fp;
  14. char name[256];
  15. int buttoncount,axiscount;
  16. int button;
  17. float axis[16];
  18. };
  19. typedef struct linuxjoy linuxjoy;
  20. typedef struct js_event js_event;
  21. typedef struct sched_param sched_param;
  22. void *joythread(void *v)
  23. {
  24. linuxjoy *j;
  25. js_event js;
  26. int n,b;
  27. int policy;
  28. sched_param sched;
  29. pthread_getschedparam(pthread_self(),&policy,&sched);
  30. sched.sched_priority++;
  31. policy=SCHED_RR;
  32. pthread_setschedparam(pthread_self(),policy,&sched);
  33. j=(linuxjoy*)v;
  34. while (j->open)
  35. {
  36. b=sizeof(struct js_event)-j->fp;
  37. n=read(j->fd,&js,b);
  38. if (n<=0) break;
  39. if (n<b) {j->fp+=b;continue;}
  40. j->fp=0;
  41. switch (js.type & ~JS_EVENT_INIT)
  42. {
  43. case JS_EVENT_AXIS:
  44. n=js.number;
  45. if (n>=0 && n<16) j->axis[n]=js.value/32767.0;
  46. break;
  47. case JS_EVENT_BUTTON:
  48. n=1<<js.number;
  49. if (js.value) j->button|=n;else j->button&=~n;
  50. break;
  51. }
  52. }
  53. close(j->fd);
  54. j->fd=0;
  55. return 0;
  56. }
  57. linuxjoy *getjoy(int n)
  58. {
  59. linuxjoy *j;
  60. char fname[16];
  61. int fd;
  62. sprintf(fname,"/dev/js%d",n);
  63. fd=open(fname,O_RDONLY);
  64. if (fd==-1) return 0;
  65. j=(linuxjoy*)calloc(1,sizeof(struct linuxjoy));
  66. j->fd=fd;
  67. ioctl(fd,JSIOCGNAME(256),&j->name);
  68. ioctl(fd,JSIOCGAXES,&j->axiscount);
  69. ioctl(fd,JSIOCGBUTTONS,&j->buttoncount);
  70. // fcntl(fd,F_SETFL,O_NONBLOCK);
  71. j->open=1;
  72. pthread_attr_t attr;
  73. pthread_attr_init(&attr);
  74. // pthread_attr_setschedpolicy(&attr,SCHED_RR);
  75. // pthread_attr_getschedparam(&attr);
  76. // printf("mypid=%x\n",getpid());
  77. // pthread_attr_setschedparam(&attr,1);
  78. j->threadid=pthread_create(&j->thread,&attr,joythread,(void*)j);
  79. return j;
  80. }
  81. void updatejoy(linuxjoy *j,int *buttons,float *axis)
  82. {
  83. *buttons=j->button;
  84. memcpy(axis,j->axis,16*4);
  85. }
  86. void freejoy(struct linuxjoy *j)
  87. {
  88. int timeout=5;
  89. j->open=0;
  90. while (timeout-- && j->fd) sleep(1);
  91. // close(j->fd);
  92. }
  93. // standard freejoy interface
  94. int ljoyopen;
  95. int ljoycount;
  96. linuxjoy *ljoys[8];
  97. int JoyCount()
  98. {
  99. linuxjoy *j;
  100. int i,n;
  101. if (!ljoyopen)
  102. {
  103. n=0;
  104. for (i=0;i<8;i++)
  105. {
  106. j=getjoy(i);
  107. if (j) ljoys[n++]=j;
  108. }
  109. ljoycount=n;
  110. ljoyopen=1;
  111. }
  112. return ljoycount;
  113. }
  114. char *JoyCName(int port)
  115. {
  116. if (port>=0 && port<ljoycount) return ljoys[port]->name;
  117. return 0;
  118. }
  119. int JoyButtonCaps(int port)
  120. {
  121. if (port>=0 && port<ljoycount) return (1<<ljoys[port]->buttoncount)-1;
  122. return 0;
  123. }
  124. int JoyAxisCaps(int port)
  125. {
  126. if (port>=0 && port<ljoycount) return (1<<ljoys[port]->axiscount)-1;
  127. return 0;
  128. }
  129. int ReadJoy(int port,int *buttons,float *axis)
  130. {
  131. if (port<0 || port>=ljoycount) return 0;
  132. updatejoy (ljoys[port],buttons,axis);
  133. return 1;
  134. }
  135. void WriteJoy(int port,int channel,float value)
  136. {
  137. }