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>
5 #include <lib/driver/rc.h>
12 - contexts that aren't compared as strings,
13 - maybe a lookup "device,key,flags" -> actions? (lazy validation, on bindAction)
15 - seperate native from python keys? (currently, if an action wasn't found, it's ignored.)
17 Sorry. I spent 3 days on thinking how this could be made better, and it just DID NOT WORKED OUT.
19 If you have a better idea, please tell me.
23 DEFINE_REF(eActionMap);
25 eActionMap *eActionMap::instance;
27 eActionMap::eActionMap()
32 eActionMap::~eActionMap()
37 RESULT eActionMap::getInstance(ePtr<eActionMap> &ptr)
45 void eActionMap::bindAction(const std::string &context, int priority, int id, eWidget *widget)
49 bnd.m_context = context;
50 bnd.m_widget = widget;
52 m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
55 void eActionMap::bindAction(const std::string &context, int priority, ePyObject function)
59 bnd.m_context = context;
63 m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
66 void eActionMap::unbindAction(eWidget *widget, int id)
68 for (std::multimap<int, eActionBinding>::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i)
69 if ((i->second.m_widget == widget) && (i->second.m_id == id))
76 void eActionMap::unbindAction(const std::string &context, ePyObject function)
78 for (std::multimap<int, eActionBinding>::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i)
80 if (i->second.m_fnc && (PyObject_Compare(i->second.m_fnc, function) == 0))
82 Py_DECREF(i->second.m_fnc);
87 eFatal("unbindAction with illegal python reference");
91 void eActionMap::bindKey(const std::string &domain, const std::string &device, int key, int flags, const std::string &context, const std::string &action)
93 // first, search the actionlist table
95 for (i=0; i<sizeof(actions)/sizeof(*actions); ++i)
97 if ((actions[i].m_context == context) && (actions[i].m_action == action))
99 // we found a native action.
100 eNativeKeyBinding bind;
101 bind.m_device = device;
103 bind.m_flags = flags;
104 bind.m_action = actions[i].m_id;
105 bind.m_domain = domain;
106 m_native_keys.insert(std::pair<std::string,eNativeKeyBinding>(context, bind));
111 // we didn't find the action, so it must be a pythonAction
112 ePythonKeyBinding bind;
114 bind.m_device = device;
116 bind.m_flags = flags;
117 bind.m_action = action;
118 bind.m_domain = domain;
119 m_python_keys.insert(std::pair<std::string,ePythonKeyBinding>(context, bind));
122 void eActionMap::unbindKeyDomain(const std::string &domain)
124 for (std::multimap<std::string, eNativeKeyBinding>::iterator i(m_native_keys.begin()); i != m_native_keys.end(); ++i)
125 if (i->second.m_domain == domain)
127 m_native_keys.erase(i);
128 i = m_native_keys.begin();
131 for (std::multimap<std::string, ePythonKeyBinding>::iterator i(m_python_keys.begin()); i != m_python_keys.end(); ++i)
132 if (i->second.m_domain == domain)
134 m_python_keys.erase(i);
135 i = m_python_keys.begin();
141 ePyObject m_fnc, m_arg;
143 void *m_widget_arg, *m_widget_arg2;
144 call_entry(ePyObject fnc, ePyObject arg): m_fnc(fnc), m_arg(arg), m_widget(0), m_widget_arg(0) { }
145 call_entry(eWidget *widget, void *arg, void *arg2): m_widget(widget), m_widget_arg(arg), m_widget_arg2(arg2) { }
148 void eActionMap::keyPressed(const std::string &device, int key, int flags)
150 std::list<call_entry> call_list;
152 /* iterate active contexts. */
153 for (std::multimap<int,eActionBinding>::iterator c(m_bindings.begin());
154 c != m_bindings.end(); ++c)
156 if (flags == eRCKey::flagMake)
157 c->second.m_prev_seen_make_key = key;
158 else if (c->second.m_prev_seen_make_key != key) // ignore repeat or break when the make code for this key was not visible
161 /* is this a native context? */
162 if (c->second.m_widget)
164 /* is this a named context, i.e. not the wildcard? */
165 if (c->second.m_context.size())
167 std::multimap<std::string,eNativeKeyBinding>::const_iterator
168 k = m_native_keys.lower_bound(c->second.m_context),
169 e = m_native_keys.upper_bound(c->second.m_context);
174 (k->second.m_key == key) &&
175 (k->second.m_flags & (1<<flags)) &&
176 ((k->second.m_device == device) || (k->second.m_device == "generic"))
178 call_list.push_back(call_entry(c->second.m_widget, (void*)c->second.m_id, (void*)k->second.m_action));
182 /* wildcard - get any keys. */
183 if (c->second.m_widget->event(eWidget::evtKey, (void*)key, (void*)flags))
186 } else if (c->second.m_fnc)
188 if (c->second.m_context.size())
190 std::multimap<std::string,ePythonKeyBinding>::const_iterator
191 k = m_python_keys.lower_bound(c->second.m_context),
192 e = m_python_keys.upper_bound(c->second.m_context);
197 (k->second.m_key == key) &&
198 (k->second.m_flags & (1<<flags)) &&
199 ((k->second.m_device == device) || (k->second.m_device == "generic"))
202 ePyObject pArgs = PyTuple_New(2);
203 PyTuple_SET_ITEM(pArgs, 0, PyString_FromString(k->first.c_str()));
204 PyTuple_SET_ITEM(pArgs, 1, PyString_FromString(k->second.m_action.c_str()));
205 Py_INCREF(c->second.m_fnc);
206 call_list.push_back(call_entry(c->second.m_fnc, pArgs));
212 ePyObject pArgs = PyTuple_New(2);
213 PyTuple_SET_ITEM(pArgs, 0, PyInt_FromLong(key));
214 PyTuple_SET_ITEM(pArgs, 1, PyInt_FromLong(flags));
215 Py_INCREF(c->second.m_fnc);
216 call_list.push_back(call_entry(c->second.m_fnc, pArgs));
222 /* we need to iterate over all to not loose a reference */
223 for (std::list<call_entry>::iterator i(call_list.begin()); i != call_list.end(); ++i)
228 res = ePython::call(i->m_fnc, i->m_arg);
231 } else if (i->m_widget && !res)
232 res = i->m_widget->event(eWidget::evtAction, (void*)i->m_widget_arg, (void*)i->m_widget_arg2 );
236 ePtr<eActionMap> NewActionMapPtr(void)
238 ePtr<eActionMap> ptr;
239 eActionMap::getInstance(ptr);
243 eAutoInitPtr<eActionMap> init_eActionMap(eAutoInitNumbers::actions, "eActionMap");