|
|
1.1 root 1: /*
2: Hatari - dlgJoystick.c
3:
4: This file is distributed under the GNU Public License, version 2 or at
5: your option any later version. Read the file gpl.txt for details.
6: */
1.1.1.3 ! root 7: char DlgJoystick_rcsid[] = "Hatari $Id: dlgJoystick.c,v 1.6 2005/09/29 08:36:59 thothy Exp $";
1.1 root 8:
9: #include "main.h"
10: #include "configuration.h"
11: #include "dialog.h"
12: #include "sdlgui.h"
13:
14:
1.1.1.3 ! root 15: #define DLGJOY_DISABLED 3
! 16: #define DLGJOY_USEREALJOY 4
! 17: #define DLGJOY_USEKEYS 5
! 18: #define DLGJOY_DEFINEKEYS 6
! 19: #define DLGJOY_SDLJOYNAME 8
! 20: #define DLGJOY_PREVSDLJOY 9
! 21: #define DLGJOY_NEXTSDLJOY 10
! 22: #define DLGJOY_AUTOFIRE 11
! 23: #define DLGJOY_STJOYNAME 13
! 24: #define DLGJOY_PREVSTJOY 14
! 25: #define DLGJOY_NEXTSTJOY 15
! 26: #define DLGJOY_EXIT 16
1.1 root 27:
28:
29: /* The joysticks dialog: */
1.1.1.3 ! root 30:
! 31: static char sSdlStickName[20];
! 32:
! 33: static SGOBJ joydlg[] =
1.1 root 34: {
1.1.1.3 ! root 35: { SGBOX, 0, 0, 0,0, 32,18, NULL },
! 36: { SGTEXT, 0, 0, 8,1, 15,1, "Joysticks setup" },
! 37:
! 38: { SGBOX, 0, 0, 1,4, 30,11, NULL },
! 39: { SGRADIOBUT, 0, 0, 2,5, 10,1, "disabled" },
! 40: { SGRADIOBUT, 0, 0, 2,9, 20,1, "use real joystick:" },
! 41: { SGRADIOBUT, 0, 0, 2,7, 14,1, "use keyboard" },
! 42:
! 43: { SGBUTTON, 0, 0, 19,7, 11,1, "Define keys" },
! 44: { SGBOX, 0, 0, 5,11, 22,1, NULL },
! 45: { SGTEXT, 0, 0, 6,11, 20,1, sSdlStickName },
! 46: { SGBUTTON, 0, 0, 4,11, 1,1, "\x04" }, /* Arrow left */
! 47: { SGBUTTON, 0, 0, 27,11, 1,1, "\x03" }, /* Arrow right */
! 48:
! 49: { SGCHECKBOX, 0, 0, 2,13, 17,1, "Enable autofire" },
! 50:
! 51: { SGBOX, 0, 0, 4,3, 24,1, NULL },
! 52: { SGTEXT, 0, 0, 5,3, 22,1, NULL },
! 53: { SGBUTTON, 0, 0, 1,3, 3,1, "\x04" }, /* Arrow left */
! 54: { SGBUTTON, 0, 0, 28,3, 3,1, "\x03" }, /* Arrow right */
! 55:
! 56: { SGBUTTON, 0, 0, 6,16, 20,1, "Back to main menu" },
! 57: { -1, 0, 0, 0,0, 0,0, NULL }
! 58: };
! 59:
! 60:
! 61: /* The joystick keys setup dialog: */
! 62:
! 63: static char sKeyInstruction[24];
! 64: static char sKeyName[24];
! 65:
! 66: static SGOBJ joykeysdlg[] =
! 67: {
! 68: { SGBOX, 0, 0, 0,0, 28,5, NULL },
! 69: { SGTEXT, 0, 0, 2,1, 24,1, sKeyInstruction },
! 70: { SGTEXT, 0, 0, 2,3, 24,1, sKeyName },
! 71: { -1, 0, 0, 0,0, 0,0, NULL }
! 72: };
! 73:
! 74:
! 75: static char *sJoystickNames[6] =
! 76: {
! 77: "ST Joystick 0",
! 78: "ST Joystick 1",
! 79: "STE Joypad A",
! 80: "STE Joypad B",
! 81: "Parallel port stick 1",
! 82: "Parallel port stick 2"
1.1 root 83: };
84:
85:
86: /*-----------------------------------------------------------------------*/
87: /*
1.1.1.3 ! root 88: Show dialogs for defining joystick keys and wait for a key press.
1.1 root 89: */
1.1.1.3 ! root 90: static void DlgJoystick_DefineOneKey(char *pType, int *pKey)
! 91: {
! 92: SDL_Event sdlEvent;
! 93:
! 94: if (bQuitProgram)
! 95: return;
! 96:
! 97: snprintf(sKeyInstruction, sizeof(sKeyInstruction), "Press key for '%s'...", pType);
! 98: snprintf(sKeyName, sizeof(sKeyName), "(was: '%s')", SDL_GetKeyName(*pKey));
! 99:
! 100: SDLGui_DrawDialog(joykeysdlg);
! 101:
! 102: do
! 103: {
! 104: SDL_WaitEvent(&sdlEvent);
! 105: if (sdlEvent.type == SDL_KEYDOWN)
! 106: {
! 107: *pKey = sdlEvent.key.keysym.sym;
! 108: snprintf(sKeyName, sizeof(sKeyName), "(now: '%s')", SDL_GetKeyName(*pKey));
! 109: SDLGui_DrawDialog(joykeysdlg);
! 110: }
! 111: else if (sdlEvent.type == SDL_QUIT)
! 112: {
! 113: bQuitProgram = TRUE;
! 114: return;
! 115: }
! 116: } while (sdlEvent.type != SDL_KEYUP);
! 117: SDL_Delay(200);
! 118: }
! 119:
! 120:
! 121: /*-----------------------------------------------------------------------*/
! 122: /*
! 123: Let the user define joystick keys.
! 124: */
! 125: static void DlgJoystick_DefineKeys(int nActJoy)
1.1 root 126: {
127:
1.1.1.3 ! root 128: SDLGui_CenterDlg(joykeysdlg);
! 129: DlgJoystick_DefineOneKey("up", &DialogParams.Joysticks.Joy[nActJoy].nKeyCodeUp);
! 130: DlgJoystick_DefineOneKey("down", &DialogParams.Joysticks.Joy[nActJoy].nKeyCodeDown);
! 131: DlgJoystick_DefineOneKey("left", &DialogParams.Joysticks.Joy[nActJoy].nKeyCodeLeft);
! 132: DlgJoystick_DefineOneKey("right", &DialogParams.Joysticks.Joy[nActJoy].nKeyCodeRight);
! 133: DlgJoystick_DefineOneKey("fire", &DialogParams.Joysticks.Joy[nActJoy].nKeyCodeFire);
! 134: }
! 135:
1.1 root 136:
1.1.1.3 ! root 137: /*-----------------------------------------------------------------------*/
! 138: /*
! 139: Adapt dialog using the values from the configration structure.
! 140: */
! 141: static void DlgJoystick_ReadValuesFromConf(int nActJoy, int nMaxId)
! 142: {
! 143: int i;
! 144:
! 145: /* Check if joystick ID is available */
! 146: if (SDL_NumJoysticks() == 0)
! 147: {
! 148: strcpy(sSdlStickName, "0: (none available)");
! 149: }
! 150: else if (DialogParams.Joysticks.Joy[nActJoy].nJoyId <= nMaxId)
! 151: {
! 152: snprintf(sSdlStickName, 20, "%i: %s", DialogParams.Joysticks.Joy[nActJoy].nJoyId,
! 153: SDL_JoystickName(DialogParams.Joysticks.Joy[nActJoy].nJoyId));
! 154: }
! 155: else
! 156: {
! 157: snprintf(sSdlStickName, 20, "0: %s", SDL_JoystickName(0));
! 158: /* Unavailable joystick ID -> disable it if necessary*/
! 159: if (DialogParams.Joysticks.Joy[nActJoy].nJoystickMode == JOYSTICK_REALSTICK)
! 160: DialogParams.Joysticks.Joy[nActJoy].nJoystickMode = JOYSTICK_DISABLED;
! 161: }
! 162:
! 163: for (i = DLGJOY_DISABLED; i <= DLGJOY_USEKEYS; i++)
! 164: joydlg[i].state &= ~SG_SELECTED;
! 165: joydlg[DLGJOY_DISABLED + DialogParams.Joysticks.Joy[nActJoy].nJoystickMode].state |= SG_SELECTED;
! 166:
! 167: if (DialogParams.Joysticks.Joy[nActJoy].bEnableAutoFire)
! 168: joydlg[DLGJOY_AUTOFIRE].state |= SG_SELECTED;
! 169: else
! 170: joydlg[DLGJOY_AUTOFIRE].state &= ~SG_SELECTED;
! 171: }
! 172:
! 173:
! 174: /*-----------------------------------------------------------------------*/
! 175: /*
! 176: Read values from dialog and write them to the configuration structure.
! 177: */
! 178: static void DlgJoystick_WriteValuesToConf(int nActJoy)
! 179: {
! 180: int i;
! 181: for (i = DLGJOY_DISABLED; i <= DLGJOY_USEKEYS; i++)
! 182: {
! 183: if (joydlg[i].state & SG_SELECTED)
! 184: {
! 185: DialogParams.Joysticks.Joy[nActJoy].nJoystickMode = i - DLGJOY_DISABLED;
! 186: break;
! 187: }
! 188: }
! 189:
! 190: DialogParams.Joysticks.Joy[nActJoy].bEnableAutoFire = (joydlg[DLGJOY_AUTOFIRE].state & SG_SELECTED);
! 191: DialogParams.Joysticks.Joy[nActJoy].nJoyId = joydlg[DLGJOY_SDLJOYNAME].txt[0] - '0';
! 192: }
! 193:
! 194:
! 195: /*-----------------------------------------------------------------------*/
! 196: /*
! 197: Show and process the joystick dialog.
! 198: */
! 199: void Dialog_JoyDlg(void)
! 200: {
! 201: int but;
! 202: static int nActJoy = 1;
! 203: int nMaxJoyId;
! 204:
! 205: SDLGui_CenterDlg(joydlg);
! 206:
! 207: joydlg[DLGJOY_STJOYNAME].txt = sJoystickNames[nActJoy];
! 208:
! 209: nMaxJoyId = SDL_NumJoysticks() - 1;
! 210: if (nMaxJoyId > 5)
! 211: nMaxJoyId = 5;
! 212:
! 213: /* Set up dialog from actual values: */
! 214: DlgJoystick_ReadValuesFromConf(nActJoy, nMaxJoyId);
! 215:
! 216: do
! 217: {
! 218: but = SDLGui_DoDialog(joydlg, NULL);
! 219: switch (but)
! 220: {
! 221: case DLGJOY_PREVSDLJOY: // Select the previous SDL joystick
! 222: if (DialogParams.Joysticks.Joy[nActJoy].nJoyId > '0')
! 223: {
! 224: DialogParams.Joysticks.Joy[nActJoy].nJoyId -= 1;
! 225: snprintf(sSdlStickName, 20, "%i: %s", DialogParams.Joysticks.Joy[nActJoy].nJoyId,
! 226: SDL_JoystickName(DialogParams.Joysticks.Joy[nActJoy].nJoyId));
! 227: }
! 228: break;
! 229: case DLGJOY_NEXTSDLJOY: // Select the next SDL joystick
! 230: if (DialogParams.Joysticks.Joy[nActJoy].nJoyId < nMaxJoyId)
! 231: {
! 232: DialogParams.Joysticks.Joy[nActJoy].nJoyId += 1;
! 233: snprintf(sSdlStickName, 20, "%i: %s", DialogParams.Joysticks.Joy[nActJoy].nJoyId,
! 234: SDL_JoystickName(DialogParams.Joysticks.Joy[nActJoy].nJoyId));
! 235: }
! 236: break;
! 237: case DLGJOY_DEFINEKEYS: // Define new keys for keyboard emulation
! 238: DlgJoystick_DefineKeys(nActJoy);
! 239: break;
! 240: case DLGJOY_PREVSTJOY: // Switch to the previous ST joystick setup tab
! 241: if (nActJoy > 0)
! 242: {
! 243: DlgJoystick_WriteValuesToConf(nActJoy);
! 244: nActJoy -= 1;
! 245: DlgJoystick_ReadValuesFromConf(nActJoy, nMaxJoyId);
! 246: joydlg[DLGJOY_STJOYNAME].txt = sJoystickNames[nActJoy];
! 247: }
! 248: break;
! 249: case DLGJOY_NEXTSTJOY: // Switch to the next ST joystick setup tab
! 250: if (nActJoy < 5)
! 251: {
! 252: DlgJoystick_WriteValuesToConf(nActJoy);
! 253: nActJoy += 1;
! 254: DlgJoystick_ReadValuesFromConf(nActJoy, nMaxJoyId);
! 255: joydlg[DLGJOY_STJOYNAME].txt = sJoystickNames[nActJoy];
! 256: }
! 257: break;
! 258: }
! 259: }
! 260: while (but != DLGJOY_EXIT && but != SDLGUI_QUIT && !bQuitProgram );
1.1 root 261:
1.1.1.3 ! root 262: DlgJoystick_WriteValuesToConf(nActJoy);
1.1 root 263: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.