| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qapplication.h"
|
|---|
| 43 | #include "qdesktopwidget.h"
|
|---|
| 44 | #include "qlibrary.h"
|
|---|
| 45 | #include "qt_x11_p.h"
|
|---|
| 46 | #include "qvariant.h"
|
|---|
| 47 | #include "qwidget_p.h"
|
|---|
| 48 | #include "qx11info_x11.h"
|
|---|
| 49 | #include <limits.h>
|
|---|
| 50 |
|
|---|
| 51 | QT_BEGIN_NAMESPACE
|
|---|
| 52 |
|
|---|
| 53 | // defined in qwidget_x11.cpp
|
|---|
| 54 | extern int qt_x11_create_desktop_on_screen;
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | // function to update the workarea of the screen
|
|---|
| 58 | static bool qt_desktopwidget_workarea_dirty = true;
|
|---|
| 59 | void qt_desktopwidget_update_workarea()
|
|---|
| 60 | {
|
|---|
| 61 | qt_desktopwidget_workarea_dirty = true;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | class QSingleDesktopWidget : public QWidget
|
|---|
| 66 | {
|
|---|
| 67 | public:
|
|---|
| 68 | QSingleDesktopWidget();
|
|---|
| 69 | ~QSingleDesktopWidget();
|
|---|
| 70 | };
|
|---|
| 71 |
|
|---|
| 72 | QSingleDesktopWidget::QSingleDesktopWidget()
|
|---|
| 73 | : QWidget(0, Qt::Desktop)
|
|---|
| 74 | {
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | QSingleDesktopWidget::~QSingleDesktopWidget()
|
|---|
| 78 | {
|
|---|
| 79 | const QObjectList &childList = children();
|
|---|
| 80 | for (int i = childList.size(); i > 0 ;) {
|
|---|
| 81 | --i;
|
|---|
| 82 | childList.at(i)->setParent(0);
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 | class QDesktopWidgetPrivate : public QWidgetPrivate
|
|---|
| 88 | {
|
|---|
| 89 | public:
|
|---|
| 90 | QDesktopWidgetPrivate();
|
|---|
| 91 | ~QDesktopWidgetPrivate();
|
|---|
| 92 |
|
|---|
| 93 | void init();
|
|---|
| 94 |
|
|---|
| 95 | bool use_xinerama;
|
|---|
| 96 | int defaultScreen;
|
|---|
| 97 | int screenCount;
|
|---|
| 98 |
|
|---|
| 99 | QWidget **screens;
|
|---|
| 100 | QRect *rects;
|
|---|
| 101 | QRect *workareas;
|
|---|
| 102 | };
|
|---|
| 103 |
|
|---|
| 104 | QDesktopWidgetPrivate::QDesktopWidgetPrivate()
|
|---|
| 105 | : use_xinerama(false), defaultScreen(0), screenCount(1),
|
|---|
| 106 | screens(0), rects(0), workareas(0)
|
|---|
| 107 | {
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | QDesktopWidgetPrivate::~QDesktopWidgetPrivate()
|
|---|
| 111 | {
|
|---|
| 112 | if (screens) {
|
|---|
| 113 | for (int i = 0; i < screenCount; ++i) {
|
|---|
| 114 | if (i == defaultScreen) continue;
|
|---|
| 115 | delete screens[i];
|
|---|
| 116 | screens[i] = 0;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | free (screens);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | if (rects) delete [] rects;
|
|---|
| 123 | if (workareas) delete [] workareas;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | void QDesktopWidgetPrivate::init()
|
|---|
| 127 | {
|
|---|
| 128 | // get the screen count
|
|---|
| 129 | int newScreenCount = ScreenCount(X11->display);
|
|---|
| 130 | #ifndef QT_NO_XINERAMA
|
|---|
| 131 |
|
|---|
| 132 | XineramaScreenInfo *xinerama_screeninfo = 0;
|
|---|
| 133 |
|
|---|
| 134 | // we ignore the Xinerama extension when using the display is
|
|---|
| 135 | // using traditional multi-screen (with multiple root windows)
|
|---|
| 136 | if (newScreenCount == 1
|
|---|
| 137 | && X11->ptrXineramaQueryExtension
|
|---|
| 138 | && X11->ptrXineramaIsActive
|
|---|
| 139 | && X11->ptrXineramaQueryScreens) {
|
|---|
| 140 | int unused;
|
|---|
| 141 | use_xinerama = (X11->ptrXineramaQueryExtension(X11->display, &unused, &unused)
|
|---|
| 142 | && X11->ptrXineramaIsActive(X11->display));
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | if (use_xinerama) {
|
|---|
| 146 | xinerama_screeninfo =
|
|---|
| 147 | X11->ptrXineramaQueryScreens(X11->display, &newScreenCount);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | if (xinerama_screeninfo) {
|
|---|
| 151 | defaultScreen = 0;
|
|---|
| 152 | } else
|
|---|
| 153 | #endif // QT_NO_XINERAMA
|
|---|
| 154 | {
|
|---|
| 155 | defaultScreen = DefaultScreen(X11->display);
|
|---|
| 156 | newScreenCount = ScreenCount(X11->display);
|
|---|
| 157 | use_xinerama = false;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | delete [] rects;
|
|---|
| 161 | rects = new QRect[newScreenCount];
|
|---|
| 162 | delete [] workareas;
|
|---|
| 163 | workareas = new QRect[newScreenCount];
|
|---|
| 164 |
|
|---|
| 165 | // get the geometry of each screen
|
|---|
| 166 | int i, j, x, y, w, h;
|
|---|
| 167 | for (i = 0, j = 0; i < newScreenCount; i++, j++) {
|
|---|
| 168 |
|
|---|
| 169 | #ifndef QT_NO_XINERAMA
|
|---|
| 170 | if (use_xinerama) {
|
|---|
| 171 | x = xinerama_screeninfo[i].x_org;
|
|---|
| 172 | y = xinerama_screeninfo[i].y_org;
|
|---|
| 173 | w = xinerama_screeninfo[i].width;
|
|---|
| 174 | h = xinerama_screeninfo[i].height;
|
|---|
| 175 | } else
|
|---|
| 176 | #endif // QT_NO_XINERAMA
|
|---|
| 177 | {
|
|---|
| 178 | x = 0;
|
|---|
| 179 | y = 0;
|
|---|
| 180 | w = WidthOfScreen(ScreenOfDisplay(X11->display, i));
|
|---|
| 181 | h = HeightOfScreen(ScreenOfDisplay(X11->display, i));
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | rects[j].setRect(x, y, w, h);
|
|---|
| 185 |
|
|---|
| 186 | if (use_xinerama && j > 0 && rects[j-1].intersects(rects[j])) {
|
|---|
| 187 | // merge a "cloned" screen with the previous, hiding all crtcs
|
|---|
| 188 | // that are currently showing a sub-rect of the previous screen
|
|---|
| 189 | if ((rects[j].width()*rects[j].height()) >
|
|---|
| 190 | (rects[j-1].width()*rects[j-1].height()))
|
|---|
| 191 | rects[j-1] = rects[j];
|
|---|
| 192 | j--;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | workareas[i] = QRect();
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | if (screens) {
|
|---|
| 199 | // leaks QWidget* pointers on purpose, can't delete them as pointer escapes
|
|---|
| 200 | screens = (QWidget**) realloc(screens, j * sizeof(QWidget*));
|
|---|
| 201 | if (j > screenCount)
|
|---|
| 202 | memset(&screens[screenCount], 0, (j-screenCount) * sizeof(QWidget*));
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | screenCount = j;
|
|---|
| 206 |
|
|---|
| 207 | #ifndef QT_NO_XINERAMA
|
|---|
| 208 | if (use_xinerama && screenCount == 1)
|
|---|
| 209 | use_xinerama = false;
|
|---|
| 210 |
|
|---|
| 211 | if (xinerama_screeninfo)
|
|---|
| 212 | XFree(xinerama_screeninfo);
|
|---|
| 213 | #endif // QT_NO_XINERAMA
|
|---|
| 214 |
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | // the QDesktopWidget itself will be created on the default screen
|
|---|
| 218 | // as qt_x11_create_desktop_on_screen defaults to -1
|
|---|
| 219 | QDesktopWidget::QDesktopWidget()
|
|---|
| 220 | : QWidget(*new QDesktopWidgetPrivate, 0, Qt::Desktop)
|
|---|
| 221 | {
|
|---|
| 222 | Q_D(QDesktopWidget);
|
|---|
| 223 | d->init();
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | QDesktopWidget::~QDesktopWidget()
|
|---|
| 227 | {
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | bool QDesktopWidget::isVirtualDesktop() const
|
|---|
| 231 | {
|
|---|
| 232 | Q_D(const QDesktopWidget);
|
|---|
| 233 | return d->use_xinerama;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | int QDesktopWidget::primaryScreen() const
|
|---|
| 237 | {
|
|---|
| 238 | Q_D(const QDesktopWidget);
|
|---|
| 239 | return d->defaultScreen;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | int QDesktopWidget::numScreens() const
|
|---|
| 243 | {
|
|---|
| 244 | Q_D(const QDesktopWidget);
|
|---|
| 245 | return d->screenCount;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | QWidget *QDesktopWidget::screen(int screen)
|
|---|
| 249 | {
|
|---|
| 250 | Q_D(QDesktopWidget);
|
|---|
| 251 | if (d->use_xinerama)
|
|---|
| 252 | return this;
|
|---|
| 253 |
|
|---|
| 254 | if (screen < 0 || screen >= d->screenCount)
|
|---|
| 255 | screen = d->defaultScreen;
|
|---|
| 256 |
|
|---|
| 257 | if (! d->screens) {
|
|---|
| 258 | d->screens = (QWidget**) calloc( d->screenCount, sizeof(QWidget*));
|
|---|
| 259 | d->screens[d->defaultScreen] = this;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | if (! d->screens[screen] || // not created yet
|
|---|
| 263 | ! (d->screens[screen]->windowType() == Qt::Desktop)) { // reparented away
|
|---|
| 264 | qt_x11_create_desktop_on_screen = screen;
|
|---|
| 265 | d->screens[screen] = new QSingleDesktopWidget;
|
|---|
| 266 | qt_x11_create_desktop_on_screen = -1;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | return d->screens[screen];
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | const QRect QDesktopWidget::availableGeometry(int screen) const
|
|---|
| 273 | {
|
|---|
| 274 | Q_D(const QDesktopWidget);
|
|---|
| 275 | if (qt_desktopwidget_workarea_dirty) {
|
|---|
| 276 | // the workareas are dirty, invalidate them
|
|---|
| 277 | for (int i = 0; i < d->screenCount; ++i)
|
|---|
| 278 | d->workareas[i] = QRect();
|
|---|
| 279 | qt_desktopwidget_workarea_dirty = false;
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | if (screen < 0 || screen >= d->screenCount)
|
|---|
| 283 | screen = d->defaultScreen;
|
|---|
| 284 |
|
|---|
| 285 | if (d->workareas[screen].isValid())
|
|---|
| 286 | return d->workareas[screen];
|
|---|
| 287 |
|
|---|
| 288 | if ((d->screenCount == 1 || !isVirtualDesktop())
|
|---|
| 289 | && X11->isSupportedByWM(ATOM(_NET_WORKAREA))) {
|
|---|
| 290 | Atom ret;
|
|---|
| 291 | int format, e;
|
|---|
| 292 | unsigned char *data = 0;
|
|---|
| 293 | unsigned long nitems, after;
|
|---|
| 294 |
|
|---|
| 295 | e = XGetWindowProperty(X11->display,
|
|---|
| 296 | QX11Info::appRootWindow(screen),
|
|---|
| 297 | ATOM(_NET_WORKAREA), 0, 4, False, XA_CARDINAL,
|
|---|
| 298 | &ret, &format, &nitems, &after, &data);
|
|---|
| 299 |
|
|---|
| 300 | if (e == Success && ret == XA_CARDINAL &&
|
|---|
| 301 | format == 32 && nitems == 4) {
|
|---|
| 302 | long *workarea = (long *) data;
|
|---|
| 303 | d->workareas[screen].setRect(workarea[0], workarea[1],
|
|---|
| 304 | workarea[2], workarea[3]);
|
|---|
| 305 | } else {
|
|---|
| 306 | d->workareas[screen] = screenGeometry(screen);
|
|---|
| 307 | }
|
|---|
| 308 | if (data)
|
|---|
| 309 | XFree(data);
|
|---|
| 310 | } else {
|
|---|
| 311 | d->workareas[screen] = screenGeometry(screen);
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | return d->workareas[screen];
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | const QRect QDesktopWidget::screenGeometry(int screen) const
|
|---|
| 318 | {
|
|---|
| 319 | Q_D(const QDesktopWidget);
|
|---|
| 320 | if (screen < 0 || screen >= d->screenCount)
|
|---|
| 321 | screen = d->defaultScreen;
|
|---|
| 322 |
|
|---|
| 323 | return d->rects[screen];
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | int QDesktopWidget::screenNumber(const QWidget *widget) const
|
|---|
| 327 | {
|
|---|
| 328 | Q_D(const QDesktopWidget);
|
|---|
| 329 | if (!widget)
|
|---|
| 330 | return d->defaultScreen;
|
|---|
| 331 |
|
|---|
| 332 | #ifndef QT_NO_XINERAMA
|
|---|
| 333 | if (d->use_xinerama) {
|
|---|
| 334 | // this is how we do it for xinerama
|
|---|
| 335 | QRect frame = widget->frameGeometry();
|
|---|
| 336 | if (!widget->isWindow())
|
|---|
| 337 | frame.moveTopLeft(widget->mapToGlobal(QPoint(0, 0)));
|
|---|
| 338 |
|
|---|
| 339 | int maxSize = -1;
|
|---|
| 340 | int maxScreen = -1;
|
|---|
| 341 |
|
|---|
| 342 | for (int i = 0; i < d->screenCount; ++i) {
|
|---|
| 343 | QRect sect = d->rects[i].intersected(frame);
|
|---|
| 344 | int size = sect.width() * sect.height();
|
|---|
| 345 | if (size > maxSize && sect.width() > 0 && sect.height() > 0) {
|
|---|
| 346 | maxSize = size;
|
|---|
| 347 | maxScreen = i;
|
|---|
| 348 | }
|
|---|
| 349 | }
|
|---|
| 350 | return maxScreen;
|
|---|
| 351 | }
|
|---|
| 352 | #endif // QT_NO_XINERAMA
|
|---|
| 353 |
|
|---|
| 354 | return widget->x11Info().screen();
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | int QDesktopWidget::screenNumber(const QPoint &point) const
|
|---|
| 358 | {
|
|---|
| 359 | Q_D(const QDesktopWidget);
|
|---|
| 360 | int closestScreen = -1;
|
|---|
| 361 | int shortestDistance = INT_MAX;
|
|---|
| 362 | for (int i = 0; i < d->screenCount; ++i) {
|
|---|
| 363 | int thisDistance = d->pointToRect(point, d->rects[i]);
|
|---|
| 364 | if (thisDistance < shortestDistance) {
|
|---|
| 365 | shortestDistance = thisDistance;
|
|---|
| 366 | closestScreen = i;
|
|---|
| 367 | }
|
|---|
| 368 | }
|
|---|
| 369 | return closestScreen;
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | void QDesktopWidget::resizeEvent(QResizeEvent *event)
|
|---|
| 373 | {
|
|---|
| 374 | Q_D(QDesktopWidget);
|
|---|
| 375 | d->init();
|
|---|
| 376 | qt_desktopwidget_workarea_dirty = true;
|
|---|
| 377 | QWidget::resizeEvent(event);
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | QT_END_NAMESPACE
|
|---|