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.
506 lines
14 KiB
506 lines
14 KiB
15 years ago
|
/* This file is part of the KDE libraries
|
||
|
Copyright (c) 1999 Preston Brown <pbrown@kde.org>
|
||
|
|
||
|
$Id$
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
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 <config.h>
|
||
|
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/wait.h>
|
||
|
|
||
|
#include <assert.h>
|
||
|
#include <errno.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
15 years ago
|
#include <tqfile.h>
|
||
|
#include <tqptrlist.h>
|
||
|
#include <tqtimer.h>
|
||
15 years ago
|
|
||
|
#include <dcopclient.h>
|
||
12 years ago
|
#include <tdecmdlineargs.h>
|
||
15 years ago
|
#include <kstandarddirs.h>
|
||
12 years ago
|
#include <tdeaboutdata.h>
|
||
15 years ago
|
|
||
10 months ago
|
#if defined TQ_WS_X11
|
||
13 years ago
|
#include <twin.h>
|
||
12 years ago
|
#include <tdestartupinfo.h>
|
||
15 years ago
|
#endif
|
||
|
|
||
12 years ago
|
#include <tdeconfig.h>
|
||
15 years ago
|
#include "kdebug.h"
|
||
1 month ago
|
#include "tdeuniqueapplication.h"
|
||
15 years ago
|
|
||
10 months ago
|
#if defined TQ_WS_X11
|
||
15 years ago
|
#include <netwm.h>
|
||
|
#include <X11/Xlib.h>
|
||
|
#define DISPLAY "DISPLAY"
|
||
|
#else
|
||
10 months ago
|
# ifdef TQ_WS_QWS
|
||
15 years ago
|
# define DISPLAY "QWS_DISPLAY"
|
||
|
# else
|
||
|
# define DISPLAY "DISPLAY"
|
||
|
# endif
|
||
|
#endif
|
||
|
|
||
1 month ago
|
bool TDEUniqueApplication::s_nofork = false;
|
||
|
bool TDEUniqueApplication::s_multipleInstances = false;
|
||
|
bool TDEUniqueApplication::s_uniqueTestDone = false;
|
||
|
bool TDEUniqueApplication::s_handleAutoStarted = false;
|
||
15 years ago
|
|
||
1 month ago
|
static TDECmdLineOptions tdeunique_options[] =
|
||
15 years ago
|
{
|
||
|
{ "nofork", "Don't run in the background.", 0 },
|
||
12 years ago
|
TDECmdLineLastOption
|
||
15 years ago
|
};
|
||
|
|
||
|
struct DCOPRequest {
|
||
15 years ago
|
TQCString fun;
|
||
|
TQByteArray data;
|
||
15 years ago
|
DCOPClientTransaction *transaction;
|
||
|
};
|
||
|
|
||
1 month ago
|
class TDEUniqueApplicationPrivate {
|
||
15 years ago
|
public:
|
||
15 years ago
|
TQPtrList <DCOPRequest> requestList;
|
||
15 years ago
|
bool processingRequest;
|
||
|
bool firstInstance;
|
||
|
};
|
||
|
|
||
|
void
|
||
1 month ago
|
TDEUniqueApplication::addCmdLineOptions()
|
||
15 years ago
|
{
|
||
1 month ago
|
TDECmdLineArgs::addCmdLineOptions(tdeunique_options, 0, "tdeuniqueapp", "tde" );
|
||
15 years ago
|
}
|
||
|
|
||
|
bool
|
||
1 month ago
|
TDEUniqueApplication::start()
|
||
15 years ago
|
{
|
||
|
if( s_uniqueTestDone )
|
||
|
return true;
|
||
|
s_uniqueTestDone = true;
|
||
|
addCmdLineOptions(); // Make sure to add cmd line options
|
||
10 months ago
|
#ifdef TQ_WS_WIN
|
||
15 years ago
|
s_nofork = true;
|
||
|
#else
|
||
1 month ago
|
TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs("tdeuniqueapp");
|
||
15 years ago
|
s_nofork = !args->isSet("fork");
|
||
|
delete args;
|
||
|
#endif
|
||
|
|
||
12 years ago
|
TQCString appName = TDECmdLineArgs::about->appName();
|
||
15 years ago
|
|
||
|
if (s_nofork)
|
||
|
{
|
||
|
if (s_multipleInstances)
|
||
|
{
|
||
15 years ago
|
TQCString pid;
|
||
15 years ago
|
pid.setNum(getpid());
|
||
|
appName = appName + "-" + pid;
|
||
|
}
|
||
|
|
||
|
// Check to make sure that we're actually able to register with the DCOP
|
||
|
// server.
|
||
|
|
||
10 months ago
|
#ifndef TQ_WS_WIN //TODO
|
||
15 years ago
|
if(dcopClient()->registerAs(appName, false).isEmpty()) {
|
||
|
startKdeinit();
|
||
|
if(dcopClient()->registerAs(appName, false).isEmpty()) {
|
||
1 month ago
|
kdError() << "TDEUniqueApplication: Can't setup DCOP communication." << endl;
|
||
15 years ago
|
::exit(255);
|
||
|
}
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
// We'll call newInstance in the constructor. Do nothing here.
|
||
|
return true;
|
||
|
}
|
||
|
DCOPClient *dc;
|
||
|
int fd[2];
|
||
|
signed char result;
|
||
|
if (0 > pipe(fd))
|
||
|
{
|
||
1 month ago
|
kdError() << "TDEUniqueApplication: pipe() failed!" << endl;
|
||
15 years ago
|
::exit(255);
|
||
|
}
|
||
|
int fork_result = fork();
|
||
|
switch(fork_result) {
|
||
|
case -1:
|
||
1 month ago
|
kdError() << "TDEUniqueApplication: fork() failed!" << endl;
|
||
15 years ago
|
::exit(255);
|
||
|
break;
|
||
|
case 0:
|
||
|
// Child
|
||
|
::close(fd[0]);
|
||
|
if (s_multipleInstances)
|
||
15 years ago
|
appName.append("-").append(TQCString().setNum(getpid()));
|
||
15 years ago
|
dc = dcopClient();
|
||
|
{
|
||
15 years ago
|
TQCString regName = dc->registerAs(appName, false);
|
||
15 years ago
|
if (regName.isEmpty())
|
||
|
{
|
||
|
// Check DISPLAY
|
||
15 years ago
|
if (TQCString(getenv(DISPLAY)).isEmpty())
|
||
15 years ago
|
{
|
||
1 month ago
|
kdError() << "TDEUniqueApplication: Can't determine DISPLAY. Aborting." << endl;
|
||
15 years ago
|
result = -1; // Error
|
||
|
::write(fd[1], &result, 1);
|
||
|
::exit(255);
|
||
|
}
|
||
|
|
||
13 years ago
|
// Try to launch tdeinit.
|
||
15 years ago
|
startKdeinit();
|
||
|
regName = dc->registerAs(appName, false);
|
||
|
if (regName.isEmpty())
|
||
|
{
|
||
1 month ago
|
kdError() << "TDEUniqueApplication: Can't setup DCOP communication." << endl;
|
||
15 years ago
|
result = -1;
|
||
|
delete dc; // Clean up DCOP commmunication
|
||
|
::write(fd[1], &result, 1);
|
||
|
::exit(255);
|
||
|
}
|
||
|
}
|
||
|
if (regName != appName)
|
||
|
{
|
||
|
// Already running. Ok.
|
||
|
result = 0;
|
||
|
delete dc; // Clean up DCOP commmunication
|
||
|
::write(fd[1], &result, 1);
|
||
|
::close(fd[1]);
|
||
|
#if 0
|
||
10 months ago
|
#ifdef TQ_WS_X11
|
||
15 years ago
|
// say we're up and running ( probably no new window will appear )
|
||
12 years ago
|
TDEStartupInfoId id;
|
||
12 years ago
|
if( kapp != NULL ) // TDEApplication constructor unsets the env. variable
|
||
15 years ago
|
id.initId( kapp->startupId());
|
||
|
else
|
||
12 years ago
|
id = TDEStartupInfo::currentStartupIdEnv();
|
||
15 years ago
|
if( !id.none())
|
||
|
{
|
||
|
Display* disp = XOpenDisplay( NULL );
|
||
|
if( disp != NULL ) // use extra X connection
|
||
|
{
|
||
12 years ago
|
TDEStartupInfo::sendFinishX( disp, id );
|
||
15 years ago
|
XCloseDisplay( disp );
|
||
|
}
|
||
|
}
|
||
|
#else //FIXME(E): implement
|
||
|
#endif
|
||
|
#endif
|
||
|
return false;
|
||
|
}
|
||
|
dc->setPriorityCall(true);
|
||
|
}
|
||
|
|
||
|
{
|
||
10 months ago
|
#ifdef TQ_WS_X11
|
||
12 years ago
|
TDEStartupInfoId id;
|
||
12 years ago
|
if( kapp != NULL ) // TDEApplication constructor unsets the env. variable
|
||
15 years ago
|
id.initId( kapp->startupId());
|
||
|
else
|
||
12 years ago
|
id = TDEStartupInfo::currentStartupIdEnv();
|
||
15 years ago
|
if( !id.none())
|
||
|
{ // notice about pid change
|
||
|
Display* disp = XOpenDisplay( NULL );
|
||
|
if( disp != NULL ) // use extra X connection
|
||
|
{
|
||
12 years ago
|
TDEStartupInfoData data;
|
||
15 years ago
|
data.addPid( getpid());
|
||
12 years ago
|
TDEStartupInfo::sendChangeX( disp, id, data );
|
||
15 years ago
|
XCloseDisplay( disp );
|
||
|
}
|
||
|
}
|
||
|
#else //FIXME(E): Implement
|
||
|
#endif
|
||
|
}
|
||
|
result = 0;
|
||
|
::write(fd[1], &result, 1);
|
||
|
::close(fd[1]);
|
||
|
return true; // Finished.
|
||
|
default:
|
||
|
// Parent
|
||
|
// DCOPClient::emergencyClose();
|
||
|
// dcopClient()->detach();
|
||
|
if (s_multipleInstances)
|
||
15 years ago
|
appName.append("-").append(TQCString().setNum(fork_result));
|
||
15 years ago
|
::close(fd[1]);
|
||
|
for(;;)
|
||
|
{
|
||
|
int n = ::read(fd[0], &result, 1);
|
||
|
if (n == 1) break;
|
||
|
if (n == 0)
|
||
|
{
|
||
1 month ago
|
kdError() << "TDEUniqueApplication: Pipe closed unexpectedly." << endl;
|
||
15 years ago
|
::exit(255);
|
||
|
}
|
||
|
if (errno != EINTR)
|
||
|
{
|
||
1 month ago
|
kdError() << "TDEUniqueApplication: Error reading from pipe." << endl;
|
||
15 years ago
|
::exit(255);
|
||
|
}
|
||
|
}
|
||
|
::close(fd[0]);
|
||
|
|
||
|
if (result != 0)
|
||
|
::exit(result); // Error occurred in child.
|
||
|
|
||
|
dc = new DCOPClient();
|
||
|
if (!dc->attach())
|
||
|
{
|
||
1 month ago
|
kdError() << "TDEUniqueApplication: Parent process can't attach to DCOP." << endl;
|
||
15 years ago
|
delete dc; // Clean up DCOP commmunication
|
||
|
::exit(255);
|
||
|
}
|
||
|
if (!dc->isApplicationRegistered(appName)) {
|
||
1 month ago
|
kdError() << "TDEUniqueApplication: Registering failed!" << endl;
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQCString new_asn_id;
|
||
10 months ago
|
#if defined TQ_WS_X11
|
||
12 years ago
|
TDEStartupInfoId id;
|
||
12 years ago
|
if( kapp != NULL ) // TDEApplication constructor unsets the env. variable
|
||
15 years ago
|
id.initId( kapp->startupId());
|
||
|
else
|
||
12 years ago
|
id = TDEStartupInfo::currentStartupIdEnv();
|
||
15 years ago
|
if( !id.none())
|
||
|
new_asn_id = id.id();
|
||
|
#endif
|
||
|
|
||
15 years ago
|
TQByteArray data, reply;
|
||
|
TQDataStream ds(data, IO_WriteOnly);
|
||
15 years ago
|
|
||
12 years ago
|
TDECmdLineArgs::saveAppArgs(ds);
|
||
15 years ago
|
ds << new_asn_id;
|
||
|
|
||
|
dc->setPriorityCall(true);
|
||
15 years ago
|
TQCString replyType;
|
||
12 years ago
|
if (!dc->call(appName, TDECmdLineArgs::about->appName(), "newInstance()", data, replyType, reply))
|
||
15 years ago
|
{
|
||
12 years ago
|
kdError() << "Communication problem with " << TDECmdLineArgs::about->appName() << ", it probably crashed." << endl;
|
||
15 years ago
|
delete dc; // Clean up DCOP commmunication
|
||
|
::exit(255);
|
||
|
}
|
||
|
dc->setPriorityCall(false);
|
||
|
if (replyType != "int")
|
||
|
{
|
||
1 month ago
|
kdError() << "TDEUniqueApplication: DCOP communication error!" << endl;
|
||
15 years ago
|
delete dc; // Clean up DCOP commmunication
|
||
|
::exit(255);
|
||
|
}
|
||
15 years ago
|
TQDataStream rs(reply, IO_ReadOnly);
|
||
15 years ago
|
int exitCode;
|
||
|
rs >> exitCode;
|
||
|
delete dc; // Clean up DCOP commmunication
|
||
|
::exit(exitCode);
|
||
|
break;
|
||
|
}
|
||
|
return false; // make insure++ happy
|
||
|
}
|
||
|
|
||
|
|
||
1 month ago
|
TDEUniqueApplication::TDEUniqueApplication(bool allowStyles, bool GUIenabled, bool configUnique)
|
||
12 years ago
|
: TDEApplication( allowStyles, GUIenabled, initHack( configUnique )),
|
||
12 years ago
|
DCOPObject(TDECmdLineArgs::about->appName())
|
||
15 years ago
|
{
|
||
1 month ago
|
d = new TDEUniqueApplicationPrivate;
|
||
15 years ago
|
d->processingRequest = false;
|
||
|
d->firstInstance = true;
|
||
|
|
||
10 years ago
|
if (s_nofork) {
|
||
15 years ago
|
// Can't call newInstance directly from the constructor since it's virtual...
|
||
11 months ago
|
TQTimer::singleShot( 0, this, TQ_SLOT(newInstanceNoFork()) );
|
||
10 years ago
|
}
|
||
|
else {
|
||
|
// Force to handle DCOP requests (newInstance call)
|
||
11 months ago
|
TQTimer::singleShot( 0, this, TQ_SLOT(processDelayed()));
|
||
10 years ago
|
}
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
10 months ago
|
#ifdef TQ_WS_X11
|
||
1 month ago
|
TDEUniqueApplication::TDEUniqueApplication(Display *display, TQt::HANDLE visual,
|
||
1 year ago
|
TQt::HANDLE colormap, bool allowStyles, bool configUnique)
|
||
12 years ago
|
: TDEApplication( display, visual, colormap, allowStyles, initHack( configUnique )),
|
||
12 years ago
|
DCOPObject(TDECmdLineArgs::about->appName())
|
||
15 years ago
|
{
|
||
1 month ago
|
d = new TDEUniqueApplicationPrivate;
|
||
15 years ago
|
d->processingRequest = false;
|
||
|
d->firstInstance = true;
|
||
|
|
||
10 years ago
|
if (s_nofork) {
|
||
15 years ago
|
// Can't call newInstance directly from the constructor since it's virtual...
|
||
11 months ago
|
TQTimer::singleShot( 0, this, TQ_SLOT(newInstanceNoFork()) );
|
||
10 years ago
|
}
|
||
|
else {
|
||
|
// Force to handle DCOP requests (newInstance call)
|
||
11 months ago
|
TQTimer::singleShot( 0, this, TQ_SLOT(processDelayed()));
|
||
10 years ago
|
}
|
||
15 years ago
|
}
|
||
|
#endif
|
||
|
|
||
|
|
||
1 month ago
|
TDEUniqueApplication::~TDEUniqueApplication()
|
||
15 years ago
|
{
|
||
|
delete d;
|
||
|
}
|
||
|
|
||
15 years ago
|
// this gets called before even entering TQApplication::TQApplication()
|
||
1 month ago
|
TDEInstance* TDEUniqueApplication::initHack( bool configUnique )
|
||
15 years ago
|
{
|
||
12 years ago
|
TDEInstance* inst = new TDEInstance( TDECmdLineArgs::about );
|
||
15 years ago
|
if (configUnique)
|
||
|
{
|
||
12 years ago
|
TDEConfigGroupSaver saver( inst->config(), "KDE" );
|
||
15 years ago
|
s_multipleInstances = inst->config()->readBoolEntry("MultipleInstances", false);
|
||
|
}
|
||
|
if( !start())
|
||
|
// Already running
|
||
|
::exit( 0 );
|
||
|
return inst;
|
||
|
}
|
||
|
|
||
1 month ago
|
void TDEUniqueApplication::newInstanceNoFork()
|
||
15 years ago
|
{
|
||
|
if (dcopClient()->isSuspended())
|
||
|
{
|
||
|
// Try again later.
|
||
11 months ago
|
TQTimer::singleShot( 200, this, TQ_SLOT(newInstanceNoFork()) );
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
s_handleAutoStarted = false;
|
||
|
newInstance();
|
||
|
d->firstInstance = false;
|
||
10 months ago
|
#if defined TQ_WS_X11
|
||
15 years ago
|
// KDE4 remove
|
||
|
// A hack to make startup notification stop for apps which override newInstance()
|
||
|
// and reuse an already existing window there, but use KWin::activateWindow()
|
||
12 years ago
|
// instead of TDEStartupInfo::setNewStartupId(). Therefore KWin::activateWindow()
|
||
15 years ago
|
// for now sets this flag. Automatically ending startup notification always
|
||
|
// would cause problem if the new window would show up with a small delay.
|
||
|
if( s_handleAutoStarted )
|
||
12 years ago
|
TDEStartupInfo::handleAutoAppStartedSending();
|
||
15 years ago
|
#endif
|
||
|
// What to do with the return value ?
|
||
|
}
|
||
|
|
||
1 month ago
|
bool TDEUniqueApplication::process(const TQCString &fun, const TQByteArray &data,
|
||
15 years ago
|
TQCString &replyType, TQByteArray &replyData)
|
||
15 years ago
|
{
|
||
|
if (fun == "newInstance()")
|
||
|
{
|
||
|
delayRequest(fun, data);
|
||
|
return true;
|
||
|
} else
|
||
|
return DCOPObject::process(fun, data, replyType, replyData);
|
||
|
}
|
||
|
|
||
|
void
|
||
1 month ago
|
TDEUniqueApplication::delayRequest(const TQCString &fun, const TQByteArray &data)
|
||
15 years ago
|
{
|
||
|
DCOPRequest *request = new DCOPRequest;
|
||
|
request->fun = fun;
|
||
|
request->data = data;
|
||
|
request->transaction = dcopClient()->beginTransaction();
|
||
|
d->requestList.append(request);
|
||
|
if (!d->processingRequest)
|
||
|
{
|
||
11 months ago
|
TQTimer::singleShot(0, this, TQ_SLOT(processDelayed()));
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void
|
||
1 month ago
|
TDEUniqueApplication::processDelayed()
|
||
15 years ago
|
{
|
||
|
if (dcopClient()->isSuspended())
|
||
|
{
|
||
|
// Try again later.
|
||
11 months ago
|
TQTimer::singleShot( 200, this, TQ_SLOT(processDelayed()));
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
d->processingRequest = true;
|
||
|
while( !d->requestList.isEmpty() )
|
||
|
{
|
||
|
DCOPRequest *request = d->requestList.take(0);
|
||
15 years ago
|
TQByteArray replyData;
|
||
|
TQCString replyType;
|
||
15 years ago
|
if (request->fun == "newInstance()") {
|
||
|
dcopClient()->setPriorityCall(false);
|
||
15 years ago
|
TQDataStream ds(request->data, IO_ReadOnly);
|
||
12 years ago
|
TDECmdLineArgs::loadAppArgs(ds);
|
||
15 years ago
|
if( !ds.atEnd()) // backwards compatibility
|
||
|
{
|
||
15 years ago
|
TQCString asn_id;
|
||
15 years ago
|
ds >> asn_id;
|
||
|
setStartupId( asn_id );
|
||
|
}
|
||
|
s_handleAutoStarted = false;
|
||
|
int exitCode = newInstance();
|
||
|
d->firstInstance = false;
|
||
10 months ago
|
#if defined TQ_WS_X11
|
||
15 years ago
|
if( s_handleAutoStarted )
|
||
12 years ago
|
TDEStartupInfo::handleAutoAppStartedSending(); // KDE4 remove?
|
||
15 years ago
|
#endif
|
||
15 years ago
|
TQDataStream rs(replyData, IO_WriteOnly);
|
||
15 years ago
|
rs << exitCode;
|
||
|
replyType = "int";
|
||
|
}
|
||
|
dcopClient()->endTransaction( request->transaction, replyType, replyData);
|
||
|
delete request;
|
||
|
}
|
||
|
|
||
|
d->processingRequest = false;
|
||
|
}
|
||
|
|
||
1 month ago
|
bool TDEUniqueApplication::restoringSession()
|
||
15 years ago
|
{
|
||
|
return d->firstInstance && isRestored();
|
||
|
}
|
||
|
|
||
1 month ago
|
int TDEUniqueApplication::newInstance()
|
||
15 years ago
|
{
|
||
|
if (!d->firstInstance)
|
||
|
{
|
||
|
|
||
|
if ( mainWidget() )
|
||
|
{
|
||
|
mainWidget()->show();
|
||
10 months ago
|
#if defined TQ_WS_X11
|
||
15 years ago
|
// This is the line that handles window activation if necessary,
|
||
|
// and what's important, it does it properly. If you reimplement newInstance(),
|
||
|
// and don't call the inherited one, use this (but NOT when newInstance()
|
||
|
// is called for the first time, like here).
|
||
12 years ago
|
TDEStartupInfo::setNewStartupId( mainWidget(), kapp->startupId());
|
||
15 years ago
|
#endif
|
||
|
}
|
||
|
}
|
||
|
return 0; // do nothing in default implementation
|
||
|
}
|
||
|
|
||
1 month ago
|
void TDEUniqueApplication::setHandleAutoStarted()
|
||
15 years ago
|
{
|
||
|
s_handleAutoStarted = false;
|
||
|
}
|
||
|
|
||
1 month ago
|
void TDEUniqueApplication::virtual_hook( int id, void* data )
|
||
12 years ago
|
{ TDEApplication::virtual_hook( id, data );
|
||
15 years ago
|
DCOPObject::virtual_hook( id, data ); }
|
||
|
|
||
1 month ago
|
#include "tdeuniqueapplication.moc"
|