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