summaryrefslogtreecommitdiff
path: root/indra/test_apps/llplugintest/llmediaplugintest.cpp
blob: 1d6ea8e2703a559ada944062a8f586526cc75181 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
/**
 * @file LLMediaPluginTest.cpp
 * @brief Primary test application for LLMedia (Separate Process) Plugin system
 *
 * $LicenseInfo:firstyear=2008&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"
#include "indra_constants.h"

#include "llapr.h"
#include "llerrorcontrol.h"

#include <math.h>
#include <iomanip>
#include <sstream>
#include <ctime>

#include "llmediaplugintest.h"

#if __APPLE__
	#include <GLUT/glut.h>
	#include <CoreFoundation/CoreFoundation.h>
#else
	#define FREEGLUT_STATIC
	#include "GL/freeglut.h"
	#define GLUI_FREEGLUT
#endif

#if LL_WINDOWS
#pragma warning(disable: 4263)
#pragma warning(disable: 4264)
#endif
#include "glui.h"


LLMediaPluginTest* gApplication = 0;
static void gluiCallbackWrapper( int control_id );

////////////////////////////////////////////////////////////////////////////////
//
static bool isTexture( GLuint texture )
{
	bool result = false;

	// glIsTexture will sometimes return false for real textures... do this instead.
	if(texture != 0)
		result = true;

	return result;
}

////////////////////////////////////////////////////////////////////////////////
//
mediaPanel::mediaPanel()
{
	mMediaTextureHandle = 0;
	mPickTextureHandle = 0;
	mMediaSource = NULL;
	mPickTexturePixels = NULL;
}

////////////////////////////////////////////////////////////////////////////////
//
mediaPanel::~mediaPanel()
{
	// delete OpenGL texture handles
	if ( isTexture( mPickTextureHandle ) )
	{
		std::cerr << "remMediaPanel: deleting pick texture " << mPickTextureHandle << std::endl;
		glDeleteTextures( 1, &mPickTextureHandle );
		mPickTextureHandle = 0;
	}

	if ( isTexture( mMediaTextureHandle ) )
	{
		std::cerr << "remMediaPanel: deleting media texture " << mMediaTextureHandle << std::endl;
		glDeleteTextures( 1, &mMediaTextureHandle );
		mMediaTextureHandle = 0;
	}

	if(mPickTexturePixels)
	{
		delete mPickTexturePixels;
	}

	if(mMediaSource)
	{
		delete mMediaSource;
	}

}

////////////////////////////////////////////////////////////////////////////////
//
LLMediaPluginTest::LLMediaPluginTest( int app_window, int window_width, int window_height ) :
	mVersionMajor( 2 ),
	mVersionMinor( 0 ),
	mVersionPatch( 0 ),
	mMaxPanels( 25 ),
	mViewportAspect( 0 ),
	mAppWindow( app_window ),
	mCurMouseX( 0 ),
	mCurMouseY( 0 ),
	mFuzzyMedia( true ),
	mSelectedPanel( 0 ),
	mDistanceCameraToSelectedGeometry( 0.0f ),
	mMediaBrowserControlEnableCookies( 0 ),
	mMediaBrowserControlBackButton( 0 ),
	mMediaBrowserControlForwardButton( 0 ),
	mMediaTimeControlVolume( 100 ),
	mMediaTimeControlSeekSeconds( 0 ),
	mGluiMediaTimeControlWindowFlag( true ),
	mGluiMediaBrowserControlWindowFlag( true ),
	mMediaBrowserControlBackButtonFlag( true ),
	mMediaBrowserControlForwardButtonFlag( true ),
	mHomeWebUrl( "http://www.google.com/" )
{
	// debugging spam
	std::cout << std::endl << "             GLUT version: " << "3.7.6" << std::endl;	// no way to get real version from GLUT
	std::cout << std::endl << "             GLUI version: " << GLUI_Master.get_version() << std::endl;
	std::cout << std::endl << "Media Plugin Test version: " << mVersionMajor << "." << mVersionMinor << "." << mVersionPatch << std::endl;

	// bookmark title
	mBookmarks.push_back( std::pair< std::string, std::string >( "--- Bookmarks ---", "" ) );

	// insert hardcoded URLs here as required for testing
	//mBookmarks.push_back( std::pair< std::string, std::string >( "description", "url" ) );

	// read bookmarks from file.
	// note: uses command in ./CmakeLists.txt which copies bookmmarks file from source directory
	//       to app directory (WITHOUT build configuration dir) (this is cwd in Windows within MSVC)
	//		 For example, test_apps\llplugintest and not test_apps\llplugintest\Release
	//		 This may need to be changed for Mac/Linux builds.
	// See https://jira.lindenlab.com/browse/DEV-31350 for large list of media URLs from AGNI
	const std::string bookmarks_filename( "bookmarks.txt" );
	std::ifstream file_handle( bookmarks_filename.c_str() );
	if ( file_handle.is_open() )
	{
		std::cout << "Reading bookmarks for test" << std::endl;
		while( ! file_handle.eof() )
		{
			std::string line;
			std::getline( file_handle, line );
			if ( file_handle.eof() )
				break;

			if ( line.substr( 0, 1 ) != "#" )
			{
				size_t comma_pos = line.find_first_of( ',' );
				if ( comma_pos != std::string::npos )
				{
					std::string description = line.substr( 0, comma_pos );
					std::string url = line.substr( comma_pos + 1 );
					mBookmarks.push_back( std::pair< std::string, std::string >( description, url ) );
				}
				else
				{
					mBookmarks.push_back( std::pair< std::string, std::string >( line, line ) );
				};
			};
		};
		std::cout << "Read " << mBookmarks.size() << " bookmarks" << std::endl;
	}
	else
	{
		std::cout << "Unable to read bookmarks from file: " << bookmarks_filename << std::endl;
	};

	// initialize linden lab APR module
	ll_init_apr();

	// Set up llerror logging
	{
		LLError::initForApplication(".");
		LLError::setDefaultLevel(LLError::LEVEL_INFO);
		//LLError::setTagLevel("Plugin", LLError::LEVEL_DEBUG);
	}

	// lots of randomness in this app
	srand( ( unsigned int )time( 0 ) );

	// build GUI
	makeChrome();

	// OpenGL initialilzation
	glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
	glClearDepth( 1.0f );
	glEnable( GL_DEPTH_TEST );
	glEnable( GL_COLOR_MATERIAL );
	glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
	glDepthFunc( GL_LEQUAL );
	glEnable( GL_TEXTURE_2D );
	glDisable( GL_BLEND );
	glColor3f( 1.0f, 1.0f, 1.0f );
	glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
	glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );

	// start with a sane view
	resetView();

	// initial media panel
	const int num_initial_panels = 1;
	for( int i = 0; i < num_initial_panels; ++i )
	{
		//addMediaPanel( mBookmarks[ rand() % ( mBookmarks.size() - 1 ) + 1 ].second );
		addMediaPanel( mHomeWebUrl );
	};
}

////////////////////////////////////////////////////////////////////////////////
//
LLMediaPluginTest::~LLMediaPluginTest()
{
	// delete all media panels
	for( int i = 0; i < (int)mMediaPanels.size(); ++i )
	{
		remMediaPanel( mMediaPanels[ i ] );
	};
	
	// Stop the plugin read thread if it's running.
	LLPluginProcessParent::setUseReadThread(false);
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::reshape( int width, int height )
{
	// update viewport (the active window inside the chrome)
	int viewport_x, viewport_y;
	int viewport_height, viewport_width;
	GLUI_Master.get_viewport_area( &viewport_x, &viewport_y, &viewport_width, &viewport_height );
	mViewportAspect = (float)( viewport_width ) / (float)( viewport_height );
	glViewport( viewport_x, viewport_y, viewport_width, viewport_height );

	// save these as we'll need them later
	mWindowWidth = width;
	mWindowHeight = height;

	// adjust size of URL bar so it doesn't get clipped
	mUrlEdit->set_w( mWindowWidth - 360 );

	// GLUI requires this
	if ( glutGetWindow() != mAppWindow )
		glutSetWindow( mAppWindow );

	// trigger re-display
	glutPostRedisplay();
};

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::bindTexture(GLuint texture, GLint row_length, GLint alignment)
{
	glEnable( GL_TEXTURE_2D );

	glBindTexture( GL_TEXTURE_2D, texture );
	glPixelStorei( GL_UNPACK_ROW_LENGTH, row_length );
	glPixelStorei( GL_UNPACK_ALIGNMENT, alignment );
}

////////////////////////////////////////////////////////////////////////////////
//
bool LLMediaPluginTest::checkGLError(const char *name)
{
	bool result = false;
	GLenum error = glGetError();

	if(error != GL_NO_ERROR)
	{
		// For some reason, glGenTextures is returning GL_INVALID_VALUE...
		std::cout << name << " ERROR 0x" << std::hex << error << std::dec << std::endl;
		result = true;
	}

	return result;
}

////////////////////////////////////////////////////////////////////////////////
//
GLfloat LLMediaPluginTest::distanceToCamera( GLfloat point_x, GLfloat point_y, GLfloat point_z )
{
	GLdouble camera_pos_x = 0.0f;
	GLdouble camera_pos_y = 0.0f;
	GLdouble camera_pos_z = 0.0f;

	GLdouble modelMatrix[16];
	GLdouble projMatrix[16];
	GLint viewport[4];

	glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
	glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
	glGetIntegerv(GL_VIEWPORT, viewport);

	gluUnProject(
		(viewport[2]-viewport[0])/2 , (viewport[3]-viewport[1])/2,
		0.0,
		modelMatrix, projMatrix, viewport,
		&camera_pos_x, &camera_pos_y, &camera_pos_z );

	GLfloat distance =
		sqrt( ( camera_pos_x - point_x ) * ( camera_pos_x - point_x ) +
			  ( camera_pos_y - point_y ) * ( camera_pos_y - point_y ) +
			  ( camera_pos_z - point_z ) * ( camera_pos_z - point_z ) );

	return distance;
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::drawGeometry( int panel, bool selected )
{
	// texture coordinates for each panel
	GLfloat non_opengl_texture_coords[ 8 ] = { 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f };
	GLfloat opengl_texture_coords[ 8 ] =     { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f };

	GLfloat *texture_coords = mMediaPanels[ panel ]->mAppTextureCoordsOpenGL?opengl_texture_coords:non_opengl_texture_coords;

	// base coordinates for each panel
	GLfloat base_vertex_pos[ 8 ] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f };

	// calculate posiitons
	const int num_panels = (int)mMediaPanels.size();
	const int num_rows = (int)sqrt( (float)num_panels );
	const int num_cols = num_panels / num_rows;
	const int panel_x = ( panel / num_rows );
	const int panel_y = ( panel % num_rows );

	// default spacing is small - make it larger if checkbox set - for testing positional audio
	float spacing = 0.1f;
	if ( mLargePanelSpacing )
		spacing = 2.0f;

	const GLfloat offset_x = num_cols * ( 1.0 + spacing ) / 2;
	const GLfloat offset_y = num_rows * ( 1.0 + spacing ) / 2;

	// Adjust for media aspect ratios
	{
		float aspect = 1.0f;

		if(mMediaPanels[ panel ]->mMediaHeight != 0)
		{
			aspect = (float)mMediaPanels[ panel ]->mMediaWidth / (float)mMediaPanels[ panel ]->mMediaHeight;
		}

		if(aspect > 1.0f)
		{
			// media is wider than it is high -- adjust the top and bottom in
			for( int corner = 0; corner < 4; ++corner )
			{
				float temp = base_vertex_pos[corner * 2 + 1];

				if(temp < 0.5f)
					temp += 0.5 - (0.5f / aspect);
				else
					temp -= 0.5 - (0.5f / aspect);

				base_vertex_pos[corner * 2 + 1] = temp;
			}
		}
		else if(aspect < 1.0f)
		{
			// media is higher than it is wide -- adjust the left and right sides in
			for( int corner = 0; corner < 4; ++corner )
			{
				float temp = base_vertex_pos[corner * 2];

				if(temp < 0.5f)
					temp += 0.5f - (0.5f * aspect);
				else
					temp -= 0.5f - (0.5f * aspect);

				base_vertex_pos[corner * 2] = temp;
			}
		}
	}

	glBegin( GL_QUADS );
	for( int corner = 0; corner < 4; ++corner )
	{
		glTexCoord2f( texture_coords[ corner * 2 ], texture_coords[ corner * 2 + 1 ] );
		GLfloat x = base_vertex_pos[ corner * 2 ] + panel_x * ( 1.0 + spacing ) - offset_x + spacing / 2.0f;
		GLfloat y = base_vertex_pos[ corner * 2 + 1 ] + panel_y * ( 1.0 + spacing ) - offset_y + spacing / 2.0f;

		glVertex3f( x, y, 0.0f );
	};
	glEnd();

	// calculate distance to this panel if it's selected
	if ( selected )
	{
		GLfloat point_x = base_vertex_pos[ 0 ] + panel_x * ( 1.0 + spacing ) - offset_x + spacing / 2.0f;
		GLfloat point_y = base_vertex_pos[ 0 + 1 ] + panel_y * ( 1.0 + spacing ) - offset_y + spacing / 2.0f;
		GLfloat point_z = 0.0f;
		mDistanceCameraToSelectedGeometry = distanceToCamera( point_x, point_y, point_z );
	};
}

//////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::startPanelHighlight( float red, float green, float blue, float line_width )
{
	glPushAttrib( GL_ALL_ATTRIB_BITS );
	glEnable( GL_POLYGON_OFFSET_FILL );
	glPolygonOffset( -2.5f, -2.5f );
	glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
	glLineWidth( line_width );
	glColor3f( red, green, blue );
	glDisable( GL_TEXTURE_2D );
}

//////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::endPanelHighlight()
{
	glPopAttrib();
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::draw( int draw_type )
{
	for( int panel = 0; panel < (int)mMediaPanels.size(); ++panel )
	{
		// drawing pick texture
		if ( draw_type == DrawTypePickTexture )
		{
			// only bother with pick if we have something to render
			// Actually, we need to pick even if we're not ready to render.
			// Otherwise you can't select and remove a panel which has gone bad.
			//if ( mMediaPanels[ panel ]->mReadyToRender )
			{
				glMatrixMode( GL_TEXTURE );
				glPushMatrix();

				// pick texture is a power of 2 so no need to scale
				glLoadIdentity();

				// bind to media texture
				glLoadIdentity();
				bindTexture( mMediaPanels[ panel ]->mPickTextureHandle );
				glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
				glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );

				// draw geometry using pick texture
				drawGeometry( panel, false );

				glMatrixMode( GL_TEXTURE );
				glPopMatrix();
			};
		}
		else
		if ( draw_type == DrawTypeMediaTexture )
		{
			bool texture_valid = false;
			bool plugin_exited = false;

			if(mMediaPanels[ panel ]->mMediaSource)
			{
				texture_valid = mMediaPanels[ panel ]->mMediaSource->textureValid();
				plugin_exited = mMediaPanels[ panel ]->mMediaSource->isPluginExited();
			}

			// save texture matrix (changes for each panel)
			glMatrixMode( GL_TEXTURE );
			glPushMatrix();

			// only process texture if the media is ready to draw
			// (we still want to draw the geometry)
			if ( mMediaPanels[ panel ]->mReadyToRender && texture_valid )
			{
				// bind to media texture
				bindTexture( mMediaPanels[ panel ]->mMediaTextureHandle );

				if ( mFuzzyMedia )
				{
					glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
					glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
				}
				else
				{
					glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
					glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
				}

				// scale to fit panel
				glScalef( mMediaPanels[ panel ]->mTextureScaleX,
							mMediaPanels[ panel ]->mTextureScaleY,
								1.0f );
			};

			float intensity = plugin_exited?0.25f:1.0f;

			// highlight the selected panel
			if ( mSelectedPanel && ( mMediaPanels[ panel ]->mId == mSelectedPanel->mId ) )
			{
				startPanelHighlight( intensity, intensity, 0.0f, 5.0f );
				drawGeometry( panel, true );
				endPanelHighlight();
			}
			else
			// this panel not able to render yet since it
			// doesn't have enough information
			if ( !mMediaPanels[ panel ]->mReadyToRender )
			{
				startPanelHighlight( intensity, 0.0f, 0.0f, 2.0f );
				drawGeometry( panel, false );
				endPanelHighlight();
			}
			else
			// just display a border around the media
			{
				startPanelHighlight( 0.0f, intensity, 0.0f, 2.0f );
				drawGeometry( panel, false );
				endPanelHighlight();
			};

			if ( mMediaPanels[ panel ]->mReadyToRender && texture_valid )
			{
				// draw visual geometry
				drawGeometry( panel, false );
			}

			// restore texture matrix (changes for each panel)
			glMatrixMode( GL_TEXTURE );
			glPopMatrix();
		};
	};
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::display()
{
	// GLUI requires this
	if ( glutGetWindow() != mAppWindow )
		glutSetWindow( mAppWindow );

	// start with a clean slate
	glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	// set up OpenGL view
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	glFrustum( -mViewportAspect * 0.04f, mViewportAspect * 0.04f, -0.04f, 0.04f, 0.1f, 50.0f );
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
	glTranslatef( 0.0, 0.0, 0.0f );
	glTranslatef( mViewPos[ 0 ], mViewPos[ 1 ], -mViewPos[ 2 ] );
	glMultMatrixf( mViewRotation );

	// draw pick texture
	draw( DrawTypePickTexture );

	// read colors and get coordinate values
	glReadPixels( mCurMouseX, mCurMouseY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, mPixelReadColor );

	// clear the pick render (otherwise it may depth-fight with the textures rendered later)
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	// draw visible geometry
	draw( DrawTypeMediaTexture );

	glutSwapBuffers();
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::idle()
{
//	checkGLError("LLMediaPluginTest::idle");

	// GLUI requires this
	if ( glutGetWindow() != mAppWindow )
		glutSetWindow( mAppWindow );

	// random creation/destruction of panels enabled?
	const time_t panel_timeout_time = 5;
	if ( mRandomPanelCount )
	{
		// time for a change
		static time_t last_panel_time = 0;
		if ( time( NULL ) - last_panel_time > panel_timeout_time )
		{
			if ( rand() % 2 == 0 )
			{
				if ( mMediaPanels.size() < 16 )
				{
					std::cout << "Randomly adding new panel" << std::endl;
					addMediaPanel( mBookmarks[ rand() % ( mBookmarks.size() - 1 ) + 1 ].second );
				};
			}
			else
			{
				if ( mMediaPanels.size() > 0 )
				{
					std::cout << "Deleting selected panel" << std::endl;
					remMediaPanel( mSelectedPanel );
				};
			};
			time( &last_panel_time );
		};
	};

	// random selection of bookmarks enabled?
	const time_t bookmark_timeout_time = 5;
	if ( mRandomBookmarks )
	{
		// time for a change
		static time_t last_bookmark_time = 0;
		if ( time( NULL ) - last_bookmark_time > bookmark_timeout_time )
		{
			// go to a different random bookmark on each panel
			for( int panel = 0; panel < (int)mMediaPanels.size(); ++panel )
			{
				std::string uri = mBookmarks[ rand() % ( mBookmarks.size() - 1 ) + 1 ].second;

				std::cout << "Random: navigating to : " << uri << std::endl;

				std::string mime_type = mimeTypeFromUrl( uri );

				if ( mime_type != mMediaPanels[ panel ]->mMimeType )
				{
					replaceMediaPanel( mMediaPanels[ panel ], uri );
				}
				else
				{
					mMediaPanels[ panel ]->mMediaSource->loadURI( uri );
					mMediaPanels[ panel ]->mMediaSource->start();
				};
			};

			time( &last_bookmark_time );
		};
	};

	// update UI
	if ( mSelectedPanel )
	{
		// set volume based on slider if we have time media
		//if ( mGluiMediaTimeControlWindowFlag )
		//{
		//	mSelectedPanel->mMediaSource->setVolume( (float)mMediaTimeControlVolume / 100.0f );
		//};

		// NOTE: it is absurd that we need cache the state of GLUI controls
		//       but enabling/disabling controls drags framerate from 500+
		//		 down to 15. Not a problem for plugin system - only this test
		// enable/disable time based UI controls based on type of plugin
		if ( mSelectedPanel->mMediaSource->pluginSupportsMediaTime() )
		{
			if ( ! mGluiMediaTimeControlWindowFlag )
			{
				mGluiMediaTimeControlWindow->enable();
				mGluiMediaTimeControlWindowFlag = true;
			};
		}
		else
		{
			if ( mGluiMediaTimeControlWindowFlag )
			{
				mGluiMediaTimeControlWindow->disable();
				mGluiMediaTimeControlWindowFlag = false;
			};
		};

		// enable/disable browser based UI controls based on type of plugin
		if ( mSelectedPanel->mMediaSource->pluginSupportsMediaBrowser() )
		{
			if ( ! mGluiMediaBrowserControlWindowFlag )
			{
				mGluiMediaBrowserControlWindow->enable();
				mGluiMediaBrowserControlWindowFlag = true;
			};
		}
		else
		{
			if ( mGluiMediaBrowserControlWindowFlag )
			{
				mGluiMediaBrowserControlWindow->disable();
				mGluiMediaBrowserControlWindowFlag = false;
			};
		};

		// enable/disable browser back button depending on browser history
		if ( mSelectedPanel->mMediaSource->getHistoryBackAvailable()  )
		{
			if ( ! mMediaBrowserControlBackButtonFlag )
			{
				mMediaBrowserControlBackButton->enable();
				mMediaBrowserControlBackButtonFlag = true;
			};
		}
		else
		{
			if ( mMediaBrowserControlBackButtonFlag )
			{
				mMediaBrowserControlBackButton->disable();
				mMediaBrowserControlBackButtonFlag = false;
			};
		};

		// enable/disable browser forward button depending on browser history
		if ( mSelectedPanel->mMediaSource->getHistoryForwardAvailable()  )
		{
			if ( ! mMediaBrowserControlForwardButtonFlag )
			{
				mMediaBrowserControlForwardButton->enable();
				mMediaBrowserControlForwardButtonFlag = true;
			};
		}
		else
		{
			if ( mMediaBrowserControlForwardButtonFlag )
			{
				mMediaBrowserControlForwardButton->disable();
				mMediaBrowserControlForwardButtonFlag = false;
			};
		};

		// NOTE: This is *very* slow and not worth optimising
		updateStatusBar();
	};

	// update all the panels
	for( int panel_index = 0; panel_index < (int)mMediaPanels.size(); ++panel_index )
	{
		mediaPanel *panel = mMediaPanels[ panel_index ];

		// call plugins idle function so it can potentially update itself
		panel->mMediaSource->idle();

		// update each media panel
		updateMediaPanel( panel );

		LLRect dirty_rect;
		if ( ! panel->mMediaSource->textureValid() )
		{
			//std::cout << "texture invalid, skipping update..." << std::endl;
		}
		else
		if ( panel &&
			 ( panel->mMediaWidth != panel->mMediaSource->getWidth() ||
			   panel->mMediaHeight != panel->mMediaSource->getHeight() ) )
		{
			//std::cout << "Resize in progress, skipping update..." << std::endl;
		}
		else
		if ( panel->mMediaSource->getDirty( &dirty_rect ) )
		{
			const unsigned char* pixels = panel->mMediaSource->getBitsData();
			if ( pixels && isTexture(panel->mMediaTextureHandle))
			{
				int x_offset = dirty_rect.mLeft;
				int y_offset = dirty_rect.mBottom;
				int width = dirty_rect.mRight - dirty_rect.mLeft;
				int height = dirty_rect.mTop - dirty_rect.mBottom;

				if((dirty_rect.mRight <= panel->mTextureWidth) && (dirty_rect.mTop <= panel->mTextureHeight))
				{
					// Offset the pixels pointer properly
					pixels += ( y_offset * panel->mMediaSource->getTextureDepth() * panel->mMediaSource->getBitsWidth() );
					pixels += ( x_offset * panel->mMediaSource->getTextureDepth() );

					// set up texture
					bindTexture( panel->mMediaTextureHandle, panel->mMediaSource->getBitsWidth() );
					if ( mFuzzyMedia )
					{
						glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
						glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
					}
					else
					{
						glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
						glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
					};

					checkGLError("glTexParameteri");

					if(panel->mMediaSource->getTextureFormatSwapBytes())
					{
						glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
						checkGLError("glPixelStorei");
					}

					// draw portion that changes into texture
					glTexSubImage2D( GL_TEXTURE_2D, 0,
						x_offset,
						y_offset,
						width,
						height,
						panel->mMediaSource->getTextureFormatPrimary(),
						panel->mMediaSource->getTextureFormatType(),
						pixels );

					if(checkGLError("glTexSubImage2D"))
					{
						std::cerr << "    panel ID=" << panel->mId << std::endl;
						std::cerr << "    texture size = " << panel->mTextureWidth << " x " << panel->mTextureHeight << std::endl;
						std::cerr << "    media size = " << panel->mMediaWidth << " x " << panel->mMediaHeight << std::endl;
						std::cerr << "    dirty rect = " << dirty_rect.mLeft << ", " << dirty_rect.mBottom << ", " << dirty_rect.mRight << ", " << dirty_rect.mTop << std::endl;
						std::cerr << "    texture width = " << panel->mMediaSource->getBitsWidth() << std::endl;
						std::cerr << "    format primary = 0x" << std::hex << panel->mMediaSource->getTextureFormatPrimary() << std::dec << std::endl;
						std::cerr << "    format type = 0x" << std::hex << panel->mMediaSource->getTextureFormatType() << std::dec << std::endl;
						std::cerr << "    pixels = " << (void*)pixels << std::endl;
					}

					if(panel->mMediaSource->getTextureFormatSwapBytes())
					{
						glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
						checkGLError("glPixelStorei");
					}

					panel->mMediaSource->resetDirty();

					panel->mReadyToRender = true;
				}
				else
				{
					std::cerr << "dirty rect is outside current media size, skipping update" << std::endl;
				}
			};
		};
	};

	// GLUI requires this
	if ( glutGetWindow() != mAppWindow )
		glutSetWindow( mAppWindow );

	// trigger re-display
	glutPostRedisplay();
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::windowPosToTexturePos( int window_x, int window_y,
											   int& media_x, int& media_y,
											   int& id )
{
	if ( ! mSelectedPanel )
	{
		media_x = 0;
		media_y = 0;
		id = 0;
		return;
	};

	// record cursor poisiton for a readback next frame
	mCurMouseX = window_x;
	// OpenGL app == coordinate system this way
	// NOTE: unrelated to settings in plugin - this
	// is just for this app
	mCurMouseY = mWindowHeight - window_y;

	// extract x (0..1023, y (0..1023) and id (0..15) from RGB components
	unsigned long pixel_read_color_bits = ( mPixelReadColor[ 0 ] << 16 ) | ( mPixelReadColor[ 1 ] << 8 ) | mPixelReadColor[ 2 ];
	int texture_x = pixel_read_color_bits & 0x3ff;
	int texture_y = ( pixel_read_color_bits >> 10 ) & 0x3ff;
	id = ( pixel_read_color_bits >> 20 ) & 0x0f;

	// scale to size of media (1024 because we use 10 bits for X and Y from 24)
	media_x = (int)( ( (float)mSelectedPanel->mMediaWidth * (float)texture_x ) / 1024.0f );
	media_y = (int)( ( (float)mSelectedPanel->mMediaHeight * (float)texture_y ) / 1024.0f );

	// we assume the plugin uses an inverted coordinate scheme like OpenGL
	// if not, the plugin code inverts the Y coordinate for us - we don't need to
	media_y = mSelectedPanel->mMediaHeight - media_y;

	if ( media_x > 0 && media_y > 0 )
	{
		//std::cout << "      mouse coords: " << mCurMouseX << " x " << mCurMouseY << " and id = " << id  << std::endl;
		//std::cout << "raw texture coords: " << texture_x << " x " << texture_y << " and id = " << id  << std::endl;
		//std::cout << "      media coords: " << media_x << " x " << media_y << " and id = " << id  << std::endl;
		//std::cout << std::endl;
	};
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::selectPanelById( int id )
{
	for( int panel = 0; panel < (int)mMediaPanels.size(); ++panel )
	{
		if ( mMediaPanels[ panel ]->mId == id )
		{
			selectPanel(mMediaPanels[ panel ]);
			return;
		};
	};
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::selectPanel( mediaPanel* panel )
{
	if( mSelectedPanel == panel )
		return;

	// turn off volume before we delete it
	if( mSelectedPanel && mSelectedPanel->mMediaSource )
	{
		mSelectedPanel->mMediaSource->setVolume( 0.0f );
		mSelectedPanel->mMediaSource->setPriority( LLPluginClassMedia::PRIORITY_LOW );
	};

	mSelectedPanel = panel;

	if( mSelectedPanel && mSelectedPanel->mMediaSource )
	{
		mSelectedPanel->mMediaSource->setVolume( (float)mMediaTimeControlVolume / 100.0f );
		mSelectedPanel->mMediaSource->setPriority( LLPluginClassMedia::PRIORITY_NORMAL );

		if(!mSelectedPanel->mStartUrl.empty())
		{
			mUrlEdit->set_text(const_cast<char*>(mSelectedPanel->mStartUrl.c_str()) );
		}
	};
}

////////////////////////////////////////////////////////////////////////////////
//
mediaPanel*  LLMediaPluginTest::findMediaPanel( LLPluginClassMedia* source )
{
	mediaPanel *result = NULL;

	for( int panel = 0; panel < (int)mMediaPanels.size(); ++panel )
	{
		if ( mMediaPanels[ panel ]->mMediaSource == source )
		{
			result = mMediaPanels[ panel ];
		}
	}

	return result;
}

////////////////////////////////////////////////////////////////////////////////
//
mediaPanel* LLMediaPluginTest::findMediaPanel( const std::string &target_name )
{
	mediaPanel *result = NULL;

	for( int panel = 0; panel < (int)mMediaPanels.size(); ++panel )
	{
		if ( mMediaPanels[ panel ]->mTarget == target_name )
		{
			result = mMediaPanels[ panel ];
		}
	}

	return result;
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::navigateToNewURI( std::string uri )
{
	if ( uri.length() )
	{
		std::string mime_type = mimeTypeFromUrl( uri );

		if ( !mSelectedPanel->mMediaSource->isPluginExited() && (mime_type == mSelectedPanel->mMimeType) )
		{
			std::cout << "MIME type is the same" << std::endl;
			mSelectedPanel->mMediaSource->loadURI( uri );
			mSelectedPanel->mMediaSource->start();
			mBookmarkList->do_selection( 0 );
		}
		else
		{
			std::cout << "MIME type changed or plugin had exited" << std::endl;
			replaceMediaPanel( mSelectedPanel, uri );
			mBookmarkList->do_selection( 0 );
		}
	};
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::initUrlHistory( std::string uris )
{
	if ( uris.length() > 0 )
	{
		std::cout << "init URL : " << uris << std::endl;
		LLSD historySD;

		char *cstr, *p;
		cstr = new char[uris.size()+1];
		strcpy(cstr, uris.c_str());
		const char *DELIMS = " ,;";
		p = strtok(cstr, DELIMS);
		while (p != NULL) {
			historySD.insert(0, p);
			p = strtok(NULL, DELIMS);
		}
		mSelectedPanel->mMediaSource->initializeUrlHistory(historySD);
		delete[] cstr;
	}
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::gluiCallback( int control_id )
{
	if ( control_id == mIdBookmarks )
	{
		std::string uri = mBookmarks[ mSelBookmark ].second;

		navigateToNewURI( uri );
	}
	else
    if ( control_id == mIdUrlEdit)
	{
		std::string uri = mUrlEdit->get_text();

		navigateToNewURI( uri );
	}
	else
	if ( control_id == mIdUrlInitHistoryEdit )
	{
		std::string uri = mUrlInitHistoryEdit->get_text();

		initUrlHistory( uri );
	}
	else
	if ( control_id == mIdControlAddPanel )
	{
		addMediaPanel( mBookmarks[ rand() % ( mBookmarks.size() - 1 ) + 1 ].second );
	}
	else
	if ( control_id == mIdControlRemPanel )
	{
		remMediaPanel( mSelectedPanel );
	}
	else
	if ( control_id == mIdDisableTimeout )
	{
		// Set the "disable timeout" flag for all active plugins.
		for( int i = 0; i < (int)mMediaPanels.size(); ++i )
		{
			mMediaPanels[ i ]->mMediaSource->setDisableTimeout(mDisableTimeout);
		}
	}
	else
	if ( control_id == mIdUsePluginReadThread )
	{
		LLPluginProcessParent::setUseReadThread(mUsePluginReadThread);
	}
	else
	if ( control_id == mIdControlCrashPlugin )
	{
		// send message to plugin and ask it to crash
		// (switch out for ReleaseCandidate version :) )
		if(mSelectedPanel && mSelectedPanel->mMediaSource)
		{
			mSelectedPanel->mMediaSource->crashPlugin();
		}
	}
	else
	if ( control_id == mIdControlHangPlugin )
	{
		// send message to plugin and ask it to hang
		// (switch out for ReleaseCandidate version :) )
		if(mSelectedPanel && mSelectedPanel->mMediaSource)
		{
			mSelectedPanel->mMediaSource->hangPlugin();
		}
	}
	else
	if ( control_id == mIdControlExitApp )
	{
		// text for exiting plugin system cleanly
		delete this;	// clean up
		exit( 0 );
	}
	else
	if ( control_id == mIdMediaTimeControlPlay )
	{
		if ( mSelectedPanel )
		{
			mSelectedPanel->mMediaSource->setLoop( false );
			mSelectedPanel->mMediaSource->start();
		};
	}
	else
	if ( control_id == mIdMediaTimeControlLoop )
	{
		if ( mSelectedPanel )
		{
			mSelectedPanel->mMediaSource->setLoop( true );
			mSelectedPanel->mMediaSource->start();
		};
	}
	else
	if ( control_id == mIdMediaTimeControlPause )
	{
		if ( mSelectedPanel )
			mSelectedPanel->mMediaSource->pause();
	}
	else
	if ( control_id == mIdMediaTimeControlStop )
	{
		if ( mSelectedPanel )
		{
			mSelectedPanel->mMediaSource->stop();
		};
	}
	else
	if ( control_id == mIdMediaTimeControlSeek )
	{
		if ( mSelectedPanel )
		{
			// get value from spinner
			float seconds_to_seek = mMediaTimeControlSeekSeconds;
			mSelectedPanel->mMediaSource->seek( seconds_to_seek );
			mSelectedPanel->mMediaSource->start();
		};
	}
	else
	if ( control_id == mIdMediaTimeControlRewind )
	{
		if ( mSelectedPanel )
		{
			mSelectedPanel->mMediaSource->setLoop( false );
			mSelectedPanel->mMediaSource->start(-2.0f);
		};
	}
	else
	if ( control_id == mIdMediaTimeControlFastForward )
	{
		if ( mSelectedPanel )
		{
			mSelectedPanel->mMediaSource->setLoop( false );
			mSelectedPanel->mMediaSource->start(2.0f);
		};
	}
	else
	if ( control_id == mIdMediaBrowserControlBack )
	{
		if ( mSelectedPanel )
			mSelectedPanel->mMediaSource->browse_back();
	}
	else
	if ( control_id == mIdMediaBrowserControlStop )
	{
		if ( mSelectedPanel )
			mSelectedPanel->mMediaSource->browse_stop();
	}
	else
	if ( control_id == mIdMediaBrowserControlForward )
	{
		if ( mSelectedPanel )
			mSelectedPanel->mMediaSource->browse_forward();
	}
	else
	if ( control_id == mIdMediaBrowserControlHome )
	{
		if ( mSelectedPanel )
			mSelectedPanel->mMediaSource->loadURI( mHomeWebUrl );
	}
	else
	if ( control_id == mIdMediaBrowserControlReload )
	{
		if ( mSelectedPanel )
			mSelectedPanel->mMediaSource->browse_reload( true );
	}
	else
	if ( control_id == mIdMediaBrowserControlClearCache )
	{
		if ( mSelectedPanel )
			mSelectedPanel->mMediaSource->clear_cache();
	}
	else
	if ( control_id == mIdMediaBrowserControlClearCookies )
	{
		if ( mSelectedPanel )
			mSelectedPanel->mMediaSource->clear_cookies();
	}
	else
	if ( control_id == mIdMediaBrowserControlEnableCookies )
	{
		if ( mSelectedPanel )
		{
			if ( mMediaBrowserControlEnableCookies )
			{
				mSelectedPanel->mMediaSource->enable_cookies( true );
			}
			else
			{
				mSelectedPanel->mMediaSource->enable_cookies( false );
			}
		};
	};
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::keyboard( int key )
{
	//if ( key == 'a' || key == 'A' )
	//	addMediaPanel( mBookmarks[ rand() % ( mBookmarks.size() - 1 ) + 1 ].second );
	//else
	//if ( key == 'r' || key == 'R' )
	//	remMediaPanel( mSelectedPanel );
	//else
	//if ( key == 'd' || key == 'D' )
	//	dumpPanelInfo();
	//else
	if ( key == 27 )
	{
		std::cout << "Application finished - exiting..." << std::endl;
		delete this;
		exit( 0 );
	};

	mSelectedPanel->mMediaSource->keyEvent( LLPluginClassMedia::KEY_EVENT_DOWN, key, 0 , LLSD());
	mSelectedPanel->mMediaSource->keyEvent( LLPluginClassMedia::KEY_EVENT_UP, key, 0, LLSD());
};

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::mouseButton( int button, int state, int x, int y )
{
	if ( button == GLUT_LEFT_BUTTON )
	{
		if ( state == GLUT_DOWN )
		{
			int media_x, media_y, id;
			windowPosToTexturePos( x, y, media_x, media_y, id );

			if ( mSelectedPanel )
				mSelectedPanel->mMediaSource->mouseEvent( LLPluginClassMedia::MOUSE_EVENT_DOWN, 0, media_x, media_y, 0 );
		}
		else
		if ( state == GLUT_UP )
		{
			int media_x, media_y, id;
			windowPosToTexturePos( x, y, media_x, media_y, id );

			// only select a panel if we're on a panel
			// (HACK: strictly speaking this rules out clicking on
			// the origin of a panel but that's very unlikely)
			if ( media_x > 0 && media_y > 0 )
			{
				selectPanelById( id );

				if ( mSelectedPanel )
					mSelectedPanel->mMediaSource->mouseEvent( LLPluginClassMedia::MOUSE_EVENT_UP, 0, media_x, media_y, 0 );
			};
		};
	};
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::mousePassive( int x, int y )
{
	int media_x, media_y, id;
	windowPosToTexturePos( x, y, media_x, media_y, id );

	if ( mSelectedPanel )
		mSelectedPanel->mMediaSource->mouseEvent( LLPluginClassMedia::MOUSE_EVENT_MOVE, 0, media_x, media_y, 0 );
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::mouseMove( int x, int y )
{
	int media_x, media_y, id;
	windowPosToTexturePos( x, y, media_x, media_y, id );

	if ( mSelectedPanel )
		mSelectedPanel->mMediaSource->mouseEvent( LLPluginClassMedia::MOUSE_EVENT_MOVE, 0, media_x, media_y, 0 );
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::makeChrome()
{
	// IDs used by GLUI
	int start_id = 0x1000;

	// right side window - geometry manipulators
#if __APPLE__
	// the Apple GLUT implementation doesn't seem to set the graphic offset of subwindows correctly when they overlap in certain ways.
	// Use a separate controls window in this case.
	// GLUI window at right containing manipulation controls and other buttons
	int x = glutGet(GLUT_WINDOW_X) + glutGet(GLUT_WINDOW_WIDTH) + 4;
	int y = glutGet(GLUT_WINDOW_Y);
	GLUI* right_glui_window = GLUI_Master.create_glui( "", 0, x, y );
#else
	GLUI* right_glui_window = GLUI_Master.create_glui_subwindow( mAppWindow, GLUI_SUBWINDOW_RIGHT );
#endif
	mViewRotationCtrl = right_glui_window->add_rotation( "Rotation", mViewRotation );
	mViewTranslationCtrl = right_glui_window->add_translation( "Translate", GLUI_TRANSLATION_XY, mViewPos );
	mViewTranslationCtrl->set_speed( 0.01f );
	mViewScaleCtrl = right_glui_window->add_translation( "Scale", GLUI_TRANSLATION_Z, &mViewPos[ 2 ] );
	mViewScaleCtrl->set_speed( 0.05f );
	right_glui_window->set_main_gfx_window( mAppWindow );

	// right side window - app controls
	mIdControlAddPanel = start_id++;
	right_glui_window->add_statictext( "" );
	right_glui_window->add_separator();
	right_glui_window->add_statictext( "" );
	right_glui_window->add_button( "Add panel", mIdControlAddPanel, gluiCallbackWrapper );
	right_glui_window->add_statictext( "" );
	mIdControlRemPanel = start_id++;
	right_glui_window->add_button( "Rem panel", mIdControlRemPanel, gluiCallbackWrapper );
	right_glui_window->add_statictext( "" );
	right_glui_window->add_separator();
	right_glui_window->add_statictext( "" );
	mIdControlCrashPlugin = start_id++;
	right_glui_window->add_button( "Crash plugin", mIdControlCrashPlugin, gluiCallbackWrapper );
	mIdControlHangPlugin = start_id++;
	right_glui_window->add_button( "Hang plugin", mIdControlHangPlugin, gluiCallbackWrapper );

	right_glui_window->add_statictext( "" );
	right_glui_window->add_separator();
	right_glui_window->add_statictext( "" );
	mIdControlExitApp = start_id++;
	right_glui_window->add_button( "Exit app", mIdControlExitApp, gluiCallbackWrapper );

	//// top window - holds bookmark UI
	mIdBookmarks = start_id++;
	mSelBookmark = 0;
	GLUI* glui_window_top = GLUI_Master.create_glui_subwindow( mAppWindow, GLUI_SUBWINDOW_TOP );
	mBookmarkList = glui_window_top->add_listbox( "", &mSelBookmark, mIdBookmarks, gluiCallbackWrapper );
	// only add the first 50 bookmarks - list can be very long sometimes (30,000+)
	// when testing list of media URLs from AGNI for example
	for( unsigned int each = 0; each < mBookmarks.size() && each < 50; ++each )
		mBookmarkList->add_item( each, const_cast< char* >( mBookmarks[ each ].first.c_str() ) );
	glui_window_top->set_main_gfx_window( mAppWindow );

	glui_window_top->add_column( false );
	mIdUrlEdit = start_id++;
	mUrlEdit = glui_window_top->add_edittext( "Url:", GLUI_EDITTEXT_TEXT, 0, mIdUrlEdit, gluiCallbackWrapper );
	mUrlEdit->set_w( 600 );
	GLUI* glui_window_top2 = GLUI_Master.create_glui_subwindow( mAppWindow, GLUI_SUBWINDOW_TOP );
	mIdUrlInitHistoryEdit = start_id++;
	mUrlInitHistoryEdit = glui_window_top2->add_edittext( "Init History (separate by commas or semicolons):",
		GLUI_EDITTEXT_TEXT, 0, mIdUrlInitHistoryEdit, gluiCallbackWrapper );
	mUrlInitHistoryEdit->set_w( 800 );

	// top window - media controls for "time" media types (e.g. movies)
	mGluiMediaTimeControlWindow = GLUI_Master.create_glui_subwindow( mAppWindow, GLUI_SUBWINDOW_TOP );
	mGluiMediaTimeControlWindow->set_main_gfx_window( mAppWindow );
	mIdMediaTimeControlPlay = start_id++;
	mGluiMediaTimeControlWindow->add_button( "PLAY", mIdMediaTimeControlPlay, gluiCallbackWrapper );
	mGluiMediaTimeControlWindow->add_column( false );
	mIdMediaTimeControlLoop = start_id++;
	mGluiMediaTimeControlWindow->add_button( "LOOP", mIdMediaTimeControlLoop, gluiCallbackWrapper );
	mGluiMediaTimeControlWindow->add_column( false );
	mIdMediaTimeControlPause = start_id++;
	mGluiMediaTimeControlWindow->add_button( "PAUSE", mIdMediaTimeControlPause, gluiCallbackWrapper );
	mGluiMediaTimeControlWindow->add_column( false );

	GLUI_Button  *button;
	mIdMediaTimeControlRewind = start_id++;
	button = mGluiMediaTimeControlWindow->add_button( "<<", mIdMediaTimeControlRewind, gluiCallbackWrapper );
	button->set_w(30);
	mGluiMediaTimeControlWindow->add_column( false );
	mIdMediaTimeControlFastForward = start_id++;
	button = mGluiMediaTimeControlWindow->add_button( ">>", mIdMediaTimeControlFastForward, gluiCallbackWrapper );
	button->set_w(30);

	mGluiMediaTimeControlWindow->add_column( true );

	mIdMediaTimeControlStop = start_id++;
	mGluiMediaTimeControlWindow->add_button( "STOP", mIdMediaTimeControlStop, gluiCallbackWrapper );
	mGluiMediaTimeControlWindow->add_column( false );
	mIdMediaTimeControlVolume = start_id++;
	GLUI_Spinner* spinner = mGluiMediaTimeControlWindow->add_spinner( "Volume", 2, &mMediaTimeControlVolume, mIdMediaTimeControlVolume, gluiCallbackWrapper);
	spinner->set_float_limits( 0, 100 );
	mGluiMediaTimeControlWindow->add_column( true );
	mIdMediaTimeControlSeekSeconds = start_id++;
	spinner = mGluiMediaTimeControlWindow->add_spinner( "", 2, &mMediaTimeControlSeekSeconds, mIdMediaTimeControlSeekSeconds, gluiCallbackWrapper);
	spinner->set_float_limits( 0, 200 );
	spinner->set_w( 32 );
	spinner->set_speed( 0.025f );
	mGluiMediaTimeControlWindow->add_column( false );
	mIdMediaTimeControlSeek = start_id++;
	mGluiMediaTimeControlWindow->add_button( "SEEK", mIdMediaTimeControlSeek, gluiCallbackWrapper );
	mGluiMediaTimeControlWindow->add_column( false );


	// top window - media controls for "browser" media types (e.g. web browser)
	mGluiMediaBrowserControlWindow = GLUI_Master.create_glui_subwindow( mAppWindow, GLUI_SUBWINDOW_TOP );
	mGluiMediaBrowserControlWindow->set_main_gfx_window( mAppWindow );
	mIdMediaBrowserControlBack = start_id++;
	mMediaBrowserControlBackButton = mGluiMediaBrowserControlWindow->add_button( "BACK", mIdMediaBrowserControlBack, gluiCallbackWrapper );
	mGluiMediaBrowserControlWindow->add_column( false );
	mIdMediaBrowserControlStop = start_id++;
	mGluiMediaBrowserControlWindow->add_button( "STOP", mIdMediaBrowserControlStop, gluiCallbackWrapper );
	mGluiMediaBrowserControlWindow->add_column( false );
	mIdMediaBrowserControlForward = start_id++;
	mMediaBrowserControlForwardButton = mGluiMediaBrowserControlWindow->add_button( "FORWARD", mIdMediaBrowserControlForward, gluiCallbackWrapper );
	mGluiMediaBrowserControlWindow->add_column( false );
	mIdMediaBrowserControlHome = start_id++;
	mGluiMediaBrowserControlWindow->add_button( "HOME", mIdMediaBrowserControlHome, gluiCallbackWrapper );
	mGluiMediaBrowserControlWindow->add_column( false );
	mIdMediaBrowserControlReload = start_id++;
	mGluiMediaBrowserControlWindow->add_button( "RELOAD", mIdMediaBrowserControlReload, gluiCallbackWrapper );
	mGluiMediaBrowserControlWindow->add_column( false );
	mIdMediaBrowserControlClearCache = start_id++;
	mGluiMediaBrowserControlWindow->add_button( "CLEAR CACHE", mIdMediaBrowserControlClearCache, gluiCallbackWrapper );
	mGluiMediaBrowserControlWindow->add_column( false );
	mIdMediaBrowserControlClearCookies = start_id++;
	mGluiMediaBrowserControlWindow->add_button( "CLEAR COOKIES", mIdMediaBrowserControlClearCookies, gluiCallbackWrapper );
	mGluiMediaBrowserControlWindow->add_column( false );
	mIdMediaBrowserControlEnableCookies = start_id++;
	mMediaBrowserControlEnableCookies = 0;
	mGluiMediaBrowserControlWindow->add_checkbox( "Enable Cookies", &mMediaBrowserControlEnableCookies, mIdMediaBrowserControlEnableCookies, gluiCallbackWrapper );

	// top window - misc controls
	GLUI* glui_window_misc_control = GLUI_Master.create_glui_subwindow( mAppWindow, GLUI_SUBWINDOW_TOP );
	mIdRandomPanelCount = start_id++;
	mRandomPanelCount = 0;
	glui_window_misc_control->add_checkbox( "Randomize panel count", &mRandomPanelCount, mIdRandomPanelCount, gluiCallbackWrapper );
	glui_window_misc_control->set_main_gfx_window( mAppWindow );
	glui_window_misc_control->add_column( true );
	mIdRandomBookmarks = start_id++;
	mRandomBookmarks = 0;
	glui_window_misc_control->add_checkbox( "Randomize bookmarks", &mRandomBookmarks, mIdRandomBookmarks, gluiCallbackWrapper );
	glui_window_misc_control->set_main_gfx_window( mAppWindow );
	glui_window_misc_control->add_column( true );

	mIdDisableTimeout = start_id++;
	mDisableTimeout = 0;
	glui_window_misc_control->add_checkbox( "Disable plugin timeout", &mDisableTimeout, mIdDisableTimeout, gluiCallbackWrapper );
	glui_window_misc_control->set_main_gfx_window( mAppWindow );
	glui_window_misc_control->add_column( true );

	mIdUsePluginReadThread = start_id++;
	mUsePluginReadThread = 0;
	glui_window_misc_control->add_checkbox( "Use plugin read thread", &mUsePluginReadThread, mIdUsePluginReadThread, gluiCallbackWrapper );
	glui_window_misc_control->set_main_gfx_window( mAppWindow );
	glui_window_misc_control->add_column( true );

	mIdLargePanelSpacing = start_id++;
	mLargePanelSpacing = 0;
	glui_window_misc_control->add_checkbox( "Large Panel Spacing", &mLargePanelSpacing, mIdLargePanelSpacing, gluiCallbackWrapper );
	glui_window_misc_control->set_main_gfx_window( mAppWindow );
	glui_window_misc_control->add_column( true );

	// bottom window - status
	mBottomGLUIWindow = GLUI_Master.create_glui_subwindow( mAppWindow, GLUI_SUBWINDOW_BOTTOM );
	mStatusText = mBottomGLUIWindow->add_statictext( "" );
	mBottomGLUIWindow->set_main_gfx_window( mAppWindow );
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::resetView()
{
	mViewRotationCtrl->reset();

	mViewScaleCtrl->set_x( 0.0f );
	mViewScaleCtrl->set_y( 0.0f );
	mViewScaleCtrl->set_z( 3.0f );

	mViewTranslationCtrl->set_x( 0.0f );
	mViewTranslationCtrl->set_y( 0.0f );
	mViewTranslationCtrl->set_z( 0.0f );
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::makePickTexture( int id, GLuint* texture_handle, unsigned char** texture_pixels )
{
	int pick_texture_width = 1024;
	int pick_texture_height = 1024;
	int pick_texture_depth = 3;
	unsigned char* ptr = new unsigned char[ pick_texture_width * pick_texture_height * pick_texture_depth ];
	for( int y = 0; y < pick_texture_height; ++y )
	{
		for( int x = 0; x < pick_texture_width * pick_texture_depth ; x += pick_texture_depth )
		{
			unsigned long bits = 0L;
			bits |= ( id << 20 ) | ( y << 10 ) | ( x / 3 );
			unsigned char r_component = ( bits >> 16 ) & 0xff;
			unsigned char g_component = ( bits >> 8 ) & 0xff;
			unsigned char b_component = bits & 0xff;

			ptr[ y * pick_texture_width * pick_texture_depth + x + 0 ] = r_component;
			ptr[ y * pick_texture_width * pick_texture_depth + x + 1 ] = g_component;
			ptr[ y * pick_texture_width * pick_texture_depth + x + 2 ] = b_component;
		};
	};

	glGenTextures( 1, texture_handle );

	checkGLError("glGenTextures");
	std::cout << "glGenTextures returned " << *texture_handle << std::endl;

	bindTexture( *texture_handle );
	glTexImage2D( GL_TEXTURE_2D, 0,
					GL_RGB,
						pick_texture_width, pick_texture_height,
							0, GL_RGB, GL_UNSIGNED_BYTE, ptr );

	*texture_pixels = ptr;
}

////////////////////////////////////////////////////////////////////////////////
//
std::string LLMediaPluginTest::mimeTypeFromUrl( std::string& url )
{
	// default to web
	std::string mime_type = "text/html";

	// we may need a more advanced MIME type accessor later :-)
	if ( url.find( ".mov" ) != std::string::npos )	// Movies
		mime_type = "video/quicktime";
	else
	if ( url.find( ".txt" ) != std::string::npos )	// Apple Text descriptors
		mime_type = "video/quicktime";
	else
	if ( url.find( ".mp3" ) != std::string::npos )	// Apple Text descriptors
		mime_type = "video/quicktime";
	else
	if ( url.find( "example://" ) != std::string::npos )	// Example plugin
		mime_type = "example/example";

	return mime_type;
}

////////////////////////////////////////////////////////////////////////////////
//
std::string LLMediaPluginTest::pluginNameFromMimeType( std::string& mime_type )
{
#if LL_DARWIN
	std::string plugin_name( "media_plugin_null.dylib" );
	if ( mime_type == "video/quicktime" )
		plugin_name = "media_plugin_quicktime.dylib";
	else
	if ( mime_type == "text/html" )
		plugin_name = "media_plugin_webkit.dylib";

#elif LL_WINDOWS
	std::string plugin_name( "media_plugin_null.dll" );

	if ( mime_type == "video/quicktime" )
		plugin_name = "media_plugin_quicktime.dll";
	else
	if ( mime_type == "text/html" )
		plugin_name = "media_plugin_webkit.dll";
	else
	if ( mime_type == "example/example" )
		plugin_name = "media_plugin_example.dll";

#elif LL_LINUX
	std::string plugin_name( "libmedia_plugin_null.so" );

	if ( mime_type == "video/quicktime" )
		plugin_name = "libmedia_plugin_quicktime.so";
	else
	if ( mime_type == "text/html" )
		plugin_name = "libmedia_plugin_webkit.so";
#endif
	return plugin_name;
}

////////////////////////////////////////////////////////////////////////////////
//
mediaPanel* LLMediaPluginTest::addMediaPanel( std::string url )
{
	// Get the plugin filename using the URL
	std::string mime_type = mimeTypeFromUrl( url );
	std::string plugin_name = pluginNameFromMimeType( mime_type );

	// create a random size for the new media
	int media_width;
	int media_height;
	getRandomMediaSize( media_width, media_height, mime_type );

	// make a new plugin
	LLPluginClassMedia* media_source = new LLPluginClassMedia(this);

	// tell the plugin what size we asked for
	media_source->setSize( media_width, media_height );

	// Use the launcher start and initialize the plugin
#if LL_DARWIN || LL_LINUX
	std::string launcher_name( "SLPlugin" );
#elif LL_WINDOWS
	std::string launcher_name( "SLPlugin.exe" );
#endif

	// for this test app, use the cwd as the user data path (ugh).
#if LL_WINDOWS
	std::string user_data_path = ".\\";
#else
        char cwd[ FILENAME_MAX ];
	if (NULL == getcwd( cwd, FILENAME_MAX - 1 ))
	{
		std::cerr << "Couldn't get cwd - probably too long - failing to init." << std::endl;
		return NULL;
	}
	std::string user_data_path = std::string( cwd ) + "/";
#endif
	media_source->setUserDataPath(user_data_path);
	media_source->init( launcher_name, plugin_name, false );
	media_source->setDisableTimeout(mDisableTimeout);

	// make a new panel and save parameters
	mediaPanel* panel = new mediaPanel;
	panel->mMediaSource = media_source;
	panel->mStartUrl = url;
	panel->mMimeType = mime_type;
	panel->mMediaWidth = media_width;
	panel->mMediaHeight = media_height;
	panel->mTextureWidth = 0;
	panel->mTextureHeight = 0;
	panel->mTextureScaleX = 0;
	panel->mTextureScaleY = 0;
	panel->mMediaTextureHandle = 0;
	panel->mPickTextureHandle = 0;
	panel->mAppTextureCoordsOpenGL = false;	// really need an 'undefined' state here too
	panel->mReadyToRender = false;

	// look through current media panels to find an unused index number
	bool id_exists = true;
	for( int nid = 0; nid < mMaxPanels; ++nid )
	{
		// does this id exist already?
		id_exists = false;
		for( int pid = 0; pid < (int)mMediaPanels.size(); ++pid )
		{
			if ( nid == mMediaPanels[ pid ]->mId )
			{
				id_exists = true;
				break;
			};
		};

		// id wasn't found so we can use it
		if ( ! id_exists )
		{
			panel->mId = nid;
			break;
		};
	};

	// if we get here and this flag is set, there is no room for any more panels
	if ( id_exists )
	{
		std::cout << "No room for any more panels" << std::endl;
	}
	else
	{
		// now we have the ID we can use it to make the
		// pick texture (id is baked into texture pixels)
		makePickTexture( panel->mId, &panel->mPickTextureHandle, &panel->mPickTexturePixels );

		// save this in the list of panels
		mMediaPanels.push_back( panel );

		// select the panel that was just created
		selectPanel( panel );

		// load and start the URL
		panel->mMediaSource->loadURI( url );
		panel->mMediaSource->start();

		std::cout << "Adding new media panel for " << url << "(" << media_width << "x" << media_height << ") with index " << panel->mId << " - total panels = " << mMediaPanels.size() << std::endl;
	}
	
	return panel;
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::updateMediaPanel( mediaPanel* panel )
{
//	checkGLError("LLMediaPluginTest::updateMediaPanel");

	if ( ! panel )
		return;

	if(!panel->mMediaSource || !panel->mMediaSource->textureValid())
	{
		panel->mReadyToRender = false;
		return;
	}

	// take a reference copy of the plugin values since they
	// might change during this lifetime of this function
	int plugin_media_width = panel->mMediaSource->getWidth();
	int plugin_media_height = panel->mMediaSource->getHeight();
	int plugin_texture_width = panel->mMediaSource->getBitsWidth();
	int plugin_texture_height = panel->mMediaSource->getBitsHeight();

	// If the texture isn't created or the media or texture dimensions changed AND
	// the sizes are valid then we need to delete the old media texture (if necessary)
	// then make a new one.
	if ((panel->mMediaTextureHandle == 0 ||
		 panel->mMediaWidth != plugin_media_width ||
		 panel->mMediaHeight != plugin_media_height ||
		 panel->mTextureWidth != plugin_texture_width ||
		 panel->mTextureHeight != plugin_texture_height) &&
		( plugin_media_width > 0 && plugin_media_height > 0 &&
		  plugin_texture_width > 0 && plugin_texture_height > 0 ) )
	{
		std::cout << "Valid media size (" <<  plugin_media_width << " x " << plugin_media_height
				<< ") and texture size (" <<  plugin_texture_width << " x " << plugin_texture_height
				<< ") for panel with ID=" << panel->mId << " - making texture" << std::endl;

		// delete old GL texture
		if ( isTexture( panel->mMediaTextureHandle ) )
		{
			std::cerr << "updateMediaPanel: deleting texture " << panel->mMediaTextureHandle << std::endl;
			glDeleteTextures( 1, &panel->mMediaTextureHandle );
			panel->mMediaTextureHandle = 0;
		}

		std::cerr << "before: pick texture is " << panel->mPickTextureHandle << ", media texture is " << panel->mMediaTextureHandle << std::endl;

		// make a GL texture based on the dimensions the plugin told us
		GLuint new_texture = 0;
		glGenTextures( 1, &new_texture );

		checkGLError("glGenTextures");

		std::cout << "glGenTextures returned " << new_texture << std::endl;

		panel->mMediaTextureHandle = new_texture;

		bindTexture( panel->mMediaTextureHandle );

		std::cout << "Setting texture size to " << plugin_texture_width << " x " << plugin_texture_height << std::endl;
		glTexImage2D( GL_TEXTURE_2D, 0,
			GL_RGB,
				plugin_texture_width, plugin_texture_height,
					0, GL_RGB, GL_UNSIGNED_BYTE,
						0 );


		std::cerr << "after: pick texture is " << panel->mPickTextureHandle << ", media texture is " << panel->mMediaTextureHandle << std::endl;
	};

	// update our record of the media and texture dimensions
	// NOTE: do this after we we check for sizes changes
	panel->mMediaWidth = plugin_media_width;
	panel->mMediaHeight = plugin_media_height;
	panel->mTextureWidth = plugin_texture_width;
	panel->mTextureHeight = plugin_texture_height;
	if ( plugin_texture_width > 0 )
	{
		panel->mTextureScaleX = (double)panel->mMediaWidth / (double)panel->mTextureWidth;
	};
	if ( plugin_texture_height > 0 )
	{
		panel->mTextureScaleY = (double)panel->mMediaHeight / (double)panel->mTextureHeight;
	};

	// update the flag which tells us if the media source uses OprnGL coords or not.
	panel->mAppTextureCoordsOpenGL = panel->mMediaSource->getTextureCoordsOpenGL();

	// Check to see if we have enough to render this panel.
	// If we do, set a flag that the display functions use so
	// they only render a panel with media if it's ready.
	if ( panel->mMediaWidth < 0 ||
		 panel->mMediaHeight < 0 ||
		 panel->mTextureWidth < 1 ||
		 panel->mTextureHeight < 1 ||
		 panel->mMediaTextureHandle == 0 )
	{
		panel->mReadyToRender = false;
	};
}

////////////////////////////////////////////////////////////////////////////////
//
mediaPanel* LLMediaPluginTest::replaceMediaPanel( mediaPanel* panel, std::string url )
{
	// no media panels so we can't change anything - have to add
	if ( mMediaPanels.size() == 0 )
		return NULL;

	// sanity check
	if ( ! panel )
		return NULL;

	int index;
	for(index = 0; index < (int)mMediaPanels.size(); index++)
	{
		if(mMediaPanels[index] == panel)
			break;
	}

	if(index >= (int)mMediaPanels.size())
	{
		// panel isn't in mMediaPanels
		return NULL;
	}

	std::cout << "Replacing media panel with index " << panel->mId << std::endl;

	int panel_id = panel->mId;

	if(mSelectedPanel == panel)
		mSelectedPanel = NULL;

	delete panel;

	// Get the plugin filename using the URL
	std::string mime_type = mimeTypeFromUrl( url );
	std::string plugin_name = pluginNameFromMimeType( mime_type );

	// create a random size for the new media
	int media_width;
	int media_height;
	getRandomMediaSize( media_width, media_height, mime_type );

	// make a new plugin
	LLPluginClassMedia* media_source = new LLPluginClassMedia(this);

	// tell the plugin what size we asked for
	media_source->setSize( media_width, media_height );

	// Use the launcher start and initialize the plugin
#if LL_DARWIN || LL_LINUX
	std::string launcher_name( "SLPlugin" );
#elif LL_WINDOWS
	std::string launcher_name( "SLPlugin.exe" );
#endif

	// for this test app, use the cwd as the user data path (ugh).
#if LL_WINDOWS
	std::string user_data_path = ".\\";
#else
        char cwd[ FILENAME_MAX ];
	if (NULL == getcwd( cwd, FILENAME_MAX - 1 ))
	{
		std::cerr << "Couldn't get cwd - probably too long - failing to init." << std::endl;
		return NULL;
	}
	std::string user_data_path = std::string( cwd ) + "/";
#endif

	media_source->setUserDataPath(user_data_path);
	media_source->init( launcher_name, plugin_name, false );
	media_source->setDisableTimeout(mDisableTimeout);

	// make a new panel and save parameters
	panel = new mediaPanel;
	panel->mMediaSource = media_source;
	panel->mStartUrl = url;
	panel->mMimeType = mime_type;
	panel->mMediaWidth = media_width;
	panel->mMediaHeight = media_height;
	panel->mTextureWidth = 0;
	panel->mTextureHeight = 0;
	panel->mTextureScaleX = 0;
	panel->mTextureScaleY = 0;
	panel->mMediaTextureHandle = 0;
	panel->mPickTextureHandle = 0;
	panel->mAppTextureCoordsOpenGL = false;	// really need an 'undefined' state here too
	panel->mReadyToRender = false;

	panel->mId = panel_id;

	// Replace the entry in the panels array
	mMediaPanels[index] = panel;

	// now we have the ID we can use it to make the
	// pick texture (id is baked into texture pixels)
	makePickTexture( panel->mId, &panel->mPickTextureHandle, &panel->mPickTexturePixels );

	// select the panel that was just created
	selectPanel( panel );

	// load and start the URL
	panel->mMediaSource->loadURI( url );
	panel->mMediaSource->start();
	
	return panel;
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::getRandomMediaSize( int& width, int& height, std::string mime_type )
{
	// Make a new media source with a random size which we'll either
	// directly or the media plugin will tell us what it wants later.
	// Use a random size so we can test support for weird media sizes.
	// (Almost everything else will get filled in later once the
	// plugin responds)
	// NB. Do we need to enforce that width is on 4 pixel boundary?
	width = ( ( rand() % 170 ) + 30 ) * 4;
	height = ( ( rand() % 170 ) + 30 ) * 4;

	// adjust this random size if it's a browser so we get
	// a more useful size for testing..
	if ( mime_type == "text/html" || mime_type == "example/example"  )
	{
		width = ( ( rand() % 100 ) + 100 ) * 4;
		height = ( width * ( ( rand() % 400 ) + 1000 ) ) / 1000;
	};
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::remMediaPanel( mediaPanel* panel )
{
	// always leave one panel
	if ( mMediaPanels.size() == 1 )
		return;

	// sanity check - don't think this can happen but see above for a case where it might...
	if ( ! panel )
		return;

	std::cout << "Removing media panel with index " << panel->mId << " - total panels = " << mMediaPanels.size() - 1 << std::endl;

	if(mSelectedPanel == panel)
		mSelectedPanel = NULL;

	delete panel;

	// remove from storage list
	for( int i = 0; i < (int)mMediaPanels.size(); ++i )
	{
		if ( mMediaPanels[ i ] == panel )
		{
			mMediaPanels.erase( mMediaPanels.begin() + i );
			break;
		};
	};

	// select the first panel
	selectPanel( mMediaPanels[ 0 ] );
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::updateStatusBar()
{
	if ( ! mSelectedPanel )
		return;

	// cache results - this is a very slow function
	static int cached_id = -1;
	static int cached_media_width = -1;
	static int cached_media_height = -1;
	static int cached_texture_width = -1;
	static int cached_texture_height = -1;
	static bool cached_supports_browser_media = true;
	static bool cached_supports_time_media = false;
	static int cached_movie_time = -1;
	static GLfloat cached_distance = -1.0f;

	static std::string cached_plugin_version = "";
	if (
		 cached_id == mSelectedPanel->mId &&
		 cached_media_width == mSelectedPanel->mMediaWidth &&
		 cached_media_height  == mSelectedPanel->mMediaHeight &&
		 cached_texture_width == mSelectedPanel->mTextureWidth &&
		 cached_texture_height == mSelectedPanel->mTextureHeight &&
		 cached_supports_browser_media == mSelectedPanel->mMediaSource->pluginSupportsMediaBrowser() &&
		 cached_supports_time_media == mSelectedPanel->mMediaSource->pluginSupportsMediaTime() &&
		 cached_plugin_version == mSelectedPanel->mMediaSource->getPluginVersion() &&
		 cached_movie_time == (int)mSelectedPanel->mMediaSource->getCurrentTime() &&
		 cached_distance == mDistanceCameraToSelectedGeometry
	   )
	{
		// nothing changed so don't spend time here
		return;
	};

	std::ostringstream stream( "" );

	stream.str( "" );
	stream.clear();

	stream << "Id: ";
	stream << std::setw( 2 ) << std::setfill( '0' );
	stream << mSelectedPanel->mId;
	stream << " | ";
	stream << "Media: ";
	stream << std::setw( 3 ) << std::setfill( '0' );
	stream << mSelectedPanel->mMediaWidth;
	stream << " x ";
	stream << std::setw( 3 ) << std::setfill( '0' );
	stream << mSelectedPanel->mMediaHeight;
	stream << " | ";
	stream << "Texture: ";
	stream << std::setw( 4 ) << std::setfill( '0' );
	stream << mSelectedPanel->mTextureWidth;
	stream << " x ";
	stream << std::setw( 4 ) << std::setfill( '0' );
	stream << mSelectedPanel->mTextureHeight;

	stream << " | ";
	stream << "Distance: ";
	stream << std::setw( 6 );
	stream << std::setprecision( 3 );
	stream << std::setprecision( 3 );
	stream << mDistanceCameraToSelectedGeometry;
	stream << " | ";

	if ( mSelectedPanel->mMediaSource->pluginSupportsMediaBrowser() )
		stream << "BROWSER";
	else
	if ( mSelectedPanel->mMediaSource->pluginSupportsMediaTime() )
		stream << "TIME   ";
	stream << " | ";
	stream << mSelectedPanel->mMediaSource->getPluginVersion();
	stream << " | ";
	if ( mSelectedPanel->mMediaSource->pluginSupportsMediaTime() )
	{
		stream << std::setw( 3 ) << std::setfill( '0' );
		stream << (int)mSelectedPanel->mMediaSource->getCurrentTime();
		stream << " / ";
		stream << std::setw( 3 ) << std::setfill( '0' );
		stream << (int)mSelectedPanel->mMediaSource->getDuration();
		stream << " @ ";
		stream << (int)mSelectedPanel->mMediaSource->getCurrentPlayRate();
		stream << " | ";
	};

	glutSetWindow( mBottomGLUIWindow->get_glut_window_id() );
	mStatusText->set_text( const_cast< char*>( stream.str().c_str() ) );
	glutSetWindow( mAppWindow );

	// caching
	cached_id = mSelectedPanel->mId;
	cached_media_width = mSelectedPanel->mMediaWidth;
	cached_media_height = mSelectedPanel->mMediaHeight;
	cached_texture_width = mSelectedPanel->mTextureWidth;
	cached_texture_height = mSelectedPanel->mTextureHeight;
	cached_supports_browser_media = mSelectedPanel->mMediaSource->pluginSupportsMediaBrowser();
	cached_supports_time_media = mSelectedPanel->mMediaSource->pluginSupportsMediaTime();
	cached_plugin_version = mSelectedPanel->mMediaSource->getPluginVersion();
	cached_movie_time = (int)mSelectedPanel->mMediaSource->getCurrentTime();
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::dumpPanelInfo()
{
	std::cout << std::endl << "===== Media Panels =====" << std::endl;
	for( int i = 0; i < (int)mMediaPanels.size(); ++i )
	{
		std::cout << std::setw( 2 ) << std::setfill( '0' );
		std::cout << i + 1 << "> ";
		std::cout << "Id: ";
		std::cout << std::setw( 2 ) << std::setfill( '0' );
		std::cout << mMediaPanels[ i ]->mId;
		std::cout << " | ";
		std::cout << "Media: ";
		std::cout << std::setw( 3 ) << std::setfill( '0' );
		std::cout << mMediaPanels[ i ]->mMediaWidth;
		std::cout << " x ";
		std::cout << std::setw( 3 ) << std::setfill( '0' );
		std::cout << mMediaPanels[ i ]->mMediaHeight;
		std::cout << " | ";
		std::cout << "Texture: ";
		std::cout << std::setw( 4 ) << std::setfill( '0' );
		std::cout << mMediaPanels[ i ]->mTextureWidth;
		std::cout << " x ";
		std::cout << std::setw( 4 ) << std::setfill( '0' );
		std::cout << mMediaPanels[ i ]->mTextureHeight;
		std::cout << " | ";
		if ( mMediaPanels[ i ] == mSelectedPanel )
			std::cout << "(selected)";

		std::cout << std::endl;
	};
	std::cout << "========================" << std::endl;
}

////////////////////////////////////////////////////////////////////////////////
//
void LLMediaPluginTest::handleMediaEvent(LLPluginClassMedia* self, EMediaEvent event)
{
	// Uncomment this to make things much, much quieter.
//	return;

	switch(event)
	{
		case MEDIA_EVENT_CONTENT_UPDATED:
			// too spammy -- don't log these
//			std::cerr <<  "Media event:  MEDIA_EVENT_CONTENT_UPDATED " << std::endl;
		break;

		case MEDIA_EVENT_TIME_DURATION_UPDATED:
			// too spammy -- don't log these
//			std::cerr <<  "Media event:  MEDIA_EVENT_TIME_DURATION_UPDATED, time is " << self->getCurrentTime() << " of " << self->getDuration() << std::endl;
		break;

		case MEDIA_EVENT_SIZE_CHANGED:
			std::cerr <<  "Media event:  MEDIA_EVENT_SIZE_CHANGED " << std::endl;
		break;

		case MEDIA_EVENT_CURSOR_CHANGED:
			std::cerr <<  "Media event:  MEDIA_EVENT_CURSOR_CHANGED, new cursor is " << self->getCursorName() << std::endl;
		break;

		case MEDIA_EVENT_NAVIGATE_BEGIN:
			std::cerr <<  "Media event:  MEDIA_EVENT_NAVIGATE_BEGIN " << std::endl;
		break;

		case MEDIA_EVENT_NAVIGATE_COMPLETE:
			std::cerr <<  "Media event:  MEDIA_EVENT_NAVIGATE_COMPLETE, result string is: " << self->getNavigateResultString() << std::endl;
		break;

		case MEDIA_EVENT_PROGRESS_UPDATED:
			std::cerr <<  "Media event:  MEDIA_EVENT_PROGRESS_UPDATED, loading at " << self->getProgressPercent() << "%" << std::endl;
		break;

		case MEDIA_EVENT_STATUS_TEXT_CHANGED:
			std::cerr <<  "Media event:  MEDIA_EVENT_STATUS_TEXT_CHANGED, new status text is: " << self->getStatusText() << std::endl;
		break;

		case MEDIA_EVENT_NAME_CHANGED:
			std::cerr <<  "Media event:  MEDIA_EVENT_NAME_CHANGED, new name is: " << self->getMediaName() << std::endl;
			glutSetWindowTitle( self->getMediaName().c_str() );
		break;

		case MEDIA_EVENT_LOCATION_CHANGED:
		{
			std::cerr <<  "Media event:  MEDIA_EVENT_LOCATION_CHANGED, new uri is: " << self->getLocation() << std::endl;
			mediaPanel* panel = findMediaPanel(self);
			if(panel != NULL)
			{
				panel->mStartUrl = self->getLocation();
				if(panel == mSelectedPanel)
				{
					mUrlEdit->set_text(const_cast<char*>(panel->mStartUrl.c_str()) );
				}
			}
		}
		break;

		case MEDIA_EVENT_CLICK_LINK_HREF:
		{
			std::cerr <<  "Media event:  MEDIA_EVENT_CLICK_LINK_HREF, uri is " << self->getClickURL() << ", target is " << self->getClickTarget() << std::endl;
			// retrieve the event parameters
			std::string url = self->getClickURL();
			std::string target = self->getClickTarget();
			U32 target_type = self->getClickTargetType();

			switch (target_type)
			{
				case LLPluginClassMedia::TARGET_NONE:
					// ignore this click
				break;
				
				case LLPluginClassMedia::TARGET_EXTERNAL:
					// this should open in an external browser, but since this is a test app we don't care.
				break;
				
				case LLPluginClassMedia::TARGET_BLANK:
					// Create a new panel with the specified URL.
					addMediaPanel(url);
				break;

				case LLPluginClassMedia::TARGET_OTHER:
					mediaPanel *target_panel = findMediaPanel(target);
					if(target_panel)
					{
						target_panel = replaceMediaPanel(target_panel, url);
					}
					else
					{
						target_panel = addMediaPanel(url);
					}

					if(target_panel)
					{
						target_panel->mTarget = target;
					}
				break;
			}
		}
		break;

		case MEDIA_EVENT_CLICK_LINK_NOFOLLOW:
			std::cerr <<  "Media event:  MEDIA_EVENT_CLICK_LINK_NOFOLLOW, uri is " << self->getClickURL() << std::endl;
		break;

		case MEDIA_EVENT_PLUGIN_FAILED:
			std::cerr <<  "Media event:  MEDIA_EVENT_PLUGIN_FAILED" << std::endl;
		break;

		case MEDIA_EVENT_PLUGIN_FAILED_LAUNCH:
			std::cerr <<  "Media event:  MEDIA_EVENT_PLUGIN_FAILED_LAUNCH" << std::endl;
		break;

		case MEDIA_EVENT_CLOSE_REQUEST:
			std::cerr <<  "Media event:  MEDIA_EVENT_CLOSE_REQUEST" << std::endl;
		break;
		
		case MEDIA_EVENT_PICK_FILE_REQUEST:
			std::cerr <<  "Media event:  MEDIA_EVENT_PICK_FILE_REQUEST" << std::endl;
			// TODO: display an actual file picker
			self->sendPickFileResponse("cake");
		break;
	}
}

////////////////////////////////////////////////////////////////////////////////
//
static void gluiCallbackWrapper( int control_id )
{
	if ( gApplication )
		gApplication->gluiCallback( control_id );
}

////////////////////////////////////////////////////////////////////////////////
//
void glutReshape( int width, int height )
{
	if ( gApplication )
		gApplication->reshape( width, height );
};

////////////////////////////////////////////////////////////////////////////////
//
void glutDisplay()
{
	if ( gApplication )
		gApplication->display();
};

////////////////////////////////////////////////////////////////////////////////
//
void glutIdle(int update_ms)
{
	GLUI_Master.set_glutTimerFunc( update_ms, glutIdle, update_ms);

	if ( gApplication )
		gApplication->idle();

};

////////////////////////////////////////////////////////////////////////////////
//
void glutKeyboard( unsigned char key, int x, int y )
{
	if ( gApplication )
		gApplication->keyboard( key );
};

////////////////////////////////////////////////////////////////////////////////
//
void glutMousePassive( int x, int y )
{
	if ( gApplication )
		gApplication->mousePassive( x, y );
}

////////////////////////////////////////////////////////////////////////////////
//
void glutMouseMove( int x , int y )
{
	if ( gApplication )
		gApplication->mouseMove( x, y );
}

////////////////////////////////////////////////////////////////////////////////
//
void glutMouseButton( int button, int state, int x, int y )
{
	if ( gApplication )
		gApplication->mouseButton( button, state, x, y );
}

////////////////////////////////////////////////////////////////////////////////
//
int main( int argc, char* argv[] )
{
#if LL_DARWIN
	// Set the current working directory to <application bundle>/Contents/Resources/
	CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
	if(resources_url != NULL)
	{
		CFStringRef resources_string = CFURLCopyFileSystemPath(resources_url, kCFURLPOSIXPathStyle);
		CFRelease(resources_url);
		if(resources_string != NULL)
		{
			char buffer[PATH_MAX] = "";
			if(CFStringGetCString(resources_string, buffer, sizeof(buffer), kCFStringEncodingUTF8))
			{
				chdir(buffer);
			}
			CFRelease(resources_string);
		}
	}
#endif

	glutInit( &argc, argv );
	glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB );

	const int app_window_x = 80;
	const int app_window_y = 0;
	const int app_window_width = 960;
	const int app_window_height = 960;

	glutInitWindowPosition( app_window_x, app_window_y );
	glutInitWindowSize( app_window_width, app_window_height );

	int app_window_handle = glutCreateWindow( "LLMediaPluginTest" );

	glutDisplayFunc( glutDisplay );

	GLUI_Master.set_glutReshapeFunc( glutReshape );
	GLUI_Master.set_glutKeyboardFunc( glutKeyboard );
	GLUI_Master.set_glutMouseFunc( glutMouseButton );

	glutPassiveMotionFunc( glutMousePassive );
	glutMotionFunc( glutMouseMove );

	glutSetWindow( app_window_handle );

	gApplication = new LLMediaPluginTest( app_window_handle, app_window_width, app_window_height );

	// update at approximately 60hz
	int update_ms = 1000 / 60;

	GLUI_Master.set_glutTimerFunc( update_ms, glutIdle, update_ms);

	glutMainLoop();

	delete gApplication;
}