]> git.vomp.tv Git - vompclient.git/blob - winmain.cc
Sleep timer
[vompclient.git] / winmain.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20 #ifdef WIN32
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <signal.h>
25
26 #define _WIN32_WINNT 0x501
27 #include <winsock2.h>
28 #include <windows.h>
29
30 #include "vompreswin.h"
31
32 #include "defines.h"
33 #include "log.h"
34 #include "remotewin.h"
35 #include "ledwin.h"
36 #include "mtdwin.h"
37 #include "timers.h"
38 #include "videowin.h"
39 #include "audiowin.h"
40 #include "vdr.h"
41 #include "osdwin.h"
42 #include "boxstack.h"
43 #include "command.h"
44 #include "wol.h"
45
46 void sighandler(int signalReceived);
47 void shutdown(int code);
48
49 // Global variables --------------------------------------------------------------------------------------------------
50 int debugEnabled = 0;
51 Log* logger;
52 Remote* remote;
53 Mtd* mtd;
54 Led* led;
55 Osd* osd;
56 Timers* timers;
57 BoxStack* boxstack;
58 Command* command;
59 VDR* vdr;
60 Video* video;
61 Audio* audio;
62 Wol* wol;
63
64 bool wnd_fullscreen=false;
65 bool wnd_topmost=false;
66 RECT wnd_fs_rect={20,20,768+20,576+20};
67 RECT wnd_fs_rect_client={0,0,768,576};
68 //OSVERSIONINFO windows_ver; //attempt to distigsh windows versions
69 bool remotefnc=false;
70 HINSTANCE  hinstance;
71 bool cmenu=false;
72
73 HMODULE user32dll;
74 typedef UINT (WINAPI *GETRAWINPUTDATAFNC) (HRAWINPUT,UINT,LPVOID,PUINT,UINT);
75 typedef UINT (WINAPI *REGISTERRAWINPUTDEVICEFNC)  (PCRAWINPUTDEVICE,UINT,UINT);
76
77 GETRAWINPUTDATAFNC dynGetRawInputData=NULL;
78 REGISTERRAWINPUTDEVICEFNC dynRegisterRawInputDevices=NULL;
79
80 DWORD lastmousemove;
81
82
83 void MILLISLEEP(ULONG a)
84 {
85
86   Sleep(a);
87
88 }
89
90 DWORD WINAPI commandthreadStart(void *arg)
91 {
92    command->run();
93    return 0;
94 }
95
96 void LoadRemoteFunctions() {
97   user32dll=LoadLibrary("user32.dll");
98   if (user32dll!=NULL) {
99     dynGetRawInputData=(GETRAWINPUTDATAFNC)GetProcAddress(user32dll,"GetRawInputData");
100     if (dynGetRawInputData!=NULL) {
101       dynRegisterRawInputDevices=(REGISTERRAWINPUTDEVICEFNC)GetProcAddress(user32dll,"RegisterRawInputDevices");
102       if (dynRegisterRawInputDevices!=NULL) {
103         remotefnc=true;
104       }
105     }
106   }
107 }
108
109 bool InitApp(HINSTANCE hinst,int cmdshow);
110
111 HWND win_main;//global window handle
112 HWND win;//global child window handle
113 HACCEL acc;
114
115 #define ERROR_MSG(str) MessageBox(win_main,str,"Error!",MB_OK|MB_ICONWARNING)
116 INT WINAPI WinMain( HINSTANCE hinst , HINSTANCE previnst, LPSTR cmdline, int cmdshow)
117 {
118   hinstance=hinst;
119   //On Windows we have to init a window, we use DXUT
120   LoadRemoteFunctions();
121   if (!InitApp(hinst,cmdshow)) return false;
122   //Starting Network support
123   WSADATA wsadat;
124   int result = WSAStartup(MAKEWORD(2,2),&wsadat);
125   if (result!=NO_ERROR) {
126         ERROR_MSG("Initialising WinSocked: Error at WSAStartup()\n");
127     return 0;
128   }
129
130   result= CoInitializeEx(NULL,COINIT_MULTITHREADED );//Initialize COM for DirectShow
131   if (result!=S_OK) {
132     ERROR_MSG("Initialising COM: Error at Coinitialize()\n");
133     return 0;
134   }
135
136
137
138
139   // Init global vars ------------------------------------------------------------------------------------------------
140
141   logger     = new Log();
142   remote     = new RemoteWin();
143   mtd        = new MtdWin();
144   led        = new LedWin();
145   timers     = new Timers();
146   osd        = new OsdWin();
147   vdr        = new VDR();
148   video      = new VideoWin();
149   audio      = new AudioWin();
150   boxstack   = new BoxStack();
151   command    = new Command();
152   wol        = new Wol();
153
154   if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !boxstack || !command)
155   {
156     ERROR_MSG("Could not create objects. Memory problems?\n");
157     shutdown(1);
158   WSACleanup();
159   return 0;
160   }
161
162   // Get logging module started --------------------------------------------------------------------------------------
163
164   if (!logger->init(Log::DEBUG, "vompwin.log", true))
165   {
166     ERROR_MSG("Could not initialise log object. Aborting.\n");
167     shutdown(1);
168   WSACleanup();
169   return 0;
170   }
171
172   logger->log("Core", Log::INFO, "Starting up...");
173
174
175
176   // Init modules ----------------------------------------------------------------------------------------------------
177   int success;
178
179   success = remote->init("/dev/rawir");
180   if (success)
181   {
182     logger->log("Core", Log::INFO, "Remote module initialised");
183   }
184   else
185   {
186     logger->log("Core", Log::EMERG, "Remote module failed to initialise");
187     shutdown(1);
188   WSACleanup();
189   return 0;
190   }
191
192   success = led->init(0);
193   if (success)
194   {
195     logger->log("Core", Log::INFO, "LED module initialised");
196   }
197   else
198   {
199     logger->log("Core", Log::EMERG, "LED module failed to initialise");
200     shutdown(1);
201   WSACleanup();
202   return 0;
203   }
204
205   success = mtd->init();
206   if (success)
207   {
208     logger->log("Core", Log::INFO, "Mtd module initialised");
209   }
210   else
211   {
212     logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
213     shutdown(1);
214   WSACleanup();
215   return 0;
216   }
217
218   success = timers->init();
219   if (success)
220   {
221     logger->log("Core", Log::INFO, "Timers module initialised");
222   }
223   else
224   {
225     logger->log("Core", Log::EMERG, "Timers module failed to initialise");
226     shutdown(1);
227   WSACleanup();
228   return 0;
229   }
230
231   UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
232   if      (videoFormat == Video::PAL)  logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
233   else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
234   else                                 logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
235
236   success = video->init(videoFormat);
237   if (success)
238   {
239     logger->log("Core", Log::INFO, "Video module initialised");
240   }
241   else
242   {
243     logger->log("Core", Log::EMERG, "Video module failed to initialise");
244     shutdown(1);
245   WSACleanup();
246   return 0;
247   }
248
249   success = osd->init((void*)&win);
250   if (success)
251   {
252     logger->log("Core", Log::INFO, "OSD module initialised");
253   }
254   else
255   {
256     logger->log("Core", Log::EMERG, "OSD module failed to initialise");
257     shutdown(1);
258   WSACleanup();
259   return 0;
260   }
261
262   success = audio->init(Audio::MPEG2_PES);
263   if (success)
264   {
265     logger->log("Core", Log::INFO, "Audio module initialised");
266   }
267   else
268   {
269     logger->log("Core", Log::EMERG, "Audio module failed to initialise");
270     shutdown(1);
271   WSACleanup();
272   return 0;
273   }
274
275   success = vdr->init(3024);
276   if (success)
277   {
278     logger->log("Core", Log::INFO, "VDR module initialised");
279   }
280   else
281   {
282     logger->log("Core", Log::EMERG, "VDR module failed to initialise");
283     shutdown(1);
284   WSACleanup();
285   return 0;
286   }
287
288   success = boxstack->init();
289   if (success)
290   {
291     logger->log("Core", Log::INFO, "BoxStack module initialised");
292   }
293   else
294   {
295     logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
296     shutdown(1);
297   WSACleanup();
298   return 0;
299   }
300
301   success = command->init();
302   if (success)
303   {
304     logger->log("Core", Log::INFO, "Command module initialised");
305   }
306   else
307   {
308     logger->log("Core", Log::EMERG, "Command module failed to initialise");
309     shutdown(1);
310   WSACleanup();
311   return 0;
312   }
313
314   // Other init ------------------------------------------------------------------------------------------------------
315
316   logger->log("Core", Log::NOTICE, "Startup successful");
317
318   // Run main loop ---------------------------------------------------------------------------------------------------
319
320   // Ok, all major device components and other bits are loaded and ready
321   lastmousemove=timeGetTime();
322
323   HANDLE commandthread;
324  commandthread= CreateThread(NULL, 0, commandthreadStart, NULL,0,
325     NULL);
326   MSG message;
327   message.message=WM_NULL;
328   bool run=true;
329   while(run && WaitForSingleObject(commandthread,0)==WAIT_TIMEOUT) {
330     if (PeekMessage(&message, NULL, 0,0,PM_REMOVE)!=0) {
331       if (TranslateAccelerator(win_main,acc,&message)==NULL) {
332         TranslateMessage(&message);
333         DispatchMessage(&message);
334         switch (message.message) {
335         case WM_QUIT:
336           run=false; //TODO post exit to command Messages
337         };
338       }
339     } else {
340       //Render
341       ((OsdWin*)osd)->Render();
342     }
343   }
344   // When that returns quit ------------------------------------------------------------------------------------------
345   WaitForSingleObject(commandthread,INFINITE);
346   shutdown(0);
347   WSACleanup();
348   if (user32dll) FreeModule(user32dll);
349   return 0;
350
351 }
352
353 bool TranslateMousePosition(POINT *pos) {
354
355   RECT clientrect;
356   ScreenToClient(win,pos);
357   GetClientRect(win,&clientrect);
358   if (!PtInRect(&clientrect,*pos)) return false;//Don't pass it further
359   pos->x=((double)pos->x)/((double) (clientrect.right-clientrect.left))
360     *((double)Video::getInstance()->getScreenWidth());
361   pos->y=((double)pos->y)/((double) (clientrect.bottom-clientrect.top))
362     *((double)Video::getInstance()->getScreenHeight());
363   return true;
364
365 }
366
367
368
369 void CalculateWindowSize(RECT * size,ULONG size_mode) {
370
371   DWORD width, height;
372   DWORD adjheight,adjwidth;
373   if (!wnd_fullscreen) {
374     DWORD flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
375                |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
376     RECT wnted={50,50,150,150};
377     AdjustWindowRect(&wnted,flags ,false);
378     adjwidth=-wnted.left+wnted.right-100;
379     adjheight=-wnted.top+wnted.bottom-100;
380   } else {
381     adjwidth=adjheight=0;
382   }
383   width=size->right-size->left-adjwidth;
384   height=size->bottom-size->top-adjheight;
385   UCHAR mode=video->getMode();
386   UCHAR aspect=((VideoWin*)video)->getAspectRatio();
387   UCHAR tvsize=((VideoWin*)video)->getPseudoTVsize();
388   double aspectrt=4./3.;
389   if (tvsize==Video::ASPECT16X9) {
390     if (aspect==Video::ASPECT16X9) {
391       aspectrt=4./3.; //looks strange, but it is a 16:9 tv
392     } else if (aspect==Video::ASPECT4X3) {
393       aspectrt=4./3./(16./9.)*(4./3.); //I hope this is correct
394     }
395   } else if (tvsize==Video::ASPECT4X3) {
396     if (aspect==Video::ASPECT16X9) {
397       if (mode!=Video::NORMAL) {
398         aspectrt=16./9.;
399       } else {
400         aspectrt=4./3.;
401       }
402     } else if (aspect==Video::ASPECT4X3) {
403       aspectrt=4./3.;
404     }
405   }
406   if (!wnd_fullscreen) {
407     switch (size_mode) {
408     case WMSZ_BOTTOM:
409     case WMSZ_BOTTOMRIGHT:
410     case WMSZ_TOP:
411     case WMSZ_TOPRIGHT:
412     width=(ULONG)(((double)height)*aspectrt);
413     size->right=size->left+width+adjwidth;
414     break;
415     case WMSZ_BOTTOMLEFT:
416     case WMSZ_TOPLEFT:
417     width=(ULONG)(((double)height)*aspectrt);
418     size->left=size->right-width-adjwidth;
419     break;
420     case WMSZ_LEFT:
421     case WMSZ_RIGHT:
422     height=(ULONG)(((double)width)/aspectrt);
423     size->bottom=size->top+height+adjheight;
424     break;
425     }
426     MoveWindow(win,0,0,width,height,TRUE);
427   } else {
428     RECT newrect={0,0,width,height};
429     DWORD newlength;
430     if ((ULONG)(((double)height)*aspectrt)>width) {
431       newlength=(ULONG)(((double)width)/aspectrt);
432       newrect.top+=(height-newlength)/2;
433       newrect.bottom-=(height-newlength);
434     } else {
435       newlength=(ULONG)(((double)height)*aspectrt);
436       newrect.left+=(width-newlength)/2;
437       newrect.right-=(width-newlength);
438     }
439     MoveWindow(win,newrect.left,newrect.top,newrect.right,newrect.bottom,TRUE);
440   }
441
442 }
443
444 void AdjustWindow() {
445   if (!wnd_fullscreen) {
446     RECT winrect;
447     GetWindowRect(win_main,&winrect);
448     CalculateWindowSize(&winrect,WMSZ_BOTTOM);
449     MoveWindow(win_main,winrect.left,
450       winrect.top,winrect.right-winrect.left,winrect.bottom-winrect.top,true);
451   } else {
452     RECT winrect;
453     GetWindowRect(win_main,&winrect);
454     CalculateWindowSize(&winrect,WMSZ_BOTTOM);
455
456   }
457 }
458
459 void ToggleFullscreen() {
460   if (wnd_fullscreen) {
461     wnd_fullscreen=false;
462     SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
463                  |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX);
464     SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
465
466     SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_TOP,wnd_fs_rect.left,wnd_fs_rect.top,
467               wnd_fs_rect.right-wnd_fs_rect.left,
468               wnd_fs_rect.bottom-wnd_fs_rect.top,
469               SWP_DRAWFRAME | SWP_FRAMECHANGED);
470     MoveWindow(win,wnd_fs_rect_client.left,wnd_fs_rect_client.top,
471               wnd_fs_rect_client.right-wnd_fs_rect_client.left,
472               wnd_fs_rect_client.bottom-wnd_fs_rect_client.top,TRUE);
473     AdjustWindow();
474   } else {
475     GetWindowRect(win_main,&wnd_fs_rect);
476     GetWindowRect(win,&wnd_fs_rect_client);
477     SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP );
478     SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
479     HMONITOR monitor=MonitorFromWindow(win_main,MONITOR_DEFAULTTONEAREST);
480     MONITORINFO moninfo;
481     moninfo.cbSize=sizeof(moninfo);
482     wnd_fullscreen=true;
483     if (!GetMonitorInfo(monitor,&moninfo)) return ;
484     SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_TOP,moninfo.rcMonitor.left,moninfo.rcMonitor.top,
485       moninfo.rcMonitor.right,moninfo.rcMonitor.bottom,SWP_FRAMECHANGED);
486
487     AdjustWindow();
488
489   }
490
491
492 }
493
494 void ToggleTopmost() {
495   wnd_topmost=!wnd_topmost;
496   SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_NOTOPMOST,0,0,
497       0,0,SWP_NOMOVE | SWP_NOSIZE);
498 }
499
500 void CursorUpdate() {
501   POINT cursorpos;
502   GetCursorPos(&cursorpos);
503   HWND asswind;
504   asswind=WindowFromPoint(cursorpos);
505   if (asswind!=win_main && asswind!=win) {
506     return ; //not our responsibility
507   }
508   if ((timeGetTime()-lastmousemove)<4000 || cmenu) {
509     SetCursor(LoadCursor(NULL,IDC_ARROW));
510   } else {
511     SetCursor(NULL);
512   }
513 }
514
515 bool ContextMenu(HWND wind,int x,int y) {
516   POINT p={x,y};
517   RECT clientrect;
518   ScreenToClient(wind,&p);
519   GetClientRect(wind,&clientrect);
520   if (!PtInRect(&clientrect,p)) return false;
521   ClientToScreen(wind,&p);
522   HMENU menu;
523   HMENU popup;
524   menu=LoadMenu(hinstance,MAKEINTRESOURCE(VOMPMENU));
525   popup=GetSubMenu(menu,0);
526   if (wnd_fullscreen) {
527     CheckMenuItem(popup,VOMP_FULL_SCREEN,MF_BYCOMMAND|MF_CHECKED);
528   } else {
529     CheckMenuItem(popup,VOMP_FULL_SCREEN,MF_BYCOMMAND|MF_UNCHECKED);
530   }
531   if (wnd_topmost) {
532     CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_CHECKED);
533   } else {
534     CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_UNCHECKED);
535   }
536   cmenu=true;
537   TrackPopupMenu(popup,TPM_RIGHTBUTTON|TPM_LEFTALIGN,x,y,0,wind, NULL);
538   cmenu=false;
539
540
541   DestroyMenu(menu);
542   return true;
543 }
544
545 LONG FAR PASCAL WindowProc(HWND wind, UINT msg, WPARAM wparam, LPARAM lparam)
546 {
547
548    switch (msg) {
549    case WM_DESTROY: {
550      //TODO: call command
551      logger->log("Core", Log::NOTICE, "Window closed, shutting down...");
552
553      ((RemoteWin*)Remote::getInstance())->SendPower();
554      PostQuitMessage(0);
555   }break;
556    case WM_SIZING: {
557      CalculateWindowSize((RECT*) lparam,wparam);
558      return TRUE;
559            }break;
560   case WM_SIZE: {
561         int width = LOWORD(lparam);
562         int height = HIWORD(lparam);
563          //Call device
564         if (wparam == SIZE_MAXIMIZED) {
565             ToggleFullscreen();
566             return 0;
567         } else if (wparam == SIZE_MINIMIZED) {
568             ToggleFullscreen();
569             return 0;
570         }
571         }
572         break;
573    case WM_PAINT:
574         RECT r;
575         PAINTSTRUCT ps;
576         if (GetUpdateRect(wind, &r, FALSE)) {
577             BeginPaint(wind, &ps);
578             //Call Painting Mechanism
579             EndPaint(wind, &ps);
580         }
581         break;
582    case WM_KEYDOWN:
583      if (((RemoteWin*)remote)->ReceiveButtonVK(wparam)) {
584        return 0L; //We process that Key
585      } else {
586        return DefWindowProc(wind, msg, wparam, lparam);
587      }
588
589      break;
590      case WM_CHAR:
591      if (((RemoteWin*)remote)->ReceiveButtonCH(wparam)) {
592        return 0L; //We process that Key
593      } else {
594        return DefWindowProc(wind, msg, wparam, lparam);
595      }
596
597      break;
598   case WM_APPCOMMAND:
599     if (((RemoteWin*)remote)->ReceiveButtonAP(GET_APPCOMMAND_LPARAM(lparam))){
600       return TRUE; //yes we process that message
601     } else {
602       return DefWindowProc(wind, msg, wparam, lparam);
603     }
604
605     break;
606   case WM_INPUT:
607     if (remotefnc ) {
608       //only on XP!
609        LPRAWINPUT lpit;
610        UINT risize;
611        dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,NULL,&risize,sizeof(RAWINPUTHEADER));
612        lpit=(LPRAWINPUT)malloc(risize);
613        dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,lpit,&risize,sizeof(RAWINPUTHEADER));
614
615       if (lpit->header.dwType==RIM_TYPEHID && lpit->data.hid.dwSizeHid>=2) {
616         DWORD button=lpit->data.hid.bRawData[1] | (lpit->data.hid.bRawData[0]<< 8);
617         if (((RemoteWin*)remote)->ReceiveButtonRI(button)){
618           free(lpit);
619           return 0; //yes we process that message
620         }
621       }
622       free(lpit);
623     }
624     return DefWindowProc(wind, msg, wparam, lparam);
625
626
627     break;
628   case WM_COMMAND:
629     if (LOWORD(wparam)==VOMP_FULL_SCREEN) {
630       ToggleFullscreen();
631       return 0;
632     }
633     if (LOWORD(wparam)==VOMP_TOPMOST) {
634       ToggleTopmost();
635       return 0;
636     }
637     if (((RemoteWin*)remote)->ReceiveButtonAP(LOWORD(wparam))){
638       return 0; //yes we process that message
639     } else {
640       return DefWindowProc(wind, msg, wparam, lparam);
641     }
642
643     break;
644   case WM_SETCURSOR:
645     if (((HANDLE)wparam)==win) {
646       CursorUpdate();
647       return 1;
648     } else {
649       return DefWindowProc(wind, msg, wparam, lparam);
650     }
651     break;
652   case WM_SYSCOMMAND:
653     if (wparam==SC_MAXIMIZE) {
654       ToggleFullscreen();
655       return 0;
656     } else if (wparam==SC_SCREENSAVE || wparam==SC_MONITORPOWER) {
657       return 0;
658     } else {
659       return DefWindowProc(wind,msg,wparam, lparam);
660     }
661     break;
662   case WM_MOUSEMOVE: {
663
664     lastmousemove=timeGetTime();
665     SetCursor(LoadCursor(NULL,IDC_ARROW));
666     SetTimer(wind,VOMP_CURSORUPDATE,4500,NULL);
667     POINT mpos={GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam)};
668     ClientToScreen(wind,&mpos);
669     if (TranslateMousePosition(&mpos)) {
670       Message *mousemes=new Message();
671       mousemes->message=Message::MOUSE_MOVE;
672       mousemes->from=NULL;
673       mousemes->to=BoxStack::getInstance();
674       mousemes->parameter=(mpos.x & 0xFFFF)<< 16| (mpos.y & 0xFFFF);
675       mousemes->tag=0;
676       //command->postMessageFromOuterSpace(mousemes);
677       command->postMessageIfNotBusy(mousemes);
678     }
679
680     return 0;
681     //return DefWindowProc(wind,msg,wparam, lparam);
682              }
683     break;
684   case WM_TIMER:
685     if (wparam==VOMP_CURSORUPDATE) {
686       CursorUpdate();
687       return 0;
688     }
689     return DefWindowProc(wind, msg, wparam, lparam);
690
691     break;
692   case WM_CONTEXTMENU:
693     if (!ContextMenu(wind,GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam))) {
694       return DefWindowProc(wind, msg, wparam, lparam);
695     } else {
696       return 0;
697     }
698     break;
699   case WM_LBUTTONDOWN:{
700     POINT mpos={GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam)};
701     ClientToScreen(wind,&mpos);
702     if (TranslateMousePosition(&mpos)) {
703       Message *mousemes=new Message();
704       mousemes->message=Message::MOUSE_LBDOWN;
705       mousemes->from=NULL;
706       mousemes->to=BoxStack::getInstance();
707       mousemes->parameter=(mpos.x & 0xFFFF)<< 16| (mpos.y & 0xFFFF);
708       mousemes->tag=0;
709       command->postMessageFromOuterSpace(mousemes);
710     }
711             }break;
712     default:
713         return DefWindowProc(wind, msg, wparam, lparam);
714     }
715     return 0L;
716 }
717
718
719 bool InitApp(HINSTANCE hinst,int cmdshow) {
720   /* main window */
721   WNDCLASS wcs;
722   DWORD flags;
723   wcs.style = CS_HREDRAW | CS_VREDRAW;
724     wcs.lpfnWndProc = WindowProc;
725     wcs.cbClsExtra = 0;
726     wcs.cbWndExtra = sizeof(DWORD);
727     wcs.hInstance = hinst;
728     wcs.hIcon = NULL;
729     wcs.hCursor = NULL;
730     wcs.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
731     wcs.lpszMenuName = NULL;
732     wcs.lpszClassName = "vomp";
733   acc=LoadAccelerators(hinst,MAKEINTRESOURCE(VOMPACCELERATOR));
734   if (!RegisterClass(&wcs))
735         return false;
736   flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
737                  |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
738   RECT wnted={50,50,768+50,576+50};
739   AdjustWindowRect(&wnted,flags ,false);
740   win_main=CreateWindow("vomp","VOMP on Windows",flags, CW_USEDEFAULT,CW_USEDEFAULT,
741     wnted.right-wnted.left,wnted.bottom-wnted.top,NULL,NULL,hinst,NULL);
742   if (!win_main)
743         return FALSE;
744   ShowWindow(win_main,SW_SHOWNORMAL);
745     UpdateWindow(win_main);
746   /* in order to handle letterboxing we use a child window */
747   WNDCLASS child_wcs;
748   child_wcs.style = CS_HREDRAW | CS_VREDRAW;
749     child_wcs.lpfnWndProc = WindowProc;
750     child_wcs.cbClsExtra = 0;
751     child_wcs.cbWndExtra = sizeof(DWORD);
752     child_wcs.hInstance = hinst;
753     child_wcs.hIcon = NULL;
754     child_wcs.hCursor = NULL;
755     child_wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
756     child_wcs.lpszMenuName = NULL;
757     child_wcs.lpszClassName = "vomp_playback";
758   if (!RegisterClass(&child_wcs))
759         return false;
760
761   win=CreateWindow("vomp_playback","Vomp Playback Window",WS_VISIBLE | WS_CHILD |WS_CLIPCHILDREN,
762     0,0,768,576,win_main,NULL,hinst,NULL);
763   if (!win)
764     return FALSE;
765   ShowWindow(win,SW_SHOWNORMAL);
766   UpdateWindow(win);
767   if (remotefnc) {//at least windows XP
768     /* We want to support MCE Remote controls*/
769     RAWINPUTDEVICE remote_control_data[4];
770     ZeroMemory(remote_control_data,sizeof(remote_control_data));
771     remote_control_data[0].usUsagePage=0xFFBC;
772     remote_control_data[0].usUsage=0x88;
773     remote_control_data[0].dwFlags=0;
774     remote_control_data[1].usUsagePage=0x0C;
775     remote_control_data[1].usUsage=0x80;
776     remote_control_data[1].dwFlags=0;
777     remote_control_data[2].usUsagePage=0x0C;
778     remote_control_data[2].usUsage=0x01;
779     remote_control_data[2].dwFlags=0;
780     remote_control_data[3].usUsagePage=0x01;
781     remote_control_data[3].usUsage=0x80;
782     remote_control_data[3].dwFlags=0;
783     if (dynRegisterRawInputDevices(remote_control_data,4,sizeof(remote_control_data[0]))!=TRUE) {
784       MessageBox(0,"Registering remote control failed!","Aborting!",0);
785       return FALSE;
786     }
787
788   }
789   return TRUE;
790 }
791
792
793
794
795
796 // -------------------------------------------------------------------------------------------------------------------
797
798 void shutdown(int code)
799 {
800   if (boxstack)
801   {
802     boxstack->shutdown();
803     delete boxstack;
804     logger->log("Core", Log::NOTICE, "BoxStack module shut down");
805   }
806
807   if (command) // shut down command here in case views have posted messages
808   {
809     command->shutdown();
810     delete command;
811     logger->log("Core", Log::NOTICE, "Command module shut down");
812   }
813
814   if (vdr)
815   {
816     vdr->shutdown();
817     delete vdr;
818     logger->log("Core", Log::NOTICE, "VDR module shut down");
819   }
820
821   if (osd)
822   {
823     osd->shutdown();
824     delete osd;
825     logger->log("Core", Log::NOTICE, "OSD module shut down");
826   }
827
828   if (audio)
829   {
830     audio->shutdown();
831     delete audio;
832     logger->log("Core", Log::NOTICE, "Audio module shut down");
833   }
834
835   if (video)
836   {
837     video->shutdown();
838     delete video;
839     logger->log("Core", Log::NOTICE, "Video module shut down");
840   }
841
842   if (timers)
843   {
844     timers->shutdown();
845     delete timers;
846     logger->log("Core", Log::NOTICE, "Timers module shut down");
847   }
848
849   if (mtd)
850   {
851     mtd->shutdown();
852     delete mtd;
853     logger->log("Core", Log::NOTICE, "MTD module shut down");
854   }
855
856   if (led)
857   {
858     led->shutdown();
859     delete led;
860     logger->log("Core", Log::NOTICE, "LED module shut down");
861   }
862
863   if (remote)
864   {
865     remote->shutdown();
866     delete remote;
867     logger->log("Core", Log::NOTICE, "Remote module shut down");
868   }
869   if (wol)
870   {
871     delete wol;
872     logger->log("Core", Log::NOTICE, "WOL module shut down");
873   }
874
875   if (logger)
876   {
877     logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
878     logger->shutdown();
879     delete logger;
880   }
881   ExitProcess(0);
882
883 }
884
885 // -------------------------------------------------------------------------------------------------------------------
886
887 ULLONG ntohll(ULLONG a)
888 {
889   return htonll(a);
890 }
891
892 ULLONG htonll(ULLONG a)
893 {
894   return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32)
895     |(ULONG)htonl(((ULONG) (a >> 32))));
896 }
897 #endif
898