e51d868591c147f1922321b0a5e767b98850b276
[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 const struct wl_tdm_output_interface tdm_output_implementation = {
600         _tdm_server_output_cb_destroy,
601         _tdm_server_output_cb_create_vblank,
602         _tdm_server_output_cb_watch_output_changes,
603         _tdm_server_output_cb_get_connection,
604         _tdm_server_output_cb_get_mode,
605         _tdm_server_output_cb_get_dpms,
606 };
607
608 static void
609 destroy_output_callback(struct wl_resource *resource)
610 {
611         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
612         tdm_server_vblank_info *v = NULL, *vv = NULL;
613
614         TDM_RETURN_IF_FAIL(output_info != NULL);
615
616         LIST_DEL(&output_info->link);
617
618         tdm_output_remove_change_handler(output_info->output,
619                                                                          _tdm_server_cb_output_change, output_info);
620
621         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
622                 wl_resource_destroy(v->resource);
623         }
624
625         free(output_info);
626 }
627
628 static void
629 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
630                                                          const char *name, uint32_t id)
631 {
632         tdm_private_server *private_server = wl_resource_get_user_data(resource);
633         tdm_server_output_info *output_info;
634         struct wl_resource *output_resource = NULL;
635         tdm_output *output;
636         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
637         tdm_error ret;
638
639         output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
640         if (!output) {
641                 /* LCOV_EXCL_START */
642
643                 TDM_ERR("There is no '%s' output", name);
644                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
645                                                            "There is no '%s' output", name);
646                 return;
647
648                 /* LCOV_EXCL_STOP */
649         }
650
651         output_resource =
652                 wl_resource_create(client, &wl_tdm_output_interface,
653                                                    wl_resource_get_version(resource), id);
654         if (!output_resource) {
655                 /* LCOV_EXCL_START */
656
657                 wl_resource_post_no_memory(resource);
658                 TDM_ERR("wl_resource_create failed");
659                 return;
660
661                 /* LCOV_EXCL_STOP */
662         }
663
664         output_info = calloc(1, sizeof * output_info);
665         if (!output_info) {
666                 /* LCOV_EXCL_START */
667
668                 wl_resource_post_no_memory(resource);
669                 wl_resource_destroy(output_resource);
670                 TDM_ERR("alloc failed");
671                 return;
672
673                 /* LCOV_EXCL_STOP */
674         }
675
676         ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
677         if (ret != TDM_ERROR_NONE) {
678                 wl_resource_post_no_memory(resource);
679                 wl_resource_destroy(output_resource);
680                 free(output_info);
681                 TDM_ERR("tdm_output_add_change_handler failed");
682                 return;
683         }
684
685         LIST_ADDTAIL(&output_info->link, &private_server->output_list);
686         output_info->private_server = private_server;
687         output_info->resource = output_resource;
688         output_info->output = output;
689         LIST_INITHEAD(&output_info->vblank_list);
690
691         wl_resource_set_implementation(output_resource, &tdm_output_implementation,
692                                                                    output_info, destroy_output_callback);
693
694         ret = tdm_output_get_conn_status(output, &status);
695         wl_tdm_output_send_connection(output_resource, status, ret);
696
697         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
698                 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
699                 const tdm_output_mode *mode = NULL;
700                 unsigned int hdisplay, vdisplay, vrefresh;
701
702                 ret = tdm_output_get_mode(output, &mode);
703                 hdisplay = (mode) ? mode->hdisplay : 0;
704                 vdisplay = (mode) ? mode->vdisplay : 0;
705                 vrefresh = (mode) ? mode->vrefresh : 0;
706                 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
707
708                 ret = tdm_output_get_dpms(output, &dpms_value);
709                 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
710         } else {
711                 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
712                 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
713         }
714 }
715
716 static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource)
717 {
718         wl_resource_destroy(resource);
719 }
720
721 static void
722 _tdm_voutput_cb_set_available_modes(struct wl_client *client,
723                                                                         struct wl_resource *resource,
724                                                                         struct wl_array *modes)
725 {
726         tdm_server_voutput_info *voutput_info;
727         tdm_output_mode *mode;
728         int size, count = 0, i = 0;
729
730         voutput_info = wl_resource_get_user_data(resource);
731
732         voutput_info->available_modes.count = 0;
733         if (voutput_info->available_modes.modes)
734                 free(voutput_info->available_modes.modes);
735
736         wl_array_for_each(mode, modes)
737                 count++;
738         size = sizeof(tdm_output_mode);
739
740         voutput_info->available_modes.modes = malloc(count * size);
741         voutput_info->available_modes.count = count;
742
743         wl_array_for_each(mode, modes)
744                 memcpy(&voutput_info->available_modes.modes[i++], mode, size);
745 }
746
747 static void
748 _tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource,
749                                                                   unsigned int mmwidth, unsigned int mmheight)
750 {
751         tdm_server_voutput_info *voutput_info;
752
753         voutput_info = wl_resource_get_user_data(resource);
754
755         voutput_info->mmwidth = mmwidth;
756         voutput_info->mmheight = mmheight;
757 }
758
759 static void
760 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
761 {
762         tdm_server_voutput_info *voutput_info;
763
764         voutput_info = wl_resource_get_user_data(resource);
765         voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
766
767         tdm_output_set_physical_size(voutput_info->output, voutput_info->mmwidth, voutput_info->mmheight);
768         tdm_output_set_available_mode(voutput_info->output, voutput_info->available_modes.modes, voutput_info->available_modes.count);
769         tdm_output_set_connect(voutput_info->output);
770 }
771
772 static void
773 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
774 {
775         tdm_server_voutput_info *voutput_info;
776
777         voutput_info = wl_resource_get_user_data(resource);
778         voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
779
780         /* Do free resources when it's being disconnected */
781         free(voutput_info->available_modes.modes);
782         voutput_info->available_modes.modes = NULL;
783         voutput_info->available_modes.count = 0;
784         voutput_info->mmwidth = 0;
785         voutput_info->mmheight = 0;
786
787         if (voutput_info->attach_buffer) {
788                 tbm_surface_h buffer = voutput_info->attach_buffer->buffer;
789                 tbm_surface_internal_unref(buffer);
790                 voutput_info->committing = 0;
791                 voutput_info->attach_buffer = NULL;
792         }
793
794         tdm_output_set_disconnect(voutput_info->output);
795 }
796
797 static void
798 _tdm_voutput_cb_commit_done(struct wl_client *client, struct wl_resource *resource)
799 {
800         tdm_server_voutput_info *voutput_info;
801         tbm_surface_h buffer;
802
803         voutput_info = wl_resource_get_user_data(resource);
804         if (voutput_info->status != TDM_OUTPUT_CONN_STATUS_CONNECTED) {
805                 TDM_DBG("not connected.");
806                 return;
807         }
808
809         buffer = voutput_info->attach_buffer->buffer;
810         tbm_surface_internal_unref(buffer);
811         voutput_info->committing = 0;
812         voutput_info->attach_buffer = NULL;
813
814         tdm_output_commit_done(voutput_info->output, buffer);
815 }
816
817 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
818         _tdm_voutput_cb_destroy,
819         _tdm_voutput_cb_set_available_modes,
820         _tdm_voutput_cb_set_physical_size,
821         _tdm_voutput_cb_connect,
822         _tdm_voutput_cb_disconnect,
823         _tdm_voutput_cb_commit_done
824 };
825
826 static void
827 _tdm_voutput_wl_buffer_destroy(struct wl_client *client, struct wl_resource *wl_buffer)
828 {
829         wl_resource_destroy(wl_buffer);
830 }
831
832 static const struct wl_buffer_interface _tdm_voutput_buffer_impementation = {
833         _tdm_voutput_wl_buffer_destroy
834 };
835
836 static void
837 _tdm_voutput_buffer_destory(struct wl_resource *wl_buffer)
838 {
839         tdm_server_voutput_info *voutput_info = NULL;
840         tdm_server_voutput_buffer *vb = NULL, *vbb = NULL;
841
842         voutput_info = (tdm_server_voutput_info *)wl_resource_get_user_data(wl_buffer);
843         TDM_RETURN_IF_FAIL(voutput_info != NULL);
844
845         LIST_FOR_EACH_ENTRY_SAFE(vb, vbb, &voutput_info->buffer_list, link) {
846                 if (vb->wl_buffer == wl_buffer) {
847                         tbm_surface_internal_unref(vb->buffer);
848                         wl_resource_set_user_data(wl_buffer, NULL);
849                         LIST_DEL(&vb->link);
850                         free(vb);
851                 }
852         }
853 }
854
855 struct wl_resource *
856 _tdm_voutput_create_wl_buffer(tdm_server_voutput_info *voutput_info)
857 {
858         struct wl_client *wl_client;
859         struct wl_resource *wl_buffer = NULL;
860
861         wl_client = wl_resource_get_client(voutput_info->resource);
862
863         /* create a wl_buffer resource */
864         wl_buffer = wl_resource_create(wl_client, &wl_buffer_interface, 1, 0);
865         TDM_RETURN_VAL_IF_FAIL(wl_buffer != NULL, NULL);
866
867         wl_resource_set_implementation(wl_buffer,
868                                (void (**)(void)) &_tdm_voutput_buffer_impementation,
869                                voutput_info, _tdm_voutput_buffer_destory);
870
871         return wl_buffer;
872 }
873
874 struct wl_resource *
875 _tdm_voutput_export_buffer(tdm_server_voutput_info *voutput_info,
876                                                    tbm_surface_h buffer)
877 {
878         int bufs[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
879         struct wl_resource *wl_buffer = NULL;
880         int num_buf, is_fd = -1, i;
881         tbm_surface_info_s info;
882         uint32_t flags = 0;
883         struct wl_array plane_buf_idx, plane_offset, plane_stride, plane_size;
884         int *p;
885
886         TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, NULL);
887         TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
888
889         if (tbm_surface_get_info(buffer, &info) != TBM_SURFACE_ERROR_NONE) {
890                 TDM_ERR("Failed to create buffer from surface");
891                 return NULL;
892         }
893
894         if (info.num_planes > 3) {
895                 TDM_ERR("invalid num_planes(%d)", info.num_planes);
896                 return NULL;
897         }
898
899         num_buf = tbm_surface_internal_get_num_bos(buffer);
900         if (num_buf == 0) {
901                 TDM_ERR("surface doesn't have any bo.");
902                 return NULL;
903         }
904
905         for (i = 0; i < num_buf; i++) {
906                 tbm_bo bo = tbm_surface_internal_get_bo(buffer, i);
907                 if (bo == NULL) {
908                         TDM_ERR("Failed to get bo from surface");
909                         goto err;
910                 }
911
912                 /* try to get fd first */
913                 if (is_fd == -1 || is_fd == 1) {
914                         bufs[i] = tbm_bo_export_fd(bo);
915                         if (is_fd == -1 && bufs[i] >= 0)
916                                 is_fd = 1;
917                 }
918
919                 /* if fail to get fd, try to get name second */
920                 if (is_fd == -1 || is_fd == 0) {
921                         bufs[i] = tbm_bo_export(bo);
922                         if (is_fd == -1 && bufs[i] > 0)
923                                 is_fd = 0;
924                 }
925
926                 if (is_fd == -1 ||
927                     (is_fd == 1 && bufs[i] < 0) ||
928                     (is_fd == 0 && bufs[i] <= 0)) {
929                         TDM_ERR("Failed to export(is_fd:%d, bufs:%d)", is_fd, bufs[i]);
930                         goto err;
931                 }
932         }
933
934         wl_buffer = _tdm_voutput_create_wl_buffer(voutput_info);
935         if (!wl_buffer) {
936                 TDM_ERR("Failed to create wl_buffer");
937                 goto err;
938         }
939
940         wl_array_init(&plane_buf_idx);
941         wl_array_init(&plane_offset);
942         wl_array_init(&plane_stride);
943         wl_array_init(&plane_size);
944
945         for (i = 0; i < 3; i++) {
946                 p = wl_array_add(&plane_buf_idx, sizeof(int));
947                 *p = tbm_surface_internal_get_plane_bo_idx(buffer, i);
948                 p = wl_array_add(&plane_offset, sizeof(int));
949                 *p = info.planes[i].offset;
950                 p = wl_array_add(&plane_stride, sizeof(int));
951                 *p = info.planes[i].stride;
952                 p = wl_array_add(&plane_size, sizeof(int));
953                 *p = info.planes[i].size;
954         }
955
956         if (is_fd == 1)
957                 wl_tdm_voutput_send_buffer_set_with_fd(voutput_info->resource,
958                                 wl_buffer,
959                                 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
960                                 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
961                                 flags, num_buf, bufs[0],
962                                 (bufs[1] == -1) ? bufs[0] : bufs[1],
963                                 (bufs[2] == -1) ? bufs[0] : bufs[2]);
964         else
965                 wl_tdm_voutput_send_buffer_set_with_id(voutput_info->resource,
966                                 wl_buffer,
967                                 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
968                                 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
969                                 flags,
970                                 num_buf, bufs[0], bufs[1], bufs[2]);
971
972         wl_array_release(&plane_buf_idx);
973         wl_array_release(&plane_offset);
974         wl_array_release(&plane_stride);
975         wl_array_release(&plane_size);
976
977         for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
978                 if (is_fd == 1 && (bufs[i] >= 0))
979                         close(bufs[i]);
980         }
981
982         return wl_buffer;
983
984 err:
985         for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
986                 if (is_fd == 1 && (bufs[i] >= 0))
987                         close(bufs[i]);
988         }
989
990         return NULL;
991 }
992
993 tdm_server_voutput_buffer *
994 _tdm_output_get_voutput_buffer(tdm_server_voutput_info *voutput_info, tbm_surface_h buffer)
995 {
996         tdm_server_voutput_buffer *voutput_buffer = NULL, *vb = NULL;
997
998         LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
999                 if (vb && vb->buffer == buffer) {
1000                         tbm_surface_internal_ref(vb->buffer);
1001                         return vb;
1002                 }
1003         }
1004
1005         tbm_surface_internal_ref(buffer);
1006         voutput_buffer = calloc(1, sizeof *voutput_buffer);
1007         if (!voutput_buffer) {
1008                 /* LCOV_EXCL_START */
1009
1010                 TDM_ERR("fail calloc");
1011                 tbm_surface_internal_unref(buffer);
1012                 return NULL;
1013
1014                 /* LCOV_EXCL_STOP */
1015         }
1016
1017         voutput_buffer->wl_buffer = _tdm_voutput_export_buffer(voutput_info, buffer);
1018         if (!voutput_buffer->wl_buffer) {
1019                 /* LCOV_EXCL_START */
1020
1021                 TDM_ERR("fail export buffer");
1022                 free(voutput_buffer);
1023                 tbm_surface_internal_unref(buffer);
1024                 return NULL;
1025
1026                 /* LCOV_EXCL_STOP */
1027         }
1028
1029         voutput_buffer->buffer = buffer;
1030         LIST_ADDTAIL(&voutput_buffer->link, &voutput_info->buffer_list);
1031
1032         return voutput_buffer;
1033 }
1034
1035 INTERN tdm_error
1036 tdm_output_attach_buffer(tdm_output *output, tbm_surface_h buffer)
1037 {
1038         tdm_private_server *private_server = keep_private_server;
1039         tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1040         tdm_server_voutput_buffer *voutput_buffer = NULL;
1041
1042         TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1043
1044         LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1045                 if (vo && vo->output == output) {
1046                         voutput_info = vo;
1047                         break;
1048                 }
1049         }
1050         TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1051         TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer == NULL, TDM_ERROR_OPERATION_FAILED);
1052
1053         voutput_buffer = _tdm_output_get_voutput_buffer(voutput_info, buffer);
1054         TDM_RETURN_VAL_IF_FAIL(voutput_buffer != NULL, TDM_ERROR_OUT_OF_MEMORY);
1055
1056         voutput_info->attach_buffer = voutput_buffer;
1057
1058         wl_tdm_voutput_send_attach_buffer(voutput_info->resource, voutput_buffer->wl_buffer);
1059
1060         return TDM_ERROR_NONE;
1061 }
1062
1063 INTERN tdm_error
1064 tdm_output_commit_buffer(tdm_output *output)
1065 {
1066         tdm_private_server *private_server = keep_private_server;
1067         tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1068
1069         TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1070
1071         LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1072                 if (vo && vo->output == output) {
1073                         voutput_info = vo;
1074                         break;
1075                 }
1076         }
1077         TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1078         TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer != NULL, TDM_ERROR_OPERATION_FAILED);
1079         TDM_RETURN_VAL_IF_FAIL(voutput_info->committing == 0, TDM_ERROR_OPERATION_FAILED);
1080
1081         tbm_surface_internal_ref(voutput_info->attach_buffer->buffer);
1082         voutput_info->committing = 1;
1083
1084         wl_tdm_voutput_send_commit(voutput_info->resource);
1085
1086         return TDM_ERROR_NONE;
1087 }
1088
1089 void
1090 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
1091 {
1092         tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
1093         tdm_server_voutput_buffer *vb;
1094         tdm_private_server *private_server;
1095         tdm_output *output;
1096         tdm_error ret = TDM_ERROR_NONE;
1097
1098         TDM_RETURN_IF_FAIL(voutput_info != NULL);
1099
1100         private_server = voutput_info->private_server;
1101         output = voutput_info->output;
1102
1103         if (output)
1104                 ret = tdm_display_destroy_output(private_server->private_loop->dpy, output);
1105         if (ret != TDM_ERROR_NONE)
1106                 TDM_ERR("_tdm_voutput_cb_destroy fail");
1107
1108         if (voutput_info->attach_buffer) {
1109                 tbm_surface_h buffer = voutput_info->attach_buffer->buffer;
1110                 tbm_surface_internal_unref(buffer);
1111                 voutput_info->committing = 0;
1112                 voutput_info->attach_buffer = NULL;
1113         }
1114
1115         LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
1116                 if (!vb) continue;
1117
1118                 if (vb->wl_buffer)
1119                         wl_resource_destroy(vb->wl_buffer);
1120         }
1121
1122         LIST_DEL(&voutput_info->link);
1123
1124         /* Do free your own resource */
1125         free(voutput_info);
1126 }
1127
1128 static void
1129 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
1130 {
1131         struct wl_resource *voutput_resource = NULL;
1132         tdm_private_server *private_server = wl_resource_get_user_data(resource);
1133         tdm_server_voutput_info *voutput_info;
1134         tdm_output *output;
1135         tdm_error ret;
1136
1137         output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
1138         if (output) {
1139                 TDM_ERR("There is '%s' output, cannot create.", name);
1140                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1141                                                            "There is '%s' output", name);
1142                 return;
1143         }
1144
1145         output = tdm_display_create_output(private_server->private_loop->dpy, name, &ret);
1146         if (!output) {
1147                 TDM_ERR("output creation fail(%s)(%d).", name, ret);
1148                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
1149                                                            "%s output creation fail", name);
1150                 return;
1151         }
1152
1153         voutput_resource =
1154                 wl_resource_create(client, &wl_tdm_voutput_interface,
1155                                                    wl_resource_get_version(resource), id);
1156         if (!voutput_resource) {
1157                 /* LCOV_EXCL_START */
1158
1159                 wl_resource_post_no_memory(resource);
1160                 TDM_ERR("wl_resource_create failed");
1161                 return;
1162
1163                 /* LCOV_EXCL_STOP */
1164         }
1165
1166         voutput_info = calloc(1, sizeof * voutput_info);
1167         if (!voutput_info) {
1168                 /* LCOV_EXCL_START */
1169
1170                 wl_resource_post_no_memory(resource);
1171                 wl_resource_destroy(voutput_resource);
1172                 TDM_ERR("alloc failed");
1173                 return;
1174
1175                 /* LCOV_EXCL_STOP */
1176         }
1177
1178         LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
1179         voutput_info->private_server = private_server;
1180         voutput_info->resource = voutput_resource;
1181         voutput_info->output = output;
1182         LIST_INITHEAD(&voutput_info->output_list);
1183         LIST_INITHEAD(&voutput_info->buffer_list);
1184
1185         wl_resource_set_implementation(voutput_resource,
1186                                                                    &tdm_voutput_implementation,
1187                                                                    voutput_info,
1188                                                                    tdm_voutput_cb_resource_destroy);
1189
1190         wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
1191 }
1192
1193 /* LCOV_EXCL_START */
1194 static void
1195 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
1196 {
1197         tdm_private_server *private_server = wl_resource_get_user_data(resource);
1198         tdm_private_loop *private_loop = private_server->private_loop;
1199         char message[TDM_SERVER_REPLY_MSG_LEN];
1200         char *m;
1201         int len = sizeof(message), size;
1202         uid_t uid;
1203
1204         wl_client_get_credentials(client, NULL, &uid, NULL);
1205
1206         if (uid != 0) {
1207                 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
1208                 TDM_ERR("%s", message);
1209         } else {
1210                 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
1211         }
1212
1213         size = sizeof(message) - len;
1214         m = message;
1215
1216         wl_client_flush(client);
1217
1218         while (size > 0) {
1219                 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
1220                 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
1221
1222                 strncpy(buffer, m, copylen);
1223                 m += copylen;
1224                 size -= copylen;
1225
1226                 buffer[copylen] = '\0';
1227
1228                 wl_tdm_send_debug_message(resource, buffer);
1229         }
1230
1231         wl_tdm_send_debug_done(resource);
1232 }
1233 /* LCOV_EXCL_STOP */
1234
1235 static const struct wl_tdm_interface tdm_implementation = {
1236         _tdm_server_cb_debug,
1237         _tdm_server_cb_create_output,
1238         _tdm_server_cb_create_virtual_output
1239 };
1240
1241 static void
1242 destroy_client(struct wl_resource *resource)
1243 {
1244         tdm_server_client_info *c = NULL, *cc = NULL;
1245         struct wl_client *client;
1246
1247         client = wl_resource_get_client(resource);
1248         TDM_RETURN_IF_FAIL(client != NULL);
1249
1250         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1251                 if (c->resource == resource) {
1252                         LIST_DEL(&c->link);
1253                         free(c);
1254                         return;
1255                 }
1256         }
1257 }
1258
1259 static void
1260 _tdm_server_bind(struct wl_client *client, void *data,
1261                                  uint32_t version, uint32_t id)
1262 {
1263         struct wl_resource *resource;
1264         tdm_server_client_info *cinfo;
1265
1266         resource = wl_resource_create(client, &wl_tdm_interface, version, id);
1267         if (!resource) {
1268                 /* LCOV_EXCL_START */
1269
1270                 wl_client_post_no_memory(client);
1271                 return;
1272
1273                 /* LCOV_EXCL_STOP */
1274         }
1275
1276         cinfo = calloc(1, sizeof(tdm_server_client_info));
1277         if (!cinfo) {
1278                 /* LCOV_EXCL_START */
1279
1280                 wl_client_post_no_memory(client);
1281                 wl_resource_destroy(resource);
1282                 return;
1283
1284                 /* LCOV_EXCL_STOP */
1285         }
1286
1287         cinfo->resource = resource;
1288
1289         LIST_ADDTAIL(&cinfo->link, &client_list);
1290         wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
1291         _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
1292
1293         wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1294 }
1295
1296 INTERN tdm_error
1297 tdm_server_init(tdm_private_loop *private_loop)
1298 {
1299         tdm_private_server *private_server;
1300
1301         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1302         TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1303
1304         if (private_loop->private_server)
1305                 return TDM_ERROR_NONE;
1306
1307         if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1308                 /* LCOV_EXCL_START */
1309
1310                 TDM_ERR("createing a tdm-socket failed");
1311                 return TDM_ERROR_OPERATION_FAILED;
1312
1313                 /* LCOV_EXCL_STOP */
1314         }
1315
1316         private_server = calloc(1, sizeof * private_server);
1317         if (!private_server) {
1318                 TDM_ERR("alloc failed");
1319                 return TDM_ERROR_OUT_OF_MEMORY;
1320         }
1321
1322         LIST_INITHEAD(&private_server->output_list);
1323         LIST_INITHEAD(&private_server->voutput_list);
1324         LIST_INITHEAD(&private_server->wait_list);
1325
1326         if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1327                                                   private_server, _tdm_server_bind)) {
1328                 /* LCOV_EXCL_START */
1329
1330                 TDM_ERR("creating a global resource failed");
1331                 free(private_server);
1332                 return TDM_ERROR_OUT_OF_MEMORY;
1333
1334                 /* LCOV_EXCL_STOP */
1335         }
1336
1337         private_server->private_loop = private_loop;
1338         private_loop->private_server = private_server;
1339         keep_private_server = private_server;
1340
1341         LIST_INITHEAD(&client_list);
1342
1343         return TDM_ERROR_NONE;
1344 }
1345
1346 INTERN void
1347 tdm_server_deinit(tdm_private_loop *private_loop)
1348 {
1349         tdm_server_output_info *o = NULL, *oo = NULL;
1350         tdm_server_voutput_info *vo = NULL, *voo = NULL;
1351         tdm_server_wait_info *w = NULL, *ww = NULL;
1352         tdm_server_client_info *c = NULL, *cc = NULL;
1353         tdm_private_server *private_server;
1354
1355         if (!private_loop->private_server)
1356                 return;
1357
1358         private_server = private_loop->private_server;
1359
1360         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1361                 destroy_wait(w);
1362         }
1363
1364         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1365                 wl_resource_destroy(o->resource);
1366         }
1367
1368         LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1369                 wl_resource_destroy(vo->resource);
1370         }
1371
1372         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1373                 wl_resource_destroy(c->resource);
1374         }
1375
1376         free(private_server);
1377         private_loop->private_server = NULL;
1378         keep_private_server = NULL;
1379 }
1380
1381 /* LCOV_EXCL_START */
1382 INTERN const char*
1383 tdm_server_get_client_name(pid_t pid)
1384 {
1385         tdm_server_client_info *c = NULL;
1386
1387         LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1388                 if (c->pid == pid)
1389                         return (const char*)c->name;
1390         }
1391
1392         return NULL;
1393 }
1394 /* LCOV_EXCL_STOP */
1395
1396 /* LCOV_EXCL_START */
1397 INTERN tdm_error
1398 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1399 {
1400         tdm_private_server *private_server = keep_private_server;
1401         tdm_server_output_info *output_info = NULL;
1402
1403         if (!keep_private_server)
1404                 return TDM_ERROR_NONE;
1405
1406         LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1407                 tdm_server_vblank_info *vblank_info = NULL;
1408
1409                 if (output && output_info->output != output)
1410                         continue;
1411
1412                 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1413                         wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1414                 }
1415         }
1416
1417         return TDM_ERROR_NONE;
1418 }
1419 /* LCOV_EXCL_STOP */