Skip to content

Commit

Permalink
Output layout variants together with layouts (sergei-mironov#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-mironov committed Apr 17, 2016
1 parent e2fa28d commit e6266a1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 44 deletions.
6 changes: 3 additions & 3 deletions .vimrc_local.vim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set tabstop=8
set shiftwidth=8
set cinoptions=g0,(8
set tabstop=2
set shiftwidth=2
set cinoptions=g0,(2
set textwidth=80
9 changes: 5 additions & 4 deletions XKbSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ int main( int argc, char* argv[] )
int m_list = 0;
string newgrp;

XKeyboard xkb;

for(int i=1; i<argc; i) {
string arg(argv[i++]);
if(arg == "-s") {
Expand Down Expand Up @@ -121,22 +119,25 @@ int main( int argc, char* argv[] )
if(m_cnt==0)
m_print = 1;

XKeyboard xkb;
xkb.open_display();

if(m_wait) {
xkb.wait_event();
}

if(m_lwait) {
while(true) {
xkb.wait_event();
xkb.BuildLayout(syms);
xkb.build_layout(syms);
cout << syms.at(xkb.get_group()) << endl;
}
}

if (m_lwait)
syms.clear();

xkb.BuildLayout(syms);
xkb.build_layout(syms);

if (m_next) {
CHECK_MSG(!syms.empty(), "No layout groups configured");
Expand Down
3 changes: 2 additions & 1 deletion XKbSwitchApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace
try
{
xkb = new XKeyboard();
xkb->open_display();
}
catch( ... )
{
Expand Down Expand Up @@ -72,7 +73,7 @@ namespace
return symNames;
}

xkb->BuildLayout(symNames);
xkb->build_layout(symNames);
return symNames;
}
}
Expand Down
68 changes: 33 additions & 35 deletions XKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,18 @@
#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>
#include <sstream>

namespace kb {

XKeyboard::XKeyboard()
: _display(0), _deviceId(XkbUseCoreKbd)
{
}

void XKeyboard::open_display()
{

XkbIgnoreExtension(False);
Expand All @@ -44,6 +40,7 @@ XKeyboard::XKeyboard()
int major = XkbMajorVersion;
int minor = XkbMinorVersion;
int reasonReturn;

_display = XkbOpenDisplay(displayName, &eventCode, &errorReturn, &major,
&minor, &reasonReturn);
switch (reasonReturn) {
Expand All @@ -65,7 +62,6 @@ XKeyboard::XKeyboard()

_kbdDescPtr = XkbAllocKeyboard();
if (_kbdDescPtr == NULL) {
XCloseDisplay(_display);
throw X11Exception("Failed to get keyboard description.");
}

Expand All @@ -84,34 +80,36 @@ XKeyboard::~XKeyboard()
}


void XKeyboard::BuildLayout(string_vector& vec)
void XKeyboard::build_layout(string_vector& out)
{
XkbRF_VarDefsRec vdr;
int i = 0;
char str[256] = {0};
const char s[2] = ",";
char *token;
char* tmp = NULL;

strncpy(str, (
!_display ? NO_KEYBOARD :
(XkbRF_GetNamesProp(_display, &tmp, &vdr) && vdr.layout) ?
vdr.layout : DFLT_XKB_LAYOUT),
256);
str[255] = 0;

/* get the first token */
token = strtok(str, s);

/* walk through other tokens */
while( token != NULL )
{
vec.push_back(token);
token = strtok(NULL, s);
}
}
using namespace std;

XkbRF_VarDefsRec vdr;
char* tmp = NULL;
Bool bret;

bret = XkbRF_GetNamesProp(_display, &tmp, &vdr);
CHECK_MSG(bret==True, "Failed to get keyboard properties");

istringstream layout(vdr.layout ? vdr.layout : "us");
istringstream variant(vdr.variant ? vdr.variant : "");

while(true) {
string l,v;

getline(layout, l, ',');
getline(variant, v, ',');
if(!layout && !variant)
break;

if(v!="") {
v = "(" + v + ")";
}
if(l!="") {
out.push_back(l+v);
}
}
}

void XKeyboard::wait_event()
{
Expand Down
7 changes: 6 additions & 1 deletion XKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,29 @@ namespace kb {

typedef std::vector<std::string> string_vector;


class XKeyboard
{
public:

Display* _display;
int _deviceId;
XkbDescRec* _kbdDescPtr;

XKeyboard();
~XKeyboard();

// Opens display (or throw X11Exception)
void open_display(void);

// Gets the current layout
int get_group() const;

// Sets the layout
void set_group(int num);

// Returns keyboard layout string
void BuildLayout(string_vector& vec);
void build_layout(string_vector& vec);

// Waits for kb event
void wait_event();
Expand Down

0 comments on commit e6266a1

Please sign in to comment.