X-Git-Url: https://git.brokenzipper.com/gitweb?a=blobdiff_plain;f=src%2Fpython.cc;h=21158dbea24166ce7d6b485eefd6eda83e00c01b;hb=098fa0ce20f440da3140adad2cfe8ae9a6854d46;hp=ed71463c8489f1b68ddd9aee5dc69aa1481a94c0;hpb=d4853f108c1d22c344c1cb9a8a8b7cdc46648983;p=chaz%2Fopenbox diff --git a/src/python.cc b/src/python.cc index ed71463c..21158dbe 100644 --- a/src/python.cc +++ b/src/python.cc @@ -2,6 +2,7 @@ #include "python.hh" #include "openbox.hh" +#include "otk/display.hh" #include #include @@ -11,12 +12,12 @@ namespace ob { typedef std::vector FunctionList; static FunctionList callbacks[OBActions::NUM_ACTIONS]; -static FunctionList keyfuncs; -static FunctionList mousefuncs; +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; } @@ -37,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; } @@ -58,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; } @@ -157,10 +160,10 @@ bool python_bind_key(PyObject *keylist, PyObject *callback) // the id is what the binding class can call back with so it doesnt have to // worry about the python function pointer - int id = keyfuncs.size(); + int id = bindfuncs.size(); if (Openbox::instance->bindings()->add_key(vectkeylist, id)) { Py_XINCREF(callback); // Add a reference to new callback - keyfuncs.push_back(callback); + bindfuncs.push_back(callback); return true; } else { PyErr_SetString(PyExc_AssertionError,"Unable to create binding. Invalid."); @@ -189,17 +192,22 @@ bool python_unbind_key(PyObject *keylist) int id; if ((id = Openbox::instance->bindings()->remove_key(vectkeylist)) >= 0) { - assert(keyfuncs[id]); // shouldn't be able to remove it twice - Py_XDECREF(keyfuncs[id]); // Dispose of previous callback + 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. - keyfuncs[id] = 0; + bindfuncs[id] = 0; return true; } return false; } +void python_set_reset_key(const std::string &key) +{ + Openbox::instance->bindings()->setResetKey(key); +} + bool python_bind_mouse(const std::string &button, PyObject *callback) { if (!PyCallable_Check(callback)) { @@ -209,10 +217,10 @@ bool python_bind_mouse(const std::string &button, PyObject *callback) // the id is what the binding class can call back with so it doesnt have to // worry about the python function pointer - int id = mousefuncs.size(); + int id = bindfuncs.size(); if (Openbox::instance->bindings()->add_mouse(button, id)) { Py_XINCREF(callback); // Add a reference to new callback - mousefuncs.push_back(callback); + bindfuncs.push_back(callback); return true; } else { PyErr_SetString(PyExc_AssertionError,"Unable to create binding. Invalid."); @@ -225,22 +233,50 @@ bool python_unbind_mouse(const std::string &button) int id; if ((id = Openbox::instance->bindings()->remove_mouse(button)) >= 0) { - assert(mousefuncs[id]); // shouldn't be able to remove it twice - Py_XDECREF(mousefuncs[id]); // Dispose of previous callback + 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. - mousefuncs[id] = 0; + bindfuncs[id] = 0; return true; } return false; } -bool python_unbind_all() +void python_unbind_all() { Openbox::instance->bindings()->remove_all(); - return true; } +void python_callback_binding(int id, OBActions::ActionType action, + Window window, unsigned int state, + unsigned int keybutton, Time time) +{ + assert(action >= 0 && action < OBActions::NUM_ACTIONS); + + if (!bindfuncs[id]) return; // the key was unbound + + PyObject *arglist; + PyObject *result; + + arglist = Py_BuildValue("ilisl", action, 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); +} + }