| 1 | /* |
| 2 | * Copyright (C) 2016 Canonical, Ltd. |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify it under |
| 5 | * the terms of the GNU Lesser General Public License version 3, as published by |
| 6 | * the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, |
| 10 | * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include "menuregistrar.h" |
| 18 | #include "registry.h" |
| 19 | #include "logging.h" |
| 20 | |
| 21 | #include <QDebug> |
| 22 | #include <QDBusObjectPath> |
| 23 | #include <QGuiApplication> |
| 24 | #include <qpa/qplatformnativeinterface.h> |
| 25 | #include <qpa/qplatformwindow.h> |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | bool isMirClient() { |
| 30 | return qGuiApp->platformName() == "ubuntumirclient"; |
| 31 | } |
| 32 | |
| 33 | } |
| 34 | |
| 35 | LomiriMenuRegistrar::LomiriMenuRegistrar() |
| 36 | : m_connection(nullptr) |
| 37 | , m_registeredProcessId(~0) |
| 38 | { |
| 39 | GError *error = NULL; |
| 40 | m_connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); |
| 41 | if (!m_connection) { |
| 42 | qCWarning(lomiriappmenuRegistrar, "Failed to retrieve session bus - %s", error ? error->message : "unknown error"); |
| 43 | g_error_free (error); |
| 44 | return; |
| 45 | } |
| 46 | m_service = g_dbus_connection_get_unique_name(m_connection); |
| 47 | connect(LomiriMenuRegistry::instance(), &LomiriMenuRegistry::serviceChanged, this, &LomiriMenuRegistrar::onRegistrarServiceChanged); |
| 48 | |
| 49 | if (isMirClient()) { |
| 50 | auto nativeInterface = qGuiApp->platformNativeInterface(); |
| 51 | connect(nativeInterface, &QPlatformNativeInterface::windowPropertyChanged, this, [this](QPlatformWindow* window, const QString &property) { |
| 52 | if (property != QStringLiteral("persistentSurfaceId")) { |
| 53 | return; |
| 54 | } |
| 55 | if (window->window() == m_window) { |
| 56 | registerMenuForWindow(m_window, m_path); |
| 57 | } |
| 58 | }); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | LomiriMenuRegistrar::~LomiriMenuRegistrar() |
| 63 | { |
| 64 | if (m_connection) { |
| 65 | g_object_unref(m_connection); |
| 66 | } |
| 67 | unregisterMenu(); |
| 68 | } |
| 69 | |
| 70 | void LomiriMenuRegistrar::registerMenuForWindow(QWindow* window, const QDBusObjectPath& path) |
| 71 | { |
| 72 | unregisterMenu(); |
| 73 | |
| 74 | m_window = window; |
| 75 | m_path = path; |
| 76 | |
| 77 | registerMenu(); |
| 78 | } |
| 79 | |
| 80 | void LomiriMenuRegistrar::registerMenu() |
| 81 | { |
| 82 | if (LomiriMenuRegistry::instance()->isConnected() && m_window) { |
| 83 | if (isMirClient()) { |
| 84 | registerSurfaceMenu(); |
| 85 | } else { |
| 86 | registerApplicationMenu(); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void LomiriMenuRegistrar::unregisterMenu() |
| 92 | { |
| 93 | if (!m_registeredSurfaceId.isEmpty()) { |
| 94 | unregisterSurfaceMenu(); |
| 95 | } else if (m_registeredProcessId != ~0) { |
| 96 | unregisterApplicationMenu(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void LomiriMenuRegistrar::registerSurfaceMenu() |
| 101 | { |
| 102 | auto nativeInterface = qGuiApp->platformNativeInterface(); |
| 103 | QByteArray persistentSurfaceId = nativeInterface->windowProperty(m_window->handle(), "persistentSurfaceId", QByteArray()).toByteArray(); |
| 104 | if (persistentSurfaceId.isEmpty()) return; |
| 105 | |
| 106 | LomiriMenuRegistry::instance()->registerSurfaceMenu(persistentSurfaceId, m_path, m_service); |
| 107 | m_registeredSurfaceId = persistentSurfaceId; |
| 108 | } |
| 109 | |
| 110 | void LomiriMenuRegistrar::unregisterSurfaceMenu() |
| 111 | { |
| 112 | if (LomiriMenuRegistry::instance()->isConnected()) { |
| 113 | LomiriMenuRegistry::instance()->unregisterSurfaceMenu(m_registeredSurfaceId, m_path); |
| 114 | } |
| 115 | m_registeredSurfaceId.clear(); |
| 116 | } |
| 117 | |
| 118 | void LomiriMenuRegistrar::registerApplicationMenu() |
| 119 | { |
| 120 | pid_t pid = getpid(); |
| 121 | LomiriMenuRegistry::instance()->registerApplicationMenu(pid, m_path, m_service); |
| 122 | m_registeredProcessId = pid; |
| 123 | } |
| 124 | |
| 125 | void LomiriMenuRegistrar::unregisterApplicationMenu() |
| 126 | { |
| 127 | if (LomiriMenuRegistry::instance()->isConnected()) { |
| 128 | LomiriMenuRegistry::instance()->unregisterApplicationMenu(m_registeredProcessId, m_path); |
| 129 | } |
| 130 | m_registeredProcessId = ~0; |
| 131 | } |
| 132 | |
| 133 | void LomiriMenuRegistrar::onRegistrarServiceChanged() |
| 134 | { |
| 135 | unregisterMenu(); |
| 136 | registerMenu(); |
| 137 | } |