4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * See the included file LICENSE for details.
14 #include <libmainloop/select.h>
15 #include <libmainloop/exec.h>
28 void ioncore_setup_display(int xscr)
35 display=XDisplayName(ioncore_g.display);
37 /* %ui, UINT_MAX is used to ensure there is enough space for the screen
40 libtu_asprintf(&tmp, "DISPLAY=%s.0123456789a", display);
53 snprintf(tmp+strlen(tmp), 11, ".%u", (unsigned)xscr);
57 /* No need to free it, we'll execve soon */
64 void ioncore_setup_environ(const WExecP *p)
68 ioncore_setup_display(p->target!=NULL
69 ? region_rootwin_of(p->target)->xscr
72 /* Set up working directory */
81 WHook *ioncore_exec_environ_hook=NULL;
84 static void setup_exec(void *execp)
86 hook_call_p(ioncore_exec_environ_hook, execp, NULL);
97 int ioncore_do_exec_on(WRegion *reg, const char *cmd, const char *wd,
106 return mainloop_popen_bgread(cmd, setup_exec, (void*)&p,
107 extl_fn_none(), errh);
112 * Run \var{cmd} with the environment variable DISPLAY set to point to the
113 * X display the WM is running on. No specific screen is set unlike with
114 * \fnref{WRootWin.exec_on}. The PID of the (shell executing the) new
115 * process is returned.
119 int ioncore_exec(const char *cmd)
121 return ioncore_do_exec_on(NULL, cmd, NULL, extl_fn_none());
126 * Run \var{cmd} with a read pipe connected to its stdout and stderr.
127 * When data is received through one of these pipes, \var{h} or \var{errh}
128 * is called with that data. When the pipe is closed, the handler is called
129 * with \code{nil} argument. The PID of the new process is returned, or
134 int ioncore_popen_bgread(const char *cmd, ExtlFn h, ExtlFn errh)
142 return mainloop_popen_bgread(cmd, setup_exec, (void*)&p, h, errh);
150 /*{{{ Exit, restart, snapshot */
153 static void (*smhook)(int what);
155 bool ioncore_set_smhook(void (*fn)(int what))
162 void ioncore_do_exit()
169 bool ioncore_do_snapshot()
171 if(!ioncore_save_layout())
175 hook_call_v(ioncore_snapshot_hook);
176 extl_unprotect(NULL);
182 void ioncore_emergency_snapshot()
185 warn(TR("Not saving state: running under session manager."));
187 ioncore_do_snapshot();
192 static char *other=NULL;
194 static void set_other(const char *s)
198 other=(s==NULL ? NULL : scopy(s));
202 void ioncore_do_restart()
206 if(ioncore_g.display!=NULL)
207 ioncore_setup_display(-1);
208 mainloop_do_exec(other);
211 execvp(ioncore_g.argv[0], ioncore_g.argv);
212 die_err_obj(ioncore_g.argv[0]);
217 * Causes the window manager to simply exit without saving
221 void ioncore_resign()
224 smhook(IONCORE_SM_RESIGN);
232 * End session saving it first.
235 void ioncore_shutdown()
238 smhook(IONCORE_SM_SHUTDOWN);
240 ioncore_do_snapshot();
247 * Restart, saving session first.
250 void ioncore_restart()
255 smhook(IONCORE_SM_RESTART);
257 ioncore_do_snapshot();
258 ioncore_do_restart();
264 * Attempt to restart another window manager \var{cmd}.
267 void ioncore_restart_other(const char *cmd)
272 smhook(IONCORE_SM_RESTART_OTHER);
274 ioncore_do_snapshot();
275 ioncore_do_restart();
284 void ioncore_snapshot()
287 smhook(IONCORE_SM_SNAPSHOT);
289 ioncore_do_snapshot();