9a3a045178d923835c185dcdc827bd63a29a9f47
[platform/core/uifw/pepper-dali.git] / pepper-dali / internal / extensions / tizen-policy.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "tizen-policy.h"
19
20 #include <dali/integration-api/debug.h>
21 #include <tizen-extension-server-protocol.h>
22
23 namespace Dali
24 {
25
26 namespace Pepper
27 {
28
29 namespace Internal
30 {
31
32 namespace Extension
33 {
34
35 namespace
36 {
37
38 #if defined(DEBUG_ENABLED)
39 Integration::Log::Filter* gPepperTizenPolicyLogging  = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_PEPPER_TIZEN_POLICY" );
40 #endif
41
42 } // unnamed namespace
43
44 /* tizen policy data for pepper_surface_t */
45 typedef struct
46 {
47    pepper_surface_t *psurf;
48    pepper_event_listener_t *psurf_destroy_listener;
49    struct wl_list resource_list;
50    VisibilityState visibility;
51    int references;
52 } tzpol_surface_t;
53
54 const static char       *tsurf_data_key = "tzpol-surf";
55 static struct wl_global *_tzpol_global = NULL;
56 static struct wl_list    _tzpol_res_list;
57
58 static void
59 _tzpol_surface_del(tzpol_surface_t *tsurf)
60 {
61    free(tsurf);
62 }
63
64 static void
65 _tzpol_surface_unref(tzpol_surface_t *tsurf, struct wl_resource *unref)
66 {
67    int res;
68
69    res = wl_list_empty(&tsurf->resource_list);
70    if (res)
71      {
72         DALI_LOG_INFO( gPepperTizenPolicyLogging, Debug::General, "Couldn't unreference, list is empty" );
73         return;
74      }
75
76    wl_list_remove(wl_resource_get_link(unref));
77    wl_resource_set_user_data(unref, NULL);
78
79    res = wl_list_empty(&tsurf->resource_list);
80    if (res)
81      {
82         pepper_event_listener_remove(tsurf->psurf_destroy_listener);
83         pepper_object_set_user_data((pepper_object_t *)tsurf->psurf,
84                                     (const void *)tsurf_data_key, NULL, NULL);
85         _tzpol_surface_del(tsurf);
86      }
87 }
88
89 static void
90 _tzpol_surface_ref(tzpol_surface_t *tsurf, struct wl_resource *ref)
91 {
92    wl_list_insert(&tsurf->resource_list, wl_resource_get_link(ref));
93    wl_resource_set_user_data(ref, tsurf);
94 }
95
96 static void
97 _tzpol_surface_pepper_surf_cb_destroy(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
98 {
99    tzpol_surface_t *tsurf;
100    struct wl_resource *res, *tmp;
101
102    tsurf = (tzpol_surface_t*)data;
103
104    wl_resource_for_each_safe(res, tmp, &tsurf->resource_list)
105         _tzpol_surface_unref(tsurf, res);
106 }
107
108 static tzpol_surface_t *
109 _tzpol_surface_get(pepper_surface_t *psurf)
110 {
111    tzpol_surface_t *tsurf;
112
113    tsurf = (tzpol_surface_t*)pepper_object_get_user_data((pepper_object_t *)psurf,
114                                        (const void *)tsurf_data_key);
115    if (!tsurf)
116      {
117         tsurf = (tzpol_surface_t*)calloc(1, sizeof(*tsurf));
118         if (!tsurf)
119           return NULL;
120
121         tsurf->psurf = psurf;
122
123         wl_list_init(&tsurf->resource_list);
124
125         tsurf->psurf_destroy_listener =
126            pepper_object_add_event_listener((pepper_object_t *)psurf,
127                                             PEPPER_EVENT_OBJECT_DESTROY, 0,
128                                             _tzpol_surface_pepper_surf_cb_destroy, tsurf);
129         pepper_object_set_user_data((pepper_object_t *)psurf,
130                                     (const void *)tsurf_data_key, tsurf, NULL);
131      }
132
133    tsurf->references++;
134
135    return tsurf;
136 }
137
138 // --------------------------------------------------------
139 // visibility
140 // --------------------------------------------------------
141 static void
142 _tzvis_iface_cb_destroy(struct wl_client *client, struct wl_resource *res_tzvis)
143 {
144    wl_resource_destroy(res_tzvis);
145 }
146
147 static const struct tizen_visibility_interface _tzvis_iface =
148 {
149    _tzvis_iface_cb_destroy
150 };
151
152 static void
153 _tzvis_res_cb_destroy(struct wl_resource *res_tzvis)
154 {
155    tzpol_surface_t *tsurf;
156
157    tsurf = (tzpol_surface_t*)wl_resource_get_user_data(res_tzvis);
158    if (!tsurf)
159      return;
160
161    _tzpol_surface_unref(tsurf, res_tzvis);
162 }
163
164 static void
165 _tzpol_iface_cb_vis_get(struct wl_client *client, struct wl_resource *res_tzpol, uint32_t id, struct wl_resource *surf)
166 {
167    tzpol_surface_t *tsurf;
168    pepper_surface_t *psurf;
169    struct wl_resource *new_res;
170    int ver;
171
172    if (!surf)
173      {
174         wl_resource_post_error(res_tzpol, WL_DISPLAY_ERROR_INVALID_OBJECT,
175                                "tizen_policy::visibility_get requires surface");
176         return;
177      }
178
179    psurf = (pepper_surface_t*)wl_resource_get_user_data(surf);
180    if (!psurf)
181      {
182         DALI_LOG_INFO( gPepperTizenPolicyLogging, Debug::General, "failed to get pepper_surface from wl_resource" );
183         wl_resource_post_error(res_tzpol, WL_DISPLAY_ERROR_INVALID_OBJECT,
184                                "tizen_policy::visibility_get invalid surface resource");
185         return;
186      }
187
188    DALI_LOG_INFO( gPepperTizenPolicyLogging, Debug::General, "tizen_policy::visibility_get" );
189    tsurf = _tzpol_surface_get(psurf);
190    if (!tsurf)
191      {
192         wl_resource_post_no_memory(res_tzpol);
193         return;
194      }
195
196    ver = wl_resource_get_version(res_tzpol);
197    new_res = wl_resource_create(client, &tizen_visibility_interface, ver, id);
198    if (!new_res)
199      {
200         wl_resource_post_no_memory(res_tzpol);
201         return;
202      }
203
204    wl_resource_set_implementation(new_res, &_tzvis_iface, tsurf, _tzvis_res_cb_destroy);
205    _tzpol_surface_ref(tsurf, new_res);
206 }
207
208 // --------------------------------------------------------
209 // position
210 // --------------------------------------------------------
211 static void
212 _tzpos_iface_cb_destroy(struct wl_client *client, struct wl_resource *res_tzpos)
213 {
214    wl_resource_destroy(res_tzpos);
215 }
216
217 static void
218 _tzpos_iface_cb_set(struct wl_client *client, struct wl_resource *res_tzpos, int32_t x, int32_t y)
219 {
220    (void)client;
221    (void)res_tzpos;
222    (void)x;
223    (void)y;
224 }
225
226 static const struct tizen_position_interface _tzpos_iface =
227 {
228    _tzpos_iface_cb_destroy,
229    _tzpos_iface_cb_set,
230 };
231
232 static void
233 _tzpos_res_cb_destroy(struct wl_resource *res_tzpos)
234 {
235    tzpol_surface_t *tsurf;
236
237    tsurf = (tzpol_surface_t*)wl_resource_get_user_data(res_tzpos);
238    if (!tsurf)
239      return;
240
241    _tzpol_surface_unref(tsurf, res_tzpos);
242 }
243
244 static void
245 _tzpol_iface_cb_pos_get(struct wl_client *client, struct wl_resource *res_tzpol, uint32_t id, struct wl_resource *surf)
246 {
247    tzpol_surface_t *tsurf;
248    pepper_surface_t *psurf;
249    struct wl_resource *new_res;
250    int ver;
251
252    if (!surf)
253      {
254         wl_resource_post_error(res_tzpol, WL_DISPLAY_ERROR_INVALID_OBJECT,
255                                "tizen_policy::visibility_get requires surface");
256         return;
257      }
258
259    psurf = (pepper_surface_t*)wl_resource_get_user_data(surf);
260    if (!psurf)
261      {
262         DALI_LOG_INFO( gPepperTizenPolicyLogging, Debug::General, "failed to get pepper_surface from wl_resource" );
263         wl_resource_post_error(res_tzpol, WL_DISPLAY_ERROR_INVALID_OBJECT,
264                                "tizen_policy::visibility_get invalid surface resource");
265         return;
266      }
267
268    DALI_LOG_INFO( gPepperTizenPolicyLogging, Debug::General, "tizen_policy::visibility_get" );
269    tsurf = _tzpol_surface_get(psurf);
270    if (!tsurf)
271      {
272         wl_resource_post_no_memory(res_tzpol);
273         return;
274      }
275
276    ver = wl_resource_get_version(res_tzpol);
277    new_res = wl_resource_create(client, &tizen_position_interface, ver, id);
278    if (!new_res)
279      {
280         wl_resource_post_no_memory(res_tzpol);
281         return;
282      }
283
284    wl_resource_set_implementation(new_res, &_tzpos_iface, tsurf, _tzpos_res_cb_destroy);
285    _tzpol_surface_ref(tsurf, new_res);
286 }
287
288 // --------------------------------------------------------
289 // stack: activate, raise, lower
290 // --------------------------------------------------------
291 static void
292 _tzpol_iface_cb_activate(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf)
293 {
294    (void)client;
295    (void)res_tzpol;
296    (void)surf;
297 }
298
299 static void
300 _tzpol_iface_cb_activate_below_by_res_id(struct wl_client *client, struct wl_resource *res_tzpol,  uint32_t res_id, uint32_t below_res_id)
301 {
302    (void)client;
303    (void)res_tzpol;
304    (void)res_id;
305    (void)below_res_id;
306 }
307
308 static void
309 _tzpol_iface_cb_raise(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf)
310 {
311    (void)client;
312    (void)res_tzpol;
313    (void)surf;
314 }
315
316 static void
317 _tzpol_iface_cb_lower(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf)
318 {
319    (void)client;
320    (void)res_tzpol;
321    (void)surf;
322 }
323
324 static void
325 _tzpol_iface_cb_lower_by_res_id(struct wl_client *client, struct wl_resource *res_tzpol,  uint32_t res_id)
326 {
327    (void)client;
328    (void)res_tzpol;
329    (void)res_id;
330 }
331
332 // --------------------------------------------------------
333 // focus
334 // --------------------------------------------------------
335 static void
336 _tzpol_iface_cb_focus_skip_set(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf)
337 {
338    (void)client;
339    (void)res_tzpol;
340    (void)surf;
341 }
342
343 static void
344 _tzpol_iface_cb_focus_skip_unset(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf)
345 {
346    (void)client;
347    (void)res_tzpol;
348    (void)surf;
349 }
350
351 // --------------------------------------------------------
352 // role
353 // --------------------------------------------------------
354 static void
355 _tzpol_iface_cb_role_set(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf, const char *role)
356 {
357    (void)client;
358    (void)res_tzpol;
359    (void)surf;
360    (void)role;
361 }
362
363 static void
364 _tzpol_iface_cb_type_set(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf, uint32_t type)
365 {
366    (void)client;
367    (void)res_tzpol;
368    (void)surf;
369    (void)type;
370 }
371
372 // --------------------------------------------------------
373 // conformant
374 // --------------------------------------------------------
375 static void
376 _tzpol_iface_cb_conformant_set(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf)
377 {
378    (void)client;
379    (void)res_tzpol;
380    (void)surf;
381 }
382
383 static void
384 _tzpol_iface_cb_conformant_unset(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf)
385 {
386    (void)client;
387    (void)res_tzpol;
388    (void)surf;
389 }
390
391 static void
392 _tzpol_iface_cb_conformant_get(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf)
393 {
394    (void)client;
395    (void)res_tzpol;
396    (void)surf;
397 }
398
399 // --------------------------------------------------------
400 // notification level
401 // --------------------------------------------------------
402 static void
403 _tzpol_iface_cb_notilv_set(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf, int32_t lv)
404 {
405    (void)client;
406    (void)res_tzpol;
407    (void)surf;
408    (void)lv;
409 }
410
411 // --------------------------------------------------------
412 // transient for
413 // --------------------------------------------------------
414 static void
415 _tzpol_iface_cb_transient_for_set(struct wl_client *client, struct wl_resource *res_tzpol, uint32_t child_id, uint32_t parent_id)
416 {
417    (void)client;
418    (void)res_tzpol;
419    (void)child_id;
420    (void)parent_id;
421 }
422
423 static void
424 _tzpol_iface_cb_transient_for_unset(struct wl_client *client, struct wl_resource *res_tzpol, uint32_t child_id)
425 {
426    (void)client;
427    (void)res_tzpol;
428    (void)child_id;
429 }
430
431 // --------------------------------------------------------
432 // window screen mode
433 // --------------------------------------------------------
434 static void
435 _tzpol_iface_cb_win_scrmode_set(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf, uint32_t mode)
436 {
437    (void)client;
438    (void)res_tzpol;
439    (void)surf;
440    (void)mode;
441 }
442
443 // --------------------------------------------------------
444 // subsurface
445 // --------------------------------------------------------
446 static void
447 _tzpol_iface_cb_subsurf_place_below_parent(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *subsurf)
448 {
449    (void)client;
450    (void)res_tzpol;
451    (void)subsurf;
452 }
453
454 static void
455 _tzpol_iface_cb_subsurface_get(struct wl_client *client, struct wl_resource *res_tzpol, uint32_t id, struct wl_resource *surface, uint32_t parent_id)
456 {
457    (void)client;
458    (void)res_tzpol;
459    (void)id;
460    (void)surface;
461    (void)parent_id;
462 }
463
464 static void
465 _tzpol_iface_cb_opaque_state_set(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surface, int32_t state)
466 {
467    (void)client;
468    (void)res_tzpol;
469    (void)surface;
470    (void)state;
471 }
472
473 // --------------------------------------------------------
474 // iconify
475 // --------------------------------------------------------
476 static void
477 _tzpol_iface_cb_iconify(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf)
478 {
479    (void)client;
480    (void)res_tzpol;
481    (void)surf;
482 }
483
484 static void
485 _tzpol_iface_cb_uniconify(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf)
486 {
487    (void)client;
488    (void)res_tzpol;
489    (void)surf;
490 }
491
492 static void
493 _tzpol_iface_cb_aux_hint_add(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf, int32_t id, const char *name, const char *value)
494 {
495    (void)client;
496    (void)res_tzpol;
497    (void)surf;
498    (void)id;
499    (void)name;
500    (void)value;
501 }
502
503 static void
504 _tzpol_iface_cb_aux_hint_change(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf, int32_t id, const char *value)
505 {
506    (void)client;
507    (void)res_tzpol;
508    (void)surf;
509    (void)id;
510    (void)value;
511 }
512
513 static void
514 _tzpol_iface_cb_aux_hint_del(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf, int32_t id)
515 {
516    (void)client;
517    (void)res_tzpol;
518    (void)surf;
519    (void)id;
520 }
521
522 static void
523 _tzpol_iface_cb_supported_aux_hints_get(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf)
524 {
525    (void)client;
526    (void)res_tzpol;
527    (void)surf;
528 }
529
530 static void
531 _tzpol_iface_cb_background_state_set(struct wl_client *client, struct wl_resource *res_tzpol, uint32_t pid)
532 {
533    (void)client;
534    (void)res_tzpol;
535    (void)pid;
536 }
537
538 static void
539 _tzpol_iface_cb_background_state_unset(struct wl_client *client, struct wl_resource *res_tzpol, uint32_t pid)
540 {
541    (void)client;
542    (void)res_tzpol;
543    (void)pid;
544 }
545
546 static void
547 _tzpol_iface_cb_floating_mode_set(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf)
548 {
549    (void)client;
550    (void)res_tzpol;
551    (void)surf;
552 }
553
554 static void
555 _tzpol_iface_cb_floating_mode_unset(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf)
556 {
557    (void)client;
558    (void)res_tzpol;
559    (void)surf;
560 }
561
562 static void
563 _tzpol_iface_cb_stack_mode_set(struct wl_client *client, struct wl_resource *res_tzpol, struct wl_resource *surf, uint32_t mode)
564 {
565    (void)client;
566    (void)res_tzpol;
567    (void)surf;
568    (void)mode;
569 }
570
571 static const struct tizen_policy_interface _tzpol_iface =
572 {
573    _tzpol_iface_cb_vis_get,
574    _tzpol_iface_cb_pos_get,
575    _tzpol_iface_cb_activate,
576    _tzpol_iface_cb_activate_below_by_res_id,
577    _tzpol_iface_cb_raise,
578    _tzpol_iface_cb_lower,
579    _tzpol_iface_cb_lower_by_res_id,
580    _tzpol_iface_cb_focus_skip_set,
581    _tzpol_iface_cb_focus_skip_unset,
582    _tzpol_iface_cb_role_set,
583    _tzpol_iface_cb_type_set,
584    _tzpol_iface_cb_conformant_set,
585    _tzpol_iface_cb_conformant_unset,
586    _tzpol_iface_cb_conformant_get,
587    _tzpol_iface_cb_notilv_set,
588    _tzpol_iface_cb_transient_for_set,
589    _tzpol_iface_cb_transient_for_unset,
590    _tzpol_iface_cb_win_scrmode_set,
591    _tzpol_iface_cb_subsurf_place_below_parent,
592    _tzpol_iface_cb_subsurface_get,
593    _tzpol_iface_cb_opaque_state_set,
594    _tzpol_iface_cb_iconify,
595    _tzpol_iface_cb_uniconify,
596    _tzpol_iface_cb_aux_hint_add,
597    _tzpol_iface_cb_aux_hint_change,
598    _tzpol_iface_cb_aux_hint_del,
599    _tzpol_iface_cb_supported_aux_hints_get,
600    _tzpol_iface_cb_background_state_set,
601    _tzpol_iface_cb_background_state_unset,
602    _tzpol_iface_cb_floating_mode_set,
603    _tzpol_iface_cb_floating_mode_unset,
604    _tzpol_iface_cb_stack_mode_set,
605 };
606
607 static void
608 _tzpol_cb_bind(struct wl_client *client, void *data, uint32_t ver, uint32_t id)
609 {
610    struct wl_resource *resource;
611
612    DALI_LOG_INFO( gPepperTizenPolicyLogging, Debug::General, "tizen_policy::bind" );
613
614    resource = wl_resource_create(client, &tizen_policy_interface, ver, id);
615    if (!resource)
616      {
617         DALI_LOG_INFO( gPepperTizenPolicyLogging, Debug::General, "failed to create resource for tizen_policy" );
618         wl_client_post_no_memory(client);
619         return;
620      }
621
622    wl_resource_set_implementation(resource, &_tzpol_iface, NULL, NULL);
623
624    wl_list_insert(&_tzpol_res_list, wl_resource_get_link(resource));
625 }
626
627 bool
628 TizenPolicyInit(pepper_compositor_t *comp)
629 {
630    struct wl_display *wl_disp;
631
632    DALI_LOG_INFO( gPepperTizenPolicyLogging, Debug::General, "tizen_policy::init" );
633
634    if (_tzpol_global)
635      goto end;
636
637    wl_list_init(&_tzpol_res_list);
638
639    wl_disp = pepper_compositor_get_display(comp);
640    _tzpol_global = wl_global_create(wl_disp, &tizen_policy_interface, 1, NULL, _tzpol_cb_bind);
641    if (!_tzpol_global)
642      {
643         DALI_LOG_INFO( gPepperTizenPolicyLogging, Debug::General, "failed to create global for tizen policy" );
644         return false;
645      }
646 end:
647    return true;
648 }
649
650 void
651 TizenPolicyShutdown(void)
652 {
653    struct wl_resource *res, *tmp;
654
655    DALI_LOG_INFO( gPepperTizenPolicyLogging, Debug::General, "tizen_policy::shutdown" );
656
657    wl_resource_for_each_safe(res, tmp, &_tzpol_res_list)
658      {
659         wl_list_remove(wl_resource_get_link(res));
660         wl_resource_destroy(res);
661      }
662
663    wl_global_destroy(_tzpol_global);
664    _tzpol_global = NULL;
665 }
666
667 bool
668 TizenPolicySetVisibility( pepper_surface_t* psurf, VisibilityState state )
669 {
670    tzpol_surface_t *tsurf;
671    struct wl_resource *resource;
672    int res;
673
674    tsurf = (tzpol_surface_t*)pepper_object_get_user_data((pepper_object_t *)psurf, (const void *)tsurf_data_key);
675    if (!tsurf)
676      {
677         DALI_LOG_INFO( gPepperTizenPolicyLogging, Debug::General, "there is no data for 'tzpol_surface_t' in 'pepper_surface_t'" );
678         return false;
679      }
680
681    if (tsurf->visibility == state)
682      return true;
683
684    tsurf->visibility = state;
685    wl_resource_for_each(resource, &tsurf->resource_list)
686      {
687         res = wl_resource_instance_of(resource, &tizen_visibility_interface, &_tzvis_iface);
688         if (!res)
689           continue;
690         tizen_visibility_send_notify(resource, state);
691      }
692
693    return true;
694 }
695
696 } // namespace Extension
697
698 } // namespace Internal
699
700 } // namespace Pepper
701
702 } // namespace Dali