1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
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 2 of the License, or
7
* (at your option) any later version.
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.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
#define _GNU_SOURCE /* for the sigaction stuff */
38
#include <glib-object.h>
41
#include <io.h> /* get_osfhandle */
44
#ifndef GIMP_CONSOLE_COMPILATION
48
#include "libgimpbase/gimpbase.h"
50
#include "core/core-types.h"
52
#include "base/tile.h"
54
#include "config/gimpconfig-dump.h"
56
#include "core/gimp.h"
67
/* To get PROCESS_DEP_* defined we need _WIN32_WINNT at 0x0601. We still
68
* use the API optionally only if present, though.
70
#define _WIN32_WINNT 0x0601
76
#include "gimp-intl.h"
79
static gboolean gimp_option_fatal_warnings (const gchar *option_name,
83
static gboolean gimp_option_stack_trace_mode (const gchar *option_name,
87
static gboolean gimp_option_pdb_compat_mode (const gchar *option_name,
91
static gboolean gimp_option_dump_gimprc (const gchar *option_name,
96
static void gimp_show_version_and_exit (void) G_GNUC_NORETURN;
97
static void gimp_show_license_and_exit (void) G_GNUC_NORETURN;
99
static void gimp_init_i18n (void);
100
static void gimp_init_malloc (void);
101
static void gimp_init_signal_handlers (void);
104
static void gimp_sigfatal_handler (gint sig_num) G_GNUC_NORETURN;
107
#if defined (G_OS_WIN32) && !defined (GIMP_CONSOLE_COMPILATION)
108
static void gimp_open_console_window (void);
110
#define gimp_open_console_window() /* as nothing */
113
static const gchar *system_gimprc = NULL;
114
static const gchar *user_gimprc = NULL;
115
static const gchar *session_name = NULL;
116
static const gchar *batch_interpreter = NULL;
117
static const gchar **batch_commands = NULL;
118
static const gchar **filenames = NULL;
119
static gboolean as_new = FALSE;
120
static gboolean no_interface = FALSE;
121
static gboolean no_data = FALSE;
122
static gboolean no_fonts = FALSE;
123
static gboolean no_splash = FALSE;
124
static gboolean be_verbose = FALSE;
125
static gboolean new_instance = FALSE;
126
#if defined (USE_SYSV_SHM) || defined (USE_POSIX_SHM) || defined (G_OS_WIN32)
127
static gboolean use_shm = TRUE;
129
static gboolean use_shm = FALSE;
131
static gboolean use_cpu_accel = TRUE;
132
static gboolean console_messages = FALSE;
133
static gboolean use_debug_handler = FALSE;
136
static GimpStackTraceMode stack_trace_mode = GIMP_STACK_TRACE_QUERY;
137
static GimpPDBCompatMode pdb_compat_mode = GIMP_PDB_COMPAT_WARN;
139
static GimpStackTraceMode stack_trace_mode = GIMP_STACK_TRACE_NEVER;
140
static GimpPDBCompatMode pdb_compat_mode = GIMP_PDB_COMPAT_ON;
144
static const GOptionEntry main_entries[] =
146
{ "version", 'v', G_OPTION_FLAG_NO_ARG,
147
G_OPTION_ARG_CALLBACK, (GOptionArgFunc) gimp_show_version_and_exit,
148
N_("Show version information and exit"), NULL
151
"license", 0, G_OPTION_FLAG_NO_ARG,
152
G_OPTION_ARG_CALLBACK, (GOptionArgFunc) gimp_show_license_and_exit,
153
N_("Show license information and exit"), NULL
157
G_OPTION_ARG_NONE, &be_verbose,
158
N_("Be more verbose"), NULL
161
"new-instance", 'n', 0,
162
G_OPTION_ARG_NONE, &new_instance,
163
N_("Start a new GIMP instance"), NULL
167
G_OPTION_ARG_NONE, &as_new,
168
N_("Open images as new"), NULL
171
"no-interface", 'i', 0,
172
G_OPTION_ARG_NONE, &no_interface,
173
N_("Run without a user interface"), NULL
177
G_OPTION_ARG_NONE, &no_data,
178
N_("Do not load brushes, gradients, patterns, ..."), NULL
182
G_OPTION_ARG_NONE, &no_fonts,
183
N_("Do not load any fonts"), NULL
187
G_OPTION_ARG_NONE, &no_splash,
188
N_("Do not show a startup window"), NULL
191
"no-shm", 0, G_OPTION_FLAG_REVERSE,
192
G_OPTION_ARG_NONE, &use_shm,
193
N_("Do not use shared memory between GIMP and plugins"), NULL
196
"no-cpu-accel", 0, G_OPTION_FLAG_REVERSE,
197
G_OPTION_ARG_NONE, &use_cpu_accel,
198
N_("Do not use special CPU acceleration functions"), NULL
202
G_OPTION_ARG_FILENAME, &session_name,
203
N_("Use an alternate sessionrc file"), "<name>"
207
G_OPTION_ARG_FILENAME, &user_gimprc,
208
N_("Use an alternate user gimprc file"), "<filename>"
211
"system-gimprc", 0, 0,
212
G_OPTION_ARG_FILENAME, &system_gimprc,
213
N_("Use an alternate system gimprc file"), "<filename>"
217
G_OPTION_ARG_STRING_ARRAY, &batch_commands,
218
N_("Batch command to run (can be used multiple times)"), "<command>"
221
"batch-interpreter", 0, 0,
222
G_OPTION_ARG_STRING, &batch_interpreter,
223
N_("The procedure to process batch commands with"), "<proc>"
226
"console-messages", 'c', 0,
227
G_OPTION_ARG_NONE, &console_messages,
228
N_("Send messages to console instead of using a dialog"), NULL
231
"pdb-compat-mode", 0, 0,
232
G_OPTION_ARG_CALLBACK, gimp_option_pdb_compat_mode,
233
/* don't translate the mode names (off|on|warn) */
234
N_("PDB compatibility mode (off|on|warn)"), "<mode>"
237
"stack-trace-mode", 0, 0,
238
G_OPTION_ARG_CALLBACK, gimp_option_stack_trace_mode,
239
/* don't translate the mode names (never|query|always) */
240
N_("Debug in case of a crash (never|query|always)"), "<mode>"
243
"debug-handlers", 0, G_OPTION_FLAG_NO_ARG,
244
G_OPTION_ARG_NONE, &use_debug_handler,
245
N_("Enable non-fatal debugging signal handlers"), NULL
248
"g-fatal-warnings", 0, G_OPTION_FLAG_NO_ARG,
249
G_OPTION_ARG_CALLBACK, gimp_option_fatal_warnings,
250
N_("Make all warnings fatal"), NULL
253
"dump-gimprc", 0, G_OPTION_FLAG_NO_ARG,
254
G_OPTION_ARG_CALLBACK, gimp_option_dump_gimprc,
255
N_("Output a gimprc file with default settings"), NULL
258
"dump-gimprc-system", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN,
259
G_OPTION_ARG_CALLBACK, gimp_option_dump_gimprc,
263
"dump-gimprc-manpage", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN,
264
G_OPTION_ARG_CALLBACK, gimp_option_dump_gimprc,
268
G_OPTION_REMAINING, 0, 0,
269
G_OPTION_ARG_FILENAME_ARRAY, &filenames,
279
GOptionContext *context;
280
GError *error = NULL;
281
const gchar *abort_message;
285
#if defined (__GNUC__) && defined (_WIN64)
286
/* mingw-w64, at least the unstable build from late July 2008,
287
* starts subsystem:windows programs in main(), but passes them
288
* bogus argc and argv. __argc and __argv are OK, though, so just
298
typedef BOOL (WINAPI *t_SetDllDirectoryA) (LPCSTR lpPathName);
299
t_SetDllDirectoryA p_SetDllDirectoryA;
301
p_SetDllDirectoryA = GetProcAddress (GetModuleHandle ("kernel32.dll"),
303
if (p_SetDllDirectoryA)
304
(*p_SetDllDirectoryA) ("");
308
typedef BOOL (WINAPI *t_SetProcessDEPPolicy) (DWORD dwFlags);
309
t_SetProcessDEPPolicy p_SetProcessDEPPolicy;
311
p_SetProcessDEPPolicy = GetProcAddress (GetModuleHandle ("kernel32.dll"),
312
"SetProcessDEPPolicy");
313
if (p_SetProcessDEPPolicy)
314
(*p_SetProcessDEPPolicy) (PROCESS_DEP_ENABLE|PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION);
319
g_thread_init (NULL);
322
gimp_open_console_window ();
327
gimp_env_init (FALSE);
333
g_set_application_name (GIMP_NAME);
335
basename = g_path_get_basename (argv[0]);
336
g_set_prgname (basename);
339
/* Check argv[] for "--verbose" first */
340
for (i = 1; i < argc; i++)
342
const gchar *arg = argv[i];
347
if ((strcmp (arg, "--verbose") == 0) || (strcmp (arg, "-v") == 0))
353
/* Check argv[] for "--no-interface" before trying to initialize gtk+. */
354
for (i = 1; i < argc; i++)
356
const gchar *arg = argv[i];
361
if ((strcmp (arg, "--no-interface") == 0) || (strcmp (arg, "-i") == 0))
365
else if ((strcmp (arg, "--version") == 0) || (strcmp (arg, "-v") == 0))
367
gimp_show_version_and_exit ();
369
#if defined (G_OS_WIN32) && !defined (GIMP_CONSOLE_COMPILATION)
370
else if ((strcmp (arg, "--help") == 0) ||
371
(strcmp (arg, "-?") == 0) ||
372
(strncmp (arg, "--help-", 7) == 0))
374
gimp_open_console_window ();
379
#ifdef GIMP_CONSOLE_COMPILATION
383
context = g_option_context_new (_("[FILE|URI...]"));
384
g_option_context_set_summary (context, GIMP_NAME);
386
g_option_context_add_main_entries (context, main_entries, GETTEXT_PACKAGE);
388
app_libs_init (context, no_interface);
390
if (! g_option_context_parse (context, &argc, &argv, &error))
394
gimp_open_console_window ();
395
g_print ("%s\n", error->message);
396
g_error_free (error);
401
_("GIMP could not initialize the graphical user interface.\n"
402
"Make sure a proper setup for your display environment "
406
app_exit (EXIT_FAILURE);
409
if (no_interface || be_verbose || console_messages || batch_commands != NULL)
410
gimp_open_console_window ();
415
#ifndef GIMP_CONSOLE_COMPILATION
416
if (! new_instance && gimp_unique_open (filenames, as_new))
420
_("Another GIMP instance is already running."));
422
gdk_notify_startup_complete ();
428
abort_message = sanity_check ();
430
app_abort (no_interface, abort_message);
432
gimp_init_signal_handlers ();
454
g_option_context_free (context);
462
/* Provide WinMain in case we build GIMP as a subsystem:windows
463
* application. Well, we do. When built with mingw, though, user code
464
* execution still starts in main() in that case. So WinMain() gets
465
* used on MSVC builds only.
470
# define _stdcall __attribute__((stdcall))
475
WinMain (struct HINSTANCE__ *hInstance,
476
struct HINSTANCE__ *hPrevInstance,
480
return main (__argc, __argv);
483
#ifndef GIMP_CONSOLE_COMPILATION
486
wait_console_window (void)
488
FILE *console = fopen ("CONOUT$", "w");
490
SetConsoleTitleW (g_utf8_to_utf16 (_("GIMP output. Type any character to close this window."), -1, NULL, NULL, NULL));
491
fprintf (console, _("(Type any character to close this window)\n"));
497
gimp_open_console_window (void)
499
if (((HANDLE) _get_osfhandle (fileno (stdout)) == INVALID_HANDLE_VALUE ||
500
(HANDLE) _get_osfhandle (fileno (stderr)) == INVALID_HANDLE_VALUE) && AllocConsole ())
502
if ((HANDLE) _get_osfhandle (fileno (stdout)) == INVALID_HANDLE_VALUE)
503
freopen ("CONOUT$", "w", stdout);
505
if ((HANDLE) _get_osfhandle (fileno (stderr)) == INVALID_HANDLE_VALUE)
506
freopen ("CONOUT$", "w", stderr);
508
SetConsoleTitleW (g_utf8_to_utf16 (_("GIMP output. You can minimize this window, but don't close it."), -1, NULL, NULL, NULL));
510
atexit (wait_console_window);
515
#endif /* G_OS_WIN32 */
519
gimp_option_fatal_warnings (const gchar *option_name,
524
GLogLevelFlags fatal_mask;
526
fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
527
fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
529
g_log_set_always_fatal (fatal_mask);
535
gimp_option_stack_trace_mode (const gchar *option_name,
540
if (strcmp (value, "never") == 0)
541
stack_trace_mode = GIMP_STACK_TRACE_NEVER;
542
else if (strcmp (value, "query") == 0)
543
stack_trace_mode = GIMP_STACK_TRACE_QUERY;
544
else if (strcmp (value, "always") == 0)
545
stack_trace_mode = GIMP_STACK_TRACE_ALWAYS;
553
gimp_option_pdb_compat_mode (const gchar *option_name,
558
if (! strcmp (value, "off"))
559
pdb_compat_mode = GIMP_PDB_COMPAT_OFF;
560
else if (! strcmp (value, "on"))
561
pdb_compat_mode = GIMP_PDB_COMPAT_ON;
562
else if (! strcmp (value, "warn"))
563
pdb_compat_mode = GIMP_PDB_COMPAT_WARN;
571
gimp_option_dump_gimprc (const gchar *option_name,
576
GimpConfigDumpFormat format = GIMP_CONFIG_DUMP_NONE;
578
gimp_open_console_window ();
580
if (strcmp (option_name, "--dump-gimprc") == 0)
581
format = GIMP_CONFIG_DUMP_GIMPRC;
582
if (strcmp (option_name, "--dump-gimprc-system") == 0)
583
format = GIMP_CONFIG_DUMP_GIMPRC_SYSTEM;
584
else if (strcmp (option_name, "--dump-gimprc-manpage") == 0)
585
format = GIMP_CONFIG_DUMP_GIMPRC_MANPAGE;
592
gimp = g_object_new (GIMP_TYPE_GIMP, NULL);
596
success = gimp_config_dump (format);
598
g_object_unref (gimp);
600
app_exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
607
gimp_show_version_and_exit (void)
609
gimp_open_console_window ();
610
gimp_version_show (be_verbose);
612
app_exit (EXIT_SUCCESS);
616
gimp_show_license_and_exit (void)
618
gimp_open_console_window ();
619
gimp_version_show (be_verbose);
622
g_print (GIMP_LICENSE);
625
app_exit (EXIT_SUCCESS);
629
gimp_init_malloc (void)
631
#ifdef GIMP_GLIB_MEM_PROFILER
632
g_mem_set_vtable (glib_mem_profiler_table);
633
g_atexit (g_mem_profile);
637
/* Tweak memory allocation so that memory allocated in chunks >= 4k
638
* (64x64 pixel 1bpp tile) gets returned to the system when free()'d.
640
* The default value for M_MMAP_THRESHOLD in glibc-2.3 is 128k.
641
* This is said to be an empirically derived value that works well
642
* in most systems. Lowering it to 4k is thus probably not the ideal
645
* An alternative to tuning this parameter would be to use
646
* malloc_trim(), for example after releasing a large tile-manager.
648
mallopt (M_MMAP_THRESHOLD, TILE_WIDTH * TILE_HEIGHT);
653
gimp_init_i18n (void)
655
setlocale (LC_ALL, "");
657
bindtextdomain (GETTEXT_PACKAGE"-libgimp", gimp_locale_directory ());
658
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
659
bind_textdomain_codeset (GETTEXT_PACKAGE"-libgimp", "UTF-8");
662
bindtextdomain (GETTEXT_PACKAGE, gimp_locale_directory ());
663
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
664
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
667
textdomain (GETTEXT_PACKAGE);
671
gimp_init_signal_handlers (void)
674
/* No use catching these on Win32, the user won't get any
675
* stack trace from glib anyhow. It's better to let Windows inform
676
* about the program error, and offer debugging (if the user
677
* has installed MSVC or some other compiler that knows how to
678
* install itself as a handler for program errors).
681
/* Handle fatal signals */
683
/* these are handled by gimp_terminate() */
684
gimp_signal_private (SIGHUP, gimp_sigfatal_handler, 0);
685
gimp_signal_private (SIGINT, gimp_sigfatal_handler, 0);
686
gimp_signal_private (SIGQUIT, gimp_sigfatal_handler, 0);
687
gimp_signal_private (SIGABRT, gimp_sigfatal_handler, 0);
688
gimp_signal_private (SIGTERM, gimp_sigfatal_handler, 0);
690
if (stack_trace_mode != GIMP_STACK_TRACE_NEVER)
692
/* these are handled by gimp_fatal_error() */
693
gimp_signal_private (SIGBUS, gimp_sigfatal_handler, 0);
694
gimp_signal_private (SIGSEGV, gimp_sigfatal_handler, 0);
695
gimp_signal_private (SIGFPE, gimp_sigfatal_handler, 0);
698
/* Ignore SIGPIPE because plug_in.c handles broken pipes */
699
gimp_signal_private (SIGPIPE, SIG_IGN, 0);
701
/* Restart syscalls on SIGCHLD */
702
gimp_signal_private (SIGCHLD, SIG_DFL, SA_RESTART);
704
#endif /* G_OS_WIN32 */
710
/* gimp core signal handler for fatal signals */
713
gimp_sigfatal_handler (gint sig_num)
722
gimp_terminate (g_strsignal (sig_num));
729
gimp_fatal_error (g_strsignal (sig_num));
734
#endif /* ! G_OS_WIN32 */