Merge branch 'master' of git.opendreambox.org:/git/enigma2
[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         bnd.m_context = context;
50         bnd.m_widget = widget;
51         bnd.m_id = id;
52         m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
53 }
54
55 void eActionMap::bindAction(const std::string &context, int priority, ePyObject function)
56 {
57         eActionBinding bnd;
58         
59         bnd.m_context = context;
60         bnd.m_widget = 0;
61         Py_INCREF(function);
62         bnd.m_fnc = function;
63         m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
64 }
65
66 void eActionMap::unbindAction(eWidget *widget, int id)
67 {
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))
70                 {
71                         m_bindings.erase(i);
72                         return;
73                 }
74 }
75
76 void eActionMap::unbindAction(const std::string &context, ePyObject function)
77 {
78         for (std::multimap<int, eActionBinding>::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i)
79         {
80                 if (i->second.m_fnc && (PyObject_Compare(i->second.m_fnc, function) == 0))
81                 {
82                         Py_DECREF(i->second.m_fnc);
83                         m_bindings.erase(i);
84                         return;
85                 }
86         }
87         eFatal("unbindAction with illegal python reference");
88 }
89
90
91 void eActionMap::bindKey(const std::string &domain, const std::string &device, int key, int flags, const std::string &context, const std::string &action)
92 {
93                 // first, search the actionlist table
94         unsigned int i;
95         for (i=0; i<sizeof(actions)/sizeof(*actions); ++i)
96         {
97                 if ((actions[i].m_context == context) && (actions[i].m_action == action))
98                 {
99                                 // we found a native action.
100                         eNativeKeyBinding bind;
101                         bind.m_device = device;
102                         bind.m_key = key;
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));
107                         return;
108                 }
109         }
110         
111                 // we didn't find the action, so it must be a pythonAction
112         ePythonKeyBinding bind;
113
114         bind.m_device = device;
115         bind.m_key = key;
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));
120 }
121
122 void eActionMap::unbindKeyDomain(const std::string &domain)
123 {
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)
126                 {
127                         m_native_keys.erase(i);
128                         i = m_native_keys.begin();
129                 }
130
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)
133                 {
134                         m_python_keys.erase(i);
135                         i = m_python_keys.begin();
136                 }
137 }
138
139 struct call_entry
140 {
141         ePyObject m_fnc, m_arg;
142         eWidget *m_widget;
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) { }
146 };
147
148 void eActionMap::keyPressed(const std::string &device, int key, int flags)
149 {
150         std::list<call_entry> call_list;
151         
152                 /* iterate active contexts. */
153         for (std::multimap<int,eActionBinding>::iterator c(m_bindings.begin());
154                 c != m_bindings.end(); ++c)
155         {
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
159                         continue;
160
161                         /* is this a native context? */
162                 if (c->second.m_widget)
163                 {
164                                 /* is this a named context, i.e. not the wildcard? */
165                         if (c->second.m_context.size())
166                         {
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);
170
171                                 for (; k != e; ++k)
172                                 {
173                                         if (
174                                                         (k->second.m_key == key) &&
175                                                         (k->second.m_flags & (1<<flags)) &&
176                                                   ((k->second.m_device == device) || (k->second.m_device == "generic"))
177                                                   )
178                                                 call_list.push_back(call_entry(c->second.m_widget, (void*)c->second.m_id, (void*)k->second.m_action));
179                                 }
180                         } else
181                         {
182                                         /* wildcard - get any keys. */
183                                 if (c->second.m_widget->event(eWidget::evtKey, (void*)key, (void*)flags))
184                                         return;
185                         }
186                 } else if (c->second.m_fnc)
187                 {
188                         if (c->second.m_context.size())
189                         {
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);
193
194                                 for (; k != e; ++k)
195                                 {
196                                         if (
197                                                 (k->second.m_key == key) &&
198                                                 (k->second.m_flags & (1<<flags)) &&
199                                                 ((k->second.m_device == device) || (k->second.m_device == "generic"))
200                                                 )
201                                         {
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));
207                                         }
208                                 }
209                         } else
210                         {
211                                 eDebug("wildcard.");
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));
217                         }
218                 }
219         }
220
221         int res = 0;
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)
224         {
225                 if (i->m_fnc)
226                 {
227                         if (!res)
228                                 res = ePython::call(i->m_fnc, i->m_arg);
229                         Py_DECREF(i->m_fnc);
230                         Py_DECREF(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 );
233         }
234 }
235
236 ePtr<eActionMap> NewActionMapPtr(void)
237 {
238         ePtr<eActionMap> ptr;
239         eActionMap::getInstance(ptr);
240         return ptr;
241 }
242
243 eAutoInitPtr<eActionMap> init_eActionMap(eAutoInitNumbers::actions, "eActionMap");