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