aboutsummaryrefslogtreecommitdiffstats
path: root/src/wth-receiver-comm.c
blob: 0527ecdb8f2ec4b7580e3f4bbaa95085fc957dfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
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
470
471
472
473
/*
 * Copyright © 2019 Advanced Driver Information Technology GmbH
 * Copyright © 2020 Collabora, Ltd.
 *
 * 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:
 *
 * 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.
 */

/*******************************************************************************
**                                                                            **
**  TARGET    : linux                                                         **
**                                                                            **
**  PROJECT   : waltham-receiver                                              **
**                                                                            **
**  PURPOSE   :  This file acts as interface to waltham IPC library           **
**                                                                            **
**                                                                            **
*******************************************************************************/
#include <sys/types.h>
#include <signal.h>

#include "wth-receiver-comm.h"
#include "wth-receiver-surface.h"
#include "wth-receiver-seat.h"
#include "wth-receiver-buffer.h"

#include <waltham-util.h>

extern uint16_t tcp_port;
extern const char *my_app_id;

void
client_post_out_of_memory(struct client *c)
{
	struct wth_display *disp;

	disp = wth_connection_get_display(c->connection);
	wth_object_post_error((struct wth_object *) disp, 1, "out of memory");
}

/*
 *  waltam ivi surface implementation
 */
static void
wthp_ivi_surface_destroy(struct wthp_ivi_surface * ivi_surface)
{
	struct ivisurface *ivisurf = wth_object_get_user_data((struct wth_object *)ivi_surface);
	struct client *client = ivisurf->appid->client;

	if (client) {
		client->pid_destroying = true;
		fprintf(stdout, "client pid_destroying to true for client %p pid %d\n",
				client, client->pid);
		if (kill(client->pid, SIGINT) < 0) {
			fprintf(stderr, "Failed to send SIGINT to child %d\n",
					client->pid);
		}
	}

	free(ivisurf);
}

static const struct wthp_ivi_surface_interface wthp_ivi_surface_implementation = {
	wthp_ivi_surface_destroy,
};


/**
 * app_id version
 */
static void
wthp_ivi_app_id_surface_create(struct wthp_ivi_app_id *ivi_application,
			       const char *app_id,
			       struct wthp_surface * wthp_surface,
			       struct wthp_ivi_surface *obj)
{
	struct surface *surface =
		wth_object_get_user_data((struct wth_object *)wthp_surface);
	struct application_id *appid =
		wth_object_get_user_data((struct wth_object *) ivi_application);
	pid_t cpid;

	struct ivisurface *ivisurf = zalloc(sizeof *ivisurf);
	if (!ivisurf) {
		return;
	}

	ivisurf->obj = obj;
	ivisurf->surf = surface;
	ivisurf->appid = appid;

	wthp_ivi_surface_set_interface(obj,
				       &wthp_ivi_surface_implementation, ivisurf);

	cpid = fork();
	if (cpid == -1) {
		fprintf(stderr, "Failed to fork()\n");
		exit(EXIT_FAILURE);
	}

	if (cpid == 0) {
		if (my_app_id)
			wth_receiver_weston_main(surface->shm_window, my_app_id, tcp_port);
		else
			wth_receiver_weston_main(surface->shm_window, app_id, tcp_port);
	} else {
		/* this is parent, in wthp_ivi_surface_destroy() we mark that the
		 * client should be waited for so wait4() will be blocked.
		 */
		appid->client->pid = cpid;
	}

}

static const struct wthp_ivi_app_id_interface wthp_ivi_app_id_implementation = {
	wthp_ivi_app_id_surface_create,
};

static void
client_bind_wthp_ivi_app_id(struct client *c, struct wthp_ivi_app_id *obj)
{
	struct application_id *app;

	app = zalloc(sizeof *app);
	if (!app) {
		client_post_out_of_memory(c);
		return;
	}

	app->obj = obj;
	app->client = c;
	wl_list_insert(&c->compositor_list, &app->link);

	wthp_ivi_app_id_set_interface(obj, &wthp_ivi_app_id_implementation, app);
}

/*
 * waltham registry implementation
 */
static void
registry_destroy(struct registry *reg)
{
	wthp_registry_free(reg->obj);
	wl_list_remove(&reg->link);
	free(reg);
}

static void
registry_handle_destroy(struct wthp_registry *registry)
{
	struct registry *reg = wth_object_get_user_data((struct wth_object *)registry);
	registry_destroy(reg);
}

static void
registry_handle_bind(struct wthp_registry *registry,
		     uint32_t name, struct wth_object *id,
		     const char *interface, uint32_t version)
{

	struct registry *reg = wth_object_get_user_data((struct wth_object *)registry);

	if (strcmp(interface, "wthp_compositor") == 0) {
		client_bind_compositor(reg->client, (struct wthp_compositor *)id);
	} else if (strcmp(interface, "wthp_blob_factory") == 0) {
		struct client *client = reg->client;
		struct seat *seat, *tmp, *get_seat;

		client_bind_blob_factory(reg->client, (struct wthp_blob_factory *)id);

		get_seat = NULL;
		wl_list_for_each_safe(seat, tmp, &client->seat_list, link) {
			get_seat = seat;
		}

		if (get_seat)
			seat_send_updated_caps(get_seat);
	} else if (strcmp(interface, "wthp_ivi_app_id") == 0) {
		client_bind_wthp_ivi_app_id(reg->client, (struct wthp_ivi_app_id *) id);
	} else if (strcmp(interface, "wthp_seat") == 0) {
		client_bind_seat(reg->client, (struct wthp_seat *)id);
	} else {
		wth_object_post_error((struct wth_object *)registry, 0,
				"%s: unknown name %u", __func__, name);
		wth_object_delete(id);
	}
}

static const struct wthp_registry_interface registry_implementation = {
	registry_handle_destroy,
	registry_handle_bind
};

/*
 * waltham display implementation
 */

static void
display_handle_client_version(struct wth_display *wth_display,
		uint32_t client_version)
{
	wth_object_post_error((struct wth_object *)wth_display, 0,
			"unimplemented: %s", __func__);
}

static void
display_handle_sync(struct wth_display * wth_display, struct wthp_callback * callback)
{
	wthp_callback_send_done(callback, 0);
	wthp_callback_free(callback);
}

static void
display_handle_get_registry(struct wth_display *wth_display,
			    struct wthp_registry *registry)
{
	struct client *c = wth_object_get_user_data((struct wth_object *)wth_display);
	struct registry *reg;

	reg = zalloc(sizeof *reg);
	if (!reg) {
		client_post_out_of_memory(c);
		return;
	}

	reg->obj = registry;
	reg->client = c;
	wl_list_insert(&c->registry_list, &reg->link);
	wthp_registry_set_interface(registry,
			&registry_implementation, reg);

	wthp_registry_send_global(registry, 1, "wthp_compositor", 4);
	wthp_registry_send_global(registry, 1, "wthp_ivi_app_id", 1);
	wthp_registry_send_global(registry, 1, "wthp_seat", 4);
	wthp_registry_send_global(registry, 1, "wthp_blob_factory", 4);

}

const struct wth_display_interface display_implementation = {
	display_handle_client_version,
	display_handle_sync,
	display_handle_get_registry
};

/*
 * utility functions
 */
static int
watch_ctl(struct watch *w, int op, uint32_t events)
{
	struct epoll_event ee;

	ee.events = events;
	ee.data.ptr = w;
	return epoll_ctl(w->receiver->epoll_fd, op, w->fd, &ee);
}

/**
* client_destroy
*
* Destroy client connection
*
* @param names        struct client *c
* @param value        client data
* @return             none
*/
void
client_destroy(struct client *c)
{
	struct region *region;
	struct compositor *comp;
	struct registry *reg;
	struct surface *surface;

	/* clean up remaining client resources in case the client
	 * did not.
	 */
	wl_list_last_until_empty(region, &c->region_list, link)
		region_destroy(region);

	wl_list_last_until_empty(comp, &c->compositor_list, link)
		compositor_destroy(comp);

	wl_list_last_until_empty(reg, &c->registry_list, link)
		registry_destroy(reg);

	wl_list_last_until_empty(surface, &c->surface_list, link)
		surface_destroy(surface);

	wl_list_remove(&c->link);
	watch_ctl(&c->conn_watch, EPOLL_CTL_DEL, 0);
	wth_connection_destroy(c->connection);
	free(c);
}

/*
 * functions to handle waltham client connections
 */
static void
connection_handle_data(struct watch *w, uint32_t events)
{

	struct client *c = container_of(w, struct client, conn_watch);
	int ret;

	if (events & EPOLLERR) {
		wth_error("Client %p errored out.\n", c);
		client_destroy(c);
		return;
	}

	if (events & EPOLLHUP) {
		wth_error("Client %p hung up.\n", c);
		client_destroy(c);
		return;
	}

	if (events & EPOLLOUT) {
		ret = wth_connection_flush(c->connection);
		if (ret == 0) {
			watch_ctl(&c->conn_watch, EPOLL_CTL_MOD, EPOLLIN);
		} else if (ret < 0 && errno != EAGAIN) {
			wth_error("Client %p flush error.\n", c);
			client_destroy(c);
			return;
		}
	}

	if (events & EPOLLIN) {
		ret = wth_connection_read(c->connection);
		if (ret < 0) {
			wth_error("Client %p read error.\n", c);
			client_destroy(c);
			return;
		}

		ret = wth_connection_dispatch(c->connection);
		if (ret < 0 && errno != EPROTO) {
			wth_error("Client %p dispatch error.\n", c);
			client_destroy(c);
			return;
		}
	}
}

/**
 * client_create
 *
 * Create new client connection
 *
 * @param srv                   receiver structure
 * @param wth_connection        Waltham connection handle
 * @return                      Pointer to client structure
 */
static struct client *
client_create(struct receiver *srv, struct wth_connection *conn)
{

	struct client *c;
	struct wth_display *disp;

	c = zalloc(sizeof *c);
	if (!c)
		return NULL;

	c->receiver = srv;
	c->connection = conn;

	c->conn_watch.receiver = srv;
	c->conn_watch.fd = wth_connection_get_fd(conn);
	c->conn_watch.cb = connection_handle_data;
	if (watch_ctl(&c->conn_watch, EPOLL_CTL_ADD, EPOLLIN) < 0) {
		free(c);
		return NULL;
	}


	wl_list_insert(&srv->client_list, &c->link);

	wl_list_init(&c->registry_list);
	wl_list_init(&c->compositor_list);
	wl_list_init(&c->seat_list);
	wl_list_init(&c->pointer_list);
	wl_list_init(&c->touch_list);
	wl_list_init(&c->region_list);
	wl_list_init(&c->surface_list);
	wl_list_init(&c->buffer_list);

	disp = wth_connection_get_display(c->connection);
	wth_display_set_interface(disp, &display_implementation, c);

	fprintf(stdout, "Client %p created\n", c);
	return c;
}


/**
* receiver_flush_clients
*
* write all the pending requests from the clients to socket
*
* @param names        struct receiver *srv
* @param value        socket connection info and client data
* @return             none
*/
void
receiver_flush_clients(struct receiver *srv)
{
	struct client *c, *tmp;
	int ret;

	wl_list_for_each_safe(c, tmp, &srv->client_list, link) {
		/* Flush out buffered requests. If the Waltham socket is
		 * full, poll it for writable too.
		 */
		ret = wth_connection_flush(c->connection);
		if (ret < 0 && errno == EAGAIN) {
			watch_ctl(&c->conn_watch, EPOLL_CTL_MOD, EPOLLIN | EPOLLOUT);
		} else if (ret < 0) {
			perror("Connection flush failed");
			client_destroy(c);
			return;
		}
	}
}

/**
* receiver_accept_client
*
* Accepts new waltham client connection and instantiates client structure
*
* @param names        struct receiver *srv
* @param value        socket connection info and client data
* @return             none
*/
void
receiver_accept_client(struct receiver *srv)
{
	struct client *client;
	struct wth_connection *conn;
	struct sockaddr_in addr;
	socklen_t len;

	len = sizeof(addr);
	conn = wth_accept(srv->listen_fd, (struct sockaddr *)&addr, &len);
	if (!conn) {
		wth_error("Failed to accept a connection.\n");
		return;
	}
	client = client_create(srv, conn);
	if (!client) {
		wth_error("Failed client_create().\n");
		return;
	}
}