TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkdesktopscreen.cpp
Go to the documentation of this file.
1 #include "ttkdesktopscreen.h"
2 #include "ttklibrary.h"
3 
4 #include <QScreen>
5 #include <QApplication>
6 #ifdef Q_OS_WIN
7 # define WIN32_LEAN_AND_MEAN
8 # include <qt_windows.h>
9 #endif
10 #if !TTK_QT_VERSION_CHECK(5,0,0)
11 # include <QDesktopWidget>
12 #endif
13 #if defined Q_OS_LINUX
14 # include <QRegExp>
15 # include <X11/Xlib.h>
16 #endif
17 
19 {
20  const QRect &sr = screenGeometry(index);
21  const QRect &dr = availableGeometry(index);
22 
23  TaskbarInfo info;
24  if(sr.left() != dr.left())
25  {
26  info.m_size = std::abs(sr.left() - dr.left());
28  }
29  else if(sr.right() != dr.right())
30  {
31  info.m_size = std::abs(sr.right() - dr.right());
33  }
34  else if(sr.top() != dr.top())
35  {
36  info.m_size = std::abs(sr.top() - dr.top());
38  }
39  else if(sr.bottom() != dr.bottom())
40  {
41  info.m_size = std::abs(sr.bottom() - dr.bottom());
43  }
44  else
45  {
46  info.m_size = 0;
48  }
49  return info;
50 }
51 
53 {
54 #if TTK_QT_VERSION_CHECK(5,0,0)
55  Q_UNUSED(index);
56  QScreen *screen = QApplication::primaryScreen();
57  return screen ? screen->availableGeometry() : QRect();
58 #else
59  QDesktopWidget *widget = QApplication::desktop();
60  return widget ? widget->availableGeometry(index) : QRect();
61 #endif
62 }
63 
65 {
66 #if TTK_QT_VERSION_CHECK(5,0,0)
67  const QList<QScreen*> &screens = QApplication::screens();
68  return (index < 0 || index >= screens.count()) ? QRect() : screens[index]->geometry();
69 #else
70  QDesktopWidget *widget = QApplication::desktop();
71  return widget ? widget->screenGeometry(index) : QRect();
72 #endif
73 }
74 
76 {
77 #if TTK_QT_VERSION_CHECK(5,0,0)
78  const QList<QScreen*> &screens = QApplication::screens();
79  QRegion virtualGeometry;
80  for(QScreen *screen : screens)
81  {
82  virtualGeometry += screen->geometry();
83  }
84  return virtualGeometry.boundingRect();
85 #else
86  QDesktopWidget *widget = QApplication::desktop();
87  return widget ? widget->geometry() : QRect();
88 #endif
89 }
90 
92 {
93  int index = 0;
94 #if TTK_QT_VERSION_CHECK(5,0,0)
95  const int count = QApplication::screens().count();
96 #else
97  const int count = QApplication::desktop()->screenCount();
98 #endif
99  if(count > 1)
100  {
101  const QPoint &pos = QCursor::pos();
102  for(int i = 0; i < count; ++i)
103  {
104 #if TTK_QT_VERSION_CHECK(5,0,0)
105  if(QApplication::screens().at(i)->geometry().contains(pos))
106 #else
107  if(QApplication::desktop()->screenGeometry(i).contains(pos))
108 #endif
109  {
110  index = i;
111  break;
112  }
113  }
114  }
115  return index;
116 }
117 
118 QPixmap TTKDesktopScreen::grabWidget(QWidget *widget, const QRect &rect)
119 {
120 #if TTK_QT_VERSION_CHECK(5,0,0)
121  return widget->grab(rect);
122 #else
123  return QPixmap::grabWidget(widget, rect);
124 #endif
125 }
126 
127 QPixmap TTKDesktopScreen::grabWindow(int x, int y, int w, int h)
128 {
129  QWidget widget(nullptr, Qt::Desktop);
130  widget.setVisible(false);
131  widget.setGeometry(geometry());
132 #if TTK_QT_VERSION_CHECK(5,0,0)
133  QScreen *screen = QApplication::primaryScreen();
134  return screen ? screen->grabWindow(widget.winId(), x, y, w, h) : QPixmap();
135 #else
136  return QPixmap::grabWindow(widget.winId(), x, y, w, h);
137 #endif
138 }
139 
140 static constexpr int DEFAULT_DPI = 96;
141 
142 static QSize generateDPIValue()
143 {
144  QSize dpiSize(DEFAULT_DPI, DEFAULT_DPI);
145 #ifdef Q_OS_WIN
146  MONITORINFOEX miex;
147  miex.cbSize = sizeof(miex);
148 
149  HMONITOR monitor = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTONEAREST);
150  if(!GetMonitorInfo(monitor, &miex))
151  {
152  return dpiSize;
153  }
154 
155  DEVMODE dm;
156  dm.dmSize = sizeof(dm);
157  dm.dmDriverExtra = 0;
158  if(!EnumDisplaySettings(miex.szDevice, ENUM_CURRENT_SETTINGS, &dm))
159  {
160  return dpiSize;
161  }
162 
163  dpiSize.setWidth(dm.dmPelsWidth * DEFAULT_DPI * 1.0 / (miex.rcMonitor.right - miex.rcMonitor.left));
164  dpiSize.setHeight(dm.dmPelsHeight * DEFAULT_DPI * 1.0 / (miex.rcMonitor.bottom - miex.rcMonitor.top));
165 #elif defined Q_OS_LINUX
166  Display *dp = XOpenDisplay(nullptr);
167  if(!dp)
168  {
169  return dpiSize;
170  }
171 
172  double dpi = DEFAULT_DPI;
173  const char *resource = XResourceManagerString(dp);
174  if(resource)
175  {
176  const QString data(resource);
177  const QRegExp regx("Xft.dpi:\t(\\d+)");
178  if(regx.indexIn(data) != -1)
179  {
180  dpi = regx.cap(1).toDouble();
181  }
182  }
183 
184  const int screen = 0; /* Screen number */
185  const double x = (DisplayWidth(dp, screen) * 25.4) / DisplayWidthMM(dp, screen);
186  const double y = (DisplayHeight(dp, screen) * 25.4) / DisplayHeightMM(dp, screen);
187 
188  if(x < dpi && x != DEFAULT_DPI && TTKStaticCast(int, x) % DEFAULT_DPI != 0 &&
189  y < dpi && y != DEFAULT_DPI && TTKStaticCast(int, y) % DEFAULT_DPI != 0)
190  {
191  dpiSize.setWidth(x + 0.5);
192  dpiSize.setHeight(y + 0.5);
193  }
194  else
195  {
196  dpiSize.setWidth(dpi + 0.5);
197  dpiSize.setHeight(dpi + 0.5);
198  }
199 
200  XCloseDisplay(dp);
201 #elif defined Q_OS_MAC
202  if(!qApp)
203  {
204  int count = 0;
205  QApplication(count, nullptr);
206  }
207 
208 #if TTK_QT_VERSION_CHECK(5,0,0)
209  QScreen *screen = QApplication::primaryScreen();
210  if(screen)
211  {
212  dpiSize.setWidth(screen->logicalDotsPerInchX());
213  dpiSize.setHeight(screen->logicalDotsPerInchY());
214  }
215  // fallback below if no screen
216 #else
217  QDesktopWidget* desktop = QApplication::desktop();
218  if(desktop)
219  {
220  dpiSize.setWidth(desktop->logicalDpiX());
221  dpiSize.setHeight(desktop->logicalDpiY());
222  }
223 #endif
224 #endif
225  return dpiSize;
226 }
227 
229 {
230  const QSize dpiSize(generateDPIValue());
231  return dpiSize.width();
232 }
233 
235 {
236  const QSize dpiSize(generateDPIValue());
237  return dpiSize.height();
238 }
239 
241 {
242  const QSize dpiSize(generateDPIValue());
243  return (dpiSize.width() + dpiSize.height()) / 2;
244 }
245 
247 {
248  QSize dpiSize(DEFAULT_DPI, DEFAULT_DPI);
249 #ifdef Q_OS_WIN
250  HDC screen = GetDC(GetDesktopWindow());
251  dpiSize.setWidth(GetDeviceCaps(screen, LOGPIXELSX));
252  dpiSize.setHeight(GetDeviceCaps(screen, LOGPIXELSY));
253  ReleaseDC(GetDesktopWindow(), screen);
254 #else
255  dpiSize = generateDPIValue();
256 #endif
257  return (dpiSize.width() + dpiSize.height()) / 2;
258 }
#define TTKStaticCast(x, y)
Definition: ttkglobal.h:231
static QRect geometry()
static int dotsPerInchY()
static constexpr int DEFAULT_DPI
static QPixmap grabWindow(int x=0, int y=0, int w=-1, int h=-1)
static TaskbarInfo screenTaskbar(int index=0)
static int dotsPerInchX()
static int logicDotsPerInch()
static QSize generateDPIValue()
static QPixmap grabWidget(QWidget *widget, const QRect &rect)
static int dotsPerInch()
static QRect screenGeometry(int index=0)
static int screenIndex()
static QRect availableGeometry(int index=0)