]>
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>
37 #include "gdk/gdkx.h" // GDK_DISPLAY
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
)
83 XQueryPointer( GDK_DISPLAY(),GDK_ROOT_WINDOW(),
84 &dumw
,&dumw
,x
,y
,&dumi
,&dumi
,&dumu
);
87 bool wxColourDisplay(void)
92 int wxDisplayDepth(void)
94 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
98 //------------------------------------------------------------------------
99 // user and home routines
100 //------------------------------------------------------------------------
102 const char* wxGetHomeDir( wxString
*home
)
104 *home
= wxGetUserHome( wxString() );
105 if (home
->IsNull()) *home
= "/";
109 char *wxGetUserHome( const wxString
&user
)
111 struct passwd
*who
= (struct passwd
*) NULL
;
113 if (user
.IsNull() || (user
== ""))
117 if ((ptr
= getenv("HOME")) != NULL
)
121 if ((ptr
= getenv("USER")) != NULL
|| (ptr
= getenv("LOGNAME")) != NULL
)
125 // We now make sure the the user exists!
128 who
= getpwuid(getuid());
133 who
= getpwnam (user
);
136 return who
? who
->pw_dir
: (char*)NULL
;
139 //------------------------------------------------------------------------
141 //------------------------------------------------------------------------
143 bool wxGetHostName(char *buf
, int sz
)
146 #if defined(__SVR4__) && !defined(__sgi)
147 //KB: does this return the fully qualified host.domain name?
148 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
149 #else /* BSD Sockets */
150 char name
[255], domain
[255];
153 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
155 if (getdomainname(domain
, sizeof(domain
)/sizeof(char)-1) == -1)
157 // Get official full name of host
158 // doesn't return the full qualified name, replaced by following
160 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
161 if((unsigned)sz
> strlen(name
)+strlen(domain
)+1)
164 if(strcmp(domain
,"(none)") == 0) // standalone machine
176 bool wxGetUserId(char *buf
, int sz
)
181 if ((who
= getpwuid(getuid ())) != NULL
) {
182 strncpy (buf
, who
->pw_name
, sz
-1);
188 bool wxGetUserName(char *buf
, int sz
)
194 if ((who
= getpwuid (getuid ())) != NULL
) {
195 comma
= strchr(who
->pw_gecos
,'c');
196 if(comma
) *comma
= '\0'; // cut off non-name comment fields
197 strncpy (buf
, who
->pw_gecos
, sz
- 1);
203 //------------------------------------------------------------------------
204 // error and debug output routines
205 //------------------------------------------------------------------------
207 void wxDebugMsg( const char *format
, ... )
210 va_start( ap
, format
);
211 vfprintf( stderr
, format
, ap
);
216 void wxError( const wxString
&msg
, const wxString
&title
)
218 fprintf( stderr
, "Error " );
219 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
220 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
221 fprintf( stderr
, ".\n" );
224 void wxFatalError( const wxString
&msg
, const wxString
&title
)
226 fprintf( stderr
, "Error " );
227 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
228 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
229 fprintf( stderr
, ".\n" );
230 exit(3); // the same exit code as for abort()
233 //------------------------------------------------------------------------
234 // directory routines
235 //------------------------------------------------------------------------
237 bool wxDirExists( const wxString
& dir
)
240 strcpy( buf
, WXSTRINGCAST(dir
) );
242 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
245 //------------------------------------------------------------------------
246 // subprocess routines
247 //------------------------------------------------------------------------
249 struct wxEndProcessData
255 static void GTK_EndProcessDetector(gpointer data
, gint source
,
256 GdkInputCondition
WXUNUSED(condition
) )
258 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
261 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
263 /* wait4 is not part of any standard, use at own risk
264 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
265 * --- offer@sgi.com */
267 wait4(proc_data
->pid
, NULL
, 0, NULL
);
269 wait3((int *) NULL
, 0, (rusage
*) NULL
);
273 gdk_input_remove(proc_data
->tag
);
275 if (proc_data
->process
)
276 proc_data
->process
->OnTerminate(proc_data
->pid
);
278 if (proc_data
->pid
> 0)
284 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
286 wxEndProcessData
*data
= new wxEndProcessData
;
287 int end_proc_detect
[2];
289 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
292 if (pipe(end_proc_detect
) == -1) {
293 wxLogSysError(_("Pipe creation failed"));
297 /* fork the process */
298 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
305 wxLogSysError(_("Fork failed"));
310 close(end_proc_detect
[0]); // close reading side
311 // These three lines close the open file descriptors to
312 // to avoid any input/output which might block the process
313 // or irritate the user. If one wants proper IO for the sub-
314 // process, the "right thing to do" is to start an xterm executing
317 close(STDOUT_FILENO
);
318 close(STDERR_FILENO
);
321 execvp ((const char *)*argv
, (const char **)argv
);
323 execvp (*argv
, argv
);
325 // there is no return after successful exec()
326 wxLogSysError(_("Can't execute '%s'"), *argv
);
332 close(end_proc_detect
[1]); // close writing side
333 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
334 GTK_EndProcessDetector
, (gpointer
)data
);
337 data
->process
= process
;
340 data
->process
= (wxProcess
*) NULL
;
341 data
->pid
= -(data
->pid
);
343 while (data
->pid
!= 0)
349 // @@@ our return value indicates success even if execvp() in the child
355 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
357 static const char *IFS
= " \t\n";
359 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
363 char *tmp
= new char[command
.Len() + 1];
364 strcpy(tmp
, command
);
366 argv
[argc
++] = strtok(tmp
, IFS
);
367 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
370 long lRc
= wxExecute(argv
, sync
, process
);