main.c 23.3 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
 */
Sven Neumann's avatar
Sven Neumann committed
17

Elliot Lee's avatar
Elliot Lee committed
18
#include "config.h"
Tor Lillqvist's avatar
Tor Lillqvist committed
19

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

24
#ifdef HAVE_UNISTD_H
Elliot Lee's avatar
Elliot Lee committed
25
#include <unistd.h>
26 27
#endif

28 29 30 31
#ifdef __GLIBC__
#include <malloc.h>
#endif

32 33
#include <locale.h>

34
#include <gio/gio.h>
Sven Neumann's avatar
Sven Neumann committed
35

36 37
#ifdef G_OS_WIN32
#include <io.h> /* get_osfhandle */
38 39 40 41

#ifdef HAVE_EXCHNDL
#include <time.h>
#include <exchndl.h>
42 43
#endif

44 45
#endif /* G_OS_WIN32 */

46 47
#ifndef GIMP_CONSOLE_COMPILATION
#include <gdk/gdk.h>
48 49
#else
#include <gdk-pixbuf/gdk-pixbuf.h>
50 51
#endif

52 53
#include <babl/babl.h>

54
#include "libgimpbase/gimpbase.h"
Manish Singh's avatar
Manish Singh committed
55

56
#include "pdb/pdb-types.h"
57

58 59 60
#include "config/gimpconfig-dump.h"

#include "core/gimp.h"
Sven Neumann's avatar
Sven Neumann committed
61

62 63 64 65
#include "pdb/gimppdb.h"
#include "pdb/gimpprocedure.h"
#include "pdb/internal-procs.h"

66
#include "about.h"
Sven Neumann's avatar
Sven Neumann committed
67
#include "app.h"
68
#include "sanity.h"
69
#include "signals.h"
70
#include "unique.h"
71
#include "version.h"
Elliot Lee's avatar
Elliot Lee committed
72

Sven Neumann's avatar
Sven Neumann committed
73
#ifdef G_OS_WIN32
74 75 76
/* To get PROCESS_DEP_* defined we need _WIN32_WINNT at 0x0601. We still
 * use the API optionally only if present, though.
 */
77 78 79
#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif
80
#define _WIN32_WINNT 0x0601
Sven Neumann's avatar
Sven Neumann committed
81
#include <windows.h>
82
#include <conio.h>
Sven Neumann's avatar
Sven Neumann committed
83 84
#endif

85
#include "gimp-log.h"
86
#include "gimp-intl.h"
87

88

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
static gboolean  gimp_option_fatal_warnings   (const gchar  *option_name,
                                               const gchar  *value,
                                               gpointer      data,
                                               GError      **error);
static gboolean  gimp_option_stack_trace_mode (const gchar  *option_name,
                                               const gchar  *value,
                                               gpointer      data,
                                               GError      **error);
static gboolean  gimp_option_pdb_compat_mode  (const gchar  *option_name,
                                               const gchar  *value,
                                               gpointer      data,
                                               GError      **error);
static gboolean  gimp_option_dump_gimprc      (const gchar  *option_name,
                                               const gchar  *value,
                                               gpointer      data,
                                               GError      **error);
105 106 107 108 109
static gboolean  gimp_option_dump_pdb_procedures_deprecated
                                              (const gchar  *option_name,
                                               const gchar  *value,
                                               gpointer      data,
                                               GError      **error);
110

111 112
static void      gimp_show_version_and_exit   (void) G_GNUC_NORETURN;
static void      gimp_show_license_and_exit   (void) G_GNUC_NORETURN;
113 114 115

static void      gimp_init_i18n               (void);
static void      gimp_init_malloc             (void);
116

117 118 119 120 121
#if defined (G_OS_WIN32) && !defined (GIMP_CONSOLE_COMPILATION)
static void      gimp_open_console_window     (void);
#else
#define gimp_open_console_window() /* as nothing */
#endif
Sven Neumann's avatar
Sven Neumann committed
122

123 124 125 126 127 128
static const gchar        *system_gimprc     = NULL;
static const gchar        *user_gimprc       = NULL;
static const gchar        *session_name      = NULL;
static const gchar        *batch_interpreter = NULL;
static const gchar       **batch_commands    = NULL;
static const gchar       **filenames         = NULL;
129
static gboolean            as_new            = FALSE;
130 131 132 133 134
static gboolean            no_interface      = FALSE;
static gboolean            no_data           = FALSE;
static gboolean            no_fonts          = FALSE;
static gboolean            no_splash         = FALSE;
static gboolean            be_verbose        = FALSE;
135
static gboolean            new_instance      = FALSE;
136
#if defined (USE_SYSV_SHM) || defined (USE_POSIX_SHM) || defined (G_OS_WIN32)
137
static gboolean            use_shm           = TRUE;
138
#else
139
static gboolean            use_shm           = FALSE;
140
#endif
141 142 143
static gboolean            use_cpu_accel     = TRUE;
static gboolean            console_messages  = FALSE;
static gboolean            use_debug_handler = FALSE;
Sven Neumann's avatar
Sven Neumann committed
144

Sven Neumann's avatar
Sven Neumann committed
145
#ifdef GIMP_UNSTABLE
146
static gboolean            show_playground   = TRUE;
147 148
static GimpStackTraceMode  stack_trace_mode  = GIMP_STACK_TRACE_QUERY;
static GimpPDBCompatMode   pdb_compat_mode   = GIMP_PDB_COMPAT_WARN;
Sven Neumann's avatar
Sven Neumann committed
149
#else
150
static gboolean            show_playground   = FALSE;
151 152
static GimpStackTraceMode  stack_trace_mode  = GIMP_STACK_TRACE_NEVER;
static GimpPDBCompatMode   pdb_compat_mode   = GIMP_PDB_COMPAT_ON;
153
#endif
Elliot Lee's avatar
Elliot Lee committed
154

Sven Neumann's avatar
Sven Neumann committed
155 156 157

static const GOptionEntry main_entries[] =
{
158
  { "version", 'v', G_OPTION_FLAG_NO_ARG,
159
    G_OPTION_ARG_CALLBACK, (GOptionArgFunc) gimp_show_version_and_exit,
Sven Neumann's avatar
Sven Neumann committed
160 161
    N_("Show version information and exit"), NULL
  },
162 163 164 165 166
  {
    "license", 0, G_OPTION_FLAG_NO_ARG,
    G_OPTION_ARG_CALLBACK, (GOptionArgFunc) gimp_show_license_and_exit,
    N_("Show license information and exit"), NULL
  },
Sven Neumann's avatar
Sven Neumann committed
167 168 169 170 171
  {
    "verbose", 0, 0,
    G_OPTION_ARG_NONE, &be_verbose,
    N_("Be more verbose"), NULL
  },
172 173 174 175 176
  {
    "new-instance", 'n', 0,
    G_OPTION_ARG_NONE, &new_instance,
    N_("Start a new GIMP instance"), NULL
  },
177 178 179 180 181
  {
    "as-new", 'a', 0,
    G_OPTION_ARG_NONE, &as_new,
    N_("Open images as new"), NULL
  },
Sven Neumann's avatar
Sven Neumann committed
182 183 184 185 186 187 188 189
  {
    "no-interface", 'i', 0,
    G_OPTION_ARG_NONE, &no_interface,
    N_("Run without a user interface"), NULL
  },
  {
    "no-data", 'd', 0,
    G_OPTION_ARG_NONE, &no_data,
Sven Neumann's avatar
Sven Neumann committed
190
    N_("Do not load brushes, gradients, patterns, ..."), NULL
Sven Neumann's avatar
Sven Neumann committed
191 192 193 194 195 196 197 198 199
  },
  {
    "no-fonts", 'f', 0,
    G_OPTION_ARG_NONE, &no_fonts,
    N_("Do not load any fonts"), NULL
  },
  {
    "no-splash", 's', 0,
    G_OPTION_ARG_NONE, &no_splash,
200
    N_("Do not show a splash screen"), NULL
Sven Neumann's avatar
Sven Neumann committed
201 202 203 204
  },
  {
    "no-shm", 0, G_OPTION_FLAG_REVERSE,
    G_OPTION_ARG_NONE, &use_shm,
205
    N_("Do not use shared memory between GIMP and plug-ins"), NULL
Sven Neumann's avatar
Sven Neumann committed
206 207 208 209 210 211 212 213 214 215 216 217
  },
  {
    "no-cpu-accel", 0, G_OPTION_FLAG_REVERSE,
    G_OPTION_ARG_NONE, &use_cpu_accel,
    N_("Do not use special CPU acceleration functions"), NULL
  },
  {
    "session", 0, 0,
    G_OPTION_ARG_FILENAME, &session_name,
    N_("Use an alternate sessionrc file"), "<name>"
  },
  {
218
    "gimprc", 'g', 0,
Sven Neumann's avatar
Sven Neumann committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    G_OPTION_ARG_FILENAME, &user_gimprc,
    N_("Use an alternate user gimprc file"), "<filename>"
  },
  {
    "system-gimprc", 0, 0,
    G_OPTION_ARG_FILENAME, &system_gimprc,
    N_("Use an alternate system gimprc file"), "<filename>"
  },
  {
    "batch", 'b', 0,
    G_OPTION_ARG_STRING_ARRAY, &batch_commands,
    N_("Batch command to run (can be used multiple times)"), "<command>"
  },
  {
    "batch-interpreter", 0, 0,
    G_OPTION_ARG_STRING, &batch_interpreter,
Sven Neumann's avatar
Sven Neumann committed
235
    N_("The procedure to process batch commands with"), "<proc>"
Sven Neumann's avatar
Sven Neumann committed
236 237
  },
  {
238
    "console-messages", 'c', 0,
Sven Neumann's avatar
Sven Neumann committed
239
    G_OPTION_ARG_NONE, &console_messages,
240
    N_("Send messages to console instead of using a dialog"), NULL
Sven Neumann's avatar
Sven Neumann committed
241 242 243 244
  },
  {
    "pdb-compat-mode", 0, 0,
    G_OPTION_ARG_CALLBACK, gimp_option_pdb_compat_mode,
Sven Neumann's avatar
Sven Neumann committed
245 246
    /*  don't translate the mode names (off|on|warn)  */
    N_("PDB compatibility mode (off|on|warn)"), "<mode>"
Sven Neumann's avatar
Sven Neumann committed
247 248 249 250
  },
  {
    "stack-trace-mode", 0, 0,
    G_OPTION_ARG_CALLBACK, gimp_option_stack_trace_mode,
Sven Neumann's avatar
Sven Neumann committed
251 252
    /*  don't translate the mode names (never|query|always)  */
    N_("Debug in case of a crash (never|query|always)"), "<mode>"
Sven Neumann's avatar
Sven Neumann committed
253 254
  },
  {
255
    "debug-handlers", 0, 0,
Sven Neumann's avatar
Sven Neumann committed
256 257 258 259
    G_OPTION_ARG_NONE, &use_debug_handler,
    N_("Enable non-fatal debugging signal handlers"), NULL
  },
  {
260
    "g-fatal-warnings", 0, G_OPTION_FLAG_NO_ARG,
261 262
    G_OPTION_ARG_CALLBACK, gimp_option_fatal_warnings,
    N_("Make all warnings fatal"), NULL
Sven Neumann's avatar
Sven Neumann committed
263 264
  },
  {
265
    "dump-gimprc", 0, G_OPTION_FLAG_NO_ARG,
Sven Neumann's avatar
Sven Neumann committed
266 267 268 269
    G_OPTION_ARG_CALLBACK, gimp_option_dump_gimprc,
    N_("Output a gimprc file with default settings"), NULL
  },
  {
270
    "dump-gimprc-system", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN,
Sven Neumann's avatar
Sven Neumann committed
271 272 273 274
    G_OPTION_ARG_CALLBACK, gimp_option_dump_gimprc,
    NULL, NULL
  },
  {
275
    "dump-gimprc-manpage", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN,
Sven Neumann's avatar
Sven Neumann committed
276 277 278
    G_OPTION_ARG_CALLBACK, gimp_option_dump_gimprc,
    NULL, NULL
  },
279 280 281 282
  {
    "dump-pdb-procedures-deprecated", 0,
    G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN,
    G_OPTION_ARG_CALLBACK, gimp_option_dump_pdb_procedures_deprecated,
283
    N_("Output a sorted list of deprecated procedures in the PDB"), NULL
284
  },
285 286 287 288 289
  {
    "show-playground", 0, G_OPTION_FLAG_HIDDEN,
    G_OPTION_ARG_NONE, &show_playground,
    N_("Show a preferences page with experimental features"), NULL
  },
Sven Neumann's avatar
Sven Neumann committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
  {
    G_OPTION_REMAINING, 0, 0,
    G_OPTION_ARG_FILENAME_ARRAY, &filenames,
    NULL, NULL
  },
  { NULL }
};

int
main (int    argc,
      char **argv)
{
  GOptionContext *context;
  GError         *error = NULL;
  const gchar    *abort_message;
305
  gchar          *basename;
306 307
  GFile          *system_gimprc_file = NULL;
  GFile          *user_gimprc_file   = NULL;
308
  gint            i;
309

310 311 312 313 314 315 316 317 318 319
#if defined (__GNUC__) && defined (_WIN64)
  /* mingw-w64, at least the unstable build from late July 2008,
   * starts subsystem:windows programs in main(), but passes them
   * bogus argc and argv. __argc and __argv are OK, though, so just
   * use them.
   */
  argc = __argc;
  argv = __argv;
#endif

320 321 322 323 324 325
#ifdef G_OS_WIN32
  /* Reduce risks */
  {
    typedef BOOL (WINAPI *t_SetDllDirectoryA) (LPCSTR lpPathName);
    t_SetDllDirectoryA p_SetDllDirectoryA;

326 327 328
    p_SetDllDirectoryA =
      (t_SetDllDirectoryA) GetProcAddress (GetModuleHandle ("kernel32.dll"),
                                           "SetDllDirectoryA");
329 330 331
    if (p_SetDllDirectoryA)
      (*p_SetDllDirectoryA) ("");
  }
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

  /* On Windows, set DLL search path to $INSTALLDIR/bin so that .exe
     plug-ins in the plug-ins directory can find libgimp and file
     library DLLs without needing to set external PATH. */
  {
    const gchar *install_dir;
    gchar       *bin_dir;
    LPWSTR       w_bin_dir;
    int          n;

    w_bin_dir = NULL;
    install_dir = gimp_installation_directory ();
    bin_dir = g_build_filename (install_dir, "bin", NULL);

    n = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS,
                             bin_dir, -1, NULL, 0);
    if (n == 0)
      goto out;

    w_bin_dir = g_malloc_n (n + 1, sizeof (wchar_t));
    n = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS,
                             bin_dir, -1,
                             w_bin_dir, (n + 1) * sizeof (wchar_t));
    if (n == 0)
      goto out;

    SetDllDirectoryW (w_bin_dir);

  out:
    if (w_bin_dir)
      g_free (w_bin_dir);
    g_free (bin_dir);
  }

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
#ifdef HAVE_EXCHNDL
  /* Use Dr. Mingw (dumps backtrace on crash) if it is available. */
  {
    time_t t;
    gchar *filename;
    gchar *dir;
    gchar *path;

    /* This has to be the non-roaming directory (i.e., the local
       directory) as backtraces correspond to the binaries on this
       system. */
    dir = g_build_filename (g_get_user_data_dir (),
                            GIMPDIR, GIMP_USER_VERSION, "CrashLog",
                            NULL);
    /* Ensure the path exists. */
    g_mkdir_with_parents (dir, 0700);

    time (&t);
    filename = g_strdup_printf ("%s-crash-%" G_GUINT64_FORMAT ".txt",
                                g_get_prgname(), t);
    path = g_build_filename (dir, filename, NULL);
    g_free (filename);
    g_free (dir);

    ExcHndlInit ();
    ExcHndlSetLogFileNameA (path);

    g_free (path);
  }
#endif

397 398 399 400 401
#ifndef _WIN64
  {
    typedef BOOL (WINAPI *t_SetProcessDEPPolicy) (DWORD dwFlags);
    t_SetProcessDEPPolicy p_SetProcessDEPPolicy;

402 403 404
    p_SetProcessDEPPolicy =
      (t_SetProcessDEPPolicy) GetProcAddress (GetModuleHandle ("kernel32.dll"),
                                              "SetProcessDEPPolicy");
405 406 407 408
    if (p_SetProcessDEPPolicy)
      (*p_SetProcessDEPPolicy) (PROCESS_DEP_ENABLE|PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION);
  }
#endif
409 410 411 412 413 414

  /* Group all our windows together on the taskbar */
  {
    typedef HRESULT (WINAPI *t_SetCurrentProcessExplicitAppUserModelID) (PCWSTR lpPathName);
    t_SetCurrentProcessExplicitAppUserModelID p_SetCurrentProcessExplicitAppUserModelID;

415 416 417
    p_SetCurrentProcessExplicitAppUserModelID =
      (t_SetCurrentProcessExplicitAppUserModelID) GetProcAddress (GetModuleHandle ("shell32.dll"),
                                                                  "SetCurrentProcessExplicitAppUserModelID");
418 419 420
    if (p_SetCurrentProcessExplicitAppUserModelID)
      (*p_SetCurrentProcessExplicitAppUserModelID) (L"gimp.GimpApplication");
  }
421 422
#endif

423 424 425 426
#ifdef GIMP_UNSTABLE
  gimp_open_console_window ();
#endif

Sven Neumann's avatar
Sven Neumann committed
427
  gimp_init_malloc ();
428

429 430
  gimp_env_init (FALSE);

431 432
  gimp_log_init ();

Sven Neumann's avatar
Sven Neumann committed
433
  gimp_init_i18n ();
434

435
  g_set_application_name (GIMP_NAME);
436

437 438 439 440 441 442
#ifdef G_OS_WIN32
  argv = g_win32_get_command_line ();
#else
  argv = g_strdupv (argv);
#endif

443 444 445 446
  basename = g_path_get_basename (argv[0]);
  g_set_prgname (basename);
  g_free (basename);

447 448 449 450 451 452 453 454 455 456 457 458 459 460
  /* Check argv[] for "--verbose" first */
  for (i = 1; i < argc; i++)
    {
      const gchar *arg = argv[i];

      if (arg[0] != '-')
        continue;

      if ((strcmp (arg, "--verbose") == 0) || (strcmp (arg, "-v") == 0))
        {
          be_verbose = TRUE;
        }
    }

461
  /* Check argv[] for "--no-interface" before trying to initialize gtk+. */
462 463 464 465 466 467 468 469
  for (i = 1; i < argc; i++)
    {
      const gchar *arg = argv[i];

      if (arg[0] != '-')
        continue;

      if ((strcmp (arg, "--no-interface") == 0) || (strcmp (arg, "-i") == 0))
470 471 472
        {
          no_interface = TRUE;
        }
473
      else if ((strcmp (arg, "--version") == 0) || (strcmp (arg, "-v") == 0))
474 475 476
        {
          gimp_show_version_and_exit ();
        }
477 478 479 480 481 482 483 484
#if defined (G_OS_WIN32) && !defined (GIMP_CONSOLE_COMPILATION)
      else if ((strcmp (arg, "--help") == 0) ||
               (strcmp (arg, "-?") == 0) ||
               (strncmp (arg, "--help-", 7) == 0))
        {
          gimp_open_console_window ();
        }
#endif
485
    }
486

487 488 489
#ifdef GIMP_CONSOLE_COMPILATION
  no_interface = TRUE;
#endif
490

491
  context = g_option_context_new (_("[FILE|URI...]"));
Sven Neumann's avatar
Sven Neumann committed
492 493
  g_option_context_set_summary (context, GIMP_NAME);

494
  g_option_context_add_main_entries (context, main_entries, GETTEXT_PACKAGE);
Elliot Lee's avatar
Elliot Lee committed
495

496
  app_libs_init (context, no_interface);
Sven Neumann's avatar
Sven Neumann committed
497

498
  if (! g_option_context_parse_strv (context, &argv, &error))
Elliot Lee's avatar
Elliot Lee committed
499
    {
500 501
      if (error)
        {
502
          gimp_open_console_window ();
503 504 505 506 507 508 509 510 511 512
          g_print ("%s\n", error->message);
          g_error_free (error);
        }
      else
        {
          g_print ("%s\n",
                   _("GIMP could not initialize the graphical user interface.\n"
                     "Make sure a proper setup for your display environment "
                     "exists."));
        }
513

Sven Neumann's avatar
Sven Neumann committed
514
      app_exit (EXIT_FAILURE);
Elliot Lee's avatar
Elliot Lee committed
515 516
    }

517 518 519
  if (no_interface || be_verbose || console_messages || batch_commands != NULL)
    gimp_open_console_window ();

520 521 522
  if (no_interface)
    new_instance = TRUE;

523 524
#ifndef GIMP_CONSOLE_COMPILATION
  if (! new_instance && gimp_unique_open (filenames, as_new))
525
    {
526
      if (be_verbose)
Jehan's avatar
Jehan committed
527 528
        g_print ("%s\n",
                 _("Another GIMP instance is already running."));
529

530 531 532
      if (batch_commands)
        gimp_unique_batch_run (batch_interpreter, batch_commands);

533 534 535
      gdk_notify_startup_complete ();

      return EXIT_SUCCESS;
536
    }
537
#endif
538

539
  abort_message = sanity_check_early ();
540 541
  if (abort_message)
    app_abort (no_interface, abort_message);
Sven Neumann's avatar
Sven Neumann committed
542

543
  gimp_init_signal_handlers (stack_trace_mode);
Sven Neumann's avatar
Sven Neumann committed
544

545 546 547 548 549 550
  if (system_gimprc)
    system_gimprc_file = g_file_new_for_commandline_arg (system_gimprc);

  if (user_gimprc)
    user_gimprc_file = g_file_new_for_commandline_arg (user_gimprc);

Sven Neumann's avatar
Sven Neumann committed
551 552
  app_run (argv[0],
           filenames,
553 554
           system_gimprc_file,
           user_gimprc_file,
Sven Neumann's avatar
Sven Neumann committed
555 556 557
           session_name,
           batch_interpreter,
           batch_commands,
558
           as_new,
Sven Neumann's avatar
Sven Neumann committed
559 560 561 562 563 564 565 566
           no_interface,
           no_data,
           no_fonts,
           no_splash,
           be_verbose,
           use_shm,
           use_cpu_accel,
           console_messages,
567
           use_debug_handler,
568
           show_playground,
Sven Neumann's avatar
Sven Neumann committed
569 570 571
           stack_trace_mode,
           pdb_compat_mode);

572 573 574 575 576 577
  if (system_gimprc_file)
    g_object_unref (system_gimprc_file);

  if (user_gimprc_file)
    g_object_unref (user_gimprc_file);

578 579
  g_strfreev (argv);

Sven Neumann's avatar
Sven Neumann committed
580 581 582 583 584 585
  g_option_context_free (context);

  return EXIT_SUCCESS;
}


586
#ifdef G_OS_WIN32
Sven Neumann's avatar
Sven Neumann committed
587

588 589 590 591 592
/* Provide WinMain in case we build GIMP as a subsystem:windows
 * application. Well, we do. When built with mingw, though, user code
 * execution still starts in main() in that case. So WinMain() gets
 * used on MSVC builds only.
 */
593

594 595 596 597
#ifdef __GNUC__
#  ifndef _stdcall
#    define _stdcall  __attribute__((stdcall))
#  endif
Sven Neumann's avatar
Sven Neumann committed
598 599
#endif

600 601
int _stdcall
WinMain (struct HINSTANCE__ *hInstance,
602 603 604
         struct HINSTANCE__ *hPrevInstance,
         char               *lpszCmdLine,
         int                 nCmdShow)
Sven Neumann's avatar
Sven Neumann committed
605
{
606 607
  return main (__argc, __argv);
}
608

609 610 611 612 613 614 615
#ifndef GIMP_CONSOLE_COMPILATION

static void
wait_console_window (void)
{
  FILE *console = fopen ("CONOUT$", "w");

616
  SetConsoleTitleW (g_utf8_to_utf16 (_("GIMP output. Type any character to close this window."), -1, NULL, NULL, NULL));
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
  fprintf (console, _("(Type any character to close this window)\n"));
  fflush (console);
  _getch ();
}

static void
gimp_open_console_window (void)
{
  if (((HANDLE) _get_osfhandle (fileno (stdout)) == INVALID_HANDLE_VALUE ||
       (HANDLE) _get_osfhandle (fileno (stderr)) == INVALID_HANDLE_VALUE) && AllocConsole ())
    {
      if ((HANDLE) _get_osfhandle (fileno (stdout)) == INVALID_HANDLE_VALUE)
        freopen ("CONOUT$", "w", stdout);

      if ((HANDLE) _get_osfhandle (fileno (stderr)) == INVALID_HANDLE_VALUE)
        freopen ("CONOUT$", "w", stderr);

634
      SetConsoleTitleW (g_utf8_to_utf16 (_("GIMP output. You can minimize this window, but don't close it."), -1, NULL, NULL, NULL));
635 636 637 638 639 640

      atexit (wait_console_window);
    }
}
#endif

641
#endif /* G_OS_WIN32 */
642

643

644 645 646 647 648 649 650
static gboolean
gimp_option_fatal_warnings (const gchar  *option_name,
                            const gchar  *value,
                            gpointer      data,
                            GError      **error)
{
  GLogLevelFlags fatal_mask;
Elliot Lee's avatar
Elliot Lee committed
651

652 653
  fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
  fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
654

655
  g_log_set_always_fatal (fatal_mask);
656

657
  return TRUE;
Elliot Lee's avatar
Elliot Lee committed
658 659
}

Sven Neumann's avatar
Sven Neumann committed
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
static gboolean
gimp_option_stack_trace_mode (const gchar  *option_name,
                              const gchar  *value,
                              gpointer      data,
                              GError      **error)
{
  if (strcmp (value, "never") == 0)
    stack_trace_mode = GIMP_STACK_TRACE_NEVER;
  else if (strcmp (value, "query") == 0)
    stack_trace_mode = GIMP_STACK_TRACE_QUERY;
  else if (strcmp (value, "always") == 0)
    stack_trace_mode = GIMP_STACK_TRACE_ALWAYS;
  else
    return FALSE;

  return TRUE;
}

static gboolean
gimp_option_pdb_compat_mode (const gchar  *option_name,
                             const gchar  *value,
                             gpointer      data,
                             GError      **error)
{
  if (! strcmp (value, "off"))
    pdb_compat_mode = GIMP_PDB_COMPAT_OFF;
  else if (! strcmp (value, "on"))
    pdb_compat_mode = GIMP_PDB_COMPAT_ON;
  else if (! strcmp (value, "warn"))
    pdb_compat_mode = GIMP_PDB_COMPAT_WARN;
  else
    return FALSE;

  return TRUE;
}
695

696 697 698 699 700
static gboolean
gimp_option_dump_gimprc (const gchar  *option_name,
                         const gchar  *value,
                         gpointer      data,
                         GError      **error)
701
{
702 703
  GimpConfigDumpFormat format = GIMP_CONFIG_DUMP_NONE;

704 705
  gimp_open_console_window ();

706 707 708 709 710 711 712 713 714 715 716 717
  if (strcmp (option_name, "--dump-gimprc") == 0)
    format = GIMP_CONFIG_DUMP_GIMPRC;
  if (strcmp (option_name, "--dump-gimprc-system") == 0)
    format = GIMP_CONFIG_DUMP_GIMPRC_SYSTEM;
  else if (strcmp (option_name, "--dump-gimprc-manpage") == 0)
    format = GIMP_CONFIG_DUMP_GIMPRC_MANPAGE;

  if (format)
    {
      Gimp     *gimp;
      gboolean  success;

718
      babl_init ();
719
      gimp = g_object_new (GIMP_TYPE_GIMP, NULL);
720
      gimp_load_config (gimp, NULL, NULL);
721

722
      success = gimp_config_dump (G_OBJECT (gimp), format);
723 724 725 726 727 728 729

      g_object_unref (gimp);

      app_exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
    }

  return FALSE;
730 731
}

732 733 734 735 736 737 738 739 740 741
static gboolean
gimp_option_dump_pdb_procedures_deprecated (const gchar  *option_name,
                                            const gchar  *value,
                                            gpointer      data,
                                            GError      **error)
{
  Gimp  *gimp;
  GList *deprecated_procs;
  GList *iter;

742
  babl_init ();
743
  gimp = g_object_new (GIMP_TYPE_GIMP, NULL);
744
  gimp_load_config (gimp, NULL, NULL);
745

746 747
  /* Make sure to turn on compatibility mode so deprecated procedures
   * are included
748 749 750
   */
  gimp->pdb_compat_mode = GIMP_PDB_COMPAT_ON;

751
  /* Initialize the list of procedures */
752 753
  internal_procs_init (gimp->pdb);

754
  /* Get deprecated procedures */
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
  deprecated_procs = gimp_pdb_get_deprecated_procedures (gimp->pdb);

  for (iter = deprecated_procs; iter; iter = g_list_next (iter))
    {
      GimpProcedure *procedure = GIMP_PROCEDURE (iter->data);

      g_print ("%s\n", procedure->original_name);
    }

  g_list_free (deprecated_procs);

  g_object_unref (gimp);

  app_exit (EXIT_SUCCESS);

  return FALSE;
}

773 774 775
static void
gimp_show_version_and_exit (void)
{
776 777
  gimp_open_console_window ();
  gimp_version_show (be_verbose);
778 779

  app_exit (EXIT_SUCCESS);
780
}
781

782 783 784
static void
gimp_show_license_and_exit (void)
{
785 786
  gimp_open_console_window ();
  gimp_version_show (be_verbose);
787 788 789 790 791 792 793

  g_print ("\n");
  g_print (GIMP_LICENSE);
  g_print ("\n\n");

  app_exit (EXIT_SUCCESS);
}
794

795 796 797
static void
gimp_init_malloc (void)
{
798
#ifdef GIMP_GLIB_MEM_PROFILER
799 800 801
  g_mem_set_vtable (glib_mem_profiler_table);
  g_atexit (g_mem_profile);
#endif
802

803 804
#ifdef __GLIBC__
  /* Tweak memory allocation so that memory allocated in chunks >= 4k
805 806 807 808 809 810 811 812 813
   * (64x64 pixel 1bpp tile) gets returned to the system when free()'d.
   *
   * The default value for M_MMAP_THRESHOLD in glibc-2.3 is 128k.
   * This is said to be an empirically derived value that works well
   * in most systems. Lowering it to 4k is thus probably not the ideal
   * solution.
   *
   * An alternative to tuning this parameter would be to use
   * malloc_trim(), for example after releasing a large tile-manager.
814
   */
815
#if 0
816
  mallopt (M_MMAP_THRESHOLD, TILE_WIDTH * TILE_HEIGHT);
Tor Lillqvist's avatar
Tor Lillqvist committed
817
#endif
818
#endif
819
}
Tor Lillqvist's avatar
Tor Lillqvist committed
820

821 822
static void
gimp_init_i18n (void)
823
{
824 825 826 827 828 829
  /*  We may change the locale later if the user specifies a language
   *  in the gimprc file. Here we are just initializing the locale
   *  according to the environment variables and set up the paths to
   *  the message catalogs.
   */

830 831 832 833 834 835 836 837 838 839 840 841 842
  setlocale (LC_ALL, "");

  bindtextdomain (GETTEXT_PACKAGE"-libgimp", gimp_locale_directory ());
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
  bind_textdomain_codeset (GETTEXT_PACKAGE"-libgimp", "UTF-8");
#endif

  bindtextdomain (GETTEXT_PACKAGE, gimp_locale_directory ());
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
#endif

  textdomain (GETTEXT_PACKAGE);
843
}