]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/utilsgtk.cpp
wxFrame size bug
[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 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
90 return TRUE;
91 }
92
93 int wxDisplayDepth(void)
94 {
95 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
96 return 8;
97 }
98
99 //------------------------------------------------------------------------
100 // user and home routines
101 //------------------------------------------------------------------------
102
103 const char* wxGetHomeDir( wxString *home )
104 {
105 *home = wxGetUserHome( wxString() );
106 if (home->IsNull()) *home = "/";
107 return *home;
108 }
109
110 char *wxGetUserHome( const wxString &user )
111 {
112 struct passwd *who = (struct passwd *) NULL;
113
114 if (user.IsNull() || (user== ""))
115 {
116 register char *ptr;
117
118 if ((ptr = getenv("HOME")) != NULL)
119 {
120 return ptr;
121 }
122 if ((ptr = getenv("USER")) != NULL || (ptr = getenv("LOGNAME")) != NULL)
123 {
124 who = getpwnam(ptr);
125 }
126 // We now make sure the the user exists!
127 if (who == NULL)
128 {
129 who = getpwuid(getuid());
130 }
131 }
132 else
133 {
134 who = getpwnam (user);
135 }
136
137 return who ? who->pw_dir : (char*)NULL;
138 }
139
140 //------------------------------------------------------------------------
141 // id routines
142 //------------------------------------------------------------------------
143
144 bool wxGetHostName(char *buf, int sz)
145 {
146 *buf = '\0';
147 #if defined(__SVR4__) && !defined(__sgi)
148 //KB: does this return the fully qualified host.domain name?
149 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
150 #else /* BSD Sockets */
151 char name[255], domain[255];
152 //struct hostent *h;
153 // Get hostname
154 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
155 return FALSE;
156 if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1)
157 return FALSE;
158 // Get official full name of host
159 // doesn't return the full qualified name, replaced by following
160 // code (KB)
161 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
162 if((unsigned)sz > strlen(name)+strlen(domain)+1)
163 {
164 strcpy(buf, name);
165 if(strcmp(domain,"(none)") == 0) // standalone machine
166 {
167 strcat(buf,".");
168 strcat(buf,domain);
169 }
170 }
171 else
172 return FALSE;
173 return TRUE;
174 #endif
175 }
176
177 bool wxGetUserId(char *buf, int sz)
178 {
179 struct passwd *who;
180
181 *buf = '\0';
182 if ((who = getpwuid(getuid ())) != NULL) {
183 strncpy (buf, who->pw_name, sz-1);
184 return TRUE;
185 }
186 return FALSE;
187 }
188
189 bool wxGetUserName(char *buf, int sz)
190 {
191 struct passwd *who;
192 char *comma;
193
194 *buf = '\0';
195 if ((who = getpwuid (getuid ())) != NULL) {
196 comma = strchr(who->pw_gecos,'c');
197 if(comma) *comma = '\0'; // cut off non-name comment fields
198 strncpy (buf, who->pw_gecos, sz - 1);
199 return TRUE;
200 }
201 return FALSE;
202 }
203
204 //------------------------------------------------------------------------
205 // error and debug output routines
206 //------------------------------------------------------------------------
207
208 void wxDebugMsg( const char *format, ... )
209 {
210 va_list ap;
211 va_start( ap, format );
212 vfprintf( stderr, format, ap );
213 fflush( stderr );
214 va_end(ap);
215 };
216
217 void wxError( const wxString &msg, const wxString &title )
218 {
219 fprintf( stderr, "Error " );
220 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
221 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
222 fprintf( stderr, ".\n" );
223 };
224
225 void wxFatalError( const wxString &msg, const wxString &title )
226 {
227 fprintf( stderr, "Error " );
228 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
229 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
230 fprintf( stderr, ".\n" );
231 exit(3); // the same exit code as for abort()
232 };
233
234 //------------------------------------------------------------------------
235 // directory routines
236 //------------------------------------------------------------------------
237
238 bool wxDirExists( const wxString& dir )
239 {
240 char buf[500];
241 strcpy( buf, WXSTRINGCAST(dir) );
242 struct stat sbuf;
243 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
244 };
245
246 //------------------------------------------------------------------------
247 // subprocess routines
248 //------------------------------------------------------------------------
249
250 struct wxEndProcessData
251 {
252 gint pid, tag;
253 wxProcess *process;
254 };
255
256 static void GTK_EndProcessDetector(gpointer data, gint source,
257 GdkInputCondition WXUNUSED(condition) )
258 {
259 wxEndProcessData *proc_data = (wxEndProcessData *)data;
260 int pid;
261
262 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
263
264 /* wait4 is not part of any standard, use at own risk
265 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
266 * --- offer@sgi.com */
267 #if !defined(__sgi)
268 wait4(proc_data->pid, NULL, 0, NULL);
269 #else
270 wait3((int *) NULL, 0, (rusage *) NULL);
271 #endif
272
273 close(source);
274 gdk_input_remove(proc_data->tag);
275
276 if (proc_data->process)
277 proc_data->process->OnTerminate(proc_data->pid);
278
279 if (proc_data->pid > 0)
280 delete proc_data;
281 else
282 proc_data->pid = 0;
283 };
284
285 long wxExecute( char **argv, bool sync, wxProcess *process )
286 {
287 wxEndProcessData *data = new wxEndProcessData;
288 int end_proc_detect[2];
289
290 wxCHECK_MSG( *argv, 0, "can't exec empty command" );
291
292 /* Create pipes */
293 if (pipe(end_proc_detect) == -1) {
294 wxLogSysError(_("Pipe creation failed"));
295 return 0;
296 }
297
298 /* fork the process */
299 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
300 pid_t pid = vfork();
301 #else
302 pid_t pid = fork();
303 #endif
304 if (pid == -1) {
305 // error
306 wxLogSysError(_("Fork failed"));
307 return 0;
308 }
309 else if (pid == 0) {
310 // we're in child
311 close(end_proc_detect[0]); // close reading side
312
313 #ifdef _AIX
314 execvp ((const char *)*argv, (const char **)argv);
315 #else
316 execvp (*argv, argv);
317 #endif
318 // there is no return after successful exec()
319 wxLogSysError(_("Can't execute '%s'"), *argv);
320
321 _exit(-1);
322 }
323 else {
324 // we're in parent
325 close(end_proc_detect[1]); // close writing side
326 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
327 GTK_EndProcessDetector, (gpointer)data);
328 data->pid = pid;
329 if (!sync) {
330 data->process = process;
331 }
332 else {
333 data->process = (wxProcess *) NULL;
334 data->pid = -(data->pid);
335
336 while (data->pid != 0)
337 wxYield();
338
339 delete data;
340 }
341
342 // @@@ our return value indicates success even if execvp() in the child
343 // failed!
344 return pid;
345 }
346 };
347
348 long wxExecute( const wxString& command, bool sync, wxProcess *process )
349 {
350 static const char *IFS = " \t\n";
351
352 wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" );
353
354 int argc = 0;
355 char *argv[127];
356 char *tmp = new char[command.Len() + 1];
357 strcpy(tmp, command);
358
359 argv[argc++] = strtok(tmp, IFS);
360 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
361 /* loop */ ;
362
363 long lRc = wxExecute(argv, sync, process);
364
365 delete [] tmp;
366
367 return lRc;
368 };