LCOV - code coverage report
Current view: directory - gfx/src - X11Util.h (source / functions) Found Hit Coverage
Test: app.info Lines: 3 3 100.0 %
Date: 2012-06-02 Functions: 1 1 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
       2                 :  * vim: sw=2 ts=8 et :
       3                 :  */
       4                 : /* ***** BEGIN LICENSE BLOCK *****
       5                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       6                 :  *
       7                 :  * The contents of this file are subject to the Mozilla Public License Version
       8                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       9                 :  * the License. You may obtain a copy of the License at:
      10                 :  * http://www.mozilla.org/MPL/
      11                 :  *
      12                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      13                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      14                 :  * for the specific language governing rights and limitations under the
      15                 :  * License.
      16                 :  *
      17                 :  * The Original Code is Mozilla Code.
      18                 :  *
      19                 :  * The Initial Developer of the Original Code is
      20                 :  *   The Mozilla Foundation
      21                 :  * Portions created by the Initial Developer are Copyright (C) 2010
      22                 :  * the Initial Developer. All Rights Reserved.
      23                 :  *
      24                 :  * Contributor(s):
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      28                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #ifndef mozilla_X11Util_h
      41                 : #define mozilla_X11Util_h
      42                 : 
      43                 : // Utilities common to all X clients, regardless of UI toolkit.
      44                 : 
      45                 : #if defined(MOZ_WIDGET_GTK2)
      46                 : #  include <gdk/gdkx.h>
      47                 : #elif defined(MOZ_WIDGET_QT)
      48                 : // X11/X.h has #define CursorShape 0, but Qt's qnamespace.h defines
      49                 : //   enum CursorShape { ... }.  Good times!
      50                 : #undef CursorShape
      51                 : #  include <QX11Info>
      52                 : #  include <X11/Xlib.h>
      53                 : #else
      54                 : #  error Unknown toolkit
      55                 : #endif 
      56                 : 
      57                 : #include "gfxCore.h"
      58                 : #include "nsDebug.h"
      59                 : 
      60                 : namespace mozilla {
      61                 : 
      62                 : /**
      63                 :  * Return the default X Display created and used by the UI toolkit.
      64                 :  */
      65                 : inline Display*
      66                 : DefaultXDisplay()
      67                 : {
      68                 : #if defined(MOZ_WIDGET_GTK2)
      69                 :   return GDK_DISPLAY();
      70                 : #elif defined(MOZ_WIDGET_QT)
      71                 :   return QX11Info::display();
      72                 : #endif
      73                 : }
      74                 : 
      75                 : /**
      76                 :  * Sets *aVisual to point to aDisplay's Visual struct corresponding to
      77                 :  * aVisualID, and *aDepth to its depth.  When aVisualID is None, these are set
      78                 :  * to NULL and 0 respectively.  Both out-parameter pointers are assumed
      79                 :  * non-NULL.  Returns true in both of these situations, but false if aVisualID
      80                 :  * is not None and not found on the Display.
      81                 :  */
      82                 : bool
      83                 : XVisualIDToInfo(Display* aDisplay, VisualID aVisualID,
      84                 :                 Visual** aVisual, unsigned int* aDepth);
      85                 : 
      86                 : /**
      87                 :  * Invoke XFree() on a pointer to memory allocated by Xlib (if the
      88                 :  * pointer is nonnull) when this class goes out of scope.
      89                 :  */
      90                 : template<typename T>
      91                 : struct ScopedXFree
      92                 : {
      93                 :   ScopedXFree() : mPtr(NULL) {}
      94                 :   ScopedXFree(T* aPtr) : mPtr(aPtr) {}
      95                 : 
      96                 :   ~ScopedXFree() { Assign(NULL); }
      97                 : 
      98                 :   ScopedXFree& operator=(T* aPtr) { Assign(aPtr); return *this; }
      99                 : 
     100                 :   operator T*() const { return get(); }
     101                 :   T* operator->() const { return get(); }
     102                 :   T* get() const { return mPtr; }
     103                 : 
     104                 : private:
     105                 :   void Assign(T* aPtr)
     106                 :   {
     107                 :     NS_ASSERTION(!mPtr || mPtr != aPtr, "double-XFree() imminent");
     108                 : 
     109                 :     if (mPtr)
     110                 :       XFree(mPtr);
     111                 :     mPtr = aPtr;
     112                 :   }
     113                 : 
     114                 :   T* mPtr;
     115                 : 
     116                 :   // disable these
     117                 :   ScopedXFree(const ScopedXFree&);
     118                 :   ScopedXFree& operator=(const ScopedXFree&);
     119                 :   static void* operator new (size_t);
     120                 :   static void operator delete (void*);
     121                 : };
     122                 : 
     123                 : /**
     124                 :  * On construction, set a graceful X error handler that doesn't crash the application and records X errors.
     125                 :  * On destruction, restore the X error handler to what it was before construction.
     126                 :  * 
     127                 :  * The SyncAndGetError() method allows to know whether a X error occurred, optionally allows to get the full XErrorEvent,
     128                 :  * and resets the recorded X error state so that a single X error will be reported only once.
     129                 :  *
     130                 :  * Nesting is correctly handled: multiple nested ScopedXErrorHandler's don't interfere with each other's state. However,
     131                 :  * if SyncAndGetError is not called on the nested ScopedXErrorHandler, then any X errors caused by X calls made while the nested
     132                 :  * ScopedXErrorHandler was in place may then be caught by the other ScopedXErrorHandler. This is just a result of X being
     133                 :  * asynchronous and us not doing any implicit syncing: the only method in this class what causes syncing is SyncAndGetError().
     134                 :  *
     135                 :  * This class is not thread-safe at all. It is assumed that only one thread is using any ScopedXErrorHandler's. Given that it's
     136                 :  * not used on Mac, it should be easy to make it thread-safe by using thread-local storage with __thread.
     137                 :  */
     138                 : class NS_GFX ScopedXErrorHandler
     139                 : {
     140                 : public:
     141                 :     // trivial wrapper around XErrorEvent, just adding ctor initializing by zero.
     142                 :     struct ErrorEvent
     143                 :     {
     144                 :         XErrorEvent mError;
     145                 : 
     146            1464 :         ErrorEvent()
     147                 :         {
     148            1464 :             memset(this, 0, sizeof(ErrorEvent));
     149            1464 :         }
     150                 :     };
     151                 : 
     152                 : private:
     153                 : 
     154                 :     // this ScopedXErrorHandler's ErrorEvent object
     155                 :     ErrorEvent mXError;
     156                 : 
     157                 :     // static pointer for use by the error handler
     158                 :     static ErrorEvent* sXErrorPtr;
     159                 : 
     160                 :     // what to restore sXErrorPtr to on destruction
     161                 :     ErrorEvent* mOldXErrorPtr;
     162                 : 
     163                 :     // what to restore the error handler to on destruction
     164                 :     int (*mOldErrorHandler)(Display *, XErrorEvent *);
     165                 : 
     166                 : public:
     167                 : 
     168                 :     static int
     169                 :     ErrorHandler(Display *, XErrorEvent *ev);
     170                 : 
     171                 :     ScopedXErrorHandler();
     172                 : 
     173                 :     ~ScopedXErrorHandler();
     174                 : 
     175                 :     /** \returns true if a X error occurred since the last time this method was called on this ScopedXErrorHandler object,
     176                 :      *           or since the creation of this ScopedXErrorHandler object if this method was never called on it.
     177                 :      *
     178                 :      * \param ev this optional parameter, if set, will be filled with the XErrorEvent object. If multiple errors occurred,
     179                 :      *           the first one will be returned.
     180                 :      */
     181                 :     bool SyncAndGetError(Display *dpy, XErrorEvent *ev = nsnull);
     182                 : 
     183                 :     /** Like SyncAndGetError, but does not sync. Faster, but only reliably catches errors in synchronous calls.
     184                 :      *
     185                 :      * \param ev this optional parameter, if set, will be filled with the XErrorEvent object. If multiple errors occurred,
     186                 :      *           the first one will be returned.
     187                 :      */
     188                 :     bool GetError(XErrorEvent *ev = nsnull);
     189                 : };
     190                 : 
     191                 : } // namespace mozilla
     192                 : 
     193                 : #endif  // mozilla_X11Util_h

Generated by: LCOV version 1.7