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