Cloudef / wlc

High-level Wayland compositor library
MIT License
330 stars 58 forks source link

random key sym captured in bool (*key)(wlc_handle view, uint32_t time, const struct wlc_modifiers*, uint32_t key, enum wlc_key_state); #153

Closed xeechou closed 8 years ago

xeechou commented 8 years ago

Hi,

I am writing code to test key press event, and I found if I only pressed modifiers, I would get random key symbol, the code is here:

bool
keyboard_key(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, uint32_t key, enum wlc_key_state state)
{
(void)time, (void)key;

const uint32_t sym = wlc_keyboard_get_keysym_for_key(key, NULL);

if (state == WLC_KEY_STATE_PRESSED) {
    //debug
    fprintf(glog_file, "KEYKEY: modifiers: %d, leds:%d,  key: %c\n", modifiers->mods, modifiers->leds, sym);
    struct wlc_modifiers test_modifiers;
    uint32_t test_key;
    uint64_t code;
    code = keysym_encode(modifiers, sym);
    keysym_decode(&test_modifiers, &test_key, code);
    if (test_modifiers.leds != modifiers->leds ||
        test_modifiers.mods != modifiers->mods ||
        test_key != sym) {
        fprintf(glog_file, "KEYKEY: encode\\decode is buggy\n");
    }
    //debug

In the log file, I found the last key field in fprintf is random key if no key is pressed. Is it normal?

Cloudef commented 8 years ago

You are printing uint32_t as %c. I'm not sure what you mean by "random key", but the keysym isn't ASCII character.

xeechou commented 8 years ago

I used %c for 'pretty print', I used %d before, the result is similar, the values of sym was not consistent while I didn't press any key(but modifiers). that is, meaning of "random key" means I got random value for sym.

These are the sample output of log:

KEYKEY: modifiers: 4, leds:0,  key: ã
KEYKEY: modifiers: 12, leds:0,  key: é
KEYKEY: modifiers: 4, leds:0,  key: ã
KEYKEY: modifiers: 12, leds:0,  key: é
KEYKEY: modifiers: 4, leds:0,  key: ã
KEYKEY: modifiers: 12, leds:0,  key: é
KEYKEY: modifiers: 0, leds:0,  key: 
KEYKEY: modifiers: 0, leds:0,  key:  
KEYKEY: modifiers: 0, leds:0,  key:  
KEYKEY: modifiers: 0, leds:0,  key:  
KEYKEY: modifiers: 4, leds:0,  key: ã
KEYKEY: modifiers: 12, leds:0,  key: é
KEYKEY: modifiers: 12, leds:0,  key: 
Earnestly commented 8 years ago

What exactly is inconsistent here? We know %c isn't going to work at all on uint32_t so you'll get random memory differencing that as a char (essentially 1 byte).

But the number, the %d looks fine to me?

_Edit: Don't tell me you expected naively using %c was going to work on a uint32_t storage type..._

xeechou commented 8 years ago

No, the %d won't work either, I used %c just to see if [a-z] are mapped correctly, the inconsistent means if I don't press any key sym but modifiers for two times, the sym values are different.

KEYKEY: modifiers: 0, leds:0,  key: 97   //I pressed 'a'
KEYKEY: modifiers: 0, leds:0,  key: 98   //I pressed 'b'
KEYKEY: modifiers: 0, leds:0,  key: 99   //I pressed 'c'
KEYKEY: modifiers: 0, leds:0,  key: 100  //I pressed 'd'
KEYKEY: modifiers: 0, leds:0,  key: 101  //I pressed 'e' they all correct
KEYKEY: modifiers: 4, leds:0,  key: 65507 //I pressed 'ctrl'
KEYKEY: modifiers: 8, leds:0,  key: 65513 //I pressed 'alt'
KEYKEY: modifiers: 12, leds:0,  key: 65507 //I tried to press ctrl+alt+esc to quit program
KEYKEY: modifiers: 12, leds:0,  key: 65307 //I guess 

when I pressed ctrl, the sym is 65507, when I pressed alt, the sym is 65513. Just wondering that shouldn't the sym be the same in two cases, because I didn't press any sym at the times?

Cloudef commented 8 years ago

No, ctrl and alt are different keys, of course they will have different symbol.

Earnestly commented 8 years ago

Isn't there a function to print a nice character that's associated with the uint32_t value?

xeechou commented 8 years ago

I see, I thought keysym is independent from modifiers, thanks.

Cloudef commented 8 years ago

@Earnestly xkb_keysym_get_name and xkb_keysym_to_utf8/xkb_keysym_to_utf32, depending what you want.