Start app and get runnables list by homescreen
[src/libhomescreen.git] / include / libhomescreen.hpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  * Copyright (c) 2018,2019 TOYOTA MOTOR CORPORATION
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef LIBHOMESCREEN_H
19 #define LIBHOMESCREEN_H
20 #include <vector>
21 #include <map>
22 #include <string>
23 #include <functional>
24 #include <json-c/json.h>
25 #include <systemd/sd-event.h>
26 extern "C"
27 {
28 #include <afb/afb-wsj1.h>
29 #include <afb/afb-ws-client.h>
30 }
31
32 class LibHomeScreen
33 {
34 public:
35         LibHomeScreen();
36         ~LibHomeScreen();
37
38         LibHomeScreen(const LibHomeScreen &) = delete;
39         LibHomeScreen &operator=(const LibHomeScreen &) = delete;
40
41         using handler_func = std::function<void(json_object*)>;
42
43         enum EventType {
44                 Event_Min,
45                 Event_ShowWindow = 1,
46                 Event_TapShortcut = 1,
47                 Event_OnScreenMessage,
48                 Event_OnScreenReply,
49                 Event_HideWindow,
50                 Event_ReplyShowWindow,
51                 Event_ShowNotification,
52                 Event_ShowInformation,
53                 Event_AppListChanged,
54                 Event_Max
55         };
56
57         static const std::vector<std::string> api_list;
58         static const std::vector<std::string> event_list;
59
60         /* Method */
61         int init(const int port, const std::string& token);
62
63         int tapShortcut(const char* application_id);
64         int onScreenMessage(const char* display_message);
65         int onScreenReply(const char* reply_message);
66
67         void set_event_handler(enum EventType et, handler_func f);
68
69         void registerCallback(
70                 void (*event_cb)(const std::string& event, struct json_object* event_contents),
71                 void (*reply_cb)(struct json_object* reply_contents),
72                 void (*hangup_cb)(void) = nullptr);
73
74         int call(const std::string& verb, struct json_object* arg);
75         int call(const char* verb, struct json_object* arg);
76         int subscribe(const std::string& event_name);
77         int unsubscribe(const std::string& event_name);
78
79         int showWindow(const char* application_id, json_object* json);
80         int hideWindow(const char* application_id);
81         int replyShowWindow(const char* application_id, json_object* json);
82         int showNotification(json_object* json);
83         int showInformation(json_object* json);
84         int getRunnables(void);
85
86
87 private:
88         int initialize_websocket();
89
90         void (*onEvent)(const std::string& event, struct json_object* event_contents);
91         void (*onReply)(struct json_object* reply);
92         void (*onHangup)(void);
93
94         struct afb_wsj1* sp_websock;
95         struct afb_wsj1_itf minterface;
96         sd_event* mploop;
97         std::string muri;
98
99         int mport = 2000;
100         std::string mtoken = "hs";
101
102         std::map<EventType, handler_func> handlers;
103
104 public:
105         /* Don't use/ Internal only */
106         void on_hangup(void *closure, struct afb_wsj1 *wsj);
107         void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
108         void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg);
109         void on_reply(void *closure, struct afb_wsj1_msg *msg);
110 };
111
112 #endif /* LIBHOMESCREEN_H */