~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/mouse/cursornotificationhandler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright © 2007 Fredrik Höglund <fredrik@kde.org>
 
3
 *
 
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
 
7
 *
 
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
 
12
 *
 
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.
 
17
 */
 
18
 
 
19
#include "cursornotificationhandler.h"
 
20
 
 
21
#include <QX11Info>
 
22
 
 
23
#include <X11/extensions/Xfixes.h>
 
24
#include <fixx11h.h>
 
25
 
 
26
 
 
27
/*
 
28
 * This class is a QWidget because we need an X window to
 
29
 * be able to receive XFixes events. We don't actually map
 
30
 * the widget.
 
31
 */
 
32
 
 
33
CursorNotificationHandler::CursorNotificationHandler()
 
34
    : QWidget(), currentName(0)
 
35
{
 
36
    Display *dpy = QX11Info::display();
 
37
    int errorBase;
 
38
    haveXfixes = false;
 
39
 
 
40
    // Request cursor change notification events
 
41
    if (XFixesQueryExtension(dpy, &fixesEventBase, &errorBase))
 
42
    {
 
43
        int major, minor;
 
44
        XFixesQueryVersion(dpy, &major, &minor);
 
45
 
 
46
        if (major >= 2)
 
47
        {
 
48
            XFixesSelectCursorInput(dpy, winId(), XFixesDisplayCursorNotifyMask);
 
49
            haveXfixes = true;
 
50
        }
 
51
    }
 
52
}
 
53
 
 
54
 
 
55
CursorNotificationHandler::~CursorNotificationHandler()
 
56
{
 
57
}
 
58
 
 
59
 
 
60
QString CursorNotificationHandler::cursorName()
 
61
{
 
62
    if (!haveXfixes)
 
63
        return QString();
 
64
 
 
65
    if (!currentName)
 
66
    {
 
67
        // Xfixes doesn't have a request for getting the current cursor name,
 
68
        // but it's included in the XFixesCursorImage struct.
 
69
        XFixesCursorImage *image = XFixesGetCursorImage(QX11Info::display());
 
70
        currentName = image->atom;
 
71
        XFree(image);
 
72
    }
 
73
 
 
74
    return cursorName(currentName);
 
75
}
 
76
 
 
77
 
 
78
QString CursorNotificationHandler::cursorName(Atom cursor)
 
79
{
 
80
    QString name;
 
81
 
 
82
    // XGetAtomName() is a synchronous call, so we cache the name
 
83
    // in an atom<->string map the first time we see a name
 
84
    // to keep the X server round trips down.
 
85
    if (names.contains(cursor))
 
86
        name = names[cursor];
 
87
    else
 
88
    {
 
89
        char *data = XGetAtomName(QX11Info::display(), cursor);
 
90
        name = QString::fromUtf8(data);
 
91
        XFree(data);
 
92
 
 
93
        names.insert(cursor, name);
 
94
    }
 
95
 
 
96
    return name;
 
97
}
 
98
 
 
99
 
 
100
bool CursorNotificationHandler::x11Event(XEvent* event)
 
101
{
 
102
    if (event->type != fixesEventBase + XFixesCursorNotify)
 
103
        return false;
 
104
 
 
105
    XFixesCursorNotifyEvent *xfe = reinterpret_cast<XFixesCursorNotifyEvent*>(event);
 
106
    currentName = xfe->cursor_name;
 
107
 
 
108
    emit cursorNameChanged(cursorName(currentName));
 
109
 
 
110
    return false;
 
111
}
 
112
 
 
113
#include "cursornotificationhandler.moc"
 
114