1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the plugins of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qdirectfbinput.h"
43 #include "qdirectfbconvenience.h"
47 #include <QtGui/qwindowsysteminterface.h>
48 #include <QMouseEvent>
55 QDirectFbInput::QDirectFbInput(IDirectFB *dfb, IDirectFBDisplayLayer *dfbLayer)
57 , m_dfbDisplayLayer(dfbLayer)
60 DFBResult ok = m_dfbInterface->CreateEventBuffer(m_dfbInterface, m_eventBuffer.outPtr());
62 DirectFBError("Failed to initialise eventbuffer", ok);
65 void QDirectFbInput::run()
67 while (!m_shouldStop) {
68 if (m_eventBuffer->WaitForEvent(m_eventBuffer.data()) == DFB_OK)
73 void QDirectFbInput::stopInputEventLoop()
76 m_eventBuffer->WakeUp(m_eventBuffer.data());
79 void QDirectFbInput::addWindow(IDirectFBWindow *window, QWindow *platformWindow)
84 res = window->GetID(window, &id);
86 DirectFBError("QDirectFbInput::addWindow", res);
90 m_tlwMap.insert(id, platformWindow);
91 window->AttachEventBuffer(window, m_eventBuffer.data());
94 void QDirectFbInput::removeWindow(IDirectFBWindow *window)
99 res = window->GetID(window, &id);
101 DirectFBError("QDirectFbInput::removeWindow", res);
105 window->DetachEventBuffer(window, m_eventBuffer.data());
109 void QDirectFbInput::handleEvents()
111 DFBResult hasEvent = m_eventBuffer->HasEvent(m_eventBuffer.data());
112 while(hasEvent == DFB_OK){
114 DFBResult ok = m_eventBuffer->GetEvent(m_eventBuffer.data(), &event);
116 DirectFBError("Failed to get event",ok);
117 if (event.clazz == DFEC_WINDOW) {
118 switch (event.window.type) {
119 case DWET_BUTTONDOWN:
122 handleMouseEvents(event);
125 handleWheelEvent(event);
129 handleKeyEvents(event);
133 handleEnterLeaveEvents(event);
136 handleGotFocusEvent(event);
139 handleCloseEvent(event);
141 case DWET_POSITION_SIZE:
142 handleGeometryEvent(event);
150 hasEvent = m_eventBuffer->HasEvent(m_eventBuffer.data());
154 void QDirectFbInput::handleMouseEvents(const DFBEvent &event)
156 QPoint p(event.window.x, event.window.y);
157 QPoint globalPos = globalPoint(event);
158 Qt::MouseButtons buttons = QDirectFbConvenience::mouseButtons(event.window.buttons);
160 QDirectFBPointer<IDirectFBDisplayLayer> layer(QDirectFbConvenience::dfbDisplayLayer());
161 QDirectFBPointer<IDirectFBWindow> window;
162 layer->GetWindow(layer.data(), event.window.window_id, window.outPtr());
164 long timestamp = (event.window.timestamp.tv_sec*1000) + (event.window.timestamp.tv_usec/1000);
166 QWindow *tlw = m_tlwMap.value(event.window.window_id);
167 QWindowSystemInterface::handleMouseEvent(tlw, timestamp, p, globalPos, buttons);
170 void QDirectFbInput::handleWheelEvent(const DFBEvent &event)
172 QPoint p(event.window.cx, event.window.cy);
173 QPoint globalPos = globalPoint(event);
174 long timestamp = (event.window.timestamp.tv_sec*1000) + (event.window.timestamp.tv_usec/1000);
175 QWindow *tlw = m_tlwMap.value(event.window.window_id);
176 QWindowSystemInterface::handleWheelEvent(tlw, timestamp, p, globalPos,
177 event.window.step*120,
181 void QDirectFbInput::handleKeyEvents(const DFBEvent &event)
183 QEvent::Type type = QDirectFbConvenience::eventType(event.window.type);
184 Qt::Key key = QDirectFbConvenience::keyMap()->value(event.window.key_symbol);
185 Qt::KeyboardModifiers modifiers = QDirectFbConvenience::keyboardModifiers(event.window.modifiers);
187 long timestamp = (event.window.timestamp.tv_sec*1000) + (event.window.timestamp.tv_usec/1000);
190 if (DFB_KEY_TYPE(event.window.key_symbol) == DIKT_UNICODE)
191 character = QChar(event.window.key_symbol);
192 QWindow *tlw = m_tlwMap.value(event.window.window_id);
193 QWindowSystemInterface::handleKeyEvent(tlw, timestamp, type, key, modifiers, character);
196 void QDirectFbInput::handleEnterLeaveEvents(const DFBEvent &event)
198 QWindow *tlw = m_tlwMap.value(event.window.window_id);
199 switch (event.window.type) {
201 QWindowSystemInterface::handleEnterEvent(tlw);
204 QWindowSystemInterface::handleLeaveEvent(tlw);
211 void QDirectFbInput::handleGotFocusEvent(const DFBEvent &event)
213 QWindow *tlw = m_tlwMap.value(event.window.window_id);
214 QWindowSystemInterface::handleWindowActivated(tlw);
217 void QDirectFbInput::handleCloseEvent(const DFBEvent &event)
219 QWindow *tlw = m_tlwMap.value(event.window.window_id);
220 QWindowSystemInterface::handleCloseEvent(tlw);
223 void QDirectFbInput::handleGeometryEvent(const DFBEvent &event)
225 QWindow *tlw = m_tlwMap.value(event.window.window_id);
226 QRect rect(event.window.x, event.window.y, event.window.w, event.window.h);
227 QWindowSystemInterface::handleGeometryChange(tlw, rect);
230 inline QPoint QDirectFbInput::globalPoint(const DFBEvent &event) const
232 QDirectFBPointer<IDirectFBWindow> window;
233 m_dfbDisplayLayer->GetWindow(m_dfbDisplayLayer, event.window.window_id, window.outPtr());
235 window->GetPosition(window.data(), &x, &y);
236 return QPoint(event.window.cx +x, event.window.cy + y);