]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkspawn-x11.c
[introspection] add transfer none annotation to gdk_keyval_name return
[~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
24 #include "gdkspawn.h"
25
26 #include <glib.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30
31 typedef struct {
32   char *display;
33   GSpawnChildSetupFunc child_setup;
34   gpointer user_data;
35 } UserChildSetup;
36
37 /*
38  * Set the DISPLAY variable, and then call the user-specified child setup
39  * function.  This is required so that applications can use gdk_spawn_* and 
40  * call putenv() in their child_setup functions.
41  */
42 static void
43 set_environment (gpointer user_data)
44 {
45   UserChildSetup *setup = user_data;
46   
47   g_setenv ("DISPLAY", setup->display, TRUE);
48   
49   if (setup->child_setup)
50     setup->child_setup (setup->user_data);
51 }
52
53 /**
54  * gdk_spawn_on_screen:
55  * @screen: a #GdkScreen
56  * @working_directory: child's current working directory, or %NULL to 
57  *   inherit parent's
58  * @argv: child's argument vector
59  * @envp: child's environment, or %NULL to inherit parent's
60  * @flags: flags from #GSpawnFlags
61  * @child_setup: function to run in the child just before exec()
62  * @user_data: user data for @child_setup
63  * @child_pid: return location for child process ID, or %NULL
64  * @error: return location for error
65  *
66  * Like g_spawn_async(), except the child process is spawned in such
67  * an environment that on calling gdk_display_open() it would be
68  * returned a #GdkDisplay with @screen as the default screen.
69  *
70  * This is useful for applications which wish to launch an application
71  * on a specific screen.
72  *
73  * Return value: %TRUE on success, %FALSE if error is set
74  *
75  * Since: 2.4
76  **/
77 gboolean
78 gdk_spawn_on_screen (GdkScreen             *screen,
79                      const gchar           *working_directory,
80                      gchar                **argv,
81                      gchar                **envp,
82                      GSpawnFlags            flags,
83                      GSpawnChildSetupFunc   child_setup,
84                      gpointer               user_data,
85                      GPid                  *child_pid,
86                      GError               **error)
87 {
88   UserChildSetup setup_data;
89
90   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
91
92   setup_data.display = gdk_screen_make_display_name (screen);
93   setup_data.child_setup = child_setup;
94   setup_data.user_data = user_data;
95
96   return g_spawn_async (working_directory,
97                           argv,
98                           envp,
99                           flags,
100                           set_environment,
101                           &setup_data,
102                           child_pid,
103                           error);
104 }
105
106 /**
107  * gdk_spawn_on_screen_with_pipes:
108  * @screen: a #GdkScreen
109  * @working_directory: child's current working directory, or %NULL to 
110  *   inherit parent's
111  * @argv: child's argument vector
112  * @envp: child's environment, or %NULL to inherit parent's
113  * @flags: flags from #GSpawnFlags
114  * @child_setup: function to run in the child just before exec()
115  * @user_data: user data for @child_setup
116  * @child_pid: return location for child process ID, or %NULL
117  * @standard_input: return location for file descriptor to write to 
118  *   child's stdin, or %NULL
119  * @standard_output: return location for file descriptor to read child's 
120  *   stdout, or %NULL
121  * @standard_error: return location for file descriptor to read child's 
122  *   stderr, or %NULL
123  * @error: return location for error
124  *
125  * Like g_spawn_async_with_pipes(), except the child process is
126  * spawned in such an environment that on calling gdk_display_open()
127  * it would be returned a #GdkDisplay with @screen as the default
128  * screen.
129  *
130  * This is useful for applications which wish to launch an application
131  * on a specific screen.
132  *
133  * Return value: %TRUE on success, %FALSE if an error was set
134  *
135  * Since: 2.4
136  **/
137 gboolean
138 gdk_spawn_on_screen_with_pipes (GdkScreen            *screen,
139                                 const gchar          *working_directory,
140                                 gchar               **argv,
141                                 gchar               **envp,
142                                 GSpawnFlags           flags,
143                                 GSpawnChildSetupFunc  child_setup,
144                                 gpointer              user_data,
145                                 GPid                 *child_pid,
146                                 gint                 *standard_input,
147                                 gint                 *standard_output,
148                                 gint                 *standard_error,
149                                 GError              **error)
150 {
151   UserChildSetup setup_data;
152
153   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
154
155   setup_data.display = gdk_screen_make_display_name (screen);
156   setup_data.child_setup = child_setup;
157   setup_data.user_data = user_data;
158
159   return g_spawn_async_with_pipes (working_directory,
160                                      argv,
161                                      envp,
162                                      flags,
163                                      set_environment,
164                                      &setup_data,
165                                      child_pid,
166                                      standard_input,
167                                      standard_output,
168                                      standard_error,
169                                      error);
170
171 }
172
173 /**
174  * gdk_spawn_command_line_on_screen:
175  * @screen: a #GdkScreen
176  * @command_line: a command line
177  * @error: return location for errors
178  *
179  * Like g_spawn_command_line_async(), except the child process is
180  * spawned in such an environment that on calling gdk_display_open()
181  * it would be returned a #GdkDisplay with @screen as the default
182  * screen.
183  *
184  * This is useful for applications which wish to launch an application
185  * on a specific screen.
186  *
187  * Return value: %TRUE on success, %FALSE if error is set.
188  *
189  * Since: 2.4
190  **/
191 gboolean
192 gdk_spawn_command_line_on_screen (GdkScreen    *screen,
193                                   const gchar  *command_line,
194                                   GError      **error)
195 {
196   gchar    **argv = NULL;
197   gboolean   retval;
198
199   g_return_val_if_fail (command_line != NULL, FALSE);
200
201   if (!g_shell_parse_argv (command_line,
202                            NULL, &argv,
203                            error))
204     return FALSE;
205
206   retval = gdk_spawn_on_screen (screen,
207                                 NULL, argv, NULL,
208                                 G_SPAWN_SEARCH_PATH,
209                                 NULL, NULL, NULL,
210                                 error);
211   g_strfreev (argv);
212
213   return retval;
214 }