Removed usage of GdkImlib
[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
34 #ifdef __SVR4__
35 #include <sys/systeminfo.h>
36 #endif
37
38 //------------------------------------------------------------------------
39 // misc.
40 //------------------------------------------------------------------------
41
42 void wxBell(void)
43 {
44 gdk_beep();
45 };
46
47 void wxSleep(int nSecs)
48 {
49 sleep(nSecs);
50 };
51
52 int wxKill(long pid, int sig)
53 {
54 return kill(pid, sig);
55 };
56
57 void wxDisplaySize( int *width, int *height )
58 {
59 if (width) *width = gdk_screen_width();
60 if (height) *height = gdk_screen_height();
61 }
62
63 void 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
70 bool wxColourDisplay(void)
71 {
72 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
73 return TRUE;
74 }
75
76 int wxDisplayDepth(void)
77 {
78 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
79 return 8;
80 }
81
82 //------------------------------------------------------------------------
83 // user and home routines
84 //------------------------------------------------------------------------
85
86 const char* wxGetHomeDir( wxString *home )
87 {
88 *home = wxGetUserHome( wxString() );
89 if (home->IsNull()) *home = "/";
90 return *home;
91 };
92
93 char *wxGetUserHome( const wxString &user )
94 {
95 struct passwd *who = (struct passwd *) NULL;
96
97 if (user.IsNull() || (user== ""))
98 {
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 }
111 else
112 who = getpwnam (user);
113
114 return who ? who->pw_dir : (char*)NULL;
115 };
116
117 //------------------------------------------------------------------------
118 // id routines
119 //------------------------------------------------------------------------
120
121 bool wxGetHostName(char *buf, int sz)
122 {
123 *buf = '\0';
124 #if defined(__SVR4__) && !defined(__sgi)
125 //KB: does this return the fully qualified host.domain name?
126 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
127 #else /* BSD Sockets */
128 char name[255], domain[255];
129 //struct hostent *h;
130 // Get hostname
131 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
132 return FALSE;
133 if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1)
134 return FALSE;
135 // Get official full name of host
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;
150 return TRUE;
151 #endif
152 }
153
154 bool wxGetUserId(char *buf, int sz)
155 {
156 struct passwd *who;
157
158 *buf = '\0';
159 if ((who = getpwuid(getuid ())) != NULL) {
160 strncpy (buf, who->pw_name, sz-1);
161 return TRUE;
162 }
163 return FALSE;
164 }
165
166 bool wxGetUserName(char *buf, int sz)
167 {
168 struct passwd *who;
169 char *comma;
170
171 *buf = '\0';
172 if ((who = getpwuid (getuid ())) != NULL) {
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);
176 return TRUE;
177 }
178 return FALSE;
179 }
180
181 //------------------------------------------------------------------------
182 // error and debug output routines
183 //------------------------------------------------------------------------
184
185 void wxDebugMsg( const char *format, ... )
186 {
187 va_list ap;
188 va_start( ap, format );
189 vfprintf( stderr, format, ap );
190 fflush( stderr );
191 va_end(ap);
192 };
193
194 void 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
202 void 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" );
208 exit(3); // the same exit code as for abort()
209 };
210
211 //------------------------------------------------------------------------
212 // directory routines
213 //------------------------------------------------------------------------
214
215 bool 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
223 //------------------------------------------------------------------------
224 // subprocess routines
225 //------------------------------------------------------------------------
226
227 struct wxEndProcessData
228 {
229 gint pid, tag;
230 wxProcess *process;
231 };
232
233 static void GTK_EndProcessDetector(gpointer data, gint source,
234 GdkInputCondition WXUNUSED(condition) )
235 {
236 wxEndProcessData *proc_data = (wxEndProcessData *)data;
237 int pid;
238
239 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
240
241 /* wait4 is not part of any standard, use at own risk
242 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
243 * --- offer@sgi.com */
244 #if !defined(__sgi)
245 wait4(proc_data->pid, NULL, 0, NULL);
246 #else
247 wait3((int *) NULL, 0, (rusage *) NULL);
248 #endif
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
262 long wxExecute( char **argv, bool sync, wxProcess *process )
263 {
264 wxEndProcessData *data = new wxEndProcessData;
265 int end_proc_detect[2];
266
267 wxCHECK_MSG( *argv, 0, "can't exec empty command" );
268
269 /* Create pipes */
270 if (pipe(end_proc_detect) == -1) {
271 wxLogSysError(_("Pipe creation failed"));
272 return 0;
273 }
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) {
282 // error
283 wxLogSysError(_("Fork failed"));
284 return 0;
285 }
286 else if (pid == 0) {
287 // we're in child
288 close(end_proc_detect[0]); // close reading side
289
290 #ifdef _AIX
291 execvp ((const char *)*argv, (const char **)argv);
292 #else
293 execvp (*argv, argv);
294 #endif
295 // there is no return after successful exec()
296 wxLogSysError(_("Can't execute '%s'"), *argv);
297
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;
322 }
323 };
324
325 long wxExecute( const wxString& command, bool sync, wxProcess *process )
326 {
327 static const char *IFS = " \t\n";
328
329 wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" );
330
331 int argc = 0;
332 char *argv[127];
333 char *tmp = new char[command.Len() + 1];
334 strcpy(tmp, command);
335
336 argv[argc++] = strtok(tmp, IFS);
337 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
338 /* loop */ ;
339
340 long lRc = wxExecute(argv, sync, process);
341
342 delete [] tmp;
343
344 return lRc;
345 };