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