]> git.saurik.com Git - wxWidgets.git/blob - src/os2/utils.cpp
7509ab57661c577456bfcb9f2f931b9ad9422db1
[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 #include "wx/platinfo.h"
26
27 #include <ctype.h>
28 #ifdef __EMX__
29 #include <dirent.h>
30 #endif
31
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 #if defined(__WATCOMC__)
44 extern "C"
45 {
46 #include <upm.h>
47 }
48 #elif !defined(__EMX__)
49 #include <upm.h>
50 #include <netcons.h>
51 #include <netbios.h>
52 #endif
53
54 static const wxChar WX_SECTION[] = _T("wxWidgets");
55 static const wxChar eHOSTNAME[] = _T("HostName");
56
57 // For the following functions we SHOULD fill in support
58 // for Windows-NT (which I don't know) as I assume it begin
59 // a POSIX Unix (so claims MS) that it has some special
60 // functions beyond those provided by WinSock
61
62 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
63 bool wxGetHostName( wxChar* zBuf, int nMaxSize )
64 {
65 if (!zBuf) return false;
66
67 #if defined(wxUSE_NET_API) && 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 {
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
109 bool 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
119 bool 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
129 int 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 //
140 bool 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.c_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
195 bool 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)
202 wxMemorySize 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
217 unsigned long wxGetProcessId()
218 {
219 return (unsigned long)getpid();
220 }
221
222 // ----------------------------------------------------------------------------
223 // env vars
224 // ----------------------------------------------------------------------------
225
226 bool wxGetEnv(const wxString& var, wxString *value)
227 {
228 // wxGetenv is defined as getenv()
229 wxChar *p = wxGetenv(var);
230 if ( !p )
231 return false;
232
233 if ( value )
234 {
235 *value = p;
236 }
237
238 return true;
239 }
240
241 bool wxSetEnv(const wxString& variable, const wxChar *value)
242 {
243 #if defined(HAVE_SETENV)
244 return setenv(variable.mb_str(), value ? wxString(value).mb_str().data()
245 : NULL, 1 /* overwrite */) == 0;
246 #elif defined(HAVE_PUTENV)
247 wxString s = variable;
248 if ( value )
249 s << _T('=') << value;
250
251 // transform to ANSI
252 const char *p = s.mb_str();
253
254 // the string will be free()d by libc
255 char *buf = (char *)malloc(strlen(p) + 1);
256 strcpy(buf, p);
257
258 return putenv(buf) == 0;
259 #else // no way to set an env var
260 wxUnusedVar(variable);
261 wxUnusedVar(value);
262 return false;
263 #endif
264 }
265
266 void wxMilliSleep(
267 unsigned long ulMilliseconds
268 )
269 {
270 ::DosSleep(ulMilliseconds);
271 }
272
273 void wxMicroSleep(
274 unsigned long ulMicroseconds
275 )
276 {
277 ::DosSleep(ulMicroseconds/1000);
278 }
279
280 void wxSleep(
281 int nSecs
282 )
283 {
284 ::DosSleep(1000 * nSecs);
285 }
286
287 // Consume all events until no more left
288 void wxFlushEvents()
289 {
290 // wxYield();
291 }
292
293 // Emit a beeeeeep
294 void wxBell()
295 {
296 DosBeep(1000,1000); // 1kHz during 1 sec.
297 }
298
299 wxString wxGetOsDescription()
300 {
301 wxString strVer(_T("OS/2"));
302 ULONG ulSysInfo[QSV_MAX] = {0};
303
304 if (::DosQuerySysInfo( 1L,
305 QSV_MAX,
306 (PVOID)ulSysInfo,
307 sizeof(ULONG) * QSV_MAX
308 ) == 0L )
309 {
310 wxString ver;
311 ver.Printf( _T(" ver. %d.%d rev. %c"),
312 int(ulSysInfo[QSV_VERSION_MAJOR] / 10),
313 int(ulSysInfo[QSV_VERSION_MINOR]),
314 char(ulSysInfo[QSV_VERSION_REVISION])
315 );
316 strVer += ver;
317 }
318
319 return strVer;
320 }
321
322 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab))
323 {
324 }
325
326 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab))
327 {
328 }
329
330 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
331 {
332 static wxToolkitInfo vInfo;
333 ULONG ulSysInfo[QSV_MAX] = {0};
334 APIRET ulrc;
335
336 ulrc = ::DosQuerySysInfo( 1L
337 ,QSV_MAX
338 ,(PVOID)ulSysInfo
339 ,sizeof(ULONG) * QSV_MAX
340 );
341
342 if (ulrc == 0L)
343 {
344 if ( verMaj )
345 *verMaj = ulSysInfo[QSV_VERSION_MAJOR] / 10;
346 if ( verMin )
347 *verMin = ulSysInfo[QSV_VERSION_MINOR];
348 }
349
350 return wxOS_OS2;
351 }
352
353
354 // ---------------------------------------------------------------------------
355 const wxChar* wxGetHomeDir(
356 wxString* pStr
357 )
358 {
359 wxString& rStrDir = *pStr;
360
361 // OS/2 has no idea about home,
362 // so use the working directory instead.
363 // However, we might have a valid HOME directory,
364 // as is used on many machines that have unix utilities
365 // on them, so we should use that, if available.
366
367 // 256 was taken from os2def.h
368 #ifndef MAX_PATH
369 # define MAX_PATH 256
370 #endif
371
372 const wxChar *szHome = wxGetenv((wxChar*)"HOME");
373 if ( szHome == NULL ) {
374 // we're homeless, use current directory.
375 rStrDir = wxT(".");
376 }
377 else
378 rStrDir = szHome;
379
380 return rStrDir.c_str();
381 }
382
383 // Hack for OS/2
384 #if wxUSE_UNICODE
385 const wxMB2WXbuf wxGetUserHome( const wxString &rUser )
386 #else // just for binary compatibility -- there is no 'const' here
387 wxChar* wxGetUserHome ( const wxString &rUser )
388 #endif
389 {
390 wxChar* zHome;
391 wxString sUser1(rUser);
392
393 wxChar *wxBuffer = new wxChar[256];
394 #ifndef __EMX__
395 if (!sUser1.empty())
396 {
397 wxChar zTmp[64];
398
399 if (wxGetUserId( zTmp
400 ,sizeof(zTmp)/sizeof(char)
401 ))
402 {
403 // Guests belong in the temp dir
404 if (wxStricmp(zTmp, _T("annonymous")) == 0)
405 {
406 if ((zHome = wxGetenv(_T("TMP"))) != NULL ||
407 (zHome = wxGetenv(_T("TMPDIR"))) != NULL ||
408 (zHome = wxGetenv(_T("TEMP"))) != NULL)
409 delete[] wxBuffer;
410 return *zHome ? zHome : (wxChar*)_T("\\");
411 }
412 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
413 sUser1 = wxEmptyString;
414 }
415 }
416 #endif
417 if (sUser1.empty())
418 {
419 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
420 {
421 wxStrcpy(wxBuffer, zHome);
422 wxUnix2DosFilename(wxBuffer);
423 #if wxUSE_UNICODE
424 wxWCharBuffer retBuffer (wxBuffer);
425 delete[] wxBuffer;
426 return retBuffer;
427 #else
428 wxStrcpy(zHome, wxBuffer);
429 delete[] wxBuffer;
430 return zHome;
431 #endif
432 }
433 }
434 delete[] wxBuffer;
435 return (wxChar*)wxEmptyString; // No home known!
436 }
437
438 wxString wxPMErrorToStr(ERRORID vError)
439 {
440 wxString sError;
441
442 //
443 // Remove the high order byte -- it is useless
444 //
445 vError &= 0x0000ffff;
446 switch(vError)
447 {
448 case PMERR_INVALID_HWND:
449 sError = wxT("Invalid window handle specified");
450 break;
451
452 case PMERR_INVALID_FLAG:
453 sError = wxT("Invalid flag bit set");
454 break;
455
456 case PMERR_NO_MSG_QUEUE:
457 sError = wxT("No message queue available");
458 break;
459
460 case PMERR_INVALID_PARM:
461 sError = wxT("Parameter contained invalid data");
462 break;
463
464 case PMERR_INVALID_PARAMETERS:
465 sError = wxT("Parameter value is out of range");
466 break;
467
468 case PMERR_PARAMETER_OUT_OF_RANGE:
469 sError = wxT("Parameter value is out of range");
470 break;
471
472 case PMERR_INVALID_INTEGER_ATOM:
473 sError = wxT("Not a valid atom");
474 break;
475
476 case PMERR_INVALID_HATOMTBL:
477 sError = wxT("Atom table handle is invalid");
478 break;
479
480 case PMERR_INVALID_ATOM_NAME:
481 sError = wxT("Not a valid atom name");
482 break;
483
484 case PMERR_ATOM_NAME_NOT_FOUND:
485 sError = wxT("Valid name format, but cannot find name in atom table");
486 break;
487
488 case PMERR_INV_HPS:
489 sError = wxT("PMERR_INV_HPS");
490 break;
491
492 case PMERR_PS_BUSY:
493 sError = wxT("PMERR_PS_BUSY");
494 break;
495
496 case PMERR_INV_PRIMITIVE_TYPE:
497 sError = wxT("PMERR_INV_PRIMITIVE_TYPE");
498 break;
499
500 case PMERR_UNSUPPORTED_ATTR:
501 sError = wxT("PMERR_UNSUPPORTED_ATTR");
502 break;
503
504 case PMERR_INV_COLOR_ATTR:
505 sError = wxT("PMERR_INV_COLOR_ATTR");
506 break;
507
508 case PMERR_INV_BACKGROUND_COL_ATTR:
509 sError = wxT("PMERR_INV_BACKGROUND_COL_ATTR");
510 break;
511
512 case PMERR_INV_MIX_ATTR:
513 sError = wxT("PMERR_INV_MIX_ATTR");
514 break;
515
516 case PMERR_INV_LINE_WIDTH_ATTR:
517 sError = wxT("PMERR_INV_LINE_WIDTH_ATTR");
518 break;
519
520 case PMERR_INV_GEOM_LINE_WIDTH_ATTR:
521 sError = wxT("PMERR_INV_GEOM_LINE_WIDTH_ATTR");
522 break;
523
524 case PMERR_INV_LINE_TYPE_ATTR:
525 sError = wxT("PMERR_INV_LINE_TYPE_ATTR");
526 break;
527
528 case PMERR_INV_LINE_END_ATTR:
529 sError = wxT("PMERR_INV_LINE_END_ATTR");
530 break;
531
532 case PMERR_INV_LINE_JOIN_ATTR:
533 sError = wxT("PMERR_INV_LINE_JOIN_ATTR");
534 break;
535
536 case PMERR_INV_CHAR_SET_ATTR:
537 sError = wxT("PMERR_INV_CHAR_SET_ATTR");
538 break;
539
540 case PMERR_INV_CHAR_MODE_ATTR:
541 sError = wxT("PMERR_INV_CHAR_MODE_ATTR");
542 break;
543
544 case PMERR_INV_CHAR_DIRECTION_ATTR:
545 sError = wxT("PMERR_INV_CHAR_DIRECTION_ATTR");
546 break;
547
548 case PMERR_INV_CHAR_SHEAR_ATTR:
549 sError = wxT("PMERR_INV_CHAR_SHEAR_ATTR");
550 break;
551
552 case PMERR_INV_CHAR_ANGLE_ATTR:
553 sError = wxT("PMERR_INV_CHAR_ANGLE_ATTR");
554 break;
555
556 case PMERR_INV_MARKER_SET_ATTR:
557 sError = wxT("PMERR_INV_MARKER_SET_ATTR");
558 break;
559
560 case PMERR_INV_MARKER_SYMBOL_ATTR:
561 sError = wxT("PMERR_INV_MARKER_SYMBOL_ATTR");
562 break;
563
564 case PMERR_INV_PATTERN_SET_ATTR:
565 sError = wxT("PMERR_INV_PATTERN_SET_ATTR");
566 break;
567
568 case PMERR_INV_PATTERN_ATTR:
569 sError = wxT("PMERR_INV_PATTERN_ATTR");
570 break;
571
572 case PMERR_INV_COORDINATE:
573 sError = wxT("PMERR_INV_COORDINATE");
574 break;
575
576 case PMERR_UNSUPPORTED_ATTR_VALUE:
577 sError = wxT("PMERR_UNSUPPORTED_ATTR_VALUE");
578 break;
579
580 case PMERR_INV_PATTERN_SET_FONT:
581 sError = wxT("PMERR_INV_PATTERN_SET_FONT");
582 break;
583
584 case PMERR_HUGE_FONTS_NOT_SUPPORTED:
585 sError = wxT("PMERR_HUGE_FONTS_NOT_SUPPORTED");
586 break;
587
588 default:
589 sError = wxT("Unknown error");
590 }
591 return sError;
592 } // end of wxPMErrorToStr
593
594 // replacement for implementation in unix/utilsunx.cpp,
595 // to be used by all X11 based ports.
596 struct wxEndProcessData;
597
598 void wxHandleProcessTermination(wxEndProcessData *WXUNUSED(proc_data))
599 {
600 // For now, just do nothing. To be filled in as needed.
601 }