added: new command "XBMC.RestartApp" for skinners to restart only the application
[xbmc:xbmc-antiquated.git] / xbmc / Application.cpp
1
2 #include "stdafx.h"
3 #include <XFont.h>
4 #include "application.h"
5 #include "utils/lcd.h"
6 #include "xbox\iosupport.h"
7 #include "xbox/xkutils.h"
8 #include "utils/log.h"
9 #include "settings.h"
10 #include "util.h"
11 #include "sectionLoader.h"
12 #include "texturemanager.h"
13 #include "stdstring.h"
14 #include "cores/playercorefactory.h"
15 #include "playlistplayer.h"
16 #include "musicdatabase.h"
17 #include "url.h"
18 #include "autorun.h"
19 #include "ActionManager.h"
20 #include "utils/LCDFactory.h"
21 #include "cores/ModPlayer.h"
22 #include "cores/mplayer/ASyncDirectSound.h"
23 #include "GUIButtonControl.h"
24 #include "GUISpinControl.h"
25 #include "GUIListControl.h"
26 #include "GUIThumbnailPanel.h"
27 #include "utils/KaiClient.h"
28 #include "utils/MemUnit.h"
29 #include "FileSystem/DAAPDirectory.h"
30 #include "utils/FanController.h"
31 #include "utils/CharsetConverter.h"
32 #include "XBVideoConfig.h"
33 #include "GUIStandardWindow.h"
34
35 // uncomment this if you want to use release libs in the debug build.
36 // Atm this saves you 7 mb of memory
37
38 //#define USE_RELEASE_LIBS
39
40         #pragma comment (lib,"xbmc/lib/libXenium/XeniumSPIg.lib")
41
42 #if defined(_DEBUG) && !defined(USE_RELEASE_LIBS)
43         #pragma comment (lib,"xbmc/lib/libXBMS/libXBMSd.lib")    // SECTIONNAME=LIBXBMS
44         #pragma comment (lib,"xbmc/lib/libsmb/libsmbd.lib")      // SECTIONNAME=LIBSMB
45         #pragma comment (lib,"xbmc/lib/cximage/ImageLibd.lib")   // SECTIONNAME=CXIMAGE
46         #pragma comment (lib,"xbmc/lib/libID3/i3dlibd.lib")                      // SECTIONNAME=LIBID3
47         #pragma comment (lib,"xbmc/lib/libCDRip/cdripd.lib")             // SECTIONNAME=LIBCDRIP
48         #pragma comment (lib,"xbmc/lib/libLame/liblamed.lib")            // SECTIONNAME=LIBLAME
49         #pragma comment (lib,"xbmc/lib/libOggVorbis/libOggVorbisd.lib")          // SECTIONNAME=LIBOGGVO
50         #pragma comment (lib,"xbmc/lib/libPython/pythond.lib")   // SECTIONNAME=PYTHON,PY_RW
51         #pragma comment (lib,"xbmc/lib/libGoAhead/goaheadd.lib") // SECTIONNAME=LIBHTTP
52         #pragma comment (lib,"xbmc/lib/sqlLite/libSQLited.lib")
53         #pragma comment (lib,"xbmc/lib/libcdio/libcdiod.lib" )
54         #pragma comment (lib,"xbmc/lib/libshout/libshoutd.lib" )
55         #pragma comment (lib,"xbmc/lib/libRTV/libRTVd.lib")    // SECTIONNAME=LIBRTV
56         #pragma comment (lib,"xbmc/lib/mikxbox/mikxboxd.lib")  // SECTIONNAME=MOD_RW,MOD_RX
57         #pragma comment (lib,"xbmc/lib/libsidplay/libsidplayd.lib")                     // SECTIONNAME=SID_RW,SID_RX
58         #pragma comment (lib,"xbmc/lib/libsidplay/libsidutilsd.lib")            // SECTIONNAME=SID_RW,SID_RX
59         #pragma comment (lib,"xbmc/lib/libsidplay/resid_builderd.lib")  // SECTIONNAME=SID_RW,SID_RX
60         #pragma comment (lib,"xbmc/lib/libmp4/libmp4v2d.lib")   // SECTIONNAME=LIBMP4
61         #pragma comment (lib,"xbmc/lib/libxdaap/libxdaapd.lib") // SECTIONNAME=LIBXDAAP
62         #pragma comment (lib,"xbmc/lib/libiconv/libiconvd.lib")
63         #pragma comment (lib,"xbmc/lib/libfribidi/libfribidid.lib")
64 #else
65         #pragma comment (lib,"xbmc/lib/libXBMS/libXBMS.lib")
66         #pragma comment (lib,"xbmc/lib/libsmb/libsmb.lib")
67         #pragma comment (lib,"xbmc/lib/cximage/ImageLib.lib")
68         #pragma comment (lib,"xbmc/lib/libID3/i3dlib.lib")
69         #pragma comment (lib,"xbmc/lib/libCDRip/cdrip.lib")
70         #pragma comment (lib,"xbmc/lib/libLame/liblame.lib")
71         #pragma comment (lib,"xbmc/lib/libOggVorbis/libOggVorbis.lib")
72         #pragma comment (lib,"xbmc/lib/libPython/python.lib")
73         #pragma comment (lib,"xbmc/lib/libGoAhead/goahead.lib")
74         #pragma comment (lib,"xbmc/lib/sqlLite/libSQLite.lib")
75         #pragma comment (lib,"xbmc/lib/libcdio/libcdio.lib")
76         #pragma comment (lib,"xbmc/lib/libshout/libshout.lib")
77         #pragma comment (lib,"xbmc/lib/libRTV/libRTV.lib")
78         #pragma comment (lib,"xbmc/lib/mikxbox/mikxbox.lib")
79         #pragma comment (lib,"xbmc/lib/libsidplay/libsidplay.lib")    // SECTIONNAME=SID_RW,SID_RX
80         #pragma comment (lib,"xbmc/lib/libsidplay/libsidutils.lib")   // SECTIONNAME=SID_RW,SID_RX
81         #pragma comment (lib,"xbmc/lib/libsidplay/resid_builder.lib") // SECTIONNAME=SID_RW,SID_RX
82         #pragma comment (lib,"xbmc/lib/libmp4/libmp4v2.lib")    // SECTIONNAME=LIBMP4
83         #pragma comment (lib,"xbmc/lib/libxdaap/libxdaap.lib")  // SECTIONNAME=LIBXDAAP
84         #pragma comment (lib,"xbmc/lib/libiconv/libiconv.lib")
85         #pragma comment (lib,"xbmc/lib/libfribidi/libfribidi.lib")
86 #endif
87
88 CStdString g_LoadErrorStr;
89
90 //extern IDirectSoundRenderer* m_pAudioDecoder;
91 CApplication::CApplication(void)
92 :m_ctrDpad(220,220)
93 ,m_ctrIR(220,220)
94 {
95         m_iPlaySpeed       = 1;
96         m_bSpinDown        = false;
97   m_bNetworkSpinDown = false;
98         m_dwSpinDownTime   = timeGetTime();
99         m_pWebServer       = NULL;
100         m_pFileZilla       = NULL;
101         m_pPlayer          = NULL;
102         XSetProcessQuantumLength(5); //default=20msec
103         XSetFileCacheSize (256*1024);//default=64kb
104         m_bInactive   = false;                  // CB: SCREENSAVER PATCH
105         m_bScreenSave = false;                  // CB: SCREENSAVER PATCH
106         m_dwSaverTick = timeGetTime();  // CB: SCREENSAVER PATCH
107         m_dwSkinTime  = 0;
108         m_DAAPSong = NULL;
109         m_DAAPPtr = NULL;
110         m_DAAPArtistPtr = NULL;
111 }
112
113 CApplication::~CApplication(void)
114 {
115 }
116
117 // text out routine for below
118 static void __cdecl FEH_TextOut(XFONT* pFont, int iLine, const wchar_t* fmt, ...)
119 {
120         wchar_t buf[100];
121         va_list args;
122         va_start(args, fmt);
123         _vsnwprintf(buf, 100, fmt, args);
124         va_end(args);
125
126         if (!(iLine & 0x8000))
127                 CLog::Log(LOGFATAL, "%S", buf);
128
129         bool Center = (iLine & 0x10000) > 0;
130         pFont->SetTextAlignment(Center ? XFONT_CENTER : XFONT_LEFT);
131
132         iLine &= 0x7fff;
133
134         D3DRECT rc = { 0, 50 + 25*iLine, 720, 50 + 25*(iLine+1) };
135         D3DDevice::Clear(1, &rc, D3DCLEAR_TARGET, 0, 0, 0);
136         pFont->TextOut(g_application.m_pBackBuffer, buf, -1, Center ? 360 : 80, 50 + 25*iLine);
137         D3DDevice::Present(0,0,0,0);
138 }
139
140 // This function does not return!
141 void CApplication::FatalErrorHandler(bool InitD3D, bool MapDrives, bool InitNetwork)
142 {
143         // XBMC couldn't start for some reason...
144         // g_LoadErrorStr should contain the reason
145         CLog::Log(LOGWARNING, "Emergency recovery console starting...");
146
147         bool HaveGamepad = !InitD3D;
148         bool Pal = g_videoConfig.HasPAL();
149         if (InitD3D)
150         {
151                 CLog::Log(LOGINFO, "Init display in default mode: %s", Pal ? "PAL" : "NTSC");
152                 // init D3D with defaults (NTSC or PAL standard res)
153                 m_d3dpp.BackBufferWidth        = 720;
154                 m_d3dpp.BackBufferHeight       = Pal ? 576 : 480;
155                 m_d3dpp.BackBufferFormat       = D3DFMT_LIN_X8R8G8B8;
156                 m_d3dpp.BackBufferCount        = 1;
157                 m_d3dpp.EnableAutoDepthStencil = FALSE;
158                 m_d3dpp.SwapEffect             = D3DSWAPEFFECT_COPY;
159                 m_d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
160
161                 if (!(m_pD3D = Direct3DCreate8(D3D_SDK_VERSION)))
162                 {
163                         CLog::Log(LOGFATAL, "FATAL ERROR: Unable to create Direct3D!");
164                         Sleep(INFINITE); // die
165                 }
166
167                 // Check if we have the required modes available
168                 g_videoConfig.GetModes(m_pD3D);
169                 if (!g_graphicsContext.IsValidResolution(g_stSettings.m_GUIResolution))
170                 {
171                         // Oh uh - doesn't look good for starting in their wanted screenmode
172                         CLog::Log(LOGERROR, "The screen resolution requested is not valid, resetting to a valid mode");
173                         g_stSettings.m_GUIResolution = g_videoConfig.GetSafeMode();
174                         CLog::Log(LOGERROR, "Resetting to mode %s", g_settings.m_ResInfo[g_stSettings.m_GUIResolution].strMode);
175                         // Transfer the resolution information to our graphics context
176                         g_graphicsContext.SetD3DParameters(&m_d3dpp, g_settings.m_ResInfo);
177                         g_graphicsContext.SetGUIResolution(g_stSettings.m_GUIResolution);
178                         CLog::Log(LOGERROR, "Done reset");
179                 }
180
181                 // Create the device
182                 if (m_pD3D->CreateDevice(0, D3DDEVTYPE_HAL, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, &m_d3dpp, &m_pd3dDevice) != S_OK)
183                 {
184                         CLog::Log(LOGFATAL, "FATAL ERROR: Unable to create D3D Device!");
185                         Sleep(INFINITE); // die
186                 }
187
188                 m_pd3dDevice->GetBackBuffer(0, 0, &m_pBackBuffer);
189
190 //              XInitDevices( m_dwNumInputDeviceTypes, m_InputDeviceTypes );
191
192                 // Create the gamepad devices
193 //              HaveGamepad = (XBInput_CreateGamepads(&m_Gamepad) == S_OK);
194         }
195         m_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, 0, 0, 0);
196
197         // D3D is up, load default font
198         XFONT* pFont;
199         if (XFONT_OpenDefaultFont(&pFont) != S_OK)
200         {
201                 CLog::Log(LOGFATAL, "FATAL ERROR: Unable to open default font!");
202                 Sleep(INFINITE); // die
203         }
204
205         // defaults for text
206         pFont->SetBkMode(XFONT_OPAQUE);
207         pFont->SetBkColor(D3DCOLOR_XRGB(0,0,0));
208         pFont->SetTextColor(D3DCOLOR_XRGB(0xff,0x20,0x20));
209
210         int iLine = 0;
211         FEH_TextOut(pFont, iLine++, L"XBMC Fatal Load Error:");
212         char buf[500];
213         strncpy(buf, g_LoadErrorStr.c_str(), 500);
214         buf[499] = 0;
215         char* pNewline = strtok(buf, "\n");
216         while (pNewline)
217         {
218                 FEH_TextOut(pFont, iLine++, L"%S", pNewline);
219                 pNewline = strtok(NULL, "\n");
220         }
221         ++iLine;
222
223         if (MapDrives)
224         {
225                 CIoSupport helper;
226                 // map in default drives
227                 helper.Remap("C:,Harddisk0\\Partition2");
228                 helper.Remount("D:","Cdrom0");
229                 helper.Remap("E:,Harddisk0\\Partition1");
230         }
231
232         if (HaveGamepad)
233                 FEH_TextOut(pFont, (Pal ? 16 : 12) | 0x18000, L"Press start to reboot");
234
235         // Boot up the network for FTP
236         bool NetworkUp = false;
237         IN_ADDR ip_addr;
238         ip_addr.S_un.S_addr = 0;
239
240         if (InitNetwork)
241         {
242                 bool TriedDash = false;
243                 bool ForceDHCP = false;
244                 bool ForceStatic = false;
245                 if (m_bXboxMediacenterLoaded)
246                 {
247                         if (strcmp(g_stSettings.m_strIPAssignment, "dhcp") == 0)
248                         {
249                                 TriedDash = true;
250                                 ForceDHCP = true;
251                         }
252                         else if (strcmp(g_stSettings.m_strIPAssignment, "static") == 0)
253                         {
254                                 ForceStatic = true;
255                                 TriedDash = true;
256                         }
257                 }
258
259                 for (;;)
260                 {
261                         if (!(XNetGetEthernetLinkStatus() & XNET_ETHERNET_LINK_ACTIVE))
262                         {
263                                 FEH_TextOut(pFont, iLine, L"Network cable unplugged");
264                         }
265                         else
266                         {
267                                 int err = 1;
268                                 if (!TriedDash)
269                                 {
270                                         if (!m_bXboxMediacenterLoaded)
271                                                 TriedDash = true;
272                                         FEH_TextOut(pFont, iLine, L"Init network using dash settings...");
273                                         XNetStartupParams xnsp;
274                                         memset(&xnsp, 0, sizeof(xnsp));
275                                         xnsp.cfgSizeOfStruct = sizeof(XNetStartupParams);
276
277                                         // Bypass security so that we may connect to 'untrusted' hosts
278                                         xnsp.cfgFlags = XNET_STARTUP_BYPASS_SECURITY;
279                                         // create more memory for networking
280                                         xnsp.cfgPrivatePoolSizeInPages = 64; // == 256kb, default = 12 (48kb)
281                                         xnsp.cfgEnetReceiveQueueLength = 16; // == 32kb, default = 8 (16kb)
282                                         xnsp.cfgIpFragMaxSimultaneous = 16; // default = 4
283                                         xnsp.cfgIpFragMaxPacketDiv256 = 32; // == 8kb, default = 8 (2kb)
284                                         xnsp.cfgSockMaxSockets = 64; // default = 64
285                                         xnsp.cfgSockDefaultRecvBufsizeInK = 128; // default = 16
286                                         xnsp.cfgSockDefaultSendBufsizeInK = 128; // default = 16
287                                         err = XNetStartup(&xnsp);
288                                 }
289
290                                 if (err && TriedDash)
291                                 {
292                                         if (!ForceStatic)
293                                         {
294                                                 FEH_TextOut(pFont, iLine, L"Init network using DHCP...");
295                                                 network_info ni;
296                                                 memset(&ni, 0, sizeof(ni));
297                                                 ni.DHCP = true;
298                                                 int iCount=0;
299                                                 while ((err = CUtil::SetUpNetwork(iCount == 0, ni)) == 1 && iCount < 100)
300                                                 {
301                                                         Sleep(50);
302                                                         ++iCount;
303
304                                                         if (HaveGamepad)
305                                                         {
306                                                                 ReadInput();
307                                                                 if (m_DefaultGamepad.wPressedButtons & XINPUT_GAMEPAD_START)
308                                                                         XKUtils::XBOXPowerCycle();
309                                                         }
310                                                 }
311                                         }
312
313                                         if ((err || ForceStatic) && !ForceDHCP)
314                                         {
315                                                 XNetCleanup();
316
317                                                 FEH_TextOut(pFont, iLine, L"Init network using static ip...");
318                                                 network_info ni;
319                                                 memset(&ni, 0, sizeof(ni));
320                                                 if (ForceStatic)
321                                                 {
322                                                         strcpy(ni.ip, g_stSettings.m_strLocalIPAdres);
323                                                         strcpy(ni.subnet, g_stSettings.m_strLocalNetmask);
324                                                         strcpy(ni.gateway, g_stSettings.m_strGateway);
325                                                         strcpy(ni.DNS1, g_stSettings.m_strNameServer);
326                                                 }
327                                                 else
328                                                 {
329                                                         strcpy(ni.ip, "192.168.0.42");
330                                                         strcpy(ni.subnet, "255.255.255.0");
331                                                         strcpy(ni.gateway, "192.168.0.1");
332                                                         strcpy(ni.DNS1, "192.168.0.1");
333                                                 }
334                                                 int iCount=0;
335                                                 while ((err = CUtil::SetUpNetwork(iCount == 0, ni)) == 1 && iCount < 100)
336                                                 {
337                                                         Sleep(50);
338                                                         ++iCount;
339
340                                                         if (HaveGamepad)
341                                                         {
342                                                                 ReadInput();
343                                                                 if (m_DefaultGamepad.wPressedButtons & XINPUT_GAMEPAD_START)
344                                                                         XKUtils::XBOXPowerCycle();
345                                                         }
346                                                 }
347                                         }
348                                 }
349
350                                 if (!err)
351                                 {
352                                         XNADDR xna;
353                                         DWORD dwState;
354                                         do
355                                         {
356                                                 dwState = XNetGetTitleXnAddr(&xna);
357                                                 Sleep(50);
358
359                                                 if (HaveGamepad)
360                                                 {
361                                                         ReadInput();
362                                                         if (m_DefaultGamepad.wPressedButtons & XINPUT_GAMEPAD_START)
363                                                                 XKUtils::XBOXPowerCycle();
364                                                 }
365                                         } while (dwState==XNET_GET_XNADDR_PENDING);
366                                         ip_addr = xna.ina;
367
368                                         if (ip_addr.S_un.S_addr)
369                                         {
370                                                 WSADATA WsaData;
371                                                 err = WSAStartup( MAKEWORD(2,2), &WsaData );
372                                                 if (err)
373                                                         FEH_TextOut(pFont, iLine, L"Winsock init error: %d", err);
374                                                 else
375                                                         NetworkUp = true;
376                                         }
377                                 }
378                         }
379                         if (!NetworkUp)
380                         {
381                                 XNetCleanup();
382                                 int n = 10;
383                                 while (n)
384                                 {
385                                         FEH_TextOut(pFont, (iLine+1)|0x8000, L"Unable to init network, retrying in %d seconds", n--);
386                                         for (int i = 0; i < 20; ++i)
387                                         {
388                                                 Sleep(50);
389
390                                                 if (HaveGamepad)
391                                                 {
392                                                         ReadInput();
393                                                         if (m_DefaultGamepad.wPressedButtons & XINPUT_GAMEPAD_START)
394                                                                 XKUtils::XBOXPowerCycle();
395                                                 }
396                                         }
397                                 }
398                         }
399                         else
400                                 break;
401                 }
402                 ++iLine;
403         }
404         else
405         {
406                 NetworkUp = true;
407                 XNADDR xna;
408                 DWORD dwState;
409                 do
410                 {
411                         dwState = XNetGetTitleXnAddr(&xna);
412                         Sleep(50);
413
414                         if (HaveGamepad)
415                         {
416                                 ReadInput();
417                                 if (m_DefaultGamepad.wPressedButtons & XINPUT_GAMEPAD_START)
418                                         XKUtils::XBOXPowerCycle();
419                         }
420                 } while (dwState==XNET_GET_XNADDR_PENDING);
421                 ip_addr = xna.ina;
422         }
423         char addr[32];
424         XNetInAddrToString(ip_addr,addr,32);
425         FEH_TextOut(pFont, iLine++, L"IP Address: %S", addr);
426         ++iLine;
427
428         if (NetworkUp)
429         {
430                 // Start FTP with default settings
431                 FEH_TextOut(pFont, iLine++, L"Starting FTP server...");
432
433                 m_pFileZilla = new CXBFileZilla(NULL);
434                 m_pFileZilla->Start();
435
436                 // Default settings
437                 m_pFileZilla->mSettings.SetMaxUsers(1);
438                 m_pFileZilla->mSettings.SetWelcomeMessage("XBMC emergency recovery console FTP.");
439
440                 // default user
441                 CXFUser* pUser;
442                 m_pFileZilla->AddUser("xbox", pUser);
443                 pUser->SetPassword("xbox");
444                 pUser->SetShortcutsEnabled(false);
445                 pUser->SetUseRelativePaths(false);
446                 pUser->SetBypassUserLimit(false);
447                 pUser->SetUserLimit(0);
448                 pUser->SetIPLimit(0);
449                 pUser->AddDirectory("/", XBFILE_READ|XBFILE_WRITE|XBFILE_DELETE|XBFILE_APPEND|XBDIR_DELETE|XBDIR_CREATE|XBDIR_LIST|XBDIR_SUBDIRS|XBDIR_HOME);
450                 pUser->AddDirectory("C:\\", XBFILE_READ|XBFILE_WRITE|XBFILE_DELETE|XBFILE_APPEND|XBDIR_DELETE|XBDIR_CREATE|XBDIR_LIST|XBDIR_SUBDIRS);
451                 pUser->AddDirectory("D:\\", XBFILE_READ|XBDIR_LIST|XBDIR_SUBDIRS);
452                 pUser->AddDirectory("E:\\", XBFILE_READ|XBFILE_WRITE|XBFILE_DELETE|XBFILE_APPEND|XBDIR_DELETE|XBDIR_CREATE|XBDIR_LIST|XBDIR_SUBDIRS);
453                 pUser->AddDirectory("Q:\\", XBFILE_READ|XBFILE_WRITE|XBFILE_DELETE|XBFILE_APPEND|XBDIR_DELETE|XBDIR_CREATE|XBDIR_LIST|XBDIR_SUBDIRS);
454                 pUser->CommitChanges();
455
456                 FEH_TextOut(pFont, iLine++, L"FTP server running on port %d, login: xbox/xbox", m_pFileZilla->mSettings.GetServerPort());
457                 ++iLine;
458         }
459
460         if (HaveGamepad)
461         {
462                 for (;;)
463                 {
464                         Sleep(50);
465                         ReadInput();
466                         if (m_DefaultGamepad.wPressedButtons & XINPUT_GAMEPAD_START)
467                                 XKUtils::XBOXPowerCycle();
468                 }
469         }
470         else
471                 Sleep(INFINITE);
472 }
473
474 HRESULT CApplication::Create()
475 {
476         CIoSupport helper;
477         CStdString strPath;
478         char szDevicePath[1024];
479
480         // map Q to home drive of xbe to load the config file
481         CUtil::GetHomePath(strPath);
482         helper.GetPartition(strPath, szDevicePath);
483         strcat(szDevicePath,&strPath.c_str()[2]);
484
485         helper.Mount("Q:","Harddisk0\\Partition2");
486         helper.Unmount("Q:");
487         helper.Mount("Q:", szDevicePath);
488
489         ::DeleteFile("Q:\\xbmc.old.log");
490         ::MoveFile("Q:\\xbmc.log","Q:\\xbmc.old.log");
491         CLog::Log(LOGNOTICE, "-----------------------------------------------------------------------");
492         CLog::Log(LOGNOTICE, "starting...");
493         CLog::Log(LOGNOTICE, "Q is mapped to:%s",szDevicePath);
494
495         // Initialize core peripheral port support. Note: If these parameters
496         // are 0 and NULL, respectively, then the default number and types of
497         // controllers will be initialized.
498         XInitDevices( m_dwNumInputDeviceTypes, m_InputDeviceTypes );
499
500         // Create the gamepad devices
501         HRESULT hr;
502         if( FAILED(hr = XBInput_CreateGamepads(&m_Gamepad)) )
503         {
504                 CLog::Log(LOGERROR, "XBAppEx: Call to CreateGamepads() failed!" );
505                 return hr;
506         }
507
508         if( FAILED(hr = XBInput_CreateIR_Remotes()) )
509         {
510                 CLog::Log(LOGERROR, "XBAppEx: Call to CreateIRRemotes() failed!" );
511                 return hr;
512         }
513
514         // Create the Mouse and Keyboard devices
515         g_Mouse.Initialize();
516         g_Keyboard.Initialize();
517
518         // Wait for controller polling to finish. in an elegant way, instead of a Sleep(1000)
519         while (XGetDeviceEnumerationStatus()==XDEVICE_ENUMERATION_BUSY) {
520                 ReadInput();
521         }
522
523         //Check for START+BACK and BLACK+WHITE
524         if (m_DefaultGamepad.wButtons & XINPUT_GAMEPAD_START+XINPUT_GAMEPAD_BACK ||
525                 m_DefaultGamepad.bAnalogButtons[XINPUT_GAMEPAD_BLACK] && m_DefaultGamepad.bAnalogButtons[XINPUT_GAMEPAD_WHITE])
526         {
527                 CLog::Log(LOGINFO, "Key combination detected for TDATA deletion (START+BACK or BLACK+WHITE)");
528                 CUtil::DeleteTDATA();
529         }
530
531         CLog::Log(LOGNOTICE, "load settings...");
532         g_LoadErrorStr = "Unable to load settings";
533         m_bAllSettingsLoaded=g_settings.Load(m_bXboxMediacenterLoaded,m_bSettingsLoaded);
534         if (!m_bAllSettingsLoaded)
535                 FatalErrorHandler(true, true, true);
536
537   // Transfer the resolution information to our graphics context
538         g_graphicsContext.SetD3DParameters(&m_d3dpp, g_settings.m_ResInfo);
539
540         CLog::Log(LOGINFO, "map drives...");
541         CLog::Log(LOGINFO, "  map drive C:");
542         helper.Remap("C:,Harddisk0\\Partition2");
543
544         CLog::Log(LOGINFO, "  map drive E:");
545         helper.Remap("E:,Harddisk0\\Partition1");
546
547         CLog::Log(LOGINFO, "  map drive D:");
548         helper.Remount("D:","Cdrom0");
549
550         if (g_stSettings.m_bUseFDrive)
551         {
552                 CLog::Log(LOGINFO, "  map drive F:");
553                 helper.Remap("F:,Harddisk0\\Partition6");
554         }
555
556         // used for the LBA-48 hack allowing >120 gig
557         if (g_stSettings.m_bUseGDrive)
558         {
559                 CLog::Log(LOGINFO, "  map drive G:");
560                 helper.Remap("G:,Harddisk0\\Partition7");
561         }
562
563         // check settings to see if another home dir is defined.
564         // if there is, we check if it's a xbmc dir and map to it Q:
565         if (strlen(g_stSettings.szHomeDir) > 1)
566         {
567                 CLog::Log(LOGNOTICE, "map Q: to homedir:%s...",g_stSettings.szHomeDir);
568                 // home dir is defined in xboxmediacenter.xml
569                 CStdString strHomePath = g_stSettings.szHomeDir;
570
571                 if(!access(strHomePath + "\\skin", 0))
572                 {
573                         helper.GetPartition(strHomePath, szDevicePath);
574                         strcat(szDevicePath, &strHomePath.c_str()[2]);
575
576                         CLog::Close();
577                         helper.Unmount("Q:");
578                         helper.Mount("Q:", szDevicePath);
579                         ::DeleteFile("Q:\\xbmc.old.log");
580                         ::MoveFile("Q:\\xbmc.log","Q:\\xbmc.old.log");
581                         CLog::Close();
582                         CLog::Log(LOGNOTICE, "Q is mapped to:%s",szDevicePath);
583                 }
584                 else
585                 {
586                         g_LoadErrorStr = "Invalid <home> tag in xml - no skins found";
587                         FatalErrorHandler(true, false, true);
588                 }
589         }
590
591         CStdString strLanguagePath;
592         strLanguagePath.Format("Q:\\language\\%s\\strings.xml", g_stSettings.szDefaultLanguage);
593
594         CLog::Log(LOGINFO, "load language file:%s",strLanguagePath.c_str());
595         if (!g_localizeStrings.Load(strLanguagePath ))
596                 FatalErrorHandler(true, false, true);
597
598         CLog::Log(LOGINFO, "load keymapping");
599         if (!g_buttonTranslator.Load())
600                 FatalErrorHandler(true, false, true);
601
602         CLog::Log(LOGINFO, "setup DirectX");
603         g_graphicsContext.SetGUIResolution(g_stSettings.m_GUIResolution);
604         g_graphicsContext.SetOffset(g_stSettings.m_iUIOffsetX, g_stSettings.m_iUIOffsetY);
605
606         int  iResolution=g_stSettings.m_GUIResolution;
607         CLog::Log(LOGINFO, " GUI format %ix%i %s",
608                 g_settings.m_ResInfo[iResolution].iWidth,
609                 g_settings.m_ResInfo[iResolution].iHeight,
610                 g_settings.m_ResInfo[iResolution].strMode);
611         CLog::Log(LOGINFO, " GUI screen offset (%i,%i)", g_stSettings.m_iUIOffsetX, g_stSettings.m_iUIOffsetY);
612         m_gWindowManager.Initialize();
613         g_actionManager.SetScriptActionCallback(&g_pythonParser);
614
615         return CXBApplicationEx::Create();
616 }
617
618 HRESULT CApplication::Initialize()
619 {
620         CLog::Log(LOGINFO, "creating subdirectories");
621         if (g_stSettings.szThumbnailsDirectory[0]==0)
622         {
623                 strcpy(g_stSettings.szThumbnailsDirectory,"Q:\\thumbs");
624         }
625         if (g_stSettings.m_szShortcutDirectory[0]==0 && !g_stSettings.m_bMyProgramsNoShortcuts)
626         {
627                 strcpy(g_stSettings.m_szShortcutDirectory,"Q:\\shortcuts");
628         }
629         if (g_stSettings.m_szAlbumDirectory[0]==0)
630         {
631                 strcpy(g_stSettings.m_szAlbumDirectory,"Q:\\albums");
632         }
633         if (g_stSettings.m_szMusicRecordingDirectory[0]==0)
634         {
635                 strcpy(g_stSettings.m_szMusicRecordingDirectory,"Q:\\recordings");
636         }
637         if (g_stSettings.m_szScreenshotsDirectory[0]==0)
638         {
639                 strcpy(g_stSettings.m_szScreenshotsDirectory, "Q:\\screenshots");
640         }
641
642         CreateDirectory(g_stSettings.szThumbnailsDirectory,NULL);
643         CStdString strThumbIMDB=g_stSettings.szThumbnailsDirectory;
644         strThumbIMDB+="\\imdb";
645         CreateDirectory(strThumbIMDB.c_str(),NULL);
646
647         if (!g_stSettings.m_bMyProgramsNoShortcuts)
648                 CreateDirectory(g_stSettings.m_szShortcutDirectory,NULL);
649         CreateDirectory(g_stSettings.m_szAlbumDirectory,NULL);
650         CreateDirectory(g_stSettings.m_szMusicRecordingDirectory,NULL);
651         CreateDirectory(g_stSettings.m_szScreenshotsDirectory, NULL);
652
653         CLog::Log(LOGINFO, "  thumbnails folder:%s", g_stSettings.szThumbnailsDirectory);
654         CLog::Log(LOGINFO, "  shortcuts folder:%s", g_stSettings.m_szShortcutDirectory);
655         CLog::Log(LOGINFO, "  albums folder:%s", g_stSettings.m_szAlbumDirectory);
656         CLog::Log(LOGINFO, "  recording folder:%s", g_stSettings.m_szMusicRecordingDirectory);
657         CLog::Log(LOGINFO, "  screenshots folder:%s", g_stSettings.m_szScreenshotsDirectory);
658
659         string strAlbumDir=g_stSettings.m_szAlbumDirectory;
660         CreateDirectory((strAlbumDir+"\\playlists").c_str(),NULL);
661         CreateDirectory((strAlbumDir+"\\cddb").c_str(),NULL);
662         CreateDirectory((strAlbumDir+"\\thumbs").c_str(),NULL); // contains the album thumbs
663         CreateDirectory((strAlbumDir+"\\thumbs\\temp").c_str(),NULL);
664         CreateDirectory("Q:\\python",NULL);
665         CreateDirectory("Q:\\python\\Lib",NULL);
666         CreateDirectory("Q:\\python\\temp",NULL);
667         CreateDirectory("Q:\\scripts",NULL);
668         CreateDirectory("Q:\\language",NULL);
669         CreateDirectory("Q:\\visualisations",NULL);
670
671         if (g_stSettings.m_szAlternateSubtitleDirectory[0]==0)
672         {
673                 strcpy(g_stSettings.m_szAlternateSubtitleDirectory,"Q:\\subtitles");
674         }
675         CLog::Log(LOGINFO, "  subtitle folder:%s", g_stSettings.m_szAlternateSubtitleDirectory);
676         CreateDirectory(g_stSettings.m_szAlternateSubtitleDirectory,NULL);
677
678         InitMemoryUnits();
679
680         // initialize network
681         if (!m_bXboxMediacenterLoaded)
682         {
683                 CLog::Log(LOGINFO, "using default network settings");
684                 strcpy (g_stSettings.m_strLocalIPAdres,"192.168.0.100");
685                 strcpy (g_stSettings.m_strLocalNetmask,"255.255.255.0");
686                 strcpy (g_stSettings.m_strGateway,"192.168.0.1");
687                 strcpy (g_stSettings.m_strNameServer,"192.168.0.1");
688                 g_stSettings.m_bFTPServerEnabled=true;
689                 g_stSettings.m_bHTTPServerEnabled=false;
690                 g_stSettings.m_bTimeServerEnabled=false;
691         }
692         CLog::Log(LOGNOTICE, "initialize assignment:[%s] network ip:[%s] netmask:[%s] gateway:[%s] nameserver:[%s]",
693                 g_stSettings.m_strIPAssignment,
694                 g_stSettings.m_strLocalIPAdres,
695                 g_stSettings.m_strLocalNetmask,
696                 g_stSettings.m_strGateway,
697                 g_stSettings.m_strNameServer);
698
699         if ( !CUtil::InitializeNetwork(g_stSettings.m_strIPAssignment,
700                 g_stSettings.m_strLocalIPAdres,
701                 g_stSettings.m_strLocalNetmask,
702                 g_stSettings.m_strGateway,
703                 g_stSettings.m_strNameServer) )
704         {
705                 CLog::Log(LOGERROR, "initialize network failed");
706         }
707
708         // set filters
709         g_graphicsContext.Get3DDevice()->SetTextureStageState(0, D3DTSS_MINFILTER, g_stSettings.m_minFilter );
710         g_graphicsContext.Get3DDevice()->SetTextureStageState(0, D3DTSS_MAGFILTER, g_stSettings.m_maxFilter );
711
712         g_graphicsContext.SetD3DDevice(m_pd3dDevice);
713
714   StartServices();
715
716   CLog::Log(LOGNOTICE, "load default skin:[%s]",g_stSettings.szDefaultSkin);
717         LoadSkin(g_stSettings.szDefaultSkin);
718
719         CLog::Log(LOGINFO, "initializing skin");
720         m_gWindowManager.Add(&m_guiHome);                                                                                       // window id = 0
721         m_gWindowManager.Add(&m_guiPrograms);                                                                   // window id = 1
722         m_gWindowManager.Add(&m_guiPictures);                                                                   // window id = 2
723         m_gWindowManager.Add(&m_guiFileManager);                                                        // window id = 3
724         m_gWindowManager.Add(&m_guiMyVideo);                                                                    // window id = 6
725         m_gWindowManager.Add(&m_guiSettings);                                                                   // window id = 4
726         m_gWindowManager.Add(&m_guiSystemInfo);                                                         // window id = 7
727         m_gWindowManager.Add(&m_guiSettingsGeneral);                                    // window id = 8
728         //m_gWindowManager.Add(&m_guiSettingsScreen);                           // window id = 9
729         m_gWindowManager.Add(&m_guiSettingsMyVideo);                            // window id = 9
730         m_gWindowManager.Add(&m_guiSettingsUICalibration);              // window id = 10
731         m_gWindowManager.Add(&m_guiSettingsScreenCalibration);  // window id = 11
732         m_gWindowManager.Add(&m_guiSettingsSlideShow);                          // window id = 12 slideshow:window id 2007
733         m_gWindowManager.Add(&m_guiSettingsFilter);                                             // window id = 13
734         //m_gWindowManager.Add(&m_guiSettingsMusic);                            // window id = 14
735         m_gWindowManager.Add(&m_guiSettingsMyMusic);                            // window id = 14
736         m_gWindowManager.Add(&m_guiSettingsSubtitles);                          // window id = 15
737         m_gWindowManager.Add(&m_guiSettingsScreensaver);                                // window id = 16
738         m_gWindowManager.Add(&m_guiSettingsSkinLanguage);                       // window id = 19
739         m_gWindowManager.Add(&m_guiScripts);                                                                    // window id = 20
740         m_gWindowManager.Add(&m_guiVideoGenre);                                                         // window id = 21
741         m_gWindowManager.Add(&m_guiVideoActors);                                                        // window id = 22
742         m_gWindowManager.Add(&m_guiVideoYear);                                                    // window id = 23
743         m_gWindowManager.Add(&m_guiSettingsPrograms);                                   // window id = 24
744         m_gWindowManager.Add(&m_guiVideoTitle);                                                   // window id = 25
745         m_gWindowManager.Add(&m_guiSettingsCache);                                              // window id = 26
746         m_gWindowManager.Add(&m_guiSettingsAutoRun);                                    // window id = 27
747         m_gWindowManager.Add(&m_guiMyVideoPlayList);                                    // window id = 28
748         m_gWindowManager.Add(&m_guiSettingsCDRipper);                                   // window id = 32
749         m_gWindowManager.Add(&m_guiSettingsLCD);                                        // window id = 29
750         m_gWindowManager.Add(&m_guiSettingsUserInterface);                      // window id = 30
751         m_gWindowManager.Add(&m_guiSettingsAudio);                                          // window id = 31
752   m_gWindowManager.Add(&m_guiSettingsSytem);            // window id = 32
753   m_gWindowManager.Add(&m_guiSettingsCDRipper);                                 // window id = 33
754   m_gWindowManager.Add(&m_guiSettingsProfile);          // window id = 34
755   m_gWindowManager.Add(&m_guiSettingsNetwork);          // window id = 35
756     m_gWindowManager.Add(&m_guiSettingsNetworkProxy);          // window id = 36
757         m_gWindowManager.Add(&m_guiSettingsNetworkKai);          // window id = 37
758         m_gWindowManager.Add(&m_guiSettingsNetworkIP);          // window id = 38
759         m_gWindowManager.Add(&m_guiSettingsNetworkWeb);          // window id = 39
760         m_gWindowManager.Add(&m_guiSettingsNetworkFTP);          // window id = 40
761         m_gWindowManager.Add(&m_guiSettingsNetworkTime);          // window id = 41
762         m_gWindowManager.Add(&m_guiSettingsCdg);                                                //window id =42
763
764         m_gWindowManager.Add(&m_guiDialogYesNo);                                                        // window id = 100
765         m_gWindowManager.Add(&m_guiDialogProgress);                                             // window id = 101
766         m_gWindowManager.Add(&m_guiDialogInvite);                                               // window id = 102
767         m_gWindowManager.Add(&m_guiDialogKeyboard);                                             // window id = 103
768         m_gWindowManager.Add(&m_guiDialogVolumeBar);                                    // window id = 104
769         m_gWindowManager.Add(&m_guiDialogSubMenu);                                              // window id = 105
770         m_gWindowManager.Add(&m_guiDialogContextMenu);                          // window id = 106
771         m_gWindowManager.Add(&m_guiMyMusicPlayList);                                    // window id = 500
772         m_gWindowManager.Add(&m_guiMyMusicSongs);                                                       // window id = 501
773         m_gWindowManager.Add(&m_guiMyMusicAlbum);                                                       // window id = 502
774         m_gWindowManager.Add(&m_guiMyMusicArtists);                                             // window id = 503
775         m_gWindowManager.Add(&m_guiMyMusicGenres);                                              // window id = 504
776         m_gWindowManager.Add(&m_guiMyMusicTop100);                                              // window id = 505
777         m_gWindowManager.Add(&m_keyboard);                                                                              // window id = 1000
778         m_gWindowManager.Add(&m_guiDialogSelect);                                                       // window id = 2000
779         m_gWindowManager.Add(&m_guiMusicInfo);                                                          // window id = 2001
780         m_gWindowManager.Add(&m_guiDialogOK);                                                                   // window id = 2002
781         m_gWindowManager.Add(&m_guiVideoInfo);                                                          // window id = 2003
782         m_gWindowManager.Add(&m_guiScriptsInfo);                                                        // window id = 2004
783         m_gWindowManager.Add(&m_guiWindowFullScreen);                                   // window id = 2005
784         m_gWindowManager.Add(&m_guiWindowVisualisation);                        // window id = 2006
785         m_gWindowManager.Add(&m_guiWindowSlideshow);                                    // window id = 2007
786         m_gWindowManager.Add(&m_guiDialogFileStacking);                         // window id = 2008
787         m_gWindowManager.Add(&m_guiWindowOSD);                                          // window id = 2901
788         m_gWindowManager.Add(&m_guiWindowScreensaver);                          // window id = 2900 Screensaver
789         m_gWindowManager.Add(&m_guiMyWeather);                                              // window id = 2600 WEATHER
790         m_gWindowManager.Add(&m_guiSettingsWeather);                              // window id = 17 WEATHER SETTINGS
791         m_gWindowManager.Add(&m_guiMyBuddies);                                              // window id = 2700 BUDDIES
792         CKaiClient::GetInstance()->SetObserver(&m_guiMyBuddies);
793
794         /* window id's 3000 - 3100 are reserved for python */
795         CLog::Log(LOGINFO, "initializing virtual keyboard");
796         m_keyboard.Initialize();
797
798         m_ctrDpad.SetDelays(g_stSettings.m_iMoveDelayController,g_stSettings.m_iRepeatDelayController);
799         m_ctrIR.SetDelays(g_stSettings.m_iMoveDelayIR,g_stSettings.m_iRepeatDelayIR);
800
801         m_gWindowManager.ActivateWindow(g_stSettings.m_iStartupWindow);
802         CLog::Log(LOGINFO, "removing tempfiles");
803         CUtil::RemoveTempFiles();
804
805         if (!m_bAllSettingsLoaded)
806         {
807                 CLog::Log(LOGWARNING, "settings not correct, show dialog");
808                 CStdString test;
809                 CUtil::GetHomePath(test);
810                 m_guiDialogOK.SetHeading(279);
811                 m_guiDialogOK.SetLine(0,"Error while loading settings");
812                 m_guiDialogOK.SetLine(1,test);
813                 m_guiDialogOK.SetLine(2,L"");
814                 m_guiDialogOK.DoModal(g_stSettings.m_iStartupWindow);
815         }
816
817         CLog::Log(LOGNOTICE, "initialize done");
818         CUtil::InitGamma();
819         if (g_stSettings.m_bLCDUsed)
820         {
821                 if (g_stSettings.m_iLCDMode==LCD_MODE_NOTV)
822                 {
823                         // jump to my music when we're in NO tv mode
824                         m_gWindowManager.ActivateWindow(WINDOW_MUSIC_FILES);
825                 }
826         }
827         return S_OK;
828 }
829
830 void CApplication::StartWebServer()
831 {
832         if (g_stSettings.m_bHTTPServerEnabled && CUtil::IsNetworkUp())
833         {
834                 CLog::Log(LOGNOTICE, "start webserver");
835                 CSectionLoader::Load("LIBHTTP");
836                 m_pWebServer = new CWebServer();
837                 CStdString ipadres;
838                 CUtil::GetTitleIP(ipadres);
839                 m_pWebServer->Start(ipadres.c_str(), g_stSettings.m_iWebServerPort, "Q:\\web");
840         }
841 }
842
843 void CApplication::StopWebServer()
844 {
845         if (m_pWebServer)
846         {
847                 CLog::Log(LOGNOTICE, "stop webserver");
848                 m_pWebServer->Stop();
849                 delete m_pWebServer;
850                 m_pWebServer = NULL;
851     CSectionLoader::Unload("LIBHTTP");
852         }
853 }
854
855 void CApplication::StartFtpServer()
856 {
857         if ( g_stSettings.m_bFTPServerEnabled && CUtil::IsNetworkUp())
858         {
859                 CLog::Log(LOGNOTICE, "start ftpserver");
860     if (!m_pFileZilla) {
861                         m_pFileZilla = new CXBFileZilla("Q:\\");
862                 m_pFileZilla->Start();
863     }
864         }
865 }
866
867 void CApplication::StopFtpServer()
868 {
869   /* filezilla doesn't like to be deleted?
870   if (m_pFileZilla) {
871                 CLog::Log(LOGINFO, "stop ftpserver");
872                 m_pFileZilla->Stop();
873     delete m_pFileZilla;
874     m_pFileZilla = NULL;
875   }
876   */
877 }
878
879 void CApplication::StartTimeServer()
880 {
881         if (g_stSettings.m_bTimeServerEnabled && CUtil::IsNetworkUp())
882         {
883                 CLog::Log(LOGNOTICE, "start timeserver thread");
884                 m_sntpClient.Create();
885         }
886 }
887
888 void CApplication::StopTimeServer()
889 {
890   CLog::Log(LOGNOTICE, "stop time server");
891         m_sntpClient.StopThread();
892 }
893
894 void CApplication::StartServices()
895 {
896   StartTimeServer();
897   StartWebServer();
898   StartFtpServer();
899
900   //    Start Thread for DVD Mediatype detection
901         CLog::Log(LOGNOTICE, "start dvd mediatype detection");
902         m_DetectDVDType.Create( false);
903
904         CLog::Log(LOGNOTICE, "initializing playlistplayer");
905         g_playlistPlayer.Repeat(PLAYLIST_MUSIC, g_stSettings.m_bMyMusicPlaylistRepeat);
906         g_playlistPlayer.Repeat(PLAYLIST_MUSIC_TEMP, g_stSettings.m_bMyMusicRepeat);
907         g_playlistPlayer.Repeat(PLAYLIST_VIDEO, g_stSettings.m_bMyVideoPlaylistRepeat);
908         g_playlistPlayer.Repeat(PLAYLIST_VIDEO_TEMP, false);
909
910         CLCDFactory factory;
911         g_lcd=factory.Create();
912         g_lcd->Initialize();
913
914         CLog::Log(LOGNOTICE, "start fancontroller");
915   if (g_stSettings.m_bAutoTemperature) {
916     CFanController::Instance()->Start(g_stSettings.m_iTargetTemperature);
917   }
918   else if (g_stSettings.m_bFanSpeedControl) {
919     CFanController::Instance()->SetFanSpeed(g_stSettings.m_iFanSpeed);
920   }
921 }
922
923 void CApplication::StopServices()
924 {
925   StopWebServer();
926   StopTimeServer();
927   StopFtpServer();
928
929         CLog::Log(LOGNOTICE, "stop dvd detect media");
930         m_DetectDVDType.StopThread();
931
932         CLog::Log(LOGNOTICE, "stop LCD");
933         g_lcd->Stop();
934   g_lcd->WaitForThreadExit(INFINITE);
935   delete g_lcd;
936
937         CLog::Log(LOGNOTICE, "stop fancontroller");
938   CFanController::Instance()->Stop();
939 }
940
941 void CApplication::DelayLoadSkin()
942 {
943         m_dwSkinTime = timeGetTime() + 2000;
944         return;
945 }
946
947 void CApplication::CancelDelayLoadSkin()
948 {
949         m_dwSkinTime=0;
950 }
951
952 void CApplication::LoadSkin(const CStdString& strSkin)
953 {
954         m_dwSkinTime = 0;
955
956         CStdString strHomePath;
957         CStdString strSkinPath = "Q:\\skin\\";
958         strSkinPath+=strSkin;
959
960         CLog::Log(LOGINFO, "  load skin from:%s",strSkinPath.c_str());
961
962         if ( IsPlaying() )
963         {
964                 CLog::Log(LOGINFO, " stop playing...");
965                 m_pPlayer->closefile();
966                 m_itemCurrentFile.Clear();
967                 delete m_pPlayer;
968                 m_pPlayer=NULL;
969         }
970
971         CLog::Log(LOGINFO, "  delete old skin...");
972         m_guiWindowVideoOverlay.FreeResources();
973         m_guiWindowVideoOverlay.ClearAll();
974
975         m_guiMusicOverlay.FreeResources();
976         m_guiMusicOverlay.ClearAll();
977
978   m_guiDialogVolumeBar.FreeResources();
979   m_guiDialogVolumeBar.ClearAll();
980
981         m_guiPointer.FreeResources();
982         m_guiPointer.ClearAll();
983
984         m_gWindowManager.DeInitialize();
985         g_TextureManager.Cleanup();
986
987         g_fontManager.Clear();
988
989         g_charsetConverter.reset();
990
991         // Load in the skin.xml file if it exists
992         g_SkinInfo.Load(strSkinPath);
993
994         CLog::Log(LOGINFO, "  load fonts for skin...");
995         g_graphicsContext.SetMediaDir(strSkinPath);
996         g_fontManager.LoadFonts(CStdString(g_stSettings.m_szSkinFontSet));
997
998         LARGE_INTEGER start;
999         QueryPerformanceCounter(&start);
1000
1001         CLog::Log(LOGINFO, "  load new skin...");
1002         if (!m_guiHome.Load( "home.xml"))
1003         {
1004                 // failed to load home.xml
1005                 // fallback to default skin
1006                 if ( CUtil::cmpnocase(strSkin.c_str(),"Project Mayhem") !=0)
1007                 {
1008                         CLog::Log(LOGERROR, "failed to load home.xml for skin:%s, fallback to Project Mayhem skin", strSkin.c_str());
1009                         LoadSkin("Project Mayhem");
1010                         return;
1011                 }
1012         }
1013
1014         m_guiPrograms.Load( "myprograms.xml");
1015         m_guiPictures.Load( "mypics.xml");
1016         m_guiFileManager.Load( "filemanager.xml");
1017         m_guiMyVideo.Load("myvideo.xml");
1018         m_guiVideoGenre.Load("myvideogenre.xml");
1019         m_guiVideoActors.Load("myvideoactors.xml");
1020         m_guiVideoYear.Load("myvideoYear.xml");
1021         m_guiVideoTitle.Load("myvideoTitle.xml");
1022         m_guiSettings.Load("settings.xml");
1023         m_guiMyVideoPlayList.Load("myvideoplaylist.xml");
1024         m_guiSettingsLCD.Load("settingsLCD.xml");
1025         m_guiSystemInfo.Load("SettingsSystemInfo.xml");
1026         m_guiMusicInfo.Load("DialogAlbumInfo.xml");
1027         m_guiScriptsInfo.Load("DialogScriptInfo.xml");
1028         m_guiSettingsGeneral.Load("SettingsGeneral.xml");
1029         m_guiSettingsPrograms.Load("SettingsPrograms.xml");
1030         m_guiSettingsSytem.Load("SettingsSystem.xml");
1031   m_guiSettingsProfile.Load("SettingsProfile.xml");
1032         m_guiDialogYesNo.Load("dialogYesNo.xml");
1033         m_guiDialogProgress.Load("dialogProgress.xml");
1034         m_guiDialogVolumeBar.Load("dialogVolumeBar.xml");
1035         m_guiDialogSubMenu.Load("dialogSubMenu.xml");
1036         m_guiDialogContextMenu.Load("dialogContextMenu.xml");
1037         m_guiMyMusicPlayList.Load("mymusicplaylist.xml");
1038         m_guiMyMusicSongs.Load("mymusicsongs.xml");
1039         m_guiMyMusicAlbum.Load("mymusicalbum.xml");
1040         m_guiMyMusicArtists.Load("mymusicartists.xml");
1041         m_guiMyMusicGenres.Load("mymusicgenres.xml");
1042         m_guiMyMusicTop100.Load("mymusictop100.xml");
1043         m_guiDialogSelect.Load("dialogSelect.xml");
1044         m_guiDialogOK.Load("dialogOK.xml");
1045         m_guiDialogFileStacking.Load("dialogFileStacking.xml");
1046         m_guiVideoInfo.Load("DialogVideoInfo.xml");
1047         m_guiMusicOverlay.Load("musicOverlay.xml");
1048         //m_guiSettingsScreen.Load("settingsScreen.xml");
1049         m_guiSettingsMyVideo.Load("SettingsMyVideo.xml");
1050         m_guiSettingsUICalibration.Load("settingsUICalibration.xml");
1051         m_guiSettingsScreenCalibration.Load("settingsScreenCalibration.xml");
1052         m_guiSettingsSlideShow.Load("SettingsSlideShow.xml");
1053         m_guiSettingsScreensaver.Load("SettingsScreensaver.xml");
1054         m_guiSettingsAutoRun.Load("SettingsAutoRun.xml");
1055         m_guiSettingsFilter.Load("SettingsFilter.xml");
1056         m_guiSettingsCache.Load("SettingsCache.xml");
1057         m_guiWindowVideoOverlay.Load("videoOverlay.xml");
1058         m_guiWindowFullScreen.Load("videoFullScreen.xml");
1059         m_guiScripts.Load("myscripts.xml");
1060         m_guiWindowVisualisation.Load("musicVisualisation.xml");
1061         //m_guiSettingsMusic.Load("SettingsMusic.xml");
1062         m_guiSettingsMyMusic.Load("SettingsMyMusic.xml");
1063         m_guiWindowSlideshow.Load("slideshow.xml");
1064         m_guiSettingsSubtitles.Load("SettingsScreenSubtitles.xml");
1065         m_guiWindowScreensaver.SetID(WINDOW_SCREENSAVER);                               // Matrix Screensaver - saves us having to have our own XML file
1066         m_guiWindowOSD.Load("videoOSD.xml");
1067         m_guiMyWeather.Load("myweather.xml");                                                   //WEATHER
1068         m_guiSettingsWeather.Load("SettingsWeather.xml");                               //WEATHER SETTINGS
1069         m_guiSettingsNetwork.Load("SettingsNetwork.xml");                               //NETWORK SETTINGS
1070         m_guiSettingsNetworkProxy.Load("SettingsNetworkProxy.xml");
1071         m_guiSettingsNetworkKai.Load("SettingsNetworkKai.xml");
1072         m_guiSettingsNetworkIP.Load("SettingsNetworkIP.xml");
1073         m_guiSettingsNetworkFTP.Load("SettingsNetworkFTP.xml");
1074         m_guiSettingsNetworkWeb.Load("SettingsNetworkWeb.xml");
1075         m_guiSettingsNetworkTime.Load("SettingsNetworkTime.xml");
1076         m_guiSettingsSkinLanguage.Load("SettingsSkinLanguage.xml");             // Skin & Language settings
1077         m_guiSettingsUserInterface.Load("SettingsUserInterface.xml");   // User Interface settings
1078         m_guiSettingsAudio.Load("SettingsAudioOptions.xml");                    // Audio Options
1079         m_guiDialogInvite.Load( "dialogInvite.xml" );
1080         m_guiDialogKeyboard.Load( "dialogKeyboard.xml" );
1081         m_guiMyBuddies.Load( "mybuddies.xml");
1082         m_guiSettingsCDRipper.Load("SettingsCDRipper.xml");
1083         m_guiPointer.Load("Pointer.xml");
1084         m_guiSettingsCdg.Load("SettingsCdg.xml");
1085         
1086         // Load the user windows
1087         LoadUserWindows(strSkinPath);
1088
1089         CGUIWindow::FlushReferenceCache(); // flush the cache so it doesn't use memory all the time
1090
1091         LARGE_INTEGER end, freq;
1092         QueryPerformanceCounter(&end);
1093         QueryPerformanceFrequency(&freq);
1094         CLog::DebugLog("Load Skin XML: %.2fms", 1000.f * (end.QuadPart - start.QuadPart) / freq.QuadPart);
1095
1096         CLog::Log(LOGINFO, "  initialize new skin...");
1097         m_guiPointer.AllocResources();
1098         m_guiMusicOverlay.AllocResources();
1099         m_guiWindowVideoOverlay.AllocResources();
1100   m_guiDialogVolumeBar.AllocResources();
1101         m_gWindowManager.AddMsgTarget(this);
1102         m_gWindowManager.AddMsgTarget(&g_playlistPlayer);
1103         m_gWindowManager.SetCallback(*this);
1104         m_gWindowManager.Initialize();
1105         CLog::Log(LOGINFO, "  skin loaded...");
1106 }
1107
1108 bool CApplication::LoadUserWindows(const CStdString& strSkinPath)
1109 {
1110   WIN32_FIND_DATA FindFileData;
1111   WIN32_FIND_DATA NextFindFileData;
1112   HANDLE hFind;
1113   TiXmlDocument xmlDoc;
1114   RESOLUTION resToUse = INVALID;
1115
1116   // Load from wherever home.xml is
1117   g_SkinInfo.GetSkinPath("home.xml", &resToUse);
1118
1119   CStdString strLoadPath;
1120   strLoadPath.Format("%s%s", strSkinPath, g_SkinInfo.GetDirFromRes(resToUse));
1121
1122   CStdString strPath;
1123   strPath.Format("%s\\%s", strLoadPath, "custom*.xml");
1124   CLog::Log(LOGINFO, "Loading user windows %s", strPath.c_str());
1125   hFind = FindFirstFile(strPath.c_str(), &NextFindFileData);
1126
1127   CStdString strFileName;
1128   while (hFind != INVALID_HANDLE_VALUE)
1129   {
1130     FindFileData=NextFindFileData;
1131
1132     if (!FindNextFile(hFind, &NextFindFileData))
1133     {
1134       FindClose(hFind);
1135       hFind = INVALID_HANDLE_VALUE;
1136     }
1137
1138     // skip "up" directories, which come in all queries
1139     if (!_tcscmp(FindFileData.cFileName, _T(".")) || !_tcscmp(FindFileData.cFileName, _T("..")))
1140       continue;
1141
1142     strFileName.Format("%s\\%s", strLoadPath.c_str(), FindFileData.cFileName);
1143     CLog::Log(LOGINFO, "Loading skin file: %s", strFileName.c_str());
1144     if (!xmlDoc.LoadFile(strFileName.c_str()))
1145     {
1146                         CLog::Log(LOGERROR, "unable to load:%s", strFileName.c_str());
1147                         continue;
1148     }
1149
1150     // Root element should be <window>
1151     TiXmlElement* pRootElement = xmlDoc.RootElement();
1152     CStdString strValue=pRootElement->Value();
1153     if (!strValue.Equals("window"))
1154     {
1155       CLog::Log(LOGERROR, "file :%s doesnt contain <window>", strFileName.c_str());
1156       continue;
1157     }
1158
1159     // Read the <type> element to get the window type to create
1160     // If no type is specified, create a CGUIWindow as default
1161     CGUIWindow* pWindow = NULL;
1162     const TiXmlNode *pType = pRootElement->FirstChild("type");
1163     if (pType == NULL)
1164     {
1165       pWindow = new CGUIStandardWindow();
1166     }
1167     else
1168     {
1169       CStdString strType = pType->FirstChild()->Value();
1170       if (strType == "dialog")
1171       {
1172         pWindow = new CGUIDialog(0);
1173       }
1174       else if (strType == "subMenu")
1175       {
1176         pWindow = new CGUIDialogSubMenu();
1177       }
1178       else
1179       {
1180         pWindow = new CGUIStandardWindow();
1181       }
1182     }
1183
1184     // Check to make sure the pointer isn't still null
1185     if (pWindow == NULL)
1186     {
1187       CLog::Log(LOGERROR, "Out of memory / Failed to create new object in LoadUserWindows");
1188       return false;
1189     }
1190
1191     // Try to load the page.  If the load fails, delete the pointer
1192     if (pWindow->Load(pRootElement, resToUse))
1193     {
1194       m_gWindowManager.Add(pWindow);
1195     }
1196                 else
1197                 {
1198                         delete pWindow;
1199                 }
1200   }
1201
1202   return true;
1203 }
1204
1205 void CApplication::Render()
1206 {
1207         // update sound
1208   if (m_pPlayer) {
1209     m_pPlayer->DoAudioWork();
1210   }
1211
1212         // check that we haven't passed the end of the file (for queue sheets)
1213   if ((m_pPlayer != NULL) && m_pPlayer->IsPlaying())
1214   {
1215           __int64 iPTS = m_pPlayer->GetPTS();
1216           int timeinsecs = (int)(iPTS / 10);
1217           if (m_itemCurrentFile.m_lEndOffset && m_itemCurrentFile.m_lEndOffset/75 < timeinsecs)
1218           {     // time to stop the file...
1219                   OnPlayBackEnded();
1220           }
1221   }
1222         // dont show GUI when playing full screen video
1223         if (m_gWindowManager.GetActiveWindow() == WINDOW_FULLSCREEN_VIDEO)
1224         {
1225                 if ( g_graphicsContext.IsFullScreenVideo() )
1226                 {
1227                         g_graphicsContext.Lock();
1228                         g_graphicsContext.Unlock();
1229
1230                         if (m_pPlayer)
1231                         {
1232                                 if (m_pPlayer->IsPaused())
1233                                 {
1234                                         g_graphicsContext.Lock();
1235                                         m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0x00010001, 1.0f, 0L );
1236                                         extern void xbox_video_render_update();
1237                                         xbox_video_render_update();
1238                                         RenderFullScreen();
1239                                         m_gWindowManager.Render();
1240                                         m_pd3dDevice->BlockUntilVerticalBlank();
1241                                         m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
1242                                         g_graphicsContext.Unlock();
1243                                         return;
1244                                 }
1245                         }
1246                         Sleep(50);
1247                         ResetScreenSaver();
1248                         return;
1249                 }
1250         }
1251
1252         // enable/disable video overlay window
1253         if (IsPlayingVideo() && m_gWindowManager.GetActiveWindow() != WINDOW_FULLSCREEN_VIDEO && !m_bScreenSave)
1254         {
1255                 g_graphicsContext.EnablePreviewWindow(true);
1256         }
1257         else
1258         {
1259                 g_graphicsContext.EnablePreviewWindow(false);
1260         }
1261
1262         g_graphicsContext.Lock();
1263
1264         // draw GUI
1265         m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0x00010001, 1.0f, 0L );
1266
1267         // render current window/dialog
1268         m_gWindowManager.Render();
1269
1270         // check if we're playing a file
1271         if (g_graphicsContext.IsOverlayAllowed())
1272         {
1273                 // if we're playing a movie
1274                 if ( IsPlayingVideo() && m_gWindowManager.GetActiveWindow() != WINDOW_FULLSCREEN_VIDEO)
1275                 {
1276                         // then show video overlay window
1277                         m_guiWindowVideoOverlay.Render();
1278                 }
1279                 else if ( IsPlayingAudio() )
1280                 {
1281                         // audio show audio overlay window
1282                         m_guiMusicOverlay.Render();
1283                 }
1284         }
1285                 // Render the mouse pointer
1286                 if (g_Mouse.IsActive())
1287                 {
1288                         m_guiPointer.Render();
1289                 }
1290
1291         {
1292                 // free memory if we got les then 10megs free ram
1293                 MEMORYSTATUS stat;
1294                 GlobalMemoryStatus(&stat);
1295                 DWORD dwMegFree=stat.dwAvailPhys / (1024*1024);
1296                 if (dwMegFree <= 10)
1297                 {
1298                         g_TextureManager.Flush();
1299                 }
1300
1301                 // if we're recording an audio stream then show blinking REC
1302                 if (IsPlayingAudio())
1303                 {
1304                         if (m_pPlayer->IsRecording() )
1305                         {
1306                                 static iBlinkRecord=0;
1307                                 CGUIFont* pFont=g_fontManager.GetFont("font13");
1308                                 if (pFont)
1309                                 {
1310                                         iBlinkRecord++;
1311                                         if (iBlinkRecord>25)
1312                                                 pFont->DrawText(60, 50, 0xffff0000, L"REC"); //Draw REC in RED
1313                                         if (iBlinkRecord> 50)
1314                                                 iBlinkRecord=0;
1315                                 }
1316                         }
1317                 }
1318
1319                 // If we have the remote codes enabled, then show them
1320                 if (g_stSettings.m_bDisplayRemoteCodes)
1321                 {
1322                         XBIR_REMOTE* pRemote    = &m_DefaultIR_Remote;
1323                         static iRemoteCode=0;
1324                         static iShowRemoteCode=0;
1325                         if (pRemote->wButtons)
1326                         {
1327                                 iRemoteCode = 255-pRemote->wButtons;    // remote OBC code is 255-wButtons
1328                                 iShowRemoteCode = 50;
1329                         }
1330                         if (iShowRemoteCode > 0)
1331                         {
1332                                 CStdStringW wszText;
1333                                 wszText.Format(L"Remote Code: %i",iRemoteCode);
1334                                 CGUIFont* pFont=g_fontManager.GetFont("font13");
1335                                 if (pFont)
1336                                 {
1337 #ifdef _DEBUG
1338                                         pFont->DrawText( 60, 60, 0xffffffff, wszText);
1339 #else
1340                                         if (g_stSettings.m_bShowFreeMem)
1341                                                 pFont->DrawText( 60, 60, 0xffffffff, wszText);
1342                                         else
1343                                                 pFont->DrawText( 60, 40, 0xffffffff, wszText);
1344 #endif
1345                                 }
1346                                 iShowRemoteCode--;
1347                         }
1348                 }
1349
1350 #ifndef _DEBUG
1351                 if (g_stSettings.m_bShowFreeMem)
1352 #endif
1353                 {
1354                         // in debug mode, show freememory
1355                         CStdStringW wszText;
1356                         wszText.Format(L"FreeMem %d/%d",stat.dwAvailPhys,
1357                                 stat.dwTotalPhys);
1358
1359                         CGUIFont* pFont=g_fontManager.GetFont("font13");
1360                         if (pFont)
1361                         {
1362                                 pFont->DrawText( 60, 40, 0xffffffff, wszText);
1363                         }
1364                 }
1365         }
1366         // Present the backbuffer contents to the display
1367         m_pd3dDevice->BlockUntilVerticalBlank();
1368         m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
1369         g_graphicsContext.Unlock();
1370
1371 }
1372
1373 void CApplication::OnKey(CKey& key)
1374 {
1375         // Turn the mouse off, as we've just got a keypress from controller or remote
1376         g_Mouse.SetInactive();
1377         CAction action;
1378         // a key has been pressed.
1379         // Reset the screensaver timer
1380         // but not for the analog thumbsticks
1381         if (key.GetButtonCode() != KEY_BUTTON_LEFT_THUMB_STICK &&
1382                 key.GetButtonCode() != KEY_BUTTON_RIGHT_THUMB_STICK)
1383         {
1384                 // reset harddisk spindown timer
1385                 m_bSpinDown        = false;
1386     m_bNetworkSpinDown = false;
1387
1388
1389
1390                 ResetScreenSaver();
1391                 if (ResetScreenSaverWindow()) return;
1392         }
1393
1394         // get the current active window
1395         int iWin = m_gWindowManager.GetActiveWindow();
1396         // change this if we have a dialog up
1397         if (m_gWindowManager.IsRouted())
1398         {
1399                 iWin = m_gWindowManager.m_pRouteWindow->GetID();
1400         }
1401         if (iWin==WINDOW_FULLSCREEN_VIDEO)
1402         {
1403                 // current active window is full screen video.
1404                 // check if OSD is visible
1405                 CGUIWindowFullScreen *pFSWin = (CGUIWindowFullScreen *)m_gWindowManager.GetWindow(WINDOW_FULLSCREEN_VIDEO);
1406                 if ( pFSWin->OSDVisible() )
1407                 {
1408                         // yes then use OSD section of keymap.xml to map key->action
1409                         g_buttonTranslator.GetAction(WINDOW_OSD, key, action);
1410                 }
1411                 else
1412                 {
1413                         // no then use the fullscreen window section of keymap.xml to map key->action
1414                         g_buttonTranslator.GetAction(iWin, key, action);
1415                 }
1416         }
1417         else
1418         {
1419                 // current active window isnt the fullscreen window
1420                 // just use corresponding section from keymap.xml
1421                 // to map key->action
1422                 if (key.FromKeyboard() && iWin == WINDOW_DIALOG_KEYBOARD)
1423                 {
1424                         // see if we've got an ascii key
1425                         if (g_Keyboard.GetAscii() != 0)
1426                                 action.wID = (WORD)g_Keyboard.GetAscii() | KEY_ASCII;
1427                         else
1428                                 action.wID = (WORD)g_Keyboard.GetKey() | KEY_VKEY;
1429                 }
1430                 else
1431                         g_buttonTranslator.GetAction(iWin, key, action);
1432         }
1433
1434         // special case for switching between GUI & fullscreen mode.
1435         if (action.wID == ACTION_SHOW_GUI)
1436         {       // Switch to fullscreen mode if we can
1437                 if (SwitchToFullScreen()) return;
1438         }
1439
1440         // in normal case
1441         // just pass the action to the current window and let it handle it
1442         m_gWindowManager.OnAction(action);
1443
1444         /* handle extra global presses */
1445         if (iWin != WINDOW_FULLSCREEN_VIDEO)
1446         {
1447                 // stop : stops playing current audio song
1448                 if (action.wID == ACTION_STOP)
1449                 {
1450                         StopPlaying();
1451                 }
1452
1453                 // pause : pauses current audio song
1454                 if (action.wID == ACTION_PAUSE)
1455                 {
1456                         if (m_pPlayer)
1457                         {
1458                                 m_pPlayer->Pause();
1459                                 if (!m_pPlayer->IsPaused())
1460                                 {
1461                                         SetPlaySpeed(1);
1462                                 }
1463                         }
1464                 }
1465
1466
1467                 // previous : play previous song from playlist
1468                 if (action.wID == ACTION_PREV_ITEM)
1469                 {
1470                         g_playlistPlayer.PlayPrevious();
1471                 }
1472
1473                 // next : play next song from playlist
1474                 if (action.wID == ACTION_NEXT_ITEM)
1475                 {
1476                         g_playlistPlayer.PlayNext();
1477                 }
1478
1479                 if ( IsPlaying())
1480                 {
1481                         if (!m_pPlayer->IsPaused())
1482                         {
1483                                 // if we do a FF/RW in my music then map PLAY action togo back to normal speed
1484                                 if (action.wID == ACTION_MUSIC_PLAY)
1485                                 {
1486
1487                                         if (m_iPlaySpeed!=1)
1488                                         {
1489                                                 SetPlaySpeed(1);
1490                                         }
1491                                 }
1492                                 if (action.wID == ACTION_MUSIC_FORWARD || action.wID == ACTION_MUSIC_REWIND)
1493                                 {
1494                                         if (m_strCurrentPlayer == "sid")
1495                                         {
1496                                                 // sid uses these to track skip
1497                                                 m_pPlayer->Seek(action.wID == ACTION_MUSIC_FORWARD);
1498                                         }
1499                                         else
1500                                         {
1501                                                 int iPlaySpeed=m_iPlaySpeed;
1502                                                 if (action.wID == ACTION_MUSIC_REWIND && iPlaySpeed == 1) // Enables Rewinding
1503                                                         iPlaySpeed *=-2;
1504                                                 else if (action.wID == ACTION_MUSIC_REWIND && iPlaySpeed > 1) //goes down a notch if you're FFing
1505                                                         iPlaySpeed /=2;
1506                                                 else if (action.wID == ACTION_MUSIC_FORWARD && iPlaySpeed < 1) //goes up a notch if you're RWing
1507                                                         iPlaySpeed /= 2;
1508                                                 else
1509                                                         iPlaySpeed *= 2;
1510
1511                                                 if (action.wID == ACTION_MUSIC_FORWARD && iPlaySpeed == -1) //sets iSpeed back to 1 if -1 (didn't plan for a -1)
1512                                                         iPlaySpeed = 1;
1513                                                 if (iPlaySpeed > 32 || iPlaySpeed < -32)
1514                                                         iPlaySpeed = 1;
1515
1516                                                 SetPlaySpeed(iPlaySpeed);
1517                                         }
1518                                 }
1519                         }
1520                 }
1521         }
1522         // Check for global volume control
1523         if (action.wID == ACTION_VOLUME_UP || action.wID == ACTION_VOLUME_DOWN)
1524         {       // increase or decrease the volume
1525                 if (action.wID == ACTION_VOLUME_UP)
1526                         g_stSettings.m_nVolumeLevel += (int)(action.fAmount1*100);
1527                 else
1528                         g_stSettings.m_nVolumeLevel -= (int)(action.fAmount1*100);
1529                 if (g_stSettings.m_nVolumeLevel > VOLUME_MAXIMUM) g_stSettings.m_nVolumeLevel = VOLUME_MAXIMUM;
1530                 if (g_stSettings.m_nVolumeLevel < VOLUME_MINIMUM) g_stSettings.m_nVolumeLevel = VOLUME_MINIMUM;
1531                 // tell our hardware to update the volume level...
1532                 if (m_pPlayer)
1533                         m_pPlayer->SetVolume(g_stSettings.m_nVolumeLevel);
1534                 // show visual feedback of volume change...
1535                 if (!m_guiDialogVolumeBar.IsRunning())
1536                         m_guiDialogVolumeBar.DoModal(m_gWindowManager.GetActiveWindow());
1537         }
1538 }
1539 void CApplication::UpdateLCD()
1540 {
1541         static lTickCount=0;
1542
1543         if (!g_stSettings.m_bLCDUsed) return;
1544         long lTimeOut=1000;
1545         if ( m_iPlaySpeed != 1) lTimeOut=0;
1546         if ( ((long)GetTickCount()-lTickCount) >=lTimeOut)
1547         {
1548                 CStdString strTime;
1549                 CStdString strIcon;
1550                 CStdString strLine;
1551                 if (IsPlayingVideo())
1552                 {
1553                         // line 0: play symbol current time/total time
1554                         // line 1: movie filename / title
1555                         // line 2: genre
1556                         // line 3: year
1557                         CStdString strTotalTime;
1558                         unsigned int tmpvar = g_application.m_pPlayer->GetTotalTime();
1559                         if(tmpvar != 0)
1560                         {
1561                                 int ihour = tmpvar / 3600;
1562                                 int imin  = (tmpvar-ihour*3600) / 60;
1563                                 int isec = (tmpvar-ihour*3600) % 60;
1564                                 strTotalTime.Format("/%2.2d:%2.2d:%2.2d", ihour,imin,isec);
1565                         }
1566                         else
1567                         {
1568                                 strTotalTime=" ";
1569                         }
1570
1571                         __int64 lPTS=10*g_application.m_pPlayer->GetTime();
1572                         int hh = (int)(lPTS / 36000) % 100;
1573                         int mm = (int)((lPTS / 600) % 60);
1574                         int ss = (int)((lPTS /  10) % 60);
1575                         if (hh >=1)
1576                         {
1577                                 strTime.Format("%02.2i:%02.2i:%02.2i",hh,mm,ss);
1578                         }
1579                         else
1580                         {
1581                                 strTime.Format("%02.2i:%02.2i",mm,ss);
1582                         }
1583                         if (m_iPlaySpeed < 1)
1584                                 strIcon.Format("\3 %ix", m_iPlaySpeed);
1585                         else if (m_iPlaySpeed > 1)
1586                                 strIcon.Format("\4 %ix", m_iPlaySpeed);
1587                         else if (m_pPlayer->IsPaused())
1588                                 strIcon.Format("\7");
1589                         else
1590                                 strIcon.Format("\5");
1591                         strLine.Format("%s %s%s", strIcon.c_str(), strTime.c_str(), strTotalTime.c_str());
1592                         g_lcd->SetLine(0,strLine);
1593
1594                         strLine=CUtil::GetTitleFromPath(m_itemCurrentFile.m_strPath);
1595                         int iLine=1;
1596                         if (m_tagCurrentMovie.m_strTitle!="") strLine=m_tagCurrentMovie.m_strTitle;
1597                         g_lcd->SetLine(iLine++,strLine);
1598
1599                         if (iLine<4 && m_tagCurrentMovie.m_strGenre!="") g_lcd->SetLine(iLine++,m_tagCurrentMovie.m_strGenre);
1600                         if (iLine<4 && m_tagCurrentMovie.m_iYear>1900)
1601                         {
1602                                 strLine.Format("%i", m_tagCurrentMovie.m_iYear);
1603                                 g_lcd->SetLine(iLine++,strLine);
1604                         }
1605                         if (iLine < 4)
1606                         {
1607                                 MEMORYSTATUS stat;
1608                                 GlobalMemoryStatus(&stat);
1609                                 DWORD dwMegFree=stat.dwAvailPhys / (1024*1024);
1610                                 strTime.Format("Freemem:%i meg", dwMegFree);
1611                                 g_lcd->SetLine(iLine++,strTime);
1612
1613                         }
1614                         while (iLine < 4) g_lcd->SetLine(iLine++,"");
1615
1616                 }
1617                 else if (IsPlayingAudio())
1618                 {
1619                         // Show:
1620                         // line 0: play symbol current time/total time
1621                         // line 1: song title
1622                         // line 2: artist
1623                         // line 3: release date
1624                         __int64 lPTS=g_application.m_pPlayer->GetPTS() - (m_itemCurrentFile.m_lStartOffset*10)/75;
1625                         int hh = (int)(lPTS / 36000) % 100;
1626                         int mm = (int)((lPTS / 600) % 60);
1627                         int ss = (int)((lPTS /  10) % 60);
1628                         if (hh >=1)
1629                         {
1630                                 strTime.Format("%02.2i:%02.2i:%02.2i",hh,mm,ss);
1631                         }
1632                         else
1633                         {
1634                                 strTime.Format("%02.2i:%02.2i",mm,ss);
1635                         }
1636                         if (m_iPlaySpeed < 1)
1637                                 strIcon.Format("\3:%ix", m_iPlaySpeed);
1638                         else if (m_iPlaySpeed > 1)
1639                                 strIcon.Format("\4:%ix", m_iPlaySpeed);
1640                         else if (m_pPlayer->IsPaused())
1641                                 strIcon.Format("\7");
1642                         else
1643                                 strIcon.Format("\5");
1644                         strLine.Format("%s %s", strIcon.c_str(), strTime.c_str());
1645
1646                         int iLine=1;
1647                         if (m_itemCurrentFile.m_musicInfoTag.Loaded())
1648                         {
1649                                 int iDuration=m_itemCurrentFile.m_musicInfoTag.GetDuration();
1650                                 if (iDuration>0)
1651                                 {
1652                                         CStdString strDuration;
1653                                         CUtil::SecondsToHMSString(iDuration, strDuration);
1654                                         strLine.Format("%s %s/%s", strIcon.c_str(), strTime.c_str(),strDuration.c_str());
1655                                 }
1656                                 g_lcd->SetLine(0,strLine);
1657                                 strLine=m_itemCurrentFile.m_musicInfoTag.GetTitle();
1658                                 if (strLine=="") strLine=CUtil::GetTitleFromPath(m_itemCurrentFile.m_strPath);
1659                                 if (iLine < 4 && strLine!="") g_lcd->SetLine(iLine++,strLine);
1660                                 strLine=m_itemCurrentFile.m_musicInfoTag.GetArtist();
1661                                 if (iLine < 4 && strLine!="") g_lcd->SetLine(iLine++,strLine);
1662         strLine=m_itemCurrentFile.m_musicInfoTag.GetAlbum();
1663                                 SYSTEMTIME systemtime;
1664         m_itemCurrentFile.m_musicInfoTag.GetReleaseDate(systemtime);
1665         if (iLine < 4 && strLine!="")
1666                                 {
1667                                         if (systemtime.wYear>=1900)
1668                                         {
1669                                                 CStdString strYearLine;
1670                                                 strYearLine.Format("%s (%i)", strLine.c_str(), systemtime.wYear);
1671                                                 g_lcd->SetLine(iLine++,strYearLine);
1672                                         }
1673                                         else
1674                                         {
1675                                                 g_lcd->SetLine(iLine++,strLine);
1676                                         }
1677                                 }
1678                                 while (iLine < 4) g_lcd->SetLine(iLine++,"");
1679                         }
1680                         else if (CUtil::IsCDDA(m_itemCurrentFile.m_strPath))
1681                         {
1682                                 //      we have the tracknumber...
1683                                 int iTrack=m_itemCurrentFile.m_musicInfoTag.GetTrackNumber();
1684                                 //  ...and it's duration for display
1685                                 int iDuration=m_itemCurrentFile.m_musicInfoTag.GetDuration();
1686                                 // format the duration string
1687                                 if (iDuration>0)
1688                                 {
1689                                         CStdString strDuration;
1690                                         CUtil::SecondsToHMSString(iDuration, strDuration);
1691                                         strLine.Format("%s %s/%s", strIcon.c_str(), strTime.c_str(),strDuration.c_str());
1692                                 }
1693                                 g_lcd->SetLine(0,strLine);
1694                                 if (iTrack >=1)
1695                                 {
1696                                         CStdString strText=g_localizeStrings.Get(435);  //      "Track"
1697                                         if (strText.GetAt(strText.size()-1) != ' ')
1698                                                 strText+=" ";
1699                                         CStdString strTrack;
1700                                         strTrack.Format(strText+"%i", iTrack);
1701                                         g_lcd->SetLine(1,strTrack);
1702                                 }
1703                                 else
1704                                 {
1705                                         g_lcd->SetLine(1,"");
1706                                 }
1707                                 // fill in the others as blanks
1708                                 g_lcd->SetLine(2,"");
1709                                 g_lcd->SetLine(3,"");
1710                         }
1711                         else
1712                         {
1713                                 g_lcd->SetLine(0,strLine);
1714                                 g_lcd->SetLine(1,CUtil::GetTitleFromPath(m_itemCurrentFile.m_strPath));
1715                                 g_lcd->SetLine(2,"");
1716                                 g_lcd->SetLine(3,"");
1717                         }
1718                 }
1719                 else
1720                 {
1721                         if (g_stSettings.m_iLCDMode==LCD_MODE_NORMAL)
1722                         {
1723                                 // line 0: XBMC running...
1724                                 // line 1: time/date
1725                                 // line 2: free memory (megs)
1726                                 // line 3: GUI resolution
1727                                 g_lcd->SetLine(0,"XBMC running...");
1728                                 SYSTEMTIME time;
1729                                 GetLocalTime(&time);
1730                                 strTime.Format("%02.2i:%02.2i:%02.2i %02.2i-%02.2i-%02.2i", time.wHour,time.wMinute,time.wSecond,time.wDay,time.wMonth,time.wYear);
1731                                 g_lcd->SetLine(1,strTime);
1732                                 MEMORYSTATUS stat;
1733                                 GlobalMemoryStatus(&stat);
1734                                 DWORD dwMegFree=stat.dwAvailPhys / (1024*1024);
1735                                 strTime.Format("Freemem:%i meg", dwMegFree);
1736                                 g_lcd->SetLine(2,strTime);
1737                                 int  iResolution=g_graphicsContext.GetVideoResolution();
1738                                 strTime.Format("%ix%i %s", g_settings.m_ResInfo[iResolution].iWidth, g_settings.m_ResInfo[iResolution].iHeight, g_settings.m_ResInfo[iResolution].strMode);
1739                                 g_lcd->SetLine(3,strTime);
1740                         }
1741                         if (g_stSettings.m_iLCDMode==LCD_MODE_NOTV)
1742                         {
1743                                 // line 0: window name like   My music/songs
1744                                 // line 1: current control or selected item
1745                                 // line 2: time/date
1746                                 // line 3: free memory (megs)
1747                                 CStdString strTmp;
1748                                 int iWin=m_gWindowManager.GetActiveWindow();
1749                                 CGUIWindow* pWindow=m_gWindowManager.GetWindow(iWin);
1750                                 if (pWindow)
1751                                 {
1752                                         CStdString strLine;
1753                                         wstring wstrLine;
1754                                         wstrLine=g_localizeStrings.Get(iWin);
1755                                         CUtil::Unicode2Ansi(wstrLine,strLine);
1756                                         g_lcd->SetLine(0,strLine);
1757
1758                                         int iControl=pWindow->GetFocusedControl();
1759                                         CGUIControl* pControl=(CGUIControl* )pWindow->GetControl(iControl);
1760                                         if (pControl)
1761                                         {
1762                                                 if (pControl->GetControlType() == CGUIControl::GUICONTROL_BUTTON)
1763                                                         g_lcd->SetLine(1,((CGUIButtonControl*)pControl)->GetLabel());
1764                                                 else if (pControl->GetControlType() == CGUIControl::GUICONTROL_SPIN)
1765                                                 {
1766                                                         CGUISpinControl* pSpinControl = (CGUISpinControl*)pControl;
1767                                                         strTmp.Format("%i/%i", 1+pSpinControl->GetValue(), pSpinControl->GetMaximum());
1768                                                         g_lcd->SetLine(1,strTmp);
1769                                                 }
1770                                                 else if (pControl->GetControlType() == CGUIControl::GUICONTROL_LABEL)
1771                                                 {
1772                                                         CGUIListControl* pListControl = (CGUIListControl*)pControl;
1773                                                         pListControl->GetSelectedItem(strTmp);
1774                                                         g_lcd->SetLine(1,strTmp);
1775                                                 }
1776                                                 else if (pControl->GetControlType() == CGUIControl::GUICONTROL_THUMBNAIL)
1777                                                 {
1778                                                         CGUIThumbnailPanel* pThumbControl = (CGUIThumbnailPanel*)pControl;
1779                                                         pThumbControl->GetSelectedItem(strTmp);
1780                                                         g_lcd->SetLine(1,strTmp);
1781                                                 }
1782                                                 else if (pControl->GetControlType() == CGUIControl::GUICONTROL_LIST)
1783                                                 {
1784                                                         CGUIListControl* pListControl = (CGUIListControl*)pControl;
1785                                                         pListControl->GetSelectedItem(strTmp);
1786                                                         g_lcd->SetLine(1,strTmp);
1787                                                 }
1788                                         }
1789                                         else g_lcd->SetLine(1," ");
1790                                         SYSTEMTIME time;
1791                                         GetLocalTime(&time);
1792                                         strLine.Format("%02.2i:%02.2i:%02.2i %02.2i-%02.2i-%02.2i", time.wHour,time.wMinute,time.wSecond,time.wDay,time.wMonth,time.wYear);
1793                                         g_lcd->SetLine(2,strLine);
1794                                         MEMORYSTATUS stat;
1795                                         GlobalMemoryStatus(&stat);
1796                                         DWORD dwMegFree=stat.dwAvailPhys / (1024*1024);
1797                                         strLine.Format("Freemem:%i meg", dwMegFree);
1798                                         g_lcd->SetLine(3,strLine);
1799
1800                                 }
1801                         }
1802                 }
1803                 lTickCount=GetTickCount();
1804         }
1805 }
1806
1807 void CApplication::FrameMove()
1808 {
1809         // reset the fullscreen analog options if needed
1810         m_guiWindowFullScreen.m_bSmoothFFwdRewd = false;
1811
1812         if (g_lcd) UpdateLCD();
1813         // read raw input from controller, remote control, mouse and keyboard
1814         ReadInput();
1815         // process mouse actions
1816         if (g_Mouse.IsActive())
1817         {
1818                 // Reset the screensaver
1819                 ResetScreenSaver();
1820                 if (ResetScreenSaverWindow()) return;
1821
1822                 // call OnAction with ACTION_MOUSE
1823                 CAction action;
1824                 action.wID = ACTION_MOUSE;
1825                 action.fAmount1 = (float) m_guiPointer.GetPosX();
1826                 action.fAmount2 = (float) m_guiPointer.GetPosY();
1827                 // send mouse event to the music + video overlays, if they're enabled
1828                 if (g_graphicsContext.IsOverlayAllowed())
1829                 {
1830                         // if we're playing a movie
1831                         if ( IsPlayingVideo() && m_gWindowManager.GetActiveWindow() != WINDOW_FULLSCREEN_VIDEO)
1832                         {
1833                                 // then send the action to the video overlay window
1834                                 m_guiWindowVideoOverlay.OnAction(action);
1835                         }
1836                         else if ( IsPlayingAudio() )
1837                         {
1838                                 // send message to the audio overlay window
1839                                 m_guiMusicOverlay.OnAction(action);
1840                         }
1841                 }
1842                 m_gWindowManager.OnAction(action);
1843         }
1844         // process the keyboard buttons etc.
1845         BYTE vkey = g_Keyboard.GetKey();
1846         if (vkey)
1847         {
1848                 // got a valid keypress - convert to a key code
1849                 WORD wkeyID = (WORD)vkey | KEY_VKEY;
1850 //              CLog::DebugLog("Keyboard: time=%i key=%i", timeGetTime(), vkey);
1851                 CKey key(wkeyID);
1852                 OnKey(key);
1853                 return;
1854         }
1855
1856         XBIR_REMOTE* pRemote    = &m_DefaultIR_Remote;
1857         XBGAMEPAD*  pGamepad    = &m_DefaultGamepad;
1858
1859         WORD wButtons = pGamepad->wButtons;
1860         WORD wRemotes = pRemote->wButtons;
1861         WORD wDpad = wButtons&(XINPUT_GAMEPAD_DPAD_UP|XINPUT_GAMEPAD_DPAD_DOWN|XINPUT_GAMEPAD_DPAD_LEFT|XINPUT_GAMEPAD_DPAD_RIGHT);
1862
1863         BYTE bLeftTrigger = pGamepad->bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER];
1864         BYTE bRightTrigger = pGamepad->bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER];
1865
1866         // pass them through the delay
1867         WORD wDir = m_ctrDpad.DpadInput(wDpad,0!=bLeftTrigger,0!=bRightTrigger);
1868         wRemotes=m_ctrIR.IRInput(wRemotes);
1869
1870         bool bGotKey=false;
1871
1872         // map all controller & remote actions to their keys
1873         if (fabs(pGamepad->fX1)>g_stSettings.m_fAnalogDeadzoneController || fabs(pGamepad->fY1)>g_stSettings.m_fAnalogDeadzoneController)
1874         {
1875                 bGotKey=true;
1876                 CKey key(KEY_BUTTON_LEFT_THUMB_STICK,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1877                 OnKey(key);
1878         }
1879
1880         if ((pGamepad->fX2)>g_stSettings.m_fAnalogDeadzoneController || (pGamepad->fY2)>g_stSettings.m_fAnalogDeadzoneController)
1881         {
1882                 bGotKey=true;
1883                 CKey key(KEY_BUTTON_RIGHT_THUMB_STICK,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1884                 OnKey(key);
1885         }
1886         // direction specific keys (for defining different actions for each direction)
1887         if (pGamepad->fY2 > g_stSettings.m_fAnalogDeadzoneController && pGamepad->fX2 < pGamepad->fY2 && -pGamepad->fX2 < pGamepad->fY2)
1888         {
1889                 bGotKey=true;
1890                 CKey key(KEY_BUTTON_RIGHT_THUMB_STICK_UP,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1891                 OnKey(key);
1892         }
1893         if (pGamepad->fY2 < -g_stSettings.m_fAnalogDeadzoneController && pGamepad->fX2 < -pGamepad->fY2 && -pGamepad->fX2 < -pGamepad->fY2)
1894         {
1895                 bGotKey=true;
1896                 CKey key(KEY_BUTTON_RIGHT_THUMB_STICK_DOWN,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,-pGamepad->fY2);
1897                 OnKey(key);
1898         }
1899         if (pGamepad->fX2 > g_stSettings.m_fAnalogDeadzoneController && pGamepad->fY2 < pGamepad->fX2 && -pGamepad->fY2 < pGamepad->fX2)
1900         {
1901                 bGotKey=true;
1902                 CKey key(KEY_BUTTON_RIGHT_THUMB_STICK_RIGHT,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1903                 OnKey(key);
1904         }
1905         if (pGamepad->fX2 < -g_stSettings.m_fAnalogDeadzoneController && pGamepad->fY2 < -pGamepad->fX2 && -pGamepad->fY2 < -pGamepad->fX2)
1906         {
1907                 bGotKey=true;
1908                 CKey key(KEY_BUTTON_RIGHT_THUMB_STICK_LEFT,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,-pGamepad->fX2,pGamepad->fY2);
1909                 OnKey(key);
1910         }
1911         // analog trigger detection
1912         if (bLeftTrigger)
1913         {
1914                 bGotKey=true;
1915                 CKey key(KEY_BUTTON_LEFT_ANALOG_TRIGGER,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1916                 OnKey(key);
1917         }
1918         if (bRightTrigger)
1919         {
1920                 bGotKey=true;
1921                 CKey key(KEY_BUTTON_RIGHT_ANALOG_TRIGGER,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1922                 OnKey(key);
1923         }
1924         if ( wDir & DC_LEFTTRIGGER)
1925         {
1926                 bGotKey=true;
1927                 CKey key(KEY_BUTTON_LEFT_TRIGGER,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1928                 OnKey(key);
1929         }
1930         if ( wDir & DC_RIGHTTRIGGER)
1931         {
1932                 bGotKey=true;
1933                 CKey key(KEY_BUTTON_RIGHT_TRIGGER,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1934                 OnKey(key);
1935         }
1936         if ( wDir & DC_LEFT )
1937         {
1938                 bGotKey=true;
1939                 CKey key(KEY_BUTTON_DPAD_LEFT,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1940                 OnKey(key);
1941         }
1942         if ( wDir & DC_RIGHT)
1943         {
1944                 bGotKey=true;
1945                 CKey key(KEY_BUTTON_DPAD_RIGHT,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1946                 OnKey(key);
1947         }
1948         if ( wDir & DC_UP )
1949         {
1950                 bGotKey=true;
1951                 CKey key(KEY_BUTTON_DPAD_UP,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1952                 OnKey(key);
1953         }
1954         if ( wDir & DC_DOWN )
1955         {
1956                 bGotKey=true;
1957                 CKey key(KEY_BUTTON_DPAD_DOWN,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1958                 OnKey(key);
1959         }
1960
1961
1962         if ( pGamepad->wPressedButtons & XINPUT_GAMEPAD_BACK )
1963         {
1964                 bGotKey=true;
1965                 CKey key(KEY_BUTTON_BACK,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1966                 OnKey(key);
1967                 return;
1968         }
1969         if ( pGamepad->wPressedButtons & XINPUT_GAMEPAD_START)
1970         {
1971                 bGotKey=true;
1972                 CKey key(KEY_BUTTON_START,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1973                 OnKey(key);
1974         }
1975
1976         if ( pGamepad->wPressedButtons & XINPUT_GAMEPAD_LEFT_THUMB)
1977         {
1978                 bGotKey=true;
1979                 CKey key(KEY_BUTTON_LEFT_THUMB_BUTTON,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1980                 OnKey(key);
1981         }
1982
1983         if ( pGamepad->wPressedButtons & XINPUT_GAMEPAD_RIGHT_THUMB)
1984         {
1985                 bGotKey=true;
1986                 CKey key(KEY_BUTTON_RIGHT_THUMB_BUTTON,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1987                 OnKey(key);
1988         }
1989
1990
1991         if (pGamepad->bPressedAnalogButtons[XINPUT_GAMEPAD_A])
1992         {
1993                 bGotKey=true;
1994                 CKey key(KEY_BUTTON_A,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
1995                 OnKey(key);
1996         }
1997         if (pGamepad->bPressedAnalogButtons[XINPUT_GAMEPAD_B])
1998         {
1999                 bGotKey=true;
2000                 CKey key(KEY_BUTTON_B,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
2001                 OnKey(key);
2002         }
2003
2004         if (pGamepad->bPressedAnalogButtons[XINPUT_GAMEPAD_X])
2005         {
2006                 bGotKey=true;
2007                 CKey key(KEY_BUTTON_X,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
2008                 OnKey(key);
2009
2010         }
2011         if (pGamepad->bPressedAnalogButtons[XINPUT_GAMEPAD_Y])
2012         {
2013                 bGotKey=true;
2014                 CKey key(KEY_BUTTON_Y,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
2015                 OnKey(key);
2016         }
2017         if (pGamepad->bPressedAnalogButtons[XINPUT_GAMEPAD_BLACK])
2018         {
2019                 bGotKey=true;
2020                 CKey key(KEY_BUTTON_BLACK,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
2021                 OnKey(key);
2022         }
2023         if (pGamepad->bPressedAnalogButtons[XINPUT_GAMEPAD_WHITE])
2024         {
2025                 bGotKey=true;
2026                 CKey key(KEY_BUTTON_WHITE,bLeftTrigger,bRightTrigger,pGamepad->fX1,pGamepad->fY1,pGamepad->fX2,pGamepad->fY2);
2027                 OnKey(key);
2028         }
2029
2030         switch (wRemotes)
2031         {
2032                 // 0 is invalid
2033         case 0:
2034                 break;
2035                 // Map all other keys unchanged
2036         default:
2037                 {
2038                         bGotKey=true;
2039                         CKey key(wRemotes);
2040                         OnKey(key);
2041                         break;
2042                 }
2043         }
2044 }
2045
2046 bool CApplication::IsButtonDown(DWORD code)
2047 {
2048         if (code >= KEY_BUTTON_A && code <= KEY_BUTTON_RIGHT_TRIGGER)
2049         {
2050                 // analogue
2051                 return (m_DefaultGamepad.bAnalogButtons[code - KEY_BUTTON_A + XINPUT_GAMEPAD_A] > XINPUT_GAMEPAD_MAX_CROSSTALK);
2052         }
2053         else if (code >= KEY_BUTTON_DPAD_UP && code <= KEY_BUTTON_RIGHT_THUMB_BUTTON)
2054         {
2055                 // digital
2056                 return (m_DefaultGamepad.wButtons & (1 << (code - KEY_BUTTON_DPAD_UP))) != 0;
2057         }
2058         else
2059         {
2060                 // remote
2061                 return m_DefaultIR_Remote.wButtons == code;
2062         }
2063         return false;
2064 }
2065
2066 void CApplication::Stop()
2067 {
2068         try
2069         {
2070     m_bStop = true;
2071                 CLog::Log(LOGNOTICE, "stop all");
2072
2073                 CKaiClient::GetInstance()->RemoveObserver();
2074
2075     StopServices();
2076
2077                 if (m_pPlayer)
2078                 {
2079                         CLog::Log(LOGNOTICE, "stop mplayer");
2080                         delete m_pPlayer;
2081                         m_pPlayer=NULL;
2082                 }
2083
2084                 // if we have an active connection to iTunes, stop that too
2085                 if (g_application.m_DAAPPtr)
2086                 {
2087                         CDAAPDirectory *objDAAP;
2088
2089                         objDAAP = new CDAAPDirectory();
2090                         objDAAP->CloseDAAP();
2091                 }
2092
2093                 //g_lcd->StopThread();
2094                 CLog::Log(LOGNOTICE, "stop python");
2095                 g_applicationMessenger.Cleanup();
2096                 g_pythonParser.FreeResources();
2097
2098                 CLog::Log(LOGNOTICE, "unload skin");
2099                 m_guiMusicOverlay.FreeResources();
2100                 m_guiWindowVideoOverlay.FreeResources();
2101     m_guiPointer.FreeResources();
2102     m_guiDialogVolumeBar.FreeResources();
2103                 g_fontManager.Clear();
2104                 m_gWindowManager.DeInitialize();
2105                 g_TextureManager.Cleanup();
2106
2107                 CLog::Log(LOGNOTICE, "unload sections");
2108                 CSectionLoader::UnloadAll();
2109                 CLog::Log(LOGNOTICE, "destroy");
2110                 Destroy();
2111                 CLog::Log(LOGNOTICE, "stopped");
2112         }
2113         catch(...)
2114         {
2115         }
2116 }
2117
2118 bool CApplication::PlayFile(const CFileItem& item, bool bRestart)
2119 {
2120         CStdString strFile = item.m_strPath;
2121         if (CUtil::IsPlayList(strFile)) return false;
2122
2123         float AVDelay = 0;
2124
2125         m_tagCurrentMovie.Reset();
2126         m_iPlaySpeed=1;
2127         if (!bRestart)
2128         {
2129                 OutputDebugString("new file set audiostream:0\n");
2130                 g_stSettings.m_iAudioStream=-1;
2131                 g_stSettings.m_iSubtitleStream=-1;
2132                 g_stSettings.m_bNoCache=false;
2133                 g_stSettings.m_bNonInterleaved=false;
2134
2135                 // switch 2 default settings...
2136                 g_settings.m_iBrightness=50;
2137                 g_settings.m_iContrast=50;
2138                 g_settings.m_iGamma=20;
2139         }
2140         else
2141         {
2142                 AVDelay = m_pPlayer->GetAVDelay();
2143         }
2144
2145         CURL url(item.m_strPath);
2146         CStdString strNewPlayer = "mplayer";
2147         if ( url.GetProtocol() == "cdda")
2148         {
2149                 strNewPlayer = "cdda";
2150         }
2151         else if (ModPlayer::IsSupportedFormat(url.GetFileType()))
2152         {
2153                 strNewPlayer = "mod";
2154         }
2155         else if (url.GetFileType() == "sid")
2156         {
2157                 strNewPlayer = "sid";
2158         }
2159         // Check if we are moving from one cue sheet item to the next
2160         // need:
2161         // 1.  player to exist
2162         // 2.  current play time > endtime of current song.
2163         // 3.  next item's startoffset>0
2164         // 4.  next item start offset == current items end offset
2165         // 5.  current and next item based on same media file.
2166         if (m_pPlayer && (m_pPlayer->GetPTS() > m_itemCurrentFile.m_lEndOffset*10/75))
2167         {
2168                 if (item.m_lStartOffset > 0 && item.m_lStartOffset == m_itemCurrentFile.m_lEndOffset &&
2169                         item.m_strPath == m_itemCurrentFile.m_strPath && m_pPlayer)
2170                 {       // this is the next cue sheet item, so we don't have to restart the player
2171                         // just update our display etc.
2172                         m_itemCurrentFile=item;
2173                         m_guiMusicOverlay.SetCurrentFile(m_itemCurrentFile);
2174                         m_guiWindowVideoOverlay.SetCurrentFile(m_itemCurrentFile.m_strPath);
2175
2176                         m_dwIdleTime=timeGetTime();
2177                         return true;
2178                 }
2179         }
2180         //We have to stop parsing a cdg before mplayer is deallocated
2181         m_CdgParser.Stop();
2182         // We should restart the player, unless the previous and next tracks are using the cdda player
2183         // (allows gapless cdda playback)
2184         if (m_pPlayer && !(m_strCurrentPlayer == strNewPlayer && (m_strCurrentPlayer == "cdda" || m_strCurrentPlayer == "dvdplayer")))
2185         {
2186                 if (1||m_strCurrentPlayer != strNewPlayer || !CUtil::IsAudio(m_itemCurrentFile.m_strPath) )
2187                 {
2188                         delete m_pPlayer;
2189                         m_pPlayer=NULL;
2190                 }
2191         }
2192
2193         m_itemCurrentFile=item;
2194         m_strCurrentPlayer=strNewPlayer;
2195         if (!m_pPlayer)
2196         {
2197                 CPlayerCoreFactory factory;
2198                 m_pPlayer = factory.CreatePlayer(strNewPlayer,*this);
2199         }
2200
2201         bool bResult=m_pPlayer->openfile(m_itemCurrentFile.m_strPath, m_itemCurrentFile.m_lStartOffset*1000/75);
2202         if (bResult)
2203         {
2204                 m_guiMusicOverlay.SetCurrentFile(m_itemCurrentFile);
2205                 m_guiWindowVideoOverlay.SetCurrentFile(m_itemCurrentFile.m_strPath);
2206
2207                 if(CUtil::IsAudio(m_itemCurrentFile.m_strPath) && g_stSettings.m_bIsCdgEnabled)
2208                         m_CdgParser.Start(m_itemCurrentFile.m_strPath);
2209
2210                 m_dwIdleTime=timeGetTime();
2211
2212                 //if (bRestart)
2213                 //{
2214                 //      m_pPlayer->SetAVDelay(AVDelay);
2215                 //}
2216
2217                 //// if file happens to contain video stream
2218                 //if ( IsPlayingVideo())
2219                 //{
2220                 //      // then switch to fullscreen video mode if we can
2221                 //      SwitchToFullScreen();
2222                 //}
2223         }
2224         return bResult;
2225 }
2226
2227 void CApplication::OnPlayBackEnded()
2228 {
2229         //playback ended
2230         m_iPlaySpeed=1;
2231
2232         // informs python script currently running playback has started
2233         // (does nothing if python is not loaded)
2234         g_pythonParser.OnPlayBackEnded();
2235
2236         OutputDebugString("Playback has finished\n");
2237         CGUIMessage msg(GUI_MSG_PLAYBACK_ENDED, 0, 0, 0, 0, NULL);
2238         m_gWindowManager.SendThreadMessage(msg);
2239 }
2240
2241 void CApplication::OnPlayBackStarted()
2242 {
2243         // informs python script currently running playback has started
2244         // (does nothing if python is not loaded)
2245         g_pythonParser.OnPlayBackStarted();
2246
2247         CGUIMessage msg(GUI_MSG_PLAYBACK_STARTED, 0, 0, 0, 0, NULL);
2248         m_gWindowManager.SendThreadMessage(msg);
2249
2250   CheckNetworkHDSpinDown(true);
2251 }
2252
2253 bool CApplication::IsPlaying() const
2254 {
2255         if (!m_pPlayer) return false;
2256         if (!m_pPlayer->IsPlaying()) return false;
2257         return true;
2258 }
2259
2260 bool CApplication::IsPlayingAudio() const
2261 {
2262         if (!m_pPlayer) return false;
2263         if (!m_pPlayer->IsPlaying()) return false;
2264         if (m_pPlayer->HasVideo()) return false;
2265         if (m_pPlayer->HasAudio()) return true;
2266         return false;
2267 }
2268
2269 bool CApplication::IsPlayingVideo() const
2270 {
2271         if (!m_pPlayer) return false;
2272         if (!m_pPlayer->IsPlaying()) return false;
2273         if (m_pPlayer->HasVideo()) return true;
2274         return false;
2275 }
2276
2277
2278 void CApplication::StopPlaying()
2279 {
2280         int iWin = m_gWindowManager.GetActiveWindow();
2281         if ( IsPlaying() )
2282         {
2283                 //      turn off visualisation window when stopping
2284                 if (iWin==WINDOW_VISUALISATION)
2285                         m_gWindowManager.PreviousWindow();
2286                 m_pPlayer->closefile();
2287         }
2288         m_CdgParser.Free();
2289         CGUIMessage msg( GUI_MSG_PLAYBACK_STOPPED, 0, 0, 0, 0, NULL );
2290         m_gWindowManager.SendMessage(msg);
2291 }
2292
2293
2294 bool CApplication::NeedRenderFullScreen()
2295 {
2296         if (m_gWindowManager.GetActiveWindow() == WINDOW_FULLSCREEN_VIDEO)
2297         {
2298                 CGUIWindowFullScreen *pFSWin = (CGUIWindowFullScreen *)m_gWindowManager.GetWindow(WINDOW_FULLSCREEN_VIDEO);
2299                 if (!pFSWin) return false;
2300                 return pFSWin->NeedRenderFullScreen();
2301         }
2302         return false;
2303 }
2304 void CApplication::RenderFullScreen()
2305 {
2306         if (m_gWindowManager.GetActiveWindow() == WINDOW_FULLSCREEN_VIDEO)
2307         {
2308                 CGUIWindowFullScreen *pFSWin = (CGUIWindowFullScreen *)m_gWindowManager.GetWindow(WINDOW_FULLSCREEN_VIDEO);
2309                 if (!pFSWin) return ;
2310                 pFSWin->RenderFullScreen();
2311         }
2312 }
2313
2314 void CApplication::ResetScreenSaver()
2315 {
2316         if (m_bInactive)
2317         {
2318                 m_dwSaverTick=timeGetTime();    // Start the timer going ...
2319         }
2320 }
2321
2322 bool CApplication::ResetScreenSaverWindow()
2323 {
2324         m_bInactive=false;              // reset the inactive flag as a key has been pressed
2325         // if Screen saver is active
2326         if (m_bScreenSave)
2327         {
2328                 // disable screensaver
2329                 m_bScreenSave = false;
2330
2331                 // if matrix trails screensaver is active
2332                 int iWin = m_gWindowManager.GetActiveWindow();
2333                 if (iWin  == WINDOW_SCREENSAVER)
2334                 {
2335                         // then show previous window
2336                         m_gWindowManager.PreviousWindow();
2337                 }
2338                 else
2339                 {
2340                         // Fade to dim or black screensaver is active
2341                         // just un-dim the screen
2342                         m_pd3dDevice->SetGammaRamp(0, &m_OldRamp);      // put the old gamma ramp back in place
2343                 }
2344                 return true;
2345         }
2346         else
2347         {
2348                 return false;
2349         }
2350 }
2351
2352 void CApplication::CheckScreenSaver()
2353 {
2354         D3DGAMMARAMP Ramp;
2355         FLOAT fFadeLevel;
2356
2357         if ( m_gWindowManager.IsRouted()) return;
2358         if (g_stSettings.m_iLCDMode==LCD_MODE_NOTV) return;
2359
2360         if (!m_bInactive)
2361         {
2362                 if (IsPlayingVideo() && !m_pPlayer->IsPaused()) // are we playing a movie and is it paused?
2363                 {
2364                         m_bInactive=false;
2365                 }
2366                 else if (IsPlayingAudio())      // are we playing some music?
2367                 {
2368                         if (m_gWindowManager.GetActiveWindow() == WINDOW_VISUALISATION)
2369                         {
2370                                 m_bInactive=false;      // visualisation is on, so we cannot show a screensaver
2371                         }
2372                         else
2373                         {
2374                                 m_bInactive=true;       // music playing from GUI, we can display a screensaver
2375                         }
2376                 }
2377                 else
2378                 {
2379                         // we can display a screensaver
2380                         m_bInactive=true;
2381                 }
2382
2383                 // if we can display a screensaver, then start screensaver timer
2384                 if (m_bInactive)
2385                 {
2386                         m_dwSaverTick=timeGetTime();    // Start the timer going ...
2387                 }
2388         }
2389         else
2390         {
2391                 // Check we're not already in screensaver mode
2392                 if (!m_bScreenSave)
2393                 {
2394                         // no, then check the timer if screensaver should pop up
2395                         if ( (long)(timeGetTime() - m_dwSaverTick) >= (long)(g_stSettings.m_iScreenSaverTime*60*1000L) )
2396                         {
2397                                 //yes, show the screensaver
2398                                 m_bScreenSave = true;
2399                                 m_dwSaverTick=timeGetTime();            // Save the current time for the shutdown timeout
2400
2401                                 switch ( g_stSettings.m_iScreenSaverMode )
2402                                 {
2403                                 case SCREENSAVER_FADE:
2404                                         {
2405                                                 fFadeLevel = (FLOAT) g_stSettings.m_iScreenSaverFadeLevel / 100; // 0.07f;
2406                                         }
2407                                         break;
2408
2409                                 case SCREENSAVER_BLACK:
2410                                         {
2411                                                 fFadeLevel = 0;
2412                                         }
2413                                         break;
2414
2415                                 case SCREENSAVER_MATRIX:
2416                                         {
2417                                                 if (!IsPlayingVideo())
2418                                                 {
2419                                                         m_gWindowManager.ActivateWindow(WINDOW_SCREENSAVER);
2420                                                         return;
2421                                                 }
2422                                                 else
2423                                                 {
2424                                                         fFadeLevel = (FLOAT) g_stSettings.m_iScreenSaverFadeLevel / 100; // 0.07f;
2425                                                 }
2426                                         }
2427                                         break;
2428                                 }
2429
2430                                 m_pd3dDevice->GetGammaRamp(&m_OldRamp); // Store the old gamma ramp
2431                                 // Fade to fFadeLevel
2432                                 for (float fade=1.f; fade>=fFadeLevel; fade-=0.01f)
2433                                 {
2434                                         for(int i=0;i<256;i++)
2435                                         {
2436                                                 Ramp.red[i]=(int)((float)m_OldRamp.red[i]*fade);
2437                                                 Ramp.green[i]=(int)((float)m_OldRamp.green[i]*fade);
2438                                                 Ramp.blue[i]=(int)((float)m_OldRamp.blue[i]*fade);
2439                                         }
2440                                         Sleep(5);
2441                                         m_pd3dDevice->SetGammaRamp(D3DSGR_IMMEDIATE, &Ramp);    // use immediate to get a smooth fade
2442                                 }
2443                         }
2444                 }
2445         }
2446
2447         return;
2448 }
2449
2450 void CApplication::CheckShutdown()
2451 {
2452         // Note: if the the screensaver is switched on, the shutdown timeout is
2453         // counted from when the screensaver activates.
2454         if (!m_bInactive)
2455         {
2456                 if (IsPlayingVideo() && !m_pPlayer->IsPaused()) // are we playing a movie?
2457             &n