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