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