summaryrefslogtreecommitdiffstats
path: root/client/myrootpixmap.cpp
blob: 150414727cc32388bd3d3fa1ceb4a83a6352d72b (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 *
 * $Id: krootpixmap.cpp,v 1.20 2003/06/01 01:49:31 hadacek Exp $
 *
 * This file is part of the KDE project, module tdeui.
 * Copyright (C) 1999,2000 Geert Jansen <[email protected]>
 *
 * You can Freely distribute this program under the GNU Library
 * General Public License. See the file "COPYING.LIB" for the exact
 * licensing terms.
 */
 
/* Modified by Sascha Hlusiak */
/* Further modified 11/18/2014 by Timothy Pearson <[email protected]> */

#include <tqwidget.h>
#include <tqtimer.h>
#include <tqrect.h>
#include <tqimage.h>

#ifndef TQ_WS_TQWS //FIXME
#include <tdeapplication.h>
#include <kimageeffect.h>
#include <kpixmapio.h>
#include <netwm.h>
#include <twin.h>
#include <kdebug.h>
#include <netwm.h>
#include <dcopclient.h>
#include <dcopref.h>
#include <tqpainter.h>

#include <ksharedpixmap.h>
#include "myrootpixmap.h"

static TQString wallpaperForDesktop(int desktop)
{
    return DCOPRef("kdesktop", "KBackgroundIface").call("currentWallpaper", desktop);
}

DesktopWallpaperWatcher::DesktopWallpaperWatcher() : TQWidget(), m_old_current_desktop(-1)
{
	kapp->installX11EventFilter( this );
	(void ) kapp->desktop(); //trigger desktop widget creation to select root window events
}

DesktopWallpaperWatcher::~DesktopWallpaperWatcher()
{
}

bool DesktopWallpaperWatcher::x11Event( XEvent * ev )
{
	if ( ev->xany.window == tqt_xrootwin() ) {
		NETRootInfo rinfo( tqt_xdisplay(), NET::CurrentDesktop );
		rinfo.activate();

		if ( rinfo.currentDesktop() != m_old_current_desktop ) {
			emit currentDesktopChanged( rinfo.currentDesktop() );
		}

		m_old_current_desktop = rinfo.currentDesktop();
	}

	return false;
}

class KMyRootPixmapData
{
public:
#ifdef TQ_WS_X11
    DesktopWallpaperWatcher *twin;
#endif
};

KMyRootPixmap::KMyRootPixmap( TQWidget * widget, const char *name )
    : TQObject(widget, name ? name : "KMyRootPixmap" ) 
{
    init();
}

KMyRootPixmap::KMyRootPixmap( TQWidget *, TQObject *parent, const char *name )
    : TQObject( parent, name ? name : "KMyRootPixmap" ) 
{
    init();
}

void KMyRootPixmap::init()
{
    d = new KMyRootPixmapData;
//    m_Fade = 0;
    m_pPixmap = new TDESharedPixmap;
//    m_pTimer = new TQTimer( this );
    m_bInit = false;
    m_bActive = false;
    m_Desk=-1;
//    m_bCustomPaint = false;

//    connect(kapp, TQ_SIGNAL(backgroundChanged(int)), TQ_SLOT(slotBackgroundChanged(int)));
    connect(m_pPixmap, TQ_SIGNAL(done(bool)), TQ_SLOT(slotDone(bool)));
//    connect(m_pTimer, TQ_SIGNAL(timeout()), TQ_SLOT(repaint()));

#ifdef TQ_WS_X11
    d->twin = new DesktopWallpaperWatcher();
    connect(d->twin, TQ_SIGNAL(currentDesktopChanged(int)), TQ_SLOT(desktopChanged(int)));
#endif

//    d->toplevel = m_pWidget->topLevelWidget();
//    d->toplevel->installEventFilter(this);
}

KMyRootPixmap::~KMyRootPixmap()
{
    delete m_pPixmap;
    delete d;
}

void KMyRootPixmap::desktopChanged(int desktop)
{
    if (wallpaperForDesktop(m_Desk) == wallpaperForDesktop(desktop) &&
	!wallpaperForDesktop(m_Desk).isNull())
	return;

// #ifdef TQ_WS_X11
//     if (KWin::windowInfo(m_pWidget->topLevelWidget()->winId()).desktop() == NET::OnAllDesktops &&
// 	pixmapName(m_Desk) != pixmapName(desktop))
// #endif
	repaint(true);
}

int KMyRootPixmap::currentDesktop() const
{
    NETRootInfo rinfo( tqt_xdisplay(), NET::CurrentDesktop );
    rinfo.activate();
    return rinfo.currentDesktop();
}

void KMyRootPixmap::start()
{
    if (m_bActive)
    return;

    m_bActive = true;
    enableExports();
    return;
//    if (m_bInit)
//    repaint(true);
}

void KMyRootPixmap::stop()
{
    m_bActive = false;
//    m_pTimer->stop();
}


void KMyRootPixmap::repaint()
{
    repaint(false);
}

void KMyRootPixmap::repaint(bool force)
{
//	printf("KMyRootPixmap::repaint(%s)\n",force?"true":"false");
    if ((!force) && (m_Desk==currentDesktop()))return;
    
    m_Desk = currentDesktop();

    if (!isAvailable())
    {
    	emit backgroundUpdated(NULL);
    }else{
    	// TDESharedPixmap will correctly generate a tile for us.
    	m_pPixmap->loadFromShared(pixmapName(m_Desk));
    	updateBackground( m_pPixmap );
    }
}

bool KMyRootPixmap::isAvailable() 
{
    return m_pPixmap->isAvailable(pixmapName(m_Desk));
}

TQString KMyRootPixmap::pixmapName(int desk) 
{
    TQString pattern = TQString("DESKTOP%1");
    int screen_number = DefaultScreen(tqt_xdisplay());
    if (screen_number) {
        pattern = TQString("SCREEN%1-DESKTOP").arg(screen_number) + "%1";
    }
    return pattern.arg( desk );
}


void KMyRootPixmap::enableExports()
{
//    kdDebug(270) << k_lineinfo << "activating background exports.\n";
    DCOPClient *client = kapp->dcopClient();
    if (!client->isAttached())
    client->attach();
    TQByteArray data;
    TQDataStream args( data, IO_WriteOnly );
    args << 1;

    TQCString appname( "kdesktop" );
    int screen_number = DefaultScreen(tqt_xdisplay());
    if ( screen_number )
        appname.sprintf("kdesktop-screen-%d", screen_number );

    client->send( appname, "KBackgroundIface", "setExport(int)", data );
}


void KMyRootPixmap::slotDone(bool success)
{
    if (!success)
    {
//    	kdWarning(270) << k_lineinfo << "loading of desktop background failed.\n";
    	return;
    }

    // We need to test active as the pixmap might become available
    // after the widget has been destroyed.
    if ( m_bActive )
    updateBackground( m_pPixmap );
}

void KMyRootPixmap::updateBackground( TDESharedPixmap *spm )
{
//	printf("KMyRootPixmap::updateBackground(%p)\n",spm);
    TQPixmap *px=spm;
    if (px->isNull() || px->width()==0 || px->height()==0) 
    {	// This is NOT an image, something went wrong, update to plain
    	emit backgroundUpdated(NULL);
	return;
    }
    KPixmapIO io;
    TQSize desktopsize(TQApplication::desktop()->width(),TQApplication::desktop()->height());
    
    if (px->rect().size()==desktopsize)
    {	// Image has already the right dimension, make a quick update
    	TQImage img = io.convertToImage(*spm);
    	emit backgroundUpdated(&img);
	return;  
    }else{	// we need to create a tiled pixmap and then the image to update
    	TQPixmap pix(desktopsize,spm->TQPixmap::depth());
    	TQPainter pufferPainter(&pix);
    
    	pufferPainter.drawTiledPixmap(pix.rect(),*spm);
    
    	pufferPainter.end();
    
    	TQImage img=io.convertToImage(pix);
    	emit backgroundUpdated(&img);
    }
}

#include "myrootpixmap.moc"
#endif