blob: 5e437fa7fe482e3317680e1f49644308a045a4c3 [file] [log] [blame]
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +09001/*
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
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090035#include <string.h>
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090036#include <dlfcn.h>
37#include <limits.h>
38#include <assert.h>
39
40#include "ivi-shell.h"
41#include "ivi-application-server-protocol.h"
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +090042#include "ivi-layout-export.h"
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090043#include "ivi-layout-private.h"
44
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090045/* Representation of ivi_surface protocol object. */
46struct 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
65struct ivi_shell_setting
66{
67 char *ivi_module;
Pekka Paalanene35b2232015-02-19 17:08:44 +020068 int developermode;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090069};
70
71/*
72 * Implementation of ivi_surface
73 */
74
75static void
76surface_configure_notify(struct wl_listener *listener, void *data)
77{
78 struct ivi_layout_surface *layout_surf =
79 (struct ivi_layout_surface *)data;
80
81 struct ivi_shell_surface *shell_surf =
82 container_of(listener,
83 struct ivi_shell_surface,
84 configured_listener);
85
86 int32_t dest_width = 0;
87 int32_t dest_height = 0;
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +090088
89 ivi_layout_surface_get_dimension(layout_surf,
90 &dest_width, &dest_height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090091
92 if (shell_surf->resource)
93 ivi_surface_send_configure(shell_surf->resource,
94 dest_width, dest_height);
95}
96
97static void
98ivi_shell_surface_configure(struct weston_surface *, int32_t, int32_t);
99
100static struct ivi_shell_surface *
101get_ivi_shell_surface(struct weston_surface *surface)
102{
103 if (surface->configure == ivi_shell_surface_configure)
104 return surface->configure_private;
105
106 return NULL;
107}
108
109static void
110ivi_shell_surface_configure(struct weston_surface *surface,
111 int32_t sx, int32_t sy)
112{
113 struct ivi_shell_surface *ivisurf = get_ivi_shell_surface(surface);
114 struct weston_view *view;
115 float from_x;
116 float from_y;
117 float to_x;
118 float to_y;
119
120 if (surface->width == 0 || surface->height == 0 || ivisurf == NULL)
121 return;
122
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900123 view = ivi_layout_get_weston_view(ivisurf->layout_surface);
124
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900125 if (view == NULL)
126 return;
127
128 if (ivisurf->width != surface->width ||
129 ivisurf->height != surface->height) {
130 ivisurf->width = surface->width;
131 ivisurf->height = surface->height;
132
133 weston_view_to_global_float(view, 0, 0, &from_x, &from_y);
134 weston_view_to_global_float(view, sx, sy, &to_x, &to_y);
135
136 weston_view_set_position(view,
137 view->geometry.x + to_x - from_x,
138 view->geometry.y + to_y - from_y);
139 weston_view_update_transform(view);
140
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900141 ivi_layout_surface_configure(ivisurf->layout_surface,
142 surface->width, surface->height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900143 }
144}
145
146/*
147 * The ivi_surface wl_resource destructor.
148 *
149 * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
150 */
151static void
152shell_destroy_shell_surface(struct wl_resource *resource)
153{
154 struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
155 if (ivisurf != NULL) {
156 ivisurf->resource = NULL;
157 }
158}
159
160/* Gets called through the weston_surface destroy signal. */
161static void
162shell_handle_surface_destroy(struct wl_listener *listener, void *data)
163{
164 struct ivi_shell_surface *ivisurf =
165 container_of(listener, struct ivi_shell_surface,
166 surface_destroy_listener);
167
168 assert(ivisurf != NULL);
169
170 if (ivisurf->surface!=NULL) {
171 ivisurf->surface->configure = NULL;
172 ivisurf->surface->configure_private = NULL;
173 ivisurf->surface = NULL;
174 }
175
176 wl_list_remove(&ivisurf->surface_destroy_listener.link);
177 wl_list_remove(&ivisurf->link);
178
179 if (ivisurf->resource != NULL) {
180 wl_resource_set_user_data(ivisurf->resource, NULL);
181 ivisurf->resource = NULL;
182 }
183 free(ivisurf);
184
185}
186
187/* Gets called, when a client sends ivi_surface.destroy request. */
188static void
189surface_destroy(struct wl_client *client, struct wl_resource *resource)
190{
191 /*
192 * Fires the wl_resource destroy signal, and then calls
193 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
194 */
195 wl_resource_destroy(resource);
196}
197
198static const struct ivi_surface_interface surface_implementation = {
199 surface_destroy,
200};
201
202/**
203 * Request handler for ivi_application.surface_create.
204 *
205 * Creates an ivi_surface protocol object associated with the given wl_surface.
206 * ivi_surface protocol object is represented by struct ivi_shell_surface.
207 *
208 * \param client The client.
209 * \param resource The ivi_application protocol object.
210 * \param id_surface The IVI surface ID.
211 * \param surface_resource The wl_surface protocol object.
212 * \param id The protocol object id for the new ivi_surface protocol object.
213 *
214 * The wl_surface is given the ivi_surface role and associated with a unique
215 * IVI ID which is used to identify the surface in a controller
216 * (window manager).
217 */
218static void
219application_surface_create(struct wl_client *client,
220 struct wl_resource *resource,
221 uint32_t id_surface,
222 struct wl_resource *surface_resource,
223 uint32_t id)
224{
225 struct ivi_shell *shell = wl_resource_get_user_data(resource);
226 struct ivi_shell_surface *ivisurf;
227 struct ivi_layout_surface *layout_surface;
228 struct weston_surface *weston_surface =
229 wl_resource_get_user_data(surface_resource);
230 struct wl_resource *res;
231
232 if (weston_surface_set_role(weston_surface, "ivi_surface",
233 resource, IVI_APPLICATION_ERROR_ROLE) < 0)
234 return;
235
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900236 layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900237
238 /* check if id_ivi is already used for wl_surface*/
239 if (layout_surface == NULL){
240 wl_resource_post_error(resource,
241 IVI_APPLICATION_ERROR_IVI_ID,
242 "surface_id is already assigned "
243 "by another app");
244 return;
245 }
246
247 ivisurf = zalloc(sizeof *ivisurf);
248 if (ivisurf == NULL) {
249 wl_resource_post_no_memory(resource);
250 return;
251 }
252
253 wl_list_init(&ivisurf->link);
254 wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);
255
256 ivisurf->shell = shell;
257 ivisurf->id_surface = id_surface;
258
259 ivisurf->width = 0;
260 ivisurf->height = 0;
261 ivisurf->layout_surface = layout_surface;
262 ivisurf->configured_listener.notify = surface_configure_notify;
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900263 ivi_layout_surface_add_configured_listener(layout_surface,
264 &ivisurf->configured_listener);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900265 /*
266 * The following code relies on wl_surface destruction triggering
267 * immediateweston_surface destruction
268 */
269 ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
270 wl_signal_add(&weston_surface->destroy_signal,
271 &ivisurf->surface_destroy_listener);
272
273 ivisurf->surface = weston_surface;
274
275 weston_surface->configure = ivi_shell_surface_configure;
276 weston_surface->configure_private = ivisurf;
277
278 res = wl_resource_create(client, &ivi_surface_interface, 1, id);
279 if (res == NULL) {
280 wl_client_post_no_memory(client);
281 return;
282 }
283
284 ivisurf->resource = res;
285
286 wl_resource_set_implementation(res, &surface_implementation,
287 ivisurf, shell_destroy_shell_surface);
288}
289
290static const struct ivi_application_interface application_implementation = {
291 application_surface_create
292};
293
294/*
295 * Handle wl_registry.bind of ivi_application global singleton.
296 */
297static void
298bind_ivi_application(struct wl_client *client,
299 void *data, uint32_t version, uint32_t id)
300{
301 struct ivi_shell *shell = data;
302 struct wl_resource *resource;
303
304 resource = wl_resource_create(client, &ivi_application_interface,
305 1, id);
306
307 wl_resource_set_implementation(resource,
308 &application_implementation,
309 shell, NULL);
310}
311
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900312struct weston_view *
313get_default_view(struct weston_surface *surface)
314{
315 struct ivi_shell_surface *shsurf;
316 struct weston_view *view;
317
318 if (!surface || wl_list_empty(&surface->views))
319 return NULL;
320
321 shsurf = get_ivi_shell_surface(surface);
322 if (shsurf && shsurf->layout_surface) {
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900323 view = ivi_layout_get_weston_view(shsurf->layout_surface);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900324 if (view)
325 return view;
326 }
327
328 wl_list_for_each(view, &surface->views, surface_link) {
329 if (weston_view_is_mapped(view))
330 return view;
331 }
332
333 return container_of(surface->views.next,
334 struct weston_view, surface_link);
335}
336
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900337/*
338 * Called through the compositor's destroy signal.
339 */
340static void
341shell_destroy(struct wl_listener *listener, void *data)
342{
343 struct ivi_shell *shell =
344 container_of(listener, struct ivi_shell, destroy_listener);
345 struct ivi_shell_surface *ivisurf, *next;
346
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900347 input_panel_destroy(shell);
348
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900349 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
350 wl_list_remove(&ivisurf->link);
351 free(ivisurf);
352 }
353
354 free(shell);
355}
356
357static void
Pekka Paalanene35b2232015-02-19 17:08:44 +0200358init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell,
359 const struct ivi_shell_setting *setting)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900360{
361 shell->compositor = compositor;
362
363 wl_list_init(&shell->ivi_surface_list);
364
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900365 weston_layer_init(&shell->input_panel_layer, NULL);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200366
367 if (setting->developermode)
368 weston_install_debug_key_binding(compositor, MODIFIER_SUPER);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900369}
370
371static int
372ivi_shell_setting_create(struct ivi_shell_setting *dest,
373 struct weston_compositor *compositor)
374{
375 int result = 0;
376 struct weston_config *config = compositor->config;
377 struct weston_config_section *section;
378
379 if (NULL == dest)
380 return -1;
381
382 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
383
Ondřej Majerech4bb99292014-12-06 02:49:18 +0100384 if (weston_config_section_get_string(section, "ivi-module",
385 &dest->ivi_module, NULL) != 0) {
386 weston_log("ivi-shell: No ivi-module set in config\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900387 result = -1;
388 }
389
Pekka Paalanene35b2232015-02-19 17:08:44 +0200390 weston_config_section_get_bool(section, "developermode",
391 &dest->developermode, 0);
392
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900393 return result;
394}
395
396/*
397 * Initialization of ivi-shell.
398 */
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900399WL_EXPORT int
400module_init(struct weston_compositor *compositor,
401 int *argc, char *argv[])
402{
403 struct ivi_shell *shell;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900404 struct ivi_shell_setting setting = { };
Pekka Paalanene35b2232015-02-19 17:08:44 +0200405 int retval = -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900406
407 shell = zalloc(sizeof *shell);
408 if (shell == NULL)
409 return -1;
410
Pekka Paalanene35b2232015-02-19 17:08:44 +0200411 if (ivi_shell_setting_create(&setting, compositor) != 0)
412 return -1;
413
414 init_ivi_shell(compositor, shell, &setting);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900415
416 shell->destroy_listener.notify = shell_destroy;
417 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
418
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900419 if (input_panel_setup(shell) < 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200420 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900421
422 if (wl_global_create(compositor->wl_display,
423 &ivi_application_interface, 1,
424 shell, bind_ivi_application) == NULL)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200425 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900426
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900427 ivi_layout_init_with_compositor(compositor);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900428
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900429 /* Call module_init of ivi-modules which are defined in weston.ini */
Pekka Paalanene35b2232015-02-19 17:08:44 +0200430 if (load_controller_modules(compositor, setting.ivi_module,
431 argc, argv) < 0)
432 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900433
Pekka Paalanene35b2232015-02-19 17:08:44 +0200434 retval = 0;
435
436out_settings:
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900437 free(setting.ivi_module);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200438
439 return retval;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900440}