ivi-shell: rewrite controller API
[platform/upstream/weston.git] / ivi-shell / ivi-shell.c
1 /*
2  * Copyright (C) 2013 DENSO CORPORATION
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 /*
24  * ivi-shell supports a type of shell for In-Vehicle Infotainment system.
25  * In-Vehicle Infotainment system traditionally manages surfaces with global
26  * identification. A protocol, ivi_application, supports such a feature
27  * by implementing a request, ivi_application::surface_creation defined in
28  * ivi_application.xml.
29  *
30  *  The ivi-shell explicitly loads a module to add business logic like how to
31  *  layout surfaces by using internal ivi-layout APIs.
32  */
33 #include "config.h"
34
35 #include <string.h>
36 #include <dlfcn.h>
37 #include <limits.h>
38 #include <assert.h>
39
40 #include "ivi-shell.h"
41 #include "ivi-application-server-protocol.h"
42 #include "ivi-layout-export.h"
43 #include "ivi-layout-private.h"
44
45 /* Representation of ivi_surface protocol object. */
46 struct ivi_shell_surface
47 {
48         struct wl_resource* resource;
49         struct ivi_shell *shell;
50         struct ivi_layout_surface *layout_surface;
51
52         struct weston_surface *surface;
53         struct wl_listener surface_destroy_listener;
54
55         uint32_t id_surface;
56
57         int32_t width;
58         int32_t height;
59
60         struct wl_list link;
61
62         struct wl_listener configured_listener;
63 };
64
65 struct ivi_shell_setting
66 {
67         char *ivi_module;
68 };
69
70 /*
71  * Implementation of ivi_surface
72  */
73
74 static void
75 surface_configure_notify(struct wl_listener *listener, void *data)
76 {
77         struct ivi_layout_surface *layout_surf =
78                 (struct ivi_layout_surface *)data;
79
80         struct ivi_shell_surface *shell_surf =
81                 container_of(listener,
82                              struct ivi_shell_surface,
83                              configured_listener);
84
85         int32_t dest_width = 0;
86         int32_t dest_height = 0;
87
88         ivi_layout_surface_get_dimension(layout_surf,
89                                          &dest_width, &dest_height);
90
91         if (shell_surf->resource)
92                 ivi_surface_send_configure(shell_surf->resource,
93                                            dest_width, dest_height);
94 }
95
96 static void
97 ivi_shell_surface_configure(struct weston_surface *, int32_t, int32_t);
98
99 static struct ivi_shell_surface *
100 get_ivi_shell_surface(struct weston_surface *surface)
101 {
102         if (surface->configure == ivi_shell_surface_configure)
103                 return surface->configure_private;
104
105         return NULL;
106 }
107
108 static void
109 ivi_shell_surface_configure(struct weston_surface *surface,
110                             int32_t sx, int32_t sy)
111 {
112         struct ivi_shell_surface *ivisurf = get_ivi_shell_surface(surface);
113         struct weston_view *view;
114         float from_x;
115         float from_y;
116         float to_x;
117         float to_y;
118
119         if (surface->width == 0 || surface->height == 0 || ivisurf == NULL)
120                 return;
121
122         view = ivi_layout_get_weston_view(ivisurf->layout_surface);
123
124         if (view == NULL)
125                 return;
126
127         if (ivisurf->width != surface->width ||
128             ivisurf->height != surface->height) {
129                 ivisurf->width  = surface->width;
130                 ivisurf->height = surface->height;
131
132                 weston_view_to_global_float(view, 0, 0, &from_x, &from_y);
133                 weston_view_to_global_float(view, sx, sy, &to_x, &to_y);
134
135                 weston_view_set_position(view,
136                                          view->geometry.x + to_x - from_x,
137                                          view->geometry.y + to_y - from_y);
138                 weston_view_update_transform(view);
139
140                 ivi_layout_surface_configure(ivisurf->layout_surface,
141                                              surface->width, surface->height);
142         }
143 }
144
145 /*
146  * The ivi_surface wl_resource destructor.
147  *
148  * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
149  */
150 static void
151 shell_destroy_shell_surface(struct wl_resource *resource)
152 {
153         struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
154         if (ivisurf != NULL) {
155                 ivisurf->resource = NULL;
156         }
157 }
158
159 /* Gets called through the weston_surface destroy signal. */
160 static void
161 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
162 {
163         struct ivi_shell_surface *ivisurf =
164                         container_of(listener, struct ivi_shell_surface,
165                                      surface_destroy_listener);
166
167         assert(ivisurf != NULL);
168
169         if (ivisurf->surface!=NULL) {
170                 ivisurf->surface->configure = NULL;
171                 ivisurf->surface->configure_private = NULL;
172                 ivisurf->surface = NULL;
173         }
174
175         wl_list_remove(&ivisurf->surface_destroy_listener.link);
176         wl_list_remove(&ivisurf->link);
177
178         if (ivisurf->resource != NULL) {
179                 wl_resource_set_user_data(ivisurf->resource, NULL);
180                 ivisurf->resource = NULL;
181         }
182         free(ivisurf);
183
184 }
185
186 /* Gets called, when a client sends ivi_surface.destroy request. */
187 static void
188 surface_destroy(struct wl_client *client, struct wl_resource *resource)
189 {
190         /*
191          * Fires the wl_resource destroy signal, and then calls
192          * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
193          */
194         wl_resource_destroy(resource);
195 }
196
197 static const struct ivi_surface_interface surface_implementation = {
198         surface_destroy,
199 };
200
201 /**
202  * Request handler for ivi_application.surface_create.
203  *
204  * Creates an ivi_surface protocol object associated with the given wl_surface.
205  * ivi_surface protocol object is represented by struct ivi_shell_surface.
206  *
207  * \param client The client.
208  * \param resource The ivi_application protocol object.
209  * \param id_surface The IVI surface ID.
210  * \param surface_resource The wl_surface protocol object.
211  * \param id The protocol object id for the new ivi_surface protocol object.
212  *
213  * The wl_surface is given the ivi_surface role and associated with a unique
214  * IVI ID which is used to identify the surface in a controller
215  * (window manager).
216  */
217 static void
218 application_surface_create(struct wl_client *client,
219                            struct wl_resource *resource,
220                            uint32_t id_surface,
221                            struct wl_resource *surface_resource,
222                            uint32_t id)
223 {
224         struct ivi_shell *shell = wl_resource_get_user_data(resource);
225         struct ivi_shell_surface *ivisurf;
226         struct ivi_layout_surface *layout_surface;
227         struct weston_surface *weston_surface =
228                 wl_resource_get_user_data(surface_resource);
229         struct wl_resource *res;
230
231         if (weston_surface_set_role(weston_surface, "ivi_surface",
232                                     resource, IVI_APPLICATION_ERROR_ROLE) < 0)
233                 return;
234
235         layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
236
237         /* check if id_ivi is already used for wl_surface*/
238         if (layout_surface == NULL){
239                 wl_resource_post_error(resource,
240                                        IVI_APPLICATION_ERROR_IVI_ID,
241                                        "surface_id is already assigned "
242                                        "by another app");
243                 return;
244         }
245
246         ivisurf = zalloc(sizeof *ivisurf);
247         if (ivisurf == NULL) {
248                 wl_resource_post_no_memory(resource);
249                 return;
250         }
251
252         wl_list_init(&ivisurf->link);
253         wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);
254
255         ivisurf->shell = shell;
256         ivisurf->id_surface = id_surface;
257
258         ivisurf->width = 0;
259         ivisurf->height = 0;
260         ivisurf->layout_surface = layout_surface;
261         ivisurf->configured_listener.notify = surface_configure_notify;
262         ivi_layout_surface_add_configured_listener(layout_surface,
263                                      &ivisurf->configured_listener);
264         /*
265          * The following code relies on wl_surface destruction triggering
266          * immediateweston_surface destruction
267          */
268         ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
269         wl_signal_add(&weston_surface->destroy_signal,
270                       &ivisurf->surface_destroy_listener);
271
272         ivisurf->surface = weston_surface;
273
274         weston_surface->configure = ivi_shell_surface_configure;
275         weston_surface->configure_private = ivisurf;
276
277         res = wl_resource_create(client, &ivi_surface_interface, 1, id);
278         if (res == NULL) {
279                 wl_client_post_no_memory(client);
280                 return;
281         }
282
283         ivisurf->resource = res;
284
285         wl_resource_set_implementation(res, &surface_implementation,
286                                        ivisurf, shell_destroy_shell_surface);
287 }
288
289 static const struct ivi_application_interface application_implementation = {
290         application_surface_create
291 };
292
293 /*
294  * Handle wl_registry.bind of ivi_application global singleton.
295  */
296 static void
297 bind_ivi_application(struct wl_client *client,
298                      void *data, uint32_t version, uint32_t id)
299 {
300         struct ivi_shell *shell = data;
301         struct wl_resource *resource;
302
303         resource = wl_resource_create(client, &ivi_application_interface,
304                                       1, id);
305
306         wl_resource_set_implementation(resource,
307                                        &application_implementation,
308                                        shell, NULL);
309 }
310
311 struct weston_view *
312 get_default_view(struct weston_surface *surface)
313 {
314         struct ivi_shell_surface *shsurf;
315         struct weston_view *view;
316
317         if (!surface || wl_list_empty(&surface->views))
318                 return NULL;
319
320         shsurf = get_ivi_shell_surface(surface);
321         if (shsurf && shsurf->layout_surface) {
322                 view = ivi_layout_get_weston_view(shsurf->layout_surface);
323                 if (view)
324                         return view;
325         }
326
327         wl_list_for_each(view, &surface->views, surface_link) {
328                 if (weston_view_is_mapped(view))
329                         return view;
330         }
331
332         return container_of(surface->views.next,
333                             struct weston_view, surface_link);
334 }
335
336 /*
337  * Called through the compositor's destroy signal.
338  */
339 static void
340 shell_destroy(struct wl_listener *listener, void *data)
341 {
342         struct ivi_shell *shell =
343                 container_of(listener, struct ivi_shell, destroy_listener);
344         struct ivi_shell_surface *ivisurf, *next;
345
346         input_panel_destroy(shell);
347
348         wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
349                 wl_list_remove(&ivisurf->link);
350                 free(ivisurf);
351         }
352
353         free(shell);
354 }
355
356 static void
357 init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell)
358 {
359         shell->compositor = compositor;
360
361         wl_list_init(&shell->ivi_surface_list);
362
363         weston_layer_init(&shell->input_panel_layer, NULL);
364 }
365
366 static int
367 ivi_shell_setting_create(struct ivi_shell_setting *dest,
368                          struct weston_compositor *compositor)
369 {
370         int result = 0;
371         struct weston_config *config = compositor->config;
372         struct weston_config_section *section;
373
374         if (NULL == dest)
375                 return -1;
376
377         section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
378
379         if (weston_config_section_get_string(section, "ivi-module",
380                                              &dest->ivi_module, NULL) != 0) {
381                 weston_log("ivi-shell: No ivi-module set in config\n");
382                 result = -1;
383         }
384
385         return result;
386 }
387
388 /*
389  * Initialization of ivi-shell.
390  */
391 WL_EXPORT int
392 module_init(struct weston_compositor *compositor,
393             int *argc, char *argv[])
394 {
395         struct ivi_shell *shell;
396         struct ivi_shell_setting setting = { };
397
398         shell = zalloc(sizeof *shell);
399         if (shell == NULL)
400                 return -1;
401
402         init_ivi_shell(compositor, shell);
403
404         shell->destroy_listener.notify = shell_destroy;
405         wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
406
407         if (input_panel_setup(shell) < 0)
408                 return -1;
409
410         if (wl_global_create(compositor->wl_display,
411                              &ivi_application_interface, 1,
412                              shell, bind_ivi_application) == NULL)
413                 return -1;
414
415         if (ivi_shell_setting_create(&setting, compositor) != 0)
416                 return -1;
417
418         ivi_layout_init_with_compositor(compositor);
419
420
421         /* Call module_init of ivi-modules which are defined in weston.ini */
422         if (load_controller_modules(compositor, setting.ivi_module, argc, argv) < 0) {
423                 free(setting.ivi_module);
424                 return -1;
425         }
426
427         free(setting.ivi_module);
428         return 0;
429 }