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