Package version up to 0.8.8
[platform/core/uifw/wayland-tbm.git] / test / tbm-client-remote-provider-test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5
6 #define WL_HIDE_DEPRECATED
7 #include <wayland-client.h>
8 #include <wayland-tbm-client.h>
9 #include <wayland-tbm-int.h>
10
11 #include <tbm_surface.h>
12
13 #include "wayland-tbm-test-client-protocol.h"
14
15 #define WL_APP_C_LOG(fmt, ...)   fprintf(stderr, "[CLIENT(%d):%s] " fmt, getpid(), __func__, ##__VA_ARGS__)
16
17 typedef struct {
18         struct wayland_tbm_client *tbm_client;
19         struct wl_tbm *wl_tbm;
20         struct wl_tbm_test *wl_tbm_test;
21         struct wl_test_surface *surface;
22
23         struct wl_buffer *bufs[3];
24         int cur_buf;
25
26         int exit;
27 } AppInfoClient;
28
29 AppInfoClient gApp;
30
31 static void
32 _wl_registry_global_cb(void *data,
33                        struct wl_registry *wl_registry,
34                        uint32_t name,
35                        const char *interface,
36                        uint32_t version)
37 {
38         AppInfoClient *app = (AppInfoClient *)data;
39
40         if (!strcmp(interface, "wl_tbm_test")) {
41                 WL_APP_C_LOG("bind %s", interface);
42                 app->wl_tbm_test = wl_registry_bind(wl_registry, name,
43                                                     &wl_tbm_test_interface, 1);
44         }
45 }
46
47 static void
48 _wl_registry_global_remove_cb(void *data,
49                               struct wl_registry *wl_registry,
50                               uint32_t name)
51 {
52 }
53
54 static const struct wl_registry_listener wl_registry_impl = {
55         _wl_registry_global_cb,
56         _wl_registry_global_remove_cb,
57 };
58
59 void create_provider(AppInfoClient *app)
60 {
61         tbm_surface_h tbm_surf;
62         int i;
63
64         app->surface = wl_tbm_test_create_surface(app->wl_tbm_test);
65         wl_tbm_test_set_provider(app->wl_tbm_test, app->surface, "test");
66
67         //Make wl_buffer
68         for (i = 0; i < 3; i++) {
69                 tbm_surf = tbm_surface_create(16, 16, TBM_FORMAT_ABGR8888);
70                 app->bufs[i] = wayland_tbm_client_create_buffer(app->tbm_client, tbm_surf);
71         }
72 }
73
74 void _wl_callback_done_cb(void *data,
75                      struct wl_callback *wl_callback,
76                      uint32_t callback_data)
77 {
78         AppInfoClient *app = (AppInfoClient *)data;
79
80         wl_test_surface_attach(app->surface, app->bufs[app->cur_buf]);
81         app->cur_buf = (app->cur_buf + 1) % 3;
82
83         wl_callback_destroy(wl_callback);
84 }
85
86 static struct wl_callback_listener do_done_impl = {
87         _wl_callback_done_cb
88 };
89
90 int
91 main(int argc, char *argv[])
92 {
93         struct wl_display *dpy = NULL;
94         struct wl_registry *registry;
95         const char *dpy_name = NULL;
96         static const char *default_dpy_name = "tbm_remote";
97         int ret = 0;
98
99         struct wl_callback *wl_cb;
100
101         if (argc > 1)
102                 dpy_name = argv[1];
103         else
104                 dpy_name = default_dpy_name;
105
106         dpy = wl_display_connect(dpy_name);
107         if (!dpy) {
108                 printf("[APP] failed to connect server\n");
109                 return -1;
110         }
111
112         registry = wl_display_get_registry(dpy);
113         wl_registry_add_listener(registry, &wl_registry_impl, &gApp);
114         wl_display_roundtrip(dpy);
115         if (gApp.wl_tbm_test == NULL) {
116                 WL_APP_C_LOG("fail to bind::wl_tbm_test");
117                 return 0;
118         }
119
120         gApp.tbm_client = wayland_tbm_client_init(dpy);
121         if (!gApp.tbm_client) {
122                 WL_APP_C_LOG("fail to wayland_tbm_client_init()\n");
123                 goto finish;
124         }
125         gApp.wl_tbm = wayland_tbm_client_get_wl_tbm(gApp.tbm_client);
126
127         create_provider(&gApp);
128
129         while (ret >= 0 && gApp.exit == 0) {
130                 wl_cb = wl_display_sync(dpy);
131                 wl_callback_add_listener(wl_cb, &do_done_impl, &gApp);
132                 ret = wl_display_dispatch(dpy);
133         }
134
135 finish:
136         return 1;
137 }