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