source: npl/mediabox/lcdproc_edwin/src/server/drivers/joy.c @ 0105685

gcc484ntopperl-5.22
Last change on this file since 0105685 was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 4.6 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <unistd.h>
4#include <termios.h>
5#include <fcntl.h>
6#include <string.h>
7#include <sys/errno.h>
8#include <sys/ioctl.h>
9#include <sys/types.h>
10
11
12#define __u32 unsigned int
13#define __u8 unsigned char
14#include <linux/joystick.h>
15
16
17#include "../../shared/debug.h"
18#include "../../shared/str.h"
19
20#define NAME_LENGTH 128
21
22
23
24#include "lcd.h"
25#include "joy.h"
26
27
28
29//////////////////////////////////////////////////////////////////////////
30////////////////////// Base "class" to derive from ///////////////////////
31//////////////////////////////////////////////////////////////////////////
32
33lcd_logical_driver *joy;
34
35int fd;
36
37struct js_event js;
38
39char axes = 2;
40char buttons = 2;
41int jsversion = 0x000800;
42char jsname[NAME_LENGTH] = "Unknown";
43
44int *axis;
45int *button;
46
47
48// Configured for a Gravis Gamepad  (2 axis, 4 button)
49char *axismap   = "EFGHIJKLMNOPQRST";
50char *buttonmap = "BDACEFGHIJKLMNOP";
51
52
53////////////////////////////////////////////////////////////
54// init() should set up any device-specific stuff, and
55// point all the function pointers.
56int joy_init(struct lcd_logical_driver *driver, char *args)
57{
58   char device[256];
59   char *argv[64];
60   int argc, i, j;
61   
62   joy = driver;
63
64   
65   strcpy(device, "/dev/js0");
66
67   argc = get_args(argv, args, 64);
68
69   for(i=0; i<argc; i++)
70   {
71      //printf("Arg(%i): %s\n", i, argv[i]);
72      if(0 == strcmp(argv[i], "-d")  ||
73         0 == strcmp(argv[i], "--device"))
74      {
75         if(i + 1 > argc) {
76            fprintf(stderr, "joy_init: %s requires an argument\n",
77                    argv[i]);
78            return -1;
79         }
80         strcpy(device, argv[++i]);
81      }
82      else if(0 == strcmp(argv[i], "-a")  ||
83         0 == strcmp(argv[i], "--axes"))
84      {
85         if(i + 1 > argc) {
86            fprintf(stderr, "joy_init: %s requires an argument\n",
87                    argv[i]);
88            return -1;
89         }
90         strncpy(axismap, argv[++i], 16);
91      }
92      else if(0 == strcmp(argv[i], "-b")  ||
93         0 == strcmp(argv[i], "--buttons"))
94      {
95         if(i + 1 > argc) {
96            fprintf(stderr, "joy_init: %s requires an argument\n",
97                    argv[i]);
98            return -1;
99         }
100         strncpy(buttonmap, argv[++i], 16);
101      }
102      else if(0 == strcmp(argv[i], "-h")  ||
103         0 == strcmp(argv[i], "--help"))
104      {
105         printf("LCDproc Joystick input driver\n"
106                "\t-d\t--device\tSelect the input device to use [/dev/js0]\n"
107                "\t-a\t--axes\t\tModify the axis map [%s]\n"
108                "\t-b\t--buttons\tModify the button map [%s]\n"
109                "\t-h\t--help\t\tShow this help information\n",
110                axismap, buttonmap);
111         return -1;
112      }
113      else
114      {
115         printf("Invalid parameter: %s\n", argv[i]);
116      }
117     
118   }
119   
120   
121   
122   
123   
124   driver->getkey = joy_getkey;
125   driver->close = joy_close;
126
127
128   /*  FIXME:  This crashes!
129   if(args)
130   {
131      if(strlen(args) > 0)
132         strcpy(device, args);
133   }
134   */
135   
136   fd = open (device, O_RDONLY);
137   fcntl(fd, F_SETFL, O_NONBLOCK);
138
139   if(fd < 0) return -1;
140   
141   ioctl(fd, JSIOCGVERSION, &jsversion);
142   ioctl(fd, JSIOCGAXES, &axes);
143   ioctl(fd, JSIOCGBUTTONS, &buttons);
144   ioctl(fd, JSIOCGNAME(NAME_LENGTH), jsname);
145   
146   debug("Joystick (%s) has %d axes and %d buttons. Driver version is %d.%d.%d.\n",
147          jsname, axes, buttons,
148          jsversion >> 16, (jsversion >> 8) & 0xff, jsversion & 0xff);
149   
150   axis = calloc(axes, sizeof(int));
151   button = calloc(buttons, sizeof(char));
152
153   return fd;  // 200 is arbitrary.  (must be 1 or more)
154}
155
156
157void joy_close()
158{
159  if(joy->framebuf != NULL) free(joy->framebuf);
160  close(fd);
161 
162  joy->framebuf = NULL;
163
164  // Why do I have so much trouble getting memory freed without segfaults??
165  //if(axis) free(axis);
166  //if(button) free(button);
167 
168}
169
170
171//////////////////////////////////////////////////////////////////////
172// Tries to read a character from an input device...
173//
174// Return 0 for "nothing available".
175//
176char joy_getkey()
177{
178   int i;
179   int err;
180   
181   err = read(fd, &js, sizeof(struct js_event));
182   if (err <= 0) return 0;
183   if (err != sizeof(struct js_event)) {
184      fprintf(stderr, "\nerror reading joystick\n");
185      return 0;
186   }
187
188//   if(js.type & JS_EVENT_INIT) return 0;
189   
190   switch(js.type & ~JS_EVENT_INIT) {
191      case JS_EVENT_BUTTON:
192         button[js.number] = js.value;
193         break;
194      case JS_EVENT_AXIS:
195         axis[js.number] = js.value;
196         break;
197   }
198
199   if (buttons) {
200      //printf("Buttons: ");
201      for (i = 0; i < buttons; i++)
202         //printf("%2d:%s ", i, button[i] ? "on " : "off");
203         if(button[i]) return buttonmap[i];
204   }
205
206   if (axes) {
207      //printf("Axes: ");
208      for (i = 0; i < axes; i++)
209      {
210         //printf("%2d:%6d ", i, axis[i]);
211         // Eliminate noise...
212         if(axis[i] > 20000) return axismap[(2*i) + 1];
213         if(axis[i] < -20000) return axismap[(2*i)];
214      }
215   }
216   
217   return 0;
218}
Note: See TracBrowser for help on using the repository browser.