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) :
33 m_layoutFullScreenAssociated(),
40 mp_layoutAreaToPidAssignment(0)
42 qDebug("-=[WindowManager]=-");
43 qDebug("WindowManager");
44 // publish windowmanager interface
45 mp_windowManagerAdaptor = new WindowmanagerAdaptor((QObject*)this);
47 QDBusConnection dbus = QDBusConnection::sessionBus();
48 dbus.registerObject("/windowmanager", this);
49 dbus.registerService("org.agl.windowmanager");
52 void WindowManager::start()
54 qDebug("-=[start]=-");
55 mp_layoutAreaToPidAssignment = new QMap<int, unsigned int>;
57 mp_processLayers = new QList<int>;
58 mp_surfaces = new QMap<t_ilm_uint, SurfaceInfo>;
63 qDebug("ilm_init = %d", err);
66 err = ilm_registerNotification(WindowManager::notificationFunc_static, this);
70 WindowManager::~WindowManager()
72 qDebug("-=[~WindowManager]=-");
73 delete mp_windowManagerAdaptor;
76 delete mp_processLayers;
80 delete mp_layoutAreaToPidAssignment;
83 void WindowManager::dumpScene()
86 qDebug("current layout : %d", m_currentLayout);
87 qDebug("available layouts: %d", m_layouts.size());
88 QMap<int, QList<SimpleRect> >::iterator i = m_layouts.begin();
91 while (i != m_layouts.constEnd())
93 qDebug("--[id: %d]--[%s]--", i.key(), m_layoutNames.find(i.key()).value().toStdString().c_str());
94 qDebug(" %d surface areas", i.value().size());
95 for (int j = 0; j < i.value().size(); ++j)
97 qDebug(" -area %d", j);
98 qDebug(" -x : %d", i.value().at(j).x);
99 qDebug(" -y : %d", i.value().at(j).y);
100 qDebug(" -width : %d", i.value().at(j).width);
101 qDebug(" -height: %d", i.value().at(j).height);
110 void WindowManager::createNewLayer(int layerId)
112 qDebug("-=[createNewLayer]=-");
113 qDebug("layerId %d", layerId);
116 t_ilm_uint screenID = 0;
120 err = ilm_getScreenResolution(screenID, &width, &height);
122 t_ilm_layer newLayerId = layerId;
123 err = ilm_layerCreateWithDimension(&newLayerId, width, height);
125 t_ilm_float opacity = 0.0;
126 err = ilm_layerSetOpacity(newLayerId, opacity);
128 ilm_layerSetVisibility(newLayerId, ILM_FALSE);
133 void WindowManager::addSurfaceToLayer(int surfaceId, int layerId)
135 qDebug("-=[addSurfaceToLayer]=-");
136 qDebug("surfaceId %d", surfaceId);
137 qDebug("layerId %d", layerId);
141 ilm_getLayerIDs(&length, &pArray);
142 bool layerFound(false);
143 for (int i = 0; i < length; ++i)
145 if (layerId == pArray[i])
153 createNewLayer(layerId);
156 struct ilmSurfaceProperties surfaceProperties;
157 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
158 //qDebug(" origSourceWidth : %d", surfaceProperties.origSourceWidth);
159 //qDebug(" origSourceHeight: %d", surfaceProperties.origSourceHeight);
161 ilm_layerSetSourceRectangle(layerId,
164 surfaceProperties.origSourceWidth,
165 surfaceProperties.origSourceHeight);
168 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
169 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
170 ilm_surfaceSetOpacity(surfaceId, 1.0);
171 ilm_surfaceSetVisibility(surfaceId, true);
173 ilm_layerAddSurface(layerId, surfaceId);
178 void WindowManager::updateScreen()
180 qDebug("-=[updateScreen]=-");
181 int numberOfLayersToShow = 0;
183 QMap<int, QList<SimpleRect> >::iterator i = m_layouts.find(m_currentLayout);
184 if (m_layouts.end() != i)
186 numberOfLayersToShow += i.value().size();
187 qDebug("the current layout provides %d render areas", i.value().size());
191 qDebug("the current layout provides no render areas!");
195 t_ilm_layer renderOrder[numberOfLayersToShow];
196 int renderOrderCounter = 0;
198 qDebug("show home screen app");
199 if (-1 != m_homeScreenPid)
201 renderOrder[renderOrderCounter] = m_homeScreenPid;
202 ++renderOrderCounter;
204 ilm_layerSetVisibility(m_homeScreenPid, ILM_TRUE);
205 t_ilm_float opacity = 1.0;
206 ilm_layerSetOpacity(m_homeScreenPid, opacity);
208 // homescreen app always fullscreen in the back
209 t_ilm_uint screenID = 0;
213 ilm_getScreenResolution(screenID, &width, &height);
215 ilm_layerSetDestinationRectangle(m_homeScreenPid,
223 qDebug("show %d apps", numberOfLayersToShow);
224 for (int j = 0; j < numberOfLayersToShow; ++j)
226 int layerToShow = mp_layoutAreaToPidAssignment->find(j).value();
227 if (layerToShow != m_homeScreenPid)
229 qDebug(" app no. %d: %d", j, layerToShow);
230 renderOrder[renderOrderCounter] = layerToShow;
231 ++renderOrderCounter;
233 ilm_layerSetVisibility(layerToShow, ILM_TRUE);
234 t_ilm_float opacity = 1.0;
235 ilm_layerSetOpacity(layerToShow, opacity);
237 qDebug(" layout area %d", j);
238 qDebug(" x: %d", m_layouts.find(m_currentLayout).value()[j].x);
239 qDebug(" x: %d", m_layouts.find(m_currentLayout).value()[j].y);
240 qDebug(" w: %d", m_layouts.find(m_currentLayout).value()[j].width);
241 qDebug(" h: %d", m_layouts.find(m_currentLayout).value()[j].height);
243 ilm_layerSetDestinationRectangle(layerToShow,
244 m_layouts.find(m_currentLayout).value()[j].x,
245 m_layouts.find(m_currentLayout).value()[j].y,
246 m_layouts.find(m_currentLayout).value()[j].width,
247 m_layouts.find(m_currentLayout).value()[j].height);
251 qDebug("renderOrder");
252 for (int j = 0; j < renderOrderCounter; ++j)
254 qDebug(" %d: %d", j, renderOrder[j]);
257 ilm_displaySetRenderOrder(0, renderOrder, renderOrderCounter);
262 void WindowManager::notificationFunc_non_static(ilmObjectType object,
266 qDebug("-=[notificationFunc_non_static]=-");
267 qDebug("Notification from weston!");
268 if (ILM_SURFACE == object)
270 struct ilmSurfaceProperties surfaceProperties;
274 qDebug("Surface created, ID: %d", id);
275 //qDebug("Surface created, ID: 0x%s", QString(QByteArray::number(id,16)).toStdString().c_str());
276 ilm_getPropertiesOfSurface(id, &surfaceProperties);
277 qDebug(" origSourceWidth : %d", surfaceProperties.origSourceWidth);
278 qDebug(" origSourceHeight: %d", surfaceProperties.origSourceHeight);
279 SurfaceInfo surfaceInfo;
280 surfaceInfo.pid = surfaceProperties.creatorPid;
281 QString procInfoFileName = QString("/proc/") + QString::number(surfaceInfo.pid) + QString("/comm");
282 QFile procInfo(procInfoFileName);
283 if (procInfo.open(QIODevice::ReadOnly))
285 QTextStream in(&procInfo);
286 surfaceInfo.processName = in.readLine();
287 qDebug(" creator pid %d %s", surfaceInfo.pid, surfaceInfo.processName.toStdString().c_str());
288 addSurfaceToLayer(id, surfaceProperties.creatorPid);
290 mp_surfaces->insert(id, surfaceInfo);
291 ilm_surfaceAddNotification(id, surfaceCallbackFunction_static);
295 if (0 == QString::compare("weston-fullscre", surfaceInfo.processName))
297 qDebug("HomeScreen app detected");
298 m_homeScreenPid = surfaceInfo.pid;
304 qDebug("no creator pid found. Ignoring surface!");
309 qDebug("Surface destroyed, ID: %d", id);
310 mp_surfaces->erase(mp_surfaces->find(id));
311 ilm_surfaceRemoveNotification(id);
316 if (ILM_LAYER == object)
318 //qDebug("Layer.. we don't care...");
322 void WindowManager::notificationFunc_static(ilmObjectType object,
327 static_cast<WindowManager*>(WindowManager::myThis)->notificationFunc_non_static(object, id, created);
333 void WindowManager::surfaceCallbackFunction_non_static(t_ilm_surface surface,
334 struct ilmSurfaceProperties* surfaceProperties,
335 t_ilm_notification_mask mask)
337 qDebug("-=[surfaceCallbackFunction_non_static]=-");
338 qDebug("surfaceCallbackFunction_non_static changes for surface %d", surface);
339 if (ILM_NOTIFICATION_VISIBILITY & mask)
341 qDebug("ILM_NOTIFICATION_VISIBILITY");
344 if (ILM_NOTIFICATION_OPACITY & mask)
346 qDebug("ILM_NOTIFICATION_OPACITY");
349 if (ILM_NOTIFICATION_ORIENTATION & mask)
351 qDebug("ILM_NOTIFICATION_ORIENTATION");
354 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
356 qDebug("ILM_NOTIFICATION_SOURCE_RECT");
359 if (ILM_NOTIFICATION_DEST_RECT & mask)
361 qDebug("ILM_NOTIFICATION_DEST_RECT");
365 void WindowManager::surfaceCallbackFunction_static(t_ilm_surface surface,
366 struct ilmSurfaceProperties* surfaceProperties,
367 t_ilm_notification_mask mask)
370 static_cast<WindowManager*>(WindowManager::myThis)->surfaceCallbackFunction_non_static(surface, surfaceProperties, mask);
374 int WindowManager::addLayout(int layoutId, const QString &layoutName, bool isFullScreen, int associatedFullScreenLayout, const QList<SimpleRect> &surfaceAreas)
376 qDebug("-=[addLayout]=-");
377 m_layouts.insert(layoutId, surfaceAreas);
378 m_layoutNames.insert(layoutId, layoutName);
379 m_layoutFullScreen.insert(layoutId, isFullScreen);
380 m_layoutFullScreenAssociated.insert(layoutId, associatedFullScreenLayout);
381 qDebug("addLayout %d %s %s, %d, size %d",
383 layoutName.toStdString().c_str(),
384 isFullScreen ? "true" : "false",
385 associatedFullScreenLayout,
386 surfaceAreas.size());
393 int WindowManager::getAssociatedFullScreenLayout(int layoutId)
395 qDebug("-=[getAssociatedFullScreenLayout]=-");
396 return m_layoutFullScreenAssociated.find(layoutId).value();
399 QList<int> WindowManager::getAvailableLayouts(int numberOfAppSurfaces)
401 qDebug("-=[getAvailableLayouts]=-");
402 QMap<int, QList<SimpleRect> >::iterator i = m_layouts.begin();
405 while (i != m_layouts.constEnd())
407 if (i.value().size() == numberOfAppSurfaces)
409 result.append(i.key());
418 // maybe not needed anymore
419 QList<SimplePoint> WindowManager::getAvailableSurfaces()
421 qDebug("-=[getAvailableSurfaces]=-");
422 QList<SimplePoint> points;
426 points.append(point);
429 points.append(point);
432 points.append(point);
437 int WindowManager::getLayout()
439 qDebug("-=[getLayout]=-");
440 return m_currentLayout;
443 QString WindowManager::getLayoutName(int layoutId)
445 qDebug("-=[getLayoutName]=-");
446 return m_layoutNames.find(layoutId).value();
449 bool WindowManager::isLayoutFullScreen(int layoutId)
451 qDebug("-=[isLayoutFullScreen]=-");
452 return m_layoutFullScreen.find(layoutId).value();
455 void WindowManager::setLayoutById(int layoutId)
457 qDebug("-=[setLayoutById]=-");
458 m_currentLayout = layoutId;
460 mp_layoutAreaToPidAssignment->clear();
465 void WindowManager::setLayoutByName(const QString &layoutName)
467 qDebug("-=[setLayoutByName]=-");
468 QMap<int, QString>::iterator i = m_layoutNames.begin();
469 while (i != m_layoutNames.constEnd())
471 if (i.value() == layoutName)
473 m_currentLayout = i.key();
478 mp_layoutAreaToPidAssignment->clear();
483 void WindowManager::setPidToLayoutArea(int pid, int layoutAreaId)
485 qDebug("-=[setPidToLayoutArea]=-");
486 qDebug("pid %d", pid);
487 qDebug("layoutAreaId %d", layoutAreaId);
488 mp_layoutAreaToPidAssignment->insert(layoutAreaId, pid);