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