X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fpython.cc;h=dbe35f5d0f3a0159cdc0f14da9c28fef5dffd01e;hb=1161a90a70b21d3064a9dee62c72dd4be3025ada;hp=ed171f9cc4880659e2f3642dd310b606964751d4;hpb=c645416035d64cc959d1dd0e937b31b07489e54b;p=chaz%2Fopenbox diff --git a/src/python.cc b/src/python.cc index ed171f9c..dbe35f5d 100644 --- a/src/python.cc +++ b/src/python.cc @@ -1,6 +1,8 @@ // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*- #include "python.hh" +#include "openbox.hh" +#include "otk/display.hh" #include #include @@ -10,10 +12,12 @@ namespace ob { typedef std::vector FunctionList; static FunctionList callbacks[OBActions::NUM_ACTIONS]; +static FunctionList bindfuncs; bool python_register(int action, PyObject *callback) { - if (action < 0 || action >= OBActions::NUM_ACTIONS) { + if (action < 0 || action >= OBActions::NUM_ACTIONS || + action == OBActions::Action_KeyPress) { PyErr_SetString(PyExc_AssertionError, "Invalid action type."); return false; } @@ -34,7 +38,8 @@ bool python_register(int action, PyObject *callback) bool python_preregister(int action, PyObject *callback) { - if (action < 0 || action >= OBActions::NUM_ACTIONS) { + if (action < 0 || action >= OBActions::NUM_ACTIONS || + action == OBActions::Action_KeyPress) { PyErr_SetString(PyExc_AssertionError, "Invalid action type."); return false; } @@ -55,7 +60,8 @@ bool python_preregister(int action, PyObject *callback) bool python_unregister(int action, PyObject *callback) { - if (action < 0 || action >= OBActions::NUM_ACTIONS) { + if (action < 0 || action >= OBActions::NUM_ACTIONS || + action == OBActions::Action_KeyPress) { PyErr_SetString(PyExc_AssertionError, "Invalid action type."); return false; } @@ -125,4 +131,113 @@ void python_callback(OBActions::ActionType action, Window window, Py_DECREF(arglist); } + + + + + +bool python_bind(PyObject *keylist, PyObject *callback) +{ + if (!PyList_Check(keylist)) { + PyErr_SetString(PyExc_AssertionError, "Invalid keylist. Not a list."); + return false; + } + if (!PyCallable_Check(callback)) { + PyErr_SetString(PyExc_AssertionError, "Invalid callback function."); + return false; + } + + OBBindings::StringVect vectkeylist; + for (int i = 0, end = PyList_Size(keylist); i < end; ++i) { + PyObject *str = PyList_GetItem(keylist, i); + if (!PyString_Check(str)) { + PyErr_SetString(PyExc_AssertionError, + "Invalid keylist. It must contain only strings."); + return false; + } + vectkeylist.push_back(PyString_AsString(str)); + } + + // the id is what the binding class can call back with so it doesnt have to + // worry about the python function pointer + int id = bindfuncs.size(); + if (Openbox::instance->bindings()->add(vectkeylist, id)) { + Py_XINCREF(callback); // Add a reference to new callback + bindfuncs.push_back(callback); + return true; + } else { + PyErr_SetString(PyExc_AssertionError,"Unable to create binding. Invalid."); + return false; + } +} + +bool python_unbind(PyObject *keylist) +{ + if (!PyList_Check(keylist)) { + PyErr_SetString(PyExc_AssertionError, "Invalid keylist. Not a list."); + return false; + } + + OBBindings::StringVect vectkeylist; + for (int i = 0, end = PyList_Size(keylist); i < end; ++i) { + PyObject *str = PyList_GetItem(keylist, i); + if (!PyString_Check(str)) { + PyErr_SetString(PyExc_AssertionError, + "Invalid keylist. It must contain only strings."); + return false; + } + vectkeylist.push_back(PyString_AsString(str)); + } + + int id; + if ((id = + Openbox::instance->bindings()->remove(vectkeylist)) >= 0) { + assert(bindfuncs[id]); // shouldn't be able to remove it twice + Py_XDECREF(bindfuncs[id]); // Dispose of previous callback + // important note: we don't erase the item from the list cuz that would + // ruin all the id's that are in use. simply nullify it. + bindfuncs[id] = 0; + return true; + } + + return false; +} + +void python_set_reset_key(const std::string &key) +{ + Openbox::instance->bindings()->setResetKey(key); +} + +void python_unbind_all() +{ + Openbox::instance->bindings()->remove_all(); +} + + +void python_callback_binding(int id, Window window, unsigned int state, + unsigned int keybutton, Time time) +{ + if (!bindfuncs[id]) return; // the key was unbound + + PyObject *arglist; + PyObject *result; + + arglist = Py_BuildValue("lisl", window, state, + XKeysymToString( + XKeycodeToKeysym(otk::OBDisplay::display, + keybutton, 0)), + time); + + // call the callback + result = PyEval_CallObject(bindfuncs[id], arglist); + if (result) { + Py_DECREF(result); + } else { + // an exception occured in the script, display it + PyErr_Print(); + } + + Py_DECREF(arglist); +} + }