summaryrefslogtreecommitdiffstats
path: root/app/main.cpp
blob: 827a5619a90b9832fc18a8be38e9155cbaad080c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <QQmlApplicationEngine>

#include <QtCore/QDebug>
#include <QtCore/QCommandLineParser>
#include <QtCore/QUrlQuery>
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlContext>
#include <QtQuick/QQuickWindow>
#include <navigation.h>
#include <vehiclesignals.h>
#include <QScreen>

#include "navigation_client.h"
#include "qcheapruler.hpp"
#include "file_operation.h"
#include "shell-desktop.h"

#include <qpa/qplatformnativeinterface.h>
#include <wayland-client.h>

#include "wayland-agl-shell-desktop-client-protocol.h"

#define OUTPUT_ID	"remoting"

static void
global_add(void *data, struct wl_registry *reg, uint32_t name,
	   const char *interface, uint32_t)
{
	struct agl_shell_desktop **shell = static_cast<struct agl_shell_desktop **>(data);

	if (strcmp(interface, agl_shell_desktop_interface.name) == 0) {
		*shell = static_cast<struct agl_shell_desktop *>(
			wl_registry_bind(reg, name, &agl_shell_desktop_interface, 1)
		);
	}
}

static void
global_remove(void *data, struct wl_registry *reg, uint32_t id)
{
	/* Don't care */
	(void) data;
	(void) reg;
	(void) id;
}

static const struct wl_registry_listener registry_listener = {
	global_add,
	global_remove,
};

static struct agl_shell_desktop *
register_agl_shell_desktop(QPlatformNativeInterface *native)
{
	struct wl_display *wl;
	struct wl_registry *registry;
	struct agl_shell_desktop *shell = nullptr;

	wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
	registry = wl_display_get_registry(wl);

	wl_registry_add_listener(registry, &registry_listener, &shell);

	/* Roundtrip to get all globals advertised by the compositor */
	wl_display_roundtrip(wl);
	wl_registry_destroy(registry);

	return shell;
}


static QScreen *
find_qscreen(const char *screen_name)
{
	QList<QScreen *> screens = qApp->screens();
	QScreen *found = nullptr;
	QString qstr_name = QString::fromUtf8(screen_name, -1);

	for (QScreen *xscreen : screens) {
		if (qstr_name == xscreen->name()) {
			found = xscreen;
			break;
		}
	}

	return found;
}

int main(int argc, char *argv[])
{
	QString graphic_role = QString("tbtnavi");
	struct agl_shell_desktop *agl_shell_desktop = nullptr;

	setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);

	QGuiApplication app(argc, argv);
	QCoreApplication::setOrganizationDomain("automotivelinux.org");
	QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
	QCoreApplication::setApplicationName(graphic_role);
	QCoreApplication::setApplicationVersion("0.1.0");
	app.setDesktopFileName(graphic_role);

	QPlatformNativeInterface *native = qApp->platformNativeInterface();
	agl_shell_desktop = register_agl_shell_desktop(native);
	if (!agl_shell_desktop) {
		qDebug() << "Could not find agl_shell_desktop extension. Is agl-compositor running?";
		exit(EXIT_FAILURE);
	}

	std::shared_ptr<struct agl_shell_desktop> shell{agl_shell_desktop, agl_shell_desktop_destroy};
	Shell *aglShell = new Shell(shell, nullptr);

	/* inform the compositor that the window be placed on a different
	 * output */
	QScreen *screen_to_put = find_qscreen(OUTPUT_ID);
	if (screen_to_put) {
		qDebug() << "Found screen to put " << screen_to_put->name();
		aglShell->set_window_on_screen(screen_to_put, graphic_role);
	} else {
		qDebug() << "Couldn't find screen to put " << OUTPUT_ID;
		qDebug() << "Available screens: ";
		for (auto &ss: qApp->screens()) {
			qDebug() << "screen: " << ss->name();
		}
	}

	// Load qml
	QQmlApplicationEngine engine;

	qmlRegisterType<QCheapRuler>("com.mapbox.cheap_ruler", 1, 0, "CheapRuler");

	QQmlContext *context = engine.rootContext();

	File_Operation file;
	context->setContextProperty("fileOperation", &file);

	VehicleSignalsConfig vsConfig("tbtnavi");
	VehicleSignals *vs = new VehicleSignals(vsConfig);
	if (!vs) {
		qFatal("Could not create VehicleSignals!");
	}
	context->setContextProperty("VehicleSignals", vs);

	// Give the navigation client it's own vehicle signals connection
	// to simplify state management wrt QML initialization, and keep the
	// notification streams separate.
	Navigation *navigation = new Navigation(new VehicleSignals(vsConfig), context);

	engine.load(QUrl(QStringLiteral("qrc:qml/Main.qml")));

	QObject *root = engine.rootObjects().first();
	new navigation_client(navigation, root->findChild<QObject*>("mapwindow"));

	return app.exec();
}