keytable.c.xml 5.7 KB

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