Annotation of hatari/src/gui-sdl/dlgJoystick.c, revision 1.1.1.10

1.1       root        1: /*
                      2:   Hatari - dlgJoystick.c
                      3: 
1.1.1.10! root        4:   This file is distributed under the GNU General Public License, version 2
        !             5:   or at your option any later version. Read the file gpl.txt for details.
1.1       root        6: */
1.1.1.8   root        7: const char DlgJoystick_fileid[] = "Hatari dlgJoystick.c : " __DATE__ " " __TIME__;
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: 
1.1.1.5   root       56:        { SGBUTTON, SG_DEFAULT, 0, 6,16, 20,1, "Back to main menu" },
1.1.1.3   root       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: 
1.1.1.4   root       75: static char *sJoystickNames[JOYSTICK_COUNT] =
1.1.1.3   root       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: /*-----------------------------------------------------------------------*/
1.1.1.9   root       87: /**
                     88:  * Show dialogs for defining joystick keys and wait for a key press.
                     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:                {
1.1.1.8   root      113:                        bQuitProgram = true;
1.1.1.3   root      114:                        return;
                    115:                }
                    116:        } while (sdlEvent.type != SDL_KEYUP);
                    117:        SDL_Delay(200);
                    118: }
                    119: 
                    120: 
                    121: /*-----------------------------------------------------------------------*/
1.1.1.9   root      122: /**
                    123:  * Let the user define joystick keys.
                    124:  */
1.1.1.3   root      125: static void DlgJoystick_DefineKeys(int nActJoy)
1.1       root      126: {
                    127: 
1.1.1.3   root      128:        SDLGui_CenterDlg(joykeysdlg);
1.1.1.6   root      129:        DlgJoystick_DefineOneKey("up", &ConfigureParams.Joysticks.Joy[nActJoy].nKeyCodeUp);
                    130:        DlgJoystick_DefineOneKey("down", &ConfigureParams.Joysticks.Joy[nActJoy].nKeyCodeDown);
                    131:        DlgJoystick_DefineOneKey("left", &ConfigureParams.Joysticks.Joy[nActJoy].nKeyCodeLeft);
                    132:        DlgJoystick_DefineOneKey("right", &ConfigureParams.Joysticks.Joy[nActJoy].nKeyCodeRight);
                    133:        DlgJoystick_DefineOneKey("fire", &ConfigureParams.Joysticks.Joy[nActJoy].nKeyCodeFire);
1.1.1.3   root      134: }
                    135: 
1.1       root      136: 
1.1.1.3   root      137: /*-----------------------------------------------------------------------*/
1.1.1.9   root      138: /**
                    139:  * Adapt dialog using the values from the configration structure.
                    140:  */
1.1.1.3   root      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:        }
1.1.1.6   root      150:        else if (ConfigureParams.Joysticks.Joy[nActJoy].nJoyId <= nMaxId)
1.1.1.3   root      151:        {
1.1.1.6   root      152:                snprintf(sSdlStickName, 20, "%i: %s", ConfigureParams.Joysticks.Joy[nActJoy].nJoyId,
                    153:                         SDL_JoystickName(ConfigureParams.Joysticks.Joy[nActJoy].nJoyId));
1.1.1.3   root      154:        }
                    155:        else
                    156:        {
                    157:                snprintf(sSdlStickName, 20, "0: %s", SDL_JoystickName(0));
                    158:                /* Unavailable joystick ID -> disable it if necessary*/
1.1.1.6   root      159:                if (ConfigureParams.Joysticks.Joy[nActJoy].nJoystickMode == JOYSTICK_REALSTICK)
                    160:                        ConfigureParams.Joysticks.Joy[nActJoy].nJoystickMode = JOYSTICK_DISABLED;
1.1.1.3   root      161:        }
                    162: 
                    163:        for (i = DLGJOY_DISABLED; i <= DLGJOY_USEKEYS; i++)
                    164:                joydlg[i].state &= ~SG_SELECTED;
1.1.1.6   root      165:        joydlg[DLGJOY_DISABLED + ConfigureParams.Joysticks.Joy[nActJoy].nJoystickMode].state |= SG_SELECTED;
1.1.1.3   root      166: 
1.1.1.6   root      167:        if (ConfigureParams.Joysticks.Joy[nActJoy].bEnableAutoFire)
1.1.1.3   root      168:                joydlg[DLGJOY_AUTOFIRE].state |= SG_SELECTED;
                    169:        else
                    170:                joydlg[DLGJOY_AUTOFIRE].state &= ~SG_SELECTED;
                    171: }
                    172: 
                    173: 
                    174: /*-----------------------------------------------------------------------*/
1.1.1.9   root      175: /**
                    176:  * Read values from dialog and write them to the configuration structure.
                    177:  */
1.1.1.3   root      178: static void DlgJoystick_WriteValuesToConf(int nActJoy)
                    179: {
1.1.1.8   root      180:        JOYSTICKMODE jmi;
                    181:        for (jmi = JOYSTICK_DISABLED; jmi <= JOYSTICK_KEYBOARD; jmi++)
1.1.1.3   root      182:        {
1.1.1.8   root      183:                if (joydlg[jmi + DLGJOY_DISABLED].state & SG_SELECTED)
1.1.1.3   root      184:                {
1.1.1.8   root      185:                        ConfigureParams.Joysticks.Joy[nActJoy].nJoystickMode = jmi;
1.1.1.3   root      186:                        break;
                    187:                }
                    188:        }
                    189: 
1.1.1.6   root      190:        ConfigureParams.Joysticks.Joy[nActJoy].bEnableAutoFire = (joydlg[DLGJOY_AUTOFIRE].state & SG_SELECTED);
                    191:        ConfigureParams.Joysticks.Joy[nActJoy].nJoyId = joydlg[DLGJOY_SDLJOYNAME].txt[0] - '0';
1.1.1.3   root      192: }
                    193: 
                    194: 
                    195: /*-----------------------------------------------------------------------*/
1.1.1.9   root      196: /**
                    197:  * Show and process the joystick dialog.
                    198:  */
1.1.1.3   root      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
1.1.1.6   root      222:                        if (ConfigureParams.Joysticks.Joy[nActJoy].nJoyId > 0)
1.1.1.3   root      223:                        {
1.1.1.6   root      224:                                ConfigureParams.Joysticks.Joy[nActJoy].nJoyId -= 1;
                    225:                                snprintf(sSdlStickName, 20, "%i: %s", ConfigureParams.Joysticks.Joy[nActJoy].nJoyId,
                    226:                                         SDL_JoystickName(ConfigureParams.Joysticks.Joy[nActJoy].nJoyId));
1.1.1.3   root      227:                        }
                    228:                        break;
                    229:                 case DLGJOY_NEXTSDLJOY:        // Select the next SDL joystick
1.1.1.6   root      230:                        if (ConfigureParams.Joysticks.Joy[nActJoy].nJoyId < nMaxJoyId)
1.1.1.3   root      231:                        {
1.1.1.6   root      232:                                ConfigureParams.Joysticks.Joy[nActJoy].nJoyId += 1;
                    233:                                snprintf(sSdlStickName, 20, "%i: %s", ConfigureParams.Joysticks.Joy[nActJoy].nJoyId,
                    234:                                         SDL_JoystickName(ConfigureParams.Joysticks.Joy[nActJoy].nJoyId));
1.1.1.3   root      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:        }
1.1.1.5   root      260:        while (but != DLGJOY_EXIT && but != SDLGUI_QUIT
                    261:               && but != SDLGUI_ERROR && !bQuitProgram);
1.1       root      262: 
1.1.1.3   root      263:        DlgJoystick_WriteValuesToConf(nActJoy);
1.1       root      264: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.