1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 or (at your option) any later version
20 ** approved by the KDE Free Qt Foundation. The licenses are as published by
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
26 ** $QT_END_LICENSE$
27 **
28 ****************************************************************************/
29 
30 #include "qwasmcursor.h"
31 #include "qwasmscreen.h"
32 #include "qwasmstring.h"
33 
34 #include <QtCore/qdebug.h>
35 #include <QtGui/qwindow.h>
36 
37 #include <emscripten/emscripten.h>
38 #include <emscripten/bind.h>
39 
40 using namespace emscripten;
41 
changeCursor(QCursor * windowCursor,QWindow * window)42 void QWasmCursor::changeCursor(QCursor *windowCursor, QWindow *window)
43 {
44     if (!windowCursor || !window)
45         return;
46     QScreen *screen = window->screen();
47     if (!screen)
48         return;
49 
50     // Bitmap and custom cursors are not implemented (will fall back to "auto")
51     if (windowCursor->shape() == Qt::BitmapCursor || windowCursor->shape() >= Qt::CustomCursor)
52         qWarning() << "QWasmCursor: bitmap and custom cursors are not supported";
53 
54     QByteArray htmlCursorName = cursorShapeToHtml(windowCursor->shape());
55 
56     if (htmlCursorName.isEmpty())
57         htmlCursorName = "auto";
58 
59     // Set cursor on the canvas
60     val canvas = QWasmScreen::get(screen)->canvas();
61     val canvasStyle = canvas["style"];
62     canvasStyle.set("cursor", val(htmlCursorName.constData()));
63 }
64 
cursorShapeToHtml(Qt::CursorShape shape)65 QByteArray QWasmCursor::cursorShapeToHtml(Qt::CursorShape shape)
66 {
67     QByteArray cursorName;
68 
69     switch (shape) {
70     case Qt::ArrowCursor:
71         cursorName = "default";
72         break;
73     case Qt::UpArrowCursor:
74         cursorName = "n-resize";
75         break;
76     case Qt::CrossCursor:
77         cursorName = "crosshair";
78         break;
79     case Qt::WaitCursor:
80         cursorName = "wait";
81         break;
82     case Qt::IBeamCursor:
83         cursorName = "text";
84         break;
85     case Qt::SizeVerCursor:
86         cursorName = "ns-resize";
87         break;
88     case Qt::SizeHorCursor:
89         cursorName = "ew-resize";
90         break;
91     case Qt::SizeBDiagCursor:
92         cursorName = "nesw-resize";
93         break;
94     case Qt::SizeFDiagCursor:
95         cursorName = "nwse-resize";
96         break;
97     case Qt::SizeAllCursor:
98         cursorName = "move";
99         break;
100     case Qt::BlankCursor:
101         cursorName = "none";
102         break;
103     case Qt::SplitVCursor:
104         cursorName = "row-resize";
105         break;
106     case Qt::SplitHCursor:
107         cursorName = "col-resize";
108         break;
109     case Qt::PointingHandCursor:
110         cursorName = "pointer";
111         break;
112     case Qt::ForbiddenCursor:
113         cursorName = "not-allowed";
114         break;
115     case Qt::WhatsThisCursor:
116         cursorName = "help";
117         break;
118     case Qt::BusyCursor:
119         cursorName = "progress";
120         break;
121     case Qt::OpenHandCursor:
122         cursorName = "grab";
123         break;
124     case Qt::ClosedHandCursor:
125         cursorName = "grabbing";
126         break;
127     case Qt::DragCopyCursor:
128         cursorName = "copy";
129         break;
130     case Qt::DragMoveCursor:
131         cursorName = "default";
132         break;
133     case Qt::DragLinkCursor:
134         cursorName = "alias";
135         break;
136     default:
137         break;
138     }
139 
140     return cursorName;
141 }
142