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.
21 #include <X11/Xutil.h>
22 #include <X11/Xatom.h>
23 #include <X11/extensions/Xinerama.h>
29 #include "common/util.h"
31 extern AwesomeConf globalconf
;
33 /** Execute another process, replacing the current instance of Awesome
34 * \param screen Screen ID
36 * \ingroup ui_callback
39 uicb_exec(int screen
__attribute__ ((unused
)), char *arg
)
42 if(globalconf
.display
)
43 close(ConnectionNumber(globalconf
.display
));
45 sscanf(arg
, "%s", path
);
46 execlp(path
, arg
, NULL
);
49 /** Spawn another process
50 * \param screen Screen ID
52 * \ingroup ui_callback
55 uicb_spawn(int screen
, char *arg
)
57 static char *shell
= NULL
;
59 char *tmp
, newdisplay
[128];
61 if(!shell
&& !(shell
= getenv("SHELL")))
62 shell
= a_strdup("/bin/sh");
66 if(!XineramaIsActive(globalconf
.display
) && (tmp
= getenv("DISPLAY")))
68 display
= a_strdup(tmp
);
69 if((tmp
= strrchr(display
, '.')))
71 snprintf(newdisplay
, sizeof(newdisplay
), "%s.%d", display
, screen
);
72 setenv("DISPLAY", newdisplay
, 1);
76 /* The double-fork construct avoids zombie processes and keeps the code
77 * clean from stupid signal handlers. */
82 if(globalconf
.display
)
83 close(ConnectionNumber(globalconf
.display
));
85 execl(shell
, shell
, "-c", arg
, (char *) NULL
);
86 warn("execl '%s -c %s'", shell
, arg
);
95 xgettextprop(Window w
, Atom atom
, char *text
, ssize_t textlen
)
102 if(!text
|| !textlen
)
106 XGetTextProperty(globalconf
.display
, w
, &name
, atom
);
111 if(name
.encoding
== XA_STRING
)
112 a_strncpy(text
, textlen
, (char *) name
.value
, textlen
- 1);
114 else if(XmbTextPropertyToTextList(globalconf
.display
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
)
116 a_strncpy(text
, textlen
, *list
, textlen
- 1);
117 XFreeStringList(list
);
120 text
[textlen
- 1] = '\0';
127 get_numlockmask(Display
*disp
)
129 XModifierKeymap
*modmap
;
130 unsigned int mask
= 0;
133 modmap
= XGetModifierMapping(disp
);
134 for(i
= 0; i
< 8; i
++)
135 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
136 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
137 == XKeysymToKeycode(disp
, XK_Num_Lock
))
140 XFreeModifiermap(modmap
);
145 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80