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

1.1       root        1:  /* 
                      2:   * UAE - The Un*x Amiga Emulator
                      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       root        6:   * 
                      7:   * Copyright 1997 Bernd Schmidt
1.1.1.2   root        8:   * Copyright 1998 Krister Walfridsson
1.1       root        9:   */
                     10: 
                     11: #include "sysconfig.h"
                     12: #include "sysdeps.h"
                     13: 
                     14: #include "config.h"
                     15: #include "options.h"
                     16: #include "memory.h"
                     17: #include "custom.h"
                     18: #include "joystick.h"
                     19: 
1.1.1.4 ! root       20: #ifdef HAVE_MACHINE_JOYSTICK_H
1.1       root       21: 
1.1.1.2   root       22: /* The BSD way.  */
                     23: 
1.1.1.4 ! root       24: # include <machine/joystick.h>
        !            25: typedef struct joystick uae_joystick_t;
1.1.1.2   root       26: 
                     27: #define JS_DEVNAME_PREFIX "joy"
                     28: 
                     29: #else
                     30: 
                     31: /* The Linux way.  */
1.1       root       32: 
                     33: /* There are too many different versions of <linux/joystick.h>.  Rather
                     34:  * than trying to work correctly with all of them, we duplicate the
                     35:  * necessary definitions here.  */
1.1.1.2   root       36: typedef struct
1.1       root       37: {
                     38:     int buttons;
                     39:     int x;
                     40:     int y;
1.1.1.2   root       41: } uae_joystick_t;
1.1       root       42: 
1.1.1.2   root       43: #define JS_DEVNAME_PREFIX "js"
                     44: 
                     45: #endif
                     46: 
                     47: int nr_joysticks;
                     48: 
                     49: static int js0, js1;
1.1       root       50: 
1.1.1.3   root       51: struct joy_range
                     52: {
                     53:     int minx, maxx, miny, maxy;
                     54: } range0, range1;
                     55: 
1.1       root       56: void read_joystick(int nr, unsigned int *dir, int *button)
                     57: {
1.1.1.2   root       58:     static int minx = INT_MAX, maxx = INT_MIN;
                     59:     static int miny = INT_MAX, maxy = INT_MIN;
1.1       root       60:     int left = 0, right = 0, top = 0, bot = 0;
1.1.1.2   root       61:     uae_joystick_t buffer;
1.1       root       62:     int len;
                     63:     int fd = nr == 0 ? js0 : js1;
1.1.1.3   root       64:     struct joy_range *r = nr == 0 ? &range0 : &range1;
1.1       root       65: 
                     66:     *dir = 0;
                     67:     *button = 0;
                     68:     if (nr >= nr_joysticks)
                     69:        return;
                     70:     
                     71:     len = read(fd, &buffer, sizeof(buffer));
                     72:     if (len != sizeof(buffer)) 
                     73:        return;
                     74:     
1.1.1.3   root       75:     if (buffer.x < r->minx) r->minx = buffer.x;
                     76:     if (buffer.y < r->miny) r->miny = buffer.y;
                     77:     if (buffer.x > r->maxx) r->maxx = buffer.x;
                     78:     if (buffer.y > r->maxy) r->maxy = buffer.y;
1.1       root       79:     
1.1.1.3   root       80:     if (buffer.x < (r->minx + (r->maxx - r->minx)/3))
1.1       root       81:        left = 1;
1.1.1.3   root       82:     else if (buffer.x > (r->minx + 2*(r->maxx - r->minx)/3))
1.1       root       83:        right = 1;
                     84: 
1.1.1.3   root       85:     if (buffer.y < (r->miny + (r->maxy - r->miny)/3))
1.1       root       86:        top = 1;
1.1.1.3   root       87:     else if (buffer.y > (r->miny + 2*(r->maxy - r->miny)/3))
1.1       root       88:        bot = 1;
                     89:        
                     90:     if (left) top = !top;
                     91:     if (right) bot = !bot;
                     92:     *dir = bot | (right << 1) | (top << 8) | (left << 9);
1.1.1.4 ! root       93: #ifdef HAVE_MACHINE_JOYSTICK_H
        !            94:     *button = buffer.b1 || buffer.b2;
        !            95: #else
1.1       root       96:     *button = buffer.buttons & 3;
1.1.1.4 ! root       97: #endif
1.1       root       98: }
                     99: 
                    100: void init_joystick(void)
                    101: {
                    102:     nr_joysticks = 0;
                    103:     js0 = -1; js1 = -1;
1.1.1.2   root      104:     js0 = open("/dev/" JS_DEVNAME_PREFIX "0", O_RDONLY);
1.1       root      105:     if (js0 < 0) {
                    106:        write_log ("No joysticks found\n");
                    107:        return;
                    108:     }
                    109:     nr_joysticks = 1;
1.1.1.2   root      110:     js1 = open("/dev/" JS_DEVNAME_PREFIX "1", O_RDONLY);
1.1       root      111:     if (js1 < 0) {
                    112:        write_log ("Found one joystick\n");
                    113:        return;
                    114:     }
                    115:     write_log ("Found two joysticks\n");
                    116:     nr_joysticks = 2;
1.1.1.3   root      117: 
                    118:     range0.minx = INT_MAX;
                    119:     range0.maxx = INT_MIN;
                    120:     range0.miny = INT_MAX;
                    121:     range0.maxy = INT_MIN;
                    122:     range1.minx = INT_MAX;
                    123:     range1.maxx = INT_MIN;
                    124:     range1.miny = INT_MAX;
                    125:     range1.maxy = INT_MIN;
1.1       root      126: }
                    127: 
                    128: void close_joystick(void)
                    129: {
                    130:     if (js0 >= 0)
1.1.1.3   root      131:        close (js0);
1.1       root      132:     if (js1 >= 0)
1.1.1.3   root      133:        close (js1);
1.1       root      134: }

unix.superglobalmegacorp.com

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