server: fix voutput buffer ref 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_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->request_commit == 1) {
930                 tdm_output_unset_voutput_commit(voutput_info->voutput);
931                 voutput_info->request_commit = 0;
932         }
933
934         if (voutput_info->attach_buffer) {
935                 tbm_surface_h buffer = voutput_info->attach_buffer->buffer;
936                 tbm_surface_internal_unref(buffer);
937                 voutput_info->committing = 0;
938                 voutput_info->attach_buffer = NULL;
939                 tdm_voutput_commit_done(voutput_info->voutput);
940         }
941
942         tdm_voutput_disconnect(voutput_info->voutput);
943 }
944
945 static void
946 _tdm_voutput_cb_commit_done(struct wl_client *client, struct wl_resource *resource)
947 {
948         tdm_server_voutput_info *voutput_info;
949         tbm_surface_h buffer;
950
951         voutput_info = wl_resource_get_user_data(resource);
952         if (voutput_info->status != TDM_OUTPUT_CONN_STATUS_CONNECTED) {
953                 TDM_DBG("not connected.");
954                 return;
955         }
956
957         buffer = voutput_info->attach_buffer->buffer;
958         tbm_surface_internal_unref(buffer);
959         voutput_info->committing = 0;
960         voutput_info->attach_buffer = NULL;
961
962         if (voutput_info->request_commit == 1)
963                 tdm_output_set_voutput_commit(voutput_info->voutput);
964
965         tdm_voutput_commit_done(voutput_info->voutput);
966 }
967
968 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
969         _tdm_voutput_cb_destroy,
970         _tdm_voutput_cb_set_available_modes,
971         _tdm_voutput_cb_set_physical_size,
972         _tdm_voutput_cb_set_mode,
973         _tdm_voutput_cb_connect,
974         _tdm_voutput_cb_disconnect,
975         _tdm_voutput_cb_commit_done
976 };
977
978 static void
979 _tdm_voutput_wl_buffer_destroy(struct wl_client *client, struct wl_resource *wl_buffer)
980 {
981         wl_resource_destroy(wl_buffer);
982 }
983
984 static const struct wl_buffer_interface _tdm_voutput_buffer_impementation = {
985         _tdm_voutput_wl_buffer_destroy
986 };
987
988 static void
989 _tdm_voutput_buffer_destory(struct wl_resource *wl_buffer)
990 {
991         tdm_server_voutput_info *voutput_info = NULL;
992         tdm_server_voutput_buffer *vb = NULL, *vbb = NULL;
993
994         voutput_info = (tdm_server_voutput_info *)wl_resource_get_user_data(wl_buffer);
995         TDM_RETURN_IF_FAIL(voutput_info != NULL);
996
997         LIST_FOR_EACH_ENTRY_SAFE(vb, vbb, &voutput_info->buffer_list, link) {
998                 if (vb->wl_buffer == wl_buffer) {
999                         tbm_surface_internal_unref(vb->buffer);
1000                         wl_resource_set_user_data(wl_buffer, NULL);
1001                         LIST_DEL(&vb->link);
1002                         free(vb);
1003                 }
1004         }
1005 }
1006
1007 struct wl_resource *
1008 _tdm_voutput_create_wl_buffer(tdm_server_voutput_info *voutput_info)
1009 {
1010         struct wl_client *wl_client;
1011         struct wl_resource *wl_buffer = NULL;
1012
1013         wl_client = wl_resource_get_client(voutput_info->resource);
1014
1015         /* create a wl_buffer resource */
1016         wl_buffer = wl_resource_create(wl_client, &wl_buffer_interface, 1, 0);
1017         TDM_RETURN_VAL_IF_FAIL(wl_buffer != NULL, NULL);
1018
1019         wl_resource_set_implementation(wl_buffer,
1020                                (void (**)(void)) &_tdm_voutput_buffer_impementation,
1021                                voutput_info, _tdm_voutput_buffer_destory);
1022
1023         return wl_buffer;
1024 }
1025
1026 struct wl_resource *
1027 _tdm_voutput_export_buffer(tdm_server_voutput_info *voutput_info,
1028                                                    tbm_surface_h buffer)
1029 {
1030         int bufs[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
1031         struct wl_resource *wl_buffer = NULL;
1032         int num_buf, is_fd = -1, i;
1033         tbm_surface_info_s info;
1034         uint32_t flags = 0;
1035         struct wl_array plane_buf_idx, plane_offset, plane_stride, plane_size;
1036         int *p;
1037
1038         TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, NULL);
1039         TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
1040
1041         if (tbm_surface_get_info(buffer, &info) != TBM_SURFACE_ERROR_NONE) {
1042                 TDM_ERR("Failed to create buffer from surface");
1043                 return NULL;
1044         }
1045
1046         if (info.num_planes > 3) {
1047                 TDM_ERR("invalid num_planes(%d)", info.num_planes);
1048                 return NULL;
1049         }
1050
1051         num_buf = tbm_surface_internal_get_num_bos(buffer);
1052         if (num_buf == 0) {
1053                 TDM_ERR("surface doesn't have any bo.");
1054                 return NULL;
1055         }
1056
1057         for (i = 0; i < num_buf; i++) {
1058                 tbm_bo bo = tbm_surface_internal_get_bo(buffer, i);
1059                 if (bo == NULL) {
1060                         TDM_ERR("Failed to get bo from surface");
1061                         goto err;
1062                 }
1063
1064                 /* try to get fd first */
1065                 if (is_fd == -1 || is_fd == 1) {
1066                         bufs[i] = tbm_bo_export_fd(bo);
1067                         if (is_fd == -1 && bufs[i] >= 0)
1068                                 is_fd = 1;
1069                 }
1070
1071                 /* if fail to get fd, try to get name second */
1072                 if (is_fd == -1 || is_fd == 0) {
1073                         bufs[i] = tbm_bo_export(bo);
1074                         if (is_fd == -1 && bufs[i] > 0)
1075                                 is_fd = 0;
1076                 }
1077
1078                 if (is_fd == -1 ||
1079                     (is_fd == 1 && bufs[i] < 0) ||
1080                     (is_fd == 0 && bufs[i] <= 0)) {
1081                         TDM_ERR("Failed to export(is_fd:%d, bufs:%d)", is_fd, bufs[i]);
1082                         goto err;
1083                 }
1084         }
1085
1086         wl_buffer = _tdm_voutput_create_wl_buffer(voutput_info);
1087         if (!wl_buffer) {
1088                 TDM_ERR("Failed to create wl_buffer");
1089                 goto err;
1090         }
1091
1092         wl_array_init(&plane_buf_idx);
1093         wl_array_init(&plane_offset);
1094         wl_array_init(&plane_stride);
1095         wl_array_init(&plane_size);
1096
1097         for (i = 0; i < 3; i++) {
1098                 p = wl_array_add(&plane_buf_idx, sizeof(int));
1099                 *p = tbm_surface_internal_get_plane_bo_idx(buffer, i);
1100                 p = wl_array_add(&plane_offset, sizeof(int));
1101                 *p = info.planes[i].offset;
1102                 p = wl_array_add(&plane_stride, sizeof(int));
1103                 *p = info.planes[i].stride;
1104                 p = wl_array_add(&plane_size, sizeof(int));
1105                 *p = info.planes[i].size;
1106         }
1107
1108         if (is_fd == 1)
1109                 wl_tdm_voutput_send_buffer_set_with_fd(voutput_info->resource,
1110                                 wl_buffer,
1111                                 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
1112                                 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
1113                                 flags, num_buf, bufs[0],
1114                                 (bufs[1] == -1) ? bufs[0] : bufs[1],
1115                                 (bufs[2] == -1) ? bufs[0] : bufs[2]);
1116         else
1117                 wl_tdm_voutput_send_buffer_set_with_id(voutput_info->resource,
1118                                 wl_buffer,
1119                                 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
1120                                 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
1121                                 flags,
1122                                 num_buf, bufs[0], bufs[1], bufs[2]);
1123
1124         wl_array_release(&plane_buf_idx);
1125         wl_array_release(&plane_offset);
1126         wl_array_release(&plane_stride);
1127         wl_array_release(&plane_size);
1128
1129         for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
1130                 if (is_fd == 1 && (bufs[i] >= 0))
1131                         close(bufs[i]);
1132         }
1133
1134         return wl_buffer;
1135
1136 err:
1137         for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
1138                 if (is_fd == 1 && (bufs[i] >= 0))
1139                         close(bufs[i]);
1140         }
1141
1142         return NULL;
1143 }
1144
1145 tdm_server_voutput_buffer *
1146 _tdm_output_get_voutput_buffer(tdm_server_voutput_info *voutput_info, tbm_surface_h buffer)
1147 {
1148         tdm_server_voutput_buffer *voutput_buffer = NULL, *vb = NULL;
1149
1150         LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
1151                 if (vb && vb->buffer == 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         tbm_surface_internal_ref(buffer);
1210         wl_tdm_voutput_send_attach_buffer(voutput_info->resource, voutput_buffer->wl_buffer);
1211
1212         return TDM_ERROR_NONE;
1213 }
1214
1215 tdm_error
1216 tdm_voutput_commit_buffer(tdm_voutput *voutput)
1217 {
1218         tdm_private_server *private_server = keep_private_server;
1219         tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1220
1221         TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1222
1223         LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1224                 if (vo && vo->voutput == voutput) {
1225                         voutput_info = vo;
1226                         break;
1227                 }
1228         }
1229         TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1230         TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer != NULL, TDM_ERROR_OPERATION_FAILED);
1231         TDM_RETURN_VAL_IF_FAIL(voutput_info->committing == 0, TDM_ERROR_OPERATION_FAILED);
1232
1233         voutput_info->committing = 1;
1234
1235         wl_tdm_voutput_send_commit(voutput_info->resource);
1236
1237         return TDM_ERROR_NONE;
1238 }
1239
1240 void
1241 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
1242 {
1243         tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
1244         tdm_server_voutput_buffer *vb = NULL;
1245         tdm_voutput *voutput;
1246         tdm_error ret = TDM_ERROR_NONE;
1247
1248         TDM_RETURN_IF_FAIL(voutput_info != NULL);
1249
1250         voutput = voutput_info->voutput;
1251
1252         if (voutput_info->request_commit)
1253                 tdm_output_unset_voutput_commit(voutput_info->voutput);
1254
1255         if (voutput) {
1256                 if (voutput_info->request_commit == 1) {
1257                         tdm_output_unset_voutput_commit(voutput_info->voutput);
1258                         voutput_info->request_commit = 0;
1259                         tdm_voutput_disconnect(voutput_info->voutput);
1260                 }
1261
1262                 ret = tdm_voutput_destroy(voutput);
1263                 if (ret != TDM_ERROR_NONE)
1264                         TDM_ERR("_tdm_voutput_cb_destroy fail");
1265         }
1266
1267         if (voutput_info->attach_buffer) {
1268                 tbm_surface_h buffer = voutput_info->attach_buffer->buffer;
1269                 tbm_surface_internal_unref(buffer);
1270                 voutput_info->committing = 0;
1271                 voutput_info->attach_buffer = NULL;
1272         }
1273
1274         LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
1275                 if (vb->wl_buffer)
1276                         wl_resource_destroy(vb->wl_buffer);
1277         }
1278
1279         LIST_DEL(&voutput_info->link);
1280
1281         /* Do free your own resource */
1282         free(voutput_info);
1283 }
1284
1285 static void
1286 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
1287 {
1288         struct wl_resource *voutput_resource = NULL;
1289         tdm_private_server *private_server = wl_resource_get_user_data(resource);
1290         tdm_server_voutput_info *voutput_info;
1291         tdm_voutput *voutput;
1292         tdm_output *output;
1293         tdm_error ret;
1294
1295         output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
1296         if (output) {
1297                 TDM_ERR("There is '%s' output, cannot create.", name);
1298                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1299                                                            "There is '%s' output", name);
1300                 return;
1301         }
1302
1303         voutput = tdm_voutput_create(private_server->private_loop->dpy, name, &ret);
1304         if (!voutput) {
1305                 TDM_ERR("voutput creation fail(%s)(%d).", name, ret);
1306                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
1307                                                            "%s output creation fail", name);
1308                 return;
1309         }
1310
1311         output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
1312         if (!output) {
1313                 TDM_ERR("There is no '%s' output.", name);
1314                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1315                                                            "There is '%s' output", name);
1316                 return;
1317         }
1318
1319         voutput_resource =
1320                 wl_resource_create(client, &wl_tdm_voutput_interface,
1321                                                    wl_resource_get_version(resource), id);
1322         if (!voutput_resource) {
1323                 /* LCOV_EXCL_START */
1324
1325                 wl_resource_post_no_memory(resource);
1326                 TDM_ERR("wl_resource_create failed");
1327                 return;
1328
1329                 /* LCOV_EXCL_STOP */
1330         }
1331
1332         voutput_info = calloc(1, sizeof * voutput_info);
1333         if (!voutput_info) {
1334                 /* LCOV_EXCL_START */
1335
1336                 wl_resource_post_no_memory(resource);
1337                 wl_resource_destroy(voutput_resource);
1338                 TDM_ERR("alloc failed");
1339                 return;
1340
1341                 /* LCOV_EXCL_STOP */
1342         }
1343
1344         LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
1345         voutput_info->private_server = private_server;
1346         voutput_info->resource = voutput_resource;
1347         voutput_info->voutput = voutput;
1348         voutput_info->output = output;
1349         LIST_INITHEAD(&voutput_info->buffer_list);
1350
1351         wl_resource_set_implementation(voutput_resource,
1352                                                                    &tdm_voutput_implementation,
1353                                                                    voutput_info,
1354                                                                    tdm_voutput_cb_resource_destroy);
1355
1356         wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
1357 }
1358
1359 /* LCOV_EXCL_START */
1360 static void
1361 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
1362 {
1363         tdm_private_server *private_server = wl_resource_get_user_data(resource);
1364         tdm_private_loop *private_loop = private_server->private_loop;
1365         char message[TDM_SERVER_REPLY_MSG_LEN];
1366         char *m;
1367         int len = sizeof(message), size;
1368         uid_t uid;
1369
1370         wl_client_get_credentials(client, NULL, &uid, NULL);
1371
1372         if (uid != 0) {
1373                 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
1374                 TDM_ERR("%s", message);
1375         } else {
1376                 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
1377         }
1378
1379         size = sizeof(message) - len;
1380         m = message;
1381
1382         wl_client_flush(client);
1383
1384         while (size > 0) {
1385                 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
1386                 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
1387
1388                 strncpy(buffer, m, copylen);
1389                 m += copylen;
1390                 size -= copylen;
1391
1392                 buffer[copylen] = '\0';
1393
1394                 wl_tdm_send_debug_message(resource, buffer);
1395         }
1396
1397         wl_tdm_send_debug_done(resource);
1398 }
1399 /* LCOV_EXCL_STOP */
1400
1401 static const struct wl_tdm_interface tdm_implementation = {
1402         _tdm_server_cb_debug,
1403         _tdm_server_cb_create_output,
1404         _tdm_server_cb_create_virtual_output
1405 };
1406
1407 static void
1408 destroy_client(struct wl_resource *resource)
1409 {
1410         tdm_server_client_info *c = NULL, *cc = NULL;
1411         struct wl_client *client;
1412
1413         client = wl_resource_get_client(resource);
1414         TDM_RETURN_IF_FAIL(client != NULL);
1415
1416         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1417                 if (c->resource == resource) {
1418                         LIST_DEL(&c->link);
1419                         free(c);
1420                         return;
1421                 }
1422         }
1423 }
1424
1425 static void
1426 _tdm_server_bind(struct wl_client *client, void *data,
1427                                  uint32_t version, uint32_t id)
1428 {
1429         struct wl_resource *resource;
1430         tdm_server_client_info *cinfo;
1431
1432         resource = wl_resource_create(client, &wl_tdm_interface, version, id);
1433         if (!resource) {
1434                 /* LCOV_EXCL_START */
1435
1436                 wl_client_post_no_memory(client);
1437                 return;
1438
1439                 /* LCOV_EXCL_STOP */
1440         }
1441
1442         cinfo = calloc(1, sizeof(tdm_server_client_info));
1443         if (!cinfo) {
1444                 /* LCOV_EXCL_START */
1445
1446                 wl_client_post_no_memory(client);
1447                 wl_resource_destroy(resource);
1448                 return;
1449
1450                 /* LCOV_EXCL_STOP */
1451         }
1452
1453         cinfo->resource = resource;
1454
1455         LIST_ADDTAIL(&cinfo->link, &client_list);
1456         wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
1457         _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
1458
1459         wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1460 }
1461
1462 INTERN tdm_error
1463 tdm_server_init(tdm_private_loop *private_loop)
1464 {
1465         tdm_private_server *private_server;
1466
1467         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1468         TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1469
1470         if (private_loop->private_server)
1471                 return TDM_ERROR_NONE;
1472
1473         if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1474                 /* LCOV_EXCL_START */
1475
1476                 TDM_ERR("createing a tdm-socket failed");
1477                 return TDM_ERROR_OPERATION_FAILED;
1478
1479                 /* LCOV_EXCL_STOP */
1480         }
1481
1482         private_server = calloc(1, sizeof * private_server);
1483         if (!private_server) {
1484                 TDM_ERR("alloc failed");
1485                 return TDM_ERROR_OUT_OF_MEMORY;
1486         }
1487
1488         LIST_INITHEAD(&private_server->output_list);
1489         LIST_INITHEAD(&private_server->voutput_list);
1490         LIST_INITHEAD(&private_server->wait_list);
1491
1492         if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1493                                                   private_server, _tdm_server_bind)) {
1494                 /* LCOV_EXCL_START */
1495
1496                 TDM_ERR("creating a global resource failed");
1497                 free(private_server);
1498                 return TDM_ERROR_OUT_OF_MEMORY;
1499
1500                 /* LCOV_EXCL_STOP */
1501         }
1502
1503         private_server->private_loop = private_loop;
1504         private_loop->private_server = private_server;
1505         keep_private_server = private_server;
1506
1507         LIST_INITHEAD(&client_list);
1508
1509         return TDM_ERROR_NONE;
1510 }
1511
1512 INTERN void
1513 tdm_server_deinit(tdm_private_loop *private_loop)
1514 {
1515         tdm_server_output_info *o = NULL, *oo = NULL;
1516         tdm_server_voutput_info *vo = NULL, *voo = NULL;
1517         tdm_server_wait_info *w = NULL, *ww = NULL;
1518         tdm_server_client_info *c = NULL, *cc = NULL;
1519         tdm_private_server *private_server;
1520
1521         if (!private_loop->private_server)
1522                 return;
1523
1524         private_server = private_loop->private_server;
1525
1526         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1527                 destroy_wait(w);
1528         }
1529
1530         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1531                 wl_resource_destroy(o->resource);
1532         }
1533
1534         LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1535                 wl_resource_destroy(vo->resource);
1536         }
1537
1538         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1539                 wl_resource_destroy(c->resource);
1540         }
1541
1542         free(private_server);
1543         private_loop->private_server = NULL;
1544         keep_private_server = NULL;
1545 }
1546
1547 /* LCOV_EXCL_START */
1548 INTERN const char*
1549 tdm_server_get_client_name(pid_t pid)
1550 {
1551         tdm_server_client_info *c = NULL;
1552
1553         LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1554                 if (c->pid == pid)
1555                         return (const char*)c->name;
1556         }
1557
1558         return NULL;
1559 }
1560 /* LCOV_EXCL_STOP */
1561
1562 /* LCOV_EXCL_START */
1563 INTERN tdm_error
1564 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1565 {
1566         tdm_private_server *private_server = keep_private_server;
1567         tdm_server_output_info *output_info = NULL;
1568
1569         if (!keep_private_server)
1570                 return TDM_ERROR_NONE;
1571
1572         LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1573                 tdm_server_vblank_info *vblank_info = NULL;
1574
1575                 if (output && output_info->output != output)
1576                         continue;
1577
1578                 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1579                         wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1580                 }
1581         }
1582
1583         return TDM_ERROR_NONE;
1584 }
1585 /* LCOV_EXCL_STOP */