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