summaryrefslogtreecommitdiff
path: root/src/examples/evas/evas-multiseat-events.c
blob: b93e5161b8f617c144e6c8d3158d04546a18af4a (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
/**
 * Evas example illustrating how to handle events with multiseat information:
 *    * mouse events
 *    * keyboard events
 *    * focus events
 *
 * You'll need at least one engine built for it (excluding the buffer
 * one) and the png image loader also built. See stdout/stderr for
 * output.
 *
 * @verbatim
 * gcc -o evas_multiseat_events evas-multiseat-events.c `pkg-config --libs --cflags evas ecore ecore-evas`
 * @endverbatim
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <Ecore.h>
#include <Ecore_Evas.h>
#include <Efl.h>
#include <stdio.h>

#define WIDTH  (400)
#define HEIGHT (200)

static void
_canvas_focus_in_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Input_Device *seat;
   Efl_Input_Focus *ev;

   ev = event->info;
   seat = efl_input_device_get(ev);

   fprintf(stdout, "Object %s was focused by seat %s\n",
           evas_object_name_get(event->object),
           efl_input_device_name_get(seat));
}

static void
_hold_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Input_Device *seat;
   Efl_Input_Hold *ev;

   ev = event->info;
   seat = efl_input_device_seat_get(efl_input_device_get(ev));

   fprintf(stdout, "Hold %s at object %s from seat %s\n",
           efl_input_hold_get(ev) ? "on" : "off",
           evas_object_name_get(event->object),
           efl_input_device_name_get(seat));
}

static void
_focus_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Input_Device *seat;
   Efl_Input_Focus *ev;

   ev = event->info;
   seat = efl_input_device_get(ev);

   fprintf(stdout, "Focus %s at object %s from seat %s\n",
           event->desc == EFL_EVENT_FOCUS_IN ? "in" : "out",
           evas_object_name_get(event->object),
           efl_input_device_name_get(seat));
}

static void
_pointer_in_out_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Input_Pointer *ev;
   Efl_Input_Device *seat;

   ev = event->info;
   seat = efl_input_device_seat_get(efl_input_device_get(ev));

   fprintf(stdout, "Pointer %s at object %s from seat %s\n",
           event->desc == EFL_EVENT_POINTER_IN ? "in" : "out",
           evas_object_name_get(event->object),
           efl_input_device_name_get(seat));
}

static void
_pointer_down_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Input_Pointer *ev;
   Efl_Input_Device *seat;
   Efl_Pointer_Flags pointer_flags;
   char buf[256];

   ev = event->info;
   seat = efl_input_device_seat_get(efl_input_device_get(ev));
   pointer_flags = efl_input_pointer_button_flags_get(ev);

   if (pointer_flags & EFL_POINTER_FLAGS_TRIPLE_CLICK)
     snprintf(buf, sizeof(buf), "Triple click with button %i",
              efl_input_pointer_button_get(ev));
   else if (pointer_flags & EFL_POINTER_FLAGS_DOUBLE_CLICK)
     snprintf(buf, sizeof(buf), "Double click with button %i",
              efl_input_pointer_button_get(ev));
   else
     {
        snprintf(buf, sizeof(buf), "Pointer button %i down",
                 efl_input_pointer_button_get(ev));
        efl_canvas_object_seat_focus_add(event->object, seat);
     }

   fprintf(stdout, "%s at object %s from seat %s\n", buf,
           evas_object_name_get(event->object),
           efl_input_device_name_get(seat));
}

static void
_pointer_up_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Input_Pointer *ev;
   Efl_Input_Device *seat;

   ev = event->info;
   seat = efl_input_device_seat_get(efl_input_device_get(ev));

   fprintf(stdout, "Pointer button %i up at object %s from seat %s\n",
           efl_input_pointer_button_get(ev),
           evas_object_name_get(event->object),
           efl_input_device_name_get(seat));
}

static void
_pointer_move_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Input_Pointer *ev;
   Efl_Input_Device *seat;

   ev = event->info;
   seat = efl_input_device_seat_get(efl_input_device_get(ev));

   fprintf(stdout, "Pointer moved to %1.f,%1.f at object %s from seat %s\n",
           efl_input_pointer_value_get(ev, EFL_INPUT_VALUE_X),
           efl_input_pointer_value_get(ev, EFL_INPUT_VALUE_Y),
           evas_object_name_get(event->object),
           efl_input_device_name_get(seat));
}

static void
_pointer_wheel_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Input_Pointer *ev;
   Efl_Input_Device *seat;

   ev = event->info;
   seat = efl_input_device_seat_get(efl_input_device_get(ev));

   fprintf(stdout, "Wheel: '%i,%i' on object %s from seat %s\n",
           efl_input_pointer_wheel_direction_get(ev),
           efl_input_pointer_wheel_delta_get(ev),
           evas_object_name_get(event->object),
           efl_input_device_name_get(seat));
}

static void
_key_down_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   const Evas_Modifier *mods;
   Efl_Input_Device *seat;
   Eina_Iterator *iter;
   Efl_Input_Key *ev;
   const char *modifier;

   ev = event->info;
   mods = evas_key_modifier_get(evas_object_evas_get(event->object));
   seat = efl_input_device_seat_get(efl_input_device_get(ev));

   fprintf(stdout, "Key down: '%s' on object %s from seat %s\n",
           efl_input_key_name_get(ev),
           evas_object_name_get(event->object),
           efl_input_device_name_get(seat));
}

static void
_key_up_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Input_Key *ev;
   Efl_Input_Device *seat;

   ev = event->info;
   seat = efl_input_device_seat_get(efl_input_device_get(ev));

   fprintf(stdout, "Key up: '%s' on object %s from seat %s\n",
           efl_input_key_name_get(ev),
           evas_object_name_get(event->object),
           efl_input_device_name_get(seat));
}

static void
_dev_added_or_removed(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Input_Device *dev = event->info;

   if (efl_input_device_type_get(dev) != EFL_INPUT_DEVICE_CLASS_SEAT)
     return;

   fprintf(stdout, "The seat '%s' - description: '%s' was '%s'\n",
           efl_input_device_name_get(dev),
           efl_input_device_description_get(dev),
           event->desc == EFL_CANVAS_EVENT_DEVICE_ADDED ? "added" : "removed");
}

EFL_CALLBACKS_ARRAY_DEFINE(callbacks,
                           { EFL_EVENT_FOCUS_IN, _focus_cb },
                           { EFL_EVENT_FOCUS_OUT, _focus_cb },
                           { EFL_EVENT_KEY_DOWN, _key_down_cb },
                           { EFL_EVENT_KEY_UP, _key_up_cb },
                           { EFL_EVENT_HOLD, _hold_cb },
                           { EFL_EVENT_POINTER_IN, _pointer_in_out_cb },
                           { EFL_EVENT_POINTER_OUT, _pointer_in_out_cb },
                           { EFL_EVENT_POINTER_DOWN, _pointer_down_cb },
                           { EFL_EVENT_POINTER_UP, _pointer_up_cb },
                           { EFL_EVENT_POINTER_MOVE, _pointer_move_cb },
                           { EFL_EVENT_POINTER_WHEEL, _pointer_wheel_cb });

int
main(void)
{
   Evas_Object *bg, *red_rect, *blue_rect;
   const Eina_List *devices, *l;
   Efl_Input_Device *dev;
   Ecore_Evas *ee;
   Evas *canvas;

   if (!ecore_evas_init())
     return EXIT_FAILURE;

   ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
   if (!ee)
     goto error;

   canvas = ecore_evas_get(ee);

   bg = evas_object_rectangle_add(canvas);
   evas_object_name_set(bg, "background");
   evas_object_color_set(bg, 255, 255, 255, 255);
   evas_object_move(bg, 0, 0);
   evas_object_resize(bg, WIDTH, HEIGHT);
   evas_object_show(bg);

   red_rect = evas_object_rectangle_add(canvas);
   evas_object_name_set(red_rect, "red rect");
   evas_object_color_set(red_rect, 255, 0, 0, 255);
   evas_object_move(red_rect, WIDTH/4, HEIGHT/4);
   evas_object_resize(red_rect, WIDTH/4 - 10, HEIGHT/2);
   evas_object_show(red_rect);
   efl_event_callback_array_add(red_rect, callbacks(), NULL);

   blue_rect = evas_object_rectangle_add(canvas);
   evas_object_name_set(blue_rect, "blue rect");
   evas_object_color_set(blue_rect, 0, 0, 255, 255);
   evas_object_move(blue_rect, WIDTH/2 + 10, HEIGHT/4);
   evas_object_resize(blue_rect, WIDTH/4 - 10, HEIGHT/2);
   evas_object_show(blue_rect);
   efl_event_callback_array_add(blue_rect, callbacks(), NULL);

   devices = evas_device_list(canvas, NULL);
   EINA_LIST_FOREACH(devices, l, dev)
     {
        if (efl_input_device_type_get(dev) == EFL_INPUT_DEVICE_CLASS_SEAT)
          fprintf(stdout, "The seat '%s' - description: '%s' was 'added'\n",
                  efl_input_device_name_get(dev),
                  efl_input_device_description_get(dev));
     }

   efl_event_callback_add(canvas, EFL_EVENT_FOCUS_IN,
                           _canvas_focus_in_cb, NULL);
   efl_event_callback_add(canvas, EFL_CANVAS_EVENT_DEVICE_ADDED,
                          _dev_added_or_removed, NULL);
   efl_event_callback_add(canvas, EFL_CANVAS_EVENT_DEVICE_REMOVED,
                          _dev_added_or_removed, NULL);

   ecore_evas_show(ee);
   ecore_main_loop_begin();

   ecore_evas_free(ee);
   ecore_evas_shutdown();

   return 0;

error:
   fprintf(stderr, "At least one evas engine built and linked up to ecore evas "
                   "is required for this example to run properly.\n");
   ecore_evas_shutdown();
   return -1;
}