main.c: set "register-session" property for gtk_application
[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
242 static void
243 on_activate (GApplication * app, gpointer ud)
244 {
245   post_initialise (app);
246
247   GtkWindow *x = create_data_window ();
248   gtk_application_add_window (GTK_APPLICATION (app), x);
249
250   wait_for_splash (app, x);
251 }
252
253
254 static void
255 on_open (GApplication *app, GFile **files, gint n_files, gchar * hint,
256          gpointer ud)
257 {
258   post_initialise (app);
259
260   gchar *file = g_file_get_parse_name (files[0]);
261   GtkWindow *x = psppire_preload_file (file);
262   g_free (file);
263
264   wait_for_splash (app, x);
265 }
266
267
268 /* These are arguments which must be processed BEFORE the X server has been initialised */
269 static void
270 process_pre_start_arguments (int *argc, char ***argv)
271 {
272   GOptionEntry oe[] = {
273     {"version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
274      show_version_and_exit, N_("Show version information and exit"), 0},
275     {NULL}
276   };
277
278   GOptionContext *oc = g_option_context_new ("");
279   g_option_context_set_help_enabled (oc, FALSE);
280   g_option_context_set_ignore_unknown_options (oc, FALSE);
281   g_option_context_add_main_entries (oc, oe, NULL);
282   g_option_context_parse (oc, argc, argv, NULL);
283 }
284
285 #if ENABLE_RELOCATABLE && defined(__APPLE__)
286 static void
287 pspp_macos_setenv (const char * progname)
288 {
289   /* helper to set environment variables for pspp to be relocatable.
290    * Due to the latest changes it is not recommended to set it in the shell
291    * wrapper anymore.
292    */
293   gchar resolved_path[PATH_MAX];
294   /* on some OSX installations open file limit is 256 and GIMP needs more */
295   struct rlimit limit;
296   limit.rlim_cur = 10000;
297   limit.rlim_max = 10000;
298   setrlimit (RLIMIT_NOFILE, &limit);
299   if (realpath (progname, resolved_path))
300     {
301       gchar  tmp[PATH_MAX];
302       gchar *app_dir;
303       gchar  res_dir[PATH_MAX];
304       struct stat sb;
305
306       app_dir = g_path_get_dirname (resolved_path);
307       g_snprintf (tmp, sizeof(tmp), "%s/../../Resources", app_dir);
308       if (realpath (tmp, res_dir) && !stat (res_dir,&sb) && S_ISDIR (sb.st_mode))
309         g_print ("pspp is started as MacOS application\n");
310       else
311         return;
312       g_free (app_dir);
313
314       g_snprintf (tmp, sizeof(tmp), "%s/lib/gtk-3.0/3.0.0", res_dir);
315       g_setenv ("GTK_PATH", tmp, TRUE);
316       g_snprintf (tmp, sizeof(tmp), "%s/etc/gtk-3.0/gtk.immodules", res_dir);
317       g_setenv ("GTK_IM_MODULE_FILE", tmp, TRUE);
318       g_snprintf (tmp, sizeof(tmp), "%s/lib/gegl-0.4", res_dir);
319       g_setenv ("GEGL_PATH", tmp, TRUE);
320       g_snprintf (tmp, sizeof(tmp), "%s/lib/babl-0.1", res_dir);
321       g_setenv ("BABL_PATH", tmp, TRUE);
322       g_snprintf (tmp, sizeof(tmp), "%s/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache", res_dir);
323       g_setenv ("GDK_PIXBUF_MODULE_FILE", tmp, TRUE);
324       g_snprintf (tmp, sizeof(tmp), "%s/etc/fonts", res_dir);
325       g_setenv ("FONTCONFIG_PATH", tmp, TRUE);
326       g_snprintf (tmp, sizeof(tmp), "%s/lib/gio/modules", res_dir);
327       g_setenv ("GIO_MODULE_DIR", tmp, TRUE);
328       g_snprintf (tmp, sizeof(tmp), "%s/etc/xdg", res_dir);
329       g_setenv ("XDG_CONFIG_DIRS", tmp, TRUE);
330       g_snprintf (tmp, sizeof(tmp), "%s/share", res_dir);
331       g_setenv ("XDG_DATA_DIRS", tmp, TRUE);
332
333       if (g_getenv ("HOME")!=NULL)
334         {
335           g_snprintf (tmp, sizeof(tmp),
336                       "%s/Library/Application Support/pspp/1.3/cache",
337                       g_getenv("HOME"));
338           g_setenv ("XDG_CACHE_HOME", tmp, TRUE);
339         }
340     }
341 }
342 #endif
343
344 int
345 main (int argc, char *argv[])
346 {
347
348 #if ENABLE_RELOCATABLE && defined(__APPLE__)
349   /* remove MacOS session identifier from the command line args */
350   gint newargc = 0;
351   for (gint i = 0; i < argc; i++)
352     {
353       if (!g_str_has_prefix (argv[i], "-psn_"))
354         {
355           argv[newargc] = argv[i];
356           newargc++;
357         }
358     }
359   if (argc > newargc)
360     {
361       argv[newargc] = NULL; /* glib expects NULL terminated array */
362       argc = newargc;
363     }
364   pspp_macos_setenv (argv[0]);
365 #endif
366
367   set_program_name (argv[0]);
368
369   GtkApplication *app =
370     gtk_application_new ("gnu.pspp", G_APPLICATION_HANDLES_OPEN);
371
372   process_pre_start_arguments (&argc, &argv);
373
374   GOptionEntry oe[] = {
375     {"no-splash", 'q', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
376       N_("Do not display the splash screen"), 0},
377     {"no-unique", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
378       N_("Do not attempt single instance negotiation"), 0},
379     {NULL}
380   };
381
382   g_application_add_main_option_entries (G_APPLICATION (app), oe);
383
384   g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL);
385   g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
386   g_signal_connect (app, "handle-local-options",
387                     G_CALLBACK (on_local_options), NULL);
388   g_signal_connect (app, "open", G_CALLBACK (on_open), NULL);
389
390   {
391     GSimpleAction *act_new_syntax = g_simple_action_new ("new-syntax", NULL);
392     g_signal_connect_swapped (act_new_syntax, "activate",
393                               G_CALLBACK (create_syntax_window), NULL);
394     g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_syntax));
395   }
396
397   {
398     GSimpleAction *act_new_data = g_simple_action_new ("new-data", NULL);
399     g_signal_connect_swapped (act_new_data, "activate",
400                               G_CALLBACK (create_data_window), NULL);
401     g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_data));
402   }
403
404   g_object_set (G_OBJECT (app), "register-session", TRUE, NULL);
405   return g_application_run (G_APPLICATION (app), argc, argv);
406 }