2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #define _WIN32_WINNT 0x501
30 #include "vompreswin.h"
34 #include "remotewin.h"
45 void sighandler(int signalReceived);
46 void shutdown(int code);
48 // Global variables --------------------------------------------------------------------------------------------------
63 void MILLISLEEP(ULONG a)
70 DWORD WINAPI commandthreadStart(void *arg)
76 bool InitApp(HINSTANCE hinst,int cmdshow);
78 HWND win;//global window handle
81 #define ERROR_MSG(str) MessageBox(win,str,"Error!",MB_OK|MB_ICONWARNING)
82 INT WINAPI WinMain( HINSTANCE hinst , HINSTANCE previnst, LPSTR cmdline, int cmdshow)
84 //On Windows we have to init a window, we use DXUT
85 if (!InitApp(hinst,cmdshow)) return false;
86 //Starting Network support
88 int result = WSAStartup(MAKEWORD(2,2),&wsadat);
89 if (result!=NO_ERROR) {
90 ERROR_MSG("Initialising WinSocked: Error at WSAStartup()\n");
93 result= CoInitializeEx(NULL,COINIT_MULTITHREADED );//Initialize COM for DirectShow
95 ERROR_MSG("Initialising COM: Error at Coinitialize()\n");
102 // Init global vars ------------------------------------------------------------------------------------------------
105 remote = new RemoteWin();
108 timers = new Timers();
111 video = new VideoWin();
112 audio = new AudioWin();
113 viewman = new ViewMan();
114 command = new Command();
116 if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !viewman || !command)
118 ERROR_MSG("Could not create objects. Memory problems?\n");
124 // Get logging module started --------------------------------------------------------------------------------------
126 if (!logger->init(Log::DEBUG, "vompwin.log", true))
128 ERROR_MSG("Could not initialise log object. Aborting.\n");
134 logger->log("Core", Log::INFO, "Starting up...");
138 // Init modules ----------------------------------------------------------------------------------------------------
141 success = remote->init("/dev/rawir");
144 logger->log("Core", Log::INFO, "Remote module initialised");
148 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
154 success = led->init(0);
157 logger->log("Core", Log::INFO, "LED module initialised");
161 logger->log("Core", Log::EMERG, "LED module failed to initialise");
167 success = mtd->init("/dev/mtd1");
170 logger->log("Core", Log::INFO, "Mtd module initialised");
174 logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
180 success = timers->init();
183 logger->log("Core", Log::INFO, "Timers module initialised");
187 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
193 UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
194 if (videoFormat == Video::PAL) logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
195 else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
196 else logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
198 success = video->init(videoFormat);
201 logger->log("Core", Log::INFO, "Video module initialised");
205 logger->log("Core", Log::EMERG, "Video module failed to initialise");
211 success = osd->init((void*)&win);
214 logger->log("Core", Log::INFO, "OSD module initialised");
218 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
224 success = audio->init(Audio::MPEG2_PES);
227 logger->log("Core", Log::INFO, "Audio module initialised");
231 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
237 success = vdr->init(3024);
240 logger->log("Core", Log::INFO, "VDR module initialised");
244 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
250 success = viewman->init();
253 logger->log("Core", Log::INFO, "ViewMan module initialised");
257 logger->log("Core", Log::EMERG, "ViewMan module failed to initialise");
263 success = command->init();
266 logger->log("Core", Log::INFO, "Command module initialised");
270 logger->log("Core", Log::EMERG, "Command module failed to initialise");
276 // Other init ------------------------------------------------------------------------------------------------------
278 logger->log("Core", Log::NOTICE, "Startup successful");
280 // Run main loop ---------------------------------------------------------------------------------------------------
282 // Ok, all major device components and other bits are loaded and ready
284 HANDLE commandthread;
285 commandthread= CreateThread(NULL, 0, commandthreadStart, NULL,0,
288 message.message=WM_NULL;
290 while(run && WaitForSingleObject(commandthread,0)==WAIT_TIMEOUT) {
291 if (PeekMessage(&message, NULL, 0,0,PM_REMOVE)!=0) {
292 if (TranslateAccelerator(win,acc,&message)==NULL) {
293 TranslateMessage(&message);
294 DispatchMessage(&message);
295 switch (message.message) {
297 run=false; //TODO post exit to command Messages
302 ((OsdWin*)osd)->Render();
305 // When that returns quit ------------------------------------------------------------------------------------------
306 WaitForSingleObject(commandthread,INFINITE);
315 void CalculateWindowSize(RECT * size,ULONG size_mode) {
317 DWORD adjheight,adjwidth;
318 DWORD flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
319 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
320 RECT wnted={50,50,150,150};
321 AdjustWindowRect(&wnted,flags ,false);
322 adjwidth=-wnted.left+wnted.right-100;
323 adjheight=-wnted.top+wnted.bottom-100;
324 width=size->right-size->left-adjwidth;
325 height=size->bottom-size->top-adjheight;
326 UCHAR mode=video->getMode();
327 UCHAR aspect=((VideoWin*)video)->getAspectRatio();
328 UCHAR tvsize=((VideoWin*)video)->getPseudoTVsize();
329 double aspectrt=4./3.;
330 if (tvsize==Video::ASPECT16X9) {
331 if (aspect==Video::ASPECT16X9) {
332 aspectrt=4./3.; //looks strange, but it is a 16:9 tv
333 } else if (aspect==Video::ASPECT4X3) {
334 aspectrt=4./3./(16./9.)*(4./3.); //I hope this is correct
336 } else if (tvsize==Video::ASPECT4X3) {
337 if (aspect==Video::ASPECT16X9) {
338 if (mode==Video::LETTERBOX) {
343 } if (aspect==Video::ASPECT4X3) {
349 case WMSZ_BOTTOMRIGHT:
352 width=(ULONG)(((double)height)*aspectrt);
353 size->right=size->left+width+adjwidth;
355 case WMSZ_BOTTOMLEFT:
357 width=(ULONG)(((double)height)*aspectrt);
358 size->left=size->right-width-adjwidth;
362 height=(ULONG)(((double)width)/aspectrt);
363 size->bottom=size->top+height+adjheight;
368 void AdjustWindow() {
370 GetWindowRect(win,&winrect);
371 CalculateWindowSize(&winrect,WMSZ_BOTTOM);
372 MoveWindow(win,winrect.left,
373 winrect.top,winrect.right-winrect.left,winrect.bottom-winrect.top,true);
377 LONG FAR PASCAL WindowProc(HWND win, UINT msg, WPARAM wparam, LPARAM lparam)
382 logger->log("Core", Log::NOTICE, "Window closed, shutting down...");
383 command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
384 ((RemoteWin*)Remote::getInstance())->Signal();
388 CalculateWindowSize((RECT*) lparam,wparam);
392 int width = LOWORD(lparam);
393 int height = HIWORD(lparam);
400 if (GetUpdateRect(win, &r, FALSE)) {
401 BeginPaint(win, &ps);
402 //Call Painting Mechanism
407 if (((RemoteWin*)remote)->ReceiveButtonVK(wparam)) {
408 return 0L; //We process that Key
410 return DefWindowProc(win, msg, wparam, lparam);
415 if (((RemoteWin*)remote)->ReceiveButtonAP(GET_APPCOMMAND_LPARAM(lparam))){
416 return TRUE; //yes we process that message
418 return DefWindowProc(win, msg, wparam, lparam);
423 if (((RemoteWin*)remote)->ReceiveButtonAP(LOWORD(wparam))){
424 return 0; //yes we process that message
426 return DefWindowProc(win, msg, wparam, lparam);
431 return DefWindowProc(win, msg, wparam, lparam);
437 bool InitApp(HINSTANCE hinst,int cmdshow) {
440 wcs.style = CS_HREDRAW | CS_VREDRAW;
441 wcs.lpfnWndProc = WindowProc;
443 wcs.cbWndExtra = sizeof(DWORD);
444 wcs.hInstance = hinst;
447 wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
448 wcs.lpszMenuName = NULL;
449 wcs.lpszClassName = "vomp";
450 acc=LoadAccelerators(hinst,MAKEINTRESOURCE(VOMPACCELERATOR));
451 if (!RegisterClass(&wcs))
453 flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
454 |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
455 RECT wnted={50,50,768+50,576+50};
456 AdjustWindowRect(&wnted,flags ,false);
457 win=CreateWindow("vomp","vomp",flags, CW_USEDEFAULT,CW_USEDEFAULT,
458 wnted.right-wnted.left,wnted.bottom-wnted.top,NULL,NULL,hinst,NULL);
461 ShowWindow(win,SW_SHOWNORMAL);
470 // -------------------------------------------------------------------------------------------------------------------
472 void shutdown(int code)
478 logger->log("Core", Log::NOTICE, "ViewMan module shut down");
481 if (command) // shut down command here in case views have posted messages
485 logger->log("Core", Log::NOTICE, "Command module shut down");
492 logger->log("Core", Log::NOTICE, "VDR module shut down");
499 logger->log("Core", Log::NOTICE, "OSD module shut down");
506 logger->log("Core", Log::NOTICE, "Audio module shut down");
513 logger->log("Core", Log::NOTICE, "Video module shut down");
520 logger->log("Core", Log::NOTICE, "Timers module shut down");
527 logger->log("Core", Log::NOTICE, "MTD module shut down");
534 logger->log("Core", Log::NOTICE, "LED module shut down");
541 logger->log("Core", Log::NOTICE, "Remote module shut down");
546 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
554 // -------------------------------------------------------------------------------------------------------------------
556 ULLONG ntohll(ULLONG a)
561 ULLONG htonll(ULLONG a)
563 return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32)
564 |(ULONG)htonl(((ULONG) (a >> 32))));