]> git.vomp.tv Git - vompclient.git/blob - winmain.cc
Windows port changes
[vompclient.git] / winmain.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
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
19 */
20 #ifdef WIN32
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <signal.h>
25
26 #define _WIN32_WINNT 0x501
27 #include <winsock2.h>
28 #include <windows.h>
29
30 #include "vompreswin.h"
31
32 #include "defines.h"
33 #include "log.h"
34 #include "remotewin.h"
35 #include "ledwin.h"
36 #include "mtdwin.h"
37 #include "timers.h"
38 #include "videowin.h"
39 #include "audiowin.h"
40 #include "vdr.h"
41 #include "osdwin.h"
42 #include "viewman.h"
43 #include "command.h"
44
45 void sighandler(int signalReceived);
46 void shutdown(int code);
47
48 // Global variables --------------------------------------------------------------------------------------------------
49 int debugEnabled = 0;
50 Log* logger;
51 Remote* remote;
52 Mtd* mtd;
53 Led* led;
54 Osd* osd;
55 Timers* timers;
56 ViewMan* viewman;
57 Command* command;
58 VDR* vdr;
59 Video* video;
60 Audio* audio;
61
62
63 void MILLISLEEP(ULONG a)
64 {
65
66   Sleep(a);
67
68 }
69
70 DWORD WINAPI commandthreadStart(void *arg)
71 {
72          command->run();
73          return 0;
74 }
75
76 bool InitApp(HINSTANCE hinst,int cmdshow);
77
78 HWND win;//global window handle
79 HACCEL acc;
80
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)
83 {
84   //On Windows we have to init a window, we use DXUT
85         if (!InitApp(hinst,cmdshow)) return false;
86   //Starting Network support
87   WSADATA wsadat; 
88   int result = WSAStartup(MAKEWORD(2,2),&wsadat);
89   if (result!=NO_ERROR) {
90         ERROR_MSG("Initialising WinSocked: Error at WSAStartup()\n");
91                 return 0;
92   }
93   result= CoInitializeEx(NULL,COINIT_MULTITHREADED );//Initialize COM for DirectShow
94   if (result!=S_OK) {
95           ERROR_MSG("Initialising COM: Error at Coinitialize()\n");
96           return 0;
97   }
98
99
100
101
102   // Init global vars ------------------------------------------------------------------------------------------------
103
104   logger     = new Log();
105   remote     = new RemoteWin();
106   mtd        = new MtdWin();
107   led        = new LedWin();
108   timers     = new Timers();
109   osd        = new OsdWin();
110   vdr        = new VDR();
111   video      = new VideoWin();
112   audio      = new AudioWin();
113   viewman    = new ViewMan();
114   command    = new Command();
115
116   if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !viewman || !command)
117   {
118     ERROR_MSG("Could not create objects. Memory problems?\n");
119     shutdown(1);
120         WSACleanup();
121         return 0;
122   }
123
124   // Get logging module started --------------------------------------------------------------------------------------
125
126   if (!logger->init(Log::DEBUG, "vompwin.log", true))
127   {
128     ERROR_MSG("Could not initialise log object. Aborting.\n");
129     shutdown(1);
130         WSACleanup();
131         return 0;
132   }
133
134   logger->log("Core", Log::INFO, "Starting up...");
135   
136
137
138   // Init modules ----------------------------------------------------------------------------------------------------
139   int success;
140
141   success = remote->init("/dev/rawir");
142   if (success)
143   {
144     logger->log("Core", Log::INFO, "Remote module initialised");
145   }
146   else
147   {
148     logger->log("Core", Log::EMERG, "Remote module failed to initialise");
149     shutdown(1);
150         WSACleanup();
151         return 0;
152   }
153
154   success = led->init(0);
155   if (success)
156   {
157     logger->log("Core", Log::INFO, "LED module initialised");
158   }
159   else
160   {
161     logger->log("Core", Log::EMERG, "LED module failed to initialise");
162     shutdown(1);
163         WSACleanup();
164         return 0;
165   }
166
167   success = mtd->init("/dev/mtd1");
168   if (success)
169   {
170     logger->log("Core", Log::INFO, "Mtd module initialised");
171   }
172   else
173   {
174     logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
175     shutdown(1);
176         WSACleanup();
177         return 0;
178   }
179
180   success = timers->init();
181   if (success)
182   {
183     logger->log("Core", Log::INFO, "Timers module initialised");
184   }
185   else
186   {
187     logger->log("Core", Log::EMERG, "Timers module failed to initialise");
188     shutdown(1);
189         WSACleanup();
190         return 0;
191   }
192
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");
197
198   success = video->init(videoFormat);
199   if (success)
200   {
201     logger->log("Core", Log::INFO, "Video module initialised");
202   }
203   else
204   {
205     logger->log("Core", Log::EMERG, "Video module failed to initialise");
206     shutdown(1);
207         WSACleanup();
208         return 0;
209   }
210
211   success = osd->init((void*)&win);
212   if (success)
213   {
214     logger->log("Core", Log::INFO, "OSD module initialised");
215   }
216   else
217   {
218     logger->log("Core", Log::EMERG, "OSD module failed to initialise");
219     shutdown(1);
220         WSACleanup();
221         return 0;
222   }
223
224   success = audio->init(Audio::MPEG2_PES);
225   if (success)
226   {
227     logger->log("Core", Log::INFO, "Audio module initialised");
228   }
229   else
230   {
231     logger->log("Core", Log::EMERG, "Audio module failed to initialise");
232     shutdown(1);
233         WSACleanup();
234         return 0;
235   }
236
237   success = vdr->init(3024);
238   if (success)
239   {
240     logger->log("Core", Log::INFO, "VDR module initialised");
241   }
242   else
243   {
244     logger->log("Core", Log::EMERG, "VDR module failed to initialise");
245     shutdown(1);
246         WSACleanup();
247         return 0;
248   }
249
250   success = viewman->init();
251   if (success)
252   {
253     logger->log("Core", Log::INFO, "ViewMan module initialised");
254   }
255   else
256   {
257     logger->log("Core", Log::EMERG, "ViewMan module failed to initialise");
258     shutdown(1);
259         WSACleanup();
260         return 0;
261   }
262
263   success = command->init();
264   if (success)
265   {
266     logger->log("Core", Log::INFO, "Command module initialised");
267   }
268   else
269   {
270     logger->log("Core", Log::EMERG, "Command module failed to initialise");
271     shutdown(1);
272         WSACleanup();
273         return 0;
274   }
275
276   // Other init ------------------------------------------------------------------------------------------------------
277
278   logger->log("Core", Log::NOTICE, "Startup successful");
279
280   // Run main loop ---------------------------------------------------------------------------------------------------
281
282   // Ok, all major device components and other bits are loaded and ready
283   
284   HANDLE commandthread;
285  commandthread= CreateThread(NULL, 0, commandthreadStart, NULL,0, 
286           NULL);
287   MSG message;
288   message.message=WM_NULL;
289   bool run=true;
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) {
296                           case WM_QUIT:
297                                   run=false; //TODO post exit to command Messages
298                           };
299                   }
300           } else {
301                   //Render
302                   ((OsdWin*)osd)->Render();
303           }
304   }
305   // When that returns quit ------------------------------------------------------------------------------------------
306   WaitForSingleObject(commandthread,INFINITE);
307   shutdown(0);
308   WSACleanup();
309   return 0;
310
311 }
312
313
314
315 void CalculateWindowSize(RECT * size,ULONG size_mode) {
316         DWORD width, height;
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
335                 }
336         } else if (tvsize==Video::ASPECT4X3) {
337                 if (aspect==Video::ASPECT16X9) {
338                         if (mode==Video::LETTERBOX) {
339                                 aspectrt=16./9.;
340                         } else {
341                                 aspectrt=4./3.;
342                         }
343                 } if (aspect==Video::ASPECT4X3) {
344                         aspectrt=4./3.;
345                 }
346         }
347         switch (size_mode) {
348         case WMSZ_BOTTOM:
349         case WMSZ_BOTTOMRIGHT:
350         case WMSZ_TOP:
351         case WMSZ_TOPRIGHT:
352         width=(ULONG)(((double)height)*aspectrt);
353         size->right=size->left+width+adjwidth;
354         break;
355         case WMSZ_BOTTOMLEFT:
356         case WMSZ_TOPLEFT:
357         width=(ULONG)(((double)height)*aspectrt);
358         size->left=size->right-width-adjwidth;
359         break;
360         case WMSZ_LEFT:
361         case WMSZ_RIGHT:
362         height=(ULONG)(((double)width)/aspectrt);
363         size->bottom=size->top+height+adjheight;
364         break;
365         }
366 }
367
368 void AdjustWindow() {
369         RECT winrect;
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);
374
375 }
376
377 LONG FAR PASCAL WindowProc(HWND win, UINT msg, WPARAM wparam, LPARAM lparam)
378 {
379    switch (msg) {
380    case WM_DESTROY: {
381            //TODO: call command
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();
385            PostQuitMessage(0);
386         }break;
387    case WM_SIZING: {
388            CalculateWindowSize((RECT*) lparam,wparam);
389            return TRUE;
390                                    }break;
391         case WM_SIZE: {
392         int width = LOWORD(lparam);
393         int height = HIWORD(lparam);
394          //Call device
395         }
396         break;
397          case WM_PAINT:
398         RECT r;
399         PAINTSTRUCT ps;
400         if (GetUpdateRect(win, &r, FALSE)) {
401             BeginPaint(win, &ps);
402             //Call Painting Mechanism
403             EndPaint(win, &ps);
404         }
405         break;
406          case WM_KEYDOWN:
407                  if (((RemoteWin*)remote)->ReceiveButtonVK(wparam)) {
408                          return 0L; //We process that Key
409                  } else {
410                          return DefWindowProc(win, msg, wparam, lparam);
411                  }
412
413                  break;
414         case WM_APPCOMMAND:
415                 if (((RemoteWin*)remote)->ReceiveButtonAP(GET_APPCOMMAND_LPARAM(lparam))){
416                         return TRUE; //yes we process that message
417                 } else {
418                         return DefWindowProc(win, msg, wparam, lparam);
419                 }
420
421                 break;
422         case WM_COMMAND:
423                 if (((RemoteWin*)remote)->ReceiveButtonAP(LOWORD(wparam))){
424                         return 0; //yes we process that message
425                 } else {
426                         return DefWindowProc(win, msg, wparam, lparam);
427                 }
428
429                 break;
430     default:
431         return DefWindowProc(win, msg, wparam, lparam);
432     }
433     return 0L;
434 }
435
436
437 bool InitApp(HINSTANCE hinst,int cmdshow) {
438         WNDCLASS wcs;
439         DWORD flags;
440         wcs.style = CS_HREDRAW | CS_VREDRAW;
441     wcs.lpfnWndProc = WindowProc;
442     wcs.cbClsExtra = 0;
443     wcs.cbWndExtra = sizeof(DWORD);
444     wcs.hInstance = hinst;
445     wcs.hIcon = NULL;
446     wcs.hCursor = NULL;
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))
452         return false;
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);
459         if (!win)
460         return FALSE;
461         ShowWindow(win,SW_SHOWNORMAL);
462     UpdateWindow(win);
463         return TRUE;
464 }
465
466
467
468
469
470 // -------------------------------------------------------------------------------------------------------------------
471
472 void shutdown(int code)
473 {
474   if (viewman)
475   {
476     viewman->shutdown();
477     delete viewman;
478     logger->log("Core", Log::NOTICE, "ViewMan module shut down");
479   }
480
481   if (command) // shut down command here in case views have posted messages
482   {
483     command->shutdown();
484     delete command;
485     logger->log("Core", Log::NOTICE, "Command module shut down");
486   }
487
488   if (vdr)
489   {
490     vdr->shutdown();
491     delete vdr;
492     logger->log("Core", Log::NOTICE, "VDR module shut down");
493   }
494
495   if (osd)
496   {
497     osd->shutdown();
498     delete osd;
499     logger->log("Core", Log::NOTICE, "OSD module shut down");
500   }
501
502   if (audio)
503   {
504     audio->shutdown();
505     delete audio;
506     logger->log("Core", Log::NOTICE, "Audio module shut down");
507   }
508
509   if (video)
510   {
511     video->shutdown();
512     delete video;
513     logger->log("Core", Log::NOTICE, "Video module shut down");
514   }
515
516   if (timers)
517   {
518     timers->shutdown();
519     delete timers;
520     logger->log("Core", Log::NOTICE, "Timers module shut down");
521   }
522
523   if (mtd)
524   {
525     mtd->shutdown();
526     delete mtd;
527     logger->log("Core", Log::NOTICE, "MTD module shut down");
528   }
529
530   if (led)
531   {
532     led->shutdown();
533     delete led;
534     logger->log("Core", Log::NOTICE, "LED module shut down");
535   }
536
537   if (remote)
538   {
539     remote->shutdown();
540     delete remote;
541     logger->log("Core", Log::NOTICE, "Remote module shut down");
542   }
543
544   if (logger)
545   {
546     logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
547     logger->shutdown();
548     delete logger;
549   }
550   ExitProcess(0);
551
552 }
553
554 // -------------------------------------------------------------------------------------------------------------------
555
556 ULLONG ntohll(ULLONG a)
557 {
558   return htonll(a);
559 }
560
561 ULLONG htonll(ULLONG a)
562 {
563         return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32) 
564                 |(ULONG)htonl(((ULONG) (a >> 32))));
565 }
566 #endif