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"
39 #include "windowsosd.h"
43 #include "messagequeue.h"
45 void sighandler(int signalReceived);
46 void shutdown(int code);
48 // Global variables --------------------------------------------------------------------------------------------------
54 bool wnd_fullscreen=false;
55 bool wnd_topmost=false;
56 RECT wnd_fs_rect={20,20,768+20,576+20};
57 RECT wnd_fs_rect_client={0,0,768,576};
58 //OSVERSIONINFO windows_ver; //attempt to distigsh windows versions
64 typedef u4 (WINAPI *GETRAWINPUTDATAFNC) (HRAWINPUT,u4,LPVOID,Pu4,u4);
65 typedef u4 (WINAPI *REGISTERRAWINPUTDEVICEFNC) (PCRAWINPUTDEVICE,u4,u4);
67 GETRAWINPUTDATAFNC dynGetRawInputData=NULL;
68 REGISTERRAWINPUTDEVICEFNC dynRegisterRawInputDevices=NULL;
72 MessageQueue* messageQueue;
74 int getClockRealTime(struct timespec *tp){
78 GetSystemTime(&systime);
79 SystemTimeToFileTime(&systime, (FILETIME*)&filetime);
80 tp->tv_sec = (filetime - WINDOWS_TIME_BASE_OFFSET) / (10 * 1000 * 1000);
81 tp->tv_nsec = ((filetime - WINDOWS_TIME_BASE_OFFSET) % (10 * 1000 * 1000)) * 100;
85 DWORD WINAPI controlthreadStart(void *arg)
92 void LoadRemoteFunctions() {
93 user32dll=LoadLibrary("user32.dll");
94 if (user32dll!=NULL) {
95 dynGetRawInputData=(GETRAWINPUTDATAFNC)GetProcAddress(user32dll,"GetRawInputData");
96 if (dynGetRawInputData!=NULL) {
97 dynRegisterRawInputDevices=(REGISTERRAWINPUTDEVICEFNC)GetProcAddress(user32dll,"RegisterRawInputDevices");
98 if (dynRegisterRawInputDevices!=NULL) {
105 bool InitApp(HINSTANCE hinst,int cmdshow);
107 HWND win_main;//global window handle
108 HWND win;//global child window handle
111 #define ERROR_MSG(str) MessageBox(win_main,str,"Error!",MB_OK|MB_ICONWARNING)
112 INT WINAPI WinMain( HINSTANCE hinst , HINSTANCE previnst, LPSTR cmdline, int cmdshow)
115 //On Windows we have to init a window, we use DXUT
116 LoadRemoteFunctions();
117 if (!InitApp(hinst,cmdshow)) return false;
118 //Starting Network support
120 int result = WSAStartup(MAKEWORD(2,2),&wsadat);
121 if (result!=NO_ERROR) {
122 ERROR_MSG("Initialising WinSocked: Error at WSAStartup()\n");
126 result= CoInitializeEx(NULL,COINIT_MULTITHREADED );//Initialize COM for DirectShow
128 ERROR_MSG("Initialising COM: Error at Coinitialize()\n");
135 // Init global vars ------------------------------------------------------------------------------------------------
138 control = new Control();
140 if (!logger || !control)
142 ERROR_MSG("Could not create objects. Memory problems?\n");
148 messageQueue = control;
150 // Get logging module started --------------------------------------------------------------------------------------
152 if (!logger->init(Log::DEBUG, "vompwin.log", true))
154 ERROR_MSG("Could not initialise log object. Aborting.\n");
160 logger->log("Core", Log::INFO, "Starting up...");
163 // Init modules ----------------------------------------------------------------------------------------------------
166 dynamic_cast<WindowsOsd*>(osd)->setWindow(win);
168 success = control->init();
171 logger->log("Core", Log::INFO, "Control module initialised");
175 logger->log("Core", Log::EMERG, "Control module failed to initialise");
183 inputWin = InputMan::getInstance()->getInputWin();
184 Video::getInstance()->setDefaultAspect();
186 // Other init ------------------------------------------------------------------------------------------------------
188 logger->log("Core", Log::NOTICE, "Startup successful");
190 // Run main loop ---------------------------------------------------------------------------------------------------
192 // Ok, all major device components and other bits are loaded and ready
193 lastmousemove=timeGetTime();
195 HANDLE controlthread;
196 controlthread= CreateThread(NULL, 0, controlthreadStart, NULL,0,
199 message.message=WM_NULL;
201 while(run && WaitForSingleObject(controlthread,0)==WAIT_TIMEOUT) {
202 if (PeekMessage(&message, NULL, 0,0,PM_REMOVE)!=0) {
203 if (TranslateAccelerator(win_main,acc,&message)==NULL) {
204 TranslateMessage(&message);
205 DispatchMessage(&message);
206 switch (message.message) {
208 run=false; //TODO post exit to control Messages
212 //Render, moved to OSD !
214 //((OsdWin*)osd)->Render();
217 // When that returns quit ------------------------------------------------------------------------------------------
218 WaitForSingleObject(controlthread,INFINITE);
221 if (user32dll) FreeModule(user32dll);
226 bool TranslateMousePosition(POINT *pos) {
229 ScreenToClient(win,pos);
230 GetClientRect(win,&clientrect);
231 if (!PtInRect(&clientrect,*pos)) return false;//Don't pass it further
232 pos->x=((double)pos->x)/((double) (clientrect.right-clientrect.left))
233 *((double)Video::getInstance()->getScreenWidth());
234 pos->y=((double)pos->y)/((double) (clientrect.bottom-clientrect.top))
235 *((double)Video::getInstance()->getScreenHeight());
242 void CalculateWindowSize(RECT * size,u4 size_mode) {
245 DWORD adjheight,adjwidth;
246 if (!wnd_fullscreen) {
247 DWORD flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
248 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
249 RECT wnted={50,50,150,150};
250 AdjustWindowRect(&wnted,flags ,false);
251 adjwidth=-wnted.left+wnted.right-100;
252 adjheight=-wnted.top+wnted.bottom-100;
254 adjwidth=adjheight=0;
256 width=size->right-size->left-adjwidth;
257 height=size->bottom-size->top-adjheight;
259 u1 aspect=Video::ASPECT4X3;
260 u1 tvsize=Video::ASPECT4X3;
261 Video* video = Video::getInstance();
263 mode=video->getMode();
264 aspect=((VideoWin*)video)->getAspectRatio();
265 tvsize=((VideoWin*)video)->getPseudoTVsize();
268 double aspectrt=4./3.;
269 if (tvsize==Video::ASPECT16X9) {
270 if (aspect==Video::ASPECT16X9) {
271 aspectrt=4./3.; //looks strange, but it is a 16:9 tv
272 } else if (aspect==Video::ASPECT4X3) {
273 aspectrt=4./3./(16./9.)*(4./3.); //I hope this is correct
275 } else if (tvsize==Video::ASPECT4X3) {
276 if (aspect==Video::ASPECT16X9) {
277 if (mode!=Video::NORMAL) {
282 } else if (aspect==Video::ASPECT4X3) {
286 if (!wnd_fullscreen) {
289 case WMSZ_BOTTOMRIGHT:
292 width=(u4)(((double)height)*aspectrt);
293 size->right=size->left+width+adjwidth;
295 case WMSZ_BOTTOMLEFT:
297 width=(u4)(((double)height)*aspectrt);
298 size->left=size->right-width-adjwidth;
302 height=(u4)(((double)width)/aspectrt);
303 size->bottom=size->top+height+adjheight;
306 MoveWindow(win,0,0,width,height,TRUE);
308 RECT newrect={0,0,width,height};
310 if ((u4)(((double)height)*aspectrt)>width) {
311 newlength=(u4)(((double)width)/aspectrt);
312 newrect.top+=(height-newlength)/2;
313 newrect.bottom-=(height-newlength);
315 newlength=(u4)(((double)height)*aspectrt);
316 newrect.left+=(width-newlength)/2;
317 newrect.right-=(width-newlength);
319 MoveWindow(win,newrect.left,newrect.top,newrect.right,newrect.bottom,TRUE);
324 void AdjustWindow() {
325 if (!wnd_fullscreen) {
327 GetWindowRect(win_main,&winrect);
328 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
329 MoveWindow(win_main,winrect.left,
330 winrect.top,winrect.right-winrect.left,winrect.bottom-winrect.top,true);
333 GetWindowRect(win_main,&winrect);
334 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
339 void ToggleFullscreen() {
340 if (wnd_fullscreen) {
341 wnd_fullscreen=false;
342 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
343 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX);
344 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
346 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_TOP,wnd_fs_rect.left,wnd_fs_rect.top,
347 wnd_fs_rect.right-wnd_fs_rect.left,
348 wnd_fs_rect.bottom-wnd_fs_rect.top,
349 SWP_DRAWFRAME | SWP_FRAMECHANGED);
350 MoveWindow(win,wnd_fs_rect_client.left,wnd_fs_rect_client.top,
351 wnd_fs_rect_client.right-wnd_fs_rect_client.left,
352 wnd_fs_rect_client.bottom-wnd_fs_rect_client.top,TRUE);
355 GetWindowRect(win_main,&wnd_fs_rect);
356 GetWindowRect(win,&wnd_fs_rect_client);
357 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP );
358 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
359 HMONITOR monitor=MonitorFromWindow(win_main,MONITOR_DEFAULTTONEAREST);
361 moninfo.cbSize=sizeof(moninfo);
363 if (!GetMonitorInfo(monitor,&moninfo)) return ;
364 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_TOP,moninfo.rcMonitor.left,moninfo.rcMonitor.top,
365 moninfo.rcMonitor.right,moninfo.rcMonitor.bottom,SWP_FRAMECHANGED);
374 void ToggleTopmost() {
375 wnd_topmost=!wnd_topmost;
376 SetWindowPos(win_main,wnd_topmost?HWND_TOPMOST:HWND_NOTOPMOST,0,0,
377 0,0,SWP_NOMOVE | SWP_NOSIZE);
380 void CursorUpdate() {
382 GetCursorPos(&cursorpos);
384 asswind=WindowFromPoint(cursorpos);
385 if (asswind!=win_main && asswind!=win) {
386 return ; //not our responsibility
388 if ((timeGetTime()-lastmousemove)<4000 || cmenu) {
389 SetCursor(LoadCursor(NULL,IDC_ARROW));
395 bool ContextMenu(HWND wind,int x,int y) {
398 ScreenToClient(wind,&p);
399 GetClientRect(wind,&clientrect);
400 if (!PtInRect(&clientrect,p)) return false;
401 ClientToScreen(wind,&p);
404 menu=LoadMenu(hinstance,MAKEINTRESOURCE(VOMPMENU));
405 popup=GetSubMenu(menu,0);
406 if (wnd_fullscreen) {
407 CheckMenuItem(popup,VOMP_FULL_SCREEN,MF_BYCOMMAND|MF_CHECKED);
409 CheckMenuItem(popup,VOMP_FULL_SCREEN,MF_BYCOMMAND|MF_UNCHECKED);
412 CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_CHECKED);
414 CheckMenuItem(popup,VOMP_TOPMOST,MF_BYCOMMAND|MF_UNCHECKED);
417 TrackPopupMenu(popup,TPM_RIGHTBUTTON|TPM_LEFTALIGN,x,y,0,wind, NULL);
425 LONG FAR PASCAL WindowProc(HWND wind, u4 msg, WPARAM wparam, LPARAM lparam)
431 logger->log("Core", Log::NOTICE, "Window closed, shutting down...");
436 CalculateWindowSize((RECT*) lparam,wparam);
440 int width = LOWORD(lparam);
441 int height = HIWORD(lparam);
443 if (wparam == SIZE_MAXIMIZED) {
446 } /*else if (wparam == SIZE_MINIMIZED) {
456 if (GetUpdateRect(wind, &r, FALSE)) {
457 BeginPaint(wind, &ps);
458 //Call Painting Mechanism
463 if (inputWin->ReceiveButtonVK(wparam)) {
464 return 0L; //We process that Key
466 return DefWindowProc(wind, msg, wparam, lparam);
471 if (inputWin->ReceiveButtonCH(wparam)) {
472 return 0L; //We process that Key
474 return DefWindowProc(wind, msg, wparam, lparam);
479 if (inputWin->ReceiveButtonAP(GET_APPCOMMAND_LPARAM(lparam))){
480 return TRUE; //yes we process that message
482 return DefWindowProc(wind, msg, wparam, lparam);
491 dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,NULL,&risize,sizeof(RAWINPUTHEADER));
492 lpit=(LPRAWINPUT)malloc(risize);
493 dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,lpit,&risize,sizeof(RAWINPUTHEADER));
495 if (lpit->header.dwType==RIM_TYPEHID && lpit->data.hid.dwSizeHid>=2) {
496 DWORD button=lpit->data.hid.bRawData[1] | (lpit->data.hid.bRawData[0]<< 8);
497 if (inputWin->ReceiveButtonRI(button)){
499 return 0; //yes we process that message
504 return DefWindowProc(wind, msg, wparam, lparam);
509 if (LOWORD(wparam)==VOMP_FULL_SCREEN) {
513 if (LOWORD(wparam)==VOMP_TOPMOST) {
517 if (inputWin->ReceiveButtonAP(LOWORD(wparam))){
518 return 0; //yes we process that message
520 return DefWindowProc(wind, msg, wparam, lparam);
525 if (((HANDLE)wparam)==win) {
529 return DefWindowProc(wind, msg, wparam, lparam);
533 if (wparam==SC_MAXIMIZE) {
536 } else if (wparam==SC_SCREENSAVE || wparam==SC_MONITORPOWER) {
539 return DefWindowProc(wind,msg,wparam, lparam);
543 if (!ContextMenu(wind,GET_X_LPARAM(lparam),GET_Y_LPARAM(lparam))) {
544 return DefWindowProc(wind, msg, wparam, lparam);
550 if (wparam==VOMP_CURSORUPDATE) {
554 return DefWindowProc(wind, msg, wparam, lparam);
560 // data = wheel delta
564 lastmousemove = timeGetTime();
565 SetCursor(LoadCursor(NULL,IDC_ARROW));
566 SetTimer(wind, VOMP_CURSORUPDATE, 4500, NULL);
567 POINT mpos = { GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam) };
568 ClientToScreen(wind, &mpos);
569 if (TranslateMousePosition(&mpos))
571 Message* mousemes = new Message();
572 mousemes->message = Message::MOUSE_MOVE;
573 mousemes->from = NULL;
574 mousemes->p_to = Message::MOUSE_RECEIVER;
575 mousemes->parameter = mpos.x;
576 mousemes->tag = mpos.y;
578 //mousemes->parameter = (mpos.x & 0xFFFF) << 16 | (mpos.y & 0xFFFF);
580 messageQueue->postMessage(mousemes);
582 //return DefWindowProc(wind,msg,wparam, lparam);
587 POINT mpos = { GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam) };
588 ClientToScreen(wind, &mpos);
589 if (TranslateMousePosition(&mpos))
591 Message* mousemes = new Message();
592 mousemes->message = Message::MOUSE_LBDOWN;
593 mousemes->from = NULL;
594 mousemes->p_to = Message::MOUSE_RECEIVER;
595 mousemes->parameter = mpos.x;
596 mousemes->tag = mpos.y;
598 //mousemes->parameter = (mpos.x & 0xFFFF) << 16 | (mpos.y & 0xFFFF);
600 messageQueue->postMessage(mousemes);
606 POINT mpos = { GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam) };
607 ClientToScreen(wind, &mpos);
608 if (TranslateMousePosition(&mpos))
610 Message* mousemes = new Message();
611 mousemes->message = Message::MOUSE_SCROLL;
612 mousemes->from = NULL;
613 mousemes->p_to = Message::MOUSE_RECEIVER;
614 mousemes->parameter = mpos.x;
615 mousemes->tag = mpos.y;
616 mousemes->data = GET_WHEEL_DELTA_WPARAM(wparam) & 0xFFFF; // filter prob not necessary
618 //mousemes->parameter = (0 & 0xFFFF) << 16 | (GET_WHEEL_DELTA_WPARAM(wparam) & 0xFFFF);
619 //mousemes->tag = (mpos.x & 0xFFFF) << 16 | (mpos.y & 0xFFFF);
620 messageQueue->postMessage(mousemes);
627 return DefWindowProc(wind, msg, wparam, lparam);
634 bool InitApp(HINSTANCE hinst,int cmdshow) {
638 wcs.style = CS_HREDRAW | CS_VREDRAW;
639 wcs.lpfnWndProc = WindowProc;
641 wcs.cbWndExtra = sizeof(DWORD);
642 wcs.hInstance = hinst;
645 wcs.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
646 wcs.lpszMenuName = NULL;
647 wcs.lpszClassName = "vomp";
648 acc=LoadAccelerators(hinst,MAKEINTRESOURCE(VOMPACCELERATOR));
649 if (!RegisterClass(&wcs))
651 flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
652 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
653 RECT wnted={50,50,768+50,576+50};
654 AdjustWindowRect(&wnted,flags ,false);
655 win_main=CreateWindow("vomp","VOMP on Windows",flags, CW_USEDEFAULT,CW_USEDEFAULT,
656 wnted.right-wnted.left,wnted.bottom-wnted.top,NULL,NULL,hinst,NULL);
659 ShowWindow(win_main,SW_SHOWNORMAL);
660 UpdateWindow(win_main);
661 /* in order to handle letterboxing we use a child window */
663 child_wcs.style = CS_HREDRAW | CS_VREDRAW;
664 child_wcs.lpfnWndProc = WindowProc;
665 child_wcs.cbClsExtra = 0;
666 child_wcs.cbWndExtra = sizeof(DWORD);
667 child_wcs.hInstance = hinst;
668 child_wcs.hIcon = NULL;
669 child_wcs.hCursor = NULL;
670 child_wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
671 child_wcs.lpszMenuName = NULL;
672 child_wcs.lpszClassName = "vomp_playback";
673 if (!RegisterClass(&child_wcs))
676 win=CreateWindow("vomp_playback","Vomp Playback Window",WS_VISIBLE | WS_CHILD |WS_CLIPCHILDREN,
677 0,0,768,576,win_main,NULL,hinst,NULL);
680 ShowWindow(win,SW_SHOWNORMAL);
682 if (remotefnc) {//at least windows XP
683 /* We want to support MCE Remote controls*/
684 RAWINPUTDEVICE remote_control_data[4];
685 ZeroMemory(remote_control_data,sizeof(remote_control_data));
686 remote_control_data[0].usUsagePage=0xFFBC;
687 remote_control_data[0].usUsage=0x88;
688 remote_control_data[0].dwFlags=0;
689 remote_control_data[1].usUsagePage=0x0C;
690 remote_control_data[1].usUsage=0x80;
691 remote_control_data[1].dwFlags=0;
692 remote_control_data[2].usUsagePage=0x0C;
693 remote_control_data[2].usUsage=0x01;
694 remote_control_data[2].dwFlags=0;
695 remote_control_data[3].usUsagePage=0x01;
696 remote_control_data[3].usUsage=0x80;
697 remote_control_data[3].dwFlags=0;
698 if (dynRegisterRawInputDevices(remote_control_data,4,sizeof(remote_control_data[0]))!=TRUE) {
699 MessageBox(0,"Registering remote control failed!","Aborting!",0);
711 // -------------------------------------------------------------------------------------------------------------------
713 void shutdown(int code)
715 if (control) // shut down control here in case views have posted messages
719 logger->log("Core", Log::NOTICE, "Control module shut down");
724 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
732 // -------------------------------------------------------------------------------------------------------------------