ivi-shell.c 13.6 KB
Newer Older
1
2
3
/*
 * Copyright (C) 2013 DENSO CORPORATION
 *
4
5
6
7
8
9
10
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
11
 *
12
13
14
15
16
17
18
19
20
21
22
23
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 */

/*
 * ivi-shell supports a type of shell for In-Vehicle Infotainment system.
 * In-Vehicle Infotainment system traditionally manages surfaces with global
 * identification. A protocol, ivi_application, supports such a feature
 * by implementing a request, ivi_application::surface_creation defined in
 * ivi_application.xml.
 *
 *  The ivi-shell explicitly loads a module to add business logic like how to
 *  layout surfaces by using internal ivi-layout APIs.
 */
#include "config.h"

#include <string.h>
#include <dlfcn.h>
#include <limits.h>
#include <assert.h>
Pekka Paalanen's avatar
Pekka Paalanen committed
42
#include <linux/input.h>
43
44
45

#include "ivi-shell.h"
#include "ivi-application-server-protocol.h"
46
#include "ivi-layout-export.h"
47
#include "ivi-layout-private.h"
48
#include "shared/helpers.h"
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

/* Representation of ivi_surface protocol object. */
struct ivi_shell_surface
{
	struct wl_resource* resource;
	struct ivi_shell *shell;
	struct ivi_layout_surface *layout_surface;

	struct weston_surface *surface;
	struct wl_listener surface_destroy_listener;

	uint32_t id_surface;

	int32_t width;
	int32_t height;

	struct wl_list link;

	struct wl_listener configured_listener;
};

struct ivi_shell_setting
{
	char *ivi_module;
73
	int developermode;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
};

/*
 * Implementation of ivi_surface
 */

static void
surface_configure_notify(struct wl_listener *listener, void *data)
{
	struct ivi_layout_surface *layout_surf =
		(struct ivi_layout_surface *)data;

	struct ivi_shell_surface *shell_surf =
		container_of(listener,
			     struct ivi_shell_surface,
			     configured_listener);

	int32_t dest_width = 0;
	int32_t dest_height = 0;
93
94
95

	ivi_layout_surface_get_dimension(layout_surf,
					 &dest_width, &dest_height);
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

	if (shell_surf->resource)
		ivi_surface_send_configure(shell_surf->resource,
					   dest_width, dest_height);
}

static void
ivi_shell_surface_configure(struct weston_surface *, int32_t, int32_t);

static struct ivi_shell_surface *
get_ivi_shell_surface(struct weston_surface *surface)
{
	if (surface->configure == ivi_shell_surface_configure)
		return surface->configure_private;

	return NULL;
}

static void
ivi_shell_surface_configure(struct weston_surface *surface,
			    int32_t sx, int32_t sy)
{
	struct ivi_shell_surface *ivisurf = get_ivi_shell_surface(surface);

	if (surface->width == 0 || surface->height == 0 || ivisurf == NULL)
		return;

	if (ivisurf->width != surface->width ||
	    ivisurf->height != surface->height) {
		ivisurf->width  = surface->width;
		ivisurf->height = surface->height;

128
129
		ivi_layout_surface_configure(ivisurf->layout_surface,
					     surface->width, surface->height);
130
131
132
	}
}

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
static void
layout_surface_cleanup(struct ivi_shell_surface *ivisurf)
{
	assert(ivisurf->layout_surface != NULL);

	ivi_layout_surface_destroy(ivisurf->layout_surface);
	ivisurf->layout_surface = NULL;

	ivisurf->surface->configure = NULL;
	ivisurf->surface->configure_private = NULL;
	ivisurf->surface = NULL;

	// destroy weston_surface destroy signal.
	wl_list_remove(&ivisurf->surface_destroy_listener.link);
}

149
150
151
152
153
154
155
156
157
/*
 * The ivi_surface wl_resource destructor.
 *
 * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
 */
static void
shell_destroy_shell_surface(struct wl_resource *resource)
{
	struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
158
159
160
161
162
163
164
165
166
167
168
169

	if (ivisurf == NULL)
		return;

	assert(ivisurf->resource == resource);

	if (ivisurf->layout_surface != NULL)
		layout_surface_cleanup(ivisurf);

	wl_list_remove(&ivisurf->link);

	free(ivisurf);
170
171
172
173
174
175
176
177
178
179
180
181
}

/* Gets called through the weston_surface destroy signal. */
static void
shell_handle_surface_destroy(struct wl_listener *listener, void *data)
{
	struct ivi_shell_surface *ivisurf =
			container_of(listener, struct ivi_shell_surface,
				     surface_destroy_listener);

	assert(ivisurf != NULL);

182
183
	if (ivisurf->layout_surface != NULL)
		layout_surface_cleanup(ivisurf);
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
}

/* Gets called, when a client sends ivi_surface.destroy request. */
static void
surface_destroy(struct wl_client *client, struct wl_resource *resource)
{
	/*
	 * Fires the wl_resource destroy signal, and then calls
	 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
	 */
	wl_resource_destroy(resource);
}

static const struct ivi_surface_interface surface_implementation = {
	surface_destroy,
};

/**
 * Request handler for ivi_application.surface_create.
 *
 * Creates an ivi_surface protocol object associated with the given wl_surface.
 * ivi_surface protocol object is represented by struct ivi_shell_surface.
 *
 * \param client The client.
 * \param resource The ivi_application protocol object.
 * \param id_surface The IVI surface ID.
 * \param surface_resource The wl_surface protocol object.
 * \param id The protocol object id for the new ivi_surface protocol object.
 *
 * The wl_surface is given the ivi_surface role and associated with a unique
 * IVI ID which is used to identify the surface in a controller
 * (window manager).
 */
static void
application_surface_create(struct wl_client *client,
			   struct wl_resource *resource,
			   uint32_t id_surface,
			   struct wl_resource *surface_resource,
			   uint32_t id)
{
	struct ivi_shell *shell = wl_resource_get_user_data(resource);
	struct ivi_shell_surface *ivisurf;
	struct ivi_layout_surface *layout_surface;
	struct weston_surface *weston_surface =
		wl_resource_get_user_data(surface_resource);
	struct wl_resource *res;

	if (weston_surface_set_role(weston_surface, "ivi_surface",
				    resource, IVI_APPLICATION_ERROR_ROLE) < 0)
		return;

235
	layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
236
237

	/* check if id_ivi is already used for wl_surface*/
Dawid Gajownik's avatar
Dawid Gajownik committed
238
	if (layout_surface == NULL) {
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
		wl_resource_post_error(resource,
				       IVI_APPLICATION_ERROR_IVI_ID,
				       "surface_id is already assigned "
				       "by another app");
		return;
	}

	ivisurf = zalloc(sizeof *ivisurf);
	if (ivisurf == NULL) {
		wl_resource_post_no_memory(resource);
		return;
	}

	wl_list_init(&ivisurf->link);
	wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);

	ivisurf->shell = shell;
	ivisurf->id_surface = id_surface;

	ivisurf->width = 0;
	ivisurf->height = 0;
	ivisurf->layout_surface = layout_surface;
	ivisurf->configured_listener.notify = surface_configure_notify;
262
263
	ivi_layout_surface_add_configured_listener(layout_surface,
				     &ivisurf->configured_listener);
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
	/*
	 * The following code relies on wl_surface destruction triggering
	 * immediateweston_surface destruction
	 */
	ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
	wl_signal_add(&weston_surface->destroy_signal,
		      &ivisurf->surface_destroy_listener);

	ivisurf->surface = weston_surface;

	weston_surface->configure = ivi_shell_surface_configure;
	weston_surface->configure_private = ivisurf;

	res = wl_resource_create(client, &ivi_surface_interface, 1, id);
	if (res == NULL) {
		wl_client_post_no_memory(client);
		return;
	}

	ivisurf->resource = res;

	wl_resource_set_implementation(res, &surface_implementation,
				       ivisurf, shell_destroy_shell_surface);
}

static const struct ivi_application_interface application_implementation = {
	application_surface_create
};

/*
 * Handle wl_registry.bind of ivi_application global singleton.
 */
static void
bind_ivi_application(struct wl_client *client,
		     void *data, uint32_t version, uint32_t id)
{
	struct ivi_shell *shell = data;
	struct wl_resource *resource;

	resource = wl_resource_create(client, &ivi_application_interface,
				      1, id);

	wl_resource_set_implementation(resource,
				       &application_implementation,
				       shell, NULL);
}

311
312
313
314
315
316
317
318
319
320
321
struct weston_view *
get_default_view(struct weston_surface *surface)
{
	struct ivi_shell_surface *shsurf;
	struct weston_view *view;

	if (!surface || wl_list_empty(&surface->views))
		return NULL;

	shsurf = get_ivi_shell_surface(surface);
	if (shsurf && shsurf->layout_surface) {
322
		view = ivi_layout_get_weston_view(shsurf->layout_surface);
323
324
325
326
327
328
329
330
331
332
333
334
335
		if (view)
			return view;
	}

	wl_list_for_each(view, &surface->views, surface_link) {
		if (weston_view_is_mapped(view))
			return view;
	}

	return container_of(surface->views.next,
			    struct weston_view, surface_link);
}

336
337
338
339
340
341
342
343
344
345
/*
 * Called through the compositor's destroy signal.
 */
static void
shell_destroy(struct wl_listener *listener, void *data)
{
	struct ivi_shell *shell =
		container_of(listener, struct ivi_shell, destroy_listener);
	struct ivi_shell_surface *ivisurf, *next;

346
	text_backend_destroy(shell->text_backend);
347
348
	input_panel_destroy(shell);

349
350
351
352
353
354
355
356
	wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
		wl_list_remove(&ivisurf->link);
		free(ivisurf);
	}

	free(shell);
}

Pekka Paalanen's avatar
Pekka Paalanen committed
357
static void
358
359
terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
		  uint32_t key, void *data)
Pekka Paalanen's avatar
Pekka Paalanen committed
360
361
362
363
364
365
{
	struct weston_compositor *compositor = data;

	wl_display_terminate(compositor->wl_display);
}

366
static void
367
368
init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell,
	       const struct ivi_shell_setting *setting)
369
370
371
372
373
{
	shell->compositor = compositor;

	wl_list_init(&shell->ivi_surface_list);

374
	weston_layer_init(&shell->input_panel_layer, NULL);
375

Pekka Paalanen's avatar
Pekka Paalanen committed
376
	if (setting->developermode) {
377
		weston_install_debug_key_binding(compositor, MODIFIER_SUPER);
Pekka Paalanen's avatar
Pekka Paalanen committed
378
379
380
381
382
383

		weston_compositor_add_key_binding(compositor, KEY_BACKSPACE,
						  MODIFIER_CTRL | MODIFIER_ALT,
						  terminate_binding,
						  compositor);
	}
384
385
386
387
}

static int
ivi_shell_setting_create(struct ivi_shell_setting *dest,
388
389
			 struct weston_compositor *compositor,
			 int *argc, char *argv[])
390
391
392
393
394
{
	int result = 0;
	struct weston_config *config = compositor->config;
	struct weston_config_section *section;

395
396
397
398
399
400
	const struct weston_option ivi_shell_options[] = {
		{ WESTON_OPTION_STRING, "ivi-module", 0, &dest->ivi_module },
	};

	parse_options(ivi_shell_options, ARRAY_LENGTH(ivi_shell_options),
		      argc, argv);
401
402
403

	section = weston_config_get_section(config, "ivi-shell", NULL, NULL);

404
405
406
407
	if (!dest->ivi_module &&
	    weston_config_section_get_string(section, "ivi-module",
					     &dest->ivi_module, NULL) < 0) {
		weston_log("Error: ivi-shell: No ivi-module set\n");
408
409
410
		result = -1;
	}

411
412
413
	weston_config_section_get_bool(section, "developermode",
				       &dest->developermode, 0);

414
415
416
	return result;
}

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
static void
activate_binding(struct weston_seat *seat,
		 struct weston_view *focus_view)
{
	struct weston_surface *focus = focus_view->surface;
	struct weston_surface *main_surface =
		weston_surface_get_main_surface(focus);

	if (get_ivi_shell_surface(main_surface) == NULL)
		return;

	weston_surface_activate(focus, seat);
}

static void
click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
			  uint32_t button, void *data)
{
	if (pointer->grab != &pointer->default_grab)
		return;
	if (pointer->focus == NULL)
		return;

	activate_binding(pointer->seat, pointer->focus);
}

static void
touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
			  void *data)
{
	if (touch->grab != &touch->default_grab)
		return;
	if (touch->focus == NULL)
		return;

	activate_binding(touch->seat, touch->focus);
}

static void
shell_add_bindings(struct weston_compositor *compositor,
		   struct ivi_shell *shell)
{
	weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
					     click_to_activate_binding,
					     shell);
	weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
					     click_to_activate_binding,
					     shell);
	weston_compositor_add_touch_binding(compositor, 0,
					    touch_to_activate_binding,
					    shell);
}

470
471
472
473
474
475
476
477
478
/*
 * Initialization of ivi-shell.
 */
WL_EXPORT int
module_init(struct weston_compositor *compositor,
	    int *argc, char *argv[])
{
	struct ivi_shell *shell;
	struct ivi_shell_setting setting = { };
479
	int retval = -1;
480
481
482
483
484

	shell = zalloc(sizeof *shell);
	if (shell == NULL)
		return -1;

485
	if (ivi_shell_setting_create(&setting, compositor, argc, argv) != 0)
486
487
488
		return -1;

	init_ivi_shell(compositor, shell, &setting);
489
490
491
492

	shell->destroy_listener.notify = shell_destroy;
	wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);

493
	if (input_panel_setup(shell) < 0)
494
		goto out_settings;
495

496
497
	shell->text_backend = text_backend_init(compositor);
	if (!shell->text_backend)
498
499
		goto out_settings;

500
501
502
	if (wl_global_create(compositor->wl_display,
			     &ivi_application_interface, 1,
			     shell, bind_ivi_application) == NULL)
503
		goto out_settings;
504

505
	ivi_layout_init_with_compositor(compositor);
506
	shell_add_bindings(compositor, shell);
507
508

	/* Call module_init of ivi-modules which are defined in weston.ini */
509
510
511
512
513
	if (load_controller_modules(compositor, setting.ivi_module,
				    argc, argv) < 0)
		goto out_settings;

	retval = 0;
514

515
out_settings:
516
	free(setting.ivi_module);
517
518

	return retval;
519
}