2 * Copyright (C) 2016 Mentor Graphics Development (Deutschland) GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "windowmanager.hpp"
18 #include <wayland-client.h>
21 //////////////////////////////////////////
22 // THIS IS STILL UNDER HEAVY DEVELOPMENT!
23 // DO NOT JUDGE THE SOURCE CODE :)
24 //////////////////////////////////////////
26 void* WindowManager::myThis = 0;
28 WindowManager::WindowManager(QObject *parent) :
38 mp_layoutAreaToPidAssignment(0)
40 qDebug("-=[WindowManager]=-");
41 qDebug("WindowManager");
42 // publish windowmanager interface
43 mp_windowManagerAdaptor = new WindowmanagerAdaptor((QObject*)this);
45 QDBusConnection dbus = QDBusConnection::sessionBus();
46 dbus.registerObject("/windowmanager", this);
47 dbus.registerService("org.agl.windowmanager");
50 void WindowManager::start()
52 qDebug("-=[start]=-");
53 mp_layoutAreaToPidAssignment = new QMap<int, unsigned int>;
55 mp_processLayers = new QList<int>;
56 mp_surfaces = new QMap<t_ilm_uint, SurfaceInfo>;
61 qDebug("ilm_init = %d", err);
64 err = ilm_registerNotification(WindowManager::notificationFunc_static, this);
68 WindowManager::~WindowManager()
70 qDebug("-=[~WindowManager]=-");
71 delete mp_windowManagerAdaptor;
74 delete mp_processLayers;
78 delete mp_layoutAreaToPidAssignment;
81 void WindowManager::dumpScene()
84 qDebug("current layout : %d", m_currentLayout);
85 qDebug("available layouts: %d", m_layouts.size());
86 QMap<int, QList<SimpleRect> >::iterator i = m_layouts.begin();
89 while (i != m_layouts.constEnd())
91 qDebug("--[id: %d]--[%s]--", i.key(), m_layoutNames.find(i.key()).value().toStdString().c_str());
92 qDebug(" %d surface areas", i.value().size());
93 for (int j = 0; j < i.value().size(); ++j)
95 qDebug(" -area %d", j);
96 qDebug(" -x : %d", i.value().at(j).x);
97 qDebug(" -y : %d", i.value().at(j).y);
98 qDebug(" -width : %d", i.value().at(j).width);
99 qDebug(" -height: %d", i.value().at(j).height);
108 void WindowManager::createNewLayer(int layerId)
110 qDebug("-=[createNewLayer]=-");
111 qDebug("layerId %d", layerId);
114 t_ilm_uint screenID = 0;
118 err = ilm_getScreenResolution(screenID, &width, &height);
120 t_ilm_layer newLayerId = layerId;
121 err = ilm_layerCreateWithDimension(&newLayerId, width, height);
123 t_ilm_float opacity = 0.0;
124 err = ilm_layerSetOpacity(newLayerId, opacity);
126 ilm_layerSetVisibility(newLayerId, ILM_FALSE);
131 void WindowManager::addSurfaceToLayer(int surfaceId, int layerId)
133 qDebug("-=[addSurfaceToLayer]=-");
134 qDebug("surfaceId %d", surfaceId);
135 qDebug("layerId %d", layerId);
139 ilm_getLayerIDs(&length, &pArray);
140 bool layerFound(false);
141 for (int i = 0; i < length; ++i)
143 if (layerId == pArray[i])
151 createNewLayer(layerId);
154 struct ilmSurfaceProperties surfaceProperties;
155 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
156 //qDebug(" origSourceWidth : %d", surfaceProperties.origSourceWidth);
157 //qDebug(" origSourceHeight: %d", surfaceProperties.origSourceHeight);
159 ilm_layerSetSourceRectangle(layerId,
162 surfaceProperties.origSourceWidth,
163 surfaceProperties.origSourceHeight);
166 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
167 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
168 ilm_surfaceSetOpacity(surfaceId, 1.0);
169 ilm_surfaceSetVisibility(surfaceId, true);
171 ilm_layerAddSurface(layerId, surfaceId);
176 void WindowManager::updateScreen()
178 qDebug("-=[updateScreen]=-");
179 int numberOfLayersToShow = 0;
181 QMap<int, QList<SimpleRect> >::iterator i = m_layouts.find(m_currentLayout);
182 if (m_layouts.end() != i)
184 numberOfLayersToShow += i.value().size();
185 qDebug("the current layout provides %d render areas", i.value().size());
189 qDebug("the current layout provides no render areas!");
193 t_ilm_layer renderOrder[numberOfLayersToShow];
194 int renderOrderCounter = 0;
196 qDebug("show home screen app");
197 if (-1 != m_homeScreenPid)
199 renderOrder[renderOrderCounter] = m_homeScreenPid;
200 ++renderOrderCounter;
202 ilm_layerSetVisibility(m_homeScreenPid, ILM_TRUE);
203 t_ilm_float opacity = 1.0;
204 ilm_layerSetOpacity(m_homeScreenPid, opacity);
206 // homescreen app always fullscreen in the back
207 t_ilm_uint screenID = 0;
211 ilm_getScreenResolution(screenID, &width, &height);
213 ilm_layerSetDestinationRectangle(m_homeScreenPid,
221 qDebug("show %d apps", numberOfLayersToShow);
222 for (int j = 0; j < numberOfLayersToShow; ++j)
224 int layerToShow = mp_layoutAreaToPidAssignment->find(j).value();
225 if (layerToShow != m_homeScreenPid)
227 qDebug(" app no. %d: %d", j, layerToShow);
228 renderOrder[renderOrderCounter] = layerToShow;
229 ++renderOrderCounter;
231 ilm_layerSetVisibility(layerToShow, ILM_TRUE);
232 t_ilm_float opacity = 1.0;
233 ilm_layerSetOpacity(layerToShow, opacity);
235 qDebug(" layout area %d", j);
236 qDebug(" x: %d", m_layouts.find(m_currentLayout).value()[j].x);
237 qDebug(" x: %d", m_layouts.find(m_currentLayout).value()[j].y);
238 qDebug(" w: %d", m_layouts.find(m_currentLayout).value()[j].width);
239 qDebug(" h: %d", m_layouts.find(m_currentLayout).value()[j].height);
241 ilm_layerSetDestinationRectangle(layerToShow,
242 m_layouts.find(m_currentLayout).value()[j].x,
243 m_layouts.find(m_currentLayout).value()[j].y,
244 m_layouts.find(m_currentLayout).value()[j].width,
245 m_layouts.find(m_currentLayout).value()[j].height);
249 qDebug("renderOrder");
250 for (int j = 0; j < renderOrderCounter; ++j)
252 qDebug(" %d: %d", j, renderOrder[j]);
255 ilm_displaySetRenderOrder(0, renderOrder, renderOrderCounter);
260 void WindowManager::notificationFunc_non_static(ilmObjectType object,
264 qDebug("-=[notificationFunc_non_static]=-");
265 qDebug("Notification from weston!");
266 if (ILM_SURFACE == object)
268 struct ilmSurfaceProperties surfaceProperties;
272 qDebug("Surface created, ID: %d", id);
273 //qDebug("Surface created, ID: 0x%s", QString(QByteArray::number(id,16)).toStdString().c_str());
274 ilm_getPropertiesOfSurface(id, &surfaceProperties);
275 qDebug(" origSourceWidth : %d", surfaceProperties.origSourceWidth);
276 qDebug(" origSourceHeight: %d", surfaceProperties.origSourceHeight);
277 SurfaceInfo surfaceInfo;
278 surfaceInfo.pid = surfaceProperties.creatorPid;
279 QString procInfoFileName = QString("/proc/") + QString::number(surfaceInfo.pid) + QString("/comm");
280 QFile procInfo(procInfoFileName);
281 if (procInfo.open(QIODevice::ReadOnly))
283 QTextStream in(&procInfo);
284 surfaceInfo.processName = in.readLine();
285 qDebug(" creator pid %d %s", surfaceInfo.pid, surfaceInfo.processName.toStdString().c_str());
286 addSurfaceToLayer(id, surfaceProperties.creatorPid);
288 mp_surfaces->insert(id, surfaceInfo);
289 ilm_surfaceAddNotification(id, surfaceCallbackFunction_static);
293 if (0 == QString::compare("weston-fullscre", surfaceInfo.processName))
295 qDebug("HomeScreen app detected");
296 m_homeScreenPid = surfaceInfo.pid;
302 qDebug("no creator pid found. Ignoring surface!");
307 qDebug("Surface destroyed, ID: %d", id);
308 mp_surfaces->erase(mp_surfaces->find(id));
309 ilm_surfaceRemoveNotification(id);
314 if (ILM_LAYER == object)
316 //qDebug("Layer.. we don't care...");
320 void WindowManager::notificationFunc_static(ilmObjectType object,
325 static_cast<WindowManager*>(WindowManager::myThis)->notificationFunc_non_static(object, id, created);
331 void WindowManager::surfaceCallbackFunction_non_static(t_ilm_surface surface,
332 struct ilmSurfaceProperties* surfaceProperties,
333 t_ilm_notification_mask mask)
335 qDebug("-=[surfaceCallbackFunction_non_static]=-");
336 qDebug("surfaceCallbackFunction_non_static changes for surface %d", surface);
337 if (ILM_NOTIFICATION_VISIBILITY & mask)
339 qDebug("ILM_NOTIFICATION_VISIBILITY");
342 if (ILM_NOTIFICATION_OPACITY & mask)
344 qDebug("ILM_NOTIFICATION_OPACITY");
347 if (ILM_NOTIFICATION_ORIENTATION & mask)
349 qDebug("ILM_NOTIFICATION_ORIENTATION");
352 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
354 qDebug("ILM_NOTIFICATION_SOURCE_RECT");
357 if (ILM_NOTIFICATION_DEST_RECT & mask)
359 qDebug("ILM_NOTIFICATION_DEST_RECT");
363 void WindowManager::surfaceCallbackFunction_static(t_ilm_surface surface,
364 struct ilmSurfaceProperties* surfaceProperties,
365 t_ilm_notification_mask mask)
368 static_cast<WindowManager*>(WindowManager::myThis)->surfaceCallbackFunction_non_static(surface, surfaceProperties, mask);
372 int WindowManager::addLayout(int layoutId, const QString &layoutName, const QList<SimpleRect> &surfaceAreas)
374 qDebug("-=[addLayout]=-");
375 m_layouts.insert(layoutId, surfaceAreas);
376 m_layoutNames.insert(layoutId, layoutName);
377 qDebug("addLayout %d %s, size %d", layoutId, layoutName.toStdString().c_str(), surfaceAreas.size());
384 QList<int> WindowManager::getAvailableLayouts(int numberOfAppSurfaces)
386 qDebug("-=[getAvailableLayouts]=-");
387 QMap<int, QList<SimpleRect> >::iterator i = m_layouts.begin();
390 while (i != m_layouts.constEnd())
392 if (i.value().size() == numberOfAppSurfaces)
394 result.append(i.key());
403 // maybe not needed anymore
404 QList<SimplePoint> WindowManager::getAvailableSurfaces()
406 qDebug("-=[getAvailableSurfaces]=-");
407 QList<SimplePoint> points;
411 points.append(point);
414 points.append(point);
417 points.append(point);
422 int WindowManager::getLayout()
424 qDebug("-=[getLayout]=-");
425 return m_currentLayout;
428 QString WindowManager::getLayoutName(int layoutId)
430 qDebug("-=[getLayoutName]=-");
431 return m_layoutNames.find(layoutId).value();
434 void WindowManager::setLayoutById(int layoutId)
436 qDebug("-=[setLayoutById]=-");
437 m_currentLayout = layoutId;
439 mp_layoutAreaToPidAssignment->clear();
444 void WindowManager::setLayoutByName(const QString &layoutName)
446 qDebug("-=[setLayoutByName]=-");
447 QMap<int, QString>::iterator i = m_layoutNames.begin();
448 while (i != m_layoutNames.constEnd())
450 if (i.value() == layoutName)
452 m_currentLayout = i.key();
457 mp_layoutAreaToPidAssignment->clear();
462 void WindowManager::setPidToLayoutArea(int pid, int layoutAreaId)
464 qDebug("-=[setPidToLayoutArea]=-");
465 qDebug("pid %d", pid);
466 qDebug("layoutAreaId %d", layoutAreaId);
467 mp_layoutAreaToPidAssignment->insert(layoutAreaId, pid);