correct email address
[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 /* LCOV_EXCL_START */
686 static void
687 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
688 {
689         tdm_private_server *private_server = wl_resource_get_user_data(resource);
690         tdm_private_loop *private_loop = private_server->private_loop;
691         char message[TDM_SERVER_REPLY_MSG_LEN];
692         char *m;
693         int len = sizeof(message), size;
694         uid_t uid;
695
696         wl_client_get_credentials(client, NULL, &uid, NULL);
697
698         if (uid != 0) {
699                 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
700                 TDM_ERR("%s", message);
701         } else {
702                 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
703         }
704
705         size = sizeof(message) - len;
706         m = message;
707
708         wl_client_flush(client);
709
710         while (size > 0) {
711                 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
712                 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
713
714                 strncpy(buffer, m, copylen);
715                 m += copylen;
716                 size -= copylen;
717
718                 buffer[copylen] = '\0';
719
720                 wl_tdm_send_debug_message(resource, buffer);
721         }
722
723         wl_tdm_send_debug_done(resource);
724 }
725 /* LCOV_EXCL_STOP */
726
727 static const struct wl_tdm_interface tdm_implementation = {
728         _tdm_server_cb_debug,
729         _tdm_server_cb_create_output,
730 };
731
732 static void
733 destroy_client(struct wl_resource *resource)
734 {
735         tdm_server_client_info *c = NULL, *cc = NULL;
736         struct wl_client *client;
737
738         client = wl_resource_get_client(resource);
739         TDM_RETURN_IF_FAIL(client != NULL);
740
741         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
742                 if (c->resource == resource) {
743                         LIST_DEL(&c->link);
744                         free(c);
745                         return;
746                 }
747         }
748 }
749
750 static void
751 _tdm_server_bind(struct wl_client *client, void *data,
752                                  uint32_t version, uint32_t id)
753 {
754         struct wl_resource *resource;
755         tdm_server_client_info *cinfo;
756
757         resource = wl_resource_create(client, &wl_tdm_interface, version, id);
758         if (!resource) {
759                 /* LCOV_EXCL_START */
760
761                 wl_client_post_no_memory(client);
762                 return;
763
764                 /* LCOV_EXCL_STOP */
765         }
766
767         cinfo = calloc(1, sizeof(tdm_server_client_info));
768         if (!cinfo) {
769                 /* LCOV_EXCL_START */
770
771                 wl_client_post_no_memory(client);
772                 wl_resource_destroy(resource);
773                 return;
774
775                 /* LCOV_EXCL_STOP */
776         }
777
778         cinfo->resource = resource;
779
780         LIST_ADDTAIL(&cinfo->link, &client_list);
781         wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
782         _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
783
784         wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
785 }
786
787 INTERN tdm_error
788 tdm_server_init(tdm_private_loop *private_loop)
789 {
790         tdm_private_server *private_server;
791
792         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
793         TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
794
795         if (private_loop->private_server)
796                 return TDM_ERROR_NONE;
797
798         if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
799                 /* LCOV_EXCL_START */
800
801                 TDM_ERR("createing a tdm-socket failed");
802                 return TDM_ERROR_OPERATION_FAILED;
803
804                 /* LCOV_EXCL_STOP */
805         }
806
807         private_server = calloc(1, sizeof * private_server);
808         if (!private_server) {
809                 TDM_ERR("alloc failed");
810                 return TDM_ERROR_OUT_OF_MEMORY;
811         }
812
813         LIST_INITHEAD(&private_server->output_list);
814         LIST_INITHEAD(&private_server->wait_list);
815
816         if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
817                                                   private_server, _tdm_server_bind)) {
818                 /* LCOV_EXCL_START */
819
820                 TDM_ERR("creating a global resource failed");
821                 free(private_server);
822                 return TDM_ERROR_OUT_OF_MEMORY;
823
824                 /* LCOV_EXCL_STOP */
825         }
826
827         private_server->private_loop = private_loop;
828         private_loop->private_server = private_server;
829         keep_private_server = private_server;
830
831         LIST_INITHEAD(&client_list);
832
833         return TDM_ERROR_NONE;
834 }
835
836 INTERN void
837 tdm_server_deinit(tdm_private_loop *private_loop)
838 {
839         tdm_server_output_info *o = NULL, *oo = NULL;
840         tdm_server_wait_info *w = NULL, *ww = NULL;
841         tdm_server_client_info *c = NULL, *cc = NULL;
842         tdm_private_server *private_server;
843
844         if (!private_loop->private_server)
845                 return;
846
847         private_server = private_loop->private_server;
848
849         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
850                 destroy_wait(w);
851         }
852
853         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
854                 wl_resource_destroy(o->resource);
855         }
856
857         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
858                 wl_resource_destroy(c->resource);
859         }
860
861         free(private_server);
862         private_loop->private_server = NULL;
863         keep_private_server = NULL;
864 }
865
866 /* LCOV_EXCL_START */
867 INTERN const char*
868 tdm_server_get_client_name(pid_t pid)
869 {
870         tdm_server_client_info *c = NULL;
871
872         LIST_FOR_EACH_ENTRY(c, &client_list, link) {
873                 if (c->pid == pid)
874                         return (const char*)c->name;
875         }
876
877         return NULL;
878 }
879 /* LCOV_EXCL_STOP */
880
881 /* LCOV_EXCL_START */
882 INTERN tdm_error
883 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
884 {
885         tdm_private_server *private_server = keep_private_server;
886         tdm_server_output_info *output_info = NULL;
887
888         if (!keep_private_server)
889                 return TDM_ERROR_NONE;
890
891         LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
892                 tdm_server_vblank_info *vblank_info = NULL;
893
894                 if (output && output_info->output != output)
895                         continue;
896
897                 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
898                         wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
899                 }
900         }
901
902         return TDM_ERROR_NONE;
903 }
904 /* LCOV_EXCL_STOP */