summaryrefslogtreecommitdiffstats
path: root/shell/src/main.cpp
blob: 75cb6eca2d34c3f7d13c47b727fca0bfcbc7abac (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <QGuiApplication>
#include <QCommandLineParser>
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlContext>
#include <QtQml/QQmlComponent>
#include <QtQml/qqml.h>
#include <QWindow>
#include <QQuickWindow>
#include <QScreen>
#include <QTimer>
#include <qpa/qplatformnativeinterface.h>

#include <cstdlib>
#include <cstring>
#include <memory>
#include <wayland-client.h>
#include <time.h>

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

// QGuiApplication::platformNativeInterface

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

	if (strcmp(interface, agl_shell_interface.name) == 0) {
		*shell = static_cast<struct agl_shell *>(
			wl_registry_bind(reg, name, &agl_shell_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,
};

wl_surface *
getWlSurface(QWindow *window)
{
	QPlatformNativeInterface *native = qApp->platformNativeInterface();
	
	//void *surf = QGuiApplication::platformNativeInterface()->nativeResourceForWindow("surface", window);
	void *surf = native->nativeResourceForWindow("surface", window);
	return static_cast<struct ::wl_surface *>(surf);
}

struct wl_display *
getWlDisplay(void)
{
	QPlatformNativeInterface *native = qApp->platformNativeInterface();

	//void *display = QGuiApplication::platformNativeInterface()->nativeResourceForIntegration("display");
	void *display = native->nativeResourceForIntegration("display");
	return static_cast<struct ::wl_display *>(display);
}

struct wl_compositor *
getWlCompositor(QWindow *window)
{
	QPlatformNativeInterface *native = qApp->platformNativeInterface();

	//void *compositor = QGuiApplication::platformNativeInterface()->nativeResourceForWindow("compositor", window);
	void *compositor = native->nativeResourceForWindow("compositor", window);
	return static_cast<struct ::wl_compositor*>(compositor);
}

struct wl_output *
getWlOutput(QWindow *window)
{
	QPlatformNativeInterface *native = qApp->platformNativeInterface();

	//void *output = QGuiApplication::platformNativeInterface()->nativeResourceForScreen("output", window->screen());
	void *output = native->nativeResourceForScreen("output", window->screen());
	return static_cast<struct ::wl_output*>(output);
}

struct wl_output *
getWlOutput(QScreen *screen)
{
	QPlatformNativeInterface *native = qApp->platformNativeInterface();

	//void *output = QGuiApplication::platformNativeInterface()->nativeResourceForScreen("output", window->screen());
	void *output = native->nativeResourceForScreen("output", screen);
	return static_cast<struct ::wl_output*>(output);
}

static struct wl_surface *
create_component(QQmlComponent *comp, QScreen *screen)
{
	QObject *obj = comp->create();
	obj->setParent(screen);

	QWindow *win = qobject_cast<QWindow *>(obj);
	return getWlSurface(win);
}


int main(int argc, char *argv[])
{
	setenv("QT_QPA_PLATFORM", "wayland", 1);
	QGuiApplication app(argc, argv);
	app.setDesktopFileName("homescreen-demo-ci");

	QPlatformNativeInterface *native = qApp->platformNativeInterface();
	struct agl_shell *agl_shell = nullptr;

	struct wl_display *wl;
	struct wl_registry *registry;

	char *xdgdir = getenv("XDG_RUNTIME_DIR");
	if (xdgdir) {
		qInfo() << "xdg runtime dir " << xdgdir;
	}

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

	wl_registry_add_listener(registry, &registry_listener, &agl_shell);
	// Roundtrip to get all globals advertised by the compositor
	wl_display_roundtrip(wl);
	wl_registry_destroy(registry);

	// check if binding to agl shell worked
	std::shared_ptr<struct agl_shell> shell{agl_shell, agl_shell_destroy};

	QQmlApplicationEngine engine;
	QQmlContext *context = engine.rootContext();

	context->setContextProperty("shell", new Shell(shell, &app));

	QQmlComponent bg_comp(&engine, QUrl("qrc:/bg.qml"));
	qInfo() << bg_comp.errors();

	QQmlComponent top_comp(&engine, QUrl("qrc:/top.qml"));
	qInfo() << top_comp.errors();

	QQmlComponent bot_comp(&engine, QUrl("qrc:/bottom.qml"));
	qInfo() << bot_comp.errors();

	QQmlComponent left_comp(&engine, QUrl("qrc:/left.qml"));
	qInfo() << left_comp.errors();

	QQmlComponent right_comp(&engine, QUrl("qrc:/right.qml"));
	qInfo() << right_comp.errors();

	struct wl_surface *bg, *top, *bot;
	struct wl_output *output;

	QScreen *screen = app.primaryScreen();
	qDebug() << "primary screen is " << screen->name();

	if (qApp->screens().length() > 1) {
		for (auto &ss: qApp->screens()) {
			bg = create_component(&bg_comp, ss);
			output = getWlOutput(ss);
			qDebug() << "Seting background to screen " << ss->name();
			agl_shell_set_background(agl_shell, bg, output);
		}
	} else {
		bg = create_component(&bg_comp, screen);
		output = getWlOutput(screen);
		qDebug() << "Seting background to screen " << screen->name();
		agl_shell_set_background(agl_shell, bg, output);
	}

	top = create_component(&top_comp, screen);
	bot = create_component(&bot_comp, screen);

	agl_shell_set_panel(agl_shell, bot, output, AGL_SHELL_EDGE_BOTTOM);
	agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);

	/* need to wait a bit in order for qtwayland to start dispatching */
	QTimer::singleShot(500, [agl_shell](){
		agl_shell_ready(agl_shell);
	});

	return app.exec();
}