summaryrefslogtreecommitdiff
path: root/ivi-layermanagement-examples/simple-weston-client/src/simple-weston-client.c
blob: a0632c7a6bf0ecfa1dabfaa066b2a02573f78dd1 (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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/*
 * 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 <assert.h>
#include <pthread.h>
#include <signal.h>
#include <poll.h>

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

#ifdef LIBWESTON_DEBUG_PROTOCOL
#include "weston-debug-client-protocol.h"
#define MAXSTRLEN 1024
#endif

#ifdef DLT
#include "dlt.h"

#define WESTON_DLT_APP_DESC "messages from weston debug protocol"
#define WESTON_DLT_CONTEXT_DESC "weston debug context"

#define WESTON_DLT_APP "WESN"
#define WESTON_DLT_CONTEXT "WESC"
#endif

#ifndef MIN
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
#endif

static int running = 1;

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;
#ifdef LIBWESTON_DEBUG_PROTOCOL
    struct weston_debug_v1  *debug_iface;
    struct wl_list          stream_list;
    int                     debug_fd;
    char                    thread_running;
    pthread_t               dlt_ctx_thread;
    int                     pipefd[2];
#endif
    uint8_t                 enable_cursor;
}WaylandContextStruct;

struct debug_stream {
    struct wl_list link;
    int should_bind;
    char *name;
    struct weston_debug_stream_v1 *obj;
};

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

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

static BkGndSettingsStruct*
get_bkgnd_settings_cursor_info(WaylandContextStruct* wlcontext)
{
    BkGndSettingsStruct* bkgnd_settings;
    char *option;
    char *end;

    option = getenv("IVI_CLIENT_ENABLE_CURSOR");
    if(option)
        wlcontext->enable_cursor = (uint8_t)strtol(option, &end, 0);
    else
        wlcontext->enable_cursor = 0;

    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;
}

#ifdef LIBWESTON_DEBUG_PROTOCOL
static struct debug_stream *
stream_alloc(WaylandContextStruct* wlcontext, const char *name)
{
    struct debug_stream *stream;

    stream = calloc(1, (sizeof *stream));
    if (!stream)
        return NULL;

    stream->name = strdup(name);
    if (!stream->name) {
        free(stream);
        return NULL;
    }

    stream->should_bind = 0;

    wl_list_insert(wlcontext->stream_list.prev, &stream->link);

    return stream;
}

static void
stream_destroy(struct debug_stream *stream)
{
    if (stream->obj)
        weston_debug_stream_v1_destroy(stream->obj);

    wl_list_remove(&stream->link);
    free(stream->name);
    free(stream);
}

static void
destroy_streams(WaylandContextStruct* wlcontext)
{
    struct debug_stream *stream;
    struct debug_stream *tmp;

    wl_list_for_each_safe(stream, tmp, &wlcontext->stream_list, link) {
        stream_destroy(stream);
    }
}

static void
handle_stream_complete(void *data, struct weston_debug_stream_v1 *obj)
{
    struct debug_stream *stream = data;

    assert(stream->obj == obj);

    stream_destroy(stream);
}

static void
handle_stream_failure(void *data, struct weston_debug_stream_v1 *obj,
		      const char *msg)
{
    struct debug_stream *stream = data;

    assert(stream->obj == obj);

    fprintf(stderr, "Debug stream '%s' aborted: %s\n", stream->name, msg);

    stream_destroy(stream);
}

static const struct weston_debug_stream_v1_listener stream_listener = {
    handle_stream_complete,
    handle_stream_failure
};

static void
start_streams(WaylandContextStruct* wlcontext)
{
    struct debug_stream *stream;

    wl_list_for_each(stream, &wlcontext->stream_list, link) {
        if (stream->should_bind) {
        stream->obj = weston_debug_v1_subscribe(wlcontext->debug_iface,
                            stream->name,
                            wlcontext->debug_fd);
        weston_debug_stream_v1_add_listener(stream->obj,
                            &stream_listener, stream);
        }
    }
}

static void
get_debug_streams(WaylandContextStruct* wlcontext)
{
    char *stream_names;
    char *stream;
    const char separator[2] = " ";

    stream_names = getenv("IVI_CLIENT_DEBUG_STREAM_NAMES");

    if(NULL == stream_names)
        return;

    /* get the first stream */
    stream = strtok(stream_names, separator);

    /* walk through other streams */
    while( stream != NULL ) {
        stream_alloc(wlcontext, stream);
        stream = strtok(NULL, separator);
    }
}
#endif

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 < ARRAY_SIZE(left_ptrs); ++j)
        wlcontext->cursor = wl_cursor_theme_get_cursor(wlcontext->cursor_theme, left_ptrs[j]);

    if (!wlcontext->cursor)
    {
        fprintf(stderr, "could not load any cursor\n");
        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
};

#ifdef LIBWESTON_DEBUG_PROTOCOL
static void
stream_find(WaylandContextStruct* wlcontext, const char *name, const char *desc)
{
    struct debug_stream *stream;
    wl_list_for_each(stream, &wlcontext->stream_list, link)
        if (strcmp(stream->name, name) == 0) {
            stream->should_bind = 1;
        }
}

static void
debug_advertise(void *data, struct weston_debug_v1 *debug, const char *name,
                const char *desc)
{
        WaylandContextStruct* wlcontext = data;
        stream_find(wlcontext, name, desc);
}

static const struct weston_debug_v1_listener debug_listener = {
        debug_advertise
};
#endif

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")) {
        if (wlcontext->enable_cursor) {
            wlcontext->wl_seat =
                wl_registry_bind(registry, name, &wl_seat_interface, 1);
            wl_seat_add_listener(wlcontext->wl_seat, &seat_Listener, data);
        }
    }
#ifdef LIBWESTON_DEBUG_PROTOCOL
    else if (!strcmp(interface, weston_debug_v1_interface.name)) {
        uint32_t myver;

        if (wlcontext->debug_iface || wl_list_empty(&wlcontext->stream_list))
            return;

        myver = MIN(1, version);
        wlcontext->debug_iface =
                wl_registry_bind(registry, name,
                    &weston_debug_v1_interface, myver);
        weston_debug_v1_add_listener(wlcontext->debug_iface, &debug_listener,
                                             wlcontext);
    }
#endif
}

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;
    }

#ifdef LIBWESTON_DEBUG_PROTOCOL
    if (!wl_list_empty(&wlcontext->stream_list) &&
        (wlcontext->debug_iface == NULL)) {
        fprintf(stderr, "WARNING: weston_debug protocol is not available,"
                " missed enabling --debug option to weston ?\n");
    }
#endif

    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);
}

#ifdef LIBWESTON_DEBUG_PROTOCOL
static void *
weston_dlt_thread_function(void *data)
{
    WaylandContextStruct* wlcontext;

#ifdef DLT
    /*init dlt*/
    char apid[DLT_ID_SIZE];
    char ctid[DLT_ID_SIZE];
    DLT_DECLARE_CONTEXT(weston_dlt_context)
    dlt_set_id(apid, WESTON_DLT_APP);
    dlt_set_id(ctid, WESTON_DLT_CONTEXT);

    DLT_REGISTER_APP(apid, WESTON_DLT_APP_DESC);
    DLT_REGISTER_CONTEXT(weston_dlt_context, ctid, WESTON_DLT_CONTEXT_DESC);
#endif
    wlcontext = (WaylandContextStruct*)data;
    /*make the stdin as read end of the pipe*/
    dup2(wlcontext->pipefd[0], STDIN_FILENO);

    while (running && wlcontext->thread_running)
    {
        char str[MAXSTRLEN] = {0};
        int i = -1;

        /* read from std-in(read end of pipe) till newline char*/
        do {
            i++;
            if(read(wlcontext->pipefd[0], &str[i], 1) < 0)
                printf("read failed : %s", strerror(errno));
        } while (str[i] != '\n');

        if (strcmp(str,"")!=0)
        {
#ifdef DLT
            DLT_LOG(weston_dlt_context, DLT_LOG_INFO, DLT_STRING(str));
#else
            fprintf(stderr,"%s",str);
#endif
        }
    }

#ifdef DLT
    DLT_UNREGISTER_CONTEXT(weston_dlt_context);
    DLT_UNREGISTER_APP();
#endif

    pthread_exit(NULL);
}
#endif

static void
signal_int(int signum)
{
    running = 0;
}

static int
display_poll(struct wl_display *display, short int events)
{
    int ret;
    struct pollfd pfd[1];

    pfd[0].fd = wl_display_get_fd(display);
    pfd[0].events = events;
    do {
        ret = poll(pfd, 1, -1);
    } while (ret == -1 && errno == EINTR && running);

    if(0 == running)
        ret = -1;

    return ret;
}

/* implemented local API for dispatch the default queue
 * to handle the Ctrl+C signal properly, with the wl_display_dispatch
 * the poll is continuing because the generated errno is EINTR,
 * so added running flag also to decide whether to continue polling or not */
static int
display_dispatch(struct wl_display *display)
{
    int ret;

    if (wl_display_prepare_read(display) == -1)
        return wl_display_dispatch_pending(display);

    while (1) {
        ret = wl_display_flush(display);

        if (ret != -1 || errno != EAGAIN)
            break;

        if (display_poll(display, POLLOUT) == -1) {
            wl_display_cancel_read(display);
            return -1;
        }
    }

    /* Don't stop if flushing hits an EPIPE; continue so we can read any
     * protocol error that may have triggered it. */
    if (ret < 0 && errno != EPIPE) {
        wl_display_cancel_read(display);
        return -1;
    }

    if (display_poll(display, POLLIN) == -1) {
        wl_display_cancel_read(display);
        return -1;
    }

    if (wl_display_read_events(display) == -1)
        return -1;

    return wl_display_dispatch_pending(display);
}

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

    struct sigaction sigint;
    int ret = 0;

    sigint.sa_handler = signal_int;
    sigemptyset(&sigint.sa_mask);
    sigaction(SIGINT, &sigint, NULL);
    sigaction(SIGTERM, &sigint, NULL);
    sigaction(SIGSEGV, &sigint, NULL);

    wlcontext = (WaylandContextStruct*)calloc(1, sizeof(WaylandContextStruct));

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

    /*init wayland context*/
    wlcontext->bkgnd_settings = bkgnd_settings;

#ifdef LIBWESTON_DEBUG_PROTOCOL
    /*init debug stream list*/
    wl_list_init(&wlcontext->stream_list);
    get_debug_streams(wlcontext);
    wlcontext->debug_fd = STDOUT_FILENO;
#else
    fprintf(stderr, "WARNING: weston_debug protocol is not available\n");
#endif

    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);

#ifdef LIBWESTON_DEBUG_PROTOCOL
    if (!wl_list_empty(&wlcontext->stream_list) &&
            wlcontext->debug_iface) {
        /* create the pipe b/w stdout and stdin
         * stdout - write end
         * stdin - read end
         * weston will write to stdout and the
         * dlt_ctx_thread will read from stdin */
        if((pipe(wlcontext->pipefd)) < 0)
            printf("Error in pipe() processing : %s", strerror(errno));

        dup2(wlcontext->pipefd[1], STDOUT_FILENO);

        wlcontext->thread_running = 1;
        pthread_create(&wlcontext->dlt_ctx_thread, NULL,
                weston_dlt_thread_function, wlcontext);
        start_streams(wlcontext);
    }
#endif

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

    while (running && (ret != -1))
        ret = display_dispatch(wlcontext->wl_display);

Error:
#ifdef LIBWESTON_DEBUG_PROTOCOL
    weston_debug_v1_destroy(wlcontext->debug_iface);

    while (1) {
        struct debug_stream *stream;
        int empty = 1;

        wl_list_for_each(stream, &wlcontext->stream_list, link)
            if (stream->obj) {
                empty = 0;
                break;
            }

        if (empty)
            break;

        if (wl_display_dispatch(wlcontext->wl_display) < 0)
            break;
    }

    destroy_streams(wlcontext);
    wl_display_roundtrip(wlcontext->wl_display);

    if(wlcontext->thread_running)
    {
        close(wlcontext->pipefd[1]);
        close(STDOUT_FILENO);
        wlcontext->thread_running = 0;
        pthread_join(wlcontext->dlt_ctx_thread, NULL);
        close(wlcontext->pipefd[0]);
    }
#endif

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

    free(bkgnd_settings);
    free(wlcontext);

    return 0;
}