Annotation of uae/src/inputdevice.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/mouse emulation
                      5:   *
                      6:   * Copyright 2001, 2002 Toni Wilen
                      7:   *
                      8:   * new fetures:
                      9:   * - very configurable (and very complex to configure :)
                     10:   * - supports multiple native input devices (joysticks and mice)
                     11:   * - supports mapping joystick/mouse buttons to keys and vice versa
                     12:   * - joystick mouse emulation (supports both ports)
                     13:   * - supports parallel port joystick adapter
                     14:   * - full cd32 pad support (supports both ports)
                     15:   * - fully backward compatible with old joystick/mouse configuration
1.1       root       16:   *
                     17:   */
                     18: 
1.1.1.2   root       19: //#define DONGLE_DEBUG
                     20: 
1.1       root       21: #include "sysconfig.h"
                     22: #include "sysdeps.h"
                     23: 
                     24: #include "options.h"
                     25: #include "custom.h"
                     26: #include "xwin.h"
1.1.1.2   root       27: #include "drawing.h"
1.1       root       28: #include "memory.h"
1.1.1.2   root       29: #include "events.h"
1.1       root       30: #include "newcpu.h"
1.1.1.2   root       31: #include "uae.h"
1.1       root       32: #include "picasso96.h"
1.1.1.2   root       33: #include "debug.h"
                     34: #include "gui.h"
                     35: #include "disk.h"
                     36: #include "audio.h"
1.1.1.4 ! root       37: #include "autoconf.h"
        !            38: #include "traps.h"
        !            39: #include "keyboard.h"
        !            40: #include "inputdevice.h"
        !            41: #include "keybuf.h"
1.1.1.2   root       42: #include "savestate.h"
                     43: 
                     44: #define DIR_LEFT 1
                     45: #define DIR_RIGHT 2
                     46: #define DIR_UP 4
                     47: #define DIR_DOWN 8
                     48: 
                     49: struct inputevent {
                     50:     char *confname;
                     51:     char *name;
                     52:     int allow_mask;
                     53:     int type;
                     54:     int unit;
                     55:     int data;
                     56: };
                     57: 
                     58: #define JOYBUTTON_1 0 /* fire/left mousebutton */
                     59: #define JOYBUTTON_2 1 /* 2nd/right mousebutton */
                     60: #define JOYBUTTON_3 2 /* 3rd/middle mousebutton */
                     61: #define JOYBUTTON_CD32_PLAY 3
                     62: #define JOYBUTTON_CD32_RWD 4
                     63: #define JOYBUTTON_CD32_FFW 5
                     64: #define JOYBUTTON_CD32_GREEN 6
                     65: #define JOYBUTTON_CD32_YELLOW 7
                     66: #define JOYBUTTON_CD32_RED 8
                     67: #define JOYBUTTON_CD32_BLUE 9
                     68: 
                     69: #define INPUTEVENT_JOY1_CD32_FIRST INPUTEVENT_JOY1_CD32_PLAY
                     70: #define INPUTEVENT_JOY2_CD32_FIRST INPUTEVENT_JOY2_CD32_PLAY
                     71: #define INPUTEVENT_JOY1_CD32_LAST INPUTEVENT_JOY1_CD32_BLUE
                     72: #define INPUTEVENT_JOY2_CD32_LAST INPUTEVENT_JOY2_CD32_BLUE
                     73: 
                     74: /* event masks */
                     75: #define AM_KEY 1 /* keyboard allowed */
                     76: #define AM_JOY_BUT 2 /* joystick buttons allowed */
                     77: #define AM_JOY_AXIS 4 /* joystick axis allowed */
                     78: #define AM_MOUSE_BUT 8 /* mouse buttons allowed */
                     79: #define AM_MOUSE_AXIS 16 /* mouse direction allowed */
                     80: #define AM_AF 32 /* supports autofire */
                     81: #define AM_INFO 64 /* information data for gui */
                     82: #define AM_DUMMY 128 /* placeholder */
1.1.1.3   root       83: #define AM_AUTO 256 /* forces autofire */
1.1.1.2   root       84: #define AM_K (AM_KEY|AM_JOY_BUT|AM_MOUSE_BUT|AM_AF) /* generic button/switch */
                     85: 
                     86: /* event flags */
                     87: #define ID_FLAG_AUTOFIRE 1
                     88: 
                     89: #define DEFEVENT(A, B, C, D, E, F) {#A, B, C, D, E, F },
                     90: struct inputevent events[] = {
1.1.1.3   root       91: {0, 0, AM_K, 0, 0, 0},
1.1.1.2   root       92: #include "inputevents.def"
                     93: {0, 0, 0, 0, 0, 0}
                     94: };
                     95: #undef DEFEVENT
                     96: 
                     97: static int sublevdir[2][MAX_INPUT_SUB_EVENT];
                     98: 
                     99: struct uae_input_device2 {
                    100:     uae_u32 buttonmask;
                    101:     int states[MAX_INPUT_DEVICE_EVENTS / 2];
                    102: };
1.1       root      103: 
1.1.1.2   root      104: static struct uae_input_device2 joysticks2[MAX_INPUT_DEVICES];
                    105: static struct uae_input_device2 mice2[MAX_INPUT_DEVICES];
1.1       root      106: 
1.1.1.2   root      107: static uae_u8 mouse_settings_reset[MAX_INPUT_SETTINGS][MAX_INPUT_DEVICES];
                    108: static uae_u8 joystick_settings_reset[MAX_INPUT_SETTINGS][MAX_INPUT_DEVICES];
1.1       root      109: 
1.1.1.3   root      110: static struct inputdevice_functions idev[3];
                    111: 
1.1.1.2   root      112: static int isdevice (struct uae_input_device *id)
                    113: {
                    114:     int i, j;
                    115:     for (i = 0; i < MAX_INPUT_DEVICE_EVENTS; i++) {
                    116:        for (j = 0; j < MAX_INPUT_SUB_EVENT; j++) {
                    117:            if (id->eventid[i][j] > 0)
                    118:                return 1;
                    119:        }
                    120:     }
                    121:     return 0;
                    122: }
1.1       root      123: 
1.1.1.2   root      124: int inputdevice_uaelib (char *s, char *parm)
                    125: {
                    126:     int i;
                    127: 
                    128:     for (i = 1; events[i].name; i++) {
                    129:        if (!strcmp (s, events[i].confname)) {
                    130:            handle_input_event (i, atol (parm), 1, 0);
                    131:            return 1;
                    132:        }
                    133:     }
                    134:     return 0;
                    135: }
                    136: 
                    137: static struct uae_input_device *joysticks;
                    138: static struct uae_input_device *mice;
                    139: static struct uae_input_device *keyboards;
                    140: static struct uae_input_device_kbr_default *keyboard_default;
                    141: 
                    142: static double mouse_axis[MAX_INPUT_DEVICES][MAX_INPUT_DEVICE_EVENTS];
                    143: static double oldm_axis[MAX_INPUT_DEVICES][MAX_INPUT_DEVICE_EVENTS];
                    144: 
                    145: static int mouse_x[MAX_INPUT_DEVICES], mouse_y[MAX_INPUT_DEVICE_EVENTS];
                    146: static int mouse_delta[MAX_INPUT_DEVICES][MAX_INPUT_DEVICE_EVENTS];
                    147: static int mouse_deltanoreset[MAX_INPUT_DEVICES][MAX_INPUT_DEVICE_EVENTS];
                    148: static int joybutton[MAX_INPUT_DEVICES];
                    149: static unsigned int joydir[MAX_INPUT_DEVICE_EVENTS];
                    150: static int joydirpot[MAX_INPUT_DEVICE_EVENTS][2];
                    151: static int mouse_frame_x[2], mouse_frame_y[2];
                    152: 
                    153: static int mouse_port[2];
                    154: static int cd32_shifter[2];
                    155: static int cd32_pad_enabled[2];
                    156: static int parport_joystick_enabled;
                    157: static int oldmx[4], oldmy[4];
                    158: static int oleft[4], oright[4], otop[4], obot[4];
                    159: static int potgo_hsync;
                    160: 
                    161: static int use_joysticks[MAX_INPUT_DEVICES];
                    162: static int use_mice[MAX_INPUT_DEVICES];
                    163: static int use_keyboards[MAX_INPUT_DEVICES];
                    164: 
                    165: #define INPUT_QUEUE_SIZE 16
                    166: struct input_queue_struct {
                    167:     int event, storedstate, state, max, framecnt, nextframecnt;
                    168: };
                    169: static struct input_queue_struct input_queue[INPUT_QUEUE_SIZE];
                    170: 
                    171: /* Sigh.  */
                    172: #define cfgfile_write fprintf
                    173: 
                    174: static void out_config (FILE *f, int id, int num, char *s1, char *s2)
                    175: {
                    176:     cfgfile_write (f, "input.%d.%s%d=%s\n", id, s1, num, s2);
                    177:     //write_log ("-input.%d.%s%d=%s\n", id, s1, num, s2);
                    178: }
                    179: 
                    180: static void write_config2 (FILE *f, int idnum, int i, int offset, char *tmp1, struct uae_input_device *id)
                    181: {
                    182:     char tmp2[200], *p;
                    183:     int event, got, j, k;
                    184:     char *custom;
                    185: 
                    186:     p = tmp2;
                    187:     got = 0;
                    188:     for (j = 0; j < MAX_INPUT_SUB_EVENT; j++) {
                    189:        event = id->eventid[i + offset][j];
                    190:        custom = id->custom[i + offset][j];
                    191:        if (custom == NULL && event <= 0) {
                    192:            for (k = j + 1; k < MAX_INPUT_SUB_EVENT; k++) {
                    193:                if (id->eventid[i + offset][k] > 0) break;
                    194:            }
                    195:            if (k == MAX_INPUT_SUB_EVENT)
                    196:                break;
                    197:        }
                    198:        if (p > tmp2) {
                    199:            *p++ = ',';
                    200:            *p = 0;
                    201:        }
                    202:        if (custom)
                    203:            sprintf (p, "'%s'.%d", custom, id->flags[i + offset][j]);
                    204:        else if (event <= 0)
                    205:            sprintf (p, "NULL");
                    206:        else
                    207:            sprintf (p, "%s.%d", events[event].confname, id->flags[i + offset][j]);
                    208:        p += strlen (p);
                    209:     }
                    210:     if (p > tmp2)
                    211:        out_config (f, idnum, i, tmp1, tmp2);
                    212: }
1.1       root      213: 
1.1.1.2   root      214: static void write_config (FILE *f, int idnum, int devnum, char *name, struct uae_input_device *id, struct uae_input_device2 *id2)
                    215: {
                    216:     char tmp1[100];
                    217:     int i;
1.1       root      218: 
1.1.1.2   root      219:     if (!isdevice (id))
                    220:        return;
                    221:     cfgfile_write (f, "input.%d.%s.%d.disabled=%d\n", idnum, name, devnum, id->enabled ? 0 : 1);
                    222:     sprintf (tmp1, "%s.%d.axis.", name, devnum);
                    223:     for (i = 0; i < ID_AXIS_TOTAL; i++)
                    224:        write_config2 (f, idnum, i, ID_AXIS_OFFSET, tmp1, id);
                    225:     sprintf (tmp1, "%s.%d.button." ,name, devnum);
                    226:     for (i = 0; i < ID_BUTTON_TOTAL; i++)
                    227:        write_config2 (f, idnum, i, ID_BUTTON_OFFSET, tmp1, id);
                    228: }
1.1       root      229: 
1.1.1.2   root      230: static void kbrlabel (char *s)
                    231: {
                    232:     while (*s) {
                    233:        *s = toupper(*s);
                    234:        if (*s == ' ') *s = '_';
                    235:        s++;
                    236:     }
                    237: }
1.1       root      238: 
1.1.1.2   root      239: static void write_kbr_config (FILE *f, int idnum, int devnum, struct uae_input_device *kbr)
1.1       root      240: {
1.1.1.2   root      241:     char tmp1[200], tmp2[200], tmp3[200], *p;
                    242:     int i, j, k, event, skip;
                    243: 
                    244:     if (!keyboard_default)
1.1       root      245:        return;
1.1.1.2   root      246:     i = 0;
                    247:     while (i < MAX_INPUT_DEVICE_EVENTS && kbr->extra[i][0] >= 0) {
                    248:        skip = 0;
                    249:        k = 0;
                    250:        while (keyboard_default[k].scancode >= 0) {
                    251:            if (keyboard_default[k].scancode == kbr->extra[i][0]) {
                    252:                skip = 1;
                    253:                for (j = 1; j < MAX_INPUT_SUB_EVENT; j++) {
                    254:                    if (kbr->flags[i][j] || kbr->eventid[i][j] > 0)
                    255:                        skip = 0;
                    256:                }
                    257:                if (keyboard_default[k].event != kbr->eventid[i][0] || kbr->flags[i][0] != 0)
                    258:                    skip = 0;
                    259:                break;
                    260:            }
                    261:            k++;
                    262:        }
                    263:        if (kbr->eventid[i][0] == 0 && kbr->flags[i][0] == 0 && keyboard_default[k].scancode < 0)
                    264:            skip = 1;
                    265:        if (skip) {
                    266:            i++;
                    267:            continue;
                    268:        }
                    269:        p = tmp2;
                    270:        p[0] = 0;
                    271:        for (j = 0; j < MAX_INPUT_SUB_EVENT; j++) {
                    272:            char *custom = kbr->custom[i][j];
                    273:            event = kbr->eventid[i][j];
                    274:            if (custom == NULL && event <= 0) {
                    275:                for (k = j + 1; k < MAX_INPUT_SUB_EVENT; k++) {
                    276:                    if (kbr->eventid[i][k] > 0) break;
                    277:                }
                    278:                if (k == MAX_INPUT_SUB_EVENT)
                    279:                    break;
                    280:            }
                    281:            if (p > tmp2) {
                    282:                *p++ = ',';
                    283:                *p = 0;
                    284:            }
                    285:            if (custom)
                    286:                sprintf (p, "'%s'.%d", custom, kbr->flags[i][j]);
                    287:            else if (event > 0)
                    288:                sprintf (p, "%s.%d", events[event].confname, kbr->flags[i][j]);
                    289:            else
                    290:                strcat (p, "NULL");
                    291:            p += strlen(p);
                    292:        }
                    293:        sprintf (tmp3, "%d", kbr->extra[i][0]);
                    294:        kbrlabel (tmp3);
                    295:        sprintf (tmp1, "keyboard.%d.button.%s", devnum, tmp3);
                    296:        cfgfile_write (f, "input.%d.%s=%s\n", idnum, tmp1, tmp2);
                    297:        i++;
                    298:     }
                    299: }
                    300: 
                    301: void write_inputdevice_config (struct uae_prefs *p, FILE *f)
                    302: {
                    303:     int i, id;
                    304: 
                    305:     cfgfile_write (f, "input.config=%d\n", p->input_selected_setting);
                    306:     cfgfile_write (f, "input.joymouse_speed_analog=%d\n", p->input_joymouse_multiplier);
                    307:     cfgfile_write (f, "input.joymouse_speed_digital=%d\n", p->input_joymouse_speed);
                    308:     cfgfile_write (f, "input.joymouse_deadzone=%d\n", p->input_joymouse_deadzone);
                    309:     cfgfile_write (f, "input.joystick_deadzone=%d\n", p->input_joystick_deadzone);
                    310:     cfgfile_write (f, "input.mouse_speed=%d\n", p->input_mouse_speed);
                    311:     cfgfile_write (f, "input.autofire=%d\n", p->input_autofire_framecnt);
                    312:     for (id = 1; id <= MAX_INPUT_SETTINGS; id++) {
                    313:        for (i = 0; i < MAX_INPUT_DEVICES; i++)
                    314:            write_config (f, id, i, "joystick", &p->joystick_settings[id][i], &joysticks2[i]);
                    315:        for (i = 0; i < MAX_INPUT_DEVICES; i++)
                    316:            write_config (f, id, i, "mouse", &p->mouse_settings[id][i], &mice2[i]);
                    317:        for (i = 0; i < MAX_INPUT_DEVICES; i++)
                    318:            write_kbr_config (f, id, i, &p->keyboard_settings[id][i]);
                    319:     }
                    320: }
                    321: 
                    322: static int getnum (char **pp)
                    323: {
                    324:     char *p = *pp;
                    325:     int v = atol (p);
                    326: 
1.1.1.3   root      327:     while (*p != 0 && *p !='.' && *p != ',')
                    328:        p++;
                    329:     if (*p == '.' || *p == ',')
                    330:        p++;
1.1.1.2   root      331:     *pp = p;
                    332:     return v;
                    333: }
                    334: static char *getstring (char **pp)
                    335: {
                    336:     int i;
                    337:     static char str[1000];
                    338:     char *p = *pp;
                    339: 
                    340:     if (*p == 0)
                    341:        return 0;
                    342:     i = 0;
1.1.1.3   root      343:     while (*p != 0 && *p !='.' && *p != ',')
                    344:        str[i++] = *p++;
                    345:     if (*p == '.' || *p == ',')
                    346:        p++;
1.1.1.2   root      347:     str[i] = 0;
                    348:     *pp = p;
                    349:     return str;
                    350: }
                    351: 
                    352: void reset_inputdevice_config (struct uae_prefs *pr)
                    353: {
                    354:     memset (joystick_settings_reset, 0, sizeof (joystick_settings_reset));
                    355:     memset (mouse_settings_reset, 0, sizeof (mouse_settings_reset));
                    356: }
1.1       root      357: 
1.1.1.2   root      358: static void clear_id (struct uae_input_device *id)
                    359: {
                    360: #ifndef _DEBUG
                    361:     int i, j;
                    362:     for (i = 0; i < MAX_INPUT_DEVICE_EVENTS; i++) {
                    363:        for (j = 0; j < MAX_INPUT_SUB_EVENT; j++)
                    364:            free (id->custom[i][j]);
                    365:     }
                    366: #endif
                    367:     memset (id, 0, sizeof (struct uae_input_device));
                    368:     id->enabled = 1;
1.1       root      369: }
                    370: 
1.1.1.2   root      371: void read_inputdevice_config (struct uae_prefs *pr, char *option, char *value)
1.1       root      372: {
1.1.1.2   root      373:     struct uae_input_device *id = 0;
                    374:     struct inputevent *ie;
                    375:     int devnum, num, button, joystick, flags, i, subnum, idnum, keynum;
                    376:     int mask;
                    377:     char *p, *p2, *custom;
                    378: 
                    379:     option += 6; /* "input." */
                    380:     p = getstring (&option);
                    381:     if (!strcasecmp (p, "config"))
                    382:        pr->input_selected_setting = atol (value);
                    383:     if (!strcasecmp (p, "joymouse_speed_analog"))
                    384:        pr->input_joymouse_multiplier = atol (value);
                    385:     if (!strcasecmp (p, "joymouse_speed_digital"))
                    386:        pr->input_joymouse_speed = atol (value);
                    387:     if (!strcasecmp (p, "joystick_deadzone"))
                    388:        pr->input_joystick_deadzone = atol (value);
                    389:     if (!strcasecmp (p, "joymouse_deadzone"))
                    390:        pr->input_joymouse_deadzone = atol (value);
                    391:     if (!strcasecmp (p, "mouse_speed"))
                    392:        pr->input_mouse_speed = atol (value);
                    393:     if (!strcasecmp (p, "autofire"))
                    394:        pr->input_autofire_framecnt = atol (value);
                    395:     idnum = atol (p);
                    396:     if (idnum <= 0 || idnum > MAX_INPUT_SETTINGS)
                    397:        return;
                    398:     if (memcmp (option, "mouse.", 6) == 0) {
                    399:        p = option + 6;
                    400:        devnum = getnum (&p);
                    401:        if (devnum < 0 || devnum >= MAX_INPUT_DEVICES)
                    402:            return;
                    403:        id = &pr->mouse_settings[idnum][devnum];
                    404:        if (!mouse_settings_reset[idnum][devnum])
                    405:            clear_id (id);
                    406:        mouse_settings_reset[idnum][devnum] = 1;
                    407:        joystick = 0;
                    408:     } else if (memcmp (option, "joystick.", 9) == 0) {
                    409:        p = option + 9;
                    410:        devnum = getnum (&p);
                    411:        if (devnum < 0 || devnum >= MAX_INPUT_DEVICES)
                    412:            return;
                    413:        id = &pr->joystick_settings[idnum][devnum];
                    414:        if (!joystick_settings_reset[idnum][devnum])
                    415:            clear_id (id);
                    416:        joystick_settings_reset[idnum][devnum] = 1;
                    417:        joystick = 1;
                    418:     } else if (memcmp (option, "keyboard.", 9) == 0) {
                    419:        joystick = -1;
                    420:        p = option + 9;
                    421:        devnum = getnum (&p);
                    422:        if (devnum < 0 || devnum >= MAX_INPUT_DEVICES)
                    423:            return;
                    424:        id = &pr->keyboard_settings[idnum][devnum];
                    425:     }
                    426:     if (!id)
                    427:        return;
                    428:     p2 = getstring (&p);
                    429:     if (!p2)
1.1       root      430:        return;
1.1.1.2   root      431:     if (!strcmp (p2, "disabled")) {
                    432:        int disabled;
                    433:        p = value;
                    434:        disabled = getnum (&p);
                    435:        id->enabled = disabled == 0 ? 1 : 0;
                    436:        return;
                    437:     }
                    438: 
                    439:     if (joystick < 0) {
                    440:        num = getnum (&p);
                    441:        keynum = 0;
                    442:        while (id->extra[keynum][0] >= 0) {
                    443:            if (id->extra[keynum][0] == num)
                    444:                break;
                    445:            keynum++;
                    446:        }
                    447:        if (id->extra[keynum][0] < 0)
                    448:            return;
                    449:     } else {
                    450:        button = -1;
                    451:        if (!strcmp (p2, "axis"))
                    452:            button = 0;
                    453:        else if(!strcmp (p2, "button"))
                    454:            button = 1;
                    455:        if (button < 0)
                    456:            return;
                    457:        num = getnum (&p);
                    458:     }
                    459:     p = value;
                    460: 
                    461:     custom = NULL;
                    462:     for (subnum = 0; subnum < MAX_INPUT_SUB_EVENT; subnum++) {
                    463:        free (custom);
                    464:        custom = NULL;
                    465:        p2 = getstring (&p);
                    466:        if (!p2)
                    467:            break;
                    468:        i = 1;
                    469:        while (events[i].name) {
                    470:            if (!strcmp (events[i].confname, p2))
                    471:                break;
                    472:            i++;
                    473:        }
                    474:        ie = &events[i];
                    475:        if (!ie->name) {
                    476:            ie = &events[0];
                    477:            if (strlen (p2) > 2 && p2[0] == '\'' && p2[strlen (p2) - 1] == '\'') {
                    478:                custom = my_strdup (p2 + 1);
                    479:                custom[strlen (custom) - 1] = 0;
                    480:            }
                    481:        }
                    482:        flags = getnum (&p);
                    483:        if (custom == NULL && ie->name == NULL)
                    484:            continue;
                    485:        if (joystick < 0) {
                    486:            if (!(ie->allow_mask & AM_K))
                    487:                continue;
                    488:            id->eventid[keynum][subnum] = ie - events;
                    489:            id->flags[keynum][subnum] = flags;
                    490:            free (id->custom[keynum][subnum]);
                    491:            id->custom[keynum][subnum] = custom;
                    492:        } else  if (button) {
                    493:            if (joystick)
                    494:                mask = AM_JOY_BUT;
                    495:            else
                    496:                mask = AM_MOUSE_BUT;
                    497:            if (!(ie->allow_mask & mask))
                    498:                continue;
                    499:            id->eventid[num + ID_BUTTON_OFFSET][subnum] = ie - events;
                    500:            id->flags[num + ID_BUTTON_OFFSET][subnum] = flags;
                    501:            free (id->custom[num + ID_BUTTON_OFFSET][subnum]);
                    502:            id->custom[num + ID_BUTTON_OFFSET][subnum] = custom;
                    503:        } else {
                    504:            if (joystick)
                    505:                mask = AM_JOY_AXIS;
                    506:            else
                    507:                mask = AM_MOUSE_AXIS;
                    508:            if (!(ie->allow_mask & mask))
                    509:                continue;
                    510:            id->eventid[num + ID_AXIS_OFFSET][subnum] = ie - events;
                    511:            id->flags[num + ID_AXIS_OFFSET][subnum] = flags;
                    512:            free (id->custom[num + ID_AXIS_OFFSET][subnum]);
                    513:            id->custom[num + ID_AXIS_OFFSET][subnum] = custom;
                    514:        }
                    515:        custom = NULL;
                    516:     }
                    517:     free (custom);
                    518: }
                    519: 
                    520: /* Mousehack stuff */
1.1       root      521: 
1.1.1.2   root      522: static int ievent_alive = 0;
                    523: static int lastmx, lastmy;
                    524: 
1.1.1.4 ! root      525: static void mousehack_setpos (int mousexpos, int mouseypos)
        !           526: {
        !           527:     uae_u8 *p;
        !           528:     if (!uae_boot_rom)
        !           529:        return;
        !           530:     p = rtarea + get_long (RTAREA_BASE + 40) + 12;
        !           531:     p[0] = mousexpos >> 8;
        !           532:     p[1] = mousexpos;
        !           533:     p[2] = mouseypos >> 8;
        !           534:     p[3] = mouseypos;
        !           535:     //write_log ("%dx%d\n", mousexpos, mouseypos);
        !           536: }
        !           537: 
        !           538: static void new_mousehack_helper (void)
        !           539: {
        !           540:     int mousexpos, mouseypos;
        !           541: 
        !           542:     if (!mousehack_allowed ())
        !           543:        return;
        !           544: #ifdef PICASSO96
        !           545:     if (picasso_on) {
        !           546:        mousexpos = lastmx - picasso96_state.XOffset;
        !           547:        mouseypos = lastmy - picasso96_state.YOffset;
        !           548:     } else
        !           549: #endif
        !           550:     {
        !           551:        mouseypos = coord_native_to_amiga_y (lastmy) << 1;
        !           552:        mousexpos = coord_native_to_amiga_x (lastmx);
        !           553:     }
        !           554:     mousehack_setpos (mousexpos, mouseypos);
        !           555: }
        !           556: 
1.1.1.2   root      557: int mousehack_alive (void)
                    558: {
                    559:     return ievent_alive > 0;
1.1       root      560: }
                    561: 
1.1.1.4 ! root      562: uae_u32 mousehack_helper (TrapContext *dummy)
1.1       root      563: {
                    564:     int mousexpos, mouseypos;
                    565: 
                    566: #ifdef PICASSO96
                    567:     if (picasso_on) {
1.1.1.2   root      568:        mousexpos = lastmx - picasso96_state.XOffset;
                    569:        mouseypos = lastmy - picasso96_state.YOffset;
1.1       root      570:     } else
                    571: #endif
                    572:     {
1.1.1.2   root      573:        if (mouse_y[0] >= gfxvidinfo.height)
                    574:            mouse_y[0] = gfxvidinfo.height - 1;
1.1       root      575:        mouseypos = coord_native_to_amiga_y (lastmy) << 1;
                    576:        mousexpos = coord_native_to_amiga_x (lastmx);
                    577:     }
                    578: 
                    579:     switch (m68k_dreg (regs, 0)) {
                    580:     case 0:
1.1.1.4 ! root      581:        return ievent_alive ? -1 : (1 || !uae_boot_rom) && needmousehack ();
1.1       root      582:     case 1:
                    583:        ievent_alive = 10;
1.1.1.2   root      584:        if (!mousehack_allowed ())
                    585:            return 0;
1.1       root      586:        return mousexpos;
                    587:     case 2:
1.1.1.2   root      588:        if (!mousehack_allowed ())
                    589:            return 0;
1.1       root      590:        return mouseypos;
                    591:     }
                    592:     return 0;
                    593: }
                    594: 
                    595: STATIC_INLINE int adjust (int val)
                    596: {
                    597:     if (val > 127)
                    598:        return 127;
                    599:     else if (val < -127)
                    600:        return -127;
                    601:     return val;
                    602: }
                    603: 
1.1.1.2   root      604: int getbuttonstate (int joy, int button)
                    605: {
                    606:     return joybutton[joy] & (1 << button);
                    607: }
                    608: 
                    609: static void mouseupdate (int pct)
1.1       root      610: {
1.1.1.2   root      611:     int v, i;
                    612: 
                    613:     for (i = 0; i < 2; i++) {
                    614: 
                    615:        v = mouse_delta[i][0] * pct / 100;
                    616:        mouse_x[i] += v;
                    617:        if (!mouse_deltanoreset[i][0])
                    618:            mouse_delta[i][0] -= v;
                    619: 
                    620:        v = mouse_delta[i][1] * pct / 100;
                    621:        mouse_y[i] += v;
                    622:        if (!mouse_deltanoreset[i][1])
                    623:            mouse_delta[i][1] -= v;
                    624: 
                    625:        v = mouse_delta[i][2] * pct / 100;
                    626:        if (v > 0)
                    627:            record_key (0x7a << 1);
                    628:        else if (v < 0)
                    629:            record_key (0x7b << 1);
                    630:        if (!mouse_deltanoreset[i][2])
                    631:            mouse_delta[i][2] = 0;
                    632: 
                    633:        if (mouse_frame_x[i] - mouse_x[i] > 127)
                    634:            mouse_x[i] = mouse_frame_x[i] - 127;
                    635:        if (mouse_frame_x[i] - mouse_x[i] < -127)
                    636:            mouse_x[i] = mouse_frame_x[i] + 127;
                    637: 
                    638:        if (mouse_frame_y[i] - mouse_y[i] > 127)
                    639:            mouse_y[i] = mouse_frame_y[i] - 127;
                    640:        if (mouse_frame_y[i] - mouse_y[i] < -127)
                    641:            mouse_y[i] = mouse_frame_y[i] + 127;
                    642: 
                    643:        if (pct == 100) {
                    644:            if (!mouse_deltanoreset[i][0])
                    645:                mouse_delta[i][0] = 0;
                    646:            if (!mouse_deltanoreset[i][1])
                    647:                mouse_delta[i][1] = 0;
                    648:            if (!mouse_deltanoreset[i][2])
                    649:                mouse_delta[i][2] = 0;
                    650:            mouse_frame_x[i] = mouse_x[i];
                    651:            mouse_frame_y[i] = mouse_y[i];
1.1       root      652:        }
1.1.1.2   root      653: 
                    654:     }
                    655: }
                    656: 
                    657: static int input_read, input_vpos;
                    658: 
                    659: static void readinput (void)
                    660: {
                    661:     if (!input_read && (vpos & ~31) != (input_vpos & ~31)) {
                    662:        idev[IDTYPE_JOYSTICK].read ();
                    663:        idev[IDTYPE_MOUSE].read ();
                    664:        mouseupdate ((vpos - input_vpos) * 100 / maxvpos);
                    665:        input_vpos = vpos;
                    666:     }
                    667:     if (input_read) {
                    668:        input_vpos = vpos;
                    669:        input_read = 0;
                    670:     }
                    671: }
                    672: 
                    673: int getjoystate (int joy)
                    674: {
                    675:     int left = 0, right = 0, top = 0, bot = 0;
                    676:     uae_u16 v = 0;
                    677: 
                    678:     readinput ();
                    679:     if (joydir[joy] & DIR_LEFT)
                    680:        left = 1;
                    681:     if (joydir[joy] & DIR_RIGHT)
                    682:        right = 1;
                    683:     if (joydir[joy] & DIR_UP)
                    684:        top = 1;
                    685:     if (joydir[joy] & DIR_DOWN)
                    686:        bot = 1;
                    687:     v = (uae_u8)mouse_x[joy] | (mouse_y[joy] << 8);
                    688:     if (left || right || top || bot || !mouse_port[joy]) {
                    689:        if (left)
                    690:            top = !top;
                    691:        if (right)
                    692:            bot = !bot;
                    693:        v &= ~0x0303;
                    694:        v |= bot | (right << 1) | (top << 8) | (left << 9);
                    695:     }
                    696: #ifdef DONGLE_DEBUG
                    697:     if (notinrom ())
                    698:        write_log ("JOY%dDAT %04.4X %s\n", joy, v, debuginfo (0));
                    699: #endif
                    700:     return v;
                    701: }
                    702: 
                    703: uae_u16 JOY0DAT (void)
                    704: {
                    705:     return getjoystate (0);
                    706: }
                    707: uae_u16 JOY1DAT (void)
                    708: {
                    709:     return getjoystate (1);
                    710: }
                    711: 
                    712: void JOYTEST (uae_u16 v)
                    713: {
                    714:     mouse_x[0] &= 3;
                    715:     mouse_y[0] &= 3;
                    716:     mouse_x[1] &= 3;
                    717:     mouse_y[1] &= 3;
                    718:     mouse_x[0] |= v & 0xFC;
                    719:     mouse_x[1] |= v & 0xFC;
                    720:     mouse_y[0] |= (v >> 8) & 0xFC;
                    721:     mouse_y[1] |= (v >> 8) & 0xFC;
                    722:     mouse_frame_x[0] = mouse_x[0];
                    723:     mouse_frame_y[0] = mouse_y[0];
                    724:     mouse_frame_x[1] = mouse_x[1];
                    725:     mouse_frame_y[1] = mouse_y[1];
1.1.1.4 ! root      726: //    write_log ("%d:%04.4X %p\n",vpos,v,m68k_getpc ());
1.1.1.2   root      727: }
                    728: 
                    729: static uae_u8 parconvert (uae_u8 v, int jd, int shift)
                    730: {
                    731:     if (jd & DIR_UP)
                    732:        v &= ~(1 << shift);
                    733:     if (jd & DIR_DOWN)
                    734:        v &= ~(2 << shift);
                    735:     if (jd & DIR_LEFT)
                    736:        v &= ~(4 << shift);
                    737:     if (jd & DIR_RIGHT)
                    738:        v &= ~(8 << shift);
                    739:     return v;
                    740: }
                    741: 
                    742: 
                    743: 
                    744: /* io-pins floating: dir=1 -> return data, dir=0 -> always return 1 */
                    745: uae_u8 handle_parport_joystick (int port, uae_u8 pra, uae_u8 dra)
                    746: {
                    747:     uae_u8 v;
                    748:     switch (port)
                    749:     {
                    750:        case 0:
                    751:        v = (pra & dra) | (dra ^ 0xff);
                    752:        if (parport_joystick_enabled) {
                    753:            v = parconvert (v, joydir[2], 0);
                    754:            v = parconvert (v, joydir[3], 4);
                    755:        }
                    756:        return v;
                    757:        case 1:
                    758:        v = ((pra & dra) | (dra ^ 0xff)) & 0x7;
                    759:        if (parport_joystick_enabled) {
                    760:            if (getbuttonstate (2, 0)) v &= ~1;
                    761:            if (getbuttonstate (3, 0)) v &= ~4;
                    762:        }
                    763:        return v;
                    764:        default:
1.1       root      765:        abort ();
1.1.1.2   root      766:        return 0;
1.1       root      767:     }
                    768: }
                    769: 
1.1.1.2   root      770: uae_u8 handle_joystick_buttons (uae_u8 dra)
1.1       root      771: {
1.1.1.2   root      772:     uae_u8 but = 0;
                    773:     int i;
                    774: 
                    775:     for (i = 0; i < 2; i++) {
                    776:        if (cd32_pad_enabled[i]) {
                    777:            uae_u16 p5dir = 0x0200 << (i * 4);
                    778:            uae_u16 p5dat = 0x0100 << (i * 4);
                    779:            but |= 0x40 << i;
                    780:            /* Red button is connected to fire when p5 is 1 or floating */
                    781:            if (!(potgo_value & p5dir) || ((potgo_value & p5dat) && (potgo_value & p5dir))) {
                    782:                if (getbuttonstate (i, JOYBUTTON_CD32_RED))
                    783:                    but &= ~(0x40 << i);
                    784:            }
                    785:        } else if (!getbuttonstate (i, JOYBUTTON_1)) {
                    786:            but |= 0x40 << i;
                    787:        }
1.1       root      788:     }
1.1.1.2   root      789:     return but;
1.1       root      790: }
                    791: 
1.1.1.2   root      792: /* joystick 1 button 1 is used as a output for incrementing shift register */
                    793: void handle_cd32_joystick_cia (uae_u8 pra, uae_u8 dra)
                    794: {
                    795:     static int oldstate[2];
                    796:     int i;
1.1       root      797: 
1.1.1.2   root      798:     for (i = 0; i < 2; i++) {
                    799:        uae_u8 but = 0x40 << i;
                    800:        uae_u16 p5dir = 0x0200 << (i * 4); /* output enable P5 */
                    801:        uae_u16 p5dat = 0x0100 << (i * 4); /* data P5 */
                    802:        if (!(potgo_value & p5dir) || !(potgo_value & p5dat)) {
                    803:            if ((dra & but) && (pra & but) != oldstate[i]) {
                    804:                if (!(pra & but)) {
                    805:                    cd32_shifter[i]--;
                    806:                    if (cd32_shifter[i] < 0)
                    807:                        cd32_shifter[i] = 0;
                    808:                }
                    809:            }
                    810:        }
                    811:        oldstate[i] = pra & but;
                    812:     }
                    813: }
                    814: 
                    815: /* joystick port 1 button 2 is input for button state */
                    816: static uae_u16 handle_joystick_potgor (uae_u16 potgor)
1.1       root      817: {
1.1.1.2   root      818:     int i;
                    819: 
                    820:     for (i = 0; i < 2; i++) {
                    821:        uae_u16 p9dir = 0x0800 << (i * 4); /* output enable P9 */
                    822:        uae_u16 p9dat = 0x0400 << (i * 4); /* data P9 */
                    823:        uae_u16 p5dir = 0x0200 << (i * 4); /* output enable P5 */
                    824:        uae_u16 p5dat = 0x0100 << (i * 4); /* data P5 */
                    825: 
                    826:        if (mouse_port[i]) {
                    827:            /* mouse has pull-up resistors in button lines */
                    828:            if (!(potgo_value & p5dir))
                    829:                potgor |= p5dat;
                    830:            if (!(potgo_value & p9dir))
                    831:                potgor |= p9dat;
                    832:        }
                    833:        if (potgo_hsync < 0) {
                    834:            /* first 10 or so lines after potgo has started
                    835:             * forces input-lines to zero
                    836:             */
                    837:            if (!(potgo_value & p5dir))
                    838:                potgor &= ~p5dat;
                    839:            if (!(potgo_value & p9dir))
                    840:                potgor &= ~p9dat;
                    841:        }
                    842: 
                    843:        if (cd32_pad_enabled[i]) {
                    844:            /* p5 is floating in input-mode */
                    845:            potgor &= ~p5dat;
                    846:            potgor |= potgo_value & p5dat;
                    847:            if (!(potgo_value & p9dir))
                    848:                potgor |= p9dat;
                    849:            /* P5 output and 1 -> shift register is kept reset (Blue button) */
                    850:            if ((potgo_value & p5dir) && (potgo_value & p5dat))
                    851:                cd32_shifter[i] = 8;
                    852:            /* shift at 1 == return one, >1 = return button states */
                    853:            if (cd32_shifter[i] == 0)
                    854:                potgor &= ~p9dat; /* shift at zero == return zero */
                    855:            if (cd32_shifter[i] >= 2 && (joybutton[i] & ((1 << JOYBUTTON_CD32_PLAY) << (cd32_shifter[i] - 2))))
                    856:                potgor &= ~p9dat;
1.1.1.4 ! root      857:            //write_log ("%d:%04.4X %08.8X\n", cd32_shifter[i], potgor, m68k_getpc ());
1.1.1.2   root      858:        } else {
                    859:            if (getbuttonstate (i, JOYBUTTON_3))
                    860:                potgor &= ~p5dat;
                    861:            if (getbuttonstate (i, JOYBUTTON_2))
                    862:                potgor &= ~p9dat;
                    863:        }
                    864:     }
                    865:     return potgor;
1.1       root      866: }
                    867: 
1.1.1.2   root      868: uae_u16 potgo_value;
                    869: static uae_u16 potdats[2];
                    870: static int inputdelay;
                    871: 
                    872: void inputdevice_hsync (void)
1.1       root      873: {
1.1.1.2   root      874:     int joy;
1.1       root      875: 
1.1.1.2   root      876:     for (joy = 0; joy < 2; joy++) {
                    877:        if (potgo_hsync >= 0) {
                    878:            int active;
                    879: 
                    880:            active = 0;
                    881:            if ((potgo_value >> 9) & 1) /* output? */
                    882:                active = ((potgo_value >> 8) & 1) ? 0 : 1;
                    883:            if (potgo_hsync < joydirpot[joy][0])
                    884:                active = 1;
                    885:            if (getbuttonstate (joy, JOYBUTTON_3))
                    886:                active = 1;
                    887:            if (active)
                    888:                potdats[joy] = ((potdats[joy] + 1) & 0xFF) | (potdats[joy] & 0xFF00);
                    889: 
                    890:            active = 0;
                    891:            if ((potgo_value >> 11) & 1) /* output? */
                    892:                active = ((potgo_value >> 10) & 1) ? 0 : 1;
                    893:            if (potgo_hsync < joydirpot[joy][1])
                    894:                active = 1;
                    895:            if (getbuttonstate (joy, JOYBUTTON_2))
                    896:                active = 1;
                    897:            if (active)
                    898:                potdats[joy] += 0x100;
                    899:        }
                    900:     }
                    901:     potgo_hsync++;
                    902:     if (potgo_hsync > 255)
                    903:        potgo_hsync = 255;
1.1       root      904: 
                    905: 
1.1.1.2   root      906: #ifdef CATWEASEL
                    907:     catweasel_hsync ();
                    908: #endif
                    909:     if (inputdelay > 0) {
                    910:        inputdelay--;
                    911:        if (inputdelay == 0) {
                    912:            idev[IDTYPE_JOYSTICK].read ();
                    913:            idev[IDTYPE_KEYBOARD].read ();
                    914:        }
1.1       root      915:     }
1.1.1.2   root      916: }
                    917: 
                    918: uae_u16 POT0DAT (void)
                    919: {
                    920:     return potdats[0];
                    921: }
                    922: uae_u16 POT1DAT (void)
                    923: {
                    924:     return potdats[1];
                    925: }
                    926: 
                    927: /* direction=input, data pin floating, last connected logic level or previous status
                    928:                    written when direction was ouput
                    929:  *                  otherwise it is currently connected logic level.
                    930:  * direction=output, data pin is current value, forced to zero if joystick button is pressed
                    931:  * it takes some tens of microseconds before data pin changes state
                    932:  */
                    933: 
                    934: void POTGO (uae_u16 v)
                    935: {
                    936:     int i;
1.1       root      937: 
1.1.1.4 ! root      938:     //write_log ("W:%d: %04.4X %p\n", vpos, v, m68k_getpc ());
1.1.1.2   root      939: #ifdef DONGLE_DEBUG
                    940:     if (notinrom ())
                    941:        write_log ("POTGO %04.4X %s\n", v, debuginfo(0));
                    942: #endif
                    943:     potgo_value = potgo_value & 0x5500; /* keep state of data bits */
                    944:     potgo_value |= v & 0xaa00; /* get new direction bits */
                    945:     for (i = 0; i < 8; i += 2) {
                    946:        uae_u16 dir = 0x0200 << i;
                    947:        if (v & dir) {
                    948:            uae_u16 data = 0x0100 << i;
                    949:            potgo_value &= ~data;
                    950:            potgo_value |= v & data;
                    951:        }
                    952:     }
                    953:     for (i = 0; i < 2; i++) {
                    954:        if (cd32_pad_enabled[i]) {
                    955:            uae_u16 p5dir = 0x0200 << (i * 4); /* output enable P5 */
                    956:            uae_u16 p5dat = 0x0100 << (i * 4); /* data P5 */
                    957:            if ((potgo_value & p5dir) && (potgo_value & p5dat))
                    958:                cd32_shifter[i] = 8;
                    959:        }
                    960:     }
                    961:     if (v & 1) {
                    962:        potdats[0] = potdats[1] = 0;
                    963:        potgo_hsync = -15;
1.1       root      964:     }
1.1.1.2   root      965: }
1.1       root      966: 
1.1.1.2   root      967: uae_u16 POTGOR (void)
                    968: {
                    969:     uae_u16 v = handle_joystick_potgor (potgo_value) & 0x5500;
1.1.1.4 ! root      970:     //write_log ("R:%d:%04.4X %d %p\n", vpos, v, cd32_shifter[1], m68k_getpc ());
1.1       root      971:     return v;
                    972: }
                    973: 
1.1.1.2   root      974: static int check_input_queue (int event)
1.1       root      975: {
1.1.1.2   root      976:     struct input_queue_struct *iq;
                    977:     int i;
                    978:     for (i = 0; i < INPUT_QUEUE_SIZE; i++) {
                    979:        iq = &input_queue[i];
                    980:        if (iq->event == event) return i;
1.1       root      981:     }
1.1.1.2   root      982:     return -1;
                    983: }
                    984: 
                    985: static void queue_input_event (int event, int state, int max, int framecnt, int autofire)
                    986: {
                    987:     struct input_queue_struct *iq;
                    988:     int i = check_input_queue (event);
1.1       root      989: 
1.1.1.2   root      990:     if (event <= 0)
                    991:        return;
                    992:     if (state < 0 && i >= 0) {
                    993:        iq = &input_queue[i];
                    994:        iq->nextframecnt = -1;
                    995:        iq->framecnt = -1;
                    996:        iq->event = 0;
                    997:        if (iq->state == 0)
                    998:            handle_input_event (event, 0, 1, 0);
                    999:     } else if (i < 0) {
                   1000:        for (i = 0; i < INPUT_QUEUE_SIZE; i++) {
                   1001:            iq = &input_queue[i];
                   1002:            if (iq->framecnt < 0) break;
                   1003:        }
                   1004:        if (i == INPUT_QUEUE_SIZE) {
                   1005:            write_log ("input queue overflow\n");
                   1006:            return;
                   1007:        }
                   1008:        iq->event = event;
                   1009:        iq->state = iq->storedstate = state;
                   1010:        iq->max = max;
                   1011:        iq->framecnt = framecnt;
                   1012:        iq->nextframecnt = autofire > 0 ? framecnt : -1;
                   1013:     }
1.1       root     1014: }
1.1.1.2   root     1015: 
                   1016: static uae_u8 keybuf[256];
                   1017: static int inputcode_pending, inputcode_pending_state;
                   1018: 
                   1019: /* Generate key up events for any keys that are 'stuck' down */
                   1020: void inputdevice_release_all_keys (void)
1.1       root     1021: {
1.1.1.2   root     1022:     int i;
                   1023: 
                   1024:     for (i = 0; i < 0x80; i++) {
                   1025:        if (keybuf[i] != 0) {
                   1026:            keybuf[i] = 0;
                   1027:            record_key (i << 1|1);
                   1028:        }
1.1       root     1029:     }
                   1030: }
1.1.1.2   root     1031: 
                   1032: void inputdevice_add_inputcode (int code, int state)
1.1       root     1033: {
1.1.1.2   root     1034:     inputcode_pending = code;
                   1035:     inputcode_pending_state = state;
                   1036: }
                   1037: 
                   1038: void inputdevice_do_keyboard (int code, int state)
                   1039: {
                   1040:     if (code < 0x80) {
                   1041:        uae_u8 key = code | (state ? 0x00 : 0x80);
                   1042:        keybuf[key & 0x7f] = (key & 0x80) ? 0 : 1;
                   1043:        if (((keybuf[AK_CTRL] || keybuf[AK_RCTRL]) && keybuf[AK_LAMI] && keybuf[AK_RAMI]) || key == AK_RESETWARNING) {
                   1044:            int r = keybuf[AK_LALT] | keybuf[AK_RALT];
                   1045:            memset (keybuf, 0, sizeof (keybuf));
                   1046:            uae_reset (r);
                   1047:        }
                   1048:        record_key ((uae_u8)((key << 1) | (key >> 7)));
1.1.1.4 ! root     1049:        //write_log ("Amiga key %02.2X %d\n", key & 0x7f, key >> 7);
1.1.1.2   root     1050:        return;
1.1       root     1051:     }
1.1.1.2   root     1052:     inputdevice_add_inputcode (code, state);
1.1       root     1053: }
1.1.1.2   root     1054: 
                   1055: void inputdevice_handle_inputcode (void)
1.1       root     1056: {
1.1.1.2   root     1057:     int code = inputcode_pending;
                   1058:     int state = inputcode_pending_state;
                   1059:     inputcode_pending = 0;
                   1060:     if (code == 0)
                   1061:        return;
                   1062:     if (vpos != 0)
                   1063:        write_log ("inputcode=%d but vpos = %d", code, vpos);
                   1064: 
                   1065: #ifdef ARCADIA
                   1066:     switch (code)
                   1067:     {
                   1068:     case AKS_ARCADIADIAGNOSTICS:
                   1069:        arcadia_flag &= ~1;
                   1070:        arcadia_flag |= state ? 1 : 0;
                   1071:        break;
                   1072:     case AKS_ARCADIAPLY1:
                   1073:        arcadia_flag &= ~4;
                   1074:        arcadia_flag |= state ? 4 : 0;
                   1075:        break;
                   1076:     case AKS_ARCADIAPLY2:
                   1077:        arcadia_flag &= ~2;
                   1078:        arcadia_flag |= state ? 2 : 0;
                   1079:        break;
                   1080:     case AKS_ARCADIACOIN1:
                   1081:        if (state)
                   1082:            arcadia_coin[0]++;
                   1083:        break;
                   1084:     case AKS_ARCADIACOIN2:
                   1085:        if (state)
                   1086:            arcadia_coin[1]++;
                   1087:        break;
                   1088:     }
                   1089: #endif
                   1090: 
                   1091:     if (!state)
                   1092:        return;
                   1093:     switch (code)
                   1094:     {
                   1095:     case AKS_ENTERGUI:
                   1096:        gui_display (-1);
                   1097:        break;
                   1098: #if 0
                   1099:     case AKS_SCREENSHOT:
                   1100:        screenshot(1, 1);
                   1101:        break;
                   1102: #endif
                   1103: #ifdef ACTION_REPLAY
                   1104:     case AKS_FREEZEBUTTON:
                   1105:        action_replay_freeze ();
                   1106:        break;
                   1107: #endif
                   1108:     case AKS_FLOPPY0:
                   1109:        gui_display (0);
                   1110:        break;
                   1111:     case AKS_FLOPPY1:
                   1112:        gui_display (1);
                   1113:        break;
                   1114:     case AKS_FLOPPY2:
                   1115:        gui_display (2);
                   1116:        break;
                   1117:     case AKS_FLOPPY3:
                   1118:        gui_display (3);
                   1119:        break;
                   1120:     case AKS_EFLOPPY0:
                   1121:        disk_eject (0);
                   1122:        break;
                   1123:     case AKS_EFLOPPY1:
                   1124:        disk_eject (1);
                   1125:        break;
                   1126:     case AKS_EFLOPPY2:
                   1127:        disk_eject (2);
                   1128:        break;
                   1129:     case AKS_EFLOPPY3:
                   1130:        disk_eject (3);
                   1131:        break;
                   1132: #if 0
                   1133:     case AKS_IRQ7:
                   1134:        Interrupt (7);
                   1135:        break;
                   1136: #endif
                   1137:     case AKS_PAUSE:
                   1138:        pausemode (-1);
                   1139:        break;
                   1140: #if 0
                   1141:     case AKS_WARP:
                   1142:        warpmode (-1);
                   1143:        break;
                   1144: #endif
                   1145:     case AKS_INHIBITSCREEN:
                   1146:        toggle_inhibit_frame (IHF_SCROLLLOCK);
                   1147:        break;
                   1148: #if 0
                   1149:     case AKS_STATEREWIND:
                   1150:        savestate_dorewind(1);
                   1151:        break;
                   1152:     case AKS_VOLDOWN:
                   1153:        sound_volume (-1);
                   1154:        break;
                   1155:     case AKS_VOLUP:
                   1156:        sound_volume (1);
                   1157:        break;
                   1158:     case AKS_VOLMUTE:
                   1159:        sound_volume (0);
                   1160:        break;
                   1161: #endif
                   1162:     case AKS_QUIT:
                   1163:        uae_quit ();
                   1164:        break;
                   1165:     case AKS_SOFTRESET:
                   1166:        uae_reset (0);
                   1167:        break;
                   1168:     case AKS_HARDRESET:
                   1169:        uae_reset (1);
                   1170:        break;
                   1171: #if 0
                   1172:     case AKS_STATESAVEQUICK:
                   1173:     case AKS_STATESAVEQUICK1:
                   1174:     case AKS_STATESAVEQUICK2:
                   1175:     case AKS_STATESAVEQUICK3:
                   1176:     case AKS_STATESAVEQUICK4:
                   1177:     case AKS_STATESAVEQUICK5:
                   1178:     case AKS_STATESAVEQUICK6:
                   1179:     case AKS_STATESAVEQUICK7:
                   1180:     case AKS_STATESAVEQUICK8:
                   1181:     case AKS_STATESAVEQUICK9:
                   1182:        savestate_quick ((code - AKS_STATESAVEQUICK) / 2, 1);
                   1183:        break;
                   1184:     case AKS_STATERESTOREQUICK:
                   1185:     case AKS_STATERESTOREQUICK1:
                   1186:     case AKS_STATERESTOREQUICK2:
                   1187:     case AKS_STATERESTOREQUICK3:
                   1188:     case AKS_STATERESTOREQUICK4:
                   1189:     case AKS_STATERESTOREQUICK5:
                   1190:     case AKS_STATERESTOREQUICK6:
                   1191:     case AKS_STATERESTOREQUICK7:
                   1192:     case AKS_STATERESTOREQUICK8:
                   1193:     case AKS_STATERESTOREQUICK9:
                   1194:        savestate_quick ((code - AKS_STATERESTOREQUICK) / 2, 0);
                   1195:        break;
                   1196: #endif
                   1197:     case AKS_TOGGLEFULLSCREEN:
                   1198:        toggle_fullscreen ();
                   1199:        break;
                   1200:     case AKS_TOGGLEMOUSEGRAB:
                   1201:        toggle_mousegrab ();
                   1202:        break;
                   1203:     case AKS_ENTERDEBUGGER:
                   1204:        activate_debugger ();
                   1205:        break;
                   1206:     case AKS_STATESAVEDIALOG:
                   1207:        gui_display (5);
                   1208:        break;
                   1209:     case AKS_STATERESTOREDIALOG:
                   1210:        gui_display (4);
                   1211:        break;
                   1212: #if 0
                   1213:     case AKS_DECREASEREFRESHRATE:
                   1214:     case AKS_INCREASEREFRESHRATE:
                   1215:     {
                   1216:        int dir = code == AKS_INCREASEREFRESHRATE ? 5 : -5;
                   1217:        if (currprefs.chipset_refreshrate == 0)
                   1218:            currprefs.chipset_refreshrate = currprefs.ntscmode ? 60 : 50;
                   1219:        changed_prefs.chipset_refreshrate = currprefs.chipset_refreshrate + dir;
                   1220:        if (changed_prefs.chipset_refreshrate < 10)
                   1221:            changed_prefs.chipset_refreshrate = 10;
                   1222:        if (changed_prefs.chipset_refreshrate > 900)
                   1223:            changed_prefs.chipset_refreshrate = 900;
                   1224:     }
                   1225:     break;
                   1226: #endif
1.1.1.3   root     1227:     case AKS_SWITCHINTERPOL:
                   1228:        switch_audio_interpol ();
                   1229:        break;
1.1       root     1230:     }
                   1231: }
1.1.1.2   root     1232: 
                   1233: void handle_input_event (int nr, int state, int max, int autofire)
                   1234: {
                   1235:     struct inputevent *ie;
                   1236:     int joy;
                   1237: 
                   1238:     if (nr <= 0)
                   1239:        return;
                   1240:     ie = &events[nr];
1.1.1.4 ! root     1241:     //write_log ("'%s' %d %d\n", ie->name, state, max);
1.1.1.2   root     1242:     if (autofire) {
                   1243:        if (state)
                   1244:            queue_input_event (nr, state, max, currprefs.input_autofire_framecnt, 1);
                   1245:        else
                   1246:            queue_input_event (nr, -1, 0, 0, 1);
                   1247:     }
                   1248:     switch (ie->unit)
                   1249:     {
                   1250:        case 1: /* ->JOY1 */
                   1251:        case 2: /* ->JOY2 */
                   1252:        case 3: /* ->Parallel port joystick adapter port #1 */
                   1253:        case 4: /* ->Parallel port joystick adapter port #2 */
                   1254:            joy = ie->unit - 1;
                   1255:            if (ie->type & 4) {
                   1256:                if (state)
                   1257:                    joybutton[joy] |= 1 << ie->data;
                   1258:                else
                   1259:                    joybutton[joy] &= ~(1 << ie->data);
                   1260:            } else if (ie->type & 8) {
                   1261:                /* real mouse / analog stick mouse emulation */
                   1262:                int delta;
                   1263:                int deadzone = currprefs.input_joymouse_deadzone * max / 100;
                   1264:                if (max) {
                   1265:                    if (state < deadzone && state > -deadzone) {
                   1266:                        state = 0;
                   1267:                    } else if (state < 0) {
                   1268:                        state += deadzone;
                   1269:                    } else {
                   1270:                        state -= deadzone;
                   1271:                    }
                   1272:                    max -= deadzone;
                   1273:                    delta = state * currprefs.input_joymouse_multiplier / max;
                   1274:                } else {
                   1275:                    delta = state;
                   1276:                }
                   1277:                mouse_delta[joy][ie->data] += delta;
                   1278:            } else if (ie->type & 32) {
                   1279:                int speed = currprefs.input_joymouse_speed;
                   1280: 
                   1281:                /* button mouse emulation */
                   1282:                if (state && (ie->data & DIR_LEFT)) {
                   1283:                    mouse_delta[joy][0] = -speed;
                   1284:                    mouse_deltanoreset[joy][0] = 1;
                   1285:                } else if (state && (ie->data & DIR_RIGHT)) {
                   1286:                    mouse_delta[joy][0] = speed;
                   1287:                    mouse_deltanoreset[joy][0] = 1;
                   1288:                } else
                   1289:                    mouse_deltanoreset[joy][0] = 0;
                   1290: 
                   1291:                if (state && (ie->data & DIR_UP)) {
                   1292:                    mouse_delta[joy][1] = -speed;
                   1293:                    mouse_deltanoreset[joy][1] = 1;
                   1294:                } else if (state && (ie->data & DIR_DOWN)) {
                   1295:                    mouse_delta[joy][1] = speed;
                   1296:                    mouse_deltanoreset[joy][1] = 1;
                   1297:                } else
                   1298:                    mouse_deltanoreset[joy][1] = 0;
                   1299: 
                   1300:            } else if (ie->type & 64) { /* analog (paddle) */
                   1301:                int deadzone = currprefs.input_joymouse_deadzone * max / 100;
                   1302:                if (max) {
                   1303:                    if (state < deadzone && state > -deadzone) {
                   1304:                        state = 0;
                   1305:                    } else if (state < 0) {
                   1306:                        state += deadzone;
                   1307:                    } else {
                   1308:                        state -= deadzone;
                   1309:                    }
                   1310:                    state = state * max / (max - deadzone);
                   1311:                }
                   1312:                state = state / 256 + 128;
                   1313:                joydirpot[joy][ie->data] = state;
                   1314:            } else {
                   1315:                int left = oleft[joy], right = oright[joy], top = otop[joy], bot = obot[joy];
                   1316:                if (ie->type & 16) {
                   1317:                    /* button to axis mapping */
                   1318:                    if (ie->data & DIR_LEFT) left = oleft[joy] = state ? 1 : 0;
                   1319:                    if (ie->data & DIR_RIGHT) right = oright[joy] = state ? 1 : 0;
                   1320:                    if (ie->data & DIR_UP) top = otop[joy] = state ? 1 : 0;
                   1321:                    if (ie->data & DIR_DOWN) bot = obot[joy] = state ? 1 : 0;
                   1322:                } else {
                   1323:                    /* "normal" joystick axis */
                   1324:                    int deadzone = currprefs.input_joystick_deadzone * max / 100;
                   1325:                    int neg, pos;
                   1326:                    if (state < deadzone && state > -deadzone)
                   1327:                        state = 0;
                   1328:                    neg = state < 0 ? 1 : 0;
                   1329:                    pos = state > 0 ? 1 : 0;
                   1330:                    if (ie->data & DIR_LEFT) left = oleft[joy] = neg;
                   1331:                    if (ie->data & DIR_RIGHT) right = oright[joy] = pos;
                   1332:                    if (ie->data & DIR_UP) top = otop[joy] = neg;
                   1333:                    if (ie->data & DIR_DOWN) bot = obot[joy] = pos;
                   1334:                }
                   1335:                joydir[joy] = 0;
                   1336:                if (left) joydir[joy] |= DIR_LEFT;
                   1337:                if (right) joydir[joy] |= DIR_RIGHT;
                   1338:                if (top) joydir[joy] |= DIR_UP;
                   1339:                if (bot) joydir[joy] |= DIR_DOWN;
                   1340:            }
                   1341:        break;
                   1342:        case 0: /* ->KEY */
                   1343:            inputdevice_do_keyboard (ie->data, state);
                   1344:        break;
                   1345:     }
                   1346: }
                   1347: 
                   1348: void inputdevice_vsync (void)
                   1349: {
                   1350:     struct input_queue_struct *iq;
                   1351:     int i;
                   1352: 
                   1353:     for (i = 0; i < INPUT_QUEUE_SIZE; i++) {
                   1354:        iq = &input_queue[i];
                   1355:        if (iq->framecnt > 0) {
                   1356:            iq->framecnt--;
                   1357:            if (iq->framecnt == 0) {
                   1358:                if (iq->state) iq->state = 0; else iq->state = iq->storedstate;
                   1359:                handle_input_event (iq->event, iq->state, iq->max, 0);
                   1360:                iq->framecnt = iq->nextframecnt;
                   1361:            }
                   1362:        }
                   1363:     }
                   1364:     mouseupdate (100);
                   1365:     inputdelay = rand () % (maxvpos - 1);
                   1366:     idev[IDTYPE_MOUSE].read ();
                   1367:     input_read = 1;
                   1368:     input_vpos = 0;
                   1369:     inputdevice_handle_inputcode ();
                   1370:     if (ievent_alive > 0)
                   1371:        ievent_alive--;
                   1372: #ifdef ARCADIA
                   1373:     if (arcadia_rom)
                   1374:        arcadia_vsync ();
                   1375: #endif
                   1376: }
                   1377: 
                   1378: void inputdevice_reset (void)
                   1379: {
                   1380:     ievent_alive = 0;
                   1381: }
                   1382: 
                   1383: static void setbuttonstateall (struct uae_input_device *id, struct uae_input_device2 *id2, int button, int state)
                   1384: {
                   1385:     int event, autofire, i;
                   1386:     uae_u32 mask = 1 << button;
                   1387:     uae_u32 omask = id2->buttonmask & mask;
                   1388:     uae_u32 nmask = (state ? 1 : 0) << button;
                   1389:     char *custom;
                   1390: 
                   1391:     if (button >= ID_BUTTON_TOTAL)
                   1392:        return;
                   1393:     for (i = 0; i < MAX_INPUT_SUB_EVENT; i++) {
1.1.1.3   root     1394:        struct inputevent *ie;
                   1395: 
1.1.1.2   root     1396:        event = id->eventid[ID_BUTTON_OFFSET + button][sublevdir[state <= 0 ? 1 : 0][i]];
                   1397:        custom = id->custom[ID_BUTTON_OFFSET + button][sublevdir[state <= 0 ? 1 : 0][i]];
                   1398:        if (event <= 0 && custom == NULL)
                   1399:            continue;
                   1400:        autofire = (id->flags[ID_BUTTON_OFFSET + button][sublevdir[state <= 0 ? 1 : 0][i]] & ID_FLAG_AUTOFIRE) ? 1 : 0;
1.1.1.3   root     1401:        ie = events + event;
                   1402:        if (ie->allow_mask & AM_AUTO)
                   1403:            autofire = 1;
1.1.1.2   root     1404:        if (state < 0) {
                   1405:            handle_input_event (event, 1, 1, 0);
                   1406:            queue_input_event (event, 0, 1, 1, 0); /* send release event next frame */
                   1407:        } else {
                   1408:            if ((omask ^ nmask) & mask)
                   1409:                handle_input_event (event, state, 1, autofire);
                   1410:        }
                   1411:     }
                   1412:     if ((omask ^ nmask) & mask) {
                   1413:        if (state)
                   1414:            id2->buttonmask |= mask;
                   1415:        else
                   1416:            id2->buttonmask &= ~mask;
                   1417:     }
                   1418: }
                   1419: 
                   1420: 
                   1421: /* - detect required number of joysticks and mice from configuration data
                   1422:  * - detect if CD32 pad emulation is needed
                   1423:  * - detect device type in ports (mouse or joystick)
                   1424:  */
                   1425: 
                   1426: static int iscd32 (int ei)
                   1427: {
                   1428:     if (ei >= INPUTEVENT_JOY1_CD32_FIRST && ei <= INPUTEVENT_JOY1_CD32_LAST) {
                   1429:        cd32_pad_enabled[0] = 1;
                   1430:        return 1;
                   1431:     }
                   1432:     if (ei >= INPUTEVENT_JOY2_CD32_FIRST && ei <= INPUTEVENT_JOY2_CD32_LAST) {
                   1433:        cd32_pad_enabled[1] = 1;
                   1434:        return 2;
                   1435:     }
                   1436:     return 0;
                   1437: }
                   1438: 
                   1439: static int isparport (int ei)
                   1440: {
                   1441:     if (ei > INPUTEVENT_PAR_JOY1_START && ei < INPUTEVENT_PAR_JOY_END) {
                   1442:        parport_joystick_enabled = 1;
                   1443:        return 1;
                   1444:     }
                   1445:     return 0;
                   1446: }
                   1447: 
                   1448: static int ismouse (int ei)
                   1449: {
                   1450:     if (ei >= INPUTEVENT_MOUSE1_FIRST && ei <= INPUTEVENT_MOUSE1_LAST) {
                   1451:        mouse_port[0] = 1;
                   1452:        return 1;
                   1453:     }
                   1454:     if (ei >= INPUTEVENT_MOUSE2_FIRST && ei <= INPUTEVENT_MOUSE2_LAST) {
                   1455:        mouse_port[1] = 1;
                   1456:        return 2;
                   1457:     }
                   1458:     return 0;
                   1459: }
                   1460: 
                   1461: #ifdef CD32
                   1462: extern int cd32_enabled;
                   1463: #endif
                   1464: 
                   1465: static void scanevents(struct uae_prefs *p)
                   1466: {
                   1467:     int i, j, k, ei;
                   1468:     struct inputevent *e;
                   1469:     int n_joy = idev[IDTYPE_JOYSTICK].get_num();
                   1470:     int n_mouse = idev[IDTYPE_MOUSE].get_num();
                   1471: 
                   1472:     cd32_pad_enabled[0] = cd32_pad_enabled[1] = 0;
                   1473:     parport_joystick_enabled = 0;
                   1474:     mouse_port[0] = mouse_port[1] = 0;
                   1475:     for (i = 0; i < MAX_INPUT_DEVICE_EVENTS; i++)
                   1476:        joydir[i] = 0;
                   1477: 
                   1478:     for (i = 0; i < MAX_INPUT_DEVICES; i++) {
                   1479:        use_joysticks[i] = 0;
                   1480:        use_mice[i] = 0;
                   1481:        for (k = 0; k < MAX_INPUT_SUB_EVENT; k++) {
                   1482:            for (j = 0; j < ID_BUTTON_TOTAL; j++) {
                   1483: 
                   1484:                if (joysticks[i].enabled && i < n_joy) {
                   1485:                    ei = joysticks[i].eventid[ID_BUTTON_OFFSET + j][k];
                   1486:                    e = &events[ei];
                   1487:                    iscd32 (ei);
                   1488:                    isparport (ei);
                   1489:                    ismouse (ei);
                   1490:                    if (joysticks[i].eventid[ID_BUTTON_OFFSET + j][k] > 0)
                   1491:                        use_joysticks[i] = 1;
                   1492:                }
                   1493:                if (mice[i].enabled && i < n_mouse) {
                   1494:                    ei = mice[i].eventid[ID_BUTTON_OFFSET + j][k];
                   1495:                    e = &events[ei];
                   1496:                    iscd32 (ei);
                   1497:                    isparport (ei);
                   1498:                    ismouse (ei);
                   1499:                    if (mice[i].eventid[ID_BUTTON_OFFSET + j][k] > 0)
                   1500:                        use_mice[i] = 1;
                   1501:                }
                   1502: 
                   1503:            }
                   1504: 
                   1505:            for (j = 0; j < ID_AXIS_TOTAL; j++) {
                   1506: 
                   1507:                if (joysticks[i].enabled && i < n_joy) {
                   1508:                    ei = joysticks[i].eventid[ID_AXIS_OFFSET + j][k];
                   1509:                    iscd32 (ei);
                   1510:                    isparport (ei);
                   1511:                    ismouse (ei);
                   1512:                    if (ei > 0)
                   1513:                        use_joysticks[i] = 1;
                   1514:                }
                   1515:                if (mice[i].enabled && i < n_mouse) {
                   1516:                    ei = mice[i].eventid[ID_AXIS_OFFSET + j][k];
                   1517:                    iscd32 (ei);
                   1518:                    isparport (ei);
                   1519:                    ismouse (ei);
                   1520:                    if (ei > 0)
                   1521:                        use_mice[i] = 1;
                   1522:                }
                   1523:            }
                   1524:        }
                   1525:     }
                   1526:     for (i = 0; i < MAX_INPUT_DEVICES; i++) {
                   1527:        use_keyboards[i] = 0;
                   1528:        if (keyboards[i].enabled && i < idev[IDTYPE_KEYBOARD].get_num()) {
                   1529:            j = 0;
                   1530:            while (keyboards[i].extra[j][0] >= 0) {
                   1531:                use_keyboards[i] = 1;
                   1532:                for (k = 0; k < MAX_INPUT_SUB_EVENT; k++) {
                   1533:                    ei = keyboards[i].eventid[j][k];
                   1534:                    iscd32 (ei);
                   1535:                    isparport (ei);
                   1536:                    ismouse (ei);
                   1537:                }
                   1538:                j++;
                   1539:            }
                   1540:        }
                   1541:     }
                   1542: }
                   1543: 
                   1544: #ifdef CD32
                   1545: static void setcd32 (int joy, int n)
                   1546: {
                   1547:     joysticks[joy].eventid[ID_BUTTON_OFFSET + 0][0] = n ? INPUTEVENT_JOY2_CD32_RED : INPUTEVENT_JOY1_CD32_RED;
                   1548:     joysticks[joy].eventid[ID_BUTTON_OFFSET + 1][0] = n ? INPUTEVENT_JOY2_CD32_BLUE : INPUTEVENT_JOY1_CD32_BLUE;
                   1549:     joysticks[joy].eventid[ID_BUTTON_OFFSET + 2][0] = n ? INPUTEVENT_JOY2_CD32_YELLOW : INPUTEVENT_JOY1_CD32_YELLOW;
                   1550:     joysticks[joy].eventid[ID_BUTTON_OFFSET + 3][0] = n ? INPUTEVENT_JOY2_CD32_GREEN : INPUTEVENT_JOY1_CD32_GREEN;
                   1551:     joysticks[joy].eventid[ID_BUTTON_OFFSET + 4][0] = n ? INPUTEVENT_JOY2_CD32_FFW : INPUTEVENT_JOY1_CD32_FFW;
                   1552:     joysticks[joy].eventid[ID_BUTTON_OFFSET + 5][0] = n ? INPUTEVENT_JOY2_CD32_RWD : INPUTEVENT_JOY1_CD32_RWD;
                   1553:     joysticks[joy].eventid[ID_BUTTON_OFFSET + 6][0] = n ? INPUTEVENT_JOY2_CD32_PLAY :  INPUTEVENT_JOY1_CD32_PLAY;
                   1554: }
                   1555: #endif
                   1556: 
                   1557: int compatibility_device[2];
                   1558: static void compatibility_mode (struct uae_prefs *prefs)
                   1559: {
                   1560:     int joy, i;
                   1561:     int used[4] = { 0, 0, 0, 0};
                   1562: 
                   1563:     compatibility_device[0] = -1;
                   1564:     compatibility_device[1] = -1;
                   1565:     for (i = 0; i < MAX_INPUT_DEVICES; i++) {
                   1566:        memset (&mice[i], 0, sizeof (*mice));
                   1567:        memset (&joysticks[i], 0, sizeof (*joysticks));
                   1568:     }
                   1569: 
                   1570:     if ((joy = jsem_ismouse (0, prefs)) >= 0) {
                   1571:        mice[joy].eventid[ID_AXIS_OFFSET + 0][0] = INPUTEVENT_MOUSE1_HORIZ;
                   1572:        mice[joy].eventid[ID_AXIS_OFFSET + 1][0] = INPUTEVENT_MOUSE1_VERT;
                   1573:        mice[joy].eventid[ID_AXIS_OFFSET + 2][0] = INPUTEVENT_MOUSE1_WHEEL;
                   1574:        mice[joy].eventid[ID_BUTTON_OFFSET + 0][0] = INPUTEVENT_JOY1_FIRE_BUTTON;
                   1575:        mice[joy].eventid[ID_BUTTON_OFFSET + 1][0] = INPUTEVENT_JOY1_2ND_BUTTON;
                   1576:        mice[joy].eventid[ID_BUTTON_OFFSET + 2][0] = INPUTEVENT_JOY1_3RD_BUTTON;
                   1577:        mice[joy].eventid[ID_BUTTON_OFFSET + 3][0] = INPUTEVENT_KEY_ALT_LEFT;
                   1578:        mice[joy].eventid[ID_BUTTON_OFFSET + 3][1] = INPUTEVENT_KEY_CURSOR_LEFT;
                   1579:        mice[joy].eventid[ID_BUTTON_OFFSET + 4][0] = INPUTEVENT_KEY_ALT_LEFT;
                   1580:        mice[joy].eventid[ID_BUTTON_OFFSET + 4][1] = INPUTEVENT_KEY_CURSOR_RIGHT;
                   1581:        mice[joy].enabled = 1;
                   1582:     }
                   1583:     if ((joy = jsem_ismouse (1, prefs)) >= 0) {
                   1584:        mice[joy].eventid[ID_AXIS_OFFSET + 0][0] = INPUTEVENT_MOUSE2_HORIZ;
                   1585:        mice[joy].eventid[ID_AXIS_OFFSET + 1][0] = INPUTEVENT_MOUSE2_VERT;
                   1586:        mice[joy].eventid[ID_BUTTON_OFFSET + 0][0] = INPUTEVENT_JOY2_FIRE_BUTTON;
                   1587:        mice[joy].eventid[ID_BUTTON_OFFSET + 1][0] = INPUTEVENT_JOY2_2ND_BUTTON;
                   1588:        mice[joy].eventid[ID_BUTTON_OFFSET + 2][0] = INPUTEVENT_JOY2_3RD_BUTTON;
                   1589:        mice[joy].enabled = 1;
                   1590:     }
                   1591: 
                   1592:     joy = jsem_isjoy (1, prefs);
                   1593:     if (joy >= 0) {
                   1594:        joysticks[joy].eventid[ID_AXIS_OFFSET + 0][0] = INPUTEVENT_JOY2_HORIZ;
                   1595:        joysticks[joy].eventid[ID_AXIS_OFFSET + 1][0] = INPUTEVENT_JOY2_VERT;
                   1596:        used[joy] = 1;
                   1597:        joysticks[joy].eventid[ID_BUTTON_OFFSET + 0][0] = INPUTEVENT_JOY2_FIRE_BUTTON;
                   1598:        joysticks[joy].eventid[ID_BUTTON_OFFSET + 1][0] = INPUTEVENT_JOY2_2ND_BUTTON;
1.1.1.3   root     1599:        joysticks[joy].eventid[ID_BUTTON_OFFSET + 2][0] = INPUTEVENT_JOY2_FIRE_BUTTON_AF;
1.1.1.2   root     1600: #ifdef CD32
                   1601:        if (cd32_enabled)
                   1602:            setcd32 (joy, 1);
                   1603: #endif
                   1604:        joysticks[joy].enabled = 1;
                   1605:     }
                   1606: 
                   1607:     joy = jsem_isjoy (0, prefs);
                   1608:     if (joy >= 0) {
                   1609:        used[joy] = 1;
                   1610:        joysticks[joy].eventid[ID_AXIS_OFFSET + 0][0] = INPUTEVENT_JOY1_HORIZ;
                   1611:        joysticks[joy].eventid[ID_AXIS_OFFSET + 1][0] = INPUTEVENT_JOY1_VERT;
                   1612:        joysticks[joy].eventid[ID_BUTTON_OFFSET + 0][0] = INPUTEVENT_JOY1_FIRE_BUTTON;
                   1613:        joysticks[joy].eventid[ID_BUTTON_OFFSET + 1][0] = INPUTEVENT_JOY1_2ND_BUTTON;
1.1.1.3   root     1614:        joysticks[joy].eventid[ID_BUTTON_OFFSET + 2][0] = INPUTEVENT_JOY1_FIRE_BUTTON_AF;
1.1.1.2   root     1615:        joysticks[joy].enabled = 1;
                   1616:     }
                   1617: 
                   1618:     for (joy = 0; used[joy]; joy++);
                   1619:     if (JSEM_ISANYKBD (0, prefs)) {
                   1620:        joysticks[joy].eventid[ID_AXIS_OFFSET +  0][0] = INPUTEVENT_JOY1_HORIZ;
                   1621:        joysticks[joy].eventid[ID_AXIS_OFFSET +  1][0] = INPUTEVENT_JOY1_VERT;
                   1622:        joysticks[joy].eventid[ID_BUTTON_OFFSET + 0][0] = INPUTEVENT_JOY1_FIRE_BUTTON;
                   1623:        joysticks[joy].eventid[ID_BUTTON_OFFSET + 1][0] = INPUTEVENT_JOY1_2ND_BUTTON;
                   1624:        joysticks[joy].eventid[ID_BUTTON_OFFSET + 2][0] = INPUTEVENT_JOY1_3RD_BUTTON;
                   1625:        joysticks[joy].enabled = 1;
                   1626:        used[joy] = 1;
                   1627:        compatibility_device[0] = joy;
                   1628:     }
                   1629:     for (joy = 0; used[joy]; joy++);
                   1630:     if (JSEM_ISANYKBD (1, prefs)) {
                   1631:        joysticks[joy].eventid[ID_AXIS_OFFSET +  0][0] = INPUTEVENT_JOY2_HORIZ;
                   1632:        joysticks[joy].eventid[ID_AXIS_OFFSET +  1][0] = INPUTEVENT_JOY2_VERT;
                   1633:        joysticks[joy].eventid[ID_BUTTON_OFFSET + 0][0] = INPUTEVENT_JOY2_FIRE_BUTTON;
                   1634:        joysticks[joy].eventid[ID_BUTTON_OFFSET + 1][0] = INPUTEVENT_JOY2_2ND_BUTTON;
                   1635:        joysticks[joy].eventid[ID_BUTTON_OFFSET + 2][0] = INPUTEVENT_JOY2_3RD_BUTTON;
                   1636: #ifdef CD32
                   1637:        if (cd32_enabled)
                   1638:            setcd32 (joy, 1);
                   1639: #endif
                   1640:        joysticks[joy].enabled = 1;
                   1641:        used[joy] = 1;
                   1642:        compatibility_device[1] = joy;
                   1643:     }
                   1644: }
                   1645: 
                   1646: void inputdevice_updateconfig (struct uae_prefs *prefs)
                   1647: {
                   1648:     int i;
                   1649: 
                   1650:     if (currprefs.jport0 != changed_prefs.jport0
                   1651:        || currprefs.jport1 != changed_prefs.jport1) {
                   1652:        currprefs.jport0 = changed_prefs.jport0;
                   1653:        currprefs.jport1 = changed_prefs.jport1;
                   1654:     }
                   1655:     joybutton[0] = joybutton[1] = 0;
                   1656:     joydir[0] = joydir[1] = 0;
                   1657:     oldmx[0] = oldmx[1] = -1;
                   1658:     oldmy[0] = oldmy[1] = -1;
                   1659:     cd32_shifter[0] = cd32_shifter[1] = 8;
                   1660:     oleft[0] = oleft[1] = 0;
                   1661:     oright[0] = oright[1] = 0;
                   1662:     otop[0] = otop[1] = 0;
                   1663:     obot[0] = obot[1] = 0;
                   1664:     for (i = 0; i < 2; i++) {
                   1665:        mouse_deltanoreset[i][0] = 0;
                   1666:        mouse_delta[i][0] = 0;
                   1667:        mouse_deltanoreset[i][1] = 0;
                   1668:        mouse_delta[i][1] = 0;
                   1669:        mouse_deltanoreset[i][2] = 0;
                   1670:        mouse_delta[i][2] = 0;
                   1671:     }
                   1672:     memset (keybuf, 0, sizeof (keybuf));
                   1673: 
                   1674:     for (i = 0; i < INPUT_QUEUE_SIZE; i++)
                   1675:        input_queue[i].framecnt = input_queue[i].nextframecnt = -1;
                   1676: 
                   1677:     for (i = 0; i < MAX_INPUT_SUB_EVENT; i++) {
                   1678:        sublevdir[0][i] = i;
                   1679:        sublevdir[1][i] = MAX_INPUT_SUB_EVENT - i - 1;
                   1680:     }
                   1681: 
                   1682:     joysticks = prefs->joystick_settings[prefs->input_selected_setting];
                   1683:     mice = prefs->mouse_settings[prefs->input_selected_setting];
                   1684:     keyboards = prefs->keyboard_settings[prefs->input_selected_setting];
                   1685: 
                   1686:     memset (joysticks2, 0, sizeof (joysticks2));
                   1687:     memset (mice2, 0, sizeof (mice2));
                   1688:     if (prefs->input_selected_setting == 0)
                   1689:        compatibility_mode (prefs);
                   1690: 
                   1691:     joystick_setting_changed ();
                   1692: 
                   1693:     scanevents (prefs);
                   1694: 
                   1695: #ifdef CD32
                   1696:     if (currprefs.input_selected_setting == 0 && cd32_enabled)
                   1697:        cd32_pad_enabled[1] = 1;
                   1698: #endif
                   1699: }
                   1700: 
                   1701: static void set_kbr_default (struct uae_prefs *p, int index, int num)
                   1702: {
                   1703:     int i, j, k, l;
                   1704:     struct uae_input_device_kbr_default *trans = keyboard_default;
                   1705:     struct uae_input_device *kbr;
                   1706:     struct inputdevice_functions *id = &idev[IDTYPE_KEYBOARD];
                   1707:     uae_u32 scancode;
                   1708: 
                   1709:     if (!trans)
                   1710:        return;
                   1711:     for (j = 0; j < MAX_INPUT_DEVICES; j++) {
                   1712:        kbr = &p->keyboard_settings[index][j];
                   1713:        for (i = 0; i < MAX_INPUT_DEVICE_EVENTS; i++) {
                   1714:            memset (kbr, 0, sizeof (struct uae_input_device));
                   1715:            kbr->extra[i][0] = -1;
                   1716:        }
                   1717:        if (j < id->get_num ()) {
                   1718:            if (j == 0)
                   1719:                kbr->enabled = 1;
                   1720:            for (i = 0; i < id->get_widget_num (num); i++) {
                   1721:                id->get_widget_type (num, i, 0, &scancode);
                   1722:                kbr->extra[i][0] = scancode;
                   1723:                l = 0;
                   1724:                while (trans[l].scancode >= 0) {
                   1725:                    if (kbr->extra[i][0] == trans[l].scancode) {
                   1726:                        for (k = 0; k < MAX_INPUT_SUB_EVENT; k++) {
                   1727:                            if (kbr->eventid[i][k] == 0) break;
                   1728:                        }
                   1729:                        if (k == MAX_INPUT_SUB_EVENT) {
                   1730:                            write_log ("corrupt default keyboard mappings\n");
                   1731:                            return;
                   1732:                        }
                   1733:                        kbr->eventid[i][k] = trans[l].event;
                   1734:                        break;
                   1735:                    }
                   1736:                    l++;
                   1737:                }
                   1738:            }
                   1739:        }
                   1740:     }
                   1741: }
                   1742: 
                   1743: void inputdevice_default_prefs (struct uae_prefs *p)
                   1744: {
                   1745:     int i;
                   1746: 
                   1747:     inputdevice_init ();
                   1748:     p->input_joymouse_multiplier = 20;
                   1749:     p->input_joymouse_deadzone = 33;
                   1750:     p->input_joystick_deadzone = 33;
                   1751:     p->input_joymouse_speed = 10;
                   1752:     p->input_mouse_speed = 100;
                   1753:     p->input_autofire_framecnt = 10;
                   1754:     for (i = 0; i <= MAX_INPUT_SETTINGS; i++) {
                   1755:        set_kbr_default (p, i, 0);
                   1756:        input_get_default_mouse (p->mouse_settings[i]);
                   1757:        input_get_default_joystick (p->joystick_settings[i]);
                   1758:     }
                   1759: }
                   1760: 
                   1761: void inputdevice_setkeytranslation (struct uae_input_device_kbr_default *trans)
                   1762: {
                   1763:     keyboard_default = trans;
                   1764: }
                   1765: 
                   1766: int inputdevice_translatekeycode (int keyboard, int scancode, int state)
                   1767: {
                   1768:     struct uae_input_device *na = &keyboards[keyboard];
                   1769:     int j, k;
                   1770: 
                   1771:     if (!keyboards || scancode < 0)
                   1772:        return 0;
                   1773:     j = 0;
                   1774:     while (na->extra[j][0] >= 0) {
                   1775:        if (na->extra[j][0] == scancode) {
                   1776:            for (k = 0; k < MAX_INPUT_SUB_EVENT; k++) {/* send key release events in reverse order */
                   1777:                int autofire = (na->flags[j][sublevdir[state == 0 ? 1 : 0][k]] & ID_FLAG_AUTOFIRE) ? 1 : 0;
                   1778:                int event = na->eventid[j][sublevdir[state == 0 ? 1 : 0][k]];
                   1779:                char *custom = na->custom[j][sublevdir[state == 0 ? 1 : 0][k]];
                   1780:                handle_input_event (event, state, 1, autofire);
                   1781:                //write_log ("'%s' %d ('%s') %d\n", na->name, event, events[event].name,  state);
                   1782:            }
                   1783:            return 1;
                   1784:        }
                   1785:        j++;
                   1786:     }
                   1787:     return 0;
                   1788: }
                   1789: 
                   1790: void inputdevice_init (void)
                   1791: {
                   1792:     idev[IDTYPE_JOYSTICK] = inputdevicefunc_joystick;
                   1793:     idev[IDTYPE_JOYSTICK].init ();
                   1794:     idev[IDTYPE_MOUSE] = inputdevicefunc_mouse;
                   1795:     idev[IDTYPE_MOUSE].init ();
                   1796:     idev[IDTYPE_KEYBOARD] = inputdevicefunc_keyboard;
                   1797:     idev[IDTYPE_KEYBOARD].init ();
                   1798: }
                   1799: 
                   1800: void inputdevice_close (void)
                   1801: {
                   1802:     idev[IDTYPE_JOYSTICK].close ();
                   1803:     idev[IDTYPE_MOUSE].close ();
                   1804:     idev[IDTYPE_KEYBOARD].close ();
                   1805: }
                   1806: 
                   1807: static struct uae_input_device *get_uid (struct inputdevice_functions *id, int devnum)
                   1808: {
                   1809:     struct uae_input_device *uid = 0;
                   1810:     if (id == &idev[IDTYPE_JOYSTICK]) {
                   1811:        uid = &joysticks[devnum];
                   1812:     } else if (id == &idev[IDTYPE_MOUSE]) {
                   1813:        uid = &mice[devnum];
                   1814:     } else if (id == &idev[IDTYPE_KEYBOARD]) {
                   1815:        uid = &keyboards[devnum];
                   1816:     }
                   1817:     return uid;
                   1818: }
                   1819: 
                   1820: static int get_event_data (struct inputdevice_functions *id, int devnum, int num, int *eventid, char **custom, int *flags, int sub)
                   1821: {
                   1822:     struct uae_input_device *uid = get_uid (id, devnum);
                   1823:     int type = id->get_widget_type (devnum, num, 0, 0);
                   1824:     int i;
                   1825:     if (type == IDEV_WIDGET_BUTTON) {
                   1826:        i = num - id->get_widget_first (devnum, type) + ID_BUTTON_OFFSET;
                   1827:        *eventid = uid->eventid[i][sub];
                   1828:        *flags = uid->flags[i][sub];
                   1829:        *custom = uid->custom[i][sub];
                   1830:        return i;
                   1831:     } else if (type == IDEV_WIDGET_AXIS) {
                   1832:        i = num - id->get_widget_first (devnum, type) + ID_AXIS_OFFSET;
                   1833:        *eventid = uid->eventid[i][sub];
                   1834:        *flags = uid->flags[i][sub];
                   1835:        *custom = uid->custom[i][sub];
                   1836:        return i;
                   1837:     } else if (type == IDEV_WIDGET_KEY) {
                   1838:        i = num - id->get_widget_first (devnum, type);
                   1839:        *eventid = uid->eventid[i][sub];
                   1840:        *flags = uid->flags[i][sub];
                   1841:        *custom = uid->custom[i][sub];
                   1842:        return i;
                   1843:     }
                   1844:     return -1;
                   1845: }
                   1846: 
                   1847: static int put_event_data (struct inputdevice_functions *id, int devnum, int num, int eventid, char *custom, int flags, int sub)
                   1848: {
                   1849:     struct uae_input_device *uid = get_uid (id, devnum);
                   1850:     int type = id->get_widget_type (devnum, num, 0, 0);
                   1851:     int i;
                   1852:     if (type == IDEV_WIDGET_BUTTON) {
                   1853:        i = num - id->get_widget_first (devnum, type) + ID_BUTTON_OFFSET;
                   1854:        uid->eventid[i][sub] = eventid;
                   1855:        uid->flags[i][sub] = flags;
                   1856:        free (uid->custom[i][sub]);
                   1857:        uid->custom[i][sub] = custom ? my_strdup (custom) : NULL;
                   1858:        return i;
                   1859:     } else if (type == IDEV_WIDGET_AXIS) {
                   1860:        i = num - id->get_widget_first (devnum, type) + ID_AXIS_OFFSET;
                   1861:        uid->eventid[i][sub] = eventid;
                   1862:        uid->flags[i][sub] = flags;
                   1863:        free (uid->custom[i][sub]);
                   1864:        uid->custom[i][sub] = custom ? my_strdup (custom) : NULL;
                   1865:        return i;
                   1866:     } else if (type == IDEV_WIDGET_KEY) {
                   1867:        i = num - id->get_widget_first (devnum, type);
                   1868:        uid->eventid[i][sub] = eventid;
                   1869:        uid->flags[i][sub] = flags;
                   1870:        free (uid->custom[i][sub]);
                   1871:        uid->custom[i][sub] = custom ? my_strdup (custom) : NULL;
                   1872:        return i;
                   1873:     }
                   1874:     return -1;
                   1875: }
                   1876: 
                   1877: static int is_event_used (struct inputdevice_functions *id, int devnum, int isnum, int isevent)
                   1878: {
                   1879:     struct uae_input_device *uid = get_uid (id, devnum);
                   1880:     int num, event, flag, sub;
                   1881:     char *custom;
                   1882: 
                   1883:     for (num = 0; num < id->get_widget_num (devnum); num++) {
                   1884:        for (sub = 0; sub < MAX_INPUT_SUB_EVENT; sub++) {
                   1885:            if (get_event_data (id, devnum, num, &event, &custom, &flag, sub) >= 0) {
                   1886:                if (event == isevent && isnum != num)
                   1887:                    return 1;
                   1888:            }
                   1889:        }
                   1890:     }
                   1891:     return 0;
                   1892: }
                   1893: 
                   1894: int inputdevice_get_device_index (int devnum)
                   1895: {
                   1896:     if (devnum < idev[IDTYPE_JOYSTICK].get_num())
                   1897:        return devnum;
                   1898:     else if (devnum < idev[IDTYPE_JOYSTICK].get_num() + idev[IDTYPE_MOUSE].get_num())
                   1899:        return devnum - idev[IDTYPE_JOYSTICK].get_num();
                   1900:     else if (devnum < idev[IDTYPE_JOYSTICK].get_num() + idev[IDTYPE_MOUSE].get_num() + idev[IDTYPE_KEYBOARD].get_num())
                   1901:        return devnum - idev[IDTYPE_JOYSTICK].get_num() - idev[IDTYPE_MOUSE].get_num();
                   1902:     else
                   1903:        return -1;
                   1904: }
                   1905: 
                   1906: static int gettype (int devnum)
                   1907: {
                   1908:     if (devnum < idev[IDTYPE_JOYSTICK].get_num())
                   1909:        return IDTYPE_JOYSTICK;
                   1910:     else if (devnum < idev[IDTYPE_JOYSTICK].get_num() + idev[IDTYPE_MOUSE].get_num())
                   1911:        return IDTYPE_MOUSE;
                   1912:     else if (devnum < idev[IDTYPE_JOYSTICK].get_num() + idev[IDTYPE_MOUSE].get_num() + idev[IDTYPE_KEYBOARD].get_num())
                   1913:        return IDTYPE_KEYBOARD;
                   1914:     else
                   1915:        return -1;
                   1916: }
                   1917: 
                   1918: static struct inputdevice_functions *getidf (int devnum)
                   1919: {
                   1920:     return &idev[gettype (devnum)];
                   1921: }
                   1922: 
                   1923: 
                   1924: /* returns number of devices of type "type" */
                   1925: int inputdevice_get_device_total (int type)
                   1926: {
                   1927:     return idev[type].get_num ();
                   1928: }
                   1929: /* returns the name of device */
                   1930: char *inputdevice_get_device_name (int type, int devnum)
                   1931: {
                   1932:     return idev[type].get_name (devnum);
                   1933: }
                   1934: /* returns state (enabled/disabled) */
                   1935: int inputdevice_get_device_status (int devnum)
                   1936: {
                   1937:     struct inputdevice_functions *idf = getidf (devnum);
                   1938:     struct uae_input_device *uid = get_uid (idf, inputdevice_get_device_index (devnum));
                   1939:     return uid->enabled;
                   1940: }
                   1941: 
                   1942: /* set state (enabled/disabled) */
                   1943: void inputdevice_set_device_status (int devnum, int enabled)
                   1944: {
                   1945:     struct inputdevice_functions *idf = getidf (devnum);
                   1946:     struct uae_input_device *uid = get_uid (idf, inputdevice_get_device_index (devnum));
                   1947:     uid->enabled = enabled;
                   1948: }
                   1949: 
                   1950: /* returns number of axis/buttons and keys from selected device */
                   1951: int inputdevice_get_widget_num (int devnum)
                   1952: {
                   1953:     struct inputdevice_functions *idf = getidf (devnum);
                   1954:     return idf->get_widget_num (inputdevice_get_device_index (devnum));
                   1955: }
                   1956: 
                   1957: static void get_ename (struct inputevent *ie, char *out)
                   1958: {
                   1959:     if (!out)
                   1960:        return;
                   1961:     if (ie->allow_mask == AM_K)
                   1962:        sprintf (out, "%s (0x%02.2X)", ie->name, ie->data);
                   1963:     else
                   1964:        strcpy (out, ie->name);
                   1965: }
                   1966: 
                   1967: int inputdevice_iterate (int devnum, int num, char *name, int *af)
                   1968: {
                   1969:     struct inputdevice_functions *idf = getidf (devnum);
                   1970:     static int id_iterator;
                   1971:     struct inputevent *ie;
                   1972:     int mask, data, flags, type;
                   1973:     int devindex = inputdevice_get_device_index (devnum);
                   1974:     char *custom;
                   1975: 
                   1976:     *af = 0;
                   1977:     *name = 0;
                   1978:     for (;;) {
                   1979:        ie = &events[++id_iterator];
                   1980:        if (!ie->confname) {
                   1981:            id_iterator = 0;
                   1982:            return 0;
                   1983:        }
                   1984:        mask = 0;
                   1985:        type = idf->get_widget_type (devindex, num, 0, 0);
                   1986:        if (type == IDEV_WIDGET_BUTTON) {
                   1987:            if (idf == &idev[IDTYPE_JOYSTICK]) {
                   1988:                mask |= AM_JOY_BUT;
                   1989:            } else {
                   1990:                mask |= AM_MOUSE_BUT;
                   1991:            }
                   1992:        } else if (type == IDEV_WIDGET_AXIS) {
                   1993:            if (idf == &idev[IDTYPE_JOYSTICK]) {
                   1994:                mask |= AM_JOY_AXIS;
                   1995:            } else {
                   1996:                mask |= AM_MOUSE_AXIS;
                   1997:            }
                   1998:        } else if (type == IDEV_WIDGET_KEY) {
                   1999:            mask |= AM_K;
                   2000:        }
                   2001:        if (ie->allow_mask & AM_INFO) {
                   2002:            struct inputevent *ie2 = ie + 1;
                   2003:            while (!(ie2->allow_mask & AM_INFO)) {
                   2004:                if (is_event_used (idf, devindex, ie2 - ie, -1)) {
                   2005:                    ie2++;
                   2006:                    continue;
                   2007:                }
                   2008:                if (ie2->allow_mask & mask) break;
                   2009:                ie2++;
                   2010:            }
                   2011:            if (!(ie2->allow_mask & AM_INFO))
                   2012:                mask |= AM_INFO;
                   2013:        }
                   2014:        if (!(ie->allow_mask & mask))
                   2015:            continue;
                   2016:        get_event_data (idf, devindex, num, &data, &custom, &flags, 0);
                   2017:        get_ename (ie, name);
                   2018:        *af = (flags & ID_FLAG_AUTOFIRE) ? 1 : 0;
                   2019:        return 1;
                   2020:     }
                   2021: }
                   2022: 
                   2023: int inputdevice_get_mapped_name (int devnum, int num, int *pflags, char *name, char *custom, int sub)
                   2024: {
                   2025:     struct inputdevice_functions *idf = getidf (devnum);
                   2026:     struct uae_input_device *uid = get_uid (idf, inputdevice_get_device_index (devnum));
                   2027:     int flags = 0, flag, data;
                   2028:     int devindex = inputdevice_get_device_index (devnum);
                   2029:     char *customp = NULL;
                   2030: 
                   2031:     if (name)
                   2032:        strcpy (name, "<none>");
                   2033:     if (custom)
                   2034:        custom[0] = 0;
                   2035:     if (pflags)
                   2036:        *pflags = 0;
                   2037:     if (uid == 0 || num < 0)
                   2038:        return 0;
                   2039:     if (get_event_data (idf, devindex, num, &data, &customp, &flag, sub) < 0)
                   2040:        return 0;
                   2041:     if (customp && custom)
                   2042:        sprintf (custom, "\"%s\"", customp);
                   2043:     if (flag & ID_FLAG_AUTOFIRE)
                   2044:        flags |= IDEV_MAPPED_AUTOFIRE_SET;
                   2045:     if (!data)
                   2046:        return 0;
                   2047:     if (events[data].allow_mask & AM_AF)
                   2048:        flags |= IDEV_MAPPED_AUTOFIRE_POSSIBLE;
                   2049:     if (pflags)
                   2050:        *pflags = flags;
                   2051:     get_ename (&events[data], name);
                   2052:     return data;
                   2053: }
                   2054: 
                   2055: int inputdevice_set_mapping (int devnum, int num, char *name, char *custom, int af, int sub)
                   2056: {
                   2057:     struct inputdevice_functions *idf = getidf (devnum);
                   2058:     struct uae_input_device *uid = get_uid (idf, inputdevice_get_device_index (devnum));
                   2059:     int eid, data, flag, amask;
                   2060:     char ename[256];
                   2061:     int devindex = inputdevice_get_device_index (devnum);
                   2062: 
                   2063:     if (uid == 0 || num < 0)
                   2064:        return 0;
                   2065:     if (name) {
                   2066:        eid = 1;
                   2067:        while (events[eid].name) {
                   2068:            get_ename (&events[eid], ename);
                   2069:            if (!strcmp(ename, name)) break;
                   2070:            eid++;
                   2071:        }
                   2072:        if (!events[eid].name)
                   2073:            return 0;
                   2074:        if (events[eid].allow_mask & AM_INFO)
                   2075:            return 0;
                   2076:     } else {
                   2077:        eid = 0;
                   2078:     }
                   2079:     if (get_event_data (idf, devindex, num, &data, &custom, &flag, sub) < 0)
                   2080:        return 0;
                   2081:     if (data >= 0) {
                   2082:        amask = events[eid].allow_mask;
                   2083:        flag &= ~ID_FLAG_AUTOFIRE;
                   2084:        if (amask & AM_AF)
                   2085:            flag |= af ? ID_FLAG_AUTOFIRE : 0;
                   2086:        put_event_data (idf, devindex, num, eid, custom, flag, sub);
                   2087:        return 1;
                   2088:     }
                   2089:     return 0;
                   2090: }
                   2091: 
                   2092: int inputdevice_get_widget_type (int devnum, int num, char *name)
                   2093: {
                   2094:     struct inputdevice_functions *idf = getidf (devnum);
                   2095:     return idf->get_widget_type (inputdevice_get_device_index (devnum), num, name, 0);
                   2096: }
                   2097: 
                   2098: static int config_change;
                   2099: 
                   2100: void inputdevice_config_change (void)
                   2101: {
                   2102:     config_change = 1;
                   2103: }
                   2104: 
                   2105: int inputdevice_config_change_test (void)
                   2106: {
                   2107:     int v = config_change;
                   2108:     config_change = 0;
                   2109:     return v;
                   2110: }
                   2111: 
                   2112: void inputdevice_copyconfig (struct uae_prefs *src, struct uae_prefs *dst)
                   2113: {
                   2114:     int i, j;
                   2115: 
                   2116:     dst->input_selected_setting = src->input_selected_setting;
                   2117:     dst->input_joymouse_multiplier = src->input_joymouse_multiplier;
                   2118:     dst->input_joymouse_deadzone = src->input_joymouse_deadzone;
                   2119:     dst->input_joystick_deadzone = src->input_joystick_deadzone;
                   2120:     dst->input_joymouse_speed = src->input_joymouse_speed;
                   2121:     dst->input_mouse_speed = src->input_mouse_speed;
                   2122:     dst->input_autofire_framecnt = src->input_autofire_framecnt;
                   2123:     dst->jport0 = src->jport0;
                   2124:     dst->jport1 = src->jport1;
                   2125: 
                   2126:     for (i = 0; i < MAX_INPUT_SETTINGS + 1; i++) {
                   2127:        for (j = 0; j < MAX_INPUT_DEVICES; j++) {
                   2128:            memcpy (&dst->joystick_settings[i][j], &src->joystick_settings[i][j], sizeof (struct uae_input_device));
                   2129:            memcpy (&dst->mouse_settings[i][j], &src->mouse_settings[i][j], sizeof (struct uae_input_device));
                   2130:            memcpy (&dst->keyboard_settings[i][j], &src->keyboard_settings[i][j], sizeof (struct uae_input_device));
                   2131:        }
                   2132:     }
                   2133: 
                   2134:     inputdevice_updateconfig (dst);
                   2135: }
                   2136: 
                   2137: void inputdevice_swap_ports (struct uae_prefs *p, int devnum)
                   2138: {
                   2139:     struct inputdevice_functions *idf = getidf (devnum);
                   2140:     struct uae_input_device *uid = get_uid (idf, inputdevice_get_device_index (devnum));
                   2141:     int i, j, k, event, unit;
                   2142:     struct inputevent *ie, *ie2;
                   2143: 
                   2144:     for (i = 0; i < MAX_INPUT_DEVICE_EVENTS; i++) {
                   2145:        for (j = 0; j < MAX_INPUT_SUB_EVENT; j++) {
                   2146:            event = uid->eventid[i][j];
                   2147:            if (event <= 0)
                   2148:                continue;
                   2149:            ie = &events[event];
                   2150:            if (ie->unit <= 0)
                   2151:                continue;
                   2152:            unit = ie->unit;
                   2153:            k = 1;
                   2154:            while (events[k].confname) {
                   2155:                ie2 = &events[k];
                   2156:                if (ie2->type == ie->type && ie2->data == ie->data && ie2->unit - 1 == ((ie->unit - 1) ^ 1) && ie2->allow_mask == ie->allow_mask) {
                   2157:                    uid->eventid[i][j] = k;
                   2158:                    break;
                   2159:                }
                   2160:                k++;
                   2161:            }
                   2162:        }
                   2163:     }
                   2164: }
                   2165: 
                   2166: void inputdevice_copy_single_config (struct uae_prefs *p, int src, int dst, int devnum)
                   2167: {
                   2168:     if (src == dst)
                   2169:        return;
                   2170:     if (devnum < 0 || gettype (devnum) == IDTYPE_JOYSTICK)
                   2171:        memcpy (p->joystick_settings[dst], p->joystick_settings[src], sizeof (struct uae_input_device) * MAX_INPUT_DEVICES);
                   2172:     if (devnum < 0 || gettype (devnum) == IDTYPE_MOUSE)
                   2173:        memcpy (p->mouse_settings[dst], p->mouse_settings[src], sizeof (struct uae_input_device) * MAX_INPUT_DEVICES);
                   2174:     if (devnum < 0 || gettype (devnum) == IDTYPE_KEYBOARD)
                   2175:        memcpy (p->keyboard_settings[dst], p->keyboard_settings[src], sizeof (struct uae_input_device) * MAX_INPUT_DEVICES);
                   2176: }
                   2177: 
                   2178: void inputdevice_acquire (void)
                   2179: {
                   2180:     int i;
                   2181: 
                   2182:     inputdevice_unacquire ();
                   2183:     for (i = 0; i < MAX_INPUT_DEVICES; i++) {
                   2184:        if (use_joysticks[i])
                   2185:            idev[IDTYPE_JOYSTICK].acquire (i, 0);
                   2186:     }
                   2187:     for (i = 0; i < MAX_INPUT_DEVICES; i++) {
                   2188:        if (use_mice[i])
                   2189:            idev[IDTYPE_MOUSE].acquire (i, 0);
                   2190:     }
                   2191:     for (i = 0; i < MAX_INPUT_DEVICES; i++) {
                   2192:        if (use_keyboards[i])
                   2193:            idev[IDTYPE_KEYBOARD].acquire (i, 0);
                   2194:     }
                   2195: }
                   2196: 
                   2197: void inputdevice_unacquire (void)
                   2198: {
                   2199:     int i;
                   2200: 
                   2201:     for (i = 0; i < MAX_INPUT_DEVICES; i++)
                   2202:        idev[IDTYPE_JOYSTICK].unacquire (i);
                   2203:     for (i = 0; i < MAX_INPUT_DEVICES; i++)
                   2204:        idev[IDTYPE_MOUSE].unacquire (i);
                   2205:     for (i = 0; i < MAX_INPUT_DEVICES; i++)
                   2206:        idev[IDTYPE_KEYBOARD].unacquire (i);
                   2207: }
                   2208: 
                   2209: /* Call this function when host machine's joystick/joypad/etc button state changes
                   2210:  * This function translates button events to Amiga joybutton/joyaxis/keyboard events
                   2211:  */
                   2212: 
                   2213: /* button states:
                   2214:  * state = -1 -> mouse wheel turned or similar (button without release)
                   2215:  * state = 1 -> button pressed
                   2216:  * state = 0 -> button released
                   2217:  */
                   2218: 
                   2219: void setjoybuttonstate (int joy, int button, int state)
                   2220: {
                   2221:     if (!joysticks[joy].enabled)
                   2222:        return;
                   2223:     setbuttonstateall (&joysticks[joy], &joysticks2[joy], button, state ? 1 : 0);
                   2224: }
                   2225: 
                   2226: /* buttonmask = 1 = normal toggle button, 0 = mouse wheel turn or similar
                   2227:  */
                   2228: void setjoybuttonstateall (int joy, uae_u32 buttonbits, uae_u32 buttonmask)
                   2229: {
                   2230:     int i;
                   2231: 
                   2232:     if (!joysticks[joy].enabled)
                   2233:        return;
                   2234:     for (i = 0; i < ID_BUTTON_TOTAL; i++) {
                   2235:        if (buttonmask & (1 << i))
                   2236:            setbuttonstateall (&joysticks[joy], &joysticks2[joy], i, (buttonbits & (1 << i)) ? 1 : 0);
                   2237:        else if (buttonbits & (1 << i))
                   2238:            setbuttonstateall (&joysticks[joy], &joysticks2[joy], i, -1);
                   2239:     }
                   2240: }
                   2241: /* mouse buttons (just like joystick buttons)
                   2242:  */
                   2243: void setmousebuttonstateall (int mouse, uae_u32 buttonbits, uae_u32 buttonmask)
                   2244: {
                   2245:     int i;
                   2246: 
                   2247:     if (!mice[mouse].enabled)
                   2248:        return;
                   2249:     for (i = 0; i < ID_BUTTON_TOTAL; i++) {
                   2250:        if (buttonmask & (1 << i))
                   2251:            setbuttonstateall (&mice[mouse], &mice2[mouse], i, (buttonbits & (1 << i)) ? 1 : 0);
                   2252:        else if (buttonbits & (1 << i))
                   2253:            setbuttonstateall (&mice[mouse], &mice2[mouse], i, -1);
                   2254:     }
                   2255: }
                   2256: 
                   2257: void setmousebuttonstate (int mouse, int button, int state)
                   2258: {
                   2259:     if (!mice[mouse].enabled)
                   2260:        return;
                   2261:     setbuttonstateall (&mice[mouse], &mice2[mouse], button, state);
                   2262: }
                   2263: 
                   2264: /* same for joystick axis (analog or digital)
                   2265:  * (0 = center, -max = full left/top, max = full right/bottom)
                   2266:  */
                   2267: void setjoystickstate (int joy, int axis, int state, int max)
                   2268: {
                   2269:     struct uae_input_device *id = &joysticks[joy];
                   2270:     struct uae_input_device2 *id2 = &joysticks2[joy];
                   2271:     int deadzone = currprefs.input_joymouse_deadzone * max / 100;
                   2272:     int i, v1, v2;
                   2273: 
                   2274:     if (!joysticks[joy].enabled)
                   2275:        return;
                   2276:     v1 = state;
                   2277:     v2 = id2->states[axis];
                   2278:     if (v1 < deadzone && v1 > -deadzone)
                   2279:        v1 = 0;
                   2280:     if (v2 < deadzone && v2 > -deadzone)
                   2281:        v2 = 0;
                   2282:     if (v1 == v2)
                   2283:        return;
                   2284:     for (i = 0; i < MAX_INPUT_SUB_EVENT; i++)
                   2285:        handle_input_event (id->eventid[ID_AXIS_OFFSET + axis][i], state, max,
                   2286:        id->flags[ID_AXIS_OFFSET + axis][i]);
                   2287:     id2->states[axis] = state;
                   2288: }
                   2289: 
                   2290: void setmousestate (int mouse, int axis, int data, int isabs)
                   2291: {
                   2292:     int i, v;
                   2293:     double *mouse_p, *oldm_p, d, diff;
                   2294:     struct uae_input_device *id = &mice[mouse];
                   2295:     static double fract1[MAX_INPUT_DEVICES][MAX_INPUT_DEVICE_EVENTS];
                   2296:     static double fract2[MAX_INPUT_DEVICES][MAX_INPUT_DEVICE_EVENTS];
                   2297: 
                   2298:     if (!mice[mouse].enabled)
                   2299:        return;
                   2300:     d = 0;
                   2301:     mouse_p = &mouse_axis[mouse][axis];
                   2302:     oldm_p = &oldm_axis[mouse][axis];
                   2303:     if (!isabs) {
                   2304:        *oldm_p = *mouse_p;
                   2305:        *mouse_p += data;
                   2306:        d = (*mouse_p - *oldm_p) * currprefs.input_mouse_speed / 100.0;
1.1.1.3   root     2307:        /*      printf (" offs %f\n", d); */
1.1.1.2   root     2308:     } else {
                   2309:        d = data - (int)(*oldm_p);
                   2310:        *oldm_p = data;
                   2311:        *mouse_p += d;
                   2312:        if (axis == 0)
                   2313:            lastmx = data;
                   2314:        else
                   2315:            lastmy = data;
                   2316:     }
                   2317:     v = (int)(d > 0 ? d + 0.5 : d - 0.5);
                   2318:     fract1[mouse][axis] += d;
                   2319:     fract2[mouse][axis] += v;
                   2320:     diff = fract2[mouse][axis] - fract1[mouse][axis];
                   2321:     if (diff > 1 || diff < -1) {
                   2322:        v -= (int)diff;
                   2323:        fract2[mouse][axis] -= diff;
                   2324:     }
                   2325:     for (i = 0; i < MAX_INPUT_SUB_EVENT; i++)
                   2326:        handle_input_event (id->eventid[ID_AXIS_OFFSET + axis][i], v, 0, 0);
1.1.1.4 ! root     2327:     new_mousehack_helper();
1.1.1.2   root     2328: }
                   2329: 
                   2330: #if 0
                   2331: void warpmode (int mode)
                   2332: {
                   2333:     if (mode < 0) {
                   2334:        if (turbo_emulation) {
                   2335:            changed_prefs.gfx_framerate = currprefs.gfx_framerate = turbo_emulation;
                   2336:            turbo_emulation = 0;
                   2337:        }  else {
                   2338:            turbo_emulation = currprefs.gfx_framerate;
                   2339:        }
                   2340:     } else if (mode == 0 && turbo_emulation > 0) {
                   2341:        changed_prefs.gfx_framerate = currprefs.gfx_framerate = turbo_emulation;
                   2342:        turbo_emulation = 0;
                   2343:     } else if (mode > 0 && !turbo_emulation) {
                   2344:        turbo_emulation = currprefs.gfx_framerate;
                   2345:     }
                   2346:     if (turbo_emulation) {
                   2347: #if 0
                   2348:        if (!currprefs.cpu_cycle_exact && !currprefs.blitter_cycle_exact)
                   2349:            changed_prefs.gfx_framerate = currprefs.gfx_framerate = 10;
                   2350: #endif
                   2351:        pause_sound ();
                   2352:     } else {
                   2353:        resume_sound ();
                   2354:     }
                   2355:     compute_vsynctime ();
                   2356: }
                   2357: #endif
                   2358: 
                   2359: void pausemode (int mode)
                   2360: {
                   2361:     if (mode < 0)
                   2362:        pause_emulation = pause_emulation ? 0 : 1;
                   2363:     else
                   2364:        pause_emulation = mode;
                   2365: }
                   2366: 
                   2367: int jsem_isjoy (int port, struct uae_prefs *p)
                   2368: {
                   2369:     int v = JSEM_DECODEVAL (port, p);
                   2370:     if (v < JSEM_JOYS)
                   2371:        return -1;
                   2372:     v -= JSEM_JOYS;
                   2373:     if (v >= inputdevice_get_device_total (IDTYPE_JOYSTICK))
                   2374:        return -1;
                   2375:     return v;
                   2376: }
                   2377: int jsem_ismouse (int port, struct uae_prefs *p)
                   2378: {
                   2379:     int v = JSEM_DECODEVAL (port, p);
                   2380:     if (v < JSEM_MICE)
                   2381:        return -1;
                   2382:     v -= JSEM_MICE;
                   2383:     if (v >= inputdevice_get_device_total (IDTYPE_MOUSE))
                   2384:        return -1;
                   2385:     return v;
                   2386: }
                   2387: int jsem_iskbdjoy (int port, struct uae_prefs *p)
                   2388: {
                   2389:     int v = JSEM_DECODEVAL (port, p);
                   2390:     if (v < JSEM_KBDLAYOUT)
                   2391:        return -1;
                   2392:     v -= JSEM_KBDLAYOUT;
                   2393:     if (v >= JSEM_LASTKBD)
                   2394:        return -1;
                   2395:     return v;
                   2396: }
                   2397: 
                   2398: extern int jsem_ismouse (int v, struct uae_prefs*);
                   2399: extern int jsem_iskbd (int v, struct uae_prefs*);

unix.superglobalmegacorp.com

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