Removed Fluxbox and ScreenInfo dep, moved from strtok to StringUtil::stringtok
This commit is contained in:
parent
472602d2bc
commit
2765d34a19
1 changed files with 74 additions and 66 deletions
132
src/Keys.cc
132
src/Keys.cc
|
@ -19,6 +19,7 @@
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
// DEALINGS IN THE SOFTWARE.
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
//$Id: Keys.cc,v 1.4 2002/01/07 23:44:09 fluxgen Exp $
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
#ifdef HAVE_CONFIG_H
|
||||||
# include "config.h"
|
# include "config.h"
|
||||||
|
@ -26,9 +27,7 @@
|
||||||
|
|
||||||
#include "Keys.hh"
|
#include "Keys.hh"
|
||||||
|
|
||||||
#ifndef _FLUXBOX_HH_
|
#include "StringUtil.hh"
|
||||||
# include "fluxbox.hh"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef HAVE_STDIO_H
|
#ifdef HAVE_STDIO_H
|
||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
|
@ -70,6 +69,8 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -122,13 +123,18 @@ Keys::t_actionstr Keys::m_actionlist[] = {
|
||||||
{0, LASTKEYGRAB}
|
{0, LASTKEYGRAB}
|
||||||
};
|
};
|
||||||
|
|
||||||
Keys::Keys(char *filename) {
|
Keys::Keys(Display *display, char *filename):
|
||||||
m_abortkey=0;
|
m_abortkey(0),
|
||||||
|
m_display(display)
|
||||||
|
{
|
||||||
|
assert(display);
|
||||||
|
if (filename)
|
||||||
load(filename);
|
load(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
Keys::~Keys() {
|
Keys::~Keys() {
|
||||||
deleteTree();
|
deleteTree();
|
||||||
|
ungrabKeys();
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------- deleteTree -----------
|
//--------- deleteTree -----------
|
||||||
|
@ -146,27 +152,29 @@ void Keys::deleteTree() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------- ungrabKeys ---------
|
||||||
|
// Ungrabs the keys
|
||||||
|
//-----------------------------
|
||||||
|
void Keys::ungrabKeys() {
|
||||||
|
for (int screen=0; screen<ScreenCount(m_display); screen++) {
|
||||||
|
XUngrabKey(m_display, AnyKey, AnyModifier,
|
||||||
|
RootWindow(m_display, screen));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//-------------- load ----------------
|
//-------------- load ----------------
|
||||||
// Load and grab keys
|
// Load and grab keys
|
||||||
// Returns true on success else false
|
// Returns true on success else false
|
||||||
// TODO: error checking and (nls on them? )
|
// TODO: error checking
|
||||||
// possible replacement of strtok
|
|
||||||
//------------------------------------
|
//------------------------------------
|
||||||
bool Keys::load(char *filename) {
|
bool Keys::load(char *filename) {
|
||||||
if (!filename)
|
if (!filename)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
Fluxbox *fluxbox = Fluxbox::instance();
|
|
||||||
Display *display = fluxbox->getXDisplay();
|
|
||||||
ScreenInfo *screeninfo=0;
|
|
||||||
//ungrab all keys
|
//ungrab all keys
|
||||||
int screen=0;
|
ungrabKeys();
|
||||||
while ((screeninfo = fluxbox->getScreenInfo(screen++)) ) {
|
|
||||||
XUngrabKey(display, AnyKey, AnyModifier,
|
|
||||||
screeninfo->getRootWindow());
|
|
||||||
}
|
|
||||||
|
|
||||||
XSync(display, False);
|
XSync(m_display, False);
|
||||||
|
|
||||||
//open the file
|
//open the file
|
||||||
ifstream infile(filename);
|
ifstream infile(filename);
|
||||||
|
@ -174,35 +182,41 @@ bool Keys::load(char *filename) {
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|
||||||
char *linebuffer = new char[1024];
|
auto_ptr<char> linebuffer(new char[1024]);
|
||||||
int line=0;
|
|
||||||
int linepos=0; //position in the line
|
int line=0;//current line, so we can tell the user where the fault is
|
||||||
|
|
||||||
while (!infile.eof()) {
|
while (!infile.eof()) {
|
||||||
infile.getline(linebuffer, 1024);
|
|
||||||
|
infile.getline(linebuffer.get(), 1024);
|
||||||
|
|
||||||
line++;
|
line++;
|
||||||
char *val = strtok(linebuffer, " ");
|
vector<string> val;
|
||||||
linepos = (val==0 ? 0 : strlen(val) + 1);
|
//Parse arguments
|
||||||
|
StringUtil::stringtok(val, linebuffer.get());
|
||||||
|
//must have at least 1 argument
|
||||||
|
if (val.size()<=0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
|
||||||
int numarg = 1;
|
|
||||||
unsigned int key=0, mod=0;
|
unsigned int key=0, mod=0;
|
||||||
char keyarg=0;
|
char keyarg=0;
|
||||||
t_key *current_key=0, *last_key=0;
|
t_key *current_key=0, *last_key=0;
|
||||||
|
|
||||||
while (val) {
|
for (unsigned int argc=0; argc<val.size(); argc++) {
|
||||||
|
|
||||||
if (val[0]!=':') {
|
if (val[argc][0]!=':') {
|
||||||
keyarg++;
|
keyarg++;
|
||||||
if (keyarg==1) //first arg is modifier
|
if (keyarg==1) //first arg is modifier
|
||||||
mod = getModifier(val);
|
mod = getModifier(val[argc].c_str());
|
||||||
else if (keyarg>1) {
|
else if (keyarg>1) {
|
||||||
|
|
||||||
//keyarg=0;
|
//keyarg=0;
|
||||||
int tmpmod=getModifier(val);
|
int tmpmod=getModifier(val[argc].c_str());
|
||||||
if(tmpmod) mod|=tmpmod; //If it's a modifier
|
if(tmpmod)
|
||||||
|
mod|=tmpmod; //If it's a modifier
|
||||||
else{
|
else{
|
||||||
key = getKey(val); // else get the key
|
key = getKey(val[argc].c_str()); // else get the key
|
||||||
if (!current_key) {
|
if (!current_key) {
|
||||||
current_key = new t_key(key, mod);
|
current_key = new t_key(key, mod);
|
||||||
last_key = current_key;
|
last_key = current_key;
|
||||||
|
@ -216,19 +230,18 @@ bool Keys::load(char *filename) {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
val++; //ignore the ':'
|
|
||||||
|
|
||||||
unsigned int i=0;
|
unsigned int i=0;
|
||||||
|
|
||||||
for (i=0; i< LASTKEYGRAB; i++) {
|
for (i=0; i< LASTKEYGRAB; i++) {
|
||||||
if (strcasecmp(m_actionlist[i].string, val) == 0)
|
// +1 on the val[argc] because we dont want to compare the ':'
|
||||||
|
if (strcasecmp(m_actionlist[i].string, val[argc].c_str()+1) == 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < LASTKEYGRAB ) {
|
if (i < LASTKEYGRAB ) {
|
||||||
if (!current_key) {
|
if (!current_key) {
|
||||||
cerr<<"Error on line: "<<line<<endl;
|
cerr<<"Error on line: "<<line<<endl;
|
||||||
cerr<<linebuffer<<endl;
|
cerr<<linebuffer.get()<<endl;
|
||||||
delete current_key;
|
delete current_key;
|
||||||
current_key = 0;
|
current_key = 0;
|
||||||
last_key = 0;
|
last_key = 0;
|
||||||
|
@ -252,7 +265,9 @@ bool Keys::load(char *filename) {
|
||||||
|
|
||||||
last_key->action = m_actionlist[i].action;
|
last_key->action = m_actionlist[i].action;
|
||||||
if (last_key->action == Keys::EXECUTE)
|
if (last_key->action == Keys::EXECUTE)
|
||||||
last_key->execcommand = &linebuffer[linepos];
|
last_key->execcommand =
|
||||||
|
static_cast<char *>(strcasestr(linebuffer.get(), getActionStr(Keys::EXECUTE))+
|
||||||
|
strlen(getActionStr(Keys::EXECUTE)));
|
||||||
|
|
||||||
//add the keychain to list
|
//add the keychain to list
|
||||||
if (!mergeTree(current_key))
|
if (!mergeTree(current_key))
|
||||||
|
@ -261,8 +276,9 @@ bool Keys::load(char *filename) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
if (m_actionlist[i].action == Keys::EXECUTE) {
|
if (m_actionlist[i].action == Keys::EXECUTE) {
|
||||||
|
|
||||||
cerr<<"linepos:"<<linepos<<endl;
|
cerr<<"line:"<<line<<endl;
|
||||||
cerr<<"buffer:"<<&linebuffer[linepos]<<endl;
|
cerr<<"buffer:"<<static_cast<char *>(strcasestr(linebuffer.get(), getActionStr(Keys::EXECUTE))+
|
||||||
|
strlen(getActionStr(Keys::EXECUTE)))<<endl;
|
||||||
cerr<<"command:"<<last_key->execcommand<<endl;
|
cerr<<"command:"<<last_key->execcommand<<endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -275,7 +291,7 @@ bool Keys::load(char *filename) {
|
||||||
|
|
||||||
} else { //destroy list if no action is found
|
} else { //destroy list if no action is found
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
cerr<<"Didnt find action="<<val<<endl;
|
cerr<<"Didnt find action="<<val[argc]<<endl;
|
||||||
#endif
|
#endif
|
||||||
//destroy current_key ... this will also destroy the last_key
|
//destroy current_key ... this will also destroy the last_key
|
||||||
delete current_key;
|
delete current_key;
|
||||||
|
@ -285,13 +301,9 @@ bool Keys::load(char *filename) {
|
||||||
|
|
||||||
break; //dont process this linebuffer more
|
break; //dont process this linebuffer more
|
||||||
}
|
}
|
||||||
numarg++;
|
|
||||||
val = strtok(0, " ");
|
|
||||||
linepos += (val == 0 ? 0 : strlen(val) + 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
delete linebuffer;
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
showTree();
|
showTree();
|
||||||
#endif
|
#endif
|
||||||
|
@ -304,53 +316,50 @@ bool Keys::load(char *filename) {
|
||||||
//----------------------------------------
|
//----------------------------------------
|
||||||
void Keys::grabKey(unsigned int key, unsigned int mod) {
|
void Keys::grabKey(unsigned int key, unsigned int mod) {
|
||||||
|
|
||||||
Fluxbox *fluxbox = Fluxbox::instance();
|
|
||||||
Display *display = fluxbox->getXDisplay();
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
cerr<<__FILE__<<"("<<__LINE__<<"): keycode "<<key<<" mod "<<hex<<mod<<dec<<endl;
|
cerr<<__FILE__<<"("<<__LINE__<<"): keycode "<<key<<" mod "<<hex<<mod<<dec<<endl;
|
||||||
#endif
|
#endif
|
||||||
int i=0;
|
|
||||||
ScreenInfo *screeninfo=0;
|
|
||||||
|
|
||||||
while ((screeninfo = fluxbox->getScreenInfo(i++)) ) {
|
for (int screen=0; screen<ScreenCount(m_display); screen++) {
|
||||||
Window root = screeninfo->getRootWindow();
|
|
||||||
XGrabKey(display, key, mod,
|
Window root = RootWindow(m_display, screen);
|
||||||
|
|
||||||
|
XGrabKey(m_display, key, mod,
|
||||||
root, True,
|
root, True,
|
||||||
GrabModeAsync, GrabModeAsync);
|
GrabModeAsync, GrabModeAsync);
|
||||||
|
|
||||||
// Grab with numlock, capslock and scrlock
|
// Grab with numlock, capslock and scrlock
|
||||||
|
|
||||||
//numlock
|
//numlock
|
||||||
XGrabKey(display, key, mod|Mod2Mask,
|
XGrabKey(m_display, key, mod|Mod2Mask,
|
||||||
root, True,
|
root, True,
|
||||||
GrabModeAsync, GrabModeAsync);
|
GrabModeAsync, GrabModeAsync);
|
||||||
//scrolllock
|
//scrolllock
|
||||||
XGrabKey(display, key, mod|Mod5Mask,
|
XGrabKey(m_display, key, mod|Mod5Mask,
|
||||||
root, True,
|
root, True,
|
||||||
GrabModeAsync, GrabModeAsync);
|
GrabModeAsync, GrabModeAsync);
|
||||||
//capslock
|
//capslock
|
||||||
XGrabKey(display, key, mod|LockMask,
|
XGrabKey(m_display, key, mod|LockMask,
|
||||||
root, True,
|
root, True,
|
||||||
GrabModeAsync, GrabModeAsync);
|
GrabModeAsync, GrabModeAsync);
|
||||||
|
|
||||||
//capslock+numlock
|
//capslock+numlock
|
||||||
XGrabKey(display, key, mod|LockMask|Mod2Mask,
|
XGrabKey(m_display, key, mod|LockMask|Mod2Mask,
|
||||||
root, True,
|
root, True,
|
||||||
GrabModeAsync, GrabModeAsync);
|
GrabModeAsync, GrabModeAsync);
|
||||||
|
|
||||||
//capslock+scrolllock
|
//capslock+scrolllock
|
||||||
XGrabKey(display, key, mod|LockMask|Mod5Mask,
|
XGrabKey(m_display, key, mod|LockMask|Mod5Mask,
|
||||||
root, True,
|
root, True,
|
||||||
GrabModeAsync, GrabModeAsync);
|
GrabModeAsync, GrabModeAsync);
|
||||||
|
|
||||||
//capslock+numlock+scrolllock
|
//capslock+numlock+scrolllock
|
||||||
XGrabKey(display, key, mod|Mod2Mask|Mod5Mask|LockMask,
|
XGrabKey(m_display, key, mod|Mod2Mask|Mod5Mask|LockMask,
|
||||||
root, True,
|
root, True,
|
||||||
GrabModeAsync, GrabModeAsync);
|
GrabModeAsync, GrabModeAsync);
|
||||||
|
|
||||||
//numlock+scrollLock
|
//numlock+scrollLock
|
||||||
XGrabKey(display, key, mod|Mod2Mask|Mod5Mask,
|
XGrabKey(m_display, key, mod|Mod2Mask|Mod5Mask,
|
||||||
root, True,
|
root, True,
|
||||||
GrabModeAsync, GrabModeAsync);
|
GrabModeAsync, GrabModeAsync);
|
||||||
|
|
||||||
|
@ -363,13 +372,13 @@ void Keys::grabKey(unsigned int key, unsigned int mod) {
|
||||||
// else zero on failure.
|
// else zero on failure.
|
||||||
// TODO fix more masks
|
// TODO fix more masks
|
||||||
//----------------------------------------
|
//----------------------------------------
|
||||||
unsigned int Keys::getModifier(char *modstr) {
|
unsigned int Keys::getModifier(const char *modstr) {
|
||||||
if (!modstr)
|
if (!modstr)
|
||||||
return 0;
|
return 0;
|
||||||
struct t_modlist{
|
struct t_modlist{
|
||||||
char *string;
|
char *string;
|
||||||
unsigned int mask;
|
unsigned int mask;
|
||||||
bool operator == (char *modstr) {
|
bool operator == (const char *modstr) {
|
||||||
return (strcasecmp(string, modstr) == 0 && mask !=0);
|
return (strcasecmp(string, modstr) == 0 && mask !=0);
|
||||||
}
|
}
|
||||||
} modlist[] = {
|
} modlist[] = {
|
||||||
|
@ -395,12 +404,11 @@ unsigned int Keys::getModifier(char *modstr) {
|
||||||
// Returns keycode of keystr on success
|
// Returns keycode of keystr on success
|
||||||
// else it returns zero
|
// else it returns zero
|
||||||
//-----------------------------------
|
//-----------------------------------
|
||||||
unsigned int Keys::getKey(char *keystr) {
|
unsigned int Keys::getKey(const char *keystr) {
|
||||||
if (!keystr)
|
if (!keystr)
|
||||||
return 0;
|
return 0;
|
||||||
return XKeysymToKeycode(Fluxbox::instance()->getXDisplay(),
|
return XKeysymToKeycode(m_display,
|
||||||
XStringToKeysym
|
XStringToKeysym(keystr));
|
||||||
(keystr));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------- getAction -----------------
|
//--------- getAction -----------------
|
||||||
|
|
Loading…
Reference in a new issue