1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/utils.cpp
3 // Purpose: Various utilities
4 // Author: David Webster
7 // Copyright: (c) David Webster
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
21 #include "wx/os2/private.h"
22 #include "wx/apptrait.h"
23 #include "wx/filename.h"
41 #if defined(__WATCOMC__)
46 #elif !defined(__EMX__)
52 static const wxChar WX_SECTION
[] = wxT("wxWidgets");
53 static const wxChar eHOSTNAME
[] = wxT("HostName");
55 // For the following functions we SHOULD fill in support
56 // for Windows-NT (which I don't know) as I assume it begin
57 // a POSIX Unix (so claims MS) that it has some special
58 // functions beyond those provided by WinSock
60 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
61 bool wxGetHostName( wxChar
* zBuf
, int nMaxSize
)
63 if (!zBuf
) return false;
65 #if defined(wxUSE_NET_API) && wxUSE_NET_API
68 unsigned long ulLevel
= 0;
69 unsigned char* zBuffer
= NULL
;
70 unsigned long ulBuffer
= 256;
71 unsigned long* pulTotalAvail
= NULL
;
73 NetBios32GetInfo( (const unsigned char*)zServer
74 ,(const unsigned char*)zComputer
80 strcpy(zBuf
, zServer
);
83 const wxChar
* zDefaultHost
= wxT("noname");
85 if ((zSysname
= wxGetenv(wxT("SYSTEM_NAME"))) == NULL
&&
86 (zSysname
= wxGetenv(wxT("HOSTNAME"))) == NULL
)
88 ::PrfQueryProfileString( HINI_PROFILE
95 zBuf
[nMaxSize
] = wxT('\0');
99 wxStrlcpy(zBuf
, zSysname
, nMaxSize
);
103 return *zBuf
? true : false;
106 // Get user ID e.g. jacs
107 bool wxGetUserId(wxChar
* zBuf
, int nType
)
109 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
110 // UPM procs return 0 on success
111 long lrc
= U32ELOCU((PUCHAR
)zBuf
, (PULONG
)&nType
);
112 if (lrc
== 0) return true;
117 bool wxGetUserName( wxChar
* zBuf
, int nMaxSize
)
120 wxGetUserId( zBuf
, nMaxSize
);
122 wxStrlcpy(zBuf
, wxT("Unknown User"), nMaxSize
);
127 int wxKill(long lPid
,
128 wxSignal
WXUNUSED(eSig
),
129 wxKillError
* WXUNUSED(peError
),
132 return((int)::DosKillProcess(0, (PID
)lPid
));
136 // Execute a program in an Interactive Shell
138 bool wxShell( const wxString
& rCommand
)
140 wxChar
* zShell
= wxT("CMD.EXE");
142 STARTDATA SData
= {0};
143 PSZ PgmTitle
= "Command Shell";
147 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
150 SData
.Length
= sizeof(STARTDATA
);
151 SData
.Related
= SSF_RELATED_INDEPENDENT
;
152 SData
.FgBg
= SSF_FGBG_FORE
;
153 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
154 SData
.PgmTitle
= PgmTitle
;
155 SData
.PgmName
= (char*)zShell
;
157 sInputs
= wxT("/C ") + rCommand
;
158 SData
.PgmInputs
= (BYTE
*)sInputs
.wx_str();
160 SData
.Environment
= 0;
161 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
162 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
165 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
168 SData
.InitXSize
= 200;
169 SData
.InitYSize
= 140;
171 SData
.ObjectBuffer
= (char*)achObjBuf
;
172 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
174 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
175 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
180 ::DosGetInfoBlocks(&ptib
, &ppib
);
182 ::DosWaitChild( DCWA_PROCESS
192 // Shutdown or reboot the PC
193 bool wxShutdown(wxShutdownFlags
WXUNUSED(wFlags
))
199 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
200 wxMemorySize
wxGetFreeMemory()
202 void* pMemptr
= NULL
;
207 lMemFlags
= PAG_FREE
;
208 rc
= ::DosQueryMem(pMemptr
, (PULONG
)&lSize
, &lMemFlags
);
211 return (wxMemorySize
)lSize
;
215 unsigned long wxGetProcessId()
217 return (unsigned long)getpid();
220 // ----------------------------------------------------------------------------
222 // ----------------------------------------------------------------------------
224 bool wxGetEnv(const wxString
& var
, wxString
*value
)
226 // wxGetenv is defined as getenv()
227 wxChar
*p
= wxGetenv((const wxChar
*)var
);
239 static bool wxDoSetEnv(const wxString
& variable
, const char *value
)
241 #if defined(HAVE_SETENV)
245 return unsetenv(variable
.mb_str()) == 0;
247 value
= wxT(""); // mustn't pass NULL to setenv()
250 return setenv(variable
.mb_str(), value
, 1 /* overwrite */) == 0;
251 #elif defined(HAVE_PUTENV)
252 wxString s
= variable
;
254 s
<< wxT('=') << value
;
257 const char *p
= s
.mb_str();
259 // the string will be free()d by libc
260 char *buf
= (char *)malloc(strlen(p
) + 1);
263 return putenv(buf
) == 0;
264 #else // no way to set an env var
265 wxUnusedVar(variable
);
271 bool wxSetEnv(const wxString
& variable
, const wxString
& value
)
273 return wxDoSetEnv(variable
, value
.mb_str());
276 bool wxUnsetEnv(const wxString
& variable
)
278 return wxDoSetEnv(variable
, NULL
);
282 unsigned long ulMilliseconds
285 ::DosSleep(ulMilliseconds
);
289 unsigned long ulMicroseconds
292 ::DosSleep(ulMicroseconds
/1000);
299 ::DosSleep(1000 * nSecs
);
302 // Consume all events until no more left
308 wxString
wxGetOsDescription()
310 wxString
strVer(wxT("OS/2"));
313 if (::DosQuerySysInfo( QSV_VERSION_MINOR
,
320 ver
.Printf( wxT(" ver. %d.%d"),
330 bool wxIsPlatform64Bit()
332 // FIXME: No idea how to test for 64 bit processor
333 // (Probably irrelevant anyhow, though).
337 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab
))
341 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab
))
345 wxOperatingSystemId
wxGetOsVersion(int *verMaj
, int *verMin
)
350 ulrc
= ::DosQuerySysInfo( QSV_VERSION_MINOR
,
359 *verMaj
= ulSysInfo
/ 10;
361 *verMin
= ulSysInfo
% 10;
368 // ---------------------------------------------------------------------------
369 const wxChar
* wxGetHomeDir(
373 wxString
& rStrDir
= *pStr
;
375 // OS/2 has no idea about home,
376 // so use the working directory instead.
377 // However, we might have a valid HOME directory,
378 // as is used on many machines that have unix utilities
379 // on them, so we should use that, if available.
381 // 256 was taken from os2def.h
383 # define MAX_PATH 256
386 const wxChar
*szHome
= wxGetenv((wxChar
*)"HOME");
387 if ( szHome
== NULL
) {
388 // we're homeless, use current directory.
394 return rStrDir
.c_str();
397 wxString
wxGetUserHome ( const wxString
&rUser
)
400 wxString
sUser(rUser
);
407 const wxString currentUser
= wxGetUserId();
409 // Guests belong in the temp dir
410 if ( currentUser
== "annonymous" )
412 zHome
= wxGetenv(wxT("TMP"));
414 zHome
= wxGetenv(wxT("TMPDIR"));
416 zHome
= wxGetenv(wxT("TEMP"));
418 if ( zHome
&& *zHome
)
422 if ( sUser
== currentUser
)
428 if ((zHome
= wxGetenv(wxT("HOME"))) != NULL
)
431 home
.Replace("/", "\\");
438 bool wxGetDiskSpace(const wxString
& path
,
439 wxDiskspaceSize_t
*pTotal
,
440 wxDiskspaceSize_t
*pFree
)
446 FSALLOCATE fsaBuf
= {0};
447 APIRET rc
= NO_ERROR
;
452 if (wxDirExists(fn
.GetFullPath()) == false)
455 disknum
= wxToupper(fn
.GetVolume().GetChar(0)) - wxT('A') + 1;
457 rc
= ::DosQueryFSInfo(disknum
, // 1 = A, 2 = B, 3 = C, ...
458 FSIL_ALLOC
, // allocation info
468 // to try to avoid 32-bit overflow, let's not multiply right away
469 // (num of alloc units)
470 *pTotal
= fsaBuf
.cUnit
;
471 // * (num of sectors per alloc unit) * (num of bytes per sector)
472 (*pTotal
) *= fsaBuf
.cSectorUnit
* fsaBuf
.cbSector
;
476 *pFree
= fsaBuf
.cUnitAvail
;
477 (*pFree
) *= fsaBuf
.cSectorUnit
* fsaBuf
.cbSector
;
483 wxString
wxPMErrorToStr(ERRORID vError
)
488 // Remove the high order byte -- it is useless
490 vError
&= 0x0000ffff;
493 case PMERR_INVALID_HWND
:
494 sError
= wxT("Invalid window handle specified");
497 case PMERR_INVALID_FLAG
:
498 sError
= wxT("Invalid flag bit set");
501 case PMERR_NO_MSG_QUEUE
:
502 sError
= wxT("No message queue available");
505 case PMERR_INVALID_PARM
:
506 sError
= wxT("Parameter contained invalid data");
509 case PMERR_INVALID_PARAMETERS
:
510 sError
= wxT("Parameter value is out of range");
513 case PMERR_PARAMETER_OUT_OF_RANGE
:
514 sError
= wxT("Parameter value is out of range");
517 case PMERR_INVALID_INTEGER_ATOM
:
518 sError
= wxT("Not a valid atom");
521 case PMERR_INVALID_HATOMTBL
:
522 sError
= wxT("Atom table handle is invalid");
525 case PMERR_INVALID_ATOM_NAME
:
526 sError
= wxT("Not a valid atom name");
529 case PMERR_ATOM_NAME_NOT_FOUND
:
530 sError
= wxT("Valid name format, but cannot find name in atom table");
534 sError
= wxT("PMERR_INV_HPS");
538 sError
= wxT("PMERR_PS_BUSY");
541 case PMERR_INV_PRIMITIVE_TYPE
:
542 sError
= wxT("PMERR_INV_PRIMITIVE_TYPE");
545 case PMERR_UNSUPPORTED_ATTR
:
546 sError
= wxT("PMERR_UNSUPPORTED_ATTR");
549 case PMERR_INV_COLOR_ATTR
:
550 sError
= wxT("PMERR_INV_COLOR_ATTR");
553 case PMERR_INV_BACKGROUND_COL_ATTR
:
554 sError
= wxT("PMERR_INV_BACKGROUND_COL_ATTR");
557 case PMERR_INV_MIX_ATTR
:
558 sError
= wxT("PMERR_INV_MIX_ATTR");
561 case PMERR_INV_LINE_WIDTH_ATTR
:
562 sError
= wxT("PMERR_INV_LINE_WIDTH_ATTR");
565 case PMERR_INV_GEOM_LINE_WIDTH_ATTR
:
566 sError
= wxT("PMERR_INV_GEOM_LINE_WIDTH_ATTR");
569 case PMERR_INV_LINE_TYPE_ATTR
:
570 sError
= wxT("PMERR_INV_LINE_TYPE_ATTR");
573 case PMERR_INV_LINE_END_ATTR
:
574 sError
= wxT("PMERR_INV_LINE_END_ATTR");
577 case PMERR_INV_LINE_JOIN_ATTR
:
578 sError
= wxT("PMERR_INV_LINE_JOIN_ATTR");
581 case PMERR_INV_CHAR_SET_ATTR
:
582 sError
= wxT("PMERR_INV_CHAR_SET_ATTR");
585 case PMERR_INV_CHAR_MODE_ATTR
:
586 sError
= wxT("PMERR_INV_CHAR_MODE_ATTR");
589 case PMERR_INV_CHAR_DIRECTION_ATTR
:
590 sError
= wxT("PMERR_INV_CHAR_DIRECTION_ATTR");
593 case PMERR_INV_CHAR_SHEAR_ATTR
:
594 sError
= wxT("PMERR_INV_CHAR_SHEAR_ATTR");
597 case PMERR_INV_CHAR_ANGLE_ATTR
:
598 sError
= wxT("PMERR_INV_CHAR_ANGLE_ATTR");
601 case PMERR_INV_MARKER_SET_ATTR
:
602 sError
= wxT("PMERR_INV_MARKER_SET_ATTR");
605 case PMERR_INV_MARKER_SYMBOL_ATTR
:
606 sError
= wxT("PMERR_INV_MARKER_SYMBOL_ATTR");
609 case PMERR_INV_PATTERN_SET_ATTR
:
610 sError
= wxT("PMERR_INV_PATTERN_SET_ATTR");
613 case PMERR_INV_PATTERN_ATTR
:
614 sError
= wxT("PMERR_INV_PATTERN_ATTR");
617 case PMERR_INV_COORDINATE
:
618 sError
= wxT("PMERR_INV_COORDINATE");
621 case PMERR_UNSUPPORTED_ATTR_VALUE
:
622 sError
= wxT("PMERR_UNSUPPORTED_ATTR_VALUE");
625 case PMERR_INV_PATTERN_SET_FONT
:
626 sError
= wxT("PMERR_INV_PATTERN_SET_FONT");
629 case PMERR_HUGE_FONTS_NOT_SUPPORTED
:
630 sError
= wxT("PMERR_HUGE_FONTS_NOT_SUPPORTED");
634 sError
= wxT("Unknown error");
637 } // end of wxPMErrorToStr