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
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.
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.
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/>. */
20 #include "ui/gui/psppire.h"
25 #if ENABLE_RELOCATABLE && defined(__APPLE__)
26 #include <sys/resource.h>
27 static const bool apple_relocatable = true;
29 static const bool apple_relocatable = false;
30 #if HAVE_SYS_RESOURCE_H
31 #include <sys/resource.h>
33 /* Dummy definitions to keep the compiler happy. */
39 #define RLIMIT_NOFILE 0
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"
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"
64 #define _(msgid) gettext (msgid)
65 #define N_(msgid) msgid
70 show_version_and_exit ()
72 version_etc (stdout, "psppire", PACKAGE_NAME, PACKAGE_VERSION,
73 "Ben Pfaff", "John Darrington", "Jason Stover", NULL_SENTINEL);
83 init_prepare (GSource * source, gint * timeout_)
89 init_check (GSource * source)
95 init_dispatch (GSource * ss, GSourceFunc callback, gpointer user_data)
97 struct init_source *is = (struct init_source *) ss;
99 bool finished = initialize (is);
104 g_main_loop_quit (is->loop);
111 static GSourceFuncs init_funcs =
112 { init_prepare, init_check, init_dispatch, NULL };
116 GtkWidget *wsplash = 0;
117 gint64 start_time = 0;
121 create_splash_window (void)
123 GtkWidget *sp = gtk_window_new (GTK_WINDOW_TOPLEVEL);
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));
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);
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);
147 gtk_window_set_gravity (GTK_WINDOW (sp), GDK_GRAVITY_CENTER);
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);
158 on_local_options (GApplication * application,
159 GVariantDict * options, gpointer user_data)
163 g_variant_dict_lookup_value (options, "no-unique",
164 G_VARIANT_TYPE_BOOLEAN);
167 GApplicationFlags flags = g_application_get_flags (application);
168 flags |= G_APPLICATION_NON_UNIQUE;
169 g_application_set_flags (application, flags);
175 g_variant_dict_lookup_value (options, "no-splash",
176 G_VARIANT_TYPE_BOOLEAN);
180 start_time = g_get_monotonic_time ();
189 on_startup (GApplication * app, gpointer ud)
191 GMainContext *context = g_main_context_new ();
195 wsplash = create_splash_window ();
196 gtk_application_add_window (GTK_APPLICATION (app),
197 GTK_WINDOW (wsplash));
200 GMainLoop *loop = g_main_loop_new (context, FALSE);
202 GSource *ss = g_source_new (&init_funcs, sizeof (struct init_source));
204 ((struct init_source *) ss)->loop = loop;
205 ((struct init_source *) ss)->state = 0;
207 g_source_set_priority (ss, G_PRIORITY_DEFAULT);
209 g_source_attach (ss, context);
210 g_main_loop_run (loop);
215 post_initialise (GApplication * app)
217 register_selection_functions ();
218 psppire_output_window_setup ();
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));
226 #define SPLASH_DURATION 1000
229 destroy_splash (gpointer ud)
231 GtkWidget *sp = GTK_WIDGET (ud);
232 gtk_widget_destroy (sp);
234 return G_SOURCE_REMOVE;
239 wait_for_splash (GApplication *app, GtkWindow *x)
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));
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);
253 g_timeout_add (SPLASH_DURATION - elapsed_time, destroy_splash, wsplash);
258 on_activate (GApplication * app, gpointer ud)
260 post_initialise (app);
262 GtkWindow *x = create_data_window ();
263 gtk_application_add_window (GTK_APPLICATION (app), x);
265 wait_for_splash (app, x);
269 find_empty_data_window (GApplication *app)
271 GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
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);
283 find_psppire_window (GApplication *app)
285 GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
288 if (wl->data && PSPPIRE_IS_WINDOW (GTK_WINDOW (wl->data)))
289 return GTK_WINDOW (wl->data);
296 on_open (GApplication *app, GFile **files, gint n_files, gchar * hint,
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);
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
308 GtkWindow *victim = find_empty_data_window (app);
310 gchar *file = g_file_get_parse_name (files[0]);
311 GtkWindow *x = psppire_preload_file (file, victim);
314 wait_for_splash (app, x);
318 /* These are arguments which must be processed BEFORE the X server has been initialised */
320 process_pre_start_arguments (int *argc, char ***argv)
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},
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);
337 pspp_macos_setenv (const char * progname)
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
343 gchar resolved_path[PATH_MAX];
344 /* on some OSX installations open file limit is 256 and GIMP needs more */
346 limit.rlim_cur = 10000;
347 limit.rlim_max = 10000;
348 setrlimit (RLIMIT_NOFILE, &limit);
349 if (realpath (progname, resolved_path))
353 gchar res_dir[PATH_MAX];
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");
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);
383 if (g_getenv ("HOME")!=NULL)
385 g_snprintf (tmp, sizeof(tmp),
386 "%s/Library/Application Support/pspp/1.3/cache",
388 g_setenv ("XDG_CACHE_HOME", tmp, TRUE);
394 main (int argc, char *argv[])
396 if (apple_relocatable)
398 /* remove MacOS session identifier from the command line args */
400 for (gint i = 0; i < argc; i++)
402 if (!g_str_has_prefix (argv[i], "-psn_"))
404 argv[newargc] = argv[i];
410 argv[newargc] = NULL; /* glib expects NULL terminated array */
413 pspp_macos_setenv (argv[0]);
416 set_program_name (argv[0]);
418 GtkApplication *app =
419 gtk_application_new ("gnu.pspp", G_APPLICATION_HANDLES_OPEN);
421 process_pre_start_arguments (&argc, &argv);
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},
431 g_application_add_main_option_entries (G_APPLICATION (app), oe);
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);
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));
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));
453 g_object_set (G_OBJECT (app), "register-session", TRUE, NULL);
454 return g_application_run (G_APPLICATION (app), argc, argv);