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
66 void MILLISLEEP(ULONG a)
73 DWORD WINAPI commandthreadStart(void *arg)
\r
79 bool InitApp(HINSTANCE hinst,int cmdshow);
\r
81 HWND win_main;//global window handle
\r
82 HWND win;//global child window handle
\r
85 #define ERROR_MSG(str) MessageBox(win_main,str,"Error!",MB_OK|MB_ICONWARNING)
\r
86 INT WINAPI WinMain( HINSTANCE hinst , HINSTANCE previnst, LPSTR cmdline, int cmdshow)
\r
88 //On Windows we have to init a window, we use DXUT
\r
89 if (!InitApp(hinst,cmdshow)) return false;
\r
90 //Starting Network support
\r
92 int result = WSAStartup(MAKEWORD(2,2),&wsadat);
\r
93 if (result!=NO_ERROR) {
\r
94 ERROR_MSG("Initialising WinSocked: Error at WSAStartup()\n");
\r
97 result= CoInitializeEx(NULL,COINIT_MULTITHREADED );//Initialize COM for DirectShow
\r
99 ERROR_MSG("Initialising COM: Error at Coinitialize()\n");
\r
106 // Init global vars ------------------------------------------------------------------------------------------------
\r
108 logger = new Log();
\r
109 remote = new RemoteWin();
\r
110 mtd = new MtdWin();
\r
111 led = new LedWin();
\r
112 timers = new Timers();
\r
113 osd = new OsdWin();
\r
115 video = new VideoWin();
\r
116 audio = new AudioWin();
\r
117 viewman = new ViewMan();
\r
118 command = new Command();
\r
120 if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !viewman || !command)
\r
122 ERROR_MSG("Could not create objects. Memory problems?\n");
\r
128 // Get logging module started --------------------------------------------------------------------------------------
\r
130 if (!logger->init(Log::DEBUG, "vompwin.log", true))
\r
132 ERROR_MSG("Could not initialise log object. Aborting.\n");
\r
138 logger->log("Core", Log::INFO, "Starting up...");
\r
142 // Init modules ----------------------------------------------------------------------------------------------------
\r
145 success = remote->init("/dev/rawir");
\r
148 logger->log("Core", Log::INFO, "Remote module initialised");
\r
152 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
\r
158 success = led->init(0);
\r
161 logger->log("Core", Log::INFO, "LED module initialised");
\r
165 logger->log("Core", Log::EMERG, "LED module failed to initialise");
\r
171 success = mtd->init("/dev/mtd1");
\r
174 logger->log("Core", Log::INFO, "Mtd module initialised");
\r
178 logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
\r
184 success = timers->init();
\r
187 logger->log("Core", Log::INFO, "Timers module initialised");
\r
191 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
\r
197 UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
\r
198 if (videoFormat == Video::PAL) logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
\r
199 else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
\r
200 else logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
\r
202 success = video->init(videoFormat);
\r
205 logger->log("Core", Log::INFO, "Video module initialised");
\r
209 logger->log("Core", Log::EMERG, "Video module failed to initialise");
\r
215 success = osd->init((void*)&win);
\r
218 logger->log("Core", Log::INFO, "OSD module initialised");
\r
222 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
\r
228 success = audio->init(Audio::MPEG2_PES);
\r
231 logger->log("Core", Log::INFO, "Audio module initialised");
\r
235 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
\r
241 success = vdr->init(3024);
\r
244 logger->log("Core", Log::INFO, "VDR module initialised");
\r
248 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
\r
254 success = viewman->init();
\r
257 logger->log("Core", Log::INFO, "ViewMan module initialised");
\r
261 logger->log("Core", Log::EMERG, "ViewMan module failed to initialise");
\r
267 success = command->init();
\r
270 logger->log("Core", Log::INFO, "Command module initialised");
\r
274 logger->log("Core", Log::EMERG, "Command module failed to initialise");
\r
280 // Other init ------------------------------------------------------------------------------------------------------
\r
282 logger->log("Core", Log::NOTICE, "Startup successful");
\r
284 // Run main loop ---------------------------------------------------------------------------------------------------
\r
286 // Ok, all major device components and other bits are loaded and ready
\r
288 HANDLE commandthread;
\r
289 commandthread= CreateThread(NULL, 0, commandthreadStart, NULL,0,
\r
292 message.message=WM_NULL;
\r
294 while(run && WaitForSingleObject(commandthread,0)==WAIT_TIMEOUT) {
\r
295 if (PeekMessage(&message, NULL, 0,0,PM_REMOVE)!=0) {
\r
296 if (TranslateAccelerator(win_main,acc,&message)==NULL) {
\r
297 TranslateMessage(&message);
\r
298 DispatchMessage(&message);
\r
299 switch (message.message) {
\r
301 run=false; //TODO post exit to command Messages
\r
306 ((OsdWin*)osd)->Render();
\r
309 // When that returns quit ------------------------------------------------------------------------------------------
\r
310 WaitForSingleObject(commandthread,INFINITE);
\r
319 void CalculateWindowSize(RECT * size,ULONG size_mode) {
\r
321 DWORD width, height;
\r
322 DWORD adjheight,adjwidth;
\r
323 if (!wnd_fullscreen) {
\r
324 DWORD flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
\r
325 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
\r
326 RECT wnted={50,50,150,150};
\r
327 AdjustWindowRect(&wnted,flags ,false);
\r
328 adjwidth=-wnted.left+wnted.right-100;
\r
329 adjheight=-wnted.top+wnted.bottom-100;
\r
331 adjwidth=adjheight=0;
\r
333 width=size->right-size->left-adjwidth;
\r
334 height=size->bottom-size->top-adjheight;
\r
335 UCHAR mode=video->getMode();
\r
336 UCHAR aspect=((VideoWin*)video)->getAspectRatio();
\r
337 UCHAR tvsize=((VideoWin*)video)->getPseudoTVsize();
\r
338 double aspectrt=4./3.;
\r
339 if (tvsize==Video::ASPECT16X9) {
\r
340 if (aspect==Video::ASPECT16X9) {
\r
341 aspectrt=4./3.; //looks strange, but it is a 16:9 tv
\r
342 } else if (aspect==Video::ASPECT4X3) {
\r
343 aspectrt=4./3./(16./9.)*(4./3.); //I hope this is correct
\r
345 } else if (tvsize==Video::ASPECT4X3) {
\r
346 if (aspect==Video::ASPECT16X9) {
\r
347 if (mode!=Video::NORMAL) {
\r
352 } else if (aspect==Video::ASPECT4X3) {
\r
356 if (!wnd_fullscreen) {
\r
357 switch (size_mode) {
\r
359 case WMSZ_BOTTOMRIGHT:
\r
361 case WMSZ_TOPRIGHT:
\r
362 width=(ULONG)(((double)height)*aspectrt);
\r
363 size->right=size->left+width+adjwidth;
\r
365 case WMSZ_BOTTOMLEFT:
\r
367 width=(ULONG)(((double)height)*aspectrt);
\r
368 size->left=size->right-width-adjwidth;
\r
372 height=(ULONG)(((double)width)/aspectrt);
\r
373 size->bottom=size->top+height+adjheight;
\r
376 MoveWindow(win,0,0,width,height,TRUE);
\r
378 RECT newrect={0,0,width,height};
\r
380 if ((ULONG)(((double)height)*aspectrt)>width) {
\r
381 newlength=(ULONG)(((double)width)/aspectrt);
\r
382 newrect.top+=(height-newlength)/2;
\r
383 newrect.bottom-=(height-newlength);
\r
385 newlength=(ULONG)(((double)height)*aspectrt);
\r
386 newrect.left+=(width-newlength)/2;
\r
387 newrect.right-=(width-newlength);
\r
389 MoveWindow(win,newrect.left,newrect.top,newrect.right,newrect.bottom,TRUE);
\r
394 void AdjustWindow() {
\r
395 if (!wnd_fullscreen) {
\r
397 GetWindowRect(win_main,&winrect);
\r
398 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
\r
399 MoveWindow(win_main,winrect.left,
\r
400 winrect.top,winrect.right-winrect.left,winrect.bottom-winrect.top,true);
\r
403 GetWindowRect(win_main,&winrect);
\r
404 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
\r
409 void ToggleFullscreen() {
\r
410 if (wnd_fullscreen) {
\r
411 wnd_fullscreen=false;
\r
412 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
\r
413 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX);
\r
414 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
\r
416 SetWindowPos(win_main,HWND_TOP,wnd_fs_rect.left,wnd_fs_rect.top,
\r
417 wnd_fs_rect.right-wnd_fs_rect.left,
\r
418 wnd_fs_rect.bottom-wnd_fs_rect.top,
\r
419 SWP_DRAWFRAME | SWP_FRAMECHANGED);
\r
420 MoveWindow(win,wnd_fs_rect_client.left,wnd_fs_rect_client.top,
\r
421 wnd_fs_rect_client.right-wnd_fs_rect_client.left,
\r
422 wnd_fs_rect_client.bottom-wnd_fs_rect_client.top,TRUE);
\r
425 GetWindowRect(win_main,&wnd_fs_rect);
\r
426 GetWindowRect(win,&wnd_fs_rect_client);
\r
427 SetWindowLong(win_main,GWL_STYLE,WS_VISIBLE | WS_POPUP );
\r
428 SetWindowPos(win_main,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
\r
429 HMONITOR monitor=MonitorFromWindow(win_main,MONITOR_DEFAULTTONEAREST);
\r
430 MONITORINFO moninfo;
\r
431 moninfo.cbSize=sizeof(moninfo);
\r
432 wnd_fullscreen=true;
\r
433 if (!GetMonitorInfo(monitor,&moninfo)) return ;
\r
434 SetWindowPos(win_main,HWND_TOP,moninfo.rcMonitor.left,moninfo.rcMonitor.top,
\r
435 moninfo.rcMonitor.right,moninfo.rcMonitor.bottom,SWP_FRAMECHANGED);
\r
444 LONG FAR PASCAL WindowProc(HWND wind, UINT msg, WPARAM wparam, LPARAM lparam)
\r
448 //TODO: call command
\r
449 logger->log("Core", Log::NOTICE, "Window closed, shutting down...");
\r
451 ((RemoteWin*)Remote::getInstance())->SendPower();
\r
452 PostQuitMessage(0);
\r
455 CalculateWindowSize((RECT*) lparam,wparam);
\r
459 int width = LOWORD(lparam);
\r
460 int height = HIWORD(lparam);
\r
467 if (GetUpdateRect(wind, &r, FALSE)) {
\r
468 BeginPaint(wind, &ps);
\r
469 //Call Painting Mechanism
\r
470 EndPaint(wind, &ps);
\r
474 if (((RemoteWin*)remote)->ReceiveButtonVK(wparam)) {
\r
475 return 0L; //We process that Key
\r
477 return DefWindowProc(wind, msg, wparam, lparam);
\r
481 case WM_APPCOMMAND:
\r
482 if (((RemoteWin*)remote)->ReceiveButtonAP(GET_APPCOMMAND_LPARAM(lparam))){
\r
483 return TRUE; //yes we process that message
\r
485 return DefWindowProc(wind, msg, wparam, lparam);
\r
490 if (LOWORD(wparam)==VOMP_FULL_SCREEN) {
\r
491 ToggleFullscreen();
\r
494 if (((RemoteWin*)remote)->ReceiveButtonAP(LOWORD(wparam))){
\r
495 return 0; //yes we process that message
\r
497 return DefWindowProc(wind, msg, wparam, lparam);
\r
502 if (((HANDLE)wparam)==win) {
\r
506 return DefWindowProc(wind, msg, wparam, lparam);
\r
509 case WM_SYSCOMMAND:
\r
510 if (wparam==SC_MAXIMIZE) {
\r
511 ToggleFullscreen();
\r
514 return DefWindowProc(wind,msg,wparam, lparam);
\r
518 return DefWindowProc(wind, msg, wparam, lparam);
\r
524 bool InitApp(HINSTANCE hinst,int cmdshow) {
\r
528 wcs.style = CS_HREDRAW | CS_VREDRAW;
\r
529 wcs.lpfnWndProc = WindowProc;
\r
530 wcs.cbClsExtra = 0;
\r
531 wcs.cbWndExtra = sizeof(DWORD);
\r
532 wcs.hInstance = hinst;
\r
534 wcs.hCursor = NULL;
\r
535 wcs.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
\r
536 wcs.lpszMenuName = NULL;
\r
537 wcs.lpszClassName = "vomp";
\r
538 acc=LoadAccelerators(hinst,MAKEINTRESOURCE(VOMPACCELERATOR));
\r
539 if (!RegisterClass(&wcs))
\r
541 flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
\r
542 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
\r
543 RECT wnted={50,50,768+50,576+50};
\r
544 AdjustWindowRect(&wnted,flags ,false);
\r
545 win_main=CreateWindow("vomp","VOMP on Windows",flags, CW_USEDEFAULT,CW_USEDEFAULT,
\r
546 wnted.right-wnted.left,wnted.bottom-wnted.top,NULL,NULL,hinst,NULL);
\r
549 ShowWindow(win_main,SW_SHOWNORMAL);
\r
550 UpdateWindow(win_main);
\r
551 /* in order to handle letterboxing we use a child window */
\r
552 WNDCLASS child_wcs;
\r
553 child_wcs.style = CS_HREDRAW | CS_VREDRAW;
\r
554 child_wcs.lpfnWndProc = DefWindowProc;
\r
555 child_wcs.cbClsExtra = 0;
\r
556 child_wcs.cbWndExtra = sizeof(DWORD);
\r
557 child_wcs.hInstance = hinst;
\r
558 child_wcs.hIcon = NULL;
\r
559 child_wcs.hCursor = NULL;
\r
560 child_wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
\r
561 child_wcs.lpszMenuName = NULL;
\r
562 child_wcs.lpszClassName = "vomp_playback";
\r
563 if (!RegisterClass(&child_wcs))
\r
566 win=CreateWindow("vomp_playback","Vomp Playback Window",WS_VISIBLE | WS_CHILD |WS_CLIPCHILDREN,
\r
567 0,0,768,576,win_main,NULL,hinst,NULL);
\r
570 ShowWindow(win,SW_SHOWNORMAL);
\r
579 // -------------------------------------------------------------------------------------------------------------------
\r
581 void shutdown(int code)
\r
585 viewman->shutdown();
\r
587 logger->log("Core", Log::NOTICE, "ViewMan module shut down");
\r
590 if (command) // shut down command here in case views have posted messages
\r
592 command->shutdown();
\r
594 logger->log("Core", Log::NOTICE, "Command module shut down");
\r
601 logger->log("Core", Log::NOTICE, "VDR module shut down");
\r
608 logger->log("Core", Log::NOTICE, "OSD module shut down");
\r
615 logger->log("Core", Log::NOTICE, "Audio module shut down");
\r
622 logger->log("Core", Log::NOTICE, "Video module shut down");
\r
627 timers->shutdown();
\r
629 logger->log("Core", Log::NOTICE, "Timers module shut down");
\r
636 logger->log("Core", Log::NOTICE, "MTD module shut down");
\r
643 logger->log("Core", Log::NOTICE, "LED module shut down");
\r
648 remote->shutdown();
\r
650 logger->log("Core", Log::NOTICE, "Remote module shut down");
\r
655 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
\r
656 logger->shutdown();
\r
663 // -------------------------------------------------------------------------------------------------------------------
\r
665 ULLONG ntohll(ULLONG a)
\r
670 ULLONG htonll(ULLONG a)
\r
672 return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32)
\r
673 |(ULONG)htonl(((ULONG) (a >> 32))));
\r