]>
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 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/string.h"
16 #include "wx/process.h"
22 #include <sys/types.h>
29 #include <fcntl.h> // for O_WRONLY and friends
30 #include <time.h> // nanosleep() and/or usleep()
35 #include <gtk/gtkfeatures.h>
39 #include <sys/systeminfo.h>
42 // many versions of Unices have this function, but it is not defined in system
43 // headers - please add your system here if it is the case for your OS.
44 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
45 #if (defined(__SUN__) && !defined(__SunOs_5_6) && \
46 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
50 void usleep(unsigned long usec
);
52 #endif // Unices without usleep()
54 // many versions of Unices have this function, but it is not defined in system
55 // headers - please add your system here if it is the case for your OS.
56 // SunOS (and Solaris) and DG-UX are like this.
57 #if defined(__SOLARIS__) || defined(__osf__)
60 pid_t
wait4(pid_t pid
, int *statusp
, int options
,
61 struct rusage
*rusage
);
64 #define wxWait4(pid, stat, flags, rusage) wait4(pid, stat, flags, rusage)
65 #elif defined(__sgi) || defined(__HPUX__)
66 // no wait4() at all on these systems
67 // TODO verify whether wait3() really works in this situation
68 #define wxWait4(pid, stat, flags, rusage) wait3(stat, flags, rusage)
70 // other Unices: assume have wait4(), although it's not standard (but
71 // Linux and FreeBSD do have it)
72 #define wxWait4(pid, stat, flags, rusage) wait4(pid, stat, flags, rusage)
75 //------------------------------------------------------------------------
77 //------------------------------------------------------------------------
84 void wxSleep(int nSecs
)
89 void wxUsleep(unsigned long milliseconds
)
91 #if defined(HAVE_NANOSLEEP)
93 tmReq
.tv_sec
= milliseconds
/ 1000;
94 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
96 // we're not interested in remaining time nor in return value
97 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
98 #elif defined(HAVE_USLEEP)
99 // uncomment this if you feel brave or if you are sure that your version
100 // of Solaris has a safe usleep() function but please notice that usleep()
101 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
102 // documented as MT-Safe
103 #if defined(__SUN__) && defined(wxUSE_THREADS)
104 #error "usleep() cannot be used in MT programs under Solaris."
107 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
108 #else // !sleep function
109 #error "usleep() or nanosleep() function required for wxUsleep"
110 #endif // sleep function
113 int wxKill(long pid
, int sig
)
115 return kill(pid
, sig
);
118 void wxDisplaySize( int *width
, int *height
)
120 if (width
) *width
= gdk_screen_width();
121 if (height
) *height
= gdk_screen_height();
124 void wxGetMousePosition( int* x
, int* y
)
126 gdk_window_get_pointer( (GdkWindow
*) NULL
, x
, y
, (GdkModifierType
*) NULL
);
129 bool wxColourDisplay(void)
134 int wxDisplayDepth(void)
136 return gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
)->depth
;
139 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
141 if (majorVsn
) *majorVsn
= GTK_MAJOR_VERSION
;
142 if (minorVsn
) *minorVsn
= GTK_MINOR_VERSION
;
147 //------------------------------------------------------------------------
148 // user and home routines
149 //------------------------------------------------------------------------
151 const char* wxGetHomeDir( wxString
*home
)
153 *home
= wxGetUserHome( wxString() );
154 if (home
->IsNull()) *home
= "/";
158 char *wxGetUserHome( const wxString
&user
)
160 struct passwd
*who
= (struct passwd
*) NULL
;
162 if (user
.IsNull() || (user
== ""))
166 if ((ptr
= getenv("HOME")) != NULL
)
170 if ((ptr
= getenv("USER")) != NULL
|| (ptr
= getenv("LOGNAME")) != NULL
)
175 /* We now make sure the the user exists! */
178 who
= getpwuid(getuid());
183 who
= getpwnam (user
);
186 return who
? who
->pw_dir
: (char*)NULL
;
189 //------------------------------------------------------------------------
191 //------------------------------------------------------------------------
193 bool wxGetHostName(char *buf
, int sz
)
196 #if defined(__SVR4__) && !defined(__sgi)
197 //KB: does this return the fully qualified host.domain name?
198 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
199 #else /* BSD Sockets */
200 char name
[255], domain
[255];
203 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
205 if (getdomainname(domain
, sizeof(domain
)/sizeof(char)-1) == -1)
207 // Get official full name of host
208 // doesn't return the full qualified name, replaced by following
210 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
211 if((unsigned)sz
> strlen(name
)+strlen(domain
)+1)
214 if(strcmp(domain
,"(none)") == 0) // standalone machine
226 bool wxGetUserId(char *buf
, int sz
)
231 if ((who
= getpwuid(getuid ())) != NULL
) {
232 strncpy (buf
, who
->pw_name
, sz
-1);
238 bool wxGetUserName(char *buf
, int sz
)
244 if ((who
= getpwuid (getuid ())) != NULL
) {
245 comma
= strchr(who
->pw_gecos
,'c');
246 if(comma
) *comma
= '\0'; // cut off non-name comment fields
247 strncpy (buf
, who
->pw_gecos
, sz
- 1);
253 //------------------------------------------------------------------------
254 // error and debug output routines
255 //------------------------------------------------------------------------
257 void wxDebugMsg( const char *format
, ... )
260 va_start( ap
, format
);
261 vfprintf( stderr
, format
, ap
);
266 void wxError( const wxString
&msg
, const wxString
&title
)
268 fprintf( stderr
, "Error " );
269 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
270 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
271 fprintf( stderr
, ".\n" );
274 void wxFatalError( const wxString
&msg
, const wxString
&title
)
276 fprintf( stderr
, "Error " );
277 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
278 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
279 fprintf( stderr
, ".\n" );
280 exit(3); // the same exit code as for abort()
283 //------------------------------------------------------------------------
284 // directory routines
285 //------------------------------------------------------------------------
287 bool wxDirExists( const wxString
& dir
)
290 strcpy( buf
, WXSTRINGCAST(dir
) );
292 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
295 //------------------------------------------------------------------------
296 // subprocess routines
297 //------------------------------------------------------------------------
299 // if pid > 0, the execution is async and the data is freed in
300 // GTK_EndProcessDetector, if pid < 0, the execution is synchronous and the
301 // caller (wxExecute) frees the data
302 struct wxEndProcessData
309 static void GTK_EndProcessDetector(gpointer data
, gint source
,
310 GdkInputCondition
WXUNUSED(condition
) )
312 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
315 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
318 wxWait4(pid
, &status
, 0, (rusage
*) NULL
);
321 gdk_input_remove(proc_data
->tag
);
323 if (proc_data
->process
)
324 proc_data
->process
->OnTerminate(proc_data
->pid
, status
);
326 if (proc_data
->pid
> 0)
332 // wxExecute() will know about it
333 proc_data
->exitcode
= status
;
339 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
341 wxEndProcessData
*data
= new wxEndProcessData
;
342 int end_proc_detect
[2];
344 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
347 if (pipe(end_proc_detect
) == -1)
349 wxLogSysError( _("Pipe creation failed") );
353 /* fork the process */
354 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
361 wxLogSysError( _("Fork failed") );
367 close(end_proc_detect
[0]); // close reading side
369 // These three lines close the open file descriptors to to avoid any
370 // input/output which might block the process or irritate the user. If
371 // one wants proper IO for the subprocess, the "right thing to do is
372 // to start an xterm executing it.
374 close(STDOUT_FILENO
);
376 // leave stderr opened, it won't do any hurm
378 close(STDERR_FILENO
);
380 // some programs complain about stderr not being open, so redirect
382 open("/dev/null", O_RDONLY
); // stdin
383 open("/dev/null", O_WRONLY
); // stdout
384 open("/dev/null", O_WRONLY
); // stderr
388 execvp ((const char *)*argv
, (const char **)argv
);
390 execvp (*argv
, argv
);
393 // there is no return after successful exec()
394 fprintf(stderr
, _("Can't execute '%s'\n"), *argv
);
401 close(end_proc_detect
[1]); // close writing side
402 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
403 GTK_EndProcessDetector
, (gpointer
)data
);
406 wxASSERT_MSG( !process
, "wxProcess param ignored for sync exec" );
407 data
->process
= NULL
;
409 // sync execution: indicate it by negating the pid
412 // it will be set to 0 from GTK_EndProcessDetector
413 while (data
->pid
!= 0)
416 int exitcode
= data
->exitcode
;
424 // async execution, nothing special to do - caller will be
425 // notified about the process terminationif process != NULL, data
426 // will be deleted in GTK_EndProcessDetector
427 data
->process
= process
;
435 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
437 static const char *IFS
= " \t\n";
439 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
443 char *tmp
= new char[command
.Len() + 1];
444 strcpy(tmp
, command
);
446 argv
[argc
++] = strtok(tmp
, IFS
);
447 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
450 long lRc
= wxExecute(argv
, sync
, process
);