]> git.vomp.tv Git - vompclient.git/blob - main.cc
NTSC support, needs testing
[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   // temp hack - simulate ntsc on pal
218 //  SCREENHEIGHT = 480;
219 //  videoFormat = Video::NTSC;
220
221   success = osd->init("/dev/stbgfx", SCREENHEIGHT, SCREENWIDTH, videoFormat, 1);
222   if (success)
223   {
224     logger->log("Core", Log::INFO, "OSD module initialised");
225   }
226   else
227   {
228     logger->log("Core", Log::EMERG, "OSD module failed to initialise");
229     shutdown(1);
230   }
231
232   success = vdr->init(3024);
233   if (success)
234   {
235     logger->log("Core", Log::INFO, "VDR module initialised");
236   }
237   else
238   {
239     logger->log("Core", Log::EMERG, "VDR module failed to initialise");
240     shutdown(1);
241   }
242
243   success = video->init(videoFormat, videoConnection, Video::ASPECT4X3, Video::NORMAL);
244   if (success)
245   {
246     logger->log("Core", Log::INFO, "Video module initialised");
247   }
248   else
249   {
250     logger->log("Core", Log::EMERG, "Video module failed to initialise");
251     shutdown(1);
252   }
253
254   success = audio->init(Audio::MPEG2_PES);
255   if (success)
256   {
257     logger->log("Core", Log::INFO, "Audio module initialised");
258   }
259   else
260   {
261     logger->log("Core", Log::EMERG, "Audio module failed to initialise");
262     shutdown(1);
263   }
264
265   success = viewman->init();
266   if (success)
267   {
268     logger->log("Core", Log::INFO, "ViewMan module initialised");
269   }
270   else
271   {
272     logger->log("Core", Log::EMERG, "ViewMan module failed to initialise");
273     shutdown(1);
274   }
275
276   success = command->init();
277   if (success)
278   {
279     logger->log("Core", Log::INFO, "Command module initialised");
280   }
281   else
282   {
283     logger->log("Core", Log::EMERG, "Command module failed to initialise");
284     shutdown(1);
285   }
286
287   // Other init ------------------------------------------------------------------------------------------------------
288
289   logger->log("Core", Log::NOTICE, "Startup successful");
290
291   // Run main loop ---------------------------------------------------------------------------------------------------
292
293   // Ok, all major device components and other bits are loaded and ready
294   command->run();
295
296   // When that returns quit ------------------------------------------------------------------------------------------
297
298   shutdown(0);
299   return 0;
300 }
301
302 // -------------------------------------------------------------------------------------------------------------------
303
304 void shutdown(int code)
305 {
306   if (command)
307   {
308     command->shutdown();
309     delete command;
310     logger->log("Core", Log::NOTICE, "Command module shut down");
311   }
312
313   if (viewman)
314   {
315     viewman->shutdown();
316     delete viewman;
317     logger->log("Core", Log::NOTICE, "ViewMan module shut down");
318   }
319
320   if (audio)
321   {
322     audio->shutdown();
323     delete audio;
324     logger->log("Core", Log::NOTICE, "Audio module shut down");
325   }
326
327   if (video)
328   {
329     video->shutdown();
330     delete video;
331     logger->log("Core", Log::NOTICE, "Video module shut down");
332   }
333
334   if (vdr)
335   {
336     vdr->shutdown();
337     delete vdr;
338     logger->log("Core", Log::NOTICE, "VDR module shut down");
339   }
340
341   if (osd)
342   {
343     osd->shutdown();
344     delete osd;
345     logger->log("Core", Log::NOTICE, "OSD module shut down");
346   }
347
348   if (mtd)
349   {
350     mtd->shutdown();
351     delete mtd;
352     logger->log("Core", Log::NOTICE, "MTD module shut down");
353   }
354
355   if (led)
356   {
357     led->shutdown();
358     delete led;
359     logger->log("Core", Log::NOTICE, "LED module shut down");
360   }
361
362   if (remote)
363   {
364     remote->shutdown();
365     delete remote;
366     logger->log("Core", Log::NOTICE, "Remote module shut down");
367   }
368
369   if (logger)
370   {
371     logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
372     logger->shutdown();
373     delete logger;
374   }
375
376   exit(code);
377 }
378
379 // -------------------------------------------------------------------------------------------------------------------
380
381 void sighandler(int signal)
382 {
383   Log* logger = Log::getInstance();
384
385   logger->log("Core", Log::NOTICE, "Signal %i received", signal);
386
387   switch (signal)
388   {
389     case SIGINT:
390     {
391       logger->log("Core", Log::NOTICE, "Interrupt signal, shutting down...");
392       command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
393       break;
394     }
395     case SIGTERM:
396     {
397       logger->log("Core", Log::NOTICE, "Term signal, shutting down...");
398       command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
399       break;
400     }
401     case SIGUSR1:
402     {
403       logger->log("Core", Log::DEBUG, "SIGUSR1 caught");
404       logger->upLogLevel();
405       break;
406     }
407     case SIGUSR2:
408     {
409       logger->log("Core", Log::DEBUG, "SIGUSR2 caught");
410       logger->downLogLevel();
411       break;
412     }
413     case SIGURG:
414     {
415       logger->log("Core", Log::DEBUG, "SIGURG caught");
416       break;
417     }
418   }
419 }
420
421 // -------------------------------------------------------------------------------------------------------------------
422
423 ULLONG ntohll(ULLONG a)
424 {
425   return htonll(a);
426 }
427
428 ULLONG htonll(ULLONG a)
429 {
430   #if BYTE_ORDER == BIG_ENDIAN
431     return a;
432   #else
433     ULLONG b = 0;
434
435     b = ((a << 56) & 0xFF00000000000000ULL)
436       | ((a << 40) & 0x00FF000000000000ULL)
437       | ((a << 24) & 0x0000FF0000000000ULL)
438       | ((a <<  8) & 0x000000FF00000000ULL)
439       | ((a >>  8) & 0x00000000FF000000ULL)
440       | ((a >> 24) & 0x0000000000FF0000ULL)
441       | ((a >> 40) & 0x000000000000FF00ULL)
442       | ((a >> 56) & 0x00000000000000FFULL) ;
443
444     return b;
445   #endif
446 }