1// Copyright (C) 2017 ITAGE Corporation, author: <yusuke.binsaki@itage.co.jp>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qwaylandivishellintegration.h"
5
6#include <QtCore/qsize.h>
7#include <QtCore/qdebug.h>
8
9#include <QtWaylandClient/private/qwaylanddisplay_p.h>
10#include <QtWaylandClient/private/qwaylandwindow_p.h>
11#include <QtWaylandClient/private/qwaylandabstractdecoration_p.h>
12
13#include "qwaylandivisurface_p.h"
14
15#include <mutex>
16
17#include <unistd.h>
18
19QT_BEGIN_NAMESPACE
20
21namespace QtWaylandClient {
22
23class QWaylandIviController : public QWaylandClientExtensionTemplate<QWaylandIviController>,
24 public QtWayland::ivi_controller
25{
26public:
27 QWaylandIviController() : QWaylandClientExtensionTemplate(1) { }
28 void initialize() { QWaylandClientExtensionTemplate::initialize(); }
29};
30
31QWaylandIviShellIntegration::QWaylandIviShellIntegration()
32 : QWaylandShellIntegrationTemplate(1), m_iviController(new QWaylandIviController)
33{
34}
35
36bool QWaylandIviShellIntegration::initialize(QWaylandDisplay *display)
37{
38 QWaylandShellIntegrationTemplate::initialize(display);
39 m_iviController->initialize();
40 return isActive();
41}
42
43/* get unique id
44 * pattern1:
45 * When set QT_IVI_SURFACE_ID, We use it as ID.
46 * Next ID is increment.
47 * pattern2:
48 * When not set QT_IVI_SURFACE_ID, We use process ID and unused bit.
49 * process ID maximum is 2^22. Unused bit is 23 to 32 bit.
50 * Therefor, We use 23 to 32 bit. This do not overlap with other clients.
51 * Next ID is increment of 23 to 32 bit.
52 * +------------+---------------------------+
53 * |31 23|22 0|
54 * +------------+---------------------------+
55 * |0000 0000 00|00 0000 0000 0000 0000 0000|
56 * |<- ID ->|<- process ID ->|
57 * +------------+---------------------------+
58 */
59uint32_t QWaylandIviShellIntegration::getNextUniqueSurfaceId()
60{
61 const uint32_t PID_MAX_EXPONENTIATION = 22; // 22 bit shift operation
62 const uint32_t ID_LIMIT = 1 << (32 - PID_MAX_EXPONENTIATION); // 10 bit is unique id
63 const std::lock_guard<QRecursiveMutex> locker(m_mutex);
64
65 if (m_lastSurfaceId == 0) {
66 QByteArray env = qgetenv(varName: "QT_IVI_SURFACE_ID");
67 bool ok;
68 m_lastSurfaceId = env.toUInt(ok: &ok, base: 10);
69 if (ok)
70 m_useEnvSurfaceId = true;
71 else
72 m_lastSurfaceId = getpid();
73
74 return m_lastSurfaceId;
75 }
76
77 if (m_useEnvSurfaceId) {
78 m_lastSurfaceId++;
79 } else {
80 m_surfaceNumber++;
81 if (m_surfaceNumber >= ID_LIMIT) {
82 qWarning(msg: "IVI surface id counter overflow\n");
83 return 0;
84 }
85 m_lastSurfaceId += (m_surfaceNumber << PID_MAX_EXPONENTIATION);
86 }
87
88 return m_lastSurfaceId;
89}
90
91QWaylandShellSurface *QWaylandIviShellIntegration::createShellSurface(QWaylandWindow *window)
92{
93 if (!isActive())
94 return nullptr;
95
96 uint32_t surfaceId = getNextUniqueSurfaceId();
97 if (surfaceId == 0)
98 return nullptr;
99
100 struct ivi_surface *surface = surface_create(surfaceId, window->wlSurface());
101 if (!m_iviController->isActive())
102 return new QWaylandIviSurface(surface, window);
103
104 struct ::ivi_controller_surface *controller = m_iviController->ivi_controller::surface_create(surfaceId);
105 QWaylandIviSurface *iviSurface = new QWaylandIviSurface(surface, window, controller);
106
107 if (window->window()->type() == Qt::Popup) {
108 QPoint transientPos = window->geometry().topLeft(); // this is absolute
109 QWaylandWindow *parent = window->transientParent();
110 if (parent && parent->decoration()) {
111 transientPos -= parent->geometry().topLeft();
112 transientPos.setX(transientPos.x() + parent->decoration()->margins().left());
113 transientPos.setY(transientPos.y() + parent->decoration()->margins().top());
114 }
115 QSize size = window->windowGeometry().size();
116 iviSurface->ivi_controller_surface::set_destination_rectangle(transientPos.x(),
117 transientPos.y(),
118 size.width(),
119 size.height());
120 }
121
122 return iviSurface;
123}
124
125}
126
127QT_END_NAMESPACE
128

source code of qtwayland/src/plugins/shellintegration/ivi-shell/qwaylandivishellintegration.cpp