sync / no changes
[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 #if 0
45 void eActionMap::getInstance(eActionMap **ptr)
46 {
47         *ptr = instance;
48 }
49 #endif
50
51 void eActionMap::bindAction(const std::string &context, int priority, int id, eWidget *widget)
52 {
53         eActionBinding bnd;
54         
55         bnd.m_context = context;
56         bnd.m_widget = widget;
57         bnd.m_fnc = 0;
58         bnd.m_id = id;
59         m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
60 }
61
62 void eActionMap::bindAction(const std::string &context, int priority, PyObject *function)
63 {
64         eActionBinding bnd;
65         
66         bnd.m_context = context;
67         bnd.m_widget = 0;
68         Py_INCREF(function);
69         bnd.m_fnc = function;
70         m_bindings.insert(std::pair<int,eActionBinding>(priority, bnd));
71 }
72
73 void eActionMap::unbindAction(eWidget *widget, int id)
74 {
75         for (std::multimap<int, eActionBinding>::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i)
76                 if ((i->second.m_widget == widget) && (i->second.m_id == id))
77                 {
78                         m_bindings.erase(i);
79                         return;
80                 }
81 }
82
83 void eActionMap::unbindAction(const std::string &context, PyObject *function)
84 {
85         for (std::multimap<int, eActionBinding>::iterator i(m_bindings.begin()); i != m_bindings.end(); ++i)
86         {
87                 if (i->second.m_fnc && (PyObject_Compare(i->second.m_fnc, function) == 0))
88                 {
89                         Py_DECREF(i->second.m_fnc);
90                         m_bindings.erase(i);
91                         return;
92                 }
93         }
94         eFatal("unbindAction with illegal python reference");
95 }
96
97
98 void eActionMap::bindKey(const std::string &device, int key, int flags, const std::string &context, const std::string &action)
99 {
100                 // first, search the actionlist table
101         unsigned int i;
102         for (i=0; i<sizeof(actions)/sizeof(*actions); ++i)
103         {
104                 if ((actions[i].m_context == context) && (actions[i].m_action == action))
105                 {
106                                 // we found a native action.
107                         eNativeKeyBinding bind;
108                         bind.m_device = 0;
109                         bind.m_key = key;
110                         bind.m_flags = flags;
111                         bind.m_action = actions[i].m_id;
112                         m_native_keys.insert(std::pair<std::string,eNativeKeyBinding>(context, bind));
113                         return;
114                 }
115         }
116         
117                 // we didn't find the action, so it must be a pythonAction
118         ePythonKeyBinding bind;
119
120         bind.m_device = 0;
121         bind.m_key = key;
122         bind.m_flags = flags;
123         bind.m_action = action;
124         m_python_keys.insert(std::pair<std::string,ePythonKeyBinding>(context, bind));
125 }
126
127 void eActionMap::keyPressed(int device, int key, int flags)
128 {
129         std::list<std::pair<PyObject*,PyObject*> > call_list;
130         
131                 /* iterate active contexts. */
132         for (std::multimap<int,eActionBinding>::const_iterator c(m_bindings.begin()); c != m_bindings.end();)
133         {
134                 std::multimap<int,eActionBinding>::const_iterator i = c;
135                 ++c;
136                         /* is this a native context? */
137                 if (i->second.m_widget)
138                 {
139                                 /* is this a named context, i.e. not the wildcard? */
140                         if (i->second.m_context.size())
141                         {
142                                 std::multimap<std::string,eNativeKeyBinding>::const_iterator
143                                         k = m_native_keys.lower_bound(i->second.m_context),
144                                         e = m_native_keys.upper_bound(i->second.m_context);
145                                 
146                                 for (; k != e; ++k)
147                                 {
148                                         if (
149                                                 // (k->second.m_device == m_device) &&
150                                                 (k->second.m_key == key) &&
151                                                 (k->second.m_flags & (1<<flags)))
152                                         {
153                                                 if (i->second.m_widget->event(eWidget::evtAction, 0, (void*)k->second.m_action))
154                                                         return;
155                                         }
156                                 }
157                         } else
158                         {
159                                         /* wildcard - get any keys. */
160                                 if (i->second.m_widget->event(eWidget::evtKey, (void*)key, (void*)flags))
161                                         return;
162                         }
163                 } else if (i->second.m_fnc)
164                 {
165                         if (i->second.m_context.size())
166                         {
167                                 std::multimap<std::string,ePythonKeyBinding>::const_iterator
168                                         k = m_python_keys.lower_bound(i->second.m_context),
169                                         e = m_python_keys.upper_bound(i->second.m_context);
170                                 
171                                 for (; k != e;)
172                                 {
173                                         if (
174                                                 // (k->second.m_device == m_device) &&
175                                                 (k->second.m_key == key) &&
176                                                 (k->second.m_flags & (1<<flags)))
177                                         {
178                                                 PyObject *pArgs = PyTuple_New(2);
179                                                 PyTuple_SetItem(pArgs, 0, PyString_FromString(k->first.c_str()));
180                                                 PyTuple_SetItem(pArgs, 1, PyString_FromString(k->second.m_action.c_str()));
181                                                 ++k;
182                                                 Py_INCREF(i->second.m_fnc);
183                                                 call_list.push_back(std::pair<PyObject*,PyObject*>(i->second.m_fnc, pArgs));
184                                         } else
185                                                 ++k;
186                                 }
187                         } else
188                         {
189                                 PyObject *pArgs = PyTuple_New(2);
190                                 PyTuple_SetItem(pArgs, 0, PyInt_FromLong(key));
191                                 PyTuple_SetItem(pArgs, 1, PyInt_FromLong(flags));
192                                 Py_INCREF(i->second.m_fnc);
193                                 call_list.push_back(std::pair<PyObject*,PyObject*>(i->second.m_fnc, pArgs));
194                         }
195                 }
196         }
197
198         for (std::list<std::pair<PyObject*,PyObject*> >::iterator i(call_list.begin()); i != call_list.end(); ++i)
199         {
200                 ePython::call(i->first, i->second);
201                 Py_DECREF(i->first);
202                 Py_DECREF(i->second);
203         }
204 }
205
206 eAutoInitPtr<eActionMap> init_eActionMap(eAutoInitNumbers::actions, "eActionMap");