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