Revert "screen: add wtz_screen_set_opaque_region request callback"
[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 new_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 new_visibility;
81         struct wl_signal new_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 new_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.new_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_new_surface_listener(
229         struct ds_tizen_policy *policy,
230         struct wl_listener *listener)
231 {
232     wl_signal_add(&policy->events.new_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_new_visibility_listener(
333         struct ds_tizen_policy_surface *policy_surface,
334         struct wl_listener *listener)
335 {
336     wl_signal_add(&policy_surface->events.new_visibility, listener);
337 }
338
339 WL_EXPORT void
340 ds_tizen_policy_surface_add_new_position_listener(
341         struct ds_tizen_policy_surface *policy_surface,
342         struct wl_listener *listener)
343 {
344     wl_signal_add(&policy_surface->events.new_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_new_subsurface_watcher_listener(
525         struct ds_tizen_policy_surface *policy_surface,
526         struct wl_listener *listener)
527 {
528     wl_signal_add(&policy_surface->events.new_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     if (wl_resource_get_version(policy_surface->client->resource) >=
642         TIZEN_POLICY_CONFORMANT_REGION_SINCE_VERSION) {
643         tizen_policy_send_conformant_area(policy_surface->client->resource,
644             ds_surface_get_wl_resource(policy_surface->surface), conformant_part, visible,
645             x, y, w, h);
646     } else {
647         ds_err("client does not support TIZEN_POLICY_CONFORMANT_REGION."
648             "client version(%d) < server version(%d)",
649             wl_resource_get_version(policy_surface->client->resource),
650             TIZEN_POLICY_CONFORMANT_REGION_SINCE_VERSION);
651     }
652 }
653
654 WL_EXPORT void
655 ds_tizen_policy_surface_send_notification_done(struct ds_tizen_policy_surface *policy_surface,
656     enum ds_tizen_policy_notification_level level,
657     enum ds_tizen_policy_error_state state)
658 {
659     uint32_t notification_level;
660     uint32_t error_state;
661
662     switch (level) {
663         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_1:
664             notification_level = TIZEN_POLICY_LEVEL_1;
665             break;
666         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_2:
667             notification_level = TIZEN_POLICY_LEVEL_2;
668             break;
669         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_3:
670             notification_level = TIZEN_POLICY_LEVEL_3;
671             break;
672         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_NONE:
673             notification_level = TIZEN_POLICY_LEVEL_NONE;
674             break;
675         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_DEFAULT:
676             notification_level = TIZEN_POLICY_LEVEL_DEFAULT;
677             break;
678         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_MEDIUM:
679             notification_level = TIZEN_POLICY_LEVEL_MEDIUM;
680             break;
681         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_HIGH:
682             notification_level = TIZEN_POLICY_LEVEL_HIGH;
683             break;
684         case DS_TIZEN_POLICY_NOTIFICATION_LEVEL_TOP:
685             notification_level = TIZEN_POLICY_LEVEL_TOP;
686             break;
687         default:
688             ds_err("Not supported notification_level(%d)", level);
689             return;
690     }
691
692     switch (state) {
693         case DS_TIZEN_POLICY_ERROR_STATE_NONE:
694             error_state = TIZEN_POLICY_ERROR_STATE_NONE;
695             break;
696         case DS_TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED:
697             error_state = TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED;
698             break;
699         default:
700             ds_err("Not supported error_state(%d)", state);
701             return;
702     }
703
704     tizen_policy_send_notification_done(policy_surface->client->resource,
705         ds_surface_get_wl_resource(policy_surface->surface),
706         notification_level, error_state);
707 }
708
709 WL_EXPORT void
710 ds_tizen_policy_surface_send_window_screen_mode_done(struct ds_tizen_policy_surface *policy_surface,
711     enum ds_tizen_policy_window_screen_mode mode,
712     enum ds_tizen_policy_error_state state)
713 {
714     uint32_t window_screen_mode;
715     uint32_t error_state;
716
717     switch (mode) {
718         case DS_TIZEN_POLICY_WINDOW_SCREEN_MODE_DEFAULT:
719             window_screen_mode = TIZEN_POLICY_MODE_DEFAULT;
720             break;
721         case DS_TIZEN_POLICY_WINDOW_SCREEN_MODE_ALWAYS_ON:
722             window_screen_mode = TIZEN_POLICY_MODE_ALWAYS_ON;
723             break;
724         default:
725             ds_err("Not supported window_screen_mode(%d)", mode);
726             return;
727     }
728
729     switch (state) {
730         case DS_TIZEN_POLICY_ERROR_STATE_NONE:
731             error_state = TIZEN_POLICY_ERROR_STATE_NONE;
732             break;
733         case DS_TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED:
734             error_state = TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED;
735             break;
736         default:
737             ds_err("Not supported error_state(%d)", state);
738             return;
739     }
740
741     tizen_policy_send_window_screen_mode_done(policy_surface->client->resource,
742         ds_surface_get_wl_resource(policy_surface->surface),
743         window_screen_mode, error_state);
744 }
745
746 WL_EXPORT void
747 ds_tizen_policy_surface_send_iconify_state_changed(struct ds_tizen_policy_surface *policy_surface,
748     bool iconified, bool force)
749 {
750     tizen_policy_send_iconify_state_changed(policy_surface->client->resource,
751         ds_surface_get_wl_resource(policy_surface->surface), iconified, force);
752 }
753
754 WL_EXPORT void
755 ds_tizen_policy_surface_send_supported_aux_hints(struct ds_tizen_policy_surface *policy_surface,
756     struct wl_array *hints, uint32_t force)
757 {
758     // TODO:
759 }
760
761 WL_EXPORT void
762 ds_tizen_policy_surface_send_allowed_aux_hint(struct ds_tizen_policy_surface *policy_surface,
763     int32_t hint_id)
764 {
765     // TODO:
766 }
767
768 WL_EXPORT void
769 ds_tizen_policy_surface_send_aux_message(struct ds_tizen_policy_surface *policy_surface,
770     const char *key, const char *value, struct wl_array *options)
771 {
772     // TODO:
773 }
774
775 WL_EXPORT void
776 ds_tizen_policy_surface_send_conformant_region(struct ds_tizen_policy_surface *policy_surface,
777     enum ds_tizen_policy_conformant_part part, bool visible,
778     int32_t x, int32_t y, int32_t w, int32_t h, uint32_t serial)
779 {
780     uint32_t conformant_part;
781
782     switch (part) {
783         case DS_TIZEN_POLICY_CONFORMANT_PART_INDICATOR:
784             conformant_part = TIZEN_POLICY_CONFORMANT_PART_INDICATOR;
785             break;
786         case DS_TIZEN_POLICY_CONFORMANT_PART_KEYBOARD:
787             conformant_part = TIZEN_POLICY_CONFORMANT_PART_KEYBOARD;
788             break;
789         case DS_TIZEN_POLICY_CONFORMANT_PART_CLIPBOARD:
790             conformant_part = TIZEN_POLICY_CONFORMANT_PART_CLIPBOARD;
791             break;
792         default:
793             ds_err("Not supported conformant_part(%d)", part);
794             return;
795     }
796
797     tizen_policy_send_conformant_region(policy_surface->client->resource,
798         ds_surface_get_wl_resource(policy_surface->surface), conformant_part, visible,
799         x, y, w, h, serial);
800 }
801
802 WL_EXPORT void
803 ds_tizen_policy_surface_send_interactive_move_done(struct ds_tizen_policy_surface *policy_surface,
804     int32_t x, int32_t y, uint32_t w, uint32_t h)
805 {
806     // TODO:
807 }
808
809 WL_EXPORT void
810 ds_tizen_policy_surface_send_interactive_resize_done(struct ds_tizen_policy_surface *policy_surface,
811     int32_t x, int32_t y, uint32_t w, uint32_t h)
812 {
813         // TODO:
814 }
815
816 static int32_t
817 tizen_policy_visibility_get_type(enum ds_tizen_policy_visibility_type type)
818 {
819     uint32_t vis_type;
820
821     switch (type) {
822         case DS_TIZEN_POLICY_VISIBILITY_TYPE_UNOBSCURED:
823             vis_type = TIZEN_VISIBILITY_VISIBILITY_UNOBSCURED;
824             break;
825         case DS_TIZEN_POLICY_VISIBILITY_TYPE_PARTIALLY_OBSCURED:
826             vis_type = TIZEN_VISIBILITY_VISIBILITY_PARTIALLY_OBSCURED;
827             break;
828         case DS_TIZEN_POLICY_VISIBILITY_TYPE_FULLY_OBSCURED:
829             vis_type = TIZEN_VISIBILITY_VISIBILITY_FULLY_OBSCURED;
830             break;
831         case DS_TIZEN_POLICY_VISIBILITY_TYPE_PRE_UNOBSCURED:
832             vis_type = TIZEN_VISIBILITY_VISIBILITY_PRE_UNOBSCURED;
833             break;
834         default:
835             ds_err("Not supported visible type (%d)", type);
836             vis_type = DS_TIZEN_POLICY_VISIBILITY_TYPE_UNKNOWN;
837             break;
838     }
839
840     return vis_type;
841 }
842
843 WL_EXPORT void
844 ds_tizen_policy_visibility_send_notify(
845     struct ds_tizen_policy_visibility *visibility,
846     enum ds_tizen_policy_visibility_type type)
847 {
848     uint32_t vis_type;
849
850     vis_type = tizen_policy_visibility_get_type(type);
851     if (vis_type == DS_TIZEN_POLICY_VISIBILITY_TYPE_UNKNOWN)
852         return;
853
854     tizen_visibility_send_notify(visibility->resource, vis_type);
855 }
856
857 WL_EXPORT void
858 ds_tizen_policy_visibility_send_changed(
859     struct ds_tizen_policy_visibility *visibility,
860     enum ds_tizen_policy_visibility_type type, uint32_t option)
861 {
862     uint32_t vis_type;
863
864     vis_type = tizen_policy_visibility_get_type(type);
865     if (vis_type == DS_TIZEN_POLICY_VISIBILITY_TYPE_UNKNOWN)
866         return;
867
868     tizen_visibility_send_changed(visibility->resource, vis_type, option);
869 }
870
871 WL_EXPORT void
872 ds_tizen_policy_position_send_changed(
873     struct ds_tizen_policy_position *position, int32_t x, int32_t y)
874 {
875     tizen_position_send_changed(position->resource, x, y);
876 }
877
878 WL_EXPORT struct ds_surface *
879 ds_tizen_policy_surface_get_surface(struct ds_tizen_policy_surface *policy_surface)
880 {
881     return policy_surface->surface;
882 }
883
884 WL_EXPORT bool
885 ds_tizen_policy_surface_get_conformant(struct ds_tizen_policy_surface *policy_surface)
886 {
887     return policy_surface->conformant;
888 }
889
890 WL_EXPORT int32_t
891 ds_tizen_policy_surface_get_opaque_state(struct ds_tizen_policy_surface *policy_surface)
892 {
893     return policy_surface->opaque_state;
894 }
895
896 WL_EXPORT bool
897 ds_tizen_policy_surface_get_iconified(struct ds_tizen_policy_surface *policy_surface)
898 {
899     return policy_surface->iconified;
900 }
901
902 WL_EXPORT bool
903 ds_tizen_policy_surface_get_floating_mode(struct ds_tizen_policy_surface *policy_surface)
904 {
905     return policy_surface->floating_mode;
906 }
907
908 WL_EXPORT enum ds_tizen_policy_stack_mode
909 ds_tizen_policy_surface_get_stack_mode(struct ds_tizen_policy_surface *policy_surface)
910 {
911     return policy_surface->stack_mode;
912 }
913
914 static struct ds_tizen_policy_surface *
915 tizen_policy_client_find_policy_surface(struct ds_tizen_policy_client *client,
916     struct ds_surface *surface)
917 {
918     struct ds_tizen_policy_surface *policy_surface;
919
920     wl_list_for_each(policy_surface, &client->policy_surfaces, link) {
921         if (surface == policy_surface->surface)
922             return policy_surface;
923     }
924
925     return NULL;
926 }
927
928 static struct ds_tizen_policy_surface *
929 tizen_policy_client_get_surface(struct wl_resource *resource,
930     struct wl_resource *surface_resource)
931 {
932     struct ds_tizen_policy_client *client;
933     struct ds_tizen_policy_surface *policy_surface;
934     struct ds_surface *surface;
935
936     client = wl_resource_get_user_data(resource);
937     surface = ds_surface_from_resource(surface_resource);
938
939     policy_surface = tizen_policy_client_find_policy_surface(client, surface);
940     if (policy_surface)
941         return policy_surface;
942
943     policy_surface = calloc(1, sizeof *policy_surface);
944     if (policy_surface == NULL) {
945         ds_err("calloc() failed. tizen_policy");
946         return NULL;
947     }
948
949     policy_surface->client = client;
950     policy_surface->surface = surface;
951     wl_client_get_credentials(client->wl_client, &policy_surface->pid, &policy_surface->uid, NULL);
952
953     wl_list_init(&policy_surface->visibilities);
954     wl_list_init(&policy_surface->positions);
955     wl_list_init(&policy_surface->subsurface_watchers);
956
957     wl_signal_init(&policy_surface->events.destroy);
958     wl_signal_init(&policy_surface->events.new_visibility);
959     wl_signal_init(&policy_surface->events.new_position);
960     wl_signal_init(&policy_surface->events.activate);
961     wl_signal_init(&policy_surface->events.raise);
962     wl_signal_init(&policy_surface->events.lower);
963     wl_signal_init(&policy_surface->events.set_focus_skip);
964     wl_signal_init(&policy_surface->events.unset_focus_skip);
965     wl_signal_init(&policy_surface->events.set_role);
966     wl_signal_init(&policy_surface->events.set_window_type);
967     wl_signal_init(&policy_surface->events.set_conformant);
968     wl_signal_init(&policy_surface->events.unset_conformant);
969     wl_signal_init(&policy_surface->events.get_conformant);
970     wl_signal_init(&policy_surface->events.set_notification_level);
971     wl_signal_init(&policy_surface->events.set_window_screen_mode);
972     wl_signal_init(&policy_surface->events.get_subsurface);
973     wl_signal_init(&policy_surface->events.iconify);
974     wl_signal_init(&policy_surface->events.uniconify);
975     wl_signal_init(&policy_surface->events.add_aux_hint);
976     wl_signal_init(&policy_surface->events.change_aux_hint);
977     wl_signal_init(&policy_surface->events.delete_aux_hint);
978     wl_signal_init(&policy_surface->events.get_supported_aux_hints);
979     wl_signal_init(&policy_surface->events.set_floating_mode);
980     wl_signal_init(&policy_surface->events.unset_floating_mode);
981     wl_signal_init(&policy_surface->events.set_stack_mode);
982     wl_signal_init(&policy_surface->events.new_subsurface_watcher);
983     wl_signal_init(&policy_surface->events.set_parent);
984     wl_signal_init(&policy_surface->events.ack_conformant_region);
985     wl_signal_init(&policy_surface->events.set_video);
986     wl_signal_init(&policy_surface->events.show);
987     wl_signal_init(&policy_surface->events.hide);
988     wl_signal_init(&policy_surface->events.set_parent_with_below);
989
990     wl_list_insert(&client->policy_surfaces, &policy_surface->link);
991
992     struct ds_tizen_policy_event_new_surface event = {
993         .policy = client->policy,
994         .policy_surface = policy_surface,
995     };
996     wl_signal_emit_mutable(&client->policy->events.new_surface, &event);
997
998     return policy_surface;
999 }
1000
1001 static void
1002 policy_handle_display_destroy(struct wl_listener *listener, void *data)
1003 {
1004     struct ds_tizen_policy *policy;
1005
1006     policy = wl_container_of(listener, policy, destroy);
1007
1008     ds_inf("Global destroy: policy(%p)", policy);
1009
1010     wl_signal_emit_mutable(&policy->events.destroy, policy);
1011
1012     if (policy->use_security)
1013         tizen_security_finish();
1014
1015     wl_list_remove(&policy->destroy.link);
1016     wl_global_destroy(policy->global);
1017     free(policy);
1018 }
1019
1020 static void
1021 visibility_handle_destroy(struct wl_client *wl_client,
1022     struct wl_resource *resource)
1023 {
1024    wl_resource_destroy(resource);
1025 }
1026
1027 static const struct tizen_visibility_interface visibility_impl =
1028 {
1029    visibility_handle_destroy,
1030 };
1031
1032 static void
1033 _tizen_policy_visibility_handle_destroy(struct wl_resource *resource)
1034 {
1035     struct ds_tizen_policy_visibility *visibility;
1036
1037     visibility = wl_resource_get_user_data(resource);
1038
1039     ds_inf("_tizen_policy_visibility_handle_destroy (visibility:%p)",
1040         visibility);
1041
1042     wl_signal_emit_mutable(&visibility->events.destroy, visibility);
1043     wl_list_remove(&visibility->link);
1044     free(visibility);
1045 }
1046
1047 static void
1048 policy_handle_get_visibility(struct wl_client *wl_client, struct wl_resource *resource,
1049     uint32_t id, struct wl_resource *surface_resource)
1050 {
1051     struct ds_tizen_policy_surface *policy_surface;
1052     struct ds_tizen_policy_visibility *visibility;
1053
1054     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1055     if (policy_surface == NULL) {
1056         ds_err("tizen_policy_client_get_surface() failed.");
1057         wl_client_post_no_memory(wl_client);
1058         return;
1059     }
1060
1061     visibility = calloc(1, sizeof *visibility);
1062     if (visibility == NULL) {
1063         ds_err("calloc() failed. tizen_policy");
1064         return;
1065     }
1066
1067     visibility->policy_surface = policy_surface;
1068     wl_signal_init(&visibility->events.destroy);
1069
1070     wl_list_insert(&policy_surface->visibilities, &visibility->link);
1071
1072     visibility->resource = wl_resource_create(wl_client,
1073         &tizen_visibility_interface, wl_resource_get_version(resource),
1074         id);
1075     if (visibility->resource == NULL) {
1076         ds_err("tizen_policy : wl_resource_create() failed.");
1077         wl_list_remove(&visibility->link);
1078         free(visibility);
1079         wl_client_post_no_memory(wl_client);
1080         return;
1081     }
1082
1083     wl_resource_set_implementation(visibility->resource,
1084         &visibility_impl, visibility,
1085         _tizen_policy_visibility_handle_destroy);
1086
1087     struct ds_tizen_policy_surface_event_new_visibility event = {
1088         .policy_surface = policy_surface,
1089         .visibility = visibility,
1090     };
1091     wl_signal_emit_mutable(&policy_surface->events.new_visibility, &event);
1092 }
1093
1094 static void
1095 position_handle_destroy(struct wl_client *wl_client, struct wl_resource *resource)
1096 {
1097    wl_resource_destroy(resource);
1098 }
1099
1100 static void
1101 position_handle_set(struct wl_client *wl_client, struct wl_resource *resource,
1102     int32_t x, int32_t y)
1103 {
1104     struct ds_tizen_policy_position *position;
1105
1106     position = wl_resource_get_user_data(resource);
1107
1108     struct ds_tizen_policy_event_position_set event = {
1109         .position = position,
1110         .x = x,
1111         .y = y,
1112     };
1113     wl_signal_emit_mutable(&position->events.set, &event);
1114 }
1115
1116 static const struct tizen_position_interface position_impl =
1117 {
1118    position_handle_destroy,
1119    position_handle_set,
1120 };
1121
1122 static void
1123 _tizen_policy_position_handle_destroy(struct wl_resource *resource)
1124 {
1125     struct ds_tizen_policy_position *position;
1126
1127     position = wl_resource_get_user_data(resource);
1128
1129     ds_inf("_tizen_policy_position_handle_destroy (position:%p)",
1130         position);
1131
1132     wl_signal_emit_mutable(&position->events.destroy, position);
1133     wl_list_remove(&position->link);
1134     free(position);
1135 }
1136
1137 static void
1138 policy_handle_get_position(struct wl_client *wl_client, struct wl_resource *resource,
1139     uint32_t id, struct wl_resource *surface_resource)
1140 {
1141     struct ds_tizen_policy_surface *policy_surface;
1142     struct ds_tizen_policy_position *position;
1143
1144     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1145     if (policy_surface == NULL) {
1146         ds_err("tizen_policy_client_get_surface() failed.");
1147         wl_client_post_no_memory(wl_client);
1148         return;
1149     }
1150
1151     position = calloc(1, sizeof *position);
1152     if (position == NULL) {
1153         ds_err("calloc() failed. tizen_policy");
1154         return;
1155     }
1156
1157     position->policy_surface = policy_surface;
1158     wl_signal_init(&position->events.destroy);
1159     wl_signal_init(&position->events.set);
1160
1161     wl_list_insert(&policy_surface->positions, &position->link);
1162
1163     position->resource = wl_resource_create(wl_client,
1164         &tizen_position_interface, wl_resource_get_version(resource),
1165         id);
1166     if (position->resource == NULL) {
1167         ds_err("tizen_policy : wl_resource_create() failed.");
1168         wl_list_remove(&position->link);
1169         free(position);
1170         wl_client_post_no_memory(wl_client);
1171         return;
1172     }
1173
1174     wl_resource_set_implementation(position->resource,
1175         &position_impl, position,
1176         _tizen_policy_position_handle_destroy);
1177
1178     struct ds_tizen_policy_surface_event_new_position event = {
1179         .policy_surface = policy_surface,
1180         .position = position,
1181     };
1182     wl_signal_emit_mutable(&policy_surface->events.new_position, &event);
1183 }
1184
1185 static void
1186 policy_handle_activate(struct wl_client *wl_client,
1187     struct wl_resource *resource, struct wl_resource *surface_resource)
1188 {
1189     struct ds_tizen_policy_surface *policy_surface;
1190
1191     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1192     if (policy_surface == NULL) {
1193         ds_err("tizen_policy_client_get_surface() failed.");
1194         wl_client_post_no_memory(wl_client);
1195         return;
1196     }
1197
1198     struct ds_tizen_policy_surface_event_activate event = {
1199         .policy_surface = policy_surface,
1200     };
1201     wl_signal_emit_mutable(&policy_surface->events.activate, &event);
1202 }
1203
1204 static void
1205 policy_handle_activate_below_by_res_id(struct wl_client *wl_client,
1206     struct wl_resource *resource,  uint32_t universal_id,
1207     uint32_t below_universal_id)
1208 {
1209     struct ds_tizen_policy_client *client;
1210
1211     client = wl_resource_get_user_data(resource);
1212
1213     struct ds_tizen_policy_event_activate_below_by_univeral_id event = {
1214         .policy = client->policy,
1215         .universal_id = universal_id,
1216         .below_universal_id = below_universal_id,
1217     };
1218     wl_signal_emit_mutable(&client->policy->events.activate_below_by_univeral_id, &event);
1219 }
1220
1221 static void
1222 policy_handle_raise(struct wl_client *wl_client, struct wl_resource *resource,
1223     struct wl_resource *surface_resource)
1224 {
1225     struct ds_tizen_policy_surface *policy_surface;
1226
1227     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1228     if (policy_surface == NULL) {
1229         ds_err("tizen_policy_client_get_surface() failed.");
1230         wl_client_post_no_memory(wl_client);
1231         return;
1232     }
1233
1234     struct ds_tizen_policy_surface_event_raise event = {
1235         .policy_surface = policy_surface,
1236     };
1237     wl_signal_emit_mutable(&policy_surface->events.raise, &event);
1238 }
1239
1240 static void
1241 policy_handle_lower(struct wl_client *wl_client, struct wl_resource *resource,
1242     struct wl_resource *surface_resource)
1243 {
1244     struct ds_tizen_policy_surface *policy_surface;
1245
1246     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1247     if (policy_surface == NULL) {
1248         ds_err("tizen_policy_client_get_surface() failed.");
1249         wl_client_post_no_memory(wl_client);
1250         return;
1251     }
1252
1253     struct ds_tizen_policy_surface_event_lower event = {
1254         .policy_surface = policy_surface,
1255     };
1256     wl_signal_emit_mutable(&policy_surface->events.lower, &event);
1257 }
1258
1259 static void
1260 policy_handle_lower_by_res_id(struct wl_client *wl_client,
1261     struct wl_resource *resource,  uint32_t universal_id)
1262 {
1263     struct ds_tizen_policy_client *client;
1264
1265     client = wl_resource_get_user_data(resource);
1266
1267     struct ds_tizen_policy_event_lower_by_universal_id event = {
1268         .policy = client->policy,
1269         .universal_id = universal_id,
1270     };
1271     wl_signal_emit_mutable(&client->policy->events.lower_by_universal_id, &event);
1272 }
1273
1274 static void
1275 policy_handle_set_focus_skip(struct wl_client *wl_client,
1276     struct wl_resource *resource, struct wl_resource *surface_resource)
1277 {
1278     struct ds_tizen_policy_surface *policy_surface;
1279
1280     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1281     if (policy_surface == NULL) {
1282         ds_err("tizen_policy_client_get_surface() failed.");
1283         wl_client_post_no_memory(wl_client);
1284         return;
1285     }
1286
1287     struct ds_tizen_policy_surface_event_set_focus_skip event = {
1288         .policy_surface = policy_surface,
1289     };
1290     wl_signal_emit_mutable(&policy_surface->events.set_focus_skip, &event);
1291 }
1292
1293 static void
1294 policy_handle_unset_focus_skip(struct wl_client *wl_client,
1295     struct wl_resource *resource, struct wl_resource *surface_resource)
1296 {
1297     struct ds_tizen_policy_surface *policy_surface;
1298
1299     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1300     if (policy_surface == NULL) {
1301         ds_err("tizen_policy_client_get_surface() failed.");
1302         wl_client_post_no_memory(wl_client);
1303         return;
1304     }
1305
1306     struct ds_tizen_policy_surface_event_unset_focus_skip event = {
1307         .policy_surface = policy_surface,
1308     };
1309     wl_signal_emit_mutable(&policy_surface->events.unset_focus_skip, &event);
1310 }
1311
1312 static void
1313 policy_handle_set_role(struct wl_client *wl_client, struct wl_resource *resource,
1314     struct wl_resource *surface_resource, const char *role)
1315 {
1316     struct ds_tizen_policy_surface *policy_surface;
1317
1318     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1319     if (policy_surface == NULL) {
1320         ds_err("tizen_policy_client_get_surface() failed.");
1321         wl_client_post_no_memory(wl_client);
1322         return;
1323     }
1324
1325     struct ds_tizen_policy_surface_event_set_role event = {
1326         .policy_surface = policy_surface,
1327         .role = role,
1328     };
1329     wl_signal_emit_mutable(&policy_surface->events.set_role, &event);
1330 }
1331
1332 static void
1333 policy_handle_set_type(struct wl_client *wl_client, struct wl_resource *resource,
1334     struct wl_resource *surface_resource, uint32_t type)
1335 {
1336     struct ds_tizen_policy_surface *policy_surface;
1337     enum ds_tizen_policy_window_type win_type;
1338
1339     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1340     if (policy_surface == NULL) {
1341         ds_err("tizen_policy_client_get_surface() failed.");
1342         wl_client_post_no_memory(wl_client);
1343         return;
1344     }
1345
1346     switch (type) {
1347         case TIZEN_POLICY_WIN_TYPE_NONE:
1348             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_NONE;
1349             break;
1350         case TIZEN_POLICY_WIN_TYPE_TOPLEVEL:
1351             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_TOPLEVEL;
1352             break;
1353         case TIZEN_POLICY_WIN_TYPE_FULLSCREEN:
1354             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_FULLLSCREEN;
1355             break;
1356         case TIZEN_POLICY_WIN_TYPE_MAXIMIZED:
1357             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_MAXIMIZED;
1358             break;
1359         case TIZEN_POLICY_WIN_TYPE_TRANSIENT:
1360             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_TRANSIENT;
1361             break;
1362         case TIZEN_POLICY_WIN_TYPE_MENU:
1363             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_MENU;
1364             break;
1365         case TIZEN_POLICY_WIN_TYPE_DND:
1366             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_DND;
1367             break;
1368         case TIZEN_POLICY_WIN_TYPE_CUSTOM:
1369             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_CUSTOM;
1370             break;
1371         case TIZEN_POLICY_WIN_TYPE_NOTIFICATION:
1372             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_NOTIFICATION;
1373             break;
1374         case TIZEN_POLICY_WIN_TYPE_UTILITY:
1375             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_UTILITY;
1376             break;
1377         case TIZEN_POLICY_WIN_TYPE_DIALOG:
1378             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_DIALOG;
1379             break;
1380         case TIZEN_POLICY_WIN_TYPE_DOCK:
1381             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_DOCK;
1382             break;
1383         case TIZEN_POLICY_WIN_TYPE_SPLASH:
1384             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_SPLASH;
1385             break;
1386         case TIZEN_POLICY_WIN_TYPE_DESKTOP:
1387             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_DESKTOP;
1388             break;
1389         default:
1390             win_type = DS_TIZEN_POLICY_WINDOW_TYPE_NONE;
1391             break;
1392     }
1393
1394     struct ds_tizen_policy_surface_event_set_window_type event = {
1395         .policy_surface = policy_surface,
1396         .win_type = win_type,
1397     };
1398     wl_signal_emit_mutable(&policy_surface->events.set_window_type, &event);
1399 }
1400
1401 static void
1402 policy_handle_set_conformant(struct wl_client *wl_client,
1403     struct wl_resource *resource, struct wl_resource *surface_resource)
1404 {
1405     struct ds_tizen_policy_surface *policy_surface;
1406
1407     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1408     if (policy_surface == NULL) {
1409         ds_err("tizen_policy_client_get_surface() failed.");
1410         wl_client_post_no_memory(wl_client);
1411         return;
1412     }
1413
1414     if (!policy_surface->conformant)
1415         policy_surface->conformant = true;
1416
1417     struct ds_tizen_policy_surface_event_set_conformant event = {
1418         .policy_surface = policy_surface,
1419     };
1420     wl_signal_emit_mutable(&policy_surface->events.set_conformant, &event);
1421 }
1422
1423 static void
1424 policy_handle_unset_conformant(struct wl_client *wl_client,
1425     struct wl_resource *resource, struct wl_resource *surface_resource)
1426 {
1427     struct ds_tizen_policy_surface *policy_surface;
1428
1429     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1430     if (policy_surface == NULL) {
1431         ds_err("tizen_policy_client_get_surface() failed.");
1432         wl_client_post_no_memory(wl_client);
1433         return;
1434     }
1435
1436     if (policy_surface->conformant)
1437         policy_surface->conformant = false;
1438
1439     struct ds_tizen_policy_surface_event_unset_conformant event = {
1440         .policy_surface = policy_surface,
1441     };
1442     wl_signal_emit_mutable(&policy_surface->events.unset_conformant, &event);
1443 }
1444
1445 static void
1446 policy_handle_get_conformant(struct wl_client *wl_client,
1447     struct wl_resource *resource, struct wl_resource *surface_resource)
1448 {
1449     struct ds_tizen_policy_surface *policy_surface;
1450
1451     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1452     if (policy_surface == NULL) {
1453         ds_err("tizen_policy_client_get_surface() failed.");
1454         wl_client_post_no_memory(wl_client);
1455         return;
1456     }
1457
1458     struct ds_tizen_policy_surface_event_get_conformant event = {
1459         .policy_surface = policy_surface,
1460     };
1461     wl_signal_emit_mutable(&policy_surface->events.get_conformant, &event);
1462 }
1463
1464 static void
1465 policy_handle_set_notification_level(struct wl_client *wl_client,
1466     struct wl_resource *resource, struct wl_resource *surface_resource,
1467     int32_t level)
1468 {
1469     struct ds_tizen_policy_surface *policy_surface;
1470     enum ds_tizen_policy_notification_level noti_level;
1471     bool ret;
1472
1473     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1474     if (policy_surface == NULL) {
1475         ds_err("tizen_policy_client_get_surface() failed.");
1476         wl_client_post_no_memory(wl_client);
1477         return;
1478     }
1479
1480     ret = tizen_security_check_privilege(policy_surface->pid, policy_surface->uid,
1481             TIZEN_POLICY_PRIVILEGE_SET_NOTIFICATION_LEVEL);
1482     if (ret == false) {
1483         ds_err("tizen_security_check_privilege() failed. "
1484             "Privilege Denied on set_notification_level.");
1485
1486         tizen_policy_send_notification_done(resource, surface_resource,
1487             -1, TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED);
1488
1489         return;
1490     }
1491
1492     switch (level) {
1493         case TIZEN_POLICY_LEVEL_1:
1494             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_1;
1495             break;
1496         case TIZEN_POLICY_LEVEL_2:
1497             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_2;
1498             break;
1499         case TIZEN_POLICY_LEVEL_3:
1500             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_3;
1501             break;
1502         case TIZEN_POLICY_LEVEL_NONE:
1503             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_NONE;
1504             break;
1505         case TIZEN_POLICY_LEVEL_DEFAULT:
1506             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_DEFAULT;
1507             break;
1508         case TIZEN_POLICY_LEVEL_MEDIUM:
1509             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_MEDIUM;
1510             break;
1511         case TIZEN_POLICY_LEVEL_HIGH:
1512             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_HIGH;
1513             break;
1514         case TIZEN_POLICY_LEVEL_TOP:
1515             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_TOP;
1516             break;
1517         default:
1518             noti_level = DS_TIZEN_POLICY_NOTIFICATION_LEVEL_NONE;
1519             break;
1520     }
1521
1522     struct ds_tizen_policy_surface_event_set_notification_level event = {
1523         .policy_surface = policy_surface,
1524         .level = noti_level,
1525     };
1526     wl_signal_emit_mutable(&policy_surface->events.set_notification_level, &event);
1527 }
1528
1529 static void
1530 policy_handle_set_transient_for(struct wl_client *wl_client,
1531     struct wl_resource *resource, uint32_t child_id, uint32_t parent_id)
1532 {
1533     struct ds_tizen_policy_client *client;
1534
1535     client = wl_resource_get_user_data(resource);
1536
1537     struct ds_tizen_policy_event_set_transient_for event = {
1538         .policy = client->policy,
1539         .child_universal_id = child_id,
1540         .parent_universal_id = parent_id,
1541     };
1542     wl_signal_emit_mutable(&client->policy->events.set_transient_for, &event);
1543
1544     tizen_policy_send_transient_for_done(resource, child_id);
1545 }
1546
1547 static void
1548 policy_handle_unset_transient_for(struct wl_client *wl_client,
1549     struct wl_resource *resource, uint32_t child_id)
1550 {
1551     struct ds_tizen_policy_client *client;
1552
1553     client = wl_resource_get_user_data(resource);
1554
1555     struct ds_tizen_policy_event_unset_transient_for event = {
1556         .policy = client->policy,
1557         .child_universal_id = child_id,
1558     };
1559     wl_signal_emit_mutable(&client->policy->events.unset_transient_for, &event);
1560 }
1561
1562 static void
1563 policy_handle_set_window_screen_mode(struct wl_client *wl_client,
1564     struct wl_resource *resource, struct wl_resource *surface_resource,
1565     uint32_t mode)
1566 {
1567     struct ds_tizen_policy_surface *policy_surface;
1568     enum ds_tizen_policy_window_screen_mode screen_mode;
1569     bool ret;
1570
1571     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1572     if (policy_surface == NULL) {
1573         ds_err("tizen_policy_client_get_surface() failed.");
1574         wl_client_post_no_memory(wl_client);
1575         return;
1576     }
1577
1578     ret = tizen_security_check_privilege(policy_surface->pid, policy_surface->uid,
1579             TIZEN_POLICY_PRIVILEGE_SET_SCREEN_MODE);
1580     if (ret == false) {
1581         ds_err("tizen_security_check_privilege() failed. "
1582             "Privilege Denied on set_window_screen_mode.");
1583
1584         tizen_policy_send_notification_done(resource, surface_resource,
1585             -1, TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED);
1586
1587         return;
1588     }
1589
1590     switch (mode) {
1591         case TIZEN_POLICY_MODE_DEFAULT:
1592             screen_mode = DS_TIZEN_POLICY_WINDOW_SCREEN_MODE_DEFAULT;
1593             break;
1594         case TIZEN_POLICY_MODE_ALWAYS_ON:
1595             screen_mode = DS_TIZEN_POLICY_WINDOW_SCREEN_MODE_ALWAYS_ON;
1596             break;
1597         default:
1598             screen_mode = DS_TIZEN_POLICY_WINDOW_SCREEN_MODE_DEFAULT;
1599             break;
1600     }
1601
1602     struct ds_tizen_policy_surface_event_set_window_screen_mode event = {
1603         .policy_surface = policy_surface,
1604         .mode = screen_mode,
1605     };
1606     wl_signal_emit_mutable(&policy_surface->events.set_window_screen_mode, &event);
1607 }
1608
1609 static void
1610 policy_handle_place_subsurface_below_parent(struct wl_client *wl_client,
1611     struct wl_resource *resource, struct wl_resource *subsurface_resource)
1612 {
1613     struct ds_tizen_policy_client *client;
1614     struct ds_subsurface *subsurface;
1615
1616     client = wl_resource_get_user_data(resource);
1617     subsurface = ds_subsurface_from_resource(subsurface_resource);
1618
1619     struct ds_tizen_policy_event_place_subsurface_below_parent event = {
1620         .policy = client->policy,
1621         .subsurface = subsurface,
1622     };
1623     wl_signal_emit_mutable(
1624         &client->policy->events.place_subsurface_below_parent,
1625         &event);
1626 }
1627
1628 static void
1629 policy_handle_set_subsurface_stand_alone(struct wl_client *wl_client,
1630     struct wl_resource *resource, struct wl_resource *subsurface_resource)
1631 {
1632     struct ds_tizen_policy_client *client;
1633     struct ds_subsurface *subsurface;
1634
1635     client = wl_resource_get_user_data(resource);
1636     subsurface = ds_subsurface_from_resource(subsurface_resource);
1637
1638     struct ds_tizen_policy_event_set_subsurface_stand_alone event = {
1639         .policy = client->policy,
1640         .subsurface = subsurface,
1641     };
1642     wl_signal_emit_mutable(
1643         &client->policy->events.set_subsurface_stand_alone,
1644         &event);
1645 }
1646
1647 static void
1648 policy_handle_get_subsurface(struct wl_client *wl_client,
1649     struct wl_resource *resource, uint32_t id,
1650     struct wl_resource *surface_resource, uint32_t parent_id)
1651 {
1652     struct ds_tizen_policy_surface *policy_surface;
1653
1654     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1655     if (policy_surface == NULL) {
1656         ds_err("tizen_policy_client_get_surface() failed.");
1657         wl_client_post_no_memory(wl_client);
1658         return;
1659     }
1660
1661     // TODO: How to create the ds_subsurface with a parent_universal_id.
1662
1663     struct ds_tizen_policy_surface_event_get_subsurface event = {
1664         .policy_surface = policy_surface,
1665         .parent_universal_id = parent_id,
1666     };
1667     wl_signal_emit_mutable(&policy_surface->events.get_subsurface, &event);
1668 }
1669
1670 static void
1671 policy_handle_set_opaque_state(struct wl_client *wl_client,
1672     struct wl_resource *resource, struct wl_resource *surface_resource,
1673     int32_t state)
1674 {
1675     struct ds_tizen_policy_surface *policy_surface;
1676
1677     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1678     if (policy_surface == NULL) {
1679         ds_err("tizen_policy_client_get_surface() failed.");
1680         wl_client_post_no_memory(wl_client);
1681         return;
1682     }
1683
1684     if (policy_surface->opaque_state != state)
1685         policy_surface->opaque_state = state;
1686 }
1687
1688 static void
1689 policy_handle_iconify(struct wl_client *wl_client, struct wl_resource *resource,
1690     struct wl_resource *surface_resource)
1691 {
1692     struct ds_tizen_policy_surface *policy_surface;
1693
1694     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1695     if (policy_surface == NULL) {
1696         ds_err("tizen_policy_client_get_surface() failed.");
1697         wl_client_post_no_memory(wl_client);
1698         return;
1699     }
1700
1701     if (!policy_surface->iconified)
1702         policy_surface->iconified = true;
1703
1704     struct ds_tizen_policy_surface_event_iconify event = {
1705         .policy_surface = policy_surface,
1706     };
1707     wl_signal_emit_mutable(&policy_surface->events.iconify, &event);
1708 }
1709
1710 static void
1711 policy_handle_uniconify(struct wl_client *wl_client, struct wl_resource *resource,
1712     struct wl_resource *surface_resource)
1713 {
1714     struct ds_tizen_policy_surface *policy_surface;
1715
1716     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1717     if (policy_surface == NULL) {
1718         ds_err("tizen_policy_client_get_surface() failed.");
1719         wl_client_post_no_memory(wl_client);
1720         return;
1721     }
1722
1723     if (policy_surface->iconified)
1724         policy_surface->iconified = false;
1725
1726     struct ds_tizen_policy_surface_event_uniconify event = {
1727         .policy_surface = policy_surface,
1728     };
1729     wl_signal_emit_mutable(&policy_surface->events.uniconify, &event);
1730 }
1731
1732 static void
1733 policy_handle_add_aux_hint(struct wl_client *wl_client,
1734     struct wl_resource *resource, struct wl_resource *surface_resource,
1735     int32_t id, const char *name, const char *value)
1736 {
1737     struct ds_tizen_policy_surface *policy_surface;
1738
1739     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1740     if (policy_surface == NULL) {
1741         ds_err("tizen_policy_client_get_surface() failed.");
1742         wl_client_post_no_memory(wl_client);
1743         return;
1744     }
1745
1746     struct ds_tizen_policy_surface_event_add_aux_hint event = {
1747         .policy_surface = policy_surface,
1748         .id = id,
1749         .name = name,
1750         .value = value,
1751     };
1752     wl_signal_emit_mutable(&policy_surface->events.add_aux_hint, &event);
1753 }
1754
1755 static void
1756 policy_handle_change_aux_hint(struct wl_client *wl_client,
1757     struct wl_resource *resource, struct wl_resource *surface_resource,
1758     int32_t id, const char *value)
1759 {
1760     struct ds_tizen_policy_surface *policy_surface;
1761
1762     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1763     if (policy_surface == NULL) {
1764         ds_err("tizen_policy_client_get_surface() failed.");
1765         wl_client_post_no_memory(wl_client);
1766         return;
1767     }
1768
1769     struct ds_tizen_policy_surface_event_change_aux_hint event = {
1770         .policy_surface = policy_surface,
1771         .id = id,
1772         .value = value,
1773     };
1774     wl_signal_emit_mutable(&policy_surface->events.change_aux_hint, &event);
1775 }
1776
1777 static void
1778 policy_handle_delete_aux_hint(struct wl_client *wl_client,
1779     struct wl_resource *resource, struct wl_resource *surface_resource,
1780     int32_t id)
1781 {
1782     struct ds_tizen_policy_surface *policy_surface;
1783
1784     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1785     if (policy_surface == NULL) {
1786         ds_err("tizen_policy_client_get_surface() failed.");
1787         wl_client_post_no_memory(wl_client);
1788         return;
1789     }
1790
1791     struct ds_tizen_policy_surface_event_delete_aux_hint event = {
1792         .policy_surface = policy_surface,
1793         .id = id,
1794     };
1795     wl_signal_emit_mutable(&policy_surface->events.delete_aux_hint, &event);
1796 }
1797
1798 static void
1799 policy_handle_get_supported_aux_hints(struct wl_client *wl_client,
1800     struct wl_resource *resource, struct wl_resource *surface_resource)
1801 {
1802     struct ds_tizen_policy_surface *policy_surface;
1803
1804     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1805     if (policy_surface == NULL) {
1806         ds_err("tizen_policy_client_get_surface() failed.");
1807         wl_client_post_no_memory(wl_client);
1808         return;
1809     }
1810
1811     struct ds_tizen_policy_surface_event_get_supported_aux_hints event = {
1812         .policy_surface = policy_surface,
1813     };
1814     wl_signal_emit_mutable(&policy_surface->events.get_supported_aux_hints, &event);
1815 }
1816
1817 static void
1818 policy_handle_set_background_state(struct wl_client *wl_client,
1819     struct wl_resource *resource, uint32_t pid)
1820 {
1821     struct ds_tizen_policy_client *client;
1822     struct ds_tizen_policy *policy;
1823
1824     client = wl_resource_get_user_data(resource);
1825     policy = client->policy;
1826
1827     wl_list_for_each(client, &policy->clients, link) {
1828         if (pid == (uint32_t)client->pid) {
1829             struct ds_tizen_policy_event_set_background_state event = {
1830                 .policy = client->policy,
1831                 .pid = pid,
1832             };
1833             wl_signal_emit_mutable(&client->policy->events.set_background_state, &event);
1834         }
1835     }
1836 }
1837
1838 static void
1839 policy_handle_unset_background_state(struct wl_client *wl_client,
1840     struct wl_resource *resource, uint32_t pid)
1841 {
1842     struct ds_tizen_policy_client *client;
1843     struct ds_tizen_policy *policy;
1844
1845     client = wl_resource_get_user_data(resource);
1846     policy = client->policy;
1847
1848     wl_list_for_each(client, &policy->clients, link) {
1849         if (pid == (uint32_t)client->pid) {
1850             struct ds_tizen_policy_event_unset_background_state event = {
1851                 .policy = client->policy,
1852                 .pid = pid,
1853             };
1854             wl_signal_emit_mutable(&client->policy->events.unset_background_state, &event);
1855         }
1856     }
1857 }
1858
1859 static void
1860 policy_handle_set_floating_mode(struct wl_client *wl_client,
1861     struct wl_resource *resource, struct wl_resource *surface_resource)
1862 {
1863     struct ds_tizen_policy_surface *policy_surface;
1864
1865     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1866     if (policy_surface == NULL) {
1867         ds_err("tizen_policy_client_get_surface() failed.");
1868         wl_client_post_no_memory(wl_client);
1869         return;
1870     }
1871
1872     if (policy_surface->floating_mode == false)
1873         return;
1874
1875     policy_surface->floating_mode = false;
1876
1877     struct ds_tizen_policy_surface_event_set_floating_mode event = {
1878         .policy_surface = policy_surface,
1879     };
1880     wl_signal_emit_mutable(&policy_surface->events.set_floating_mode, &event);
1881 }
1882
1883 static void
1884 policy_handle_unset_floating_mode(struct wl_client *wl_client,
1885     struct wl_resource *resource, struct wl_resource *surface_resource)
1886 {
1887     struct ds_tizen_policy_surface *policy_surface;
1888
1889     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1890     if (policy_surface == NULL) {
1891         ds_err("tizen_policy_client_get_surface() failed.");
1892         wl_client_post_no_memory(wl_client);
1893         return;
1894     }
1895
1896     if (policy_surface->floating_mode == false)
1897         return;
1898
1899     policy_surface->floating_mode = false;
1900
1901     struct ds_tizen_policy_surface_event_unset_floating_mode event = {
1902         .policy_surface = policy_surface,
1903     };
1904     wl_signal_emit_mutable(&policy_surface->events.unset_floating_mode, &event);
1905 }
1906
1907 static void
1908 policy_handle_set_stack_mode(struct wl_client *wl_client,
1909     struct wl_resource *resource, struct wl_resource *surface_resource,
1910     uint32_t mode)
1911 {
1912     struct ds_tizen_policy_surface *policy_surface;
1913     enum ds_tizen_policy_stack_mode stack_mode;
1914
1915     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1916     if (policy_surface == NULL) {
1917         ds_err("tizen_policy_client_get_surface() failed.");
1918         wl_client_post_no_memory(wl_client);
1919         return;
1920     }
1921
1922     switch (mode) {
1923         case TIZEN_POLICY_STACK_MODE_ABOVE:
1924             stack_mode = DS_TIZEN_POLICY_STACK_MODE_ABOVE;
1925             break;
1926         case TIZEN_POLICY_STACK_MODE_BELOW:
1927             stack_mode = DS_TIZEN_POLICY_STACK_MODE_BELOW;
1928             break;
1929         default:
1930             stack_mode = DS_TIZEN_POLICY_STACK_MODE_NONE;
1931             break;
1932     }
1933
1934     if (policy_surface->stack_mode == stack_mode)
1935         return;
1936
1937     policy_surface->stack_mode = stack_mode;
1938
1939     struct ds_tizen_policy_surface_event_set_stack_mode event = {
1940         .policy_surface = policy_surface,
1941         .mode = stack_mode,
1942     };
1943     wl_signal_emit_mutable(&policy_surface->events.set_stack_mode, &event);
1944 }
1945
1946 static void
1947 policy_handle_activate_above_by_res_id(struct wl_client *wl_client,
1948     struct wl_resource *resource, uint32_t universal_id,
1949     uint32_t above_universal_id)
1950 {
1951     struct ds_tizen_policy_client *client;
1952
1953     client = wl_resource_get_user_data(resource);
1954
1955     struct ds_tizen_policy_event_activate_above_by_universal_id event = {
1956         .policy = client->policy,
1957         .universal_id = universal_id,
1958         .above_universal_id = above_universal_id,
1959     };
1960     wl_signal_emit_mutable(&client->policy->events.activate_above_by_universal_id, &event);
1961 }
1962
1963 static void
1964 subsurface_watcher_handle_destroy(struct wl_client *wl_client,
1965     struct wl_resource *resource)
1966 {
1967    wl_resource_destroy(resource);
1968 }
1969
1970 static const struct tizen_subsurface_watcher_interface subsurface_watcher_impl =
1971 {
1972    subsurface_watcher_handle_destroy,
1973 };
1974
1975 static void
1976 _tizen_policy_subsurface_watcher_handle_destroy(struct wl_resource *resource)
1977 {
1978     struct ds_tizen_policy_subsurface_watcher *subsurface_watcher;
1979
1980     subsurface_watcher = wl_resource_get_user_data(resource);
1981
1982     ds_inf("_tizen_policy_subsurface_watcher_handle_destroy (subsurface_watcher:%p)",
1983         subsurface_watcher);
1984
1985     wl_signal_emit_mutable(&subsurface_watcher->events.destroy, subsurface_watcher);
1986     free(subsurface_watcher);
1987 }
1988
1989 static void
1990 policy_handle_get_subsurface_watcher(struct wl_client *wl_client,
1991     struct wl_resource *resource, uint32_t id,
1992     struct wl_resource *surface_resource)
1993 {
1994     struct ds_tizen_policy_surface *policy_surface;
1995     struct ds_tizen_policy_subsurface_watcher *subsurface_watcher;
1996
1997     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
1998     if (policy_surface == NULL) {
1999         ds_err("tizen_policy_client_get_surface() failed.");
2000         wl_client_post_no_memory(wl_client);
2001         return;
2002     }
2003
2004     subsurface_watcher = calloc(1, sizeof *subsurface_watcher);
2005     if (!subsurface_watcher) {
2006         ds_err("calloc() failed. tizen_policy");
2007         return;
2008     }
2009
2010     subsurface_watcher->policy_surface = policy_surface;
2011     wl_signal_init(&subsurface_watcher->events.destroy);
2012
2013     wl_list_insert(&policy_surface->subsurface_watchers, &subsurface_watcher->link);
2014
2015     subsurface_watcher->resource = wl_resource_create(wl_client,
2016         &tizen_subsurface_watcher_interface, wl_resource_get_version(resource),
2017         id);
2018     if (subsurface_watcher->resource == NULL) {
2019         ds_err("tizen_policy : wl_resource_create() failed.");
2020         free(subsurface_watcher);
2021         wl_client_post_no_memory(wl_client);
2022         return;
2023     }
2024
2025     wl_resource_set_implementation(subsurface_watcher->resource,
2026         &subsurface_watcher_impl, subsurface_watcher,
2027         _tizen_policy_subsurface_watcher_handle_destroy);
2028
2029     struct ds_tizen_policy_surface_event_new_subsurface_watcher event = {
2030         .policy_surface = policy_surface,
2031         .subsurface_watcher = subsurface_watcher,
2032     };
2033     wl_signal_emit_mutable(&policy_surface->events.new_subsurface_watcher, &event);
2034 }
2035
2036 static void
2037 policy_handle_set_parent(struct wl_client *wl_client,
2038     struct wl_resource *resource, struct wl_resource *surface_resource,
2039     struct wl_resource *parent_surface_resource)
2040 {
2041     struct ds_tizen_policy_surface *policy_surface;
2042     struct ds_surface *parent_surface;
2043
2044     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
2045     if (policy_surface == NULL) {
2046         ds_err("tizen_policy_client_get_surface() failed.");
2047         wl_client_post_no_memory(wl_client);
2048         return;
2049     }
2050
2051     parent_surface = ds_surface_from_resource(parent_surface_resource);
2052
2053     struct ds_tizen_policy_surface_event_set_parent event = {
2054         .policy_surface = policy_surface,
2055         .parent_surface = parent_surface,
2056     };
2057     wl_signal_emit_mutable(&policy_surface->events.set_parent, &event);
2058 }
2059
2060 static void
2061 policy_handle_ack_conformant_region(struct wl_client *wl_client,
2062     struct wl_resource *resource, struct wl_resource *surface_resource,
2063     uint32_t serial)
2064 {
2065     struct ds_tizen_policy_surface *policy_surface;
2066
2067     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
2068     if (policy_surface == NULL) {
2069         ds_err("tizen_policy_client_get_surface() failed.");
2070         wl_client_post_no_memory(wl_client);
2071         return;
2072     }
2073
2074     struct ds_tizen_policy_surface_event_ack_conformant_region event = {
2075         .policy_surface = policy_surface,
2076         .serial = serial,
2077     };
2078     wl_signal_emit_mutable(&policy_surface->events.ack_conformant_region, &event);
2079 }
2080
2081 static void
2082 policy_handle_destroy(struct wl_client *wl_client, struct wl_resource *resource)
2083 {
2084     struct ds_tizen_policy_client *client;
2085
2086     client = wl_resource_get_user_data(resource);
2087
2088     if (!wl_list_empty(&client->policy_surfaces)) {
2089         ds_err("tizen_policy was destroyed before children");
2090         return;
2091     }
2092
2093     wl_resource_destroy(resource);
2094 }
2095
2096 static void
2097 policy_handle_has_video(struct wl_client *wl_client,
2098     struct wl_resource *resource, struct wl_resource *surface_resource,
2099     uint32_t has)
2100 {
2101     struct ds_tizen_policy_surface *policy_surface;
2102
2103     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
2104     if (policy_surface == NULL) {
2105         ds_err("tizen_policy_client_get_surface() failed.");
2106         wl_client_post_no_memory(wl_client);
2107         return;
2108     }
2109
2110     if (policy_surface->video == has)
2111         return;
2112
2113     policy_surface->video = has;
2114
2115     struct ds_tizen_policy_surface_event_set_video event = {
2116         .policy_surface = policy_surface,
2117         .video = has,
2118     };
2119     wl_signal_emit_mutable(&policy_surface->events.set_video, &event);
2120 }
2121
2122 static void
2123 policy_handle_set_appid(struct wl_client *wl_client,
2124     struct wl_resource *resource, int32_t pid, const char *appid)
2125 {
2126     struct ds_tizen_policy_client *client;
2127
2128     client = wl_resource_get_user_data(resource);
2129
2130     struct ds_tizen_policy_event_set_appid event = {
2131         .policy = client->policy,
2132         .pid = pid,
2133         .appid = appid,
2134     };
2135     wl_signal_emit_mutable(&client->policy->events.set_appid, &event);
2136 }
2137
2138 static void
2139 policy_handle_show(struct wl_client *wl_client, struct wl_resource *resource,
2140     struct wl_resource *surface_resource)
2141 {
2142     struct ds_tizen_policy_surface *policy_surface;
2143
2144     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
2145     if (policy_surface == NULL) {
2146         ds_err("tizen_policy_client_get_surface() failed.");
2147         wl_client_post_no_memory(wl_client);
2148         return;
2149     }
2150
2151     struct ds_tizen_policy_surface_event_show event = {
2152         .policy_surface = policy_surface,
2153     };
2154     wl_signal_emit_mutable(&policy_surface->events.show, &event);
2155 }
2156
2157 static void
2158 policy_handle_hide(struct wl_client *wl_client, struct wl_resource *resource,
2159     struct wl_resource *surface_resource)
2160 {
2161     struct ds_tizen_policy_surface *policy_surface;
2162
2163     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
2164     if (policy_surface == NULL) {
2165         ds_err("tizen_policy_client_get_surface() failed.");
2166         wl_client_post_no_memory(wl_client);
2167         return;
2168     }
2169
2170     struct ds_tizen_policy_surface_event_hide event = {
2171         .policy_surface = policy_surface,
2172     };
2173     wl_signal_emit_mutable(&policy_surface->events.hide, &event);
2174 }
2175
2176 static void
2177 policy_handle_set_transient_for_below(struct wl_client *wl_client,
2178     struct wl_resource *resource, uint32_t universal_id,
2179     uint32_t parent_universal_id)
2180 {
2181     struct ds_tizen_policy_client *client;
2182
2183     client = wl_resource_get_user_data(resource);
2184
2185     struct ds_tizen_policy_event_set_transient_for_below event = {
2186         .policy = client->policy,
2187         .universal_id = universal_id,
2188         .parent_universal_id = parent_universal_id,
2189     };
2190     wl_signal_emit_mutable(&client->policy->events.set_transient_for_below, &event);
2191 }
2192
2193 static void
2194 policy_handle_set_parent_with_below(struct wl_client *wl_client,
2195     struct wl_resource *resource, struct wl_resource *surface_resource,
2196     struct wl_resource *parent_surface_resource)
2197 {
2198     struct ds_tizen_policy_surface *policy_surface;
2199     struct ds_surface *parent_surface;
2200
2201     policy_surface = tizen_policy_client_get_surface(resource, surface_resource);
2202     if (policy_surface == NULL) {
2203         ds_err("tizen_policy_client_get_surface() failed.");
2204         wl_client_post_no_memory(wl_client);
2205         return;
2206     }
2207
2208     parent_surface = ds_surface_from_resource(parent_surface_resource);
2209
2210     struct ds_tizen_policy_surface_event_set_parent_with_below event = {
2211         .policy_surface = policy_surface,
2212         .parent_surface = parent_surface,
2213     };
2214     wl_signal_emit_mutable(&policy_surface->events.set_parent_with_below, &event);
2215 }
2216
2217 static const struct tizen_policy_interface policy_impl =
2218 {
2219    .get_visibility = policy_handle_get_visibility,
2220    .get_position = policy_handle_get_position,
2221    .activate = policy_handle_activate,
2222    .activate_below_by_res_id = policy_handle_activate_below_by_res_id,
2223    .raise = policy_handle_raise,
2224    .lower = policy_handle_lower,
2225    .lower_by_res_id = policy_handle_lower_by_res_id,
2226    .set_focus_skip = policy_handle_set_focus_skip,
2227    .unset_focus_skip = policy_handle_unset_focus_skip,
2228    .set_role = policy_handle_set_role,
2229    .set_type = policy_handle_set_type,
2230    .set_conformant = policy_handle_set_conformant,
2231    .unset_conformant = policy_handle_unset_conformant,
2232    .get_conformant = policy_handle_get_conformant,
2233    .set_notification_level = policy_handle_set_notification_level,
2234    .set_transient_for = policy_handle_set_transient_for,
2235    .unset_transient_for = policy_handle_unset_transient_for,
2236    .set_window_screen_mode = policy_handle_set_window_screen_mode,
2237    .place_subsurface_below_parent = policy_handle_place_subsurface_below_parent,
2238    .set_subsurface_stand_alone = policy_handle_set_subsurface_stand_alone,
2239    .get_subsurface = policy_handle_get_subsurface,
2240    .set_opaque_state = policy_handle_set_opaque_state,
2241    .iconify = policy_handle_iconify,
2242    .uniconify = policy_handle_uniconify,
2243    .add_aux_hint = policy_handle_add_aux_hint,
2244    .change_aux_hint = policy_handle_change_aux_hint,
2245    .del_aux_hint = policy_handle_delete_aux_hint,
2246    .get_supported_aux_hints = policy_handle_get_supported_aux_hints,
2247    .set_background_state = policy_handle_set_background_state,
2248    .unset_background_state = policy_handle_unset_background_state,
2249    .set_floating_mode = policy_handle_set_floating_mode,
2250    .unset_floating_mode = policy_handle_unset_floating_mode,
2251    .set_stack_mode = policy_handle_set_stack_mode,
2252    .activate_above_by_res_id = policy_handle_activate_above_by_res_id,
2253    .get_subsurface_watcher = policy_handle_get_subsurface_watcher,
2254    .set_parent = policy_handle_set_parent,
2255    .ack_conformant_region = policy_handle_ack_conformant_region,
2256    .destroy = policy_handle_destroy,
2257    .has_video = policy_handle_has_video,
2258    .set_appid = policy_handle_set_appid,
2259    .show = policy_handle_show,
2260    .hide = policy_handle_hide,
2261    .set_transient_for_below = policy_handle_set_transient_for_below,
2262    .set_parent_with_below = policy_handle_set_parent_with_below,
2263 };
2264
2265 static void
2266 _tizen_policy_client_handle_destroy(struct wl_resource *resource)
2267 {
2268     struct ds_tizen_policy_client *client;
2269     struct ds_tizen_policy_surface *policy_surface, *tmp;
2270
2271     client = wl_resource_get_user_data(resource);
2272
2273     ds_inf("_tizen_policy_client_handle_destroy (client:%p)", client);
2274
2275     wl_list_for_each_safe(policy_surface, tmp, &client->policy_surfaces, link) {
2276         wl_signal_emit_mutable(&policy_surface->events.destroy, policy_surface);
2277         wl_list_remove(&policy_surface->link);
2278         free(policy_surface);
2279     }
2280
2281     wl_list_remove(&client->link);
2282     free(client);
2283 }
2284
2285 static void
2286 policy_bind(struct wl_client *wl_client, void *data, uint32_t version,
2287         uint32_t id)
2288 {
2289     struct ds_tizen_policy *policy = data;
2290     struct ds_tizen_policy_client *client;
2291
2292     client = calloc(1, sizeof *client);
2293     if (client == NULL) {
2294         ds_err("calloc() failed. tizen_policy");
2295         wl_client_post_no_memory(wl_client);
2296         return;
2297     }
2298
2299     ds_inf("tizen_policy_client binds. (client:%p)", client);
2300
2301     client->policy = policy;
2302     client->wl_client = wl_client;
2303     wl_client_get_credentials(client->wl_client, &client->pid, &client->uid, NULL);
2304
2305     wl_list_init(&client->policy_surfaces);
2306
2307     client->resource = wl_resource_create(wl_client, &tizen_policy_interface,
2308             MIN(version, TIZEN_POLICY_VERSION), id);
2309
2310     if (client->resource == NULL) {
2311         ds_err("tizen_policy : wl_resource_create() failed.");
2312         free(client);
2313         wl_client_post_no_memory(wl_client);
2314         return;
2315     }
2316
2317     wl_resource_set_implementation(client->resource, &policy_impl, client,
2318             _tizen_policy_client_handle_destroy);
2319
2320     wl_list_insert(&policy->clients, &client->link);
2321 }