katie/src/gui/kernel/qcursor_x11.cpp

379 lines
11 KiB
C++
Raw Normal View History

/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Copyright (C) 2016-2019 Ivailo Monev
**
** This file is part of the QtGui module of the Katie Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qbitmap.h"
#include "qcursor.h"
#include <qcursor_p.h>
#include "qx11info_x11.h"
#include <qpixmap_x11_p.h>
#include <qt_x11_p.h>
QT_BEGIN_NAMESPACE
extern QPixmap qt_toX11Pixmap(const QPixmap &pixmap); // qpixmap_x11.cpp
/*****************************************************************************
Internal QCursorData class
*****************************************************************************/
QCursorData::QCursorData(Qt::CursorShape s)
: cshape(s), bm(0), bmm(0), hx(0), hy(0), hcurs(0), pm(0)
{
ref = 1;
}
QCursorData::~QCursorData()
{
Display *dpy = qt_x11Data ? qt_x11Data->display : (Display*)0;
// Add in checking for the display too as on HP-UX
// we seem to get a core dump as the cursor data is
// deleted again from main() on exit...
if (hcurs && dpy)
XFreeCursor(dpy, hcurs);
if (pm && dpy)
XFreePixmap(dpy, pm);
delete bm;
delete bmm;
}
#ifndef QT_NO_CURSOR
QCursor::QCursor(Qt::HANDLE cursor)
{
if (!QCursorData::initialized)
QCursorData::initialize();
d = new QCursorData(Qt::CustomCursor);
d->hcurs = cursor;
}
#endif
QCursorData *QCursorData::setBitmap(const QBitmap &bitmap, const QBitmap &mask, int hotX, int hotY)
{
if (!QCursorData::initialized)
QCursorData::initialize();
if (bitmap.depth() != 1 || mask.depth() != 1 || bitmap.size() != mask.size()) {
qWarning("QCursor: Cannot create bitmap cursor; invalid bitmap(s)");
QCursorData *c = qt_cursorTable[0];
c->ref.ref();
return c;
}
QCursorData *d = new QCursorData;
d->ref = 1;
d->bm = new QBitmap(qt_toX11Pixmap(bitmap));
d->bmm = new QBitmap(qt_toX11Pixmap(mask));
d->hcurs = 0;
d->cshape = Qt::BitmapCursor;
d->hx = hotX >= 0 ? hotX : bitmap.width() / 2;
d->hy = hotY >= 0 ? hotY : bitmap.height() / 2;
d->fg.red = 0x0000;
d->fg.green = 0x0000;
d->fg.blue = 0x0000;
d->bg.red = 0xffff;
d->bg.green = 0xffff;
d->bg.blue = 0xffff;
return d;
}
#ifndef QT_NO_CURSOR
Qt::HANDLE QCursor::handle() const
{
if (!QCursorData::initialized)
QCursorData::initialize();
if (!d->hcurs)
d->update();
return d->hcurs;
}
#endif
QPoint QCursor::pos()
{
Window root;
Window child;
int root_x, root_y, win_x, win_y;
uint buttons;
Display* dpy = qt_x11Data->display;
for (int i = 0; i < ScreenCount(dpy); ++i) {
if (XQueryPointer(dpy, QX11Info::appRootWindow(i), &root, &child, &root_x, &root_y,
&win_x, &win_y, &buttons))
return QPoint(root_x, root_y);
}
return QPoint();
}
/*! \internal
*/
#ifndef QT_NO_CURSOR
int QCursor::x11Screen()
{
Window root;
Window child;
int root_x, root_y, win_x, win_y;
uint buttons;
Display* dpy = qt_x11Data->display;
for (int i = 0; i < ScreenCount(dpy); ++i) {
if (XQueryPointer(dpy, QX11Info::appRootWindow(i), &root, &child, &root_x, &root_y,
&win_x, &win_y, &buttons))
return i;
}
return -1;
}
#endif
void QCursor::setPos(int x, int y)
{
QPoint current, target(x, y);
// this is copied from pos(), since we need the screen number for the correct
// root window in the XWarpPointer call
Window root;
Window child;
int root_x, root_y, win_x, win_y;
uint buttons;
Display* dpy = qt_x11Data->display;
int screen;
for (screen = 0; screen < ScreenCount(dpy); ++screen) {
if (XQueryPointer(dpy, QX11Info::appRootWindow(screen), &root, &child, &root_x, &root_y,
&win_x, &win_y, &buttons)) {
current = QPoint(root_x, root_y);
break;
}
}
if (screen >= ScreenCount(dpy))
return;
// Need to check, since some X servers generate null mouse move
// events, causing looping in applications which call setPos() on
// every mouse move event.
if (current == target)
return;
XWarpPointer(qt_x11Data->display, XNone, QX11Info::appRootWindow(screen), 0, 0, 0, 0, x, y);
}
/*!
\internal
Creates the cursor.
*/
void QCursorData::update()
{
if (!QCursorData::initialized)
QCursorData::initialize();
if (hcurs)
return;
Display *dpy = qt_x11Data->display;
// Non-standard X11 cursors are created from bitmaps
if (cshape == Qt::BitmapCursor) {
#ifndef QT_NO_XRENDER
if (!pixmap.isNull() && qt_x11Data->use_xrender) {
pixmap = qt_toX11Pixmap(pixmap);
hcurs = XRenderCreateCursor(qt_x11Data->display, pixmap.x11PictureHandle(), hx, hy);
} else
#endif
{
hcurs = XCreatePixmapCursor(dpy, bm->handle(), bmm->handle(), &fg, &bg, hx, hy);
}
return;
}
static const char *cursorNames[] = {
"left_ptr",
"up_arrow",
"cross",
"wait",
"ibeam",
"size_ver",
"size_hor",
"size_bdiag",
"size_fdiag",
"size_all",
"blank",
"split_v",
"split_h",
"pointing_hand",
"forbidden",
"whats_this",
"left_ptr_watch",
"openhand",
"closedhand",
"copy",
"move",
"link"
};
#ifndef QT_NO_XCURSOR
// special case for non-standard dnd-* cursors
switch (cshape) {
case Qt::DragCopyCursor: {
hcurs = XcursorLibraryLoadCursor(dpy, "dnd-copy");
break;
}
case Qt::DragMoveCursor: {
hcurs = XcursorLibraryLoadCursor(dpy, "dnd-move");
break;
}
case Qt::DragLinkCursor: {
hcurs = XcursorLibraryLoadCursor(dpy, "dnd-link");
break;
}
default: {
break;
}
}
if (!hcurs)
hcurs = XcursorLibraryLoadCursor(dpy, cursorNames[cshape]);
if (hcurs)
return;
#endif // QT_NO_XCURSOR
static const char cur_blank_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// Q cursor to X cursor
switch (cshape) {
case Qt::BlankCursor: {
XColor bg, fg;
bg.red = 255 << 8;
bg.green = 255 << 8;
bg.blue = 255 << 8;
fg.red = 0;
fg.green = 0;
fg.blue = 0;
pm = XCreateBitmapFromData(dpy, QX11Info::appRootWindow(), cur_blank_bits, 16, 16);
// reusing the pixmap as mask to create invisible cursor
hcurs = XCreatePixmapCursor(dpy, pm, pm, &fg, &bg, 8, 8);
return;
}
case Qt::ArrowCursor: {
hcurs = XCreateFontCursor(dpy, XC_left_ptr);
break;
}
case Qt::CrossCursor: {
hcurs = XCreateFontCursor(dpy, XC_crosshair);
break;
}
case Qt::BusyCursor:
case Qt::WaitCursor: {
hcurs = XCreateFontCursor(dpy, XC_watch);
break;
}
case Qt::IBeamCursor: {
hcurs = XCreateFontCursor(dpy, XC_xterm);
break;
}
case Qt::SizeAllCursor: {
hcurs = XCreateFontCursor(dpy, XC_fleur);
break;
}
case Qt::PointingHandCursor: {
hcurs = XCreateFontCursor(dpy, XC_hand2);
break;
}
case Qt::SizeBDiagCursor: {
hcurs = XCreateFontCursor(dpy, XC_top_right_corner);
break;
}
case Qt::SizeFDiagCursor: {
hcurs = XCreateFontCursor(dpy, XC_bottom_right_corner);
break;
}
case Qt::SizeVerCursor:
case Qt::SplitVCursor: {
hcurs = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
break;
}
case Qt::SizeHorCursor:
case Qt::SplitHCursor: {
hcurs = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
break;
}
case Qt::WhatsThisCursor: {
hcurs = XCreateFontCursor(dpy, XC_question_arrow);
break;
}
case Qt::ForbiddenCursor: {
hcurs = XCreateFontCursor(dpy, XC_circle);
break;
}
case Qt::DragCopyCursor: {
hcurs = XCreateFontCursor(dpy, XC_tcross);
break;
}
case Qt::UpArrowCursor:
case Qt::DragLinkCursor: {
hcurs = XCreateFontCursor(dpy, XC_center_ptr);
break;
}
case Qt::DragMoveCursor: {
hcurs = XCreateFontCursor(dpy, XC_top_left_arrow);
break;
}
default: {
qWarning("QCursor::update: Invalid cursor shape %d", cshape);
return;
}
}
#ifndef QT_NO_XFIXES
if (qt_x11Data->use_xfixes)
XFixesSetCursorName(dpy, hcurs, cursorNames[cshape]);
#endif /* ! QT_NO_XFIXES */
}
QT_END_NAMESPACE