app.c 9.51 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
Elliot Lee's avatar
Elliot Lee committed
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
Elliot Lee's avatar
Elliot Lee committed
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17

18
#include "config.h"
19

Elliot Lee's avatar
Elliot Lee committed
20 21
#include <stdio.h>
#include <string.h>
22
#include <stdlib.h>
23
#include <locale.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 <gio/gio.h>
34
#include <gegl.h>
35

36 37 38 39 40
#ifdef G_OS_WIN32
#include <windows.h>
#include <winnls.h>
#endif

41
#include "libgimpbase/gimpbase.h"
42
#include "libgimpconfig/gimpconfig.h"
43

44
#include "core/core-types.h"
45

46 47
#include "config/gimprc.h"

Michael Natterer's avatar
Michael Natterer committed
48 49
#include "gegl/gimp-gegl.h"

50
#include "core/gimp.h"
Sven Neumann's avatar
Sven Neumann committed
51
#include "core/gimp-user-install.h"
Michael Natterer's avatar
Michael Natterer committed
52

Michael Natterer's avatar
Michael Natterer committed
53 54
#include "file/file-open.h"

55
#ifndef GIMP_CONSOLE_COMPILATION
56 57
#include "dialogs/user-install-dialog.h"

58
#include "gui/gui.h"
59
#endif
60

Sven Neumann's avatar
Sven Neumann committed
61
#include "app.h"
Elliot Lee's avatar
Elliot Lee committed
62
#include "batch.h"
63
#include "errors.h"
64
#include "units.h"
65
#include "language.h"
66
#include "gimp-debug.h"
Elliot Lee's avatar
Elliot Lee committed
67

68
#include "gimp-intl.h"
69

70

Michael Natterer's avatar
Michael Natterer committed
71
/*  local prototypes  */
Elliot Lee's avatar
Elliot Lee committed
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86
static void       app_init_update_noop       (const gchar        *text1,
                                              const gchar        *text2,
                                              gdouble             percentage);
static void       app_restore_after_callback (Gimp               *gimp,
                                              GimpInitStatusFunc  status_callback);
static gboolean   app_exit_after_callback    (Gimp               *gimp,
                                              gboolean            kill_it,
                                              GMainLoop         **loop);


/*  local variables  */

static GObject *initial_screen  = NULL;
static gint     initial_monitor = 0;
Elliot Lee's avatar
Elliot Lee committed
87

88

Michael Natterer's avatar
Michael Natterer committed
89
/*  public functions  */
90

91 92 93
void
app_libs_init (GOptionContext *context,
               gboolean        no_interface)
94
{
95 96 97 98 99 100
  /* disable OpenCL before GEGL is even initialized; this way we only
   * enable if wanted in gimprc, instead of always enabling, and then
   * disabling again if wanted in gimprc
   */
  g_object_set (gegl_config (),
                "use-opencl", FALSE,
101
                "application-license", "GPL3",
102 103
                NULL);

104 105
  g_option_context_add_group (context, gegl_get_option_group ());

106
#ifndef GIMP_CONSOLE_COMPILATION
Sven Neumann's avatar
Sven Neumann committed
107
  if (! no_interface)
108
    {
109
      gui_libs_init (context);
110 111
    }
#endif
112 113
}

114
void
115 116
app_abort (gboolean     no_interface,
           const gchar *abort_message)
117
{
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
#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);
138 139
}

140
void
141
app_run (const gchar         *full_prog_name,
142
         const gchar        **filenames,
143 144
         GFile               *alternate_system_gimprc,
         GFile               *alternate_gimprc,
145
         const gchar         *session_name,
146 147
         const gchar         *batch_interpreter,
         const gchar        **batch_commands,
148
         gboolean             as_new,
149 150 151 152 153 154 155 156
         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,
157
         gboolean             use_debug_handler,
158 159
         GimpStackTraceMode   stack_trace_mode,
         GimpPDBCompatMode    pdb_compat_mode)
Elliot Lee's avatar
Elliot Lee committed
160
{
161 162 163
  GimpInitStatusFunc  update_status_func = NULL;
  Gimp               *gimp;
  GMainLoop          *loop;
164
  GMainLoop          *run_loop;
165
  GFile              *default_folder = NULL;
166 167 168 169 170 171

  if (filenames && filenames[0] && ! filenames[1] &&
      g_file_test (filenames[0], G_FILE_TEST_IS_DIR))
    {
      if (g_path_is_absolute (filenames[0]))
        {
172
          default_folder = g_file_new_for_path (filenames[0]);
173 174 175 176 177 178 179
        }
      else
        {
          gchar *absolute = g_build_path (G_DIR_SEPARATOR_S,
                                          g_get_current_dir (),
                                          filenames[0],
                                          NULL);
180
          default_folder = g_file_new_for_path (absolute);
181 182 183 184 185
          g_free (absolute);
        }

      filenames = NULL;
    }
186

187 188 189
  /*  Create an instance of the "Gimp" object which is the root of the
   *  core object system
   */
190 191
  gimp = gimp_new (full_prog_name,
                   session_name,
192
                   default_folder,
193 194 195 196 197
                   be_verbose,
                   no_data,
                   no_fonts,
                   no_interface,
                   use_shm,
198
                   use_cpu_accel,
199
                   console_messages,
200 201
                   stack_trace_mode,
                   pdb_compat_mode);
202

203 204 205
  if (default_folder)
    g_object_unref (default_folder);

206 207
  gimp_cpu_accel_set_use (use_cpu_accel);

208
  errors_init (gimp, full_prog_name, use_debug_handler, stack_trace_mode);
209

210
  units_init (gimp);
211

212
  /*  Check if the user's gimp_directory exists
213
   */
214
  if (! g_file_test (gimp_directory (), G_FILE_TEST_IS_DIR))
215
    {
Sven Neumann's avatar
Sven Neumann committed
216
      GimpUserInstall *install = gimp_user_install_new (be_verbose);
217

Sven Neumann's avatar
Sven Neumann committed
218
#ifdef GIMP_CONSOLE_COMPILATION
219
      gimp_user_install_run (install);
Sven Neumann's avatar
Sven Neumann committed
220
#else
221 222 223 224
      if (! (no_interface ?
	     gimp_user_install_run (install) :
	     user_install_dialog_run (install)))
	exit (EXIT_FAILURE);
225
#endif
Sven Neumann's avatar
Sven Neumann committed
226 227

      gimp_user_install_free (install);
228
    }
Manish Singh's avatar
Manish Singh committed
229

230
  gimp_load_config (gimp, alternate_system_gimprc, alternate_gimprc);
231

232
  /*  change the locale if a language if specified  */
233
  language_init (gimp->config->language);
234

235
  /*  initialize lowlevel stuff  */
236
  gimp_gegl_init (gimp);
Michael Natterer's avatar
Michael Natterer committed
237

238 239 240 241 242 243 244 245
  /*  Connect our restore_after callback before gui_init() connects
   *  theirs, so ours runs first and can grab the initial monitor
   *  before the GUI's restore_after callback resets it.
   */
  g_signal_connect_after (gimp, "restore",
                          G_CALLBACK (app_restore_after_callback),
                          NULL);

246
#ifndef GIMP_CONSOLE_COMPILATION
Michael Natterer's avatar
Michael Natterer committed
247
  if (! no_interface)
248
    update_status_func = gui_init (gimp, no_splash);
249
#endif
250

251
  if (! update_status_func)
252
    update_status_func = app_init_update_noop;
253

254 255
  /*  Create all members of the global Gimp instance which need an already
   *  parsed gimprc, e.g. the data factories
256
   */
257
  gimp_initialize (gimp, update_status_func);
258

259 260
  /*  Load all data files
   */
261
  gimp_restore (gimp, update_status_func);
262

263 264 265
  /*  enable autosave late so we don't autosave when the
   *  monitor resolution is set in gui_init()
   */
266
  gimp_rc_set_autosave (GIMP_RC (gimp->edit_config), TRUE);
267

268 269 270 271 272 273
  loop = run_loop = g_main_loop_new (NULL, FALSE);

  g_signal_connect_after (gimp, "exit",
                          G_CALLBACK (app_exit_after_callback),
                          &run_loop);

274
  /*  Load the images given on the command-line.
275
   */
276
  if (filenames)
277 278 279 280
    {
      gint i;

      for (i = 0; filenames[i] != NULL; i++)
281 282
        {
          if (run_loop)
283 284 285 286 287 288 289 290 291
            {
              GFile *file = g_file_new_for_commandline_arg (filenames[i]);

              file_open_from_command_line (gimp, file, as_new,
                                           initial_screen,
                                           initial_monitor);

              g_object_unref (file);
            }
292
        }
293
    }
294

295 296
  if (run_loop)
    batch_run (gimp, batch_interpreter, batch_commands);
297

298 299 300 301 302 303
  if (run_loop)
    {
      gimp_threads_leave (gimp);
      g_main_loop_run (loop);
      gimp_threads_enter (gimp);
    }
304

305
  g_main_loop_unref (loop);
306

307
  g_object_unref (gimp);
Sven Neumann's avatar
Sven Neumann committed
308

309
  gimp_debug_instances ();
310

311
  errors_exit ();
Sven Neumann's avatar
Sven Neumann committed
312
  gegl_exit ();
Elliot Lee's avatar
Elliot Lee committed
313 314
}

315

Michael Natterer's avatar
Michael Natterer committed
316
/*  private functions  */
317

318
static void
319
app_init_update_noop (const gchar *text1,
320 321 322
                      const gchar *text2,
                      gdouble      percentage)
{
323 324 325
  /*  deliberately do nothing  */
}

326 327 328 329 330 331 332 333 334 335 336 337
static void
app_restore_after_callback (Gimp               *gimp,
                            GimpInitStatusFunc  status_callback)
{
  /*  Getting the display name for a -1 display returns the initial
   *  monitor during startup. Need to call this from a restore_after
   *  callback, because before restore(), the GUI can't return anything,
   *  after after restore() the initial monitor gets reset.
   */
  g_free (gimp_get_display_name (gimp, -1, &initial_screen, &initial_monitor));
}

338
static gboolean
339 340 341
app_exit_after_callback (Gimp       *gimp,
                         gboolean    kill_it,
                         GMainLoop **loop)
Michael Natterer's avatar
Michael Natterer committed
342
{
343
  if (gimp->be_verbose)
344
    g_print ("EXIT: %s\n", G_STRFUNC);
Elliot Lee's avatar
Elliot Lee committed
345

346 347 348 349 350 351 352 353 354 355
  /*
   *  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
Sven Neumann's avatar
Sven Neumann committed
356

357 358 359 360
  if (g_main_loop_is_running (*loop))
    g_main_loop_quit (*loop);

  *loop = NULL;
Sven Neumann's avatar
Sven Neumann committed
361

362
#else
Sven Neumann's avatar
Sven Neumann committed
363

Michael Natterer's avatar
Michael Natterer committed
364 365
  gegl_exit ();

366
  exit (EXIT_SUCCESS);
Sven Neumann's avatar
Sven Neumann committed
367

368 369
#endif

370
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
371
}