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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #define _WIN32_WINNT 0x501
30 #include "vompreswin.h"
34 #include "remotewin.h"
45 void sighandler(int signalReceived);
46 void shutdown(int code);
48 // Global variables --------------------------------------------------------------------------------------------------
61 bool wnd_fullscreen=false;
62 bool wnd_topmost=false;
63 RECT wnd_fs_rect={20,20,768+20,576+20};
64 RECT wnd_fs_rect_client={0,0,768,576};
65 //OSVERSIONINFO windows_ver; //attempt to distigsh windows versions
71 typedef UINT (WINAPI *GETRAWINPUTDATAFNC) (HRAWINPUT,UINT,LPVOID,PUINT,UINT);
72 typedef UINT (WINAPI *REGISTERRAWINPUTDEVICEFNC) (PCRAWINPUTDEVICE,UINT,UINT);
74 GETRAWINPUTDATAFNC dynGetRawInputData=NULL;
75 REGISTERRAWINPUTDEVICEFNC dynRegisterRawInputDevices=NULL;
80 void MILLISLEEP(ULONG a)
87 DWORD WINAPI commandthreadStart(void *arg)
93 void LoadRemoteFunctions() {
94 user32dll=LoadLibrary("user32.dll");
95 if (user32dll!=NULL) {
96 dynGetRawInputData=(GETRAWINPUTDATAFNC)GetProcAddress(user32dll,"GetRawInputData");
97 if (dynGetRawInputData!=NULL) {
98 dynRegisterRawInputDevices=(REGISTERRAWINPUTDEVICEFNC)GetProcAddress(user32dll,"RegisterRawInputDevices");
99 if (dynRegisterRawInputDevices!=NULL) {
106 bool InitApp(HINSTANCE hinst,int cmdshow);
108 HWND win_main;//global window handle
109 HWND win;//global child window handle
112 #define ERROR_MSG(str) MessageBox(win_main,str,"Error!",MB_OK|MB_ICONWARNING)
113 INT WINAPI WinMain( HINSTANCE hinst , HINSTANCE previnst, LPSTR cmdline, int cmdshow)
116 //On Windows we have to init a window, we use DXUT
117 LoadRemoteFunctions();
118 if (!InitApp(hinst,cmdshow)) return false;
119 //Starting Network support
121 int result = WSAStartup(MAKEWORD(2,2),&wsadat);
122 if (result!=NO_ERROR) {
123 ERROR_MSG("Initialising WinSocked: Error at WSAStartup()\n");
127 result= CoInitializeEx(NULL,COINIT_MULTITHREADED );//Initialize COM for DirectShow
129 ERROR_MSG("Initialising COM: Error at Coinitialize()\n");
136 // Init global vars ------------------------------------------------------------------------------------------------
139 remote = new RemoteWin();
142 timers = new Timers();
145 video = new VideoWin();
146 audio = new AudioWin();
147 viewman = new ViewMan();
148 command = new Command();
150 if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !viewman || !command)
152 ERROR_MSG("Could not create objects. Memory problems?\n");
158 // Get logging module started --------------------------------------------------------------------------------------
160 if (!logger->init(Log::DEBUG, "vompwin.log", true))
162 ERROR_MSG("Could not initialise log object. Aborting.\n");
168 logger->log("Core", Log::INFO, "Starting up...");
172 // Init modules ----------------------------------------------------------------------------------------------------
175 success = remote->init("/dev/rawir");
178 logger->log("Core", Log::INFO, "Remote module initialised");
182 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
188 success = led->init(0);
191 logger->log("Core", Log::INFO, "LED module initialised");
195 logger->log("Core", Log::EMERG, "LED module failed to initialise");
201 success = mtd->init();
204 logger->log("Core", Log::INFO, "Mtd module initialised");
208 logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
214 success = timers->init();
217 logger->log("Core", Log::INFO, "Timers module initialised");
221 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
227 UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
228 if (videoFormat == Video::PAL) logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
229 else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
230 else logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
232 success = video->init(videoFormat);
235 logger->log("Core", Log::INFO, "Video module initialised");
239 logger->log("Core", Log::EMERG, "Video module failed to initialise");
245 success = osd->init((void*)&win);
248 logger->log("Core", Log::INFO, "OSD module initialised");
252 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
258 success = audio->init(Audio::MPEG2_PES);
261 logger->log("Core", Log::INFO, "Audio module initialised");
265 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
271 success = vdr->init(3024);
274 logger->log("Core", Log::INFO, "VDR module initialised");
278 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
284 success = viewman->init();
287 logger->log("Core", Log::INFO, "ViewMan module initialised");
291 logger->log("Core", Log::EMERG, "ViewMan module failed to initialise");
297 success = command->init();
300 logger->log("Core", Log::INFO, "Command module initialised");
304 logger->log("Core", Log::EMERG, "Command module failed to initialise");
310 // Other init ------------------------------------------------------------------------------------------------------
312 logger->log("Core", Log::NOTICE, "Startup successful");
314 // Run main loop ---------------------------------------------------------------------------------------------------
316 // Ok, all major device components and other bits are loaded and ready
317 lastmousemove=timeGetTime();
319 HANDLE commandthread;
320 commandthread= CreateThread(NULL, 0, commandthreadStart, NULL,0,
323 message.message=WM_NULL;
325 while(run && WaitForSingleObject(commandthread,0)==WAIT_TIMEOUT) {
326 if (PeekMessage(&message, NULL, 0,0,PM_REMOVE)!=0) {
327 if (TranslateAccelerator(win_main,acc,&message)==NULL) {
328 TranslateMessage(&message);
329 DispatchMessage(&message);
330 switch (message.message) {
332 run=false; //TODO post exit to command Messages
337 ((OsdWin*)osd)->Render();
340 // When that returns quit ------------------------------------------------------------------------------------------
341 WaitForSingleObject(commandthread,INFINITE);
344 if (user32dll) FreeModule(user32dll);
349 bool TranslateMousePosition(POINT *pos) {
352 ScreenToClient(win,pos);
353 GetClientRect(win,&clientrect);
354 if (!PtInRect(&clientrect,*pos)) return false;//Don't pass it further
355 pos->x=((double)pos->x)/((double) (clientrect.right-clientrect.left))
356 *((double)Video::getInstance()->getScreenWidth());
357 pos->y=((double)pos->y)/((double) (clientrect.bottom-clientrect.top))
358 *((double)Video::getInstance()->getScreenHeight());
365 void CalculateWindowSize(RECT * size,ULONG size_mode) {
368 DWORD adjheight,adjwidth;
369 if (!wnd_fullscreen) {
370 DWORD flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
371 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
372 RECT wnted={50,50,150,150};
373 AdjustWindowRect(&wnted,flags ,false);
374 adjwidth=-wnted.left+wnted.right-100;
375 adjheight=-wnted.top+wnted.bottom-100;
377 adjwidth=adjheight=0;
379 width=size->right-size->left-adjwidth;
380 height=size->bottom-size->top-adjheight;
381 UCHAR mode=video->getMode();
382 UCHAR aspect=((VideoWin*)video)->getAspectRatio();
383 UCHAR tvsize=((VideoWin*)video)->getPseudoTVsize();
384 double aspectrt=4./3.;
385 if (tvsize==Video::ASPECT16X9) {
386 if (aspect==Video::ASPECT16X9) {
387 aspectrt=4./3.; //looks strange, but it is a 16:9 tv
388 } else if (aspect==Video::ASPECT4X3) {
389 aspectrt=4./3./(16./9.)*(4./3.); //I hope this is correct
391 } else if (tvsize==Video::ASPECT4X3) {
392 if (aspect==Video::ASPECT16X9) {
393 if (mode!=Video::NORMAL) {
398 } else if (aspect==Video::ASPECT4X3) {
402 if (!wnd_fullscreen) {
405 case WMSZ_BOTTOMRIGHT:
408 width=(ULONG)(((double)height)*aspectrt);
409 size->right=size->left+width+adjwidth;
411 case WMSZ_BOTTOMLEFT:
413 width=(ULONG)(((double)height)*aspectrt);
414 size->left=size->right-width-adjwidth;
418 height=(ULONG)(((double)width)/aspectrt);
419 size->bottom=size->top+height+adjheight;
422 MoveWindow(win,0,0,width,height,TRUE);
424 RECT newrect={0,0,width,height};
426 if ((ULONG)(((double)height)*aspectrt)>width) {
427 newlength=(ULONG)(((double)width)/aspectrt);
428 newrect.top+=(height-newlength)/2;
429 newrect.bottom-=(height-newlength);
431 newlength=(ULONG)(((double)height)*aspectrt);
432 newrect.left+=(width-newlength)/2;
433 newrect.right-=(width-newlength);
435 MoveWindow(win,newrect.left,newrect.top,newrect.right,newrect.bottom,TRUE);
440 void AdjustWindow() {
441 if (!wnd_fullscreen) {
443 GetWindowRect(win_main,&winrect);
444 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
445 MoveWindow(win_main,winrect.left,
446 winrect.top,winrect.right-winrect.left,winrect.bottom-winrect.top,true);
449 GetWindowRect(win_main,&winrect);
450 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
455 void ToggleFullscreen() {
456 if (wnd_fullscreen) {
457 wnd_fullscreen=false;
458 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
459 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX);
460 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
462 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_TOP,wnd_fs_rect.left,wnd_fs_rect.top,
463 wnd_fs_rect.right-wnd_fs_rect.left,
464 wnd_fs_rect.bottom-wnd_fs_rect.top,
465 SWP_DRAWFRAME | SWP_FRAMECHANGED);
466 MoveWindow(win,wnd_fs_rect_client.left,wnd_fs_rect_client.top,
467 wnd_fs_rect_client.right-wnd_fs_rect_client.left,
468 wnd_fs_rect_client.bottom-wnd_fs_rect_client.top,TRUE);
471 GetWindowRect(win_main,&wnd_fs_rect);
472 GetWindowRect(win,&wnd_fs_rect_client);
473 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP );
474 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
475 HMONITOR monitor=MonitorFromWindow(win_main,MONITOR_DEFAULTTONEAREST);
477 moninfo.cbSize=sizeof(moninfo);
479 if (!GetMonitorInfo(monitor,&moninfo)) return ;
480 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_TOP,moninfo.rcMonitor.left,moninfo.rcMonitor.top,
481 moninfo.rcMonitor.right,moninfo.rcMonitor.bottom,SWP_FRAMECHANGED);
490 void ToggleTopmost() {
491 wnd_topmost=!wnd_topmost;
492 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_NOTOPMOST,0,0,
493 0,0,SWP_NOMOVE | SWP_NOSIZE);
496 void CursorUpdate() {
498 GetCursorPos(&cursorpos);
500 asswind=WindowFromPoint(cursorpos);
501 if (asswind!=win_main && asswind!=win) {
502 return ; //not our responsibility
504 if ((timeGetTime()-lastmousemove)<4000 || cmenu) {
505 SetCursor(LoadCursor(NULL,IDC_ARROW));
511 bool ContextMenu(HWND wind,int x,int y) {
514 ScreenToClient(wind,&p);
515 GetClientRect(wind,&clientrect);
516 if (!PtInRect(&clientrect,p)) return false;
517 ClientToScreen(wind,&p);
520 menu=LoadMenu(hinstance,MAKEINTRESOURCE(VOMPMENU));
521 popup=GetSubMenu(menu,0);
522 if (wnd_fullscreen) {
523 CheckMenuItem(popup,VOMP_FULL_SCREEN,MF_BYCOMMAND|MF_CHECKED);
525 CheckMenuItem(popup,VOMP_FULL_SCREEN,MF_BYCOMMAND|MF_UNCHECKED);
528 CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_CHECKED);
530 CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_UNCHECKED);
533 TrackPopupMenu(popup,TPM_RIGHTBUTTON|TPM_LEFTALIGN,x,y,0,wind, NULL);
541 LONG FAR PASCAL WindowProc(HWND wind, UINT msg, WPARAM wparam, LPARAM lparam)
547 logger->log("Core", Log::NOTICE, "Window closed, shutting down...");
549 ((RemoteWin*)Remote::getInstance())->SendPower();
553 CalculateWindowSize((RECT*) lparam,wparam);
557 int width = LOWORD(lparam);
558 int height = HIWORD(lparam);
565 if (GetUpdateRect(wind, &r, FALSE)) {
566 BeginPaint(wind, &ps);
567 //Call Painting Mechanism
572 if (((RemoteWin*)remote)->ReceiveButtonVK(wparam)) {
573 return 0L; //We process that Key
575 return DefWindowProc(wind, msg, wparam, lparam);
580 if (((RemoteWin*)remote)->ReceiveButtonCH(wparam)) {
581 return 0L; //We process that Key
583 return DefWindowProc(wind, msg, wparam, lparam);
588 if (((RemoteWin*)remote)->ReceiveButtonAP(GET_APPCOMMAND_LPARAM(lparam))){
589 return TRUE; //yes we process that message
591 return DefWindowProc(wind, msg, wparam, lparam);
600 dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,NULL,&risize,sizeof(RAWINPUTHEADER));
601 lpit=(LPRAWINPUT)malloc(risize);
602 dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,lpit,&risize,sizeof(RAWINPUTHEADER));
604 if (lpit->header.dwType==RIM_TYPEHID && lpit->data.hid.dwSizeHid>=2) {
605 DWORD button=lpit->data.hid.bRawData[1] | (lpit->data.hid.bRawData[0]<< 8);
606 if (((RemoteWin*)remote)->ReceiveButtonRI(button)){
608 return 0; //yes we process that message
613 return DefWindowProc(wind, msg, wparam, lparam);
618 if (LOWORD(wparam)==VOMP_FULL_SCREEN) {
622 if (LOWORD(wparam)==VOMP_TOPMOST) {
626 if (((RemoteWin*)remote)->ReceiveButtonAP(LOWORD(wparam))){
627 return 0; //yes we process that message
629 return DefWindowProc(wind, msg, wparam, lparam);
634 if (((HANDLE)wparam)==win) {
638 return DefWindowProc(wind, msg, wparam, lparam);
642 if (wparam==SC_MAXIMIZE) {
645 } else if (wparam==SC_SCREENSAVE || wparam==SC_MONITORPOWER) {
648 return DefWindowProc(wind,msg,wparam, lparam);
653 lastmousemove=timeGetTime();
654 SetCursor(LoadCursor(NULL,IDC_ARROW));
655 SetTimer(wind,VOMP_CURSORUPDATE,4500,NULL);
656 POINT mpos={GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam)};
657 ClientToScreen(wind,&mpos);
658 if (TranslateMousePosition(&mpos)) {
659 Message *mousemes=new Message();
660 mousemes->message=Message::MOUSE_MOVE;
662 mousemes->to=ViewMan::getInstance();
663 mousemes->parameter=(mpos.x & 0xFFFF)<< 16| (mpos.y & 0xFFFF);
665 //command->postMessageFromOuterSpace(mousemes);
666 command->postMessageIfNotBusy(mousemes);
670 //return DefWindowProc(wind,msg,wparam, lparam);
674 if (wparam==VOMP_CURSORUPDATE) {
678 return DefWindowProc(wind, msg, wparam, lparam);
682 if (!ContextMenu(wind,GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam))) {
683 return DefWindowProc(wind, msg, wparam, lparam);
688 case WM_LBUTTONDOWN:{
689 POINT mpos={GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam)};
690 ClientToScreen(wind,&mpos);
691 if (TranslateMousePosition(&mpos)) {
692 Message *mousemes=new Message();
693 mousemes->message=Message::MOUSE_LBDOWN;
695 mousemes->to=ViewMan::getInstance();
696 mousemes->parameter=(mpos.x & 0xFFFF)<< 16| (mpos.y & 0xFFFF);
698 command->postMessageFromOuterSpace(mousemes);
702 return DefWindowProc(wind, msg, wparam, lparam);
708 bool InitApp(HINSTANCE hinst,int cmdshow) {
712 wcs.style = CS_HREDRAW | CS_VREDRAW;
713 wcs.lpfnWndProc = WindowProc;
715 wcs.cbWndExtra = sizeof(DWORD);
716 wcs.hInstance = hinst;
719 wcs.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
720 wcs.lpszMenuName = NULL;
721 wcs.lpszClassName = "vomp";
722 acc=LoadAccelerators(hinst,MAKEINTRESOURCE(VOMPACCELERATOR));
723 if (!RegisterClass(&wcs))
725 flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
726 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
727 RECT wnted={50,50,768+50,576+50};
728 AdjustWindowRect(&wnted,flags ,false);
729 win_main=CreateWindow("vomp","VOMP on Windows",flags, CW_USEDEFAULT,CW_USEDEFAULT,
730 wnted.right-wnted.left,wnted.bottom-wnted.top,NULL,NULL,hinst,NULL);
733 ShowWindow(win_main,SW_SHOWNORMAL);
734 UpdateWindow(win_main);
735 /* in order to handle letterboxing we use a child window */
737 child_wcs.style = CS_HREDRAW | CS_VREDRAW;
738 child_wcs.lpfnWndProc = WindowProc;
739 child_wcs.cbClsExtra = 0;
740 child_wcs.cbWndExtra = sizeof(DWORD);
741 child_wcs.hInstance = hinst;
742 child_wcs.hIcon = NULL;
743 child_wcs.hCursor = NULL;
744 child_wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
745 child_wcs.lpszMenuName = NULL;
746 child_wcs.lpszClassName = "vomp_playback";
747 if (!RegisterClass(&child_wcs))
750 win=CreateWindow("vomp_playback","Vomp Playback Window",WS_VISIBLE | WS_CHILD |WS_CLIPCHILDREN,
751 0,0,768,576,win_main,NULL,hinst,NULL);
754 ShowWindow(win,SW_SHOWNORMAL);
756 if (remotefnc) {//at least windows XP
757 /* We want to support MCE Remote controls*/
758 RAWINPUTDEVICE remote_control_data[4];
759 ZeroMemory(remote_control_data,sizeof(remote_control_data));
760 remote_control_data[0].usUsagePage=0xFFBC;
761 remote_control_data[0].usUsage=0x88;
762 remote_control_data[0].dwFlags=0;
763 remote_control_data[1].usUsagePage=0x0C;
764 remote_control_data[1].usUsage=0x80;
765 remote_control_data[1].dwFlags=0;
766 remote_control_data[2].usUsagePage=0x0C;
767 remote_control_data[2].usUsage=0x01;
768 remote_control_data[2].dwFlags=0;
769 remote_control_data[3].usUsagePage=0x01;
770 remote_control_data[3].usUsage=0x80;
771 remote_control_data[3].dwFlags=0;
772 if (dynRegisterRawInputDevices(remote_control_data,4,sizeof(remote_control_data[0]))!=TRUE) {
773 MessageBox(0,"Registering remote control failed!","Aborting!",0);
785 // -------------------------------------------------------------------------------------------------------------------
787 void shutdown(int code)
793 logger->log("Core", Log::NOTICE, "ViewMan module shut down");
796 if (command) // shut down command here in case views have posted messages
800 logger->log("Core", Log::NOTICE, "Command module shut down");
807 logger->log("Core", Log::NOTICE, "VDR module shut down");
814 logger->log("Core", Log::NOTICE, "OSD module shut down");
821 logger->log("Core", Log::NOTICE, "Audio module shut down");
828 logger->log("Core", Log::NOTICE, "Video module shut down");
835 logger->log("Core", Log::NOTICE, "Timers module shut down");
842 logger->log("Core", Log::NOTICE, "MTD module shut down");
849 logger->log("Core", Log::NOTICE, "LED module shut down");
856 logger->log("Core", Log::NOTICE, "Remote module shut down");
861 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
869 // -------------------------------------------------------------------------------------------------------------------
871 ULLONG ntohll(ULLONG a)
876 ULLONG htonll(ULLONG a)
878 return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32)
879 |(ULONG)htonl(((ULONG) (a >> 32))));