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