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/>.
26 #define _WIN32_WINNT 0x501
30 #include "vompreswin.h"
41 #include "windowsosd.h"
43 #include "osdwinpixel.h"
45 #include "osdwinvector.h"
50 #include "vsleeptimer.h"
51 #include "messagequeue.h"
53 void sighandler(int signalReceived);
54 void shutdown(int code);
56 // Global variables --------------------------------------------------------------------------------------------------
70 Sleeptimer* sleeptimer;
73 bool wnd_fullscreen=false;
74 bool wnd_topmost=false;
75 RECT wnd_fs_rect={20,20,768+20,576+20};
76 RECT wnd_fs_rect_client={0,0,768,576};
77 //OSVERSIONINFO windows_ver; //attempt to distigsh windows versions
83 typedef UINT (WINAPI *GETRAWINPUTDATAFNC) (HRAWINPUT,UINT,LPVOID,PUINT,UINT);
84 typedef UINT (WINAPI *REGISTERRAWINPUTDEVICEFNC) (PCRAWINPUTDEVICE,UINT,UINT);
86 GETRAWINPUTDATAFNC dynGetRawInputData=NULL;
87 REGISTERRAWINPUTDEVICEFNC dynRegisterRawInputDevices=NULL;
91 MessageQueue* messageQueue;
94 void MILLISLEEP(ULONG a)
101 int getClockRealTime(struct timespec *tp){
105 GetSystemTime(&systime);
106 SystemTimeToFileTime(&systime, (FILETIME*)&filetime);
107 tp->tv_sec = (filetime - WINDOWS_TIME_BASE_OFFSET) / (10 * 1000 * 1000);
108 tp->tv_nsec = ((filetime - WINDOWS_TIME_BASE_OFFSET) % (10 * 1000 * 1000)) * 100;
112 DWORD WINAPI commandthreadStart(void *arg)
118 void LoadRemoteFunctions() {
119 user32dll=LoadLibrary("user32.dll");
120 if (user32dll!=NULL) {
121 dynGetRawInputData=(GETRAWINPUTDATAFNC)GetProcAddress(user32dll,"GetRawInputData");
122 if (dynGetRawInputData!=NULL) {
123 dynRegisterRawInputDevices=(REGISTERRAWINPUTDEVICEFNC)GetProcAddress(user32dll,"RegisterRawInputDevices");
124 if (dynRegisterRawInputDevices!=NULL) {
131 bool InitApp(HINSTANCE hinst,int cmdshow);
133 HWND win_main;//global window handle
134 HWND win;//global child window handle
137 #define ERROR_MSG(str) MessageBox(win_main,str,"Error!",MB_OK|MB_ICONWARNING)
138 INT WINAPI WinMain( HINSTANCE hinst , HINSTANCE previnst, LPSTR cmdline, int cmdshow)
141 //On Windows we have to init a window, we use DXUT
142 LoadRemoteFunctions();
143 if (!InitApp(hinst,cmdshow)) return false;
144 //Starting Network support
146 int result = WSAStartup(MAKEWORD(2,2),&wsadat);
147 if (result!=NO_ERROR) {
148 ERROR_MSG("Initialising WinSocked: Error at WSAStartup()\n");
152 result= CoInitializeEx(NULL,COINIT_MULTITHREADED );//Initialize COM for DirectShow
154 ERROR_MSG("Initialising COM: Error at Coinitialize()\n");
161 // Init global vars ------------------------------------------------------------------------------------------------
164 remote = new InputMan();
166 timers = new Timers();
167 osd = new Osd_TYPE();
169 video = new VideoWin();
170 audio = new AudioWin();
171 boxstack = new BoxStack();
172 command = new Command();
174 sleeptimer = new Sleeptimer();
176 if (!logger || !remote || !led || !osd || !video || !audio || !boxstack || !command || !sleeptimer)
178 ERROR_MSG("Could not create objects. Memory problems?\n");
184 messageQueue = command;
186 // Get logging module started --------------------------------------------------------------------------------------
188 if (!logger->init(Log::DEBUG, "vompwin.log", true))
190 ERROR_MSG("Could not initialise log object. Aborting.\n");
196 logger->log("Core", Log::INFO, "Starting up...");
200 // Init modules ----------------------------------------------------------------------------------------------------
203 success = remote->init();
206 logger->log("Core", Log::INFO, "Remote module initialised");
210 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
216 inputWin = remote->getInputWin();
218 success = led->init(0);
221 logger->log("Core", Log::INFO, "LED module initialised");
225 logger->log("Core", Log::EMERG, "LED module failed to initialise");
231 success = timers->init();
234 logger->log("Core", Log::INFO, "Timers module initialised");
238 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
244 UCHAR videoFormat = Video::PAL; // PALNTSC FIXME.
246 success = video->init(videoFormat);
249 logger->log("Core", Log::INFO, "Video module initialised");
253 logger->log("Core", Log::EMERG, "Video module failed to initialise");
259 dynamic_cast<WindowsOsd*>(osd)->setWindow(win);
260 success = osd->init();
263 logger->log("Core", Log::INFO, "OSD module initialised");
267 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
272 video->setDefaultAspect();
274 success = audio->init(Audio::MPEG2_PES);
277 logger->log("Core", Log::INFO, "Audio module initialised");
281 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
287 success = vdr->init();
290 logger->log("Core", Log::INFO, "VDR module initialised");
294 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
300 success = boxstack->init();
303 logger->log("Core", Log::INFO, "BoxStack module initialised");
307 logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
313 success = command->init();
316 logger->log("Core", Log::INFO, "Command module initialised");
320 logger->log("Core", Log::EMERG, "Command module failed to initialise");
326 // Other init ------------------------------------------------------------------------------------------------------
328 logger->log("Core", Log::NOTICE, "Startup successful");
330 // Run main loop ---------------------------------------------------------------------------------------------------
332 // Ok, all major device components and other bits are loaded and ready
333 lastmousemove=timeGetTime();
335 HANDLE commandthread;
336 commandthread= CreateThread(NULL, 0, commandthreadStart, NULL,0,
339 message.message=WM_NULL;
341 while(run && WaitForSingleObject(commandthread,0)==WAIT_TIMEOUT) {
342 if (PeekMessage(&message, NULL, 0,0,PM_REMOVE)!=0) {
343 if (TranslateAccelerator(win_main,acc,&message)==NULL) {
344 TranslateMessage(&message);
345 DispatchMessage(&message);
346 switch (message.message) {
348 run=false; //TODO post exit to command Messages
352 //Render, moved to OSD !
354 //((OsdWin*)osd)->Render();
357 // When that returns quit ------------------------------------------------------------------------------------------
358 WaitForSingleObject(commandthread,INFINITE);
361 if (user32dll) FreeModule(user32dll);
366 bool TranslateMousePosition(POINT *pos) {
369 ScreenToClient(win,pos);
370 GetClientRect(win,&clientrect);
371 if (!PtInRect(&clientrect,*pos)) return false;//Don't pass it further
372 pos->x=((double)pos->x)/((double) (clientrect.right-clientrect.left))
373 *((double)Video::getInstance()->getScreenWidth());
374 pos->y=((double)pos->y)/((double) (clientrect.bottom-clientrect.top))
375 *((double)Video::getInstance()->getScreenHeight());
382 void CalculateWindowSize(RECT * size,ULONG size_mode) {
385 DWORD adjheight,adjwidth;
386 if (!wnd_fullscreen) {
387 DWORD flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
388 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
389 RECT wnted={50,50,150,150};
390 AdjustWindowRect(&wnted,flags ,false);
391 adjwidth=-wnted.left+wnted.right-100;
392 adjheight=-wnted.top+wnted.bottom-100;
394 adjwidth=adjheight=0;
396 width=size->right-size->left-adjwidth;
397 height=size->bottom-size->top-adjheight;
398 UCHAR mode=Video::PAL;
399 UCHAR aspect=Video::ASPECT4X3;
400 UCHAR tvsize=Video::ASPECT4X3;
402 mode=video->getMode();
403 aspect=((VideoWin*)video)->getAspectRatio();
404 tvsize=((VideoWin*)video)->getPseudoTVsize();
407 double aspectrt=4./3.;
408 if (tvsize==Video::ASPECT16X9) {
409 if (aspect==Video::ASPECT16X9) {
410 aspectrt=4./3.; //looks strange, but it is a 16:9 tv
411 } else if (aspect==Video::ASPECT4X3) {
412 aspectrt=4./3./(16./9.)*(4./3.); //I hope this is correct
414 } else if (tvsize==Video::ASPECT4X3) {
415 if (aspect==Video::ASPECT16X9) {
416 if (mode!=Video::NORMAL) {
421 } else if (aspect==Video::ASPECT4X3) {
425 if (!wnd_fullscreen) {
428 case WMSZ_BOTTOMRIGHT:
431 width=(ULONG)(((double)height)*aspectrt);
432 size->right=size->left+width+adjwidth;
434 case WMSZ_BOTTOMLEFT:
436 width=(ULONG)(((double)height)*aspectrt);
437 size->left=size->right-width-adjwidth;
441 height=(ULONG)(((double)width)/aspectrt);
442 size->bottom=size->top+height+adjheight;
445 MoveWindow(win,0,0,width,height,TRUE);
447 RECT newrect={0,0,width,height};
449 if ((ULONG)(((double)height)*aspectrt)>width) {
450 newlength=(ULONG)(((double)width)/aspectrt);
451 newrect.top+=(height-newlength)/2;
452 newrect.bottom-=(height-newlength);
454 newlength=(ULONG)(((double)height)*aspectrt);
455 newrect.left+=(width-newlength)/2;
456 newrect.right-=(width-newlength);
458 MoveWindow(win,newrect.left,newrect.top,newrect.right,newrect.bottom,TRUE);
463 void AdjustWindow() {
464 if (!wnd_fullscreen) {
466 GetWindowRect(win_main,&winrect);
467 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
468 MoveWindow(win_main,winrect.left,
469 winrect.top,winrect.right-winrect.left,winrect.bottom-winrect.top,true);
472 GetWindowRect(win_main,&winrect);
473 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
478 void ToggleFullscreen() {
479 if (wnd_fullscreen) {
480 wnd_fullscreen=false;
481 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
482 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX);
483 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
485 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_TOP,wnd_fs_rect.left,wnd_fs_rect.top,
486 wnd_fs_rect.right-wnd_fs_rect.left,
487 wnd_fs_rect.bottom-wnd_fs_rect.top,
488 SWP_DRAWFRAME | SWP_FRAMECHANGED);
489 MoveWindow(win,wnd_fs_rect_client.left,wnd_fs_rect_client.top,
490 wnd_fs_rect_client.right-wnd_fs_rect_client.left,
491 wnd_fs_rect_client.bottom-wnd_fs_rect_client.top,TRUE);
494 GetWindowRect(win_main,&wnd_fs_rect);
495 GetWindowRect(win,&wnd_fs_rect_client);
496 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP );
497 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
498 HMONITOR monitor=MonitorFromWindow(win_main,MONITOR_DEFAULTTONEAREST);
500 moninfo.cbSize=sizeof(moninfo);
502 if (!GetMonitorInfo(monitor,&moninfo)) return ;
503 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_TOP,moninfo.rcMonitor.left,moninfo.rcMonitor.top,
504 moninfo.rcMonitor.right,moninfo.rcMonitor.bottom,SWP_FRAMECHANGED);
513 void ToggleTopmost() {
514 wnd_topmost=!wnd_topmost;
515 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_NOTOPMOST,0,0,
516 0,0,SWP_NOMOVE | SWP_NOSIZE);
519 void CursorUpdate() {
521 GetCursorPos(&cursorpos);
523 asswind=WindowFromPoint(cursorpos);
524 if (asswind!=win_main && asswind!=win) {
525 return ; //not our responsibility
527 if ((timeGetTime()-lastmousemove)<4000 || cmenu) {
528 SetCursor(LoadCursor(NULL,IDC_ARROW));
534 bool ContextMenu(HWND wind,int x,int y) {
537 ScreenToClient(wind,&p);
538 GetClientRect(wind,&clientrect);
539 if (!PtInRect(&clientrect,p)) return false;
540 ClientToScreen(wind,&p);
543 menu=LoadMenu(hinstance,MAKEINTRESOURCE(VOMPMENU));
544 popup=GetSubMenu(menu,0);
545 if (wnd_fullscreen) {
546 CheckMenuItem(popup,VOMP_FULL_SCREEN,MF_BYCOMMAND|MF_CHECKED);
548 CheckMenuItem(popup,VOMP_FULL_SCREEN,MF_BYCOMMAND|MF_UNCHECKED);
551 CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_CHECKED);
553 CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_UNCHECKED);
556 TrackPopupMenu(popup,TPM_RIGHTBUTTON|TPM_LEFTALIGN,x,y,0,wind, NULL);
564 LONG FAR PASCAL WindowProc(HWND wind, UINT msg, WPARAM wparam, LPARAM lparam)
570 logger->log("Core", Log::NOTICE, "Window closed, shutting down...");
575 CalculateWindowSize((RECT*) lparam,wparam);
579 int width = LOWORD(lparam);
580 int height = HIWORD(lparam);
582 if (wparam == SIZE_MAXIMIZED) {
585 } /*else if (wparam == SIZE_MINIMIZED) {
595 if (GetUpdateRect(wind, &r, FALSE)) {
596 BeginPaint(wind, &ps);
597 //Call Painting Mechanism
602 if (inputWin->ReceiveButtonVK(wparam)) {
603 return 0L; //We process that Key
605 return DefWindowProc(wind, msg, wparam, lparam);
610 if (inputWin->ReceiveButtonCH(wparam)) {
611 return 0L; //We process that Key
613 return DefWindowProc(wind, msg, wparam, lparam);
618 if (inputWin->ReceiveButtonAP(GET_APPCOMMAND_LPARAM(lparam))){
619 return TRUE; //yes we process that message
621 return DefWindowProc(wind, msg, wparam, lparam);
630 dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,NULL,&risize,sizeof(RAWINPUTHEADER));
631 lpit=(LPRAWINPUT)malloc(risize);
632 dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,lpit,&risize,sizeof(RAWINPUTHEADER));
634 if (lpit->header.dwType==RIM_TYPEHID && lpit->data.hid.dwSizeHid>=2) {
635 DWORD button=lpit->data.hid.bRawData[1] | (lpit->data.hid.bRawData[0]<< 8);
636 if (inputWin->ReceiveButtonRI(button)){
638 return 0; //yes we process that message
643 return DefWindowProc(wind, msg, wparam, lparam);
648 if (LOWORD(wparam)==VOMP_FULL_SCREEN) {
652 if (LOWORD(wparam)==VOMP_TOPMOST) {
656 if (inputWin->ReceiveButtonAP(LOWORD(wparam))){
657 return 0; //yes we process that message
659 return DefWindowProc(wind, msg, wparam, lparam);
664 if (((HANDLE)wparam)==win) {
668 return DefWindowProc(wind, msg, wparam, lparam);
672 if (wparam==SC_MAXIMIZE) {
675 } else if (wparam==SC_SCREENSAVE || wparam==SC_MONITORPOWER) {
678 return DefWindowProc(wind,msg,wparam, lparam);
683 lastmousemove=timeGetTime();
684 SetCursor(LoadCursor(NULL,IDC_ARROW));
685 SetTimer(wind,VOMP_CURSORUPDATE,4500,NULL);
686 POINT mpos={GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam)};
687 ClientToScreen(wind,&mpos);
688 if (TranslateMousePosition(&mpos)) {
689 Message *mousemes=new Message();
690 mousemes->message=Message::MOUSE_MOVE;
692 mousemes->to=BoxStack::getInstance();
693 mousemes->parameter=(mpos.x & 0xFFFF)<< 16| (mpos.y & 0xFFFF);
695 messageQueue->postMessage(mousemes);
699 //return DefWindowProc(wind,msg,wparam, lparam);
703 if (wparam==VOMP_CURSORUPDATE) {
707 return DefWindowProc(wind, msg, wparam, lparam);
711 if (!ContextMenu(wind,GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam))) {
712 return DefWindowProc(wind, msg, wparam, lparam);
717 case WM_LBUTTONDOWN:{
718 POINT mpos={GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam)};
719 ClientToScreen(wind,&mpos);
720 if (TranslateMousePosition(&mpos)) {
721 Message *mousemes=new Message();
722 mousemes->message=Message::MOUSE_LBDOWN;
724 mousemes->to=BoxStack::getInstance();
725 mousemes->parameter=(mpos.x & 0xFFFF)<< 16| (mpos.y & 0xFFFF);
727 messageQueue->postMessage(mousemes);
731 POINT mpos = { GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam) };
732 ClientToScreen(wind, &mpos);
733 if (TranslateMousePosition(&mpos)) {
734 Message *mousemes = new Message();
735 mousemes->message = Message::MOUSE_SCROLL;
736 mousemes->from = NULL;
737 mousemes->to = BoxStack::getInstance();
738 mousemes->parameter = (0 & 0xFFFF) << 16 | (GET_WHEEL_DELTA_WPARAM(wparam) &0xFFFF);
739 mousemes->tag = (mpos.x & 0xFFFF) << 16 | (mpos.y & 0xFFFF);
740 messageQueue->postMessage(mousemes);
745 return DefWindowProc(wind, msg, wparam, lparam);
751 bool InitApp(HINSTANCE hinst,int cmdshow) {
755 wcs.style = CS_HREDRAW | CS_VREDRAW;
756 wcs.lpfnWndProc = WindowProc;
758 wcs.cbWndExtra = sizeof(DWORD);
759 wcs.hInstance = hinst;
762 wcs.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
763 wcs.lpszMenuName = NULL;
764 wcs.lpszClassName = "vomp";
765 acc=LoadAccelerators(hinst,MAKEINTRESOURCE(VOMPACCELERATOR));
766 if (!RegisterClass(&wcs))
768 flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
769 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
770 RECT wnted={50,50,768+50,576+50};
771 AdjustWindowRect(&wnted,flags ,false);
772 win_main=CreateWindow("vomp","VOMP on Windows",flags, CW_USEDEFAULT,CW_USEDEFAULT,
773 wnted.right-wnted.left,wnted.bottom-wnted.top,NULL,NULL,hinst,NULL);
776 ShowWindow(win_main,SW_SHOWNORMAL);
777 UpdateWindow(win_main);
778 /* in order to handle letterboxing we use a child window */
780 child_wcs.style = CS_HREDRAW | CS_VREDRAW;
781 child_wcs.lpfnWndProc = WindowProc;
782 child_wcs.cbClsExtra = 0;
783 child_wcs.cbWndExtra = sizeof(DWORD);
784 child_wcs.hInstance = hinst;
785 child_wcs.hIcon = NULL;
786 child_wcs.hCursor = NULL;
787 child_wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
788 child_wcs.lpszMenuName = NULL;
789 child_wcs.lpszClassName = "vomp_playback";
790 if (!RegisterClass(&child_wcs))
793 win=CreateWindow("vomp_playback","Vomp Playback Window",WS_VISIBLE | WS_CHILD |WS_CLIPCHILDREN,
794 0,0,768,576,win_main,NULL,hinst,NULL);
797 ShowWindow(win,SW_SHOWNORMAL);
799 if (remotefnc) {//at least windows XP
800 /* We want to support MCE Remote controls*/
801 RAWINPUTDEVICE remote_control_data[4];
802 ZeroMemory(remote_control_data,sizeof(remote_control_data));
803 remote_control_data[0].usUsagePage=0xFFBC;
804 remote_control_data[0].usUsage=0x88;
805 remote_control_data[0].dwFlags=0;
806 remote_control_data[1].usUsagePage=0x0C;
807 remote_control_data[1].usUsage=0x80;
808 remote_control_data[1].dwFlags=0;
809 remote_control_data[2].usUsagePage=0x0C;
810 remote_control_data[2].usUsage=0x01;
811 remote_control_data[2].dwFlags=0;
812 remote_control_data[3].usUsagePage=0x01;
813 remote_control_data[3].usUsage=0x80;
814 remote_control_data[3].dwFlags=0;
815 if (dynRegisterRawInputDevices(remote_control_data,4,sizeof(remote_control_data[0]))!=TRUE) {
816 MessageBox(0,"Registering remote control failed!","Aborting!",0);
828 // -------------------------------------------------------------------------------------------------------------------
830 void shutdown(int code)
834 boxstack->shutdown();
836 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
839 if (command) // shut down command here in case views have posted messages
843 logger->log("Core", Log::NOTICE, "Command module shut down");
850 logger->log("Core", Log::NOTICE, "VDR module shut down");
857 logger->log("Core", Log::NOTICE, "OSD module shut down");
864 logger->log("Core", Log::NOTICE, "Audio module shut down");
871 logger->log("Core", Log::NOTICE, "Video module shut down");
878 logger->log("Core", Log::NOTICE, "Timers module shut down");
885 logger->log("Core", Log::NOTICE, "LED module shut down");
892 logger->log("Core", Log::NOTICE, "Remote module shut down");
897 logger->log("Core", Log::NOTICE, "WOL module shut down");
902 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
909 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
915 // -------------------------------------------------------------------------------------------------------------------
917 ULLONG htonll(ULLONG a)
919 return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32)
920 |(ULONG)htonl(((ULONG) (a >> 32))));
923 ULLONG ntohll(ULLONG a)