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"
25 #include "wx/platinfo.h"
43 #if defined(__WATCOMC__)
48 #elif !defined(__EMX__)
54 static const wxChar WX_SECTION
[] = _T("wxWidgets");
55 static const wxChar eHOSTNAME
[] = _T("HostName");
57 // For the following functions we SHOULD fill in support
58 // for Windows-NT (which I don't know) as I assume it begin
59 // a POSIX Unix (so claims MS) that it has some special
60 // functions beyond those provided by WinSock
62 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
63 bool wxGetHostName( wxChar
* zBuf
, int nMaxSize
)
65 if (!zBuf
) return false;
67 #if defined(wxUSE_NET_API) && wxUSE_NET_API
70 unsigned long ulLevel
= 0;
71 unsigned char* zBuffer
= NULL
;
72 unsigned long ulBuffer
= 256;
73 unsigned long* pulTotalAvail
= NULL
;
75 NetBios32GetInfo( (const unsigned char*)zServer
76 ,(const unsigned char*)zComputer
82 strcpy(zBuf
, zServer
);
85 const wxChar
* zDefaultHost
= _T("noname");
87 if ((zSysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
89 ::PrfQueryProfileString( HINI_PROFILE
99 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
102 zBuf
[nMaxSize
] = _T('\0');
105 return *zBuf
? true : false;
108 // Get user ID e.g. jacs
109 bool wxGetUserId(wxChar
* zBuf
, int nType
)
111 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
112 // UPM procs return 0 on success
113 long lrc
= U32ELOCU((PUCHAR
)zBuf
, (PULONG
)&nType
);
114 if (lrc
== 0) return true;
119 bool wxGetUserName( wxChar
* zBuf
, int nMaxSize
)
122 wxGetUserId( zBuf
, nMaxSize
);
124 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
129 int wxKill(long lPid
,
130 wxSignal
WXUNUSED(eSig
),
131 wxKillError
* WXUNUSED(peError
),
134 return((int)::DosKillProcess(0, (PID
)lPid
));
138 // Execute a program in an Interactive Shell
140 bool wxShell( 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 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
202 wxMemorySize
wxGetFreeMemory()
204 void* pMemptr
= NULL
;
209 lMemFlags
= PAG_FREE
;
210 rc
= ::DosQueryMem(pMemptr
, (PULONG
)&lSize
, &lMemFlags
);
213 return (wxMemorySize
)lSize
;
217 unsigned long wxGetProcessId()
219 return (unsigned long)getpid();
222 // ----------------------------------------------------------------------------
224 // ----------------------------------------------------------------------------
226 bool wxGetEnv(const wxString
& var
, wxString
*value
)
228 // wxGetenv is defined as getenv()
229 wxChar
*p
= wxGetenv(var
);
241 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
243 #if defined(HAVE_SETENV)
244 return setenv(variable
.mb_str(), value
? wxString(value
).mb_str().data()
245 : NULL
, 1 /* overwrite */) == 0;
246 #elif defined(HAVE_PUTENV)
247 wxString s
= variable
;
249 s
<< _T('=') << value
;
252 const char *p
= s
.mb_str();
254 // the string will be free()d by libc
255 char *buf
= (char *)malloc(strlen(p
) + 1);
258 return putenv(buf
) == 0;
259 #else // no way to set an env var
260 wxUnusedVar(variable
);
267 unsigned long ulMilliseconds
270 ::DosSleep(ulMilliseconds
);
274 unsigned long ulMicroseconds
277 ::DosSleep(ulMicroseconds
/1000);
284 ::DosSleep(1000 * nSecs
);
287 // Consume all events until no more left
296 DosBeep(1000,1000); // 1kHz during 1 sec.
299 wxString
wxGetOsDescription()
301 wxString
strVer(_T("OS/2"));
302 ULONG ulSysInfo
[QSV_MAX
] = {0};
304 if (::DosQuerySysInfo( 1L,
307 sizeof(ULONG
) * QSV_MAX
311 ver
.Printf( _T(" ver. %d.%d rev. %c"),
312 int(ulSysInfo
[QSV_VERSION_MAJOR
] / 10),
313 int(ulSysInfo
[QSV_VERSION_MINOR
]),
314 char(ulSysInfo
[QSV_VERSION_REVISION
])
322 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab
))
326 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab
))
330 wxOperatingSystemId
wxGetOsVersion(int *verMaj
, int *verMin
)
332 static wxToolkitInfo vInfo
;
333 ULONG ulSysInfo
[QSV_MAX
] = {0};
336 ulrc
= ::DosQuerySysInfo( 1L
339 ,sizeof(ULONG
) * QSV_MAX
345 *verMaj
= ulSysInfo
[QSV_VERSION_MAJOR
] / 10;
347 *verMin
= ulSysInfo
[QSV_VERSION_MINOR
];
354 // ---------------------------------------------------------------------------
355 const wxChar
* wxGetHomeDir(
359 wxString
& rStrDir
= *pStr
;
361 // OS/2 has no idea about home,
362 // so use the working directory instead.
363 // However, we might have a valid HOME directory,
364 // as is used on many machines that have unix utilities
365 // on them, so we should use that, if available.
367 // 256 was taken from os2def.h
369 # define MAX_PATH 256
372 const wxChar
*szHome
= wxGetenv((wxChar
*)"HOME");
373 if ( szHome
== NULL
) {
374 // we're homeless, use current directory.
380 return rStrDir
.c_str();
385 const wxMB2WXbuf
wxGetUserHome( const wxString
&rUser
)
386 #else // just for binary compatibility -- there is no 'const' here
387 wxChar
* wxGetUserHome ( const wxString
&rUser
)
391 wxString
sUser1(rUser
);
393 wxChar
*wxBuffer
= new wxChar
[256];
399 if (wxGetUserId( zTmp
400 ,sizeof(zTmp
)/sizeof(char)
403 // Guests belong in the temp dir
404 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
406 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
407 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
408 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
410 return *zHome
? zHome
: (wxChar
*)_T("\\");
412 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
413 sUser1
= wxEmptyString
;
419 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
421 wxStrcpy(wxBuffer
, zHome
);
422 wxUnix2DosFilename(wxBuffer
);
424 wxWCharBuffer
retBuffer (wxBuffer
);
428 wxStrcpy(zHome
, wxBuffer
);
435 return (wxChar
*)wxEmptyString
; // No home known!
438 wxString
wxPMErrorToStr(ERRORID vError
)
443 // Remove the high order byte -- it is useless
445 vError
&= 0x0000ffff;
448 case PMERR_INVALID_HWND
:
449 sError
= wxT("Invalid window handle specified");
452 case PMERR_INVALID_FLAG
:
453 sError
= wxT("Invalid flag bit set");
456 case PMERR_NO_MSG_QUEUE
:
457 sError
= wxT("No message queue available");
460 case PMERR_INVALID_PARM
:
461 sError
= wxT("Parameter contained invalid data");
464 case PMERR_INVALID_PARAMETERS
:
465 sError
= wxT("Parameter value is out of range");
468 case PMERR_PARAMETER_OUT_OF_RANGE
:
469 sError
= wxT("Parameter value is out of range");
472 case PMERR_INVALID_INTEGER_ATOM
:
473 sError
= wxT("Not a valid atom");
476 case PMERR_INVALID_HATOMTBL
:
477 sError
= wxT("Atom table handle is invalid");
480 case PMERR_INVALID_ATOM_NAME
:
481 sError
= wxT("Not a valid atom name");
484 case PMERR_ATOM_NAME_NOT_FOUND
:
485 sError
= wxT("Valid name format, but cannot find name in atom table");
489 sError
= wxT("PMERR_INV_HPS");
493 sError
= wxT("PMERR_PS_BUSY");
496 case PMERR_INV_PRIMITIVE_TYPE
:
497 sError
= wxT("PMERR_INV_PRIMITIVE_TYPE");
500 case PMERR_UNSUPPORTED_ATTR
:
501 sError
= wxT("PMERR_UNSUPPORTED_ATTR");
504 case PMERR_INV_COLOR_ATTR
:
505 sError
= wxT("PMERR_INV_COLOR_ATTR");
508 case PMERR_INV_BACKGROUND_COL_ATTR
:
509 sError
= wxT("PMERR_INV_BACKGROUND_COL_ATTR");
512 case PMERR_INV_MIX_ATTR
:
513 sError
= wxT("PMERR_INV_MIX_ATTR");
516 case PMERR_INV_LINE_WIDTH_ATTR
:
517 sError
= wxT("PMERR_INV_LINE_WIDTH_ATTR");
520 case PMERR_INV_GEOM_LINE_WIDTH_ATTR
:
521 sError
= wxT("PMERR_INV_GEOM_LINE_WIDTH_ATTR");
524 case PMERR_INV_LINE_TYPE_ATTR
:
525 sError
= wxT("PMERR_INV_LINE_TYPE_ATTR");
528 case PMERR_INV_LINE_END_ATTR
:
529 sError
= wxT("PMERR_INV_LINE_END_ATTR");
532 case PMERR_INV_LINE_JOIN_ATTR
:
533 sError
= wxT("PMERR_INV_LINE_JOIN_ATTR");
536 case PMERR_INV_CHAR_SET_ATTR
:
537 sError
= wxT("PMERR_INV_CHAR_SET_ATTR");
540 case PMERR_INV_CHAR_MODE_ATTR
:
541 sError
= wxT("PMERR_INV_CHAR_MODE_ATTR");
544 case PMERR_INV_CHAR_DIRECTION_ATTR
:
545 sError
= wxT("PMERR_INV_CHAR_DIRECTION_ATTR");
548 case PMERR_INV_CHAR_SHEAR_ATTR
:
549 sError
= wxT("PMERR_INV_CHAR_SHEAR_ATTR");
552 case PMERR_INV_CHAR_ANGLE_ATTR
:
553 sError
= wxT("PMERR_INV_CHAR_ANGLE_ATTR");
556 case PMERR_INV_MARKER_SET_ATTR
:
557 sError
= wxT("PMERR_INV_MARKER_SET_ATTR");
560 case PMERR_INV_MARKER_SYMBOL_ATTR
:
561 sError
= wxT("PMERR_INV_MARKER_SYMBOL_ATTR");
564 case PMERR_INV_PATTERN_SET_ATTR
:
565 sError
= wxT("PMERR_INV_PATTERN_SET_ATTR");
568 case PMERR_INV_PATTERN_ATTR
:
569 sError
= wxT("PMERR_INV_PATTERN_ATTR");
572 case PMERR_INV_COORDINATE
:
573 sError
= wxT("PMERR_INV_COORDINATE");
576 case PMERR_UNSUPPORTED_ATTR_VALUE
:
577 sError
= wxT("PMERR_UNSUPPORTED_ATTR_VALUE");
580 case PMERR_INV_PATTERN_SET_FONT
:
581 sError
= wxT("PMERR_INV_PATTERN_SET_FONT");
584 case PMERR_HUGE_FONTS_NOT_SUPPORTED
:
585 sError
= wxT("PMERR_HUGE_FONTS_NOT_SUPPORTED");
589 sError
= wxT("Unknown error");
592 } // end of wxPMErrorToStr
594 // replacement for implementation in unix/utilsunx.cpp,
595 // to be used by all X11 based ports.
596 struct wxEndProcessData
;
598 void wxHandleProcessTermination(wxEndProcessData
*WXUNUSED(proc_data
))
600 // For now, just do nothing. To be filled in as needed.