#include #include #include #include /* THIS CODE SUCKS. we need: - contexts that aren't compared as strings, - maybe a lookup "device,key,flags" -> actions? (lazy validation, on bindAction) - devices as ids - seperate native from python keys? (currently, if an action wasn't found, it's ignored.) Sorry. I spent 3 days on thinking how this could be made better, and it just DID NOT WORKED OUT. If you have a better idea, please tell me. */ DEFINE_REF(eActionMap); eActionMap *eActionMap::instance; eActionMap::eActionMap() { instance = this; } eActionMap::~eActionMap() { instance = 0; } RESULT eActionMap::getInstance(ePtr &ptr) { ptr = instance; if (!ptr) return -1; return 0; } #if 0 void eActionMap::getInstance(eActionMap **ptr) { *ptr = instance; } #endif void eActionMap::bindAction(const std::string &context, int priority, int id, eWidget *widget) { eActionBinding bnd; bnd.m_context = context; bnd.m_widget = widget; bnd.m_fnc = 0; bnd.m_id = id; m_bindings.insert(std::pair(priority, bnd)); } void eActionMap::bindAction(const std::string &context, int priority, PyObject *function) { eActionBinding bnd; bnd.m_context = context; bnd.m_widget = 0; Py_INCREF(function); bnd.m_fnc = function; m_bindings.insert(std::pair(priority, bnd)); } void eActionMap::unbindAction(eWidget *widget, int id) { for (std::multimap::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i) if ((i->second.m_widget == widget) && (i->second.m_id == id)) { m_bindings.erase(i); return; } } void eActionMap::unbindAction(const std::string &context, PyObject *function) { for (std::multimap::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i) { if (i->second.m_fnc && (PyObject_Compare(i->second.m_fnc, function) == 0)) { Py_DECREF(i->second.m_fnc); m_bindings.erase(i); return; } } eFatal("unbindAction with illegal python reference"); } void eActionMap::bindKey(const std::string &device, int key, int flags, const std::string &context, const std::string &action) { // first, search the actionlist table unsigned int i; for (i=0; i(context, bind)); return; } } // we didn't find the action, so it must be a pythonAction ePythonKeyBinding bind; bind.m_device = 0; bind.m_key = key; bind.m_flags = flags; bind.m_action = action; m_python_keys.insert(std::pair(context, bind)); } struct call_entry { PyObject *m_fnc, *m_arg; eWidget *m_widget; void *m_widget_arg; call_entry(PyObject *fnc, PyObject *arg): m_fnc(fnc), m_arg(arg), m_widget(0), m_widget_arg(0) { } call_entry(eWidget *widget, void *arg): m_fnc(0), m_arg(0), m_widget(widget), m_widget_arg(arg) { } }; void eActionMap::keyPressed(int device, int key, int flags) { std::list call_list; /* iterate active contexts. */ for (std::multimap::const_iterator c(m_bindings.begin()); c != m_bindings.end();) { std::multimap::const_iterator i = c; ++c; /* is this a native context? */ if (i->second.m_widget) { /* is this a named context, i.e. not the wildcard? */ if (i->second.m_context.size()) { std::multimap::const_iterator k = m_native_keys.lower_bound(i->second.m_context), e = m_native_keys.upper_bound(i->second.m_context); for (; k != e; ++k) { if ( // (k->second.m_device == m_device) && (k->second.m_key == key) && (k->second.m_flags & (1<second.m_widget, (void*)k->second.m_action)); } } else { /* wildcard - get any keys. */ if (i->second.m_widget->event(eWidget::evtKey, (void*)key, (void*)flags)) return; } } else if (i->second.m_fnc) { if (i->second.m_context.size()) { std::multimap::const_iterator k = m_python_keys.lower_bound(i->second.m_context), e = m_python_keys.upper_bound(i->second.m_context); for (; k != e;) { if ( // (k->second.m_device == m_device) && (k->second.m_key == key) && (k->second.m_flags & (1<first.c_str())); PyTuple_SET_ITEM(pArgs, 1, PyString_FromString(k->second.m_action.c_str())); ++k; Py_INCREF(i->second.m_fnc); call_list.push_back(call_entry(i->second.m_fnc, pArgs)); } else ++k; } } else { PyObject *pArgs = PyTuple_New(2); PyTuple_SET_ITEM(pArgs, 0, PyInt_FromLong(key)); PyTuple_SET_ITEM(pArgs, 1, PyInt_FromLong(flags)); Py_INCREF(i->second.m_fnc); call_list.push_back(call_entry(i->second.m_fnc, pArgs)); } } } int res = 0; /* we need to iterate over all to not loose a reference */ for (std::list::iterator i(call_list.begin()); i != call_list.end(); ++i) { if (i->m_fnc) { if (!res) res = ePython::call(i->m_fnc, i->m_arg); Py_DECREF(i->m_fnc); Py_DECREF(i->m_arg); } else if (i->m_widget) { if (!res) res = i->m_widget->event(eWidget::evtAction, 0, (void*)i->m_widget_arg); } } } eAutoInitPtr init_eActionMap(eAutoInitNumbers::actions, "eActionMap");