2 * Copyright (c) 2017, 2018, 2019 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "homescreenhandler.h"
23 #include "hmi-debug.h"
26 void* HomescreenHandler::myThis = 0;
28 HomescreenHandler::HomescreenHandler(QObject *parent) :
31 current_application("launcher")
35 HomescreenHandler::~HomescreenHandler()
42 void HomescreenHandler::init(int port, const char *token, QLibWindowmanager *qwm, QString myname)
44 mp_qhs = new QLibHomeScreen();
45 mp_qhs->init(port, token);
51 mp_qhs->registerCallback(nullptr, HomescreenHandler::onRep_static);
53 mp_qhs->set_event_handler(QLibHomeScreen::Event_ShowWindow,[this](json_object *object){
54 HMI_DEBUG("Launcher","Surface launcher got Event_ShowWindow\n");
55 mp_qwm->activateWindow(m_myname);
59 mp_qhs->set_event_handler(QLibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
60 const char *display_message = json_object_get_string(
61 json_object_object_get(object, "display_message"));
62 HMI_DEBUG("HomeScreen","set_event_handler Event_OnScreenMessage display_message = %s", display_message);
65 mp_qhs->set_event_handler(QLibHomeScreen::Event_ShowNotification,[this](json_object *object){
66 json_object *p_obj = json_object_object_get(object, "parameter");
67 const char *icon = json_object_get_string(
68 json_object_object_get(p_obj, "icon"));
69 const char *text = json_object_get_string(
70 json_object_object_get(p_obj, "text"));
71 const char *app_id = json_object_get_string(
72 json_object_object_get(p_obj, "caller"));
73 HMI_DEBUG("HomeScreen","Event_ShowNotification icon=%s, text=%s, caller=%s", icon, text, app_id);
74 QFileInfo icon_file(icon);
76 if (icon_file.isFile() && icon_file.exists()) {
77 icon_path = QString(QLatin1String(icon));
79 icon_path = "./images/Utility_Logo_Grey-01.svg";
82 emit showNotification(QString(QLatin1String(app_id)), icon_path, QString(QLatin1String(text)));
85 mp_qhs->set_event_handler(QLibHomeScreen::Event_ShowInformation,[this](json_object *object){
86 json_object *p_obj = json_object_object_get(object, "parameter");
87 const char *info = json_object_get_string(
88 json_object_object_get(p_obj, "info"));
90 emit showInformation(QString(QLatin1String(info)));
93 mp_qhs->set_event_handler(QLibHomeScreen::Event_HideWindow, [this](json_object *object) {
95 HMI_DEBUG("HomeScreen","set_event_handler Event_HideWindow");
98 mp_qhs->set_event_handler(QLibHomeScreen::Event_RegisterShortcut,[this](json_object *object){
99 HMI_DEBUG("HomeScreen","set_event_handler Event_RegisterShortcut");
100 json_object *p_obj = json_object_object_get(object, "parameter");
101 const char *shortcut_id = json_object_get_string(
102 json_object_object_get(p_obj, "shortcut_id"));
103 const char *shortcut_name = json_object_get_string(
104 json_object_object_get(p_obj, "shortcut_name"));
105 const char *position = json_object_get_string(
106 json_object_object_get(p_obj, "position"));
107 HMI_DEBUG("HomeScreen", "Event_RegisterShortcut id==%s, name==%s, position ==%s", shortcut_id, shortcut_name, position);
108 emit shortcutChanged(QString(QLatin1String(shortcut_id)), QString(QLatin1String(shortcut_name)), QString(QLatin1String(position)));
112 void HomescreenHandler::tapShortcut(QString application_id, bool is_full)
114 HMI_DEBUG("HomeScreen","tapShortcut %s", application_id.toStdString().c_str());
115 struct json_object* j_json = json_object_new_object();
116 struct json_object* value;
118 value = json_object_new_string("fullscreen");
119 HMI_DEBUG("HomeScreen","fullscreen");
121 value = json_object_new_string("normal.full");
122 HMI_DEBUG("HomeScreen","normal");
124 json_object_object_add(j_json, "area", value);
125 mp_qhs->showWindow(application_id.section('@', 0, 0).toStdString().c_str(), j_json);
128 void HomescreenHandler::updateShortcut(QString id, struct json_object* object)
130 mp_qhs->updateShortcut(id.toStdString().c_str(), object);
134 void HomescreenHandler::setCurrentApplication(QString application_name)
136 HMI_DEBUG("HomeScreen","setCurrentApplication %s", application_name.toStdString().c_str());
137 current_application = application_name;
140 QString HomescreenHandler::getCurrentApplication()
142 HMI_DEBUG("HomeScreen","getCurrentApplication %s", current_application.toStdString().c_str());
143 return current_application;
146 int HomescreenHandler::getPidOfApplication(QString application_name) {
148 struct dirent *dir_ent_ptr = NULL;
150 char file_path[50] = {0};
151 char cur_task_ame[50] = {0};
152 char buf[BUF_SIZE] = {0};
155 dir = opendir("/proc");
157 while((dir_ent_ptr = readdir(dir)) != NULL) {
158 if ((strcmp(dir_ent_ptr->d_name, ".") == 0) || (strcmp(dir_ent_ptr->d_name, "..") == 0)
159 || (DT_DIR != dir_ent_ptr->d_type))
161 sprintf(file_path, "/proc/%s/status", dir_ent_ptr->d_name);
162 fp = fopen(file_path, "r");
164 if (fgets(buf, BUF_SIZE - 1, fp) == NULL) {
168 sscanf(buf, "%*s %s", cur_task_ame);
169 if (0 == strcmp(application_name.toStdString().c_str(), cur_task_ame)) {
170 pid = atoi(dir_ent_ptr->d_name);
180 void HomescreenHandler::killRunningApplications()
182 QProcess *proc = new QProcess;
183 QProcess *proc2 = new QProcess;
184 // int num = getPidOfApplication("afbd-video@0.1");
185 // QString procNum = QString::number(num);
186 QString command = "/usr/bin/pkill videoplayer";
187 QString command2 = "/usr/bin/pkill navigation";
188 proc->start(command);
189 proc2->start(command2);
190 HMI_DEBUG("homescreen", command.toStdString().c_str());
191 HMI_DEBUG("homescreen", command2.toStdString().c_str());
194 void HomescreenHandler::reboot()
196 QProcess::execute("sync");
197 QProcess::execute("reboot -f");
200 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
202 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
205 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
207 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
210 void HomescreenHandler::onRep(struct json_object* reply_contents)
212 const char* str = json_object_to_json_string(reply_contents);
213 HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
216 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
218 const char* str = json_object_to_json_string(event_contents);
219 HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
221 if (event.compare("homescreen/on_screen_message") == 0) {
222 struct json_object *json_data = json_object_object_get(event_contents, "data");
223 struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
224 const char* display_message = json_object_get_string(json_display_message);
226 HMI_DEBUG("HomeScreen","display_message = %s", display_message);
230 void HomescreenHandler::setQuickWindow(QQuickWindow *qw)
232 mp_qhs->setQuickWindow(qw);