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