File: kxutils.cpp

package info (click to toggle)
kde4libs 4%3A4.14.38-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 82,620 kB
  • sloc: cpp: 763,763; xml: 12,007; ansic: 5,223; java: 4,060; perl: 2,938; yacc: 2,484; python: 1,219; sh: 1,152; ruby: 337; lex: 278; makefile: 28
file content (131 lines) | stat: -rw-r--r-- 4,297 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
    This file is part of the KDE libraries
    Copyright (C) 2008 Lubos Lunak (l.lunak@kde.org)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kxutils.h"

#include <config.h>

#ifdef Q_WS_X11

#include <kxerrorhandler.h>
#include <qbitmap.h>
#include <qpixmap.h>

#ifdef HAVE_XRENDER
#include <X11/extensions/Xrender.h>
#endif

namespace KXUtils
{

// Create QPixmap from X pixmap. Take care of different depths if needed.
QPixmap createPixmapFromHandle( WId pixmap, WId pixmap_mask )
{
    Display* dpy = QX11Info::display();
    KXErrorHandler handler;
    Window root;
    int x, y;
    unsigned int w = 0;
    unsigned int h = 0;
    unsigned int border_w, depth;
    if( XGetGeometry( dpy, pixmap, &root, &x, &y, &w, &h, &border_w, &depth )
        && !handler.error( false ) && w > 0 && h > 0 )
    {
        QPixmap pm( w, h );
        // Always detach before doing something behind QPixmap's back.
        pm.detach();
#ifdef HAVE_XRENDER
        if( int( depth ) != pm.depth() && depth != 1 && pm.x11PictureHandle() != None )
        {
            XRenderPictFormat tmpl;
            tmpl.type = PictTypeDirect;
            tmpl.depth = depth;
            XRenderPictFormat* format = XRenderFindFormat( dpy, PictFormatType | PictFormatDepth, &tmpl, 0 );
            Picture pic = XRenderCreatePicture( dpy, pixmap, format, 0, NULL );
            XRenderComposite( dpy, PictOpSrc, pic, None, pm.x11PictureHandle(), 0, 0, 0, 0, 0, 0, w, h );
            XRenderFreePicture( dpy, pic );
        }
        else
#endif
        { // the normal X11 way
            GC gc = XCreateGC( dpy, pixmap, 0, NULL );
            if( depth == 1 )
            {
                QBitmap bm( w, h );
                XCopyArea( dpy, pixmap, bm.handle(), gc, 0, 0, w, h, 0, 0 );
                pm = bm;
            }
            else // depth == pm.depth()
                XCopyArea( dpy, pixmap, pm.handle(), gc, 0, 0, w, h, 0, 0 );
            XFreeGC( dpy, gc );
        }

        if( pixmap_mask != None )
        {
            QBitmap bm( w, h );
            bm.detach();
            GC gc = XCreateGC( dpy, pixmap_mask, 0, NULL );
            XCopyArea( dpy, pixmap_mask, bm.handle(), gc, 0, 0, w, h, 0, 0 );
            pm.setMask( bm );
            XFreeGC( dpy, gc );
        }
        if( !handler.error( true )) // sync, check for error
            return pm;
    }
    return QPixmap();
}

// Functions for X timestamp comparing. For Time being 32bit they're fairly simple
// (the #if 0 part), but on 64bit architectures Time is 64bit unsigned long,
// so there special care needs to be taken to always use only the lower 32bits.
#if 0
int timestampCompare( Time time1, Time time2 ) // like strcmp()
    {
    if( time1 == time2 )
        return 0;
    return ( time1 - time2 ) < 0x7fffffffU ? 1 : -1; // time1 > time2 -> 1, handle wrapping
    }

Time timestampDiff( Time time1, Time time2 ) // returns time2 - time1
    { // no need to handle wrapping?
    return time2 - time1;
    }
#else
int timestampCompare( unsigned long time1_, unsigned long time2_ ) // like strcmp()
    {
    quint32 time1 = time1_;
    quint32 time2 = time2_;
    if( time1 == time2 )
        return 0;
    return quint32( time1 - time2 ) < 0x7fffffffU ? 1 : -1; // time1 > time2 -> 1, handle wrapping
    }

int timestampDiff( unsigned long time1_, unsigned long time2_ ) // returns time2 - time1
    { // no need to handle wrapping?
    quint32 time1 = time1_;
    quint32 time2 = time2_;
    return quint32( time2 - time1 );
    }
#endif


} // namespace

#endif