widget_api.cpp 80.9 KB
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 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *   widget_api.c
 *
 * Project:
 * --------
 *   Maui_Software
 *
 * Description:
 * ------------
 *   This file implements binding utilities for widget delegator and widget runtime
 *
 * Author:
 * -------
 * -------
 *
 *==============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *==============================================================================
 *******************************************************************************/

extern "C"
{
//#include "kal_release.h"        /* Basic data type */

#include "stack_common.h"
#include "stack_msgs.h"
#include "app_ltlcom.h" /* Task message communiction */
//#include "app_buff_alloc.h"

//#include "syscomp_config.h"
#include "task_config.h"        /* Task creation */
//#include "custom_config.h"

//#include "mmi_include.h"
#include "kal_general_types.h"
#include "stack_config.h"
#include "stack_ltlcom.h"
#include "MMIDataType.h"
#include "sim_common_enums.h"
#include "string.h"
}

#include "MMI_features.h"
#ifdef __MMI_GADGET_SUPPORT__

extern "C"
{
#include "ImeiSrvGprot.h"
#include "simctrlsrvgprot.h"
//#include "nwnamesrvgprot.h"
#include "nwinfosrvgprot.h"
#include "cphssrvgprot.h"
#include "conversions.h"
//#include "commonscreens.h"
//#include "commonscreensprot.h"
//#include "simdetectionresdef.h"
#include "idlegprot.h"
#ifdef __MMI_URI_AGENT__
#include "UriAgentSrvGprot.h"
#endif /*__MMI_URI_AGENT__*/
#include "widget_adp_struct.h"
#include "widget_api.h"
#include "mmi_rp_app_widget_delegator_def.h"
#include "gadget_adp_audio.h"
#include "custom_gadget_config.h"
#include "mmi_rp_app_wgtmgr_def.h"
#include "WgtMgrSrvGprot.h"
#include "SmsSrvGprot.h"
#include "FileMgrSrvGprot.h"
#include "ModeSwitchSrvGProt.h"

#include "CommonScreensResDef.h"
#include "AlertScreen.h"
#include "kal_public_api.h"
#include "mmi_frm_mem_gprot.h"
#include "CustDataRes.h"
#include "mmi_frm_events_gprot.h"
#include "Unicodexdcl.h"
#include "app_mem.h"
#include "med_utility.h"
#include "GlobalConstants.h"
#include "fs_type.h"
#include "fs_func.h"
#include "PhbSrvGprot.h"
#include "app_str.h"

//#include "gadget_adp_log.h"
#include "vrt_system.h"     /* Add for calling vrt related function which used to adjust framerate */

#ifdef __MMI_USB_SUPPORT__
#include "mmi_rp_app_usbsrv_def.h"
#endif
}

#include "vadp_p2v_hs.h"
#include "vapp_hs_def.h"
#include "vapp_hs_widget_delegator.h"

#ifdef __MTK_INTERNAL__
/* under construction !*/
/* under construction !*/
#endif /*__MTK_INTERNAL__ */

extern void gadget_adp_add_input_event(int id, void *view_handle, int type, int keycode, int x, int y);
extern "C" mmi_ret srv_wgtmgr_usb_notify_hdlr(mmi_event_struct *param);

extern "C" void widget_handle_browse_image_req(void *param);
extern "C" void widget_handle_capture_image_req(void *param);
static void widget_load_widget_response_handler(void *param);
static void widget_notify_content_update(void *param);
static void widget_notify_size_update(void *param);
static void widget_handle_display_dialog_ind(void *param);
static void widget_handle_open_url_ind(void *param);
static void widget_send_sms(WCHAR* number, WCHAR* data, void* user_data);
static void widget_send_sms_rsp(void *user_data, int ret);
static void widget_abort_all_sms_req(void);
int g_confirm_dialog_gid;
void *g_widget_asm = NULL;
widget_sending_sms_struct *g_widget_sms_list = NULL;
widget_contact_op_struct *g_widget_contact_op_list = NULL;


/* For Gadget and LiveWallpaper co-existence case */
kal_mutexid active_mode_mutex = NULL;
bool g_widget_active = false;
bool g_widget_active_mode_entered = false;

static void send_message(void *local_para_ptr, void *peer_buff_ptr, msg_type ilm_id, module_type dst_id)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    ilm_struct *ilm_ptr = NULL;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ilm_ptr = allocate_ilm(MOD_MMI);
    ilm_ptr->local_para_ptr = (local_para_struct*) local_para_ptr;
    ilm_ptr->msg_id = ilm_id;
    ilm_ptr->peer_buff_ptr = (peer_buff_struct*) peer_buff_ptr;

    SEND_ILM(MOD_MMI, dst_id, MMI_GADGET_SAP, ilm_ptr);
}


static void widget_handle_query_device_info_ind(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_query_device_info_cnf_struct *p = (widget_query_device_info_cnf_struct*) construct_local_para(
                                                sizeof(widget_query_device_info_cnf_struct),
                                                TD_RESET);

    mmi_sim_enum sim = (mmi_sim_enum)srv_sim_ctrl_get_one_available_sim();
    srv_nw_info_location_info_struct location_info = {0};

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    /* IMEI */
    srv_imei_get_imei((sim == MMI_SIM2) ? MMI_SIM2 : MMI_SIM1, p->imei, sizeof(p->imei));

    if (sim > 0)
    {
        /* IMSI */
        srv_sim_ctrl_get_imsi(sim, p->imsi, sizeof(p->imsi));

        /* MSISDN */
        {
            const U16 *out = srv_cphs_get_msisdn_number(SRV_CPHS_MSISDN_TYPE_LINE1, sim);

            if (out)
            {
                mmi_chset_ucs2_to_utf8_string((kal_uint8 *) p->msisdn, sizeof(p->msisdn), (kal_uint8 *)out);
            }
        }

        /* Cell ID, MNC, MCC */
        if(srv_nw_info_get_location_info(sim, &location_info))
        {
            p->cell_id = location_info.cell_id;

            memcpy(p->network_mnc, location_info.plmn + SRV_MCC_LEN, sizeof(p->network_mnc));
            memcpy(p->network_mcc, location_info.plmn, (sizeof(p->network_mcc) - 1));
        }
    }

    send_message(p, NULL, MSG_ID_WIDGET_QUERY_DEVICE_INFO_CNF, MOD_GADGET);

}


static void widget_handle_terminate_widget_ind(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_terminate_wgt_ind_struct *p = (widget_terminate_wgt_ind_struct*)param;

    VappHsWidgetDelegator *widget = VappHsWidgetDelegator::findByInstancId(p->wgt_id);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!widget)
    {
        return;
    }

    widget->onTerminate();
}

static void widget_handle_terminate_all_ind(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_terminate_all_wgt_ind_struct *ind = (widget_terminate_all_wgt_ind_struct *)param;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (ind->type == WIDGET_RESET_MEMORY_FULL)
    {
        /* reset all widget only when out of memory */
        vadp_p2v_hs_widget_reset(VAPP_HS_WIDGET_TYPE_GADGET, STR_ID_WIDGET_MEMORY_FULL);
    }
    else if (ind->type == WIDGET_RESET_NO_POPUP)
    {
        vadp_p2v_hs_widget_reset(VAPP_HS_WIDGET_TYPE_GADGET, 0);
    }

    send_message(NULL, NULL, MSG_ID_WIDGET_TERMINATE_ALL_WGT_CNF, MOD_GADGET);
}



static void widget_send_check_network_access_response(int response)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_check_network_access_cnf_struct *p = (widget_check_network_access_cnf_struct*) construct_local_para(
                                                sizeof(widget_check_network_access_cnf_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    p->response = response; ;

    send_message(p, NULL, MSG_ID_WIDGET_CHECK_NETWORK_ACCESS_CNF, MOD_GADGET);
}



static mmi_ret widget_check_network_access_dialog_callback(mmi_event_struct *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (param->evt_id == EVT_ID_POPUP_QUIT)
    {
        switch(((mmi_alert_result_evt_struct *)param)->result)
        {
        case MMI_ALERT_CNFM_YES:
            widget_send_check_network_access_response(WIDGET_DLG_RESPONSE_YES);
            break;
        case MMI_ALERT_CNFM_NO:
            widget_send_check_network_access_response(WIDGET_DLG_RESPONSE_NO);
            break;
        case MMI_ALERT_CNFM_CANCEL:
            widget_send_check_network_access_response(WIDGET_DLG_RESPONSE_CANCEL);
            break;
        default:
            return MMI_RET_OK;
        }
        g_confirm_dialog_gid = 0;
    }

    return MMI_RET_OK;
}


static mmi_ret widget_check_sms_access_callback(mmi_event_struct *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (param->evt_id == EVT_ID_POPUP_QUIT)
    {
        mmi_alert_result_evt_struct *evt = (mmi_alert_result_evt_struct *)param;
        widget_sms_req_struct *req = (widget_sms_req_struct *)evt->user_tag;
        ASSERT(req);

        switch(evt->result)
        {
        case MMI_ALERT_CNFM_YES:
            widget_send_sms(req->number, req->data, req->user_data);
            break;

        case MMI_ALERT_CNFM_NO:
        case MMI_ALERT_CNFM_CANCEL:
            widget_send_sms_rsp(req->user_data, WIDGET_SMS_NOT_ALLOWED);
            break;
        default:
            return MMI_RET_OK;
        }
        g_confirm_dialog_gid = 0;
        OslMfree(req);
    }

    return MMI_RET_OK;
}


static void widget_handle_check_network_access_ind(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_confirm_property_struct arg;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_confirm_property_init(&arg, CNFM_TYPE_YESNO);

    if (mmi_frm_group_get_active_id() != GRP_ID_WIDGET_VIEW)
    {
        arg.parent_id = mmi_idle_get_group_id();
    }
    else
    {
        arg.parent_id = GRP_ID_WIDGET_VIEW;
    }

    arg.f_enter_history = 1;
    arg.callback = widget_check_network_access_dialog_callback;

    /* This is a dirty fix to let widget delegator know it is an interrupt raised by gadget engine.
       The function of wiget delegator might get called during mmi_confirm_display and
       g_confirm_dialog_gid is still 0 at that moment .*/
    g_confirm_dialog_gid = -1;

    g_confirm_dialog_gid = mmi_confirm_display((WCHAR *)GetString(STR_ID_WIDGET_ALLOW_NETWORK_ACCESS), MMI_EVENT_QUERY, &arg);

}


static void widget_check_access(mmi_id str_id, mmi_proc_func callback, void *user_data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_confirm_property_struct arg;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_confirm_property_init(&arg, CNFM_TYPE_YESNO);

    if (mmi_frm_group_get_active_id() != GRP_ID_WIDGET_VIEW)
    {
        arg.parent_id = mmi_idle_get_group_id();
    }
    else
    {
        arg.parent_id = GRP_ID_WIDGET_VIEW;
    }

    arg.f_enter_history = 1;
    arg.callback = callback;
    arg.user_tag = user_data;

    /* This is a dirty fix to let widget delegator know it is an interrupt raised by gadget engine.
       The function of wiget delegator might get called during mmi_confirm_display and
       g_confirm_dialog_gid is still 0 at that moment .*/
    g_confirm_dialog_gid = -1;
    g_confirm_dialog_gid = mmi_confirm_display((WCHAR *)GetString(str_id), MMI_EVENT_QUERY, &arg);
}


static void widget_send_sms_callback(srv_sms_callback_struct* data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_send_sms_rsp_struct *rsp = (widget_send_sms_rsp_struct *)construct_local_para(
                                                sizeof(widget_send_sms_rsp_struct),
                                                TD_RESET);
    widget_sending_sms_struct *temp = g_widget_sms_list;
    widget_sending_sms_struct *prev = NULL;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (data->result == MMI_TRUE)
    {
        rsp->ret = WIDGET_SMS_OK;
    }
    else
    {
        rsp->ret = data->cause;
    }
    rsp->user_data = data->user_data;

    while (temp != NULL)
    {
        if (temp->user_data == data->user_data)
        {
            if (prev != NULL)
            {
                prev->next = temp->next;
            }
            else
            {
                g_widget_sms_list = temp->next;
            }
            OslMfree(temp);
            break;
        }
        prev = temp;
        temp = temp->next;
    }

    send_message(rsp, NULL, MSG_ID_WIDGET_SEND_SMS_RSP, MOD_GADGET);
}


static void widget_send_sms(WCHAR* number, WCHAR* data, void* user_data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_sim_enum sim = (mmi_sim_enum)srv_sim_ctrl_get_one_available_sim();
    srv_sms_sim_enum sms_sim= SRV_SMS_SIM_1;
    SMS_HANDLE sms_handle;
    U16 content_size;
    widget_sending_sms_struct *new_node;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (sim == MMI_SIM1 && srv_mode_switch_get_network_mode(MMI_SIM1) == SRV_MODE_SWITCH_OFF)
    {
        sim = MMI_SIM2;
    }

    if (sim == MMI_SIM2)
    {
        sms_sim = SRV_SMS_SIM_2;
    }

    sms_handle = srv_sms_get_send_handle();

    /* insert a new node to sms list */
    new_node = (widget_sending_sms_struct *)OslMalloc(sizeof(widget_sending_sms_struct));
    new_node->next = g_widget_sms_list;
    g_widget_sms_list = new_node;
    new_node->sms_handle = sms_handle;
    new_node->user_data = user_data;

    /* set related info and send */
    srv_sms_set_address(sms_handle, (char*)number);
    srv_sms_set_sim_id(sms_handle, sms_sim);
    srv_sms_set_content_dcs(sms_handle, SRV_SMS_DCS_UCS2);
    content_size = mmi_ucs2strlen((const CHAR*)data) * ENCODING_LENGTH;
    srv_sms_set_content(sms_handle, (char*)data, content_size);
    srv_sms_send_msg(sms_handle, widget_send_sms_callback, user_data);
}


static void widget_abort_all_sms_req(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_sending_sms_struct *next = g_widget_sms_list;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (next != NULL)
    {
        srv_sms_abort_send(next->sms_handle);
        next = next->next;
    }
}


static void widget_send_sms_rsp(void *user_data, int ret)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_send_sms_rsp_struct *rsp = NULL;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    rsp = (widget_send_sms_rsp_struct *)construct_local_para(
                                         sizeof(widget_send_sms_rsp_struct),
                                         TD_RESET);

    rsp->ret = ret;
    rsp->user_data = user_data;
    send_message(rsp, NULL, MSG_ID_WIDGET_SEND_SMS_RSP, MOD_GADGET);
}



static void widget_send_sms_req_hdlr(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_send_sms_req_struct *req = (widget_send_sms_req_struct*)param;
    widget_sms_req_struct *sms = NULL;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (srv_sms_is_sms_valid() == MMI_FALSE)
    {
        widget_send_sms_rsp(req->user_data, WIDGET_SMS_NOT_AVAILABLE);
    }
    else if (srv_sms_is_sms_ready() == MMI_FALSE)
    {
        widget_send_sms_rsp(req->user_data, SRV_SMS_CAUSE_NOT_READY);
    }
    else if (srv_sms_check_ucs2_number((char*)req->number) == MMI_FALSE)
    {
        widget_send_sms_rsp(req->user_data, SRV_SMS_CAUSE_NUMBER_INVALID);
    }
    else
    {
        sms = (widget_sms_req_struct *)OslMalloc(sizeof(widget_sms_req_struct));
        sms->user_data = req->user_data;
        kal_mem_cpy(sms->data, req->data, sizeof(sms->data));
        kal_mem_cpy(sms->number, req->number, sizeof(sms->number));
        widget_check_access(STR_ID_WIDGET_ALLOW_SEND_SMS, widget_check_sms_access_callback, sms);
    }
}

static void widget_send_contact_op_rsp(void *req, int ret, int index, void *info)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_contact_general_op_finished_ind_struct *ind = NULL;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ind = (widget_contact_general_op_finished_ind_struct *)construct_local_para(
                                         sizeof(widget_contact_general_op_finished_ind_struct),
                                         TD_RESET);

    ind->ret = ret;
    ind->req = req;
    ind->contact_info = info;
    ind->store_index = index;
    send_message(ind, NULL, MSG_ID_WIDGET_CONTACT_GENERAL_OP_FINISHED_IND, MOD_GADGET);
}

void widget_contact_callback_func(S32 result, U16 store_index, void *user_data)
{

    widget_contact_op_struct *temp = g_widget_contact_op_list;
    widget_contact_op_struct *prev = NULL;
    void *contact_info = NULL;
    while (temp != NULL)
    {
        if (temp->user_data == user_data)
        {
            if (prev != NULL)
            {
                prev->next = temp->next;
            }
            else
            {
                g_widget_contact_op_list = temp->next;
            }
            contact_info = temp->info;
            break;
        }
        prev = temp;
        temp = temp->next;
    }
    if (result == SRV_PHB_NO_ERROR)
    {
        widget_send_contact_op_rsp(user_data, 1, store_index, contact_info);
    }
    else
    {
        widget_send_contact_op_rsp(user_data, 0, NULL, contact_info);
    }

}

static void widget_commit_contact_req_hdlr(void *param)
{
    widget_commit_contact_req_struct *req = (widget_commit_contact_req_struct*)param;

    if (srv_phb_check_storage_accessible(req->storage) == MMI_TRUE)
    {
        if (req->store_index != -1)
        {
            srv_phb_oplib_update_contact(req->store_index , (srv_phb_contact_info_struct *)req->contact_info, widget_contact_callback_func, req->user_data);
        }
        else
        {
            srv_phb_oplib_add_contact(req->storage, SRV_PHB_INVALID_INDEX, (srv_phb_contact_info_struct *)req->contact_info, widget_contact_callback_func, req->user_data);
        }
        widget_contact_op_struct *node = (widget_contact_op_struct *)OslMalloc(sizeof(widget_contact_op_struct));
        node->info = req->contact_info;
        node->user_data = req->user_data;
        node->next = g_widget_contact_op_list;
        g_widget_contact_op_list = node;

    }
    else
    {
        widget_send_contact_op_rsp(req->user_data, 0, 0, NULL);
    }
}


static void widget_delete_contact_req_hdlr(void *param)
{
    widget_delete_contact_req_struct *req = (widget_delete_contact_req_struct*)param;

    if (srv_phb_check_storage_accessible(req->storage) == MMI_TRUE)
    {
        srv_phb_oplib_delete_contact(req->store_index, widget_contact_callback_func, req->user_data);
    }
    else
    {
        widget_send_contact_op_rsp(req->user_data, 0, 0, NULL);
    }
}

static void widget_find_contacts_req_hdlr(void *param)
{
    widget_find_contacts_req_struct *req = (widget_find_contacts_req_struct*)param;
    widget_find_contacts_rsp_struct *rsp = (widget_find_contacts_rsp_struct *)construct_local_para(
                                         sizeof(widget_find_contacts_rsp_struct),
                                         TD_RESET);
    if (srv_phb_check_storage_accessible(req->storage) == MMI_TRUE)
    {
        srv_phb_filter_struct filter;
        filter.group_filter = NULL;
        filter.name_filter = NULL;
        filter.qsearch_filter = NULL;
        filter.storage = req->storage;
        
        if (app_ucs2_strlen((const kal_int8 *) req->name) > 0)
        {
            filter.name_filter = (srv_phb_name_filter_struct *)OslMalloc(sizeof(srv_phb_name_filter_struct));
            int len = app_ucs2_strlen((const kal_int8 *)req->name);
            filter.name_filter->key_word = (U16 *) OslMalloc((len+1) * sizeof(U16));
            app_ucs2_strcpy((kal_int8 *)filter.name_filter->key_word, (const kal_int8 *)req->name);
            filter.field_filter = SRV_PHB_ENTRY_FIELD_NAME;
        }
        else
        {
            filter.field_filter = SRV_PHB_ENTRY_FIELD_ALL;
        }
        int contact_length = 0;
      
        contact_length = srv_phb_oplib_build_contact_list(&filter, (U16 *)req->index_array, req->array_length);
        if (filter.name_filter)
        {
            if (filter.name_filter->key_word)
            {
                OslMfree(filter.name_filter->key_word);
            }
            OslMfree(filter.name_filter);
        }
        
        rsp->ret = contact_length >= 0 ? contact_length : -1;
        rsp->user_data = req->user_data;
        send_message(rsp, NULL, MSG_ID_WIDGET_FIND_CONTACTS_RSP, MOD_GADGET);


    }
    else
    {
        rsp->ret = -1;
        rsp->user_data = req->user_data;
        send_message(rsp, NULL, MSG_ID_WIDGET_FIND_CONTACTS_RSP, MOD_GADGET);
    }
}

static void widget_get_contact_info_req_hdlr(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_get_contact_info_req_struct *req = (widget_get_contact_info_req_struct *)param;
    widget_get_contact_info_rsp_struct *rsp = (widget_get_contact_info_rsp_struct *)construct_local_para(
                                         sizeof(widget_get_contact_info_rsp_struct),
                                         TD_RESET);
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    S32 result = srv_phb_oplib_get_contact_info(req->store_index, (srv_phb_contact_info_struct *)req->info);
    rsp->ret = result;
    rsp->user_data = req->info;
    send_message(rsp, NULL, MSG_ID_WIDGET_GET_CONTACT_INFO_RSP, MOD_GADGET);


}

static void widget_load_widget_response_handler(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_load_widget_rsp_struct *p = (widget_load_widget_rsp_struct *)param;

    VappHsWidgetDelegator *widget = VappHsWidgetDelegator::findByInstancId(p->instance_id);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!widget)
    {
        return;
    }

    if(p->result == WIDGET_LOAD_OK)
    {
        mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_NOTIFY_CONTENT_UPDATE_IND, (PsIntFuncPtr)widget_notify_content_update, KAL_FALSE);
        mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_UPDATE_SIZE_IND, (PsIntFuncPtr)widget_notify_size_update, KAL_FALSE);
        mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_SHOW_VIEW_IND, (PsIntFuncPtr)widget_show_view_handler, KAL_FALSE);
        mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_ENTER_TEXT_IND, (PsIntFuncPtr)widget_handle_enter_text_ind, KAL_FALSE);
        mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_OPEN_URL_IND, (PsIntFuncPtr)widget_handle_open_url_ind, KAL_FALSE);
    }

    widget->onWidgetLoadResult(p->result, p->view_handle, p->width, p->height);
}


static void widget_handle_open_url_ind(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_open_url_ind_struct *p = (widget_open_url_ind_struct*)param;
    mmi_popup_property_struct arg;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MMI_URI_AGENT__
    if (strlen(p->url))
    {
        if (srv_uriagent_dispatch_uri_request(SRV_URIAGENT_APPID_WIDGET, p->url, SRV_URIAGENT_OPTION_UNSPECIFIED, NULL) == SRV_URIAGENT_ERR_PROCESSING)
        {          
            mmi_popup_property_init(&arg);
            
            if (mmi_frm_group_get_active_id() != GRP_ID_WIDGET_VIEW)
            {
                arg.parent_id = mmi_idle_get_group_id();
            }
            else
            {
                arg.parent_id = GRP_ID_WIDGET_VIEW;
            }
            
            mmi_popup_display((WCHAR*) ((UI_string_type) GetString(STR_ID_WIDGET_BUSY_NOW_PLEASE_TRY_LATER)), MMI_EVENT_WARNING, &arg);
        }
    }
#endif /*__MMI_URI_AGENT__*/
}


static void widget_send_notify_content_update_response(int instance_id, void *view_handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_notify_content_update_cnf_struct *p = (widget_notify_content_update_cnf_struct*) construct_local_para(
                                                sizeof(widget_notify_content_update_cnf_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    p->instance_id = instance_id;
    p->view_handle = view_handle;

    send_message(p, NULL, MSG_ID_WIDGET_NOTIFY_CONTENT_UPDATE_CNF, MOD_GADGET);
}


static void widget_notify_content_update(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_notify_content_update_ind_struct *p = (widget_notify_content_update_ind_struct*)param;

    VappHsWidgetDelegator *widget = VappHsWidgetDelegator::findByInstancId(p->instance_id);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!widget)
    {
        widget_canvas_free(p->buffer);
        goto final;
    }

    widget->onWidgetContentUpdate(p->buffer);

final:
    //gadget_adp_set_redraw_pending(p->view_handle, MMI_FALSE); /*TESTING*/
    widget_send_notify_content_update_response(p->instance_id, p->view_handle);

}

static void widget_notify_size_update(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_update_size_ind_struct *p = (widget_update_size_ind_struct*)param;

    VappHsWidgetDelegator *widget = VappHsWidgetDelegator::findByInstancId(p->instance_id);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!widget || !p->view_handle)
    {
        return;
    }

    widget->onWidgetResize(p->width, p->height);
}

static void widget_send_allocate_asm_rsp(kal_bool ret, void *mem_addr, kal_int32 size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_allocate_asm_rsp_struct *p = (widget_allocate_asm_rsp_struct*) construct_local_para(
                                                sizeof(widget_allocate_asm_rsp_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    p->ret = ret;
    p->size = size;
    p->mem_addr	 = mem_addr;
    send_message(p, NULL, MSG_ID_WIDGET_ALLOCATE_ASM_RSP, MOD_GADGET);
}


static void widget_send_dialog_response(int response)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_display_dialog_cnf_struct *p = (widget_display_dialog_cnf_struct*) construct_local_para(
                                                sizeof(widget_display_dialog_cnf_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    p->dlg_response = response; ;

    send_message(p, NULL, MSG_ID_WIDGET_DISPLAY_DIALOG_CNF, MOD_GADGET);
}


static mmi_ret widget_confirm_dialog_callback(mmi_event_struct *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (param->evt_id == EVT_ID_POPUP_QUIT)
    {
        switch(((mmi_alert_result_evt_struct *)param)->result)
        {
        case MMI_ALERT_CNFM_YES:
            widget_send_dialog_response(WIDGET_DLG_RESPONSE_YES);
            break;
        case MMI_ALERT_CNFM_NO:
            widget_send_dialog_response(WIDGET_DLG_RESPONSE_NO);
            break;
        case MMI_ALERT_CNFM_CANCEL:
            widget_send_dialog_response(WIDGET_DLG_RESPONSE_CANCEL);
            break;
        default:
            return MMI_RET_OK;
        }
        g_confirm_dialog_gid = 0;
    }

    return MMI_RET_OK;
}


static void widget_display_confirm_dialog(void *handle, const char *message)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint8 *buf;
    int buflen = (mmi_chset_utf8_strlen((kal_uint8*)message) > 100 ? 100 : mmi_chset_utf8_strlen((kal_uint8*)message));
    mmi_confirm_property_struct arg;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    buflen = (buflen + 1) * ENCODING_LENGTH;
    buf = (kal_uint8 *)OslMalloc(buflen);
    mmi_chset_utf8_to_ucs2_string(buf, buflen, (kal_uint8 *)message);

    mmi_confirm_property_init(&arg, CNFM_TYPE_YESNO);

    if (mmi_frm_group_get_active_id() != GRP_ID_WIDGET_VIEW)
    {
        arg.parent_id = mmi_idle_get_group_id();
    }
    else
    {
        arg.parent_id = GRP_ID_WIDGET_VIEW;
    }

    arg.f_enter_history = 1;
    arg.user_tag = handle;
    arg.callback = widget_confirm_dialog_callback;

    /* This is a dirty fix to let widget delegator know it is an interrupt raised by gadget engine.
       The function of wiget delegator might get called during mmi_confirm_display and
       g_confirm_dialog_gid is still 0 at that moment .*/
    g_confirm_dialog_gid = -1;

    g_confirm_dialog_gid = mmi_confirm_display((WCHAR *)buf, MMI_EVENT_QUERY, &arg);

    OslMfree(buf);
}

static void widget_display_alert_dialog(const char *message)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint8 *buf;
    int buflen = (mmi_chset_utf8_strlen((kal_uint8*)message) > 100 ? 100 : mmi_chset_utf8_strlen((kal_uint8*)message));
    mmi_popup_property_struct arg;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    buflen = (buflen + 1) * ENCODING_LENGTH;
    buf = (kal_uint8 *)OslMalloc(buflen);
    mmi_chset_utf8_to_ucs2_string(buf, buflen, (kal_uint8 *)message);

    mmi_popup_property_init(&arg);

    if (mmi_frm_group_get_active_id() != GRP_ID_WIDGET_VIEW)
    {
        arg.parent_id = mmi_idle_get_group_id();
    }
    else
    {
        arg.parent_id = GRP_ID_WIDGET_VIEW;
    }

    mmi_popup_display((WCHAR *)buf, MMI_EVENT_WARNING, &arg);

    OslMfree(buf);
}

static void widget_handle_display_dialog_ind(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_display_dialog_ind_struct *p = (widget_display_dialog_ind_struct *)param;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    switch (p->dlg_type)
    {
        case WIDGET_DLG_TYPE_ALERT:
            widget_display_alert_dialog(p->message);
            break;
        case WIDGET_DLG_TYPE_CONFIRM:
            widget_display_confirm_dialog(p->view_handle, p->message);
            break;
        case WIDGET_DLG_TYPE_PROMPT:
            break;
        default:
            break;
    }
}


static void widget_cancel_allocate_asm(mmi_frm_appmem_evt_struct* event)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_allocate_asm_rsp_struct *p = (widget_allocate_asm_rsp_struct*) construct_local_para(
                                                sizeof(widget_allocate_asm_rsp_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    p->ret = KAL_FALSE;
    send_message(p, NULL, MSG_ID_WIDGET_ALLOCATE_ASM_RSP, MOD_GADGET);
}


static void widget_free_asm(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (g_widget_asm != NULL)
    {
        //applib_mem_ap_free(g_widget_asm);
        //g_widget_asm = NULL;
        mmi_frm_asm_free_r(APP_WIDGET_ADP, g_widget_asm);
        g_widget_asm = NULL;
    }
}


static void widget_free_asm_response_handler(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    widget_free_asm();
    mmi_frm_group_close(APP_WIDGET_ADP);

    // TODO:  think about how to do

    if (mmi_frm_group_is_present(GRP_ID_WIDGET_VIEW))
    {
        mmi_frm_group_close(GRP_ID_WIDGET_VIEW);
    }
    //else if (mmi_frm_group_is_present(GRP_ID_WGTMGR_MAIN))
    //{
    //    mmi_frm_group_close(GRP_ID_WGTMGR_MAIN);
    //}
    else
    {
        mmi_frm_asm_notify_stop_finished(APP_WIDGET_ADP, KAL_TRUE);
    }
}


static void widget_free_asm_indication_handler(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    widget_free_asm();
}

#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif

static mmi_ret widget_asm_proc(mmi_event_struct *evt)
{
    switch (evt->evt_id)
   	{
    case EVT_ID_ASM_CANCELED:
    {
        mmi_frm_group_close(APP_WIDGET_ADP);

        widget_send_allocate_asm_rsp(KAL_FALSE, NULL, 0);

        break;
    }
    case EVT_ID_ASM_PREPARED:
    {          
        int asm_size = custom_gadget_get_asm_mem_size();
        
        g_widget_asm = mmi_frm_asm_alloc_r(APP_WIDGET_ADP, asm_size);
        ASSERT(g_widget_asm);

        widget_send_allocate_asm_rsp(KAL_TRUE, g_widget_asm, asm_size);

        break;
    }
    case EVT_ID_ASM_FREE_REQ:
    {
        //if there is a detail view, gadget does not free ASM voluntarily
        // TODO: think about how to do???
        if (mmi_frm_group_is_present(GRP_ID_WIDGET_VIEW) == MMI_FALSE)
       	{
            //send_message(NULL, NULL, MSG_ID_WIDGET_FREE_ASM_REQ, MOD_GADGET);
            //return MMI_FRM_ASM_WAITING_TO_FREE;
        }

        break;
    }
    case EVT_ID_ASM_FORCE_FREE:
    {
        send_message(NULL, NULL, MSG_ID_WIDGET_FREE_ASM_REQ, MOD_GADGET);
        return MMI_FRM_ASM_WAITING_TO_FREE;
    }
    case EVT_ID_DELETE_DANGLE_GROUP_REQ:
        /* do not delete this dangle group */
        return MMI_RET_ERR;
    }
    return MMI_RET_OK;
}


static void widget_allocate_asm(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    int asm_size = custom_gadget_get_asm_mem_size();
    mmi_frm_asm_property_struct p;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(g_widget_asm == NULL);
    mmi_frm_group_create(GRP_ID_ROOT, APP_WIDGET_ADP, widget_asm_proc, NULL);

    mmi_frm_asm_property_init(&p);

    p.f_hide_in_oom = 1;

    mmi_frm_asm_set_property(APP_WIDGET_ADP, &p);

    //g_widget_wait_asm_rsp = VFX_TRUE;
    
    mmi_frm_asm_prepare(
            APP_WIDGET_ADP,
            asm_size,
            widget_asm_proc,
            NULL,
            MMI_FRM_ASM_F_NONE);
}


static void widget_handle_allocate_asm_req(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    widget_allocate_asm();
}



/*************************************************************************
    Exported Functions
 *************************************************************************/
/*****************************************************************************
 * FUNCTION
 *  widget_load_widget
 * DESCRIPTION
 *  to load a widget with specified id
 * PARAMETERS
 *  instance_id              [IN]
 * RETURNS
 *  void
 *****************************************************************************/
void widget_load_widget(int instance_id)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_load_widget_req_struct *p = (widget_load_widget_req_struct*) construct_local_para(
                                                sizeof(widget_load_widget_req_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    srv_wgtmgr_check_data_account();

    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_LOAD_WIDGET_RSP, (PsIntFuncPtr)widget_load_widget_response_handler, KAL_FALSE);

    p->is_details = 0;
    p->instance_id = instance_id;

    send_message(p, NULL, MSG_ID_WIDGET_LOAD_WIDGET_REQ, MOD_GADGET);
}


/*****************************************************************************
 * FUNCTION
 *  widget_unload_widget
 * DESCRIPTION
 *  To unload a widget with specified id
 * PARAMETERS
 *  instance_id              [IN]
 * RETURNS
 *  void
 *****************************************************************************/
void widget_unload_widget(int instance_id)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_unload_widget_ind_struct *p = (widget_unload_widget_ind_struct*) construct_local_para(
                                                sizeof(widget_unload_widget_ind_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    widget_close_editor_by_id(instance_id);
    widget_close_view_by_id(instance_id);

    p->instance_id = instance_id;

    send_message(p, NULL, MSG_ID_WIDGET_UNLOAD_WIDGET_IND, MOD_GADGET);
}


/*****************************************************************************
 * FUNCTION
 *  widget_show_setting_dialog
 * DESCRIPTION
 *  To ask widget runtime to bring up setting dialog
 * PARAMETERS
 *  instance_id              [IN]
 * RETURNS
 *  void
 *****************************************************************************/
void widget_show_setting_dialog(int instance_id)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_show_setting_dialog_req_struct *p = (widget_show_setting_dialog_req_struct*) construct_local_para(
                                                sizeof(widget_show_setting_dialog_req_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    p->instance_id = instance_id;

    send_message(p, NULL, MSG_ID_WIDGET_SHOW_SETTING_DIALOG_REQ, MOD_GADGET);
}


/*****************************************************************************
 * FUNCTION
 *  widget_update_content
 * DESCRIPTION
 *  To notify widget runtime to redraw the content
 * PARAMETERS
 *  instance_id              [IN]
 *  view_handle              [IN]
 * RETURNS
 *  void
 *****************************************************************************/
void widget_update_content(int instance_id, void *view_handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_update_content_ind_struct *p = (widget_update_content_ind_struct*) construct_local_para(
                                                sizeof(widget_update_content_ind_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    p->instance_id = instance_id;
    p->view_handle = view_handle;

    send_message(p, NULL, MSG_ID_WIDGET_UPDATE_CONTENT_IND, MOD_GADGET);
}


/*****************************************************************************
 * FUNCTION
 *  widget_canvas_free
 * DESCRIPTION
 *
 * PARAMETERS
 *  instance_id              [IN]
 * RETURNS
 *  void
 *****************************************************************************/
void widget_canvas_free(void *p)
{
    extern void gadget_adp_canvas_free(void *);
    gadget_adp_canvas_free(p);
}


/*****************************************************************************
 * FUNCTION
 *  widget_focus_in
 * DESCRIPTION
 *
 * PARAMETERS
 *  instance_id              [IN]
 * RETURNS
 *  void
 *****************************************************************************/
void widget_focus_in(int instance_id, void *view_handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_focus_in_ind_struct *p = (widget_focus_in_ind_struct*) construct_local_para(
                                                sizeof(widget_focus_in_ind_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    p->instance_id = instance_id;
    p->view_handle = view_handle;

    send_message(p, NULL, MSG_ID_WIDGET_FOCUS_IN_IND, MOD_GADGET);
}

/*****************************************************************************
 * FUNCTION
 *  widget_focus_out
 * DESCRIPTION
 *
 * PARAMETERS
 *  instance_id              [IN]
 * RETURNS
 *  void
 *****************************************************************************/
void widget_focus_out(int instance_id, void *view_handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_focus_out_ind_struct *p = (widget_focus_out_ind_struct*) construct_local_para(
                                                sizeof(widget_focus_out_ind_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    p->instance_id = instance_id;
    p->view_handle = view_handle;

    send_message(p, NULL, MSG_ID_WIDGET_FOCUS_OUT_IND, MOD_GADGET);
}


/*****************************************************************************
 * FUNCTION
 *  widget_get_slmm_pool_id
 * DESCRIPTION
 *  This function is used to get slmm pool id.
 * PARAMETERS
 *  void             
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" GADGET_ASM_SLMM_ID widget_get_slmm_pool_id(void)
{
    return NULL;
}

/*****************************************************************************
 * FUNCTION
 *  widget_focus_out
 * DESCRIPTION
 *
 * PARAMETERS
 *  instance_id              [IN]
 * RETURNS
 *  void
 *****************************************************************************/
void widget_disable_view_cache()
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_disable_view_cache_ind_struct *p = (widget_disable_view_cache_ind_struct*) construct_local_para(
                                                sizeof(widget_disable_view_cache_ind_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    
    p->is_heap_shrink = 0;
    
    send_message(p, NULL, MSG_ID_WIDGET_DISABLE_VIEW_CACHE_IND, MOD_GADGET);
}

/*****************************************************************************
 * FUNCTION
 *  widget_handle_key_event
 * DESCRIPTION
 *
 * PARAMETERS
 *  instance_id              [IN]
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" void widget_handle_key_event(int id, void *view_handle, int mmi_keycode, int mmi_keytype)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    int keycode = 0, keytype = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

#ifdef __MTK_INTERNAL__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif 

    switch(mmi_keycode)
    {
    case KEY_0:
    case KEY_1:
    case KEY_2:
    case KEY_3:
    case KEY_4:
    case KEY_5:
    case KEY_6:
    case KEY_7:
    case KEY_8:
    case KEY_9:
        keycode = 0 + (keycode - KEY_0);
        break;
    case KEY_UP_ARROW:
        keycode = KEY_CODE_UP;
        break;
    case KEY_DOWN_ARROW:
        keycode = KEY_CODE_DOWN;
        break;
    case KEY_LEFT_ARROW:
        keycode = KEY_CODE_LEFT;
        break;
    case KEY_RIGHT_ARROW:
        keycode = KEY_CODE_RIGHT;
        break;
    case KEY_CLEAR:
        keycode = KEY_CODE_CLEAR;
        break;
    case KEY_VOL_UP:
        keycode = KEY_CODE_F23;
        break;
    case KEY_VOL_DOWN:
        keycode = KEY_CODE_F24;
        break;
    default:
        break;
    }

    switch(mmi_keytype)
    {
    case KEY_EVENT_DOWN:
    case KEY_LONG_PRESS:
    case KEY_REPEAT:
        keytype = KEY_DOWN_;
        break;
    case KEY_EVENT_UP:
        keytype = KEY_UP_;
        break;
    default:
        ASSERT(0);
    }

    gadget_adp_add_input_event(id, view_handle, keytype, keycode, 0, 0);
}


/*****************************************************************************
 * FUNCTION
 *  widget_handle_pen_event
 * DESCRIPTION
 *
 * PARAMETERS
 *  instance_id              [IN]
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" void widget_handle_pen_event(int id, void *view_handle, int x, int y, int event_type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    int type = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    switch(event_type)
    {
    case PEN_DOWN:
        type = MOUSE_DOWN;
        break;
    case PEN_MOVE:
        type = MOUSE_MOVE;
        break;
    case PEN_UP:
        type = MOUSE_UP;
        break;
    case PEN_ABORT:
        type = MOUSE_ABORT;
        break;
    default:
        ASSERT(0);
    }

    gadget_adp_add_input_event(id, view_handle, type, 0, x, y);

}


/*****************************************************************************
 * FUNCTION
 *  widget_handle_enter_text_response
 * DESCRIPTION
 *
 * PARAMETERS
 *  instance_id              [IN]
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" void widget_handle_enter_text_response(void *view_handle, int text_modified, kal_uint8 *text)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_enter_text_cnf_struct *p = (widget_enter_text_cnf_struct*) construct_local_para(
                                                sizeof(widget_enter_text_cnf_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    p->view_handle = view_handle;
    p->text_modified = text_modified;

    if (text_modified)
    {
        memcpy(p->text, text, MAX_STRING_LEN);
    }

    send_message(p, NULL, MSG_ID_WIDGET_ENTER_TEXT_CNF, MOD_GADGET);
}


/*****************************************************************************
 * FUNCTION
 *  widget_close_view
 * DESCRIPTION
 *  Notify widget runtime to close the specified view
 * PARAMETERS
 *  instance_id              [IN]
 *  view_handle              [IN]
 *  type                     [IN]
 *  reponse                  [IN]
 * RETURNS
 *  void
 *****************************************************************************/
void widget_close_view(int instance_id, void *view_handle, int type, int response)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_close_view_ind_struct *p = (widget_close_view_ind_struct*) construct_local_para(
                                                sizeof(widget_close_view_ind_struct),
                                                TD_RESET);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    p->instance_id = instance_id;
    p->view_handle = view_handle;
    p->type = type;
    p->response = response;

    send_message(p, NULL, MSG_ID_WIDGET_CLOSE_VIEW_IND, MOD_GADGET);

}


void *widget_lock_canvas(int instance_id, void *view_handle, void **buffer, int width, int height)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    VappHsWidgetDelegator *widget;
    void *lock = NULL;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    lock = mmi_widget_view_lock_canvas(instance_id, view_handle, buffer, width, height);

    if (lock)
    {
        return lock;
    }

    widget = VappHsWidgetDelegator::findByInstancId(instance_id);

    if (!widget || widget->getViewHandle() != view_handle)
    {
        return NULL;
    }

    return widget->lockCanvas(buffer, width, height);
}


void widget_unlock_canvas(void *lock)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    ASSERT(lock);

    ((canvas_lock_struct *)lock)->unlock_canvas(lock);
}



/*****************************************************************************
 * FUNCTION
 *  widget_mode_switch_notify_hdlr
 * DESCRIPTION
 *  check memory
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" mmi_ret widget_mode_switch_notify_hdlr(mmi_event_struct *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    srv_mode_switch_notify_struct *event = (srv_mode_switch_notify_struct *) param;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    switch(event->evt_id)
    {
        case EVT_ID_SRV_MODE_SWITCH_PRE_NOTIFY:
            if (event->gsm_select_mode == SRV_MODE_SWITCH_ALL_OFF)
            {
                widget_abort_all_sms_req();
            }
            break;
    }
    return MMI_RET_OK;
}




/*****************************************************************************
 * FUNCTION
 *  widget_fmgr_notify_hdlr
 * DESCRIPTION
 *  check memory
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" mmi_ret widget_fmgr_notify_hdlr(mmi_event_struct *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    widget_dev_op_enum op = WIDGET_DEV_UNMOUNT;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    switch(param->evt_id)
    {
        case EVT_ID_SRV_FMGR_NOTIFICATION_DEV_PLUG_IN:
            op = WIDGET_DEV_MOUNT;

        case EVT_ID_SRV_FMGR_NOTIFICATION_DEV_PLUG_OUT:
            srv_fmgr_notification_dev_plug_event_struct *event = (srv_fmgr_notification_dev_plug_event_struct *)param;
            U32 i;

            for (i = 0; i < event->count; i++)
            {
                if (event->drv_letters[i] == SRV_FMGR_CARD_DRV)
                {

                    widget_device_update_ind_struct *p = (widget_device_update_ind_struct*) construct_local_para(
                                                        sizeof(widget_device_update_ind_struct),
                                                        TD_RESET);

                    p->op = op;
                    send_message(p, NULL, MSG_ID_WIDGET_DEV_UPDATE_IND, MOD_GADGET);
                }
            }
            break;
    }
    return MMI_RET_OK;
}

/*****************************************************************************
 * FUNCTION
 *  widget_usb_notify_hdlr
 * DESCRIPTION
 *  check memory
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" mmi_ret widget_usb_notify_hdlr(mmi_event_struct *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MMI_USB_SUPPORT__
    switch(param->evt_id)
    {
        case EVT_ID_USB_EXIT_MS_MODE:
        {          
            srv_wgtmgr_usb_notify_hdlr(param);

            mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_NOTIFY_CONTENT_UPDATE_IND, (PsIntFuncPtr)widget_notify_content_update, KAL_FALSE);

            VfxObjListEntry *iter = VFX_OBJ_GET_INSTANCE(VappWidgetObjectList)->getHead();

            if(iter)
            {
                mmi_frm_display_dummy_screen();
                mmi_frm_scrn_close_active_id();
            }

            break;
        }
    }
#endif
    return MMI_RET_OK;

}


/*****************************************************************************
 * FUNCTION
 *  widget_wgtmgr_notify_hdlr
 * DESCRIPTION
 *
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" mmi_ret widget_wgtmgr_notify_hdlr(mmi_event_struct *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    srv_wgtmgr_notify_evt_struct *event = (srv_wgtmgr_notify_evt_struct *) param;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    switch(event->evt_id)
    {
        case EVT_ID_SRV_WGTMGR_FILE_NOT_EXISTS_IND:
            vadp_p2v_hs_widget_reset_by_id(VAPP_HS_WIDGET_TYPE_GADGET, event->id, 0);
            widget_unload_widget(event->id);
            break;

        case EVT_ID_SRV_WGTMGR_DELETE_WIDGET_IND:
            vadp_p2v_hs_widget_reset_by_id(VAPP_HS_WIDGET_TYPE_GADGET, event->id, 0);
            break;
    }
    return MMI_RET_OK;
}


/*****************************************************************************
 * FUNCTION
 *  widget_wgtmgr_notify_hdlr
 * DESCRIPTION
 *
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" mmi_ret widget_init_evt_hdlr(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_TERMINATE_WGT_IND, (PsIntFuncPtr)widget_handle_terminate_widget_ind, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_TERMINATE_ALL_WGT_IND, (PsIntFuncPtr)widget_handle_terminate_all_ind, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_ALLOCATE_ASM_REQ, (PsIntFuncPtr)widget_handle_allocate_asm_req, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_FREE_ASM_RSP, (PsIntFuncPtr)widget_free_asm_response_handler, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_FREE_ASM_IND, (PsIntFuncPtr)widget_free_asm_indication_handler, KAL_FALSE);
    return MMI_RET_OK;
}


/*****************************************************************************
 * FUNCTION
 *  vapp_widget_get_widget_active_flag
 * DESCRIPTION
 *
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" bool widget_get_widget_active_flag(void)
{
    return g_widget_active;
}

/*****************************************************************************
 * FUNCTION
 *  widget_set_widget_active_flag
 * DESCRIPTION
 *
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" void widget_set_widget_active_flag(kal_bool active_flag)
{
    g_widget_active = active_flag;
}

/*****************************************************************************
 * FUNCTION
 *  widget_enter_widget_active_mode
 * DESCRIPTION
 *
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" void widget_enter_widget_active_mode(void)
{
    if (active_mode_mutex == NULL)
    {
        active_mode_mutex = kal_create_mutex("widget_active_mode");
    }

    if (widget_get_widget_active_flag() == true) 
    {
        kal_take_mutex(active_mode_mutex);
        
        if (g_widget_active_mode_entered == false)
        {
            g_widget_active_mode_entered = true;
     
            //gadget_adp_log_prompt_trace("Enter throttle mode");

            vrt_enter_throttle_mode('GAGT');
        }
        
        kal_give_mutex(active_mode_mutex);
    }
}

/*****************************************************************************
 * FUNCTION
 *  widget_leave_widget_active_mode
 * DESCRIPTION
 *
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" void widget_leave_widget_active_mode(void)
{
    if (active_mode_mutex == NULL)
    {
        active_mode_mutex = kal_create_mutex("widget_active_mode");
    }

    kal_take_mutex(active_mode_mutex);

    if (g_widget_active_mode_entered == true)
    {
        g_widget_active_mode_entered = false;

        //gadget_adp_log_prompt_trace("Leave throttle mode");

        vrt_leave_throttle_mode('GAGT');
    }

    kal_give_mutex(active_mode_mutex);
}

/*****************************************************************************
 * FUNCTION
 *  widget_alloc_nc_mem
 * DESCRIPTION
 * This function is used to allocate CAIRO memory for gadget engine to draw image and text.
 * PARAMETERS
 *  void
 * RETURNS
 *  void*
 *****************************************************************************/
extern "C" void *widget_alloc_nc_mem(U32 size)
{
    //return med_alloc_ext_mem(size);
    return applib_asm_alloc_anonymous_nc(size);
}

/*****************************************************************************
 * FUNCTION
 *  widget_free_nc_mem
 * DESCRIPTION
 * This function is used to free CAIRO memory for gadget engine to draw image and text.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
extern "C" void widget_free_nc_mem(void *ptr)
{
    //med_free_ext_mem((void**)&ptr);
    applib_asm_free_anonymous(ptr);
}

void widget_initialize(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_DISPLAY_DIALOG_IND, (PsIntFuncPtr)widget_handle_display_dialog_ind, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_TERMINATE_WGT_IND, (PsIntFuncPtr)widget_handle_terminate_widget_ind, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_TERMINATE_ALL_WGT_IND, (PsIntFuncPtr)widget_handle_terminate_all_ind, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_CHECK_NETWORK_ACCESS_IND, (PsIntFuncPtr)widget_handle_check_network_access_ind, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_ALLOCATE_ASM_REQ, (PsIntFuncPtr)widget_handle_allocate_asm_req, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_FREE_ASM_RSP, (PsIntFuncPtr)widget_free_asm_response_handler, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_FREE_ASM_IND, (PsIntFuncPtr)widget_free_asm_indication_handler, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_QUERY_DEVICE_INFO_IND, (PsIntFuncPtr)widget_handle_query_device_info_ind, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_SEND_SMS_REQ, (PsIntFuncPtr)widget_send_sms_req_hdlr, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_COMMIT_CONTACT_REQ, (PsIntFuncPtr)widget_commit_contact_req_hdlr, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_DELETE_CONTACT_REQ, (PsIntFuncPtr)widget_delete_contact_req_hdlr, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_FIND_CONTACTS_REQ, (PsIntFuncPtr)widget_find_contacts_req_hdlr, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_GET_CONTACT_INFO_REQ, (PsIntFuncPtr)widget_get_contact_info_req_hdlr, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_BROWSE_IMAGE_REQ, (PsIntFuncPtr)widget_handle_browse_image_req, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_CAPTURE_IMAGE_REQ, (PsIntFuncPtr)widget_handle_capture_image_req, KAL_FALSE);

    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_AUDIO_PLAY_IN_MMI_REQ, (PsIntFuncPtr)gadget_adp_audio_play_in_mmi, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_AUDIO_STOP_IN_MMI_IND, (PsIntFuncPtr)gadget_adp_audio_stop_in_mmi, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_AUDIO_GET_CURR_POS_IN_MMI_REQ, (PsIntFuncPtr)gadget_adp_audio_get_current_position_in_mmi, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_AUDIO_SET_CURR_POS_IN_MMI_IND, (PsIntFuncPtr)gadget_adp_audio_set_current_position_in_mmi, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_AUDIO_GET_DUR_IN_MMI_REQ, (PsIntFuncPtr)gadget_adp_audio_get_duration_in_mmi, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_AUDIO_GET_VOL_IN_MMI_REQ, (PsIntFuncPtr)gadget_adp_audio_get_volume_in_mmi, KAL_FALSE);
	mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_AUDIO_SET_VOL_IN_MMI_IND, (PsIntFuncPtr)gadget_adp_audio_set_volume_in_mmi, KAL_FALSE);
#ifdef __BITSTREAM_API_SUPPORT__
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_BITSTREAM_AUDIO_OPEN_HANDLE_IN_MMI_REQ, (PsIntFuncPtr)gadget_adp_bitstream_audio_open_handle_in_mmi, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_BITSTREAM_AUDIO_CLOSE_IN_MMI_REQ, (PsIntFuncPtr)gadget_adp_bitstream_audio_close_in_mmi, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_BITSTREAM_AUDIO_GET_BUFFER_STATUS_IN_MMI_REQ, (PsIntFuncPtr)gadget_adp_bitstream_audio_get_buffer_status_in_mmi, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_BITSTREAM_AUDIO_PUT_DATA_IN_MMI_REQ, (PsIntFuncPtr)gadget_adp_bitstream_audio_put_data_in_mmi, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_BITSTREAM_AUDIO_START_IN_MMI_REQ, (PsIntFuncPtr)gadget_adp_bitstream_audio_start_in_mmi, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_BITSTREAM_AUDIO_STOP_IN_MMI_REQ, (PsIntFuncPtr)gadget_adp_bitstream_audio_stop_in_mmi, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_BITSTREAM_AUDIO_GET_PLAY_TIME_IN_MMI_REQ, (PsIntFuncPtr)gadget_adp_bitstream_audio_get_play_time_in_mmi, KAL_FALSE);
	mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_BITSTREAM_AUDIO_SUSPEND_BACKGROUND_PLAY_IN_MMI_REQ, (PsIntFuncPtr)gadget_adp_bitstream_audio_suspend_background_play_in_mmi, KAL_FALSE);
    mmi_frm_set_protocol_event_handler(MSG_ID_WIDGET_BITSTREAM_AUDIO_RESUME_BACKGROUND_PLAY_IN_MMI_REQ, (PsIntFuncPtr)gadget_adp_bitstream_audio_resume_background_play_in_mmi, KAL_FALSE);
#endif
}
#endif /* __MMI_GADGET_SUPPORT__ */