1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/utilsgtk.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id: utilsgtk.cpp 49838 2007-11-11 23:52:54Z VZ $
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/utils.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/string.h"
17 #include "wx/intl.h"
18 #include "wx/log.h"
19 #endif
20
21 #include "wx/apptrait.h"
22
23 #include "wx/process.h"
24
25 #include "wx/unix/execute.h"
26
27 #include <stdarg.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/wait.h> // for WNOHANG
32 #include <unistd.h>
33
34 #include "glib.h"
35 #include "gdk/gdk.h"
36 #include "gtk/gtk.h"
37 #include "gtk/gtkfeatures.h"
38 #include "gdk/gdkx.h"
39
40 #ifdef HAVE_X11_XKBLIB_H
41 /* under HP-UX and Solaris 2.6, at least, XKBlib.h defines structures with
42 * field named "explicit" - which is, of course, an error for a C++
43 * compiler. To be on the safe side, just redefine it everywhere. */
44 #define explicit __wx_explicit
45
46 #include "X11/XKBlib.h"
47
48 #undef explicit
49 #endif // HAVE_X11_XKBLIB_H
50
51 //-----------------------------------------------------------------------------
52 // data
53 //-----------------------------------------------------------------------------
54
55 extern GtkWidget *wxGetRootWindow();
56
57 //----------------------------------------------------------------------------
58 // misc.
59 //----------------------------------------------------------------------------
60 #ifndef __EMX__
61 // on OS/2, we use the wxBell from wxBase library
62
wxBell()63 void wxBell()
64 {
65 gdk_beep();
66 }
67 #endif
68
69 /* Don't synthesize KeyUp events holding down a key and producing
70 KeyDown events with autorepeat. */
71 #ifdef HAVE_X11_XKBLIB_H
wxSetDetectableAutoRepeat(bool flag)72 bool wxSetDetectableAutoRepeat( bool flag )
73 {
74 Bool result;
75 XkbSetDetectableAutoRepeat( GDK_DISPLAY(), flag, &result );
76 return result; /* true if keyboard hardware supports this mode */
77 }
78 #else
wxSetDetectableAutoRepeat(bool WXUNUSED (flag))79 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
80 {
81 return false;
82 }
83 #endif
84
85 // ----------------------------------------------------------------------------
86 // display characterstics
87 // ----------------------------------------------------------------------------
88
wxGetDisplay()89 void *wxGetDisplay()
90 {
91 return GDK_DISPLAY();
92 }
93
wxDisplaySize(int * width,int * height)94 void wxDisplaySize( int *width, int *height )
95 {
96 if (width) *width = gdk_screen_width();
97 if (height) *height = gdk_screen_height();
98 }
99
wxDisplaySizeMM(int * width,int * height)100 void wxDisplaySizeMM( int *width, int *height )
101 {
102 if (width) *width = gdk_screen_width_mm();
103 if (height) *height = gdk_screen_height_mm();
104 }
105
wxGetMousePosition(int * x,int * y)106 void wxGetMousePosition( int* x, int* y )
107 {
108 gdk_window_get_pointer( (GdkWindow*) NULL, x, y, (GdkModifierType*) NULL );
109 }
110
wxColourDisplay()111 bool wxColourDisplay()
112 {
113 return true;
114 }
115
wxDisplayDepth()116 int wxDisplayDepth()
117 {
118 return gdk_window_get_visual( wxGetRootWindow()->window )->depth;
119 }
120
wxFindWindowAtPoint(const wxPoint & pt)121 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
122 {
123 return wxGenericFindWindowAtPoint(pt);
124 }
125
126
127 // ----------------------------------------------------------------------------
128 // subprocess routines
129 // ----------------------------------------------------------------------------
130
131 extern "C" {
132 static
GTK_EndProcessDetector(gpointer data,gint source,GdkInputCondition WXUNUSED (condition))133 void GTK_EndProcessDetector(gpointer data, gint source,
134 GdkInputCondition WXUNUSED(condition) )
135 {
136 wxEndProcessData *proc_data = (wxEndProcessData *)data;
137
138 // has the process really terminated? unfortunately GDK (or GLib) seem to
139 // generate G_IO_HUP notification even when it simply tries to read from a
140 // closed fd and hasn't terminated at all
141 int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
142 int status = 0;
143 int rc = waitpid(pid, &status, WNOHANG);
144
145 if ( rc == 0 )
146 {
147 // no, it didn't exit yet, continue waiting
148 return;
149 }
150
151 // set exit code to -1 if something bad happened
152 proc_data->exitcode = rc != -1 && WIFEXITED(status) ? WEXITSTATUS(status)
153 : -1;
154
155 // child exited, end waiting
156 close(source);
157
158 // don't call us again!
159 gdk_input_remove(proc_data->tag);
160
161 wxHandleProcessTermination(proc_data);
162 }
163 }
164
wxAddProcessCallback(wxEndProcessData * proc_data,int fd)165 int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
166 {
167 int tag = gdk_input_add(fd,
168 GDK_INPUT_READ,
169 GTK_EndProcessDetector,
170 (gpointer)proc_data);
171
172 return tag;
173 }
174
175 // ----------------------------------------------------------------------------
176 // wxPlatformInfo-related
177 // ----------------------------------------------------------------------------
178
GetToolkitVersion(int * verMaj,int * verMin) const179 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
180 {
181 if ( verMaj )
182 *verMaj = gtk_major_version;
183 if ( verMin )
184 *verMin = gtk_minor_version;
185
186 return wxPORT_GTK;
187 }
188
GetDesktopEnvironment() const189 wxString wxGUIAppTraits::GetDesktopEnvironment() const
190 {
191 return wxEmptyString;
192 }
193