]> git.saurik.com Git - wxWidgets.git/blob - src/os2/utils.cpp
Unified with Unix version.
[wxWidgets.git] / src / os2 / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose: Various utilities
4 // Author: David Webster
5 // Modified by:
6 // Created: 09/17/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/setup.h"
17 #include "wx/utils.h"
18 #include "wx/app.h"
19 #endif //WX_PRECOMP
20
21 #include "wx/os2/private.h"
22 #include "wx/intl.h"
23 #include "wx/apptrait.h"
24
25 #include <ctype.h>
26 #ifdef __EMX__
27 #include <dirent.h>
28 #endif
29
30 #include "wx/log.h"
31
32 #include <io.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <stdarg.h>
39
40 #define PURE_32
41
42 #ifndef __EMX__
43 #include <upm.h>
44 #include <netcons.h>
45 #include <netbios.h>
46 #endif
47
48 static const wxChar WX_SECTION[] = _T("wxWindows");
49 static const wxChar eHOSTNAME[] = _T("HostName");
50 static const wxChar eUSERID[] = _T("UserId");
51 static const wxChar eUSERNAME[] = _T("UserName");
52
53 // For the following functions we SHOULD fill in support
54 // for Windows-NT (which I don't know) as I assume it begin
55 // a POSIX Unix (so claims MS) that it has some special
56 // functions beyond those provided by WinSock
57
58 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
59 bool wxGetHostName(
60 wxChar* zBuf
61 , int nMaxSize
62 )
63 {
64 #if wxUSE_NET_API
65 char zServer[256];
66 char zComputer[256];
67 unsigned long ulLevel = 0;
68 unsigned char* zBuffer = NULL;
69 unsigned long ulBuffer = 256;
70 unsigned long* pulTotalAvail = NULL;
71
72 NetBios32GetInfo( (const unsigned char*)zServer
73 ,(const unsigned char*)zComputer
74 ,ulLevel
75 ,zBuffer
76 ,ulBuffer
77 ,pulTotalAvail
78 );
79 strcpy(zBuf, zServer);
80 #else
81 wxChar* zSysname;
82 const wxChar* zDefaultHost = _T("noname");
83
84 if ((zSysname = wxGetenv(_T("SYSTEM_NAME"))) == NULL)
85 {
86 ULONG n = ::PrfQueryProfileString( HINI_PROFILE
87 ,(PSZ)WX_SECTION
88 ,(PSZ)eHOSTNAME
89 ,(PSZ)zDefaultHost
90 ,(void*)zBuf
91 ,(ULONG)nMaxSize - 1
92 );
93 }
94 else
95 wxStrncpy(zBuf, zSysname, nMaxSize - 1);
96 zBuf[nMaxSize] = _T('\0');
97 #endif
98 return *zBuf ? TRUE : FALSE;
99 }
100
101 // Get user ID e.g. jacs
102 bool wxGetUserId(
103 wxChar* zBuf
104 , int nType
105 )
106 {
107 #if defined(__VISAGECPP__)
108 long lrc;
109 // UPM procs return 0 on success
110 lrc = U32ELOCU((unsigned char*)zBuf, (unsigned long *)&nType);
111 if (lrc == 0) return TRUE;
112 #endif
113 return FALSE;
114 }
115
116 bool wxGetUserName(
117 wxChar* zBuf
118 , int nMaxSize
119 )
120 {
121 #ifdef USE_NET_API
122 wxGetUserId( zBuf
123 ,nMaxSize
124 );
125 #else
126 wxStrncpy(zBuf, _T("Unknown User"), nMaxSize);
127 #endif
128 return TRUE;
129 }
130
131 int wxKill(
132 long lPid
133 , wxSignal eSig
134 , wxKillError* peError
135 )
136 {
137 return((int)::DosKillProcess(0, (PID)lPid));
138 }
139
140 //
141 // Execute a program in an Interactive Shell
142 //
143 bool wxShell(
144 const wxString& rCommand
145 )
146 {
147 wxChar* zShell = _T("CMD.EXE");
148 wxString sInputs;
149 STARTDATA SData = {0};
150 PSZ PgmTitle = "Command Shell";
151 APIRET rc;
152 PID vPid = 0;
153 ULONG ulSessID = 0;
154 UCHAR achObjBuf[256] = {0}; //error data if DosStart fails
155 RESULTCODES vResult;
156
157 SData.Length = sizeof(STARTDATA);
158 SData.Related = SSF_RELATED_INDEPENDENT;
159 SData.FgBg = SSF_FGBG_FORE;
160 SData.TraceOpt = SSF_TRACEOPT_NONE;
161 SData.PgmTitle = PgmTitle;
162 SData.PgmName = zShell;
163
164 sInputs = "/C " + rCommand;
165 SData.PgmInputs = (BYTE*)sInputs.c_str();
166 SData.TermQ = 0;
167 SData.Environment = 0;
168 SData.InheritOpt = SSF_INHERTOPT_SHELL;
169 SData.SessionType = SSF_TYPE_WINDOWABLEVIO;
170 SData.IconFile = 0;
171 SData.PgmHandle = 0;
172 SData.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_MAXIMIZE;
173 SData.InitXPos = 30;
174 SData.InitYPos = 40;
175 SData.InitXSize = 200;
176 SData.InitYSize = 140;
177 SData.Reserved = 0;
178 SData.ObjectBuffer = (char*)achObjBuf;
179 SData.ObjectBuffLen = (ULONG)sizeof(achObjBuf);
180
181 rc = ::DosStartSession(&SData, &ulSessID, &vPid);
182 if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND
183 {
184 PTIB ptib;
185 PPIB ppib;
186
187 ::DosGetInfoBlocks(&ptib, &ppib);
188
189 ::DosWaitChild( DCWA_PROCESS
190 ,DCWW_WAIT
191 ,&vResult
192 ,&ppib->pib_ulpid
193 ,vPid
194 );
195 }
196 return (rc != 0);
197 }
198
199 // Shutdown or reboot the PC
200 bool wxShutdown(wxShutdownFlags wFlags)
201 {
202 // TODO
203 return FALSE;
204 }
205
206 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
207 long wxGetFreeMemory()
208 {
209 void* pMemptr = NULL;
210 ULONG lSize;
211 ULONG lMemFlags;
212 APIRET rc;
213
214 lMemFlags = PAG_FREE;
215 rc = ::DosQueryMem(pMemptr, &lSize, &lMemFlags);
216 if (rc != 0)
217 return -1L;
218 return (long)lSize;
219 }
220
221 // ----------------------------------------------------------------------------
222 // env vars
223 // ----------------------------------------------------------------------------
224
225 bool wxGetEnv(const wxString& var, wxString *value)
226 {
227 // wxGetenv is defined as getenv()
228 wxChar *p = wxGetenv(var);
229 if ( !p )
230 return FALSE;
231
232 if ( value )
233 {
234 *value = p;
235 }
236
237 return TRUE;
238 }
239
240 bool wxSetEnv(const wxString& variable, const wxChar *value)
241 {
242 #if defined(HAVE_SETENV)
243 return setenv(variable.mb_str(), value ? wxString(value).mb_str().data()
244 : NULL, 1 /* overwrite */) == 0;
245 #elif defined(HAVE_PUTENV)
246 wxString s = variable;
247 if ( value )
248 s << _T('=') << value;
249
250 // transform to ANSI
251 const char *p = s.mb_str();
252
253 // the string will be free()d by libc
254 char *buf = (char *)malloc(strlen(p) + 1);
255 strcpy(buf, p);
256
257 return putenv(buf) == 0;
258 #else // no way to set an env var
259 return FALSE;
260 #endif
261 }
262
263 void wxUsleep(
264 unsigned long ulMilliseconds
265 )
266 {
267 ::DosSleep(ulMilliseconds);
268 }
269
270 void wxSleep(
271 int nSecs
272 )
273 {
274 ::DosSleep(1000 * nSecs);
275 }
276
277 // Consume all events until no more left
278 void wxFlushEvents()
279 {
280 // wxYield();
281 }
282
283 #if WXWIN_COMPATIBILITY_2_2
284
285 // Output a debug mess., in a system dependent fashion.
286 void wxDebugMsg(
287 const wxChar* zFmt ...
288 )
289 {
290 va_list vAp;
291 static wxChar zBuffer[512];
292
293 if (!wxTheApp->GetWantDebugOutput())
294 return ;
295 va_start(vAp, zFmt);
296 sprintf(zBuffer, zFmt, vAp) ;
297 va_end(vAp);
298 }
299
300 // Non-fatal error: pop up message box and (possibly) continue
301 void wxError(
302 const wxString& rMsg
303 , const wxString& rTitle
304 )
305 {
306 wxBuffer = new wxChar[256];
307 wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST rMsg);
308 if (::WinMessageBox( HWND_DESKTOP
309 ,NULL
310 ,(PSZ)wxBuffer
311 ,(PSZ)WXSTRINGCAST rTitle
312 ,0
313 ,MB_ICONEXCLAMATION | MB_YESNO
314 ) == MBID_YES)
315 delete[] wxBuffer;
316 wxExit();
317 }
318
319 // Fatal error: pop up message box and abort
320 void wxFatalError(
321 const wxString& rMsg
322 , const wxString& rTitle
323 )
324 {
325 unsigned long ulRc;
326
327 ulRc = ::WinMessageBox( HWND_DESKTOP
328 ,NULL
329 ,WXSTRINGCAST rMsg
330 ,WXSTRINGCAST rTitle
331 ,0
332 ,MB_NOICON | MB_OK
333 );
334 DosExit(EXIT_PROCESS, ulRc);
335 }
336
337 #endif // WXWIN_COMPATIBILITY_2_2
338
339 // Emit a beeeeeep
340 void wxBell()
341 {
342 DosBeep(1000,1000); // 1kHz during 1 sec.
343 }
344
345 wxToolkitInfo & wxConsoleAppTraits::GetToolkitInfo()
346 {
347 static wxToolkitInfo vInfo;
348 ULONG ulSysInfo[QSV_MAX] = {0};
349 APIRET ulrc;
350
351 vInfo.name = _T("wxBase");
352 ulrc = ::DosQuerySysInfo( 1L
353 ,QSV_MAX
354 ,(PVOID)ulSysInfo
355 ,sizeof(ULONG) * QSV_MAX
356 );
357 if (ulrc == 0L)
358 {
359 vInfo.versionMajor = ulSysInfo[QSV_VERSION_MAJOR] / 10;
360 vInfo.versionMinor = ulSysInfo[QSV_VERSION_MINOR];
361 }
362 vInfo.os = wxOS2_PM;
363 return vInfo;
364 }
365
366 // ---------------------------------------------------------------------------
367 const wxChar* wxGetHomeDir(
368 wxString* pStr
369 )
370 {
371 wxString& rStrDir = *pStr;
372
373 // OS/2 has no idea about home,
374 // so use the working directory instead?
375
376 // 256 was taken from os2def.h
377 #ifndef MAX_PATH
378 # define MAX_PATH 256
379 #endif
380
381 char zDirName[256];
382 ULONG ulDirLen;
383
384 ::DosQueryCurrentDir(0, zDirName, &ulDirLen);
385 rStrDir = zDirName;
386 return rStrDir.c_str();
387 }
388
389 // Hack for OS/2
390 wxChar* wxGetUserHome (
391 const wxString& rUser
392 )
393 {
394 wxChar* zHome;
395 wxString sUser1(rUser);
396
397 wxBuffer = new wxChar[256];
398 #ifndef __EMX__
399 if (sUser1 != _T(""))
400 {
401 wxChar zTmp[64];
402
403 if (wxGetUserId( zTmp
404 ,sizeof(zTmp)/sizeof(char)
405 ))
406 {
407 // Guests belong in the temp dir
408 if (wxStricmp(zTmp, _T("annonymous")) == 0)
409 {
410 if ((zHome = wxGetenv(_T("TMP"))) != NULL ||
411 (zHome = wxGetenv(_T("TMPDIR"))) != NULL ||
412 (zHome = wxGetenv(_T("TEMP"))) != NULL)
413 delete[] wxBuffer;
414 return *zHome ? zHome : (wxChar*)_T("\\");
415 }
416 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
417 sUser1 = _T("");
418 }
419 }
420 #endif
421 if (sUser1 == _T(""))
422 {
423 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
424 {
425 wxStrcpy(wxBuffer, zHome);
426 wxUnix2DosFilename(wxBuffer);
427 wxStrcpy(zHome, wxBuffer);
428 delete[] wxBuffer;
429 return zHome;
430 }
431 }
432 delete[] wxBuffer;
433 return NULL; // No home known!
434 }
435
436 bool wxDirExists(
437 const wxString& rDir
438 )
439 {
440 return (::DosSetCurrentDir(WXSTRINGCAST rDir));
441 }
442
443 wxString WXDLLEXPORT wxPMErrorToStr(
444 ERRORID vError
445 )
446 {
447 wxString sError;
448
449 //
450 // Remove the high order byte -- it is useless
451 //
452 vError &= 0x0000ffff;
453 switch(vError)
454 {
455 case PMERR_INVALID_HWND:
456 sError = wxT("Invalid window handle specified");
457 break;
458
459 case PMERR_INVALID_FLAG:
460 sError = wxT("Invalid flag bit set");
461 break;
462
463 case PMERR_NO_MSG_QUEUE:
464 sError = wxT("No message queue available");
465 break;
466
467 case PMERR_INVALID_PARM:
468 sError = wxT("Parameter contained invalid data");
469 break;
470
471 case PMERR_INVALID_PARAMETERS:
472 sError = wxT("Parameter value is out of range");
473 break;
474
475 case PMERR_PARAMETER_OUT_OF_RANGE:
476 sError = wxT("Parameter value is out of range");
477 break;
478
479 case PMERR_INVALID_INTEGER_ATOM:
480 sError = wxT("Not a valid atom");
481 break;
482
483 case PMERR_INVALID_HATOMTBL:
484 sError = wxT("Atom table handle is invalid");
485 break;
486
487 case PMERR_INVALID_ATOM_NAME:
488 sError = wxT("Not a valid atom name");
489 break;
490
491 case PMERR_ATOM_NAME_NOT_FOUND:
492 sError = wxT("Valid name format, but cannot find name in atom table");
493 break;
494
495 default:
496 sError = wxT("Unknown error");
497 }
498 return(sError);
499 } // end of wxPMErrorToStr
500
501 // replacement for implementation in unix/utilsunx.cpp,
502 // to be used by all X11 based ports.
503 struct wxEndProcessData;
504
505 void wxHandleProcessTermination(wxEndProcessData *proc_data)
506 {
507 // For now, just do nothing. To be filled in as needed.
508 }