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>;
67 qDebug("ilm_init = %d", err);
70 err = ilm_registerNotification(WindowManager::notificationFunc_static, this);
72 createNewLayer(WINDOWMANAGER_LAYER_POPUP);
73 createNewLayer(WINDOWMANAGER_LAYER_APPLICATIONS);
74 createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN);
78 WindowManager::~WindowManager()
80 qDebug("-=[~WindowManager]=-");
81 delete mp_windowManagerAdaptor;
85 delete mp_layoutAreaToSurfaceIdAssignment;
88 void WindowManager::dumpScene()
91 qDebug("current layout : %d", m_currentLayout);
92 qDebug("available layouts: %d", m_layouts.size());
93 QList<Layout>::const_iterator i = m_layouts.begin();
95 while (i != m_layouts.constEnd())
97 qDebug("--[id: %d]--[%s]--", i->id, i->name.toStdString().c_str());
98 qDebug(" %d surface areas", i->layoutAreas.size());
99 for (int j = 0; j < i->layoutAreas.size(); ++j)
101 qDebug(" -area %d", j);
102 qDebug(" -x : %d", i->layoutAreas.at(j).x);
103 qDebug(" -y : %d", i->layoutAreas.at(j).y);
104 qDebug(" -width : %d", i->layoutAreas.at(j).width);
105 qDebug(" -height: %d", i->layoutAreas.at(j).height);
114 void WindowManager::createNewLayer(int layerId)
116 qDebug("-=[createNewLayer]=-");
117 qDebug("layerId %d", layerId);
119 t_ilm_uint screenID = 0;
123 ilm_getScreenResolution(screenID, &width, &height);
125 t_ilm_layer newLayerId = layerId;
126 ilm_layerCreateWithDimension(&newLayerId, width, height);
127 ilm_layerSetOpacity(newLayerId, 1.0);
128 ilm_layerSetVisibility(newLayerId, ILM_TRUE);
129 ilm_layerSetSourceRectangle(newLayerId,
134 ilm_layerSetDestinationRectangle(newLayerId,
143 void WindowManager::addSurfaceToLayer(int surfaceId, int layerId)
145 qDebug("-=[addSurfaceToLayer]=-");
146 qDebug("surfaceId %d", surfaceId);
147 qDebug("layerId %d", layerId);
149 if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN)
151 struct ilmSurfaceProperties surfaceProperties;
152 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
154 qDebug("sourceX %d", surfaceProperties.sourceX);
155 qDebug("sourceY %d", surfaceProperties.sourceY);
156 qDebug("sourceWidth %d", surfaceProperties.sourceWidth);
157 qDebug("sourceHeight %d", surfaceProperties.sourceHeight);
159 // homescreen app always fullscreen in the back
160 t_ilm_uint screenID = 0;
164 ilm_getScreenResolution(screenID, &width, &height);
166 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, width, height);
167 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, width, height);
168 ilm_surfaceSetOpacity(surfaceId, 1.0);
169 ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);
171 ilm_layerAddSurface(layerId, surfaceId);
174 if (layerId == WINDOWMANAGER_LAYER_APPLICATIONS)
176 struct ilmSurfaceProperties surfaceProperties;
177 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
179 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
180 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
181 ilm_surfaceSetOpacity(surfaceId, 0.0);
182 ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
184 ilm_layerAddSurface(layerId, surfaceId);
187 if (layerId == WINDOWMANAGER_LAYER_POPUP)
189 struct ilmSurfaceProperties surfaceProperties;
190 ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
192 ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
193 ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
194 ilm_surfaceSetOpacity(surfaceId, 0.0);
195 ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
197 ilm_layerAddSurface(layerId, surfaceId);
205 void WindowManager::updateScreen()
207 qDebug("-=[updateScreen]=-");
210 if (-1 != m_currentLayout)
214 for (int i = 0; i < m_surfaces.size(); ++i)
216 ilm_surfaceSetVisibility(m_surfaces.at(i), ILM_FALSE);
217 ilm_surfaceSetOpacity(m_surfaces.at(i), 0.0);
220 // find the current used layout
221 QList<Layout>::const_iterator ci = m_layouts.begin();
223 Layout currentLayout;
224 while (ci != m_layouts.constEnd())
226 if (ci->id == m_currentLayout)
234 qDebug("show %d apps", mp_layoutAreaToSurfaceIdAssignment->size());
235 for (int j = 0; j < mp_layoutAreaToSurfaceIdAssignment->size(); ++j)
237 int surfaceToShow = mp_layoutAreaToSurfaceIdAssignment->find(j).value();
238 qDebug(" surface no. %d: %d", j, surfaceToShow);
240 ilm_surfaceSetVisibility(surfaceToShow, ILM_TRUE);
241 ilm_surfaceSetOpacity(surfaceToShow, 1.0);
243 qDebug(" layout area %d", j);
244 qDebug(" x: %d", currentLayout.layoutAreas[j].x);
245 qDebug(" y: %d", currentLayout.layoutAreas[j].y);
246 qDebug(" w: %d", currentLayout.layoutAreas[j].width);
247 qDebug(" h: %d", currentLayout.layoutAreas[j].height);
249 ilm_surfaceSetDestinationRectangle(surfaceToShow,
250 currentLayout.layoutAreas[j].x,
251 currentLayout.layoutAreas[j].y,
252 currentLayout.layoutAreas[j].width,
253 currentLayout.layoutAreas[j].height);
259 t_ilm_layer renderOrder[WINDOWMANAGER_LAYER_NUM];
260 renderOrder[0] = WINDOWMANAGER_LAYER_HOMESCREEN;
261 renderOrder[1] = WINDOWMANAGER_LAYER_APPLICATIONS;
262 renderOrder[2] = WINDOWMANAGER_LAYER_POPUP;
264 ilm_displaySetRenderOrder(0, renderOrder, WINDOWMANAGER_LAYER_NUM);
272 void WindowManager::notificationFunc_non_static(ilmObjectType object,
276 qDebug("-=[notificationFunc_non_static]=-");
277 qDebug("Notification from weston!");
278 if (ILM_SURFACE == object)
280 struct ilmSurfaceProperties surfaceProperties;
284 qDebug("Surface created, ID: %d", id);
285 ilm_getPropertiesOfSurface(id, &surfaceProperties);
286 qDebug(" origSourceWidth : %d", surfaceProperties.origSourceWidth);
287 qDebug(" origSourceHeight: %d", surfaceProperties.origSourceHeight);
289 if (WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID == id)
291 qDebug("HomeScreen app detected");
292 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");
338 if (ILM_NOTIFICATION_OPACITY & mask)
340 qDebug("ILM_NOTIFICATION_OPACITY");
342 if (ILM_NOTIFICATION_ORIENTATION & mask)
344 qDebug("ILM_NOTIFICATION_ORIENTATION");
346 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
348 qDebug("ILM_NOTIFICATION_SOURCE_RECT");
350 if (ILM_NOTIFICATION_DEST_RECT & mask)
352 qDebug("ILM_NOTIFICATION_DEST_RECT");
354 if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
356 qDebug("ILM_NOTIFICATION_CONTENT_AVAILABLE");
359 if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
361 qDebug("ILM_NOTIFICATION_CONTENT_REMOVED");
363 if (ILM_NOTIFICATION_CONFIGURED & mask)
365 qDebug("ILM_NOTIFICATION_CONFIGURED");
370 void WindowManager::surfaceCallbackFunction_static(t_ilm_surface surface,
371 struct ilmSurfaceProperties* surfaceProperties,
372 t_ilm_notification_mask mask)
375 static_cast<WindowManager*>(WindowManager::myThis)->surfaceCallbackFunction_non_static(surface, surfaceProperties, mask);
379 int WindowManager::layoutId() const
381 return m_currentLayout;
384 QString WindowManager::layoutName() const
386 QList<Layout>::const_iterator i = m_layouts.begin();
388 QString result = "not found";
389 while (i != m_layouts.constEnd())
391 if (i->id == m_currentLayout)
403 int WindowManager::addLayout(int layoutId, const QString &layoutName, const QList<LayoutArea> &surfaceAreas)
405 qDebug("-=[addLayout]=-");
406 m_layouts.append(Layout(layoutId, layoutName, surfaceAreas));
408 qDebug("addLayout %d %s, size %d",
410 layoutName.toStdString().c_str(),
411 surfaceAreas.size());
415 return WINDOWMANAGER_NO_ERROR;
418 int WindowManager::deleteLayoutById(int layoutId)
420 qDebug("-=[deleteLayoutById]=-");
421 qDebug("layoutId: %d", layoutId);
422 int result = WINDOWMANAGER_NO_ERROR;
424 if (m_currentLayout == layoutId)
426 result = WINDOWMANAGER_ERROR_ID_IN_USE;
430 QList<Layout>::iterator i = m_layouts.begin();
431 result = WINDOWMANAGER_ERROR_ID_IN_USE;
432 while (i != m_layouts.constEnd())
434 if (i->id == layoutId)
437 result = WINDOWMANAGER_NO_ERROR;
449 QList<Layout> WindowManager::getAllLayouts()
451 qDebug("-=[getAllLayouts]=-");
456 QList<int> WindowManager::getAllSurfacesOfProcess(int pid)
460 struct ilmSurfaceProperties surfaceProperties;
462 for (int i = 0; i < m_surfaces.size(); ++i)
464 ilm_getPropertiesOfSurface(m_surfaces.at(i), &surfaceProperties);
465 if (pid == surfaceProperties.creatorPid)
467 result.append(m_surfaces.at(i));
474 QList<int> WindowManager::getAvailableLayouts(int numberOfAppSurfaces)
476 qDebug("-=[getAvailableLayouts]=-");
477 QList<Layout>::const_iterator i = m_layouts.begin();
480 while (i != m_layouts.constEnd())
482 if (i->layoutAreas.size() == numberOfAppSurfaces)
484 result.append(i->id);
493 QList<int> WindowManager::getAvailableSurfaces()
495 qDebug("-=[getAvailableSurfaces]=-");
500 QString WindowManager::getLayoutName(int layoutId)
502 qDebug("-=[getLayoutName]=-");
503 QList<Layout>::const_iterator i = m_layouts.begin();
505 QString result = "not found";
506 while (i != m_layouts.constEnd())
508 if (i->id == layoutId)
519 void WindowManager::hideLayer(int layer)
521 qDebug("-=[hideLayer]=-");
522 qDebug("layer %d", layer);
527 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_POPUP, ILM_FALSE);
531 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_APPLICATIONS, ILM_FALSE);
535 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN, ILM_FALSE);
541 int WindowManager::setLayoutById(int layoutId)
543 qDebug("-=[setLayoutById]=-");
544 int result = WINDOWMANAGER_NO_ERROR;
545 m_currentLayout = layoutId;
547 mp_layoutAreaToSurfaceIdAssignment->clear();
554 int WindowManager::setLayoutByName(const QString &layoutName)
556 qDebug("-=[setLayoutByName]=-");
557 int result = WINDOWMANAGER_NO_ERROR;
559 QList<Layout>::const_iterator i = m_layouts.begin();
561 while (i != m_layouts.constEnd())
563 if (i->name == layoutName)
565 m_currentLayout = i->id;
567 mp_layoutAreaToSurfaceIdAssignment->clear();
578 int WindowManager::setSurfaceToLayoutArea(int surfaceId, int layoutAreaId)
580 qDebug("-=[setSurfaceToLayoutArea]=-");
581 int result = WINDOWMANAGER_NO_ERROR;
583 qDebug("surfaceId %d", surfaceId);
584 qDebug("layoutAreaId %d", layoutAreaId);
585 mp_layoutAreaToSurfaceIdAssignment->insert(layoutAreaId, surfaceId);
594 void WindowManager::showLayer(int layer)
596 qDebug("-=[showLayer]=-");
597 qDebug("layer %d", layer);
602 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_POPUP, ILM_TRUE);
606 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_APPLICATIONS, ILM_TRUE);
610 ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN, ILM_TRUE);