]> 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   // Set up signal handling ------------------------------------------------------------------------------------------
91
92   sighandler_t sigtest;
93
94   sigtest = signal(SIGPIPE, SIG_IGN);
95   if (sigtest == SIG_ERR)
96   {
97     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGPIPE. Aborting.");
98     shutdown(1);
99   }
100   sigtest = signal(SIGINT, sighandler);
101   if (sigtest == SIG_ERR)
102   {
103     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGINT. Aborting.");
104     shutdown(1);
105   }
106   sigtest = signal(SIGTERM, sighandler);
107   if (sigtest == SIG_ERR)
108   {
109     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGTERM. Aborting.");
110     shutdown(1);
111   }
112   sigtest = signal(SIGUSR1, sighandler);
113   if (sigtest == SIG_ERR)
114   {
115     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR1. Aborting.");
116     shutdown(1);
117   }
118   sigtest = signal(SIGUSR2, sighandler);
119   if (sigtest == SIG_ERR)
120   {
121     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR2. Aborting.");
122     shutdown(1);
123   }
124   sigtest = signal(SIGURG, sighandler);
125   if (sigtest == SIG_ERR)
126   {
127     logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGURG. Aborting.");
128     shutdown(1);
129   }
130
131   logger->log("Core", Log::INFO, "Signal handlers set up successfully");
132
133
134   // Init modules ----------------------------------------------------------------------------------------------------
135   int success;
136
137   success = remote->init("/dev/rawir");
138   if (success)
139   {
140     logger->log("Core", Log::INFO, "Remote module initialised");
141   }
142   else
143   {
144     logger->log("Core", Log::EMERG, "Remote module failed to initialise");
145     shutdown(1);
146   }
147
148   success = led->init(remote->getDevice());
149   if (success)
150   {
151     logger->log("Core", Log::INFO, "LED module initialised");
152   }
153   else
154   {
155     logger->log("Core", Log::EMERG, "LED module failed to initialise");
156     shutdown(1);
157   }
158
159   success = mtd->init("/dev/mtd1");
160   if (success)
161   {
162     logger->log("Core", Log::INFO, "Mtd module initialised");
163   }
164   else
165   {
166     logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
167     shutdown(1);
168   }
169
170   UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
171   if      (videoFormat == Video::PAL)  logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
172   else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
173   else                                 logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
174
175   success = video->init(videoFormat);
176   if (success)
177   {
178     logger->log("Core", Log::INFO, "Video module initialised");
179   }
180   else
181   {
182     logger->log("Core", Log::EMERG, "Video module failed to initialise");
183     shutdown(1);
184   }
185
186   success = osd->init("/dev/stbgfx", 1);
187   if (success)
188   {
189     logger->log("Core", Log::INFO, "OSD module initialised");
190   }
191   else
192   {
193     logger->log("Core", Log::EMERG, "OSD module failed to initialise");
194     shutdown(1);
195   }
196
197   success = audio->init(Audio::MPEG2_PES);
198   if (success)
199   {
200     logger->log("Core", Log::INFO, "Audio module initialised");
201   }
202   else
203   {
204     logger->log("Core", Log::EMERG, "Audio module failed to initialise");
205     shutdown(1);
206   }
207
208   success = vdr->init(3024);
209   if (success)
210   {
211     logger->log("Core", Log::INFO, "VDR module initialised");
212   }
213   else
214   {
215     logger->log("Core", Log::EMERG, "VDR module failed to initialise");
216     shutdown(1);
217   }
218
219   success = viewman->init();
220   if (success)
221   {
222     logger->log("Core", Log::INFO, "ViewMan module initialised");
223   }
224   else
225   {
226     logger->log("Core", Log::EMERG, "ViewMan module failed to initialise");
227     shutdown(1);
228   }
229
230   success = command->init();
231   if (success)
232   {
233     logger->log("Core", Log::INFO, "Command module initialised");
234   }
235   else
236   {
237     logger->log("Core", Log::EMERG, "Command module failed to initialise");
238     shutdown(1);
239   }
240
241   // Other init ------------------------------------------------------------------------------------------------------
242
243   logger->log("Core", Log::NOTICE, "Startup successful");
244
245   // Run main loop ---------------------------------------------------------------------------------------------------
246
247   // Ok, all major device components and other bits are loaded and ready
248   command->run();
249
250   // When that returns quit ------------------------------------------------------------------------------------------
251
252   shutdown(0);
253   return 0;
254 }
255
256 // -------------------------------------------------------------------------------------------------------------------
257
258 void shutdown(int code)
259 {
260   if (command)
261   {
262     command->shutdown();
263     delete command;
264     logger->log("Core", Log::NOTICE, "Command module shut down");
265   }
266
267   if (viewman)
268   {
269     viewman->shutdown();
270     delete viewman;
271     logger->log("Core", Log::NOTICE, "ViewMan module shut down");
272   }
273
274   if (vdr)
275   {
276     vdr->shutdown();
277     delete vdr;
278     logger->log("Core", Log::NOTICE, "VDR module shut down");
279   }
280
281   if (osd)
282   {
283     osd->shutdown();
284     delete osd;
285     logger->log("Core", Log::NOTICE, "OSD module shut down");
286   }
287
288   if (audio)
289   {
290     audio->shutdown();
291     delete audio;
292     logger->log("Core", Log::NOTICE, "Audio module shut down");
293   }
294
295   if (video)
296   {
297     video->shutdown();
298     delete video;
299     logger->log("Core", Log::NOTICE, "Video module shut down");
300   }
301
302   if (mtd)
303   {
304     mtd->shutdown();
305     delete mtd;
306     logger->log("Core", Log::NOTICE, "MTD module shut down");
307   }
308
309   if (led)
310   {
311     led->shutdown();
312     delete led;
313     logger->log("Core", Log::NOTICE, "LED module shut down");
314   }
315
316   if (remote)
317   {
318     remote->shutdown();
319     delete remote;
320     logger->log("Core", Log::NOTICE, "Remote module shut down");
321   }
322
323   if (logger)
324   {
325     logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
326     logger->shutdown();
327     delete logger;
328   }
329
330   exit(code);
331 }
332
333 // -------------------------------------------------------------------------------------------------------------------
334
335 void sighandler(int signalReceived)
336 {
337   logger->log("Core", Log::NOTICE, "Signal %i received", signal);
338
339   switch (signalReceived)
340   {
341     case SIGINT:
342     {
343       logger->log("Core", Log::NOTICE, "Interrupt signal, shutting down...");
344       command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
345       break;
346     }
347     case SIGTERM:
348     {
349       logger->log("Core", Log::NOTICE, "Term signal, shutting down...");
350       command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
351       break;
352     }
353     case SIGUSR1:
354     {
355       logger->log("Core", Log::DEBUG, "SIGUSR1 caught");
356       logger->upLogLevel();
357       break;
358     }
359     case SIGUSR2:
360     {
361       logger->log("Core", Log::DEBUG, "SIGUSR2 caught");
362       logger->downLogLevel();
363       break;
364     }
365     case SIGURG:
366     {
367       logger->log("Core", Log::DEBUG, "SIGURG caught");
368       break;
369     }
370   }
371 }
372
373 // -------------------------------------------------------------------------------------------------------------------
374
375 ULLONG ntohll(ULLONG a)
376 {
377   return htonll(a);
378 }
379
380 ULLONG htonll(ULLONG a)
381 {
382   #if BYTE_ORDER == BIG_ENDIAN
383     return a;
384   #else
385     ULLONG b = 0;
386
387     b = ((a << 56) & 0xFF00000000000000ULL)
388       | ((a << 40) & 0x00FF000000000000ULL)
389       | ((a << 24) & 0x0000FF0000000000ULL)
390       | ((a <<  8) & 0x000000FF00000000ULL)
391       | ((a >>  8) & 0x00000000FF000000ULL)
392       | ((a >> 24) & 0x0000000000FF0000ULL)
393       | ((a >> 40) & 0x000000000000FF00ULL)
394       | ((a >> 56) & 0x00000000000000FFULL) ;
395
396     return b;
397   #endif
398 }