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"
19 #include <QQmlApplicationEngine>
20 #include <QtQuick/QQuickWindow>
22 #include "hmi-debug.h"
24 void* HomescreenHandler::myThis = 0;
26 HomescreenHandler::HomescreenHandler(QObject *parent) :
28 mp_qhs(NULL), mp_wm(NULL), m_role()
33 HomescreenHandler::~HomescreenHandler()
43 void HomescreenHandler::init(const char* role, int port, const char *token)
47 // LibWindowManager initialize
48 mp_wm = new LibWindowmanager();
49 if(mp_wm->init(port,token) != 0){
53 int surface = mp_wm->requestSurface(m_role.c_str());
57 std::string ivi_id = std::to_string(surface);
58 setenv("QT_IVI_SURFACE_ID", ivi_id.c_str(), true);
60 // LibHomeScreen initialize
61 mp_qhs = new QLibHomeScreen();
62 mp_qhs->init(port, token);
66 mp_qhs->registerCallback(nullptr, HomescreenHandler::onRep_static);
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);
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;
79 this->mp_wm->activateWindow(this->m_role.c_str(), "fullscreen");
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());
88 mp_wm->setEventHandler(h);
91 void HomescreenHandler::disconnect_frame_swapped(void)
93 qDebug("Let's start homescreen");
94 QObject::disconnect(this->loading);
95 mp_wm->activateWindow(m_role.c_str(), "fullscreen");
98 void HomescreenHandler::attach(QQmlApplicationEngine* engine)
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);
105 void HomescreenHandler::changeLayout(int pattern)
107 HMI_NOTICE("HomeScreen", "Pressed %d, %s", pattern,
108 (pattern == P_LEFT_METER_RIGHT_MAP) ? "left:meter, right:map": "left:map, right:meter");
110 std::unordered_map<std::string, Rect> map_list;
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);
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);
123 if(map_list.size() != 0)
125 req.setAreaReq(map_list);
126 HMI_NOTICE("Homescreen", "Change layout");
127 mp_wm->changeAreaSize(req);
131 void HomescreenHandler::reboot()
133 QProcess::execute("sync");
134 QProcess::execute("reboot -f");
137 void HomescreenHandler::tapShortcut(QString application_name)
139 HMI_DEBUG("HomeScreen","tapShortcut %s", application_name.toStdString().c_str());
140 mp_qhs->tapShortcut(application_name.toStdString().c_str());
143 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
145 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
148 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
150 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
153 void HomescreenHandler::onRep(struct json_object* reply_contents)
155 const char* str = json_object_to_json_string(reply_contents);
156 HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
159 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
161 const char* str = json_object_to_json_string(event_contents);
162 HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
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);
169 HMI_DEBUG("HomeScreen","display_message = %s", display_message);