downstream: IVISurfaceCreator: fix a wrong surface_id was set
[profile/ivi/wayland-ivi-extension.git] / ivi-layermanagement-examples / IVISurfaceCreator / src / main.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <assert.h>
5 #include <signal.h>
6 #include <wayland-client-protocol.h>
7
8 #include "ivi-application-client-protocol.h"
9 #include "ivi-controller-client-protocol.h"
10
11 #define ASSERT(x) assert(x)
12
13 struct display {
14     struct wl_display      *display;
15     struct wl_registry     *registry;
16     struct ivi_application *ivi_application;
17     struct ivi_controller  *ivi_controller;
18     uint32_t                surface_id;
19     int32_t                 error_code;
20     int                     surface_created;
21 };
22
23 static int running = 1;
24
25 static void
26 controller_event_screen(void *data, struct ivi_controller *ivi_controller,
27                         uint32_t id_screen, struct ivi_controller_screen *screen)
28 {
29     /* do nothing */
30 }
31
32 static void
33 controller_event_layer(void *data, struct ivi_controller *ivi_controller,
34                        uint32_t id_layer)
35 {
36     /* do nothing */
37 }
38
39 static void
40 controller_event_surface(void *data, struct ivi_controller *ivi_controller,
41                          uint32_t id_surface)
42 {
43     /* do nothing */
44 }
45
46 static void
47 controller_event_error(void *data, struct ivi_controller *ivi_controller,
48                        int32_t object_id, int32_t object_type, int32_t error_code,
49                        const char *error_text)
50 {
51     struct display *d = data;
52
53     printf("IVISurfaceCreator: receive event [error=%d]\n", error_code);
54
55     if (object_type == IVI_CONTROLLER_OBJECT_TYPE_SURFACE)
56     {
57         d->error_code = error_code;
58     }
59 }
60
61 static void
62 controller_event_native_handle(void *data, struct ivi_controller *ivi_controller,
63                                struct wl_surface *surface)
64 {
65     struct display *d = data;
66
67     printf("IVISurfaceCreator: receive event [native_handle]\n");
68
69     if (d && d->ivi_application)
70     {
71         printf("IVISurfaceCreator: create ivi_surface (ID:%u)\n", d->surface_id);
72         ivi_application_surface_create(d->ivi_application,
73                                        d->surface_id, surface);
74         ++(d->surface_id);
75         d->surface_created = 1;
76     }
77 }
78
79 static const struct ivi_controller_listener controller_listener = {
80     controller_event_screen,
81     controller_event_layer,
82     controller_event_surface,
83     controller_event_error,
84     controller_event_native_handle
85 };
86
87 static void
88 registry_handle_global(void *data, struct wl_registry *registry,
89                        uint32_t name, const char *interface, uint32_t version)
90 {
91     struct display *d = data;
92
93     if (strcmp(interface, "ivi_application") == 0)
94     {
95         d->ivi_application = wl_registry_bind(registry, name,
96                                               &ivi_application_interface, 1);
97     }
98     else if (strcmp(interface, "ivi_controller") == 0)
99     {
100         d->ivi_controller = wl_registry_bind(registry, name,
101                                              &ivi_controller_interface, 1);
102         ivi_controller_add_listener(d->ivi_controller,
103                                     &controller_listener, data);
104     }
105 }
106
107 static void
108 registry_handle_global_remove(void *data, struct wl_registry *registry,
109                               uint32_t name)
110 {
111     /* do nothing */
112 }
113
114 static const struct wl_registry_listener registry_listener = {
115     registry_handle_global,
116     registry_handle_global_remove
117 };
118
119 static void
120 signal_action(int signum)
121 {
122     running = 0;
123 }
124
125 static void
126 usage(int status)
127 {
128     fprintf(stderr, "Usage: IVISurfaceCreator <Process ID> <Window Title> <IVI-Surface ID>\n");
129     exit(status);
130 }
131
132 int
133 main(int argc, char **argv)
134 {
135     struct display display = {0};
136     struct sigaction sigact;
137
138     if (argc < 4)
139     {
140         usage(EXIT_FAILURE);
141     }
142
143     int32_t process_id   = atoi(argv[1]);
144     char *  window_title = NULL;
145     char *  endptr = NULL;
146     display.surface_id   = strtoul(argv[3], &endptr, 0);
147
148     if (strlen(argv[2]) > 0)
149     {
150         window_title = strdup(argv[2]);
151     }
152
153     sigact.sa_handler = signal_action;
154     sigemptyset(&sigact.sa_mask);
155     sigact.sa_flags = SA_RESETHAND;
156     sigaction(SIGINT, &sigact, NULL);
157
158     display.display = wl_display_connect(NULL);
159     ASSERT(display.display);
160
161     display.registry = wl_display_get_registry(display.display);
162     wl_registry_add_listener(display.registry,
163                              &registry_listener, &display);
164
165     wl_display_dispatch(display.display);
166
167     ASSERT(display.ivi_application &&
168            display.ivi_controller);
169     int retry_count = 0;
170     int rc = 0;
171     do
172     {
173         ivi_controller_get_native_handle(display.ivi_controller,
174                                          process_id,
175                                          window_title);
176
177         wl_display_roundtrip(display.display);
178
179         while (!display.error_code && running && (rc != -1))
180         {
181             rc = wl_display_dispatch(display.display);
182         }
183
184         if (display.error_code == IVI_CONTROLLER_ERROR_CODE_NATIVE_HANDLE_END)
185         {
186             if (display.surface_created){
187                 break;
188             } else
189             {
190                 printf("IVISurfaceCreator: Search of native handle was ended.\n"
191                        "                   But no surface was created.\n"
192                        "                   Retry get_native_handle.\n");
193                 display.error_code = 0;
194                 sleep(1);
195             }
196         }
197
198     } while ((++retry_count < 10) && running);
199
200     wl_display_roundtrip(display.display);
201
202     printf("IVISurfaceCreator: exit\n");
203
204     wl_registry_destroy(display.registry);
205     wl_display_flush(display.display);
206     wl_display_disconnect(display.display);
207
208     if (window_title)
209         free(window_title);
210
211     return 0;
212 }