forked from sergei-mironov/xkb-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXKeyboard.cpp
305 lines (258 loc) · 6.64 KB
/
XKeyboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Implementation of a class to get keyboard layout information and change layouts
*
* Copyright (C) 2008 by Jay Bromley <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*/
#include "XKeyboard.h"
#include "X11Exception.h"
#include <algorithm>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <X11/XKBlib.h>
#include "X11/extensions/XKBrules.h"
#ifndef DFLT_XKB_LAYOUT
#define DFLT_XKB_LAYOUT "us"
#endif
#ifndef NO_KEYBOARD
#define NO_KEYBOARD "no keyboard"
#endif
#include <iostream>
#include <string>
namespace kb {
XKeyboard::XKeyboard()
: _display(0), _deviceId(XkbUseCoreKbd)
{
XkbIgnoreExtension(False);
char* displayName = strdup("");
int eventCode;
int errorReturn;
int major = XkbMajorVersion;
int minor = XkbMinorVersion;
int reasonReturn;
_display = XkbOpenDisplay(displayName, &eventCode, &errorReturn, &major,
&minor, &reasonReturn);
switch (reasonReturn) {
case XkbOD_BadLibraryVersion:
throw X11Exception("Bad XKB library version.");
break;
case XkbOD_ConnectionRefused:
throw X11Exception("Connection to X server refused.");
break;
case XkbOD_BadServerVersion:
throw X11Exception("Bad X11 server version.");
break;
case XkbOD_NonXkbServer:
throw X11Exception("XKB not present.");
break;
case XkbOD_Success:
break;
}
_kbdDescPtr = XkbAllocKeyboard();
if (_kbdDescPtr == NULL) {
XCloseDisplay(_display);
throw X11Exception("Failed to get keyboard description.");
}
_kbdDescPtr->dpy = _display;
if (_deviceId != XkbUseCoreKbd) {
_kbdDescPtr->device_spec = _deviceId;
}
}
XKeyboard::~XKeyboard()
{
if(_kbdDescPtr!=NULL)
XkbFreeKeyboard(_kbdDescPtr, 0, True);
XCloseDisplay(_display);
}
void XKeyboard::BuildLayout(string_vector& vec) {
XkbRF_VarDefsRec vdr;
int i = 0;
char str[20] = {0};
const char s[2] = ",";
char *token;
char* tmp = NULL;
strcpy(str, (
!_display ? NO_KEYBOARD :
(XkbRF_GetNamesProp(_display, &tmp, &vdr) && vdr.layout) ?
vdr.layout : DFLT_XKB_LAYOUT));
/* get the first token */
token = strtok(str, s);
/* walk through other tokens */
while( token != NULL )
{
vec.push_back(token);
token = strtok(NULL, s);
}
}
void XKeyboard::wait_event()
{
CHECK(_display != 0);
Bool bret = XkbSelectEventDetails(_display, XkbUseCoreKbd,
XkbStateNotify, XkbAllStateComponentsMask, XkbGroupStateMask);
CHECK_MSG(bret==True, "XkbSelectEventDetails failed");
XEvent event;
int iret = XNextEvent(_display, &event);
CHECK_MSG(iret==0, "XNextEvent failed with " << iret);
}
void XKeyboard::set_group(int groupNum)
{
Bool result = XkbLockGroup(_display, _deviceId, groupNum);
CHECK(result == True);
}
int XKeyboard::get_group() const
{
XkbStateRec xkbState;
XkbGetState(_display, _deviceId, &xkbState);
return static_cast<int>(xkbState.group);
}
// returns true if symbol is ok
bool filter(const string_vector& nonsyms, const std::string& symbol)
{
if(symbol.empty())
return false;
// Filter out all prohibited words
string_vector::const_iterator r = find(nonsyms.begin(), nonsyms.end(), symbol);
if(r != nonsyms.end())
return false;
// Filter out all numbers groups started with number
if(isdigit(symbol[0]))
return false;
return true;
}
string_vector parse1(const std::string& symbols, const string_vector& nonsyms)
{
bool inSymbol = false;
std::string sym;
string_vector symlist;
for (size_t i = 0; i < symbols.size(); i++) {
char ch = symbols[i];
if (ch == '+') {
if (inSymbol && !sym.empty() && filter(nonsyms, sym)) {
symlist.push_back(sym);
}
inSymbol = true;
sym.clear();
}
else if (inSymbol && ch == '(') {
inSymbol = false;
}
else if (inSymbol && (isalpha(static_cast<int>(ch)) || ch == '_')) {
sym.append(1, ch);
}
else {
if (inSymbol && !sym.empty() && filter(nonsyms, sym)) {
symlist.push_back(sym);
}
inSymbol = false;
}
}
if (inSymbol && !sym.empty() && filter(nonsyms, sym)) {
symlist.push_back(sym);
}
return symlist;
}
void safe_push_back(string_vector& v, std::string s, std::string note)
{
if(s.empty()) return;
if(!note.empty()) {
s += "(" + note + ")";
}
v.push_back(s);
}
bool goodchar(char ch)
{
return (isdigit(ch) || isalpha(static_cast<int>(ch)) || ch == '_' || ch == '-');
}
string_vector parse2(const std::string& symbols, const string_vector& nonsyms)
{
enum{ok,skip,broken} state = ok;
int paren = 0;
std::string sym;
// Words between optional '(' ')'
std::string note;
string_vector symlist;
for (size_t i = 0; i < symbols.size(); i++) {
char ch = symbols[i];
if (ch == '+') {
if (state != broken && paren == 0 && filter(nonsyms, sym)) {
safe_push_back(symlist, sym, note);
}
state = ok;
paren = 0;
sym.clear();
note.clear();
}
else if (state == ok && ch == '(') {
paren++;
}
else if (state == ok && ch == ')') {
paren--;
}
else if (state == ok && ch == ':') {
state = skip;
}
else if (state == ok && goodchar(ch)) {
if (paren == 0)
sym.append(1, ch);
else
note.append(1, ch);
}
else if(state == ok) {
state = broken;
}
}
if (state != broken && paren == 0 && filter(nonsyms, sym)) {
safe_push_back(symlist, sym, note);
}
return symlist;
}
string_vector parse3(const std::string& symbols, const string_vector& nonsyms)
{
enum{ok,skip,broken} state = ok;
int paren = 0;
std::string sym;
// Words between optional '(' ')'
std::string note;
string_vector symlist;
for (size_t i = 0; i < symbols.size(); i++) {
char ch = symbols[i];
if (ch == '+' || ch == '_') {
if(paren == 0) {
if (state != broken && paren == 0 && filter(nonsyms, sym)) {
safe_push_back(symlist, sym, note);
}
state = ok;
sym.clear();
note.clear();
}
}
else if (state == ok && ch == '(') {
paren++;
}
else if (state == ok && ch == ')') {
paren--;
}
else if (state == ok && ch == ':') {
state = skip;
}
else if (state == ok && goodchar(ch)) {
if (paren == 0)
sym.append(1, ch);
else
note.append(1, ch);
}
else if(state == ok) {
state = broken;
}
}
if (state != broken && paren == 0 && filter(nonsyms, sym)) {
safe_push_back(symlist, sym, note);
}
return symlist;
}
}