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),
48 m_screenId(0), // use screen "0"
52 qDebug("-=[WindowManager]=-");
55 void WindowManager::start()
57 qDebug("-=[start]=-");
58 mp_layoutAreaToSurfaceIdAssignment = new QMap<int, unsigned int>;
59 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
63 qDebug("ilm_init = %d", err);
64 if(ILM_SUCCESS != err)
66 qDebug("failed! Exiting!");
72 ilm_getScreenResolution(m_screenId, &m_screenWidth, &m_screenHeight);
74 createNewLayer(WINDOWMANAGER_LAYER_POPUP);
75 createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY);
76 createNewLayer(WINDOWMANAGER_LAYER_APPLICATIONS);
77 createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN);
79 ilm_registerNotification(WindowManager::notificationFunc_static, this);
82 QDBusConnection dbus = QDBusConnection::sessionBus();
83 dbus.registerObject("/windowmanager", this);
84 dbus.registerService("org.agl.windowmanager");
86 // publish windowmanager interface
87 mp_windowManagerAdaptor = new WindowmanagerAdaptor((QObject*)this);
90 WindowManager::~WindowManager()
92 qDebug("-=[~WindowManager]=-");
93 delete mp_windowManagerAdaptor;
94 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
97 delete mp_layoutAreaToSurfaceIdAssignment;
100 void WindowManager::dumpScene()
103 qDebug("current layout : %d", m_currentLayout);
104 qDebug("available layouts: %d", m_layouts.size());
105 QList<Layout>::const_iterator i = m_layouts.begin();
107 while (i != m_layouts.constEnd())
109 qDebug("--[id: %d]--[%s]--", i->id, i->name.toStdString().c_str());
110 qDebug(" %d surface areas", i->layoutAreas.size());
111 for (int j = 0; j < i->layoutAreas.size(); ++j)
113 qDebug(" -area %d", j);
114 qDebug(" -x : %d", i->layoutAreas.at(j).x);
115 qDebug(" -y : %d", i->layoutAreas.at(j).y);
116 qDebug(" -width : %d", i->layoutAreas.at(j).width);
117 qDebug(" -height: %d", i->layoutAreas.at(j).height);
124 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
126 void WindowManager::createNewLayer(int layerId)
128 qDebug("-=[createNewLayer]=-");
129 qDebug(" layerId %d", layerId);
131 t_ilm_layer newLayerId = layerId;
132 ilm_layerCreateWithDimension(&newLayerId, m_screenWidth, m_screenHeight);
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 // homescreen app always fullscreen in the back
161 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, m_screenWidth, m_screenHeight);
162 //ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, m_screenWidth, m_screenHeight);
163 ilm_surfaceSetOpacity(surfaceId, 1.0);
164 ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);
166 ilm_layerAddSurface(layerId, surfaceId);
169 if (layerId == WINDOWMANAGER_LAYER_APPLICATIONS)
171 struct ilmSurfaceProperties surfaceProperties;
172 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
174 //ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
175 //ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
176 //ilm_surfaceSetOpacity(surfaceId, 0.0);
177 //ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
179 ilm_layerAddSurface(layerId, surfaceId);
182 if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY)
184 struct ilmSurfaceProperties surfaceProperties;
185 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
187 //ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
188 //ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
189 //ilm_surfaceSetOpacity(surfaceId, 0.5);
190 //ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);
192 ilm_layerAddSurface(layerId, surfaceId);
195 if (layerId == WINDOWMANAGER_LAYER_POPUP)
197 struct ilmSurfaceProperties surfaceProperties;
198 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
200 //ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
201 //ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
202 //ilm_surfaceSetOpacity(surfaceId, 0.0);
203 //ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
205 ilm_layerAddSurface(layerId, surfaceId);
213 void WindowManager::updateScreen()
215 qDebug("-=[updateScreen]=-");
217 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
218 if (-1 != m_currentLayout)
221 for (int i = 0; i < m_appSurfaces.size(); ++i)
223 ilm_layerRemoveSurface(WINDOWMANAGER_LAYER_APPLICATIONS, m_appSurfaces.at(i));
224 //ilm_surfaceSetVisibility(m_appSurfaces.at(i), ILM_FALSE);
225 //ilm_surfaceSetOpacity(m_appSurfaces.at(i), 0.0);
229 // find the current used layout
230 QList<Layout>::const_iterator ci = m_layouts.begin();
232 Layout currentLayout;
233 while (ci != m_layouts.constEnd())
235 if (ci->id == m_currentLayout)
243 qDebug("show %d apps", mp_layoutAreaToSurfaceIdAssignment->size());
244 for (int j = 0; j < mp_layoutAreaToSurfaceIdAssignment->size(); ++j)
246 int surfaceToShow = mp_layoutAreaToSurfaceIdAssignment->find(j).value();
247 qDebug(" surface no. %d: %d", j, surfaceToShow);
249 addSurfaceToLayer(surfaceToShow, WINDOWMANAGER_LAYER_APPLICATIONS);
251 ilm_surfaceSetVisibility(surfaceToShow, ILM_TRUE);
252 ilm_surfaceSetOpacity(surfaceToShow, 1.0);
254 qDebug(" layout area %d", j);
255 qDebug(" x: %d", currentLayout.layoutAreas[j].x);
256 qDebug(" y: %d", currentLayout.layoutAreas[j].y);
257 qDebug(" w: %d", currentLayout.layoutAreas[j].width);
258 qDebug(" h: %d", currentLayout.layoutAreas[j].height);
260 ilm_surfaceSetDestinationRectangle(surfaceToShow,
261 currentLayout.layoutAreas[j].x,
262 currentLayout.layoutAreas[j].y,
263 currentLayout.layoutAreas[j].width,
264 currentLayout.layoutAreas[j].height);
269 // layer surface render order
271 t_ilm_surface* pArray;
272 ilm_getSurfaceIDsOnLayer(WINDOWMANAGER_LAYER_HOMESCREEN, &length, &pArray);
273 ilm_layerSetRenderOrder(WINDOWMANAGER_LAYER_HOMESCREEN, pArray, length);
275 ilm_getSurfaceIDsOnLayer(WINDOWMANAGER_LAYER_APPLICATIONS, &length, &pArray);
276 ilm_layerSetRenderOrder(WINDOWMANAGER_LAYER_APPLICATIONS, pArray, length);
278 ilm_getSurfaceIDsOnLayer(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, &length, &pArray);
279 ilm_layerSetRenderOrder(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, pArray, length);
281 ilm_getSurfaceIDsOnLayer(WINDOWMANAGER_LAYER_POPUP, &length, &pArray);
282 ilm_layerSetRenderOrder(WINDOWMANAGER_LAYER_POPUP, pArray, length);
285 // display layer render order
286 t_ilm_layer renderOrder[WINDOWMANAGER_LAYER_NUM];
287 renderOrder[0] = WINDOWMANAGER_LAYER_HOMESCREEN;
288 renderOrder[1] = WINDOWMANAGER_LAYER_APPLICATIONS;
289 renderOrder[2] = WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY;
290 renderOrder[3] = WINDOWMANAGER_LAYER_POPUP;
291 ilm_displaySetRenderOrder(0, renderOrder, WINDOWMANAGER_LAYER_NUM);
292 ilm_displaySetRenderOrder(1, renderOrder, WINDOWMANAGER_LAYER_NUM);
297 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
298 void WindowManager::notificationFunc_non_static(ilmObjectType object,
302 qDebug("-=[notificationFunc_non_static]=-");
303 qDebug("Notification from weston!");
304 if (ILM_SURFACE == object)
306 struct ilmSurfaceProperties surfaceProperties;
310 qDebug("Surface created, ID: %d", id);
311 ilm_getPropertiesOfSurface(id, &surfaceProperties);
312 qDebug(" origSourceWidth : %d", surfaceProperties.origSourceWidth);
313 qDebug(" origSourceHeight: %d", surfaceProperties.origSourceHeight);
315 if (WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID == id)
317 qDebug("HomeScreen app detected");
318 addSurfaceToLayer(id, WINDOWMANAGER_LAYER_HOMESCREEN);
323 //addSurfaceToLayer(id, WINDOWMANAGER_LAYER_APPLICATIONS);
325 m_appSurfaces.append(id);
327 ilm_surfaceAddNotification(id, surfaceCallbackFunction_static);
333 qDebug("Surface destroyed, ID: %d", id);
334 m_appSurfaces.removeAt(m_appSurfaces.indexOf(id));
335 ilm_surfaceRemoveNotification(id);
340 if (ILM_LAYER == object)
342 //qDebug("Layer.. we don't care...");
346 void WindowManager::notificationFunc_static(ilmObjectType object,
351 static_cast<WindowManager*>(WindowManager::myThis)->notificationFunc_non_static(object, id, created);
354 void WindowManager::surfaceCallbackFunction_non_static(t_ilm_surface surface,
355 struct ilmSurfaceProperties* surfaceProperties,
356 t_ilm_notification_mask mask)
358 qDebug("-=[surfaceCallbackFunction_non_static]=-");
359 qDebug("surfaceCallbackFunction_non_static changes for surface %d", surface);
360 if (ILM_NOTIFICATION_VISIBILITY & mask)
362 qDebug("ILM_NOTIFICATION_VISIBILITY");
363 surfaceVisibilityChanged(surface, surfaceProperties->visibility);
365 if (ILM_NOTIFICATION_OPACITY & mask)
367 qDebug("ILM_NOTIFICATION_OPACITY");
369 if (ILM_NOTIFICATION_ORIENTATION & mask)
371 qDebug("ILM_NOTIFICATION_ORIENTATION");
373 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
375 qDebug("ILM_NOTIFICATION_SOURCE_RECT");
377 if (ILM_NOTIFICATION_DEST_RECT & mask)
379 qDebug("ILM_NOTIFICATION_DEST_RECT");
381 if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
383 qDebug("ILM_NOTIFICATION_CONTENT_AVAILABLE");
386 if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
388 qDebug("ILM_NOTIFICATION_CONTENT_REMOVED");
390 if (ILM_NOTIFICATION_CONFIGURED & mask)
392 qDebug("ILM_NOTIFICATION_CONFIGURED");
393 qDebug(" surfaceProperties %d", surface);
394 qDebug(" surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
395 qDebug(" surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
397 ilm_surfaceSetSourceRectangle(surface,
400 surfaceProperties->origSourceWidth,
401 surfaceProperties->origSourceHeight);
408 void WindowManager::surfaceCallbackFunction_static(t_ilm_surface surface,
409 struct ilmSurfaceProperties* surfaceProperties,
410 t_ilm_notification_mask mask)
413 static_cast<WindowManager*>(WindowManager::myThis)->surfaceCallbackFunction_non_static(surface, surfaceProperties, mask);
417 int WindowManager::layoutId() const
419 return m_currentLayout;
422 QString WindowManager::layoutName() const
424 QList<Layout>::const_iterator i = m_layouts.begin();
426 QString result = "not found";
427 while (i != m_layouts.constEnd())
429 if (i->id == m_currentLayout)
441 int WindowManager::addLayout(int layoutId, const QString &layoutName, const QList<LayoutArea> &surfaceAreas)
443 qDebug("-=[addLayout]=-");
444 m_layouts.append(Layout(layoutId, layoutName, surfaceAreas));
446 qDebug("addLayout %d %s, size %d",
448 layoutName.toStdString().c_str(),
449 surfaceAreas.size());
453 return WINDOWMANAGER_NO_ERROR;
456 int WindowManager::deleteLayoutById(int layoutId)
458 qDebug("-=[deleteLayoutById]=-");
459 qDebug("layoutId: %d", layoutId);
460 int result = WINDOWMANAGER_NO_ERROR;
462 if (m_currentLayout == layoutId)
464 result = WINDOWMANAGER_ERROR_ID_IN_USE;
468 QList<Layout>::iterator i = m_layouts.begin();
469 result = WINDOWMANAGER_ERROR_ID_IN_USE;
470 while (i != m_layouts.constEnd())
472 if (i->id == layoutId)
475 result = WINDOWMANAGER_NO_ERROR;
487 QList<Layout> WindowManager::getAllLayouts()
489 qDebug("-=[getAllLayouts]=-");
494 QList<int> WindowManager::getAllSurfacesOfProcess(int pid)
497 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
498 struct ilmSurfaceProperties surfaceProperties;
500 for (int i = 0; i < m_appSurfaces.size(); ++i)
502 ilm_getPropertiesOfSurface(m_appSurfaces.at(i), &surfaceProperties);
503 if (pid == surfaceProperties.creatorPid)
505 result.append(m_appSurfaces.at(i));
512 QList<int> WindowManager::getAvailableLayouts(int numberOfAppSurfaces)
514 qDebug("-=[getAvailableLayouts]=-");
515 QList<Layout>::const_iterator i = m_layouts.begin();
518 while (i != m_layouts.constEnd())
520 if (i->layoutAreas.size() == numberOfAppSurfaces)
522 result.append(i->id);
531 QList<int> WindowManager::getAvailableSurfaces()
533 qDebug("-=[getAvailableSurfaces]=-");
535 return m_appSurfaces;
538 QString WindowManager::getLayoutName(int layoutId)
540 qDebug("-=[getLayoutName]=-");
541 QList<Layout>::const_iterator i = m_layouts.begin();
543 QString result = "not found";
544 while (i != m_layouts.constEnd())
546 if (i->id == layoutId)
557 void WindowManager::hideLayer(int layer)
559 qDebug("-=[hideLayer]=-");
560 qDebug("layer %d", layer);
562 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
565 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_POPUP, ILM_FALSE);
569 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, ILM_FALSE);
573 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_APPLICATIONS, ILM_FALSE);
577 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN, ILM_FALSE);
583 int WindowManager::setLayoutById(int layoutId)
585 qDebug("-=[setLayoutById]=-");
586 int result = WINDOWMANAGER_NO_ERROR;
587 m_currentLayout = layoutId;
589 mp_layoutAreaToSurfaceIdAssignment->clear();
596 int WindowManager::setLayoutByName(const QString &layoutName)
598 qDebug("-=[setLayoutByName]=-");
599 int result = WINDOWMANAGER_NO_ERROR;
601 QList<Layout>::const_iterator i = m_layouts.begin();
603 while (i != m_layouts.constEnd())
605 if (i->name == layoutName)
607 m_currentLayout = i->id;
609 mp_layoutAreaToSurfaceIdAssignment->clear();
620 int WindowManager::setSurfaceToLayoutArea(int surfaceId, int layoutAreaId)
622 qDebug("-=[setSurfaceToLayoutArea]=-");
623 int result = WINDOWMANAGER_NO_ERROR;
625 qDebug("surfaceId %d", surfaceId);
626 qDebug("layoutAreaId %d", layoutAreaId);
627 mp_layoutAreaToSurfaceIdAssignment->insert(layoutAreaId, surfaceId);
636 void WindowManager::showLayer(int layer)
638 qDebug("-=[showLayer]=-");
639 qDebug("layer %d", layer);
641 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
644 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_POPUP, ILM_TRUE);
648 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, ILM_TRUE);
652 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_APPLICATIONS, ILM_TRUE);
656 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN, ILM_TRUE);