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