]> git.saurik.com Git - wxWidgets.git/blame - src/os2/utils.cpp
fixed warnings about truncating 64 bit integers
[wxWidgets.git] / src / os2 / utils.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: utils.cpp
3// Purpose: Various utilities
45fcbf3b 4// Author: David Webster
0e320a79 5// Modified by:
45fcbf3b 6// Created: 09/17/99
0e320a79 7// RCS-ID: $Id$
45fcbf3b 8// Copyright: (c) David Webster
65571936 9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
45fcbf3b
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifndef WX_PRECOMP
16 #include "wx/setup.h"
17 #include "wx/utils.h"
18 #include "wx/app.h"
45fcbf3b
DW
19#endif //WX_PRECOMP
20
21#include "wx/os2/private.h"
45fcbf3b 22#include "wx/intl.h"
cd478a95 23#include "wx/apptrait.h"
45fcbf3b 24
0e320a79 25#include <ctype.h>
06298235
SN
26#ifdef __EMX__
27#include <dirent.h>
06298235 28#endif
45fcbf3b
DW
29
30#include "wx/log.h"
31
32#include <io.h>
0e320a79
DW
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
45fcbf3b 37#include <errno.h>
0e320a79
DW
38#include <stdarg.h>
39
d90895ac 40#define PURE_32
468e327a 41
6670f564
WS
42#if defined(__WATCOMC__)
43extern "C"
44{
45 #include <upm.h>
46}
47#elif !defined(__EMX__)
48 #include <upm.h>
49 #include <netcons.h>
50 #include <netbios.h>
06298235 51#endif
45fcbf3b 52
77ffb593 53static const wxChar WX_SECTION[] = _T("wxWidgets");
86de7616 54static const wxChar eHOSTNAME[] = _T("HostName");
45fcbf3b
DW
55
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
60
0e320a79 61// Get full hostname (eg. DoDo.BSn-Germany.crg.de)
10e5b930
DW
62bool wxGetHostName(
63 wxChar* zBuf
64, int nMaxSize
65)
0e320a79 66{
d90895ac 67#if wxUSE_NET_API
10e5b930
DW
68 char zServer[256];
69 char zComputer[256];
909b4f08 70 unsigned long ulLevel = 0;
9dea36ef
DW
71 unsigned char* zBuffer = NULL;
72 unsigned long ulBuffer = 256;
73 unsigned long* pulTotalAvail = NULL;
10e5b930 74
dde11e60
DW
75 NetBios32GetInfo( (const unsigned char*)zServer
76 ,(const unsigned char*)zComputer
909b4f08 77 ,ulLevel
dde11e60 78 ,zBuffer
909b4f08
DW
79 ,ulBuffer
80 ,pulTotalAvail
dde11e60 81 );
10e5b930 82 strcpy(zBuf, zServer);
45fcbf3b 83#else
10e5b930
DW
84 wxChar* zSysname;
85 const wxChar* zDefaultHost = _T("noname");
45fcbf3b 86
10e5b930
DW
87 if ((zSysname = wxGetenv(_T("SYSTEM_NAME"))) == NULL)
88 {
9923c37d
DW
89 ::PrfQueryProfileString( HINI_PROFILE
90 ,(PSZ)WX_SECTION
91 ,(PSZ)eHOSTNAME
92 ,(PSZ)zDefaultHost
93 ,(void*)zBuf
94 ,(ULONG)nMaxSize - 1
95 );
10e5b930
DW
96 }
97 else
98 wxStrncpy(zBuf, zSysname, nMaxSize - 1);
99 zBuf[nMaxSize] = _T('\0');
45fcbf3b 100#endif
9d8aca48 101 return *zBuf ? true : false;
0e320a79
DW
102}
103
104// Get user ID e.g. jacs
6670f564 105bool wxGetUserId(wxChar* zBuf, int nType)
0e320a79 106{
6670f564 107#if defined(__VISAGECPP__) || defined(__WATCOMC__)
2bd5bbc9 108 // UPM procs return 0 on success
6670f564 109 long lrc = U32ELOCU((PUCHAR)zBuf, (PULONG)&nType);
9d8aca48 110 if (lrc == 0) return true;
06298235 111#endif
9d8aca48 112 return false;
0e320a79
DW
113}
114
6670f564 115bool wxGetUserName( wxChar* zBuf, int nMaxSize )
0e320a79 116{
45fcbf3b 117#ifdef USE_NET_API
6670f564 118 wxGetUserId( zBuf, nMaxSize );
45fcbf3b 119#else
10e5b930 120 wxStrncpy(zBuf, _T("Unknown User"), nMaxSize);
45fcbf3b 121#endif
9d8aca48 122 return true;
0e320a79
DW
123}
124
6670f564
WS
125int wxKill(long lPid,
126 wxSignal WXUNUSED(eSig),
127 wxKillError* WXUNUSED(peError),
128 int WXUNUSED(flags))
0e320a79 129{
10e5b930 130 return((int)::DosKillProcess(0, (PID)lPid));
0e320a79
DW
131}
132
133//
134// Execute a program in an Interactive Shell
135//
10e5b930
DW
136bool wxShell(
137 const wxString& rCommand
138)
0e320a79 139{
c5fb56c0
DW
140 wxChar* zShell = _T("CMD.EXE");
141 wxString sInputs;
c5fb56c0
DW
142 STARTDATA SData = {0};
143 PSZ PgmTitle = "Command Shell";
144 APIRET rc;
145 PID vPid = 0;
146 ULONG ulSessID = 0;
147 UCHAR achObjBuf[256] = {0}; //error data if DosStart fails
148 RESULTCODES vResult;
149
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;
0fba44b4 155 SData.PgmName = (char*)zShell;
c5fb56c0 156
0fba44b4 157 sInputs = _T("/C ") + rCommand;
9ac6ff7b 158 SData.PgmInputs = (BYTE*)sInputs.c_str();
c5fb56c0
DW
159 SData.TermQ = 0;
160 SData.Environment = 0;
161 SData.InheritOpt = SSF_INHERTOPT_SHELL;
162 SData.SessionType = SSF_TYPE_WINDOWABLEVIO;
163 SData.IconFile = 0;
164 SData.PgmHandle = 0;
165 SData.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_MAXIMIZE;
166 SData.InitXPos = 30;
167 SData.InitYPos = 40;
168 SData.InitXSize = 200;
169 SData.InitYSize = 140;
170 SData.Reserved = 0;
171 SData.ObjectBuffer = (char*)achObjBuf;
172 SData.ObjectBuffLen = (ULONG)sizeof(achObjBuf);
173
174 rc = ::DosStartSession(&SData, &ulSessID, &vPid);
9ac6ff7b 175 if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND
c5fb56c0
DW
176 {
177 PTIB ptib;
178 PPIB ppib;
10e5b930 179
c5fb56c0 180 ::DosGetInfoBlocks(&ptib, &ppib);
10e5b930 181
c5fb56c0
DW
182 ::DosWaitChild( DCWA_PROCESS
183 ,DCWW_WAIT
184 ,&vResult
185 ,&ppib->pib_ulpid
186 ,vPid
187 );
188 }
189 return (rc != 0);
0e320a79
DW
190}
191
a23692f0 192// Shutdown or reboot the PC
6670f564 193bool wxShutdown(wxShutdownFlags WXUNUSED(wFlags))
f6ba47d9
VZ
194{
195 // TODO
9d8aca48 196 return false;
f6ba47d9
VZ
197}
198
8ea92b4d
WS
199wxPowerType wxGetPowerType()
200{
201 // TODO
202 return wxPOWER_UNKNOWN;
203}
204
205wxBatteryState wxGetBatteryState()
206{
207 // TODO
208 return wxBATTERY_UNKNOWN_STATE;
209}
210
0e320a79 211// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
9d8aca48 212wxMemorySize wxGetFreeMemory()
0e320a79 213{
5a23a495
WS
214 void* pMemptr = NULL;
215 LONG lSize;
216 ULONG lMemFlags;
217 APIRET rc;
78d50441
DW
218
219 lMemFlags = PAG_FREE;
0d598bae 220 rc = ::DosQueryMem(pMemptr, (PULONG)&lSize, &lMemFlags);
78d50441 221 if (rc != 0)
9d8aca48
WS
222 lSize = -1L;
223 return (wxMemorySize)lSize;
45fcbf3b
DW
224}
225
264f00e7
SN
226// Get Process ID
227unsigned long wxGetProcessId()
228{
229 return (unsigned long)getpid();
230}
231
8269a903
SN
232// ----------------------------------------------------------------------------
233// env vars
234// ----------------------------------------------------------------------------
235
236bool wxGetEnv(const wxString& var, wxString *value)
237{
238 // wxGetenv is defined as getenv()
239 wxChar *p = wxGetenv(var);
240 if ( !p )
9d8aca48 241 return false;
8269a903
SN
242
243 if ( value )
244 {
245 *value = p;
246 }
247
9d8aca48 248 return true;
8269a903
SN
249}
250
251bool wxSetEnv(const wxString& variable, const wxChar *value)
252{
253#if defined(HAVE_SETENV)
254 return setenv(variable.mb_str(), value ? wxString(value).mb_str().data()
255 : NULL, 1 /* overwrite */) == 0;
256#elif defined(HAVE_PUTENV)
257 wxString s = variable;
258 if ( value )
259 s << _T('=') << value;
260
261 // transform to ANSI
262 const char *p = s.mb_str();
263
264 // the string will be free()d by libc
265 char *buf = (char *)malloc(strlen(p) + 1);
266 strcpy(buf, p);
267
268 return putenv(buf) == 0;
269#else // no way to set an env var
6670f564
WS
270 wxUnusedVar(variable);
271 wxUnusedVar(value);
9d8aca48 272 return false;
8269a903
SN
273#endif
274}
275
d3a919bd 276void wxMilliSleep(
10e5b930
DW
277 unsigned long ulMilliseconds
278)
45fcbf3b 279{
f53a0d32 280 ::DosSleep(ulMilliseconds);
0e320a79
DW
281}
282
d3a919bd
SN
283void wxMicroSleep(
284 unsigned long ulMicroseconds
285)
286{
287 ::DosSleep(ulMicroseconds/1000);
288}
289
10e5b930
DW
290void wxSleep(
291 int nSecs
292)
0e320a79 293{
10e5b930 294 ::DosSleep(1000 * nSecs);
0e320a79
DW
295}
296
297// Consume all events until no more left
298void wxFlushEvents()
299{
45fcbf3b 300// wxYield();
0e320a79
DW
301}
302
73deed44
VZ
303#if WXWIN_COMPATIBILITY_2_2
304
45fcbf3b 305// Output a debug mess., in a system dependent fashion.
10e5b930
DW
306void wxDebugMsg(
307 const wxChar* zFmt ...
308)
309{
310 va_list vAp;
311 static wxChar zBuffer[512];
312
313 if (!wxTheApp->GetWantDebugOutput())
314 return ;
315 va_start(vAp, zFmt);
316 sprintf(zBuffer, zFmt, vAp) ;
317 va_end(vAp);
0e320a79
DW
318}
319
320// Non-fatal error: pop up message box and (possibly) continue
10e5b930
DW
321void wxError(
322 const wxString& rMsg
323, const wxString& rTitle
324)
325{
bed67b7f 326 wxChar *wxBuffer = new wxChar[256];
10e5b930
DW
327 wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST rMsg);
328 if (::WinMessageBox( HWND_DESKTOP
329 ,NULL
330 ,(PSZ)wxBuffer
331 ,(PSZ)WXSTRINGCAST rTitle
332 ,0
333 ,MB_ICONEXCLAMATION | MB_YESNO
334 ) == MBID_YES)
13a4ea8d 335 delete[] wxBuffer;
0e320a79
DW
336 wxExit();
337}
338
339// Fatal error: pop up message box and abort
10e5b930
DW
340void wxFatalError(
341 const wxString& rMsg
342, const wxString& rTitle
343)
344{
345 unsigned long ulRc;
346
347 ulRc = ::WinMessageBox( HWND_DESKTOP
348 ,NULL
349 ,WXSTRINGCAST rMsg
350 ,WXSTRINGCAST rTitle
351 ,0
352 ,MB_NOICON | MB_OK
353 );
354 DosExit(EXIT_PROCESS, ulRc);
0e320a79
DW
355}
356
73deed44
VZ
357#endif // WXWIN_COMPATIBILITY_2_2
358
0e320a79
DW
359// Emit a beeeeeep
360void wxBell()
361{
45fcbf3b 362 DosBeep(1000,1000); // 1kHz during 1 sec.
0e320a79 363}
621ccd8a 364
5a23a495
WS
365wxString wxGetOsDescription()
366{
367 wxString strVer(_T("OS/2"));
368 ULONG ulSysInfo[QSV_MAX] = {0};
369
370 if (::DosQuerySysInfo( 1L,
371 QSV_MAX,
372 (PVOID)ulSysInfo,
373 sizeof(ULONG) * QSV_MAX
374 ) == 0L )
375 {
376 wxString ver;
377 ver.Printf( _T(" ver. %d.%d rev. %c"),
378 int(ulSysInfo[QSV_VERSION_MAJOR] / 10),
379 int(ulSysInfo[QSV_VERSION_MINOR]),
380 char(ulSysInfo[QSV_VERSION_REVISION])
381 );
382 strVer += ver;
383 }
384
385 return strVer;
386}
8329e6c7 387
6670f564 388void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab))
8329e6c7
SN
389{
390}
391
6670f564 392void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab))
8329e6c7
SN
393{
394}
395
621ccd8a 396wxToolkitInfo & wxConsoleAppTraits::GetToolkitInfo()
10e5b930 397{
9d8aca48
WS
398 static wxToolkitInfo vInfo;
399 ULONG ulSysInfo[QSV_MAX] = {0};
400 APIRET ulrc;
5d4b632b 401
621ccd8a 402 vInfo.name = _T("wxBase");
5d4b632b
DW
403 ulrc = ::DosQuerySysInfo( 1L
404 ,QSV_MAX
405 ,(PVOID)ulSysInfo
406 ,sizeof(ULONG) * QSV_MAX
407 );
408 if (ulrc == 0L)
10e5b930 409 {
621ccd8a
SN
410 vInfo.versionMajor = ulSysInfo[QSV_VERSION_MAJOR] / 10;
411 vInfo.versionMinor = ulSysInfo[QSV_VERSION_MINOR];
10e5b930 412 }
621ccd8a
SN
413 vInfo.os = wxOS2_PM;
414 return vInfo;
0e320a79 415}
621ccd8a 416
45fcbf3b 417// ---------------------------------------------------------------------------
1be7f92a
DW
418const wxChar* wxGetHomeDir(
419 wxString* pStr
420)
45fcbf3b 421{
1be7f92a 422 wxString& rStrDir = *pStr;
0e320a79 423
1be7f92a 424 // OS/2 has no idea about home,
b08c90ca
SN
425 // so use the working directory instead.
426 // However, we might have a valid HOME directory,
427 // as is used on many machines that have unix utilities
428 // on them, so we should use that, if available.
45fcbf3b 429
1be7f92a 430 // 256 was taken from os2def.h
45fcbf3b
DW
431#ifndef MAX_PATH
432# define MAX_PATH 256
433#endif
434
0fba44b4 435 const wxChar *szHome = wxGetenv((wxChar*)"HOME");
b08c90ca
SN
436 if ( szHome == NULL ) {
437 // we're homeless, use current directory.
438 rStrDir = wxT(".");
439 }
440 else
441 rStrDir = szHome;
45fcbf3b 442
1be7f92a 443 return rStrDir.c_str();
45fcbf3b
DW
444}
445
10e5b930 446// Hack for OS/2
2173b18f
SN
447#if wxUSE_UNICODE
448const wxMB2WXbuf wxGetUserHome( const wxString &rUser )
449#else // just for binary compatibility -- there is no 'const' here
450wxChar* wxGetUserHome ( const wxString &rUser )
451#endif
1be7f92a 452{
5a23a495
WS
453 wxChar* zHome;
454 wxString sUser1(rUser);
1be7f92a 455
0fba44b4 456 wxChar *wxBuffer = new wxChar[256];
06298235 457#ifndef __EMX__
27b2dd53 458 if (!sUser1.empty())
45fcbf3b 459 {
1be7f92a
DW
460 wxChar zTmp[64];
461
462 if (wxGetUserId( zTmp
463 ,sizeof(zTmp)/sizeof(char)
464 ))
465 {
466 // Guests belong in the temp dir
467 if (wxStricmp(zTmp, _T("annonymous")) == 0)
468 {
469 if ((zHome = wxGetenv(_T("TMP"))) != NULL ||
470 (zHome = wxGetenv(_T("TMPDIR"))) != NULL ||
471 (zHome = wxGetenv(_T("TEMP"))) != NULL)
13a4ea8d 472 delete[] wxBuffer;
1be7f92a
DW
473 return *zHome ? zHome : (wxChar*)_T("\\");
474 }
475 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
5a23a495 476 sUser1 = wxEmptyString;
1be7f92a 477 }
45fcbf3b 478 }
06298235 479#endif
27b2dd53 480 if (sUser1.empty())
13a4ea8d 481 {
1be7f92a
DW
482 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
483 {
484 wxStrcpy(wxBuffer, zHome);
2b5f62a0 485 wxUnix2DosFilename(wxBuffer);
2173b18f 486#if wxUSE_UNICODE
6670f564 487 wxWCharBuffer retBuffer (wxBuffer);
2173b18f
SN
488 delete[] wxBuffer;
489 return retBuffer;
6670f564 490#else
13a4ea8d
DW
491 wxStrcpy(zHome, wxBuffer);
492 delete[] wxBuffer;
493 return zHome;
2173b18f 494#endif
1be7f92a 495 }
13a4ea8d
DW
496 }
497 delete[] wxBuffer;
0fba44b4 498 return (wxChar*)wxEmptyString; // No home known!
0e320a79
DW
499}
500
6670f564 501wxString WXDLLEXPORT wxPMErrorToStr(ERRORID vError)
914589c2
DW
502{
503 wxString sError;
504
505 //
506 // Remove the high order byte -- it is useless
507 //
508 vError &= 0x0000ffff;
509 switch(vError)
510 {
511 case PMERR_INVALID_HWND:
512 sError = wxT("Invalid window handle specified");
513 break;
514
515 case PMERR_INVALID_FLAG:
516 sError = wxT("Invalid flag bit set");
517 break;
518
519 case PMERR_NO_MSG_QUEUE:
520 sError = wxT("No message queue available");
521 break;
522
523 case PMERR_INVALID_PARM:
524 sError = wxT("Parameter contained invalid data");
525 break;
526
527 case PMERR_INVALID_PARAMETERS:
528 sError = wxT("Parameter value is out of range");
529 break;
530
531 case PMERR_PARAMETER_OUT_OF_RANGE:
532 sError = wxT("Parameter value is out of range");
533 break;
534
535 case PMERR_INVALID_INTEGER_ATOM:
536 sError = wxT("Not a valid atom");
537 break;
538
539 case PMERR_INVALID_HATOMTBL:
540 sError = wxT("Atom table handle is invalid");
541 break;
542
543 case PMERR_INVALID_ATOM_NAME:
544 sError = wxT("Not a valid atom name");
545 break;
546
547 case PMERR_ATOM_NAME_NOT_FOUND:
548 sError = wxT("Valid name format, but cannot find name in atom table");
549 break;
550
551 default:
552 sError = wxT("Unknown error");
553 }
554 return(sError);
555} // end of wxPMErrorToStr
621ccd8a
SN
556
557// replacement for implementation in unix/utilsunx.cpp,
558// to be used by all X11 based ports.
559struct wxEndProcessData;
560
6670f564 561void wxHandleProcessTermination(wxEndProcessData *WXUNUSED(proc_data))
621ccd8a
SN
562{
563 // For now, just do nothing. To be filled in as needed.
564}