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
67 void MILLISLEEP(ULONG a)
74 DWORD WINAPI commandthreadStart(void *arg)
\r
80 bool InitApp(HINSTANCE hinst,int cmdshow);
\r
82 HWND win_main;//global window handle
\r
83 HWND win;//global child window handle
\r
86 #define ERROR_MSG(str) MessageBox(win_main,str,"Error!",MB_OK|MB_ICONWARNING)
\r
87 INT WINAPI WinMain( HINSTANCE hinst , HINSTANCE previnst, LPSTR cmdline, int cmdshow)
\r
89 //On Windows we have to init a window, we use DXUT
\r
90 if (!InitApp(hinst,cmdshow)) return false;
\r
91 //Starting Network support
\r
93 int result = WSAStartup(MAKEWORD(2,2),&wsadat);
\r
94 if (result!=NO_ERROR) {
\r
95 ERROR_MSG("Initialising WinSocked: Error at WSAStartup()\n");
\r
98 result= CoInitializeEx(NULL,COINIT_MULTITHREADED );//Initialize COM for DirectShow
\r
100 ERROR_MSG("Initialising COM: Error at Coinitialize()\n");
\r
107 // Init global vars ------------------------------------------------------------------------------------------------
\r
109 logger = new Log();
\r
110 remote = new RemoteWin();
\r
111 mtd = new MtdWin();
\r
112 led = new LedWin();
\r
113 timers = new Timers();
\r
114 osd = new OsdWin();
\r
116 video = new VideoWin();
\r
117 audio = new AudioWin();
\r
118 viewman = new ViewMan();
\r
119 command = new Command();
\r
121 if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !viewman || !command)
\r
123 ERROR_MSG("Could not create objects. Memory problems?\n");
\r
129 // Get logging module started --------------------------------------------------------------------------------------
\r
131 if (!logger->init(Log::DEBUG, "vompwin.log", true))
\r
133 ERROR_MSG("Could not initialise log object. Aborting.\n");
\r
139 logger->log("Core", Log::INFO, "Starting up...");
\r
143 // Init modules ----------------------------------------------------------------------------------------------------
\r
146 success = remote->init("/dev/rawir");
\r
149 logger->log("Core", Log::INFO, "Remote module initialised");
\r
153 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
\r
159 success = led->init(0);
\r
162 logger->log("Core", Log::INFO, "LED module initialised");
\r
166 logger->log("Core", Log::EMERG, "LED module failed to initialise");
\r
172 success = mtd->init("/dev/mtd1");
\r
175 logger->log("Core", Log::INFO, "Mtd module initialised");
\r
179 logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
\r
185 success = timers->init();
\r
188 logger->log("Core", Log::INFO, "Timers module initialised");
\r
192 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
\r
198 UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
\r
199 if (videoFormat == Video::PAL) logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
\r
200 else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
\r
201 else logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
\r
203 success = video->init(videoFormat);
\r
206 logger->log("Core", Log::INFO, "Video module initialised");
\r
210 logger->log("Core", Log::EMERG, "Video module failed to initialise");
\r
216 success = osd->init((void*)&win);
\r
219 logger->log("Core", Log::INFO, "OSD module initialised");
\r
223 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
\r
229 success = audio->init(Audio::MPEG2_PES);
\r
232 logger->log("Core", Log::INFO, "Audio module initialised");
\r
236 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
\r
242 success = vdr->init(3024);
\r
245 logger->log("Core", Log::INFO, "VDR module initialised");
\r
249 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
\r
255 success = viewman->init();
\r
258 logger->log("Core", Log::INFO, "ViewMan module initialised");
\r
262 logger->log("Core", Log::EMERG, "ViewMan module failed to initialise");
\r
268 success = command->init();
\r
271 logger->log("Core", Log::INFO, "Command module initialised");
\r
275 logger->log("Core", Log::EMERG, "Command module failed to initialise");
\r
281 // Other init ------------------------------------------------------------------------------------------------------
\r
283 logger->log("Core", Log::NOTICE, "Startup successful");
\r
285 // Run main loop ---------------------------------------------------------------------------------------------------
\r
287 // Ok, all major device components and other bits are loaded and ready
\r
289 HANDLE commandthread;
\r
290 commandthread= CreateThread(NULL, 0, commandthreadStart, NULL,0,
\r
293 message.message=WM_NULL;
\r
295 while(run && WaitForSingleObject(commandthread,0)==WAIT_TIMEOUT) {
\r
296 if (PeekMessage(&message, NULL, 0,0,PM_REMOVE)!=0) {
\r
297 if (TranslateAccelerator(win_main,acc,&message)==NULL) {
\r
298 TranslateMessage(&message);
\r
299 DispatchMessage(&message);
\r
300 switch (message.message) {
\r
302 run=false; //TODO post exit to command Messages
\r
307 ((OsdWin*)osd)->Render();
\r
310 // When that returns quit ------------------------------------------------------------------------------------------
\r
311 WaitForSingleObject(commandthread,INFINITE);
\r
320 void CalculateWindowSize(RECT * size,ULONG size_mode) {
\r
322 DWORD width, height;
\r
323 DWORD adjheight,adjwidth;
\r
324 if (!wnd_fullscreen) {
\r
325 DWORD flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
\r
326 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
\r
327 RECT wnted={50,50,150,150};
\r
328 AdjustWindowRect(&wnted,flags ,false);
\r
329 adjwidth=-wnted.left+wnted.right-100;
\r
330 adjheight=-wnted.top+wnted.bottom-100;
\r
332 adjwidth=adjheight=0;
\r
334 width=size->right-size->left-adjwidth;
\r
335 height=size->bottom-size->top-adjheight;
\r
336 UCHAR mode=video->getMode();
\r
337 UCHAR aspect=((VideoWin*)video)->getAspectRatio();
\r
338 UCHAR tvsize=((VideoWin*)video)->getPseudoTVsize();
\r
339 double aspectrt=4./3.;
\r
340 if (tvsize==Video::ASPECT16X9) {
\r
341 if (aspect==Video::ASPECT16X9) {
\r
342 aspectrt=4./3.; //looks strange, but it is a 16:9 tv
\r
343 } else if (aspect==Video::ASPECT4X3) {
\r
344 aspectrt=4./3./(16./9.)*(4./3.); //I hope this is correct
\r
346 } else if (tvsize==Video::ASPECT4X3) {
\r
347 if (aspect==Video::ASPECT16X9) {
\r
348 if (mode!=Video::NORMAL) {
\r
353 } else if (aspect==Video::ASPECT4X3) {
\r
357 if (!wnd_fullscreen) {
\r
358 switch (size_mode) {
\r
360 case WMSZ_BOTTOMRIGHT:
\r
362 case WMSZ_TOPRIGHT:
\r
363 width=(ULONG)(((double)height)*aspectrt);
\r
364 size->right=size->left+width+adjwidth;
\r
366 case WMSZ_BOTTOMLEFT:
\r
368 width=(ULONG)(((double)height)*aspectrt);
\r
369 size->left=size->right-width-adjwidth;
\r
373 height=(ULONG)(((double)width)/aspectrt);
\r
374 size->bottom=size->top+height+adjheight;
\r
377 MoveWindow(win,0,0,width,height,TRUE);
\r
379 RECT newrect={0,0,width,height};
\r
381 if ((ULONG)(((double)height)*aspectrt)>width) {
\r
382 newlength=(ULONG)(((double)width)/aspectrt);
\r
383 newrect.top+=(height-newlength)/2;
\r
384 newrect.bottom-=(height-newlength);
\r
386 newlength=(ULONG)(((double)height)*aspectrt);
\r
387 newrect.left+=(width-newlength)/2;
\r
388 newrect.right-=(width-newlength);
\r
390 MoveWindow(win,newrect.left,newrect.top,newrect.right,newrect.bottom,TRUE);
\r
395 void AdjustWindow() {
\r
396 if (!wnd_fullscreen) {
\r
398 GetWindowRect(win_main,&winrect);
\r
399 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
\r
400 MoveWindow(win_main,winrect.left,
\r
401 winrect.top,winrect.right-winrect.left,winrect.bottom-winrect.top,true);
\r
404 GetWindowRect(win_main,&winrect);
\r
405 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
\r
410 void ToggleFullscreen() {
\r
411 if (wnd_fullscreen) {
\r
412 wnd_fullscreen=false;
\r
413 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
\r
414 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX);
\r
415 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
\r
417 SetWindowPos(win_main,HWND_TOP,wnd_fs_rect.left,wnd_fs_rect.top,
\r
418 wnd_fs_rect.right-wnd_fs_rect.left,
\r
419 wnd_fs_rect.bottom-wnd_fs_rect.top,
\r
420 SWP_DRAWFRAME | SWP_FRAMECHANGED);
\r
421 MoveWindow(win,wnd_fs_rect_client.left,wnd_fs_rect_client.top,
\r
422 wnd_fs_rect_client.right-wnd_fs_rect_client.left,
\r
423 wnd_fs_rect_client.bottom-wnd_fs_rect_client.top,TRUE);
\r
426 GetWindowRect(win_main,&wnd_fs_rect);
\r
427 GetWindowRect(win,&wnd_fs_rect_client);
\r
428 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP );
\r
429 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
\r
430 HMONITOR monitor=MonitorFromWindow(win_main,MONITOR_DEFAULTTONEAREST);
\r
431 MONITORINFO moninfo;
\r
432 moninfo.cbSize=sizeof(moninfo);
\r
433 wnd_fullscreen=true;
\r
434 if (!GetMonitorInfo(monitor,&moninfo)) return ;
\r
435 SetWindowPos(win_main,HWND_TOP,moninfo.rcMonitor.left,moninfo.rcMonitor.top,
\r
436 moninfo.rcMonitor.right,moninfo.rcMonitor.bottom,SWP_FRAMECHANGED);
\r
445 LONG FAR PASCAL WindowProc(HWND wind, UINT msg, WPARAM wparam, LPARAM lparam)
\r
450 //TODO: call command
\r
451 logger->log("Core", Log::NOTICE, "Window closed, shutting down...");
\r
453 ((RemoteWin*)Remote::getInstance())->SendPower();
\r
454 PostQuitMessage(0);
\r
457 CalculateWindowSize((RECT*) lparam,wparam);
\r
461 int width = LOWORD(lparam);
\r
462 int height = HIWORD(lparam);
\r
469 if (GetUpdateRect(wind, &r, FALSE)) {
\r
470 BeginPaint(wind, &ps);
\r
471 //Call Painting Mechanism
\r
472 EndPaint(wind, &ps);
\r
476 if (((RemoteWin*)remote)->ReceiveButtonVK(wparam)) {
\r
477 return 0L; //We process that Key
\r
479 return DefWindowProc(wind, msg, wparam, lparam);
\r
483 case WM_APPCOMMAND:
\r
484 if (((RemoteWin*)remote)->ReceiveButtonAP(GET_APPCOMMAND_LPARAM(lparam))){
\r
485 return TRUE; //yes we process that message
\r
487 return DefWindowProc(wind, msg, wparam, lparam);
\r
492 if ((windows_ver.dwMajorVersion>5 ||
\r
493 (windows_ver.dwMajorVersion==5 && windows_ver.dwMinorVersion>=1)) ) {
\r
497 GetRawInputData((HRAWINPUT)lparam,RID_INPUT,NULL,&risize,sizeof(RAWINPUTHEADER));
\r
498 lpit=(LPRAWINPUT)malloc(risize);
\r
499 GetRawInputData((HRAWINPUT)lparam,RID_INPUT,lpit,&risize,sizeof(RAWINPUTHEADER));
\r
501 if (lpit->header.dwType==RIM_TYPEHID && lpit->data.hid.dwSizeHid>=2) {
\r
502 DWORD button=lpit->data.hid.bRawData[1] | (lpit->data.hid.bRawData[0]<< 8);
\r
503 if (((RemoteWin*)remote)->ReceiveButtonRI(button)){
\r
505 return 0; //yes we process that message
\r
510 return DefWindowProc(wind, msg, wparam, lparam);
\r
515 if (LOWORD(wparam)==VOMP_FULL_SCREEN) {
\r
516 ToggleFullscreen();
\r
519 if (((RemoteWin*)remote)->ReceiveButtonAP(LOWORD(wparam))){
\r
520 return 0; //yes we process that message
\r
522 return DefWindowProc(wind, msg, wparam, lparam);
\r
527 if (((HANDLE)wparam)==win) {
\r
531 return DefWindowProc(wind, msg, wparam, lparam);
\r
534 case WM_SYSCOMMAND:
\r
535 if (wparam==SC_MAXIMIZE) {
\r
536 ToggleFullscreen();
\r
539 return DefWindowProc(wind,msg,wparam, lparam);
\r
543 return DefWindowProc(wind, msg, wparam, lparam);
\r
549 bool InitApp(HINSTANCE hinst,int cmdshow) {
\r
551 windows_ver.dwOSVersionInfoSize=sizeof(windows_ver);
\r
552 GetVersionEx(&windows_ver);
\r
555 wcs.style = CS_HREDRAW | CS_VREDRAW;
\r
556 wcs.lpfnWndProc = WindowProc;
\r
557 wcs.cbClsExtra = 0;
\r
558 wcs.cbWndExtra = sizeof(DWORD);
\r
559 wcs.hInstance = hinst;
\r
561 wcs.hCursor = NULL;
\r
562 wcs.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
\r
563 wcs.lpszMenuName = NULL;
\r
564 wcs.lpszClassName = "vomp";
\r
565 acc=LoadAccelerators(hinst,MAKEINTRESOURCE(VOMPACCELERATOR));
\r
566 if (!RegisterClass(&wcs))
\r
568 flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
\r
569 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
\r
570 RECT wnted={50,50,768+50,576+50};
\r
571 AdjustWindowRect(&wnted,flags ,false);
\r
572 win_main=CreateWindow("vomp","VOMP on Windows",flags, CW_USEDEFAULT,CW_USEDEFAULT,
\r
573 wnted.right-wnted.left,wnted.bottom-wnted.top,NULL,NULL,hinst,NULL);
\r
576 ShowWindow(win_main,SW_SHOWNORMAL);
\r
577 UpdateWindow(win_main);
\r
578 /* in order to handle letterboxing we use a child window */
\r
579 WNDCLASS child_wcs;
\r
580 child_wcs.style = CS_HREDRAW | CS_VREDRAW;
\r
581 child_wcs.lpfnWndProc = DefWindowProc;
\r
582 child_wcs.cbClsExtra = 0;
\r
583 child_wcs.cbWndExtra = sizeof(DWORD);
\r
584 child_wcs.hInstance = hinst;
\r
585 child_wcs.hIcon = NULL;
\r
586 child_wcs.hCursor = NULL;
\r
587 child_wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
\r
588 child_wcs.lpszMenuName = NULL;
\r
589 child_wcs.lpszClassName = "vomp_playback";
\r
590 if (!RegisterClass(&child_wcs))
\r
593 win=CreateWindow("vomp_playback","Vomp Playback Window",WS_VISIBLE | WS_CHILD |WS_CLIPCHILDREN,
\r
594 0,0,768,576,win_main,NULL,hinst,NULL);
\r
597 ShowWindow(win,SW_SHOWNORMAL);
\r
599 if (windows_ver.dwMajorVersion>5 ||
\r
600 ( windows_ver.dwMajorVersion==5 && windows_ver.dwMinorVersion>=1)) {//at least windows XP
\r
601 /* We want to support MCE Remote controls*/
\r
602 RAWINPUTDEVICE remote_control_data[4];
\r
603 ZeroMemory(remote_control_data,sizeof(remote_control_data));
\r
604 remote_control_data[0].usUsagePage=0xFFBC;
\r
605 remote_control_data[0].usUsage=0x88;
\r
606 remote_control_data[0].dwFlags=0;
\r
607 remote_control_data[1].usUsagePage=0x0C;
\r
608 remote_control_data[1].usUsage=0x80;
\r
609 remote_control_data[1].dwFlags=0;
\r
610 remote_control_data[2].usUsagePage=0x0C;
\r
611 remote_control_data[2].usUsage=0x01;
\r
612 remote_control_data[2].dwFlags=0;
\r
613 remote_control_data[3].usUsagePage=0x01;
\r
614 remote_control_data[3].usUsage=0x80;
\r
615 remote_control_data[3].dwFlags=0;
\r
616 if (RegisterRawInputDevices(remote_control_data,4,sizeof(remote_control_data[0]))!=TRUE) {
\r
617 MessageBox(0,"Registering remote control failed!","Aborting!",0);
\r
629 // -------------------------------------------------------------------------------------------------------------------
\r
631 void shutdown(int code)
\r
635 viewman->shutdown();
\r
637 logger->log("Core", Log::NOTICE, "ViewMan module shut down");
\r
640 if (command) // shut down command here in case views have posted messages
\r
642 command->shutdown();
\r
644 logger->log("Core", Log::NOTICE, "Command module shut down");
\r
651 logger->log("Core", Log::NOTICE, "VDR module shut down");
\r
658 logger->log("Core", Log::NOTICE, "OSD module shut down");
\r
665 logger->log("Core", Log::NOTICE, "Audio module shut down");
\r
672 logger->log("Core", Log::NOTICE, "Video module shut down");
\r
677 timers->shutdown();
\r
679 logger->log("Core", Log::NOTICE, "Timers module shut down");
\r
686 logger->log("Core", Log::NOTICE, "MTD module shut down");
\r
693 logger->log("Core", Log::NOTICE, "LED module shut down");
\r
698 remote->shutdown();
\r
700 logger->log("Core", Log::NOTICE, "Remote module shut down");
\r
705 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
\r
706 logger->shutdown();
\r
713 // -------------------------------------------------------------------------------------------------------------------
\r
715 ULLONG ntohll(ULLONG a)
\r
720 ULLONG htonll(ULLONG a)
\r
722 return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32)
\r
723 |(ULONG)htonl(((ULONG) (a >> 32))));
\r