1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Util.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000, 2002 - 2005
5 // Bradley T Hughes <bhughes at trolltech.com>
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the "Software"),
9 // to deal in the Software without restriction, including without limitation
10 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 // and/or sell copies of the Software, and to permit persons to whom the
12 // Software is furnished to do so, subject to the following conditions:
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 // DEALINGS IN THE SOFTWARE.
25 // need to include these before Util.hh
27 #include <X11/Xutil.h>
33 #include <X11/Xatom.h>
44 std::string
bt::basename(const std::string
& path
) {
45 std::string::size_type slash
= path
.rfind('/');
46 if (slash
== std::string::npos
)
48 return path
.substr(slash
+1);
52 std::string
bt::expandTilde(const std::string
& s
) {
55 const char* const home
= getenv("HOME");
58 return std::string(home
+ s
.substr(s
.find('/')));
62 void bt::bexec(const std::string
& command
, const std::string
& displaystring
) {
65 #ifndef __QNXTO__ // apparently, setsid interferes with signals on QNX
68 int ret
= putenv(const_cast<char *>(displaystring
.c_str()));
70 std::string cmd
= "exec ";
72 ret
= execl("/bin/sh", "/bin/sh", "-c", cmd
.c_str(), NULL
);
76 spawnlp(P_NOWAIT
, "cmd.exe", "cmd.exe", "/c", command
.c_str(), NULL
);
81 std::string
bt::itostring(unsigned long i
) {
83 return std::string("0");
85 const char nums
[] = "0123456789";
88 for (; i
> 0; i
/= 10)
89 tmp
.insert(tmp
.begin(), nums
[i
%10]);
94 std::string
bt::itostring(long i
) {
95 std::string tmp
= bt::itostring(static_cast<unsigned long>(abs(i
)));
97 tmp
.insert(tmp
.begin(), '-');
102 std::string
bt::textPropertyToString(::Display
*display
,
103 ::XTextProperty
& text_prop
) {
106 if (text_prop
.value
&& text_prop
.nitems
> 0) {
107 if (text_prop
.encoding
== XA_STRING
) {
108 ret
= reinterpret_cast<char *>(text_prop
.value
);
110 text_prop
.nitems
= strlen(reinterpret_cast<char *>(text_prop
.value
));
114 if (XmbTextPropertyToTextList(display
, &text_prop
,
115 &list
, &num
) == Success
&&
118 XFreeStringList(list
);