1/********************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5Copyright (C) 2004 Lubos Lunak <l.lunak@kde.org>
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program. If not, see <http://www.gnu.org/licenses/>.
19*********************************************************************/
20
21#ifndef KWIN_RULES_H
22#define KWIN_RULES_H
23
24
25#include <netwm_def.h>
26#include <QRect>
27#include <kconfiggroup.h>
28#include <kdebug.h>
29
30#include "placement.h"
31#include <kdecoration.h>
32#include "options.h"
33#include "utils.h"
34
35class KConfig;
36class KXMessages;
37
38namespace KWin
39{
40
41class Client;
42class Rules;
43
44#ifndef KCMRULES // only for kwin core
45
46class WindowRules
47 : public KDecorationDefines
48{
49public:
50 explicit WindowRules(const QVector< Rules* >& rules);
51 WindowRules();
52 void update(Client*, int selection);
53 void discardTemporary();
54 bool contains(const Rules* rule) const;
55 void remove(Rules* rule);
56 Placement::Policy checkPlacement(Placement::Policy placement) const;
57 QRect checkGeometry(QRect rect, bool init = false) const;
58 // use 'invalidPoint' with checkPosition, unlike QSize() and QRect(), QPoint() is a valid point
59 QPoint checkPosition(QPoint pos, bool init = false) const;
60 QSize checkSize(QSize s, bool init = false) const;
61 QSize checkMinSize(QSize s) const;
62 QSize checkMaxSize(QSize s) const;
63 int checkOpacityActive(int s) const;
64 int checkOpacityInactive(int s) const;
65 bool checkIgnoreGeometry(bool ignore, bool init = false) const;
66 int checkDesktop(int desktop, bool init = false) const;
67 int checkScreen(int screen, bool init = false) const;
68 QString checkActivity(QString activity, bool init = false) const;
69 NET::WindowType checkType(NET::WindowType type) const;
70 MaximizeMode checkMaximize(MaximizeMode mode, bool init = false) const;
71 bool checkMinimize(bool minimized, bool init = false) const;
72 ShadeMode checkShade(ShadeMode shade, bool init = false) const;
73 bool checkSkipTaskbar(bool skip, bool init = false) const;
74 bool checkSkipPager(bool skip, bool init = false) const;
75 bool checkSkipSwitcher(bool skip, bool init = false) const;
76 bool checkKeepAbove(bool above, bool init = false) const;
77 bool checkKeepBelow(bool below, bool init = false) const;
78 bool checkFullScreen(bool fs, bool init = false) const;
79 bool checkNoBorder(bool noborder, bool init = false) const;
80 bool checkBlockCompositing(bool block) const;
81 int checkFSP(int fsp) const;
82 bool checkAcceptFocus(bool focus) const;
83 bool checkCloseable(bool closeable) const;
84 bool checkAutogrouping(bool autogroup) const;
85 bool checkAutogroupInForeground(bool fg) const;
86 QString checkAutogroupById(QString id) const;
87 bool checkStrictGeometry(bool strict) const;
88 QString checkShortcut(QString s, bool init = false) const;
89 bool checkDisableGlobalShortcuts(bool disable) const;
90private:
91 MaximizeMode checkMaximizeVert(MaximizeMode mode, bool init) const;
92 MaximizeMode checkMaximizeHoriz(MaximizeMode mode, bool init) const;
93 QVector< Rules* > rules;
94};
95
96#endif
97
98class Rules
99 : public KDecorationDefines
100{
101public:
102 Rules();
103 explicit Rules(const KConfigGroup&);
104 Rules(const QString&, bool temporary);
105 enum Type {
106 Position = 1<<0, Size = 1<<1, Desktop = 1<<2,
107 MaximizeVert = 1<<3, MaximizeHoriz = 1<<4, Minimize = 1<<5,
108 Shade = 1<<6, SkipTaskbar = 1<<7, SkipPager = 1<<8,
109 SkipSwitcher = 1<<9, Above = 1<<10, Below = 1<<11, Fullscreen = 1<<12,
110 NoBorder = 1<<13, OpacityActive = 1<<14, OpacityInactive = 1<<15,
111 Activity = 1<<16, Screen = 1<<17, All = 0xffffffff
112 };
113 Q_DECLARE_FLAGS(Types, Type)
114 void write(KConfigGroup&) const;
115 bool isEmpty() const;
116#ifndef KCMRULES
117 void discardUsed(bool withdrawn);
118 bool match(const Client* c) const;
119 bool update(Client*, int selection);
120 bool isTemporary() const;
121 bool discardTemporary(bool force); // removes if temporary and forced or too old
122 bool applyPlacement(Placement::Policy& placement) const;
123 bool applyGeometry(QRect& rect, bool init) const;
124 // use 'invalidPoint' with applyPosition, unlike QSize() and QRect(), QPoint() is a valid point
125 bool applyPosition(QPoint& pos, bool init) const;
126 bool applySize(QSize& s, bool init) const;
127 bool applyMinSize(QSize& s) const;
128 bool applyMaxSize(QSize& s) const;
129 bool applyOpacityActive(int& s) const;
130 bool applyOpacityInactive(int& s) const;
131 bool applyIgnoreGeometry(bool& ignore, bool init) const;
132 bool applyDesktop(int& desktop, bool init) const;
133 bool applyScreen(int& desktop, bool init) const;
134 bool applyActivity(QString& activity, bool init) const;
135 bool applyType(NET::WindowType& type) const;
136 bool applyMaximizeVert(MaximizeMode& mode, bool init) const;
137 bool applyMaximizeHoriz(MaximizeMode& mode, bool init) const;
138 bool applyMinimize(bool& minimized, bool init) const;
139 bool applyShade(ShadeMode& shade, bool init) const;
140 bool applySkipTaskbar(bool& skip, bool init) const;
141 bool applySkipPager(bool& skip, bool init) const;
142 bool applySkipSwitcher(bool& skip, bool init) const;
143 bool applyKeepAbove(bool& above, bool init) const;
144 bool applyKeepBelow(bool& below, bool init) const;
145 bool applyFullScreen(bool& fs, bool init) const;
146 bool applyNoBorder(bool& noborder, bool init) const;
147 bool applyBlockCompositing(bool& block) const;
148 bool applyFSP(int& fsp) const;
149 bool applyAcceptFocus(bool& focus) const;
150 bool applyCloseable(bool& closeable) const;
151 bool applyAutogrouping(bool& autogroup) const;
152 bool applyAutogroupInForeground(bool& fg) const;
153 bool applyAutogroupById(QString& id) const;
154 bool applyStrictGeometry(bool& strict) const;
155 bool applyShortcut(QString& shortcut, bool init) const;
156 bool applyDisableGlobalShortcuts(bool& disable) const;
157private:
158#endif
159 bool matchType(NET::WindowType match_type) const;
160 bool matchWMClass(const QByteArray& match_class, const QByteArray& match_name) const;
161 bool matchRole(const QByteArray& match_role) const;
162 bool matchTitle(const QString& match_title) const;
163 bool matchClientMachine(const QByteArray& match_machine, bool local) const;
164 // All these values are saved to the cfg file, and are also used in kstart!
165 enum {
166 Unused = 0,
167 DontAffect, // use the default value
168 Force, // force the given value
169 Apply, // apply only after initial mapping
170 Remember, // like apply, and remember the value when the window is withdrawn
171 ApplyNow, // apply immediatelly, then forget the setting
172 ForceTemporarily // apply and force until the window is withdrawn
173 };
174 enum SetRule {
175 UnusedSetRule = Unused,
176 SetRuleDummy = 256 // so that it's at least short int
177 };
178 enum ForceRule {
179 UnusedForceRule = Unused,
180 ForceRuleDummy = 256 // so that it's at least short int
181 };
182 enum StringMatch {
183 FirstStringMatch,
184 UnimportantMatch = FirstStringMatch,
185 ExactMatch,
186 SubstringMatch,
187 RegExpMatch,
188 LastStringMatch = RegExpMatch
189 };
190 void readFromCfg(const KConfigGroup& cfg);
191 static SetRule readSetRule(const KConfigGroup&, const QString& key);
192 static ForceRule readForceRule(const KConfigGroup&, const QString& key);
193 static NET::WindowType readType(const KConfigGroup&, const QString& key);
194#ifndef KCMRULES
195 static bool checkSetRule(SetRule rule, bool init);
196 static bool checkForceRule(ForceRule rule);
197 static bool checkSetStop(SetRule rule);
198 static bool checkForceStop(ForceRule rule);
199#endif
200 int temporary_state; // e.g. for kstart
201 QString description;
202 QByteArray wmclass;
203 StringMatch wmclassmatch;
204 bool wmclasscomplete;
205 QByteArray windowrole;
206 StringMatch windowrolematch;
207 QString title;
208 StringMatch titlematch;
209 QByteArray clientmachine;
210 StringMatch clientmachinematch;
211 unsigned long types; // types for matching
212 Placement::Policy placement;
213 ForceRule placementrule;
214 QPoint position;
215 SetRule positionrule;
216 QSize size;
217 SetRule sizerule;
218 QSize minsize;
219 ForceRule minsizerule;
220 QSize maxsize;
221 ForceRule maxsizerule;
222 int opacityactive;
223 ForceRule opacityactiverule;
224 int opacityinactive;
225 ForceRule opacityinactiverule;
226 bool ignoregeometry;
227 SetRule ignoregeometryrule;
228 int desktop;
229 SetRule desktoprule;
230 int screen;
231 SetRule screenrule;
232 QString activity;
233 SetRule activityrule;
234 NET::WindowType type; // type for setting
235 ForceRule typerule;
236 bool maximizevert;
237 SetRule maximizevertrule;
238 bool maximizehoriz;
239 SetRule maximizehorizrule;
240 bool minimize;
241 SetRule minimizerule;
242 bool shade;
243 SetRule shaderule;
244 bool skiptaskbar;
245 SetRule skiptaskbarrule;
246 bool skippager;
247 SetRule skippagerrule;
248 bool skipswitcher;
249 SetRule skipswitcherrule;
250 bool above;
251 SetRule aboverule;
252 bool below;
253 SetRule belowrule;
254 bool fullscreen;
255 SetRule fullscreenrule;
256 bool noborder;
257 SetRule noborderrule;
258 bool blockcompositing;
259 ForceRule blockcompositingrule;
260 int fsplevel;
261 ForceRule fsplevelrule;
262 bool acceptfocus;
263 ForceRule acceptfocusrule;
264 bool closeable;
265 ForceRule closeablerule;
266 bool autogroup;
267 ForceRule autogrouprule;
268 bool autogroupfg;
269 ForceRule autogroupfgrule;
270 QString autogroupid;
271 ForceRule autogroupidrule;
272 bool strictgeometry;
273 ForceRule strictgeometryrule;
274 QString shortcut;
275 SetRule shortcutrule;
276 bool disableglobalshortcuts;
277 ForceRule disableglobalshortcutsrule;
278 friend QDebug& operator<<(QDebug& stream, const Rules*);
279};
280
281#ifndef KCMRULES
282class RuleBook : public QObject
283{
284 Q_OBJECT
285public:
286 virtual ~RuleBook();
287 WindowRules find(const Client*, bool);
288 void discardUsed(Client* c, bool withdraw);
289 void setUpdatesDisabled(bool disable);
290 bool areUpdatesDisabled() const;
291 void load();
292 void edit(Client* c, bool whole_app);
293 void requestDiskStorage();
294private Q_SLOTS:
295 void temporaryRulesMessage(const QString&);
296 void cleanupTemporaryRules();
297 void save();
298
299private:
300 void deleteAll();
301 QTimer *m_updateTimer;
302 bool m_updatesDisabled;
303 QList<Rules*> m_rules;
304 QScopedPointer<KXMessages> m_temporaryRulesMessages;
305
306 KWIN_SINGLETON(RuleBook)
307};
308
309inline
310bool RuleBook::areUpdatesDisabled() const
311{
312 return m_updatesDisabled;
313}
314
315inline
316bool Rules::checkSetRule(SetRule rule, bool init)
317{
318 if (rule > (SetRule)DontAffect) { // Unused or DontAffect
319 if (rule == (SetRule)Force || rule == (SetRule) ApplyNow
320 || rule == (SetRule) ForceTemporarily || init)
321 return true;
322 }
323 return false;
324}
325
326inline
327bool Rules::checkForceRule(ForceRule rule)
328{
329 return rule == (ForceRule)Force || rule == (ForceRule) ForceTemporarily;
330}
331
332inline
333bool Rules::checkSetStop(SetRule rule)
334{
335 return rule != UnusedSetRule;
336}
337
338inline
339bool Rules::checkForceStop(ForceRule rule)
340{
341 return rule != UnusedForceRule;
342}
343
344inline
345WindowRules::WindowRules(const QVector< Rules* >& r)
346 : rules(r)
347{
348}
349
350inline
351WindowRules::WindowRules()
352{
353}
354
355inline
356bool WindowRules::contains(const Rules* rule) const
357{
358 return qFind(rules.begin(), rules.end(), rule) != rules.end();
359}
360
361inline
362void WindowRules::remove(Rules* rule)
363{
364 QVector< Rules* >::Iterator pos = qFind(rules.begin(), rules.end(), rule);
365 if (pos != rules.end())
366 rules.erase(pos);
367}
368
369#endif
370
371QDebug& operator<<(QDebug& stream, const Rules*);
372
373} // namespace
374
375Q_DECLARE_OPERATORS_FOR_FLAGS(KWin::Rules::Types)
376
377#endif
378