1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Changing default Remote Controller mappings</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="LINUX MEDIA INFRASTRUCTURE API"><link rel="up" href="remote_controllers.html" title="Chapter 17. Remote Controllers"><link rel="prev" href="Remote_controllers_tables.html" title="Remote controller tables"><link rel="next" href="lirc_dev.html" title="LIRC Device Interface"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Changing default Remote Controller mappings</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="Remote_controllers_tables.html">Prev</a> </td><th width="60%" align="center">Chapter 17. Remote Controllers</th><td width="20%" align="right"> <a accesskey="n" href="lirc_dev.html">Next</a></td></tr></table><hr></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="Remote_controllers_table_change"></a>Changing default Remote Controller mappings</h2></div></div></div><p>The event interface provides two ioctls to be used against 2the /dev/input/event device, to allow changing the default 3keymapping.</p><p>This program demonstrates how to replace the keymap tables.</p><pre class="programlisting"> 4/* keytable.c - This program allows checking/replacing keys at IR 5 6 Copyright (C) 2006-2009 Mauro Carvalho Chehab <mchehab@infradead.org> 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation, version 2 of the License. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 */ 17 18#include <ctype.h> 19#include <errno.h> 20#include <fcntl.h> 21#include <stdio.h> 22#include <stdlib.h> 23#include <string.h> 24#include <linux/input.h> 25#include <sys/ioctl.h> 26 27#include "parse.h" 28 29void prtcode (int *codes) 30{ 31 struct parse_key *p; 32 33 for (p=keynames;p->name!=NULL;p++) { 34 if (p->value == (unsigned)codes[1]) { 35 printf("scancode 0x%04x = %s (0x%02x)\n", codes[0], p->name, codes[1]); 36 return; 37 } 38 } 39 40 if (isprint (codes[1])) 41 printf("scancode %d = '%c' (0x%02x)\n", codes[0], codes[1], codes[1]); 42 else 43 printf("scancode %d = 0x%02x\n", codes[0], codes[1]); 44} 45 46int parse_code(char *string) 47{ 48 struct parse_key *p; 49 50 for (p=keynames;p->name!=NULL;p++) { 51 if (!strcasecmp(p->name, string)) { 52 return p->value; 53 } 54 } 55 return -1; 56} 57 58int main (int argc, char *argv[]) 59{ 60 int fd; 61 unsigned int i, j; 62 int codes[2]; 63 64 if (argc<2 || argc>4) { 65 printf ("usage: %s <device> to get table; or\n" 66 " %s <device> <scancode> <keycode>\n" 67 " %s <device> <keycode_file>\n",*argv,*argv,*argv); 68 return -1; 69 } 70 71 if ((fd = open(argv[1], O_RDONLY)) < 0) { 72 perror("Couldn't open input device"); 73 return(-1); 74 } 75 76 if (argc==4) { 77 int value; 78 79 value=parse_code(argv[3]); 80 81 if (value==-1) { 82 value = strtol(argv[3], NULL, 0); 83 if (errno) 84 perror("value"); 85 } 86 87 codes [0] = (unsigned) strtol(argv[2], NULL, 0); 88 codes [1] = (unsigned) value; 89 90 if(ioctl(fd, EVIOCSKEYCODE, codes)) 91 perror ("EVIOCSKEYCODE"); 92 93 if(ioctl(fd, EVIOCGKEYCODE, codes)==0) 94 prtcode(codes); 95 return 0; 96 } 97 98 if (argc==3) { 99 FILE *fin; 100 int value; 101 char *scancode, *keycode, s[2048]; 102 103 fin=fopen(argv[2],"r"); 104 if (fin==NULL) { 105 perror ("opening keycode file"); 106 return -1; 107 } 108 109 /* Clears old table */ 110 for (j = 0; j < 256; j++) { 111 for (i = 0; i < 256; i++) { 112 codes[0] = (j << 8) | i; 113 codes[1] = KEY_RESERVED; 114 ioctl(fd, EVIOCSKEYCODE, codes); 115 } 116 } 117 118 while (fgets(s,sizeof(s),fin)) { 119 scancode=strtok(s,"\n\t =:"); 120 if (!scancode) { 121 perror ("parsing input file scancode"); 122 return -1; 123 } 124 if (!strcasecmp(scancode, "scancode")) { 125 scancode = strtok(NULL,"\n\t =:"); 126 if (!scancode) { 127 perror ("parsing input file scancode"); 128 return -1; 129 } 130 } 131 132 keycode=strtok(NULL,"\n\t =:("); 133 if (!keycode) { 134 perror ("parsing input file keycode"); 135 return -1; 136 } 137 138 // printf ("parsing %s=%s:", scancode, keycode); 139 value=parse_code(keycode); 140 // printf ("\tvalue=%d\n",value); 141 142 if (value==-1) { 143 value = strtol(keycode, NULL, 0); 144 if (errno) 145 perror("value"); 146 } 147 148 codes [0] = (unsigned) strtol(scancode, NULL, 0); 149 codes [1] = (unsigned) value; 150 151 // printf("\t%04x=%04x\n",codes[0], codes[1]); 152 if(ioctl(fd, EVIOCSKEYCODE, codes)) { 153 fprintf(stderr, "Setting scancode 0x%04x with 0x%04x via ",codes[0], codes[1]); 154 perror ("EVIOCSKEYCODE"); 155 } 156 157 if(ioctl(fd, EVIOCGKEYCODE, codes)==0) 158 prtcode(codes); 159 } 160 return 0; 161 } 162 163 /* Get scancode table */ 164 for (j = 0; j < 256; j++) { 165 for (i = 0; i < 256; i++) { 166 codes[0] = (j << 8) | i; 167 if (!ioctl(fd, EVIOCGKEYCODE, codes) && codes[1] != KEY_RESERVED) 168 prtcode(codes); 169 } 170 } 171 return 0; 172} 173 174</pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="Remote_controllers_tables.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="remote_controllers.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="lirc_dev.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Remote controller tables </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> LIRC Device Interface</td></tr></table></div></body></html> 175