1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/platinfo.cpp
3 // Purpose: implements wxPlatformInfo class
4 // Author: Francesco Montorsi
6 // Created: 07.07.2006 (based on wxToolkitInfo)
8 // Copyright: (c) 2006 Francesco Montorsi
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/platinfo.h"
34 #include "wx/apptrait.h"
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
);
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 static const wxChar
* const wxOperatingSystemIdNames
[] =
49 wxT("Apple Mac OS X"),
51 wxT("Microsoft Windows 9X"),
52 wxT("Microsoft Windows NT"),
53 wxT("Microsoft Windows Micro"),
54 wxT("Microsoft Windows CE"),
72 static const wxChar
* const wxPortIdNames
[] =
87 static const wxChar
* const wxArchitectureNames
[] =
93 static const wxChar
* const wxEndiannessNames
[] =
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 // returns the logarithm in base 2 of 'value'; this maps the enum values to the
105 // corresponding indexes of the string arrays above
106 static unsigned wxGetIndexFromEnumValue(int value
)
108 wxCHECK_MSG( value
, (unsigned)-1, wxT("invalid enum value") );
111 while ( !(value
& 1) )
117 wxASSERT_MSG( value
== 1, wxT("more than one bit set in enum value") );
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 wxPlatformInfo::wxPlatformInfo()
128 // just copy platform info for currently running platform
132 wxPlatformInfo::wxPlatformInfo(wxPortId pid
, int tkMajor
, int tkMinor
,
133 wxOperatingSystemId id
, int osMajor
, int osMinor
,
138 m_tkVersionMajor
= tkMajor
;
139 m_tkVersionMinor
= tkMinor
;
141 m_usingUniversal
= usingUniversal
;
144 m_osVersionMajor
= osMajor
;
145 m_osVersionMinor
= osMinor
;
151 bool wxPlatformInfo::operator==(const wxPlatformInfo
&t
) const
153 return m_tkVersionMajor
== t
.m_tkVersionMajor
&&
154 m_tkVersionMinor
== t
.m_tkVersionMinor
&&
155 m_osVersionMajor
== t
.m_osVersionMajor
&&
156 m_osVersionMinor
== t
.m_osVersionMinor
&&
158 m_osDesc
== t
.m_osDesc
&&
160 m_desktopEnv
== t
.m_desktopEnv
&&
161 m_port
== t
.m_port
&&
162 m_usingUniversal
== t
.m_usingUniversal
&&
163 m_arch
== t
.m_arch
&&
164 m_endian
== t
.m_endian
;
167 void wxPlatformInfo::InitForCurrentPlatform()
169 // autodetect all informations
170 const wxAppTraits
* const traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
173 wxFAIL_MSG( wxT("failed to initialize wxPlatformInfo") );
175 m_port
= wxPORT_UNKNOWN
;
176 m_usingUniversal
= false;
178 m_tkVersionMinor
= 0;
182 m_port
= traits
->GetToolkitVersion(&m_tkVersionMajor
, &m_tkVersionMinor
);
183 m_usingUniversal
= traits
->IsUsingUniversalWidgets();
184 m_desktopEnv
= traits
->GetDesktopEnvironment();
187 m_os
= wxGetOsVersion(&m_osVersionMajor
, &m_osVersionMinor
);
188 m_osDesc
= wxGetOsDescription();
189 m_endian
= wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE
: wxENDIAN_BIG
;
190 m_arch
= wxIsPlatform64Bit() ? wxARCH_64
: wxARCH_32
;
193 m_ldi
= wxGetLinuxDistributionInfo();
195 // else: leave m_ldi empty
199 const wxPlatformInfo
& wxPlatformInfo::Get()
201 static bool initialized
= false;
204 gs_platInfo
.InitForCurrentPlatform();
212 wxString
wxPlatformInfo::GetOperatingSystemDirectory()
214 return wxGetOSDirectory();
219 // ----------------------------------------------------------------------------
220 // wxPlatformInfo - enum -> string conversions
221 // ----------------------------------------------------------------------------
223 wxString
wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os
)
225 const wxChar
* string
= wxT("Unknown");
227 string
= wxT("Macintosh");
228 else if ( os
& wxOS_WINDOWS
)
229 string
= wxT("Windows");
230 else if ( os
& wxOS_UNIX
)
231 string
= wxT("Unix");
232 else if ( os
== wxOS_DOS
)
234 else if ( os
== wxOS_OS2
)
235 string
= wxT("OS/2");
240 wxString
wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os
)
242 const unsigned idx
= wxGetIndexFromEnumValue(os
);
244 wxCHECK_MSG( idx
< WXSIZEOF(wxOperatingSystemIdNames
), wxEmptyString
,
245 wxT("invalid OS id") );
247 return wxOperatingSystemIdNames
[idx
];
250 wxString
wxPlatformInfo::GetPortIdName(wxPortId port
, bool usingUniversal
)
252 const unsigned idx
= wxGetIndexFromEnumValue(port
);
254 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
255 wxT("invalid port id") );
257 wxString ret
= wxPortIdNames
[idx
];
259 if ( usingUniversal
)
260 ret
+= wxT("/wxUniversal");
265 wxString
wxPlatformInfo::GetPortIdShortName(wxPortId port
, bool usingUniversal
)
267 const unsigned idx
= wxGetIndexFromEnumValue(port
);
269 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
270 wxT("invalid port id") );
272 wxString ret
= wxPortIdNames
[idx
];
273 ret
= ret
.Mid(2).Lower(); // remove 'wx' prefix
275 if ( usingUniversal
)
281 wxString
wxPlatformInfo::GetArchName(wxArchitecture arch
)
283 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames
) == wxARCH_MAX
,
284 wxArchitectureNamesMismatch
);
286 return wxArchitectureNames
[arch
];
289 wxString
wxPlatformInfo::GetEndiannessName(wxEndianness end
)
291 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames
) == wxENDIAN_MAX
,
292 wxEndiannessNamesMismatch
);
294 return wxEndiannessNames
[end
];
298 // ----------------------------------------------------------------------------
299 // wxPlatformInfo - string -> enum conversions
300 // ----------------------------------------------------------------------------
302 wxOperatingSystemId
wxPlatformInfo::GetOperatingSystemId(const wxString
&str
)
304 for ( size_t i
= 0; i
< WXSIZEOF(wxOperatingSystemIdNames
); i
++ )
306 if ( wxString(wxOperatingSystemIdNames
[i
]).CmpNoCase(str
) == 0 )
307 return (wxOperatingSystemId
)(1 << i
);
313 wxPortId
wxPlatformInfo::GetPortId(const wxString
&str
)
315 // recognize both short and long port names
316 for ( size_t i
= 0; i
< WXSIZEOF(wxPortIdNames
); i
++ )
318 wxPortId current
= (wxPortId
)(1 << i
);
320 if ( wxString(wxPortIdNames
[i
]).CmpNoCase(str
) == 0 ||
321 GetPortIdShortName(current
, true).CmpNoCase(str
) == 0 ||
322 GetPortIdShortName(current
, false).CmpNoCase(str
) == 0 )
326 return wxPORT_UNKNOWN
;
329 wxArchitecture
wxPlatformInfo::GetArch(const wxString
&arch
)
331 if ( arch
.Contains(wxT("32")) )
334 if ( arch
.Contains(wxT("64")) )
337 return wxARCH_INVALID
;
340 wxEndianness
wxPlatformInfo::GetEndianness(const wxString
& end
)
342 const wxString
endl(end
.Lower());
343 if ( endl
.StartsWith(wxT("little")) )
344 return wxENDIAN_LITTLE
;
346 if ( endl
.StartsWith(wxT("big")) )
349 return wxENDIAN_INVALID
;