summaryrefslogtreecommitdiff
path: root/ivi-layermanagement-examples/simple-weston-client/src/simple-weston-client.c
blob: 5b2c96074db1df38559a0b05b5ede0d08e3cbcee (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
/*
 * Copyright (C) 2015 Advanced Driver Information Technology Joint Venture GmbH
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>

#include <wayland-cursor.h>
#include <ivi-application-client-protocol.h>

typedef struct _BkGndSettings
{
    uint32_t surface_id;
    uint32_t surface_width;
    uint32_t surface_height;
    uint32_t surface_stride;
}BkGndSettingsStruct;

typedef struct _WaylandContext {
    struct wl_display       *wl_display;
    struct wl_registry      *wl_registry;
    struct wl_compositor    *wl_compositor;
    struct wl_shm           *wl_shm;
    struct wl_seat          *wl_seat;
    struct wl_pointer       *wl_pointer;
    struct wl_surface       *wl_pointer_surface;
    struct ivi_application  *ivi_application;
    BkGndSettingsStruct     *bkgnd_settings;
    struct wl_surface       *wlBkgndSurface;
    struct ivi_surface      *ivi_surf;
    struct wl_buffer        *wlBkgndBuffer;
    struct wl_cursor_theme  *cursor_theme;
    struct wl_cursor        *cursor;
    void                    *bkgnddata;
    uint32_t                formats;
}WaylandContextStruct;

static const char *left_ptrs[] = {
    "left_ptr",
    "default",
    "top_left_arrow",
    "left-arrow"
};

static BkGndSettingsStruct*
get_bkgnd_settings(void)
{
    BkGndSettingsStruct* bkgnd_settings;
    char *option;
    char *end;

    bkgnd_settings =
            (BkGndSettingsStruct*)calloc(1, sizeof(BkGndSettingsStruct));

    option = getenv("IVI_CLIENT_SURFACE_ID");
    if(option)
        bkgnd_settings->surface_id = strtol(option, &end, 0);
    else
        bkgnd_settings->surface_id = 10;

    bkgnd_settings->surface_width = 1;
    bkgnd_settings->surface_height = 1;
    bkgnd_settings->surface_stride = bkgnd_settings->surface_width * 4;

    return bkgnd_settings;
}

static void
shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
{
    WaylandContextStruct* wlcontext = data;

    wlcontext->formats |= (1 << format);
}

static struct wl_shm_listener shm_listenter = {
    shm_format
};

static int create_cursors(WaylandContextStruct* wlcontext) {
    int size = 32;
    unsigned int j;

    wlcontext->cursor_theme = wl_cursor_theme_load(NULL, size, wlcontext->wl_shm);
    if (!wlcontext->cursor_theme) {
        fprintf(stderr, "could not load default theme\n");
        return -1;
    }

    wlcontext->cursor = NULL;

    for (j = 0; !wlcontext->cursor && j < 4; ++j)
        wlcontext->cursor = wl_cursor_theme_get_cursor(wlcontext->cursor_theme, left_ptrs[j]);

    if (!wlcontext->cursor)
    {
        fprintf(stderr, "could not load cursor '%s'\n", left_ptrs[j]);
        return -1;
    }

    return 0;
}

static int set_pointer_image(WaylandContextStruct* wlcontext)
{
    struct wl_cursor *cursor = NULL;
    struct wl_cursor_image *image = NULL;
    struct wl_buffer *buffer = NULL;

    if (!wlcontext->wl_pointer || !wlcontext->cursor) {
        fprintf(stderr, "no wl_pointer or no wl_cursor\n");
        return -1;
    }

    cursor = wlcontext->cursor;
    image = cursor->images[0];
    buffer = wl_cursor_image_get_buffer(image);
    if (!buffer) {
        fprintf(stderr, "buffer for cursor not available\n");
        return -1;
    }

    wl_pointer_set_cursor(wlcontext->wl_pointer, 0,
                          wlcontext->wl_pointer_surface,
                          image->hotspot_x, image->hotspot_y);
    wl_surface_attach(wlcontext->wl_pointer_surface, buffer, 0, 0);
    wl_surface_damage(wlcontext->wl_pointer_surface, 0, 0,
                      image->width, image->height);
    wl_surface_commit(wlcontext->wl_pointer_surface);

    return 0;
}

static void
PointerHandleEnter(void *data, struct wl_pointer *wlPointer, uint32_t serial,
                   struct wl_surface *wlSurface, wl_fixed_t sx, wl_fixed_t sy)
{
    WaylandContextStruct *wlcontext = (WaylandContextStruct*)data;

    set_pointer_image(wlcontext);

    printf("ENTER EGLWLINPUT PointerHandleEnter: x(%d), y(%d)\n",
           wl_fixed_to_int(sx), wl_fixed_to_int(sy));
}

static void
PointerHandleLeave(void *data, struct wl_pointer *wlPointer, uint32_t serial,
                   struct wl_surface *wlSurface)
{

}

static void
PointerHandleMotion(void *data, struct wl_pointer *wlPointer, uint32_t time,
                    wl_fixed_t sx, wl_fixed_t sy)
{

}

static void
PointerHandleAxis(void *data, struct wl_pointer *wlPointer, uint32_t time,
                  uint32_t axis, wl_fixed_t value)
{

}

static void
PointerHandleButton(void *data, struct wl_pointer *wlPointer, uint32_t serial,
                    uint32_t time, uint32_t button, uint32_t state)
{

}

static void
PointerHandleFrame(void *data, struct wl_pointer *wl_pointer)
{

}

static void
PointerHandleAxisSource(void *data, struct wl_pointer *wl_pointer,
                        uint32_t axis_source)
{

}

static void
PointerHandleAxisStop(void *data, struct wl_pointer *wl_pointer,
                      uint32_t time, uint32_t axis)
{

}

static void
PointerHandleAxisDiscrete(void *data, struct wl_pointer *wl_pointer,
                          uint32_t axis, int32_t discrete)
{

}

static struct wl_pointer_listener pointer_listener = {
    PointerHandleEnter,
    PointerHandleLeave,
    PointerHandleMotion,
    PointerHandleButton,
    PointerHandleAxis,
    PointerHandleFrame,
    PointerHandleAxisSource,
    PointerHandleAxisStop,
    PointerHandleAxisDiscrete
};

static void
seat_handle_capabilities(void *data, struct wl_seat *seat, uint32_t caps)
{
    WaylandContextStruct *wlcontext = (WaylandContextStruct*)data;
    struct wl_seat *wl_seat = wlcontext->wl_seat;

    if ((caps & WL_SEAT_CAPABILITY_POINTER) && !wlcontext->wl_pointer) {
        wlcontext->wl_pointer = wl_seat_get_pointer(wl_seat);
        wl_pointer_add_listener(wlcontext->wl_pointer,
                                &pointer_listener, data);
        /*create cursors*/
        create_cursors(wlcontext);
        wlcontext->wl_pointer_surface =
                wl_compositor_create_surface(wlcontext->wl_compositor);
    } else
    if (!(caps & WL_SEAT_CAPABILITY_POINTER) && wlcontext->wl_pointer) {
        wl_pointer_destroy(wlcontext->wl_pointer);
        wlcontext->wl_pointer = NULL;

        if (wlcontext->wl_pointer_surface) {
            wl_surface_destroy(wlcontext->wl_pointer_surface);
            wlcontext->wl_pointer_surface = NULL;
        }

        if (wlcontext->cursor_theme)
            wl_cursor_theme_destroy(wlcontext->cursor_theme);
    }
}

static void
seat_name(void *data, struct wl_seat *wl_seat, const char *name)
{

}

static struct wl_seat_listener seat_Listener = {
    seat_handle_capabilities,
    seat_name
};

static void
registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
                       const char *interface, uint32_t version)
{
    WaylandContextStruct* wlcontext = (WaylandContextStruct*)data;

    if (!strcmp(interface, "wl_compositor")) {
        wlcontext->wl_compositor =
                wl_registry_bind(registry, name,
                                 &wl_compositor_interface, 1);
    }
    else if (!strcmp(interface, "wl_shm")) {
        wlcontext->wl_shm =
                wl_registry_bind(registry, name, &wl_shm_interface, 1);
        wl_shm_add_listener(wlcontext->wl_shm, &shm_listenter, wlcontext);
    }
    else if (!strcmp(interface, "ivi_application")) {
        wlcontext->ivi_application =
                wl_registry_bind(registry, name,
                                 &ivi_application_interface, 1);
    }
    else if (!strcmp(interface, "wl_seat")) {
        wlcontext->wl_seat =
                wl_registry_bind(registry, name, &wl_seat_interface, 1);
        wl_seat_add_listener(wlcontext->wl_seat, &seat_Listener, data);
    }
}

static void
registry_handle_global_remove(void *data, struct wl_registry *registry,
                              uint32_t name)
{

}

static const struct wl_registry_listener registry_listener = {
    registry_handle_global,
    registry_handle_global_remove
};

int init_wayland_context(WaylandContextStruct* wlcontext)
{
    wlcontext->wl_display = wl_display_connect(NULL);
    if (NULL == wlcontext->wl_display) {
        printf("Error: wl_display_connect failed\n");
        return -1;
    }

    wlcontext->wl_registry = wl_display_get_registry(wlcontext->wl_display);
    wl_registry_add_listener(wlcontext->wl_registry,
                             &registry_listener, wlcontext);
    wl_display_roundtrip(wlcontext->wl_display);

    if (wlcontext->wl_shm == NULL) {
        fprintf(stderr, "No wl_shm global\n");
        return -1;
    }

    wl_display_roundtrip(wlcontext->wl_display);

    if (!(wlcontext->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
        fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
        return -1;
    }

    return 0;
}

void destroy_wayland_context(WaylandContextStruct* wlcontext)
{
    if(wlcontext->wl_compositor)
        wl_compositor_destroy(wlcontext->wl_compositor);

    if(wlcontext->wl_display)
        wl_display_disconnect(wlcontext->wl_display);
}

int
create_file(int size)
{
    static const char template[] = "/weston-shared-XXXXXX";
    const char *path;
    char *name;
    int fd;
    int ret;
    long flags;

    path = getenv("XDG_RUNTIME_DIR");
    if (!path) {
        errno = ENOENT;
        return -1;
    }

    name = malloc(strlen(path) + sizeof(template));
    if (!name)
        return -1;

    strcpy(name, path);
    strcat(name, template);

    fd = mkstemp(name);
    if (fd >= 0) {
        flags = fcntl(fd, F_GETFD);
        fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
        unlink(name);
    }

    free(name);

    if (fd < 0)
        return -1;

    ret = ftruncate(fd, size);
    if (ret < 0) {
        close(fd);
        return -1;
    }

    return fd;
}

static int
create_shm_buffer(WaylandContextStruct* wlcontext)
{
    struct wl_shm_pool *pool;
    BkGndSettingsStruct* bkgnd_settings = wlcontext->bkgnd_settings;

    int fd = -1;
    int size = 0;
    int stride = 0;

    stride = bkgnd_settings->surface_stride;
    size = stride * bkgnd_settings->surface_height;
    fd = create_file(size);
    if (fd < 0) {
        fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
                size);
        return -1;
    }

    wlcontext->bkgnddata =
            mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

    if (MAP_FAILED == wlcontext->bkgnddata) {
        fprintf(stderr, "mmap failed: %m\n");
        close(fd);
        return -1;
    }

    pool = wl_shm_create_pool(wlcontext->wl_shm, fd, size);
    wlcontext->wlBkgndBuffer = wl_shm_pool_create_buffer(pool, 0,
                                bkgnd_settings->surface_width,
                                bkgnd_settings->surface_height,
                                stride,
                                WL_SHM_FORMAT_ARGB8888);
    if (NULL == wlcontext->wlBkgndBuffer) {
        fprintf(stderr, "wl_shm_create_buffer failed: %m\n");
        close(fd);
        return -1;
    }

    wl_shm_pool_destroy(pool);
    close(fd);

    return 0;
}

int draw_bkgnd_surface(WaylandContextStruct* wlcontext)
{
    BkGndSettingsStruct *bkgnd_settings = wlcontext->bkgnd_settings;
    uint32_t* pixels;

    pixels = (uint32_t*)wlcontext->bkgnddata;
    *pixels = 0x0;

    wl_surface_attach(wlcontext->wlBkgndSurface, wlcontext->wlBkgndBuffer, 0, 0);
    wl_surface_damage(wlcontext->wlBkgndSurface, 0, 0,
                      bkgnd_settings->surface_width, bkgnd_settings->surface_height);
    wl_surface_commit(wlcontext->wlBkgndSurface);

    return 0;
}

int create_bkgnd_surface(WaylandContextStruct* wlcontext)
{
    BkGndSettingsStruct *bkgnd_settings = wlcontext->bkgnd_settings;

    wlcontext->wlBkgndSurface =
            wl_compositor_create_surface(wlcontext->wl_compositor);
    if (NULL == wlcontext->wlBkgndSurface) {
        printf("Error: wl_compositor_create_surface failed.\n");
        return -1;
    }

    if (create_shm_buffer(wlcontext)) {
        printf("Error: shm buffer creation failed.\n");
        return -1;
    }

    wlcontext->ivi_surf = ivi_application_surface_create(wlcontext->ivi_application,
                                                         bkgnd_settings->surface_id,
                                                         wlcontext->wlBkgndSurface);

    return 0;
}

void destroy_bkgnd_surface(WaylandContextStruct* wlcontext)
{
    if (wlcontext->ivi_surf)
        ivi_surface_destroy(wlcontext->ivi_surf);

    if (wlcontext->wlBkgndSurface)
        wl_surface_destroy(wlcontext->wlBkgndSurface);
}

int main (int argc, const char * argv[])
{
    WaylandContextStruct* wlcontext;
    BkGndSettingsStruct* bkgnd_settings;
    int ret = 0;

    /*get bkgnd settings and create shm-surface*/
    bkgnd_settings = get_bkgnd_settings();

    /*init wayland context*/
    wlcontext = (WaylandContextStruct*)calloc(1, sizeof(WaylandContextStruct));
    wlcontext->bkgnd_settings = bkgnd_settings;
    if (init_wayland_context(wlcontext)) {
        fprintf(stderr, "init_wayland_context failed\n");
        goto ErrorContext;
    }

    if (create_bkgnd_surface(wlcontext)) {
        fprintf(stderr, "create_bkgnd_surface failed\n");
        goto Error;
    }

    wl_display_roundtrip(wlcontext->wl_display);

    /*draw the bkgnd display*/
    draw_bkgnd_surface(wlcontext);

    while (ret != -1)
        ret = wl_display_dispatch(wlcontext->wl_display);

Error:
    destroy_bkgnd_surface(wlcontext);
ErrorContext:
    destroy_wayland_context(wlcontext);

    free(bkgnd_settings);
    free(wlcontext);

    return 0;
}