From dce1817d8eca16058a517be533542f605ec7ac62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Sun, 23 Dec 2012 16:14:58 +0100 Subject: [PATCH] Introduce helper classes to perform common xcb requests Two helper classes WindowGeometry and WindowAttributes which can be used to request the geometry and attributes more easily. This is based on a templated class, taking cookieType, replyType and function pointers to the request and reply functions as template parameters. The ctor performs the async request and the reply is stored in a QSharedPointer. Whenever the reply is needed it is checked whether it has already been retrieved and if not will block by calling the reply method. The class provides operator bool() to check whether the reply succeeded (pointer is not null) and operator->() to directly access the reply pointer. --- xcbutils.h | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 xcbutils.h diff --git a/xcbutils.h b/xcbutils.h new file mode 100644 index 0000000000..ebe7c283c1 --- /dev/null +++ b/xcbutils.h @@ -0,0 +1,102 @@ +/******************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2012, 2013 Martin Gräßlin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*********************************************************************/ +#ifndef KWIN_XCB_UTILS_H +#define KWIN_XCB_UTILS_H + +#include +#include "utils.h" + +#include + +#include + +namespace KWin { + +namespace Xcb { + +typedef xcb_window_t WindowId; + +template +class Wrapper +{ +public: + explicit Wrapper(WindowId window) + : m_retrieved(false) + , m_cookie(requestFunc(connection(), window)) + , m_window(window) + { + } + + inline const Reply *operator->() { + getReply(); + return m_reply.data(); + } + inline operator bool() { + getReply(); + return !m_reply.isNull(); + } + inline const QSharedPointer &data() { + getReply(); + return m_reply; + } + inline WindowId window() const { + return m_window; + } + +protected: + void getReply() { + if (m_retrieved) { + return; + } + m_reply = QSharedPointer(replyFunc(connection(), m_cookie, NULL), &free); + m_retrieved = true; + } + +private: + bool m_retrieved; + Cookie m_cookie; + WindowId m_window; + QSharedPointer m_reply; +}; + +typedef Wrapper WindowAttributes; + + +class WindowGeometry : public Wrapper +{ +public: + explicit WindowGeometry(xcb_window_t window) : Wrapper(window) {} + + inline QRect rect() { + const QSharedPointer &geometry = data(); + if (geometry.isNull()) { + return QRect(); + } + return QRect(geometry->x, geometry->y, geometry->width, geometry->height); + } +}; + +} // namespace X11 + +} // namespace KWin +#endif // KWIN_X11_UTILS_H