]> git.vomp.tv Git - vompclient.git/blob - main.cc
New timers code
[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   sigtest = signal(SIGUSR2, sighandler);
143   if (sigtest == SIG_ERR)
144   {
145     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR2. Aborting.");
146     shutdown(1);
147   }
148   sigtest = signal(SIGURG, sighandler);
149   if (sigtest == SIG_ERR)
150   {
151     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGURG. Aborting.");
152     shutdown(1);
153   }
154
155   logger->log("Core", Log::INFO, "Signal handlers set up successfully");
156
157
158   // Init modules ----------------------------------------------------------------------------------------------------
159   int success;
160
161   success = remote->init("/dev/rawir");
162   if (success)
163   {
164     logger->log("Core", Log::INFO, "Remote module initialised");
165   }
166   else
167   {
168     logger->log("Core", Log::EMERG, "Remote module failed to initialise");
169     shutdown(1);
170   }
171
172   success = led->init(remote->getDevice());
173   if (success)
174   {
175     logger->log("Core", Log::INFO, "LED module initialised");
176   }
177   else
178   {
179     logger->log("Core", Log::EMERG, "LED module failed to initialise");
180     shutdown(1);
181   }
182
183   success = mtd->init("/dev/mtd1");
184   if (success)
185   {
186     logger->log("Core", Log::INFO, "Mtd module initialised");
187   }
188   else
189   {
190     logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
191     shutdown(1);
192   }
193
194   success = timers->init();
195   if (success)
196   {
197     logger->log("Core", Log::INFO, "Timers module initialised");
198   }
199   else
200   {
201     logger->log("Core", Log::EMERG, "Timers module failed to initialise");
202     shutdown(1);
203   }
204
205   UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
206   if      (videoFormat == Video::PAL)  logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
207   else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
208   else                                 logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
209
210   //videoFormat = Video::NTSC; // enable this line to test NTSC in PAL land
211
212   success = video->init(videoFormat);
213   if (success)
214   {
215     logger->log("Core", Log::INFO, "Video module initialised");
216   }
217   else
218   {
219     logger->log("Core", Log::EMERG, "Video module failed to initialise");
220     shutdown(1);
221   }
222
223   success = osd->init("/dev/stbgfx");
224   if (success)
225   {
226     logger->log("Core", Log::INFO, "OSD module initialised");
227   }
228   else
229   {
230     logger->log("Core", Log::EMERG, "OSD module failed to initialise");
231     shutdown(1);
232   }
233
234   success = audio->init(Audio::MPEG2_PES);
235   if (success)
236   {
237     logger->log("Core", Log::INFO, "Audio module initialised");
238   }
239   else
240   {
241     logger->log("Core", Log::EMERG, "Audio module failed to initialise");
242     shutdown(1);
243   }
244
245   success = vdr->init(3024);
246   if (success)
247   {
248     logger->log("Core", Log::INFO, "VDR module initialised");
249   }
250   else
251   {
252     logger->log("Core", Log::EMERG, "VDR module failed to initialise");
253     shutdown(1);
254   }
255
256   success = viewman->init();
257   if (success)
258   {
259     logger->log("Core", Log::INFO, "ViewMan module initialised");
260   }
261   else
262   {
263     logger->log("Core", Log::EMERG, "ViewMan module failed to initialise");
264     shutdown(1);
265   }
266
267   success = command->init();
268   if (success)
269   {
270     logger->log("Core", Log::INFO, "Command module initialised");
271   }
272   else
273   {
274     logger->log("Core", Log::EMERG, "Command module failed to initialise");
275     shutdown(1);
276   }
277
278   // Other init ------------------------------------------------------------------------------------------------------
279
280   logger->log("Core", Log::NOTICE, "Startup successful");
281
282   // Run main loop ---------------------------------------------------------------------------------------------------
283
284   // Ok, all major device components and other bits are loaded and ready
285   command->run();
286
287   // When that returns quit ------------------------------------------------------------------------------------------
288
289   shutdown(0);
290   return 0;
291 }
292
293 // -------------------------------------------------------------------------------------------------------------------
294
295 void shutdown(int code)
296 {
297   if (command)
298   {
299     command->shutdown();
300     delete command;
301     logger->log("Core", Log::NOTICE, "Command module shut down");
302   }
303
304   if (viewman)
305   {
306     viewman->shutdown();
307     delete viewman;
308     logger->log("Core", Log::NOTICE, "ViewMan module shut down");
309   }
310
311   if (vdr)
312   {
313     vdr->shutdown();
314     delete vdr;
315     logger->log("Core", Log::NOTICE, "VDR module shut down");
316   }
317
318   if (osd)
319   {
320     osd->shutdown();
321     delete osd;
322     logger->log("Core", Log::NOTICE, "OSD module shut down");
323   }
324
325   if (audio)
326   {
327     audio->shutdown();
328     delete audio;
329     logger->log("Core", Log::NOTICE, "Audio module shut down");
330   }
331
332   if (video)
333   {
334     video->shutdown();
335     delete video;
336     logger->log("Core", Log::NOTICE, "Video module shut down");
337   }
338
339   if (timers)
340   {
341     timers->shutdown();
342     delete timers;
343     logger->log("Core", Log::NOTICE, "Timers module shut down");
344   }
345
346   if (mtd)
347   {
348     mtd->shutdown();
349     delete mtd;
350     logger->log("Core", Log::NOTICE, "MTD module shut down");
351   }
352
353   if (led)
354   {
355     led->shutdown();
356     delete led;
357     logger->log("Core", Log::NOTICE, "LED module shut down");
358   }
359
360   if (remote)
361   {
362     remote->shutdown();
363     delete remote;
364     logger->log("Core", Log::NOTICE, "Remote module shut down");
365   }
366
367   if (logger)
368   {
369     logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
370     logger->shutdown();
371     delete logger;
372   }
373
374   exit(code);
375 }
376
377 // -------------------------------------------------------------------------------------------------------------------
378
379 void sighandler(int signalReceived)
380 {
381   logger->log("Core", Log::NOTICE, "Signal %i received", signal);
382
383   switch (signalReceived)
384   {
385     case SIGINT:
386     {
387       logger->log("Core", Log::NOTICE, "Interrupt signal, shutting down...");
388       command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
389       break;
390     }
391     case SIGTERM:
392     {
393       logger->log("Core", Log::NOTICE, "Term signal, shutting down...");
394       command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
395       break;
396     }
397     case SIGUSR1:
398     {
399       logger->log("Core", Log::DEBUG, "SIGUSR1 caught");
400       logger->upLogLevel();
401       break;
402     }
403     case SIGUSR2:
404     {
405       logger->log("Core", Log::DEBUG, "SIGUSR2 caught");
406       logger->downLogLevel();
407       break;
408     }
409     case SIGURG:
410     {
411       logger->log("Core", Log::DEBUG, "SIGURG caught");
412       break;
413     }
414   }
415 }
416
417 // -------------------------------------------------------------------------------------------------------------------
418
419 ULLONG ntohll(ULLONG a)
420 {
421   return htonll(a);
422 }
423
424 ULLONG htonll(ULLONG a)
425 {
426   #if BYTE_ORDER == BIG_ENDIAN
427     return a;
428   #else
429     ULLONG b = 0;
430
431     b = ((a << 56) & 0xFF00000000000000ULL)
432       | ((a << 40) & 0x00FF000000000000ULL)
433       | ((a << 24) & 0x0000FF0000000000ULL)
434       | ((a <<  8) & 0x000000FF00000000ULL)
435       | ((a >>  8) & 0x00000000FF000000ULL)
436       | ((a >> 24) & 0x0000000000FF0000ULL)
437       | ((a >> 40) & 0x000000000000FF00ULL)
438       | ((a >> 56) & 0x00000000000000FFULL) ;
439
440     return b;
441   #endif
442 }