0001 .. SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later
0002
0003 file: uapi/v4l/keytable.c
0004 =========================
0005
0006 .. code-block:: c
0007
0008 /* keytable.c - This program allows checking/replacing keys at IR
0009
0010 Copyright (C) 2006-2009 Mauro Carvalho Chehab <mchehab@kernel.org>
0011
0012 This program is free software; you can redistribute it and/or modify
0013 it under the terms of the GNU General Public License as published by
0014 the Free Software Foundation, version 2 of the License.
0015
0016 This program is distributed in the hope that it will be useful,
0017 but WITHOUT ANY WARRANTY; without even the implied warranty of
0018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0019 GNU General Public License for more details.
0020 */
0021
0022 #include <ctype.h>
0023 #include <errno.h>
0024 #include <fcntl.h>
0025 #include <stdio.h>
0026 #include <stdlib.h>
0027 #include <string.h>
0028 #include <linux/input.h>
0029 #include <sys/ioctl.h>
0030
0031 #include "parse.h"
0032
0033 void prtcode (int *codes)
0034 {
0035 struct parse_key *p;
0036
0037 for (p=keynames;p->name!=NULL;p++) {
0038 if (p->value == (unsigned)codes[1]) {
0039 printf("scancode 0x%04x = %s (0x%02x)\\n", codes[0], p->name, codes[1]);
0040 return;
0041 }
0042 }
0043
0044 if (isprint (codes[1]))
0045 printf("scancode %d = '%c' (0x%02x)\\n", codes[0], codes[1], codes[1]);
0046 else
0047 printf("scancode %d = 0x%02x\\n", codes[0], codes[1]);
0048 }
0049
0050 int parse_code(char *string)
0051 {
0052 struct parse_key *p;
0053
0054 for (p=keynames;p->name!=NULL;p++) {
0055 if (!strcasecmp(p->name, string)) {
0056 return p->value;
0057 }
0058 }
0059 return -1;
0060 }
0061
0062 int main (int argc, char *argv[])
0063 {
0064 int fd;
0065 unsigned int i, j;
0066 int codes[2];
0067
0068 if (argc<2 || argc>4) {
0069 printf ("usage: %s <device> to get table; or\\n"
0070 " %s <device> <scancode> <keycode>\\n"
0071 " %s <device> <keycode_file>n",*argv,*argv,*argv);
0072 return -1;
0073 }
0074
0075 if ((fd = open(argv[1], O_RDONLY)) < 0) {
0076 perror("Couldn't open input device");
0077 return(-1);
0078 }
0079
0080 if (argc==4) {
0081 int value;
0082
0083 value=parse_code(argv[3]);
0084
0085 if (value==-1) {
0086 value = strtol(argv[3], NULL, 0);
0087 if (errno)
0088 perror("value");
0089 }
0090
0091 codes [0] = (unsigned) strtol(argv[2], NULL, 0);
0092 codes [1] = (unsigned) value;
0093
0094 if(ioctl(fd, EVIOCSKEYCODE, codes))
0095 perror ("EVIOCSKEYCODE");
0096
0097 if(ioctl(fd, EVIOCGKEYCODE, codes)==0)
0098 prtcode(codes);
0099 return 0;
0100 }
0101
0102 if (argc==3) {
0103 FILE *fin;
0104 int value;
0105 char *scancode, *keycode, s[2048];
0106
0107 fin=fopen(argv[2],"r");
0108 if (fin==NULL) {
0109 perror ("opening keycode file");
0110 return -1;
0111 }
0112
0113 /* Clears old table */
0114 for (j = 0; j < 256; j++) {
0115 for (i = 0; i < 256; i++) {
0116 codes[0] = (j << 8) | i;
0117 codes[1] = KEY_RESERVED;
0118 ioctl(fd, EVIOCSKEYCODE, codes);
0119 }
0120 }
0121
0122 while (fgets(s,sizeof(s),fin)) {
0123 scancode=strtok(s,"\\n\\t =:");
0124 if (!scancode) {
0125 perror ("parsing input file scancode");
0126 return -1;
0127 }
0128 if (!strcasecmp(scancode, "scancode")) {
0129 scancode = strtok(NULL,"\\n\\t =:");
0130 if (!scancode) {
0131 perror ("parsing input file scancode");
0132 return -1;
0133 }
0134 }
0135
0136 keycode=strtok(NULL,"\\n\\t =:(");
0137 if (!keycode) {
0138 perror ("parsing input file keycode");
0139 return -1;
0140 }
0141
0142 // printf ("parsing %s=%s:", scancode, keycode);
0143 value=parse_code(keycode);
0144 // printf ("\\tvalue=%d\\n",value);
0145
0146 if (value==-1) {
0147 value = strtol(keycode, NULL, 0);
0148 if (errno)
0149 perror("value");
0150 }
0151
0152 codes [0] = (unsigned) strtol(scancode, NULL, 0);
0153 codes [1] = (unsigned) value;
0154
0155 // printf("\\t%04x=%04x\\n",codes[0], codes[1]);
0156 if(ioctl(fd, EVIOCSKEYCODE, codes)) {
0157 fprintf(stderr, "Setting scancode 0x%04x with 0x%04x via ",codes[0], codes[1]);
0158 perror ("EVIOCSKEYCODE");
0159 }
0160
0161 if(ioctl(fd, EVIOCGKEYCODE, codes)==0)
0162 prtcode(codes);
0163 }
0164 return 0;
0165 }
0166
0167 /* Get scancode table */
0168 for (j = 0; j < 256; j++) {
0169 for (i = 0; i < 256; i++) {
0170 codes[0] = (j << 8) | i;
0171 if (!ioctl(fd, EVIOCGKEYCODE, codes) && codes[1] != KEY_RESERVED)
0172 prtcode(codes);
0173 }
0174 }
0175 return 0;
0176 }