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