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)
61 bool wxGetHostName( wxChar
* zBuf
, int nMaxSize
)
63 if (!zBuf
) return false;
65 #if defined(wxUSE_NET_API) && wxUSE_NET_API
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 ::PrfQueryProfileString( HINI_PROFILE
97 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
100 zBuf
[nMaxSize
] = _T('\0');
103 return *zBuf
? true : false;
106 // Get user ID e.g. jacs
107 bool wxGetUserId(wxChar
* zBuf
, int nType
)
109 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
110 // UPM procs return 0 on success
111 long lrc
= U32ELOCU((PUCHAR
)zBuf
, (PULONG
)&nType
);
112 if (lrc
== 0) return true;
117 bool wxGetUserName( wxChar
* zBuf
, int nMaxSize
)
120 wxGetUserId( zBuf
, nMaxSize
);
122 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
127 int wxKill(long lPid
,
128 wxSignal
WXUNUSED(eSig
),
129 wxKillError
* WXUNUSED(peError
),
132 return((int)::DosKillProcess(0, (PID
)lPid
));
136 // Execute a program in an Interactive Shell
139 const wxString
& rCommand
142 wxChar
* zShell
= _T("CMD.EXE");
144 STARTDATA SData
= {0};
145 PSZ PgmTitle
= "Command Shell";
149 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
152 SData
.Length
= sizeof(STARTDATA
);
153 SData
.Related
= SSF_RELATED_INDEPENDENT
;
154 SData
.FgBg
= SSF_FGBG_FORE
;
155 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
156 SData
.PgmTitle
= PgmTitle
;
157 SData
.PgmName
= (char*)zShell
;
159 sInputs
= _T("/C ") + rCommand
;
160 SData
.PgmInputs
= (BYTE
*)sInputs
.c_str();
162 SData
.Environment
= 0;
163 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
164 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
167 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
170 SData
.InitXSize
= 200;
171 SData
.InitYSize
= 140;
173 SData
.ObjectBuffer
= (char*)achObjBuf
;
174 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
176 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
177 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
182 ::DosGetInfoBlocks(&ptib
, &ppib
);
184 ::DosWaitChild( DCWA_PROCESS
194 // Shutdown or reboot the PC
195 bool wxShutdown(wxShutdownFlags
WXUNUSED(wFlags
))
201 wxPowerType
wxGetPowerType()
204 return wxPOWER_UNKNOWN
;
207 wxBatteryState
wxGetBatteryState()
210 return wxBATTERY_UNKNOWN_STATE
;
213 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
214 wxMemorySize
wxGetFreeMemory()
216 void* pMemptr
= NULL
;
221 lMemFlags
= PAG_FREE
;
222 rc
= ::DosQueryMem(pMemptr
, (PULONG
)&lSize
, &lMemFlags
);
225 return (wxMemorySize
)lSize
;
229 unsigned long wxGetProcessId()
231 return (unsigned long)getpid();
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
238 bool wxGetEnv(const wxString
& var
, wxString
*value
)
240 // wxGetenv is defined as getenv()
241 wxChar
*p
= wxGetenv(var
);
253 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
255 #if defined(HAVE_SETENV)
256 return setenv(variable
.mb_str(), value
? wxString(value
).mb_str().data()
257 : NULL
, 1 /* overwrite */) == 0;
258 #elif defined(HAVE_PUTENV)
259 wxString s
= variable
;
261 s
<< _T('=') << value
;
264 const char *p
= s
.mb_str();
266 // the string will be free()d by libc
267 char *buf
= (char *)malloc(strlen(p
) + 1);
270 return putenv(buf
) == 0;
271 #else // no way to set an env var
272 wxUnusedVar(variable
);
279 unsigned long ulMilliseconds
282 ::DosSleep(ulMilliseconds
);
286 unsigned long ulMicroseconds
289 ::DosSleep(ulMicroseconds
/1000);
296 ::DosSleep(1000 * nSecs
);
299 // Consume all events until no more left
308 DosBeep(1000,1000); // 1kHz during 1 sec.
311 wxString
wxGetOsDescription()
313 wxString
strVer(_T("OS/2"));
314 ULONG ulSysInfo
[QSV_MAX
] = {0};
316 if (::DosQuerySysInfo( 1L,
319 sizeof(ULONG
) * QSV_MAX
323 ver
.Printf( _T(" ver. %d.%d rev. %c"),
324 int(ulSysInfo
[QSV_VERSION_MAJOR
] / 10),
325 int(ulSysInfo
[QSV_VERSION_MINOR
]),
326 char(ulSysInfo
[QSV_VERSION_REVISION
])
334 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab
))
338 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab
))
342 wxToolkitInfo
& wxConsoleAppTraits::GetToolkitInfo()
344 static wxToolkitInfo vInfo
;
345 ULONG ulSysInfo
[QSV_MAX
] = {0};
348 vInfo
.name
= _T("wxBase");
349 ulrc
= ::DosQuerySysInfo( 1L
352 ,sizeof(ULONG
) * QSV_MAX
356 vInfo
.versionMajor
= ulSysInfo
[QSV_VERSION_MAJOR
] / 10;
357 vInfo
.versionMinor
= ulSysInfo
[QSV_VERSION_MINOR
];
363 // ---------------------------------------------------------------------------
364 const wxChar
* wxGetHomeDir(
368 wxString
& rStrDir
= *pStr
;
370 // OS/2 has no idea about home,
371 // so use the working directory instead.
372 // However, we might have a valid HOME directory,
373 // as is used on many machines that have unix utilities
374 // on them, so we should use that, if available.
376 // 256 was taken from os2def.h
378 # define MAX_PATH 256
381 const wxChar
*szHome
= wxGetenv((wxChar
*)"HOME");
382 if ( szHome
== NULL
) {
383 // we're homeless, use current directory.
389 return rStrDir
.c_str();
394 const wxMB2WXbuf
wxGetUserHome( const wxString
&rUser
)
395 #else // just for binary compatibility -- there is no 'const' here
396 wxChar
* wxGetUserHome ( const wxString
&rUser
)
400 wxString
sUser1(rUser
);
402 wxChar
*wxBuffer
= new wxChar
[256];
408 if (wxGetUserId( zTmp
409 ,sizeof(zTmp
)/sizeof(char)
412 // Guests belong in the temp dir
413 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
415 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
416 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
417 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
419 return *zHome
? zHome
: (wxChar
*)_T("\\");
421 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
422 sUser1
= wxEmptyString
;
428 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
430 wxStrcpy(wxBuffer
, zHome
);
431 wxUnix2DosFilename(wxBuffer
);
433 wxWCharBuffer
retBuffer (wxBuffer
);
437 wxStrcpy(zHome
, wxBuffer
);
444 return (wxChar
*)wxEmptyString
; // No home known!
447 wxString
wxPMErrorToStr(ERRORID vError
)
452 // Remove the high order byte -- it is useless
454 vError
&= 0x0000ffff;
457 case PMERR_INVALID_HWND
:
458 sError
= wxT("Invalid window handle specified");
461 case PMERR_INVALID_FLAG
:
462 sError
= wxT("Invalid flag bit set");
465 case PMERR_NO_MSG_QUEUE
:
466 sError
= wxT("No message queue available");
469 case PMERR_INVALID_PARM
:
470 sError
= wxT("Parameter contained invalid data");
473 case PMERR_INVALID_PARAMETERS
:
474 sError
= wxT("Parameter value is out of range");
477 case PMERR_PARAMETER_OUT_OF_RANGE
:
478 sError
= wxT("Parameter value is out of range");
481 case PMERR_INVALID_INTEGER_ATOM
:
482 sError
= wxT("Not a valid atom");
485 case PMERR_INVALID_HATOMTBL
:
486 sError
= wxT("Atom table handle is invalid");
489 case PMERR_INVALID_ATOM_NAME
:
490 sError
= wxT("Not a valid atom name");
493 case PMERR_ATOM_NAME_NOT_FOUND
:
494 sError
= wxT("Valid name format, but cannot find name in atom table");
498 sError
= wxT("Unknown error");
501 } // end of wxPMErrorToStr
503 // replacement for implementation in unix/utilsunx.cpp,
504 // to be used by all X11 based ports.
505 struct wxEndProcessData
;
507 void wxHandleProcessTermination(wxEndProcessData
*WXUNUSED(proc_data
))
509 // For now, just do nothing. To be filled in as needed.