1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/utils.cpp
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"
23 #include "wx/os2/private.h"
24 #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)
62 bool wxGetHostName( wxChar
* zBuf
, int nMaxSize
)
64 if (!zBuf
) return false;
66 #if defined(wxUSE_NET_API) && wxUSE_NET_API
69 unsigned long ulLevel
= 0;
70 unsigned char* zBuffer
= NULL
;
71 unsigned long ulBuffer
= 256;
72 unsigned long* pulTotalAvail
= NULL
;
74 NetBios32GetInfo( (const unsigned char*)zServer
75 ,(const unsigned char*)zComputer
81 strcpy(zBuf
, zServer
);
84 const wxChar
* zDefaultHost
= _T("noname");
86 if ((zSysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
88 ::PrfQueryProfileString( HINI_PROFILE
98 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
101 zBuf
[nMaxSize
] = _T('\0');
104 return *zBuf
? true : false;
107 // Get user ID e.g. jacs
108 bool wxGetUserId(wxChar
* zBuf
, int nType
)
110 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
111 // UPM procs return 0 on success
112 long lrc
= U32ELOCU((PUCHAR
)zBuf
, (PULONG
)&nType
);
113 if (lrc
== 0) return true;
118 bool wxGetUserName( wxChar
* zBuf
, int nMaxSize
)
121 wxGetUserId( zBuf
, nMaxSize
);
123 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
128 int wxKill(long lPid
,
129 wxSignal
WXUNUSED(eSig
),
130 wxKillError
* WXUNUSED(peError
),
133 return((int)::DosKillProcess(0, (PID
)lPid
));
137 // Execute a program in an Interactive Shell
139 bool wxShell( const wxString
& rCommand
)
141 wxChar
* zShell
= _T("CMD.EXE");
143 STARTDATA SData
= {0};
144 PSZ PgmTitle
= "Command Shell";
148 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
151 SData
.Length
= sizeof(STARTDATA
);
152 SData
.Related
= SSF_RELATED_INDEPENDENT
;
153 SData
.FgBg
= SSF_FGBG_FORE
;
154 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
155 SData
.PgmTitle
= PgmTitle
;
156 SData
.PgmName
= (char*)zShell
;
158 sInputs
= _T("/C ") + rCommand
;
159 SData
.PgmInputs
= (BYTE
*)sInputs
.c_str();
161 SData
.Environment
= 0;
162 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
163 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
166 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
169 SData
.InitXSize
= 200;
170 SData
.InitYSize
= 140;
172 SData
.ObjectBuffer
= (char*)achObjBuf
;
173 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
175 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
176 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
181 ::DosGetInfoBlocks(&ptib
, &ppib
);
183 ::DosWaitChild( DCWA_PROCESS
193 // Shutdown or reboot the PC
194 bool wxShutdown(wxShutdownFlags
WXUNUSED(wFlags
))
200 wxPowerType
wxGetPowerType()
203 return wxPOWER_UNKNOWN
;
206 wxBatteryState
wxGetBatteryState()
209 return wxBATTERY_UNKNOWN_STATE
;
212 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
213 wxMemorySize
wxGetFreeMemory()
215 void* pMemptr
= NULL
;
220 lMemFlags
= PAG_FREE
;
221 rc
= ::DosQueryMem(pMemptr
, (PULONG
)&lSize
, &lMemFlags
);
224 return (wxMemorySize
)lSize
;
228 unsigned long wxGetProcessId()
230 return (unsigned long)getpid();
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 bool wxGetEnv(const wxString
& var
, wxString
*value
)
239 // wxGetenv is defined as getenv()
240 wxChar
*p
= wxGetenv(var
);
252 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
254 #if defined(HAVE_SETENV)
255 return setenv(variable
.mb_str(), value
? wxString(value
).mb_str().data()
256 : NULL
, 1 /* overwrite */) == 0;
257 #elif defined(HAVE_PUTENV)
258 wxString s
= variable
;
260 s
<< _T('=') << value
;
263 const char *p
= s
.mb_str();
265 // the string will be free()d by libc
266 char *buf
= (char *)malloc(strlen(p
) + 1);
269 return putenv(buf
) == 0;
270 #else // no way to set an env var
271 wxUnusedVar(variable
);
278 unsigned long ulMilliseconds
281 ::DosSleep(ulMilliseconds
);
285 unsigned long ulMicroseconds
288 ::DosSleep(ulMicroseconds
/1000);
295 ::DosSleep(1000 * nSecs
);
298 // Consume all events until no more left
307 DosBeep(1000,1000); // 1kHz during 1 sec.
310 wxString
wxGetOsDescription()
312 wxString
strVer(_T("OS/2"));
313 ULONG ulSysInfo
[QSV_MAX
] = {0};
315 if (::DosQuerySysInfo( 1L,
318 sizeof(ULONG
) * QSV_MAX
322 ver
.Printf( _T(" ver. %d.%d rev. %c"),
323 int(ulSysInfo
[QSV_VERSION_MAJOR
] / 10),
324 int(ulSysInfo
[QSV_VERSION_MINOR
]),
325 char(ulSysInfo
[QSV_VERSION_REVISION
])
333 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab
))
337 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab
))
341 wxToolkitInfo
& wxConsoleAppTraits::GetToolkitInfo()
343 static wxToolkitInfo vInfo
;
344 ULONG ulSysInfo
[QSV_MAX
] = {0};
347 vInfo
.name
= _T("wxBase");
348 ulrc
= ::DosQuerySysInfo( 1L
351 ,sizeof(ULONG
) * QSV_MAX
355 vInfo
.versionMajor
= ulSysInfo
[QSV_VERSION_MAJOR
] / 10;
356 vInfo
.versionMinor
= ulSysInfo
[QSV_VERSION_MINOR
];
362 // ---------------------------------------------------------------------------
363 const wxChar
* wxGetHomeDir(
367 wxString
& rStrDir
= *pStr
;
369 // OS/2 has no idea about home,
370 // so use the working directory instead.
371 // However, we might have a valid HOME directory,
372 // as is used on many machines that have unix utilities
373 // on them, so we should use that, if available.
375 // 256 was taken from os2def.h
377 # define MAX_PATH 256
380 const wxChar
*szHome
= wxGetenv((wxChar
*)"HOME");
381 if ( szHome
== NULL
) {
382 // we're homeless, use current directory.
388 return rStrDir
.c_str();
393 const wxMB2WXbuf
wxGetUserHome( const wxString
&rUser
)
394 #else // just for binary compatibility -- there is no 'const' here
395 wxChar
* wxGetUserHome ( const wxString
&rUser
)
399 wxString
sUser1(rUser
);
401 wxChar
*wxBuffer
= new wxChar
[256];
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)
421 sUser1
= wxEmptyString
;
427 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
429 wxStrcpy(wxBuffer
, zHome
);
430 wxUnix2DosFilename(wxBuffer
);
432 wxWCharBuffer
retBuffer (wxBuffer
);
436 wxStrcpy(zHome
, wxBuffer
);
443 return (wxChar
*)wxEmptyString
; // No home known!
446 wxString
wxPMErrorToStr(ERRORID vError
)
451 // Remove the high order byte -- it is useless
453 vError
&= 0x0000ffff;
456 case PMERR_INVALID_HWND
:
457 sError
= wxT("Invalid window handle specified");
460 case PMERR_INVALID_FLAG
:
461 sError
= wxT("Invalid flag bit set");
464 case PMERR_NO_MSG_QUEUE
:
465 sError
= wxT("No message queue available");
468 case PMERR_INVALID_PARM
:
469 sError
= wxT("Parameter contained invalid data");
472 case PMERR_INVALID_PARAMETERS
:
473 sError
= wxT("Parameter value is out of range");
476 case PMERR_PARAMETER_OUT_OF_RANGE
:
477 sError
= wxT("Parameter value is out of range");
480 case PMERR_INVALID_INTEGER_ATOM
:
481 sError
= wxT("Not a valid atom");
484 case PMERR_INVALID_HATOMTBL
:
485 sError
= wxT("Atom table handle is invalid");
488 case PMERR_INVALID_ATOM_NAME
:
489 sError
= wxT("Not a valid atom name");
492 case PMERR_ATOM_NAME_NOT_FOUND
:
493 sError
= wxT("Valid name format, but cannot find name in atom table");
497 sError
= wxT("Unknown error");
500 } // end of wxPMErrorToStr
502 // replacement for implementation in unix/utilsunx.cpp,
503 // to be used by all X11 based ports.
504 struct wxEndProcessData
;
506 void wxHandleProcessTermination(wxEndProcessData
*WXUNUSED(proc_data
))
508 // For now, just do nothing. To be filled in as needed.