aboutsummaryrefslogtreecommitdiffstats
path: root/WindowManager/src/windowmanager.cpp
blob: ce59fa02dabf2ccb912fef2dc8ed7096fc49553a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
 * Copyright (C) 2016 Mentor Graphics Development (Deutschland) GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "windowmanager.hpp"
#include <wayland-client.h>
#include <QFile>

//////////////////////////////////////////
// THIS IS STILL UNDER HEAVY DEVELOPMENT!
// DO NOT JUDGE THE SOURCE CODE :)
//////////////////////////////////////////

void* WindowManager::myThis = 0;

WindowManager::WindowManager(QObject *parent) :
    QObject(parent),
    m_layouts(),
    m_layoutNames(),
    m_layoutFullScreen(),
    m_layoutFullScreenAssociated(),
    m_currentLayout(-1),
    m_homeScreenPid(-1),
#ifdef __arm__
    mp_surfaces(0),
    mp_processLayers(0),
#endif
    mp_layoutAreaToPidAssignment(0)
{
    qDebug("-=[WindowManager]=-");
    qDebug("WindowManager");
    // publish windowmanager interface
    mp_windowManagerAdaptor = new WindowmanagerAdaptor((QObject*)this);

    QDBusConnection dbus = QDBusConnection::sessionBus();
    dbus.registerObject("/windowmanager", this);
    dbus.registerService("org.agl.windowmanager");
}

void WindowManager::start()
{
    qDebug("-=[start]=-");
    mp_layoutAreaToPidAssignment = new QMap<int, unsigned int>;
#ifdef __arm__
    mp_processLayers = new QList<int>;
    mp_surfaces = new QMap<t_ilm_uint, SurfaceInfo>;

    ilmErrorTypes err;

    err = ilm_init();
    qDebug("ilm_init = %d", err);

    myThis = this;
    err =  ilm_registerNotification(WindowManager::notificationFunc_static, this);
#endif
}

WindowManager::~WindowManager()
{
    qDebug("-=[~WindowManager]=-");
    delete mp_windowManagerAdaptor;
#ifdef __arm__
    delete mp_surfaces;
    delete mp_processLayers;

    ilm_destroy();
#endif
    delete mp_layoutAreaToPidAssignment;
}

void WindowManager::dumpScene()
{
    qDebug("\n");
    qDebug("current layout   : %d", m_currentLayout);
    qDebug("available layouts: %d", m_layouts.size());
    QMap<int, QList<SimpleRect> >::iterator i = m_layouts.begin();

    QList<int> result;
    while (i != m_layouts.constEnd())
    {
        qDebug("--[id: %d]--[%s]--", i.key(), m_layoutNames.find(i.key()).value().toStdString().c_str());
        qDebug("  %d surface areas", i.value().size());
        for (int j = 0; j < i.value().size(); ++j)
        {
            qDebug("  -area %d", j);
            qDebug("    -x     : %d", i.value().at(j).x);
            qDebug("    -y     : %d", i.value().at(j).y);
            qDebug("    -width : %d", i.value().at(j).width);
            qDebug("    -height: %d", i.value().at(j).height);
        }

        ++i;
    }
}

#ifdef __arm__

void WindowManager::createNewLayer(int layerId)
{
    qDebug("-=[createNewLayer]=-");
    qDebug("layerId %d", layerId);
    ilmErrorTypes err;

    t_ilm_uint screenID = 0;
    t_ilm_uint width;
    t_ilm_uint height;

    err = ilm_getScreenResolution(screenID, &width, &height);

    t_ilm_layer newLayerId = layerId;
    err = ilm_layerCreateWithDimension(&newLayerId, width, height);

    t_ilm_float opacity = 0.0;
    err =  ilm_layerSetOpacity(newLayerId, opacity);

    ilm_layerSetVisibility(newLayerId, ILM_FALSE);

    ilm_commitChanges();
}

void WindowManager::addSurfaceToLayer(int surfaceId, int layerId)
{
    qDebug("-=[addSurfaceToLayer]=-");
    qDebug("surfaceId %d", surfaceId);
    qDebug("layerId %d", layerId);
    t_ilm_int length;
    t_ilm_layer* pArray;

    ilm_getLayerIDs(&length, &pArray);
    bool layerFound(false);
    for (int i = 0; i < length; ++i)
    {
        if (layerId == pArray[i])
        {
            layerFound = true;
        }
    }

    if (!layerFound)
    {
        createNewLayer(layerId);
    }

    struct ilmSurfaceProperties surfaceProperties;
    ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
    //qDebug("  origSourceWidth : %d", surfaceProperties.origSourceWidth);
    //qDebug("  origSourceHeight: %d", surfaceProperties.origSourceHeight);

    ilm_layerSetSourceRectangle(layerId,
                                     0,
                                     0,
                                     surfaceProperties.origSourceWidth,
                                     surfaceProperties.origSourceHeight);
    ilm_commitChanges();

    ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
    ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
    ilm_surfaceSetOpacity(surfaceId, 1.0);
    ilm_surfaceSetVisibility(surfaceId, true);

    ilm_layerAddSurface(layerId, surfaceId);

    ilm_commitChanges();
}

void WindowManager::updateScreen()
{
    qDebug("-=[updateScreen]=-");
    int numberOfLayersToShow = 0;

    QMap<int, QList<SimpleRect> >::iterator i = m_layouts.find(m_currentLayout);
    if (m_layouts.end() != i)
    {
        numberOfLayersToShow += i.value().size();
        qDebug("the current layout provides %d render areas", i.value().size());
    }
    else
    {
        qDebug("the current layout provides no render areas!");
    }


    t_ilm_layer renderOrder[numberOfLayersToShow];
    int renderOrderCounter = 0;

    qDebug("show home screen app");
    if (-1 != m_homeScreenPid)
    {
        renderOrder[renderOrderCounter] = m_homeScreenPid;
        ++renderOrderCounter;

        ilm_layerSetVisibility(m_homeScreenPid, ILM_TRUE);
        t_ilm_float opacity = 1.0;
        ilm_layerSetOpacity(m_homeScreenPid, opacity);

        // homescreen app always fullscreen in the back
        t_ilm_uint screenID = 0;
        t_ilm_uint width;
        t_ilm_uint height;

        ilm_getScreenResolution(screenID, &width, &height);

        ilm_layerSetDestinationRectangle(m_homeScreenPid,
                                         0,
                                         0,
                                         width,
                                         height);
    }


    qDebug("show %d apps", numberOfLayersToShow);
    for (int j = 0; j < numberOfLayersToShow; ++j)
    {
        int layerToShow = mp_layoutAreaToPidAssignment->find(j).value();
        if (layerToShow != m_homeScreenPid)
        {
            qDebug("  app no. %d: %d", j, layerToShow);
            renderOrder[renderOrderCounter] = layerToShow;
            ++renderOrderCounter;

            ilm_layerSetVisibility(layerToShow, ILM_TRUE);
            t_ilm_float opacity = 1.0;
            ilm_layerSetOpacity(layerToShow, opacity);

            qDebug("  layout area %d", j);
            qDebug("    x: %d", m_layouts.find(m_currentLayout).value()[j].x);
            qDebug("    x: %d", m_layouts.find(m_currentLayout).value()[j].y);
            qDebug("    w: %d", m_layouts.find(m_currentLayout).value()[j].width);
            qDebug("    h: %d", m_layouts.find(m_currentLayout).value()[j].height);

            ilm_layerSetDestinationRectangle(layerToShow,
                                             m_layouts.find(m_currentLayout).value()[j].x,
                                             m_layouts.find(m_currentLayout).value()[j].y,
                                             m_layouts.find(m_currentLayout).value()[j].width,
                                             m_layouts.find(m_currentLayout).value()[j].height);
        }
    }

    qDebug("renderOrder");
    for (int j = 0; j < renderOrderCounter; ++j)
    {
        qDebug("  %d: %d", j, renderOrder[j]);
    }

    ilm_displaySetRenderOrder(0, renderOrder, renderOrderCounter);

    ilm_commitChanges();
}

void WindowManager::notificationFunc_non_static(ilmObjectType object,
                                    t_ilm_uint id,
                                    t_ilm_bool created)
{
    qDebug("-=[notificationFunc_non_static]=-");
    qDebug("Notification from weston!");
    if (ILM_SURFACE == object)
    {
        struct ilmSurfaceProperties surfaceProperties;

        if (created)
        {
            qDebug("Surface created, ID: %d", id);
            //qDebug("Surface created, ID: 0x%s", QString(QByteArray::number(id,16)).toStdString().c_str());
            ilm_getPropertiesOfSurface(id, &surfaceProperties);
            qDebug("  origSourceWidth : %d", surfaceProperties.origSourceWidth);
            qDebug("  origSourceHeight: %d", surfaceProperties.origSourceHeight);
            SurfaceInfo surfaceInfo;
            surfaceInfo.pid = surfaceProperties.creatorPid;
            QString procInfoFileName = QString("/proc/") + QString::number(surfaceInfo.pid) + QString("/comm");
            QFile procInfo(procInfoFileName);
            if (procInfo.open(QIODevice::ReadOnly))
            {
                QTextStream in(&procInfo);
                surfaceInfo.processName = in.readLine();
                qDebug("  creator pid %d %s", surfaceInfo.pid, surfaceInfo.processName.toStdString().c_str());
                addSurfaceToLayer(id, surfaceProperties.creatorPid);

                mp_surfaces->insert(id, surfaceInfo);
                ilm_surfaceAddNotification(id, surfaceCallbackFunction_static);

                ilm_commitChanges();

                if (0 == QString::compare("weston-fullscre", surfaceInfo.processName))
                {
                    qDebug("HomeScreen app detected");
                    m_homeScreenPid = surfaceInfo.pid;
                    updateScreen();
                }
            }
            else
            {
                qDebug("no creator pid found. Ignoring surface!");
            }
        }
        else
        {
            qDebug("Surface destroyed, ID: %d", id);
            mp_surfaces->erase(mp_surfaces->find(id));
            ilm_surfaceRemoveNotification(id);

            ilm_commitChanges();
        }
    }
    if (ILM_LAYER == object)
    {
        //qDebug("Layer.. we don't care...");
    }
}

void WindowManager::notificationFunc_static(ilmObjectType object,
                                            t_ilm_uint id,
                                            t_ilm_bool created,
                                            void* user_data)
{
    static_cast<WindowManager*>(WindowManager::myThis)->notificationFunc_non_static(object, id, created);
}




void WindowManager::surfaceCallbackFunction_non_static(t_ilm_surface surface,
                                    struct ilmSurfaceProperties* surfaceProperties,
                                    t_ilm_notification_mask mask)
{
    qDebug("-=[surfaceCallbackFunction_non_static]=-");
    qDebug("surfaceCallbackFunction_non_static changes for surface %d", surface);
    if (ILM_NOTIFICATION_VISIBILITY & mask)
    {
        qDebug("ILM_NOTIFICATION_VISIBILITY");
    }

    if (ILM_NOTIFICATION_OPACITY & mask)
    {
        qDebug("ILM_NOTIFICATION_OPACITY");
    }

    if (ILM_NOTIFICATION_ORIENTATION & mask)
    {
        qDebug("ILM_NOTIFICATION_ORIENTATION");
    }

    if (ILM_NOTIFICATION_SOURCE_RECT & mask)
    {
        qDebug("ILM_NOTIFICATION_SOURCE_RECT");
    }

    if (ILM_NOTIFICATION_DEST_RECT & mask)
    {
        qDebug("ILM_NOTIFICATION_DEST_RECT");
    }
}

void WindowManager::surfaceCallbackFunction_static(t_ilm_surface surface,
                                    struct ilmSurfaceProperties* surfaceProperties,
                                    t_ilm_notification_mask mask)

{
    static_cast<WindowManager*>(WindowManager::myThis)->surfaceCallbackFunction_non_static(surface, surfaceProperties, mask);
}
#endif

int WindowManager::addLayout(int layoutId, const QString &layoutName, bool isFullScreen, int associatedFullScreenLayout, const QList<SimpleRect> &surfaceAreas)
{
    qDebug("-=[addLayout]=-");
    m_layouts.insert(layoutId, surfaceAreas);
    m_layoutNames.insert(layoutId, layoutName);
    m_layoutFullScreen.insert(layoutId, isFullScreen);
    m_layoutFullScreenAssociated.insert(layoutId, associatedFullScreenLayout);
    qDebug("addLayout %d %s %s, %d, size %d",
           layoutId,
           layoutName.toStdString().c_str(),
           isFullScreen ? "true" : "false",
           associatedFullScreenLayout,
           surfaceAreas.size());

    dumpScene();

    return true;
}

int WindowManager::getAssociatedFullScreenLayout(int layoutId)
{
    qDebug("-=[getAssociatedFullScreenLayout]=-");
    return m_layoutFullScreenAssociated.find(layoutId).value();
}

QList<int> WindowManager::getAvailableLayouts(int numberOfAppSurfaces)
{
    qDebug("-=[getAvailableLayouts]=-");
    QMap<int, QList<SimpleRect> >::iterator i = m_layouts.begin();

    QList<int> result;
    while (i != m_layouts.constEnd())
    {
        if (i.value().size() == numberOfAppSurfaces)
        {
            result.append(i.key());
        }

        ++i;
    }

    return result;
}

// maybe not needed anymore
QList<SimplePoint> WindowManager::getAvailableSurfaces()
{
    qDebug("-=[getAvailableSurfaces]=-");
    QList<SimplePoint> points;
    SimplePoint point;
    point.x = 1;
    point.y = 2;
    points.append(point);
    point.x = 11;
    point.y = 22;
    points.append(point);
    point.x = 111;
    point.y = 222;
    points.append(point);

    return points;
}

int WindowManager::getLayout()
{
    qDebug("-=[getLayout]=-");
    return m_currentLayout;
}

QString WindowManager::getLayoutName(int layoutId)
{
    qDebug("-=[getLayoutName]=-");
    return m_layoutNames.find(layoutId).value();
}

bool WindowManager::isLayoutFullScreen(int layoutId)
{
    qDebug("-=[isLayoutFullScreen]=-");
    return m_layoutFullScreen.find(layoutId).value();
}

void WindowManager::setLayoutById(int layoutId)
{
    qDebug("-=[setLayoutById]=-");
    m_currentLayout = layoutId;

    mp_layoutAreaToPidAssignment->clear();

    dumpScene();
}

void WindowManager::setLayoutByName(const QString &layoutName)
{
    qDebug("-=[setLayoutByName]=-");
    QMap<int, QString>::iterator i = m_layoutNames.begin();
    while (i != m_layoutNames.constEnd())
    {
        if (i.value() == layoutName)
        {
            m_currentLayout = i.key();
        }
        ++i;
    }

    mp_layoutAreaToPidAssignment->clear();

    dumpScene();
}

void WindowManager::setPidToLayoutArea(int pid, int layoutAreaId)
{
    qDebug("-=[setPidToLayoutArea]=-");
    qDebug("pid %d", pid);
    qDebug("layoutAreaId %d", layoutAreaId);
    mp_layoutAreaToPidAssignment->insert(layoutAreaId, pid);

#ifdef __arm__
    updateScreen();
#endif

    dumpScene();
}