Add chage remote app
[apps/agl-service-windowmanager.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     // Surfaces that where requested but not yet created
70     std::unordered_map<unsigned, std::string> id2name;
71     std::unordered_map<std::string, unsigned> name2id;
72
73     id_allocator(id_allocator const &) = delete;
74     id_allocator(id_allocator &&) = delete;
75     id_allocator &operator=(id_allocator const &);
76     id_allocator &operator=(id_allocator &&) = delete;
77
78     // Insert and return a new ID
79     unsigned generate_id(std::string const &name)
80     {
81         unsigned sid = this->next++;
82         this->id2name[sid] = name;
83         this->name2id[name] = sid;
84         HMI_DEBUG("allocated new id %u with name %s", sid, name.c_str());
85         return sid;
86     }
87
88     // Insert a new ID which defined outside
89     void register_name_id(std::string const &name, unsigned sid)
90     {
91         this->id2name[sid] = name;
92         this->name2id[name] = sid;
93         HMI_DEBUG("register id %u with name %s", sid, name.c_str());
94         return;
95     }
96
97     // Lookup by ID or by name
98     optional<unsigned> lookup(std::string const &name) const
99     {
100         auto i = this->name2id.find(name);
101         return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
102     }
103
104     optional<std::string> lookup(unsigned id) const
105     {
106         auto i = this->id2name.find(id);
107         return i == this->id2name.end() ? nullopt
108                                         : optional<std::string>(i->second);
109     }
110
111     // Remove a surface id and name
112     void remove_id(std::string const &name)
113     {
114         auto i = this->name2id.find(name);
115         if (i != this->name2id.end())
116         {
117             this->id2name.erase(i->second);
118             this->name2id.erase(i);
119         }
120     }
121
122     void remove_id(unsigned id)
123     {
124         auto i = this->id2name.find(id);
125         if (i != this->id2name.end())
126         {
127             this->name2id.erase(i->second);
128             this->id2name.erase(i);
129         }
130     }
131 };
132
133 struct TmpClient
134 {
135     std::string   appid;
136     unsigned pid;
137 };
138
139 struct TmpService
140 {
141     std::string appid;  // Used to search who create service surface
142     std::string dest;   // Used to attach service to destination application
143     std::string service;// The name of service surface
144     std::string uuid;   // uuid
145     TmpService(const std::string& app, const std::string& dst,
146                const std::string& svc, const std::string& uuid)
147     : appid(app), dest(dst), service(svc), uuid(uuid) {}
148 };
149
150 struct CarInfo
151 {
152     CarInfo()
153         : parking_brake_stt(true),
154           accel_pedal_stt(false),
155           accel_pedal_pos(0.0),
156           running_stt(false),
157           headlamp_stt(false),
158           lightstatus_brake_stt(true)
159     {};
160
161     bool parking_brake_stt;
162     bool accel_pedal_stt;
163     double accel_pedal_pos;
164     bool running_stt;
165     bool headlamp_stt;
166     bool lightstatus_brake_stt;
167 };
168
169 class WindowManager
170 {
171   public:
172     typedef std::unordered_map<uint32_t, struct rect> rect_map;
173     typedef std::function<void(const char *err_msg)> reply_func;
174
175     enum EventType
176     {
177         Event_Val_Min = 0,
178
179         Event_Active = Event_Val_Min,
180         Event_Inactive,
181
182         Event_Visible,
183         Event_Invisible,
184
185         Event_SyncDraw,
186         Event_FlushDraw,
187
188         Event_ScreenUpdated,
189
190         Event_Handshake,
191
192         Event_HeadlampOff,
193         Event_HeadlampOn,
194
195         Event_ParkingBrakeOff,
196         Event_ParkingBrakeOn,
197
198         Event_LightstatusBrakeOff,
199         Event_LightstatusBrakeOn,
200
201         Event_CarStop,
202         Event_CarRun,
203
204         Event_Error,
205
206         Event_Val_Max = Event_Error,
207     };
208
209     enum WM_Mode
210     {
211         Mode_Standalone = 0,
212         Mode_Connection,
213     };
214
215     explicit WindowManager();
216     ~WindowManager() = default;
217     WindowManager(WindowManager const &) = delete;
218     WindowManager &operator=(WindowManager const &) = delete;
219     WindowManager(WindowManager &&) = delete;
220     WindowManager &operator=(WindowManager &&) = delete;
221
222     int init();
223     void sendHandshake();
224     void setSubscribed(bool flg);
225
226     result<int> api_request_surface(char const *appid, char const *role);
227     char const *api_request_surface(char const *appid, char const *role, char const *ivi_id);
228     bool api_set_role(char const *appid, char const *role);
229     void api_activate_window(char const *appid, char const *role, char const *drawing_area, const reply_func &reply);
230     void api_activate_surface_for_slave(char const *appid, char const *drawing_name,
231                                         char const *drawing_area, const reply_func &reply);
232     void api_activate_surface_to_master(char const *appid, char const *drawing_name,
233                                         char const *drawing_area, const reply_func &reply);
234     void api_deactivate_window(char const *appid, char const *role, const reply_func &reply);
235     void api_deactivate_surface_for_slave(char const *appid, char const *drawing_name,
236                                           const reply_func &reply);
237     void api_deactivate_surface_to_master(char const *appid, char const *drawing_name,
238                                           const reply_func &reply);
239     void api_enddraw(char const *appid, char const *role);
240     int  api_subscribe(afb_req req, int event_id);
241     void api_handshake();
242     void api_enddraw_for_remote(char const *appid, char const *drawing_name);
243     bool api_client_set_render_order(const char *appid, const std::vector<std::string> &render_order);
244     std::string api_client_attach_service_surface(const char* appid, const char* dest, const char* service_surface);
245     json_object* api_get_area_list();
246     void api_change_area_size(ChangeAreaReq &areas);
247     result<json_object *> api_get_display_info();
248     result<json_object *> api_get_area_info(char const *role);
249     result<json_object *> api_get_car_info(char const *label);
250     void send_event(const std::string& evname);
251     void send_event(const std::string& evname, const std::string& role);
252     void send_event(const std::string& evname, const std::string& role, const std::string& area, int x, int y, int w, int h);
253
254     // Events from the compositor we are interested in
255     void surface_created(unsigned pid, unsigned surface_id);
256     void surface_removed(unsigned surface_id);
257
258     void removeClient(const std::string &appid);
259     void exceptionProcessForTransition();
260     const char* convertRoleOldToNew(char const *role);
261
262     void analyzeReceivedEvent(const char *event, struct json_object *object);
263
264
265     // Do not use this function
266     void timerHandler();
267     void startTransitionWrapper(std::vector<WMAction> &actions);
268     void processError(WMError error);
269     void processForRemoteRequest(json_object *data);
270     std::string searchApp(unsigned pid, unsigned ppid, unsigned surface, json_object* resp);
271     void storeSurface(const std::string& appid, unsigned ppid, unsigned surface);
272
273     WMConnection wmcon;
274
275   private:
276     // WM Events to clients
277     void emit_activated(const std::string& role);
278     void emit_deactivated(const std::string& role);
279     void emit_syncdraw(const std::string& role, char const *area, int x, int y, int w, int h);
280     void emit_syncdraw(const std::string &role, const std::string &area);
281     void emit_flushdraw(const std::string& role);
282     void emit_visible(const std::string& role, bool is_visible);
283     void emit_invisible(const std::string& role);
284     void emit_visible(const std::string& role);
285     void emitHeadlampOff();
286     void emitHeadlampOn();
287     void emitParkingBrakeOff();
288     void emitParkingBrakeOn();
289     void emitLightstatusBrakeOff();
290     void emitLightstatusBrakeOn();
291     void emitCarStop();
292     void emitCarRun();
293
294     WMError setRequest(const std::string &appid, const std::string &role, const std::string &area,
295                              Task task, unsigned *req_num);
296     WMError setRequest(Task task, unsigned* req_num);
297     WMError setRequestForSlave(const std::string& appid, const std::string &role,
298                                const std::string &area, Task task, unsigned* req_num);
299     WMError checkPolicy(unsigned req_num);
300     WMError checkPolicyForSlave(unsigned req_num);
301     WMError startTransition(unsigned req_num);
302     void transitionCarState(TaskCarState task);
303
304     WMError doEndDraw(unsigned req_num);
305     void    emitScreenUpdated(unsigned req_num);
306
307     void setTimer();
308     void stopTimer();
309     void processNextRequest();
310
311     int loadOldRolesConfigFile();
312
313     int saveLastModeData(unsigned req_num);
314
315     Task convertCanSignalToCarStateTask(const char *signal_name);
316     void inputCarStateTask(Task task);
317
318     const char *check_surface_exist(const char *role);
319
320   private:
321     std::map<std::string, struct afb_event> map_afb_event;
322     std::unordered_map<std::string, std::string> roleold2new;
323     std::unordered_map<std::string, std::string> rolenew2old;
324     std::shared_ptr<LayerControl> lc;
325
326     LowCanClient lcc;
327     CarInfo crr_car_info;
328
329     PMWrapper pmw;
330     rect_map area_info;
331     struct id_allocator id_alloc;
332
333     bool subscribed;
334
335     // ID allocation and proxy methods for lookup
336     std::unordered_map<unsigned, struct TmpClient> tmp_surface2app;
337     std::vector<struct TmpService> tmp_services;
338     static const char* kDefaultOldRolesConfig;
339 };
340
341 } // namespace wm
342
343 #endif // WINDOW_MANAGER_HPP