modify homescreen seq
[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 "hmi-debug.h"
22
23 void* HomescreenHandler::myThis = 0;
24
25 HomescreenHandler::HomescreenHandler(QObject *parent) :
26     QObject(parent),
27     mp_qhs(NULL), mp_wm(NULL), m_role()
28 {
29
30 }
31
32 HomescreenHandler::~HomescreenHandler()
33 {
34     if (mp_qhs != NULL) {
35         delete mp_qhs;
36     }
37     if (mp_wm != NULL) {
38         delete mp_wm;
39     }
40 }
41
42 void HomescreenHandler::init(const char* role, int port, const char *token)
43 {
44     this->m_role = role;
45
46     // LibWindowManager initialize
47     mp_wm = new LibWindowmanager();
48     if(mp_wm->init(port,token) != 0){
49         exit(EXIT_FAILURE);
50     }
51
52     int surface = mp_wm->requestSurface(m_role.c_str());
53     if (surface < 0) {
54         exit(EXIT_FAILURE);
55     }
56     std::string ivi_id = std::to_string(surface);
57     setenv("QT_IVI_SURFACE_ID", ivi_id.c_str(), true);
58
59     // LibHomeScreen initialize
60     mp_qhs = new QLibHomeScreen();
61     mp_qhs->init(port, token);
62
63     myThis = this;
64
65     mp_qhs->registerCallback(nullptr, HomescreenHandler::onRep_static);
66
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;
70         if (first_start) {
71             first_start = false;
72             this->mp_wm->activateWindow(this->m_role.c_str(), "fullscreen");
73         }
74     });
75
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);
80     });
81 }
82
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());
86     };
87     mp_wm->setEventHandler(h);
88 }
89
90 void HomescreenHandler::disconnect_frame_swapped(void)
91 {
92     qDebug("Let's start homescreen");
93     QObject::disconnect(this->loading);
94     mp_wm->activateWindow(m_role.c_str(), "fullscreen");
95 }
96
97 void HomescreenHandler::attach(QQmlApplicationEngine* engine)
98 {
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);
102 }
103
104 void HomescreenHandler::changeLayout(int pattern)
105 {
106     HMI_NOTICE("HomeScreen", "Pressed %d, %s", pattern,
107         (pattern == P_LEFT_METER_RIGHT_MAP) ? "left:meter, right:map": "left:map, right:meter");
108     ChangeAreaReq req;
109     std::unordered_map<std::string, Rect> map_list;
110     switch(pattern) {
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);
114             break;
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);
118             break;
119         default:
120             break;
121     }
122     if(map_list.size() != 0)
123     {
124         req.setAreaReq(map_list);
125         HMI_NOTICE("Homescreen", "Change layout");
126         mp_wm->changeAreaSize(req);
127     }
128 }
129
130 void HomescreenHandler::tapShortcut(QString application_name)
131 {
132     HMI_DEBUG("HomeScreen","tapShortcut %s", application_name.toStdString().c_str());
133     mp_qhs->tapShortcut(application_name.toStdString().c_str());
134 }
135
136 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
137 {
138     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
139 }
140
141 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
142 {
143     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
144 }
145
146 void HomescreenHandler::onRep(struct json_object* reply_contents)
147 {
148     const char* str = json_object_to_json_string(reply_contents);
149     HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
150 }
151
152 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
153 {
154     const char* str = json_object_to_json_string(event_contents);
155     HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
156
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);
161
162         HMI_DEBUG("HomeScreen","display_message = %s", display_message);
163     }
164 }