xtsscale.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /* $OpenBSD: xtsscale.c,v 1.23 2012/03/06 23:24:37 matthieu Exp $ */
  2. /*
  3. * Copyright (c) 2007 Robert Nagy <robert@openbsd.org>
  4. * Copyright (c) 2009,2011 Matthieu Herrb <matthieu@herrb.eu>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
  16. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
  19. * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * Copyright 1996 by Frederic Lepied, France. <Frederic.Lepied@sugix.frmug.org>
  29. *
  30. * Permission to use, copy, modify, distribute, and sell this software and its
  31. * documentation for any purpose is hereby granted without fee, provided that
  32. * the above copyright notice appear in all copies and that both that
  33. * copyright notice and this permission notice appear in supporting
  34. * documentation, and that the name of the authors not be used in
  35. * advertising or publicity pertaining to distribution of the software without
  36. * specific, written prior permission. The authors make no
  37. * representations about the suitability of this software for any purpose. It
  38. * is provided "as is" without express or implied warranty.
  39. *
  40. * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  41. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  42. * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  43. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  44. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  45. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  46. * PERFORMANCE OF THIS SOFTWARE.
  47. *
  48. */
  49. #include <X11/Xlib.h>
  50. #include <X11/Xatom.h>
  51. #include <X11/Xft/Xft.h>
  52. #include <X11/extensions/Xrender.h>
  53. #include <X11/extensions/XInput.h>
  54. #include <X11/extensions/Xrandr.h>
  55. #include <ctype.h>
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <unistd.h>
  59. #include <math.h>
  60. #include <ws-properties.h>
  61. #define FONT_NAME "mono"
  62. #define FONT_SIZE 14
  63. #define Background "white"
  64. #define TouchCross "black"
  65. #define PromptText "black"
  66. #define Error "red"
  67. #define INVALID_EVENT_TYPE -1
  68. static int motion_type = INVALID_EVENT_TYPE;
  69. static int button_press_type = INVALID_EVENT_TYPE;
  70. static int button_release_type = INVALID_EVENT_TYPE;
  71. static int proximity_in_type = INVALID_EVENT_TYPE;
  72. static int proximity_out_type = INVALID_EVENT_TYPE;
  73. int has_xrandr = False;
  74. int has_xrandr_1_2 = False;
  75. int has_xrandr_1_3 = False;
  76. int has_xinerama = False;
  77. Atom prop_calibration, prop_swap;
  78. /* where the calibration points are placed */
  79. #define SCREEN_DIVIDE 16
  80. #define SCREEN_MAX 0x800
  81. #define M_POINT (SCREEN_MAX/SCREEN_DIVIDE)
  82. int MARK_POINT[] = {M_POINT, SCREEN_MAX - 1 - M_POINT};
  83. int touched = 0;
  84. char *deviceName;
  85. Display *display;
  86. int screen;
  87. Window root;
  88. Window win;
  89. XftFont *font;
  90. XftColor cross, errorColor, promptColor, bg;
  91. XftDraw *draw;
  92. unsigned int xpos, ypos, width, height; /* window size */
  93. Bool interrupted = False;
  94. Bool verbose = False;
  95. int cx[5], cy[5];
  96. int x[5], y[5];
  97. extern char * __progname;
  98. struct { int minx, maxx, miny, maxy, swapxy, resx, resy; } calib, old_calib;
  99. Bool old_swap;
  100. static char *prompt_message[] = {
  101. "TOUCH SCREEN CALIBRATION",
  102. "Press on the crosshairs please...",
  103. "Use the ESC key to cancel.",
  104. NULL
  105. };
  106. static char *error_message[] = {
  107. "Not accurate enough.",
  108. "Try again...",
  109. NULL
  110. };
  111. void
  112. cleanup_exit(XDevice *device)
  113. {
  114. long values[4];
  115. values[0] = old_calib.minx;
  116. values[1] = old_calib.maxx;
  117. values[2] = old_calib.miny;
  118. values[3] = old_calib.maxy;
  119. XChangeDeviceProperty(display, device, prop_calibration,
  120. XA_INTEGER, 32, PropModeReplace, (unsigned char *)values, 4);
  121. XChangeDeviceProperty(display, device, prop_swap,
  122. XA_INTEGER, 8, PropModeReplace, (unsigned char *)&old_swap, 1);
  123. XCloseDevice(display, device);
  124. XUngrabServer(display);
  125. XUngrabKeyboard(display, CurrentTime);
  126. XCloseDisplay(display);
  127. exit(1);
  128. }
  129. void
  130. render_init(void)
  131. {
  132. font = XftFontOpen(display, screen,
  133. XFT_FAMILY, XftTypeString, FONT_NAME,
  134. XFT_SIZE, XftTypeInteger, FONT_SIZE,
  135. NULL);
  136. if (!XftColorAllocName(display, XDefaultVisual(display, screen),
  137. DefaultColormap(display, screen), TouchCross, &cross)) {
  138. fprintf(stderr, "Cannot get color");
  139. exit(2);
  140. }
  141. if (!XftColorAllocName(display, XDefaultVisual(display, screen),
  142. DefaultColormap(display, screen), PromptText, &promptColor)) {
  143. fprintf(stderr, "Cannot get color");
  144. exit(2);
  145. }
  146. if (!XftColorAllocName(display, XDefaultVisual(display, screen),
  147. DefaultColormap(display, screen), Background, &bg)) {
  148. fprintf(stderr, "Cannot get bg color");
  149. exit(2);
  150. }
  151. if (!XftColorAllocName(display, XDefaultVisual(display, screen),
  152. DefaultColormap(display, screen), Error, &errorColor)) {
  153. fprintf(stderr, "Cannot get color");
  154. exit(2);
  155. }
  156. draw = XftDrawCreate(display, win, DefaultVisual(display, screen),
  157. DefaultColormap(display, screen));
  158. }
  159. void
  160. draw_point(int x, int y, int width, int size, XftColor *color)
  161. {
  162. XPointDouble p[4];
  163. p[0].x = x - size;
  164. p[0].y = y - 1;
  165. p[1].x = x - size;
  166. p[1].y = y + 1;
  167. p[2].x = x + size;
  168. p[2].y = y + 1;
  169. p[3].x = x + size;
  170. p[3].y = y - 1;
  171. XRenderCompositeDoublePoly(display, PictOpOver,
  172. XftDrawSrcPicture(draw, color),
  173. XftDrawPicture(draw),
  174. XRenderFindStandardFormat(display, PictStandardA8),
  175. 0, 0, 0, 0, p, 4, 0);
  176. p[0].x = x - 1;
  177. p[0].y = y - size;
  178. p[1].x = x + 1;
  179. p[1].y = y - size;
  180. p[2].x = x + 1;
  181. p[2].y = y + size;
  182. p[3].x = x - 1;
  183. p[3].y = y + size;
  184. XRenderCompositeDoublePoly(display, PictOpOver,
  185. XftDrawSrcPicture(draw, color),
  186. XftDrawPicture(draw),
  187. XRenderFindStandardFormat(display, PictStandardA8),
  188. 0, 0, 0, 0, p, 4, 0);
  189. }
  190. void
  191. draw_text(char **message, XftColor *color)
  192. {
  193. int len;
  194. int i, x, y;
  195. XGlyphInfo extents;
  196. i = 0;
  197. y = height / 3;
  198. while (message[i] != NULL) {
  199. len = strlen(message[i]);
  200. XftTextExtents8(display, font, message[i], len, &extents);
  201. x = (width - extents.width)/2;
  202. XftDrawString8(draw, color, font, x, y, message[i], len);
  203. y += extents.height * 1.5;
  204. i++;
  205. }
  206. }
  207. void
  208. draw_graphics(int i, int j, int n)
  209. {
  210. draw_text(prompt_message, &promptColor);
  211. if (n == 2) {
  212. cx[n] = width / 2;
  213. cy[n] = height / 2;
  214. } else {
  215. cx[n] = (MARK_POINT[i] * width) / SCREEN_MAX;
  216. cy[n] = (MARK_POINT[j] * height) / SCREEN_MAX;
  217. }
  218. draw_point(cx[n], cy[n], width / 200, width / 64, &cross);
  219. }
  220. Cursor
  221. create_empty_cursor(void)
  222. {
  223. char nothing[] = {0};
  224. XColor nullcolor;
  225. Pixmap src = XCreateBitmapFromData(display, root, nothing, 1, 1);
  226. Pixmap msk = XCreateBitmapFromData(display, root, nothing, 1, 1);
  227. Cursor mcyursor = XCreatePixmapCursor(display, src, msk,
  228. &nullcolor, &nullcolor, 0, 0);
  229. XFreePixmap(display, src);
  230. XFreePixmap(display, msk);
  231. return mcyursor;
  232. }
  233. int
  234. check_device(XDeviceInfo *info)
  235. {
  236. XDevice *device;
  237. Atom type;
  238. int format;
  239. unsigned long nitems, nbytes;
  240. unsigned char *retval;
  241. if (verbose)
  242. printf("Checking device %lu: %s...", info->id, info->name);
  243. device = XOpenDevice(display, info->id);
  244. XGetDeviceProperty(display,
  245. device, prop_calibration,
  246. 0, 4, False,
  247. XA_INTEGER, &type, &format,
  248. &nitems, &nbytes, &retval);
  249. XCloseDevice(display, device);
  250. if (nitems != 4) {
  251. if (verbose)
  252. printf("can't be calibrated\n");
  253. return False;
  254. }
  255. if (verbose)
  256. printf("can be calibrated\n");
  257. return True;
  258. }
  259. XDeviceInfo*
  260. find_device_info(char *name)
  261. {
  262. XDeviceInfo *devices;
  263. XDeviceInfo *found = NULL;
  264. int i, max_id;
  265. int num_devices, num_found;
  266. Bool is_id = True;
  267. XID id = (XID)-1;
  268. const char *errstr;
  269. devices = XListInputDevices(display, &num_devices);
  270. max_id = 0;
  271. for (i = 0; i < num_devices; i++)
  272. if (devices[i].id > max_id)
  273. max_id = devices[i].id;
  274. if (name != NULL) {
  275. for(i = 0; i < strlen(name); i++) {
  276. if (!isdigit(name[i])) {
  277. is_id = False;
  278. break;
  279. }
  280. }
  281. if (is_id) {
  282. id = strtonum(name, 0, max_id, &errstr);
  283. if (errstr != NULL) {
  284. fprintf(stderr, "Invalid device id %s: %s\n",
  285. name, errstr);
  286. exit(1);
  287. }
  288. }
  289. }
  290. num_found = 0;
  291. for(i = 0; i < num_devices; i++) {
  292. if (devices[i].use != IsXExtensionPointer)
  293. continue;
  294. if (name == NULL) {
  295. if (check_device(&devices[i])) {
  296. found = &devices[i];
  297. num_found++;
  298. }
  299. continue;
  300. }
  301. if ((!is_id && strcmp(devices[i].name, name) == 0) ||
  302. (is_id && devices[i].id == id)) {
  303. found = &devices[i];
  304. num_found++;
  305. }
  306. }
  307. if (num_found > 1) {
  308. fprintf(stderr,
  309. "Error: found multiple matching devices.\n"
  310. "To ensure the correct one is selected, please use "
  311. "the device ID instead.\n");
  312. return NULL;
  313. }
  314. return found;
  315. }
  316. static int
  317. register_events(XDeviceInfo *info, XDevice *device,
  318. Bool handle_proximity)
  319. {
  320. int number = 0; /* number of events registered */
  321. XEventClass event_list[7];
  322. int i;
  323. unsigned long screen;
  324. XInputClassInfo *ip;
  325. screen = DefaultScreen(display);
  326. if (device->num_classes > 0) {
  327. for (ip = device->classes, i=0; i<info->num_classes;
  328. ip++, i++) {
  329. switch (ip->input_class) {
  330. case ButtonClass:
  331. DeviceButtonPress(device, button_press_type,
  332. event_list[number]);
  333. number++;
  334. DeviceButtonRelease(device,
  335. button_release_type, event_list[number]);
  336. number++;
  337. break;
  338. case ValuatorClass:
  339. DeviceMotionNotify(device, motion_type,
  340. event_list[number]); number++;
  341. if (handle_proximity) {
  342. ProximityIn(device, proximity_in_type,
  343. event_list[number]); number++;
  344. ProximityOut(device,
  345. proximity_out_type,
  346. event_list[number]); number++;
  347. }
  348. break;
  349. default:
  350. fprintf(stderr,
  351. "Found unknown device class %d\n",
  352. ip->input_class);
  353. break;
  354. }
  355. }
  356. if (XSelectExtensionEvent(display, root, event_list, number)) {
  357. fprintf(stderr, "Error selecting extended events\n");
  358. return 0;
  359. }
  360. }
  361. return number;
  362. }
  363. static Bool
  364. get_events(int i)
  365. {
  366. XEvent Event;
  367. XDeviceMotionEvent *motion = (XDeviceMotionEvent *) &Event;
  368. int j, a;
  369. char c;
  370. x[i] = y[i] = -1;
  371. while (1) {
  372. XNextEvent(display, &Event);
  373. if (Event.type == motion_type) {
  374. for (j = 0; j < motion->axes_count; j++) {
  375. a = motion->first_axis + j;
  376. switch (a) {
  377. case 0:
  378. x[i] = motion->axis_data[j];
  379. break;
  380. case 1:
  381. y[i] = motion->axis_data[j];
  382. break;
  383. default:
  384. fprintf(stderr,
  385. "Unknown axis %d\n", a);
  386. }
  387. }
  388. } else if (Event.type == button_release_type) {
  389. if (x[i] != -1 && y[i] != -1)
  390. break;
  391. } else if (Event.type == KeyPress) {
  392. a = XLookupString(&Event.xkey, &c, 1, NULL, NULL);
  393. if ((a == 1) && ((c == 'q') || (c == 'Q') ||
  394. (c == '\03') || (c == '\033'))) {
  395. interrupted++;
  396. return False;
  397. }
  398. }
  399. }
  400. if (verbose)
  401. printf("x[%d] = %d y[%d] = %d\n", i, x[i], i, y[i]);
  402. return True;
  403. }
  404. int
  405. uncalibrate(XDevice *device)
  406. {
  407. Atom type;
  408. int format;
  409. unsigned long nitems, nbytes;
  410. long values[4] = { 0, 32767, 0, 32767 }; /* uncalibrated */
  411. Bool swap = 0;
  412. unsigned char *retval;
  413. /* Save old values */
  414. XGetDeviceProperty(display, device, prop_calibration, 0,
  415. 4, False, XA_INTEGER, &type, &format, &nitems,
  416. &nbytes, &retval);
  417. if (type != XA_INTEGER) {
  418. fprintf(stderr, "Device property \"%s\": invalid type %s\n",
  419. WS_PROP_CALIBRATION, XGetAtomName(display, type));
  420. return -1;
  421. }
  422. if (nitems != 4 && nitems != 0) {
  423. fprintf(stderr, "Device property \"%s\": "
  424. "invalid number of items %ld\n",
  425. WS_PROP_CALIBRATION, nitems);
  426. return -1;
  427. }
  428. old_calib.minx = *(long *)retval;
  429. old_calib.maxx = *((long *)retval + 1);
  430. old_calib.miny = *((long *)retval + 2);
  431. old_calib.maxy = *((long *)retval + 3);
  432. XFree(retval);
  433. XGetDeviceProperty(display, device, prop_swap, 0,
  434. 1, False, XA_INTEGER, &type, &format, &nitems,
  435. &nbytes, &retval);
  436. old_swap = *(Bool *)retval;
  437. XFree(retval);
  438. /* Force uncalibrated state */
  439. XChangeDeviceProperty(display, device, prop_calibration,
  440. XA_INTEGER, 32, PropModeReplace, (unsigned char *)values, 4);
  441. XChangeDeviceProperty(display, device, prop_swap,
  442. XA_INTEGER, 8, PropModeReplace, (unsigned char *)&swap, 1);
  443. return 0;
  444. }
  445. void
  446. get_xrandr_config(Display *dpy, Window root, char *name,
  447. int *x, int *y, int *width, int *height)
  448. {
  449. XRRScreenResources *res;
  450. XRROutputInfo *output_info;
  451. XRRCrtcInfo *crtc_info;
  452. int o, found = 0;
  453. res = XRRGetScreenResources(dpy, root);
  454. for (o = 0; o < res->noutput; o++) {
  455. output_info = XRRGetOutputInfo (dpy, res, res->outputs[o]);
  456. if (!output_info) {
  457. fprintf(stderr,
  458. "could not get output 0x%lx information\n",
  459. res->outputs[o]);
  460. exit(2);
  461. }
  462. if (output_info->crtc != 0) {
  463. crtc_info = XRRGetCrtcInfo(dpy, res,
  464. output_info->crtc);
  465. if (!crtc_info) {
  466. fprintf(stderr,
  467. "%s: could not get crtc 0x%lx "
  468. "information\n", __progname,
  469. output_info->crtc);
  470. exit(2);
  471. }
  472. printf("%s: %dx%d+%d+%d\n",
  473. output_info->name,
  474. crtc_info->width, crtc_info->height,
  475. crtc_info->x, crtc_info->y);
  476. if (!strcmp(output_info->name, name)) {
  477. *x = crtc_info->x;
  478. *y = crtc_info->y;
  479. *width = crtc_info->width;
  480. *height = crtc_info->height;
  481. found = 1;
  482. }
  483. }
  484. }
  485. if (!found) {
  486. fprintf(stderr, "%s: output %s not found\n", __progname, name);
  487. exit(2);
  488. }
  489. }
  490. void __dead
  491. usage(void)
  492. {
  493. fprintf(stderr, "usage: xtsscale [-c][-D display]"
  494. "[-d device][-o output]\n");
  495. exit(2);
  496. }
  497. int
  498. main(int argc, char *argv[], char *env[])
  499. {
  500. char *display_name = NULL;
  501. char *device_name = NULL;
  502. char *output_name = NULL;
  503. XSetWindowAttributes xswa;
  504. int i = 0;
  505. double a, a1, a2, b, b1, b2, xerr, yerr;
  506. int xi_opcode, event, error;
  507. XExtensionVersion *version;
  508. XDeviceInfo *info;
  509. XDevice *device;
  510. long calib_data[4];
  511. unsigned long mask;
  512. unsigned char swap;
  513. int keep_cursor = 0, ch;
  514. /* Crosshair placement */
  515. int cpx[] = { 0, 0, 1, 1, 1 };
  516. int cpy[] = { 0, 1, 0, 0, 1 };
  517. while ((ch = getopt(argc, argv, "cD:d:o:v")) != -1) {
  518. switch (ch) {
  519. case 'c':
  520. keep_cursor++;
  521. break;
  522. case 'D':
  523. display_name = optarg;
  524. break;
  525. case 'd':
  526. device_name = optarg;
  527. break;
  528. case 'o':
  529. output_name = optarg;
  530. break;
  531. case 'v':
  532. verbose = True;
  533. break;
  534. default:
  535. usage();
  536. /* NOTREACHED */
  537. }
  538. }
  539. argc -= optind;
  540. argv += optind;
  541. if (argc != 0)
  542. usage();
  543. /* connect to X server */
  544. if ((display = XOpenDisplay(display_name)) == NULL) {
  545. fprintf(stderr, "%s: cannot connect to X server %s\n",
  546. __progname, XDisplayName(display_name));
  547. exit(1);
  548. }
  549. screen = DefaultScreen(display);
  550. root = RootWindow(display, screen);
  551. /* get screen size from display structure macro */
  552. xpos = 0;
  553. ypos = 0;
  554. width = DisplayWidth(display, screen);
  555. height = DisplayHeight(display, screen);
  556. if (XRRQueryExtension(display, &event, &error)) {
  557. int major, minor;
  558. if (XRRQueryVersion(display, &major, &minor) != True) {
  559. fprintf(stderr, "Error querying XRandR version");
  560. } else {
  561. printf("XRandR extension version %d.%d present\n",
  562. major, minor);
  563. has_xrandr = True;
  564. if (major > 1 || (major == 1 && minor >=2))
  565. has_xrandr_1_2 = True;
  566. if (major > 1 || (major == 1 && minor >=3))
  567. has_xrandr_1_3 = True;
  568. }
  569. }
  570. if (output_name != NULL) {
  571. if (has_xrandr_1_2) {
  572. get_xrandr_config(display, root, output_name,
  573. &xpos, &ypos, &width, &height);
  574. } else {
  575. fprintf(stderr, "%s: can not specify an output "
  576. "whithout XRandr 1.2 or later", __progname);
  577. exit(2);
  578. }
  579. }
  580. if (!XQueryExtension(display, INAME, &xi_opcode,
  581. &event, &error)) {
  582. fprintf(stderr, "%s: X Input extension not available.\n",
  583. __progname);
  584. exit(1);
  585. }
  586. version = XGetExtensionVersion(display, INAME);
  587. if (version == NULL ||
  588. version == (XExtensionVersion *)NoSuchExtension) {
  589. fprintf(stderr, "Cannot query X Input version.\n");
  590. exit(1);
  591. }
  592. XFree(version);
  593. prop_calibration = XInternAtom(display, WS_PROP_CALIBRATION, True);
  594. if (prop_calibration == None) {
  595. fprintf(stderr, "Unable to find the \"%s\" device property.\n"
  596. "There are probably no calibrable devices "
  597. "on this system.\n", WS_PROP_CALIBRATION);
  598. exit(1);
  599. }
  600. prop_swap = XInternAtom(display, WS_PROP_SWAP_AXES, True);
  601. if (prop_swap == None) {
  602. fprintf(stderr, "Unable to find the \"%s\" device property\n",
  603. WS_PROP_SWAP_AXES);
  604. exit(1);
  605. }
  606. info = find_device_info(device_name);
  607. if (info == NULL) {
  608. fprintf(stderr, "Unable to find the %s device\n",
  609. device_name ? device_name : "default");
  610. exit(1);
  611. }
  612. /* setup window attributes */
  613. xswa.override_redirect = True;
  614. xswa.background_pixel = BlackPixel(display, screen);
  615. xswa.event_mask = ExposureMask | KeyPressMask;
  616. mask = CWOverrideRedirect | CWBackPixel | CWEventMask;
  617. if (!keep_cursor) {
  618. xswa.cursor = create_empty_cursor();
  619. mask |= CWCursor;
  620. }
  621. win = XCreateWindow(display, RootWindow(display, screen),
  622. xpos, ypos, width, height, 0,
  623. CopyFromParent, InputOutput, CopyFromParent,
  624. mask, &xswa);
  625. render_init();
  626. XMapWindow(display, win);
  627. XGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync,
  628. CurrentTime);
  629. XGrabServer(display);
  630. XClearWindow(display, win);
  631. if (verbose)
  632. printf("Calibrating %s\n", info->name);
  633. device = XOpenDevice(display, info->id);
  634. if (!device) {
  635. fprintf(stderr, "Unable to open the X input device \"%s\"\n",
  636. info->name);
  637. return 0;
  638. }
  639. if (!register_events(info, device, 0))
  640. exit(1);
  641. uncalibrate(device);
  642. calib:
  643. XftDrawRect(draw, &bg, 0, 0, width, height);
  644. for (i = 0; i < 5; i++) {
  645. draw_graphics(cpx[i], cpy[i], i);
  646. XFlush(display);
  647. if (!get_events(i))
  648. break;
  649. XftDrawRect(draw, &bg, 0, 0, width, height);
  650. }
  651. if (interrupted)
  652. cleanup_exit(device);
  653. /* Check if X and Y should be swapped */
  654. if (fabs(x[0] - x[1]) > fabs(y[0] - y[1])) {
  655. calib.swapxy = 1;
  656. for (i = 0; i < 5; i++) {
  657. int t = x[i];
  658. x[i] = y[i];
  659. y[i] = t;
  660. }
  661. }
  662. /* get touch pad resolution to screen resolution ratio */
  663. a1 = (double) (x[4] - x[0]) / (double) (cx[4] - cx[0]);
  664. a2 = (double) (x[3] - x[1]) / (double) (cx[3] - cx[1]);
  665. /* get the minimum pad position on the X-axis */
  666. b1 = x[0] - a1 * cx[0];
  667. b2 = x[1] - a2 * cx[1];
  668. /* use the average ratio and average minimum position */
  669. a = (a1 + a2) / 2.0;
  670. b = (b1 + b2) / 2.0;
  671. xerr = a * width / 2 + b - x[2];
  672. if (fabs(xerr) > fabs(a * width * .01)) {
  673. fprintf(stderr, "Calibration problem: X axis error (%.2f) too high, try again\n",
  674. fabs(xerr));
  675. goto err;
  676. }
  677. calib.minx = (int) (b + 0.5);
  678. calib.maxx = (int) (a * width + b + 0.5);
  679. /* get touch pad resolution to screen resolution ratio */
  680. a1 = (double) (y[4] - y[0]) / (double) (cy[4] - cy[0]);
  681. a2 = (double) (y[3] - y[1]) / (double) (cy[3] - cy[1]);
  682. /* get the minimum pad position on the Y-axis */
  683. b1 = y[0] - a1 * cy[0];
  684. b2 = y[1] - a2 * cy[1];
  685. /* use the average ratio and average minimum position */
  686. a = (a1 + a2) / 2.0;
  687. b = (b1 + b2) / 2.0;
  688. yerr = a * height / 2 + b - y[2];
  689. if (fabs(yerr) > fabs(a * height * 0.01)) {
  690. fprintf(stderr, "Calibration problem: Y axis error (%.2f) too high, try again\n",
  691. fabs(yerr));
  692. goto err;
  693. }
  694. calib.miny = (int) (b + 0.5);
  695. calib.maxy = (int) (a * height + b + 0.5);
  696. XFlush(display);
  697. calib.resx = width;
  698. calib.resy = height;
  699. /* Send new values to the X server */
  700. calib_data[0] = calib.minx;
  701. calib_data[1] = calib.maxx;
  702. calib_data[2] = calib.miny;
  703. calib_data[3] = calib.maxy;
  704. XChangeDeviceProperty(display, device, prop_calibration,
  705. XA_INTEGER, 32, PropModeReplace, (unsigned char *)calib_data, 4);
  706. swap = calib.swapxy;
  707. XChangeDeviceProperty(display, device, prop_swap,
  708. XA_INTEGER, 8, PropModeReplace, (unsigned char *)&swap, 1);
  709. XCloseDevice(display, device);
  710. XCloseDisplay(display);
  711. /* And print them for storage in wsconsctl.conf */
  712. printf("mouse.scale=%d,%d,%d,%d,%d,%d,%d\n",
  713. calib.minx, calib.maxx,
  714. calib.miny, calib.maxy,
  715. calib.swapxy,
  716. calib.resx, calib.resy);
  717. return 0;
  718. err:
  719. draw_text(error_message, &errorColor);
  720. XFlush(display);
  721. sleep(2);
  722. goto calib;
  723. }