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 "qlibhomescreen.h"
18 #include <QJsonDocument>
19 #include <QJsonObject>
20 #include "hmi-debug.h"
23 #define _POPUPREPLY "on_screen_reply"
24 #define _REQ_POPUP_MESSAGE "on_screen_message"
25 #define _TAPSHORTCUT "tap_shortcut"
26 #define _KEY_DATA "data"
27 #define _KEY_APPLICATION_DATA "application_id"
28 #define _KEY_REPLY_MESSAGE "reply_message"
29 #define _KEY_REQUEST_MESSAGE "display_message"
31 static QLibHomeScreen* myThis;
33 // Note: qlibhomescreen will be integrated to libqtappfw
35 * QLibHomeScreen construction function
38 * - parent [in] : object parent.
44 QLibHomeScreen::QLibHomeScreen(QObject *parent) :
49 HMI_DEBUG("qlibhomescreen", "called.");
53 * QLibHomeScreen destruction function
62 QLibHomeScreen::~QLibHomeScreen()
64 HMI_DEBUG("qlibhomescreen", "called.");
73 * call libhomescreen init function to connect to binder by websocket
76 * - prot : port from application
77 * - token : token from application
83 void QLibHomeScreen::init(int port, const QString &token)
85 HMI_DEBUG("qlibhomescreen", "called.");
86 string ctoken = token.toStdString();
87 mp_hs = new LibHomeScreen();
88 mp_hs->init(port, ctoken.c_str());
95 * call on screen message
97 * use libhomescreen api to call onscreen message
100 * - message : message contents
103 * - Returns 0 on success or -1 in case of error.
106 int QLibHomeScreen::onScreenMessage(const QString &message)
108 HMI_DEBUG("qlibhomescreen", "called.");
109 string str = message.toStdString();
110 return mp_hs->onScreenMessage(str.c_str());
116 * use libhomescreen api to subscribe homescreen event
119 * - evetNave : homescreen event name
122 * - Returns 0 on success or -1 in case of error.
125 int QLibHomeScreen::subscribe(const QString &evetName)
127 HMI_DEBUG("qlibhomescreen", "called.");
128 string str = evetName.toStdString();
129 return mp_hs->subscribe(str);
135 * use libhomescreen api to unsubscribe homescreen event
138 * - evetNave : homescreen event name
141 * - Returns 0 on success or -1 in case of error.
144 int QLibHomeScreen::unsubscribe(const QString &evetName)
146 HMI_DEBUG("qlibhomescreen", "called.");
147 string str = evetName.toStdString();
148 return mp_hs->unsubscribe(str);
152 * set homescreen event handler function
155 * - et : homescreen event name
156 * - f : event handler function
162 void QLibHomeScreen::set_event_handler(enum QEventType et, handler_fun f)
164 HMI_DEBUG("qlibhomescreen", "called.");
165 LibHomeScreen::EventType hs_et = (LibHomeScreen::EventType)et;
166 return this->mp_hs->set_event_handler(hs_et, std::move(f));
170 * tapShortcut function
173 * - application_id : tapped application id
179 void QLibHomeScreen::tapShortcut(QString application_id)
181 HMI_DEBUG("qlibhomescreen","tapShortcut %s", application_id.toStdString().c_str());
182 mp_hs->showWindow(application_id.toStdString().c_str(), nullptr);
186 * show application by application id and display area
189 * - application_id : application id
190 * - area : display area liked {"area":"normal"}
196 void QLibHomeScreen::showWindow(QString application_id, json_object* area)
198 mp_hs->showWindow(application_id.toStdString().c_str(), area);
202 * show application by application id and display area
205 * - application_id : application id
206 * - area : display area liked "normal"
212 void QLibHomeScreen::showWindow(QString application_id, QString area)
215 mp_hs->showWindow(application_id.toStdString().c_str(), nullptr);
217 struct json_object *j_obj = json_object_new_object();
218 struct json_object *value = json_object_new_string(area.toStdString().c_str());
219 json_object_object_add(j_obj, "area", value);
220 mp_hs->showWindow(application_id.toStdString().c_str(), j_obj);
225 * hide application by application id
228 * - application_id : application id
234 void QLibHomeScreen::hideWindow(QString application_id)
236 mp_hs->hideWindow(application_id.toStdString().c_str());
240 * send onscreen reply to application
243 * - application_id : application id
244 * - reply : the reply contents
250 void QLibHomeScreen::replyShowWindow(QString application_id, json_object* reply)
252 mp_hs->replyShowWindow(application_id.toStdString().c_str(), reply);
256 * send onscreen reply to application
259 * - application_id : application id
260 * - reply : the reply contents which can convert to json
266 void QLibHomeScreen::replyShowWindow(QString application_id, QString reply)
269 mp_hs->replyShowWindow(application_id.toStdString().c_str(), nullptr);
271 mp_hs->replyShowWindow(application_id.toStdString().c_str(), json_tokener_parse(reply.toStdString().c_str()));
277 * push information to HomeScreen
280 * - info : information that want to show
286 void QLibHomeScreen::showInformation(QString info)
288 struct json_object* j_obj = json_object_new_object();
289 struct json_object* val = json_object_new_string(info.toStdString().c_str());
290 json_object_object_add(j_obj, "info", val);
292 mp_hs->showInformation(j_obj);
298 * push notification to HomeScreen
301 * - icon : provided icon
302 * - text : text that want to show
308 void QLibHomeScreen::showNotification(QString icon, QString text)
310 struct json_object* j_obj = json_object_new_object();
311 struct json_object* val_icon = json_object_new_string(icon.toStdString().c_str());
312 struct json_object* val_text = json_object_new_string(text.toStdString().c_str());
313 json_object_object_add(j_obj, "icon", val_icon);
314 json_object_object_add(j_obj, "text", val_text);
316 mp_hs->showNotification(j_obj);
320 * get runnables list from homescreen-service
329 void QLibHomeScreen::getRunnables(void)
331 mp_hs->getRunnables();
335 * publlish subscription to homescreen-service
344 void QLibHomeScreen::slotPublishSubscription(void) {
345 HMI_NOTICE("qlibhomescreen","slotPublishSubscription");
347 mp_hs->publishSubscription();
348 QObject::disconnect(m_loading);
353 * This function register callback function for reply/event message from home screen
356 * - event_cb [in] : This argument should be specified to the callback for subscribed event
357 * - reply_cb [in] : This argument should be specified to the reply callback for call function
363 * Event callback is invoked by home screen for event you subscribed.
364 * If you would like to get event, please call subscribe function before/after this function
366 void QLibHomeScreen::registerCallback(
367 void (*event_cb)(const std::string& event, struct json_object* event_contents),
368 void (*reply_cb)(struct json_object* reply_contents),
369 void (*hangup_cb)(void))
372 mp_hs->registerCallback(event_cb, reply_cb, hangup_cb);
375 void QLibHomeScreen::setQuickWindow(QQuickWindow *qw)
378 if(mp_window != NULL) {
379 HMI_NOTICE("qlibhomescreen", "connect frameSwapped signal.");
380 m_loading = QObject::connect(mp_window, SIGNAL(frameSwapped()), this, SLOT(slotPublishSubscription()));