aboutsummaryrefslogtreecommitdiffstats
path: root/efl/ecore_input/efl.ecore_input_events.pxi
blob: 6824f9a8bd946e761205432f703d85b8ed65fc9b (plain) (blame)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# Copyright (C) 2007-2015 various contributors (see AUTHORS)
#
# This file is part of Python-EFL.
#
# Python-EFL 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 3 of the License, or (at your option) any later version.
#
# Python-EFL 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-EFL.  If not, see <http://www.gnu.org/licenses/>.


from efl.utils.conversions cimport _ctouni
from efl.ecore cimport _event_mapping_register, _event_mapping_get


cdef int _input_events_registered = 0

cdef int _ecore_input_events_register() except 0:
    global _input_events_registered

    if _input_events_registered == 0:
        _event_mapping_register(ECORE_EVENT_KEY_DOWN, EventKey)
        _event_mapping_register(ECORE_EVENT_KEY_UP, EventKey)
        _event_mapping_register(ECORE_EVENT_MOUSE_BUTTON_DOWN, EventMouseButton)
        _event_mapping_register(ECORE_EVENT_MOUSE_BUTTON_UP, EventMouseButton)
        _event_mapping_register(ECORE_EVENT_MOUSE_MOVE, EventMouseMove)
        _event_mapping_register(ECORE_EVENT_MOUSE_IN, EventMouseIO)
        _event_mapping_register(ECORE_EVENT_MOUSE_OUT, EventMouseIO)
        _event_mapping_register(ECORE_EVENT_MOUSE_WHEEL, EventMouseWheel)

        _input_events_registered = 1

    return 1


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

    assert event != NULL
    assert data != NULL, "data should not be NULL!"
    handler = <EventHandler>data
    assert type == handler.type, "handler type isn't the same as event type!"

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

    return r


cdef class InputEventHandler(EventHandler):
    """
    The event handler class

    You never instantiate this class directly, instead use one of the
    on_*_add() functions.
    """
    def __init__(self, int type, func, *args, **kargs):

        if not callable(func):
            raise TypeError("Parameter 'func' must be callable")

        event_cls = _event_mapping_get(type)
        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, _input_event_handler_cb,
                                              <void *>self))


cdef class EventKey(Event):
    """ EventKey()

    Contains information about an Ecore keyboard event.

    .. seealso :: :func:`on_key_down_add`, :func:`on_key_up_add`

    :ivar str keyname: The key name
    :ivar str key: The key symbol
    :ivar str string:
    :ivar int compose: Final string corresponding to the key symbol composed 
    :ivar int timestamp: Time when the event occurred.
    :ivar int modifiers: :ref:`Ecore_Event_Modifier` The OR combination of modifiers key
    :ivar bool same_screen: Same screen flag
    :ivar int keycode: Key scan code numeric value

    """
    cdef int _set_obj(self, void *o) except 0:
        cdef Ecore_Event_Key *obj
        obj = <Ecore_Event_Key*>o
        self.keyname = _ctouni(obj.keyname)
        self.key = _ctouni(obj.key)
        self.string = _ctouni(obj.string)
        self.compose = _ctouni(obj.compose)
        ## Can we do something with this Window opaque struct ?
        # self.window = Window(<unsigned long><void*>obj.window)
        # self.root_window = Window(<unsigned long><void*>obj.root_window)
        # self.event_window = Window(<unsigned long><void*>obj.event_window)
        self.timestamp = obj.timestamp
        self.modifiers = obj.modifiers
        self.same_screen = bool(obj.same_screen)
        self.keycode = obj.keycode
        return 1


cdef class EventPoint:
    """
    :ivar int x: x coordinate
    :ivar int y: y coordinate
    """
    def __init__(self, int x, int y):
        self.x = x
        self.y = y

    def __repr__(self):
        return "<EventPoint x=%d y=%d>" % (self.x, self.y)

cdef class EventMulti:
    """
    :ivar int device: 0 if normal mouse, 1+ for other mouse-devices (eg multi-touch - other fingers) 
    :ivar double radius: radius of press point - radius_x and y if its an ellipse (radius is the average of the 2) 
    :ivar double radius_x: 
    :ivar double radius_y: 
    :ivar double pressure: 1.0 == normal, > 1.0 == more, 0.0 == none 
    :ivar double angle: relative to perpendicular (0.0 == perpendicular), in degrees 
    :ivar double x: with sub-pixel precision, if available 
    :ivar double y:  with sub-pixel precision, if available 
    :ivar double root_x:  with sub-pixel precision, if available 
    :ivar double root_y:  with sub-pixel precision, if available 
    """
    def __init__(self, int device, double radius, double radius_x, double radius_y,
                 double pressure, double angle, double x, double y,
                 double root_x, double root_y):
        self.device = device
        self.radius = radius
        self.radius_x = radius_x
        self.radius_y = radius_y
        self.pressure = pressure
        self.angle = angle
        self.x = x
        self.y = y
        self.root_x = root_x
        self.root_y = root_y

    def __repr__(self):
        return "<EventMulti>"


cdef class EventMouseButton(Event):
    """ EventMouseButton()

    Contains information about an Ecore mouse button event.

    .. seealso :: :func:`on_mouse_button_down_add`, :func:`on_mouse_button_up_add`

    :ivar int timestamp: Time when the event occurred
    :ivar int modifiers: :ref:`Ecore_Event_Modifier` The OR combination of modifiers key
    :ivar int buttons: The button that was used
    :ivar bool double_click: Double click event
    :ivar bool triple_click: Triple click event
    :ivar bool same_screen: Same screen flag 
    :ivar int x: x coordinate relative to window where event happened 
    :ivar int y: y coordinate relative to window where event happened 
    :ivar EventPoint root: :class:`EventPoint` Coordinates relative to root window

    """
    cdef int _set_obj(self, void *o) except 0:
        cdef Ecore_Event_Mouse_Button *obj
        obj = <Ecore_Event_Mouse_Button*>o
        # self.window = Window(<unsigned long><void*>obj.window)
        # self.root_window = Window(<unsigned long><void*>obj.root_window)
        # self.event_window = Window(<unsigned long><void*>obj.event_window)
        self.timestamp = obj.timestamp
        self.modifiers = obj.modifiers
        self.buttons = obj.buttons
        self.double_click = bool(obj.double_click)
        self.triple_click = bool(obj.triple_click)
        self.same_screen = bool(obj.same_screen)
        self.x = obj.x
        self.y = obj.y
        self.root = EventPoint(obj.root.x, obj.root.y)
        self.multi = EventMulti(obj.multi.device, obj.multi.radius,
                                obj.multi.radius_x, obj.multi.radius_y,
                                obj.multi.pressure, obj.multi.angle,
                                obj.multi.x, obj.multi.y,
                                obj.multi.root.x, obj.multi.root.y)
        return 1


cdef class EventMouseMove(Event):
    """ EventMouseMove()

    Contains information about an Ecore mouse move event.

    .. seealso :: :func:`on_mouse_move_add`

    :ivar int timestamp: Time when the event occurred
    :ivar int modifiers: :ref:`Ecore_Event_Modifier` The OR combination of modifiers key
    :ivar bool same_screen: Same screen flag 
    :ivar int x: x coordinate relative to window where event happened 
    :ivar int y: y coordinate relative to window where event happened 
    :ivar EventPoint root: Coordinates relative to root window

    """
    cdef int _set_obj(self, void *o) except 0:
        cdef Ecore_Event_Mouse_Move *obj
        obj = <Ecore_Event_Mouse_Move *>o
        # self.window = Window(<unsigned long><void*>obj.window)
        # self.root_window = Window(<unsigned long><void*>obj.root_window)
        # self.event_window = Window(<unsigned long><void*>obj.event_window)
        self.timestamp = obj.timestamp
        self.modifiers = obj.modifiers
        self.same_screen = bool(obj.same_screen)
        self.x = obj.x
        self.y = obj.y
        self.root = EventPoint(obj.root.x, obj.root.y)
        self.multi = EventMulti(obj.multi.device, obj.multi.radius,
                                obj.multi.radius_x, obj.multi.radius_y,
                                obj.multi.pressure, obj.multi.angle,
                                obj.multi.x, obj.multi.y,
                                obj.multi.root.x, obj.multi.root.y)
        return 1


cdef class EventMouseIO(Event):
    """ EventMouseIO()

    Contains information about an Ecore mouse input/output event.

    .. seealso :: :func:`on_mouse_in_add`, :func:`on_mouse_out_add`

    :ivar int timestamp: Time when the event occurred
    :ivar int modifiers: :ref:`Ecore_Event_Modifier` The OR combination of modifiers key
    :ivar int x: x coordinate relative to window where event happened 
    :ivar int y: y coordinate relative to window where event happened 

    """
    cdef int _set_obj(self, void *o) except 0:
        cdef Ecore_Event_Mouse_IO *obj
        obj = <Ecore_Event_Mouse_IO *>o
        # self.window = Window(<unsigned long><void*>obj.window)
        # self.event_window = Window(<unsigned long><void*>obj.event_window)
        self.timestamp = obj.timestamp
        self.modifiers = obj.modifiers
        self.x = obj.x
        self.y = obj.y
        return 1


cdef class EventMouseWheel(Event):
    """ EventMouseWheel()

    Contains information about an Ecore mouse wheel event.

    .. seealso :: :func:`on_mouse_wheel_add`

    :ivar int timestamp: Time when the event occurred
    :ivar int modifiers: :ref:`Ecore_Event_Modifier` The OR combination of modifiers key
    :ivar bool same_screen: Same screen flag 
    :ivar int direction: Orientation of the wheel (horizontal/vertical)  
    :ivar int z: Value of the wheel event (+1/-1) 
    :ivar int x: x coordinate relative to window where event happened 
    :ivar int y: y coordinate relative to window where event happened 
    :ivar EventPoint root: Coordinates relative to root window. 

    """
    cdef int _set_obj(self, void *o) except 0:
        cdef Ecore_Event_Mouse_Wheel *obj
        obj = <Ecore_Event_Mouse_Wheel *>o
        # self.window = Window(<unsigned long><void*>obj.window)
        # self.root_window = Window(<unsigned long><void*>obj.root_window)
        # self.event_window = Window(<unsigned long><void*>obj.event_window)
        self.timestamp = obj.timestamp
        self.modifiers = obj.modifiers
        self.same_screen = bool(obj.same_screen)
        self.direction = obj.direction
        self.z = obj.z
        self.x = obj.x
        self.y = obj.y
        self.root = EventPoint(obj.root.x, obj.root.y)
        return 1


def on_key_down_add(func, *args, **kargs):
    """
    Creates an ecore event handler for the ECORE_EVENT_KEY_DOWN event.

    :return: :class:`InputEventHandler`

    .. seealso:: :class:`EventKey`
    """
    return InputEventHandler(ECORE_EVENT_KEY_DOWN, func, *args, **kargs)

def on_key_up_add(func, *args, **kargs):
    """
    Creates an ecore event handler for the ECORE_EVENT_KEY_UP event.

    :return: :class:`InputEventHandler`

    .. seealso:: :class:`EventKey`
    """
    return InputEventHandler(ECORE_EVENT_KEY_UP, func, *args, **kargs)

def on_mouse_button_down_add(func, *args, **kargs):
    """
    Creates an ecore event handler for the ECORE_EVENT_MOUSE_BUTTON_DOWN event.

    :return: :class:`InputEventHandler`

    .. seealso:: :class:`EventMouseButton`
    """
    return InputEventHandler(ECORE_EVENT_MOUSE_BUTTON_DOWN, func, *args, **kargs)

def on_mouse_button_up_add(func, *args, **kargs):
    """
    Create an ecore event handler for the ECORE_EVENT_MOUSE_BUTTON_UP event.

    :return: :class:`InputEventHandler`

    .. seealso:: :class:`EventMouseButton`
    """
    return InputEventHandler(ECORE_EVENT_MOUSE_BUTTON_UP, func, *args, **kargs)

def on_mouse_move_add(func, *args, **kargs):
    """
    Create an ecore event handler for the ECORE_EVENT_MOUSE_MOVE event.

    :return: :class:`InputEventHandler`

    .. seealso:: :class:`EventMouseMove`
    """
    return InputEventHandler(ECORE_EVENT_MOUSE_MOVE, func, *args, **kargs)

def on_mouse_in_add(func, *args, **kargs):
    """
    Create an ecore event handler for the ECORE_EVENT_MOUSE_IN event.

    :return: :class:`InputEventHandler`

    .. seealso:: :class:`EventMouseIO`
    """
    return InputEventHandler(ECORE_EVENT_MOUSE_IN, func, *args, **kargs)

def on_mouse_out_add(func, *args, **kargs):
    """
    Create an ecore event handler for the ECORE_EVENT_MOUSE_OUT event.

    :return: :class:`InputEventHandler`

    .. seealso:: :class:`EventMouseIO`
    """
    return InputEventHandler(ECORE_EVENT_MOUSE_OUT, func, *args, **kargs)

def on_mouse_wheel_add(func, *args, **kargs):
    """
    Create an ecore event handler for the ECORE_EVENT_MOUSE_WHEEL event.

    :return: :class:`InputEventHandler`

    .. seealso:: :class:`EventMouseWheel`
    """
    return InputEventHandler(ECORE_EVENT_MOUSE_WHEEL, func, *args, **kargs)