Skip to content

Commit

Permalink
dsads
Browse files Browse the repository at this point in the history
  • Loading branch information
SiegeLord committed Nov 18, 2024
1 parent 572f013 commit 59fdae2
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 56 deletions.
1 change: 1 addition & 0 deletions allegro5.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,4 @@ osx_tell_dock_outside_bundle = true
# To restore behavior of older code versions, specify this value to the
# Allegro version that had the desired old behavior.
# joystick_version = 5.2.9
# keyboard_version = 5.2.9
1 change: 1 addition & 0 deletions include/allegro5/internal/aintern_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ AL_FUNC(void *, _al_open_library, (const char *filename));
AL_FUNC(void *, _al_import_symbol, (void *library, const char *symbol));
AL_FUNC(void, _al_close_library, (void *library));
AL_FUNC(uint32_t, _al_get_joystick_compat_version, (void));
AL_FUNC(uint32_t, _al_get_keyboard_compat_version, (void));

#ifdef __cplusplus
}
Expand Down
16 changes: 0 additions & 16 deletions src/joynu.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,22 +302,6 @@ void al_get_joystick_state(ALLEGRO_JOYSTICK *joy, ALLEGRO_JOYSTICK_STATE *ret_st
new_joystick_driver->get_joystick_state(joy, ret_state);
}



uint32_t _al_get_joystick_compat_version(void)
{
ALLEGRO_CONFIG *system_config = al_get_system_config();
const char* compat_version = al_get_config_value(system_config, "compatibility", "joystick_version");
if (!compat_version || strlen(compat_version) == 0)
return al_get_allegro_version();
int version = 0;
int sub_version = 0;
int wip_version = 0;
/* Ignore the release number, we don't expect that to make a difference */
sscanf(compat_version, "%2d.%2d.%2d", &version, &sub_version, &wip_version);
return AL_ID(version, sub_version, wip_version, 0);
}

/*
* Local Variables:
* c-basic-offset: 3
Expand Down
89 changes: 49 additions & 40 deletions src/macosx/keybd.m
Original file line number Diff line number Diff line change
Expand Up @@ -268,58 +268,67 @@ void _al_osx_keyboard_handler(int pressed, NSEvent *event, ALLEGRO_DISPLAY* dpy)
int key_shifts = translate_modifier_flags([event modifierFlags]);

if (pressed) {
/* https://stackoverflow.com/a/22677690 */
TISInputSourceRef keyboard_input = TISCopyCurrentKeyboardInputSource();
CFDataRef layout_data = TISGetInputSourceProperty(keyboard_input, kTISPropertyUnicodeKeyLayoutData);
const UCKeyboardLayout *layout = (const UCKeyboardLayout *)CFDataGetBytePtr(layout_data);

CGEventFlags modifier_flags = [event modifierFlags];
UInt32 modifier_key_state = (modifier_flags >> 16) & 0xff;

UniChar unicode_string[5];
unicode_string[4] = 0;
UniCharCount unicode_length;

UCKeyTranslate(layout,
[event keyCode],
kUCKeyActionDown,
modifier_key_state,
LMGetKbdType(),
0,
&dead_key_state,
4,
&unicode_length,
unicode_string);

int32_t unichar = 0;
if (unicode_length > 0) {
ALLEGRO_USTR *ustr = al_ustr_new_from_utf16(unicode_string);
unichar = al_ustr_get(ustr, 0);
if (unichar < 0)
unichar = 0;
/* For some reason, pad enter sends a ^C. */
if (scancode == ALLEGRO_KEY_PAD_ENTER && unichar == 3)
unichar = '\r';
/* Single out the few printable characters under 32 */
if (unichar < ' ' && (unichar != '\r' && unichar != '\t' && unichar != '\b'))
unichar = 0;
al_ustr_free(ustr);
}
CFRelease(keyboard_input);

bool new_input = _al_get_keyboard_compat_version() >= AL_ID(5, 2, 10, 0);
NSString *raw_characters = [event charactersIgnoringModifiers];
NSString *characters = [event characters];
UniChar raw_character = ([raw_characters length] > 0) ? [raw_characters characterAtIndex: 0] : 0;
UniChar character = ([characters length] > 0) ? [characters characterAtIndex: 0] : 0;

if (new_input) {
/* https://stackoverflow.com/a/22677690 */
TISInputSourceRef keyboard_input = TISCopyCurrentKeyboardInputSource();
CFDataRef layout_data = TISGetInputSourceProperty(keyboard_input, kTISPropertyUnicodeKeyLayoutData);
const UCKeyboardLayout *layout = (const UCKeyboardLayout *)CFDataGetBytePtr(layout_data);

CGEventFlags modifier_flags = [event modifierFlags];
UInt32 modifier_key_state = (modifier_flags >> 16) & 0xff;

UniChar unicode_string[5];
unicode_string[4] = 0;
UniCharCount unicode_length;

UCKeyTranslate(layout,
[event keyCode],
kUCKeyActionDown,
modifier_key_state,
LMGetKbdType(),
0,
&dead_key_state,
4,
&unicode_length,
unicode_string);

if (unicode_length > 0) {
ALLEGRO_USTR *ustr = al_ustr_new_from_utf16(unicode_string);
unichar = al_ustr_get(ustr, 0);
if (unichar < 0)
unichar = 0;
/* For some reason, pad enter sends a ^C. */
if (scancode == ALLEGRO_KEY_PAD_ENTER && unichar == 3)
unichar = '\r';
/* Single out the few printable characters under 32 */
if (unichar < ' ' && (unichar != '\r' && unichar != '\t' && unichar != '\b'))
unichar = 0;
al_ustr_free(ustr);
}
CFRelease(keyboard_input);
}
else
unichar = character;

/* Apple maps function, arrow, and other keys to Unicode points.
We want to generate CHAR events for them, so we'll override the translation logic.
_handle_key_press will set the unichar back to 0 for these keys. */
if (unichar == 0 && character >= 0xF700 && character <= 0xF747)
if (character >= 0xF700 && character <= 0xF747)
unichar = -1;
bool is_repeat = pressed ? ([event isARepeat] == YES) : false;
/* The delete key. */
if (character == 0xF728 && new_input)
unichar = 127;
/* Special processing to send character 1 for CTRL-A, 2 for CTRL-B etc. */
if ((key_shifts & ALLEGRO_KEYMOD_CTRL) && (isalpha(raw_character)))
unichar = tolower(raw_character) - 'a' + 1;
bool is_repeat = pressed ? ([event isARepeat] == YES) : false;
_handle_key_press(dpy, unichar, scancode, key_shifts, is_repeat);
}
else {
Expand Down
26 changes: 26 additions & 0 deletions src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,30 @@ void al_init_timeout(ALLEGRO_TIMEOUT *timeout, double seconds)
active_sysdrv->vt->init_timeout(timeout, seconds);
}


static uint32_t get_joystick_compat_version(const char* key)
{
ALLEGRO_CONFIG *system_config = al_get_system_config();
const char* compat_version = al_get_config_value(system_config, "compatibility", key);
if (!compat_version || strlen(compat_version) == 0)
return al_get_allegro_version();
int version = 0;
int sub_version = 0;
int wip_version = 0;
/* Ignore the release number, we don't expect that to make a difference */
sscanf(compat_version, "%2d.%2d.%2d", &version, &sub_version, &wip_version);
return AL_ID(version, sub_version, wip_version, 0);
}


uint32_t _al_get_joystick_compat_version(void)
{
return get_joystick_compat_version("joystick_version");
}

uint32_t _al_get_keyboard_compat_version(void)
{
return get_joystick_compat_version("keyboard_version");
}

/* vim: set sts=3 sw=3 et: */

0 comments on commit 59fdae2

Please sign in to comment.