2007-10-12 17:10:36 +02:00
|
|
|
/*
|
|
|
|
* uicb.c - user interface callbacks management
|
|
|
|
*
|
2008-03-15 09:10:32 +01:00
|
|
|
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
|
2007-10-12 17:10:36 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-02-06 08:49:31 +01:00
|
|
|
/**
|
|
|
|
* @defgroup ui_callback User Interface Callbacks
|
|
|
|
*/
|
|
|
|
|
2008-03-04 20:39:21 +01:00
|
|
|
#include <sys/wait.h>
|
2008-03-13 15:11:59 +01:00
|
|
|
#include <string.h>
|
2008-03-16 19:21:21 +01:00
|
|
|
#include <unistd.h>
|
2008-03-19 13:11:13 +01:00
|
|
|
#include <errno.h>
|
2008-03-04 20:39:21 +01:00
|
|
|
|
2007-11-15 14:41:03 +01:00
|
|
|
#include "awesome.h"
|
|
|
|
#include "tag.h"
|
|
|
|
#include "mouse.h"
|
|
|
|
#include "statusbar.h"
|
2007-12-16 10:25:13 +01:00
|
|
|
#include "widget.h"
|
2007-12-15 20:32:49 +01:00
|
|
|
#include "focus.h"
|
2008-01-01 17:25:48 +01:00
|
|
|
#include "client.h"
|
2008-01-01 18:02:36 +01:00
|
|
|
#include "screen.h"
|
2008-03-15 13:59:04 +01:00
|
|
|
#include "titlebar.h"
|
2007-11-15 14:41:03 +01:00
|
|
|
#include "layouts/tile.h"
|
2007-10-12 17:10:36 +02:00
|
|
|
|
2007-12-19 04:46:44 +01:00
|
|
|
extern AwesomeConf globalconf;
|
2007-12-16 02:45:38 +01:00
|
|
|
|
2008-01-15 16:52:30 +01:00
|
|
|
#include "uicbgen.h"
|
2007-10-12 17:10:36 +02:00
|
|
|
|
2008-04-10 23:37:22 +02:00
|
|
|
/** Restart awesome with the current command line.
|
2008-03-27 15:00:46 +01:00
|
|
|
* \param screen ID
|
2008-03-21 14:31:10 +01:00
|
|
|
* \param arg arg (unused)
|
|
|
|
* \ingroup ui_callback
|
|
|
|
*/
|
|
|
|
void
|
2008-03-27 15:00:46 +01:00
|
|
|
uicb_restart(int screen, char *arg __attribute__ ((unused)))
|
2008-03-21 14:31:10 +01:00
|
|
|
{
|
2008-03-27 15:00:46 +01:00
|
|
|
uicb_exec(screen, globalconf.argv);
|
2008-03-21 14:31:10 +01:00
|
|
|
}
|
|
|
|
|
2008-04-10 23:37:22 +02:00
|
|
|
/** Execute another process, replacing the current instance of awesome.
|
2008-03-04 20:39:21 +01:00
|
|
|
* \param screen Screen ID
|
2008-03-21 14:31:10 +01:00
|
|
|
* \param cmd Command
|
2008-03-04 20:39:21 +01:00
|
|
|
* \ingroup ui_callback
|
|
|
|
*/
|
|
|
|
void
|
2008-03-13 15:11:59 +01:00
|
|
|
uicb_exec(int screen __attribute__ ((unused)), char *cmd)
|
2008-03-04 20:39:21 +01:00
|
|
|
{
|
|
|
|
Client *c;
|
2008-03-13 15:11:59 +01:00
|
|
|
char *args, *path;
|
2008-03-04 20:39:21 +01:00
|
|
|
|
|
|
|
/* remap all clients since some WM won't handle them otherwise */
|
|
|
|
for(c = globalconf.clients; c; c = c->next)
|
|
|
|
client_unban(c);
|
|
|
|
|
|
|
|
XSync(globalconf.display, False);
|
2008-03-24 14:45:46 +01:00
|
|
|
XCloseDisplay(globalconf.display);
|
2008-03-04 20:39:21 +01:00
|
|
|
|
2008-03-13 15:11:59 +01:00
|
|
|
/* Ignore the leading spaces if any */
|
2008-03-14 18:12:33 +01:00
|
|
|
while(cmd[0] && cmd[0] == ' ')
|
|
|
|
cmd++;
|
2008-03-13 15:11:59 +01:00
|
|
|
|
|
|
|
/* Get the beginning of the arguments */
|
2008-03-14 18:12:33 +01:00
|
|
|
args = strchr(cmd, ' ');
|
|
|
|
|
|
|
|
if(args)
|
2008-04-19 21:24:23 +02:00
|
|
|
path = a_strndup(cmd, args - cmd);
|
2008-03-14 18:12:33 +01:00
|
|
|
else
|
2008-04-19 21:24:23 +02:00
|
|
|
path = a_strndup(cmd, a_strlen(cmd));
|
2008-03-13 15:11:59 +01:00
|
|
|
|
|
|
|
execlp(path, cmd, NULL);
|
2008-03-14 18:12:33 +01:00
|
|
|
|
2008-03-13 15:11:59 +01:00
|
|
|
p_delete(&path);
|
2008-03-04 20:39:21 +01:00
|
|
|
}
|
|
|
|
|
2008-04-10 23:37:22 +02:00
|
|
|
/** Spawn another process.
|
2008-03-04 20:39:21 +01:00
|
|
|
* \param screen Screen ID
|
|
|
|
* \param arg Command
|
|
|
|
* \ingroup ui_callback
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
uicb_spawn(int screen, char *arg)
|
|
|
|
{
|
|
|
|
static char *shell = NULL;
|
|
|
|
char *display = NULL;
|
|
|
|
char *tmp, newdisplay[128];
|
|
|
|
|
|
|
|
if(!arg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!shell && !(shell = getenv("SHELL")))
|
|
|
|
shell = a_strdup("/bin/sh");
|
|
|
|
|
2008-03-13 09:31:35 +01:00
|
|
|
if(!globalconf.screens_info->xinerama_is_active && (tmp = getenv("DISPLAY")))
|
2008-03-04 20:39:21 +01:00
|
|
|
{
|
|
|
|
display = a_strdup(tmp);
|
2009-03-15 09:50:56 +01:00
|
|
|
if ((tmp = strrchr(display, '.')) && !strchr(tmp, ':'))
|
2008-03-04 20:39:21 +01:00
|
|
|
*tmp = '\0';
|
|
|
|
snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, screen);
|
|
|
|
setenv("DISPLAY", newdisplay, 1);
|
2009-03-15 09:50:56 +01:00
|
|
|
p_delete(&display);
|
2008-03-04 20:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* The double-fork construct avoids zombie processes and keeps the code
|
|
|
|
* clean from stupid signal handlers. */
|
|
|
|
if(fork() == 0)
|
|
|
|
{
|
|
|
|
if(fork() == 0)
|
|
|
|
{
|
|
|
|
if(globalconf.display)
|
|
|
|
close(ConnectionNumber(globalconf.display));
|
|
|
|
setsid();
|
|
|
|
execl(shell, shell, "-c", arg, NULL);
|
2008-03-19 13:11:13 +01:00
|
|
|
warn("execl '%s -c %s' failed: %s\n", shell, arg, strerror(errno));
|
2008-03-04 20:39:21 +01:00
|
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
wait(0);
|
|
|
|
}
|
|
|
|
|
2008-04-08 09:45:48 +02:00
|
|
|
/** Run the uicb
|
|
|
|
* \param cmd the uicb command to parse
|
|
|
|
* \return 0 on succes, -1 on failure
|
|
|
|
*/
|
2007-12-14 16:07:34 +01:00
|
|
|
static int
|
2008-04-09 13:14:56 +02:00
|
|
|
__uicb_run(char *cmd)
|
2007-10-12 17:10:36 +02:00
|
|
|
{
|
2008-01-06 22:42:06 +01:00
|
|
|
char *p, *argcpy;
|
2007-10-15 19:11:19 +02:00
|
|
|
const char *arg;
|
2008-01-06 22:42:06 +01:00
|
|
|
int screen;
|
|
|
|
ssize_t len;
|
|
|
|
Uicb *uicb;
|
2007-10-12 17:10:36 +02:00
|
|
|
|
2008-01-06 22:42:06 +01:00
|
|
|
len = a_strlen(cmd);
|
2007-12-13 02:32:16 +01:00
|
|
|
p = strtok(cmd, " ");
|
2008-01-06 22:42:06 +01:00
|
|
|
if (!p)
|
|
|
|
{
|
2008-03-14 13:15:15 +01:00
|
|
|
warn("ignoring malformed command\n");
|
2007-10-12 17:10:36 +02:00
|
|
|
return -1;
|
2007-12-13 02:37:30 +01:00
|
|
|
}
|
2007-12-12 08:41:57 +01:00
|
|
|
screen = atoi(cmd);
|
2008-03-13 09:28:21 +01:00
|
|
|
if(screen >= globalconf.screens_info->nscreen || screen < 0)
|
2008-01-06 22:42:06 +01:00
|
|
|
{
|
2008-03-14 13:15:15 +01:00
|
|
|
warn("invalid screen specified: %i\n", screen);
|
2007-10-12 17:10:36 +02:00
|
|
|
return -1;
|
2007-12-13 02:37:30 +01:00
|
|
|
}
|
2007-10-12 17:10:36 +02:00
|
|
|
|
2007-12-13 02:32:16 +01:00
|
|
|
p = strtok(NULL, " ");
|
2008-01-06 22:42:06 +01:00
|
|
|
if (!p)
|
|
|
|
{
|
2008-03-14 13:15:15 +01:00
|
|
|
warn("ignoring malformed command.\n");
|
2007-12-13 02:32:16 +01:00
|
|
|
return -1;
|
2007-12-13 02:37:30 +01:00
|
|
|
}
|
2008-01-06 22:42:06 +01:00
|
|
|
|
2007-12-13 02:32:16 +01:00
|
|
|
uicb = name_func_lookup(p, UicbList);
|
2008-01-06 22:42:06 +01:00
|
|
|
if (!uicb)
|
|
|
|
{
|
2008-03-14 13:15:15 +01:00
|
|
|
warn("unknown uicb function: %s.\n", p);
|
2007-12-13 02:32:16 +01:00
|
|
|
return -1;
|
2007-12-13 02:37:30 +01:00
|
|
|
}
|
2007-10-12 17:10:36 +02:00
|
|
|
|
2008-01-06 22:42:06 +01:00
|
|
|
if (p + a_strlen(p) < cmd + len)
|
2007-12-16 10:25:13 +01:00
|
|
|
{
|
2008-01-06 22:42:06 +01:00
|
|
|
arg = p + a_strlen(p) + 1;
|
|
|
|
len = a_strlen(arg);
|
2007-12-16 10:25:13 +01:00
|
|
|
/* Allow our callees to modify this string. */
|
2008-01-06 22:42:06 +01:00
|
|
|
argcpy = p_new(char, len + 1);
|
|
|
|
a_strncpy(argcpy, len + 1, arg, len);
|
2007-12-16 10:25:13 +01:00
|
|
|
uicb(screen, argcpy);
|
|
|
|
p_delete(&argcpy);
|
|
|
|
}
|
2007-10-12 17:10:36 +02:00
|
|
|
else
|
2007-12-16 10:25:13 +01:00
|
|
|
uicb(screen, NULL);
|
2008-01-06 22:42:06 +01:00
|
|
|
|
2007-10-12 17:10:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2007-12-13 02:32:16 +01:00
|
|
|
|
2008-04-08 09:45:48 +02:00
|
|
|
/** Parse the control buffer.
|
|
|
|
* \param cmd the control buffer
|
|
|
|
* \return 0 on succes, -1 on failure
|
|
|
|
*/
|
2007-12-14 16:07:34 +01:00
|
|
|
int
|
2007-12-16 02:45:38 +01:00
|
|
|
parse_control(char *cmd)
|
2007-12-14 16:07:34 +01:00
|
|
|
{
|
|
|
|
char *p, *curcmd = cmd;
|
|
|
|
|
|
|
|
if(!a_strlen(cmd))
|
|
|
|
return -1;
|
2008-01-07 18:12:38 +01:00
|
|
|
|
2007-12-14 16:07:34 +01:00
|
|
|
while((p = strchr(curcmd, '\n')))
|
|
|
|
{
|
|
|
|
*p = '\0';
|
2008-04-09 13:14:56 +02:00
|
|
|
__uicb_run(curcmd);
|
2007-12-14 16:07:34 +01:00
|
|
|
curcmd = p + 1;
|
|
|
|
}
|
2008-01-06 22:42:06 +01:00
|
|
|
|
2007-12-14 16:07:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2007-12-13 02:32:16 +01:00
|
|
|
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|