bmx-ng / sdl.mod

SDL backend for BlitzMax
7 stars 6 forks source link

Keyboard special keys ("media player keys") lead to segfault #17

Closed GWRon closed 5 years ago

GWRon commented 5 years ago

As soon as I press a "special key" on my keyboard which is relayed by the OS to the app, the app segfaults. So a "volume key" is handled by the OS (OS master volume changes) but if I eg. press "play/pause" or "forward/backwards" my app receives a segfault.

Seems correlated to the latest replacement of the key map values - and a unchecked array access with a way too high number.

img

GWRon commented 5 years ago

Seems "glue.c" returns the given mapped keycode if it does not map it to a "blitzmax key code".

So if a code bigger than the keycode array is received - it will fail.

Either "clamp" it to allowed values - or introduce the new keys (and increase array size then accordingly). What's better?

GWRon commented 5 years ago

I tried to extend "brl.mod/keycodes.mod" (.bmx and .h file) to register some more keys

keycodes.h:


    KEY_MEDIA_NEXT_TRACK=176, KEY_MEDIA_PREV_TRACK=177, KEY_MEDIA_STOP=178,
    KEY_MEDIA_PLAY_PAUSE=179,
    KEY_VOLUME_MUTE=173, KEY_VOLUME_DOWN=174, KEY_VOLUME_UP=175

keycodes.bmx

Const KEY_MEDIA_NEXT_TRACK=176
Const KEY_MEDIA_PREV_TRACK=177
Const KEY_MEDIA_STOP=178
Const KEY_MEDIA_PLAY_PAUSE=179
Const KEY_VOLUME_MUTE=173
Const KEY_VOLUME_DOWN=174
Const KEY_VOLUME_UP=175

and in sdl.mod/sdlsystem.mod/glue.c:


        case SDLK_AUDIONEXT:
            return KEY_MEDIA_NEXT_TRACK;
        case SDLK_AUDIOPREV:
            return KEY_MEDIA_PREV_TRACK;
        case SDLK_AUDIOSTOP:
            return KEY_MEDIA_STOP;
        case SDLK_AUDIOPLAY:
            return KEY_MEDIA_PLAY_PAUSE;
        case SDLK_AUDIOMUTE: // or SDLK_MUTE ?
            return KEY_VOLUME_MUTE;
        case SDLK_MUTE: // or SDLK_MUTE ?
            return KEY_VOLUME_MUTE;

but it still segfaults - as if the codes/mapping differs. Maybe a "clamp" is still needed to at least avoid a segfault.

GWRon commented 5 years ago

A basic

int mapkey(SDL_Keycode keycode) {
[...]
    return keycode > 256 ? 0 : keycode;
    //return keycode;

in sdl.mod/sdlsystem.mod/glue.c did at least avoid the segfault (quick n dirty fix).

GWRon commented 5 years ago

Hmmm ... might it be that the number reached a limit? the passed integer data is "1073741824" which seems to be the half of an 32bit signed integer limit ... might be a chance but ... I doubt that. More likely it is a "number type issue".

GWRon commented 5 years ago

Events generated contain this values:

key.keysym.sym=1073741824
key.keysym.scancode=259
SDL_GetScancodeFromKey(key.keysym.sym)=46

and another key:

key.keysym.sym=1073741824
key.keysym.scancode=258
SDL_GetScancodeFromKey(key.keysym.sym)=46

259 and 258 are defined in SDL/include/SDL_scancode.h as

SDL_SCANCODE_AUDIONEXT = 258,
SDL_SCANCODE_AUDIOPREV = 259,

So somehow the "scancode" is corret, but the "keysym.sym" value is faulty.

GWRon commented 5 years ago

key.keysym.sym is of type "Sint32". Might the different encoding of the numbers lead to issues in combination with BlitzMax?

woollybah commented 5 years ago

Some info :

The values in the SDL_Keycode enumeration are based on Unicode values representing the unmodified character that would be generated by pressing the key, or the scancode value with the high bit set (bitwise ORed with 0x40000000) for those keys that do not generate characters.

So if key & 0x40000000 is set, then there's no character for it.

GWRon commented 5 years ago

Hope this was a side note for yourself as I am not sure what to do with that information for now ;-)

GWRon commented 5 years ago

Your commit ... uhm... I now can no longer press Escape, Cursor-Keys, Enter ... :-)