File: XcbInterface.cpp

package info (click to toggle)
qtop 2.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,840 kB
  • ctags: 5,775
  • sloc: cpp: 38,795; makefile: 9
file content (168 lines) | stat: -rw-r--r-- 5,716 bytes parent folder | download
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software 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 General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program.  If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

#include "XcbInterface.h"

#include "Debug.h"
#include "XcbUtil.h"

#if HAVE_XCB
#include <xcb/xcb.h>
#endif

//_________________________________________________________
void XcbInterface::findIcons( Job::List& jobs ) const
{

    #if HAVE_XCB
    // check atoms
    if( !( XcbUtil::get().isSupported( XcbDefines::_NET_WM_PID ) && XcbUtil::get().isSupported( XcbDefines::_NET_WM_ICON ) ) )
    { return; }

    // find recursively, starting from root window
    _findIcons( XcbUtil::get().appRootWindow(), jobs );
    #else
    return;
    #endif
}

//_________________________________________________________
void XcbInterface::_findIcons( WId window, Job::List& jobs ) const
{

    #if HAVE_XCB
    Debug::Throw() << "XcbInterface::_findIcons - window: " << window << endl;

    // connection and atom
    xcb_connection_t* connection( XcbUtil::get().connection<xcb_connection_t>() );
    xcb_atom_t atom( *XcbUtil::get().atom<xcb_atom_t>( XcbDefines::_NET_WM_PID ) );

    xcb_get_property_cookie_t cookie( xcb_get_property( connection, 0, window, atom, XCB_ATOM_CARDINAL, 0, 1 ) );
    XcbUtil::ScopedPointer<xcb_get_property_reply_t> reply( xcb_get_property_reply( connection, cookie, nullptr ) );
    if( reply )
    {
        const uint32_t pid( reinterpret_cast<uint32_t*>(xcb_get_property_value( reply.data() ))[0] );

        // find matching job in list
        Job::List::iterator jobIter = std::find_if( jobs.begin(), jobs.end(), Job::SameIdFTor( pid ) );
        if( jobIter != jobs.end() )
        {
            // check job pixmap status
            // no need to go further, nor find children windows if jobIter already has a pixmap
            if( jobIter->iconInitialized() ) return;

            // find window icon
            QIcon icon( _icon( window ) );
            if( !icon.isNull() )
            {
                jobIter->setIconInitialized( true );
                jobIter->setIcon( icon );
                return;
            }
        }
    }

    {
        // parse children
        xcb_query_tree_cookie_t cookie = xcb_query_tree( connection, window );
        XcbUtil::ScopedPointer<xcb_query_tree_reply_t> reply( xcb_query_tree_reply( connection, cookie, nullptr ) );
        if( !reply ) return;

        const int childCount( xcb_query_tree_children_length( reply.data() ) );
        xcb_window_t* children( xcb_query_tree_children( reply.data() ) );
        for( int i=0; i < childCount; ++i )
        { _findIcons( children[i], jobs ); }
    }

    #endif
    return;

}

//_________________________________________________________
QIcon XcbInterface::_icon( WId window ) const
{

    QIcon result;

    #if HAVE_XCB
    Debug::Throw() << "XcbInterface::_icon - window: " << window << endl;

    // connection and atom
    xcb_connection_t* connection( XcbUtil::get().connection<xcb_connection_t>() );
    xcb_atom_t atom( *XcbUtil::get().atom<xcb_atom_t>( XcbDefines::_NET_WM_ICON ) );

    uint32_t offset(0);
    xcb_get_property_reply_t* reply( nullptr );
    forever
    {

        // width
        uint32_t width(0);
        xcb_get_property_cookie_t cookie( xcb_get_property( connection, 0, window, atom, XCB_ATOM_CARDINAL, offset, 1 ) );
        if( (reply = xcb_get_property_reply( connection, cookie, nullptr )) && xcb_get_property_value_length( reply ) > 0 )
        {
            width = reinterpret_cast<uint32_t*>( xcb_get_property_value(reply) )[0];
            free( reply );
            ++offset;

        } else break;

        // height
        uint32_t height(0);
        cookie = xcb_get_property( connection, 0, window, atom, XCB_ATOM_CARDINAL, offset, 1 );
        if( (reply = xcb_get_property_reply( connection, cookie, nullptr )) && xcb_get_property_value_length( reply ) > 0 )
        {
            height = reinterpret_cast<uint32_t*>( xcb_get_property_value(reply) )[0];
            free( reply );
            ++offset;

        } else break;

        // data
        const uint32_t length( width*height );
        cookie = xcb_get_property( connection, 0, window, atom, XCB_ATOM_CARDINAL, offset, length );
        if( (reply = xcb_get_property_reply( connection, cookie, nullptr )) && xcb_get_property_value_length( reply ) > 0 )
        {

            // get image data
            const uint32_t* imageData( reinterpret_cast<uint32_t*>( xcb_get_property_value( reply ) ) );

            // create image
            QImage image( width, height, QImage::Format_ARGB32);
            for(int i=0; i<image.byteCount()/4; ++i)
            { ((uint32_t*)image.bits())[i] = imageData[i]; }

            // add to icon
            result.addPixmap( QPixmap::fromImage( image ) );

            // free reply and increment offset
            free( reply );
            offset += length;

        } else break;
    }

    // free
    if( reply ) free( reply );

    #endif

    return result;
}