^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) file: uapi/v4l/keytable.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) =========================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /* keytable.c - This program allows checking/replacing keys at IR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) Copyright (C) 2006-2009 Mauro Carvalho Chehab <mchehab@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) the Free Software Foundation, version 2 of the License.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "parse.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) void prtcode (int *codes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct parse_key *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) for (p=keynames;p->name!=NULL;p++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (p->value == (unsigned)codes[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) printf("scancode 0x%04x = %s (0x%02x)\\n", codes[0], p->name, codes[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (isprint (codes[1]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) printf("scancode %d = '%c' (0x%02x)\\n", codes[0], codes[1], codes[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) printf("scancode %d = 0x%02x\\n", codes[0], codes[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int parse_code(char *string)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct parse_key *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) for (p=keynames;p->name!=NULL;p++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (!strcasecmp(p->name, string)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return p->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int main (int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int codes[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (argc<2 || argc>4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) printf ("usage: %s <device> to get table; or\\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) " %s <device> <scancode> <keycode>\\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) " %s <device> <keycode_file>n",*argv,*argv,*argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if ((fd = open(argv[1], O_RDONLY)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) perror("Couldn't open input device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return(-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (argc==4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) value=parse_code(argv[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (value==-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) value = strtol(argv[3], NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (errno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) perror("value");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) codes [0] = (unsigned) strtol(argv[2], NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) codes [1] = (unsigned) value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if(ioctl(fd, EVIOCSKEYCODE, codes))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) perror ("EVIOCSKEYCODE");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if(ioctl(fd, EVIOCGKEYCODE, codes)==0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) prtcode(codes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (argc==3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) FILE *fin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) char *scancode, *keycode, s[2048];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) fin=fopen(argv[2],"r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (fin==NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) perror ("opening keycode file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* Clears old table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) for (j = 0; j < 256; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) for (i = 0; i < 256; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) codes[0] = (j << 8) | i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) codes[1] = KEY_RESERVED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ioctl(fd, EVIOCSKEYCODE, codes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) while (fgets(s,sizeof(s),fin)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) scancode=strtok(s,"\\n\\t =:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (!scancode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) perror ("parsing input file scancode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (!strcasecmp(scancode, "scancode")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) scancode = strtok(NULL,"\\n\\t =:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (!scancode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) perror ("parsing input file scancode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) keycode=strtok(NULL,"\\n\\t =:(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!keycode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) perror ("parsing input file keycode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) // printf ("parsing %s=%s:", scancode, keycode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) value=parse_code(keycode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) // printf ("\\tvalue=%d\\n",value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (value==-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) value = strtol(keycode, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (errno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) perror("value");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) codes [0] = (unsigned) strtol(scancode, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) codes [1] = (unsigned) value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) // printf("\\t%04x=%04x\\n",codes[0], codes[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if(ioctl(fd, EVIOCSKEYCODE, codes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) fprintf(stderr, "Setting scancode 0x%04x with 0x%04x via ",codes[0], codes[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) perror ("EVIOCSKEYCODE");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if(ioctl(fd, EVIOCGKEYCODE, codes)==0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) prtcode(codes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* Get scancode table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) for (j = 0; j < 256; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) for (i = 0; i < 256; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) codes[0] = (j << 8) | i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!ioctl(fd, EVIOCGKEYCODE, codes) && codes[1] != KEY_RESERVED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) prtcode(codes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }