]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/utilsgtk.cpp
Dnd constant name changes because names like Copy can be #defined by other
[wxWidgets.git] / src / gtk1 / utilsgtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 //#ifdef __GNUG__
13 //#pragma implementation "utils.h"
14 //#endif
15
16 #include "wx/utils.h"
17 #include "wx/string.h"
18
19 #include <stdarg.h>
20 #include <dirent.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <sys/wait.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <netdb.h>
29 #include <signal.h>
30
31 #ifdef __SVR4__
32 #include <sys/systeminfo.h>
33 #endif
34
35 //------------------------------------------------------------------------
36 // misc.
37 //------------------------------------------------------------------------
38
39 void wxBell(void)
40 {
41 gdk_beep();
42 };
43
44 void wxSleep(int nSecs)
45 {
46 sleep(nSecs);
47 };
48
49 int wxKill(long pid, int sig)
50 {
51 return kill(pid, sig);
52 };
53
54 void wxDisplaySize( int *width, int *height )
55 {
56 if (width) *width = gdk_screen_width();
57 if (height) *height = gdk_screen_height();
58 }
59
60 void wxGetMousePosition( int* x, int* y )
61 {
62 wxFAIL_MSG( "GetMousePosition not yet implemented" );
63 if (x) *x = 0;
64 if (y) *y = 0;
65 };
66
67 bool wxColourDisplay(void)
68 {
69 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
70 return TRUE;
71 }
72
73 int wxDisplayDepth(void)
74 {
75 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
76 return 8;
77 }
78
79 //------------------------------------------------------------------------
80 // user and home routines
81 //------------------------------------------------------------------------
82
83 const char* wxGetHomeDir( wxString *home )
84 {
85 *home = wxGetUserHome( wxString() );
86 if (home->IsNull()) *home = "/";
87 return *home;
88 };
89
90 char *wxGetUserHome( const wxString &user )
91 {
92 struct passwd *who = (struct passwd *) NULL;
93
94 if (user.IsNull() || (user== ""))
95 {
96 register char *ptr;
97
98 if ((ptr = getenv("HOME")) != NULL)
99 return ptr;
100 if ((ptr = getenv("USER")) != NULL
101 || (ptr = getenv("LOGNAME")) != NULL) {
102 who = getpwnam(ptr);
103 }
104 // We now make sure the the user exists!
105 if (who == NULL)
106 who = getpwuid(getuid());
107 }
108 else
109 who = getpwnam (user);
110
111 return who ? who->pw_dir : (char*)NULL;
112 };
113
114 //------------------------------------------------------------------------
115 // id routines
116 //------------------------------------------------------------------------
117
118 bool wxGetHostName(char *buf, int sz)
119 {
120 *buf = '\0';
121 #if defined(__SVR4__) && !defined(__sgi)
122 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
123 #else /* BSD Sockets */
124 char name[255];
125 struct hostent *h;
126 // Get hostname
127 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
128 return FALSE;
129 // Get official full name of host
130 strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
131 return TRUE;
132 #endif
133 }
134
135 bool wxGetUserId(char *buf, int sz)
136 {
137 struct passwd *who;
138
139 *buf = '\0';
140 if ((who = getpwuid(getuid ())) != NULL) {
141 strncpy (buf, who->pw_name, sz-1);
142 return TRUE;
143 }
144 return FALSE;
145 }
146
147 bool wxGetUserName(char *buf, int sz)
148 {
149 struct passwd *who;
150
151 *buf = '\0';
152 if ((who = getpwuid (getuid ())) != NULL) {
153 strncpy (buf, who->pw_gecos, sz - 1);
154 return TRUE;
155 }
156 return FALSE;
157 }
158
159 //------------------------------------------------------------------------
160 // error and debug output routines
161 //------------------------------------------------------------------------
162
163 void wxDebugMsg( const char *format, ... )
164 {
165 va_list ap;
166 va_start( ap, format );
167 vfprintf( stderr, format, ap );
168 fflush( stderr );
169 va_end(ap);
170 };
171
172 void wxError( const wxString &msg, const wxString &title )
173 {
174 fprintf( stderr, "Error " );
175 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
176 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
177 fprintf( stderr, ".\n" );
178 };
179
180 void wxFatalError( const wxString &msg, const wxString &title )
181 {
182 fprintf( stderr, "Error " );
183 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
184 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
185 fprintf( stderr, ".\n" );
186 exit(1);
187 };
188
189 //------------------------------------------------------------------------
190 // directory routines
191 //------------------------------------------------------------------------
192
193 bool wxDirExists( const wxString& dir )
194 {
195 char buf[500];
196 strcpy( buf, WXSTRINGCAST(dir) );
197 struct stat sbuf;
198 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
199 };
200
201 //------------------------------------------------------------------------
202 // subprocess routines
203 //------------------------------------------------------------------------
204
205 typedef struct {
206 gint pid, tag;
207 wxProcess *process;
208 } wxEndProcessData;
209
210 static void GTK_EndProcessDetector(gpointer data, gint source,
211 GdkInputCondition WXUNUSED(condition) )
212 {
213 wxEndProcessData *proc_data = (wxEndProcessData *)data;
214 int pid;
215
216 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
217
218 /* wait4 is not part of any standard, use at own risk
219 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
220 * --- offer@sgi.com */
221 #if !defined(__sgi)
222 wait4(proc_data->pid, NULL, 0, NULL);
223 #else
224 wait3((int *) NULL, 0, (rusage *) NULL);
225 #endif
226
227 close(source);
228 gdk_input_remove(proc_data->tag);
229
230 if (proc_data->process)
231 proc_data->process->OnTerminate(proc_data->pid);
232
233 if (proc_data->pid > 0)
234 delete proc_data;
235 else
236 proc_data->pid = 0;
237 };
238
239 long wxExecute( char **argv, bool sync, wxProcess *process )
240 {
241 wxEndProcessData *data = new wxEndProcessData;
242 int end_proc_detect[2];
243
244 if (*argv == NULL)
245 return 0;
246
247 /* Create pipes */
248 if (pipe(end_proc_detect) == -1) {
249 perror("pipe failed");
250 return 0;
251 }
252
253 /* fork the process */
254 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
255 pid_t pid = vfork();
256 #else
257 pid_t pid = fork();
258 #endif
259 if (pid == -1) {
260 perror ("fork failed");
261 return 0;
262 } else if (pid == 0) {
263 /* Close fd not useful */
264 close(end_proc_detect[0]); // close reading side
265
266 /* child */
267 #ifdef _AIX
268 execvp ((const char *)*argv, (const char **)argv);
269 #else
270 execvp (*argv, argv);
271 #endif
272 if (errno == ENOENT)
273 wxError("command not found", *argv);
274 else
275 perror (*argv);
276 wxError("could not execute", *argv);
277 _exit (-1);
278 }
279
280 close(end_proc_detect[1]); // close writing side
281 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
282 GTK_EndProcessDetector, (gpointer)data);
283 data->pid = pid;
284 if (!sync) {
285 data->process = process;
286 } else {
287 data->process = (wxProcess *) NULL;
288 data->pid = -(data->pid);
289
290 while (data->pid != 0)
291 wxYield();
292
293 delete data;
294 }
295
296 return pid;
297 };
298
299 long wxExecute( const wxString& command, bool sync, wxProcess *process )
300 {
301 if (command.IsNull() || command == "") return FALSE;
302
303 int argc = 0;
304 char *argv[127];
305 char tmp[1024];
306 const char *IFS = " \t\n";
307
308 strncpy (tmp, command, sizeof(tmp) / sizeof(char) - 1);
309 tmp[sizeof (tmp) / sizeof (char) - 1] = '\0';
310 argv[argc++] = strtok (tmp, IFS);
311 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
312 /* loop */ ;
313 return wxExecute(argv, sync, process);
314 };
315
316