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
&&
87 (zSysname
= wxGetenv(_T("HOSTNAME"))) == 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
.wx_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"));
304 if (::DosQuerySysInfo( QSV_VERSION_MINOR
,
311 ver
.Printf( _T(" ver. %d.%d"),
321 bool wxIsPlatform64Bit()
323 // FIXME: No idea how to test for 64 bit processor
324 // (Probably irrelevant anyhow, though).
328 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab
))
332 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab
))
336 wxOperatingSystemId
wxGetOsVersion(int *verMaj
, int *verMin
)
341 ulrc
= ::DosQuerySysInfo( QSV_VERSION_MINOR
,
350 *verMaj
= ulSysInfo
/ 10;
352 *verMin
= ulSysInfo
% 10;
359 // ---------------------------------------------------------------------------
360 const wxChar
* wxGetHomeDir(
364 wxString
& rStrDir
= *pStr
;
366 // OS/2 has no idea about home,
367 // so use the working directory instead.
368 // However, we might have a valid HOME directory,
369 // as is used on many machines that have unix utilities
370 // on them, so we should use that, if available.
372 // 256 was taken from os2def.h
374 # define MAX_PATH 256
377 const wxChar
*szHome
= wxGetenv((wxChar
*)"HOME");
378 if ( szHome
== NULL
) {
379 // we're homeless, use current directory.
385 return rStrDir
.c_str();
390 const wxMB2WXbuf
wxGetUserHome( const wxString
&rUser
)
391 #else // just for binary compatibility -- there is no 'const' here
392 wxChar
* wxGetUserHome ( const wxString
&rUser
)
396 wxString
sUser1(rUser
);
398 wxChar
*wxBuffer
= new wxChar
[256];
404 if (wxGetUserId( zTmp
405 ,sizeof(zTmp
)/sizeof(char)
408 // Guests belong in the temp dir
409 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
411 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
412 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
413 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
415 return *zHome
? zHome
: (wxChar
*)_T("\\");
417 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
418 sUser1
= wxEmptyString
;
424 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
426 wxStrcpy(wxBuffer
, zHome
);
427 wxUnix2DosFilename(wxBuffer
);
429 wxWCharBuffer
retBuffer (wxBuffer
);
433 wxStrcpy(zHome
, wxBuffer
);
440 return (wxChar
*)wxEmptyString
; // No home known!
443 bool wxGetDiskSpace(const wxString
& path
,
444 wxDiskspaceSize_t
*pTotal
,
445 wxDiskspaceSize_t
*pFree
)
451 FSALLOCATE fsaBuf
= {0};
452 APIRET rc
= NO_ERROR
;
457 if (wxDirExists(fn
.GetFullPath()) == false)
460 disknum
= 1 + wxToupper(fn
.GetVolume().GetChar(0)) - _T('A');
462 rc
= ::DosQueryFSInfo(disknum
, // 1 = A, 2 = B, 3 = C, ...
463 FSIL_ALLOC
, // allocation info
473 // to try to avoid 32-bit overflow, let's not multiply right away
474 // (num of alloc units)
475 *pTotal
= fsaBuf
.cUnit
;
476 // * (num of sectors per alloc unit) * (num of bytes per sector)
477 (*pTotal
) *= fsaBuf
.cSectorUnit
* fsaBuf
.cbSector
;
481 *pFree
= fsaBuf
.cUnitAvail
;
482 (*pFree
) *= fsaBuf
.cSectorUnit
* fsaBuf
.cbSector
;
488 wxString
wxPMErrorToStr(ERRORID vError
)
493 // Remove the high order byte -- it is useless
495 vError
&= 0x0000ffff;
498 case PMERR_INVALID_HWND
:
499 sError
= wxT("Invalid window handle specified");
502 case PMERR_INVALID_FLAG
:
503 sError
= wxT("Invalid flag bit set");
506 case PMERR_NO_MSG_QUEUE
:
507 sError
= wxT("No message queue available");
510 case PMERR_INVALID_PARM
:
511 sError
= wxT("Parameter contained invalid data");
514 case PMERR_INVALID_PARAMETERS
:
515 sError
= wxT("Parameter value is out of range");
518 case PMERR_PARAMETER_OUT_OF_RANGE
:
519 sError
= wxT("Parameter value is out of range");
522 case PMERR_INVALID_INTEGER_ATOM
:
523 sError
= wxT("Not a valid atom");
526 case PMERR_INVALID_HATOMTBL
:
527 sError
= wxT("Atom table handle is invalid");
530 case PMERR_INVALID_ATOM_NAME
:
531 sError
= wxT("Not a valid atom name");
534 case PMERR_ATOM_NAME_NOT_FOUND
:
535 sError
= wxT("Valid name format, but cannot find name in atom table");
539 sError
= wxT("PMERR_INV_HPS");
543 sError
= wxT("PMERR_PS_BUSY");
546 case PMERR_INV_PRIMITIVE_TYPE
:
547 sError
= wxT("PMERR_INV_PRIMITIVE_TYPE");
550 case PMERR_UNSUPPORTED_ATTR
:
551 sError
= wxT("PMERR_UNSUPPORTED_ATTR");
554 case PMERR_INV_COLOR_ATTR
:
555 sError
= wxT("PMERR_INV_COLOR_ATTR");
558 case PMERR_INV_BACKGROUND_COL_ATTR
:
559 sError
= wxT("PMERR_INV_BACKGROUND_COL_ATTR");
562 case PMERR_INV_MIX_ATTR
:
563 sError
= wxT("PMERR_INV_MIX_ATTR");
566 case PMERR_INV_LINE_WIDTH_ATTR
:
567 sError
= wxT("PMERR_INV_LINE_WIDTH_ATTR");
570 case PMERR_INV_GEOM_LINE_WIDTH_ATTR
:
571 sError
= wxT("PMERR_INV_GEOM_LINE_WIDTH_ATTR");
574 case PMERR_INV_LINE_TYPE_ATTR
:
575 sError
= wxT("PMERR_INV_LINE_TYPE_ATTR");
578 case PMERR_INV_LINE_END_ATTR
:
579 sError
= wxT("PMERR_INV_LINE_END_ATTR");
582 case PMERR_INV_LINE_JOIN_ATTR
:
583 sError
= wxT("PMERR_INV_LINE_JOIN_ATTR");
586 case PMERR_INV_CHAR_SET_ATTR
:
587 sError
= wxT("PMERR_INV_CHAR_SET_ATTR");
590 case PMERR_INV_CHAR_MODE_ATTR
:
591 sError
= wxT("PMERR_INV_CHAR_MODE_ATTR");
594 case PMERR_INV_CHAR_DIRECTION_ATTR
:
595 sError
= wxT("PMERR_INV_CHAR_DIRECTION_ATTR");
598 case PMERR_INV_CHAR_SHEAR_ATTR
:
599 sError
= wxT("PMERR_INV_CHAR_SHEAR_ATTR");
602 case PMERR_INV_CHAR_ANGLE_ATTR
:
603 sError
= wxT("PMERR_INV_CHAR_ANGLE_ATTR");
606 case PMERR_INV_MARKER_SET_ATTR
:
607 sError
= wxT("PMERR_INV_MARKER_SET_ATTR");
610 case PMERR_INV_MARKER_SYMBOL_ATTR
:
611 sError
= wxT("PMERR_INV_MARKER_SYMBOL_ATTR");
614 case PMERR_INV_PATTERN_SET_ATTR
:
615 sError
= wxT("PMERR_INV_PATTERN_SET_ATTR");
618 case PMERR_INV_PATTERN_ATTR
:
619 sError
= wxT("PMERR_INV_PATTERN_ATTR");
622 case PMERR_INV_COORDINATE
:
623 sError
= wxT("PMERR_INV_COORDINATE");
626 case PMERR_UNSUPPORTED_ATTR_VALUE
:
627 sError
= wxT("PMERR_UNSUPPORTED_ATTR_VALUE");
630 case PMERR_INV_PATTERN_SET_FONT
:
631 sError
= wxT("PMERR_INV_PATTERN_SET_FONT");
634 case PMERR_HUGE_FONTS_NOT_SUPPORTED
:
635 sError
= wxT("PMERR_HUGE_FONTS_NOT_SUPPORTED");
639 sError
= wxT("Unknown error");
642 } // end of wxPMErrorToStr
644 // replacement for implementation in unix/utilsunx.cpp,
645 // to be used by all X11 based ports.
646 struct wxEndProcessData
;
648 void wxHandleProcessTermination(wxEndProcessData
*WXUNUSED(proc_data
))
650 // For now, just do nothing. To be filled in as needed.