Browse Source

SDL_GetKeyboards() follows the SDL_GetStringRule

Sam Lantinga 9 months ago
parent
commit
6ca18ed0e5

+ 5 - 4
include/SDL3/SDL_keyboard.h

@@ -73,9 +73,10 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasKeyboard(void);
  * power buttons, etc. You should wait for input from a device before you
  * consider it actively in use.
  *
- * \param count a pointer filled in with the number of keyboards returned.
- * \returns a 0 terminated array of keyboards instance IDs which should be
- *          freed with SDL_free(), or NULL on failure; call SDL_GetError() for
+ * The returned array follows the SDL_GetStringRule, and will be automatically freed later.
+ *
+ * \param count a pointer filled in with the number of keyboards returned, may be NULL.
+ * \returns a 0 terminated array of keyboards instance IDs or NULL on failure; call SDL_GetError() for
  *          more information.
  *
  * \since This function is available since SDL 3.0.0.
@@ -83,7 +84,7 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasKeyboard(void);
  * \sa SDL_GetKeyboardNameForID
  * \sa SDL_HasKeyboard
  */
-extern SDL_DECLSPEC SDL_KeyboardID * SDLCALL SDL_GetKeyboards(int *count);
+extern SDL_DECLSPEC const SDL_KeyboardID * SDLCALL SDL_GetKeyboards(int *count);
 
 /**
  * Get the name of a keyboard.

+ 1 - 1
src/dynapi/SDL_dynapi_procs.h

@@ -372,7 +372,7 @@ SDL_DYNAPI_PROC(const char*,SDL_GetKeyName,(SDL_Keycode a),(a),return)
 SDL_DYNAPI_PROC(SDL_Window*,SDL_GetKeyboardFocus,(void),(),return)
 SDL_DYNAPI_PROC(const char*,SDL_GetKeyboardNameForID,(SDL_KeyboardID a),(a),return)
 SDL_DYNAPI_PROC(const Uint8*,SDL_GetKeyboardState,(int *a),(a),return)
-SDL_DYNAPI_PROC(SDL_KeyboardID*,SDL_GetKeyboards,(int *a),(a),return)
+SDL_DYNAPI_PROC(const SDL_KeyboardID*,SDL_GetKeyboards,(int *a),(a),return)
 SDL_DYNAPI_PROC(void,SDL_GetLogOutputFunction,(SDL_LogOutputFunction *a, void **b),(a,b),)
 SDL_DYNAPI_PROC(SDL_LogPriority,SDL_GetLogPriority,(int a),(a),return)
 SDL_DYNAPI_PROC(int,SDL_GetMasksForPixelFormat,(SDL_PixelFormat a, int *b, Uint32 *c, Uint32 *d, Uint32 *e, Uint32 *f),(a,b,c,d,e,f),return)

+ 2 - 2
src/events/SDL_keyboard.c

@@ -177,7 +177,7 @@ SDL_bool SDL_HasKeyboard(void)
     return (SDL_keyboard_count > 0);
 }
 
-SDL_KeyboardID *SDL_GetKeyboards(int *count)
+const SDL_KeyboardID *SDL_GetKeyboards(int *count)
 {
     int i;
     SDL_KeyboardID *keyboards;
@@ -198,7 +198,7 @@ SDL_KeyboardID *SDL_GetKeyboards(int *count)
         }
     }
 
-    return keyboards;
+    return SDL_FreeLater(keyboards);
 }
 
 const char *SDL_GetKeyboardNameForID(SDL_KeyboardID instance_id)

+ 2 - 3
src/video/windows/SDL_windowsevents.c

@@ -824,7 +824,7 @@ static void AddDeviceID(Uint32 deviceID, Uint32 **list, int *count)
     *list = new_list;
 }
 
-static SDL_bool HasDeviceID(Uint32 deviceID, Uint32 *list, int count)
+static SDL_bool HasDeviceID(Uint32 deviceID, const Uint32 *list, int count)
 {
     for (int i = 0; i < count; ++i) {
         if (deviceID == list[i]) {
@@ -867,7 +867,7 @@ void WIN_CheckKeyboardAndMouseHotplug(SDL_VideoDevice *_this, SDL_bool initial_c
     PRAWINPUTDEVICELIST raw_devices = NULL;
     UINT raw_device_count = 0;
     int old_keyboard_count = 0;
-    SDL_KeyboardID *old_keyboards = NULL;
+    const SDL_KeyboardID *old_keyboards = NULL;
     int new_keyboard_count = 0;
     SDL_KeyboardID *new_keyboards = NULL;
     int old_mouse_count = 0;
@@ -982,7 +982,6 @@ void WIN_CheckKeyboardAndMouseHotplug(SDL_VideoDevice *_this, SDL_bool initial_c
         }
     }
 
-    SDL_free(old_keyboards);
     SDL_free(new_keyboards);
     SDL_free(old_mice);
     SDL_free(new_mice);

+ 2 - 3
src/video/x11/SDL_x11xinput2.c

@@ -715,7 +715,7 @@ static void AddDeviceID(Uint32 deviceID, Uint32 **list, int *count)
     *list = new_list;
 }
 
-static SDL_bool HasDeviceID(Uint32 deviceID, Uint32 *list, int count)
+static SDL_bool HasDeviceID(Uint32 deviceID, const Uint32 *list, int count)
 {
     for (int i = 0; i < count; ++i) {
         if (deviceID == list[i]) {
@@ -734,7 +734,7 @@ void X11_Xinput2UpdateDevices(SDL_VideoDevice *_this, SDL_bool initial_check)
     XIDeviceInfo *info;
     int ndevices;
     int old_keyboard_count = 0;
-    SDL_KeyboardID *old_keyboards = NULL;
+    const SDL_KeyboardID *old_keyboards = NULL;
     int new_keyboard_count = 0;
     SDL_KeyboardID *new_keyboards = NULL;
     int old_mouse_count = 0;
@@ -839,7 +839,6 @@ void X11_Xinput2UpdateDevices(SDL_VideoDevice *_this, SDL_bool initial_check)
         }
     }
 
-    SDL_free(old_keyboards);
     SDL_free(new_keyboards);
     SDL_free(old_mice);
     SDL_free(new_mice);

+ 1 - 1
test/testhotplug.c

@@ -80,7 +80,7 @@ int main(int argc, char *argv[])
     //SDL_CreateWindow("Dummy", 128, 128, 0);
     */
 
-    SDL_free(SDL_GetKeyboards(&num_keyboards));
+    SDL_GetKeyboards(&num_keyboards);
     SDL_Log("There are %d keyboards at startup\n", num_keyboards);
 
     SDL_free(SDL_GetMice(&num_mice));