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_hs(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_hs = new LibHomeScreen();
61 mp_hs->init(port, token);
65 mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
67 mp_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
68 const char *display_message = json_object_get_string(
69 json_object_object_get(object, "display_message"));
70 HMI_DEBUG("HomeScreen","set_event_handler Event_OnScreenMessage display_message = %s", display_message);
74 void HomescreenHandler::setWMHandler(WMHandler& h) {
75 h.on_sync_draw = [&](const char* role, const char* area, Rect r) {
76 this->mp_wm->endDraw(this->m_role.c_str());
78 mp_wm->setEventHandler(h);
81 void HomescreenHandler::disconnect_frame_swapped(void)
83 qDebug("Let's start homescreen");
84 QObject::disconnect(this->loading);
85 mp_wm->activateWindow(m_role.c_str(), "fullscreen");
88 void HomescreenHandler::attach(QQmlApplicationEngine* engine)
90 QQuickWindow *window = qobject_cast<QQuickWindow *>(engine->rootObjects().first());
91 this->loading = QObject::connect(window, SIGNAL(frameSwapped()), this, SLOT(disconnect_frame_swapped()));
94 void HomescreenHandler::changeLayout(int pattern)
96 HMI_NOTICE("HomeScreen", "Pressed %d, %s", pattern,
97 (pattern == P_LEFT_METER_RIGHT_MAP) ? "left:meter, right:map": "left:map, right:meter");
99 std::unordered_map<std::string, Rect> map_list;
101 case P_LEFT_METER_RIGHT_MAP:
102 map_list["split.main"] = Rect(0, 0, 1280, 720);
103 map_list["split.sub"] = Rect(1280, 0, 640, 720);
105 case P_LEFT_MAP_RIGHT_METER:
106 map_list["split.main"] = Rect(640, 0, 1280, 720);
107 map_list["split.sub"] = Rect(0, 0, 640, 720);
112 if(map_list.size() != 0)
114 req.setAreaReq(map_list);
115 HMI_NOTICE("Homescreen", "Change layout");
116 mp_wm->changeAreaSize(req);
120 void HomescreenHandler::tapShortcut(QString application_name)
122 HMI_DEBUG("HomeScreen","tapShortcut %s", application_name.toStdString().c_str());
123 mp_hs->tapShortcut(application_name.toStdString().c_str());
126 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
128 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
131 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
133 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
136 void HomescreenHandler::onRep(struct json_object* reply_contents)
138 const char* str = json_object_to_json_string(reply_contents);
139 HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
142 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
144 const char* str = json_object_to_json_string(event_contents);
145 HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
147 if (event.compare("homescreen/on_screen_message") == 0) {
148 struct json_object *json_data = json_object_object_get(event_contents, "data");
149 struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
150 const char* display_message = json_object_get_string(json_display_message);
152 HMI_DEBUG("HomeScreen","display_message = %s", display_message);