-
Notifications
You must be signed in to change notification settings - Fork 16
Discover X keycodes at runtime, as they are server dependent. #8
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
Open
tom-seddon
wants to merge
2
commits into
master
Choose a base branch
from
ts/xkeycodes2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
#include "os_x11_keys.h" | ||
|
||
#include <X11/Xlib.h> | ||
|
||
struct mapping_struct { | ||
const char *p_x_key_name; | ||
uint8_t key; | ||
}; | ||
|
||
static const struct mapping_struct s_x_keys_mapping[] = { | ||
{"Escape", k_keyboard_key_escape}, | ||
{"0", '0'}, | ||
{"1", '1'}, | ||
{"2", '2'}, | ||
{"3", '3'}, | ||
{"4", '4'}, | ||
{"5", '5'}, | ||
{"6", '6'}, | ||
{"7", '7'}, | ||
{"8", '8'}, | ||
{"9", '9'}, | ||
{"minus", '-'}, | ||
{"equal", '='}, | ||
{"BackSpace", k_keyboard_key_backspace}, | ||
|
||
{"Tab", k_keyboard_key_tab}, | ||
{"Q", 'Q'}, | ||
{"W", 'W'}, | ||
{"E", 'E'}, | ||
{"R", 'R'}, | ||
{"T", 'T'}, | ||
{"Y", 'Y'}, | ||
{"U", 'U'}, | ||
{"I", 'I'}, | ||
{"O", 'O'}, | ||
{"P", 'P'}, | ||
{"bracketleft", '['}, | ||
{"bracketright", ']'}, | ||
{"backslash", '\\'}, | ||
|
||
{"Caps_Lock", k_keyboard_key_caps_lock}, | ||
{"A", 'A'}, | ||
{"S", 'S'}, | ||
{"D", 'D'}, | ||
{"F", 'F'}, | ||
{"G", 'G'}, | ||
{"H", 'H'}, | ||
{"J", 'J'}, | ||
{"K", 'K'}, | ||
{"L", 'L'}, | ||
{"semicolon", ';'}, | ||
{"apostrophe", '\''}, | ||
{"Return", k_keyboard_key_enter}, | ||
|
||
{"Shift_L", k_keyboard_key_shift_left}, | ||
{"Z", 'Z'}, | ||
{"X", 'X'}, | ||
{"C", 'C'}, | ||
{"V", 'V'}, | ||
{"B", 'B'}, | ||
{"N", 'N'}, | ||
{"M", 'M'}, | ||
{"comma", ','}, | ||
{"period", '.'}, | ||
{"slash", '/'}, | ||
{"Shift_R", k_keyboard_key_shift_right}, | ||
|
||
{"Control_L", k_keyboard_key_ctrl_left}, | ||
{"Meta_L", k_keyboard_key_alt_left}, | ||
{"space", ' '}, | ||
|
||
{"F1", k_keyboard_key_f1}, | ||
{"F2", k_keyboard_key_f2}, | ||
{"F3", k_keyboard_key_f3}, | ||
{"F4", k_keyboard_key_f4}, | ||
{"F5", k_keyboard_key_f5}, | ||
{"F6", k_keyboard_key_f6}, | ||
{"F7", k_keyboard_key_f7}, | ||
{"F8", k_keyboard_key_f8}, | ||
{"F9", k_keyboard_key_f9}, | ||
{"F10", k_keyboard_key_f0}, | ||
{"F11", k_keyboard_key_f11}, | ||
{"F12", k_keyboard_key_f12}, | ||
|
||
{"Up", k_keyboard_key_arrow_up}, | ||
{"Down", k_keyboard_key_arrow_down}, | ||
{"Left", k_keyboard_key_arrow_left}, | ||
{"Right", k_keyboard_key_arrow_right}, | ||
|
||
{"End", k_keyboard_key_end}, | ||
|
||
{NULL}, | ||
}; | ||
|
||
static int s_keys_mapping_built; | ||
static uint8_t s_keys_mapping[256]; | ||
|
||
/* XKeysymToKeycode only returns one keycode - but a keysym can map to multiple | ||
* keycodes. So this code does an exhaustive search to find everything. | ||
* | ||
* There's probably a better way of doing this. | ||
*/ | ||
static void | ||
init_mapping(Display* p_display) { | ||
const struct mapping_struct* p_mapping; | ||
int keycode; | ||
int min_keycode; | ||
int max_keycode; | ||
/* X keycodes are byte values, so 256 entries is sufficient. */ | ||
KeySym* keysyms_by_keycode[256]; | ||
int num_keysyms_by_keycode[256]; | ||
|
||
(void) XDisplayKeycodes(p_display, &min_keycode, &max_keycode); | ||
if ((min_keycode > max_keycode) || (min_keycode < 8) || (max_keycode > 255)) { | ||
errx(1, "inexplicable XDisplayKeycodes result"); | ||
} | ||
|
||
/* XGetKeyboardMapping took long enough on my Mac that it seems to be worth | ||
* calling it for each keycode outside the loop. (The endless calls to | ||
* XKeysymToString in the inner loop below, on the other hand, aren't | ||
* obviously causing a problem.) | ||
*/ | ||
for (keycode = min_keycode; keycode <= max_keycode; ++keycode) { | ||
keysyms_by_keycode[keycode] = XGetKeyboardMapping(p_display, | ||
keycode, | ||
1, | ||
&num_keysyms_by_keycode[keycode]); | ||
} | ||
|
||
for (p_mapping = s_x_keys_mapping; | ||
p_mapping->p_x_key_name != NULL; | ||
++p_mapping) { | ||
int found_x_key = 0; | ||
|
||
for (keycode = min_keycode; keycode <= max_keycode; ++keycode) { | ||
int i; | ||
const KeySym *keysyms = keysyms_by_keycode[keycode]; | ||
|
||
for (i = 0; i < num_keysyms_by_keycode[keycode]; ++i) { | ||
const char *keysym_name = XKeysymToString(keysyms[i]); | ||
if (keysym_name != NULL) { | ||
if (strcmp(keysym_name, p_mapping->p_x_key_name) == 0) { | ||
/* Can multiple keysyms map to the same keycode? Should probably | ||
* print a warning if that happens. */ | ||
tom-seddon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
s_keys_mapping[keycode] = p_mapping->key; | ||
|
||
found_x_key = 1; | ||
|
||
/* Continue checking key codes. The same X key name might map to | ||
* multiple physical keys. | ||
*/ | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!found_x_key) { | ||
/* Not an error... right? This key presumably just isn't on the | ||
* keyboard. | ||
*/ | ||
log_do_log(k_log_misc, k_log_info, "X key not available: %s", p_mapping->p_x_key_name); | ||
} | ||
} | ||
|
||
for (keycode = min_keycode; keycode <= max_keycode; ++keycode) { | ||
(void) XFree(keysyms_by_keycode[keycode]); | ||
keysyms_by_keycode[keycode] = NULL; | ||
} | ||
} | ||
|
||
uint8_t* | ||
os_x11_keys_get_mapping(void* display) { | ||
tom-seddon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!s_keys_mapping_built) { | ||
init_mapping(display); | ||
|
||
s_keys_mapping_built = 1; | ||
} | ||
|
||
return s_keys_mapping; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.