title: Detect window focus changes with XCB
body: <p>I'm writing a program with XCB that needs to detect whenever a window gains or loses focus. So far I have this but it just hangs on the <code>xcb_wait_for_event</code> call, never entering the loop. What am I missing here to grab root events? Or am I just going about this totally wrong and there's a better way than listening to the root?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;xcb/xcb.h&gt;
  
  int main (int argc, char **argv)
  {
      xcb_connection_t* conn = xcb_connect(NULL, NULL);
      if (xcb_connection_has_error(conn)) {
          printf("Cannot open daemon connection.");
          return 0;
      }
  
      xcb_screen_t* screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
  
      uint32_t values[] = { XCB_EVENT_MASK_FOCUS_CHANGE };
      xcb_change_window_attributes(
          conn,
          screen-&gt;root,
          XCB_CW_EVENT_MASK,
          values);
  
      xcb_generic_event_t *ev;
      while ((ev = xcb_wait_for_event(conn))) {
          printf("IN LOOP\n");
          switch (ev-&gt;response_type &amp; 0x7F) {
          case XCB_FOCUS_IN:
          case XCB_FOCUS_OUT:
              printf("IN CASE\n");
              break;
          default:
              printf("IN DEFAULT\n");
              break;
          }
          free(ev);
      }
  
      return 0;
  }
  </code></pre>