]> git.saurik.com Git - wxWidgets.git/blame - src/x11/utils.cpp
include wrapwin.h to get HMONITOR and friends declarations
[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;
113 }
114 for (it = gs_procmap->begin();it != gs_procmap->end(); ++it)
115 {
116 wxEndProcessData *proc_data = it->second;
117 int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
118 int status = 0;
119 // has the process really terminated?
120 int rc = waitpid(pid, &status, WNOHANG);
121 if (rc == 0)
122 continue; // no, it didn't exit yet, continue waiting
123
124 // set exit code to -1 if something bad happened
125 proc_data->exitcode = rc != -1 && WIFEXITED(status) ?
126 WEXITSTATUS(status) : -1;
127
128 // child exited, end waiting
129 close(it->first);
130
131 // don't call us again!
132 gs_procmap->erase(it->first);
133
134 wxHandleProcessTermination(proc_data);
135
136 // Iterator is invalid. Handle any further children in subsequent
137 // calls.
138 break;
139 }
707440dc 140}
83df96d6
JS
141
142// ----------------------------------------------------------------------------
143// misc
144// ----------------------------------------------------------------------------
145
146// Emit a beeeeeep
189d1ae7
SN
147#ifndef __EMX__
148// on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
83df96d6
JS
149void wxBell()
150{
151 // Use current setting for the bell
152 XBell ((Display*) wxGetDisplay(), 0);
153}
189d1ae7 154#endif
83df96d6 155
8bb6b2c0 156wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
83df96d6 157{
8bb6b2c0
VZ
158 // get X protocol version
159 Display *display = wxGlobalDisplay();
160 if (display)
161 {
162 if ( verMaj )
163 *verMaj = ProtocolVersion (display);
164 if ( verMin )
165 *verMin = ProtocolRevision (display);
166 }
167
168 return wxPORT_X11;
83df96d6
JS
169}
170
8bb6b2c0 171
83df96d6
JS
172// ----------------------------------------------------------------------------
173// display info
174// ----------------------------------------------------------------------------
175
176void wxGetMousePosition( int* x, int* y )
177{
461e93f9
JS
178#if wxUSE_NANOX
179 // TODO
180 *x = 0;
181 *y = 0;
182#else
83df96d6
JS
183 XMotionEvent xev;
184 Window root, child;
185 XQueryPointer((Display*) wxGetDisplay(),
186 DefaultRootWindow((Display*) wxGetDisplay()),
187 &root, &child,
188 &(xev.x_root), &(xev.y_root),
189 &(xev.x), &(xev.y),
190 &(xev.state));
191 *x = xev.x_root;
192 *y = xev.y_root;
461e93f9 193#endif
83df96d6
JS
194};
195
670f9935 196// Return true if we have a colour display
83df96d6
JS
197bool wxColourDisplay()
198{
199 return wxDisplayDepth() > 1;
200}
201
202// Returns depth of screen
203int wxDisplayDepth()
204{
205 Display *dpy = (Display*) wxGetDisplay();
206
207 return DefaultDepth (dpy, DefaultScreen (dpy));
208}
209
210// Get size of display
211void wxDisplaySize(int *width, int *height)
212{
213 Display *dpy = (Display*) wxGetDisplay();
214
215 if ( width )
216 *width = DisplayWidth (dpy, DefaultScreen (dpy));
217 if ( height )
218 *height = DisplayHeight (dpy, DefaultScreen (dpy));
219}
220
221void wxDisplaySizeMM(int *width, int *height)
222{
223 Display *dpy = (Display*) wxGetDisplay();
224
225 if ( width )
226 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
227 if ( height )
228 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
229}
230
231void wxClientDisplayRect(int *x, int *y, int *width, int *height)
232{
233 // This is supposed to return desktop dimensions minus any window
234 // manager panels, menus, taskbars, etc. If there is a way to do that
235 // for this platform please fix this function, otherwise it defaults
236 // to the entire desktop.
237 if (x) *x = 0;
238 if (y) *y = 0;
239 wxDisplaySize(width, height);
240}
241
b886fae6
VZ
242wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
243{
244 return wxGenericFindWindowAtPoint(pt);
245}
246
83df96d6 247
5e29f97a 248// Configurable display in wxX11 and wxMotif
b886fae6 249static Display *gs_currentDisplay = NULL;
83df96d6
JS
250static wxString gs_displayName;
251
252WXDisplay *wxGetDisplay()
253{
b886fae6 254 return (WXDisplay *)gs_currentDisplay;
83df96d6
JS
255}
256
b886fae6
VZ
257// close the current display
258void wxCloseDisplay()
83df96d6 259{
b886fae6 260 if ( gs_currentDisplay )
83df96d6 261 {
b886fae6
VZ
262 if ( XCloseDisplay(gs_currentDisplay) != 0 )
263 {
264 wxLogWarning(_("Failed to close the display \"%s\""),
265 gs_displayName.c_str());
266 }
83df96d6 267
b886fae6
VZ
268 gs_currentDisplay = NULL;
269 gs_displayName.clear();
83df96d6 270 }
b886fae6 271}
83df96d6 272
b886fae6
VZ
273bool wxSetDisplay(const wxString& displayName)
274{
0e4f7936
VZ
275 Display *dpy = XOpenDisplay
276 (
277 displayName.empty() ? NULL
278 : (const char *)displayName.mb_str()
279 );
b886fae6
VZ
280
281 if ( !dpy )
282 {
283 wxLogError(_("Failed to open display \"%s\"."), displayName.c_str());
284 return false;
83df96d6 285 }
b886fae6
VZ
286
287 wxCloseDisplay();
288
289 gs_currentDisplay = dpy;
290 gs_displayName = displayName;
291
292 return true;
83df96d6
JS
293}
294
295wxString wxGetDisplayName()
296{
297 return gs_displayName;
298}
299
b886fae6
VZ
300#include "wx/module.h"
301
302// the module responsible for closing the X11 display at the program end
303class wxX11DisplayModule : public wxModule
83df96d6 304{
b886fae6
VZ
305public:
306 virtual bool OnInit() { return true; }
307 virtual void OnExit() { wxCloseDisplay(); }
308
309private:
310 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule)
311};
312
313IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule, wxModule)
83df96d6 314
83df96d6
JS
315// ----------------------------------------------------------------------------
316// Some colour manipulation routines
317// ----------------------------------------------------------------------------
318
319void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
320{
321 int h = hsv->h;
322 int s = hsv->s;
323 int v = hsv->v;
324 int r = 0, g = 0, b = 0;
325 int i, f;
326 int p, q, t;
327 s = (s * wxMAX_RGB) / wxMAX_SV;
328 v = (v * wxMAX_RGB) / wxMAX_SV;
329 if (h == 360) h = 0;
330 if (s == 0) { h = 0; r = g = b = v; }
331 i = h / 60;
332 f = h % 60;
333 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
334 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
335 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
336 switch (i)
337 {
338 case 0: r = v, g = t, b = p; break;
339 case 1: r = q, g = v, b = p; break;
340 case 2: r = p, g = v, b = t; break;
341 case 3: r = p, g = q, b = v; break;
342 case 4: r = t, g = p, b = v; break;
343 case 5: r = v, g = p, b = q; break;
344 }
345 rgb->red = r << 8;
346 rgb->green = g << 8;
347 rgb->blue = b << 8;
348}
349
350void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
351{
352 int r = rgb->red >> 8;
353 int g = rgb->green >> 8;
354 int b = rgb->blue >> 8;
355 int maxv = wxMax3(r, g, b);
356 int minv = wxMin3(r, g, b);
357 int h = 0, s, v;
358 v = maxv;
359 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
360 else s = 0;
361 if (s == 0) h = 0;
362 else
363 {
364 int rc, gc, bc, hex = 0;
365 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
366 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
367 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
368 if (r == maxv) { h = bc - gc, hex = 0; }
369 else if (g == maxv) { h = rc - bc, hex = 2; }
370 else if (b == maxv) { h = gc - rc, hex = 4; }
371 h = hex * 60 + (h * 60 / wxMAX_RGB);
372 if (h < 0) h += 360;
373 }
374 hsv->h = h;
375 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
376 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
377}
378
379void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
380{
461e93f9 381#if !wxUSE_NANOX
83df96d6
JS
382 int llp;
383
384 int screen = DefaultScreen(d);
385 int num_colors = DisplayCells(d,screen);
386
387 XColor *color_defs = new XColor[num_colors];
388 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
389 XQueryColors(d,cmp,color_defs,num_colors);
390
391 wxHSV hsv_defs, hsv;
392 wxXColorToHSV(&hsv,xc);
393
394 int diff, min_diff = 0, pixel = 0;
395
396 for(llp = 0;llp < num_colors;llp++)
397 {
398 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
399 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
400 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
401 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
402 if (llp == 0) min_diff = diff;
403 if (min_diff > diff) { min_diff = diff; pixel = llp; }
404 if (min_diff == 0) break;
405 }
406
407 xc -> red = color_defs[pixel].red;
408 xc -> green = color_defs[pixel].green;
409 xc -> blue = color_defs[pixel].blue;
410 xc -> flags = DoRed | DoGreen | DoBlue;
411
412/* FIXME, TODO
413 if (!XAllocColor(d,cmp,xc))
414 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
415*/
416
417 delete[] color_defs;
461e93f9 418#endif
83df96d6
JS
419}
420
421void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
422{
423 if (!XAllocColor(d,cmp,xc))
424 {
425 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
426 wxAllocNearestColor(d,cmp,xc);
427 }
428}
429
45ff6421
JS
430#ifdef __WXDEBUG__
431wxString wxGetXEventName(XEvent& event)
432{
461e93f9
JS
433#if wxUSE_NANOX
434 wxString str(wxT("(some event)"));
435 return str;
436#else
45ff6421 437 int type = event.xany.type;
521bf4ff
WS
438 static char* event_name[] = {
439 "", "unknown(-)", // 0-1
440 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
441 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
442 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
443 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
444 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
445 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
446 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
447 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
448 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
449 "ClientMessage", "MappingNotify", // 33-34
450 "unknown(+)"}; // 35
451 type = wxMin(35, type); type = wxMax(1, type);
54385bdb 452 return wxString::FromAscii(event_name[type]);
461e93f9
JS
453#endif
454}
45ff6421
JS
455#endif
456
3a0b23eb
JS
457bool wxWindowIsVisible(Window win)
458{
459 XWindowAttributes wa;
460 XGetWindowAttributes(wxGlobalDisplay(), win, &wa);
461
462 return (wa.map_state == IsViewable);
463}