]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/os2/utils.cpp
switching to registry based type info
[wxWidgets.git] / src / os2 / utils.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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/setup.h"
17 #include "wx/utils.h"
18 #include "wx/app.h"
19#endif //WX_PRECOMP
20
21#include "wx/os2/private.h"
22#include "wx/timer.h"
23#include "wx/intl.h"
24#include "wx/apptrait.h"
25
26#include <ctype.h>
27#ifdef __EMX__
28#include <dirent.h>
29#endif
30
31#include "wx/log.h"
32
33#include <io.h>
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <errno.h>
39#include <stdarg.h>
40
41#define PURE_32
42
43#ifndef __EMX__
44#include <upm.h>
45#include <netcons.h>
46#include <netbios.h>
47#endif
48
49static const wxChar WX_SECTION[] = _T("wxWindows");
50static const wxChar eHOSTNAME[] = _T("HostName");
51static const wxChar eUSERID[] = _T("UserId");
52static const wxChar eUSERNAME[] = _T("UserName");
53
54// For the following functions we SHOULD fill in support
55// for Windows-NT (which I don't know) as I assume it begin
56// a POSIX Unix (so claims MS) that it has some special
57// functions beyond those provided by WinSock
58
59// Get full hostname (eg. DoDo.BSn-Germany.crg.de)
60bool wxGetHostName(
61 wxChar* zBuf
62, int nMaxSize
63)
64{
65#if 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 ULONG n = ::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 wxStrncpy(zBuf, zSysname, nMaxSize - 1);
97 zBuf[nMaxSize] = _T('\0');
98#endif
99 return *zBuf ? TRUE : FALSE;
100}
101
102// Get user ID e.g. jacs
103bool wxGetUserId(
104 wxChar* zBuf
105, int nType
106)
107{
108#if defined(__VISAGECPP__)
109 long lrc;
110 // UPM procs return 0 on success
111 lrc = U32ELOCU((unsigned char*)zBuf, (unsigned long *)&nType);
112 if (lrc == 0) return TRUE;
113#endif
114 return FALSE;
115}
116
117bool wxGetUserName(
118 wxChar* zBuf
119, int nMaxSize
120)
121{
122#ifdef USE_NET_API
123 wxGetUserId( zBuf
124 ,nMaxSize
125 );
126#else
127 wxStrncpy(zBuf, _T("Unknown User"), nMaxSize);
128#endif
129 return TRUE;
130}
131
132int wxKill(
133 long lPid
134, wxSignal eSig
135, wxKillError* peError
136)
137{
138 return((int)::DosKillProcess(0, (PID)lPid));
139}
140
141//
142// Execute a program in an Interactive Shell
143//
144bool wxShell(
145 const wxString& rCommand
146)
147{
148 wxChar* zShell = _T("CMD.EXE");
149 wxString sInputs;
150 wxChar zTmp[255];
151 STARTDATA SData = {0};
152 PSZ PgmTitle = "Command Shell";
153 APIRET rc;
154 PID vPid = 0;
155 ULONG ulSessID = 0;
156 UCHAR achObjBuf[256] = {0}; //error data if DosStart fails
157 RESULTCODES vResult;
158
159 SData.Length = sizeof(STARTDATA);
160 SData.Related = SSF_RELATED_INDEPENDENT;
161 SData.FgBg = SSF_FGBG_FORE;
162 SData.TraceOpt = SSF_TRACEOPT_NONE;
163 SData.PgmTitle = PgmTitle;
164 SData.PgmName = zShell;
165
166 sInputs = "/C " + rCommand;
167 SData.PgmInputs = (BYTE*)sInputs.c_str();
168 SData.TermQ = 0;
169 SData.Environment = 0;
170 SData.InheritOpt = SSF_INHERTOPT_SHELL;
171 SData.SessionType = SSF_TYPE_WINDOWABLEVIO;
172 SData.IconFile = 0;
173 SData.PgmHandle = 0;
174 SData.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_MAXIMIZE;
175 SData.InitXPos = 30;
176 SData.InitYPos = 40;
177 SData.InitXSize = 200;
178 SData.InitYSize = 140;
179 SData.Reserved = 0;
180 SData.ObjectBuffer = (char*)achObjBuf;
181 SData.ObjectBuffLen = (ULONG)sizeof(achObjBuf);
182
183 rc = ::DosStartSession(&SData, &ulSessID, &vPid);
184 if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND
185 {
186 PTIB ptib;
187 PPIB ppib;
188
189 ::DosGetInfoBlocks(&ptib, &ppib);
190
191 ::DosWaitChild( DCWA_PROCESS
192 ,DCWW_WAIT
193 ,&vResult
194 ,&ppib->pib_ulpid
195 ,vPid
196 );
197 }
198 return (rc != 0);
199}
200
201// Shutdown or reboot the PC
202bool wxShutdown(wxShutdownFlags wFlags)
203{
204 // TODO
205 return FALSE;
206}
207
208// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
209long wxGetFreeMemory()
210{
211 void* pMemptr = NULL;
212 ULONG lSize;
213 ULONG lMemFlags;
214 APIRET rc;
215
216 lMemFlags = PAG_FREE;
217 rc = ::DosQueryMem(pMemptr, &lSize, &lMemFlags);
218 if (rc != 0)
219 return -1L;
220 return (long)lSize;
221}
222
223// ----------------------------------------------------------------------------
224// env vars
225// ----------------------------------------------------------------------------
226
227bool wxGetEnv(const wxString& var, wxString *value)
228{
229 // wxGetenv is defined as getenv()
230 wxChar *p = wxGetenv(var);
231 if ( !p )
232 return FALSE;
233
234 if ( value )
235 {
236 *value = p;
237 }
238
239 return TRUE;
240}
241
242bool wxSetEnv(const wxString& variable, const wxChar *value)
243{
244#if defined(HAVE_SETENV)
245 return setenv(variable.mb_str(), value ? wxString(value).mb_str().data()
246 : NULL, 1 /* overwrite */) == 0;
247#elif defined(HAVE_PUTENV)
248 wxString s = variable;
249 if ( value )
250 s << _T('=') << value;
251
252 // transform to ANSI
253 const char *p = s.mb_str();
254
255 // the string will be free()d by libc
256 char *buf = (char *)malloc(strlen(p) + 1);
257 strcpy(buf, p);
258
259 return putenv(buf) == 0;
260#else // no way to set an env var
261 return FALSE;
262#endif
263}
264
265void wxUsleep(
266 unsigned long ulMilliseconds
267)
268{
269 ::DosSleep(ulMilliseconds);
270}
271
272void wxSleep(
273 int nSecs
274)
275{
276 ::DosSleep(1000 * nSecs);
277}
278
279// Consume all events until no more left
280void wxFlushEvents()
281{
282// wxYield();
283}
284
285#if WXWIN_COMPATIBILITY_2_2
286
287// Output a debug mess., in a system dependent fashion.
288void wxDebugMsg(
289 const wxChar* zFmt ...
290)
291{
292 va_list vAp;
293 static wxChar zBuffer[512];
294
295 if (!wxTheApp->GetWantDebugOutput())
296 return ;
297 va_start(vAp, zFmt);
298 sprintf(zBuffer, zFmt, vAp) ;
299 va_end(vAp);
300}
301
302// Non-fatal error: pop up message box and (possibly) continue
303void wxError(
304 const wxString& rMsg
305, const wxString& rTitle
306)
307{
308 wxBuffer = new wxChar[256];
309 wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST rMsg);
310 if (::WinMessageBox( HWND_DESKTOP
311 ,NULL
312 ,(PSZ)wxBuffer
313 ,(PSZ)WXSTRINGCAST rTitle
314 ,0
315 ,MB_ICONEXCLAMATION | MB_YESNO
316 ) == MBID_YES)
317 delete[] wxBuffer;
318 wxExit();
319}
320
321// Fatal error: pop up message box and abort
322void wxFatalError(
323 const wxString& rMsg
324, const wxString& rTitle
325)
326{
327 unsigned long ulRc;
328
329 ulRc = ::WinMessageBox( HWND_DESKTOP
330 ,NULL
331 ,WXSTRINGCAST rMsg
332 ,WXSTRINGCAST rTitle
333 ,0
334 ,MB_NOICON | MB_OK
335 );
336 DosExit(EXIT_PROCESS, ulRc);
337}
338
339#endif // WXWIN_COMPATIBILITY_2_2
340
341// Emit a beeeeeep
342void wxBell()
343{
344 DosBeep(1000,1000); // 1kHz during 1 sec.
345}
346#if 0
347int wxGUIAppTraits::GetOSVersion(
348 int* pMajorVsn
349, int* pMinorVsn
350)
351{
352 ULONG ulSysInfo[QSV_MAX] = {0};
353 APIRET ulrc;
354
355 ulrc = ::DosQuerySysInfo( 1L
356 ,QSV_MAX
357 ,(PVOID)ulSysInfo
358 ,sizeof(ULONG) * QSV_MAX
359 );
360 if (ulrc == 0L)
361 {
362 *pMajorVsn = ulSysInfo[QSV_VERSION_MAJOR];
363 *pMajorVsn = *pMajorVsn/10;
364 *pMinorVsn = ulSysInfo[QSV_VERSION_MINOR];
365 return wxWINDOWS_OS2;
366 }
367 return wxWINDOWS; // error if we get here, return generic value
368}
369#endif
370// ---------------------------------------------------------------------------
371const wxChar* wxGetHomeDir(
372 wxString* pStr
373)
374{
375 wxString& rStrDir = *pStr;
376
377 // OS/2 has no idea about home,
378 // so use the working directory instead?
379
380 // 256 was taken from os2def.h
381#ifndef MAX_PATH
382# define MAX_PATH 256
383#endif
384
385 char zDirName[256];
386 ULONG ulDirLen;
387
388 ::DosQueryCurrentDir(0, zDirName, &ulDirLen);
389 rStrDir = zDirName;
390 return rStrDir.c_str();
391}
392
393// Hack for OS/2
394wxChar* wxGetUserHome (
395 const wxString& rUser
396)
397{
398 wxChar* zHome;
399 wxString sUser1(rUser);
400
401 wxBuffer = new wxChar[256];
402#ifndef __EMX__
403 if (sUser1 != _T(""))
404 {
405 wxChar zTmp[64];
406
407 if (wxGetUserId( zTmp
408 ,sizeof(zTmp)/sizeof(char)
409 ))
410 {
411 // Guests belong in the temp dir
412 if (wxStricmp(zTmp, _T("annonymous")) == 0)
413 {
414 if ((zHome = wxGetenv(_T("TMP"))) != NULL ||
415 (zHome = wxGetenv(_T("TMPDIR"))) != NULL ||
416 (zHome = wxGetenv(_T("TEMP"))) != NULL)
417 delete[] wxBuffer;
418 return *zHome ? zHome : (wxChar*)_T("\\");
419 }
420 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
421 sUser1 = _T("");
422 }
423 }
424#endif
425 if (sUser1 == _T(""))
426 {
427 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
428 {
429 wxStrcpy(wxBuffer, zHome);
430 wxUnix2DosFilename(wxBuffer);
431 wxStrcpy(zHome, wxBuffer);
432 delete[] wxBuffer;
433 return zHome;
434 }
435 }
436 delete[] wxBuffer;
437 return NULL; // No home known!
438}
439
440bool wxDirExists(
441 const wxString& rDir
442)
443{
444 return (::DosSetCurrentDir(WXSTRINGCAST rDir));
445}
446
447wxString WXDLLEXPORT wxPMErrorToStr(
448 ERRORID vError
449)
450{
451 wxString sError;
452
453 //
454 // Remove the high order byte -- it is useless
455 //
456 vError &= 0x0000ffff;
457 switch(vError)
458 {
459 case PMERR_INVALID_HWND:
460 sError = wxT("Invalid window handle specified");
461 break;
462
463 case PMERR_INVALID_FLAG:
464 sError = wxT("Invalid flag bit set");
465 break;
466
467 case PMERR_NO_MSG_QUEUE:
468 sError = wxT("No message queue available");
469 break;
470
471 case PMERR_INVALID_PARM:
472 sError = wxT("Parameter contained invalid data");
473 break;
474
475 case PMERR_INVALID_PARAMETERS:
476 sError = wxT("Parameter value is out of range");
477 break;
478
479 case PMERR_PARAMETER_OUT_OF_RANGE:
480 sError = wxT("Parameter value is out of range");
481 break;
482
483 case PMERR_INVALID_INTEGER_ATOM:
484 sError = wxT("Not a valid atom");
485 break;
486
487 case PMERR_INVALID_HATOMTBL:
488 sError = wxT("Atom table handle is invalid");
489 break;
490
491 case PMERR_INVALID_ATOM_NAME:
492 sError = wxT("Not a valid atom name");
493 break;
494
495 case PMERR_ATOM_NAME_NOT_FOUND:
496 sError = wxT("Valid name format, but cannot find name in atom table");
497 break;
498
499 default:
500 sError = wxT("Unknown error");
501 }
502 return(sError);
503} // end of wxPMErrorToStr