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