Plasma5Support

cursornotificationhandler.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Fredrik Höglund <fredrik@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-only
5*/
6
7#include "cursornotificationhandler.h"
8
9#include <private/qtx11extras_p.h>
10
11#include <X11/extensions/Xfixes.h>
12
13/*
14 * This class is a QWidget because we need an X window to
15 * be able to receive XFixes events. We don't actually map
16 * the widget.
17 */
18
19CursorNotificationHandler::CursorNotificationHandler()
20 : QWidget()
21 , currentName(0)
22{
23 Display *dpy = QX11Info::display();
24 int errorBase;
25 haveXfixes = false;
26
27 // Request cursor change notification events
28 if (XFixesQueryExtension(dpy, &fixesEventBase, &errorBase)) {
29 int major, minor;
30 XFixesQueryVersion(dpy, &major, &minor);
31
32 if (major >= 2) {
33 XFixesSelectCursorInput(dpy, winId(), XFixesDisplayCursorNotifyMask);
34 haveXfixes = true;
35 }
36 }
37}
38
39CursorNotificationHandler::~CursorNotificationHandler()
40{
41}
42
43QString CursorNotificationHandler::cursorName()
44{
45 if (!haveXfixes)
46 return QString();
47
48 if (!currentName) {
49 // Xfixes doesn't have a request for getting the current cursor name,
50 // but it's included in the XFixesCursorImage struct.
51 XFixesCursorImage *image = XFixesGetCursorImage(QX11Info::display());
52 currentName = image->atom;
53 XFree(image);
54 }
55
56 return cursorName(currentName);
57}
58
59QString CursorNotificationHandler::cursorName(Atom cursor)
60{
62
63 // XGetAtomName() is a synchronous call, so we cache the name
64 // in an atom<->string map the first time we see a name
65 // to keep the X server round trips down.
66 if (names.contains(cursor))
67 name = names[cursor];
68 else {
69 char *data = XGetAtomName(QX11Info::display(), cursor);
70 name = QString::fromUtf8(data);
71 XFree(data);
72
73 names.insert(cursor, name);
74 }
75
76 return name;
77}
78
79bool CursorNotificationHandler::x11Event(XEvent *event)
80{
81 if (event->type != fixesEventBase + XFixesCursorNotify)
82 return false;
83
84 XFixesCursorNotifyEvent *xfe = reinterpret_cast<XFixesCursorNotifyEvent *>(event);
85 currentName = xfe->cursor_name;
86
87 Q_EMIT cursorNameChanged(cursorName(currentName));
88
89 return false;
90}
QString name(StandardAction id)
bool contains(const Key &key) const const
iterator insert(const Key &key, const T &value)
Q_EMITQ_EMIT
QString fromUtf8(QByteArrayView str)
virtual bool event(QEvent *event) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.