summaryrefslogtreecommitdiffstats
path: root/windowmanager/src/windowmanager.cpp
blob: 588f7d477551b61c194b6ff2424ab1ff6b31e009 (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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
/*
 * Copyright (C) 2016, 2017 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"


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

// three layers will be defined. The HomeScreen will be placed
// full screen in the background.
// On top all applications in one layer.
// On top of that, the popup layer.
#define WINDOWMANAGER_LAYER_POPUP 100
#define WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY 101
#define WINDOWMANAGER_LAYER_APPLICATIONS 102
#define WINDOWMANAGER_LAYER_HOMESCREEN 103

#define WINDOWMANAGER_LAYER_NUM 4

#define WINDOWMANAGER_SURFACE_ID_SHIFT 22

// the HomeScreen app has to have the surface id 4194304
#define WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID (1 << WINDOWMANAGER_SURFACE_ID_SHIFT)

// Quick hack for scaling layer to fit non-FHD(1920x1080) screen
//  * source rect of layer should be 1920x1080
//  * destination rect of layer should fit physical display resolution
//  * source rect of surface shoud be based on 1920x1080
//  * destination rect of surface should be based on 1920x1080
#define WINDOWMANAGER_HOMESCREEN_WIDTH  1080
#define WINDOWMANAGER_HOMESCREEN_HEIGHT 1920

void* WindowManager::myThis = 0;

WindowManager::WindowManager(int displayId, QObject *parent) :
    QObject(parent),
    m_layouts(),
    mp_layoutAreaToSurfaceIdAssignment(0),
    m_currentLayout(-1),
    m_screenId(displayId),
    m_screen()
#ifdef HAVE_IVI_LAYERMANAGEMENT_API
    ,
    m_layerSrc(0, 0, WINDOWMANAGER_HOMESCREEN_WIDTH, WINDOWMANAGER_HOMESCREEN_HEIGHT),
    m_layerDest(),
    m_appSurfaces(),
    m_appLayers(),
#if !defined(NO_PROCESS_GROUP) || !defined(NO_PROCESS_SESSION)
    m_appPid2surfPid(),
#endif
    m_keepApps(),
    m_bgApps(),
    m_pending_to_show(-1)
#endif
{
#ifdef HAVE_IVI_LAYERMANAGEMENT_API
    m_showLayers = new t_ilm_layer[WINDOWMANAGER_LAYER_NUM];

    m_showLayers[0] = 0; /* POPUP is not shown by default */
    m_showLayers[1] = 0; /* HOMESCREEN_OVERLAY is not shown by default */
    m_showLayers[2] = 0; /* APPLICATIONS is not shown by default */
    m_showLayers[3] = WINDOWMANAGER_LAYER_HOMESCREEN; /* HOMESCREEN is shwon by default */

    m_keepApps.append(QString("mediaplayer@"));
    m_keepApps.append(QString("navigation@"));
#endif
    qDebug("-=[WindowManager]=-");
}

void WindowManager::start()
{
    qDebug("-=[start]=-");
    mp_layoutAreaToSurfaceIdAssignment = new QMap<int, unsigned int>;
#ifdef HAVE_IVI_LAYERMANAGEMENT_API
    ilmErrorTypes err;

    err = ilm_init();
    qDebug("ilm_init = %d", err);
    if(ILM_SUCCESS != err)
    {
        qDebug("failed! Exiting!");
        exit(-1);
    }

    myThis = this;

    t_ilm_uint width, height;
    ilm_getScreenResolution(m_screenId, &width, &height);
    m_screen.setWidth(width);
    m_screen.setHeight(height);

    const char *mode = secure_getenv("CONTENT_MODE");
    if (!mode) {
        qDebug("CONTENT_MODE not specified.  Falling back to aspect-fit");
        mode = "aspect-fit";
    }

    setRootGeometry(mode);

    createNewLayer(WINDOWMANAGER_LAYER_POPUP);
    createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY);
    createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN);

    ilm_registerNotification(WindowManager::notificationFunc_static, this);
#endif

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

    // publish windowmanager interface
    mp_windowManagerAdaptor = new WindowmanagerAdaptor((QObject*)this);
}

WindowManager::~WindowManager()
{
    qDebug("-=[~WindowManager]=-");
    delete mp_windowManagerAdaptor;
#ifdef HAVE_IVI_LAYERMANAGEMENT_API
    ilm_destroy();
#endif
    delete mp_layoutAreaToSurfaceIdAssignment;
}

#ifdef HAVE_IVI_LAYERMANAGEMENT_API
t_ilm_layer* WindowManager::getLayerRenderOrder(int& num_layers)
{
    int i, j, n_bg = m_bgApps.size();

    t_ilm_layer* id_array = new t_ilm_layer[n_bg + WINDOWMANAGER_LAYER_NUM];

    qDebug("Layer render order (ivi-layer-id), %d bgApps: ", n_bg);

    num_layers = 0;
    for (i = 0; i < n_bg; i++) {
        if (m_bgApps[i] != 0) {
            QMap<pid_t, t_ilm_layer>::const_iterator i_layers;
            QMap<pid_t, pid_t>::const_iterator i_surf_pids;

            i_surf_pids = m_appPid2surfPid.find(m_bgApps[i]);
            if (i_surf_pids != m_appPid2surfPid.end()) {
                i_layers = m_appLayers.find(i_surf_pids.value());
                /* m_showLayers[2] means layer for apps */
                if (i_layers != m_appLayers.end() && i_layers.value() != 0
                    && i_layers.value() != m_showLayers[2]) {
                    qDebug("  m_bgApps[%d]=%d", i, i_layers.value());
                    id_array[num_layers++] = i_layers.value();
                }
            }
        }
    }

    for (i = WINDOWMANAGER_LAYER_NUM - 1, j = 0; i >= 0; i--,j++) {
        if (m_showLayers[i] != 0) {
            qDebug("  m_showLayers[%d]=%d", i, m_showLayers[i]);
            id_array[num_layers++] = m_showLayers[i];
        }
    }

    return id_array;
}

void WindowManager::renderLayers()
{
    int num_layers;
    t_ilm_layer* renderOrder = getLayerRenderOrder(num_layers);

    qDebug("Screen render order %d, %d layers", m_screenId, num_layers);
    ilm_displaySetRenderOrder(m_screenId, renderOrder, num_layers);
    ilm_commitChanges();
}
#endif

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

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

        ++i;
    }
}

#ifdef HAVE_IVI_LAYERMANAGEMENT_API

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

    t_ilm_layer newLayerId = layerId;

    // Currently the dimension of layer for apps inherits the root layer's.
    ilm_layerCreateWithDimension(&newLayerId,
                                 m_layerSrc.width(),
                                 m_layerSrc.height());
    ilm_commitChanges();
    ilm_layerSetSourceRectangle(newLayerId,
                                m_layerSrc.x(),
                                m_layerSrc.y(),
                                m_layerSrc.width(),
                                m_layerSrc.height());

    ilm_layerSetDestinationRectangle(newLayerId,
                                     m_layerDest.x(),
                                     m_layerDest.y(),
                                     m_layerDest.width(),
                                     m_layerDest.height());
    ilm_commitChanges();
    ilm_layerSetOpacity(newLayerId, 1.0);
    ilm_layerSetVisibility(newLayerId, ILM_TRUE);
    ilm_commitChanges();
}


void WindowManager::setRootGeometry(const char *mode)
{
    // To calculate layer management parameters

    // Calcurate parameters based on the content mode id given
    // May return false if parameter check, etc. fails (currently not done).

    if (!strcmp("scale-to-fill", mode)) {
        // scale, ignore aspect ratio
        m_layerDest.setRect(0, 0, m_screen.width(), m_screen.height());
    }
    else if (!strcmp("aspect-fit", mode) || !strcmp("aspect-fill", mode)) {
        // scale, keeping aspect ratio
        QSize size = m_layerSrc.size();

        if (!strcmp("aspect-fit", mode)) {
            size.scale(m_screen, Qt::KeepAspectRatio);
        } else {
            size.scale(m_screen, Qt::KeepAspectRatioByExpanding);
        }

        m_layerDest.setSize(size);
        m_layerDest.moveCenter(QPoint(m_screen.width() / 2, m_screen.height() / 2));
    }
    else {
        // other modes without scaling
        m_layerDest.setSize(m_layerSrc.size());

        // At first, move to center, then move to proper position
        m_layerDest.moveCenter(QPoint(m_screen.width() / 2, m_screen.height() / 2));

        if (!strcmp("center", mode)) {
            ;
        }
        else if (!strcmp("top", mode)) {
            m_layerDest.moveTop(0);
        }
        else if (!strcmp("bottom", mode)) {
            m_layerDest.moveBottom(m_screen.height());
        }
        else if (!strcmp("left", mode)) {
            m_layerDest.moveLeft(0);
        }
        else if (!strcmp("right", mode)) {
            m_layerDest.moveRight(m_screen.width());
        }
        else if (!strcmp("top-left", mode)) {
            m_layerDest.moveTo(0,0);
        }
        else if (!strcmp("top-right", mode)) {
            m_layerDest.moveTopRight(QPoint(m_screen.width(), 0));
        }
        else if (!strcmp("bottom-left", mode)) {
            m_layerDest.moveBottomLeft(QPoint(0, m_screen.height()));
        }
        else if (!strcmp("bottom-right", mode)) {
            m_layerDest.moveBottomRight(QPoint(m_screen.width(), m_screen.height()));
        }
        else {
            // fallback
            qDebug("Mode \"%s\" not known, fallingback to aspect-fit", mode);
            setRootGeometry("aspect-fit");
            return;
        }
    }

    qDebug("layer src=%dx%d dest=(%d,%d) %dx%d",
           m_layerSrc.width(), m_layerSrc.height(),
           m_layerDest.x(), m_layerDest.y(), m_layerDest.width(), m_layerDest.height());
}

t_ilm_layer WindowManager::getAppLayerID(pid_t pid)
{
    t_ilm_layer layer_id;

//    layer_id = pid + (WINDOWMANAGER_LAYER_APPLICATIONS << WINDOWMANAGER_LAYER_ID_SHIFT);
    layer_id = pid + (WINDOWMANAGER_LAYER_APPLICATIONS * 100000); /* for debug */

    return layer_id;
}


void WindowManager::addSurface(t_ilm_surface surfaceId)
{
    struct ilmSurfaceProperties surfaceProperties;
    pid_t surf_pid;

    ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
    surf_pid = surfaceProperties.creatorPid;

    QMap<pid_t, t_ilm_surface>::const_iterator i = m_appSurfaces.find(surf_pid);
    if (i == m_appSurfaces.end() || i.value() == 0) {
        /* Only the 1st surface is handled by Window Manager */
        qDebug("This surface (%d) is 1st one for app (%d)", surfaceId, surf_pid);
        /* update surface id */
        m_appSurfaces.insert(surf_pid, surfaceId);

#if !defined(NO_PROCESS_GROUP) || !defined(NO_PROCESS_SESSION)
        registerAppPid2surfPid(surf_pid);
#endif
        /* this surface should be handled by WindowManager */
        ilm_surfaceAddNotification(surfaceId, surfaceCallbackFunction_static);
        ilm_commitChanges();
    }
}

t_ilm_layer WindowManager::addSurfaceToAppLayer(pid_t pid, int surfaceId)
{
    t_ilm_layer layer_id;

    qDebug("-=[addSurfaceToAppLayer]=-");
    qDebug("  surfaceId %d", surfaceId);

    if (pid < 0)
        return 0;

    QMap<pid_t, t_ilm_layer>::const_iterator i = m_appLayers.find(pid);
    if (i == m_appLayers.end()) {
        qDebug("No layer found, create new for app(pid=%d)", pid);

        /* not found, create new one */
        layer_id = getAppLayerID(pid);

        createNewLayer(layer_id);
        m_appLayers.insert(pid, layer_id);
    } else {
        layer_id = i.value();
    }

    return layer_id;
}

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

    if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN)
    {
      ilm_layerAddSurface(layerId, surfaceId);
    }
    else if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY)
    {
        struct ilmSurfaceProperties surfaceProperties;
        ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);

        //ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
        //ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
        //ilm_surfaceSetOpacity(surfaceId, 0.5);
        //ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);

        ilm_layerAddSurface(layerId, surfaceId);
    }
    else if (layerId == WINDOWMANAGER_LAYER_POPUP)
    {
        struct ilmSurfaceProperties surfaceProperties;
        ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);

        //ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
        //ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
        //ilm_surfaceSetOpacity(surfaceId, 0.0);
        //ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);

        ilm_layerAddSurface(layerId, surfaceId);
    } else {
        return;
    }

    ilm_commitChanges();
}

void WindowManager::configureHomeScreenMainSurface(t_ilm_surface surface, t_ilm_uint width, t_ilm_uint height)
{
    // homescreen app always fullscreen in the back
    ilm_surfaceSetDestinationRectangle(surface, 0, 0,
                                       WINDOWMANAGER_HOMESCREEN_WIDTH,
                                       WINDOWMANAGER_HOMESCREEN_HEIGHT);
    ilm_surfaceSetSourceRectangle(surface, 0, 0, width, height);
    ilm_surfaceSetOpacity(surface, 1.0);
    ilm_surfaceSetVisibility(surface, ILM_TRUE);

    ilm_commitChanges();
}

void WindowManager::configureAppSurface(t_ilm_surface surface, t_ilm_uint width, t_ilm_uint height)
{
    /* Dirty hack! cut & paste from HomeScreen/src/layouthandler.cpp */
    const int SCREEN_WIDTH = 1080;
    const int SCREEN_HEIGHT = 1920;

    const int TOPAREA_HEIGHT = 218;
    const int TOPAREA_WIDTH = SCREEN_WIDTH;
    const int TOPAREA_X = 0;
    const int TOPAREA_Y = 0;
    const int MEDIAAREA_HEIGHT = 215;
    const int MEDIAAREA_Y = SCREEN_HEIGHT - MEDIAAREA_HEIGHT;

    const t_ilm_int APPAREA_X = TOPAREA_X;
    const t_ilm_int APPAREA_Y = TOPAREA_Y + TOPAREA_HEIGHT;
    const t_ilm_uint APPAREA_WIDTH = TOPAREA_WIDTH;
    const t_ilm_uint APPAREA_HEIGHT = MEDIAAREA_Y - APPAREA_Y;

    ilm_surfaceSetDestinationRectangle(surface, APPAREA_X, APPAREA_Y,
                                       APPAREA_WIDTH, APPAREA_HEIGHT);
    ilm_surfaceSetSourceRectangle(surface, 0, 0, width, height);
    ilm_surfaceSetOpacity(surface, 1.0);
    ilm_surfaceSetVisibility(surface, ILM_TRUE); /* Hack to avoid blank screen when switch apps */

    ilm_commitChanges();
}
#endif

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

#ifdef HAVE_IVI_LAYERMANAGEMENT_API
    if (m_pending_to_show != -1) {
        qDebug("show pending app (%d)", m_pending_to_show);
        showAppLayer(m_pending_to_show);
    } else {
        // display layer render order
        renderLayers();
    }
#endif
}

#ifdef HAVE_IVI_LAYERMANAGEMENT_API
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)
    {
        if (created)
        {
            if (WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID == id)
            {
                ilm_surfaceAddNotification(id, surfaceCallbackFunction_static);
                ilm_commitChanges();
            }
            else
            {
                addSurface(id);
            }
        }
        else
        {
            qDebug("Surface destroyed, ID: %d", id);
#if 0
            m_appSurfaces.removeAt(m_appSurfaces.indexOf(id));
            ilm_surfaceRemoveNotification(id);

            ilm_commitChanges();
#endif
        }
    }
    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*)
{
    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)
{
    pid_t pid = surfaceProperties->creatorPid;

    qDebug("-=[surfaceCallbackFunction_non_static]=-");
    qDebug("surfaceCallbackFunction_non_static changes for surface %d", surface);
    if (ILM_NOTIFICATION_VISIBILITY & mask)
    {
        qDebug("ILM_NOTIFICATION_VISIBILITY");
        surfaceVisibilityChanged(surface, surfaceProperties->visibility);
        updateScreen();
    }
    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");
    }
    if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
    {
        qDebug("ILM_NOTIFICATION_CONTENT_AVAILABLE");
    }
    if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
    {
        qDebug("ILM_NOTIFICATION_CONTENT_REMOVED");

        /* application being down */
        m_appLayers.remove(pid);
    }
    if (ILM_NOTIFICATION_CONFIGURED & mask)
    {
        qDebug("ILM_NOTIFICATION_CONFIGURED");
        qDebug("  surfaceProperties %d", surface);
        qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
        qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);

        if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
            addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
            configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
        } else {
            ilmErrorTypes result;
            t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);

            if (layer != 0) {
                configureAppSurface(surface,
                                    surfaceProperties->origSourceWidth,
                                    surfaceProperties->origSourceHeight);

                result = ilm_layerAddSurface(layer, surface);
                if (result != ILM_SUCCESS) {
                    qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
                }
                ilm_commitChanges();
            }
        }
        updateScreen();
    }
}

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::layoutId() const
{
    return m_currentLayout;
}

QString WindowManager::layoutName() const
{
    QList<Layout>::const_iterator i = m_layouts.begin();

    QString result = "not found";
    while (i != m_layouts.constEnd())
    {
        if (i->id == m_currentLayout)
        {
            result = i->name;
        }

        ++i;
    }

    return result;
}


int WindowManager::addLayout(int layoutId, const QString &layoutName, const QList<LayoutArea> &surfaceAreas)
{
    qDebug("-=[addLayout]=-");
    m_layouts.append(Layout(layoutId, layoutName, surfaceAreas));

    qDebug("addLayout %d %s, size %d",
           layoutId,
           layoutName.toStdString().c_str(),
           surfaceAreas.size());

    dumpScene();

    return WINDOWMANAGER_NO_ERROR;
}

int WindowManager::deleteLayoutById(int layoutId)
{
    qDebug("-=[deleteLayoutById]=-");
    qDebug("layoutId: %d", layoutId);
    int result = WINDOWMANAGER_NO_ERROR;

    if (m_currentLayout == layoutId)
    {
        result = WINDOWMANAGER_ERROR_ID_IN_USE;
    }
    else
    {
        QList<Layout>::iterator i = m_layouts.begin();
        result = WINDOWMANAGER_ERROR_ID_IN_USE;
        while (i != m_layouts.constEnd())
        {
            if (i->id == layoutId)
            {
                m_layouts.erase(i);
                result = WINDOWMANAGER_NO_ERROR;
                break;
            }

            ++i;
        }
    }

    return result;
}


QList<Layout> WindowManager::getAllLayouts()
{
    qDebug("-=[getAllLayouts]=-");

    return m_layouts;
}

#if 0
QList<int> WindowManager::getAllSurfacesOfProcess(int pid)
{
    QList<int> result;
#ifdef HAVE_IVI_LAYERMANAGEMENT_API
    struct ilmSurfaceProperties surfaceProperties;

    for (int i = 0; i < m_appSurfaces.size(); ++i)
    {
        ilm_getPropertiesOfSurface(m_appSurfaces.at(i), &surfaceProperties);
        if (pid == surfaceProperties.creatorPid)
        {
            result.append(m_appSurfaces.at(i));
        }
    }
#endif
    return result;
}
#endif

QList<int> WindowManager::getAvailableLayouts(int numberOfAppSurfaces)
{
    qDebug("-=[getAvailableLayouts]=-");
    QList<Layout>::const_iterator i = m_layouts.begin();

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

        ++i;
    }

    return result;
}

#if 0
QList<int> WindowManager::getAvailableSurfaces()
{
    qDebug("-=[getAvailableSurfaces]=-");

    return m_appSurfaces;
}
#endif

QString WindowManager::getLayoutName(int layoutId)
{
    qDebug("-=[getLayoutName]=-");
    QList<Layout>::const_iterator i = m_layouts.begin();

    QString result = "not found";
    while (i != m_layouts.constEnd())
    {
        if (i->id == layoutId)
        {
            result = i->name;
        }

        ++i;
    }

    return result;
}

void WindowManager::hideLayer(int layer)
{
    qDebug("-=[hideLayer]=-");
    qDebug("layer %d", layer);

#ifdef HAVE_IVI_LAYERMANAGEMENT_API
    // POPUP=0, HOMESCREEN_OVERLAY=1, APPS=2, HOMESCREEN=3
    if (layer >= 0 && layer < WINDOWMANAGER_LAYER_NUM) {
        /* hide target layer */
        m_showLayers[layer] = 0;
        if (layer == 2) {
            /* clear pending flag */
            m_pending_to_show = -1;
        }
        renderLayers();
    }
#endif
}

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

    mp_layoutAreaToSurfaceIdAssignment->clear();

    dumpScene();

    return result;
}

int WindowManager::setLayoutByName(const QString &layoutName)
{
    qDebug("-=[setLayoutByName]=-");
    int result = WINDOWMANAGER_NO_ERROR;

    QList<Layout>::const_iterator i = m_layouts.begin();

    while (i != m_layouts.constEnd())
    {
        if (i->name == layoutName)
        {
            m_currentLayout = i->id;

            mp_layoutAreaToSurfaceIdAssignment->clear();

            dumpScene();
        }

        ++i;
    }

    return result;
}

int WindowManager::setSurfaceToLayoutArea(int surfaceId, int layoutAreaId)
{
    qDebug("-=[setSurfaceToLayoutArea]=-");
    int result = WINDOWMANAGER_NO_ERROR;

    qDebug("surfaceId %d", surfaceId);
    qDebug("layoutAreaId %d", layoutAreaId);
    mp_layoutAreaToSurfaceIdAssignment->insert(layoutAreaId, surfaceId);

    updateScreen();

    dumpScene();

    return result;
}

void WindowManager::showLayer(int layer)
{
    qDebug("-=[showLayer]=-");
    qDebug("layer %d", layer);

#ifdef HAVE_IVI_LAYERMANAGEMENT_API
    // POPUP=0, HOMESCREEN_OVERLAY=1, APPS=2, HOMESCREEN=3
    if (layer >= 0 && layer < WINDOWMANAGER_LAYER_NUM) {
        static const int layer_id_array[] = {
            WINDOWMANAGER_LAYER_POPUP,
            WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY,
            WINDOWMANAGER_LAYER_APPLICATIONS,
            WINDOWMANAGER_LAYER_HOMESCREEN,
        };

        m_showLayers[layer] = layer_id_array[layer];

        renderLayers();
    }
#endif
}

void WindowManager::showAppLayer(int app_pid)
{
    qDebug("-=[showAppLayer]=-");
    qDebug("pid %d", app_pid);

    if (app_pid == -1) {
        /* nothing to show */
        return;
    }

#ifdef HAVE_IVI_LAYERMANAGEMENT_API
#if !defined(NO_PROCESS_GROUP) || !defined(NO_PROCESS_SESSION)
    pid_t surf_pid = appPid2surfPid(app_pid);
    if (surf_pid == -1) {
        qDebug("Error: app's pid(%d) failed to convert surface's pid.", app_pid);
        return;
    }
#else
    pid_t surf_pid = app_pid;
#endif

    /* clear pending flag */
    m_pending_to_show = -1;

    /* search layer id for application to show */
    QMap<pid_t, t_ilm_layer>::const_iterator i = m_appLayers.find(surf_pid);

    if (i != m_appLayers.end()) {
        qDebug("Found layer(%d) to show for app(pid=%d)", m_showLayers[2], surf_pid);
        m_showLayers[2] = i.value();
    } else {
        QMap<pid_t, t_ilm_surface>::const_iterator j = m_appSurfaces.find(surf_pid);
        /* check if this app is registered */
        if (j == m_appSurfaces.end()) {
            qDebug("New app %d", surf_pid);
            m_appSurfaces.insert(surf_pid, 0); /* register pid only so far */
        } /* check app is required keep running while background */

        /* Probably app layer hasn't been made yet */
        m_pending_to_show = app_pid;
        /* hide current app once, back to default screen */
        m_showLayers[2] = 0;
        qDebug("No layer to show for app(pid=%d)", surf_pid);
    }
    renderLayers();
#endif
}

void WindowManager::showAppLayer(const QString &app_id, int app_pid)
{
    qDebug("-=[showAppLayer]=-");
    qDebug() << "id=" << app_id << ", pid=" << app_pid;

    if (app_pid == -1) {
        /* nothing to show */
        return;
    }
#ifdef HAVE_IVI_LAYERMANAGEMENT_API
#if !defined(NO_PROCESS_GROUP) || !defined(NO_PROCESS_SESSION)
    pid_t surf_pid = appPid2surfPid(app_pid);
    if (surf_pid == -1) {
        qDebug("NOTE: surface for this app(%d) hasn't created yet.", app_pid);

        checkBackgroundApps(app_id, app_pid);
        m_pending_to_show = app_pid;
        return;
    }
#else
    pid_t surf_pid = app_pid;
#endif
    /* clear pending flag */
    m_pending_to_show = -1;

    /* search layer id for application to show */
    QMap<pid_t, t_ilm_layer>::const_iterator i = m_appLayers.find(surf_pid);

    if (i != m_appLayers.end()) {
        qDebug("Found layer(%d) to show for app(pid=%d)", m_showLayers[2], surf_pid);
        m_showLayers[2] = i.value();
    } else {
        QMap<pid_t, t_ilm_surface>::const_iterator j = m_appSurfaces.find(surf_pid);
        /* check if this app is registered */
        if (j == m_appSurfaces.end()) {
            qDebug("New app %d", surf_pid);
            m_appSurfaces.insert(surf_pid, 0); /* register pid only so far */
        }
        /* check app is required keep running while background */
        checkBackgroundApps(app_id, app_pid);

        /* Probably app layer hasn't been made yet */
        m_pending_to_show = app_pid;
        /* hide current app once, back to default screen */
        m_showLayers[2] = 0;
        qDebug("No layer to show for app(pid=%d)", surf_pid);
    }
    renderLayers();
#endif
}

#ifdef HAVE_IVI_LAYERMANAGEMENT_API
void WindowManager::checkBackgroundApps(const QString &app_id, int app_pid)
{
    /* check if rendering is always required */
    /* QML apps should be always rendered if */
    for (int k = 0; k != m_keepApps.size(); k++) {
        if (app_id.indexOf(m_keepApps[k]) == 0) {
          int idx = m_bgApps.indexOf(app_pid);
          if (idx == -1) {
              m_bgApps.append(app_pid);
          }
        }
    }
}

#if !defined(NO_PROCESS_GROUP) || !defined(NO_PROCESS_SESSION)
bool WindowManager::otherPids(pid_t pid, pid_t &gpid, pid_t &spid)
{
    char buffer[60];
    int rc;
    FILE *f;

    // get the filename
    rc = snprintf(buffer, sizeof buffer, "/proc/%d/stat", (int)pid);
    if (rc < 0 || rc >= (int)(sizeof buffer)) {
        qDebug("internal unexpected error %d", rc);
        return false;
    }
    // open the status file
    f = fopen(buffer, "r");
    if (f == NULL) {
        qDebug("failed to open %s: %m", buffer);
        return false;
    }
    // read and parse the status file
    rc = fscanf(f, "%*d %*s %*c %*d %d %d", &gpid, &spid);
    fclose(f);
    if (rc != 2) {
        qDebug("failed to parse %s: %d", buffer, rc);
        return false;
    }
    qDebug("retrieved for pid=%d gpid=%d spid=%d", (int)pid, (int)gpid, (int)spid);
    return true;
}

void WindowManager::registerAppPid2surfPid(pid_t surf_pid)
{
    /* Register app's and surface's pid to conversion table */
    pid_t gpid, spid;

    if (otherPids(surf_pid, gpid, spid)) {
        QMap<pid_t, pid_t>::const_iterator i;
#if !defined(NO_PROCESS_GROUP)
        i == m_appPid2surfPid.find(gpid);
        if (i != m_appPid2surfPid.end()) {
            m_appPid2surfPid.insert(gpid, surf_pid);
        }
#endif
#if !defined(NO_PROCESS_SESSION)
        i == m_appPid2surfPid.find(spid);
        if (i != m_appPid2surfPid.end()) {
            m_appPid2surfPid.insert(spid, surf_pid);
        }
#endif
    }
}

pid_t WindowManager::appPid2surfPid(pid_t app_pid)
{
    if (app_pid == 0) {
        /* pid = 0 means it is HomeScreen itself */
        return app_pid;
    }

    QMap<pid_t, pid_t>::const_iterator j;
    j = m_appPid2surfPid.find(app_pid);
    if (j != m_appPid2surfPid.end()) {
        /* app pid has already been registered. */
        return j.value();
    }

    return -1;
}
#endif

#endif  // HAVE_IVI_LAYMERMANAGEMENT_API