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