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