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>
21 #include "hmi-debug.h"
23 void* HomescreenHandler::myThis = 0;
25 HomescreenHandler::HomescreenHandler(QObject *parent) :
27 mp_qhs(NULL), mp_wm(NULL), m_role()
32 HomescreenHandler::~HomescreenHandler()
42 void HomescreenHandler::init(const char* role, int port, const char *token)
46 // LibWindowManager initialize
47 mp_wm = new LibWindowmanager();
48 if(mp_wm->init(port,token) != 0){
52 int surface = mp_wm->requestSurface(m_role.c_str());
56 std::string ivi_id = std::to_string(surface);
57 setenv("QT_IVI_SURFACE_ID", ivi_id.c_str(), true);
59 // LibHomeScreen initialize
60 mp_qhs = new QLibHomeScreen();
61 mp_qhs->init(port, token);
65 mp_qhs->registerCallback(nullptr, HomescreenHandler::onRep_static);
67 mp_qhs->set_event_handler(QLibHomeScreen::Event_ShowWindow,[this](json_object *object){
68 HMI_DEBUG("HomeScreen","Surface HomeScreen got Event_ShowWindow\n");
69 static bool first_start = true;
72 this->mp_wm->activateWindow(this->m_role.c_str(), "fullscreen");
76 mp_qhs->set_event_handler(QLibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
77 const char *display_message = json_object_get_string(
78 json_object_object_get(object, "display_message"));
79 HMI_DEBUG("HomeScreen","set_event_handler Event_OnScreenMessage display_message = %s", display_message);
83 void HomescreenHandler::setWMHandler(WMHandler& h) {
84 h.on_sync_draw = [&](const char* role, const char* area, Rect r) {
85 this->mp_wm->endDraw(this->m_role.c_str());
87 mp_wm->setEventHandler(h);
90 void HomescreenHandler::disconnect_frame_swapped(void)
92 qDebug("Let's start homescreen");
93 QObject::disconnect(this->loading);
94 mp_wm->activateWindow(m_role.c_str(), "fullscreen");
97 void HomescreenHandler::attach(QQmlApplicationEngine* engine)
99 QQuickWindow *window = qobject_cast<QQuickWindow *>(engine->rootObjects().first());
100 // this->loading = QObject::connect(window, SIGNAL(frameSwapped()), this, SLOT(disconnect_frame_swapped()));
101 mp_qhs->setQuickWindow(window);
104 void HomescreenHandler::changeLayout(int pattern)
106 HMI_NOTICE("HomeScreen", "Pressed %d, %s", pattern,
107 (pattern == P_LEFT_METER_RIGHT_MAP) ? "left:meter, right:map": "left:map, right:meter");
109 std::unordered_map<std::string, Rect> map_list;
111 case P_LEFT_METER_RIGHT_MAP:
112 map_list["split.main"] = Rect(0, 180, 1280, 720);
113 map_list["split.sub"] = Rect(1280, 180, 640, 720);
115 case P_LEFT_MAP_RIGHT_METER:
116 map_list["split.main"] = Rect(640, 180, 1280, 720);
117 map_list["split.sub"] = Rect(0, 180, 640, 720);
122 if(map_list.size() != 0)
124 req.setAreaReq(map_list);
125 HMI_NOTICE("Homescreen", "Change layout");
126 mp_wm->changeAreaSize(req);
130 void HomescreenHandler::tapShortcut(QString application_name)
132 HMI_DEBUG("HomeScreen","tapShortcut %s", application_name.toStdString().c_str());
133 mp_qhs->tapShortcut(application_name.toStdString().c_str());
136 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
138 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
141 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
143 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
146 void HomescreenHandler::onRep(struct json_object* reply_contents)
148 const char* str = json_object_to_json_string(reply_contents);
149 HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
152 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
154 const char* str = json_object_to_json_string(event_contents);
155 HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
157 if (event.compare("homescreen/on_screen_message") == 0) {
158 struct json_object *json_data = json_object_object_get(event_contents, "data");
159 struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
160 const char* display_message = json_object_get_string(json_display_message);
162 HMI_DEBUG("HomeScreen","display_message = %s", display_message);