forked from sergei-mironov/xkb-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXKbSwitch.cpp
More file actions
228 lines (202 loc) · 5.65 KB
/
Copy pathXKbSwitch.cpp
File metadata and controls
228 lines (202 loc) · 5.65 KB
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
/*
* Plain launcher program for Jay Bromley's Xkeyboard library.
*
* Copyright (C) 2010 by Sergey Mironov <ierton@gmail.com>
*
* 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 2 of the License, or (at your option)
* any later version.
*/
#include <iostream>
#include <algorithm>
#include "XKeyboard.h"
#include "X11Exception.h"
#include <X11/XKBlib.h>
#include <sstream>
#define XKB_SWITCH_VERSION "1.3.1"
using namespace std;
using namespace kb;
void usage()
{
cerr << "Usage: xkb-switch -s ARG Sets current layout group to ARG" << endl;
cerr << " xkb-switch -l|--list Displays all layout groups" << endl;
cerr << " xkb-switch -h|--help Displays this message" << endl;
cerr << " xkb-switch -v|--version Shows version number" << endl;
cerr << " xkb-switch -w|--wait [-p] Waits for group change and exits" << endl;
cerr << " xkb-switch -W Infinitely waits for group change" << endl;
cerr << " xkb-switch -n|--next Switch to the next layout group" << endl;
cerr << " xkb-switch -x Print X layout string" << endl;
cerr << " xkb-switch [-p] Displays current layout group" << endl;
cerr << " xkb-switch --test Run some internal tests" << endl;
}
string_vector (*parse)(const std::string&, const string_vector&) = parse2;
string print_layouts(const string_vector& sv)
{
ostringstream oss;
bool fst = true;
oss << "[";
for(string_vector::const_iterator i=sv.begin(); i!=sv.end(); i++) {
if(!fst) oss << " ";
oss << *i;
fst = false;
}
oss << "]";
return oss.str();
}
int run_tests()
{
string kbs;
string_vector sv;
try {
kbs = "us+sk1(qwerty1):2+at:3+us(alt-intl):4+inet(evdev)+compose(ralt)";
sv = parse(kbs, nonsyms());
CHECK(sv.at(0) == "us");
CHECK(sv.at(1) == "sk1(qwerty1)");
CHECK(sv.at(2) == "at");
CHECK(sv.at(3) == "us(alt-intl)");
cout << kbs << " " << print_layouts(sv) << endl;
kbs = "us+sk(qwerty):2+at:3+us(alt-intl):4+inet(evdev)+compose(ralt)";
sv = parse(kbs, nonsyms());
CHECK(sv.at(0) == "us");
CHECK(sv.at(1) == "sk(qwerty)");
CHECK(sv.at(2) == "at");
CHECK(sv.at(3) == "us(alt-intl)");
cout << kbs << " " << print_layouts(sv) << endl;
kbs = "pc+us+ru:2+inet(evdev)+group(alt_space_toggle)+ctrl(nocaps)+ctrl(swapcaps)+eurosign(e)";
sv = parse(kbs, nonsyms());
CHECK(sv.at(0) == "us");
CHECK(sv.at(1) == "ru");
cout << kbs << " " << print_layouts(sv) << endl;
return 0;
}
catch (exception & e) {
cerr << "xkb-switch: test failed: " << e.what() << endl;
cerr << "xkb-switch: kbs: " << kbs << endl;
cerr << "xkb-switch: layouts: " << print_layouts(sv) << endl;
return 1;
}
}
int main( int argc, char* argv[] )
{
string_vector syms;
using namespace std;
try {
int m_cnt = 0;
int m_wait = 0;
int m_lwait = 0;
int m_print = 0;
int m_next = 0;
int m_list = 0;
int m_x = 0;
int m_test = 0;
string newgrp;
XKeyboard xkb;
for(int i=1; i<argc; i) {
string arg(argv[i++]);
if(arg == "-s") {
CHECK_MSG(i<argc, "Argument expected");
newgrp=argv[i++];
m_cnt++;
}
else if(arg == "-l" || arg == "--list") {
m_list = 1;
m_cnt++;
}
else if(arg == "-v" || arg=="--version") {
cerr << "xkb-switch " << XKB_SWITCH_VERSION << endl;
return 0;
}
else if(arg == "-w" || arg == "--wait") {
m_wait = 1;
m_cnt++;
}
else if(arg == "-W" || arg == "--longwait") {
m_lwait = 1;
m_cnt++;
}
else if(arg == "-p" || arg == "--print") {
m_print = 1;
m_cnt++;
}
else if(arg == "-n" || arg == "--next") {
m_next = 1;
m_cnt++;
}
else if(arg == "-x") {
m_x = 1;
m_cnt++;
}
else if(arg == "--test") {
m_test = 1;
m_cnt++;
}
else if(arg == "-h" || arg == "--help") {
usage();
return 1;
}
else {
throw string("Invalid argument: " + arg);
}
}
if(m_list || m_lwait || !newgrp.empty() || m_next)
CHECK_MSG(m_cnt==1, "Invalid flag combination. Try --help.");
// Default action
if(m_cnt==0)
m_print = 1;
if(m_test) {
return run_tests();
}
if(m_wait) {
xkb.wait_event();
}
if(m_lwait) {
while(true) {
xkb.wait_event();
syms = parse(xkb.get_kb_string(), nonsyms());
cout << syms.at(xkb.get_group()) << endl;
}
}
string kbs = xkb.get_kb_string();
if (m_x) {
cout << kbs << endl;
}
syms = parse(kbs, nonsyms());
if (m_next) {
CHECK_MSG(!syms.empty(), "No layout groups configured");
const string nextgrp = syms.at(xkb.get_group());
string_vector::iterator i = find(syms.begin(), syms.end(), nextgrp);
if (++i == syms.end()) i = syms.begin();
xkb.set_group(i - syms.begin());
}
else if(!newgrp.empty()) {
string_vector::iterator i = find(syms.begin(), syms.end(), newgrp);
CHECK_MSG(i!=syms.end(),
"Group " << newgrp << "' is not supported by current layout. Try xkb-switch -l.");
xkb.set_group(i-syms.begin());
}
if(m_print) {
cout << syms.at(xkb.get_group()) << endl;
}
if(m_list) {
for(int i=0; i<syms.size(); i++) {
cout << syms[i] << endl;
}
}
return 0;
}
catch(X11Exception &err) {
cerr << "xkb-switch: " << err.what() << endl;
cerr << "xkb-switch: layouts: " << print_layouts(syms) << endl;
return 2;
}
catch(std::string & err) {
cerr << "xkb-switch: " << err << endl;
cerr << "xkb-switch: layouts: " << print_layouts(syms) << endl;
return 2;
}
catch(std::exception & err) {
cerr << "xkb-switch: " << err.what() << endl;
cerr << "xkb-switch: layouts: " << print_layouts(syms) << endl;
}
}