change size to 1920x1080
[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_hs(NULL), mp_wm(NULL), m_role()
28 {
29
30 }
31
32 HomescreenHandler::~HomescreenHandler()
33 {
34     if (mp_hs != NULL) {
35         delete mp_hs;
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_hs = new LibHomeScreen();
61     mp_hs->init(port, token);
62
63     myThis = this;
64
65     mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
66
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);
71     });
72 }
73
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());
77     };
78     mp_wm->setEventHandler(h);
79 }
80
81 void HomescreenHandler::disconnect_frame_swapped(void)
82 {
83     qDebug("Let's start homescreen");
84     QObject::disconnect(this->loading);
85     mp_wm->activateWindow(m_role.c_str(), "fullscreen");
86 }
87
88 void HomescreenHandler::attach(QQmlApplicationEngine* engine)
89 {
90     QQuickWindow *window = qobject_cast<QQuickWindow *>(engine->rootObjects().first());
91     this->loading = QObject::connect(window, SIGNAL(frameSwapped()), this, SLOT(disconnect_frame_swapped()));
92 }
93
94 void HomescreenHandler::changeLayout(int pattern)
95 {
96     HMI_NOTICE("HomeScreen", "Pressed %d, %s", pattern,
97         (pattern == P_LEFT_METER_RIGHT_MAP) ? "left:meter, right:map": "left:map, right:meter");
98     ChangeAreaReq req;
99     std::unordered_map<std::string, Rect> map_list;
100     switch(pattern) {
101         case P_LEFT_METER_RIGHT_MAP:
102             map_list["split.main"] = Rect(0, 180, 1280, 720);
103             map_list["split.sub"] = Rect(1280, 180, 640, 720);
104             break;
105         case P_LEFT_MAP_RIGHT_METER:
106             map_list["split.main"] = Rect(640, 180, 1280, 720);
107             map_list["split.sub"] = Rect(0, 180, 640, 720);
108             break;
109         default:
110             break;
111     }
112     if(map_list.size() != 0)
113     {
114         req.setAreaReq(map_list);
115         HMI_NOTICE("Homescreen", "Change layout");
116         mp_wm->changeAreaSize(req);
117     }
118 }
119
120 void HomescreenHandler::tapShortcut(QString application_name)
121 {
122     HMI_DEBUG("HomeScreen","tapShortcut %s", application_name.toStdString().c_str());
123     mp_hs->tapShortcut(application_name.toStdString().c_str());
124 }
125
126 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
127 {
128     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
129 }
130
131 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
132 {
133     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
134 }
135
136 void HomescreenHandler::onRep(struct json_object* reply_contents)
137 {
138     const char* str = json_object_to_json_string(reply_contents);
139     HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
140 }
141
142 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
143 {
144     const char* str = json_object_to_json_string(event_contents);
145     HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
146
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);
151
152         HMI_DEBUG("HomeScreen","display_message = %s", display_message);
153     }
154 }