virtual: add protocol to share wl_buffer(tbm_surface) with tdm_client
[platform/core/uifw/libtdm.git] / src / tdm_server.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <boram1288.park@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #define WL_HIDE_DEPRECATED
41
42 #include <tdm-server-protocol.h>
43
44 #include "tdm_private.h"
45
46 /* CAUTION:
47  * - tdm server doesn't care about thread things.
48  * - DO NOT use the TDM internal functions here.
49  *     However, the internal function which does lock/unlock the mutex of
50  *     private_display in itself can be called.
51  * - DO NOT use the tdm_private_display structure here.
52  * - The callback function things can be called in main thread.
53  */
54
55 struct _tdm_private_server {
56         tdm_private_loop *private_loop;
57         struct list_head output_list;
58         struct list_head voutput_list;
59         struct list_head wait_list;
60 };
61
62 typedef struct _tdm_server_output_info {
63         struct list_head link;
64         tdm_private_server *private_server;
65         struct wl_resource *resource;
66         tdm_output *output;
67         struct list_head vblank_list;
68         unsigned int watch_output_changes;
69 } tdm_server_output_info;
70
71 typedef struct _tdm_server_voutput_buffer {
72         struct list_head link;
73         struct wl_resource *wl_buffer;
74         tbm_surface_h surface;
75 } tdm_server_voutput_buffer;
76
77 typedef struct _tdm_server_voutput_info {
78         struct list_head link;
79         tdm_private_server *private_server;
80         struct wl_resource *resource;
81         tdm_output *output;
82         struct list_head output_list;
83
84         tdm_output_conn_status status;
85         struct
86         {
87                 int count;
88                 tdm_output_mode *modes;
89         } available_modes;
90
91         unsigned int mmwidth;
92         unsigned int mmheight;
93
94         struct list_head buffer_list;
95         tdm_server_voutput_buffer *attach_buffer;
96 } tdm_server_voutput_info;
97
98 typedef struct _tdm_server_vblank_info {
99         struct list_head link;
100         tdm_server_output_info *output_info;
101         struct wl_resource *resource;
102
103         tdm_vblank *vblank;
104         unsigned int stamp;
105 } tdm_server_vblank_info;
106
107 typedef struct _tdm_server_wait_info {
108         struct list_head link;
109         tdm_server_vblank_info *vblank_info;
110
111         unsigned int req_id;
112         double req_time;
113 } tdm_server_wait_info;
114
115 typedef struct _tdm_server_client_info {
116         struct list_head link;
117         pid_t pid;
118         char name[TDM_NAME_LEN];
119         struct wl_resource *resource;
120 } tdm_server_client_info;
121
122 static tdm_private_server *keep_private_server;
123 static struct list_head client_list;
124
125 static void destroy_wait(tdm_server_wait_info *wait_info);
126
127 static void
128 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
129 {
130         char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
131         FILE *h;
132         size_t len;
133
134         snprintf(name, size, "Unknown");
135
136         snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
137         h = fopen(proc, "r");
138         if (!h)
139                 return;
140
141         len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
142         if (len == 0) {
143                 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
144                 len = p - pname;
145         }
146         pname[len - 1] = '\0';
147
148         strncpy(name, pname, size - 1);
149         name[size - 1] = '\0';
150
151         fclose(h);
152 }
153
154 /* LCOV_EXCL_START */
155 static void
156 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
157                                           unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
158 {
159         tdm_server_wait_info *found;
160         tdm_server_vblank_info *vblank_info;
161
162         TDM_RETURN_IF_FAIL(keep_private_server != NULL);
163
164         LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
165                                    tdm_server_wait_info, link, found);
166         if (!found) {
167                 TDM_ERR("wait_info(%p) is destroyed", wait_info);
168                 return;
169         }
170
171         if (tdm_debug_module & TDM_DEBUG_VBLANK)
172                 TDM_DBG("req_id(%d) done", wait_info->req_id);
173
174         vblank_info = wait_info->vblank_info;
175
176         if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
177                 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
178
179         wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
180                                                         sequence, tv_sec, tv_usec, error);
181
182         destroy_wait(wait_info);
183 }
184 /* LCOV_EXCL_STOP */
185
186 /* LCOV_EXCL_START */
187 static void
188 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
189                                           unsigned int tv_sec, unsigned int tv_usec, void *user_data)
190 {
191         _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
192 }
193 /* LCOV_EXCL_STOP */
194
195 /* LCOV_EXCL_START */
196 static void
197 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
198                                                          tdm_value value, void *user_data)
199 {
200         tdm_server_output_info *output_info = user_data;
201         struct wl_client *client;
202         pid_t pid = 0;
203
204         TDM_RETURN_IF_FAIL(output_info != NULL);
205
206         client = wl_resource_get_client(output_info->resource);
207         TDM_RETURN_IF_FAIL(client != NULL);
208
209         wl_client_get_credentials(client, &pid, NULL, NULL);
210
211         if (!output_info->watch_output_changes) {
212                 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
213                 return;
214         }
215
216         TDM_DBG("send the output changes: %d", (unsigned int)pid);
217
218         switch (type) {
219         case TDM_OUTPUT_CHANGE_DPMS:
220                 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
221                 break;
222         case TDM_OUTPUT_CHANGE_CONNECTION:
223                 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
224                 break;
225         default:
226                 break;
227         }
228 }
229 /* LCOV_EXCL_STOP */
230
231 static void
232 destroy_wait(tdm_server_wait_info *wait_info)
233 {
234         LIST_DEL(&wait_info->link);
235         free(wait_info);
236 }
237
238 static void
239 destroy_vblank_callback(struct wl_resource *resource)
240 {
241         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
242         tdm_server_wait_info *w = NULL, *ww = NULL;
243
244         TDM_RETURN_IF_FAIL(vblank_info != NULL);
245
246         LIST_DEL(&vblank_info->link);
247
248         if (vblank_info->vblank)
249                 tdm_vblank_destroy(vblank_info->vblank);
250
251         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
252                 if (w->vblank_info == vblank_info)
253                         destroy_wait(w);
254         }
255
256         free(vblank_info);
257 }
258
259 /* LCOV_EXCL_START */
260 static void
261 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
262 {
263         wl_resource_destroy(resource);
264 }
265 /* LCOV_EXCL_STOP */
266
267 /* LCOV_EXCL_START */
268 static void
269 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
270 {
271         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
272         tdm_error ret;
273
274         ret = tdm_vblank_set_name(vblank_info->vblank, name);
275         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
276 }
277 /* LCOV_EXCL_STOP */
278
279 /* LCOV_EXCL_START */
280 static void
281 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
282 {
283         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
284         tdm_error ret;
285
286         ret = tdm_vblank_set_fps(vblank_info->vblank, fps);
287         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
288 }
289 /* LCOV_EXCL_STOP */
290
291 /* LCOV_EXCL_START */
292 static void
293 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
294 {
295         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
296         tdm_error ret;
297
298         ret = tdm_vblank_set_offset(vblank_info->vblank, offset);
299         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
300 }
301 /* LCOV_EXCL_STOP */
302
303 static void
304 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
305 {
306         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
307         tdm_error ret;
308
309         ret = tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
310         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
311 }
312
313 static void
314 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
315                                                                   uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
316 {
317         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
318         tdm_server_output_info *output_info = vblank_info->output_info;
319         tdm_private_server *private_server = output_info->private_server;
320         tdm_server_wait_info *wait_info;
321         unsigned int enable_fake = 0;
322         tdm_error ret;
323
324         wait_info = calloc(1, sizeof * wait_info);
325         if (!wait_info) {
326                 /* LCOV_EXCL_START */
327
328                 TDM_ERR("alloc failed");
329                 ret = TDM_ERROR_OUT_OF_MEMORY;
330                 goto wait_failed;
331
332                 /* LCOV_EXCL_STOP */
333         }
334
335         LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
336         wait_info->vblank_info = vblank_info;
337         wait_info->req_id = req_id;
338         wait_info->req_time = TDM_TIME(req_sec, req_usec);
339
340         if (tdm_debug_module & TDM_DEBUG_VBLANK)
341                 TDM_DBG("req_id(%d) wait", req_id);
342
343         if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
344                 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
345
346         ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
347
348         tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
349         if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
350                 goto wait_failed;
351
352         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
353
354         return;
355 wait_failed:
356         /* LCOV_EXCL_START */
357
358         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
359         if (wait_info)
360                 destroy_wait(wait_info);
361
362         /* LCOV_EXCL_STOP */
363 }
364
365 static void
366 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
367                                                                           uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
368 {
369         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
370         tdm_server_output_info *output_info = vblank_info->output_info;
371         tdm_private_server *private_server = output_info->private_server;
372         tdm_server_wait_info *wait_info;
373         unsigned int enable_fake = 0;
374         tdm_error ret;
375
376         wait_info = calloc(1, sizeof * wait_info);
377         if (!wait_info) {
378                 /* LCOV_EXCL_START */
379
380                 TDM_ERR("alloc failed");
381                 ret = TDM_ERROR_OUT_OF_MEMORY;
382                 goto wait_failed;
383
384                 /* LCOV_EXCL_STOP */
385         }
386
387         LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
388         wait_info->vblank_info = vblank_info;
389         wait_info->req_id = req_id;
390         wait_info->req_time = TDM_TIME(req_sec, req_usec);
391
392         if (tdm_debug_module & TDM_DEBUG_VBLANK)
393                 TDM_DBG("req_id(%d) wait", req_id);
394
395         if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
396                 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
397
398         ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
399
400         tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
401         if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
402                 goto wait_failed;
403
404         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
405
406         return;
407 wait_failed:
408         /* LCOV_EXCL_START */
409
410         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
411         if (wait_info)
412                 destroy_wait(wait_info);
413
414         /* LCOV_EXCL_STOP */
415 }
416
417 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
418         _tdm_server_vblank_cb_destroy,
419         _tdm_server_vblank_cb_set_name,
420         _tdm_server_vblank_cb_set_fps,
421         _tdm_server_vblank_cb_set_offset,
422         _tdm_server_vblank_cb_set_enable_fake,
423         _tdm_server_vblank_cb_wait_vblank,
424         _tdm_server_vblank_cb_wait_vblank_seq,
425 };
426
427 /* LCOV_EXCL_START */
428 static void
429 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
430 {
431         wl_resource_destroy(resource);
432 }
433 /* LCOV_EXCL_STOP */
434
435 static void
436 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
437 {
438         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
439         tdm_private_server *private_server = output_info->private_server;
440         tdm_private_loop *private_loop = private_server->private_loop;
441         struct wl_resource *vblank_resource;
442         tdm_vblank *vblank;
443         tdm_server_vblank_info *vblank_info;
444
445         vblank_resource =
446                 wl_resource_create(client, &wl_tdm_vblank_interface,
447                                                    wl_resource_get_version(resource), id);
448         if (!vblank_resource) {
449                 /* LCOV_EXCL_START */
450
451                 wl_resource_post_no_memory(resource);
452                 TDM_ERR("wl_resource_create failed");
453                 return;
454
455                 /* LCOV_EXCL_STOP */
456         }
457
458         vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
459         if (!vblank) {
460                 /* LCOV_EXCL_START */
461
462                 wl_resource_post_no_memory(resource);
463                 wl_resource_destroy(vblank_resource);
464                 TDM_ERR("tdm_vblank_create failed");
465                 return;
466
467                 /* LCOV_EXCL_STOP */
468         }
469
470         vblank_info = calloc(1, sizeof * vblank_info);
471         if (!vblank_info) {
472                 /* LCOV_EXCL_START */
473
474                 wl_resource_post_no_memory(resource);
475                 wl_resource_destroy(vblank_resource);
476                 tdm_vblank_destroy(vblank);
477                 TDM_ERR("alloc failed");
478                 return;
479
480                 /* LCOV_EXCL_STOP */
481         }
482
483         LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
484         vblank_info->output_info = output_info;
485         vblank_info->resource = vblank_resource;
486         vblank_info->vblank = vblank;
487         vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
488
489         tdm_vblank_set_resource(vblank, vblank_resource);
490
491         wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
492                                                                    vblank_info, destroy_vblank_callback);
493
494         wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
495
496         if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) {
497                 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
498                 if (output == output_info->output)
499                         wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
500         }
501
502         return;
503 }
504
505 /* LCOV_EXCL_START */
506 static void
507 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
508 {
509         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
510
511         TDM_RETURN_IF_FAIL(output_info != NULL);
512
513         output_info->watch_output_changes = enable;
514 }
515 /* LCOV_EXCL_STOP */
516
517 static void
518 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
519 {
520         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
521         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
522         tdm_error ret;
523
524         TDM_RETURN_IF_FAIL(output_info != NULL);
525
526         ret = tdm_output_get_conn_status(output_info->output, &status);
527         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
528
529         wl_tdm_output_send_connection(output_info->resource, status, ret);
530
531         return;
532
533 failed:
534         wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
535 }
536
537 static void
538 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
539 {
540         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
541         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
542         tdm_error ret;
543
544         TDM_RETURN_IF_FAIL(output_info != NULL);
545
546         ret = tdm_output_get_conn_status(output_info->output, &status);
547         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
548
549         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
550                 const tdm_output_mode *mode = NULL;
551                 unsigned int hdisplay, vdisplay, vrefresh;
552
553                 ret = tdm_output_get_mode(output_info->output, &mode);
554                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
555
556                 hdisplay = (mode) ? mode->hdisplay : 0;
557                 vdisplay = (mode) ? mode->vdisplay : 0;
558                 vrefresh = (mode) ? mode->vrefresh : 0;
559
560                 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
561         } else {
562                 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
563         }
564
565         return;
566 failed:
567         wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
568 }
569
570 static void
571 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
572 {
573         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
574         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
575         tdm_error ret;
576
577         TDM_RETURN_IF_FAIL(output_info != NULL);
578
579         ret = tdm_output_get_conn_status(output_info->output, &status);
580         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
581
582         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
583                 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
584
585                 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
586                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
587
588                 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
589         } else {
590                 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
591         }
592
593         return;
594 failed:
595         wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
596 }
597
598 static const struct wl_tdm_output_interface tdm_output_implementation = {
599         _tdm_server_output_cb_destroy,
600         _tdm_server_output_cb_create_vblank,
601         _tdm_server_output_cb_watch_output_changes,
602         _tdm_server_output_cb_get_connection,
603         _tdm_server_output_cb_get_mode,
604         _tdm_server_output_cb_get_dpms,
605 };
606
607 static void
608 destroy_output_callback(struct wl_resource *resource)
609 {
610         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
611         tdm_server_vblank_info *v = NULL, *vv = NULL;
612
613         TDM_RETURN_IF_FAIL(output_info != NULL);
614
615         LIST_DEL(&output_info->link);
616
617         tdm_output_remove_change_handler(output_info->output,
618                                                                          _tdm_server_cb_output_change, output_info);
619
620         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
621                 wl_resource_destroy(v->resource);
622         }
623
624         free(output_info);
625 }
626
627 static void
628 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
629                                                          const char *name, uint32_t id)
630 {
631         tdm_private_server *private_server = wl_resource_get_user_data(resource);
632         tdm_server_output_info *output_info;
633         struct wl_resource *output_resource = NULL;
634         tdm_output *output;
635         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
636         tdm_error ret;
637
638         output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
639         if (!output) {
640                 /* LCOV_EXCL_START */
641
642                 TDM_ERR("There is no '%s' output", name);
643                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
644                                                            "There is no '%s' output", name);
645                 return;
646
647                 /* LCOV_EXCL_STOP */
648         }
649
650         output_resource =
651                 wl_resource_create(client, &wl_tdm_output_interface,
652                                                    wl_resource_get_version(resource), id);
653         if (!output_resource) {
654                 /* LCOV_EXCL_START */
655
656                 wl_resource_post_no_memory(resource);
657                 TDM_ERR("wl_resource_create failed");
658                 return;
659
660                 /* LCOV_EXCL_STOP */
661         }
662
663         output_info = calloc(1, sizeof * output_info);
664         if (!output_info) {
665                 /* LCOV_EXCL_START */
666
667                 wl_resource_post_no_memory(resource);
668                 wl_resource_destroy(output_resource);
669                 TDM_ERR("alloc failed");
670                 return;
671
672                 /* LCOV_EXCL_STOP */
673         }
674
675         ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
676         if (ret != TDM_ERROR_NONE) {
677                 wl_resource_post_no_memory(resource);
678                 wl_resource_destroy(output_resource);
679                 free(output_info);
680                 TDM_ERR("tdm_output_add_change_handler failed");
681                 return;
682         }
683
684         LIST_ADDTAIL(&output_info->link, &private_server->output_list);
685         output_info->private_server = private_server;
686         output_info->resource = output_resource;
687         output_info->output = output;
688         LIST_INITHEAD(&output_info->vblank_list);
689
690         wl_resource_set_implementation(output_resource, &tdm_output_implementation,
691                                                                    output_info, destroy_output_callback);
692
693         ret = tdm_output_get_conn_status(output, &status);
694         wl_tdm_output_send_connection(output_resource, status, ret);
695
696         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
697                 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
698                 const tdm_output_mode *mode = NULL;
699                 unsigned int hdisplay, vdisplay, vrefresh;
700
701                 ret = tdm_output_get_mode(output, &mode);
702                 hdisplay = (mode) ? mode->hdisplay : 0;
703                 vdisplay = (mode) ? mode->vdisplay : 0;
704                 vrefresh = (mode) ? mode->vrefresh : 0;
705                 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
706
707                 ret = tdm_output_get_dpms(output, &dpms_value);
708                 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
709         } else {
710                 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
711                 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
712         }
713 }
714
715 static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource)
716 {
717         tdm_server_voutput_info *voutput_info;
718         tdm_private_server *private_server;
719         tdm_output *output;
720         tdm_error ret = TDM_ERROR_NONE;
721
722         voutput_info = wl_resource_get_user_data(resource);
723
724         private_server = voutput_info->private_server;
725         output = voutput_info->output;
726
727         if (output)
728                 ret = tdm_display_destroy_output(private_server->private_loop->dpy, output);
729
730         if (ret != TDM_ERROR_NONE)
731                 TDM_ERR("_tdm_voutput_cb_destroy fail");
732
733         wl_resource_destroy(resource);
734 }
735
736 static void
737 _tdm_voutput_cb_set_available_modes(struct wl_client *client,
738                                                                         struct wl_resource *resource,
739                                                                         struct wl_array *modes)
740 {
741         tdm_server_voutput_info *voutput_info;
742         tdm_output_mode *mode;
743         int size, count = 0, i = 0;
744
745         voutput_info = wl_resource_get_user_data(resource);
746
747         voutput_info->available_modes.count = 0;
748         if (voutput_info->available_modes.modes)
749                 free(voutput_info->available_modes.modes);
750
751         wl_array_for_each(mode, modes)
752                 count++;
753         size = sizeof(tdm_output_mode);
754
755         voutput_info->available_modes.modes = malloc(count * size);
756         voutput_info->available_modes.count = count;
757
758         wl_array_for_each(mode, modes)
759                 memcpy(&voutput_info->available_modes.modes[i++], mode, size);
760 }
761
762 static void
763 _tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource,
764                                                                   unsigned int mmwidth, unsigned int mmheight)
765 {
766         tdm_server_voutput_info *voutput_info;
767
768         voutput_info = wl_resource_get_user_data(resource);
769
770         voutput_info->mmwidth = mmwidth;
771         voutput_info->mmheight = mmheight;
772 }
773
774 static void
775 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
776 {
777         tdm_server_voutput_info *voutput_info;
778
779         voutput_info = wl_resource_get_user_data(resource);
780         voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
781
782         tdm_output_set_physical_size(voutput_info->output, voutput_info->mmwidth, voutput_info->mmheight);
783         tdm_output_set_available_mode(voutput_info->output, voutput_info->available_modes.modes, voutput_info->available_modes.count);
784         tdm_output_set_connect(voutput_info->output);
785 }
786
787 static void
788 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
789 {
790         tdm_server_voutput_info *voutput_info;
791
792         voutput_info = wl_resource_get_user_data(resource);
793         voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
794
795         /* Do free resources when it's being disconnected */
796         free(voutput_info->available_modes.modes);
797         voutput_info->available_modes.modes = NULL;
798         voutput_info->available_modes.count = 0;
799         voutput_info->mmwidth = 0;
800         voutput_info->mmheight = 0;
801
802         tdm_output_set_disconnect(voutput_info->output);
803 }
804
805 static void
806 _tdm_voutput_cb_commit_done(struct wl_client *client, struct wl_resource *resource)
807 {
808         tdm_server_voutput_info *voutput_info;
809
810         voutput_info = wl_resource_get_user_data(resource);
811         if (voutput_info->status != TDM_OUTPUT_CONN_STATUS_CONNECTED)
812         {
813                 // handle error
814                 return;
815         }
816 }
817
818 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
819         _tdm_voutput_cb_destroy,
820         _tdm_voutput_cb_set_available_modes,
821         _tdm_voutput_cb_set_physical_size,
822         _tdm_voutput_cb_connect,
823         _tdm_voutput_cb_disconnect,
824         _tdm_voutput_cb_commit_done
825 };
826
827 static void
828 _tdm_voutput_wl_buffer_destroy(struct wl_client *client, struct wl_resource *wl_buffer)
829 {
830         wl_resource_destroy(wl_buffer);
831 }
832
833 static const struct wl_buffer_interface _tdm_voutput_buffer_impementation = {
834         _tdm_voutput_wl_buffer_destroy
835 };
836
837 static void
838 _tdm_voutput_buffer_destory(struct wl_resource *wl_buffer)
839 {
840         tdm_server_voutput_info *voutput_info = NULL;
841         tdm_server_voutput_buffer *vb = NULL;
842
843         voutput_info = (tdm_server_voutput_info *)wl_resource_get_user_data(wl_buffer);
844         TDM_RETURN_IF_FAIL(voutput_info != NULL);
845
846         LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
847                 if (vb->wl_buffer == wl_buffer) {
848                         tbm_surface_internal_unref(vb->surface);
849                         wl_resource_set_user_data(wl_buffer, NULL);
850                         free(vb);
851                 }
852         }
853 }
854
855 struct wl_resource *
856 _tdm_voutput_create_wl_buffer(tdm_server_voutput_info *voutput_info)
857 {
858         struct wl_client *wl_client;
859         struct wl_resource *wl_buffer = NULL;
860
861         wl_client = wl_resource_get_client(voutput_info->resource);
862
863         /* create a wl_buffer resource */
864         wl_buffer = wl_resource_create(wl_client, &wl_buffer_interface, 1, 0);
865         TDM_RETURN_VAL_IF_FAIL(wl_buffer != NULL, NULL);
866
867         wl_resource_set_implementation(wl_buffer,
868                                (void (**)(void)) &_tdm_voutput_buffer_impementation,
869                                voutput_info, _tdm_voutput_buffer_destory);
870
871         return wl_buffer;
872 }
873
874 struct wl_resource *
875 _tdm_voutput_export_buffer(tdm_server_voutput_info *voutput_info,
876                                                    tbm_surface_h surface)
877 {
878         int bufs[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
879         struct wl_resource *wl_buffer = NULL;
880         int num_buf, is_fd = -1, i;
881         tbm_surface_info_s info;
882         uint32_t flags = 0;
883         struct wl_array plane_buf_idx, plane_offset, plane_stride, plane_size;
884         int *p;
885
886         TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, NULL);
887         TDM_RETURN_VAL_IF_FAIL(surface != NULL, NULL);
888
889         if (tbm_surface_get_info(surface, &info) != TBM_SURFACE_ERROR_NONE) {
890                 TDM_ERR("Failed to create buffer from surface");
891                 return NULL;
892         }
893
894         if (info.num_planes > 3) {
895                 TDM_ERR("invalid num_planes(%d)", info.num_planes);
896                 return NULL;
897         }
898
899         num_buf = tbm_surface_internal_get_num_bos(surface);
900         if (num_buf == 0) {
901                 TDM_ERR("surface doesn't have any bo.");
902                 return NULL;
903         }
904
905         for (i = 0; i < num_buf; i++) {
906                 tbm_bo bo = tbm_surface_internal_get_bo(surface, i);
907                 if (bo == NULL) {
908                         TDM_ERR("Failed to get bo from surface");
909                         goto err;
910                 }
911
912                 /* try to get fd first */
913                 if (is_fd == -1 || is_fd == 1) {
914                         bufs[i] = tbm_bo_export_fd(bo);
915                         if (is_fd == -1 && bufs[i] >= 0)
916                                 is_fd = 1;
917                 }
918
919                 /* if fail to get fd, try to get name second */
920                 if (is_fd == -1 || is_fd == 0) {
921                         bufs[i] = tbm_bo_export(bo);
922                         if (is_fd == -1 && bufs[i] > 0)
923                                 is_fd = 0;
924                 }
925
926                 if (is_fd == -1 ||
927                     (is_fd == 1 && bufs[i] < 0) ||
928                     (is_fd == 0 && bufs[i] <= 0)) {
929                         TDM_ERR("Failed to export(is_fd:%d, bufs:%d)", is_fd, bufs[i]);
930                         goto err;
931                 }
932         }
933
934         wl_buffer = _tdm_voutput_create_wl_buffer(voutput_info);
935         if (!wl_buffer) {
936                 TDM_ERR("Failed to create wl_buffer");
937                 goto err;
938         }
939
940         wl_array_init(&plane_buf_idx);
941         wl_array_init(&plane_offset);
942         wl_array_init(&plane_stride);
943         wl_array_init(&plane_size);
944
945         for (i = 0; i < 3; i++) {
946                 p = wl_array_add(&plane_buf_idx, sizeof(int));
947                 *p = tbm_surface_internal_get_plane_bo_idx(surface, i);
948                 p = wl_array_add(&plane_offset, sizeof(int));
949                 *p = info.planes[i].offset;
950                 p = wl_array_add(&plane_stride, sizeof(int));
951                 *p = info.planes[i].stride;
952                 p = wl_array_add(&plane_size, sizeof(int));
953                 *p = info.planes[i].size;
954         }
955
956         if (is_fd == 1)
957                 wl_tdm_voutput_send_buffer_set_with_fd(voutput_info->resource,
958                                 wl_buffer,
959                                 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
960                                 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
961                                 flags, num_buf, bufs[0],
962                                 (bufs[1] == -1) ? bufs[0] : bufs[1],
963                                 (bufs[2] == -1) ? bufs[0] : bufs[2]);
964         else
965                 wl_tdm_voutput_send_buffer_set_with_id(voutput_info->resource,
966                                 wl_buffer,
967                                 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
968                                 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
969                                 flags,
970                                 num_buf, bufs[0], bufs[1], bufs[2]);
971
972         wl_array_release(&plane_buf_idx);
973         wl_array_release(&plane_offset);
974         wl_array_release(&plane_stride);
975         wl_array_release(&plane_size);
976
977         for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
978                 if (is_fd == 1 && (bufs[i] >= 0))
979                         close(bufs[i]);
980         }
981
982         return wl_buffer;
983
984 err:
985         for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
986                 if (is_fd == 1 && (bufs[i] >= 0))
987                         close(bufs[i]);
988         }
989
990         return NULL;
991 }
992
993 tdm_server_voutput_buffer *
994 _tdm_output_get_voutput_buffer(tdm_server_voutput_info *voutput_info, tbm_surface_h surface)
995 {
996         tdm_server_voutput_buffer *voutput_buffer = NULL, *vb = NULL;
997
998         LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
999                 if (vb && vb->surface == surface)
1000                         return vb;
1001         }
1002
1003         tbm_surface_internal_ref(surface);
1004         voutput_buffer = calloc(1, sizeof *voutput_buffer);
1005         if (!voutput_buffer) {
1006                 /* LCOV_EXCL_START */
1007
1008                 TDM_ERR("fail calloc");
1009                 tbm_surface_internal_unref(surface);
1010                 return NULL;
1011
1012                 /* LCOV_EXCL_STOP */
1013         }
1014
1015         voutput_buffer->wl_buffer = _tdm_voutput_export_buffer(voutput_info, surface);
1016         if (!voutput_buffer->wl_buffer) {
1017                 /* LCOV_EXCL_START */
1018
1019                 TDM_ERR("fail export buffer");
1020                 free(voutput_buffer);
1021                 tbm_surface_internal_unref(surface);
1022                 return NULL;
1023
1024                 /* LCOV_EXCL_STOP */
1025         }
1026
1027         voutput_buffer->surface = surface;
1028         LIST_ADDTAIL(&voutput_info->link, &voutput_info->buffer_list);
1029
1030         return voutput_buffer;
1031 }
1032
1033 tdm_error
1034 tdm_output_send_buffer(tdm_output *output, tbm_surface_h surface)
1035 {
1036         tdm_private_server *private_server = keep_private_server;
1037         tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1038         tdm_server_voutput_buffer *voutput_buffer = NULL;
1039
1040         TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1041
1042         LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1043                 if (vo && vo->output == output) {
1044                         voutput_info = vo;
1045                         break;
1046                 }
1047         }
1048         TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1049
1050         voutput_buffer = _tdm_output_get_voutput_buffer(voutput_info, surface);
1051         TDM_RETURN_VAL_IF_FAIL(voutput_buffer != NULL, TDM_ERROR_OUT_OF_MEMORY);
1052
1053         voutput_info->attach_buffer = voutput_buffer;
1054
1055         return TDM_ERROR_NONE;
1056 }
1057
1058 tdm_error
1059 tdm_output_commit_buffer(tdm_output *output)
1060 {
1061         tdm_private_server *private_server = keep_private_server;
1062         tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1063
1064         TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1065
1066         LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1067                 if (vo && vo->output == output) {
1068                         voutput_info = vo;
1069                         break;
1070                 }
1071         }
1072         TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1073         TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
1074
1075         wl_tdm_voutput_send_commit(voutput_info->resource);
1076
1077         return TDM_ERROR_NONE;
1078 }
1079
1080 void
1081 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
1082 {
1083         tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
1084         tdm_server_voutput_buffer *vb, *vbb;
1085         TDM_RETURN_IF_FAIL(voutput_info != NULL);
1086
1087         LIST_FOR_EACH_ENTRY_SAFE(vb, vbb, &voutput_info->buffer_list, link) {
1088                 if (!vb) continue;
1089
1090                 LIST_DEL(&vb->link);
1091
1092                 if (vb->wl_buffer)
1093                         wl_resource_destroy(vb->wl_buffer);
1094         }
1095
1096         LIST_DEL(&voutput_info->link);
1097
1098         /* Do free your own resource */
1099         free(voutput_info);
1100 }
1101
1102 static void
1103 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
1104 {
1105         struct wl_resource *voutput_resource = NULL;
1106         tdm_private_server *private_server = wl_resource_get_user_data(resource);
1107         tdm_server_voutput_info *voutput_info;
1108         tdm_output *output;
1109         tdm_error ret;
1110
1111         output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
1112         if (output) {
1113                 TDM_ERR("There is '%s' output, cannot create.", name);
1114                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1115                                                            "There is '%s' output", name);
1116                 return;
1117         }
1118
1119         output = tdm_display_create_output(private_server->private_loop->dpy, name, &ret);
1120         if (!output) {
1121                 TDM_ERR("output creation fail(%s)(%d).", name, ret);
1122                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
1123                                                            "%s output creation fail", name);
1124                 return;
1125         }
1126
1127         voutput_resource =
1128                 wl_resource_create(client, &wl_tdm_voutput_interface,
1129                                                    wl_resource_get_version(resource), id);
1130         if (!voutput_resource) {
1131                 /* LCOV_EXCL_START */
1132
1133                 wl_resource_post_no_memory(resource);
1134                 TDM_ERR("wl_resource_create failed");
1135                 return;
1136
1137                 /* LCOV_EXCL_STOP */
1138         }
1139
1140         voutput_info = calloc(1, sizeof * voutput_info);
1141         if (!voutput_info) {
1142                 /* LCOV_EXCL_START */
1143
1144                 wl_resource_post_no_memory(resource);
1145                 wl_resource_destroy(voutput_resource);
1146                 TDM_ERR("alloc failed");
1147                 return;
1148
1149                 /* LCOV_EXCL_STOP */
1150         }
1151
1152         LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
1153         voutput_info->private_server = private_server;
1154         voutput_info->resource = voutput_resource;
1155         voutput_info->output = output;
1156         LIST_INITHEAD(&voutput_info->output_list);
1157         LIST_INITHEAD(&voutput_info->buffer_list);
1158
1159         wl_resource_set_implementation(voutput_resource,
1160                                                                    &tdm_voutput_implementation,
1161                                                                    voutput_info,
1162                                                                    tdm_voutput_cb_resource_destroy);
1163
1164         wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
1165 }
1166
1167 /* LCOV_EXCL_START */
1168 static void
1169 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
1170 {
1171         tdm_private_server *private_server = wl_resource_get_user_data(resource);
1172         tdm_private_loop *private_loop = private_server->private_loop;
1173         char message[TDM_SERVER_REPLY_MSG_LEN];
1174         char *m;
1175         int len = sizeof(message), size;
1176         uid_t uid;
1177
1178         wl_client_get_credentials(client, NULL, &uid, NULL);
1179
1180         if (uid != 0) {
1181                 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
1182                 TDM_ERR("%s", message);
1183         } else {
1184                 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
1185         }
1186
1187         size = sizeof(message) - len;
1188         m = message;
1189
1190         wl_client_flush(client);
1191
1192         while (size > 0) {
1193                 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
1194                 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
1195
1196                 strncpy(buffer, m, copylen);
1197                 m += copylen;
1198                 size -= copylen;
1199
1200                 buffer[copylen] = '\0';
1201
1202                 wl_tdm_send_debug_message(resource, buffer);
1203         }
1204
1205         wl_tdm_send_debug_done(resource);
1206 }
1207 /* LCOV_EXCL_STOP */
1208
1209 static const struct wl_tdm_interface tdm_implementation = {
1210         _tdm_server_cb_debug,
1211         _tdm_server_cb_create_output,
1212         _tdm_server_cb_create_virtual_output
1213 };
1214
1215 static void
1216 destroy_client(struct wl_resource *resource)
1217 {
1218         tdm_server_client_info *c = NULL, *cc = NULL;
1219         struct wl_client *client;
1220
1221         client = wl_resource_get_client(resource);
1222         TDM_RETURN_IF_FAIL(client != NULL);
1223
1224         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1225                 if (c->resource == resource) {
1226                         LIST_DEL(&c->link);
1227                         free(c);
1228                         return;
1229                 }
1230         }
1231 }
1232
1233 static void
1234 _tdm_server_bind(struct wl_client *client, void *data,
1235                                  uint32_t version, uint32_t id)
1236 {
1237         struct wl_resource *resource;
1238         tdm_server_client_info *cinfo;
1239
1240         resource = wl_resource_create(client, &wl_tdm_interface, version, id);
1241         if (!resource) {
1242                 /* LCOV_EXCL_START */
1243
1244                 wl_client_post_no_memory(client);
1245                 return;
1246
1247                 /* LCOV_EXCL_STOP */
1248         }
1249
1250         cinfo = calloc(1, sizeof(tdm_server_client_info));
1251         if (!cinfo) {
1252                 /* LCOV_EXCL_START */
1253
1254                 wl_client_post_no_memory(client);
1255                 wl_resource_destroy(resource);
1256                 return;
1257
1258                 /* LCOV_EXCL_STOP */
1259         }
1260
1261         cinfo->resource = resource;
1262
1263         LIST_ADDTAIL(&cinfo->link, &client_list);
1264         wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
1265         _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
1266
1267         wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1268 }
1269
1270 INTERN tdm_error
1271 tdm_server_init(tdm_private_loop *private_loop)
1272 {
1273         tdm_private_server *private_server;
1274
1275         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1276         TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1277
1278         if (private_loop->private_server)
1279                 return TDM_ERROR_NONE;
1280
1281         if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1282                 /* LCOV_EXCL_START */
1283
1284                 TDM_ERR("createing a tdm-socket failed");
1285                 return TDM_ERROR_OPERATION_FAILED;
1286
1287                 /* LCOV_EXCL_STOP */
1288         }
1289
1290         private_server = calloc(1, sizeof * private_server);
1291         if (!private_server) {
1292                 TDM_ERR("alloc failed");
1293                 return TDM_ERROR_OUT_OF_MEMORY;
1294         }
1295
1296         LIST_INITHEAD(&private_server->output_list);
1297         LIST_INITHEAD(&private_server->voutput_list);
1298         LIST_INITHEAD(&private_server->wait_list);
1299
1300         if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1301                                                   private_server, _tdm_server_bind)) {
1302                 /* LCOV_EXCL_START */
1303
1304                 TDM_ERR("creating a global resource failed");
1305                 free(private_server);
1306                 return TDM_ERROR_OUT_OF_MEMORY;
1307
1308                 /* LCOV_EXCL_STOP */
1309         }
1310
1311         private_server->private_loop = private_loop;
1312         private_loop->private_server = private_server;
1313         keep_private_server = private_server;
1314
1315         LIST_INITHEAD(&client_list);
1316
1317         return TDM_ERROR_NONE;
1318 }
1319
1320 INTERN void
1321 tdm_server_deinit(tdm_private_loop *private_loop)
1322 {
1323         tdm_server_output_info *o = NULL, *oo = NULL;
1324         tdm_server_voutput_info *vo = NULL, *voo = NULL;
1325         tdm_server_wait_info *w = NULL, *ww = NULL;
1326         tdm_server_client_info *c = NULL, *cc = NULL;
1327         tdm_private_server *private_server;
1328
1329         if (!private_loop->private_server)
1330                 return;
1331
1332         private_server = private_loop->private_server;
1333
1334         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1335                 destroy_wait(w);
1336         }
1337
1338         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1339                 wl_resource_destroy(o->resource);
1340         }
1341
1342         LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1343                 wl_resource_destroy(vo->resource);
1344         }
1345
1346         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1347                 wl_resource_destroy(c->resource);
1348         }
1349
1350         free(private_server);
1351         private_loop->private_server = NULL;
1352         keep_private_server = NULL;
1353 }
1354
1355 /* LCOV_EXCL_START */
1356 INTERN const char*
1357 tdm_server_get_client_name(pid_t pid)
1358 {
1359         tdm_server_client_info *c = NULL;
1360
1361         LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1362                 if (c->pid == pid)
1363                         return (const char*)c->name;
1364         }
1365
1366         return NULL;
1367 }
1368 /* LCOV_EXCL_STOP */
1369
1370 /* LCOV_EXCL_START */
1371 INTERN tdm_error
1372 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1373 {
1374         tdm_private_server *private_server = keep_private_server;
1375         tdm_server_output_info *output_info = NULL;
1376
1377         if (!keep_private_server)
1378                 return TDM_ERROR_NONE;
1379
1380         LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1381                 tdm_server_vblank_info *vblank_info = NULL;
1382
1383                 if (output && output_info->output != output)
1384                         continue;
1385
1386                 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1387                         wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1388                 }
1389         }
1390
1391         return TDM_ERROR_NONE;
1392 }
1393 /* LCOV_EXCL_STOP */