keytable.c.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. .. SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later
  2. file: uapi/v4l/keytable.c
  3. =========================
  4. .. code-block:: c
  5. /* keytable.c - This program allows checking/replacing keys at IR
  6. Copyright (C) 2006-2009 Mauro Carvalho Chehab <mchehab@kernel.org>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. 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. */
  15. #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>
  23. #include "parse.h"
  24. void prtcode (int *codes)
  25. {
  26. struct parse_key *p;
  27. for (p=keynames;p->name!=NULL;p++) {
  28. if (p->value == (unsigned)codes[1]) {
  29. printf("scancode 0x%04x = %s (0x%02x)\\n", codes[0], p->name, codes[1]);
  30. return;
  31. }
  32. }
  33. if (isprint (codes[1]))
  34. printf("scancode %d = '%c' (0x%02x)\\n", codes[0], codes[1], codes[1]);
  35. else
  36. printf("scancode %d = 0x%02x\\n", codes[0], codes[1]);
  37. }
  38. int parse_code(char *string)
  39. {
  40. struct parse_key *p;
  41. for (p=keynames;p->name!=NULL;p++) {
  42. if (!strcasecmp(p->name, string)) {
  43. return p->value;
  44. }
  45. }
  46. return -1;
  47. }
  48. int main (int argc, char *argv[])
  49. {
  50. int fd;
  51. unsigned int i, j;
  52. int codes[2];
  53. if (argc<2 || argc>4) {
  54. printf ("usage: %s <device> to get table; or\\n"
  55. " %s <device> <scancode> <keycode>\\n"
  56. " %s <device> <keycode_file>n",*argv,*argv,*argv);
  57. return -1;
  58. }
  59. if ((fd = open(argv[1], O_RDONLY)) < 0) {
  60. perror("Couldn't open input device");
  61. return(-1);
  62. }
  63. if (argc==4) {
  64. int value;
  65. value=parse_code(argv[3]);
  66. if (value==-1) {
  67. value = strtol(argv[3], NULL, 0);
  68. if (errno)
  69. perror("value");
  70. }
  71. codes [0] = (unsigned) strtol(argv[2], NULL, 0);
  72. codes [1] = (unsigned) value;
  73. if(ioctl(fd, EVIOCSKEYCODE, codes))
  74. perror ("EVIOCSKEYCODE");
  75. if(ioctl(fd, EVIOCGKEYCODE, codes)==0)
  76. prtcode(codes);
  77. return 0;
  78. }
  79. if (argc==3) {
  80. FILE *fin;
  81. int value;
  82. char *scancode, *keycode, s[2048];
  83. fin=fopen(argv[2],"r");
  84. if (fin==NULL) {
  85. perror ("opening keycode file");
  86. return -1;
  87. }
  88. /* Clears old table */
  89. for (j = 0; j < 256; j++) {
  90. for (i = 0; i < 256; i++) {
  91. codes[0] = (j << 8) | i;
  92. codes[1] = KEY_RESERVED;
  93. ioctl(fd, EVIOCSKEYCODE, codes);
  94. }
  95. }
  96. while (fgets(s,sizeof(s),fin)) {
  97. scancode=strtok(s,"\\n\\t =:");
  98. if (!scancode) {
  99. perror ("parsing input file scancode");
  100. return -1;
  101. }
  102. if (!strcasecmp(scancode, "scancode")) {
  103. scancode = strtok(NULL,"\\n\\t =:");
  104. if (!scancode) {
  105. perror ("parsing input file scancode");
  106. return -1;
  107. }
  108. }
  109. keycode=strtok(NULL,"\\n\\t =:(");
  110. if (!keycode) {
  111. perror ("parsing input file keycode");
  112. return -1;
  113. }
  114. // printf ("parsing %s=%s:", scancode, keycode);
  115. value=parse_code(keycode);
  116. // printf ("\\tvalue=%d\\n",value);
  117. if (value==-1) {
  118. value = strtol(keycode, NULL, 0);
  119. if (errno)
  120. perror("value");
  121. }
  122. codes [0] = (unsigned) strtol(scancode, NULL, 0);
  123. codes [1] = (unsigned) value;
  124. // printf("\\t%04x=%04x\\n",codes[0], codes[1]);
  125. if(ioctl(fd, EVIOCSKEYCODE, codes)) {
  126. fprintf(stderr, "Setting scancode 0x%04x with 0x%04x via ",codes[0], codes[1]);
  127. perror ("EVIOCSKEYCODE");
  128. }
  129. if(ioctl(fd, EVIOCGKEYCODE, codes)==0)
  130. prtcode(codes);
  131. }
  132. return 0;
  133. }
  134. /* Get scancode table */
  135. for (j = 0; j < 256; j++) {
  136. for (i = 0; i < 256; i++) {
  137. codes[0] = (j << 8) | i;
  138. if (!ioctl(fd, EVIOCGKEYCODE, codes) && codes[1] != KEY_RESERVED)
  139. prtcode(codes);
  140. }
  141. }
  142. return 0;
  143. }