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