app.c 7.21 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
18

19
#include "config.h"
20

Elliot Lee's avatar
Elliot Lee committed
21 22
#include <stdio.h>
#include <string.h>
23
#include <stdlib.h>
24

25
#ifdef HAVE_SYS_PARAM_H
Elliot Lee's avatar
Elliot Lee committed
26
#include <sys/param.h>
27
#endif
28

29 30 31
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
Elliot Lee's avatar
Elliot Lee committed
32

33
#include <glib-object.h>
34

35
#include "libgimpbase/gimpbase.h"
36
#include "libgimpconfig/gimpconfig.h"
37

38
#include "core/core-types.h"
39

40 41
#include "config/gimprc.h"

42 43
#include "base/base.h"

44
#include "core/gimp.h"
Sven Neumann's avatar
Sven Neumann committed
45
#include "core/gimp-user-install.h"
Michael Natterer's avatar
Michael Natterer committed
46

Michael Natterer's avatar
Michael Natterer committed
47 48
#include "file/file-open.h"

49
#ifndef GIMP_CONSOLE_COMPILATION
50 51
#include "dialogs/user-install-dialog.h"

52
#include "gui/gui.h"
53
#endif
54

Sven Neumann's avatar
Sven Neumann committed
55
#include "app.h"
Elliot Lee's avatar
Elliot Lee committed
56
#include "batch.h"
57
#include "errors.h"
58
#include "units.h"
Elliot Lee's avatar
Elliot Lee committed
59

60
#include "gimp-intl.h"
61

Michael Natterer's avatar
Michael Natterer committed
62
/*  local prototypes  */
Elliot Lee's avatar
Elliot Lee committed
63

64 65 66 67
static void       app_init_update_none    (const gchar *text1,
                                           const gchar *text2,
                                           gdouble      percentage);
static gboolean   app_exit_after_callback (Gimp        *gimp,
68 69
                                           gboolean     kill_it,
                                           GMainLoop   *loop);
Elliot Lee's avatar
Elliot Lee committed
70

71

Michael Natterer's avatar
Michael Natterer committed
72
/*  public functions  */
73

74 75 76
void
app_libs_init (GOptionContext *context,
               gboolean        no_interface)
77
{
78
  if (no_interface)
79 80 81 82 83 84
    {
      g_type_init ();
    }
#ifndef GIMP_CONSOLE_COMPILATION
  else
    {
85
      gui_libs_init (context);
86 87
    }
#endif
88 89
}

90
void
91 92
app_abort (gboolean     no_interface,
           const gchar *abort_message)
93
{
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#ifndef GIMP_CONSOLE_COMPILATION
  if (no_interface)
#endif
    {
      g_print ("%s\n\n", abort_message);
    }
#ifndef GIMP_CONSOLE_COMPILATION
  else
    {
      gui_abort (abort_message);
    }
#endif

  app_exit (EXIT_FAILURE);
}

void
app_exit (gint status)
{
  exit (status);
114 115
}

116
void
117
app_run (const gchar         *full_prog_name,
118
         const gchar        **filenames,
119 120 121
         const gchar         *alternate_system_gimprc,
         const gchar         *alternate_gimprc,
         const gchar         *session_name,
122 123
         const gchar         *batch_interpreter,
         const gchar        **batch_commands,
124
         gboolean             as_new,
125 126 127 128 129 130 131 132
         gboolean             no_interface,
         gboolean             no_data,
         gboolean             no_fonts,
         gboolean             no_splash,
         gboolean             be_verbose,
         gboolean             use_shm,
         gboolean             use_cpu_accel,
         gboolean             console_messages,
133
         gboolean             use_debug_handler,
134 135
         GimpStackTraceMode   stack_trace_mode,
         GimpPDBCompatMode    pdb_compat_mode)
Elliot Lee's avatar
Elliot Lee committed
136
{
137 138
  GimpInitStatusFunc  update_status_func = NULL;
  Gimp               *gimp;
139
  GimpBaseConfig     *config;
140
  GMainLoop          *loop;
141
  gboolean            swap_is_ok;
142

143 144 145
  /*  Create an instance of the "Gimp" object which is the root of the
   *  core object system
   */
146 147 148 149 150 151 152 153
  gimp = gimp_new (full_prog_name,
                   session_name,
                   be_verbose,
                   no_data,
                   no_fonts,
                   no_interface,
                   use_shm,
                   console_messages,
154 155
                   stack_trace_mode,
                   pdb_compat_mode);
156

157
  errors_init (gimp, full_prog_name, use_debug_handler, stack_trace_mode);
158

159
  units_init (gimp);
160

161
  /*  Check if the user's gimp_directory exists
162
   */
163
  if (! g_file_test (gimp_directory (), G_FILE_TEST_IS_DIR))
164
    {
Sven Neumann's avatar
Sven Neumann committed
165
      GimpUserInstall *install = gimp_user_install_new (be_verbose);
166

Sven Neumann's avatar
Sven Neumann committed
167
#ifdef GIMP_CONSOLE_COMPILATION
168
      gimp_user_install_run (install);
Sven Neumann's avatar
Sven Neumann committed
169
#else
170 171 172 173
      if (! (no_interface ?
	     gimp_user_install_run (install) :
	     user_install_dialog_run (install)))
	exit (EXIT_FAILURE);
174
#endif
Sven Neumann's avatar
Sven Neumann committed
175 176

      gimp_user_install_free (install);
177
    }
Manish Singh's avatar
Manish Singh committed
178

179
  gimp_load_config (gimp, alternate_system_gimprc, alternate_gimprc);
180

181 182
  config = GIMP_BASE_CONFIG (gimp->config);

183
  /*  initialize lowlevel stuff  */
184
  swap_is_ok = base_init (config, be_verbose, use_cpu_accel);
185

186
#ifndef GIMP_CONSOLE_COMPILATION
Michael Natterer's avatar
Michael Natterer committed
187
  if (! no_interface)
188
    update_status_func = gui_init (gimp, no_splash);
189
#endif
190

191 192
  if (! update_status_func)
    update_status_func = app_init_update_none;
193

194 195
  /*  Create all members of the global Gimp instance which need an already
   *  parsed gimprc, e.g. the data factories
196
   */
197
  gimp_initialize (gimp, update_status_func);
198

199 200
  /*  Load all data files
   */
201
  gimp_restore (gimp, update_status_func);
202

203 204
  /* display a warning when no test swap file could be generated */
  if (! swap_is_ok)
205 206 207 208 209 210 211 212 213 214
    {
      gchar *path = gimp_config_path_expand (config->swap_path, FALSE, NULL);

      g_message (_("Unable to open a test swap file.\n\n"
		   "To avoid data loss, please check the location "
		   "and permissions of the swap directory defined in "
		   "your Preferences (currently \"%s\")."), path);

      g_free (path);
    }
215

216 217 218
  /*  enable autosave late so we don't autosave when the
   *  monitor resolution is set in gui_init()
   */
219
  gimp_rc_set_autosave (GIMP_RC (gimp->edit_config), TRUE);
220

221
  /*  Load the images given on the command-line.
222
   */
223
  if (filenames)
224 225 226 227
    {
      gint i;

      for (i = 0; filenames[i] != NULL; i++)
228
        file_open_from_command_line (gimp, filenames[i], as_new);
229
    }
230

231
#ifndef GIMP_CONSOLE_COMPILATION
232 233
  if (! no_interface)
    gui_post_init (gimp);
234
#endif
235

236
  batch_run (gimp, batch_interpreter, batch_commands);
237

238
  loop = g_main_loop_new (NULL, FALSE);
239 240 241 242

  g_signal_connect_after (gimp, "exit",
                          G_CALLBACK (app_exit_after_callback),
                          loop);
243

244 245 246
  gimp_threads_leave (gimp);
  g_main_loop_run (loop);
  gimp_threads_enter (gimp);
247

248
  g_main_loop_unref (loop);
249

250
  g_object_unref (gimp);
251
  base_exit ();
Elliot Lee's avatar
Elliot Lee committed
252 253
}

254

Michael Natterer's avatar
Michael Natterer committed
255
/*  private functions  */
256

257 258 259 260 261
static void
app_init_update_none (const gchar *text1,
                      const gchar *text2,
                      gdouble      percentage)
{
Michael Natterer's avatar
Michael Natterer committed
262 263
}

264
static gboolean
265 266 267
app_exit_after_callback (Gimp      *gimp,
                         gboolean   kill_it,
                         GMainLoop *loop)
Michael Natterer's avatar
Michael Natterer committed
268
{
269 270
  if (gimp->be_verbose)
    g_print ("EXIT: app_exit_after_callback\n");
Elliot Lee's avatar
Elliot Lee committed
271

272 273 274 275 276 277 278 279 280 281
  /*
   *  In stable releases, we simply call exit() here. This speeds up
   *  the process of quitting GIMP and also works around the problem
   *  that plug-ins might still be running.
   *
   *  In unstable releases, we shut down GIMP properly in an attempt
   *  to catch possible problems in our finalizers.
   */

#ifdef GIMP_UNSTABLE
282
  g_main_loop_quit (loop);
283
#else
284 285
  /*  make sure that the swap file is removed before we quit */
  base_exit ();
286 287 288
  exit (EXIT_SUCCESS);
#endif

289
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
290
}