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