protocol: Allow to activate windows
authorMarius Vlad <marius.vlad@collabora.com>
Sat, 11 Apr 2020 15:52:21 +0000 (18:52 +0300)
committerMarius Vlad <marius.vlad@collabora.com>
Sat, 11 Apr 2020 15:52:53 +0000 (18:52 +0300)
Signed-off-by: Marius Vlad <marius.vlad@collabora.com>
launcher/launcher.pro
launcher/protocol/agl-shell-desktop.xml [new file with mode: 0644]
launcher/src/main.cpp
launcher/src/shell-desktop.cpp [new file with mode: 0644]
launcher/src/shell-desktop.h [new file with mode: 0644]

index 513dd6d..f08b26e 100644 (file)
 
 TEMPLATE = app
 TARGET = launcher
-QT = qml quick dbus websockets
-CONFIG += c++11 link_pkgconfig
+QT = qml quick dbus websockets gui-private
+CONFIG += c++11 link_pkgconfig wayland-scanner pkgdatadir
 DESTDIR = $${OUT_PWD}/../package/root/bin
+PKGCONFIG += wayland-client
 
 include(../pws/pws.pri)
 
 SOURCES += \
     src/main.cpp \
     src/applicationmodel.cpp \
+    src/shell-desktop.cpp \
     src/appinfo.cpp \
     src/applicationlauncher.cpp \
     src/applicationhandler.cpp
@@ -31,6 +33,7 @@ SOURCES += \
 HEADERS  += \
     src/applicationlauncher.h \
     src/applicationmodel.h \
+    src/shell-desktop.h \
     src/appinfo.h \
     src/applicationhandler.h
 
@@ -40,3 +43,6 @@ OTHER_FILES += \
 RESOURCES += \
     qml/images/images.qrc \
     qml/qml.qrc
+
+WAYLANDCLIENTSOURCES += \
+    protocol/agl-shell-desktop.xml
diff --git a/launcher/protocol/agl-shell-desktop.xml b/launcher/protocol/agl-shell-desktop.xml
new file mode 100644 (file)
index 0000000..6d53f92
--- /dev/null
@@ -0,0 +1,96 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<protocol name="agl_shell_desktop">
+  <copyright>
+    Copyright © 2020 Collabora, Ltd.
+
+    Permission is hereby granted, free of charge, to any person obtaining a
+    copy of this software and associated documentation files (the "Software"),
+    to deal in the Software without restriction, including without limitation
+    the rights to use, copy, modify, merge, publish, distribute, sublicense,
+    and/or sell copies of the Software, and to permit persons to whom the
+    Software is furnished to do so, subject to the following conditions:
+
+    The above copyright notice and this permission notice (including the next
+    paragraph) shall be included in all copies or substantial portions of the
+    Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+    DEALINGS IN THE SOFTWARE.
+  </copyright>
+  <interface name="agl_shell_desktop" version="1">
+    <description summary="Private extension to allow applications activate other apps">
+      This extension can be used by regular application to instruct to compositor
+      to activate or switch to other running (regular) applications. The client
+      is responsbile for filtering their own app_id when receiving application id.
+
+      Note that other (regular) applications can bind to this interface and there is
+      no mechanism to place to restrict or limit that.
+    </description>
+
+    <enum name="app_role">
+      <entry name="popup" value="0"/>
+      <entry name="fullscreen" value="1"/>
+    </enum>
+
+    <event name="application">
+      <description summary="advertise application id">
+        The compositor may choose to advertise one or more application ids which
+        can be used to activate/switch to.
+
+        When this global is bound, the compositor will send all application ids
+        available for activation, but may send additional application id at any
+        time (when they've been mapped in the compositor).
+      </description>
+      <arg name="app_id" type="string"/>
+    </event>
+
+    <request name="activate_app">
+      <description summary="make client current window">
+        Ask the compositor to make a toplevel to become the current/focused
+        window for window management purposes.
+
+        See xdg_toplevel.set_app_id from the xdg-shell protocol for a
+        description of app_id.
+      </description>
+      <arg name="app_id" type="string"/>
+      <arg name="output" type="object" interface="wl_output"/>
+    </request>
+
+    <request name="set_app_property">
+      <description summary="set properties for a client identified by app_id">
+        Ask the compositor to make a toplevel obey the app_role and, depending
+        on the role, to use the the x and y values as initial positional values.
+        The x and y values would only make sense for certain roles.
+
+        See xdg_toplevel.set_app_id from the xdg-shell protocol for a
+        description of app_id.
+      </description>
+      <arg name="app_id" type="string"/>
+      <arg name="role" type="uint" enum="app_role"/>
+      <arg name="x" type="int"/>
+      <arg name="y" type="int"/>
+      <arg name="output" type="object" interface="wl_output"/>
+    </request>
+
+    <request name="deactivate_app">
+      <description summary="de-activate/hide window identified by app_id">
+        Ask the compositor to hide the toplevel window for window
+        management purposes. Depending on the window role, this request
+        will either display the previously active window (or the background
+        in case there's no previously activate surface) or temporarly (or
+        until a 'activate_app' is called upon) hide the surface. All
+        the surfaces are identifiable by using the app_id, and no actions are
+        taken in case the app_id is not/was not present.
+
+        See xdg_toplevel.set_app_id from the xdg-shell protocol for a
+        description of app_id.
+      </description>
+      <arg name="app_id" type="string"/>
+    </request>
+  </interface>
+</protocol>
index 6d20ec8..2355022 100644 (file)
 #include <QQuickWindow>
 #include <QThread>
 #include <QUrlQuery>
+#include <qpa/qplatformnativeinterface.h>
 
 #include <QDBusMetaType>
 #include <QDBusArgument>
 
+#include <wayland-client.h>
+#include "wayland-agl-shell-desktop-client-protocol.h"
+
 #include "applicationlauncher.h"
 #include "applicationmodel.h"
 #include "applicationhandler.h"
+#include "shell-desktop.h"
 #include "appinfo.h"
 
 #define CONNECT_STR "unix:/run/platform/apis/ws/afm-main"
 
+// this and the agl-shell extension should be added in some kind of a wrapper
+// for easy usage
+static void global_add(void *data, struct wl_registry *reg, uint32_t name,
+               const char *interface, uint32_t version)
+{
+       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, version)
+                               );
+       }
+}
+
+static void global_remove(void *data, struct wl_registry *reg, uint32_t id)
+{
+       (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(void)
+{
+       struct wl_display *wl;
+       struct wl_registry *registry;
+       struct agl_shell_desktop *shell = nullptr;
+
+       QPlatformNativeInterface *native = qApp->platformNativeInterface();
+
+       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;
+}
+
+
+
 int main(int argc, char *argv[])
 {
     QString myname = QString("launcher");
+    struct agl_shell_desktop *ashell = nullptr;
     QGuiApplication a(argc, argv);
 
     QCommandLineParser parser;
@@ -67,6 +122,7 @@ int main(int argc, char *argv[])
            new ApplicationLauncher(CONNECT_STR, &a);
     ApplicationHandler *homescreenHandler =
            new ApplicationHandler(nullptr, launcher->get_launcher());
+    ashell = register_agl_shell_desktop();
 
     QUrl bindingAddress;
     bindingAddress.setScheme(QStringLiteral("ws"));
@@ -83,6 +139,13 @@ int main(int argc, char *argv[])
 
     engine.rootContext()->setContextProperty(QStringLiteral("homescreenHandler"), homescreenHandler);
     engine.rootContext()->setContextProperty(QStringLiteral("launcher"), launcher);
+
+    if (ashell) {
+           std::shared_ptr<struct agl_shell_desktop> shell{ashell, agl_shell_desktop_destroy};
+           Shell *agl_shell = new Shell(shell, &a);
+           engine.rootContext()->setContextProperty("shell", agl_shell);
+    }
+
     engine.load(QUrl(QStringLiteral("qrc:/Launcher.qml")));
 
     homescreenHandler->getRunnables();
diff --git a/launcher/src/shell-desktop.cpp b/launcher/src/shell-desktop.cpp
new file mode 100644 (file)
index 0000000..8503c01
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2020 Collabora Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <QGuiApplication>
+#include <QDebug>
+#include "shell-desktop.h"
+#include <qpa/qplatformnativeinterface.h>
+#include <stdio.h>
+
+static struct wl_output *
+getWlOutput(QScreen *screen)
+{
+        QPlatformNativeInterface *native = qApp->platformNativeInterface();
+
+        void *output = native->nativeResourceForScreen("output", screen);
+        return static_cast<struct ::wl_output*>(output);
+}
+
+void Shell::activate_app(QWindow *win, const QString &app_id)
+{
+       QScreen *screen = nullptr;
+       struct wl_output *output;
+
+       if (!win || !win->screen()) {
+               qDebug() << "window or screen, not available, using the first one available";
+               screen = qApp->screens().first();
+       } else {
+               screen = win->screen();
+       }
+
+       if (!screen) {
+               qDebug() << "screen not available?";
+               return;
+       }
+
+       output = getWlOutput(screen);
+       qDebug() << "will activate app: " << app_id;
+       agl_shell_desktop_activate_app(this->shell.get(),
+                              app_id.toStdString().c_str(), output);
+}
diff --git a/launcher/src/shell-desktop.h b/launcher/src/shell-desktop.h
new file mode 100644 (file)
index 0000000..eb6f8c5
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2020 Collabora Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SHELLHANDLER_H
+#define SHELLHANDLER_H
+
+#include <QObject>
+#include <QString>
+#include <QScreen>
+#include <QWindow>
+#include <memory>
+#include "wayland-agl-shell-desktop-client-protocol.h"
+
+/*
+ * Basic type to wrap the agl_shell wayland object into a QObject, so that it
+ * can be used in callbacks from QML.
+ */
+
+class Shell : public QObject
+{
+Q_OBJECT
+       std::shared_ptr<struct agl_shell_desktop> shell;
+
+public:
+       Shell(std::shared_ptr<struct agl_shell_desktop> shell, QObject *parent = nullptr) :
+               QObject(parent), shell(shell) 
+       {}
+public slots:
+       void activate_app(QWindow *win, const QString &app_id);
+};
+
+#endif // SHELLHANDLER_H