File xshowcursor.c of Package jw-env

/*
 * xshowcursor.c -- simple tool to be called after using xrandr.
 * xrandr has the unfortunate side effect of diabling the mouse cursor sometimes.
 * We find that moving focus to an xterm, tping o key and moving the mouse makes it reappear.
 *
 * This appears more than what xterm does, but it does not help.
 *
 * 2009-12-07, jw@suse.de
 *
 * zypper in xorg-x11-libX11-devel
 * make xshowcursor LDFLAGS=-lX11
 */
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/cursorfont.h>	// XC_crosshair XC_xterm XC_clock ...

main()
{
  Display *dpy = XOpenDisplay(NULL);
  int scr = DefaultScreen(dpy);
  Window w = XCreateSimpleWindow(dpy, RootWindow(dpy, scr), 0, 0, 500, 500, \
  0, BlackPixel (dpy, 0), BlackPixel(dpy, 0));
  XMapWindow(dpy, w);
  XSync(dpy,1); // wait for the window to really exist.

  int i;
  for (i = 0; i < 1000; i++)
    {
      if (XGrabPointer(dpy,w,0,0,GrabModeSync,GrabModeSync,w,None,CurrentTime) == GrabSuccess)
        break;
      XSync(dpy,1); // prevent optimization, if any.
    }

  // Big fat ugly race here. Values like 25 or 61 seen.
  if (i == 1000)
    printf("XGrabPointer failed\n");
//  else
//    printf("%d\n", i);
  XSync(dpy,1); // prevent optimization, if any.
  Cursor c = XCreateFontCursor(dpy, XC_crosshair);
  XDefineCursor(dpy,w,c);
  XSync(dpy,1); // prevent optimization, if any.
  sleep(0);
  c = XCreateFontCursor(dpy, XC_X_cursor);
  XDefineCursor(dpy,w,c);
  XSync(dpy,1); // prevent optimization, if any.
  XUngrabPointer(dpy,CurrentTime);
}
openSUSE Build Service is sponsored by