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