]> git.saurik.com Git - wxWidgets.git/blob - src/x11/utils.cpp
Add wxX11EventLoopSourcesManager stub implementation to fix wxX11 linking.
[wxWidgets.git] / src / x11 / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/utils.cpp
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
9 // (c) 2013 Rob Bresalier
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // for compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
15
16 #if defined(__BORLANDC__)
17 #pragma hdrstop
18 #endif
19
20 #include "wx/private/eventloopsourcesmanager.h"
21
22 // ============================================================================
23 // declarations
24 // ============================================================================
25
26 // ----------------------------------------------------------------------------
27 // headers
28 // ----------------------------------------------------------------------------
29
30 #include "wx/utils.h"
31
32 #ifndef WX_PRECOMP
33 #include "wx/app.h"
34 #include "wx/window.h" // for wxTopLevelWindows
35 #include "wx/cursor.h"
36 #include "wx/msgdlg.h"
37 #endif
38
39 #include "wx/apptrait.h"
40 #include "wx/generic/private/timer.h"
41 #include "wx/evtloop.h"
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>
53 // #include <netdb.h>
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
63
64 #include "wx/x11/private.h"
65
66 #include "X11/Xutil.h"
67
68 #ifdef __VMS__
69 #pragma message enable nosimpint
70 #endif
71
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
83 // TODO for X11
84 // ??
85 }
86
87 bool wxCheckForInterrupt(wxWindow *WXUNUSED(wnd))
88 {
89 wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented."));
90 return false;
91 }
92
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
104 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
105 {
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;
117 }
118
119 wxEventLoopBase* wxGUIAppTraits::CreateEventLoop()
120 {
121 return new wxEventLoop;
122 }
123
124 // ----------------------------------------------------------------------------
125 // display info
126 // ----------------------------------------------------------------------------
127
128 void wxGetMousePosition( int* x, int* y )
129 {
130 #if wxUSE_NANOX
131 // TODO
132 *x = 0;
133 *y = 0;
134 #else
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;
145 #endif
146 };
147
148 // Return true if we have a colour display
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
183 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
184 {
185 return wxGenericFindWindowAtPoint(pt);
186 }
187
188
189 // Configurable display in wxX11 and wxMotif
190 static Display *gs_currentDisplay = NULL;
191 static wxString gs_displayName;
192
193 WXDisplay *wxGetDisplay()
194 {
195 return (WXDisplay *)gs_currentDisplay;
196 }
197
198 // close the current display
199 void wxCloseDisplay()
200 {
201 if ( gs_currentDisplay )
202 {
203 if ( XCloseDisplay(gs_currentDisplay) != 0 )
204 {
205 wxLogWarning(_("Failed to close the display \"%s\""),
206 gs_displayName.c_str());
207 }
208
209 gs_currentDisplay = NULL;
210 gs_displayName.clear();
211 }
212 }
213
214 bool wxSetDisplay(const wxString& displayName)
215 {
216 Display *dpy = XOpenDisplay
217 (
218 displayName.empty() ? NULL
219 : (const char *)displayName.mb_str()
220 );
221
222 if ( !dpy )
223 {
224 wxLogError(_("Failed to open display \"%s\"."), displayName.c_str());
225 return false;
226 }
227
228 wxCloseDisplay();
229
230 gs_currentDisplay = dpy;
231 gs_displayName = displayName;
232
233 return true;
234 }
235
236 wxString wxGetDisplayName()
237 {
238 return gs_displayName;
239 }
240
241 #include "wx/module.h"
242
243 // the module responsible for closing the X11 display at the program end
244 class wxX11DisplayModule : public wxModule
245 {
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)
255
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 {
322 #if !wxUSE_NANOX
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;
359 #endif
360 }
361
362 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
363 {
364 if (!XAllocColor(d,cmp,xc))
365 {
366 // cout << "wxAllocColor : Warning : Cannot allocate color, attempt find nearest !\n";
367 wxAllocNearestColor(d,cmp,xc);
368 }
369 }
370
371 wxString wxGetXEventName(XEvent& event)
372 {
373 #if wxUSE_NANOX
374 wxString str(wxT("(some event)"));
375 return str;
376 #else
377 int type = event.xany.type;
378 static const char* event_name[] = {
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);
392 return wxString::FromAscii(event_name[type]);
393 #endif
394 }
395
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