2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
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.
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.
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.
26 #define _WIN32_WINNT 0x501
30 #include "vompreswin.h"
34 #include "remotewin.h"
45 #include "vsleeptimer.h"
47 void sighandler(int signalReceived);
48 void shutdown(int code);
50 // Global variables --------------------------------------------------------------------------------------------------
64 Sleeptimer* sleeptimer;
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
77 typedef UINT (WINAPI *GETRAWINPUTDATAFNC) (HRAWINPUT,UINT,LPVOID,PUINT,UINT);
78 typedef UINT (WINAPI *REGISTERRAWINPUTDEVICEFNC) (PCRAWINPUTDEVICE,UINT,UINT);
80 GETRAWINPUTDATAFNC dynGetRawInputData=NULL;
81 REGISTERRAWINPUTDEVICEFNC dynRegisterRawInputDevices=NULL;
86 void MILLISLEEP(ULONG a)
93 DWORD WINAPI commandthreadStart(void *arg)
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) {
112 bool InitApp(HINSTANCE hinst,int cmdshow);
114 HWND win_main;//global window handle
115 HWND win;//global child window handle
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)
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
127 int result = WSAStartup(MAKEWORD(2,2),&wsadat);
128 if (result!=NO_ERROR) {
129 ERROR_MSG("Initialising WinSocked: Error at WSAStartup()\n");
133 result= CoInitializeEx(NULL,COINIT_MULTITHREADED );//Initialize COM for DirectShow
135 ERROR_MSG("Initialising COM: Error at Coinitialize()\n");
142 // Init global vars ------------------------------------------------------------------------------------------------
145 remote = new RemoteWin();
148 timers = new Timers();
151 video = new VideoWin();
152 audio = new AudioWin();
153 boxstack = new BoxStack();
154 command = new Command();
156 sleeptimer = new Sleeptimer();
158 if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !boxstack || !command || !sleeptimer)
160 ERROR_MSG("Could not create objects. Memory problems?\n");
166 // Get logging module started --------------------------------------------------------------------------------------
168 if (!logger->init(Log::DEBUG, "vompwin.log", true))
170 ERROR_MSG("Could not initialise log object. Aborting.\n");
176 logger->log("Core", Log::INFO, "Starting up...");
180 // Init modules ----------------------------------------------------------------------------------------------------
183 success = remote->init("/dev/rawir");
186 logger->log("Core", Log::INFO, "Remote module initialised");
190 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
196 success = led->init(0);
199 logger->log("Core", Log::INFO, "LED module initialised");
203 logger->log("Core", Log::EMERG, "LED module failed to initialise");
209 success = mtd->init();
212 logger->log("Core", Log::INFO, "Mtd module initialised");
216 logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
222 success = timers->init();
225 logger->log("Core", Log::INFO, "Timers module initialised");
229 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
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");
240 success = video->init(videoFormat);
243 logger->log("Core", Log::INFO, "Video module initialised");
247 logger->log("Core", Log::EMERG, "Video module failed to initialise");
253 success = osd->init((void*)&win);
256 logger->log("Core", Log::INFO, "OSD module initialised");
260 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
266 success = audio->init(Audio::MPEG2_PES);
269 logger->log("Core", Log::INFO, "Audio module initialised");
273 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
279 success = vdr->init(3024);
282 logger->log("Core", Log::INFO, "VDR module initialised");
286 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
292 success = boxstack->init();
295 logger->log("Core", Log::INFO, "BoxStack module initialised");
299 logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
305 success = command->init();
308 logger->log("Core", Log::INFO, "Command module initialised");
312 logger->log("Core", Log::EMERG, "Command module failed to initialise");
318 // Other init ------------------------------------------------------------------------------------------------------
320 logger->log("Core", Log::NOTICE, "Startup successful");
322 // Run main loop ---------------------------------------------------------------------------------------------------
324 // Ok, all major device components and other bits are loaded and ready
325 lastmousemove=timeGetTime();
327 HANDLE commandthread;
328 commandthread= CreateThread(NULL, 0, commandthreadStart, NULL,0,
331 message.message=WM_NULL;
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) {
340 run=false; //TODO post exit to command Messages
345 ((OsdWin*)osd)->Render();
348 // When that returns quit ------------------------------------------------------------------------------------------
349 WaitForSingleObject(commandthread,INFINITE);
352 if (user32dll) FreeModule(user32dll);
357 bool TranslateMousePosition(POINT *pos) {
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());
373 void CalculateWindowSize(RECT * size,ULONG size_mode) {
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;
385 adjwidth=adjheight=0;
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
399 } else if (tvsize==Video::ASPECT4X3) {
400 if (aspect==Video::ASPECT16X9) {
401 if (mode!=Video::NORMAL) {
406 } else if (aspect==Video::ASPECT4X3) {
410 if (!wnd_fullscreen) {
413 case WMSZ_BOTTOMRIGHT:
416 width=(ULONG)(((double)height)*aspectrt);
417 size->right=size->left+width+adjwidth;
419 case WMSZ_BOTTOMLEFT:
421 width=(ULONG)(((double)height)*aspectrt);
422 size->left=size->right-width-adjwidth;
426 height=(ULONG)(((double)width)/aspectrt);
427 size->bottom=size->top+height+adjheight;
430 MoveWindow(win,0,0,width,height,TRUE);
432 RECT newrect={0,0,width,height};
434 if ((ULONG)(((double)height)*aspectrt)>width) {
435 newlength=(ULONG)(((double)width)/aspectrt);
436 newrect.top+=(height-newlength)/2;
437 newrect.bottom-=(height-newlength);
439 newlength=(ULONG)(((double)height)*aspectrt);
440 newrect.left+=(width-newlength)/2;
441 newrect.right-=(width-newlength);
443 MoveWindow(win,newrect.left,newrect.top,newrect.right,newrect.bottom,TRUE);
448 void AdjustWindow() {
449 if (!wnd_fullscreen) {
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);
457 GetWindowRect(win_main,&winrect);
458 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
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);
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);
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);
485 moninfo.cbSize=sizeof(moninfo);
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);
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);
504 void CursorUpdate() {
506 GetCursorPos(&cursorpos);
508 asswind=WindowFromPoint(cursorpos);
509 if (asswind!=win_main && asswind!=win) {
510 return ; //not our responsibility
512 if ((timeGetTime()-lastmousemove)<4000 || cmenu) {
513 SetCursor(LoadCursor(NULL,IDC_ARROW));
519 bool ContextMenu(HWND wind,int x,int y) {
522 ScreenToClient(wind,&p);
523 GetClientRect(wind,&clientrect);
524 if (!PtInRect(&clientrect,p)) return false;
525 ClientToScreen(wind,&p);
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);
533 CheckMenuItem(popup,VOMP_FULL_SCREEN,MF_BYCOMMAND|MF_UNCHECKED);
536 CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_CHECKED);
538 CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_UNCHECKED);
541 TrackPopupMenu(popup,TPM_RIGHTBUTTON|TPM_LEFTALIGN,x,y,0,wind, NULL);
549 LONG FAR PASCAL WindowProc(HWND wind, UINT msg, WPARAM wparam, LPARAM lparam)
555 logger->log("Core", Log::NOTICE, "Window closed, shutting down...");
557 ((RemoteWin*)Remote::getInstance())->SendPower();
561 CalculateWindowSize((RECT*) lparam,wparam);
565 int width = LOWORD(lparam);
566 int height = HIWORD(lparam);
568 if (wparam == SIZE_MAXIMIZED) {
571 } else if (wparam == SIZE_MINIMIZED) {
580 if (GetUpdateRect(wind, &r, FALSE)) {
581 BeginPaint(wind, &ps);
582 //Call Painting Mechanism
587 if (((RemoteWin*)remote)->ReceiveButtonVK(wparam)) {
588 return 0L; //We process that Key
590 return DefWindowProc(wind, msg, wparam, lparam);
595 if (((RemoteWin*)remote)->ReceiveButtonCH(wparam)) {
596 return 0L; //We process that Key
598 return DefWindowProc(wind, msg, wparam, lparam);
603 if (((RemoteWin*)remote)->ReceiveButtonAP(GET_APPCOMMAND_LPARAM(lparam))){
604 return TRUE; //yes we process that message
606 return DefWindowProc(wind, msg, wparam, lparam);
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));
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)){
623 return 0; //yes we process that message
628 return DefWindowProc(wind, msg, wparam, lparam);
633 if (LOWORD(wparam)==VOMP_FULL_SCREEN) {
637 if (LOWORD(wparam)==VOMP_TOPMOST) {
641 if (((RemoteWin*)remote)->ReceiveButtonAP(LOWORD(wparam))){
642 return 0; //yes we process that message
644 return DefWindowProc(wind, msg, wparam, lparam);
649 if (((HANDLE)wparam)==win) {
653 return DefWindowProc(wind, msg, wparam, lparam);
657 if (wparam==SC_MAXIMIZE) {
660 } else if (wparam==SC_SCREENSAVE || wparam==SC_MONITORPOWER) {
663 return DefWindowProc(wind,msg,wparam, lparam);
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;
677 mousemes->to=BoxStack::getInstance();
678 mousemes->parameter=(mpos.x & 0xFFFF)<< 16| (mpos.y & 0xFFFF);
680 //command->postMessageFromOuterSpace(mousemes);
681 command->postMessageIfNotBusy(mousemes);
685 //return DefWindowProc(wind,msg,wparam, lparam);
689 if (wparam==VOMP_CURSORUPDATE) {
693 return DefWindowProc(wind, msg, wparam, lparam);
697 if (!ContextMenu(wind,GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam))) {
698 return DefWindowProc(wind, msg, wparam, lparam);
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;
710 mousemes->to=BoxStack::getInstance();
711 mousemes->parameter=(mpos.x & 0xFFFF)<< 16| (mpos.y & 0xFFFF);
713 command->postMessageFromOuterSpace(mousemes);
717 return DefWindowProc(wind, msg, wparam, lparam);
723 bool InitApp(HINSTANCE hinst,int cmdshow) {
727 wcs.style = CS_HREDRAW | CS_VREDRAW;
728 wcs.lpfnWndProc = WindowProc;
730 wcs.cbWndExtra = sizeof(DWORD);
731 wcs.hInstance = hinst;
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))
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);
748 ShowWindow(win_main,SW_SHOWNORMAL);
749 UpdateWindow(win_main);
750 /* in order to handle letterboxing we use a child window */
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))
765 win=CreateWindow("vomp_playback","Vomp Playback Window",WS_VISIBLE | WS_CHILD |WS_CLIPCHILDREN,
766 0,0,768,576,win_main,NULL,hinst,NULL);
769 ShowWindow(win,SW_SHOWNORMAL);
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);
800 // -------------------------------------------------------------------------------------------------------------------
802 void shutdown(int code)
806 boxstack->shutdown();
808 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
811 if (command) // shut down command here in case views have posted messages
815 logger->log("Core", Log::NOTICE, "Command module shut down");
822 logger->log("Core", Log::NOTICE, "VDR module shut down");
829 logger->log("Core", Log::NOTICE, "OSD module shut down");
836 logger->log("Core", Log::NOTICE, "Audio module shut down");
843 logger->log("Core", Log::NOTICE, "Video module shut down");
850 logger->log("Core", Log::NOTICE, "Timers module shut down");
857 logger->log("Core", Log::NOTICE, "MTD module shut down");
864 logger->log("Core", Log::NOTICE, "LED module shut down");
871 logger->log("Core", Log::NOTICE, "Remote module shut down");
876 logger->log("Core", Log::NOTICE, "WOL module shut down");
881 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
888 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
894 // -------------------------------------------------------------------------------------------------------------------
896 ULLONG ntohll(ULLONG a)
901 ULLONG htonll(ULLONG a)
903 return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32)
904 |(ULONG)htonl(((ULONG) (a >> 32))));