apply tizen coding style
[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         LIST_DEL(&vblank_info->link);
203         free(vblank_info);
204 }
205
206 static void
207 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
208 {
209         wl_resource_destroy(resource);
210 }
211
212 static void
213 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
214 {
215         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
216
217         tdm_vblank_set_fps(vblank_info->vblank, fps);
218 }
219
220 static void
221 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
222 {
223         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
224
225         tdm_vblank_set_offset(vblank_info->vblank, offset);
226 }
227
228 static void
229 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
230 {
231         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
232
233         tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
234 }
235
236 static void
237 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
238                                                                   uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
239 {
240         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
241         tdm_server_output_info *output_info = vblank_info->output_info;
242         tdm_private_server *private_server = output_info->private_server;
243         tdm_server_wait_info *wait_info;
244         tdm_error ret;
245
246         wait_info = calloc(1, sizeof *wait_info);
247         if (!wait_info) {
248                 TDM_ERR("alloc failed");
249                 ret = TDM_ERROR_OUT_OF_MEMORY;
250                 goto wait_failed;
251         }
252
253         LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
254         wait_info->vblank_info = vblank_info;
255         wait_info->req_id = req_id;
256
257         if (tdm_debug_module & TDM_DEBUG_VBLANK)
258                 TDM_INFO("req_id(%d) wait", req_id);
259
260         ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
261         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
262
263         return;
264 wait_failed:
265         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
266 }
267
268 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
269         _tdm_server_vblank_cb_destroy,
270         _tdm_server_vblank_cb_set_fps,
271         _tdm_server_vblank_cb_set_offset,
272         _tdm_server_vblank_cb_set_enable_fake,
273         _tdm_server_vblank_cb_wait_vblank,
274 };
275
276 static void
277 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
278 {
279         wl_resource_destroy(resource);
280 }
281
282 static void
283 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
284 {
285         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
286         tdm_private_server *private_server = output_info->private_server;
287         tdm_private_loop *private_loop = private_server->private_loop;
288         struct wl_resource *vblank_resource;
289         tdm_vblank *vblank;
290         tdm_server_vblank_info *vblank_info;
291
292         vblank_resource =
293                 wl_resource_create(client, &wl_tdm_vblank_interface,
294                                                    wl_resource_get_version(resource), id);
295         if (!vblank_resource) {
296                 wl_resource_post_no_memory(resource);
297                 TDM_ERR("wl_resource_create failed");
298                 return;
299         }
300
301         vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
302         if (!vblank) {
303                 wl_resource_post_no_memory(resource);
304                 wl_resource_destroy(vblank_resource);
305                 TDM_ERR("tdm_vblank_create failed");
306                 return;
307         }
308
309         vblank_info = calloc(1, sizeof *vblank_info);
310         if (!vblank_info) {
311                 wl_resource_post_no_memory(resource);
312                 wl_resource_destroy(vblank_resource);
313                 tdm_vblank_destroy(vblank);
314                 TDM_ERR("alloc failed");
315                 return;
316         }
317
318         LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
319         vblank_info->output_info = output_info;
320         vblank_info->resource = vblank_resource;
321         vblank_info->vblank = vblank;
322
323         wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
324                                                                    vblank_info, destroy_vblank_callback);
325
326         return;
327 }
328
329 static const struct wl_tdm_output_interface tdm_output_implementation = {
330         _tdm_server_output_cb_destroy,
331         _tdm_server_output_cb_create_vblank,
332 };
333
334 static void
335 destroy_output_callback(struct wl_resource *resource)
336 {
337         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
338         tdm_server_vblank_info *v = NULL, *vv = NULL;
339
340         TDM_RETURN_IF_FAIL(output_info != NULL);
341
342         tdm_output_remove_change_handler(output_info->output,
343                                                                          _tdm_server_cb_output_change, output_info);
344
345         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
346                 wl_resource_destroy(v->resource);
347         }
348
349         LIST_DEL(&output_info->link);
350         free(output_info);
351 }
352
353 static void
354 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
355                                                          const char *name, uint32_t id)
356 {
357         tdm_private_server *private_server = wl_resource_get_user_data(resource);
358         tdm_server_output_info *output_info;
359         struct wl_resource *output_resource = NULL;
360         tdm_output *output;
361         const tdm_output_mode *mode = NULL;
362         tdm_output_dpms dpms_value;
363         tdm_output_conn_status status;
364
365         output = _tdm_server_find_output(private_server, name);
366         if (!output) {
367                 TDM_ERR("There is no '%s' output", name);
368                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
369                                                            "There is no '%s' output", name);
370                 return;
371         }
372
373         tdm_output_get_mode(output, &mode);
374         if (!mode) {
375                 TDM_ERR("no mode for '%s' output", name);
376                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
377                                                            "no mode for '%s' output", name);
378                 return;
379         }
380
381         tdm_output_get_dpms(output, &dpms_value);
382         tdm_output_get_conn_status(output, &status);
383
384         output_resource =
385                 wl_resource_create(client, &wl_tdm_output_interface,
386                                                    wl_resource_get_version(resource), id);
387         if (!output_resource) {
388                 wl_resource_post_no_memory(resource);
389                 TDM_ERR("wl_resource_create failed");
390                 return;
391         }
392
393         output_info = calloc(1, sizeof * output_info);
394         if (!output_info) {
395                 wl_resource_post_no_memory(resource);
396                 wl_resource_destroy(output_resource);
397                 TDM_ERR("alloc failed");
398                 return;
399         }
400
401         LIST_ADDTAIL(&output_info->link, &private_server->output_list);
402         output_info->private_server = private_server;
403         output_info->resource = output_resource;
404         output_info->output = output;
405         LIST_INITHEAD(&output_info->vblank_list);
406
407         tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
408
409         wl_resource_set_implementation(output_resource, &tdm_output_implementation,
410                                                                    output_info, destroy_output_callback);
411
412         wl_tdm_output_send_mode(output_resource, mode->hdisplay, mode->vdisplay, mode->vrefresh);
413         wl_tdm_output_send_dpms(output_resource, dpms_value);
414         wl_tdm_output_send_connection(output_resource, status);
415 }
416
417 static void
418 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
419 {
420         tdm_private_server *private_server = wl_resource_get_user_data(resource);
421         tdm_private_loop *private_loop = private_server->private_loop;
422
423         char message[TDM_SERVER_REPLY_MSG_LEN];
424         int size = sizeof(message);
425
426         tdm_monitor_server_command(private_loop->dpy, options, message, &size);
427         wl_tdm_send_debug_done(resource, message);
428 }
429
430 static const struct wl_tdm_interface tdm_implementation = {
431         _tdm_server_cb_create_output,
432         _tdm_server_cb_debug,
433 };
434
435 static void
436 _tdm_server_bind(struct wl_client *client, void *data,
437                                  uint32_t version, uint32_t id)
438 {
439         struct wl_resource *resource;
440
441         resource = wl_resource_create(client, &wl_tdm_interface, version, id);
442         if (!resource) {
443                 wl_client_post_no_memory(client);
444                 return;
445         }
446
447         wl_resource_set_implementation(resource, &tdm_implementation, data, NULL);
448 }
449
450 INTERN tdm_error
451 tdm_server_init(tdm_private_loop *private_loop)
452 {
453         tdm_private_server *private_server;
454
455         if (private_loop->private_server)
456                 return TDM_ERROR_NONE;
457
458         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
459         TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
460
461         if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
462                 TDM_ERR("createing a tdm-socket failed");
463                 return TDM_ERROR_OPERATION_FAILED;
464         }
465
466         private_server = calloc(1, sizeof * private_server);
467         if (!private_server) {
468                 TDM_ERR("alloc failed");
469                 return TDM_ERROR_OUT_OF_MEMORY;
470         }
471
472         LIST_INITHEAD(&private_server->output_list);
473         LIST_INITHEAD(&private_server->wait_list);
474
475         if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
476                                                   private_server, _tdm_server_bind)) {
477                 TDM_ERR("creating a global resource failed");
478                 free(private_server);
479                 return TDM_ERROR_OUT_OF_MEMORY;
480         }
481
482         private_server->private_loop = private_loop;
483         private_loop->private_server = private_server;
484         keep_private_server = private_server;
485
486         return TDM_ERROR_NONE;
487 }
488
489 INTERN void
490 tdm_server_deinit(tdm_private_loop *private_loop)
491 {
492         tdm_server_output_info *o = NULL, *oo = NULL;
493         tdm_server_wait_info *w = NULL, *ww = NULL;
494         tdm_private_server *private_server;
495
496         if (!private_loop->private_server)
497                 return;
498
499         private_server = private_loop->private_server;
500
501         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
502                 destroy_wait(w);
503         }
504
505         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
506                 wl_resource_destroy(o->resource);
507         }
508
509         free(private_server);
510         private_loop->private_server = NULL;
511         keep_private_server = NULL;
512 }