]>
Dogcows Code - chaz/openbox/blob - src/Util.cc
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
35 #ifdef TIME_WITH_SYS_TIME
36 # include <sys/time.h>
38 #else // !TIME_WITH_SYS_TIME
39 # ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
41 # else // !HAVE_SYS_TIME_H
43 # endif // HAVE_SYS_TIME_H
44 #endif // TIME_WITH_SYS_TIME
47 #endif // HAVE_UNISTD_H
48 #if defined(HAVE_PROCESS_H) && defined(__EMX__)
50 #endif // HAVE_PROCESS_H __EMX__
53 #include <X11/Xatom.h>
62 void Rect::setX(int __x
) {
68 void Rect::setY(int __y
)
75 void Rect::setPos(int __x
, int __y
) {
83 void Rect::setWidth(unsigned int __w
) {
88 void Rect::setHeight(unsigned int __h
) {
93 void Rect::setSize(unsigned int __w
, unsigned int __h
) {
99 void Rect::setRect(int __x
, int __y
, unsigned int __w
, unsigned int __h
) {
100 *this = Rect(__x
, __y
, __w
, __h
);
104 void Rect::setCoords(int __l
, int __t
, int __r
, int __b
) {
112 Rect
Rect::operator|(const Rect
&a
) const {
115 b
._x1
= std::min(_x1
, a
._x1
);
116 b
._y1
= std::min(_y1
, a
._y1
);
117 b
._x2
= std::max(_x2
, a
._x2
);
118 b
._y2
= std::max(_y2
, a
._y2
);
124 Rect
Rect::operator&(const Rect
&a
) const {
127 b
._x1
= std::max(_x1
, a
._x1
);
128 b
._y1
= std::max(_y1
, a
._y1
);
129 b
._x2
= std::min(_x2
, a
._x2
);
130 b
._y2
= std::min(_y2
, a
._y2
);
136 bool Rect::intersects(const Rect
&a
) const {
137 return std::max(_x1
, a
._x1
) <= std::min(_x2
, a
._x2
) &&
138 std::max(_y1
, a
._y1
) <= std::min(_y2
, a
._y2
);
142 string
expandTilde(const string
& s
) {
143 if (s
[0] != '~') return s
;
145 const char* const home
= getenv("HOME");
146 if (home
== NULL
) return s
;
148 return string(home
+ s
.substr(s
.find('/')));
152 void bexec(const string
& command
, const string
& displaystring
) {
156 int ret
= putenv(const_cast<char *>(displaystring
.c_str()));
158 string cmd
= "exec ";
160 execl("/bin/sh", "/bin/sh", "-c", cmd
.c_str(), NULL
);
164 spawnlp(P_NOWAIT
, "cmd.exe", "cmd.exe", "/c", command
, NULL
);
169 #ifndef HAVE_BASENAME
170 string
basename (const string
& path
) {
171 string::size_type slash
= path
.rfind('/');
172 if (slash
== string::npos
)
174 return path
.substr(slash
+1);
176 #endif // HAVE_BASENAME
179 string
textPropertyToString(Display
*display
, XTextProperty
& text_prop
) {
182 if (text_prop
.value
&& text_prop
.nitems
> 0) {
183 ret
= (char *) text_prop
.value
;
184 if (text_prop
.encoding
!= XA_STRING
) {
185 text_prop
.nitems
= strlen((char *) text_prop
.value
);
189 if (XmbTextPropertyToTextList(display
, &text_prop
,
190 &list
, &num
) == Success
&&
193 XFreeStringList(list
);
202 timeval
normalizeTimeval(const timeval
&tm
) {
205 while (ret
.tv_usec
< 0) {
206 if (ret
.tv_sec
> 0) {
208 ret
.tv_usec
+= 1000000;
214 if (ret
.tv_usec
>= 1000000) {
215 ret
.tv_sec
+= ret
.tv_usec
/ 1000000;
216 ret
.tv_usec
%= 1000000;
219 if (ret
.tv_sec
< 0) ret
.tv_sec
= 0;
This page took 0.044431 seconds and 4 git commands to generate.