]>
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 |
9d8aca48 | 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); | |
9d8aca48 | 111 | if (lrc == 0) return true; |
06298235 | 112 | #endif |
9d8aca48 | 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 |
9d8aca48 | 128 | return true; |
0e320a79 DW |
129 | } |
130 | ||
10e5b930 DW |
131 | int wxKill( |
132 | long lPid | |
2004cad1 DW |
133 | , wxSignal eSig |
134 | , wxKillError* peError | |
e0f6b731 | 135 | , int flags |
10e5b930 | 136 | ) |
0e320a79 | 137 | { |
10e5b930 | 138 | return((int)::DosKillProcess(0, (PID)lPid)); |
0e320a79 DW |
139 | } |
140 | ||
141 | // | |
142 | // Execute a program in an Interactive Shell | |
143 | // | |
10e5b930 DW |
144 | bool wxShell( |
145 | const wxString& rCommand | |
146 | ) | |
0e320a79 | 147 | { |
c5fb56c0 DW |
148 | wxChar* zShell = _T("CMD.EXE"); |
149 | wxString sInputs; | |
c5fb56c0 DW |
150 | STARTDATA SData = {0}; |
151 | PSZ PgmTitle = "Command Shell"; | |
152 | APIRET rc; | |
153 | PID vPid = 0; | |
154 | ULONG ulSessID = 0; | |
155 | UCHAR achObjBuf[256] = {0}; //error data if DosStart fails | |
156 | RESULTCODES vResult; | |
157 | ||
158 | SData.Length = sizeof(STARTDATA); | |
159 | SData.Related = SSF_RELATED_INDEPENDENT; | |
160 | SData.FgBg = SSF_FGBG_FORE; | |
161 | SData.TraceOpt = SSF_TRACEOPT_NONE; | |
162 | SData.PgmTitle = PgmTitle; | |
0fba44b4 | 163 | SData.PgmName = (char*)zShell; |
c5fb56c0 | 164 | |
0fba44b4 | 165 | sInputs = _T("/C ") + rCommand; |
9ac6ff7b | 166 | SData.PgmInputs = (BYTE*)sInputs.c_str(); |
c5fb56c0 DW |
167 | SData.TermQ = 0; |
168 | SData.Environment = 0; | |
169 | SData.InheritOpt = SSF_INHERTOPT_SHELL; | |
170 | SData.SessionType = SSF_TYPE_WINDOWABLEVIO; | |
171 | SData.IconFile = 0; | |
172 | SData.PgmHandle = 0; | |
173 | SData.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_MAXIMIZE; | |
174 | SData.InitXPos = 30; | |
175 | SData.InitYPos = 40; | |
176 | SData.InitXSize = 200; | |
177 | SData.InitYSize = 140; | |
178 | SData.Reserved = 0; | |
179 | SData.ObjectBuffer = (char*)achObjBuf; | |
180 | SData.ObjectBuffLen = (ULONG)sizeof(achObjBuf); | |
181 | ||
182 | rc = ::DosStartSession(&SData, &ulSessID, &vPid); | |
9ac6ff7b | 183 | if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND |
c5fb56c0 DW |
184 | { |
185 | PTIB ptib; | |
186 | PPIB ppib; | |
10e5b930 | 187 | |
c5fb56c0 | 188 | ::DosGetInfoBlocks(&ptib, &ppib); |
10e5b930 | 189 | |
c5fb56c0 DW |
190 | ::DosWaitChild( DCWA_PROCESS |
191 | ,DCWW_WAIT | |
192 | ,&vResult | |
193 | ,&ppib->pib_ulpid | |
194 | ,vPid | |
195 | ); | |
196 | } | |
197 | return (rc != 0); | |
0e320a79 DW |
198 | } |
199 | ||
a23692f0 | 200 | // Shutdown or reboot the PC |
f6ba47d9 VZ |
201 | bool wxShutdown(wxShutdownFlags wFlags) |
202 | { | |
203 | // TODO | |
9d8aca48 | 204 | return false; |
f6ba47d9 VZ |
205 | } |
206 | ||
8ea92b4d WS |
207 | wxPowerType wxGetPowerType() |
208 | { | |
209 | // TODO | |
210 | return wxPOWER_UNKNOWN; | |
211 | } | |
212 | ||
213 | wxBatteryState wxGetBatteryState() | |
214 | { | |
215 | // TODO | |
216 | return wxBATTERY_UNKNOWN_STATE; | |
217 | } | |
218 | ||
0e320a79 | 219 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) |
9d8aca48 | 220 | wxMemorySize wxGetFreeMemory() |
0e320a79 | 221 | { |
9dea36ef | 222 | void* pMemptr = NULL; |
0d598bae | 223 | LONG lSize; |
78d50441 DW |
224 | ULONG lMemFlags; |
225 | APIRET rc; | |
226 | ||
227 | lMemFlags = PAG_FREE; | |
0d598bae | 228 | rc = ::DosQueryMem(pMemptr, (PULONG)&lSize, &lMemFlags); |
78d50441 | 229 | if (rc != 0) |
9d8aca48 WS |
230 | lSize = -1L; |
231 | return (wxMemorySize)lSize; | |
45fcbf3b DW |
232 | } |
233 | ||
264f00e7 SN |
234 | // Get Process ID |
235 | unsigned long wxGetProcessId() | |
236 | { | |
237 | return (unsigned long)getpid(); | |
238 | } | |
239 | ||
8269a903 SN |
240 | // ---------------------------------------------------------------------------- |
241 | // env vars | |
242 | // ---------------------------------------------------------------------------- | |
243 | ||
244 | bool wxGetEnv(const wxString& var, wxString *value) | |
245 | { | |
246 | // wxGetenv is defined as getenv() | |
247 | wxChar *p = wxGetenv(var); | |
248 | if ( !p ) | |
9d8aca48 | 249 | return false; |
8269a903 SN |
250 | |
251 | if ( value ) | |
252 | { | |
253 | *value = p; | |
254 | } | |
255 | ||
9d8aca48 | 256 | return true; |
8269a903 SN |
257 | } |
258 | ||
259 | bool wxSetEnv(const wxString& variable, const wxChar *value) | |
260 | { | |
261 | #if defined(HAVE_SETENV) | |
262 | return setenv(variable.mb_str(), value ? wxString(value).mb_str().data() | |
263 | : NULL, 1 /* overwrite */) == 0; | |
264 | #elif defined(HAVE_PUTENV) | |
265 | wxString s = variable; | |
266 | if ( value ) | |
267 | s << _T('=') << value; | |
268 | ||
269 | // transform to ANSI | |
270 | const char *p = s.mb_str(); | |
271 | ||
272 | // the string will be free()d by libc | |
273 | char *buf = (char *)malloc(strlen(p) + 1); | |
274 | strcpy(buf, p); | |
275 | ||
276 | return putenv(buf) == 0; | |
277 | #else // no way to set an env var | |
9d8aca48 | 278 | return false; |
8269a903 SN |
279 | #endif |
280 | } | |
281 | ||
d3a919bd | 282 | void wxMilliSleep( |
10e5b930 DW |
283 | unsigned long ulMilliseconds |
284 | ) | |
45fcbf3b | 285 | { |
f53a0d32 | 286 | ::DosSleep(ulMilliseconds); |
0e320a79 DW |
287 | } |
288 | ||
d3a919bd SN |
289 | void wxMicroSleep( |
290 | unsigned long ulMicroseconds | |
291 | ) | |
292 | { | |
293 | ::DosSleep(ulMicroseconds/1000); | |
294 | } | |
295 | ||
10e5b930 DW |
296 | void wxSleep( |
297 | int nSecs | |
298 | ) | |
0e320a79 | 299 | { |
10e5b930 | 300 | ::DosSleep(1000 * nSecs); |
0e320a79 DW |
301 | } |
302 | ||
303 | // Consume all events until no more left | |
304 | void wxFlushEvents() | |
305 | { | |
45fcbf3b | 306 | // wxYield(); |
0e320a79 DW |
307 | } |
308 | ||
73deed44 VZ |
309 | #if WXWIN_COMPATIBILITY_2_2 |
310 | ||
45fcbf3b | 311 | // Output a debug mess., in a system dependent fashion. |
10e5b930 DW |
312 | void wxDebugMsg( |
313 | const wxChar* zFmt ... | |
314 | ) | |
315 | { | |
316 | va_list vAp; | |
317 | static wxChar zBuffer[512]; | |
318 | ||
319 | if (!wxTheApp->GetWantDebugOutput()) | |
320 | return ; | |
321 | va_start(vAp, zFmt); | |
322 | sprintf(zBuffer, zFmt, vAp) ; | |
323 | va_end(vAp); | |
0e320a79 DW |
324 | } |
325 | ||
326 | // Non-fatal error: pop up message box and (possibly) continue | |
10e5b930 DW |
327 | void wxError( |
328 | const wxString& rMsg | |
329 | , const wxString& rTitle | |
330 | ) | |
331 | { | |
bed67b7f | 332 | wxChar *wxBuffer = new wxChar[256]; |
10e5b930 DW |
333 | wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST rMsg); |
334 | if (::WinMessageBox( HWND_DESKTOP | |
335 | ,NULL | |
336 | ,(PSZ)wxBuffer | |
337 | ,(PSZ)WXSTRINGCAST rTitle | |
338 | ,0 | |
339 | ,MB_ICONEXCLAMATION | MB_YESNO | |
340 | ) == MBID_YES) | |
13a4ea8d | 341 | delete[] wxBuffer; |
0e320a79 DW |
342 | wxExit(); |
343 | } | |
344 | ||
345 | // Fatal error: pop up message box and abort | |
10e5b930 DW |
346 | void wxFatalError( |
347 | const wxString& rMsg | |
348 | , const wxString& rTitle | |
349 | ) | |
350 | { | |
351 | unsigned long ulRc; | |
352 | ||
353 | ulRc = ::WinMessageBox( HWND_DESKTOP | |
354 | ,NULL | |
355 | ,WXSTRINGCAST rMsg | |
356 | ,WXSTRINGCAST rTitle | |
357 | ,0 | |
358 | ,MB_NOICON | MB_OK | |
359 | ); | |
360 | DosExit(EXIT_PROCESS, ulRc); | |
0e320a79 DW |
361 | } |
362 | ||
73deed44 VZ |
363 | #endif // WXWIN_COMPATIBILITY_2_2 |
364 | ||
0e320a79 DW |
365 | // Emit a beeeeeep |
366 | void wxBell() | |
367 | { | |
45fcbf3b | 368 | DosBeep(1000,1000); // 1kHz during 1 sec. |
0e320a79 | 369 | } |
621ccd8a | 370 | |
8329e6c7 SN |
371 | |
372 | void wxAppTraits::InitializeGui(unsigned long &ulHab) | |
373 | { | |
374 | } | |
375 | ||
376 | void wxAppTraits::TerminateGui(unsigned long ulHab) | |
377 | { | |
378 | } | |
379 | ||
621ccd8a | 380 | wxToolkitInfo & wxConsoleAppTraits::GetToolkitInfo() |
10e5b930 | 381 | { |
9d8aca48 WS |
382 | static wxToolkitInfo vInfo; |
383 | ULONG ulSysInfo[QSV_MAX] = {0}; | |
384 | APIRET ulrc; | |
5d4b632b | 385 | |
621ccd8a | 386 | vInfo.name = _T("wxBase"); |
5d4b632b DW |
387 | ulrc = ::DosQuerySysInfo( 1L |
388 | ,QSV_MAX | |
389 | ,(PVOID)ulSysInfo | |
390 | ,sizeof(ULONG) * QSV_MAX | |
391 | ); | |
392 | if (ulrc == 0L) | |
10e5b930 | 393 | { |
621ccd8a SN |
394 | vInfo.versionMajor = ulSysInfo[QSV_VERSION_MAJOR] / 10; |
395 | vInfo.versionMinor = ulSysInfo[QSV_VERSION_MINOR]; | |
10e5b930 | 396 | } |
621ccd8a SN |
397 | vInfo.os = wxOS2_PM; |
398 | return vInfo; | |
0e320a79 | 399 | } |
621ccd8a | 400 | |
45fcbf3b | 401 | // --------------------------------------------------------------------------- |
1be7f92a DW |
402 | const wxChar* wxGetHomeDir( |
403 | wxString* pStr | |
404 | ) | |
45fcbf3b | 405 | { |
1be7f92a | 406 | wxString& rStrDir = *pStr; |
0e320a79 | 407 | |
1be7f92a | 408 | // OS/2 has no idea about home, |
b08c90ca SN |
409 | // so use the working directory instead. |
410 | // However, we might have a valid HOME directory, | |
411 | // as is used on many machines that have unix utilities | |
412 | // on them, so we should use that, if available. | |
45fcbf3b | 413 | |
1be7f92a | 414 | // 256 was taken from os2def.h |
45fcbf3b DW |
415 | #ifndef MAX_PATH |
416 | # define MAX_PATH 256 | |
417 | #endif | |
418 | ||
0fba44b4 | 419 | const wxChar *szHome = wxGetenv((wxChar*)"HOME"); |
b08c90ca SN |
420 | if ( szHome == NULL ) { |
421 | // we're homeless, use current directory. | |
422 | rStrDir = wxT("."); | |
423 | } | |
424 | else | |
425 | rStrDir = szHome; | |
45fcbf3b | 426 | |
1be7f92a | 427 | return rStrDir.c_str(); |
45fcbf3b DW |
428 | } |
429 | ||
10e5b930 | 430 | // Hack for OS/2 |
1be7f92a DW |
431 | wxChar* wxGetUserHome ( |
432 | const wxString& rUser | |
433 | ) | |
434 | { | |
435 | wxChar* zHome; | |
436 | wxString sUser1(rUser); | |
437 | ||
0fba44b4 | 438 | wxChar *wxBuffer = new wxChar[256]; |
06298235 | 439 | #ifndef __EMX__ |
27b2dd53 | 440 | if (!sUser1.empty()) |
45fcbf3b | 441 | { |
1be7f92a DW |
442 | wxChar zTmp[64]; |
443 | ||
444 | if (wxGetUserId( zTmp | |
445 | ,sizeof(zTmp)/sizeof(char) | |
446 | )) | |
447 | { | |
448 | // Guests belong in the temp dir | |
449 | if (wxStricmp(zTmp, _T("annonymous")) == 0) | |
450 | { | |
451 | if ((zHome = wxGetenv(_T("TMP"))) != NULL || | |
452 | (zHome = wxGetenv(_T("TMPDIR"))) != NULL || | |
453 | (zHome = wxGetenv(_T("TEMP"))) != NULL) | |
13a4ea8d | 454 | delete[] wxBuffer; |
1be7f92a DW |
455 | return *zHome ? zHome : (wxChar*)_T("\\"); |
456 | } | |
457 | if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0) | |
458 | sUser1 = _T(""); | |
459 | } | |
45fcbf3b | 460 | } |
06298235 | 461 | #endif |
27b2dd53 | 462 | if (sUser1.empty()) |
13a4ea8d | 463 | { |
1be7f92a DW |
464 | if ((zHome = wxGetenv(_T("HOME"))) != NULL) |
465 | { | |
466 | wxStrcpy(wxBuffer, zHome); | |
2b5f62a0 | 467 | wxUnix2DosFilename(wxBuffer); |
13a4ea8d DW |
468 | wxStrcpy(zHome, wxBuffer); |
469 | delete[] wxBuffer; | |
470 | return zHome; | |
1be7f92a | 471 | } |
13a4ea8d DW |
472 | } |
473 | delete[] wxBuffer; | |
0fba44b4 | 474 | return (wxChar*)wxEmptyString; // No home known! |
0e320a79 DW |
475 | } |
476 | ||
914589c2 DW |
477 | wxString WXDLLEXPORT wxPMErrorToStr( |
478 | ERRORID vError | |
479 | ) | |
480 | { | |
481 | wxString sError; | |
482 | ||
483 | // | |
484 | // Remove the high order byte -- it is useless | |
485 | // | |
486 | vError &= 0x0000ffff; | |
487 | switch(vError) | |
488 | { | |
489 | case PMERR_INVALID_HWND: | |
490 | sError = wxT("Invalid window handle specified"); | |
491 | break; | |
492 | ||
493 | case PMERR_INVALID_FLAG: | |
494 | sError = wxT("Invalid flag bit set"); | |
495 | break; | |
496 | ||
497 | case PMERR_NO_MSG_QUEUE: | |
498 | sError = wxT("No message queue available"); | |
499 | break; | |
500 | ||
501 | case PMERR_INVALID_PARM: | |
502 | sError = wxT("Parameter contained invalid data"); | |
503 | break; | |
504 | ||
505 | case PMERR_INVALID_PARAMETERS: | |
506 | sError = wxT("Parameter value is out of range"); | |
507 | break; | |
508 | ||
509 | case PMERR_PARAMETER_OUT_OF_RANGE: | |
510 | sError = wxT("Parameter value is out of range"); | |
511 | break; | |
512 | ||
513 | case PMERR_INVALID_INTEGER_ATOM: | |
514 | sError = wxT("Not a valid atom"); | |
515 | break; | |
516 | ||
517 | case PMERR_INVALID_HATOMTBL: | |
518 | sError = wxT("Atom table handle is invalid"); | |
519 | break; | |
520 | ||
521 | case PMERR_INVALID_ATOM_NAME: | |
522 | sError = wxT("Not a valid atom name"); | |
523 | break; | |
524 | ||
525 | case PMERR_ATOM_NAME_NOT_FOUND: | |
526 | sError = wxT("Valid name format, but cannot find name in atom table"); | |
527 | break; | |
528 | ||
529 | default: | |
530 | sError = wxT("Unknown error"); | |
531 | } | |
532 | return(sError); | |
533 | } // end of wxPMErrorToStr | |
621ccd8a SN |
534 | |
535 | // replacement for implementation in unix/utilsunx.cpp, | |
536 | // to be used by all X11 based ports. | |
537 | struct wxEndProcessData; | |
538 | ||
539 | void wxHandleProcessTermination(wxEndProcessData *proc_data) | |
540 | { | |
541 | // For now, just do nothing. To be filled in as needed. | |
542 | } |