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"
22 #include "wx/os2/private.h"
23 #include "wx/apptrait.h"
24 #include "wx/filename.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"));
303 if (::DosQuerySysInfo( QSV_VERSION_MINOR
,
310 ver
.Printf( _T(" ver. %d.%d"),
320 bool wxIsPlatform64Bit()
322 // FIXME: No idea how to test for 64 bit processor
323 // (Probably irrelevant anyhow, though).
327 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab
))
331 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab
))
335 wxOperatingSystemId
wxGetOsVersion(int *verMaj
, int *verMin
)
340 ulrc
= ::DosQuerySysInfo( QSV_VERSION_MINOR
,
349 *verMaj
= ulSysInfo
/ 10;
351 *verMin
= ulSysInfo
% 10;
358 // ---------------------------------------------------------------------------
359 const wxChar
* wxGetHomeDir(
363 wxString
& rStrDir
= *pStr
;
365 // OS/2 has no idea about home,
366 // so use the working directory instead.
367 // However, we might have a valid HOME directory,
368 // as is used on many machines that have unix utilities
369 // on them, so we should use that, if available.
371 // 256 was taken from os2def.h
373 # define MAX_PATH 256
376 const wxChar
*szHome
= wxGetenv((wxChar
*)"HOME");
377 if ( szHome
== NULL
) {
378 // we're homeless, use current directory.
384 return rStrDir
.c_str();
389 const wxMB2WXbuf
wxGetUserHome( const wxString
&rUser
)
390 #else // just for binary compatibility -- there is no 'const' here
391 wxChar
* wxGetUserHome ( const wxString
&rUser
)
395 wxString
sUser1(rUser
);
397 wxChar
*wxBuffer
= new wxChar
[256];
403 if (wxGetUserId( zTmp
404 ,sizeof(zTmp
)/sizeof(char)
407 // Guests belong in the temp dir
408 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
410 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
411 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
412 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
414 return *zHome
? zHome
: (wxChar
*)_T("\\");
416 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
417 sUser1
= wxEmptyString
;
423 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
425 wxStrcpy(wxBuffer
, zHome
);
426 wxUnix2DosFilename(wxBuffer
);
428 wxWCharBuffer
retBuffer (wxBuffer
);
432 wxStrcpy(zHome
, wxBuffer
);
439 return (wxChar
*)wxEmptyString
; // No home known!
442 bool wxGetDiskSpace(const wxString
& path
,
443 wxDiskspaceSize_t
*pTotal
,
444 wxDiskspaceSize_t
*pFree
)
450 FSALLOCATE fsaBuf
= {0};
451 APIRET rc
= NO_ERROR
;
456 if (wxDirExists(fn
.GetFullPath()) == false)
459 disknum
= 1 + wxToupper(fn
.GetVolume().GetChar(0)) - _T('A');
461 rc
= ::DosQueryFSInfo(disknum
, // 1 = A, 2 = B, 3 = C, ...
462 FSIL_ALLOC
, // allocation info
472 // to try to avoid 32-bit overflow, let's not multiply right away
473 // (num of alloc units)
474 *pTotal
= fsaBuf
.cUnit
;
475 // * (num of sectors per alloc unit) * (num of bytes per sector)
476 (*pTotal
) *= fsaBuf
.cSectorUnit
* fsaBuf
.cbSector
;
480 *pFree
= fsaBuf
.cUnitAvail
;
481 (*pFree
) *= fsaBuf
.cSectorUnit
* fsaBuf
.cbSector
;
487 wxString
wxPMErrorToStr(ERRORID vError
)
492 // Remove the high order byte -- it is useless
494 vError
&= 0x0000ffff;
497 case PMERR_INVALID_HWND
:
498 sError
= wxT("Invalid window handle specified");
501 case PMERR_INVALID_FLAG
:
502 sError
= wxT("Invalid flag bit set");
505 case PMERR_NO_MSG_QUEUE
:
506 sError
= wxT("No message queue available");
509 case PMERR_INVALID_PARM
:
510 sError
= wxT("Parameter contained invalid data");
513 case PMERR_INVALID_PARAMETERS
:
514 sError
= wxT("Parameter value is out of range");
517 case PMERR_PARAMETER_OUT_OF_RANGE
:
518 sError
= wxT("Parameter value is out of range");
521 case PMERR_INVALID_INTEGER_ATOM
:
522 sError
= wxT("Not a valid atom");
525 case PMERR_INVALID_HATOMTBL
:
526 sError
= wxT("Atom table handle is invalid");
529 case PMERR_INVALID_ATOM_NAME
:
530 sError
= wxT("Not a valid atom name");
533 case PMERR_ATOM_NAME_NOT_FOUND
:
534 sError
= wxT("Valid name format, but cannot find name in atom table");
538 sError
= wxT("PMERR_INV_HPS");
542 sError
= wxT("PMERR_PS_BUSY");
545 case PMERR_INV_PRIMITIVE_TYPE
:
546 sError
= wxT("PMERR_INV_PRIMITIVE_TYPE");
549 case PMERR_UNSUPPORTED_ATTR
:
550 sError
= wxT("PMERR_UNSUPPORTED_ATTR");
553 case PMERR_INV_COLOR_ATTR
:
554 sError
= wxT("PMERR_INV_COLOR_ATTR");
557 case PMERR_INV_BACKGROUND_COL_ATTR
:
558 sError
= wxT("PMERR_INV_BACKGROUND_COL_ATTR");
561 case PMERR_INV_MIX_ATTR
:
562 sError
= wxT("PMERR_INV_MIX_ATTR");
565 case PMERR_INV_LINE_WIDTH_ATTR
:
566 sError
= wxT("PMERR_INV_LINE_WIDTH_ATTR");
569 case PMERR_INV_GEOM_LINE_WIDTH_ATTR
:
570 sError
= wxT("PMERR_INV_GEOM_LINE_WIDTH_ATTR");
573 case PMERR_INV_LINE_TYPE_ATTR
:
574 sError
= wxT("PMERR_INV_LINE_TYPE_ATTR");
577 case PMERR_INV_LINE_END_ATTR
:
578 sError
= wxT("PMERR_INV_LINE_END_ATTR");
581 case PMERR_INV_LINE_JOIN_ATTR
:
582 sError
= wxT("PMERR_INV_LINE_JOIN_ATTR");
585 case PMERR_INV_CHAR_SET_ATTR
:
586 sError
= wxT("PMERR_INV_CHAR_SET_ATTR");
589 case PMERR_INV_CHAR_MODE_ATTR
:
590 sError
= wxT("PMERR_INV_CHAR_MODE_ATTR");
593 case PMERR_INV_CHAR_DIRECTION_ATTR
:
594 sError
= wxT("PMERR_INV_CHAR_DIRECTION_ATTR");
597 case PMERR_INV_CHAR_SHEAR_ATTR
:
598 sError
= wxT("PMERR_INV_CHAR_SHEAR_ATTR");
601 case PMERR_INV_CHAR_ANGLE_ATTR
:
602 sError
= wxT("PMERR_INV_CHAR_ANGLE_ATTR");
605 case PMERR_INV_MARKER_SET_ATTR
:
606 sError
= wxT("PMERR_INV_MARKER_SET_ATTR");
609 case PMERR_INV_MARKER_SYMBOL_ATTR
:
610 sError
= wxT("PMERR_INV_MARKER_SYMBOL_ATTR");
613 case PMERR_INV_PATTERN_SET_ATTR
:
614 sError
= wxT("PMERR_INV_PATTERN_SET_ATTR");
617 case PMERR_INV_PATTERN_ATTR
:
618 sError
= wxT("PMERR_INV_PATTERN_ATTR");
621 case PMERR_INV_COORDINATE
:
622 sError
= wxT("PMERR_INV_COORDINATE");
625 case PMERR_UNSUPPORTED_ATTR_VALUE
:
626 sError
= wxT("PMERR_UNSUPPORTED_ATTR_VALUE");
629 case PMERR_INV_PATTERN_SET_FONT
:
630 sError
= wxT("PMERR_INV_PATTERN_SET_FONT");
633 case PMERR_HUGE_FONTS_NOT_SUPPORTED
:
634 sError
= wxT("PMERR_HUGE_FONTS_NOT_SUPPORTED");
638 sError
= wxT("Unknown error");
641 } // end of wxPMErrorToStr
643 // replacement for implementation in unix/utilsunx.cpp,
644 // to be used by all X11 based ports.
645 struct wxEndProcessData
;
647 void wxHandleProcessTermination(wxEndProcessData
*WXUNUSED(proc_data
))
649 // For now, just do nothing. To be filled in as needed.