]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/utilsgtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 //#pragma implementation "utils.h"
16 #include "wx/string.h"
25 #include <sys/types.h>
34 #include <X11/Xutil.h>
35 #include <X11/Xresource.h>
37 #include "gdk/gdkx.h" // GDK_DISPLAY
40 #include <sys/systeminfo.h>
44 // somehow missing from sys/wait.h but in the system's docs
47 pid_t
wait4(pid_t pid
, int *statusp
, int options
, struct rusage
52 //------------------------------------------------------------------------
54 //------------------------------------------------------------------------
61 void wxSleep(int nSecs
)
66 int wxKill(long pid
, int sig
)
68 return kill(pid
, sig
);
71 void wxDisplaySize( int *width
, int *height
)
73 if (width
) *width
= gdk_screen_width();
74 if (height
) *height
= gdk_screen_height();
77 void wxGetMousePosition( int* x
, int* y
)
83 XQueryPointer( GDK_DISPLAY(),GDK_ROOT_WINDOW(),
84 &dumw
,&dumw
,x
,y
,&dumi
,&dumi
,&dumu
);
87 bool wxColourDisplay(void)
89 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
93 int wxDisplayDepth(void)
95 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
99 //------------------------------------------------------------------------
100 // user and home routines
101 //------------------------------------------------------------------------
103 const char* wxGetHomeDir( wxString
*home
)
105 *home
= wxGetUserHome( wxString() );
106 if (home
->IsNull()) *home
= "/";
110 char *wxGetUserHome( const wxString
&user
)
112 struct passwd
*who
= (struct passwd
*) NULL
;
114 if (user
.IsNull() || (user
== ""))
118 if ((ptr
= getenv("HOME")) != NULL
)
122 if ((ptr
= getenv("USER")) != NULL
|| (ptr
= getenv("LOGNAME")) != NULL
)
126 // We now make sure the the user exists!
129 who
= getpwuid(getuid());
134 who
= getpwnam (user
);
137 return who
? who
->pw_dir
: (char*)NULL
;
140 //------------------------------------------------------------------------
142 //------------------------------------------------------------------------
144 bool wxGetHostName(char *buf
, int sz
)
147 #if defined(__SVR4__) && !defined(__sgi)
148 //KB: does this return the fully qualified host.domain name?
149 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
150 #else /* BSD Sockets */
151 char name
[255], domain
[255];
154 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
156 if (getdomainname(domain
, sizeof(domain
)/sizeof(char)-1) == -1)
158 // Get official full name of host
159 // doesn't return the full qualified name, replaced by following
161 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
162 if((unsigned)sz
> strlen(name
)+strlen(domain
)+1)
165 if(strcmp(domain
,"(none)") == 0) // standalone machine
177 bool wxGetUserId(char *buf
, int sz
)
182 if ((who
= getpwuid(getuid ())) != NULL
) {
183 strncpy (buf
, who
->pw_name
, sz
-1);
189 bool wxGetUserName(char *buf
, int sz
)
195 if ((who
= getpwuid (getuid ())) != NULL
) {
196 comma
= strchr(who
->pw_gecos
,'c');
197 if(comma
) *comma
= '\0'; // cut off non-name comment fields
198 strncpy (buf
, who
->pw_gecos
, sz
- 1);
204 //------------------------------------------------------------------------
205 // error and debug output routines
206 //------------------------------------------------------------------------
208 void wxDebugMsg( const char *format
, ... )
211 va_start( ap
, format
);
212 vfprintf( stderr
, format
, ap
);
217 void wxError( const wxString
&msg
, const wxString
&title
)
219 fprintf( stderr
, "Error " );
220 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
221 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
222 fprintf( stderr
, ".\n" );
225 void wxFatalError( const wxString
&msg
, const wxString
&title
)
227 fprintf( stderr
, "Error " );
228 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
229 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
230 fprintf( stderr
, ".\n" );
231 exit(3); // the same exit code as for abort()
234 //------------------------------------------------------------------------
235 // directory routines
236 //------------------------------------------------------------------------
238 bool wxDirExists( const wxString
& dir
)
241 strcpy( buf
, WXSTRINGCAST(dir
) );
243 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
246 //------------------------------------------------------------------------
247 // subprocess routines
248 //------------------------------------------------------------------------
250 struct wxEndProcessData
256 static void GTK_EndProcessDetector(gpointer data
, gint source
,
257 GdkInputCondition
WXUNUSED(condition
) )
259 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
262 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
264 /* wait4 is not part of any standard, use at own risk
265 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
266 * --- offer@sgi.com */
268 wait4(proc_data
->pid
, NULL
, 0, NULL
);
270 wait3((int *) NULL
, 0, (rusage
*) NULL
);
274 gdk_input_remove(proc_data
->tag
);
276 if (proc_data
->process
)
277 proc_data
->process
->OnTerminate(proc_data
->pid
);
279 if (proc_data
->pid
> 0)
285 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
287 wxEndProcessData
*data
= new wxEndProcessData
;
288 int end_proc_detect
[2];
290 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
293 if (pipe(end_proc_detect
) == -1) {
294 wxLogSysError(_("Pipe creation failed"));
298 /* fork the process */
299 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
306 wxLogSysError(_("Fork failed"));
311 close(end_proc_detect
[0]); // close reading side
312 // These three lines close the open file descriptors to
313 // to avoid any input/output which might block the process
314 // or irritate the user. If one wants proper IO for the sub-
315 // process, the "right thing to do" is to start an xterm executing
318 close(STDOUT_FILENO
);
319 close(STDERR_FILENO
);
322 execvp ((const char *)*argv
, (const char **)argv
);
324 execvp (*argv
, argv
);
326 // there is no return after successful exec()
327 wxLogSysError(_("Can't execute '%s'"), *argv
);
333 close(end_proc_detect
[1]); // close writing side
334 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
335 GTK_EndProcessDetector
, (gpointer
)data
);
338 data
->process
= process
;
341 data
->process
= (wxProcess
*) NULL
;
342 data
->pid
= -(data
->pid
);
344 while (data
->pid
!= 0)
350 // @@@ our return value indicates success even if execvp() in the child
356 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
358 static const char *IFS
= " \t\n";
360 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
364 char *tmp
= new char[command
.Len() + 1];
365 strcpy(tmp
, command
);
367 argv
[argc
++] = strtok(tmp
, IFS
);
368 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
371 long lRc
= wxExecute(argv
, sync
, process
);