summaryrefslogtreecommitdiff
path: root/ivi-layermanagement-api/ilmClient/src/ilm_client_wayland_platform.c
blob: 254e5f9ac2a623d6d14e816e1e3e4447e1444ff0 (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/**************************************************************************
 *
 * Copyright (C) 2013 DENSO CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <unistd.h>
#include <signal.h>
#include <poll.h>
#include "ilm_client.h"
#include "ilm_client_platform.h"
#include "wayland-util.h"
#include "ivi-application-client-protocol.h"

static ilmErrorTypes wayland_getScreenResolution(t_ilm_uint screenID,
                         t_ilm_uint* pWidth, t_ilm_uint* pHeight);
static ilmErrorTypes wayland_surfaceCreate(t_ilm_nativehandle nativehandle,
                         t_ilm_int width, t_ilm_int height,
                         ilmPixelFormat pixelFormat,
                         t_ilm_surface* pSurfaceId);
static ilmErrorTypes wayland_surfaceRemove(const t_ilm_surface surfaceId);
static ilmErrorTypes wayland_surfaceRemoveNativeContent(
                         t_ilm_surface surfaceId);
static ilmErrorTypes wayland_surfaceSetNativeContent(
                         t_ilm_nativehandle nativehandle,
                         t_ilm_int width, t_ilm_int height,
                         ilmPixelFormat pixelFormat,
                         t_ilm_surface surfaceId);
static ilmErrorTypes wayland_UpdateInputEventAcceptanceOn(
                         t_ilm_surface surfaceId,
                         ilmInputDevice devices,
                         t_ilm_bool acceptance);
static ilmErrorTypes wayland_init(t_ilm_nativedisplay nativedisplay);
static void wayland_destroy(void);
static ilmErrorTypes wayland_surfaceInitialize(t_ilm_surface *pSurfaceId);

void init_ilmClientPlatformTable(void)
{
    gIlmClientPlatformFunc.getScreenResolution =
        wayland_getScreenResolution;
    gIlmClientPlatformFunc.surfaceCreate =
        wayland_surfaceCreate;
    gIlmClientPlatformFunc.surfaceRemove =
        wayland_surfaceRemove;
    gIlmClientPlatformFunc.surfaceRemoveNativeContent =
        wayland_surfaceRemoveNativeContent;
    gIlmClientPlatformFunc.surfaceSetNativeContent =
        wayland_surfaceSetNativeContent;
    gIlmClientPlatformFunc.UpdateInputEventAcceptanceOn =
        wayland_UpdateInputEventAcceptanceOn;
    gIlmClientPlatformFunc.init =
        wayland_init;
    gIlmClientPlatformFunc.destroy =
        wayland_destroy;
    gIlmClientPlatformFunc.surfaceInitialize =
        wayland_surfaceInitialize;
}

struct surface_context {
    struct ivi_surface *surface;
    t_ilm_uint id_surface;
    struct ilmSurfaceProperties prop;

    struct wl_list link;
};

struct screen_context {
    struct wl_output *output;
    t_ilm_uint id_screen;

    uint32_t width;
    uint32_t height;

    struct wl_list link;
};

struct ilm_client_context {
    int32_t valid;
    struct wl_display *display;
    struct wl_registry *registry;
    struct wl_compositor *compositor;
    struct ivi_application *ivi_application;

    int32_t num_screen;
    struct wl_list list_surface;
    struct wl_list list_screen;

    uint32_t internal_id_surface;
    uint32_t name_controller;

    struct wl_registry *registry_for_output;
    struct wl_display *display_for_output;
};

static void
wayland_client_init(struct ilm_client_context *ctx)
{
    ctx->internal_id_surface = 0;
}

static uint32_t
wayland_client_gen_surface_id(struct ilm_client_context *ctx)
{
    struct surface_context *ctx_surf = NULL;
    do {
        int found = 0;
        if (wl_list_length(&ctx->list_surface) == 0) {
            ctx->internal_id_surface++;
            return ctx->internal_id_surface;
        }
        wl_list_for_each(ctx_surf, &ctx->list_surface, link) {
            if (ctx_surf->id_surface == ctx->internal_id_surface) {
                found = 1;
                break;
            }

            if (found == 0) {
                return ctx->internal_id_surface;
            }
        }
        ctx->internal_id_surface++;
    } while(1);
}

static void
output_listener_geometry(void *data,
                         struct wl_output *wl_output,
                         int32_t x,
                         int32_t y,
                         int32_t physical_width,
                         int32_t physical_height,
                         int32_t subpixel,
                         const char *make,
                         const char *model,
                         int32_t transform)
{
    struct screen_context *ctx_scrn = data;
    (void)wl_output;
    (void)x;
    (void)y;
    (void)subpixel;
    (void)make;
    (void)model;
    (void)transform;

    ctx_scrn->width = physical_width;
    ctx_scrn->height = physical_height;
}

static void
output_listener_mode(void *data,
                     struct wl_output *wl_output,
                     uint32_t flags,
                     int32_t width,
                     int32_t height,
                     int32_t refresh)
{
    (void)data;
    (void)wl_output;
    (void)flags;
    (void)width;
    (void)height;
    (void)refresh;
}

static void
output_listener_done(void *data,
                     struct wl_output *output)
{
    (void)data;
    (void)output;
}

static void
output_listener_scale(void *data,
                      struct wl_output *output,
                      int32_t factor)
{
    (void)data;
    (void)output;
    (void)factor;
}

static struct
wl_output_listener output_listener = {
    output_listener_geometry,
    output_listener_mode,
    output_listener_done,
    output_listener_scale
};

static void
registry_handle_client(void *data, struct wl_registry *registry,
                       uint32_t name, const char *interface,
                       uint32_t version)
{
    struct ilm_client_context *ctx = data;
    (void)version;

    if (strcmp(interface, "ivi_application") == 0) {
        ctx->ivi_application = wl_registry_bind(registry, name,
                                           &ivi_application_interface, 1);
        if (ctx->ivi_application == NULL) {
            fprintf(stderr, "Failed to registry bind ivi_application\n");
            return;
        }
    }
}

static const struct wl_registry_listener registry_client_listener = {
    registry_handle_client,
    NULL
};

static void
registry_handle_client_for_output(void *data, struct wl_registry *registry,
                                  uint32_t name, const char *interface,
                                  uint32_t version)
{
    struct ilm_client_context *ctx = data;
    (void)version;

    if (strcmp(interface, "wl_output") == 0) {
        struct screen_context *ctx_scrn = calloc(1, sizeof *ctx_scrn);
        if (ctx_scrn == NULL) {
            fprintf(stderr, "Failed to allocate memory for screen_context\n");
            return;
        }
        wl_list_init(&ctx_scrn->link);
        ctx_scrn->output = wl_registry_bind(registry, name,
                                           &wl_output_interface, 1);
        if (ctx_scrn->output == NULL) {
            free(ctx_scrn);
            fprintf(stderr, "Failed to registry bind wl_output\n");
            return;
        }

        if (wl_output_add_listener(ctx_scrn->output,
                                   &output_listener,
                                   ctx_scrn)) {
            free(ctx_scrn);
            fprintf(stderr, "Failed to add wl_output listener\n");
            return;
        }

        ctx_scrn->id_screen = ctx->num_screen;
        ctx->num_screen++;
        wl_list_insert(&ctx->list_screen, &ctx_scrn->link);
    }
}

static const struct wl_registry_listener registry_client_listener_for_output = {
    registry_handle_client_for_output,
    NULL
};

static struct ilm_client_context ilm_context = {0};

static void
destroy_client_resouses(void);

static void
wayland_destroy(void)
{
    struct ilm_client_context *ctx = &ilm_context;
    if (ctx->valid)
    {
        destroy_client_resouses();
        ctx->valid = 0;
    }
}

static void
destroy_client_resouses(void)
{
    struct ilm_client_context *ctx = &ilm_context;

    {
        struct surface_context *c = NULL;
        struct surface_context *n = NULL;
        wl_list_for_each_safe(c, n, &ctx->list_surface, link) {
            wl_list_remove(&c->link);
            ivi_surface_destroy(c->surface);
            free(c);
        }
    }

    {
        struct screen_context *ctx_scrn = NULL;
        struct screen_context *next = NULL;
        wl_list_for_each_safe(ctx_scrn, next, &ctx->list_screen, link) {
            if (ctx_scrn->output != NULL) {
                wl_output_destroy(ctx_scrn->output);
            }

            wl_list_remove(&ctx_scrn->link);
            free(ctx_scrn);
        }
    }

    if (ctx->ivi_application != NULL) {
        ivi_application_destroy(ctx->ivi_application);
        ctx->ivi_application = NULL;
    }

    if (ctx->registry)
    {
        wl_registry_destroy(ctx->registry);
        ctx->registry = NULL;
    }

    if (ctx->registry_for_output) {
        wl_registry_destroy(ctx->registry_for_output);
        ctx->registry_for_output = NULL;
        wl_display_disconnect(ctx->display_for_output);
        ctx->display_for_output = NULL;
    }
}

static ilmErrorTypes
wayland_init(t_ilm_nativedisplay nativedisplay)
{
    struct ilm_client_context *ctx = &ilm_context;
    memset(ctx, 0, sizeof *ctx);

    wayland_client_init(ctx);
    ctx->display = (struct wl_display*)nativedisplay;

    return ILM_SUCCESS;
}

static void
init_client(void)
{
    struct ilm_client_context *ctx = &ilm_context;

    if (ctx->display == NULL) {
	ctx->display = wl_display_connect(NULL);
    }

    ctx->num_screen = 0;

    wl_list_init(&ctx->list_screen);
    wl_list_init(&ctx->list_surface);

    ctx->registry = wl_display_get_registry(ctx->display);
    if (ctx->registry == NULL) {
        fprintf(stderr, "Failed to get registry\n");
        return;
    }
    if (wl_registry_add_listener(ctx->registry,
                             &registry_client_listener, ctx)) {
        fprintf(stderr, "Failed to add registry listener\n");
        return;
    }
    wl_display_dispatch(ctx->display);
    wl_display_roundtrip(ctx->display);

    if ((ctx->display == NULL) || (ctx->ivi_application == NULL)) {
        fprintf(stderr, "Failed to connect display at ilm_client\n");
        return;
    }

    {
        /* For output, use independent display connection.
           Because threre are duplicate wl_output bindings in a client,
           when using a display connection that is shared
           between ilmClient and ilmContol. */
        ctx->display_for_output = wl_display_connect(NULL);
        if (ctx->display_for_output == NULL) {
            fprintf(stderr, "Failed to connect display for output at ilm_client\n");
            return;
        }

        ctx->registry_for_output = wl_display_get_registry(ctx->display_for_output);
        if (ctx->registry_for_output == NULL) {
            fprintf(stderr, "Failed to get registry for output\n");
            return;
        }
        if (wl_registry_add_listener(ctx->registry_for_output,
                                     &registry_client_listener_for_output, ctx)) {
            fprintf(stderr, "Failed to add registry listener\n");
            return;
        }
        wl_display_dispatch(ctx->display_for_output);
        wl_display_roundtrip(ctx->display_for_output);
    }

    ctx->valid = 1;
}

static struct ilm_client_context*
get_client_instance(void)
{
    struct ilm_client_context *ctx = &ilm_context;
    if (ctx->valid == 0) {
        init_client();
    }

    if (ctx->valid < 0) {
        exit(0);
    }

    wl_display_roundtrip(ctx->display);

    return ctx;
}

static void
create_client_surface(struct ilm_client_context *ctx,
                      uint32_t id_surface,
                      struct ivi_surface *surface)
{
    struct surface_context *ctx_surf = NULL;

    ctx_surf = calloc(1, sizeof *ctx_surf);
    if (ctx_surf == NULL) {
        fprintf(stderr, "Failed to allocate memory for surface_context\n");
        return;
    }

    ctx_surf->surface = surface;
    ctx_surf->id_surface = id_surface;
    wl_list_init(&ctx_surf->link);
    wl_list_insert(&ctx->list_surface, &ctx_surf->link);
}

static struct surface_context*
get_surface_context_by_id(struct ilm_client_context *ctx,
                          uint32_t id_surface)
{
    struct surface_context *ctx_surf = NULL;

    wl_list_for_each(ctx_surf, &ctx->list_surface, link) {
        if (ctx_surf->id_surface == id_surface) {
            return ctx_surf;
        }
    }

    return NULL;
}

static ilmErrorTypes
wayland_getScreenResolution(t_ilm_uint screenID,
                            t_ilm_uint* pWidth,
                            t_ilm_uint* pHeight)
{
    ilmErrorTypes returnValue = ILM_FAILED;
    struct ilm_client_context *ctx = get_client_instance();

    if ((pWidth != NULL) && (pHeight != NULL))
    {
        struct screen_context *ctx_scrn;
        wl_list_for_each(ctx_scrn, &ctx->list_screen, link) {
            if (screenID == ctx_scrn->id_screen) {
                *pWidth = ctx_scrn->width;
                *pHeight = ctx_scrn->height;
                returnValue = ILM_SUCCESS;
                break;
            }
        }
    }

    return returnValue;
}

static ilmErrorTypes
wayland_surfaceCreate(t_ilm_nativehandle nativehandle,
                      t_ilm_int width,
                      t_ilm_int height,
                      ilmPixelFormat pixelFormat,
                      t_ilm_surface* pSurfaceId)
{
    ilmErrorTypes returnValue = ILM_FAILED;
    struct ilm_client_context *ctx = get_client_instance();
    uint32_t surfaceid = 0;
    struct ivi_surface *surf = NULL;
    (void)pixelFormat;
    (void)width;
    (void)height;

    if (nativehandle == 0) {
        return returnValue;
    }

    if (pSurfaceId != NULL) {
        if (*pSurfaceId == INVALID_ID) {
            surfaceid =
                wayland_client_gen_surface_id(ctx);
        }
        else {
            surfaceid = *pSurfaceId;
        }

        surf = ivi_application_surface_create(ctx->ivi_application, surfaceid,
                                         (struct wl_surface*)nativehandle);

        if (surf != NULL) {
            create_client_surface(ctx, surfaceid, surf);
            *pSurfaceId = surfaceid;
            returnValue = ILM_SUCCESS;
        }
        else {
            fprintf(stderr, "Failed to create ivi_surface\n");
        }
    }

    return returnValue;
}

static ilmErrorTypes
wayland_surfaceRemove(t_ilm_surface surfaceId)
{
    struct ilm_client_context *ctx = get_client_instance();
    struct surface_context *ctx_surf = NULL;
    struct surface_context *ctx_next = NULL;

    wl_list_for_each_safe(ctx_surf, ctx_next,
                          &ctx->list_surface,
                          link) {
        if (ctx_surf->id_surface == surfaceId) {
            ivi_surface_destroy(ctx_surf->surface);
            wl_list_remove(&ctx_surf->link);
            free(ctx_surf);
            break;
        }
    }

    return ILM_SUCCESS;
}

static ilmErrorTypes
wayland_surfaceRemoveNativeContent(t_ilm_surface surfaceId)
{
    (void)surfaceId;

    /* There is no API to set native content
        as such ivi_surface_set_native. */
    return ILM_FAILED;
}

static ilmErrorTypes
wayland_surfaceSetNativeContent(t_ilm_nativehandle nativehandle,
                                t_ilm_int width,
                                t_ilm_int height,
                                ilmPixelFormat pixelFormat,
                                t_ilm_surface surfaceId)
{
    (void)nativehandle;
    (void)width;
    (void)height;
    (void)pixelFormat;
    (void)surfaceId;

    /* There is no API to set native content
        as such ivi_surface_set_native. */
    return ILM_FAILED;
}

static ilmErrorTypes
wayland_UpdateInputEventAcceptanceOn(t_ilm_surface surfaceId,
                                     ilmInputDevice devices,
                                     t_ilm_bool acceptance)
{
    ilmErrorTypes returnValue = ILM_FAILED;
    struct ilm_client_context *ctx = get_client_instance();
    struct surface_context *ctx_surf = NULL;

    ctx_surf = get_surface_context_by_id(ctx, (uint32_t)surfaceId);

    if (ctx_surf != NULL) {
        if (acceptance == ILM_TRUE) {
            ctx_surf->prop.inputDevicesAcceptance = devices;
        } else {
            ctx_surf->prop.inputDevicesAcceptance &= ~devices;
        }
        returnValue = ILM_SUCCESS;
    }

    return returnValue;
}

static ilmErrorTypes
wayland_surfaceInitialize(t_ilm_surface *pSurfaceId)
{
    ilmErrorTypes returnValue = ILM_FAILED;

    returnValue = wayland_surfaceCreate((t_ilm_nativehandle)NULL,
                                        100, 100, (ilmPixelFormat)NULL,
                                        (t_ilm_surface*)pSurfaceId);
    return returnValue;
}