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