2 * Copyright (c) 2017 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.
17 #include "homescreenhandler.h"
22 #include "hmi-debug.h"
26 void* HomescreenHandler::myThis = 0;
28 HomescreenHandler::HomescreenHandler(QObject *parent) :
31 current_applciation("launcher")
36 HomescreenHandler::~HomescreenHandler()
43 void HomescreenHandler::init(int port, const char *token)
45 mp_hs = new LibHomeScreen();
46 mp_hs->init(port, token);
50 mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
52 mp_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
53 const char *display_message = json_object_get_string(
54 json_object_object_get(object, "display_message"));
55 HMI_DEBUG("HomeScreen","set_event_handler Event_OnScreenMessage display_message = %s", display_message);
59 void HomescreenHandler::tapShortcut(QString application_name, bool is_full)
61 HMI_DEBUG("HomeScreen","tapShortcut %s", application_name.toStdString().c_str());
62 struct json_object* j_json = json_object_new_object();
63 struct json_object* value;
65 value = json_object_new_string("fullscreen");
66 HMI_DEBUG("HomeScreen","ullscreen");
68 value = json_object_new_string("normal");
69 HMI_DEBUG("HomeScreen","normal");
71 json_object_object_add(j_json, "area", value);
72 mp_hs->showWindow(application_name.toStdString().c_str(), j_json);
75 void HomescreenHandler::setCurrentApplication(QString application_name)
77 HMI_DEBUG("HomeScreen","setCurrentApplication %s", application_name.toStdString().c_str());
78 current_applciation = application_name;
81 QString HomescreenHandler::getCurrentApplication()
83 HMI_DEBUG("HomeScreen","getCurrentApplication %s", current_applciation.toStdString().c_str());
84 return current_applciation;
87 int HomescreenHandler::getPidOfApplication(QString application_name) {
89 struct dirent *dir_ent_ptr = NULL;
91 char file_path[50] = {0};
92 char cur_task_ame[50] = {0};
93 char buf[BUF_SIZE] = {0};
96 dir = opendir("/proc");
98 while((dir_ent_ptr = readdir(dir)) != NULL) {
99 if ((strcmp(dir_ent_ptr->d_name, ".") == 0) || (strcmp(dir_ent_ptr->d_name, "..") == 0)
100 || (DT_DIR != dir_ent_ptr->d_type))
102 sprintf(file_path, "/proc/%s/status", dir_ent_ptr->d_name);
103 fp = fopen(file_path, "r");
105 if (fgets(buf, BUF_SIZE - 1, fp) == NULL) {
109 sscanf(buf, "%*s %s", cur_task_ame);
110 if (0 == strcmp(application_name.toStdString().c_str(), cur_task_ame)) {
111 pid = atoi(dir_ent_ptr->d_name);
121 void HomescreenHandler::killRunningApplications()
123 QProcess *proc = new QProcess;
124 QProcess *proc2 = new QProcess;
125 // int num = getPidOfApplication("afbd-video@0.1");
126 // QString procNum = QString::number(num);
127 QString command = "/usr/bin/pkill videoplayer";
128 QString command2 = "/usr/bin/pkill navigation";
129 proc->start(command);
130 proc2->start(command2);
131 HMI_DEBUG("homescreen", command.toStdString().c_str());
132 HMI_DEBUG("homescreen", command2.toStdString().c_str());
135 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
137 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
140 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
142 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
145 void HomescreenHandler::onRep(struct json_object* reply_contents)
147 const char* str = json_object_to_json_string(reply_contents);
148 HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
151 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
153 const char* str = json_object_to_json_string(event_contents);
154 HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
156 if (event.compare("homescreen/on_screen_message") == 0) {
157 struct json_object *json_data = json_object_object_get(event_contents, "data");
158 struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
159 const char* display_message = json_object_get_string(json_display_message);
161 HMI_DEBUG("HomeScreen","display_message = %s", display_message);