1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/*
* ion/ioncore/exec.c
*
* Copyright (c) Tuomo Valkonen 1999-2007.
*
* See the included file LICENSE for details.
*/
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <libmainloop/select.h>
#include <libmainloop/exec.h>
#include "common.h"
#include "exec.h"
#include "property.h"
#include "global.h"
#include "ioncore.h"
#include "saveload.h"
/*{{{ Exec */
void ioncore_setup_display(int xscr)
{
char *tmp, *ptr;
char *display;
/* Set up $DISPLAY */
display=XDisplayName(ioncore_g.display);
/* %ui, UINT_MAX is used to ensure there is enough space for the screen
* number
*/
libtu_asprintf(&tmp, "DISPLAY=%s.0123456789a", display);
if(tmp==NULL)
return;
ptr=strchr(tmp, ':');
if(ptr!=NULL){
ptr=strchr(ptr, '.');
if(ptr!=NULL)
*ptr='\0';
}
if(xscr>=0)
snprintf(tmp+strlen(tmp), 11, ".%u", (unsigned)xscr);
putenv(tmp);
/* No need to free it, we'll execve soon */
/*free(tmp);*/
/*XFree(display);*/
}
void ioncore_setup_environ(const WExecP *p)
{
/* Set up $DISPLAY */
ioncore_setup_display(p->target!=NULL
? region_rootwin_of(p->target)->xscr
: -1);
/* Set up working directory */
if(p->wd!=NULL){
if(chdir(p->wd)!=0)
warn_err_obj(p->wd);
}
}
WHook *ioncore_exec_environ_hook=NULL;
static void setup_exec(void *execp)
{
hook_call_p(ioncore_exec_environ_hook, execp, NULL);
#ifndef CF_NO_SETPGID
setpgid(0, 0);
#endif
ioncore_g.dpy=NULL;
}
EXTL_EXPORT
int ioncore_do_exec_on(WRegion *reg, const char *cmd, const char *wd,
ExtlFn errh)
{
WExecP p;
p.target=reg;
p.cmd=cmd;
p.wd=wd;
return mainloop_popen_bgread(cmd, setup_exec, (void*)&p,
extl_fn_none(), errh);
}
/*EXTL_DOC
* Run \var{cmd} with the environment variable DISPLAY set to point to the
* X display the WM is running on. No specific screen is set unlike with
* \fnref{WRootWin.exec_on}. The PID of the (shell executing the) new
* process is returned.
*/
EXTL_SAFE
EXTL_EXPORT
int ioncore_exec(const char *cmd)
{
return ioncore_do_exec_on(NULL, cmd, NULL, extl_fn_none());
}
/*EXTL_DOC
* Run \var{cmd} with a read pipe connected to its stdout.
* When data is received through the pipe, \var{handler} is called
* with that data.
*/
EXTL_SAFE
EXTL_EXPORT
int ioncore_popen_bgread(const char *cmd, ExtlFn h, ExtlFn errh)
{
WExecP p;
p.target=NULL;
p.wd=NULL;
p.cmd=cmd;
return mainloop_popen_bgread(cmd, setup_exec, (void*)&p, h, errh);
}
/*}}}*/
/*{{{ Exit, restart, snapshot */
static void (*smhook)(int what);
bool ioncore_set_smhook(void (*fn)(int what))
{
smhook=fn;
return TRUE;
}
void ioncore_do_exit()
{
ioncore_deinit();
exit(0);
}
bool ioncore_do_snapshot(bool save_layout)
{
if(save_layout && !ioncore_save_layout())
return FALSE;
extl_protect(NULL);
hook_call_v(ioncore_snapshot_hook);
extl_unprotect(NULL);
return TRUE;
}
void ioncore_emergency_snapshot()
{
if(smhook!=NULL)
warn(TR("Not saving state: running under session manager."));
else
ioncore_do_snapshot(TRUE);
}
static char *other=NULL;
static void set_other(const char *s)
{
if(other!=NULL)
free(other);
other=(s==NULL ? NULL : scopy(s));
}
void ioncore_do_restart()
{
ioncore_deinit();
if(other!=NULL){
if(ioncore_g.display!=NULL)
ioncore_setup_display(-1);
mainloop_do_exec(other);
warn_err_obj(other);
}
execvp(ioncore_g.argv[0], ioncore_g.argv);
die_err_obj(ioncore_g.argv[0]);
}
/*EXTL_DOC
* Causes the window manager to simply exit without saving
* state/session.
*/
EXTL_EXPORT
void ioncore_resign()
{
if(smhook!=NULL){
smhook(IONCORE_SM_RESIGN);
}else{
ioncore_do_exit();
}
}
/*EXTL_DOC
* End session saving it first.
*/
EXTL_EXPORT
void ioncore_shutdown()
{
if(smhook!=NULL){
smhook(IONCORE_SM_SHUTDOWN);
}else{
ioncore_do_snapshot(ioncore_g.autosave_layout);
ioncore_do_exit();
}
}
/*EXTL_DOC
* Restart, saving session first.
*/
EXTL_EXPORT
void ioncore_restart()
{
set_other(NULL);
if(smhook!=NULL){
smhook(IONCORE_SM_RESTART);
}else{
ioncore_do_snapshot(ioncore_g.autosave_layout);
ioncore_do_restart();
}
}
/*EXTL_DOC
* Attempt to restart another window manager \var{cmd}.
*/
EXTL_EXPORT
void ioncore_restart_other(const char *cmd)
{
set_other(cmd);
if(smhook!=NULL){
smhook(IONCORE_SM_RESTART_OTHER);
}else{
ioncore_do_snapshot(ioncore_g.autosave_layout);
ioncore_do_restart();
}
}
/*EXTL_DOC
* Save session.
*/
EXTL_EXPORT
void ioncore_snapshot()
{
if(smhook!=NULL)
smhook(IONCORE_SM_SNAPSHOT);
else
ioncore_do_snapshot(TRUE);
}
/*}}}*/
|