Import Debian changes 1.23-11
[debian-dgen.git] / md-phil.cpp
blob81c8d2f3a5c4a5fb2028963e807352a913155421
1 // Ripped Snes9X joystick code and made it Genesis friendly, with a few
2 // slight modifications. [PKH]
4 #include <sys/ioctl.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include "md.h"
12 #include "md-phil.h"
14 // Default setup for Gravis GamePad Pro 10 button controllers.
16 // Yellow ... A
17 // Green .... C
18 // Red ...... A
19 // Blue ..... B
20 // L1 ....... Y
21 // R1 ....... Z
22 // L2 ....... X
23 // R2 ....... X
24 // Start .... START :)
25 // Select ... MODE
27 // Mappings are configurable via dgenrc. Mappings are done from 0-16
28 // Standard 2 button joysticks will only have buttons 0 and 1. [PKH]
30 extern int js_map_button [2][16];
32 #ifdef LINUX_JOYSTICK_SUPPORT
33 #warning USING LINUX JOYSTICK
34 #include <linux/joystick.h>
36 int joypads[2] = {0};
38 int js_fd[2] = {-1, -1};
40 char *js_device [2] = {"/dev/js0", "/dev/js1"};
42 void md::init_joysticks() {
43 #ifdef JSIOCGVERSION
44 int version;
45 unsigned char axes, buttons;
47 if ((js_fd [0] = open (js_device [0], O_RDONLY | O_NONBLOCK)) < 0)
49 perror (js_device [0]);
50 return;
53 if (ioctl (js_fd [0], JSIOCGVERSION, &version))
55 puts("joystick: You need at least driver version 1.0 for joystick support");
56 close (js_fd [0]);
57 return;
59 js_fd [1] = open (js_device [1], O_RDONLY | O_NONBLOCK);
61 #ifdef JSIOCGNAME
62 char name [130];
63 bzero (name, 128);
64 if (ioctl (js_fd [0], JSIOCGNAME(128), name) > 0)
66 printf ("Using %s (%s) as pad1", name, js_device [0]);
67 if (js_fd [1] > 0)
69 ioctl (js_fd [1], JSIOCGNAME(128), name);
70 printf ("and %s (%s) as pad2", name, js_device [1]);
73 else
75 #endif // JSIOCGNAME
77 ioctl (js_fd [0], JSIOCGAXES, &axes);
78 ioctl (js_fd [0], JSIOCGBUTTONS, &buttons);
79 printf ("Using %d-axis %d-button joystick (%s) as pad1", axes, buttons, js_device [0]);
80 if (js_fd [1] > 0)
82 ioctl (js_fd [0], JSIOCGAXES, &axes);
83 ioctl (js_fd [0], JSIOCGBUTTONS, &buttons);
84 printf (" and %d-axis %d-button (%s) as pad2", axes, buttons, js_device [1]);
87 puts (".");
89 #endif // JSIOCGVERSION
92 void md::read_joysticks()
94 #ifdef JSIOCGVERSION
95 struct js_event js_ev;
96 int i;
98 for (i = 0; i < 2 && js_fd [i] >= 0; i++)
100 while (read (js_fd[i], &js_ev, sizeof (struct js_event)) == sizeof (struct js_event) )
102 switch (js_ev.type & ~JS_EVENT_INIT)
104 case JS_EVENT_AXIS:
105 if (js_ev.number == 0)
107 if(js_ev.value < -16384)
109 pad[i] &= ~MD_LEFT_MASK;
110 pad[i] |= MD_RIGHT_MASK;
111 break;
113 if (js_ev.value > 16384)
115 pad[i] |= MD_LEFT_MASK;
116 pad[i] &= ~MD_RIGHT_MASK;
117 break;
119 pad[i] |= MD_LEFT_MASK;
120 pad[i] |= MD_RIGHT_MASK;
121 break;
124 if (js_ev.number == 1)
126 if (js_ev.value < -16384)
128 pad[i] &= ~MD_UP_MASK;
129 pad[i] |= MD_DOWN_MASK;
130 break;
132 if (js_ev.value > 16384)
134 pad[i] |= MD_UP_MASK;
135 pad[i] &= ~MD_DOWN_MASK;
136 break;
138 pad[i] |= MD_UP_MASK;
139 pad[i] |= MD_DOWN_MASK;
140 break;
143 break;
145 case JS_EVENT_BUTTON:
146 if (js_ev.number > 15)
147 break;
149 if (js_ev.value)
150 pad[i] &= ~js_map_button [i][js_ev.number];
151 else
152 pad[i] |= js_map_button [i][js_ev.number];
154 break;
158 #endif // JSIOCGVERSION
161 #elif defined(SDL_JOYSTICK_SUPPORT)
162 #warning USING SDL JOYSTICK
163 #include <SDL.h>
164 #include <SDL_joystick.h>
166 static SDL_Joystick *js_handle[2] = { NULL, NULL };
168 void md::init_joysticks() {
169 // Initialize the joystick support
170 // Thanks to Cameron Moore <cameron@unbeatenpath.net>
171 if(SDL_Init(SDL_INIT_JOYSTICK) < 0)
173 fprintf(stderr, "joystick: Unable to initialize joystick system\n");
174 return;
177 // Open the first couple of joysticks, if possible
178 js_handle[0] = SDL_JoystickOpen(0);
179 js_handle[1] = SDL_JoystickOpen(1);
181 // If neither opened, quit
182 if(!(js_handle[0] || js_handle[1]))
184 fprintf(stderr, "joystick: Unable to open any joysticks\n");
185 return;
188 // Print the joystick names
189 printf("Using ");
190 if(js_handle[0]) printf("%s (#0) as pad1 ", SDL_JoystickName(0));
191 if(js_handle[0] && js_handle[1]) printf("and ");
192 if(js_handle[1]) printf("%s (#1) as pad2 ", SDL_JoystickName(1));
193 printf("\n");
195 // Enable joystick events
196 SDL_JoystickEventState(SDL_ENABLE);
199 // This does nothing; SDL joystick handling is in the main event loop in
200 // sdl/sdl.cpp
201 void md::read_joysticks()
203 //empty
206 #endif