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