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.
//
// $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 "X11Exception.h"
@ -70,7 +72,7 @@ Bool XKeyboard::initializeXkb()
int major = XkbMajorVersion;
int minor = XkbMinorVersion;
int opCode;
Bool status = XkbQueryExtension(_display, &opCode, &_baseEventCode, &_baseErrorCode, &major, &minor);
/*Bool status =*/ XkbQueryExtension(_display, &opCode, &_baseEventCode, &_baseErrorCode, &major, &minor);
XkbDescRec* kbdDescPtr = XkbAllocKeyboard();
if (kbdDescPtr == NULL) {
@ -145,7 +147,7 @@ Bool XKeyboard::initializeXkb()
}
XkbSymbolParser symParser;
symParser.parse(symName, _symbolNames);
symParser.parse(symName, _symbolNames, _variantNames);
int count = _symbolNames.size();
if (count == 1 && _groupNames[0].empty() && _symbolNames[0] == "jp") {
_groupCount = 2;
@ -245,6 +247,11 @@ StringVector XKeyboard::groupSymbols() const
return _symbolNames;
}
StringVector XKeyboard::groupVariants() const
{
return _variantNames;
}
int XKeyboard::currentGroupNum() const
{
XkbStateRec xkbState;
@ -262,6 +269,11 @@ std::string XKeyboard::currentGroupSymbol() const
return _symbolNames[currentGroupNum()];
}
std::string XKeyboard::currentGroupVariant() const
{
return _variantNames[currentGroupNum()];
}
bool XKeyboard::setGroupByNum(int groupNum)
{
if (_groupCount <= 1) {
@ -302,30 +314,48 @@ XkbSymbolParser::~XkbSymbolParser()
_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;
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];
if (ch == '+') {
if (inSymbol) {
if (isXkbLayoutSymbol(curSymbol)) {
symbolList.push_back(curSymbol);
variantList.push_back(curVariant);
}
curSymbol.clear();
curVariant.clear();
} else {
inSymbol = true;
}
} else if (inSymbol && (isalpha(static_cast<int>(ch)) || 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 {
if (inSymbol) {
if (isXkbLayoutSymbol(curSymbol)) {
symbolList.push_back(curSymbol);
variantList.push_back(curVariant);
}
curSymbol.clear();
curVariant.clear();
inSymbol = false;
}
}
@ -333,6 +363,7 @@ void XkbSymbolParser::parse(const std::string& symbols, StringVector& symbolList
if (inSymbol && !curSymbol.empty() && isXkbLayoutSymbol(curSymbol)) {
symbolList.push_back(curSymbol);
variantList.push_back(curVariant);
}
}

View File

@ -8,6 +8,8 @@
// any later version.
//
// $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
#define XKEYBOARD_H_1C79861A_49B3_4A95_88D6_455C22FEB222
@ -31,9 +33,11 @@ public:
int groupCount() const;
StringVector groupNames() const;
StringVector groupSymbols() const;
StringVector groupVariants() const;
int currentGroupNum() const;
std::string currentGroupName() const;
std::string currentGroupSymbol() const;
std::string currentGroupVariant() const;
bool setGroupByNum(int groupNum);
bool changeGroup(int increment);
@ -51,6 +55,7 @@ private:
int _groupCount;
StringVector _groupNames;
StringVector _symbolNames;
StringVector _variantNames;
int _currentGroupNum;
int _deviceId;
@ -68,7 +73,8 @@ public:
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:
bool isXkbLayoutSymbol(const std::string& symbol);