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
137 return((int)::DosKillProcess(0, (PID
)lPid
));
141 // Execute a program in an Interactive Shell
144 const wxString
& rCommand
147 wxChar
* zShell
= _T("CMD.EXE");
149 STARTDATA SData
= {0};
150 PSZ PgmTitle
= "Command Shell";
154 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
157 SData
.Length
= sizeof(STARTDATA
);
158 SData
.Related
= SSF_RELATED_INDEPENDENT
;
159 SData
.FgBg
= SSF_FGBG_FORE
;
160 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
161 SData
.PgmTitle
= PgmTitle
;
162 SData
.PgmName
= zShell
;
164 sInputs
= "/C " + rCommand
;
165 SData
.PgmInputs
= (BYTE
*)sInputs
.c_str();
167 SData
.Environment
= 0;
168 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
169 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
172 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
175 SData
.InitXSize
= 200;
176 SData
.InitYSize
= 140;
178 SData
.ObjectBuffer
= (char*)achObjBuf
;
179 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
181 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
182 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
187 ::DosGetInfoBlocks(&ptib
, &ppib
);
189 ::DosWaitChild( DCWA_PROCESS
199 // Shutdown or reboot the PC
200 bool wxShutdown(wxShutdownFlags wFlags
)
206 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
207 long wxGetFreeMemory()
209 void* pMemptr
= NULL
;
214 lMemFlags
= PAG_FREE
;
215 rc
= ::DosQueryMem(pMemptr
, &lSize
, &lMemFlags
);
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
225 bool wxGetEnv(const wxString
& var
, wxString
*value
)
227 // wxGetenv is defined as getenv()
228 wxChar
*p
= wxGetenv(var
);
240 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
242 #if defined(HAVE_SETENV)
243 return setenv(variable
.mb_str(), value
? wxString(value
).mb_str().data()
244 : NULL
, 1 /* overwrite */) == 0;
245 #elif defined(HAVE_PUTENV)
246 wxString s
= variable
;
248 s
<< _T('=') << value
;
251 const char *p
= s
.mb_str();
253 // the string will be free()d by libc
254 char *buf
= (char *)malloc(strlen(p
) + 1);
257 return putenv(buf
) == 0;
258 #else // no way to set an env var
264 unsigned long ulMilliseconds
267 ::DosSleep(ulMilliseconds
);
271 unsigned long ulMicroseconds
274 ::DosSleep(ulMicroseconds
/1000);
281 ::DosSleep(1000 * nSecs
);
284 // Consume all events until no more left
290 #if WXWIN_COMPATIBILITY_2_2
292 // Output a debug mess., in a system dependent fashion.
294 const wxChar
* zFmt
...
298 static wxChar zBuffer
[512];
300 if (!wxTheApp
->GetWantDebugOutput())
303 sprintf(zBuffer
, zFmt
, vAp
) ;
307 // Non-fatal error: pop up message box and (possibly) continue
310 , const wxString
& rTitle
313 wxChar
*wxBuffer
= new wxChar
[256];
314 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
315 if (::WinMessageBox( HWND_DESKTOP
318 ,(PSZ
)WXSTRINGCAST rTitle
320 ,MB_ICONEXCLAMATION
| MB_YESNO
326 // Fatal error: pop up message box and abort
329 , const wxString
& rTitle
334 ulRc
= ::WinMessageBox( HWND_DESKTOP
341 DosExit(EXIT_PROCESS
, ulRc
);
344 #endif // WXWIN_COMPATIBILITY_2_2
349 DosBeep(1000,1000); // 1kHz during 1 sec.
353 void wxAppTraits::InitializeGui(unsigned long &ulHab
)
357 void wxAppTraits::TerminateGui(unsigned long ulHab
)
361 wxToolkitInfo
& wxConsoleAppTraits::GetToolkitInfo()
363 static wxToolkitInfo vInfo
;
364 ULONG ulSysInfo
[QSV_MAX
] = {0};
367 vInfo
.name
= _T("wxBase");
368 ulrc
= ::DosQuerySysInfo( 1L
371 ,sizeof(ULONG
) * QSV_MAX
375 vInfo
.versionMajor
= ulSysInfo
[QSV_VERSION_MAJOR
] / 10;
376 vInfo
.versionMinor
= ulSysInfo
[QSV_VERSION_MINOR
];
382 // ---------------------------------------------------------------------------
383 const wxChar
* wxGetHomeDir(
387 wxString
& rStrDir
= *pStr
;
389 // OS/2 has no idea about home,
390 // so use the working directory instead.
391 // However, we might have a valid HOME directory,
392 // as is used on many machines that have unix utilities
393 // on them, so we should use that, if available.
395 // 256 was taken from os2def.h
397 # define MAX_PATH 256
400 const wxChar
*szHome
= wxGetenv("HOME");
401 if ( szHome
== NULL
) {
402 // we're homeless, use current directory.
408 return rStrDir
.c_str();
412 wxChar
* wxGetUserHome (
413 const wxString
& rUser
417 wxString
sUser1(rUser
);
419 char *wxBuffer
= new wxChar
[256];
421 if (sUser1
!= _T(""))
425 if (wxGetUserId( zTmp
426 ,sizeof(zTmp
)/sizeof(char)
429 // Guests belong in the temp dir
430 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
432 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
433 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
434 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
436 return *zHome
? zHome
: (wxChar
*)_T("\\");
438 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
443 if (sUser1
== _T(""))
445 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
447 wxStrcpy(wxBuffer
, zHome
);
448 wxUnix2DosFilename(wxBuffer
);
449 wxStrcpy(zHome
, wxBuffer
);
455 return NULL
; // No home known!
462 return (::DosSetCurrentDir(WXSTRINGCAST rDir
));
465 wxString WXDLLEXPORT
wxPMErrorToStr(
472 // Remove the high order byte -- it is useless
474 vError
&= 0x0000ffff;
477 case PMERR_INVALID_HWND
:
478 sError
= wxT("Invalid window handle specified");
481 case PMERR_INVALID_FLAG
:
482 sError
= wxT("Invalid flag bit set");
485 case PMERR_NO_MSG_QUEUE
:
486 sError
= wxT("No message queue available");
489 case PMERR_INVALID_PARM
:
490 sError
= wxT("Parameter contained invalid data");
493 case PMERR_INVALID_PARAMETERS
:
494 sError
= wxT("Parameter value is out of range");
497 case PMERR_PARAMETER_OUT_OF_RANGE
:
498 sError
= wxT("Parameter value is out of range");
501 case PMERR_INVALID_INTEGER_ATOM
:
502 sError
= wxT("Not a valid atom");
505 case PMERR_INVALID_HATOMTBL
:
506 sError
= wxT("Atom table handle is invalid");
509 case PMERR_INVALID_ATOM_NAME
:
510 sError
= wxT("Not a valid atom name");
513 case PMERR_ATOM_NAME_NOT_FOUND
:
514 sError
= wxT("Valid name format, but cannot find name in atom table");
518 sError
= wxT("Unknown error");
521 } // end of wxPMErrorToStr
523 // replacement for implementation in unix/utilsunx.cpp,
524 // to be used by all X11 based ports.
525 struct wxEndProcessData
;
527 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
529 // For now, just do nothing. To be filled in as needed.