]>
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"
21 #include "wx/process.h"
27 #include <sys/types.h>
34 #include <fcntl.h> // for O_WRONLY and friends
39 #include "gtk/gtkfeatures.h"
43 #include <sys/systeminfo.h>
46 // many versions of Unices have this function, but it is not defined in system
47 // headers - please add your system here if it is the case for your OS.
48 // SunOS (and Solaris) and DG-UX are like this.
49 #if defined(__SOLARIS__) || defined(__osf__)
52 pid_t
wait4(pid_t pid
, int *statusp
, int options
,
53 struct rusage
*rusage
);
56 #define wxWait4(pid, stat, flags, rusage) wait4(pid, stat, flags, rusage)
57 #elif defined(__sgi) || defined(__HPUX__)
58 // no wait4() at all on these systems
59 // TODO verify whether wait3() really works in this situation
60 #define wxWait4(pid, stat, flags, rusage) wait3(stat, flags, rusage)
62 // other Unices: assume have wait4(), although it's not standard (but
63 // Linux and FreeBSD do have it)
64 #define wxWait4(pid, stat, flags, rusage) wait4(pid, stat, flags, rusage)
67 //------------------------------------------------------------------------
69 //------------------------------------------------------------------------
76 void wxSleep(int nSecs
)
81 int wxKill(long pid
, int sig
)
83 return kill(pid
, sig
);
86 void wxDisplaySize( int *width
, int *height
)
88 if (width
) *width
= gdk_screen_width();
89 if (height
) *height
= gdk_screen_height();
92 void wxGetMousePosition( int* x
, int* y
)
94 gdk_window_get_pointer( (GdkWindow
*) NULL
, x
, y
, (GdkModifierType
*) NULL
);
97 bool wxColourDisplay(void)
102 int wxDisplayDepth(void)
104 return gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
)->depth
;
107 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
109 if (majorVsn
) *majorVsn
= GTK_MAJOR_VERSION
;
110 if (minorVsn
) *minorVsn
= GTK_MINOR_VERSION
;
115 //------------------------------------------------------------------------
116 // user and home routines
117 //------------------------------------------------------------------------
119 const char* wxGetHomeDir( wxString
*home
)
121 *home
= wxGetUserHome( wxString() );
122 if (home
->IsNull()) *home
= "/";
126 char *wxGetUserHome( const wxString
&user
)
128 struct passwd
*who
= (struct passwd
*) NULL
;
130 if (user
.IsNull() || (user
== ""))
134 if ((ptr
= getenv("HOME")) != NULL
)
138 if ((ptr
= getenv("USER")) != NULL
|| (ptr
= getenv("LOGNAME")) != NULL
)
143 /* We now make sure the the user exists! */
146 who
= getpwuid(getuid());
151 who
= getpwnam (user
);
154 return who
? who
->pw_dir
: (char*)NULL
;
157 //------------------------------------------------------------------------
159 //------------------------------------------------------------------------
161 bool wxGetHostName(char *buf
, int sz
)
164 #if defined(__SVR4__) && !defined(__sgi)
165 //KB: does this return the fully qualified host.domain name?
166 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
167 #else /* BSD Sockets */
168 char name
[255], domain
[255];
171 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
173 if (getdomainname(domain
, sizeof(domain
)/sizeof(char)-1) == -1)
175 // Get official full name of host
176 // doesn't return the full qualified name, replaced by following
178 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
179 if((unsigned)sz
> strlen(name
)+strlen(domain
)+1)
182 if(strcmp(domain
,"(none)") == 0) // standalone machine
194 bool wxGetUserId(char *buf
, int sz
)
199 if ((who
= getpwuid(getuid ())) != NULL
) {
200 strncpy (buf
, who
->pw_name
, sz
-1);
206 bool wxGetUserName(char *buf
, int sz
)
212 if ((who
= getpwuid (getuid ())) != NULL
) {
213 comma
= strchr(who
->pw_gecos
,'c');
214 if(comma
) *comma
= '\0'; // cut off non-name comment fields
215 strncpy (buf
, who
->pw_gecos
, sz
- 1);
221 //------------------------------------------------------------------------
222 // error and debug output routines
223 //------------------------------------------------------------------------
225 void wxDebugMsg( const char *format
, ... )
228 va_start( ap
, format
);
229 vfprintf( stderr
, format
, ap
);
234 void wxError( const wxString
&msg
, const wxString
&title
)
236 fprintf( stderr
, "Error " );
237 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
238 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
239 fprintf( stderr
, ".\n" );
242 void wxFatalError( const wxString
&msg
, const wxString
&title
)
244 fprintf( stderr
, "Error " );
245 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
246 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
247 fprintf( stderr
, ".\n" );
248 exit(3); // the same exit code as for abort()
251 //------------------------------------------------------------------------
252 // directory routines
253 //------------------------------------------------------------------------
255 bool wxDirExists( const wxString
& dir
)
258 strcpy( buf
, WXSTRINGCAST(dir
) );
260 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
263 //------------------------------------------------------------------------
264 // subprocess routines
265 //------------------------------------------------------------------------
267 // if pid > 0, the execution is async and the data is freed in
268 // GTK_EndProcessDetector, if pid < 0, the execution is synchronous and the
269 // caller (wxExecute) frees the data
270 struct wxEndProcessData
277 static void GTK_EndProcessDetector(gpointer data
, gint source
,
278 GdkInputCondition
WXUNUSED(condition
) )
280 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
283 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
286 wxWait4(pid
, &status
, 0, (rusage
*) NULL
);
289 gdk_input_remove(proc_data
->tag
);
291 if (proc_data
->process
)
292 proc_data
->process
->OnTerminate(proc_data
->pid
, status
);
294 if (proc_data
->pid
> 0)
300 // wxExecute() will know about it
301 proc_data
->exitcode
= status
;
307 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
309 wxEndProcessData
*data
= new wxEndProcessData
;
310 int end_proc_detect
[2];
312 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
315 if (pipe(end_proc_detect
) == -1)
317 wxLogSysError( _("Pipe creation failed") );
321 /* fork the process */
322 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
329 wxLogSysError( _("Fork failed") );
335 close(end_proc_detect
[0]); // close reading side
337 // These three lines close the open file descriptors to to avoid any
338 // input/output which might block the process or irritate the user. If
339 // one wants proper IO for the subprocess, the "right thing to do is
340 // to start an xterm executing it.
342 close(STDOUT_FILENO
);
344 // leave stderr opened, it won't do any hurm
346 close(STDERR_FILENO
);
348 // some programs complain about stderr not being open, so redirect
350 open("/dev/null", O_RDONLY
); // stdin
351 open("/dev/null", O_WRONLY
); // stdout
352 open("/dev/null", O_WRONLY
); // stderr
356 execvp ((const char *)*argv
, (const char **)argv
);
358 execvp (*argv
, argv
);
361 // there is no return after successful exec()
362 fprintf(stderr
, _("Can't execute '%s'\n"), *argv
);
369 close(end_proc_detect
[1]); // close writing side
370 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
371 GTK_EndProcessDetector
, (gpointer
)data
);
374 wxASSERT_MSG( !process
, "wxProcess param ignored for sync exec" );
375 data
->process
= NULL
;
377 // sync execution: indicate it by negating the pid
380 // it will be set to 0 from GTK_EndProcessDetector
381 while (data
->pid
!= 0)
384 int exitcode
= data
->exitcode
;
392 // async execution, nothing special to do - caller will be
393 // notified about the process terminationif process != NULL, data
394 // will be deleted in GTK_EndProcessDetector
395 data
->process
= process
;
403 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
405 static const char *IFS
= " \t\n";
407 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
411 char *tmp
= new char[command
.Len() + 1];
412 strcpy(tmp
, command
);
414 argv
[argc
++] = strtok(tmp
, IFS
);
415 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
418 long lRc
= wxExecute(argv
, sync
, process
);