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