src/joystick/SDL_joystick.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 14 Jul 2013 18:17:28 -0700
changeset 7523 9e9ab1dc3811
parent 7500 6b484ff38f86
child 7662 d31efd717578
permissions -rw-r--r--
Fixed bug 1919 - Window icon disappears as soon as a renderer is created

Sebastian

Setting a window icon works just fine until a renderer is added to the window.
After adding the renderer the icon disappears.

Reproduce by:
- Take the example code from the wiki: http://wiki.libsdl.org/moin.fcg/SDL_SetWindowIcon

- Add the following two lines after SDL_FreeSurface(surface);
SDL_Delay(1000);
SDL_Renderer* ren = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

-compile and run

You will see the window icon correctly at first. After the Delay the Icon will disappear.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 /* This is the joystick API for Simple DirectMedia Layer */
    24 
    25 #include "SDL.h"
    26 #include "SDL_events.h"
    27 #include "SDL_sysjoystick.h"
    28 #include "SDL_assert.h"
    29 #include "SDL_hints.h"
    30 
    31 #if !SDL_EVENTS_DISABLED
    32 #include "../events/SDL_events_c.h"
    33 #endif
    34 
    35 static SDL_bool SDL_joystick_allows_background_events = SDL_FALSE;
    36 static SDL_Joystick *SDL_joysticks = NULL;
    37 static SDL_Joystick *SDL_updating_joystick = NULL;
    38 
    39 static void
    40 SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
    41 {
    42     if (hint && *hint == '1') {
    43         SDL_joystick_allows_background_events = SDL_TRUE;
    44     } else {
    45         SDL_joystick_allows_background_events = SDL_FALSE;
    46     }
    47 }
    48 
    49 int
    50 SDL_JoystickInit(void)
    51 {
    52     int status;
    53 
    54     /* See if we should allow joystick events while in the background */
    55     SDL_AddHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
    56                         SDL_JoystickAllowBackgroundEventsChanged, NULL);
    57 
    58 #if !SDL_EVENTS_DISABLED
    59     if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
    60         return -1;
    61     }
    62 #endif /* !SDL_EVENTS_DISABLED */
    63 
    64     status = SDL_SYS_JoystickInit();
    65     if (status >= 0) {
    66         status = 0;
    67     }
    68     return (status);
    69 }
    70 
    71 /*
    72  * Count the number of joysticks attached to the system
    73  */
    74 int
    75 SDL_NumJoysticks(void)
    76 {
    77     return SDL_SYS_NumJoysticks();
    78 }
    79 
    80 /*
    81  * Get the implementation dependent name of a joystick
    82  */
    83 const char *
    84 SDL_JoystickNameForIndex(int device_index)
    85 {
    86     if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
    87         SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
    88         return (NULL);
    89     }
    90     return (SDL_SYS_JoystickNameForDeviceIndex(device_index));
    91 }
    92 
    93 /*
    94  * Open a joystick for use - the index passed as an argument refers to
    95  * the N'th joystick on the system.  This index is the value which will
    96  * identify this joystick in future joystick events.
    97  *
    98  * This function returns a joystick identifier, or NULL if an error occurred.
    99  */
   100 SDL_Joystick *
   101 SDL_JoystickOpen(int device_index)
   102 {
   103     SDL_Joystick *joystick;
   104     SDL_Joystick *joysticklist;
   105     const char *joystickname = NULL;
   106 
   107     if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
   108         SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
   109         return (NULL);
   110     }
   111 
   112     joysticklist = SDL_joysticks;
   113     /* If the joystick is already open, return it
   114     * it is important that we have a single joystick * for each instance id
   115     */
   116     while ( joysticklist )
   117     {
   118         if ( SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == joysticklist->instance_id ) {
   119                 joystick = joysticklist;
   120                 ++joystick->ref_count;
   121                 return (joystick);
   122         }
   123         joysticklist = joysticklist->next;
   124     }
   125 
   126     /* Create and initialize the joystick */
   127     joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick));
   128     if (joystick == NULL) {
   129         SDL_OutOfMemory();
   130         return NULL;
   131     }
   132 
   133     SDL_memset(joystick, 0, (sizeof *joystick));
   134     if (SDL_SYS_JoystickOpen(joystick, device_index) < 0) {
   135         SDL_free(joystick);
   136         return NULL;
   137     }
   138 
   139     joystickname = SDL_SYS_JoystickNameForDeviceIndex( device_index );
   140     if ( joystickname )
   141         joystick->name = SDL_strdup( joystickname );
   142     else
   143         joystick->name = NULL;
   144 
   145     if (joystick->naxes > 0) {
   146         joystick->axes = (Sint16 *) SDL_malloc
   147             (joystick->naxes * sizeof(Sint16));
   148     }
   149     if (joystick->nhats > 0) {
   150         joystick->hats = (Uint8 *) SDL_malloc
   151             (joystick->nhats * sizeof(Uint8));
   152     }
   153     if (joystick->nballs > 0) {
   154         joystick->balls = (struct balldelta *) SDL_malloc
   155             (joystick->nballs * sizeof(*joystick->balls));
   156     }
   157     if (joystick->nbuttons > 0) {
   158         joystick->buttons = (Uint8 *) SDL_malloc
   159             (joystick->nbuttons * sizeof(Uint8));
   160     }
   161     if (((joystick->naxes > 0) && !joystick->axes)
   162         || ((joystick->nhats > 0) && !joystick->hats)
   163         || ((joystick->nballs > 0) && !joystick->balls)
   164         || ((joystick->nbuttons > 0) && !joystick->buttons)) {
   165         SDL_OutOfMemory();
   166         SDL_JoystickClose(joystick);
   167         return NULL;
   168     }
   169     if (joystick->axes) {
   170         SDL_memset(joystick->axes, 0, joystick->naxes * sizeof(Sint16));
   171     }
   172     if (joystick->hats) {
   173         SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8));
   174     }
   175     if (joystick->balls) {
   176         SDL_memset(joystick->balls, 0,
   177             joystick->nballs * sizeof(*joystick->balls));
   178     }
   179     if (joystick->buttons) {
   180         SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8));
   181     }
   182 
   183     /* Add joystick to list */
   184     ++joystick->ref_count;
   185     /* Link the joystick in the list */
   186     joystick->next = SDL_joysticks;
   187     SDL_joysticks = joystick;
   188 
   189     SDL_SYS_JoystickUpdate( joystick );
   190 
   191     return (joystick);
   192 }
   193 
   194 
   195 /*
   196  * Checks to make sure the joystick is valid.
   197  */
   198 int
   199 SDL_PrivateJoystickValid(SDL_Joystick * joystick)
   200 {
   201     int valid;
   202 
   203     if ( joystick == NULL ) {
   204         SDL_SetError("Joystick hasn't been opened yet");
   205         valid = 0;
   206     } else {
   207         valid = 1;
   208     }
   209 
   210     if ( joystick && joystick->closed )
   211     {
   212         valid = 0;
   213     }
   214 
   215     return valid;
   216 }
   217 
   218 /*
   219  * Get the number of multi-dimensional axis controls on a joystick
   220  */
   221 int
   222 SDL_JoystickNumAxes(SDL_Joystick * joystick)
   223 {
   224     if (!SDL_PrivateJoystickValid(joystick)) {
   225         return (-1);
   226     }
   227     return (joystick->naxes);
   228 }
   229 
   230 /*
   231  * Get the number of hats on a joystick
   232  */
   233 int
   234 SDL_JoystickNumHats(SDL_Joystick * joystick)
   235 {
   236     if (!SDL_PrivateJoystickValid(joystick)) {
   237         return (-1);
   238     }
   239     return (joystick->nhats);
   240 }
   241 
   242 /*
   243  * Get the number of trackballs on a joystick
   244  */
   245 int
   246 SDL_JoystickNumBalls(SDL_Joystick * joystick)
   247 {
   248     if (!SDL_PrivateJoystickValid(joystick)) {
   249         return (-1);
   250     }
   251     return (joystick->nballs);
   252 }
   253 
   254 /*
   255  * Get the number of buttons on a joystick
   256  */
   257 int
   258 SDL_JoystickNumButtons(SDL_Joystick * joystick)
   259 {
   260     if (!SDL_PrivateJoystickValid(joystick)) {
   261         return (-1);
   262     }
   263     return (joystick->nbuttons);
   264 }
   265 
   266 /*
   267  * Get the current state of an axis control on a joystick
   268  */
   269 Sint16
   270 SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
   271 {
   272     Sint16 state;
   273 
   274     if (!SDL_PrivateJoystickValid(joystick)) {
   275         return (0);
   276     }
   277     if (axis < joystick->naxes) {
   278         state = joystick->axes[axis];
   279     } else {
   280         SDL_SetError("Joystick only has %d axes", joystick->naxes);
   281         state = 0;
   282     }
   283     return (state);
   284 }
   285 
   286 /*
   287  * Get the current state of a hat on a joystick
   288  */
   289 Uint8
   290 SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
   291 {
   292     Uint8 state;
   293 
   294     if (!SDL_PrivateJoystickValid(joystick)) {
   295         return (0);
   296     }
   297     if (hat < joystick->nhats) {
   298         state = joystick->hats[hat];
   299     } else {
   300         SDL_SetError("Joystick only has %d hats", joystick->nhats);
   301         state = 0;
   302     }
   303     return (state);
   304 }
   305 
   306 /*
   307  * Get the ball axis change since the last poll
   308  */
   309 int
   310 SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
   311 {
   312     int retval;
   313 
   314     if (!SDL_PrivateJoystickValid(joystick)) {
   315         return (-1);
   316     }
   317 
   318     retval = 0;
   319     if (ball < joystick->nballs) {
   320         if (dx) {
   321             *dx = joystick->balls[ball].dx;
   322         }
   323         if (dy) {
   324             *dy = joystick->balls[ball].dy;
   325         }
   326         joystick->balls[ball].dx = 0;
   327         joystick->balls[ball].dy = 0;
   328     } else {
   329         return SDL_SetError("Joystick only has %d balls", joystick->nballs);
   330     }
   331     return (retval);
   332 }
   333 
   334 /*
   335  * Get the current state of a button on a joystick
   336  */
   337 Uint8
   338 SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
   339 {
   340     Uint8 state;
   341 
   342     if (!SDL_PrivateJoystickValid(joystick)) {
   343         return (0);
   344     }
   345     if (button < joystick->nbuttons) {
   346         state = joystick->buttons[button];
   347     } else {
   348         SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
   349         state = 0;
   350     }
   351     return (state);
   352 }
   353 
   354 /*
   355  * Return if the joystick in question is currently attached to the system,
   356  *  \return SDL_FALSE if not plugged in, SDL_TRUE if still present.
   357  */
   358 SDL_bool
   359 SDL_JoystickGetAttached(SDL_Joystick * joystick)
   360 {
   361     if (!SDL_PrivateJoystickValid(joystick)) {
   362         return SDL_FALSE;
   363     }
   364 
   365     return SDL_SYS_JoystickAttached(joystick);
   366 }
   367 
   368 /*
   369  * Get the instance id for this opened joystick
   370  */
   371 SDL_JoystickID
   372 SDL_JoystickInstanceID(SDL_Joystick * joystick)
   373 {
   374     if (!SDL_PrivateJoystickValid(joystick)) {
   375         return (-1);
   376     }
   377 
   378     return (joystick->instance_id);
   379 }
   380 
   381 /*
   382  * Get the friendly name of this joystick
   383  */
   384 const char *
   385 SDL_JoystickName(SDL_Joystick * joystick)
   386 {
   387     if (!SDL_PrivateJoystickValid(joystick)) {
   388         return (NULL);
   389     }
   390 
   391     return (joystick->name);
   392 }
   393 
   394 /*
   395  * Close a joystick previously opened with SDL_JoystickOpen()
   396  */
   397 void
   398 SDL_JoystickClose(SDL_Joystick * joystick)
   399 {
   400     SDL_Joystick *joysticklist;
   401     SDL_Joystick *joysticklistprev;
   402 
   403     if (!joystick) {
   404         return;
   405     }
   406 
   407     /* First decrement ref count */
   408     if (--joystick->ref_count > 0) {
   409         return;
   410     }
   411 
   412     if (joystick == SDL_updating_joystick) {
   413         return;
   414     }
   415 
   416     SDL_SYS_JoystickClose(joystick);
   417 
   418     joysticklist = SDL_joysticks;
   419     joysticklistprev = NULL;
   420     while ( joysticklist )
   421     {
   422         if (joystick == joysticklist)
   423         {
   424             if ( joysticklistprev )
   425             {
   426                 /* unlink this entry */
   427                 joysticklistprev->next = joysticklist->next;
   428             }
   429             else
   430             {
   431                 SDL_joysticks = joystick->next;
   432             }
   433 
   434             break;
   435         }
   436         joysticklistprev = joysticklist;
   437         joysticklist = joysticklist->next;
   438     }
   439 
   440     if (joystick->name)
   441         SDL_free(joystick->name);
   442 
   443     /* Free the data associated with this joystick */
   444     if (joystick->axes) {
   445         SDL_free(joystick->axes);
   446     }
   447     if (joystick->hats) {
   448         SDL_free(joystick->hats);
   449     }
   450     if (joystick->balls) {
   451         SDL_free(joystick->balls);
   452     }
   453     if (joystick->buttons) {
   454         SDL_free(joystick->buttons);
   455     }
   456     SDL_free(joystick);
   457 }
   458 
   459 void
   460 SDL_JoystickQuit(void)
   461 {
   462     /* Make sure we're not getting called in the middle of updating joysticks */
   463     SDL_assert(!SDL_updating_joystick);
   464 
   465     /* Stop the event polling */
   466     while ( SDL_joysticks )
   467     {
   468         SDL_joysticks->ref_count = 1;
   469         SDL_JoystickClose(SDL_joysticks);
   470     }
   471 
   472     /* Quit the joystick setup */
   473     SDL_SYS_JoystickQuit();
   474 
   475 #if !SDL_EVENTS_DISABLED
   476     SDL_QuitSubSystem(SDL_INIT_EVENTS);
   477 #endif
   478 }
   479 
   480 
   481 static SDL_bool
   482 SDL_PrivateJoystickShouldIgnoreEvent()
   483 {
   484     if (SDL_joystick_allows_background_events)
   485     {
   486         return SDL_FALSE;
   487     }
   488 
   489     if (SDL_WasInit(SDL_INIT_VIDEO)) {
   490         if (SDL_GetKeyboardFocus() == NULL) {
   491             // Video is initialized and we don't have focus, ignore the event.
   492             return SDL_TRUE;
   493         } else {
   494             return SDL_FALSE;
   495         }
   496     }
   497 
   498     // Video subsystem wasn't initialized, always allow the event
   499     return SDL_FALSE;
   500 }
   501 
   502 /* These are global for SDL_sysjoystick.c and SDL_events.c */
   503 
   504 int
   505 SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
   506 {
   507     int posted;
   508 
   509     /* Make sure we're not getting garbage events */
   510     if (axis >= joystick->naxes) {
   511         return 0;
   512     }
   513 
   514     /* Update internal joystick state */
   515     if (value == joystick->axes[axis]) {
   516         return 0;
   517     }
   518     joystick->axes[axis] = value;
   519 
   520     /* We ignore events if we don't have keyboard focus, except for centering
   521      * events.
   522      */
   523     if (SDL_PrivateJoystickShouldIgnoreEvent()) {
   524         if (!(joystick->closed && joystick->uncentered)) {
   525             return 0;
   526         }
   527     }
   528 
   529     /* Post the event, if desired */
   530     posted = 0;
   531 #if !SDL_EVENTS_DISABLED
   532     if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
   533         SDL_Event event;
   534         event.type = SDL_JOYAXISMOTION;
   535         event.jaxis.which = joystick->instance_id;
   536         event.jaxis.axis = axis;
   537         event.jaxis.value = value;
   538         posted = SDL_PushEvent(&event) == 1;
   539     }
   540 #endif /* !SDL_EVENTS_DISABLED */
   541     return (posted);
   542 }
   543 
   544 int
   545 SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
   546 {
   547     int posted;
   548 
   549     /* Make sure we're not getting garbage events */
   550     if (hat >= joystick->nhats) {
   551         return 0;
   552     }
   553 
   554     /* Update internal joystick state */
   555     joystick->hats[hat] = value;
   556 
   557     /* We ignore events if we don't have keyboard focus, except for centering
   558      * events.
   559      */
   560     if (SDL_PrivateJoystickShouldIgnoreEvent()) {
   561         if (!(joystick->closed && joystick->uncentered)) {
   562             return 0;
   563         }
   564     }
   565 
   566 
   567     /* Post the event, if desired */
   568     posted = 0;
   569 #if !SDL_EVENTS_DISABLED
   570     if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
   571         SDL_Event event;
   572         event.jhat.type = SDL_JOYHATMOTION;
   573         event.jhat.which = joystick->instance_id;
   574         event.jhat.hat = hat;
   575         event.jhat.value = value;
   576         posted = SDL_PushEvent(&event) == 1;
   577     }
   578 #endif /* !SDL_EVENTS_DISABLED */
   579     return (posted);
   580 }
   581 
   582 int
   583 SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
   584                         Sint16 xrel, Sint16 yrel)
   585 {
   586     int posted;
   587 
   588     /* Make sure we're not getting garbage events */
   589     if (ball >= joystick->nballs) {
   590         return 0;
   591     }
   592 
   593     /* We ignore events if we don't have keyboard focus. */
   594     if (SDL_PrivateJoystickShouldIgnoreEvent()) {
   595         return 0;
   596     }
   597 
   598     /* Update internal mouse state */
   599     joystick->balls[ball].dx += xrel;
   600     joystick->balls[ball].dy += yrel;
   601 
   602     /* Post the event, if desired */
   603     posted = 0;
   604 #if !SDL_EVENTS_DISABLED
   605     if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
   606         SDL_Event event;
   607         event.jball.type = SDL_JOYBALLMOTION;
   608         event.jball.which = joystick->instance_id;
   609         event.jball.ball = ball;
   610         event.jball.xrel = xrel;
   611         event.jball.yrel = yrel;
   612         posted = SDL_PushEvent(&event) == 1;
   613     }
   614 #endif /* !SDL_EVENTS_DISABLED */
   615     return (posted);
   616 }
   617 
   618 int
   619 SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
   620 {
   621     int posted;
   622 #if !SDL_EVENTS_DISABLED
   623     SDL_Event event;
   624 
   625     switch (state) {
   626     case SDL_PRESSED:
   627         event.type = SDL_JOYBUTTONDOWN;
   628         break;
   629     case SDL_RELEASED:
   630         event.type = SDL_JOYBUTTONUP;
   631         break;
   632     default:
   633         /* Invalid state -- bail */
   634         return (0);
   635     }
   636 #endif /* !SDL_EVENTS_DISABLED */
   637 
   638     /* Make sure we're not getting garbage events */
   639     if (button >= joystick->nbuttons) {
   640         return 0;
   641     }
   642 
   643     /* We ignore events if we don't have keyboard focus, except for button
   644      * release. */
   645     if (state == SDL_PRESSED && SDL_PrivateJoystickShouldIgnoreEvent()) {
   646         return 0;
   647     }
   648 
   649     /* Update internal joystick state */
   650     joystick->buttons[button] = state;
   651 
   652     /* Post the event, if desired */
   653     posted = 0;
   654 #if !SDL_EVENTS_DISABLED
   655     if (SDL_GetEventState(event.type) == SDL_ENABLE) {
   656         event.jbutton.which = joystick->instance_id;
   657         event.jbutton.button = button;
   658         event.jbutton.state = state;
   659         posted = SDL_PushEvent(&event) == 1;
   660     }
   661 #endif /* !SDL_EVENTS_DISABLED */
   662     return (posted);
   663 }
   664 
   665 void
   666 SDL_JoystickUpdate(void)
   667 {
   668     SDL_Joystick *joystick;
   669 
   670     joystick = SDL_joysticks;
   671     while ( joystick )
   672     {
   673         SDL_Joystick *joysticknext;
   674         /* save off the next pointer, the Update call may cause a joystick removed event
   675          * and cause our joystick pointer to be freed
   676          */
   677         joysticknext = joystick->next;
   678 
   679         SDL_updating_joystick = joystick;
   680 
   681         SDL_SYS_JoystickUpdate( joystick );
   682 
   683         if ( joystick->closed && joystick->uncentered )
   684         {
   685             int i;
   686 
   687             /* Tell the app that everything is centered/unpressed...  */
   688             for (i = 0; i < joystick->naxes; i++)
   689                 SDL_PrivateJoystickAxis(joystick, i, 0);
   690 
   691             for (i = 0; i < joystick->nbuttons; i++)
   692                 SDL_PrivateJoystickButton(joystick, i, 0);
   693 
   694             for (i = 0; i < joystick->nhats; i++)
   695                 SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
   696 
   697             joystick->uncentered = 0;
   698         }
   699 
   700         SDL_updating_joystick = NULL;
   701 
   702         /* If the joystick was closed while updating, free it here */
   703         if ( joystick->ref_count <= 0 ) {
   704             SDL_JoystickClose(joystick);
   705         }
   706 
   707         joystick = joysticknext;
   708     }
   709 
   710     /* this needs to happen AFTER walking the joystick list above, so that any
   711        dangling hardware data from removed devices can be free'd
   712      */
   713     SDL_SYS_JoystickDetect();
   714 }
   715 
   716 int
   717 SDL_JoystickEventState(int state)
   718 {
   719 #if SDL_EVENTS_DISABLED
   720     return SDL_DISABLE;
   721 #else
   722     const Uint32 event_list[] = {
   723         SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
   724         SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED
   725     };
   726     unsigned int i;
   727 
   728     switch (state) {
   729     case SDL_QUERY:
   730         state = SDL_DISABLE;
   731         for (i = 0; i < SDL_arraysize(event_list); ++i) {
   732             state = SDL_EventState(event_list[i], SDL_QUERY);
   733             if (state == SDL_ENABLE) {
   734                 break;
   735             }
   736         }
   737         break;
   738     default:
   739         for (i = 0; i < SDL_arraysize(event_list); ++i) {
   740             SDL_EventState(event_list[i], state);
   741         }
   742         break;
   743     }
   744     return (state);
   745 #endif /* SDL_EVENTS_DISABLED */
   746 }
   747 
   748 /* return 1 if you want to run the joystick update loop this frame, used by hotplug support */
   749 SDL_bool
   750 SDL_PrivateJoystickNeedsPolling()
   751 {
   752     if (SDL_joysticks != NULL) {
   753         return SDL_TRUE;
   754     } else {
   755         return SDL_SYS_JoystickNeedsPolling();
   756     }
   757 }
   758 
   759 
   760 /* return the guid for this index */
   761 SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
   762 {
   763     if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
   764         SDL_JoystickGUID emptyGUID;
   765         SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
   766         SDL_zero( emptyGUID );
   767         return emptyGUID;
   768     }
   769     return SDL_SYS_JoystickGetDeviceGUID( device_index );
   770 }
   771 
   772 /* return the guid for this opened device */
   773 SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
   774 {
   775     return SDL_SYS_JoystickGetGUID( joystick );
   776 }
   777 
   778 /* convert the guid to a printable string */
   779 void SDL_JoystickGetGUIDString( SDL_JoystickGUID guid, char *pszGUID, int cbGUID )
   780 {
   781     static const char k_rgchHexToASCII[] = "0123456789abcdef";
   782     int i;
   783 
   784     if ((pszGUID == NULL) || (cbGUID <= 0)) {
   785         return;
   786     }
   787 
   788     for ( i = 0; i < sizeof(guid.data) && i < (cbGUID-1); i++ )
   789     {
   790         /* each input byte writes 2 ascii chars, and might write a null byte. */
   791         /* If we don't have room for next input byte, stop */
   792         unsigned char c = guid.data[i];
   793 
   794         *pszGUID++ = k_rgchHexToASCII[ c >> 4 ];
   795         *pszGUID++ = k_rgchHexToASCII[ c & 0x0F ];
   796     }
   797     *pszGUID = '\0';
   798 }
   799 
   800 
   801 /*-----------------------------------------------------------------------------
   802  * Purpose: Returns the 4 bit nibble for a hex character
   803  * Input  : c -
   804  * Output : unsigned char
   805  *-----------------------------------------------------------------------------*/
   806 static unsigned char nibble( char c )
   807 {
   808     if ( ( c >= '0' ) &&
   809         ( c <= '9' ) )
   810     {
   811         return (unsigned char)(c - '0');
   812     }
   813 
   814     if ( ( c >= 'A' ) &&
   815         ( c <= 'F' ) )
   816     {
   817         return (unsigned char)(c - 'A' + 0x0a);
   818     }
   819 
   820     if ( ( c >= 'a' ) &&
   821         ( c <= 'f' ) )
   822     {
   823         return (unsigned char)(c - 'a' + 0x0a);
   824     }
   825 
   826     /* received an invalid character, and no real way to return an error */
   827     /* AssertMsg1( false, "Q_nibble invalid hex character '%c' ", c ); */
   828     return 0;
   829 }
   830 
   831 
   832 /* convert the string version of a joystick guid to the struct */
   833 SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
   834 {
   835     SDL_JoystickGUID guid;
   836     int maxoutputbytes= sizeof(guid);
   837     int len = SDL_strlen( pchGUID );
   838     Uint8 *p;
   839     int i;
   840 
   841     /* Make sure it's even */
   842     len = ( len ) & ~0x1;
   843 
   844     SDL_memset( &guid, 0x00, sizeof(guid) );
   845 
   846     p = (Uint8 *)&guid;
   847     for ( i = 0;
   848         ( i < len ) && ( ( p - (Uint8 *)&guid ) < maxoutputbytes );
   849         i+=2, p++ )
   850     {
   851         *p = ( nibble( pchGUID[i] ) << 4 ) | nibble( pchGUID[i+1] );
   852     }
   853 
   854     return guid;
   855 }
   856 
   857 
   858 /* vi: set ts=4 sw=4 expandtab: */