2004-06-21 16:25:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004 Lubos Lunak <l.lunak@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-12-21 06:45:09 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-06-21 16:25:24 +00:00
|
|
|
*/
|
|
|
|
|
2004-07-29 17:08:25 +00:00
|
|
|
#include <kcmdlineargs.h>
|
2004-06-21 16:25:24 +00:00
|
|
|
#include <kapplication.h>
|
2004-07-29 17:08:25 +00:00
|
|
|
#include <kconfig.h>
|
|
|
|
#include <klocale.h>
|
|
|
|
#include <kwin.h>
|
2006-07-02 18:31:00 +00:00
|
|
|
#include <QtDBus/QtDBus>
|
2007-02-23 14:38:01 +00:00
|
|
|
|
2004-07-29 17:08:25 +00:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <fixx11h.h>
|
2004-06-21 16:25:24 +00:00
|
|
|
|
2004-07-29 17:08:25 +00:00
|
|
|
#include "ruleswidget.h"
|
|
|
|
#include "../../rules.h"
|
2005-09-25 14:51:13 +00:00
|
|
|
#include <QByteArray>
|
2004-06-21 16:25:24 +00:00
|
|
|
|
|
|
|
namespace KWinInternal
|
|
|
|
{
|
|
|
|
|
2005-09-18 14:52:14 +00:00
|
|
|
static void loadRules( QList< Rules* >& rules )
|
2004-06-21 16:25:24 +00:00
|
|
|
{
|
2007-02-20 07:00:30 +00:00
|
|
|
KConfig _cfg( "kwinrulesrc" );
|
|
|
|
KConfigGroup cfg(&_cfg, "General" );
|
2006-01-21 19:36:31 +00:00
|
|
|
int count = cfg.readEntry( "count",0 );
|
2004-07-29 17:08:25 +00:00
|
|
|
for( int i = 1;
|
|
|
|
i <= count;
|
|
|
|
++i )
|
|
|
|
{
|
2007-02-20 07:00:30 +00:00
|
|
|
cfg.changeGroup( QString::number( i ));
|
2004-07-29 17:08:25 +00:00
|
|
|
Rules* rule = new Rules( cfg );
|
|
|
|
rules.append( rule );
|
|
|
|
}
|
2004-06-21 16:25:24 +00:00
|
|
|
}
|
|
|
|
|
2005-09-18 14:52:14 +00:00
|
|
|
static void saveRules( const QList< Rules* >& rules )
|
2004-06-21 16:25:24 +00:00
|
|
|
{
|
2004-07-29 17:08:25 +00:00
|
|
|
KConfig cfg( "kwinrulesrc" );
|
2006-02-06 20:44:10 +00:00
|
|
|
QStringList groups = cfg.groupList();
|
|
|
|
for( QStringList::ConstIterator it = groups.begin();
|
|
|
|
it != groups.end();
|
|
|
|
++it )
|
|
|
|
cfg.deleteGroup( *it );
|
2007-02-21 02:59:04 +00:00
|
|
|
cfg.group("General").writeEntry( "count", rules.count());
|
2004-07-29 17:08:25 +00:00
|
|
|
int i = 1;
|
2005-09-18 14:52:14 +00:00
|
|
|
for( QList< Rules* >::ConstIterator it = rules.begin();
|
2004-07-29 17:08:25 +00:00
|
|
|
it != rules.end();
|
|
|
|
++it )
|
|
|
|
{
|
2007-02-20 07:00:30 +00:00
|
|
|
KConfigGroup cg( &cfg, QString::number( i ));
|
|
|
|
(*it)->write( cg );
|
2004-07-29 17:08:25 +00:00
|
|
|
++i;
|
|
|
|
}
|
2004-06-21 16:25:24 +00:00
|
|
|
}
|
|
|
|
|
2005-09-18 14:52:14 +00:00
|
|
|
static Rules* findRule( const QList< Rules* >& rules, Window wid, bool whole_app )
|
2004-06-21 16:25:24 +00:00
|
|
|
{
|
2004-07-29 17:08:25 +00:00
|
|
|
KWin::WindowInfo info = KWin::windowInfo( wid,
|
|
|
|
NET::WMName | NET::WMWindowType,
|
|
|
|
NET::WM2WindowClass | NET::WM2WindowRole | NET::WM2ClientMachine );
|
|
|
|
if( !info.valid()) // shouldn't really happen
|
|
|
|
return NULL;
|
2006-03-17 18:18:20 +00:00
|
|
|
QByteArray wmclass_class = info.windowClassClass().toLower();
|
|
|
|
QByteArray wmclass_name = info.windowClassName().toLower();
|
|
|
|
QByteArray role = info.windowRole().toLower();
|
2004-07-29 17:08:25 +00:00
|
|
|
NET::WindowType type = info.windowType( NET::NormalMask | NET::DesktopMask | NET::DockMask
|
|
|
|
| NET::ToolbarMask | NET::MenuMask | NET::DialogMask | NET::OverrideMask | NET::TopMenuMask
|
|
|
|
| NET::UtilityMask | NET::SplashMask );
|
2004-09-13 08:41:00 +00:00
|
|
|
QString title = info.name();
|
2004-07-29 17:08:25 +00:00
|
|
|
// QCString extrarole = ""; // TODO
|
2006-03-17 18:18:20 +00:00
|
|
|
QByteArray machine = info.clientMachine().toLower();
|
2004-07-29 17:08:25 +00:00
|
|
|
Rules* best_match = NULL;
|
2004-11-05 15:29:50 +00:00
|
|
|
int match_quality = 0;
|
2005-09-18 14:52:14 +00:00
|
|
|
for( QList< Rules* >::ConstIterator it = rules.begin();
|
2004-07-29 17:08:25 +00:00
|
|
|
it != rules.end();
|
|
|
|
++it )
|
|
|
|
{
|
|
|
|
// try to find an exact match, i.e. not a generic rule
|
|
|
|
Rules* rule = *it;
|
|
|
|
int quality = 0;
|
|
|
|
bool generic = true;
|
|
|
|
if( rule->wmclassmatch != Rules::ExactMatch )
|
|
|
|
continue; // too generic
|
|
|
|
if( !rule->matchWMClass( wmclass_class, wmclass_name ))
|
|
|
|
continue;
|
|
|
|
// from now on, it matches the app - now try to match for a specific window
|
|
|
|
if( rule->wmclasscomplete )
|
|
|
|
{
|
|
|
|
quality += 1;
|
|
|
|
generic = false; // this can be considered specific enough (old X apps)
|
|
|
|
}
|
2005-08-16 17:46:42 +00:00
|
|
|
if( !whole_app )
|
2004-07-29 17:08:25 +00:00
|
|
|
{
|
2005-08-16 17:46:42 +00:00
|
|
|
if( rule->windowrolematch != Rules::UnimportantMatch )
|
|
|
|
{
|
|
|
|
quality += rule->windowrolematch == Rules::ExactMatch ? 5 : 1;
|
|
|
|
generic = false;
|
|
|
|
}
|
|
|
|
if( rule->titlematch != Rules::UnimportantMatch )
|
|
|
|
{
|
|
|
|
quality += rule->titlematch == Rules::ExactMatch ? 3 : 1;
|
|
|
|
generic = false;
|
|
|
|
}
|
|
|
|
if( rule->types != NET::AllTypesMask )
|
|
|
|
{
|
|
|
|
int bits = 0;
|
2006-04-09 09:23:16 +00:00
|
|
|
for( unsigned int bit = 1;
|
|
|
|
bit < 1U << 31;
|
2005-08-16 17:46:42 +00:00
|
|
|
bit <<= 1 )
|
|
|
|
if( rule->types & bit )
|
|
|
|
++bits;
|
|
|
|
if( bits == 1 )
|
|
|
|
quality += 2;
|
|
|
|
}
|
|
|
|
if( generic ) // ignore generic rules, use only the ones that are for this window
|
|
|
|
continue;
|
2004-07-29 17:08:25 +00:00
|
|
|
}
|
2005-08-16 17:46:42 +00:00
|
|
|
else
|
2004-07-29 17:08:25 +00:00
|
|
|
{
|
2005-08-16 17:46:42 +00:00
|
|
|
if( rule->types == NET::AllTypesMask )
|
2004-07-29 17:08:25 +00:00
|
|
|
quality += 2;
|
|
|
|
}
|
|
|
|
if( !rule->matchType( type )
|
|
|
|
|| !rule->matchRole( role )
|
|
|
|
|| !rule->matchTitle( title )
|
|
|
|
|| !rule->matchClientMachine( machine ))
|
|
|
|
continue;
|
|
|
|
if( quality > match_quality )
|
|
|
|
{
|
|
|
|
best_match = rule;
|
|
|
|
match_quality = quality;
|
|
|
|
}
|
|
|
|
}
|
2004-11-05 15:29:50 +00:00
|
|
|
if( best_match != NULL )
|
|
|
|
return best_match;
|
|
|
|
Rules* ret = new Rules;
|
2005-08-16 17:46:42 +00:00
|
|
|
if( whole_app )
|
|
|
|
{
|
2006-04-10 11:57:13 +00:00
|
|
|
ret->description = i18n( "Application settings for %1", QString::fromLatin1( wmclass_class ));
|
2005-08-16 17:46:42 +00:00
|
|
|
// TODO maybe exclude some types? If yes, then also exclude them above
|
|
|
|
// when searching.
|
2007-02-20 07:00:30 +00:00
|
|
|
ret->types = NET::AllTypesMask;
|
2005-08-16 17:46:42 +00:00
|
|
|
ret->titlematch = Rules::UnimportantMatch;
|
|
|
|
ret->clientmachine = machine; // set, but make unimportant
|
|
|
|
ret->clientmachinematch = Rules::UnimportantMatch;
|
|
|
|
ret->extrarolematch = Rules::UnimportantMatch;
|
|
|
|
ret->windowrolematch = Rules::UnimportantMatch;
|
|
|
|
if( wmclass_name == wmclass_class )
|
|
|
|
{
|
|
|
|
ret->wmclasscomplete = false;
|
|
|
|
ret->wmclass = wmclass_class;
|
|
|
|
ret->wmclassmatch = Rules::ExactMatch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// WM_CLASS components differ - perhaps the app got -name argument
|
|
|
|
ret->wmclasscomplete = true;
|
|
|
|
ret->wmclass = wmclass_name + ' ' + wmclass_class;
|
|
|
|
ret->wmclassmatch = Rules::ExactMatch;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2006-04-10 11:57:13 +00:00
|
|
|
ret->description = i18n( "Window settings for %1", QString::fromLatin1( wmclass_class ));
|
2004-11-05 15:29:50 +00:00
|
|
|
if( type == NET::Unknown )
|
|
|
|
ret->types = NET::NormalMask;
|
|
|
|
else
|
|
|
|
ret->types = 1 << type; // convert type to its mask
|
|
|
|
ret->title = title; // set, but make unimportant
|
|
|
|
ret->titlematch = Rules::UnimportantMatch;
|
2004-11-05 15:36:03 +00:00
|
|
|
ret->clientmachine = machine; // set, but make unimportant
|
2005-05-31 11:30:39 +00:00
|
|
|
ret->clientmachinematch = Rules::UnimportantMatch;
|
2004-11-05 15:29:50 +00:00
|
|
|
// ret->extrarole = extra; TODO
|
|
|
|
ret->extrarolematch = Rules::UnimportantMatch;
|
|
|
|
if( !role.isEmpty()
|
|
|
|
&& role != "unknown" && role != "unnamed" ) // Qt sets this if not specified
|
|
|
|
{
|
|
|
|
ret->windowrole = role;
|
|
|
|
ret->windowrolematch = Rules::ExactMatch;
|
2004-11-05 17:05:40 +00:00
|
|
|
if( wmclass_name == wmclass_class )
|
|
|
|
{
|
|
|
|
ret->wmclasscomplete = false;
|
|
|
|
ret->wmclass = wmclass_class;
|
|
|
|
ret->wmclassmatch = Rules::ExactMatch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// WM_CLASS components differ - perhaps the app got -name argument
|
|
|
|
ret->wmclasscomplete = true;
|
|
|
|
ret->wmclass = wmclass_name + ' ' + wmclass_class;
|
|
|
|
ret->wmclassmatch = Rules::ExactMatch;
|
|
|
|
}
|
2004-11-05 15:29:50 +00:00
|
|
|
}
|
|
|
|
else // no role set
|
|
|
|
{
|
|
|
|
if( wmclass_name != wmclass_class )
|
|
|
|
{
|
|
|
|
ret->wmclasscomplete = true;
|
|
|
|
ret->wmclass = wmclass_name + ' ' + wmclass_class;
|
|
|
|
ret->wmclassmatch = Rules::ExactMatch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This is a window that has no role set, and both components of WM_CLASS
|
|
|
|
// match (possibly only differing in case), which most likely means either
|
|
|
|
// the application doesn't give a damn about distinguishing its various
|
|
|
|
// windows, or it's an app that uses role for that, but this window
|
|
|
|
// lacks it for some reason. Use non-complete WM_CLASS matching, also
|
|
|
|
// include window title in the matching, and pray it causes many more positive
|
|
|
|
// matches than negative matches.
|
|
|
|
ret->titlematch = Rules::ExactMatch;
|
|
|
|
ret->wmclasscomplete = false;
|
|
|
|
ret->wmclass = wmclass_class;
|
|
|
|
ret->wmclassmatch = Rules::ExactMatch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2004-06-21 16:25:24 +00:00
|
|
|
}
|
|
|
|
|
2005-08-16 17:46:42 +00:00
|
|
|
static int edit( Window wid, bool whole_app )
|
2004-06-21 16:25:24 +00:00
|
|
|
{
|
2005-09-18 14:52:14 +00:00
|
|
|
QList< Rules* > rules;
|
2004-07-29 17:08:25 +00:00
|
|
|
loadRules( rules );
|
2005-08-16 17:46:42 +00:00
|
|
|
Rules* orig_rule = findRule( rules, wid, whole_app );
|
2004-07-29 17:08:25 +00:00
|
|
|
RulesDialog dlg;
|
2004-08-04 14:30:59 +00:00
|
|
|
// dlg.edit() creates new Rules instance if edited
|
2005-08-19 13:46:45 +00:00
|
|
|
Rules* edited_rule = dlg.edit( orig_rule, wid, true );
|
2004-11-05 15:29:50 +00:00
|
|
|
if( edited_rule == NULL || edited_rule->isEmpty())
|
2004-07-29 17:08:25 +00:00
|
|
|
{
|
2006-03-06 14:30:06 +00:00
|
|
|
rules.removeAll( orig_rule );
|
2004-11-05 15:29:50 +00:00
|
|
|
delete orig_rule;
|
|
|
|
if( orig_rule != edited_rule )
|
2004-08-04 14:26:34 +00:00
|
|
|
delete edited_rule;
|
|
|
|
}
|
2004-08-04 14:30:59 +00:00
|
|
|
else if( edited_rule != orig_rule )
|
2004-08-04 14:26:34 +00:00
|
|
|
{
|
2006-04-12 15:13:31 +00:00
|
|
|
int pos = rules.indexOf( orig_rule );
|
|
|
|
if( pos != -1)
|
|
|
|
rules[ pos ] = edited_rule;
|
2004-08-04 14:26:34 +00:00
|
|
|
else
|
|
|
|
rules.prepend( edited_rule );
|
|
|
|
delete orig_rule;
|
2004-07-29 17:08:25 +00:00
|
|
|
}
|
|
|
|
saveRules( rules );
|
2007-02-23 14:38:01 +00:00
|
|
|
QDBusInterface kwin( "org.kde.kwin", "/KWin", "org.kde.KWin" );
|
|
|
|
kwin.call( "reconfigure" );
|
2004-07-29 17:08:25 +00:00
|
|
|
return 0;
|
2004-06-21 16:25:24 +00:00
|
|
|
}
|
2007-02-20 07:00:30 +00:00
|
|
|
|
2004-07-29 17:08:25 +00:00
|
|
|
} // namespace
|
2004-06-21 16:25:24 +00:00
|
|
|
|
2004-07-29 17:08:25 +00:00
|
|
|
static const KCmdLineOptions options[] =
|
2004-06-21 16:25:24 +00:00
|
|
|
{
|
2004-07-29 17:08:25 +00:00
|
|
|
// no need for I18N_NOOP(), this is not supposed to be used directly
|
|
|
|
{ "wid <wid>", "WId of the window for special window settings.", 0 },
|
2005-08-16 17:46:42 +00:00
|
|
|
{ "whole-app", "Whether the settings should affect all windows of the application.", 0 },
|
2004-07-29 17:08:25 +00:00
|
|
|
KCmdLineLastOption
|
|
|
|
};
|
2004-06-21 16:25:24 +00:00
|
|
|
|
2004-07-29 17:08:25 +00:00
|
|
|
extern "C"
|
2004-11-17 17:56:22 +00:00
|
|
|
KDE_EXPORT int kdemain( int argc, char* argv[] )
|
2004-06-21 16:25:24 +00:00
|
|
|
{
|
2005-10-03 15:13:54 +00:00
|
|
|
KLocale::setMainCatalog( "kcmkwinrules" );
|
2004-07-29 17:08:25 +00:00
|
|
|
KCmdLineArgs::init( argc, argv, "kwin_rules_dialog", I18N_NOOP( "KWin" ),
|
|
|
|
I18N_NOOP( "KWin helper utility" ), "1.0" );
|
|
|
|
KCmdLineArgs::addCmdLineOptions( options );
|
|
|
|
KApplication app;
|
|
|
|
KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
|
|
|
|
bool id_ok = false;
|
2005-07-28 14:59:42 +00:00
|
|
|
Window id = args->getOption( "wid" ).toULongLong( &id_ok );
|
2005-08-16 17:46:42 +00:00
|
|
|
bool whole_app = args->isSet( "whole-app" );
|
2004-07-29 17:08:25 +00:00
|
|
|
args->clear();
|
|
|
|
if( !id_ok || id == None )
|
|
|
|
{
|
|
|
|
KCmdLineArgs::usage( i18n( "This helper utility is not supposed to be called directly." ));
|
|
|
|
return 1;
|
|
|
|
}
|
2005-08-16 17:46:42 +00:00
|
|
|
return KWinInternal::edit( id, whole_app );
|
2004-06-21 16:25:24 +00:00
|
|
|
}
|