multiscreen execing functions



Hey Guys,
	In the course of doing panel and nautilus multihead support
the need for some variants of the gnome-exec and gspawn functions were
needed. Basically, they change the DISPLAY var of the child process so
that its default screen is the one specified in the GdkScreen arg.

	I guess the only sensible place for this is in libgnomeui and
that if Anders is willing to sponsor they should go into libegg for
the moment.

	I've attached the implementations.

	Thoughts, anyone ? Anders ?

Good Luck,
Mark.
/* egg-screen-exec.c
 *
 * Copyright (C) 2002  Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors: Mark McLoughlin <mark skynet ie>
 */

#include <config.h>

#include "egg-screen-exec.h"

#include <string.h>
#include <libgnome/gnome-exec.h>

#ifndef HAVE_GTK_MULTIHEAD
#include <gdk/gdkx.h>
#endif

extern char **environ;

char *
egg_screen_exec_display_string (GdkScreen *screen)
{
#ifdef HAVE_GTK_MULTIHEAD
	GString    *str;
	const char *old_display;
	char       *retval;
	char       *p;

	old_display = gdk_display_get_name (gdk_display_get_default ());

	str = g_string_new ("DISPLAY=");
	g_string_append (str, old_display);

	p = strrchr (str->str, '.');
	if (p && p >  strchr (str->str, ':'))
		g_string_truncate (str, p - str->str);

	g_string_append_printf (str, ".%d", gdk_screen_get_number (screen));

	retval = str->str;

	g_string_free (str, FALSE);

	return retval;
#else
	return g_strdup (DisplayString (GDK_DISPLAY ()));
#endif
}

char **
egg_screen_exec_environment (GdkScreen *screen)
{
#ifdef HAVE_GTK_MULTIHEAD
	char **retval = NULL;
	int    display_index = -1;
	int    i;

	for (i = 0; environ [i]; i++)
		if (!strncmp (environ [i], "DISPLAY", 7))
			display_index = i;

	if (display_index == -1)
		display_index = i++;

	retval = g_new (char *, i + 1);

	for (i = 0; environ [i]; i++)
		if (i == display_index)
			retval [i] = egg_screen_exec_display_string (screen);
		else
			retval [i] = g_strdup (environ [i]);
	retval [i] = NULL;

	return retval;
#else
	int i;

	for (i = 0; environ [i]; i++);

	retval = g_new (char *, i + 1);

	for (i = 0; environ [i]; i++)
		retval [i] = g_strdup (environ [i]);
	retval [i] = NULL;

	return retval;
#endif
}

int
egg_screen_execute_async (GdkScreen    *screen,
                          const char   *dir,
                          int           argc,
                          char * const  argv [])
{
#ifdef HAVE_GTK_MULTIHEAD
	char **envp = NULL;
	int    envc = 0;
	int    retval;

	g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);

	if (gdk_screen_get_default () != screen) {
		envc = 1;
		envp = g_new0 (char *, 2);
		envp [0] = egg_screen_exec_display_string (screen);
	}

	retval = gnome_execute_async_with_env (dir, argc, argv, envc, envp);

	g_strfreev (envp);

	return retval;
#else
	return gnome_execute_async (dir, argc, argv);
#endif
}

int
egg_screen_execute_shell (GdkScreen    *screen,
                          const char   *dir,
                          const char   *command)
{
#ifdef HAVE_GTK_MULTIHEAD
	int retval = -1;

	g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);

	if (gdk_screen_get_default () == screen)
		retval = gnome_execute_shell (dir, command);

	else {
		char *exec;
		char *display;

		display = egg_screen_exec_display_string (screen);
		exec = g_strconcat (display, " ", command, NULL);

		retval = gnome_execute_shell (dir, exec);

		g_free (display);
		g_free (exec);
	}

	return retval;
#else
	return gnome_execute_shell (dir, command);
#endif
}

gboolean
egg_screen_execute_command_line_async (GdkScreen    *screen,
                                       const char   *command,
                                       GError      **error)
{
#ifdef HAVE_GTK_MULTIHEAD
	gboolean   retval;
	char     **argv = NULL;
	char     **envp = NULL;

	g_return_val_if_fail (command != NULL, FALSE);

	if (!g_shell_parse_argv (command, NULL, &argv, error))
		return FALSE;

	if (gdk_screen_get_default () != screen)
		envp = egg_screen_exec_environment (screen);

	retval = g_spawn_async (g_get_home_dir (),
				argv, envp, G_SPAWN_SEARCH_PATH,
				NULL, NULL, NULL, error);
	g_strfreev (argv);
	g_strfreev (envp);

	return retval;
#else
	return g_spawn_command_line_async (command, error);
#endif
}
/* egg-screen-exec.h
 *
 * Copyright (C) 2002  Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors: Mark McLoughlin <mark skynet ie>
 */

#ifndef __EGG_SCREEN_EXEC_H__
#define __EGG_SCREEN_EXEC_H__

#include <gdk/gdk.h>

G_BEGIN_DECLS

char      *egg_screen_exec_display_string        (GdkScreen    *screen);
char     **egg_screen_exec_environment           (GdkScreen    *screen);

int        egg_screen_execute_async              (GdkScreen    *screen,
                                                  const char   *dir,
                                                  int           argc,
                                                  char * const  argv []);
int        egg_screen_execute_shell              (GdkScreen    *screen,
                                                  const char   *dir,
                                                  const char   *command);
gboolean   egg_screen_execute_command_line_async (GdkScreen    *screen,
                                                  const char   *command,
                                                  GError      **error);

G_END_DECLS

#endif /* __EGG_SCREEN_EXEC_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]