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 // three layers will be defined. The HomeScreen will be placed
27 // full screen in the background.
28 // On top all applications in one layer.
29 // On top of that, the popup layer.
30 #define WINDOWMANAGER_LAYER_POPUP 100
31 #define WINDOWMANAGER_LAYER_APPLICATIONS 101
32 #define WINDOWMANAGER_LAYER_HOMESCREEN 102
34 #define WINDOWMANAGER_LAYER_NUM 3
36 void* WindowManager::myThis = 0;
38 WindowManager::WindowManager(QObject *parent) :
42 mp_layoutAreaToSurfaceIdAssignment(0),
46 qDebug("-=[WindowManager]=-");
47 // publish windowmanager interface
48 mp_windowManagerAdaptor = new WindowmanagerAdaptor((QObject*)this);
50 QDBusConnection dbus = QDBusConnection::sessionBus();
51 dbus.registerObject("/windowmanager", this);
52 dbus.registerService("org.agl.windowmanager");
55 void WindowManager::start()
57 qDebug("-=[start]=-");
58 mp_layoutAreaToSurfaceIdAssignment = new QMap<int, unsigned int>;
63 qDebug("ilm_init = %d", err);
66 err = ilm_registerNotification(WindowManager::notificationFunc_static, this);
68 createNewLayer(WINDOWMANAGER_LAYER_POPUP);
69 createNewLayer(WINDOWMANAGER_LAYER_APPLICATIONS);
70 createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN);
74 WindowManager::~WindowManager()
76 qDebug("-=[~WindowManager]=-");
77 delete mp_windowManagerAdaptor;
81 delete mp_layoutAreaToSurfaceIdAssignment;
84 void WindowManager::dumpScene()
87 qDebug("current layout : %d", m_currentLayout);
88 qDebug("available layouts: %d", m_layouts.size());
89 QList<Layout>::const_iterator i = m_layouts.begin();
91 while (i != m_layouts.constEnd())
93 qDebug("--[id: %d]--[%s]--", i->id, i->name.toStdString().c_str());
94 qDebug(" %d surface areas", i->layoutAreas.size());
95 for (int j = 0; j < i->layoutAreas.size(); ++j)
97 qDebug(" -area %d", j);
98 qDebug(" -x : %d", i->layoutAreas.at(j).x);
99 qDebug(" -y : %d", i->layoutAreas.at(j).y);
100 qDebug(" -width : %d", i->layoutAreas.at(j).width);
101 qDebug(" -height: %d", i->layoutAreas.at(j).height);
110 void WindowManager::createNewLayer(int layerId)
112 qDebug("-=[createNewLayer]=-");
113 qDebug("layerId %d", layerId);
115 t_ilm_uint screenID = 0;
119 ilm_getScreenResolution(screenID, &width, &height);
121 t_ilm_layer newLayerId = layerId;
122 ilm_layerCreateWithDimension(&newLayerId, width, height);
123 ilm_layerSetOpacity(newLayerId, 1.0);
124 ilm_layerSetVisibility(newLayerId, ILM_TRUE);
125 ilm_layerSetSourceRectangle(newLayerId,
130 ilm_layerSetDestinationRectangle(newLayerId,
139 void WindowManager::addSurfaceToLayer(int surfaceId, int layerId)
141 qDebug("-=[addSurfaceToLayer]=-");
142 qDebug("surfaceId %d", surfaceId);
143 qDebug("layerId %d", layerId);
145 if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN)
147 struct ilmSurfaceProperties surfaceProperties;
148 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
150 qDebug("sourceX %d", surfaceProperties.sourceX);
151 qDebug("sourceY %d", surfaceProperties.sourceY);
152 qDebug("sourceWidth %d", surfaceProperties.sourceWidth);
153 qDebug("sourceHeight %d", surfaceProperties.sourceHeight);
155 // homescreen app always fullscreen in the back
156 t_ilm_uint screenID = 0;
160 ilm_getScreenResolution(screenID, &width, &height);
162 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, width, height);
163 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
164 ilm_surfaceSetOpacity(surfaceId, 1.0);
165 ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);
167 ilm_layerAddSurface(layerId, surfaceId);
170 if (layerId == WINDOWMANAGER_LAYER_APPLICATIONS)
172 struct ilmSurfaceProperties surfaceProperties;
173 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
175 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
176 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
177 ilm_surfaceSetOpacity(surfaceId, 0.0);
178 ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
180 ilm_layerAddSurface(layerId, surfaceId);
183 if (layerId == WINDOWMANAGER_LAYER_POPUP)
185 struct ilmSurfaceProperties surfaceProperties;
186 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
188 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
189 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
190 ilm_surfaceSetOpacity(surfaceId, 0.0);
191 ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
193 ilm_layerAddSurface(layerId, surfaceId);
201 void WindowManager::updateScreen()
203 qDebug("-=[updateScreen]=-");
206 if (-1 != m_currentLayout)
210 for (int i = 0; i < m_surfaces.size(); ++i)
212 ilm_surfaceSetVisibility(m_surfaces.at(i), ILM_FALSE);
213 ilm_surfaceSetOpacity(m_surfaces.at(i), 0.0);
216 // find the current used layout
217 QList<Layout>::const_iterator ci = m_layouts.begin();
219 Layout currentLayout;
220 while (ci != m_layouts.constEnd())
222 if (ci->id == m_currentLayout)
230 qDebug("show %d apps", mp_layoutAreaToSurfaceIdAssignment->size());
231 for (int j = 0; j < mp_layoutAreaToSurfaceIdAssignment->size(); ++j)
233 int surfaceToShow = mp_layoutAreaToSurfaceIdAssignment->find(j).value();
234 qDebug(" surface no. %d: %d", j, surfaceToShow);
236 ilm_surfaceSetVisibility(surfaceToShow, ILM_TRUE);
237 ilm_surfaceSetOpacity(surfaceToShow, 1.0);
239 qDebug(" layout area %d", j);
240 qDebug(" x: %d", currentLayout.layoutAreas[j].x);
241 qDebug(" y: %d", currentLayout.layoutAreas[j].y);
242 qDebug(" w: %d", currentLayout.layoutAreas[j].width);
243 qDebug(" h: %d", currentLayout.layoutAreas[j].height);
245 ilm_surfaceSetDestinationRectangle(surfaceToShow,
246 currentLayout.layoutAreas[j].x,
247 currentLayout.layoutAreas[j].y,
248 currentLayout.layoutAreas[j].width,
249 currentLayout.layoutAreas[j].height);
255 t_ilm_layer renderOrder[WINDOWMANAGER_LAYER_NUM];
256 renderOrder[0] = WINDOWMANAGER_LAYER_HOMESCREEN;
257 renderOrder[1] = WINDOWMANAGER_LAYER_APPLICATIONS;
258 renderOrder[2] = WINDOWMANAGER_LAYER_POPUP;
260 ilm_displaySetRenderOrder(0, renderOrder, WINDOWMANAGER_LAYER_NUM);
268 void WindowManager::notificationFunc_non_static(ilmObjectType object,
272 qDebug("-=[notificationFunc_non_static]=-");
273 qDebug("Notification from weston!");
274 if (ILM_SURFACE == object)
276 struct ilmSurfaceProperties surfaceProperties;
280 qDebug("Surface created, ID: %d", id);
281 ilm_getPropertiesOfSurface(id, &surfaceProperties);
282 qDebug(" origSourceWidth : %d", surfaceProperties.origSourceWidth);
283 qDebug(" origSourceHeight: %d", surfaceProperties.origSourceHeight);
285 if (m_homeScreenPid == surfaceProperties.creatorPid)
287 if (m_homeScreenSurfaceId != id)
289 qDebug("HomeScreen app detected");
290 m_homeScreenSurfaceId = id;
291 addSurfaceToLayer(id, WINDOWMANAGER_LAYER_HOMESCREEN);
297 addSurfaceToLayer(id, WINDOWMANAGER_LAYER_APPLICATIONS);
299 m_surfaces.append(id);
301 ilm_surfaceAddNotification(id, surfaceCallbackFunction_static);
307 qDebug("Surface destroyed, ID: %d", id);
308 m_surfaces.removeAt(m_surfaces.indexOf(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);
328 void WindowManager::surfaceCallbackFunction_non_static(t_ilm_surface surface,
329 struct ilmSurfaceProperties* surfaceProperties,
330 t_ilm_notification_mask mask)
332 qDebug("-=[surfaceCallbackFunction_non_static]=-");
333 qDebug("surfaceCallbackFunction_non_static changes for surface %d", surface);
334 if (ILM_NOTIFICATION_VISIBILITY & mask)
336 qDebug("ILM_NOTIFICATION_VISIBILITY");
339 if (ILM_NOTIFICATION_OPACITY & mask)
341 qDebug("ILM_NOTIFICATION_OPACITY");
344 if (ILM_NOTIFICATION_ORIENTATION & mask)
346 qDebug("ILM_NOTIFICATION_ORIENTATION");
349 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
351 qDebug("ILM_NOTIFICATION_SOURCE_RECT");
354 if (ILM_NOTIFICATION_DEST_RECT & mask)
356 qDebug("ILM_NOTIFICATION_DEST_RECT");
360 void WindowManager::surfaceCallbackFunction_static(t_ilm_surface surface,
361 struct ilmSurfaceProperties* surfaceProperties,
362 t_ilm_notification_mask mask)
365 static_cast<WindowManager*>(WindowManager::myThis)->surfaceCallbackFunction_non_static(surface, surfaceProperties, mask);
370 int WindowManager::homeScreenPid() const
372 return m_homeScreenPid;
375 void WindowManager::setHomeScreenPid(int value)
377 m_homeScreenPid = value;
379 // maybe the HomeSceen app has already provided its surface.
380 // if so, shift it to the correct layer
381 // find the current used layout
382 QList<int>::iterator ci = m_surfaces.begin();
384 struct ilmSurfaceProperties surfaceProperties;
386 while ((!found) && (ci != m_surfaces.constEnd()))
388 ilm_getPropertiesOfSurface(*ci, &surfaceProperties);
389 if (m_homeScreenPid == surfaceProperties.creatorPid)
391 qDebug("HomeScreen app detected");
392 m_homeScreenSurfaceId = *ci;
393 addSurfaceToLayer(*ci, WINDOWMANAGER_LAYER_HOMESCREEN);
394 m_surfaces.erase(ci);
407 int WindowManager::layoutId() const
409 return m_currentLayout;
412 QString WindowManager::layoutName() const
414 QList<Layout>::const_iterator i = m_layouts.begin();
416 QString result = "not found";
417 while (i != m_layouts.constEnd())
419 if (i->id == m_currentLayout)
431 int WindowManager::addLayout(int layoutId, const QString &layoutName, const QList<LayoutArea> &surfaceAreas)
433 qDebug("-=[addLayout]=-");
434 m_layouts.append(Layout(layoutId, layoutName, surfaceAreas));
436 qDebug("addLayout %d %s, size %d",
438 layoutName.toStdString().c_str(),
439 surfaceAreas.size());
443 return WINDOWMANAGER_NO_ERROR;
446 QList<Layout> WindowManager::getAllLayouts()
448 qDebug("-=[getAllLayouts]=-");
453 QList<int> WindowManager::getAvailableLayouts(int numberOfAppSurfaces)
455 qDebug("-=[getAvailableLayouts]=-");
456 QList<Layout>::const_iterator i = m_layouts.begin();
459 while (i != m_layouts.constEnd())
461 if (i->layoutAreas.size() == numberOfAppSurfaces)
463 result.append(i->id);
472 QList<int> WindowManager::getAvailableSurfaces()
474 qDebug("-=[getAvailableSurfaces]=-");
479 QString WindowManager::getLayoutName(int layoutId)
481 qDebug("-=[getLayoutName]=-");
482 QList<Layout>::const_iterator i = m_layouts.begin();
484 QString result = "not found";
485 while (i != m_layouts.constEnd())
487 if (i->id == layoutId)
499 int WindowManager::setLayoutById(int layoutId)
501 qDebug("-=[setLayoutById]=-");
502 int result = WINDOWMANAGER_NO_ERROR;
503 m_currentLayout = layoutId;
505 mp_layoutAreaToSurfaceIdAssignment->clear();
512 int WindowManager::setLayoutByName(const QString &layoutName)
514 qDebug("-=[setLayoutByName]=-");
515 int result = WINDOWMANAGER_NO_ERROR;
517 QList<Layout>::const_iterator i = m_layouts.begin();
519 while (i != m_layouts.constEnd())
521 if (i->name == layoutName)
523 m_currentLayout = i->id;
525 mp_layoutAreaToSurfaceIdAssignment->clear();
536 int WindowManager::setSurfaceToLayoutArea(int surfaceId, int layoutAreaId)
538 qDebug("-=[setSurfaceToLayoutArea]=-");
539 int result = WINDOWMANAGER_NO_ERROR;
541 qDebug("surfaceId %d", surfaceId);
542 qDebug("layoutAreaId %d", layoutAreaId);
543 mp_layoutAreaToSurfaceIdAssignment->insert(layoutAreaId, surfaceId);