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);
60 ev_st->name = zalloc(sizeof(char) * value_len + 1);
65 memcpy(ev_st->name, value, value_len);
71 ivi_policy_add_state(struct ivi_policy *policy, uint32_t state, const char *value)
73 struct state_event *ev_st;
77 ev_st = ivi_policy_state_event_create(state, value);
80 wl_list_insert(&policy->states, &ev_st->link);
84 ivi_policy_add_event(struct ivi_policy *policy, uint32_t ev, const char *value)
86 struct state_event *ev_st;
90 ev_st = ivi_policy_state_event_create(ev, value);
93 wl_list_insert(&policy->events, &ev_st->link);
97 ivi_policy_add_default_states(struct ivi_policy *policy)
99 const char *default_states[] = { "invalid", "start", "stop", "reverse" };
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]);
108 wl_list_insert(&policy->states, &ev_st->link);
113 ivi_policy_add_default_events(struct ivi_policy *policy)
115 const char *default_events[] = { "show", "hide" };
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]);
124 wl_list_insert(&policy->events, &ev_st->link);
129 ivi_policy_try_event(struct ivi_a_policy *a_policy)
131 struct ivi_policy *policy = a_policy->policy;
133 if (policy->api.policy_rule_try_event)
134 return policy->api.policy_rule_try_event(a_policy);
138 ivi_policy_try_event_timeout(void *user_data)
140 struct ivi_a_policy *a_policy = user_data;
141 ivi_policy_try_event(a_policy);
146 ivi_policy_setup_event_timeout(struct ivi_policy *ivi_policy,
147 struct ivi_a_policy *a_policy)
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);
153 a_policy->timer = wl_event_loop_add_timer(loop,
154 ivi_policy_try_event_timeout,
157 wl_event_source_timer_update(a_policy->timer, a_policy->timeout);
161 ivi_policy_check_policies(struct wl_listener *listener, void *data)
163 struct ivi_a_policy *a_policy;
164 struct ivi_policy *ivi_policy =
165 wl_container_of(listener, ivi_policy, listener_check_policies);
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,
175 ivi_policy_try_event(a_policy);
179 ivi_policy->previous_state = ivi_policy->current_state;
180 ivi_policy->state_change_in_progress = false;
185 ivi_policy_create(struct ivi_compositor *ivi,
186 const struct ivi_policy_api *api, void *user_data)
188 struct ivi_policy *policy = zalloc(sizeof(*policy));
192 policy->user_data = user_data;
194 policy->state_change_in_progress = false;
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);
201 /* to trigger a check for policies use */
202 wl_signal_init(&policy->signal_state_change);
204 policy->listener_check_policies.notify = ivi_policy_check_policies;
205 wl_signal_add(&policy->signal_state_change,
206 &policy->listener_check_policies);
208 policy->current_state = AGL_SHELL_POLICY_STATE_INVALID;
209 policy->previous_state = AGL_SHELL_POLICY_STATE_INVALID;
212 wl_list_init(&policy->policies);
214 wl_list_init(&policy->events);
215 wl_list_init(&policy->states);
217 /* add the default states and enums */
218 ivi_policy_add_default_states(policy);
219 ivi_policy_add_default_events(policy);
225 ivi_policy_destroy(struct ivi_policy *ivi_policy)
227 struct ivi_a_policy *a_policy, *a_policy_tmp;
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);
239 ivi_policy_destroy_state_event(&ivi_policy->states);
240 ivi_policy_destroy_state_event(&ivi_policy->events);
246 /* verifies if the state is one has been added */
248 ivi_policy_state_is_known(uint32_t state, struct ivi_policy *policy)
250 struct state_event *ev_st;
252 wl_list_for_each(ev_st, &policy->states, link) {
253 if (ev_st->value == state) {
261 * The generic way would be the following:
263 * - 'car' is in 'state' ->
264 * { do 'event' for app 'app_id' at 'timeout' time if same state as 'car_state' }
266 * a 0 timeout means, immediately, a timeout > 0, means to install timer an
267 * execute when timeout expires
269 * The following happens:
270 * 'car' changes its state -> verify what policy needs to be run
271 * 'car' in same state -> no action
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)
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;
285 weston_log("Failed to retrieve policy!\n");
289 if (policy->state_change_in_progress)
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))
298 if (!ivi_policy_state_is_known(state, policy))
301 a_policy = zalloc(sizeof(*a_policy));
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) {
311 memcpy(a_policy->app_id, app_id, app_id_len);
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;
319 wl_list_insert(&policy->policies, &a_policy->link);
324 /* we start with 'invalid' state, so a initial state to even 'stop' should
325 * trigger a check of policies
328 ivi_policy_state_change(struct ivi_policy *policy, uint32_t state)
330 bool found_state = false;
332 weston_log("Failed to retrieve policy!\n");
336 if (policy->current_state == state) {
340 /* if we don't know the state, make sure it is first added */
341 found_state = ivi_policy_state_is_known(state, policy);
346 /* current_state is actually the new state */
347 policy->current_state = state;
349 /* signal that we need to check the current policies */
350 wl_signal_emit(&policy->signal_state_change, policy);