Skip to content

Commit

Permalink
Fix TIC-80 when using a French AZERTY layout (#2663)
Browse files Browse the repository at this point in the history
* Initial naive implementation

* Reorganize code

* Generalize & future-proof

Detects more keyboard layouts etc.

* Unbreak other platforms

* Refactor keyboard layouts into an enum
  • Loading branch information
aliceisjustplaying authored Aug 26, 2024
1 parent 3af8080 commit e60a708
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
10 changes: 10 additions & 0 deletions include/tic80.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ typedef union
u32 data;
} tic80_keyboard;

typedef enum {
tic_layout_unknown = 0,
tic_layout_qwerty,
tic_layout_azerty,
tic_layout_qwertz,
tic_layout_qzerty,
tic_layout_de_neo,
tic_layout_de_bone
} tic_layout;

typedef struct
{
tic80_gamepads gamepads;
Expand Down
10 changes: 8 additions & 2 deletions src/studio/studio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ static void processShortcuts(Studio* studio)
{
if (enterWasPressedOnce(studio)) gotoFullscreen(studio);
#if defined(BUILD_EDITORS)
else if(studio->mode != TIC_RUN_MODE)
else if(studio->mode != TIC_RUN_MODE && studio->config->data.keyboardLayout != tic_layout_azerty)
{
if(keyWasPressedOnce(studio, tic_key_grave)) setStudioMode(studio, TIC_CONSOLE_MODE);
else if(keyWasPressedOnce(studio, tic_key_1)) setStudioMode(studio, TIC_CODE_MODE);
Expand Down Expand Up @@ -2652,6 +2652,11 @@ static void setPopupHide(void* data)

#endif

void studio_keymapchanged(Studio* studio, tic_layout keyboardLayout)
{
studio->config->data.keyboardLayout = keyboardLayout;
}

bool studio_alive(Studio* studio)
{
return studio->alive;
Expand Down Expand Up @@ -2683,7 +2688,7 @@ static bool onEnumModule(const char* name, const char* title, const char* hash,
}
#endif

Studio* studio_create(s32 argc, char **argv, s32 samplerate, tic80_pixel_color_format format, const char* folder, s32 maxscale)
Studio* studio_create(s32 argc, char **argv, s32 samplerate, tic80_pixel_color_format format, const char* folder, s32 maxscale, tic_layout keyboardLayout)
{
setbuf(stdout, NULL);

Expand Down Expand Up @@ -2857,6 +2862,7 @@ Studio* studio_create(s32 argc, char **argv, s32 samplerate, tic80_pixel_color_f
studio->config->data.fft = args.fft;
studio->config->data.fftcaptureplaybackdevices = args.fftcaptureplaybackdevices;
studio->config->data.fftdevice = args.fftdevice;
studio->config->data.keyboardLayout = keyboardLayout;
#endif

studioConfigChanged(studio);
Expand Down
5 changes: 4 additions & 1 deletion src/studio/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ typedef struct
int fft;
int fftcaptureplaybackdevices;
const char *fftdevice;

tic_layout keyboardLayout;
} StudioConfig;

typedef struct Studio Studio;
Expand All @@ -162,12 +164,13 @@ const tic_mem* studio_mem(Studio* studio);
void studio_tick(Studio* studio, tic80_input input);
void studio_sound(Studio* studio);
void studio_load(Studio* studio, const char* file);
void studio_keymapchanged(Studio *studio, tic_layout keyboardLayout);
bool studio_alive(Studio* studio);
void studio_exit(Studio* studio);
void studio_delete(Studio* studio);
const StudioConfig* studio_config(Studio* studio);

Studio* studio_create(s32 argc, char **argv, s32 samplerate, tic80_pixel_color_format format, const char* appFolder, s32 maxscale);
Studio* studio_create(s32 argc, char **argv, s32 samplerate, tic80_pixel_color_format format, const char* appFolder, s32 maxscale, tic_layout keyboardLayout);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions src/system/baremetalpi/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ TShutdownMode Run(void)
char* argv[] = { &arg0[0], NULL };
int argc = 1;
malloc(88);
platform.studio = studio_create(argc, argv, 44100, TIC80_PIXEL_COLOR_BGRA8888, "tic80", INT32_MAX);
platform.studio = studio_create(argc, argv, 44100, TIC80_PIXEL_COLOR_BGRA8888, "tic80", INT32_MAX, tic_layout_qwerty);
malloc(99);

}
Expand All @@ -404,7 +404,7 @@ TShutdownMode Run(void)
char* argv[] = { &arg0[0], &arg1[0], NULL };
int argc = 2;
dbg("Without keyboard\n");
platform.studio = studio_create(argc, argv, 44100, TIC80_PIXEL_COLOR_BGRA8888, "tic80", INT32_MAX);
platform.studio = studio_create(argc, argv, 44100, TIC80_PIXEL_COLOR_BGRA8888, "tic80", INT32_MAX, tic_layout_qwerty);
}
dbg("studio_create OK\n");

Expand Down
2 changes: 1 addition & 1 deletion src/system/n3ds/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ int main(int argc, char **argv) {
n3ds_draw_init();
n3ds_keyboard_init(&platform.keyboard);

platform.studio = studio_create(argc_used, argv_used, AUDIO_FREQ, TIC80_PIXEL_COLOR_ABGR8888, "./", INT32_MAX);
platform.studio = studio_create(argc_used, argv_used, AUDIO_FREQ, TIC80_PIXEL_COLOR_ABGR8888, "./", INT32_MAX, tic_layout_qwerty);

n3ds_sound_init(AUDIO_FREQ);

Expand Down
25 changes: 24 additions & 1 deletion src/system/sdl/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,26 @@ static void handleKeydown(SDL_Keycode keycode, bool down, bool* state, bool* pre
#endif
}

tic_layout detect_keyboard_layout()
{
char q = SDL_GetKeyFromScancode(SDL_SCANCODE_Q);
char w = SDL_GetKeyFromScancode(SDL_SCANCODE_W);
char y = SDL_GetKeyFromScancode(SDL_SCANCODE_Y);

tic_layout layout = tic_layout_unknown;

if (q == 'q' && w == 'w' && y == 'y') layout = tic_layout_qwerty; // US etc.
if (q == 'a' && w == 'z' && y == 'y') layout = tic_layout_azerty; // French
if (q == 'q' && w == 'w' && y == 'z') layout = tic_layout_qwertz; // German etc.
if (q == 'q' && w == 'z' && y == 'y') layout = tic_layout_qzerty; // Italian
// Don't ask me why it detects k instead of l
if (q == 'x' && w == 'v' && y == 'k') layout = tic_layout_de_neo; // xvlcwk - German Neo
// ...or why it detects p instead of u
if (q == 'j' && w == 'd' && y == 'p') layout = tic_layout_de_bone; // jduaxp - German Bone

return layout;
}

static void pollEvents()
{
// check if releative mode was enabled
Expand Down Expand Up @@ -1193,6 +1213,9 @@ static void pollEvents()
case SDL_KEYUP:
handleKeydown(event.key.keysym.sym, false, platform.keyboard.state, platform.keyboard.pressed);
break;
case SDL_KEYMAPCHANGED:
studio_keymapchanged(platform.studio, detect_keyboard_layout());
break;
case SDL_TEXTINPUT:
if(strlen(event.text.text) == 1)
platform.keyboard.text = event.text.text[0];
Expand Down Expand Up @@ -1894,7 +1917,7 @@ static s32 start(s32 argc, char **argv, const char* folder)
SDL_Log("Unable to initialize SDL Game Controller: %i, %s\n", result, SDL_GetError());
}

platform.studio = studio_create(argc, argv, TIC80_SAMPLERATE, SCREEN_FORMAT, folder, determineMaximumScale());
platform.studio = studio_create(argc, argv, TIC80_SAMPLERATE, SCREEN_FORMAT, folder, determineMaximumScale(), detect_keyboard_layout());

SCOPE(studio_delete(platform.studio))
{
Expand Down

0 comments on commit e60a708

Please sign in to comment.