]> Dogcows Code - chaz/openbox/blobdiff - src/python.cc
allow to bind multiple functions to everything
[chaz/openbox] / src / python.cc
index 7ff12452d4efda7a6817257ebff5eb7810652fa3..c463fdc824d0edca37e2465a09f9cdc31757fa64 100644 (file)
@@ -171,6 +171,28 @@ static PyMethodDef ButtonData_methods[] = {
   {NULL, NULL, 0, NULL}
 };
 
+PyObject *EventData_action(EventData *self, PyObject *args)
+{
+  if(!PyArg_ParseTuple(args,":action")) return NULL;
+  return PyLong_FromLong((int)self->action);
+}
+
+PyObject *EventData_modifiers(EventData *self, PyObject *args)
+{
+  if(!PyArg_ParseTuple(args,":modifiers")) return NULL;
+  return PyLong_FromUnsignedLong(self->state);
+}
+
+static PyMethodDef EventData_methods[] = {
+  {"window", (PyCFunction)MotionData_window, METH_VARARGS,
+   "Return the client window id."},
+  {"action", (PyCFunction)EventData_action, METH_VARARGS,
+   "Return the action being executed."},
+  {"modifiers", (PyCFunction)EventData_modifiers, METH_VARARGS,
+   "Return the modifier keys state."},
+  {NULL, NULL, 0, NULL}
+};
+
 PyObject *KeyData_key(KeyData *self, PyObject *args)
 {
   if(!PyArg_ParseTuple(args,":key")) return NULL;
@@ -201,6 +223,11 @@ static PyObject *ButtonDataGetAttr(PyObject *obj, char *name)
   return Py_FindMethod(ButtonData_methods, obj, name);
 }
 
+static PyObject *EventDataGetAttr(PyObject *obj, char *name)
+{
+  return Py_FindMethod(EventData_methods, obj, name);
+}
+
 static PyObject *KeyDataGetAttr(PyObject *obj, char *name)
 {
   return Py_FindMethod(KeyData_methods, obj, name);
@@ -230,6 +257,18 @@ static PyTypeObject ButtonData_Type = {
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
 };
 
+static PyTypeObject EventData_Type = {
+  PyObject_HEAD_INIT(NULL)
+  0,
+  "EventData",
+  sizeof(EventData),
+  0,
+  dealloc,
+  0,
+  (getattrfunc)EventDataGetAttr,
+  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
+};
+
 static PyTypeObject KeyData_Type = {
   PyObject_HEAD_INIT(NULL)
   0,
@@ -279,6 +318,16 @@ ButtonData *new_button_data(Window window, Time time, unsigned int state,
   return data;
 }
 
+EventData *new_event_data(Window window, EventAction action,
+                          unsigned int state)
+{
+  EventData *data = PyObject_New(EventData, &EventData_Type);
+  data->window = window;
+  data->action = action;
+  data->state  = state;
+  return data;
+}
+
 KeyData *new_key_data(Window window, Time time, unsigned int state,
                        unsigned int key)
 {
@@ -303,6 +352,11 @@ void python_init(char *argv0)
   PyRun_SimpleString("from _otk import *; from _openbox import *;");
   PyRun_SimpleString("openbox = Openbox_instance()");
 
+  /* XXX
+     sys.path.append('stuff')
+     install the .py wrappers, and include their path with this, then import em
+  */
+  
   // set up access to the python global variables
   PyObject *obmodule = PyImport_AddModule("__main__");
   obdict = PyModule_GetDict(obmodule);
@@ -348,6 +402,15 @@ void python_callback(PyObject *func, PyObject *data)
   Py_DECREF(arglist);
 }
 
+bool python_get_long(const char *name, long *value)
+{
+  PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
+  if (!(val && PyLong_Check(val))) return false;
+  
+  *value = PyLong_AsLong(val);
+  return true;
+}
+
 bool python_get_string(const char *name, std::string *value)
 {
   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
@@ -374,65 +437,37 @@ bool python_get_stringlist(const char *name, std::vector<std::string> *value)
 // Stuff for calling from Python scripts //
 // ************************************* //
 
-/*
-PyObject * python_register(int action, PyObject *func, bool infront = false)
-{
-  if (!PyCallable_Check(func)) {
-    PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
-    return NULL;
-  }
-
-  if (!ob::Openbox::instance->actions()->registerCallback(
-        (ob::OBActions::ActionType)action, func, infront)) {
-    PyErr_SetString(PyExc_RuntimeError, "Unable to register action callback.");
-    return NULL;
-  }
-  Py_INCREF(Py_None); return Py_None;
-}
-
-PyObject *unregister(int action, PyObject *func)
+PyObject *mbind(const std::string &button, ob::MouseContext context,
+                ob::MouseAction action, PyObject *func)
 {
   if (!PyCallable_Check(func)) {
     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
     return NULL;
   }
-
-  if (!ob::Openbox::instance->actions()->unregisterCallback(
-        (ob::OBActions::ActionType)action, func)) {
-    PyErr_SetString(PyExc_RuntimeError, "Unable to unregister action callback.");
+  
+  if (!ob::Openbox::instance->bindings()->addButton(button, context,
+                                                    action, func)) {
+    PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
     return NULL;
   }
   Py_INCREF(Py_None); return Py_None;
 }
 
-PyObject *unregister_all(int action)
-{
-  if (!ob::Openbox::instance->actions()->unregisterAllCallbacks(
-        (ob::OBActions::ActionType)action)) {
-    PyErr_SetString(PyExc_RuntimeError,
-                    "Unable to unregister action callbacks.");
-    return NULL;
-  }
-  Py_INCREF(Py_None); return Py_None;
-}
-*/
-PyObject * mbind(const std::string &button, ob::MouseContext context,
-                 ob::MouseAction action, PyObject *func)
+PyObject *ebind(ob::EventAction action, PyObject *func)
 {
   if (!PyCallable_Check(func)) {
     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
     return NULL;
   }
   
-  if (!ob::Openbox::instance->bindings()->addButton(button, context,
-                                                    action, func)) {
+  if (!ob::Openbox::instance->bindings()->addEvent(action, func)) {
     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
     return NULL;
   }
   Py_INCREF(Py_None); return Py_None;
 }
 
-PyObject * kbind(PyObject *keylist, ob::KeyContext context, PyObject *func)
+PyObject *kbind(PyObject *keylist, ob::KeyContext context, PyObject *func)
 {
   if (!PyCallable_Check(func)) {
     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
@@ -454,20 +489,24 @@ PyObject * kbind(PyObject *keylist, ob::KeyContext context, PyObject *func)
     vectkeylist.push_back(PyString_AsString(str));
   }
 
-  if (!ob::Openbox::instance->bindings()->add(vectkeylist, func)) {
+  if (!ob::Openbox::instance->bindings()->addKey(vectkeylist, func)) {
     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
     return NULL;
   }
   Py_INCREF(Py_None); return Py_None;
 }
 
-PyObject * kunbind(PyObject *keylist)
+PyObject *kunbind(PyObject *keylist, PyObject *func)
 {
   if (!PyList_Check(keylist)) {
     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
     return NULL;
   }
-
+  if (!PyCallable_Check(func)) {
+    PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
+    return NULL;
+  }
+  
   ob::OBBindings::StringVect vectkeylist;
   for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
     PyObject *str = PyList_GetItem(keylist, i);
@@ -479,13 +518,16 @@ PyObject * kunbind(PyObject *keylist)
     vectkeylist.push_back(PyString_AsString(str));
   }
 
-  ob::Openbox::instance->bindings()->remove(vectkeylist);
+  if (!ob::Openbox::instance->bindings()->removeKey(vectkeylist, func)) {
+      PyErr_SetString(PyExc_RuntimeError, "Could not remove callback.");
+      return NULL;
+  }
   Py_INCREF(Py_None); return Py_None;
 }
 
 void kunbind_all()
 {
-  ob::Openbox::instance->bindings()->removeAll();
+  ob::Openbox::instance->bindings()->removeAllKeys();
 }
 
 void set_reset_key(const std::string &key)
This page took 0.025217 seconds and 4 git commands to generate.