]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/utilsgtk.cpp
28d35e2adc6373c11a8181a77264ea69714937f4
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>
36 #include "gtk/gtkfeatures.h"
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
)
79 gdk_window_get_pointer( (GdkWindow
*) NULL
, x
, y
, (GdkModifierType
*) NULL
);
82 bool wxColourDisplay(void)
87 int wxDisplayDepth(void)
89 return gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
)->depth
;
92 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
94 if (majorVsn
) *majorVsn
= GTK_MAJOR_VERSION
;
95 if (minorVsn
) *minorVsn
= GTK_MINOR_VERSION
;
100 //------------------------------------------------------------------------
101 // user and home routines
102 //------------------------------------------------------------------------
104 const char* wxGetHomeDir( wxString
*home
)
106 *home
= wxGetUserHome( wxString() );
107 if (home
->IsNull()) *home
= "/";
111 char *wxGetUserHome( const wxString
&user
)
113 struct passwd
*who
= (struct passwd
*) NULL
;
115 if (user
.IsNull() || (user
== ""))
119 if ((ptr
= getenv("HOME")) != NULL
)
123 if ((ptr
= getenv("USER")) != NULL
|| (ptr
= getenv("LOGNAME")) != NULL
)
128 /* We now make sure the the user exists! */
131 who
= getpwuid(getuid());
136 who
= getpwnam (user
);
139 return who
? who
->pw_dir
: (char*)NULL
;
142 //------------------------------------------------------------------------
144 //------------------------------------------------------------------------
146 bool wxGetHostName(char *buf
, int sz
)
149 #if defined(__SVR4__) && !defined(__sgi)
150 //KB: does this return the fully qualified host.domain name?
151 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
152 #else /* BSD Sockets */
153 char name
[255], domain
[255];
156 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
158 if (getdomainname(domain
, sizeof(domain
)/sizeof(char)-1) == -1)
160 // Get official full name of host
161 // doesn't return the full qualified name, replaced by following
163 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
164 if((unsigned)sz
> strlen(name
)+strlen(domain
)+1)
167 if(strcmp(domain
,"(none)") == 0) // standalone machine
179 bool wxGetUserId(char *buf
, int sz
)
184 if ((who
= getpwuid(getuid ())) != NULL
) {
185 strncpy (buf
, who
->pw_name
, sz
-1);
191 bool wxGetUserName(char *buf
, int sz
)
197 if ((who
= getpwuid (getuid ())) != NULL
) {
198 comma
= strchr(who
->pw_gecos
,'c');
199 if(comma
) *comma
= '\0'; // cut off non-name comment fields
200 strncpy (buf
, who
->pw_gecos
, sz
- 1);
206 //------------------------------------------------------------------------
207 // error and debug output routines
208 //------------------------------------------------------------------------
210 void wxDebugMsg( const char *format
, ... )
213 va_start( ap
, format
);
214 vfprintf( stderr
, format
, ap
);
219 void wxError( const wxString
&msg
, const wxString
&title
)
221 fprintf( stderr
, "Error " );
222 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
223 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
224 fprintf( stderr
, ".\n" );
227 void wxFatalError( const wxString
&msg
, const wxString
&title
)
229 fprintf( stderr
, "Error " );
230 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
231 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
232 fprintf( stderr
, ".\n" );
233 exit(3); // the same exit code as for abort()
236 //------------------------------------------------------------------------
237 // directory routines
238 //------------------------------------------------------------------------
240 bool wxDirExists( const wxString
& dir
)
243 strcpy( buf
, WXSTRINGCAST(dir
) );
245 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
248 //------------------------------------------------------------------------
249 // subprocess routines
250 //------------------------------------------------------------------------
252 struct wxEndProcessData
258 static void GTK_EndProcessDetector(gpointer data
, gint source
,
259 GdkInputCondition
WXUNUSED(condition
) )
261 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
264 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
266 /* wait4 is not part of any standard, use at own risk
267 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
268 * --- offer@sgi.com */
270 wait4(proc_data
->pid
, (int*) NULL
, 0, (rusage
*) NULL
);
272 wait3((int *) NULL
, 0, (rusage
*) NULL
);
276 gdk_input_remove(proc_data
->tag
);
278 if (proc_data
->process
)
279 proc_data
->process
->OnTerminate(proc_data
->pid
);
281 if (proc_data
->pid
> 0)
287 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
289 wxEndProcessData
*data
= new wxEndProcessData
;
290 int end_proc_detect
[2];
292 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
295 if (pipe(end_proc_detect
) == -1)
297 wxLogSysError( "Pipe creation failed" );
301 /* fork the process */
302 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
309 wxLogSysError( "Fork failed" );
315 close(end_proc_detect
[0]); // close reading side
316 // These three lines close the open file descriptors to
317 // to avoid any input/output which might block the process
318 // or irritate the user. If one wants proper IO for the sub-
319 // process, the "right thing to do" is to start an xterm executing
322 close(STDOUT_FILENO
);
323 close(STDERR_FILENO
);
326 execvp ((const char *)*argv
, (const char **)argv
);
328 execvp (*argv
, argv
);
330 // there is no return after successful exec()
331 wxLogSysError( "Can't execute '%s'", *argv
);
338 close(end_proc_detect
[1]); // close writing side
339 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
340 GTK_EndProcessDetector
, (gpointer
)data
);
344 data
->process
= process
;
348 data
->process
= (wxProcess
*) NULL
;
349 data
->pid
= -(data
->pid
);
351 while (data
->pid
!= 0)
357 // @@@ our return value indicates success even if execvp() in the child
363 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
365 static const char *IFS
= " \t\n";
367 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
371 char *tmp
= new char[command
.Len() + 1];
372 strcpy(tmp
, command
);
374 argv
[argc
++] = strtok(tmp
, IFS
);
375 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
378 long lRc
= wxExecute(argv
, sync
, process
);