home *** CD-ROM | disk | FTP | other *** search
- /*
- * gbe - gameboy emulator
- * Copyright (C) 1999 Chuck Mason, Steven Fuller
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- *
- * Chuck Mason <chuckjr@sinclair.net>
- * Steven Fuller <relnev@atdot.org>
- */
-
- #ifdef BUILD_JLIB
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/ioctl.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <errno.h>
-
- #include <linux/joystick.h>
-
- #include "jlib.h"
-
- jlib_joystick joysticks[4] = {
- { -1, 0, 0, 0, 0x00000000 },
- { -1, 0, 0, 0, 0x00000000 },
- { -1, 0, 0, 0, 0x00000000 },
- { -1, 0, 0, 0, 0x00000000 }
- };
-
- /*
- * Default would be 0 for 'device'
- *
- * returns greater than or equal to 0 (returns the joystick file desc)
- * on success, otherwise less than 0 is failure.
- */
- int jlib_open(int device)
- {
- char buffer[80];
- char temp;
-
- if(device < 0 || device > 3)
- return -1;
-
- if(joysticks[device].joystick_fd >= 0)
- return -2;
-
- sprintf(buffer, "/dev/js%d", device);
-
- joysticks[device].joystick_fd = open(buffer, O_RDONLY | O_NONBLOCK);
-
- if(joysticks[device].joystick_fd < 0)
- return -3; /* Could not open joystick for reading */
-
- if(ioctl(joysticks[device].joystick_fd, JSIOCGNAME(sizeof(buffer)), buffer) < 0)
- strncpy(buffer, "Unknown", sizeof(buffer));
-
- if(ioctl(joysticks[device].joystick_fd, JSIOCGAXES, &temp) < 0)
- temp = 2;
- joysticks[device].num_axes = temp;
-
- if(ioctl(joysticks[device].joystick_fd, JSIOCGBUTTONS, &temp) < 0)
- temp = 2; /* Default to 2 button joystick */
- joysticks[device].num_buttons = temp;
-
- #ifdef DEBUG
- fprintf(stdout, "Joystick: %s\n\tButtons: %d\n\tAxis: %d\n", buffer, joysticks[device].num_buttons, joysticks[device].num_axes);
- #endif
-
- return joysticks[device].joystick_fd;
- }
-
- int jlib_close(int device)
- {
- if(joysticks[device].joystick_fd < 0)
- return 0;
-
- close(joysticks[device].joystick_fd);
- joysticks[device].joystick_fd = -1;
-
- return 1;
- }
-
- static int jlib_handle_event(int device, struct js_event e)
- {
- /* I dont think we really care about the init stuff */
- e.type &= ~JS_EVENT_INIT;
-
- switch(e.type) {
- case JS_EVENT_BUTTON:
- if(e.number >= jlib_getmaxbuttons(device))
- return -1; /* Cannot get a button that we don't have */
-
- if(e.value)
- joysticks[device].joystick_data |= JOYSTICK_BUTTON(e.number);
- else
- joysticks[device].joystick_data &= ~JOYSTICK_BUTTON(e.number);
- break;
- case JS_EVENT_AXIS:
- switch(e.number) {
- case 0: /* 1st X Axis */
- if(e.value < 0) {
- joysticks[device].joystick_data |= JOYSTICK_LEFT;
- joysticks[device].joystick_data &= ~JOYSTICK_RIGHT;
- } else if(e.value == 0) {
- joysticks[device].joystick_data &= ~(JOYSTICK_LEFT | JOYSTICK_RIGHT);
- } else {
- joysticks[device].joystick_data |= JOYSTICK_RIGHT;
- joysticks[device].joystick_data &= ~JOYSTICK_LEFT;
- }
- break;
- case 1: /* 1st Y Axis */
- if(e.value < 0) {
- joysticks[device].joystick_data |= JOYSTICK_UP;
- joysticks[device].joystick_data &= ~JOYSTICK_DOWN;
- } else if(e.value == 0) {
- joysticks[device].joystick_data &= ~(JOYSTICK_UP | JOYSTICK_DOWN);
- } else {
- joysticks[device].joystick_data |= JOYSTICK_DOWN;
- joysticks[device].joystick_data &= ~JOYSTICK_UP;
- }
- break;
- default:
- /* Ignore */
- break;
- }
- default: /* Ignore other events */
- break;
- }
- return 1;
- }
-
- /*
- * Returns positive number (or equal to 0)
- * number of events processed
- */
- int jlib_update(int device)
- {
- struct js_event jsevent;
- int num_events = 0;
-
- if(joysticks[device].joystick_fd < 0)
- return -1;
-
- while(read(joysticks[device].joystick_fd, &jsevent, sizeof(struct js_event)) > 0) {
- if(jlib_handle_event(device, jsevent) < 0) {
- joysticks[device].total_events += num_events;
- return -(num_events+1);
- }
- num_events++;
- }
- joysticks[device].total_events += num_events;
- if(errno != EAGAIN) /* We _really_ did have an error */
- return -1;
-
- return num_events;
- }
-
- int jlib_check(int device, unsigned long val)
- {
- if(joysticks[device].joystick_fd < 0)
- return -1;
-
- if(joysticks[device].joystick_data & val)
- return 1;
- else
- return 0;
- }
-
- int jlib_getmaxbuttons(int device)
- {
- if(joysticks[device].joystick_fd < 0)
- return 0;
- else
- return joysticks[device].num_buttons;
- }
-
- int jlib_getmaxaxes(int device)
- {
- if(joysticks[device].joystick_fd < 0)
- return 0;
- else
- return joysticks[device].num_axes;
- }
-
- #elif defined (WIN32)
-
- //////////////////////////////////////////////////////////////////////////////
-
- #define STRICT
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <mmsystem.h>
- #pragma comment(lib, "winmm")
- #include <stdio.h>
-
- #include "jlib.h"
-
- jlib_joystick joysticks[4] =
- {
- { -1, 0, 0, 0, 0x00000000 },
- { -1, 0, 0, 0, 0x00000000 },
- { -1, 0, 0, 0, 0x00000000 },
- { -1, 0, 0, 0, 0x00000000 }
- };
-
- int jlib_open(int device)
- {
- UINT uNumDevs;
- MMRESULT mmr;
- JOYINFO ji;
-
- #if 1
- // TODO: joyGetPos is painfully slow!!!
- return -1;
- #endif
-
- if(device < 0 && device > 1) {
- return -1;
- }
-
- if(joysticks[device].joystick_fd >= 0) {
- return -2;
- }
-
- uNumDevs = joyGetNumDevs();
- if(uNumDevs == 0) {
- return -1;
- }
-
- if(uNumDevs < device + 1) {
- return -1;
- }
-
- joysticks[device].joystick_fd = (device == 0) ? JOYSTICKID1 : JOYSTICKID2;
-
- mmr = joyGetPos(joysticks[device].joystick_fd, &ji);
- if (mmr == JOYERR_UNPLUGGED) {
- printf("Joystick #%d is not connected!\n", device + 1);
- return -3;
- }
-
- joysticks[device].num_buttons = jlib_getmaxbuttons(joysticks[device].joystick_fd);
-
- return joysticks[device].joystick_fd;
- }
-
- int jlib_close(int device)
- {
- if (joysticks[device].joystick_fd < 0) {
- return 0;
- }
-
- joysticks[device].joystick_fd = -1;
-
- return -1;
- }
-
- static int jlib_handle_event(int device, JOYINFO* pji)
- {
- static BOOL fB1Pressed = FALSE;
- static BOOL fB2Pressed = FALSE;
- static BOOL fB3Pressed = FALSE;
- static BOOL fB4Pressed = FALSE;
-
- if(pji->wButtons & JOY_BUTTON1) {
- if(!fB1Pressed) {
- fB1Pressed = TRUE;
- joysticks[device].joystick_data |= JOYSTICK_BUTTON(0);
- }
- } else {
- if(fB1Pressed) {
- fB1Pressed = FALSE;
- joysticks[device].joystick_data &= ~JOYSTICK_BUTTON(0);
- }
- }
-
- if(pji->wButtons & JOY_BUTTON2) {
- if(!fB2Pressed) {
- fB2Pressed = TRUE;
- joysticks[device].joystick_data |= JOYSTICK_BUTTON(1);
- }
- } else {
- if (fB2Pressed) {
- fB2Pressed = FALSE;
- joysticks[device].joystick_data &= ~JOYSTICK_BUTTON(1);
- }
- }
-
-
- if(pji->wButtons & JOY_BUTTON3) {
- if(!fB3Pressed) {
- fB3Pressed = TRUE;
- joysticks[device].joystick_data |= JOYSTICK_BUTTON(2);
- }
- } else {
- if(fB3Pressed) {
- fB3Pressed = FALSE;
- joysticks[device].joystick_data &= ~JOYSTICK_BUTTON(2);
- }
- }
-
- if(pji->wButtons & JOY_BUTTON4) {
- if(!fB4Pressed) {
- fB4Pressed = TRUE;
- joysticks[device].joystick_data |= JOYSTICK_BUTTON(3);
- }
- } else {
- if(fB4Pressed) {
- fB4Pressed = FALSE;
- joysticks[device].joystick_data &= ~JOYSTICK_BUTTON(3);
- }
- }
-
-
- #if 0
- case JS_EVENT_AXIS:
- switch(e.number) {
- case 0: /* 1st X Axis */
- if(e.value < 0) {
- joysticks[device].joystick_data |= JOYSTICK_LEFT;
- joysticks[device].joystick_data &= ~JOYSTICK_RIGHT;
- } else if(e.value == 0) {
- joysticks[device].joystick_data &= ~(JOYSTICK_LEFT | JOYSTICK_RIGHT);
- } else {
- joysticks[device].joystick_data |= JOYSTICK_RIGHT;
- joysticks[device].joystick_data &= ~JOYSTICK_LEFT;
- }
- break;
- case 1: /* 1st Y Axis */
- if(e.value < 0) {
- joysticks[device].joystick_data |= JOYSTICK_UP;
- joysticks[device].joystick_data &= ~JOYSTICK_DOWN;
- } else if(e.value == 0) {
- joysticks[device].joystick_data &= ~(JOYSTICK_UP | JOYSTICK_DOWN);
- } else {
- joysticks[device].joystick_data |= JOYSTICK_DOWN;
- joysticks[device].joystick_data &= ~JOYSTICK_UP;
- }
- break;
- }
- }
- #endif
-
- return 1;
- }
-
- int jlib_update(int device)
- {
- MMRESULT mmr;
- JOYINFO ji;
-
- if(joysticks[device].joystick_fd < 0) {
- return -1;
- }
-
- mmr = joyGetPos(joysticks[device].joystick_fd, &ji);
- if(mmr != JOYERR_NOERROR) {
- return -1;
- }
-
- if (jlib_handle_event(device, &ji) < 0) {
- return -1;
- }
-
- return 1;
- }
-
- int jlib_check(int device, unsigned long event)
- {
- if(joysticks[device].joystick_fd < 0) {
- return -1;
- }
-
- if(joysticks[device].joystick_data & event) {
- return 1;
- }
-
- return 0;
- }
-
- int jlib_getmaxbuttons(int device)
- {
- MMRESULT mmr;
- JOYCAPS jc;
-
- mmr = joyGetDevCaps(joysticks[device].joystick_fd, &jc, sizeof(jc));
- if(mmr != JOYERR_NOERROR) {
- return -1;
- }
-
- return jc.wNumButtons;
- }
-
- int jlib_getmaxaxes(int device)
- {
- MMRESULT mmr;
- JOYCAPS jc;
-
- mmr = joyGetDevCaps(joysticks[device].joystick_fd, &jc, sizeof(jc));
- if (mmr != JOYERR_NOERROR) {
- return -1;
- }
-
- return jc.wMaxAxes;
- }
-
- //////////////////////////////////////////////////////////////////////////////
-
- #else
-
- /* Returns: >=0 on success <0 on failure */
- int jlib_open(int o)
- {
- return -1;
- }
-
- int jlib_close(int c)
- {
- return -1;
- }
-
- int jlib_update(int u)
- {
- return -1;
- }
-
- int jlib_check(int device, unsigned long val)
- {
- return -1;
- }
-
- int jlib_getmaxbuttons(int device)
- {
- return -1;
- }
-
- int jlib_getmaxaxes(int a)
- {
- return -1;
- }
-
- #endif
-