comparison src/gui.c @ 3087:3ecf9e91d88a v7.3.315

updated for version 7.3.315 Problem: Opening a window before forking causes problems for GTK. Solution: Fork first, create the window in the child and report back to the parent process whether it worked. If successful the parent exits, if unsuccessful the child exits and the parent continues in the terminal. (Tim Starling)
author Bram Moolenaar <bram@vim.org>
date Wed, 14 Sep 2011 19:04:39 +0200
parents 342b17608967
children 7ba2f171cdac
comparison
equal deleted inserted replaced
3086:83590251fae1 3087:3ecf9e91d88a
35 static void gui_update_horiz_scrollbar __ARGS((int)); 35 static void gui_update_horiz_scrollbar __ARGS((int));
36 static void gui_set_fg_color __ARGS((char_u *name)); 36 static void gui_set_fg_color __ARGS((char_u *name));
37 static void gui_set_bg_color __ARGS((char_u *name)); 37 static void gui_set_bg_color __ARGS((char_u *name));
38 static win_T *xy2win __ARGS((int x, int y)); 38 static win_T *xy2win __ARGS((int x, int y));
39 39
40 #if defined(UNIX) && !defined(__BEOS__) && !defined(MACOS_X) \
41 && !defined(__APPLE__)
42 # define MAY_FORK
43 static void gui_do_fork __ARGS((void));
44
45 static int gui_read_child_pipe __ARGS((int fd));
46
47 /* Return values for gui_read_child_pipe */
48 enum {
49 GUI_CHILD_IO_ERROR,
50 GUI_CHILD_OK,
51 GUI_CHILD_FAILED
52 };
53
54 #endif /* MAY_FORK */
55
56 static void gui_attempt_start __ARGS((void));
57
40 static int can_update_cursor = TRUE; /* can display the cursor */ 58 static int can_update_cursor = TRUE; /* can display the cursor */
41 59
42 /* 60 /*
43 * The Athena scrollbars can move the thumb to after the end of the scrollbar, 61 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
44 * this makes the thumb indicate the part of the text that is shown. Motif 62 * this makes the thumb indicate the part of the text that is shown. Motif
57 */ 75 */
58 void 76 void
59 gui_start() 77 gui_start()
60 { 78 {
61 char_u *old_term; 79 char_u *old_term;
62 #if defined(UNIX) && !defined(__BEOS__) && !defined(MACOS_X) \
63 && !defined(__APPLE__)
64 # define MAY_FORK
65 int dofork = TRUE;
66 #endif
67 static int recursive = 0; 80 static int recursive = 0;
68 81
69 old_term = vim_strsave(T_NAME); 82 old_term = vim_strsave(T_NAME);
70 83
71 /*
72 * Set_termname() will call gui_init() to start the GUI.
73 * Set the "starting" flag, to indicate that the GUI will start.
74 *
75 * We don't want to open the GUI shell until after we've read .gvimrc,
76 * otherwise we don't know what font we will use, and hence we don't know
77 * what size the shell should be. So if there are errors in the .gvimrc
78 * file, they will have to go to the terminal: Set full_screen to FALSE.
79 * full_screen will be set to TRUE again by a successful termcapinit().
80 */
81 settmode(TMODE_COOK); /* stop RAW mode */ 84 settmode(TMODE_COOK); /* stop RAW mode */
82 if (full_screen) 85 if (full_screen)
83 cursor_on(); /* needed for ":gui" in .vimrc */ 86 cursor_on(); /* needed for ":gui" in .vimrc */
84 gui.starting = TRUE;
85 full_screen = FALSE; 87 full_screen = FALSE;
86 88
87 #ifdef FEAT_GUI_GTK 89 ++recursive;
88 gui.event_time = GDK_CURRENT_TIME;
89 #endif
90 90
91 #ifdef MAY_FORK 91 #ifdef MAY_FORK
92 if (!gui.dofork || vim_strchr(p_go, GO_FORG) || recursive)
93 dofork = FALSE;
94 #endif
95 ++recursive;
96
97 termcapinit((char_u *)"builtin_gui");
98 gui.starting = recursive - 1;
99
100 if (!gui.in_use) /* failed to start GUI */
101 {
102 termcapinit(old_term); /* back to old term settings */
103 settmode(TMODE_RAW); /* restart RAW mode */
104 #ifdef FEAT_TITLE
105 set_title_defaults(); /* set 'title' and 'icon' again */
106 #endif
107 }
108
109 vim_free(old_term);
110
111 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
112 if (gui.in_use)
113 {
114 # ifdef FEAT_EVAL
115 Window x11_window;
116 Display *x11_display;
117
118 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
119 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
120 # endif
121
122 /* Display error messages in a dialog now. */
123 display_errors();
124 }
125 #endif
126
127 #if defined(MAY_FORK) && !defined(__QNXNTO__)
128 /* 92 /*
129 * Quit the current process and continue in the child. 93 * Quit the current process and continue in the child.
130 * Makes "gvim file" disconnect from the shell it was started in. 94 * Makes "gvim file" disconnect from the shell it was started in.
131 * Don't do this when Vim was started with "-f" or the 'f' flag is present 95 * Don't do this when Vim was started with "-f" or the 'f' flag is present
132 * in 'guioptions'. 96 * in 'guioptions'.
133 */ 97 */
134 if (gui.in_use && dofork) 98 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1)
135 { 99 {
136 int pipefd[2]; /* pipe between parent and child */ 100 gui_do_fork();
137 int pipe_error; 101 }
138 char dummy; 102 else
139 pid_t pid = -1; 103 #endif
140 104 {
141 /* Setup a pipe between the child and the parent, so that the parent 105 gui_attempt_start();
142 * knows when the child has done the setsid() call and is allowed to 106 }
143 * exit. */ 107
144 pipe_error = (pipe(pipefd) < 0); 108 if (!gui.in_use) /* failed to start GUI */
145 pid = fork(); 109 {
146 if (pid > 0) /* Parent */ 110 /* Back to old term settings
147 { 111 *
148 /* Give the child some time to do the setsid(), otherwise the 112 * FIXME: If we got here because a child process failed and flagged to
149 * exit() may kill the child too (when starting gvim from inside a 113 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
150 * gvim). */ 114 * hit an X11 I/O error and do a longjmp(), leaving recursive
151 if (pipe_error) 115 * permanently set to 1. This is probably not as big a problem as it
152 ui_delay(300L, TRUE); 116 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
153 else 117 * return "OK" unconditionally, so it would be very difficult to
154 { 118 * actually hit this case.
155 /* The read returns when the child closes the pipe (or when
156 * the child dies for some reason). */
157 close(pipefd[1]);
158 ignored = (int)read(pipefd[0], &dummy, (size_t)1);
159 close(pipefd[0]);
160 }
161
162 /* When swapping screens we may need to go to the next line, e.g.,
163 * after a hit-enter prompt and using ":gui". */
164 if (newline_on_exit)
165 mch_errmsg("\r\n");
166
167 /*
168 * The parent must skip the normal exit() processing, the child
169 * will do it. For example, GTK messes up signals when exiting.
170 */
171 _exit(0);
172 }
173
174 # if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
175 /*
176 * Change our process group. On some systems/shells a CTRL-C in the
177 * shell where Vim was started would otherwise kill gvim!
178 */ 119 */
179 if (pid == 0) /* child */ 120 termcapinit(old_term);
180 # if defined(HAVE_SETSID) 121 settmode(TMODE_RAW); /* restart RAW mode */
181 (void)setsid(); 122 #ifdef FEAT_TITLE
182 # else 123 set_title_defaults(); /* set 'title' and 'icon' again */
183 (void)setpgid(0, 0); 124 #endif
184 # endif 125 }
185 # endif 126
186 if (!pipe_error) 127 vim_free(old_term);
187 {
188 close(pipefd[0]);
189 close(pipefd[1]);
190 }
191
192 # if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
193 /* Tell the session manager our new PID */
194 gui_mch_forked();
195 # endif
196 }
197 #else
198 # if defined(__QNXNTO__)
199 if (gui.in_use && dofork)
200 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
201 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
202 # endif
203 #endif
204 128
205 #ifdef FEAT_AUTOCMD 129 #ifdef FEAT_AUTOCMD
206 /* If the GUI started successfully, trigger the GUIEnter event, otherwise 130 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
207 * the GUIFailed event. */ 131 * the GUIFailed event. */
208 gui_mch_update(); 132 gui_mch_update();
209 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED, 133 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
210 NULL, NULL, FALSE, curbuf); 134 NULL, NULL, FALSE, curbuf);
211 #endif 135 #endif
212
213 --recursive; 136 --recursive;
214 } 137 }
138
139 /*
140 * Set_termname() will call gui_init() to start the GUI.
141 * Set the "starting" flag, to indicate that the GUI will start.
142 *
143 * We don't want to open the GUI shell until after we've read .gvimrc,
144 * otherwise we don't know what font we will use, and hence we don't know
145 * what size the shell should be. So if there are errors in the .gvimrc
146 * file, they will have to go to the terminal: Set full_screen to FALSE.
147 * full_screen will be set to TRUE again by a successful termcapinit().
148 */
149 static void
150 gui_attempt_start()
151 {
152 static int recursive = 0;
153
154 ++recursive;
155 gui.starting = TRUE;
156
157 #ifdef FEAT_GUI_GTK
158 gui.event_time = GDK_CURRENT_TIME;
159 #endif
160
161 termcapinit((char_u *)"builtin_gui");
162 gui.starting = recursive - 1;
163
164 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
165 if (gui.in_use)
166 {
167 # ifdef FEAT_EVAL
168 Window x11_window;
169 Display *x11_display;
170
171 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
172 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
173 # endif
174
175 /* Display error messages in a dialog now. */
176 display_errors();
177 }
178 #endif
179 --recursive;
180 }
181
182 #ifdef MAY_FORK
183
184 /* for waitpid() */
185 # if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
186 # include <sys/wait.h>
187 # endif
188
189 /*
190 * Create a new process, by forking. In the child, start the GUI, and in
191 * the parent, exit.
192 *
193 * If something goes wrong, this will return with gui.in_use still set
194 * to FALSE, in which case the caller should continue execution without
195 * the GUI.
196 *
197 * If the child fails to start the GUI, then the child will exit and the
198 * parent will return. If the child succeeds, then the parent will exit
199 * and the child will return.
200 */
201 static void
202 gui_do_fork()
203 {
204 #ifdef __QNXNTO__
205 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
206 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
207 gui_attempt_start();
208 return;
209 #else
210 int pipefd[2]; /* pipe between parent and child */
211 int pipe_error;
212 int status;
213 int exit_status;
214 pid_t pid = -1;
215 FILE *parent_file;
216
217 /* Setup a pipe between the child and the parent, so that the parent
218 * knows when the child has done the setsid() call and is allowed to
219 * exit. */
220 pipe_error = (pipe(pipefd) < 0);
221 pid = fork();
222 if (pid < 0) /* Fork error */
223 {
224 EMSG(_("E851: Failed to create a new process for the GUI"));
225 return;
226 }
227 else if (pid > 0) /* Parent */
228 {
229 /* Give the child some time to do the setsid(), otherwise the
230 * exit() may kill the child too (when starting gvim from inside a
231 * gvim). */
232 if (!pipe_error)
233 {
234 /* The read returns when the child closes the pipe (or when
235 * the child dies for some reason). */
236 close(pipefd[1]);
237 status = gui_read_child_pipe(pipefd[0]);
238 if (status == GUI_CHILD_FAILED)
239 {
240 /* The child failed to start the GUI, so the caller must
241 * continue. There may be more error information written
242 * to stderr by the child. */
243 # ifdef __NeXT__
244 wait4(pid, &exit_status, 0, (struct rusage *)0);
245 # else
246 waitpid(pid, &exit_status, 0);
247 # endif
248 EMSG(_("E852: The child process failed to start the GUI"));
249 return;
250 }
251 else if (status == GUI_CHILD_IO_ERROR)
252 {
253 pipe_error = TRUE;
254 }
255 /* else GUI_CHILD_OK: parent exit */
256 }
257
258 if (pipe_error)
259 ui_delay(300L, TRUE);
260
261 /* When swapping screens we may need to go to the next line, e.g.,
262 * after a hit-enter prompt and using ":gui". */
263 if (newline_on_exit)
264 mch_errmsg("\r\n");
265
266 /*
267 * The parent must skip the normal exit() processing, the child
268 * will do it. For example, GTK messes up signals when exiting.
269 */
270 _exit(0);
271 }
272 /* Child */
273
274 # if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
275 /*
276 * Change our process group. On some systems/shells a CTRL-C in the
277 * shell where Vim was started would otherwise kill gvim!
278 */
279 # if defined(HAVE_SETSID)
280 (void)setsid();
281 # else
282 (void)setpgid(0, 0);
283 # endif
284 # endif
285 if (!pipe_error)
286 close(pipefd[0]);
287
288 # if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
289 /* Tell the session manager our new PID */
290 gui_mch_forked();
291 # endif
292
293 if (!pipe_error)
294 parent_file = fdopen(pipefd[1], "w");
295 else
296 parent_file = NULL;
297
298 /* Try to start the GUI */
299 gui_attempt_start();
300
301 /* Notify the parent */
302 if (parent_file != NULL)
303 {
304 fputs(gui.in_use ? "ok" : "fail", parent_file);
305 fclose(parent_file);
306 }
307
308 /* If we failed to start the GUI, exit now. */
309 if (!gui.in_use)
310 exit(1);
311 #endif
312 }
313
314 /*
315 * Read from a pipe assumed to be connected to the child process (this
316 * function is called from the parent).
317 * Return GUI_CHILD_OK if the child successfully started the GUI,
318 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
319 * some other error.
320 *
321 * The file descriptor will be closed before the function returns.
322 */
323 static int
324 gui_read_child_pipe(int fd)
325 {
326 size_t bytes_read;
327 FILE *file;
328 char buffer[10];
329
330 file = fdopen(fd, "r");
331 if (!file)
332 return GUI_CHILD_IO_ERROR;
333
334 bytes_read = fread(buffer, sizeof(char), sizeof(buffer)-1, file);
335 buffer[bytes_read] = '\0';
336 fclose(file);
337 if (strcmp(buffer, "ok") == 0)
338 return GUI_CHILD_OK;
339 return GUI_CHILD_FAILED;
340 }
341
342 #endif /* MAY_FORK */
215 343
216 /* 344 /*
217 * Call this when vim starts up, whether or not the GUI is started 345 * Call this when vim starts up, whether or not the GUI is started
218 */ 346 */
219 void 347 void