]> git.vomp.tv Git - vompclient.git/blob - winmain.cc
Changes for demuxer. New mutex code
[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 LONG FAR PASCAL WindowProc(HWND win, UINT msg, WPARAM wparam, LPARAM lparam)
314 {
315    switch (msg) {
316    case WM_DESTROY: {
317      //TODO: call command
318      logger->log("Core", Log::NOTICE, "Window closed, shutting down...");
319        command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
320      ((RemoteWin*)Remote::getInstance())->Signal();
321      PostQuitMessage(0);
322   }break;
323   case WM_SIZE: {
324         int width = LOWORD(lparam);
325         int height = HIWORD(lparam);
326          //Call device
327         }
328         break;
329    case WM_PAINT:
330         RECT r;
331         PAINTSTRUCT ps;
332         if (GetUpdateRect(win, &r, FALSE)) {
333             BeginPaint(win, &ps);
334             //Call Painting Mechanism
335             EndPaint(win, &ps);
336         }
337         break;
338    case WM_KEYDOWN:
339      if (((RemoteWin*)remote)->ReceiveButtonVK(wparam)) {
340        return 0L; //We process that Key
341      } else {
342        return DefWindowProc(win, msg, wparam, lparam);
343      }
344
345      break;
346   case WM_APPCOMMAND:
347     if (((RemoteWin*)remote)->ReceiveButtonAP(GET_APPCOMMAND_LPARAM(lparam))){
348       return TRUE; //yes we process that message
349     } else {
350       return DefWindowProc(win, msg, wparam, lparam);
351     }
352
353     break;
354   case WM_COMMAND:
355     if (((RemoteWin*)remote)->ReceiveButtonAP(LOWORD(wparam))){
356       return 0; //yes we process that message
357     } else {
358       return DefWindowProc(win, msg, wparam, lparam);
359     }
360
361     break;
362     default:
363         return DefWindowProc(win, msg, wparam, lparam);
364     }
365     return 0L;
366 }
367
368
369 bool InitApp(HINSTANCE hinst,int cmdshow) {
370   WNDCLASS wcs;
371   DWORD flags;
372   wcs.style = CS_HREDRAW | CS_VREDRAW;
373     wcs.lpfnWndProc = WindowProc;
374     wcs.cbClsExtra = 0;
375     wcs.cbWndExtra = sizeof(DWORD);
376     wcs.hInstance = hinst;
377     wcs.hIcon = NULL;
378     wcs.hCursor = NULL;
379     wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
380     wcs.lpszMenuName = NULL;
381     wcs.lpszClassName = "vomp";
382   acc=LoadAccelerators(hinst,MAKEINTRESOURCE(VOMPACCELERATOR));
383   if (!RegisterClass(&wcs))
384         return false;
385   flags =WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU
386                  |WS_MINIMIZEBOX | WS_SIZEBOX |WS_MAXIMIZEBOX;
387   RECT wnted={0,0,720,576};
388   AdjustWindowRect(&wnted,flags ,false);
389   win=CreateWindow("vomp","vomp",flags, CW_USEDEFAULT,CW_USEDEFAULT,
390     wnted.right-wnted.left,wnted.bottom-wnted.top,NULL,NULL,hinst,NULL);
391   if (!win)
392         return FALSE;
393   ShowWindow(win,SW_SHOWNORMAL);
394     UpdateWindow(win);
395   return TRUE;
396 }
397
398
399
400
401
402 // -------------------------------------------------------------------------------------------------------------------
403
404 void shutdown(int code)
405 {
406   if (viewman)
407   {
408     viewman->shutdown();
409     delete viewman;
410     logger->log("Core", Log::NOTICE, "ViewMan module shut down");
411   }
412
413   if (command) // shut down command here in case views have posted messages
414   {
415     command->shutdown();
416     delete command;
417     logger->log("Core", Log::NOTICE, "Command module shut down");
418   }
419
420   if (vdr)
421   {
422     vdr->shutdown();
423     delete vdr;
424     logger->log("Core", Log::NOTICE, "VDR module shut down");
425   }
426
427   if (osd)
428   {
429     osd->shutdown();
430     delete osd;
431     logger->log("Core", Log::NOTICE, "OSD module shut down");
432   }
433
434   if (audio)
435   {
436     audio->shutdown();
437     delete audio;
438     logger->log("Core", Log::NOTICE, "Audio module shut down");
439   }
440
441   if (video)
442   {
443     video->shutdown();
444     delete video;
445     logger->log("Core", Log::NOTICE, "Video module shut down");
446   }
447
448   if (timers)
449   {
450     timers->shutdown();
451     delete timers;
452     logger->log("Core", Log::NOTICE, "Timers module shut down");
453   }
454
455   if (mtd)
456   {
457     mtd->shutdown();
458     delete mtd;
459     logger->log("Core", Log::NOTICE, "MTD module shut down");
460   }
461
462   if (led)
463   {
464     led->shutdown();
465     delete led;
466     logger->log("Core", Log::NOTICE, "LED module shut down");
467   }
468
469   if (remote)
470   {
471     remote->shutdown();
472     delete remote;
473     logger->log("Core", Log::NOTICE, "Remote module shut down");
474   }
475
476   if (logger)
477   {
478     logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
479     logger->shutdown();
480     delete logger;
481   }
482   ExitProcess(0);
483
484 }
485
486 // -------------------------------------------------------------------------------------------------------------------
487
488 ULLONG ntohll(ULLONG a)
489 {
490   return htonll(a);
491 }
492
493 ULLONG htonll(ULLONG a)
494 {
495 /*
496   #if BYTE_ORDER == BIG_ENDIAN
497     return a;
498   #else
499     ULLONG b = 0;
500
501     b = ((a << 56) & 0xFF00000000000000ULL)
502       | ((a << 40) & 0x00FF000000000000ULL)
503       | ((a << 24) & 0x0000FF0000000000ULL)
504       | ((a <<  8) & 0x000000FF00000000ULL)
505       | ((a >>  8) & 0x00000000FF000000ULL)
506       | ((a >> 24) & 0x0000000000FF0000ULL)
507       | ((a >> 40) & 0x000000000000FF00ULL)
508       | ((a >> 56) & 0x00000000000000FFULL) ;
509
510     return b;
511   #endif*///This macro switching does not work for windows, here is a implementation without
512   // using BYTE_ORDER
513   //#define ntohll(x) (((_int64)(ntohl((int)((x << 32) >> 32))) << 32) |
514                  //    (unsigned int)ntohl(((int)(x >> 32)))) //By Runner
515
516   return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32)
517     |(ULONG)htonl(((ULONG) (a >> 32))));
518 }
519 #endif