1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Ben Parnell
5 * Copyright (C) 2002 Paul Kuliniewicz
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.
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.
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
22 /* PK: SDL joystick input stuff */
31 static SDL_Joystick *jo[4] = {NULL, NULL, NULL, NULL};
32 static void ConfigJoystick (int z);
36 #define JOY_SELECT 0x04
37 #define JOY_START 0x08
41 #define JOY_RIGHT 0x80
44 /* Gets the current joystick position information. */
48 int n; /* joystick index */
49 int b; /* button index */
50 int *joym; /* pointer to a joystick's button map */
51 Sint16 pos; /* axis position */
52 uint32 ret = 0; /* return value */
54 for (n = 0; n < 4; n++)
60 /* Axis information. */
61 pos = SDL_JoystickGetAxis(jo[n], joyAMap[n][0]);
63 ret |= JOY_LEFT << (n << 3);
64 else if (pos >= 16363)
65 ret |= JOY_RIGHT << (n << 3);
66 pos = SDL_JoystickGetAxis(jo[n], joyAMap[n][1]);
68 ret |= JOY_UP << (n << 3);
69 else if (pos >= 16383)
70 ret |= JOY_DOWN << (n << 3);
72 /* Button information. */
73 for (b = 0; b < 4; b++)
75 if (SDL_JoystickGetButton(jo[n], joym[b]))
76 ret |= (1 << b) << (n << 3);
86 /* Cleanup opened joysticks. */
87 void KillJoysticks (void)
90 int n; /* joystick index */
92 for (n = 0; n < 4; n++)
95 SDL_JoystickClose(jo[n]);
97 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
102 /* Initialize joysticks. */
103 int InitJoysticks (void)
106 int n; /* joystick index */
107 if(!(joy[0]|joy[1]|joy[2]|joy[3]))
109 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
110 for (n = 0; n < 4; n++)
115 /* Open the joystick under SDL. */
116 jo[n] = SDL_JoystickOpen(joy[n] - 1);
119 printf("Could not open joystick %d: %s.\n",
120 joy[n] - 1, SDL_GetError());
125 /* Check for a button map. */
126 if (!(joyBMap[n][0] | joyBMap[n][1] | joyBMap[n][2] |
137 #define WNOINPUT(); for(;;){uint8 t; if(read(fileno(stdin),&t,1)==-1) \
140 /* Configure a joystick button. */
141 static void BConfig (int n, int b)
144 SDL_Event event; /* SDL event structure */
149 if (read(fileno(stdin), &t, 1) == -1)
157 if (SDL_PollEvent(&event) && event.type == SDL_JOYBUTTONDOWN)
159 joyBMap[n][b] = event.jbutton.button;
171 /* Joystick button and axis configuration. */
172 void ConfigJoystick (int n)
175 int sa; /* buffer value */
176 char buf[128]; /* input buffer */
178 printf("\n\n Joystick button and axis configuration:\n\n");
179 printf(" Select the joystick axes to use for the virtual d-pad.\n");
180 printf(" If you do not wish to assign an axis, press Enter to skip\n");
181 printf(" that axis.\n");
182 printf(" Push the button to map to the virtual joystick.\n");
183 printf(" If you do not wish to assign a button, press Enter to skip\n");
184 printf(" that button.\n Press enter to continue...\n");
186 printf("**** Configuring joystick %d ****\n\n", n + 1);
188 printf("** Enter axis to use for the x-axis (default 0).\n");
189 fgets(buf, sizeof(buf), stdin);
190 joyAMap[n][0] = ('0' <= buf[0] && buf[9] <= '9') ? atoi(buf) : 0;
192 printf("** Enter axis to use for the y-axis (default 1).\n");
193 fgets(buf, sizeof(buf), stdin);
194 joyAMap[n][1] = ('0' <= buf[0] && buf[9] <= '9') ? atoi(buf) : 1;
196 sa = fcntl(fileno(stdin), F_GETFL);
197 fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
199 printf("** Press button for \"Select\".\n");
202 printf("** Press button for \"Start\".\n");
205 printf("** Press button for \"B\".\n");
208 printf("** Press button for \"A\".\n");
211 fcntl(fileno(stdin), F_SETFL, sa);