not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / dataengines / mouse / cursornotificationhandler.cpp
blob631fc1e0fdcdc6342c76afef87bf32562e0c444a
1 /*
2 * Copyright © 2007 Fredrik Höglund <fredrik@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License version 2 as
6 * published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details
13 * You should have received a copy of the GNU Library General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "cursornotificationhandler.h"
21 #include <QX11Info>
23 #include <X11/extensions/Xfixes.h>
27 * This class is a QWidget because we need an X window to
28 * be able to receive XFixes events. We don't actually map
29 * the widget.
32 CursorNotificationHandler::CursorNotificationHandler()
33 : QWidget(), currentName(0)
35 Display *dpy = QX11Info::display();
36 int errorBase;
37 haveXfixes = false;
39 // Request cursor change notification events
40 if (XFixesQueryExtension(dpy, &fixesEventBase, &errorBase))
42 int major, minor;
43 XFixesQueryVersion(dpy, &major, &minor);
45 if (major >= 2)
47 XFixesSelectCursorInput(dpy, winId(), XFixesDisplayCursorNotifyMask);
48 haveXfixes = true;
54 CursorNotificationHandler::~CursorNotificationHandler()
59 QString CursorNotificationHandler::cursorName()
61 if (!haveXfixes)
62 return QString();
64 if (!currentName)
66 // Xfixes doesn't have a request for getting the current cursor name,
67 // but it's included in the XFixesCursorImage struct.
68 XFixesCursorImage *image = XFixesGetCursorImage(QX11Info::display());
69 currentName = image->atom;
70 XFree(image);
73 return cursorName(currentName);
77 QString CursorNotificationHandler::cursorName(Atom cursor)
79 QString name;
81 // XGetAtomName() is a synchronous call, so we cache the name
82 // in an atom<->string map the first time we see a name
83 // to keep the X server round trips down.
84 if (names.contains(cursor))
85 name = names[cursor];
86 else
88 char *data = XGetAtomName(QX11Info::display(), cursor);
89 name = QString::fromUtf8(data);
90 XFree(data);
92 names.insert(cursor, name);
95 return name;
99 bool CursorNotificationHandler::x11Event(XEvent* event)
101 if (event->type != fixesEventBase + XFixesCursorNotify)
102 return false;
104 XFixesCursorNotifyEvent *xfe = reinterpret_cast<XFixesCursorNotifyEvent*>(event);
105 currentName = xfe->cursor_name;
107 emit cursorNameChanged(cursorName(currentName));
109 return false;
112 #include "cursornotificationhandler.moc"