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