Added layout variant code to XKeyboard class

This commit is contained in:
Kristian Setälä 2010-01-02 23:01:53 +02:00
parent 430b9daac6
commit 2170545899
2 changed files with 42 additions and 5 deletions

View File

@ -8,6 +8,8 @@
// any later version. // any later version.
// //
// $Id: XKeyboard.cpp 53 2008-07-18 08:38:47Z jay $ // $Id: XKeyboard.cpp 53 2008-07-18 08:38:47Z jay $
//
// 2010-01-02 Kristian Setälä added code to retrieve layout variant information
#include "XKeyboard.h" #include "XKeyboard.h"
#include "X11Exception.h" #include "X11Exception.h"
@ -70,7 +72,7 @@ Bool XKeyboard::initializeXkb()
int major = XkbMajorVersion; int major = XkbMajorVersion;
int minor = XkbMinorVersion; int minor = XkbMinorVersion;
int opCode; int opCode;
Bool status = XkbQueryExtension(_display, &opCode, &_baseEventCode, &_baseErrorCode, &major, &minor); /*Bool status =*/ XkbQueryExtension(_display, &opCode, &_baseEventCode, &_baseErrorCode, &major, &minor);
XkbDescRec* kbdDescPtr = XkbAllocKeyboard(); XkbDescRec* kbdDescPtr = XkbAllocKeyboard();
if (kbdDescPtr == NULL) { if (kbdDescPtr == NULL) {
@ -145,7 +147,7 @@ Bool XKeyboard::initializeXkb()
} }
XkbSymbolParser symParser; XkbSymbolParser symParser;
symParser.parse(symName, _symbolNames); symParser.parse(symName, _symbolNames, _variantNames);
int count = _symbolNames.size(); int count = _symbolNames.size();
if (count == 1 && _groupNames[0].empty() && _symbolNames[0] == "jp") { if (count == 1 && _groupNames[0].empty() && _symbolNames[0] == "jp") {
_groupCount = 2; _groupCount = 2;
@ -245,6 +247,11 @@ StringVector XKeyboard::groupSymbols() const
return _symbolNames; return _symbolNames;
} }
StringVector XKeyboard::groupVariants() const
{
return _variantNames;
}
int XKeyboard::currentGroupNum() const int XKeyboard::currentGroupNum() const
{ {
XkbStateRec xkbState; XkbStateRec xkbState;
@ -262,6 +269,11 @@ std::string XKeyboard::currentGroupSymbol() const
return _symbolNames[currentGroupNum()]; return _symbolNames[currentGroupNum()];
} }
std::string XKeyboard::currentGroupVariant() const
{
return _variantNames[currentGroupNum()];
}
bool XKeyboard::setGroupByNum(int groupNum) bool XKeyboard::setGroupByNum(int groupNum)
{ {
if (_groupCount <= 1) { if (_groupCount <= 1) {
@ -302,30 +314,48 @@ XkbSymbolParser::~XkbSymbolParser()
_nonSymbols.clear(); _nonSymbols.clear();
} }
void XkbSymbolParser::parse(const std::string& symbols, StringVector& symbolList) void XkbSymbolParser::parse(const std::string& symbols, StringVector& symbolList,
StringVector& variantList)
{ {
bool inSymbol = false; bool inSymbol = false;
std::string curSymbol; std::string curSymbol;
std::string curVariant;
for (int i = 0; i < symbols.size(); i++) { //std::cout << symbols << std::endl;
// A sample line:
// pc+fi(dvorak)+fi:2+ru:3+inet(evdev)+group(menu_toggle)
for (size_t i = 0; i < symbols.size(); i++) {
char ch = symbols[i]; char ch = symbols[i];
if (ch == '+') { if (ch == '+') {
if (inSymbol) { if (inSymbol) {
if (isXkbLayoutSymbol(curSymbol)) { if (isXkbLayoutSymbol(curSymbol)) {
symbolList.push_back(curSymbol); symbolList.push_back(curSymbol);
variantList.push_back(curVariant);
} }
curSymbol.clear(); curSymbol.clear();
curVariant.clear();
} else { } else {
inSymbol = true; inSymbol = true;
} }
} else if (inSymbol && (isalpha(static_cast<int>(ch)) || ch == '_')) { } else if (inSymbol && (isalpha(static_cast<int>(ch)) || ch == '_')) {
curSymbol.append(1, ch); curSymbol.append(1, ch);
} else if (inSymbol && ch == '(') {
while (++i < symbols.size()) {
ch = symbols[i];
if (ch == ')')
break;
else
curVariant.append(1, ch);
}
} else { } else {
if (inSymbol) { if (inSymbol) {
if (isXkbLayoutSymbol(curSymbol)) { if (isXkbLayoutSymbol(curSymbol)) {
symbolList.push_back(curSymbol); symbolList.push_back(curSymbol);
variantList.push_back(curVariant);
} }
curSymbol.clear(); curSymbol.clear();
curVariant.clear();
inSymbol = false; inSymbol = false;
} }
} }
@ -333,6 +363,7 @@ void XkbSymbolParser::parse(const std::string& symbols, StringVector& symbolList
if (inSymbol && !curSymbol.empty() && isXkbLayoutSymbol(curSymbol)) { if (inSymbol && !curSymbol.empty() && isXkbLayoutSymbol(curSymbol)) {
symbolList.push_back(curSymbol); symbolList.push_back(curSymbol);
variantList.push_back(curVariant);
} }
} }

View File

@ -8,6 +8,8 @@
// any later version. // any later version.
// //
// $Id: XKeyboard.h 29 2008-04-09 21:37:44Z jay $ // $Id: XKeyboard.h 29 2008-04-09 21:37:44Z jay $
//
// 2010-01-02 Kristian Setälä added code to retrieve layout variant information
#ifndef XKEYBOARD_H_1C79861A_49B3_4A95_88D6_455C22FEB222 #ifndef XKEYBOARD_H_1C79861A_49B3_4A95_88D6_455C22FEB222
#define XKEYBOARD_H_1C79861A_49B3_4A95_88D6_455C22FEB222 #define XKEYBOARD_H_1C79861A_49B3_4A95_88D6_455C22FEB222
@ -31,9 +33,11 @@ public:
int groupCount() const; int groupCount() const;
StringVector groupNames() const; StringVector groupNames() const;
StringVector groupSymbols() const; StringVector groupSymbols() const;
StringVector groupVariants() const;
int currentGroupNum() const; int currentGroupNum() const;
std::string currentGroupName() const; std::string currentGroupName() const;
std::string currentGroupSymbol() const; std::string currentGroupSymbol() const;
std::string currentGroupVariant() const;
bool setGroupByNum(int groupNum); bool setGroupByNum(int groupNum);
bool changeGroup(int increment); bool changeGroup(int increment);
@ -51,6 +55,7 @@ private:
int _groupCount; int _groupCount;
StringVector _groupNames; StringVector _groupNames;
StringVector _symbolNames; StringVector _symbolNames;
StringVector _variantNames;
int _currentGroupNum; int _currentGroupNum;
int _deviceId; int _deviceId;
@ -68,7 +73,8 @@ public:
XkbSymbolParser(); XkbSymbolParser();
~XkbSymbolParser(); ~XkbSymbolParser();
void parse(const std::string& symbols, std::vector<std::string>& symbolList); void parse(const std::string& symbols, std::vector<std::string>& symbolList,
std::vector<std::string>& variantList);
private: private:
bool isXkbLayoutSymbol(const std::string& symbol); bool isXkbLayoutSymbol(const std::string& symbol);