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