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_environ(int xscr)
36 display=XDisplayName(ioncore_g.display);
38 /* %ui, UINT_MAX is used to ensure there is enough space for the screen
41 libtu_asprintf(&tmp, "DISPLAY=%s.0123456789a", display);
54 snprintf(tmp+strlen(tmp), 11, ".%u", (unsigned)xscr);
58 /* No need to free it, we'll execve soon */
71 static void setup_exec(void *p_)
75 close(ioncore_g.conn);
83 ioncore_setup_environ(p->rw==NULL ? -1 : p->rw->xscr);
93 int ioncore_do_exec_rw(WRootWin *rw, const char *cmd, const char *wd,
101 return mainloop_popen_bgread(cmd, setup_exec, (void*)&p,
102 extl_fn_none(), errh);
107 * Run \var{cmd} with the environment variable DISPLAY set to point to the
108 * X display the WM is running on. No specific screen is set unlike with
109 * \fnref{WRootWin.exec_on}. The PID of the (shell executing the) new
110 * process is returned.
114 int ioncore_exec(const char *cmd)
116 return ioncore_do_exec_rw(NULL, cmd, NULL, extl_fn_none());
121 * Run \var{cmd} with a read pipe connected to its stdout.
122 * When data is received through the pipe, \var{handler} is called
127 int ioncore_popen_bgread(const char *cmd, ExtlFn h, ExtlFn errh)
134 return mainloop_popen_bgread(cmd, setup_exec, (void*)&p, h, errh);
142 /*{{{ Exit, restart, snapshot */
145 static void (*smhook)(int what);
147 bool ioncore_set_smhook(void (*fn)(int what))
154 void ioncore_do_exit()
161 bool ioncore_do_snapshot()
163 if(!ioncore_save_layout())
167 hook_call_v(ioncore_snapshot_hook);
168 extl_unprotect(NULL);
174 void ioncore_emergency_snapshot()
177 warn(TR("Not saving state: running under session manager."));
179 ioncore_do_snapshot();
184 static char *other=NULL;
186 static void set_other(const char *s)
190 other=(s==NULL ? NULL : scopy(s));
194 void ioncore_do_restart()
198 if(ioncore_g.display!=NULL)
199 ioncore_setup_environ(-1);
200 mainloop_do_exec(other);
203 execvp(ioncore_g.argv[0], ioncore_g.argv);
204 die_err_obj(ioncore_g.argv[0]);
209 * Causes the window manager to simply exit without saving
213 void ioncore_resign()
216 smhook(IONCORE_SM_RESIGN);
224 * End session saving it first.
227 void ioncore_shutdown()
230 smhook(IONCORE_SM_SHUTDOWN);
232 ioncore_do_snapshot();
239 * Restart, saving session first.
242 void ioncore_restart()
247 smhook(IONCORE_SM_RESTART);
249 ioncore_do_snapshot();
250 ioncore_do_restart();
256 * Attempt to restart another window manager \var{cmd}.
259 void ioncore_restart_other(const char *cmd)
264 smhook(IONCORE_SM_RESTART_OTHER);
266 ioncore_do_snapshot();
267 ioncore_do_restart();
276 void ioncore_snapshot()
279 smhook(IONCORE_SM_SNAPSHOT);
281 ioncore_do_snapshot();