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