2022-11-05 08:26:58 +02:00
|
|
|
/* This file is part of the KDE libraries
|
|
|
|
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License version 2, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
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 "kselectionowner.h"
|
|
|
|
#include "kxerrorhandler.h"
|
|
|
|
#include "kdebug.h"
|
2022-11-15 06:53:23 +02:00
|
|
|
#include "netwm.h"
|
2022-11-05 08:26:58 +02:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QThread>
|
|
|
|
|
2022-11-15 06:53:23 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2022-11-05 10:33:18 +02:00
|
|
|
#define KSELECTIONOWNER_TIMEOUT 500
|
|
|
|
#define KSELECTIONOWNER_SLEEPTIME 500
|
2022-11-16 03:48:33 +02:00
|
|
|
#define KSELECTIONOWNER_CHECKTIME 500
|
2022-11-05 08:26:58 +02:00
|
|
|
|
2022-11-15 03:02:41 +02:00
|
|
|
static Window kWaitForOwner(Display* x11display, const Atom x11atom, Window currentowner)
|
2022-11-15 04:35:24 +02:00
|
|
|
{
|
|
|
|
ushort counter = 0;
|
|
|
|
while (currentowner != None && counter < 10) {
|
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, KSELECTIONOWNER_TIMEOUT);
|
|
|
|
QThread::msleep(KSELECTIONOWNER_SLEEPTIME);
|
|
|
|
counter++;
|
|
|
|
currentowner = XGetSelectionOwner(x11display, x11atom);
|
|
|
|
}
|
|
|
|
return currentowner;
|
|
|
|
}
|
|
|
|
|
2022-11-05 08:26:58 +02:00
|
|
|
class KSelectionOwnerPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
KSelectionOwnerPrivate();
|
2022-11-12 15:19:03 +02:00
|
|
|
~KSelectionOwnerPrivate();
|
2022-11-05 08:26:58 +02:00
|
|
|
|
2022-11-15 01:57:27 +02:00
|
|
|
QByteArray atomname;
|
2022-11-05 08:26:58 +02:00
|
|
|
Atom x11atom;
|
|
|
|
Display* x11display;
|
|
|
|
int x11screen;
|
|
|
|
Window x11window;
|
2022-11-05 12:24:51 +02:00
|
|
|
int timerid;
|
2022-11-05 08:26:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
KSelectionOwnerPrivate::KSelectionOwnerPrivate()
|
|
|
|
: x11atom(None),
|
2022-11-12 15:19:03 +02:00
|
|
|
x11display(nullptr),
|
|
|
|
x11screen(0),
|
2022-11-05 12:24:51 +02:00
|
|
|
x11window(None),
|
|
|
|
timerid(0)
|
2022-11-05 08:26:58 +02:00
|
|
|
{
|
2022-11-12 15:19:03 +02:00
|
|
|
x11display = XOpenDisplay(NULL);
|
|
|
|
if (!x11display) {
|
|
|
|
kFatal() << "Could not open X11 display";
|
|
|
|
}
|
|
|
|
x11screen = DefaultScreen(x11display);
|
2022-11-05 08:26:58 +02:00
|
|
|
}
|
|
|
|
|
2022-11-12 15:19:03 +02:00
|
|
|
KSelectionOwnerPrivate::~KSelectionOwnerPrivate()
|
|
|
|
{
|
|
|
|
if (x11display) {
|
|
|
|
XCloseDisplay(x11display);
|
|
|
|
}
|
|
|
|
}
|
2022-11-05 08:26:58 +02:00
|
|
|
|
2022-11-05 20:11:20 +02:00
|
|
|
KSelectionOwner::KSelectionOwner(const char* const atomname, const int screen, QObject *parent)
|
2022-11-05 08:26:58 +02:00
|
|
|
: QObject(parent),
|
|
|
|
d(new KSelectionOwnerPrivate())
|
|
|
|
{
|
2022-11-15 01:57:27 +02:00
|
|
|
d->atomname = atomname;
|
|
|
|
d->x11atom = XInternAtom(d->x11display, d->atomname.constData(), False);
|
2022-11-05 08:26:58 +02:00
|
|
|
if (screen >= 0) {
|
|
|
|
d->x11screen = screen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KSelectionOwner::~KSelectionOwner()
|
|
|
|
{
|
|
|
|
release();
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2022-11-05 17:17:05 +02:00
|
|
|
Window KSelectionOwner::ownerWindow() const
|
|
|
|
{
|
2022-11-15 07:29:15 +02:00
|
|
|
kDebug(240) << "Owner of" << d->atomname << "is" << d->x11window;
|
2022-11-05 17:17:05 +02:00
|
|
|
return d->x11window;
|
|
|
|
}
|
|
|
|
|
2022-11-15 07:29:15 +02:00
|
|
|
Window KSelectionOwner::currentOwnerWindow() const
|
|
|
|
{
|
|
|
|
kDebug(240) << "Current" << d->atomname << "owner is" << d->x11window;
|
|
|
|
return XGetSelectionOwner(d->x11display, d->x11atom);
|
|
|
|
}
|
|
|
|
|
2022-11-05 08:26:58 +02:00
|
|
|
bool KSelectionOwner::claim(const bool force)
|
|
|
|
{
|
|
|
|
Window currentowner = XGetSelectionOwner(d->x11display, d->x11atom);
|
|
|
|
if (currentowner != None && !force) {
|
2022-11-15 01:57:27 +02:00
|
|
|
kDebug(240) << d->atomname << "is owned";
|
2022-11-05 08:26:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-11-05 10:33:18 +02:00
|
|
|
if (currentowner != None) {
|
2022-11-15 06:53:23 +02:00
|
|
|
kDebug(240) << d->atomname << "is owned, killing owner via kill()";
|
2022-11-15 04:35:24 +02:00
|
|
|
KXErrorHandler kx11errorhandler(d->x11display);
|
2022-11-15 06:53:23 +02:00
|
|
|
KXErrorHandler handler;
|
2022-11-15 16:47:05 +02:00
|
|
|
NETWinInfo netwininfo(
|
|
|
|
d->x11display,
|
|
|
|
currentowner,
|
|
|
|
RootWindow(d->x11display, d->x11screen),
|
|
|
|
NET::WMPid
|
|
|
|
);
|
2022-11-15 04:35:24 +02:00
|
|
|
if (kx11errorhandler.error(true)) {
|
|
|
|
kWarning(240) << KXErrorHandler::errorMessage(kx11errorhandler.errorEvent());
|
|
|
|
return false;
|
2022-11-05 08:26:58 +02:00
|
|
|
}
|
2022-11-15 06:53:23 +02:00
|
|
|
const int currentownerpid = netwininfo.pid();
|
|
|
|
if (currentownerpid > 0) {
|
|
|
|
::kill(currentownerpid, SIGKILL);
|
|
|
|
::kill(currentownerpid, SIGTERM);
|
|
|
|
// ::kill(currentownerpid, YOUSHALLDIEONEWAYORTHEOTHER);
|
|
|
|
kDebug(240) << "Waiting for" << d->atomname << "owner";
|
|
|
|
currentowner = kWaitForOwner(d->x11display, d->x11atom, currentowner);
|
|
|
|
} else {
|
|
|
|
kDebug(240) << "Invalid" << d->atomname << "PID";
|
|
|
|
}
|
2022-11-05 08:26:58 +02:00
|
|
|
}
|
|
|
|
if (currentowner != None) {
|
2022-11-15 06:53:23 +02:00
|
|
|
kDebug(240) << d->atomname << "is owned, killing owner via XKillClient()";
|
2022-11-12 21:39:59 +02:00
|
|
|
KXErrorHandler kx11errorhandler(d->x11display);
|
2022-11-05 08:26:58 +02:00
|
|
|
XKillClient(d->x11display, currentowner);
|
|
|
|
XFlush(d->x11display);
|
2022-11-15 04:35:24 +02:00
|
|
|
kDebug(240) << "Waiting for" << d->atomname << "owner";
|
2022-11-15 03:02:41 +02:00
|
|
|
currentowner = kWaitForOwner(d->x11display, d->x11atom, currentowner);
|
2022-11-15 04:35:24 +02:00
|
|
|
}
|
|
|
|
if (currentowner != None) {
|
|
|
|
kWarning(240) << d->atomname << "is still owned";
|
|
|
|
return false;
|
2022-11-05 08:26:58 +02:00
|
|
|
}
|
2022-11-15 01:57:27 +02:00
|
|
|
kDebug(240) << "Creating" << d->atomname << "owner";
|
2022-11-12 21:39:59 +02:00
|
|
|
KXErrorHandler kx11errorhandler(d->x11display);
|
2022-11-05 16:58:35 +02:00
|
|
|
d->x11window = XCreateSimpleWindow(
|
2022-11-15 16:47:05 +02:00
|
|
|
d->x11display,
|
|
|
|
RootWindow(d->x11display, d->x11screen),
|
2022-11-05 16:58:35 +02:00
|
|
|
0, 0, // x and y
|
|
|
|
1, 1, // width and height
|
|
|
|
0, 0, 0 // border width, border and background pixels
|
2022-11-05 08:26:58 +02:00
|
|
|
);
|
2022-11-06 08:14:33 +02:00
|
|
|
if (kx11errorhandler.error(true)) {
|
2022-11-09 02:28:14 +02:00
|
|
|
kWarning(240) << KXErrorHandler::errorMessage(kx11errorhandler.errorEvent());
|
2022-11-06 08:14:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-11-15 16:47:05 +02:00
|
|
|
NETWinInfo netwininfo(
|
|
|
|
d->x11display, d->x11window,
|
|
|
|
RootWindow(d->x11display, d->x11screen),
|
|
|
|
NET::WMPid
|
|
|
|
);
|
2022-11-15 06:53:23 +02:00
|
|
|
netwininfo.setPid(::getpid());
|
2022-11-12 13:56:53 +02:00
|
|
|
XSelectInput(d->x11display, d->x11window, NoEventMask);
|
2022-11-05 08:26:58 +02:00
|
|
|
XSetSelectionOwner(d->x11display, d->x11atom, d->x11window, CurrentTime);
|
|
|
|
XFlush(d->x11display);
|
2022-11-05 12:24:51 +02:00
|
|
|
d->timerid = startTimer(KSELECTIONOWNER_CHECKTIME);
|
2022-11-05 08:26:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KSelectionOwner::release()
|
|
|
|
{
|
|
|
|
if (d->x11window == None) {
|
2022-11-15 01:57:27 +02:00
|
|
|
kDebug(240) << "No" << d->atomname << "owner";
|
2022-11-05 08:26:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-11-06 08:05:15 +02:00
|
|
|
if (d->timerid > 0) {
|
2022-11-05 12:24:51 +02:00
|
|
|
killTimer(d->timerid);
|
|
|
|
d->timerid = 0;
|
|
|
|
}
|
2022-11-15 01:57:27 +02:00
|
|
|
kDebug(240) << "Destroying" << d->atomname << "owner window";
|
|
|
|
KXErrorHandler kx11errorhandler(d->x11display);
|
2022-11-05 08:26:58 +02:00
|
|
|
XDestroyWindow(d->x11display, d->x11window);
|
|
|
|
XFlush(d->x11display);
|
2022-11-15 01:57:27 +02:00
|
|
|
if (kx11errorhandler.error(true)) {
|
|
|
|
kWarning(240) << KXErrorHandler::errorMessage(kx11errorhandler.errorEvent());
|
|
|
|
}
|
2022-11-05 08:26:58 +02:00
|
|
|
d->x11window = None;
|
|
|
|
}
|
|
|
|
|
2022-11-05 12:24:51 +02:00
|
|
|
void KSelectionOwner::timerEvent(QTimerEvent *event)
|
2022-11-05 08:26:58 +02:00
|
|
|
{
|
2022-11-05 12:24:51 +02:00
|
|
|
if (event->timerId() == d->timerid) {
|
2022-11-15 01:57:27 +02:00
|
|
|
// kDebug(240) << "Checking owner for" << d->atomname;
|
2022-11-05 20:11:20 +02:00
|
|
|
Q_ASSERT(d->x11window != None);
|
2022-11-05 12:24:51 +02:00
|
|
|
Window currentowner = XGetSelectionOwner(d->x11display, d->x11atom);
|
|
|
|
if (currentowner != d->x11window) {
|
2022-11-15 01:57:27 +02:00
|
|
|
kDebug(240) << d->atomname << "owner changed";
|
2022-11-05 12:24:51 +02:00
|
|
|
killTimer(d->timerid);
|
|
|
|
d->timerid = 0;
|
|
|
|
emit lostOwnership();
|
2022-11-05 11:14:15 +02:00
|
|
|
}
|
2022-11-05 12:24:51 +02:00
|
|
|
event->accept();
|
|
|
|
} else {
|
|
|
|
event->ignore();
|
2022-11-05 08:26:58 +02:00
|
|
|
}
|
|
|
|
}
|