2009-09-13 16:43:26 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2009-09-15 07:12:54 +00:00
|
|
|
// oxygendetectwidget.cpp
|
2009-09-13 16:43:26 +00:00
|
|
|
// Note: this class is a stripped down version of
|
|
|
|
// /kdebase/workspace/kwin/kcmkwin/kwinrules/detectwidget.cpp
|
|
|
|
// Copyright (c) 2004 Lubos Lunak <l.lunak@kde.org>
|
|
|
|
// -------------------
|
|
|
|
//
|
|
|
|
// Copyright (c) 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-09-16 06:09:31 +00:00
|
|
|
#include "oxygendetectwidget.h"
|
|
|
|
#include "oxygendetectwidget.moc"
|
|
|
|
|
2013-02-26 08:00:51 +00:00
|
|
|
#include <QButtonGroup>
|
|
|
|
#include <QLayout>
|
|
|
|
#include <QGroupBox>
|
2013-08-01 15:04:58 +00:00
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QPushButton>
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2013-08-02 06:37:36 +00:00
|
|
|
#include <QX11Info>
|
2013-10-18 19:18:59 +00:00
|
|
|
#include <xcb/xcb.h>
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2009-09-15 07:12:54 +00:00
|
|
|
namespace Oxygen
|
2009-09-13 16:43:26 +00:00
|
|
|
{
|
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
//_________________________________________________________
|
|
|
|
DetectDialog::DetectDialog( QWidget* parent ):
|
2013-08-01 15:04:58 +00:00
|
|
|
QDialog( parent ),
|
2013-10-18 19:18:59 +00:00
|
|
|
_grabber( 0 ),
|
|
|
|
_wmStateAtom( 0 )
|
2010-05-02 18:34:08 +00:00
|
|
|
{
|
2009-10-29 02:55:41 +00:00
|
|
|
|
2013-08-01 15:04:58 +00:00
|
|
|
// setup
|
|
|
|
setupUi( this );
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2013-08-01 15:04:58 +00:00
|
|
|
connect( buttonBox->button( QDialogButtonBox::Cancel ), SIGNAL(clicked()), this, SLOT(close()) );
|
|
|
|
windowClassCheckBox->setChecked( true );
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2013-10-18 19:18:59 +00:00
|
|
|
// create atom
|
|
|
|
xcb_connection_t* connection( QX11Info::connection() );
|
2013-11-11 09:04:33 +00:00
|
|
|
const QString atomName( QStringLiteral( "WM_STATE" ) );
|
2013-10-18 19:18:59 +00:00
|
|
|
xcb_intern_atom_cookie_t cookie( xcb_intern_atom( connection, false, atomName.size(), qPrintable( atomName ) ) );
|
2013-11-11 09:04:33 +00:00
|
|
|
QScopedPointer<xcb_intern_atom_reply_t, QScopedPointerPodDeleter> reply( xcb_intern_atom_reply( connection, cookie, nullptr) );
|
|
|
|
_wmStateAtom = reply ? reply->atom : 0;
|
2013-10-18 19:18:59 +00:00
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
}
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
//_________________________________________________________
|
|
|
|
void DetectDialog::detect( WId window )
|
2009-09-13 16:43:26 +00:00
|
|
|
{
|
2010-05-02 18:34:08 +00:00
|
|
|
if( window == 0 ) selectWindow();
|
|
|
|
else readWindow( window );
|
2009-09-13 16:43:26 +00:00
|
|
|
}
|
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
//_________________________________________________________
|
|
|
|
void DetectDialog::readWindow( WId window )
|
2009-09-13 16:43:26 +00:00
|
|
|
{
|
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
if( window == 0 )
|
|
|
|
{
|
|
|
|
emit detectionDone( false );
|
|
|
|
return;
|
|
|
|
}
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2011-02-24 16:47:38 +00:00
|
|
|
_info = KWindowSystem::windowInfo( window, -1U, -1U );
|
|
|
|
if( !_info.valid())
|
2010-05-02 18:34:08 +00:00
|
|
|
{
|
|
|
|
emit detectionDone( false );
|
|
|
|
return;
|
|
|
|
}
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2013-08-01 15:04:58 +00:00
|
|
|
const QString wmClassClass( QString::fromUtf8( _info.windowClassClass() ) );
|
|
|
|
const QString wmClassName( QString::fromUtf8( _info.windowClassName() ) );
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2013-08-01 15:04:58 +00:00
|
|
|
windowClass->setText( QStringLiteral( "%1 (%2 %3)" ).arg( wmClassClass ).arg( wmClassName ).arg( wmClassClass ) );
|
|
|
|
Ui::OxygenDetectWidget::windowTitle->setText( _info.name() );
|
|
|
|
emit detectionDone( exec() == QDialog::Accepted );
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
return;
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
}
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
//_________________________________________________________
|
|
|
|
void DetectDialog::selectWindow()
|
|
|
|
{
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
// use a dialog, so that all user input is blocked
|
|
|
|
// use WX11BypassWM and moving away so that it's not actually visible
|
|
|
|
// grab only mouse, so that keyboard can be used e.g. for switching windows
|
2013-08-01 15:04:58 +00:00
|
|
|
_grabber = new QDialog( 0, Qt::X11BypassWindowManagerHint );
|
2011-02-24 16:47:38 +00:00
|
|
|
_grabber->move( -1000, -1000 );
|
|
|
|
_grabber->setModal( true );
|
|
|
|
_grabber->show();
|
2013-11-15 15:53:31 +00:00
|
|
|
|
|
|
|
// need to explicitly override cursor for Qt5
|
|
|
|
qApp->setOverrideCursor( Qt::CrossCursor );
|
2011-02-24 16:47:38 +00:00
|
|
|
_grabber->grabMouse( Qt::CrossCursor );
|
|
|
|
_grabber->installEventFilter( this );
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
}
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
//_________________________________________________________
|
|
|
|
bool DetectDialog::eventFilter( QObject* o, QEvent* e )
|
|
|
|
{
|
|
|
|
// check object and event type
|
2011-02-24 16:47:38 +00:00
|
|
|
if( o != _grabber ) return false;
|
2010-05-02 18:34:08 +00:00
|
|
|
if( e->type() != QEvent::MouseButtonRelease ) return false;
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2013-11-15 15:53:31 +00:00
|
|
|
// need to explicitely release cursor for Qt5
|
|
|
|
qApp->restoreOverrideCursor();
|
|
|
|
|
2011-02-24 16:47:38 +00:00
|
|
|
// delete old _grabber
|
|
|
|
delete _grabber;
|
|
|
|
_grabber = 0;
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
// check button
|
|
|
|
if( static_cast< QMouseEvent* >( e )->button() != Qt::LeftButton ) return true;
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
// read window information
|
|
|
|
readWindow( findWindow() );
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
//_________________________________________________________
|
|
|
|
WId DetectDialog::findWindow()
|
2009-09-13 16:43:26 +00:00
|
|
|
{
|
|
|
|
|
2013-10-18 19:18:59 +00:00
|
|
|
// check atom
|
|
|
|
if( !_wmStateAtom ) return 0;
|
|
|
|
|
|
|
|
xcb_connection_t* connection( QX11Info::connection() );
|
|
|
|
xcb_window_t parent( QX11Info::appRootWindow() );
|
2010-05-02 18:34:08 +00:00
|
|
|
|
|
|
|
// why is there a loop of only 10 here
|
|
|
|
for( int i = 0; i < 10; ++i )
|
|
|
|
{
|
2013-10-18 19:18:59 +00:00
|
|
|
|
|
|
|
// query pointer
|
2013-11-11 09:04:33 +00:00
|
|
|
xcb_query_pointer_cookie_t pointerCookie( xcb_query_pointer( connection, parent ) );
|
|
|
|
QScopedPointer<xcb_query_pointer_reply_t, QScopedPointerPodDeleter> pointerReply( xcb_query_pointer_reply( connection, pointerCookie, nullptr ) );
|
2013-10-18 19:18:59 +00:00
|
|
|
if( !( pointerReply && pointerReply->child ) ) return 0;
|
|
|
|
|
|
|
|
const xcb_window_t child( pointerReply->child );
|
2013-11-11 09:04:33 +00:00
|
|
|
xcb_get_property_cookie_t cookie( xcb_get_property( connection, 0, child, _wmStateAtom, XCB_GET_PROPERTY_TYPE_ANY, 0, 0 ) );
|
|
|
|
QScopedPointer<xcb_get_property_reply_t, QScopedPointerPodDeleter> reply( xcb_get_property_reply( connection, cookie, nullptr ) );
|
2013-10-18 19:18:59 +00:00
|
|
|
if( reply && reply->type ) return child;
|
|
|
|
else parent = child;
|
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-09-13 16:43:26 +00:00
|
|
|
|
2010-05-02 18:34:08 +00:00
|
|
|
}
|
2009-09-13 16:43:26 +00:00
|
|
|
|
|
|
|
}
|