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

Extract glfw key event data to a struct #1998

Merged
merged 2 commits into from
Sep 25, 2019
Merged

Extract glfw key event data to a struct #1998

merged 2 commits into from
Sep 25, 2019

Conversation

bew
Copy link
Contributor

@bew bew commented Sep 24, 2019

Initial work for #1980 (comment)

Notes:

  • I changed the struct KeyEvent used by IBUS handling to use embed a GLFWkeyevent struct, to avoid duplication, but I'm not sure it's a good idea after all, we can discuss this.
  • the words scancode and keycode seems to be used interchangeably, I'd suggest to choose only one and stick with it. I think that scancode is more platform-agnostic. There is also device_code which could be used in the GLFWkeyevent struct, while we keep scancode in the plateform-specific KB handling code..
    What do you think?
  • ⚠️ I did not test IBUS handling (not sure how..)

Is there a reason why there are almost no newlines nor comments in functions? It feels hard to read the code sometime.. (and splitting them in multiple functions might not always help)

@bew
Copy link
Contributor Author

bew commented Sep 24, 2019

@Luflosi I'll need help for macos if you have time
Actually no, it was simple to fix the compilation, but I can't test if it still works 😬

@Luflosi
Copy link
Contributor

Luflosi commented Sep 24, 2019

You managed to make it work for macOS yourself, GJ 👍.

@kovidgoyal kovidgoyal merged commit aadab38 into kovidgoyal:master Sep 25, 2019
@bew bew deleted the extract-glfw-key-event-data branch September 25, 2019 11:23
@bew
Copy link
Contributor Author

bew commented Sep 25, 2019

Thanks @kovidgoyal, that was fast! Do you have any comments on the questions I raised in the PR description?

@kovidgoyal
Copy link
Owner

  • I changed the struct KeyEvent used by IBUS handling to use embed a GLFWkeyevent struct, to avoid duplication, but I'm not sure it's a good idea after all, we can discuss this.

It's fine by me, I made the code more robust, to handle null text and ensure it is zero terminated.

  • the words scancode and keycode seems to be used interchangeably, I'd suggest to choose only one and stick with it. I think that scancode is more platform-agnostic. There is also device_code which could be used in the GLFWkeyevent struct, while we keep scancode in the plateform-specific KB handling code..

I would actually prefer native_keycode but it's not that big of a deal.

What do you think?

  • warning I did not test IBUS handling (not sure how..)

You can just start the ibus daemon if it is not already running and turn on some keyboard layout such as the one for abbreviations or unicode input or similar. It's been a while since I wrote that code so dont remember the details.

Is there a reason why there are almost no newlines nor comments in functions? It feels hard to read the code sometime.. (and splitting them in multiple functions might not always help)

newlines are matter of taste, I happen to find the number of newlines well suited to the way I read code. As for comments, I prefer to make code as clear as possible. I only add comments when something is counter-intuitive or complex or could benefit from a high level description. If something is unclear, feel free to ask and add a comment.

debug("↳ to IBUS: keycode: 0x%x keysym: 0x%x (%s) %s\n", key_event.ibus_keycode, key_event.ibus_sym, glfw_xkb_keysym_name(key_event.ibus_sym), format_mods(key_event.glfw_modifiers));

glfw_ev.action = action;
glfw_ev.scancode = xkb_sym;
Copy link
Contributor Author

@bew bew Sep 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this line looks wrong: Because this is not a scancode but the native keysym instead... when it should be xkb_keycode instead of xkb_sym here (for coherence with the name of the receiving field).

This also means that on_key_event then kitty's keymap handling used the value of the native keysym all along.. this doesn't sound right.. does it?

Do you actually want the native keycode (scancode) or the native keysym in kitty's keyboard handling?
I would say the native keycode would make more sense, but to avoid breaking user configs, we must (?) stick with keysym?

Copy link
Contributor Author

@bew bew Sep 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that keeping the keysym value as the "scancode" in GLFWkeyevent for linux would be inconsistent with macos, which seems to correctly put the native keycode in event struct.. (by doing [event keyCode], but which code is it? @Luflosi ?)

Now sure what to do... break user's linux configs? (might be ok, since this feature is probably not that used.. ?)

Copy link
Contributor Author

@bew bew Sep 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also have a "migration" path (over N releases), where we add the keycode (good) and the keysym (bad) to the key event struct.

Then if a kitty shortcut like ctrl+XX matches the keysym, display a sort of toast message (in addition to doing the action) telling the user to run Kitty from a terminal, and print logs telling him to update the mapping with ctrl+YY instead (using the true keycode).

WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but which code is it

What do you mean exactly?

Copy link
Contributor Author

@bew bew Sep 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading more, it looks like having the keyevent's "scancode" be the keysym value on linux seems to be the expected behavior (I was wrong...):

kitty/glfw/x11_window.c

Lines 2594 to 2597 in 4ec1a8d

int _glfwPlatformGetKeyScancode(int key)
{
return glfw_xkb_sym_for_key(key);
}

So maybe I can just change the keyevent field to native_keycode like you suggested, and document somewhere that on linux the native_keycode is not the xkb_keycode but the xkb_keysym..

Copy link
Contributor Author

@bew bew Sep 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Luflosi forget what I said, I think I got confused by native_keycode, because X11 has something called a keycode and a keysym, and it's the keysym value that is to be assigned to native_keycode.......

I'll make a PR to change the field name to native_keycode, and change vars named scancode to keycode where appropriate

@kovidgoyal
Copy link
Owner

No, we want scancode to be the XKB keysym value. Actual scancodes are
kernel/driver/hardware dependent numbers that are useless for anything
portable. Currently, if you wish to create shortcuts for keys that dont
exist in glfw you can do so using XKB symbolic names on linux and actual
scancode numbers on macOS. This functionality needs to remain, as I
doubt we will ever end up adding all possible keys to glfw.

On macOS they might make sense since the hardware is Apple
made as well, but not on Linux. You can, if you wish add the scancode as
an extra field to the struct.

So maybe to avoid confusion name it native_key instead of
native_keycode, that way there is no chance of mistaking it for an X11
keycode or a kernel scancode or whatever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants