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

« back to all changes in this revision

Viewing changes to kcontrol/keyboard/x11_helper.h

  • 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 (C) 2010 Andriy Rysin (rysin@kde.org)
 
3
 *
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
17
 */
 
18
 
 
19
 
 
20
#ifndef X11_HELPER_H_
 
21
#define X11_HELPER_H_
 
22
 
 
23
#include <QtGui/QKeySequence>
 
24
#include <QtCore/QString>
 
25
#include <QtCore/QStringList>
 
26
#include <QtGui/QWidget>
 
27
 
 
28
 
 
29
class XEventNotifier : public QWidget {
 
30
        Q_OBJECT
 
31
 
 
32
Q_SIGNALS:
 
33
        void layoutChanged();
 
34
        void layoutMapChanged();
 
35
 
 
36
public:
 
37
        XEventNotifier(QWidget* parent=NULL);
 
38
        virtual ~XEventNotifier() {}
 
39
 
 
40
        virtual void start();
 
41
        virtual void stop();
 
42
 
 
43
protected:
 
44
    bool x11Event(XEvent * e);
 
45
    virtual bool processOtherEvents(XEvent* e);
 
46
    virtual bool processXkbEvents(XEvent* e);
 
47
 
 
48
private:
 
49
        int registerForXkbEvents(Display* display);
 
50
        bool isXkbEvent(XEvent* event);
 
51
        bool isGroupSwitchEvent(XEvent* event);
 
52
        bool isLayoutSwitchEvent(XEvent* event);
 
53
 
 
54
        int xkbOpcode;
 
55
};
 
56
 
 
57
struct XkbConfig {
 
58
        QString keyboardModel;
 
59
        QStringList layouts;
 
60
        QStringList variants;
 
61
        QStringList options;
 
62
 
 
63
        bool isValid() { return ! layouts.empty(); }
 
64
};
 
65
 
 
66
 
 
67
struct LayoutUnit {
 
68
        static const int MAX_LABEL_LENGTH;
 
69
 
 
70
        //TODO: move these to private?
 
71
        QString layout;
 
72
        QString variant;
 
73
 
 
74
        LayoutUnit() {}
 
75
        explicit LayoutUnit(const QString& fullLayoutName);
 
76
        LayoutUnit(const QString& layout_, const QString& variant_) {
 
77
                layout = layout_;
 
78
                variant = variant_;
 
79
        }
 
80
        /*explicit*/ LayoutUnit(const LayoutUnit& layoutUnit) {
 
81
                layout = layoutUnit.layout;
 
82
                variant = layoutUnit.variant;
 
83
                displayName = layoutUnit.displayName;
 
84
                shortcut = layoutUnit.shortcut;
 
85
        }
 
86
 
 
87
        QString getRawDisplayName() const { return displayName; }
 
88
        QString getDisplayName() const { return !displayName.isEmpty() ? displayName :  layout; }
 
89
        void setDisplayName(const QString& name) { displayName = name; }
 
90
 
 
91
        void setShortcut(const QKeySequence& shortcut) { this->shortcut = shortcut; }
 
92
        QKeySequence getShortcut() const { return shortcut; }
 
93
 
 
94
        bool isEmpty() const { return layout.isEmpty(); }
 
95
        bool isValid() const { return ! isEmpty(); }
 
96
        bool operator==(const LayoutUnit& layoutItem) const {
 
97
                return layout==layoutItem.layout && variant==layoutItem.variant;
 
98
        }
 
99
        bool operator!=(const LayoutUnit& layoutItem) const {
 
100
                return ! (*this == layoutItem);
 
101
        }
 
102
        QString toString() const;
 
103
 
 
104
private:
 
105
        QString displayName;
 
106
        QKeySequence shortcut;
 
107
};
 
108
 
 
109
struct LayoutSet {
 
110
        QList<LayoutUnit> layouts;
 
111
        LayoutUnit currentLayout;
 
112
 
 
113
        LayoutSet() {}
 
114
 
 
115
        LayoutSet(const LayoutSet& currentLayouts) {
 
116
                this->layouts = currentLayouts.layouts;
 
117
                this->currentLayout = currentLayouts.currentLayout;
 
118
        }
 
119
 
 
120
        bool isValid() const {
 
121
                return currentLayout.isValid() && layouts.contains(currentLayout);
 
122
        }
 
123
 
 
124
        bool operator == (const LayoutSet& currentLayouts) const {
 
125
                return this->layouts == currentLayouts.layouts
 
126
                                && this->currentLayout == currentLayouts.currentLayout;
 
127
        }
 
128
 
 
129
        LayoutSet& operator = (const LayoutSet& currentLayouts) {
 
130
                this->layouts = currentLayouts.layouts;
 
131
                this->currentLayout = currentLayouts.currentLayout;
 
132
                return *this;
 
133
        }
 
134
 
 
135
        QString toString() const {
 
136
                QString str(currentLayout.toString());
 
137
                str += ": ";
 
138
                foreach(const LayoutUnit& layoutUnit, layouts) {
 
139
                        str += layoutUnit.toString() + " ";
 
140
                }
 
141
                return str;
 
142
        }
 
143
 
 
144
        static QString toString(const QList<LayoutUnit>& layoutUnits) {
 
145
                QString str;
 
146
                foreach(const LayoutUnit& layoutUnit, layoutUnits) {
 
147
                        str += layoutUnit.toString() + ",";
 
148
                }
 
149
                return str;
 
150
        }
 
151
};
 
152
 
 
153
class X11Helper
 
154
{
 
155
public:
 
156
        static int MAX_GROUP_COUNT;
 
157
        static const char* LEFT_VARIANT_STR;
 
158
        static const char* RIGHT_VARIANT_STR;
 
159
 
 
160
        static bool xkbSupported(int* xkbOpcode);
 
161
 
 
162
        static void switchToNextLayout();
 
163
        static void scrollLayouts(int delta);
 
164
        static bool isDefaultLayout();
 
165
        static bool setDefaultLayout();
 
166
        static bool setLayout(const LayoutUnit& layout);
 
167
        static LayoutUnit getCurrentLayout();
 
168
        static LayoutSet getCurrentLayouts();
 
169
        static QList<LayoutUnit> getLayoutsList();
 
170
        static QStringList getLayoutsListAsString(const QList<LayoutUnit>& layoutsList);
 
171
 
 
172
        enum FetchType { ALL, LAYOUTS_ONLY, MODEL_ONLY };
 
173
        static bool getGroupNames(Display* dpy, XkbConfig* xkbConfig, FetchType fetchType);
 
174
 
 
175
private:
 
176
        static unsigned int getGroup();
 
177
        static bool setGroup(unsigned int group);
 
178
};
 
179
 
 
180
#endif /* X11_HELPER_H_ */