add 'domain' in actionmap for loading (and unloading) multiple keymaps at once
[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
6 /*
7
8   THIS CODE SUCKS.
9
10 we need:
11  - contexts that aren't compared as strings,
12  - maybe a lookup "device,key,flags" -> actions? (lazy validation, on bindAction)
13  - devices as ids
14  - seperate native from python keys? (currently, if an action wasn't found, it's ignored.)
15  
16 Sorry. I spent 3 days on thinking how this could be made better, and it just DID NOT WORKED OUT.
17
18 If you have a better idea, please tell me.
19
20  */
21
22 DEFINE_REF(eActionMap);
23
24 eActionMap *eActionMap::instance;
25
26 eActionMap::eActionMap()
27 {
28         instance = this;
29 }
30
31 eActionMap::~eActionMap()
32 {
33         instance = 0;
34 }
35
36 RESULT eActionMap::getInstance(ePtr<eActionMap> &ptr)
37 {
38         ptr = instance;
39         if (!ptr)
40                 return -1;
41         return 0;
42 }
43
44 void eActionMap::bindAction(const std::string &context, int priority, int id, eWidget *widget)
45 {
46         eActionBinding bnd;
47         
48         bnd.m_context = context;
49         bnd.m_widget = widget;
50         bnd.m_id = id;
51         m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
52 }
53
54 void eActionMap::bindAction(const std::string &context, int priority, ePyObject function)
55 {
56         eActionBinding bnd;
57         
58         bnd.m_context = context;
59         bnd.m_widget = 0;
60         Py_INCREF(function);
61         bnd.m_fnc = function;
62         m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
63 }
64
65 void eActionMap::unbindAction(eWidget *widget, int id)
66 {
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))
69                 {
70                         m_bindings.erase(i);
71                         return;
72                 }
73 }
74
75 void eActionMap::unbindAction(const std::string &context, ePyObject function)
76 {
77         for (std::multimap<int, eActionBinding>::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i)
78         {
79                 if (i->second.m_fnc && (PyObject_Compare(i->second.m_fnc, function) == 0))
80                 {
81                         Py_DECREF(i->second.m_fnc);
82                         m_bindings.erase(i);
83                         return;
84                 }
85         }
86         eFatal("unbindAction with illegal python reference");
87 }
88
89
90 void eActionMap::bindKey(const std::string &domain, const std::string &device, int key, int flags, const std::string &context, const std::string &action)
91 {
92                 // first, search the actionlist table
93         unsigned int i;
94         for (i=0; i<sizeof(actions)/sizeof(*actions); ++i)
95         {
96                 if ((actions[i].m_context == context) && (actions[i].m_action == action))
97                 {
98                                 // we found a native action.
99                         eNativeKeyBinding bind;
100                         bind.m_device = device;
101                         bind.m_key = key;
102                         bind.m_flags = flags;
103                         bind.m_action = actions[i].m_id;
104                         bind.m_domain = domain;
105                         m_native_keys.insert(std::pair<std::string,eNativeKeyBinding>(context, bind));
106                         return;
107                 }
108         }
109         
110                 // we didn't find the action, so it must be a pythonAction
111         ePythonKeyBinding bind;
112
113         bind.m_device = device;
114         bind.m_key = key;
115         bind.m_flags = flags;
116         bind.m_action = action;
117         bind.m_domain = domain;
118         m_python_keys.insert(std::pair<std::string,ePythonKeyBinding>(context, bind));
119 }
120
121 void eActionMap::unbindKeyDomain(const std::string &domain)
122 {
123         for (std::multimap<std::string, eNativeKeyBinding>::iterator i(m_native_keys.begin()); i != m_native_keys.end(); ++i)
124                 if (i->second.m_domain == domain)
125                 {
126                         m_native_keys.erase(i);
127                         i = m_native_keys.begin();
128                 }
129
130         for (std::multimap<std::string, ePythonKeyBinding>::iterator i(m_python_keys.begin()); i != m_python_keys.end(); ++i)
131                 if (i->second.m_domain == domain)
132                 {
133                         m_python_keys.erase(i);
134                         i = m_python_keys.begin();
135                 }
136 }
137
138 struct call_entry
139 {
140         ePyObject m_fnc, m_arg;
141         eWidget *m_widget;
142         void *m_widget_arg, *m_widget_arg2;
143         call_entry(ePyObject fnc, ePyObject arg): m_fnc(fnc), m_arg(arg), m_widget(0), m_widget_arg(0) { }
144         call_entry(eWidget *widget, void *arg, void *arg2): m_widget(widget), m_widget_arg(arg), m_widget_arg2(arg2) { }
145 };
146
147 void eActionMap::keyPressed(const std::string &device, int key, int flags)
148 {
149         std::list<call_entry> call_list;
150         
151                 /* iterate active contexts. */
152         for (std::multimap<int,eActionBinding>::const_iterator c(m_bindings.begin());
153                 c != m_bindings.end(); ++c)
154         {
155                         /* is this a native context? */
156                 if (c->second.m_widget)
157                 {
158                                 /* is this a named context, i.e. not the wildcard? */
159                         if (c->second.m_context.size())
160                         {
161                                 std::multimap<std::string,eNativeKeyBinding>::const_iterator
162                                         k = m_native_keys.lower_bound(c->second.m_context),
163                                         e = m_native_keys.upper_bound(c->second.m_context);
164
165                                 for (; k != e; ++k)
166                                 {
167                                         if (
168                                                         (k->second.m_key == key) &&
169                                                         (k->second.m_flags & (1<<flags)) &&
170                                                   ((k->second.m_device == device) || (k->second.m_device == "generic"))
171                                                   )
172                                                 call_list.push_back(call_entry(c->second.m_widget, (void*)c->second.m_id, (void*)k->second.m_action));
173                                 }
174                         } else
175                         {
176                                         /* wildcard - get any keys. */
177                                 if (c->second.m_widget->event(eWidget::evtKey, (void*)key, (void*)flags))
178                                         return;
179                         }
180                 } else if (c->second.m_fnc)
181                 {
182                         if (c->second.m_context.size())
183                         {
184                                 std::multimap<std::string,ePythonKeyBinding>::const_iterator
185                                         k = m_python_keys.lower_bound(c->second.m_context),
186                                         e = m_python_keys.upper_bound(c->second.m_context);
187
188                                 for (; k != e; ++k)
189                                 {
190                                         if (
191                                                 (k->second.m_key == key) &&
192                                                 (k->second.m_flags & (1<<flags)) &&
193                                                 ((k->second.m_device == device) || (k->second.m_device == "generic"))
194                                                 )
195                                         {
196                                                 ePyObject pArgs = PyTuple_New(2);
197                                                 PyTuple_SET_ITEM(pArgs, 0, PyString_FromString(k->first.c_str()));
198                                                 PyTuple_SET_ITEM(pArgs, 1, PyString_FromString(k->second.m_action.c_str()));
199                                                 Py_INCREF(c->second.m_fnc);
200                                                 call_list.push_back(call_entry(c->second.m_fnc, pArgs));
201                                         }
202                                 }
203                         } else
204                         {
205                                 eDebug("wildcard.");
206                                 ePyObject pArgs = PyTuple_New(2);
207                                 PyTuple_SET_ITEM(pArgs, 0, PyInt_FromLong(key));
208                                 PyTuple_SET_ITEM(pArgs, 1, PyInt_FromLong(flags));
209                                 Py_INCREF(c->second.m_fnc);
210                                 call_list.push_back(call_entry(c->second.m_fnc, pArgs));
211                         }
212                 }
213         }
214
215         int res = 0;
216                         /* we need to iterate over all to not loose a reference */
217         for (std::list<call_entry>::iterator i(call_list.begin()); i != call_list.end(); ++i)
218         {
219                 if (i->m_fnc)
220                 {
221                         if (!res)
222                                 res = ePython::call(i->m_fnc, i->m_arg);
223                         Py_DECREF(i->m_fnc);
224                         Py_DECREF(i->m_arg);
225                 } else if (i->m_widget && !res)
226                         res = i->m_widget->event(eWidget::evtAction, (void*)i->m_widget_arg, (void*)i->m_widget_arg2 );
227         }
228 }
229
230 ePtr<eActionMap> NewActionMapPtr(void)
231 {
232         ePtr<eActionMap> ptr;
233         eActionMap::getInstance(ptr);
234         return ptr;
235 }
236
237 eAutoInitPtr<eActionMap> init_eActionMap(eAutoInitNumbers::actions, "eActionMap");