static xcb_window_t create_window(xcb_connection_t *conn, xcb_screen_t *screen, xcb_image_t *background) { xcb_window_t window = xcb_generate_id(conn); xcb_create_window(conn, XCB_COPY_FROM_PARENT, window, screen->root, 0, 0, screen->width_in_pixels, screen->height_in_pixels, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, XCB_CW_BACK_PIXMAP | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_CURSOR, (uint32_t[]){ image_to_pixmap(conn, screen, background), 1, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS, create_empty_cursor(conn, screen) }); const char name[] = "Hamartu"; xcb_change_property(conn, XCB_PROP_MODE_REPLACE, window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, sizeof(name), name); xcb_configure_window(conn, window, XCB_CONFIG_WINDOW_STACK_MODE, (uint32_t[]){ XCB_STACK_MODE_ABOVE }); return window; }
struct rutabaga * window_impl_rtb_alloc(void) { struct xcb_rutabaga *self; Display *dpy; if (!(self = calloc(1, sizeof(*self)))) goto err_malloc; if (!(self->dpy = dpy = XOpenDisplay(NULL))) { ERR("can't open X display\n"); goto err_dpy; } if (!(self->xcb_conn = XGetXCBConnection(dpy))) { ERR("can't get XCB connection for display\n"); goto err_get_conn; } if (xrtb_keyboard_init(self)) { ERR("can't initialize keyboard\n"); goto err_keyboard_init; } XSetEventQueueOwner(dpy, XCBOwnsEventQueue); self->xkb_event = get_xkb_event_id(self->xcb_conn); self->xkb_core_kbd_id = get_core_kbd_id(self->xcb_conn); if (self->xkb_event >= 0 && self->xkb_core_kbd_id >= 0 && !receive_xkb_events(self->xcb_conn)) self->xkb_supported = 1; self->running_in_xwayland = intern_atom(self->xcb_conn, "WL_SURFACE_ID", 1) != XCB_NONE; #define INTERN_ATOM(atom, name) \ self->atoms.atom = intern_atom(self->xcb_conn, name, 0); INTERN_ATOM(wm_protocols, "WM_PROTOCOLS"); INTERN_ATOM(wm_delete_window, "WM_DELETE_WINDOW"); #undef INTERN_ATOM self->empty_cursor = create_empty_cursor(self); return (struct rutabaga *) self; err_keyboard_init: err_get_conn: XCloseDisplay(self->dpy); err_dpy: free(self); err_malloc: return NULL; }
int main(int argc, char *argv[], char *env[]) { char *display_name = NULL; char *device_name = NULL; char *output_name = NULL; XSetWindowAttributes xswa; int i = 0; double a, a1, a2, b, b1, b2, xerr, yerr; int xi_opcode, event, error; XExtensionVersion *version; XDeviceInfo *info; XDevice *device; long calib_data[4]; unsigned long mask; unsigned char swap; int keep_cursor = 0, ch; /* Crosshair placement */ int cpx[] = { 0, 0, 1, 1, 1 }; int cpy[] = { 0, 1, 0, 0, 1 }; while ((ch = getopt(argc, argv, "cD:d:o:v")) != -1) { switch (ch) { case 'c': keep_cursor++; break; case 'D': display_name = optarg; break; case 'd': device_name = optarg; break; case 'o': output_name = optarg; break; case 'v': verbose = True; break; default: usage(); /* NOTREACHED */ } } argc -= optind; argv += optind; if (argc != 0) usage(); /* connect to X server */ if ((display = XOpenDisplay(display_name)) == NULL) { fprintf(stderr, "%s: cannot connect to X server %s\n", __progname, XDisplayName(display_name)); exit(1); } screen = DefaultScreen(display); root = RootWindow(display, screen); /* get screen size from display structure macro */ xpos = 0; ypos = 0; width = DisplayWidth(display, screen); height = DisplayHeight(display, screen); if (XRRQueryExtension(display, &event, &error)) { int major, minor; if (XRRQueryVersion(display, &major, &minor) != True) { fprintf(stderr, "Error querying XRandR version"); } else { printf("XRandR extension version %d.%d present\n", major, minor); has_xrandr = True; if (major > 1 || (major == 1 && minor >=2)) has_xrandr_1_2 = True; if (major > 1 || (major == 1 && minor >=3)) has_xrandr_1_3 = True; } } if (output_name != NULL) { if (has_xrandr_1_2) { get_xrandr_config(display, root, output_name, &xpos, &ypos, &width, &height); } else { fprintf(stderr, "%s: can not specify an output " "whithout XRandr 1.2 or later", __progname); exit(2); } } if (!XQueryExtension(display, INAME, &xi_opcode, &event, &error)) { fprintf(stderr, "%s: X Input extension not available.\n", __progname); exit(1); } version = XGetExtensionVersion(display, INAME); if (version == NULL || version == (XExtensionVersion *)NoSuchExtension) { fprintf(stderr, "Cannot query X Input version.\n"); exit(1); } XFree(version); prop_calibration = XInternAtom(display, WS_PROP_CALIBRATION, True); if (prop_calibration == None) { fprintf(stderr, "Unable to find the \"%s\" device property.\n" "There are probably no calibrable devices " "on this system.\n", WS_PROP_CALIBRATION); exit(1); } prop_swap = XInternAtom(display, WS_PROP_SWAP_AXES, True); if (prop_swap == None) { fprintf(stderr, "Unable to find the \"%s\" device property\n", WS_PROP_SWAP_AXES); exit(1); } info = find_device_info(device_name); if (info == NULL) { fprintf(stderr, "Unable to find the %s device\n", device_name ? device_name : "default"); exit(1); } /* setup window attributes */ xswa.override_redirect = True; xswa.background_pixel = BlackPixel(display, screen); xswa.event_mask = ExposureMask | KeyPressMask; mask = CWOverrideRedirect | CWBackPixel | CWEventMask; if (!keep_cursor) { xswa.cursor = create_empty_cursor(); mask |= CWCursor; } win = XCreateWindow(display, RootWindow(display, screen), xpos, ypos, width, height, 0, CopyFromParent, InputOutput, CopyFromParent, mask, &xswa); render_init(); XMapWindow(display, win); XGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime); XGrabServer(display); XClearWindow(display, win); if (verbose) printf("Calibrating %s\n", info->name); device = XOpenDevice(display, info->id); if (!device) { fprintf(stderr, "Unable to open the X input device \"%s\"\n", info->name); return 0; } if (!register_events(info, device, 0)) exit(1); uncalibrate(device); calib: XftDrawRect(draw, &bg, 0, 0, width, height); for (i = 0; i < 5; i++) { draw_graphics(cpx[i], cpy[i], i); XFlush(display); if (!get_events(i)) break; XftDrawRect(draw, &bg, 0, 0, width, height); } if (interrupted) cleanup_exit(device); /* Check if X and Y should be swapped */ if (fabs(x[0] - x[1]) > fabs(y[0] - y[1])) { calib.swapxy = 1; for (i = 0; i < 5; i++) { int t = x[i]; x[i] = y[i]; y[i] = t; } } /* get touch pad resolution to screen resolution ratio */ a1 = (double) (x[4] - x[0]) / (double) (cx[4] - cx[0]); a2 = (double) (x[3] - x[1]) / (double) (cx[3] - cx[1]); /* get the minimum pad position on the X-axis */ b1 = x[0] - a1 * cx[0]; b2 = x[1] - a2 * cx[1]; /* use the average ratio and average minimum position */ a = (a1 + a2) / 2.0; b = (b1 + b2) / 2.0; xerr = a * width / 2 + b - x[2]; if (fabs(xerr) > fabs(a * width * .01)) { fprintf(stderr, "Calibration problem: X axis error (%.2f) too high, try again\n", fabs(xerr)); goto err; } calib.minx = (int) (b + 0.5); calib.maxx = (int) (a * width + b + 0.5); /* get touch pad resolution to screen resolution ratio */ a1 = (double) (y[4] - y[0]) / (double) (cy[4] - cy[0]); a2 = (double) (y[3] - y[1]) / (double) (cy[3] - cy[1]); /* get the minimum pad position on the Y-axis */ b1 = y[0] - a1 * cy[0]; b2 = y[1] - a2 * cy[1]; /* use the average ratio and average minimum position */ a = (a1 + a2) / 2.0; b = (b1 + b2) / 2.0; yerr = a * height / 2 + b - y[2]; if (fabs(yerr) > fabs(a * height * 0.01)) { fprintf(stderr, "Calibration problem: Y axis error (%.2f) too high, try again\n", fabs(yerr)); goto err; } calib.miny = (int) (b + 0.5); calib.maxy = (int) (a * height + b + 0.5); XFlush(display); calib.resx = width; calib.resy = height; /* Send new values to the X server */ calib_data[0] = calib.minx; calib_data[1] = calib.maxx; calib_data[2] = calib.miny; calib_data[3] = calib.maxy; XChangeDeviceProperty(display, device, prop_calibration, XA_INTEGER, 32, PropModeReplace, (unsigned char *)calib_data, 4); swap = calib.swapxy; XChangeDeviceProperty(display, device, prop_swap, XA_INTEGER, 8, PropModeReplace, (unsigned char *)&swap, 1); XCloseDevice(display, device); XCloseDisplay(display); /* And print them for storage in wsconsctl.conf */ printf("mouse.scale=%d,%d,%d,%d,%d,%d,%d\n", calib.minx, calib.maxx, calib.miny, calib.maxy, calib.swapxy, calib.resx, calib.resy); return 0; err: draw_text(error_message, &errorColor); XFlush(display); sleep(2); goto calib; }