From a9a5f0d7510be5dbf8ae1db99e6c1c01035503d9 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Fri, 3 Jan 2003 21:48:11 +0000 Subject: [PATCH] put event bindings in OBBindings too --- src/actions.cc | 74 ++++++++++--------------------------------------- src/actions.hh | 10 ------- src/bindings.cc | 42 ++++++++++++++++++++++++++++ src/bindings.hh | 13 +++++++++ src/python.cc | 2 +- 5 files changed, 71 insertions(+), 70 deletions(-) diff --git a/src/actions.cc b/src/actions.cc index 00d4c1bf..fba1eaf1 100644 --- a/src/actions.cc +++ b/src/actions.cc @@ -23,9 +23,6 @@ OBActions::OBActions() { for (int i=0; ibindings()->fireEvent(data); + Py_DECREF((PyObject*)data); } @@ -163,11 +158,9 @@ void OBActions::leaveHandler(const XCrossingEvent &e) OtkEventHandler::leaveHandler(e); // run the LEAVE python hook - if (_callback[EventLeaveWindow]) { - EventData *data = new_event_data(e.window, EventLeaveWindow, e.state); - python_callback(_callback[EventLeaveWindow], (PyObject*)data); - Py_DECREF((PyObject*)data); - } + EventData *data = new_event_data(e.window, EventLeaveWindow, e.state); + Openbox::instance->bindings()->fireEvent(data); + Py_DECREF((PyObject*)data); } @@ -221,64 +214,27 @@ void OBActions::mapRequestHandler(const XMapRequestEvent &e) { OtkEventHandler::mapRequestHandler(e); - if (_callback[EventNewWindow]) { - EventData *data = new_event_data(e.window, EventNewWindow, 0); - python_callback(_callback[EventNewWindow], (PyObject*)data); - Py_DECREF((PyObject*)data); - } + EventData *data = new_event_data(e.window, EventNewWindow, 0); + Openbox::instance->bindings()->fireEvent(data); + Py_DECREF((PyObject*)data); } void OBActions::unmapHandler(const XUnmapEvent &e) { OtkEventHandler::unmapHandler(e); - if (_callback[EventCloseWindow]) { - EventData *data = new_event_data(e.window, EventCloseWindow, 0); - python_callback(_callback[EventCloseWindow], (PyObject*)data); - Py_DECREF((PyObject*)data); - } + EventData *data = new_event_data(e.window, EventCloseWindow, 0); + Openbox::instance->bindings()->fireEvent(data); + Py_DECREF((PyObject*)data); } void OBActions::destroyHandler(const XDestroyWindowEvent &e) { OtkEventHandler::destroyHandler(e); - if (_callback[EventCloseWindow]) { - EventData *data = new_event_data(e.window, EventCloseWindow, 0); - python_callback(_callback[EventCloseWindow], (PyObject*)data); - Py_DECREF((PyObject*)data); - } -} - -bool OBActions::bind(EventAction action, PyObject *func) -{ - if (action < 0 || action >= NUM_EVENTS) { - return false; - } - - Py_XDECREF(_callback[action]); - _callback[action] = func; - Py_INCREF(func); - return true; -} - -bool OBActions::unbind(EventAction action) -{ - if (action < 0 || action >= NUM_EVENTS) { - return false; - } - - Py_XDECREF(_callback[action]); - _callback[action] = 0; - return true; -} - -void OBActions::unbindAll() -{ - for (int i = 0; i < NUM_EVENTS; ++i) { - Py_XDECREF(_callback[i]); - _callback[i] = 0; - } + EventData *data = new_event_data(e.window, EventCloseWindow, 0); + Openbox::instance->bindings()->fireEvent(data); + Py_DECREF((PyObject*)data); } } diff --git a/src/actions.hh b/src/actions.hh index 970a18a4..1da6e278 100644 --- a/src/actions.hh +++ b/src/actions.hh @@ -60,8 +60,6 @@ private: void insertPress(const XButtonEvent &e); void removePress(const XButtonEvent &e); - PyObject *_callback[NUM_EVENTS]; - public: //! Constructs an OBActions object OBActions(); @@ -82,14 +80,6 @@ public: virtual void unmapHandler(const XUnmapEvent &e); virtual void destroyHandler(const XDestroyWindowEvent &e); - //! Bind a callback for an action - bool bind(EventAction action, PyObject *func); - - //! Unbind the callback function from an action - bool unbind(EventAction action); - - //! Remove all callback functions - void unbindAll(); }; } diff --git a/src/bindings.cc b/src/bindings.cc index 2b81381c..3df98da9 100644 --- a/src/bindings.cc +++ b/src/bindings.cc @@ -147,6 +147,9 @@ OBBindings::OBBindings() _timer.setTimeout(5000); // chains reset after 5 seconds setResetKey("C-g"); // set the default reset key + + for (int i = 0; i < NUM_EVENTS; ++i) + _events[i] = 0; } @@ -155,6 +158,7 @@ OBBindings::~OBBindings() grabKeys(false); removeAllKeys(); removeAllButtons(); + removeAllEvents(); } @@ -493,4 +497,42 @@ void OBBindings::fireButton(ButtonData *data) } } + +bool OBBindings::addEvent(EventAction action, PyObject *callback) +{ + if (action < 0 || action >= NUM_EVENTS) { + return false; + } + + Py_XDECREF(_events[action]); + _events[action] = callback; + Py_INCREF(callback); + return true; +} + +bool OBBindings::removeEvent(EventAction action) +{ + if (action < 0 || action >= NUM_EVENTS) { + return false; + } + + Py_XDECREF(_events[action]); + _events[action] = 0; + return true; +} + +void OBBindings::removeAllEvents() +{ + for (int i = 0; i < NUM_EVENTS; ++i) { + Py_XDECREF(_events[i]); + _events[i] = 0; + } +} + +void OBBindings::fireEvent(EventData *data) +{ + if (_events[data->action]) + python_callback(_events[data->action], (PyObject*)data); +} + } diff --git a/src/bindings.hh b/src/bindings.hh index 71df3011..8e91b5b6 100644 --- a/src/bindings.hh +++ b/src/bindings.hh @@ -84,6 +84,8 @@ private: void grabButton(bool grab, const Binding &b, MouseContext context, OBClient *client); + + PyObject *_events[NUM_EVENTS]; public: //! Initializes an OBBindings object @@ -127,6 +129,17 @@ public: void removeAllButtons(); void fireButton(ButtonData *data); + + //! Bind a callback for an event + bool addEvent(EventAction action, PyObject *callback); + + //! Unbind the callback function from an event + bool removeEvent(EventAction action); + + //! Remove all callback functions + void removeAllEvents(); + + void fireEvent(EventData *data); }; } diff --git a/src/python.cc b/src/python.cc index 4bddce8f..e5224921 100644 --- a/src/python.cc +++ b/src/python.cc @@ -455,7 +455,7 @@ PyObject *ebind(ob::EventAction action, PyObject *func) return NULL; } - if (!ob::Openbox::instance->actions()->bind(action, func)) { + if (!ob::Openbox::instance->bindings()->addEvent(action, func)) { PyErr_SetString(PyExc_RuntimeError,"Unable to add binding."); return NULL; } -- 2.45.2