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"
23 #include "wx/os2/private.h"
24 #include "wx/apptrait.h"
42 #if defined(__WATCOMC__)
47 #elif !defined(__EMX__)
53 static const wxChar WX_SECTION
[] = _T("wxWidgets");
54 static const wxChar eHOSTNAME
[] = _T("HostName");
56 // For the following functions we SHOULD fill in support
57 // for Windows-NT (which I don't know) as I assume it begin
58 // a POSIX Unix (so claims MS) that it has some special
59 // functions beyond those provided by WinSock
61 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
62 bool wxGetHostName( wxChar
* zBuf
, int nMaxSize
)
64 if (!zBuf
) return false;
66 #if defined(wxUSE_NET_API) && wxUSE_NET_API
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
98 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
101 zBuf
[nMaxSize
] = _T('\0');
104 return *zBuf
? true : false;
107 // Get user ID e.g. jacs
108 bool wxGetUserId(wxChar
* zBuf
, int nType
)
110 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
111 // UPM procs return 0 on success
112 long lrc
= U32ELOCU((PUCHAR
)zBuf
, (PULONG
)&nType
);
113 if (lrc
== 0) return true;
118 bool wxGetUserName( wxChar
* zBuf
, int nMaxSize
)
121 wxGetUserId( zBuf
, nMaxSize
);
123 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
128 int wxKill(long lPid
,
129 wxSignal
WXUNUSED(eSig
),
130 wxKillError
* WXUNUSED(peError
),
133 return((int)::DosKillProcess(0, (PID
)lPid
));
137 // Execute a program in an Interactive Shell
139 bool wxShell( const wxString
& rCommand
)
141 wxChar
* zShell
= _T("CMD.EXE");
143 STARTDATA SData
= {0};
144 PSZ PgmTitle
= "Command Shell";
148 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
151 SData
.Length
= sizeof(STARTDATA
);
152 SData
.Related
= SSF_RELATED_INDEPENDENT
;
153 SData
.FgBg
= SSF_FGBG_FORE
;
154 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
155 SData
.PgmTitle
= PgmTitle
;
156 SData
.PgmName
= (char*)zShell
;
158 sInputs
= _T("/C ") + rCommand
;
159 SData
.PgmInputs
= (BYTE
*)sInputs
.c_str();
161 SData
.Environment
= 0;
162 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
163 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
166 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
169 SData
.InitXSize
= 200;
170 SData
.InitYSize
= 140;
172 SData
.ObjectBuffer
= (char*)achObjBuf
;
173 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
175 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
176 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
181 ::DosGetInfoBlocks(&ptib
, &ppib
);
183 ::DosWaitChild( DCWA_PROCESS
193 // Shutdown or reboot the PC
194 bool wxShutdown(wxShutdownFlags
WXUNUSED(wFlags
))
200 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
201 wxMemorySize
wxGetFreeMemory()
203 void* pMemptr
= NULL
;
208 lMemFlags
= PAG_FREE
;
209 rc
= ::DosQueryMem(pMemptr
, (PULONG
)&lSize
, &lMemFlags
);
212 return (wxMemorySize
)lSize
;
216 unsigned long wxGetProcessId()
218 return (unsigned long)getpid();
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
259 wxUnusedVar(variable
);
266 unsigned long ulMilliseconds
269 ::DosSleep(ulMilliseconds
);
273 unsigned long ulMicroseconds
276 ::DosSleep(ulMicroseconds
/1000);
283 ::DosSleep(1000 * nSecs
);
286 // Consume all events until no more left
295 DosBeep(1000,1000); // 1kHz during 1 sec.
298 wxString
wxGetOsDescription()
300 wxString
strVer(_T("OS/2"));
301 ULONG ulSysInfo
[QSV_MAX
] = {0};
303 if (::DosQuerySysInfo( 1L,
306 sizeof(ULONG
) * QSV_MAX
310 ver
.Printf( _T(" ver. %d.%d rev. %c"),
311 int(ulSysInfo
[QSV_VERSION_MAJOR
] / 10),
312 int(ulSysInfo
[QSV_VERSION_MINOR
]),
313 char(ulSysInfo
[QSV_VERSION_REVISION
])
321 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab
))
325 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab
))
329 wxToolkitInfo
& wxConsoleAppTraits::GetToolkitInfo()
331 static wxToolkitInfo vInfo
;
332 ULONG ulSysInfo
[QSV_MAX
] = {0};
335 vInfo
.name
= _T("wxBase");
336 ulrc
= ::DosQuerySysInfo( 1L
339 ,sizeof(ULONG
) * QSV_MAX
343 vInfo
.versionMajor
= ulSysInfo
[QSV_VERSION_MAJOR
] / 10;
344 vInfo
.versionMinor
= ulSysInfo
[QSV_VERSION_MINOR
];
350 // ---------------------------------------------------------------------------
351 const wxChar
* wxGetHomeDir(
355 wxString
& rStrDir
= *pStr
;
357 // OS/2 has no idea about home,
358 // so use the working directory instead.
359 // However, we might have a valid HOME directory,
360 // as is used on many machines that have unix utilities
361 // on them, so we should use that, if available.
363 // 256 was taken from os2def.h
365 # define MAX_PATH 256
368 const wxChar
*szHome
= wxGetenv((wxChar
*)"HOME");
369 if ( szHome
== NULL
) {
370 // we're homeless, use current directory.
376 return rStrDir
.c_str();
381 const wxMB2WXbuf
wxGetUserHome( const wxString
&rUser
)
382 #else // just for binary compatibility -- there is no 'const' here
383 wxChar
* wxGetUserHome ( const wxString
&rUser
)
387 wxString
sUser1(rUser
);
389 wxChar
*wxBuffer
= new wxChar
[256];
395 if (wxGetUserId( zTmp
396 ,sizeof(zTmp
)/sizeof(char)
399 // Guests belong in the temp dir
400 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
402 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
403 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
404 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
406 return *zHome
? zHome
: (wxChar
*)_T("\\");
408 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
409 sUser1
= wxEmptyString
;
415 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
417 wxStrcpy(wxBuffer
, zHome
);
418 wxUnix2DosFilename(wxBuffer
);
420 wxWCharBuffer
retBuffer (wxBuffer
);
424 wxStrcpy(zHome
, wxBuffer
);
431 return (wxChar
*)wxEmptyString
; // No home known!
434 wxString
wxPMErrorToStr(ERRORID vError
)
439 // Remove the high order byte -- it is useless
441 vError
&= 0x0000ffff;
444 case PMERR_INVALID_HWND
:
445 sError
= wxT("Invalid window handle specified");
448 case PMERR_INVALID_FLAG
:
449 sError
= wxT("Invalid flag bit set");
452 case PMERR_NO_MSG_QUEUE
:
453 sError
= wxT("No message queue available");
456 case PMERR_INVALID_PARM
:
457 sError
= wxT("Parameter contained invalid data");
460 case PMERR_INVALID_PARAMETERS
:
461 sError
= wxT("Parameter value is out of range");
464 case PMERR_PARAMETER_OUT_OF_RANGE
:
465 sError
= wxT("Parameter value is out of range");
468 case PMERR_INVALID_INTEGER_ATOM
:
469 sError
= wxT("Not a valid atom");
472 case PMERR_INVALID_HATOMTBL
:
473 sError
= wxT("Atom table handle is invalid");
476 case PMERR_INVALID_ATOM_NAME
:
477 sError
= wxT("Not a valid atom name");
480 case PMERR_ATOM_NAME_NOT_FOUND
:
481 sError
= wxT("Valid name format, but cannot find name in atom table");
485 sError
= wxT("PMERR_INV_HPS");
489 sError
= wxT("PMERR_PS_BUSY");
492 case PMERR_INV_PRIMITIVE_TYPE
:
493 sError
= wxT("PMERR_INV_PRIMITIVE_TYPE");
496 case PMERR_UNSUPPORTED_ATTR
:
497 sError
= wxT("PMERR_UNSUPPORTED_ATTR");
500 case PMERR_INV_COLOR_ATTR
:
501 sError
= wxT("PMERR_INV_COLOR_ATTR");
504 case PMERR_INV_BACKGROUND_COL_ATTR
:
505 sError
= wxT("PMERR_INV_BACKGROUND_COL_ATTR");
508 case PMERR_INV_MIX_ATTR
:
509 sError
= wxT("PMERR_INV_MIX_ATTR");
512 case PMERR_INV_LINE_WIDTH_ATTR
:
513 sError
= wxT("PMERR_INV_LINE_WIDTH_ATTR");
516 case PMERR_INV_GEOM_LINE_WIDTH_ATTR
:
517 sError
= wxT("PMERR_INV_GEOM_LINE_WIDTH_ATTR");
520 case PMERR_INV_LINE_TYPE_ATTR
:
521 sError
= wxT("PMERR_INV_LINE_TYPE_ATTR");
524 case PMERR_INV_LINE_END_ATTR
:
525 sError
= wxT("PMERR_INV_LINE_END_ATTR");
528 case PMERR_INV_LINE_JOIN_ATTR
:
529 sError
= wxT("PMERR_INV_LINE_JOIN_ATTR");
532 case PMERR_INV_CHAR_SET_ATTR
:
533 sError
= wxT("PMERR_INV_CHAR_SET_ATTR");
536 case PMERR_INV_CHAR_MODE_ATTR
:
537 sError
= wxT("PMERR_INV_CHAR_MODE_ATTR");
540 case PMERR_INV_CHAR_DIRECTION_ATTR
:
541 sError
= wxT("PMERR_INV_CHAR_DIRECTION_ATTR");
544 case PMERR_INV_CHAR_SHEAR_ATTR
:
545 sError
= wxT("PMERR_INV_CHAR_SHEAR_ATTR");
548 case PMERR_INV_CHAR_ANGLE_ATTR
:
549 sError
= wxT("PMERR_INV_CHAR_ANGLE_ATTR");
552 case PMERR_INV_MARKER_SET_ATTR
:
553 sError
= wxT("PMERR_INV_MARKER_SET_ATTR");
556 case PMERR_INV_MARKER_SYMBOL_ATTR
:
557 sError
= wxT("PMERR_INV_MARKER_SYMBOL_ATTR");
560 case PMERR_INV_PATTERN_SET_ATTR
:
561 sError
= wxT("PMERR_INV_PATTERN_SET_ATTR");
564 case PMERR_INV_PATTERN_ATTR
:
565 sError
= wxT("PMERR_INV_PATTERN_ATTR");
568 case PMERR_INV_COORDINATE
:
569 sError
= wxT("PMERR_INV_COORDINATE");
572 case PMERR_UNSUPPORTED_ATTR_VALUE
:
573 sError
= wxT("PMERR_UNSUPPORTED_ATTR_VALUE");
576 case PMERR_INV_PATTERN_SET_FONT
:
577 sError
= wxT("PMERR_INV_PATTERN_SET_FONT");
580 case PMERR_HUGE_FONTS_NOT_SUPPORTED
:
581 sError
= wxT("PMERR_HUGE_FONTS_NOT_SUPPORTED");
585 sError
= wxT("Unknown error");
588 } // end of wxPMErrorToStr
590 // replacement for implementation in unix/utilsunx.cpp,
591 // to be used by all X11 based ports.
592 struct wxEndProcessData
;
594 void wxHandleProcessTermination(wxEndProcessData
*WXUNUSED(proc_data
))
596 // For now, just do nothing. To be filled in as needed.