2 #include <libweston/zalloc.h>
5 #include "shared/helpers.h"
6 #include "ivi-compositor.h"
9 #include "agl-shell-policy-server-protocol.h"
11 static struct wl_global *
12 ivi_policy_proto_create(struct ivi_compositor *ivi, struct ivi_policy *policy);
15 ivi_policy_remove_state_event(struct state_event *st_ev)
18 wl_list_remove(&st_ev->link);
23 ivi_policy_destroy_state_event(struct wl_list *list)
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);
30 static struct state_event *
31 ivi_policy_state_event_create(uint32_t val, const char *value)
33 struct state_event *ev_st = zalloc(sizeof(*ev_st));
34 size_t value_len = strlen(value);
37 ev_st->name = zalloc(sizeof(char) * value_len + 1);
38 memcpy(ev_st->name, value, value_len);
44 ivi_policy_add_state(struct wl_client *client,
45 struct wl_resource *res, uint32_t state, const char *value)
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);
53 ivi_policy_add_event(struct wl_client *client,
54 struct wl_resource *res, uint32_t ev, const char *value)
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);
61 /* note, if protocol modifications, adapt here as well */
63 ivi_policy_add_default_states(struct ivi_policy *policy)
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);
73 /* note, if protocol modifications, adapt here as well */
75 ivi_policy_add_default_events(struct ivi_policy *policy)
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);
86 ivi_policy_try_event(struct ivi_a_policy *a_policy)
88 struct ivi_policy *policy = a_policy->policy;
90 if (policy->api.policy_rule_try_event)
91 return policy->api.policy_rule_try_event(a_policy);
95 ivi_policy_try_event_timeout(void *user_data)
97 struct ivi_a_policy *a_policy = user_data;
98 ivi_policy_try_event(a_policy);
103 ivi_policy_setup_event_timeout(struct ivi_policy *ivi_policy,
104 struct ivi_a_policy *a_policy)
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);
110 a_policy->timer = wl_event_loop_add_timer(loop,
111 ivi_policy_try_event_timeout,
114 wl_event_source_timer_update(a_policy->timer, a_policy->timeout);
118 ivi_policy_check_policies(struct wl_listener *listener, void *data)
120 struct ivi_a_policy *a_policy;
121 struct ivi_policy *ivi_policy =
122 wl_container_of(listener, ivi_policy, listener_check_policies);
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,
132 ivi_policy_try_event(a_policy);
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);
143 * The generic way would be the following:
145 * - 'car' is in 'state' ->
146 * { do 'event' for app 'app_id' at 'timeout' time if same state as 'car_state' }
148 * a 0 timeout means, immediately, a timeout > 0, means to install timer an
149 * execute when timeout expires
151 * The following happens:
152 * 'car' changes its state -> verify what policy needs to be run
153 * 'car' in same state -> no action
157 ivi_policy_create(struct ivi_compositor *ivi,
158 const struct ivi_policy_api *api, void *user_data)
160 struct ivi_policy *policy = zalloc(sizeof(*policy));
162 policy->user_data = user_data;
164 policy->state_change_in_progress = false;
166 policy->api.struct_size =
167 MIN(sizeof(struct ivi_policy_api), api->struct_size);
168 memcpy(&policy->api, api, policy->api.struct_size);
170 policy->policy_shell = ivi_policy_proto_create(ivi, policy);
171 if (!policy->policy_shell) {
176 wl_signal_init(&policy->signal_state_change);
178 policy->listener_check_policies.notify = ivi_policy_check_policies;
179 wl_signal_add(&policy->signal_state_change,
180 &policy->listener_check_policies);
182 policy->current_state = AGL_SHELL_POLICY_STATE_INVALID;
183 policy->previous_state = AGL_SHELL_POLICY_STATE_INVALID;
185 wl_list_init(&policy->policies);
186 wl_list_init(&policy->events);
187 wl_list_init(&policy->states);
189 /* add the default states and enums */
190 ivi_policy_add_default_states(policy);
191 ivi_policy_add_default_events(policy);
197 ivi_policy_destroy(struct ivi_policy *ivi_policy)
199 struct ivi_a_policy *a_policy, *a_policy_tmp;
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);
211 ivi_policy_destroy_state_event(&ivi_policy->states);
212 ivi_policy_destroy_state_event(&ivi_policy->events);
214 if (ivi_policy->policy_shell)
215 wl_global_destroy(ivi_policy->policy_shell);
220 /* verifies if the state is one has been added */
222 ivi_policy_state_is_known(uint32_t state, struct ivi_policy *policy)
224 struct state_event *ev_st;
226 wl_list_for_each(ev_st, &policy->states, link) {
227 if (ev_st->value == state) {
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)
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));
245 struct ivi_output *output = to_ivi_output(woutput);
246 struct ivi_policy *policy = wl_resource_get_user_data(res);
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");
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");
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");
275 c_policy = zalloc(sizeof(*c_policy));
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);
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;
287 wl_list_insert(&policy->policies, &c_policy->link);
290 /* we start with 'invalid' state, so a initial state to even 'stop' should
291 * trigger a check of policies
294 ivi_policy_state_change(struct wl_client *client, struct wl_resource *res,
297 struct ivi_policy *policy = wl_resource_get_user_data(res);
298 bool found_state = false;
301 weston_log("Failed to retrieve policy!\n");
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);
313 /* if we don't know the state, make sure it is first added */
314 found_state = ivi_policy_state_is_known(state, policy);
316 agl_shell_policy_send_done(policy->resource,
317 AGL_SHELL_POLICY_STATE_INVALID);
321 /* current_state is actually the new state */
322 policy->current_state = state;
324 /* signal that we need to check the current policies */
325 wl_signal_emit(&policy->signal_state_change, policy);
328 static const struct agl_shell_policy_interface ivi_policy_interface = {
329 ivi_policy_add_state,
330 ivi_policy_add_event,
332 ivi_policy_state_change,
336 ivi_policy_bind(struct wl_client *client, void *data,
337 uint32_t version, uint32_t id)
339 struct ivi_policy *ivi_policy = data;
340 struct wl_resource *resource;
342 resource = wl_resource_create(client, &agl_shell_policy_interface,
345 wl_client_post_no_memory(client);
349 wl_resource_set_implementation(resource, &ivi_policy_interface,
351 ivi_policy->resource = resource;
354 static struct wl_global *
355 ivi_policy_proto_create(struct ivi_compositor *ivi, struct ivi_policy *policy)
357 struct wl_global *policy_global = NULL;
362 policy_global = wl_global_create(ivi->compositor->wl_display,
363 &agl_shell_policy_interface, 1,
364 policy, ivi_policy_bind);
366 return policy_global;