]>
Commit | Line | Data |
---|---|---|
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 | |
c5453c79 | 9 | // (c) 2013 Rob Bresalier |
65571936 | 10 | // Licence: wxWindows licence |
83df96d6 JS |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
521bf4ff WS |
13 | // for compilers that support precompilation, includes "wx.h". |
14 | #include "wx/wxprec.h" | |
15 | ||
16 | #if defined(__BORLANDC__) | |
670f9935 | 17 | #pragma hdrstop |
521bf4ff WS |
18 | #endif |
19 | ||
c5453c79 VZ |
20 | #include "wx/private/eventloopsourcesmanager.h" |
21 | ||
83df96d6 JS |
22 | // ============================================================================ |
23 | // declarations | |
24 | // ============================================================================ | |
25 | ||
26 | // ---------------------------------------------------------------------------- | |
27 | // headers | |
28 | // ---------------------------------------------------------------------------- | |
29 | ||
83df96d6 | 30 | #include "wx/utils.h" |
670f9935 WS |
31 | |
32 | #ifndef WX_PRECOMP | |
33 | #include "wx/app.h" | |
cdccdfab | 34 | #include "wx/window.h" // for wxTopLevelWindows |
c8326d64 | 35 | #include "wx/cursor.h" |
246c5004 | 36 | #include "wx/msgdlg.h" |
670f9935 WS |
37 | #endif |
38 | ||
cd478a95 | 39 | #include "wx/apptrait.h" |
c2ca375c | 40 | #include "wx/generic/private/timer.h" |
b46b1d59 | 41 | #include "wx/evtloop.h" |
83df96d6 JS |
42 | |
43 | #include <ctype.h> | |
44 | #include <stdarg.h> | |
45 | #include <dirent.h> | |
46 | #include <string.h> | |
47 | #include <sys/stat.h> | |
48 | #include <sys/types.h> | |
49 | #include <unistd.h> | |
50 | #include <sys/wait.h> | |
51 | #include <pwd.h> | |
52 | #include <errno.h> | |
2b5f62a0 | 53 | // #include <netdb.h> |
83df96d6 JS |
54 | #include <signal.h> |
55 | ||
56 | #if (defined(__SUNCC__) || defined(__CLCC__)) | |
57 | #include <sysent.h> | |
58 | #endif | |
59 | ||
60 | #ifdef __VMS__ | |
61 | #pragma message disable nosimpint | |
62 | #endif | |
bc797f4c | 63 | |
bc797f4c | 64 | #include "wx/x11/private.h" |
256d631a | 65 | |
7266b672 | 66 | #include "X11/Xutil.h" |
83df96d6 | 67 | |
256d631a JS |
68 | #ifdef __VMS__ |
69 | #pragma message enable nosimpint | |
70 | #endif | |
71 | ||
83df96d6 JS |
72 | // ---------------------------------------------------------------------------- |
73 | // async event processing | |
74 | // ---------------------------------------------------------------------------- | |
75 | ||
76 | // Consume all events until no more left | |
77 | void wxFlushEvents() | |
78 | { | |
79 | Display *display = (Display*) wxGetDisplay(); | |
80 | ||
81 | XSync (display, FALSE); | |
82 | ||
5e29f97a JS |
83 | // TODO for X11 |
84 | // ?? | |
83df96d6 JS |
85 | } |
86 | ||
89954433 | 87 | bool wxCheckForInterrupt(wxWindow *WXUNUSED(wnd)) |
83df96d6 | 88 | { |
670f9935 WS |
89 | wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented.")); |
90 | return false; | |
83df96d6 JS |
91 | } |
92 | ||
83df96d6 JS |
93 | // ---------------------------------------------------------------------------- |
94 | // misc | |
95 | // ---------------------------------------------------------------------------- | |
96 | ||
97 | // Emit a beeeeeep | |
98 | void wxBell() | |
99 | { | |
100 | // Use current setting for the bell | |
101 | XBell ((Display*) wxGetDisplay(), 0); | |
102 | } | |
103 | ||
8bb6b2c0 | 104 | wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const |
83df96d6 | 105 | { |
8bb6b2c0 VZ |
106 | // get X protocol version |
107 | Display *display = wxGlobalDisplay(); | |
108 | if (display) | |
109 | { | |
110 | if ( verMaj ) | |
111 | *verMaj = ProtocolVersion (display); | |
112 | if ( verMin ) | |
113 | *verMin = ProtocolRevision (display); | |
114 | } | |
115 | ||
116 | return wxPORT_X11; | |
83df96d6 JS |
117 | } |
118 | ||
2ddff00c | 119 | wxEventLoopBase* wxGUIAppTraits::CreateEventLoop() |
b46b1d59 VZ |
120 | { |
121 | return new wxEventLoop; | |
122 | } | |
123 | ||
83df96d6 JS |
124 | // ---------------------------------------------------------------------------- |
125 | // display info | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
128 | void wxGetMousePosition( int* x, int* y ) | |
129 | { | |
461e93f9 JS |
130 | #if wxUSE_NANOX |
131 | // TODO | |
132 | *x = 0; | |
133 | *y = 0; | |
134 | #else | |
83df96d6 JS |
135 | XMotionEvent xev; |
136 | Window root, child; | |
137 | XQueryPointer((Display*) wxGetDisplay(), | |
138 | DefaultRootWindow((Display*) wxGetDisplay()), | |
139 | &root, &child, | |
140 | &(xev.x_root), &(xev.y_root), | |
141 | &(xev.x), &(xev.y), | |
142 | &(xev.state)); | |
143 | *x = xev.x_root; | |
144 | *y = xev.y_root; | |
461e93f9 | 145 | #endif |
83df96d6 JS |
146 | }; |
147 | ||
670f9935 | 148 | // Return true if we have a colour display |
83df96d6 JS |
149 | bool wxColourDisplay() |
150 | { | |
151 | return wxDisplayDepth() > 1; | |
152 | } | |
153 | ||
154 | // Returns depth of screen | |
155 | int wxDisplayDepth() | |
156 | { | |
157 | Display *dpy = (Display*) wxGetDisplay(); | |
158 | ||
159 | return DefaultDepth (dpy, DefaultScreen (dpy)); | |
160 | } | |
161 | ||
162 | // Get size of display | |
163 | void wxDisplaySize(int *width, int *height) | |
164 | { | |
165 | Display *dpy = (Display*) wxGetDisplay(); | |
166 | ||
167 | if ( width ) | |
168 | *width = DisplayWidth (dpy, DefaultScreen (dpy)); | |
169 | if ( height ) | |
170 | *height = DisplayHeight (dpy, DefaultScreen (dpy)); | |
171 | } | |
172 | ||
173 | void wxDisplaySizeMM(int *width, int *height) | |
174 | { | |
175 | Display *dpy = (Display*) wxGetDisplay(); | |
176 | ||
177 | if ( width ) | |
178 | *width = DisplayWidthMM(dpy, DefaultScreen (dpy)); | |
179 | if ( height ) | |
180 | *height = DisplayHeightMM(dpy, DefaultScreen (dpy)); | |
181 | } | |
182 | ||
b886fae6 VZ |
183 | wxWindow* wxFindWindowAtPoint(const wxPoint& pt) |
184 | { | |
185 | return wxGenericFindWindowAtPoint(pt); | |
186 | } | |
187 | ||
83df96d6 | 188 | |
5e29f97a | 189 | // Configurable display in wxX11 and wxMotif |
b886fae6 | 190 | static Display *gs_currentDisplay = NULL; |
83df96d6 JS |
191 | static wxString gs_displayName; |
192 | ||
193 | WXDisplay *wxGetDisplay() | |
194 | { | |
b886fae6 | 195 | return (WXDisplay *)gs_currentDisplay; |
83df96d6 JS |
196 | } |
197 | ||
b886fae6 VZ |
198 | // close the current display |
199 | void wxCloseDisplay() | |
83df96d6 | 200 | { |
b886fae6 | 201 | if ( gs_currentDisplay ) |
83df96d6 | 202 | { |
b886fae6 VZ |
203 | if ( XCloseDisplay(gs_currentDisplay) != 0 ) |
204 | { | |
205 | wxLogWarning(_("Failed to close the display \"%s\""), | |
206 | gs_displayName.c_str()); | |
207 | } | |
83df96d6 | 208 | |
b886fae6 VZ |
209 | gs_currentDisplay = NULL; |
210 | gs_displayName.clear(); | |
83df96d6 | 211 | } |
b886fae6 | 212 | } |
83df96d6 | 213 | |
b886fae6 VZ |
214 | bool wxSetDisplay(const wxString& displayName) |
215 | { | |
0e4f7936 VZ |
216 | Display *dpy = XOpenDisplay |
217 | ( | |
218 | displayName.empty() ? NULL | |
219 | : (const char *)displayName.mb_str() | |
220 | ); | |
b886fae6 VZ |
221 | |
222 | if ( !dpy ) | |
223 | { | |
224 | wxLogError(_("Failed to open display \"%s\"."), displayName.c_str()); | |
225 | return false; | |
83df96d6 | 226 | } |
b886fae6 VZ |
227 | |
228 | wxCloseDisplay(); | |
229 | ||
230 | gs_currentDisplay = dpy; | |
231 | gs_displayName = displayName; | |
232 | ||
233 | return true; | |
83df96d6 JS |
234 | } |
235 | ||
236 | wxString wxGetDisplayName() | |
237 | { | |
238 | return gs_displayName; | |
239 | } | |
240 | ||
b886fae6 VZ |
241 | #include "wx/module.h" |
242 | ||
243 | // the module responsible for closing the X11 display at the program end | |
244 | class wxX11DisplayModule : public wxModule | |
83df96d6 | 245 | { |
b886fae6 VZ |
246 | public: |
247 | virtual bool OnInit() { return true; } | |
248 | virtual void OnExit() { wxCloseDisplay(); } | |
249 | ||
250 | private: | |
251 | DECLARE_DYNAMIC_CLASS(wxX11DisplayModule) | |
252 | }; | |
253 | ||
254 | IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule, wxModule) | |
83df96d6 | 255 | |
83df96d6 JS |
256 | // ---------------------------------------------------------------------------- |
257 | // Some colour manipulation routines | |
258 | // ---------------------------------------------------------------------------- | |
259 | ||
260 | void wxHSVToXColor(wxHSV *hsv,XColor *rgb) | |
261 | { | |
262 | int h = hsv->h; | |
263 | int s = hsv->s; | |
264 | int v = hsv->v; | |
265 | int r = 0, g = 0, b = 0; | |
266 | int i, f; | |
267 | int p, q, t; | |
268 | s = (s * wxMAX_RGB) / wxMAX_SV; | |
269 | v = (v * wxMAX_RGB) / wxMAX_SV; | |
270 | if (h == 360) h = 0; | |
271 | if (s == 0) { h = 0; r = g = b = v; } | |
272 | i = h / 60; | |
273 | f = h % 60; | |
274 | p = v * (wxMAX_RGB - s) / wxMAX_RGB; | |
275 | q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB; | |
276 | t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB; | |
277 | switch (i) | |
278 | { | |
279 | case 0: r = v, g = t, b = p; break; | |
280 | case 1: r = q, g = v, b = p; break; | |
281 | case 2: r = p, g = v, b = t; break; | |
282 | case 3: r = p, g = q, b = v; break; | |
283 | case 4: r = t, g = p, b = v; break; | |
284 | case 5: r = v, g = p, b = q; break; | |
285 | } | |
286 | rgb->red = r << 8; | |
287 | rgb->green = g << 8; | |
288 | rgb->blue = b << 8; | |
289 | } | |
290 | ||
291 | void wxXColorToHSV(wxHSV *hsv,XColor *rgb) | |
292 | { | |
293 | int r = rgb->red >> 8; | |
294 | int g = rgb->green >> 8; | |
295 | int b = rgb->blue >> 8; | |
296 | int maxv = wxMax3(r, g, b); | |
297 | int minv = wxMin3(r, g, b); | |
298 | int h = 0, s, v; | |
299 | v = maxv; | |
300 | if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv; | |
301 | else s = 0; | |
302 | if (s == 0) h = 0; | |
303 | else | |
304 | { | |
305 | int rc, gc, bc, hex = 0; | |
306 | rc = (maxv - r) * wxMAX_RGB / (maxv - minv); | |
307 | gc = (maxv - g) * wxMAX_RGB / (maxv - minv); | |
308 | bc = (maxv - b) * wxMAX_RGB / (maxv - minv); | |
309 | if (r == maxv) { h = bc - gc, hex = 0; } | |
310 | else if (g == maxv) { h = rc - bc, hex = 2; } | |
311 | else if (b == maxv) { h = gc - rc, hex = 4; } | |
312 | h = hex * 60 + (h * 60 / wxMAX_RGB); | |
313 | if (h < 0) h += 360; | |
314 | } | |
315 | hsv->h = h; | |
316 | hsv->s = (s * wxMAX_SV) / wxMAX_RGB; | |
317 | hsv->v = (v * wxMAX_SV) / wxMAX_RGB; | |
318 | } | |
319 | ||
320 | void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc) | |
321 | { | |
461e93f9 | 322 | #if !wxUSE_NANOX |
83df96d6 JS |
323 | int llp; |
324 | ||
325 | int screen = DefaultScreen(d); | |
326 | int num_colors = DisplayCells(d,screen); | |
327 | ||
328 | XColor *color_defs = new XColor[num_colors]; | |
329 | for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp; | |
330 | XQueryColors(d,cmp,color_defs,num_colors); | |
331 | ||
332 | wxHSV hsv_defs, hsv; | |
333 | wxXColorToHSV(&hsv,xc); | |
334 | ||
335 | int diff, min_diff = 0, pixel = 0; | |
336 | ||
337 | for(llp = 0;llp < num_colors;llp++) | |
338 | { | |
339 | wxXColorToHSV(&hsv_defs,&color_defs[llp]); | |
340 | diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) + | |
341 | wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) + | |
342 | wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v)); | |
343 | if (llp == 0) min_diff = diff; | |
344 | if (min_diff > diff) { min_diff = diff; pixel = llp; } | |
345 | if (min_diff == 0) break; | |
346 | } | |
347 | ||
348 | xc -> red = color_defs[pixel].red; | |
349 | xc -> green = color_defs[pixel].green; | |
350 | xc -> blue = color_defs[pixel].blue; | |
351 | xc -> flags = DoRed | DoGreen | DoBlue; | |
352 | ||
353 | /* FIXME, TODO | |
354 | if (!XAllocColor(d,cmp,xc)) | |
355 | cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n"; | |
356 | */ | |
357 | ||
358 | delete[] color_defs; | |
461e93f9 | 359 | #endif |
83df96d6 JS |
360 | } |
361 | ||
362 | void wxAllocColor(Display *d,Colormap cmp,XColor *xc) | |
363 | { | |
364 | if (!XAllocColor(d,cmp,xc)) | |
365 | { | |
4c51a665 | 366 | // cout << "wxAllocColor : Warning : Cannot allocate color, attempt find nearest !\n"; |
83df96d6 JS |
367 | wxAllocNearestColor(d,cmp,xc); |
368 | } | |
369 | } | |
370 | ||
45ff6421 JS |
371 | wxString wxGetXEventName(XEvent& event) |
372 | { | |
461e93f9 JS |
373 | #if wxUSE_NANOX |
374 | wxString str(wxT("(some event)")); | |
375 | return str; | |
376 | #else | |
45ff6421 | 377 | int type = event.xany.type; |
ffbd0d14 | 378 | static const char* event_name[] = { |
521bf4ff WS |
379 | "", "unknown(-)", // 0-1 |
380 | "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5 | |
381 | "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9 | |
382 | "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13 | |
383 | "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16 | |
384 | "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20 | |
385 | "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23 | |
386 | "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26 | |
387 | "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29 | |
388 | "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32 | |
389 | "ClientMessage", "MappingNotify", // 33-34 | |
390 | "unknown(+)"}; // 35 | |
391 | type = wxMin(35, type); type = wxMax(1, type); | |
54385bdb | 392 | return wxString::FromAscii(event_name[type]); |
461e93f9 JS |
393 | #endif |
394 | } | |
45ff6421 | 395 | |
c5453c79 VZ |
396 | #if wxUSE_EVENTLOOP_SOURCE |
397 | ||
398 | class wxX11EventLoopSourcesManager : public wxEventLoopSourcesManagerBase | |
399 | { | |
400 | public: | |
401 | wxEventLoopSource * | |
402 | AddSourceForFD(int WXUNUSED(fd), | |
403 | wxEventLoopSourceHandler* WXUNUSED(handler), | |
404 | int WXUNUSED(flags)) | |
405 | { | |
406 | wxFAIL_MSG("Monitoring FDs in the main loop is not implemented in wxX11"); | |
407 | ||
408 | return NULL; | |
409 | } | |
410 | }; | |
411 | ||
412 | wxEventLoopSourcesManagerBase* wxGUIAppTraits::GetEventLoopSourcesManager() | |
413 | { | |
414 | static wxX11EventLoopSourcesManager s_eventLoopSourcesManager; | |
415 | ||
416 | return &s_eventLoopSourcesManager; | |
417 | } | |
418 | ||
419 | #endif // wxUSE_EVENTLOOP_SOURCE |