10763c4ab41d51b193106992cdd0bd09626ea7e7
[profile/ivi/qtbase.git] / src / plugins / platforms / directfb / qdirectfbwindow.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdirectfbwindow.h"
43 #include "qdirectfbbackingstore.h"
44 #include "qdirectfbinput.h"
45 #include "qdirectfbscreen.h"
46
47 #include <QtGui/qwindowsysteminterface.h>
48
49 #include <directfb.h>
50
51 QT_BEGIN_NAMESPACE
52
53 QDirectFbWindow::QDirectFbWindow(QWindow *tlw, QDirectFbInput *inputhandler)
54     : QPlatformWindow(tlw), m_inputHandler(inputhandler)
55 {
56 }
57
58 void QDirectFbWindow::createDirectFBWindow()
59 {
60     Q_ASSERT(!m_dfbWindow.data());
61
62     DFBDisplayLayerConfig layerConfig;
63     IDirectFBDisplayLayer *layer;
64
65     layer = toDfbScreen(window())->dfbLayer();
66     layer->GetConfiguration(layer, &layerConfig);
67
68     DFBWindowDescription description;
69     memset(&description,0,sizeof(DFBWindowDescription));
70     description.flags = DFBWindowDescriptionFlags(DWDESC_WIDTH|DWDESC_HEIGHT|DWDESC_POSX|DWDESC_POSY|DWDESC_SURFACE_CAPS
71                                                   |DWDESC_OPTIONS
72                                                   |DWDESC_CAPS);
73     description.width = qMax(1, window()->width());
74     description.height = qMax(1, window()->height());
75     description.posx = window()->x();
76     description.posy = window()->y();
77
78     if (layerConfig.surface_caps & DSCAPS_PREMULTIPLIED)
79         description.surface_caps = DSCAPS_PREMULTIPLIED;
80     description.pixelformat = layerConfig.pixelformat;
81
82     description.options = DFBWindowOptions(DWOP_ALPHACHANNEL);
83     description.caps = DFBWindowCapabilities(DWCAPS_DOUBLEBUFFER|DWCAPS_ALPHACHANNEL);
84
85     DFBResult result = layer->CreateWindow(layer, &description, m_dfbWindow.outPtr());
86     if (result != DFB_OK)
87         DirectFBError("QDirectFbWindow: failed to create window", result);
88
89     m_dfbWindow->SetOpacity(m_dfbWindow.data(), 0xff);
90     m_inputHandler->addWindow(m_dfbWindow.data(), window());
91 }
92
93 QDirectFbWindow::~QDirectFbWindow()
94 {
95     m_inputHandler->removeWindow(m_dfbWindow.data());
96     m_dfbWindow->Destroy(m_dfbWindow.data());
97 }
98
99 void QDirectFbWindow::setGeometry(const QRect &rect)
100 {
101 //    bool isMoveOnly = (rect.topLeft() != geometry().topLeft()) && (rect.size() == geometry().size());
102
103     QPlatformWindow::setGeometry(rect);
104     if (window()->isVisible()) {
105         m_dfbWindow->SetBounds(m_dfbWindow.data(), rect.x(),rect.y(),
106                                rect.width(), rect.height());
107 // ### TODO port, verify if this is needed
108 #if 0
109         //Hack. When moving since the WindowSurface of a window becomes invalid when moved
110         if (isMoveOnly) { //if resize then windowsurface is updated.
111             widget()->windowSurface()->resize(rect.size());
112             window()->update();
113         }
114 #endif
115     }
116 }
117
118 void QDirectFbWindow::setOpacity(qreal level)
119 {
120     const quint8 windowOpacity = quint8(level * 0xff);
121     m_dfbWindow->SetOpacity(m_dfbWindow.data(), windowOpacity);
122 }
123
124 void QDirectFbWindow::setVisible(bool visible)
125 {
126     if (visible) {
127         int x = geometry().x();
128         int y = geometry().y();
129         m_dfbWindow->MoveTo(m_dfbWindow.data(), x, y);
130     } else {
131         QDirectFBPointer<IDirectFBDisplayLayer> displayLayer;
132         QDirectFbConvenience::dfbInterface()->GetDisplayLayer(QDirectFbConvenience::dfbInterface(), DLID_PRIMARY, displayLayer.outPtr());
133
134         DFBDisplayLayerConfig config;
135         displayLayer->GetConfiguration(displayLayer.data(), &config);
136         m_dfbWindow->MoveTo(m_dfbWindow.data(), config. width + 1, config.height + 1);
137     }
138
139     if (window()->isTopLevel() && visible)
140         QWindowSystemInterface::handleExposeEvent(window(), window()->geometry());
141 }
142
143 Qt::WindowFlags QDirectFbWindow::setWindowFlags(Qt::WindowFlags flags)
144 {
145     switch (flags & Qt::WindowType_Mask) {
146     case Qt::ToolTip: {
147         DFBWindowOptions options;
148         m_dfbWindow->GetOptions(m_dfbWindow.data(), &options);
149         options = DFBWindowOptions(options | DWOP_GHOST);
150         m_dfbWindow->SetOptions(m_dfbWindow.data(), options);
151         break; }
152     default:
153         break;
154     }
155
156     m_dfbWindow->SetStackingClass(m_dfbWindow.data(), flags & Qt::WindowStaysOnTopHint ? DWSC_UPPER : DWSC_MIDDLE);
157     return flags;
158 }
159
160 void QDirectFbWindow::raise()
161 {
162     m_dfbWindow->RaiseToTop(m_dfbWindow.data());
163 }
164
165 void QDirectFbWindow::lower()
166 {
167     m_dfbWindow->LowerToBottom(m_dfbWindow.data());
168 }
169
170 WId QDirectFbWindow::winId() const
171 {
172     DFBWindowID id;
173     m_dfbWindow->GetID(m_dfbWindow.data(), &id);
174     return WId(id);
175 }
176
177 bool QDirectFbWindow::setKeyboardGrabEnabled(bool grab)
178 {
179     DFBResult res;
180
181     if (grab)
182         res = m_dfbWindow->GrabKeyboard(m_dfbWindow.data());
183     else
184         res = m_dfbWindow->UngrabKeyboard(m_dfbWindow.data());
185
186     return res == DFB_OK;
187 }
188
189 bool QDirectFbWindow::setMouseGrabEnabled(bool grab)
190 {
191     DFBResult res;
192
193     if (grab)
194         res = m_dfbWindow->GrabPointer(m_dfbWindow.data());
195     else
196         res = m_dfbWindow->UngrabPointer(m_dfbWindow.data());
197
198     return res == DFB_OK;
199 }
200
201 IDirectFBWindow *QDirectFbWindow::dfbWindow() const
202 {
203     return m_dfbWindow.data();
204 }
205
206 IDirectFBSurface *QDirectFbWindow::dfbSurface()
207 {
208     if (!m_dfbSurface) {
209         DFBResult res = m_dfbWindow->GetSurface(m_dfbWindow.data(), m_dfbSurface.outPtr());
210         if (res != DFB_OK)
211             DirectFBError(QDFB_PRETTY, res);
212     }
213
214     return m_dfbSurface.data();
215 }
216
217 QT_END_NAMESPACE