Splash screen: Flush display after drawing.
[pspp] / src / ui / gui / main.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2005, 2006, 2010, 2011, 2012, 2013, 2014, 2015  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "ui/gui/psppire.h"
20
21 #include <gtk/gtk.h>
22 #include <stdlib.h>
23
24 #include "language/lexer/include-path.h"
25 #include "libpspp/argv-parser.h"
26 #include "libpspp/array.h"
27 #include "libpspp/assertion.h"
28 #include "libpspp/cast.h"
29 #include "libpspp/copyleft.h"
30 #include "libpspp/str.h"
31 #include "libpspp/string-array.h"
32 #include "libpspp/version.h"
33 #include "ui/source-init-opts.h"
34
35 #include "gl/configmake.h"
36 #include "gl/progname.h"
37 #include "gl/relocatable.h"
38 #include "gl/version-etc.h"
39 #include "gl/xalloc.h"
40
41 #include "gettext.h"
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) msgid
44
45
46 GdkWindow *create_splash_window (GMainContext *context);
47 gboolean destroy_splash_window (gpointer ud);
48
49
50
51 \f
52 /* Arguments to be interpreted before the X server gets initialised */
53
54 enum
55   {
56     OPT_HELP,
57     OPT_VERSION,
58     OPT_NO_SPLASH,
59     OPT_MEASURE_STARTUP,
60     N_STARTUP_OPTIONS
61   };
62
63 static const struct argv_option startup_options[N_STARTUP_OPTIONS] =
64   {
65     {"help",      'h', no_argument, OPT_HELP},
66     {"version",   'V', no_argument, OPT_VERSION},
67     {"no-splash", 'q', no_argument, OPT_NO_SPLASH},
68     {"measure-startup", 0, no_argument, OPT_MEASURE_STARTUP},
69   };
70
71 /* --measure-startup: Prints the elapsed time to start up and load any file
72    specified on the command line. */
73 static gboolean measure_startup;
74 static GTimer *startup;
75
76 static void
77 usage (void)
78 {
79   char *inc_path = string_array_join (include_path_default (), " ");
80   GOptionGroup *gtk_options;
81   GOptionContext *ctx;
82   gchar *gtk_help_base, *gtk_help;
83
84   /* Get help text for GTK+ options.  */
85   ctx = g_option_context_new ("psppire");
86   gtk_options = gtk_get_option_group (FALSE);
87   gtk_help_base = g_option_context_get_help (ctx, FALSE, gtk_options);
88   g_option_context_free (ctx);
89
90   /* The GTK+ help text starts with usage instructions that we don't want,
91      followed by a blank line.  Trim off everything up to and including the
92      first blank line. */
93   gtk_help = strstr (gtk_help_base, "\n\n");
94   gtk_help = gtk_help != NULL ? gtk_help + 2 : gtk_help_base;
95
96   printf (_("\
97 PSPPIRE, a GUI for PSPP, a program for statistical analysis of sampled data.\n\
98 Usage: %s [OPTION]... FILE\n\
99 \n\
100 Arguments to long options also apply to equivalent short options.\n\
101 \n\
102 GUI options:\n\
103   -q, --no-splash           don't show splash screen during startup\n\
104 \n\
105 %s\
106 Language options:\n\
107   -I, --include=DIR         append DIR to search path\n\
108   -I-, --no-include         clear search path\n\
109   -a, --algorithm={compatible|enhanced}\n\
110                             set to `compatible' if you want output\n\
111                             calculated from broken algorithms\n\
112   -x, --syntax={compatible|enhanced}\n\
113                             set to `compatible' to disable PSPP extensions\n\
114   -i, --interactive         interpret syntax in interactive mode\n\
115   -s, --safer               don't allow some unsafe operations\n\
116 Default search path: %s\n\
117 \n\
118 Informative output:\n\
119   -h, --help                display this help and exit\n\
120   -V, --version             output version information and exit\n\
121 \n\
122 A non-option argument is interpreted as a data file in .sav or .zsav or .por\n\
123 format or a syntax file to load.\n"),
124           program_name, gtk_help, inc_path);
125
126   free (inc_path);
127   g_free (gtk_help_base);
128
129   emit_bug_reporting_address ();
130   exit (EXIT_SUCCESS);
131 }
132
133 static void
134 startup_option_callback (int id, void *show_splash_)
135 {
136   gboolean *show_splash = show_splash_;
137
138   switch (id)
139     {
140     case OPT_HELP:
141       usage ();
142       break;
143
144     case OPT_VERSION:
145       version_etc (stdout, "psppire", PACKAGE_NAME, PACKAGE_VERSION,
146                    "Ben Pfaff", "John Darrington", "Jason Stover",
147                    NULL_SENTINEL);
148       exit (EXIT_SUCCESS);
149
150     case OPT_NO_SPLASH:
151       *show_splash = FALSE;
152       break;
153
154     case OPT_MEASURE_STARTUP:
155       measure_startup = TRUE;
156       break;
157
158     default:
159       NOT_REACHED ();
160     }
161 }
162
163 static gboolean
164 print_startup_time (gpointer data)
165 {
166   g_timer_stop (startup);
167   printf ("%.3f seconds elapsed\n", g_timer_elapsed (startup, NULL));
168   g_timer_destroy (startup);
169   startup = NULL;
170
171   return FALSE;
172 }
173
174 static GMemVTable vtable =
175   {
176     xmalloc,
177     xrealloc,
178     free,
179     xcalloc,
180     malloc,
181     realloc
182   };
183
184 #ifdef __APPLE__
185 static const bool apple = true;
186 #else
187 static const bool apple = false;
188 #endif
189
190 /* Searches ARGV for the -psn_xxxx option that the desktop application
191    launcher passes in, and removes it if it finds it.  Returns the new value
192    of ARGC. */
193 static inline int
194 remove_psn (int argc, char **argv)
195 {
196   if (apple)
197     {
198       int i;
199
200       for (i = 0; i < argc; i++)
201         {
202           if (!strncmp (argv[i], "-psn", 4))
203             {
204               remove_element (argv, argc + 1, sizeof *argv, i);
205               return argc - 1;
206             }
207         }
208     }
209   return argc;
210 }
211
212 \f
213
214 struct init_source
215 {
216   GSource parent;
217   int state;
218   GMainLoop *loop;
219   gchar *file;
220 };
221
222
223 gboolean
224 init_prepare (GSource *source, gint *timeout_)
225 {
226   return TRUE;
227 }
228
229
230
231 gboolean
232 init_check (GSource *source)
233 {
234   return TRUE;
235 }
236
237
238 gboolean
239 init_dispatch (GSource *ss,
240                GSourceFunc callback,
241                gpointer user_data)
242 {
243   struct init_source *is = (struct init_source *)ss;
244
245   bool finished = initialize (is->file, is->state++);
246   
247   if (finished)
248     {
249       g_main_loop_quit (is->loop);
250       return FALSE;
251     }
252
253   return TRUE;
254 }
255
256 static GSourceFuncs init_funcs = {init_prepare, init_check, init_dispatch, NULL};
257
258 \f
259
260 int
261 main (int argc, char *argv[])
262 {
263   gboolean show_splash = TRUE;
264   struct argv_parser *parser;
265   const gchar *vers;
266
267   set_program_name (argv[0]);
268
269   g_mem_set_vtable (&vtable);
270
271 #if !GLIB_CHECK_VERSION(2,32,0)
272   /* g_thread_init() was required before glib 2.32, but it is deprecated since
273      then and calling it yields a compile-time warning. */
274   g_thread_init (NULL);
275 #endif
276
277   gtk_disable_setlocale ();
278
279   startup = g_timer_new ();
280   g_timer_start (startup);
281
282   if ( ! gtk_parse_args (&argc, &argv) )
283     {
284       perror ("Error parsing arguments");
285       exit (1);
286     }
287
288   if ( (vers = gtk_check_version (GTK_MAJOR_VERSION,
289                                  GTK_MINOR_VERSION,
290                                  GTK_MICRO_VERSION)) )
291     {
292       g_warning ("%s", vers);
293     }
294
295   argc = remove_psn (argc, argv);
296
297   /* Parse our own options. 
298      This must come BEFORE gdk_init otherwise options such as 
299      --help --version which ought to work without an X server, won't.
300   */
301   parser = argv_parser_create ();
302   argv_parser_add_options (parser, startup_options, N_STARTUP_OPTIONS,
303                            startup_option_callback, &show_splash);
304   source_init_register_argv_parser (parser);
305   if (!argv_parser_run (parser, argc, argv))
306     exit (EXIT_FAILURE);
307   argv_parser_destroy (parser);
308
309   /* Initialise GDK.  Theoretically this call can remove options from argc,argv if
310      it thinks they are gdk options.
311      However there shouldn't be any here because of the gtk_parse_args call above. */
312   gdk_init (&argc, &argv);
313
314   GMainContext *context = g_main_context_new ();
315   
316   GdkWindow *win = show_splash ? create_splash_window (context) : NULL;
317
318   GMainLoop *loop = g_main_loop_new (context, FALSE);
319
320   GSource *ss = g_source_new (&init_funcs,
321                               sizeof (struct init_source));
322   
323   ((struct init_source *) ss)->state = 0;
324   
325   g_source_set_priority (ss, G_PRIORITY_DEFAULT);
326     
327   g_source_attach (ss, context);
328
329   ((struct init_source *) ss)->loop = loop;
330   ((struct init_source *) ss)->file = optind < argc ? argv[optind] : NULL;
331   
332   g_source_unref (ss);
333
334   g_main_loop_run (loop);
335
336   g_main_loop_unref (loop);
337   g_main_context_unref (context);
338
339   if (win)
340     g_timeout_add (500, destroy_splash_window, win);
341
342   gtk_main ();
343
344   /* Not much point in this except to check for memory leaks */
345   de_initialize ();
346   
347   return 0;
348 }
349
350
351
352 \f
353
354 struct splash_source
355 {
356   GSource parent;
357   cairo_surface_t *sfc;
358 };
359
360 void
361 fill_splash_window (GdkWindow *win, cairo_surface_t *sfce)
362 {
363   cairo_t *cr = gdk_cairo_create (win);
364   
365   cairo_set_source_surface (cr, sfce, 0, 0);
366   
367   cairo_paint (cr);
368   cairo_destroy (cr);
369 }
370
371 gboolean
372 splash_prepare  (GSource    *source,
373             gint       *timeout_)
374 {
375   GdkEvent *e = gdk_event_peek ();
376   if (!e)
377     return FALSE;
378
379   gdk_event_free (e);
380   return TRUE;
381 }
382
383 gboolean
384 splash_check   (GSource    *source)
385 {
386   GdkEvent *e = gdk_event_peek ();
387   if (!e)
388     return FALSE;
389
390   gdk_event_free (e);
391   return TRUE;
392 }
393
394
395 gboolean
396 splash_dispatch (GSource *ss,
397             GSourceFunc callback,
398             gpointer    user_data)
399 {
400   struct splash_source *source = (struct splash_source *) ss;
401   GdkEvent *e = gdk_event_get ();
402   if (!e)
403     return TRUE;
404
405   GdkWindow *w = ((GdkEventAny *)e)->window;
406
407   if (!w)
408     {
409       gdk_event_free (e);
410       return TRUE;
411     }
412
413   fill_splash_window (w, source->sfc);
414   gdk_display_flush (gdk_window_get_display (w));
415
416   gdk_event_free (e);
417
418   return TRUE;
419 }
420
421
422 gboolean
423 destroy_splash_window (gpointer ud)
424 {
425   GdkWindow *win = GDK_WINDOW (ud);
426   gdk_window_withdraw (win);
427   gdk_display_flush (gdk_window_get_display (win));
428   gdk_window_destroy (win);
429   
430   return FALSE;
431 }
432
433 GSourceFuncs splash_funcs = {splash_prepare, splash_check, splash_dispatch, NULL};
434
435
436 GdkWindow *
437 create_splash_window (GMainContext *context)
438 {
439   const gchar *filename = PKGDATADIR "/splash.png";
440
441   const char *relocated_filename = relocate (filename);
442   cairo_surface_t *the_surface = 
443     cairo_image_surface_create_from_png  (relocated_filename);
444
445   if (filename != relocated_filename)
446     free (CONST_CAST (char *, relocated_filename));
447
448   
449   g_return_val_if_fail (the_surface, NULL);
450     
451   int attr_mask = GDK_WA_TYPE_HINT;
452   GdkWindowAttr attr;
453     
454   attr.width =  cairo_image_surface_get_width (the_surface);
455   attr.height = cairo_image_surface_get_height (the_surface);
456   attr.wclass = GDK_INPUT_OUTPUT; 
457   attr.window_type = GDK_WINDOW_TOPLEVEL;
458     
459   attr.type_hint = GDK_WINDOW_TYPE_HINT_SPLASHSCREEN;
460     
461   GdkWindow *win = gdk_window_new (NULL, &attr, attr_mask);
462     
463   gdk_window_set_events (win, GDK_EXPOSURE_MASK);
464   gdk_window_set_keep_above (win, TRUE);
465   gdk_window_show (win);
466   
467
468   GSource *ss = g_source_new (&splash_funcs,
469                               sizeof (struct splash_source));
470   
471   ((struct splash_source *) ss)->sfc = the_surface;
472   g_source_set_priority (ss, G_PRIORITY_HIGH);
473     
474   g_source_attach (ss, context);
475
476   g_source_unref (ss);
477     
478   return win;
479 }
480
481