#707 added very dirty hack to support international keyboard input
This commit is contained in:
		
							
								
								
									
										42
									
								
								src/studio.c
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								src/studio.c
									
									
									
									
									
								
							@@ -237,35 +237,31 @@ static struct
 | 
			
		||||
 | 
			
		||||
char getKeyboardText()
 | 
			
		||||
{
 | 
			
		||||
	char text = getSystem()->getKeyboardText();
 | 
			
		||||
    tic_mem* tic = impl.studio.tic;
 | 
			
		||||
 | 
			
		||||
	if(text) return text;
 | 
			
		||||
    static const char Symbols[] = 	" abcdefghijklmnopqrstuvwxyz0123456789-=[]\\;'`,./ ";
 | 
			
		||||
    static const char Shift[] =		" ABCDEFGHIJKLMNOPQRSTUVWXYZ)!@#$%^&*(_+{}|:\"~<>? ";
 | 
			
		||||
 | 
			
		||||
	tic_mem* tic = impl.studio.tic;
 | 
			
		||||
    enum{Count = sizeof Symbols};
 | 
			
		||||
 | 
			
		||||
	static const char Symbols[] = 	" abcdefghijklmnopqrstuvwxyz0123456789-=[]\\;'`,./ ";
 | 
			
		||||
	static const char Shift[] = 	" ABCDEFGHIJKLMNOPQRSTUVWXYZ)!@#$%^&*(_+{}|:\"~<>? ";
 | 
			
		||||
    for(s32 i = 0; i < TIC80_KEY_BUFFER; i++)
 | 
			
		||||
    {
 | 
			
		||||
        tic_key key = tic->ram.input.keyboard.keys[i];
 | 
			
		||||
 | 
			
		||||
	enum{Count = sizeof Symbols};
 | 
			
		||||
        if(key > 0 && key < Count && tic->api.keyp(tic, key, KEYBOARD_HOLD, KEYBOARD_PERIOD))
 | 
			
		||||
        {
 | 
			
		||||
            bool caps = tic->api.key(tic, tic_key_capslock);
 | 
			
		||||
            bool shift = tic->api.key(tic, tic_key_shift);
 | 
			
		||||
 | 
			
		||||
	for(s32 i = 0; i < TIC80_KEY_BUFFER; i++)
 | 
			
		||||
	{
 | 
			
		||||
		tic_key key = tic->ram.input.keyboard.keys[i];
 | 
			
		||||
            return caps
 | 
			
		||||
                ? key >= tic_key_a && key <= tic_key_z 
 | 
			
		||||
                    ? shift ? Symbols[key] : Shift[key]
 | 
			
		||||
                    : shift ? Shift[key] : Symbols[key]
 | 
			
		||||
                : shift ? Shift[key] : Symbols[key];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		if(key > 0 && key < Count && tic->api.keyp(tic, key, KEYBOARD_HOLD, KEYBOARD_PERIOD))
 | 
			
		||||
		{
 | 
			
		||||
			bool caps = tic->api.key(tic, tic_key_capslock);
 | 
			
		||||
			bool shift = tic->api.key(tic, tic_key_shift);
 | 
			
		||||
 | 
			
		||||
			return caps
 | 
			
		||||
				? key >= tic_key_a && key <= tic_key_z 
 | 
			
		||||
					? shift ? Symbols[key] : Shift[key]
 | 
			
		||||
					: shift ? Shift[key] : Symbols[key]
 | 
			
		||||
				: shift ? Shift[key] : Symbols[key];
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool keyWasPressed(tic_key key)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										110
									
								
								src/system.c
									
									
									
									
									
								
							
							
						
						
									
										110
									
								
								src/system.c
									
									
									
									
									
								
							@@ -395,52 +395,94 @@ static void processMouse()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TODO: ugly hack, but I didn't find a better solution
 | 
			
		||||
// will try to fix it later
 | 
			
		||||
static char getKeyboardText()
 | 
			
		||||
{
 | 
			
		||||
	char text = platform.keyboard.text;
 | 
			
		||||
	platform.keyboard.text = 0;
 | 
			
		||||
	return text;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void processKeyboard()
 | 
			
		||||
{
 | 
			
		||||
	static const u32 KeyboardCodes[tic_keys_count] = 
 | 
			
		||||
	{
 | 
			
		||||
		#include "keycodes.inl"
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	tic80_input* input = &platform.studio->tic->ram.input;
 | 
			
		||||
	input->keyboard.data = 0;
 | 
			
		||||
 | 
			
		||||
	enum{BufSize = COUNT_OF(input->keyboard.keys)};
 | 
			
		||||
 | 
			
		||||
	s32 c = 0;
 | 
			
		||||
	{
 | 
			
		||||
		SDL_Keymod mod = SDL_GetModState();
 | 
			
		||||
		if(mod & KMOD_SHIFT) input->keyboard.keys[c++] = tic_key_shift;
 | 
			
		||||
		if(mod & (KMOD_CTRL | KMOD_GUI)) input->keyboard.keys[c++] = tic_key_ctrl;
 | 
			
		||||
		if(mod & KMOD_ALT) input->keyboard.keys[c++] = tic_key_alt;
 | 
			
		||||
		if(mod & KMOD_CAPS) input->keyboard.keys[c++] = tic_key_capslock;
 | 
			
		||||
	}
 | 
			
		||||
	SDL_Keymod mod = SDL_GetModState();
 | 
			
		||||
 | 
			
		||||
	const u8* keyboard = SDL_GetKeyboardState(NULL);
 | 
			
		||||
 | 
			
		||||
	for(s32 i = 0; i < SDL_NUM_SCANCODES && c < BufSize; i++)
 | 
			
		||||
	// TODO: the ugliest hack ever
 | 
			
		||||
	// will try to remove it
 | 
			
		||||
	if(mod & KMOD_RALT)
 | 
			
		||||
	{
 | 
			
		||||
		if(keyboard[i])
 | 
			
		||||
		static const struct {tic_keycode code; tic_keycode shift;} TextCodes[] =	
 | 
			
		||||
		{	
 | 
			
		||||
			SDL_Keycode keycode = i == SDL_SCANCODE_AC_BACK 
 | 
			
		||||
				? SDLK_ESCAPE
 | 
			
		||||
				: SDL_GetKeyFromScancode(i);
 | 
			
		||||
			['-'] = {tic_key_minus, tic_key_unknown},	
 | 
			
		||||
			['='] = {tic_key_equals, tic_key_unknown},	
 | 
			
		||||
			['['] = {tic_key_leftbracket, tic_key_unknown},	
 | 
			
		||||
			[']'] = {tic_key_rightbracket, tic_key_unknown},	
 | 
			
		||||
			['\\'] = {tic_key_backslash, tic_key_unknown},	
 | 
			
		||||
			[';'] = {tic_key_semicolon, tic_key_unknown},	
 | 
			
		||||
			['\''] = {tic_key_apostrophe, tic_key_unknown},	
 | 
			
		||||
			['`'] = {tic_key_grave, tic_key_unknown},	
 | 
			
		||||
			[','] = {tic_key_comma, tic_key_unknown},	
 | 
			
		||||
			['.'] = {tic_key_period, tic_key_unknown},	
 | 
			
		||||
			['/'] = {tic_key_slash, tic_key_unknown},	
 | 
			
		||||
			[')'] = {tic_key_0, tic_key_shift},	
 | 
			
		||||
			['!'] = {tic_key_1, tic_key_shift},	
 | 
			
		||||
			['@'] = {tic_key_2, tic_key_shift},	
 | 
			
		||||
			['#'] = {tic_key_3, tic_key_shift},	
 | 
			
		||||
			['$'] = {tic_key_4, tic_key_shift},	
 | 
			
		||||
			['%'] = {tic_key_5, tic_key_shift},	
 | 
			
		||||
			['^'] = {tic_key_6, tic_key_shift},	
 | 
			
		||||
			['&'] = {tic_key_7, tic_key_shift},	
 | 
			
		||||
			['*'] = {tic_key_8, tic_key_shift},	
 | 
			
		||||
			['('] = {tic_key_9, tic_key_shift},	
 | 
			
		||||
			['_'] = {tic_key_minus, tic_key_shift},	
 | 
			
		||||
			['+'] = {tic_key_equals, tic_key_shift},	
 | 
			
		||||
			['{'] = {tic_key_leftbracket, tic_key_shift},	
 | 
			
		||||
			['}'] = {tic_key_rightbracket, tic_key_shift},	
 | 
			
		||||
			['|'] = {tic_key_backslash, tic_key_shift},	
 | 
			
		||||
			[':'] = {tic_key_semicolon, tic_key_shift},	
 | 
			
		||||
			['"'] = {tic_key_apostrophe, tic_key_shift},	
 | 
			
		||||
			['~'] = {tic_key_grave, tic_key_shift},	
 | 
			
		||||
			['<'] = {tic_key_comma, tic_key_shift},	
 | 
			
		||||
			['>'] = {tic_key_period, tic_key_shift},	
 | 
			
		||||
			['?'] = {tic_key_slash, tic_key_shift},	
 | 
			
		||||
		};
 | 
			
		||||
 | 
			
		||||
			for(s32 k = 0; k < COUNT_OF(KeyboardCodes); k++)
 | 
			
		||||
		u8 text = platform.keyboard.text;
 | 
			
		||||
 | 
			
		||||
		if(text && text < COUNT_OF(TextCodes))
 | 
			
		||||
		{
 | 
			
		||||
			input->keyboard.keys[0] = TextCodes[text].code;
 | 
			
		||||
			input->keyboard.keys[1] = TextCodes[text].shift;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		static const u32 KeyboardCodes[tic_keys_count] = 
 | 
			
		||||
		{
 | 
			
		||||
			#include "keycodes.inl"
 | 
			
		||||
		};
 | 
			
		||||
 | 
			
		||||
		s32 c = 0;
 | 
			
		||||
		{
 | 
			
		||||
			if(mod & KMOD_SHIFT) input->keyboard.keys[c++] = tic_key_shift;
 | 
			
		||||
			if(mod & KMOD_CTRL) input->keyboard.keys[c++] = tic_key_ctrl;
 | 
			
		||||
			if(mod & KMOD_CAPS) input->keyboard.keys[c++] = tic_key_capslock;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		const u8* keyboard = SDL_GetKeyboardState(NULL);
 | 
			
		||||
 | 
			
		||||
		for(s32 i = 0; i < SDL_NUM_SCANCODES && c < BufSize; i++)
 | 
			
		||||
		{
 | 
			
		||||
			if(keyboard[i])
 | 
			
		||||
			{			
 | 
			
		||||
				if(KeyboardCodes[k] == keycode)
 | 
			
		||||
				SDL_Keycode keycode = i == SDL_SCANCODE_AC_BACK 
 | 
			
		||||
					? SDLK_ESCAPE
 | 
			
		||||
					: SDL_GetKeyFromScancode(i);
 | 
			
		||||
 | 
			
		||||
				for(s32 k = 0; k < COUNT_OF(KeyboardCodes); k++)
 | 
			
		||||
				{
 | 
			
		||||
					input->keyboard.keys[c++] = k;
 | 
			
		||||
					break;
 | 
			
		||||
					if(KeyboardCodes[k] == keycode)
 | 
			
		||||
					{
 | 
			
		||||
						input->keyboard.keys[c++] = k;
 | 
			
		||||
						break;
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}		
 | 
			
		||||
@@ -756,6 +798,7 @@ static void pollEvent()
 | 
			
		||||
 | 
			
		||||
	{
 | 
			
		||||
		input->mouse.btns = 0;
 | 
			
		||||
		platform.keyboard.text = 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	SDL_Event event;
 | 
			
		||||
@@ -1311,7 +1354,6 @@ static System systemInterface =
 | 
			
		||||
	.preseed = preseed,
 | 
			
		||||
	.poll = pollEvent,
 | 
			
		||||
	.updateConfig = updateConfig,
 | 
			
		||||
	.getKeyboardText = getKeyboardText,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void gpuTick()
 | 
			
		||||
 
 | 
			
		||||
@@ -29,8 +29,6 @@ typedef struct
 | 
			
		||||
 | 
			
		||||
	void (*updateConfig)();
 | 
			
		||||
 | 
			
		||||
	char (*getKeyboardText)();
 | 
			
		||||
 | 
			
		||||
} System;
 | 
			
		||||
 | 
			
		||||
typedef struct
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user