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

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