summaryrefslogtreecommitdiff
path: root/src/mwindowpropertycache.h
blob: 4c582e5ef6e5dd8cd44be16b2743e87e4f9d2fd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of mcompositor.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#ifndef MWINDOWPROPERTYCACHE_H
#define MWINDOWPROPERTYCACHE_H

#include <X11/Xutil.h>
#include <X11/Xlib-xcb.h>
#include <xcb/render.h>
#include <xcb/shape.h>
#include <X11/extensions/shape.h>
#include "mcompatoms_p.h"
#include <QRegion>

/*!
 * This is a class for caching window property values for a window.
 */
class MWindowPropertyCache
{
public:

    /*! Construct a MWindowPropertyCache
     * \param window id to the window whose properties are cached
     */
    MWindowPropertyCache(Window window,
                         xcb_get_window_attributes_reply_t *attrs = 0,
                         xcb_get_geometry_reply_t *geom = 0);
    virtual ~MWindowPropertyCache();

    Atom windowTypeAtom() const { return window_type_atom; }

    void setWindowTypeAtom(Atom atom) { window_type_atom = atom; }

    void setRequestedGeometry(const QRect &rect) {
        req_geom = rect;
    }
    const QRect requestedGeometry() const {
        return req_geom;
    }

    // this is called on ConfigureNotify
    void setRealGeometry(const QRect &rect) {
        if (!xcb_real_geom)
            xcb_real_geom = xcb_get_geometry_reply(xcb_conn,
                                                   xcb_real_geom_cookie, 0);
        real_geom_valid = true;
        real_geom = rect;

        // shape needs to be refreshed in case it was the default value
        // (i.e. the same as geometry), because there is no ShapeNotify
        if ((shape_rects_valid && QRegion(real_geom) != shape_region)
            || !shape_rects_valid)
            shapeRefresh();
    }
    const QRect realGeometry() {
        if (!xcb_real_geom) {
            xcb_real_geom = xcb_get_geometry_reply(xcb_conn,
                                                   xcb_real_geom_cookie, 0);
            if (xcb_real_geom && !real_geom_valid) {
                real_geom = QRect(xcb_real_geom->x, xcb_real_geom->y,
                                  xcb_real_geom->width, xcb_real_geom->height);
                real_geom_valid = true;
            }
        }
        return real_geom;
    }
    const QRegion &shapeRegion();
    void shapeRefresh() {
        if (!shape_rects_valid)
            shapeRegion();
        shape_rects_valid = false;
        xcb_shape_rects_cookie = xcb_shape_get_rectangles(xcb_conn, window,
                                                          ShapeBounding);
    }

    Window parentWindow() const { return parent_window; }
    void setParentWindow(Window w) { parent_window = w; }

    /*!
     * Returns value of TRANSIENT_FOR property.
     */
    Window transientFor();

    /*!
     * Returns true if we should give focus to this window.
     */
    bool wantsFocus();

    /*!
     * Returns the window group or 0.
     */
    XID windowGroup();

    /*!
     * Returns list of WM_PROTOCOLS of the window.
     */
    const QList<Atom>& supportedProtocols();

    /*!
     * Returns list of _NET_WM_STATE of the window.
     */
    const QList<Atom>& netWmState();

    // used to set the atom list now, for immediate effect in e.g. stacking
    void setNetWmState(const QList<Atom>& s) {
        if (!net_wm_state_valid)
            // receive the obsolete query
            netWmState();
        net_wm_state_valid = true;
        net_wm_state = s;
    }

    /*!
     * Returns true if this window has received MapRequest but not
     * the MapNotify yet.
     */
    bool beingMapped() const { return being_mapped; }
    void setBeingMapped(bool s) { being_mapped = s; }

    bool isMapped() const { return attrs->map_state == XCB_MAP_STATE_VIEWABLE; }

    void setIsMapped(bool s) {
        // a bit ugly but avoids a round trip to X...
        if (s)
            attrs->map_state = XCB_MAP_STATE_VIEWABLE;
        else
            attrs->map_state = XCB_MAP_STATE_UNVIEWABLE;
    }

    /*!
     * Returns the first cardinal of WM_STATE of this window
     */
    int windowState();

    void setWindowState(int state) { window_state = state; }
    
    /*!
     * Returns whether override_redirect flag was in XWindowAttributes at
     * object creation time.
     */
    bool isOverrideRedirect() const { return attrs->override_redirect; }

    const XWMHints &getWMHints();

    const xcb_get_window_attributes_reply_t* windowAttributes() const {
            return attrs; };

    const QRectF &iconGeometry();

    const QRect &homeButtonGeometry();
    const QRect &closeButtonGeometry();

    /*!
     * Returns value of _MEEGO_STACKING_LAYER. The value is between [0, 6].
     */
    unsigned int meegoStackingLayer();

    /*!
     * Called on PropertyNotify for this window.
     * Returns true if we should re-check stacking order.
     */
    bool propertyEvent(XPropertyEvent *e);

    MCompAtoms::Type windowType();

    bool hasAlpha();
    bool isDecorator();
    int globalAlpha();
    bool is_valid;

    static void set_xcb_connection(xcb_connection_t *c) {
        MWindowPropertyCache::xcb_conn = c;
    }

private:
    void buttonGeometryHelper();

    Atom window_type_atom;
    Window transient_for;
    QList<Atom> wm_protocols;
    bool wm_protocols_valid;
    bool icon_geometry_valid;
    bool decor_buttons_valid;
    bool shape_rects_valid;
    bool real_geom_valid;
    bool net_wm_state_valid;
    bool wm_state_query;
    QRectF icon_geometry;
    int has_alpha;
    int global_alpha;
    int is_decorator;
    QList<Atom> net_wm_state;
    QRect req_geom, real_geom;
    QRect home_button_geom, close_button_geom;
    XWMHints *wmhints;
    xcb_get_window_attributes_reply_t *attrs;
    int meego_layer, window_state;
    MCompAtoms::Type window_type;
    Window window, parent_window;
    bool being_mapped;
    // geometry is requested only once in the beginning, after that, we
    // use ConfigureNotifys to update the size through setRealGeometry()
    xcb_get_geometry_reply_t *xcb_real_geom;
    xcb_get_geometry_cookie_t xcb_real_geom_cookie;
    xcb_get_property_cookie_t xcb_transient_for_cookie;
    xcb_get_property_cookie_t xcb_meego_layer_cookie;
    xcb_get_property_cookie_t xcb_is_decorator_cookie;
    xcb_get_property_cookie_t xcb_window_type_cookie;
    xcb_get_property_cookie_t xcb_decor_buttons_cookie;
    xcb_get_property_cookie_t xcb_wm_protocols_cookie;
    xcb_get_property_cookie_t xcb_wm_state_cookie;
    xcb_get_property_cookie_t xcb_wm_hints_cookie;
    xcb_get_property_cookie_t xcb_icon_geom_cookie;
    xcb_get_property_cookie_t xcb_global_alpha_cookie;
    xcb_get_property_cookie_t xcb_net_wm_state_cookie;
    xcb_render_query_pict_formats_cookie_t xcb_pict_formats_cookie;
    xcb_shape_get_rectangles_cookie_t xcb_shape_rects_cookie;
    QRegion shape_region;

    static xcb_connection_t *xcb_conn;
};

#endif