2 * Copyright (c) 2019 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 <QGuiApplication>
18 #include <QCommandLineParser>
19 #include <QtGui/QGuiApplication>
20 #include <QtQml/QQmlApplicationEngine>
21 #include <QtQml/QQmlContext>
22 #include <QtQml/qqml.h>
23 #include <QQuickWindow>
24 #include <QtQuickControls2/QQuickStyle>
28 #include <json-c/json.h>
30 #include "eventhandler.h"
31 #include "onscreenmodel.h"
35 "application_id": "onscreenapp",
36 "title": "onscreen title",
37 "type": "information",
38 "contents": "message contents",
39 "buttons": ["button_name1"],
40 "replyto":"onstestapp"
43 static struct json_object *
46 struct json_object *tmp;
47 struct json_object *obj = json_object_new_object();
48 struct json_object *array = json_object_new_array();
50 tmp = json_object_new_string("Big Title");
51 json_object_object_add(obj, "title", tmp);
53 tmp = json_object_new_string("Informatin");
54 json_object_object_add(obj, "type", tmp);
56 tmp = json_object_new_string("Our message contents");
57 json_object_object_add(obj, "contents", tmp);
59 tmp = json_object_new_string("onstestapp");
60 json_object_object_add(obj, "replyto", tmp);
63 tmp = json_object_new_string("Button");
64 json_object_array_add(array, tmp);
65 json_object_object_add(obj, "buttons", array);
67 tmp = json_object_new_string(APP_ID);
68 json_object_object_add(obj, "application_id", tmp);
70 /* adds the entire object */
74 int main(int argc, char *argv[])
76 QGuiApplication app(argc, argv);
79 QCoreApplication::setOrganizationDomain("LinuxFoundation");
80 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
81 QCoreApplication::setApplicationName("Onscreenapp");
82 QCoreApplication::setApplicationVersion("0.1.0");
85 // this is necessary to identify app, setApplicationName is only for the
87 app.setDesktopFileName(APP_ID);
89 //QQuickStyle::setStyle("AGL");
91 QCommandLineParser parser;
92 parser.addPositionalArgument("port", app.translate("main", "port for binding"));
93 parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
94 parser.addHelpOption();
95 parser.addVersionOption();
97 QStringList positionalArguments = parser.positionalArguments();
100 QString token = "wm";
102 if (positionalArguments.length() == 2) {
103 port = positionalArguments.takeFirst().toInt();
104 token = positionalArguments.takeFirst();
107 HMI_DEBUG(APP_ID, "port = %d, token = %s", port, token.toStdString().c_str());
109 QQmlApplicationEngine engine;
110 EventHandler *eventHandler = new EventHandler();
111 eventHandler->init(port, token.toStdString().c_str());
112 engine.rootContext()->setContextProperty("eventHandler", eventHandler);
113 qmlRegisterType<OnScreenModel>("OnScreenModel", 1, 0, "OnScreenModel");
114 engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
115 if (engine.rootObjects().isEmpty()) {
116 HMI_DEBUG(APP_ID, "Fatal Error, rootObject is empty!");
120 QObject *root = engine.rootObjects().first();
121 QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
123 QObject::connect(eventHandler, SIGNAL(updateModel(QVariant)), window, SLOT(setOnScreenModel(QVariant)));
124 QObject::connect(eventHandler, SIGNAL(showOnScreen()), window, SLOT(showOnScreen()));
125 QObject::connect(eventHandler, SIGNAL(hideOnScreen()), window, SLOT(hideOnScreen()));
128 HMI_DEBUG(APP_ID, "onscreenapp started!");
130 /* fake(s) an event */
131 QTimer::singleShot(1000, [eventHandler](){
132 struct json_object *json = build_fake_json();
133 const char *json_str = json_object_to_json_string_ext(json,
134 JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
135 emit eventHandler->updateModel(QVariant(json_str));
136 emit eventHandler->showOnScreen();