openbox/src/bindings.cc

478 lines
12 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 "screen.hh"
#include "openbox.hh"
#include "client.hh"
#include "frame.hh"
#include "python.hh"
2002-12-30 06:31:45 +00:00
#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 {
static bool buttonvalue(const std::string &button, unsigned int *val)
{
if (button == "1" || button == "Button1") {
*val |= Button1;
} else if (button == "2" || button == "Button2") {
*val |= Button2;
} else if (button == "3" || button == "Button3") {
*val |= Button3;
} else if (button == "4" || button == "Button4") {
*val |= Button4;
} else if (button == "5" || button == "Button5") {
*val |= Button5;
} else
return false;
return true;
}
2002-12-30 08:59:46 +00:00
static bool modvalue(const std::string &mod, unsigned int *val)
{
if (mod == "C") { // control
*val |= ControlMask;
} else if (mod == "S") { // shift
*val |= ShiftMask;
} else if (mod == "A" || // alt/mod1
mod == "M" ||
mod == "Mod1" ||
mod == "M1") {
2002-12-30 08:59:46 +00:00
*val |= Mod1Mask;
} else if (mod == "Mod2" || // mod2
mod == "M2") {
2002-12-30 08:59:46 +00:00
*val |= Mod2Mask;
} else if (mod == "Mod3" || // mod3
mod == "M3") {
2002-12-30 08:59:46 +00:00
*val |= Mod3Mask;
} else if (mod == "W" || // windows/mod4
mod == "Mod4" ||
mod == "M4") {
2002-12-30 08:59:46 +00:00
*val |= Mod4Mask;
} else if (mod == "Mod5" || // mod5
mod == "M5") {
2002-12-30 08:59:46 +00:00
*val |= Mod5Mask;
} else { // invalid
return false;
}
return true;
}
2002-12-30 06:31:45 +00:00
2003-01-02 22:58:32 +00:00
bool OBBindings::translate(const std::string &str, Binding &b,bool askey) const
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:59:46 +00:00
unsigned int modval = 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);
2002-12-30 08:59:46 +00:00
if (!modvalue(mod, &modval)) {
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
}
2002-12-30 08:59:46 +00:00
2002-12-30 07:33:13 +00:00
begin = end + 1;
}
2002-12-30 08:59:46 +00:00
// set the binding
b.modifiers = modval;
2003-01-02 22:58:32 +00:00
if (askey) {
KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
if (sym == NoSymbol) {
printf(_("Invalid Key name in key binding: %s\n"), key.c_str());
return false;
}
if (!(b.key = XKeysymToKeycode(otk::OBDisplay::display, sym)))
printf(_("No valid keycode for Key in key binding: %s\n"), key.c_str());
return b.key != 0;
} else {
return buttonvalue(key, &b.key);
}
2002-12-30 06:31:45 +00:00
}
static void destroytree(KeyBindingTree *tree)
2002-12-30 08:52:46 +00:00
{
while (tree) {
KeyBindingTree *c = tree->first_child;
2002-12-30 08:52:46 +00:00
delete tree;
tree = c;
}
}
KeyBindingTree *OBBindings::buildtree(const StringVect &keylist,
PyObject *callback) const
2002-12-30 06:31:45 +00:00
{
if (keylist.empty()) return 0; // nothing in the list.. return 0
KeyBindingTree *ret = 0, *p;
2002-12-30 06:31:45 +00:00
2002-12-30 08:52:46 +00:00
StringVect::const_reverse_iterator it, end = keylist.rend();
for (it = keylist.rbegin(); it != end; ++it) {
2002-12-30 06:31:45 +00:00
p = ret;
ret = new KeyBindingTree(callback);
2002-12-30 21:06:08 +00:00
if (!p) ret->chain = false; // only the first built node
2002-12-30 08:52:46 +00:00
ret->first_child = p;
2002-12-31 09:17:16 +00:00
if (!translate(*it, ret->binding)) {
2002-12-30 08:52:46 +00:00
destroytree(ret);
ret = 0;
break;
2002-12-30 06:31:45 +00:00
}
}
return ret;
}
OBBindings::OBBindings()
: _curpos(&_keytree),
_resetkey(0,0),
_timer(Openbox::instance->timerManager(),
(otk::OBTimeoutHandler)reset, this)
2002-12-30 06:31:45 +00:00
{
_timer.setTimeout(5000); // chains reset after 5 seconds
setResetKey("C-g"); // set the default reset key
2002-12-30 06:31:45 +00:00
}
OBBindings::~OBBindings()
{
grabKeys(false);
2003-01-03 07:03:59 +00:00
removeAllKeys();
removeAllButtons();
2002-12-30 06:31:45 +00:00
}
void OBBindings::assimilate(KeyBindingTree *node)
2002-12-30 06:31:45 +00:00
{
KeyBindingTree *a, *b, *tmp, *last;
2002-12-30 06:31:45 +00:00
if (!_keytree.first_child) {
2002-12-30 06:31:45 +00:00
// there are no nodes at this level yet
_keytree.first_child = node;
2002-12-30 06:31:45 +00:00
} else {
a = _keytree.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;
2002-12-30 21:10:13 +00:00
else {
2002-12-30 08:40:38 +00:00
last->first_child = b->first_child;
2002-12-30 21:10:13 +00:00
delete b;
}
2002-12-30 06:31:45 +00:00
}
}
PyObject *OBBindings::find(KeyBindingTree *search, bool *conflict) const {
*conflict = false;
KeyBindingTree *a, *b;
a = _keytree.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) {
2002-12-30 21:04:09 +00:00
if (!a->chain) {
// found it! (return the actual id, not the search's)
return a->callback;
2002-12-30 21:04:09 +00:00
}
} else {
*conflict = true;
return 0; // the chain status' don't match (conflict!)
2002-12-30 21:04:09 +00:00
}
2002-12-30 06:31:45 +00:00
b = b->first_child;
a = a->first_child;
}
}
return 0; // it just isn't in here
2002-12-30 06:31:45 +00:00
}
2003-01-03 07:03:59 +00:00
bool OBBindings::addKey(const StringVect &keylist, PyObject *callback)
2002-12-30 06:31:45 +00:00
{
KeyBindingTree *tree;
bool conflict;
2002-12-30 06:31:45 +00:00
if (!(tree = buildtree(keylist, callback)))
2002-12-30 06:31:45 +00:00
return false; // invalid binding requested
if (find(tree, &conflict) || conflict) {
2002-12-30 06:31:45 +00:00
// conflicts with another binding
destroytree(tree);
return false;
}
grabKeys(false);
2002-12-30 06:31:45 +00:00
// 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 20:55:33 +00:00
Py_INCREF(callback);
grabKeys(true);
2002-12-30 06:31:45 +00:00
return true;
}
2003-01-03 07:03:59 +00:00
bool OBBindings::removeKey(const StringVect &keylist)
2002-12-30 06:31:45 +00:00
{
assert(false); // XXX: function not implemented yet
KeyBindingTree *tree;
bool conflict;
2002-12-30 06:31:45 +00:00
if (!(tree = buildtree(keylist, 0)))
return false; // invalid binding requested
PyObject *func = find(tree, &conflict);
if (func) {
grabKeys(false);
_curpos = &_keytree;
// XXX do shit here ...
Py_DECREF(func);
grabKeys(true);
return true;
}
return false;
2002-12-30 06:31:45 +00:00
}
void OBBindings::setResetKey(const std::string &key)
{
Binding b(0, 0);
2002-12-31 09:17:16 +00:00
if (translate(key, b)) {
grabKeys(false);
_resetkey.key = b.key;
_resetkey.modifiers = b.modifiers;
grabKeys(true);
}
}
static void remove_branch(KeyBindingTree *first)
2002-12-30 06:31:45 +00:00
{
KeyBindingTree *p = first;
2002-12-30 06:31:45 +00:00
while (p) {
if (p->first_child)
remove_branch(p->first_child);
KeyBindingTree *s = p->next_sibling;
Py_XDECREF(p->callback);
2002-12-30 06:31:45 +00:00
delete p;
p = s;
}
}
2003-01-03 07:03:59 +00:00
void OBBindings::removeAllKeys()
2002-12-30 06:31:45 +00:00
{
2003-01-03 07:03:59 +00:00
grabKeys(false);
if (_keytree.first_child) {
remove_branch(_keytree.first_child);
_keytree.first_child = 0;
}
2003-01-03 07:03:59 +00:00
grabKeys(true);
}
void OBBindings::grabKeys(bool grab)
{
for (int i = 0; i < Openbox::instance->screenCount(); ++i) {
Window root = otk::OBDisplay::screenInfo(i)->rootWindow();
KeyBindingTree *p = _curpos->first_child;
while (p) {
if (grab) {
otk::OBDisplay::grabKey(p->binding.key, p->binding.modifiers,
root, false, GrabModeAsync, GrabModeAsync,
false);
}
else
otk::OBDisplay::ungrabKey(p->binding.key, p->binding.modifiers,
root);
p = p->next_sibling;
}
if (grab)
otk::OBDisplay::grabKey(_resetkey.key, _resetkey.modifiers,
root, true, GrabModeAsync, GrabModeAsync,
false);
else
otk::OBDisplay::ungrabKey(_resetkey.key, _resetkey.modifiers,
root);
}
}
2003-01-03 07:03:59 +00:00
void OBBindings::fireKey(unsigned int modifiers, unsigned int key, Time time)
{
if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
2003-01-03 18:51:00 +00:00
resetChains(this);
} else {
KeyBindingTree *p = _curpos->first_child;
while (p) {
if (p->binding.key == key && p->binding.modifiers == modifiers) {
if (p->chain) {
_timer.start(); // start/restart the timer
grabKeys(false);
_curpos = p;
grabKeys(true);
} else {
Window win = None;
OBClient *c = Openbox::instance->focusedClient();
if (c) win = c->window();
2003-01-03 05:26:04 +00:00
KeyData *data = new_key_data(win, time, modifiers, key);
python_callback(p->callback, (PyObject*)data);
Py_DECREF((PyObject*)data);
2003-01-03 18:51:00 +00:00
resetChains(this);
}
break;
}
p = p->next_sibling;
}
}
}
2003-01-03 18:51:00 +00:00
void OBBindings::resetChains(OBBindings *self)
{
self->_timer.stop();
self->grabKeys(false);
self->_curpos = &self->_keytree;
self->grabKeys(true);
}
bool OBBindings::addButton(const std::string &but, MouseContext context,
MouseAction action, PyObject *callback)
{
assert(context >= 0 && context < NUM_MOUSE_CONTEXT);
Binding b(0,0);
if (!translate(but, b, false))
return false;
ButtonBindingList::iterator it, end = _buttons[context].end();
// look for a duplicate binding
for (it = _buttons[context].begin(); it != end; ++it)
if ((*it)->binding.key == b.key &&
(*it)->binding.modifiers == b.modifiers) {
if ((*it)->callback[action] == callback)
return true; // already bound
break;
}
ButtonBinding *bind;
// the binding didnt exist yet, add it
if (it == end) {
bind = new ButtonBinding();
bind->binding.key = b.key;
bind->binding.modifiers = b.modifiers;
_buttons[context].push_back(bind);
// XXX GRAB the new button everywhere!
} else
bind = *it;
Py_XDECREF(bind->callback[action]); // if it was already bound, unbind it
bind->callback[action] = callback;
Py_INCREF(callback);
return true;
}
2003-01-03 07:03:59 +00:00
void OBBindings::removeAllButtons()
{
// XXX: UNGRAB shits
for (int i = i; i < NUM_MOUSE_CONTEXT; ++i) {
ButtonBindingList::iterator it, end = _buttons[i].end();
for (it = _buttons[i].begin(); it != end; ++it)
for (int a = 0; a < NUM_MOUSE_ACTION; ++a) {
Py_XDECREF((*it)->callback[a]);
(*it)->callback[a] = 0;
}
}
}
void OBBindings::grabButtons(bool grab, OBClient *client)
{
for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
Window win;
int mode = GrabModeAsync;
switch (i) {
case MC_Frame:
win = client->frame->window();
break;
case MC_Window:
win = client->frame->plate();
2003-01-03 18:51:00 +00:00
mode = GrabModeSync; // this is handled in fireButton
break;
default:
2003-01-03 06:07:53 +00:00
// any other elements already get button events, don't grab on them
continue;
}
ButtonBindingList::iterator it, end = _buttons[i].end();
for (it = _buttons[i].begin(); it != end; ++it)
if (grab)
otk::OBDisplay::grabButton((*it)->binding.key,
(*it)->binding.modifiers, win, false,
ButtonPressMask | ButtonMotionMask |
ButtonReleaseMask, mode, GrabModeAsync,
None, None, false);
else
otk::OBDisplay::ungrabButton((*it)->binding.key,
(*it)->binding.modifiers, win);
}
}
2003-01-03 07:03:59 +00:00
void OBBindings::fireButton(ButtonData *data)
{
2003-01-03 05:26:04 +00:00
printf("but.mods %d.%d\n", data->button, data->state);
if (data->context == MC_Window) {
// these are grabbed in Sync mode to allow the press to be normal to the
// client
XAllowEvents(otk::OBDisplay::display, ReplayPointer, data->time);
}
2003-01-03 05:26:04 +00:00
ButtonBindingList::iterator it, end = _buttons[data->context].end();
for (it = _buttons[data->context].begin(); it != end; ++it)
if ((*it)->binding.key == data->button &&
(*it)->binding.modifiers == data->state) {
if ((*it)->callback[data->action])
python_callback((*it)->callback[data->action], (PyObject*)data);
}
}
2002-12-30 06:31:45 +00:00
}