1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/platinfo.cpp
3 // Purpose: implements wxPlatformInfo class
4 // Author: Francesco Montorsi
6 // Created: 07.07.2006 (based on wxToolkitInfo)
7 // Copyright: (c) 2006 Francesco Montorsi
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/platinfo.h"
33 #include "wx/apptrait.h"
36 // VERY IMPORTANT: do not use the default constructor since it would
37 // try to init the wxPlatformInfo instance using
38 // gs_platInfo itself!
39 static wxPlatformInfo
gs_platInfo(wxPORT_UNKNOWN
);
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 static const wxChar
* const wxOperatingSystemIdNames
[] =
48 wxT("Apple Mac OS X"),
50 wxT("Microsoft Windows 9X"),
51 wxT("Microsoft Windows NT"),
52 wxT("Microsoft Windows Micro"),
53 wxT("Microsoft Windows CE"),
71 static const wxChar
* const wxPortIdNames
[] =
85 static const wxChar
* const wxArchitectureNames
[] =
91 static const wxChar
* const wxEndiannessNames
[] =
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // returns the logarithm in base 2 of 'value'; this maps the enum values to the
103 // corresponding indexes of the string arrays above
104 static unsigned wxGetIndexFromEnumValue(int value
)
106 wxCHECK_MSG( value
, (unsigned)-1, wxT("invalid enum value") );
109 while ( !(value
& 1) )
115 wxASSERT_MSG( value
== 1, wxT("more than one bit set in enum value") );
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 wxPlatformInfo::wxPlatformInfo()
126 // just copy platform info for currently running platform
130 wxPlatformInfo::wxPlatformInfo(wxPortId pid
, int tkMajor
, int tkMinor
,
131 wxOperatingSystemId id
, int osMajor
, int osMinor
,
136 m_tkVersionMajor
= tkMajor
;
137 m_tkVersionMinor
= tkMinor
;
139 m_usingUniversal
= usingUniversal
;
142 m_osVersionMajor
= osMajor
;
143 m_osVersionMinor
= osMinor
;
149 bool wxPlatformInfo::operator==(const wxPlatformInfo
&t
) const
151 return m_tkVersionMajor
== t
.m_tkVersionMajor
&&
152 m_tkVersionMinor
== t
.m_tkVersionMinor
&&
153 m_osVersionMajor
== t
.m_osVersionMajor
&&
154 m_osVersionMinor
== t
.m_osVersionMinor
&&
156 m_osDesc
== t
.m_osDesc
&&
158 m_desktopEnv
== t
.m_desktopEnv
&&
159 m_port
== t
.m_port
&&
160 m_usingUniversal
== t
.m_usingUniversal
&&
161 m_arch
== t
.m_arch
&&
162 m_endian
== t
.m_endian
;
165 void wxPlatformInfo::InitForCurrentPlatform()
167 // autodetect all informations
168 const wxAppTraits
* const traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
171 wxFAIL_MSG( wxT("failed to initialize wxPlatformInfo") );
173 m_port
= wxPORT_UNKNOWN
;
174 m_usingUniversal
= false;
176 m_tkVersionMinor
= 0;
180 m_port
= traits
->GetToolkitVersion(&m_tkVersionMajor
, &m_tkVersionMinor
);
181 m_usingUniversal
= traits
->IsUsingUniversalWidgets();
182 m_desktopEnv
= traits
->GetDesktopEnvironment();
185 m_os
= wxGetOsVersion(&m_osVersionMajor
, &m_osVersionMinor
);
186 m_osDesc
= wxGetOsDescription();
187 m_endian
= wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE
: wxENDIAN_BIG
;
188 m_arch
= wxIsPlatform64Bit() ? wxARCH_64
: wxARCH_32
;
191 m_ldi
= wxGetLinuxDistributionInfo();
193 // else: leave m_ldi empty
197 const wxPlatformInfo
& wxPlatformInfo::Get()
199 static bool initialized
= false;
202 gs_platInfo
.InitForCurrentPlatform();
210 wxString
wxPlatformInfo::GetOperatingSystemDirectory()
212 return wxGetOSDirectory();
217 // ----------------------------------------------------------------------------
218 // wxPlatformInfo - enum -> string conversions
219 // ----------------------------------------------------------------------------
221 wxString
wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os
)
223 const wxChar
* string
= wxT("Unknown");
225 string
= wxT("Macintosh");
226 else if ( os
& wxOS_WINDOWS
)
227 string
= wxT("Windows");
228 else if ( os
& wxOS_UNIX
)
229 string
= wxT("Unix");
230 else if ( os
== wxOS_DOS
)
232 else if ( os
== wxOS_OS2
)
233 string
= wxT("OS/2");
238 wxString
wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os
)
240 const unsigned idx
= wxGetIndexFromEnumValue(os
);
242 wxCHECK_MSG( idx
< WXSIZEOF(wxOperatingSystemIdNames
), wxEmptyString
,
243 wxT("invalid OS id") );
245 return wxOperatingSystemIdNames
[idx
];
248 wxString
wxPlatformInfo::GetPortIdName(wxPortId port
, bool usingUniversal
)
250 const unsigned idx
= wxGetIndexFromEnumValue(port
);
252 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
253 wxT("invalid port id") );
255 wxString ret
= wxPortIdNames
[idx
];
257 if ( usingUniversal
)
258 ret
+= wxT("/wxUniversal");
263 wxString
wxPlatformInfo::GetPortIdShortName(wxPortId port
, bool usingUniversal
)
265 const unsigned idx
= wxGetIndexFromEnumValue(port
);
267 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
268 wxT("invalid port id") );
270 wxString ret
= wxPortIdNames
[idx
];
271 ret
= ret
.Mid(2).Lower(); // remove 'wx' prefix
273 if ( usingUniversal
)
279 wxString
wxPlatformInfo::GetArchName(wxArchitecture arch
)
281 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames
) == wxARCH_MAX
,
282 wxArchitectureNamesMismatch
);
284 return wxArchitectureNames
[arch
];
287 wxString
wxPlatformInfo::GetEndiannessName(wxEndianness end
)
289 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames
) == wxENDIAN_MAX
,
290 wxEndiannessNamesMismatch
);
292 return wxEndiannessNames
[end
];
296 // ----------------------------------------------------------------------------
297 // wxPlatformInfo - string -> enum conversions
298 // ----------------------------------------------------------------------------
300 wxOperatingSystemId
wxPlatformInfo::GetOperatingSystemId(const wxString
&str
)
302 for ( size_t i
= 0; i
< WXSIZEOF(wxOperatingSystemIdNames
); i
++ )
304 if ( wxString(wxOperatingSystemIdNames
[i
]).CmpNoCase(str
) == 0 )
305 return (wxOperatingSystemId
)(1 << i
);
311 wxPortId
wxPlatformInfo::GetPortId(const wxString
&str
)
313 // recognize both short and long port names
314 for ( size_t i
= 0; i
< WXSIZEOF(wxPortIdNames
); i
++ )
316 wxPortId current
= (wxPortId
)(1 << i
);
318 if ( wxString(wxPortIdNames
[i
]).CmpNoCase(str
) == 0 ||
319 GetPortIdShortName(current
, true).CmpNoCase(str
) == 0 ||
320 GetPortIdShortName(current
, false).CmpNoCase(str
) == 0 )
324 return wxPORT_UNKNOWN
;
327 wxArchitecture
wxPlatformInfo::GetArch(const wxString
&arch
)
329 if ( arch
.Contains(wxT("32")) )
332 if ( arch
.Contains(wxT("64")) )
335 return wxARCH_INVALID
;
338 wxEndianness
wxPlatformInfo::GetEndianness(const wxString
& end
)
340 const wxString
endl(end
.Lower());
341 if ( endl
.StartsWith(wxT("little")) )
342 return wxENDIAN_LITTLE
;
344 if ( endl
.StartsWith(wxT("big")) )
347 return wxENDIAN_INVALID
;