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