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"
20 //////////////////////////////////////////
21 // THIS IS STILL UNDER HEAVY DEVELOPMENT!
22 // DO NOT JUDGE THE SOURCE CODE :)
23 //////////////////////////////////////////
25 // three layers will be defined. The HomeScreen will be placed
26 // full screen in the background.
27 // On top all applications in one layer.
28 // On top of that, the popup layer.
29 #define WINDOWMANAGER_LAYER_POPUP 100
30 #define WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY 101
31 #define WINDOWMANAGER_LAYER_APPLICATIONS 102
32 #define WINDOWMANAGER_LAYER_HOMESCREEN 103
34 #define WINDOWMANAGER_LAYER_NUM 4
36 // the HomeScreen app has to have the surface id 1000
37 #define WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID 1000
40 void* WindowManager::myThis = 0;
42 WindowManager::WindowManager(QObject *parent) :
46 mp_layoutAreaToSurfaceIdAssignment(0),
49 qDebug("-=[WindowManager]=-");
52 void WindowManager::start()
54 qDebug("-=[start]=-");
55 mp_layoutAreaToSurfaceIdAssignment = new QMap<int, unsigned int>;
56 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
60 qDebug("ilm_init = %d", err);
61 if(ILM_SUCCESS != err)
63 qDebug("failed! Exiting!");
68 err = ilm_registerNotification(WindowManager::notificationFunc_static, this);
70 createNewLayer(WINDOWMANAGER_LAYER_POPUP);
71 createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY);
72 createNewLayer(WINDOWMANAGER_LAYER_APPLICATIONS);
73 createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN);
76 QDBusConnection dbus = QDBusConnection::sessionBus();
77 dbus.registerObject("/windowmanager", this);
78 dbus.registerService("org.agl.windowmanager");
80 // publish windowmanager interface
81 mp_windowManagerAdaptor = new WindowmanagerAdaptor((QObject*)this);
84 WindowManager::~WindowManager()
86 qDebug("-=[~WindowManager]=-");
87 delete mp_windowManagerAdaptor;
88 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
91 delete mp_layoutAreaToSurfaceIdAssignment;
94 void WindowManager::dumpScene()
97 qDebug("current layout : %d", m_currentLayout);
98 qDebug("available layouts: %d", m_layouts.size());
99 QList<Layout>::const_iterator i = m_layouts.begin();
101 while (i != m_layouts.constEnd())
103 qDebug("--[id: %d]--[%s]--", i->id, i->name.toStdString().c_str());
104 qDebug(" %d surface areas", i->layoutAreas.size());
105 for (int j = 0; j < i->layoutAreas.size(); ++j)
107 qDebug(" -area %d", j);
108 qDebug(" -x : %d", i->layoutAreas.at(j).x);
109 qDebug(" -y : %d", i->layoutAreas.at(j).y);
110 qDebug(" -width : %d", i->layoutAreas.at(j).width);
111 qDebug(" -height: %d", i->layoutAreas.at(j).height);
118 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
120 void WindowManager::createNewLayer(int layerId)
122 qDebug("-=[createNewLayer]=-");
123 qDebug("layerId %d", layerId);
125 t_ilm_uint screenID = 0;
129 ilm_getScreenResolution(screenID, &width, &height);
131 t_ilm_layer newLayerId = layerId;
132 ilm_layerCreateWithDimension(&newLayerId, width, height);
133 ilm_layerSetOpacity(newLayerId, 1.0);
134 ilm_layerSetVisibility(newLayerId, ILM_TRUE);
135 ilm_layerSetSourceRectangle(newLayerId,
140 ilm_layerSetDestinationRectangle(newLayerId,
149 void WindowManager::addSurfaceToLayer(int surfaceId, int layerId)
151 qDebug("-=[addSurfaceToLayer]=-");
152 qDebug("surfaceId %d", surfaceId);
153 qDebug("layerId %d", layerId);
155 if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN)
157 struct ilmSurfaceProperties surfaceProperties;
158 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
160 qDebug("sourceX %d", surfaceProperties.sourceX);
161 qDebug("sourceY %d", surfaceProperties.sourceY);
162 qDebug("sourceWidth %d", surfaceProperties.sourceWidth);
163 qDebug("sourceHeight %d", surfaceProperties.sourceHeight);
165 // homescreen app always fullscreen in the back
166 t_ilm_uint screenID = 0;
170 ilm_getScreenResolution(screenID, &width, &height);
172 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, width, height);
173 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, width, height);
174 ilm_surfaceSetOpacity(surfaceId, 1.0);
175 ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);
177 ilm_layerAddSurface(layerId, surfaceId);
180 if (layerId == WINDOWMANAGER_LAYER_APPLICATIONS)
182 struct ilmSurfaceProperties surfaceProperties;
183 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
185 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
186 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
187 ilm_surfaceSetOpacity(surfaceId, 0.0);
188 ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
190 ilm_layerAddSurface(layerId, surfaceId);
193 if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY)
195 struct ilmSurfaceProperties surfaceProperties;
196 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
198 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
199 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
200 ilm_surfaceSetOpacity(surfaceId, 0.5);
201 ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);
203 ilm_layerAddSurface(layerId, surfaceId);
206 if (layerId == WINDOWMANAGER_LAYER_POPUP)
208 struct ilmSurfaceProperties surfaceProperties;
209 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
211 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
212 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
213 ilm_surfaceSetOpacity(surfaceId, 0.0);
214 ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
216 ilm_layerAddSurface(layerId, surfaceId);
224 void WindowManager::updateScreen()
226 qDebug("-=[updateScreen]=-");
228 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
229 if (-1 != m_currentLayout)
233 for (int i = 0; i < m_surfaces.size(); ++i)
235 ilm_surfaceSetVisibility(m_surfaces.at(i), ILM_FALSE);
236 ilm_surfaceSetOpacity(m_surfaces.at(i), 0.0);
239 // find the current used layout
240 QList<Layout>::const_iterator ci = m_layouts.begin();
242 Layout currentLayout;
243 while (ci != m_layouts.constEnd())
245 if (ci->id == m_currentLayout)
253 qDebug("show %d apps", mp_layoutAreaToSurfaceIdAssignment->size());
254 for (int j = 0; j < mp_layoutAreaToSurfaceIdAssignment->size(); ++j)
256 int surfaceToShow = mp_layoutAreaToSurfaceIdAssignment->find(j).value();
257 qDebug(" surface no. %d: %d", j, surfaceToShow);
259 ilm_surfaceSetVisibility(surfaceToShow, ILM_TRUE);
260 ilm_surfaceSetOpacity(surfaceToShow, 1.0);
262 qDebug(" layout area %d", j);
263 qDebug(" x: %d", currentLayout.layoutAreas[j].x);
264 qDebug(" y: %d", currentLayout.layoutAreas[j].y);
265 qDebug(" w: %d", currentLayout.layoutAreas[j].width);
266 qDebug(" h: %d", currentLayout.layoutAreas[j].height);
268 ilm_surfaceSetDestinationRectangle(surfaceToShow,
269 currentLayout.layoutAreas[j].x,
270 currentLayout.layoutAreas[j].y,
271 currentLayout.layoutAreas[j].width,
272 currentLayout.layoutAreas[j].height);
278 // display layer render order
279 t_ilm_layer renderOrder[WINDOWMANAGER_LAYER_NUM];
280 renderOrder[0] = WINDOWMANAGER_LAYER_HOMESCREEN;
281 renderOrder[1] = WINDOWMANAGER_LAYER_APPLICATIONS;
282 renderOrder[2] = WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY;
283 renderOrder[3] = WINDOWMANAGER_LAYER_POPUP;
284 ilm_displaySetRenderOrder(0, renderOrder, WINDOWMANAGER_LAYER_NUM);
286 // layer surface render order
288 t_ilm_surface* pArray;
289 ilm_getSurfaceIDsOnLayer(WINDOWMANAGER_LAYER_HOMESCREEN, &length, &pArray);
290 ilm_layerSetRenderOrder(WINDOWMANAGER_LAYER_HOMESCREEN, pArray, length);
291 ilm_getSurfaceIDsOnLayer(WINDOWMANAGER_LAYER_APPLICATIONS, &length, &pArray);
292 ilm_layerSetRenderOrder(WINDOWMANAGER_LAYER_APPLICATIONS, pArray, length);
293 ilm_getSurfaceIDsOnLayer(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, &length, &pArray);
294 ilm_layerSetRenderOrder(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, pArray, length);
295 ilm_getSurfaceIDsOnLayer(WINDOWMANAGER_LAYER_POPUP, &length, &pArray);
296 ilm_layerSetRenderOrder(WINDOWMANAGER_LAYER_POPUP, pArray, length);
303 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
304 void WindowManager::notificationFunc_non_static(ilmObjectType object,
308 qDebug("-=[notificationFunc_non_static]=-");
309 qDebug("Notification from weston!");
310 if (ILM_SURFACE == object)
312 struct ilmSurfaceProperties surfaceProperties;
316 qDebug("Surface created, ID: %d", id);
317 ilm_getPropertiesOfSurface(id, &surfaceProperties);
318 qDebug(" origSourceWidth : %d", surfaceProperties.origSourceWidth);
319 qDebug(" origSourceHeight: %d", surfaceProperties.origSourceHeight);
321 if (WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID == id)
323 qDebug("HomeScreen app detected");
324 addSurfaceToLayer(id, WINDOWMANAGER_LAYER_HOMESCREEN);
329 addSurfaceToLayer(id, WINDOWMANAGER_LAYER_APPLICATIONS);
331 m_surfaces.append(id);
333 ilm_surfaceAddNotification(id, surfaceCallbackFunction_static);
339 qDebug("Surface destroyed, ID: %d", id);
340 m_surfaces.removeAt(m_surfaces.indexOf(id));
341 ilm_surfaceRemoveNotification(id);
346 if (ILM_LAYER == object)
348 //qDebug("Layer.. we don't care...");
352 void WindowManager::notificationFunc_static(ilmObjectType object,
357 static_cast<WindowManager*>(WindowManager::myThis)->notificationFunc_non_static(object, id, created);
360 void WindowManager::surfaceCallbackFunction_non_static(t_ilm_surface surface,
361 struct ilmSurfaceProperties* surfaceProperties,
362 t_ilm_notification_mask mask)
364 qDebug("-=[surfaceCallbackFunction_non_static]=-");
365 qDebug("surfaceCallbackFunction_non_static changes for surface %d", surface);
366 if (ILM_NOTIFICATION_VISIBILITY & mask)
368 qDebug("ILM_NOTIFICATION_VISIBILITY");
369 surfaceVisibilityChanged(surface, surfaceProperties->visibility);
371 if (ILM_NOTIFICATION_OPACITY & mask)
373 qDebug("ILM_NOTIFICATION_OPACITY");
375 if (ILM_NOTIFICATION_ORIENTATION & mask)
377 qDebug("ILM_NOTIFICATION_ORIENTATION");
379 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
381 qDebug("ILM_NOTIFICATION_SOURCE_RECT");
383 if (ILM_NOTIFICATION_DEST_RECT & mask)
385 qDebug("ILM_NOTIFICATION_DEST_RECT");
387 if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
389 qDebug("ILM_NOTIFICATION_CONTENT_AVAILABLE");
392 if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
394 qDebug("ILM_NOTIFICATION_CONTENT_REMOVED");
396 if (ILM_NOTIFICATION_CONFIGURED & mask)
398 qDebug("ILM_NOTIFICATION_CONFIGURED");
399 qDebug(" surfaceProperties %d", surface);
400 qDebug(" surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
401 qDebug(" surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
403 ilm_surfaceSetSourceRectangle(surface,
406 surfaceProperties->origSourceWidth,
407 surfaceProperties->origSourceHeight);
414 void WindowManager::surfaceCallbackFunction_static(t_ilm_surface surface,
415 struct ilmSurfaceProperties* surfaceProperties,
416 t_ilm_notification_mask mask)
419 static_cast<WindowManager*>(WindowManager::myThis)->surfaceCallbackFunction_non_static(surface, surfaceProperties, mask);
423 int WindowManager::layoutId() const
425 return m_currentLayout;
428 QString WindowManager::layoutName() const
430 QList<Layout>::const_iterator i = m_layouts.begin();
432 QString result = "not found";
433 while (i != m_layouts.constEnd())
435 if (i->id == m_currentLayout)
447 int WindowManager::addLayout(int layoutId, const QString &layoutName, const QList<LayoutArea> &surfaceAreas)
449 qDebug("-=[addLayout]=-");
450 m_layouts.append(Layout(layoutId, layoutName, surfaceAreas));
452 qDebug("addLayout %d %s, size %d",
454 layoutName.toStdString().c_str(),
455 surfaceAreas.size());
459 return WINDOWMANAGER_NO_ERROR;
462 int WindowManager::deleteLayoutById(int layoutId)
464 qDebug("-=[deleteLayoutById]=-");
465 qDebug("layoutId: %d", layoutId);
466 int result = WINDOWMANAGER_NO_ERROR;
468 if (m_currentLayout == layoutId)
470 result = WINDOWMANAGER_ERROR_ID_IN_USE;
474 QList<Layout>::iterator i = m_layouts.begin();
475 result = WINDOWMANAGER_ERROR_ID_IN_USE;
476 while (i != m_layouts.constEnd())
478 if (i->id == layoutId)
481 result = WINDOWMANAGER_NO_ERROR;
493 QList<Layout> WindowManager::getAllLayouts()
495 qDebug("-=[getAllLayouts]=-");
500 QList<int> WindowManager::getAllSurfacesOfProcess(int pid)
503 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
504 struct ilmSurfaceProperties surfaceProperties;
506 for (int i = 0; i < m_surfaces.size(); ++i)
508 ilm_getPropertiesOfSurface(m_surfaces.at(i), &surfaceProperties);
509 if (pid == surfaceProperties.creatorPid)
511 result.append(m_surfaces.at(i));
518 QList<int> WindowManager::getAvailableLayouts(int numberOfAppSurfaces)
520 qDebug("-=[getAvailableLayouts]=-");
521 QList<Layout>::const_iterator i = m_layouts.begin();
524 while (i != m_layouts.constEnd())
526 if (i->layoutAreas.size() == numberOfAppSurfaces)
528 result.append(i->id);
537 QList<int> WindowManager::getAvailableSurfaces()
539 qDebug("-=[getAvailableSurfaces]=-");
544 QString WindowManager::getLayoutName(int layoutId)
546 qDebug("-=[getLayoutName]=-");
547 QList<Layout>::const_iterator i = m_layouts.begin();
549 QString result = "not found";
550 while (i != m_layouts.constEnd())
552 if (i->id == layoutId)
563 void WindowManager::hideLayer(int layer)
565 qDebug("-=[hideLayer]=-");
566 qDebug("layer %d", layer);
568 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
571 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_POPUP, ILM_FALSE);
575 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, ILM_FALSE);
579 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_APPLICATIONS, ILM_FALSE);
583 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN, ILM_FALSE);
589 int WindowManager::setLayoutById(int layoutId)
591 qDebug("-=[setLayoutById]=-");
592 int result = WINDOWMANAGER_NO_ERROR;
593 m_currentLayout = layoutId;
595 mp_layoutAreaToSurfaceIdAssignment->clear();
602 int WindowManager::setLayoutByName(const QString &layoutName)
604 qDebug("-=[setLayoutByName]=-");
605 int result = WINDOWMANAGER_NO_ERROR;
607 QList<Layout>::const_iterator i = m_layouts.begin();
609 while (i != m_layouts.constEnd())
611 if (i->name == layoutName)
613 m_currentLayout = i->id;
615 mp_layoutAreaToSurfaceIdAssignment->clear();
626 int WindowManager::setSurfaceToLayoutArea(int surfaceId, int layoutAreaId)
628 qDebug("-=[setSurfaceToLayoutArea]=-");
629 int result = WINDOWMANAGER_NO_ERROR;
631 qDebug("surfaceId %d", surfaceId);
632 qDebug("layoutAreaId %d", layoutAreaId);
633 mp_layoutAreaToSurfaceIdAssignment->insert(layoutAreaId, surfaceId);
642 void WindowManager::showLayer(int layer)
644 qDebug("-=[showLayer]=-");
645 qDebug("layer %d", layer);
647 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
650 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_POPUP, ILM_TRUE);
654 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, ILM_TRUE);
658 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_APPLICATIONS, ILM_TRUE);
662 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN, ILM_TRUE);