implement tizen_policy
[platform/core/uifw/libds-tizen.git] / src / policy / policy.c
1 #include <assert.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <wayland-server.h>
5 #include <tizen-extension-server-protocol.h>
6 #include <libds/log.h>
7
8 #include "util.h"
9 #include "security.h"
10 #include "libds-tizen/policy.h"
11
12 #define TIZEN_POLICY_VERSION 11
13 #define TIZEN_POLICY_PRIVILEGE_SET_NOTIFICATION_LEVEL \
14         "http://tizen.org/privilege/window.priority.set"
15 #define TIZEN_POLICY_PRIVILEGE_SET_SCREEN_MODE \
16         "http://tizen.org/privilege/display"
17
18 struct ds_tizen_policy
19 {
20     struct wl_global *global;
21
22     struct wl_list clients;
23
24     struct wl_listener destroy;
25
26     bool use_security;
27
28     struct {
29         struct wl_signal destroy;
30         struct wl_signal get_surface;
31         struct wl_signal activate_below_by_univeral_id;
32         struct wl_signal lower_by_universal_id;
33         struct wl_signal set_transient_for;
34         struct wl_signal unset_transient_for;
35         struct wl_signal place_subsurface_below_parent;
36         struct wl_signal set_subsurface_stand_alone;
37         struct wl_signal set_background_state;
38         struct wl_signal unset_background_state;
39         struct wl_signal activate_above_by_universal_id;
40         struct wl_signal set_appid;
41         struct wl_signal set_transient_for_below;
42     } events;
43 };
44
45 struct ds_tizen_policy_client
46 {
47     struct ds_tizen_policy *policy;
48
49     struct wl_resource *resource;
50     struct wl_client *wl_client;
51     pid_t pid;
52     uid_t uid;
53
54     struct wl_list policy_surfaces;
55
56     struct wl_list link; // ds_tizen_policy::clients
57 };
58
59 struct ds_tizen_policy_surface
60 {
61     struct ds_tizen_policy_client *client;
62
63     struct ds_surface *surface;
64     pid_t pid;
65     uid_t uid;
66
67     struct wl_list visibilities;
68     struct wl_list positions;
69     struct wl_list subsurface_watchers;
70
71     bool conformant;
72     int32_t opaque_state;
73     bool iconified;
74     bool floating_mode;
75     enum ds_tizen_policy_stack_mode stack_mode;
76     bool video;
77
78     struct {
79         struct wl_signal destroy;
80         struct wl_signal get_visibility;
81         struct wl_signal get_position;
82         struct wl_signal activate;
83         struct wl_signal raise;
84         struct wl_signal lower;
85         struct wl_signal set_focus_skip;
86         struct wl_signal unset_focus_skip;
87         struct wl_signal set_role;
88         struct wl_signal set_window_type;
89         struct wl_signal set_conformant;
90         struct wl_signal unset_conformant;
91         struct wl_signal get_conformant;
92         struct wl_signal set_notification_level;
93         struct wl_signal set_window_screen_mode;
94         struct wl_signal get_subsurface;
95         struct wl_signal iconify;
96         struct wl_signal uniconify;
97         struct wl_signal add_aux_hint;
98         struct wl_signal change_aux_hint;
99         struct wl_signal delete_aux_hint;
100         struct wl_signal get_supported_aux_hints;
101         struct wl_signal set_floating_mode;
102         struct wl_signal unset_floating_mode;
103         struct wl_signal set_stack_mode;
104         struct wl_signal get_subsurface_watcher;
105         struct wl_signal set_parent;
106         struct wl_signal ack_conformant_region;
107         struct wl_signal set_video;
108         struct wl_signal show;
109         struct wl_signal hide;
110         struct wl_signal set_parent_with_below;
111     } events;
112
113     struct wl_list link; // ds_tizen_policy_client::policy_surfaces
114 };
115
116 struct ds_tizen_policy_visibility
117 {
118     struct ds_tizen_policy_surface *policy_surface;
119
120     struct wl_resource *resource;
121
122     struct {
123         struct wl_signal destroy;
124     } events;
125
126     struct wl_list link; // ds_tizen_policy_surface::visibilities
127 };
128
129 struct ds_tizen_policy_position
130 {
131     struct ds_tizen_policy_surface *policy_surface;
132
133     struct wl_resource *resource;
134
135     struct {
136         struct wl_signal destroy;
137         struct wl_signal set;
138     } events;
139
140     struct wl_list link; // ds_tizen_policy_surface::positions
141 };
142
143 struct ds_tizen_policy_subsurface_watcher
144 {
145     struct ds_tizen_policy_surface *policy_surface;
146
147     struct wl_resource *resource;
148
149     struct {
150         struct wl_signal destroy;
151     } events;
152
153     struct wl_list link; // ds_tizen_policy_surface::subsurface_watchers
154 };
155
156 static void policy_handle_display_destroy(struct wl_listener *listener,
157         void *data);
158
159 static void policy_bind(struct wl_client *wl_client, void *data,
160         uint32_t version, uint32_t id);
161
162 static struct ds_tizen_policy_surface *tizen_policy_client_find_policy_surface(
163     struct ds_tizen_policy_client *client,
164     struct ds_surface *surface);
165
166 static struct ds_tizen_policy_surface *tizen_policy_client_get_surface(
167     struct wl_resource *resource,
168     struct wl_resource *surface_resource);
169
170 WL_EXPORT struct ds_tizen_policy *
171 ds_tizen_policy_create(struct wl_display *display)
172 {
173     struct ds_tizen_policy *policy;
174
175     policy = calloc(1, sizeof *policy);
176     if (!policy) {
177         ds_err("calloc() failed.");
178         return NULL;
179     }
180
181     policy->global = wl_global_create(display, &tizen_policy_interface,
182             TIZEN_POLICY_VERSION, policy, policy_bind);
183     if (!policy->global) {
184         ds_err("wl_global_create() failed. tizen_policy_interface");
185         free(policy);
186         return NULL;
187     }
188
189     wl_list_init(&policy->clients);
190
191     policy->destroy.notify = policy_handle_display_destroy;
192     wl_display_add_destroy_listener(display, &policy->destroy);
193
194     policy->use_security = tizen_security_init();
195     if (!policy->use_security) {
196         ds_inf("tizen_security_init() is not successful. "
197                 "policy works without security.");
198     }
199
200     wl_signal_init(&policy->events.destroy);
201     wl_signal_init(&policy->events.get_surface);
202     wl_signal_init(&policy->events.activate_below_by_univeral_id);
203     wl_signal_init(&policy->events.lower_by_universal_id);
204     wl_signal_init(&policy->events.set_transient_for);
205     wl_signal_init(&policy->events.unset_transient_for);
206     wl_signal_init(&policy->events.place_subsurface_below_parent);
207     wl_signal_init(&policy->events.set_subsurface_stand_alone);
208     wl_signal_init(&policy->events.set_background_state);
209     wl_signal_init(&policy->events.unset_background_state);
210     wl_signal_init(&policy->events.activate_above_by_universal_id);
211     wl_signal_init(&policy->events.set_appid);
212     wl_signal_init(&policy->events.set_transient_for_below);
213
214     ds_inf("Global created: tizen_policy(%p)", policy);
215
216     return policy;
217 }
218
219 WL_EXPORT void
220 ds_tizen_policy_add_destroy_listener(
221         struct ds_tizen_policy *policy,
222         struct wl_listener *listener)
223 {
224     wl_signal_add(&policy->events.destroy, listener);
225 }
226
227 WL_EXPORT void
228 ds_tizen_policy_add_get_surface_listener(
229         struct ds_tizen_policy *policy,
230         struct wl_listener *listener)
231 {
232     wl_signal_add(&policy->events.get_surface, listener);
233 }
234
235 WL_EXPORT void
236 ds_tizen_policy_add_activate_below_by_univeral_id_listener(
237         struct ds_tizen_policy *policy,
238         struct wl_listener *listener)
239 {
240     wl_signal_add(&policy->events.activate_below_by_univeral_id, listener);
241 }
242
243 WL_EXPORT void
244 ds_tizen_policy_add_lower_by_universal_id_listener(
245         struct ds_tizen_policy *policy,
246         struct wl_listener *listener)
247 {
248     wl_signal_add(&policy->events.lower_by_universal_id, listener);
249 }
250
251 WL_EXPORT void
252 ds_tizen_policy_add_set_transient_for_listener(
253         struct ds_tizen_policy *policy,
254         struct wl_listener *listener)
255 {
256     wl_signal_add(&policy->events.set_transient_for, listener);
257 }
258
259 WL_EXPORT void
260 ds_tizen_policy_add_unset_transient_for_listener(
261         struct ds_tizen_policy *policy,
262         struct wl_listener *listener)
263 {
264     wl_signal_add(&policy->events.unset_transient_for, listener);
265 }
266
267 WL_EXPORT void
268 ds_tizen_policy_add_place_subsurface_below_parent_listener(
269         struct ds_tizen_policy *policy,
270         struct wl_listener *listener)
271 {
272     wl_signal_add(&policy->events.place_subsurface_below_parent, listener);
273 }
274
275 WL_EXPORT void
276 ds_tizen_policy_add_set_subsurface_stand_alone_listener(
277         struct ds_tizen_policy *policy,
278         struct wl_listener *listener)
279 {
280     wl_signal_add(&policy->events.set_subsurface_stand_alone, listener);
281 }
282
283 WL_EXPORT void
284 ds_tizen_policy_add_set_background_state_listener(
285         struct ds_tizen_policy *policy,
286         struct wl_listener *listener)
287 {
288     wl_signal_add(&policy->events.set_background_state, listener);
289 }
290
291 WL_EXPORT void
292 ds_tizen_policy_add_unset_background_state_listener(
293         struct ds_tizen_policy *policy,
294         struct wl_listener *listener)
295 {
296     wl_signal_add(&policy->events.unset_background_state, listener);
297 }
298
299 WL_EXPORT void
300 ds_tizen_policy_add_activate_above_by_universal_id_listener(
301         struct ds_tizen_policy *policy,
302         struct wl_listener *listener)
303 {
304     wl_signal_add(&policy->events.activate_above_by_universal_id, listener);
305 }
306
307 WL_EXPORT void
308 ds_tizen_policy_add_set_appid_listener(
309         struct ds_tizen_policy *policy,
310         struct wl_listener *listener)
311 {
312     wl_signal_add(&policy->events.set_appid, listener);
313 }
314
315 WL_EXPORT void
316 ds_tizen_policy_add_set_transient_for_below_listener(
317         struct ds_tizen_policy *policy,
318         struct wl_listener *listener)
319 {
320     wl_signal_add(&policy->events.set_transient_for_below, listener);
321 }
322
323 WL_EXPORT void
324 ds_tizen_policy_surface_add_destroy_listener(
325         struct ds_tizen_policy_surface *policy_surface,
326         struct wl_listener *listener)
327 {
328     wl_signal_add(&policy_surface->events.destroy, listener);
329 }
330
331 WL_EXPORT void
332 ds_tizen_policy_surface_add_get_visibility_listener(
333         struct ds_tizen_policy_surface *policy_surface,
334         struct wl_listener *listener)
335 {
336     wl_signal_add(&policy_surface->events.get_visibility, listener);
337 }
338
339 WL_EXPORT void
340 ds_tizen_policy_surface_add_get_position_listener(
341         struct ds_tizen_policy_surface *policy_surface,
342         struct wl_listener *listener)
343 {
344     wl_signal_add(&policy_surface->events.get_position, listener);
345 }
346
347 WL_EXPORT void
348 ds_tizen_policy_surface_add_activate_listener(
349         struct ds_tizen_policy_surface *policy_surface,
350         struct wl_listener *listener)
351 {
352     wl_signal_add(&policy_surface->events.activate, listener);
353 }
354
355 WL_EXPORT void
356 ds_tizen_policy_surface_add_raise_listener(
357         struct ds_tizen_policy_surface *policy_surface,
358         struct wl_listener *listener)
359 {
360     wl_signal_add(&policy_surface->events.raise, listener);
361 }
362
363 WL_EXPORT void
364 ds_tizen_policy_surface_add_lower_listener(
365         struct ds_tizen_policy_surface *policy_surface,
366         struct wl_listener *listener)
367 {
368     wl_signal_add(&policy_surface->events.lower, listener);
369 }
370
371 WL_EXPORT void
372 ds_tizen_policy_surface_add_set_focus_skip_listener(
373         struct ds_tizen_policy_surface *policy_surface,
374         struct wl_listener *listener)
375 {
376     wl_signal_add(&policy_surface->events.set_focus_skip, listener);
377 }
378
379 WL_EXPORT void
380 ds_tizen_policy_surface_add_unset_focus_skip_listener(
381         struct ds_tizen_policy_surface *policy_surface,
382         struct wl_listener *listener)
383 {
384     wl_signal_add(&policy_surface->events.unset_focus_skip, listener);
385 }
386
387 WL_EXPORT void
388 ds_tizen_policy_surface_add_set_role_listener(
389         struct ds_tizen_policy_surface *policy_surface,
390         struct wl_listener *listener)
391 {
392     wl_signal_add(&policy_surface->events.set_role, listener);
393 }
394
395 WL_EXPORT void
396 ds_tizen_policy_surface_add_set_window_type_listener(
397         struct ds_tizen_policy_surface *policy_surface,
398         struct wl_listener *listener)
399 {
400     wl_signal_add(&policy_surface->events.set_window_type, listener);
401 }
402
403 WL_EXPORT void
404 ds_tizen_policy_surface_add_set_conformant_listener(
405         struct ds_tizen_policy_surface *policy_surface,
406         struct wl_listener *listener)
407 {
408     wl_signal_add(&policy_surface->events.set_conformant, listener);
409 }
410
411 WL_EXPORT void
412 ds_tizen_policy_surface_add_unset_conformant_listener(
413         struct ds_tizen_policy_surface *policy_surface,
414         struct wl_listener *listener)
415 {
416     wl_signal_add(&policy_surface->events.unset_conformant, listener);
417 }
418
419 WL_EXPORT void
420 ds_tizen_policy_surface_add_get_conformant_listener(
421         struct ds_tizen_policy_surface *policy_surface,
422         struct wl_listener *listener)
423 {
424     wl_signal_add(&policy_surface->events.get_conformant, listener);
425 }
426
427 WL_EXPORT void
428 ds_tizen_policy_surface_add_set_notification_level_listener(
429         struct ds_tizen_policy_surface *policy_surface,
430         struct wl_listener *listener)
431 {
432     wl_signal_add(&policy_surface->events.set_notification_level, listener);
433 }
434
435 WL_EXPORT void
436 ds_tizen_policy_surface_add_set_window_screen_mode_listener(
437         struct ds_tizen_policy_surface *policy_surface,
438         struct wl_listener *listener)
439 {
440     wl_signal_add(&policy_surface->events.set_window_screen_mode, listener);
441 }
442
443 WL_EXPORT void
444 ds_tizen_policy_surface_add_get_subsurface_listener(
445         struct ds_tizen_policy_surface *policy_surface,
446         struct wl_listener *listener)
447 {
448     wl_signal_add(&policy_surface->events.get_subsurface, listener);
449 }
450
451 WL_EXPORT void
452 ds_tizen_policy_surface_add_iconify_listener(
453         struct ds_tizen_policy_surface *policy_surface,
454         struct wl_listener *listener)
455 {
456     wl_signal_add(&policy_surface->events.iconify, listener);
457 }
458
459 WL_EXPORT void
460 ds_tizen_policy_surface_add_uniconify_listener(
461         struct ds_tizen_policy_surface *policy_surface,
462         struct wl_listener *listener)
463 {
464     wl_signal_add(&policy_surface->events.uniconify, listener);
465 }
466
467 WL_EXPORT void
468 ds_tizen_policy_surface_add_add_aux_hint_listener(
469         struct ds_tizen_policy_surface *policy_surface,
470         struct wl_listener *listener)
471 {
472     wl_signal_add(&policy_surface->events.add_aux_hint, listener);
473 }
474
475 WL_EXPORT void
476 ds_tizen_policy_surface_add_change_aux_hint_listener(
477         struct ds_tizen_policy_surface *policy_surface,
478         struct wl_listener *listener)
479 {
480     wl_signal_add(&policy_surface->events.change_aux_hint, listener);
481 }
482
483 WL_EXPORT void
484 ds_tizen_policy_surface_add_delete_aux_hint_listener(
485         struct ds_tizen_policy_surface *policy_surface,
486         struct wl_listener *listener)
487 {
488     wl_signal_add(&policy_surface->events.delete_aux_hint, listener);
489 }
490
491 WL_EXPORT void
492 ds_tizen_policy_surface_add_get_supported_aux_hints_listener(
493         struct ds_tizen_policy_surface *policy_surface,
494         struct wl_listener *listener)
495 {
496     wl_signal_add(&policy_surface->events.get_supported_aux_hints, listener);
497 }
498
499 WL_EXPORT void
500 ds_tizen_policy_surface_add_set_floating_mode_listener(
501         struct ds_tizen_policy_surface *policy_surface,
502         struct wl_listener *listener)
503 {
504     wl_signal_add(&policy_surface->events.set_floating_mode, listener);
505 }
506
507 WL_EXPORT void
508 ds_tizen_policy_surface_add_unset_floating_mode_listener(
509         struct ds_tizen_policy_surface *policy_surface,
510         struct wl_listener *listener)
511 {
512     wl_signal_add(&policy_surface->events.unset_floating_mode, listener);
513 }
514
515 WL_EXPORT void
516 ds_tizen_policy_surface_add_set_stack_mode_listener(
517         struct ds_tizen_policy_surface *policy_surface,
518         struct wl_listener *listener)
519 {
520     wl_signal_add(&policy_surface->events.set_stack_mode, listener);
521 }
522
523 WL_EXPORT void
524 ds_tizen_policy_surface_add_get_subsurface_watcher_listener(
525         struct ds_tizen_policy_surface *policy_surface,
526         struct wl_listener *listener)
527 {
528     wl_signal_add(&policy_surface->events.get_subsurface_watcher, listener);
529 }
530
531 WL_EXPORT void
532 ds_tizen_policy_surface_add_set_parent_listener(
533         struct ds_tizen_policy_surface *policy_surface,
534         struct wl_listener *listener)
535 {
536     wl_signal_add(&policy_surface->events.set_parent, listener);
537 }
538
539 WL_EXPORT void
540 ds_tizen_policy_surface_add_ack_conformant_region_listener(
541         struct ds_tizen_policy_surface *policy_surface,
542         struct wl_listener *listener)
543 {
544     wl_signal_add(&policy_surface->events.ack_conformant_region, listener);
545 }
546
547 WL_EXPORT void
548 ds_tizen_policy_surface_add_set_video_listener(
549         struct ds_tizen_policy_surface *policy_surface,
550         struct wl_listener *listener)
551 {
552     wl_signal_add(&policy_surface->events.set_video, listener);
553 }
554
555 WL_EXPORT void
556 ds_tizen_policy_surface_add_show_listener(
557         struct ds_tizen_policy_surface *policy_surface,
558         struct wl_listener *listener)
559 {
560     wl_signal_add(&policy_surface->events.show, listener);
561 }
562
563 WL_EXPORT void
564 ds_tizen_policy_surface_add_hide_listener(
565         struct ds_tizen_policy_surface *policy_surface,
566         struct wl_listener *listener)
567 {
568     wl_signal_add(&policy_surface->events.hide, listener);
569 }
570
571 WL_EXPORT void
572 ds_tizen_policy_surface_add_set_parent_with_below_listener(
573         struct ds_tizen_policy_surface *policy_surface,
574         struct wl_listener *listener)
575 {
576     wl_signal_add(&policy_surface->events.set_parent_with_below, listener);
577 }
578
579 WL_EXPORT void
580 ds_tizen_policy_visibility_add_destroy_listener(
581         struct ds_tizen_policy_visibility *visibility,
582         struct wl_listener *listener)
583 {
584     wl_signal_add(&visibility->events.destroy, listener);
585 }
586
587 WL_EXPORT void
588 ds_tizen_policy_position_add_destroy_listener(
589         struct ds_tizen_policy_position *position,
590         struct wl_listener *listener)
591 {
592     wl_signal_add(&position->events.destroy, listener);
593 }
594
595 WL_EXPORT void
596 ds_tizen_policy_position_add_set_listener(
597         struct ds_tizen_policy_position *position,
598         struct wl_listener *listener)
599 {
600     wl_signal_add(&position->events.set, listener);
601 }
602
603 void
604 ds_tizen_policy_subsurface_watcher_add_destroy_listener(
605         struct ds_tizen_policy_subsurface_watcher *subsurface_watcher,
606         struct wl_listener *listener)
607 {
608     wl_signal_add(&subsurface_watcher->events.destroy, listener);
609 }
610
611 WL_EXPORT void
612 ds_tizen_policy_surface_send_conformant(struct ds_tizen_policy_surface *policy_surface,
613     bool active)
614 {
615    tizen_policy_send_conformant(policy_surface->client->resource,
616     ds_surface_get_wl_resource(policy_surface->surface), active);
617 }
618
619 WL_EXPORT void
620 ds_tizen_policy_surface_send_conformant_area(struct ds_tizen_policy_surface *policy_surface,
621     enum ds_tizen_policy_conformant_part part, bool visible,
622     int32_t x, int32_t y, int32_t w, int32_t h)
623 {
624     uint32_t conformant_part;
625
626     switch (part) {
627         case DS_TIZEN_POLICY_CONFORMANT_PART_INDICATOR:
628             conformant_part = TIZEN_POLICY_CONFORMANT_PART_INDICATOR;
629             break;
630         case DS_TIZEN_POLICY_CONFORMANT_PART_KEYBOARD:
631             conformant_part = TIZEN_POLICY_CONFORMANT_PART_KEYBOARD;
632             break;
633         case DS_TIZEN_POLICY_CONFORMANT_PART_CLIPBOARD:
634             conformant_part = TIZEN_POLICY_CONFORMANT_PART_CLIPBOARD;
635             break;
636         default:
637             ds_err("Not supported conformant_part(%d)", part);
638             return;
639     }
640
641     tizen_policy_send_conformant_area(policy_surface->client->resource,
642         ds_surface_get_wl_resource(policy_surface->surface), conformant_part, visible,
643         x, y, w, h);
644 }
645
646 WL_EXPORT void
647 ds_tizen_policy_surface_send_notification_done(struct ds_tizen_policy_surface *policy_surface,
648     enum ds_tizen_policy_notification_level level,
649     enum ds_tizen_policy_error_state state)
650 {
651     uint32_t notification_level;
652     uint32_t error_state;
653
654     switch (level) {
655         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_1:
656             notification_level = TIZEN_POLICY_LEVEL_1;
657             break;
658         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_2:
659             notification_level = TIZEN_POLICY_LEVEL_2;
660             break;
661         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_3:
662             notification_level = TIZEN_POLICY_LEVEL_3;
663             break;
664         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_NONE:
665             notification_level = TIZEN_POLICY_LEVEL_NONE;
666             break;
667         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_DEFAULT:
668             notification_level = TIZEN_POLICY_LEVEL_DEFAULT;
669             break;
670         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_MEDIUM:
671             notification_level = TIZEN_POLICY_LEVEL_MEDIUM;
672             break;
673         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_HIGH:
674             notification_level = TIZEN_POLICY_LEVEL_HIGH;
675             break;
676         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_TOP:
677             notification_level = TIZEN_POLICY_LEVEL_TOP;
678             break;
679         default:
680             ds_err("Not supported notification_level(%d)", level);
681             return;
682     }
683
684     switch (state) {
685         case DS_TIZEN_POLICY_ERROR_STATE_NONE:
686             error_state = TIZEN_POLICY_ERROR_STATE_NONE;
687             break;
688         case DS_TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED:
689             error_state = TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED;
690             break;
691         default:
692             ds_err("Not supported error_state(%d)", state);
693             return;
694     }
695
696     tizen_policy_send_notification_done(policy_surface->client->resource,
697         ds_surface_get_wl_resource(policy_surface->surface),
698         notification_level, error_state);
699 }
700
701 WL_EXPORT void
702 ds_tizen_policy_surface_send_window_screen_mode_done(struct ds_tizen_policy_surface *policy_surface,
703     enum ds_tizen_policy_window_screen_mode mode,
704     enum ds_tizen_policy_error_state state)
705 {
706     uint32_t window_screen_mode;
707     uint32_t error_state;
708
709     switch (mode) {
710         case DS_TIZEN_POLICY_WINDOW_SCREEN_MODE_DEFAULT:
711             window_screen_mode = TIZEN_POLICY_MODE_DEFAULT;
712             break;
713         case DS_TIZEN_POLICY_WINDOW_SCREEN_MODE_ALWAYS_ON:
714             window_screen_mode = TIZEN_POLICY_MODE_ALWAYS_ON;
715             break;
716         default:
717             ds_err("Not supported window_screen_mode(%d)", mode);
718             return;
719     }
720
721     switch (state) {
722         case DS_TIZEN_POLICY_ERROR_STATE_NONE:
723             error_state = TIZEN_POLICY_ERROR_STATE_NONE;
724             break;
725         case DS_TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED:
726             error_state = TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED;
727             break;
728         default:
729             ds_err("Not supported error_state(%d)", state);
730             return;
731     }
732
733     tizen_policy_send_window_screen_mode_done(policy_surface->client->resource,
734         ds_surface_get_wl_resource(policy_surface->surface),
735         window_screen_mode, error_state);
736 }
737
738 WL_EXPORT void
739 ds_tizen_policy_surface_send_iconify_state_changed(struct ds_tizen_policy_surface *policy_surface,
740     bool iconified, bool force)
741 {
742     tizen_policy_send_iconify_state_changed(policy_surface->client->resource,
743         ds_surface_get_wl_resource(policy_surface->surface), iconified, force);
744 }
745
746 WL_EXPORT void
747 ds_tizen_policy_surface_send_supported_aux_hints(struct ds_tizen_policy_surface *policy_surface,
748     struct wl_array *hints, uint32_t force)
749 {
750     // TODO:
751 }
752
753 WL_EXPORT void
754 ds_tizen_policy_surface_send_allowed_aux_hint(struct ds_tizen_policy_surface *policy_surface,
755     int32_t hint_id)
756 {
757     // TODO:
758 }
759
760 WL_EXPORT void
761 ds_tizen_policy_surface_send_aux_message(struct ds_tizen_policy_surface *policy_surface,
762     const char *key, const char *value, struct wl_array *options)
763 {
764     // TODO:
765 }
766
767 WL_EXPORT void
768 ds_tizen_policy_surface_send_conformant_region(struct ds_tizen_policy_surface *policy_surface,
769     enum ds_tizen_policy_conformant_part part, bool visible,
770     int32_t x, int32_t y, int32_t w, int32_t h, uint32_t serial)
771 {
772     uint32_t conformant_part;
773
774     switch (part) {
775         case DS_TIZEN_POLICY_CONFORMANT_PART_INDICATOR:
776             conformant_part = TIZEN_POLICY_CONFORMANT_PART_INDICATOR;
777             break;
778         case DS_TIZEN_POLICY_CONFORMANT_PART_KEYBOARD:
779             conformant_part = TIZEN_POLICY_CONFORMANT_PART_KEYBOARD;
780             break;
781         case DS_TIZEN_POLICY_CONFORMANT_PART_CLIPBOARD:
782             conformant_part = TIZEN_POLICY_CONFORMANT_PART_CLIPBOARD;
783             break;
784         default:
785             ds_err("Not supported conformant_part(%d)", part);
786             return;
787     }
788
789     tizen_policy_send_conformant_region(policy_surface->client->resource,
790         ds_surface_get_wl_resource(policy_surface->surface), conformant_part, visible,
791         x, y, w, h, serial);
792 }
793
794 WL_EXPORT void
795 ds_tizen_policy_surface_send_interactive_move_done(struct ds_tizen_policy_surface *policy_surface,
796     int32_t x, int32_t y, uint32_t w, uint32_t h)
797 {
798     // TODO:
799 }
800
801 WL_EXPORT void
802 ds_tizen_policy_surface_send_interactive_resize_done(struct ds_tizen_policy_surface *policy_surface,
803     int32_t x, int32_t y, uint32_t w, uint32_t h)
804 {
805         // TODO:
806 }
807
808 static int32_t
809 tizen_policy_visibility_get_type(enum ds_tizen_policy_visibility_type type)
810 {
811     uint32_t vis_type;
812
813     switch (type) {
814         case DS_TIZEN_POLICY_VISIBILITY_TYPE_UNOBSCURED:
815             vis_type = TIZEN_VISIBILITY_VISIBILITY_UNOBSCURED;
816             break;
817         case DS_TIZEN_POLICY_VISIBILITY_TYPE_PARTIALLY_OBSCURED:
818             vis_type = TIZEN_VISIBILITY_VISIBILITY_PARTIALLY_OBSCURED;
819             break;
820         case DS_TIZEN_POLICY_VISIBILITY_TYPE_FULLY_OBSCURED:
821             vis_type = TIZEN_VISIBILITY_VISIBILITY_FULLY_OBSCURED;
822             break;
823         case DS_TIZEN_POLICY_VISIBILITY_TYPE_PRE_UNOBSCURED:
824             vis_type = TIZEN_VISIBILITY_VISIBILITY_PRE_UNOBSCURED;
825             break;
826         default:
827             ds_err("Not supported visible type (%d)", type);
828             vis_type = DS_TIZEN_POLICY_VISIBILITY_TYPE_UNKNOWN;
829             break;
830     }
831
832     return vis_type;
833 }
834
835 WL_EXPORT void
836 ds_tizen_policy_visibility_send_notify(
837     struct ds_tizen_policy_visibility *visibility,
838     enum ds_tizen_policy_visibility_type type)
839 {
840     uint32_t vis_type;
841
842     vis_type = tizen_policy_visibility_get_type(type);
843     if (vis_type == DS_TIZEN_POLICY_VISIBILITY_TYPE_UNKNOWN)
844         return;
845
846     tizen_visibility_send_notify(visibility->resource, vis_type);
847 }
848
849 WL_EXPORT void
850 ds_tizen_policy_visibility_send_changed(
851     struct ds_tizen_policy_visibility *visibility,
852     enum ds_tizen_policy_visibility_type type, uint32_t option)
853 {
854     uint32_t vis_type;
855
856     vis_type = tizen_policy_visibility_get_type(type);
857     if (vis_type == DS_TIZEN_POLICY_VISIBILITY_TYPE_UNKNOWN)
858         return;
859
860     tizen_visibility_send_changed(visibility->resource, vis_type, option);
861 }
862
863 WL_EXPORT void
864 ds_tizen_policy_position_send_changed(
865     struct ds_tizen_policy_position *position, int32_t x, int32_t y)
866 {
867     tizen_position_send_changed(position->resource, x, y);
868 }
869
870 WL_EXPORT struct ds_surface *
871 ds_tizen_policy_surface_get_surface(struct ds_tizen_policy_surface *policy_surface)
872 {
873     return policy_surface->surface;
874 }
875
876 WL_EXPORT bool
877 ds_tizen_policy_surface_get_conformant(struct ds_tizen_policy_surface *policy_surface)
878 {
879     return policy_surface->conformant;
880 }
881
882 WL_EXPORT int32_t
883 ds_tizen_policy_surface_get_opaque_state(struct ds_tizen_policy_surface *policy_surface)
884 {
885     return policy_surface->opaque_state;
886 }
887
888 WL_EXPORT bool
889 ds_tizen_policy_surface_get_iconified(struct ds_tizen_policy_surface *policy_surface)
890 {
891     return policy_surface->iconified;
892 }
893
894 WL_EXPORT bool
895 ds_tizen_policy_surface_get_floating_mode(struct ds_tizen_policy_surface *policy_surface)
896 {
897     return policy_surface->floating_mode;
898 }
899
900 WL_EXPORT enum ds_tizen_policy_stack_mode
901 ds_tizen_policy_surface_get_stack_mode(struct ds_tizen_policy_surface *policy_surface)
902 {
903     return policy_surface->stack_mode;
904 }
905
906 static struct ds_tizen_policy_surface *
907 tizen_policy_client_find_policy_surface(struct ds_tizen_policy_client *client,
908     struct ds_surface *surface)
909 {
910     struct ds_tizen_policy_surface *policy_surface;
911
912     wl_list_for_each(policy_surface, &client->policy_surfaces, link) {
913         if (surface == policy_surface->surface)
914             return policy_surface;
915     }
916
917     return NULL;
918 }
919
920 static struct ds_tizen_policy_surface *
921 tizen_policy_client_get_surface(struct wl_resource *resource,
922     struct wl_resource *surface_resource)
923 {
924     struct ds_tizen_policy_client *client;
925     struct ds_tizen_policy_surface *policy_surface;
926     struct ds_surface *surface;
927
928     client = wl_resource_get_user_data(resource);
929     surface = ds_surface_from_resource(surface_resource);
930
931     policy_surface = tizen_policy_client_find_policy_surface(client, surface);
932     if (policy_surface)
933         return policy_surface;
934
935     policy_surface = calloc(1, sizeof *policy_surface);
936     if (policy_surface == NULL) {
937         ds_err("calloc() failed. tizen_policy");
938         return NULL;
939     }
940
941     policy_surface->client = client;
942     policy_surface->surface = surface;
943     wl_client_get_credentials(client->wl_client, &policy_surface->pid, &policy_surface->uid, NULL);
944
945     wl_list_init(&policy_surface->visibilities);
946     wl_list_init(&policy_surface->positions);
947     wl_list_init(&policy_surface->subsurface_watchers);
948
949     wl_signal_init(&policy_surface->events.destroy);
950     wl_signal_init(&policy_surface->events.get_visibility);
951     wl_signal_init(&policy_surface->events.get_position);
952     wl_signal_init(&policy_surface->events.activate);
953     wl_signal_init(&policy_surface->events.raise);
954     wl_signal_init(&policy_surface->events.lower);
955     wl_signal_init(&policy_surface->events.set_focus_skip);
956     wl_signal_init(&policy_surface->events.unset_focus_skip);
957     wl_signal_init(&policy_surface->events.set_role);
958     wl_signal_init(&policy_surface->events.set_window_type);
959     wl_signal_init(&policy_surface->events.set_conformant);
960     wl_signal_init(&policy_surface->events.unset_conformant);
961     wl_signal_init(&policy_surface->events.get_conformant);
962     wl_signal_init(&policy_surface->events.set_notification_level);
963     wl_signal_init(&policy_surface->events.set_window_screen_mode);
964     wl_signal_init(&policy_surface->events.get_subsurface);
965     wl_signal_init(&policy_surface->events.iconify);
966     wl_signal_init(&policy_surface->events.uniconify);
967     wl_signal_init(&policy_surface->events.add_aux_hint);
968     wl_signal_init(&policy_surface->events.change_aux_hint);
969     wl_signal_init(&policy_surface->events.delete_aux_hint);
970     wl_signal_init(&policy_surface->events.get_supported_aux_hints);
971     wl_signal_init(&policy_surface->events.set_floating_mode);
972     wl_signal_init(&policy_surface->events.unset_floating_mode);
973     wl_signal_init(&policy_surface->events.set_stack_mode);
974     wl_signal_init(&policy_surface->events.get_subsurface_watcher);
975     wl_signal_init(&policy_surface->events.set_parent);
976     wl_signal_init(&policy_surface->events.ack_conformant_region);
977     wl_signal_init(&policy_surface->events.set_video);
978     wl_signal_init(&policy_surface->events.show);
979     wl_signal_init(&policy_surface->events.hide);
980     wl_signal_init(&policy_surface->events.set_parent_with_below);
981
982     wl_list_insert(&client->policy_surfaces, &policy_surface->link);
983
984     struct ds_tizen_event_policy_get_surface event = {
985         .policy = client->policy,
986         .policy_surface = policy_surface,
987     };
988     wl_signal_emit(&client->policy->events.get_surface, &event);
989
990     return policy_surface;
991 }
992
993 static void
994 policy_handle_display_destroy(struct wl_listener *listener, void *data)
995 {
996     struct ds_tizen_policy *policy;
997
998     policy = wl_container_of(listener, policy, destroy);
999
1000     ds_inf("Global destroy: policy(%p)", policy);
1001
1002     wl_signal_emit(&policy->events.destroy, policy);
1003
1004     if (policy->use_security)
1005         tizen_security_finish();
1006
1007     wl_list_remove(&policy->destroy.link);
1008     wl_global_destroy(policy->global);
1009     free(policy);
1010 }
1011
1012 static void
1013 visibility_handle_destroy(struct wl_client *wl_client,
1014     struct wl_resource *resource)
1015 {
1016    wl_resource_destroy(resource);
1017 }
1018
1019 static const struct tizen_visibility_interface visibility_impl =
1020 {
1021    visibility_handle_destroy,
1022 };
1023
1024 static void
1025 _tizen_policy_visibility_handle_destroy(struct wl_resource *resource)
1026 {
1027     struct ds_tizen_policy_visibility *visibility;
1028
1029     visibility = wl_resource_get_user_data(resource);
1030
1031     ds_inf("_tizen_policy_visibility_handle_destroy (visibility:%p)",
1032         visibility);
1033
1034     wl_signal_emit(&visibility->events.destroy, visibility);
1035     wl_list_remove(&visibility->link);
1036     free(visibility);
1037 }
1038
1039 static void
1040 policy_handle_get_visibility(struct wl_client *wl_client, struct wl_resource *resource,
1041     uint32_t id, struct wl_resource *surface_resource)
1042 {
1043     struct ds_tizen_policy_surface *policy_surface;
1044     struct ds_tizen_policy_visibility *visibility;
1045
1046     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1047     if (policy_surface == NULL) {
1048         ds_err("tizen_policy_client_get_surface() failed.");
1049         wl_client_post_no_memory(wl_client);
1050         return;
1051     }
1052
1053     visibility = calloc(1, sizeof *visibility);
1054     if (visibility == NULL) {
1055         ds_err("calloc() failed. tizen_policy");
1056         return;
1057     }
1058
1059     visibility->policy_surface = policy_surface;
1060     wl_signal_init(&visibility->events.destroy);
1061
1062     wl_list_insert(&policy_surface->visibilities, &visibility->link);
1063
1064     visibility->resource = wl_resource_create(wl_client,
1065         &tizen_visibility_interface, wl_resource_get_version(resource),
1066         id);
1067     if (visibility->resource == NULL) {
1068         ds_err("tizen_policy : wl_resource_create() failed.");
1069         wl_list_remove(&visibility->link);
1070         free(visibility);
1071         wl_client_post_no_memory(wl_client);
1072         return;
1073     }
1074
1075     wl_resource_set_implementation(visibility->resource,
1076         &visibility_impl, visibility,
1077         _tizen_policy_visibility_handle_destroy);
1078
1079     struct ds_tizen_event_policy_surface_get_visibility event = {
1080         .policy_surface = policy_surface,
1081         .visibility = visibility,
1082     };
1083     wl_signal_emit(&policy_surface->events.get_visibility, &event);
1084 }
1085
1086 static void
1087 position_handle_destroy(struct wl_client *wl_client, struct wl_resource *resource)
1088 {
1089    wl_resource_destroy(resource);
1090 }
1091
1092 static void
1093 position_handle_set(struct wl_client *wl_client, struct wl_resource *resource,
1094     int32_t x, int32_t y)
1095 {
1096     struct ds_tizen_policy_position *position;
1097
1098     position = wl_resource_get_user_data(resource);
1099
1100     struct ds_tizen_event_policy_position_set event = {
1101         .position = position,
1102         .x = x,
1103         .y = y,
1104     };
1105     wl_signal_emit(&position->events.set, &event);
1106 }
1107
1108 static const struct tizen_position_interface position_impl =
1109 {
1110    position_handle_destroy,
1111    position_handle_set,
1112 };
1113
1114 static void
1115 _tizen_policy_position_handle_destroy(struct wl_resource *resource)
1116 {
1117     struct ds_tizen_policy_position *position;
1118
1119     position = wl_resource_get_user_data(resource);
1120
1121     ds_inf("_tizen_policy_position_handle_destroy (position:%p)",
1122         position);
1123
1124     wl_signal_emit(&position->events.destroy, position);
1125     wl_list_remove(&position->link);
1126     free(position);
1127 }
1128
1129 static void
1130 policy_handle_get_position(struct wl_client *wl_client, struct wl_resource *resource,
1131     uint32_t id, struct wl_resource *surface_resource)
1132 {
1133     struct ds_tizen_policy_surface *policy_surface;
1134     struct ds_tizen_policy_position *position;
1135
1136     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1137     if (policy_surface == NULL) {
1138         ds_err("tizen_policy_client_get_surface() failed.");
1139         wl_client_post_no_memory(wl_client);
1140         return;
1141     }
1142
1143     position = calloc(1, sizeof *position);
1144     if (position == NULL) {
1145         ds_err("calloc() failed. tizen_policy");
1146         return;
1147     }
1148
1149     position->policy_surface = policy_surface;
1150     wl_signal_init(&position->events.destroy);
1151     wl_signal_init(&position->events.set);
1152
1153     wl_list_insert(&policy_surface->positions, &position->link);
1154
1155     position->resource = wl_resource_create(wl_client,
1156         &tizen_position_interface, wl_resource_get_version(resource),
1157         id);
1158     if (position->resource == NULL) {
1159         ds_err("tizen_policy : wl_resource_create() failed.");
1160         wl_list_remove(&position->link);
1161         free(position);
1162         wl_client_post_no_memory(wl_client);
1163         return;
1164     }
1165
1166     wl_resource_set_implementation(position->resource,
1167         &position_impl, position,
1168         _tizen_policy_position_handle_destroy);
1169
1170     struct ds_tizen_event_policy_surface_get_position event = {
1171         .policy_surface = policy_surface,
1172         .position = position,
1173     };
1174     wl_signal_emit(&policy_surface->events.get_position, &event);
1175 }
1176
1177 static void
1178 policy_handle_activate(struct wl_client *wl_client,
1179     struct wl_resource *resource, struct wl_resource *surface_resource)
1180 {
1181     struct ds_tizen_policy_surface *policy_surface;
1182
1183     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1184     if (policy_surface == NULL) {
1185         ds_err("tizen_policy_client_get_surface() failed.");
1186         wl_client_post_no_memory(wl_client);
1187         return;
1188     }
1189
1190     struct ds_tizen_event_policy_surface_activate event = {
1191         .policy_surface = policy_surface,
1192     };
1193     wl_signal_emit(&policy_surface->events.activate, &event);
1194 }
1195
1196 static void
1197 policy_handle_activate_below_by_res_id(struct wl_client *wl_client,
1198     struct wl_resource *resource,  uint32_t universal_id,
1199     uint32_t below_universal_id)
1200 {
1201     struct ds_tizen_policy_client *client;
1202
1203     client = wl_resource_get_user_data(resource);
1204
1205     struct ds_tizen_event_policy_activate_below_by_univeral_id event = {
1206         .policy = client->policy,
1207         .universal_id = universal_id,
1208         .below_universal_id = below_universal_id,
1209     };
1210     wl_signal_emit(&client->policy->events.activate_below_by_univeral_id, &event);
1211 }
1212
1213 static void
1214 policy_handle_raise(struct wl_client *wl_client, struct wl_resource *resource,
1215     struct wl_resource *surface_resource)
1216 {
1217     struct ds_tizen_policy_surface *policy_surface;
1218
1219     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1220     if (policy_surface == NULL) {
1221         ds_err("tizen_policy_client_get_surface() failed.");
1222         wl_client_post_no_memory(wl_client);
1223         return;
1224     }
1225
1226     struct ds_tizen_event_policy_surface_raise event = {
1227         .policy_surface = policy_surface,
1228     };
1229     wl_signal_emit(&policy_surface->events.raise, &event);
1230 }
1231
1232 static void
1233 policy_handle_lower(struct wl_client *wl_client, struct wl_resource *resource,
1234     struct wl_resource *surface_resource)
1235 {
1236     struct ds_tizen_policy_surface *policy_surface;
1237
1238     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1239     if (policy_surface == NULL) {
1240         ds_err("tizen_policy_client_get_surface() failed.");
1241         wl_client_post_no_memory(wl_client);
1242         return;
1243     }
1244
1245     struct ds_tizen_event_policy_surface_lower event = {
1246         .policy_surface = policy_surface,
1247     };
1248     wl_signal_emit(&policy_surface->events.lower, &event);
1249 }
1250
1251 static void
1252 policy_handle_lower_by_res_id(struct wl_client *wl_client,
1253     struct wl_resource *resource,  uint32_t universal_id)
1254 {
1255     struct ds_tizen_policy_client *client;
1256
1257     client = wl_resource_get_user_data(resource);
1258
1259     struct ds_tizen_event_policy_lower_by_universal_id event = {
1260         .policy = client->policy,
1261         .universal_id = universal_id,
1262     };
1263     wl_signal_emit(&client->policy->events.lower_by_universal_id, &event);
1264 }
1265
1266 static void
1267 policy_handle_set_focus_skip(struct wl_client *wl_client,
1268     struct wl_resource *resource, struct wl_resource *surface_resource)
1269 {
1270     struct ds_tizen_policy_surface *policy_surface;
1271
1272     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1273     if (policy_surface == NULL) {
1274         ds_err("tizen_policy_client_get_surface() failed.");
1275         wl_client_post_no_memory(wl_client);
1276         return;
1277     }
1278
1279     struct ds_tizen_event_policy_surface_set_focus_skip event = {
1280         .policy_surface = policy_surface,
1281     };
1282     wl_signal_emit(&policy_surface->events.set_focus_skip, &event);
1283 }
1284
1285 static void
1286 policy_handle_unset_focus_skip(struct wl_client *wl_client,
1287     struct wl_resource *resource, struct wl_resource *surface_resource)
1288 {
1289     struct ds_tizen_policy_surface *policy_surface;
1290
1291     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1292     if (policy_surface == NULL) {
1293         ds_err("tizen_policy_client_get_surface() failed.");
1294         wl_client_post_no_memory(wl_client);
1295         return;
1296     }
1297
1298     struct ds_tizen_event_policy_surface_unset_focus_skip event = {
1299         .policy_surface = policy_surface,
1300     };
1301     wl_signal_emit(&policy_surface->events.unset_focus_skip, &event);
1302 }
1303
1304 static void
1305 policy_handle_set_role(struct wl_client *wl_client, struct wl_resource *resource,
1306     struct wl_resource *surface_resource, const char *role)
1307 {
1308     struct ds_tizen_policy_surface *policy_surface;
1309
1310     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1311     if (policy_surface == NULL) {
1312         ds_err("tizen_policy_client_get_surface() failed.");
1313         wl_client_post_no_memory(wl_client);
1314         return;
1315     }
1316
1317     struct ds_tizen_event_policy_surface_set_role event = {
1318         .policy_surface = policy_surface,
1319         .role = role,
1320     };
1321     wl_signal_emit(&policy_surface->events.set_role, &event);
1322 }
1323
1324 static void
1325 policy_handle_set_type(struct wl_client *wl_client, struct wl_resource *resource,
1326     struct wl_resource *surface_resource, uint32_t type)
1327 {
1328     struct ds_tizen_policy_surface *policy_surface;
1329     enum ds_tizen_policy_window_type win_type;
1330
1331     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1332     if (policy_surface == NULL) {
1333         ds_err("tizen_policy_client_get_surface() failed.");
1334         wl_client_post_no_memory(wl_client);
1335         return;
1336     }
1337
1338     switch (type) {
1339         case TIZEN_POLICY_WIN_TYPE_NONE:
1340             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_NONE;
1341             break;
1342         case TIZEN_POLICY_WIN_TYPE_TOPLEVEL:
1343             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_TOPLEVEL;
1344             break;
1345         case TIZEN_POLICY_WIN_TYPE_FULLSCREEN:
1346             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_FULLLSCREEN;
1347             break;
1348         case TIZEN_POLICY_WIN_TYPE_MAXIMIZED:
1349             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_MAXIMIZED;
1350             break;
1351         case TIZEN_POLICY_WIN_TYPE_TRANSIENT:
1352             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_TRANSIENT;
1353             break;
1354         case TIZEN_POLICY_WIN_TYPE_MENU:
1355             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_MENU;
1356             break;
1357         case TIZEN_POLICY_WIN_TYPE_DND:
1358             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_DND;
1359             break;
1360         case TIZEN_POLICY_WIN_TYPE_CUSTOM:
1361             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_CUSTOM;
1362             break;
1363         case TIZEN_POLICY_WIN_TYPE_NOTIFICATION:
1364             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_NOTIFICATION;
1365             break;
1366         case TIZEN_POLICY_WIN_TYPE_UTILITY:
1367             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_UTILITY;
1368             break;
1369         case TIZEN_POLICY_WIN_TYPE_DIALOG:
1370             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_DIALOG;
1371             break;
1372         case TIZEN_POLICY_WIN_TYPE_DOCK:
1373             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_DOCK;
1374             break;
1375         case TIZEN_POLICY_WIN_TYPE_SPLASH:
1376             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_SPLASH;
1377             break;
1378         case TIZEN_POLICY_WIN_TYPE_DESKTOP:
1379             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_DESKTOP;
1380             break;
1381         default:
1382             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_NONE;
1383             break;
1384     }
1385
1386     struct ds_tizen_event_policy_surface_set_window_type event = {
1387         .policy_surface = policy_surface,
1388         .win_type = win_type,
1389     };
1390     wl_signal_emit(&policy_surface->events.set_window_type, &event);
1391 }
1392
1393 static void
1394 policy_handle_set_conformant(struct wl_client *wl_client,
1395     struct wl_resource *resource, struct wl_resource *surface_resource)
1396 {
1397     struct ds_tizen_policy_surface *policy_surface;
1398
1399     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1400     if (policy_surface == NULL) {
1401         ds_err("tizen_policy_client_get_surface() failed.");
1402         wl_client_post_no_memory(wl_client);
1403         return;
1404     }
1405
1406     if (!policy_surface->conformant)
1407         policy_surface->conformant = true;
1408
1409     struct ds_tizen_event_policy_surface_set_conformant event = {
1410         .policy_surface = policy_surface,
1411     };
1412     wl_signal_emit(&policy_surface->events.set_conformant, &event);
1413 }
1414
1415 static void
1416 policy_handle_unset_conformant(struct wl_client *wl_client,
1417     struct wl_resource *resource, struct wl_resource *surface_resource)
1418 {
1419     struct ds_tizen_policy_surface *policy_surface;
1420
1421     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1422     if (policy_surface == NULL) {
1423         ds_err("tizen_policy_client_get_surface() failed.");
1424         wl_client_post_no_memory(wl_client);
1425         return;
1426     }
1427
1428     if (policy_surface->conformant)
1429         policy_surface->conformant = false;
1430
1431     struct ds_tizen_event_policy_surface_unset_conformant event = {
1432         .policy_surface = policy_surface,
1433     };
1434     wl_signal_emit(&policy_surface->events.unset_conformant, &event);
1435 }
1436
1437 static void
1438 policy_handle_get_conformant(struct wl_client *wl_client,
1439     struct wl_resource *resource, struct wl_resource *surface_resource)
1440 {
1441     struct ds_tizen_policy_surface *policy_surface;
1442
1443     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1444     if (policy_surface == NULL) {
1445         ds_err("tizen_policy_client_get_surface() failed.");
1446         wl_client_post_no_memory(wl_client);
1447         return;
1448     }
1449
1450     struct ds_tizen_event_policy_surface_get_conformant event = {
1451         .policy_surface = policy_surface,
1452     };
1453     wl_signal_emit(&policy_surface->events.get_conformant, &event);
1454 }
1455
1456 static void
1457 policy_handle_set_notification_level(struct wl_client *wl_client,
1458     struct wl_resource *resource, struct wl_resource *surface_resource,
1459     int32_t level)
1460 {
1461     struct ds_tizen_policy_surface *policy_surface;
1462     enum ds_tizen_policy_notification_level noti_level;
1463     bool ret;
1464
1465     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1466     if (policy_surface == NULL) {
1467         ds_err("tizen_policy_client_get_surface() failed.");
1468         wl_client_post_no_memory(wl_client);
1469         return;
1470     }
1471
1472     ret = tizen_security_check_privilege(policy_surface->pid, policy_surface->uid,
1473             TIZEN_POLICY_PRIVILEGE_SET_NOTIFICATION_LEVEL);
1474     if (ret == false) {
1475         ds_err("tizen_security_check_privilege() failed. "
1476             "Privilege Denied on set_notification_level.");
1477
1478         tizen_policy_send_notification_done(resource, surface_resource,
1479             -1, TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED);
1480
1481         return;
1482     }
1483
1484     switch (level) {
1485         case TIZEN_POLICY_LEVEL_1:
1486             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_1;
1487             break;
1488         case TIZEN_POLICY_LEVEL_2:
1489             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_2;
1490             break;
1491         case TIZEN_POLICY_LEVEL_3:
1492             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_3;
1493             break;
1494         case TIZEN_POLICY_LEVEL_NONE:
1495             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_NONE;
1496             break;
1497         case TIZEN_POLICY_LEVEL_DEFAULT:
1498             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_DEFAULT;
1499             break;
1500         case TIZEN_POLICY_LEVEL_MEDIUM:
1501             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_MEDIUM;
1502             break;
1503         case TIZEN_POLICY_LEVEL_HIGH:
1504             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_HIGH;
1505             break;
1506         case TIZEN_POLICY_LEVEL_TOP:
1507             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_TOP;
1508             break;
1509         default:
1510             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_NONE;
1511             break;
1512     }
1513
1514     struct ds_tizen_event_policy_surface_set_notification_level event = {
1515         .policy_surface = policy_surface,
1516         .level = noti_level,
1517     };
1518     wl_signal_emit(&policy_surface->events.set_notification_level, &event);
1519 }
1520
1521 static void
1522 policy_handle_set_transient_for(struct wl_client *wl_client,
1523     struct wl_resource *resource, uint32_t child_id, uint32_t parent_id)
1524 {
1525     struct ds_tizen_policy_client *client;
1526
1527     client = wl_resource_get_user_data(resource);
1528
1529     struct ds_tizen_event_policy_set_transient_for event = {
1530         .policy = client->policy,
1531         .child_universal_id = child_id,
1532         .parent_universal_id = parent_id,
1533     };
1534     wl_signal_emit(&client->policy->events.set_transient_for, &event);
1535
1536     tizen_policy_send_transient_for_done(resource, child_id);
1537 }
1538
1539 static void
1540 policy_handle_unset_transient_for(struct wl_client *wl_client,
1541     struct wl_resource *resource, uint32_t child_id)
1542 {
1543     struct ds_tizen_policy_client *client;
1544
1545     client = wl_resource_get_user_data(resource);
1546
1547     struct ds_tizen_event_policy_unset_transient_for event = {
1548         .policy = client->policy,
1549         .child_universal_id = child_id,
1550     };
1551     wl_signal_emit(&client->policy->events.unset_transient_for, &event);
1552 }
1553
1554 static void
1555 policy_handle_set_window_screen_mode(struct wl_client *wl_client,
1556     struct wl_resource *resource, struct wl_resource *surface_resource,
1557     uint32_t mode)
1558 {
1559     struct ds_tizen_policy_surface *policy_surface;
1560     enum ds_tizen_policy_window_screen_mode screen_mode;
1561     bool ret;
1562
1563     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1564     if (policy_surface == NULL) {
1565         ds_err("tizen_policy_client_get_surface() failed.");
1566         wl_client_post_no_memory(wl_client);
1567         return;
1568     }
1569
1570     ret = tizen_security_check_privilege(policy_surface->pid, policy_surface->uid,
1571             TIZEN_POLICY_PRIVILEGE_SET_SCREEN_MODE);
1572     if (ret == false) {
1573         ds_err("tizen_security_check_privilege() failed. "
1574             "Privilege Denied on set_window_screen_mode.");
1575
1576         tizen_policy_send_notification_done(resource, surface_resource,
1577             -1, TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED);
1578
1579         return;
1580     }
1581
1582     switch (mode) {
1583         case TIZEN_POLICY_MODE_DEFAULT:
1584             screen_mode = DS_TIZEN_POLICY_WINDOW_SCREEN_MODE_DEFAULT;
1585             break;
1586         case TIZEN_POLICY_MODE_ALWAYS_ON:
1587             screen_mode = DS_TIZEN_POLICY_WINDOW_SCREEN_MODE_ALWAYS_ON;
1588             break;
1589         default:
1590             screen_mode = DS_TIZEN_POLICY_WINDOW_SCREEN_MODE_DEFAULT;
1591             break;
1592     }
1593
1594     struct ds_tizen_event_policy_surface_set_window_screen_mode event = {
1595         .policy_surface = policy_surface,
1596         .mode = screen_mode,
1597     };
1598     wl_signal_emit(&policy_surface->events.set_window_screen_mode, &event);
1599 }
1600
1601 static void
1602 policy_handle_place_subsurface_below_parent(struct wl_client *wl_client,
1603     struct wl_resource *resource, struct wl_resource *subsurface_resource)
1604 {
1605     struct ds_tizen_policy_client *client;
1606     struct ds_subsurface *subsurface;
1607
1608     client = wl_resource_get_user_data(resource);
1609     subsurface = ds_subsurface_from_resource(subsurface_resource);
1610
1611     struct ds_tizen_event_policy_place_subsurface_below_parent event = {
1612         .policy = client->policy,
1613         .subsurface = subsurface,
1614     };
1615     wl_signal_emit(
1616         &client->policy->events.place_subsurface_below_parent,
1617         &event);
1618 }
1619
1620 static void
1621 policy_handle_set_subsurface_stand_alone(struct wl_client *wl_client,
1622     struct wl_resource *resource, struct wl_resource *subsurface_resource)
1623 {
1624     struct ds_tizen_policy_client *client;
1625     struct ds_subsurface *subsurface;
1626
1627     client = wl_resource_get_user_data(resource);
1628     subsurface = ds_subsurface_from_resource(subsurface_resource);
1629
1630     struct ds_tizen_event_policy_set_subsurface_stand_alone event = {
1631         .policy = client->policy,
1632         .subsurface = subsurface,
1633     };
1634     wl_signal_emit(
1635         &client->policy->events.set_subsurface_stand_alone,
1636         &event);
1637 }
1638
1639 static void
1640 policy_handle_get_subsurface(struct wl_client *wl_client,
1641     struct wl_resource *resource, uint32_t id,
1642     struct wl_resource *surface_resource, uint32_t parent_id)
1643 {
1644     struct ds_tizen_policy_surface *policy_surface;
1645
1646     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1647     if (policy_surface == NULL) {
1648         ds_err("tizen_policy_client_get_surface() failed.");
1649         wl_client_post_no_memory(wl_client);
1650         return;
1651     }
1652
1653     // TODO: How to create the ds_subsurface with a parent_universal_id.
1654
1655     struct ds_tizen_event_policy_surface_get_subsurface event = {
1656         .policy_surface = policy_surface,
1657         .parent_universal_id = parent_id,
1658     };
1659     wl_signal_emit(&policy_surface->events.get_subsurface, &event);
1660 }
1661
1662 static void
1663 policy_handle_set_opaque_state(struct wl_client *wl_client,
1664     struct wl_resource *resource, struct wl_resource *surface_resource,
1665     int32_t state)
1666 {
1667     struct ds_tizen_policy_surface *policy_surface;
1668
1669     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1670     if (policy_surface == NULL) {
1671         ds_err("tizen_policy_client_get_surface() failed.");
1672         wl_client_post_no_memory(wl_client);
1673         return;
1674     }
1675
1676     if (policy_surface->opaque_state != state)
1677         policy_surface->opaque_state = state;
1678 }
1679
1680 static void
1681 policy_handle_iconify(struct wl_client *wl_client, struct wl_resource *resource,
1682     struct wl_resource *surface_resource)
1683 {
1684     struct ds_tizen_policy_surface *policy_surface;
1685
1686     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1687     if (policy_surface == NULL) {
1688         ds_err("tizen_policy_client_get_surface() failed.");
1689         wl_client_post_no_memory(wl_client);
1690         return;
1691     }
1692
1693     if (!policy_surface->iconified)
1694         policy_surface->iconified = true;
1695
1696     struct ds_tizen_event_policy_surface_iconify event = {
1697         .policy_surface = policy_surface,
1698     };
1699     wl_signal_emit(&policy_surface->events.iconify, &event);
1700 }
1701
1702 static void
1703 policy_handle_uniconify(struct wl_client *wl_client, struct wl_resource *resource,
1704     struct wl_resource *surface_resource)
1705 {
1706     struct ds_tizen_policy_surface *policy_surface;
1707
1708     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1709     if (policy_surface == NULL) {
1710         ds_err("tizen_policy_client_get_surface() failed.");
1711         wl_client_post_no_memory(wl_client);
1712         return;
1713     }
1714
1715     if (policy_surface->iconified)
1716         policy_surface->iconified = false;
1717
1718     struct ds_tizen_event_policy_surface_uniconify event = {
1719         .policy_surface = policy_surface,
1720     };
1721     wl_signal_emit(&policy_surface->events.uniconify, &event);
1722 }
1723
1724 static void
1725 policy_handle_add_aux_hint(struct wl_client *wl_client,
1726     struct wl_resource *resource, struct wl_resource *surface_resource,
1727     int32_t id, const char *name, const char *value)
1728 {
1729     struct ds_tizen_policy_surface *policy_surface;
1730
1731     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1732     if (policy_surface == NULL) {
1733         ds_err("tizen_policy_client_get_surface() failed.");
1734         wl_client_post_no_memory(wl_client);
1735         return;
1736     }
1737
1738     struct ds_tizen_event_policy_surface_add_aux_hint event = {
1739         .policy_surface = policy_surface,
1740         .id = id,
1741         .name = name,
1742         .value = value,
1743     };
1744     wl_signal_emit(&policy_surface->events.add_aux_hint, &event);
1745 }
1746
1747 static void
1748 policy_handle_change_aux_hint(struct wl_client *wl_client,
1749     struct wl_resource *resource, struct wl_resource *surface_resource,
1750     int32_t id, const char *value)
1751 {
1752     struct ds_tizen_policy_surface *policy_surface;
1753
1754     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1755     if (policy_surface == NULL) {
1756         ds_err("tizen_policy_client_get_surface() failed.");
1757         wl_client_post_no_memory(wl_client);
1758         return;
1759     }
1760
1761     struct ds_tizen_event_policy_surface_change_aux_hint event = {
1762         .policy_surface = policy_surface,
1763         .id = id,
1764         .value = value,
1765     };
1766     wl_signal_emit(&policy_surface->events.change_aux_hint, &event);
1767 }
1768
1769 static void
1770 policy_handle_delete_aux_hint(struct wl_client *wl_client,
1771     struct wl_resource *resource, struct wl_resource *surface_resource,
1772     int32_t id)
1773 {
1774     struct ds_tizen_policy_surface *policy_surface;
1775
1776     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1777     if (policy_surface == NULL) {
1778         ds_err("tizen_policy_client_get_surface() failed.");
1779         wl_client_post_no_memory(wl_client);
1780         return;
1781     }
1782
1783     struct ds_tizen_event_policy_surface_delete_aux_hint event = {
1784         .policy_surface = policy_surface,
1785         .id = id,
1786     };
1787     wl_signal_emit(&policy_surface->events.delete_aux_hint, &event);
1788 }
1789
1790 static void
1791 policy_handle_get_supported_aux_hints(struct wl_client *wl_client,
1792     struct wl_resource *resource, struct wl_resource *surface_resource)
1793 {
1794     struct ds_tizen_policy_surface *policy_surface;
1795
1796     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1797     if (policy_surface == NULL) {
1798         ds_err("tizen_policy_client_get_surface() failed.");
1799         wl_client_post_no_memory(wl_client);
1800         return;
1801     }
1802
1803     struct ds_tizen_event_policy_surface_get_supported_aux_hints event = {
1804         .policy_surface = policy_surface,
1805     };
1806     wl_signal_emit(&policy_surface->events.get_supported_aux_hints, &event);
1807 }
1808
1809 static void
1810 policy_handle_set_background_state(struct wl_client *wl_client,
1811     struct wl_resource *resource, uint32_t pid)
1812 {
1813     struct ds_tizen_policy_client *client;
1814     struct ds_tizen_policy *policy;
1815
1816     client = wl_resource_get_user_data(resource);
1817     policy = client->policy;
1818
1819     wl_list_for_each(client, &policy->clients, link) {
1820         if (pid == client->pid) {
1821             struct ds_tizen_event_policy_set_background_state event = {
1822                 .policy = client->policy,
1823                 .pid = pid,
1824             };
1825             wl_signal_emit(&client->policy->events.set_background_state, &event);
1826         }
1827     }
1828 }
1829
1830 static void
1831 policy_handle_unset_background_state(struct wl_client *wl_client,
1832     struct wl_resource *resource, uint32_t pid)
1833 {
1834     struct ds_tizen_policy_client *client;
1835     struct ds_tizen_policy *policy;
1836
1837     client = wl_resource_get_user_data(resource);
1838     policy = client->policy;
1839
1840     wl_list_for_each(client, &policy->clients, link) {
1841         if (pid == client->pid) {
1842             struct ds_tizen_event_policy_unset_background_state event = {
1843                 .policy = client->policy,
1844                 .pid = pid,
1845             };
1846             wl_signal_emit(&client->policy->events.unset_background_state, &event);
1847         }
1848     }
1849 }
1850
1851 static void
1852 policy_handle_set_floating_mode(struct wl_client *wl_client,
1853     struct wl_resource *resource, struct wl_resource *surface_resource)
1854 {
1855     struct ds_tizen_policy_surface *policy_surface;
1856
1857     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1858     if (policy_surface == NULL) {
1859         ds_err("tizen_policy_client_get_surface() failed.");
1860         wl_client_post_no_memory(wl_client);
1861         return;
1862     }
1863
1864     if (policy_surface->floating_mode == false)
1865         return;
1866
1867     policy_surface->floating_mode = false;
1868
1869     struct ds_tizen_event_policy_surface_set_floating_mode event = {
1870         .policy_surface = policy_surface,
1871     };
1872     wl_signal_emit(&policy_surface->events.set_floating_mode, &event);
1873 }
1874
1875 static void
1876 policy_handle_unset_floating_mode(struct wl_client *wl_client,
1877     struct wl_resource *resource, struct wl_resource *surface_resource)
1878 {
1879     struct ds_tizen_policy_surface *policy_surface;
1880
1881     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1882     if (policy_surface == NULL) {
1883         ds_err("tizen_policy_client_get_surface() failed.");
1884         wl_client_post_no_memory(wl_client);
1885         return;
1886     }
1887
1888     if (policy_surface->floating_mode == false)
1889         return;
1890
1891     policy_surface->floating_mode = false;
1892
1893     struct ds_tizen_event_policy_surface_unset_floating_mode event = {
1894         .policy_surface = policy_surface,
1895     };
1896     wl_signal_emit(&policy_surface->events.unset_floating_mode, &event);
1897 }
1898
1899 static void
1900 policy_handle_set_stack_mode(struct wl_client *wl_client,
1901     struct wl_resource *resource, struct wl_resource *surface_resource,
1902     uint32_t mode)
1903 {
1904     struct ds_tizen_policy_surface *policy_surface;
1905     enum ds_tizen_policy_stack_mode stack_mode;
1906
1907     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1908     if (policy_surface == NULL) {
1909         ds_err("tizen_policy_client_get_surface() failed.");
1910         wl_client_post_no_memory(wl_client);
1911         return;
1912     }
1913
1914     switch (mode) {
1915         case TIZEN_POLICY_STACK_MODE_ABOVE:
1916             stack_mode = DS_TIZEN_POLICY_STACK_MODE_ABOVE;
1917             break;
1918         case TIZEN_POLICY_STACK_MODE_BELOW:
1919             stack_mode = DS_TIZEN_POLICY_STACK_MODE_BELOW;
1920             break;
1921         default:
1922             stack_mode = DS_TIZEN_POLICY_STACK_MODE_NONE;
1923             break;
1924     }
1925
1926     if (policy_surface->stack_mode == stack_mode)
1927         return;
1928
1929     policy_surface->stack_mode = stack_mode;
1930
1931     struct ds_tizen_event_policy_surface_set_stack_mode event = {
1932         .policy_surface = policy_surface,
1933         .mode = stack_mode,
1934     };
1935     wl_signal_emit(&policy_surface->events.set_stack_mode, &event);
1936 }
1937
1938 static void
1939 policy_handle_activate_above_by_res_id(struct wl_client *wl_client,
1940     struct wl_resource *resource, uint32_t universal_id,
1941     uint32_t above_universal_id)
1942 {
1943     struct ds_tizen_policy_client *client;
1944
1945     client = wl_resource_get_user_data(resource);
1946
1947     struct ds_tizen_event_policy_activate_above_by_universal_id event = {
1948         .policy = client->policy,
1949         .universal_id = universal_id,
1950         .above_universal_id = above_universal_id,
1951     };
1952     wl_signal_emit(&client->policy->events.activate_above_by_universal_id, &event);
1953 }
1954
1955 static void
1956 subsurface_watcher_handle_destroy(struct wl_client *wl_client,
1957     struct wl_resource *resource)
1958 {
1959    wl_resource_destroy(resource);
1960 }
1961
1962 static const struct tizen_subsurface_watcher_interface subsurface_watcher_impl =
1963 {
1964    subsurface_watcher_handle_destroy,
1965 };
1966
1967 static void
1968 _tizen_policy_subsurface_watcher_handle_destroy(struct wl_resource *resource)
1969 {
1970     struct ds_tizen_policy_subsurface_watcher *subsurface_watcher;
1971
1972     subsurface_watcher = wl_resource_get_user_data(resource);
1973
1974     ds_inf("_tizen_policy_subsurface_watcher_handle_destroy (subsurface_watcher:%p)",
1975         subsurface_watcher);
1976
1977     wl_signal_emit(&subsurface_watcher->events.destroy, subsurface_watcher);
1978     free(subsurface_watcher);
1979 }
1980
1981 static void
1982 policy_handle_get_subsurface_watcher(struct wl_client *wl_client,
1983     struct wl_resource *resource, uint32_t id,
1984     struct wl_resource *surface_resource)
1985 {
1986     struct ds_tizen_policy_surface *policy_surface;
1987     struct ds_tizen_policy_subsurface_watcher *subsurface_watcher;
1988
1989     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1990     if (policy_surface == NULL) {
1991         ds_err("tizen_policy_client_get_surface() failed.");
1992         wl_client_post_no_memory(wl_client);
1993         return;
1994     }
1995
1996     subsurface_watcher = calloc(1, sizeof *subsurface_watcher);
1997     if (!subsurface_watcher) {
1998         ds_err("calloc() failed. tizen_policy");
1999         return;
2000     }
2001
2002     subsurface_watcher->policy_surface = policy_surface;
2003     wl_signal_init(&subsurface_watcher->events.destroy);
2004
2005     wl_list_insert(&policy_surface->subsurface_watchers, &subsurface_watcher->link);
2006
2007     subsurface_watcher->resource = wl_resource_create(wl_client,
2008         &tizen_subsurface_watcher_interface, wl_resource_get_version(resource),
2009         id);
2010     if (subsurface_watcher->resource == NULL) {
2011         ds_err("tizen_policy : wl_resource_create() failed.");
2012         free(subsurface_watcher);
2013         wl_client_post_no_memory(wl_client);
2014         return;
2015     }
2016
2017     wl_resource_set_implementation(subsurface_watcher->resource,
2018         &subsurface_watcher_impl, subsurface_watcher,
2019         _tizen_policy_subsurface_watcher_handle_destroy);
2020
2021     struct ds_tizen_event_policy_surface_get_subsurface_watcher event = {
2022         .policy_surface = policy_surface,
2023         .subsurface_watcher = subsurface_watcher,
2024     };
2025     wl_signal_emit(&policy_surface->events.get_subsurface_watcher, &event);
2026 }
2027
2028 static void
2029 policy_handle_set_parent(struct wl_client *wl_client,
2030     struct wl_resource *resource, struct wl_resource *surface_resource,
2031     struct wl_resource *parent_surface_resource)
2032 {
2033     struct ds_tizen_policy_surface *policy_surface;
2034     struct ds_surface *parent_surface;
2035
2036     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
2037     if (policy_surface == NULL) {
2038         ds_err("tizen_policy_client_get_surface() failed.");
2039         wl_client_post_no_memory(wl_client);
2040         return;
2041     }
2042
2043     parent_surface = ds_surface_from_resource(parent_surface_resource);
2044
2045     struct ds_tizen_event_policy_surface_set_parent event = {
2046         .policy_surface = policy_surface,
2047         .parent_surface = parent_surface,
2048     };
2049     wl_signal_emit(&policy_surface->events.set_parent, &event);
2050 }
2051
2052 static void
2053 policy_handle_ack_conformant_region(struct wl_client *wl_client,
2054     struct wl_resource *resource, struct wl_resource *surface_resource,
2055     uint32_t serial)
2056 {
2057     struct ds_tizen_policy_surface *policy_surface;
2058
2059     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
2060     if (policy_surface == NULL) {
2061         ds_err("tizen_policy_client_get_surface() failed.");
2062         wl_client_post_no_memory(wl_client);
2063         return;
2064     }
2065
2066     struct ds_tizen_event_policy_surface_ack_conformant_region event = {
2067         .policy_surface = policy_surface,
2068         .serial = serial,
2069     };
2070     wl_signal_emit(&policy_surface->events.ack_conformant_region, &event);
2071 }
2072
2073 static void
2074 policy_handle_destroy(struct wl_client *wl_client, struct wl_resource *resource)
2075 {
2076     struct ds_tizen_policy_client *client;
2077
2078     client = wl_resource_get_user_data(resource);
2079
2080     if (!wl_list_empty(&client->policy_surfaces)) {
2081         ds_err("tizen_policy was destroyed before children");
2082         return;
2083     }
2084
2085     wl_resource_destroy(resource);
2086 }
2087
2088 static void
2089 policy_handle_has_video(struct wl_client *wl_client,
2090     struct wl_resource *resource, struct wl_resource *surface_resource,
2091     uint32_t has)
2092 {
2093     struct ds_tizen_policy_surface *policy_surface;
2094
2095     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
2096     if (policy_surface == NULL) {
2097         ds_err("tizen_policy_client_get_surface() failed.");
2098         wl_client_post_no_memory(wl_client);
2099         return;
2100     }
2101
2102     if (policy_surface->video == has)
2103         return;
2104
2105     policy_surface->video = has;
2106
2107     struct ds_tizen_event_policy_surface_set_video event = {
2108         .policy_surface = policy_surface,
2109         .video = has,
2110     };
2111     wl_signal_emit(&policy_surface->events.set_video, &event);
2112 }
2113
2114 static void
2115 policy_handle_set_appid(struct wl_client *wl_client,
2116     struct wl_resource *resource, int32_t pid, const char *appid)
2117 {
2118     struct ds_tizen_policy_client *client;
2119
2120     client = wl_resource_get_user_data(resource);
2121
2122     struct ds_tizen_event_policy_set_appid event = {
2123         .policy = client->policy,
2124         .pid = pid,
2125         .appid = appid,
2126     };
2127     wl_signal_emit(&client->policy->events.set_appid, &event);
2128 }
2129
2130 static void
2131 policy_handle_show(struct wl_client *wl_client, struct wl_resource *resource,
2132     struct wl_resource *surface_resource)
2133 {
2134     struct ds_tizen_policy_surface *policy_surface;
2135
2136     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
2137     if (policy_surface == NULL) {
2138         ds_err("tizen_policy_client_get_surface() failed.");
2139         wl_client_post_no_memory(wl_client);
2140         return;
2141     }
2142
2143     struct ds_tizen_event_policy_surface_show event = {
2144         .policy_surface = policy_surface,
2145     };
2146     wl_signal_emit(&policy_surface->events.show, &event);
2147 }
2148
2149 static void
2150 policy_handle_hide(struct wl_client *wl_client, struct wl_resource *resource,
2151     struct wl_resource *surface_resource)
2152 {
2153     struct ds_tizen_policy_surface *policy_surface;
2154
2155     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
2156     if (policy_surface == NULL) {
2157         ds_err("tizen_policy_client_get_surface() failed.");
2158         wl_client_post_no_memory(wl_client);
2159         return;
2160     }
2161
2162     struct ds_tizen_event_policy_surface_hide event = {
2163         .policy_surface = policy_surface,
2164     };
2165     wl_signal_emit(&policy_surface->events.hide, &event);
2166 }
2167
2168 static void
2169 policy_handle_set_transient_for_below(struct wl_client *wl_client,
2170     struct wl_resource *resource, uint32_t universal_id,
2171     uint32_t parent_universal_id)
2172 {
2173     struct ds_tizen_policy_client *client;
2174
2175     client = wl_resource_get_user_data(resource);
2176
2177     struct ds_tizen_event_policy_set_transient_for_below event = {
2178         .policy = client->policy,
2179         .universal_id = universal_id,
2180         .parent_universal_id = parent_universal_id,
2181     };
2182     wl_signal_emit(&client->policy->events.set_transient_for_below, &event);
2183 }
2184
2185 static void
2186 policy_handle_set_parent_with_below(struct wl_client *wl_client,
2187     struct wl_resource *resource, struct wl_resource *surface_resource,
2188     struct wl_resource *parent_surface_resource)
2189 {
2190     struct ds_tizen_policy_surface *policy_surface;
2191     struct ds_surface *parent_surface;
2192
2193     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
2194     if (policy_surface == NULL) {
2195         ds_err("tizen_policy_client_get_surface() failed.");
2196         wl_client_post_no_memory(wl_client);
2197         return;
2198     }
2199
2200     parent_surface = ds_surface_from_resource(parent_surface_resource);
2201
2202     struct ds_tizen_event_policy_surface_set_parent_with_below event = {
2203         .policy_surface = policy_surface,
2204         .parent_surface = parent_surface,
2205     };
2206     wl_signal_emit(&policy_surface->events.set_parent_with_below, &event);
2207 }
2208
2209 static const struct tizen_policy_interface policy_impl =
2210 {
2211    policy_handle_get_visibility,
2212    policy_handle_get_position,
2213    policy_handle_activate,
2214    policy_handle_activate_below_by_res_id,
2215    policy_handle_raise,
2216    policy_handle_lower,
2217    policy_handle_lower_by_res_id,
2218    policy_handle_set_focus_skip,
2219    policy_handle_unset_focus_skip,
2220    policy_handle_set_role,
2221    policy_handle_set_type,
2222    policy_handle_set_conformant,
2223    policy_handle_unset_conformant,
2224    policy_handle_get_conformant,
2225    policy_handle_set_notification_level,
2226    policy_handle_set_transient_for,
2227    policy_handle_unset_transient_for,
2228    policy_handle_set_window_screen_mode,
2229    policy_handle_place_subsurface_below_parent,
2230    policy_handle_set_subsurface_stand_alone,
2231    policy_handle_get_subsurface,
2232    policy_handle_set_opaque_state,
2233    policy_handle_iconify,
2234    policy_handle_uniconify,
2235    policy_handle_add_aux_hint,
2236    policy_handle_change_aux_hint,
2237    policy_handle_delete_aux_hint,
2238    policy_handle_get_supported_aux_hints,
2239    policy_handle_set_background_state,
2240    policy_handle_unset_background_state,
2241    policy_handle_set_floating_mode,
2242    policy_handle_unset_floating_mode,
2243    policy_handle_set_stack_mode,
2244    policy_handle_activate_above_by_res_id,
2245    policy_handle_get_subsurface_watcher,
2246    policy_handle_set_parent,
2247    policy_handle_ack_conformant_region,
2248    policy_handle_destroy,
2249    policy_handle_has_video,
2250    policy_handle_set_appid,
2251    policy_handle_show,
2252    policy_handle_hide,
2253    policy_handle_set_transient_for_below,
2254    policy_handle_set_parent_with_below,
2255 };
2256
2257 static void
2258 _tizen_policy_client_handle_destroy(struct wl_resource *resource)
2259 {
2260     struct ds_tizen_policy_client *client;
2261     struct ds_tizen_policy_surface *policy_surface, *tmp;
2262
2263     client = wl_resource_get_user_data(resource);
2264
2265     ds_inf("_tizen_policy_client_handle_destroy (client:%p)", client);
2266
2267     wl_list_for_each_safe(policy_surface, tmp, &client->policy_surfaces, link) {
2268         wl_signal_emit(&policy_surface->events.destroy, policy_surface);
2269         wl_list_remove(&policy_surface->link);
2270         free(policy_surface);
2271     }
2272
2273     wl_list_remove(&client->link);
2274     free(client);
2275 }
2276
2277 static void
2278 policy_bind(struct wl_client *wl_client, void *data, uint32_t version,
2279         uint32_t id)
2280 {
2281     struct ds_tizen_policy *policy = data;
2282     struct ds_tizen_policy_client *client;
2283
2284     client = calloc(1, sizeof *client);
2285     if (client == NULL) {
2286         ds_err("calloc() failed. tizen_policy");
2287         wl_client_post_no_memory(wl_client);
2288         return;
2289     }
2290
2291     ds_inf("tizen_policy_client binds. (client:%p)", client);
2292
2293     client->policy = policy;
2294     client->wl_client = wl_client;
2295     wl_client_get_credentials(client->wl_client, &client->pid, &client->uid, NULL);
2296
2297     wl_list_init(&client->policy_surfaces);
2298
2299     client->resource = wl_resource_create(wl_client, &tizen_policy_interface,
2300             MIN(version, TIZEN_POLICY_VERSION), id);
2301
2302     if (client->resource == NULL) {
2303         ds_err("tizen_policy : wl_resource_create() failed.");
2304         free(client);
2305         wl_client_post_no_memory(wl_client);
2306         return;
2307     }
2308
2309     wl_resource_set_implementation(client->resource, &policy_impl, client,
2310             _tizen_policy_client_handle_destroy);
2311
2312     wl_list_insert(&policy->clients, &client->link);
2313 }