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