2 Copyright 2004-2005 Chris Tallon
3 Copyright 2012 Marten Richter
5 This file is part of VOMP.
7 VOMP is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 VOMP is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with VOMP. If not, see <https://www.gnu.org/licenses/>.
27 #define _WIN32_WINNT 0x501
31 #include "vompreswin.h"
41 #include "windowsosd.h"
43 #include "osdwinpixel.h"
45 #include "osdwinvector.h"
49 #include "messagequeue.h"
51 void sighandler(int signalReceived);
52 void shutdown(int code);
54 // Global variables --------------------------------------------------------------------------------------------------
65 std::string commandLineServer; // NCONFIG
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;
85 MessageQueue* messageQueue;
88 void MILLISLEEP(ULONG a)
93 int getClockRealTime(struct timespec *tp){
97 GetSystemTime(&systime);
98 SystemTimeToFileTime(&systime, (FILETIME*)&filetime);
99 tp->tv_sec = (filetime - WINDOWS_TIME_BASE_OFFSET) / (10 * 1000 * 1000);
100 tp->tv_nsec = ((filetime - WINDOWS_TIME_BASE_OFFSET) % (10 * 1000 * 1000)) * 100;
104 DWORD WINAPI controlthreadStart(void *arg)
111 void LoadRemoteFunctions() {
112 user32dll=LoadLibrary("user32.dll");
113 if (user32dll!=NULL) {
114 dynGetRawInputData=(GETRAWINPUTDATAFNC)GetProcAddress(user32dll,"GetRawInputData");
115 if (dynGetRawInputData!=NULL) {
116 dynRegisterRawInputDevices=(REGISTERRAWINPUTDEVICEFNC)GetProcAddress(user32dll,"RegisterRawInputDevices");
117 if (dynRegisterRawInputDevices!=NULL) {
124 bool InitApp(HINSTANCE hinst,int cmdshow);
126 HWND win_main;//global window handle
127 HWND win;//global child window handle
130 #define ERROR_MSG(str) MessageBox(win_main,str,"Error!",MB_OK|MB_ICONWARNING)
131 INT WINAPI WinMain( HINSTANCE hinst , HINSTANCE previnst, LPSTR cmdline, int cmdshow)
134 //On Windows we have to init a window, we use DXUT
135 LoadRemoteFunctions();
136 if (!InitApp(hinst,cmdshow)) return false;
137 //Starting Network support
139 int result = WSAStartup(MAKEWORD(2,2),&wsadat);
140 if (result!=NO_ERROR) {
141 ERROR_MSG("Initialising WinSocked: Error at WSAStartup()\n");
145 result= CoInitializeEx(NULL,COINIT_MULTITHREADED );//Initialize COM for DirectShow
147 ERROR_MSG("Initialising COM: Error at Coinitialize()\n");
154 // Init global vars ------------------------------------------------------------------------------------------------
157 remote = new InputMan();
159 timers = new Timers();
160 osd = new Osd_TYPE();
161 video = new VideoWin();
162 audio = new AudioWin();
163 control = new Control();
165 if (!logger || !remote || !led || !osd || !video || !audio || !control)
167 ERROR_MSG("Could not create objects. Memory problems?\n");
173 messageQueue = control;
175 // Get logging module started --------------------------------------------------------------------------------------
177 if (!logger->init(Log::DEBUG, "vompwin.log", true))
179 ERROR_MSG("Could not initialise log object. Aborting.\n");
185 logger->log("Core", Log::INFO, "Starting up...");
189 // Init modules ----------------------------------------------------------------------------------------------------
192 success = remote->init();
195 logger->log("Core", Log::INFO, "Remote module initialised");
199 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
205 inputWin = remote->getInputWin();
207 success = led->init(0);
210 logger->log("Core", Log::INFO, "LED module initialised");
214 logger->log("Core", Log::EMERG, "LED module failed to initialise");
220 success = timers->init();
223 logger->log("Core", Log::INFO, "Timers module initialised");
227 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
233 UCHAR videoFormat = Video::PAL; // PALNTSC FIXME.
235 success = video->init(videoFormat);
238 logger->log("Core", Log::INFO, "Video module initialised");
242 logger->log("Core", Log::EMERG, "Video module failed to initialise");
248 dynamic_cast<WindowsOsd*>(osd)->setWindow(win);
249 success = osd->init();
252 logger->log("Core", Log::INFO, "OSD module initialised");
256 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
261 video->setDefaultAspect();
263 success = audio->init(Audio::MPEG2_PES);
266 logger->log("Core", Log::INFO, "Audio module initialised");
270 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
276 success = control->init();
279 logger->log("Core", Log::INFO, "Control module initialised");
283 logger->log("Core", Log::EMERG, "Control module failed to initialise");
289 // Other init ------------------------------------------------------------------------------------------------------
291 logger->log("Core", Log::NOTICE, "Startup successful");
293 // Run main loop ---------------------------------------------------------------------------------------------------
295 // Ok, all major device components and other bits are loaded and ready
296 lastmousemove=timeGetTime();
298 HANDLE controlthread;
299 controlthread= CreateThread(NULL, 0, controlthreadStart, NULL,0,
302 message.message=WM_NULL;
304 while(run && WaitForSingleObject(controlthread,0)==WAIT_TIMEOUT) {
305 if (PeekMessage(&message, NULL, 0,0,PM_REMOVE)!=0) {
306 if (TranslateAccelerator(win_main,acc,&message)==NULL) {
307 TranslateMessage(&message);
308 DispatchMessage(&message);
309 switch (message.message) {
311 run=false; //TODO post exit to control Messages
315 //Render, moved to OSD !
317 //((OsdWin*)osd)->Render();
320 // When that returns quit ------------------------------------------------------------------------------------------
321 WaitForSingleObject(controlthread,INFINITE);
324 if (user32dll) FreeModule(user32dll);
329 bool TranslateMousePosition(POINT *pos) {
332 ScreenToClient(win,pos);
333 GetClientRect(win,&clientrect);
334 if (!PtInRect(&clientrect,*pos)) return false;//Don't pass it further
335 pos->x=((double)pos->x)/((double) (clientrect.right-clientrect.left))
336 *((double)Video::getInstance()->getScreenWidth());
337 pos->y=((double)pos->y)/((double) (clientrect.bottom-clientrect.top))
338 *((double)Video::getInstance()->getScreenHeight());
345 void CalculateWindowSize(RECT * size,ULONG size_mode) {
348 DWORD adjheight,adjwidth;
349 if (!wnd_fullscreen) {
350 DWORD flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
351 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
352 RECT wnted={50,50,150,150};
353 AdjustWindowRect(&wnted,flags ,false);
354 adjwidth=-wnted.left+wnted.right-100;
355 adjheight=-wnted.top+wnted.bottom-100;
357 adjwidth=adjheight=0;
359 width=size->right-size->left-adjwidth;
360 height=size->bottom-size->top-adjheight;
361 UCHAR mode=Video::PAL;
362 UCHAR aspect=Video::ASPECT4X3;
363 UCHAR tvsize=Video::ASPECT4X3;
365 mode=video->getMode();
366 aspect=((VideoWin*)video)->getAspectRatio();
367 tvsize=((VideoWin*)video)->getPseudoTVsize();
370 double aspectrt=4./3.;
371 if (tvsize==Video::ASPECT16X9) {
372 if (aspect==Video::ASPECT16X9) {
373 aspectrt=4./3.; //looks strange, but it is a 16:9 tv
374 } else if (aspect==Video::ASPECT4X3) {
375 aspectrt=4./3./(16./9.)*(4./3.); //I hope this is correct
377 } else if (tvsize==Video::ASPECT4X3) {
378 if (aspect==Video::ASPECT16X9) {
379 if (mode!=Video::NORMAL) {
384 } else if (aspect==Video::ASPECT4X3) {
388 if (!wnd_fullscreen) {
391 case WMSZ_BOTTOMRIGHT:
394 width=(ULONG)(((double)height)*aspectrt);
395 size->right=size->left+width+adjwidth;
397 case WMSZ_BOTTOMLEFT:
399 width=(ULONG)(((double)height)*aspectrt);
400 size->left=size->right-width-adjwidth;
404 height=(ULONG)(((double)width)/aspectrt);
405 size->bottom=size->top+height+adjheight;
408 MoveWindow(win,0,0,width,height,TRUE);
410 RECT newrect={0,0,width,height};
412 if ((ULONG)(((double)height)*aspectrt)>width) {
413 newlength=(ULONG)(((double)width)/aspectrt);
414 newrect.top+=(height-newlength)/2;
415 newrect.bottom-=(height-newlength);
417 newlength=(ULONG)(((double)height)*aspectrt);
418 newrect.left+=(width-newlength)/2;
419 newrect.right-=(width-newlength);
421 MoveWindow(win,newrect.left,newrect.top,newrect.right,newrect.bottom,TRUE);
426 void AdjustWindow() {
427 if (!wnd_fullscreen) {
429 GetWindowRect(win_main,&winrect);
430 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
431 MoveWindow(win_main,winrect.left,
432 winrect.top,winrect.right-winrect.left,winrect.bottom-winrect.top,true);
435 GetWindowRect(win_main,&winrect);
436 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
441 void ToggleFullscreen() {
442 if (wnd_fullscreen) {
443 wnd_fullscreen=false;
444 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
445 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX);
446 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
448 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_TOP,wnd_fs_rect.left,wnd_fs_rect.top,
449 wnd_fs_rect.right-wnd_fs_rect.left,
450 wnd_fs_rect.bottom-wnd_fs_rect.top,
451 SWP_DRAWFRAME | SWP_FRAMECHANGED);
452 MoveWindow(win,wnd_fs_rect_client.left,wnd_fs_rect_client.top,
453 wnd_fs_rect_client.right-wnd_fs_rect_client.left,
454 wnd_fs_rect_client.bottom-wnd_fs_rect_client.top,TRUE);
457 GetWindowRect(win_main,&wnd_fs_rect);
458 GetWindowRect(win,&wnd_fs_rect_client);
459 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP );
460 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
461 HMONITOR monitor=MonitorFromWindow(win_main,MONITOR_DEFAULTTONEAREST);
463 moninfo.cbSize=sizeof(moninfo);
465 if (!GetMonitorInfo(monitor,&moninfo)) return ;
466 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_TOP,moninfo.rcMonitor.left,moninfo.rcMonitor.top,
467 moninfo.rcMonitor.right,moninfo.rcMonitor.bottom,SWP_FRAMECHANGED);
476 void ToggleTopmost() {
477 wnd_topmost=!wnd_topmost;
478 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_NOTOPMOST,0,0,
479 0,0,SWP_NOMOVE | SWP_NOSIZE);
482 void CursorUpdate() {
484 GetCursorPos(&cursorpos);
486 asswind=WindowFromPoint(cursorpos);
487 if (asswind!=win_main && asswind!=win) {
488 return ; //not our responsibility
490 if ((timeGetTime()-lastmousemove)<4000 || cmenu) {
491 SetCursor(LoadCursor(NULL,IDC_ARROW));
497 bool ContextMenu(HWND wind,int x,int y) {
500 ScreenToClient(wind,&p);
501 GetClientRect(wind,&clientrect);
502 if (!PtInRect(&clientrect,p)) return false;
503 ClientToScreen(wind,&p);
506 menu=LoadMenu(hinstance,MAKEINTRESOURCE(VOMPMENU));
507 popup=GetSubMenu(menu,0);
508 if (wnd_fullscreen) {
509 CheckMenuItem(popup,VOMP_FULL_SCREEN,MF_BYCOMMAND|MF_CHECKED);
511 CheckMenuItem(popup,VOMP_FULL_SCREEN,MF_BYCOMMAND|MF_UNCHECKED);
514 CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_CHECKED);
516 CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_UNCHECKED);
519 TrackPopupMenu(popup,TPM_RIGHTBUTTON|TPM_LEFTALIGN,x,y,0,wind, NULL);
527 LONG FAR PASCAL WindowProc(HWND wind, UINT msg, WPARAM wparam, LPARAM lparam)
533 logger->log("Core", Log::NOTICE, "Window closed, shutting down...");
538 CalculateWindowSize((RECT*) lparam,wparam);
542 int width = LOWORD(lparam);
543 int height = HIWORD(lparam);
545 if (wparam == SIZE_MAXIMIZED) {
548 } /*else if (wparam == SIZE_MINIMIZED) {
558 if (GetUpdateRect(wind, &r, FALSE)) {
559 BeginPaint(wind, &ps);
560 //Call Painting Mechanism
565 if (inputWin->ReceiveButtonVK(wparam)) {
566 return 0L; //We process that Key
568 return DefWindowProc(wind, msg, wparam, lparam);
573 if (inputWin->ReceiveButtonCH(wparam)) {
574 return 0L; //We process that Key
576 return DefWindowProc(wind, msg, wparam, lparam);
581 if (inputWin->ReceiveButtonAP(GET_APPCOMMAND_LPARAM(lparam))){
582 return TRUE; //yes we process that message
584 return DefWindowProc(wind, msg, wparam, lparam);
593 dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,NULL,&risize,sizeof(RAWINPUTHEADER));
594 lpit=(LPRAWINPUT)malloc(risize);
595 dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,lpit,&risize,sizeof(RAWINPUTHEADER));
597 if (lpit->header.dwType==RIM_TYPEHID && lpit->data.hid.dwSizeHid>=2) {
598 DWORD button=lpit->data.hid.bRawData[1] | (lpit->data.hid.bRawData[0]<< 8);
599 if (inputWin->ReceiveButtonRI(button)){
601 return 0; //yes we process that message
606 return DefWindowProc(wind, msg, wparam, lparam);
611 if (LOWORD(wparam)==VOMP_FULL_SCREEN) {
615 if (LOWORD(wparam)==VOMP_TOPMOST) {
619 if (inputWin->ReceiveButtonAP(LOWORD(wparam))){
620 return 0; //yes we process that message
622 return DefWindowProc(wind, msg, wparam, lparam);
627 if (((HANDLE)wparam)==win) {
631 return DefWindowProc(wind, msg, wparam, lparam);
635 if (wparam==SC_MAXIMIZE) {
638 } else if (wparam==SC_SCREENSAVE || wparam==SC_MONITORPOWER) {
641 return DefWindowProc(wind,msg,wparam, lparam);
646 lastmousemove=timeGetTime();
647 SetCursor(LoadCursor(NULL,IDC_ARROW));
648 SetTimer(wind,VOMP_CURSORUPDATE,4500,NULL);
649 POINT mpos={GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam)};
650 ClientToScreen(wind,&mpos);
651 if (TranslateMousePosition(&mpos)) {
652 Message *mousemes=new Message();
653 mousemes->message=Message::MOUSE_MOVE;
655 mousemes->p_to = Message::MOUSE_RECEIVER;
656 mousemes->parameter=(mpos.x & 0xFFFF)<< 16| (mpos.y & 0xFFFF);
658 messageQueue->postMessage(mousemes);
662 //return DefWindowProc(wind,msg,wparam, lparam);
666 if (wparam==VOMP_CURSORUPDATE) {
670 return DefWindowProc(wind, msg, wparam, lparam);
674 if (!ContextMenu(wind,GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam))) {
675 return DefWindowProc(wind, msg, wparam, lparam);
680 case WM_LBUTTONDOWN:{
681 POINT mpos={GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam)};
682 ClientToScreen(wind,&mpos);
683 if (TranslateMousePosition(&mpos)) {
684 Message *mousemes=new Message();
685 mousemes->message=Message::MOUSE_LBDOWN;
687 mousemes->p_to = Message::MOUSE_RECEIVER;
688 mousemes->parameter=(mpos.x & 0xFFFF)<< 16| (mpos.y & 0xFFFF);
690 messageQueue->postMessage(mousemes);
694 POINT mpos = { GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam) };
695 ClientToScreen(wind, &mpos);
696 if (TranslateMousePosition(&mpos)) {
697 Message *mousemes = new Message();
698 mousemes->message = Message::MOUSE_SCROLL;
699 mousemes->from = NULL;
700 mousemes->p_to = Message::MOUSE_RECEIVER;
701 mousemes->parameter = (0 & 0xFFFF) << 16 | (GET_WHEEL_DELTA_WPARAM(wparam) &0xFFFF);
702 mousemes->tag = (mpos.x & 0xFFFF) << 16 | (mpos.y & 0xFFFF);
703 messageQueue->postMessage(mousemes);
708 return DefWindowProc(wind, msg, wparam, lparam);
714 bool InitApp(HINSTANCE hinst,int cmdshow) {
718 wcs.style = CS_HREDRAW | CS_VREDRAW;
719 wcs.lpfnWndProc = WindowProc;
721 wcs.cbWndExtra = sizeof(DWORD);
722 wcs.hInstance = hinst;
725 wcs.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
726 wcs.lpszMenuName = NULL;
727 wcs.lpszClassName = "vomp";
728 acc=LoadAccelerators(hinst,MAKEINTRESOURCE(VOMPACCELERATOR));
729 if (!RegisterClass(&wcs))
731 flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
732 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
733 RECT wnted={50,50,768+50,576+50};
734 AdjustWindowRect(&wnted,flags ,false);
735 win_main=CreateWindow("vomp","VOMP on Windows",flags, CW_USEDEFAULT,CW_USEDEFAULT,
736 wnted.right-wnted.left,wnted.bottom-wnted.top,NULL,NULL,hinst,NULL);
739 ShowWindow(win_main,SW_SHOWNORMAL);
740 UpdateWindow(win_main);
741 /* in order to handle letterboxing we use a child window */
743 child_wcs.style = CS_HREDRAW | CS_VREDRAW;
744 child_wcs.lpfnWndProc = WindowProc;
745 child_wcs.cbClsExtra = 0;
746 child_wcs.cbWndExtra = sizeof(DWORD);
747 child_wcs.hInstance = hinst;
748 child_wcs.hIcon = NULL;
749 child_wcs.hCursor = NULL;
750 child_wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
751 child_wcs.lpszMenuName = NULL;
752 child_wcs.lpszClassName = "vomp_playback";
753 if (!RegisterClass(&child_wcs))
756 win=CreateWindow("vomp_playback","Vomp Playback Window",WS_VISIBLE | WS_CHILD |WS_CLIPCHILDREN,
757 0,0,768,576,win_main,NULL,hinst,NULL);
760 ShowWindow(win,SW_SHOWNORMAL);
762 if (remotefnc) {//at least windows XP
763 /* We want to support MCE Remote controls*/
764 RAWINPUTDEVICE remote_control_data[4];
765 ZeroMemory(remote_control_data,sizeof(remote_control_data));
766 remote_control_data[0].usUsagePage=0xFFBC;
767 remote_control_data[0].usUsage=0x88;
768 remote_control_data[0].dwFlags=0;
769 remote_control_data[1].usUsagePage=0x0C;
770 remote_control_data[1].usUsage=0x80;
771 remote_control_data[1].dwFlags=0;
772 remote_control_data[2].usUsagePage=0x0C;
773 remote_control_data[2].usUsage=0x01;
774 remote_control_data[2].dwFlags=0;
775 remote_control_data[3].usUsagePage=0x01;
776 remote_control_data[3].usUsage=0x80;
777 remote_control_data[3].dwFlags=0;
778 if (dynRegisterRawInputDevices(remote_control_data,4,sizeof(remote_control_data[0]))!=TRUE) {
779 MessageBox(0,"Registering remote control failed!","Aborting!",0);
791 // -------------------------------------------------------------------------------------------------------------------
793 void shutdown(int code)
795 if (control) // shut down control here in case views have posted messages
799 logger->log("Core", Log::NOTICE, "Control module shut down");
806 logger->log("Core", Log::NOTICE, "OSD module shut down");
813 logger->log("Core", Log::NOTICE, "Audio module shut down");
820 logger->log("Core", Log::NOTICE, "Video module shut down");
827 logger->log("Core", Log::NOTICE, "Timers module shut down");
834 logger->log("Core", Log::NOTICE, "LED module shut down");
841 logger->log("Core", Log::NOTICE, "Remote module shut down");
846 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
854 // -------------------------------------------------------------------------------------------------------------------
856 ULLONG htonll(ULLONG a)
858 return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32)
859 |(ULONG)htonl(((ULONG) (a >> 32))));
862 ULLONG ntohll(ULLONG a)
867 const std::string& getCommandLineServer()
869 return commandLineServer;