]> icculus.org git repositories - mikachu/openbox.git/blob - src/Util.cc
use the raw screen size not the strut adjusted size to determine max_height and width...
[mikachu/openbox.git] / 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 <algorithm>
56
57 #include "Util.hh"
58
59 using std::string;
60
61
62 void Rect::setX(int __x) {
63   _x2 += __x - _x1;
64   _x1 = __x;
65 }
66
67
68 void Rect::setY(int __y)
69 {
70   _y2 += __y - _y1;
71   _y1 = __y;
72 }
73
74
75 void Rect::setPos(int __x, int __y) {
76   _x2 += __x - _x1;
77   _x1 = __x;
78   _y2 += __y - _y1;
79   _y1 = __y;
80 }
81
82
83 void Rect::setWidth(unsigned int __w) {
84   _x2 = __w + _x1 - 1;
85 }
86
87
88 void Rect::setHeight(unsigned int __h) {
89   _y2 = __h + _y1 - 1;
90 }
91
92
93 void Rect::setSize(unsigned int __w, unsigned int __h) {
94   _x2 = __w + _x1 - 1;
95   _y2 = __h + _y1 - 1;
96 }
97
98
99 void Rect::setRect(int __x, int __y, unsigned int __w, unsigned int __h) {
100   *this = Rect(__x, __y, __w, __h);
101 }
102
103
104 void Rect::setCoords(int __l, int __t, int __r, int __b) {
105   _x1 = __l;
106   _y1 = __t;
107   _x2 = __r;
108   _y2 = __b;
109 }
110
111
112 Rect Rect::operator|(const Rect &a) const {
113   Rect b;
114
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);
119
120   return b;
121 }
122
123
124 Rect Rect::operator&(const Rect &a) const {
125   Rect b;
126
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);
131
132   return b;
133 }
134
135
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);
139 }
140
141
142 string expandTilde(const string& s) {
143   if (s[0] != '~') return s;
144
145   const char* const home = getenv("HOME");
146   if (home == NULL) return s;
147
148   return string(home + s.substr(s.find('/')));
149 }
150
151
152 void bexec(const string& command, const string& displaystring) {
153 #ifndef    __EMX__
154   if (! fork()) {
155     setsid();
156     int ret = putenv(const_cast<char *>(displaystring.c_str()));
157     assert(ret != -1);
158     string cmd = "exec ";
159     cmd += command;
160     execl("/bin/sh", "/bin/sh", "-c", cmd.c_str(), NULL);
161     exit(0);
162   }
163 #else //   __EMX__
164   spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", command, NULL);
165 #endif // !__EMX__
166 }
167
168
169 #ifndef   HAVE_BASENAME
170 string basename (const string& path) {
171   string::size_type slash = path.rfind('/');
172   if (slash == string::npos)
173     return path;
174   return path.substr(slash+1);
175 }
176 #endif // HAVE_BASENAME
177
178
179 string textPropertyToString(Display *display, XTextProperty& text_prop) {
180   string ret;
181
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);
186
187       char **list;
188       int num;
189       if (XmbTextPropertyToTextList(display, &text_prop,
190                                     &list, &num) == Success &&
191           num > 0 && *list) {
192         ret = *list;
193         XFreeStringList(list);
194       }
195     }
196   }
197
198   return ret;
199 }
200
201
202 timeval normalizeTimeval(const timeval &tm) {
203   timeval ret = tm;
204
205   while (ret.tv_usec < 0) {
206     if (ret.tv_sec > 0) {
207       --ret.tv_sec;
208       ret.tv_usec += 1000000;
209     } else {
210       ret.tv_usec = 0;
211     }
212   }
213
214   if (ret.tv_usec >= 1000000) {
215     ret.tv_sec += ret.tv_usec / 1000000;
216     ret.tv_usec %= 1000000;
217   }
218
219   if (ret.tv_sec < 0) ret.tv_sec = 0;
220
221   return ret;
222 }
223
224
225 string itostring(unsigned long i) {
226   if (i == 0)
227     return string("0");
228   
229   string tmp;
230   for (; i > 0; i /= 10)
231     tmp.insert(tmp.begin(), "0123456789"[i%10]);
232   return tmp;
233 }
234
235
236 string itostring(long i) {
237   if (i < 0) {
238     std::string tmp = itostring( (unsigned long) -i);
239     tmp.insert(tmp.begin(), '-');
240     return tmp;
241   } else
242     return itostring( (unsigned long) i);
243 }