2007-12-14 16:15:40 +01:00
|
|
|
/*
|
|
|
|
* xutil.c - X-related useful functions
|
|
|
|
*
|
|
|
|
* Copyright © 2007 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-01-29 11:27:14 +01:00
|
|
|
|
2007-11-17 20:53:31 +01:00
|
|
|
#include <X11/Xutil.h>
|
|
|
|
#include <X11/Xatom.h>
|
|
|
|
#include <X11/extensions/Xinerama.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2008-01-29 11:27:14 +01:00
|
|
|
#include "structs.h"
|
2007-11-17 20:53:31 +01:00
|
|
|
#include "xutil.h"
|
2007-12-16 02:45:38 +01:00
|
|
|
|
2007-12-19 04:46:44 +01:00
|
|
|
extern AwesomeConf globalconf;
|
2007-12-16 02:45:38 +01:00
|
|
|
|
2007-12-19 04:39:59 +01:00
|
|
|
/** Execute another process, replacing the current instance of Awesome
|
|
|
|
* \param screen Screen ID
|
|
|
|
* \param arg Command
|
|
|
|
* \ingroup ui_callback
|
|
|
|
*/
|
2007-11-17 20:53:31 +01:00
|
|
|
void
|
2007-12-17 01:17:54 +01:00
|
|
|
uicb_exec(int screen __attribute__ ((unused)), char *arg)
|
2007-11-17 20:53:31 +01:00
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
2007-12-16 02:45:38 +01:00
|
|
|
if(globalconf.display)
|
|
|
|
close(ConnectionNumber(globalconf.display));
|
2007-11-17 20:53:31 +01:00
|
|
|
|
|
|
|
sscanf(arg, "%s", path);
|
|
|
|
execlp(path, arg, NULL);
|
|
|
|
}
|
|
|
|
|
2007-12-19 04:39:59 +01:00
|
|
|
/** Spawn another process
|
|
|
|
* \param screen Screen ID
|
|
|
|
* \param arg Command
|
|
|
|
* \ingroup ui_callback
|
|
|
|
*/
|
2007-11-17 20:53:31 +01:00
|
|
|
void
|
2007-12-17 01:17:54 +01:00
|
|
|
uicb_spawn(int screen, char *arg)
|
2007-11-17 20:53:31 +01:00
|
|
|
{
|
|
|
|
static char *shell = NULL;
|
|
|
|
char *display = NULL;
|
|
|
|
char *tmp, newdisplay[128];
|
|
|
|
|
|
|
|
if(!shell && !(shell = getenv("SHELL")))
|
|
|
|
shell = a_strdup("/bin/sh");
|
|
|
|
if(!arg)
|
|
|
|
return;
|
|
|
|
|
2007-12-16 02:45:38 +01:00
|
|
|
if(!XineramaIsActive(globalconf.display) && (tmp = getenv("DISPLAY")))
|
2007-11-17 20:53:31 +01:00
|
|
|
{
|
|
|
|
display = a_strdup(tmp);
|
|
|
|
if((tmp = strrchr(display, '.')))
|
|
|
|
*tmp = '\0';
|
2007-12-11 20:56:51 +01:00
|
|
|
snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, screen);
|
2007-11-17 20:53:31 +01:00
|
|
|
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)
|
|
|
|
{
|
2007-12-16 02:45:38 +01:00
|
|
|
if(globalconf.display)
|
|
|
|
close(ConnectionNumber(globalconf.display));
|
2007-11-17 20:53:31 +01:00
|
|
|
setsid();
|
|
|
|
execl(shell, shell, "-c", arg, (char *) NULL);
|
2007-12-13 02:40:45 +01:00
|
|
|
warn("execl '%s -c %s'", shell, arg);
|
2007-11-17 20:53:31 +01:00
|
|
|
perror(" failed");
|
|
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
wait(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bool
|
2008-01-06 22:53:40 +01:00
|
|
|
xgettextprop(Window w, Atom atom, char *text, ssize_t textlen)
|
2007-11-17 20:53:31 +01:00
|
|
|
{
|
|
|
|
char **list = NULL;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
XTextProperty name;
|
|
|
|
|
|
|
|
if(!text || !textlen)
|
|
|
|
return False;
|
|
|
|
|
|
|
|
text[0] = '\0';
|
2008-01-06 22:53:40 +01:00
|
|
|
XGetTextProperty(globalconf.display, w, &name, atom);
|
2007-11-17 20:53:31 +01:00
|
|
|
|
|
|
|
if(!name.nitems)
|
|
|
|
return False;
|
|
|
|
|
|
|
|
if(name.encoding == XA_STRING)
|
|
|
|
a_strncpy(text, textlen, (char *) name.value, textlen - 1);
|
|
|
|
|
2008-01-06 22:53:40 +01:00
|
|
|
else if(XmbTextPropertyToTextList(globalconf.display, &name, &list, &n) >= Success && n > 0 && *list)
|
2007-11-17 20:53:31 +01:00
|
|
|
{
|
|
|
|
a_strncpy(text, textlen, *list, textlen - 1);
|
|
|
|
XFreeStringList(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
text[textlen - 1] = '\0';
|
|
|
|
XFree(name.value);
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2008-01-16 15:22:40 +01:00
|
|
|
unsigned int
|
|
|
|
get_numlockmask(Display *disp)
|
|
|
|
{
|
|
|
|
XModifierKeymap *modmap;
|
|
|
|
unsigned int mask = 0;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
modmap = XGetModifierMapping(disp);
|
|
|
|
for(i = 0; i < 8; i++)
|
|
|
|
for(j = 0; j < modmap->max_keypermod; j++)
|
|
|
|
if(modmap->modifiermap[i * modmap->max_keypermod + j]
|
|
|
|
== XKeysymToKeycode(disp, XK_Num_Lock))
|
|
|
|
mask = (1 << i);
|
|
|
|
|
|
|
|
XFreeModifiermap(modmap);
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|