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: platinfo.cpp 44078 2006-12-30 21:46:22Z SN $
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 
27 #include "wx/platinfo.h"
28 
29 #ifndef WX_PRECOMP
30     #include "wx/app.h"
31     #include "wx/utils.h"
32 #endif //WX_PRECOMP
33 
34 #include "wx/apptrait.h"
35 
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 
42 // ----------------------------------------------------------------------------
43 // constants
44 // ----------------------------------------------------------------------------
45 
46 static const wxChar* const wxOperatingSystemIdNames[] =
47 {
48     _T("Apple Mac OS"),
49     _T("Apple Mac OS X"),
50 
51     _T("Microsoft Windows 9X"),
52     _T("Microsoft Windows NT"),
53     _T("Microsoft Windows Micro"),
54     _T("Microsoft Windows CE"),
55 
56     _T("Linux"),
57     _T("FreeBSD"),
58     _T("OpenBSD"),
59     _T("NetBSD"),
60 
61     _T("SunOS"),
62     _T("AIX"),
63     _T("HPUX"),
64 
65     _T("Other Unix"),
66     _T("Other Unix"),
67 
68     _T("DOS"),
69     _T("OS/2")
70 };
71 
72 static const wxChar* const wxPortIdNames[] =
73 {
74     _T("wxBase"),
75     _T("wxMSW"),
76     _T("wxMotif"),
77     _T("wxGTK"),
78     _T("wxMGL"),
79     _T("wxX11"),
80     _T("wxOS2"),
81     _T("wxMac"),
82     _T("wxCocoa"),
83     _T("wxWinCE"),
84     _T("wxPalmOS"),
85     _T("wxDFB")
86 };
87 
88 static const wxChar* const wxArchitectureNames[] =
89 {
90     _T("32 bit"),
91     _T("64 bit")
92 };
93 
94 static const wxChar* const wxEndiannessNames[] =
95 {
96     _T("Big endian"),
97     _T("Little endian"),
98     _T("PDP endian")
99 };
100 
101 // ----------------------------------------------------------------------------
102 // local functions
103 // ----------------------------------------------------------------------------
104 
105 // returns log in base 2 of the value, this maps the enum values to the
106 // corresponding indices
wxGetIndexFromEnumValue(int value)107 static unsigned wxGetIndexFromEnumValue(int value)
108 {
109     wxCHECK_MSG( value, (unsigned)-1, _T("invalid enum value") );
110 
111     int n = 0;
112     while ( !(value & 1) )
113     {
114         value >>= 1;
115         n++;
116     }
117 
118     wxASSERT_MSG( value == 1, _T("more than one bit set in enum value") );
119 
120     return n;
121 }
122 
123 // ----------------------------------------------------------------------------
124 // wxPlatformInfo
125 // ----------------------------------------------------------------------------
126 
wxPlatformInfo()127 wxPlatformInfo::wxPlatformInfo()
128 {
129     // just copy platform info for currently running platform
130     *this = Get();
131 }
132 
wxPlatformInfo(wxPortId pid,int tkMajor,int tkMinor,wxOperatingSystemId id,int osMajor,int osMinor,wxArchitecture arch,wxEndianness endian,bool usingUniversal)133 wxPlatformInfo::wxPlatformInfo(wxPortId pid, int tkMajor, int tkMinor,
134                                wxOperatingSystemId id, int osMajor, int osMinor,
135                                wxArchitecture arch,
136                                wxEndianness endian,
137                                bool usingUniversal)
138 {
139     m_tkVersionMajor = tkMajor;
140     m_tkVersionMinor = tkMinor;
141     m_port = pid;
142     m_usingUniversal = usingUniversal;
143 
144     m_os = id;
145     m_osVersionMajor = osMajor;
146     m_osVersionMinor = osMinor;
147 
148     m_endian = endian;
149     m_arch = arch;
150 }
151 
operator ==(const wxPlatformInfo & t) const152 bool wxPlatformInfo::operator==(const wxPlatformInfo &t) const
153 {
154     return m_tkVersionMajor == t.m_tkVersionMajor &&
155            m_tkVersionMinor == t.m_tkVersionMinor &&
156            m_osVersionMajor == t.m_osVersionMajor &&
157            m_osVersionMinor == t.m_osVersionMinor &&
158            m_os == t.m_os &&
159            m_port == t.m_port &&
160            m_usingUniversal == t.m_usingUniversal &&
161            m_arch == t.m_arch &&
162            m_endian == t.m_endian;
163 }
164 
InitForCurrentPlatform()165 void wxPlatformInfo::InitForCurrentPlatform()
166 {
167     // autodetect all informations
168     const wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
169     if ( !traits )
170     {
171         wxFAIL_MSG( _T("failed to initialize wxPlatformInfo") );
172 
173         m_port = wxPORT_UNKNOWN;
174         m_usingUniversal = false;
175         m_tkVersionMajor =
176                 m_tkVersionMinor = 0;
177     }
178     else
179     {
180         m_port = traits->GetToolkitVersion(&m_tkVersionMajor, &m_tkVersionMinor);
181         m_usingUniversal = traits->IsUsingUniversalWidgets();
182     }
183 
184     m_os = wxGetOsVersion(&m_osVersionMajor, &m_osVersionMinor);
185     m_endian = wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE : wxENDIAN_BIG;
186     m_arch = wxIsPlatform64Bit() ? wxARCH_64 : wxARCH_32;
187 }
188 
189 /* static */
Get()190 const wxPlatformInfo& wxPlatformInfo::Get()
191 {
192     static bool initialized = false;
193     if ( !initialized )
194     {
195         gs_platInfo.InitForCurrentPlatform();
196         initialized = true;
197     }
198 
199     return gs_platInfo;
200 }
201 
202 
203 
204 // ----------------------------------------------------------------------------
205 // wxPlatformInfo - enum -> string conversions
206 // ----------------------------------------------------------------------------
207 
GetOperatingSystemFamilyName(wxOperatingSystemId os)208 wxString wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os)
209 {
210     const wxChar* string = _T("Unknown");
211     if ( os & wxOS_MAC )
212         string = _T("Macintosh");
213     else if ( os & wxOS_WINDOWS )
214         string = _T("Windows");
215     else if ( os & wxOS_UNIX )
216         string = _T("Unix");
217     else if ( os == wxOS_DOS )
218         string = _T("DOS");
219     else if ( os == wxOS_OS2 )
220         string = _T("OS/2");
221 
222     return string;
223 }
224 
GetOperatingSystemIdName(wxOperatingSystemId os)225 wxString wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os)
226 {
227     const unsigned idx = wxGetIndexFromEnumValue(os);
228 
229     wxCHECK_MSG( idx < WXSIZEOF(wxOperatingSystemIdNames), wxEmptyString,
230                  _T("invalid OS id") );
231 
232     return wxOperatingSystemIdNames[idx];
233 }
234 
GetPortIdName(wxPortId port,bool usingUniversal)235 wxString wxPlatformInfo::GetPortIdName(wxPortId port, bool usingUniversal)
236 {
237     const unsigned idx = wxGetIndexFromEnumValue(port);
238 
239     wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
240                  _T("invalid port id") );
241 
242     wxString ret = wxPortIdNames[idx];
243 
244     if ( usingUniversal )
245         ret += wxT("/wxUniversal");
246 
247     return ret;
248 }
249 
GetPortIdShortName(wxPortId port,bool usingUniversal)250 wxString wxPlatformInfo::GetPortIdShortName(wxPortId port, bool usingUniversal)
251 {
252     const unsigned idx = wxGetIndexFromEnumValue(port);
253 
254     wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
255                  _T("invalid port id") );
256 
257     wxString ret = wxPortIdNames[idx];
258     ret = ret.Mid(2).Lower();       // remove 'wx' prefix
259 
260     if ( usingUniversal )
261         ret += wxT("univ");
262 
263     return ret;
264 }
265 
GetArchName(wxArchitecture arch)266 wxString wxPlatformInfo::GetArchName(wxArchitecture arch)
267 {
268     wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames) == wxARCH_MAX,
269                            wxArchitectureNamesMismatch );
270 
271     return wxArchitectureNames[arch];
272 }
273 
GetEndiannessName(wxEndianness end)274 wxString wxPlatformInfo::GetEndiannessName(wxEndianness end)
275 {
276     wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames) == wxENDIAN_MAX,
277                            wxEndiannessNamesMismatch );
278 
279     return wxEndiannessNames[end];
280 }
281 
282 
283 // ----------------------------------------------------------------------------
284 // wxPlatformInfo - string -> enum conversions
285 // ----------------------------------------------------------------------------
286 
GetOperatingSystemId(const wxString & str)287 wxOperatingSystemId wxPlatformInfo::GetOperatingSystemId(const wxString &str)
288 {
289     for ( size_t i = 0; i < WXSIZEOF(wxOperatingSystemIdNames); i++ )
290     {
291         if ( wxString(wxOperatingSystemIdNames[i]).CmpNoCase(str) == 0 )
292             return (wxOperatingSystemId)(1 << i);
293     }
294 
295     return wxOS_UNKNOWN;
296 }
297 
GetPortId(const wxString & str)298 wxPortId wxPlatformInfo::GetPortId(const wxString &str)
299 {
300     // recognize both short and long port names
301     for ( size_t i = 0; i < WXSIZEOF(wxPortIdNames); i++ )
302     {
303         wxPortId current = (wxPortId)(1 << i);
304 
305         if ( wxString(wxPortIdNames[i]).CmpNoCase(str) == 0 ||
306              GetPortIdShortName(current, true).CmpNoCase(str) == 0 ||
307              GetPortIdShortName(current, false).CmpNoCase(str) == 0 )
308             return current;
309     }
310 
311     return wxPORT_UNKNOWN;
312 }
313 
GetArch(const wxString & arch)314 wxArchitecture wxPlatformInfo::GetArch(const wxString &arch)
315 {
316     if ( arch.Contains(wxT("32")) )
317         return wxARCH_32;
318 
319     if ( arch.Contains(wxT("64")) )
320         return wxARCH_64;
321 
322     return wxARCH_INVALID;
323 }
324 
GetEndianness(const wxString & end)325 wxEndianness wxPlatformInfo::GetEndianness(const wxString& end)
326 {
327     wxString endl(end.Lower());
328     if ( end.StartsWith(wxT("little")) )
329         return wxENDIAN_LITTLE;
330 
331     if ( end.StartsWith(wxT("big")) )
332         return wxENDIAN_BIG;
333 
334     return wxENDIAN_INVALID;
335 }
336 
337