08fb539a038e0ac060afb92e3237e499c496968c
[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                                                                         struct wl_array *modes)
734 {
735         tdm_server_voutput_info *voutput_info;
736         tdm_output_mode *mode;
737         int size, count = 0, i = 0;
738
739         voutput_info = wl_resource_get_user_data(resource);
740
741         voutput_info->available_modes.count = 0;
742         if (voutput_info->available_modes.modes)
743                 free(voutput_info->available_modes.modes);
744
745         wl_array_for_each(mode, modes)
746                 count++;
747         size = sizeof(tdm_output_mode);
748
749         voutput_info->available_modes.modes = malloc(count * size);
750         voutput_info->available_modes.count = count;
751
752         wl_array_for_each(mode, modes)
753                 memcpy(&voutput_info->available_modes.modes[i++], mode, size);
754 }
755
756 static void
757 _tdm_voutput_cb_set_available_formats(struct wl_client *client,
758                                                                           struct wl_resource *resource,
759                                                                           struct wl_array *formats)
760 {
761         tdm_server_voutput_info *voutput_info;
762         tbm_format *f;
763         int count = 0, i = 0;
764
765         voutput_info = wl_resource_get_user_data(resource);
766
767         voutput_info->available_formats.count = 0;
768         if (voutput_info->available_formats.formats)
769                 free(voutput_info->available_formats.formats);
770
771         wl_array_for_each(f, formats)
772                 count++;
773
774         voutput_info->available_formats.formats = malloc(count * sizeof(tbm_format));
775         voutput_info->available_formats.count = count;
776
777         wl_array_for_each(f, formats)
778                 voutput_info->available_formats.formats[i++] = *f;
779 }
780
781 static void
782 _tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource,
783                                                                   unsigned int mmwidth, unsigned int mmheight)
784 {
785         tdm_server_voutput_info *voutput_info;
786
787         voutput_info = wl_resource_get_user_data(resource);
788
789         voutput_info->mmwidth = mmwidth;
790         voutput_info->mmheight = mmheight;
791 }
792
793 static void
794 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
795 {
796         tdm_server_voutput_info *voutput_info;
797
798         voutput_info = wl_resource_get_user_data(resource);
799         voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
800
801         tdm_output_set_physical_size(voutput_info->output, voutput_info->mmwidth, voutput_info->mmheight);
802         tdm_output_set_available_mode(voutput_info->output, voutput_info->available_modes.modes, voutput_info->available_modes.count);
803         tdm_output_set_connect(voutput_info->output);
804 }
805
806 static void
807 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
808 {
809         tdm_server_voutput_info *voutput_info;
810
811         voutput_info = wl_resource_get_user_data(resource);
812         voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
813
814         /* Do free resources when it's being disconnected */
815         free(voutput_info->available_modes.modes);
816         voutput_info->available_modes.modes = NULL;
817         voutput_info->available_modes.count = 0;
818         voutput_info->mmwidth = 0;
819         voutput_info->mmheight = 0;
820
821         tdm_output_set_disconnect(voutput_info->output);
822 }
823
824 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
825         _tdm_voutput_cb_destroy,
826         _tdm_voutput_cb_set_available_modes,
827         _tdm_voutput_cb_set_available_formats,
828         _tdm_voutput_cb_set_physical_size,
829         _tdm_voutput_cb_connect,
830         _tdm_voutput_cb_disconnect
831 };
832
833 void
834 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
835 {
836         tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
837
838         TDM_RETURN_IF_FAIL(voutput_info != NULL);
839
840         LIST_DEL(&voutput_info->link);
841
842         /* Do free your own resource */
843         free(voutput_info);
844 }
845
846 static void
847 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
848 {
849         struct wl_resource *voutput_resource = NULL;
850         tdm_private_server *private_server = wl_resource_get_user_data(resource);
851         tdm_server_voutput_info *voutput_info;
852         tdm_output *output;
853         tdm_error ret;
854
855         output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
856         if (output) {
857                 TDM_ERR("There is '%s' output, cannot create.", name);
858                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
859                                                            "There is '%s' output", name);
860                 return;
861         }
862
863         output = tdm_display_create_output(private_server->private_loop->dpy, name, &ret);
864         if (!output) {
865                 TDM_ERR("output creation fail(%s)(%d).", name, ret);
866                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
867                                                            "%s output creation fail", name);
868                 return;
869         }
870
871         voutput_resource =
872                 wl_resource_create(client, &wl_tdm_voutput_interface,
873                                                    wl_resource_get_version(resource), id);
874         if (!voutput_resource) {
875                 /* LCOV_EXCL_START */
876
877                 wl_resource_post_no_memory(resource);
878                 TDM_ERR("wl_resource_create failed");
879                 return;
880
881                 /* LCOV_EXCL_STOP */
882         }
883
884         voutput_info = calloc(1, sizeof * voutput_info);
885         if (!voutput_info) {
886                 /* LCOV_EXCL_START */
887
888                 wl_resource_post_no_memory(resource);
889                 wl_resource_destroy(voutput_resource);
890                 TDM_ERR("alloc failed");
891                 return;
892
893                 /* LCOV_EXCL_STOP */
894         }
895
896         LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
897         voutput_info->private_server = private_server;
898         voutput_info->resource = voutput_resource;
899         voutput_info->output = output;
900         LIST_INITHEAD(&voutput_info->output_list);
901
902         wl_resource_set_implementation(voutput_resource,
903                                                                    &tdm_voutput_implementation,
904                                                                    voutput_info,
905                                                                    tdm_voutput_cb_resource_destroy);
906
907         wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
908 }
909
910 /* LCOV_EXCL_START */
911 static void
912 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
913 {
914         tdm_private_server *private_server = wl_resource_get_user_data(resource);
915         tdm_private_loop *private_loop = private_server->private_loop;
916         char message[TDM_SERVER_REPLY_MSG_LEN];
917         char *m;
918         int len = sizeof(message), size;
919         uid_t uid;
920
921         wl_client_get_credentials(client, NULL, &uid, NULL);
922
923         if (uid != 0) {
924                 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
925                 TDM_ERR("%s", message);
926         } else {
927                 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
928         }
929
930         size = sizeof(message) - len;
931         m = message;
932
933         wl_client_flush(client);
934
935         while (size > 0) {
936                 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
937                 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
938
939                 strncpy(buffer, m, copylen);
940                 m += copylen;
941                 size -= copylen;
942
943                 buffer[copylen] = '\0';
944
945                 wl_tdm_send_debug_message(resource, buffer);
946         }
947
948         wl_tdm_send_debug_done(resource);
949 }
950 /* LCOV_EXCL_STOP */
951
952 static const struct wl_tdm_interface tdm_implementation = {
953         _tdm_server_cb_debug,
954         _tdm_server_cb_create_output,
955         _tdm_server_cb_create_virtual_output
956 };
957
958 static void
959 destroy_client(struct wl_resource *resource)
960 {
961         tdm_server_client_info *c = NULL, *cc = NULL;
962         struct wl_client *client;
963
964         client = wl_resource_get_client(resource);
965         TDM_RETURN_IF_FAIL(client != NULL);
966
967         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
968                 if (c->resource == resource) {
969                         LIST_DEL(&c->link);
970                         free(c);
971                         return;
972                 }
973         }
974 }
975
976 static void
977 _tdm_server_bind(struct wl_client *client, void *data,
978                                  uint32_t version, uint32_t id)
979 {
980         struct wl_resource *resource;
981         tdm_server_client_info *cinfo;
982
983         resource = wl_resource_create(client, &wl_tdm_interface, version, id);
984         if (!resource) {
985                 /* LCOV_EXCL_START */
986
987                 wl_client_post_no_memory(client);
988                 return;
989
990                 /* LCOV_EXCL_STOP */
991         }
992
993         cinfo = calloc(1, sizeof(tdm_server_client_info));
994         if (!cinfo) {
995                 /* LCOV_EXCL_START */
996
997                 wl_client_post_no_memory(client);
998                 wl_resource_destroy(resource);
999                 return;
1000
1001                 /* LCOV_EXCL_STOP */
1002         }
1003
1004         cinfo->resource = resource;
1005
1006         LIST_ADDTAIL(&cinfo->link, &client_list);
1007         wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
1008         _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
1009
1010         wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1011 }
1012
1013 INTERN tdm_error
1014 tdm_server_init(tdm_private_loop *private_loop)
1015 {
1016         tdm_private_server *private_server;
1017
1018         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1019         TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1020
1021         if (private_loop->private_server)
1022                 return TDM_ERROR_NONE;
1023
1024         if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1025                 /* LCOV_EXCL_START */
1026
1027                 TDM_ERR("createing a tdm-socket failed");
1028                 return TDM_ERROR_OPERATION_FAILED;
1029
1030                 /* LCOV_EXCL_STOP */
1031         }
1032
1033         private_server = calloc(1, sizeof * private_server);
1034         if (!private_server) {
1035                 TDM_ERR("alloc failed");
1036                 return TDM_ERROR_OUT_OF_MEMORY;
1037         }
1038
1039         LIST_INITHEAD(&private_server->output_list);
1040         LIST_INITHEAD(&private_server->voutput_list);
1041         LIST_INITHEAD(&private_server->wait_list);
1042
1043         if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1044                                                   private_server, _tdm_server_bind)) {
1045                 /* LCOV_EXCL_START */
1046
1047                 TDM_ERR("creating a global resource failed");
1048                 free(private_server);
1049                 return TDM_ERROR_OUT_OF_MEMORY;
1050
1051                 /* LCOV_EXCL_STOP */
1052         }
1053
1054         private_server->private_loop = private_loop;
1055         private_loop->private_server = private_server;
1056         keep_private_server = private_server;
1057
1058         LIST_INITHEAD(&client_list);
1059
1060         return TDM_ERROR_NONE;
1061 }
1062
1063 INTERN void
1064 tdm_server_deinit(tdm_private_loop *private_loop)
1065 {
1066         tdm_server_output_info *o = NULL, *oo = NULL;
1067 //      tdm_server_voutput_info *vo = NULL, *voo = NULL;
1068         tdm_server_wait_info *w = NULL, *ww = NULL;
1069         tdm_server_client_info *c = NULL, *cc = NULL;
1070         tdm_private_server *private_server;
1071
1072         if (!private_loop->private_server)
1073                 return;
1074
1075         private_server = private_loop->private_server;
1076
1077         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1078                 destroy_wait(w);
1079         }
1080
1081         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1082                 wl_resource_destroy(o->resource);
1083         }
1084 #if 0
1085         LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1086                 wl_resource_destroy(vo->resource);
1087         }
1088 #endif
1089         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1090                 wl_resource_destroy(c->resource);
1091         }
1092
1093         free(private_server);
1094         private_loop->private_server = NULL;
1095         keep_private_server = NULL;
1096 }
1097
1098 /* LCOV_EXCL_START */
1099 INTERN const char*
1100 tdm_server_get_client_name(pid_t pid)
1101 {
1102         tdm_server_client_info *c = NULL;
1103
1104         LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1105                 if (c->pid == pid)
1106                         return (const char*)c->name;
1107         }
1108
1109         return NULL;
1110 }
1111 /* LCOV_EXCL_STOP */
1112
1113 /* LCOV_EXCL_START */
1114 INTERN tdm_error
1115 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1116 {
1117         tdm_private_server *private_server = keep_private_server;
1118         tdm_server_output_info *output_info = NULL;
1119
1120         if (!keep_private_server)
1121                 return TDM_ERROR_NONE;
1122
1123         LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1124                 tdm_server_vblank_info *vblank_info = NULL;
1125
1126                 if (output && output_info->output != output)
1127                         continue;
1128
1129                 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1130                         wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1131                 }
1132         }
1133
1134         return TDM_ERROR_NONE;
1135 }
1136 /* LCOV_EXCL_STOP */