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