Annotation of uae/src/od-linux/joystick.c, revision 1.1.1.6

1.1.1.6 ! root        1:  /*
1.1       root        2:   * UAE - The Un*x Amiga Emulator
1.1.1.6 ! root        3:   *
1.1.1.2   root        4:   * Joystick emulation for Linux and BSD. They share too much code to
                      5:   * split this file.
1.1.1.6 ! root        6:   *
        !             7:   * This uses the deprecated 0.x Linux joystick API.
        !             8:   *
1.1       root        9:   * Copyright 1997 Bernd Schmidt
1.1.1.2   root       10:   * Copyright 1998 Krister Walfridsson
1.1.1.6 ! root       11:   * Copyright 2003-2004 Richard Drummond
        !            12:   * Copyright 2004 Nick Seow (Alternative Linux joystick device path)
1.1       root       13:   */
                     14: 
                     15: #include "sysconfig.h"
                     16: #include "sysdeps.h"
                     17: 
                     18: #include "config.h"
                     19: #include "options.h"
                     20: #include "memory.h"
                     21: #include "custom.h"
1.1.1.6 ! root       22: #include "inputdevice.h"
        !            23: 
        !            24: #define JS_MAXPATHLEN  20      // Longest device name would be "/dev/input/js"
        !            25: char js_prefix[JS_MAXPATHLEN]; // Joystick device, which varies, except number
1.1       root       26: 
1.1.1.4   root       27: #ifdef HAVE_MACHINE_JOYSTICK_H
1.1       root       28: 
1.1.1.2   root       29: /* The BSD way.  */
                     30: 
1.1.1.4   root       31: # include <machine/joystick.h>
                     32: typedef struct joystick uae_joystick_t;
1.1.1.2   root       33: 
                     34: #define JS_DEVNAME_PREFIX "joy"
                     35: 
                     36: #else
                     37: 
                     38: /* The Linux way.  */
1.1       root       39: 
                     40: /* There are too many different versions of <linux/joystick.h>.  Rather
                     41:  * than trying to work correctly with all of them, we duplicate the
                     42:  * necessary definitions here.  */
1.1.1.2   root       43: typedef struct
1.1       root       44: {
                     45:     int buttons;
                     46:     int x;
                     47:     int y;
1.1.1.2   root       48: } uae_joystick_t;
1.1       root       49: 
1.1.1.6 ! root       50: #define JS_DEVNAME_PREFIX "js" // Try this first
1.1.1.2   root       51: 
                     52: #endif
                     53: 
1.1.1.6 ! root       54: /* Hard code these for the old joystick API */
        !            55: #define MAX_BUTTONS  2
        !            56: #define MAX_AXLES    2
        !            57: #define FIRST_AXLE   0
        !            58: #define FIRST_BUTTON 2
        !            59: 
        !            60: static int nr_joysticks;
1.1.1.2   root       61: 
                     62: static int js0, js1;
1.1       root       63: 
1.1.1.3   root       64: struct joy_range
                     65: {
                     66:     int minx, maxx, miny, maxy;
1.1.1.6 ! root       67:     int centrex, centrey;
1.1.1.3   root       68: } range0, range1;
                     69: 
1.1.1.6 ! root       70: static void read_joy(int nr)
1.1       root       71: {
1.1.1.2   root       72:     uae_joystick_t buffer;
1.1       root       73:     int len;
                     74:     int fd = nr == 0 ? js0 : js1;
1.1.1.3   root       75:     struct joy_range *r = nr == 0 ? &range0 : &range1;
1.1       root       76: 
                     77:     if (nr >= nr_joysticks)
1.1.1.6 ! root       78:        return;
        !            79: 
1.1       root       80:     len = read(fd, &buffer, sizeof(buffer));
1.1.1.6 ! root       81:     if (len != sizeof(buffer))
        !            82:        return;
        !            83: 
        !            84:     /* According to old 0.x JS API, we don't know the range
        !            85:      * or the centre for either axis, so we try to work these
        !            86:      * out as we go along.
        !            87:      *
        !            88:      * Must be a better way to do this . . .
        !            89:      */
1.1.1.3   root       90:     if (buffer.x < r->minx) r->minx = buffer.x;
                     91:     if (buffer.y < r->miny) r->miny = buffer.y;
                     92:     if (buffer.x > r->maxx) r->maxx = buffer.x;
                     93:     if (buffer.y > r->maxy) r->maxy = buffer.y;
1.1.1.6 ! root       94: 
        !            95:     r->centrex = (r->maxx-r->minx)/2 + r->minx;
        !            96:     r->centrey = (r->maxy-r->miny)/2 + r->miny;
        !            97: 
        !            98:     /* Translate these values to be centred on 0 and
        !            99:      * feed 'em to the inputdevice system */
        !           100:     setjoystickstate (nr, 0, buffer.x - r->centrex, r->centrex);
        !           101:     setjoystickstate (nr, 1, buffer.y - r->centrey, r->centrey);
        !           102: 
1.1.1.4   root      103: #ifdef HAVE_MACHINE_JOYSTICK_H
1.1.1.6 ! root      104:     setjoybuttonstate (nr, 0, buffer.b1);
        !           105:     setjoybuttonstate (nr, 1, buffer.b2);
1.1.1.4   root      106: #else
1.1.1.6 ! root      107:     setjoybuttonstate (nr, 0, buffer.buttons & 1);
        !           108:     setjoybuttonstate (nr, 1, buffer.buttons & 2);
1.1.1.4   root      109: #endif
1.1       root      110: }
                    111: 
1.1.1.6 ! root      112: static int init_joysticks(void)
1.1       root      113: {
                    114:     nr_joysticks = 0;
                    115:     js0 = -1; js1 = -1;
1.1.1.6 ! root      116:     char js_path[JS_MAXPATHLEN]; // temporary buffer for device name
        !           117: 
        !           118:     snprintf (js_prefix, JS_MAXPATHLEN, "/dev/%s", JS_DEVNAME_PREFIX);
        !           119: 
        !           120:     snprintf (js_path, JS_MAXPATHLEN, "%s0", js_prefix);
        !           121:     if ((js0 = open (js_path, O_RDONLY)) >= 0)
        !           122:        nr_joysticks++;
        !           123: 
        !           124:     snprintf (js_path, JS_MAXPATHLEN, "%s1", js_prefix);
        !           125:     if ((js1 = open (js_path, O_RDONLY)) >= 0)
        !           126:        nr_joysticks++;
        !           127: 
        !           128: #ifdef __linux__
        !           129:    if (nr_joysticks == 0) {
        !           130:        /*
        !           131:         * If we haven't found any joysticks yet,
        !           132:         * look for /dev/input/js* nodes
        !           133:         */
        !           134:        sprintf (js_prefix, "/dev/input/%s", JS_DEVNAME_PREFIX);
        !           135: 
        !           136:        snprintf (js_path, JS_MAXPATHLEN, "%s0", js_prefix);
        !           137:        if ((js0 = open (js_path, O_RDONLY)) >= 0)
        !           138:            nr_joysticks++;
        !           139: 
        !           140:        snprintf (js_path, JS_MAXPATHLEN, "%s1", js_prefix);
        !           141:        if ((js1 = open (js_path, O_RDONLY)) >= 0)
        !           142:            nr_joysticks++;
1.1       root      143:     }
1.1.1.6 ! root      144: #endif
        !           145: 
        !           146:     write_log ("Found %d joystick(s)\n", nr_joysticks);
1.1.1.3   root      147: 
                    148:     range0.minx = INT_MAX;
                    149:     range0.maxx = INT_MIN;
                    150:     range0.miny = INT_MAX;
                    151:     range0.maxy = INT_MIN;
                    152:     range1.minx = INT_MAX;
                    153:     range1.maxx = INT_MIN;
                    154:     range1.miny = INT_MAX;
                    155:     range1.maxy = INT_MIN;
1.1.1.6 ! root      156:     range0.centrex = 0;
        !           157:     range1.centrey = 0;
        !           158:     return 1;
1.1       root      159: }
                    160: 
1.1.1.6 ! root      161: static void close_joysticks(void)
1.1       root      162: {
                    163:     if (js0 >= 0)
1.1.1.3   root      164:        close (js0);
1.1       root      165:     if (js1 >= 0)
1.1.1.3   root      166:        close (js1);
1.1       root      167: }
1.1.1.6 ! root      168: 
        !           169: static int acquire_joy (int num, int flags)
        !           170: {
        !           171:     return 1;
        !           172: }
        !           173: 
        !           174: static void unacquire_joy (int num)
        !           175: {
        !           176: }
        !           177: 
        !           178: static int get_joystick_num (void)
        !           179: {
        !           180:     return nr_joysticks;
        !           181: }
        !           182: 
        !           183: static void read_joysticks (void)
        !           184: {
        !           185:     int i;
        !           186:     for (i = 0; i < get_joystick_num (); i++) {
        !           187:        if (currprefs.input_selected_setting == 0) {
        !           188:            if (jsem_isjoy (0, &currprefs) != i && jsem_isjoy (1, &currprefs) != i)
        !           189:                continue;
        !           190:        }
        !           191:        read_joy (i);
        !           192:     }
        !           193: }
        !           194: 
        !           195: static char *get_joystick_name (int joy)
        !           196: {
        !           197:     static char name[100];
        !           198:     sprintf (name, "%d: %s%d", joy + 1, js_prefix, joy);
        !           199:     return name;
        !           200: }
        !           201: 
        !           202: static int get_joystick_widget_num (int joy)
        !           203: {
        !           204:     return MAX_AXLES + MAX_BUTTONS;
        !           205: }
        !           206: 
        !           207: static int get_joystick_widget_type (int joy, int num, char *name, uae_u32 *dummy)
        !           208: {
        !           209:     if (num >= MAX_AXLES && num < MAX_AXLES+MAX_BUTTONS) {
        !           210:        if (name)
        !           211:            sprintf (name, "Button %d", num + 1 - MAX_AXLES);
        !           212:        return IDEV_WIDGET_BUTTON;
        !           213:     } else if (num < MAX_AXLES) {
        !           214:        if (name)
        !           215:            sprintf (name, "Axis %d", num + 1);
        !           216:        return IDEV_WIDGET_AXIS;
        !           217:     }
        !           218:     return IDEV_WIDGET_NONE;
        !           219: }
        !           220: 
        !           221: static int get_joystick_widget_first (int joy, int type)
        !           222: {
        !           223:     switch (type) {
        !           224:        case IDEV_WIDGET_BUTTON:
        !           225:            return FIRST_BUTTON;
        !           226:        case IDEV_WIDGET_AXIS:
        !           227:            return FIRST_AXLE;
        !           228:     }
        !           229: 
        !           230:     return -1;
        !           231: }
        !           232: 
        !           233: struct inputdevice_functions inputdevicefunc_joystick = {
        !           234:     init_joysticks, close_joysticks, acquire_joy, unacquire_joy,
        !           235:     read_joysticks, get_joystick_num, get_joystick_name,
        !           236:     get_joystick_widget_num, get_joystick_widget_type,
        !           237:     get_joystick_widget_first
        !           238: };
        !           239: 
        !           240: /*
        !           241:  * Set default inputdevice config for joysticks
        !           242:  */
        !           243: void input_get_default_joystick (struct uae_input_device *uid)
        !           244: {
        !           245:     int i, port;
        !           246: 
        !           247:     for (i = 0; i < nr_joysticks; i++) {
        !           248:        port = i & 1;
        !           249:        uid[i].eventid[ID_AXIS_OFFSET + 0][0]   = port ? INPUTEVENT_JOY2_HORIZ : INPUTEVENT_JOY1_HORIZ;
        !           250:        uid[i].eventid[ID_AXIS_OFFSET + 1][0]   = port ? INPUTEVENT_JOY2_VERT  : INPUTEVENT_JOY1_VERT;
        !           251:        uid[i].eventid[ID_BUTTON_OFFSET + 0][0] = port ? INPUTEVENT_JOY2_FIRE_BUTTON : INPUTEVENT_JOY1_FIRE_BUTTON;
        !           252:        uid[i].eventid[ID_BUTTON_OFFSET + 1][0] = port ? INPUTEVENT_JOY2_2ND_BUTTON  : INPUTEVENT_JOY1_2ND_BUTTON;
        !           253:        uid[i].eventid[ID_BUTTON_OFFSET + 2][0] = port ? INPUTEVENT_JOY2_3RD_BUTTON  : INPUTEVENT_JOY1_3RD_BUTTON;
        !           254:     }
        !           255:     uid[0].enabled = 1;
        !           256: }

unix.superglobalmegacorp.com

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