summaryrefslogtreecommitdiff
path: root/src/lib/ecore_wl2/ecore_wl2_seat.c
blob: 7bf26bb770f124f6dc6fb84eb8f51987f0867cf3 (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "ecore_wl2_private.h"

static void
_seat_cb_unbind(struct wl_resource *resource)
{
   Ecore_Wl2_Seat *seat;

   DBG("Seat Unbind");

   seat = wl_resource_get_user_data(resource);
   if (!seat) return;

   seat->resources = eina_list_remove(seat->resources, resource);

   if (seat->unbind_cb) seat->unbind_cb(resource);
}

static void
_seat_cb_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
{
   Ecore_Wl2_Seat *seat;
   struct wl_resource *res;

   seat = data;
   seat->id = id;

   DBG("Seat Bind");

   res = wl_resource_create(client, &wl_seat_interface, version, id);
   if (!res)
     {
        ERR("Failed to create seat resource: %m");
        return;
     }

   seat->resources = eina_list_append(seat->resources, res);

   wl_resource_set_implementation(res, seat->implementation, seat,
                                  _seat_cb_unbind);

   if (version >= WL_SEAT_NAME_SINCE_VERSION)
     wl_seat_send_name(res, seat->name);

   if (seat->bind_cb) seat->bind_cb(client, seat, version, id);
}

static void
_pointer_cb_unbind(struct wl_resource *resource)
{
   Ecore_Wl2_Pointer *ptr;

   DBG("Pointer Unbind");

   ptr = wl_resource_get_user_data(resource);
   if (!ptr) return;

   ptr->resources = eina_list_remove(ptr->resources, resource);

   /* wl_pointer_release(); */
}

static Ecore_Wl2_Pointer *
_ecore_wl2_seat_pointer_create(Ecore_Wl2_Seat *seat)
{
   Ecore_Wl2_Pointer *ptr;

   ptr = calloc(1, sizeof(Ecore_Wl2_Pointer));
   if (!ptr) return NULL;

   /* FIXME: Init pointer fields */

   ptr->seat = seat;

   return ptr;
}

static void
_ecore_wl2_seat_pointer_destroy(Ecore_Wl2_Pointer *ptr)
{
   /* FIXME: Free pointer fields */
   free(ptr);
}

static void
_keyboard_cb_unbind(struct wl_resource *resource)
{
   Ecore_Wl2_Keyboard *kbd;

   DBG("Keyboard Unbind");

   kbd = wl_resource_get_user_data(resource);
   if (!kbd) return;

   kbd->resources = eina_list_remove(kbd->resources, resource);

   /* wl_keyboard_release(); */
}

static Ecore_Wl2_Keyboard *
_ecore_wl2_seat_keyboard_create(Ecore_Wl2_Seat *seat)
{
   Ecore_Wl2_Keyboard *kbd;

   kbd = calloc(1, sizeof(Ecore_Wl2_Keyboard));
   if (!kbd) return NULL;

   /* FIXME: Init keyboard fields */

   kbd->seat = seat;

   return kbd;
}

static void
_ecore_wl2_seat_keyboard_destroy(Ecore_Wl2_Keyboard *kbd)
{
   /* FIXME: Free keyboard fields */
   free(kbd);
}

EAPI Ecore_Wl2_Seat *
ecore_wl2_seat_create(Ecore_Wl2_Display *display, const char *name, const struct wl_seat_interface *implementation, int version, Ecore_Wl2_Bind_Cb bind_cb, Ecore_Wl2_Unbind_Cb unbind_cb)
{
   Ecore_Wl2_Seat *seat;

   EINA_SAFETY_ON_NULL_RETURN_VAL(display, NULL);

   if (!name) name = "default";

   EINA_INLIST_FOREACH(display->seats, seat)
     {
        if (!strcmp(seat->name, name))
          return seat;
     }

   seat = calloc(1, sizeof(Ecore_Wl2_Seat));
   if (!seat) return NULL;

   seat->version = version;
   seat->implementation = implementation;
   seat->bind_cb = bind_cb;
   seat->unbind_cb = unbind_cb;

   eina_stringshare_replace(&seat->name, name);

   seat->global =
     wl_global_create(display->wl.display, &wl_seat_interface, seat->version,
                      seat, _seat_cb_bind);
   if (!seat->global)
     {
        ERR("Could not create seat global: %m");
        free(seat);
        return NULL;
     }

   return seat;
}

EAPI void
ecore_wl2_seat_destroy(Ecore_Wl2_Seat *seat)
{
   EINA_SAFETY_ON_NULL_RETURN(seat);

   eina_stringshare_del(seat->name);

   if (seat->pointer) _ecore_wl2_seat_pointer_destroy(seat->pointer);
   if (seat->keyboard) _ecore_wl2_seat_keyboard_destroy(seat->keyboard);

   /* NB: Hmmm, should we iterate and free resources here ?? */

   wl_global_destroy(seat->global);

   free(seat);
}

EAPI void
ecore_wl2_seat_capabilities_send(Ecore_Wl2_Seat *seat, enum wl_seat_capability caps)
{
   Eina_List *l;
   struct wl_resource *res;

   EINA_SAFETY_ON_NULL_RETURN(seat);

   EINA_LIST_FOREACH(seat->resources, l, res)
     wl_seat_send_capabilities(res, caps);
}

EAPI void
ecore_wl2_seat_pointer_release(Ecore_Wl2_Seat *seat)
{
   enum wl_seat_capability caps = 0;

   EINA_SAFETY_ON_NULL_RETURN(seat);

   seat->pointer_count--;
   if (seat->pointer_count == 0)
     {
        if (seat->pointer_count > 0)
          caps |= WL_SEAT_CAPABILITY_POINTER;
        if (seat->keyboard_count > 0)
          caps |= WL_SEAT_CAPABILITY_KEYBOARD;
        if (seat->touch_count > 0)
          caps |= WL_SEAT_CAPABILITY_TOUCH;

        ecore_wl2_seat_capabilities_send(seat, caps);
     }
}

EAPI Ecore_Wl2_Pointer *
ecore_wl2_pointer_get(Ecore_Wl2_Seat *seat)
{
   enum wl_seat_capability caps = 0;

   EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);

   if (seat->pointer_count > 0)
     caps |= WL_SEAT_CAPABILITY_POINTER;
   if (seat->keyboard_count > 0)
     caps |= WL_SEAT_CAPABILITY_KEYBOARD;
   if (seat->touch_count > 0)
     caps |= WL_SEAT_CAPABILITY_TOUCH;

   if (seat->pointer)
     {
        seat->pointer_count += 1;
        if (seat->pointer_count == 1)
          {
             caps |= WL_SEAT_CAPABILITY_POINTER;
             ecore_wl2_seat_capabilities_send(seat, caps);
          }

        return seat->pointer;
     }

   seat->pointer = _ecore_wl2_seat_pointer_create(seat);
   seat->pointer_count = 1;

   caps |= WL_SEAT_CAPABILITY_POINTER;
   ecore_wl2_seat_capabilities_send(seat, caps);

   return seat->pointer;
}

EAPI Eina_Bool
ecore_wl2_pointer_resource_create(Ecore_Wl2_Pointer *ptr, struct wl_client *client, const struct wl_pointer_interface *implementation, int version, uint32_t id)
{
   struct wl_resource *res;

   EINA_SAFETY_ON_NULL_RETURN_VAL(ptr, EINA_FALSE);

   res = wl_resource_create(client, &wl_pointer_interface, version, id);
   if (!res)
     {
        ERR("Could not create pointer resource: %m");
        wl_client_post_no_memory(client);
        return EINA_FALSE;
     }

   wl_resource_set_implementation(res, implementation, ptr, _pointer_cb_unbind);

   ptr->resources = eina_list_append(ptr->resources, res);

   /* FIXME: Hmmm, should we sent a pointer_enter to ptr->focus'd surface
    * here like weston does ? */

   return EINA_TRUE;
}

EAPI Ecore_Wl2_Keyboard *
ecore_wl2_keyboard_get(Ecore_Wl2_Seat *seat)
{
   enum wl_seat_capability caps = 0;

   EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);

   if (seat->pointer_count > 0)
     caps |= WL_SEAT_CAPABILITY_POINTER;
   if (seat->keyboard_count > 0)
     caps |= WL_SEAT_CAPABILITY_KEYBOARD;
   if (seat->touch_count > 0)
     caps |= WL_SEAT_CAPABILITY_TOUCH;

   if (seat->keyboard)
     {
        seat->keyboard_count += 1;
        if (seat->keyboard_count == 1)
          {
             caps |= WL_SEAT_CAPABILITY_KEYBOARD;
             ecore_wl2_seat_capabilities_send(seat, caps);
          }

        return seat->keyboard;
     }

   seat->keyboard = _ecore_wl2_seat_keyboard_create(seat);
   seat->keyboard_count = 1;

   caps |= WL_SEAT_CAPABILITY_KEYBOARD;
   ecore_wl2_seat_capabilities_send(seat, caps);

   return seat->keyboard;
}

EAPI Eina_Bool
ecore_wl2_keyboard_resource_create(Ecore_Wl2_Keyboard *kbd, struct wl_client *client, const struct wl_keyboard_interface *implementation, int version, uint32_t id)
{
   struct wl_resource *res;

   EINA_SAFETY_ON_NULL_RETURN_VAL(kbd, EINA_FALSE);

   res = wl_resource_create(client, &wl_keyboard_interface, version, id);
   if (!res)
     {
        ERR("Could not create keyboard resource: %m");
        wl_client_post_no_memory(client);
        return EINA_FALSE;
     }

   wl_resource_set_implementation(res, implementation, kbd, _keyboard_cb_unbind);

   kbd->resources = eina_list_append(kbd->resources, res);

   /* FIXME: Hmmm, should we sent a keyboard_enter to kbd->focus'd surface
    * here like weston does ? */

   return EINA_TRUE;
}

EAPI void
ecore_wl2_keyboard_repeat_info_set(Ecore_Wl2_Keyboard *kbd, double rate, double delay)
{
   struct wl_resource *res;
   Eina_List *l;

   EINA_SAFETY_ON_NULL_RETURN(kbd);

   kbd->repeat.rate = rate;
   kbd->repeat.delay = delay;

   EINA_LIST_FOREACH(kbd->resources, l, res)
     {
        if (wl_resource_get_version(res) >=
            WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION)
          wl_keyboard_send_repeat_info(res, rate, delay);
     }
}