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"
48 static const wxChar WX_SECTION
[] = _T("wxWidgets");
49 static const wxChar eHOSTNAME
[] = _T("HostName");
50 static const wxChar eUSERID
[] = _T("UserId");
51 static const wxChar eUSERNAME
[] = _T("UserName");
53 // For the following functions we SHOULD fill in support
54 // for Windows-NT (which I don't know) as I assume it begin
55 // a POSIX Unix (so claims MS) that it has some special
56 // functions beyond those provided by WinSock
58 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
67 unsigned long ulLevel
= 0;
68 unsigned char* zBuffer
= NULL
;
69 unsigned long ulBuffer
= 256;
70 unsigned long* pulTotalAvail
= NULL
;
72 NetBios32GetInfo( (const unsigned char*)zServer
73 ,(const unsigned char*)zComputer
79 strcpy(zBuf
, zServer
);
82 const wxChar
* zDefaultHost
= _T("noname");
84 if ((zSysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
86 ::PrfQueryProfileString( HINI_PROFILE
95 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
96 zBuf
[nMaxSize
] = _T('\0');
98 return *zBuf
? true : false;
101 // Get user ID e.g. jacs
107 #if defined(__VISAGECPP__)
109 // UPM procs return 0 on success
110 lrc
= U32ELOCU((unsigned char*)zBuf
, (unsigned long *)&nType
);
111 if (lrc
== 0) return true;
126 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
134 , 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");
150 STARTDATA SData
= {0};
151 PSZ PgmTitle
= "Command Shell";
155 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
158 SData
.Length
= sizeof(STARTDATA
);
159 SData
.Related
= SSF_RELATED_INDEPENDENT
;
160 SData
.FgBg
= SSF_FGBG_FORE
;
161 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
162 SData
.PgmTitle
= PgmTitle
;
163 SData
.PgmName
= (char*)zShell
;
165 sInputs
= _T("/C ") + rCommand
;
166 SData
.PgmInputs
= (BYTE
*)sInputs
.c_str();
168 SData
.Environment
= 0;
169 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
170 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
173 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
176 SData
.InitXSize
= 200;
177 SData
.InitYSize
= 140;
179 SData
.ObjectBuffer
= (char*)achObjBuf
;
180 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
182 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
183 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
188 ::DosGetInfoBlocks(&ptib
, &ppib
);
190 ::DosWaitChild( DCWA_PROCESS
200 // Shutdown or reboot the PC
201 bool wxShutdown(wxShutdownFlags wFlags
)
207 wxPowerType
wxGetPowerType()
210 return wxPOWER_UNKNOWN
;
213 wxBatteryState
wxGetBatteryState()
216 return wxBATTERY_UNKNOWN_STATE
;
219 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
220 wxMemorySize
wxGetFreeMemory()
222 void* pMemptr
= NULL
;
227 lMemFlags
= PAG_FREE
;
228 rc
= ::DosQueryMem(pMemptr
, (PULONG
)&lSize
, &lMemFlags
);
231 return (wxMemorySize
)lSize
;
235 unsigned long wxGetProcessId()
237 return (unsigned long)getpid();
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 bool wxGetEnv(const wxString
& var
, wxString
*value
)
246 // wxGetenv is defined as getenv()
247 wxChar
*p
= wxGetenv(var
);
259 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
261 #if defined(HAVE_SETENV)
262 return setenv(variable
.mb_str(), value
? wxString(value
).mb_str().data()
263 : NULL
, 1 /* overwrite */) == 0;
264 #elif defined(HAVE_PUTENV)
265 wxString s
= variable
;
267 s
<< _T('=') << value
;
270 const char *p
= s
.mb_str();
272 // the string will be free()d by libc
273 char *buf
= (char *)malloc(strlen(p
) + 1);
276 return putenv(buf
) == 0;
277 #else // no way to set an env var
283 unsigned long ulMilliseconds
286 ::DosSleep(ulMilliseconds
);
290 unsigned long ulMicroseconds
293 ::DosSleep(ulMicroseconds
/1000);
300 ::DosSleep(1000 * nSecs
);
303 // Consume all events until no more left
309 #if WXWIN_COMPATIBILITY_2_2
311 // Output a debug mess., in a system dependent fashion.
313 const wxChar
* zFmt
...
317 static wxChar zBuffer
[512];
319 if (!wxTheApp
->GetWantDebugOutput())
322 sprintf(zBuffer
, zFmt
, vAp
) ;
326 // Non-fatal error: pop up message box and (possibly) continue
329 , const wxString
& rTitle
332 wxChar
*wxBuffer
= new wxChar
[256];
333 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
334 if (::WinMessageBox( HWND_DESKTOP
337 ,(PSZ
)WXSTRINGCAST rTitle
339 ,MB_ICONEXCLAMATION
| MB_YESNO
345 // Fatal error: pop up message box and abort
348 , const wxString
& rTitle
353 ulRc
= ::WinMessageBox( HWND_DESKTOP
360 DosExit(EXIT_PROCESS
, ulRc
);
363 #endif // WXWIN_COMPATIBILITY_2_2
368 DosBeep(1000,1000); // 1kHz during 1 sec.
372 void wxAppTraits::InitializeGui(unsigned long &ulHab
)
376 void wxAppTraits::TerminateGui(unsigned long ulHab
)
380 wxToolkitInfo
& wxConsoleAppTraits::GetToolkitInfo()
382 static wxToolkitInfo vInfo
;
383 ULONG ulSysInfo
[QSV_MAX
] = {0};
386 vInfo
.name
= _T("wxBase");
387 ulrc
= ::DosQuerySysInfo( 1L
390 ,sizeof(ULONG
) * QSV_MAX
394 vInfo
.versionMajor
= ulSysInfo
[QSV_VERSION_MAJOR
] / 10;
395 vInfo
.versionMinor
= ulSysInfo
[QSV_VERSION_MINOR
];
401 // ---------------------------------------------------------------------------
402 const wxChar
* wxGetHomeDir(
406 wxString
& rStrDir
= *pStr
;
408 // OS/2 has no idea about home,
409 // so use the working directory instead.
410 // However, we might have a valid HOME directory,
411 // as is used on many machines that have unix utilities
412 // on them, so we should use that, if available.
414 // 256 was taken from os2def.h
416 # define MAX_PATH 256
419 const wxChar
*szHome
= wxGetenv((wxChar
*)"HOME");
420 if ( szHome
== NULL
) {
421 // we're homeless, use current directory.
427 return rStrDir
.c_str();
431 wxChar
* wxGetUserHome (
432 const wxString
& rUser
436 wxString
sUser1(rUser
);
438 wxChar
*wxBuffer
= new wxChar
[256];
444 if (wxGetUserId( zTmp
445 ,sizeof(zTmp
)/sizeof(char)
448 // Guests belong in the temp dir
449 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
451 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
452 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
453 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
455 return *zHome
? zHome
: (wxChar
*)_T("\\");
457 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
464 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
466 wxStrcpy(wxBuffer
, zHome
);
467 wxUnix2DosFilename(wxBuffer
);
468 wxStrcpy(zHome
, wxBuffer
);
474 return (wxChar
*)wxEmptyString
; // No home known!
477 wxString WXDLLEXPORT
wxPMErrorToStr(
484 // Remove the high order byte -- it is useless
486 vError
&= 0x0000ffff;
489 case PMERR_INVALID_HWND
:
490 sError
= wxT("Invalid window handle specified");
493 case PMERR_INVALID_FLAG
:
494 sError
= wxT("Invalid flag bit set");
497 case PMERR_NO_MSG_QUEUE
:
498 sError
= wxT("No message queue available");
501 case PMERR_INVALID_PARM
:
502 sError
= wxT("Parameter contained invalid data");
505 case PMERR_INVALID_PARAMETERS
:
506 sError
= wxT("Parameter value is out of range");
509 case PMERR_PARAMETER_OUT_OF_RANGE
:
510 sError
= wxT("Parameter value is out of range");
513 case PMERR_INVALID_INTEGER_ATOM
:
514 sError
= wxT("Not a valid atom");
517 case PMERR_INVALID_HATOMTBL
:
518 sError
= wxT("Atom table handle is invalid");
521 case PMERR_INVALID_ATOM_NAME
:
522 sError
= wxT("Not a valid atom name");
525 case PMERR_ATOM_NAME_NOT_FOUND
:
526 sError
= wxT("Valid name format, but cannot find name in atom table");
530 sError
= wxT("Unknown error");
533 } // end of wxPMErrorToStr
535 // replacement for implementation in unix/utilsunx.cpp,
536 // to be used by all X11 based ports.
537 struct wxEndProcessData
;
539 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
541 // For now, just do nothing. To be filled in as needed.