]>
Commit | Line | Data |
---|---|---|
8bb6b2c0 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/platinfo.cpp | |
3 | // Purpose: implements wxPlatformInfo class | |
4 | // Author: Francesco Montorsi | |
5 | // Modified by: | |
6 | // Created: 07.07.2006 (based on wxToolkitInfo) | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2006 Francesco Montorsi | |
9 | // License: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
f566c8e2 PC |
27 | #include "wx/platinfo.h" |
28 | ||
8bb6b2c0 | 29 | #ifndef WX_PRECOMP |
f566c8e2 | 30 | #include "wx/app.h" |
8bb6b2c0 VZ |
31 | #include "wx/utils.h" |
32 | #endif //WX_PRECOMP | |
33 | ||
8bb6b2c0 VZ |
34 | #include "wx/apptrait.h" |
35 | ||
449090b5 VZ |
36 | // global object |
37 | // VERY IMPORTANT: do not use the default constructor since it would | |
38 | // try to init the wxPlatformInfo instance using | |
39 | // gs_platInfo itself! | |
40 | static wxPlatformInfo gs_platInfo(wxPORT_UNKNOWN); | |
41 | ||
8bb6b2c0 VZ |
42 | // ---------------------------------------------------------------------------- |
43 | // constants | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
8f493187 | 46 | static const wxChar* const wxOperatingSystemIdNames[] = |
8bb6b2c0 VZ |
47 | { |
48 | _T("Apple Mac OS"), | |
49 | _T("Apple Mac OS X"), | |
50 | ||
51 | _T("Microsoft Windows 9X"), | |
52 | _T("Microsoft Windows NT"), | |
53 | _T("Microsoft Windows Micro"), | |
54 | _T("Microsoft Windows CE"), | |
55 | ||
56 | _T("Linux"), | |
57 | _T("FreeBSD"), | |
58 | _T("OpenBSD"), | |
59 | _T("NetBSD"), | |
60 | ||
61 | _T("SunOS"), | |
62 | _T("AIX"), | |
63 | _T("HPUX"), | |
64 | ||
7528971e SN |
65 | _T("Other Unix"), |
66 | _T("Other Unix"), | |
67 | ||
8bb6b2c0 | 68 | _T("DOS"), |
027fe16c VZ |
69 | _T("OS/2"), |
70 | ||
71 | _T("PalmOS"), | |
72 | _T("PalmOS(Over Linux)"), | |
8bb6b2c0 VZ |
73 | }; |
74 | ||
8f493187 | 75 | static const wxChar* const wxPortIdNames[] = |
8bb6b2c0 VZ |
76 | { |
77 | _T("wxBase"), | |
78 | _T("wxMSW"), | |
79 | _T("wxMotif"), | |
80 | _T("wxGTK"), | |
81 | _T("wxMGL"), | |
82 | _T("wxX11"), | |
83 | _T("wxOS2"), | |
84 | _T("wxMac"), | |
85 | _T("wxCocoa"), | |
86 | _T("wxWinCE"), | |
b3c86150 VS |
87 | _T("wxPalmOS"), |
88 | _T("wxDFB") | |
8bb6b2c0 VZ |
89 | }; |
90 | ||
8f493187 | 91 | static const wxChar* const wxArchitectureNames[] = |
8bb6b2c0 VZ |
92 | { |
93 | _T("32 bit"), | |
94 | _T("64 bit") | |
95 | }; | |
96 | ||
8f493187 | 97 | static const wxChar* const wxEndiannessNames[] = |
8bb6b2c0 VZ |
98 | { |
99 | _T("Big endian"), | |
100 | _T("Little endian"), | |
101 | _T("PDP endian") | |
102 | }; | |
103 | ||
104 | // ---------------------------------------------------------------------------- | |
105 | // local functions | |
106 | // ---------------------------------------------------------------------------- | |
107 | ||
23790a2a FM |
108 | // returns the logarithm in base 2 of 'value'; this maps the enum values to the |
109 | // corresponding indexes of the string arrays above | |
78e9b418 | 110 | static unsigned wxGetIndexFromEnumValue(int value) |
8bb6b2c0 | 111 | { |
78e9b418 | 112 | wxCHECK_MSG( value, (unsigned)-1, _T("invalid enum value") ); |
8bb6b2c0 VZ |
113 | |
114 | int n = 0; | |
115 | while ( !(value & 1) ) | |
116 | { | |
117 | value >>= 1; | |
118 | n++; | |
119 | } | |
120 | ||
121 | wxASSERT_MSG( value == 1, _T("more than one bit set in enum value") ); | |
122 | ||
123 | return n; | |
124 | } | |
125 | ||
126 | // ---------------------------------------------------------------------------- | |
127 | // wxPlatformInfo | |
128 | // ---------------------------------------------------------------------------- | |
129 | ||
130 | wxPlatformInfo::wxPlatformInfo() | |
131 | { | |
449090b5 VZ |
132 | // just copy platform info for currently running platform |
133 | *this = Get(); | |
8bb6b2c0 VZ |
134 | } |
135 | ||
136 | wxPlatformInfo::wxPlatformInfo(wxPortId pid, int tkMajor, int tkMinor, | |
137 | wxOperatingSystemId id, int osMajor, int osMinor, | |
138 | wxArchitecture arch, | |
b98bd6af VS |
139 | wxEndianness endian, |
140 | bool usingUniversal) | |
8bb6b2c0 VZ |
141 | { |
142 | m_tkVersionMajor = tkMajor; | |
143 | m_tkVersionMinor = tkMinor; | |
144 | m_port = pid; | |
b98bd6af | 145 | m_usingUniversal = usingUniversal; |
8bb6b2c0 VZ |
146 | |
147 | m_os = id; | |
148 | m_osVersionMajor = osMajor; | |
149 | m_osVersionMinor = osMinor; | |
150 | ||
151 | m_endian = endian; | |
152 | m_arch = arch; | |
153 | } | |
154 | ||
155 | bool wxPlatformInfo::operator==(const wxPlatformInfo &t) const | |
156 | { | |
157 | return m_tkVersionMajor == t.m_tkVersionMajor && | |
158 | m_tkVersionMinor == t.m_tkVersionMinor && | |
159 | m_osVersionMajor == t.m_osVersionMajor && | |
160 | m_osVersionMinor == t.m_osVersionMinor && | |
161 | m_os == t.m_os && | |
23790a2a FM |
162 | m_osDesc == t.m_osDesc && |
163 | m_ldi == t.m_ldi && | |
164 | m_desktopEnv == t.m_desktopEnv && | |
8bb6b2c0 | 165 | m_port == t.m_port && |
b98bd6af | 166 | m_usingUniversal == t.m_usingUniversal && |
8bb6b2c0 VZ |
167 | m_arch == t.m_arch && |
168 | m_endian == t.m_endian; | |
169 | } | |
170 | ||
449090b5 VZ |
171 | void wxPlatformInfo::InitForCurrentPlatform() |
172 | { | |
173 | // autodetect all informations | |
174 | const wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
175 | if ( !traits ) | |
176 | { | |
177 | wxFAIL_MSG( _T("failed to initialize wxPlatformInfo") ); | |
178 | ||
179 | m_port = wxPORT_UNKNOWN; | |
180 | m_usingUniversal = false; | |
181 | m_tkVersionMajor = | |
182 | m_tkVersionMinor = 0; | |
183 | } | |
184 | else | |
185 | { | |
186 | m_port = traits->GetToolkitVersion(&m_tkVersionMajor, &m_tkVersionMinor); | |
187 | m_usingUniversal = traits->IsUsingUniversalWidgets(); | |
23790a2a | 188 | m_desktopEnv = traits->GetDesktopEnvironment(); |
449090b5 VZ |
189 | } |
190 | ||
191 | m_os = wxGetOsVersion(&m_osVersionMajor, &m_osVersionMinor); | |
23790a2a | 192 | m_osDesc = wxGetOsDescription(); |
449090b5 VZ |
193 | m_endian = wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE : wxENDIAN_BIG; |
194 | m_arch = wxIsPlatform64Bit() ? wxARCH_64 : wxARCH_32; | |
23790a2a FM |
195 | |
196 | #ifdef __LINUX__ | |
197 | m_ldi = wxGetLinuxDistributionInfo(); | |
198 | #endif | |
199 | // else: leave m_ldi empty | |
449090b5 VZ |
200 | } |
201 | ||
202 | /* static */ | |
203 | const wxPlatformInfo& wxPlatformInfo::Get() | |
204 | { | |
205 | static bool initialized = false; | |
206 | if ( !initialized ) | |
207 | { | |
208 | gs_platInfo.InitForCurrentPlatform(); | |
209 | initialized = true; | |
210 | } | |
211 | ||
212 | return gs_platInfo; | |
213 | } | |
214 | ||
23790a2a FM |
215 | /* static */ |
216 | wxString wxPlatformInfo::GetOperatingSystemDirectory() | |
217 | { | |
218 | return wxGetOSDirectory(); | |
219 | } | |
220 | ||
449090b5 VZ |
221 | |
222 | ||
8bb6b2c0 VZ |
223 | // ---------------------------------------------------------------------------- |
224 | // wxPlatformInfo - enum -> string conversions | |
225 | // ---------------------------------------------------------------------------- | |
226 | ||
227 | wxString wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os) | |
228 | { | |
8f493187 | 229 | const wxChar* string = _T("Unknown"); |
8bb6b2c0 | 230 | if ( os & wxOS_MAC ) |
8f493187 | 231 | string = _T("Macintosh"); |
8bb6b2c0 | 232 | else if ( os & wxOS_WINDOWS ) |
8f493187 | 233 | string = _T("Windows"); |
8bb6b2c0 | 234 | else if ( os & wxOS_UNIX ) |
8f493187 | 235 | string = _T("Unix"); |
8bb6b2c0 | 236 | else if ( os == wxOS_DOS ) |
8f493187 | 237 | string = _T("DOS"); |
8bb6b2c0 | 238 | else if ( os == wxOS_OS2 ) |
8f493187 | 239 | string = _T("OS/2"); |
8bb6b2c0 | 240 | |
8f493187 | 241 | return string; |
8bb6b2c0 VZ |
242 | } |
243 | ||
244 | wxString wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os) | |
245 | { | |
78e9b418 | 246 | const unsigned idx = wxGetIndexFromEnumValue(os); |
8bb6b2c0 VZ |
247 | |
248 | wxCHECK_MSG( idx < WXSIZEOF(wxOperatingSystemIdNames), wxEmptyString, | |
249 | _T("invalid OS id") ); | |
250 | ||
251 | return wxOperatingSystemIdNames[idx]; | |
252 | } | |
253 | ||
b98bd6af | 254 | wxString wxPlatformInfo::GetPortIdName(wxPortId port, bool usingUniversal) |
8bb6b2c0 | 255 | { |
78e9b418 | 256 | const unsigned idx = wxGetIndexFromEnumValue(port); |
8bb6b2c0 VZ |
257 | |
258 | wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString, | |
259 | _T("invalid port id") ); | |
260 | ||
261 | wxString ret = wxPortIdNames[idx]; | |
262 | ||
b98bd6af | 263 | if ( usingUniversal ) |
8bb6b2c0 VZ |
264 | ret += wxT("/wxUniversal"); |
265 | ||
266 | return ret; | |
267 | } | |
268 | ||
b98bd6af | 269 | wxString wxPlatformInfo::GetPortIdShortName(wxPortId port, bool usingUniversal) |
8bb6b2c0 | 270 | { |
78e9b418 | 271 | const unsigned idx = wxGetIndexFromEnumValue(port); |
8bb6b2c0 VZ |
272 | |
273 | wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString, | |
274 | _T("invalid port id") ); | |
275 | ||
276 | wxString ret = wxPortIdNames[idx]; | |
277 | ret = ret.Mid(2).Lower(); // remove 'wx' prefix | |
278 | ||
b98bd6af | 279 | if ( usingUniversal ) |
8bb6b2c0 VZ |
280 | ret += wxT("univ"); |
281 | ||
282 | return ret; | |
283 | } | |
284 | ||
285 | wxString wxPlatformInfo::GetArchName(wxArchitecture arch) | |
286 | { | |
287 | wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames) == wxARCH_MAX, | |
288 | wxArchitectureNamesMismatch ); | |
289 | ||
290 | return wxArchitectureNames[arch]; | |
291 | } | |
292 | ||
293 | wxString wxPlatformInfo::GetEndiannessName(wxEndianness end) | |
294 | { | |
295 | wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames) == wxENDIAN_MAX, | |
296 | wxEndiannessNamesMismatch ); | |
297 | ||
298 | return wxEndiannessNames[end]; | |
299 | } | |
300 | ||
301 | ||
302 | // ---------------------------------------------------------------------------- | |
303 | // wxPlatformInfo - string -> enum conversions | |
304 | // ---------------------------------------------------------------------------- | |
305 | ||
306 | wxOperatingSystemId wxPlatformInfo::GetOperatingSystemId(const wxString &str) | |
307 | { | |
308 | for ( size_t i = 0; i < WXSIZEOF(wxOperatingSystemIdNames); i++ ) | |
309 | { | |
8f493187 | 310 | if ( wxString(wxOperatingSystemIdNames[i]).CmpNoCase(str) == 0 ) |
8bb6b2c0 VZ |
311 | return (wxOperatingSystemId)(1 << i); |
312 | } | |
313 | ||
314 | return wxOS_UNKNOWN; | |
315 | } | |
316 | ||
317 | wxPortId wxPlatformInfo::GetPortId(const wxString &str) | |
318 | { | |
319 | // recognize both short and long port names | |
320 | for ( size_t i = 0; i < WXSIZEOF(wxPortIdNames); i++ ) | |
321 | { | |
322 | wxPortId current = (wxPortId)(1 << i); | |
323 | ||
8f493187 PC |
324 | if ( wxString(wxPortIdNames[i]).CmpNoCase(str) == 0 || |
325 | GetPortIdShortName(current, true).CmpNoCase(str) == 0 || | |
b98bd6af | 326 | GetPortIdShortName(current, false).CmpNoCase(str) == 0 ) |
8bb6b2c0 VZ |
327 | return current; |
328 | } | |
329 | ||
330 | return wxPORT_UNKNOWN; | |
331 | } | |
332 | ||
333 | wxArchitecture wxPlatformInfo::GetArch(const wxString &arch) | |
334 | { | |
335 | if ( arch.Contains(wxT("32")) ) | |
336 | return wxARCH_32; | |
337 | ||
338 | if ( arch.Contains(wxT("64")) ) | |
339 | return wxARCH_64; | |
340 | ||
341 | return wxARCH_INVALID; | |
342 | } | |
343 | ||
344 | wxEndianness wxPlatformInfo::GetEndianness(const wxString& end) | |
345 | { | |
523a54d9 VZ |
346 | const wxString endl(end.Lower()); |
347 | if ( endl.StartsWith(wxT("little")) ) | |
8bb6b2c0 VZ |
348 | return wxENDIAN_LITTLE; |
349 | ||
523a54d9 | 350 | if ( endl.StartsWith(wxT("big")) ) |
8bb6b2c0 VZ |
351 | return wxENDIAN_BIG; |
352 | ||
353 | return wxENDIAN_INVALID; | |
354 | } | |
355 |