compositor: Use stdint for specifing integer storage
[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         bool (*surface_deactivate)(struct ivi_surface *surf, void *user_data);
74
75         bool (*surface_activate_by_default)(struct ivi_surface *surf, void *user_data);
76         bool (*surface_advertise_state_change)(struct ivi_surface *surf, void *user_data);
77
78         bool (*shell_bind_interface)(void *client, void *interface);
79
80         /** see also ivi_policy_add(). If set this will be executed before
81          * adding a new policy rule  */
82         bool (*policy_rule_allow_to_add)(void *user_data);
83
84         /** this callback will be executed when a there's a policy state change */
85         void (*policy_rule_try_event)(struct ivi_a_policy *a_policy);
86 };
87
88 struct ivi_policy {
89         struct ivi_compositor *ivi;
90         /* user-defined hooks */
91         struct ivi_policy_api api;
92         void *user_data;
93
94         /* represents the policy rules */
95         struct wl_list policies;        /* ivi_a_policy::link */
96
97         /* no state update chnage is being done as long as we have the same
98          * state */
99         uint32_t current_state;
100         uint32_t previous_state;
101
102         /* guards against current in change in progress */
103         bool state_change_in_progress;
104
105         /* additional states which can be verified in
106          * ivi_policy_api::policy_rule_try_event() */
107         struct wl_list states;  /* state_event::link */
108         struct wl_list events;  /* state_event::link */
109
110         /* necessary to for signaling the state change */
111         struct wl_listener listener_check_policies;
112         struct wl_signal signal_state_change;
113 };
114
115
116 /** Initialize the policy setup
117  *
118  * Policy engine should call ivi_policy_create() with its own ivi_policy_api
119  * setup.
120  */
121 struct ivi_policy *
122 ivi_policy_create(struct ivi_compositor *compositor,
123                   const struct ivi_policy_api *api, void *user_data);
124
125 /** Destroys the policy setup
126  *
127  */
128 void
129 ivi_policy_destroy(struct ivi_policy *ivi_policy);
130
131 /** Add a policy rule.
132  *
133  * ivi_policy_api::policy_rule_allow_to_add() can be used to limit adding
134  * policy rules.
135  *
136  * Returns 0 in case of success, or -1 in case of failure.
137  *
138  */
139 int
140 ivi_policy_add(struct ivi_policy *policy, const char *app_id, uint32_t state,
141                uint32_t event, uint32_t timeout, struct wl_resource *output_res);
142
143 /** Trigger a state change. This should be called **each time** there is a need
144  * to apply the policy rules.
145  *
146  * Returns 0 in case of success, or -1 in case of failure.
147  */
148 int
149 ivi_policy_state_change(struct ivi_policy *policy, uint32_t state);
150
151
152 /** Add a new state. The state can be verified in ivi_policy_api::policy_rule_try_event()
153  *
154  */
155 void
156 ivi_policy_add_state(struct ivi_policy *policy, uint32_t state, const char *value);
157
158 /** Add a new event. The event can be verified in ivi_policy_api::policy_rule_try_event()
159  *
160  */
161 void
162 ivi_policy_add_event(struct ivi_policy *policy, uint32_t state, const char *value);
163
164 /** Initialize the policy. Not implemented.
165  *
166  * Should be implemented by the policy engine. A single policy engine can be used
167  * at one time.
168  *
169  * Returns 0 in case of success, or -1 in case of failure.
170  */
171 int
172 ivi_policy_init(struct ivi_compositor *ivi);
173
174 #endif