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"
24 #include "wx/apptrait.h"
49 static const wxChar WX_SECTION
[] = _T("wxWindows");
50 static const wxChar eHOSTNAME
[] = _T("HostName");
51 static const wxChar eUSERID
[] = _T("UserId");
52 static const wxChar eUSERNAME
[] = _T("UserName");
54 // For the following functions we SHOULD fill in support
55 // for Windows-NT (which I don't know) as I assume it begin
56 // a POSIX Unix (so claims MS) that it has some special
57 // functions beyond those provided by WinSock
59 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
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 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
96 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
97 zBuf
[nMaxSize
] = _T('\0');
99 return *zBuf
? TRUE
: FALSE
;
102 // Get user ID e.g. jacs
108 #if defined(__VISAGECPP__)
110 // UPM procs return 0 on success
111 lrc
= U32ELOCU((unsigned char*)zBuf
, (unsigned long *)&nType
);
112 if (lrc
== 0) return TRUE
;
127 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
135 , 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");
151 STARTDATA SData
= {0};
152 PSZ PgmTitle
= "Command Shell";
156 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
159 SData
.Length
= sizeof(STARTDATA
);
160 SData
.Related
= SSF_RELATED_INDEPENDENT
;
161 SData
.FgBg
= SSF_FGBG_FORE
;
162 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
163 SData
.PgmTitle
= PgmTitle
;
164 SData
.PgmName
= zShell
;
166 sInputs
= "/C " + rCommand
;
167 SData
.PgmInputs
= (BYTE
*)sInputs
.c_str();
169 SData
.Environment
= 0;
170 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
171 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
174 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
177 SData
.InitXSize
= 200;
178 SData
.InitYSize
= 140;
180 SData
.ObjectBuffer
= (char*)achObjBuf
;
181 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
183 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
184 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
189 ::DosGetInfoBlocks(&ptib
, &ppib
);
191 ::DosWaitChild( DCWA_PROCESS
201 // Shutdown or reboot the PC
202 bool wxShutdown(wxShutdownFlags wFlags
)
208 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
209 long wxGetFreeMemory()
211 void* pMemptr
= NULL
;
216 lMemFlags
= PAG_FREE
;
217 rc
= ::DosQueryMem(pMemptr
, &lSize
, &lMemFlags
);
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
227 bool wxGetEnv(const wxString
& var
, wxString
*value
)
229 // wxGetenv is defined as getenv()
230 wxChar
*p
= wxGetenv(var
);
242 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
244 #if defined(HAVE_SETENV)
245 return setenv(variable
.mb_str(), value
? wxString(value
).mb_str().data()
246 : NULL
, 1 /* overwrite */) == 0;
247 #elif defined(HAVE_PUTENV)
248 wxString s
= variable
;
250 s
<< _T('=') << value
;
253 const char *p
= s
.mb_str();
255 // the string will be free()d by libc
256 char *buf
= (char *)malloc(strlen(p
) + 1);
259 return putenv(buf
) == 0;
260 #else // no way to set an env var
266 unsigned long ulMilliseconds
269 ::DosSleep(ulMilliseconds
);
276 ::DosSleep(1000 * nSecs
);
279 // Consume all events until no more left
285 #if WXWIN_COMPATIBILITY_2_2
287 // Output a debug mess., in a system dependent fashion.
289 const wxChar
* zFmt
...
293 static wxChar zBuffer
[512];
295 if (!wxTheApp
->GetWantDebugOutput())
298 sprintf(zBuffer
, zFmt
, vAp
) ;
302 // Non-fatal error: pop up message box and (possibly) continue
305 , const wxString
& rTitle
308 wxBuffer
= new wxChar
[256];
309 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
310 if (::WinMessageBox( HWND_DESKTOP
313 ,(PSZ
)WXSTRINGCAST rTitle
315 ,MB_ICONEXCLAMATION
| MB_YESNO
321 // Fatal error: pop up message box and abort
324 , const wxString
& rTitle
329 ulRc
= ::WinMessageBox( HWND_DESKTOP
336 DosExit(EXIT_PROCESS
, ulRc
);
339 #endif // WXWIN_COMPATIBILITY_2_2
344 DosBeep(1000,1000); // 1kHz during 1 sec.
347 wxToolkitInfo
& wxConsoleAppTraits::GetToolkitInfo()
349 static wxToolkitInfo vInfo
;
350 ULONG ulSysInfo
[QSV_MAX
] = {0};
353 vInfo
.name
= _T("wxBase");
354 ulrc
= ::DosQuerySysInfo( 1L
357 ,sizeof(ULONG
) * QSV_MAX
361 vInfo
.versionMajor
= ulSysInfo
[QSV_VERSION_MAJOR
] / 10;
362 vInfo
.versionMinor
= ulSysInfo
[QSV_VERSION_MINOR
];
368 // ---------------------------------------------------------------------------
369 const wxChar
* wxGetHomeDir(
373 wxString
& rStrDir
= *pStr
;
375 // OS/2 has no idea about home,
376 // so use the working directory instead?
378 // 256 was taken from os2def.h
380 # define MAX_PATH 256
386 ::DosQueryCurrentDir(0, zDirName
, &ulDirLen
);
388 return rStrDir
.c_str();
392 wxChar
* wxGetUserHome (
393 const wxString
& rUser
397 wxString
sUser1(rUser
);
399 wxBuffer
= new wxChar
[256];
401 if (sUser1
!= _T(""))
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)
423 if (sUser1
== _T(""))
425 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
427 wxStrcpy(wxBuffer
, zHome
);
428 wxUnix2DosFilename(wxBuffer
);
429 wxStrcpy(zHome
, wxBuffer
);
435 return NULL
; // No home known!
442 return (::DosSetCurrentDir(WXSTRINGCAST rDir
));
445 wxString WXDLLEXPORT
wxPMErrorToStr(
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
*proc_data
)
509 // For now, just do nothing. To be filled in as needed.