]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/utilsgtk.cpp
fixed wxICON() usage
[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
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
dfcb1ae0 33
c801d85f 34#ifdef __SVR4__
02847e59 35 #include <sys/systeminfo.h>
c801d85f
KB
36#endif
37
71317c5b
KB
38#ifdef __SOLARIS__
39// somehow missing from sys/wait.h but in the system's docs
40pid_t wait4(pid_t pid, int *statusp, int options, struct rusage *rusage);
41#endif
42
c801d85f
KB
43//------------------------------------------------------------------------
44// misc.
45//------------------------------------------------------------------------
46
47void wxBell(void)
48{
49 gdk_beep();
50};
51
82052aff
GL
52void wxSleep(int nSecs)
53{
54 sleep(nSecs);
55};
56
57int wxKill(long pid, int sig)
58{
59 return kill(pid, sig);
60};
61
c0392997
RR
62void wxDisplaySize( int *width, int *height )
63{
64 if (width) *width = gdk_screen_width();
65 if (height) *height = gdk_screen_height();
66}
67
6de97a3b
RR
68void wxGetMousePosition( int* x, int* y )
69{
70 wxFAIL_MSG( "GetMousePosition not yet implemented" );
71 if (x) *x = 0;
72 if (y) *y = 0;
73};
74
75bool wxColourDisplay(void)
76{
77 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
78 return TRUE;
79}
80
81int wxDisplayDepth(void)
82{
83 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
84 return 8;
85}
86
c801d85f
KB
87//------------------------------------------------------------------------
88// user and home routines
89//------------------------------------------------------------------------
90
d84eb083 91const char* wxGetHomeDir( wxString *home )
c801d85f 92{
d84eb083
RR
93 *home = wxGetUserHome( wxString() );
94 if (home->IsNull()) *home = "/";
95 return *home;
c801d85f
KB
96};
97
98char *wxGetUserHome( const wxString &user )
99{
c67daf87 100 struct passwd *who = (struct passwd *) NULL;
c801d85f 101
03f38c58 102 if (user.IsNull() || (user== ""))
c801d85f 103 {
03f38c58
VZ
104 register char *ptr;
105
106 if ((ptr = getenv("HOME")) != NULL)
107 return ptr;
108 if ((ptr = getenv("USER")) != NULL
109 || (ptr = getenv("LOGNAME")) != NULL) {
110 who = getpwnam(ptr);
111 }
112 // We now make sure the the user exists!
113 if (who == NULL)
114 who = getpwuid(getuid());
115 }
c801d85f
KB
116 else
117 who = getpwnam (user);
03f38c58 118
c801d85f
KB
119 return who ? who->pw_dir : (char*)NULL;
120};
121
122//------------------------------------------------------------------------
123// id routines
124//------------------------------------------------------------------------
125
126bool wxGetHostName(char *buf, int sz)
127{
128 *buf = '\0';
129#if defined(__SVR4__) && !defined(__sgi)
3ad0023f 130 //KB: does this return the fully qualified host.domain name?
c801d85f
KB
131 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
132#else /* BSD Sockets */
3ad0023f
KB
133 char name[255], domain[255];
134 //struct hostent *h;
c801d85f
KB
135 // Get hostname
136 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
03f38c58 137 return FALSE;
3ad0023f 138 if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1)
03f38c58 139 return FALSE;
c801d85f 140 // Get official full name of host
3ad0023f
KB
141 // doesn't return the full qualified name, replaced by following
142 // code (KB)
143 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
144 if((unsigned)sz > strlen(name)+strlen(domain)+1)
145 {
146 strcpy(buf, name);
147 if(strcmp(domain,"(none)") == 0) // standalone machine
148 {
149 strcat(buf,".");
150 strcat(buf,domain);
151 }
152 }
153 else
154 return FALSE;
c801d85f
KB
155 return TRUE;
156#endif
157}
158
159bool wxGetUserId(char *buf, int sz)
160{
161 struct passwd *who;
162
163 *buf = '\0';
164 if ((who = getpwuid(getuid ())) != NULL) {
03f38c58
VZ
165 strncpy (buf, who->pw_name, sz-1);
166 return TRUE;
c801d85f
KB
167 }
168 return FALSE;
169}
170
171bool wxGetUserName(char *buf, int sz)
172{
173 struct passwd *who;
3ad0023f 174 char *comma;
03f38c58 175
c801d85f
KB
176 *buf = '\0';
177 if ((who = getpwuid (getuid ())) != NULL) {
3ad0023f
KB
178 comma = strchr(who->pw_gecos,'c');
179 if(comma) *comma = '\0'; // cut off non-name comment fields
180 strncpy (buf, who->pw_gecos, sz - 1);
03f38c58 181 return TRUE;
c801d85f
KB
182 }
183 return FALSE;
184}
185
186//------------------------------------------------------------------------
187// error and debug output routines
188//------------------------------------------------------------------------
189
190void wxDebugMsg( const char *format, ... )
191{
192 va_list ap;
193 va_start( ap, format );
03f38c58 194 vfprintf( stderr, format, ap );
c801d85f
KB
195 fflush( stderr );
196 va_end(ap);
197};
198
199void wxError( const wxString &msg, const wxString &title )
200{
201 fprintf( stderr, "Error " );
202 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
203 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
204 fprintf( stderr, ".\n" );
205};
206
207void wxFatalError( const wxString &msg, const wxString &title )
208{
209 fprintf( stderr, "Error " );
210 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
211 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
212 fprintf( stderr, ".\n" );
02847e59 213 exit(3); // the same exit code as for abort()
c801d85f
KB
214};
215
216//------------------------------------------------------------------------
217// directory routines
218//------------------------------------------------------------------------
219
220bool wxDirExists( const wxString& dir )
221{
222 char buf[500];
223 strcpy( buf, WXSTRINGCAST(dir) );
224 struct stat sbuf;
225 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
226};
227
c801d85f
KB
228//------------------------------------------------------------------------
229// subprocess routines
230//------------------------------------------------------------------------
231
02847e59
VZ
232struct wxEndProcessData
233{
cf447356
GL
234 gint pid, tag;
235 wxProcess *process;
02847e59 236};
cf447356
GL
237
238static void GTK_EndProcessDetector(gpointer data, gint source,
e3e65dac 239 GdkInputCondition WXUNUSED(condition) )
cf447356
GL
240{
241 wxEndProcessData *proc_data = (wxEndProcessData *)data;
242 int pid;
243
244 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
245
03f38c58 246 /* wait4 is not part of any standard, use at own risk
c67daf87
UR
247 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
248 * --- offer@sgi.com */
77e7a1dc 249#if !defined(__sgi)
cf447356 250 wait4(proc_data->pid, NULL, 0, NULL);
77e7a1dc 251#else
c67daf87 252 wait3((int *) NULL, 0, (rusage *) NULL);
77e7a1dc 253#endif
cf447356
GL
254
255 close(source);
256 gdk_input_remove(proc_data->tag);
257
258 if (proc_data->process)
259 proc_data->process->OnTerminate(proc_data->pid);
260
261 if (proc_data->pid > 0)
262 delete proc_data;
263 else
264 proc_data->pid = 0;
265};
266
eafc087e 267long wxExecute( char **argv, bool sync, wxProcess *process )
c801d85f 268{
cf447356
GL
269 wxEndProcessData *data = new wxEndProcessData;
270 int end_proc_detect[2];
271
02847e59 272 wxCHECK_MSG( *argv, 0, "can't exec empty command" );
cf447356
GL
273
274 /* Create pipes */
275 if (pipe(end_proc_detect) == -1) {
02847e59 276 wxLogSysError(_("Pipe creation failed"));
cf447356
GL
277 return 0;
278 }
c801d85f
KB
279
280 /* fork the process */
281#if defined(sun) || defined(__ultrix) || defined(__bsdi__)
282 pid_t pid = vfork();
283#else
284 pid_t pid = fork();
285#endif
286 if (pid == -1) {
02847e59
VZ
287 // error
288 wxLogSysError(_("Fork failed"));
03f38c58 289 return 0;
02847e59
VZ
290 }
291 else if (pid == 0) {
292 // we're in child
cf447356
GL
293 close(end_proc_detect[0]); // close reading side
294
c801d85f 295#ifdef _AIX
03f38c58 296 execvp ((const char *)*argv, (const char **)argv);
c801d85f 297#else
03f38c58 298 execvp (*argv, argv);
c801d85f 299#endif
02847e59
VZ
300 // there is no return after successful exec()
301 wxLogSysError(_("Can't execute '%s'"), *argv);
cf447356 302
02847e59
VZ
303 _exit(-1);
304 }
305 else {
306 // we're in parent
307 close(end_proc_detect[1]); // close writing side
308 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
309 GTK_EndProcessDetector, (gpointer)data);
310 data->pid = pid;
311 if (!sync) {
312 data->process = process;
313 }
314 else {
315 data->process = (wxProcess *) NULL;
316 data->pid = -(data->pid);
317
318 while (data->pid != 0)
319 wxYield();
320
321 delete data;
322 }
323
324 // @@@ our return value indicates success even if execvp() in the child
325 // failed!
326 return pid;
cf447356 327 }
c801d85f
KB
328};
329
eafc087e 330long wxExecute( const wxString& command, bool sync, wxProcess *process )
c801d85f 331{
02847e59
VZ
332 static const char *IFS = " \t\n";
333
334 wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" );
c801d85f
KB
335
336 int argc = 0;
337 char *argv[127];
02847e59
VZ
338 char *tmp = new char[command.Len() + 1];
339 strcpy(tmp, command);
c801d85f 340
02847e59 341 argv[argc++] = strtok(tmp, IFS);
c67daf87 342 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
03f38c58 343 /* loop */ ;
c801d85f 344
02847e59
VZ
345 long lRc = wxExecute(argv, sync, process);
346
347 delete [] tmp;
c0392997 348
02847e59
VZ
349 return lRc;
350};