actionmap: cosmetics/debugs
[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 void eActionMap::unbindKeyDomain(const std::string &domain)
126 {
127         //eDebug("[eActionMap] unbindDomain %s", domain.c_str());
128         for (std::multimap<std::string, eNativeKeyBinding>::iterator i(m_native_keys.begin()); i != m_native_keys.end(); ++i)
129                 if (i->second.m_domain == domain)
130                 {
131                         m_native_keys.erase(i);
132                         i = m_native_keys.begin();
133                 }
134
135         for (std::multimap<std::string, ePythonKeyBinding>::iterator i(m_python_keys.begin()); i != m_python_keys.end(); ++i)
136                 if (i->second.m_domain == domain)
137                 {
138                         m_python_keys.erase(i);
139                         i = m_python_keys.begin();
140                 }
141 }
142
143 struct call_entry
144 {
145         ePyObject m_fnc, m_arg;
146         eWidget *m_widget;
147         void *m_widget_arg, *m_widget_arg2;
148         call_entry(ePyObject fnc, ePyObject arg): m_fnc(fnc), m_arg(arg), m_widget(0), m_widget_arg(0) { }
149         call_entry(eWidget *widget, void *arg, void *arg2): m_widget(widget), m_widget_arg(arg), m_widget_arg2(arg2) { }
150 };
151
152 void eActionMap::keyPressed(const std::string &device, int key, int flags)
153 {
154         //eDebug("[eActionMap] key from %s: %d %d", device.c_str(), key, flags);
155         std::list<call_entry> call_list;
156
157         // iterate active contexts
158         for (std::multimap<int,eActionBinding>::iterator c(m_bindings.begin()); c != m_bindings.end(); ++c)
159         {
160                 if (flags == eRCKey::flagMake)
161                         c->second.m_prev_seen_make_key = key;
162                 else if (c->second.m_prev_seen_make_key != key)  // ignore repeat or break when the make code for this key was not visible
163                         continue;
164
165                 // is this a native context?
166                 if (c->second.m_widget)
167                 {
168                         // is this a named context, i.e. not the wildcard?
169                         if (c->second.m_context.size())
170                         {
171                                 //eDebug("[eActionMap]   native context %s", c->second.m_context.c_str());
172                                 std::multimap<std::string,eNativeKeyBinding>::const_iterator
173                                         k = m_native_keys.lower_bound(c->second.m_context),
174                                         e = m_native_keys.upper_bound(c->second.m_context);
175
176                                 for (; k != e; ++k)
177                                 {
178                                         if (    k->second.m_key == key &&
179                                                 k->second.m_flags & (1<<flags) &&
180                                                 (k->second.m_device == device || k->second.m_device == "generic") )
181                                                 call_list.push_back(call_entry(c->second.m_widget, (void*)c->second.m_id, (void*)k->second.m_action));
182                                 }
183                         }
184                         else
185                         {
186                                 // wildcard - get any keys.
187                                 //eDebug("[eActionMap]    native wildcard");
188                                 if (c->second.m_widget->event(eWidget::evtKey, (void*)key, (void*)flags))
189                                         return;
190                         }
191                 }
192                 else if (c->second.m_fnc)
193                 {
194                         if (c->second.m_context.size())
195                         {
196                                 //eDebug("[eActionMap]   python context %s", c->second.m_context.c_str());
197                                 std::multimap<std::string,ePythonKeyBinding>::const_iterator
198                                         k = m_python_keys.lower_bound(c->second.m_context),
199                                         e = m_python_keys.upper_bound(c->second.m_context);
200
201                                 for (; k != e; ++k)
202                                 {
203                                         if (    k->second.m_key == key &&
204                                                 k->second.m_flags & (1<<flags) &&
205                                                 (k->second.m_device == device || k->second.m_device == "generic") )
206                                         {
207                                                 ePyObject pArgs = PyTuple_New(2);
208                                                 PyTuple_SET_ITEM(pArgs, 0, PyString_FromString(k->first.c_str()));
209                                                 PyTuple_SET_ITEM(pArgs, 1, PyString_FromString(k->second.m_action.c_str()));
210                                                 Py_INCREF(c->second.m_fnc);
211                                                 call_list.push_back(call_entry(c->second.m_fnc, pArgs));
212                                         }
213                                 }
214                         }
215                         else
216                         {
217                                 //eDebug("[eActionMap]   python wildcard.");
218                                 ePyObject pArgs = PyTuple_New(2);
219                                 PyTuple_SET_ITEM(pArgs, 0, PyInt_FromLong(key));
220                                 PyTuple_SET_ITEM(pArgs, 1, PyInt_FromLong(flags));
221                                 Py_INCREF(c->second.m_fnc);
222                                 call_list.push_back(call_entry(c->second.m_fnc, pArgs));
223                         }
224                 }
225         }
226
227         int res = 0;
228         // iterate over all to not loose a reference
229         for (std::list<call_entry>::iterator i(call_list.begin()); i != call_list.end(); ++i)
230         {
231                 if (i->m_fnc)
232                 {
233                         if (!res)
234                                 res = ePython::call(i->m_fnc, i->m_arg);
235                         Py_DECREF(i->m_fnc);
236                         Py_DECREF(i->m_arg);
237                 }
238                 else if (i->m_widget && !res)
239                         res = i->m_widget->event(eWidget::evtAction, (void*)i->m_widget_arg, (void*)i->m_widget_arg2 );
240         }
241 }
242
243 ePtr<eActionMap> NewActionMapPtr(void)
244 {
245         ePtr<eActionMap> ptr;
246         eActionMap::getInstance(ptr);
247         return ptr;
248 }
249
250 eAutoInitPtr<eActionMap> init_eActionMap(eAutoInitNumbers::actions, "eActionMap");