add source for ces2019
[apps/agl-service-windowmanager-2017.git] / src / window_manager.hpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef WINDOW_MANAGER_HPP
18 #define WINDOW_MANAGER_HPP
19
20 #include <atomic>
21 #include <memory>
22 #include <unordered_map>
23 #include <experimental/optional>
24 #include "result.hpp"
25 #include "pm_wrapper.hpp"
26 #include "util.hpp"
27 #include "request.hpp"
28 #include "wm_error.hpp"
29 #include "wm_layer_control.hpp"
30 #include "wm_connection.hpp"
31 #include "low_can_client.hpp"
32 extern "C"
33 {
34 #include <afb/afb-binding.h>
35 }
36
37 struct json_object;
38
39 namespace wm
40 {
41
42 using std::experimental::optional;
43
44 /* DrawingArea name used by "{layout}.{area}" */
45 extern const char kNameLayoutNormal[];
46 extern const char kNameLayoutSplit[];
47 extern const char kNameAreaFull[];
48 extern const char kNameAreaMain[];
49 extern const char kNameAreaSub[];
50
51 /* Key for json obejct */
52 extern const char kKeyDrawingName[];
53 extern const char kKeyDrawingArea[];
54 extern const char kKeyDrawingRect[];
55 extern const char kKeyX[];
56 extern const char kKeyY[];
57 extern const char kKeyWidth[];
58 extern const char kKeyHeigh[];
59 extern const char kKeyWidthPixel[];
60 extern const char kKeyHeightPixel[];
61 extern const char kKeyWidthMm[];
62 extern const char kKeyHeightMm[];
63 extern const char kKeyScale[];
64 extern const char kKeyIds[];
65
66 struct id_allocator
67 {
68     unsigned next = 1;
69
70     // Surfaces that where requested but not yet created
71     std::unordered_map<unsigned, std::string> id2name;
72     std::unordered_map<std::string, unsigned> name2id;
73
74     id_allocator(id_allocator const &) = delete;
75     id_allocator(id_allocator &&) = delete;
76     id_allocator &operator=(id_allocator const &);
77     id_allocator &operator=(id_allocator &&) = delete;
78
79     // Insert and return a new ID
80     unsigned generate_id(std::string const &name)
81     {
82         unsigned sid = this->next++;
83         this->id2name[sid] = name;
84         this->name2id[name] = sid;
85         HMI_DEBUG("allocated new id %u with name %s", sid, name.c_str());
86         return sid;
87     }
88
89     // Insert a new ID which defined outside
90     void register_name_id(std::string const &name, unsigned sid)
91     {
92         this->id2name[sid] = name;
93         this->name2id[name] = sid;
94         HMI_DEBUG("register id %u with name %s", sid, name.c_str());
95         return;
96     }
97
98     // Lookup by ID or by name
99     optional<unsigned> lookup(std::string const &name) const
100     {
101         auto i = this->name2id.find(name);
102         return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
103     }
104
105     optional<std::string> lookup(unsigned id) const
106     {
107         auto i = this->id2name.find(id);
108         return i == this->id2name.end() ? nullopt
109                                         : optional<std::string>(i->second);
110     }
111
112     // Remove a surface id and name
113     void remove_id(std::string const &name)
114     {
115         auto i = this->name2id.find(name);
116         if (i != this->name2id.end())
117         {
118             this->id2name.erase(i->second);
119             this->name2id.erase(i);
120         }
121     }
122
123     void remove_id(unsigned id)
124     {
125         auto i = this->id2name.find(id);
126         if (i != this->id2name.end())
127         {
128             this->name2id.erase(i->second);
129             this->id2name.erase(i);
130         }
131     }
132 };
133
134 struct TmpClient
135 {
136     std::string   appid;
137     unsigned pid;
138 };
139
140 struct TmpService
141 {
142     std::string appid;  // Used to search who create service surface
143     std::string dest;   // Used to attach service to destination application
144     std::string service;// The name of service surface
145     std::string uuid;   // uuid
146     TmpService(const std::string& app, const std::string& dst,
147                const std::string& svc, const std::string& uuid)
148     : appid(app), dest(dst), service(svc), uuid(uuid) {}
149 };
150
151 struct CarInfo
152 {
153     CarInfo()
154         : parking_brake_stt(true),
155           accel_pedal_stt(false),
156           accel_pedal_pos(0.0),
157           running_stt(false),
158           headlamp_stt(false),
159           lightstatus_brake_stt(true)
160     {};
161
162     bool parking_brake_stt;
163     bool accel_pedal_stt;
164     double accel_pedal_pos;
165     bool running_stt;
166     bool headlamp_stt;
167     bool lightstatus_brake_stt;
168 };
169
170 class WindowManager
171 {
172   public:
173     typedef std::unordered_map<uint32_t, struct rect> rect_map;
174     typedef std::function<void(const char *err_msg)> reply_func;
175
176     enum EventType
177     {
178         Event_Val_Min = 0,
179
180         Event_Active = Event_Val_Min,
181         Event_Inactive,
182
183         Event_Visible,
184         Event_Invisible,
185
186         Event_SyncDraw,
187         Event_FlushDraw,
188
189         Event_ScreenUpdated,
190
191         Event_HeadlampOff,
192         Event_HeadlampOn,
193
194         Event_ParkingBrakeOff,
195         Event_ParkingBrakeOn,
196
197         Event_LightstatusBrakeOff,
198         Event_LightstatusBrakeOn,
199
200         Event_CarStop,
201         Event_CarRun,
202
203         Event_Error,
204
205         Event_Val_Max = Event_Error,
206     };
207
208     explicit WindowManager();
209     ~WindowManager() = default;
210
211     WindowManager(WindowManager const &) = delete;
212     WindowManager &operator=(WindowManager const &) = delete;
213     WindowManager(WindowManager &&) = delete;
214     WindowManager &operator=(WindowManager &&) = delete;
215
216     int init();
217
218     result<int> api_request_surface(char const *appid, char const *role);
219     char const *api_request_surface(char const *appid, char const *role, char const *ivi_id);
220     bool api_set_role(char const *appid, char const *role);
221     void api_activate_surface(char const *appid, char const *role, char const *drawing_area, const reply_func &reply);
222     void api_activate_surface_for_slave(char const *appid, char const *drawing_name,
223                                         char const *drawing_area, const reply_func &reply);
224     void api_activate_surface_to_master(char const *appid, char const *drawing_name,
225                                         char const *drawing_area, const reply_func &reply);
226     void api_deactivate_surface(char const *appid, char const *role, const reply_func &reply);
227     void api_deactivate_surface_for_slave(char const *appid, char const *drawing_name,
228                                           const reply_func &reply);
229     void api_deactivate_surface_to_master(char const *appid, char const *drawing_name,
230                                           const reply_func &reply);
231     void api_enddraw(char const *appid, char const *role);
232     void api_enddraw_for_remote(char const *appid, char const *drawing_name);
233     bool api_client_set_render_order(const char *appid, const std::vector<std::string> &render_order);
234     std::string api_client_attach_service_surface(const char* appid, const char* dest, const char* service_surface);
235     result<json_object *> api_get_display_info();
236     result<json_object *> api_get_area_info(char const *role);
237     result<json_object *> api_get_car_info(char const *label);
238     void send_event(char const *evname);
239     void send_event(char const *evname, char const *label);
240     void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
241
242     // Events from the compositor we are interested in
243     void surface_created(unsigned pid, unsigned surface_id);
244     void surface_removed(unsigned surface_id);
245
246     void removeClient(const std::string &appid);
247     void exceptionProcessForTransition();
248     const char* convertRoleOldToNew(char const *role);
249
250     void analyzeReceivedEvent(const char *event, struct json_object *object);
251
252     // Do not use this function
253     void timerHandler();
254     void startTransitionWrapper(std::vector<WMAction> &actions);
255     void processError(WMError error);
256     void processForRemoteRequest(json_object *data);
257     std::string searchApp(unsigned pid, unsigned ppid, unsigned surface, json_object* resp);
258     void storeSurface(const std::string& appid, unsigned ppid, unsigned surface);
259
260     const std::vector<const char *> kListEventName{
261         "active",
262         "inactive",
263         "visible",
264         "invisible",
265         "syncDraw",
266         "flushDraw",
267         "screenUpdated",
268         "headlampOff",
269         "headlampOn",
270         "parkingBrakeOff",
271         "parkingBrakeOn",
272         "lightstatusBrakeOff",
273         "lightstatusBrakeOn",
274         "carStop",
275         "carRun",
276         "error"};
277     std::map<const char *, struct afb_event> map_afb_event;
278
279     WMConnection wmcon;
280
281   private:
282     // WM Events to clients
283     void emit_activated(char const *label);
284     void emit_deactivated(char const *label);
285     void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
286     void emit_syncdraw(const std::string &role, const std::string &area);
287     void emit_flushdraw(char const *label);
288     void emit_visible(char const *label, bool is_visible);
289     void emit_invisible(char const *label);
290     void emit_visible(char const *label);
291     void emitHeadlampOff();
292     void emitHeadlampOn();
293     void emitParkingBrakeOff();
294     void emitParkingBrakeOn();
295     void emitLightstatusBrakeOff();
296     void emitLightstatusBrakeOn();
297     void emitCarStop();
298     void emitCarRun();
299
300     WMError setRequest(const std::string &appid, const std::string &role, const std::string &area,
301                              Task task, unsigned *req_num);
302     WMError setRequest(Task task, unsigned* req_num);
303     WMError setRequestForSlave(const std::string& appid, const std::string &role,
304                                const std::string &area, Task task, unsigned* req_num);
305     WMError checkPolicy(unsigned req_num);
306     WMError checkPolicyForSlave(unsigned req_num);
307     WMError startTransition(unsigned req_num);
308     void transitionCarState(TaskCarState task);
309
310     WMError doEndDraw(unsigned req_num);
311     void    emitScreenUpdated(unsigned req_num);
312
313     void setTimer();
314     void stopTimer();
315     void processNextRequest();
316
317     int loadOldRolesConfigFile();
318
319     Task convertCanSignalToCarStateTask(const char *signal_name);
320     void inputCarStateTask(Task task);
321
322     const char *check_surface_exist(const char *role);
323
324   private:
325     std::unordered_map<std::string, std::string> roleold2new;
326     std::unordered_map<std::string, std::string> rolenew2old;
327     std::shared_ptr<LayerControl> lc;
328
329     LowCanClient lcc;
330     CarInfo crr_car_info;
331
332     PMWrapper pmw;
333
334     // ID allocation and proxy methods for lookup
335     struct id_allocator id_alloc;
336     // Surface are info (x, y, w, h)
337     rect_map area_info;
338     // FOR CES DEMO
339     std::unordered_map<unsigned, struct TmpClient> tmp_surface2app;
340     std::vector<struct TmpService> tmp_services;
341     static const char* kDefaultOldRolesConfig;
342 };
343
344 } // namespace wm
345
346 #endif // WINDOW_MANAGER_HPP