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
[] = wxT("wxWidgets");
54 static const wxChar eHOSTNAME
[] = wxT("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
= wxT("noname");
86 if ((zSysname
= wxGetenv(wxT("SYSTEM_NAME"))) == NULL
&&
87 (zSysname
= wxGetenv(wxT("HOSTNAME"))) == NULL
)
89 ::PrfQueryProfileString( HINI_PROFILE
96 zBuf
[nMaxSize
] = wxT('\0');
100 wxStrlcpy(zBuf
, zSysname
, nMaxSize
);
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 wxStrlcpy(zBuf
, wxT("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
= wxT("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
= wxT("/C ") + rCommand
;
159 SData
.PgmInputs
= (BYTE
*)sInputs
.wx_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((const wxChar
*)var
);
240 static bool wxDoSetEnv(const wxString
& variable
, const char *value
)
242 #if defined(HAVE_SETENV)
246 return unsetenv(variable
.mb_str()) == 0;
248 value
= wxT(""); // mustn't pass NULL to setenv()
251 return setenv(variable
.mb_str(), value
, 1 /* overwrite */) == 0;
252 #elif defined(HAVE_PUTENV)
253 wxString s
= variable
;
255 s
<< wxT('=') << value
;
258 const char *p
= s
.mb_str();
260 // the string will be free()d by libc
261 char *buf
= (char *)malloc(strlen(p
) + 1);
264 return putenv(buf
) == 0;
265 #else // no way to set an env var
266 wxUnusedVar(variable
);
272 bool wxSetEnv(const wxString
& variable
, const wxString
& value
)
274 return wxDoSetEnv(variable
, value
.mb_str());
277 bool wxUnsetEnv(const wxString
& variable
)
279 return wxDoSetEnv(variable
, NULL
);
283 unsigned long ulMilliseconds
286 ::DosSleep(ulMilliseconds
);
290 unsigned long ulMicroseconds
293 ::DosSleep(ulMicroseconds
/1000);
300 ::DosSleep(1000 * nSecs
);
303 // Consume all events until no more left
312 DosBeep(1000,1000); // 1kHz during 1 sec.
315 wxString
wxGetOsDescription()
317 wxString
strVer(wxT("OS/2"));
320 if (::DosQuerySysInfo( QSV_VERSION_MINOR
,
327 ver
.Printf( wxT(" ver. %d.%d"),
337 bool wxIsPlatform64Bit()
339 // FIXME: No idea how to test for 64 bit processor
340 // (Probably irrelevant anyhow, though).
344 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab
))
348 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab
))
352 wxOperatingSystemId
wxGetOsVersion(int *verMaj
, int *verMin
)
357 ulrc
= ::DosQuerySysInfo( QSV_VERSION_MINOR
,
366 *verMaj
= ulSysInfo
/ 10;
368 *verMin
= ulSysInfo
% 10;
375 // ---------------------------------------------------------------------------
376 const wxChar
* wxGetHomeDir(
380 wxString
& rStrDir
= *pStr
;
382 // OS/2 has no idea about home,
383 // so use the working directory instead.
384 // However, we might have a valid HOME directory,
385 // as is used on many machines that have unix utilities
386 // on them, so we should use that, if available.
388 // 256 was taken from os2def.h
390 # define MAX_PATH 256
393 const wxChar
*szHome
= wxGetenv((wxChar
*)"HOME");
394 if ( szHome
== NULL
) {
395 // we're homeless, use current directory.
401 return rStrDir
.c_str();
404 wxString
wxGetUserHome ( const wxString
&rUser
)
407 wxString
sUser(rUser
);
414 const wxString currentUser
= wxGetUserId();
416 // Guests belong in the temp dir
417 if ( currentUser
== "annonymous" )
419 zHome
= wxGetenv(wxT("TMP"));
421 zHome
= wxGetenv(wxT("TMPDIR"));
423 zHome
= wxGetenv(wxT("TEMP"));
425 if ( zHome
&& *zHome
)
429 if ( sUser
== currentUser
)
435 if ((zHome
= wxGetenv(wxT("HOME"))) != NULL
)
438 home
.Replace("/", "\\");
445 bool wxGetDiskSpace(const wxString
& path
,
446 wxDiskspaceSize_t
*pTotal
,
447 wxDiskspaceSize_t
*pFree
)
453 FSALLOCATE fsaBuf
= {0};
454 APIRET rc
= NO_ERROR
;
459 if (wxDirExists(fn
.GetFullPath()) == false)
462 disknum
= wxToupper(fn
.GetVolume().GetChar(0)) - wxT('A') + 1;
464 rc
= ::DosQueryFSInfo(disknum
, // 1 = A, 2 = B, 3 = C, ...
465 FSIL_ALLOC
, // allocation info
475 // to try to avoid 32-bit overflow, let's not multiply right away
476 // (num of alloc units)
477 *pTotal
= fsaBuf
.cUnit
;
478 // * (num of sectors per alloc unit) * (num of bytes per sector)
479 (*pTotal
) *= fsaBuf
.cSectorUnit
* fsaBuf
.cbSector
;
483 *pFree
= fsaBuf
.cUnitAvail
;
484 (*pFree
) *= fsaBuf
.cSectorUnit
* fsaBuf
.cbSector
;
490 wxString
wxPMErrorToStr(ERRORID vError
)
495 // Remove the high order byte -- it is useless
497 vError
&= 0x0000ffff;
500 case PMERR_INVALID_HWND
:
501 sError
= wxT("Invalid window handle specified");
504 case PMERR_INVALID_FLAG
:
505 sError
= wxT("Invalid flag bit set");
508 case PMERR_NO_MSG_QUEUE
:
509 sError
= wxT("No message queue available");
512 case PMERR_INVALID_PARM
:
513 sError
= wxT("Parameter contained invalid data");
516 case PMERR_INVALID_PARAMETERS
:
517 sError
= wxT("Parameter value is out of range");
520 case PMERR_PARAMETER_OUT_OF_RANGE
:
521 sError
= wxT("Parameter value is out of range");
524 case PMERR_INVALID_INTEGER_ATOM
:
525 sError
= wxT("Not a valid atom");
528 case PMERR_INVALID_HATOMTBL
:
529 sError
= wxT("Atom table handle is invalid");
532 case PMERR_INVALID_ATOM_NAME
:
533 sError
= wxT("Not a valid atom name");
536 case PMERR_ATOM_NAME_NOT_FOUND
:
537 sError
= wxT("Valid name format, but cannot find name in atom table");
541 sError
= wxT("PMERR_INV_HPS");
545 sError
= wxT("PMERR_PS_BUSY");
548 case PMERR_INV_PRIMITIVE_TYPE
:
549 sError
= wxT("PMERR_INV_PRIMITIVE_TYPE");
552 case PMERR_UNSUPPORTED_ATTR
:
553 sError
= wxT("PMERR_UNSUPPORTED_ATTR");
556 case PMERR_INV_COLOR_ATTR
:
557 sError
= wxT("PMERR_INV_COLOR_ATTR");
560 case PMERR_INV_BACKGROUND_COL_ATTR
:
561 sError
= wxT("PMERR_INV_BACKGROUND_COL_ATTR");
564 case PMERR_INV_MIX_ATTR
:
565 sError
= wxT("PMERR_INV_MIX_ATTR");
568 case PMERR_INV_LINE_WIDTH_ATTR
:
569 sError
= wxT("PMERR_INV_LINE_WIDTH_ATTR");
572 case PMERR_INV_GEOM_LINE_WIDTH_ATTR
:
573 sError
= wxT("PMERR_INV_GEOM_LINE_WIDTH_ATTR");
576 case PMERR_INV_LINE_TYPE_ATTR
:
577 sError
= wxT("PMERR_INV_LINE_TYPE_ATTR");
580 case PMERR_INV_LINE_END_ATTR
:
581 sError
= wxT("PMERR_INV_LINE_END_ATTR");
584 case PMERR_INV_LINE_JOIN_ATTR
:
585 sError
= wxT("PMERR_INV_LINE_JOIN_ATTR");
588 case PMERR_INV_CHAR_SET_ATTR
:
589 sError
= wxT("PMERR_INV_CHAR_SET_ATTR");
592 case PMERR_INV_CHAR_MODE_ATTR
:
593 sError
= wxT("PMERR_INV_CHAR_MODE_ATTR");
596 case PMERR_INV_CHAR_DIRECTION_ATTR
:
597 sError
= wxT("PMERR_INV_CHAR_DIRECTION_ATTR");
600 case PMERR_INV_CHAR_SHEAR_ATTR
:
601 sError
= wxT("PMERR_INV_CHAR_SHEAR_ATTR");
604 case PMERR_INV_CHAR_ANGLE_ATTR
:
605 sError
= wxT("PMERR_INV_CHAR_ANGLE_ATTR");
608 case PMERR_INV_MARKER_SET_ATTR
:
609 sError
= wxT("PMERR_INV_MARKER_SET_ATTR");
612 case PMERR_INV_MARKER_SYMBOL_ATTR
:
613 sError
= wxT("PMERR_INV_MARKER_SYMBOL_ATTR");
616 case PMERR_INV_PATTERN_SET_ATTR
:
617 sError
= wxT("PMERR_INV_PATTERN_SET_ATTR");
620 case PMERR_INV_PATTERN_ATTR
:
621 sError
= wxT("PMERR_INV_PATTERN_ATTR");
624 case PMERR_INV_COORDINATE
:
625 sError
= wxT("PMERR_INV_COORDINATE");
628 case PMERR_UNSUPPORTED_ATTR_VALUE
:
629 sError
= wxT("PMERR_UNSUPPORTED_ATTR_VALUE");
632 case PMERR_INV_PATTERN_SET_FONT
:
633 sError
= wxT("PMERR_INV_PATTERN_SET_FONT");
636 case PMERR_HUGE_FONTS_NOT_SUPPORTED
:
637 sError
= wxT("PMERR_HUGE_FONTS_NOT_SUPPORTED");
641 sError
= wxT("Unknown error");
644 } // end of wxPMErrorToStr
646 // replacement for implementation in unix/utilsunx.cpp,
647 // to be used by all X11 based ports.
648 struct wxEndProcessData
;
650 void wxHandleProcessTermination(wxEndProcessData
*WXUNUSED(proc_data
))
652 // For now, just do nothing. To be filled in as needed.