]>
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>
43 #include <sys/systeminfo.h>
47 // somehow missing from sys/wait.h but in the system's docs
50 pid_t
wait4(pid_t pid
, int *statusp
, int options
, struct rusage
55 //------------------------------------------------------------------------
57 //------------------------------------------------------------------------
64 void wxSleep(int nSecs
)
69 int wxKill(long pid
, int sig
)
71 return kill(pid
, sig
);
74 void wxDisplaySize( int *width
, int *height
)
76 if (width
) *width
= gdk_screen_width();
77 if (height
) *height
= gdk_screen_height();
80 void wxGetMousePosition( int* x
, int* y
)
86 XQueryPointer( GDK_DISPLAY(),GDK_ROOT_WINDOW(),
87 &dumw
,&dumw
,x
,y
,&dumi
,&dumi
,&dumu
);
90 bool wxColourDisplay(void)
95 int wxDisplayDepth(void)
97 return gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
)->depth
;
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
)
127 // We now make sure the the user exists!
130 who
= getpwuid(getuid());
135 who
= getpwnam (user
);
138 return who
? who
->pw_dir
: (char*)NULL
;
141 //------------------------------------------------------------------------
143 //------------------------------------------------------------------------
145 bool wxGetHostName(char *buf
, int sz
)
148 #if defined(__SVR4__) && !defined(__sgi)
149 //KB: does this return the fully qualified host.domain name?
150 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
151 #else /* BSD Sockets */
152 char name
[255], domain
[255];
155 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
157 if (getdomainname(domain
, sizeof(domain
)/sizeof(char)-1) == -1)
159 // Get official full name of host
160 // doesn't return the full qualified name, replaced by following
162 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
163 if((unsigned)sz
> strlen(name
)+strlen(domain
)+1)
166 if(strcmp(domain
,"(none)") == 0) // standalone machine
178 bool wxGetUserId(char *buf
, int sz
)
183 if ((who
= getpwuid(getuid ())) != NULL
) {
184 strncpy (buf
, who
->pw_name
, sz
-1);
190 bool wxGetUserName(char *buf
, int sz
)
196 if ((who
= getpwuid (getuid ())) != NULL
) {
197 comma
= strchr(who
->pw_gecos
,'c');
198 if(comma
) *comma
= '\0'; // cut off non-name comment fields
199 strncpy (buf
, who
->pw_gecos
, sz
- 1);
205 //------------------------------------------------------------------------
206 // error and debug output routines
207 //------------------------------------------------------------------------
209 void wxDebugMsg( const char *format
, ... )
212 va_start( ap
, format
);
213 vfprintf( stderr
, format
, ap
);
218 void wxError( const wxString
&msg
, const wxString
&title
)
220 fprintf( stderr
, "Error " );
221 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
222 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
223 fprintf( stderr
, ".\n" );
226 void wxFatalError( const wxString
&msg
, const wxString
&title
)
228 fprintf( stderr
, "Error " );
229 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
230 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
231 fprintf( stderr
, ".\n" );
232 exit(3); // the same exit code as for abort()
235 //------------------------------------------------------------------------
236 // directory routines
237 //------------------------------------------------------------------------
239 bool wxDirExists( const wxString
& dir
)
242 strcpy( buf
, WXSTRINGCAST(dir
) );
244 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
247 //------------------------------------------------------------------------
248 // subprocess routines
249 //------------------------------------------------------------------------
251 struct wxEndProcessData
257 static void GTK_EndProcessDetector(gpointer data
, gint source
,
258 GdkInputCondition
WXUNUSED(condition
) )
260 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
263 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
265 /* wait4 is not part of any standard, use at own risk
266 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
267 * --- offer@sgi.com */
269 wait4(proc_data
->pid
, NULL
, 0, NULL
);
271 wait3((int *) NULL
, 0, (rusage
*) NULL
);
275 gdk_input_remove(proc_data
->tag
);
277 if (proc_data
->process
)
278 proc_data
->process
->OnTerminate(proc_data
->pid
);
280 if (proc_data
->pid
> 0)
286 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
288 wxEndProcessData
*data
= new wxEndProcessData
;
289 int end_proc_detect
[2];
291 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
294 if (pipe(end_proc_detect
) == -1) {
295 wxLogSysError(_("Pipe creation failed"));
299 /* fork the process */
300 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
307 wxLogSysError(_("Fork failed"));
312 close(end_proc_detect
[0]); // close reading side
313 // These three lines close the open file descriptors to
314 // to avoid any input/output which might block the process
315 // or irritate the user. If one wants proper IO for the sub-
316 // process, the "right thing to do" is to start an xterm executing
319 close(STDOUT_FILENO
);
320 close(STDERR_FILENO
);
323 execvp ((const char *)*argv
, (const char **)argv
);
325 execvp (*argv
, argv
);
327 // there is no return after successful exec()
328 wxLogSysError(_("Can't execute '%s'"), *argv
);
334 close(end_proc_detect
[1]); // close writing side
335 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
336 GTK_EndProcessDetector
, (gpointer
)data
);
339 data
->process
= process
;
342 data
->process
= (wxProcess
*) NULL
;
343 data
->pid
= -(data
->pid
);
345 while (data
->pid
!= 0)
351 // @@@ our return value indicates success even if execvp() in the child
357 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
359 static const char *IFS
= " \t\n";
361 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
365 char *tmp
= new char[command
.Len() + 1];
366 strcpy(tmp
, command
);
368 argv
[argc
++] = strtok(tmp
, IFS
);
369 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
372 long lRc
= wxExecute(argv
, sync
, process
);