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_HOMESCREEN_OVERLAY 101
32 #define WINDOWMANAGER_LAYER_APPLICATIONS 102
33 #define WINDOWMANAGER_LAYER_HOMESCREEN 103
35 #define WINDOWMANAGER_LAYER_NUM 3
37 // the HomeScreen app has to have the surface id 1000
38 #define WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID 1000
41 void* WindowManager::myThis = 0;
43 WindowManager::WindowManager(QObject *parent) :
47 mp_layoutAreaToSurfaceIdAssignment(0),
50 qDebug("-=[WindowManager]=-");
51 // publish windowmanager interface
52 mp_windowManagerAdaptor = new WindowmanagerAdaptor((QObject*)this);
54 QDBusConnection dbus = QDBusConnection::sessionBus();
55 dbus.registerObject("/windowmanager", this);
56 dbus.registerService("org.agl.windowmanager");
59 void WindowManager::start()
61 qDebug("-=[start]=-");
62 mp_layoutAreaToSurfaceIdAssignment = new QMap<int, unsigned int>;
63 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
67 qDebug("ilm_init = %d", err);
70 err = ilm_registerNotification(WindowManager::notificationFunc_static, this);
72 createNewLayer(WINDOWMANAGER_LAYER_POPUP);
73 createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY);
74 createNewLayer(WINDOWMANAGER_LAYER_APPLICATIONS);
75 createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN);
79 WindowManager::~WindowManager()
81 qDebug("-=[~WindowManager]=-");
82 delete mp_windowManagerAdaptor;
83 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
86 delete mp_layoutAreaToSurfaceIdAssignment;
89 void WindowManager::dumpScene()
92 qDebug("current layout : %d", m_currentLayout);
93 qDebug("available layouts: %d", m_layouts.size());
94 QList<Layout>::const_iterator i = m_layouts.begin();
96 while (i != m_layouts.constEnd())
98 qDebug("--[id: %d]--[%s]--", i->id, i->name.toStdString().c_str());
99 qDebug(" %d surface areas", i->layoutAreas.size());
100 for (int j = 0; j < i->layoutAreas.size(); ++j)
102 qDebug(" -area %d", j);
103 qDebug(" -x : %d", i->layoutAreas.at(j).x);
104 qDebug(" -y : %d", i->layoutAreas.at(j).y);
105 qDebug(" -width : %d", i->layoutAreas.at(j).width);
106 qDebug(" -height: %d", i->layoutAreas.at(j).height);
113 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
115 void WindowManager::createNewLayer(int layerId)
117 qDebug("-=[createNewLayer]=-");
118 qDebug("layerId %d", layerId);
120 t_ilm_uint screenID = 0;
124 ilm_getScreenResolution(screenID, &width, &height);
126 t_ilm_layer newLayerId = layerId;
127 ilm_layerCreateWithDimension(&newLayerId, width, height);
128 ilm_layerSetOpacity(newLayerId, 1.0);
129 ilm_layerSetVisibility(newLayerId, ILM_TRUE);
130 ilm_layerSetSourceRectangle(newLayerId,
135 ilm_layerSetDestinationRectangle(newLayerId,
144 void WindowManager::addSurfaceToLayer(int surfaceId, int layerId)
146 qDebug("-=[addSurfaceToLayer]=-");
147 qDebug("surfaceId %d", surfaceId);
148 qDebug("layerId %d", layerId);
150 if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN)
152 struct ilmSurfaceProperties surfaceProperties;
153 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
155 qDebug("sourceX %d", surfaceProperties.sourceX);
156 qDebug("sourceY %d", surfaceProperties.sourceY);
157 qDebug("sourceWidth %d", surfaceProperties.sourceWidth);
158 qDebug("sourceHeight %d", surfaceProperties.sourceHeight);
160 // homescreen app always fullscreen in the back
161 t_ilm_uint screenID = 0;
165 ilm_getScreenResolution(screenID, &width, &height);
167 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, width, height);
168 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, width, height);
169 ilm_surfaceSetOpacity(surfaceId, 1.0);
170 ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);
172 ilm_layerAddSurface(layerId, surfaceId);
175 if (layerId == WINDOWMANAGER_LAYER_APPLICATIONS)
177 struct ilmSurfaceProperties surfaceProperties;
178 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
180 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
181 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
182 ilm_surfaceSetOpacity(surfaceId, 0.0);
183 ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
185 ilm_layerAddSurface(layerId, surfaceId);
188 if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY)
190 struct ilmSurfaceProperties surfaceProperties;
191 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
193 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
194 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
195 ilm_surfaceSetOpacity(surfaceId, 0.5);
196 ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);
198 ilm_layerAddSurface(layerId, surfaceId);
201 if (layerId == WINDOWMANAGER_LAYER_POPUP)
203 struct ilmSurfaceProperties surfaceProperties;
204 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
206 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
207 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
208 ilm_surfaceSetOpacity(surfaceId, 0.0);
209 ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
211 ilm_layerAddSurface(layerId, surfaceId);
219 void WindowManager::updateScreen()
221 qDebug("-=[updateScreen]=-");
223 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
224 if (-1 != m_currentLayout)
228 for (int i = 0; i < m_surfaces.size(); ++i)
230 ilm_surfaceSetVisibility(m_surfaces.at(i), ILM_FALSE);
231 ilm_surfaceSetOpacity(m_surfaces.at(i), 0.0);
234 // find the current used layout
235 QList<Layout>::const_iterator ci = m_layouts.begin();
237 Layout currentLayout;
238 while (ci != m_layouts.constEnd())
240 if (ci->id == m_currentLayout)
248 qDebug("show %d apps", mp_layoutAreaToSurfaceIdAssignment->size());
249 for (int j = 0; j < mp_layoutAreaToSurfaceIdAssignment->size(); ++j)
251 int surfaceToShow = mp_layoutAreaToSurfaceIdAssignment->find(j).value();
252 qDebug(" surface no. %d: %d", j, surfaceToShow);
254 ilm_surfaceSetVisibility(surfaceToShow, ILM_TRUE);
255 ilm_surfaceSetOpacity(surfaceToShow, 1.0);
257 qDebug(" layout area %d", j);
258 qDebug(" x: %d", currentLayout.layoutAreas[j].x);
259 qDebug(" y: %d", currentLayout.layoutAreas[j].y);
260 qDebug(" w: %d", currentLayout.layoutAreas[j].width);
261 qDebug(" h: %d", currentLayout.layoutAreas[j].height);
263 ilm_surfaceSetDestinationRectangle(surfaceToShow,
264 currentLayout.layoutAreas[j].x,
265 currentLayout.layoutAreas[j].y,
266 currentLayout.layoutAreas[j].width,
267 currentLayout.layoutAreas[j].height);
273 t_ilm_layer renderOrder[WINDOWMANAGER_LAYER_NUM];
274 renderOrder[0] = WINDOWMANAGER_LAYER_HOMESCREEN;
275 renderOrder[1] = WINDOWMANAGER_LAYER_APPLICATIONS;
276 renderOrder[2] = WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY;
277 renderOrder[3] = WINDOWMANAGER_LAYER_POPUP;
279 ilm_displaySetRenderOrder(0, renderOrder, WINDOWMANAGER_LAYER_NUM);
286 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
287 void WindowManager::notificationFunc_non_static(ilmObjectType object,
291 qDebug("-=[notificationFunc_non_static]=-");
292 qDebug("Notification from weston!");
293 if (ILM_SURFACE == object)
295 struct ilmSurfaceProperties surfaceProperties;
299 qDebug("Surface created, ID: %d", id);
300 ilm_getPropertiesOfSurface(id, &surfaceProperties);
301 qDebug(" origSourceWidth : %d", surfaceProperties.origSourceWidth);
302 qDebug(" origSourceHeight: %d", surfaceProperties.origSourceHeight);
304 if (WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID == id)
306 qDebug("HomeScreen app detected");
307 addSurfaceToLayer(id, WINDOWMANAGER_LAYER_HOMESCREEN);
312 addSurfaceToLayer(id, WINDOWMANAGER_LAYER_APPLICATIONS);
314 m_surfaces.append(id);
316 ilm_surfaceAddNotification(id, surfaceCallbackFunction_static);
322 qDebug("Surface destroyed, ID: %d", id);
323 m_surfaces.removeAt(m_surfaces.indexOf(id));
324 ilm_surfaceRemoveNotification(id);
329 if (ILM_LAYER == object)
331 //qDebug("Layer.. we don't care...");
335 void WindowManager::notificationFunc_static(ilmObjectType object,
340 static_cast<WindowManager*>(WindowManager::myThis)->notificationFunc_non_static(object, id, created);
343 void WindowManager::surfaceCallbackFunction_non_static(t_ilm_surface surface,
344 struct ilmSurfaceProperties* surfaceProperties,
345 t_ilm_notification_mask mask)
347 qDebug("-=[surfaceCallbackFunction_non_static]=-");
348 qDebug("surfaceCallbackFunction_non_static changes for surface %d", surface);
349 if (ILM_NOTIFICATION_VISIBILITY & mask)
351 qDebug("ILM_NOTIFICATION_VISIBILITY");
352 surfaceVisibilityChanged(surface, surfaceProperties->visibility);
354 if (ILM_NOTIFICATION_OPACITY & mask)
356 qDebug("ILM_NOTIFICATION_OPACITY");
358 if (ILM_NOTIFICATION_ORIENTATION & mask)
360 qDebug("ILM_NOTIFICATION_ORIENTATION");
362 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
364 qDebug("ILM_NOTIFICATION_SOURCE_RECT");
366 if (ILM_NOTIFICATION_DEST_RECT & mask)
368 qDebug("ILM_NOTIFICATION_DEST_RECT");
370 if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
372 qDebug("ILM_NOTIFICATION_CONTENT_AVAILABLE");
375 if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
377 qDebug("ILM_NOTIFICATION_CONTENT_REMOVED");
379 if (ILM_NOTIFICATION_CONFIGURED & mask)
381 qDebug("ILM_NOTIFICATION_CONFIGURED");
382 qDebug(" surfaceProperties %d", surface);
383 qDebug(" surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
384 qDebug(" surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
386 ilm_surfaceSetSourceRectangle(surface,
389 surfaceProperties->origSourceWidth,
390 surfaceProperties->origSourceHeight);
397 void WindowManager::surfaceCallbackFunction_static(t_ilm_surface surface,
398 struct ilmSurfaceProperties* surfaceProperties,
399 t_ilm_notification_mask mask)
402 static_cast<WindowManager*>(WindowManager::myThis)->surfaceCallbackFunction_non_static(surface, surfaceProperties, mask);
406 int WindowManager::layoutId() const
408 return m_currentLayout;
411 QString WindowManager::layoutName() const
413 QList<Layout>::const_iterator i = m_layouts.begin();
415 QString result = "not found";
416 while (i != m_layouts.constEnd())
418 if (i->id == m_currentLayout)
430 int WindowManager::addLayout(int layoutId, const QString &layoutName, const QList<LayoutArea> &surfaceAreas)
432 qDebug("-=[addLayout]=-");
433 m_layouts.append(Layout(layoutId, layoutName, surfaceAreas));
435 qDebug("addLayout %d %s, size %d",
437 layoutName.toStdString().c_str(),
438 surfaceAreas.size());
442 return WINDOWMANAGER_NO_ERROR;
445 int WindowManager::deleteLayoutById(int layoutId)
447 qDebug("-=[deleteLayoutById]=-");
448 qDebug("layoutId: %d", layoutId);
449 int result = WINDOWMANAGER_NO_ERROR;
451 if (m_currentLayout == layoutId)
453 result = WINDOWMANAGER_ERROR_ID_IN_USE;
457 QList<Layout>::iterator i = m_layouts.begin();
458 result = WINDOWMANAGER_ERROR_ID_IN_USE;
459 while (i != m_layouts.constEnd())
461 if (i->id == layoutId)
464 result = WINDOWMANAGER_NO_ERROR;
476 QList<Layout> WindowManager::getAllLayouts()
478 qDebug("-=[getAllLayouts]=-");
483 QList<int> WindowManager::getAllSurfacesOfProcess(int pid)
486 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
487 struct ilmSurfaceProperties surfaceProperties;
489 for (int i = 0; i < m_surfaces.size(); ++i)
491 ilm_getPropertiesOfSurface(m_surfaces.at(i), &surfaceProperties);
492 if (pid == surfaceProperties.creatorPid)
494 result.append(m_surfaces.at(i));
501 QList<int> WindowManager::getAvailableLayouts(int numberOfAppSurfaces)
503 qDebug("-=[getAvailableLayouts]=-");
504 QList<Layout>::const_iterator i = m_layouts.begin();
507 while (i != m_layouts.constEnd())
509 if (i->layoutAreas.size() == numberOfAppSurfaces)
511 result.append(i->id);
520 QList<int> WindowManager::getAvailableSurfaces()
522 qDebug("-=[getAvailableSurfaces]=-");
527 QString WindowManager::getLayoutName(int layoutId)
529 qDebug("-=[getLayoutName]=-");
530 QList<Layout>::const_iterator i = m_layouts.begin();
532 QString result = "not found";
533 while (i != m_layouts.constEnd())
535 if (i->id == layoutId)
546 void WindowManager::hideLayer(int layer)
548 qDebug("-=[hideLayer]=-");
549 qDebug("layer %d", layer);
551 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
554 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_POPUP, ILM_FALSE);
558 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, ILM_FALSE);
562 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_APPLICATIONS, ILM_FALSE);
566 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN, ILM_FALSE);
572 int WindowManager::setLayoutById(int layoutId)
574 qDebug("-=[setLayoutById]=-");
575 int result = WINDOWMANAGER_NO_ERROR;
576 m_currentLayout = layoutId;
578 mp_layoutAreaToSurfaceIdAssignment->clear();
585 int WindowManager::setLayoutByName(const QString &layoutName)
587 qDebug("-=[setLayoutByName]=-");
588 int result = WINDOWMANAGER_NO_ERROR;
590 QList<Layout>::const_iterator i = m_layouts.begin();
592 while (i != m_layouts.constEnd())
594 if (i->name == layoutName)
596 m_currentLayout = i->id;
598 mp_layoutAreaToSurfaceIdAssignment->clear();
609 int WindowManager::setSurfaceToLayoutArea(int surfaceId, int layoutAreaId)
611 qDebug("-=[setSurfaceToLayoutArea]=-");
612 int result = WINDOWMANAGER_NO_ERROR;
614 qDebug("surfaceId %d", surfaceId);
615 qDebug("layoutAreaId %d", layoutAreaId);
616 mp_layoutAreaToSurfaceIdAssignment->insert(layoutAreaId, surfaceId);
625 void WindowManager::showLayer(int layer)
627 qDebug("-=[showLayer]=-");
628 qDebug("layer %d", layer);
630 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
633 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_POPUP, ILM_TRUE);
637 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, ILM_TRUE);
641 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_APPLICATIONS, ILM_TRUE);
645 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN, ILM_TRUE);