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

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: 
                     51: void read_joystick(int nr, unsigned int *dir, int *button)
                     52: {
1.1.1.2 ! root       53:     static int minx = INT_MAX, maxx = INT_MIN;
        !            54:     static int miny = INT_MAX, maxy = INT_MIN;
1.1       root       55:     int left = 0, right = 0, top = 0, bot = 0;
1.1.1.2 ! root       56:     uae_joystick_t buffer;
1.1       root       57:     int len;
                     58:     int fd = nr == 0 ? js0 : js1;
                     59: 
                     60:     *dir = 0;
                     61:     *button = 0;
                     62:     if (nr >= nr_joysticks)
                     63:        return;
                     64:     
                     65:     len = read(fd, &buffer, sizeof(buffer));
                     66:     if (len != sizeof(buffer)) 
                     67:        return;
                     68:     
                     69:     if (buffer.x < minx) minx = buffer.x;
                     70:     if (buffer.y < miny) miny = buffer.y;
                     71:     if (buffer.x > maxx) maxx = buffer.x;
                     72:     if (buffer.y > maxy) maxy = buffer.y;
                     73:     
                     74:     if (buffer.x < (minx + (maxx-minx)/3))
                     75:        left = 1;
                     76:     else if (buffer.x > (minx + 2*(maxx-minx)/3))
                     77:        right = 1;
                     78: 
                     79:     if (buffer.y < (miny + (maxy-miny)/3))
                     80:        top = 1;
                     81:     else if (buffer.y > (miny + 2*(maxy-miny)/3))
                     82:        bot = 1;
                     83:        
                     84:     if (left) top = !top;
                     85:     if (right) bot = !bot;
                     86:     *dir = bot | (right << 1) | (top << 8) | (left << 9);
                     87:     *button = buffer.buttons & 3;
                     88: }
                     89: 
                     90: void init_joystick(void)
                     91: {
                     92:     nr_joysticks = 0;
                     93:     js0 = -1; js1 = -1;
1.1.1.2 ! root       94:     js0 = open("/dev/" JS_DEVNAME_PREFIX "0", O_RDONLY);
1.1       root       95:     if (js0 < 0) {
                     96:        write_log ("No joysticks found\n");
                     97:        return;
                     98:     }
                     99:     nr_joysticks = 1;
1.1.1.2 ! root      100:     js1 = open("/dev/" JS_DEVNAME_PREFIX "1", O_RDONLY);
1.1       root      101:     if (js1 < 0) {
                    102:        write_log ("Found one joystick\n");
                    103:        return;
                    104:     }
                    105:     write_log ("Found two joysticks\n");
                    106:     nr_joysticks = 2;
                    107: }
                    108: 
                    109: void close_joystick(void)
                    110: {
                    111:     if (js0 >= 0)
                    112:        close(js0);
                    113:     if (js1 >= 0)
                    114:        close(js1);
                    115: }

unix.superglobalmegacorp.com

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