3ce52cb91ab0f125b345a4e1585866a31c1d6497
[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 <boram1288.park@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 voutput_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_voutput_info {
70         struct list_head link;
71         tdm_private_server *private_server;
72         struct wl_resource *resource;
73         tdm_output *output;
74         struct list_head output_list;
75
76         tdm_output_conn_status status;
77         struct
78         {
79                 int count;
80                 tdm_output_mode *modes;
81         } available_modes;
82 } tdm_server_voutput_info;
83
84 typedef struct _tdm_server_vblank_info {
85         struct list_head link;
86         tdm_server_output_info *output_info;
87         struct wl_resource *resource;
88
89         tdm_vblank *vblank;
90         unsigned int stamp;
91 } tdm_server_vblank_info;
92
93 typedef struct _tdm_server_wait_info {
94         struct list_head link;
95         tdm_server_vblank_info *vblank_info;
96
97         unsigned int req_id;
98         double req_time;
99 } tdm_server_wait_info;
100
101 typedef struct _tdm_server_client_info {
102         struct list_head link;
103         pid_t pid;
104         char name[TDM_NAME_LEN];
105         struct wl_resource *resource;
106 } tdm_server_client_info;
107
108 static tdm_private_server *keep_private_server;
109 static struct list_head client_list;
110
111 static void destroy_wait(tdm_server_wait_info *wait_info);
112
113 static void
114 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
115 {
116         char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
117         FILE *h;
118         size_t len;
119
120         snprintf(name, size, "Unknown");
121
122         snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
123         h = fopen(proc, "r");
124         if (!h)
125                 return;
126
127         len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
128         if (len == 0) {
129                 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
130                 len = p - pname;
131         }
132         pname[len - 1] = '\0';
133
134         strncpy(name, pname, size - 1);
135         name[size - 1] = '\0';
136
137         fclose(h);
138 }
139
140 /* LCOV_EXCL_START */
141 static void
142 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
143                                           unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
144 {
145         tdm_server_wait_info *found;
146         tdm_server_vblank_info *vblank_info;
147
148         TDM_RETURN_IF_FAIL(keep_private_server != NULL);
149
150         LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
151                                    tdm_server_wait_info, link, found);
152         if (!found) {
153                 TDM_ERR("wait_info(%p) is destroyed", wait_info);
154                 return;
155         }
156
157         if (tdm_debug_module & TDM_DEBUG_VBLANK)
158                 TDM_DBG("req_id(%d) done", wait_info->req_id);
159
160         vblank_info = wait_info->vblank_info;
161
162         if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
163                 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
164
165         wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
166                                                         sequence, tv_sec, tv_usec, error);
167
168         destroy_wait(wait_info);
169 }
170 /* LCOV_EXCL_STOP */
171
172 /* LCOV_EXCL_START */
173 static void
174 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
175                                           unsigned int tv_sec, unsigned int tv_usec, void *user_data)
176 {
177         _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
178 }
179 /* LCOV_EXCL_STOP */
180
181 /* LCOV_EXCL_START */
182 static void
183 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
184                                                          tdm_value value, void *user_data)
185 {
186         tdm_server_output_info *output_info = user_data;
187         struct wl_client *client;
188         pid_t pid = 0;
189
190         TDM_RETURN_IF_FAIL(output_info != NULL);
191
192         client = wl_resource_get_client(output_info->resource);
193         TDM_RETURN_IF_FAIL(client != NULL);
194
195         wl_client_get_credentials(client, &pid, NULL, NULL);
196
197         if (!output_info->watch_output_changes) {
198                 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
199                 return;
200         }
201
202         TDM_DBG("send the output changes: %d", (unsigned int)pid);
203
204         switch (type) {
205         case TDM_OUTPUT_CHANGE_DPMS:
206                 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
207                 break;
208         case TDM_OUTPUT_CHANGE_CONNECTION:
209                 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
210                 break;
211         default:
212                 break;
213         }
214 }
215 /* LCOV_EXCL_STOP */
216
217 static void
218 destroy_wait(tdm_server_wait_info *wait_info)
219 {
220         LIST_DEL(&wait_info->link);
221         free(wait_info);
222 }
223
224 static void
225 destroy_vblank_callback(struct wl_resource *resource)
226 {
227         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
228         tdm_server_wait_info *w = NULL, *ww = NULL;
229
230         TDM_RETURN_IF_FAIL(vblank_info != NULL);
231
232         LIST_DEL(&vblank_info->link);
233
234         if (vblank_info->vblank)
235                 tdm_vblank_destroy(vblank_info->vblank);
236
237         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
238                 if (w->vblank_info == vblank_info)
239                         destroy_wait(w);
240         }
241
242         free(vblank_info);
243 }
244
245 /* LCOV_EXCL_START */
246 static void
247 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
248 {
249         wl_resource_destroy(resource);
250 }
251 /* LCOV_EXCL_STOP */
252
253 /* LCOV_EXCL_START */
254 static void
255 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
256 {
257         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
258         tdm_error ret;
259
260         ret = tdm_vblank_set_name(vblank_info->vblank, name);
261         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
262 }
263 /* LCOV_EXCL_STOP */
264
265 /* LCOV_EXCL_START */
266 static void
267 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
268 {
269         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
270         tdm_error ret;
271
272         ret = tdm_vblank_set_fps(vblank_info->vblank, fps);
273         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
274 }
275 /* LCOV_EXCL_STOP */
276
277 /* LCOV_EXCL_START */
278 static void
279 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
280 {
281         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
282         tdm_error ret;
283
284         ret = tdm_vblank_set_offset(vblank_info->vblank, offset);
285         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
286 }
287 /* LCOV_EXCL_STOP */
288
289 static void
290 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
291 {
292         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
293         tdm_error ret;
294
295         ret = tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
296         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
297 }
298
299 static void
300 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
301                                                                   uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
302 {
303         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
304         tdm_server_output_info *output_info = vblank_info->output_info;
305         tdm_private_server *private_server = output_info->private_server;
306         tdm_server_wait_info *wait_info;
307         unsigned int enable_fake = 0;
308         tdm_error ret;
309
310         wait_info = calloc(1, sizeof * wait_info);
311         if (!wait_info) {
312                 /* LCOV_EXCL_START */
313
314                 TDM_ERR("alloc failed");
315                 ret = TDM_ERROR_OUT_OF_MEMORY;
316                 goto wait_failed;
317
318                 /* LCOV_EXCL_STOP */
319         }
320
321         LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
322         wait_info->vblank_info = vblank_info;
323         wait_info->req_id = req_id;
324         wait_info->req_time = TDM_TIME(req_sec, req_usec);
325
326         if (tdm_debug_module & TDM_DEBUG_VBLANK)
327                 TDM_DBG("req_id(%d) wait", req_id);
328
329         if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
330                 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
331
332         ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
333
334         tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
335         if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
336                 goto wait_failed;
337
338         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
339
340         return;
341 wait_failed:
342         /* LCOV_EXCL_START */
343
344         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
345         if (wait_info)
346                 destroy_wait(wait_info);
347
348         /* LCOV_EXCL_STOP */
349 }
350
351 static void
352 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
353                                                                           uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
354 {
355         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
356         tdm_server_output_info *output_info = vblank_info->output_info;
357         tdm_private_server *private_server = output_info->private_server;
358         tdm_server_wait_info *wait_info;
359         unsigned int enable_fake = 0;
360         tdm_error ret;
361
362         wait_info = calloc(1, sizeof * wait_info);
363         if (!wait_info) {
364                 /* LCOV_EXCL_START */
365
366                 TDM_ERR("alloc failed");
367                 ret = TDM_ERROR_OUT_OF_MEMORY;
368                 goto wait_failed;
369
370                 /* LCOV_EXCL_STOP */
371         }
372
373         LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
374         wait_info->vblank_info = vblank_info;
375         wait_info->req_id = req_id;
376         wait_info->req_time = TDM_TIME(req_sec, req_usec);
377
378         if (tdm_debug_module & TDM_DEBUG_VBLANK)
379                 TDM_DBG("req_id(%d) wait", req_id);
380
381         if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
382                 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
383
384         ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
385
386         tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
387         if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
388                 goto wait_failed;
389
390         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
391
392         return;
393 wait_failed:
394         /* LCOV_EXCL_START */
395
396         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
397         if (wait_info)
398                 destroy_wait(wait_info);
399
400         /* LCOV_EXCL_STOP */
401 }
402
403 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
404         _tdm_server_vblank_cb_destroy,
405         _tdm_server_vblank_cb_set_name,
406         _tdm_server_vblank_cb_set_fps,
407         _tdm_server_vblank_cb_set_offset,
408         _tdm_server_vblank_cb_set_enable_fake,
409         _tdm_server_vblank_cb_wait_vblank,
410         _tdm_server_vblank_cb_wait_vblank_seq,
411 };
412
413 /* LCOV_EXCL_START */
414 static void
415 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
416 {
417         wl_resource_destroy(resource);
418 }
419 /* LCOV_EXCL_STOP */
420
421 static void
422 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
423 {
424         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
425         tdm_private_server *private_server = output_info->private_server;
426         tdm_private_loop *private_loop = private_server->private_loop;
427         struct wl_resource *vblank_resource;
428         tdm_vblank *vblank;
429         tdm_server_vblank_info *vblank_info;
430
431         vblank_resource =
432                 wl_resource_create(client, &wl_tdm_vblank_interface,
433                                                    wl_resource_get_version(resource), id);
434         if (!vblank_resource) {
435                 /* LCOV_EXCL_START */
436
437                 wl_resource_post_no_memory(resource);
438                 TDM_ERR("wl_resource_create failed");
439                 return;
440
441                 /* LCOV_EXCL_STOP */
442         }
443
444         vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
445         if (!vblank) {
446                 /* LCOV_EXCL_START */
447
448                 wl_resource_post_no_memory(resource);
449                 wl_resource_destroy(vblank_resource);
450                 TDM_ERR("tdm_vblank_create failed");
451                 return;
452
453                 /* LCOV_EXCL_STOP */
454         }
455
456         vblank_info = calloc(1, sizeof * vblank_info);
457         if (!vblank_info) {
458                 /* LCOV_EXCL_START */
459
460                 wl_resource_post_no_memory(resource);
461                 wl_resource_destroy(vblank_resource);
462                 tdm_vblank_destroy(vblank);
463                 TDM_ERR("alloc failed");
464                 return;
465
466                 /* LCOV_EXCL_STOP */
467         }
468
469         LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
470         vblank_info->output_info = output_info;
471         vblank_info->resource = vblank_resource;
472         vblank_info->vblank = vblank;
473         vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
474
475         tdm_vblank_set_resource(vblank, vblank_resource);
476
477         wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
478                                                                    vblank_info, destroy_vblank_callback);
479
480         wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
481
482         if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) {
483                 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
484                 if (output == output_info->output)
485                         wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
486         }
487
488         return;
489 }
490
491 /* LCOV_EXCL_START */
492 static void
493 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
494 {
495         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
496
497         TDM_RETURN_IF_FAIL(output_info != NULL);
498
499         output_info->watch_output_changes = enable;
500 }
501 /* LCOV_EXCL_STOP */
502
503 static void
504 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
505 {
506         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
507         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
508         tdm_error ret;
509
510         TDM_RETURN_IF_FAIL(output_info != NULL);
511
512         ret = tdm_output_get_conn_status(output_info->output, &status);
513         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
514
515         wl_tdm_output_send_connection(output_info->resource, status, ret);
516
517         return;
518
519 failed:
520         wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
521 }
522
523 static void
524 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
525 {
526         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
527         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
528         tdm_error ret;
529
530         TDM_RETURN_IF_FAIL(output_info != NULL);
531
532         ret = tdm_output_get_conn_status(output_info->output, &status);
533         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
534
535         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
536                 const tdm_output_mode *mode = NULL;
537                 unsigned int hdisplay, vdisplay, vrefresh;
538
539                 ret = tdm_output_get_mode(output_info->output, &mode);
540                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
541
542                 hdisplay = (mode) ? mode->hdisplay : 0;
543                 vdisplay = (mode) ? mode->vdisplay : 0;
544                 vrefresh = (mode) ? mode->vrefresh : 0;
545
546                 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
547         } else {
548                 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
549         }
550
551         return;
552 failed:
553         wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
554 }
555
556 static void
557 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
558 {
559         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
560         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
561         tdm_error ret;
562
563         TDM_RETURN_IF_FAIL(output_info != NULL);
564
565         ret = tdm_output_get_conn_status(output_info->output, &status);
566         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
567
568         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
569                 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
570
571                 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
572                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
573
574                 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
575         } else {
576                 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
577         }
578
579         return;
580 failed:
581         wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
582 }
583
584 static const struct wl_tdm_output_interface tdm_output_implementation = {
585         _tdm_server_output_cb_destroy,
586         _tdm_server_output_cb_create_vblank,
587         _tdm_server_output_cb_watch_output_changes,
588         _tdm_server_output_cb_get_connection,
589         _tdm_server_output_cb_get_mode,
590         _tdm_server_output_cb_get_dpms,
591 };
592
593 static void
594 destroy_output_callback(struct wl_resource *resource)
595 {
596         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
597         tdm_server_vblank_info *v = NULL, *vv = NULL;
598
599         TDM_RETURN_IF_FAIL(output_info != NULL);
600
601         LIST_DEL(&output_info->link);
602
603         tdm_output_remove_change_handler(output_info->output,
604                                                                          _tdm_server_cb_output_change, output_info);
605
606         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
607                 wl_resource_destroy(v->resource);
608         }
609
610         free(output_info);
611 }
612 #if 0
613 static void
614 destroy_voutput_callback(struct wl_resource *resource)
615 {
616         tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
617
618         TDM_RETURN_IF_FAIL(voutput_info != NULL);
619
620         LIST_DEL(&voutput_info->link);
621
622         free(voutput_info);
623 }
624 #endif
625 static void
626 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
627                                                          const char *name, uint32_t id)
628 {
629         tdm_private_server *private_server = wl_resource_get_user_data(resource);
630         tdm_server_output_info *output_info;
631         struct wl_resource *output_resource = NULL;
632         tdm_output *output;
633         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
634         tdm_error ret;
635
636         output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
637         if (!output) {
638                 /* LCOV_EXCL_START */
639
640                 TDM_ERR("There is no '%s' output", name);
641                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
642                                                            "There is no '%s' output", name);
643                 return;
644
645                 /* LCOV_EXCL_STOP */
646         }
647
648         output_resource =
649                 wl_resource_create(client, &wl_tdm_output_interface,
650                                                    wl_resource_get_version(resource), id);
651         if (!output_resource) {
652                 /* LCOV_EXCL_START */
653
654                 wl_resource_post_no_memory(resource);
655                 TDM_ERR("wl_resource_create failed");
656                 return;
657
658                 /* LCOV_EXCL_STOP */
659         }
660
661         output_info = calloc(1, sizeof * output_info);
662         if (!output_info) {
663                 /* LCOV_EXCL_START */
664
665                 wl_resource_post_no_memory(resource);
666                 wl_resource_destroy(output_resource);
667                 TDM_ERR("alloc failed");
668                 return;
669
670                 /* LCOV_EXCL_STOP */
671         }
672
673         ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
674         if (ret != TDM_ERROR_NONE) {
675                 wl_resource_post_no_memory(resource);
676                 wl_resource_destroy(output_resource);
677                 free(output_info);
678                 TDM_ERR("tdm_output_add_change_handler failed");
679                 return;
680         }
681
682         LIST_ADDTAIL(&output_info->link, &private_server->output_list);
683         output_info->private_server = private_server;
684         output_info->resource = output_resource;
685         output_info->output = output;
686         LIST_INITHEAD(&output_info->vblank_list);
687
688         wl_resource_set_implementation(output_resource, &tdm_output_implementation,
689                                                                    output_info, destroy_output_callback);
690
691         ret = tdm_output_get_conn_status(output, &status);
692         wl_tdm_output_send_connection(output_resource, status, ret);
693
694         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
695                 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
696                 const tdm_output_mode *mode = NULL;
697                 unsigned int hdisplay, vdisplay, vrefresh;
698
699                 ret = tdm_output_get_mode(output, &mode);
700                 hdisplay = (mode) ? mode->hdisplay : 0;
701                 vdisplay = (mode) ? mode->vdisplay : 0;
702                 vrefresh = (mode) ? mode->vrefresh : 0;
703                 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
704
705                 ret = tdm_output_get_dpms(output, &dpms_value);
706                 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
707         } else {
708                 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
709                 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
710         }
711 }
712
713 static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource)
714 {
715         wl_resource_destroy(resource);
716 }
717
718 static void
719 _tdm_voutput_cb_set_available_modes(struct wl_client *client,
720                                                                         struct wl_resource *resource,
721                                                                         uint32_t index,
722                                                                         uint32_t clock,
723                                                                         uint32_t hdisplay,
724                                                                         uint32_t hsync_start,
725                                                                         uint32_t hsync_end,
726                                                                         uint32_t htotal,
727                                                                         uint32_t hskew,
728                                                                         uint32_t vdisplay,
729                                                                         uint32_t vsync_start,
730                                                                         uint32_t vsync_end,
731                                                                         uint32_t vtotal,
732                                                                         uint32_t vscan,
733                                                                         uint32_t vrefresh,
734                                                                         uint32_t flags,
735                                                                         uint32_t type,
736                                                                         const char *name)
737 {
738         tdm_server_voutput_info *voutput_info;
739         tdm_output_mode *tmp_modes, *old_modes;
740         tdm_output_mode *new_mode;
741         int count, len;
742
743         voutput_info = wl_resource_get_user_data(resource);
744
745         count = voutput_info->available_modes.count;
746         old_modes = voutput_info->available_modes.modes;
747         if (index >= count)
748         {
749                 if (count > 0)
750                 {
751                         tmp_modes = malloc(count * sizeof(*tmp_modes));
752                         memcpy(tmp_modes, old_modes, count * sizeof(tdm_output_mode));
753                 }
754
755                 voutput_info->available_modes.count = index + 1;
756                 voutput_info->available_modes.modes =
757                         realloc(voutput_info->available_modes.modes,
758                                         sizeof(tdm_output_mode) * (index + 1));
759
760                 if (count > 0)
761                 {
762                         memcpy(voutput_info->available_modes.modes, tmp_modes, count * sizeof(tdm_output_mode));
763                         free(tmp_modes);
764                 }
765         }
766
767         new_mode = &voutput_info->available_modes.modes[index];
768         new_mode->clock = clock;
769         new_mode->hdisplay = hdisplay;
770         new_mode->hsync_start = hsync_start;
771         new_mode->hsync_end = hsync_end;
772         new_mode->htotal = htotal;
773         new_mode->hskew= hskew;
774         new_mode->vdisplay= vdisplay;
775         new_mode->vsync_start= vsync_start;
776         new_mode->vsync_end = vsync_end;
777         new_mode->vtotal = vtotal;
778         new_mode->vscan = vscan;
779         new_mode->vrefresh = vrefresh;
780         new_mode->flags = flags;
781         new_mode->type = type;
782
783         len = strlen(name);
784         strncpy(new_mode->name, name, len);
785         new_mode->name[len] = '\0';
786 }
787
788 static void
789 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
790 {
791         tdm_server_voutput_info *voutput_info;
792
793         voutput_info = wl_resource_get_user_data(resource);
794         voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
795 }
796
797 static void
798 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
799 {
800         tdm_server_voutput_info *voutput_info;
801
802         voutput_info = wl_resource_get_user_data(resource);
803         voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
804
805         /* Do free resources when it's being disconnected */
806         free(voutput_info->available_modes.modes);
807         voutput_info->available_modes.modes = NULL;
808         voutput_info->available_modes.count = 0;
809 }
810
811 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
812         _tdm_voutput_cb_destroy,
813         _tdm_voutput_cb_set_available_modes,
814         _tdm_voutput_cb_connect,
815         _tdm_voutput_cb_disconnect
816 };
817
818 void
819 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
820 {
821         tdm_server_voutput_info *voutput_info;
822
823         voutput_info = wl_resource_get_user_data(resource);
824
825         /* Do free your own resource */
826         free(voutput_info);
827 }
828
829 static void
830 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
831 {
832         struct wl_resource *voutput_resource = NULL;
833         tdm_private_server *private_server = wl_resource_get_user_data(resource);
834         tdm_server_voutput_info *voutput_info;
835         tdm_output *output;
836         tdm_error ret;
837
838         output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
839         if (output) {
840                 TDM_ERR("There is '%s' output, cannot create.", name);
841                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
842                                                            "There is '%s' output", name);
843                 return;
844         }
845
846         output = tdm_display_create_output(private_server->private_loop->dpy, name, &ret);
847         if (!output) {
848                 TDM_ERR("output creation fail(%s)(%d).", name, ret);
849                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
850                                                            "%s output creation fail", name);
851                 return;
852         }
853
854         voutput_resource =
855                 wl_resource_create(client, &wl_tdm_voutput_interface,
856                                                    wl_resource_get_version(resource), id);
857         if (!voutput_resource) {
858                 /* LCOV_EXCL_START */
859
860                 wl_resource_post_no_memory(resource);
861                 TDM_ERR("wl_resource_create failed");
862                 return;
863
864                 /* LCOV_EXCL_STOP */
865         }
866
867         voutput_info = calloc(1, sizeof * voutput_info);
868         if (!voutput_info) {
869                 /* LCOV_EXCL_START */
870
871                 wl_resource_post_no_memory(resource);
872                 wl_resource_destroy(voutput_resource);
873                 TDM_ERR("alloc failed");
874                 return;
875
876                 /* LCOV_EXCL_STOP */
877         }
878
879         LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
880         voutput_info->private_server = private_server;
881         voutput_info->resource = voutput_resource;
882         voutput_info->output = output;
883         LIST_INITHEAD(&voutput_info->output_list);
884
885         wl_resource_set_implementation(voutput_resource,
886                                                                    &tdm_voutput_implementation,
887                                                                    voutput_info,
888                                                                    tdm_voutput_cb_resource_destroy);
889
890         wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
891 }
892
893 /* LCOV_EXCL_START */
894 static void
895 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
896 {
897         tdm_private_server *private_server = wl_resource_get_user_data(resource);
898         tdm_private_loop *private_loop = private_server->private_loop;
899         char message[TDM_SERVER_REPLY_MSG_LEN];
900         char *m;
901         int len = sizeof(message), size;
902         uid_t uid;
903
904         wl_client_get_credentials(client, NULL, &uid, NULL);
905
906         if (uid != 0) {
907                 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
908                 TDM_ERR("%s", message);
909         } else {
910                 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
911         }
912
913         size = sizeof(message) - len;
914         m = message;
915
916         wl_client_flush(client);
917
918         while (size > 0) {
919                 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
920                 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
921
922                 strncpy(buffer, m, copylen);
923                 m += copylen;
924                 size -= copylen;
925
926                 buffer[copylen] = '\0';
927
928                 wl_tdm_send_debug_message(resource, buffer);
929         }
930
931         wl_tdm_send_debug_done(resource);
932 }
933 /* LCOV_EXCL_STOP */
934
935 static const struct wl_tdm_interface tdm_implementation = {
936         _tdm_server_cb_debug,
937         _tdm_server_cb_create_output,
938         _tdm_server_cb_create_virtual_output
939 };
940
941 static void
942 destroy_client(struct wl_resource *resource)
943 {
944         tdm_server_client_info *c = NULL, *cc = NULL;
945         struct wl_client *client;
946
947         client = wl_resource_get_client(resource);
948         TDM_RETURN_IF_FAIL(client != NULL);
949
950         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
951                 if (c->resource == resource) {
952                         LIST_DEL(&c->link);
953                         free(c);
954                         return;
955                 }
956         }
957 }
958
959 static void
960 _tdm_server_bind(struct wl_client *client, void *data,
961                                  uint32_t version, uint32_t id)
962 {
963         struct wl_resource *resource;
964         tdm_server_client_info *cinfo;
965
966         resource = wl_resource_create(client, &wl_tdm_interface, version, id);
967         if (!resource) {
968                 /* LCOV_EXCL_START */
969
970                 wl_client_post_no_memory(client);
971                 return;
972
973                 /* LCOV_EXCL_STOP */
974         }
975
976         cinfo = calloc(1, sizeof(tdm_server_client_info));
977         if (!cinfo) {
978                 /* LCOV_EXCL_START */
979
980                 wl_client_post_no_memory(client);
981                 wl_resource_destroy(resource);
982                 return;
983
984                 /* LCOV_EXCL_STOP */
985         }
986
987         cinfo->resource = resource;
988
989         LIST_ADDTAIL(&cinfo->link, &client_list);
990         wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
991         _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
992
993         wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
994 }
995
996 INTERN tdm_error
997 tdm_server_init(tdm_private_loop *private_loop)
998 {
999         tdm_private_server *private_server;
1000
1001         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1002         TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1003
1004         if (private_loop->private_server)
1005                 return TDM_ERROR_NONE;
1006
1007         if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1008                 /* LCOV_EXCL_START */
1009
1010                 TDM_ERR("createing a tdm-socket failed");
1011                 return TDM_ERROR_OPERATION_FAILED;
1012
1013                 /* LCOV_EXCL_STOP */
1014         }
1015
1016         private_server = calloc(1, sizeof * private_server);
1017         if (!private_server) {
1018                 TDM_ERR("alloc failed");
1019                 return TDM_ERROR_OUT_OF_MEMORY;
1020         }
1021
1022         LIST_INITHEAD(&private_server->output_list);
1023         LIST_INITHEAD(&private_server->voutput_list);
1024         LIST_INITHEAD(&private_server->wait_list);
1025
1026         if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1027                                                   private_server, _tdm_server_bind)) {
1028                 /* LCOV_EXCL_START */
1029
1030                 TDM_ERR("creating a global resource failed");
1031                 free(private_server);
1032                 return TDM_ERROR_OUT_OF_MEMORY;
1033
1034                 /* LCOV_EXCL_STOP */
1035         }
1036
1037         private_server->private_loop = private_loop;
1038         private_loop->private_server = private_server;
1039         keep_private_server = private_server;
1040
1041         LIST_INITHEAD(&client_list);
1042
1043         return TDM_ERROR_NONE;
1044 }
1045
1046 INTERN void
1047 tdm_server_deinit(tdm_private_loop *private_loop)
1048 {
1049         tdm_server_output_info *o = NULL, *oo = NULL;
1050 //      tdm_server_voutput_info *vo = NULL, *voo = NULL;
1051         tdm_server_wait_info *w = NULL, *ww = NULL;
1052         tdm_server_client_info *c = NULL, *cc = NULL;
1053         tdm_private_server *private_server;
1054
1055         if (!private_loop->private_server)
1056                 return;
1057
1058         private_server = private_loop->private_server;
1059
1060         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1061                 destroy_wait(w);
1062         }
1063
1064         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1065                 wl_resource_destroy(o->resource);
1066         }
1067 #if 0
1068         LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1069                 wl_resource_destroy(vo->resource);
1070         }
1071 #endif
1072         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1073                 wl_resource_destroy(c->resource);
1074         }
1075
1076         free(private_server);
1077         private_loop->private_server = NULL;
1078         keep_private_server = NULL;
1079 }
1080
1081 /* LCOV_EXCL_START */
1082 INTERN const char*
1083 tdm_server_get_client_name(pid_t pid)
1084 {
1085         tdm_server_client_info *c = NULL;
1086
1087         LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1088                 if (c->pid == pid)
1089                         return (const char*)c->name;
1090         }
1091
1092         return NULL;
1093 }
1094 /* LCOV_EXCL_STOP */
1095
1096 /* LCOV_EXCL_START */
1097 INTERN tdm_error
1098 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1099 {
1100         tdm_private_server *private_server = keep_private_server;
1101         tdm_server_output_info *output_info = NULL;
1102
1103         if (!keep_private_server)
1104                 return TDM_ERROR_NONE;
1105
1106         LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1107                 tdm_server_vblank_info *vblank_info = NULL;
1108
1109                 if (output && output_info->output != output)
1110                         continue;
1111
1112                 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1113                         wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1114                 }
1115         }
1116
1117         return TDM_ERROR_NONE;
1118 }
1119 /* LCOV_EXCL_STOP */