allow the user to override the rootCommand from their ~/.blackboxrc
[blackbox.git] / lib / Util.cc
blob305494f00672c4fc197d8bd653ccf3951c6b0941
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>
6 //
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
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
29 #include "Util.hh"
31 #include <algorithm>
33 #include <X11/Xatom.h>
35 #include <assert.h>
36 #if defined(__EMX__)
37 # include <process.h>
38 #endif // __EMX__
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
44 std::string bt::basename(const std::string& path) {
45 std::string::size_type slash = path.rfind('/');
46 if (slash == std::string::npos)
47 return path;
48 return path.substr(slash+1);
52 std::string bt::expandTilde(const std::string& s) {
53 if (s[0] != '~')
54 return s;
55 const char* const home = getenv("HOME");
56 if (home == NULL)
57 return s;
58 return std::string(home + s.substr(s.find('/')));
62 void bt::bexec(const std::string& command, const std::string& displaystring) {
63 #ifndef __EMX__
64 if (!fork()) {
65 #ifndef __QNXTO__ // apparently, setsid interferes with signals on QNX
66 setsid();
67 #endif
68 int ret = putenv(const_cast<char *>(displaystring.c_str()));
69 assert(ret != -1);
70 std::string cmd = "exec ";
71 cmd += command;
72 ret = execl("/bin/sh", "/bin/sh", "-c", cmd.c_str(), NULL);
73 exit(ret);
75 #else // __EMX__
76 spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", command.c_str(), NULL);
77 #endif // !__EMX__
81 std::string bt::itostring(unsigned long i) {
82 if (i == 0)
83 return std::string("0");
85 const char nums[] = "0123456789";
87 std::string tmp;
88 for (; i > 0; i /= 10)
89 tmp.insert(tmp.begin(), nums[i%10]);
90 return tmp;
94 std::string bt::itostring(long i) {
95 std::string tmp = bt::itostring(static_cast<unsigned long>(abs(i)));
96 if (i < 0)
97 tmp.insert(tmp.begin(), '-');
98 return tmp;
102 std::string bt::textPropertyToString(::Display *display,
103 ::XTextProperty& text_prop) {
104 std::string ret;
106 if (text_prop.value && text_prop.nitems > 0) {
107 if (text_prop.encoding == XA_STRING) {
108 ret = reinterpret_cast<char *>(text_prop.value);
109 } else {
110 text_prop.nitems = strlen(reinterpret_cast<char *>(text_prop.value));
112 char **list;
113 int num;
114 if (XmbTextPropertyToTextList(display, &text_prop,
115 &list, &num) == Success &&
116 num > 0 && *list) {
117 ret = *list;
118 XFreeStringList(list);
123 return ret;