]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkspawn-x11.c
ca40b285f8fdbbd96514b20e220c145c75bd5fa7
[~andy/gtk] / gdk / x11 / gdkspawn-x11.c
1 /*
2  * Copyright (C) 2003 Sun Microsystems Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Authors: Mark McLoughlin <mark@skynet.ie>
20  */
21
22 #include <config.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include "gdkspawn.h"
27
28 #include <glib.h>
29 #include <gdk/gdk.h>
30 #include "gdkalias.h"
31   
32 typedef struct {
33   char *display;
34   GSpawnChildSetupFunc child_setup;
35   gpointer user_data;
36 } UserChildSetup;
37
38 /*
39  * Set the DISPLAY variable, and then call the user-specified child setup
40  * function.  This is required so that applications can use gdk_spawn_* and 
41  * call putenv() in their child_setup functions.
42  */
43 static void
44 set_environment (gpointer user_data)
45 {
46   UserChildSetup *setup = user_data;
47   
48   g_setenv ("DISPLAY", setup->display, TRUE);
49   
50   if (setup->child_setup)
51     setup->child_setup (setup->user_data);
52 }
53
54 /**
55  * gdk_spawn_on_screen:
56  * @screen: a #GdkScreen
57  * @working_directory: child's current working directory, or %NULL to 
58  *   inherit parent's
59  * @argv: child's argument vector
60  * @envp: child's environment, or %NULL to inherit parent's
61  * @flags: flags from #GSpawnFlags
62  * @child_setup: function to run in the child just before exec()
63  * @user_data: user data for @child_setup
64  * @child_pid: return location for child process ID, or %NULL
65  * @error: return location for error
66  *
67  * Like g_spawn_async(), except the child process is spawned in such
68  * an environment that on calling gdk_display_open() it would be
69  * returned a #GdkDisplay with @screen as the default screen.
70  *
71  * This is useful for applications which wish to launch an application
72  * on a specific screen.
73  *
74  * Return value: %TRUE on success, %FALSE if error is set
75  *
76  * Since: 2.4
77  **/
78 gboolean
79 gdk_spawn_on_screen (GdkScreen             *screen,
80                      const gchar           *working_directory,
81                      gchar                **argv,
82                      gchar                **envp,
83                      GSpawnFlags            flags,
84                      GSpawnChildSetupFunc   child_setup,
85                      gpointer               user_data,
86                      gint                  *child_pid,
87                      GError               **error)
88 {
89   UserChildSetup setup_data;
90
91   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
92
93   setup_data.display = gdk_screen_make_display_name (screen);
94   setup_data.child_setup = child_setup;
95   setup_data.user_data = user_data;
96
97   return g_spawn_async (working_directory,
98                           argv,
99                           envp,
100                           flags,
101                           set_environment,
102                           &setup_data,
103                           child_pid,
104                           error);
105 }
106
107 /**
108  * gdk_spawn_on_screen_with_pipes:
109  * @screen: a #GdkScreen
110  * @working_directory: child's current working directory, or %NULL to 
111  *   inherit parent's
112  * @argv: child's argument vector
113  * @envp: child's environment, or %NULL to inherit parent's
114  * @flags: flags from #GSpawnFlags
115  * @child_setup: function to run in the child just before exec()
116  * @user_data: user data for @child_setup
117  * @child_pid: return location for child process ID, or %NULL
118  * @standard_input: return location for file descriptor to write to 
119  *   child's stdin, or %NULL
120  * @standard_output: return location for file descriptor to read child's 
121  *   stdout, or %NULL
122  * @standard_error: return location for file descriptor to read child's 
123  *   stderr, or %NULL
124  * @error: return location for error
125  *
126  * Like g_spawn_async_with_pipes(), except the child process is
127  * spawned in such an environment that on calling gdk_display_open()
128  * it would be returned a #GdkDisplay with @screen as the default
129  * screen.
130  *
131  * This is useful for applications which wish to launch an application
132  * on a specific screen.
133  *
134  * Return value: %TRUE on success, %FALSE if an error was set
135  *
136  * Since: 2.4
137  **/
138 gboolean
139 gdk_spawn_on_screen_with_pipes (GdkScreen            *screen,
140                                 const gchar          *working_directory,
141                                 gchar               **argv,
142                                 gchar               **envp,
143                                 GSpawnFlags           flags,
144                                 GSpawnChildSetupFunc  child_setup,
145                                 gpointer              user_data,
146                                 gint                 *child_pid,
147                                 gint                 *standard_input,
148                                 gint                 *standard_output,
149                                 gint                 *standard_error,
150                                 GError              **error)
151 {
152   UserChildSetup setup_data;
153
154   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
155
156   setup_data.display = gdk_screen_make_display_name (screen);
157   setup_data.child_setup = child_setup;
158   setup_data.user_data = user_data;
159
160   return g_spawn_async_with_pipes (working_directory,
161                                      argv,
162                                      envp,
163                                      flags,
164                                      set_environment,
165                                      &setup_data,
166                                      child_pid,
167                                      standard_input,
168                                      standard_output,
169                                      standard_error,
170                                      error);
171
172 }
173
174 /**
175  * gdk_spawn_command_line_on_screen:
176  * @screen: a #GdkScreen
177  * @command_line: a command line
178  * @error: return location for errors
179  *
180  * Like g_spawn_command_line_async(), except the child process is
181  * spawned in such an environment that on calling gdk_display_open()
182  * it would be returned a #GdkDisplay with @screen as the default
183  * screen.
184  *
185  * This is useful for applications which wish to launch an application
186  * on a specific screen.
187  *
188  * Return value: %TRUE on success, %FALSE if error is set.
189  *
190  * Since: 2.4
191  **/
192 gboolean
193 gdk_spawn_command_line_on_screen (GdkScreen    *screen,
194                                   const gchar  *command_line,
195                                   GError      **error)
196 {
197   gchar    **argv = NULL;
198   gboolean   retval;
199
200   g_return_val_if_fail (command_line != NULL, FALSE);
201
202   if (!g_shell_parse_argv (command_line,
203                            NULL, &argv,
204                            error))
205     return FALSE;
206
207   retval = gdk_spawn_on_screen (screen,
208                                 NULL, argv, NULL,
209                                 G_SPAWN_SEARCH_PATH,
210                                 NULL, NULL, NULL,
211                                 error);
212   g_strfreev (argv);
213
214   return retval;
215 }
216
217 #define __GDK_SPAWN_X11_C__
218 #include "gdkaliasdef.c"