2010-01-03 05:02:57 +08:00
|
|
|
// wrapper.cpp
|
|
|
|
// A small wrapper program around Jay Bromley's keyboard layout information class.
|
|
|
|
//
|
|
|
|
// Compilation:
|
|
|
|
// g++ -o xkblayout-state XKeyboard.cpp wrapper.cpp -lX11
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2010-01-08 04:01:36 +08:00
|
|
|
#include <cstdlib>
|
2010-01-03 05:02:57 +08:00
|
|
|
#include <iostream>
|
2010-01-08 04:01:36 +08:00
|
|
|
#include <sstream>
|
2010-01-03 05:02:57 +08:00
|
|
|
#include <string>
|
|
|
|
#include "XKeyboard.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
void print_usage(string progname)
|
|
|
|
{
|
|
|
|
cerr << endl
|
2010-01-08 04:01:36 +08:00
|
|
|
<< "xkblayout-state version 1b" << endl
|
2010-01-03 05:02:57 +08:00
|
|
|
<< endl
|
|
|
|
<< "Usage: " << endl
|
|
|
|
<< endl
|
|
|
|
<< "To get the current layout(s):" << endl
|
2010-01-08 02:32:06 +08:00
|
|
|
<< " " << progname << " print format" << endl
|
2010-01-03 05:02:57 +08:00
|
|
|
<< endl
|
|
|
|
<< "This causes the 'format' string to be printed on stdout with the following substitutions:" << endl
|
|
|
|
<< " %c -> current layout number" << endl
|
|
|
|
<< " %n -> current layout name" << endl
|
|
|
|
<< " %s -> current layout symbol" << endl
|
|
|
|
<< " %v -> current layout variant" << endl
|
|
|
|
<< " %e -> current layout variant (equals to %s if %v is empty)" << endl
|
|
|
|
<< " %C -> layout count" << endl
|
|
|
|
<< " %N -> layout names (one per line)" << endl
|
|
|
|
<< " %S -> layout symbols (one per line)" << endl
|
|
|
|
<< " %V -> layout variants (one per line)" << endl
|
|
|
|
<< " %E -> layout variants (one per line; layout symbol is used if variant is empty)" << endl
|
|
|
|
<< " %% -> A literal '%'" << endl
|
|
|
|
<< endl
|
|
|
|
<< "For example:" << endl
|
2010-01-08 02:32:06 +08:00
|
|
|
<< " " << progname << " print \"Current layout: %s(%e)\"" << endl
|
2010-01-03 05:02:57 +08:00
|
|
|
<< endl
|
|
|
|
<< "To set the current layout:" << endl
|
2010-01-08 02:32:06 +08:00
|
|
|
<< " " << progname << " set layout_number" << endl
|
2010-01-03 05:02:57 +08:00
|
|
|
<< endl
|
|
|
|
<< "Here 'layout_number' is the number of the layout to be set (starting from 0)" << endl
|
|
|
|
<< "and can be either absolute (default) or relative (if preceded with a plus or minus sign)." << endl
|
|
|
|
<< endl
|
|
|
|
<< "For example:" << endl
|
2010-01-08 02:32:06 +08:00
|
|
|
<< " " << progname << " set 1" << endl
|
|
|
|
<< " " << progname << " set +1" << endl
|
2010-01-03 05:02:57 +08:00
|
|
|
<< endl;
|
|
|
|
}
|
|
|
|
|
2010-01-08 04:01:36 +08:00
|
|
|
bool print_status(XKeyboard& xkb, string format) {
|
|
|
|
stringstream r; // resulting string
|
|
|
|
|
2010-01-03 05:02:57 +08:00
|
|
|
for (size_t i = 0; i < format.length(); ++i) {
|
2015-07-28 06:02:07 +08:00
|
|
|
if (i < format.length()-2 && format[i] == '%') {
|
2010-01-03 05:02:57 +08:00
|
|
|
switch (format[i+1]) {
|
|
|
|
case 'c':
|
2010-01-08 04:01:36 +08:00
|
|
|
r << xkb.currentGroupNum();
|
2010-01-03 05:02:57 +08:00
|
|
|
break;
|
|
|
|
case 'n':
|
2010-01-08 04:01:36 +08:00
|
|
|
r << xkb.currentGroupName();
|
2010-01-03 05:02:57 +08:00
|
|
|
break;
|
|
|
|
case 's':
|
2010-01-08 04:01:36 +08:00
|
|
|
r << xkb.currentGroupSymbol();
|
2010-01-03 05:02:57 +08:00
|
|
|
break;
|
|
|
|
case 'v':
|
2010-01-08 04:01:36 +08:00
|
|
|
r << xkb.currentGroupVariant();
|
2010-01-03 05:02:57 +08:00
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
if (xkb.currentGroupVariant().empty())
|
2010-01-08 04:01:36 +08:00
|
|
|
r << xkb.currentGroupSymbol();
|
2010-01-03 05:02:57 +08:00
|
|
|
else
|
2010-01-08 04:01:36 +08:00
|
|
|
r << xkb.currentGroupVariant();
|
2010-01-03 05:02:57 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
2010-01-08 04:01:36 +08:00
|
|
|
r << xkb.groupCount();
|
2010-01-03 05:02:57 +08:00
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
for (size_t j = 0; j < xkb.groupNames().size(); ++j)
|
2010-01-08 04:01:36 +08:00
|
|
|
r << xkb.groupNames()[j] << endl;
|
2010-01-03 05:02:57 +08:00
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
for (size_t j = 0; j < xkb.groupSymbols().size(); ++j)
|
2010-01-08 04:01:36 +08:00
|
|
|
r << xkb.groupSymbols()[j] << endl;
|
2010-01-03 05:02:57 +08:00
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
for (size_t j = 0; j < xkb.groupVariants().size(); ++j)
|
2010-01-08 04:01:36 +08:00
|
|
|
r << xkb.groupVariants()[j] << endl;
|
2010-01-03 05:02:57 +08:00
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
for (size_t j = 0; j < xkb.groupVariants().size(); ++j)
|
|
|
|
if (xkb.groupVariants()[j].empty())
|
2010-01-08 04:01:36 +08:00
|
|
|
r << xkb.groupSymbols()[j] << endl;
|
2010-01-03 05:02:57 +08:00
|
|
|
else
|
2010-01-08 04:01:36 +08:00
|
|
|
r << xkb.groupVariants()[j] << endl;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '%':
|
|
|
|
r << '%';
|
2010-01-03 05:02:57 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-01-08 04:01:36 +08:00
|
|
|
cerr << "Unknown format character: " << format[i+1] << endl;
|
|
|
|
return false;
|
2010-01-03 05:02:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
} else { // not '%'
|
2010-01-08 04:01:36 +08:00
|
|
|
r << format[i];
|
2010-01-03 05:02:57 +08:00
|
|
|
}
|
|
|
|
}
|
2010-01-08 04:01:36 +08:00
|
|
|
cout << r.str();
|
|
|
|
return true;
|
2010-01-03 05:02:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool set_group(XKeyboard& xkb, string group) {
|
2010-01-08 04:23:02 +08:00
|
|
|
bool relative = false;
|
|
|
|
|
|
|
|
// Check that 'group' is a valid integer (and whether it's relative or not)
|
|
|
|
size_t i = 0;
|
|
|
|
if (group[0] == '+' || group[0] == '-') {
|
|
|
|
relative = true;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
for (; i < group.length(); ++i) {
|
|
|
|
if (!isdigit(group[i])) {
|
|
|
|
cerr << group << " is not an integer" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-03 05:02:57 +08:00
|
|
|
int group_int = atoi(group.c_str());
|
2010-01-08 04:23:02 +08:00
|
|
|
if (relative) {
|
|
|
|
if (!xkb.changeGroup(group_int)) {
|
|
|
|
cerr << "Failed to change group" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (group_int >= xkb.groupCount()) {
|
|
|
|
cerr << "layout_number must be between 0 and " << xkb.groupCount()-1 << endl;
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (!xkb.setGroupByNum(group_int)) {
|
|
|
|
cerr << "Failed to change group" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2010-01-03 05:02:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
XKeyboard xkb;
|
|
|
|
|
|
|
|
if (argc != 3) {
|
|
|
|
print_usage(string(argv[0]));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
} else {
|
|
|
|
string command(argv[1]);
|
2010-01-08 02:32:06 +08:00
|
|
|
if (command == "print") {
|
2010-01-08 04:01:36 +08:00
|
|
|
if (!print_status(xkb, string(argv[2])))
|
|
|
|
return EXIT_FAILURE;
|
2010-01-08 02:32:06 +08:00
|
|
|
} else if (command == "set") {
|
2010-01-08 04:23:02 +08:00
|
|
|
if (!set_group(xkb, string(argv[2])))
|
2010-01-03 05:02:57 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
} else {
|
|
|
|
print_usage(string(argv[0]));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-28 06:02:07 +08:00
|
|
|
catch (const exception *e) {
|
|
|
|
cerr << e->what() << endl;
|
2010-01-03 05:02:57 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2010-01-08 04:01:36 +08:00
|
|
|
return EXIT_SUCCESS;
|
2010-01-03 05:02:57 +08:00
|
|
|
}
|
2010-01-08 04:23:02 +08:00
|
|
|
|