]> git.vomp.tv Git - vompclient.git/blob - main.cc
Remove manual sync buttons
[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 #ifndef WIN32
26 #include <unistd.h>
27 #include <endian.h>
28 #endif
29
30 #include "defines.h"
31 #include "log.h"
32 #include "timers.h"
33 #include "vdr.h"
34 #include "viewman.h"
35 #include "command.h"
36
37 #ifdef WIN32
38   #include "mtdwin.h"
39   #include "remotewin.h"
40   #include "ledwin.h"
41   #include "osdwin.h"
42   #include "audiowin.h"
43   #include "videowin.h"
44 #else
45   #include "mtdmvp.h"
46   #include "remotemvp.h"
47   #include "ledmvp.h"
48   #include "osdmvp.h"
49   #include "audiomvp.h"
50   #include "videomvp.h"
51 #endif
52
53 #ifndef WIN32
54 void sighandler(int signalReceived);
55 #endif
56
57 void shutdown(int code);
58
59 // Global variables --------------------------------------------------------------------------------------------------
60 int debugEnabled = 0;
61 Log* logger;
62 Remote* remote;
63 Mtd* mtd;
64 Led* led;
65 Osd* osd;
66 Timers* timers;
67 ViewMan* viewman;
68 Command* command;
69 VDR* vdr;
70 Video* video;
71 Audio* audio;
72
73 // Linux MVP main function and sighandler
74 #ifndef WIN32
75 int main(int argc, char** argv)
76 {
77   if ((argc > 1) && (!strcmp(argv[1], "-d"))) debugEnabled = 1;
78
79
80   // Init global vars ------------------------------------------------------------------------------------------------
81
82   logger     = new Log();
83   timers     = new Timers();
84   vdr        = new VDR();
85   mtd        = new MtdMVP();
86   remote     = new RemoteMVP();
87   led        = new LedMVP();
88   osd        = new OsdMVP();
89   audio      = new AudioMVP();
90   video      = new VideoMVP();
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   success = led->init(((RemoteMVP*)remote)->getDevice());
192   if (success)
193   {
194     logger->log("Core", Log::INFO, "LED module initialised");
195   }
196   else
197   {
198     logger->log("Core", Log::EMERG, "LED module failed to initialise");
199     shutdown(1);
200   }
201
202   success = mtd->init("/dev/mtd1");
203   if (success)
204   {
205     logger->log("Core", Log::INFO, "Mtd module initialised");
206   }
207   else
208   {
209     logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
210     shutdown(1);
211   }
212
213   success = timers->init();
214   if (success)
215   {
216     logger->log("Core", Log::INFO, "Timers module initialised");
217   }
218   else
219   {
220     logger->log("Core", Log::EMERG, "Timers module failed to initialise");
221     shutdown(1);
222   }
223
224   UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
225   if      (videoFormat == Video::PAL)  logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
226   else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
227   else                                 logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
228
229   success = video->init(videoFormat);
230   if (success)
231   {
232     logger->log("Core", Log::INFO, "Video module initialised");
233   }
234   else
235   {
236     logger->log("Core", Log::EMERG, "Video module failed to initialise");
237     shutdown(1);
238   }
239
240   success = osd->init((void*)("/dev/stbgfx"));
241   if (success)
242   {
243     logger->log("Core", Log::INFO, "OSD module initialised");
244   }
245   else
246   {
247     logger->log("Core", Log::EMERG, "OSD module failed to initialise");
248     shutdown(1);
249   }
250
251   success = audio->init(Audio::MPEG2_PES);
252   if (success)
253   {
254     logger->log("Core", Log::INFO, "Audio module initialised");
255   }
256   else
257   {
258     logger->log("Core", Log::EMERG, "Audio module failed to initialise");
259     shutdown(1);
260   }
261
262   success = vdr->init(3024);
263   if (success)
264   {
265     logger->log("Core", Log::INFO, "VDR module initialised");
266   }
267   else
268   {
269     logger->log("Core", Log::EMERG, "VDR module failed to initialise");
270     shutdown(1);
271   }
272
273   success = viewman->init();
274   if (success)
275   {
276     logger->log("Core", Log::INFO, "ViewMan module initialised");
277   }
278   else
279   {
280     logger->log("Core", Log::EMERG, "ViewMan module failed to initialise");
281     shutdown(1);
282   }
283
284   success = command->init();
285   if (success)
286   {
287     logger->log("Core", Log::INFO, "Command module initialised");
288   }
289   else
290   {
291     logger->log("Core", Log::EMERG, "Command module failed to initialise");
292     shutdown(1);
293   }
294
295   // Other init ------------------------------------------------------------------------------------------------------
296
297   logger->log("Core", Log::NOTICE, "Startup successful");
298
299   // Run main loop ---------------------------------------------------------------------------------------------------
300
301   // Ok, all major device components and other bits are loaded and ready
302   command->run();
303
304   // When that returns quit ------------------------------------------------------------------------------------------
305
306   shutdown(0);
307   return 0;
308 }
309
310 // -------------------------------------------------------------------------------------------------------------------
311
312 void sighandler(int signalReceived)
313 {
314   logger->log("Core", Log::NOTICE, "Signal %i received", signalReceived);
315
316   switch (signalReceived)
317   {
318     case SIGINT:
319     {
320       logger->log("Core", Log::NOTICE, "Interrupt signal, shutting down...");
321       command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
322       break;
323     }
324     case SIGTERM:
325     {
326       logger->log("Core", Log::NOTICE, "Term signal, shutting down...");
327       command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
328       break;
329     }
330     case SIGUSR1:
331     {
332       command->sig1();
333       break;
334     }
335 /*
336     case SIGUSR1:
337     {
338       logger->log("Core", Log::DEBUG, "SIGUSR1 caught");
339       logger->upLogLevel();
340       break;
341     }
342     case SIGUSR2:
343     {
344       logger->log("Core", Log::DEBUG, "SIGUSR2 caught");
345       logger->downLogLevel();
346       break;
347     }
348 */
349     case SIGURG:
350     {
351       logger->log("Core", Log::DEBUG, "SIGURG caught");
352       break;
353     }
354   }
355 }
356 #endif
357
358 // -------------------------------------------------------------------------------------------------------------------
359
360 void shutdown(int code)
361 {
362   if (viewman)
363   {
364     viewman->shutdown();
365     delete viewman;
366     logger->log("Core", Log::NOTICE, "ViewMan module shut down");
367   }
368
369   if (command) // shut down command here in case views have posted messages
370   {
371     command->shutdown();
372     delete command;
373     logger->log("Core", Log::NOTICE, "Command module shut down");
374   }
375
376   if (vdr)
377   {
378     vdr->shutdown();
379     delete vdr;
380     logger->log("Core", Log::NOTICE, "VDR module shut down");
381   }
382
383   if (osd)
384   {
385     osd->shutdown();
386     delete osd;
387     logger->log("Core", Log::NOTICE, "OSD module shut down");
388   }
389
390   if (audio)
391   {
392     audio->shutdown();
393     delete audio;
394     logger->log("Core", Log::NOTICE, "Audio module shut down");
395   }
396
397   if (video)
398   {
399     video->shutdown();
400     delete video;
401     logger->log("Core", Log::NOTICE, "Video module shut down");
402   }
403
404   if (timers)
405   {
406     timers->shutdown();
407     delete timers;
408     logger->log("Core", Log::NOTICE, "Timers module shut down");
409   }
410
411   if (mtd)
412   {
413     mtd->shutdown();
414     delete mtd;
415     logger->log("Core", Log::NOTICE, "MTD module shut down");
416   }
417
418   if (led)
419   {
420     led->shutdown();
421     delete led;
422     logger->log("Core", Log::NOTICE, "LED module shut down");
423   }
424
425   if (remote)
426   {
427     remote->shutdown();
428     delete remote;
429     logger->log("Core", Log::NOTICE, "Remote module shut down");
430   }
431
432   if (logger)
433   {
434     logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
435     logger->shutdown();
436     delete logger;
437   }
438
439   exit(code);
440 }
441
442 // -------------------------------------------------------------------------------------------------------------------
443
444 ULLONG ntohll(ULLONG a)
445 {
446   return htonll(a);
447 }
448
449 ULLONG htonll(ULLONG a)
450 {
451   #if BYTE_ORDER == BIG_ENDIAN
452     return a;
453   #else
454     ULLONG b = 0;
455
456     b = ((a << 56) & 0xFF00000000000000ULL)
457       | ((a << 40) & 0x00FF000000000000ULL)
458       | ((a << 24) & 0x0000FF0000000000ULL)
459       | ((a <<  8) & 0x000000FF00000000ULL)
460       | ((a >>  8) & 0x00000000FF000000ULL)
461       | ((a >> 24) & 0x0000000000FF0000ULL)
462       | ((a >> 40) & 0x000000000000FF00ULL)
463       | ((a >> 56) & 0x00000000000000FFULL) ;
464
465     return b;
466   #endif
467 }
468
469 void MILLISLEEP(ULONG a)
470 {
471 #ifndef WIN32
472   struct timespec delayTime;
473   delayTime.tv_sec = a / 1000;
474   delayTime.tv_nsec = (a % 1000) * 1000000;
475   nanosleep(&delayTime, NULL);
476 #else
477   Sleep(a);
478 #endif
479 }