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