work on docs
[pspp] / src / ui / gui / pre-initialisation.h
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2020  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 #if !ENABLE_RELOCATABLE || !defined(__APPLE__)
20
21 static inline void
22 pre_initialisation (int *argc, char **argv)
23 {
24 }
25
26 #else
27
28 #include <sys/stat.h>
29 #include <glib.h>
30
31 static inline void
32 pre_initialisation (int *argc, char **argv)
33 {
34   /* remove MacOS session identifier from the command line args */
35   gint newargc = 0;
36   for (gint i = 0; i < *argc; i++)
37     {
38       if (!g_str_has_prefix (argv[i], "-psn_"))
39         {
40           argv[newargc] = argv[i];
41           newargc++;
42         }
43     }
44   if (*argc > newargc)
45     {
46       argv[newargc] = NULL; /* glib expects NULL terminated array */
47       *argc = newargc;
48     }
49
50   const char * progname = argv[0];
51
52   /* helper to set environment variables for pspp to be relocatable.
53    * Due to the latest changes it is not recommended to set it in the shell
54    * wrapper anymore.
55    */
56   gchar resolved_path[PATH_MAX];
57   /* on some OSX installations open file limit is 256 and GIMP needs more */
58   struct rlimit limit;
59   limit.rlim_cur = 10000;
60   limit.rlim_max = 10000;
61   setrlimit (RLIMIT_NOFILE, &limit);
62   if (realpath (progname, resolved_path))
63     {
64       gchar  tmp[PATH_MAX];
65       gchar *app_dir;
66       gchar  res_dir[PATH_MAX];
67       struct stat sb;
68
69       app_dir = g_path_get_dirname (resolved_path);
70       g_snprintf (tmp, sizeof(tmp), "%s/../../Resources", app_dir);
71       if (realpath (tmp, res_dir) && !stat (res_dir,&sb) && S_ISDIR (sb.st_mode))
72         g_print ("pspp is started as MacOS application\n");
73       else
74         return;
75       g_free (app_dir);
76
77       g_snprintf (tmp, sizeof(tmp), "%s/lib/gtk-3.0/3.0.0", res_dir);
78       g_setenv ("GTK_PATH", tmp, TRUE);
79       g_snprintf (tmp, sizeof(tmp), "%s/etc/gtk-3.0/gtk.immodules", res_dir);
80       g_setenv ("GTK_IM_MODULE_FILE", tmp, TRUE);
81       g_snprintf (tmp, sizeof(tmp), "%s/lib/gegl-0.4", res_dir);
82       g_setenv ("GEGL_PATH", tmp, TRUE);
83       g_snprintf (tmp, sizeof(tmp), "%s/lib/babl-0.1", res_dir);
84       g_setenv ("BABL_PATH", tmp, TRUE);
85       g_snprintf (tmp, sizeof(tmp), "%s/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache", res_dir);
86       g_setenv ("GDK_PIXBUF_MODULE_FILE", tmp, TRUE);
87       g_snprintf (tmp, sizeof(tmp), "%s/etc/fonts", res_dir);
88       g_setenv ("FONTCONFIG_PATH", tmp, TRUE);
89       g_snprintf (tmp, sizeof(tmp), "%s/lib/gio/modules", res_dir);
90       g_setenv ("GIO_MODULE_DIR", tmp, TRUE);
91       g_snprintf (tmp, sizeof(tmp), "%s/etc/xdg", res_dir);
92       g_setenv ("XDG_CONFIG_DIRS", tmp, TRUE);
93       g_snprintf (tmp, sizeof(tmp), "%s/share", res_dir);
94       g_setenv ("XDG_DATA_DIRS", tmp, TRUE);
95
96       if (g_getenv ("HOME")!=NULL)
97         {
98           g_snprintf (tmp, sizeof(tmp),
99                       "%s/Library/Application Support/pspp/1.3/cache",
100                       g_getenv("HOME"));
101           g_setenv ("XDG_CACHE_HOME", tmp, TRUE);
102         }
103     }
104 }
105 #endif