actionmap: Implement key translations
[openblackhole/openblackhole-enigma2.git] / lib / actions / action.cpp
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>
6
7 /*
8
9   THIS CODE SUCKS.
10
11 we need:
12  - contexts that aren't compared as strings,
13  - maybe a lookup "device,key,flags" -> actions? (lazy validation, on bindAction)
14  - devices as ids
15  - seperate native from python keys? (currently, if an action wasn't found, it's ignored.)
16
17 Sorry. I spent 3 days on thinking how this could be made better, and it just DID NOT WORKED OUT.
18
19 If you have a better idea, please tell me.
20
21  */
22
23 DEFINE_REF(eActionMap);
24
25 eActionMap *eActionMap::instance;
26
27 eActionMap::eActionMap()
28 {
29         instance = this;
30 }
31
32 eActionMap::~eActionMap()
33 {
34         instance = 0;
35 }
36
37 RESULT eActionMap::getInstance(ePtr<eActionMap> &ptr)
38 {
39         ptr = instance;
40         if (!ptr)
41                 return -1;
42         return 0;
43 }
44
45 void eActionMap::bindAction(const std::string &context, int priority, int id, eWidget *widget)
46 {
47         eActionBinding bnd;
48
49         //eDebug("[eActionMap] bind widget to %s: prio=%d id=%d", context.c_str(), priority, id);
50         bnd.m_context = context;
51         bnd.m_widget = widget;
52         bnd.m_id = id;
53         m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
54 }
55
56 void eActionMap::bindAction(const std::string &context, int priority, ePyObject function)
57 {
58         eActionBinding bnd;
59
60         //eDebug("[eActionMap] bind function to %s: prio=%d", context.c_str(), priority);
61         bnd.m_context = context;
62         bnd.m_widget = 0;
63         Py_INCREF(function);
64         bnd.m_fnc = function;
65         m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
66 }
67
68 void eActionMap::unbindAction(eWidget *widget, int id)
69 {
70         //eDebug("[eActionMap] unbind widget id=%d", id);
71         for (std::multimap<int, eActionBinding>::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i)
72                 if (i->second.m_widget == widget && i->second.m_id == id)
73                 {
74                         m_bindings.erase(i);
75                         return;
76                 }
77 }
78
79 void eActionMap::unbindAction(const std::string &context, ePyObject function)
80 {
81         //eDebug("[eActionMap] unbind function from %s", context.c_str());
82         for (std::multimap<int, eActionBinding>::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i)
83                 if (i->second.m_fnc && (PyObject_Compare(i->second.m_fnc, function) == 0))
84                 {
85                         Py_DECREF(i->second.m_fnc);
86                         m_bindings.erase(i);
87                         return;
88                 }
89         eFatal("[eActionMap] unbindAction with illegal python reference");
90 }
91
92
93 void eActionMap::bindKey(const std::string &domain, const std::string &device, int key, int flags, const std::string &context, const std::string &action)
94 {
95         // start searching the actionlist table
96         //eDebug("[eActionMap] bind key from %s to %s: domain=%s action=%s key=%d flags=%d", device.c_str(), context.c_str(), domain.c_str(), action.c_str(), key, flags);
97         unsigned int i;
98         for (i = 0; i < sizeof(actions)/sizeof(*actions); ++i)
99         {
100                 if (actions[i].m_context == context && actions[i].m_action == action)
101                 {
102                         // found native action
103                         eNativeKeyBinding bind;
104                         bind.m_device = device;
105                         bind.m_key = key;
106                         bind.m_flags = flags;
107                         bind.m_action = actions[i].m_id;
108                         bind.m_domain = domain;
109                         m_native_keys.insert(std::pair<std::string,eNativeKeyBinding>(context, bind));
110                         return;
111                 }
112         }
113
114         // no action, so it must be a pythonAction
115         ePythonKeyBinding bind;
116
117         bind.m_device = device;
118         bind.m_key = key;
119         bind.m_flags = flags;
120         bind.m_action = action;
121         bind.m_domain = domain;
122         m_python_keys.insert(std::pair<std::string,ePythonKeyBinding>(context, bind));
123 }
124
125
126 void eActionMap::bindTranslation(const std::string &domain, const std::string &device, int keyin, int keyout, int toggle)
127 {
128         //eDebug("[eActionMap] bind translation for %s from %d to %d toggle=%d in %s", device.c_str(), keyin, keyout, toggle, domain.c_str());
129         eTranslationBinding trans;
130
131         trans.m_keyin  = keyin;
132         trans.m_keyout = keyout;
133         trans.m_toggle = toggle;
134         trans.m_domain = domain;
135
136         std::map<std::string, eDeviceBinding>::iterator r = m_rcDevices.find(device);
137         if (r == m_rcDevices.end())
138         {
139                 eDeviceBinding rc;
140                 rc.m_togglekey = KEY_RESERVED;
141                 rc.m_toggle = 0;;
142                 rc.m_translations.push_back(trans);
143                 m_rcDevices.insert(std::pair<std::string, eDeviceBinding>(device, rc));
144         }
145         else
146                 r->second.m_translations.push_back(trans);
147 }
148
149
150 void eActionMap::bindToggle(const std::string &domain, const std::string &device, int togglekey)
151 {
152         //eDebug("[eActionMap] bind togglekey for %s togglekey=%d in %s", device.c_str(), togglekey, domain.c_str());
153         std::map<std::string, eDeviceBinding>::iterator r = m_rcDevices.find(device);
154         if (r == m_rcDevices.end())
155         {
156                 eDeviceBinding rc;
157                 rc.m_togglekey = togglekey;
158                 rc.m_toggle = 0;;
159                 m_rcDevices.insert(std::pair<std::string, eDeviceBinding>(device, rc));
160         }
161         else
162         {
163                 r->second.m_togglekey = togglekey;
164                 r->second.m_toggle = 0;
165         }
166 }
167
168
169 void eActionMap::unbindKeyDomain(const std::string &domain)
170 {
171         //eDebug("[eActionMap] unbindDomain %s", domain.c_str());
172         for (std::multimap<std::string, eNativeKeyBinding>::iterator i(m_native_keys.begin()); i != m_native_keys.end(); ++i)
173                 if (i->second.m_domain == domain)
174                 {
175                         m_native_keys.erase(i);
176                         i = m_native_keys.begin();
177                 }
178
179         for (std::multimap<std::string, ePythonKeyBinding>::iterator i(m_python_keys.begin()); i != m_python_keys.end(); ++i)
180                 if (i->second.m_domain == domain)
181                 {
182                         m_python_keys.erase(i);
183                         i = m_python_keys.begin();
184                 }
185 }
186
187 struct call_entry
188 {
189         ePyObject m_fnc, m_arg;
190         eWidget *m_widget;
191         void *m_widget_arg, *m_widget_arg2;
192         call_entry(ePyObject fnc, ePyObject arg): m_fnc(fnc), m_arg(arg), m_widget(0), m_widget_arg(0) { }
193         call_entry(eWidget *widget, void *arg, void *arg2): m_widget(widget), m_widget_arg(arg), m_widget_arg2(arg2) { }
194 };
195
196 void eActionMap::keyPressed(const std::string &device, int key, int flags)
197 {
198         //eDebug("[eActionMap] key from %s: %d %d", device.c_str(), key, flags);
199
200         // Check for remotes that need key translations
201         std::map<std::string, eDeviceBinding>::iterator r = m_rcDevices.find(device);
202         if (r != m_rcDevices.end())
203         {
204
205                 if (key == r->second.m_togglekey && flags == eRCKey::flagMake)
206                 {
207                         r->second.m_toggle ^= 1;
208                         //eDebug("[eActionMap]   toggle key %d: now %d", key, r->second.m_toggle);
209                         return;
210                 }
211                 std::vector<eTranslationBinding> *trans = &r->second.m_translations;
212                 for (std::vector<eTranslationBinding>::iterator t(trans->begin()); t != trans->end(); ++t)
213                 {
214                         if (t->m_keyin == key && (t->m_toggle == 0 || r->second.m_toggle))
215                         {
216                                 //eDebug("[eActionMap]   translate from %d to %d", key, t->m_keyout);
217                                 key = t->m_keyout;
218                                 break;
219                         }
220                 }
221         }
222
223         std::vector<call_entry> call_list;
224         // iterate active contexts
225         for (std::multimap<int,eActionBinding>::iterator c(m_bindings.begin()); c != m_bindings.end(); ++c)
226         {
227                 if (flags == eRCKey::flagMake)
228                         c->second.m_prev_seen_make_key = key;
229                 else if (c->second.m_prev_seen_make_key != key)  // ignore repeat or break when the make code for this key was not visible
230                         continue;
231
232                 // is this a native context?
233                 if (c->second.m_widget)
234                 {
235                         // is this a named context, i.e. not the wildcard?
236                         if (c->second.m_context.size())
237                         {
238                                 //eDebug("[eActionMap]   native context %s", c->second.m_context.c_str());
239                                 std::multimap<std::string,eNativeKeyBinding>::const_iterator
240                                         k = m_native_keys.lower_bound(c->second.m_context),
241                                         e = m_native_keys.upper_bound(c->second.m_context);
242
243                                 for (; k != e; ++k)
244                                 {
245                                         if (    k->second.m_key == key &&
246                                                 k->second.m_flags & (1<<flags) &&
247                                                 (k->second.m_device == device || k->second.m_device == "generic") )
248                                                 call_list.push_back(call_entry(c->second.m_widget, (void*)c->second.m_id, (void*)k->second.m_action));
249                                 }
250                         }
251                         else
252                         {
253                                 // wildcard - get any keys.
254                                 //eDebug("[eActionMap]    native wildcard");
255                                 if (c->second.m_widget->event(eWidget::evtKey, (void*)key, (void*)flags))
256                                         return;
257                         }
258                 }
259                 else if (c->second.m_fnc)
260                 {
261                         if (c->second.m_context.size())
262                         {
263                                 //eDebug("[eActionMap]   python context %s", c->second.m_context.c_str());
264                                 std::multimap<std::string,ePythonKeyBinding>::const_iterator
265                                         k = m_python_keys.lower_bound(c->second.m_context),
266                                         e = m_python_keys.upper_bound(c->second.m_context);
267
268                                 for (; k != e; ++k)
269                                 {
270                                         if (    k->second.m_key == key &&
271                                                 k->second.m_flags & (1<<flags) &&
272                                                 (k->second.m_device == device || k->second.m_device == "generic") )
273                                         {
274                                                 ePyObject pArgs = PyTuple_New(2);
275                                                 PyTuple_SET_ITEM(pArgs, 0, PyString_FromString(k->first.c_str()));
276                                                 PyTuple_SET_ITEM(pArgs, 1, PyString_FromString(k->second.m_action.c_str()));
277                                                 Py_INCREF(c->second.m_fnc);
278                                                 call_list.push_back(call_entry(c->second.m_fnc, pArgs));
279                                         }
280                                 }
281                         }
282                         else
283                         {
284                                 //eDebug("[eActionMap]   python wildcard.");
285                                 ePyObject pArgs = PyTuple_New(2);
286                                 PyTuple_SET_ITEM(pArgs, 0, PyInt_FromLong(key));
287                                 PyTuple_SET_ITEM(pArgs, 1, PyInt_FromLong(flags));
288                                 Py_INCREF(c->second.m_fnc);
289                                 call_list.push_back(call_entry(c->second.m_fnc, pArgs));
290                         }
291                 }
292         }
293
294         int res = 0;
295         // iterate over all to not loose a reference
296         for (std::vector<call_entry>::iterator i(call_list.begin()); i != call_list.end(); ++i)
297         {
298                 if (i->m_fnc)
299                 {
300                         if (!res)
301                                 res = ePython::call(i->m_fnc, i->m_arg);
302                         Py_DECREF(i->m_fnc);
303                         Py_DECREF(i->m_arg);
304                 }
305                 else if (i->m_widget && !res)
306                         res = i->m_widget->event(eWidget::evtAction, (void*)i->m_widget_arg, (void*)i->m_widget_arg2 );
307         }
308 }
309
310 ePtr<eActionMap> NewActionMapPtr(void)
311 {
312         ePtr<eActionMap> ptr;
313         eActionMap::getInstance(ptr);
314         return ptr;
315 }
316
317 eAutoInitPtr<eActionMap> init_eActionMap(eAutoInitNumbers::actions, "eActionMap");