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)
44 void eActionMap::bindAction(const std::string &context, int priority, int id, eWidget *widget)
48 bnd.m_context = context;
49 bnd.m_widget = widget;
51 m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
54 void eActionMap::bindAction(const std::string &context, int priority, ePyObject function)
58 bnd.m_context = context;
62 m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
65 void eActionMap::unbindAction(eWidget *widget, int id)
67 for (std::multimap<int, eActionBinding>::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i)
68 if ((i->second.m_widget == widget) && (i->second.m_id == id))
75 void eActionMap::unbindAction(const std::string &context, ePyObject function)
77 for (std::multimap<int, eActionBinding>::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i)
79 if (i->second.m_fnc && (PyObject_Compare(i->second.m_fnc, function) == 0))
81 Py_DECREF(i->second.m_fnc);
86 eFatal("unbindAction with illegal python reference");
90 void eActionMap::bindKey(const std::string &device, int key, int flags, const std::string &context, const std::string &action)
92 // first, search the actionlist table
94 for (i=0; i<sizeof(actions)/sizeof(*actions); ++i)
96 if ((actions[i].m_context == context) && (actions[i].m_action == action))
98 // we found a native action.
99 eNativeKeyBinding bind;
100 bind.m_device = device;
102 bind.m_flags = flags;
103 bind.m_action = actions[i].m_id;
104 m_native_keys.insert(std::pair<std::string,eNativeKeyBinding>(context, bind));
109 // we didn't find the action, so it must be a pythonAction
110 ePythonKeyBinding bind;
112 bind.m_device = device;
114 bind.m_flags = flags;
115 bind.m_action = action;
116 m_python_keys.insert(std::pair<std::string,ePythonKeyBinding>(context, bind));
121 ePyObject m_fnc, m_arg;
123 void *m_widget_arg, *m_widget_arg2;
124 call_entry(ePyObject fnc, ePyObject arg): m_fnc(fnc), m_arg(arg), m_widget(0), m_widget_arg(0) { }
125 call_entry(eWidget *widget, void *arg, void *arg2): m_widget(widget), m_widget_arg(arg), m_widget_arg2(arg2) { }
128 void eActionMap::keyPressed(const std::string &device, int key, int flags)
130 std::list<call_entry> call_list;
132 /* iterate active contexts. */
133 for (std::multimap<int,eActionBinding>::const_iterator c(m_bindings.begin()); c != m_bindings.end();)
135 std::multimap<int,eActionBinding>::const_iterator i = c;
137 /* is this a native context? */
138 if (i->second.m_widget)
140 /* is this a named context, i.e. not the wildcard? */
141 if (i->second.m_context.size())
143 std::multimap<std::string,eNativeKeyBinding>::const_iterator
144 k = m_native_keys.lower_bound(i->second.m_context),
145 e = m_native_keys.upper_bound(i->second.m_context);
150 (k->second.m_key == key) &&
151 (k->second.m_flags & (1<<flags)) &&
152 ((k->second.m_device == device) || (k->second.m_device == "generic"))
154 call_list.push_back(call_entry(i->second.m_widget, (void*)i->second.m_id, (void*)k->second.m_action));
158 /* wildcard - get any keys. */
159 if (i->second.m_widget->event(eWidget::evtKey, (void*)key, (void*)flags))
162 } else if (i->second.m_fnc)
164 if (i->second.m_context.size())
166 std::multimap<std::string,ePythonKeyBinding>::const_iterator
167 k = m_python_keys.lower_bound(i->second.m_context),
168 e = m_python_keys.upper_bound(i->second.m_context);
173 (k->second.m_key == key) &&
174 (k->second.m_flags & (1<<flags)) &&
175 ((k->second.m_device == device) || (k->second.m_device == "generic"))
178 ePyObject pArgs = PyTuple_New(2);
179 PyTuple_SET_ITEM(pArgs, 0, PyString_FromString(k->first.c_str()));
180 PyTuple_SET_ITEM(pArgs, 1, PyString_FromString(k->second.m_action.c_str()));
182 Py_INCREF(i->second.m_fnc);
183 call_list.push_back(call_entry(i->second.m_fnc, pArgs));
190 ePyObject pArgs = PyTuple_New(2);
191 PyTuple_SET_ITEM(pArgs, 0, PyInt_FromLong(key));
192 PyTuple_SET_ITEM(pArgs, 1, PyInt_FromLong(flags));
193 Py_INCREF(i->second.m_fnc);
194 call_list.push_back(call_entry(i->second.m_fnc, pArgs));
200 /* we need to iterate over all to not loose a reference */
201 for (std::list<call_entry>::iterator i(call_list.begin()); i != call_list.end(); ++i)
206 res = ePython::call(i->m_fnc, i->m_arg);
209 } else if (i->m_widget)
212 res = i->m_widget->event(eWidget::evtAction, (void*)i->m_widget_arg, (void*)i->m_widget_arg2 );
217 ePtr<eActionMap> NewActionMapPtr(void)
219 ePtr<eActionMap> ptr;
220 eActionMap::getInstance(ptr);
224 eAutoInitPtr<eActionMap> init_eActionMap(eAutoInitNumbers::actions, "eActionMap");