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