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