summaryrefslogtreecommitdiff
path: root/decorators/mdecorator/mdecoratorwindow.cpp
blob: bb5534b3280a60403d18e8e0c373d5365fde948b (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
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of duicompositor.
**
** 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.
**
****************************************************************************/

#include <QtDebug>

#include <MSceneManager>
#include <MScene>

#include <QCoreApplication>
#include <QX11Info>
#include <QGLFormat>
#include <QGLWidget>

#include "mdecoratorwindow.h"

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xmd.h>
#include <X11/extensions/Xfixes.h>
#ifdef HAVE_SHAPECONST
#include <X11/extensions/shapeconst.h>
#else
#include <X11/extensions/shape.h>
#endif

#include <mabstractdecorator.h>


class MDecorator: public MAbstractDecorator
{
    Q_OBJECT
public:
    MDecorator(MDecoratorWindow *p)
        : MAbstractDecorator(p)
    {
        connect(this, SIGNAL(windowTitleChanged(const QString&)),
                p, SIGNAL(windowTitleChanged(const QString&)));
    }

    ~MDecorator() {
    }

protected:
    virtual void activateEvent() {
    }

    virtual void manageEvent(Qt::HANDLE window)
    {
        XTextProperty p;
        QString title;

        if(XGetWMName(QX11Info::display(), window, &p)) {
            if (p.value) {
                title = (char*) p.value;
                XFree(p.value);
            }
        }

        emit windowTitleChanged(title);
    }

signals:

    void windowTitleChanged(const QString&);
};

MDecoratorWindow::MDecoratorWindow(QWidget *parent)
    : MWindow(parent)
{
    setTranslucentBackground(true);
    // We do not rotate (change orientation) at all.
    setOrientationAngle(M::Angle0);
    setOrientationAngleLocked(true);

    MDecorator *d = new MDecorator(this);
    connect(this, SIGNAL(homeClicked()), d, SLOT(minimize()));
    connect(this, SIGNAL(escapeClicked()), d, SLOT(close()));
}

MDecoratorWindow::~MDecoratorWindow()
{
}

void MDecoratorWindow::init(MSceneManager &sceneManager)
{
    setFocusPolicy(Qt::NoFocus);
    setSceneSize(sceneManager);
    setMDecoratorWindowProperty();
}

void MDecoratorWindow::setInputRegion(const QRegion &region)
{
    Display *dpy = QX11Info::display();

    int size = region.rects().size();

    //we should receive correct pointer even if region is empty
    XRectangle *rects = (XRectangle *)malloc(sizeof(XRectangle) * size);

    XRectangle *rect = rects;
    for (int i = 0; i < size; i ++, rect++) {
        fillXRectangle(rect, region.rects().at(i));
    }


    XserverRegion shapeRegion = XFixesCreateRegion(dpy, rects, size);
    XFixesSetWindowShapeRegion(dpy, winId(), ShapeBounding, 0, 0, 0);
    XFixesSetWindowShapeRegion(dpy, winId(), ShapeInput, 0, 0, shapeRegion);

    XFixesDestroyRegion(dpy, shapeRegion);

    free(rects);
    XSync(dpy, False);

    // selective compositing
    if (isVisible() && region.isEmpty()) {
        hide();
    } else if (!isVisible() && !region.isEmpty()) {
        show();
    }
}

void MDecoratorWindow::fillXRectangle(XRectangle *xRect, const QRect &rect) const
{
    xRect->x = rect.x();
    xRect->y = rect.y();
    xRect->width = rect.width();
    xRect->height = rect.height();
}

void MDecoratorWindow::setSceneSize(MSceneManager &sceneManager)
{
    QSize sceneSize = sceneManager.visibleSceneSize();
    scene()->setSceneRect(0, 0, sceneSize.width(), sceneSize.height());
    setMinimumSize(sceneSize.width(), sceneSize.height());
    setMaximumSize(sceneSize.width(), sceneSize.height());
}

void MDecoratorWindow::setMDecoratorWindowProperty()
{

    long on = 1;

    XChangeProperty(QX11Info::display(), winId(),
                    XInternAtom(QX11Info::display(), "_MEEGOTOUCH_DECORATOR_WINDOW", False),
                    XA_CARDINAL,
                    32, PropModeReplace,
                    (unsigned char *) &on, 1);
}

#include "mdecoratorwindow.moc"