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