policy: Init policy framework API
[src/agl-compositor.git] / src / policy.h
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 #ifndef POLICY_H
27 #define POLICY_H
28
29 #include "ivi-compositor.h"
30
31 /* default state, invalid should at least be in order
32  * to signal states */
33 #ifndef AGL_SHELL_POLICY_STATE_INVALID
34 #define AGL_SHELL_POLICY_STATE_INVALID 0
35 #endif
36
37 /* default events */
38 #ifndef AGL_SHELL_POLICY_EVENT_SHOW
39 #define AGL_SHELL_POLICY_EVENT_SHOW 0
40 #endif
41
42 #ifndef AGL_SHELL_POLICY_EVENT_HIDE
43 #define AGL_SHELL_POLICY_EVENT_HIDE 1
44 #endif
45
46 struct ivi_policy;
47
48 struct state_event {
49         uint32_t value;
50         char *name;
51         struct wl_list link;    /* ivi_policy::states or ivi_policy::events */
52 };
53
54 struct ivi_a_policy {
55         struct ivi_policy *policy;
56
57         char *app_id;
58         uint32_t state;
59         uint32_t event;
60         uint32_t timeout;
61         struct ivi_output *output;
62         struct wl_event_source *timer;  /* for policies that have a timeout */
63
64         struct wl_list link;    /* ivi_policy::ivi_policies */
65 };
66
67 struct ivi_policy_api {
68         size_t struct_size;
69
70         bool (*surface_create)(struct ivi_surface *surf, void *user_data);
71         bool (*surface_commited)(struct ivi_surface *surf, void *user_data);
72         bool (*surface_activate)(struct ivi_surface *surf, void *user_data);
73
74         bool (*surface_activate_by_default)(struct ivi_surface *surf, void *user_data);
75
76         /** see also ivi_policy_add(). If set this will be executed before
77          * adding a new policy rule  */
78         bool (*policy_rule_allow_to_add)(void *user_data);
79
80         /** this callback will be executed when a there's a policy state change */
81         void (*policy_rule_try_event)(struct ivi_a_policy *a_policy);
82 };
83
84 struct ivi_policy {
85         struct ivi_compositor *ivi;
86         /* user-defined hooks */
87         struct ivi_policy_api api;
88         void *user_data;
89
90         /* represents the policy rules */
91         struct wl_list policies;        /* ivi_a_policy::link */
92
93         /* no state update chnage is being done as long as we have the same
94          * state */
95         uint32_t current_state;
96         uint32_t previous_state;
97
98         /* guards against current in change in progress */
99         bool state_change_in_progress;
100
101         /* additional states which can be verified in
102          * ivi_policy_api::policy_rule_try_event() */
103         struct wl_list states;  /* state_event::link */
104         struct wl_list events;  /* state_event::link */
105
106         /* necessary to for signaling the state change */
107         struct wl_listener listener_check_policies;
108         struct wl_signal signal_state_change;
109 };
110
111
112 /** Initialize the policy setup
113  *
114  * Policy engine should call ivi_policy_create() with its own ivi_policy_api
115  * setup.
116  */
117 struct ivi_policy *
118 ivi_policy_create(struct ivi_compositor *compositor,
119                   const struct ivi_policy_api *api, void *user_data);
120
121 /** Destroys the policy setup
122  *
123  */
124 void
125 ivi_policy_destroy(struct ivi_policy *ivi_policy);
126
127 /** Add a policy rule.
128  *
129  * ivi_policy_api::policy_rule_allow_to_add() can be used to limit adding
130  * policy rules.
131  *
132  * Returns 0 in case of success, or -1 in case of failure.
133  *
134  */
135 int
136 ivi_policy_add(struct ivi_policy *policy, const char *app_id, uint32_t state,
137                uint32_t event, uint32_t timeout, struct wl_resource *output_res);
138
139 /** Trigger a state change. This should be called **each time** there is a need
140  * to apply the policy rules.
141  *
142  * Returns 0 in case of success, or -1 in case of failure.
143  */
144 int
145 ivi_policy_state_change(struct ivi_policy *policy, uint32_t state);
146
147
148 /** Add a new state. The state can be verified in ivi_policy_api::policy_rule_try_event()
149  *
150  */
151 void
152 ivi_policy_add_state(struct ivi_policy *policy, uint32_t state, const char *value);
153
154 /** Add a new event. The event can be verified in ivi_policy_api::policy_rule_try_event()
155  *
156  */
157 void
158 ivi_policy_add_event(struct ivi_policy *policy, uint32_t state, const char *value);
159
160 /** Initialize the policy. Not implemented.
161  *
162  * Should be implemented by the policy engine. A single policy engine can be used
163  * at one time.
164  *
165  * Returns 0 in case of success, or -1 in case of failure.
166  */
167 int
168 ivi_policy_init(struct ivi_compositor *ivi);
169
170 #endif