TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkplatformsystem.cpp
Go to the documentation of this file.
1 #include "ttkplatformsystem.h"
2 
3 #ifdef Q_OS_WIN
4 # define WIN32_LEAN_AND_MEAN
5 # include <qt_windows.h>
6 # include <QScreen>
7 # include <QApplication>
8 #elif defined Q_OS_UNIX
9 # include <X11/Xlib.h>
10 #endif
11 #include <QSize>
12 #include <QFile>
13 #include <QRegExp>
14 
15 static constexpr int DEFAULT_DPI = 96;
16 
17 static QSize generateDPIValue()
18 {
19  const QSize defaultSize(DEFAULT_DPI, DEFAULT_DPI);
20 #ifdef Q_OS_WIN
21 # if TTK_QT_VERSION_CHECK(5,0,0)
22  if(!qApp)
23  {
24  int count = 0;
25  QApplication(count, nullptr);
26  }
27 
28  QScreen *screen = QApplication::primaryScreen();
29  if(!screen)
30  {
31  return defaultSize;
32  }
33 
34  const double x = screen->logicalDotsPerInchX();
35  const double y = screen->logicalDotsPerInchY();
36 
37  return QSize(x, y);
38 # endif
39 #elif defined Q_OS_UNIX
40  Display *dp = XOpenDisplay(nullptr);
41  if(!dp)
42  {
43  return defaultSize;
44  }
45 
46  const int screen = 0; /* Screen number */
47  const double x = (DisplayWidth(dp, screen) * 25.4) / DisplayWidthMM(dp, screen);
48  const double y = (DisplayHeight(dp, screen) * 25.4) / DisplayHeightMM(dp, screen);
49 
50  XCloseDisplay(dp);
51  return QSize(x + 0.5, y + 0.5);
52 #endif
53  return defaultSize;
54 }
55 
57 {
58  const QSize dpi(generateDPIValue());
59  return dpi.width();
60 }
61 
63 {
64  const QSize dpi(generateDPIValue());
65  return dpi.height();
66 }
67 
69 {
70  const QSize dpi(generateDPIValue());
71  return (dpi.width() + dpi.height()) / 2;
72 }
73 
75 {
76 #ifdef Q_OS_WIN
77  typedef void (__stdcall *NTPROC)(DWORD*, DWORD*, DWORD*);
78  HINSTANCE instance = LoadLibraryA("ntdll.dll");
79  DWORD major, minor, buildNumber;
80  NTPROC proc = TTKVoidCast(NTPROC)GetProcAddress(instance, "RtlGetNtVersionNumbers");
81  proc(&major, &minor, &buildNumber);
82 
83  if(major == 6 && minor == 1) //win 7
84  {
85  return System::Win7;
86  }
87  else if(major == 6 && minor == 2) //win 8
88  {
89  return System::Win8;
90  }
91  else if(major == 6 && minor == 3) //win 8.1
92  {
93  return System::Win81;
94  }
95  else if(major == 10 && minor == 0) //win 10 or win 11
96  {
97  return buildNumber < (0xF0000000 | 22000) ? System::Win10 : System::Win11;
98  }
99  FreeLibrary(instance);
100 
101  SYSTEM_INFO info;
102  GetSystemInfo(&info);
103 
104  OSVERSIONINFOEX os;
105  os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
106  if(GetVersionEx((OSVERSIONINFO *)&os))
107  {
108  switch(os.dwMajorVersion)
109  {
110  case 4:
111  switch(os.dwMinorVersion)
112  {
113  case 0:
114  if(os.dwPlatformId == VER_PLATFORM_WIN32_NT)
115  {
116  return System::WinNT40;
117  }
118  else if(os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
119  {
120  return System::Win95;
121  }
122  break;
123  case 10: return System::Win98;
124  case 90: return System::WinMe;
125  default: break;
126  }
127  break;
128  case 5:
129  switch(os.dwMinorVersion)
130  {
131  case 0: return System::Win2000;
132  case 1: return System::WinXP;
133  case 2:
134  if(os.wProductType == VER_NT_WORKSTATION && info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
135  {
137  }
138  else if(GetSystemMetrics(SM_SERVERR2) == 0)
139  {
140  return System::WinServer2003;
141  }
142  else if(GetSystemMetrics(SM_SERVERR2) != 0)
143  {
145  }
146  default: break;
147  }
148  break;
149  case 6:
150  switch(os.dwMinorVersion)
151  {
152  case 0: return os.wProductType == VER_NT_WORKSTATION ? System::WinVista : System::WinServer2008;
153  case 1: return os.wProductType == VER_NT_WORKSTATION ? System::Win7 : System::WinServer2008R2;
154  case 2: return os.wProductType == VER_NT_WORKSTATION ? System::Win8 : System::WinServer2012;
155  default: break;
156  }
157  break;
158  default: return System::Unkown;
159  }
160  }
161 #elif defined Q_OS_LINUX
162  QFile file("/etc/lsb-release");
163  if(file.open(QIODevice::ReadOnly))
164  {
165  const QRegExp regx("DISTRIB_ID=(\\w+)");
166  if(regx.indexIn(QString(file.readAll())) != -1)
167  {
168  const QString &system = regx.cap(1).toLower();
169 
170  if(system == "ubuntu")
171  {
172  return System::LinuxUbuntu;
173  }
174  else if(system == "debian")
175  {
176  return System::LinuxDebian;
177  }
178  else if(system == "arch")
179  {
180  return System::LinuxArch;
181  }
182  else if(system == "centos")
183  {
184  return System::LinuxCentOS;
185  }
186  }
187  file.close();
188  }
189  return System::Linux;
190 #elif defined Q_OS_MACOS
191  return System::Mac;
192 #endif
193  return System::Unkown;
194 }
#define TTKVoidCast(x)
Definition: ttkqtglobal.h:66
int logicalDotsPerInch() const
System systemName() const
static QSize generateDPIValue()
static constexpr int DEFAULT_DPI
int logicalDotsPerInchY() const
int logicalDotsPerInchX() const