home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 13 / MA_Cover_13.bin / source / c / gbe / jlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-12  |  11.8 KB  |  457 lines

  1. /*
  2.  *  gbe - gameboy emulator
  3.  *  Copyright (C) 1999  Chuck Mason, Steven Fuller
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program; if not, write to the Free Software
  17.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18.  *
  19.  *
  20.  *  Chuck Mason <chuckjr@sinclair.net>
  21.  *  Steven Fuller <relnev@atdot.org>
  22.  */
  23.  
  24. #ifdef BUILD_JLIB
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <sys/ioctl.h>
  29. #include <fcntl.h>
  30. #include <unistd.h>
  31. #include <errno.h>
  32.  
  33. #include <linux/joystick.h>
  34.  
  35. #include "jlib.h"
  36.  
  37. jlib_joystick joysticks[4] = {
  38.      { -1, 0, 0, 0, 0x00000000 },
  39.      { -1, 0, 0, 0, 0x00000000 },
  40.      { -1, 0, 0, 0, 0x00000000 },
  41.      { -1, 0, 0, 0, 0x00000000 }
  42. };
  43.  
  44. /*
  45.  * Default would be 0 for 'device'
  46.  *
  47.  * returns greater than or equal to 0 (returns the joystick file desc)
  48.  * on success, otherwise less than 0 is failure.
  49.  */
  50. int jlib_open(int device)
  51. {
  52.      char buffer[80];
  53.      char temp;
  54.      
  55.      if(device < 0 || device > 3)
  56.           return -1;
  57.           
  58.      if(joysticks[device].joystick_fd >= 0)
  59.           return -2;
  60.      
  61.      sprintf(buffer, "/dev/js%d", device);
  62.      
  63.      joysticks[device].joystick_fd = open(buffer, O_RDONLY | O_NONBLOCK);
  64.      
  65.      if(joysticks[device].joystick_fd < 0) 
  66.           return -3;     /* Could not open joystick for reading */
  67.           
  68.      if(ioctl(joysticks[device].joystick_fd, JSIOCGNAME(sizeof(buffer)), buffer) < 0)
  69.           strncpy(buffer, "Unknown", sizeof(buffer));
  70.  
  71.      if(ioctl(joysticks[device].joystick_fd, JSIOCGAXES, &temp) < 0) 
  72.           temp = 2;
  73.      joysticks[device].num_axes = temp;
  74.      
  75.      if(ioctl(joysticks[device].joystick_fd, JSIOCGBUTTONS, &temp) < 0)
  76.           temp = 2; /* Default to 2 button joystick */
  77.      joysticks[device].num_buttons = temp;
  78.      
  79. #ifdef DEBUG
  80.      fprintf(stdout, "Joystick: %s\n\tButtons: %d\n\tAxis: %d\n", buffer, joysticks[device].num_buttons, joysticks[device].num_axes);
  81. #endif
  82.  
  83.      return joysticks[device].joystick_fd;
  84. }
  85.  
  86. int jlib_close(int device)
  87. {
  88.      if(joysticks[device].joystick_fd < 0)
  89.           return 0;
  90.           
  91.      close(joysticks[device].joystick_fd);
  92.      joysticks[device].joystick_fd = -1;
  93.      
  94.      return 1;
  95. }
  96.  
  97. static int jlib_handle_event(int device, struct js_event e)
  98. {
  99.      /* I dont think we really care about the init stuff */ 
  100.      e.type &= ~JS_EVENT_INIT;
  101.      
  102.      switch(e.type) {
  103.           case JS_EVENT_BUTTON:
  104.                if(e.number >= jlib_getmaxbuttons(device))
  105.                     return -1; /* Cannot get a button that we don't have */
  106.  
  107.                if(e.value)
  108.                     joysticks[device].joystick_data |= JOYSTICK_BUTTON(e.number);
  109.                else
  110.                     joysticks[device].joystick_data &= ~JOYSTICK_BUTTON(e.number);
  111.                break;
  112.           case JS_EVENT_AXIS:
  113.                switch(e.number) {
  114.                     case 0:   /* 1st X Axis */
  115.                          if(e.value < 0) {
  116.                               joysticks[device].joystick_data |= JOYSTICK_LEFT;
  117.                               joysticks[device].joystick_data &= ~JOYSTICK_RIGHT;
  118.                          } else if(e.value == 0) {
  119.                               joysticks[device].joystick_data &= ~(JOYSTICK_LEFT | JOYSTICK_RIGHT);
  120.                          } else {
  121.                               joysticks[device].joystick_data |= JOYSTICK_RIGHT;
  122.                               joysticks[device].joystick_data &= ~JOYSTICK_LEFT;
  123.                          }
  124.                          break;
  125.                     case 1:        /* 1st Y Axis */
  126.                          if(e.value < 0) {
  127.                               joysticks[device].joystick_data |= JOYSTICK_UP;
  128.                               joysticks[device].joystick_data &= ~JOYSTICK_DOWN;
  129.                          } else if(e.value == 0) {
  130.                               joysticks[device].joystick_data &= ~(JOYSTICK_UP | JOYSTICK_DOWN);
  131.                          } else {
  132.                               joysticks[device].joystick_data |= JOYSTICK_DOWN;
  133.                               joysticks[device].joystick_data &= ~JOYSTICK_UP;
  134.                          }
  135.                          break;
  136.                     default:
  137.                          /* Ignore */
  138.                          break;
  139.                }
  140.           default: /* Ignore other events */
  141.                break;
  142.      }
  143.      return 1;                          
  144. }
  145.  
  146. /* 
  147.  * Returns positive number (or equal to 0) 
  148.  * number of events processed 
  149.  */
  150. int jlib_update(int device)
  151. {
  152.      struct js_event jsevent;
  153.      int num_events = 0;
  154.  
  155.      if(joysticks[device].joystick_fd < 0)
  156.           return -1;
  157.                
  158.      while(read(joysticks[device].joystick_fd, &jsevent, sizeof(struct js_event)) > 0) {
  159.           if(jlib_handle_event(device, jsevent) < 0) {
  160.                joysticks[device].total_events += num_events;
  161.                return -(num_events+1);
  162.           }
  163.           num_events++;
  164.      }
  165.      joysticks[device].total_events += num_events;
  166.      if(errno != EAGAIN) /* We _really_ did have an error */
  167.           return -1;
  168.      
  169.      return num_events;
  170. }
  171.  
  172. int jlib_check(int device, unsigned long val)
  173. {
  174.      if(joysticks[device].joystick_fd < 0)
  175.           return -1;
  176.      
  177.      if(joysticks[device].joystick_data & val)
  178.           return 1;
  179.      else
  180.           return 0;
  181. }
  182.  
  183. int jlib_getmaxbuttons(int device)
  184. {
  185.      if(joysticks[device].joystick_fd < 0)
  186.           return 0;
  187.      else
  188.           return joysticks[device].num_buttons;
  189. }
  190.  
  191. int jlib_getmaxaxes(int device)
  192. {
  193.      if(joysticks[device].joystick_fd < 0)
  194.           return 0;
  195.      else
  196.           return joysticks[device].num_axes;
  197. }
  198.      
  199. #elif defined (WIN32)
  200.  
  201. //////////////////////////////////////////////////////////////////////////////
  202.  
  203. #define STRICT
  204. #define WIN32_LEAN_AND_MEAN
  205. #include <windows.h>
  206. #include <mmsystem.h>
  207. #pragma comment(lib, "winmm")
  208. #include <stdio.h>
  209.  
  210. #include "jlib.h"
  211.  
  212. jlib_joystick joysticks[4] = 
  213. {
  214.      { -1, 0, 0, 0, 0x00000000 },
  215.      { -1, 0, 0, 0, 0x00000000 },
  216.      { -1, 0, 0, 0, 0x00000000 },
  217.      { -1, 0, 0, 0, 0x00000000 }
  218. };
  219.  
  220. int jlib_open(int device)
  221. {
  222.      UINT uNumDevs;
  223.      MMRESULT mmr;
  224.      JOYINFO ji;
  225.  
  226. #if 1
  227.      // TODO: joyGetPos is painfully slow!!!
  228.      return -1;
  229. #endif
  230.  
  231.      if(device < 0 && device > 1) {
  232.           return -1;
  233.      }
  234.  
  235.      if(joysticks[device].joystick_fd >= 0) {
  236.           return -2;
  237.      }
  238.  
  239.      uNumDevs = joyGetNumDevs();
  240.      if(uNumDevs == 0) {
  241.           return -1;
  242.      }
  243.  
  244.      if(uNumDevs  < device + 1) {
  245.           return -1;
  246.      }
  247.  
  248.      joysticks[device].joystick_fd = (device == 0) ? JOYSTICKID1 : JOYSTICKID2;
  249.  
  250.      mmr = joyGetPos(joysticks[device].joystick_fd, &ji);
  251.      if (mmr == JOYERR_UNPLUGGED) {
  252.           printf("Joystick #%d is not connected!\n", device + 1);
  253.           return -3;
  254.      }
  255.  
  256.      joysticks[device].num_buttons = jlib_getmaxbuttons(joysticks[device].joystick_fd);
  257.  
  258.      return joysticks[device].joystick_fd;
  259. }
  260.  
  261. int jlib_close(int device)
  262. {
  263.      if (joysticks[device].joystick_fd < 0) {
  264.           return 0;
  265.      }
  266.  
  267.      joysticks[device].joystick_fd = -1;
  268.  
  269.      return -1;
  270. }
  271.  
  272. static int jlib_handle_event(int device, JOYINFO* pji)
  273. {
  274.      static BOOL fB1Pressed = FALSE;
  275.      static BOOL fB2Pressed = FALSE;
  276.      static BOOL fB3Pressed = FALSE;
  277.      static BOOL fB4Pressed = FALSE;
  278.  
  279.      if(pji->wButtons & JOY_BUTTON1) {
  280.           if(!fB1Pressed) {
  281.                fB1Pressed = TRUE;
  282.                joysticks[device].joystick_data |= JOYSTICK_BUTTON(0);
  283.           }
  284.      } else {
  285.           if(fB1Pressed) {
  286.                fB1Pressed = FALSE;
  287.                joysticks[device].joystick_data &= ~JOYSTICK_BUTTON(0);
  288.           }
  289.      }
  290.  
  291.      if(pji->wButtons & JOY_BUTTON2) {
  292.           if(!fB2Pressed) {
  293.                fB2Pressed = TRUE;
  294.                joysticks[device].joystick_data |= JOYSTICK_BUTTON(1);
  295.           }
  296.      } else {
  297.           if (fB2Pressed) {
  298.                fB2Pressed = FALSE;
  299.                joysticks[device].joystick_data &= ~JOYSTICK_BUTTON(1);
  300.           }
  301.      }
  302.  
  303.  
  304.      if(pji->wButtons & JOY_BUTTON3) {
  305.           if(!fB3Pressed) {
  306.                fB3Pressed = TRUE;
  307.                joysticks[device].joystick_data |= JOYSTICK_BUTTON(2);
  308.           }
  309.      } else {
  310.           if(fB3Pressed) {
  311.                fB3Pressed = FALSE;
  312.                joysticks[device].joystick_data &= ~JOYSTICK_BUTTON(2);
  313.           }
  314.      }
  315.  
  316.      if(pji->wButtons & JOY_BUTTON4) {
  317.           if(!fB4Pressed) {
  318.                fB4Pressed = TRUE;
  319.                joysticks[device].joystick_data |= JOYSTICK_BUTTON(3);
  320.           }
  321.      } else {
  322.           if(fB4Pressed) {
  323.                fB4Pressed = FALSE;
  324.                joysticks[device].joystick_data &= ~JOYSTICK_BUTTON(3);
  325.           }
  326.      }
  327.  
  328.  
  329. #if 0
  330.      case JS_EVENT_AXIS:
  331.           switch(e.number)  {
  332.                case 0:   /* 1st X Axis */
  333.                     if(e.value < 0) {
  334.                          joysticks[device].joystick_data |= JOYSTICK_LEFT;
  335.                          joysticks[device].joystick_data &= ~JOYSTICK_RIGHT;
  336.                     } else if(e.value == 0) {
  337.                          joysticks[device].joystick_data &= ~(JOYSTICK_LEFT | JOYSTICK_RIGHT);
  338.                     } else {
  339.                          joysticks[device].joystick_data |= JOYSTICK_RIGHT;
  340.                          joysticks[device].joystick_data &= ~JOYSTICK_LEFT;
  341.                     }
  342.                     break;
  343.                case 1:        /* 1st Y Axis */
  344.                     if(e.value < 0) {
  345.                          joysticks[device].joystick_data |= JOYSTICK_UP;
  346.                          joysticks[device].joystick_data &= ~JOYSTICK_DOWN;
  347.                     } else if(e.value == 0) {
  348.                          joysticks[device].joystick_data &= ~(JOYSTICK_UP | JOYSTICK_DOWN);
  349.                     } else {
  350.                          joysticks[device].joystick_data |= JOYSTICK_DOWN;
  351.                          joysticks[device].joystick_data &= ~JOYSTICK_UP;
  352.                     }
  353.                     break;
  354.           }
  355.      }
  356. #endif
  357.  
  358.      return 1;
  359. }
  360.  
  361. int jlib_update(int device)
  362. {
  363.      MMRESULT mmr;
  364.      JOYINFO ji;
  365.  
  366.      if(joysticks[device].joystick_fd < 0) {
  367.           return -1;
  368.      }
  369.  
  370.      mmr = joyGetPos(joysticks[device].joystick_fd, &ji);
  371.      if(mmr != JOYERR_NOERROR) {
  372.           return -1;
  373.      }
  374.  
  375.      if (jlib_handle_event(device, &ji) < 0) {
  376.           return -1;
  377.      }
  378.  
  379.      return 1;
  380. }
  381.  
  382. int jlib_check(int device, unsigned long event)
  383. {
  384.      if(joysticks[device].joystick_fd < 0) {
  385.           return -1;
  386.      }
  387.      
  388.      if(joysticks[device].joystick_data & event) {
  389.           return 1;
  390.      }
  391.  
  392.      return 0;
  393. }
  394.  
  395. int jlib_getmaxbuttons(int device)
  396. {
  397.      MMRESULT mmr;
  398.      JOYCAPS jc;
  399.  
  400.      mmr = joyGetDevCaps(joysticks[device].joystick_fd, &jc, sizeof(jc));
  401.      if(mmr != JOYERR_NOERROR) {
  402.           return -1;
  403.      }
  404.  
  405.      return jc.wNumButtons;
  406. }
  407.  
  408. int jlib_getmaxaxes(int device)
  409. {
  410.      MMRESULT mmr;
  411.      JOYCAPS jc;
  412.  
  413.      mmr = joyGetDevCaps(joysticks[device].joystick_fd, &jc, sizeof(jc));
  414.      if (mmr != JOYERR_NOERROR) {
  415.           return -1;
  416.      }
  417.  
  418.      return jc.wMaxAxes;
  419. }
  420.  
  421. //////////////////////////////////////////////////////////////////////////////
  422.  
  423. #else
  424.  
  425. /* Returns: >=0 on success   <0 on failure */
  426. int jlib_open(int o)
  427. {
  428.      return -1;
  429. }
  430.  
  431. int jlib_close(int c)
  432. {
  433.      return -1;
  434. }
  435.  
  436. int jlib_update(int u)
  437. {
  438.      return -1;
  439. }
  440.  
  441. int jlib_check(int device, unsigned long val)
  442. {
  443.      return -1;
  444. }
  445.  
  446. int jlib_getmaxbuttons(int device)
  447. {
  448.      return -1;
  449. }
  450.  
  451. int jlib_getmaxaxes(int a)
  452. {
  453.      return -1;
  454. }
  455.  
  456. #endif
  457.