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"
41 #if defined(__WATCOMC__)
46 #elif !defined(__EMX__)
52 static const wxChar WX_SECTION
[] = _T("wxWidgets");
53 static const wxChar eHOSTNAME
[] = _T("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
= _T("noname");
85 if ((zSysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
87 ::PrfQueryProfileString( HINI_PROFILE
97 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
100 zBuf
[nMaxSize
] = _T('\0');
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 wxStrncpy(zBuf
, _T("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
= _T("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
= _T("/C ") + rCommand
;
158 SData
.PgmInputs
= (BYTE
*)sInputs
.c_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(var
);
239 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
241 #if defined(HAVE_SETENV)
242 return setenv(variable
.mb_str(), value
? wxString(value
).mb_str().data()
243 : NULL
, 1 /* overwrite */) == 0;
244 #elif defined(HAVE_PUTENV)
245 wxString s
= variable
;
247 s
<< _T('=') << value
;
250 const char *p
= s
.mb_str();
252 // the string will be free()d by libc
253 char *buf
= (char *)malloc(strlen(p
) + 1);
256 return putenv(buf
) == 0;
257 #else // no way to set an env var
258 wxUnusedVar(variable
);
265 unsigned long ulMilliseconds
268 ::DosSleep(ulMilliseconds
);
272 unsigned long ulMicroseconds
275 ::DosSleep(ulMicroseconds
/1000);
282 ::DosSleep(1000 * nSecs
);
285 // Consume all events until no more left
294 DosBeep(1000,1000); // 1kHz during 1 sec.
297 wxString
wxGetOsDescription()
299 wxString
strVer(_T("OS/2"));
302 if (::DosQuerySysInfo( QSV_VERSION_MINOR
,
309 ver
.Printf( _T(" ver. %d.%d"),
319 bool wxIsPlatform64Bit()
321 // FIXME: No idea how to test for 64 bit processor
322 // (Probably irrelevant anyhow, though).
326 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab
))
330 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab
))
334 wxOperatingSystemId
wxGetOsVersion(int *verMaj
, int *verMin
)
339 ulrc
= ::DosQuerySysInfo( QSV_VERSION_MINOR
,
348 *verMaj
= ulSysInfo
/ 10;
350 *verMin
= ulSysInfo
% 10;
357 // ---------------------------------------------------------------------------
358 const wxChar
* wxGetHomeDir(
362 wxString
& rStrDir
= *pStr
;
364 // OS/2 has no idea about home,
365 // so use the working directory instead.
366 // However, we might have a valid HOME directory,
367 // as is used on many machines that have unix utilities
368 // on them, so we should use that, if available.
370 // 256 was taken from os2def.h
372 # define MAX_PATH 256
375 const wxChar
*szHome
= wxGetenv((wxChar
*)"HOME");
376 if ( szHome
== NULL
) {
377 // we're homeless, use current directory.
383 return rStrDir
.c_str();
388 const wxMB2WXbuf
wxGetUserHome( const wxString
&rUser
)
389 #else // just for binary compatibility -- there is no 'const' here
390 wxChar
* wxGetUserHome ( const wxString
&rUser
)
394 wxString
sUser1(rUser
);
396 wxChar
*wxBuffer
= new wxChar
[256];
402 if (wxGetUserId( zTmp
403 ,sizeof(zTmp
)/sizeof(char)
406 // Guests belong in the temp dir
407 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
409 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
410 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
411 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
413 return *zHome
? zHome
: (wxChar
*)_T("\\");
415 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
416 sUser1
= wxEmptyString
;
422 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
424 wxStrcpy(wxBuffer
, zHome
);
425 wxUnix2DosFilename(wxBuffer
);
427 wxWCharBuffer
retBuffer (wxBuffer
);
431 wxStrcpy(zHome
, wxBuffer
);
438 return (wxChar
*)wxEmptyString
; // No home known!
441 wxString
wxPMErrorToStr(ERRORID vError
)
446 // Remove the high order byte -- it is useless
448 vError
&= 0x0000ffff;
451 case PMERR_INVALID_HWND
:
452 sError
= wxT("Invalid window handle specified");
455 case PMERR_INVALID_FLAG
:
456 sError
= wxT("Invalid flag bit set");
459 case PMERR_NO_MSG_QUEUE
:
460 sError
= wxT("No message queue available");
463 case PMERR_INVALID_PARM
:
464 sError
= wxT("Parameter contained invalid data");
467 case PMERR_INVALID_PARAMETERS
:
468 sError
= wxT("Parameter value is out of range");
471 case PMERR_PARAMETER_OUT_OF_RANGE
:
472 sError
= wxT("Parameter value is out of range");
475 case PMERR_INVALID_INTEGER_ATOM
:
476 sError
= wxT("Not a valid atom");
479 case PMERR_INVALID_HATOMTBL
:
480 sError
= wxT("Atom table handle is invalid");
483 case PMERR_INVALID_ATOM_NAME
:
484 sError
= wxT("Not a valid atom name");
487 case PMERR_ATOM_NAME_NOT_FOUND
:
488 sError
= wxT("Valid name format, but cannot find name in atom table");
492 sError
= wxT("PMERR_INV_HPS");
496 sError
= wxT("PMERR_PS_BUSY");
499 case PMERR_INV_PRIMITIVE_TYPE
:
500 sError
= wxT("PMERR_INV_PRIMITIVE_TYPE");
503 case PMERR_UNSUPPORTED_ATTR
:
504 sError
= wxT("PMERR_UNSUPPORTED_ATTR");
507 case PMERR_INV_COLOR_ATTR
:
508 sError
= wxT("PMERR_INV_COLOR_ATTR");
511 case PMERR_INV_BACKGROUND_COL_ATTR
:
512 sError
= wxT("PMERR_INV_BACKGROUND_COL_ATTR");
515 case PMERR_INV_MIX_ATTR
:
516 sError
= wxT("PMERR_INV_MIX_ATTR");
519 case PMERR_INV_LINE_WIDTH_ATTR
:
520 sError
= wxT("PMERR_INV_LINE_WIDTH_ATTR");
523 case PMERR_INV_GEOM_LINE_WIDTH_ATTR
:
524 sError
= wxT("PMERR_INV_GEOM_LINE_WIDTH_ATTR");
527 case PMERR_INV_LINE_TYPE_ATTR
:
528 sError
= wxT("PMERR_INV_LINE_TYPE_ATTR");
531 case PMERR_INV_LINE_END_ATTR
:
532 sError
= wxT("PMERR_INV_LINE_END_ATTR");
535 case PMERR_INV_LINE_JOIN_ATTR
:
536 sError
= wxT("PMERR_INV_LINE_JOIN_ATTR");
539 case PMERR_INV_CHAR_SET_ATTR
:
540 sError
= wxT("PMERR_INV_CHAR_SET_ATTR");
543 case PMERR_INV_CHAR_MODE_ATTR
:
544 sError
= wxT("PMERR_INV_CHAR_MODE_ATTR");
547 case PMERR_INV_CHAR_DIRECTION_ATTR
:
548 sError
= wxT("PMERR_INV_CHAR_DIRECTION_ATTR");
551 case PMERR_INV_CHAR_SHEAR_ATTR
:
552 sError
= wxT("PMERR_INV_CHAR_SHEAR_ATTR");
555 case PMERR_INV_CHAR_ANGLE_ATTR
:
556 sError
= wxT("PMERR_INV_CHAR_ANGLE_ATTR");
559 case PMERR_INV_MARKER_SET_ATTR
:
560 sError
= wxT("PMERR_INV_MARKER_SET_ATTR");
563 case PMERR_INV_MARKER_SYMBOL_ATTR
:
564 sError
= wxT("PMERR_INV_MARKER_SYMBOL_ATTR");
567 case PMERR_INV_PATTERN_SET_ATTR
:
568 sError
= wxT("PMERR_INV_PATTERN_SET_ATTR");
571 case PMERR_INV_PATTERN_ATTR
:
572 sError
= wxT("PMERR_INV_PATTERN_ATTR");
575 case PMERR_INV_COORDINATE
:
576 sError
= wxT("PMERR_INV_COORDINATE");
579 case PMERR_UNSUPPORTED_ATTR_VALUE
:
580 sError
= wxT("PMERR_UNSUPPORTED_ATTR_VALUE");
583 case PMERR_INV_PATTERN_SET_FONT
:
584 sError
= wxT("PMERR_INV_PATTERN_SET_FONT");
587 case PMERR_HUGE_FONTS_NOT_SUPPORTED
:
588 sError
= wxT("PMERR_HUGE_FONTS_NOT_SUPPORTED");
592 sError
= wxT("Unknown error");
595 } // end of wxPMErrorToStr
597 // replacement for implementation in unix/utilsunx.cpp,
598 // to be used by all X11 based ports.
599 struct wxEndProcessData
;
601 void wxHandleProcessTermination(wxEndProcessData
*WXUNUSED(proc_data
))
603 // For now, just do nothing. To be filled in as needed.