2014-11-13 19:30:51 +02:00
/****************************************************************************
Copyright ( C ) 2003 Lubos Lunak < l . lunak @ kde . org >
Permission is hereby granted , free of charge , to any person obtaining a
copy of this software and associated documentation files ( the " Software " ) ,
to deal in the Software without restriction , including without limitation
the rights to use , copy , modify , merge , publish , distribute , sublicense ,
and / or sell copies of the Software , and to permit persons to whom the
Software is furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING
FROM , OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include <kcmdlineargs.h>
# include <kapplication.h>
# include <kmessagebox.h>
2015-05-20 13:39:58 +00:00
# include <KIcon>
# include <KLocalizedString>
2014-11-13 19:30:51 +02:00
# include <kdebug.h>
2022-05-24 12:39:19 +03:00
# include <QProcess>
2014-11-13 19:30:51 +02:00
# include <X11/Xlib.h>
2015-08-12 13:11:16 +03:00
# include <QtGui/qx11info_x11.h>
2022-05-24 12:39:19 +03:00
# include <unistd.h>
2014-11-13 19:30:51 +02:00
# include <signal.h>
# include <errno.h>
int main ( int argc , char * argv [ ] )
{
KCmdLineArgs : : init ( argc , argv , " kwin_killer_helper " , " kwin " , ki18n ( " Window Manager " ) , " 1.0 " ,
ki18n ( " KWin helper utility " ) ) ;
KCmdLineOptions options ;
options . add ( " pid <pid> " , ki18n ( " PID of the application to terminate " ) ) ;
options . add ( " hostname <hostname> " , ki18n ( " Hostname on which the application is running " ) ) ;
options . add ( " windowname <caption> " , ki18n ( " Caption of the window to be terminated " ) ) ;
options . add ( " applicationname <name> " , ki18n ( " Name of the application to be terminated " ) ) ;
options . add ( " wid <id> " , ki18n ( " ID of resource belonging to the application " ) ) ;
options . add ( " timestamp <time> " , ki18n ( " Time of user action causing termination " ) ) ;
KCmdLineArgs : : addCmdLineOptions ( options ) ;
KApplication app ;
KApplication : : setWindowIcon ( KIcon ( " kwin " ) ) ;
KCmdLineArgs * args = KCmdLineArgs : : parsedArgs ( ) ;
QString hostname = args - > getOption ( " hostname " ) ;
bool pid_ok = false ;
2023-06-12 00:22:13 +03:00
pid_t pid = args - > getOption ( " pid " ) . toULong ( & pid_ok ) ;
2014-11-13 19:30:51 +02:00
QString caption = args - > getOption ( " windowname " ) ;
QString appname = args - > getOption ( " applicationname " ) ;
bool id_ok = false ;
2023-06-12 00:22:13 +03:00
Window id = args - > getOption ( " wid " ) . toULong ( & id_ok ) ;
2014-11-13 19:30:51 +02:00
bool time_ok = false ;
2023-06-12 00:22:13 +03:00
Time timestamp = args - > getOption ( " timestamp " ) . toULong ( & time_ok ) ;
2014-11-13 19:30:51 +02:00
args - > clear ( ) ;
if ( ! pid_ok | | pid = = 0 | | ! id_ok | | id = = None | | ! time_ok | | timestamp = = CurrentTime
| | hostname . isEmpty ( ) | | caption . isEmpty ( ) | | appname . isEmpty ( ) ) {
KCmdLineArgs : : usageError ( i18n ( " This helper utility is not supposed to be called directly. " ) ) ;
return 1 ;
}
bool isLocal = hostname = = " localhost " ;
caption = Qt : : escape ( caption ) ;
appname = Qt : : escape ( appname ) ;
hostname = Qt : : escape ( hostname ) ;
QString pidString = QString : : number ( pid ) ; // format pid ourself as it does not make sense to format an ID according to locale settings
QString question = i18nc ( " @info " , " <b>Application \" %1 \" is not responding</b> " , appname ) ;
question + = isLocal
2023-08-18 00:36:52 +03:00
? i18nc ( " @info " , " <p>You tried to close window \" %1 \" from application \" %2 \" (Process ID: %3) but the application is not responding.</p> " ,
2014-11-13 19:30:51 +02:00
caption , appname , pidString )
2023-08-18 00:36:52 +03:00
: i18nc ( " @info " , " <p>You tried to close window \" %1 \" from application \" %2 \" (Process ID: %3), running on host \" %4 \" , but the application is not responding.</p> " ,
2014-11-13 19:30:51 +02:00
caption , appname , pidString , hostname ) ;
question + = i18nc ( " @info " ,
2023-08-18 00:36:52 +03:00
" <p>Do you want to terminate this application?</p> "
" <p><warning>Terminating the application will close all of its child windows. Any unsaved data will be lost.</warning></p> "
2014-11-13 19:30:51 +02:00
) ;
KGuiItem continueButton = KGuiItem ( i18n ( " &Terminate Application %1 " , appname ) , " edit-bomb " ) ;
KGuiItem cancelButton = KGuiItem ( i18n ( " Wait Longer " ) , " chronometer " ) ;
app . updateUserTimestamp ( timestamp ) ;
if ( KMessageBox : : warningContinueCancelWId ( id , question , QString ( ) , continueButton , cancelButton ) = = KMessageBox : : Continue ) {
if ( ! isLocal ) {
QStringList lst ;
lst < < hostname < < " kill " < < QString : : number ( pid ) ;
QProcess : : startDetached ( " xon " , lst ) ;
} else {
2022-05-24 12:39:19 +03:00
if ( : : kill ( pid , SIGKILL ) = = - 1 ) {
kWarning ( 1212 ) < < " KWin process killer failed " ;
2014-11-13 19:30:51 +02:00
}
}
}
2022-05-24 12:39:19 +03:00
return 0 ;
2014-11-13 19:30:51 +02:00
}