2 * xutil.c - X-related useful functions
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
24 #include <X11/extensions/Xinerama.h>
30 extern AwesomeConf globalconf
;
32 /** Execute another process, replacing the current instance of Awesome
33 * \param screen Screen ID
35 * \ingroup ui_callback
38 uicb_exec(int screen
__attribute__ ((unused
)), char *arg
)
41 if(globalconf
.display
)
42 close(ConnectionNumber(globalconf
.display
));
44 sscanf(arg
, "%s", path
);
45 execlp(path
, arg
, NULL
);
48 /** Spawn another process
49 * \param screen Screen ID
51 * \ingroup ui_callback
54 uicb_spawn(int screen
, char *arg
)
56 static char *shell
= NULL
;
58 char *tmp
, newdisplay
[128];
60 if(!shell
&& !(shell
= getenv("SHELL")))
61 shell
= a_strdup("/bin/sh");
65 if(!XineramaIsActive(globalconf
.display
) && (tmp
= getenv("DISPLAY")))
67 display
= a_strdup(tmp
);
68 if((tmp
= strrchr(display
, '.')))
70 snprintf(newdisplay
, sizeof(newdisplay
), "%s.%d", display
, screen
);
71 setenv("DISPLAY", newdisplay
, 1);
75 /* The double-fork construct avoids zombie processes and keeps the code
76 * clean from stupid signal handlers. */
81 if(globalconf
.display
)
82 close(ConnectionNumber(globalconf
.display
));
84 execl(shell
, shell
, "-c", arg
, (char *) NULL
);
85 warn("execl '%s -c %s'", shell
, arg
);
94 xgettextprop(Window w
, Atom atom
, char *text
, ssize_t textlen
)
101 if(!text
|| !textlen
)
105 XGetTextProperty(globalconf
.display
, w
, &name
, atom
);
110 if(name
.encoding
== XA_STRING
)
111 a_strncpy(text
, textlen
, (char *) name
.value
, textlen
- 1);
113 else if(XmbTextPropertyToTextList(globalconf
.display
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
)
115 a_strncpy(text
, textlen
, *list
, textlen
- 1);
116 XFreeStringList(list
);
119 text
[textlen
- 1] = '\0';
126 get_numlockmask(Display
*disp
)
128 XModifierKeymap
*modmap
;
129 unsigned int mask
= 0;
132 modmap
= XGetModifierMapping(disp
);
133 for(i
= 0; i
< 8; i
++)
134 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
135 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
136 == XKeysymToKeycode(disp
, XK_Num_Lock
))
139 XFreeModifiermap(modmap
);
144 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80