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