]> git.vomp.tv Git - vompclient.git/blob - main.cc
Initial import
[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   success = osd->init("/dev/stbgfx", SCREENHEIGHT, SCREENWIDTH, 1);
175   if (success)
176   {
177     logger->log("Core", Log::INFO, "OSD module initialised");
178   }
179   else
180   {
181     logger->log("Core", Log::EMERG, "OSD module failed to initialise");
182     shutdown(1);
183   }
184
185   success = vdr->init(3024);
186   if (success)
187   {
188     logger->log("Core", Log::INFO, "VDR module initialised");
189   }
190   else
191   {
192     logger->log("Core", Log::EMERG, "VDR module failed to initialise");
193     shutdown(1);
194   }
195
196   success = video->init(Video::PAL, Video::SCART, Video::ASPECT4X3, Video::NORMAL);
197   if (success)
198   {
199     logger->log("Core", Log::INFO, "Video module initialised");
200   }
201   else
202   {
203     logger->log("Core", Log::EMERG, "Video module failed to initialise");
204     shutdown(1);
205   }
206
207   success = audio->init(Audio::MPEG2_PES);
208   if (success)
209   {
210     logger->log("Core", Log::INFO, "Audio module initialised");
211   }
212   else
213   {
214     logger->log("Core", Log::EMERG, "Audio module failed to initialise");
215     shutdown(1);
216   }
217
218   success = viewman->init();
219   if (success)
220   {
221     logger->log("Core", Log::INFO, "ViewMan module initialised");
222   }
223   else
224   {
225     logger->log("Core", Log::EMERG, "ViewMan module failed to initialise");
226     shutdown(1);
227   }
228
229   success = command->init();
230   if (success)
231   {
232     logger->log("Core", Log::INFO, "Command module initialised");
233   }
234   else
235   {
236     logger->log("Core", Log::EMERG, "Command module failed to initialise");
237     shutdown(1);
238   }
239
240   // Other init ------------------------------------------------------------------------------------------------------
241
242   logger->log("Core", Log::NOTICE, "Startup successful");
243
244   // Run main loop ---------------------------------------------------------------------------------------------------
245
246   // Ok, all major device components and other bits are loaded and ready
247   command->run();
248
249   // When that returns quit ------------------------------------------------------------------------------------------
250
251   shutdown(0);
252   return 0;
253 }
254
255 // -------------------------------------------------------------------------------------------------------------------
256
257 void shutdown(int code)
258 {
259   if (command)
260   {
261     command->shutdown();
262     delete command;
263     logger->log("Core", Log::NOTICE, "Command module shut down");
264   }
265
266   if (viewman)
267   {
268     viewman->shutdown();
269     delete viewman;
270     logger->log("Core", Log::NOTICE, "ViewMan module shut down");
271   }
272
273   if (audio)
274   {
275     audio->shutdown();
276     delete audio;
277     logger->log("Core", Log::NOTICE, "Audio module shut down");
278   }
279
280   if (video)
281   {
282     video->shutdown();
283     delete video;
284     logger->log("Core", Log::NOTICE, "Video module shut down");
285   }
286
287   if (vdr)
288   {
289     vdr->shutdown();
290     delete vdr;
291     logger->log("Core", Log::NOTICE, "VDR module shut down");
292   }
293
294   if (osd)
295   {
296     osd->shutdown();
297     delete osd;
298     logger->log("Core", Log::NOTICE, "OSD module shut down");
299   }
300
301   if (mtd)
302   {
303     mtd->shutdown();
304     delete mtd;
305     logger->log("Core", Log::NOTICE, "MTD module shut down");
306   }
307
308   if (led)
309   {
310     led->shutdown();
311     delete led;
312     logger->log("Core", Log::NOTICE, "LED module shut down");
313   }
314
315   if (remote)
316   {
317     remote->shutdown();
318     delete remote;
319     logger->log("Core", Log::NOTICE, "Remote module shut down");
320   }
321
322   if (logger)
323   {
324     logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
325     logger->shutdown();
326     delete logger;
327   }
328
329   exit(code);
330 }
331
332 // -------------------------------------------------------------------------------------------------------------------
333
334 void sighandler(int signal)
335 {
336   Log* logger = Log::getInstance();
337
338   logger->log("Core", Log::NOTICE, "Signal %i received", signal);
339
340   switch (signal)
341   {
342     case SIGINT:
343     {
344       logger->log("Core", Log::NOTICE, "Interrupt signal, shutting down...");
345       command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
346       break;
347     }
348     case SIGTERM:
349     {
350       logger->log("Core", Log::NOTICE, "Term signal, shutting down...");
351       command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
352       break;
353     }
354     case SIGUSR1:
355     {
356       logger->log("Core", Log::DEBUG, "SIGUSR1 caught");
357       logger->upLogLevel();
358       break;
359     }
360     case SIGUSR2:
361     {
362       logger->log("Core", Log::DEBUG, "SIGUSR2 caught");
363       logger->downLogLevel();
364       break;
365     }
366     case SIGURG:
367     {
368       logger->log("Core", Log::DEBUG, "SIGURG caught");
369       break;
370     }
371   }
372 }
373
374 // -------------------------------------------------------------------------------------------------------------------
375
376 ULLONG ntohll(ULLONG a)
377 {
378   return htonll(a);
379 }
380
381 ULLONG htonll(ULLONG a)
382 {
383   #if BYTE_ORDER == BIG_ENDIAN
384     return a;
385   #else
386     ULLONG b = 0;
387
388     b = ((a << 56) & 0xFF00000000000000ULL)
389       | ((a << 40) & 0x00FF000000000000ULL)
390       | ((a << 24) & 0x0000FF0000000000ULL)
391       | ((a <<  8) & 0x000000FF00000000ULL)
392       | ((a >>  8) & 0x00000000FF000000ULL)
393       | ((a >> 24) & 0x0000000000FF0000ULL)
394       | ((a >> 40) & 0x000000000000FF00ULL)
395       | ((a >> 56) & 0x00000000000000FFULL) ;
396
397     return b;
398   #endif
399 }