]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/utilsgtk.cpp
some feeble attempts at making wxBeginBusyCursor work
[wxWidgets.git] / src / gtk1 / utilsgtk.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: utils.cpp
3// Purpose:
4// Author: Robert Roebling
dfcb1ae0 5// Id: $Id$
c801d85f 6// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
03f38c58 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
11//#ifdef __GNUG__
12//#pragma implementation "utils.h"
13//#endif
14
15#include "wx/utils.h"
16#include "wx/string.h"
17
02847e59
VZ
18#include "wx/intl.h"
19#include "wx/log.h"
20
5336ece4
VZ
21#include "wx/process.h"
22
c801d85f
KB
23#include <stdarg.h>
24#include <dirent.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <unistd.h>
29#include <sys/wait.h>
30#include <pwd.h>
31#include <errno.h>
32#include <netdb.h>
82052aff 33#include <signal.h>
4e13eb84 34#include <fcntl.h> // for O_WRONLY and friends
c801d85f 35
83624f79
RR
36#include "glib.h"
37#include "gdk/gdk.h"
38#include "gtk/gtk.h"
bbe0af5b 39#include "gtk/gtkfeatures.h"
83624f79 40#include "gdk/gdkx.h"
dfcb1ae0 41
c801d85f 42#ifdef __SVR4__
02847e59 43 #include <sys/systeminfo.h>
c801d85f
KB
44#endif
45
71317c5b
KB
46#ifdef __SOLARIS__
47// somehow missing from sys/wait.h but in the system's docs
1950b38c
KB
48extern "C"
49{
50 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
51 *rusage);
52}
71317c5b
KB
53#endif
54
c801d85f
KB
55//------------------------------------------------------------------------
56// misc.
57//------------------------------------------------------------------------
58
59void wxBell(void)
60{
e52f60e6
RR
61 gdk_beep();
62}
c801d85f 63
82052aff
GL
64void wxSleep(int nSecs)
65{
e52f60e6
RR
66 sleep(nSecs);
67}
82052aff
GL
68
69int wxKill(long pid, int sig)
70{
e52f60e6
RR
71 return kill(pid, sig);
72}
82052aff 73
c0392997
RR
74void wxDisplaySize( int *width, int *height )
75{
e52f60e6
RR
76 if (width) *width = gdk_screen_width();
77 if (height) *height = gdk_screen_height();
c0392997
RR
78}
79
6de97a3b
RR
80void wxGetMousePosition( int* x, int* y )
81{
bbe0af5b 82 gdk_window_get_pointer( (GdkWindow*) NULL, x, y, (GdkModifierType*) NULL );
e52f60e6 83}
6de97a3b
RR
84
85bool wxColourDisplay(void)
86{
e52f60e6 87 return TRUE;
6de97a3b
RR
88}
89
90int wxDisplayDepth(void)
91{
2d17d68f 92 return gdk_window_get_visual( (GdkWindow*) &gdk_root_parent )->depth;
6de97a3b
RR
93}
94
bbe0af5b
RR
95int wxGetOsVersion(int *majorVsn, int *minorVsn)
96{
97 if (majorVsn) *majorVsn = GTK_MAJOR_VERSION;
98 if (minorVsn) *minorVsn = GTK_MINOR_VERSION;
7e84f02d 99
bbe0af5b
RR
100 return wxGTK;
101}
102
c801d85f
KB
103//------------------------------------------------------------------------
104// user and home routines
105//------------------------------------------------------------------------
106
d84eb083 107const char* wxGetHomeDir( wxString *home )
c801d85f 108{
e52f60e6
RR
109 *home = wxGetUserHome( wxString() );
110 if (home->IsNull()) *home = "/";
111 return *home;
112}
c801d85f
KB
113
114char *wxGetUserHome( const wxString &user )
115{
e52f60e6 116 struct passwd *who = (struct passwd *) NULL;
c801d85f 117
e52f60e6
RR
118 if (user.IsNull() || (user== ""))
119 {
03f38c58
VZ
120 register char *ptr;
121
122 if ((ptr = getenv("HOME")) != NULL)
e52f60e6 123 {
03f38c58 124 return ptr;
e52f60e6 125 }
7e84f02d 126 if ((ptr = getenv("USER")) != NULL || (ptr = getenv("LOGNAME")) != NULL)
e52f60e6 127 {
03f38c58
VZ
128 who = getpwnam(ptr);
129 }
7e84f02d 130
bbe0af5b 131 /* We now make sure the the user exists! */
03f38c58 132 if (who == NULL)
e52f60e6 133 {
03f38c58 134 who = getpwuid(getuid());
e52f60e6
RR
135 }
136 }
137 else
138 {
139 who = getpwnam (user);
140 }
03f38c58 141
e52f60e6
RR
142 return who ? who->pw_dir : (char*)NULL;
143}
c801d85f
KB
144
145//------------------------------------------------------------------------
146// id routines
147//------------------------------------------------------------------------
148
149bool wxGetHostName(char *buf, int sz)
150{
151 *buf = '\0';
152#if defined(__SVR4__) && !defined(__sgi)
3ad0023f 153 //KB: does this return the fully qualified host.domain name?
c801d85f
KB
154 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
155#else /* BSD Sockets */
3ad0023f
KB
156 char name[255], domain[255];
157 //struct hostent *h;
c801d85f
KB
158 // Get hostname
159 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
03f38c58 160 return FALSE;
3ad0023f 161 if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1)
03f38c58 162 return FALSE;
c801d85f 163 // Get official full name of host
3ad0023f
KB
164 // doesn't return the full qualified name, replaced by following
165 // code (KB)
166 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
167 if((unsigned)sz > strlen(name)+strlen(domain)+1)
168 {
e52f60e6
RR
169 strcpy(buf, name);
170 if(strcmp(domain,"(none)") == 0) // standalone machine
171 {
172 strcat(buf,".");
173 strcat(buf,domain);
174 }
3ad0023f
KB
175 }
176 else
e52f60e6 177 return FALSE;
c801d85f
KB
178 return TRUE;
179#endif
180}
181
182bool wxGetUserId(char *buf, int sz)
183{
184 struct passwd *who;
185
186 *buf = '\0';
187 if ((who = getpwuid(getuid ())) != NULL) {
03f38c58
VZ
188 strncpy (buf, who->pw_name, sz-1);
189 return TRUE;
c801d85f
KB
190 }
191 return FALSE;
192}
193
194bool wxGetUserName(char *buf, int sz)
195{
196 struct passwd *who;
3ad0023f 197 char *comma;
03f38c58 198
c801d85f
KB
199 *buf = '\0';
200 if ((who = getpwuid (getuid ())) != NULL) {
3ad0023f
KB
201 comma = strchr(who->pw_gecos,'c');
202 if(comma) *comma = '\0'; // cut off non-name comment fields
203 strncpy (buf, who->pw_gecos, sz - 1);
03f38c58 204 return TRUE;
c801d85f
KB
205 }
206 return FALSE;
207}
208
209//------------------------------------------------------------------------
210// error and debug output routines
211//------------------------------------------------------------------------
212
213void wxDebugMsg( const char *format, ... )
214{
215 va_list ap;
216 va_start( ap, format );
03f38c58 217 vfprintf( stderr, format, ap );
c801d85f
KB
218 fflush( stderr );
219 va_end(ap);
3069ac4e 220}
c801d85f
KB
221
222void wxError( const wxString &msg, const wxString &title )
223{
224 fprintf( stderr, "Error " );
225 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
226 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
227 fprintf( stderr, ".\n" );
3069ac4e 228}
c801d85f
KB
229
230void wxFatalError( const wxString &msg, const wxString &title )
231{
232 fprintf( stderr, "Error " );
233 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
234 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
235 fprintf( stderr, ".\n" );
02847e59 236 exit(3); // the same exit code as for abort()
3069ac4e 237}
c801d85f
KB
238
239//------------------------------------------------------------------------
240// directory routines
241//------------------------------------------------------------------------
242
243bool wxDirExists( const wxString& dir )
244{
e52f60e6
RR
245 char buf[500];
246 strcpy( buf, WXSTRINGCAST(dir) );
247 struct stat sbuf;
248 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
3069ac4e 249}
c801d85f 250
c801d85f
KB
251//------------------------------------------------------------------------
252// subprocess routines
253//------------------------------------------------------------------------
254
e6045e08
VZ
255// if pid > 0, the execution is async and the data is freed in
256// GTK_EndProcessDetector, if pid < 0, the execution is synchronous and the
257// caller (wxExecute) frees the data
02847e59
VZ
258struct wxEndProcessData
259{
bbe0af5b
RR
260 gint pid, tag;
261 wxProcess *process;
e6045e08 262 int exitcode;
02847e59 263};
cf447356
GL
264
265static void GTK_EndProcessDetector(gpointer data, gint source,
e3e65dac 266 GdkInputCondition WXUNUSED(condition) )
cf447356 267{
bbe0af5b
RR
268 wxEndProcessData *proc_data = (wxEndProcessData *)data;
269 int pid;
cf447356 270
bbe0af5b 271 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
cf447356 272
bbe0af5b
RR
273 /* wait4 is not part of any standard, use at own risk
274 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
275 * --- offer@sgi.com */
5336ece4
VZ
276 // VZ: wait4() will be woken up by a signal, not wait3 - so it's quite
277 // different (also, wait3() waits for any child, wait4() only for this
278 // one)
279 int status = -1;
77e7a1dc 280#if !defined(__sgi)
e6045e08 281 wait4(pid, &status, 0, (rusage *) NULL);
77e7a1dc 282#else
5336ece4 283 wait3(&status, 0, (rusage *) NULL);
77e7a1dc 284#endif
cf447356 285
bbe0af5b
RR
286 close(source);
287 gdk_input_remove(proc_data->tag);
cf447356 288
bbe0af5b 289 if (proc_data->process)
5336ece4 290 proc_data->process->OnTerminate(proc_data->pid, status);
cf447356 291
bbe0af5b 292 if (proc_data->pid > 0)
e6045e08 293 {
bbe0af5b 294 delete proc_data;
e6045e08 295 }
bbe0af5b 296 else
e6045e08
VZ
297 {
298 // wxExecute() will know about it
299 proc_data->exitcode = status;
300
bbe0af5b 301 proc_data->pid = 0;
e6045e08 302 }
3069ac4e 303}
cf447356 304
eafc087e 305long wxExecute( char **argv, bool sync, wxProcess *process )
c801d85f 306{
cf447356
GL
307 wxEndProcessData *data = new wxEndProcessData;
308 int end_proc_detect[2];
309
02847e59 310 wxCHECK_MSG( *argv, 0, "can't exec empty command" );
cf447356
GL
311
312 /* Create pipes */
7e84f02d 313 if (pipe(end_proc_detect) == -1)
bbe0af5b 314 {
220a0f42 315 wxLogSysError( _("Pipe creation failed") );
7e84f02d 316 return 0;
cf447356 317 }
c801d85f
KB
318
319 /* fork the process */
320#if defined(sun) || defined(__ultrix) || defined(__bsdi__)
321 pid_t pid = vfork();
322#else
323 pid_t pid = fork();
324#endif
7e84f02d 325 if (pid == -1)
bbe0af5b 326 {
220a0f42 327 wxLogSysError( _("Fork failed") );
03f38c58 328 return 0;
02847e59 329 }
7e84f02d 330 else if (pid == 0)
bbe0af5b 331 {
02847e59 332 // we're in child
cf447356 333 close(end_proc_detect[0]); // close reading side
7e84f02d 334
220a0f42
VZ
335 // These three lines close the open file descriptors to to avoid any
336 // input/output which might block the process or irritate the user. If
337 // one wants proper IO for the subprocess, the "right thing to do is
338 // to start an xterm executing it.
7e84f02d
VZ
339 close(STDIN_FILENO);
340 close(STDOUT_FILENO);
220a0f42
VZ
341
342 // leave stderr opened, it won't do any hurm
343#if 0
7e84f02d
VZ
344 close(STDERR_FILENO);
345
220a0f42
VZ
346 // some programs complain about stderr not being open, so redirect
347 // them:
616c87c9 348 open("/dev/null", O_RDONLY); // stdin
7e84f02d
VZ
349 open("/dev/null", O_WRONLY); // stdout
350 open("/dev/null", O_WRONLY); // stderr
220a0f42 351#endif
cf447356 352
c801d85f 353#ifdef _AIX
03f38c58 354 execvp ((const char *)*argv, (const char **)argv);
c801d85f 355#else
03f38c58 356 execvp (*argv, argv);
c801d85f 357#endif
7e84f02d 358
02847e59 359 // there is no return after successful exec()
220a0f42 360 fprintf(stderr, _("Can't execute '%s'\n"), *argv);
cf447356 361
02847e59
VZ
362 _exit(-1);
363 }
7e84f02d 364 else
bbe0af5b 365 {
7e84f02d
VZ
366 // we're in parent
367 close(end_proc_detect[1]); // close writing side
368 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
220a0f42 369 GTK_EndProcessDetector, (gpointer)data);
e6045e08 370 if ( sync )
7e84f02d 371 {
e6045e08
VZ
372 wxASSERT_MSG( !process, "wxProcess param ignored for sync exec" );
373 data->process = NULL;
374
375 // sync execution: indicate it by negating the pid
376 data->pid = -pid;
7e84f02d 377
e6045e08 378 // it will be set to 0 from GTK_EndProcessDetector
7e84f02d
VZ
379 while (data->pid != 0)
380 wxYield();
381
e6045e08
VZ
382 int exitcode = data->exitcode;
383
7e84f02d 384 delete data;
e6045e08
VZ
385
386 return exitcode;
7e84f02d 387 }
e6045e08
VZ
388 else
389 {
390 // async execution, nothing special to do - caller will be
391 // notified about the process terminationif process != NULL, data
392 // will be deleted in GTK_EndProcessDetector
393 data->process = process;
394 data->pid = pid;
7e84f02d 395
e6045e08
VZ
396 return pid;
397 }
cf447356 398 }
3069ac4e 399}
c801d85f 400
eafc087e 401long wxExecute( const wxString& command, bool sync, wxProcess *process )
c801d85f 402{
02847e59
VZ
403 static const char *IFS = " \t\n";
404
405 wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" );
c801d85f
KB
406
407 int argc = 0;
408 char *argv[127];
02847e59
VZ
409 char *tmp = new char[command.Len() + 1];
410 strcpy(tmp, command);
c801d85f 411
02847e59 412 argv[argc++] = strtok(tmp, IFS);
c67daf87 413 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
03f38c58 414 /* loop */ ;
c801d85f 415
02847e59 416 long lRc = wxExecute(argv, sync, process);
7e84f02d 417
02847e59 418 delete [] tmp;
c0392997 419
02847e59 420 return lRc;
3069ac4e 421}