1 /*
2 * Support for joysticks under Linux
3 *
4 * Wouter Verhelst <wouter@debian.org>, 2003
5 *
6 * Distribute under the terms of the GPL.
7 */
8 #ifdef __linux__
9 #include <linux/joystick.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <sys/ioctl.h>
15 #include <fcntl.h>
16 #include <errno.h>
17 #include "data.h"
18 #include "struct.h"
19 #include "defs.h"
20
21 struct js_state js_curstate;
22 int js_device;
23
24 /* make struct js_state be up-to-date */
update_curstate()25 void update_curstate() {
26 struct js_event event;
27 int npval;
28
29 while(read(js_device,&event,sizeof(struct js_event))>0) {
30 npval=event.type&JS_EVENT_INIT?0:-1;
31 event.type &= ~JS_EVENT_INIT;
32 if(event.type==JS_EVENT_BUTTON) {
33 js_curstate.but+=event.value?1:npval;
34 } else if(event.type==JS_EVENT_AXIS) {
35 if(event.number%2==0) {
36 js_curstate.dir=event.value;
37 }
38 }
39 }
40 if (errno !=EAGAIN) {
41 perror("reading joystick device");
42 return;
43 }
44 }
45
46 /*
47 * Initialize the joystick device
48 */
init_joystick()49 void init_joystick() {
50 int ioc=0;
51 js_device = open("/dev/js0",O_RDONLY|O_NONBLOCK);
52 if(js_device<0) {
53 // perror("open(/dev/js0)");
54 close(js_device);
55 js_device=0;
56 }
57 else if(ioctl(js_device,JSIOCGVERSION,&ioc)==-1) {
58 /* Driver too old */
59 fprintf(stderr,"Can't read joystick: driver too old\n");
60 close(js_device);
61 js_device=0;
62 }
63 else {
64 update_curstate();
65 }
66 }
67
do_joystick(int * x,int * y,int * but)68 void do_joystick(int* x, int* y, int* but) {
69 if (js_device>0 && !mouseControl) {
70 update_curstate();
71 *but=(js_curstate.but?W_LBUTTON:0);
72 if(*but && gameOver) {
73 gameOver=0;
74 }
75 if (js_curstate.dir>0 && !mouseControl)
76 *x = WINWIDTH;
77 else if(js_curstate.dir<0)
78 *x = 0;
79 else
80 *x=plx;
81 }
82 }
83 #else
84 js_device=0;
85 #endif /* __linux__ */
86