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