openbox/src/bindings.cc

296 lines
6.1 KiB
C++
Raw Normal View History

2002-12-30 06:31:45 +00:00
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
#ifdef HAVE_CONFIG_H
# include "../config.h"
#endif
#include "bindings.hh"
#include "otk/display.hh"
extern "C" {
#include <X11/Xlib.h>
#include "gettext.h"
#define _(str) gettext(str)
2002-12-30 06:31:45 +00:00
}
namespace ob {
#include <stdio.h>
static void print_branch(BindingTree *first, std::string str)
{
BindingTree *p = first;
while (p) {
if (p->first_child)
print_branch(p->first_child, str + " " + p->text);
2002-12-30 06:46:19 +00:00
if (!p->chain)
printf("%d%s\n", p->id, (str + " " + p->text).c_str());
2002-12-30 08:40:38 +00:00
p = p->next_sibling;
2002-12-30 06:31:45 +00:00
}
}
void OBBindings::display()
{
if (_tree.first_child)
print_branch(_tree.first_child, "");
2002-12-30 06:31:45 +00:00
}
bool OBBindings::translate(const std::string &str, Binding &b)
2002-12-30 06:31:45 +00:00
{
2002-12-30 07:33:13 +00:00
// parse out the base key name
2002-12-30 06:44:14 +00:00
std::string::size_type keybegin = str.find_last_of('-');
2002-12-30 07:33:13 +00:00
keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
std::string key(str, keybegin);
2002-12-30 06:44:14 +00:00
2002-12-30 07:33:13 +00:00
// parse out the requested modifier keys
2002-12-30 08:41:42 +00:00
unsigned int mods = 0;
2002-12-30 07:33:13 +00:00
std::string::size_type begin = 0, end;
while (begin != keybegin) {
end = str.find_first_of('-', begin);
std::string mod(str, begin, end-begin);
if (mod == "C") { // control
mods |= ControlMask;
} else if (mod == "S") { // shift
mods |= ShiftMask;
} else if (mod == "A" || // alt/mod1
mod == "M" ||
mod == "M1" ||
mod == "Mod1") {
mods |= Mod1Mask;
} else if (mod == "M2" || // mod2
mod == "Mod2") {
mods |= Mod2Mask;
} else if (mod == "M3" || // mod3
mod == "Mod3") {
mods |= Mod3Mask;
} else if (mod == "W" || // windows/mod4
mod == "M4" ||
mod == "Mod4") {
mods |= Mod4Mask;
} else if (mod == "M5" || // mod5
mod == "Mod5") {
mods |= Mod5Mask;
2002-12-30 07:36:06 +00:00
} else { // invalid
printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str());
2002-12-30 07:36:06 +00:00
return false;
2002-12-30 07:33:13 +00:00
}
begin = end + 1;
}
2002-12-30 06:44:14 +00:00
KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
2002-12-30 06:31:45 +00:00
if (sym == NoSymbol) return false;
2002-12-30 07:33:13 +00:00
b.modifiers = mods;
2002-12-30 06:31:45 +00:00
b.key = XKeysymToKeycode(otk::OBDisplay::display, sym);
return b.key != 0;
}
BindingTree *OBBindings::buildtree(const StringVect &keylist, int id)
2002-12-30 06:31:45 +00:00
{
if (keylist.empty()) return 0; // nothing in the list.. return 0
BindingTree *ret = new BindingTree(id), *p = 0;
StringVect::const_iterator it, end = keylist.end();
2002-12-30 06:31:45 +00:00
for (it = keylist.begin(); it != end; ++it) {
if (p)
p = p->first_child = new BindingTree(id);
else
p = ret; // the first node
if (!translate(*it, p->binding))
break;
p->text = *it;
}
if (it != end) {
// build failed.. clean up and return 0
p = ret;
while (p->first_child) {
BindingTree *c = p->first_child;
delete p;
p = c;
}
delete p;
return 0;
} else {
// set the proper chain status on the last node
p->chain = false;
}
// successfully built a tree
return ret;
}
static void destroytree(BindingTree *tree)
{
while (tree) {
BindingTree *c = tree->first_child;
delete tree;
tree = c;
}
}
OBBindings::OBBindings()
{
}
OBBindings::~OBBindings()
{
remove_all();
}
2002-12-30 07:33:13 +00:00
void OBBindings::assimilate(BindingTree *node)
2002-12-30 06:31:45 +00:00
{
BindingTree *a, *b, *tmp, *last;
2002-12-30 06:31:45 +00:00
2002-12-30 07:33:56 +00:00
if (!_tree.first_child) {
2002-12-30 06:31:45 +00:00
// there are no nodes at this level yet
2002-12-30 07:33:56 +00:00
_tree.first_child = node;
return;
2002-12-30 06:31:45 +00:00
} else {
2002-12-30 07:33:56 +00:00
a = _tree.first_child;
last = a;
b = node;
2002-12-30 08:40:38 +00:00
while (a) {
last = a;
if (a->binding != b->binding) {
a = a->next_sibling;
} else {
2002-12-30 07:33:13 +00:00
tmp = b;
b = b->first_child;
2002-12-30 07:33:13 +00:00
delete tmp;
a = a->first_child;
2002-12-30 06:31:45 +00:00
}
}
2002-12-30 08:40:38 +00:00
if (last->binding != b->binding)
last->next_sibling = b;
else
last->first_child = b->first_child;
delete b;
2002-12-30 06:31:45 +00:00
}
}
int OBBindings::find(BindingTree *search) {
2002-12-30 06:31:45 +00:00
BindingTree *a, *b;
2002-12-30 06:57:10 +00:00
a = _tree.first_child;
2002-12-30 06:31:45 +00:00
b = search;
while (a && b) {
if (a->binding != b->binding) {
a = a->next_sibling;
} else {
if (a->chain == b->chain) {
if (!a->chain)
return a->id; // found it! (return the actual id, not the search's)
} else
return -2; // the chain status' don't match (conflict!)
b = b->first_child;
a = a->first_child;
}
}
return -1; // it just isn't in here
}
/*
static int find(BindingTree *parent, BindingTree *node) {
BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
if (!parent->first_child)
return -1;
p = parent->first_child;
while (p) {
if (node->binding == p->binding) {
if (node->chain == p->chain) {
if (!node->chain) {
return p->id; // found it! (return the actual id, not the search's)
} else {
break; // go on to the next child in the chain
}
} else {
return -2; // the chain status' don't match (conflict!)
}
}
p = p->next_sibling;
}
if (!p) return -1; // doesn't exist
if (node->chain) {
assert(node->first_child);
return find(p, node->first_child);
} else
return -1; // it just isnt in here
}
*/
bool OBBindings::add(const StringVect &keylist, int id)
{
BindingTree *tree;
if (!(tree = buildtree(keylist, id)))
return false; // invalid binding requested
2002-12-30 06:57:10 +00:00
if (find(tree) < -1) {
2002-12-30 06:31:45 +00:00
// conflicts with another binding
destroytree(tree);
return false;
}
// assimilate this built tree into the main tree
2002-12-30 07:33:13 +00:00
assimilate(tree); // assimilation destroys/uses the tree
2002-12-30 06:31:45 +00:00
return true;
}
int OBBindings::find(const StringVect &keylist)
{
BindingTree *tree;
bool ret;
if (!(tree = buildtree(keylist, 0)))
return false; // invalid binding requested
2002-12-30 06:57:10 +00:00
ret = find(tree) >= 0;
2002-12-30 06:31:45 +00:00
destroytree(tree);
return ret;
}
int OBBindings::remove(const StringVect &keylist)
{
(void)keylist;
assert(false); // XXX: function not implemented yet
}
static void remove_branch(BindingTree *first)
{
BindingTree *p = first;
while (p) {
if (p->first_child)
remove_branch(p->first_child);
BindingTree *s = p->next_sibling;
delete p;
p = s;
}
}
void OBBindings::remove_all()
{
if (_tree.first_child)
remove_branch(_tree.first_child);
2002-12-30 06:31:45 +00:00
}
}