summaryrefslogtreecommitdiffstats
path: root/src/platformsupport/fbconvenience/qfbcursor.cpp
blob: c686cbf7319d2511139e25d066ec3cafa18ba721 (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qfbcursor_p.h"
#include "qfbscreen_p.h"
#include <QtGui/QPainter>
#include <QtGui/private/qguiapplication_p.h>

QT_BEGIN_NAMESPACE

bool QFbCursorDeviceListener::hasMouse() const
{
    return QGuiApplicationPrivate::inputDeviceManager()->deviceCount(QInputDeviceManager::DeviceTypePointer) > 0;
}

void QFbCursorDeviceListener::onDeviceListChanged(QInputDeviceManager::DeviceType type)
{
    if (type == QInputDeviceManager::DeviceTypePointer)
        m_cursor->updateMouseStatus();
}

QFbCursor::QFbCursor(QFbScreen *screen)
    : mVisible(true),
      mScreen(screen),
      mDirty(false),
      mOnScreen(false),
      mCursorImage(nullptr),
      mDeviceListener(nullptr)
{
    const char *envVar = "QT_QPA_FB_HIDECURSOR";
    if (qEnvironmentVariableIsSet(envVar))
        mVisible = qEnvironmentVariableIntValue(envVar) == 0;
    if (!mVisible)
        return;

    mCursorImage = new QPlatformCursorImage(0, 0, 0, 0, 0, 0);
    setCursor(Qt::ArrowCursor);

    mDeviceListener = new QFbCursorDeviceListener(this);
    connect(QGuiApplicationPrivate::inputDeviceManager(), &QInputDeviceManager::deviceListChanged,
            mDeviceListener, &QFbCursorDeviceListener::onDeviceListChanged);
    updateMouseStatus();
}

QFbCursor::~QFbCursor()
{
    delete mDeviceListener;
}

QRect QFbCursor::getCurrentRect() const
{
    QRect rect = mCursorImage->image()->rect().translated(-mCursorImage->hotspot().x(),
                                                          -mCursorImage->hotspot().y());
    rect.translate(m_pos);
    QPoint mScreenOffset = mScreen->geometry().topLeft();
    rect.translate(-mScreenOffset);  // global to local translation
    return rect;
}

QPoint QFbCursor::pos() const
{
    return m_pos;
}

void QFbCursor::setPos(const QPoint &pos)
{
    QGuiApplicationPrivate::inputDeviceManager()->setCursorPos(pos);
    m_pos = pos;
    if (!mVisible)
        return;
    mCurrentRect = getCurrentRect();
    if (mOnScreen || mScreen->geometry().intersects(mCurrentRect.translated(mScreen->geometry().topLeft())))
        setDirty();
}

void QFbCursor::pointerEvent(const QMouseEvent &e)
{
    if (e.type() != QEvent::MouseMove)
        return;
    m_pos = e.globalPosition().toPoint();
    if (!mVisible)
        return;
    mCurrentRect = getCurrentRect();
    if (mOnScreen || mScreen->geometry().intersects(mCurrentRect.translated(mScreen->geometry().topLeft())))
        setDirty();
}

QRect QFbCursor::drawCursor(QPainter & painter)
{
    if (!mVisible)
        return QRect();

    mDirty = false;
    if (mCurrentRect.isNull())
        return QRect();

    // We need this because the cursor might be mDirty due to moving off mScreen
    QPoint mScreenOffset = mScreen->geometry().topLeft();
    // global to local translation
    if (!mCurrentRect.translated(mScreenOffset).intersects(mScreen->geometry()))
        return QRect();

    mPrevRect = mCurrentRect;
    painter.drawImage(mPrevRect, *mCursorImage->image());
    mOnScreen = true;
    return mPrevRect;
}

QRect QFbCursor::dirtyRect()
{
    if (mOnScreen) {
        mOnScreen = false;
        return mPrevRect;
    }
    return QRect();
}

void QFbCursor::setCursor(Qt::CursorShape shape)
{
    if (mCursorImage)
        mCursorImage->set(shape);
}

void QFbCursor::setCursor(const QImage &image, int hotx, int hoty)
{
    if (mCursorImage)
        mCursorImage->set(image, hotx, hoty);
}

void QFbCursor::setCursor(const uchar *data, const uchar *mask, int width, int height, int hotX, int hotY)
{
    if (mCursorImage)
        mCursorImage->set(data, mask, width, height, hotX, hotY);
}

#ifndef QT_NO_CURSOR
void QFbCursor::changeCursor(QCursor * widgetCursor, QWindow *window)
{
    Q_UNUSED(window);
    if (!mVisible)
        return;
    const Qt::CursorShape shape = widgetCursor ? widgetCursor->shape() : Qt::ArrowCursor;

    if (shape == Qt::BitmapCursor) {
        // application supplied cursor
        QPoint spot = widgetCursor->hotSpot();
        setCursor(widgetCursor->pixmap().toImage(), spot.x(), spot.y());
    } else {
        // system cursor
        setCursor(shape);
    }
    mCurrentRect = getCurrentRect();
    QPoint mScreenOffset = mScreen->geometry().topLeft(); // global to local translation
    if (mOnScreen || mScreen->geometry().intersects(mCurrentRect.translated(mScreenOffset)))
        setDirty();
}
#endif

void QFbCursor::setDirty()
{
    if (!mVisible)
        return;

    if (!mDirty) {
        mDirty = true;
        mScreen->scheduleUpdate();
    }
}

void QFbCursor::updateMouseStatus()
{
    mVisible = mDeviceListener ? mDeviceListener->hasMouse() : false;
    mScreen->setDirty(mVisible ? getCurrentRect() : lastPainted());
}

QT_END_NAMESPACE

#include "moc_qfbcursor_p.cpp"