]> git.vomp.tv Git - vompclient.git/blob - main.cc
Switch trunk code to new boxes
[vompclient.git] / main.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
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <signal.h>
25 #ifndef WIN32
26 #include <unistd.h>
27 #include <endian.h>
28 #endif
29
30 #include "defines.h"
31 #include "log.h"
32 #include "timers.h"
33 #include "vdr.h"
34 #include "boxstack.h"
35 #include "command.h"
36
37 #include "mtdmvp.h"
38 #include "remotemvp.h"
39 #include "ledmvp.h"
40 #include "osdmvp.h"
41 #include "audiomvp.h"
42 #include "videomvp.h"
43 #include "wol.h"
44
45 #ifndef WIN32
46 void sighandler(int signalReceived);
47 #endif
48
49 void shutdown(int code);
50
51 extern "C"
52 {
53   int ticonfig_main(int, char**);
54 }
55
56 // Global variables --------------------------------------------------------------------------------------------------
57 int debugEnabled = 0;
58 Log* logger;
59 Remote* remote;
60 Mtd* mtd;
61 Led* led;
62 Osd* osd;
63 Timers* timers;
64 BoxStack* boxstack;
65 Command* command;
66 VDR* vdr;
67 Video* video;
68 Audio* audio;
69 Wol* wol;
70
71 // Linux MVP main function and sighandler
72 #ifndef WIN32
73 int main(int argc, char** argv)
74 {
75   if (strstr(argv[0], "ticonfig")) return ticonfig_main(argc, argv);
76
77   if ((argc > 1) && (!strcmp(argv[1], "-d"))) debugEnabled = 1;
78
79
80   // Init global vars ------------------------------------------------------------------------------------------------
81
82   logger     = new Log();
83   timers     = new Timers();
84   vdr        = new VDR();
85   mtd        = new MtdMVP();
86   remote     = new RemoteMVP();
87   led        = new LedMVP();
88   osd        = new OsdMVP();
89   audio      = new AudioMVP();
90   video      = new VideoMVP();
91   boxstack   = new BoxStack();
92   command    = new Command();
93   wol        = new Wol();
94
95   if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !boxstack || !command || !wol)
96   {
97     printf("Could not create objects. Memory problems?\n");
98     shutdown(1);
99   }
100
101   // Get logging module started --------------------------------------------------------------------------------------
102
103   if (!logger->init(Log::DEBUG, "dummy", debugEnabled))
104   {
105     printf("Could not initialise log object. Aborting.\n");
106     shutdown(1);
107   }
108
109   logger->log("Core", Log::INFO, "Starting up...");
110
111   // Daemonize if not -d
112
113   if (!debugEnabled)
114   {
115     // Fork away
116     pid_t forkTest = fork();
117     if (forkTest == -1)
118     { printf("Cannot fork (1).\n"); exit(1); }
119     if (forkTest != 0) _exit(0); // PID returned, I am the parent
120     // otherwise, I am the child
121     setsid();
122     forkTest = fork();
123     if (forkTest == -1)
124     { printf("Cannot fork (2).\n"); exit(1); }
125     if (forkTest != 0) _exit(0); // PID returned, I am the parent
126     // otherwise, I am the child
127     close(0);
128     close(1);
129     close(2);
130   }
131
132   // Set up signal handling ------------------------------------------------------------------------------------------
133
134   sighandler_t sigtest;
135
136   sigtest = signal(SIGPIPE, SIG_IGN);
137   if (sigtest == SIG_ERR)
138   {
139     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGPIPE. Aborting.");
140     shutdown(1);
141   }
142   sigtest = signal(SIGINT, sighandler);
143   if (sigtest == SIG_ERR)
144   {
145     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGINT. Aborting.");
146     shutdown(1);
147   }
148   sigtest = signal(SIGTERM, sighandler);
149   if (sigtest == SIG_ERR)
150   {
151     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGTERM. Aborting.");
152     shutdown(1);
153   }
154   sigtest = signal(SIGUSR1, sighandler);
155   if (sigtest == SIG_ERR)
156   {
157     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR1. Aborting.");
158     shutdown(1);
159   }
160 /*
161   sigtest = signal(SIGUSR2, sighandler);
162   if (sigtest == SIG_ERR)
163   {
164     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR2. Aborting.");
165     shutdown(1);
166   }
167 */
168   sigtest = signal(SIGURG, sighandler);
169   if (sigtest == SIG_ERR)
170   {
171     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGURG. Aborting.");
172     shutdown(1);
173   }
174
175   logger->log("Core", Log::INFO, "Signal handlers set up successfully");
176
177
178   // Init modules ----------------------------------------------------------------------------------------------------
179   int success;
180
181   success = remote->init("/dev/rawir");
182   if (success)
183   {
184     logger->log("Core", Log::INFO, "Remote module initialised");
185   }
186   else
187   {
188     logger->log("Core", Log::EMERG, "Remote module failed to initialise");
189     shutdown(1);
190   }
191
192   success = led->init(((RemoteMVP*)remote)->getDevice());
193   if (success)
194   {
195     logger->log("Core", Log::INFO, "LED module initialised");
196   }
197   else
198   {
199     logger->log("Core", Log::EMERG, "LED module failed to initialise");
200     shutdown(1);
201   }
202
203   success = mtd->init();
204   if (success)
205   {
206     logger->log("Core", Log::INFO, "Mtd module initialised");
207   }
208   else
209   {
210     logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
211     shutdown(1);
212   }
213
214   success = timers->init();
215   if (success)
216   {
217     logger->log("Core", Log::INFO, "Timers module initialised");
218   }
219   else
220   {
221     logger->log("Core", Log::EMERG, "Timers module failed to initialise");
222     shutdown(1);
223   }
224
225   UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
226   if      (videoFormat == Video::PAL)  logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
227   else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
228   else                                 logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
229
230   success = video->init(videoFormat);
231   if (success)
232   {
233     logger->log("Core", Log::INFO, "Video module initialised");
234   }
235   else
236   {
237     logger->log("Core", Log::EMERG, "Video module failed to initialise");
238     shutdown(1);
239   }
240
241   success = osd->init((void*)("/dev/stbgfx"));
242   if (success)
243   {
244     logger->log("Core", Log::INFO, "OSD module initialised");
245   }
246   else
247   {
248     logger->log("Core", Log::EMERG, "OSD module failed to initialise");
249     shutdown(1);
250   }
251
252   success = audio->init(Audio::MPEG2_PES);
253   if (success)
254   {
255     logger->log("Core", Log::INFO, "Audio module initialised");
256   }
257   else
258   {
259     logger->log("Core", Log::EMERG, "Audio module failed to initialise");
260     shutdown(1);
261   }
262
263   success = vdr->init(3024);
264   if (success)
265   {
266     logger->log("Core", Log::INFO, "VDR module initialised");
267   }
268   else
269   {
270     logger->log("Core", Log::EMERG, "VDR module failed to initialise");
271     shutdown(1);
272   }
273
274   success = boxstack->init();
275   if (success)
276   {
277     logger->log("Core", Log::INFO, "BoxStack module initialised");
278   }
279   else
280   {
281     logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
282     shutdown(1);
283   }
284
285   success = command->init();
286   if (success)
287   {
288     logger->log("Core", Log::INFO, "Command module initialised");
289   }
290   else
291   {
292     logger->log("Core", Log::EMERG, "Command module failed to initialise");
293     shutdown(1);
294   }
295
296   // Other init ------------------------------------------------------------------------------------------------------
297
298   logger->log("Core", Log::NOTICE, "Startup successful");
299
300   // Run main loop ---------------------------------------------------------------------------------------------------
301
302   // Ok, all major device components and other bits are loaded and ready
303   command->run();
304
305   // When that returns quit ------------------------------------------------------------------------------------------
306
307   shutdown(0);
308   return 0;
309 }
310
311 // -------------------------------------------------------------------------------------------------------------------
312
313 void sighandler(int signalReceived)
314 {
315   logger->log("Core", Log::NOTICE, "Signal %i received", signalReceived);
316
317   switch (signalReceived)
318   {
319     case SIGINT:
320     {
321       logger->log("Core", Log::NOTICE, "Interrupt signal, shutting down...");
322       command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
323       break;
324     }
325     case SIGTERM:
326     {
327       logger->log("Core", Log::NOTICE, "Term signal, shutting down...");
328       command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
329       break;
330     }
331     case SIGUSR1:
332     {
333       command->sig1();
334       break;
335     }
336 /*
337     case SIGUSR1:
338     {
339       logger->log("Core", Log::DEBUG, "SIGUSR1 caught");
340       logger->upLogLevel();
341       break;
342     }
343     case SIGUSR2:
344     {
345       logger->log("Core", Log::DEBUG, "SIGUSR2 caught");
346       logger->downLogLevel();
347       break;
348     }
349 */
350     case SIGURG:
351     {
352       logger->log("Core", Log::DEBUG, "SIGURG caught");
353       break;
354     }
355   }
356 }
357 #endif
358
359 // -------------------------------------------------------------------------------------------------------------------
360
361 void shutdown(int code)
362 {
363   if (boxstack)
364   {
365     boxstack->shutdown();
366     delete boxstack;
367     logger->log("Core", Log::NOTICE, "BoxStack module shut down");
368   }
369
370   // FIXME, send a del all to boxstack first, then get rid of it after command?
371   if (command) // shut down command here in case views have posted messages
372   {
373     command->shutdown();
374     delete command;
375     logger->log("Core", Log::NOTICE, "Command module shut down");
376   }
377
378   if (vdr)
379   {
380     vdr->shutdown();
381     delete vdr;
382     logger->log("Core", Log::NOTICE, "VDR module shut down");
383   }
384
385   if (osd)
386   {
387     osd->shutdown();
388     delete osd;
389     logger->log("Core", Log::NOTICE, "OSD module shut down");
390   }
391
392   if (audio)
393   {
394     audio->shutdown();
395     delete audio;
396     logger->log("Core", Log::NOTICE, "Audio module shut down");
397   }
398
399   if (video)
400   {
401     video->shutdown();
402     delete video;
403     logger->log("Core", Log::NOTICE, "Video module shut down");
404   }
405
406   if (timers)
407   {
408     timers->shutdown();
409     delete timers;
410     logger->log("Core", Log::NOTICE, "Timers module shut down");
411   }
412
413   if (mtd)
414   {
415     mtd->shutdown();
416     delete mtd;
417     logger->log("Core", Log::NOTICE, "MTD module shut down");
418   }
419
420   if (led)
421   {
422     led->shutdown();
423     delete led;
424     logger->log("Core", Log::NOTICE, "LED module shut down");
425   }
426
427   if (remote)
428   {
429     remote->shutdown();
430     delete remote;
431     logger->log("Core", Log::NOTICE, "Remote module shut down");
432   }
433
434   if (wol)
435   {
436     delete wol;
437     logger->log("Core", Log::NOTICE, "WOL module shut down");
438   }
439
440   if (logger)
441   {
442     logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
443     logger->shutdown();
444     delete logger;
445   }
446
447   exit(code);
448 }
449
450 // -------------------------------------------------------------------------------------------------------------------
451
452 ULLONG ntohll(ULLONG a)
453 {
454   return htonll(a);
455 }
456
457 ULLONG htonll(ULLONG a)
458 {
459   #if BYTE_ORDER == BIG_ENDIAN
460     return a;
461   #else
462     ULLONG b = 0;
463
464     b = ((a << 56) & 0xFF00000000000000ULL)
465       | ((a << 40) & 0x00FF000000000000ULL)
466       | ((a << 24) & 0x0000FF0000000000ULL)
467       | ((a <<  8) & 0x000000FF00000000ULL)
468       | ((a >>  8) & 0x00000000FF000000ULL)
469       | ((a >> 24) & 0x0000000000FF0000ULL)
470       | ((a >> 40) & 0x000000000000FF00ULL)
471       | ((a >> 56) & 0x00000000000000FFULL) ;
472
473     return b;
474   #endif
475 }
476
477 void MILLISLEEP(ULONG a)
478 {
479 #ifndef WIN32
480   struct timespec delayTime;
481   delayTime.tv_sec = a / 1000;
482   delayTime.tv_nsec = (a % 1000) * 1000000;
483   nanosleep(&delayTime, NULL);
484 #else
485   Sleep(a);
486 #endif
487 }
488
489 int max(int a, int b)
490 {
491   if (a > b) return a;
492   else return b;
493 }