awesome/uicb.c

268 lines
7.0 KiB
C
Raw Normal View History

/*
* uicb.c - user interface callbacks management
*
2008-03-15 09:10:32 +01:00
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
*
* 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>
#include <string.h>
#include <unistd.h>
#include <errno.h>
2008-03-04 20:39:21 +01:00
2008-03-21 16:50:17 +01:00
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
2007-11-15 14:41:03 +01:00
#include "awesome.h"
#include "tag.h"
#include "mouse.h"
#include "statusbar.h"
#include "widget.h"
#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"
#include "titlebar.h"
2007-11-15 14:41:03 +01:00
#include "layouts/tile.h"
extern AwesomeConf globalconf;
const name_func_link_t UicbList[] =
{
/* awesome.h */
{ "quit", uicb_quit },
/* client.h */
{ "client_kill", uicb_client_kill },
{ "client_moveresize", uicb_client_moveresize },
{ "client_settrans", uicb_client_settrans },
{ "client_swap", uicb_client_swap },
{ "client_togglemax", uicb_client_togglemax },
{ "client_focus", uicb_client_focus },
{ "client_setfloating", uicb_client_setfloating },
{ "client_togglescratch", uicb_client_togglescratch },
{ "client_setscratch", uicb_client_setscratch },
2008-05-11 18:41:04 +02:00
{ "client_redraw", uicb_client_redraw },
/* focus.h */
{ "focus_history", uicb_focus_history },
{ "focus_client_byname", uicb_focus_client_byname },
/* layout.h */
{ "tag_setlayout", uicb_tag_setlayout },
/* mouse.h */
{ "client_movemouse", uicb_client_movemouse },
{ "client_resizemouse", uicb_client_resizemouse },
/* screen.h */
{ "screen_focus", uicb_screen_focus },
{ "client_movetoscreen", uicb_client_movetoscreen },
/* statusbar.h */
{ "statusbar_toggle", uicb_statusbar_toggle },
/* tag.h */
{ "client_tag", uicb_client_tag },
{ "client_toggletag", uicb_client_toggletag },
{ "tag_toggleview", uicb_tag_toggleview },
{ "tag_view", uicb_tag_view },
{ "tag_prev_selected", uicb_tag_prev_selected },
{ "tag_viewnext", uicb_tag_viewnext },
{ "tag_viewprev", uicb_tag_viewprev },
{ "tag_create", uicb_tag_create },
/* titlebar.h */
{ "client_toggletitlebar", uicb_client_toggletitlebar },
/* uicb.h */
{ "restart", uicb_restart },
{ "exec", uicb_exec },
{ "spawn", uicb_spawn },
/* widget.h */
{ "widget_tell", uicb_widget_tell },
/* layouts/tile.h */
{ "tag_setnmaster", uicb_tag_setnmaster},
{ "tag_setncol", uicb_tag_setncol },
{ "tag_setmwfact", uicb_tag_setmwfact },
{ NULL, NULL }
};
/** Restart awesome with the current command line.
* \param screen The virtual screen number.
* \param arg An unused argument.
* \ingroup ui_callback
*/
void
uicb_restart(int screen, char *arg __attribute__ ((unused)))
{
uicb_exec(screen, globalconf.argv);
}
/** Execute another process, replacing the current instance of awesome.
* \param screen The virtual screen number.
* \param cmd The command to start.
2008-03-04 20:39:21 +01:00
* \ingroup ui_callback
*/
void
uicb_exec(int screen __attribute__ ((unused)), char *cmd)
2008-03-04 20:39:21 +01:00
{
client_t *c;
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);
2008-03-21 16:50:17 +01:00
xcb_aux_sync(globalconf.connection);
xcb_disconnect(globalconf.connection);
2008-03-04 20:39:21 +01:00
/* Ignore the leading spaces if any */
2008-03-14 18:12:33 +01:00
while(cmd[0] && cmd[0] == ' ')
cmd++;
/* Get the beginning of the arguments */
2008-03-14 18:12:33 +01:00
args = strchr(cmd, ' ');
if(args)
path = a_strndup(cmd, args - cmd);
2008-03-14 18:12:33 +01:00
else
path = a_strndup(cmd, a_strlen(cmd));
execlp(path, cmd, NULL);
2008-03-14 18:12:33 +01:00
p_delete(&path);
2008-03-04 20:39:21 +01:00
}
/** Spawn another process.
* \param screen The virtual screen number.
* \param arg The command to run.
2008-03-04 20:39:21 +01:00
* \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");
if(!globalconf.screens_info->xinerama_is_active && (tmp = getenv("DISPLAY")))
2008-03-04 20:39:21 +01:00
{
display = a_strdup(tmp);
if((tmp = strrchr(display, '.')))
*tmp = '\0';
snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, screen);
setenv("DISPLAY", newdisplay, 1);
}
/* The double-fork construct avoids zombie processes and keeps the code
* clean from stupid signal handlers. */
if(fork() == 0)
{
if(fork() == 0)
{
2008-03-21 16:50:17 +01:00
if(globalconf.connection)
close(xcb_get_file_descriptor(globalconf.connection));
2008-03-04 20:39:21 +01:00
setsid();
execl(shell, shell, "-c", arg, NULL);
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);
}
/** Run the uicb.
* \param cmd The uicb command to parse.
* \return true on succes, false on failure.
*/
static bool
__uicb_run(char *cmd)
{
char *p, *argcpy;
const char *arg;
int screen;
ssize_t len;
uicb_t *uicb;
len = a_strlen(cmd);
p = strtok(cmd, " ");
if (!p)
{
2008-03-14 13:15:15 +01:00
warn("ignoring malformed command\n");
return false;
}
screen = atoi(cmd);
if(screen >= globalconf.screens_info->nscreen || screen < 0)
{
2008-03-14 13:15:15 +01:00
warn("invalid screen specified: %i\n", screen);
return false;
}
p = strtok(NULL, " ");
if (!p)
{
2008-03-14 13:15:15 +01:00
warn("ignoring malformed command.\n");
return false;
}
uicb = name_func_lookup(p, UicbList);
if (!uicb)
{
2008-03-14 13:15:15 +01:00
warn("unknown uicb function: %s.\n", p);
return false;
}
if (p + a_strlen(p) < cmd + len)
{
arg = p + a_strlen(p) + 1;
len = a_strlen(arg);
/* Allow our callees to modify this string. */
argcpy = p_new(char, len + 1);
a_strncpy(argcpy, len + 1, arg, len);
uicb(screen, argcpy);
p_delete(&argcpy);
}
else
uicb(screen, NULL);
return true;
}
/** Parse a command.
* \param cmd The buffer to parse.
* \return true on succes, false on failure.
*/
bool
__uicb_parsecmd(char *cmd)
2007-12-14 16:07:34 +01:00
{
char *p, *curcmd = cmd;
bool ret = false;
2007-12-14 16:07:34 +01:00
if(a_strlen(cmd))
while((p = strchr(curcmd, '\n')))
{
*p = '\0';
ret = __uicb_run(curcmd);
curcmd = p + 1;
}
return ret;
2007-12-14 16:07:34 +01:00
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80