File indexing completed on 2024-04-21 05:27:32

0001 /*
0002 SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0003 
0004 SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include <QCoreApplication>
0007 #include <xcb/xcb.h>
0008 
0009 xcb_screen_t *defaultScreen(xcb_connection_t *c, int screen)
0010 {
0011     for (auto it = xcb_setup_roots_iterator(xcb_get_setup(c)); it.rem; --screen, xcb_screen_next(&it)) {
0012         if (screen == 0) {
0013             return it.data;
0014         }
0015     }
0016 
0017     return nullptr;
0018 }
0019 
0020 xcb_window_t rootWindow(xcb_connection_t *c, int screen)
0021 {
0022     xcb_screen_t *s = defaultScreen(c, screen);
0023     if (!s) {
0024         return XCB_WINDOW_NONE;
0025     }
0026     return s->root;
0027 }
0028 
0029 /**
0030  * This app grabs the keyboard from X.
0031  * It is used from ksldtest to verify we report if grabbing failed when the keyboard has been grabbed by another
0032  * X Client. It needs to be another application, otherwise the grab cannot fail.
0033  **/
0034 int main(int argc, char **argv)
0035 {
0036     QCoreApplication app(argc, argv);
0037 
0038     // connect to xcb
0039     int screen = 0;
0040     xcb_connection_t *c = xcb_connect(nullptr, &screen);
0041     Q_ASSERT(c);
0042 
0043     xcb_grab_keyboard(c, 1, rootWindow(c, screen), XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
0044     xcb_flush(c);
0045 
0046     const int exitCode = app.exec();
0047 
0048     xcb_ungrab_keyboard(c, XCB_CURRENT_TIME);
0049     xcb_flush(c);
0050     xcb_disconnect(c);
0051 
0052     return exitCode;
0053 }