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