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