~gerboland/qtubuntu/enable-debug-mode

« back to all changes in this revision

Viewing changes to src/ubuntumirclient/cursor.cpp

  • Committer: Gerry Boland
  • Date: 2016-04-12 13:10:43 UTC
  • mfrom: (280.2.34 enable-debug-mode)
  • Revision ID: gerry.boland@canonical.com-20160412131043-t14zmcbheuxclmhr
Merge lp:~dandrader/qtubuntu/enable-debug-mode, he kindly fixed merging this with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2015 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it under
 
5
 * the terms of the GNU Lesser General Public License version 3, as published by
 
6
 * the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
9
 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
 
10
 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
#include "cursor.h"
 
18
 
 
19
#include "logging.h"
 
20
#include "window.h"
 
21
 
 
22
#include <mir_toolkit/mir_client_library.h>
 
23
 
 
24
UbuntuCursor::UbuntuCursor(MirConnection *connection)
 
25
    : mConnection(connection)
 
26
{
 
27
    mShapeToCursorName[Qt::ArrowCursor] = "left_ptr";
 
28
    mShapeToCursorName[Qt::UpArrowCursor] = "up_arrow";
 
29
    mShapeToCursorName[Qt::CrossCursor] = "cross";
 
30
    mShapeToCursorName[Qt::WaitCursor] = "watch";
 
31
    mShapeToCursorName[Qt::IBeamCursor] = "xterm";
 
32
    mShapeToCursorName[Qt::SizeVerCursor] = "size_ver";
 
33
    mShapeToCursorName[Qt::SizeHorCursor] = "size_hor";
 
34
    mShapeToCursorName[Qt::SizeBDiagCursor] = "size_bdiag";
 
35
    mShapeToCursorName[Qt::SizeFDiagCursor] = "size_fdiag";
 
36
    mShapeToCursorName[Qt::SizeAllCursor] = "size_all";
 
37
    mShapeToCursorName[Qt::BlankCursor] = "blank";
 
38
    mShapeToCursorName[Qt::SplitVCursor] = "split_v";
 
39
    mShapeToCursorName[Qt::SplitHCursor] = "split_h";
 
40
    mShapeToCursorName[Qt::PointingHandCursor] = "hand";
 
41
    mShapeToCursorName[Qt::ForbiddenCursor] = "forbidden";
 
42
    mShapeToCursorName[Qt::WhatsThisCursor] = "whats_this";
 
43
    mShapeToCursorName[Qt::BusyCursor] = "left_ptr_watch";
 
44
    mShapeToCursorName[Qt::OpenHandCursor] = "openhand";
 
45
    mShapeToCursorName[Qt::ClosedHandCursor] = "closedhand";
 
46
    mShapeToCursorName[Qt::DragCopyCursor] = "dnd-copy";
 
47
    mShapeToCursorName[Qt::DragMoveCursor] = "dnd-move";
 
48
    mShapeToCursorName[Qt::DragLinkCursor] = "dnd-link";
 
49
}
 
50
 
 
51
namespace {
 
52
const char *qtCursorShapeToStr(Qt::CursorShape shape)
 
53
{
 
54
    switch(shape) {
 
55
    case Qt::ArrowCursor:
 
56
        return "Arrow";
 
57
    case Qt::UpArrowCursor:
 
58
        return "UpArrow";
 
59
    case Qt::CrossCursor:
 
60
        return "Cross";
 
61
    case Qt::WaitCursor:
 
62
        return "Wait";
 
63
    case Qt::IBeamCursor:
 
64
        return "IBeam";
 
65
    case Qt::SizeVerCursor:
 
66
        return "SizeVer";
 
67
    case Qt::SizeHorCursor:
 
68
        return "SizeHor";
 
69
    case Qt::SizeBDiagCursor:
 
70
        return "SizeBDiag";
 
71
    case Qt::SizeFDiagCursor:
 
72
        return "SizeFDiag";
 
73
    case Qt::SizeAllCursor:
 
74
        return "SizeAll";
 
75
    case Qt::BlankCursor:
 
76
        return "Blank";
 
77
    case Qt::SplitVCursor:
 
78
        return "SplitV";
 
79
    case Qt::SplitHCursor:
 
80
        return "SplitH";
 
81
    case Qt::PointingHandCursor:
 
82
        return "PointingHand";
 
83
    case Qt::ForbiddenCursor:
 
84
        return "Forbidden";
 
85
    case Qt::WhatsThisCursor:
 
86
        return "WhatsThis";
 
87
    case Qt::BusyCursor:
 
88
        return "Busy";
 
89
    case Qt::OpenHandCursor:
 
90
        return "OpenHand";
 
91
    case Qt::ClosedHandCursor:
 
92
        return "ClosedHand";
 
93
    case Qt::DragCopyCursor:
 
94
        return "DragCopy";
 
95
    case Qt::DragMoveCursor:
 
96
        return "DragMove";
 
97
    case Qt::DragLinkCursor:
 
98
        return "DragLink";
 
99
    case Qt::BitmapCursor:
 
100
        return "Bitmap";
 
101
    default:
 
102
        return "???";
 
103
    }
 
104
}
 
105
} // anonymous namespace
 
106
 
 
107
void UbuntuCursor::changeCursor(QCursor *windowCursor, QWindow *window)
 
108
{
 
109
    if (!window) {
 
110
        return;
 
111
    }
 
112
 
 
113
    MirSurface *surface = static_cast<UbuntuWindow*>(window->handle())->mirSurface();
 
114
 
 
115
    if (!surface) {
 
116
        return;
 
117
    }
 
118
 
 
119
 
 
120
    if (windowCursor) {
 
121
        qCDebug(ubuntumirclient, "changeCursor shape=%s, window=%p", qtCursorShapeToStr(windowCursor->shape()), window);
 
122
        if (!windowCursor->pixmap().isNull()) {
 
123
            configureMirCursorWithPixmapQCursor(surface, *windowCursor);
 
124
        } else if (windowCursor->shape() == Qt::BitmapCursor) {
 
125
            // TODO: Implement bitmap cursor support
 
126
            applyDefaultCursorConfiguration(surface);
 
127
        } else {
 
128
            const auto &cursorName = mShapeToCursorName.value(windowCursor->shape(), QByteArray("left_ptr"));
 
129
            auto cursorConfiguration = mir_cursor_configuration_from_name(cursorName.data());
 
130
            mir_surface_configure_cursor(surface, cursorConfiguration);
 
131
            mir_cursor_configuration_destroy(cursorConfiguration);
 
132
        }
 
133
    } else {
 
134
        applyDefaultCursorConfiguration(surface);
 
135
    }
 
136
 
 
137
}
 
138
 
 
139
void UbuntuCursor::configureMirCursorWithPixmapQCursor(MirSurface *surface, QCursor &cursor)
 
140
{
 
141
    QImage image = cursor.pixmap().toImage();
 
142
 
 
143
    if (image.format() != QImage::Format_ARGB32) {
 
144
        image.convertToFormat(QImage::Format_ARGB32);
 
145
    }
 
146
 
 
147
    MirBufferStream *bufferStream = mir_connection_create_buffer_stream_sync(mConnection,
 
148
            image.width(), image.height(), mir_pixel_format_argb_8888, mir_buffer_usage_software);
 
149
 
 
150
    {
 
151
        MirGraphicsRegion region;
 
152
        mir_buffer_stream_get_graphics_region(bufferStream, &region);
 
153
 
 
154
        char *regionLine = region.vaddr;
 
155
        Q_ASSERT(image.bytesPerLine() <= region.stride);
 
156
        for (int i = 0; i < image.height(); ++i) {
 
157
            memcpy(regionLine, image.scanLine(i), image.bytesPerLine());
 
158
            regionLine += region.stride;
 
159
        }
 
160
    }
 
161
 
 
162
    mir_buffer_stream_swap_buffers_sync(bufferStream);
 
163
 
 
164
    {
 
165
        auto configuration = mir_cursor_configuration_from_buffer_stream(bufferStream, cursor.hotSpot().x(), cursor.hotSpot().y());
 
166
        mir_surface_configure_cursor(surface, configuration);
 
167
        mir_cursor_configuration_destroy(configuration);
 
168
    }
 
169
 
 
170
    mir_buffer_stream_release_sync(bufferStream);
 
171
}
 
172
 
 
173
void UbuntuCursor::applyDefaultCursorConfiguration(MirSurface *surface)
 
174
{
 
175
    auto cursorConfiguration = mir_cursor_configuration_from_name("left_ptr");
 
176
    mir_surface_configure_cursor(surface, cursorConfiguration);
 
177
    mir_cursor_configuration_destroy(cursorConfiguration);
 
178
}