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