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

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