]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/utilsgtk.cpp
OpenGl works now under GTK
[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
c801d85f
KB
21#include <stdarg.h>
22#include <dirent.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27#include <sys/wait.h>
28#include <pwd.h>
29#include <errno.h>
30#include <netdb.h>
82052aff 31#include <signal.h>
c801d85f 32
83624f79
RR
33#include "glib.h"
34#include "gdk/gdk.h"
35#include "gtk/gtk.h"
bbe0af5b 36#include "gtk/gtkfeatures.h"
83624f79 37#include "gdk/gdkx.h"
dfcb1ae0 38
c801d85f 39#ifdef __SVR4__
02847e59 40 #include <sys/systeminfo.h>
c801d85f
KB
41#endif
42
71317c5b
KB
43#ifdef __SOLARIS__
44// somehow missing from sys/wait.h but in the system's docs
1950b38c
KB
45extern "C"
46{
47 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
48 *rusage);
49}
71317c5b
KB
50#endif
51
c801d85f
KB
52//------------------------------------------------------------------------
53// misc.
54//------------------------------------------------------------------------
55
56void wxBell(void)
57{
e52f60e6
RR
58 gdk_beep();
59}
c801d85f 60
82052aff
GL
61void wxSleep(int nSecs)
62{
e52f60e6
RR
63 sleep(nSecs);
64}
82052aff
GL
65
66int wxKill(long pid, int sig)
67{
e52f60e6
RR
68 return kill(pid, sig);
69}
82052aff 70
c0392997
RR
71void wxDisplaySize( int *width, int *height )
72{
e52f60e6
RR
73 if (width) *width = gdk_screen_width();
74 if (height) *height = gdk_screen_height();
c0392997
RR
75}
76
6de97a3b
RR
77void wxGetMousePosition( int* x, int* y )
78{
bbe0af5b 79 gdk_window_get_pointer( (GdkWindow*) NULL, x, y, (GdkModifierType*) NULL );
e52f60e6 80}
6de97a3b
RR
81
82bool wxColourDisplay(void)
83{
e52f60e6 84 return TRUE;
6de97a3b
RR
85}
86
87int wxDisplayDepth(void)
88{
2d17d68f 89 return gdk_window_get_visual( (GdkWindow*) &gdk_root_parent )->depth;
6de97a3b
RR
90}
91
bbe0af5b
RR
92int wxGetOsVersion(int *majorVsn, int *minorVsn)
93{
94 if (majorVsn) *majorVsn = GTK_MAJOR_VERSION;
95 if (minorVsn) *minorVsn = GTK_MINOR_VERSION;
96
97 return wxGTK;
98}
99
c801d85f
KB
100//------------------------------------------------------------------------
101// user and home routines
102//------------------------------------------------------------------------
103
d84eb083 104const char* wxGetHomeDir( wxString *home )
c801d85f 105{
e52f60e6
RR
106 *home = wxGetUserHome( wxString() );
107 if (home->IsNull()) *home = "/";
108 return *home;
109}
c801d85f
KB
110
111char *wxGetUserHome( const wxString &user )
112{
e52f60e6 113 struct passwd *who = (struct passwd *) NULL;
c801d85f 114
e52f60e6
RR
115 if (user.IsNull() || (user== ""))
116 {
03f38c58
VZ
117 register char *ptr;
118
119 if ((ptr = getenv("HOME")) != NULL)
e52f60e6 120 {
03f38c58 121 return ptr;
e52f60e6
RR
122 }
123 if ((ptr = getenv("USER")) != NULL || (ptr = getenv("LOGNAME")) != NULL)
124 {
03f38c58
VZ
125 who = getpwnam(ptr);
126 }
bbe0af5b
RR
127
128 /* We now make sure the the user exists! */
03f38c58 129 if (who == NULL)
e52f60e6 130 {
03f38c58 131 who = getpwuid(getuid());
e52f60e6
RR
132 }
133 }
134 else
135 {
136 who = getpwnam (user);
137 }
03f38c58 138
e52f60e6
RR
139 return who ? who->pw_dir : (char*)NULL;
140}
c801d85f
KB
141
142//------------------------------------------------------------------------
143// id routines
144//------------------------------------------------------------------------
145
146bool wxGetHostName(char *buf, int sz)
147{
148 *buf = '\0';
149#if defined(__SVR4__) && !defined(__sgi)
3ad0023f 150 //KB: does this return the fully qualified host.domain name?
c801d85f
KB
151 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
152#else /* BSD Sockets */
3ad0023f
KB
153 char name[255], domain[255];
154 //struct hostent *h;
c801d85f
KB
155 // Get hostname
156 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
03f38c58 157 return FALSE;
3ad0023f 158 if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1)
03f38c58 159 return FALSE;
c801d85f 160 // Get official full name of host
3ad0023f
KB
161 // doesn't return the full qualified name, replaced by following
162 // code (KB)
163 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
164 if((unsigned)sz > strlen(name)+strlen(domain)+1)
165 {
e52f60e6
RR
166 strcpy(buf, name);
167 if(strcmp(domain,"(none)") == 0) // standalone machine
168 {
169 strcat(buf,".");
170 strcat(buf,domain);
171 }
3ad0023f
KB
172 }
173 else
e52f60e6 174 return FALSE;
c801d85f
KB
175 return TRUE;
176#endif
177}
178
179bool wxGetUserId(char *buf, int sz)
180{
181 struct passwd *who;
182
183 *buf = '\0';
184 if ((who = getpwuid(getuid ())) != NULL) {
03f38c58
VZ
185 strncpy (buf, who->pw_name, sz-1);
186 return TRUE;
c801d85f
KB
187 }
188 return FALSE;
189}
190
191bool wxGetUserName(char *buf, int sz)
192{
193 struct passwd *who;
3ad0023f 194 char *comma;
03f38c58 195
c801d85f
KB
196 *buf = '\0';
197 if ((who = getpwuid (getuid ())) != NULL) {
3ad0023f
KB
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);
03f38c58 201 return TRUE;
c801d85f
KB
202 }
203 return FALSE;
204}
205
206//------------------------------------------------------------------------
207// error and debug output routines
208//------------------------------------------------------------------------
209
210void wxDebugMsg( const char *format, ... )
211{
212 va_list ap;
213 va_start( ap, format );
03f38c58 214 vfprintf( stderr, format, ap );
c801d85f
KB
215 fflush( stderr );
216 va_end(ap);
3069ac4e 217}
c801d85f
KB
218
219void wxError( const wxString &msg, const wxString &title )
220{
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" );
3069ac4e 225}
c801d85f
KB
226
227void wxFatalError( const wxString &msg, const wxString &title )
228{
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" );
02847e59 233 exit(3); // the same exit code as for abort()
3069ac4e 234}
c801d85f
KB
235
236//------------------------------------------------------------------------
237// directory routines
238//------------------------------------------------------------------------
239
240bool wxDirExists( const wxString& dir )
241{
e52f60e6
RR
242 char buf[500];
243 strcpy( buf, WXSTRINGCAST(dir) );
244 struct stat sbuf;
245 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
3069ac4e 246}
c801d85f 247
c801d85f
KB
248//------------------------------------------------------------------------
249// subprocess routines
250//------------------------------------------------------------------------
251
02847e59
VZ
252struct wxEndProcessData
253{
bbe0af5b
RR
254 gint pid, tag;
255 wxProcess *process;
02847e59 256};
cf447356
GL
257
258static void GTK_EndProcessDetector(gpointer data, gint source,
e3e65dac 259 GdkInputCondition WXUNUSED(condition) )
cf447356 260{
bbe0af5b
RR
261 wxEndProcessData *proc_data = (wxEndProcessData *)data;
262 int pid;
cf447356 263
bbe0af5b 264 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
cf447356 265
bbe0af5b
RR
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 */
77e7a1dc 269#if !defined(__sgi)
bbe0af5b 270 wait4(proc_data->pid, (int*) NULL, 0, (rusage *) NULL);
77e7a1dc 271#else
bbe0af5b 272 wait3((int *) NULL, 0, (rusage *) NULL);
77e7a1dc 273#endif
cf447356 274
bbe0af5b
RR
275 close(source);
276 gdk_input_remove(proc_data->tag);
cf447356 277
bbe0af5b
RR
278 if (proc_data->process)
279 proc_data->process->OnTerminate(proc_data->pid);
cf447356 280
bbe0af5b
RR
281 if (proc_data->pid > 0)
282 delete proc_data;
283 else
284 proc_data->pid = 0;
3069ac4e 285}
cf447356 286
eafc087e 287long wxExecute( char **argv, bool sync, wxProcess *process )
c801d85f 288{
cf447356
GL
289 wxEndProcessData *data = new wxEndProcessData;
290 int end_proc_detect[2];
291
02847e59 292 wxCHECK_MSG( *argv, 0, "can't exec empty command" );
cf447356
GL
293
294 /* Create pipes */
bbe0af5b
RR
295 if (pipe(end_proc_detect) == -1)
296 {
297 wxLogSysError( "Pipe creation failed" );
cf447356
GL
298 return 0;
299 }
c801d85f
KB
300
301 /* fork the process */
302#if defined(sun) || defined(__ultrix) || defined(__bsdi__)
303 pid_t pid = vfork();
304#else
305 pid_t pid = fork();
306#endif
bbe0af5b
RR
307 if (pid == -1)
308 {
309 wxLogSysError( "Fork failed" );
03f38c58 310 return 0;
02847e59 311 }
bbe0af5b
RR
312 else if (pid == 0)
313 {
02847e59 314 // we're in child
cf447356 315 close(end_proc_detect[0]); // close reading side
2faf15a1
KB
316 // These three lines close the open file descriptors to
317 // to avoid any input/output which might block the process
318 // or irritate the user. If one wants proper IO for the sub-
319 // process, the "right thing to do" is to start an xterm executing
320 // it.
321 close(STDIN_FILENO);
322 close(STDOUT_FILENO);
323 close(STDERR_FILENO);
cf447356 324
c801d85f 325#ifdef _AIX
03f38c58 326 execvp ((const char *)*argv, (const char **)argv);
c801d85f 327#else
03f38c58 328 execvp (*argv, argv);
c801d85f 329#endif
02847e59 330 // there is no return after successful exec()
bbe0af5b 331 wxLogSysError( "Can't execute '%s'", *argv);
cf447356 332
02847e59
VZ
333 _exit(-1);
334 }
bbe0af5b
RR
335 else
336 {
02847e59
VZ
337 // we're in parent
338 close(end_proc_detect[1]); // close writing side
339 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
340 GTK_EndProcessDetector, (gpointer)data);
341 data->pid = pid;
bbe0af5b
RR
342 if (!sync)
343 {
02847e59
VZ
344 data->process = process;
345 }
bbe0af5b
RR
346 else
347 {
02847e59
VZ
348 data->process = (wxProcess *) NULL;
349 data->pid = -(data->pid);
350
351 while (data->pid != 0)
352 wxYield();
353
354 delete data;
355 }
356
357 // @@@ our return value indicates success even if execvp() in the child
358 // failed!
359 return pid;
cf447356 360 }
3069ac4e 361}
c801d85f 362
eafc087e 363long wxExecute( const wxString& command, bool sync, wxProcess *process )
c801d85f 364{
02847e59
VZ
365 static const char *IFS = " \t\n";
366
367 wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" );
c801d85f
KB
368
369 int argc = 0;
370 char *argv[127];
02847e59
VZ
371 char *tmp = new char[command.Len() + 1];
372 strcpy(tmp, command);
c801d85f 373
02847e59 374 argv[argc++] = strtok(tmp, IFS);
c67daf87 375 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
03f38c58 376 /* loop */ ;
c801d85f 377
02847e59
VZ
378 long lRc = wxExecute(argv, sync, process);
379
380 delete [] tmp;
c0392997 381
02847e59 382 return lRc;
3069ac4e 383}