title: How to hide the mouse cursor in X11/Xorg
body: <p>Why does the following X11/Xorg code not hide the mouse cursor under Ubuntu 18.04? If this is not the way to do it what is? Is there some missing dependency/library/.dev package?</p>
  
  <p>My intuition says this may be a bug in Ubuntu's (or Debian) X11/Xorg packages or some such. This is how Haxe/Kha does Mouse hiding in order to be compatible across platforms.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;X11/X.h&gt;
  #include &lt;X11/Xlib.h&gt;
  #include &lt;X11/keysym.h&gt;
  #include &lt;X11/Xutil.h&gt;
  #include &lt;X11/Xos.h&gt;
  
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  Window window;
  void show();
  
  int main()
  {
      Display *display;
      XEvent e;
      std::string msg = "Hello, World!";
      int screen;
  
      display = XOpenDisplay(nullptr);
      if (display == nullptr)
      {
          std::cerr &lt;&lt; "Cannot open display\n";
          throw;
      }
  
      screen = XDefaultScreen(display);
      window = XCreateSimpleWindow(display, RootWindow(display, screen), 10, 10, 500, 500, 1,
                                   BlackPixel(display, screen), WhitePixel(display, screen));
      XStoreName(display, window, "Silly Window");
      XSelectInput(display, window, ExposureMask | KeyPressMask );
      XMapWindow(display, window);
  
      while (true)
      {
          XNextEvent(display, &amp;e);
          if (e.type == Expose)
          {
              std::cout &lt;&lt; "Window Exposed!\n";
              XExposeEvent ev = e.xexpose;
              if (ev.window != window) continue;
              XFillRectangle(display, window, DefaultGC(display, screen), 50, 50, 400, 50);
              XDrawString(display, window, DefaultGC(display, screen), 220, 220, msg.c_str(), msg.length());
              XFillRectangle(display, window, DefaultGC(display, screen), 50, 400, 400, 50);
          }
          else if (e.type == KeyPress)
          {
              char buf[128] = {0};
              KeySym keysym;
              int len = XLookupString(&amp;e.xkey, buf, sizeof buf, &amp;keysym, NULL);
              if (keysym == XK_Escape)
              {
                  break;
              }
              else if (keysym == XK_space)
              {
                  show();
                  XAllowEvents(display, SyncBoth, CurrentTime);
              }
          }
  
      }
  
      XDestroyWindow(display, window);
      XCloseDisplay(display);
      return 0;
  }
  
  bool toggle = false;
  void show()
  {
      Display* dpy = XOpenDisplay(0);
      if (toggle)
      {
          std::cout &lt;&lt; "toggle On\n";
          XUndefineCursor(dpy, window);
      }
      else
      {
          std::cout &lt;&lt; "toggle Off\n";
          XColor col;
          char data[1] = {0X00};
          Pixmap blank = XCreateBitmapFromData(dpy, window, data, 1, 1);
          Cursor cursor = XCreatePixmapCursor(dpy, blank, blank, &amp;col, &amp;col, 0, 0);
          XDefineCursor(dpy, window, cursor);
          // XSetWindowAttributes wa;
          // XChangeWindowAttributes(dpy, window, CWCursor, &amp;wa);
          // wa.cursor = cursor;
          XFreePixmap(dpy, blank);
      }
      toggle = !toggle;
  }
  </code></pre>