add workground funcation
[apps/homescreen.git] / homescreen / src / homescreenhandler.cpp
1 /*
2  * Copyright (c) 2017 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 "homescreenhandler.h"
18 #include <functional>
19 #include <QProcess>
20 #include <dirent.h>
21 #include <stdio.h>
22 #include "hmi-debug.h"
23
24 #define BUF_SIZE                        1024
25
26 void* HomescreenHandler::myThis = 0;
27
28 HomescreenHandler::HomescreenHandler(QObject *parent) :
29     QObject(parent),
30     mp_hs(NULL),
31     current_applciation("launcher")
32 {
33
34 }
35
36 HomescreenHandler::~HomescreenHandler()
37 {
38     if (mp_hs != NULL) {
39         delete mp_hs;
40     }
41 }
42
43 void HomescreenHandler::init(int port, const char *token)
44 {
45     mp_hs = new LibHomeScreen();
46     mp_hs->init(port, token);
47
48     myThis = this;
49
50     mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
51
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);
56     });
57 }
58
59 void HomescreenHandler::tapShortcut(QString application_name, bool is_full)
60 {
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;
64     if(is_full) {
65         value = json_object_new_string("fullscreen");
66         HMI_DEBUG("HomeScreen","ullscreen");
67     } else {
68         value = json_object_new_string("normal");
69         HMI_DEBUG("HomeScreen","normal");
70     }
71     json_object_object_add(j_json, "area", value);
72     mp_hs->showWindow(application_name.toStdString().c_str(), j_json);
73 }
74
75 void HomescreenHandler::setCurrentApplication(QString application_name)
76 {
77     HMI_DEBUG("HomeScreen","setCurrentApplication %s", application_name.toStdString().c_str());
78     current_applciation = application_name;
79 }
80
81 QString HomescreenHandler::getCurrentApplication()
82 {
83     HMI_DEBUG("HomeScreen","getCurrentApplication %s", current_applciation.toStdString().c_str());
84     return current_applciation;
85 }
86
87 int HomescreenHandler::getPidOfApplication(QString application_name) {
88     DIR *dir = NULL;
89     struct dirent *dir_ent_ptr = NULL;
90     FILE *fp = NULL;
91     char file_path[50] = {0};
92     char cur_task_ame[50] = {0};
93     char buf[BUF_SIZE] = {0};
94     int pid = -1;
95
96     dir = opendir("/proc");
97     if (dir) {
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))
101                 continue;
102             sprintf(file_path, "/proc/%s/status", dir_ent_ptr->d_name);
103             fp = fopen(file_path, "r");
104             if (fp) {
105                 if (fgets(buf, BUF_SIZE - 1, fp) == NULL) {
106                     fclose(fp);
107                     continue;
108                 }
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);
112                     break;
113                 }
114             }
115         }
116     }
117
118     return pid;
119 }
120
121 void HomescreenHandler::killRunningApplications()
122 {
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());
133 }
134
135 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
136 {
137     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
138 }
139
140 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
141 {
142     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
143 }
144
145 void HomescreenHandler::onRep(struct json_object* reply_contents)
146 {
147     const char* str = json_object_to_json_string(reply_contents);
148     HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
149 }
150
151 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
152 {
153     const char* str = json_object_to_json_string(event_contents);
154     HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
155
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);
160
161         HMI_DEBUG("HomeScreen","display_message = %s", display_message);
162     }
163 }