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"
20 #include "wx/os2/private.h"
22 #include "wx/apptrait.h"
41 #if defined(__WATCOMC__)
46 #elif !defined(__EMX__)
52 static const wxChar WX_SECTION
[] = _T("wxWidgets");
53 static const wxChar eHOSTNAME
[] = _T("HostName");
55 // For the following functions we SHOULD fill in support
56 // for Windows-NT (which I don't know) as I assume it begin
57 // a POSIX Unix (so claims MS) that it has some special
58 // functions beyond those provided by WinSock
60 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
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
97 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
98 zBuf
[nMaxSize
] = _T('\0');
100 return *zBuf
? true : false;
103 // Get user ID e.g. jacs
104 bool wxGetUserId(wxChar
* zBuf
, int nType
)
106 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
107 // UPM procs return 0 on success
108 long lrc
= U32ELOCU((PUCHAR
)zBuf
, (PULONG
)&nType
);
109 if (lrc
== 0) return true;
114 bool wxGetUserName( wxChar
* zBuf
, int nMaxSize
)
117 wxGetUserId( zBuf
, nMaxSize
);
119 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
124 int wxKill(long lPid
,
125 wxSignal
WXUNUSED(eSig
),
126 wxKillError
* WXUNUSED(peError
),
129 return((int)::DosKillProcess(0, (PID
)lPid
));
133 // Execute a program in an Interactive Shell
136 const wxString
& rCommand
139 wxChar
* zShell
= _T("CMD.EXE");
141 STARTDATA SData
= {0};
142 PSZ PgmTitle
= "Command Shell";
146 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
149 SData
.Length
= sizeof(STARTDATA
);
150 SData
.Related
= SSF_RELATED_INDEPENDENT
;
151 SData
.FgBg
= SSF_FGBG_FORE
;
152 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
153 SData
.PgmTitle
= PgmTitle
;
154 SData
.PgmName
= (char*)zShell
;
156 sInputs
= _T("/C ") + rCommand
;
157 SData
.PgmInputs
= (BYTE
*)sInputs
.c_str();
159 SData
.Environment
= 0;
160 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
161 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
164 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
167 SData
.InitXSize
= 200;
168 SData
.InitYSize
= 140;
170 SData
.ObjectBuffer
= (char*)achObjBuf
;
171 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
173 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
174 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
179 ::DosGetInfoBlocks(&ptib
, &ppib
);
181 ::DosWaitChild( DCWA_PROCESS
191 // Shutdown or reboot the PC
192 bool wxShutdown(wxShutdownFlags
WXUNUSED(wFlags
))
198 wxPowerType
wxGetPowerType()
201 return wxPOWER_UNKNOWN
;
204 wxBatteryState
wxGetBatteryState()
207 return wxBATTERY_UNKNOWN_STATE
;
210 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
211 wxMemorySize
wxGetFreeMemory()
213 void* pMemptr
= NULL
;
218 lMemFlags
= PAG_FREE
;
219 rc
= ::DosQueryMem(pMemptr
, (PULONG
)&lSize
, &lMemFlags
);
222 return (wxMemorySize
)lSize
;
226 unsigned long wxGetProcessId()
228 return (unsigned long)getpid();
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
235 bool wxGetEnv(const wxString
& var
, wxString
*value
)
237 // wxGetenv is defined as getenv()
238 wxChar
*p
= wxGetenv(var
);
250 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
252 #if defined(HAVE_SETENV)
253 return setenv(variable
.mb_str(), value
? wxString(value
).mb_str().data()
254 : NULL
, 1 /* overwrite */) == 0;
255 #elif defined(HAVE_PUTENV)
256 wxString s
= variable
;
258 s
<< _T('=') << value
;
261 const char *p
= s
.mb_str();
263 // the string will be free()d by libc
264 char *buf
= (char *)malloc(strlen(p
) + 1);
267 return putenv(buf
) == 0;
268 #else // no way to set an env var
269 wxUnusedVar(variable
);
276 unsigned long ulMilliseconds
279 ::DosSleep(ulMilliseconds
);
283 unsigned long ulMicroseconds
286 ::DosSleep(ulMicroseconds
/1000);
293 ::DosSleep(1000 * nSecs
);
296 // Consume all events until no more left
305 DosBeep(1000,1000); // 1kHz during 1 sec.
308 wxString
wxGetOsDescription()
310 wxString
strVer(_T("OS/2"));
311 ULONG ulSysInfo
[QSV_MAX
] = {0};
313 if (::DosQuerySysInfo( 1L,
316 sizeof(ULONG
) * QSV_MAX
320 ver
.Printf( _T(" ver. %d.%d rev. %c"),
321 int(ulSysInfo
[QSV_VERSION_MAJOR
] / 10),
322 int(ulSysInfo
[QSV_VERSION_MINOR
]),
323 char(ulSysInfo
[QSV_VERSION_REVISION
])
331 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab
))
335 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab
))
339 wxToolkitInfo
& wxConsoleAppTraits::GetToolkitInfo()
341 static wxToolkitInfo vInfo
;
342 ULONG ulSysInfo
[QSV_MAX
] = {0};
345 vInfo
.name
= _T("wxBase");
346 ulrc
= ::DosQuerySysInfo( 1L
349 ,sizeof(ULONG
) * QSV_MAX
353 vInfo
.versionMajor
= ulSysInfo
[QSV_VERSION_MAJOR
] / 10;
354 vInfo
.versionMinor
= ulSysInfo
[QSV_VERSION_MINOR
];
360 // ---------------------------------------------------------------------------
361 const wxChar
* wxGetHomeDir(
365 wxString
& rStrDir
= *pStr
;
367 // OS/2 has no idea about home,
368 // so use the working directory instead.
369 // However, we might have a valid HOME directory,
370 // as is used on many machines that have unix utilities
371 // on them, so we should use that, if available.
373 // 256 was taken from os2def.h
375 # define MAX_PATH 256
378 const wxChar
*szHome
= wxGetenv((wxChar
*)"HOME");
379 if ( szHome
== NULL
) {
380 // we're homeless, use current directory.
386 return rStrDir
.c_str();
391 const wxMB2WXbuf
wxGetUserHome( const wxString
&rUser
)
392 #else // just for binary compatibility -- there is no 'const' here
393 wxChar
* wxGetUserHome ( const wxString
&rUser
)
397 wxString
sUser1(rUser
);
399 wxChar
*wxBuffer
= new wxChar
[256];
405 if (wxGetUserId( zTmp
406 ,sizeof(zTmp
)/sizeof(char)
409 // Guests belong in the temp dir
410 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
412 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
413 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
414 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
416 return *zHome
? zHome
: (wxChar
*)_T("\\");
418 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
419 sUser1
= wxEmptyString
;
425 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
427 wxStrcpy(wxBuffer
, zHome
);
428 wxUnix2DosFilename(wxBuffer
);
430 wxWCharBuffer
retBuffer (wxBuffer
);
434 wxStrcpy(zHome
, wxBuffer
);
441 return (wxChar
*)wxEmptyString
; // No home known!
444 wxString
wxPMErrorToStr(ERRORID vError
)
449 // Remove the high order byte -- it is useless
451 vError
&= 0x0000ffff;
454 case PMERR_INVALID_HWND
:
455 sError
= wxT("Invalid window handle specified");
458 case PMERR_INVALID_FLAG
:
459 sError
= wxT("Invalid flag bit set");
462 case PMERR_NO_MSG_QUEUE
:
463 sError
= wxT("No message queue available");
466 case PMERR_INVALID_PARM
:
467 sError
= wxT("Parameter contained invalid data");
470 case PMERR_INVALID_PARAMETERS
:
471 sError
= wxT("Parameter value is out of range");
474 case PMERR_PARAMETER_OUT_OF_RANGE
:
475 sError
= wxT("Parameter value is out of range");
478 case PMERR_INVALID_INTEGER_ATOM
:
479 sError
= wxT("Not a valid atom");
482 case PMERR_INVALID_HATOMTBL
:
483 sError
= wxT("Atom table handle is invalid");
486 case PMERR_INVALID_ATOM_NAME
:
487 sError
= wxT("Not a valid atom name");
490 case PMERR_ATOM_NAME_NOT_FOUND
:
491 sError
= wxT("Valid name format, but cannot find name in atom table");
495 sError
= wxT("Unknown error");
498 } // end of wxPMErrorToStr
500 // replacement for implementation in unix/utilsunx.cpp,
501 // to be used by all X11 based ports.
502 struct wxEndProcessData
;
504 void wxHandleProcessTermination(wxEndProcessData
*WXUNUSED(proc_data
))
506 // For now, just do nothing. To be filled in as needed.