src/gui/kernel/qx11info_x11.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qwidget.h"
       
    43 #include "qpixmap.h"
       
    44 #include "qx11info_x11.h"
       
    45 #include "qt_x11_p.h"
       
    46 
       
    47 QT_BEGIN_NAMESPACE
       
    48 
       
    49 /*!
       
    50     \class QX11Info
       
    51     \brief The QX11Info class provides information about the X display
       
    52     configuration.
       
    53 
       
    54     \ingroup shared
       
    55 
       
    56     The class provides two APIs: a set of non-static functions that
       
    57     provide information about a specific widget or pixmap, and a set
       
    58     of static functions that provide the default information for the
       
    59     application.
       
    60 
       
    61     \warning This class is only available on X11. For querying
       
    62     per-screen information in a portable way, use QDesktopWidget.
       
    63 
       
    64     \sa QWidget::x11Info(), QPixmap::x11Info(), QDesktopWidget
       
    65 */
       
    66 
       
    67 /*!
       
    68     Constructs an empty QX11Info object.
       
    69 */
       
    70 QX11Info::QX11Info()
       
    71     : x11data(0)
       
    72 {
       
    73 }
       
    74 
       
    75 /*!
       
    76     Constructs a copy of \a other.
       
    77 */
       
    78 QX11Info::QX11Info(const QX11Info &other)
       
    79 {
       
    80     x11data = other.x11data;
       
    81     if (x11data)
       
    82         ++x11data->ref;
       
    83 }
       
    84 
       
    85 /*!
       
    86     Assigns \a other to this object and returns a reference to this
       
    87     object.
       
    88 */
       
    89 QX11Info &QX11Info::operator=(const QX11Info &other)
       
    90 {
       
    91     if (other.x11data)
       
    92         ++other.x11data->ref;
       
    93     if (x11data && !--x11data->ref)
       
    94         delete x11data;
       
    95     x11data = other.x11data;
       
    96     return *this;
       
    97 }
       
    98 
       
    99 /*!
       
   100     Destroys the QX11Info object.
       
   101 */
       
   102 QX11Info::~QX11Info()
       
   103 {
       
   104     if (x11data && !--x11data->ref)
       
   105         delete x11data;
       
   106 }
       
   107 
       
   108 /*!
       
   109     \internal
       
   110     Makes a shallow copy of the X11-specific data of \a fromDevice, if it is not
       
   111     null. Otherwise this function sets it to null.
       
   112 */
       
   113 
       
   114 void QX11Info::copyX11Data(const QPaintDevice *fromDevice)
       
   115 {
       
   116     QX11InfoData *xd = 0;
       
   117     if (fromDevice) {
       
   118         if (fromDevice->devType() == QInternal::Widget)
       
   119             xd = static_cast<const QWidget *>(fromDevice)->x11Info().x11data;
       
   120         else if (fromDevice->devType() == QInternal::Pixmap)
       
   121             xd = static_cast<const QPixmap *>(fromDevice)->x11Info().x11data;
       
   122     }
       
   123     setX11Data(xd);
       
   124 }
       
   125 
       
   126 /*!
       
   127     \internal
       
   128     Makes a deep copy of the X11-specific data of \a fromDevice, if it is not
       
   129     null. Otherwise this function sets it to null.
       
   130 */
       
   131 
       
   132 void QX11Info::cloneX11Data(const QPaintDevice *fromDevice)
       
   133 {
       
   134     QX11InfoData *d = 0;
       
   135     if (fromDevice) {
       
   136         QX11InfoData *xd;
       
   137         if (fromDevice->devType() == QInternal::Widget) {
       
   138             xd = static_cast<const QWidget *>(fromDevice)->x11Info().x11data;
       
   139 	} else {
       
   140 	    Q_ASSERT(fromDevice->devType() == QInternal::Pixmap);
       
   141             xd = static_cast<const QPixmap *>(fromDevice)->x11Info().x11data;
       
   142 	}
       
   143         d = new QX11InfoData(*xd);
       
   144         d->ref = 0;
       
   145     }
       
   146     setX11Data(d);
       
   147 }
       
   148 
       
   149 /*!
       
   150     \internal
       
   151     Makes a shallow copy of the X11-specific data \a d and assigns it to this
       
   152     class. This function increments the reference code of \a d.
       
   153 */
       
   154 
       
   155 void QX11Info::setX11Data(const QX11InfoData* d)
       
   156 {
       
   157     if (x11data && !--x11data->ref)
       
   158         delete x11data;
       
   159     x11data = (QX11InfoData *)d;
       
   160     if (x11data)
       
   161         ++x11data->ref;
       
   162 }
       
   163 
       
   164 
       
   165 /*!
       
   166     \internal
       
   167     If \a def is false, returns a deep copy of the x11Data, or 0 if x11Data is 0.
       
   168     If \a def is true, makes a QX11Data struct filled with the default
       
   169     values.
       
   170 
       
   171     In either case the caller is responsible for deleting the returned
       
   172     struct. But notice that the struct is a shared class, so other
       
   173     classes might also have a reference to it. The reference count of
       
   174     the returned QX11Data* is 0.
       
   175 */
       
   176 
       
   177 QX11InfoData* QX11Info::getX11Data(bool def) const
       
   178 {
       
   179     QX11InfoData* res = 0;
       
   180     if (def) {
       
   181         res = new QX11InfoData;
       
   182         res->ref = 0;
       
   183         res->screen = appScreen();
       
   184         res->depth = appDepth();
       
   185         res->cells = appCells();
       
   186         res->colormap = colormap();
       
   187         res->defaultColormap = appDefaultColormap();
       
   188         res->visual = (Visual*) appVisual();
       
   189         res->defaultVisual = appDefaultVisual();
       
   190     } else if (x11data) {
       
   191         res = new QX11InfoData;
       
   192         *res = *x11data;
       
   193         res->ref = 0;
       
   194     }
       
   195     return res;
       
   196 }
       
   197 
       
   198 /*!
       
   199     Returns the horizontal resolution of the given \a screen in terms of the
       
   200     number of dots per inch.
       
   201 
       
   202     The \a screen argument is an X screen number. Be aware that if
       
   203     the user's system uses Xinerama (as opposed to traditional X11
       
   204     multiscreen), there is only one X screen. Use QDesktopWidget to
       
   205     query for information about Xinerama screens.
       
   206 
       
   207     \sa setAppDpiX(), appDpiY()
       
   208 */
       
   209 int QX11Info::appDpiX(int screen)
       
   210 {
       
   211     if (!X11)
       
   212         return 75;
       
   213     if (screen < 0)
       
   214         screen = X11->defaultScreen;
       
   215     if (screen > X11->screenCount)
       
   216         return 0;
       
   217     return X11->screens[screen].dpiX;
       
   218 }
       
   219 
       
   220 /*!
       
   221     Sets the horizontal resolution of the given \a screen to the number of
       
   222     dots per inch specified by \a xdpi.
       
   223 
       
   224     The \a screen argument is an X screen number. Be aware that if
       
   225     the user's system uses Xinerama (as opposed to traditional X11
       
   226     multiscreen), there is only one X screen. Use QDesktopWidget to
       
   227     query for information about Xinerama screens.
       
   228 
       
   229     \sa appDpiX(), setAppDpiY()
       
   230 */
       
   231 
       
   232 void QX11Info::setAppDpiX(int screen, int xdpi)
       
   233 {
       
   234     if (!X11)
       
   235         return;
       
   236     if (screen < 0)
       
   237         screen = X11->defaultScreen;
       
   238     if (screen > X11->screenCount)
       
   239         return;
       
   240     X11->screens[screen].dpiX = xdpi;
       
   241 }
       
   242 
       
   243 /*!
       
   244     Returns the vertical resolution of the given \a screen in terms of the
       
   245     number of dots per inch.
       
   246 
       
   247     The \a screen argument is an X screen number. Be aware that if
       
   248     the user's system uses Xinerama (as opposed to traditional X11
       
   249     multiscreen), there is only one X screen. Use QDesktopWidget to
       
   250     query for information about Xinerama screens.
       
   251 
       
   252     \sa setAppDpiY(), appDpiX()
       
   253 */
       
   254 
       
   255 int QX11Info::appDpiY(int screen)
       
   256 {
       
   257     if (!X11)
       
   258         return 75;
       
   259     if (screen < 0)
       
   260         screen = X11->defaultScreen;
       
   261     if (screen > X11->screenCount)
       
   262         return 0;
       
   263     return X11->screens[screen].dpiY;
       
   264 }
       
   265 
       
   266 /*!
       
   267     Sets the vertical resolution of the given \a screen to the number of
       
   268     dots per inch specified by \a ydpi.
       
   269 
       
   270     The \a screen argument is an X screen number. Be aware that if
       
   271     the user's system uses Xinerama (as opposed to traditional X11
       
   272     multiscreen), there is only one X screen. Use QDesktopWidget to
       
   273     query for information about Xinerama screens.
       
   274 
       
   275     \sa appDpiY(), setAppDpiX()
       
   276 */
       
   277 void QX11Info::setAppDpiY(int screen, int ydpi)
       
   278 {
       
   279     if (!X11)
       
   280         return;
       
   281     if (screen < 0)
       
   282         screen = X11->defaultScreen;
       
   283     if (screen > X11->screenCount)
       
   284         return;
       
   285     X11->screens[screen].dpiY = ydpi;
       
   286 }
       
   287 
       
   288 /*!
       
   289     Returns the X11 time.
       
   290 
       
   291     \sa setAppTime(), appUserTime()
       
   292 */
       
   293 unsigned long QX11Info::appTime()
       
   294 {
       
   295     return X11 ? X11->time : 0;
       
   296 }
       
   297 
       
   298 /*!
       
   299     Sets the X11 time to the value specified by \a time.
       
   300 
       
   301     \sa appTime(), setAppUserTime()
       
   302 */
       
   303 void QX11Info::setAppTime(unsigned long time)
       
   304 {
       
   305     if (X11) {
       
   306         X11->time = time;
       
   307     }
       
   308 }
       
   309 
       
   310 /*!
       
   311     Returns the X11 user time.
       
   312 
       
   313     \sa setAppUserTime(), appTime()
       
   314 */
       
   315 unsigned long QX11Info::appUserTime()
       
   316 {
       
   317     return X11 ? X11->userTime : 0;
       
   318 }
       
   319 
       
   320 /*!
       
   321     Sets the X11 user time as specified by \a time.
       
   322 
       
   323     \sa appUserTime(), setAppTime()
       
   324 */
       
   325 void QX11Info::setAppUserTime(unsigned long time)
       
   326 {
       
   327     if (X11) {
       
   328         X11->userTime = time;
       
   329     }
       
   330 }
       
   331 
       
   332 
       
   333 /*!
       
   334     \fn const char *QX11Info::appClass()
       
   335 
       
   336     Returns the X11 application class.
       
   337 
       
   338     \sa display()
       
   339 */
       
   340 
       
   341 /*!
       
   342     Returns the default display for the application.
       
   343 
       
   344     \sa appScreen()
       
   345 */
       
   346 
       
   347 Display *QX11Info::display()
       
   348 {
       
   349     return X11 ? X11->display : 0;
       
   350 }
       
   351 
       
   352 /*!
       
   353     Returns the number of the screen where the application is being
       
   354     displayed.
       
   355 
       
   356     \sa display(), screen()
       
   357 */
       
   358 int QX11Info::appScreen()
       
   359 {
       
   360     return X11 ? X11->defaultScreen : 0;
       
   361 }
       
   362 
       
   363 /*!
       
   364     Returns a handle for the application's color map on the given \a screen.
       
   365 
       
   366     The \a screen argument is an X screen number. Be aware that if
       
   367     the user's system uses Xinerama (as opposed to traditional X11
       
   368     multiscreen), there is only one X screen. Use QDesktopWidget to
       
   369     query for information about Xinerama screens.
       
   370 
       
   371     \sa colormap(), defaultColormap()
       
   372 */
       
   373 Qt::HANDLE QX11Info::appColormap(int screen)
       
   374 {
       
   375     return X11 ? X11->screens[screen == -1 ? X11->defaultScreen : screen].colormap : 0;
       
   376 }
       
   377 
       
   378 /*!
       
   379     Returns the current visual used by the application on the given
       
   380     \a screen.
       
   381 
       
   382     The \a screen argument is an X screen number. Be aware that if
       
   383     the user's system uses Xinerama (as opposed to traditional X11
       
   384     multiscreen), there is only one X screen. Use QDesktopWidget to
       
   385     query for information about Xinerama screens.
       
   386 
       
   387     \sa visual(), defaultVisual()
       
   388 */
       
   389 
       
   390 void *QX11Info::appVisual(int screen)
       
   391 {
       
   392     return X11 ? X11->screens[screen == -1 ? X11->defaultScreen : screen].visual : 0;
       
   393 }
       
   394 
       
   395 /*!
       
   396     Returns a handle for the applications root window on the given \a screen.
       
   397 
       
   398     The \a screen argument is an X screen number. Be aware that if
       
   399     the user's system uses Xinerama (as opposed to traditional X11
       
   400     multiscreen), there is only one X screen. Use QDesktopWidget to
       
   401     query for information about Xinerama screens.
       
   402 
       
   403     \sa QApplication::desktop()
       
   404 */
       
   405 Qt::HANDLE QX11Info::appRootWindow(int screen)
       
   406 {
       
   407     return X11 ? RootWindow(X11->display, screen == -1 ? X11->defaultScreen : screen) : 0;
       
   408 }
       
   409 
       
   410 /*!
       
   411     Returns the color depth (bits per pixel) used by the application on the
       
   412     given \a screen.
       
   413 
       
   414     The \a screen argument is an X screen number. Be aware that if
       
   415     the user's system uses Xinerama (as opposed to traditional X11
       
   416     multiscreen), there is only one X screen. Use QDesktopWidget to
       
   417     query for information about Xinerama screens.
       
   418 
       
   419     \sa depth()
       
   420 */
       
   421 
       
   422 int QX11Info::appDepth(int screen)
       
   423 {
       
   424     return X11 ? X11->screens[screen == -1 ? X11->defaultScreen : screen].depth : 32;
       
   425 }
       
   426 
       
   427 /*!
       
   428     Returns the number of cells used by the application on the given \a screen.
       
   429 
       
   430     The \a screen argument is an X screen number. Be aware that if
       
   431     the user's system uses Xinerama (as opposed to traditional X11
       
   432     multiscreen), there is only one X screen. Use QDesktopWidget to
       
   433     query for information about Xinerama screens.
       
   434 
       
   435     \sa cells()
       
   436 */
       
   437 
       
   438 int QX11Info::appCells(int screen)
       
   439 { return X11 ? X11->screens[screen == -1 ? X11->defaultScreen : screen].cells : 0; }
       
   440 
       
   441 /*!
       
   442     Returns true if the application has a default color map on the given
       
   443     \a screen; otherwise returns false.
       
   444 
       
   445     The \a screen argument is an X screen number. Be aware that if
       
   446     the user's system uses Xinerama (as opposed to traditional X11
       
   447     multiscreen), there is only one X screen. Use QDesktopWidget to
       
   448     query for information about Xinerama screens.
       
   449 */
       
   450 bool QX11Info::appDefaultColormap(int screen)
       
   451 { return X11 ? X11->screens[screen == -1 ? X11->defaultScreen : screen].defaultColormap : true; }
       
   452 
       
   453 /*!
       
   454     Returns true if the application has a default visual on the given \a screen;
       
   455     otherwise returns false.
       
   456 
       
   457     The \a screen argument is an X screen number. Be aware that if
       
   458     the user's system uses Xinerama (as opposed to traditional X11
       
   459     multiscreen), there is only one X screen. Use QDesktopWidget to
       
   460     query for information about Xinerama screens.
       
   461 */
       
   462 bool QX11Info::appDefaultVisual(int screen)
       
   463 { return X11 ? X11->screens[screen == -1 ? X11->defaultScreen : screen].defaultVisual : true; }
       
   464 
       
   465 /*!
       
   466     Returns the number of the screen currently in use.
       
   467 
       
   468     The return value is an X screen number. Be aware that if the
       
   469     user's system uses Xinerama (as opposed to traditional X11
       
   470     multiscreen), there is only one X screen. Use QDesktopWidget to
       
   471     query for information about Xinerama screens.
       
   472 
       
   473     \sa appScreen()
       
   474 */
       
   475 int QX11Info::screen() const
       
   476 { return x11data ? x11data->screen : QX11Info::appScreen(); }
       
   477 
       
   478 /*!
       
   479     Returns the color depth (bits per pixel) of the X display.
       
   480 
       
   481     \sa appDepth()
       
   482 */
       
   483 
       
   484 int QX11Info::depth() const
       
   485 { return x11data ? x11data->depth : QX11Info::appDepth(); }
       
   486 
       
   487 /*!
       
   488     Returns the number of cells.
       
   489 
       
   490     \sa appCells()
       
   491 */
       
   492 
       
   493 int QX11Info::cells() const
       
   494 { return x11data ? x11data->cells : QX11Info::appCells(); }
       
   495 
       
   496 /*!
       
   497     Returns a handle for the color map.
       
   498 
       
   499     \sa defaultColormap()
       
   500 */
       
   501 
       
   502 Qt::HANDLE QX11Info::colormap() const
       
   503 { return x11data ? x11data->colormap : QX11Info::appColormap(); }
       
   504 
       
   505 /*!
       
   506     Returns true if there is a default color map; otherwise returns false.
       
   507 
       
   508     \sa colormap()
       
   509 */
       
   510 
       
   511 bool QX11Info::defaultColormap() const
       
   512 { return x11data ? x11data->defaultColormap : QX11Info::appDefaultColormap(); }
       
   513 
       
   514 /*!
       
   515     Returns the current visual.
       
   516 
       
   517     \sa appVisual(), defaultVisual()
       
   518 */
       
   519 
       
   520 void *QX11Info::visual() const
       
   521 { return x11data ? x11data->visual : QX11Info::appVisual(); }
       
   522 
       
   523 /*!
       
   524     Returns true if there is a default visual; otherwise returns false.
       
   525 
       
   526     \sa visual(), appVisual()
       
   527 */
       
   528 
       
   529 bool QX11Info::defaultVisual() const
       
   530 { return x11data ? x11data->defaultVisual : QX11Info::appDefaultVisual(); }
       
   531 
       
   532 
       
   533 /*!
       
   534     \since 4.4
       
   535 
       
   536     Returns true if there is a compositing manager running.
       
   537 */
       
   538 bool QX11Info::isCompositingManagerRunning()
       
   539 {
       
   540     return X11 ? X11->compositingManagerRunning : false;
       
   541 }
       
   542 
       
   543 QT_END_NAMESPACE