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

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.2   root       20: #ifdef HAVE_MACHINE_SOUNDCARD_H
1.1       root       21: 
1.1.1.2   root       22: /* The BSD way.  */
                     23: 
                     24: # include <machine/soundcard.h>
                     25: typedef struct joystick uae_joystick_t
                     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);
                     93:     *button = buffer.buttons & 3;
                     94: }
                     95: 
                     96: void init_joystick(void)
                     97: {
                     98:     nr_joysticks = 0;
                     99:     js0 = -1; js1 = -1;
1.1.1.2   root      100:     js0 = open("/dev/" JS_DEVNAME_PREFIX "0", O_RDONLY);
1.1       root      101:     if (js0 < 0) {
                    102:        write_log ("No joysticks found\n");
                    103:        return;
                    104:     }
                    105:     nr_joysticks = 1;
1.1.1.2   root      106:     js1 = open("/dev/" JS_DEVNAME_PREFIX "1", O_RDONLY);
1.1       root      107:     if (js1 < 0) {
                    108:        write_log ("Found one joystick\n");
                    109:        return;
                    110:     }
                    111:     write_log ("Found two joysticks\n");
                    112:     nr_joysticks = 2;
1.1.1.3 ! root      113: 
        !           114:     range0.minx = INT_MAX;
        !           115:     range0.maxx = INT_MIN;
        !           116:     range0.miny = INT_MAX;
        !           117:     range0.maxy = INT_MIN;
        !           118:     range1.minx = INT_MAX;
        !           119:     range1.maxx = INT_MIN;
        !           120:     range1.miny = INT_MAX;
        !           121:     range1.maxy = INT_MIN;
1.1       root      122: }
                    123: 
                    124: void close_joystick(void)
                    125: {
                    126:     if (js0 >= 0)
1.1.1.3 ! root      127:        close (js0);
1.1       root      128:     if (js1 >= 0)
1.1.1.3 ! root      129:        close (js1);
1.1       root      130: }

unix.superglobalmegacorp.com

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