// need to link with -lX11 -lX11-xcb -lxcb -lxcb-glx #include #include #include #include #include #include #include #include #include #include #include const std::string currentDateTime() { time_t now = time(0); struct tm tstruct; char buf[80]; tstruct = *localtime(&now); strftime(buf, sizeof(buf), "%X", &tstruct); return buf; } bool initGLX(xcb_connection_t *conn) { const xcb_query_extension_reply_t *reply = xcb_get_extension_data(conn, &xcb_glx_id); if (!reply || !reply->present) return false; xcb_generic_error_t *error = 0; xcb_glx_query_version_cookie_t xglx_query_cookie = xcb_glx_query_version(conn, XCB_GLX_MAJOR_VERSION, XCB_GLX_MINOR_VERSION); xcb_glx_query_version_reply_t *xglx_query = xcb_glx_query_version_reply(conn, xglx_query_cookie, &error); if (!xglx_query || error) { free(error); return false; } free(xglx_query); } // ture OR false, does not work for both cases bool useXlib = false; int main() { xcb_connection_t *conn; if (useXlib) { Display *dpy = XOpenDisplay(getenv("DISPLAY")); if (dpy) { conn = XGetXCBConnection(dpy); XSetEventQueueOwner(dpy, XCBOwnsEventQueue); } } else { conn = xcb_connect(NULL, NULL); } xcb_flush(conn); // ENABLE THIS LINE TO SEE HOW focus_reply WILL BLOCK THIS THREAD WHILE USERS HAS SWITCHED TO VIRTUAL CONSOLE //initGLX(conn); while(true) { std::cout << "syncX: " << currentDateTime() << std::endl; xcb_get_input_focus_cookie_t cookie = xcb_get_input_focus(conn); free(xcb_get_input_focus_reply(conn, cookie, 0)); xcb_flush(conn); sleep(1); } return 0; }