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
)
43 /* remap all clients since some WM won't handle them otherwise */
44 for(c
= globalconf
.clients
; c
; c
= c
->next
)
47 XSync(globalconf
.display
, False
);
49 if(globalconf
.display
)
50 close(ConnectionNumber(globalconf
.display
));
52 sscanf(arg
, "%s", path
);
53 execlp(path
, arg
, NULL
);
56 /** Spawn another process
57 * \param screen Screen ID
59 * \ingroup ui_callback
62 uicb_spawn(int screen
, char *arg
)
64 static char *shell
= NULL
;
66 char *tmp
, newdisplay
[128];
68 if(!shell
&& !(shell
= getenv("SHELL")))
69 shell
= a_strdup("/bin/sh");
73 if(!XineramaIsActive(globalconf
.display
) && (tmp
= getenv("DISPLAY")))
75 display
= a_strdup(tmp
);
76 if((tmp
= strrchr(display
, '.')))
78 snprintf(newdisplay
, sizeof(newdisplay
), "%s.%d", display
, screen
);
79 setenv("DISPLAY", newdisplay
, 1);
83 /* The double-fork construct avoids zombie processes and keeps the code
84 * clean from stupid signal handlers. */
89 if(globalconf
.display
)
90 close(ConnectionNumber(globalconf
.display
));
92 execl(shell
, shell
, "-c", arg
, (char *) NULL
);
93 warn("execl '%s -c %s'", shell
, arg
);
102 xgettextprop(Window w
, Atom atom
, char *text
, ssize_t textlen
)
109 if(!text
|| !textlen
)
113 XGetTextProperty(globalconf
.display
, w
, &name
, atom
);
118 if(name
.encoding
== XA_STRING
)
119 a_strncpy(text
, textlen
, (char *) name
.value
, textlen
- 1);
121 else if(XmbTextPropertyToTextList(globalconf
.display
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
)
123 a_strncpy(text
, textlen
, *list
, textlen
- 1);
124 XFreeStringList(list
);
127 text
[textlen
- 1] = '\0';
134 get_numlockmask(Display
*disp
)
136 XModifierKeymap
*modmap
;
137 unsigned int mask
= 0;
140 modmap
= XGetModifierMapping(disp
);
141 for(i
= 0; i
< 8; i
++)
142 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
143 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
144 == XKeysymToKeycode(disp
, XK_Num_Lock
))
147 XFreeModifiermap(modmap
);
152 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80