]> git.saurik.com Git - wxWidgets.git/blob - src/os2/utils.cpp
Removed some code duplicating wxAppBase functions.
[wxWidgets.git] / src / os2 / utils.cpp
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
49 static const wxChar WX_SECTION[] = _T("wxWindows");
50 static const wxChar eHOSTNAME[] = _T("HostName");
51 static const wxChar eUSERID[] = _T("UserId");
52 static 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)
60 bool 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
103 bool 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
117 bool 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
132 int 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 //
144 bool 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
202 bool 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)
209 long 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
227 bool 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
242 bool 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
265 void wxUsleep(
266 unsigned long ulMilliseconds
267 )
268 {
269 ::DosSleep(ulMilliseconds);
270 }
271
272 void wxSleep(
273 int nSecs
274 )
275 {
276 ::DosSleep(1000 * nSecs);
277 }
278
279 // Consume all events until no more left
280 void wxFlushEvents()
281 {
282 // wxYield();
283 }
284
285 #if WXWIN_COMPATIBILITY_2_2
286
287 // Output a debug mess., in a system dependent fashion.
288 void 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
303 void 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
322 void 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
342 void wxBell()
343 {
344 DosBeep(1000,1000); // 1kHz during 1 sec.
345 }
346
347 wxToolkitInfo & wxConsoleAppTraits::GetToolkitInfo()
348 {
349 static wxToolkitInfo vInfo;
350 ULONG ulSysInfo[QSV_MAX] = {0};
351 APIRET ulrc;
352
353 vInfo.name = _T("wxBase");
354 ulrc = ::DosQuerySysInfo( 1L
355 ,QSV_MAX
356 ,(PVOID)ulSysInfo
357 ,sizeof(ULONG) * QSV_MAX
358 );
359 if (ulrc == 0L)
360 {
361 vInfo.versionMajor = ulSysInfo[QSV_VERSION_MAJOR] / 10;
362 vInfo.versionMinor = ulSysInfo[QSV_VERSION_MINOR];
363 }
364 vInfo.os = wxOS2_PM;
365 return vInfo;
366 }
367
368 // ---------------------------------------------------------------------------
369 const wxChar* wxGetHomeDir(
370 wxString* pStr
371 )
372 {
373 wxString& rStrDir = *pStr;
374
375 // OS/2 has no idea about home,
376 // so use the working directory instead?
377
378 // 256 was taken from os2def.h
379 #ifndef MAX_PATH
380 # define MAX_PATH 256
381 #endif
382
383 char zDirName[256];
384 ULONG ulDirLen;
385
386 ::DosQueryCurrentDir(0, zDirName, &ulDirLen);
387 rStrDir = zDirName;
388 return rStrDir.c_str();
389 }
390
391 // Hack for OS/2
392 wxChar* wxGetUserHome (
393 const wxString& rUser
394 )
395 {
396 wxChar* zHome;
397 wxString sUser1(rUser);
398
399 wxBuffer = new wxChar[256];
400 #ifndef __EMX__
401 if (sUser1 != _T(""))
402 {
403 wxChar zTmp[64];
404
405 if (wxGetUserId( zTmp
406 ,sizeof(zTmp)/sizeof(char)
407 ))
408 {
409 // Guests belong in the temp dir
410 if (wxStricmp(zTmp, _T("annonymous")) == 0)
411 {
412 if ((zHome = wxGetenv(_T("TMP"))) != NULL ||
413 (zHome = wxGetenv(_T("TMPDIR"))) != NULL ||
414 (zHome = wxGetenv(_T("TEMP"))) != NULL)
415 delete[] wxBuffer;
416 return *zHome ? zHome : (wxChar*)_T("\\");
417 }
418 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
419 sUser1 = _T("");
420 }
421 }
422 #endif
423 if (sUser1 == _T(""))
424 {
425 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
426 {
427 wxStrcpy(wxBuffer, zHome);
428 wxUnix2DosFilename(wxBuffer);
429 wxStrcpy(zHome, wxBuffer);
430 delete[] wxBuffer;
431 return zHome;
432 }
433 }
434 delete[] wxBuffer;
435 return NULL; // No home known!
436 }
437
438 bool wxDirExists(
439 const wxString& rDir
440 )
441 {
442 return (::DosSetCurrentDir(WXSTRINGCAST rDir));
443 }
444
445 wxString WXDLLEXPORT wxPMErrorToStr(
446 ERRORID vError
447 )
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 *proc_data)
508 {
509 // For now, just do nothing. To be filled in as needed.
510 }