]> Dogcows Code - chaz/openbox/blob - src/util.cc
83b076771d0a4dd6b1a7811a9cffc58c8bb07793
[chaz/openbox] / src / util.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 extern "C" {
8 #include <X11/Xatom.h>
9
10 #ifdef HAVE_STRING_H
11 #include <string.h>
12 #endif
13 #ifdef HAVE_STDLIB_H
14 #include <stdlib.h>
15 #endif
16 #ifdef TIME_WITH_SYS_TIME
17 # include <sys/time.h>
18 # include <time.h>
19 #else // !TIME_WITH_SYS_TIME
20 # ifdef HAVE_SYS_TIME_H
21 # include <sys/time.h>
22 # else // !HAVE_SYS_TIME_H
23 # include <time.h>
24 # endif // HAVE_SYS_TIME_H
25 #endif // TIME_WITH_SYS_TIME
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif // HAVE_UNISTD_H
29 #if defined(HAVE_PROCESS_H) && defined(__EMX__)
30 # include <process.h>
31 #endif // HAVE_PROCESS_H __EMX__
32
33 #include <assert.h>
34 }
35
36 #include <algorithm>
37
38 #include "util.hh"
39
40 using std::string;
41
42
43 void Rect::setX(int x) {
44 _x2 += x - _x1;
45 _x1 = x;
46 }
47
48
49 void Rect::setY(int y)
50 {
51 _y2 += y - _y1;
52 _y1 = y;
53 }
54
55
56 void Rect::setPos(int x, int y) {
57 _x2 += x - _x1;
58 _x1 = x;
59 _y2 += y - _y1;
60 _y1 = y;
61 }
62
63
64 void Rect::setWidth(unsigned int w) {
65 _x2 = w + _x1 - 1;
66 }
67
68
69 void Rect::setHeight(unsigned int h) {
70 _y2 = h + _y1 - 1;
71 }
72
73
74 void Rect::setSize(unsigned int w, unsigned int h) {
75 _x2 = w + _x1 - 1;
76 _y2 = h + _y1 - 1;
77 }
78
79
80 void Rect::setRect(int x, int y, unsigned int w, unsigned int h) {
81 *this = Rect(x, y, w, h);
82 }
83
84
85 void Rect::setCoords(int l, int t, int r, int b) {
86 _x1 = l;
87 _y1 = t;
88 _x2 = r;
89 _y2 = b;
90 }
91
92
93 Rect Rect::operator|(const Rect &a) const {
94 Rect b;
95
96 b._x1 = std::min(_x1, a._x1);
97 b._y1 = std::min(_y1, a._y1);
98 b._x2 = std::max(_x2, a._x2);
99 b._y2 = std::max(_y2, a._y2);
100
101 return b;
102 }
103
104
105 Rect Rect::operator&(const Rect &a) const {
106 Rect b;
107
108 b._x1 = std::max(_x1, a._x1);
109 b._y1 = std::max(_y1, a._y1);
110 b._x2 = std::min(_x2, a._x2);
111 b._y2 = std::min(_y2, a._y2);
112
113 return b;
114 }
115
116
117 bool Rect::intersects(const Rect &a) const {
118 return std::max(_x1, a._x1) <= std::min(_x2, a._x2) &&
119 std::max(_y1, a._y1) <= std::min(_y2, a._y2);
120 }
121
122
123 bool Rect::contains(int x, int y) const {
124 return x >= _x1 && x <= _x2 &&
125 y >= _y1 && y <= _y2;
126 }
127
128
129 bool Rect::contains(const Rect& a) const {
130 return a._x1 >= _x1 && a._x2 <= _x2 &&
131 a._y1 >= _y1 && a._y2 <= _y2;
132 }
133
134
135 string expandTilde(const string& s) {
136 if (s[0] != '~') return s;
137
138 const char* const home = getenv("HOME");
139 if (home == NULL) return s;
140
141 return string(home + s.substr(s.find('/')));
142 }
143
144
145 void bexec(const string& command, const string& displaystring) {
146 #ifndef __EMX__
147 if (! fork()) {
148 setsid();
149 int ret = putenv(const_cast<char *>(displaystring.c_str()));
150 assert(ret != -1);
151 ret = execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL);
152 exit(ret);
153 }
154 #else // __EMX__
155 spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", command.c_str(), NULL);
156 #endif // !__EMX__
157 }
158
159
160 #ifndef HAVE_BASENAME
161 string basename (const string& path) {
162 string::size_type slash = path.rfind('/');
163 if (slash == string::npos)
164 return path;
165 return path.substr(slash+1);
166 }
167 #endif // HAVE_BASENAME
168
169
170 string textPropertyToString(Display *display, XTextProperty& text_prop) {
171 string ret;
172
173 if (text_prop.value && text_prop.nitems > 0) {
174 if (text_prop.encoding == XA_STRING) {
175 ret = (char *) text_prop.value;
176 } else {
177 text_prop.nitems = strlen((char *) text_prop.value);
178
179 char **list;
180 int num;
181 if (XmbTextPropertyToTextList(display, &text_prop,
182 &list, &num) == Success &&
183 num > 0 && *list) {
184 ret = *list;
185 XFreeStringList(list);
186 }
187 }
188 }
189
190 return ret;
191 }
192
193
194 timeval normalizeTimeval(const timeval &tm) {
195 timeval ret = tm;
196
197 while (ret.tv_usec < 0) {
198 if (ret.tv_sec > 0) {
199 --ret.tv_sec;
200 ret.tv_usec += 1000000;
201 } else {
202 ret.tv_usec = 0;
203 }
204 }
205
206 if (ret.tv_usec >= 1000000) {
207 ret.tv_sec += ret.tv_usec / 1000000;
208 ret.tv_usec %= 1000000;
209 }
210
211 if (ret.tv_sec < 0) ret.tv_sec = 0;
212
213 return ret;
214 }
215
216
217 string itostring(unsigned long i) {
218 if (i == 0)
219 return string("0");
220
221 string tmp;
222 for (; i > 0; i /= 10)
223 tmp.insert(tmp.begin(), "0123456789"[i%10]);
224 return tmp;
225 }
226
227
228 string itostring(long i) {
229 std::string tmp = itostring( (unsigned long) std::abs(i));
230 if (i < 0)
231 tmp.insert(tmp.begin(), '-');
232 return tmp;
233 }
This page took 0.04557 seconds and 3 git commands to generate.