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 // License: wxWindows license
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 wxT("PalmOS(Over Linux)"),
75 static const wxChar
* const wxPortIdNames
[] =
91 static const wxChar
* const wxArchitectureNames
[] =
97 static const wxChar
* const wxEndiannessNames
[] =
100 wxT("Little endian"),
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 // returns the logarithm in base 2 of 'value'; this maps the enum values to the
109 // corresponding indexes of the string arrays above
110 static unsigned wxGetIndexFromEnumValue(int value
)
112 wxCHECK_MSG( value
, (unsigned)-1, wxT("invalid enum value") );
115 while ( !(value
& 1) )
121 wxASSERT_MSG( value
== 1, wxT("more than one bit set in enum value") );
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 wxPlatformInfo::wxPlatformInfo()
132 // just copy platform info for currently running platform
136 wxPlatformInfo::wxPlatformInfo(wxPortId pid
, int tkMajor
, int tkMinor
,
137 wxOperatingSystemId id
, int osMajor
, int osMinor
,
142 m_tkVersionMajor
= tkMajor
;
143 m_tkVersionMinor
= tkMinor
;
145 m_usingUniversal
= usingUniversal
;
148 m_osVersionMajor
= osMajor
;
149 m_osVersionMinor
= osMinor
;
155 bool wxPlatformInfo::operator==(const wxPlatformInfo
&t
) const
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
&&
162 m_osDesc
== t
.m_osDesc
&&
164 m_desktopEnv
== t
.m_desktopEnv
&&
165 m_port
== t
.m_port
&&
166 m_usingUniversal
== t
.m_usingUniversal
&&
167 m_arch
== t
.m_arch
&&
168 m_endian
== t
.m_endian
;
171 void wxPlatformInfo::InitForCurrentPlatform()
173 // autodetect all informations
174 const wxAppTraits
* const traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
177 wxFAIL_MSG( wxT("failed to initialize wxPlatformInfo") );
179 m_port
= wxPORT_UNKNOWN
;
180 m_usingUniversal
= false;
182 m_tkVersionMinor
= 0;
186 m_port
= traits
->GetToolkitVersion(&m_tkVersionMajor
, &m_tkVersionMinor
);
187 m_usingUniversal
= traits
->IsUsingUniversalWidgets();
188 m_desktopEnv
= traits
->GetDesktopEnvironment();
191 m_os
= wxGetOsVersion(&m_osVersionMajor
, &m_osVersionMinor
);
192 m_osDesc
= wxGetOsDescription();
193 m_endian
= wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE
: wxENDIAN_BIG
;
194 m_arch
= wxIsPlatform64Bit() ? wxARCH_64
: wxARCH_32
;
197 m_ldi
= wxGetLinuxDistributionInfo();
199 // else: leave m_ldi empty
203 const wxPlatformInfo
& wxPlatformInfo::Get()
205 static bool initialized
= false;
208 gs_platInfo
.InitForCurrentPlatform();
216 wxString
wxPlatformInfo::GetOperatingSystemDirectory()
218 return wxGetOSDirectory();
223 // ----------------------------------------------------------------------------
224 // wxPlatformInfo - enum -> string conversions
225 // ----------------------------------------------------------------------------
227 wxString
wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os
)
229 const wxChar
* string
= wxT("Unknown");
231 string
= wxT("Macintosh");
232 else if ( os
& wxOS_WINDOWS
)
233 string
= wxT("Windows");
234 else if ( os
& wxOS_UNIX
)
235 string
= wxT("Unix");
236 else if ( os
== wxOS_DOS
)
238 else if ( os
== wxOS_OS2
)
239 string
= wxT("OS/2");
244 wxString
wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os
)
246 const unsigned idx
= wxGetIndexFromEnumValue(os
);
248 wxCHECK_MSG( idx
< WXSIZEOF(wxOperatingSystemIdNames
), wxEmptyString
,
249 wxT("invalid OS id") );
251 return wxOperatingSystemIdNames
[idx
];
254 wxString
wxPlatformInfo::GetPortIdName(wxPortId port
, bool usingUniversal
)
256 const unsigned idx
= wxGetIndexFromEnumValue(port
);
258 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
259 wxT("invalid port id") );
261 wxString ret
= wxPortIdNames
[idx
];
263 if ( usingUniversal
)
264 ret
+= wxT("/wxUniversal");
269 wxString
wxPlatformInfo::GetPortIdShortName(wxPortId port
, bool usingUniversal
)
271 const unsigned idx
= wxGetIndexFromEnumValue(port
);
273 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
274 wxT("invalid port id") );
276 wxString ret
= wxPortIdNames
[idx
];
277 ret
= ret
.Mid(2).Lower(); // remove 'wx' prefix
279 if ( usingUniversal
)
285 wxString
wxPlatformInfo::GetArchName(wxArchitecture arch
)
287 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames
) == wxARCH_MAX
,
288 wxArchitectureNamesMismatch
);
290 return wxArchitectureNames
[arch
];
293 wxString
wxPlatformInfo::GetEndiannessName(wxEndianness end
)
295 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames
) == wxENDIAN_MAX
,
296 wxEndiannessNamesMismatch
);
298 return wxEndiannessNames
[end
];
302 // ----------------------------------------------------------------------------
303 // wxPlatformInfo - string -> enum conversions
304 // ----------------------------------------------------------------------------
306 wxOperatingSystemId
wxPlatformInfo::GetOperatingSystemId(const wxString
&str
)
308 for ( size_t i
= 0; i
< WXSIZEOF(wxOperatingSystemIdNames
); i
++ )
310 if ( wxString(wxOperatingSystemIdNames
[i
]).CmpNoCase(str
) == 0 )
311 return (wxOperatingSystemId
)(1 << i
);
317 wxPortId
wxPlatformInfo::GetPortId(const wxString
&str
)
319 // recognize both short and long port names
320 for ( size_t i
= 0; i
< WXSIZEOF(wxPortIdNames
); i
++ )
322 wxPortId current
= (wxPortId
)(1 << i
);
324 if ( wxString(wxPortIdNames
[i
]).CmpNoCase(str
) == 0 ||
325 GetPortIdShortName(current
, true).CmpNoCase(str
) == 0 ||
326 GetPortIdShortName(current
, false).CmpNoCase(str
) == 0 )
330 return wxPORT_UNKNOWN
;
333 wxArchitecture
wxPlatformInfo::GetArch(const wxString
&arch
)
335 if ( arch
.Contains(wxT("32")) )
338 if ( arch
.Contains(wxT("64")) )
341 return wxARCH_INVALID
;
344 wxEndianness
wxPlatformInfo::GetEndianness(const wxString
& end
)
346 const wxString
endl(end
.Lower());
347 if ( endl
.StartsWith(wxT("little")) )
348 return wxENDIAN_LITTLE
;
350 if ( endl
.StartsWith(wxT("big")) )
353 return wxENDIAN_INVALID
;