src/joystick/SDL_joystick.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Dec 2009 08:11:06 +0000
changeset 3565 f43c8f688f77
parent 3369 71bb88152e3f
child 3608 a5a37f850d83
permissions -rw-r--r--
Fixed bug #906

Added better error reporting for OpenGL context creation failing.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library 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 GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* This is the joystick API for Simple DirectMedia Layer */
    25 
    26 #include "SDL_events.h"
    27 #include "SDL_sysjoystick.h"
    28 #include "SDL_joystick_c.h"
    29 #if !SDL_EVENTS_DISABLED
    30 #include "../events/SDL_events_c.h"
    31 #endif
    32 
    33 /* This is used for Quake III Arena */
    34 #if SDL_EVENTS_DISABLED
    35 #define SDL_Lock_EventThread()
    36 #define SDL_Unlock_EventThread()
    37 #endif
    38 
    39 Uint8 SDL_numjoysticks = 0;
    40 SDL_Joystick **SDL_joysticks = NULL;
    41 static SDL_Joystick *default_joystick = NULL;
    42 
    43 int
    44 SDL_JoystickInit(void)
    45 {
    46     int arraylen;
    47     int status;
    48 
    49     SDL_numjoysticks = 0;
    50     status = SDL_SYS_JoystickInit();
    51     if (status >= 0) {
    52         arraylen = (status + 1) * sizeof(*SDL_joysticks);
    53         SDL_joysticks = (SDL_Joystick **) SDL_malloc(arraylen);
    54         if (SDL_joysticks == NULL) {
    55             SDL_numjoysticks = 0;
    56         } else {
    57             SDL_memset(SDL_joysticks, 0, arraylen);
    58             SDL_numjoysticks = status;
    59         }
    60         status = 0;
    61     }
    62     default_joystick = NULL;
    63     return (status);
    64 }
    65 
    66 /*
    67  * Count the number of joysticks attached to the system
    68  */
    69 int
    70 SDL_NumJoysticks(void)
    71 {
    72     return SDL_numjoysticks;
    73 }
    74 
    75 /*
    76  * Get the implementation dependent name of a joystick
    77  */
    78 const char *
    79 SDL_JoystickName(int device_index)
    80 {
    81     if ((device_index < 0) || (device_index >= SDL_numjoysticks)) {
    82         SDL_SetError("There are %d joysticks available", SDL_numjoysticks);
    83         return (NULL);
    84     }
    85     return (SDL_SYS_JoystickName(device_index));
    86 }
    87 
    88 /*
    89  * Open a joystick for use - the index passed as an argument refers to
    90  * the N'th joystick on the system.  This index is the value which will
    91  * identify this joystick in future joystick events.
    92  *
    93  * This function returns a joystick identifier, or NULL if an error occurred.
    94  */
    95 SDL_Joystick *
    96 SDL_JoystickOpen(int device_index)
    97 {
    98     int i;
    99     SDL_Joystick *joystick;
   100 
   101     if ((device_index < 0) || (device_index >= SDL_numjoysticks)) {
   102         SDL_SetError("There are %d joysticks available", SDL_numjoysticks);
   103         return (NULL);
   104     }
   105 
   106     /* If the joystick is already open, return it */
   107     for (i = 0; SDL_joysticks[i]; ++i) {
   108         if (device_index == SDL_joysticks[i]->index) {
   109             joystick = SDL_joysticks[i];
   110             ++joystick->ref_count;
   111             return (joystick);
   112         }
   113     }
   114 
   115     /* Create and initialize the joystick */
   116     joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick));
   117     if (joystick != NULL) {
   118         SDL_memset(joystick, 0, (sizeof *joystick));
   119         joystick->index = device_index;
   120         if (SDL_SYS_JoystickOpen(joystick) < 0) {
   121             SDL_free(joystick);
   122             joystick = NULL;
   123         } else {
   124             if (joystick->naxes > 0) {
   125                 joystick->axes = (Sint16 *) SDL_malloc
   126                     (joystick->naxes * sizeof(Sint16));
   127             }
   128             if (joystick->nhats > 0) {
   129                 joystick->hats = (Uint8 *) SDL_malloc
   130                     (joystick->nhats * sizeof(Uint8));
   131             }
   132             if (joystick->nballs > 0) {
   133                 joystick->balls = (struct balldelta *) SDL_malloc
   134                     (joystick->nballs * sizeof(*joystick->balls));
   135             }
   136             if (joystick->nbuttons > 0) {
   137                 joystick->buttons = (Uint8 *) SDL_malloc
   138                     (joystick->nbuttons * sizeof(Uint8));
   139             }
   140             if (((joystick->naxes > 0) && !joystick->axes)
   141                 || ((joystick->nhats > 0) && !joystick->hats)
   142                 || ((joystick->nballs > 0) && !joystick->balls)
   143                 || ((joystick->nbuttons > 0) && !joystick->buttons)) {
   144                 SDL_OutOfMemory();
   145                 SDL_JoystickClose(joystick);
   146                 joystick = NULL;
   147             }
   148             if (joystick->axes) {
   149                 SDL_memset(joystick->axes, 0,
   150                            joystick->naxes * sizeof(Sint16));
   151             }
   152             if (joystick->hats) {
   153                 SDL_memset(joystick->hats, 0,
   154                            joystick->nhats * sizeof(Uint8));
   155             }
   156             if (joystick->balls) {
   157                 SDL_memset(joystick->balls, 0,
   158                            joystick->nballs * sizeof(*joystick->balls));
   159             }
   160             if (joystick->buttons) {
   161                 SDL_memset(joystick->buttons, 0,
   162                            joystick->nbuttons * sizeof(Uint8));
   163             }
   164         }
   165     }
   166     if (joystick) {
   167         /* Add joystick to list */
   168         ++joystick->ref_count;
   169         SDL_Lock_EventThread();
   170         for (i = 0; SDL_joysticks[i]; ++i)
   171             /* Skip to next joystick */ ;
   172         SDL_joysticks[i] = joystick;
   173         SDL_Unlock_EventThread();
   174     }
   175     return (joystick);
   176 }
   177 
   178 /*
   179  * Returns 1 if the joystick has been opened, or 0 if it has not.
   180  */
   181 int
   182 SDL_JoystickOpened(int device_index)
   183 {
   184     int i, opened;
   185 
   186     opened = 0;
   187     for (i = 0; SDL_joysticks[i]; ++i) {
   188         if (SDL_joysticks[i]->index == (Uint8) device_index) {
   189             opened = 1;
   190             break;
   191         }
   192     }
   193     return (opened);
   194 }
   195 
   196 
   197 /*
   198  * Checks to make sure the joystick is valid.
   199  */
   200 int
   201 SDL_PrivateJoystickValid(SDL_Joystick ** joystick)
   202 {
   203     int valid;
   204 
   205     if (*joystick == NULL) {
   206         *joystick = default_joystick;
   207     }
   208     if (*joystick == NULL) {
   209         SDL_SetError("Joystick hasn't been opened yet");
   210         valid = 0;
   211     } else {
   212         valid = 1;
   213     }
   214     return valid;
   215 }
   216 
   217 /*
   218  * Get the device index of an opened joystick.
   219  */
   220 int
   221 SDL_JoystickIndex(SDL_Joystick * joystick)
   222 {
   223     if (!SDL_PrivateJoystickValid(&joystick)) {
   224         return (-1);
   225     }
   226     return (joystick->index);
   227 }
   228 
   229 /*
   230  * Get the number of multi-dimensional axis controls on a joystick
   231  */
   232 int
   233 SDL_JoystickNumAxes(SDL_Joystick * joystick)
   234 {
   235     if (!SDL_PrivateJoystickValid(&joystick)) {
   236         return (-1);
   237     }
   238     return (joystick->naxes);
   239 }
   240 
   241 /*
   242  * Get the number of hats on a joystick
   243  */
   244 int
   245 SDL_JoystickNumHats(SDL_Joystick * joystick)
   246 {
   247     if (!SDL_PrivateJoystickValid(&joystick)) {
   248         return (-1);
   249     }
   250     return (joystick->nhats);
   251 }
   252 
   253 /*
   254  * Get the number of trackballs on a joystick
   255  */
   256 int
   257 SDL_JoystickNumBalls(SDL_Joystick * joystick)
   258 {
   259     if (!SDL_PrivateJoystickValid(&joystick)) {
   260         return (-1);
   261     }
   262     return (joystick->nballs);
   263 }
   264 
   265 /*
   266  * Get the number of buttons on a joystick
   267  */
   268 int
   269 SDL_JoystickNumButtons(SDL_Joystick * joystick)
   270 {
   271     if (!SDL_PrivateJoystickValid(&joystick)) {
   272         return (-1);
   273     }
   274     return (joystick->nbuttons);
   275 }
   276 
   277 /*
   278  * Get the current state of an axis control on a joystick
   279  */
   280 Sint16
   281 SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
   282 {
   283     Sint16 state;
   284 
   285     if (!SDL_PrivateJoystickValid(&joystick)) {
   286         return (0);
   287     }
   288     if (axis < joystick->naxes) {
   289         state = joystick->axes[axis];
   290     } else {
   291         SDL_SetError("Joystick only has %d axes", joystick->naxes);
   292         state = 0;
   293     }
   294     return (state);
   295 }
   296 
   297 /*
   298  * Get the current state of a hat on a joystick
   299  */
   300 Uint8
   301 SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
   302 {
   303     Uint8 state;
   304 
   305     if (!SDL_PrivateJoystickValid(&joystick)) {
   306         return (0);
   307     }
   308     if (hat < joystick->nhats) {
   309         state = joystick->hats[hat];
   310     } else {
   311         SDL_SetError("Joystick only has %d hats", joystick->nhats);
   312         state = 0;
   313     }
   314     return (state);
   315 }
   316 
   317 /*
   318  * Get the ball axis change since the last poll
   319  */
   320 int
   321 SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
   322 {
   323     int retval;
   324 
   325     if (!SDL_PrivateJoystickValid(&joystick)) {
   326         return (-1);
   327     }
   328 
   329     retval = 0;
   330     if (ball < joystick->nballs) {
   331         if (dx) {
   332             *dx = joystick->balls[ball].dx;
   333         }
   334         if (dy) {
   335             *dy = joystick->balls[ball].dy;
   336         }
   337         joystick->balls[ball].dx = 0;
   338         joystick->balls[ball].dy = 0;
   339     } else {
   340         SDL_SetError("Joystick only has %d balls", joystick->nballs);
   341         retval = -1;
   342     }
   343     return (retval);
   344 }
   345 
   346 /*
   347  * Get the current state of a button on a joystick
   348  */
   349 Uint8
   350 SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
   351 {
   352     Uint8 state;
   353 
   354     if (!SDL_PrivateJoystickValid(&joystick)) {
   355         return (0);
   356     }
   357     if (button < joystick->nbuttons) {
   358         state = joystick->buttons[button];
   359     } else {
   360         SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
   361         state = 0;
   362     }
   363     return (state);
   364 }
   365 
   366 /*
   367  * Close a joystick previously opened with SDL_JoystickOpen()
   368  */
   369 void
   370 SDL_JoystickClose(SDL_Joystick * joystick)
   371 {
   372     int i;
   373 
   374     if (!SDL_PrivateJoystickValid(&joystick)) {
   375         return;
   376     }
   377 
   378     /* First decrement ref count */
   379     if (--joystick->ref_count > 0) {
   380         return;
   381     }
   382 
   383     /* Lock the event queue - prevent joystick polling */
   384     SDL_Lock_EventThread();
   385 
   386     if (joystick == default_joystick) {
   387         default_joystick = NULL;
   388     }
   389     SDL_SYS_JoystickClose(joystick);
   390 
   391     /* Remove joystick from list */
   392     for (i = 0; SDL_joysticks[i]; ++i) {
   393         if (joystick == SDL_joysticks[i]) {
   394             SDL_memmove(&SDL_joysticks[i], &SDL_joysticks[i + 1],
   395                         (SDL_numjoysticks - i) * sizeof(joystick));
   396             break;
   397         }
   398     }
   399 
   400     /* Let the event thread keep running */
   401     SDL_Unlock_EventThread();
   402 
   403     /* Free the data associated with this joystick */
   404     if (joystick->axes) {
   405         SDL_free(joystick->axes);
   406     }
   407     if (joystick->hats) {
   408         SDL_free(joystick->hats);
   409     }
   410     if (joystick->balls) {
   411         SDL_free(joystick->balls);
   412     }
   413     if (joystick->buttons) {
   414         SDL_free(joystick->buttons);
   415     }
   416     SDL_free(joystick);
   417 }
   418 
   419 void
   420 SDL_JoystickQuit(void)
   421 {
   422     /* Stop the event polling */
   423     SDL_Lock_EventThread();
   424     SDL_numjoysticks = 0;
   425     SDL_Unlock_EventThread();
   426 
   427     /* Quit the joystick setup */
   428     SDL_SYS_JoystickQuit();
   429     if (SDL_joysticks) {
   430         SDL_free(SDL_joysticks);
   431         SDL_joysticks = NULL;
   432     }
   433 }
   434 
   435 
   436 /* These are global for SDL_sysjoystick.c and SDL_events.c */
   437 
   438 int
   439 SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
   440 {
   441     int posted;
   442 
   443     /* Update internal joystick state */
   444     joystick->axes[axis] = value;
   445 
   446     /* Post the event, if desired */
   447     posted = 0;
   448 #if !SDL_EVENTS_DISABLED
   449     if (SDL_ProcessEvents[SDL_JOYAXISMOTION] == SDL_ENABLE) {
   450         SDL_Event event;
   451         event.type = SDL_JOYAXISMOTION;
   452         event.jaxis.which = joystick->index;
   453         event.jaxis.axis = axis;
   454         event.jaxis.value = value;
   455         if ((SDL_EventOK == NULL)
   456             || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
   457             posted = 1;
   458             SDL_PushEvent(&event);
   459         }
   460     }
   461 #endif /* !SDL_EVENTS_DISABLED */
   462     return (posted);
   463 }
   464 
   465 int
   466 SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
   467 {
   468     int posted;
   469 
   470     /* Update internal joystick state */
   471     joystick->hats[hat] = value;
   472 
   473     /* Post the event, if desired */
   474     posted = 0;
   475 #if !SDL_EVENTS_DISABLED
   476     if (SDL_ProcessEvents[SDL_JOYHATMOTION] == SDL_ENABLE) {
   477         SDL_Event event;
   478         event.jhat.type = SDL_JOYHATMOTION;
   479         event.jhat.which = joystick->index;
   480         event.jhat.hat = hat;
   481         event.jhat.value = value;
   482         if ((SDL_EventOK == NULL)
   483             || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
   484             posted = 1;
   485             SDL_PushEvent(&event);
   486         }
   487     }
   488 #endif /* !SDL_EVENTS_DISABLED */
   489     return (posted);
   490 }
   491 
   492 int
   493 SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
   494                         Sint16 xrel, Sint16 yrel)
   495 {
   496     int posted;
   497 
   498     /* Update internal mouse state */
   499     joystick->balls[ball].dx += xrel;
   500     joystick->balls[ball].dy += yrel;
   501 
   502     /* Post the event, if desired */
   503     posted = 0;
   504 #if !SDL_EVENTS_DISABLED
   505     if (SDL_ProcessEvents[SDL_JOYBALLMOTION] == SDL_ENABLE) {
   506         SDL_Event event;
   507         event.jball.type = SDL_JOYBALLMOTION;
   508         event.jball.which = joystick->index;
   509         event.jball.ball = ball;
   510         event.jball.xrel = xrel;
   511         event.jball.yrel = yrel;
   512         if ((SDL_EventOK == NULL)
   513             || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
   514             posted = 1;
   515             SDL_PushEvent(&event);
   516         }
   517     }
   518 #endif /* !SDL_EVENTS_DISABLED */
   519     return (posted);
   520 }
   521 
   522 int
   523 SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
   524 {
   525     int posted;
   526 #if !SDL_EVENTS_DISABLED
   527     SDL_Event event;
   528 
   529     switch (state) {
   530     case SDL_PRESSED:
   531         event.type = SDL_JOYBUTTONDOWN;
   532         break;
   533     case SDL_RELEASED:
   534         event.type = SDL_JOYBUTTONUP;
   535         break;
   536     default:
   537         /* Invalid state -- bail */
   538         return (0);
   539     }
   540 #endif /* !SDL_EVENTS_DISABLED */
   541 
   542     /* Update internal joystick state */
   543     joystick->buttons[button] = state;
   544 
   545     /* Post the event, if desired */
   546     posted = 0;
   547 #if !SDL_EVENTS_DISABLED
   548     if (SDL_ProcessEvents[event.type] == SDL_ENABLE) {
   549         event.jbutton.which = joystick->index;
   550         event.jbutton.button = button;
   551         event.jbutton.state = state;
   552         if ((SDL_EventOK == NULL)
   553             || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
   554             posted = 1;
   555             SDL_PushEvent(&event);
   556         }
   557     }
   558 #endif /* !SDL_EVENTS_DISABLED */
   559     return (posted);
   560 }
   561 
   562 void
   563 SDL_JoystickUpdate(void)
   564 {
   565     int i;
   566 
   567     for (i = 0; SDL_joysticks[i]; ++i) {
   568         SDL_SYS_JoystickUpdate(SDL_joysticks[i]);
   569     }
   570 }
   571 
   572 int
   573 SDL_JoystickEventState(int state)
   574 {
   575 #if SDL_EVENTS_DISABLED
   576     return SDL_IGNORE;
   577 #else
   578     const Uint8 event_list[] = {
   579         SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
   580         SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP,
   581     };
   582     unsigned int i;
   583 
   584     switch (state) {
   585     case SDL_QUERY:
   586         state = SDL_IGNORE;
   587         for (i = 0; i < SDL_arraysize(event_list); ++i) {
   588             state = SDL_EventState(event_list[i], SDL_QUERY);
   589             if (state == SDL_ENABLE) {
   590                 break;
   591             }
   592         }
   593         break;
   594     default:
   595         for (i = 0; i < SDL_arraysize(event_list); ++i) {
   596             SDL_EventState(event_list[i], state);
   597         }
   598         break;
   599     }
   600     return (state);
   601 #endif /* SDL_EVENTS_DISABLED */
   602 }
   603 
   604 /* vi: set ts=4 sw=4 expandtab: */