libinput-quirks.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright © 2018 Red Hat, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "config.h"
  24. #include <errno.h>
  25. #include <getopt.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <sys/stat.h>
  29. #include "util-mem.h"
  30. #include "builddir.h"
  31. #include "libinput-util.h"
  32. #include "quirks.h"
  33. #include "shared.h"
  34. static bool verbose = false;
  35. LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
  36. static void
  37. log_handler(struct libinput *this_is_null,
  38. enum libinput_log_priority priority,
  39. const char *format,
  40. va_list args)
  41. {
  42. FILE *out = stdout;
  43. enum quirks_log_priorities p = (enum quirks_log_priorities)priority;
  44. char buf[256] = { 0 };
  45. const char *prefix = NULL;
  46. switch (p) {
  47. case QLOG_NOISE:
  48. case QLOG_DEBUG:
  49. if (!verbose)
  50. return;
  51. prefix = "quirks debug";
  52. break;
  53. case QLOG_INFO:
  54. prefix = "quirks info";
  55. break;
  56. case QLOG_ERROR:
  57. out = stderr;
  58. prefix = "quirks error";
  59. break;
  60. case QLOG_PARSER_ERROR:
  61. out = stderr;
  62. prefix = "quirks parser error";
  63. break;
  64. }
  65. snprintf(buf, sizeof(buf), "%s: %s", prefix, format);
  66. #pragma GCC diagnostic push
  67. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  68. vfprintf(out, buf, args);
  69. #pragma GCC diagnostic pop
  70. }
  71. static void
  72. usage(void)
  73. {
  74. printf("Usage:\n"
  75. " libinput quirks list [--data-dir /path/to/quirks/dir] /dev/input/event0\n"
  76. " Print the quirks for the given device\n"
  77. "\n"
  78. " libinput quirks validate [--data-dir /path/to/quirks/dir]\n"
  79. " Validate the database\n");
  80. }
  81. static void
  82. simple_printf(void *userdata, const char *val)
  83. {
  84. printf("%s\n", val);
  85. }
  86. int
  87. main(int argc, char **argv)
  88. {
  89. const char *data_path = NULL, *override_file = NULL;
  90. bool validate = false;
  91. while (1) {
  92. int c;
  93. int option_index = 0;
  94. enum {
  95. OPT_VERBOSE,
  96. OPT_DATADIR,
  97. };
  98. static struct option opts[] = {
  99. { "help", no_argument, 0, 'h' },
  100. { "verbose", no_argument, 0, OPT_VERBOSE },
  101. { "data-dir", required_argument, 0, OPT_DATADIR },
  102. { 0, 0, 0, 0 }
  103. };
  104. c = getopt_long(argc, argv, "h", opts, &option_index);
  105. if (c == -1)
  106. break;
  107. switch (c) {
  108. case '?':
  109. exit(1);
  110. break;
  111. case 'h':
  112. usage();
  113. exit(0);
  114. break;
  115. case OPT_VERBOSE:
  116. verbose = true;
  117. break;
  118. case OPT_DATADIR:
  119. data_path = optarg;
  120. break;
  121. default:
  122. usage();
  123. return EXIT_FAILURE;
  124. }
  125. }
  126. if (optind >= argc) {
  127. usage();
  128. return EXIT_FAILURE;
  129. }
  130. if (streq(argv[optind], "list")) {
  131. optind++;
  132. if (optind >= argc) {
  133. usage();
  134. return EXIT_FAILURE;
  135. }
  136. } else if (streq(argv[optind], "validate")) {
  137. optind++;
  138. if (optind < argc) {
  139. usage();
  140. return EXIT_FAILURE;
  141. }
  142. validate = true;
  143. } else {
  144. fprintf(stderr, "Unnkown action '%s'\n", argv[optind]);
  145. return EXIT_FAILURE;
  146. }
  147. /* Overriding the data dir means no custom override file */
  148. if (!data_path) {
  149. if (builddir_lookup(NULL)) {
  150. data_path = LIBINPUT_QUIRKS_SRCDIR;
  151. } else {
  152. data_path = LIBINPUT_QUIRKS_DIR;
  153. override_file = LIBINPUT_QUIRKS_OVERRIDE_FILE;
  154. }
  155. }
  156. _unref_(quirks_context) *quirks =
  157. quirks_init_subsystem(data_path,
  158. override_file,
  159. log_handler,
  160. NULL,
  161. QLOG_CUSTOM_LOG_PRIORITIES);
  162. if (!quirks) {
  163. fprintf(stderr,
  164. "Failed to initialize the device quirks. "
  165. "Please see the above errors "
  166. "and/or re-run with --verbose for more details\n");
  167. return EXIT_FAILURE;
  168. }
  169. if (validate)
  170. return EXIT_SUCCESS;
  171. _unref_(udev) *udev = udev_new();
  172. if (!udev)
  173. return EXIT_FAILURE;
  174. _unref_(udev_device) *device = NULL;
  175. const char *path = argv[optind];
  176. if (strstartswith(path, "/sys/")) {
  177. device = udev_device_new_from_syspath(udev, path);
  178. } else {
  179. struct stat st;
  180. if (stat(path, &st) < 0) {
  181. fprintf(stderr, "Error: %s: %m\n", path);
  182. return EXIT_FAILURE;
  183. }
  184. device = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
  185. }
  186. if (device) {
  187. tools_list_device_quirks(quirks, device, simple_printf, NULL);
  188. return EXIT_SUCCESS;
  189. } else {
  190. usage();
  191. return EXIT_FAILURE;
  192. }
  193. }