Annotation of uae/src/sdl-joystick.c, revision 1.1.1.3

1.1.1.2   root        1:  /*
1.1       root        2:   * UAE - The Un*x Amiga Emulator
1.1.1.2   root        3:   *
                      4:   * SDL Joystick code
                      5:   *
1.1       root        6:   * Copyright 1997 Bernd Schmidt
                      7:   * Copyright 1998 Krister Walfridsson
1.1.1.3 ! root        8:   * Copyright 2003-2005 Richard Drummond
1.1       root        9:   */
                     10: 
                     11: #include "sysconfig.h"
                     12: #include "sysdeps.h"
                     13: 
                     14: #include "options.h"
1.1.1.2   root       15: #include "inputdevice.h"
                     16: #include <SDL.h>
                     17: 
1.1.1.3 ! root       18: static unsigned int nr_joysticks;
1.1.1.2   root       19: static int initialized;
                     20: 
                     21: struct joyinfo {
                     22:     SDL_Joystick *joy;
1.1.1.3 ! root       23:     unsigned int axles;
        !            24:     unsigned int buttons;
1.1.1.2   root       25: };
                     26: 
                     27: static struct joyinfo joys[MAX_INPUT_DEVICES];
                     28: 
                     29: 
1.1.1.3 ! root       30: static void read_joy (unsigned int nr)
1.1.1.2   root       31: {
1.1.1.3 ! root       32:     unsigned int num, i, axes, axis;
1.1.1.2   root       33:     SDL_Joystick *joy;
                     34: 
                     35:     if (currprefs.input_selected_setting == 0) {
1.1.1.3 ! root       36:        if (jsem_isjoy (0, &currprefs) != (int)nr && jsem_isjoy (1, &currprefs) != (int)nr)
1.1.1.2   root       37:            return;
                     38:     }
                     39:     joy = joys[nr].joy;
                     40:     axes = SDL_JoystickNumAxes (joy);
                     41:     for (i = 0; i < axes; i++) {
                     42:        axis = SDL_JoystickGetAxis (joy, i);
                     43:        setjoystickstate (nr, i, axis, 32767);
                     44:     }
                     45: 
1.1       root       46:     num = SDL_JoystickNumButtons (joy);
1.1.1.2   root       47:     for (i = 0; i < num; i++) {
                     48:        int bs = SDL_JoystickGetButton (joy, i) ? 1 : 0;
                     49:        setjoybuttonstate (nr, i, bs);
                     50:     }
                     51: }
                     52: 
1.1.1.3 ! root       53: static unsigned int get_joystick_num (void)
1.1.1.2   root       54: {
                     55:     return nr_joysticks;
                     56: }
                     57: 
1.1.1.3 ! root       58: static unsigned int get_joystick_widget_num (unsigned int joy)
1.1.1.2   root       59: {
                     60:     return joys[joy].axles + joys[joy].buttons;
                     61: }
                     62: 
1.1.1.3 ! root       63: static int get_joystick_widget_type (unsigned int joy, unsigned int num, char *name, uae_u32 *code)
1.1.1.2   root       64: {
                     65:     if (num >= joys[joy].axles && num < joys[joy].axles + joys[joy].buttons) {
                     66:        if (name)
                     67:            sprintf (name, "Button %d", num + 1 - joys[joy].axles);
                     68:        return IDEV_WIDGET_BUTTON;
                     69:     } else if (num < joys[joy].axles) {
                     70:        if (name)
                     71:            sprintf (name, "Axis %d", num + 1);
                     72:        return IDEV_WIDGET_AXIS;
                     73:     }
                     74:     return IDEV_WIDGET_NONE;
                     75: }
                     76: 
1.1.1.3 ! root       77: static int get_joystick_widget_first (unsigned int joy, int type)
1.1.1.2   root       78: {
                     79:     switch (type) {
                     80:        case IDEV_WIDGET_BUTTON:
                     81:            return joys[joy].axles;
                     82:        case IDEV_WIDGET_AXIS:
                     83:            return 0;
                     84:     }
                     85:     return -1;
1.1       root       86: }
                     87: 
1.1.1.3 ! root       88: static const char *get_joystick_name (unsigned int joy)
1.1.1.2   root       89: {
1.1.1.3 ! root       90:     return SDL_JoystickName (joy);
1.1.1.2   root       91: }
                     92: 
                     93: static void read_joysticks (void)
                     94: {
                     95:     if (get_joystick_num ()) {
1.1.1.3 ! root       96:        unsigned int i;
1.1.1.2   root       97:        SDL_JoystickUpdate ();
                     98:        for (i = 0; i < get_joystick_num (); i++)
                     99:            read_joy (i);
                    100:     }
                    101: }
                    102: 
                    103: static int init_joysticks (void)
                    104: {
                    105:     int success = 0;
                    106: 
                    107:     if (!initialized) {
                    108:        if (SDL_InitSubSystem (SDL_INIT_JOYSTICK) == 0) {
1.1.1.3 ! root      109:            unsigned int i;
1.1.1.2   root      110: 
                    111:            nr_joysticks = SDL_NumJoysticks ();
                    112:            write_log ("Found %d joystick(s)\n", nr_joysticks);
                    113: 
                    114:            if (nr_joysticks > MAX_INPUT_DEVICES)
                    115:                nr_joysticks = MAX_INPUT_DEVICES;
                    116: 
                    117:            for (i = 0; i < get_joystick_num (); i++) {
                    118:                joys[i].joy     = SDL_JoystickOpen (i);
                    119:                joys[i].axles   = SDL_JoystickNumAxes (joys[i].joy);
                    120:                joys[i].buttons = SDL_JoystickNumButtons (joys[i].joy);
                    121:            }
                    122:            success = initialized = 1;
                    123:        } else
                    124:            write_log ("Failed to initialize joysticks\n");
                    125:     }
                    126: 
                    127:     return success;
                    128: }
                    129: 
                    130: static void close_joysticks (void)
1.1       root      131: {
1.1.1.3 ! root      132:     unsigned int i;
        !           133:     for (i = 0; i < nr_joysticks; i++) {
1.1.1.2   root      134:        SDL_JoystickClose (joys[i].joy);
                    135:        joys[i].joy = 0;
                    136:     }
1.1.1.3 ! root      137:     nr_joysticks = 0;
1.1.1.2   root      138: 
1.1.1.3 ! root      139:     if (initialized) {
1.1.1.2   root      140:        SDL_QuitSubSystem (SDL_INIT_JOYSTICK);
1.1.1.3 ! root      141:        initialized = 0;
        !           142:     }
1.1.1.2   root      143: }
                    144: 
1.1.1.3 ! root      145: static int acquire_joy (unsigned int num, int flags)
1.1.1.2   root      146: {
                    147:     return num < get_joystick_num ();
                    148: }
                    149: 
1.1.1.3 ! root      150: static void unacquire_joy (unsigned int num)
1.1.1.2   root      151: {
                    152: }
                    153: 
                    154: struct inputdevice_functions inputdevicefunc_joystick = {
1.1.1.3 ! root      155:     init_joysticks,
        !           156:     close_joysticks,
        !           157:     acquire_joy,
        !           158:     unacquire_joy,
        !           159:     read_joysticks,
        !           160:     get_joystick_num,
        !           161:     get_joystick_name,
        !           162:     get_joystick_widget_num,
        !           163:     get_joystick_widget_type,
1.1.1.2   root      164:     get_joystick_widget_first
                    165: };
                    166: 
                    167: /*
                    168:  * Set default inputdevice config for SDL joysticks
                    169:  */
                    170: void input_get_default_joystick (struct uae_input_device *uid)
                    171: {
1.1.1.3 ! root      172:     unsigned int i, port;
1.1.1.2   root      173: 
                    174:     for (i = 0; i < nr_joysticks; i++) {
                    175:        port = i & 1;
                    176:        uid[i].eventid[ID_AXIS_OFFSET + 0][0]   = port ? INPUTEVENT_JOY2_HORIZ : INPUTEVENT_JOY1_HORIZ;
                    177:        uid[i].eventid[ID_AXIS_OFFSET + 1][0]   = port ? INPUTEVENT_JOY2_VERT  : INPUTEVENT_JOY1_VERT;
                    178:        uid[i].eventid[ID_BUTTON_OFFSET + 0][0] = port ? INPUTEVENT_JOY2_FIRE_BUTTON : INPUTEVENT_JOY1_FIRE_BUTTON;
                    179:        uid[i].eventid[ID_BUTTON_OFFSET + 1][0] = port ? INPUTEVENT_JOY2_2ND_BUTTON  : INPUTEVENT_JOY1_2ND_BUTTON;
1.1.1.3 ! root      180:        uid[i].eventid[ID_BUTTON_OFFSET + 2][0] = port ? INPUTEVENT_JOY2_FIRE_BUTTON_AF : INPUTEVENT_JOY1_FIRE_BUTTON_AF;
1.1.1.2   root      181:     }
                    182:     uid[0].enabled = 1;
1.1       root      183: }

unix.superglobalmegacorp.com

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