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