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