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 static bool first_start = true;
58 mp_qwm->activateWindow(m_myname);
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);
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);
82 if (icon_file.isFile() && icon_file.exists()) {
83 icon_path = QString(QLatin1String(icon));
85 icon_path = "./images/Utility_Logo_Grey-01.svg";
88 emit showNotification(QString(QLatin1String(app_id)), icon_path, QString(QLatin1String(text)));
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"));
96 emit showInformation(QString(QLatin1String(info)));
99 mp_qhs->set_event_handler(QLibHomeScreen::Event_HideWindow, [this](json_object *object) {
101 HMI_DEBUG("HomeScreen","set_event_handler Event_HideWindow");
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)));
118 void HomescreenHandler::tapShortcut(QString application_id, bool is_full)
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;
124 value = json_object_new_string("fullscreen");
125 HMI_DEBUG("HomeScreen","fullscreen");
127 value = json_object_new_string("normal.full");
128 HMI_DEBUG("HomeScreen","normal");
130 json_object_object_add(j_json, "area", value);
131 mp_qhs->showWindow(application_id.section('@', 0, 0).toStdString().c_str(), j_json);
134 void HomescreenHandler::updateShortcut(QString id, struct json_object* object)
136 mp_qhs->updateShortcut(id, object);
140 void HomescreenHandler::setCurrentApplication(QString application_name)
142 HMI_DEBUG("HomeScreen","setCurrentApplication %s", application_name.toStdString().c_str());
143 current_application = application_name;
146 QString HomescreenHandler::getCurrentApplication()
148 HMI_DEBUG("HomeScreen","getCurrentApplication %s", current_application.toStdString().c_str());
149 return current_application;
152 int HomescreenHandler::getPidOfApplication(QString application_name) {
154 struct dirent *dir_ent_ptr = NULL;
156 char file_path[50] = {0};
157 char cur_task_ame[50] = {0};
158 char buf[BUF_SIZE] = {0};
161 dir = opendir("/proc");
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))
167 sprintf(file_path, "/proc/%s/status", dir_ent_ptr->d_name);
168 fp = fopen(file_path, "r");
170 if (fgets(buf, BUF_SIZE - 1, fp) == NULL) {
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);
186 void HomescreenHandler::killRunningApplications()
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());
200 void HomescreenHandler::reboot()
202 QProcess::execute("sync");
203 QProcess::execute("reboot -f");
206 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
208 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
211 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
213 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
216 void HomescreenHandler::onRep(struct json_object* reply_contents)
218 const char* str = json_object_to_json_string(reply_contents);
219 HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
222 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
224 const char* str = json_object_to_json_string(event_contents);
225 HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
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);
232 HMI_DEBUG("HomeScreen","display_message = %s", display_message);
236 void HomescreenHandler::setQuickWindow(QQuickWindow *qw)
238 mp_qhs->setQuickWindow(qw);