Skip to content

Instantly share code, notes, and snippets.

@nfedera
Created August 26, 2019 13:14
Show Gist options
  • Save nfedera/03cf3e0b69eaeef5971e1d4dc3010f12 to your computer and use it in GitHub Desktop.
Save nfedera/03cf3e0b69eaeef5971e1d4dc3010f12 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
typedef struct _skmap_t {
unsigned scancode;
unsigned keycode;
} skmap_t;
unsigned get_keycode(const char *name);
const char * get_keyname(unsigned keycode);
unsigned get_scancode(const char *str);
int main(int argc, char **argv)
{
int fd;
int rv;
skmap_t map;
if (argc < 4) {
fprintf(stderr, "usage: %s /dev/input/eventX scancode keyname\n", argv[0]);
return 1;
}
if (!(map.scancode = get_scancode(argv[2]))) {
return 1;
}
if (!(map.keycode = get_keycode(argv[3]))) {
return 1;
}
fprintf(stderr, "mapping scancode 0x%08X to key code %u (%s)\n", map.scancode, map.keycode, get_keyname(map.keycode));
if ((fd = open(argv[1], O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY)) < 1) {
perror("error: cannot open input event device");
return 1;
}
if ((rv = ioctl(fd, EVIOCSKEYCODE, &map)) < 0) {
perror("error: mapping key");
close(fd);
return 1;
}
close(fd);
return 0;
}
#define NE(e) [e] = #e
static const char * const keys[KEY_MAX + 1] = {
[0 ... KEY_MAX] = NULL,
NE(KEY_RESERVED),
NE(KEY_ESC),
NE(KEY_1),
NE(KEY_2),
NE(KEY_3),
NE(KEY_4),
NE(KEY_5),
NE(KEY_6),
NE(KEY_7),
NE(KEY_8),
NE(KEY_9),
NE(KEY_0),
NE(KEY_MINUS),
NE(KEY_EQUAL),
NE(KEY_BACKSPACE),
NE(KEY_TAB),
NE(KEY_Q),
NE(KEY_W),
NE(KEY_E),
NE(KEY_R),
NE(KEY_T),
NE(KEY_Y),
NE(KEY_U),
NE(KEY_I),
NE(KEY_O),
NE(KEY_P),
NE(KEY_LEFTBRACE),
NE(KEY_RIGHTBRACE),
NE(KEY_ENTER),
NE(KEY_LEFTCTRL),
NE(KEY_A),
NE(KEY_S),
NE(KEY_D),
NE(KEY_F),
NE(KEY_G),
NE(KEY_H),
NE(KEY_J),
NE(KEY_K),
NE(KEY_L),
NE(KEY_SEMICOLON),
NE(KEY_APOSTROPHE),
NE(KEY_GRAVE),
NE(KEY_LEFTSHIFT),
NE(KEY_BACKSLASH),
NE(KEY_Z),
NE(KEY_X),
NE(KEY_C),
NE(KEY_V),
NE(KEY_B),
NE(KEY_N),
NE(KEY_M),
NE(KEY_COMMA),
NE(KEY_DOT),
NE(KEY_SLASH),
NE(KEY_RIGHTSHIFT),
NE(KEY_KPASTERISK),
NE(KEY_LEFTALT),
NE(KEY_SPACE),
NE(KEY_CAPSLOCK),
NE(KEY_F1),
NE(KEY_F2),
NE(KEY_F3),
NE(KEY_F4),
NE(KEY_F5),
NE(KEY_F6),
NE(KEY_F7),
NE(KEY_F8),
NE(KEY_F9),
NE(KEY_F10),
NE(KEY_NUMLOCK),
NE(KEY_SCROLLLOCK),
NE(KEY_KP7),
NE(KEY_KP8),
NE(KEY_KP9),
NE(KEY_KPMINUS),
NE(KEY_KP4),
NE(KEY_KP5),
NE(KEY_KP6),
NE(KEY_KPPLUS),
NE(KEY_KP1),
NE(KEY_KP2),
NE(KEY_KP3),
NE(KEY_KP0),
NE(KEY_KPDOT),
NE(KEY_ZENKAKUHANKAKU),
NE(KEY_102ND),
NE(KEY_F11),
NE(KEY_F12),
NE(KEY_RO),
NE(KEY_KATAKANA),
NE(KEY_HIRAGANA),
NE(KEY_HENKAN),
NE(KEY_KATAKANAHIRAGANA),
NE(KEY_MUHENKAN),
NE(KEY_KPJPCOMMA),
NE(KEY_KPENTER),
NE(KEY_RIGHTCTRL),
NE(KEY_KPSLASH),
NE(KEY_SYSRQ),
NE(KEY_RIGHTALT),
NE(KEY_LINEFEED),
NE(KEY_HOME),
NE(KEY_UP),
NE(KEY_PAGEUP),
NE(KEY_LEFT),
NE(KEY_RIGHT),
NE(KEY_END),
NE(KEY_DOWN),
NE(KEY_PAGEDOWN),
NE(KEY_INSERT),
NE(KEY_DELETE),
NE(KEY_MACRO),
NE(KEY_MUTE),
NE(KEY_VOLUMEDOWN),
NE(KEY_VOLUMEUP),
NE(KEY_POWER),
NE(KEY_KPEQUAL),
NE(KEY_KPPLUSMINUS),
NE(KEY_PAUSE),
NE(KEY_KPCOMMA),
NE(KEY_HANGUEL),
NE(KEY_HANJA),
NE(KEY_YEN),
NE(KEY_LEFTMETA),
NE(KEY_RIGHTMETA),
NE(KEY_COMPOSE),
NE(KEY_STOP),
NE(KEY_AGAIN),
NE(KEY_PROPS),
NE(KEY_UNDO),
NE(KEY_FRONT),
NE(KEY_COPY),
NE(KEY_OPEN),
NE(KEY_PASTE),
NE(KEY_FIND),
NE(KEY_CUT),
NE(KEY_HELP),
NE(KEY_MENU),
NE(KEY_CALC),
NE(KEY_SETUP),
NE(KEY_SLEEP),
NE(KEY_WAKEUP),
NE(KEY_FILE),
NE(KEY_SENDFILE),
NE(KEY_DELETEFILE),
NE(KEY_XFER),
NE(KEY_PROG1),
NE(KEY_PROG2),
NE(KEY_WWW),
NE(KEY_MSDOS),
NE(KEY_COFFEE),
NE(KEY_DIRECTION),
NE(KEY_CYCLEWINDOWS),
NE(KEY_MAIL),
NE(KEY_BOOKMARKS),
NE(KEY_COMPUTER),
NE(KEY_BACK),
NE(KEY_FORWARD),
NE(KEY_CLOSECD),
NE(KEY_EJECTCD),
NE(KEY_EJECTCLOSECD),
NE(KEY_NEXTSONG),
NE(KEY_PLAYPAUSE),
NE(KEY_PREVIOUSSONG),
NE(KEY_STOPCD),
NE(KEY_RECORD),
NE(KEY_REWIND),
NE(KEY_PHONE),
NE(KEY_ISO),
NE(KEY_CONFIG),
NE(KEY_HOMEPAGE),
NE(KEY_REFRESH),
NE(KEY_EXIT),
NE(KEY_MOVE),
NE(KEY_EDIT),
NE(KEY_SCROLLUP),
NE(KEY_SCROLLDOWN),
NE(KEY_KPLEFTPAREN),
NE(KEY_KPRIGHTPAREN),
NE(KEY_F13),
NE(KEY_F14),
NE(KEY_F15),
NE(KEY_F16),
NE(KEY_F17),
NE(KEY_F18),
NE(KEY_F19),
NE(KEY_F20),
NE(KEY_F21),
NE(KEY_F22),
NE(KEY_F23),
NE(KEY_F24),
NE(KEY_PLAYCD),
NE(KEY_PAUSECD),
NE(KEY_PROG3),
NE(KEY_PROG4),
NE(KEY_SUSPEND),
NE(KEY_CLOSE),
NE(KEY_PLAY),
NE(KEY_FASTFORWARD),
NE(KEY_BASSBOOST),
NE(KEY_PRINT),
NE(KEY_HP),
NE(KEY_CAMERA),
NE(KEY_SOUND),
NE(KEY_QUESTION),
NE(KEY_EMAIL),
NE(KEY_CHAT),
NE(KEY_SEARCH),
NE(KEY_CONNECT),
NE(KEY_FINANCE),
NE(KEY_SPORT),
NE(KEY_SHOP),
NE(KEY_ALTERASE),
NE(KEY_CANCEL),
NE(KEY_BRIGHTNESSDOWN),
NE(KEY_BRIGHTNESSUP),
NE(KEY_MEDIA),
NE(KEY_UNKNOWN),
NE(KEY_OK),
NE(KEY_SELECT),
NE(KEY_GOTO),
NE(KEY_CLEAR),
NE(KEY_POWER2),
NE(KEY_OPTION),
NE(KEY_INFO),
NE(KEY_TIME),
NE(KEY_VENDOR),
NE(KEY_ARCHIVE),
NE(KEY_PROGRAM),
NE(KEY_CHANNEL),
NE(KEY_FAVORITES),
NE(KEY_EPG),
NE(KEY_PVR),
NE(KEY_MHP),
NE(KEY_LANGUAGE),
NE(KEY_TITLE),
NE(KEY_SUBTITLE),
NE(KEY_ANGLE),
NE(KEY_ZOOM),
NE(KEY_MODE),
NE(KEY_KEYBOARD),
NE(KEY_SCREEN),
NE(KEY_PC),
NE(KEY_TV),
NE(KEY_TV2),
NE(KEY_VCR),
NE(KEY_VCR2),
NE(KEY_SAT),
NE(KEY_SAT2),
NE(KEY_CD),
NE(KEY_TAPE),
NE(KEY_RADIO),
NE(KEY_TUNER),
NE(KEY_PLAYER),
NE(KEY_TEXT),
NE(KEY_DVD),
NE(KEY_AUX),
NE(KEY_MP3),
NE(KEY_AUDIO),
NE(KEY_VIDEO),
NE(KEY_DIRECTORY),
NE(KEY_LIST),
NE(KEY_MEMO),
NE(KEY_CALENDAR),
NE(KEY_RED),
NE(KEY_GREEN),
NE(KEY_YELLOW),
NE(KEY_BLUE),
NE(KEY_CHANNELUP),
NE(KEY_CHANNELDOWN),
NE(KEY_FIRST),
NE(KEY_LAST),
NE(KEY_AB),
NE(KEY_NEXT),
NE(KEY_RESTART),
NE(KEY_SLOW),
NE(KEY_SHUFFLE),
NE(KEY_BREAK),
NE(KEY_PREVIOUS),
NE(KEY_DIGITS),
NE(KEY_TEEN),
NE(KEY_TWEN),
NE(KEY_DEL_EOL),
NE(KEY_DEL_EOS),
NE(KEY_INS_LINE),
NE(KEY_DEL_LINE),
NE(KEY_VIDEOPHONE),
NE(KEY_GAMES),
NE(KEY_ZOOMIN),
NE(KEY_ZOOMOUT),
NE(KEY_ZOOMRESET),
NE(KEY_WORDPROCESSOR),
NE(KEY_EDITOR),
NE(KEY_SPREADSHEET),
NE(KEY_GRAPHICSEDITOR),
NE(KEY_PRESENTATION),
NE(KEY_DATABASE),
NE(KEY_NEWS),
NE(KEY_VOICEMAIL),
NE(KEY_ADDRESSBOOK),
NE(KEY_MESSENGER),
NE(KEY_DISPLAYTOGGLE),
#ifdef KEY_SPELLCHECK
NE(KEY_SPELLCHECK),
#endif
#ifdef KEY_LOGOFF
NE(KEY_LOGOFF),
#endif
#ifdef KEY_DOLLAR
NE(KEY_DOLLAR),
#endif
#ifdef KEY_EURO
NE(KEY_EURO),
#endif
#ifdef KEY_FRAMEBACK
NE(KEY_FRAMEBACK),
#endif
#ifdef KEY_FRAMEFORWARD
NE(KEY_FRAMEFORWARD),
#endif
#ifdef KEY_CONTEXT_MENU
NE(KEY_CONTEXT_MENU),
#endif
#ifdef KEY_MEDIA_REPEAT
NE(KEY_MEDIA_REPEAT),
#endif
#ifdef KEY_10CHANNELSUP
NE(KEY_10CHANNELSUP),
#endif
#ifdef KEY_10CHANNELSDOWN
NE(KEY_10CHANNELSDOWN),
#endif
#ifdef KEY_IMAGES
NE(KEY_IMAGES),
#endif
NE(KEY_DEL_EOL),
NE(KEY_DEL_EOS),
NE(KEY_INS_LINE),
NE(KEY_DEL_LINE),
NE(KEY_FN),
NE(KEY_FN_ESC),
NE(KEY_FN_F1),
NE(KEY_FN_F2),
NE(KEY_FN_F3),
NE(KEY_FN_F4),
NE(KEY_FN_F5),
NE(KEY_FN_F6),
NE(KEY_FN_F7),
NE(KEY_FN_F8),
NE(KEY_FN_F9),
NE(KEY_FN_F10),
NE(KEY_FN_F11),
NE(KEY_FN_F12),
NE(KEY_FN_1),
NE(KEY_FN_2),
NE(KEY_FN_D),
NE(KEY_FN_E),
NE(KEY_FN_F),
NE(KEY_FN_S),
NE(KEY_FN_B),
NE(KEY_BRL_DOT1),
NE(KEY_BRL_DOT2),
NE(KEY_BRL_DOT3),
NE(KEY_BRL_DOT4),
NE(KEY_BRL_DOT5),
NE(KEY_BRL_DOT6),
NE(KEY_BRL_DOT7),
NE(KEY_BRL_DOT8),
NE(KEY_BRL_DOT9),
NE(KEY_BRL_DOT10),
#ifdef KEY_NUMERIC_0
NE(KEY_NUMERIC_0),
NE(KEY_NUMERIC_1),
NE(KEY_NUMERIC_2),
NE(KEY_NUMERIC_3),
NE(KEY_NUMERIC_4),
NE(KEY_NUMERIC_5),
NE(KEY_NUMERIC_6),
NE(KEY_NUMERIC_7),
NE(KEY_NUMERIC_8),
NE(KEY_NUMERIC_9),
NE(KEY_NUMERIC_STAR),
NE(KEY_NUMERIC_POUND),
#endif
NE(KEY_BATTERY),
NE(KEY_BLUETOOTH),
NE(KEY_BRIGHTNESS_CYCLE),
NE(KEY_BRIGHTNESS_ZERO),
#ifdef KEY_DASHBOARD
NE(KEY_DASHBOARD),
#endif
NE(KEY_DISPLAY_OFF),
NE(KEY_DOCUMENTS),
NE(KEY_FORWARDMAIL),
NE(KEY_NEW),
NE(KEY_KBDILLUMDOWN),
NE(KEY_KBDILLUMUP),
NE(KEY_KBDILLUMTOGGLE),
NE(KEY_REDO),
NE(KEY_REPLY),
NE(KEY_SAVE),
#ifdef KEY_SCALE
NE(KEY_SCALE),
#endif
NE(KEY_SEND),
NE(KEY_SCREENLOCK),
NE(KEY_SWITCHVIDEOMODE),
#ifdef KEY_UWB
NE(KEY_UWB),
#endif
#ifdef KEY_VIDEO_NEXT
NE(KEY_VIDEO_NEXT),
#endif
#ifdef KEY_VIDEO_PREV
NE(KEY_VIDEO_PREV),
#endif
#ifdef KEY_WIMAX
NE(KEY_WIMAX),
#endif
#ifdef KEY_WLAN
NE(KEY_WLAN),
#endif
#ifdef KEY_RFKILL
NE(KEY_RFKILL),
#endif
#ifdef KEY_MICMUTE
NE(KEY_MICMUTE),
#endif
#ifdef KEY_CAMERA_FOCUS
NE(KEY_CAMERA_FOCUS),
#endif
#ifdef KEY_WPS_BUTTON
NE(KEY_WPS_BUTTON),
#endif
#ifdef KEY_TOUCHPAD_TOGGLE
NE(KEY_TOUCHPAD_TOGGLE),
NE(KEY_TOUCHPAD_ON),
NE(KEY_TOUCHPAD_OFF),
#endif
#ifdef KEY_CAMERA_ZOOMIN
NE(KEY_CAMERA_ZOOMIN),
NE(KEY_CAMERA_ZOOMOUT),
NE(KEY_CAMERA_UP),
NE(KEY_CAMERA_DOWN),
NE(KEY_CAMERA_LEFT),
NE(KEY_CAMERA_RIGHT),
#endif
#ifdef KEY_ATTENDANT_ON
NE(KEY_ATTENDANT_ON),
NE(KEY_ATTENDANT_OFF),
NE(KEY_ATTENDANT_TOGGLE),
NE(KEY_LIGHTS_TOGGLE),
#endif
NE(BTN_0),
NE(BTN_1),
NE(BTN_2),
NE(BTN_3),
NE(BTN_4),
NE(BTN_5),
NE(BTN_6),
NE(BTN_7),
NE(BTN_8),
NE(BTN_9),
NE(BTN_LEFT),
NE(BTN_RIGHT),
NE(BTN_MIDDLE),
NE(BTN_SIDE),
NE(BTN_EXTRA),
NE(BTN_FORWARD),
NE(BTN_BACK),
NE(BTN_TASK),
NE(BTN_TRIGGER),
NE(BTN_THUMB),
NE(BTN_THUMB2),
NE(BTN_TOP),
NE(BTN_TOP2),
NE(BTN_PINKIE),
NE(BTN_BASE),
NE(BTN_BASE2),
NE(BTN_BASE3),
NE(BTN_BASE4),
NE(BTN_BASE5),
NE(BTN_BASE6),
NE(BTN_DEAD),
NE(BTN_C),
#ifdef BTN_SOUTH
NE(BTN_SOUTH),
NE(BTN_EAST),
NE(BTN_NORTH),
NE(BTN_WEST),
#else
NE(BTN_A),
NE(BTN_B),
NE(BTN_X),
NE(BTN_Y),
#endif
NE(BTN_Z),
NE(BTN_TL),
NE(BTN_TR),
NE(BTN_TL2),
NE(BTN_TR2),
NE(BTN_SELECT),
NE(BTN_START),
NE(BTN_MODE),
NE(BTN_THUMBL),
NE(BTN_THUMBR),
NE(BTN_TOOL_PEN),
NE(BTN_TOOL_RUBBER),
NE(BTN_TOOL_BRUSH),
NE(BTN_TOOL_PENCIL),
NE(BTN_TOOL_AIRBRUSH),
NE(BTN_TOOL_FINGER),
NE(BTN_TOOL_MOUSE),
NE(BTN_TOOL_LENS),
NE(BTN_TOUCH),
NE(BTN_STYLUS),
NE(BTN_STYLUS2),
NE(BTN_TOOL_DOUBLETAP),
NE(BTN_TOOL_TRIPLETAP),
#ifdef BTN_TOOL_QUADTAP
NE(BTN_TOOL_QUADTAP),
#endif
NE(BTN_GEAR_DOWN),
NE(BTN_GEAR_UP),
#ifdef BTN_DPAD_UP
NE(BTN_DPAD_UP),
NE(BTN_DPAD_DOWN),
NE(BTN_DPAD_LEFT),
NE(BTN_DPAD_RIGHT),
#endif
#ifdef KEY_ALS_TOGGLE
NE(KEY_ALS_TOGGLE),
#endif
#ifdef KEY_BUTTONCONFIG
NE(KEY_BUTTONCONFIG),
#endif
#ifdef KEY_TASKMANAGER
NE(KEY_TASKMANAGER),
#endif
#ifdef KEY_JOURNAL
NE(KEY_JOURNAL),
#endif
#ifdef KEY_CONTROLPANEL
NE(KEY_CONTROLPANEL),
#endif
#ifdef KEY_APPSELECT
NE(KEY_APPSELECT),
#endif
#ifdef KEY_SCREENSAVER
NE(KEY_SCREENSAVER),
#endif
#ifdef KEY_VOICECOMMAND
NE(KEY_VOICECOMMAND),
#endif
#ifdef KEY_BRIGHTNESS_MIN
NE(KEY_BRIGHTNESS_MIN),
#endif
#ifdef KEY_BRIGHTNESS_MAX
NE(KEY_BRIGHTNESS_MAX),
#endif
#ifdef KEY_KBDINPUTASSIST_PREV
NE(KEY_KBDINPUTASSIST_PREV),
#endif
#ifdef KEY_KBDINPUTASSIST_NEXT
NE(KEY_KBDINPUTASSIST_NEXT),
#endif
#ifdef KEY_KBDINPUTASSIST_PREVGROUP
NE(KEY_KBDINPUTASSIST_PREVGROUP),
#endif
#ifdef KEY_KBDINPUTASSIST_NEXTGROUP
NE(KEY_KBDINPUTASSIST_NEXTGROUP),
#endif
#ifdef KEY_KBDINPUTASSIST_ACCEPT
NE(KEY_KBDINPUTASSIST_ACCEPT),
#endif
#ifdef KEY_KBDINPUTASSIST_CANCEL
NE(KEY_KBDINPUTASSIST_CANCEL),
#endif
};
const char * get_keyname(unsigned keycode)
{
if (keycode < KEY_MAX)
return keys[keycode];
return NULL;
}
unsigned get_keycode(const char *name)
{
unsigned i;
if (name == NULL) {
return 0;
}
for (i = 0; i < KEY_MAX; i++) {
if (keys[i] && !strcasecmp(name, keys[i])) {
return i;
}
}
fprintf(stderr, "error: unknown key \"%s\"\n", name);
return 0;
}
unsigned get_scancode(const char *str)
{
char *endptr;
unsigned scancode;
if (str == NULL) {
return 0;
}
scancode = strtoul(str, &endptr, 16);
if (*endptr != '\0' || scancode == 0) {
fprintf(stderr, "error: invalid scancode\n");
return 0;
}
return scancode;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment