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"
23 #include "wx/apptrait.h"
42 #if defined(__WATCOMC__)
47 #elif !defined(__EMX__)
53 static const wxChar WX_SECTION
[] = _T("wxWidgets");
54 static const wxChar eHOSTNAME
[] = _T("HostName");
56 // For the following functions we SHOULD fill in support
57 // for Windows-NT (which I don't know) as I assume it begin
58 // a POSIX Unix (so claims MS) that it has some special
59 // functions beyond those provided by WinSock
61 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
70 unsigned long ulLevel
= 0;
71 unsigned char* zBuffer
= NULL
;
72 unsigned long ulBuffer
= 256;
73 unsigned long* pulTotalAvail
= NULL
;
75 NetBios32GetInfo( (const unsigned char*)zServer
76 ,(const unsigned char*)zComputer
82 strcpy(zBuf
, zServer
);
85 const wxChar
* zDefaultHost
= _T("noname");
87 if ((zSysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
89 ::PrfQueryProfileString( HINI_PROFILE
98 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
99 zBuf
[nMaxSize
] = _T('\0');
101 return *zBuf
? true : false;
104 // Get user ID e.g. jacs
105 bool wxGetUserId(wxChar
* zBuf
, int nType
)
107 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
108 // UPM procs return 0 on success
109 long lrc
= U32ELOCU((PUCHAR
)zBuf
, (PULONG
)&nType
);
110 if (lrc
== 0) return true;
115 bool wxGetUserName( wxChar
* zBuf
, int nMaxSize
)
118 wxGetUserId( zBuf
, nMaxSize
);
120 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
125 int wxKill(long lPid
,
126 wxSignal
WXUNUSED(eSig
),
127 wxKillError
* WXUNUSED(peError
),
130 return((int)::DosKillProcess(0, (PID
)lPid
));
134 // Execute a program in an Interactive Shell
137 const wxString
& rCommand
140 wxChar
* zShell
= _T("CMD.EXE");
142 STARTDATA SData
= {0};
143 PSZ PgmTitle
= "Command Shell";
147 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
150 SData
.Length
= sizeof(STARTDATA
);
151 SData
.Related
= SSF_RELATED_INDEPENDENT
;
152 SData
.FgBg
= SSF_FGBG_FORE
;
153 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
154 SData
.PgmTitle
= PgmTitle
;
155 SData
.PgmName
= (char*)zShell
;
157 sInputs
= _T("/C ") + rCommand
;
158 SData
.PgmInputs
= (BYTE
*)sInputs
.c_str();
160 SData
.Environment
= 0;
161 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
162 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
165 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
168 SData
.InitXSize
= 200;
169 SData
.InitYSize
= 140;
171 SData
.ObjectBuffer
= (char*)achObjBuf
;
172 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
174 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
175 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
180 ::DosGetInfoBlocks(&ptib
, &ppib
);
182 ::DosWaitChild( DCWA_PROCESS
192 // Shutdown or reboot the PC
193 bool wxShutdown(wxShutdownFlags
WXUNUSED(wFlags
))
199 wxPowerType
wxGetPowerType()
202 return wxPOWER_UNKNOWN
;
205 wxBatteryState
wxGetBatteryState()
208 return wxBATTERY_UNKNOWN_STATE
;
211 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
212 wxMemorySize
wxGetFreeMemory()
214 void* pMemptr
= NULL
;
219 lMemFlags
= PAG_FREE
;
220 rc
= ::DosQueryMem(pMemptr
, (PULONG
)&lSize
, &lMemFlags
);
223 return (wxMemorySize
)lSize
;
227 unsigned long wxGetProcessId()
229 return (unsigned long)getpid();
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 bool wxGetEnv(const wxString
& var
, wxString
*value
)
238 // wxGetenv is defined as getenv()
239 wxChar
*p
= wxGetenv(var
);
251 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
253 #if defined(HAVE_SETENV)
254 return setenv(variable
.mb_str(), value
? wxString(value
).mb_str().data()
255 : NULL
, 1 /* overwrite */) == 0;
256 #elif defined(HAVE_PUTENV)
257 wxString s
= variable
;
259 s
<< _T('=') << value
;
262 const char *p
= s
.mb_str();
264 // the string will be free()d by libc
265 char *buf
= (char *)malloc(strlen(p
) + 1);
268 return putenv(buf
) == 0;
269 #else // no way to set an env var
270 wxUnusedVar(variable
);
277 unsigned long ulMilliseconds
280 ::DosSleep(ulMilliseconds
);
284 unsigned long ulMicroseconds
287 ::DosSleep(ulMicroseconds
/1000);
294 ::DosSleep(1000 * nSecs
);
297 // Consume all events until no more left
303 #if WXWIN_COMPATIBILITY_2_2
305 // Output a debug mess., in a system dependent fashion.
307 const wxChar
* zFmt
...
311 static wxChar zBuffer
[512];
313 if (!wxTheApp
->GetWantDebugOutput())
316 sprintf(zBuffer
, zFmt
, vAp
) ;
320 // Non-fatal error: pop up message box and (possibly) continue
323 , const wxString
& rTitle
326 wxChar
*wxBuffer
= new wxChar
[256];
327 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
328 if (::WinMessageBox( HWND_DESKTOP
331 ,(PSZ
)WXSTRINGCAST rTitle
333 ,MB_ICONEXCLAMATION
| MB_YESNO
339 // Fatal error: pop up message box and abort
342 , const wxString
& rTitle
347 ulRc
= ::WinMessageBox( HWND_DESKTOP
354 DosExit(EXIT_PROCESS
, ulRc
);
357 #endif // WXWIN_COMPATIBILITY_2_2
362 DosBeep(1000,1000); // 1kHz during 1 sec.
365 wxString
wxGetOsDescription()
367 wxString
strVer(_T("OS/2"));
368 ULONG ulSysInfo
[QSV_MAX
] = {0};
370 if (::DosQuerySysInfo( 1L,
373 sizeof(ULONG
) * QSV_MAX
377 ver
.Printf( _T(" ver. %d.%d rev. %c"),
378 int(ulSysInfo
[QSV_VERSION_MAJOR
] / 10),
379 int(ulSysInfo
[QSV_VERSION_MINOR
]),
380 char(ulSysInfo
[QSV_VERSION_REVISION
])
388 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab
))
392 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab
))
396 wxToolkitInfo
& wxConsoleAppTraits::GetToolkitInfo()
398 static wxToolkitInfo vInfo
;
399 ULONG ulSysInfo
[QSV_MAX
] = {0};
402 vInfo
.name
= _T("wxBase");
403 ulrc
= ::DosQuerySysInfo( 1L
406 ,sizeof(ULONG
) * QSV_MAX
410 vInfo
.versionMajor
= ulSysInfo
[QSV_VERSION_MAJOR
] / 10;
411 vInfo
.versionMinor
= ulSysInfo
[QSV_VERSION_MINOR
];
417 // ---------------------------------------------------------------------------
418 const wxChar
* wxGetHomeDir(
422 wxString
& rStrDir
= *pStr
;
424 // OS/2 has no idea about home,
425 // so use the working directory instead.
426 // However, we might have a valid HOME directory,
427 // as is used on many machines that have unix utilities
428 // on them, so we should use that, if available.
430 // 256 was taken from os2def.h
432 # define MAX_PATH 256
435 const wxChar
*szHome
= wxGetenv((wxChar
*)"HOME");
436 if ( szHome
== NULL
) {
437 // we're homeless, use current directory.
443 return rStrDir
.c_str();
448 const wxMB2WXbuf
wxGetUserHome( const wxString
&rUser
)
449 #else // just for binary compatibility -- there is no 'const' here
450 wxChar
* wxGetUserHome ( const wxString
&rUser
)
454 wxString
sUser1(rUser
);
456 wxChar
*wxBuffer
= new wxChar
[256];
462 if (wxGetUserId( zTmp
463 ,sizeof(zTmp
)/sizeof(char)
466 // Guests belong in the temp dir
467 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
469 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
470 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
471 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
473 return *zHome
? zHome
: (wxChar
*)_T("\\");
475 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
476 sUser1
= wxEmptyString
;
482 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
484 wxStrcpy(wxBuffer
, zHome
);
485 wxUnix2DosFilename(wxBuffer
);
487 wxWCharBuffer
retBuffer (wxBuffer
);
491 wxStrcpy(zHome
, wxBuffer
);
498 return (wxChar
*)wxEmptyString
; // No home known!
501 wxString WXDLLEXPORT
wxPMErrorToStr(ERRORID vError
)
506 // Remove the high order byte -- it is useless
508 vError
&= 0x0000ffff;
511 case PMERR_INVALID_HWND
:
512 sError
= wxT("Invalid window handle specified");
515 case PMERR_INVALID_FLAG
:
516 sError
= wxT("Invalid flag bit set");
519 case PMERR_NO_MSG_QUEUE
:
520 sError
= wxT("No message queue available");
523 case PMERR_INVALID_PARM
:
524 sError
= wxT("Parameter contained invalid data");
527 case PMERR_INVALID_PARAMETERS
:
528 sError
= wxT("Parameter value is out of range");
531 case PMERR_PARAMETER_OUT_OF_RANGE
:
532 sError
= wxT("Parameter value is out of range");
535 case PMERR_INVALID_INTEGER_ATOM
:
536 sError
= wxT("Not a valid atom");
539 case PMERR_INVALID_HATOMTBL
:
540 sError
= wxT("Atom table handle is invalid");
543 case PMERR_INVALID_ATOM_NAME
:
544 sError
= wxT("Not a valid atom name");
547 case PMERR_ATOM_NAME_NOT_FOUND
:
548 sError
= wxT("Valid name format, but cannot find name in atom table");
552 sError
= wxT("Unknown error");
555 } // end of wxPMErrorToStr
557 // replacement for implementation in unix/utilsunx.cpp,
558 // to be used by all X11 based ports.
559 struct wxEndProcessData
;
561 void wxHandleProcessTermination(wxEndProcessData
*WXUNUSED(proc_data
))
563 // For now, just do nothing. To be filled in as needed.