You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
255 lines
6.0 KiB
255 lines
6.0 KiB
4 years ago
|
/*
|
||
15 years ago
|
*
|
||
|
* This file is part of the KDE project, module kdesktop.
|
||
|
* Copyright (C) 1999 Geert Jansen <g.t.jansen@stud.tue.nl>
|
||
|
*
|
||
|
* 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>
|
||
|
|
||
|
|
||
12 years ago
|
#include <tdeapplication.h>
|
||
15 years ago
|
#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()
|
||
15 years ago
|
: TQWidget(0L, "shpixmap comm window")
|
||
15 years ago
|
{
|
||
|
kapp->installX11EventFilter(this);
|
||
13 years ago
|
pixmap = XInternAtom(tqt_xdisplay(), "PIXMAP", false);
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
|
KPixmapServer::~KPixmapServer()
|
||
|
{
|
||
|
SelectionIterator it;
|
||
|
for (it=m_Selections.begin(); it!=m_Selections.end(); it++)
|
||
13 years ago
|
XSetSelectionOwner(tqt_xdisplay(), it.key(), None, CurrentTime);
|
||
15 years ago
|
|
||
|
DataIterator it2;
|
||
|
for (it2=m_Data.begin(); it2!=m_Data.end(); it2++)
|
||
|
delete it2.data().pixmap;
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
void KPixmapServer::add(TQString name, TQPixmap *pm, bool overwrite)
|
||
15 years ago
|
{
|
||
13 years ago
|
if (m_Names.contains(name))
|
||
15 years ago
|
{
|
||
|
if (overwrite)
|
||
|
remove(name);
|
||
|
else return;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString str = TQString("KDESHPIXMAP:%1").arg(name);
|
||
13 years ago
|
Atom sel = XInternAtom(tqt_xdisplay(), str.latin1(), false);
|
||
15 years ago
|
KPixmapInode pi;
|
||
|
pi.handle = pm->handle();
|
||
|
pi.selection = sel;
|
||
|
m_Names[name] = pi;
|
||
|
|
||
12 years ago
|
TDESelectionInode si;
|
||
15 years ago
|
si.name = name;
|
||
|
si.handle = pm->handle();
|
||
|
m_Selections[sel] = si;
|
||
|
|
||
13 years ago
|
DataIterator it = m_Data.find(pm->handle());
|
||
15 years ago
|
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++;
|
||
|
|
||
13 years ago
|
XSetSelectionOwner(tqt_xdisplay(), sel, winId(), CurrentTime);
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
15 years ago
|
void KPixmapServer::remove(TQString name)
|
||
15 years ago
|
{
|
||
|
// Remove the name
|
||
13 years ago
|
NameIterator it = m_Names.find(name);
|
||
15 years ago
|
if (it == m_Names.end())
|
||
|
return;
|
||
|
KPixmapInode pi = it.data();
|
||
|
m_Names.remove(it);
|
||
|
|
||
|
// Remove and disown the selection
|
||
13 years ago
|
SelectionIterator it2 = m_Selections.find(pi.selection);
|
||
15 years ago
|
assert(it2 != m_Selections.end());
|
||
|
m_Selections.remove(it2);
|
||
13 years ago
|
XSetSelectionOwner(tqt_xdisplay(), pi.selection, None, CurrentTime);
|
||
15 years ago
|
|
||
|
// Decrease refcount on data
|
||
13 years ago
|
DataIterator it3 = m_Data.find(pi.handle);
|
||
15 years ago
|
assert(it3 != m_Data.end());
|
||
|
it3.data().refcount--;
|
||
|
if (!it3.data().refcount && !it3.data().usecount)
|
||
|
{
|
||
|
delete it3.data().pixmap;
|
||
|
m_Data.remove(it3);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
TQStringList KPixmapServer::list()
|
||
15 years ago
|
{
|
||
15 years ago
|
TQStringList lst;
|
||
15 years ago
|
NameIterator it;
|
||
|
for (it=m_Names.begin(); it!=m_Names.end(); it++)
|
||
|
lst += it.key();
|
||
|
return lst;
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
void KPixmapServer::setOwner(TQString name)
|
||
15 years ago
|
{
|
||
13 years ago
|
NameIterator it = m_Names.find(name);
|
||
15 years ago
|
if (it == m_Names.end())
|
||
|
return;
|
||
|
|
||
13 years ago
|
XSetSelectionOwner(tqt_xdisplay(), it.data().selection, winId(), CurrentTime);
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
|
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;
|
||
13 years ago
|
reply.xselection.display = tqt_xdisplay();
|
||
15 years ago
|
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;
|
||
13 years ago
|
SelectionIterator it = m_Selections.find(sel);
|
||
15 years ago
|
if (it == m_Selections.end())
|
||
|
return false;
|
||
12 years ago
|
TDESelectionInode si = it.data();
|
||
15 years ago
|
|
||
|
// Only convert to pixmap
|
||
|
if (ev->target != pixmap)
|
||
|
{
|
||
|
kdDebug(1204) << ID << "illegal target\n";
|
||
13 years ago
|
XSendEvent(tqt_xdisplay(), ev->requestor, false, 0, &reply);
|
||
15 years ago
|
return true;
|
||
|
}
|
||
|
|
||
|
// Check if there is no transaction in progress to the same property
|
||
13 years ago
|
if (m_Active.contains(ev->property))
|
||
15 years ago
|
{
|
||
|
kdDebug(1204) << ID << "selection is busy.\n";
|
||
13 years ago
|
XSendEvent(tqt_xdisplay(), ev->requestor, false, 0, &reply);
|
||
15 years ago
|
return true;
|
||
|
}
|
||
|
|
||
|
// Check if the selection was not deleted
|
||
13 years ago
|
DataIterator it2 = m_Data.find(si.handle);
|
||
15 years ago
|
if (it2 == m_Data.end())
|
||
|
{
|
||
|
kdDebug(1204) << ID << "selection has been deleted.\n";
|
||
13 years ago
|
XSendEvent(tqt_xdisplay(), ev->requestor, false, 0, &reply);
|
||
15 years ago
|
return true;
|
||
|
}
|
||
|
|
||
|
kdDebug(1204) << ID << "request for " << si.name << "\n";
|
||
|
|
||
|
// All OK: pass the pixmap handle.
|
||
13 years ago
|
XChangeProperty(tqt_xdisplay(), ev->requestor, ev->property, pixmap,
|
||
15 years ago
|
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!
|
||
13 years ago
|
XSelectInput(tqt_xdisplay(), ev->requestor, PropertyChangeMask);
|
||
15 years ago
|
|
||
|
// Acknowledge to the client and return
|
||
|
reply.xselection.property = ev->property;
|
||
13 years ago
|
XSendEvent(tqt_xdisplay(), ev->requestor, false, 0, &reply);
|
||
15 years ago
|
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;
|
||
|
|
||
13 years ago
|
AtomIterator it = m_Active.find(ev->atom);
|
||
15 years ago
|
if (it == m_Active.end())
|
||
|
return false;
|
||
|
HANDLE handle = it.data();
|
||
|
m_Active.remove(it);
|
||
|
|
||
13 years ago
|
DataIterator it2 = m_Data.find(handle);
|
||
15 years ago
|
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;
|
||
|
|
||
13 years ago
|
SelectionIterator it = m_Selections.find(ev->selection);
|
||
15 years ago
|
if (it == m_Selections.end())
|
||
|
return false;
|
||
|
|
||
|
emit selectionCleared(it.data().name);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// Process further
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
#include "pixmapserver.moc"
|