]> git.vomp.tv Git - vompclient.git/blob - main.cc
New bits of OSD for recording playback
[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   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 (viewman)
298   {
299     viewman->shutdown();
300     delete viewman;
301     logger->log("Core", Log::NOTICE, "ViewMan module shut down");
302   }
303
304   if (command) // shut down command here in case views have posted messages
305   {
306     command->shutdown();
307     delete command;
308     logger->log("Core", Log::NOTICE, "Command 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", signalReceived);
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       command->sig1();
400       break;
401     }
402 /*
403     case SIGUSR1:
404     {
405       logger->log("Core", Log::DEBUG, "SIGUSR1 caught");
406       logger->upLogLevel();
407       break;
408     }
409     case SIGUSR2:
410     {
411       logger->log("Core", Log::DEBUG, "SIGUSR2 caught");
412       logger->downLogLevel();
413       break;
414     }
415 */
416     case SIGURG:
417     {
418       logger->log("Core", Log::DEBUG, "SIGURG caught");
419       break;
420     }
421   }
422 }
423
424 // -------------------------------------------------------------------------------------------------------------------
425
426 ULLONG ntohll(ULLONG a)
427 {
428   return htonll(a);
429 }
430
431 ULLONG htonll(ULLONG a)
432 {
433   #if BYTE_ORDER == BIG_ENDIAN
434     return a;
435   #else
436     ULLONG b = 0;
437
438     b = ((a << 56) & 0xFF00000000000000ULL)
439       | ((a << 40) & 0x00FF000000000000ULL)
440       | ((a << 24) & 0x0000FF0000000000ULL)
441       | ((a <<  8) & 0x000000FF00000000ULL)
442       | ((a >>  8) & 0x00000000FF000000ULL)
443       | ((a >> 24) & 0x0000000000FF0000ULL)
444       | ((a >> 40) & 0x000000000000FF00ULL)
445       | ((a >> 56) & 0x00000000000000FFULL) ;
446
447     return b;
448   #endif
449 }