Path: blob/master/Documentation/DocBook/v4l/keytable.c.xml
10821 views
<programlisting>1/* keytable.c - This program allows checking/replacing keys at IR23Copyright (C) 2006-2009 Mauro Carvalho Chehab <[email protected]>45This program is free software; you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation, version 2 of the License.89This program is distributed in the hope that it will be useful,10but WITHOUT ANY WARRANTY; without even the implied warranty of11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12GNU General Public License for more details.13*/1415#include <ctype.h>16#include <errno.h>17#include <fcntl.h>18#include <stdio.h>19#include <stdlib.h>20#include <string.h>21#include <linux/input.h>22#include <sys/ioctl.h>2324#include "parse.h"2526void prtcode (int *codes)27{28struct parse_key *p;2930for (p=keynames;p->name!=NULL;p++) {31if (p->value == (unsigned)codes[1]) {32printf("scancode 0x%04x = %s (0x%02x)\n", codes[0], p->name, codes[1]);33return;34}35}3637if (isprint (codes[1]))38printf("scancode %d = '%c' (0x%02x)\n", codes[0], codes[1], codes[1]);39else40printf("scancode %d = 0x%02x\n", codes[0], codes[1]);41}4243int parse_code(char *string)44{45struct parse_key *p;4647for (p=keynames;p->name!=NULL;p++) {48if (!strcasecmp(p->name, string)) {49return p->value;50}51}52return -1;53}5455int main (int argc, char *argv[])56{57int fd;58unsigned int i, j;59int codes[2];6061if (argc<2 || argc>4) {62printf ("usage: %s <device> to get table; or\n"63" %s <device> <scancode> <keycode>\n"64" %s <device> <keycode_file>\n",*argv,*argv,*argv);65return -1;66}6768if ((fd = open(argv[1], O_RDONLY)) < 0) {69perror("Couldn't open input device");70return(-1);71}7273if (argc==4) {74int value;7576value=parse_code(argv[3]);7778if (value==-1) {79value = strtol(argv[3], NULL, 0);80if (errno)81perror("value");82}8384codes [0] = (unsigned) strtol(argv[2], NULL, 0);85codes [1] = (unsigned) value;8687if(ioctl(fd, EVIOCSKEYCODE, codes))88perror ("EVIOCSKEYCODE");8990if(ioctl(fd, EVIOCGKEYCODE, codes)==0)91prtcode(codes);92return 0;93}9495if (argc==3) {96FILE *fin;97int value;98char *scancode, *keycode, s[2048];99100fin=fopen(argv[2],"r");101if (fin==NULL) {102perror ("opening keycode file");103return -1;104}105106/* Clears old table */107for (j = 0; j < 256; j++) {108for (i = 0; i < 256; i++) {109codes[0] = (j << 8) | i;110codes[1] = KEY_RESERVED;111ioctl(fd, EVIOCSKEYCODE, codes);112}113}114115while (fgets(s,sizeof(s),fin)) {116scancode=strtok(s,"\n\t =:");117if (!scancode) {118perror ("parsing input file scancode");119return -1;120}121if (!strcasecmp(scancode, "scancode")) {122scancode = strtok(NULL,"\n\t =:");123if (!scancode) {124perror ("parsing input file scancode");125return -1;126}127}128129keycode=strtok(NULL,"\n\t =:(");130if (!keycode) {131perror ("parsing input file keycode");132return -1;133}134135// printf ("parsing %s=%s:", scancode, keycode);136value=parse_code(keycode);137// printf ("\tvalue=%d\n",value);138139if (value==-1) {140value = strtol(keycode, NULL, 0);141if (errno)142perror("value");143}144145codes [0] = (unsigned) strtol(scancode, NULL, 0);146codes [1] = (unsigned) value;147148// printf("\t%04x=%04x\n",codes[0], codes[1]);149if(ioctl(fd, EVIOCSKEYCODE, codes)) {150fprintf(stderr, "Setting scancode 0x%04x with 0x%04x via ",codes[0], codes[1]);151perror ("EVIOCSKEYCODE");152}153154if(ioctl(fd, EVIOCGKEYCODE, codes)==0)155prtcode(codes);156}157return 0;158}159160/* Get scancode table */161for (j = 0; j < 256; j++) {162for (i = 0; i < 256; i++) {163codes[0] = (j << 8) | i;164if (!ioctl(fd, EVIOCGKEYCODE, codes) && codes[1] != KEY_RESERVED)165prtcode(codes);166}167}168return 0;169}170171</programlisting>172173174