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 */
30 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
43 /* Gets the current joystick position information. */
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 */
52 for (n = 0; n < 4; n++)
58 /* Axis information. */
59 pos = SDL_JoystickGetAxis(jo[n], joyAMap[n][0]);
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]);
66 ret |= JOY_UP << (n << 3);
67 else if (pos >= 16383)
68 ret |= JOY_DOWN << (n << 3);
70 /* Button information. */
71 for (b = 0; b < 4; b++)
73 if (SDL_JoystickGetButton(jo[n], joym[b]))
74 ret |= (1 << b) << (n << 3);
81 /* Cleanup opened joysticks. */
82 void KillJoysticks (void)
84 int n; /* joystick index */
86 for (n = 0; n < 4; n++)
89 SDL_JoystickClose(jo[n]);
91 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
95 /* Initialize joysticks. */
96 int InitJoysticks (void)
98 int n; /* joystick index */
99 if(!(joy[0]|joy[1]|joy[2]|joy[3]))
101 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
102 for (n = 0; n < 4; n++)
107 /* Open the joystick under SDL. */
108 jo[n] = SDL_JoystickOpen(joy[n] - 1);
111 printf("Could not open joystick %d: %s.\n",
112 joy[n] - 1, SDL_GetError());
117 /* Check for a button map. */
118 if (!(joyBMap[n][0] | joyBMap[n][1] | joyBMap[n][2] |
128 #define WNOINPUT(); for(;;){uint8 t; if(read(fileno(stdin),&t,1)==-1) \
131 /* Configure a joystick button. */
132 static void BConfig (int n, int b)
134 SDL_Event event; /* SDL event structure */
139 if (read(fileno(stdin), &t, 1) == -1)
147 if (SDL_PollEvent(&event) && event.type == SDL_JOYBUTTONDOWN)
149 joyBMap[n][b] = event.jbutton.button;
160 /* Joystick button and axis configuration. */
161 void ConfigJoystick (int n)
163 int sa; /* buffer value */
164 char buf[128]; /* input buffer */
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");
174 printf("**** Configuring joystick %d ****\n\n", n + 1);
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;
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;
184 sa = fcntl(fileno(stdin), F_GETFL);
185 fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
187 printf("** Press button for \"Select\".\n");
190 printf("** Press button for \"Start\".\n");
193 printf("** Press button for \"B\".\n");
196 printf("** Press button for \"A\".\n");
199 fcntl(fileno(stdin), F_SETFL, sa);