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 // 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]=-");
50 // publish windowmanager interface
51 mp_windowManagerAdaptor = new WindowmanagerAdaptor((QObject*)this);
53 QDBusConnection dbus = QDBusConnection::sessionBus();
54 dbus.registerObject("/windowmanager", this);
55 dbus.registerService("org.agl.windowmanager");
58 void WindowManager::start()
60 qDebug("-=[start]=-");
61 mp_layoutAreaToSurfaceIdAssignment = new QMap<int, unsigned int>;
66 qDebug("ilm_init = %d", err);
69 err = ilm_registerNotification(WindowManager::notificationFunc_static, this);
71 createNewLayer(WINDOWMANAGER_LAYER_POPUP);
72 createNewLayer(WINDOWMANAGER_LAYER_APPLICATIONS);
73 createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN);
77 WindowManager::~WindowManager()
79 qDebug("-=[~WindowManager]=-");
80 delete mp_windowManagerAdaptor;
84 delete mp_layoutAreaToSurfaceIdAssignment;
87 void WindowManager::dumpScene()
90 qDebug("current layout : %d", m_currentLayout);
91 qDebug("available layouts: %d", m_layouts.size());
92 QList<Layout>::const_iterator i = m_layouts.begin();
94 while (i != m_layouts.constEnd())
96 qDebug("--[id: %d]--[%s]--", i->id, i->name.toStdString().c_str());
97 qDebug(" %d surface areas", i->layoutAreas.size());
98 for (int j = 0; j < i->layoutAreas.size(); ++j)
100 qDebug(" -area %d", j);
101 qDebug(" -x : %d", i->layoutAreas.at(j).x);
102 qDebug(" -y : %d", i->layoutAreas.at(j).y);
103 qDebug(" -width : %d", i->layoutAreas.at(j).width);
104 qDebug(" -height: %d", i->layoutAreas.at(j).height);
113 void WindowManager::createNewLayer(int layerId)
115 qDebug("-=[createNewLayer]=-");
116 qDebug("layerId %d", layerId);
118 t_ilm_uint screenID = 0;
122 ilm_getScreenResolution(screenID, &width, &height);
124 t_ilm_layer newLayerId = layerId;
125 ilm_layerCreateWithDimension(&newLayerId, width, height);
126 ilm_layerSetOpacity(newLayerId, 1.0);
127 ilm_layerSetVisibility(newLayerId, ILM_TRUE);
128 ilm_layerSetSourceRectangle(newLayerId,
133 ilm_layerSetDestinationRectangle(newLayerId,
142 void WindowManager::addSurfaceToLayer(int surfaceId, int layerId)
144 qDebug("-=[addSurfaceToLayer]=-");
145 qDebug("surfaceId %d", surfaceId);
146 qDebug("layerId %d", layerId);
148 if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN)
150 struct ilmSurfaceProperties surfaceProperties;
151 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
153 qDebug("sourceX %d", surfaceProperties.sourceX);
154 qDebug("sourceY %d", surfaceProperties.sourceY);
155 qDebug("sourceWidth %d", surfaceProperties.sourceWidth);
156 qDebug("sourceHeight %d", surfaceProperties.sourceHeight);
158 // homescreen app always fullscreen in the back
159 t_ilm_uint screenID = 0;
163 ilm_getScreenResolution(screenID, &width, &height);
165 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, width, height);
166 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, width, height);
167 ilm_surfaceSetOpacity(surfaceId, 1.0);
168 ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);
170 ilm_layerAddSurface(layerId, surfaceId);
173 if (layerId == WINDOWMANAGER_LAYER_APPLICATIONS)
175 struct ilmSurfaceProperties surfaceProperties;
176 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
178 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
179 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
180 ilm_surfaceSetOpacity(surfaceId, 0.0);
181 ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
183 ilm_layerAddSurface(layerId, surfaceId);
186 if (layerId == WINDOWMANAGER_LAYER_POPUP)
188 struct ilmSurfaceProperties surfaceProperties;
189 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
191 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
192 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
193 ilm_surfaceSetOpacity(surfaceId, 0.0);
194 ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
196 ilm_layerAddSurface(layerId, surfaceId);
204 void WindowManager::updateScreen()
206 qDebug("-=[updateScreen]=-");
209 if (-1 != m_currentLayout)
213 for (int i = 0; i < m_surfaces.size(); ++i)
215 ilm_surfaceSetVisibility(m_surfaces.at(i), ILM_FALSE);
216 ilm_surfaceSetOpacity(m_surfaces.at(i), 0.0);
219 // find the current used layout
220 QList<Layout>::const_iterator ci = m_layouts.begin();
222 Layout currentLayout;
223 while (ci != m_layouts.constEnd())
225 if (ci->id == m_currentLayout)
233 qDebug("show %d apps", mp_layoutAreaToSurfaceIdAssignment->size());
234 for (int j = 0; j < mp_layoutAreaToSurfaceIdAssignment->size(); ++j)
236 int surfaceToShow = mp_layoutAreaToSurfaceIdAssignment->find(j).value();
237 qDebug(" surface no. %d: %d", j, surfaceToShow);
239 ilm_surfaceSetVisibility(surfaceToShow, ILM_TRUE);
240 ilm_surfaceSetOpacity(surfaceToShow, 1.0);
242 qDebug(" layout area %d", j);
243 qDebug(" x: %d", currentLayout.layoutAreas[j].x);
244 qDebug(" y: %d", currentLayout.layoutAreas[j].y);
245 qDebug(" w: %d", currentLayout.layoutAreas[j].width);
246 qDebug(" h: %d", currentLayout.layoutAreas[j].height);
248 ilm_surfaceSetDestinationRectangle(surfaceToShow,
249 currentLayout.layoutAreas[j].x,
250 currentLayout.layoutAreas[j].y,
251 currentLayout.layoutAreas[j].width,
252 currentLayout.layoutAreas[j].height);
258 t_ilm_layer renderOrder[WINDOWMANAGER_LAYER_NUM];
259 renderOrder[0] = WINDOWMANAGER_LAYER_HOMESCREEN;
260 renderOrder[1] = WINDOWMANAGER_LAYER_APPLICATIONS;
261 renderOrder[2] = WINDOWMANAGER_LAYER_POPUP;
263 ilm_displaySetRenderOrder(0, renderOrder, WINDOWMANAGER_LAYER_NUM);
271 void WindowManager::notificationFunc_non_static(ilmObjectType object,
275 qDebug("-=[notificationFunc_non_static]=-");
276 qDebug("Notification from weston!");
277 if (ILM_SURFACE == object)
279 struct ilmSurfaceProperties surfaceProperties;
283 qDebug("Surface created, ID: %d", id);
284 ilm_getPropertiesOfSurface(id, &surfaceProperties);
285 qDebug(" origSourceWidth : %d", surfaceProperties.origSourceWidth);
286 qDebug(" origSourceHeight: %d", surfaceProperties.origSourceHeight);
288 if (WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID == id)
290 qDebug("HomeScreen app detected");
291 addSurfaceToLayer(id, WINDOWMANAGER_LAYER_HOMESCREEN);
296 addSurfaceToLayer(id, WINDOWMANAGER_LAYER_APPLICATIONS);
298 m_surfaces.append(id);
300 ilm_surfaceAddNotification(id, surfaceCallbackFunction_static);
306 qDebug("Surface destroyed, ID: %d", id);
307 m_surfaces.removeAt(m_surfaces.indexOf(id));
308 ilm_surfaceRemoveNotification(id);
313 if (ILM_LAYER == object)
315 //qDebug("Layer.. we don't care...");
319 void WindowManager::notificationFunc_static(ilmObjectType object,
324 static_cast<WindowManager*>(WindowManager::myThis)->notificationFunc_non_static(object, id, created);
327 void WindowManager::surfaceCallbackFunction_non_static(t_ilm_surface surface,
328 struct ilmSurfaceProperties* surfaceProperties,
329 t_ilm_notification_mask mask)
331 qDebug("-=[surfaceCallbackFunction_non_static]=-");
332 qDebug("surfaceCallbackFunction_non_static changes for surface %d", surface);
333 if (ILM_NOTIFICATION_VISIBILITY & mask)
335 qDebug("ILM_NOTIFICATION_VISIBILITY");
337 if (ILM_NOTIFICATION_OPACITY & mask)
339 qDebug("ILM_NOTIFICATION_OPACITY");
341 if (ILM_NOTIFICATION_ORIENTATION & mask)
343 qDebug("ILM_NOTIFICATION_ORIENTATION");
345 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
347 qDebug("ILM_NOTIFICATION_SOURCE_RECT");
349 if (ILM_NOTIFICATION_DEST_RECT & mask)
351 qDebug("ILM_NOTIFICATION_DEST_RECT");
353 if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
355 qDebug("ILM_NOTIFICATION_CONTENT_AVAILABLE");
357 if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
359 qDebug("ILM_NOTIFICATION_CONTENT_REMOVED");
361 if (ILM_NOTIFICATION_CONFIGURED & mask)
363 qDebug("ILM_NOTIFICATION_CONFIGURED");
368 void WindowManager::surfaceCallbackFunction_static(t_ilm_surface surface,
369 struct ilmSurfaceProperties* surfaceProperties,
370 t_ilm_notification_mask mask)
373 static_cast<WindowManager*>(WindowManager::myThis)->surfaceCallbackFunction_non_static(surface, surfaceProperties, mask);
377 int WindowManager::layoutId() const
379 return m_currentLayout;
382 QString WindowManager::layoutName() const
384 QList<Layout>::const_iterator i = m_layouts.begin();
386 QString result = "not found";
387 while (i != m_layouts.constEnd())
389 if (i->id == m_currentLayout)
401 int WindowManager::addLayout(int layoutId, const QString &layoutName, const QList<LayoutArea> &surfaceAreas)
403 qDebug("-=[addLayout]=-");
404 m_layouts.append(Layout(layoutId, layoutName, surfaceAreas));
406 qDebug("addLayout %d %s, size %d",
408 layoutName.toStdString().c_str(),
409 surfaceAreas.size());
413 return WINDOWMANAGER_NO_ERROR;
416 QList<Layout> WindowManager::getAllLayouts()
418 qDebug("-=[getAllLayouts]=-");
423 QList<int> WindowManager::getAllSurfacesOfProcess(int pid)
427 struct ilmSurfaceProperties surfaceProperties;
429 for (int i = 0; i < m_surfaces.size(); ++i)
431 ilm_getPropertiesOfSurface(m_surfaces.at(i), &surfaceProperties);
432 if (pid == surfaceProperties.creatorPid)
434 result.append(m_surfaces.at(i));
441 QList<int> WindowManager::getAvailableLayouts(int numberOfAppSurfaces)
443 qDebug("-=[getAvailableLayouts]=-");
444 QList<Layout>::const_iterator i = m_layouts.begin();
447 while (i != m_layouts.constEnd())
449 if (i->layoutAreas.size() == numberOfAppSurfaces)
451 result.append(i->id);
460 QList<int> WindowManager::getAvailableSurfaces()
462 qDebug("-=[getAvailableSurfaces]=-");
467 QString WindowManager::getLayoutName(int layoutId)
469 qDebug("-=[getLayoutName]=-");
470 QList<Layout>::const_iterator i = m_layouts.begin();
472 QString result = "not found";
473 while (i != m_layouts.constEnd())
475 if (i->id == layoutId)
487 int WindowManager::setLayoutById(int layoutId)
489 qDebug("-=[setLayoutById]=-");
490 int result = WINDOWMANAGER_NO_ERROR;
491 m_currentLayout = layoutId;
493 mp_layoutAreaToSurfaceIdAssignment->clear();
500 int WindowManager::setLayoutByName(const QString &layoutName)
502 qDebug("-=[setLayoutByName]=-");
503 int result = WINDOWMANAGER_NO_ERROR;
505 QList<Layout>::const_iterator i = m_layouts.begin();
507 while (i != m_layouts.constEnd())
509 if (i->name == layoutName)
511 m_currentLayout = i->id;
513 mp_layoutAreaToSurfaceIdAssignment->clear();
524 int WindowManager::setSurfaceToLayoutArea(int surfaceId, int layoutAreaId)
526 qDebug("-=[setSurfaceToLayoutArea]=-");
527 int result = WINDOWMANAGER_NO_ERROR;
529 qDebug("surfaceId %d", surfaceId);
530 qDebug("layoutAreaId %d", layoutAreaId);
531 mp_layoutAreaToSurfaceIdAssignment->insert(layoutAreaId, surfaceId);