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