1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Util.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000, 2002 Brad Hughes (bhughes@tcac.net)
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
29 #include <X11/Xatom.h>
37 #ifdef TIME_WITH_SYS_TIME
38 # include <sys/time.h>
40 #else // !TIME_WITH_SYS_TIME
41 # ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
43 # else // !HAVE_SYS_TIME_H
45 # endif // HAVE_SYS_TIME_H
46 #endif // TIME_WITH_SYS_TIME
49 #endif // HAVE_UNISTD_H
50 #if defined(HAVE_PROCESS_H) && defined(__EMX__)
52 #endif // HAVE_PROCESS_H __EMX__
64 void Rect::setX(int x) {
70 void Rect::setY(int y)
77 void Rect::setPos(int x, int y) {
85 void Rect::setWidth(unsigned int w) {
90 void Rect::setHeight(unsigned int h) {
95 void Rect::setSize(unsigned int w, unsigned int h) {
101 void Rect::setRect(int x, int y, unsigned int w, unsigned int h) {
102 *this = Rect(x, y, w, h);
106 void Rect::setCoords(int l, int t, int r, int b) {
114 Rect Rect::operator|(const Rect &a) const {
117 b._x1 = std::min(_x1, a._x1);
118 b._y1 = std::min(_y1, a._y1);
119 b._x2 = std::max(_x2, a._x2);
120 b._y2 = std::max(_y2, a._y2);
126 Rect Rect::operator&(const Rect &a) const {
129 b._x1 = std::max(_x1, a._x1);
130 b._y1 = std::max(_y1, a._y1);
131 b._x2 = std::min(_x2, a._x2);
132 b._y2 = std::min(_y2, a._y2);
138 bool Rect::intersects(const Rect &a) const {
139 return std::max(_x1, a._x1) <= std::min(_x2, a._x2) &&
140 std::max(_y1, a._y1) <= std::min(_y2, a._y2);
144 bool Rect::contains(int x, int y) const {
145 return x >= _x1 && x <= _x2 &&
146 y >= _y1 && y <= _y2;
150 bool Rect::contains(const Rect& a) const {
151 return a._x1 >= _x1 && a._x2 <= _x2 &&
152 a._y1 >= _y1 && a._y2 <= _y2;
156 string expandTilde(const string& s) {
157 if (s[0] != '~') return s;
159 const char* const home = getenv("HOME");
160 if (home == NULL) return s;
162 return string(home + s.substr(s.find('/')));
166 void bexec(const string& command, const string& displaystring) {
170 int ret = putenv(const_cast<char *>(displaystring.c_str()));
172 ret = execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL);
176 spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", command.c_str(), NULL);
181 #ifndef HAVE_BASENAME
182 string basename (const string& path) {
183 string::size_type slash = path.rfind('/');
184 if (slash == string::npos)
186 return path.substr(slash+1);
188 #endif // HAVE_BASENAME
191 string textPropertyToString(Display *display, XTextProperty& text_prop) {
194 if (text_prop.value && text_prop.nitems > 0) {
195 if (text_prop.encoding == XA_STRING) {
196 ret = (char *) text_prop.value;
198 text_prop.nitems = strlen((char *) text_prop.value);
202 if (XmbTextPropertyToTextList(display, &text_prop,
203 &list, &num) == Success &&
206 XFreeStringList(list);
215 timeval normalizeTimeval(const timeval &tm) {
218 while (ret.tv_usec < 0) {
219 if (ret.tv_sec > 0) {
221 ret.tv_usec += 1000000;
227 if (ret.tv_usec >= 1000000) {
228 ret.tv_sec += ret.tv_usec / 1000000;
229 ret.tv_usec %= 1000000;
232 if (ret.tv_sec < 0) ret.tv_sec = 0;
238 string itostring(unsigned long i) {
243 for (; i > 0; i /= 10)
244 tmp.insert(tmp.begin(), "0123456789"[i%10]);
249 string itostring(long i) {
250 std::string tmp = itostring( (unsigned long) std::abs(i));
252 tmp.insert(tmp.begin(), '-');