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

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

unix.superglobalmegacorp.com

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