2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2000-07-13 05:52:01 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
2003-09-08 00:23:02 +08:00
|
|
|
*
|
2000-07-13 05:52:01 +08:00
|
|
|
* gimp-remote.c
|
2007-10-25 22:46:27 +08:00
|
|
|
* Copyright (C) 2000-2007 Sven Neumann <sven@gimp.org>
|
2003-01-06 22:28:29 +08:00
|
|
|
* Simon Budig <simon@gimp.org>
|
|
|
|
*
|
2000-07-13 05:52:01 +08:00
|
|
|
* Tells a running gimp to open files by creating a synthetic drop-event.
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2000-07-13 05:52:01 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2000-07-13 05:52:01 +08:00
|
|
|
* (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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2000-07-13 05:52:01 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Disclaimer:
|
|
|
|
*
|
|
|
|
* It is a really bad idea to use Drag'n'Drop for inter-client
|
|
|
|
* communication. Dont even think about doing this in your own newly
|
2007-10-25 22:46:27 +08:00
|
|
|
* created application.
|
2000-07-13 05:52:01 +08:00
|
|
|
* Simon
|
|
|
|
*/
|
|
|
|
|
2000-08-08 22:05:50 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2001-05-10 23:39:00 +08:00
|
|
|
#include <stdlib.h>
|
2005-02-05 16:53:19 +08:00
|
|
|
#include <locale.h>
|
|
|
|
|
2000-08-08 22:05:50 +08:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2001-05-22 08:36:38 +08:00
|
|
|
#include "libgimpbase/gimpversion.h"
|
2000-07-13 05:52:01 +08:00
|
|
|
|
2005-02-04 04:23:51 +08:00
|
|
|
#include <glib/gi18n.h>
|
|
|
|
|
2007-10-25 22:46:27 +08:00
|
|
|
#include "gimp-remote.h"
|
2000-07-13 05:52:01 +08:00
|
|
|
|
2004-03-07 07:30:19 +08:00
|
|
|
|
2007-10-25 22:46:27 +08:00
|
|
|
static void show_version (void) G_GNUC_NORETURN;
|
2004-04-10 18:25:36 +08:00
|
|
|
|
2005-02-04 00:56:43 +08:00
|
|
|
|
|
|
|
static gboolean existing = FALSE;
|
|
|
|
static gboolean query = FALSE;
|
|
|
|
static gboolean no_splash = FALSE;
|
2006-08-15 03:11:15 +08:00
|
|
|
static gboolean print_xid = FALSE;
|
2005-02-04 00:56:43 +08:00
|
|
|
static const gchar **filenames = NULL;
|
|
|
|
|
|
|
|
static const GOptionEntry main_entries[] =
|
|
|
|
{
|
2005-09-26 18:01:22 +08:00
|
|
|
{ "version", 'v', G_OPTION_FLAG_NO_ARG,
|
2005-02-04 00:56:43 +08:00
|
|
|
G_OPTION_ARG_CALLBACK, (GOptionArgFunc) show_version,
|
2005-02-04 04:23:51 +08:00
|
|
|
N_("Show version information and exit"), NULL
|
2005-02-04 00:56:43 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"existing", 'e', 0,
|
|
|
|
G_OPTION_ARG_NONE, &existing,
|
2005-02-04 04:23:51 +08:00
|
|
|
N_("Use a running GIMP only, never start a new one"), NULL
|
2005-02-04 00:56:43 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"query", 'q', 0,
|
|
|
|
G_OPTION_ARG_NONE, &query,
|
2005-02-04 04:23:51 +08:00
|
|
|
N_("Only check if GIMP is running, then quit"), NULL
|
2005-02-04 00:56:43 +08:00
|
|
|
},
|
2007-10-25 22:46:27 +08:00
|
|
|
#ifdef GDK_WINDOWING_X11
|
2006-08-15 03:11:15 +08:00
|
|
|
{
|
|
|
|
"print-xid", 'p', 0,
|
|
|
|
G_OPTION_ARG_NONE, &print_xid,
|
|
|
|
N_("Print X window ID of GIMP toolbox window, then quit"), NULL
|
|
|
|
},
|
2007-10-25 22:46:27 +08:00
|
|
|
#endif
|
2005-02-04 00:56:43 +08:00
|
|
|
{
|
|
|
|
"no-splash", 's', 0,
|
|
|
|
G_OPTION_ARG_NONE, &no_splash,
|
2005-02-04 04:23:51 +08:00
|
|
|
N_("Start GIMP without showing the startup window"), NULL
|
2005-02-04 00:56:43 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
G_OPTION_REMAINING, 0, 0,
|
|
|
|
G_OPTION_ARG_FILENAME_ARRAY, &filenames,
|
|
|
|
NULL, NULL
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
2004-01-21 04:09:32 +08:00
|
|
|
|
2000-08-08 22:05:50 +08:00
|
|
|
static void
|
2005-02-04 00:56:43 +08:00
|
|
|
show_version (void)
|
2000-08-08 22:05:50 +08:00
|
|
|
{
|
2005-02-04 04:23:51 +08:00
|
|
|
g_print (_("%s version %s"), "gimp-remote", GIMP_VERSION);
|
|
|
|
g_print ("\n");
|
|
|
|
|
2005-02-04 00:56:43 +08:00
|
|
|
exit (EXIT_SUCCESS);
|
2000-08-08 22:05:50 +08:00
|
|
|
}
|
2000-07-13 05:52:01 +08:00
|
|
|
|
2005-02-04 04:23:51 +08:00
|
|
|
static void
|
|
|
|
init_i18n (void)
|
|
|
|
{
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
|
|
|
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
|
|
|
|
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
|
|
|
|
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
textdomain (GETTEXT_PACKAGE);
|
|
|
|
}
|
|
|
|
|
2006-06-23 16:33:23 +08:00
|
|
|
/* Check name for a valid URI scheme as described in RFC 2396.
|
|
|
|
* URI scheme:suffix with scheme = [alpha][alphanum|digit|+|-|.]*
|
|
|
|
* Note that a null suffix will be considered as an invalid URI.
|
|
|
|
* return 1 if valid scheme found, else 0
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
is_valid_uri (const gchar *name)
|
|
|
|
{
|
|
|
|
const gchar *c = name;
|
|
|
|
|
|
|
|
g_return_val_if_fail (name != NULL, FALSE);
|
|
|
|
|
|
|
|
/* first character must be alpha */
|
|
|
|
if (! g_ascii_isalpha (*c))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
c++;
|
|
|
|
while (*c != '\0' && *c != ':')
|
|
|
|
{
|
|
|
|
if (! g_ascii_isalnum (*c) && *c != '+' && *c != '-' && *c != '.')
|
|
|
|
return FALSE;
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*c == ':')
|
|
|
|
{
|
|
|
|
c++;
|
|
|
|
if (*c != '\0')
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2003-09-08 00:23:02 +08:00
|
|
|
gint
|
|
|
|
main (gint argc,
|
2000-08-08 22:05:50 +08:00
|
|
|
gchar **argv)
|
2000-07-13 05:52:01 +08:00
|
|
|
{
|
2005-02-04 00:56:43 +08:00
|
|
|
GOptionContext *context;
|
|
|
|
GError *error = NULL;
|
|
|
|
GdkDisplay *display;
|
|
|
|
GdkScreen *screen;
|
2007-10-25 22:46:27 +08:00
|
|
|
GdkWindow *toolbox;
|
2005-02-04 00:56:43 +08:00
|
|
|
const gchar *startup_id;
|
|
|
|
gchar *desktop_startup_id = NULL;
|
|
|
|
GString *file_list = g_string_new (NULL);
|
2004-03-07 07:30:19 +08:00
|
|
|
|
2005-02-04 04:23:51 +08:00
|
|
|
init_i18n ();
|
|
|
|
|
2004-03-07 07:30:19 +08:00
|
|
|
/* we save the startup_id before calling gtk_init()
|
|
|
|
because GTK+ will unset it */
|
|
|
|
|
|
|
|
startup_id = g_getenv ("DESKTOP_STARTUP_ID");
|
|
|
|
if (startup_id && *startup_id)
|
|
|
|
desktop_startup_id = g_strdup (startup_id);
|
|
|
|
|
2005-02-04 00:56:43 +08:00
|
|
|
/* parse the command-line options */
|
|
|
|
context = g_option_context_new ("[FILE|URI...]");
|
|
|
|
g_option_context_add_main_entries (context, main_entries, NULL);
|
2005-02-09 04:18:07 +08:00
|
|
|
g_option_context_add_group (context, gtk_get_option_group (TRUE));
|
2003-11-01 02:36:11 +08:00
|
|
|
|
2005-02-04 00:56:43 +08:00
|
|
|
if (! g_option_context_parse (context, &argc, &argv, &error))
|
2003-01-06 22:28:29 +08:00
|
|
|
{
|
2005-02-04 00:56:43 +08:00
|
|
|
g_printerr ("%s\n", error->message);
|
|
|
|
g_error_free (error);
|
2004-01-20 20:07:26 +08:00
|
|
|
|
2005-02-04 00:56:43 +08:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2003-09-08 00:23:02 +08:00
|
|
|
|
2005-02-04 00:56:43 +08:00
|
|
|
gtk_init (&argc, &argv);
|
|
|
|
|
|
|
|
if (filenames)
|
|
|
|
{
|
|
|
|
gchar *cwd = g_get_current_dir ();
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
for (i = 0; filenames[i]; i++)
|
2003-01-06 22:28:29 +08:00
|
|
|
{
|
2005-02-04 00:56:43 +08:00
|
|
|
const gchar *name = filenames[i];
|
|
|
|
gchar *uri;
|
|
|
|
|
|
|
|
/* If not already a valid URI */
|
2006-06-23 16:33:23 +08:00
|
|
|
if (! is_valid_uri (name))
|
2003-01-06 22:28:29 +08:00
|
|
|
{
|
2005-02-04 00:56:43 +08:00
|
|
|
if (g_path_is_absolute (name))
|
|
|
|
{
|
|
|
|
uri = g_filename_to_uri (name, NULL, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gchar *abs = g_build_filename (cwd, name, NULL);
|
|
|
|
|
|
|
|
uri = g_filename_to_uri (abs, NULL, NULL);
|
|
|
|
g_free (abs);
|
|
|
|
}
|
2003-01-06 22:28:29 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-02-04 00:56:43 +08:00
|
|
|
uri = g_strdup (name);
|
2003-01-06 22:28:29 +08:00
|
|
|
}
|
2003-09-08 00:23:02 +08:00
|
|
|
|
2005-02-04 00:56:43 +08:00
|
|
|
if (uri)
|
2004-01-20 20:07:26 +08:00
|
|
|
{
|
2005-02-04 00:56:43 +08:00
|
|
|
if (file_list->len > 0)
|
|
|
|
file_list = g_string_append_c (file_list, '\n');
|
2004-01-20 20:07:26 +08:00
|
|
|
|
2005-02-04 00:56:43 +08:00
|
|
|
file_list = g_string_append (file_list, uri);
|
|
|
|
g_free (uri);
|
2004-01-20 20:07:26 +08:00
|
|
|
}
|
2003-01-06 22:28:29 +08:00
|
|
|
}
|
2003-09-08 00:23:02 +08:00
|
|
|
|
2005-02-04 00:56:43 +08:00
|
|
|
g_free (cwd);
|
2003-01-06 22:28:29 +08:00
|
|
|
}
|
2000-07-13 05:52:01 +08:00
|
|
|
|
2004-01-20 21:50:10 +08:00
|
|
|
display = gdk_display_get_default ();
|
|
|
|
screen = gdk_screen_get_default ();
|
|
|
|
|
2004-01-20 21:00:17 +08:00
|
|
|
/* if called without any filenames, always start a new GIMP */
|
2006-08-15 03:11:15 +08:00
|
|
|
if (file_list->len == 0 && !query && !print_xid && !existing)
|
2004-01-20 21:00:17 +08:00
|
|
|
{
|
2007-10-25 22:46:27 +08:00
|
|
|
gimp_remote_launch (screen,
|
|
|
|
argv[0], desktop_startup_id, no_splash,
|
|
|
|
file_list);
|
2004-01-20 21:00:17 +08:00
|
|
|
}
|
2003-11-01 02:36:11 +08:00
|
|
|
|
2007-10-25 22:46:27 +08:00
|
|
|
toolbox = gimp_remote_find_toolbox (display, screen);
|
2000-07-13 05:52:01 +08:00
|
|
|
|
2006-08-15 03:11:15 +08:00
|
|
|
if (! query && ! print_xid)
|
2004-01-21 04:09:32 +08:00
|
|
|
{
|
2007-10-25 22:46:27 +08:00
|
|
|
if (toolbox)
|
2004-01-20 20:07:26 +08:00
|
|
|
{
|
2007-10-25 22:46:27 +08:00
|
|
|
if (! gimp_remote_drop_files (display, toolbox, file_list))
|
|
|
|
return EXIT_FAILURE;
|
2004-03-07 07:52:19 +08:00
|
|
|
}
|
|
|
|
else if (! existing)
|
|
|
|
{
|
2007-10-25 22:46:27 +08:00
|
|
|
gimp_remote_launch (screen,
|
|
|
|
argv[0], desktop_startup_id, no_splash,
|
|
|
|
file_list);
|
2004-03-07 07:52:19 +08:00
|
|
|
}
|
2004-01-20 20:07:26 +08:00
|
|
|
}
|
2007-10-25 22:46:27 +08:00
|
|
|
else if (print_xid && toolbox)
|
2006-08-15 03:11:15 +08:00
|
|
|
{
|
2007-10-25 22:46:27 +08:00
|
|
|
gimp_remote_print_id (toolbox);
|
2006-08-15 03:11:15 +08:00
|
|
|
}
|
2004-01-21 04:09:32 +08:00
|
|
|
|
2004-03-07 07:52:19 +08:00
|
|
|
gdk_notify_startup_complete ();
|
2000-07-13 05:52:01 +08:00
|
|
|
|
2005-02-04 00:56:43 +08:00
|
|
|
g_option_context_free (context);
|
|
|
|
|
2003-01-06 22:28:29 +08:00
|
|
|
g_string_free (file_list, TRUE);
|
2004-03-07 07:30:19 +08:00
|
|
|
g_free (desktop_startup_id);
|
|
|
|
|
2007-10-25 22:46:27 +08:00
|
|
|
return (toolbox ? EXIT_SUCCESS : EXIT_FAILURE);
|
2000-07-13 05:52:01 +08:00
|
|
|
}
|