include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / Documentation / DocBook / media / v4l / keytable.c.xml
blobd53254a3be159541b02bdda0b6bf1994b37cae52
1 <programlisting>
2 /* keytable.c - This program allows checking/replacing keys at IR
4    Copyright (C) 2006-2009 Mauro Carvalho Chehab &lt;mchehab@infradead.org&gt;
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.
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  */
16 #include &lt;ctype.h&gt;
17 #include &lt;errno.h&gt;
18 #include &lt;fcntl.h&gt;
19 #include &lt;stdio.h&gt;
20 #include &lt;stdlib.h&gt;
21 #include &lt;string.h&gt;
22 #include &lt;linux/input.h&gt;
23 #include &lt;sys/ioctl.h&gt;
25 #include "parse.h"
27 void prtcode (int *codes)
29         struct parse_key *p;
31         for (p=keynames;p-&gt;name!=NULL;p++) {
32                 if (p-&gt;value == (unsigned)codes[1]) {
33                         printf("scancode 0x%04x = %s (0x%02x)\n", codes[0], p-&gt;name, codes[1]);
34                         return;
35                 }
36         }
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]);
44 int parse_code(char *string)
46         struct parse_key *p;
48         for (p=keynames;p-&gt;name!=NULL;p++) {
49                 if (!strcasecmp(p-&gt;name, string)) {
50                         return p-&gt;value;
51                 }
52         }
53         return -1;
56 int main (int argc, char *argv[])
58         int fd;
59         unsigned int i, j;
60         int codes[2];
62         if (argc&lt;2 || argc&gt;4) {
63                 printf ("usage: %s &lt;device&gt; to get table; or\n"
64                         "       %s &lt;device&gt; &lt;scancode&gt; &lt;keycode&gt;\n"
65                         "       %s &lt;device&gt; &lt;keycode_file&gt;\n",*argv,*argv,*argv);
66                 return -1;
67         }
69         if ((fd = open(argv[1], O_RDONLY)) &lt; 0) {
70                 perror("Couldn't open input device");
71                 return(-1);
72         }
74         if (argc==4) {
75                 int value;
77                 value=parse_code(argv[3]);
79                 if (value==-1) {
80                         value = strtol(argv[3], NULL, 0);
81                         if (errno)
82                                 perror("value");
83                 }
85                 codes [0] = (unsigned) strtol(argv[2], NULL, 0);
86                 codes [1] = (unsigned) value;
88                 if(ioctl(fd, EVIOCSKEYCODE, codes))
89                         perror ("EVIOCSKEYCODE");
91                 if(ioctl(fd, EVIOCGKEYCODE, codes)==0)
92                         prtcode(codes);
93                 return 0;
94         }
96         if (argc==3) {
97                 FILE *fin;
98                 int value;
99                 char *scancode, *keycode, s[2048];
101                 fin=fopen(argv[2],"r");
102                 if (fin==NULL) {
103                         perror ("opening keycode file");
104                         return -1;
105                 }
107                 /* Clears old table */
108                 for (j = 0; j &lt; 256; j++) {
109                         for (i = 0; i &lt; 256; i++) {
110                                 codes[0] = (j &lt;&lt; 8) | i;
111                                 codes[1] = KEY_RESERVED;
112                                 ioctl(fd, EVIOCSKEYCODE, codes);
113                         }
114                 }
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                         }
130                         keycode=strtok(NULL,"\n\t =:(");
131                         if (!keycode) {
132                                 perror ("parsing input file keycode");
133                                 return -1;
134                         }
136                         // printf ("parsing %s=%s:", scancode, keycode);
137                         value=parse_code(keycode);
138                         // printf ("\tvalue=%d\n",value);
140                         if (value==-1) {
141                                 value = strtol(keycode, NULL, 0);
142                                 if (errno)
143                                         perror("value");
144                         }
146                         codes [0] = (unsigned) strtol(scancode, NULL, 0);
147                         codes [1] = (unsigned) value;
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                         }
155                         if(ioctl(fd, EVIOCGKEYCODE, codes)==0)
156                                 prtcode(codes);
157                 }
158                 return 0;
159         }
161         /* Get scancode table */
162         for (j = 0; j &lt; 256; j++) {
163                 for (i = 0; i &lt; 256; i++) {
164                         codes[0] = (j &lt;&lt; 8) | i;
165                         if (!ioctl(fd, EVIOCGKEYCODE, codes) &amp;&amp; codes[1] != KEY_RESERVED)
166                                 prtcode(codes);
167                 }
168         }
169         return 0;
172 </programlisting>