2 Copyright 2004-2005 Chris Tallon
\r
4 This file is part of VOMP.
\r
6 VOMP is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 VOMP is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with VOMP; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
26 #define _WIN32_WINNT 0x501
\r
27 #include <winsock2.h>
\r
28 #include <windows.h>
\r
30 #include "vompreswin.h"
\r
32 #include "defines.h"
\r
34 #include "remotewin.h"
\r
38 #include "videowin.h"
\r
39 #include "audiowin.h"
\r
42 #include "viewman.h"
\r
43 #include "command.h"
\r
45 void sighandler(int signalReceived);
\r
46 void shutdown(int code);
\r
48 // Global variables --------------------------------------------------------------------------------------------------
\r
49 int debugEnabled = 0;
\r
61 bool wnd_fullscreen=false;
\r
62 RECT wnd_fs_rect={20,20,768+20,576+20};
\r
63 RECT wnd_fs_rect_client={0,0,768,576};
\r
64 //OSVERSIONINFO windows_ver; //attempt to distigsh windows versions
\r
65 bool remotefnc=false;
\r
69 typedef UINT (*GETRAWINPUTDATAFNC) (HRAWINPUT,UINT,LPVOID,PUINT,UINT);
\r
70 typedef UINT (*REGISTERRAWINPUTDEVICEFNC) (PCRAWINPUTDEVICE,UINT,UINT);
\r
72 GETRAWINPUTDATAFNC dynGetRawInputData=NULL;
\r
73 REGISTERRAWINPUTDEVICEFNC dynRegisterRawInputDevices=NULL;
\r
76 void MILLISLEEP(ULONG a)
83 DWORD WINAPI commandthreadStart(void *arg)
\r
89 void LoadRemoteFunctions() {
\r
90 user32dll=LoadLibrary("user32.dll");
\r
91 if (user32dll!=NULL) {
\r
92 dynGetRawInputData=(GETRAWINPUTDATAFNC)GetProcAddress(user32dll,"GetRawInputData");
\r
93 if (dynGetRawInputData!=NULL) {
\r
94 dynRegisterRawInputDevices=(REGISTERRAWINPUTDEVICEFNC)GetProcAddress(user32dll,"RegisterRawInputDevices");
\r
95 if (dynRegisterRawInputDevices!=NULL) {
\r
102 bool InitApp(HINSTANCE hinst,int cmdshow);
\r
104 HWND win_main;//global window handle
\r
105 HWND win;//global child window handle
\r
108 #define ERROR_MSG(str) MessageBox(win_main,str,"Error!",MB_OK|MB_ICONWARNING)
\r
109 INT WINAPI WinMain( HINSTANCE hinst , HINSTANCE previnst, LPSTR cmdline, int cmdshow)
\r
111 //On Windows we have to init a window, we use DXUT
\r
112 LoadRemoteFunctions();
\r
113 if (!InitApp(hinst,cmdshow)) return false;
\r
114 //Starting Network support
\r
116 int result = WSAStartup(MAKEWORD(2,2),&wsadat);
\r
117 if (result!=NO_ERROR) {
\r
118 ERROR_MSG("Initialising WinSocked: Error at WSAStartup()\n");
\r
122 result= CoInitializeEx(NULL,COINIT_MULTITHREADED );//Initialize COM for DirectShow
\r
123 if (result!=S_OK) {
\r
124 ERROR_MSG("Initialising COM: Error at Coinitialize()\n");
\r
131 // Init global vars ------------------------------------------------------------------------------------------------
\r
133 logger = new Log();
\r
134 remote = new RemoteWin();
\r
135 mtd = new MtdWin();
\r
136 led = new LedWin();
\r
137 timers = new Timers();
\r
138 osd = new OsdWin();
\r
140 video = new VideoWin();
\r
141 audio = new AudioWin();
\r
142 viewman = new ViewMan();
\r
143 command = new Command();
\r
145 if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !viewman || !command)
\r
147 ERROR_MSG("Could not create objects. Memory problems?\n");
\r
153 // Get logging module started --------------------------------------------------------------------------------------
\r
155 if (!logger->init(Log::DEBUG, "vompwin.log", true))
\r
157 ERROR_MSG("Could not initialise log object. Aborting.\n");
\r
163 logger->log("Core", Log::INFO, "Starting up...");
\r
167 // Init modules ----------------------------------------------------------------------------------------------------
\r
170 success = remote->init("/dev/rawir");
\r
173 logger->log("Core", Log::INFO, "Remote module initialised");
\r
177 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
\r
183 success = led->init(0);
\r
186 logger->log("Core", Log::INFO, "LED module initialised");
\r
190 logger->log("Core", Log::EMERG, "LED module failed to initialise");
\r
196 success = mtd->init("/dev/mtd1");
\r
199 logger->log("Core", Log::INFO, "Mtd module initialised");
\r
203 logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
\r
209 success = timers->init();
\r
212 logger->log("Core", Log::INFO, "Timers module initialised");
\r
216 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
\r
222 UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
\r
223 if (videoFormat == Video::PAL) logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
\r
224 else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
\r
225 else logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
\r
227 success = video->init(videoFormat);
\r
230 logger->log("Core", Log::INFO, "Video module initialised");
\r
234 logger->log("Core", Log::EMERG, "Video module failed to initialise");
\r
240 success = osd->init((void*)&win);
\r
243 logger->log("Core", Log::INFO, "OSD module initialised");
\r
247 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
\r
253 success = audio->init(Audio::MPEG2_PES);
\r
256 logger->log("Core", Log::INFO, "Audio module initialised");
\r
260 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
\r
266 success = vdr->init(3024);
\r
269 logger->log("Core", Log::INFO, "VDR module initialised");
\r
273 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
\r
279 success = viewman->init();
\r
282 logger->log("Core", Log::INFO, "ViewMan module initialised");
\r
286 logger->log("Core", Log::EMERG, "ViewMan module failed to initialise");
\r
292 success = command->init();
\r
295 logger->log("Core", Log::INFO, "Command module initialised");
\r
299 logger->log("Core", Log::EMERG, "Command module failed to initialise");
\r
305 // Other init ------------------------------------------------------------------------------------------------------
\r
307 logger->log("Core", Log::NOTICE, "Startup successful");
\r
309 // Run main loop ---------------------------------------------------------------------------------------------------
\r
311 // Ok, all major device components and other bits are loaded and ready
\r
313 HANDLE commandthread;
\r
314 commandthread= CreateThread(NULL, 0, commandthreadStart, NULL,0,
\r
317 message.message=WM_NULL;
\r
319 while(run && WaitForSingleObject(commandthread,0)==WAIT_TIMEOUT) {
\r
320 if (PeekMessage(&message, NULL, 0,0,PM_REMOVE)!=0) {
\r
321 if (TranslateAccelerator(win_main,acc,&message)==NULL) {
\r
322 TranslateMessage(&message);
\r
323 DispatchMessage(&message);
\r
324 switch (message.message) {
\r
326 run=false; //TODO post exit to command Messages
\r
331 ((OsdWin*)osd)->Render();
\r
334 // When that returns quit ------------------------------------------------------------------------------------------
\r
335 WaitForSingleObject(commandthread,INFINITE);
\r
338 if (user32dll) FreeModule(user32dll);
\r
345 void CalculateWindowSize(RECT * size,ULONG size_mode) {
\r
347 DWORD width, height;
\r
348 DWORD adjheight,adjwidth;
\r
349 if (!wnd_fullscreen) {
\r
350 DWORD flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
\r
351 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
\r
352 RECT wnted={50,50,150,150};
\r
353 AdjustWindowRect(&wnted,flags ,false);
\r
354 adjwidth=-wnted.left+wnted.right-100;
\r
355 adjheight=-wnted.top+wnted.bottom-100;
\r
357 adjwidth=adjheight=0;
\r
359 width=size->right-size->left-adjwidth;
\r
360 height=size->bottom-size->top-adjheight;
\r
361 UCHAR mode=video->getMode();
\r
362 UCHAR aspect=((VideoWin*)video)->getAspectRatio();
\r
363 UCHAR tvsize=((VideoWin*)video)->getPseudoTVsize();
\r
364 double aspectrt=4./3.;
\r
365 if (tvsize==Video::ASPECT16X9) {
\r
366 if (aspect==Video::ASPECT16X9) {
\r
367 aspectrt=4./3.; //looks strange, but it is a 16:9 tv
\r
368 } else if (aspect==Video::ASPECT4X3) {
\r
369 aspectrt=4./3./(16./9.)*(4./3.); //I hope this is correct
\r
371 } else if (tvsize==Video::ASPECT4X3) {
\r
372 if (aspect==Video::ASPECT16X9) {
\r
373 if (mode!=Video::NORMAL) {
\r
378 } else if (aspect==Video::ASPECT4X3) {
\r
382 if (!wnd_fullscreen) {
\r
383 switch (size_mode) {
\r
385 case WMSZ_BOTTOMRIGHT:
\r
387 case WMSZ_TOPRIGHT:
\r
388 width=(ULONG)(((double)height)*aspectrt);
\r
389 size->right=size->left+width+adjwidth;
\r
391 case WMSZ_BOTTOMLEFT:
\r
393 width=(ULONG)(((double)height)*aspectrt);
\r
394 size->left=size->right-width-adjwidth;
\r
398 height=(ULONG)(((double)width)/aspectrt);
\r
399 size->bottom=size->top+height+adjheight;
\r
402 MoveWindow(win,0,0,width,height,TRUE);
\r
404 RECT newrect={0,0,width,height};
\r
406 if ((ULONG)(((double)height)*aspectrt)>width) {
\r
407 newlength=(ULONG)(((double)width)/aspectrt);
\r
408 newrect.top+=(height-newlength)/2;
\r
409 newrect.bottom-=(height-newlength);
\r
411 newlength=(ULONG)(((double)height)*aspectrt);
\r
412 newrect.left+=(width-newlength)/2;
\r
413 newrect.right-=(width-newlength);
\r
415 MoveWindow(win,newrect.left,newrect.top,newrect.right,newrect.bottom,TRUE);
\r
420 void AdjustWindow() {
\r
421 if (!wnd_fullscreen) {
\r
423 GetWindowRect(win_main,&winrect);
\r
424 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
\r
425 MoveWindow(win_main,winrect.left,
\r
426 winrect.top,winrect.right-winrect.left,winrect.bottom-winrect.top,true);
\r
429 GetWindowRect(win_main,&winrect);
\r
430 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
\r
435 void ToggleFullscreen() {
\r
436 if (wnd_fullscreen) {
\r
437 wnd_fullscreen=false;
\r
438 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
\r
439 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX);
\r
440 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
\r
442 SetWindowPos(win_main,HWND_TOP,wnd_fs_rect.left,wnd_fs_rect.top,
\r
443 wnd_fs_rect.right-wnd_fs_rect.left,
\r
444 wnd_fs_rect.bottom-wnd_fs_rect.top,
\r
445 SWP_DRAWFRAME | SWP_FRAMECHANGED);
\r
446 MoveWindow(win,wnd_fs_rect_client.left,wnd_fs_rect_client.top,
\r
447 wnd_fs_rect_client.right-wnd_fs_rect_client.left,
\r
448 wnd_fs_rect_client.bottom-wnd_fs_rect_client.top,TRUE);
\r
451 GetWindowRect(win_main,&wnd_fs_rect);
\r
452 GetWindowRect(win,&wnd_fs_rect_client);
\r
453 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP );
\r
454 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
\r
455 HMONITOR monitor=MonitorFromWindow(win_main,MONITOR_DEFAULTTONEAREST);
\r
456 MONITORINFO moninfo;
\r
457 moninfo.cbSize=sizeof(moninfo);
\r
458 wnd_fullscreen=true;
\r
459 if (!GetMonitorInfo(monitor,&moninfo)) return ;
\r
460 SetWindowPos(win_main,HWND_TOP,moninfo.rcMonitor.left,moninfo.rcMonitor.top,
\r
461 moninfo.rcMonitor.right,moninfo.rcMonitor.bottom,SWP_FRAMECHANGED);
\r
470 LONG FAR PASCAL WindowProc(HWND wind, UINT msg, WPARAM wparam, LPARAM lparam)
\r
475 //TODO: call command
\r
476 logger->log("Core", Log::NOTICE, "Window closed, shutting down...");
\r
478 ((RemoteWin*)Remote::getInstance())->SendPower();
\r
479 PostQuitMessage(0);
\r
482 CalculateWindowSize((RECT*) lparam,wparam);
\r
486 int width = LOWORD(lparam);
\r
487 int height = HIWORD(lparam);
\r
494 if (GetUpdateRect(wind, &r, FALSE)) {
\r
495 BeginPaint(wind, &ps);
\r
496 //Call Painting Mechanism
\r
497 EndPaint(wind, &ps);
\r
501 if (((RemoteWin*)remote)->ReceiveButtonVK(wparam)) {
\r
502 return 0L; //We process that Key
\r
504 return DefWindowProc(wind, msg, wparam, lparam);
\r
508 case WM_APPCOMMAND:
\r
509 if (((RemoteWin*)remote)->ReceiveButtonAP(GET_APPCOMMAND_LPARAM(lparam))){
\r
510 return TRUE; //yes we process that message
\r
512 return DefWindowProc(wind, msg, wparam, lparam);
\r
521 dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,NULL,&risize,sizeof(RAWINPUTHEADER));
\r
522 lpit=(LPRAWINPUT)malloc(risize);
\r
523 dynGetRawInputData((HRAWINPUT)lparam,RID_INPUT,lpit,&risize,sizeof(RAWINPUTHEADER));
\r
525 if (lpit->header.dwType==RIM_TYPEHID && lpit->data.hid.dwSizeHid>=2) {
\r
526 DWORD button=lpit->data.hid.bRawData[1] | (lpit->data.hid.bRawData[0]<< 8);
\r
527 if (((RemoteWin*)remote)->ReceiveButtonRI(button)){
\r
529 return 0; //yes we process that message
\r
534 return DefWindowProc(wind, msg, wparam, lparam);
\r
539 if (LOWORD(wparam)==VOMP_FULL_SCREEN) {
\r
540 ToggleFullscreen();
\r
543 if (((RemoteWin*)remote)->ReceiveButtonAP(LOWORD(wparam))){
\r
544 return 0; //yes we process that message
\r
546 return DefWindowProc(wind, msg, wparam, lparam);
\r
551 if (((HANDLE)wparam)==win) {
\r
555 return DefWindowProc(wind, msg, wparam, lparam);
\r
558 case WM_SYSCOMMAND:
\r
559 if (wparam==SC_MAXIMIZE) {
\r
560 ToggleFullscreen();
\r
563 return DefWindowProc(wind,msg,wparam, lparam);
\r
567 return DefWindowProc(wind, msg, wparam, lparam);
\r
573 bool InitApp(HINSTANCE hinst,int cmdshow) {
\r
577 wcs.style = CS_HREDRAW | CS_VREDRAW;
\r
578 wcs.lpfnWndProc = WindowProc;
\r
579 wcs.cbClsExtra = 0;
\r
580 wcs.cbWndExtra = sizeof(DWORD);
\r
581 wcs.hInstance = hinst;
\r
583 wcs.hCursor = NULL;
\r
584 wcs.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
\r
585 wcs.lpszMenuName = NULL;
\r
586 wcs.lpszClassName = "vomp";
\r
587 acc=LoadAccelerators(hinst,MAKEINTRESOURCE(VOMPACCELERATOR));
\r
588 if (!RegisterClass(&wcs))
\r
590 flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
\r
591 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
\r
592 RECT wnted={50,50,768+50,576+50};
\r
593 AdjustWindowRect(&wnted,flags ,false);
\r
594 win_main=CreateWindow("vomp","VOMP on Windows",flags, CW_USEDEFAULT,CW_USEDEFAULT,
\r
595 wnted.right-wnted.left,wnted.bottom-wnted.top,NULL,NULL,hinst,NULL);
\r
598 ShowWindow(win_main,SW_SHOWNORMAL);
\r
599 UpdateWindow(win_main);
\r
600 /* in order to handle letterboxing we use a child window */
\r
601 WNDCLASS child_wcs;
\r
602 child_wcs.style = CS_HREDRAW | CS_VREDRAW;
\r
603 child_wcs.lpfnWndProc = DefWindowProc;
\r
604 child_wcs.cbClsExtra = 0;
\r
605 child_wcs.cbWndExtra = sizeof(DWORD);
\r
606 child_wcs.hInstance = hinst;
\r
607 child_wcs.hIcon = NULL;
\r
608 child_wcs.hCursor = NULL;
\r
609 child_wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
\r
610 child_wcs.lpszMenuName = NULL;
\r
611 child_wcs.lpszClassName = "vomp_playback";
\r
612 if (!RegisterClass(&child_wcs))
\r
615 win=CreateWindow("vomp_playback","Vomp Playback Window",WS_VISIBLE | WS_CHILD |WS_CLIPCHILDREN,
\r
616 0,0,768,576,win_main,NULL,hinst,NULL);
\r
619 ShowWindow(win,SW_SHOWNORMAL);
\r
621 if (remotefnc) {//at least windows XP
\r
622 /* We want to support MCE Remote controls*/
\r
623 RAWINPUTDEVICE remote_control_data[4];
\r
624 ZeroMemory(remote_control_data,sizeof(remote_control_data));
\r
625 remote_control_data[0].usUsagePage=0xFFBC;
\r
626 remote_control_data[0].usUsage=0x88;
\r
627 remote_control_data[0].dwFlags=0;
\r
628 remote_control_data[1].usUsagePage=0x0C;
\r
629 remote_control_data[1].usUsage=0x80;
\r
630 remote_control_data[1].dwFlags=0;
\r
631 remote_control_data[2].usUsagePage=0x0C;
\r
632 remote_control_data[2].usUsage=0x01;
\r
633 remote_control_data[2].dwFlags=0;
\r
634 remote_control_data[3].usUsagePage=0x01;
\r
635 remote_control_data[3].usUsage=0x80;
\r
636 remote_control_data[3].dwFlags=0;
\r
637 if (dynRegisterRawInputDevices(remote_control_data,4,sizeof(remote_control_data[0]))!=TRUE) {
\r
638 MessageBox(0,"Registering remote control failed!","Aborting!",0);
\r
650 // -------------------------------------------------------------------------------------------------------------------
\r
652 void shutdown(int code)
\r
656 viewman->shutdown();
\r
658 logger->log("Core", Log::NOTICE, "ViewMan module shut down");
\r
661 if (command) // shut down command here in case views have posted messages
\r
663 command->shutdown();
\r
665 logger->log("Core", Log::NOTICE, "Command module shut down");
\r
672 logger->log("Core", Log::NOTICE, "VDR module shut down");
\r
679 logger->log("Core", Log::NOTICE, "OSD module shut down");
\r
686 logger->log("Core", Log::NOTICE, "Audio module shut down");
\r
693 logger->log("Core", Log::NOTICE, "Video module shut down");
\r
698 timers->shutdown();
\r
700 logger->log("Core", Log::NOTICE, "Timers module shut down");
\r
707 logger->log("Core", Log::NOTICE, "MTD module shut down");
\r
714 logger->log("Core", Log::NOTICE, "LED module shut down");
\r
719 remote->shutdown();
\r
721 logger->log("Core", Log::NOTICE, "Remote module shut down");
\r
726 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
\r
727 logger->shutdown();
\r
734 // -------------------------------------------------------------------------------------------------------------------
\r
736 ULLONG ntohll(ULLONG a)
\r
741 ULLONG htonll(ULLONG a)
\r
743 return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32)
\r
744 |(ULONG)htonl(((ULONG) (a >> 32))));
\r