Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TIC-80 when using a French AZERTY layout #2663

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading