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