Better keyboard detection when some of the keys are remapped, thanks to Lewis Wall

This commit is contained in:
Sam Lantinga 2014-03-23 09:44:04 -07:00
parent c2243092d7
commit c3d1037665
1 changed files with 24 additions and 18 deletions

View File

@ -207,14 +207,20 @@ X11_InitKeyboard(_THIS)
} fingerprint[] = { } fingerprint[] = {
{ SDL_SCANCODE_HOME, XK_Home, 0 }, { SDL_SCANCODE_HOME, XK_Home, 0 },
{ SDL_SCANCODE_PAGEUP, XK_Prior, 0 }, { SDL_SCANCODE_PAGEUP, XK_Prior, 0 },
{ SDL_SCANCODE_PAGEDOWN, XK_Next, 0 }, { SDL_SCANCODE_UP, XK_Up, 0 },
{ SDL_SCANCODE_LEFT, XK_Left, 0 },
{ SDL_SCANCODE_DELETE, XK_Delete, 0 },
{ SDL_SCANCODE_KP_ENTER, XK_KP_Enter, 0 },
}; };
SDL_bool fingerprint_detected; int best_distance;
int best_index;
int distance;
X11_XAutoRepeatOn(data->display); X11_XAutoRepeatOn(data->display);
/* Try to determine which scancodes are being used based on fingerprint */ /* Try to determine which scancodes are being used based on fingerprint */
fingerprint_detected = SDL_FALSE; best_distance = SDL_arraysize(fingerprint) + 1;
best_index = -1;
X11_XDisplayKeycodes(data->display, &min_keycode, &max_keycode); X11_XDisplayKeycodes(data->display, &min_keycode, &max_keycode);
for (i = 0; i < SDL_arraysize(fingerprint); ++i) { for (i = 0; i < SDL_arraysize(fingerprint); ++i) {
fingerprint[i].value = fingerprint[i].value =
@ -226,28 +232,28 @@ X11_InitKeyboard(_THIS)
if ((max_keycode - min_keycode + 1) <= scancode_set[i].table_size) { if ((max_keycode - min_keycode + 1) <= scancode_set[i].table_size) {
continue; continue;
} }
distance = 0;
for (j = 0; j < SDL_arraysize(fingerprint); ++j) { for (j = 0; j < SDL_arraysize(fingerprint); ++j) {
if (fingerprint[j].value < 0 if (fingerprint[j].value < 0
|| fingerprint[j].value >= scancode_set[i].table_size) { || fingerprint[j].value >= scancode_set[i].table_size) {
break; distance += 1;
} } else if (scancode_set[i].table[fingerprint[j].value] != fingerprint[j].scancode) {
if (scancode_set[i].table[fingerprint[j].value] != distance += 1;
fingerprint[j].scancode) {
break;
} }
} }
if (j == SDL_arraysize(fingerprint)) { if (distance < best_distance) {
#ifdef DEBUG_KEYBOARD best_distance = distance;
printf("Using scancode set %d, min_keycode = %d, max_keycode = %d, table_size = %d\n", i, min_keycode, max_keycode, scancode_set[i].table_size); best_index = i;
#endif
SDL_memcpy(&data->key_layout[min_keycode], scancode_set[i].table,
sizeof(SDL_Scancode) * scancode_set[i].table_size);
fingerprint_detected = SDL_TRUE;
break;
} }
} }
if (best_index >= 0 && best_distance <= 2) {
if (!fingerprint_detected) { #ifdef DEBUG_KEYBOARD
printf("Using scancode set %d, min_keycode = %d, max_keycode = %d, table_size = %d\n", best_index, min_keycode, max_keycode, scancode_set[best_index].table_size);
#endif
SDL_memcpy(&data->key_layout[min_keycode], scancode_set[best_index].table,
sizeof(SDL_Scancode) * scancode_set[best_index].table_size);
}
else {
SDL_Keycode keymap[SDL_NUM_SCANCODES]; SDL_Keycode keymap[SDL_NUM_SCANCODES];
printf printf