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
[] =
86 static const wxChar
* const wxArchitectureNames
[] =
92 static const wxChar
* const wxEndiannessNames
[] =
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 // returns the logarithm in base 2 of 'value'; this maps the enum values to the
104 // corresponding indexes of the string arrays above
105 static unsigned wxGetIndexFromEnumValue(int value
)
107 wxCHECK_MSG( value
, (unsigned)-1, wxT("invalid enum value") );
110 while ( !(value
& 1) )
116 wxASSERT_MSG( value
== 1, wxT("more than one bit set in enum value") );
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 wxPlatformInfo::wxPlatformInfo()
127 // just copy platform info for currently running platform
131 wxPlatformInfo::wxPlatformInfo(wxPortId pid
, int tkMajor
, int tkMinor
,
132 wxOperatingSystemId id
, int osMajor
, int osMinor
,
137 m_tkVersionMajor
= tkMajor
;
138 m_tkVersionMinor
= tkMinor
;
140 m_usingUniversal
= usingUniversal
;
143 m_osVersionMajor
= osMajor
;
144 m_osVersionMinor
= osMinor
;
150 bool wxPlatformInfo::operator==(const wxPlatformInfo
&t
) const
152 return m_tkVersionMajor
== t
.m_tkVersionMajor
&&
153 m_tkVersionMinor
== t
.m_tkVersionMinor
&&
154 m_osVersionMajor
== t
.m_osVersionMajor
&&
155 m_osVersionMinor
== t
.m_osVersionMinor
&&
157 m_osDesc
== t
.m_osDesc
&&
159 m_desktopEnv
== t
.m_desktopEnv
&&
160 m_port
== t
.m_port
&&
161 m_usingUniversal
== t
.m_usingUniversal
&&
162 m_arch
== t
.m_arch
&&
163 m_endian
== t
.m_endian
;
166 void wxPlatformInfo::InitForCurrentPlatform()
168 // autodetect all informations
169 const wxAppTraits
* const traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
172 wxFAIL_MSG( wxT("failed to initialize wxPlatformInfo") );
174 m_port
= wxPORT_UNKNOWN
;
175 m_usingUniversal
= false;
177 m_tkVersionMinor
= 0;
181 m_port
= traits
->GetToolkitVersion(&m_tkVersionMajor
, &m_tkVersionMinor
);
182 m_usingUniversal
= traits
->IsUsingUniversalWidgets();
183 m_desktopEnv
= traits
->GetDesktopEnvironment();
186 m_os
= wxGetOsVersion(&m_osVersionMajor
, &m_osVersionMinor
);
187 m_osDesc
= wxGetOsDescription();
188 m_endian
= wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE
: wxENDIAN_BIG
;
189 m_arch
= wxIsPlatform64Bit() ? wxARCH_64
: wxARCH_32
;
192 m_ldi
= wxGetLinuxDistributionInfo();
194 // else: leave m_ldi empty
198 const wxPlatformInfo
& wxPlatformInfo::Get()
200 static bool initialized
= false;
203 gs_platInfo
.InitForCurrentPlatform();
211 wxString
wxPlatformInfo::GetOperatingSystemDirectory()
213 return wxGetOSDirectory();
218 // ----------------------------------------------------------------------------
219 // wxPlatformInfo - enum -> string conversions
220 // ----------------------------------------------------------------------------
222 wxString
wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os
)
224 const wxChar
* string
= wxT("Unknown");
226 string
= wxT("Macintosh");
227 else if ( os
& wxOS_WINDOWS
)
228 string
= wxT("Windows");
229 else if ( os
& wxOS_UNIX
)
230 string
= wxT("Unix");
231 else if ( os
== wxOS_DOS
)
233 else if ( os
== wxOS_OS2
)
234 string
= wxT("OS/2");
239 wxString
wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os
)
241 const unsigned idx
= wxGetIndexFromEnumValue(os
);
243 wxCHECK_MSG( idx
< WXSIZEOF(wxOperatingSystemIdNames
), wxEmptyString
,
244 wxT("invalid OS id") );
246 return wxOperatingSystemIdNames
[idx
];
249 wxString
wxPlatformInfo::GetPortIdName(wxPortId port
, bool usingUniversal
)
251 const unsigned idx
= wxGetIndexFromEnumValue(port
);
253 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
254 wxT("invalid port id") );
256 wxString ret
= wxPortIdNames
[idx
];
258 if ( usingUniversal
)
259 ret
+= wxT("/wxUniversal");
264 wxString
wxPlatformInfo::GetPortIdShortName(wxPortId port
, bool usingUniversal
)
266 const unsigned idx
= wxGetIndexFromEnumValue(port
);
268 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
269 wxT("invalid port id") );
271 wxString ret
= wxPortIdNames
[idx
];
272 ret
= ret
.Mid(2).Lower(); // remove 'wx' prefix
274 if ( usingUniversal
)
280 wxString
wxPlatformInfo::GetArchName(wxArchitecture arch
)
282 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames
) == wxARCH_MAX
,
283 wxArchitectureNamesMismatch
);
285 return wxArchitectureNames
[arch
];
288 wxString
wxPlatformInfo::GetEndiannessName(wxEndianness end
)
290 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames
) == wxENDIAN_MAX
,
291 wxEndiannessNamesMismatch
);
293 return wxEndiannessNames
[end
];
297 // ----------------------------------------------------------------------------
298 // wxPlatformInfo - string -> enum conversions
299 // ----------------------------------------------------------------------------
301 wxOperatingSystemId
wxPlatformInfo::GetOperatingSystemId(const wxString
&str
)
303 for ( size_t i
= 0; i
< WXSIZEOF(wxOperatingSystemIdNames
); i
++ )
305 if ( wxString(wxOperatingSystemIdNames
[i
]).CmpNoCase(str
) == 0 )
306 return (wxOperatingSystemId
)(1 << i
);
312 wxPortId
wxPlatformInfo::GetPortId(const wxString
&str
)
314 // recognize both short and long port names
315 for ( size_t i
= 0; i
< WXSIZEOF(wxPortIdNames
); i
++ )
317 wxPortId current
= (wxPortId
)(1 << i
);
319 if ( wxString(wxPortIdNames
[i
]).CmpNoCase(str
) == 0 ||
320 GetPortIdShortName(current
, true).CmpNoCase(str
) == 0 ||
321 GetPortIdShortName(current
, false).CmpNoCase(str
) == 0 )
325 return wxPORT_UNKNOWN
;
328 wxArchitecture
wxPlatformInfo::GetArch(const wxString
&arch
)
330 if ( arch
.Contains(wxT("32")) )
333 if ( arch
.Contains(wxT("64")) )
336 return wxARCH_INVALID
;
339 wxEndianness
wxPlatformInfo::GetEndianness(const wxString
& end
)
341 const wxString
endl(end
.Lower());
342 if ( endl
.StartsWith(wxT("little")) )
343 return wxENDIAN_LITTLE
;
345 if ( endl
.StartsWith(wxT("big")) )
348 return wxENDIAN_INVALID
;