51d225765c12796d21cdaf1aff7f8f02dc5a8eea
[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, 2016  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 #if ENABLE_RELOCATABLE && defined(__APPLE__)
24 #include <sys/stat.h>
25 #endif
26
27 #include "language/lexer/include-path.h"
28 #include "libpspp/argv-parser.h"
29 #include "libpspp/array.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/cast.h"
32 #include "libpspp/copyleft.h"
33 #include "libpspp/str.h"
34 #include "libpspp/string-array.h"
35 #include "libpspp/version.h"
36 #include "ui/source-init-opts.h"
37 #include "ui/gui/psppire-syntax-window.h"
38 #include "ui/gui/psppire-data-window.h"
39 #include "ui/gui/psppire-output-window.h"
40
41 #include "gl/configmake.h"
42 #include "gl/progname.h"
43 #include "gl/relocatable.h"
44 #include "gl/version-etc.h"
45 #include "gl/xalloc.h"
46
47 #include "gettext.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) msgid
50
51
52
53 static gboolean
54 show_version_and_exit ()
55 {
56   version_etc (stdout, "psppire", PACKAGE_NAME, PACKAGE_VERSION,
57                "Ben Pfaff", "John Darrington", "Jason Stover", NULL_SENTINEL);
58
59   exit (0);
60
61   return TRUE;
62 }
63
64 \f
65
66 gboolean
67 init_prepare (GSource * source, gint * timeout_)
68 {
69   return TRUE;
70 }
71
72 gboolean
73 init_check (GSource * source)
74 {
75   return TRUE;
76 }
77
78 gboolean
79 init_dispatch (GSource * ss, GSourceFunc callback, gpointer user_data)
80 {
81   struct init_source *is = (struct init_source *) ss;
82
83   bool finished = initialize (is);
84   is->state++;
85
86   if (finished)
87     {
88       g_main_loop_quit (is->loop);
89       return FALSE;
90     }
91
92   return TRUE;
93 }
94
95 static GSourceFuncs init_funcs =
96   { init_prepare, init_check, init_dispatch, NULL };
97 \f
98
99
100 GtkWidget *wsplash = 0;
101 gint64 start_time = 0;
102
103
104 static GtkWidget *
105 create_splash_window (void)
106 {
107   GtkWidget *sp = gtk_window_new (GTK_WINDOW_TOPLEVEL);
108
109   const gchar *filename = PKGDATADIR "/splash.png";
110   const char *relocated_filename = relocate (filename);
111   GtkWidget *l = gtk_image_new_from_file (relocated_filename);
112   if (filename != relocated_filename)
113     free (CONST_CAST (char *, relocated_filename));
114
115   gtk_container_add (GTK_CONTAINER (sp), l);
116   gtk_window_set_type_hint (GTK_WINDOW (sp),
117                             GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
118   gtk_window_set_position (GTK_WINDOW (sp), GTK_WIN_POS_CENTER);
119   gtk_window_set_skip_pager_hint (GTK_WINDOW (sp), TRUE);
120   gtk_window_set_skip_taskbar_hint (GTK_WINDOW (sp), TRUE);
121   gtk_window_set_focus_on_map (GTK_WINDOW (sp), FALSE);
122   gtk_window_set_accept_focus (GTK_WINDOW (sp), FALSE);
123
124   GdkGeometry hints;
125   hints.max_height = 100;
126   hints.max_width = 200;
127   gtk_window_set_geometry_hints (GTK_WINDOW (sp),
128                                  NULL, &hints, GDK_HINT_MAX_SIZE);
129
130
131   gtk_window_set_gravity (GTK_WINDOW (sp), GDK_GRAVITY_CENTER);
132
133   gtk_window_set_modal (GTK_WINDOW (sp), TRUE);
134   gtk_window_set_decorated (GTK_WINDOW (sp), FALSE);
135   gtk_window_set_keep_above (GTK_WINDOW (sp), TRUE);
136   gtk_widget_show_all (sp);
137   return sp;
138 }
139
140
141 static gint
142 on_local_options (GApplication * application,
143                   GVariantDict * options, gpointer user_data)
144 {
145   {
146     GVariant *b =
147       g_variant_dict_lookup_value (options, "no-unique",
148                                    G_VARIANT_TYPE_BOOLEAN);
149     if (b)
150       {
151         GApplicationFlags flags =  g_application_get_flags (application);
152         flags |= G_APPLICATION_NON_UNIQUE;
153         g_application_set_flags (application, flags);
154         g_variant_unref (b);
155       }
156   }
157   {
158     GVariant *b =
159       g_variant_dict_lookup_value (options, "no-splash",
160                                    G_VARIANT_TYPE_BOOLEAN);
161     if (b)
162       g_variant_unref (b);
163     else
164       start_time = g_get_monotonic_time ();
165   }
166
167
168   return -1;
169 }
170
171
172 static void
173 on_startup (GApplication * app, gpointer ud)
174 {
175   GMainContext *context = g_main_context_new ();
176
177   if (start_time != 0)
178     {
179       wsplash = create_splash_window ();
180       gtk_application_add_window (GTK_APPLICATION (app),
181                                   GTK_WINDOW (wsplash));
182     }
183
184   GMainLoop *loop = g_main_loop_new (context, FALSE);
185
186   GSource *ss = g_source_new (&init_funcs, sizeof (struct init_source));
187
188   ((struct init_source *) ss)->loop = loop;
189   ((struct init_source *) ss)->state = 0;
190
191   g_source_set_priority (ss, G_PRIORITY_DEFAULT);
192
193   g_source_attach (ss, context);
194   g_main_loop_run (loop);
195 }
196
197
198 static void
199 post_initialise (GApplication * app)
200 {
201   register_selection_functions ();
202   psppire_output_window_setup ();
203
204   GSimpleAction *quit = g_simple_action_new ("quit", NULL);
205   g_signal_connect_swapped (quit, "activate", G_CALLBACK (psppire_quit), app);
206   g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit));
207 }
208
209
210 #define SPLASH_DURATION 1000
211
212 static gboolean
213 destroy_splash (gpointer ud)
214 {
215   GtkWidget *sp = GTK_WIDGET (ud);
216   gtk_widget_destroy (sp);
217   wsplash = NULL;
218   return G_SOURCE_REMOVE;
219 }
220
221
222 static void
223 wait_for_splash (GApplication *app, GtkWindow *x)
224 {
225   if (wsplash)
226     {
227       gtk_window_set_transient_for (GTK_WINDOW (wsplash), x);
228       gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (wsplash));
229       gtk_window_set_keep_above (GTK_WINDOW (wsplash), TRUE);
230       gtk_window_present (GTK_WINDOW (wsplash));
231
232       /* Remove the splash screen after SPLASH_DURATION milliseconds */
233       gint64 elapsed_time = (g_get_monotonic_time () - start_time) / 1000;
234       if (SPLASH_DURATION - elapsed_time <= 0)
235         destroy_splash (wsplash);
236       else
237         g_timeout_add (SPLASH_DURATION - elapsed_time, destroy_splash, wsplash);
238     }
239 }
240
241 static void
242 on_activate (GApplication * app, gpointer ud)
243 {
244   post_initialise (app);
245
246   GtkWindow *x = create_data_window ();
247   gtk_application_add_window (GTK_APPLICATION (app), x);
248
249   wait_for_splash (app, x);
250 }
251
252 GtkWindow *
253 find_empty_data_window (GApplication *app)
254 {
255   GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
256   while (wl)
257     {
258       if (wl->data && PSPPIRE_IS_DATA_WINDOW (GTK_WINDOW (wl->data)) &&
259           psppire_data_window_is_empty (PSPPIRE_DATA_WINDOW (wl->data)))
260         return GTK_WINDOW (wl->data);
261       wl = wl->next;
262     }
263   return NULL;
264 }
265
266 GtkWindow *
267 find_psppire_window (GApplication *app)
268 {
269   GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
270   while (wl)
271     {
272       if (wl->data && PSPPIRE_IS_WINDOW (GTK_WINDOW (wl->data)))
273         return GTK_WINDOW (wl->data);
274       wl = wl->next;
275     }
276   return NULL;
277 }
278
279 static void
280 on_open (GApplication *app, GFile **files, gint n_files, gchar * hint,
281          gpointer ud)
282 {
283   /* If the application is already open and we open another file
284      via xdg-open on GNU/Linux or via the file manager, then open is
285      called. Check if we already have a psppire window. */
286   if (find_psppire_window (app) == NULL)
287     post_initialise (app);
288
289   /* When a new data file is opened, then try to find an empty
290      data window which will then be replaced as in the open file
291      dialog */
292   GtkWindow *victim = find_empty_data_window (app);
293
294   gchar *file = g_file_get_parse_name (files[0]);
295   GtkWindow *x = psppire_preload_file (file, victim);
296   g_free (file);
297
298   wait_for_splash (app, x);
299 }
300
301
302 /* These are arguments which must be processed BEFORE the X server has been initialised */
303 static void
304 process_pre_start_arguments (int *argc, char ***argv)
305 {
306   GOptionEntry oe[] = {
307     {"version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
308      show_version_and_exit, N_("Show version information and exit"), 0},
309     {NULL}
310   };
311
312   GOptionContext *oc = g_option_context_new ("");
313   g_option_context_set_help_enabled (oc, FALSE);
314   g_option_context_set_ignore_unknown_options (oc, FALSE);
315   g_option_context_add_main_entries (oc, oe, NULL);
316   g_option_context_parse (oc, argc, argv, NULL);
317   g_option_context_free (oc);
318 }
319
320 #if ENABLE_RELOCATABLE && defined(__APPLE__)
321 static void
322 pspp_macos_setenv (const char * progname)
323 {
324   /* helper to set environment variables for pspp to be relocatable.
325    * Due to the latest changes it is not recommended to set it in the shell
326    * wrapper anymore.
327    */
328   gchar resolved_path[PATH_MAX];
329   /* on some OSX installations open file limit is 256 and GIMP needs more */
330   struct rlimit limit;
331   limit.rlim_cur = 10000;
332   limit.rlim_max = 10000;
333   setrlimit (RLIMIT_NOFILE, &limit);
334   if (realpath (progname, resolved_path))
335     {
336       gchar  tmp[PATH_MAX];
337       gchar *app_dir;
338       gchar  res_dir[PATH_MAX];
339       struct stat sb;
340
341       app_dir = g_path_get_dirname (resolved_path);
342       g_snprintf (tmp, sizeof(tmp), "%s/../../Resources", app_dir);
343       if (realpath (tmp, res_dir) && !stat (res_dir,&sb) && S_ISDIR (sb.st_mode))
344         g_print ("pspp is started as MacOS application\n");
345       else
346         return;
347       g_free (app_dir);
348
349       g_snprintf (tmp, sizeof(tmp), "%s/lib/gtk-3.0/3.0.0", res_dir);
350       g_setenv ("GTK_PATH", tmp, TRUE);
351       g_snprintf (tmp, sizeof(tmp), "%s/etc/gtk-3.0/gtk.immodules", res_dir);
352       g_setenv ("GTK_IM_MODULE_FILE", tmp, TRUE);
353       g_snprintf (tmp, sizeof(tmp), "%s/lib/gegl-0.4", res_dir);
354       g_setenv ("GEGL_PATH", tmp, TRUE);
355       g_snprintf (tmp, sizeof(tmp), "%s/lib/babl-0.1", res_dir);
356       g_setenv ("BABL_PATH", tmp, TRUE);
357       g_snprintf (tmp, sizeof(tmp), "%s/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache", res_dir);
358       g_setenv ("GDK_PIXBUF_MODULE_FILE", tmp, TRUE);
359       g_snprintf (tmp, sizeof(tmp), "%s/etc/fonts", res_dir);
360       g_setenv ("FONTCONFIG_PATH", tmp, TRUE);
361       g_snprintf (tmp, sizeof(tmp), "%s/lib/gio/modules", res_dir);
362       g_setenv ("GIO_MODULE_DIR", tmp, TRUE);
363       g_snprintf (tmp, sizeof(tmp), "%s/etc/xdg", res_dir);
364       g_setenv ("XDG_CONFIG_DIRS", tmp, TRUE);
365       g_snprintf (tmp, sizeof(tmp), "%s/share", res_dir);
366       g_setenv ("XDG_DATA_DIRS", tmp, TRUE);
367
368       if (g_getenv ("HOME")!=NULL)
369         {
370           g_snprintf (tmp, sizeof(tmp),
371                       "%s/Library/Application Support/pspp/1.3/cache",
372                       g_getenv("HOME"));
373           g_setenv ("XDG_CACHE_HOME", tmp, TRUE);
374         }
375     }
376 }
377 #endif
378
379 int
380 main (int argc, char *argv[])
381 {
382
383 #if ENABLE_RELOCATABLE && defined(__APPLE__)
384   /* remove MacOS session identifier from the command line args */
385   gint newargc = 0;
386   for (gint i = 0; i < argc; i++)
387     {
388       if (!g_str_has_prefix (argv[i], "-psn_"))
389         {
390           argv[newargc] = argv[i];
391           newargc++;
392         }
393     }
394   if (argc > newargc)
395     {
396       argv[newargc] = NULL; /* glib expects NULL terminated array */
397       argc = newargc;
398     }
399   pspp_macos_setenv (argv[0]);
400 #endif
401
402   set_program_name (argv[0]);
403
404   GtkApplication *app =
405     gtk_application_new ("gnu.pspp", G_APPLICATION_HANDLES_OPEN);
406
407   process_pre_start_arguments (&argc, &argv);
408
409   GOptionEntry oe[] = {
410     {"no-splash", 'q', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
411       N_("Do not display the splash screen"), 0},
412     {"no-unique", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
413       N_("Do not attempt single instance negotiation"), 0},
414     {NULL}
415   };
416
417   g_application_add_main_option_entries (G_APPLICATION (app), oe);
418
419   g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL);
420   g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
421   g_signal_connect (app, "handle-local-options",
422                     G_CALLBACK (on_local_options), NULL);
423   g_signal_connect (app, "open", G_CALLBACK (on_open), NULL);
424
425   {
426     GSimpleAction *act_new_syntax = g_simple_action_new ("new-syntax", NULL);
427     g_signal_connect_swapped (act_new_syntax, "activate",
428                               G_CALLBACK (create_syntax_window), NULL);
429     g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_syntax));
430   }
431
432   {
433     GSimpleAction *act_new_data = g_simple_action_new ("new-data", NULL);
434     g_signal_connect_swapped (act_new_data, "activate",
435                               G_CALLBACK (create_data_window), NULL);
436     g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_data));
437   }
438
439   g_object_set (G_OBJECT (app), "register-session", TRUE, NULL);
440   return g_application_run (G_APPLICATION (app), argc, argv);
441 }