]>
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 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
101 //------------------------------------------------------------------------
102 // user and home routines
103 //------------------------------------------------------------------------
105 const char* wxGetHomeDir( wxString
*home
)
107 *home
= wxGetUserHome( wxString() );
108 if (home
->IsNull()) *home
= "/";
112 char *wxGetUserHome( const wxString
&user
)
114 struct passwd
*who
= (struct passwd
*) NULL
;
116 if (user
.IsNull() || (user
== ""))
120 if ((ptr
= getenv("HOME")) != NULL
)
124 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
, NULL
, 0, 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) {
296 wxLogSysError(_("Pipe creation failed"));
300 /* fork the process */
301 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
308 wxLogSysError(_("Fork failed"));
313 close(end_proc_detect
[0]); // close reading side
314 // These three lines close the open file descriptors to
315 // to avoid any input/output which might block the process
316 // or irritate the user. If one wants proper IO for the sub-
317 // process, the "right thing to do" is to start an xterm executing
320 close(STDOUT_FILENO
);
321 close(STDERR_FILENO
);
324 execvp ((const char *)*argv
, (const char **)argv
);
326 execvp (*argv
, argv
);
328 // there is no return after successful exec()
329 wxLogSysError(_("Can't execute '%s'"), *argv
);
335 close(end_proc_detect
[1]); // close writing side
336 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
337 GTK_EndProcessDetector
, (gpointer
)data
);
340 data
->process
= process
;
343 data
->process
= (wxProcess
*) NULL
;
344 data
->pid
= -(data
->pid
);
346 while (data
->pid
!= 0)
352 // @@@ our return value indicates success even if execvp() in the child
358 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
360 static const char *IFS
= " \t\n";
362 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
366 char *tmp
= new char[command
.Len() + 1];
367 strcpy(tmp
, command
);
369 argv
[argc
++] = strtok(tmp
, IFS
);
370 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
373 long lRc
= wxExecute(argv
, sync
, process
);