File indexing completed on 2024-12-01 13:37:37

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2004 Lubos Lunak <l.lunak@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #pragma once
0011 
0012 #include <QRectF>
0013 #include <QVector>
0014 #include <netwm_def.h>
0015 
0016 #include "options.h"
0017 #include "utils/common.h"
0018 
0019 class QDebug;
0020 class KConfig;
0021 class KXMessages;
0022 
0023 namespace KWin
0024 {
0025 
0026 class Window;
0027 class Output;
0028 class Rules;
0029 class RuleSettings;
0030 class VirtualDesktop;
0031 
0032 #ifndef KCMRULES // only for kwin core
0033 
0034 class WindowRules
0035 {
0036 public:
0037     explicit WindowRules(const QVector<Rules *> &rules);
0038     WindowRules();
0039     void update(Window *, int selection);
0040     void discardTemporary();
0041     bool contains(const Rules *rule) const;
0042     void remove(Rules *rule);
0043     PlacementPolicy checkPlacement(PlacementPolicy placement) const;
0044     QRectF checkGeometry(QRectF rect, bool init = false) const;
0045     QRectF checkGeometrySafe(QRectF rect, bool init = false) const;
0046     // use 'invalidPoint' with checkPosition, unlike QSize() and QRect(), QPoint() is a valid point
0047     QPointF checkPositionSafe(QPointF pos, bool init = false) const;
0048     QPointF checkPosition(QPointF pos, bool init = false) const;
0049     QSizeF checkSize(QSizeF s, bool init = false) const;
0050     QSizeF checkMinSize(QSizeF s) const;
0051     QSizeF checkMaxSize(QSizeF s) const;
0052     int checkOpacityActive(int s) const;
0053     int checkOpacityInactive(int s) const;
0054     bool checkIgnoreGeometry(bool ignore, bool init = false) const;
0055     QVector<VirtualDesktop *> checkDesktops(QVector<VirtualDesktop *> desktops, bool init = false) const;
0056     Output *checkOutput(Output *output, bool init = false) const;
0057     QStringList checkActivity(QStringList activity, bool init = false) const;
0058     NET::WindowType checkType(NET::WindowType type) const;
0059     MaximizeMode checkMaximize(MaximizeMode mode, bool init = false) const;
0060     bool checkMinimize(bool minimized, bool init = false) const;
0061     ShadeMode checkShade(ShadeMode shade, bool init = false) const;
0062     bool checkSkipTaskbar(bool skip, bool init = false) const;
0063     bool checkSkipPager(bool skip, bool init = false) const;
0064     bool checkSkipSwitcher(bool skip, bool init = false) const;
0065     bool checkKeepAbove(bool above, bool init = false) const;
0066     bool checkKeepBelow(bool below, bool init = false) const;
0067     bool checkFullScreen(bool fs, bool init = false) const;
0068     bool checkNoBorder(bool noborder, bool init = false) const;
0069     QString checkDecoColor(QString schemeFile) const;
0070     bool checkBlockCompositing(bool block) const;
0071     int checkFSP(int fsp) const;
0072     int checkFPP(int fpp) const;
0073     bool checkAcceptFocus(bool focus) const;
0074     bool checkCloseable(bool closeable) const;
0075     bool checkAutogrouping(bool autogroup) const;
0076     bool checkAutogroupInForeground(bool fg) const;
0077     QString checkAutogroupById(QString id) const;
0078     bool checkStrictGeometry(bool strict) const;
0079     QString checkShortcut(QString s, bool init = false) const;
0080     bool checkDisableGlobalShortcuts(bool disable) const;
0081     QString checkDesktopFile(QString desktopFile, bool init = false) const;
0082 
0083 private:
0084     MaximizeMode checkMaximizeVert(MaximizeMode mode, bool init) const;
0085     MaximizeMode checkMaximizeHoriz(MaximizeMode mode, bool init) const;
0086     QVector<Rules *> rules;
0087 };
0088 
0089 #endif
0090 
0091 class Rules
0092 {
0093 public:
0094     Rules();
0095     explicit Rules(const RuleSettings *);
0096     Rules(const QString &, bool temporary);
0097     enum Type {
0098         Position = 1 << 0,
0099         Size = 1 << 1,
0100         Desktops = 1 << 2,
0101         MaximizeVert = 1 << 3,
0102         MaximizeHoriz = 1 << 4,
0103         Minimize = 1 << 5,
0104         Shade = 1 << 6,
0105         SkipTaskbar = 1 << 7,
0106         SkipPager = 1 << 8,
0107         SkipSwitcher = 1 << 9,
0108         Above = 1 << 10,
0109         Below = 1 << 11,
0110         Fullscreen = 1 << 12,
0111         NoBorder = 1 << 13,
0112         OpacityActive = 1 << 14,
0113         OpacityInactive = 1 << 15,
0114         Activity = 1 << 16,
0115         Screen = 1 << 17,
0116         DesktopFile = 1 << 18,
0117         All = 0xffffffff
0118     };
0119     Q_DECLARE_FLAGS(Types, Type)
0120     // All these values are saved to the cfg file, and are also used in kstart!
0121     enum {
0122         Unused = 0,
0123         DontAffect, // use the default value
0124         Force, // force the given value
0125         Apply, // apply only after initial mapping
0126         Remember, // like apply, and remember the value when the window is withdrawn
0127         ApplyNow, // apply immediatelly, then forget the setting
0128         ForceTemporarily // apply and force until the window is withdrawn
0129     };
0130     enum StringMatch {
0131         FirstStringMatch,
0132         UnimportantMatch = FirstStringMatch,
0133         ExactMatch,
0134         SubstringMatch,
0135         RegExpMatch,
0136         LastStringMatch = RegExpMatch
0137     };
0138     enum SetRule {
0139         UnusedSetRule = Unused,
0140         SetRuleDummy = 256 // so that it's at least short int
0141     };
0142     enum ForceRule {
0143         UnusedForceRule = Unused,
0144         ForceRuleDummy = 256 // so that it's at least short int
0145     };
0146     void write(RuleSettings *) const;
0147     bool isEmpty() const;
0148 #ifndef KCMRULES
0149     bool discardUsed(bool withdrawn);
0150     bool match(const Window *c) const;
0151     bool update(Window *, int selection);
0152     bool isTemporary() const;
0153     bool discardTemporary(bool force); // removes if temporary and forced or too old
0154     bool applyPlacement(PlacementPolicy &placement) const;
0155     bool applyGeometry(QRectF &rect, bool init) const;
0156     // use 'invalidPoint' with applyPosition, unlike QSize() and QRect(), QPoint() is a valid point
0157     bool applyPosition(QPointF &pos, bool init) const;
0158     bool applySize(QSizeF &s, bool init) const;
0159     bool applyMinSize(QSizeF &s) const;
0160     bool applyMaxSize(QSizeF &s) const;
0161     bool applyOpacityActive(int &s) const;
0162     bool applyOpacityInactive(int &s) const;
0163     bool applyIgnoreGeometry(bool &ignore, bool init) const;
0164     bool applyDesktops(QVector<VirtualDesktop *> &desktops, bool init) const;
0165     bool applyScreen(int &desktop, bool init) const;
0166     bool applyActivity(QStringList &activity, bool init) const;
0167     bool applyType(NET::WindowType &type) const;
0168     bool applyMaximizeVert(MaximizeMode &mode, bool init) const;
0169     bool applyMaximizeHoriz(MaximizeMode &mode, bool init) const;
0170     bool applyMinimize(bool &minimized, bool init) const;
0171     bool applyShade(ShadeMode &shade, bool init) const;
0172     bool applySkipTaskbar(bool &skip, bool init) const;
0173     bool applySkipPager(bool &skip, bool init) const;
0174     bool applySkipSwitcher(bool &skip, bool init) const;
0175     bool applyKeepAbove(bool &above, bool init) const;
0176     bool applyKeepBelow(bool &below, bool init) const;
0177     bool applyFullScreen(bool &fs, bool init) const;
0178     bool applyNoBorder(bool &noborder, bool init) const;
0179     bool applyDecoColor(QString &schemeFile) const;
0180     bool applyBlockCompositing(bool &block) const;
0181     bool applyFSP(int &fsp) const;
0182     bool applyFPP(int &fpp) const;
0183     bool applyAcceptFocus(bool &focus) const;
0184     bool applyCloseable(bool &closeable) const;
0185     bool applyAutogrouping(bool &autogroup) const;
0186     bool applyAutogroupInForeground(bool &fg) const;
0187     bool applyAutogroupById(QString &id) const;
0188     bool applyStrictGeometry(bool &strict) const;
0189     bool applyShortcut(QString &shortcut, bool init) const;
0190     bool applyDisableGlobalShortcuts(bool &disable) const;
0191     bool applyDesktopFile(QString &desktopFile, bool init) const;
0192 
0193 private:
0194 #endif
0195     bool matchType(NET::WindowType match_type) const;
0196     bool matchWMClass(const QString &match_class, const QString &match_name) const;
0197     bool matchRole(const QString &match_role) const;
0198     bool matchTitle(const QString &match_title) const;
0199     bool matchClientMachine(const QString &match_machine, bool local) const;
0200 #ifdef KCMRULES
0201 private:
0202 #endif
0203     void readFromSettings(const RuleSettings *settings);
0204     static ForceRule convertForceRule(int v);
0205     static QString getDecoColor(const QString &themeName);
0206 #ifndef KCMRULES
0207     static bool checkSetRule(SetRule rule, bool init);
0208     static bool checkForceRule(ForceRule rule);
0209     static bool checkSetStop(SetRule rule);
0210     static bool checkForceStop(ForceRule rule);
0211 #endif
0212     int temporary_state; // e.g. for kstart
0213     QString description;
0214     QString wmclass;
0215     StringMatch wmclassmatch;
0216     bool wmclasscomplete;
0217     QString windowrole;
0218     StringMatch windowrolematch;
0219     QString title;
0220     StringMatch titlematch;
0221     QString clientmachine;
0222     StringMatch clientmachinematch;
0223     NET::WindowTypes types; // types for matching
0224     PlacementPolicy placement;
0225     ForceRule placementrule;
0226     QPoint position;
0227     SetRule positionrule;
0228     QSize size;
0229     SetRule sizerule;
0230     QSize minsize;
0231     ForceRule minsizerule;
0232     QSize maxsize;
0233     ForceRule maxsizerule;
0234     int opacityactive;
0235     ForceRule opacityactiverule;
0236     int opacityinactive;
0237     ForceRule opacityinactiverule;
0238     bool ignoregeometry;
0239     SetRule ignoregeometryrule;
0240     QStringList desktops;
0241     SetRule desktopsrule;
0242     int screen;
0243     SetRule screenrule;
0244     QStringList activity;
0245     SetRule activityrule;
0246     NET::WindowType type; // type for setting
0247     ForceRule typerule;
0248     bool maximizevert;
0249     SetRule maximizevertrule;
0250     bool maximizehoriz;
0251     SetRule maximizehorizrule;
0252     bool minimize;
0253     SetRule minimizerule;
0254     bool shade;
0255     SetRule shaderule;
0256     bool skiptaskbar;
0257     SetRule skiptaskbarrule;
0258     bool skippager;
0259     SetRule skippagerrule;
0260     bool skipswitcher;
0261     SetRule skipswitcherrule;
0262     bool above;
0263     SetRule aboverule;
0264     bool below;
0265     SetRule belowrule;
0266     bool fullscreen;
0267     SetRule fullscreenrule;
0268     bool noborder;
0269     SetRule noborderrule;
0270     QString decocolor;
0271     ForceRule decocolorrule;
0272     bool blockcompositing;
0273     ForceRule blockcompositingrule;
0274     int fsplevel;
0275     int fpplevel;
0276     ForceRule fsplevelrule;
0277     ForceRule fpplevelrule;
0278     bool acceptfocus;
0279     ForceRule acceptfocusrule;
0280     bool closeable;
0281     ForceRule closeablerule;
0282     bool autogroup;
0283     ForceRule autogrouprule;
0284     bool autogroupfg;
0285     ForceRule autogroupfgrule;
0286     QString autogroupid;
0287     ForceRule autogroupidrule;
0288     bool strictgeometry;
0289     ForceRule strictgeometryrule;
0290     QString shortcut;
0291     SetRule shortcutrule;
0292     bool disableglobalshortcuts;
0293     ForceRule disableglobalshortcutsrule;
0294     QString desktopfile;
0295     SetRule desktopfilerule;
0296     friend QDebug &operator<<(QDebug &stream, const Rules *);
0297 };
0298 
0299 #ifndef KCMRULES
0300 class KWIN_EXPORT RuleBook : public QObject
0301 {
0302     Q_OBJECT
0303 public:
0304     explicit RuleBook();
0305     ~RuleBook() override;
0306     WindowRules find(const Window *, bool);
0307     void discardUsed(Window *c, bool withdraw);
0308     void setUpdatesDisabled(bool disable);
0309     bool areUpdatesDisabled() const;
0310     void load();
0311     void edit(Window *c, bool whole_app);
0312     void requestDiskStorage();
0313 
0314     void setConfig(const KSharedConfig::Ptr &config)
0315     {
0316         m_config = config;
0317     }
0318 
0319 private Q_SLOTS:
0320     void temporaryRulesMessage(const QString &);
0321     void cleanupTemporaryRules();
0322     void save();
0323 
0324 private:
0325     void deleteAll();
0326     void initializeX11();
0327     void cleanupX11();
0328     QTimer *m_updateTimer;
0329     bool m_updatesDisabled;
0330     QList<Rules *> m_rules;
0331     std::unique_ptr<KXMessages> m_temporaryRulesMessages;
0332     KSharedConfig::Ptr m_config;
0333 };
0334 
0335 inline bool RuleBook::areUpdatesDisabled() const
0336 {
0337     return m_updatesDisabled;
0338 }
0339 
0340 inline bool Rules::checkSetRule(SetRule rule, bool init)
0341 {
0342     if (rule > (SetRule)DontAffect) { // Unused or DontAffect
0343         if (rule == (SetRule)Force || rule == (SetRule)ApplyNow
0344             || rule == (SetRule)ForceTemporarily || init) {
0345             return true;
0346         }
0347     }
0348     return false;
0349 }
0350 
0351 inline bool Rules::checkForceRule(ForceRule rule)
0352 {
0353     return rule == (ForceRule)Force || rule == (ForceRule)ForceTemporarily;
0354 }
0355 
0356 inline bool Rules::checkSetStop(SetRule rule)
0357 {
0358     return rule != UnusedSetRule;
0359 }
0360 
0361 inline bool Rules::checkForceStop(ForceRule rule)
0362 {
0363     return rule != UnusedForceRule;
0364 }
0365 
0366 inline WindowRules::WindowRules(const QVector<Rules *> &r)
0367     : rules(r)
0368 {
0369 }
0370 
0371 inline WindowRules::WindowRules()
0372 {
0373 }
0374 
0375 inline bool WindowRules::contains(const Rules *rule) const
0376 {
0377     return rules.contains(const_cast<Rules *>(rule));
0378 }
0379 
0380 inline void WindowRules::remove(Rules *rule)
0381 {
0382     rules.removeOne(rule);
0383 }
0384 
0385 #endif
0386 
0387 QDebug &operator<<(QDebug &stream, const Rules *);
0388 
0389 } // namespace
0390 
0391 Q_DECLARE_OPERATORS_FOR_FLAGS(KWin::Rules::Types)