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