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
344 //Render, moved to OSD !
346 //((OsdWin*)osd)->Render();
349 // When that returns quit ------------------------------------------------------------------------------------------
350 WaitForSingleObject(commandthread,INFINITE);
353 if (user32dll) FreeModule(user32dll);
358 bool TranslateMousePosition(POINT *pos) {
361 ScreenToClient(win,pos);
362 GetClientRect(win,&clientrect);
363 if (!PtInRect(&clientrect,*pos)) return false;//Don't pass it further
364 pos->x=((double)pos->x)/((double) (clientrect.right-clientrect.left))
365 *((double)Video::getInstance()->getScreenWidth());
366 pos->y=((double)pos->y)/((double) (clientrect.bottom-clientrect.top))
367 *((double)Video::getInstance()->getScreenHeight());
374 void CalculateWindowSize(RECT * size,ULONG size_mode) {
377 DWORD adjheight,adjwidth;
378 if (!wnd_fullscreen) {
379 DWORD flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
380 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
381 RECT wnted={50,50,150,150};
382 AdjustWindowRect(&wnted,flags ,false);
383 adjwidth=-wnted.left+wnted.right-100;
384 adjheight=-wnted.top+wnted.bottom-100;
386 adjwidth=adjheight=0;
388 width=size->right-size->left-adjwidth;
389 height=size->bottom-size->top-adjheight;
390 UCHAR mode=Video::PAL;
391 UCHAR aspect=Video::ASPECT4X3;
392 UCHAR tvsize=Video::ASPECT4X3;
394 mode=video->getMode();
395 aspect=((VideoWin*)video)->getAspectRatio();
396 tvsize=((VideoWin*)video)->getPseudoTVsize();
399 double aspectrt=4./3.;
400 if (tvsize==Video::ASPECT16X9) {
401 if (aspect==Video::ASPECT16X9) {
402 aspectrt=4./3.; //looks strange, but it is a 16:9 tv
403 } else if (aspect==Video::ASPECT4X3) {
404 aspectrt=4./3./(16./9.)*(4./3.); //I hope this is correct
406 } else if (tvsize==Video::ASPECT4X3) {
407 if (aspect==Video::ASPECT16X9) {
408 if (mode!=Video::NORMAL) {
413 } else if (aspect==Video::ASPECT4X3) {
417 if (!wnd_fullscreen) {
420 case WMSZ_BOTTOMRIGHT:
423 width=(ULONG)(((double)height)*aspectrt);
424 size->right=size->left+width+adjwidth;
426 case WMSZ_BOTTOMLEFT:
428 width=(ULONG)(((double)height)*aspectrt);
429 size->left=size->right-width-adjwidth;
433 height=(ULONG)(((double)width)/aspectrt);
434 size->bottom=size->top+height+adjheight;
437 MoveWindow(win,0,0,width,height,TRUE);
439 RECT newrect={0,0,width,height};
441 if ((ULONG)(((double)height)*aspectrt)>width) {
442 newlength=(ULONG)(((double)width)/aspectrt);
443 newrect.top+=(height-newlength)/2;
444 newrect.bottom-=(height-newlength);
446 newlength=(ULONG)(((double)height)*aspectrt);
447 newrect.left+=(width-newlength)/2;
448 newrect.right-=(width-newlength);
450 MoveWindow(win,newrect.left,newrect.top,newrect.right,newrect.bottom,TRUE);
455 void AdjustWindow() {
456 if (!wnd_fullscreen) {
458 GetWindowRect(win_main,&winrect);
459 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
460 MoveWindow(win_main,winrect.left,
461 winrect.top,winrect.right-winrect.left,winrect.bottom-winrect.top,true);
464 GetWindowRect(win_main,&winrect);
465 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
470 void ToggleFullscreen() {
471 if (wnd_fullscreen) {
472 wnd_fullscreen=false;
473 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
474 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX);
475 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
477 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_TOP,wnd_fs_rect.left,wnd_fs_rect.top,
478 wnd_fs_rect.right-wnd_fs_rect.left,
479 wnd_fs_rect.bottom-wnd_fs_rect.top,
480 SWP_DRAWFRAME | SWP_FRAMECHANGED);
481 MoveWindow(win,wnd_fs_rect_client.left,wnd_fs_rect_client.top,
482 wnd_fs_rect_client.right-wnd_fs_rect_client.left,
483 wnd_fs_rect_client.bottom-wnd_fs_rect_client.top,TRUE);
486 GetWindowRect(win_main,&wnd_fs_rect);
487 GetWindowRect(win,&wnd_fs_rect_client);
488 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP );
489 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
490 HMONITOR monitor=MonitorFromWindow(win_main,MONITOR_DEFAULTTONEAREST);
492 moninfo.cbSize=sizeof(moninfo);
494 if (!GetMonitorInfo(monitor,&moninfo)) return ;
495 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_TOP,moninfo.rcMonitor.left,moninfo.rcMonitor.top,
496 moninfo.rcMonitor.right,moninfo.rcMonitor.bottom,SWP_FRAMECHANGED);
505 void ToggleTopmost() {
506 wnd_topmost=!wnd_topmost;
507 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_NOTOPMOST,0,0,
508 0,0,SWP_NOMOVE | SWP_NOSIZE);
511 void CursorUpdate() {
513 GetCursorPos(&cursorpos);
515 asswind=WindowFromPoint(cursorpos);
516 if (asswind!=win_main && asswind!=win) {
517 return ; //not our responsibility
519 if ((timeGetTime()-lastmousemove)<4000 || cmenu) {
520 SetCursor(LoadCursor(NULL,IDC_ARROW));
526 bool ContextMenu(HWND wind,int x,int y) {
529 ScreenToClient(wind,&p);
530 GetClientRect(wind,&clientrect);
531 if (!PtInRect(&clientrect,p)) return false;
532 ClientToScreen(wind,&p);
535 menu=LoadMenu(hinstance,MAKEINTRESOURCE(VOMPMENU));
536 popup=GetSubMenu(menu,0);
537 if (wnd_fullscreen) {
538 CheckMenuItem(popup,VOMP_FULL_SCREEN,MF_BYCOMMAND|MF_CHECKED);
540 CheckMenuItem(popup,VOMP_FULL_SCREEN,MF_BYCOMMAND|MF_UNCHECKED);
543 CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_CHECKED);
545 CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_UNCHECKED);
548 TrackPopupMenu(popup,TPM_RIGHTBUTTON|TPM_LEFTALIGN,x,y,0,wind, NULL);
556 LONG FAR PASCAL WindowProc(HWND wind, UINT msg, WPARAM wparam, LPARAM lparam)
562 logger->log("Core", Log::NOTICE, "Window closed, shutting down...");
564 ((RemoteWin*)Remote::getInstance())->SendPower();
568 CalculateWindowSize((RECT*) lparam,wparam);
572 int width = LOWORD(lparam);
573 int height = HIWORD(lparam);
575 if (wparam == SIZE_MAXIMIZED) {
578 } /*else if (wparam == SIZE_MINIMIZED) {
588 if (GetUpdateRect(wind, &r, FALSE)) {
589 BeginPaint(wind, &ps);
590 //Call Painting Mechanism
595 if (((RemoteWin*)remote)->ReceiveButtonVK(wparam)) {
596 return 0L; //We process that Key
598 return DefWindowProc(wind, msg, wparam, lparam);
603 if (((RemoteWin*)remote)->ReceiveButtonCH(wparam)) {
604 return 0L; //We process that Key
606 return DefWindowProc(wind, msg, wparam, lparam);
611 if (((RemoteWin*)remote)->ReceiveButtonAP(GET_APPCOMMAND_LPARAM(lparam))){
612 return TRUE; //yes we process that message
614 return DefWindowProc(wind, msg, wparam, lparam);
623 dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,NULL,&risize,sizeof(RAWINPUTHEADER));
624 lpit=(LPRAWINPUT)malloc(risize);
625 dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,lpit,&risize,sizeof(RAWINPUTHEADER));
627 if (lpit->header.dwType==RIM_TYPEHID && lpit->data.hid.dwSizeHid>=2) {
628 DWORD button=lpit->data.hid.bRawData[1] | (lpit->data.hid.bRawData[0]<< 8);
629 if (((RemoteWin*)remote)->ReceiveButtonRI(button)){
631 return 0; //yes we process that message
636 return DefWindowProc(wind, msg, wparam, lparam);
641 if (LOWORD(wparam)==VOMP_FULL_SCREEN) {
645 if (LOWORD(wparam)==VOMP_TOPMOST) {
649 if (((RemoteWin*)remote)->ReceiveButtonAP(LOWORD(wparam))){
650 return 0; //yes we process that message
652 return DefWindowProc(wind, msg, wparam, lparam);
657 if (((HANDLE)wparam)==win) {
661 return DefWindowProc(wind, msg, wparam, lparam);
665 if (wparam==SC_MAXIMIZE) {
668 } else if (wparam==SC_SCREENSAVE || wparam==SC_MONITORPOWER) {
671 return DefWindowProc(wind,msg,wparam, lparam);
676 lastmousemove=timeGetTime();
677 SetCursor(LoadCursor(NULL,IDC_ARROW));
678 SetTimer(wind,VOMP_CURSORUPDATE,4500,NULL);
679 POINT mpos={GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam)};
680 ClientToScreen(wind,&mpos);
681 if (TranslateMousePosition(&mpos)) {
682 Message *mousemes=new Message();
683 mousemes->message=Message::MOUSE_MOVE;
685 mousemes->to=BoxStack::getInstance();
686 mousemes->parameter=(mpos.x & 0xFFFF)<< 16| (mpos.y & 0xFFFF);
688 //command->postMessageFromOuterSpace(mousemes);
689 command->postMessageIfNotBusy(mousemes);
693 //return DefWindowProc(wind,msg,wparam, lparam);
697 if (wparam==VOMP_CURSORUPDATE) {
701 return DefWindowProc(wind, msg, wparam, lparam);
705 if (!ContextMenu(wind,GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam))) {
706 return DefWindowProc(wind, msg, wparam, lparam);
711 case WM_LBUTTONDOWN:{
712 POINT mpos={GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam)};
713 ClientToScreen(wind,&mpos);
714 if (TranslateMousePosition(&mpos)) {
715 Message *mousemes=new Message();
716 mousemes->message=Message::MOUSE_LBDOWN;
718 mousemes->to=BoxStack::getInstance();
719 mousemes->parameter=(mpos.x & 0xFFFF)<< 16| (mpos.y & 0xFFFF);
721 command->postMessageFromOuterSpace(mousemes);
725 return DefWindowProc(wind, msg, wparam, lparam);
731 bool InitApp(HINSTANCE hinst,int cmdshow) {
735 wcs.style = CS_HREDRAW | CS_VREDRAW;
736 wcs.lpfnWndProc = WindowProc;
738 wcs.cbWndExtra = sizeof(DWORD);
739 wcs.hInstance = hinst;
742 wcs.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
743 wcs.lpszMenuName = NULL;
744 wcs.lpszClassName = "vomp";
745 acc=LoadAccelerators(hinst,MAKEINTRESOURCE(VOMPACCELERATOR));
746 if (!RegisterClass(&wcs))
748 flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
749 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
750 RECT wnted={50,50,768+50,576+50};
751 AdjustWindowRect(&wnted,flags ,false);
752 win_main=CreateWindow("vomp","VOMP on Windows",flags, CW_USEDEFAULT,CW_USEDEFAULT,
753 wnted.right-wnted.left,wnted.bottom-wnted.top,NULL,NULL,hinst,NULL);
756 ShowWindow(win_main,SW_SHOWNORMAL);
757 UpdateWindow(win_main);
758 /* in order to handle letterboxing we use a child window */
760 child_wcs.style = CS_HREDRAW | CS_VREDRAW;
761 child_wcs.lpfnWndProc = WindowProc;
762 child_wcs.cbClsExtra = 0;
763 child_wcs.cbWndExtra = sizeof(DWORD);
764 child_wcs.hInstance = hinst;
765 child_wcs.hIcon = NULL;
766 child_wcs.hCursor = NULL;
767 child_wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
768 child_wcs.lpszMenuName = NULL;
769 child_wcs.lpszClassName = "vomp_playback";
770 if (!RegisterClass(&child_wcs))
773 win=CreateWindow("vomp_playback","Vomp Playback Window",WS_VISIBLE | WS_CHILD |WS_CLIPCHILDREN,
774 0,0,768,576,win_main,NULL,hinst,NULL);
777 ShowWindow(win,SW_SHOWNORMAL);
779 if (remotefnc) {//at least windows XP
780 /* We want to support MCE Remote controls*/
781 RAWINPUTDEVICE remote_control_data[4];
782 ZeroMemory(remote_control_data,sizeof(remote_control_data));
783 remote_control_data[0].usUsagePage=0xFFBC;
784 remote_control_data[0].usUsage=0x88;
785 remote_control_data[0].dwFlags=0;
786 remote_control_data[1].usUsagePage=0x0C;
787 remote_control_data[1].usUsage=0x80;
788 remote_control_data[1].dwFlags=0;
789 remote_control_data[2].usUsagePage=0x0C;
790 remote_control_data[2].usUsage=0x01;
791 remote_control_data[2].dwFlags=0;
792 remote_control_data[3].usUsagePage=0x01;
793 remote_control_data[3].usUsage=0x80;
794 remote_control_data[3].dwFlags=0;
795 if (dynRegisterRawInputDevices(remote_control_data,4,sizeof(remote_control_data[0]))!=TRUE) {
796 MessageBox(0,"Registering remote control failed!","Aborting!",0);
808 // -------------------------------------------------------------------------------------------------------------------
810 void shutdown(int code)
814 boxstack->shutdown();
816 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
819 if (command) // shut down command here in case views have posted messages
823 logger->log("Core", Log::NOTICE, "Command module shut down");
830 logger->log("Core", Log::NOTICE, "VDR module shut down");
837 logger->log("Core", Log::NOTICE, "OSD module shut down");
844 logger->log("Core", Log::NOTICE, "Audio module shut down");
851 logger->log("Core", Log::NOTICE, "Video module shut down");
858 logger->log("Core", Log::NOTICE, "Timers module shut down");
865 logger->log("Core", Log::NOTICE, "MTD module shut down");
872 logger->log("Core", Log::NOTICE, "LED module shut down");
879 logger->log("Core", Log::NOTICE, "Remote module shut down");
884 logger->log("Core", Log::NOTICE, "WOL module shut down");
889 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
896 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
902 // -------------------------------------------------------------------------------------------------------------------
904 ULLONG ntohll(ULLONG a)
909 ULLONG htonll(ULLONG a)
911 return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32)
912 |(ULONG)htonl(((ULONG) (a >> 32))));