shared.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * Copyright © 2014 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 <assert.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <fnmatch.h>
  28. #include <getopt.h>
  29. #include <libevdev/libevdev.h>
  30. #include <libudev.h>
  31. #include <limits.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <sys/stat.h>
  36. #include <unistd.h>
  37. #include "util-macros.h"
  38. #include "util-strings.h"
  39. #include "builddir.h"
  40. #include "libinput-util.h"
  41. #include "libinput.h"
  42. #include "shared.h"
  43. static uint32_t dispatch_counter = 0;
  44. uint32_t log_serial = 0;
  45. void
  46. tools_dispatch(struct libinput *libinput)
  47. {
  48. dispatch_counter++;
  49. libinput_dispatch(libinput);
  50. }
  51. LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
  52. static void
  53. log_handler(struct libinput *li,
  54. enum libinput_log_priority priority,
  55. const char *format,
  56. va_list args)
  57. {
  58. static int use_color = -1;
  59. static uint32_t last_dispatch_no = 0;
  60. static bool color_toggle = false;
  61. if (use_color == -1) {
  62. if (getenv("NO_COLOR"))
  63. use_color = 0;
  64. else if (getenv("FORCE_COLOR"))
  65. use_color = 1;
  66. else
  67. use_color = isatty(STDOUT_FILENO);
  68. }
  69. if (use_color) {
  70. if (priority >= LIBINPUT_LOG_PRIORITY_ERROR) {
  71. if (strstr(format, "client bug: ") ||
  72. strstr(format, "libinput bug: ") ||
  73. strstr(format, "kernel bug: "))
  74. printf(ANSI_BRIGHT_RED);
  75. else
  76. printf(ANSI_RED);
  77. } else if (priority >= LIBINPUT_LOG_PRIORITY_INFO) {
  78. printf(ANSI_BOLD);
  79. } else if (priority == LIBINPUT_LOG_PRIORITY_DEBUG) {
  80. if (dispatch_counter != last_dispatch_no)
  81. color_toggle = !color_toggle;
  82. uint8_t r = 0, g = 135, b = 95 + (color_toggle ? 80 : 0);
  83. printf("\x1B[38;2;%u;%u;%um", r, g, b);
  84. }
  85. }
  86. if (priority < LIBINPUT_LOG_PRIORITY_INFO) {
  87. if (dispatch_counter != last_dispatch_no) {
  88. last_dispatch_no = dispatch_counter;
  89. printf("%4u: ", dispatch_counter);
  90. } else {
  91. printf(" %4s ", "...");
  92. }
  93. }
  94. vprintf(format, args);
  95. if (use_color)
  96. printf(ANSI_NORMAL);
  97. log_serial++;
  98. }
  99. void
  100. tools_init_options(struct tools_options *options)
  101. {
  102. memset(options, 0, sizeof(*options));
  103. options->plugins = -1;
  104. options->plugin_paths = NULL;
  105. options->tapping = -1;
  106. options->tap_map = -1;
  107. options->drag = -1;
  108. options->drag_lock = -1;
  109. options->natural_scroll = -1;
  110. options->left_handed = -1;
  111. options->middlebutton = -1;
  112. options->dwt = -1;
  113. options->dwtp = -1;
  114. options->click_method = -1;
  115. options->scroll_method = -1;
  116. options->scroll_button = -1;
  117. options->scroll_button_lock = -1;
  118. options->speed = 0.0;
  119. options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_NONE;
  120. /* initialize accel args */
  121. static double points[] = { 0.0, 1.0 };
  122. options->custom_points = points;
  123. options->custom_npoints = ARRAY_LENGTH(points);
  124. options->custom_type = LIBINPUT_ACCEL_TYPE_FALLBACK;
  125. options->custom_step = 1.0;
  126. options->pressure_range[0] = 0.0;
  127. options->pressure_range[1] = 1.0;
  128. options->calibration[0] = 1.0;
  129. options->calibration[4] = 1.0;
  130. options->area.x1 = 0.0;
  131. options->area.y1 = 0.0;
  132. options->area.x2 = 1.0;
  133. options->area.y2 = 1.0;
  134. options->sendevents = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
  135. options->eraser_button_mode = LIBINPUT_CONFIG_ERASER_BUTTON_DEFAULT;
  136. options->eraser_button_button = 0;
  137. }
  138. int
  139. tools_parse_option(int option, const char *optarg, struct tools_options *options)
  140. {
  141. switch (option) {
  142. case OPT_PLUGINS_ENABLE:
  143. options->plugins = 1;
  144. break;
  145. case OPT_PLUGINS_DISABLE:
  146. options->plugins = 0;
  147. break;
  148. case OPT_PLUGIN_PATH:
  149. options->plugin_paths = strv_from_string(optarg, ":", NULL);
  150. break;
  151. case OPT_TAP_ENABLE:
  152. options->tapping = 1;
  153. break;
  154. case OPT_TAP_DISABLE:
  155. options->tapping = 0;
  156. break;
  157. case OPT_TAP_MAP:
  158. if (!optarg)
  159. return 1;
  160. if (streq(optarg, "lrm")) {
  161. options->tap_map = LIBINPUT_CONFIG_TAP_MAP_LRM;
  162. } else if (streq(optarg, "lmr")) {
  163. options->tap_map = LIBINPUT_CONFIG_TAP_MAP_LMR;
  164. } else {
  165. return 1;
  166. }
  167. break;
  168. case OPT_DRAG_ENABLE:
  169. options->drag = 1;
  170. break;
  171. case OPT_DRAG_DISABLE:
  172. options->drag = 0;
  173. break;
  174. case OPT_DRAG_LOCK_ENABLE:
  175. if (optarg) {
  176. if (streq(optarg, "sticky")) {
  177. options->drag_lock =
  178. LIBINPUT_CONFIG_DRAG_LOCK_ENABLED_STICKY;
  179. } else if (streq(optarg, "timeout")) {
  180. options->drag_lock =
  181. LIBINPUT_CONFIG_DRAG_LOCK_ENABLED_TIMEOUT;
  182. } else {
  183. return 1;
  184. }
  185. } else {
  186. options->drag_lock = LIBINPUT_CONFIG_DRAG_LOCK_ENABLED_TIMEOUT;
  187. }
  188. break;
  189. case OPT_DRAG_LOCK_DISABLE:
  190. options->drag_lock = LIBINPUT_CONFIG_DRAG_LOCK_DISABLED;
  191. break;
  192. case OPT_NATURAL_SCROLL_ENABLE:
  193. options->natural_scroll = 1;
  194. break;
  195. case OPT_NATURAL_SCROLL_DISABLE:
  196. options->natural_scroll = 0;
  197. break;
  198. case OPT_LEFT_HANDED_ENABLE:
  199. options->left_handed = 1;
  200. break;
  201. case OPT_LEFT_HANDED_DISABLE:
  202. options->left_handed = 0;
  203. break;
  204. case OPT_MIDDLEBUTTON_ENABLE:
  205. options->middlebutton = 1;
  206. break;
  207. case OPT_MIDDLEBUTTON_DISABLE:
  208. options->middlebutton = 0;
  209. break;
  210. case OPT_DWT_ENABLE:
  211. options->dwt = LIBINPUT_CONFIG_DWT_ENABLED;
  212. break;
  213. case OPT_DWT_DISABLE:
  214. options->dwt = LIBINPUT_CONFIG_DWT_DISABLED;
  215. break;
  216. case OPT_DWTP_ENABLE:
  217. options->dwtp = LIBINPUT_CONFIG_DWTP_ENABLED;
  218. break;
  219. case OPT_DWTP_DISABLE:
  220. options->dwtp = LIBINPUT_CONFIG_DWTP_DISABLED;
  221. break;
  222. case OPT_CLICK_METHOD:
  223. if (!optarg)
  224. return 1;
  225. if (streq(optarg, "none")) {
  226. options->click_method = LIBINPUT_CONFIG_CLICK_METHOD_NONE;
  227. } else if (streq(optarg, "clickfinger")) {
  228. options->click_method =
  229. LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER;
  230. } else if (streq(optarg, "buttonareas")) {
  231. options->click_method =
  232. LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS;
  233. } else {
  234. return 1;
  235. }
  236. break;
  237. case OPT_CLICKFINGER_MAP:
  238. if (!optarg)
  239. return 1;
  240. if (streq(optarg, "lrm")) {
  241. options->clickfinger_map = LIBINPUT_CONFIG_CLICKFINGER_MAP_LRM;
  242. } else if (streq(optarg, "lmr")) {
  243. options->clickfinger_map = LIBINPUT_CONFIG_CLICKFINGER_MAP_LMR;
  244. } else {
  245. return 1;
  246. }
  247. break;
  248. case OPT_SCROLL_METHOD:
  249. if (!optarg)
  250. return 1;
  251. if (streq(optarg, "none")) {
  252. options->scroll_method = LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
  253. } else if (streq(optarg, "twofinger")) {
  254. options->scroll_method = LIBINPUT_CONFIG_SCROLL_2FG;
  255. } else if (streq(optarg, "edge")) {
  256. options->scroll_method = LIBINPUT_CONFIG_SCROLL_EDGE;
  257. } else if (streq(optarg, "button")) {
  258. options->scroll_method = LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
  259. } else {
  260. return 1;
  261. }
  262. break;
  263. case OPT_SCROLL_BUTTON:
  264. if (!optarg) {
  265. return 1;
  266. }
  267. options->scroll_button = libevdev_event_code_from_name(EV_KEY, optarg);
  268. if (options->scroll_button == -1) {
  269. fprintf(stderr, "Invalid button %s\n", optarg);
  270. return 1;
  271. }
  272. break;
  273. case OPT_SCROLL_BUTTON_LOCK_ENABLE:
  274. options->scroll_button_lock = true;
  275. break;
  276. case OPT_SCROLL_BUTTON_LOCK_DISABLE:
  277. options->scroll_button_lock = false;
  278. break;
  279. case OPT_SPEED:
  280. if (!optarg)
  281. return 1;
  282. if (!safe_atod(optarg, &options->speed))
  283. return 1;
  284. break;
  285. case OPT_PROFILE:
  286. if (!optarg)
  287. return 1;
  288. if (streq(optarg, "adaptive"))
  289. options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE;
  290. else if (streq(optarg, "flat"))
  291. options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT;
  292. else if (streq(optarg, "custom"))
  293. options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM;
  294. else
  295. return 1;
  296. break;
  297. case OPT_DISABLE_SENDEVENTS:
  298. if (!optarg)
  299. return 1;
  300. snprintf(options->disable_pattern,
  301. sizeof(options->disable_pattern),
  302. "%s",
  303. optarg);
  304. break;
  305. case OPT_SENDEVENTS:
  306. if (streq(optarg, "disabled"))
  307. options->sendevents = LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
  308. else if (streq(optarg, "enabled"))
  309. options->sendevents = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
  310. else if (streq(optarg, "disabled-on-external-mouse"))
  311. options->sendevents =
  312. LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE;
  313. else {
  314. fprintf(stderr, "Invalid sendevents mode: %s\n", optarg);
  315. return 1;
  316. }
  317. break;
  318. case OPT_APPLY_TO:
  319. if (!optarg)
  320. return 1;
  321. snprintf(options->match, sizeof(options->match), "%s", optarg);
  322. break;
  323. case OPT_CUSTOM_POINTS:
  324. if (!optarg)
  325. return 1;
  326. options->custom_points =
  327. double_array_from_string(optarg, ";", &options->custom_npoints);
  328. if (!options->custom_points || options->custom_npoints < 2) {
  329. fprintf(stderr,
  330. "Invalid --set-custom-points\n"
  331. "Please provide at least 2 points separated by a semicolon\n"
  332. " e.g. --set-custom-points=\"1.0;1.5\"\n");
  333. return 1;
  334. }
  335. break;
  336. case OPT_CUSTOM_STEP:
  337. if (!optarg)
  338. return 1;
  339. options->custom_step = strtod(optarg, NULL);
  340. break;
  341. case OPT_CUSTOM_TYPE:
  342. if (!optarg)
  343. return 1;
  344. if (streq(optarg, "fallback"))
  345. options->custom_type = LIBINPUT_ACCEL_TYPE_FALLBACK;
  346. else if (streq(optarg, "motion"))
  347. options->custom_type = LIBINPUT_ACCEL_TYPE_MOTION;
  348. else if (streq(optarg, "scroll"))
  349. options->custom_type = LIBINPUT_ACCEL_TYPE_SCROLL;
  350. else {
  351. fprintf(stderr,
  352. "Invalid --set-custom-type\n"
  353. "Valid custom types: fallback|motion|scroll\n");
  354. return 1;
  355. }
  356. break;
  357. case OPT_ROTATION_ANGLE:
  358. if (!optarg)
  359. return 1;
  360. if (!safe_atou(optarg, &options->angle)) {
  361. fprintf(stderr, "Invalid --set-rotation-angle value\n");
  362. return 1;
  363. }
  364. break;
  365. case OPT_PRESSURE_RANGE: {
  366. if (!optarg)
  367. return 1;
  368. size_t npoints = 0;
  369. _autofree_ double *range =
  370. double_array_from_string(optarg, ":", &npoints);
  371. if (npoints != 2 || !range || range[0] < 0.0 || range[1] > 1.0 ||
  372. range[0] >= range[1]) {
  373. fprintf(stderr,
  374. "Invalid pressure range, must be in format \"min:max\"\n");
  375. return 1;
  376. }
  377. options->pressure_range[0] = range[0];
  378. options->pressure_range[1] = range[1];
  379. break;
  380. }
  381. case OPT_CALIBRATION: {
  382. if (!optarg)
  383. return 1;
  384. size_t npoints = 0;
  385. _autofree_ double *matrix =
  386. double_array_from_string(optarg, " ", &npoints);
  387. if (!matrix || npoints != 6) {
  388. fprintf(stderr,
  389. "Invalid calibration matrix, must be 6 space-separated values\n");
  390. return 1;
  391. }
  392. for (size_t i = 0; i < 6; i++)
  393. options->calibration[i] = matrix[i];
  394. break;
  395. }
  396. case OPT_AREA: {
  397. if (!optarg)
  398. return 1;
  399. double x1, x2, y1, y2;
  400. if (sscanf(optarg, "%lf/%lf %lf/%lf", &x1, &y1, &x2, &y2) != 4) {
  401. fprintf(stderr, "Invalid --set-area values\n");
  402. return 1;
  403. }
  404. options->area.x1 = x1;
  405. options->area.y1 = y1;
  406. options->area.x2 = x2;
  407. options->area.y2 = y2;
  408. break;
  409. }
  410. case OPT_3FG_DRAG:
  411. if (!optarg)
  412. return 1;
  413. if (streq(optarg, "3fg"))
  414. options->drag_3fg = LIBINPUT_CONFIG_3FG_DRAG_ENABLED_3FG;
  415. else if (streq(optarg, "4fg"))
  416. options->drag_3fg = LIBINPUT_CONFIG_3FG_DRAG_ENABLED_4FG;
  417. else if (streq(optarg, "disabled"))
  418. options->drag_3fg = LIBINPUT_CONFIG_3FG_DRAG_DISABLED;
  419. else {
  420. fprintf(stderr,
  421. "Invalid --enable-3fg-drag\n"
  422. "Valid options: 3fg|4fg|disabled\n");
  423. return 1;
  424. }
  425. break;
  426. case OPT_ERASER_BUTTON_MODE:
  427. if (!optarg)
  428. return 1;
  429. if (streq(optarg, "default"))
  430. options->eraser_button_mode =
  431. LIBINPUT_CONFIG_ERASER_BUTTON_DEFAULT;
  432. else if (streq(optarg, "button"))
  433. options->eraser_button_mode =
  434. LIBINPUT_CONFIG_ERASER_BUTTON_BUTTON;
  435. else {
  436. fprintf(stderr,
  437. "Invalid --set-eraser-button-mode\n"
  438. "Valid options: default|button\n");
  439. return 1;
  440. }
  441. break;
  442. case OPT_ERASER_BUTTON_BUTTON:
  443. if (!optarg)
  444. return 1;
  445. if (streq(optarg, "BTN_STYLUS"))
  446. options->eraser_button_button = BTN_STYLUS;
  447. else if (streq(optarg, "BTN_STYLUS2"))
  448. options->eraser_button_button = BTN_STYLUS2;
  449. else if (streq(optarg, "BTN_STYLUS3"))
  450. options->eraser_button_button = BTN_STYLUS3;
  451. else {
  452. fprintf(stderr, "Unsupported eraser button %s\n", optarg);
  453. return 1;
  454. }
  455. break;
  456. }
  457. return 0;
  458. }
  459. static int
  460. open_restricted(const char *path, int flags, void *user_data)
  461. {
  462. bool *grab = user_data;
  463. int fd = open(path, flags);
  464. if (fd < 0)
  465. fprintf(stderr, "Failed to open %s (%s)\n", path, strerror(errno));
  466. else if (grab && *grab && ioctl(fd, EVIOCGRAB, (void *)1) == -1)
  467. fprintf(stderr,
  468. "Grab requested, but failed for %s (%s)\n",
  469. path,
  470. strerror(errno));
  471. return fd < 0 ? -errno : fd;
  472. }
  473. static void
  474. close_restricted(int fd, void *user_data)
  475. {
  476. close(fd);
  477. }
  478. static const struct libinput_interface interface = {
  479. .open_restricted = open_restricted,
  480. .close_restricted = close_restricted,
  481. };
  482. static int
  483. add_path(const char *str, size_t index, void *data)
  484. {
  485. struct libinput *libinput = data;
  486. libinput_plugin_system_append_default_paths(libinput);
  487. return 0;
  488. }
  489. static void
  490. tools_load_plugins(struct libinput *libinput, char **plugin_paths)
  491. {
  492. _autofree_ char *builddir = NULL;
  493. if (plugin_paths) {
  494. strv_for_each((const char **)plugin_paths, add_path, libinput);
  495. strv_free(plugin_paths);
  496. } else {
  497. if (builddir_lookup(&builddir)) {
  498. _autofree_ char *plugindir =
  499. strdup_printf("%s/plugins", builddir);
  500. libinput_plugin_system_append_path(libinput, plugindir);
  501. }
  502. libinput_plugin_system_append_default_paths(libinput);
  503. }
  504. switch (libinput_plugin_system_load_plugins(libinput,
  505. LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE)) {
  506. case -ENOSYS:
  507. fprintf(stderr, "Warning: plugins were disabled at compile time");
  508. break;
  509. case 0:
  510. break;
  511. }
  512. }
  513. static struct libinput *
  514. tools_open_udev(const char *seat,
  515. bool verbose,
  516. bool *grab,
  517. bool with_plugins,
  518. char **plugin_paths)
  519. {
  520. _unref_(udev) *udev = udev_new();
  521. if (!udev) {
  522. fprintf(stderr, "Failed to initialize udev\n");
  523. return NULL;
  524. }
  525. _unref_(libinput) *li = libinput_udev_create_context(&interface, grab, udev);
  526. if (!li) {
  527. fprintf(stderr, "Failed to initialize context from udev\n");
  528. return NULL;
  529. }
  530. libinput_log_set_handler(li, log_handler);
  531. if (verbose)
  532. libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
  533. if (with_plugins)
  534. tools_load_plugins(li, plugin_paths);
  535. if (libinput_udev_assign_seat(li, seat)) {
  536. fprintf(stderr, "Failed to set seat\n");
  537. return NULL;
  538. }
  539. return steal(&li);
  540. }
  541. static struct libinput *
  542. tools_open_device(const char **paths,
  543. bool verbose,
  544. bool *grab,
  545. bool with_plugins,
  546. char **plugin_paths)
  547. {
  548. _unref_(libinput) *li = libinput_path_create_context(&interface, grab);
  549. if (!li) {
  550. fprintf(stderr, "Failed to initialize path context\n");
  551. return NULL;
  552. }
  553. if (verbose) {
  554. libinput_log_set_handler(li, log_handler);
  555. libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
  556. }
  557. if (with_plugins)
  558. tools_load_plugins(li, plugin_paths);
  559. const char **p = paths;
  560. while (*p) {
  561. struct libinput_device *device = libinput_path_add_device(li, *p);
  562. if (!device) {
  563. fprintf(stderr, "Failed to initialize device %s\n", *p);
  564. return NULL;
  565. }
  566. p++;
  567. }
  568. return steal(&li);
  569. }
  570. static void
  571. tools_setenv_quirks_dir(void)
  572. {
  573. if (builddir_lookup(NULL))
  574. setenv("LIBINPUT_QUIRKS_DIR", LIBINPUT_QUIRKS_SRCDIR, 0);
  575. }
  576. struct libinput *
  577. tools_open_backend(enum tools_backend which,
  578. const char **seat_or_device,
  579. bool verbose,
  580. bool *grab,
  581. bool with_plugins,
  582. char **plugin_paths)
  583. {
  584. struct libinput *li;
  585. tools_setenv_quirks_dir();
  586. switch (which) {
  587. case BACKEND_UDEV:
  588. li = tools_open_udev(seat_or_device[0],
  589. verbose,
  590. grab,
  591. with_plugins,
  592. plugin_paths);
  593. break;
  594. case BACKEND_DEVICE:
  595. li = tools_open_device(seat_or_device,
  596. verbose,
  597. grab,
  598. with_plugins,
  599. plugin_paths);
  600. break;
  601. default:
  602. abort();
  603. }
  604. return li;
  605. }
  606. void
  607. tools_device_apply_config(struct libinput_device *device, struct tools_options *options)
  608. {
  609. const char *name = libinput_device_get_name(device);
  610. if (libinput_device_config_send_events_get_modes(device) &
  611. LIBINPUT_CONFIG_SEND_EVENTS_DISABLED &&
  612. fnmatch(options->disable_pattern, name, 0) != FNM_NOMATCH) {
  613. libinput_device_config_send_events_set_mode(
  614. device,
  615. LIBINPUT_CONFIG_SEND_EVENTS_DISABLED);
  616. }
  617. if (strlen(options->match) > 0 &&
  618. fnmatch(options->match, name, 0) == FNM_NOMATCH)
  619. return;
  620. libinput_device_config_send_events_set_mode(device, options->sendevents);
  621. if (options->tapping != -1)
  622. libinput_device_config_tap_set_enabled(device, options->tapping);
  623. if (options->tap_map != (enum libinput_config_tap_button_map) - 1)
  624. libinput_device_config_tap_set_button_map(device, options->tap_map);
  625. if (options->drag != -1)
  626. libinput_device_config_tap_set_drag_enabled(device, options->drag);
  627. if (options->drag_lock != -1)
  628. libinput_device_config_tap_set_drag_lock_enabled(device,
  629. options->drag_lock);
  630. if (options->natural_scroll != -1)
  631. libinput_device_config_scroll_set_natural_scroll_enabled(
  632. device,
  633. options->natural_scroll);
  634. if (options->left_handed != -1)
  635. libinput_device_config_left_handed_set(device, options->left_handed);
  636. if (options->middlebutton != -1)
  637. libinput_device_config_middle_emulation_set_enabled(
  638. device,
  639. options->middlebutton);
  640. if (options->dwt != -1)
  641. libinput_device_config_dwt_set_enabled(device, options->dwt);
  642. if (options->dwtp != -1)
  643. libinput_device_config_dwtp_set_enabled(device, options->dwtp);
  644. if (options->click_method != (enum libinput_config_click_method) - 1)
  645. libinput_device_config_click_set_method(device, options->click_method);
  646. if (options->clickfinger_map !=
  647. (enum libinput_config_clickfinger_button_map) - 1)
  648. libinput_device_config_click_set_clickfinger_button_map(
  649. device,
  650. options->clickfinger_map);
  651. if (options->scroll_method != (enum libinput_config_scroll_method) - 1)
  652. libinput_device_config_scroll_set_method(device,
  653. options->scroll_method);
  654. if (options->scroll_button != -1)
  655. libinput_device_config_scroll_set_button(device,
  656. options->scroll_button);
  657. if (options->scroll_button_lock != -1)
  658. libinput_device_config_scroll_set_button_lock(
  659. device,
  660. options->scroll_button_lock);
  661. if (libinput_device_config_accel_is_available(device)) {
  662. libinput_device_config_accel_set_speed(device, options->speed);
  663. if (options->profile != LIBINPUT_CONFIG_ACCEL_PROFILE_NONE)
  664. libinput_device_config_accel_set_profile(device,
  665. options->profile);
  666. }
  667. if (options->profile == LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM) {
  668. _destroy_(libinput_config_accel) *config = libinput_config_accel_create(
  669. LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM);
  670. libinput_config_accel_set_points(config,
  671. options->custom_type,
  672. options->custom_step,
  673. options->custom_npoints,
  674. options->custom_points);
  675. libinput_device_config_accel_apply(device, config);
  676. }
  677. if (options->angle != 0)
  678. libinput_device_config_rotation_set_angle(device, options->angle % 360);
  679. if (libinput_device_config_calibration_has_matrix(device))
  680. libinput_device_config_calibration_set_matrix(device,
  681. options->calibration);
  682. if (libinput_device_config_area_has_rectangle(device))
  683. libinput_device_config_area_set_rectangle(device, &options->area);
  684. if (libinput_device_config_3fg_drag_get_finger_count(device) >= 3)
  685. libinput_device_config_3fg_drag_set_enabled(device, options->drag_3fg);
  686. }
  687. void
  688. tools_tablet_tool_apply_config(struct libinput_tablet_tool *tool,
  689. struct tools_options *options)
  690. {
  691. if (options->pressure_range[0] != 0.0 || options->pressure_range[1] != 1.0)
  692. libinput_tablet_tool_config_pressure_range_set(
  693. tool,
  694. options->pressure_range[0],
  695. options->pressure_range[1]);
  696. if (options->eraser_button_button)
  697. libinput_tablet_tool_config_eraser_button_set_button(
  698. tool,
  699. options->eraser_button_button);
  700. libinput_tablet_tool_config_eraser_button_set_mode(tool,
  701. options->eraser_button_mode);
  702. }
  703. static char *
  704. find_device(const char *udev_tag)
  705. {
  706. _unref_(udev) *udev = udev_new();
  707. if (!udev)
  708. return NULL;
  709. _unref_(udev_enumerate) *e = udev_enumerate_new(udev);
  710. udev_enumerate_add_match_subsystem(e, "input");
  711. udev_enumerate_scan_devices(e);
  712. struct udev_list_entry *entry = NULL;
  713. udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
  714. const char *path = udev_list_entry_get_name(entry);
  715. _unref_(udev_device) *device = udev_device_new_from_syspath(udev, path);
  716. if (!device)
  717. continue;
  718. const char *sysname = udev_device_get_sysname(device);
  719. if (!strstartswith(sysname, "event")) {
  720. continue;
  721. }
  722. if (udev_device_get_property_value(device, udev_tag)) {
  723. char *device_node =
  724. safe_strdup(udev_device_get_devnode(device));
  725. if (device_node)
  726. return device_node;
  727. }
  728. }
  729. return NULL;
  730. }
  731. bool
  732. find_touchpad_device(char *path, size_t path_len)
  733. {
  734. _autofree_ char *devnode = find_device("ID_INPUT_TOUCHPAD");
  735. if (devnode)
  736. snprintf(path, path_len, "%s", devnode);
  737. return devnode != NULL;
  738. }
  739. bool
  740. is_touchpad_device(const char *devnode)
  741. {
  742. struct stat st;
  743. if (stat(devnode, &st) < 0)
  744. return false;
  745. _unref_(udev) *udev = udev_new();
  746. if (!udev)
  747. return false;
  748. _unref_(udev_device) *dev = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
  749. if (!dev)
  750. return false;
  751. return udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD");
  752. }
  753. static inline void
  754. setup_path(void)
  755. {
  756. const char *path = getenv("PATH");
  757. char new_path[PATH_MAX];
  758. const char *extra_path = LIBINPUT_TOOL_PATH;
  759. _autofree_ char *builddir = NULL;
  760. builddir_lookup(&builddir);
  761. snprintf(new_path,
  762. sizeof(new_path),
  763. "%s:%s",
  764. builddir ? builddir : extra_path,
  765. path ? path : "");
  766. setenv("PATH", new_path, 1);
  767. }
  768. int
  769. tools_exec_command(const char *prefix, int real_argc, char **real_argv)
  770. {
  771. char *argv[64] = { NULL };
  772. char executable[128];
  773. const char *command;
  774. int rc;
  775. assert((size_t)real_argc < ARRAY_LENGTH(argv));
  776. command = real_argv[0];
  777. rc = snprintf(executable, sizeof(executable), "%s-%s", prefix, command);
  778. if (rc >= (int)sizeof(executable)) {
  779. fprintf(stderr, "Failed to assemble command.\n");
  780. return EXIT_FAILURE;
  781. }
  782. argv[0] = executable;
  783. for (int i = 1; i < real_argc; i++)
  784. argv[i] = real_argv[i];
  785. setup_path();
  786. rc = execvp(executable, argv);
  787. if (rc) {
  788. if (errno == ENOENT) {
  789. fprintf(stderr, "libinput: %s is not installed\n", command);
  790. return EXIT_INVALID_USAGE;
  791. }
  792. fprintf(stderr,
  793. "Failed to execute '%s' (%s)\n",
  794. command,
  795. strerror(errno));
  796. }
  797. return EXIT_FAILURE;
  798. }
  799. static void
  800. sprintf_event_codes(char *buf, size_t sz, struct quirks *quirks, enum quirk q)
  801. {
  802. const struct quirk_tuples *t;
  803. size_t off = 0;
  804. int printed;
  805. const char *name;
  806. quirks_get_tuples(quirks, q, &t);
  807. name = quirk_get_name(q);
  808. printed = snprintf(buf, sz, "%s=", name);
  809. assert(printed != -1);
  810. off += printed;
  811. for (size_t i = 0; off < sz && i < t->ntuples; i++) {
  812. unsigned int type = t->tuples[i].first;
  813. unsigned int code = t->tuples[i].second;
  814. bool enable = t->tuples[i].third;
  815. const char *name = libevdev_event_code_get_name(type, code);
  816. printed = snprintf(buf + off,
  817. sz - off,
  818. "%c%s;",
  819. enable ? '+' : '-',
  820. name);
  821. assert(printed != -1);
  822. off += printed;
  823. }
  824. }
  825. static void
  826. sprintf_input_props(char *buf, size_t sz, struct quirks *quirks, enum quirk q)
  827. {
  828. const struct quirk_tuples *t;
  829. size_t off = 0;
  830. int printed;
  831. const char *name;
  832. quirks_get_tuples(quirks, q, &t);
  833. name = quirk_get_name(q);
  834. printed = snprintf(buf, sz, "%s=", name);
  835. assert(printed != -1);
  836. off += printed;
  837. for (size_t i = 0; off < sz && i < t->ntuples; i++) {
  838. unsigned int prop = t->tuples[i].first;
  839. bool enable = t->tuples[i].second;
  840. const char *name = libevdev_property_get_name(prop);
  841. printed = snprintf(buf + off,
  842. sz - off,
  843. "%c%s;",
  844. enable ? '+' : '-',
  845. name);
  846. assert(printed != -1);
  847. off += printed;
  848. }
  849. }
  850. void
  851. tools_list_device_quirks(struct quirks_context *ctx,
  852. struct udev_device *device,
  853. void (*callback)(void *data, const char *str),
  854. void *userdata)
  855. {
  856. char buf[256];
  857. enum quirk q;
  858. _unref_(quirks) *quirks = quirks_fetch_for_device(ctx, device);
  859. if (!quirks)
  860. return;
  861. q = QUIRK_MODEL_ALPS_SERIAL_TOUCHPAD;
  862. do {
  863. if (quirks_has_quirk(quirks, q)) {
  864. const char *name;
  865. bool b;
  866. name = quirk_get_name(q);
  867. quirks_get_bool(quirks, q, &b);
  868. snprintf(buf, sizeof(buf), "%s=%d", name, b ? 1 : 0);
  869. callback(userdata, buf);
  870. }
  871. } while (++q < _QUIRK_LAST_MODEL_QUIRK_);
  872. q = QUIRK_ATTR_SIZE_HINT;
  873. do {
  874. if (quirks_has_quirk(quirks, q)) {
  875. const char *name;
  876. struct quirk_dimensions dim;
  877. struct quirk_range r;
  878. uint32_t v;
  879. char *s;
  880. double d;
  881. bool b;
  882. name = quirk_get_name(q);
  883. switch (q) {
  884. case QUIRK_ATTR_SIZE_HINT:
  885. case QUIRK_ATTR_RESOLUTION_HINT:
  886. quirks_get_dimensions(quirks, q, &dim);
  887. snprintf(buf,
  888. sizeof(buf),
  889. "%s=%zdx%zd",
  890. name,
  891. dim.x,
  892. dim.y);
  893. callback(userdata, buf);
  894. break;
  895. case QUIRK_ATTR_TOUCH_SIZE_RANGE:
  896. case QUIRK_ATTR_PRESSURE_RANGE:
  897. quirks_get_range(quirks, q, &r);
  898. snprintf(buf,
  899. sizeof(buf),
  900. "%s=%d:%d",
  901. name,
  902. r.upper,
  903. r.lower);
  904. callback(userdata, buf);
  905. break;
  906. case QUIRK_ATTR_PALM_SIZE_THRESHOLD:
  907. case QUIRK_ATTR_PALM_PRESSURE_THRESHOLD:
  908. case QUIRK_ATTR_THUMB_PRESSURE_THRESHOLD:
  909. case QUIRK_ATTR_THUMB_SIZE_THRESHOLD:
  910. quirks_get_uint32(quirks, q, &v);
  911. snprintf(buf, sizeof(buf), "%s=%u", name, v);
  912. callback(userdata, buf);
  913. break;
  914. case QUIRK_ATTR_LID_SWITCH_RELIABILITY:
  915. case QUIRK_ATTR_KEYBOARD_INTEGRATION:
  916. case QUIRK_ATTR_TRACKPOINT_INTEGRATION:
  917. case QUIRK_ATTR_TPKBCOMBO_LAYOUT:
  918. case QUIRK_ATTR_MSC_TIMESTAMP:
  919. quirks_get_string(quirks, q, &s);
  920. snprintf(buf, sizeof(buf), "%s=%s", name, s);
  921. callback(userdata, buf);
  922. break;
  923. case QUIRK_ATTR_TRACKPOINT_MULTIPLIER:
  924. quirks_get_double(quirks, q, &d);
  925. snprintf(buf, sizeof(buf), "%s=%0.2f", name, d);
  926. callback(userdata, buf);
  927. break;
  928. case QUIRK_ATTR_USE_VELOCITY_AVERAGING:
  929. case QUIRK_ATTR_TABLET_SMOOTHING:
  930. case QUIRK_ATTR_IS_VIRTUAL:
  931. quirks_get_bool(quirks, q, &b);
  932. snprintf(buf, sizeof(buf), "%s=%d", name, b);
  933. callback(userdata, buf);
  934. break;
  935. case QUIRK_ATTR_EVENT_CODE:
  936. sprintf_event_codes(buf, sizeof(buf), quirks, q);
  937. callback(userdata, buf);
  938. break;
  939. case QUIRK_ATTR_INPUT_PROP:
  940. sprintf_input_props(buf, sizeof(buf), quirks, q);
  941. callback(userdata, buf);
  942. break;
  943. default:
  944. abort();
  945. break;
  946. }
  947. }
  948. } while (++q < _QUIRK_LAST_ATTR_QUIRK_);
  949. }