1 #include <lib/actions/action.h>
2 #include <lib/base/init.h>
3 #include <lib/base/init_num.h>
4 #include <lib/actions/actionids.h>
11 - contexts that aren't compared as strings,
12 - maybe a lookup "device,key,flags" -> actions? (lazy validation, on bindAction)
14 - seperate native from python keys? (currently, if an action wasn't found, it's ignored.)
16 Sorry. I spent 3 days on thinking how this could be made better, and it just DID NOT WORKED OUT.
18 If you have a better idea, please tell me.
22 DEFINE_REF(eActionMap);
24 eActionMap *eActionMap::instance;
26 eActionMap::eActionMap()
31 eActionMap::~eActionMap()
36 RESULT eActionMap::getInstance(ePtr<eActionMap> &ptr)
45 void eActionMap::getInstance(eActionMap **ptr)
51 void eActionMap::bindAction(const std::string &context, int priority, int id, eWidget *widget)
55 bnd.m_context = context;
56 bnd.m_widget = widget;
59 m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
62 void eActionMap::bindAction(const std::string &context, int priority, PyObject *function)
66 bnd.m_context = context;
70 m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
73 void eActionMap::unbindAction(eWidget *widget, int id)
75 for (std::multimap<int, eActionBinding>::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i)
76 if ((i->second.m_widget == widget) && (i->second.m_id == id))
83 void eActionMap::unbindAction(const std::string &context, PyObject *function)
85 for (std::multimap<int, eActionBinding>::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i)
87 if (i->second.m_fnc && (PyObject_Compare(i->second.m_fnc, function) == 0))
89 Py_DECREF(i->second.m_fnc);
94 eFatal("unbindAction with illegal python reference");
98 void eActionMap::bindKey(const std::string &device, int key, int flags, const std::string &context, const std::string &action)
100 // first, search the actionlist table
102 for (i=0; i<sizeof(actions)/sizeof(*actions); ++i)
104 if ((actions[i].m_context == context) && (actions[i].m_action == action))
106 // we found a native action.
107 eNativeKeyBinding bind;
110 bind.m_flags = flags;
111 bind.m_action = actions[i].m_id;
112 m_native_keys.insert(std::pair<std::string,eNativeKeyBinding>(context, bind));
117 // we didn't find the action, so it must be a pythonAction
118 ePythonKeyBinding bind;
122 bind.m_flags = flags;
123 bind.m_action = action;
124 m_python_keys.insert(std::pair<std::string,ePythonKeyBinding>(context, bind));
129 PyObject *m_fnc, *m_arg;
132 call_entry(PyObject *fnc, PyObject *arg): m_fnc(fnc), m_arg(arg), m_widget(0), m_widget_arg(0) { }
133 call_entry(eWidget *widget, void *arg): m_widget(widget), m_widget_arg(arg), m_fnc(0), m_arg(0) { }
136 void eActionMap::keyPressed(int device, int key, int flags)
138 std::list<call_entry> call_list;
140 /* iterate active contexts. */
141 for (std::multimap<int,eActionBinding>::const_iterator c(m_bindings.begin()); c != m_bindings.end();)
143 std::multimap<int,eActionBinding>::const_iterator i = c;
145 /* is this a native context? */
146 if (i->second.m_widget)
148 /* is this a named context, i.e. not the wildcard? */
149 if (i->second.m_context.size())
151 std::multimap<std::string,eNativeKeyBinding>::const_iterator
152 k = m_native_keys.lower_bound(i->second.m_context),
153 e = m_native_keys.upper_bound(i->second.m_context);
158 // (k->second.m_device == m_device) &&
159 (k->second.m_key == key) &&
160 (k->second.m_flags & (1<<flags)))
161 call_list.push_back(call_entry(i->second.m_widget, (void*)k->second.m_action));
165 /* wildcard - get any keys. */
166 if (i->second.m_widget->event(eWidget::evtKey, (void*)key, (void*)flags))
169 } else if (i->second.m_fnc)
171 if (i->second.m_context.size())
173 std::multimap<std::string,ePythonKeyBinding>::const_iterator
174 k = m_python_keys.lower_bound(i->second.m_context),
175 e = m_python_keys.upper_bound(i->second.m_context);
180 // (k->second.m_device == m_device) &&
181 (k->second.m_key == key) &&
182 (k->second.m_flags & (1<<flags)))
184 PyObject *pArgs = PyTuple_New(2);
185 PyTuple_SetItem(pArgs, 0, PyString_FromString(k->first.c_str()));
186 PyTuple_SetItem(pArgs, 1, PyString_FromString(k->second.m_action.c_str()));
188 Py_INCREF(i->second.m_fnc);
189 call_list.push_back(call_entry(i->second.m_fnc, pArgs));
195 PyObject *pArgs = PyTuple_New(2);
196 PyTuple_SetItem(pArgs, 0, PyInt_FromLong(key));
197 PyTuple_SetItem(pArgs, 1, PyInt_FromLong(flags));
198 Py_INCREF(i->second.m_fnc);
199 call_list.push_back(call_entry(i->second.m_fnc, pArgs));
205 /* we need to iterate over all to not loose a reference */
206 for (std::list<call_entry>::iterator i(call_list.begin()); i != call_list.end(); ++i)
211 res = ePython::call(i->m_fnc, i->m_arg);
214 } else if (i->m_widget)
217 res = i->m_widget->event(eWidget::evtAction, 0, (void*)i->m_widget_arg);
222 eAutoInitPtr<eActionMap> init_eActionMap(eAutoInitNumbers::actions, "eActionMap");