2 * Copyright (c) 2004 Lubos Lunak <l.lunak@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "detectwidget.h"
22 #include <kapplication.h>
25 #include <kwindowsystem.h>
27 #include <QRadioButton>
30 #include <QMouseEvent>
35 #include <X11/Xatom.h>
36 #include <X11/Xutil.h>
43 DetectWidget::DetectWidget( QWidget
* parent
)
49 DetectDialog::DetectDialog( QWidget
* parent
, const char* name
)
53 setObjectName( name
);
55 setButtons( Ok
| Cancel
);
57 widget
= new DetectWidget( this );
58 setMainWidget( widget
);
61 void DetectDialog::detect( WId window
)
69 void DetectDialog::readWindow( WId w
)
73 emit
detectionDone( false );
76 info
= KWindowSystem::windowInfo( w
, -1U, -1U ); // read everything
79 emit
detectionDone( false );
82 wmclass_class
= info
.windowClassClass();
83 wmclass_name
= info
.windowClassName();
84 role
= info
.windowRole();
85 type
= info
.windowType( NET::NormalMask
| NET::DesktopMask
| NET::DockMask
86 | NET::ToolbarMask
| NET::MenuMask
| NET::DialogMask
| NET::OverrideMask
| NET::TopMenuMask
87 | NET::UtilityMask
| NET::SplashMask
);
89 extrarole
= ""; // TODO
90 machine
= info
.clientMachine();
94 void DetectDialog::executeDialog()
96 static const char* const types
[] =
98 I18N_NOOP( "Normal Window" ),
99 I18N_NOOP( "Desktop" ),
100 I18N_NOOP( "Dock (panel)" ),
101 I18N_NOOP( "Toolbar" ),
102 I18N_NOOP( "Torn-Off Menu" ),
103 I18N_NOOP( "Dialog Window" ),
104 I18N_NOOP( "Override Type" ),
105 I18N_NOOP( "Standalone Menubar" ),
106 I18N_NOOP( "Utility Window" ),
107 I18N_NOOP( "Splash Screen" )
109 widget
->class_label
->setText( wmclass_class
+ " (" + wmclass_name
+ ' ' + wmclass_class
+ ')' );
110 widget
->role_label
->setText( role
);
111 widget
->use_role
->setEnabled( !role
.isEmpty());
112 if( widget
->use_role
->isEnabled())
113 widget
->use_role
->setChecked( true );
115 widget
->use_whole_class
->setChecked( true );
116 if( type
== NET::Unknown
)
117 widget
->type_label
->setText( i18n( "Unknown - will be treated as Normal Window" ));
119 widget
->type_label
->setText( i18n( types
[ type
] ));
120 widget
->title_label
->setText( title
);
121 widget
->extrarole_label
->setText( extrarole
);
122 widget
->machine_label
->setText( machine
);
123 emit
detectionDone( exec() == KDialog::Accepted
);
126 QByteArray
DetectDialog::selectedClass() const
128 if( widget
->use_class
->isChecked() || widget
->use_role
->isChecked())
129 return wmclass_class
;
130 return wmclass_name
+ ' ' + wmclass_class
;
133 bool DetectDialog::selectedWholeClass() const
135 return widget
->use_whole_class
->isChecked();
138 QByteArray
DetectDialog::selectedRole() const
140 if( widget
->use_role
->isChecked())
145 QString
DetectDialog::selectedTitle() const
150 Rules::StringMatch
DetectDialog::titleMatch() const
152 return widget
->match_title
->isChecked() ? Rules::ExactMatch
: Rules::UnimportantMatch
;
155 bool DetectDialog::selectedWholeApp() const
157 return widget
->use_class
->isChecked();
160 NET::WindowType
DetectDialog::selectedType() const
165 QByteArray
DetectDialog::selectedMachine() const
170 void DetectDialog::selectWindow()
172 // use a dialog, so that all user input is blocked
173 // use WX11BypassWM and moving away so that it's not actually visible
174 // grab only mouse, so that keyboard can be used e.g. for switching windows
175 grabber
= new KDialog( 0, Qt::X11BypassWindowManagerHint
);
176 grabber
->move( -1000, -1000 );
177 grabber
->setModal( true );
179 grabber
->grabMouse( Qt::CrossCursor
);
180 grabber
->installEventFilter( this );
183 bool DetectDialog::eventFilter( QObject
* o
, QEvent
* e
)
187 if( e
->type() != QEvent::MouseButtonRelease
)
191 if( static_cast< QMouseEvent
* >( e
)->button() != Qt::LeftButton
)
193 emit
detectionDone( false );
196 readWindow( findWindow());
200 WId
DetectDialog::findWindow()
205 int rootX
, rootY
, x
, y
;
206 Window parent
= QX11Info::appRootWindow();
207 Atom wm_state
= XInternAtom( QX11Info::display(), "WM_STATE", False
);
212 XQueryPointer( QX11Info::display(), parent
, &root
, &child
,
213 &rootX
, &rootY
, &x
, &y
, &mask
);
218 unsigned long nitems
, after
;
220 if( XGetWindowProperty( QX11Info::display(), child
, wm_state
, 0, 0, False
, AnyPropertyType
,
221 &type
, &format
, &nitems
, &after
, &prop
) == Success
)
235 #include "detectwidget.moc"