fix the too many logs when enabled
[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 <sc1.lim@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.h"
41 #include "tdm_private.h"
42 #include "tdm_list.h"
43 #include "tdm-server-protocol.h"
44
45 /* CAUTION:
46  * - tdm server doesn't care about thread things.
47  * - DO NOT use the TDM internal functions here.
48  *     However, the internal function which does lock/unlock the mutex of
49  *     private_display in itself can be called.
50  * - DO NOT use the tdm_private_display structure here.
51  * - The callback function things can be called in main thread.
52  */
53
54 struct _tdm_private_server {
55         tdm_private_loop *private_loop;
56         struct list_head output_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 } tdm_server_output_info;
67
68 typedef struct _tdm_server_vblank_info {
69         struct list_head link;
70         tdm_server_output_info *output_info;
71         struct wl_resource *resource;
72
73         tdm_vblank *vblank;
74 } tdm_server_vblank_info;
75
76 typedef struct _tdm_server_wait_info {
77         struct list_head link;
78         tdm_server_vblank_info *vblank_info;
79
80         unsigned int req_id;
81 } tdm_server_wait_info;
82
83 static tdm_private_server *keep_private_server;
84
85 static void destroy_wait(tdm_server_wait_info *wait_info);
86
87 static tdm_output*
88 _tdm_server_find_output(tdm_private_server *private_server, const char *name)
89 {
90         tdm_private_loop *private_loop = private_server->private_loop;
91         tdm_output *found = NULL;
92
93         if (!strncasecmp(name, "primary", 7) || !strncasecmp(name, "default", 7))
94                 found = tdm_display_get_output(private_loop->dpy, 0, NULL);
95
96         if (!found) {
97                 int count = 0, i;
98
99                 tdm_display_get_output_count(private_loop->dpy, &count);
100
101                 for (i = 0; i < count; i++) {
102                         tdm_output *output = tdm_display_get_output(private_loop->dpy, i, NULL);
103                         tdm_output_conn_status status;
104                         const char *model = NULL;
105                         tdm_error ret;
106
107                         ret = tdm_output_get_conn_status(output, &status);
108                         if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
109                                 continue;
110
111                         ret = tdm_output_get_model_info(output, NULL, &model, NULL);
112                         if (ret || !model)
113                                 continue;
114
115                         if (strncmp(model, name, TDM_NAME_LEN))
116                                 continue;
117
118                         found = output;
119                         break;
120                 }
121         }
122
123         return found;
124 }
125
126 static void
127 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
128                                           unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
129 {
130         tdm_server_wait_info *found;
131         tdm_server_vblank_info *vblank_info;
132
133         if (!keep_private_server)
134                 return;
135
136         LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
137                                    tdm_server_wait_info, link, found);
138         if (!found) {
139                 TDM_DBG("wait_info(%p) is destroyed", wait_info);
140                 return;
141         }
142
143         if (tdm_debug_module & TDM_DEBUG_VBLANK)
144                 TDM_INFO("req_id(%d) done", wait_info->req_id);
145
146         vblank_info = wait_info->vblank_info;
147         wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
148                                                         sequence, tv_sec, tv_usec, error);
149         destroy_wait(wait_info);
150 }
151
152 static void
153 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
154                                           unsigned int tv_sec, unsigned int tv_usec, void *user_data)
155 {
156         _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
157 }
158
159 static void
160 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
161                                                          tdm_value value, void *user_data)
162 {
163         tdm_server_output_info *output_info = user_data;
164
165         TDM_RETURN_IF_FAIL(output_info != NULL);
166
167         switch (type) {
168         case TDM_OUTPUT_CHANGE_DPMS:
169                 wl_tdm_output_send_dpms(output_info->resource, value.u32);
170                 break;
171         case TDM_OUTPUT_CHANGE_CONNECTION:
172                 wl_tdm_output_send_connection(output_info->resource, value.u32);
173                 break;
174         default:
175                 break;
176         }
177 }
178
179 static void
180 destroy_wait(tdm_server_wait_info *wait_info)
181 {
182         LIST_DEL(&wait_info->link);
183         free(wait_info);
184 }
185
186 static void
187 destroy_vblank_callback(struct wl_resource *resource)
188 {
189         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
190         tdm_server_wait_info *w = NULL, *ww = NULL;
191
192         TDM_RETURN_IF_FAIL(vblank_info != NULL);
193
194         if (vblank_info->vblank)
195                 tdm_vblank_destroy(vblank_info->vblank);
196
197         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
198                 if (w->vblank_info == vblank_info) {
199                         destroy_wait(w);
200                 }
201         }
202
203         LIST_DEL(&vblank_info->link);
204         free(vblank_info);
205 }
206
207 static void
208 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
209 {
210         wl_resource_destroy(resource);
211 }
212
213 static void
214 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
215 {
216         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
217
218         tdm_vblank_set_fps(vblank_info->vblank, fps);
219 }
220
221 static void
222 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
223 {
224         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
225
226         tdm_vblank_set_offset(vblank_info->vblank, offset);
227 }
228
229 static void
230 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
231 {
232         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
233
234         tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
235 }
236
237 static void
238 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
239                                                                   uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
240 {
241         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
242         tdm_server_output_info *output_info = vblank_info->output_info;
243         tdm_private_server *private_server = output_info->private_server;
244         tdm_server_wait_info *wait_info;
245         tdm_error ret;
246
247         wait_info = calloc(1, sizeof *wait_info);
248         if (!wait_info) {
249                 TDM_ERR("alloc failed");
250                 ret = TDM_ERROR_OUT_OF_MEMORY;
251                 goto wait_failed;
252         }
253
254         LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
255         wait_info->vblank_info = vblank_info;
256         wait_info->req_id = req_id;
257
258         if (tdm_debug_module & TDM_DEBUG_VBLANK)
259                 TDM_INFO("req_id(%d) wait", req_id);
260
261         ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
262         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
263
264         return;
265 wait_failed:
266         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
267 }
268
269 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
270         _tdm_server_vblank_cb_destroy,
271         _tdm_server_vblank_cb_set_fps,
272         _tdm_server_vblank_cb_set_offset,
273         _tdm_server_vblank_cb_set_enable_fake,
274         _tdm_server_vblank_cb_wait_vblank,
275 };
276
277 static void
278 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
279 {
280         wl_resource_destroy(resource);
281 }
282
283 static void
284 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
285 {
286         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
287         tdm_private_server *private_server = output_info->private_server;
288         tdm_private_loop *private_loop = private_server->private_loop;
289         struct wl_resource *vblank_resource;
290         tdm_vblank *vblank;
291         tdm_server_vblank_info *vblank_info;
292
293         vblank_resource =
294                 wl_resource_create(client, &wl_tdm_vblank_interface,
295                                                    wl_resource_get_version(resource), id);
296         if (!vblank_resource) {
297                 wl_resource_post_no_memory(resource);
298                 TDM_ERR("wl_resource_create failed");
299                 return;
300         }
301
302         vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
303         if (!vblank) {
304                 wl_resource_post_no_memory(resource);
305                 wl_resource_destroy(vblank_resource);
306                 TDM_ERR("tdm_vblank_create failed");
307                 return;
308         }
309
310         vblank_info = calloc(1, sizeof *vblank_info);
311         if (!vblank_info) {
312                 wl_resource_post_no_memory(resource);
313                 wl_resource_destroy(vblank_resource);
314                 tdm_vblank_destroy(vblank);
315                 TDM_ERR("alloc failed");
316                 return;
317         }
318
319         LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
320         vblank_info->output_info = output_info;
321         vblank_info->resource = vblank_resource;
322         vblank_info->vblank = vblank;
323
324         wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
325                                                                    vblank_info, destroy_vblank_callback);
326
327         return;
328 }
329
330 static const struct wl_tdm_output_interface tdm_output_implementation = {
331         _tdm_server_output_cb_destroy,
332         _tdm_server_output_cb_create_vblank,
333 };
334
335 static void
336 destroy_output_callback(struct wl_resource *resource)
337 {
338         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
339         tdm_server_vblank_info *v = NULL, *vv = NULL;
340
341         TDM_RETURN_IF_FAIL(output_info != NULL);
342
343         tdm_output_remove_change_handler(output_info->output,
344                                                                          _tdm_server_cb_output_change, output_info);
345
346         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
347                 wl_resource_destroy(v->resource);
348         }
349
350         LIST_DEL(&output_info->link);
351         free(output_info);
352 }
353
354 static void
355 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
356                                                          const char *name, uint32_t id)
357 {
358         tdm_private_server *private_server = wl_resource_get_user_data(resource);
359         tdm_server_output_info *output_info;
360         struct wl_resource *output_resource = NULL;
361         tdm_output *output;
362         const tdm_output_mode *mode = NULL;
363         tdm_output_dpms dpms_value;
364         tdm_output_conn_status status;
365
366         output = _tdm_server_find_output(private_server, name);
367         if (!output) {
368                 TDM_ERR("There is no '%s' output", name);
369                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
370                                "There is no '%s' output", name);
371                 return;
372         }
373
374         tdm_output_get_mode(output, &mode);
375         if (!mode) {
376                 TDM_ERR("no mode for '%s' output", name);
377                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
378                                "no mode for '%s' output", name);
379                 return;
380         }
381
382         tdm_output_get_dpms(output, &dpms_value);
383         tdm_output_get_conn_status(output, &status);
384
385         output_resource =
386                 wl_resource_create(client, &wl_tdm_output_interface,
387                                                    wl_resource_get_version(resource), id);
388         if (!output_resource) {
389                 wl_resource_post_no_memory(resource);
390                 TDM_ERR("wl_resource_create failed");
391                 return;
392         }
393
394         output_info = calloc(1, sizeof * output_info);
395         if (!output_info) {
396                 wl_resource_post_no_memory(resource);
397                 wl_resource_destroy(output_resource);
398                 TDM_ERR("alloc failed");
399                 return;
400         }
401
402         LIST_ADDTAIL(&output_info->link, &private_server->output_list);
403         output_info->private_server = private_server;
404         output_info->resource = output_resource;
405         output_info->output = output;
406         LIST_INITHEAD(&output_info->vblank_list);
407
408         tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
409
410         wl_resource_set_implementation(output_resource, &tdm_output_implementation,
411                                                                    output_info, destroy_output_callback);
412
413         wl_tdm_output_send_mode(output_resource, mode->hdisplay, mode->vdisplay, mode->vrefresh);
414         wl_tdm_output_send_dpms(output_resource, dpms_value);
415         wl_tdm_output_send_connection(output_resource, status);
416 }
417
418 static void
419 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
420 {
421         tdm_private_server *private_server = wl_resource_get_user_data(resource);
422         tdm_private_loop *private_loop = private_server->private_loop;
423
424         char message[TDM_SERVER_REPLY_MSG_LEN];
425         int size = sizeof(message);
426
427         tdm_dbg_server_command(private_loop->dpy, options, message, &size);
428         wl_tdm_send_debug_done(resource, message);
429 }
430
431 static const struct wl_tdm_interface tdm_implementation = {
432         _tdm_server_cb_create_output,
433         _tdm_server_cb_debug,
434 };
435
436 static void
437 _tdm_server_bind(struct wl_client *client, void *data,
438                                  uint32_t version, uint32_t id)
439 {
440         struct wl_resource *resource;
441
442         resource = wl_resource_create(client, &wl_tdm_interface, version, id);
443         if (!resource) {
444                 wl_client_post_no_memory(client);
445                 return;
446         }
447
448         wl_resource_set_implementation(resource, &tdm_implementation, data, NULL);
449 }
450
451 INTERN tdm_error
452 tdm_server_init(tdm_private_loop *private_loop)
453 {
454         tdm_private_server *private_server;
455
456         if (private_loop->private_server)
457                 return TDM_ERROR_NONE;
458
459         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
460         TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
461
462         if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
463                 TDM_ERR("createing a tdm-socket failed");
464                 return TDM_ERROR_OPERATION_FAILED;
465         }
466
467         private_server = calloc(1, sizeof * private_server);
468         if (!private_server) {
469                 TDM_ERR("alloc failed");
470                 return TDM_ERROR_OUT_OF_MEMORY;
471         }
472
473         LIST_INITHEAD(&private_server->output_list);
474         LIST_INITHEAD(&private_server->wait_list);
475
476         if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
477                                                   private_server, _tdm_server_bind)) {
478                 TDM_ERR("creating a global resource failed");
479                 free(private_server);
480                 return TDM_ERROR_OUT_OF_MEMORY;
481         }
482
483         private_server->private_loop = private_loop;
484         private_loop->private_server = private_server;
485         keep_private_server = private_server;
486
487         return TDM_ERROR_NONE;
488 }
489
490 INTERN void
491 tdm_server_deinit(tdm_private_loop *private_loop)
492 {
493         tdm_server_output_info *o = NULL, *oo = NULL;
494         tdm_server_wait_info *w = NULL, *ww = NULL;
495         tdm_private_server *private_server;
496
497         if (!private_loop->private_server)
498                 return;
499
500         private_server = private_loop->private_server;
501
502         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
503                 destroy_wait(w);
504         }
505
506         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
507                 wl_resource_destroy(o->resource);
508         }
509
510         free(private_server);
511         private_loop->private_server = NULL;
512         keep_private_server = NULL;
513 }