78994fa0ecae140ce629e25474afe78a7fee4964
[src/agl-compositor.git] / src / policy.c
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 #include <string.h>
27 #include <libweston/zalloc.h>
28 #include <assert.h>
29
30 #include "shared/helpers.h"
31 #include "ivi-compositor.h"
32
33 #include "policy.h"
34
35 static void
36 ivi_policy_remove_state_event(struct state_event *st_ev)
37 {
38         free(st_ev->name);
39         wl_list_remove(&st_ev->link);
40         free(st_ev);
41 }
42
43 static void
44 ivi_policy_destroy_state_event(struct wl_list *list)
45 {
46         struct state_event *st_ev, *tmp_st_ev;
47         wl_list_for_each_safe(st_ev, tmp_st_ev, list, link)
48                 ivi_policy_remove_state_event(st_ev);
49 }
50
51 static struct state_event *
52 ivi_policy_state_event_create(uint32_t val, const char *value)
53 {
54         struct state_event *ev_st = zalloc(sizeof(*ev_st));
55         size_t value_len = strlen(value);
56
57         ev_st->value = val;
58         ev_st->name = zalloc(sizeof(char) * value_len + 1);
59         memcpy(ev_st->name, value, value_len);
60
61         return ev_st;
62 }
63
64 void
65 ivi_policy_add_state(struct ivi_policy *policy, uint32_t state, const char *value)
66 {
67         struct state_event *ev_st;
68         if (!policy)
69                 return;
70
71         ev_st = ivi_policy_state_event_create(state, value);
72         wl_list_insert(&policy->states, &ev_st->link);
73 }
74
75 void
76 ivi_policy_add_event(struct ivi_policy *policy, uint32_t ev, const char *value)
77 {
78         struct state_event *ev_st;
79         if (!policy)
80                 return;
81
82         ev_st = ivi_policy_state_event_create(ev, value);
83         wl_list_insert(&policy->events, &ev_st->link);
84 }
85
86 static void
87 ivi_policy_add_default_states(struct ivi_policy *policy)
88 {
89         const char *default_states[] = { "invalid", "start", "stop", "reverse" };
90         if (!policy)
91                 return;
92
93         for (uint32_t i = 0; i < ARRAY_LENGTH(default_states); i ++) {
94                 struct state_event *ev_st =
95                         ivi_policy_state_event_create(i, default_states[i]);
96                 wl_list_insert(&policy->states, &ev_st->link);
97         }
98 }
99
100 static void
101 ivi_policy_add_default_events(struct ivi_policy *policy)
102 {
103         const char *default_events[] = { "show", "hide" };
104         if (!policy)
105                 return;
106
107         for (uint32_t i = 0; i < ARRAY_LENGTH(default_events); i ++) {
108                 struct state_event *ev_st =
109                         ivi_policy_state_event_create(i, default_events[i]);
110                 wl_list_insert(&policy->events, &ev_st->link);
111         }
112 }
113
114 static void
115 ivi_policy_try_event(struct ivi_a_policy *a_policy)
116 {
117         struct ivi_policy *policy = a_policy->policy;
118
119         if (policy->api.policy_rule_try_event)
120             return policy->api.policy_rule_try_event(a_policy);
121 }
122
123 static int
124 ivi_policy_try_event_timeout(void *user_data)
125 {
126         struct ivi_a_policy *a_policy = user_data;
127         ivi_policy_try_event(a_policy);
128         return 0;
129 }
130
131 static void
132 ivi_policy_setup_event_timeout(struct ivi_policy *ivi_policy,
133                                struct ivi_a_policy *a_policy)
134 {
135         struct ivi_compositor *ivi = ivi_policy->ivi;
136         struct wl_display *wl_display = ivi->compositor->wl_display;
137         struct wl_event_loop *loop = wl_display_get_event_loop(wl_display);
138
139         a_policy->timer = wl_event_loop_add_timer(loop,
140                                                   ivi_policy_try_event_timeout,
141                                                   a_policy);
142
143         wl_event_source_timer_update(a_policy->timer, a_policy->timeout);
144 }
145
146 static void
147 ivi_policy_check_policies(struct wl_listener *listener, void *data)
148 {
149         struct ivi_a_policy *a_policy;
150         struct ivi_policy *ivi_policy =
151                 wl_container_of(listener, ivi_policy, listener_check_policies);
152
153         ivi_policy->state_change_in_progress = true;
154         wl_list_for_each(a_policy, &ivi_policy->policies, link) {
155                 if (ivi_policy->current_state == a_policy->state) {
156                         /* check the timeout first to see if there's a timeout */
157                         if (a_policy->timeout > 0)
158                                 ivi_policy_setup_event_timeout(ivi_policy,
159                                                                a_policy);
160                         else
161                                 ivi_policy_try_event(a_policy);
162                 }
163         }
164
165         ivi_policy->previous_state = ivi_policy->current_state;
166         ivi_policy->state_change_in_progress = false;
167 }
168
169
170 struct ivi_policy *
171 ivi_policy_create(struct ivi_compositor *ivi,
172                   const struct ivi_policy_api *api, void *user_data)
173 {
174         struct ivi_policy *policy = zalloc(sizeof(*policy));
175
176         policy->user_data = user_data;
177         policy->ivi = ivi;
178         policy->state_change_in_progress = false;
179
180         policy->api.struct_size =
181                 MIN(sizeof(struct ivi_policy_api), api->struct_size);
182         /* install the hooks */
183         memcpy(&policy->api, api, policy->api.struct_size);
184
185         /* to trigger a check for policies use */
186         wl_signal_init(&policy->signal_state_change);
187
188         policy->listener_check_policies.notify = ivi_policy_check_policies;
189         wl_signal_add(&policy->signal_state_change,
190                       &policy->listener_check_policies);
191
192         policy->current_state = AGL_SHELL_POLICY_STATE_INVALID;
193         policy->previous_state = AGL_SHELL_POLICY_STATE_INVALID;
194
195         /* policy rules */
196         wl_list_init(&policy->policies);
197
198         wl_list_init(&policy->events);
199         wl_list_init(&policy->states);
200
201         /* add the default states and enums */
202         ivi_policy_add_default_states(policy);
203         ivi_policy_add_default_events(policy);
204
205         return policy;
206 }
207
208 void
209 ivi_policy_destroy(struct ivi_policy *ivi_policy)
210 {
211         struct ivi_a_policy *a_policy, *a_policy_tmp;
212
213         if (!ivi_policy)
214                 return;
215
216         wl_list_for_each_safe(a_policy, a_policy_tmp,
217                               &ivi_policy->policies, link) {
218                 free(a_policy->app_id);
219                 wl_list_remove(&a_policy->link);
220                 free(a_policy);
221         }
222
223         ivi_policy_destroy_state_event(&ivi_policy->states);
224         ivi_policy_destroy_state_event(&ivi_policy->events);
225
226         free(ivi_policy);
227 }
228
229
230 /* verifies if the state is one has been added */
231 static bool
232 ivi_policy_state_is_known(uint32_t state, struct ivi_policy *policy)
233 {
234         struct state_event *ev_st;
235
236         wl_list_for_each(ev_st, &policy->states, link) {
237                 if (ev_st->value == state) {
238                         return true;
239                 }
240         }
241         return false;
242 }
243
244 /*
245  * The generic way would be the following:
246  *
247  * - 'car' is in 'state' ->
248  *      { do 'event' for app 'app_id' at 'timeout' time if same state as 'car_state' }
249  *
250  * a 0 timeout means, immediately, a timeout > 0, means to install timer an
251  * execute when timeout expires
252  *
253  * The following happens:
254  * 'car' changes its state -> verify what policy needs to be run
255  * 'car' in same state -> no action
256  *
257  */
258 int
259 ivi_policy_add(struct ivi_policy *policy, const char *app_id, uint32_t state,
260                uint32_t event, uint32_t timeout, struct wl_resource *output_res)
261 {
262         size_t app_id_len;
263         struct weston_head *head = weston_head_from_resource(output_res);
264         struct weston_output *woutput = weston_head_get_output(head);
265         struct ivi_output *output = to_ivi_output(woutput);
266         struct ivi_a_policy *a_policy;
267
268         if (!policy) {
269                 weston_log("Failed to retrieve policy!\n");
270                 return -1;
271         }
272
273         a_policy = zalloc(sizeof(*a_policy));
274         if (!a_policy)
275                 return -1;
276
277         if (policy->state_change_in_progress)
278                 return -1;
279
280         /* we should be allow to do this in the first place, only if the
281          * hooks allows us to  */
282         if (policy->api.policy_rule_allow_to_add &&
283             !policy->api.policy_rule_allow_to_add(policy))
284                 return -1;
285
286         if (!ivi_policy_state_is_known(state, policy))
287                 return -1;
288
289         app_id_len = strlen(app_id);
290         a_policy->app_id = zalloc(sizeof(char) * app_id_len + 1);
291         memcpy(a_policy->app_id, app_id, app_id_len);
292
293         a_policy->state = state;
294         a_policy->event = event;
295         a_policy->timeout = timeout;
296         a_policy->output = output;
297         a_policy->policy = policy;
298
299         wl_list_insert(&policy->policies, &a_policy->link);
300
301         return 0;
302 }
303
304 /* we start with 'invalid' state, so a initial state to even 'stop' should
305  * trigger a check of policies
306  */
307 int
308 ivi_policy_state_change(struct ivi_policy *policy, uint32_t state)
309 {
310         bool found_state = false;
311         if (!policy) {
312                 weston_log("Failed to retrieve policy!\n");
313                 return -1;
314         }
315
316         if (policy->current_state == state) {
317                 return -1;
318         }
319
320         /* if we don't know the state, make sure it is first added */
321         found_state = ivi_policy_state_is_known(state, policy);
322         if (!found_state) {
323                 return -1;
324         }
325
326         /* current_state is actually the new state */
327         policy->current_state = state;
328
329         /* signal that we need to check the current policies */
330         wl_signal_emit(&policy->signal_state_change, policy);
331
332         return 0;
333 }