7621cf69f7037c64ff0675b58998b533c073e426
[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 <QQmlApplicationEngine>
20 #include <QtQuick/QQuickWindow>
21 #include <QProcess>
22 #include "hmi-debug.h"
23
24 void* HomescreenHandler::myThis = 0;
25
26 HomescreenHandler::HomescreenHandler(QObject *parent) :
27     QObject(parent),
28     mp_qhs(NULL), mp_wm(NULL), m_role()
29 {
30
31 }
32
33 HomescreenHandler::~HomescreenHandler()
34 {
35     if (mp_qhs != NULL) {
36         delete mp_qhs;
37     }
38     if (mp_wm != NULL) {
39         delete mp_wm;
40     }
41 }
42
43 void HomescreenHandler::init(const char* role, int port, const char *token)
44 {
45     this->m_role = role;
46
47     // LibWindowManager initialize
48     mp_wm = new LibWindowmanager();
49     if(mp_wm->init(port,token) != 0){
50         exit(EXIT_FAILURE);
51     }
52
53     int surface = mp_wm->requestSurface(m_role.c_str());
54     if (surface < 0) {
55         exit(EXIT_FAILURE);
56     }
57     std::string ivi_id = std::to_string(surface);
58     setenv("QT_IVI_SURFACE_ID", ivi_id.c_str(), true);
59
60     // LibHomeScreen initialize
61     mp_qhs = new QLibHomeScreen();
62     mp_qhs->init(port, token);
63
64     myThis = this;
65
66     mp_qhs->registerCallback(nullptr, HomescreenHandler::onRep_static);
67
68     mp_qhs->set_event_handler(QLibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
69         const char *display_message = json_object_get_string(
70             json_object_object_get(object, "display_message"));
71         HMI_DEBUG("HomeScreen","set_event_handler Event_OnScreenMessage display_message = %s", display_message);
72     });
73
74     mp_qhs->set_event_handler(QLibHomeScreen::Event_ShowWindow,[this](json_object *object){
75         HMI_DEBUG("HomeScreen","Surface HomeScreen got Event_ShowWindow\n");
76         static bool first_start = true;
77         if (first_start) {
78             first_start = false;
79             this->mp_wm->activateWindow(this->m_role.c_str(), "fullscreen");
80         }
81     });
82 }
83
84 void HomescreenHandler::setWMHandler(WMHandler& h) {
85     h.on_sync_draw = [&](const char* role, const char* area, Rect r) {
86         this->mp_wm->endDraw(this->m_role.c_str());
87     };
88     mp_wm->setEventHandler(h);
89 }
90
91 void HomescreenHandler::disconnect_frame_swapped(void)
92 {
93     qDebug("Let's start homescreen");
94     QObject::disconnect(this->loading);
95     mp_wm->activateWindow(m_role.c_str(), "fullscreen");
96 }
97
98 void HomescreenHandler::attach(QQmlApplicationEngine* engine)
99 {
100     QQuickWindow *window = qobject_cast<QQuickWindow *>(engine->rootObjects().first());
101 //    this->loading = QObject::connect(window, SIGNAL(frameSwapped()), this, SLOT(disconnect_frame_swapped()));
102     mp_qhs->setQuickWindow(window);
103 }
104
105 void HomescreenHandler::changeLayout(int pattern)
106 {
107     HMI_NOTICE("HomeScreen", "Pressed %d, %s", pattern,
108         (pattern == P_LEFT_METER_RIGHT_MAP) ? "left:meter, right:map": "left:map, right:meter");
109     ChangeAreaReq req;
110     std::unordered_map<std::string, Rect> map_list;
111     switch(pattern) {
112         case P_LEFT_METER_RIGHT_MAP:
113             map_list["split.main"] = Rect(0, 0, 1280, 720);
114             map_list["split.sub"] = Rect(1280, 0, 640, 720);
115             break;
116         case P_LEFT_MAP_RIGHT_METER:
117             map_list["split.main"] = Rect(640, 0, 1280, 720);
118             map_list["split.sub"] = Rect(0, 0, 640, 720);
119             break;
120         default:
121             break;
122     }
123     if(map_list.size() != 0)
124     {
125         req.setAreaReq(map_list);
126         HMI_NOTICE("Homescreen", "Change layout");
127         mp_wm->changeAreaSize(req);
128     }
129 }
130
131 void HomescreenHandler::reboot()
132 {
133     QProcess::execute("sync");
134     QProcess::execute("reboot -f");
135 }
136
137 void HomescreenHandler::tapShortcut(QString application_name)
138 {
139     HMI_DEBUG("HomeScreen","tapShortcut %s", application_name.toStdString().c_str());
140     mp_qhs->tapShortcut(application_name.toStdString().c_str());
141 }
142
143 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
144 {
145     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
146 }
147
148 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
149 {
150     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
151 }
152
153 void HomescreenHandler::onRep(struct json_object* reply_contents)
154 {
155     const char* str = json_object_to_json_string(reply_contents);
156     HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
157 }
158
159 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
160 {
161     const char* str = json_object_to_json_string(event_contents);
162     HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
163
164     if (event.compare("homescreen/on_screen_message") == 0) {
165         struct json_object *json_data = json_object_object_get(event_contents, "data");
166         struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
167         const char* display_message = json_object_get_string(json_display_message);
168
169         HMI_DEBUG("HomeScreen","display_message = %s", display_message);
170     }
171 }