summaryrefslogtreecommitdiffstats
path: root/kdesktop/pixmapserver.cpp
blob: 56224f37a712b1ebd239f4b802d4391b5af6c66d (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
/*
 *
 * This file is part of the KDE project, module kdesktop.
 * Copyright (C) 1999 Geert Jansen <[email protected]>
 * 
 * You can Freely distribute this program under the GNU General Public
 * License. See the file "COPYING" for the exact licensing terms.
 *
 *
 * Shared pixmap server for KDE.
 *
 * 5 Dec 99: Geert Jansen:
 *
 *	Initial implementation using the X11 selection mechanism.
 */

#include <assert.h>


#include <tdeapplication.h>
#include <kdebug.h>

#include <X11/X.h>
#include <X11/Xlib.h>

#include "pixmapserver.h"

#ifndef None
#define None 0L
#endif

#ifdef __GNUC__
#define ID __PRETTY_FUNCTION__ << ": "
#else
#define ID "KPixmapServer: "
#endif


KPixmapServer::KPixmapServer()
    : TQWidget(0L, "shpixmap comm window")
{
    kapp->installX11EventFilter(this);
    pixmap = XInternAtom(tqt_xdisplay(), "PIXMAP", false);
}


KPixmapServer::~KPixmapServer()
{
    SelectionIterator it;
    for (it=m_Selections.begin(); it!=m_Selections.end(); it++)
	XSetSelectionOwner(tqt_xdisplay(), it.key(), None, CurrentTime);

    DataIterator it2;
    for (it2=m_Data.begin(); it2!=m_Data.end(); it2++)
	delete it2.data().pixmap;
}


void KPixmapServer::add(TQString name, TQPixmap *pm, bool overwrite)
{
    if (m_Names.contains(name)) 
    {
	if (overwrite)
	    remove(name);
	else return;
    }
	
    TQString str = TQString("KDESHPIXMAP:%1").arg(name);
    Atom sel = XInternAtom(tqt_xdisplay(), str.latin1(), false);
    KPixmapInode pi;
    pi.handle = pm->handle();
    pi.selection = sel;
    m_Names[name] = pi;

    TDESelectionInode si;
    si.name = name;
    si.handle = pm->handle();
    m_Selections[sel] = si;

    DataIterator it = m_Data.find(pm->handle());
    if (it == m_Data.end()) 
    {
	KPixmapData data;
	data.pixmap = pm;
	data.usecount = 0;
	data.refcount = 1;
	m_Data[pm->handle()] = data;
    } else
	it.data().refcount++;

    XSetSelectionOwner(tqt_xdisplay(), sel, winId(), CurrentTime);
}


void KPixmapServer::remove(TQString name)
{
    // Remove the name
    NameIterator it = m_Names.find(name);
    if (it == m_Names.end())
	return;
    KPixmapInode pi = it.data();
    m_Names.remove(it);

    // Remove and disown the selection
    SelectionIterator it2 = m_Selections.find(pi.selection);
    assert(it2 != m_Selections.end());
    m_Selections.remove(it2);
    XSetSelectionOwner(tqt_xdisplay(), pi.selection, None, CurrentTime);

    // Decrease refcount on data
    DataIterator it3 = m_Data.find(pi.handle);
    assert(it3 != m_Data.end());
    it3.data().refcount--;
    if (!it3.data().refcount && !it3.data().usecount) 
    {
	delete it3.data().pixmap;
	m_Data.remove(it3);
    }
}


TQStringList KPixmapServer::list()
{
    TQStringList lst;
    NameIterator it;
    for (it=m_Names.begin(); it!=m_Names.end(); it++)
	lst += it.key();
    return lst;
}


void KPixmapServer::setOwner(TQString name)
{
    NameIterator it = m_Names.find(name);
    if (it == m_Names.end())
	return;

    XSetSelectionOwner(tqt_xdisplay(), it.data().selection, winId(), CurrentTime);
}


bool KPixmapServer::x11Event(XEvent *event)
{
    // Handle SelectionRequest events by which a X client can request a
    // shared pixmap.

    if (event->type == SelectionRequest) 
    {
	XSelectionRequestEvent *ev = &event->xselectionrequest;

	// Build negative reply
	XEvent reply;
	reply.type = SelectionNotify;
	reply.xselection.display = tqt_xdisplay();
	reply.xselection.requestor = ev->requestor;
	reply.xselection.selection = ev->selection;
	reply.xselection.target = pixmap;
	reply.xselection.property = None;
	reply.xselection.time = ev->time;

	// Check if we know about this selection
	Atom sel = ev->selection;
	SelectionIterator it = m_Selections.find(sel);
	if (it == m_Selections.end())
	    return false;
	TDESelectionInode si = it.data();

	// Only convert to pixmap
	if (ev->target != pixmap) 
	{
	    kdDebug(1204) << ID << "illegal target\n";
	    XSendEvent(tqt_xdisplay(), ev->requestor, false, 0, &reply);
	    return true;
	}

	// Check if there is no transaction in progress to the same property
	if (m_Active.contains(ev->property)) 
	{
	    kdDebug(1204) << ID << "selection is busy.\n";
	    XSendEvent(tqt_xdisplay(), ev->requestor, false, 0, &reply);
	    return true;
	}

	// Check if the selection was not deleted
	DataIterator it2 = m_Data.find(si.handle);
	if (it2 == m_Data.end()) 
	{
	    kdDebug(1204) << ID << "selection has been deleted.\n";
	    XSendEvent(tqt_xdisplay(), ev->requestor, false, 0, &reply);
	    return true;
	}

	kdDebug(1204) << ID << "request for " << si.name << "\n";

	// All OK: pass the pixmap handle.
	XChangeProperty(tqt_xdisplay(), ev->requestor, ev->property, pixmap,
		32, PropModeReplace, (unsigned char *) &si.handle, 1);
	it2.data().usecount++;
	m_Active[ev->property] = si.handle;

	// Request PropertyNotify events for the target window
	// XXX: The target window better not be handled by us!
	XSelectInput(tqt_xdisplay(), ev->requestor, PropertyChangeMask);

	// Acknowledge to the client and return
	reply.xselection.property = ev->property;
	XSendEvent(tqt_xdisplay(), ev->requestor, false, 0, &reply);
	return true;
    }

    // ICCCM says that the target property is to be deleted by the
    // requestor. We are notified of this by a PropertyNotify. Only then, we
    // can actually delete the pixmap if it was removed.

    if (event->type == PropertyNotify) 
    {
	XPropertyEvent *ev = &event->xproperty;

	AtomIterator it = m_Active.find(ev->atom);
	if (it == m_Active.end())
	    return false;
	HANDLE handle = it.data();
	m_Active.remove(it);

	DataIterator it2 = m_Data.find(handle);
	assert(it2 != m_Data.end());
	it2.data().usecount--;
	if (!it2.data().usecount && !it2.data().refcount) 
	{
	    delete it2.data().pixmap;
	    m_Data.remove(it2);
	}
	return true;
    }
        
    // Handle SelectionClear events.

    if (event->type == SelectionClear) 
    {
	XSelectionClearEvent *ev = &event->xselectionclear;

	SelectionIterator it = m_Selections.find(ev->selection);
	if (it == m_Selections.end())
	    return false;

	emit selectionCleared(it.data().name);
	return  true;
    }

    // Process further
    return false;
}

#include "pixmapserver.moc"