]>
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 | 47 | { |
9a83f860 VZ |
48 | wxT("Apple Mac OS"), |
49 | wxT("Apple Mac OS X"), | |
8bb6b2c0 | 50 | |
9a83f860 VZ |
51 | wxT("Microsoft Windows 9X"), |
52 | wxT("Microsoft Windows NT"), | |
53 | wxT("Microsoft Windows Micro"), | |
54 | wxT("Microsoft Windows CE"), | |
8bb6b2c0 | 55 | |
9a83f860 VZ |
56 | wxT("Linux"), |
57 | wxT("FreeBSD"), | |
58 | wxT("OpenBSD"), | |
59 | wxT("NetBSD"), | |
8bb6b2c0 | 60 | |
9a83f860 VZ |
61 | wxT("SunOS"), |
62 | wxT("AIX"), | |
63 | wxT("HPUX"), | |
8bb6b2c0 | 64 | |
9a83f860 VZ |
65 | wxT("Other Unix"), |
66 | wxT("Other Unix"), | |
7528971e | 67 | |
9a83f860 VZ |
68 | wxT("DOS"), |
69 | wxT("OS/2"), | |
027fe16c | 70 | |
9a83f860 VZ |
71 | wxT("PalmOS"), |
72 | wxT("PalmOS(Over Linux)"), | |
8bb6b2c0 VZ |
73 | }; |
74 | ||
8f493187 | 75 | static const wxChar* const wxPortIdNames[] = |
8bb6b2c0 | 76 | { |
9a83f860 VZ |
77 | wxT("wxBase"), |
78 | wxT("wxMSW"), | |
79 | wxT("wxMotif"), | |
80 | wxT("wxGTK"), | |
81 | wxT("wxMGL"), | |
82 | wxT("wxX11"), | |
83 | wxT("wxOS2"), | |
84 | wxT("wxMac"), | |
85 | wxT("wxCocoa"), | |
86 | wxT("wxWinCE"), | |
87 | wxT("wxPalmOS"), | |
88 | wxT("wxDFB") | |
8bb6b2c0 VZ |
89 | }; |
90 | ||
8f493187 | 91 | static const wxChar* const wxArchitectureNames[] = |
8bb6b2c0 | 92 | { |
9a83f860 VZ |
93 | wxT("32 bit"), |
94 | wxT("64 bit") | |
8bb6b2c0 VZ |
95 | }; |
96 | ||
8f493187 | 97 | static const wxChar* const wxEndiannessNames[] = |
8bb6b2c0 | 98 | { |
9a83f860 VZ |
99 | wxT("Big endian"), |
100 | wxT("Little endian"), | |
101 | wxT("PDP endian") | |
8bb6b2c0 VZ |
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 | { |
9a83f860 | 112 | wxCHECK_MSG( value, (unsigned)-1, wxT("invalid enum value") ); |
8bb6b2c0 VZ |
113 | |
114 | int n = 0; | |
115 | while ( !(value & 1) ) | |
116 | { | |
117 | value >>= 1; | |
118 | n++; | |
119 | } | |
120 | ||
9a83f860 | 121 | wxASSERT_MSG( value == 1, wxT("more than one bit set in enum value") ); |
8bb6b2c0 VZ |
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 | { | |
9a83f860 | 177 | wxFAIL_MSG( wxT("failed to initialize wxPlatformInfo") ); |
449090b5 VZ |
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; | |
03647350 | 195 | |
23790a2a FM |
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 | { | |
9a83f860 | 229 | const wxChar* string = wxT("Unknown"); |
8bb6b2c0 | 230 | if ( os & wxOS_MAC ) |
9a83f860 | 231 | string = wxT("Macintosh"); |
8bb6b2c0 | 232 | else if ( os & wxOS_WINDOWS ) |
9a83f860 | 233 | string = wxT("Windows"); |
8bb6b2c0 | 234 | else if ( os & wxOS_UNIX ) |
9a83f860 | 235 | string = wxT("Unix"); |
8bb6b2c0 | 236 | else if ( os == wxOS_DOS ) |
9a83f860 | 237 | string = wxT("DOS"); |
8bb6b2c0 | 238 | else if ( os == wxOS_OS2 ) |
9a83f860 | 239 | string = wxT("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, | |
9a83f860 | 249 | wxT("invalid OS id") ); |
8bb6b2c0 VZ |
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, | |
9a83f860 | 259 | wxT("invalid port id") ); |
8bb6b2c0 VZ |
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, | |
9a83f860 | 274 | wxT("invalid port id") ); |
8bb6b2c0 VZ |
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 |