summaryrefslogtreecommitdiffstats
path: root/src/gui/kernel/qapplication_s60.cpp
blob: 2be3ed3fe075adfd2b04635a3b552b2b531d40c7 (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
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qapplication_p.h"
#include "qsessionmanager.h"
#include "qevent.h"
#include "qsymbianevent.h"
#include "qeventdispatcher_s60_p.h"
#include "qwidget.h"
#include "qdesktopwidget.h"
#include "private/qbackingstore_p.h"
#include "qt_s60_p.h"
#include "private/qevent_p.h"
#include "qstring.h"
#include "qdebug.h"
#include "qimage.h"
#include "qcombobox.h"
#include "private/qkeymapper_p.h"
#include "private/qfont_p.h"
#ifndef QT_NO_STYLE_S60
#include "private/qs60style_p.h"
#endif
#include "private/qwindowsurface_s60_p.h"
#include "qpaintengine.h"
#include "private/qmenubar_p.h"
#include "private/qsoftkeymanager_p.h"
#ifdef QT_GRAPHICSSYSTEM_RUNTIME
#include "private/qgraphicssystem_runtime_p.h"
#endif

#include "apgwgnam.h" // For CApaWindowGroupName
#include <mdaaudiotoneplayer.h>     // For CMdaAudioToneUtility

#if defined(Q_OS_SYMBIAN)
# include <private/qs60mainapplication_p.h>
# include <centralrepository.h>
# include "qs60mainappui.h"
# include "qinputcontext.h"
#endif

#if defined(Q_WS_S60)
# if !defined(QT_NO_IM)
#  include <private/qcoefepinputcontext_p.h>
# endif
#endif

#include "private/qstylesheetstyle_p.h"

#include <hal.h>
#include <hal_data.h>

#ifdef SYMBIAN_GRAPHICS_WSERV_QT_EFFECTS
#include <graphics/wstfxconst.h>
#endif

QT_BEGIN_NAMESPACE

// Goom Events through Window Server
static const int KGoomMemoryLowEvent = 0x10282DBF;
static const int KGoomMemoryGoodEvent = 0x20026790;

#if defined(QT_DEBUG)
static bool        appNoGrab        = false;        // Grabbing enabled
#endif
static bool        app_do_modal        = false;        // modal mode
Q_GLOBAL_STATIC(QS60Data, qt_s60Data);

extern bool qt_sendSpontaneousEvent(QObject*,QEvent*);
extern QWidgetList *qt_modal_stack;              // stack of modal widgets
extern QDesktopWidget *qt_desktopWidget; // qapplication.cpp

QWidget *qt_button_down = 0;                     // widget got last button-down

QSymbianControl *QSymbianControl::lastFocusedControl = 0;

QS60Data* qGlobalS60Data()
{
    return qt_s60Data();
}

#ifdef Q_WS_S60
void QS60Data::setStatusPaneAndButtonGroupVisibility(bool statusPaneVisible, bool buttonGroupVisible)
{
    bool buttonGroupVisibilityChanged = false;
    if (CEikButtonGroupContainer *const b = buttonGroupContainer()) {
        buttonGroupVisibilityChanged = (b->IsVisible() != buttonGroupVisible);
        b->MakeVisible(buttonGroupVisible);
    }
    bool statusPaneVisibilityChanged = false;
    if (CEikStatusPane *const s = statusPane()) {
        statusPaneVisibilityChanged = (s->IsVisible() != statusPaneVisible);
        s->MakeVisible(statusPaneVisible);
    }
    if (buttonGroupVisibilityChanged  || statusPaneVisibilityChanged) {
        const QSize size = qt_TRect2QRect(static_cast<CEikAppUi*>(S60->appUi())->ClientRect()).size();
        const QSize oldSize; // note that QDesktopWidget::resizeEvent ignores the QResizeEvent contents
        QResizeEvent event(size, oldSize);
        QApplication::instance()->sendEvent(QApplication::desktop(), &event);
    }
    if (buttonGroupVisibilityChanged  && !statusPaneVisibilityChanged && QApplication::activeWindow())
        // Ensure that control rectangle is updated
        static_cast<QSymbianControl *>(QApplication::activeWindow()->winId())->handleClientAreaChange();
}
#endif

void QS60Data::controlVisibilityChanged(CCoeControl *control, bool visible)
{
    if (QWidgetPrivate::mapper && QWidgetPrivate::mapper->contains(control)) {
        QWidget *const widget = QWidgetPrivate::mapper->value(control);
        QWidget *const window = widget->window();
        if (QTLWExtra *topData = qt_widget_private(window)->maybeTopData()) {
            QWidgetBackingStoreTracker &backingStore = topData->backingStore;
            if (visible) {
                if (backingStore.data()) {
                    backingStore.registerWidget(widget);
                } else {
#ifdef SYMBIAN_GRAPHICS_WSERV_QT_EFFECTS
                    S60->wsSession().SendEffectCommand(ETfxCmdRestoreLayer);
#endif
                    backingStore.create(window);
                    backingStore.registerWidget(widget);
                    qt_widget_private(widget)->invalidateBuffer(widget->rect());
                    widget->repaint();
                }
            } else {
#ifdef  SYMBIAN_GRAPHICS_WSERV_QT_EFFECTS
                S60->wsSession().SendEffectCommand(ETfxCmdDeallocateLayer);
#endif
                backingStore.unregisterWidget(widget);
                // In order to ensure that any resources used by the window surface
                // are immediately freed, we flush the WSERV command buffer.
                S60->wsSession().Flush();
            }
        }
    }
}

bool qt_nograb()                                // application no-grab option
{
#if defined(QT_DEBUG)
    return appNoGrab;
#else
    return false;
#endif
}

// Modified from http://www3.symbian.com/faq.nsf/0/0F1464EE96E737E780256D5E00503DD1?OpenDocument
class QS60Beep : public CBase, public MMdaAudioToneObserver
{
public:
    static QS60Beep* NewL(TInt aFrequency,  TTimeIntervalMicroSeconds iDuration);
    void Play();
    ~QS60Beep();
private:
    void ConstructL(TInt aFrequency,  TTimeIntervalMicroSeconds iDuration);
    void MatoPrepareComplete(TInt aError);
    void MatoPlayComplete(TInt aError);
private:
    typedef enum
        {
        EBeepNotPrepared,
        EBeepPrepared,
        EBeepPlaying
        } TBeepState;
private:
    CMdaAudioToneUtility* iToneUtil;
    TBeepState iState;
    TInt iFrequency;
    TTimeIntervalMicroSeconds iDuration;
};

static QS60Beep* qt_S60Beep = 0;

QS60Beep::~QS60Beep()
{
    if (iToneUtil) {
        switch (iState) {
        case EBeepPlaying:
            iToneUtil->CancelPlay();
            break;
        case EBeepNotPrepared:
            iToneUtil->CancelPrepare();
            break;
        }
    }
    delete iToneUtil;
}

QS60Beep* QS60Beep::NewL(TInt aFrequency, TTimeIntervalMicroSeconds aDuration)
{
    QS60Beep* self = new (ELeave) QS60Beep();
    CleanupStack::PushL(self);
    self->ConstructL(aFrequency, aDuration);
    CleanupStack::Pop();
    return self;
}

void QS60Beep::ConstructL(TInt aFrequency, TTimeIntervalMicroSeconds aDuration)
{
    iToneUtil = CMdaAudioToneUtility::NewL(*this);
    iState = EBeepNotPrepared;
    iFrequency = aFrequency;
    iDuration = aDuration;
    iToneUtil->PrepareToPlayTone(iFrequency, iDuration);
}

void QS60Beep::Play()
{
    if (iState == EBeepPlaying) {
        iToneUtil->CancelPlay();
        iState = EBeepPrepared;
    }

    iToneUtil->Play();
    iState = EBeepPlaying;
}

void QS60Beep::MatoPrepareComplete(TInt aError)
{
    if (aError == KErrNone) {
        iState = EBeepPrepared;
        Play();
    }
}

void QS60Beep::MatoPlayComplete(TInt aError)
{
    Q_UNUSED(aError);
    iState = EBeepPrepared;
}


static Qt::KeyboardModifiers mapToQtModifiers(TUint s60Modifiers)
{
    Qt::KeyboardModifiers result = Qt::NoModifier;

    if (s60Modifiers & EModifierKeypad)
        result |= Qt::KeypadModifier;
    if (s60Modifiers & EModifierShift || s60Modifiers & EModifierLeftShift
            || s60Modifiers & EModifierRightShift)
        result |= Qt::ShiftModifier;
    if (s60Modifiers & EModifierCtrl || s60Modifiers & EModifierLeftCtrl
            || s60Modifiers & EModifierRightCtrl)
        result |= Qt::ControlModifier;
    if (s60Modifiers & EModifierAlt || s60Modifiers & EModifierLeftAlt
            || s60Modifiers & EModifierRightAlt)
        result |= Qt::AltModifier;

    return result;
}

static void mapS60MouseEventTypeToQt(QEvent::Type *type, Qt::MouseButton *button, const TPointerEvent *pEvent)
{
    switch (pEvent->iType) {
    case TPointerEvent::EButton1Down:
        *type = QEvent::MouseButtonPress;
        *button = Qt::LeftButton;
        break;
    case TPointerEvent::EButton1Up:
        *type = QEvent::MouseButtonRelease;
        *button = Qt::LeftButton;
        break;
    case TPointerEvent::EButton2Down:
        *type = QEvent::MouseButtonPress;
        *button = Qt::MidButton;
        break;
    case TPointerEvent::EButton2Up:
        *type = QEvent::MouseButtonRelease;
        *button = Qt::MidButton;
        break;
    case TPointerEvent::EButton3Down:
        *type = QEvent::MouseButtonPress;
        *button = Qt::RightButton;
        break;
    case TPointerEvent::EButton3Up:
        *type = QEvent::MouseButtonRelease;
        *button = Qt::RightButton;
        break;
    case TPointerEvent::EDrag:
        *type = QEvent::MouseMove;
        *button = Qt::NoButton;
        break;
    case TPointerEvent::EMove:
        // Qt makes no distinction between move and drag
        *type = QEvent::MouseMove;
        *button = Qt::NoButton;
        break;
    default:
        *type = QEvent::None;
        *button = Qt::NoButton;
        break;
    }
    if (pEvent->iModifiers & EModifierDoubleClick){
        *type = QEvent::MouseButtonDblClick;
    }

    if (*type == QEvent::MouseButtonPress || *type == QEvent::MouseButtonDblClick)
        QApplicationPrivate::mouse_buttons = QApplicationPrivate::mouse_buttons | (*button);
    else if (*type == QEvent::MouseButtonRelease)
        QApplicationPrivate::mouse_buttons = QApplicationPrivate::mouse_buttons &(~(*button));

    QApplicationPrivate::mouse_buttons = QApplicationPrivate::mouse_buttons & Qt::MouseButtonMask;
}

//### Can be replaced with CAknLongTapDetector if animation is required.
//NOTE: if CAknLongTapDetector is used make sure it gets variated out of 3.1 and 3.2,.
//also MLongTapObserver needs to be changed to MAknLongTapDetectorCallBack if CAknLongTapDetector is used.
class QLongTapTimer : public CTimer
{
public:
    static QLongTapTimer* NewL(QAbstractLongTapObserver *observer);
    QLongTapTimer(QAbstractLongTapObserver *observer);
    void ConstructL();
public:
    void PointerEventL(const TPointerEvent &event);
    void RunL();
protected:
private:
    QAbstractLongTapObserver *m_observer;
    TPointerEvent m_event;
    QPoint m_pressedCoordinates;
    int m_dragDistance;
};

QLongTapTimer* QLongTapTimer::NewL(QAbstractLongTapObserver *observer)
{
    QLongTapTimer* self = new QLongTapTimer(observer);
    self->ConstructL();
    return self;
}
void QLongTapTimer::ConstructL()
{
    CTimer::ConstructL();
}

QLongTapTimer::QLongTapTimer(QAbstractLongTapObserver *observer):CTimer(CActive::EPriorityHigh)
{
    m_observer = observer;
    m_dragDistance = qApp->startDragDistance();
    CActiveScheduler::Add(this);
}

void QLongTapTimer::PointerEventL(const TPointerEvent& event)
{
    if ( event.iType == TPointerEvent::EDrag || event.iType == TPointerEvent::EButtonRepeat)
    {
        QPoint diff(QPoint(event.iPosition.iX,event.iPosition.iY) - m_pressedCoordinates);
        if (diff.manhattanLength() < m_dragDistance)
            return;
    }
    Cancel();
    m_event = event;
    if (event.iType == TPointerEvent::EButton1Down)
    {
        m_pressedCoordinates = QPoint(event.iPosition.iX,event.iPosition.iY);
        // must be same as KLongTapDelay in aknlongtapdetector.h
        After(800000);
    }
}
void QLongTapTimer::RunL()
{
    if (m_observer)
        m_observer->HandleLongTapEventL(m_event.iPosition, m_event.iParentPosition);
}

QSymbianControl::QSymbianControl(QWidget *w)
    : CCoeControl()
    , qwidget(w)
    , m_longTapDetector(0)
    , m_ignoreFocusChanged(0)
    , m_symbianPopupIsOpen(0)
{
}

void QSymbianControl::ConstructL(bool isWindowOwning, bool desktop)
{
    if (!desktop)
    {
        if (isWindowOwning || !qwidget->parentWidget())
            CreateWindowL(S60->windowGroup());
        else
            /**
             * TODO: in order to avoid creating windows for all ancestors of
             * this widget up to the root window, the parameter passed to
             * CreateWindowL should be
             * qwidget->parentWidget()->effectiveWinId().  However, if we do
             * this, then we need to take care of re-parenting when a window
             * is created for a widget between this one and the root window.
             */
            CreateWindowL(qwidget->parentWidget()->winId());

        // Necessary in order to be able to track the activation status of
        // the control's window
        qwidget->d_func()->createExtra();

        SetFocusing(true);
        m_longTapDetector = QLongTapTimer::NewL(this);
        m_doubleClickTimer.invalidate();

        DrawableWindow()->SetPointerGrab(ETrue);
    }

#ifdef SYMBIAN_GRAPHICS_WSERV_QT_EFFECTS
    if (OwnsWindow()) {
        TTfxWindowPurpose windowPurpose(ETfxPurposeNone);
        switch (qwidget->windowType()) {
        case Qt::Dialog:
            windowPurpose = ETfxPurposeDialogWindow;
            break;
        case Qt::Popup:
            windowPurpose = ETfxPurposePopupWindow;
            break;
        case Qt::Tool:
            windowPurpose = ETfxPurposeToolWindow;
            break;
        case Qt::ToolTip:
            windowPurpose = ETfxPurposeToolTipWindow;
            break;
        case Qt::SplashScreen:
            windowPurpose = ETfxPurposeSplashScreenWindow;
            break;
        default:
            windowPurpose = (isWindowOwning || !qwidget->parentWidget())
                            ? ETfxPurposeWindow : ETfxPurposeChildWindow;
            break;
        }
        Window().SetPurpose(windowPurpose);
    }
#endif
}

QSymbianControl::~QSymbianControl()
{
    // Ensure backing store is deleted before the top-level
    // window is destroyed
    qt_widget_private(qwidget)->topData()->backingStore.destroy();

    if (S60->curWin == this)
        S60->curWin = 0;
    if (!QApplicationPrivate::is_app_closing) {
        QT_TRY {
            setFocusSafely(false);
        } QT_CATCH(const std::exception&) {
            // ignore exceptions, nothing can be done
        }
    }
    S60->appUi()->RemoveFromStack(this);
    delete m_longTapDetector;
}

void QSymbianControl::setWidget(QWidget *w)
{
    qwidget = w;
}
void QSymbianControl::HandleLongTapEventL( const TPoint& aPenEventLocation, const TPoint& aPenEventScreenLocation )
{
    QWidget *alienWidget;
    QPoint widgetPos = QPoint(aPenEventLocation.iX, aPenEventLocation.iY);
    QPoint globalPos = QPoint(aPenEventScreenLocation.iX,aPenEventScreenLocation.iY);
    alienWidget = qwidget->childAt(widgetPos);
    if (!alienWidget)
        alienWidget = qwidget;

#if !defined(QT_NO_CONTEXTMENU)
    QContextMenuEvent contextMenuEvent(QContextMenuEvent::Mouse, widgetPos, globalPos, Qt::NoModifier);
    qt_sendSpontaneousEvent(alienWidget, &contextMenuEvent);
#endif
}

#ifdef QT_SYMBIAN_SUPPORTS_ADVANCED_POINTER
void QSymbianControl::translateAdvancedPointerEvent(const TAdvancedPointerEvent *event)
{
    QApplicationPrivate *d = QApplicationPrivate::instance();
    QPointF screenPos = qwidget->mapToGlobal(QPoint(event->iPosition.iX, event->iPosition.iY));
    qreal pressure;
    if(d->pressureSupported
        && event->Pressure() > 0) //workaround for misconfigured HAL
        pressure = event->Pressure() / qreal(d->maxTouchPressure);
    else
        pressure = qreal(1.0);
    processTouchEvent(event->PointerNumber(), event->iType, screenPos, pressure);
}
#endif

void QSymbianControl::processTouchEvent(int pointerNumber, TPointerEvent::TType type, QPointF screenPos, qreal pressure)
{
    QRect screenGeometry = qApp->desktop()->screenGeometry(qwidget);

    QApplicationPrivate *d = QApplicationPrivate::instance();

    QList<QTouchEvent::TouchPoint> points = d->appAllTouchPoints;
    while (points.count() <= pointerNumber)
        points.append(QTouchEvent::TouchPoint(points.count()));

    Qt::TouchPointStates allStates = 0;
    for (int i = 0; i < points.count(); ++i) {
        QTouchEvent::TouchPoint &touchPoint = points[i];

        if (touchPoint.id() == pointerNumber) {
            Qt::TouchPointStates state;
            switch (type) {
            case TPointerEvent::EButton1Down:
#ifdef QT_SYMBIAN_SUPPORTS_ADVANCED_POINTER
            case TPointerEvent::EEnterHighPressure:
#endif
                state = Qt::TouchPointPressed;
                break;
            case TPointerEvent::EButton1Up:
#ifdef QT_SYMBIAN_SUPPORTS_ADVANCED_POINTER
            case TPointerEvent::EExitCloseProximity:
#endif
                state = Qt::TouchPointReleased;
                break;
            case TPointerEvent::EDrag:
                state = Qt::TouchPointMoved;
                break;
            default:
                // how likely is this to happen?
                state = Qt::TouchPointStationary;
                break;
            }
            if (pointerNumber == 0)
                state |= Qt::TouchPointPrimary;
            touchPoint.setState(state);

            touchPoint.setScreenPos(screenPos);
            touchPoint.setNormalizedPos(QPointF(screenPos.x() / screenGeometry.width(),
                                                screenPos.y() / screenGeometry.height()));

            touchPoint.setPressure(pressure);
        } else if (touchPoint.state() != Qt::TouchPointReleased) {
            // all other active touch points should be marked as stationary
            touchPoint.setState(Qt::TouchPointStationary);
        }

        allStates |= touchPoint.state();
    }

    if ((allStates & Qt::TouchPointStateMask) == Qt::TouchPointReleased) {
        // all touch points released
        d->appAllTouchPoints.clear();
    } else {
        d->appAllTouchPoints = points;
    }

    QApplicationPrivate::translateRawTouchEvent(qwidget,
                                                QTouchEvent::TouchScreen,
                                                points);
}

void QSymbianControl::HandlePointerEventL(const TPointerEvent& pEvent)
{
#ifdef QT_SYMBIAN_SUPPORTS_ADVANCED_POINTER
    if (pEvent.IsAdvancedPointerEvent()) {
        const TAdvancedPointerEvent *advancedPointerEvent = pEvent.AdvancedPointerEvent();
        translateAdvancedPointerEvent(advancedPointerEvent);
        if (advancedPointerEvent->PointerNumber() != 0) {
            // only send mouse events for the first touch point
            return;
        }
    }
#endif

    m_longTapDetector->PointerEventL(pEvent);
    QT_TRYCATCH_LEAVING(HandlePointerEvent(pEvent));
}

void QSymbianControl::HandlePointerEvent(const TPointerEvent& pEvent)
{
    QMouseEvent::Type type;
    Qt::MouseButton button;
    mapS60MouseEventTypeToQt(&type, &button, &pEvent);
    Qt::KeyboardModifiers modifiers = mapToQtModifiers(pEvent.iModifiers);

    QPoint widgetPos = QPoint(pEvent.iPosition.iX, pEvent.iPosition.iY);
    TPoint controlScreenPos = PositionRelativeToScreen();
    QPoint globalPos = QPoint(controlScreenPos.iX, controlScreenPos.iY) + widgetPos;
    S60->lastCursorPos = globalPos;
    S60->lastPointerEventPos = widgetPos;

    QWidget *mouseGrabber = QWidget::mouseGrabber();

    QWidget *popupWidget = qApp->activePopupWidget();
    QWidget *popupReceiver = 0;
    if (popupWidget) {
        QWidget *popupChild = popupWidget->childAt(popupWidget->mapFromGlobal(globalPos));
        popupReceiver = popupChild ? popupChild : popupWidget;
    }

    if (mouseGrabber) {
        if (popupReceiver) {
            sendMouseEvent(popupReceiver, type, globalPos, button, modifiers);
        } else {
            sendMouseEvent(mouseGrabber, type, globalPos, button, modifiers);
        }
        // No Enter/Leave events in grabbing mode.
        return;
    }

    QWidget *widgetUnderPointer = qwidget->childAt(widgetPos);
    if (!widgetUnderPointer)
        widgetUnderPointer = qwidget;

    QApplicationPrivate::dispatchEnterLeave(widgetUnderPointer, S60->lastPointerEventTarget);
    S60->lastPointerEventTarget = widgetUnderPointer;

    QWidget *receiver;
    if (!popupReceiver && S60->mousePressTarget && type != QEvent::MouseButtonPress) {
        receiver = S60->mousePressTarget;
        if (type == QEvent::MouseButtonRelease)
            S60->mousePressTarget = 0;
    } else {
        receiver = popupReceiver ? popupReceiver : widgetUnderPointer;
        if (type == QEvent::MouseButtonPress)
            S60->mousePressTarget = receiver;
    }

#if !defined(QT_NO_CURSOR) && !defined(Q_SYMBIAN_FIXED_POINTER_CURSORS)
    if (S60->brokenPointerCursors)
        qt_symbian_move_cursor_sprite();
#endif

//Generate single touch event for S60 5.0 (has touchscreen, does not have advanced pointers)
#ifndef QT_SYMBIAN_SUPPORTS_ADVANCED_POINTER
    if (S60->hasTouchscreen) {
        processTouchEvent(0, pEvent.iType, QPointF(globalPos), 1.0);
    }
#endif

    sendMouseEvent(receiver, type, globalPos, button, modifiers);
}

#ifdef Q_WS_S60
void QSymbianControl::HandleStatusPaneSizeChange()
{
    QS60MainAppUi *s60AppUi = static_cast<QS60MainAppUi *>(S60->appUi());
    s60AppUi->HandleStatusPaneSizeChange();
}
#endif

void QSymbianControl::sendMouseEvent(
        QWidget *receiver,
        QEvent::Type type,
        const QPoint &globalPos,
        Qt::MouseButton button,
        Qt::KeyboardModifiers modifiers)
{
    Q_ASSERT(receiver);
    QMouseEvent mEvent(type, receiver->mapFromGlobal(globalPos), globalPos,
        button, QApplicationPrivate::mouse_buttons, modifiers);
    QEventDispatcherS60 *dispatcher;
    // It is theoretically possible for someone to install a different event dispatcher.
    if ((dispatcher = qobject_cast<QEventDispatcherS60 *>(receiver->d_func()->threadData->eventDispatcher)) != 0) {
        if (dispatcher->excludeUserInputEvents()) {
            dispatcher->saveInputEvent(this, receiver, new QMouseEvent(mEvent));
            return;
        }
    }

    sendMouseEvent(receiver, &mEvent);
}

bool QSymbianControl::sendMouseEvent(QWidget *widget, QMouseEvent *mEvent)
{
    return qt_sendSpontaneousEvent(widget, mEvent);
}

TKeyResponse QSymbianControl::OfferKeyEventL(const TKeyEvent& keyEvent, TEventCode type)
{
    TKeyResponse r = EKeyWasNotConsumed;
    QT_TRYCATCH_LEAVING(r = OfferKeyEvent(keyEvent, type));
    return r;
}

TKeyResponse QSymbianControl::OfferKeyEvent(const TKeyEvent& keyEvent, TEventCode type)
{
    /*
      S60 has a confusing way of delivering key events. There are three types of
      events: EEventKey, EEventKeyDown and EEventKeyUp. When a key is pressed,
      EEventKeyDown is first generated, followed by EEventKey. Then, when the key is
      released, EEventKeyUp is generated.
      However, it is possible that only the EEventKey is generated alone, typically
      in relation to virtual keyboards. In that case we need to take care to
      generate both press and release events in Qt, since applications expect that.
      We do this by having three states for each used scan code, depending on the
      events received. See the switch below for what happens in each state
      transition.
    */

    if (type != EEventKeyDown)
        if (handleVirtualMouse(keyEvent, type) == EKeyWasConsumed)
            return EKeyWasConsumed;

    TKeyResponse ret = EKeyWasNotConsumed;
#define GET_RETURN(x) (ret = ((x) == EKeyWasConsumed) ? EKeyWasConsumed : ret)

    // This top level switch corresponds to the states, and the inner switches
    // correspond to the transitions.
    QS60Data::ScanCodeState &scanCodeState = S60->scanCodeStates[keyEvent.iScanCode];
    switch (scanCodeState) {
    case QS60Data::Unpressed:
        switch (type) {
        case EEventKeyDown:
            scanCodeState = QS60Data::KeyDown;
            break;
        case EEventKey:
            GET_RETURN(sendSymbianKeyEvent(keyEvent, QEvent::KeyPress));
            GET_RETURN(sendSymbianKeyEvent(keyEvent, QEvent::KeyRelease));
            break;
        case EEventKeyUp:
            // No action.
            break;
        }
        break;
    case QS60Data::KeyDown:
        switch (type) {
        case EEventKeyDown:
            // This should never happen, just stay in this state to be safe.
            break;
        case EEventKey:
            GET_RETURN(sendSymbianKeyEvent(keyEvent, QEvent::KeyPress));
            scanCodeState = QS60Data::KeyDownAndKey;
            break;
        case EEventKeyUp:
            scanCodeState = QS60Data::Unpressed;
            break;
        }
        break;
    case QS60Data::KeyDownAndKey:
        switch (type) {
        case EEventKeyDown:
            // This should never happen, just stay in this state to be safe.
            break;
        case EEventKey:
            GET_RETURN(sendSymbianKeyEvent(keyEvent, QEvent::KeyRelease));
            GET_RETURN(sendSymbianKeyEvent(keyEvent, QEvent::KeyPress));
            break;
        case EEventKeyUp:
            GET_RETURN(sendSymbianKeyEvent(keyEvent, QEvent::KeyRelease));
            scanCodeState = QS60Data::Unpressed;
            break;
        }
        break;
    }
    return ret;

#undef GET_RETURN
}

TKeyResponse QSymbianControl::sendSymbianKeyEvent(const TKeyEvent &keyEvent, QEvent::Type type)
{
    // Because S60 does not generate keysyms for EKeyEventDown and EKeyEventUp
    // events, we need to cache the keysyms from the EKeyEvent events. This is what
    // resolveS60ScanCode does.
    TUint s60Keysym = QApplicationPrivate::resolveS60ScanCode(keyEvent.iScanCode,
            keyEvent.iCode);
    int keyCode;
    if (s60Keysym == EKeyNull){ //some key events have 0 in iCode, for them iScanCode should be used
        keyCode = qt_keymapper_private()->mapS60ScanCodesToQt(keyEvent.iScanCode);
    } else if (s60Keysym >= 0x20 && s60Keysym < ENonCharacterKeyBase) {
        // Normal characters keys.
        keyCode = s60Keysym;
    } else {
        // Special S60 keys.
        keyCode = qt_keymapper_private()->mapS60KeyToQt(s60Keysym);
    }

    Qt::KeyboardModifiers mods = mapToQtModifiers(keyEvent.iModifiers);
    QKeyEventEx qKeyEvent(type, keyCode, mods, qt_keymapper_private()->translateKeyEvent(keyCode, mods),
            (keyEvent.iRepeats != 0), 1, keyEvent.iScanCode, s60Keysym, keyEvent.iModifiers);
    QWidget *widget;
    widget = QWidget::keyboardGrabber();
    if (!widget) {
        if (QApplicationPrivate::popupWidgets != 0) {
            widget = QApplication::activePopupWidget()->focusWidget();
            if (!widget) {
                widget = QApplication::activePopupWidget();
            }
        } else {
            widget = QApplicationPrivate::focus_widget;
            if (!widget) {
                widget = qwidget;
            }
        }
    }

    QEventDispatcherS60 *dispatcher;
    // It is theoretically possible for someone to install a different event dispatcher.
    if ((dispatcher = qobject_cast<QEventDispatcherS60 *>(widget->d_func()->threadData->eventDispatcher)) != 0) {
        if (dispatcher->excludeUserInputEvents()) {
            dispatcher->saveInputEvent(this, widget, new QKeyEventEx(qKeyEvent));
            return EKeyWasConsumed;
        }
    }
    return sendKeyEvent(widget, &qKeyEvent);
}

TKeyResponse QSymbianControl::handleVirtualMouse(const TKeyEvent& keyEvent,TEventCode type)
{
#ifndef QT_NO_CURSOR
    if (S60->mouseInteractionEnabled && S60->virtualMouseRequired) {
        //translate keys to pointer
        if ((keyEvent.iScanCode >= EStdKeyLeftArrow && keyEvent.iScanCode <= EStdKeyDownArrow) ||
                (keyEvent.iScanCode >= EStdKeyDevice10 && keyEvent.iScanCode <= EStdKeyDevice13) ||
                keyEvent.iScanCode == EStdKeyDevice3) {
            QPoint pos = QCursor::pos();
            TPointerEvent fakeEvent;
            fakeEvent.iType = (TPointerEvent::TType)(-1);
            fakeEvent.iModifiers = keyEvent.iModifiers;
            TInt x = pos.x();
            TInt y = pos.y();
            if (type == EEventKeyUp) {
                S60->virtualMouseAccelTimeout.start();
                switch (keyEvent.iScanCode) {
                case EStdKeyLeftArrow:
                    S60->virtualMousePressedKeys &= ~QS60Data::Left;
                    break;
                case EStdKeyRightArrow:
                    S60->virtualMousePressedKeys &= ~QS60Data::Right;
                    break;
                case EStdKeyUpArrow:
                    S60->virtualMousePressedKeys &= ~QS60Data::Up;
                    break;
                case EStdKeyDownArrow:
                    S60->virtualMousePressedKeys &= ~QS60Data::Down;
                    break;
                // diagonal keys (named aliases don't exist in 3.1 SDK)
                case EStdKeyDevice10:
                    S60->virtualMousePressedKeys &= ~QS60Data::LeftUp;
                    break;
                case EStdKeyDevice11:
                    S60->virtualMousePressedKeys &= ~QS60Data::RightUp;
                    break;
                case EStdKeyDevice12:
                    S60->virtualMousePressedKeys &= ~QS60Data::RightDown;
                    break;
                case EStdKeyDevice13:
                    S60->virtualMousePressedKeys &= ~QS60Data::LeftDown;
                    break;
                case EStdKeyDevice3: //select
                    if (S60->virtualMousePressedKeys & QS60Data::Select)
                        fakeEvent.iType = TPointerEvent::EButton1Up;
                    S60->virtualMousePressedKeys &= ~QS60Data::Select;
                    break;
                }
            }
            else if (type == EEventKey) {
                int dx = 0;
                int dy = 0;
                if (keyEvent.iScanCode != EStdKeyDevice3) {
                    m_doubleClickTimer.invalidate();
                    //reset mouse accelleration after a short time with no moves
                    const int maxTimeBetweenKeyEventsMs = 500;
                    if (S60->virtualMouseAccelTimeout.isValid() &&
                            S60->virtualMouseAccelTimeout.hasExpired(maxTimeBetweenKeyEventsMs)) {
                        S60->virtualMouseAccelDX = 0;
                        S60->virtualMouseAccelDY = 0;
                    }
                    S60->virtualMouseAccelTimeout.invalidate();
                }
                switch (keyEvent.iScanCode) {
                case EStdKeyLeftArrow:
                    S60->virtualMousePressedKeys |= QS60Data::Left;
                    dx = -1;
                    fakeEvent.iType = TPointerEvent::EMove;
                    break;
                case EStdKeyRightArrow:
                    S60->virtualMousePressedKeys |= QS60Data::Right;
                    dx = 1;
                    fakeEvent.iType = TPointerEvent::EMove;
                    break;
                case EStdKeyUpArrow:
                    S60->virtualMousePressedKeys |= QS60Data::Up;
                    dy = -1;
                    fakeEvent.iType = TPointerEvent::EMove;
                    break;
                case EStdKeyDownArrow:
                    S60->virtualMousePressedKeys |= QS60Data::Down;
                    dy = 1;
                    fakeEvent.iType = TPointerEvent::EMove;
                    break;
                case EStdKeyDevice10:
                    S60->virtualMousePressedKeys |= QS60Data::LeftUp;
                    dx = -1;
                    dy = -1;
                    fakeEvent.iType = TPointerEvent::EMove;
                    break;
                case EStdKeyDevice11:
                    S60->virtualMousePressedKeys |= QS60Data::RightUp;
                    dx = 1;
                    dy = -1;
                    fakeEvent.iType = TPointerEvent::EMove;
                    break;
                case EStdKeyDevice12:
                    S60->virtualMousePressedKeys |= QS60Data::RightDown;
                    dx = 1;
                    dy = 1;
                    fakeEvent.iType = TPointerEvent::EMove;
                    break;
                case EStdKeyDevice13:
                    S60->virtualMousePressedKeys |= QS60Data::LeftDown;
                    dx = -1;
                    dy = 1;
                    fakeEvent.iType = TPointerEvent::EMove;
                    break;
                case EStdKeyDevice3:
                    // Platform bug. If you start pressing several keys simultaneously (for
                    // example for drag'n'drop), Symbian starts producing spurious up and
                    // down messages for some keys. Therefore, make sure we have a clean slate
                    // of pressed keys before starting a new button press.
                    if (S60->virtualMousePressedKeys & QS60Data::Select) {
                        return EKeyWasConsumed;
                    } else {
                        S60->virtualMousePressedKeys |= QS60Data::Select;
                        fakeEvent.iType = TPointerEvent::EButton1Down;
                        if (m_doubleClickTimer.isValid()
                                && !m_doubleClickTimer.hasExpired(QApplication::doubleClickInterval())) {
                            fakeEvent.iModifiers |= EModifierDoubleClick;
                            m_doubleClickTimer.invalidate();
                        } else {
                            m_doubleClickTimer.start();
                        }
                    }
                    break;
                }
                if (dx) {
                    int cdx = S60->virtualMouseAccelDX;
                    //reset accel on change of sign, else double accel
                    if (dx * cdx <= 0)
                        cdx = dx;
                    else
                        cdx *= 4;
                    //cap accelleration
                    if (dx * cdx > S60->virtualMouseMaxAccel)
                        cdx = dx * S60->virtualMouseMaxAccel;
                    //move mouse position
                    x += cdx;
                    S60->virtualMouseAccelDX = cdx;
                }

                if (dy) {
                    int cdy = S60->virtualMouseAccelDY;
                    if (dy * cdy <= 0)
                        cdy = dy;
                    else
                        cdy *= 4;
                    if (dy * cdy > S60->virtualMouseMaxAccel)
                        cdy = dy * S60->virtualMouseMaxAccel;
                    y += cdy;
                    S60->virtualMouseAccelDY = cdy;
                }
            }
            //clip to screen size (window server allows a sprite hotspot to be outside the screen)
            if (x < 0)
                x = 0;
            else if (x >= S60->screenWidthInPixels)
                x = S60->screenWidthInPixels - 1;
            if (y < 0)
                y = 0;
            else if (y >= S60->screenHeightInPixels)
                y = S60->screenHeightInPixels - 1;
            TPoint epos(x, y);
            TPoint cpos = epos - PositionRelativeToScreen();
            fakeEvent.iPosition = cpos;
            fakeEvent.iParentPosition = epos;
            if(fakeEvent.iType != -1)
                HandlePointerEvent(fakeEvent);
            return EKeyWasConsumed;
        }
    }
#endif

    return EKeyWasNotConsumed;
}

void QSymbianControl::sendInputEvent(QWidget *widget, QInputEvent *inputEvent)
{
    switch (inputEvent->type()) {
    case QEvent::KeyPress:
    case QEvent::KeyRelease:
        sendKeyEvent(widget, static_cast<QKeyEvent *>(inputEvent));
        break;
    case QEvent::MouseButtonDblClick:
    case QEvent::MouseButtonPress:
    case QEvent::MouseButtonRelease:
    case QEvent::MouseMove:
        sendMouseEvent(widget, static_cast<QMouseEvent *>(inputEvent));
        break;
    default:
        // Shouldn't get here.
        Q_ASSERT_X(0 == 1, "QSymbianControl::sendInputEvent()", "inputEvent->type() is unknown");
        break;
    }
}

TKeyResponse QSymbianControl::sendKeyEvent(QWidget *widget, QKeyEvent *keyEvent)
{
#if !defined(QT_NO_IM) && defined(Q_WS_S60)
    if (widget && widget->isEnabled() && widget->testAttribute(Qt::WA_InputMethodEnabled)) {
        QInputContext *qic = widget->inputContext();
        if (qic && qic->filterEvent(keyEvent))
            return EKeyWasConsumed;
    }
#endif // !defined(QT_NO_IM) && defined(Q_OS_SYMBIAN)

    if (widget && qt_sendSpontaneousEvent(widget, keyEvent))
        if (keyEvent->isAccepted())
            return EKeyWasConsumed;

    return EKeyWasNotConsumed;
}

#if !defined(QT_NO_IM) && defined(Q_WS_S60)
TCoeInputCapabilities QSymbianControl::InputCapabilities() const
{
    QWidget *w = 0;

    if (qwidget->hasFocus())
        w = qwidget;
    else
        w = qwidget->focusWidget();

    QCoeFepInputContext *ic;
    if (w && w->isEnabled() && w->testAttribute(Qt::WA_InputMethodEnabled)
            && (ic = qobject_cast<QCoeFepInputContext *>(w->inputContext()))) {
        return ic->inputCapabilities();
    } else {
        return TCoeInputCapabilities(TCoeInputCapabilities::ENone, 0, 0);
    }
}
#endif

void QSymbianControl::Draw(const TRect& controlRect) const
{
    // Set flag to avoid calling DrawNow in window surface
    QWidget *window = qwidget->window();
    Q_ASSERT(window);
    QTLWExtra *topExtra = window->d_func()->maybeTopData();
    Q_ASSERT(topExtra);
    if (!topExtra->inExpose) {
        topExtra->inExpose = true;
        QRect exposeRect = qt_TRect2QRect(controlRect);
        qwidget->d_func()->syncBackingStore(exposeRect);
        topExtra->inExpose = false;
    }

    QWindowSurface *surface = qwidget->windowSurface();
    QPaintEngine *engine = surface ? surface->paintDevice()->paintEngine() : NULL;

    if (!engine)
        return;

    const bool sendNativePaintEvents = qwidget->d_func()->extraData()->receiveNativePaintEvents;
    if (sendNativePaintEvents) {
        const QRect r = qt_TRect2QRect(controlRect);
        QMetaObject::invokeMethod(qwidget, "beginNativePaintEvent", Qt::DirectConnection, Q_ARG(QRect, r));
    }

    // Map source rectangle into coordinates of the backing store.
    const QPoint controlBase(controlRect.iTl.iX, controlRect.iTl.iY);
    const QPoint backingStoreBase = qwidget->mapTo(qwidget->window(), controlBase);
    const TRect backingStoreRect(TPoint(backingStoreBase.x(), backingStoreBase.y()), controlRect.Size());

    if (engine->type() == QPaintEngine::Raster) {
        QS60WindowSurface *s60Surface;
#ifdef QT_GRAPHICSSYSTEM_RUNTIME
        if (QApplicationPrivate::runtime_graphics_system) {
            QRuntimeWindowSurface *rtSurface =
                    static_cast<QRuntimeWindowSurface*>(qwidget->windowSurface());
            s60Surface = static_cast<QS60WindowSurface *>(rtSurface->m_windowSurface.data());
        } else
#endif
            s60Surface = static_cast<QS60WindowSurface *>(qwidget->windowSurface());

        CFbsBitmap *bitmap = s60Surface->symbianBitmap();
        CWindowGc &gc = SystemGc();

        QWExtra::NativePaintMode nativePaintMode = qwidget->d_func()->extraData()->nativePaintMode;
        if(qwidget->d_func()->paintOnScreen())
            nativePaintMode = QWExtra::Disable;

        switch(nativePaintMode) {
        case QWExtra::Disable:
            // Do nothing
            break;
        case QWExtra::Blit:
            if (qwidget->d_func()->isOpaque)
                gc.SetDrawMode(CGraphicsContext::EDrawModeWriteAlpha);
            gc.BitBlt(controlRect.iTl, bitmap, backingStoreRect);
            break;
        case QWExtra::ZeroFill:
            if (Window().DisplayMode() == EColor16MA
                || Window().DisplayMode() == Q_SYMBIAN_ECOLOR16MAP) {
                gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
                gc.SetDrawMode(CGraphicsContext::EDrawModeWriteAlpha);
                gc.SetBrushColor(TRgb::Color16MA(0));
                gc.Clear(controlRect);
            } else {
                gc.SetBrushColor(TRgb(0x000000));
                gc.Clear(controlRect);
            };
            break;
        default:
            Q_ASSERT(false);
        }
    }

    if (sendNativePaintEvents) {
        const QRect r = qt_TRect2QRect(controlRect);
        // The draw ops aren't actually sent to WSERV until the graphics
        // context is deactivated, which happens in the function calling
        // this one.  We therefore delay the delivery of endNativePaintEvent,
        // to ensure that drawing has completed by the time the widget
        // receives the event.  Note that, if the widget needs to ensure
        // that the draw ops have actually been executed into the output
        // framebuffer, a call to RWsSession::Flush is required in the
        // endNativePaintEvent implementation.
        QMetaObject::invokeMethod(qwidget, "endNativePaintEvent", Qt::QueuedConnection, Q_ARG(QRect, r));
    }
}

void QSymbianControl::SizeChanged()
{
    CCoeControl::SizeChanged();

    QSize oldSize = qwidget->size();
    QSize newSize(Size().iWidth, Size().iHeight);

    if (oldSize != newSize) {
        QRect cr = qwidget->geometry();
        cr.setSize(newSize);
        qwidget->data->crect = cr;
        if (qwidget->isVisible()) {
            QTLWExtra *tlwExtra = qwidget->d_func()->maybeTopData();
            bool slowResize = qgetenv("QT_SLOW_TOPLEVEL_RESIZE").toInt();
            if (!slowResize && tlwExtra)
                tlwExtra->inTopLevelResize = true;
            QResizeEvent e(newSize, oldSize);
            qt_sendSpontaneousEvent(qwidget, &e);
            if (!qwidget->testAttribute(Qt::WA_StaticContents))
                qwidget->d_func()->syncBackingStore();
            if (!slowResize && tlwExtra)
                tlwExtra->inTopLevelResize = false;
        } else {
            QResizeEvent *e = new QResizeEvent(newSize, oldSize);
            QApplication::postEvent(qwidget, e);
        }
    }

    // CCoeControl::SetExtent calls SizeChanged, but does not call
    // PositionChanged, so we call it here to ensure that the widget's
    // position is updated.
    PositionChanged();
}

void QSymbianControl::PositionChanged()
{
    CCoeControl::PositionChanged();

    QPoint oldPos = qwidget->geometry().topLeft();
    QPoint newPos(Position().iX, Position().iY);

    if (oldPos != newPos) {
        QRect cr = qwidget->geometry();
        cr.moveTopLeft(newPos);
        qwidget->data->crect = cr;
        QTLWExtra *top = qwidget->d_func()->maybeTopData();
        if (top && (qwidget->windowState() & (~Qt::WindowActive)) == Qt::WindowNoState)
            top->normalGeometry.moveTopLeft(newPos);
        if (qwidget->isVisible()) {
            QMoveEvent e(newPos, oldPos);
            qt_sendSpontaneousEvent(qwidget, &e);
        } else {
            QMoveEvent * e = new QMoveEvent(newPos, oldPos);
            QApplication::postEvent(qwidget, e);
        }
    }
}

void QSymbianControl::FocusChanged(TDrawNow /* aDrawNow */)
{
    if (m_ignoreFocusChanged || (qwidget->windowType() & Qt::WindowType_Mask) == Qt::Desktop)
        return;

    // Popups never get focused, but still receive the FocusChanged when they are hidden.
    if (QApplicationPrivate::popupWidgets != 0
            || (qwidget->windowType() & Qt::Popup) == Qt::Popup)
        return;

    if (IsFocused() && IsVisible()) {
        if (m_symbianPopupIsOpen) {
            QWidget *fw = QApplication::focusWidget();
            if (fw) {
                QFocusEvent event(QEvent::FocusIn, Qt::PopupFocusReason);
                QCoreApplication::sendEvent(fw, &event);
            }
            m_symbianPopupIsOpen = false;
        }

        QApplication::setActiveWindow(qwidget->window());
        qwidget->d_func()->setWindowIcon_sys(true);
        qwidget->d_func()->setWindowTitle_sys(qwidget->windowTitle());
#ifdef Q_WS_S60
        // If widget is fullscreen/minimized, hide status pane and button container otherwise show them.
        const bool visible = !(qwidget->windowState() & (Qt::WindowFullScreen | Qt::WindowMinimized));
        const bool statusPaneVisibility = visible;
        const bool isFullscreen = qwidget->windowState() & Qt::WindowFullScreen;
        const bool cbaVisibilityHint = qwidget->windowFlags() & Qt::WindowSoftkeysVisibleHint;
        const bool buttonGroupVisibility = (visible || (isFullscreen && cbaVisibilityHint));
        S60->setStatusPaneAndButtonGroupVisibility(statusPaneVisibility, buttonGroupVisibility);
#endif
    } else if (QApplication::activeWindow() == qwidget->window()) {
        bool focusedControlFound = false;
        WId winId = 0;
        for (QWidget *w = qwidget->parentWidget(); w && (winId = w->internalWinId()); w = w->parentWidget()) {
            if (winId->IsFocused() && winId->IsVisible()) {
                focusedControlFound = true;
                break;
            } else if (w->isWindow())
                break;
        }
        if (!focusedControlFound) {
            if (CCoeEnv::Static()->AppUi()->IsDisplayingMenuOrDialog() || S60->menuBeingConstructed) {
                QWidget *fw = QApplication::focusWidget();
                if (fw) {
                    QFocusEvent event(QEvent::FocusOut, Qt::PopupFocusReason);
                    QCoreApplication::sendEvent(fw, &event);
                }
                m_symbianPopupIsOpen = true;
                return;
            }

            QApplication::setActiveWindow(0);
        }
    }
    // else { We don't touch the active window unless we were explicitly activated or deactivated }
}

void QSymbianControl::handleClientAreaChange()
{
    const bool cbaVisibilityHint = qwidget->windowFlags() & Qt::WindowSoftkeysVisibleHint;
    if (qwidget->isFullScreen() && !cbaVisibilityHint) {
        SetExtentToWholeScreen();
    } else if (qwidget->isMaximized() || (qwidget->isFullScreen() && cbaVisibilityHint)) {
        TRect r = static_cast<CEikAppUi*>(S60->appUi())->ClientRect();
        SetExtent(r.iTl, r.Size());
    } else if (!qwidget->isMinimized()) { // Normal geometry
        if (!qwidget->testAttribute(Qt::WA_Resized)) {
            qwidget->adjustSize();
            qwidget->setAttribute(Qt::WA_Resized, false); //not a user resize
        }
        if (!qwidget->testAttribute(Qt::WA_Moved) && qwidget->windowType() != Qt::Dialog) {
            TRect r = static_cast<CEikAppUi*>(S60->appUi())->ClientRect();
            SetPosition(r.iTl);
            qwidget->setAttribute(Qt::WA_Moved, false); // not really an explicit position
        }
    }
}

void QSymbianControl::HandleResourceChange(int resourceType)
{
    switch (resourceType) {
    case KInternalStatusPaneChange:
        handleClientAreaChange();
        if (IsFocused() && IsVisible()) {
            qwidget->d_func()->setWindowIcon_sys(true);
            qwidget->d_func()->setWindowTitle_sys(qwidget->windowTitle());
        }
        break;
    case KUidValueCoeFontChangeEvent:
        // font change event
        break;
#ifdef Q_WS_S60
    case KEikDynamicLayoutVariantSwitch:
    {
        handleClientAreaChange();
        break;
    }
#endif
    default:
        break;
    }

    CCoeControl::HandleResourceChange(resourceType);

}
void QSymbianControl::CancelLongTapTimer()
{
    m_longTapDetector->Cancel();
}

TTypeUid::Ptr QSymbianControl::MopSupplyObject(TTypeUid id)
{
    if (id.iUid == ETypeId)
        return id.MakePtr(this);

    return CCoeControl::MopSupplyObject(id);
}

void QSymbianControl::setFocusSafely(bool focus)
{
    // The stack hack in here is very unfortunate, but it is the only way to ensure proper
    // focus in Symbian. If this is not executed, the control which happens to be on
    // the top of the stack may randomly be assigned focus by Symbian, for example
    // when creating new windows (specifically in CCoeAppUi::HandleStackChanged()).

    // Close any popups.
    CEikonEnv::Static()->EikAppUi()->StopDisplayingMenuBar();

    if (focus) {
        S60->appUi()->RemoveFromStack(this);
        // Symbian doesn't automatically remove focus from the last focused control, so we need to
        // remember it and clear focus ourselves.
        if (lastFocusedControl && lastFocusedControl != this)
            lastFocusedControl->SetFocus(false);
        QT_TRAP_THROWING(S60->appUi()->AddToStackL(this,
                ECoeStackPriorityDefault + 1, ECoeStackFlagStandard)); // Note the + 1
        lastFocusedControl = this;
        this->SetFocus(true);
    } else {
        S60->appUi()->RemoveFromStack(this);
        QT_TRAP_THROWING(S60->appUi()->AddToStackL(this,
                ECoeStackPriorityDefault, ECoeStackFlagStandard));
        if(this == lastFocusedControl)
            lastFocusedControl = 0;
        this->SetFocus(false);
    }
}

bool QSymbianControl::isControlActive()
{
    return IsActivated() ? true : false;
}

/*!
    \typedef QApplication::QS60MainApplicationFactory
    \since 4.6

    This is a typedef for a pointer to a function with the following
    signature:

    \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 47

    \sa QApplication::QApplication()
*/

/*!
    \since 4.6

    Creates an application using the application factory given in
    \a factory, and using \a argc command line arguments in \a argv.
    \a factory can be leaving, but the error will be converted to a
    standard exception.

    This function is only available on S60.
*/
QApplication::QApplication(QApplication::QS60MainApplicationFactory factory, int &argc, char **argv)
    : QCoreApplication(*new QApplicationPrivate(argc, argv, GuiClient))
{
    Q_D(QApplication);
    S60->s60ApplicationFactory = factory;
    d->construct();
}

QApplication::QApplication(QApplication::QS60MainApplicationFactory factory, int &argc, char **argv, int _internal)
    : QCoreApplication(*new QApplicationPrivate(argc, argv, GuiClient))
{
    Q_D(QApplication);
    S60->s60ApplicationFactory = factory;
    d->construct();
    QApplicationPrivate::app_compile_version = _internal;
}

void qt_init(QApplicationPrivate * /* priv */, int)
{
    if (!CCoeEnv::Static()) {
        // The S60 framework creates a new trap handler which will render any existing traps
        // invalid as long as it is active. This means that all code in main() that occurs after
        // the QApplication construction needs to be surrounded by a new trap, despite having
        // an outer one already. To avoid this, we save the original trap handler here, and set
        // it back after the S60 framework is constructed. Then we restore it right before the S60
        // framework destruction.
        TTrapHandler *origTrapHandler = User::TrapHandler();

        // The S60 framework has not been initialized. We need to do it.
        TApaApplicationFactory factory(S60->s60ApplicationFactory ?
                S60->s60ApplicationFactory : newS60Application);
        CApaCommandLine* commandLine = 0;
        TInt err = CApaCommandLine::GetCommandLineFromProcessEnvironment(commandLine);
        // After this construction, CEikonEnv will be available from CEikonEnv::Static().
        // (much like our qApp).
        CEikonEnv* coe = new CEikonEnv;
        //not using QT_TRAP_THROWING, because coe owns the cleanupstack so it can't be pushed there.
        if(err == KErrNone)
            TRAP(err, coe->ConstructAppFromCommandLineL(factory,*commandLine));
        delete commandLine;
        if(err != KErrNone) {
            qWarning() << "qt_init: Eikon application construct failed ("
                       << err
                       << "), maybe missing resource file on S60 3.1?";
            delete coe;
            qt_symbian_throwIfError(err);
        }

        S60->s60InstalledTrapHandler = User::SetTrapHandler(origTrapHandler);

        S60->qtOwnsS60Environment = true;
    } else {
        S60->qtOwnsS60Environment = false;
    }

#ifdef QT_NO_DEBUG
    if (!qgetenv("QT_S60_AUTO_FLUSH_WSERV").isEmpty())
#endif
        S60->wsSession().SetAutoFlush(ETrue);

#ifdef Q_SYMBIAN_WINDOW_SIZE_CACHE
    TRAP_IGNORE(S60->wsSession().EnableWindowSizeCacheL());
#endif

    S60->updateScreenSize();


    TDisplayMode mode = S60->screenDevice()->DisplayMode();
    S60->screenDepth = TDisplayModeUtils::NumDisplayModeBitsPerPixel(mode);

    //NB: RWsSession::GetColorModeList tells you what window modes are supported,
    //not what bitmap formats.
    if(QSysInfo::symbianVersion() == QSysInfo::SV_9_2)
        S60->supportsPremultipliedAlpha = 0;
    else
        S60->supportsPremultipliedAlpha = 1;

    RProcess me;
    TSecureId securId = me.SecureId();
    S60->uid = securId.operator TUid();

    // enable focus events - used to re-enable mouse after focus changed between mouse and non mouse app,
    // and for dimming behind modal windows
    S60->windowGroup().EnableFocusChangeEvents();

    //Check if mouse interaction is supported (either EMouse=1 in the HAL, or EMachineUID is one of the phones known to support this)
    const TInt KMachineUidSamsungI8510 = 0x2000C51E;
    // HAL::Get(HALData::EPen, TInt& result) may set 'result' to 1 on some 3.1 systems (e.g. N95).
    // But we know that S60 systems below 5.0 did not support touch.
    static const bool touchIsUnsupportedOnSystem =
        QSysInfo::s60Version() == QSysInfo::SV_S60_3_1
        || QSysInfo::s60Version() == QSysInfo::SV_S60_3_2;
    TInt machineUID;
    TInt mouse;
    TInt touch;
    TInt err;
    err = HAL::Get(HALData::EMouse, mouse);
    if (err != KErrNone)
        mouse = 0;
    err = HAL::Get(HALData::EMachineUid, machineUID);
    if (err != KErrNone)
        machineUID = 0;
    err = HAL::Get(HALData::EPen, touch);
    if (err != KErrNone || touchIsUnsupportedOnSystem)
        touch = 0;
#ifdef __WINS__
    if(QSysInfo::symbianVersion() <= QSysInfo::SV_9_4) {
        //for symbian SDK emulator, force values to match typical devices.
        mouse = 0;
        touch = touchIsUnsupportedOnSystem ? 0 : 1;
    }
#endif
    if (mouse || machineUID == KMachineUidSamsungI8510) {
        S60->hasTouchscreen = false;
        S60->virtualMouseRequired = false;
    }
    else if (!touch) {
        S60->hasTouchscreen = false;
        S60->virtualMouseRequired = true;
    }
    else {
        S60->hasTouchscreen = true;
        S60->virtualMouseRequired = false;
    }

    S60->avkonComponentsSupportTransparency = false;
    S60->menuBeingConstructed = false;

#ifdef Q_WS_S60
    TUid KCRUidAvkon = { 0x101F876E };
    TUint32 KAknAvkonTransparencyEnabled = 0x0000000D;

    CRepository* repository = 0;
    TRAP(err, repository = CRepository::NewL(KCRUidAvkon));

    if(err == KErrNone) {
        TInt value = 0;
        err = repository->Get(KAknAvkonTransparencyEnabled, value);
        if(err == KErrNone) {
            S60->avkonComponentsSupportTransparency = (value==1) ? true : false;
        }
    }
    delete repository;
    repository = 0;
#endif

#ifdef QT_KEYPAD_NAVIGATION
    if (touch) {
        QApplicationPrivate::navigationMode = Qt::NavigationModeNone;
    } else {
        QApplicationPrivate::navigationMode = Qt::NavigationModeKeypadDirectional;
    }
#endif

#ifndef QT_NO_CURSOR
    //Check if window server pointer cursors are supported or not
#ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS
    //In generic binary, use the HAL and OS version
    //Any other known good phones should be added here.
    if (machineUID == KMachineUidSamsungI8510 || (QSysInfo::symbianVersion() != QSysInfo::SV_9_4
        && QSysInfo::symbianVersion() != QSysInfo::SV_9_3 && QSysInfo::symbianVersion()
        != QSysInfo::SV_9_2)) {
        S60->brokenPointerCursors = false;
        qt_symbian_setWindowGroupCursor(Qt::ArrowCursor, S60->windowGroup());
    }
    else
        S60->brokenPointerCursors = true;
#endif

    if (S60->mouseInteractionEnabled) {
#ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS
        if (S60->brokenPointerCursors) {
            qt_symbian_set_pointer_sprite(Qt::ArrowCursor);
            qt_symbian_show_pointer_sprite();
        }
        else
#endif
            S60->wsSession().SetPointerCursorMode(EPointerCursorNormal);
    }
#endif

    QFont systemFont;
    systemFont.setFamily(systemFont.defaultFamily());
    QApplicationPrivate::setSystemFont(systemFont);

#ifdef SYMBIAN_GRAPHICS_WSERV_QT_EFFECTS
    QObject::connect(qApp, SIGNAL(aboutToQuit()), qApp, SLOT(_q_aboutToQuit()));
#endif

/*
 ### Commented out for now as parameter handling not needed in SOS(yet). Code below will break testlib with -o flag
    int argc = priv->argc;
    char **argv = priv->argv;

    // Get command line params
    int j = argc ? 1 : 0;
    for (int i=1; i<argc; i++) {
        if (argv[i] && *argv[i] != '-') {
            argv[j++] = argv[i];
            continue;
        }

#if defined(QT_DEBUG)
        if (qstrcmp(argv[i], "-nograb") == 0)
            appNoGrab = !appNoGrab;
        else
#endif // QT_DEBUG
            ;
    }
*/

    // Register WId with the metatype system.  This is to enable
    // QWidgetPrivate::create_sys to used delayed slot invocation in order
    // to destroy WId objects during reparenting.
    qRegisterMetaType<WId>("WId");
}

extern void qt_cleanup_symbianFontDatabaseExtras(); // qfontdatabase_s60.cpp

/*****************************************************************************
  qt_cleanup() - cleans up when the application is finished
 *****************************************************************************/
void qt_cleanup()
{
    if(qt_S60Beep) {
        delete qt_S60Beep;
        qt_S60Beep = 0;
    }
    QFontCache::cleanup(); // Has to happen now, since QFontEngineS60 has FBS handles
    QPixmapCache::clear(); // Has to happen now, since QS60PixmapData has FBS handles

    qt_cleanup_symbianFontDatabaseExtras();
// S60 structure and window server session are freed in eventdispatcher destructor as they are needed there

    // It's important that this happens here, before the event dispatcher gets
    // deleted, because the input context needs the event loop one last time before
    // it dies.
    delete QApplicationPrivate::inputContext;
    QApplicationPrivate::inputContext = 0;

    //Change mouse pointer back
    S60->wsSession().SetPointerCursorMode(EPointerCursorNone);

    if (S60->qtOwnsS60Environment) {
        // Restore the S60 framework trap handler. See qt_init().
        User::SetTrapHandler(S60->s60InstalledTrapHandler);

        CEikonEnv* coe = CEikonEnv::Static();
        coe->PrepareToExit();
        // The CEikonEnv itself is destroyed in here.
        coe->DestroyEnvironment();
    }
}

void QApplicationPrivate::initializeWidgetPaletteHash()
{
    // TODO: Implement QApplicationPrivate::initializeWidgetPaletteHash()
    // Possibly a task fot the S60Style guys
}

void QApplicationPrivate::createEventDispatcher()
{
    Q_Q(QApplication);
    eventDispatcher = new QEventDispatcherS60(q);
}

QString QApplicationPrivate::appName() const
{
    return QCoreApplicationPrivate::appName();
}

bool QApplicationPrivate::modalState()
{
    return app_do_modal;
}

void QApplicationPrivate::enterModal_sys(QWidget *widget)
{
#ifdef SYMBIAN_GRAPHICS_WSERV_QT_EFFECTS
    S60->wsSession().SendEffectCommand(ETfxCmdAppModalModeEnter);
#endif
    if (widget) {
        static_cast<QSymbianControl *>(widget->effectiveWinId())->FadeBehindPopup(ETrue);
        // Modal partial screen dialogs (like queries) capture pointer events.
        // ### FixMe: Add specialized behaviour for fullscreen modal dialogs
        widget->effectiveWinId()->SetGloballyCapturing(ETrue);
        widget->effectiveWinId()->SetPointerCapture(ETrue);
    }
    if (!qt_modal_stack)
        qt_modal_stack = new QWidgetList;
    qt_modal_stack->insert(0, widget);
    app_do_modal = true;
}

void QApplicationPrivate::leaveModal_sys(QWidget *widget)
{
#ifdef SYMBIAN_GRAPHICS_WSERV_QT_EFFECTS
    S60->wsSession().SendEffectCommand(ETfxCmdAppModalModeExit);
#endif
    if (widget) {
        static_cast<QSymbianControl *>(widget->effectiveWinId())->FadeBehindPopup(EFalse);
        // ### FixMe: Add specialized behaviour for fullscreen modal dialogs
        widget->effectiveWinId()->SetGloballyCapturing(EFalse);
        widget->effectiveWinId()->SetPointerCapture(EFalse);
    }
    if (qt_modal_stack && qt_modal_stack->removeAll(widget)) {
        if (qt_modal_stack->isEmpty()) {
            delete qt_modal_stack;
            qt_modal_stack = 0;
        }
    }
    app_do_modal = qt_modal_stack != 0;
}

void QApplicationPrivate::openPopup(QWidget *popup)
{
    if (popup && qobject_cast<QComboBox *>(popup->parentWidget()))
        static_cast<QSymbianControl *>(popup->effectiveWinId())->FadeBehindPopup(ETrue);

    if (!QApplicationPrivate::popupWidgets)
        QApplicationPrivate::popupWidgets = new QWidgetList;
    QApplicationPrivate::popupWidgets->append(popup);

    // Cancel focus widget pointer capture and long tap timer
    if (QApplication::focusWidget()) {
        static_cast<QSymbianControl*>(QApplication::focusWidget()->effectiveWinId())->CancelLongTapTimer();
        QApplication::focusWidget()->effectiveWinId()->SetPointerCapture(false);
        }

    if (!qt_nograb()) {
        // Cancel pointer capture and long tap timer for earlier popup
        int popupCount = QApplicationPrivate::popupWidgets->count();
        if (popupCount > 1) {
            QWidget* prevPopup = QApplicationPrivate::popupWidgets->at(popupCount-2);
            static_cast<QSymbianControl*>(prevPopup->effectiveWinId())->CancelLongTapTimer();
            prevPopup->effectiveWinId()->SetPointerCapture(false);
        }

        // Enable pointer capture for this (topmost) popup
        Q_ASSERT(popup->testAttribute(Qt::WA_WState_Created));
        WId id = popup->effectiveWinId();
        id->SetPointerCapture(true);
    }

    // popups are not focus-handled by the window system (the first
    // popup grabbed the keyboard), so we have to do that manually: A
    // new popup gets the focus
    QWidget *fw = popup->focusWidget();
    if (fw) {
        fw->setFocus(Qt::PopupFocusReason);
    } else if (QApplicationPrivate::popupWidgets->count() == 1) { // this was the first popup
        fw = QApplication::focusWidget();
        if (fw) {
            QFocusEvent e(QEvent::FocusOut, Qt::PopupFocusReason);
            q_func()->sendEvent(fw, &e);
        }
    }
}

void QApplicationPrivate::closePopup(QWidget *popup)
{
    if (popup && qobject_cast<QComboBox *>(popup->parentWidget()))
        static_cast<QSymbianControl *>(popup->effectiveWinId())->FadeBehindPopup(EFalse);

    if (!QApplicationPrivate::popupWidgets)
        return;
    QApplicationPrivate::popupWidgets->removeAll(popup);

    // Cancel pointer capture and long tap for this popup
    WId id = popup->effectiveWinId();
    id->SetPointerCapture(false);
    static_cast<QSymbianControl*>(id)->CancelLongTapTimer();

    if (QApplicationPrivate::popupWidgets->isEmpty()) { // this was the last popup
        delete QApplicationPrivate::popupWidgets;
        QApplicationPrivate::popupWidgets = 0;
        if (!qt_nograb()) {                        // grabbing not disabled
            Q_ASSERT(popup->testAttribute(Qt::WA_WState_Created));
            if (QWidgetPrivate::mouseGrabber != 0)
                QWidgetPrivate::mouseGrabber->grabMouse();

            if (QWidgetPrivate::keyboardGrabber != 0)
                QWidgetPrivate::keyboardGrabber->grabKeyboard();

        QWidget *fw = QApplicationPrivate::active_window ? QApplicationPrivate::active_window->focusWidget()
              : q_func()->focusWidget();
          if (fw) {
              if(fw->window()->isModal()) // restore pointer capture for modal window
                  fw->effectiveWinId()->SetPointerCapture(true);

              if (fw != q_func()->focusWidget()) {
                  fw->setFocus(Qt::PopupFocusReason);
              } else {
                  QFocusEvent e(QEvent::FocusIn, Qt::PopupFocusReason);
                  q_func()->sendEvent(fw, &e);
              }
          }
        }
    } else {

        // popups are not focus-handled by the window system (the
        // first popup grabbed the keyboard), so we have to do that
        // manually: A popup was closed, so the previous popup gets
        // the focus.
        QWidget* aw = QApplicationPrivate::popupWidgets->last();
        if (QWidget *fw = QApplication::focusWidget()) {
            QFocusEvent e(QEvent::FocusOut, Qt::PopupFocusReason);
            q_func()->sendEvent(fw, &e);
        }

        // Enable pointer capture for previous popup
        if (aw) {
            aw->effectiveWinId()->SetPointerCapture(true);
        }
    }
}

QWidget * QApplication::topLevelAt(QPoint const& point)
{
    QWidget *found = 0;
    int lowestZ = INT_MAX;
    QWidgetList list = QApplication::topLevelWidgets();
    for (int i = 0; i < list.count(); ++i) {
        QWidget *widget = list.at(i);
        if (widget->isVisible() && !(widget->windowType() == Qt::Desktop)) {
            Q_ASSERT(widget->testAttribute(Qt::WA_WState_Created));
            if (widget->geometry().adjusted(0,0,1,1).contains(point)) {
                // At this point we know there is a Qt widget under the point.
                // Now we need to make sure it is the top most in the z-order.
                RDrawableWindow *const window = widget->effectiveWinId()->DrawableWindow();
                int z = window->OrdinalPosition();
                if (z < lowestZ) {
                    lowestZ = z;
                    found = widget;
                }
            }
        }
    }
    return found;
}

void QApplication::alert(QWidget * /* widget */, int /* duration */)
{
    // TODO: Implement QApplication::alert(QWidget *widget, int duration)
}

int QApplication::doubleClickInterval()
{
    TTimeIntervalMicroSeconds32 us;
    TInt distance;
    S60->wsSession().GetDoubleClickSettings(us, distance);
    return (us.Int() / 1000);
}

void QApplication::setDoubleClickInterval(int ms)
{
    TTimeIntervalMicroSeconds32 newUs( ms * 1000);
    TTimeIntervalMicroSeconds32 us;
    TInt distance;
    S60->wsSession().GetDoubleClickSettings(us, distance);
    if (us != newUs)
        S60->wsSession().SetDoubleClick(newUs, distance);
}

int QApplication::keyboardInputInterval()
{
    return QApplicationPrivate::keyboard_input_time;
}

void QApplication::setKeyboardInputInterval(int ms)
{
    QApplicationPrivate::keyboard_input_time = ms;
}

int QApplication::cursorFlashTime()
{
    return QApplicationPrivate::cursor_flash_time;
}

void QApplication::setCursorFlashTime(int msecs)
{
    QApplicationPrivate::cursor_flash_time = msecs;
}

void QApplication::beep()
{
    if (!qt_S60Beep) {
        TInt frequency = 880;
        TTimeIntervalMicroSeconds duration(500000);
        TRAP_IGNORE(qt_S60Beep=QS60Beep::NewL(frequency, duration));
    }
    if (qt_S60Beep)
        qt_S60Beep->Play();
}

static inline bool callSymbianEventFilters(const QSymbianEvent *event)
{
    long unused;
    return qApp->filterEvent(const_cast<QSymbianEvent *>(event), &unused);
}

/*!
    \warning This function is only available on Symbian.
    \since 4.6

    This function processes an individual Symbian event
    \a event. It returns 1 if the event was handled, 0 if
    the \a event was not handled, and -1 if the event was
    not handled because the event is not known to Qt.
 */

int QApplication::symbianProcessEvent(const QSymbianEvent *event)
{
    Q_D(QApplication);

    QScopedLoopLevelCounter counter(d->threadData);

    if (d->eventDispatcher->filterEvent(const_cast<QSymbianEvent *>(event)))
        return 1;

    QWidget *w = qApp ? qApp->focusWidget() : 0;
    if (w) {
        QInputContext *ic = w->inputContext();
        if (ic && ic->symbianFilterEvent(w, event))
            return 1;
    }

    if (symbianEventFilter(event))
        return 1;

    switch (event->type()) {
    case QSymbianEvent::WindowServerEvent:
        return d->symbianProcessWsEvent(event);
    case QSymbianEvent::CommandEvent:
        return d->symbianHandleCommand(event);
    case QSymbianEvent::ResourceChangeEvent:
        return d->symbianResourceChange(event);
    default:
        return -1;
    }
}

int QApplicationPrivate::symbianProcessWsEvent(const QSymbianEvent *symbianEvent)
{
    // Qt event handling. Handle some events regardless of if the handle is in our
    // widget map or not.
    const TWsEvent *event = symbianEvent->windowServerEvent();
    CCoeControl* control = reinterpret_cast<CCoeControl*>(event->Handle());
    const bool controlInMap = QWidgetPrivate::mapper && QWidgetPrivate::mapper->contains(control);
    switch (event->Type()) {
    case EEventPointerEnter:
        if (controlInMap) {
            callSymbianEventFilters(symbianEvent);
            return 1; // Qt::Enter will be generated in HandlePointerL
        }
        break;
    case EEventPointerExit:
        if (controlInMap) {
            if (callSymbianEventFilters(symbianEvent))
                return 1;
            if (S60) {
                // mouseEvent outside our window, send leave event to last focused widget
                QMouseEvent mEvent(QEvent::Leave, S60->lastPointerEventPos, S60->lastCursorPos,
                    Qt::NoButton, QApplicationPrivate::mouse_buttons, Qt::NoModifier);
                if (S60->lastPointerEventTarget)
                    qt_sendSpontaneousEvent(S60->lastPointerEventTarget,&mEvent);
                S60->lastPointerEventTarget = 0;
            }
            return 1;
        }
        break;
    case EEventScreenDeviceChanged:
        if (callSymbianEventFilters(symbianEvent))
            return 1;
        if (S60)
            S60->updateScreenSize();
        if (qt_desktopWidget) {
            QSize oldSize = qt_desktopWidget->size();
            qt_desktopWidget->data->crect.setWidth(S60->screenWidthInPixels);
            qt_desktopWidget->data->crect.setHeight(S60->screenHeightInPixels);
            QResizeEvent e(qt_desktopWidget->size(), oldSize);
            QApplication::sendEvent(qt_desktopWidget, &e);
        }
        return 0; // Propagate to CONE
    case EEventWindowVisibilityChanged:
        if (controlInMap) {
            if (callSymbianEventFilters(symbianEvent))
                return 1;
            const TWsVisibilityChangedEvent *visChangedEvent = event->VisibilityChanged();
            if (visChangedEvent->iFlags & TWsVisibilityChangedEvent::ENotVisible)
                S60->controlVisibilityChanged(control, false);
            else if (visChangedEvent->iFlags & TWsVisibilityChangedEvent::EPartiallyVisible)
                S60->controlVisibilityChanged(control, true);
            return 1;
        }
        break;
    case EEventFocusGained:
        if (callSymbianEventFilters(symbianEvent))
            return 1;
#ifndef QT_NO_CURSOR
        //re-enable mouse interaction
        if (S60->mouseInteractionEnabled) {
#ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS
            if (S60->brokenPointerCursors)
                qt_symbian_show_pointer_sprite();
            else
#endif
                S60->wsSession().SetPointerCursorMode(EPointerCursorNormal);
        }
#endif
        break;
    case EEventFocusLost:
        if (callSymbianEventFilters(symbianEvent))
            return 1;
#ifndef QT_NO_CURSOR
        //disable mouse as may be moving to application that does not support it
        if (S60->mouseInteractionEnabled) {
#ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS
            if (S60->brokenPointerCursors)
                qt_symbian_hide_pointer_sprite();
            else
#endif
                S60->wsSession().SetPointerCursorMode(EPointerCursorNone);
        }
#endif
        break;
    case KGoomMemoryLowEvent:
#ifdef QT_DEBUG
        qDebug() << "QApplicationPrivate::symbianProcessWsEvent - KGoomMemoryLowEvent";
#endif
        if (callSymbianEventFilters(symbianEvent))
            return 1;
#ifdef QT_GRAPHICSSYSTEM_RUNTIME
        if(QApplicationPrivate::runtime_graphics_system) {
            bool switchToSwRendering(false);

            foreach (QWidget *w, QApplication::topLevelWidgets()) {
                if(w->d_func()->topData()->backingStore) {
                    switchToSwRendering = true;
                    break;
                }
            }

            if (switchToSwRendering) {
                QRuntimeGraphicsSystem *gs =
                   static_cast<QRuntimeGraphicsSystem*>(QApplicationPrivate::graphics_system);
                gs->setGraphicsSystem(QLatin1String("raster"));
            }
        }
#endif
        break;
    case KGoomMemoryGoodEvent:
#ifdef QT_DEBUG
        qDebug() << "QApplicationPrivate::symbianProcessWsEvent - KGoomMemoryGoodEvent";
#endif
        if (callSymbianEventFilters(symbianEvent))
            return 1;
#ifdef QT_GRAPHICSSYSTEM_RUNTIME
        if(QApplicationPrivate::runtime_graphics_system) {
            QRuntimeGraphicsSystem *gs =
                   static_cast<QRuntimeGraphicsSystem*>(QApplicationPrivate::graphics_system);
            gs->setGraphicsSystem(QLatin1String("openvg"));
        }
#endif
        break;
    default:
        break;
    }

    if (!controlInMap)
        return -1;

    return 0;
}

/*!
  \warning This virtual function is only available on Symbian.
  \since 4.6

  If you create an application that inherits QApplication and reimplement
  this function, you get direct access to events that the are received
  from Symbian. The events are passed in the \a event parameter.

  Return true if you want to stop the event from being processed. Return
  false for normal event dispatching. The default implementation returns
  false, and does nothing with \a event.
 */
bool QApplication::symbianEventFilter(const QSymbianEvent *event)
{
    Q_UNUSED(event);
    return false;
}

/*!
  \warning This function is only available on Symbian.
  \since 4.6

  Handles \a{command}s which are typically handled by
  CAknAppUi::HandleCommandL(). Qts Ui integration into Symbian is
  partially achieved by deriving from CAknAppUi. Currently, exit,
  menu and softkey commands are handled.

  \sa s60EventFilter(), s60ProcessEvent()
*/
int QApplicationPrivate::symbianHandleCommand(const QSymbianEvent *symbianEvent)
{
    Q_Q(QApplication);
    int ret = 0;

    if (callSymbianEventFilters(symbianEvent))
        return 1;

    int command = symbianEvent->command();

    switch (command) {
#ifdef Q_WS_S60
    case EAknSoftkeyExit: {
        QCloseEvent ev;
        QApplication::sendSpontaneousEvent(q, &ev);
        if (ev.isAccepted()) {
            q->quit();
            ret = 1;
        }
        break;
    }
#endif
    case EEikCmdExit:
        q->quit();
        ret = 1;
        break;
    default:
#ifdef Q_WS_S60
        bool handled = QSoftKeyManager::handleCommand(command);
        if (handled)
            ret = 1;
        else
            ret = QMenuBarPrivate::symbianCommands(command);
#endif
        break;
    }

    return ret;
}

/*!
  \warning This function is only available on Symbian.
  \since 4.6

  Handles the resource change specified by \a type.

  Currently, KEikDynamicLayoutVariantSwitch and
  KAknsMessageSkinChange are handled.
 */
int QApplicationPrivate::symbianResourceChange(const QSymbianEvent *symbianEvent)
{
    int ret = 0;

    int type = symbianEvent->resourceChangeType();

    switch (type) {
#ifdef Q_WS_S60
    case KEikDynamicLayoutVariantSwitch:
        {
        if (callSymbianEventFilters(symbianEvent))
            return 1;
        if (S60)
            S60->updateScreenSize();

#ifndef QT_NO_STYLE_S60
        QS60Style *s60Style = 0;

#ifndef QT_NO_STYLE_STYLESHEET
        QStyleSheetStyle *proxy = qobject_cast<QStyleSheetStyle*>(QApplication::style());
        if (proxy)
            s60Style = qobject_cast<QS60Style*>(proxy->baseStyle());
        else
#endif
            s60Style = qobject_cast<QS60Style*>(QApplication::style());

        if (s60Style) {
            s60Style->d_func()->handleDynamicLayoutVariantSwitch();
            ret = 1;
        }
#endif
        }
        break;

#ifndef QT_NO_STYLE_S60
    case KAknsMessageSkinChange:
        if (callSymbianEventFilters(symbianEvent))
            return 1;
        if (QS60Style *s60Style = qobject_cast<QS60Style*>(QApplication::style())) {
            s60Style->d_func()->handleSkinChange();
            ret = 1;
        }
        break;
#endif
#endif // Q_WS_S60
    default:
        break;
    }

    return ret;
}

#ifndef QT_NO_WHEELEVENT
int QApplication::wheelScrollLines()
{
    return QApplicationPrivate::wheel_scroll_lines;
}

void QApplication::setWheelScrollLines(int n)
{
    QApplicationPrivate::wheel_scroll_lines = n;
}
#endif //QT_NO_WHEELEVENT

bool QApplication::isEffectEnabled(Qt::UIEffect /* effect */)
{
    // TODO: Implement QApplication::isEffectEnabled(Qt::UIEffect effect)
    return false;
}

void QApplication::setEffectEnabled(Qt::UIEffect /* effect */, bool /* enable */)
{
    // TODO: Implement QApplication::setEffectEnabled(Qt::UIEffect effect, bool enable)
}

TUint QApplicationPrivate::resolveS60ScanCode(TInt scanCode, TUint keysym)
{
    if (!scanCode)
        return keysym;

    QApplicationPrivate *d = QApplicationPrivate::instance();

    if (keysym) {
        // If keysym is specified, cache it.
        d->scanCodeCache.insert(scanCode, keysym);
        return keysym;
    } else {
        // If not, retrieve the cached version.
        return d->scanCodeCache[scanCode];
    }
}

void QApplicationPrivate::initializeMultitouch_sys()
{
#ifdef QT_SYMBIAN_SUPPORTS_ADVANCED_POINTER
    if (HAL::Get(HALData::EPointer3DPressureSupported, pressureSupported) != KErrNone)
        pressureSupported = 0;
    if (HAL::Get(HALData::EPointer3DMaxPressure, maxTouchPressure) != KErrNone)
        maxTouchPressure = KMaxTInt;
#else
    pressureSupported = 0;
    maxTouchPressure = KMaxTInt;
#endif
}

void QApplicationPrivate::cleanupMultitouch_sys()
{ }

#ifndef QT_NO_SESSIONMANAGER
QSessionManager::QSessionManager(QApplication * /* app */, QString & /* id */, QString& /* key */)
{

}

QSessionManager::~QSessionManager()
{

}

bool QSessionManager::allowsInteraction()
{
    return false;
}

void QSessionManager::cancel()
{

}
#endif //QT_NO_SESSIONMANAGER

#ifdef QT_KEYPAD_NAVIGATION
/*
 * Show/Hide the mouse cursor depending on phone type and chosen mode
 */
void QApplicationPrivate::setNavigationMode(Qt::NavigationMode mode)
{
#ifndef QT_NO_CURSOR
    const bool wasCursorOn = (QApplicationPrivate::navigationMode == Qt::NavigationModeCursorAuto
        && !S60->hasTouchscreen)
        || QApplicationPrivate::navigationMode == Qt::NavigationModeCursorForceVisible;
    const bool isCursorOn = (mode == Qt::NavigationModeCursorAuto
        && !S60->hasTouchscreen)
        || mode == Qt::NavigationModeCursorForceVisible;

    if (!wasCursorOn && isCursorOn) {
        //Show the cursor, when changing from another mode to cursor mode
        qt_symbian_set_cursor_visible(true);
    }
    else if (wasCursorOn && !isCursorOn) {
        //Hide the cursor, when leaving cursor mode
        qt_symbian_set_cursor_visible(false);
    }
#endif
    QApplicationPrivate::navigationMode = mode;
}
#endif

#ifndef QT_NO_CURSOR
/*****************************************************************************
 QApplication cursor stack
 *****************************************************************************/

void QApplication::setOverrideCursor(const QCursor &cursor)
{
    qApp->d_func()->cursor_list.prepend(cursor);
    qt_symbian_setGlobalCursor(cursor);
}

void QApplication::restoreOverrideCursor()
{
    if (qApp->d_func()->cursor_list.isEmpty())
        return;
    qApp->d_func()->cursor_list.removeFirst();

    if (!qApp->d_func()->cursor_list.isEmpty()) {
        qt_symbian_setGlobalCursor(qApp->d_func()->cursor_list.first());
    }
    else {
        //determine which widget has focus
        QWidget *w = QApplication::widgetAt(QCursor::pos());
#ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS
        if (S60->brokenPointerCursors) {
            qt_symbian_set_pointer_sprite(w ? w->cursor() : Qt::ArrowCursor);
        }
        else
#endif
        {
            //because of the internals of window server, we need to force the cursor
            //to be set in all child windows too, otherwise when the cursor is over
            //the child window it may show a widget cursor or arrow cursor instead,
            //depending on construction order.
            QListIterator<WId> iter(QWidgetPrivate::mapper->uniqueKeys());
            while (iter.hasNext()) {
                CCoeControl *ctrl = iter.next();
                if(ctrl->OwnsWindow()) {
                    ctrl->DrawableWindow()->ClearPointerCursor();
                }
            }
            if (w)
                qt_symbian_setWindowCursor(w->cursor(), w->effectiveWinId());
            else
                qt_symbian_setWindowGroupCursor(Qt::ArrowCursor, S60->windowGroup());
        }
    }
}

#endif // QT_NO_CURSOR

void QApplicationPrivate::_q_aboutToQuit()
{
#ifdef SYMBIAN_GRAPHICS_WSERV_QT_EFFECTS
    // Send the shutdown tfx command
    S60->wsSession().SendEffectCommand(ETfxCmdAppShutDown);
#endif
}

QS60ThreadLocalData::QS60ThreadLocalData()
{
    CCoeEnv *env = CCoeEnv::Static();
    if (env) {
        //if this is the UI thread, share objects owned by CONE
        usingCONEinstances = true;
        wsSession = env->WsSession();
        screenDevice = env->ScreenDevice();
    }
    else {
        usingCONEinstances = false;
        qt_symbian_throwIfError(wsSession.Connect(qt_s60GetRFs()));
        screenDevice = new CWsScreenDevice(wsSession);
        screenDevice->Construct();
    }
}

QS60ThreadLocalData::~QS60ThreadLocalData()
{
    if (!usingCONEinstances) {
        delete screenDevice;
        wsSession.Close();
    }
}

QT_END_NAMESPACE