modify register/update shortcut
[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_RegisterShortcut,
55                 Event_UpdateShortcut,
56                 Event_Max
57         };
58
59         static const std::vector<std::string> api_list;
60         static const std::vector<std::string> event_list;
61
62         /* Method */
63         int init(const int port, const std::string& token);
64
65         int tapShortcut(const char* application_id);
66         int onScreenMessage(const char* display_message);
67         int onScreenReply(const char* reply_message);
68
69         void set_event_handler(enum EventType et, handler_func f);
70         void publishSubscription(void);
71
72         void registerCallback(
73                 void (*event_cb)(const std::string& event, struct json_object* event_contents),
74                 void (*reply_cb)(struct json_object* reply_contents),
75                 void (*hangup_cb)(void) = nullptr);
76
77         int call(const std::string& verb, struct json_object* arg);
78         int call(const char* verb, struct json_object* arg);
79         int subscribe(const std::string& event_name);
80         int unsubscribe(const std::string& event_name);
81
82         int showWindow(const char* application_id, json_object* json);
83         int hideWindow(const char* application_id);
84         int replyShowWindow(const char* application_id, json_object* json);
85         int showNotification(json_object* json);
86         int showInformation(json_object* json);
87         int getRunnables(void);
88         int registerShortcut(const char* application_id, json_object* json);
89         int updateShortcut(const char* application_id, json_object* json);
90
91 private:
92         int initialize_websocket();
93         int getEventType(const char *event);
94
95         void (*onEvent)(const std::string& event, struct json_object* event_contents);
96         void (*onReply)(struct json_object* reply);
97         void (*onHangup)(void);
98
99         struct afb_wsj1* sp_websock;
100         struct afb_wsj1_itf minterface;
101         sd_event* mploop;
102         std::string muri;
103
104         int mport = 2000;
105         std::string mtoken = "hs";
106
107         std::map<EventType, handler_func> handlers;
108
109 public:
110         /* Don't use/ Internal only */
111         void on_hangup(void *closure, struct afb_wsj1 *wsj);
112         void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
113         void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg);
114         void on_reply(void *closure, struct afb_wsj1_msg *msg);
115 };
116
117 #endif /* LIBHOMESCREEN_H */