fixed: broken fs0, sram saves
[fceu.git] / drivers / cli / sdl-joystick.c
CommitLineData
63bd5be0 1/* FCE Ultra - NES/Famicom Emulator
2 *
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Ben Parnell
5 * Copyright (C) 2002 Paul Kuliniewicz
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program 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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22/* PK: SDL joystick input stuff */
23
24#include <stdlib.h>
25#include <unistd.h>
26#include <fcntl.h>
27#include <errno.h>
28
29#include "sdl.h"
30static SDL_Joystick *jo[4] = {NULL, NULL, NULL, NULL};
31
32static void ConfigJoystick (int z);
33
34#define JOY_A 0x01
35#define JOY_B 0x02
36#define JOY_SELECT 0x04
37#define JOY_START 0x08
38#define JOY_UP 0x10
39#define JOY_DOWN 0x20
40#define JOY_LEFT 0x40
41#define JOY_RIGHT 0x80
42
43/* Gets the current joystick position information. */
44uint32 GetJSOr (void)
45{
46 int n; /* joystick index */
47 int b; /* button index */
48 int *joym; /* pointer to a joystick's button map */
49 Sint16 pos; /* axis position */
50 uint32 ret = 0; /* return value */
51
52 for (n = 0; n < 4; n++)
53 {
54 if (joy[n] == 0)
55 continue;
56 joym = joyBMap[n];
57
58 /* Axis information. */
59 pos = SDL_JoystickGetAxis(jo[n], joyAMap[n][0]);
60 if (pos <= -16383)
61 ret |= JOY_LEFT << (n << 3);
62 else if (pos >= 16363)
63 ret |= JOY_RIGHT << (n << 3);
64 pos = SDL_JoystickGetAxis(jo[n], joyAMap[n][1]);
65 if (pos <= -16383)
66 ret |= JOY_UP << (n << 3);
67 else if (pos >= 16383)
68 ret |= JOY_DOWN << (n << 3);
69
70 /* Button information. */
71 for (b = 0; b < 4; b++)
72 {
73 if (SDL_JoystickGetButton(jo[n], joym[b]))
74 ret |= (1 << b) << (n << 3);
75 }
76 }
77
78 return ret;
79}
80
81/* Cleanup opened joysticks. */
82void KillJoysticks (void)
83{
84 int n; /* joystick index */
85
86 for (n = 0; n < 4; n++)
87 {
88 if (joy[n] != 0)
89 SDL_JoystickClose(jo[n]);
90 }
91 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
92 return;
93}
94
95/* Initialize joysticks. */
96int InitJoysticks (void)
97{
98 int n; /* joystick index */
99 if(!(joy[0]|joy[1]|joy[2]|joy[3]))
100 return(0);
101 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
102 for (n = 0; n < 4; n++)
103 {
104 if (joy[n] == 0)
105 continue;
106
107 /* Open the joystick under SDL. */
108 jo[n] = SDL_JoystickOpen(joy[n] - 1);
109 if (jo[n] == NULL)
110 {
111 printf("Could not open joystick %d: %s.\n",
112 joy[n] - 1, SDL_GetError());
113 joy[n] = 0;
114 continue;
115 }
116
117 /* Check for a button map. */
118 if (!(joyBMap[n][0] | joyBMap[n][1] | joyBMap[n][2] |
119 joyBMap[n][3]))
120 {
121 ConfigJoystick(n);
122 }
123 }
124
125 return (1);
126}
127
128#define WNOINPUT(); for(;;){uint8 t; if(read(fileno(stdin),&t,1)==-1) \
129 {break;}}
130
131/* Configure a joystick button. */
132static void BConfig (int n, int b)
133{
134 SDL_Event event; /* SDL event structure */
135 WNOINPUT();
136 while (1)
137 {
138 uint8 t;
139 if (read(fileno(stdin), &t, 1) == -1)
140 {
141 if (errno != EAGAIN)
142 break;
143 }
144 else
145 break;
146
147 if (SDL_PollEvent(&event) && event.type == SDL_JOYBUTTONDOWN)
148 {
149 joyBMap[n][b] = event.jbutton.button;
150 goto endsa;
151 }
152 }
153
154 endsa:
155 WNOINPUT();
156
157 return;
158}
159
160/* Joystick button and axis configuration. */
161void ConfigJoystick (int n)
162{
163 int sa; /* buffer value */
164 char buf[128]; /* input buffer */
165
166 printf("\n\n Joystick button and axis configuration:\n\n");
167 printf(" Select the joystick axes to use for the virtual d-pad.\n");
168 printf(" If you do not wish to assign an axis, press Enter to skip\n");
169 printf(" that axis.\n");
170 printf(" Push the button to map to the virtual joystick.\n");
171 printf(" If you do not wish to assign a button, press Enter to skip\n");
172 printf(" that button.\n Press enter to continue...\n");
173 getchar();
174 printf("**** Configuring joystick %d ****\n\n", n + 1);
175
176 printf("** Enter axis to use for the x-axis (default 0).\n");
177 fgets(buf, sizeof(buf), stdin);
178 joyAMap[n][0] = ('0' <= buf[0] && buf[9] <= '9') ? atoi(buf) : 0;
179
180 printf("** Enter axis to use for the y-axis (default 1).\n");
181 fgets(buf, sizeof(buf), stdin);
182 joyAMap[n][1] = ('0' <= buf[0] && buf[9] <= '9') ? atoi(buf) : 1;
183
184 sa = fcntl(fileno(stdin), F_GETFL);
185 fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
186
187 printf("** Press button for \"Select\".\n");
188 BConfig(n, 2);
189
190 printf("** Press button for \"Start\".\n");
191 BConfig(n, 3);
192
193 printf("** Press button for \"B\".\n");
194 BConfig(n, 1);
195
196 printf("** Press button for \"A\".\n");
197 BConfig(n, 0);
198
199 fcntl(fileno(stdin), F_SETFL, sa);
200}