summaryrefslogtreecommitdiff
path: root/efl/ecore/efl.ecore_events.pxi
blob: b84ebabe9b4d0020690ff4f0b88c1578e8641c01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# Copyright (C) 2007-2008 Gustavo Sverzut Barbieri
#
# This file is part of Python-Ecore.
#
# Python-Ecore is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# Python-Ecore is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this Python-Ecore.  If not, see <http://www.gnu.org/licenses/>.

# This file is included verbatim by c_ecore.pyx


cdef object _event_type_mapping = dict()

def _event_mapping_register(int type, cls):
    if type in _event_type_mapping:
        raise ValueError("event type '%d' already registered." % type)
    if not issubclass(cls, Event):
        raise TypeError("cls (%s) must be subclass of Event" % cls)
    _event_type_mapping[type] = cls


def _event_mapping_unregister(int type):
    _event_type_mapping.pop(type)


cdef Eina_Bool event_handler_cb(void *data, int type, void *event) with gil:
    cdef EventHandler handler
    cdef Eina_Bool r

    assert event != NULL
    assert data != NULL
    handler = <EventHandler>data
    assert type == handler.type

    try:
        r = handler._exec(event)
    except Exception, e:
        traceback.print_exc()
        r = 0

    if not r:
        handler.delete()
    return r


cdef class Event(object):
    def __init__(self):
        if type(self) is Event:
            raise TypeError("Must not instantiate Event, but subclasses")

    def __str__(self):
        attrs = []
        for attr in dir(self):
            if not attr.startswith("_"):
                attrs.append("%s=%r" % (attr, getattr(self, attr)))
        return "%s(%s)" % (self.__class__.__name__, ", ".join(attrs))

    cdef int _set_obj(self, void *obj) except 0:
        raise NotImplementedError("Event._set_obj() not implemented.")


cdef class EventHandler(object):
    def __init__(self, int type, func, *args, **kargs):
        "@parm: B{type} event type, as registered with ecore_event_type_new()."
        if not callable(func):
            raise TypeError("Parameter 'func' must be callable")
        event_cls = _event_type_mapping.get(type, None)
        if event_cls is None:
            raise ValueError("Unknow Ecore_Event type %d" % type)
        self.type = type
        self.event_cls = event_cls
        self.func = func
        self.args = args
        self.kargs = kargs
        self._set_obj(ecore_event_handler_add(type, event_handler_cb,
                                              <void *>self))

    def __str__(self):
        return "%s(type=%d, func=%s, args=%s, kargs=%s, event_cls=%s)" % \
               (self.__class__.__name__, self.type,
                self.func, self.args, self.kargs, self.event_cls)

    def __repr__(self):
        return ("%s(%#x, type=%d, func=%s, args=%s, kargs=%s, event_cls=%s, "
                "Ecore_Event_Handler=%#x, refcount=%d)") % \
               (self.__class__.__name__, <unsigned long><void *>self,
                self.type, self.func, self.args, self.kargs, self.event_cls,
                <unsigned long>self.obj, PY_REFCOUNT(self))

    def __dealloc__(self):
        if self.obj != NULL:
            ecore_event_handler_del(self.obj)

    cdef int _set_obj(self, Ecore_Event_Handler *obj) except 0:
        assert self.obj == NULL, "Object must be clean"
        assert obj != NULL, "Cannot set NULL as object"
        self.obj = obj
        Py_INCREF(self)
        return 1

    cdef int _unset_obj(self) except 0:
        if self.obj != NULL:
            ecore_event_handler_del(self.obj)
            self.obj = NULL
            self.event_cls = None
            self.func = None
            self.args = None
            self.kargs = None
            Py_DECREF(self)
        return 1

    cdef Eina_Bool _exec(self, void *event) except 2:
        cdef Event e
        e = self.event_cls()
        e._set_obj(event)
        return bool(self.func(e, *self.args, **self.kargs))

    def delete(self):
        if self.obj != NULL:
            self._unset_obj()

    def stop(self):
        self.delete()


def event_handler_add(int type, func, *args, **kargs):
    return EventHandler(type, func, *args, **kargs)


cdef class EventSignalUser(Event):
    cdef int _set_obj(self, void *o) except 0:
        cdef Ecore_Event_Signal_User *obj
        obj = <Ecore_Event_Signal_User*>o
        self.number = obj.number
        return 1

    def __str__(self):
        return "%s(number=%d)" % (self.__class__.__name__, self.number)

    def __repr__(self):
        return "%s(number=%d)" % (self.__class__.__name__, self.number)


cdef class EventSignalUser1(EventSignalUser):
    def __str__(self):
        return "%s()" % (self.__class__.__name__,)

    def __repr__(self):
        return "%s()" % (self.__class__.__name__,)


cdef class EventSignalUser2(EventSignalUser):
    def __str__(self):
        return "%s()" % (self.__class__.__name__,)

    def __repr__(self):
        return "%s()" % (self.__class__.__name__,)


cdef class EventHandlerSignalUser(EventHandler):
    def __init__(self, func, *args, **kargs):
        EventHandler.__init__(self, ECORE_EVENT_SIGNAL_USER,
                              func, *args, **kargs)

    cdef Eina_Bool _exec(self, void *event) except 2:
        cdef Ecore_Event_Signal_User *obj = <Ecore_Event_Signal_User *>event
        cdef EventSignalUser e
        if obj.number == 1:
            e = EventSignalUser1()
        elif obj.number == 2:
            e = EventSignalUser2()
        else:
            e = EventSignalUser()
        e._set_obj(event)
        return bool(self.func(e, *self.args, **self.kargs))


def on_signal_user(func, *args, **kargs):
    return EventHandlerSignalUser(func, *args, **kargs)


cdef class EventSignalHup(Event):
    cdef int _set_obj(self, void *o) except 0:
        return 1

    def __str__(self):
        return "%s()" % (self.__class__.__name__,)

    def __repr__(self):
        return "%s()" % (self.__class__.__name__,)


def on_signal_hup(func, *args, **kargs):
    return EventHandler(ECORE_EVENT_SIGNAL_HUP, func, *args, **kargs)


cdef class EventSignalExit(Event):
    cdef int _set_obj(self, void *o) except 0:
        cdef Ecore_Event_Signal_Exit *obj
        obj = <Ecore_Event_Signal_Exit*>o
        self.interrupt = bool(obj.interrupt)
        self.quit = bool(obj.quit)
        self.terminate = bool(obj.terminate)
        return 1

    def __str__(self):
        return "%s(interrupt=%s, quit=%s, terminate=%s)" % \
            (self.__class__.__name__, self.interrupt, self.quit, self.terminate)

    def __repr__(self):
        return "%s(interrupt=%s, quit=%s, terminate=%s)" % \
            (self.__class__.__name__, self.interrupt, self.quit, self.terminate)


cdef class EventSignalQuit(EventSignalExit):
    def __str__(self):
        return "%s()" % (self.__class__.__name__,)

    def __repr__(self):
        return "%s()" % (self.__class__.__name__,)


cdef class EventSignalInterrupt(EventSignalExit):
    def __str__(self):
        return "%s()" % (self.__class__.__name__,)

    def __repr__(self):
        return "%s()" % (self.__class__.__name__,)


cdef class EventSignalTerminate(EventSignalExit):
    def __str__(self):
        return "%s()" % (self.__class__.__name__,)

    def __repr__(self):
        return "%s()" % (self.__class__.__name__,)


cdef class EventHandlerSignalExit(EventHandler):
    def __init__(self, func, *args, **kargs):
        EventHandler.__init__(self, ECORE_EVENT_SIGNAL_EXIT,
                              func, *args, **kargs)

    cdef Eina_Bool _exec(self, void *event) except 2:
        cdef Ecore_Event_Signal_Exit *obj = <Ecore_Event_Signal_Exit *>event
        cdef EventSignalExit e
        if obj.terminate:
            e = EventSignalTerminate()
        elif obj.interrupt:
            e = EventSignalInterrupt()
        elif obj.quit:
            e = EventSignalQuit()
        else:
            e = EventSignalExit()
        e._set_obj(event)
        return bool(self.func(e, *self.args, **self.kargs))


def on_signal_exit(func, *args, **kargs):
    return EventHandlerSignalExit(func, *args, **kargs)


cdef class EventSignalPower(Event):
    cdef int _set_obj(self, void *o) except 0:
        return 1

    def __str__(self):
        return "%s()" % (self.__class__.__name__,)

    def __repr__(self):
        return "%s()" % (self.__class__.__name__,)


def on_signal_power(func, *args, **kargs):
    return EventHandler(ECORE_EVENT_SIGNAL_POWER, func, *args, **kargs)


cdef class EventSignalRealtime(Event):
    cdef int _set_obj(self, void *o) except 0:
        cdef Ecore_Event_Signal_Realtime *obj
        obj = <Ecore_Event_Signal_Realtime*>o
        self.num = obj.num
        return 1

    def __str__(self):
        return "%s(num=%d)" % (self.__class__.__name__, self.num)

    def __repr__(self):
        return "%s(num=%d)" % (self.__class__.__name__, self.num)


def on_signal_realtime(func, *args, **kargs):
    return EventHandler(ECORE_EVENT_SIGNAL_REALTIME, func, *args, **kargs)


cdef class CustomEvent(Event):
    cdef int _set_obj(self, void *obj):
        self.obj = <object>obj
        return 1

def event_type_new(cls):
    cdef int type

    type = ecore_event_type_new()

    _event_mapping_register(type, cls)

    return type


cdef void _event_free_cb(void *data, void *event) with gil:
    cdef QueuedEvent ev

    ev = <QueuedEvent>data
    ev._free()


cdef class QueuedEvent(object):
    def __init__(self, int type, *args):
        self.args = args
        self._set_obj(ecore_event_add(type, <void *>self.args, _event_free_cb,
                      <void*>self))

    cdef int _set_obj(self, Ecore_Event *ev):
        assert self.obj == NULL, "Object must be clean"
        assert ev != NULL, "Cannot set NULL as object"
        self.obj = ev
        Py_INCREF(self)
        return 1

    cdef int _unset_obj(self):
        if self.obj != NULL:
            self.obj = NULL
            self.args = None
            Py_DECREF(self)
        return 1

    def _free(self):
        self._unset_obj()

    def delete(self):
        ecore_event_del(self.obj)

def event_add(int type, *args):
    return QueuedEvent(type, args)