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