Don't sigfpe as much
[ipdf/code.git] / src / main.cpp
1 #include "main.h"
2 #include <unistd.h> // Because we can.
3
4 #include "controlpanel.h"
5
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE
8 #endif
9 #include <fenv.h>
10 #include <signal.h>
11
12 bool ignore_sigfpe = false;
13 const char *script_filename;
14
15 void sigfpe_handler(int sig)
16 {
17         if (!ignore_sigfpe)
18                 Fatal("Floating point exception!");
19         exit(EXIT_SUCCESS);
20 }
21
22 int main(int argc, char ** argv)
23 {       
24         //Debug("Main!");
25         signal(SIGFPE, sigfpe_handler);
26         #if REALTYPE == REAL_IRRAM
27           iRRAM_initialize(argc,argv);
28         #endif
29         
30         #ifndef __STDC_IEC_559__
31         Warn("__STDC_IEC_559__ not defined. IEEE 754 floating point not fully supported.\n");
32         #endif
33
34         // We want to crash if we ever get a NaN.
35         // AH, so *this* is where that got enabled, I was looking for compiler flags
36         #ifndef __MINGW32__
37         feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
38         #endif
39
40         DebugRealInfo();
41
42         Document doc("","fonts/ComicSans.ttf");
43         srand(time(NULL));
44
45         enum {OUTPUT_TO_BMP, LOOP} mode = LOOP;
46         
47         
48         Colour c(0,0,0,1);
49         
50         const char * output_bmp = NULL;
51         const char * input_filename = NULL;
52         const char * input_text = NULL;
53         Real b[4] = {0,0,1,1};
54         int max_frames = -1;
55         bool hide_control_panel = false;
56         bool lazy_rendering = true;
57         bool window_visible = true;
58         bool gpu_transform = true;
59         bool gpu_rendering = true;
60         
61
62         
63         int i = 0;
64         while (++i < argc)
65         {
66                 if (argv[i][0] != '-')
67                 {
68                         input_filename = argv[i];
69                         continue;               
70                 }
71                 switch (argv[i][1])
72                 {
73                         case 'o':
74                                 mode = OUTPUT_TO_BMP;
75                                 if (++i >= argc)
76                                         Fatal("No output argument following -o switch");
77                                 output_bmp = argv[i];
78                                 hide_control_panel = true;
79                                 break;
80                         case 'b':
81                         {
82                                 Debug("Reading view bounds");
83                                 for (int j = 1; j <= 4; ++j)
84                                 {
85                                         if (i+j >= argc)
86                                                 Fatal("No %d bounds component following -b switch", j);
87                                         b[j-1] = RealFromStr(argv[i+j]);
88                                 }
89                                 i += 4;
90                                 break;
91                         }
92                         case 't':
93                         {
94                                 if (++i >= argc)
95                                         Fatal("No text input following -t switch");
96                                 input_text = argv[i];
97                                 Debug("Insert text: %s", input_text);
98                                 break;
99                         }
100                         
101                         case 'r':
102                         {
103                                 if (++i >= argc)
104                                         Fatal("Expected \"gpu\" or \"cpu\" after -r switch");
105                                 if (strcmp(argv[i], "gpu") == 0)
106                                 {
107                                         gpu_rendering = true;
108                                 }
109                                 else if (strcmp(argv[i], "cpu") == 0)
110                                 {
111                                         gpu_rendering = false;
112                                 }
113                                 else
114                                 {
115                                         Fatal("Expected \"gpu\" or \"cpu\" after -r switch, not \"%s\"", argv[i]);
116                                 }
117                                 break;
118                         }
119                         
120                         case 'T':
121                         {
122                                 if (++i >= argc)
123                                         Fatal("Expected \"gpu\" or \"cpu\" after -T switch");
124                                 if (strcmp(argv[i], "gpu") == 0)
125                                 {
126                                         gpu_transform = true;
127                                 }
128                                 else if (strcmp(argv[i], "cpu") == 0)
129                                 {
130                                         gpu_transform = false;
131                                 }
132                                 else
133                                 {
134                                         Fatal("Expected \"gpu\" or \"cpu\" after -T switch, not \"%s\"", argv[i]);
135                                 }
136                                 break;
137                         }
138                         
139                         
140                         case 'l':
141                                 lazy_rendering = !lazy_rendering;
142                                 break;
143                         
144                         case 'f':
145                                 if (++i >= argc)
146                                         Fatal("No frame number following -f switch");
147                                 max_frames = strtol(argv[i], NULL, 10);
148                                 hide_control_panel = true;
149                                 break;
150                                 
151                         case 'q':
152                                 hide_control_panel = true;
153                                 break;
154                                         
155                         case 'Q':
156                                 hide_control_panel = true;
157                                 window_visible = !window_visible;
158                                 break;
159                         case 's':
160                                 hide_control_panel = true;
161                                 if (++i >= argc)
162                                         Fatal("Expected filename after -s switch");
163                                 script_filename = argv[i];
164                                 break;
165                 }       
166         }
167
168         Rect bounds(b[0],b[1],b[2],b[3]);
169         Screen scr(window_visible);
170         View view(doc,scr, bounds);
171         
172         view.SetLazyRendering(lazy_rendering);
173         view.SetGPURendering(gpu_rendering);
174         view.SetGPUTransform(gpu_transform);
175
176         if (input_filename != NULL)
177         {
178                 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
179                         doc.LoadSVG(input_filename, Rect(Real(1)/Real(2),Real(1)/Real(2),Real(1)/Real(800),Real(1)/Real(600)));         
180                 #else
181                         doc.LoadSVG(input_filename, Rect(bounds.x+bounds.w/Real(2),bounds.y+bounds.h/Real(2),bounds.w/Real(800),bounds.h/Real(600)));
182                 #endif
183         }
184         else if (input_text != NULL)
185         {
186                 doc.AddText(input_text, bounds.h/Real(2), bounds.x, bounds.y+bounds.h/Real(2));
187         }
188
189
190
191         #ifndef CONTROLPANEL_DISABLED
192         if (!scr.Valid()) hide_control_panel = true;
193         SDL_Thread * cp_thread = NULL;
194         if (!hide_control_panel)
195         {
196                 ControlPanel::RunArgs args = {argc, argv, view, doc, scr};
197                 cp_thread = SDL_CreateThread(ControlPanel::Run, "ControlPanel", &args);
198                 if (cp_thread == NULL)
199                 {
200                         Error("Couldn't create ControlPanel thread: %s", SDL_GetError());
201                 }
202         }
203         #else //CONTROLPANEL_DISABLED
204                 Debug("No control panel, hide_control_panel is %d", hide_control_panel);
205         #endif 
206
207         if (mode == LOOP)
208                 MainLoop(doc, scr, view, max_frames);
209         else if (mode == OUTPUT_TO_BMP)
210         {
211                 view.SaveBMP(output_bmp);
212         }
213                 
214         #ifndef CONTROLPANEL_DISABLED
215                 if (cp_thread != NULL)
216                 {
217                         int cp_return;
218                         qApp->quit(); // will close the control panel
219                         // (seems to not explode if the qApp has already been quit)
220                         SDL_WaitThread(cp_thread, &cp_return);
221                         Debug("ControlPanel thread returned %d", cp_return);
222                 }
223         #endif //CONTROLPANEL_DISABLED
224         
225         ignore_sigfpe = true;
226         return 0;
227 }

UCC git Repository :: git.ucc.asn.au