]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/os2/utils.cpp
Fixed bug: Settings property attribute to wxNullVariant would cause GetAttribute...
[wxWidgets.git] / src / os2 / utils.cpp
... / ...
CommitLineData
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/intl.h"
19 #include "wx/log.h"
20#endif //WX_PRECOMP
21
22#include "wx/os2/private.h"
23#include "wx/apptrait.h"
24#include "wx/filename.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__)
43extern "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
53static const wxChar WX_SECTION[] = _T("wxWidgets");
54static 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)
62bool 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 (zSysname = wxGetenv(_T("HOSTNAME"))) == 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 {
99 wxStrncpy(zBuf, zSysname, nMaxSize - 1);
100 }
101
102 zBuf[nMaxSize] = _T('\0');
103#endif
104
105 return *zBuf ? true : false;
106}
107
108// Get user ID e.g. jacs
109bool wxGetUserId(wxChar* zBuf, int nType)
110{
111#if defined(__VISAGECPP__) || defined(__WATCOMC__)
112 // UPM procs return 0 on success
113 long lrc = U32ELOCU((PUCHAR)zBuf, (PULONG)&nType);
114 if (lrc == 0) return true;
115#endif
116 return false;
117}
118
119bool wxGetUserName( wxChar* zBuf, int nMaxSize )
120{
121#ifdef USE_NET_API
122 wxGetUserId( zBuf, nMaxSize );
123#else
124 wxStrncpy(zBuf, _T("Unknown User"), nMaxSize);
125#endif
126 return true;
127}
128
129int wxKill(long lPid,
130 wxSignal WXUNUSED(eSig),
131 wxKillError* WXUNUSED(peError),
132 int WXUNUSED(flags))
133{
134 return((int)::DosKillProcess(0, (PID)lPid));
135}
136
137//
138// Execute a program in an Interactive Shell
139//
140bool wxShell( const wxString& rCommand )
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.wx_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
195bool wxShutdown(wxShutdownFlags WXUNUSED(wFlags))
196{
197 // TODO
198 return false;
199}
200
201// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
202wxMemorySize wxGetFreeMemory()
203{
204 void* pMemptr = NULL;
205 LONG lSize;
206 ULONG lMemFlags;
207 APIRET rc;
208
209 lMemFlags = PAG_FREE;
210 rc = ::DosQueryMem(pMemptr, (PULONG)&lSize, &lMemFlags);
211 if (rc != 0)
212 lSize = -1L;
213 return (wxMemorySize)lSize;
214}
215
216// Get Process ID
217unsigned long wxGetProcessId()
218{
219 return (unsigned long)getpid();
220}
221
222// ----------------------------------------------------------------------------
223// env vars
224// ----------------------------------------------------------------------------
225
226bool wxGetEnv(const wxString& var, wxString *value)
227{
228 // wxGetenv is defined as getenv()
229 wxChar *p = wxGetenv((const wxChar *)var);
230 if ( !p )
231 return false;
232
233 if ( value )
234 {
235 *value = p;
236 }
237
238 return true;
239}
240
241static bool wxDoSetEnv(const wxString& variable, const char *value)
242{
243#if defined(HAVE_SETENV)
244 if ( !value )
245 {
246#ifdef HAVE_UNSETENV
247 return unsetenv(variable.mb_str()) == 0;
248#else
249 value = _T(""); // mustn't pass NULL to setenv()
250#endif
251 }
252 return setenv(variable.mb_str(), value, 1 /* overwrite */) == 0;
253#elif defined(HAVE_PUTENV)
254 wxString s = variable;
255 if ( value )
256 s << _T('=') << value;
257
258 // transform to ANSI
259 const char *p = s.mb_str();
260
261 // the string will be free()d by libc
262 char *buf = (char *)malloc(strlen(p) + 1);
263 strcpy(buf, p);
264
265 return putenv(buf) == 0;
266#else // no way to set an env var
267 wxUnusedVar(variable);
268 wxUnusedVar(value);
269 return false;
270#endif
271}
272
273bool wxSetEnv(const wxString& variable, const wxString& value)
274{
275 return wxDoSetEnv(variable, value.mb_str());
276}
277
278bool wxUnsetEnv(const wxString& variable)
279{
280 return wxDoSetEnv(variable, NULL);
281}
282
283void wxMilliSleep(
284 unsigned long ulMilliseconds
285)
286{
287 ::DosSleep(ulMilliseconds);
288}
289
290void wxMicroSleep(
291 unsigned long ulMicroseconds
292)
293{
294 ::DosSleep(ulMicroseconds/1000);
295}
296
297void wxSleep(
298 int nSecs
299)
300{
301 ::DosSleep(1000 * nSecs);
302}
303
304// Consume all events until no more left
305void wxFlushEvents()
306{
307// wxYield();
308}
309
310// Emit a beeeeeep
311void wxBell()
312{
313 DosBeep(1000,1000); // 1kHz during 1 sec.
314}
315
316wxString wxGetOsDescription()
317{
318 wxString strVer(_T("OS/2"));
319 ULONG ulSysInfo = 0;
320
321 if (::DosQuerySysInfo( QSV_VERSION_MINOR,
322 QSV_VERSION_MINOR,
323 (PVOID)&ulSysInfo,
324 sizeof(ULONG)
325 ) == 0L )
326 {
327 wxString ver;
328 ver.Printf( _T(" ver. %d.%d"),
329 int(ulSysInfo / 10),
330 int(ulSysInfo % 10)
331 );
332 strVer += ver;
333 }
334
335 return strVer;
336}
337
338bool wxIsPlatform64Bit()
339{
340 // FIXME: No idea how to test for 64 bit processor
341 // (Probably irrelevant anyhow, though).
342 return false;
343}
344
345void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab))
346{
347}
348
349void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab))
350{
351}
352
353wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
354{
355 ULONG ulSysInfo = 0;
356 APIRET ulrc;
357
358 ulrc = ::DosQuerySysInfo( QSV_VERSION_MINOR,
359 QSV_VERSION_MINOR,
360 (PVOID)&ulSysInfo,
361 sizeof(ULONG)
362 );
363
364 if (ulrc == 0L)
365 {
366 if ( verMaj )
367 *verMaj = ulSysInfo / 10;
368 if ( verMin )
369 *verMin = ulSysInfo % 10;
370 }
371
372 return wxOS_OS2;
373}
374
375
376// ---------------------------------------------------------------------------
377const wxChar* wxGetHomeDir(
378 wxString* pStr
379)
380{
381 wxString& rStrDir = *pStr;
382
383 // OS/2 has no idea about home,
384 // so use the working directory instead.
385 // However, we might have a valid HOME directory,
386 // as is used on many machines that have unix utilities
387 // on them, so we should use that, if available.
388
389 // 256 was taken from os2def.h
390#ifndef MAX_PATH
391# define MAX_PATH 256
392#endif
393
394 const wxChar *szHome = wxGetenv((wxChar*)"HOME");
395 if ( szHome == NULL ) {
396 // we're homeless, use current directory.
397 rStrDir = wxT(".");
398 }
399 else
400 rStrDir = szHome;
401
402 return rStrDir.c_str();
403}
404
405wxString wxGetUserHome ( const wxString &rUser )
406{
407 wxChar* zHome;
408 wxString sUser(rUser);
409
410 wxString home;
411
412#ifndef __EMX__
413 if (!sUser.empty())
414 {
415 const wxString currentUser = wxGetUserId();
416
417 // Guests belong in the temp dir
418 if ( currentUser == "annonymous" )
419 {
420 zHome = wxGetenv(_T("TMP"));
421 if ( !zHome )
422 zHome = wxGetenv(_T("TMPDIR"));
423 if ( !zHome )
424 zHome = wxGetenv(_T("TEMP"));
425
426 if ( zHome && *zHome )
427 return zHome;
428 }
429
430 if ( sUser == currentUser )
431 sUser.clear();
432 }
433#endif
434 if (sUser.empty())
435 {
436 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
437 {
438 home = zHome;
439 home.Replace("/", "\\");
440 }
441 }
442
443 return home;
444}
445
446bool wxGetDiskSpace(const wxString& path,
447 wxDiskspaceSize_t *pTotal,
448 wxDiskspaceSize_t *pFree)
449{
450 if (path.empty())
451 return false;
452
453 wxFileName fn(path);
454 FSALLOCATE fsaBuf = {0};
455 APIRET rc = NO_ERROR;
456 ULONG disknum = 0;
457
458 fn.MakeAbsolute();
459
460 if (wxDirExists(fn.GetFullPath()) == false)
461 return false;
462
463 disknum = wxToupper(fn.GetVolume().GetChar(0)) - _T('A') + 1;
464
465 rc = ::DosQueryFSInfo(disknum, // 1 = A, 2 = B, 3 = C, ...
466 FSIL_ALLOC, // allocation info
467 (PVOID)&fsaBuf,
468 sizeof(FSALLOCATE));
469
470 if (rc != NO_ERROR)
471 return false;
472 else
473 {
474 if(pTotal)
475 {
476 // to try to avoid 32-bit overflow, let's not multiply right away
477 // (num of alloc units)
478 *pTotal = fsaBuf.cUnit;
479 // * (num of sectors per alloc unit) * (num of bytes per sector)
480 (*pTotal) *= fsaBuf.cSectorUnit * fsaBuf.cbSector;
481 }
482 if(pFree)
483 {
484 *pFree = fsaBuf.cUnitAvail;
485 (*pFree) *= fsaBuf.cSectorUnit * fsaBuf.cbSector;
486 }
487 return true;
488 }
489}
490
491wxString wxPMErrorToStr(ERRORID vError)
492{
493 wxString sError;
494
495 //
496 // Remove the high order byte -- it is useless
497 //
498 vError &= 0x0000ffff;
499 switch(vError)
500 {
501 case PMERR_INVALID_HWND:
502 sError = wxT("Invalid window handle specified");
503 break;
504
505 case PMERR_INVALID_FLAG:
506 sError = wxT("Invalid flag bit set");
507 break;
508
509 case PMERR_NO_MSG_QUEUE:
510 sError = wxT("No message queue available");
511 break;
512
513 case PMERR_INVALID_PARM:
514 sError = wxT("Parameter contained invalid data");
515 break;
516
517 case PMERR_INVALID_PARAMETERS:
518 sError = wxT("Parameter value is out of range");
519 break;
520
521 case PMERR_PARAMETER_OUT_OF_RANGE:
522 sError = wxT("Parameter value is out of range");
523 break;
524
525 case PMERR_INVALID_INTEGER_ATOM:
526 sError = wxT("Not a valid atom");
527 break;
528
529 case PMERR_INVALID_HATOMTBL:
530 sError = wxT("Atom table handle is invalid");
531 break;
532
533 case PMERR_INVALID_ATOM_NAME:
534 sError = wxT("Not a valid atom name");
535 break;
536
537 case PMERR_ATOM_NAME_NOT_FOUND:
538 sError = wxT("Valid name format, but cannot find name in atom table");
539 break;
540
541 case PMERR_INV_HPS:
542 sError = wxT("PMERR_INV_HPS");
543 break;
544
545 case PMERR_PS_BUSY:
546 sError = wxT("PMERR_PS_BUSY");
547 break;
548
549 case PMERR_INV_PRIMITIVE_TYPE:
550 sError = wxT("PMERR_INV_PRIMITIVE_TYPE");
551 break;
552
553 case PMERR_UNSUPPORTED_ATTR:
554 sError = wxT("PMERR_UNSUPPORTED_ATTR");
555 break;
556
557 case PMERR_INV_COLOR_ATTR:
558 sError = wxT("PMERR_INV_COLOR_ATTR");
559 break;
560
561 case PMERR_INV_BACKGROUND_COL_ATTR:
562 sError = wxT("PMERR_INV_BACKGROUND_COL_ATTR");
563 break;
564
565 case PMERR_INV_MIX_ATTR:
566 sError = wxT("PMERR_INV_MIX_ATTR");
567 break;
568
569 case PMERR_INV_LINE_WIDTH_ATTR:
570 sError = wxT("PMERR_INV_LINE_WIDTH_ATTR");
571 break;
572
573 case PMERR_INV_GEOM_LINE_WIDTH_ATTR:
574 sError = wxT("PMERR_INV_GEOM_LINE_WIDTH_ATTR");
575 break;
576
577 case PMERR_INV_LINE_TYPE_ATTR:
578 sError = wxT("PMERR_INV_LINE_TYPE_ATTR");
579 break;
580
581 case PMERR_INV_LINE_END_ATTR:
582 sError = wxT("PMERR_INV_LINE_END_ATTR");
583 break;
584
585 case PMERR_INV_LINE_JOIN_ATTR:
586 sError = wxT("PMERR_INV_LINE_JOIN_ATTR");
587 break;
588
589 case PMERR_INV_CHAR_SET_ATTR:
590 sError = wxT("PMERR_INV_CHAR_SET_ATTR");
591 break;
592
593 case PMERR_INV_CHAR_MODE_ATTR:
594 sError = wxT("PMERR_INV_CHAR_MODE_ATTR");
595 break;
596
597 case PMERR_INV_CHAR_DIRECTION_ATTR:
598 sError = wxT("PMERR_INV_CHAR_DIRECTION_ATTR");
599 break;
600
601 case PMERR_INV_CHAR_SHEAR_ATTR:
602 sError = wxT("PMERR_INV_CHAR_SHEAR_ATTR");
603 break;
604
605 case PMERR_INV_CHAR_ANGLE_ATTR:
606 sError = wxT("PMERR_INV_CHAR_ANGLE_ATTR");
607 break;
608
609 case PMERR_INV_MARKER_SET_ATTR:
610 sError = wxT("PMERR_INV_MARKER_SET_ATTR");
611 break;
612
613 case PMERR_INV_MARKER_SYMBOL_ATTR:
614 sError = wxT("PMERR_INV_MARKER_SYMBOL_ATTR");
615 break;
616
617 case PMERR_INV_PATTERN_SET_ATTR:
618 sError = wxT("PMERR_INV_PATTERN_SET_ATTR");
619 break;
620
621 case PMERR_INV_PATTERN_ATTR:
622 sError = wxT("PMERR_INV_PATTERN_ATTR");
623 break;
624
625 case PMERR_INV_COORDINATE:
626 sError = wxT("PMERR_INV_COORDINATE");
627 break;
628
629 case PMERR_UNSUPPORTED_ATTR_VALUE:
630 sError = wxT("PMERR_UNSUPPORTED_ATTR_VALUE");
631 break;
632
633 case PMERR_INV_PATTERN_SET_FONT:
634 sError = wxT("PMERR_INV_PATTERN_SET_FONT");
635 break;
636
637 case PMERR_HUGE_FONTS_NOT_SUPPORTED:
638 sError = wxT("PMERR_HUGE_FONTS_NOT_SUPPORTED");
639 break;
640
641 default:
642 sError = wxT("Unknown error");
643 }
644 return sError;
645} // end of wxPMErrorToStr
646
647// replacement for implementation in unix/utilsunx.cpp,
648// to be used by all X11 based ports.
649struct wxEndProcessData;
650
651void wxHandleProcessTermination(wxEndProcessData *WXUNUSED(proc_data))
652{
653 // For now, just do nothing. To be filled in as needed.
654}