policy: Add a new policy hook for deactivating apps
[src/agl-compositor.git] / src / policy.h
1 /*
2  * Copyright © 2020 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #ifndef POLICY_H
27 #define POLICY_H
28
29 #include "ivi-compositor.h"
30
31 /* default state, invalid should at least be in order
32  * to signal states */
33 #ifndef AGL_SHELL_POLICY_STATE_INVALID
34 #define AGL_SHELL_POLICY_STATE_INVALID 0
35 #endif
36
37 /* default events */
38 #ifndef AGL_SHELL_POLICY_EVENT_SHOW
39 #define AGL_SHELL_POLICY_EVENT_SHOW 0
40 #endif
41
42 #ifndef AGL_SHELL_POLICY_EVENT_HIDE
43 #define AGL_SHELL_POLICY_EVENT_HIDE 1
44 #endif
45
46 struct ivi_policy;
47
48 struct state_event {
49         uint32_t value;
50         char *name;
51         struct wl_list link;    /* ivi_policy::states or ivi_policy::events */
52 };
53
54 struct ivi_a_policy {
55         struct ivi_policy *policy;
56
57         char *app_id;
58         uint32_t state;
59         uint32_t event;
60         uint32_t timeout;
61         struct ivi_output *output;
62         struct wl_event_source *timer;  /* for policies that have a timeout */
63
64         struct wl_list link;    /* ivi_policy::ivi_policies */
65 };
66
67 struct ivi_policy_api {
68         size_t struct_size;
69
70         bool (*surface_create)(struct ivi_surface *surf, void *user_data);
71         bool (*surface_commited)(struct ivi_surface *surf, void *user_data);
72         bool (*surface_activate)(struct ivi_surface *surf, void *user_data);
73         bool (*surface_deactivate)(struct ivi_surface *surf, void *user_data);
74
75         bool (*surface_activate_by_default)(struct ivi_surface *surf, void *user_data);
76
77         /** see also ivi_policy_add(). If set this will be executed before
78          * adding a new policy rule  */
79         bool (*policy_rule_allow_to_add)(void *user_data);
80
81         /** this callback will be executed when a there's a policy state change */
82         void (*policy_rule_try_event)(struct ivi_a_policy *a_policy);
83 };
84
85 struct ivi_policy {
86         struct ivi_compositor *ivi;
87         /* user-defined hooks */
88         struct ivi_policy_api api;
89         void *user_data;
90
91         /* represents the policy rules */
92         struct wl_list policies;        /* ivi_a_policy::link */
93
94         /* no state update chnage is being done as long as we have the same
95          * state */
96         uint32_t current_state;
97         uint32_t previous_state;
98
99         /* guards against current in change in progress */
100         bool state_change_in_progress;
101
102         /* additional states which can be verified in
103          * ivi_policy_api::policy_rule_try_event() */
104         struct wl_list states;  /* state_event::link */
105         struct wl_list events;  /* state_event::link */
106
107         /* necessary to for signaling the state change */
108         struct wl_listener listener_check_policies;
109         struct wl_signal signal_state_change;
110 };
111
112
113 /** Initialize the policy setup
114  *
115  * Policy engine should call ivi_policy_create() with its own ivi_policy_api
116  * setup.
117  */
118 struct ivi_policy *
119 ivi_policy_create(struct ivi_compositor *compositor,
120                   const struct ivi_policy_api *api, void *user_data);
121
122 /** Destroys the policy setup
123  *
124  */
125 void
126 ivi_policy_destroy(struct ivi_policy *ivi_policy);
127
128 /** Add a policy rule.
129  *
130  * ivi_policy_api::policy_rule_allow_to_add() can be used to limit adding
131  * policy rules.
132  *
133  * Returns 0 in case of success, or -1 in case of failure.
134  *
135  */
136 int
137 ivi_policy_add(struct ivi_policy *policy, const char *app_id, uint32_t state,
138                uint32_t event, uint32_t timeout, struct wl_resource *output_res);
139
140 /** Trigger a state change. This should be called **each time** there is a need
141  * to apply the policy rules.
142  *
143  * Returns 0 in case of success, or -1 in case of failure.
144  */
145 int
146 ivi_policy_state_change(struct ivi_policy *policy, uint32_t state);
147
148
149 /** Add a new state. The state can be verified in ivi_policy_api::policy_rule_try_event()
150  *
151  */
152 void
153 ivi_policy_add_state(struct ivi_policy *policy, uint32_t state, const char *value);
154
155 /** Add a new event. The event can be verified in ivi_policy_api::policy_rule_try_event()
156  *
157  */
158 void
159 ivi_policy_add_event(struct ivi_policy *policy, uint32_t state, const char *value);
160
161 /** Initialize the policy. Not implemented.
162  *
163  * Should be implemented by the policy engine. A single policy engine can be used
164  * at one time.
165  *
166  * Returns 0 in case of success, or -1 in case of failure.
167  */
168 int
169 ivi_policy_init(struct ivi_compositor *ivi);
170
171 #endif