cbf816af09336934999a9dca803a9dc204741f87
[apps/homescreen.git] / homescreen / src / homescreenhandler.cpp
1 /*
2  * Copyright (c) 2017, 2018, 2019 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 #include <QFileInfo>
18 #include "homescreenhandler.h"
19 #include <functional>
20 #include <QProcess>
21 #include <dirent.h>
22 #include <stdio.h>
23 #include "hmi-debug.h"
24
25 #define BUF_SIZE 1024
26 void* HomescreenHandler::myThis = 0;
27
28 HomescreenHandler::HomescreenHandler(QObject *parent) :
29     QObject(parent),
30     mp_qhs(NULL),
31     current_application("launcher")
32 {
33 }
34
35 HomescreenHandler::~HomescreenHandler()
36 {
37     if (mp_qhs != NULL) {
38         delete mp_qhs;
39     }
40 }
41
42 void HomescreenHandler::init(int port, const char *token, QLibWindowmanager *qwm, QString myname)
43 {
44     mp_qhs = new QLibHomeScreen();
45     mp_qhs->init(port, token);
46
47     myThis = this;
48     mp_qwm = qwm;
49     m_myname = myname;
50
51     mp_qhs->registerCallback(nullptr, HomescreenHandler::onRep_static);
52
53     mp_qhs->set_event_handler(QLibHomeScreen::Event_ShowWindow,[this](json_object *object){
54         HMI_DEBUG("Launcher","Surface launcher got Event_ShowWindow\n");
55         static bool first_start = true;
56         if (first_start) {
57             first_start = false;
58             mp_qwm->activateWindow(m_myname);
59         }
60         else {
61             emit showWindow();
62         }
63     });
64
65     mp_qhs->set_event_handler(QLibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
66         const char *display_message = json_object_get_string(
67             json_object_object_get(object, "display_message"));
68         HMI_DEBUG("HomeScreen","set_event_handler Event_OnScreenMessage display_message = %s", display_message);
69     });
70
71     mp_qhs->set_event_handler(QLibHomeScreen::Event_ShowNotification,[this](json_object *object){
72        json_object *p_obj = json_object_object_get(object, "parameter");
73        const char *icon = json_object_get_string(
74                    json_object_object_get(p_obj, "icon"));
75        const char *text = json_object_get_string(
76                    json_object_object_get(p_obj, "text"));
77        const char *app_id = json_object_get_string(
78                    json_object_object_get(p_obj, "caller"));
79        HMI_DEBUG("HomeScreen","Event_ShowNotification icon=%s, text=%s, caller=%s", icon, text, app_id);
80        QFileInfo icon_file(icon);
81        QString icon_path;
82        if (icon_file.isFile() && icon_file.exists()) {
83            icon_path = QString(QLatin1String(icon));
84        } else {
85            icon_path = "./images/Utility_Logo_Grey-01.svg";
86        }
87
88        emit showNotification(QString(QLatin1String(app_id)), icon_path, QString(QLatin1String(text)));
89     });
90
91     mp_qhs->set_event_handler(QLibHomeScreen::Event_ShowInformation,[this](json_object *object){
92        json_object *p_obj = json_object_object_get(object, "parameter");
93        const char *info = json_object_get_string(
94                    json_object_object_get(p_obj, "info"));
95
96        emit showInformation(QString(QLatin1String(info)));
97     });
98
99     mp_qhs->set_event_handler(QLibHomeScreen::Event_HideWindow, [this](json_object *object) {
100         emit hideWindow();
101         HMI_DEBUG("HomeScreen","set_event_handler Event_HideWindow");
102     });
103
104     mp_qhs->set_event_handler(QLibHomeScreen::Event_RegisterShortcut,[this](json_object *object){
105         HMI_DEBUG("HomeScreen","set_event_handler Event_RegisterShortcut");
106         json_object *p_obj = json_object_object_get(object, "parameter");
107         const char *shortcut_id = json_object_get_string(
108                     json_object_object_get(p_obj, "shortcut_id"));
109         const char *shortcut_name = json_object_get_string(
110                     json_object_object_get(p_obj, "shortcut_name"));
111         const char *position = json_object_get_string(
112                     json_object_object_get(p_obj, "position"));
113         HMI_DEBUG("HomeScreen", "Event_RegisterShortcut id==%s, name==%s, position ==%s", shortcut_id, shortcut_name, position);
114         emit shortcutChanged(QString(QLatin1String(shortcut_id)), QString(QLatin1String(shortcut_name)), QString(QLatin1String(position)));
115     });
116 }
117
118 void HomescreenHandler::tapShortcut(QString application_id, bool is_full)
119 {
120     HMI_DEBUG("HomeScreen","tapShortcut %s", application_id.toStdString().c_str());
121     struct json_object* j_json = json_object_new_object();
122     struct json_object* value;
123     if(is_full) {
124         value = json_object_new_string("fullscreen");
125         HMI_DEBUG("HomeScreen","fullscreen");
126     } else {
127         value = json_object_new_string("normal.full");
128         HMI_DEBUG("HomeScreen","normal");
129     }
130     json_object_object_add(j_json, "area", value);
131     mp_qhs->showWindow(application_id.section('@', 0, 0).toStdString().c_str(), j_json);
132 }
133
134 void HomescreenHandler::updateShortcut(QString id, struct json_object* object)
135 {
136     mp_qhs->updateShortcut(id.toStdString().c_str(), object);
137 }
138
139
140 void HomescreenHandler::setCurrentApplication(QString application_name)
141 {
142     HMI_DEBUG("HomeScreen","setCurrentApplication %s", application_name.toStdString().c_str());
143     current_application = application_name;
144 }
145
146 QString HomescreenHandler::getCurrentApplication()
147 {
148     HMI_DEBUG("HomeScreen","getCurrentApplication %s", current_application.toStdString().c_str());
149     return current_application;
150 }
151
152 int HomescreenHandler::getPidOfApplication(QString application_name) {
153     DIR *dir = NULL;
154     struct dirent *dir_ent_ptr = NULL;
155     FILE *fp = NULL;
156     char file_path[50] = {0};
157     char cur_task_ame[50] = {0};
158     char buf[BUF_SIZE] = {0};
159     int pid = -1;
160
161     dir = opendir("/proc");
162     if (dir) {
163         while((dir_ent_ptr = readdir(dir)) != NULL) {
164             if ((strcmp(dir_ent_ptr->d_name, ".") == 0) || (strcmp(dir_ent_ptr->d_name, "..") == 0)
165                 || (DT_DIR != dir_ent_ptr->d_type))
166                 continue;
167             sprintf(file_path, "/proc/%s/status", dir_ent_ptr->d_name);
168             fp = fopen(file_path, "r");
169             if (fp) {
170                 if (fgets(buf, BUF_SIZE - 1, fp) == NULL) {
171                     fclose(fp);
172                     continue;
173                 }
174                 sscanf(buf, "%*s %s", cur_task_ame);
175                 if (0 == strcmp(application_name.toStdString().c_str(), cur_task_ame)) {
176                     pid = atoi(dir_ent_ptr->d_name);
177                     break;
178                 }
179             }
180         }
181     }
182
183     return pid;
184 }
185
186 void HomescreenHandler::killRunningApplications()
187 {
188     QProcess *proc = new QProcess;
189     QProcess *proc2 = new QProcess;
190 //    int num = getPidOfApplication("afbd-video@0.1");
191 //    QString procNum = QString::number(num);
192     QString command = "/usr/bin/pkill videoplayer";
193     QString command2 = "/usr/bin/pkill navigation";
194     proc->start(command);
195     proc2->start(command2);
196     HMI_DEBUG("homescreen", command.toStdString().c_str());
197     HMI_DEBUG("homescreen", command2.toStdString().c_str());
198 }
199
200 void HomescreenHandler::reboot()
201 {
202     QProcess::execute("sync");
203     QProcess::execute("reboot -f");
204 }
205
206 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
207 {
208     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
209 }
210
211 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
212 {
213     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
214 }
215
216 void HomescreenHandler::onRep(struct json_object* reply_contents)
217 {
218     const char* str = json_object_to_json_string(reply_contents);
219     HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
220 }
221
222 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
223 {
224     const char* str = json_object_to_json_string(event_contents);
225     HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
226
227     if (event.compare("homescreen/on_screen_message") == 0) {
228         struct json_object *json_data = json_object_object_get(event_contents, "data");
229         struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
230         const char* display_message = json_object_get_string(json_display_message);
231
232         HMI_DEBUG("HomeScreen","display_message = %s", display_message);
233     }
234 }
235
236 void HomescreenHandler::setQuickWindow(QQuickWindow *qw)
237 {
238     mp_qhs->setQuickWindow(qw);
239 }