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