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