1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/os2/private.h"
24 #include "wx/apptrait.h"
49 static const wxChar WX_SECTION
[] = _T("wxWindows");
50 static const wxChar eHOSTNAME
[] = _T("HostName");
51 static const wxChar eUSERID
[] = _T("UserId");
52 static const wxChar eUSERNAME
[] = _T("UserName");
54 // For the following functions we SHOULD fill in support
55 // for Windows-NT (which I don't know) as I assume it begin
56 // a POSIX Unix (so claims MS) that it has some special
57 // functions beyond those provided by WinSock
59 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
68 unsigned long ulLevel
= 0;
69 unsigned char* zBuffer
= NULL
;
70 unsigned long ulBuffer
= 256;
71 unsigned long* pulTotalAvail
= NULL
;
73 NetBios32GetInfo( (const unsigned char*)zServer
74 ,(const unsigned char*)zComputer
80 strcpy(zBuf
, zServer
);
83 const wxChar
* zDefaultHost
= _T("noname");
85 if ((zSysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
87 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
96 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
97 zBuf
[nMaxSize
] = _T('\0');
99 return *zBuf
? TRUE
: FALSE
;
102 // Get user ID e.g. jacs
108 #if defined(__VISAGECPP__)
110 // UPM procs return 0 on success
111 lrc
= U32ELOCU((unsigned char*)zBuf
, (unsigned long *)&nType
);
112 if (lrc
== 0) return TRUE
;
127 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
135 , wxKillError
* peError
138 return((int)::DosKillProcess(0, (PID
)lPid
));
142 // Execute a program in an Interactive Shell
145 const wxString
& rCommand
148 wxChar
* zShell
= _T("CMD.EXE");
151 STARTDATA SData
= {0};
152 PSZ PgmTitle
= "Command Shell";
156 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
159 SData
.Length
= sizeof(STARTDATA
);
160 SData
.Related
= SSF_RELATED_INDEPENDENT
;
161 SData
.FgBg
= SSF_FGBG_FORE
;
162 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
163 SData
.PgmTitle
= PgmTitle
;
164 SData
.PgmName
= zShell
;
166 sInputs
= "/C " + rCommand
;
167 SData
.PgmInputs
= (BYTE
*)sInputs
.c_str();
169 SData
.Environment
= 0;
170 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
171 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
174 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
177 SData
.InitXSize
= 200;
178 SData
.InitYSize
= 140;
180 SData
.ObjectBuffer
= (char*)achObjBuf
;
181 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
183 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
184 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
189 ::DosGetInfoBlocks(&ptib
, &ppib
);
191 ::DosWaitChild( DCWA_PROCESS
201 // Shutdown or reboot the PC
202 bool wxShutdown(wxShutdownFlags wFlags
)
208 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
209 long wxGetFreeMemory()
211 void* pMemptr
= NULL
;
216 lMemFlags
= PAG_FREE
;
217 rc
= ::DosQueryMem(pMemptr
, &lSize
, &lMemFlags
);
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
227 bool wxGetEnv(const wxString
& var
, wxString
*value
)
229 // wxGetenv is defined as getenv()
230 wxChar
*p
= wxGetenv(var
);
242 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
244 #if defined(HAVE_SETENV)
245 return setenv(variable
.mb_str(), value
? wxString(value
).mb_str().data()
246 : NULL
, 1 /* overwrite */) == 0;
247 #elif defined(HAVE_PUTENV)
248 wxString s
= variable
;
250 s
<< _T('=') << value
;
253 const char *p
= s
.mb_str();
255 // the string will be free()d by libc
256 char *buf
= (char *)malloc(strlen(p
) + 1);
259 return putenv(buf
) == 0;
260 #else // no way to set an env var
266 unsigned long ulMilliseconds
269 ::DosSleep(ulMilliseconds
);
276 ::DosSleep(1000 * nSecs
);
279 // Consume all events until no more left
285 #if WXWIN_COMPATIBILITY_2_2
287 // Output a debug mess., in a system dependent fashion.
289 const wxChar
* zFmt
...
293 static wxChar zBuffer
[512];
295 if (!wxTheApp
->GetWantDebugOutput())
298 sprintf(zBuffer
, zFmt
, vAp
) ;
302 // Non-fatal error: pop up message box and (possibly) continue
305 , const wxString
& rTitle
308 wxBuffer
= new wxChar
[256];
309 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
310 if (::WinMessageBox( HWND_DESKTOP
313 ,(PSZ
)WXSTRINGCAST rTitle
315 ,MB_ICONEXCLAMATION
| MB_YESNO
321 // Fatal error: pop up message box and abort
324 , const wxString
& rTitle
329 ulRc
= ::WinMessageBox( HWND_DESKTOP
336 DosExit(EXIT_PROCESS
, ulRc
);
339 #endif // WXWIN_COMPATIBILITY_2_2
344 DosBeep(1000,1000); // 1kHz during 1 sec.
347 int wxGUIAppTraits::GetOSVersion(
352 ULONG ulSysInfo
[QSV_MAX
] = {0};
355 ulrc
= ::DosQuerySysInfo( 1L
358 ,sizeof(ULONG
) * QSV_MAX
362 *pMajorVsn
= ulSysInfo
[QSV_VERSION_MAJOR
];
363 *pMajorVsn
= *pMajorVsn
/10;
364 *pMinorVsn
= ulSysInfo
[QSV_VERSION_MINOR
];
365 return wxWINDOWS_OS2
;
367 return wxWINDOWS
; // error if we get here, return generic value
370 // ---------------------------------------------------------------------------
371 const wxChar
* wxGetHomeDir(
375 wxString
& rStrDir
= *pStr
;
377 // OS/2 has no idea about home,
378 // so use the working directory instead?
380 // 256 was taken from os2def.h
382 # define MAX_PATH 256
388 ::DosQueryCurrentDir(0, zDirName
, &ulDirLen
);
390 return rStrDir
.c_str();
394 wxChar
* wxGetUserHome (
395 const wxString
& rUser
399 wxString
sUser1(rUser
);
401 wxBuffer
= new wxChar
[256];
403 if (sUser1
!= _T(""))
407 if (wxGetUserId( zTmp
408 ,sizeof(zTmp
)/sizeof(char)
411 // Guests belong in the temp dir
412 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
414 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
415 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
416 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
418 return *zHome
? zHome
: (wxChar
*)_T("\\");
420 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
425 if (sUser1
== _T(""))
427 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
429 wxStrcpy(wxBuffer
, zHome
);
430 wxUnix2DosFilename(wxBuffer
);
431 wxStrcpy(zHome
, wxBuffer
);
437 return NULL
; // No home known!
444 return (::DosSetCurrentDir(WXSTRINGCAST rDir
));
447 wxString WXDLLEXPORT
wxPMErrorToStr(
454 // Remove the high order byte -- it is useless
456 vError
&= 0x0000ffff;
459 case PMERR_INVALID_HWND
:
460 sError
= wxT("Invalid window handle specified");
463 case PMERR_INVALID_FLAG
:
464 sError
= wxT("Invalid flag bit set");
467 case PMERR_NO_MSG_QUEUE
:
468 sError
= wxT("No message queue available");
471 case PMERR_INVALID_PARM
:
472 sError
= wxT("Parameter contained invalid data");
475 case PMERR_INVALID_PARAMETERS
:
476 sError
= wxT("Parameter value is out of range");
479 case PMERR_PARAMETER_OUT_OF_RANGE
:
480 sError
= wxT("Parameter value is out of range");
483 case PMERR_INVALID_INTEGER_ATOM
:
484 sError
= wxT("Not a valid atom");
487 case PMERR_INVALID_HATOMTBL
:
488 sError
= wxT("Atom table handle is invalid");
491 case PMERR_INVALID_ATOM_NAME
:
492 sError
= wxT("Not a valid atom name");
495 case PMERR_ATOM_NAME_NOT_FOUND
:
496 sError
= wxT("Valid name format, but cannot find name in atom table");
500 sError
= wxT("Unknown error");
503 } // end of wxPMErrorToStr