summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/haiku/qhaikurasterwindow.cpp
blob: 4392bf855381fac54f27f961fc7b773ea346608d (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
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
// Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Tobias Koenig <tobias.koenig@kdab.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qhaikurasterwindow.h"

#include "qhaikukeymapper.h"

#include <View.h>
#include <Window.h>

#include <qpa/qwindowsysteminterface.h>

QT_BEGIN_NAMESPACE

Q_DECLARE_METATYPE(QEvent::Type)
Q_DECLARE_METATYPE(Qt::MouseButtons)
Q_DECLARE_METATYPE(Qt::MouseEventSource)
Q_DECLARE_METATYPE(Qt::KeyboardModifiers)
Q_DECLARE_METATYPE(Qt::Orientation)

HaikuViewProxy::HaikuViewProxy(BWindow *window, QObject *parent)
    : QObject(parent)
    , BView(BRect(0, 0, window->Bounds().right, window->Bounds().bottom), 0, B_FOLLOW_ALL_SIDES, B_WILL_DRAW | B_FRAME_EVENTS)
{
}

void HaikuViewProxy::MessageReceived(BMessage *message)
{
    switch (message->what) {
    case B_MOUSE_WHEEL_CHANGED:
        {
             float deltaX = 0;
             if (message->FindFloat("be:wheel_delta_x", &deltaX) != B_OK)
                 deltaX = 0;

             float deltaY = 0;
             if (message->FindFloat("be:wheel_delta_y", &deltaY) != B_OK)
                 deltaY = 0;

             if (deltaX != 0 || deltaY != 0) {
                BPoint localPos;
                uint32 keyState = 0;
                GetMouse(&localPos, &keyState);
                const Qt::KeyboardModifiers keyboardModifiers = keyStateToModifiers(modifiers());

                const BPoint globalPos = ConvertToScreen(localPos);
                const QPoint globalPosition = QPoint(globalPos.x, globalPos.y);
                const QPoint localPosition = QPoint(localPos.x, localPos.y);

                if (deltaX != 0)
                    Q_EMIT wheelEvent(localPosition, globalPosition, (deltaX * -120), Qt::Horizontal, keyboardModifiers);

                if (deltaY != 0)
                    Q_EMIT wheelEvent(localPosition, globalPosition, (deltaY * -120), Qt::Vertical, keyboardModifiers);
             }
             break;
        }
    default:
        BView::MessageReceived(message);
        break;
    }

}

void HaikuViewProxy::Draw(BRect updateRect)
{
    BView::Draw(updateRect);

    Q_EMIT drawRequest(QRect(updateRect.left, updateRect.top, updateRect.Width(), updateRect.Height()));
}

void HaikuViewProxy::MouseDown(BPoint localPos)
{
    BPoint dummyPos;
    uint32 keyState = 0;
    GetMouse(&dummyPos, &keyState);

    const Qt::MouseButtons mouseButtons = keyStateToMouseButtons(keyState);
    const Qt::KeyboardModifiers keyboardModifiers = keyStateToModifiers(modifiers());
    const Qt::MouseEventSource source = Qt::MouseEventNotSynthesized;

    const BPoint globalPos = ConvertToScreen(localPos);
    const QPoint globalPosition = QPoint(globalPos.x, globalPos.y);
    const QPoint localPosition = QPoint(localPos.x, localPos.y);

    Q_EMIT mouseEvent(localPosition, globalPosition, mouseButtons, keyboardModifiers, source);
}

void HaikuViewProxy::MouseUp(BPoint localPos)
{
    BPoint dummyPos;
    uint32 keyState = 0;
    GetMouse(&dummyPos, &keyState);

    const Qt::MouseButtons mouseButtons = keyStateToMouseButtons(keyState);
    const Qt::KeyboardModifiers keyboardModifiers = keyStateToModifiers(modifiers());
    const Qt::MouseEventSource source = Qt::MouseEventNotSynthesized;

    const BPoint globalPos = ConvertToScreen(localPos);
    const QPoint globalPosition = QPoint(globalPos.x, globalPos.y);
    const QPoint localPosition = QPoint(localPos.x, localPos.y);

    Q_EMIT mouseEvent(localPosition, globalPosition, mouseButtons, keyboardModifiers, source);
}

void HaikuViewProxy::MouseMoved(BPoint pos, uint32 code, const BMessage *dragMessage)
{
    switch (code) {
    case B_INSIDE_VIEW:
        {
            BPoint dummyPos;
            uint32 keyState = 0;
            GetMouse(&dummyPos, &keyState);

            const Qt::MouseButtons mouseButtons = keyStateToMouseButtons(keyState);
            const Qt::KeyboardModifiers keyboardModifiers = keyStateToModifiers(modifiers());
            const Qt::MouseEventSource source = Qt::MouseEventNotSynthesized;

            const BPoint globalPos = ConvertToScreen(pos);
            const QPoint globalPosition = QPoint(globalPos.x, globalPos.y);
            const QPoint localPosition = QPoint(pos.x, pos.y);

            Q_EMIT mouseEvent(localPosition, globalPosition, mouseButtons, keyboardModifiers, source);
        }
        break;
    case B_ENTERED_VIEW:
        Q_EMIT enteredView();
        break;
    case B_EXITED_VIEW:
        Q_EMIT exitedView();
        break;
    }

    BView::MouseMoved(pos, code, dragMessage);
}

void HaikuViewProxy::KeyDown(const char*, int32)
{
    handleKeyEvent(QEvent::KeyPress, Window()->CurrentMessage());
}

void HaikuViewProxy::KeyUp(const char*, int32)
{
    handleKeyEvent(QEvent::KeyRelease, Window()->CurrentMessage());
}

Qt::MouseButtons HaikuViewProxy::keyStateToMouseButtons(uint32 keyState) const
{
    Qt::MouseButtons mouseButtons(Qt::NoButton);
    if (keyState & B_PRIMARY_MOUSE_BUTTON)
        mouseButtons |= Qt::LeftButton;
    if (keyState & B_SECONDARY_MOUSE_BUTTON)
        mouseButtons |= Qt::RightButton;
    if (keyState & B_TERTIARY_MOUSE_BUTTON)
        mouseButtons |= Qt::MiddleButton;

    return mouseButtons;
}

Qt::KeyboardModifiers HaikuViewProxy::keyStateToModifiers(uint32 keyState) const
{
    Qt::KeyboardModifiers modifiers(Qt::NoModifier);

    if (keyState & B_SHIFT_KEY)
        modifiers |= Qt::ShiftModifier;
    if (keyState & B_CONTROL_KEY)
        modifiers |= Qt::AltModifier;
    if (keyState & B_COMMAND_KEY)
        modifiers |= Qt::ControlModifier;

    return modifiers;
}

void HaikuViewProxy::handleKeyEvent(QEvent::Type type, BMessage *message)
{
    int32 key = 0;
    uint32 code = 0;
    const char *bytes = nullptr;
    QString text;

    if (message) {
        if (message->FindString("bytes", &bytes) == B_OK) {
            text = QString::fromLocal8Bit(bytes, strlen(bytes));
        }

        if (message->FindInt32("key", &key) == B_OK) {
            code = QHaikuKeyMapper::translateKeyCode(key, (modifiers() & B_NUM_LOCK));
        }
    }

    const Qt::KeyboardModifiers keyboardModifiers = keyStateToModifiers(modifiers());

    Q_EMIT keyEvent(type, code, keyboardModifiers, text);
}


QHaikuRasterWindow::QHaikuRasterWindow(QWindow *window)
    : QHaikuWindow(window)
{
    qRegisterMetaType<QEvent::Type>();
    qRegisterMetaType<Qt::MouseButtons>();
    qRegisterMetaType<Qt::MouseEventSource>();
    qRegisterMetaType<Qt::KeyboardModifiers>();
    qRegisterMetaType<Qt::Orientation>();

    HaikuViewProxy *haikuView = new HaikuViewProxy(m_window);
    connect(haikuView, SIGNAL(mouseEvent(QPoint,QPoint,Qt::MouseButtons,Qt::KeyboardModifiers,Qt::MouseEventSource)),
            this, SLOT(haikuMouseEvent(QPoint,QPoint,Qt::MouseButtons,Qt::KeyboardModifiers,Qt::MouseEventSource)));
    connect(haikuView, SIGNAL(wheelEvent(QPoint,QPoint,int,Qt::Orientation,Qt::KeyboardModifiers)),
            this, SLOT(haikuWheelEvent(QPoint,QPoint,int,Qt::Orientation,Qt::KeyboardModifiers)));
    connect(haikuView, SIGNAL(keyEvent(QEvent::Type,int,Qt::KeyboardModifiers,QString)),
            this, SLOT(haikuKeyEvent(QEvent::Type,int,Qt::KeyboardModifiers,QString)));
    connect(haikuView, SIGNAL(enteredView()), this, SLOT(haikuEnteredView()));
    connect(haikuView, SIGNAL(exitedView()), this, SLOT(haikuExitedView()));
    connect(haikuView, SIGNAL(drawRequest(QRect)), this, SLOT(haikuDrawRequest(QRect)));

    m_view = haikuView;

    m_window->LockLooper();
    m_window->AddChild(m_view);
    m_view->MakeFocus();
    m_window->UnlockLooper();
}

QHaikuRasterWindow::~QHaikuRasterWindow()
{
    m_window->LockLooper();
    m_view->RemoveSelf();
    m_window->UnlockLooper();

    delete m_view;
    m_view = nullptr;
}

BView* QHaikuRasterWindow::nativeViewHandle() const
{
    return m_view;
}

QT_END_NAMESPACE