~gerboland/unity/8-refactor-wm-and-test

« back to all changes in this revision

Viewing changes to MouseTouchAdaptor.cpp

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Daniel d'Andrada <daniel.dandrada@canonical.com>
 
17
 */
 
18
 
 
19
#include "MouseTouchAdaptor.h"
 
20
 
 
21
#include <qpa/qwindowsysteminterface.h>
 
22
 
 
23
#include <QtGui/QMouseEvent>
 
24
#include <QtTest/QTest>
 
25
 
 
26
using QTest::QTouchEventSequence;
 
27
 
 
28
namespace {
 
29
Qt::MouseButton translateMouseButton(xcb_button_t detail)
 
30
{
 
31
    switch (detail) {
 
32
        case 1: return Qt::LeftButton;
 
33
        case 2: return Qt::MidButton;
 
34
        case 3: return Qt::RightButton;
 
35
        // Button values 4-7 are Wheel events
 
36
        default: return Qt::NoButton;
 
37
    }
 
38
}
 
39
} // end of anonymous namespace
 
40
 
 
41
MouseTouchAdaptor::MouseTouchAdaptor()
 
42
    : m_targetWindow(0)
 
43
    , m_leftButtonIsPressed(false)
 
44
{
 
45
    m_touchDevice = new QTouchDevice;
 
46
    m_touchDevice->setType(QTouchDevice::TouchScreen);
 
47
    QWindowSystemInterface::registerTouchDevice(m_touchDevice);
 
48
}
 
49
 
 
50
void MouseTouchAdaptor::setTargetWindow(QWindow *window)
 
51
{
 
52
    m_targetWindow = window;
 
53
}
 
54
 
 
55
bool MouseTouchAdaptor::nativeEventFilter(const QByteArray & eventType,
 
56
                                          void * message, long * /*result*/)
 
57
{
 
58
    Q_ASSERT(m_targetWindow);
 
59
 
 
60
    if (eventType != "xcb_generic_event_t") {
 
61
        // wrong backend.
 
62
        qWarning("MouseTouchAdaptor: XCB backend not in use. Adaptor inoperative!");
 
63
        return false;
 
64
    }
 
65
 
 
66
    xcb_generic_event_t *xcbEvent = static_cast<xcb_generic_event_t *>(message);
 
67
 
 
68
    switch (xcbEvent->response_type & ~0x80) {
 
69
        case XCB_BUTTON_PRESS:
 
70
            return handleButtonPress(reinterpret_cast<xcb_button_press_event_t *>(xcbEvent));
 
71
            break;
 
72
        case XCB_BUTTON_RELEASE:
 
73
            return handleButtonRelease(reinterpret_cast<xcb_button_release_event_t *>(xcbEvent));
 
74
            break;
 
75
        case XCB_MOTION_NOTIFY:
 
76
            return handleMotionNotify(reinterpret_cast<xcb_motion_notify_event_t *>(xcbEvent));
 
77
            break;
 
78
        default:
 
79
            return false;
 
80
            break;
 
81
    };
 
82
}
 
83
 
 
84
bool MouseTouchAdaptor::handleButtonPress(xcb_button_press_event_t *pressEvent)
 
85
{
 
86
    Qt::MouseButton button = translateMouseButton(pressEvent->detail);
 
87
 
 
88
    // Just eat the event if it wasn't a left mouse press
 
89
    if (button != Qt::LeftButton)
 
90
        return true;
 
91
 
 
92
    QPoint windowPos(pressEvent->event_x, pressEvent->event_y);
 
93
 
 
94
    QTouchEventSequence touchEvent = QTest::touchEvent(m_targetWindow, m_touchDevice,
 
95
                                                       false /* autoCommit */);
 
96
    touchEvent.press(0 /* touchId */, windowPos);
 
97
    touchEvent.commit(false /* processEvents */);
 
98
 
 
99
    m_leftButtonIsPressed = true;
 
100
    return true;
 
101
}
 
102
 
 
103
bool MouseTouchAdaptor::handleButtonRelease(xcb_button_release_event_t *releaseEvent)
 
104
{
 
105
    Qt::MouseButton button = translateMouseButton(releaseEvent->detail);
 
106
 
 
107
    // Just eat the event if it wasn't a left mouse release
 
108
    if (button != Qt::LeftButton)
 
109
        return true;
 
110
 
 
111
    QPoint windowPos(releaseEvent->event_x, releaseEvent->event_y);
 
112
 
 
113
    QTouchEventSequence touchEvent = QTest::touchEvent(m_targetWindow, m_touchDevice,
 
114
                                                       false /* autoCommit */);
 
115
    touchEvent.release(0 /* touchId */, windowPos);
 
116
    touchEvent.commit(false /* processEvents */);
 
117
 
 
118
    m_leftButtonIsPressed = false;
 
119
    return true;
 
120
}
 
121
 
 
122
bool MouseTouchAdaptor::handleMotionNotify(xcb_motion_notify_event_t *event)
 
123
{
 
124
    if (!m_leftButtonIsPressed) {
 
125
        return true;
 
126
    }
 
127
 
 
128
    QPoint windowPos(event->event_x, event->event_y);
 
129
 
 
130
    QTouchEventSequence touchEvent = QTest::touchEvent(m_targetWindow, m_touchDevice,
 
131
                                                       false /* autoCommit */);
 
132
    touchEvent.move(0 /* touchId */, windowPos);
 
133
    touchEvent.commit(false /* processEvents */);
 
134
 
 
135
    return true;
 
136
}