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