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