title: Why does this program freeze after the X11 copy to clipboard function returns?
body: <p>Basically, I was messing with X11 functions and I was trying out <a href="https://github.com/exebook/x11clipboard/blob/master/x11copy.c" rel="nofollow noreferrer">this</a>. I was checking if I could use the code when I had found out it froze up and wouldn't continue. Why does it freeze? This is what the code was.</p>
  <pre><code>#include &lt;string.h&gt; // strlen
  #include &lt;X11/Xlib.h&gt;
  
  Display * display;
  Window window;
  Atom targets_atom, text_atom, UTF8, XA_ATOM = 4, XA_STRING = 31;
  
  static void XCopy(Atom selection, unsigned char * text, int size) {
      XEvent event;
      Window owner;
      XSetSelectionOwner (display, selection, window, 0);
      if (XGetSelectionOwner (display, selection) != window) return;
      while (1) {
          XNextEvent (display, &amp;event);
          switch (event.type) {
              case SelectionRequest:
                if (event.xselectionrequest.selection != selection) break;
                XSelectionRequestEvent * xsr = &amp;event.xselectionrequest;
                XSelectionEvent ev = {0};
                int R = 0;
                ev.type = SelectionNotify, ev.display = xsr-&gt;display, ev.requestor = xsr-&gt;requestor,
                ev.selection = xsr-&gt;selection, ev.time = xsr-&gt;time, ev.target = xsr-&gt;target, ev.property = xsr-&gt;property;
                if (ev.target == targets_atom) R = XChangeProperty (ev.display, ev.requestor, ev.property, XA_ATOM, 32,
                      PropModeReplace, (unsigned char*)&amp;UTF8, 1);
                else if (ev.target == XA_STRING || ev.target == text_atom) 
                  R = XChangeProperty(ev.display, ev.requestor, ev.property, XA_STRING, 8, PropModeReplace, text, size);
                else if (ev.target == UTF8)
                  R = XChangeProperty(ev.display, ev.requestor, ev.property, UTF8, 8, PropModeReplace, text, size);
                else ev.property = None;
                if ((R &amp; 2) == 0) XSendEvent (display, ev.requestor, 0, 0, (XEvent *)&amp;ev);
               break;
              case SelectionClear:
               return;
          }
      }
  }
  
  int main(int argc, char *argv[]) {
      display = XOpenDisplay(0);
      int N = DefaultScreen(display);
      window = XCreateSimpleWindow(display, RootWindow(display, N), 0, 0, 1, 1, 0,
          BlackPixel(display, N), WhitePixel(display, N));    
      targets_atom = XInternAtom(display, &quot;TARGETS&quot;, 0);
      text_atom = XInternAtom(display, &quot;TEXT&quot;, 0);
      UTF8 = XInternAtom(display, &quot;UTF8_STRING&quot;, 1);
      if (UTF8 == None) UTF8 = XA_STRING;
      Atom selection = XInternAtom(display, &quot;CLIPBOARD&quot;, 0);
      XCopy(selection, (unsigned char*) argv[1], strlen(argv[1]));
      printf(&quot;TEST&quot;);
      return 0;
  }
  </code></pre>
  <p>I haven't tested with compilers other than gcc.</p>