44
44
#define KEYCODE_OPTION_LATIN_LETTERS 0x04
45
45
#define DEFAULT_KEYCODE_OPTIONS (KEYCODE_OPTION_FRENCH_NUMBERS | KEYCODE_OPTION_LATIN_LETTERS)
46
46
47
- typedef struct SDL_KeyboardInstance
48
- {
49
- SDL_KeyboardID instance_id ;
50
- char * name ;
51
- } SDL_KeyboardInstance ;
52
-
53
47
typedef struct SDL_Keyboard
54
48
{
55
49
// Data common to all keyboards
@@ -65,7 +59,8 @@ typedef struct SDL_Keyboard
65
59
66
60
static SDL_Keyboard SDL_keyboard ;
67
61
static int SDL_keyboard_count ;
68
- static SDL_KeyboardInstance * SDL_keyboards ;
62
+ static SDL_KeyboardID * SDL_keyboards ;
63
+ static SDL_HashTable * SDL_keyboard_names ;
69
64
static bool SDL_keyboard_quitting ;
70
65
71
66
static void SDLCALL SDL_KeycodeOptionsChanged (void * userdata , const char * name , const char * oldValue , const char * hint )
@@ -95,6 +90,9 @@ bool SDL_InitKeyboard(void)
95
90
{
96
91
SDL_AddHintCallback (SDL_HINT_KEYCODE_OPTIONS ,
97
92
SDL_KeycodeOptionsChanged , & SDL_keyboard );
93
+
94
+ SDL_keyboard_names = SDL_CreateHashTable (0 , true, SDL_HashID , SDL_KeyMatchID , SDL_DestroyHashValue , NULL );
95
+
98
96
return true;
99
97
}
100
98
@@ -112,7 +110,7 @@ bool SDL_IsKeyboard(Uint16 vendor, Uint16 product, int num_keys)
112
110
static int SDL_GetKeyboardIndex (SDL_KeyboardID keyboardID )
113
111
{
114
112
for (int i = 0 ; i < SDL_keyboard_count ; ++ i ) {
115
- if (keyboardID == SDL_keyboards [i ]. instance_id ) {
113
+ if (keyboardID == SDL_keyboards [i ]) {
116
114
return i ;
117
115
}
118
116
}
@@ -129,16 +127,19 @@ void SDL_AddKeyboard(SDL_KeyboardID keyboardID, const char *name)
129
127
130
128
SDL_assert (keyboardID != 0 );
131
129
132
- SDL_KeyboardInstance * keyboards = (SDL_KeyboardInstance * )SDL_realloc (SDL_keyboards , (SDL_keyboard_count + 1 ) * sizeof (* keyboards ));
130
+ SDL_KeyboardID * keyboards = (SDL_KeyboardID * )SDL_realloc (SDL_keyboards , (SDL_keyboard_count + 1 ) * sizeof (* keyboards ));
133
131
if (!keyboards ) {
134
132
return ;
135
133
}
136
- SDL_KeyboardInstance * instance = & keyboards [SDL_keyboard_count ];
137
- instance -> instance_id = keyboardID ;
138
- instance -> name = SDL_strdup (name ? name : "" );
134
+ keyboards [SDL_keyboard_count ] = keyboardID ;
139
135
SDL_keyboards = keyboards ;
140
136
++ SDL_keyboard_count ;
141
137
138
+ if (!name ) {
139
+ name = "Keyboard" ;
140
+ }
141
+ SDL_InsertIntoHashTable (SDL_keyboard_names , (const void * )(uintptr_t )keyboardID , SDL_strdup (name ), true);
142
+
142
143
SDL_Event event ;
143
144
SDL_zero (event );
144
145
event .type = SDL_EVENT_KEYBOARD_ADDED ;
@@ -154,8 +155,6 @@ void SDL_RemoveKeyboard(SDL_KeyboardID keyboardID)
154
155
return ;
155
156
}
156
157
157
- SDL_free (SDL_keyboards [keyboard_index ].name );
158
-
159
158
if (keyboard_index != SDL_keyboard_count - 1 ) {
160
159
SDL_memmove (& SDL_keyboards [keyboard_index ], & SDL_keyboards [keyboard_index + 1 ], (SDL_keyboard_count - keyboard_index - 1 ) * sizeof (SDL_keyboards [keyboard_index ]));
161
160
}
@@ -187,7 +186,7 @@ SDL_KeyboardID *SDL_GetKeyboards(int *count)
187
186
}
188
187
189
188
for (i = 0 ; i < SDL_keyboard_count ; ++ i ) {
190
- keyboards [i ] = SDL_keyboards [i ]. instance_id ;
189
+ keyboards [i ] = SDL_keyboards [i ];
191
190
}
192
191
keyboards [i ] = 0 ;
193
192
} else {
@@ -201,12 +200,17 @@ SDL_KeyboardID *SDL_GetKeyboards(int *count)
201
200
202
201
const char * SDL_GetKeyboardNameForID (SDL_KeyboardID instance_id )
203
202
{
204
- int keyboard_index = SDL_GetKeyboardIndex ( instance_id ) ;
205
- if (keyboard_index < 0 ) {
203
+ const char * name = NULL ;
204
+ if (! SDL_FindInHashTable ( SDL_keyboard_names , ( const void * )( uintptr_t ) instance_id , ( const void * * ) & name ) ) {
206
205
SDL_SetError ("Keyboard %" SDL_PRIu32 " not found" , instance_id );
207
206
return NULL ;
208
207
}
209
- return SDL_GetPersistentString (SDL_keyboards [keyboard_index ].name );
208
+ if (!name ) {
209
+ // SDL_strdup() failed during insert
210
+ SDL_OutOfMemory ();
211
+ return NULL ;
212
+ }
213
+ return name ;
210
214
}
211
215
212
216
void SDL_ResetKeyboard (void )
@@ -871,11 +875,14 @@ void SDL_QuitKeyboard(void)
871
875
SDL_keyboard_quitting = true;
872
876
873
877
for (int i = SDL_keyboard_count ; i -- ;) {
874
- SDL_RemoveKeyboard (SDL_keyboards [i ]. instance_id );
878
+ SDL_RemoveKeyboard (SDL_keyboards [i ]);
875
879
}
876
880
SDL_free (SDL_keyboards );
877
881
SDL_keyboards = NULL ;
878
882
883
+ SDL_DestroyHashTable (SDL_keyboard_names );
884
+ SDL_keyboard_names = NULL ;
885
+
879
886
if (SDL_keyboard .keymap && SDL_keyboard .keymap -> auto_release ) {
880
887
SDL_DestroyKeymap (SDL_keyboard .keymap );
881
888
SDL_keyboard .keymap = NULL ;
0 commit comments