]> git.saurik.com Git - wxWidgets.git/blob - src/os2/utils.cpp
Dtor for wxHtmlCell
[wxWidgets.git] / src / os2 / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/utils.cpp
3 // Purpose: Various utilities
4 // Author: David Webster
5 // Modified by:
6 // Created: 09/17/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/utils.h"
17 #include "wx/app.h"
18 #endif //WX_PRECOMP
19
20 #include "wx/os2/private.h"
21 #include "wx/intl.h"
22 #include "wx/apptrait.h"
23
24 #include <ctype.h>
25 #ifdef __EMX__
26 #include <dirent.h>
27 #endif
28
29 #include "wx/log.h"
30
31 #include <io.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <stdarg.h>
38
39 #define PURE_32
40
41 #if defined(__WATCOMC__)
42 extern "C"
43 {
44 #include <upm.h>
45 }
46 #elif !defined(__EMX__)
47 #include <upm.h>
48 #include <netcons.h>
49 #include <netbios.h>
50 #endif
51
52 static const wxChar WX_SECTION[] = _T("wxWidgets");
53 static const wxChar eHOSTNAME[] = _T("HostName");
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
60 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
61 bool wxGetHostName( wxChar* zBuf, int nMaxSize )
62 {
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;
72
73 NetBios32GetInfo( (const unsigned char*)zServer
74 ,(const unsigned char*)zComputer
75 ,ulLevel
76 ,zBuffer
77 ,ulBuffer
78 ,pulTotalAvail
79 );
80 strcpy(zBuf, zServer);
81 #else
82 wxChar* zSysname;
83 const wxChar* zDefaultHost = _T("noname");
84
85 if ((zSysname = wxGetenv(_T("SYSTEM_NAME"))) == NULL)
86 {
87 ::PrfQueryProfileString( HINI_PROFILE
88 ,(PSZ)WX_SECTION
89 ,(PSZ)eHOSTNAME
90 ,(PSZ)zDefaultHost
91 ,(void*)zBuf
92 ,(ULONG)nMaxSize - 1
93 );
94 }
95 else
96 {
97 wxStrncpy(zBuf, zSysname, nMaxSize - 1);
98 }
99
100 zBuf[nMaxSize] = _T('\0');
101 #endif
102
103 return *zBuf ? true : false;
104 }
105
106 // Get user ID e.g. jacs
107 bool wxGetUserId(wxChar* zBuf, int nType)
108 {
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;
113 #endif
114 return false;
115 }
116
117 bool wxGetUserName( wxChar* zBuf, int nMaxSize )
118 {
119 #ifdef USE_NET_API
120 wxGetUserId( zBuf, nMaxSize );
121 #else
122 wxStrncpy(zBuf, _T("Unknown User"), nMaxSize);
123 #endif
124 return true;
125 }
126
127 int wxKill(long lPid,
128 wxSignal WXUNUSED(eSig),
129 wxKillError* WXUNUSED(peError),
130 int WXUNUSED(flags))
131 {
132 return((int)::DosKillProcess(0, (PID)lPid));
133 }
134
135 //
136 // Execute a program in an Interactive Shell
137 //
138 bool wxShell(
139 const wxString& rCommand
140 )
141 {
142 wxChar* zShell = _T("CMD.EXE");
143 wxString sInputs;
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;
157 SData.PgmName = (char*)zShell;
158
159 sInputs = _T("/C ") + rCommand;
160 SData.PgmInputs = (BYTE*)sInputs.c_str();
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);
177 if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND
178 {
179 PTIB ptib;
180 PPIB ppib;
181
182 ::DosGetInfoBlocks(&ptib, &ppib);
183
184 ::DosWaitChild( DCWA_PROCESS
185 ,DCWW_WAIT
186 ,&vResult
187 ,&ppib->pib_ulpid
188 ,vPid
189 );
190 }
191 return (rc != 0);
192 }
193
194 // Shutdown or reboot the PC
195 bool wxShutdown(wxShutdownFlags WXUNUSED(wFlags))
196 {
197 // TODO
198 return false;
199 }
200
201 wxPowerType wxGetPowerType()
202 {
203 // TODO
204 return wxPOWER_UNKNOWN;
205 }
206
207 wxBatteryState wxGetBatteryState()
208 {
209 // TODO
210 return wxBATTERY_UNKNOWN_STATE;
211 }
212
213 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
214 wxMemorySize wxGetFreeMemory()
215 {
216 void* pMemptr = NULL;
217 LONG lSize;
218 ULONG lMemFlags;
219 APIRET rc;
220
221 lMemFlags = PAG_FREE;
222 rc = ::DosQueryMem(pMemptr, (PULONG)&lSize, &lMemFlags);
223 if (rc != 0)
224 lSize = -1L;
225 return (wxMemorySize)lSize;
226 }
227
228 // Get Process ID
229 unsigned long wxGetProcessId()
230 {
231 return (unsigned long)getpid();
232 }
233
234 // ----------------------------------------------------------------------------
235 // env vars
236 // ----------------------------------------------------------------------------
237
238 bool wxGetEnv(const wxString& var, wxString *value)
239 {
240 // wxGetenv is defined as getenv()
241 wxChar *p = wxGetenv(var);
242 if ( !p )
243 return false;
244
245 if ( value )
246 {
247 *value = p;
248 }
249
250 return true;
251 }
252
253 bool 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
272 wxUnusedVar(variable);
273 wxUnusedVar(value);
274 return false;
275 #endif
276 }
277
278 void wxMilliSleep(
279 unsigned long ulMilliseconds
280 )
281 {
282 ::DosSleep(ulMilliseconds);
283 }
284
285 void wxMicroSleep(
286 unsigned long ulMicroseconds
287 )
288 {
289 ::DosSleep(ulMicroseconds/1000);
290 }
291
292 void wxSleep(
293 int nSecs
294 )
295 {
296 ::DosSleep(1000 * nSecs);
297 }
298
299 // Consume all events until no more left
300 void wxFlushEvents()
301 {
302 // wxYield();
303 }
304
305 // Emit a beeeeeep
306 void wxBell()
307 {
308 DosBeep(1000,1000); // 1kHz during 1 sec.
309 }
310
311 wxString 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 }
333
334 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab))
335 {
336 }
337
338 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab))
339 {
340 }
341
342 wxToolkitInfo & wxConsoleAppTraits::GetToolkitInfo()
343 {
344 static wxToolkitInfo vInfo;
345 ULONG ulSysInfo[QSV_MAX] = {0};
346 APIRET ulrc;
347
348 vInfo.name = _T("wxBase");
349 ulrc = ::DosQuerySysInfo( 1L
350 ,QSV_MAX
351 ,(PVOID)ulSysInfo
352 ,sizeof(ULONG) * QSV_MAX
353 );
354 if (ulrc == 0L)
355 {
356 vInfo.versionMajor = ulSysInfo[QSV_VERSION_MAJOR] / 10;
357 vInfo.versionMinor = ulSysInfo[QSV_VERSION_MINOR];
358 }
359 vInfo.os = wxOS2_PM;
360 return vInfo;
361 }
362
363 // ---------------------------------------------------------------------------
364 const wxChar* wxGetHomeDir(
365 wxString* pStr
366 )
367 {
368 wxString& rStrDir = *pStr;
369
370 // OS/2 has no idea about home,
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.
375
376 // 256 was taken from os2def.h
377 #ifndef MAX_PATH
378 # define MAX_PATH 256
379 #endif
380
381 const wxChar *szHome = wxGetenv((wxChar*)"HOME");
382 if ( szHome == NULL ) {
383 // we're homeless, use current directory.
384 rStrDir = wxT(".");
385 }
386 else
387 rStrDir = szHome;
388
389 return rStrDir.c_str();
390 }
391
392 // Hack for OS/2
393 #if wxUSE_UNICODE
394 const wxMB2WXbuf wxGetUserHome( const wxString &rUser )
395 #else // just for binary compatibility -- there is no 'const' here
396 wxChar* wxGetUserHome ( const wxString &rUser )
397 #endif
398 {
399 wxChar* zHome;
400 wxString sUser1(rUser);
401
402 wxChar *wxBuffer = new wxChar[256];
403 #ifndef __EMX__
404 if (!sUser1.empty())
405 {
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)
418 delete[] wxBuffer;
419 return *zHome ? zHome : (wxChar*)_T("\\");
420 }
421 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
422 sUser1 = wxEmptyString;
423 }
424 }
425 #endif
426 if (sUser1.empty())
427 {
428 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
429 {
430 wxStrcpy(wxBuffer, zHome);
431 wxUnix2DosFilename(wxBuffer);
432 #if wxUSE_UNICODE
433 wxWCharBuffer retBuffer (wxBuffer);
434 delete[] wxBuffer;
435 return retBuffer;
436 #else
437 wxStrcpy(zHome, wxBuffer);
438 delete[] wxBuffer;
439 return zHome;
440 #endif
441 }
442 }
443 delete[] wxBuffer;
444 return (wxChar*)wxEmptyString; // No home known!
445 }
446
447 wxString wxPMErrorToStr(ERRORID vError)
448 {
449 wxString sError;
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 }
500 return sError;
501 } // end of wxPMErrorToStr
502
503 // replacement for implementation in unix/utilsunx.cpp,
504 // to be used by all X11 based ports.
505 struct wxEndProcessData;
506
507 void wxHandleProcessTermination(wxEndProcessData *WXUNUSED(proc_data))
508 {
509 // For now, just do nothing. To be filled in as needed.
510 }