summaryrefslogtreecommitdiff
path: root/src/mwindowpropertycache.h
blob: 0edb34aa10f89faa5ff38afa3cc022b68a5ef856 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/***************************************************************************
**
** 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 <QRegion>
#include <QX11Info>
#include <X11/Xutil.h>
#include <X11/Xlib-xcb.h>
#include <xcb/render.h>
#include <xcb/shape.h>
#include <X11/extensions/shape.h>
#include <X11/extensions/Xdamage.h>
#include "mcompatoms_p.h"

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

    /*! Construct a MWindowPropertyCache
     * \param window id to the window whose properties are cached
     *        Without one constructs a placeholder object.
     */
    MWindowPropertyCache();
    MWindowPropertyCache(Window window,
                         xcb_get_window_attributes_reply_t *attrs = 0,
                         xcb_get_geometry_reply_t *geom = 0,
                         Damage damage_obj = 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 (!is_valid)
            return;
        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 (is_valid && !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 (!is_valid)
            return;
        if (!shape_rects_valid)
            shapeRegion();
        shape_rects_valid = false;
        xcb_shape_rects_cookie = xcb_shape_get_rectangles(xcb_conn, window,
                                                          ShapeBounding);
    }

    Window winId() const { return window; }
    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();

    /*!
     * Returns list of transients of the window.
     */
    const QList<Window>& transientWindows() const { return transients; }

    // used to set the atom list now, for immediate effect in e.g. stacking
    void setNetWmState(const QList<Atom>& s) {
        if (!is_valid)
            return;
        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; }
    void setDontIconify(bool s) { dont_iconify = s; }
    bool dontIconify();

    bool isMapped() const {
        if (!is_valid || !attrs)
            return false;
        return attrs->map_state == XCB_MAP_STATE_VIEWABLE;
    }

    void setIsMapped(bool s) {
        if (!is_valid || !attrs)
            return;
        // 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_UNMAPPED;
    }

    /*!
     * 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 {
        if (!is_valid || !attrs)
            return false;
        return attrs->override_redirect;
    }
    bool isInputOnly() const {
        if (!is_valid || !attrs)
            return false;
        return attrs->_class == XCB_WINDOW_CLASS_INPUT_ONLY;
    }

    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();

    /*!
     * Returns value of _MEEGOTOUCH_ALWAYS_MAPPED.
     */
    int alwaysMapped();

    /*!
     * Returns value of _MEEGOTOUCH_CANNOT_MINIMIZE.
     */
    int cannotMinimize();

    /*!
     * Returns value of _MEEGOTOUCH_DESKTOP_VIEW (makes sense for desktop only).
     */
    int desktopView(bool request_only = false);

    /*!
     * Returns value of _MEEGOTOUCH_CUSTOM_REGION.
     */
    const QRegion &customRegion(bool request_only = false);

    /*!
     * Returns the value of _MEEGOTOUCH_ORIENTATION_ANGLE.
     */
    unsigned orientationAngle();

    /*!
     * Returns the value of _MEEGOTOUCH_MSTATUSBAR_GEOMETRY.
     */
    const QRect &statusbarGeometry();

    /*!
     * 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();
    int videoGlobalAlpha();
    bool is_valid;

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

    void damageTracking(bool enabled)
    {
        if (!is_valid || (damage_object && enabled))
            return;
        if (damage_object && !enabled) {
            XDamageDestroy(QX11Info::display(), damage_object);
            damage_object = 0;
        }
        else if (enabled && !damage_object && !isInputOnly())
            damage_object = XDamageCreate(QX11Info::display(), window,
                                          XDamageReportNonEmpty); 
    }

signals:
    void iconGeometryUpdated();
    void meegoDecoratorButtonsChanged(Window w);
    void desktopViewChanged(MWindowPropertyCache *pc);
    void alwaysMappedChanged(MWindowPropertyCache *pc);
    void customRegionChanged(MWindowPropertyCache *pc);

private:
    void init();
    void init_invalid();
    int alphaValue(xcb_get_property_cookie_t c);
    void buttonGeometryHelper();

    Atom window_type_atom;
    Window transient_for;
    QList<Window> transients;
    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 video_global_alpha;
    int is_decorator;
    QList<Atom> net_wm_state;
    QRect req_geom, real_geom, statusbar_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;
    int always_mapped, cannot_minimize, desktop_view;
    bool being_mapped, dont_iconify;
    QRegion *custom_region;
    bool custom_region_request_fired;
    unsigned orientation_angle;
    // 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_video_global_alpha_cookie;
    xcb_get_property_cookie_t xcb_net_wm_state_cookie;
    xcb_get_property_cookie_t xcb_always_mapped_cookie;
    xcb_get_property_cookie_t xcb_cannot_minimize_cookie;
    xcb_get_property_cookie_t xcb_custom_region_cookie;
    xcb_get_property_cookie_t xcb_orientation_angle_cookie;
    xcb_get_property_cookie_t xcb_statusbar_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;
    Damage damage_object;
};

// Non-deletable dummy MWindowPropertyCache.
class MWindowDummyPropertyCache: public MWindowPropertyCache
{
public:
    static MWindowDummyPropertyCache *get();

private:
    virtual bool event(QEvent *e);

    static MWindowDummyPropertyCache *singleton;
};

#endif