]>
Commit | Line | Data |
---|---|---|
0e320a79 DW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: utils.cpp | |
3 | // Purpose: Various utilities | |
45fcbf3b | 4 | // Author: David Webster |
0e320a79 | 5 | // Modified by: |
45fcbf3b | 6 | // Created: 09/17/99 |
0e320a79 | 7 | // RCS-ID: $Id$ |
45fcbf3b | 8 | // Copyright: (c) David Webster |
65571936 | 9 | // Licence: wxWindows licence |
0e320a79 DW |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
45fcbf3b DW |
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" | |
45fcbf3b DW |
19 | #endif //WX_PRECOMP |
20 | ||
21 | #include "wx/os2/private.h" | |
45fcbf3b | 22 | #include "wx/intl.h" |
cd478a95 | 23 | #include "wx/apptrait.h" |
45fcbf3b | 24 | |
0e320a79 | 25 | #include <ctype.h> |
06298235 SN |
26 | #ifdef __EMX__ |
27 | #include <dirent.h> | |
06298235 | 28 | #endif |
45fcbf3b DW |
29 | |
30 | #include "wx/log.h" | |
31 | ||
32 | #include <io.h> | |
0e320a79 DW |
33 | |
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include <string.h> | |
45fcbf3b | 37 | #include <errno.h> |
0e320a79 DW |
38 | #include <stdarg.h> |
39 | ||
d90895ac | 40 | #define PURE_32 |
468e327a | 41 | |
06298235 | 42 | #ifndef __EMX__ |
7cdc2f1e DW |
43 | #include <upm.h> |
44 | #include <netcons.h> | |
45 | #include <netbios.h> | |
06298235 | 46 | #endif |
45fcbf3b | 47 | |
77ffb593 | 48 | static const wxChar WX_SECTION[] = _T("wxWidgets"); |
86de7616 DW |
49 | static const wxChar eHOSTNAME[] = _T("HostName"); |
50 | static const wxChar eUSERID[] = _T("UserId"); | |
51 | static const wxChar eUSERNAME[] = _T("UserName"); | |
45fcbf3b DW |
52 | |
53 | // For the following functions we SHOULD fill in support | |
54 | // for Windows-NT (which I don't know) as I assume it begin | |
55 | // a POSIX Unix (so claims MS) that it has some special | |
56 | // functions beyond those provided by WinSock | |
57 | ||
0e320a79 | 58 | // Get full hostname (eg. DoDo.BSn-Germany.crg.de) |
10e5b930 DW |
59 | bool wxGetHostName( |
60 | wxChar* zBuf | |
61 | , int nMaxSize | |
62 | ) | |
0e320a79 | 63 | { |
d90895ac | 64 | #if wxUSE_NET_API |
10e5b930 DW |
65 | char zServer[256]; |
66 | char zComputer[256]; | |
909b4f08 | 67 | unsigned long ulLevel = 0; |
9dea36ef DW |
68 | unsigned char* zBuffer = NULL; |
69 | unsigned long ulBuffer = 256; | |
70 | unsigned long* pulTotalAvail = NULL; | |
10e5b930 | 71 | |
dde11e60 DW |
72 | NetBios32GetInfo( (const unsigned char*)zServer |
73 | ,(const unsigned char*)zComputer | |
909b4f08 | 74 | ,ulLevel |
dde11e60 | 75 | ,zBuffer |
909b4f08 DW |
76 | ,ulBuffer |
77 | ,pulTotalAvail | |
dde11e60 | 78 | ); |
10e5b930 | 79 | strcpy(zBuf, zServer); |
45fcbf3b | 80 | #else |
10e5b930 DW |
81 | wxChar* zSysname; |
82 | const wxChar* zDefaultHost = _T("noname"); | |
45fcbf3b | 83 | |
10e5b930 DW |
84 | if ((zSysname = wxGetenv(_T("SYSTEM_NAME"))) == NULL) |
85 | { | |
9923c37d DW |
86 | ::PrfQueryProfileString( HINI_PROFILE |
87 | ,(PSZ)WX_SECTION | |
88 | ,(PSZ)eHOSTNAME | |
89 | ,(PSZ)zDefaultHost | |
90 | ,(void*)zBuf | |
91 | ,(ULONG)nMaxSize - 1 | |
92 | ); | |
10e5b930 DW |
93 | } |
94 | else | |
95 | wxStrncpy(zBuf, zSysname, nMaxSize - 1); | |
96 | zBuf[nMaxSize] = _T('\0'); | |
45fcbf3b | 97 | #endif |
10e5b930 | 98 | return *zBuf ? TRUE : FALSE; |
0e320a79 DW |
99 | } |
100 | ||
101 | // Get user ID e.g. jacs | |
10e5b930 DW |
102 | bool wxGetUserId( |
103 | wxChar* zBuf | |
2bd5bbc9 | 104 | , int nType |
10e5b930 | 105 | ) |
0e320a79 | 106 | { |
19193a2c | 107 | #if defined(__VISAGECPP__) |
2bd5bbc9 DW |
108 | long lrc; |
109 | // UPM procs return 0 on success | |
110 | lrc = U32ELOCU((unsigned char*)zBuf, (unsigned long *)&nType); | |
111 | if (lrc == 0) return TRUE; | |
06298235 | 112 | #endif |
2bd5bbc9 | 113 | return FALSE; |
0e320a79 DW |
114 | } |
115 | ||
10e5b930 DW |
116 | bool wxGetUserName( |
117 | wxChar* zBuf | |
118 | , int nMaxSize | |
119 | ) | |
0e320a79 | 120 | { |
45fcbf3b | 121 | #ifdef USE_NET_API |
10e5b930 DW |
122 | wxGetUserId( zBuf |
123 | ,nMaxSize | |
124 | ); | |
45fcbf3b | 125 | #else |
10e5b930 | 126 | wxStrncpy(zBuf, _T("Unknown User"), nMaxSize); |
45fcbf3b | 127 | #endif |
10e5b930 | 128 | return TRUE; |
0e320a79 DW |
129 | } |
130 | ||
10e5b930 DW |
131 | int wxKill( |
132 | long lPid | |
2004cad1 DW |
133 | , wxSignal eSig |
134 | , wxKillError* peError | |
10e5b930 | 135 | ) |
0e320a79 | 136 | { |
10e5b930 | 137 | return((int)::DosKillProcess(0, (PID)lPid)); |
0e320a79 DW |
138 | } |
139 | ||
140 | // | |
141 | // Execute a program in an Interactive Shell | |
142 | // | |
10e5b930 DW |
143 | bool wxShell( |
144 | const wxString& rCommand | |
145 | ) | |
0e320a79 | 146 | { |
c5fb56c0 DW |
147 | wxChar* zShell = _T("CMD.EXE"); |
148 | wxString sInputs; | |
c5fb56c0 DW |
149 | STARTDATA SData = {0}; |
150 | PSZ PgmTitle = "Command Shell"; | |
151 | APIRET rc; | |
152 | PID vPid = 0; | |
153 | ULONG ulSessID = 0; | |
154 | UCHAR achObjBuf[256] = {0}; //error data if DosStart fails | |
155 | RESULTCODES vResult; | |
156 | ||
157 | SData.Length = sizeof(STARTDATA); | |
158 | SData.Related = SSF_RELATED_INDEPENDENT; | |
159 | SData.FgBg = SSF_FGBG_FORE; | |
160 | SData.TraceOpt = SSF_TRACEOPT_NONE; | |
161 | SData.PgmTitle = PgmTitle; | |
162 | SData.PgmName = zShell; | |
163 | ||
909b4f08 | 164 | sInputs = "/C " + rCommand; |
9ac6ff7b | 165 | SData.PgmInputs = (BYTE*)sInputs.c_str(); |
c5fb56c0 DW |
166 | SData.TermQ = 0; |
167 | SData.Environment = 0; | |
168 | SData.InheritOpt = SSF_INHERTOPT_SHELL; | |
169 | SData.SessionType = SSF_TYPE_WINDOWABLEVIO; | |
170 | SData.IconFile = 0; | |
171 | SData.PgmHandle = 0; | |
172 | SData.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_MAXIMIZE; | |
173 | SData.InitXPos = 30; | |
174 | SData.InitYPos = 40; | |
175 | SData.InitXSize = 200; | |
176 | SData.InitYSize = 140; | |
177 | SData.Reserved = 0; | |
178 | SData.ObjectBuffer = (char*)achObjBuf; | |
179 | SData.ObjectBuffLen = (ULONG)sizeof(achObjBuf); | |
180 | ||
181 | rc = ::DosStartSession(&SData, &ulSessID, &vPid); | |
9ac6ff7b | 182 | if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND |
c5fb56c0 DW |
183 | { |
184 | PTIB ptib; | |
185 | PPIB ppib; | |
10e5b930 | 186 | |
c5fb56c0 | 187 | ::DosGetInfoBlocks(&ptib, &ppib); |
10e5b930 | 188 | |
c5fb56c0 DW |
189 | ::DosWaitChild( DCWA_PROCESS |
190 | ,DCWW_WAIT | |
191 | ,&vResult | |
192 | ,&ppib->pib_ulpid | |
193 | ,vPid | |
194 | ); | |
195 | } | |
196 | return (rc != 0); | |
0e320a79 DW |
197 | } |
198 | ||
a23692f0 | 199 | // Shutdown or reboot the PC |
f6ba47d9 VZ |
200 | bool wxShutdown(wxShutdownFlags wFlags) |
201 | { | |
202 | // TODO | |
203 | return FALSE; | |
204 | } | |
205 | ||
0e320a79 | 206 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) |
5d4b632b | 207 | long wxGetFreeMemory() |
0e320a79 | 208 | { |
9dea36ef | 209 | void* pMemptr = NULL; |
78d50441 DW |
210 | ULONG lSize; |
211 | ULONG lMemFlags; | |
212 | APIRET rc; | |
213 | ||
214 | lMemFlags = PAG_FREE; | |
10e5b930 | 215 | rc = ::DosQueryMem(pMemptr, &lSize, &lMemFlags); |
78d50441 DW |
216 | if (rc != 0) |
217 | return -1L; | |
218 | return (long)lSize; | |
45fcbf3b DW |
219 | } |
220 | ||
8269a903 SN |
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(var); | |
229 | if ( !p ) | |
230 | return FALSE; | |
231 | ||
232 | if ( value ) | |
233 | { | |
234 | *value = p; | |
235 | } | |
236 | ||
237 | return TRUE; | |
238 | } | |
239 | ||
240 | bool wxSetEnv(const wxString& variable, const wxChar *value) | |
241 | { | |
242 | #if defined(HAVE_SETENV) | |
243 | return setenv(variable.mb_str(), value ? wxString(value).mb_str().data() | |
244 | : NULL, 1 /* overwrite */) == 0; | |
245 | #elif defined(HAVE_PUTENV) | |
246 | wxString s = variable; | |
247 | if ( value ) | |
248 | s << _T('=') << value; | |
249 | ||
250 | // transform to ANSI | |
251 | const char *p = s.mb_str(); | |
252 | ||
253 | // the string will be free()d by libc | |
254 | char *buf = (char *)malloc(strlen(p) + 1); | |
255 | strcpy(buf, p); | |
256 | ||
257 | return putenv(buf) == 0; | |
258 | #else // no way to set an env var | |
259 | return FALSE; | |
260 | #endif | |
261 | } | |
262 | ||
d3a919bd | 263 | void wxMilliSleep( |
10e5b930 DW |
264 | unsigned long ulMilliseconds |
265 | ) | |
45fcbf3b | 266 | { |
f53a0d32 | 267 | ::DosSleep(ulMilliseconds); |
0e320a79 DW |
268 | } |
269 | ||
d3a919bd SN |
270 | void wxMicroSleep( |
271 | unsigned long ulMicroseconds | |
272 | ) | |
273 | { | |
274 | ::DosSleep(ulMicroseconds/1000); | |
275 | } | |
276 | ||
10e5b930 DW |
277 | void wxSleep( |
278 | int nSecs | |
279 | ) | |
0e320a79 | 280 | { |
10e5b930 | 281 | ::DosSleep(1000 * nSecs); |
0e320a79 DW |
282 | } |
283 | ||
284 | // Consume all events until no more left | |
285 | void wxFlushEvents() | |
286 | { | |
45fcbf3b | 287 | // wxYield(); |
0e320a79 DW |
288 | } |
289 | ||
73deed44 VZ |
290 | #if WXWIN_COMPATIBILITY_2_2 |
291 | ||
45fcbf3b | 292 | // Output a debug mess., in a system dependent fashion. |
10e5b930 DW |
293 | void wxDebugMsg( |
294 | const wxChar* zFmt ... | |
295 | ) | |
296 | { | |
297 | va_list vAp; | |
298 | static wxChar zBuffer[512]; | |
299 | ||
300 | if (!wxTheApp->GetWantDebugOutput()) | |
301 | return ; | |
302 | va_start(vAp, zFmt); | |
303 | sprintf(zBuffer, zFmt, vAp) ; | |
304 | va_end(vAp); | |
0e320a79 DW |
305 | } |
306 | ||
307 | // Non-fatal error: pop up message box and (possibly) continue | |
10e5b930 DW |
308 | void wxError( |
309 | const wxString& rMsg | |
310 | , const wxString& rTitle | |
311 | ) | |
312 | { | |
bed67b7f | 313 | wxChar *wxBuffer = new wxChar[256]; |
10e5b930 DW |
314 | wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST rMsg); |
315 | if (::WinMessageBox( HWND_DESKTOP | |
316 | ,NULL | |
317 | ,(PSZ)wxBuffer | |
318 | ,(PSZ)WXSTRINGCAST rTitle | |
319 | ,0 | |
320 | ,MB_ICONEXCLAMATION | MB_YESNO | |
321 | ) == MBID_YES) | |
13a4ea8d | 322 | delete[] wxBuffer; |
0e320a79 DW |
323 | wxExit(); |
324 | } | |
325 | ||
326 | // Fatal error: pop up message box and abort | |
10e5b930 DW |
327 | void wxFatalError( |
328 | const wxString& rMsg | |
329 | , const wxString& rTitle | |
330 | ) | |
331 | { | |
332 | unsigned long ulRc; | |
333 | ||
334 | ulRc = ::WinMessageBox( HWND_DESKTOP | |
335 | ,NULL | |
336 | ,WXSTRINGCAST rMsg | |
337 | ,WXSTRINGCAST rTitle | |
338 | ,0 | |
339 | ,MB_NOICON | MB_OK | |
340 | ); | |
341 | DosExit(EXIT_PROCESS, ulRc); | |
0e320a79 DW |
342 | } |
343 | ||
73deed44 VZ |
344 | #endif // WXWIN_COMPATIBILITY_2_2 |
345 | ||
0e320a79 DW |
346 | // Emit a beeeeeep |
347 | void wxBell() | |
348 | { | |
45fcbf3b | 349 | DosBeep(1000,1000); // 1kHz during 1 sec. |
0e320a79 | 350 | } |
621ccd8a | 351 | |
8329e6c7 SN |
352 | |
353 | void wxAppTraits::InitializeGui(unsigned long &ulHab) | |
354 | { | |
355 | } | |
356 | ||
357 | void wxAppTraits::TerminateGui(unsigned long ulHab) | |
358 | { | |
359 | } | |
360 | ||
621ccd8a | 361 | wxToolkitInfo & wxConsoleAppTraits::GetToolkitInfo() |
10e5b930 | 362 | { |
621ccd8a | 363 | static wxToolkitInfo vInfo; |
10e5b930 | 364 | ULONG ulSysInfo[QSV_MAX] = {0}; |
5d4b632b DW |
365 | APIRET ulrc; |
366 | ||
621ccd8a | 367 | vInfo.name = _T("wxBase"); |
5d4b632b DW |
368 | ulrc = ::DosQuerySysInfo( 1L |
369 | ,QSV_MAX | |
370 | ,(PVOID)ulSysInfo | |
371 | ,sizeof(ULONG) * QSV_MAX | |
372 | ); | |
373 | if (ulrc == 0L) | |
10e5b930 | 374 | { |
621ccd8a SN |
375 | vInfo.versionMajor = ulSysInfo[QSV_VERSION_MAJOR] / 10; |
376 | vInfo.versionMinor = ulSysInfo[QSV_VERSION_MINOR]; | |
10e5b930 | 377 | } |
621ccd8a SN |
378 | vInfo.os = wxOS2_PM; |
379 | return vInfo; | |
0e320a79 | 380 | } |
621ccd8a | 381 | |
45fcbf3b | 382 | // --------------------------------------------------------------------------- |
1be7f92a DW |
383 | const wxChar* wxGetHomeDir( |
384 | wxString* pStr | |
385 | ) | |
45fcbf3b | 386 | { |
1be7f92a | 387 | wxString& rStrDir = *pStr; |
0e320a79 | 388 | |
1be7f92a | 389 | // OS/2 has no idea about home, |
b08c90ca SN |
390 | // so use the working directory instead. |
391 | // However, we might have a valid HOME directory, | |
392 | // as is used on many machines that have unix utilities | |
393 | // on them, so we should use that, if available. | |
45fcbf3b | 394 | |
1be7f92a | 395 | // 256 was taken from os2def.h |
45fcbf3b DW |
396 | #ifndef MAX_PATH |
397 | # define MAX_PATH 256 | |
398 | #endif | |
399 | ||
b08c90ca SN |
400 | const wxChar *szHome = wxGetenv("HOME"); |
401 | if ( szHome == NULL ) { | |
402 | // we're homeless, use current directory. | |
403 | rStrDir = wxT("."); | |
404 | } | |
405 | else | |
406 | rStrDir = szHome; | |
45fcbf3b | 407 | |
1be7f92a | 408 | return rStrDir.c_str(); |
45fcbf3b DW |
409 | } |
410 | ||
10e5b930 | 411 | // Hack for OS/2 |
1be7f92a DW |
412 | wxChar* wxGetUserHome ( |
413 | const wxString& rUser | |
414 | ) | |
415 | { | |
416 | wxChar* zHome; | |
417 | wxString sUser1(rUser); | |
418 | ||
bed67b7f | 419 | char *wxBuffer = new wxChar[256]; |
06298235 | 420 | #ifndef __EMX__ |
1be7f92a | 421 | if (sUser1 != _T("")) |
45fcbf3b | 422 | { |
1be7f92a DW |
423 | wxChar zTmp[64]; |
424 | ||
425 | if (wxGetUserId( zTmp | |
426 | ,sizeof(zTmp)/sizeof(char) | |
427 | )) | |
428 | { | |
429 | // Guests belong in the temp dir | |
430 | if (wxStricmp(zTmp, _T("annonymous")) == 0) | |
431 | { | |
432 | if ((zHome = wxGetenv(_T("TMP"))) != NULL || | |
433 | (zHome = wxGetenv(_T("TMPDIR"))) != NULL || | |
434 | (zHome = wxGetenv(_T("TEMP"))) != NULL) | |
13a4ea8d | 435 | delete[] wxBuffer; |
1be7f92a DW |
436 | return *zHome ? zHome : (wxChar*)_T("\\"); |
437 | } | |
438 | if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0) | |
439 | sUser1 = _T(""); | |
440 | } | |
45fcbf3b | 441 | } |
06298235 | 442 | #endif |
1be7f92a | 443 | if (sUser1 == _T("")) |
13a4ea8d | 444 | { |
1be7f92a DW |
445 | if ((zHome = wxGetenv(_T("HOME"))) != NULL) |
446 | { | |
447 | wxStrcpy(wxBuffer, zHome); | |
2b5f62a0 | 448 | wxUnix2DosFilename(wxBuffer); |
13a4ea8d DW |
449 | wxStrcpy(zHome, wxBuffer); |
450 | delete[] wxBuffer; | |
451 | return zHome; | |
1be7f92a | 452 | } |
13a4ea8d DW |
453 | } |
454 | delete[] wxBuffer; | |
455 | return NULL; // No home known! | |
0e320a79 DW |
456 | } |
457 | ||
1be7f92a DW |
458 | bool wxDirExists( |
459 | const wxString& rDir | |
460 | ) | |
45fcbf3b | 461 | { |
1be7f92a | 462 | return (::DosSetCurrentDir(WXSTRINGCAST rDir)); |
45fcbf3b DW |
463 | } |
464 | ||
914589c2 DW |
465 | wxString WXDLLEXPORT wxPMErrorToStr( |
466 | ERRORID vError | |
467 | ) | |
468 | { | |
469 | wxString sError; | |
470 | ||
471 | // | |
472 | // Remove the high order byte -- it is useless | |
473 | // | |
474 | vError &= 0x0000ffff; | |
475 | switch(vError) | |
476 | { | |
477 | case PMERR_INVALID_HWND: | |
478 | sError = wxT("Invalid window handle specified"); | |
479 | break; | |
480 | ||
481 | case PMERR_INVALID_FLAG: | |
482 | sError = wxT("Invalid flag bit set"); | |
483 | break; | |
484 | ||
485 | case PMERR_NO_MSG_QUEUE: | |
486 | sError = wxT("No message queue available"); | |
487 | break; | |
488 | ||
489 | case PMERR_INVALID_PARM: | |
490 | sError = wxT("Parameter contained invalid data"); | |
491 | break; | |
492 | ||
493 | case PMERR_INVALID_PARAMETERS: | |
494 | sError = wxT("Parameter value is out of range"); | |
495 | break; | |
496 | ||
497 | case PMERR_PARAMETER_OUT_OF_RANGE: | |
498 | sError = wxT("Parameter value is out of range"); | |
499 | break; | |
500 | ||
501 | case PMERR_INVALID_INTEGER_ATOM: | |
502 | sError = wxT("Not a valid atom"); | |
503 | break; | |
504 | ||
505 | case PMERR_INVALID_HATOMTBL: | |
506 | sError = wxT("Atom table handle is invalid"); | |
507 | break; | |
508 | ||
509 | case PMERR_INVALID_ATOM_NAME: | |
510 | sError = wxT("Not a valid atom name"); | |
511 | break; | |
512 | ||
513 | case PMERR_ATOM_NAME_NOT_FOUND: | |
514 | sError = wxT("Valid name format, but cannot find name in atom table"); | |
515 | break; | |
516 | ||
517 | default: | |
518 | sError = wxT("Unknown error"); | |
519 | } | |
520 | return(sError); | |
521 | } // end of wxPMErrorToStr | |
621ccd8a SN |
522 | |
523 | // replacement for implementation in unix/utilsunx.cpp, | |
524 | // to be used by all X11 based ports. | |
525 | struct wxEndProcessData; | |
526 | ||
527 | void wxHandleProcessTermination(wxEndProcessData *proc_data) | |
528 | { | |
529 | // For now, just do nothing. To be filled in as needed. | |
530 | } |