fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kdeui / util / kxutils.cpp
blob84cdeb888e097b84b1f7f10bec8b1375cb5df32d
1 /*
2 This file is part of the KDE libraries
3 Copyright (C) 2008 Lubos Lunak (l.lunak@kde.org)
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include "kxutils.h"
23 #include <config.h>
25 #ifdef Q_WS_X11
27 #include <kxerrorhandler.h>
28 #include <qbitmap.h>
29 #include <qpixmap.h>
31 #ifdef HAVE_XRENDER
32 #include <X11/extensions/Xrender.h>
33 #endif
35 namespace KXUtils
38 // Create QPixmap from X pixmap. Take care of different depths if needed.
39 QPixmap createPixmapFromHandle( WId pixmap, WId pixmap_mask )
41 Display* dpy = QX11Info::display();
42 KXErrorHandler handler;
43 Window root;
44 int x, y;
45 unsigned int w = 0;
46 unsigned int h = 0;
47 unsigned int border_w, depth;
48 if( XGetGeometry( dpy, pixmap, &root, &x, &y, &w, &h, &border_w, &depth )
49 && !handler.error( false ) && w > 0 && h > 0 )
51 QPixmap pm( w, h );
52 // Always detach before doing something behind QPixmap's back.
53 pm.detach();
54 #ifdef HAVE_XRENDER
55 if( int( depth ) != pm.depth() && depth != 1 && pm.x11PictureHandle() != None )
57 XRenderPictFormat tmpl;
58 tmpl.type = PictTypeDirect;
59 tmpl.depth = depth;
60 XRenderPictFormat* format = XRenderFindFormat( dpy, PictFormatType | PictFormatDepth, &tmpl, 0 );
61 Picture pic = XRenderCreatePicture( dpy, pixmap, format, 0, NULL );
62 XRenderComposite( dpy, PictOpSrc, pic, None, pm.x11PictureHandle(), 0, 0, 0, 0, 0, 0, w, h );
63 XRenderFreePicture( dpy, pic );
65 else
66 #endif
67 { // the normal X11 way
68 GC gc = XCreateGC( dpy, pixmap, 0, NULL );
69 if( depth == 1 )
71 QBitmap bm( w, h );
72 XCopyArea( dpy, pixmap, bm.handle(), gc, 0, 0, w, h, 0, 0 );
73 pm = bm;
75 else // depth == pm.depth()
76 XCopyArea( dpy, pixmap, pm.handle(), gc, 0, 0, w, h, 0, 0 );
77 XFreeGC( dpy, gc );
80 if( pixmap_mask != None )
82 QBitmap bm( w, h );
83 bm.detach();
84 GC gc = XCreateGC( dpy, pixmap_mask, 0, NULL );
85 XCopyArea( dpy, pixmap_mask, bm.handle(), gc, 0, 0, w, h, 0, 0 );
86 pm.setMask( bm );
87 XFreeGC( dpy, gc );
89 if( !handler.error( true )) // sync, check for error
90 return pm;
92 return QPixmap();
95 // Functions for X timestamp comparing. For Time being 32bit they're fairly simple
96 // (the #if 0 part), but on 64bit architectures Time is 64bit unsigned long,
97 // so there special care needs to be taken to always use only the lower 32bits.
98 #if 0
99 int timestampCompare( Time time1, Time time2 ) // like strcmp()
101 if( time1 == time2 )
102 return 0;
103 return ( time1 - time2 ) < 0x7fffffffU ? 1 : -1; // time1 > time2 -> 1, handle wrapping
106 Time timestampDiff( Time time1, Time time2 ) // returns time2 - time1
107 { // no need to handle wrapping?
108 return time2 - time1;
110 #else
111 int timestampCompare( unsigned long time1_, unsigned long time2_ ) // like strcmp()
113 quint32 time1 = time1_;
114 quint32 time2 = time2_;
115 if( time1 == time2 )
116 return 0;
117 return quint32( time1 - time2 ) < 0x7fffffffU ? 1 : -1; // time1 > time2 -> 1, handle wrapping
120 int timestampDiff( unsigned long time1_, unsigned long time2_ ) // returns time2 - time1
121 { // no need to handle wrapping?
122 quint32 time1 = time1_;
123 quint32 time2 = time2_;
124 return quint32( time2 - time1 );
126 #endif
129 } // namespace
131 #endif