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