kwin/kcmkwin/kwinrules/main.cpp

263 lines
10 KiB
C++
Raw Normal View History

/*
* 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <kcmdlineargs.h>
#include <kapplication.h>
#include <kconfig.h>
#include <klocale.h>
#include <kwindowsystem.h>
#include <QtDBus/QtDBus>
#include <X11/Xlib.h>
#include <fixx11h.h>
#include "ruleswidget.h"
#include "../../rules.h"
Improved resolving whether a window is on local machine Most windows use the hostname in WM_CLIENT_MACHINE, but there are windows using the FQDN (for example libreoffice). So instead of "foo" it is "foo.local.net" or similar. The logic so far has been unable to properly determine whether windows with FQDN are on the local system. In order to solve this problem the handling is split out into an own class which stores the information of hostname and whether it is a local machine. This is to not query multiple times. To determine whether the Client is on the local system getaddrinfo is used for the own hostname and the FQDN provided in WM_CLIENT_MACHINE. If one of the queried names matches, we know that it is on the local machine. The old logic to compare the hostname is still used and getaddrinfo is only a fallback in case hostname does not match. The problem with getaddrinfo is, that it accesses the network and by that could block. To circumvent this problem the calls are moved into threads by using QtConcurrent::run. Obviously this brings disadvantages. When trying to resolve whether a Client is on the local machine and a FQDN is used, the information is initially wrong. The new ClientMachine class emits a signal when the information that the system is local becomes available, but for some things this is just too late: * window rules are already gathered * Session Management has already taken place In both cases this is an acceptable loss. For window rules it just needs a proper matching of the machine in case of localhost (remote hosts are not affected). And the case of session management is very academic as it is unlikely that a restoring session contains remote windows. BUG: 308391 FIXED-IN: 4.11 REVIEW: 108235
2013-01-07 07:07:27 +00:00
#include "../../client_machine.h"
#include <QByteArray>
namespace KWin
{
2011-01-30 14:34:42 +00:00
static void loadRules(QList< Rules* >& rules)
{
KConfig _cfg("kwinrulesrc");
KConfigGroup cfg(&_cfg, "General");
int count = cfg.readEntry("count", 0);
for (int i = 1;
i <= count;
++i) {
cfg = KConfigGroup(&_cfg, QString::number(i));
Rules* rule = new Rules(cfg);
rules.append(rule);
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
static void saveRules(const QList< Rules* >& rules)
{
KConfig cfg("kwinrulesrc");
QStringList groups = cfg.groupList();
2011-01-30 14:34:42 +00:00
for (QStringList::ConstIterator it = groups.constBegin();
it != groups.constEnd();
++it)
cfg.deleteGroup(*it);
cfg.group("General").writeEntry("count", rules.count());
int i = 1;
2011-01-30 14:34:42 +00:00
for (QList< Rules* >::ConstIterator it = rules.constBegin();
it != rules.constEnd();
++it) {
KConfigGroup cg(&cfg, QString::number(i));
(*it)->write(cg);
++i;
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
static Rules* findRule(const QList< Rules* >& rules, Window wid, bool whole_app)
{
KWindowInfo info = KWindowSystem::windowInfo(wid,
NET::WMName | NET::WMWindowType,
NET::WM2WindowClass | NET::WM2WindowRole | NET::WM2ClientMachine);
if (!info.valid()) // shouldn't really happen
return NULL;
Improved resolving whether a window is on local machine Most windows use the hostname in WM_CLIENT_MACHINE, but there are windows using the FQDN (for example libreoffice). So instead of "foo" it is "foo.local.net" or similar. The logic so far has been unable to properly determine whether windows with FQDN are on the local system. In order to solve this problem the handling is split out into an own class which stores the information of hostname and whether it is a local machine. This is to not query multiple times. To determine whether the Client is on the local system getaddrinfo is used for the own hostname and the FQDN provided in WM_CLIENT_MACHINE. If one of the queried names matches, we know that it is on the local machine. The old logic to compare the hostname is still used and getaddrinfo is only a fallback in case hostname does not match. The problem with getaddrinfo is, that it accesses the network and by that could block. To circumvent this problem the calls are moved into threads by using QtConcurrent::run. Obviously this brings disadvantages. When trying to resolve whether a Client is on the local machine and a FQDN is used, the information is initially wrong. The new ClientMachine class emits a signal when the information that the system is local becomes available, but for some things this is just too late: * window rules are already gathered * Session Management has already taken place In both cases this is an acceptable loss. For window rules it just needs a proper matching of the machine in case of localhost (remote hosts are not affected). And the case of session management is very academic as it is unlikely that a restoring session contains remote windows. BUG: 308391 FIXED-IN: 4.11 REVIEW: 108235
2013-01-07 07:07:27 +00:00
ClientMachine clientMachine;
clientMachine.resolve(info.win(), info.groupLeader());
QByteArray wmclass_class = info.windowClassClass().toLower();
QByteArray wmclass_name = info.windowClassName().toLower();
QByteArray role = info.windowRole().toLower();
2011-01-30 14:34:42 +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);
QString title = info.name();
Improved resolving whether a window is on local machine Most windows use the hostname in WM_CLIENT_MACHINE, but there are windows using the FQDN (for example libreoffice). So instead of "foo" it is "foo.local.net" or similar. The logic so far has been unable to properly determine whether windows with FQDN are on the local system. In order to solve this problem the handling is split out into an own class which stores the information of hostname and whether it is a local machine. This is to not query multiple times. To determine whether the Client is on the local system getaddrinfo is used for the own hostname and the FQDN provided in WM_CLIENT_MACHINE. If one of the queried names matches, we know that it is on the local machine. The old logic to compare the hostname is still used and getaddrinfo is only a fallback in case hostname does not match. The problem with getaddrinfo is, that it accesses the network and by that could block. To circumvent this problem the calls are moved into threads by using QtConcurrent::run. Obviously this brings disadvantages. When trying to resolve whether a Client is on the local machine and a FQDN is used, the information is initially wrong. The new ClientMachine class emits a signal when the information that the system is local becomes available, but for some things this is just too late: * window rules are already gathered * Session Management has already taken place In both cases this is an acceptable loss. For window rules it just needs a proper matching of the machine in case of localhost (remote hosts are not affected). And the case of session management is very academic as it is unlikely that a restoring session contains remote windows. BUG: 308391 FIXED-IN: 4.11 REVIEW: 108235
2013-01-07 07:07:27 +00:00
QByteArray machine = clientMachine.hostName();
Rules* best_match = NULL;
int match_quality = 0;
2011-01-30 14:34:42 +00:00
for (QList< Rules* >::ConstIterator it = rules.constBegin();
it != rules.constEnd();
++it) {
// try to find an exact match, i.e. not a generic rule
Rules* rule = *it;
int quality = 0;
bool generic = true;
2011-01-30 14:34:42 +00:00
if (rule->wmclassmatch != Rules::ExactMatch)
continue; // too generic
2011-01-30 14:34:42 +00:00
if (!rule->matchWMClass(wmclass_class, wmclass_name))
continue;
// from now on, it matches the app - now try to match for a specific window
2011-01-30 14:34:42 +00:00
if (rule->wmclasscomplete) {
quality += 1;
generic = false; // this can be considered specific enough (old X apps)
2011-01-30 14:34:42 +00:00
}
if (!whole_app) {
if (rule->windowrolematch != Rules::UnimportantMatch) {
quality += rule->windowrolematch == Rules::ExactMatch ? 5 : 1;
generic = false;
2011-01-30 14:34:42 +00:00
}
if (rule->titlematch != Rules::UnimportantMatch) {
quality += rule->titlematch == Rules::ExactMatch ? 3 : 1;
generic = false;
2011-01-30 14:34:42 +00:00
}
if (rule->types != NET::AllTypesMask) {
int bits = 0;
2011-01-30 14:34:42 +00:00
for (unsigned int bit = 1;
bit < 1U << 31;
bit <<= 1)
if (rule->types & bit)
++bits;
2011-01-30 14:34:42 +00:00
if (bits == 1)
quality += 2;
}
2011-01-30 14:34:42 +00:00
if (generic) // ignore generic rules, use only the ones that are for this window
continue;
} else {
if (rule->types == NET::AllTypesMask)
quality += 2;
2011-01-30 14:34:42 +00:00
}
if (!rule->matchType(type)
|| !rule->matchRole(role)
|| !rule->matchTitle(title)
Improved resolving whether a window is on local machine Most windows use the hostname in WM_CLIENT_MACHINE, but there are windows using the FQDN (for example libreoffice). So instead of "foo" it is "foo.local.net" or similar. The logic so far has been unable to properly determine whether windows with FQDN are on the local system. In order to solve this problem the handling is split out into an own class which stores the information of hostname and whether it is a local machine. This is to not query multiple times. To determine whether the Client is on the local system getaddrinfo is used for the own hostname and the FQDN provided in WM_CLIENT_MACHINE. If one of the queried names matches, we know that it is on the local machine. The old logic to compare the hostname is still used and getaddrinfo is only a fallback in case hostname does not match. The problem with getaddrinfo is, that it accesses the network and by that could block. To circumvent this problem the calls are moved into threads by using QtConcurrent::run. Obviously this brings disadvantages. When trying to resolve whether a Client is on the local machine and a FQDN is used, the information is initially wrong. The new ClientMachine class emits a signal when the information that the system is local becomes available, but for some things this is just too late: * window rules are already gathered * Session Management has already taken place In both cases this is an acceptable loss. For window rules it just needs a proper matching of the machine in case of localhost (remote hosts are not affected). And the case of session management is very academic as it is unlikely that a restoring session contains remote windows. BUG: 308391 FIXED-IN: 4.11 REVIEW: 108235
2013-01-07 07:07:27 +00:00
|| !rule->matchClientMachine(machine, clientMachine.isLocal()))
continue;
2011-01-30 14:34:42 +00:00
if (quality > match_quality) {
best_match = rule;
match_quality = quality;
}
2011-01-30 14:34:42 +00:00
}
if (best_match != NULL)
return best_match;
Rules* ret = new Rules;
2011-01-30 14:34:42 +00:00
if (whole_app) {
ret->description = i18n("Application settings for %1", QString::fromLatin1(wmclass_class));
// TODO maybe exclude some types? If yes, then also exclude them above
// when searching.
ret->types = NET::AllTypesMask;
ret->titlematch = Rules::UnimportantMatch;
ret->clientmachine = machine; // set, but make unimportant
ret->clientmachinematch = Rules::UnimportantMatch;
ret->windowrolematch = Rules::UnimportantMatch;
2011-01-30 14:34:42 +00:00
if (wmclass_name == wmclass_class) {
ret->wmclasscomplete = false;
ret->wmclass = wmclass_class;
ret->wmclassmatch = Rules::ExactMatch;
2011-01-30 14:34:42 +00:00
} else {
// WM_CLASS components differ - perhaps the app got -name argument
ret->wmclasscomplete = true;
ret->wmclass = wmclass_name + ' ' + wmclass_class;
ret->wmclassmatch = Rules::ExactMatch;
}
2011-01-30 14:34:42 +00:00
return ret;
}
ret->description = i18n("Window settings for %1", QString::fromLatin1(wmclass_class));
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;
ret->clientmachine = machine; // set, but make unimportant
ret->clientmachinematch = Rules::UnimportantMatch;
2011-01-30 14:34:42 +00:00
if (!role.isEmpty()
&& role != "unknown" && role != "unnamed") { // Qt sets this if not specified
ret->windowrole = role;
ret->windowrolematch = Rules::ExactMatch;
2011-01-30 14:34:42 +00:00
if (wmclass_name == wmclass_class) {
ret->wmclasscomplete = false;
ret->wmclass = wmclass_class;
ret->wmclassmatch = Rules::ExactMatch;
2011-01-30 14:34:42 +00:00
} else {
// WM_CLASS components differ - perhaps the app got -name argument
ret->wmclasscomplete = true;
ret->wmclass = wmclass_name + ' ' + wmclass_class;
ret->wmclassmatch = Rules::ExactMatch;
}
2011-01-30 14:34:42 +00:00
} else { // no role set
if (wmclass_name != wmclass_class) {
ret->wmclasscomplete = true;
ret->wmclass = wmclass_name + ' ' + wmclass_class;
ret->wmclassmatch = Rules::ExactMatch;
2011-01-30 14:34:42 +00:00
} 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;
}
}
2011-01-30 14:34:42 +00:00
return ret;
}
2011-01-30 14:34:42 +00:00
static int edit(Window wid, bool whole_app)
{
QList< Rules* > rules;
2011-01-30 14:34:42 +00:00
loadRules(rules);
Rules* orig_rule = findRule(rules, wid, whole_app);
RulesDialog dlg;
if (whole_app)
dlg.setWindowTitle(i18nc("Window caption for the application wide rules dialog", "Edit Application-Specific Settings"));
// dlg.edit() creates new Rules instance if edited
2011-01-30 14:34:42 +00:00
Rules* edited_rule = dlg.edit(orig_rule, wid, true);
if (edited_rule == NULL || edited_rule->isEmpty()) {
rules.removeAll(orig_rule);
delete orig_rule;
2011-01-30 14:34:42 +00:00
if (orig_rule != edited_rule)
delete edited_rule;
2011-01-30 14:34:42 +00:00
} else if (edited_rule != orig_rule) {
int pos = rules.indexOf(orig_rule);
if (pos != -1)
rules[ pos ] = edited_rule;
else
2011-01-30 14:34:42 +00:00
rules.prepend(edited_rule);
delete orig_rule;
2011-01-30 14:34:42 +00:00
}
saveRules(rules);
// Send signal to all kwin instances
QDBusMessage message =
QDBusMessage::createSignal("/KWin", "org.kde.KWin", "reloadConfig");
QDBusConnection::sessionBus().send(message);
return 0;
2011-01-30 14:34:42 +00:00
}
} // namespace
extern "C"
2011-01-30 14:34:42 +00:00
KDE_EXPORT int kdemain(int argc, char* argv[])
{
KCmdLineArgs::init(argc, argv, "kwin_rules_dialog", "kcmkwinrules", ki18n("KWin"), "1.0" ,
ki18n("KWin helper utility"));
KCmdLineOptions options;
options.add("wid <wid>", ki18n("WId of the window for special window settings."));
options.add("whole-app", ki18n("Whether the settings should affect all windows of the application."));
2011-01-30 14:34:42 +00:00
KCmdLineArgs::addCmdLineOptions(options);
KApplication app;
KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
bool id_ok = false;
2011-01-30 14:34:42 +00:00
Window id = args->getOption("wid").toULongLong(&id_ok);
bool whole_app = args->isSet("whole-app");
args->clear();
2011-01-30 14:34:42 +00:00
if (!id_ok || id == None) {
KCmdLineArgs::usageError(i18n("This helper utility is not supposed to be called directly."));
return 1;
}
2011-01-30 14:34:42 +00:00
return KWin::edit(id, whole_app);
}