2 * Copyright © 2020 Collabora, Ltd.
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:
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.
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
27 #include <libweston/zalloc.h>
30 #include "shared/helpers.h"
31 #include "ivi-compositor.h"
36 ivi_policy_remove_state_event(struct state_event *st_ev)
39 wl_list_remove(&st_ev->link);
44 ivi_policy_destroy_state_event(struct wl_list *list)
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);
51 static struct state_event *
52 ivi_policy_state_event_create(uint32_t val, const char *value)
54 struct state_event *ev_st = zalloc(sizeof(*ev_st));
55 size_t value_len = strlen(value);
58 ev_st->name = zalloc(sizeof(char) * value_len + 1);
59 memcpy(ev_st->name, value, value_len);
65 ivi_policy_add_state(struct ivi_policy *policy, uint32_t state, const char *value)
67 struct state_event *ev_st;
71 ev_st = ivi_policy_state_event_create(state, value);
72 wl_list_insert(&policy->states, &ev_st->link);
76 ivi_policy_add_event(struct ivi_policy *policy, uint32_t ev, const char *value)
78 struct state_event *ev_st;
82 ev_st = ivi_policy_state_event_create(ev, value);
83 wl_list_insert(&policy->events, &ev_st->link);
87 ivi_policy_add_default_states(struct ivi_policy *policy)
89 const char *default_states[] = { "invalid", "start", "stop", "reverse" };
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);
101 ivi_policy_add_default_events(struct ivi_policy *policy)
103 const char *default_events[] = { "show", "hide" };
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);
115 ivi_policy_try_event(struct ivi_a_policy *a_policy)
117 struct ivi_policy *policy = a_policy->policy;
119 if (policy->api.policy_rule_try_event)
120 return policy->api.policy_rule_try_event(a_policy);
124 ivi_policy_try_event_timeout(void *user_data)
126 struct ivi_a_policy *a_policy = user_data;
127 ivi_policy_try_event(a_policy);
132 ivi_policy_setup_event_timeout(struct ivi_policy *ivi_policy,
133 struct ivi_a_policy *a_policy)
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);
139 a_policy->timer = wl_event_loop_add_timer(loop,
140 ivi_policy_try_event_timeout,
143 wl_event_source_timer_update(a_policy->timer, a_policy->timeout);
147 ivi_policy_check_policies(struct wl_listener *listener, void *data)
149 struct ivi_a_policy *a_policy;
150 struct ivi_policy *ivi_policy =
151 wl_container_of(listener, ivi_policy, listener_check_policies);
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,
161 ivi_policy_try_event(a_policy);
165 ivi_policy->previous_state = ivi_policy->current_state;
166 ivi_policy->state_change_in_progress = false;
171 ivi_policy_create(struct ivi_compositor *ivi,
172 const struct ivi_policy_api *api, void *user_data)
174 struct ivi_policy *policy = zalloc(sizeof(*policy));
176 policy->user_data = user_data;
178 policy->state_change_in_progress = false;
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);
185 /* to trigger a check for policies use */
186 wl_signal_init(&policy->signal_state_change);
188 policy->listener_check_policies.notify = ivi_policy_check_policies;
189 wl_signal_add(&policy->signal_state_change,
190 &policy->listener_check_policies);
192 policy->current_state = AGL_SHELL_POLICY_STATE_INVALID;
193 policy->previous_state = AGL_SHELL_POLICY_STATE_INVALID;
196 wl_list_init(&policy->policies);
198 wl_list_init(&policy->events);
199 wl_list_init(&policy->states);
201 /* add the default states and enums */
202 ivi_policy_add_default_states(policy);
203 ivi_policy_add_default_events(policy);
209 ivi_policy_destroy(struct ivi_policy *ivi_policy)
211 struct ivi_a_policy *a_policy, *a_policy_tmp;
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);
223 ivi_policy_destroy_state_event(&ivi_policy->states);
224 ivi_policy_destroy_state_event(&ivi_policy->events);
230 /* verifies if the state is one has been added */
232 ivi_policy_state_is_known(uint32_t state, struct ivi_policy *policy)
234 struct state_event *ev_st;
236 wl_list_for_each(ev_st, &policy->states, link) {
237 if (ev_st->value == state) {
245 * The generic way would be the following:
247 * - 'car' is in 'state' ->
248 * { do 'event' for app 'app_id' at 'timeout' time if same state as 'car_state' }
250 * a 0 timeout means, immediately, a timeout > 0, means to install timer an
251 * execute when timeout expires
253 * The following happens:
254 * 'car' changes its state -> verify what policy needs to be run
255 * 'car' in same state -> no action
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)
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;
269 weston_log("Failed to retrieve policy!\n");
273 a_policy = zalloc(sizeof(*a_policy));
277 if (policy->state_change_in_progress)
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))
286 if (!ivi_policy_state_is_known(state, policy))
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);
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;
299 wl_list_insert(&policy->policies, &a_policy->link);
304 /* we start with 'invalid' state, so a initial state to even 'stop' should
305 * trigger a check of policies
308 ivi_policy_state_change(struct ivi_policy *policy, uint32_t state)
310 bool found_state = false;
312 weston_log("Failed to retrieve policy!\n");
316 if (policy->current_state == state) {
320 /* if we don't know the state, make sure it is first added */
321 found_state = ivi_policy_state_is_known(state, policy);
326 /* current_state is actually the new state */
327 policy->current_state = state;
329 /* signal that we need to check the current policies */
330 wl_signal_emit(&policy->signal_state_change, policy);