]> git.saurik.com Git - wxWidgets.git/blame - src/x11/utils.cpp
don't wake up on Windows messages when waiting for thread termination in a console...
[wxWidgets.git] / src / x11 / utils.cpp
CommitLineData
83df96d6 1/////////////////////////////////////////////////////////////////////////////
521bf4ff 2// Name: src/x11/utils.cpp
83df96d6
JS
3// Purpose: Various utilities
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
83df96d6
JS
10/////////////////////////////////////////////////////////////////////////////
11
521bf4ff
WS
12// for compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#if defined(__BORLANDC__)
670f9935 16 #pragma hdrstop
521bf4ff
WS
17#endif
18
83df96d6
JS
19// ============================================================================
20// declarations
21// ============================================================================
22
23// ----------------------------------------------------------------------------
24// headers
25// ----------------------------------------------------------------------------
26
83df96d6 27#include "wx/utils.h"
670f9935
WS
28
29#ifndef WX_PRECOMP
30 #include "wx/app.h"
cdccdfab 31 #include "wx/window.h" // for wxTopLevelWindows
c8326d64 32 #include "wx/cursor.h"
246c5004 33 #include "wx/msgdlg.h"
670f9935
WS
34#endif
35
cd478a95 36#include "wx/apptrait.h"
83df96d6
JS
37
38#include <ctype.h>
39#include <stdarg.h>
40#include <dirent.h>
41#include <string.h>
42#include <sys/stat.h>
43#include <sys/types.h>
44#include <unistd.h>
45#include <sys/wait.h>
46#include <pwd.h>
47#include <errno.h>
2b5f62a0 48// #include <netdb.h>
83df96d6
JS
49#include <signal.h>
50
51#if (defined(__SUNCC__) || defined(__CLCC__))
52 #include <sysent.h>
53#endif
54
55#ifdef __VMS__
56#pragma message disable nosimpint
57#endif
bc797f4c 58
83df96d6
JS
59#include "wx/unix/execute.h"
60
bc797f4c 61#include "wx/x11/private.h"
256d631a 62
7266b672 63#include "X11/Xutil.h"
83df96d6 64
256d631a
JS
65#ifdef __VMS__
66#pragma message enable nosimpint
67#endif
68
83df96d6
JS
69// ----------------------------------------------------------------------------
70// async event processing
71// ----------------------------------------------------------------------------
72
73// Consume all events until no more left
74void wxFlushEvents()
75{
76 Display *display = (Display*) wxGetDisplay();
77
78 XSync (display, FALSE);
79
5e29f97a
JS
80 // TODO for X11
81 // ??
83df96d6
JS
82}
83
83df96d6
JS
84bool wxCheckForInterrupt(wxWindow *wnd)
85{
670f9935
WS
86 wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented."));
87 return false;
83df96d6
JS
88}
89
90// ----------------------------------------------------------------------------
91// wxExecute stuff
92// ----------------------------------------------------------------------------
83df96d6 93
bd00fe32
VZ
94WX_DECLARE_HASH_MAP( int, wxEndProcessData*, wxIntegerHash, wxIntegerEqual, wxProcMap );
95
96static wxProcMap *gs_procmap;
97
83df96d6
JS
98int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
99{
bd00fe32
VZ
100 if (!gs_procmap) gs_procmap = new wxProcMap();
101 (*gs_procmap)[fd] = proc_data;
102 return 1;
103}
104
105void wxCheckForFinishedChildren()
106{
107 wxProcMap::iterator it;
108 if (!gs_procmap) return;
109 if (gs_procmap->size() == 0) {
110 // Map empty, delete it.
111 delete gs_procmap;
112 gs_procmap = NULL;
43f51d80 113 return;
bd00fe32
VZ
114 }
115 for (it = gs_procmap->begin();it != gs_procmap->end(); ++it)
116 {
117 wxEndProcessData *proc_data = it->second;
118 int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
119 int status = 0;
120 // has the process really terminated?
121 int rc = waitpid(pid, &status, WNOHANG);
122 if (rc == 0)
123 continue; // no, it didn't exit yet, continue waiting
124
125 // set exit code to -1 if something bad happened
126 proc_data->exitcode = rc != -1 && WIFEXITED(status) ?
127 WEXITSTATUS(status) : -1;
128
129 // child exited, end waiting
130 close(it->first);
131
132 // don't call us again!
133 gs_procmap->erase(it->first);
134
135 wxHandleProcessTermination(proc_data);
136
137 // Iterator is invalid. Handle any further children in subsequent
138 // calls.
139 break;
140 }
707440dc 141}
83df96d6
JS
142
143// ----------------------------------------------------------------------------
144// misc
145// ----------------------------------------------------------------------------
146
147// Emit a beeeeeep
189d1ae7
SN
148#ifndef __EMX__
149// on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
83df96d6
JS
150void wxBell()
151{
152 // Use current setting for the bell
153 XBell ((Display*) wxGetDisplay(), 0);
154}
189d1ae7 155#endif
83df96d6 156
8bb6b2c0 157wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
83df96d6 158{
8bb6b2c0
VZ
159 // get X protocol version
160 Display *display = wxGlobalDisplay();
161 if (display)
162 {
163 if ( verMaj )
164 *verMaj = ProtocolVersion (display);
165 if ( verMin )
166 *verMin = ProtocolRevision (display);
167 }
168
169 return wxPORT_X11;
83df96d6
JS
170}
171
8bb6b2c0 172
83df96d6
JS
173// ----------------------------------------------------------------------------
174// display info
175// ----------------------------------------------------------------------------
176
177void wxGetMousePosition( int* x, int* y )
178{
461e93f9
JS
179#if wxUSE_NANOX
180 // TODO
181 *x = 0;
182 *y = 0;
183#else
83df96d6
JS
184 XMotionEvent xev;
185 Window root, child;
186 XQueryPointer((Display*) wxGetDisplay(),
187 DefaultRootWindow((Display*) wxGetDisplay()),
188 &root, &child,
189 &(xev.x_root), &(xev.y_root),
190 &(xev.x), &(xev.y),
191 &(xev.state));
192 *x = xev.x_root;
193 *y = xev.y_root;
461e93f9 194#endif
83df96d6
JS
195};
196
670f9935 197// Return true if we have a colour display
83df96d6
JS
198bool wxColourDisplay()
199{
200 return wxDisplayDepth() > 1;
201}
202
203// Returns depth of screen
204int wxDisplayDepth()
205{
206 Display *dpy = (Display*) wxGetDisplay();
207
208 return DefaultDepth (dpy, DefaultScreen (dpy));
209}
210
211// Get size of display
212void wxDisplaySize(int *width, int *height)
213{
214 Display *dpy = (Display*) wxGetDisplay();
215
216 if ( width )
217 *width = DisplayWidth (dpy, DefaultScreen (dpy));
218 if ( height )
219 *height = DisplayHeight (dpy, DefaultScreen (dpy));
220}
221
222void wxDisplaySizeMM(int *width, int *height)
223{
224 Display *dpy = (Display*) wxGetDisplay();
225
226 if ( width )
227 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
228 if ( height )
229 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
230}
231
232void wxClientDisplayRect(int *x, int *y, int *width, int *height)
233{
234 // This is supposed to return desktop dimensions minus any window
235 // manager panels, menus, taskbars, etc. If there is a way to do that
236 // for this platform please fix this function, otherwise it defaults
237 // to the entire desktop.
238 if (x) *x = 0;
239 if (y) *y = 0;
240 wxDisplaySize(width, height);
241}
242
b886fae6
VZ
243wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
244{
245 return wxGenericFindWindowAtPoint(pt);
246}
247
83df96d6 248
5e29f97a 249// Configurable display in wxX11 and wxMotif
b886fae6 250static Display *gs_currentDisplay = NULL;
83df96d6
JS
251static wxString gs_displayName;
252
253WXDisplay *wxGetDisplay()
254{
b886fae6 255 return (WXDisplay *)gs_currentDisplay;
83df96d6
JS
256}
257
b886fae6
VZ
258// close the current display
259void wxCloseDisplay()
83df96d6 260{
b886fae6 261 if ( gs_currentDisplay )
83df96d6 262 {
b886fae6
VZ
263 if ( XCloseDisplay(gs_currentDisplay) != 0 )
264 {
265 wxLogWarning(_("Failed to close the display \"%s\""),
266 gs_displayName.c_str());
267 }
83df96d6 268
b886fae6
VZ
269 gs_currentDisplay = NULL;
270 gs_displayName.clear();
83df96d6 271 }
b886fae6 272}
83df96d6 273
b886fae6
VZ
274bool wxSetDisplay(const wxString& displayName)
275{
0e4f7936
VZ
276 Display *dpy = XOpenDisplay
277 (
278 displayName.empty() ? NULL
279 : (const char *)displayName.mb_str()
280 );
b886fae6
VZ
281
282 if ( !dpy )
283 {
284 wxLogError(_("Failed to open display \"%s\"."), displayName.c_str());
285 return false;
83df96d6 286 }
b886fae6
VZ
287
288 wxCloseDisplay();
289
290 gs_currentDisplay = dpy;
291 gs_displayName = displayName;
292
293 return true;
83df96d6
JS
294}
295
296wxString wxGetDisplayName()
297{
298 return gs_displayName;
299}
300
b886fae6
VZ
301#include "wx/module.h"
302
303// the module responsible for closing the X11 display at the program end
304class wxX11DisplayModule : public wxModule
83df96d6 305{
b886fae6
VZ
306public:
307 virtual bool OnInit() { return true; }
308 virtual void OnExit() { wxCloseDisplay(); }
309
310private:
311 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule)
312};
313
314IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule, wxModule)
83df96d6 315
83df96d6
JS
316// ----------------------------------------------------------------------------
317// Some colour manipulation routines
318// ----------------------------------------------------------------------------
319
320void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
321{
322 int h = hsv->h;
323 int s = hsv->s;
324 int v = hsv->v;
325 int r = 0, g = 0, b = 0;
326 int i, f;
327 int p, q, t;
328 s = (s * wxMAX_RGB) / wxMAX_SV;
329 v = (v * wxMAX_RGB) / wxMAX_SV;
330 if (h == 360) h = 0;
331 if (s == 0) { h = 0; r = g = b = v; }
332 i = h / 60;
333 f = h % 60;
334 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
335 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
336 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
337 switch (i)
338 {
339 case 0: r = v, g = t, b = p; break;
340 case 1: r = q, g = v, b = p; break;
341 case 2: r = p, g = v, b = t; break;
342 case 3: r = p, g = q, b = v; break;
343 case 4: r = t, g = p, b = v; break;
344 case 5: r = v, g = p, b = q; break;
345 }
346 rgb->red = r << 8;
347 rgb->green = g << 8;
348 rgb->blue = b << 8;
349}
350
351void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
352{
353 int r = rgb->red >> 8;
354 int g = rgb->green >> 8;
355 int b = rgb->blue >> 8;
356 int maxv = wxMax3(r, g, b);
357 int minv = wxMin3(r, g, b);
358 int h = 0, s, v;
359 v = maxv;
360 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
361 else s = 0;
362 if (s == 0) h = 0;
363 else
364 {
365 int rc, gc, bc, hex = 0;
366 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
367 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
368 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
369 if (r == maxv) { h = bc - gc, hex = 0; }
370 else if (g == maxv) { h = rc - bc, hex = 2; }
371 else if (b == maxv) { h = gc - rc, hex = 4; }
372 h = hex * 60 + (h * 60 / wxMAX_RGB);
373 if (h < 0) h += 360;
374 }
375 hsv->h = h;
376 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
377 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
378}
379
380void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
381{
461e93f9 382#if !wxUSE_NANOX
83df96d6
JS
383 int llp;
384
385 int screen = DefaultScreen(d);
386 int num_colors = DisplayCells(d,screen);
387
388 XColor *color_defs = new XColor[num_colors];
389 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
390 XQueryColors(d,cmp,color_defs,num_colors);
391
392 wxHSV hsv_defs, hsv;
393 wxXColorToHSV(&hsv,xc);
394
395 int diff, min_diff = 0, pixel = 0;
396
397 for(llp = 0;llp < num_colors;llp++)
398 {
399 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
400 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
401 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
402 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
403 if (llp == 0) min_diff = diff;
404 if (min_diff > diff) { min_diff = diff; pixel = llp; }
405 if (min_diff == 0) break;
406 }
407
408 xc -> red = color_defs[pixel].red;
409 xc -> green = color_defs[pixel].green;
410 xc -> blue = color_defs[pixel].blue;
411 xc -> flags = DoRed | DoGreen | DoBlue;
412
413/* FIXME, TODO
414 if (!XAllocColor(d,cmp,xc))
415 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
416*/
417
418 delete[] color_defs;
461e93f9 419#endif
83df96d6
JS
420}
421
422void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
423{
424 if (!XAllocColor(d,cmp,xc))
425 {
426 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
427 wxAllocNearestColor(d,cmp,xc);
428 }
429}
430
45ff6421
JS
431#ifdef __WXDEBUG__
432wxString wxGetXEventName(XEvent& event)
433{
461e93f9
JS
434#if wxUSE_NANOX
435 wxString str(wxT("(some event)"));
436 return str;
437#else
45ff6421 438 int type = event.xany.type;
521bf4ff
WS
439 static char* event_name[] = {
440 "", "unknown(-)", // 0-1
441 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
442 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
443 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
444 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
445 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
446 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
447 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
448 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
449 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
450 "ClientMessage", "MappingNotify", // 33-34
451 "unknown(+)"}; // 35
452 type = wxMin(35, type); type = wxMax(1, type);
54385bdb 453 return wxString::FromAscii(event_name[type]);
461e93f9
JS
454#endif
455}
45ff6421
JS
456#endif
457
3a0b23eb
JS
458bool wxWindowIsVisible(Window win)
459{
460 XWindowAttributes wa;
461 XGetWindowAttributes(wxGlobalDisplay(), win, &wa);
462
463 return (wa.map_state == IsViewable);
464}