~sil2100/unity-2d/precise-geis-rename

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
/*
 * Copyright (C) 2011 Canonical, Ltd.
 *
 * 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; version 3.
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

#include "unity2ddeclarativeview.h"

#include <debug_p.h>
#include <config.h>

#include <QDebug>
#include <QGLWidget>
#include <QVariant>
#include <QX11Info>
#include <QFileInfo>
#include <QShowEvent>

#include <X11/Xlib.h>
#include <X11/Xatom.h>

// libwnck
extern "C" {
#include <libwnck/libwnck.h>
}

#define GOBJECT_CALLBACK0(callbackName, slot) \
static void \
callbackName(GObject* src, void* dummy1, QObject* dst) \
{ \
    QMetaObject::invokeMethod(dst, slot); \
}

GOBJECT_CALLBACK0(activeWorkspaceChangedCB, "onActiveWorkspaceChanged");

Unity2DDeclarativeView::Unity2DDeclarativeView(QWidget *parent) :
    QDeclarativeView(parent),
    m_useOpenGL(false),
    m_transparentBackground(false),
    m_last_focused_window(None)
{
    if (!QFileInfo(UNITY_2D_SCHEMA_FILE).exists()) {
        m_useOpenGL = false;
    } else {
        m_useOpenGL = unity2dConfiguration().property("useOpengl").toBool();
    }

    WnckScreen* screen = wnck_screen_get_default();
    g_signal_connect(G_OBJECT(screen), "active_workspace_changed", G_CALLBACK(activeWorkspaceChangedCB), this);

    setupViewport();
}

Unity2DDeclarativeView::~Unity2DDeclarativeView()
{
}

bool Unity2DDeclarativeView::useOpenGL() const
{
    return m_useOpenGL;
}

void Unity2DDeclarativeView::setUseOpenGL(bool useOpenGL)
{
    if (useOpenGL == m_useOpenGL) {
        return;
    }

    m_useOpenGL = useOpenGL;
    setupViewport();

    Q_EMIT useOpenGLChanged(useOpenGL);
}

bool Unity2DDeclarativeView::transparentBackground() const
{
    return m_transparentBackground;
}

void Unity2DDeclarativeView::setTransparentBackground(bool transparentBackground)
{
    if (transparentBackground == m_transparentBackground) {
        return;
    }

    m_transparentBackground = transparentBackground;
    setupViewport();

    Q_EMIT transparentBackgroundChanged(transparentBackground);
}

QPoint Unity2DDeclarativeView::globalPosition() const
{
    return mapToGlobal(QPoint(0,0));
}

void Unity2DDeclarativeView::setupViewport()
{
    if (m_useOpenGL) {
        QGLFormat format = QGLFormat::defaultFormat();
        format.setSampleBuffers(false);
        /* Synchronise rendering with vblank */
        format.setSwapInterval(1);

        QGLWidget *glWidget = new QGLWidget(format);
        /* TODO: possibly faster, to be tested, only works with non transparent windows */
        //glWidget->setAutoFillBackground(false);

        if (m_transparentBackground) {
            glWidget->setAttribute(Qt::WA_TranslucentBackground, true);
            setAttribute(Qt::WA_TranslucentBackground, true);
            /* automatically set by setting WA_TranslucentBackground */
            setAttribute(Qt::WA_NoSystemBackground, true);
            setAttribute(Qt::WA_OpaquePaintEvent, true);
        } else {
            glWidget->setAttribute(Qt::WA_TranslucentBackground, false);
            setAttribute(Qt::WA_TranslucentBackground, false);
            setAttribute(Qt::WA_NoSystemBackground, true);
            setAttribute(Qt::WA_OpaquePaintEvent, true);
        }

        setViewport(glWidget);
        /* According to Qt's documentation: "This is the preferred update mode
           for viewports that do not support partial updates, such as QGLWidget [...]"
        */
        setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    } else {
        setViewport(0);
        /* This is the default update mode */
        setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate);

        if (m_transparentBackground) {
            viewport()->setAttribute(Qt::WA_TranslucentBackground, true);
            setAttribute(Qt::WA_TranslucentBackground, true);
            /* automatically set by setting WA_TranslucentBackground */
            setAttribute(Qt::WA_NoSystemBackground, true);
            setAttribute(Qt::WA_OpaquePaintEvent, false);
        } else {
            viewport()->setAttribute(Qt::WA_TranslucentBackground, false);
            setAttribute(Qt::WA_TranslucentBackground, false);
            setAttribute(Qt::WA_NoSystemBackground, true);
            setAttribute(Qt::WA_OpaquePaintEvent, true);
        }
    }
}

void Unity2DDeclarativeView::moveEvent(QMoveEvent* event)
{
    Q_EMIT globalPositionChanged(globalPosition());
}

void Unity2DDeclarativeView::showEvent(QShowEvent* event)
{
    QDeclarativeView::showEvent(event);
    Q_EMIT visibleChanged(true);
}

void Unity2DDeclarativeView::hideEvent(QHideEvent* event)
{
    QDeclarativeView::hideEvent(event);
    Q_EMIT visibleChanged(false);
}

/* Obtaining & Discarding Keyboard Focus for Window on Demand
 *
 * In the X world, activating a window means to give it the input (keyboard)
 * focus. When a new window opens, X usually makes it active immediately.
 * Clicking on a window makes it active too.
 *
 * Qt does not have the capability to explicitly ask the window manager to
 * make an existing window active - setFocus() only forwards input focus to
 * whatever QWidget you specify.
 *
 * De-Activating a window is not possible with X (and hence with Qt). So
 * we work-around this by remembering which application is active prior to
 * stealing focus, and then Re-Activating it when we're finished. This is
 * not guaranteed to succeed, as previous window may have closed.
 *
 * The following methods deal with these tasks. Note that when the window
 * has been activated (deactivated), Qt will realise it has obtained (lost)
 * focus and act appropriately.
 */

/* Ask Window Manager to activate this window and hence get keyboard focus */
void Unity2DDeclarativeView::forceActivateWindow()
{
    // Save reference to window with current keyboard focus
    if( m_last_focused_window == None ){
        saveActiveWindow();
    }

    // Show this window by giving it keyboard focus
    forceActivateThisWindow(this->effectiveWinId());
}

/* Ask Window Manager to deactivate this window - not guaranteed to succeed. */
void Unity2DDeclarativeView::forceDeactivateWindow()
{
    if( m_last_focused_window == None ){
        UQ_WARNING << "No previously focused window found, use mouse to select window.";
        return;
    }

    // What if previously focused window closed while we we had focus? Check if window
    // exists by seeing if it has attributes.
    int status;
    XWindowAttributes attributes;
    status = XGetWindowAttributes(QX11Info::display(), m_last_focused_window, &attributes);
    if ( status == BadWindow ){
        UQ_WARNING << "Previously focused window has gone, use mouse to select window.";
        return;
    }

    // Show this window by giving it keyboard focus
    forceActivateThisWindow(m_last_focused_window);

    m_last_focused_window = None;
}

void Unity2DDeclarativeView::forceActivateThisWindow(WId window)
{
    /* Workaround focus stealing prevention implemented by some window
       managers such as Compiz. This is the exact same code you will find in
       libwnck::wnck_window_activate().

       ref.: http://permalink.gmane.org/gmane.comp.lib.qt.general/4733
    */
    Display* display = QX11Info::display();
    Atom net_wm_active_window = XInternAtom(display, "_NET_ACTIVE_WINDOW",
                                            False);
    XEvent xev;
    xev.xclient.type = ClientMessage;
    xev.xclient.send_event = True;
    xev.xclient.display = display;
    xev.xclient.window = window;
    xev.xclient.message_type = net_wm_active_window;
    xev.xclient.format = 32;
    xev.xclient.data.l[0] = 2;
    xev.xclient.data.l[1] = CurrentTime;
    xev.xclient.data.l[2] = 0;
    xev.xclient.data.l[3] = 0;
    xev.xclient.data.l[4] = 0;

    XSendEvent(display, QX11Info::appRootWindow(), False,
               SubstructureRedirectMask | SubstructureNotifyMask, &xev);

    /* Ensure focus is actually switched to active window */
    XSetInputFocus(display, window, RevertToParent, CurrentTime);
    XFlush(display);
}

/* Save WId of window with keyboard focus to m_last_focused_window */
void Unity2DDeclarativeView::saveActiveWindow()
{
    Display* display = QX11Info::display();
    WId active_window;
    int current_focus_state;

    XGetInputFocus(display, &active_window, &current_focus_state);
    if( active_window != this->effectiveWinId()){
        m_last_focused_window = active_window;
    }
}

void Unity2DDeclarativeView::onActiveWorkspaceChanged() 
{
    m_last_focused_window = None;
    Q_EMIT activeWorkspaceChanged();
}

#include <unity2ddeclarativeview.moc>