virtual:add virtual output create/destroy
[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 #include <tdm-server-protocol.h>
41
42 #include "tdm_private.h"
43
44 /* CAUTION:
45  * - tdm server doesn't care about thread things.
46  * - DO NOT use the TDM internal functions here.
47  *     However, the internal function which does lock/unlock the mutex of
48  *     private_display in itself can be called.
49  * - DO NOT use the tdm_private_display structure here.
50  * - The callback function things can be called in main thread.
51  */
52
53 struct _tdm_private_server {
54         tdm_private_loop *private_loop;
55         struct list_head output_list;
56         struct list_head voutput_list;
57         struct list_head wait_list;
58 };
59
60 typedef struct _tdm_server_output_info {
61         struct list_head link;
62         tdm_private_server *private_server;
63         struct wl_resource *resource;
64         tdm_output *output;
65         struct list_head vblank_list;
66         unsigned int watch_output_changes;
67 } tdm_server_output_info;
68
69 typedef struct _tdm_server_voutput_info {
70         struct list_head link;
71         tdm_private_server *private_server;
72         struct wl_resource *resource;
73         tdm_output *output;
74         struct list_head output_list;
75 } tdm_server_voutput_info;
76
77 typedef struct _tdm_server_vblank_info {
78         struct list_head link;
79         tdm_server_output_info *output_info;
80         struct wl_resource *resource;
81
82         tdm_vblank *vblank;
83         unsigned int stamp;
84 } tdm_server_vblank_info;
85
86 typedef struct _tdm_server_wait_info {
87         struct list_head link;
88         tdm_server_vblank_info *vblank_info;
89
90         unsigned int req_id;
91         double req_time;
92 } tdm_server_wait_info;
93
94 typedef struct _tdm_server_client_info {
95         struct list_head link;
96         pid_t pid;
97         char name[TDM_NAME_LEN];
98         struct wl_resource *resource;
99 } tdm_server_client_info;
100
101 static tdm_private_server *keep_private_server;
102 static struct list_head client_list;
103
104 static void destroy_wait(tdm_server_wait_info *wait_info);
105
106 static void
107 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
108 {
109         char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
110         FILE *h;
111         size_t len;
112
113         snprintf(name, size, "Unknown");
114
115         snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
116         h = fopen(proc, "r");
117         if (!h)
118                 return;
119
120         len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
121         if (len == 0) {
122                 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
123                 len = p - pname;
124         }
125         pname[len - 1] = '\0';
126
127         strncpy(name, pname, size - 1);
128         name[size - 1] = '\0';
129
130         fclose(h);
131 }
132
133 /* LCOV_EXCL_START */
134 static void
135 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
136                                           unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
137 {
138         tdm_server_wait_info *found;
139         tdm_server_vblank_info *vblank_info;
140
141         TDM_RETURN_IF_FAIL(keep_private_server != NULL);
142
143         LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
144                                    tdm_server_wait_info, link, found);
145         if (!found) {
146                 TDM_ERR("wait_info(%p) is destroyed", wait_info);
147                 return;
148         }
149
150         if (tdm_debug_module & TDM_DEBUG_VBLANK)
151                 TDM_DBG("req_id(%d) done", wait_info->req_id);
152
153         vblank_info = wait_info->vblank_info;
154
155         if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
156                 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
157
158         wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
159                                                         sequence, tv_sec, tv_usec, error);
160
161         destroy_wait(wait_info);
162 }
163 /* LCOV_EXCL_STOP */
164
165 /* LCOV_EXCL_START */
166 static void
167 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
168                                           unsigned int tv_sec, unsigned int tv_usec, void *user_data)
169 {
170         _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
171 }
172 /* LCOV_EXCL_STOP */
173
174 /* LCOV_EXCL_START */
175 static void
176 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
177                                                          tdm_value value, void *user_data)
178 {
179         tdm_server_output_info *output_info = user_data;
180         struct wl_client *client;
181         pid_t pid = 0;
182
183         TDM_RETURN_IF_FAIL(output_info != NULL);
184
185         client = wl_resource_get_client(output_info->resource);
186         TDM_RETURN_IF_FAIL(client != NULL);
187
188         wl_client_get_credentials(client, &pid, NULL, NULL);
189
190         if (!output_info->watch_output_changes) {
191                 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
192                 return;
193         }
194
195         TDM_DBG("send the output changes: %d", (unsigned int)pid);
196
197         switch (type) {
198         case TDM_OUTPUT_CHANGE_DPMS:
199                 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
200                 break;
201         case TDM_OUTPUT_CHANGE_CONNECTION:
202                 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
203                 break;
204         default:
205                 break;
206         }
207 }
208 /* LCOV_EXCL_STOP */
209
210 static void
211 destroy_wait(tdm_server_wait_info *wait_info)
212 {
213         LIST_DEL(&wait_info->link);
214         free(wait_info);
215 }
216
217 static void
218 destroy_vblank_callback(struct wl_resource *resource)
219 {
220         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
221         tdm_server_wait_info *w = NULL, *ww = NULL;
222
223         TDM_RETURN_IF_FAIL(vblank_info != NULL);
224
225         LIST_DEL(&vblank_info->link);
226
227         if (vblank_info->vblank)
228                 tdm_vblank_destroy(vblank_info->vblank);
229
230         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
231                 if (w->vblank_info == vblank_info)
232                         destroy_wait(w);
233         }
234
235         free(vblank_info);
236 }
237
238 /* LCOV_EXCL_START */
239 static void
240 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
241 {
242         wl_resource_destroy(resource);
243 }
244 /* LCOV_EXCL_STOP */
245
246 /* LCOV_EXCL_START */
247 static void
248 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
249 {
250         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
251         tdm_error ret;
252
253         ret = tdm_vblank_set_name(vblank_info->vblank, name);
254         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
255 }
256 /* LCOV_EXCL_STOP */
257
258 /* LCOV_EXCL_START */
259 static void
260 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
261 {
262         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
263         tdm_error ret;
264
265         ret = tdm_vblank_set_fps(vblank_info->vblank, fps);
266         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
267 }
268 /* LCOV_EXCL_STOP */
269
270 /* LCOV_EXCL_START */
271 static void
272 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
273 {
274         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
275         tdm_error ret;
276
277         ret = tdm_vblank_set_offset(vblank_info->vblank, offset);
278         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
279 }
280 /* LCOV_EXCL_STOP */
281
282 static void
283 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
284 {
285         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
286         tdm_error ret;
287
288         ret = tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
289         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
290 }
291
292 static void
293 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
294                                                                   uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
295 {
296         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
297         tdm_server_output_info *output_info = vblank_info->output_info;
298         tdm_private_server *private_server = output_info->private_server;
299         tdm_server_wait_info *wait_info;
300         unsigned int enable_fake = 0;
301         tdm_error ret;
302
303         wait_info = calloc(1, sizeof * wait_info);
304         if (!wait_info) {
305                 /* LCOV_EXCL_START */
306
307                 TDM_ERR("alloc failed");
308                 ret = TDM_ERROR_OUT_OF_MEMORY;
309                 goto wait_failed;
310
311                 /* LCOV_EXCL_STOP */
312         }
313
314         LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
315         wait_info->vblank_info = vblank_info;
316         wait_info->req_id = req_id;
317         wait_info->req_time = TDM_TIME(req_sec, req_usec);
318
319         if (tdm_debug_module & TDM_DEBUG_VBLANK)
320                 TDM_DBG("req_id(%d) wait", req_id);
321
322         if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
323                 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
324
325         ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
326
327         tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
328         if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
329                 goto wait_failed;
330
331         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
332
333         return;
334 wait_failed:
335         /* LCOV_EXCL_START */
336
337         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
338         if (wait_info)
339                 destroy_wait(wait_info);
340
341         /* LCOV_EXCL_STOP */
342 }
343
344 static void
345 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
346                                                                           uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
347 {
348         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
349         tdm_server_output_info *output_info = vblank_info->output_info;
350         tdm_private_server *private_server = output_info->private_server;
351         tdm_server_wait_info *wait_info;
352         unsigned int enable_fake = 0;
353         tdm_error ret;
354
355         wait_info = calloc(1, sizeof * wait_info);
356         if (!wait_info) {
357                 /* LCOV_EXCL_START */
358
359                 TDM_ERR("alloc failed");
360                 ret = TDM_ERROR_OUT_OF_MEMORY;
361                 goto wait_failed;
362
363                 /* LCOV_EXCL_STOP */
364         }
365
366         LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
367         wait_info->vblank_info = vblank_info;
368         wait_info->req_id = req_id;
369         wait_info->req_time = TDM_TIME(req_sec, req_usec);
370
371         if (tdm_debug_module & TDM_DEBUG_VBLANK)
372                 TDM_DBG("req_id(%d) wait", req_id);
373
374         if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
375                 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
376
377         ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
378
379         tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
380         if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
381                 goto wait_failed;
382
383         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
384
385         return;
386 wait_failed:
387         /* LCOV_EXCL_START */
388
389         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
390         if (wait_info)
391                 destroy_wait(wait_info);
392
393         /* LCOV_EXCL_STOP */
394 }
395
396 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
397         _tdm_server_vblank_cb_destroy,
398         _tdm_server_vblank_cb_set_name,
399         _tdm_server_vblank_cb_set_fps,
400         _tdm_server_vblank_cb_set_offset,
401         _tdm_server_vblank_cb_set_enable_fake,
402         _tdm_server_vblank_cb_wait_vblank,
403         _tdm_server_vblank_cb_wait_vblank_seq,
404 };
405
406 /* LCOV_EXCL_START */
407 static void
408 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
409 {
410         wl_resource_destroy(resource);
411 }
412 /* LCOV_EXCL_STOP */
413
414 static void
415 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
416 {
417         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
418         tdm_private_server *private_server = output_info->private_server;
419         tdm_private_loop *private_loop = private_server->private_loop;
420         struct wl_resource *vblank_resource;
421         tdm_vblank *vblank;
422         tdm_server_vblank_info *vblank_info;
423
424         vblank_resource =
425                 wl_resource_create(client, &wl_tdm_vblank_interface,
426                                                    wl_resource_get_version(resource), id);
427         if (!vblank_resource) {
428                 /* LCOV_EXCL_START */
429
430                 wl_resource_post_no_memory(resource);
431                 TDM_ERR("wl_resource_create failed");
432                 return;
433
434                 /* LCOV_EXCL_STOP */
435         }
436
437         vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
438         if (!vblank) {
439                 /* LCOV_EXCL_START */
440
441                 wl_resource_post_no_memory(resource);
442                 wl_resource_destroy(vblank_resource);
443                 TDM_ERR("tdm_vblank_create failed");
444                 return;
445
446                 /* LCOV_EXCL_STOP */
447         }
448
449         vblank_info = calloc(1, sizeof * vblank_info);
450         if (!vblank_info) {
451                 /* LCOV_EXCL_START */
452
453                 wl_resource_post_no_memory(resource);
454                 wl_resource_destroy(vblank_resource);
455                 tdm_vblank_destroy(vblank);
456                 TDM_ERR("alloc failed");
457                 return;
458
459                 /* LCOV_EXCL_STOP */
460         }
461
462         LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
463         vblank_info->output_info = output_info;
464         vblank_info->resource = vblank_resource;
465         vblank_info->vblank = vblank;
466         vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
467
468         tdm_vblank_set_resource(vblank, vblank_resource);
469
470         wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
471                                                                    vblank_info, destroy_vblank_callback);
472
473         wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
474
475         if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) {
476                 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
477                 if (output == output_info->output)
478                         wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
479         }
480
481         return;
482 }
483
484 /* LCOV_EXCL_START */
485 static void
486 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
487 {
488         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
489
490         TDM_RETURN_IF_FAIL(output_info != NULL);
491
492         output_info->watch_output_changes = enable;
493 }
494 /* LCOV_EXCL_STOP */
495
496 static void
497 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
498 {
499         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
500         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
501         tdm_error ret;
502
503         TDM_RETURN_IF_FAIL(output_info != NULL);
504
505         ret = tdm_output_get_conn_status(output_info->output, &status);
506         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
507
508         wl_tdm_output_send_connection(output_info->resource, status, ret);
509
510         return;
511
512 failed:
513         wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
514 }
515
516 static void
517 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
518 {
519         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
520         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
521         tdm_error ret;
522
523         TDM_RETURN_IF_FAIL(output_info != NULL);
524
525         ret = tdm_output_get_conn_status(output_info->output, &status);
526         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
527
528         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
529                 const tdm_output_mode *mode = NULL;
530                 unsigned int hdisplay, vdisplay, vrefresh;
531
532                 ret = tdm_output_get_mode(output_info->output, &mode);
533                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
534
535                 hdisplay = (mode) ? mode->hdisplay : 0;
536                 vdisplay = (mode) ? mode->vdisplay : 0;
537                 vrefresh = (mode) ? mode->vrefresh : 0;
538
539                 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
540         } else {
541                 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
542         }
543
544         return;
545 failed:
546         wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
547 }
548
549 static void
550 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
551 {
552         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
553         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
554         tdm_error ret;
555
556         TDM_RETURN_IF_FAIL(output_info != NULL);
557
558         ret = tdm_output_get_conn_status(output_info->output, &status);
559         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
560
561         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
562                 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
563
564                 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
565                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
566
567                 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
568         } else {
569                 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
570         }
571
572         return;
573 failed:
574         wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
575 }
576
577 static const struct wl_tdm_output_interface tdm_output_implementation = {
578         _tdm_server_output_cb_destroy,
579         _tdm_server_output_cb_create_vblank,
580         _tdm_server_output_cb_watch_output_changes,
581         _tdm_server_output_cb_get_connection,
582         _tdm_server_output_cb_get_mode,
583         _tdm_server_output_cb_get_dpms,
584 };
585
586 static void
587 destroy_output_callback(struct wl_resource *resource)
588 {
589         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
590         tdm_server_vblank_info *v = NULL, *vv = NULL;
591
592         TDM_RETURN_IF_FAIL(output_info != NULL);
593
594         LIST_DEL(&output_info->link);
595
596         tdm_output_remove_change_handler(output_info->output,
597                                                                          _tdm_server_cb_output_change, output_info);
598
599         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
600                 wl_resource_destroy(v->resource);
601         }
602
603         free(output_info);
604 }
605 #if 0
606 static void
607 destroy_voutput_callback(struct wl_resource *resource)
608 {
609         tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
610
611         TDM_RETURN_IF_FAIL(voutput_info != NULL);
612
613         LIST_DEL(&voutput_info->link);
614
615         free(voutput_info);
616 }
617 #endif
618 static void
619 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
620                                                          const char *name, uint32_t id)
621 {
622         tdm_private_server *private_server = wl_resource_get_user_data(resource);
623         tdm_server_output_info *output_info;
624         struct wl_resource *output_resource = NULL;
625         tdm_output *output;
626         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
627         tdm_error ret;
628
629         output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
630         if (!output) {
631                 /* LCOV_EXCL_START */
632
633                 TDM_ERR("There is no '%s' output", name);
634                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
635                                                            "There is no '%s' output", name);
636                 return;
637
638                 /* LCOV_EXCL_STOP */
639         }
640
641         output_resource =
642                 wl_resource_create(client, &wl_tdm_output_interface,
643                                                    wl_resource_get_version(resource), id);
644         if (!output_resource) {
645                 /* LCOV_EXCL_START */
646
647                 wl_resource_post_no_memory(resource);
648                 TDM_ERR("wl_resource_create failed");
649                 return;
650
651                 /* LCOV_EXCL_STOP */
652         }
653
654         output_info = calloc(1, sizeof * output_info);
655         if (!output_info) {
656                 /* LCOV_EXCL_START */
657
658                 wl_resource_post_no_memory(resource);
659                 wl_resource_destroy(output_resource);
660                 TDM_ERR("alloc failed");
661                 return;
662
663                 /* LCOV_EXCL_STOP */
664         }
665
666         ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
667         if (ret != TDM_ERROR_NONE) {
668                 wl_resource_post_no_memory(resource);
669                 wl_resource_destroy(output_resource);
670                 free(output_info);
671                 TDM_ERR("tdm_output_add_change_handler failed");
672                 return;
673         }
674
675         LIST_ADDTAIL(&output_info->link, &private_server->output_list);
676         output_info->private_server = private_server;
677         output_info->resource = output_resource;
678         output_info->output = output;
679         LIST_INITHEAD(&output_info->vblank_list);
680
681         wl_resource_set_implementation(output_resource, &tdm_output_implementation,
682                                                                    output_info, destroy_output_callback);
683
684         ret = tdm_output_get_conn_status(output, &status);
685         wl_tdm_output_send_connection(output_resource, status, ret);
686
687         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
688                 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
689                 const tdm_output_mode *mode = NULL;
690                 unsigned int hdisplay, vdisplay, vrefresh;
691
692                 ret = tdm_output_get_mode(output, &mode);
693                 hdisplay = (mode) ? mode->hdisplay : 0;
694                 vdisplay = (mode) ? mode->vdisplay : 0;
695                 vrefresh = (mode) ? mode->vrefresh : 0;
696                 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
697
698                 ret = tdm_output_get_dpms(output, &dpms_value);
699                 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
700         } else {
701                 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
702                 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
703         }
704 }
705
706 static void
707 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
708 {
709         struct wl_resource *voutput_resource = NULL;
710         tdm_private_server *private_server = wl_resource_get_user_data(resource);
711         tdm_server_voutput_info *voutput_info;
712         tdm_output *output;
713         tdm_error ret;
714
715         output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
716         if (output) {
717                 TDM_ERR("There is '%s' output, cannot create.", name);
718                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
719                                                            "There is '%s' output", name);
720                 return;
721         }
722
723         output = tdm_display_create_output(private_server->private_loop->dpy, name, &ret);
724         if (!output) {
725                 TDM_ERR("output creation fail(%s)(%d).", name, ret);
726                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
727                                                            "%s output creation fail", name);
728                 return;
729         }
730
731         voutput_resource =
732                 wl_resource_create(client, &wl_tdm_voutput_interface,
733                                                    wl_resource_get_version(resource), id);
734         if (!voutput_resource) {
735                 /* LCOV_EXCL_START */
736
737                 wl_resource_post_no_memory(resource);
738                 TDM_ERR("wl_resource_create failed");
739                 return;
740
741                 /* LCOV_EXCL_STOP */
742         }
743
744         voutput_info = calloc(1, sizeof * voutput_info);
745         if (!voutput_info) {
746                 /* LCOV_EXCL_START */
747
748                 wl_resource_post_no_memory(resource);
749                 wl_resource_destroy(voutput_resource);
750                 TDM_ERR("alloc failed");
751                 return;
752
753                 /* LCOV_EXCL_STOP */
754         }
755
756         LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
757         voutput_info->private_server = private_server;
758         voutput_info->resource = voutput_resource;
759         voutput_info->output = output;
760         LIST_INITHEAD(&voutput_info->output_list);
761 #if 0
762         wl_resource_set_implementation(voutput_resource, &tdm_voutput_implementation,
763                                                                    voutput_info, destroy_voutput_callback);
764 #endif
765         wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
766 }
767
768 /* LCOV_EXCL_START */
769 static void
770 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
771 {
772         tdm_private_server *private_server = wl_resource_get_user_data(resource);
773         tdm_private_loop *private_loop = private_server->private_loop;
774         char message[TDM_SERVER_REPLY_MSG_LEN];
775         char *m;
776         int len = sizeof(message), size;
777         uid_t uid;
778
779         wl_client_get_credentials(client, NULL, &uid, NULL);
780
781         if (uid != 0) {
782                 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
783                 TDM_ERR("%s", message);
784         } else {
785                 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
786         }
787
788         size = sizeof(message) - len;
789         m = message;
790
791         wl_client_flush(client);
792
793         while (size > 0) {
794                 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
795                 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
796
797                 strncpy(buffer, m, copylen);
798                 m += copylen;
799                 size -= copylen;
800
801                 buffer[copylen] = '\0';
802
803                 wl_tdm_send_debug_message(resource, buffer);
804         }
805
806         wl_tdm_send_debug_done(resource);
807 }
808 /* LCOV_EXCL_STOP */
809
810 static const struct wl_tdm_interface tdm_implementation = {
811         _tdm_server_cb_debug,
812         _tdm_server_cb_create_output,
813         _tdm_server_cb_create_virtual_output
814 };
815
816 static void
817 destroy_client(struct wl_resource *resource)
818 {
819         tdm_server_client_info *c = NULL, *cc = NULL;
820         struct wl_client *client;
821
822         client = wl_resource_get_client(resource);
823         TDM_RETURN_IF_FAIL(client != NULL);
824
825         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
826                 if (c->resource == resource) {
827                         LIST_DEL(&c->link);
828                         free(c);
829                         return;
830                 }
831         }
832 }
833
834 static void
835 _tdm_server_bind(struct wl_client *client, void *data,
836                                  uint32_t version, uint32_t id)
837 {
838         struct wl_resource *resource;
839         tdm_server_client_info *cinfo;
840
841         resource = wl_resource_create(client, &wl_tdm_interface, version, id);
842         if (!resource) {
843                 /* LCOV_EXCL_START */
844
845                 wl_client_post_no_memory(client);
846                 return;
847
848                 /* LCOV_EXCL_STOP */
849         }
850
851         cinfo = calloc(1, sizeof(tdm_server_client_info));
852         if (!cinfo) {
853                 /* LCOV_EXCL_START */
854
855                 wl_client_post_no_memory(client);
856                 wl_resource_destroy(resource);
857                 return;
858
859                 /* LCOV_EXCL_STOP */
860         }
861
862         cinfo->resource = resource;
863
864         LIST_ADDTAIL(&cinfo->link, &client_list);
865         wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
866         _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
867
868         wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
869 }
870
871 INTERN tdm_error
872 tdm_server_init(tdm_private_loop *private_loop)
873 {
874         tdm_private_server *private_server;
875
876         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
877         TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
878
879         if (private_loop->private_server)
880                 return TDM_ERROR_NONE;
881
882         if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
883                 /* LCOV_EXCL_START */
884
885                 TDM_ERR("createing a tdm-socket failed");
886                 return TDM_ERROR_OPERATION_FAILED;
887
888                 /* LCOV_EXCL_STOP */
889         }
890
891         private_server = calloc(1, sizeof * private_server);
892         if (!private_server) {
893                 TDM_ERR("alloc failed");
894                 return TDM_ERROR_OUT_OF_MEMORY;
895         }
896
897         LIST_INITHEAD(&private_server->output_list);
898         LIST_INITHEAD(&private_server->voutput_list);
899         LIST_INITHEAD(&private_server->wait_list);
900
901         if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
902                                                   private_server, _tdm_server_bind)) {
903                 /* LCOV_EXCL_START */
904
905                 TDM_ERR("creating a global resource failed");
906                 free(private_server);
907                 return TDM_ERROR_OUT_OF_MEMORY;
908
909                 /* LCOV_EXCL_STOP */
910         }
911
912         private_server->private_loop = private_loop;
913         private_loop->private_server = private_server;
914         keep_private_server = private_server;
915
916         LIST_INITHEAD(&client_list);
917
918         return TDM_ERROR_NONE;
919 }
920
921 INTERN void
922 tdm_server_deinit(tdm_private_loop *private_loop)
923 {
924         tdm_server_output_info *o = NULL, *oo = NULL;
925 //      tdm_server_voutput_info *vo = NULL, *voo = NULL;
926         tdm_server_wait_info *w = NULL, *ww = NULL;
927         tdm_server_client_info *c = NULL, *cc = NULL;
928         tdm_private_server *private_server;
929
930         if (!private_loop->private_server)
931                 return;
932
933         private_server = private_loop->private_server;
934
935         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
936                 destroy_wait(w);
937         }
938
939         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
940                 wl_resource_destroy(o->resource);
941         }
942 #if 0
943         LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
944                 wl_resource_destroy(vo->resource);
945         }
946 #endif
947         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
948                 wl_resource_destroy(c->resource);
949         }
950
951         free(private_server);
952         private_loop->private_server = NULL;
953         keep_private_server = NULL;
954 }
955
956 /* LCOV_EXCL_START */
957 INTERN const char*
958 tdm_server_get_client_name(pid_t pid)
959 {
960         tdm_server_client_info *c = NULL;
961
962         LIST_FOR_EACH_ENTRY(c, &client_list, link) {
963                 if (c->pid == pid)
964                         return (const char*)c->name;
965         }
966
967         return NULL;
968 }
969 /* LCOV_EXCL_STOP */
970
971 /* LCOV_EXCL_START */
972 INTERN tdm_error
973 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
974 {
975         tdm_private_server *private_server = keep_private_server;
976         tdm_server_output_info *output_info = NULL;
977
978         if (!keep_private_server)
979                 return TDM_ERROR_NONE;
980
981         LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
982                 tdm_server_vblank_info *vblank_info = NULL;
983
984                 if (output && output_info->output != output)
985                         continue;
986
987                 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
988                         wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
989                 }
990         }
991
992         return TDM_ERROR_NONE;
993 }
994 /* LCOV_EXCL_STOP */