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&#160;17.&#160;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>&#160;</td><th width="60%" align="center">Chapter&#160;17.&#160;Remote Controllers</th><td width="20%" align="right">&#160;<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 &lt;mchehab@infradead.org&gt;
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 &lt;ctype.h&gt;
19#include &lt;errno.h&gt;
20#include &lt;fcntl.h&gt;
21#include &lt;stdio.h&gt;
22#include &lt;stdlib.h&gt;
23#include &lt;string.h&gt;
24#include &lt;linux/input.h&gt;
25#include &lt;sys/ioctl.h&gt;
26
27#include "parse.h"
28
29void prtcode (int *codes)
30{
31        struct parse_key *p;
32
33        for (p=keynames;p-&gt;name!=NULL;p++) {
34                if (p-&gt;value == (unsigned)codes[1]) {
35                        printf("scancode 0x%04x = %s (0x%02x)\n", codes[0], p-&gt;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-&gt;name!=NULL;p++) {
51                if (!strcasecmp(p-&gt;name, string)) {
52                        return p-&gt;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&lt;2 || argc&gt;4) {
65                printf ("usage: %s &lt;device&gt; to get table; or\n"
66                        "       %s &lt;device&gt; &lt;scancode&gt; &lt;keycode&gt;\n"
67                        "       %s &lt;device&gt; &lt;keycode_file&gt;\n",*argv,*argv,*argv);
68                return -1;
69        }
70
71        if ((fd = open(argv[1], O_RDONLY)) &lt; 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 &lt; 256; j++) {
111                        for (i = 0; i &lt; 256; i++) {
112                                codes[0] = (j &lt;&lt; 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 &lt; 256; j++) {
165                for (i = 0; i &lt; 256; i++) {
166                        codes[0] = (j &lt;&lt; 8) | i;
167                        if (!ioctl(fd, EVIOCGKEYCODE, codes) &amp;&amp; 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>&#160;</td><td width="20%" align="center"><a accesskey="u" href="remote_controllers.html">Up</a></td><td width="40%" align="right">&#160;<a accesskey="n" href="lirc_dev.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Remote controller tables&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;LIRC Device Interface</td></tr></table></div></body></html>
175