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"
30 #define _KEY_SHORTCUT "shortcut"
31 #define _KEY_SHORTCUT_ID "shortcut_id"
32 #define _KEY_SHORTCUT_NAME "shortcut_name"
33 #define _KEY_POSITION "position"
35 static QLibHomeScreen* myThis;
37 // Note: qlibhomescreen will be integrated to libqtappfw
39 * QLibHomeScreen construction function
42 * - parent [in] : object parent.
48 QLibHomeScreen::QLibHomeScreen(QObject *parent) :
53 HMI_DEBUG("qlibhomescreen", "called.");
57 * QLibHomeScreen destruction function
66 QLibHomeScreen::~QLibHomeScreen()
68 HMI_DEBUG("qlibhomescreen", "called.");
77 * call libhomescreen init function to connect to binder by websocket
80 * - prot : port from application
81 * - token : token from application
87 void QLibHomeScreen::init(int port, const QString &token)
89 HMI_DEBUG("qlibhomescreen", "called.");
90 string ctoken = token.toStdString();
91 mp_hs = new LibHomeScreen();
92 mp_hs->init(port, ctoken.c_str());
99 * call on screen message
101 * use libhomescreen api to call onscreen message
104 * - message : message contents
107 * - Returns 0 on success or -1 in case of error.
110 int QLibHomeScreen::onScreenMessage(const QString &message)
112 HMI_DEBUG("qlibhomescreen", "called.");
113 string str = message.toStdString();
114 return mp_hs->onScreenMessage(str.c_str());
120 * use libhomescreen api to subscribe homescreen event
123 * - evetNave : homescreen event name
126 * - Returns 0 on success or -1 in case of error.
129 int QLibHomeScreen::subscribe(const QString &evetName)
131 HMI_DEBUG("qlibhomescreen", "called.");
132 string str = evetName.toStdString();
133 return mp_hs->subscribe(str);
139 * use libhomescreen api to unsubscribe homescreen event
142 * - evetNave : homescreen event name
145 * - Returns 0 on success or -1 in case of error.
148 int QLibHomeScreen::unsubscribe(const QString &evetName)
150 HMI_DEBUG("qlibhomescreen", "called.");
151 string str = evetName.toStdString();
152 return mp_hs->unsubscribe(str);
156 * set homescreen event handler function
159 * - et : homescreen event name
160 * - f : event handler function
166 void QLibHomeScreen::set_event_handler(enum QEventType et, handler_fun f)
168 HMI_DEBUG("qlibhomescreen", "called.");
169 LibHomeScreen::EventType hs_et = (LibHomeScreen::EventType)et;
170 this->mp_hs->set_event_handler(hs_et, std::move(f));
174 * tapShortcut function
177 * - application_id : tapped application id
183 void QLibHomeScreen::tapShortcut(QString application_id)
185 HMI_DEBUG("qlibhomescreen","tapShortcut %s", application_id.toStdString().c_str());
186 mp_hs->showWindow(application_id.toStdString().c_str(), nullptr);
190 * show application by application id and display area
193 * - application_id : application id
194 * - area : display area liked {"area":"normal"}
200 void QLibHomeScreen::showWindow(QString application_id, json_object* area)
202 mp_hs->showWindow(application_id.toStdString().c_str(), area);
206 * show application by application id and display area
209 * - application_id : application id
210 * - area : display area liked "normal"
216 void QLibHomeScreen::showWindow(QString application_id, QString area)
219 mp_hs->showWindow(application_id.toStdString().c_str(), nullptr);
221 struct json_object *j_obj = json_object_new_object();
222 struct json_object *value = json_object_new_string(area.toStdString().c_str());
223 json_object_object_add(j_obj, "area", value);
224 mp_hs->showWindow(application_id.toStdString().c_str(), j_obj);
229 * hide application by application id
232 * - application_id : application id
238 void QLibHomeScreen::hideWindow(QString application_id)
240 mp_hs->hideWindow(application_id.toStdString().c_str());
244 * send onscreen reply to application
247 * - application_id : application id
248 * - reply : the reply contents
254 void QLibHomeScreen::replyShowWindow(QString application_id, json_object* reply)
256 mp_hs->replyShowWindow(application_id.toStdString().c_str(), reply);
260 * send onscreen reply to application
263 * - application_id : application id
264 * - reply : the reply contents which can convert to json
270 void QLibHomeScreen::replyShowWindow(QString application_id, QString reply)
273 mp_hs->replyShowWindow(application_id.toStdString().c_str(), nullptr);
275 mp_hs->replyShowWindow(application_id.toStdString().c_str(), json_tokener_parse(reply.toStdString().c_str()));
281 * push information to HomeScreen
284 * - info : information that want to show
290 void QLibHomeScreen::showInformation(QString info)
292 struct json_object* j_obj = json_object_new_object();
293 struct json_object* val = json_object_new_string(info.toStdString().c_str());
294 json_object_object_add(j_obj, "info", val);
296 mp_hs->showInformation(j_obj);
302 * push notification to HomeScreen
305 * - icon : provided icon
306 * - text : text that want to show
312 void QLibHomeScreen::showNotification(QString icon, QString text)
314 struct json_object* j_obj = json_object_new_object();
315 struct json_object* val_icon = json_object_new_string(icon.toStdString().c_str());
316 struct json_object* val_text = json_object_new_string(text.toStdString().c_str());
317 json_object_object_add(j_obj, "icon", val_icon);
318 json_object_object_add(j_obj, "text", val_text);
320 mp_hs->showNotification(j_obj);
324 * get runnables list from homescreen-service
333 void QLibHomeScreen::getRunnables(void)
335 mp_hs->getRunnables();
339 * publlish subscription to homescreen-service
348 void QLibHomeScreen::slotPublishSubscription(void) {
349 HMI_NOTICE("qlibhomescreen","slotPublishSubscription");
351 mp_hs->publishSubscription();
352 QObject::disconnect(m_loading);
357 * This function register callback function for reply/event message from home screen
360 * - event_cb [in] : This argument should be specified to the callback for subscribed event
361 * - reply_cb [in] : This argument should be specified to the reply callback for call function
367 * Event callback is invoked by home screen for event you subscribed.
368 * If you would like to get event, please call subscribe function before/after this function
370 void QLibHomeScreen::registerCallback(
371 void (*event_cb)(const std::string& event, struct json_object* event_contents),
372 void (*reply_cb)(struct json_object* reply_contents),
373 void (*hangup_cb)(void))
376 mp_hs->registerCallback(event_cb, reply_cb, hangup_cb);
379 void QLibHomeScreen::setQuickWindow(QQuickWindow *qw)
382 if(mp_window != NULL) {
383 HMI_NOTICE("qlibhomescreen", "connect frameSwapped signal.");
384 m_loading = QObject::connect(mp_window, SIGNAL(frameSwapped()), this, SLOT(slotPublishSubscription()));
389 * shortcut_id : "dashboard"
390 * shortcut_name : "Dashboard"
391 * positon : 1~3, from left to right on homescreen
393 int QLibHomeScreen::registerShortcut(QString shortcut_id, QString shortcut_name, QString position)
395 struct json_object* j_obj = json_object_new_object();
396 struct json_object* val_id = json_object_new_string(shortcut_id.toStdString().c_str());
397 struct json_object* val_name = json_object_new_string(shortcut_name.toStdString().c_str());
398 struct json_object* val_position = json_object_new_string(position.toStdString().c_str());
399 json_object_object_add(j_obj, _KEY_SHORTCUT_ID, val_id);
400 json_object_object_add(j_obj, _KEY_SHORTCUT_NAME, val_name);
401 json_object_object_add(j_obj, _KEY_POSITION, val_position);
403 return mp_hs->registerShortcut("homescreen", j_obj);
407 * shortcut_id : "shocut_id1,shocut_id2,shocut_id3"
408 * shortcut_name : "shortcut_name1,shortcut_name2,shortcut_name3"
410 int QLibHomeScreen::updateShortcut(QString application_id, QString shortcut_id, QString shortcut_name)
412 QStringList id = shortcut_id.split(',');
413 QStringList name = shortcut_name.split(',');
414 if(id.size() == 0 || id.size() != name.size()) {
415 HMI_NOTICE("qlibhomescreen","input parameters error.");
419 // make json contents
420 struct json_object* arr_obj = json_object_new_array();
421 for(int i = 0; i < id.size(); ++i) {
422 struct json_object *j_obj = json_object_new_object();
423 json_object_object_add(j_obj, _KEY_SHORTCUT_ID, json_object_new_string(id[i].toStdString().c_str()));
424 json_object_object_add(j_obj, _KEY_SHORTCUT_NAME, json_object_new_string(name[i].toStdString().c_str()));
425 json_object_array_add(arr_obj, j_obj);
427 struct json_object* push_obj = json_object_new_object();
428 json_object_object_add(push_obj, _KEY_SHORTCUT, arr_obj);
429 return mp_hs->updateShortcut(application_id.toStdString().c_str(), push_obj);
432 int QLibHomeScreen::updateShortcut(QString application_id, struct json_object* obj)
434 return mp_hs->updateShortcut(application_id.toStdString().c_str(), obj);