4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * Ion is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
17 #include <libmainloop/select.h>
18 #include <libmainloop/exec.h>
31 void ioncore_setup_display(int xscr)
38 display=XDisplayName(ioncore_g.display);
40 /* %ui, UINT_MAX is used to ensure there is enough space for the screen
43 libtu_asprintf(&tmp, "DISPLAY=%s.0123456789a", display);
56 snprintf(tmp+strlen(tmp), 11, ".%u", (unsigned)xscr);
60 /* No need to free it, we'll execve soon */
67 void ioncore_setup_environ(const WExecP *p)
71 ioncore_setup_display(p->target!=NULL
72 ? region_rootwin_of(p->target)->xscr
75 /* Set up working directory */
84 WHook *ioncore_exec_environ_hook=NULL;
87 static void setup_exec(void *execp)
89 hook_call_p(ioncore_exec_environ_hook, execp, NULL);
100 int ioncore_do_exec_on(WRegion *reg, const char *cmd, const char *wd,
109 return mainloop_popen_bgread(cmd, setup_exec, (void*)&p,
110 extl_fn_none(), errh);
115 * Run \var{cmd} with the environment variable DISPLAY set to point to the
116 * X display the WM is running on. No specific screen is set unlike with
117 * \fnref{WRootWin.exec_on}. The PID of the (shell executing the) new
118 * process is returned.
122 int ioncore_exec(const char *cmd)
124 return ioncore_do_exec_on(NULL, cmd, NULL, extl_fn_none());
129 * Run \var{cmd} with a read pipe connected to its stdout.
130 * When data is received through the pipe, \var{handler} is called
135 int ioncore_popen_bgread(const char *cmd, ExtlFn h, ExtlFn errh)
143 return mainloop_popen_bgread(cmd, setup_exec, (void*)&p, h, errh);
151 /*{{{ Exit, restart, snapshot */
154 static void (*smhook)(int what);
156 bool ioncore_set_smhook(void (*fn)(int what))
163 void ioncore_do_exit()
170 bool ioncore_do_snapshot()
172 if(!ioncore_save_layout())
176 hook_call_v(ioncore_snapshot_hook);
177 extl_unprotect(NULL);
183 void ioncore_emergency_snapshot()
186 warn(TR("Not saving state: running under session manager."));
188 ioncore_do_snapshot();
193 static char *other=NULL;
195 static void set_other(const char *s)
199 other=(s==NULL ? NULL : scopy(s));
203 void ioncore_do_restart()
207 if(ioncore_g.display!=NULL)
208 ioncore_setup_display(-1);
209 mainloop_do_exec(other);
212 execvp(ioncore_g.argv[0], ioncore_g.argv);
213 die_err_obj(ioncore_g.argv[0]);
218 * Causes the window manager to simply exit without saving
222 void ioncore_resign()
225 smhook(IONCORE_SM_RESIGN);
233 * End session saving it first.
236 void ioncore_shutdown()
239 smhook(IONCORE_SM_SHUTDOWN);
241 ioncore_do_snapshot();
248 * Restart, saving session first.
251 void ioncore_restart()
256 smhook(IONCORE_SM_RESTART);
258 ioncore_do_snapshot();
259 ioncore_do_restart();
265 * Attempt to restart another window manager \var{cmd}.
268 void ioncore_restart_other(const char *cmd)
273 smhook(IONCORE_SM_RESTART_OTHER);
275 ioncore_do_snapshot();
276 ioncore_do_restart();
285 void ioncore_snapshot()
288 smhook(IONCORE_SM_SNAPSHOT);
290 ioncore_do_snapshot();