]> Dogcows Code - chaz/openbox/blob - src/Util.cc
xinerama support for window placement
[chaz/openbox] / 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)
5 //
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:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
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.
23
24 #ifdef HAVE_CONFIG_H
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 extern "C" {
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35 #ifdef TIME_WITH_SYS_TIME
36 # include <sys/time.h>
37 # include <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
42 # include <time.h>
43 # endif // HAVE_SYS_TIME_H
44 #endif // TIME_WITH_SYS_TIME
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif // HAVE_UNISTD_H
48 #if defined(HAVE_PROCESS_H) && defined(__EMX__)
49 # include <process.h>
50 #endif // HAVE_PROCESS_H __EMX__
51 }
52
53 #include <X11/Xatom.h>
54
55 #include <assert.h>
56
57 #include <algorithm>
58
59 #include "Util.hh"
60
61 using std::string;
62
63
64 void Rect::setX(int __x) {
65 _x2 += __x - _x1;
66 _x1 = __x;
67 }
68
69
70 void Rect::setY(int __y)
71 {
72 _y2 += __y - _y1;
73 _y1 = __y;
74 }
75
76
77 void Rect::setPos(int __x, int __y) {
78 _x2 += __x - _x1;
79 _x1 = __x;
80 _y2 += __y - _y1;
81 _y1 = __y;
82 }
83
84
85 void Rect::setWidth(unsigned int __w) {
86 _x2 = __w + _x1 - 1;
87 }
88
89
90 void Rect::setHeight(unsigned int __h) {
91 _y2 = __h + _y1 - 1;
92 }
93
94
95 void Rect::setSize(unsigned int __w, unsigned int __h) {
96 _x2 = __w + _x1 - 1;
97 _y2 = __h + _y1 - 1;
98 }
99
100
101 void Rect::setRect(int __x, int __y, unsigned int __w, unsigned int __h) {
102 *this = Rect(__x, __y, __w, __h);
103 }
104
105
106 void Rect::setCoords(int __l, int __t, int __r, int __b) {
107 _x1 = __l;
108 _y1 = __t;
109 _x2 = __r;
110 _y2 = __b;
111 }
112
113
114 Rect Rect::operator|(const Rect &a) const {
115 Rect b;
116
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);
121
122 return b;
123 }
124
125
126 Rect Rect::operator&(const Rect &a) const {
127 Rect b;
128
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);
133
134 return b;
135 }
136
137
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);
141 }
142
143
144 bool Rect::contains(int __x, int __y) const {
145 return __x >= _x1 && __x <= _x2 &&
146 __y >= _y1 && __y <= _y2;
147 }
148
149
150 bool Rect::contains(const Rect& a) const {
151 return a._x1 >= _x1 && a._x2 <= _x2 &&
152 a._y1 >= _y1 && a._y2 <= _y2;
153 }
154
155
156 string expandTilde(const string& s) {
157 if (s[0] != '~') return s;
158
159 const char* const home = getenv("HOME");
160 if (home == NULL) return s;
161
162 return string(home + s.substr(s.find('/')));
163 }
164
165
166 void bexec(const string& command, const string& displaystring) {
167 #ifndef __EMX__
168 if (! fork()) {
169 setsid();
170 int ret = putenv(const_cast<char *>(displaystring.c_str()));
171 assert(ret != -1);
172 string cmd = "exec ";
173 cmd += command;
174 execl("/bin/sh", "/bin/sh", "-c", cmd.c_str(), NULL);
175 exit(0);
176 }
177 #else // __EMX__
178 spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", command, NULL);
179 #endif // !__EMX__
180 }
181
182
183 #ifndef HAVE_BASENAME
184 string basename (const string& path) {
185 string::size_type slash = path.rfind('/');
186 if (slash == string::npos)
187 return path;
188 return path.substr(slash+1);
189 }
190 #endif // HAVE_BASENAME
191
192
193 string textPropertyToString(Display *display, XTextProperty& text_prop) {
194 string ret;
195
196 if (text_prop.value && text_prop.nitems > 0) {
197 if (text_prop.encoding == XA_STRING) {
198 ret = (char *) text_prop.value;
199 } else {
200 text_prop.nitems = strlen((char *) text_prop.value);
201
202 char **list;
203 int num;
204 if (XmbTextPropertyToTextList(display, &text_prop,
205 &list, &num) == Success &&
206 num > 0 && *list) {
207 ret = *list;
208 XFreeStringList(list);
209 }
210 }
211 }
212
213 return ret;
214 }
215
216
217 timeval normalizeTimeval(const timeval &tm) {
218 timeval ret = tm;
219
220 while (ret.tv_usec < 0) {
221 if (ret.tv_sec > 0) {
222 --ret.tv_sec;
223 ret.tv_usec += 1000000;
224 } else {
225 ret.tv_usec = 0;
226 }
227 }
228
229 if (ret.tv_usec >= 1000000) {
230 ret.tv_sec += ret.tv_usec / 1000000;
231 ret.tv_usec %= 1000000;
232 }
233
234 if (ret.tv_sec < 0) ret.tv_sec = 0;
235
236 return ret;
237 }
238
239
240 string itostring(unsigned long i) {
241 if (i == 0)
242 return string("0");
243
244 string tmp;
245 for (; i > 0; i /= 10)
246 tmp.insert(tmp.begin(), "0123456789"[i%10]);
247 return tmp;
248 }
249
250
251 string itostring(long i) {
252 if (i < 0) {
253 std::string tmp = itostring( (unsigned long) -i);
254 tmp.insert(tmp.begin(), '-');
255 return tmp;
256 } else
257 return itostring( (unsigned long) i);
258 }
This page took 0.044925 seconds and 5 git commands to generate.